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
;
908 elf_link_hash_traverse (elf_hash_table (info
),
909 elf_link_renumber_hash_table_dynsyms
,
912 /* There is an unused NULL entry at the head of the table which we
913 must account for in our count even if the table is empty since it
914 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
918 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
922 /* Merge st_other field. */
925 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
926 const Elf_Internal_Sym
*isym
, asection
*sec
,
927 bfd_boolean definition
, bfd_boolean dynamic
)
929 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
931 /* If st_other has a processor-specific meaning, specific
932 code might be needed here. */
933 if (bed
->elf_backend_merge_symbol_attribute
)
934 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
939 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
940 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
942 /* Keep the most constraining visibility. Leave the remainder
943 of the st_other field to elf_backend_merge_symbol_attribute. */
944 if (symvis
- 1 < hvis
- 1)
945 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
948 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
949 && (sec
->flags
& SEC_READONLY
) == 0)
950 h
->protected_def
= 1;
953 /* This function is called when we want to merge a new symbol with an
954 existing symbol. It handles the various cases which arise when we
955 find a definition in a dynamic object, or when there is already a
956 definition in a dynamic object. The new symbol is described by
957 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
958 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
959 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
960 of an old common symbol. We set OVERRIDE if the old symbol is
961 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
962 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
963 to change. By OK to change, we mean that we shouldn't warn if the
964 type or size does change. */
967 _bfd_elf_merge_symbol (bfd
*abfd
,
968 struct bfd_link_info
*info
,
970 Elf_Internal_Sym
*sym
,
973 struct elf_link_hash_entry
**sym_hash
,
975 bfd_boolean
*pold_weak
,
976 unsigned int *pold_alignment
,
978 bfd_boolean
*override
,
979 bfd_boolean
*type_change_ok
,
980 bfd_boolean
*size_change_ok
,
981 bfd_boolean
*matched
)
983 asection
*sec
, *oldsec
;
984 struct elf_link_hash_entry
*h
;
985 struct elf_link_hash_entry
*hi
;
986 struct elf_link_hash_entry
*flip
;
989 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
990 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
991 const struct elf_backend_data
*bed
;
998 bind
= ELF_ST_BIND (sym
->st_info
);
1000 if (! bfd_is_und_section (sec
))
1001 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
1003 h
= ((struct elf_link_hash_entry
*)
1004 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
1009 bed
= get_elf_backend_data (abfd
);
1011 /* NEW_VERSION is the symbol version of the new symbol. */
1012 if (h
->versioned
!= unversioned
)
1014 /* Symbol version is unknown or versioned. */
1015 new_version
= strrchr (name
, ELF_VER_CHR
);
1018 if (h
->versioned
== unknown
)
1020 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
1021 h
->versioned
= versioned_hidden
;
1023 h
->versioned
= versioned
;
1026 if (new_version
[0] == '\0')
1030 h
->versioned
= unversioned
;
1035 /* For merging, we only care about real symbols. But we need to make
1036 sure that indirect symbol dynamic flags are updated. */
1038 while (h
->root
.type
== bfd_link_hash_indirect
1039 || h
->root
.type
== bfd_link_hash_warning
)
1040 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1044 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1048 /* OLD_HIDDEN is true if the existing symbol is only visible
1049 to the symbol with the same symbol version. NEW_HIDDEN is
1050 true if the new symbol is only visible to the symbol with
1051 the same symbol version. */
1052 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1053 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1054 if (!old_hidden
&& !new_hidden
)
1055 /* The new symbol matches the existing symbol if both
1060 /* OLD_VERSION is the symbol version of the existing
1064 if (h
->versioned
>= versioned
)
1065 old_version
= strrchr (h
->root
.root
.string
,
1070 /* The new symbol matches the existing symbol if they
1071 have the same symbol version. */
1072 *matched
= (old_version
== new_version
1073 || (old_version
!= NULL
1074 && new_version
!= NULL
1075 && strcmp (old_version
, new_version
) == 0));
1080 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1085 switch (h
->root
.type
)
1090 case bfd_link_hash_undefined
:
1091 case bfd_link_hash_undefweak
:
1092 oldbfd
= h
->root
.u
.undef
.abfd
;
1095 case bfd_link_hash_defined
:
1096 case bfd_link_hash_defweak
:
1097 oldbfd
= h
->root
.u
.def
.section
->owner
;
1098 oldsec
= h
->root
.u
.def
.section
;
1101 case bfd_link_hash_common
:
1102 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1103 oldsec
= h
->root
.u
.c
.p
->section
;
1105 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1108 if (poldbfd
&& *poldbfd
== NULL
)
1111 /* Differentiate strong and weak symbols. */
1112 newweak
= bind
== STB_WEAK
;
1113 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1114 || h
->root
.type
== bfd_link_hash_undefweak
);
1116 *pold_weak
= oldweak
;
1118 /* This code is for coping with dynamic objects, and is only useful
1119 if we are doing an ELF link. */
1120 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1123 /* We have to check it for every instance since the first few may be
1124 references and not all compilers emit symbol type for undefined
1126 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1128 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1129 respectively, is from a dynamic object. */
1131 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1133 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1134 syms and defined syms in dynamic libraries respectively.
1135 ref_dynamic on the other hand can be set for a symbol defined in
1136 a dynamic library, and def_dynamic may not be set; When the
1137 definition in a dynamic lib is overridden by a definition in the
1138 executable use of the symbol in the dynamic lib becomes a
1139 reference to the executable symbol. */
1142 if (bfd_is_und_section (sec
))
1144 if (bind
!= STB_WEAK
)
1146 h
->ref_dynamic_nonweak
= 1;
1147 hi
->ref_dynamic_nonweak
= 1;
1152 /* Update the existing symbol only if they match. */
1155 hi
->dynamic_def
= 1;
1159 /* If we just created the symbol, mark it as being an ELF symbol.
1160 Other than that, there is nothing to do--there is no merge issue
1161 with a newly defined symbol--so we just return. */
1163 if (h
->root
.type
== bfd_link_hash_new
)
1169 /* In cases involving weak versioned symbols, we may wind up trying
1170 to merge a symbol with itself. Catch that here, to avoid the
1171 confusion that results if we try to override a symbol with
1172 itself. The additional tests catch cases like
1173 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1174 dynamic object, which we do want to handle here. */
1176 && (newweak
|| oldweak
)
1177 && ((abfd
->flags
& DYNAMIC
) == 0
1178 || !h
->def_regular
))
1183 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1184 else if (oldsec
!= NULL
)
1186 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1187 indices used by MIPS ELF. */
1188 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1191 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1192 respectively, appear to be a definition rather than reference. */
1194 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1196 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1197 && h
->root
.type
!= bfd_link_hash_undefweak
1198 && h
->root
.type
!= bfd_link_hash_common
);
1200 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1201 respectively, appear to be a function. */
1203 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1204 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1206 oldfunc
= (h
->type
!= STT_NOTYPE
1207 && bed
->is_function_type (h
->type
));
1209 /* If creating a default indirect symbol ("foo" or "foo@") from a
1210 dynamic versioned definition ("foo@@") skip doing so if there is
1211 an existing regular definition with a different type. We don't
1212 want, for example, a "time" variable in the executable overriding
1213 a "time" function in a shared library. */
1214 if (pold_alignment
== NULL
1218 && (olddef
|| h
->root
.type
== bfd_link_hash_common
)
1219 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1220 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1221 && h
->type
!= STT_NOTYPE
1222 && !(newfunc
&& oldfunc
))
1228 /* Check TLS symbols. We don't check undefined symbols introduced
1229 by "ld -u" which have no type (and oldbfd NULL), and we don't
1230 check symbols from plugins because they also have no type. */
1232 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1233 && (abfd
->flags
& BFD_PLUGIN
) == 0
1234 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1235 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1238 bfd_boolean ntdef
, tdef
;
1239 asection
*ntsec
, *tsec
;
1241 if (h
->type
== STT_TLS
)
1261 (*_bfd_error_handler
)
1262 (_("%s: TLS definition in %B section %A "
1263 "mismatches non-TLS definition in %B section %A"),
1264 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1265 else if (!tdef
&& !ntdef
)
1266 (*_bfd_error_handler
)
1267 (_("%s: TLS reference in %B "
1268 "mismatches non-TLS reference in %B"),
1269 tbfd
, ntbfd
, h
->root
.root
.string
);
1271 (*_bfd_error_handler
)
1272 (_("%s: TLS definition in %B section %A "
1273 "mismatches non-TLS reference in %B"),
1274 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1276 (*_bfd_error_handler
)
1277 (_("%s: TLS reference in %B "
1278 "mismatches non-TLS definition in %B section %A"),
1279 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1281 bfd_set_error (bfd_error_bad_value
);
1285 /* If the old symbol has non-default visibility, we ignore the new
1286 definition from a dynamic object. */
1288 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1289 && !bfd_is_und_section (sec
))
1292 /* Make sure this symbol is dynamic. */
1294 hi
->ref_dynamic
= 1;
1295 /* A protected symbol has external availability. Make sure it is
1296 recorded as dynamic.
1298 FIXME: Should we check type and size for protected symbol? */
1299 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1300 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1305 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1308 /* If the new symbol with non-default visibility comes from a
1309 relocatable file and the old definition comes from a dynamic
1310 object, we remove the old definition. */
1311 if (hi
->root
.type
== bfd_link_hash_indirect
)
1313 /* Handle the case where the old dynamic definition is
1314 default versioned. We need to copy the symbol info from
1315 the symbol with default version to the normal one if it
1316 was referenced before. */
1319 hi
->root
.type
= h
->root
.type
;
1320 h
->root
.type
= bfd_link_hash_indirect
;
1321 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1323 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1324 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1326 /* If the new symbol is hidden or internal, completely undo
1327 any dynamic link state. */
1328 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1329 h
->forced_local
= 0;
1336 /* FIXME: Should we check type and size for protected symbol? */
1346 /* If the old symbol was undefined before, then it will still be
1347 on the undefs list. If the new symbol is undefined or
1348 common, we can't make it bfd_link_hash_new here, because new
1349 undefined or common symbols will be added to the undefs list
1350 by _bfd_generic_link_add_one_symbol. Symbols may not be
1351 added twice to the undefs list. Also, if the new symbol is
1352 undefweak then we don't want to lose the strong undef. */
1353 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1355 h
->root
.type
= bfd_link_hash_undefined
;
1356 h
->root
.u
.undef
.abfd
= abfd
;
1360 h
->root
.type
= bfd_link_hash_new
;
1361 h
->root
.u
.undef
.abfd
= NULL
;
1364 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1366 /* If the new symbol is hidden or internal, completely undo
1367 any dynamic link state. */
1368 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1369 h
->forced_local
= 0;
1375 /* FIXME: Should we check type and size for protected symbol? */
1381 /* If a new weak symbol definition comes from a regular file and the
1382 old symbol comes from a dynamic library, we treat the new one as
1383 strong. Similarly, an old weak symbol definition from a regular
1384 file is treated as strong when the new symbol comes from a dynamic
1385 library. Further, an old weak symbol from a dynamic library is
1386 treated as strong if the new symbol is from a dynamic library.
1387 This reflects the way glibc's ld.so works.
1389 Do this before setting *type_change_ok or *size_change_ok so that
1390 we warn properly when dynamic library symbols are overridden. */
1392 if (newdef
&& !newdyn
&& olddyn
)
1394 if (olddef
&& newdyn
)
1397 /* Allow changes between different types of function symbol. */
1398 if (newfunc
&& oldfunc
)
1399 *type_change_ok
= TRUE
;
1401 /* It's OK to change the type if either the existing symbol or the
1402 new symbol is weak. A type change is also OK if the old symbol
1403 is undefined and the new symbol is defined. */
1408 && h
->root
.type
== bfd_link_hash_undefined
))
1409 *type_change_ok
= TRUE
;
1411 /* It's OK to change the size if either the existing symbol or the
1412 new symbol is weak, or if the old symbol is undefined. */
1415 || h
->root
.type
== bfd_link_hash_undefined
)
1416 *size_change_ok
= TRUE
;
1418 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1419 symbol, respectively, appears to be a common symbol in a dynamic
1420 object. If a symbol appears in an uninitialized section, and is
1421 not weak, and is not a function, then it may be a common symbol
1422 which was resolved when the dynamic object was created. We want
1423 to treat such symbols specially, because they raise special
1424 considerations when setting the symbol size: if the symbol
1425 appears as a common symbol in a regular object, and the size in
1426 the regular object is larger, we must make sure that we use the
1427 larger size. This problematic case can always be avoided in C,
1428 but it must be handled correctly when using Fortran shared
1431 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1432 likewise for OLDDYNCOMMON and OLDDEF.
1434 Note that this test is just a heuristic, and that it is quite
1435 possible to have an uninitialized symbol in a shared object which
1436 is really a definition, rather than a common symbol. This could
1437 lead to some minor confusion when the symbol really is a common
1438 symbol in some regular object. However, I think it will be
1444 && (sec
->flags
& SEC_ALLOC
) != 0
1445 && (sec
->flags
& SEC_LOAD
) == 0
1448 newdyncommon
= TRUE
;
1450 newdyncommon
= FALSE
;
1454 && h
->root
.type
== bfd_link_hash_defined
1456 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1457 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1460 olddyncommon
= TRUE
;
1462 olddyncommon
= FALSE
;
1464 /* We now know everything about the old and new symbols. We ask the
1465 backend to check if we can merge them. */
1466 if (bed
->merge_symbol
!= NULL
)
1468 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1473 /* If both the old and the new symbols look like common symbols in a
1474 dynamic object, set the size of the symbol to the larger of the
1479 && sym
->st_size
!= h
->size
)
1481 /* Since we think we have two common symbols, issue a multiple
1482 common warning if desired. Note that we only warn if the
1483 size is different. If the size is the same, we simply let
1484 the old symbol override the new one as normally happens with
1485 symbols defined in dynamic objects. */
1487 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1488 bfd_link_hash_common
, sym
->st_size
);
1489 if (sym
->st_size
> h
->size
)
1490 h
->size
= sym
->st_size
;
1492 *size_change_ok
= TRUE
;
1495 /* If we are looking at a dynamic object, and we have found a
1496 definition, we need to see if the symbol was already defined by
1497 some other object. If so, we want to use the existing
1498 definition, and we do not want to report a multiple symbol
1499 definition error; we do this by clobbering *PSEC to be
1500 bfd_und_section_ptr.
1502 We treat a common symbol as a definition if the symbol in the
1503 shared library is a function, since common symbols always
1504 represent variables; this can cause confusion in principle, but
1505 any such confusion would seem to indicate an erroneous program or
1506 shared library. We also permit a common symbol in a regular
1507 object to override a weak symbol in a shared object. A common
1508 symbol in executable also overrides a symbol in a shared object. */
1513 || (h
->root
.type
== bfd_link_hash_common
1516 || (!olddyn
&& bfd_link_executable (info
))))))
1520 newdyncommon
= FALSE
;
1522 *psec
= sec
= bfd_und_section_ptr
;
1523 *size_change_ok
= TRUE
;
1525 /* If we get here when the old symbol is a common symbol, then
1526 we are explicitly letting it override a weak symbol or
1527 function in a dynamic object, and we don't want to warn about
1528 a type change. If the old symbol is a defined symbol, a type
1529 change warning may still be appropriate. */
1531 if (h
->root
.type
== bfd_link_hash_common
)
1532 *type_change_ok
= TRUE
;
1535 /* Handle the special case of an old common symbol merging with a
1536 new symbol which looks like a common symbol in a shared object.
1537 We change *PSEC and *PVALUE to make the new symbol look like a
1538 common symbol, and let _bfd_generic_link_add_one_symbol do the
1542 && h
->root
.type
== bfd_link_hash_common
)
1546 newdyncommon
= FALSE
;
1547 *pvalue
= sym
->st_size
;
1548 *psec
= sec
= bed
->common_section (oldsec
);
1549 *size_change_ok
= TRUE
;
1552 /* Skip weak definitions of symbols that are already defined. */
1553 if (newdef
&& olddef
&& newweak
)
1555 /* Don't skip new non-IR weak syms. */
1556 if (!(oldbfd
!= NULL
1557 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1558 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1564 /* Merge st_other. If the symbol already has a dynamic index,
1565 but visibility says it should not be visible, turn it into a
1567 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1568 if (h
->dynindx
!= -1)
1569 switch (ELF_ST_VISIBILITY (h
->other
))
1573 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1578 /* If the old symbol is from a dynamic object, and the new symbol is
1579 a definition which is not from a dynamic object, then the new
1580 symbol overrides the old symbol. Symbols from regular files
1581 always take precedence over symbols from dynamic objects, even if
1582 they are defined after the dynamic object in the link.
1584 As above, we again permit a common symbol in a regular object to
1585 override a definition in a shared object if the shared object
1586 symbol is a function or is weak. */
1591 || (bfd_is_com_section (sec
)
1592 && (oldweak
|| oldfunc
)))
1597 /* Change the hash table entry to undefined, and let
1598 _bfd_generic_link_add_one_symbol do the right thing with the
1601 h
->root
.type
= bfd_link_hash_undefined
;
1602 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1603 *size_change_ok
= TRUE
;
1606 olddyncommon
= FALSE
;
1608 /* We again permit a type change when a common symbol may be
1609 overriding a function. */
1611 if (bfd_is_com_section (sec
))
1615 /* If a common symbol overrides a function, make sure
1616 that it isn't defined dynamically nor has type
1619 h
->type
= STT_NOTYPE
;
1621 *type_change_ok
= TRUE
;
1624 if (hi
->root
.type
== bfd_link_hash_indirect
)
1627 /* This union may have been set to be non-NULL when this symbol
1628 was seen in a dynamic object. We must force the union to be
1629 NULL, so that it is correct for a regular symbol. */
1630 h
->verinfo
.vertree
= NULL
;
1633 /* Handle the special case of a new common symbol merging with an
1634 old symbol that looks like it might be a common symbol defined in
1635 a shared object. Note that we have already handled the case in
1636 which a new common symbol should simply override the definition
1637 in the shared library. */
1640 && bfd_is_com_section (sec
)
1643 /* It would be best if we could set the hash table entry to a
1644 common symbol, but we don't know what to use for the section
1645 or the alignment. */
1646 (*info
->callbacks
->multiple_common
) (info
, &h
->root
, abfd
,
1647 bfd_link_hash_common
, sym
->st_size
);
1649 /* If the presumed common symbol in the dynamic object is
1650 larger, pretend that the new symbol has its size. */
1652 if (h
->size
> *pvalue
)
1655 /* We need to remember the alignment required by the symbol
1656 in the dynamic object. */
1657 BFD_ASSERT (pold_alignment
);
1658 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1661 olddyncommon
= FALSE
;
1663 h
->root
.type
= bfd_link_hash_undefined
;
1664 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1666 *size_change_ok
= TRUE
;
1667 *type_change_ok
= TRUE
;
1669 if (hi
->root
.type
== bfd_link_hash_indirect
)
1672 h
->verinfo
.vertree
= NULL
;
1677 /* Handle the case where we had a versioned symbol in a dynamic
1678 library and now find a definition in a normal object. In this
1679 case, we make the versioned symbol point to the normal one. */
1680 flip
->root
.type
= h
->root
.type
;
1681 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1682 h
->root
.type
= bfd_link_hash_indirect
;
1683 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1684 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1688 flip
->ref_dynamic
= 1;
1695 /* This function is called to create an indirect symbol from the
1696 default for the symbol with the default version if needed. The
1697 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1698 set DYNSYM if the new indirect symbol is dynamic. */
1701 _bfd_elf_add_default_symbol (bfd
*abfd
,
1702 struct bfd_link_info
*info
,
1703 struct elf_link_hash_entry
*h
,
1705 Elf_Internal_Sym
*sym
,
1709 bfd_boolean
*dynsym
)
1711 bfd_boolean type_change_ok
;
1712 bfd_boolean size_change_ok
;
1715 struct elf_link_hash_entry
*hi
;
1716 struct bfd_link_hash_entry
*bh
;
1717 const struct elf_backend_data
*bed
;
1718 bfd_boolean collect
;
1719 bfd_boolean dynamic
;
1720 bfd_boolean override
;
1722 size_t len
, shortlen
;
1724 bfd_boolean matched
;
1726 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1729 /* If this symbol has a version, and it is the default version, we
1730 create an indirect symbol from the default name to the fully
1731 decorated name. This will cause external references which do not
1732 specify a version to be bound to this version of the symbol. */
1733 p
= strchr (name
, ELF_VER_CHR
);
1734 if (h
->versioned
== unknown
)
1738 h
->versioned
= unversioned
;
1743 if (p
[1] != ELF_VER_CHR
)
1745 h
->versioned
= versioned_hidden
;
1749 h
->versioned
= versioned
;
1754 /* PR ld/19073: We may see an unversioned definition after the
1760 bed
= get_elf_backend_data (abfd
);
1761 collect
= bed
->collect
;
1762 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1764 shortlen
= p
- name
;
1765 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1766 if (shortname
== NULL
)
1768 memcpy (shortname
, name
, shortlen
);
1769 shortname
[shortlen
] = '\0';
1771 /* We are going to create a new symbol. Merge it with any existing
1772 symbol with this name. For the purposes of the merge, act as
1773 though we were defining the symbol we just defined, although we
1774 actually going to define an indirect symbol. */
1775 type_change_ok
= FALSE
;
1776 size_change_ok
= FALSE
;
1779 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1780 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1781 &type_change_ok
, &size_change_ok
, &matched
))
1787 if (hi
->def_regular
)
1789 /* If the undecorated symbol will have a version added by a
1790 script different to H, then don't indirect to/from the
1791 undecorated symbol. This isn't ideal because we may not yet
1792 have seen symbol versions, if given by a script on the
1793 command line rather than via --version-script. */
1794 if (hi
->verinfo
.vertree
== NULL
&& info
->version_info
!= NULL
)
1799 = bfd_find_version_for_sym (info
->version_info
,
1800 hi
->root
.root
.string
, &hide
);
1801 if (hi
->verinfo
.vertree
!= NULL
&& hide
)
1803 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
1807 if (hi
->verinfo
.vertree
!= NULL
1808 && strcmp (p
+ 1 + (p
[1] == '@'), hi
->verinfo
.vertree
->name
) != 0)
1814 /* Add the default symbol if not performing a relocatable link. */
1815 if (! bfd_link_relocatable (info
))
1818 if (! (_bfd_generic_link_add_one_symbol
1819 (info
, abfd
, shortname
, BSF_INDIRECT
,
1820 bfd_ind_section_ptr
,
1821 0, name
, FALSE
, collect
, &bh
)))
1823 hi
= (struct elf_link_hash_entry
*) bh
;
1828 /* In this case the symbol named SHORTNAME is overriding the
1829 indirect symbol we want to add. We were planning on making
1830 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1831 is the name without a version. NAME is the fully versioned
1832 name, and it is the default version.
1834 Overriding means that we already saw a definition for the
1835 symbol SHORTNAME in a regular object, and it is overriding
1836 the symbol defined in the dynamic object.
1838 When this happens, we actually want to change NAME, the
1839 symbol we just added, to refer to SHORTNAME. This will cause
1840 references to NAME in the shared object to become references
1841 to SHORTNAME in the regular object. This is what we expect
1842 when we override a function in a shared object: that the
1843 references in the shared object will be mapped to the
1844 definition in the regular object. */
1846 while (hi
->root
.type
== bfd_link_hash_indirect
1847 || hi
->root
.type
== bfd_link_hash_warning
)
1848 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1850 h
->root
.type
= bfd_link_hash_indirect
;
1851 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1855 hi
->ref_dynamic
= 1;
1859 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1864 /* Now set HI to H, so that the following code will set the
1865 other fields correctly. */
1869 /* Check if HI is a warning symbol. */
1870 if (hi
->root
.type
== bfd_link_hash_warning
)
1871 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1873 /* If there is a duplicate definition somewhere, then HI may not
1874 point to an indirect symbol. We will have reported an error to
1875 the user in that case. */
1877 if (hi
->root
.type
== bfd_link_hash_indirect
)
1879 struct elf_link_hash_entry
*ht
;
1881 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1882 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1884 /* A reference to the SHORTNAME symbol from a dynamic library
1885 will be satisfied by the versioned symbol at runtime. In
1886 effect, we have a reference to the versioned symbol. */
1887 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1888 hi
->dynamic_def
|= ht
->dynamic_def
;
1890 /* See if the new flags lead us to realize that the symbol must
1896 if (! bfd_link_executable (info
)
1903 if (hi
->ref_regular
)
1909 /* We also need to define an indirection from the nondefault version
1913 len
= strlen (name
);
1914 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1915 if (shortname
== NULL
)
1917 memcpy (shortname
, name
, shortlen
);
1918 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1920 /* Once again, merge with any existing symbol. */
1921 type_change_ok
= FALSE
;
1922 size_change_ok
= FALSE
;
1924 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1925 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1926 &type_change_ok
, &size_change_ok
, &matched
))
1934 /* Here SHORTNAME is a versioned name, so we don't expect to see
1935 the type of override we do in the case above unless it is
1936 overridden by a versioned definition. */
1937 if (hi
->root
.type
!= bfd_link_hash_defined
1938 && hi
->root
.type
!= bfd_link_hash_defweak
)
1939 (*_bfd_error_handler
)
1940 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1946 if (! (_bfd_generic_link_add_one_symbol
1947 (info
, abfd
, shortname
, BSF_INDIRECT
,
1948 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1950 hi
= (struct elf_link_hash_entry
*) bh
;
1952 /* If there is a duplicate definition somewhere, then HI may not
1953 point to an indirect symbol. We will have reported an error
1954 to the user in that case. */
1956 if (hi
->root
.type
== bfd_link_hash_indirect
)
1958 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1959 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1960 hi
->dynamic_def
|= h
->dynamic_def
;
1962 /* See if the new flags lead us to realize that the symbol
1968 if (! bfd_link_executable (info
)
1974 if (hi
->ref_regular
)
1984 /* This routine is used to export all defined symbols into the dynamic
1985 symbol table. It is called via elf_link_hash_traverse. */
1988 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1990 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
1992 /* Ignore indirect symbols. These are added by the versioning code. */
1993 if (h
->root
.type
== bfd_link_hash_indirect
)
1996 /* Ignore this if we won't export it. */
1997 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
2000 if (h
->dynindx
== -1
2001 && (h
->def_regular
|| h
->ref_regular
)
2002 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
2003 h
->root
.root
.string
))
2005 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2015 /* Look through the symbols which are defined in other shared
2016 libraries and referenced here. Update the list of version
2017 dependencies. This will be put into the .gnu.version_r section.
2018 This function is called via elf_link_hash_traverse. */
2021 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
2024 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
2025 Elf_Internal_Verneed
*t
;
2026 Elf_Internal_Vernaux
*a
;
2029 /* We only care about symbols defined in shared objects with version
2034 || h
->verinfo
.verdef
== NULL
2035 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
2036 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
2039 /* See if we already know about this version. */
2040 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2044 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
2047 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
2048 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
2054 /* This is a new version. Add it to tree we are building. */
2059 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2062 rinfo
->failed
= TRUE
;
2066 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2067 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2068 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2072 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2075 rinfo
->failed
= TRUE
;
2079 /* Note that we are copying a string pointer here, and testing it
2080 above. If bfd_elf_string_from_elf_section is ever changed to
2081 discard the string data when low in memory, this will have to be
2083 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2085 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2086 a
->vna_nextptr
= t
->vn_auxptr
;
2088 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2091 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2098 /* Figure out appropriate versions for all the symbols. We may not
2099 have the version number script until we have read all of the input
2100 files, so until that point we don't know which symbols should be
2101 local. This function is called via elf_link_hash_traverse. */
2104 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2106 struct elf_info_failed
*sinfo
;
2107 struct bfd_link_info
*info
;
2108 const struct elf_backend_data
*bed
;
2109 struct elf_info_failed eif
;
2112 sinfo
= (struct elf_info_failed
*) data
;
2115 /* Fix the symbol flags. */
2118 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2121 sinfo
->failed
= TRUE
;
2125 /* We only need version numbers for symbols defined in regular
2127 if (!h
->def_regular
)
2130 bed
= get_elf_backend_data (info
->output_bfd
);
2131 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2132 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2134 struct bfd_elf_version_tree
*t
;
2137 if (*p
== ELF_VER_CHR
)
2140 /* If there is no version string, we can just return out. */
2144 /* Look for the version. If we find it, it is no longer weak. */
2145 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2147 if (strcmp (t
->name
, p
) == 0)
2151 struct bfd_elf_version_expr
*d
;
2153 len
= p
- h
->root
.root
.string
;
2154 alc
= (char *) bfd_malloc (len
);
2157 sinfo
->failed
= TRUE
;
2160 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2161 alc
[len
- 1] = '\0';
2162 if (alc
[len
- 2] == ELF_VER_CHR
)
2163 alc
[len
- 2] = '\0';
2165 h
->verinfo
.vertree
= t
;
2169 if (t
->globals
.list
!= NULL
)
2170 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2172 /* See if there is anything to force this symbol to
2174 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2176 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2179 && ! info
->export_dynamic
)
2180 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2188 /* If we are building an application, we need to create a
2189 version node for this version. */
2190 if (t
== NULL
&& bfd_link_executable (info
))
2192 struct bfd_elf_version_tree
**pp
;
2195 /* If we aren't going to export this symbol, we don't need
2196 to worry about it. */
2197 if (h
->dynindx
== -1)
2200 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
,
2204 sinfo
->failed
= TRUE
;
2209 t
->name_indx
= (unsigned int) -1;
2213 /* Don't count anonymous version tag. */
2214 if (sinfo
->info
->version_info
!= NULL
2215 && sinfo
->info
->version_info
->vernum
== 0)
2217 for (pp
= &sinfo
->info
->version_info
;
2221 t
->vernum
= version_index
;
2225 h
->verinfo
.vertree
= t
;
2229 /* We could not find the version for a symbol when
2230 generating a shared archive. Return an error. */
2231 (*_bfd_error_handler
)
2232 (_("%B: version node not found for symbol %s"),
2233 info
->output_bfd
, h
->root
.root
.string
);
2234 bfd_set_error (bfd_error_bad_value
);
2235 sinfo
->failed
= TRUE
;
2240 /* If we don't have a version for this symbol, see if we can find
2242 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2247 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2248 h
->root
.root
.string
, &hide
);
2249 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2250 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2256 /* Read and swap the relocs from the section indicated by SHDR. This
2257 may be either a REL or a RELA section. The relocations are
2258 translated into RELA relocations and stored in INTERNAL_RELOCS,
2259 which should have already been allocated to contain enough space.
2260 The EXTERNAL_RELOCS are a buffer where the external form of the
2261 relocations should be stored.
2263 Returns FALSE if something goes wrong. */
2266 elf_link_read_relocs_from_section (bfd
*abfd
,
2268 Elf_Internal_Shdr
*shdr
,
2269 void *external_relocs
,
2270 Elf_Internal_Rela
*internal_relocs
)
2272 const struct elf_backend_data
*bed
;
2273 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2274 const bfd_byte
*erela
;
2275 const bfd_byte
*erelaend
;
2276 Elf_Internal_Rela
*irela
;
2277 Elf_Internal_Shdr
*symtab_hdr
;
2280 /* Position ourselves at the start of the section. */
2281 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2284 /* Read the relocations. */
2285 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2288 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2289 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2291 bed
= get_elf_backend_data (abfd
);
2293 /* Convert the external relocations to the internal format. */
2294 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2295 swap_in
= bed
->s
->swap_reloc_in
;
2296 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2297 swap_in
= bed
->s
->swap_reloca_in
;
2300 bfd_set_error (bfd_error_wrong_format
);
2304 erela
= (const bfd_byte
*) external_relocs
;
2305 erelaend
= erela
+ shdr
->sh_size
;
2306 irela
= internal_relocs
;
2307 while (erela
< erelaend
)
2311 (*swap_in
) (abfd
, erela
, irela
);
2312 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2313 if (bed
->s
->arch_size
== 64)
2317 if ((size_t) r_symndx
>= nsyms
)
2319 (*_bfd_error_handler
)
2320 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2321 " for offset 0x%lx in section `%A'"),
2323 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2324 bfd_set_error (bfd_error_bad_value
);
2328 else if (r_symndx
!= STN_UNDEF
)
2330 (*_bfd_error_handler
)
2331 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2332 " when the object file has no symbol table"),
2334 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2335 bfd_set_error (bfd_error_bad_value
);
2338 irela
+= bed
->s
->int_rels_per_ext_rel
;
2339 erela
+= shdr
->sh_entsize
;
2345 /* Read and swap the relocs for a section O. They may have been
2346 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2347 not NULL, they are used as buffers to read into. They are known to
2348 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2349 the return value is allocated using either malloc or bfd_alloc,
2350 according to the KEEP_MEMORY argument. If O has two relocation
2351 sections (both REL and RELA relocations), then the REL_HDR
2352 relocations will appear first in INTERNAL_RELOCS, followed by the
2353 RELA_HDR relocations. */
2356 _bfd_elf_link_read_relocs (bfd
*abfd
,
2358 void *external_relocs
,
2359 Elf_Internal_Rela
*internal_relocs
,
2360 bfd_boolean keep_memory
)
2362 void *alloc1
= NULL
;
2363 Elf_Internal_Rela
*alloc2
= NULL
;
2364 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2365 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2366 Elf_Internal_Rela
*internal_rela_relocs
;
2368 if (esdo
->relocs
!= NULL
)
2369 return esdo
->relocs
;
2371 if (o
->reloc_count
== 0)
2374 if (internal_relocs
== NULL
)
2378 size
= o
->reloc_count
;
2379 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2381 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2383 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2384 if (internal_relocs
== NULL
)
2388 if (external_relocs
== NULL
)
2390 bfd_size_type size
= 0;
2393 size
+= esdo
->rel
.hdr
->sh_size
;
2395 size
+= esdo
->rela
.hdr
->sh_size
;
2397 alloc1
= bfd_malloc (size
);
2400 external_relocs
= alloc1
;
2403 internal_rela_relocs
= internal_relocs
;
2406 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2410 external_relocs
= (((bfd_byte
*) external_relocs
)
2411 + esdo
->rel
.hdr
->sh_size
);
2412 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2413 * bed
->s
->int_rels_per_ext_rel
);
2417 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2419 internal_rela_relocs
)))
2422 /* Cache the results for next time, if we can. */
2424 esdo
->relocs
= internal_relocs
;
2429 /* Don't free alloc2, since if it was allocated we are passing it
2430 back (under the name of internal_relocs). */
2432 return internal_relocs
;
2440 bfd_release (abfd
, alloc2
);
2447 /* Compute the size of, and allocate space for, REL_HDR which is the
2448 section header for a section containing relocations for O. */
2451 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2452 struct bfd_elf_section_reloc_data
*reldata
)
2454 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2456 /* That allows us to calculate the size of the section. */
2457 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2459 /* The contents field must last into write_object_contents, so we
2460 allocate it with bfd_alloc rather than malloc. Also since we
2461 cannot be sure that the contents will actually be filled in,
2462 we zero the allocated space. */
2463 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2464 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2467 if (reldata
->hashes
== NULL
&& reldata
->count
)
2469 struct elf_link_hash_entry
**p
;
2471 p
= ((struct elf_link_hash_entry
**)
2472 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2476 reldata
->hashes
= p
;
2482 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2483 originated from the section given by INPUT_REL_HDR) to the
2487 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2488 asection
*input_section
,
2489 Elf_Internal_Shdr
*input_rel_hdr
,
2490 Elf_Internal_Rela
*internal_relocs
,
2491 struct elf_link_hash_entry
**rel_hash
2494 Elf_Internal_Rela
*irela
;
2495 Elf_Internal_Rela
*irelaend
;
2497 struct bfd_elf_section_reloc_data
*output_reldata
;
2498 asection
*output_section
;
2499 const struct elf_backend_data
*bed
;
2500 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2501 struct bfd_elf_section_data
*esdo
;
2503 output_section
= input_section
->output_section
;
2505 bed
= get_elf_backend_data (output_bfd
);
2506 esdo
= elf_section_data (output_section
);
2507 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2509 output_reldata
= &esdo
->rel
;
2510 swap_out
= bed
->s
->swap_reloc_out
;
2512 else if (esdo
->rela
.hdr
2513 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2515 output_reldata
= &esdo
->rela
;
2516 swap_out
= bed
->s
->swap_reloca_out
;
2520 (*_bfd_error_handler
)
2521 (_("%B: relocation size mismatch in %B section %A"),
2522 output_bfd
, input_section
->owner
, input_section
);
2523 bfd_set_error (bfd_error_wrong_format
);
2527 erel
= output_reldata
->hdr
->contents
;
2528 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2529 irela
= internal_relocs
;
2530 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2531 * bed
->s
->int_rels_per_ext_rel
);
2532 while (irela
< irelaend
)
2534 (*swap_out
) (output_bfd
, irela
, erel
);
2535 irela
+= bed
->s
->int_rels_per_ext_rel
;
2536 erel
+= input_rel_hdr
->sh_entsize
;
2539 /* Bump the counter, so that we know where to add the next set of
2541 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2546 /* Make weak undefined symbols in PIE dynamic. */
2549 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2550 struct elf_link_hash_entry
*h
)
2552 if (bfd_link_pie (info
)
2554 && h
->root
.type
== bfd_link_hash_undefweak
)
2555 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2560 /* Fix up the flags for a symbol. This handles various cases which
2561 can only be fixed after all the input files are seen. This is
2562 currently called by both adjust_dynamic_symbol and
2563 assign_sym_version, which is unnecessary but perhaps more robust in
2564 the face of future changes. */
2567 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2568 struct elf_info_failed
*eif
)
2570 const struct elf_backend_data
*bed
;
2572 /* If this symbol was mentioned in a non-ELF file, try to set
2573 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2574 permit a non-ELF file to correctly refer to a symbol defined in
2575 an ELF dynamic object. */
2578 while (h
->root
.type
== bfd_link_hash_indirect
)
2579 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2581 if (h
->root
.type
!= bfd_link_hash_defined
2582 && h
->root
.type
!= bfd_link_hash_defweak
)
2585 h
->ref_regular_nonweak
= 1;
2589 if (h
->root
.u
.def
.section
->owner
!= NULL
2590 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2591 == bfd_target_elf_flavour
))
2594 h
->ref_regular_nonweak
= 1;
2600 if (h
->dynindx
== -1
2604 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2613 /* Unfortunately, NON_ELF is only correct if the symbol
2614 was first seen in a non-ELF file. Fortunately, if the symbol
2615 was first seen in an ELF file, we're probably OK unless the
2616 symbol was defined in a non-ELF file. Catch that case here.
2617 FIXME: We're still in trouble if the symbol was first seen in
2618 a dynamic object, and then later in a non-ELF regular object. */
2619 if ((h
->root
.type
== bfd_link_hash_defined
2620 || h
->root
.type
== bfd_link_hash_defweak
)
2622 && (h
->root
.u
.def
.section
->owner
!= NULL
2623 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2624 != bfd_target_elf_flavour
)
2625 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2626 && !h
->def_dynamic
)))
2630 /* Backend specific symbol fixup. */
2631 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2632 if (bed
->elf_backend_fixup_symbol
2633 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2636 /* If this is a final link, and the symbol was defined as a common
2637 symbol in a regular object file, and there was no definition in
2638 any dynamic object, then the linker will have allocated space for
2639 the symbol in a common section but the DEF_REGULAR
2640 flag will not have been set. */
2641 if (h
->root
.type
== bfd_link_hash_defined
2645 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2648 /* If -Bsymbolic was used (which means to bind references to global
2649 symbols to the definition within the shared object), and this
2650 symbol was defined in a regular object, then it actually doesn't
2651 need a PLT entry. Likewise, if the symbol has non-default
2652 visibility. If the symbol has hidden or internal visibility, we
2653 will force it local. */
2655 && bfd_link_pic (eif
->info
)
2656 && is_elf_hash_table (eif
->info
->hash
)
2657 && (SYMBOLIC_BIND (eif
->info
, h
)
2658 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2661 bfd_boolean force_local
;
2663 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2664 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2665 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2668 /* If a weak undefined symbol has non-default visibility, we also
2669 hide it from the dynamic linker. */
2670 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2671 && h
->root
.type
== bfd_link_hash_undefweak
)
2672 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2674 /* If this is a weak defined symbol in a dynamic object, and we know
2675 the real definition in the dynamic object, copy interesting flags
2676 over to the real definition. */
2677 if (h
->u
.weakdef
!= NULL
)
2679 /* If the real definition is defined by a regular object file,
2680 don't do anything special. See the longer description in
2681 _bfd_elf_adjust_dynamic_symbol, below. */
2682 if (h
->u
.weakdef
->def_regular
)
2683 h
->u
.weakdef
= NULL
;
2686 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2688 while (h
->root
.type
== bfd_link_hash_indirect
)
2689 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2691 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2692 || h
->root
.type
== bfd_link_hash_defweak
);
2693 BFD_ASSERT (weakdef
->def_dynamic
);
2694 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2695 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2696 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2703 /* Make the backend pick a good value for a dynamic symbol. This is
2704 called via elf_link_hash_traverse, and also calls itself
2708 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2710 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2712 const struct elf_backend_data
*bed
;
2714 if (! is_elf_hash_table (eif
->info
->hash
))
2717 /* Ignore indirect symbols. These are added by the versioning code. */
2718 if (h
->root
.type
== bfd_link_hash_indirect
)
2721 /* Fix the symbol flags. */
2722 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2725 /* If this symbol does not require a PLT entry, and it is not
2726 defined by a dynamic object, or is not referenced by a regular
2727 object, ignore it. We do have to handle a weak defined symbol,
2728 even if no regular object refers to it, if we decided to add it
2729 to the dynamic symbol table. FIXME: Do we normally need to worry
2730 about symbols which are defined by one dynamic object and
2731 referenced by another one? */
2733 && h
->type
!= STT_GNU_IFUNC
2737 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2739 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2743 /* If we've already adjusted this symbol, don't do it again. This
2744 can happen via a recursive call. */
2745 if (h
->dynamic_adjusted
)
2748 /* Don't look at this symbol again. Note that we must set this
2749 after checking the above conditions, because we may look at a
2750 symbol once, decide not to do anything, and then get called
2751 recursively later after REF_REGULAR is set below. */
2752 h
->dynamic_adjusted
= 1;
2754 /* If this is a weak definition, and we know a real definition, and
2755 the real symbol is not itself defined by a regular object file,
2756 then get a good value for the real definition. We handle the
2757 real symbol first, for the convenience of the backend routine.
2759 Note that there is a confusing case here. If the real definition
2760 is defined by a regular object file, we don't get the real symbol
2761 from the dynamic object, but we do get the weak symbol. If the
2762 processor backend uses a COPY reloc, then if some routine in the
2763 dynamic object changes the real symbol, we will not see that
2764 change in the corresponding weak symbol. This is the way other
2765 ELF linkers work as well, and seems to be a result of the shared
2768 I will clarify this issue. Most SVR4 shared libraries define the
2769 variable _timezone and define timezone as a weak synonym. The
2770 tzset call changes _timezone. If you write
2771 extern int timezone;
2773 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2774 you might expect that, since timezone is a synonym for _timezone,
2775 the same number will print both times. However, if the processor
2776 backend uses a COPY reloc, then actually timezone will be copied
2777 into your process image, and, since you define _timezone
2778 yourself, _timezone will not. Thus timezone and _timezone will
2779 wind up at different memory locations. The tzset call will set
2780 _timezone, leaving timezone unchanged. */
2782 if (h
->u
.weakdef
!= NULL
)
2784 /* If we get to this point, there is an implicit reference to
2785 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2786 h
->u
.weakdef
->ref_regular
= 1;
2788 /* Ensure that the backend adjust_dynamic_symbol function sees
2789 H->U.WEAKDEF before H by recursively calling ourselves. */
2790 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2794 /* If a symbol has no type and no size and does not require a PLT
2795 entry, then we are probably about to do the wrong thing here: we
2796 are probably going to create a COPY reloc for an empty object.
2797 This case can arise when a shared object is built with assembly
2798 code, and the assembly code fails to set the symbol type. */
2800 && h
->type
== STT_NOTYPE
2802 (*_bfd_error_handler
)
2803 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2804 h
->root
.root
.string
);
2806 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2807 bed
= get_elf_backend_data (dynobj
);
2809 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2818 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2822 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2823 struct elf_link_hash_entry
*h
,
2826 unsigned int power_of_two
;
2828 asection
*sec
= h
->root
.u
.def
.section
;
2830 /* The section aligment of definition is the maximum alignment
2831 requirement of symbols defined in the section. Since we don't
2832 know the symbol alignment requirement, we start with the
2833 maximum alignment and check low bits of the symbol address
2834 for the minimum alignment. */
2835 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2836 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2837 while ((h
->root
.u
.def
.value
& mask
) != 0)
2843 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2846 /* Adjust the section alignment if needed. */
2847 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2852 /* We make sure that the symbol will be aligned properly. */
2853 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2855 /* Define the symbol as being at this point in DYNBSS. */
2856 h
->root
.u
.def
.section
= dynbss
;
2857 h
->root
.u
.def
.value
= dynbss
->size
;
2859 /* Increment the size of DYNBSS to make room for the symbol. */
2860 dynbss
->size
+= h
->size
;
2862 /* No error if extern_protected_data is true. */
2863 if (h
->protected_def
2864 && (!info
->extern_protected_data
2865 || (info
->extern_protected_data
< 0
2866 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2867 info
->callbacks
->einfo
2868 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2869 h
->root
.root
.string
);
2874 /* Adjust all external symbols pointing into SEC_MERGE sections
2875 to reflect the object merging within the sections. */
2878 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2882 if ((h
->root
.type
== bfd_link_hash_defined
2883 || h
->root
.type
== bfd_link_hash_defweak
)
2884 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2885 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2887 bfd
*output_bfd
= (bfd
*) data
;
2889 h
->root
.u
.def
.value
=
2890 _bfd_merged_section_offset (output_bfd
,
2891 &h
->root
.u
.def
.section
,
2892 elf_section_data (sec
)->sec_info
,
2893 h
->root
.u
.def
.value
);
2899 /* Returns false if the symbol referred to by H should be considered
2900 to resolve local to the current module, and true if it should be
2901 considered to bind dynamically. */
2904 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2905 struct bfd_link_info
*info
,
2906 bfd_boolean not_local_protected
)
2908 bfd_boolean binding_stays_local_p
;
2909 const struct elf_backend_data
*bed
;
2910 struct elf_link_hash_table
*hash_table
;
2915 while (h
->root
.type
== bfd_link_hash_indirect
2916 || h
->root
.type
== bfd_link_hash_warning
)
2917 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2919 /* If it was forced local, then clearly it's not dynamic. */
2920 if (h
->dynindx
== -1)
2922 if (h
->forced_local
)
2925 /* Identify the cases where name binding rules say that a
2926 visible symbol resolves locally. */
2927 binding_stays_local_p
= (bfd_link_executable (info
)
2928 || SYMBOLIC_BIND (info
, h
));
2930 switch (ELF_ST_VISIBILITY (h
->other
))
2937 hash_table
= elf_hash_table (info
);
2938 if (!is_elf_hash_table (hash_table
))
2941 bed
= get_elf_backend_data (hash_table
->dynobj
);
2943 /* Proper resolution for function pointer equality may require
2944 that these symbols perhaps be resolved dynamically, even though
2945 we should be resolving them to the current module. */
2946 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2947 binding_stays_local_p
= TRUE
;
2954 /* If it isn't defined locally, then clearly it's dynamic. */
2955 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2958 /* Otherwise, the symbol is dynamic if binding rules don't tell
2959 us that it remains local. */
2960 return !binding_stays_local_p
;
2963 /* Return true if the symbol referred to by H should be considered
2964 to resolve local to the current module, and false otherwise. Differs
2965 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2966 undefined symbols. The two functions are virtually identical except
2967 for the place where forced_local and dynindx == -1 are tested. If
2968 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2969 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2970 the symbol is local only for defined symbols.
2971 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2972 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2973 treatment of undefined weak symbols. For those that do not make
2974 undefined weak symbols dynamic, both functions may return false. */
2977 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2978 struct bfd_link_info
*info
,
2979 bfd_boolean local_protected
)
2981 const struct elf_backend_data
*bed
;
2982 struct elf_link_hash_table
*hash_table
;
2984 /* If it's a local sym, of course we resolve locally. */
2988 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2989 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2990 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
2993 /* Common symbols that become definitions don't get the DEF_REGULAR
2994 flag set, so test it first, and don't bail out. */
2995 if (ELF_COMMON_DEF_P (h
))
2997 /* If we don't have a definition in a regular file, then we can't
2998 resolve locally. The sym is either undefined or dynamic. */
2999 else if (!h
->def_regular
)
3002 /* Forced local symbols resolve locally. */
3003 if (h
->forced_local
)
3006 /* As do non-dynamic symbols. */
3007 if (h
->dynindx
== -1)
3010 /* At this point, we know the symbol is defined and dynamic. In an
3011 executable it must resolve locally, likewise when building symbolic
3012 shared libraries. */
3013 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
3016 /* Now deal with defined dynamic symbols in shared libraries. Ones
3017 with default visibility might not resolve locally. */
3018 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
3021 hash_table
= elf_hash_table (info
);
3022 if (!is_elf_hash_table (hash_table
))
3025 bed
= get_elf_backend_data (hash_table
->dynobj
);
3027 /* If extern_protected_data is false, STV_PROTECTED non-function
3028 symbols are local. */
3029 if ((!info
->extern_protected_data
3030 || (info
->extern_protected_data
< 0
3031 && !bed
->extern_protected_data
))
3032 && !bed
->is_function_type (h
->type
))
3035 /* Function pointer equality tests may require that STV_PROTECTED
3036 symbols be treated as dynamic symbols. If the address of a
3037 function not defined in an executable is set to that function's
3038 plt entry in the executable, then the address of the function in
3039 a shared library must also be the plt entry in the executable. */
3040 return local_protected
;
3043 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3044 aligned. Returns the first TLS output section. */
3046 struct bfd_section
*
3047 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
3049 struct bfd_section
*sec
, *tls
;
3050 unsigned int align
= 0;
3052 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3053 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
3057 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
3058 if (sec
->alignment_power
> align
)
3059 align
= sec
->alignment_power
;
3061 elf_hash_table (info
)->tls_sec
= tls
;
3063 /* Ensure the alignment of the first section is the largest alignment,
3064 so that the tls segment starts aligned. */
3066 tls
->alignment_power
= align
;
3071 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3073 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3074 Elf_Internal_Sym
*sym
)
3076 const struct elf_backend_data
*bed
;
3078 /* Local symbols do not count, but target specific ones might. */
3079 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3080 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3083 bed
= get_elf_backend_data (abfd
);
3084 /* Function symbols do not count. */
3085 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3088 /* If the section is undefined, then so is the symbol. */
3089 if (sym
->st_shndx
== SHN_UNDEF
)
3092 /* If the symbol is defined in the common section, then
3093 it is a common definition and so does not count. */
3094 if (bed
->common_definition (sym
))
3097 /* If the symbol is in a target specific section then we
3098 must rely upon the backend to tell us what it is. */
3099 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3100 /* FIXME - this function is not coded yet:
3102 return _bfd_is_global_symbol_definition (abfd, sym);
3104 Instead for now assume that the definition is not global,
3105 Even if this is wrong, at least the linker will behave
3106 in the same way that it used to do. */
3112 /* Search the symbol table of the archive element of the archive ABFD
3113 whose archive map contains a mention of SYMDEF, and determine if
3114 the symbol is defined in this element. */
3116 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3118 Elf_Internal_Shdr
* hdr
;
3122 Elf_Internal_Sym
*isymbuf
;
3123 Elf_Internal_Sym
*isym
;
3124 Elf_Internal_Sym
*isymend
;
3127 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3131 if (! bfd_check_format (abfd
, bfd_object
))
3134 /* Select the appropriate symbol table. If we don't know if the
3135 object file is an IR object, give linker LTO plugin a chance to
3136 get the correct symbol table. */
3137 if (abfd
->plugin_format
== bfd_plugin_yes
3138 #if BFD_SUPPORTS_PLUGINS
3139 || (abfd
->plugin_format
== bfd_plugin_unknown
3140 && bfd_link_plugin_object_p (abfd
))
3144 /* Use the IR symbol table if the object has been claimed by
3146 abfd
= abfd
->plugin_dummy_bfd
;
3147 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3149 else if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3150 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3152 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3154 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3156 /* The sh_info field of the symtab header tells us where the
3157 external symbols start. We don't care about the local symbols. */
3158 if (elf_bad_symtab (abfd
))
3160 extsymcount
= symcount
;
3165 extsymcount
= symcount
- hdr
->sh_info
;
3166 extsymoff
= hdr
->sh_info
;
3169 if (extsymcount
== 0)
3172 /* Read in the symbol table. */
3173 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3175 if (isymbuf
== NULL
)
3178 /* Scan the symbol table looking for SYMDEF. */
3180 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3184 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3189 if (strcmp (name
, symdef
->name
) == 0)
3191 result
= is_global_data_symbol_definition (abfd
, isym
);
3201 /* Add an entry to the .dynamic table. */
3204 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3208 struct elf_link_hash_table
*hash_table
;
3209 const struct elf_backend_data
*bed
;
3211 bfd_size_type newsize
;
3212 bfd_byte
*newcontents
;
3213 Elf_Internal_Dyn dyn
;
3215 hash_table
= elf_hash_table (info
);
3216 if (! is_elf_hash_table (hash_table
))
3219 bed
= get_elf_backend_data (hash_table
->dynobj
);
3220 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3221 BFD_ASSERT (s
!= NULL
);
3223 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3224 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3225 if (newcontents
== NULL
)
3229 dyn
.d_un
.d_val
= val
;
3230 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3233 s
->contents
= newcontents
;
3238 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3239 otherwise just check whether one already exists. Returns -1 on error,
3240 1 if a DT_NEEDED tag already exists, and 0 on success. */
3243 elf_add_dt_needed_tag (bfd
*abfd
,
3244 struct bfd_link_info
*info
,
3248 struct elf_link_hash_table
*hash_table
;
3251 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3254 hash_table
= elf_hash_table (info
);
3255 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3256 if (strindex
== (size_t) -1)
3259 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3262 const struct elf_backend_data
*bed
;
3265 bed
= get_elf_backend_data (hash_table
->dynobj
);
3266 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3268 for (extdyn
= sdyn
->contents
;
3269 extdyn
< sdyn
->contents
+ sdyn
->size
;
3270 extdyn
+= bed
->s
->sizeof_dyn
)
3272 Elf_Internal_Dyn dyn
;
3274 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3275 if (dyn
.d_tag
== DT_NEEDED
3276 && dyn
.d_un
.d_val
== strindex
)
3278 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3286 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3289 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3293 /* We were just checking for existence of the tag. */
3294 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3299 /* Return true if SONAME is on the needed list between NEEDED and STOP
3300 (or the end of list if STOP is NULL), and needed by a library that
3304 on_needed_list (const char *soname
,
3305 struct bfd_link_needed_list
*needed
,
3306 struct bfd_link_needed_list
*stop
)
3308 struct bfd_link_needed_list
*look
;
3309 for (look
= needed
; look
!= stop
; look
= look
->next
)
3310 if (strcmp (soname
, look
->name
) == 0
3311 && ((elf_dyn_lib_class (look
->by
) & DYN_AS_NEEDED
) == 0
3312 /* If needed by a library that itself is not directly
3313 needed, recursively check whether that library is
3314 indirectly needed. Since we add DT_NEEDED entries to
3315 the end of the list, library dependencies appear after
3316 the library. Therefore search prior to the current
3317 LOOK, preventing possible infinite recursion. */
3318 || on_needed_list (elf_dt_name (look
->by
), needed
, look
)))
3324 /* Sort symbol by value, section, and size. */
3326 elf_sort_symbol (const void *arg1
, const void *arg2
)
3328 const struct elf_link_hash_entry
*h1
;
3329 const struct elf_link_hash_entry
*h2
;
3330 bfd_signed_vma vdiff
;
3332 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3333 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3334 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3336 return vdiff
> 0 ? 1 : -1;
3339 int sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3341 return sdiff
> 0 ? 1 : -1;
3343 vdiff
= h1
->size
- h2
->size
;
3344 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3347 /* This function is used to adjust offsets into .dynstr for
3348 dynamic symbols. This is called via elf_link_hash_traverse. */
3351 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3353 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3355 if (h
->dynindx
!= -1)
3356 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3360 /* Assign string offsets in .dynstr, update all structures referencing
3364 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3366 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3367 struct elf_link_local_dynamic_entry
*entry
;
3368 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3369 bfd
*dynobj
= hash_table
->dynobj
;
3372 const struct elf_backend_data
*bed
;
3375 _bfd_elf_strtab_finalize (dynstr
);
3376 size
= _bfd_elf_strtab_size (dynstr
);
3378 bed
= get_elf_backend_data (dynobj
);
3379 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3380 BFD_ASSERT (sdyn
!= NULL
);
3382 /* Update all .dynamic entries referencing .dynstr strings. */
3383 for (extdyn
= sdyn
->contents
;
3384 extdyn
< sdyn
->contents
+ sdyn
->size
;
3385 extdyn
+= bed
->s
->sizeof_dyn
)
3387 Elf_Internal_Dyn dyn
;
3389 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3393 dyn
.d_un
.d_val
= size
;
3403 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3408 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3411 /* Now update local dynamic symbols. */
3412 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3413 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3414 entry
->isym
.st_name
);
3416 /* And the rest of dynamic symbols. */
3417 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3419 /* Adjust version definitions. */
3420 if (elf_tdata (output_bfd
)->cverdefs
)
3425 Elf_Internal_Verdef def
;
3426 Elf_Internal_Verdaux defaux
;
3428 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3432 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3434 p
+= sizeof (Elf_External_Verdef
);
3435 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3437 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3439 _bfd_elf_swap_verdaux_in (output_bfd
,
3440 (Elf_External_Verdaux
*) p
, &defaux
);
3441 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3443 _bfd_elf_swap_verdaux_out (output_bfd
,
3444 &defaux
, (Elf_External_Verdaux
*) p
);
3445 p
+= sizeof (Elf_External_Verdaux
);
3448 while (def
.vd_next
);
3451 /* Adjust version references. */
3452 if (elf_tdata (output_bfd
)->verref
)
3457 Elf_Internal_Verneed need
;
3458 Elf_Internal_Vernaux needaux
;
3460 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3464 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3466 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3467 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3468 (Elf_External_Verneed
*) p
);
3469 p
+= sizeof (Elf_External_Verneed
);
3470 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3472 _bfd_elf_swap_vernaux_in (output_bfd
,
3473 (Elf_External_Vernaux
*) p
, &needaux
);
3474 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3476 _bfd_elf_swap_vernaux_out (output_bfd
,
3478 (Elf_External_Vernaux
*) p
);
3479 p
+= sizeof (Elf_External_Vernaux
);
3482 while (need
.vn_next
);
3488 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3489 The default is to only match when the INPUT and OUTPUT are exactly
3493 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3494 const bfd_target
*output
)
3496 return input
== output
;
3499 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3500 This version is used when different targets for the same architecture
3501 are virtually identical. */
3504 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3505 const bfd_target
*output
)
3507 const struct elf_backend_data
*obed
, *ibed
;
3509 if (input
== output
)
3512 ibed
= xvec_get_elf_backend_data (input
);
3513 obed
= xvec_get_elf_backend_data (output
);
3515 if (ibed
->arch
!= obed
->arch
)
3518 /* If both backends are using this function, deem them compatible. */
3519 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3522 /* Make a special call to the linker "notice" function to tell it that
3523 we are about to handle an as-needed lib, or have finished
3524 processing the lib. */
3527 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3528 struct bfd_link_info
*info
,
3529 enum notice_asneeded_action act
)
3531 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3534 /* Check relocations an ELF object file. */
3537 _bfd_elf_link_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
)
3539 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3540 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
3542 /* If this object is the same format as the output object, and it is
3543 not a shared library, then let the backend look through the
3546 This is required to build global offset table entries and to
3547 arrange for dynamic relocs. It is not required for the
3548 particular common case of linking non PIC code, even when linking
3549 against shared libraries, but unfortunately there is no way of
3550 knowing whether an object file has been compiled PIC or not.
3551 Looking through the relocs is not particularly time consuming.
3552 The problem is that we must either (1) keep the relocs in memory,
3553 which causes the linker to require additional runtime memory or
3554 (2) read the relocs twice from the input file, which wastes time.
3555 This would be a good case for using mmap.
3557 I have no idea how to handle linking PIC code into a file of a
3558 different format. It probably can't be done. */
3559 if ((abfd
->flags
& DYNAMIC
) == 0
3560 && is_elf_hash_table (htab
)
3561 && bed
->check_relocs
!= NULL
3562 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
3563 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
3567 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3569 Elf_Internal_Rela
*internal_relocs
;
3572 /* Don't check relocations in excluded sections. */
3573 if ((o
->flags
& SEC_RELOC
) == 0
3574 || (o
->flags
& SEC_EXCLUDE
) != 0
3575 || o
->reloc_count
== 0
3576 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
3577 && (o
->flags
& SEC_DEBUGGING
) != 0)
3578 || bfd_is_abs_section (o
->output_section
))
3581 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
3583 if (internal_relocs
== NULL
)
3586 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
3588 if (elf_section_data (o
)->relocs
!= internal_relocs
)
3589 free (internal_relocs
);
3599 /* Add symbols from an ELF object file to the linker hash table. */
3602 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3604 Elf_Internal_Ehdr
*ehdr
;
3605 Elf_Internal_Shdr
*hdr
;
3609 struct elf_link_hash_entry
**sym_hash
;
3610 bfd_boolean dynamic
;
3611 Elf_External_Versym
*extversym
= NULL
;
3612 Elf_External_Versym
*ever
;
3613 struct elf_link_hash_entry
*weaks
;
3614 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3615 size_t nondeflt_vers_cnt
= 0;
3616 Elf_Internal_Sym
*isymbuf
= NULL
;
3617 Elf_Internal_Sym
*isym
;
3618 Elf_Internal_Sym
*isymend
;
3619 const struct elf_backend_data
*bed
;
3620 bfd_boolean add_needed
;
3621 struct elf_link_hash_table
*htab
;
3623 void *alloc_mark
= NULL
;
3624 struct bfd_hash_entry
**old_table
= NULL
;
3625 unsigned int old_size
= 0;
3626 unsigned int old_count
= 0;
3627 void *old_tab
= NULL
;
3629 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3630 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3631 void *old_strtab
= NULL
;
3634 bfd_boolean just_syms
;
3636 htab
= elf_hash_table (info
);
3637 bed
= get_elf_backend_data (abfd
);
3639 if ((abfd
->flags
& DYNAMIC
) == 0)
3645 /* You can't use -r against a dynamic object. Also, there's no
3646 hope of using a dynamic object which does not exactly match
3647 the format of the output file. */
3648 if (bfd_link_relocatable (info
)
3649 || !is_elf_hash_table (htab
)
3650 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3652 if (bfd_link_relocatable (info
))
3653 bfd_set_error (bfd_error_invalid_operation
);
3655 bfd_set_error (bfd_error_wrong_format
);
3660 ehdr
= elf_elfheader (abfd
);
3661 if (info
->warn_alternate_em
3662 && bed
->elf_machine_code
!= ehdr
->e_machine
3663 && ((bed
->elf_machine_alt1
!= 0
3664 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3665 || (bed
->elf_machine_alt2
!= 0
3666 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3667 info
->callbacks
->einfo
3668 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3669 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3671 /* As a GNU extension, any input sections which are named
3672 .gnu.warning.SYMBOL are treated as warning symbols for the given
3673 symbol. This differs from .gnu.warning sections, which generate
3674 warnings when they are included in an output file. */
3675 /* PR 12761: Also generate this warning when building shared libraries. */
3676 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3680 name
= bfd_get_section_name (abfd
, s
);
3681 if (CONST_STRNEQ (name
, ".gnu.warning."))
3686 name
+= sizeof ".gnu.warning." - 1;
3688 /* If this is a shared object, then look up the symbol
3689 in the hash table. If it is there, and it is already
3690 been defined, then we will not be using the entry
3691 from this shared object, so we don't need to warn.
3692 FIXME: If we see the definition in a regular object
3693 later on, we will warn, but we shouldn't. The only
3694 fix is to keep track of what warnings we are supposed
3695 to emit, and then handle them all at the end of the
3699 struct elf_link_hash_entry
*h
;
3701 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3703 /* FIXME: What about bfd_link_hash_common? */
3705 && (h
->root
.type
== bfd_link_hash_defined
3706 || h
->root
.type
== bfd_link_hash_defweak
))
3711 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3715 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3720 if (! (_bfd_generic_link_add_one_symbol
3721 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3722 FALSE
, bed
->collect
, NULL
)))
3725 if (bfd_link_executable (info
))
3727 /* Clobber the section size so that the warning does
3728 not get copied into the output file. */
3731 /* Also set SEC_EXCLUDE, so that symbols defined in
3732 the warning section don't get copied to the output. */
3733 s
->flags
|= SEC_EXCLUDE
;
3738 just_syms
= ((s
= abfd
->sections
) != NULL
3739 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3744 /* If we are creating a shared library, create all the dynamic
3745 sections immediately. We need to attach them to something,
3746 so we attach them to this BFD, provided it is the right
3747 format and is not from ld --just-symbols. Always create the
3748 dynamic sections for -E/--dynamic-list. FIXME: If there
3749 are no input BFD's of the same format as the output, we can't
3750 make a shared library. */
3752 && (bfd_link_pic (info
)
3753 || (!bfd_link_relocatable (info
)
3754 && (info
->export_dynamic
|| info
->dynamic
)))
3755 && is_elf_hash_table (htab
)
3756 && info
->output_bfd
->xvec
== abfd
->xvec
3757 && !htab
->dynamic_sections_created
)
3759 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3763 else if (!is_elf_hash_table (htab
))
3767 const char *soname
= NULL
;
3769 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3772 /* ld --just-symbols and dynamic objects don't mix very well.
3773 ld shouldn't allow it. */
3777 /* If this dynamic lib was specified on the command line with
3778 --as-needed in effect, then we don't want to add a DT_NEEDED
3779 tag unless the lib is actually used. Similary for libs brought
3780 in by another lib's DT_NEEDED. When --no-add-needed is used
3781 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3782 any dynamic library in DT_NEEDED tags in the dynamic lib at
3784 add_needed
= (elf_dyn_lib_class (abfd
)
3785 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3786 | DYN_NO_NEEDED
)) == 0;
3788 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3793 unsigned int elfsec
;
3794 unsigned long shlink
;
3796 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3803 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3804 if (elfsec
== SHN_BAD
)
3805 goto error_free_dyn
;
3806 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3808 for (extdyn
= dynbuf
;
3809 extdyn
< dynbuf
+ s
->size
;
3810 extdyn
+= bed
->s
->sizeof_dyn
)
3812 Elf_Internal_Dyn dyn
;
3814 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3815 if (dyn
.d_tag
== DT_SONAME
)
3817 unsigned int tagv
= dyn
.d_un
.d_val
;
3818 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3820 goto error_free_dyn
;
3822 if (dyn
.d_tag
== DT_NEEDED
)
3824 struct bfd_link_needed_list
*n
, **pn
;
3826 unsigned int tagv
= dyn
.d_un
.d_val
;
3828 amt
= sizeof (struct bfd_link_needed_list
);
3829 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3830 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3831 if (n
== NULL
|| fnm
== NULL
)
3832 goto error_free_dyn
;
3833 amt
= strlen (fnm
) + 1;
3834 anm
= (char *) bfd_alloc (abfd
, amt
);
3836 goto error_free_dyn
;
3837 memcpy (anm
, fnm
, amt
);
3841 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3845 if (dyn
.d_tag
== DT_RUNPATH
)
3847 struct bfd_link_needed_list
*n
, **pn
;
3849 unsigned int tagv
= dyn
.d_un
.d_val
;
3851 amt
= sizeof (struct bfd_link_needed_list
);
3852 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3853 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3854 if (n
== NULL
|| fnm
== NULL
)
3855 goto error_free_dyn
;
3856 amt
= strlen (fnm
) + 1;
3857 anm
= (char *) bfd_alloc (abfd
, amt
);
3859 goto error_free_dyn
;
3860 memcpy (anm
, fnm
, amt
);
3864 for (pn
= & runpath
;
3870 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3871 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3873 struct bfd_link_needed_list
*n
, **pn
;
3875 unsigned int tagv
= dyn
.d_un
.d_val
;
3877 amt
= sizeof (struct bfd_link_needed_list
);
3878 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3879 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3880 if (n
== NULL
|| fnm
== NULL
)
3881 goto error_free_dyn
;
3882 amt
= strlen (fnm
) + 1;
3883 anm
= (char *) bfd_alloc (abfd
, amt
);
3885 goto error_free_dyn
;
3886 memcpy (anm
, fnm
, amt
);
3896 if (dyn
.d_tag
== DT_AUDIT
)
3898 unsigned int tagv
= dyn
.d_un
.d_val
;
3899 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3906 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3907 frees all more recently bfd_alloc'd blocks as well. */
3913 struct bfd_link_needed_list
**pn
;
3914 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3919 /* We do not want to include any of the sections in a dynamic
3920 object in the output file. We hack by simply clobbering the
3921 list of sections in the BFD. This could be handled more
3922 cleanly by, say, a new section flag; the existing
3923 SEC_NEVER_LOAD flag is not the one we want, because that one
3924 still implies that the section takes up space in the output
3926 bfd_section_list_clear (abfd
);
3928 /* Find the name to use in a DT_NEEDED entry that refers to this
3929 object. If the object has a DT_SONAME entry, we use it.
3930 Otherwise, if the generic linker stuck something in
3931 elf_dt_name, we use that. Otherwise, we just use the file
3933 if (soname
== NULL
|| *soname
== '\0')
3935 soname
= elf_dt_name (abfd
);
3936 if (soname
== NULL
|| *soname
== '\0')
3937 soname
= bfd_get_filename (abfd
);
3940 /* Save the SONAME because sometimes the linker emulation code
3941 will need to know it. */
3942 elf_dt_name (abfd
) = soname
;
3944 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3948 /* If we have already included this dynamic object in the
3949 link, just ignore it. There is no reason to include a
3950 particular dynamic object more than once. */
3954 /* Save the DT_AUDIT entry for the linker emulation code. */
3955 elf_dt_audit (abfd
) = audit
;
3958 /* If this is a dynamic object, we always link against the .dynsym
3959 symbol table, not the .symtab symbol table. The dynamic linker
3960 will only see the .dynsym symbol table, so there is no reason to
3961 look at .symtab for a dynamic object. */
3963 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3964 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3966 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3968 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3970 /* The sh_info field of the symtab header tells us where the
3971 external symbols start. We don't care about the local symbols at
3973 if (elf_bad_symtab (abfd
))
3975 extsymcount
= symcount
;
3980 extsymcount
= symcount
- hdr
->sh_info
;
3981 extsymoff
= hdr
->sh_info
;
3984 sym_hash
= elf_sym_hashes (abfd
);
3985 if (extsymcount
!= 0)
3987 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3989 if (isymbuf
== NULL
)
3992 if (sym_hash
== NULL
)
3994 /* We store a pointer to the hash table entry for each
3997 amt
*= sizeof (struct elf_link_hash_entry
*);
3998 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
3999 if (sym_hash
== NULL
)
4000 goto error_free_sym
;
4001 elf_sym_hashes (abfd
) = sym_hash
;
4007 /* Read in any version definitions. */
4008 if (!_bfd_elf_slurp_version_tables (abfd
,
4009 info
->default_imported_symver
))
4010 goto error_free_sym
;
4012 /* Read in the symbol versions, but don't bother to convert them
4013 to internal format. */
4014 if (elf_dynversym (abfd
) != 0)
4016 Elf_Internal_Shdr
*versymhdr
;
4018 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
4019 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
4020 if (extversym
== NULL
)
4021 goto error_free_sym
;
4022 amt
= versymhdr
->sh_size
;
4023 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
4024 || bfd_bread (extversym
, amt
, abfd
) != amt
)
4025 goto error_free_vers
;
4029 /* If we are loading an as-needed shared lib, save the symbol table
4030 state before we start adding symbols. If the lib turns out
4031 to be unneeded, restore the state. */
4032 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4037 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
4039 struct bfd_hash_entry
*p
;
4040 struct elf_link_hash_entry
*h
;
4042 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4044 h
= (struct elf_link_hash_entry
*) p
;
4045 entsize
+= htab
->root
.table
.entsize
;
4046 if (h
->root
.type
== bfd_link_hash_warning
)
4047 entsize
+= htab
->root
.table
.entsize
;
4051 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
4052 old_tab
= bfd_malloc (tabsize
+ entsize
);
4053 if (old_tab
== NULL
)
4054 goto error_free_vers
;
4056 /* Remember the current objalloc pointer, so that all mem for
4057 symbols added can later be reclaimed. */
4058 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
4059 if (alloc_mark
== NULL
)
4060 goto error_free_vers
;
4062 /* Make a special call to the linker "notice" function to
4063 tell it that we are about to handle an as-needed lib. */
4064 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
4065 goto error_free_vers
;
4067 /* Clone the symbol table. Remember some pointers into the
4068 symbol table, and dynamic symbol count. */
4069 old_ent
= (char *) old_tab
+ tabsize
;
4070 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
4071 old_undefs
= htab
->root
.undefs
;
4072 old_undefs_tail
= htab
->root
.undefs_tail
;
4073 old_table
= htab
->root
.table
.table
;
4074 old_size
= htab
->root
.table
.size
;
4075 old_count
= htab
->root
.table
.count
;
4076 old_strtab
= _bfd_elf_strtab_save (htab
->dynstr
);
4077 if (old_strtab
== NULL
)
4078 goto error_free_vers
;
4080 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4082 struct bfd_hash_entry
*p
;
4083 struct elf_link_hash_entry
*h
;
4085 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4087 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
4088 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4089 h
= (struct elf_link_hash_entry
*) p
;
4090 if (h
->root
.type
== bfd_link_hash_warning
)
4092 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
4093 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4100 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
4101 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
4103 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
4107 asection
*sec
, *new_sec
;
4110 struct elf_link_hash_entry
*h
;
4111 struct elf_link_hash_entry
*hi
;
4112 bfd_boolean definition
;
4113 bfd_boolean size_change_ok
;
4114 bfd_boolean type_change_ok
;
4115 bfd_boolean new_weakdef
;
4116 bfd_boolean new_weak
;
4117 bfd_boolean old_weak
;
4118 bfd_boolean override
;
4120 bfd_boolean discarded
;
4121 unsigned int old_alignment
;
4123 bfd_boolean matched
;
4127 flags
= BSF_NO_FLAGS
;
4129 value
= isym
->st_value
;
4130 common
= bed
->common_definition (isym
);
4133 bind
= ELF_ST_BIND (isym
->st_info
);
4137 /* This should be impossible, since ELF requires that all
4138 global symbols follow all local symbols, and that sh_info
4139 point to the first global symbol. Unfortunately, Irix 5
4144 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
4152 case STB_GNU_UNIQUE
:
4153 flags
= BSF_GNU_UNIQUE
;
4157 /* Leave it up to the processor backend. */
4161 if (isym
->st_shndx
== SHN_UNDEF
)
4162 sec
= bfd_und_section_ptr
;
4163 else if (isym
->st_shndx
== SHN_ABS
)
4164 sec
= bfd_abs_section_ptr
;
4165 else if (isym
->st_shndx
== SHN_COMMON
)
4167 sec
= bfd_com_section_ptr
;
4168 /* What ELF calls the size we call the value. What ELF
4169 calls the value we call the alignment. */
4170 value
= isym
->st_size
;
4174 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4176 sec
= bfd_abs_section_ptr
;
4177 else if (discarded_section (sec
))
4179 /* Symbols from discarded section are undefined. We keep
4181 sec
= bfd_und_section_ptr
;
4183 isym
->st_shndx
= SHN_UNDEF
;
4185 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4189 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4192 goto error_free_vers
;
4194 if (isym
->st_shndx
== SHN_COMMON
4195 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4197 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4201 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4203 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4205 goto error_free_vers
;
4209 else if (isym
->st_shndx
== SHN_COMMON
4210 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4211 && !bfd_link_relocatable (info
))
4213 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4217 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4218 | SEC_LINKER_CREATED
);
4219 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4221 goto error_free_vers
;
4225 else if (bed
->elf_add_symbol_hook
)
4227 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4229 goto error_free_vers
;
4231 /* The hook function sets the name to NULL if this symbol
4232 should be skipped for some reason. */
4237 /* Sanity check that all possibilities were handled. */
4240 bfd_set_error (bfd_error_bad_value
);
4241 goto error_free_vers
;
4244 /* Silently discard TLS symbols from --just-syms. There's
4245 no way to combine a static TLS block with a new TLS block
4246 for this executable. */
4247 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4248 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4251 if (bfd_is_und_section (sec
)
4252 || bfd_is_com_section (sec
))
4257 size_change_ok
= FALSE
;
4258 type_change_ok
= bed
->type_change_ok
;
4265 if (is_elf_hash_table (htab
))
4267 Elf_Internal_Versym iver
;
4268 unsigned int vernum
= 0;
4273 if (info
->default_imported_symver
)
4274 /* Use the default symbol version created earlier. */
4275 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4280 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4282 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4284 /* If this is a hidden symbol, or if it is not version
4285 1, we append the version name to the symbol name.
4286 However, we do not modify a non-hidden absolute symbol
4287 if it is not a function, because it might be the version
4288 symbol itself. FIXME: What if it isn't? */
4289 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4291 && (!bfd_is_abs_section (sec
)
4292 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4295 size_t namelen
, verlen
, newlen
;
4298 if (isym
->st_shndx
!= SHN_UNDEF
)
4300 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4302 else if (vernum
> 1)
4304 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4310 (*_bfd_error_handler
)
4311 (_("%B: %s: invalid version %u (max %d)"),
4313 elf_tdata (abfd
)->cverdefs
);
4314 bfd_set_error (bfd_error_bad_value
);
4315 goto error_free_vers
;
4320 /* We cannot simply test for the number of
4321 entries in the VERNEED section since the
4322 numbers for the needed versions do not start
4324 Elf_Internal_Verneed
*t
;
4327 for (t
= elf_tdata (abfd
)->verref
;
4331 Elf_Internal_Vernaux
*a
;
4333 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4335 if (a
->vna_other
== vernum
)
4337 verstr
= a
->vna_nodename
;
4346 (*_bfd_error_handler
)
4347 (_("%B: %s: invalid needed version %d"),
4348 abfd
, name
, vernum
);
4349 bfd_set_error (bfd_error_bad_value
);
4350 goto error_free_vers
;
4354 namelen
= strlen (name
);
4355 verlen
= strlen (verstr
);
4356 newlen
= namelen
+ verlen
+ 2;
4357 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4358 && isym
->st_shndx
!= SHN_UNDEF
)
4361 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4362 if (newname
== NULL
)
4363 goto error_free_vers
;
4364 memcpy (newname
, name
, namelen
);
4365 p
= newname
+ namelen
;
4367 /* If this is a defined non-hidden version symbol,
4368 we add another @ to the name. This indicates the
4369 default version of the symbol. */
4370 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4371 && isym
->st_shndx
!= SHN_UNDEF
)
4373 memcpy (p
, verstr
, verlen
+ 1);
4378 /* If this symbol has default visibility and the user has
4379 requested we not re-export it, then mark it as hidden. */
4380 if (!bfd_is_und_section (sec
)
4383 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4384 isym
->st_other
= (STV_HIDDEN
4385 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4387 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4388 sym_hash
, &old_bfd
, &old_weak
,
4389 &old_alignment
, &skip
, &override
,
4390 &type_change_ok
, &size_change_ok
,
4392 goto error_free_vers
;
4397 /* Override a definition only if the new symbol matches the
4399 if (override
&& matched
)
4403 while (h
->root
.type
== bfd_link_hash_indirect
4404 || h
->root
.type
== bfd_link_hash_warning
)
4405 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4407 if (elf_tdata (abfd
)->verdef
!= NULL
4410 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4413 if (! (_bfd_generic_link_add_one_symbol
4414 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4415 (struct bfd_link_hash_entry
**) sym_hash
)))
4416 goto error_free_vers
;
4418 if ((flags
& BSF_GNU_UNIQUE
)
4419 && (abfd
->flags
& DYNAMIC
) == 0
4420 && bfd_get_flavour (info
->output_bfd
) == bfd_target_elf_flavour
)
4421 elf_tdata (info
->output_bfd
)->has_gnu_symbols
|= elf_gnu_symbol_unique
;
4424 /* We need to make sure that indirect symbol dynamic flags are
4427 while (h
->root
.type
== bfd_link_hash_indirect
4428 || h
->root
.type
== bfd_link_hash_warning
)
4429 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4431 /* Setting the index to -3 tells elf_link_output_extsym that
4432 this symbol is defined in a discarded section. */
4438 new_weak
= (flags
& BSF_WEAK
) != 0;
4439 new_weakdef
= FALSE
;
4443 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4444 && is_elf_hash_table (htab
)
4445 && h
->u
.weakdef
== NULL
)
4447 /* Keep a list of all weak defined non function symbols from
4448 a dynamic object, using the weakdef field. Later in this
4449 function we will set the weakdef field to the correct
4450 value. We only put non-function symbols from dynamic
4451 objects on this list, because that happens to be the only
4452 time we need to know the normal symbol corresponding to a
4453 weak symbol, and the information is time consuming to
4454 figure out. If the weakdef field is not already NULL,
4455 then this symbol was already defined by some previous
4456 dynamic object, and we will be using that previous
4457 definition anyhow. */
4459 h
->u
.weakdef
= weaks
;
4464 /* Set the alignment of a common symbol. */
4465 if ((common
|| bfd_is_com_section (sec
))
4466 && h
->root
.type
== bfd_link_hash_common
)
4471 align
= bfd_log2 (isym
->st_value
);
4474 /* The new symbol is a common symbol in a shared object.
4475 We need to get the alignment from the section. */
4476 align
= new_sec
->alignment_power
;
4478 if (align
> old_alignment
)
4479 h
->root
.u
.c
.p
->alignment_power
= align
;
4481 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4484 if (is_elf_hash_table (htab
))
4486 /* Set a flag in the hash table entry indicating the type of
4487 reference or definition we just found. A dynamic symbol
4488 is one which is referenced or defined by both a regular
4489 object and a shared object. */
4490 bfd_boolean dynsym
= FALSE
;
4492 /* Plugin symbols aren't normal. Don't set def_regular or
4493 ref_regular for them, or make them dynamic. */
4494 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4501 if (bind
!= STB_WEAK
)
4502 h
->ref_regular_nonweak
= 1;
4514 /* If the indirect symbol has been forced local, don't
4515 make the real symbol dynamic. */
4516 if ((h
== hi
|| !hi
->forced_local
)
4517 && (bfd_link_dll (info
)
4527 hi
->ref_dynamic
= 1;
4532 hi
->def_dynamic
= 1;
4535 /* If the indirect symbol has been forced local, don't
4536 make the real symbol dynamic. */
4537 if ((h
== hi
|| !hi
->forced_local
)
4540 || (h
->u
.weakdef
!= NULL
4542 && h
->u
.weakdef
->dynindx
!= -1)))
4546 /* Check to see if we need to add an indirect symbol for
4547 the default name. */
4549 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4550 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4551 sec
, value
, &old_bfd
, &dynsym
))
4552 goto error_free_vers
;
4554 /* Check the alignment when a common symbol is involved. This
4555 can change when a common symbol is overridden by a normal
4556 definition or a common symbol is ignored due to the old
4557 normal definition. We need to make sure the maximum
4558 alignment is maintained. */
4559 if ((old_alignment
|| common
)
4560 && h
->root
.type
!= bfd_link_hash_common
)
4562 unsigned int common_align
;
4563 unsigned int normal_align
;
4564 unsigned int symbol_align
;
4568 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4569 || h
->root
.type
== bfd_link_hash_defweak
);
4571 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4572 if (h
->root
.u
.def
.section
->owner
!= NULL
4573 && (h
->root
.u
.def
.section
->owner
->flags
4574 & (DYNAMIC
| BFD_PLUGIN
)) == 0)
4576 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4577 if (normal_align
> symbol_align
)
4578 normal_align
= symbol_align
;
4581 normal_align
= symbol_align
;
4585 common_align
= old_alignment
;
4586 common_bfd
= old_bfd
;
4591 common_align
= bfd_log2 (isym
->st_value
);
4593 normal_bfd
= old_bfd
;
4596 if (normal_align
< common_align
)
4598 /* PR binutils/2735 */
4599 if (normal_bfd
== NULL
)
4600 (*_bfd_error_handler
)
4601 (_("Warning: alignment %u of common symbol `%s' in %B is"
4602 " greater than the alignment (%u) of its section %A"),
4603 common_bfd
, h
->root
.u
.def
.section
,
4604 1 << common_align
, name
, 1 << normal_align
);
4606 (*_bfd_error_handler
)
4607 (_("Warning: alignment %u of symbol `%s' in %B"
4608 " is smaller than %u in %B"),
4609 normal_bfd
, common_bfd
,
4610 1 << normal_align
, name
, 1 << common_align
);
4614 /* Remember the symbol size if it isn't undefined. */
4615 if (isym
->st_size
!= 0
4616 && isym
->st_shndx
!= SHN_UNDEF
4617 && (definition
|| h
->size
== 0))
4620 && h
->size
!= isym
->st_size
4621 && ! size_change_ok
)
4622 (*_bfd_error_handler
)
4623 (_("Warning: size of symbol `%s' changed"
4624 " from %lu in %B to %lu in %B"),
4626 name
, (unsigned long) h
->size
,
4627 (unsigned long) isym
->st_size
);
4629 h
->size
= isym
->st_size
;
4632 /* If this is a common symbol, then we always want H->SIZE
4633 to be the size of the common symbol. The code just above
4634 won't fix the size if a common symbol becomes larger. We
4635 don't warn about a size change here, because that is
4636 covered by --warn-common. Allow changes between different
4638 if (h
->root
.type
== bfd_link_hash_common
)
4639 h
->size
= h
->root
.u
.c
.size
;
4641 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4642 && ((definition
&& !new_weak
)
4643 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4644 || h
->type
== STT_NOTYPE
))
4646 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4648 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4650 if (type
== STT_GNU_IFUNC
4651 && (abfd
->flags
& DYNAMIC
) != 0)
4654 if (h
->type
!= type
)
4656 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4657 (*_bfd_error_handler
)
4658 (_("Warning: type of symbol `%s' changed"
4659 " from %d to %d in %B"),
4660 abfd
, name
, h
->type
, type
);
4666 /* Merge st_other field. */
4667 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4669 /* We don't want to make debug symbol dynamic. */
4671 && (sec
->flags
& SEC_DEBUGGING
)
4672 && !bfd_link_relocatable (info
))
4675 /* Nor should we make plugin symbols dynamic. */
4676 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4681 h
->target_internal
= isym
->st_target_internal
;
4682 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4685 if (definition
&& !dynamic
)
4687 char *p
= strchr (name
, ELF_VER_CHR
);
4688 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4690 /* Queue non-default versions so that .symver x, x@FOO
4691 aliases can be checked. */
4694 amt
= ((isymend
- isym
+ 1)
4695 * sizeof (struct elf_link_hash_entry
*));
4697 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4699 goto error_free_vers
;
4701 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4705 if (dynsym
&& h
->dynindx
== -1)
4707 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4708 goto error_free_vers
;
4709 if (h
->u
.weakdef
!= NULL
4711 && h
->u
.weakdef
->dynindx
== -1)
4713 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4714 goto error_free_vers
;
4717 else if (h
->dynindx
!= -1)
4718 /* If the symbol already has a dynamic index, but
4719 visibility says it should not be visible, turn it into
4721 switch (ELF_ST_VISIBILITY (h
->other
))
4725 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4730 /* Don't add DT_NEEDED for references from the dummy bfd nor
4731 for unmatched symbol. */
4736 && h
->ref_regular_nonweak
4738 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4739 || (h
->ref_dynamic_nonweak
4740 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4741 && !on_needed_list (elf_dt_name (abfd
),
4742 htab
->needed
, NULL
))))
4745 const char *soname
= elf_dt_name (abfd
);
4747 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4748 h
->root
.root
.string
);
4750 /* A symbol from a library loaded via DT_NEEDED of some
4751 other library is referenced by a regular object.
4752 Add a DT_NEEDED entry for it. Issue an error if
4753 --no-add-needed is used and the reference was not
4756 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4758 (*_bfd_error_handler
)
4759 (_("%B: undefined reference to symbol '%s'"),
4761 bfd_set_error (bfd_error_missing_dso
);
4762 goto error_free_vers
;
4765 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4766 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4769 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4771 goto error_free_vers
;
4773 BFD_ASSERT (ret
== 0);
4778 if (extversym
!= NULL
)
4784 if (isymbuf
!= NULL
)
4790 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4794 /* Restore the symbol table. */
4795 old_ent
= (char *) old_tab
+ tabsize
;
4796 memset (elf_sym_hashes (abfd
), 0,
4797 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4798 htab
->root
.table
.table
= old_table
;
4799 htab
->root
.table
.size
= old_size
;
4800 htab
->root
.table
.count
= old_count
;
4801 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4802 htab
->root
.undefs
= old_undefs
;
4803 htab
->root
.undefs_tail
= old_undefs_tail
;
4804 _bfd_elf_strtab_restore (htab
->dynstr
, old_strtab
);
4807 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4809 struct bfd_hash_entry
*p
;
4810 struct elf_link_hash_entry
*h
;
4812 unsigned int alignment_power
;
4814 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4816 h
= (struct elf_link_hash_entry
*) p
;
4817 if (h
->root
.type
== bfd_link_hash_warning
)
4818 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4820 /* Preserve the maximum alignment and size for common
4821 symbols even if this dynamic lib isn't on DT_NEEDED
4822 since it can still be loaded at run time by another
4824 if (h
->root
.type
== bfd_link_hash_common
)
4826 size
= h
->root
.u
.c
.size
;
4827 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4832 alignment_power
= 0;
4834 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4835 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4836 h
= (struct elf_link_hash_entry
*) p
;
4837 if (h
->root
.type
== bfd_link_hash_warning
)
4839 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4840 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4841 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4843 if (h
->root
.type
== bfd_link_hash_common
)
4845 if (size
> h
->root
.u
.c
.size
)
4846 h
->root
.u
.c
.size
= size
;
4847 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4848 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4853 /* Make a special call to the linker "notice" function to
4854 tell it that symbols added for crefs may need to be removed. */
4855 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4856 goto error_free_vers
;
4859 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4861 if (nondeflt_vers
!= NULL
)
4862 free (nondeflt_vers
);
4866 if (old_tab
!= NULL
)
4868 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4869 goto error_free_vers
;
4874 /* Now that all the symbols from this input file are created, if
4875 not performing a relocatable link, handle .symver foo, foo@BAR
4876 such that any relocs against foo become foo@BAR. */
4877 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4881 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4883 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4884 char *shortname
, *p
;
4886 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4888 || (h
->root
.type
!= bfd_link_hash_defined
4889 && h
->root
.type
!= bfd_link_hash_defweak
))
4892 amt
= p
- h
->root
.root
.string
;
4893 shortname
= (char *) bfd_malloc (amt
+ 1);
4895 goto error_free_vers
;
4896 memcpy (shortname
, h
->root
.root
.string
, amt
);
4897 shortname
[amt
] = '\0';
4899 hi
= (struct elf_link_hash_entry
*)
4900 bfd_link_hash_lookup (&htab
->root
, shortname
,
4901 FALSE
, FALSE
, FALSE
);
4903 && hi
->root
.type
== h
->root
.type
4904 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4905 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4907 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4908 hi
->root
.type
= bfd_link_hash_indirect
;
4909 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4910 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4911 sym_hash
= elf_sym_hashes (abfd
);
4913 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4914 if (sym_hash
[symidx
] == hi
)
4916 sym_hash
[symidx
] = h
;
4922 free (nondeflt_vers
);
4923 nondeflt_vers
= NULL
;
4926 /* Now set the weakdefs field correctly for all the weak defined
4927 symbols we found. The only way to do this is to search all the
4928 symbols. Since we only need the information for non functions in
4929 dynamic objects, that's the only time we actually put anything on
4930 the list WEAKS. We need this information so that if a regular
4931 object refers to a symbol defined weakly in a dynamic object, the
4932 real symbol in the dynamic object is also put in the dynamic
4933 symbols; we also must arrange for both symbols to point to the
4934 same memory location. We could handle the general case of symbol
4935 aliasing, but a general symbol alias can only be generated in
4936 assembler code, handling it correctly would be very time
4937 consuming, and other ELF linkers don't handle general aliasing
4941 struct elf_link_hash_entry
**hpp
;
4942 struct elf_link_hash_entry
**hppend
;
4943 struct elf_link_hash_entry
**sorted_sym_hash
;
4944 struct elf_link_hash_entry
*h
;
4947 /* Since we have to search the whole symbol list for each weak
4948 defined symbol, search time for N weak defined symbols will be
4949 O(N^2). Binary search will cut it down to O(NlogN). */
4951 amt
*= sizeof (struct elf_link_hash_entry
*);
4952 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4953 if (sorted_sym_hash
== NULL
)
4955 sym_hash
= sorted_sym_hash
;
4956 hpp
= elf_sym_hashes (abfd
);
4957 hppend
= hpp
+ extsymcount
;
4959 for (; hpp
< hppend
; hpp
++)
4963 && h
->root
.type
== bfd_link_hash_defined
4964 && !bed
->is_function_type (h
->type
))
4972 qsort (sorted_sym_hash
, sym_count
,
4973 sizeof (struct elf_link_hash_entry
*),
4976 while (weaks
!= NULL
)
4978 struct elf_link_hash_entry
*hlook
;
4981 size_t i
, j
, idx
= 0;
4984 weaks
= hlook
->u
.weakdef
;
4985 hlook
->u
.weakdef
= NULL
;
4987 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4988 || hlook
->root
.type
== bfd_link_hash_defweak
4989 || hlook
->root
.type
== bfd_link_hash_common
4990 || hlook
->root
.type
== bfd_link_hash_indirect
);
4991 slook
= hlook
->root
.u
.def
.section
;
4992 vlook
= hlook
->root
.u
.def
.value
;
4998 bfd_signed_vma vdiff
;
5000 h
= sorted_sym_hash
[idx
];
5001 vdiff
= vlook
- h
->root
.u
.def
.value
;
5008 int sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
5018 /* We didn't find a value/section match. */
5022 /* With multiple aliases, or when the weak symbol is already
5023 strongly defined, we have multiple matching symbols and
5024 the binary search above may land on any of them. Step
5025 one past the matching symbol(s). */
5028 h
= sorted_sym_hash
[idx
];
5029 if (h
->root
.u
.def
.section
!= slook
5030 || h
->root
.u
.def
.value
!= vlook
)
5034 /* Now look back over the aliases. Since we sorted by size
5035 as well as value and section, we'll choose the one with
5036 the largest size. */
5039 h
= sorted_sym_hash
[idx
];
5041 /* Stop if value or section doesn't match. */
5042 if (h
->root
.u
.def
.section
!= slook
5043 || h
->root
.u
.def
.value
!= vlook
)
5045 else if (h
!= hlook
)
5047 hlook
->u
.weakdef
= h
;
5049 /* If the weak definition is in the list of dynamic
5050 symbols, make sure the real definition is put
5052 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
5054 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5057 free (sorted_sym_hash
);
5062 /* If the real definition is in the list of dynamic
5063 symbols, make sure the weak definition is put
5064 there as well. If we don't do this, then the
5065 dynamic loader might not merge the entries for the
5066 real definition and the weak definition. */
5067 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
5069 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
5070 goto err_free_sym_hash
;
5077 free (sorted_sym_hash
);
5080 if (bed
->check_directives
5081 && !(*bed
->check_directives
) (abfd
, info
))
5084 if (!info
->check_relocs_after_open_input
5085 && !_bfd_elf_link_check_relocs (abfd
, info
))
5088 /* If this is a non-traditional link, try to optimize the handling
5089 of the .stab/.stabstr sections. */
5091 && ! info
->traditional_format
5092 && is_elf_hash_table (htab
)
5093 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
5097 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
5098 if (stabstr
!= NULL
)
5100 bfd_size_type string_offset
= 0;
5103 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
5104 if (CONST_STRNEQ (stab
->name
, ".stab")
5105 && (!stab
->name
[5] ||
5106 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
5107 && (stab
->flags
& SEC_MERGE
) == 0
5108 && !bfd_is_abs_section (stab
->output_section
))
5110 struct bfd_elf_section_data
*secdata
;
5112 secdata
= elf_section_data (stab
);
5113 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
5114 stabstr
, &secdata
->sec_info
,
5117 if (secdata
->sec_info
)
5118 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
5123 if (is_elf_hash_table (htab
) && add_needed
)
5125 /* Add this bfd to the loaded list. */
5126 struct elf_link_loaded_list
*n
;
5128 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5132 n
->next
= htab
->loaded
;
5139 if (old_tab
!= NULL
)
5141 if (old_strtab
!= NULL
)
5143 if (nondeflt_vers
!= NULL
)
5144 free (nondeflt_vers
);
5145 if (extversym
!= NULL
)
5148 if (isymbuf
!= NULL
)
5154 /* Return the linker hash table entry of a symbol that might be
5155 satisfied by an archive symbol. Return -1 on error. */
5157 struct elf_link_hash_entry
*
5158 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5159 struct bfd_link_info
*info
,
5162 struct elf_link_hash_entry
*h
;
5166 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5170 /* If this is a default version (the name contains @@), look up the
5171 symbol again with only one `@' as well as without the version.
5172 The effect is that references to the symbol with and without the
5173 version will be matched by the default symbol in the archive. */
5175 p
= strchr (name
, ELF_VER_CHR
);
5176 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5179 /* First check with only one `@'. */
5180 len
= strlen (name
);
5181 copy
= (char *) bfd_alloc (abfd
, len
);
5183 return (struct elf_link_hash_entry
*) 0 - 1;
5185 first
= p
- name
+ 1;
5186 memcpy (copy
, name
, first
);
5187 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5189 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5192 /* We also need to check references to the symbol without the
5194 copy
[first
- 1] = '\0';
5195 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5196 FALSE
, FALSE
, TRUE
);
5199 bfd_release (abfd
, copy
);
5203 /* Add symbols from an ELF archive file to the linker hash table. We
5204 don't use _bfd_generic_link_add_archive_symbols because we need to
5205 handle versioned symbols.
5207 Fortunately, ELF archive handling is simpler than that done by
5208 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5209 oddities. In ELF, if we find a symbol in the archive map, and the
5210 symbol is currently undefined, we know that we must pull in that
5213 Unfortunately, we do have to make multiple passes over the symbol
5214 table until nothing further is resolved. */
5217 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5220 unsigned char *included
= NULL
;
5224 const struct elf_backend_data
*bed
;
5225 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5226 (bfd
*, struct bfd_link_info
*, const char *);
5228 if (! bfd_has_map (abfd
))
5230 /* An empty archive is a special case. */
5231 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5233 bfd_set_error (bfd_error_no_armap
);
5237 /* Keep track of all symbols we know to be already defined, and all
5238 files we know to be already included. This is to speed up the
5239 second and subsequent passes. */
5240 c
= bfd_ardata (abfd
)->symdef_count
;
5244 amt
*= sizeof (*included
);
5245 included
= (unsigned char *) bfd_zmalloc (amt
);
5246 if (included
== NULL
)
5249 symdefs
= bfd_ardata (abfd
)->symdefs
;
5250 bed
= get_elf_backend_data (abfd
);
5251 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5264 symdefend
= symdef
+ c
;
5265 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5267 struct elf_link_hash_entry
*h
;
5269 struct bfd_link_hash_entry
*undefs_tail
;
5274 if (symdef
->file_offset
== last
)
5280 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5281 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5287 if (h
->root
.type
== bfd_link_hash_common
)
5289 /* We currently have a common symbol. The archive map contains
5290 a reference to this symbol, so we may want to include it. We
5291 only want to include it however, if this archive element
5292 contains a definition of the symbol, not just another common
5295 Unfortunately some archivers (including GNU ar) will put
5296 declarations of common symbols into their archive maps, as
5297 well as real definitions, so we cannot just go by the archive
5298 map alone. Instead we must read in the element's symbol
5299 table and check that to see what kind of symbol definition
5301 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5304 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5306 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5307 /* Symbol must be defined. Don't check it again. */
5312 /* We need to include this archive member. */
5313 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5314 if (element
== NULL
)
5317 if (! bfd_check_format (element
, bfd_object
))
5320 undefs_tail
= info
->hash
->undefs_tail
;
5322 if (!(*info
->callbacks
5323 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5325 if (!bfd_link_add_symbols (element
, info
))
5328 /* If there are any new undefined symbols, we need to make
5329 another pass through the archive in order to see whether
5330 they can be defined. FIXME: This isn't perfect, because
5331 common symbols wind up on undefs_tail and because an
5332 undefined symbol which is defined later on in this pass
5333 does not require another pass. This isn't a bug, but it
5334 does make the code less efficient than it could be. */
5335 if (undefs_tail
!= info
->hash
->undefs_tail
)
5338 /* Look backward to mark all symbols from this object file
5339 which we have already seen in this pass. */
5343 included
[mark
] = TRUE
;
5348 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5350 /* We mark subsequent symbols from this object file as we go
5351 on through the loop. */
5352 last
= symdef
->file_offset
;
5362 if (included
!= NULL
)
5367 /* Given an ELF BFD, add symbols to the global hash table as
5371 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5373 switch (bfd_get_format (abfd
))
5376 return elf_link_add_object_symbols (abfd
, info
);
5378 return elf_link_add_archive_symbols (abfd
, info
);
5380 bfd_set_error (bfd_error_wrong_format
);
5385 struct hash_codes_info
5387 unsigned long *hashcodes
;
5391 /* This function will be called though elf_link_hash_traverse to store
5392 all hash value of the exported symbols in an array. */
5395 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5397 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5402 /* Ignore indirect symbols. These are added by the versioning code. */
5403 if (h
->dynindx
== -1)
5406 name
= h
->root
.root
.string
;
5407 if (h
->versioned
>= versioned
)
5409 char *p
= strchr (name
, ELF_VER_CHR
);
5412 alc
= (char *) bfd_malloc (p
- name
+ 1);
5418 memcpy (alc
, name
, p
- name
);
5419 alc
[p
- name
] = '\0';
5424 /* Compute the hash value. */
5425 ha
= bfd_elf_hash (name
);
5427 /* Store the found hash value in the array given as the argument. */
5428 *(inf
->hashcodes
)++ = ha
;
5430 /* And store it in the struct so that we can put it in the hash table
5432 h
->u
.elf_hash_value
= ha
;
5440 struct collect_gnu_hash_codes
5443 const struct elf_backend_data
*bed
;
5444 unsigned long int nsyms
;
5445 unsigned long int maskbits
;
5446 unsigned long int *hashcodes
;
5447 unsigned long int *hashval
;
5448 unsigned long int *indx
;
5449 unsigned long int *counts
;
5452 long int min_dynindx
;
5453 unsigned long int bucketcount
;
5454 unsigned long int symindx
;
5455 long int local_indx
;
5456 long int shift1
, shift2
;
5457 unsigned long int mask
;
5461 /* This function will be called though elf_link_hash_traverse to store
5462 all hash value of the exported symbols in an array. */
5465 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5467 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5472 /* Ignore indirect symbols. These are added by the versioning code. */
5473 if (h
->dynindx
== -1)
5476 /* Ignore also local symbols and undefined symbols. */
5477 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5480 name
= h
->root
.root
.string
;
5481 if (h
->versioned
>= versioned
)
5483 char *p
= strchr (name
, ELF_VER_CHR
);
5486 alc
= (char *) bfd_malloc (p
- name
+ 1);
5492 memcpy (alc
, name
, p
- name
);
5493 alc
[p
- name
] = '\0';
5498 /* Compute the hash value. */
5499 ha
= bfd_elf_gnu_hash (name
);
5501 /* Store the found hash value in the array for compute_bucket_count,
5502 and also for .dynsym reordering purposes. */
5503 s
->hashcodes
[s
->nsyms
] = ha
;
5504 s
->hashval
[h
->dynindx
] = ha
;
5506 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5507 s
->min_dynindx
= h
->dynindx
;
5515 /* This function will be called though elf_link_hash_traverse to do
5516 final dynaminc symbol renumbering. */
5519 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5521 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5522 unsigned long int bucket
;
5523 unsigned long int val
;
5525 /* Ignore indirect symbols. */
5526 if (h
->dynindx
== -1)
5529 /* Ignore also local symbols and undefined symbols. */
5530 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5532 if (h
->dynindx
>= s
->min_dynindx
)
5533 h
->dynindx
= s
->local_indx
++;
5537 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5538 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5539 & ((s
->maskbits
>> s
->shift1
) - 1);
5540 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5542 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5543 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5544 if (s
->counts
[bucket
] == 1)
5545 /* Last element terminates the chain. */
5547 bfd_put_32 (s
->output_bfd
, val
,
5548 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5549 --s
->counts
[bucket
];
5550 h
->dynindx
= s
->indx
[bucket
]++;
5554 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5557 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5559 return !(h
->forced_local
5560 || h
->root
.type
== bfd_link_hash_undefined
5561 || h
->root
.type
== bfd_link_hash_undefweak
5562 || ((h
->root
.type
== bfd_link_hash_defined
5563 || h
->root
.type
== bfd_link_hash_defweak
)
5564 && h
->root
.u
.def
.section
->output_section
== NULL
));
5567 /* Array used to determine the number of hash table buckets to use
5568 based on the number of symbols there are. If there are fewer than
5569 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5570 fewer than 37 we use 17 buckets, and so forth. We never use more
5571 than 32771 buckets. */
5573 static const size_t elf_buckets
[] =
5575 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5579 /* Compute bucket count for hashing table. We do not use a static set
5580 of possible tables sizes anymore. Instead we determine for all
5581 possible reasonable sizes of the table the outcome (i.e., the
5582 number of collisions etc) and choose the best solution. The
5583 weighting functions are not too simple to allow the table to grow
5584 without bounds. Instead one of the weighting factors is the size.
5585 Therefore the result is always a good payoff between few collisions
5586 (= short chain lengths) and table size. */
5588 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5589 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5590 unsigned long int nsyms
,
5593 size_t best_size
= 0;
5594 unsigned long int i
;
5596 /* We have a problem here. The following code to optimize the table
5597 size requires an integer type with more the 32 bits. If
5598 BFD_HOST_U_64_BIT is set we know about such a type. */
5599 #ifdef BFD_HOST_U_64_BIT
5604 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5605 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5606 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5607 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5608 unsigned long int *counts
;
5610 unsigned int no_improvement_count
= 0;
5612 /* Possible optimization parameters: if we have NSYMS symbols we say
5613 that the hashing table must at least have NSYMS/4 and at most
5615 minsize
= nsyms
/ 4;
5618 best_size
= maxsize
= nsyms
* 2;
5623 if ((best_size
& 31) == 0)
5627 /* Create array where we count the collisions in. We must use bfd_malloc
5628 since the size could be large. */
5630 amt
*= sizeof (unsigned long int);
5631 counts
= (unsigned long int *) bfd_malloc (amt
);
5635 /* Compute the "optimal" size for the hash table. The criteria is a
5636 minimal chain length. The minor criteria is (of course) the size
5638 for (i
= minsize
; i
< maxsize
; ++i
)
5640 /* Walk through the array of hashcodes and count the collisions. */
5641 BFD_HOST_U_64_BIT max
;
5642 unsigned long int j
;
5643 unsigned long int fact
;
5645 if (gnu_hash
&& (i
& 31) == 0)
5648 memset (counts
, '\0', i
* sizeof (unsigned long int));
5650 /* Determine how often each hash bucket is used. */
5651 for (j
= 0; j
< nsyms
; ++j
)
5652 ++counts
[hashcodes
[j
] % i
];
5654 /* For the weight function we need some information about the
5655 pagesize on the target. This is information need not be 100%
5656 accurate. Since this information is not available (so far) we
5657 define it here to a reasonable default value. If it is crucial
5658 to have a better value some day simply define this value. */
5659 # ifndef BFD_TARGET_PAGESIZE
5660 # define BFD_TARGET_PAGESIZE (4096)
5663 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5665 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5668 /* Variant 1: optimize for short chains. We add the squares
5669 of all the chain lengths (which favors many small chain
5670 over a few long chains). */
5671 for (j
= 0; j
< i
; ++j
)
5672 max
+= counts
[j
] * counts
[j
];
5674 /* This adds penalties for the overall size of the table. */
5675 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5678 /* Variant 2: Optimize a lot more for small table. Here we
5679 also add squares of the size but we also add penalties for
5680 empty slots (the +1 term). */
5681 for (j
= 0; j
< i
; ++j
)
5682 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5684 /* The overall size of the table is considered, but not as
5685 strong as in variant 1, where it is squared. */
5686 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5690 /* Compare with current best results. */
5691 if (max
< best_chlen
)
5695 no_improvement_count
= 0;
5697 /* PR 11843: Avoid futile long searches for the best bucket size
5698 when there are a large number of symbols. */
5699 else if (++no_improvement_count
== 100)
5706 #endif /* defined (BFD_HOST_U_64_BIT) */
5708 /* This is the fallback solution if no 64bit type is available or if we
5709 are not supposed to spend much time on optimizations. We select the
5710 bucket count using a fixed set of numbers. */
5711 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5713 best_size
= elf_buckets
[i
];
5714 if (nsyms
< elf_buckets
[i
+ 1])
5717 if (gnu_hash
&& best_size
< 2)
5724 /* Size any SHT_GROUP section for ld -r. */
5727 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5731 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5732 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5733 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5738 /* Set a default stack segment size. The value in INFO wins. If it
5739 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5740 undefined it is initialized. */
5743 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5744 struct bfd_link_info
*info
,
5745 const char *legacy_symbol
,
5746 bfd_vma default_size
)
5748 struct elf_link_hash_entry
*h
= NULL
;
5750 /* Look for legacy symbol. */
5752 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5753 FALSE
, FALSE
, FALSE
);
5754 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5755 || h
->root
.type
== bfd_link_hash_defweak
)
5757 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5759 /* The symbol has no type if specified on the command line. */
5760 h
->type
= STT_OBJECT
;
5761 if (info
->stacksize
)
5762 (*_bfd_error_handler
) (_("%B: stack size specified and %s set"),
5763 output_bfd
, legacy_symbol
);
5764 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5765 (*_bfd_error_handler
) (_("%B: %s not absolute"),
5766 output_bfd
, legacy_symbol
);
5768 info
->stacksize
= h
->root
.u
.def
.value
;
5771 if (!info
->stacksize
)
5772 /* If the user didn't set a size, or explicitly inhibit the
5773 size, set it now. */
5774 info
->stacksize
= default_size
;
5776 /* Provide the legacy symbol, if it is referenced. */
5777 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5778 || h
->root
.type
== bfd_link_hash_undefweak
))
5780 struct bfd_link_hash_entry
*bh
= NULL
;
5782 if (!(_bfd_generic_link_add_one_symbol
5783 (info
, output_bfd
, legacy_symbol
,
5784 BSF_GLOBAL
, bfd_abs_section_ptr
,
5785 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5786 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5789 h
= (struct elf_link_hash_entry
*) bh
;
5791 h
->type
= STT_OBJECT
;
5797 /* Set up the sizes and contents of the ELF dynamic sections. This is
5798 called by the ELF linker emulation before_allocation routine. We
5799 must set the sizes of the sections before the linker sets the
5800 addresses of the various sections. */
5803 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5806 const char *filter_shlib
,
5808 const char *depaudit
,
5809 const char * const *auxiliary_filters
,
5810 struct bfd_link_info
*info
,
5811 asection
**sinterpptr
)
5815 const struct elf_backend_data
*bed
;
5816 struct elf_info_failed asvinfo
;
5820 soname_indx
= (size_t) -1;
5822 if (!is_elf_hash_table (info
->hash
))
5825 bed
= get_elf_backend_data (output_bfd
);
5827 /* Any syms created from now on start with -1 in
5828 got.refcount/offset and plt.refcount/offset. */
5829 elf_hash_table (info
)->init_got_refcount
5830 = elf_hash_table (info
)->init_got_offset
;
5831 elf_hash_table (info
)->init_plt_refcount
5832 = elf_hash_table (info
)->init_plt_offset
;
5834 if (bfd_link_relocatable (info
)
5835 && !_bfd_elf_size_group_sections (info
))
5838 /* The backend may have to create some sections regardless of whether
5839 we're dynamic or not. */
5840 if (bed
->elf_backend_always_size_sections
5841 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5844 /* Determine any GNU_STACK segment requirements, after the backend
5845 has had a chance to set a default segment size. */
5846 if (info
->execstack
)
5847 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5848 else if (info
->noexecstack
)
5849 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5853 asection
*notesec
= NULL
;
5856 for (inputobj
= info
->input_bfds
;
5858 inputobj
= inputobj
->link
.next
)
5863 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5865 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5868 if (s
->flags
& SEC_CODE
)
5872 else if (bed
->default_execstack
)
5875 if (notesec
|| info
->stacksize
> 0)
5876 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5877 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5878 && notesec
->output_section
!= bfd_abs_section_ptr
)
5879 notesec
->output_section
->flags
|= SEC_CODE
;
5882 dynobj
= elf_hash_table (info
)->dynobj
;
5884 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5886 struct elf_info_failed eif
;
5887 struct elf_link_hash_entry
*h
;
5889 struct bfd_elf_version_tree
*t
;
5890 struct bfd_elf_version_expr
*d
;
5892 bfd_boolean all_defined
;
5894 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5895 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
) || info
->nointerp
);
5899 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5901 if (soname_indx
== (size_t) -1
5902 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5908 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5910 info
->flags
|= DF_SYMBOLIC
;
5918 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5920 if (indx
== (size_t) -1)
5923 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5924 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5928 if (filter_shlib
!= NULL
)
5932 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5933 filter_shlib
, TRUE
);
5934 if (indx
== (size_t) -1
5935 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5939 if (auxiliary_filters
!= NULL
)
5941 const char * const *p
;
5943 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5947 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5949 if (indx
== (size_t) -1
5950 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5959 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5961 if (indx
== (size_t) -1
5962 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5966 if (depaudit
!= NULL
)
5970 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
5972 if (indx
== (size_t) -1
5973 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
5980 /* If we are supposed to export all symbols into the dynamic symbol
5981 table (this is not the normal case), then do so. */
5982 if (info
->export_dynamic
5983 || (bfd_link_executable (info
) && info
->dynamic
))
5985 elf_link_hash_traverse (elf_hash_table (info
),
5986 _bfd_elf_export_symbol
,
5992 /* Make all global versions with definition. */
5993 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
5994 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5995 if (!d
->symver
&& d
->literal
)
5997 const char *verstr
, *name
;
5998 size_t namelen
, verlen
, newlen
;
5999 char *newname
, *p
, leading_char
;
6000 struct elf_link_hash_entry
*newh
;
6002 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
6004 namelen
= strlen (name
) + (leading_char
!= '\0');
6006 verlen
= strlen (verstr
);
6007 newlen
= namelen
+ verlen
+ 3;
6009 newname
= (char *) bfd_malloc (newlen
);
6010 if (newname
== NULL
)
6012 newname
[0] = leading_char
;
6013 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
6015 /* Check the hidden versioned definition. */
6016 p
= newname
+ namelen
;
6018 memcpy (p
, verstr
, verlen
+ 1);
6019 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6020 newname
, FALSE
, FALSE
,
6023 || (newh
->root
.type
!= bfd_link_hash_defined
6024 && newh
->root
.type
!= bfd_link_hash_defweak
))
6026 /* Check the default versioned definition. */
6028 memcpy (p
, verstr
, verlen
+ 1);
6029 newh
= elf_link_hash_lookup (elf_hash_table (info
),
6030 newname
, FALSE
, FALSE
,
6035 /* Mark this version if there is a definition and it is
6036 not defined in a shared object. */
6038 && !newh
->def_dynamic
6039 && (newh
->root
.type
== bfd_link_hash_defined
6040 || newh
->root
.type
== bfd_link_hash_defweak
))
6044 /* Attach all the symbols to their version information. */
6045 asvinfo
.info
= info
;
6046 asvinfo
.failed
= FALSE
;
6048 elf_link_hash_traverse (elf_hash_table (info
),
6049 _bfd_elf_link_assign_sym_version
,
6054 if (!info
->allow_undefined_version
)
6056 /* Check if all global versions have a definition. */
6058 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
6059 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
6060 if (d
->literal
&& !d
->symver
&& !d
->script
)
6062 (*_bfd_error_handler
)
6063 (_("%s: undefined version: %s"),
6064 d
->pattern
, t
->name
);
6065 all_defined
= FALSE
;
6070 bfd_set_error (bfd_error_bad_value
);
6075 /* Find all symbols which were defined in a dynamic object and make
6076 the backend pick a reasonable value for them. */
6077 elf_link_hash_traverse (elf_hash_table (info
),
6078 _bfd_elf_adjust_dynamic_symbol
,
6083 /* Add some entries to the .dynamic section. We fill in some of the
6084 values later, in bfd_elf_final_link, but we must add the entries
6085 now so that we know the final size of the .dynamic section. */
6087 /* If there are initialization and/or finalization functions to
6088 call then add the corresponding DT_INIT/DT_FINI entries. */
6089 h
= (info
->init_function
6090 ? elf_link_hash_lookup (elf_hash_table (info
),
6091 info
->init_function
, FALSE
,
6098 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
6101 h
= (info
->fini_function
6102 ? elf_link_hash_lookup (elf_hash_table (info
),
6103 info
->fini_function
, FALSE
,
6110 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
6114 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
6115 if (s
!= NULL
&& s
->linker_has_input
)
6117 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6118 if (! bfd_link_executable (info
))
6123 for (sub
= info
->input_bfds
; sub
!= NULL
;
6124 sub
= sub
->link
.next
)
6125 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
6126 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
6127 if (elf_section_data (o
)->this_hdr
.sh_type
6128 == SHT_PREINIT_ARRAY
)
6130 (*_bfd_error_handler
)
6131 (_("%B: .preinit_array section is not allowed in DSO"),
6136 bfd_set_error (bfd_error_nonrepresentable_section
);
6140 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6141 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6144 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6145 if (s
!= NULL
&& s
->linker_has_input
)
6147 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6148 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6151 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6152 if (s
!= NULL
&& s
->linker_has_input
)
6154 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6155 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6159 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6160 /* If .dynstr is excluded from the link, we don't want any of
6161 these tags. Strictly, we should be checking each section
6162 individually; This quick check covers for the case where
6163 someone does a /DISCARD/ : { *(*) }. */
6164 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6166 bfd_size_type strsize
;
6168 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6169 if ((info
->emit_hash
6170 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6171 || (info
->emit_gnu_hash
6172 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6173 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6174 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6175 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6176 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6177 bed
->s
->sizeof_sym
))
6182 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6185 /* The backend must work out the sizes of all the other dynamic
6188 && bed
->elf_backend_size_dynamic_sections
!= NULL
6189 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6192 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6194 unsigned long section_sym_count
;
6195 struct bfd_elf_version_tree
*verdefs
;
6198 /* Set up the version definition section. */
6199 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6200 BFD_ASSERT (s
!= NULL
);
6202 /* We may have created additional version definitions if we are
6203 just linking a regular application. */
6204 verdefs
= info
->version_info
;
6206 /* Skip anonymous version tag. */
6207 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6208 verdefs
= verdefs
->next
;
6210 if (verdefs
== NULL
&& !info
->create_default_symver
)
6211 s
->flags
|= SEC_EXCLUDE
;
6216 struct bfd_elf_version_tree
*t
;
6218 Elf_Internal_Verdef def
;
6219 Elf_Internal_Verdaux defaux
;
6220 struct bfd_link_hash_entry
*bh
;
6221 struct elf_link_hash_entry
*h
;
6227 /* Make space for the base version. */
6228 size
+= sizeof (Elf_External_Verdef
);
6229 size
+= sizeof (Elf_External_Verdaux
);
6232 /* Make space for the default version. */
6233 if (info
->create_default_symver
)
6235 size
+= sizeof (Elf_External_Verdef
);
6239 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6241 struct bfd_elf_version_deps
*n
;
6243 /* Don't emit base version twice. */
6247 size
+= sizeof (Elf_External_Verdef
);
6248 size
+= sizeof (Elf_External_Verdaux
);
6251 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6252 size
+= sizeof (Elf_External_Verdaux
);
6256 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6257 if (s
->contents
== NULL
&& s
->size
!= 0)
6260 /* Fill in the version definition section. */
6264 def
.vd_version
= VER_DEF_CURRENT
;
6265 def
.vd_flags
= VER_FLG_BASE
;
6268 if (info
->create_default_symver
)
6270 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6271 def
.vd_next
= sizeof (Elf_External_Verdef
);
6275 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6276 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6277 + sizeof (Elf_External_Verdaux
));
6280 if (soname_indx
!= (size_t) -1)
6282 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6284 def
.vd_hash
= bfd_elf_hash (soname
);
6285 defaux
.vda_name
= soname_indx
;
6292 name
= lbasename (output_bfd
->filename
);
6293 def
.vd_hash
= bfd_elf_hash (name
);
6294 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6296 if (indx
== (size_t) -1)
6298 defaux
.vda_name
= indx
;
6300 defaux
.vda_next
= 0;
6302 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6303 (Elf_External_Verdef
*) p
);
6304 p
+= sizeof (Elf_External_Verdef
);
6305 if (info
->create_default_symver
)
6307 /* Add a symbol representing this version. */
6309 if (! (_bfd_generic_link_add_one_symbol
6310 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6312 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6314 h
= (struct elf_link_hash_entry
*) bh
;
6317 h
->type
= STT_OBJECT
;
6318 h
->verinfo
.vertree
= NULL
;
6320 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6323 /* Create a duplicate of the base version with the same
6324 aux block, but different flags. */
6327 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6329 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6330 + sizeof (Elf_External_Verdaux
));
6333 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6334 (Elf_External_Verdef
*) p
);
6335 p
+= sizeof (Elf_External_Verdef
);
6337 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6338 (Elf_External_Verdaux
*) p
);
6339 p
+= sizeof (Elf_External_Verdaux
);
6341 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6344 struct bfd_elf_version_deps
*n
;
6346 /* Don't emit the base version twice. */
6351 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6354 /* Add a symbol representing this version. */
6356 if (! (_bfd_generic_link_add_one_symbol
6357 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6359 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6361 h
= (struct elf_link_hash_entry
*) bh
;
6364 h
->type
= STT_OBJECT
;
6365 h
->verinfo
.vertree
= t
;
6367 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6370 def
.vd_version
= VER_DEF_CURRENT
;
6372 if (t
->globals
.list
== NULL
6373 && t
->locals
.list
== NULL
6375 def
.vd_flags
|= VER_FLG_WEAK
;
6376 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6377 def
.vd_cnt
= cdeps
+ 1;
6378 def
.vd_hash
= bfd_elf_hash (t
->name
);
6379 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6382 /* If a basever node is next, it *must* be the last node in
6383 the chain, otherwise Verdef construction breaks. */
6384 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6385 BFD_ASSERT (t
->next
->next
== NULL
);
6387 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6388 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6389 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6391 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6392 (Elf_External_Verdef
*) p
);
6393 p
+= sizeof (Elf_External_Verdef
);
6395 defaux
.vda_name
= h
->dynstr_index
;
6396 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6398 defaux
.vda_next
= 0;
6399 if (t
->deps
!= NULL
)
6400 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6401 t
->name_indx
= defaux
.vda_name
;
6403 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6404 (Elf_External_Verdaux
*) p
);
6405 p
+= sizeof (Elf_External_Verdaux
);
6407 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6409 if (n
->version_needed
== NULL
)
6411 /* This can happen if there was an error in the
6413 defaux
.vda_name
= 0;
6417 defaux
.vda_name
= n
->version_needed
->name_indx
;
6418 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6421 if (n
->next
== NULL
)
6422 defaux
.vda_next
= 0;
6424 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6426 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6427 (Elf_External_Verdaux
*) p
);
6428 p
+= sizeof (Elf_External_Verdaux
);
6432 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6433 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6436 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6439 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6441 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6444 else if (info
->flags
& DF_BIND_NOW
)
6446 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6452 if (bfd_link_executable (info
))
6453 info
->flags_1
&= ~ (DF_1_INITFIRST
6456 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6460 /* Work out the size of the version reference section. */
6462 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6463 BFD_ASSERT (s
!= NULL
);
6465 struct elf_find_verdep_info sinfo
;
6468 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6469 if (sinfo
.vers
== 0)
6471 sinfo
.failed
= FALSE
;
6473 elf_link_hash_traverse (elf_hash_table (info
),
6474 _bfd_elf_link_find_version_dependencies
,
6479 if (elf_tdata (output_bfd
)->verref
== NULL
)
6480 s
->flags
|= SEC_EXCLUDE
;
6483 Elf_Internal_Verneed
*t
;
6488 /* Build the version dependency section. */
6491 for (t
= elf_tdata (output_bfd
)->verref
;
6495 Elf_Internal_Vernaux
*a
;
6497 size
+= sizeof (Elf_External_Verneed
);
6499 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6500 size
+= sizeof (Elf_External_Vernaux
);
6504 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6505 if (s
->contents
== NULL
)
6509 for (t
= elf_tdata (output_bfd
)->verref
;
6514 Elf_Internal_Vernaux
*a
;
6518 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6521 t
->vn_version
= VER_NEED_CURRENT
;
6523 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6524 elf_dt_name (t
->vn_bfd
) != NULL
6525 ? elf_dt_name (t
->vn_bfd
)
6526 : lbasename (t
->vn_bfd
->filename
),
6528 if (indx
== (size_t) -1)
6531 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6532 if (t
->vn_nextref
== NULL
)
6535 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6536 + caux
* sizeof (Elf_External_Vernaux
));
6538 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6539 (Elf_External_Verneed
*) p
);
6540 p
+= sizeof (Elf_External_Verneed
);
6542 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6544 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6545 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6546 a
->vna_nodename
, FALSE
);
6547 if (indx
== (size_t) -1)
6550 if (a
->vna_nextptr
== NULL
)
6553 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6555 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6556 (Elf_External_Vernaux
*) p
);
6557 p
+= sizeof (Elf_External_Vernaux
);
6561 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6562 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6565 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6569 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6570 && elf_tdata (output_bfd
)->cverdefs
== 0)
6571 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6572 §ion_sym_count
) == 0)
6574 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6575 s
->flags
|= SEC_EXCLUDE
;
6581 /* Find the first non-excluded output section. We'll use its
6582 section symbol for some emitted relocs. */
6584 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6588 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6589 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6590 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6592 elf_hash_table (info
)->text_index_section
= s
;
6597 /* Find two non-excluded output sections, one for code, one for data.
6598 We'll use their section symbols for some emitted relocs. */
6600 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6604 /* Data first, since setting text_index_section changes
6605 _bfd_elf_link_omit_section_dynsym. */
6606 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6607 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6608 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6610 elf_hash_table (info
)->data_index_section
= s
;
6614 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6615 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6616 == (SEC_ALLOC
| SEC_READONLY
))
6617 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6619 elf_hash_table (info
)->text_index_section
= s
;
6623 if (elf_hash_table (info
)->text_index_section
== NULL
)
6624 elf_hash_table (info
)->text_index_section
6625 = elf_hash_table (info
)->data_index_section
;
6629 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6631 const struct elf_backend_data
*bed
;
6633 if (!is_elf_hash_table (info
->hash
))
6636 bed
= get_elf_backend_data (output_bfd
);
6637 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6639 if (elf_hash_table (info
)->dynamic_sections_created
)
6643 bfd_size_type dynsymcount
;
6644 unsigned long section_sym_count
;
6645 unsigned int dtagcount
;
6647 dynobj
= elf_hash_table (info
)->dynobj
;
6649 /* Assign dynsym indicies. In a shared library we generate a
6650 section symbol for each output section, which come first.
6651 Next come all of the back-end allocated local dynamic syms,
6652 followed by the rest of the global symbols. */
6654 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6655 §ion_sym_count
);
6657 /* Work out the size of the symbol version section. */
6658 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6659 BFD_ASSERT (s
!= NULL
);
6660 if ((s
->flags
& SEC_EXCLUDE
) == 0)
6662 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6663 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6664 if (s
->contents
== NULL
)
6667 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6671 /* Set the size of the .dynsym and .hash sections. We counted
6672 the number of dynamic symbols in elf_link_add_object_symbols.
6673 We will build the contents of .dynsym and .hash when we build
6674 the final symbol table, because until then we do not know the
6675 correct value to give the symbols. We built the .dynstr
6676 section as we went along in elf_link_add_object_symbols. */
6677 s
= elf_hash_table (info
)->dynsym
;
6678 BFD_ASSERT (s
!= NULL
);
6679 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6681 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6682 if (s
->contents
== NULL
)
6685 /* The first entry in .dynsym is a dummy symbol. Clear all the
6686 section syms, in case we don't output them all. */
6687 ++section_sym_count
;
6688 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6690 elf_hash_table (info
)->bucketcount
= 0;
6692 /* Compute the size of the hashing table. As a side effect this
6693 computes the hash values for all the names we export. */
6694 if (info
->emit_hash
)
6696 unsigned long int *hashcodes
;
6697 struct hash_codes_info hashinf
;
6699 unsigned long int nsyms
;
6701 size_t hash_entry_size
;
6703 /* Compute the hash values for all exported symbols. At the same
6704 time store the values in an array so that we could use them for
6706 amt
= dynsymcount
* sizeof (unsigned long int);
6707 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6708 if (hashcodes
== NULL
)
6710 hashinf
.hashcodes
= hashcodes
;
6711 hashinf
.error
= FALSE
;
6713 /* Put all hash values in HASHCODES. */
6714 elf_link_hash_traverse (elf_hash_table (info
),
6715 elf_collect_hash_codes
, &hashinf
);
6722 nsyms
= hashinf
.hashcodes
- hashcodes
;
6724 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6727 if (bucketcount
== 0)
6730 elf_hash_table (info
)->bucketcount
= bucketcount
;
6732 s
= bfd_get_linker_section (dynobj
, ".hash");
6733 BFD_ASSERT (s
!= NULL
);
6734 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6735 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6736 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6737 if (s
->contents
== NULL
)
6740 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6741 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6742 s
->contents
+ hash_entry_size
);
6745 if (info
->emit_gnu_hash
)
6748 unsigned char *contents
;
6749 struct collect_gnu_hash_codes cinfo
;
6753 memset (&cinfo
, 0, sizeof (cinfo
));
6755 /* Compute the hash values for all exported symbols. At the same
6756 time store the values in an array so that we could use them for
6758 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6759 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6760 if (cinfo
.hashcodes
== NULL
)
6763 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6764 cinfo
.min_dynindx
= -1;
6765 cinfo
.output_bfd
= output_bfd
;
6768 /* Put all hash values in HASHCODES. */
6769 elf_link_hash_traverse (elf_hash_table (info
),
6770 elf_collect_gnu_hash_codes
, &cinfo
);
6773 free (cinfo
.hashcodes
);
6778 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6780 if (bucketcount
== 0)
6782 free (cinfo
.hashcodes
);
6786 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6787 BFD_ASSERT (s
!= NULL
);
6789 if (cinfo
.nsyms
== 0)
6791 /* Empty .gnu.hash section is special. */
6792 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6793 free (cinfo
.hashcodes
);
6794 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6795 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6796 if (contents
== NULL
)
6798 s
->contents
= contents
;
6799 /* 1 empty bucket. */
6800 bfd_put_32 (output_bfd
, 1, contents
);
6801 /* SYMIDX above the special symbol 0. */
6802 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6803 /* Just one word for bitmask. */
6804 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6805 /* Only hash fn bloom filter. */
6806 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6807 /* No hashes are valid - empty bitmask. */
6808 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6809 /* No hashes in the only bucket. */
6810 bfd_put_32 (output_bfd
, 0,
6811 contents
+ 16 + bed
->s
->arch_size
/ 8);
6815 unsigned long int maskwords
, maskbitslog2
, x
;
6816 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6820 while ((x
>>= 1) != 0)
6822 if (maskbitslog2
< 3)
6824 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6825 maskbitslog2
= maskbitslog2
+ 3;
6827 maskbitslog2
= maskbitslog2
+ 2;
6828 if (bed
->s
->arch_size
== 64)
6830 if (maskbitslog2
== 5)
6836 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6837 cinfo
.shift2
= maskbitslog2
;
6838 cinfo
.maskbits
= 1 << maskbitslog2
;
6839 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6840 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6841 amt
+= maskwords
* sizeof (bfd_vma
);
6842 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6843 if (cinfo
.bitmask
== NULL
)
6845 free (cinfo
.hashcodes
);
6849 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6850 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6851 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6852 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6854 /* Determine how often each hash bucket is used. */
6855 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6856 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6857 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6859 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6860 if (cinfo
.counts
[i
] != 0)
6862 cinfo
.indx
[i
] = cnt
;
6863 cnt
+= cinfo
.counts
[i
];
6865 BFD_ASSERT (cnt
== dynsymcount
);
6866 cinfo
.bucketcount
= bucketcount
;
6867 cinfo
.local_indx
= cinfo
.min_dynindx
;
6869 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6870 s
->size
+= cinfo
.maskbits
/ 8;
6871 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6872 if (contents
== NULL
)
6874 free (cinfo
.bitmask
);
6875 free (cinfo
.hashcodes
);
6879 s
->contents
= contents
;
6880 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6881 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6882 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6883 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6884 contents
+= 16 + cinfo
.maskbits
/ 8;
6886 for (i
= 0; i
< bucketcount
; ++i
)
6888 if (cinfo
.counts
[i
] == 0)
6889 bfd_put_32 (output_bfd
, 0, contents
);
6891 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6895 cinfo
.contents
= contents
;
6897 /* Renumber dynamic symbols, populate .gnu.hash section. */
6898 elf_link_hash_traverse (elf_hash_table (info
),
6899 elf_renumber_gnu_hash_syms
, &cinfo
);
6901 contents
= s
->contents
+ 16;
6902 for (i
= 0; i
< maskwords
; ++i
)
6904 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6906 contents
+= bed
->s
->arch_size
/ 8;
6909 free (cinfo
.bitmask
);
6910 free (cinfo
.hashcodes
);
6914 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6915 BFD_ASSERT (s
!= NULL
);
6917 elf_finalize_dynstr (output_bfd
, info
);
6919 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6921 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6922 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6929 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6932 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6935 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6936 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6939 /* Finish SHF_MERGE section merging. */
6942 _bfd_elf_merge_sections (bfd
*obfd
, struct bfd_link_info
*info
)
6947 if (!is_elf_hash_table (info
->hash
))
6950 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6951 if ((ibfd
->flags
& DYNAMIC
) == 0
6952 && bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
6953 && (elf_elfheader (ibfd
)->e_ident
[EI_CLASS
]
6954 == get_elf_backend_data (obfd
)->s
->elfclass
))
6955 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6956 if ((sec
->flags
& SEC_MERGE
) != 0
6957 && !bfd_is_abs_section (sec
->output_section
))
6959 struct bfd_elf_section_data
*secdata
;
6961 secdata
= elf_section_data (sec
);
6962 if (! _bfd_add_merge_section (obfd
,
6963 &elf_hash_table (info
)->merge_info
,
6964 sec
, &secdata
->sec_info
))
6966 else if (secdata
->sec_info
)
6967 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
6970 if (elf_hash_table (info
)->merge_info
!= NULL
)
6971 _bfd_merge_sections (obfd
, info
, elf_hash_table (info
)->merge_info
,
6972 merge_sections_remove_hook
);
6976 /* Create an entry in an ELF linker hash table. */
6978 struct bfd_hash_entry
*
6979 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6980 struct bfd_hash_table
*table
,
6983 /* Allocate the structure if it has not already been allocated by a
6987 entry
= (struct bfd_hash_entry
*)
6988 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6993 /* Call the allocation method of the superclass. */
6994 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6997 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6998 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
7000 /* Set local fields. */
7003 ret
->got
= htab
->init_got_refcount
;
7004 ret
->plt
= htab
->init_plt_refcount
;
7005 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
7006 - offsetof (struct elf_link_hash_entry
, size
)));
7007 /* Assume that we have been called by a non-ELF symbol reader.
7008 This flag is then reset by the code which reads an ELF input
7009 file. This ensures that a symbol created by a non-ELF symbol
7010 reader will have the flag set correctly. */
7017 /* Copy data from an indirect symbol to its direct symbol, hiding the
7018 old indirect symbol. Also used for copying flags to a weakdef. */
7021 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
7022 struct elf_link_hash_entry
*dir
,
7023 struct elf_link_hash_entry
*ind
)
7025 struct elf_link_hash_table
*htab
;
7027 /* Copy down any references that we may have already seen to the
7028 symbol which just became indirect if DIR isn't a hidden versioned
7031 if (dir
->versioned
!= versioned_hidden
)
7033 dir
->ref_dynamic
|= ind
->ref_dynamic
;
7034 dir
->ref_regular
|= ind
->ref_regular
;
7035 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
7036 dir
->non_got_ref
|= ind
->non_got_ref
;
7037 dir
->needs_plt
|= ind
->needs_plt
;
7038 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
7041 if (ind
->root
.type
!= bfd_link_hash_indirect
)
7044 /* Copy over the global and procedure linkage table refcount entries.
7045 These may have been already set up by a check_relocs routine. */
7046 htab
= elf_hash_table (info
);
7047 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
7049 if (dir
->got
.refcount
< 0)
7050 dir
->got
.refcount
= 0;
7051 dir
->got
.refcount
+= ind
->got
.refcount
;
7052 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
7055 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
7057 if (dir
->plt
.refcount
< 0)
7058 dir
->plt
.refcount
= 0;
7059 dir
->plt
.refcount
+= ind
->plt
.refcount
;
7060 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
7063 if (ind
->dynindx
!= -1)
7065 if (dir
->dynindx
!= -1)
7066 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
7067 dir
->dynindx
= ind
->dynindx
;
7068 dir
->dynstr_index
= ind
->dynstr_index
;
7070 ind
->dynstr_index
= 0;
7075 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
7076 struct elf_link_hash_entry
*h
,
7077 bfd_boolean force_local
)
7079 /* STT_GNU_IFUNC symbol must go through PLT. */
7080 if (h
->type
!= STT_GNU_IFUNC
)
7082 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
7087 h
->forced_local
= 1;
7088 if (h
->dynindx
!= -1)
7091 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
7097 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7101 _bfd_elf_link_hash_table_init
7102 (struct elf_link_hash_table
*table
,
7104 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
7105 struct bfd_hash_table
*,
7107 unsigned int entsize
,
7108 enum elf_target_id target_id
)
7111 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
7113 table
->init_got_refcount
.refcount
= can_refcount
- 1;
7114 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
7115 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
7116 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
7117 /* The first dynamic symbol is a dummy. */
7118 table
->dynsymcount
= 1;
7120 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
7122 table
->root
.type
= bfd_link_elf_hash_table
;
7123 table
->hash_table_id
= target_id
;
7128 /* Create an ELF linker hash table. */
7130 struct bfd_link_hash_table
*
7131 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7133 struct elf_link_hash_table
*ret
;
7134 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7136 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7140 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7141 sizeof (struct elf_link_hash_entry
),
7147 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7152 /* Destroy an ELF linker hash table. */
7155 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7157 struct elf_link_hash_table
*htab
;
7159 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7160 if (htab
->dynstr
!= NULL
)
7161 _bfd_elf_strtab_free (htab
->dynstr
);
7162 _bfd_merge_sections_free (htab
->merge_info
);
7163 _bfd_generic_link_hash_table_free (obfd
);
7166 /* This is a hook for the ELF emulation code in the generic linker to
7167 tell the backend linker what file name to use for the DT_NEEDED
7168 entry for a dynamic object. */
7171 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7173 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7174 && bfd_get_format (abfd
) == bfd_object
)
7175 elf_dt_name (abfd
) = name
;
7179 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7182 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7183 && bfd_get_format (abfd
) == bfd_object
)
7184 lib_class
= elf_dyn_lib_class (abfd
);
7191 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7193 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7194 && bfd_get_format (abfd
) == bfd_object
)
7195 elf_dyn_lib_class (abfd
) = lib_class
;
7198 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7199 the linker ELF emulation code. */
7201 struct bfd_link_needed_list
*
7202 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7203 struct bfd_link_info
*info
)
7205 if (! is_elf_hash_table (info
->hash
))
7207 return elf_hash_table (info
)->needed
;
7210 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7211 hook for the linker ELF emulation code. */
7213 struct bfd_link_needed_list
*
7214 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7215 struct bfd_link_info
*info
)
7217 if (! is_elf_hash_table (info
->hash
))
7219 return elf_hash_table (info
)->runpath
;
7222 /* Get the name actually used for a dynamic object for a link. This
7223 is the SONAME entry if there is one. Otherwise, it is the string
7224 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7227 bfd_elf_get_dt_soname (bfd
*abfd
)
7229 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7230 && bfd_get_format (abfd
) == bfd_object
)
7231 return elf_dt_name (abfd
);
7235 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7236 the ELF linker emulation code. */
7239 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7240 struct bfd_link_needed_list
**pneeded
)
7243 bfd_byte
*dynbuf
= NULL
;
7244 unsigned int elfsec
;
7245 unsigned long shlink
;
7246 bfd_byte
*extdyn
, *extdynend
;
7248 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7252 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7253 || bfd_get_format (abfd
) != bfd_object
)
7256 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7257 if (s
== NULL
|| s
->size
== 0)
7260 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7263 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7264 if (elfsec
== SHN_BAD
)
7267 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7269 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7270 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7273 extdynend
= extdyn
+ s
->size
;
7274 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7276 Elf_Internal_Dyn dyn
;
7278 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7280 if (dyn
.d_tag
== DT_NULL
)
7283 if (dyn
.d_tag
== DT_NEEDED
)
7286 struct bfd_link_needed_list
*l
;
7287 unsigned int tagv
= dyn
.d_un
.d_val
;
7290 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7295 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7316 struct elf_symbuf_symbol
7318 unsigned long st_name
; /* Symbol name, index in string tbl */
7319 unsigned char st_info
; /* Type and binding attributes */
7320 unsigned char st_other
; /* Visibilty, and target specific */
7323 struct elf_symbuf_head
7325 struct elf_symbuf_symbol
*ssym
;
7327 unsigned int st_shndx
;
7334 Elf_Internal_Sym
*isym
;
7335 struct elf_symbuf_symbol
*ssym
;
7340 /* Sort references to symbols by ascending section number. */
7343 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7345 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7346 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7348 return s1
->st_shndx
- s2
->st_shndx
;
7352 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7354 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7355 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7356 return strcmp (s1
->name
, s2
->name
);
7359 static struct elf_symbuf_head
*
7360 elf_create_symbuf (size_t symcount
, Elf_Internal_Sym
*isymbuf
)
7362 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7363 struct elf_symbuf_symbol
*ssym
;
7364 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7365 size_t i
, shndx_count
, total_size
;
7367 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7371 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7372 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7373 *ind
++ = &isymbuf
[i
];
7376 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7377 elf_sort_elf_symbol
);
7380 if (indbufend
> indbuf
)
7381 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7382 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7385 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7386 + (indbufend
- indbuf
) * sizeof (*ssym
));
7387 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7388 if (ssymbuf
== NULL
)
7394 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7395 ssymbuf
->ssym
= NULL
;
7396 ssymbuf
->count
= shndx_count
;
7397 ssymbuf
->st_shndx
= 0;
7398 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7400 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7403 ssymhead
->ssym
= ssym
;
7404 ssymhead
->count
= 0;
7405 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7407 ssym
->st_name
= (*ind
)->st_name
;
7408 ssym
->st_info
= (*ind
)->st_info
;
7409 ssym
->st_other
= (*ind
)->st_other
;
7412 BFD_ASSERT ((size_t) (ssymhead
- ssymbuf
) == shndx_count
7413 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7420 /* Check if 2 sections define the same set of local and global
7424 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7425 struct bfd_link_info
*info
)
7428 const struct elf_backend_data
*bed1
, *bed2
;
7429 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7430 size_t symcount1
, symcount2
;
7431 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7432 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7433 Elf_Internal_Sym
*isym
, *isymend
;
7434 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7435 size_t count1
, count2
, i
;
7436 unsigned int shndx1
, shndx2
;
7442 /* Both sections have to be in ELF. */
7443 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7444 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7447 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7450 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7451 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7452 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7455 bed1
= get_elf_backend_data (bfd1
);
7456 bed2
= get_elf_backend_data (bfd2
);
7457 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7458 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7459 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7460 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7462 if (symcount1
== 0 || symcount2
== 0)
7468 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7469 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7471 if (ssymbuf1
== NULL
)
7473 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7475 if (isymbuf1
== NULL
)
7478 if (!info
->reduce_memory_overheads
)
7479 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7480 = elf_create_symbuf (symcount1
, isymbuf1
);
7483 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7485 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7487 if (isymbuf2
== NULL
)
7490 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7491 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7492 = elf_create_symbuf (symcount2
, isymbuf2
);
7495 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7497 /* Optimized faster version. */
7499 struct elf_symbol
*symp
;
7500 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7503 hi
= ssymbuf1
->count
;
7508 mid
= (lo
+ hi
) / 2;
7509 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7511 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7515 count1
= ssymbuf1
[mid
].count
;
7522 hi
= ssymbuf2
->count
;
7527 mid
= (lo
+ hi
) / 2;
7528 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7530 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7534 count2
= ssymbuf2
[mid
].count
;
7540 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7544 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7546 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7547 if (symtable1
== NULL
|| symtable2
== NULL
)
7551 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7552 ssym
< ssymend
; ssym
++, symp
++)
7554 symp
->u
.ssym
= ssym
;
7555 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7561 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7562 ssym
< ssymend
; ssym
++, symp
++)
7564 symp
->u
.ssym
= ssym
;
7565 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7570 /* Sort symbol by name. */
7571 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7572 elf_sym_name_compare
);
7573 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7574 elf_sym_name_compare
);
7576 for (i
= 0; i
< count1
; i
++)
7577 /* Two symbols must have the same binding, type and name. */
7578 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7579 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7580 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7587 symtable1
= (struct elf_symbol
*)
7588 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7589 symtable2
= (struct elf_symbol
*)
7590 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7591 if (symtable1
== NULL
|| symtable2
== NULL
)
7594 /* Count definitions in the section. */
7596 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7597 if (isym
->st_shndx
== shndx1
)
7598 symtable1
[count1
++].u
.isym
= isym
;
7601 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7602 if (isym
->st_shndx
== shndx2
)
7603 symtable2
[count2
++].u
.isym
= isym
;
7605 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7608 for (i
= 0; i
< count1
; i
++)
7610 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7611 symtable1
[i
].u
.isym
->st_name
);
7613 for (i
= 0; i
< count2
; i
++)
7615 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7616 symtable2
[i
].u
.isym
->st_name
);
7618 /* Sort symbol by name. */
7619 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7620 elf_sym_name_compare
);
7621 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7622 elf_sym_name_compare
);
7624 for (i
= 0; i
< count1
; i
++)
7625 /* Two symbols must have the same binding, type and name. */
7626 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7627 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7628 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7646 /* Return TRUE if 2 section types are compatible. */
7649 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7650 bfd
*bbfd
, const asection
*bsec
)
7654 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7655 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7658 return elf_section_type (asec
) == elf_section_type (bsec
);
7661 /* Final phase of ELF linker. */
7663 /* A structure we use to avoid passing large numbers of arguments. */
7665 struct elf_final_link_info
7667 /* General link information. */
7668 struct bfd_link_info
*info
;
7671 /* Symbol string table. */
7672 struct elf_strtab_hash
*symstrtab
;
7673 /* .hash section. */
7675 /* symbol version section (.gnu.version). */
7676 asection
*symver_sec
;
7677 /* Buffer large enough to hold contents of any section. */
7679 /* Buffer large enough to hold external relocs of any section. */
7680 void *external_relocs
;
7681 /* Buffer large enough to hold internal relocs of any section. */
7682 Elf_Internal_Rela
*internal_relocs
;
7683 /* Buffer large enough to hold external local symbols of any input
7685 bfd_byte
*external_syms
;
7686 /* And a buffer for symbol section indices. */
7687 Elf_External_Sym_Shndx
*locsym_shndx
;
7688 /* Buffer large enough to hold internal local symbols of any input
7690 Elf_Internal_Sym
*internal_syms
;
7691 /* Array large enough to hold a symbol index for each local symbol
7692 of any input BFD. */
7694 /* Array large enough to hold a section pointer for each local
7695 symbol of any input BFD. */
7696 asection
**sections
;
7697 /* Buffer for SHT_SYMTAB_SHNDX section. */
7698 Elf_External_Sym_Shndx
*symshndxbuf
;
7699 /* Number of STT_FILE syms seen. */
7700 size_t filesym_count
;
7703 /* This struct is used to pass information to elf_link_output_extsym. */
7705 struct elf_outext_info
7708 bfd_boolean localsyms
;
7709 bfd_boolean file_sym_done
;
7710 struct elf_final_link_info
*flinfo
;
7714 /* Support for evaluating a complex relocation.
7716 Complex relocations are generalized, self-describing relocations. The
7717 implementation of them consists of two parts: complex symbols, and the
7718 relocations themselves.
7720 The relocations are use a reserved elf-wide relocation type code (R_RELC
7721 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7722 information (start bit, end bit, word width, etc) into the addend. This
7723 information is extracted from CGEN-generated operand tables within gas.
7725 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7726 internal) representing prefix-notation expressions, including but not
7727 limited to those sorts of expressions normally encoded as addends in the
7728 addend field. The symbol mangling format is:
7731 | <unary-operator> ':' <node>
7732 | <binary-operator> ':' <node> ':' <node>
7735 <literal> := 's' <digits=N> ':' <N character symbol name>
7736 | 'S' <digits=N> ':' <N character section name>
7740 <binary-operator> := as in C
7741 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7744 set_symbol_value (bfd
*bfd_with_globals
,
7745 Elf_Internal_Sym
*isymbuf
,
7750 struct elf_link_hash_entry
**sym_hashes
;
7751 struct elf_link_hash_entry
*h
;
7752 size_t extsymoff
= locsymcount
;
7754 if (symidx
< locsymcount
)
7756 Elf_Internal_Sym
*sym
;
7758 sym
= isymbuf
+ symidx
;
7759 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7761 /* It is a local symbol: move it to the
7762 "absolute" section and give it a value. */
7763 sym
->st_shndx
= SHN_ABS
;
7764 sym
->st_value
= val
;
7767 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7771 /* It is a global symbol: set its link type
7772 to "defined" and give it a value. */
7774 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7775 h
= sym_hashes
[symidx
- extsymoff
];
7776 while (h
->root
.type
== bfd_link_hash_indirect
7777 || h
->root
.type
== bfd_link_hash_warning
)
7778 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7779 h
->root
.type
= bfd_link_hash_defined
;
7780 h
->root
.u
.def
.value
= val
;
7781 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7785 resolve_symbol (const char *name
,
7787 struct elf_final_link_info
*flinfo
,
7789 Elf_Internal_Sym
*isymbuf
,
7792 Elf_Internal_Sym
*sym
;
7793 struct bfd_link_hash_entry
*global_entry
;
7794 const char *candidate
= NULL
;
7795 Elf_Internal_Shdr
*symtab_hdr
;
7798 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7800 for (i
= 0; i
< locsymcount
; ++ i
)
7804 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7807 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7808 symtab_hdr
->sh_link
,
7811 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7812 name
, candidate
, (unsigned long) sym
->st_value
);
7814 if (candidate
&& strcmp (candidate
, name
) == 0)
7816 asection
*sec
= flinfo
->sections
[i
];
7818 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7819 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7821 printf ("Found symbol with value %8.8lx\n",
7822 (unsigned long) *result
);
7828 /* Hmm, haven't found it yet. perhaps it is a global. */
7829 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7830 FALSE
, FALSE
, TRUE
);
7834 if (global_entry
->type
== bfd_link_hash_defined
7835 || global_entry
->type
== bfd_link_hash_defweak
)
7837 *result
= (global_entry
->u
.def
.value
7838 + global_entry
->u
.def
.section
->output_section
->vma
7839 + global_entry
->u
.def
.section
->output_offset
);
7841 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7842 global_entry
->root
.string
, (unsigned long) *result
);
7850 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7851 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7852 names like "foo.end" which is the end address of section "foo". */
7855 resolve_section (const char *name
,
7863 for (curr
= sections
; curr
; curr
= curr
->next
)
7864 if (strcmp (curr
->name
, name
) == 0)
7866 *result
= curr
->vma
;
7870 /* Hmm. still haven't found it. try pseudo-section names. */
7871 /* FIXME: This could be coded more efficiently... */
7872 for (curr
= sections
; curr
; curr
= curr
->next
)
7874 len
= strlen (curr
->name
);
7875 if (len
> strlen (name
))
7878 if (strncmp (curr
->name
, name
, len
) == 0)
7880 if (strncmp (".end", name
+ len
, 4) == 0)
7882 *result
= curr
->vma
+ curr
->size
/ bfd_octets_per_byte (abfd
);
7886 /* Insert more pseudo-section names here, if you like. */
7894 undefined_reference (const char *reftype
, const char *name
)
7896 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7901 eval_symbol (bfd_vma
*result
,
7904 struct elf_final_link_info
*flinfo
,
7906 Elf_Internal_Sym
*isymbuf
,
7915 const char *sym
= *symp
;
7917 bfd_boolean symbol_is_section
= FALSE
;
7922 if (len
< 1 || len
> sizeof (symbuf
))
7924 bfd_set_error (bfd_error_invalid_operation
);
7937 *result
= strtoul (sym
, (char **) symp
, 16);
7941 symbol_is_section
= TRUE
;
7944 symlen
= strtol (sym
, (char **) symp
, 10);
7945 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7947 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7949 bfd_set_error (bfd_error_invalid_operation
);
7953 memcpy (symbuf
, sym
, symlen
);
7954 symbuf
[symlen
] = '\0';
7955 *symp
= sym
+ symlen
;
7957 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7958 the symbol as a section, or vice-versa. so we're pretty liberal in our
7959 interpretation here; section means "try section first", not "must be a
7960 section", and likewise with symbol. */
7962 if (symbol_is_section
)
7964 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
, input_bfd
)
7965 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7966 isymbuf
, locsymcount
))
7968 undefined_reference ("section", symbuf
);
7974 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7975 isymbuf
, locsymcount
)
7976 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
7979 undefined_reference ("symbol", symbuf
);
7986 /* All that remains are operators. */
7988 #define UNARY_OP(op) \
7989 if (strncmp (sym, #op, strlen (#op)) == 0) \
7991 sym += strlen (#op); \
7995 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7996 isymbuf, locsymcount, signed_p)) \
7999 *result = op ((bfd_signed_vma) a); \
8005 #define BINARY_OP(op) \
8006 if (strncmp (sym, #op, strlen (#op)) == 0) \
8008 sym += strlen (#op); \
8012 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8013 isymbuf, locsymcount, signed_p)) \
8016 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8017 isymbuf, locsymcount, signed_p)) \
8020 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8050 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
8051 bfd_set_error (bfd_error_invalid_operation
);
8057 put_value (bfd_vma size
,
8058 unsigned long chunksz
,
8063 location
+= (size
- chunksz
);
8065 for (; size
; size
-= chunksz
, location
-= chunksz
)
8070 bfd_put_8 (input_bfd
, x
, location
);
8074 bfd_put_16 (input_bfd
, x
, location
);
8078 bfd_put_32 (input_bfd
, x
, location
);
8079 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8085 bfd_put_64 (input_bfd
, x
, location
);
8086 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8099 get_value (bfd_vma size
,
8100 unsigned long chunksz
,
8107 /* Sanity checks. */
8108 BFD_ASSERT (chunksz
<= sizeof (x
)
8111 && (size
% chunksz
) == 0
8112 && input_bfd
!= NULL
8113 && location
!= NULL
);
8115 if (chunksz
== sizeof (x
))
8117 BFD_ASSERT (size
== chunksz
);
8119 /* Make sure that we do not perform an undefined shift operation.
8120 We know that size == chunksz so there will only be one iteration
8121 of the loop below. */
8125 shift
= 8 * chunksz
;
8127 for (; size
; size
-= chunksz
, location
+= chunksz
)
8132 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8135 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8138 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8142 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8153 decode_complex_addend (unsigned long *start
, /* in bits */
8154 unsigned long *oplen
, /* in bits */
8155 unsigned long *len
, /* in bits */
8156 unsigned long *wordsz
, /* in bytes */
8157 unsigned long *chunksz
, /* in bytes */
8158 unsigned long *lsb0_p
,
8159 unsigned long *signed_p
,
8160 unsigned long *trunc_p
,
8161 unsigned long encoded
)
8163 * start
= encoded
& 0x3F;
8164 * len
= (encoded
>> 6) & 0x3F;
8165 * oplen
= (encoded
>> 12) & 0x3F;
8166 * wordsz
= (encoded
>> 18) & 0xF;
8167 * chunksz
= (encoded
>> 22) & 0xF;
8168 * lsb0_p
= (encoded
>> 27) & 1;
8169 * signed_p
= (encoded
>> 28) & 1;
8170 * trunc_p
= (encoded
>> 29) & 1;
8173 bfd_reloc_status_type
8174 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8175 asection
*input_section ATTRIBUTE_UNUSED
,
8177 Elf_Internal_Rela
*rel
,
8180 bfd_vma shift
, x
, mask
;
8181 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8182 bfd_reloc_status_type r
;
8184 /* Perform this reloc, since it is complex.
8185 (this is not to say that it necessarily refers to a complex
8186 symbol; merely that it is a self-describing CGEN based reloc.
8187 i.e. the addend has the complete reloc information (bit start, end,
8188 word size, etc) encoded within it.). */
8190 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8191 &chunksz
, &lsb0_p
, &signed_p
,
8192 &trunc_p
, rel
->r_addend
);
8194 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8197 shift
= (start
+ 1) - len
;
8199 shift
= (8 * wordsz
) - (start
+ len
);
8201 x
= get_value (wordsz
, chunksz
, input_bfd
,
8202 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8205 printf ("Doing complex reloc: "
8206 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8207 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8208 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8209 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8210 oplen
, (unsigned long) x
, (unsigned long) mask
,
8211 (unsigned long) relocation
);
8216 /* Now do an overflow check. */
8217 r
= bfd_check_overflow ((signed_p
8218 ? complain_overflow_signed
8219 : complain_overflow_unsigned
),
8220 len
, 0, (8 * wordsz
),
8224 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8227 printf (" relocation: %8.8lx\n"
8228 " shifted mask: %8.8lx\n"
8229 " shifted/masked reloc: %8.8lx\n"
8230 " result: %8.8lx\n",
8231 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8232 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8234 put_value (wordsz
, chunksz
, input_bfd
, x
,
8235 contents
+ rel
->r_offset
* bfd_octets_per_byte (input_bfd
));
8239 /* Functions to read r_offset from external (target order) reloc
8240 entry. Faster than bfd_getl32 et al, because we let the compiler
8241 know the value is aligned. */
8244 ext32l_r_offset (const void *p
)
8251 const union aligned32
*a
8252 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8254 uint32_t aval
= ( (uint32_t) a
->c
[0]
8255 | (uint32_t) a
->c
[1] << 8
8256 | (uint32_t) a
->c
[2] << 16
8257 | (uint32_t) a
->c
[3] << 24);
8262 ext32b_r_offset (const void *p
)
8269 const union aligned32
*a
8270 = (const union aligned32
*) &((const Elf32_External_Rel
*) p
)->r_offset
;
8272 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8273 | (uint32_t) a
->c
[1] << 16
8274 | (uint32_t) a
->c
[2] << 8
8275 | (uint32_t) a
->c
[3]);
8279 #ifdef BFD_HOST_64_BIT
8281 ext64l_r_offset (const void *p
)
8288 const union aligned64
*a
8289 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8291 uint64_t aval
= ( (uint64_t) a
->c
[0]
8292 | (uint64_t) a
->c
[1] << 8
8293 | (uint64_t) a
->c
[2] << 16
8294 | (uint64_t) a
->c
[3] << 24
8295 | (uint64_t) a
->c
[4] << 32
8296 | (uint64_t) a
->c
[5] << 40
8297 | (uint64_t) a
->c
[6] << 48
8298 | (uint64_t) a
->c
[7] << 56);
8303 ext64b_r_offset (const void *p
)
8310 const union aligned64
*a
8311 = (const union aligned64
*) &((const Elf64_External_Rel
*) p
)->r_offset
;
8313 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8314 | (uint64_t) a
->c
[1] << 48
8315 | (uint64_t) a
->c
[2] << 40
8316 | (uint64_t) a
->c
[3] << 32
8317 | (uint64_t) a
->c
[4] << 24
8318 | (uint64_t) a
->c
[5] << 16
8319 | (uint64_t) a
->c
[6] << 8
8320 | (uint64_t) a
->c
[7]);
8325 /* When performing a relocatable link, the input relocations are
8326 preserved. But, if they reference global symbols, the indices
8327 referenced must be updated. Update all the relocations found in
8331 elf_link_adjust_relocs (bfd
*abfd
,
8332 struct bfd_elf_section_reloc_data
*reldata
,
8336 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8338 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8339 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8340 bfd_vma r_type_mask
;
8342 unsigned int count
= reldata
->count
;
8343 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8345 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8347 swap_in
= bed
->s
->swap_reloc_in
;
8348 swap_out
= bed
->s
->swap_reloc_out
;
8350 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8352 swap_in
= bed
->s
->swap_reloca_in
;
8353 swap_out
= bed
->s
->swap_reloca_out
;
8358 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8361 if (bed
->s
->arch_size
== 32)
8368 r_type_mask
= 0xffffffff;
8372 erela
= reldata
->hdr
->contents
;
8373 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8375 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8378 if (*rel_hash
== NULL
)
8381 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8383 (*swap_in
) (abfd
, erela
, irela
);
8384 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8385 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8386 | (irela
[j
].r_info
& r_type_mask
));
8387 (*swap_out
) (abfd
, irela
, erela
);
8390 if (sort
&& count
!= 0)
8392 bfd_vma (*ext_r_off
) (const void *);
8395 bfd_byte
*base
, *end
, *p
, *loc
;
8396 bfd_byte
*buf
= NULL
;
8398 if (bed
->s
->arch_size
== 32)
8400 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8401 ext_r_off
= ext32l_r_offset
;
8402 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8403 ext_r_off
= ext32b_r_offset
;
8409 #ifdef BFD_HOST_64_BIT
8410 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8411 ext_r_off
= ext64l_r_offset
;
8412 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8413 ext_r_off
= ext64b_r_offset
;
8419 /* Must use a stable sort here. A modified insertion sort,
8420 since the relocs are mostly sorted already. */
8421 elt_size
= reldata
->hdr
->sh_entsize
;
8422 base
= reldata
->hdr
->contents
;
8423 end
= base
+ count
* elt_size
;
8424 if (elt_size
> sizeof (Elf64_External_Rela
))
8427 /* Ensure the first element is lowest. This acts as a sentinel,
8428 speeding the main loop below. */
8429 r_off
= (*ext_r_off
) (base
);
8430 for (p
= loc
= base
; (p
+= elt_size
) < end
; )
8432 bfd_vma r_off2
= (*ext_r_off
) (p
);
8441 /* Don't just swap *base and *loc as that changes the order
8442 of the original base[0] and base[1] if they happen to
8443 have the same r_offset. */
8444 bfd_byte onebuf
[sizeof (Elf64_External_Rela
)];
8445 memcpy (onebuf
, loc
, elt_size
);
8446 memmove (base
+ elt_size
, base
, loc
- base
);
8447 memcpy (base
, onebuf
, elt_size
);
8450 for (p
= base
+ elt_size
; (p
+= elt_size
) < end
; )
8452 /* base to p is sorted, *p is next to insert. */
8453 r_off
= (*ext_r_off
) (p
);
8454 /* Search the sorted region for location to insert. */
8456 while (r_off
< (*ext_r_off
) (loc
))
8461 /* Chances are there is a run of relocs to insert here,
8462 from one of more input files. Files are not always
8463 linked in order due to the way elf_link_input_bfd is
8464 called. See pr17666. */
8465 size_t sortlen
= p
- loc
;
8466 bfd_vma r_off2
= (*ext_r_off
) (loc
);
8467 size_t runlen
= elt_size
;
8468 size_t buf_size
= 96 * 1024;
8469 while (p
+ runlen
< end
8470 && (sortlen
<= buf_size
8471 || runlen
+ elt_size
<= buf_size
)
8472 && r_off2
> (*ext_r_off
) (p
+ runlen
))
8476 buf
= bfd_malloc (buf_size
);
8480 if (runlen
< sortlen
)
8482 memcpy (buf
, p
, runlen
);
8483 memmove (loc
+ runlen
, loc
, sortlen
);
8484 memcpy (loc
, buf
, runlen
);
8488 memcpy (buf
, loc
, sortlen
);
8489 memmove (loc
, p
, runlen
);
8490 memcpy (loc
+ runlen
, buf
, sortlen
);
8492 p
+= runlen
- elt_size
;
8495 /* Hashes are no longer valid. */
8496 free (reldata
->hashes
);
8497 reldata
->hashes
= NULL
;
8503 struct elf_link_sort_rela
8509 enum elf_reloc_type_class type
;
8510 /* We use this as an array of size int_rels_per_ext_rel. */
8511 Elf_Internal_Rela rela
[1];
8515 elf_link_sort_cmp1 (const void *A
, const void *B
)
8517 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8518 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8519 int relativea
, relativeb
;
8521 relativea
= a
->type
== reloc_class_relative
;
8522 relativeb
= b
->type
== reloc_class_relative
;
8524 if (relativea
< relativeb
)
8526 if (relativea
> relativeb
)
8528 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8530 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8532 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8534 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8540 elf_link_sort_cmp2 (const void *A
, const void *B
)
8542 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8543 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8545 if (a
->type
< b
->type
)
8547 if (a
->type
> b
->type
)
8549 if (a
->u
.offset
< b
->u
.offset
)
8551 if (a
->u
.offset
> b
->u
.offset
)
8553 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8555 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8561 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8563 asection
*dynamic_relocs
;
8566 bfd_size_type count
, size
;
8567 size_t i
, ret
, sort_elt
, ext_size
;
8568 bfd_byte
*sort
, *s_non_relative
, *p
;
8569 struct elf_link_sort_rela
*sq
;
8570 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8571 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8572 unsigned int opb
= bfd_octets_per_byte (abfd
);
8573 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8574 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8575 struct bfd_link_order
*lo
;
8577 bfd_boolean use_rela
;
8579 /* Find a dynamic reloc section. */
8580 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8581 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8582 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8583 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8585 bfd_boolean use_rela_initialised
= FALSE
;
8587 /* This is just here to stop gcc from complaining.
8588 Its initialization checking code is not perfect. */
8591 /* Both sections are present. Examine the sizes
8592 of the indirect sections to help us choose. */
8593 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8594 if (lo
->type
== bfd_indirect_link_order
)
8596 asection
*o
= lo
->u
.indirect
.section
;
8598 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8600 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8601 /* Section size is divisible by both rel and rela sizes.
8602 It is of no help to us. */
8606 /* Section size is only divisible by rela. */
8607 if (use_rela_initialised
&& (use_rela
== FALSE
))
8609 _bfd_error_handler (_("%B: Unable to sort relocs - "
8610 "they are in more than one size"),
8612 bfd_set_error (bfd_error_invalid_operation
);
8618 use_rela_initialised
= TRUE
;
8622 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8624 /* Section size is only divisible by rel. */
8625 if (use_rela_initialised
&& (use_rela
== TRUE
))
8627 _bfd_error_handler (_("%B: Unable to sort relocs - "
8628 "they are in more than one size"),
8630 bfd_set_error (bfd_error_invalid_operation
);
8636 use_rela_initialised
= TRUE
;
8641 /* The section size is not divisible by either -
8642 something is wrong. */
8643 _bfd_error_handler (_("%B: Unable to sort relocs - "
8644 "they are of an unknown size"), abfd
);
8645 bfd_set_error (bfd_error_invalid_operation
);
8650 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8651 if (lo
->type
== bfd_indirect_link_order
)
8653 asection
*o
= lo
->u
.indirect
.section
;
8655 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8657 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8658 /* Section size is divisible by both rel and rela sizes.
8659 It is of no help to us. */
8663 /* Section size is only divisible by rela. */
8664 if (use_rela_initialised
&& (use_rela
== FALSE
))
8666 _bfd_error_handler (_("%B: Unable to sort relocs - "
8667 "they are in more than one size"),
8669 bfd_set_error (bfd_error_invalid_operation
);
8675 use_rela_initialised
= TRUE
;
8679 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8681 /* Section size is only divisible by rel. */
8682 if (use_rela_initialised
&& (use_rela
== TRUE
))
8684 _bfd_error_handler (_("%B: Unable to sort relocs - "
8685 "they are in more than one size"),
8687 bfd_set_error (bfd_error_invalid_operation
);
8693 use_rela_initialised
= TRUE
;
8698 /* The section size is not divisible by either -
8699 something is wrong. */
8700 _bfd_error_handler (_("%B: Unable to sort relocs - "
8701 "they are of an unknown size"), abfd
);
8702 bfd_set_error (bfd_error_invalid_operation
);
8707 if (! use_rela_initialised
)
8711 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8713 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8720 dynamic_relocs
= rela_dyn
;
8721 ext_size
= bed
->s
->sizeof_rela
;
8722 swap_in
= bed
->s
->swap_reloca_in
;
8723 swap_out
= bed
->s
->swap_reloca_out
;
8727 dynamic_relocs
= rel_dyn
;
8728 ext_size
= bed
->s
->sizeof_rel
;
8729 swap_in
= bed
->s
->swap_reloc_in
;
8730 swap_out
= bed
->s
->swap_reloc_out
;
8734 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8735 if (lo
->type
== bfd_indirect_link_order
)
8736 size
+= lo
->u
.indirect
.section
->size
;
8738 if (size
!= dynamic_relocs
->size
)
8741 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8742 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8744 count
= dynamic_relocs
->size
/ ext_size
;
8747 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8751 (*info
->callbacks
->warning
)
8752 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8756 if (bed
->s
->arch_size
== 32)
8757 r_sym_mask
= ~(bfd_vma
) 0xff;
8759 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8761 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8762 if (lo
->type
== bfd_indirect_link_order
)
8764 bfd_byte
*erel
, *erelend
;
8765 asection
*o
= lo
->u
.indirect
.section
;
8767 if (o
->contents
== NULL
&& o
->size
!= 0)
8769 /* This is a reloc section that is being handled as a normal
8770 section. See bfd_section_from_shdr. We can't combine
8771 relocs in this case. */
8776 erelend
= o
->contents
+ o
->size
;
8777 p
= sort
+ o
->output_offset
* opb
/ ext_size
* sort_elt
;
8779 while (erel
< erelend
)
8781 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8783 (*swap_in
) (abfd
, erel
, s
->rela
);
8784 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8785 s
->u
.sym_mask
= r_sym_mask
;
8791 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8793 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8795 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8796 if (s
->type
!= reloc_class_relative
)
8802 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8803 for (; i
< count
; i
++, p
+= sort_elt
)
8805 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8806 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8808 sp
->u
.offset
= sq
->rela
->r_offset
;
8811 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8813 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
8814 if (htab
->srelplt
&& htab
->srelplt
->output_section
== dynamic_relocs
)
8816 /* We have plt relocs in .rela.dyn. */
8817 sq
= (struct elf_link_sort_rela
*) sort
;
8818 for (i
= 0; i
< count
; i
++)
8819 if (sq
[count
- i
- 1].type
!= reloc_class_plt
)
8821 if (i
!= 0 && htab
->srelplt
->size
== i
* ext_size
)
8823 struct bfd_link_order
**plo
;
8824 /* Put srelplt link_order last. This is so the output_offset
8825 set in the next loop is correct for DT_JMPREL. */
8826 for (plo
= &dynamic_relocs
->map_head
.link_order
; *plo
!= NULL
; )
8827 if ((*plo
)->type
== bfd_indirect_link_order
8828 && (*plo
)->u
.indirect
.section
== htab
->srelplt
)
8834 plo
= &(*plo
)->next
;
8837 dynamic_relocs
->map_tail
.link_order
= lo
;
8842 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8843 if (lo
->type
== bfd_indirect_link_order
)
8845 bfd_byte
*erel
, *erelend
;
8846 asection
*o
= lo
->u
.indirect
.section
;
8849 erelend
= o
->contents
+ o
->size
;
8850 o
->output_offset
= (p
- sort
) / sort_elt
* ext_size
/ opb
;
8851 while (erel
< erelend
)
8853 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8854 (*swap_out
) (abfd
, s
->rela
, erel
);
8861 *psec
= dynamic_relocs
;
8865 /* Add a symbol to the output symbol string table. */
8868 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8870 Elf_Internal_Sym
*elfsym
,
8871 asection
*input_sec
,
8872 struct elf_link_hash_entry
*h
)
8874 int (*output_symbol_hook
)
8875 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8876 struct elf_link_hash_entry
*);
8877 struct elf_link_hash_table
*hash_table
;
8878 const struct elf_backend_data
*bed
;
8879 bfd_size_type strtabsize
;
8881 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8883 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8884 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8885 if (output_symbol_hook
!= NULL
)
8887 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8894 || (input_sec
->flags
& SEC_EXCLUDE
))
8895 elfsym
->st_name
= (unsigned long) -1;
8898 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8899 to get the final offset for st_name. */
8901 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8903 if (elfsym
->st_name
== (unsigned long) -1)
8907 hash_table
= elf_hash_table (flinfo
->info
);
8908 strtabsize
= hash_table
->strtabsize
;
8909 if (strtabsize
<= hash_table
->strtabcount
)
8911 strtabsize
+= strtabsize
;
8912 hash_table
->strtabsize
= strtabsize
;
8913 strtabsize
*= sizeof (*hash_table
->strtab
);
8915 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8917 if (hash_table
->strtab
== NULL
)
8920 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8921 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8922 = hash_table
->strtabcount
;
8923 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8924 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8926 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8927 hash_table
->strtabcount
+= 1;
8932 /* Swap symbols out to the symbol table and flush the output symbols to
8936 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8938 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8941 const struct elf_backend_data
*bed
;
8943 Elf_Internal_Shdr
*hdr
;
8947 if (!hash_table
->strtabcount
)
8950 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8952 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8954 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8955 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
8959 if (flinfo
->symshndxbuf
)
8961 amt
= sizeof (Elf_External_Sym_Shndx
);
8962 amt
*= bfd_get_symcount (flinfo
->output_bfd
);
8963 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
8964 if (flinfo
->symshndxbuf
== NULL
)
8971 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
8973 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
8974 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8975 elfsym
->sym
.st_name
= 0;
8978 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
8979 elfsym
->sym
.st_name
);
8980 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
8981 ((bfd_byte
*) symbuf
8982 + (elfsym
->dest_index
8983 * bed
->s
->sizeof_sym
)),
8984 (flinfo
->symshndxbuf
8985 + elfsym
->destshndx_index
));
8988 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
8989 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8990 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
8991 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
8992 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
8994 hdr
->sh_size
+= amt
;
9002 free (hash_table
->strtab
);
9003 hash_table
->strtab
= NULL
;
9008 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9011 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
9013 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
9014 && sym
->st_shndx
< SHN_LORESERVE
)
9016 /* The gABI doesn't support dynamic symbols in output sections
9018 (*_bfd_error_handler
)
9019 (_("%B: Too many sections: %d (>= %d)"),
9020 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
9021 bfd_set_error (bfd_error_nonrepresentable_section
);
9027 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9028 allowing an unsatisfied unversioned symbol in the DSO to match a
9029 versioned symbol that would normally require an explicit version.
9030 We also handle the case that a DSO references a hidden symbol
9031 which may be satisfied by a versioned symbol in another DSO. */
9034 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
9035 const struct elf_backend_data
*bed
,
9036 struct elf_link_hash_entry
*h
)
9039 struct elf_link_loaded_list
*loaded
;
9041 if (!is_elf_hash_table (info
->hash
))
9044 /* Check indirect symbol. */
9045 while (h
->root
.type
== bfd_link_hash_indirect
)
9046 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9048 switch (h
->root
.type
)
9054 case bfd_link_hash_undefined
:
9055 case bfd_link_hash_undefweak
:
9056 abfd
= h
->root
.u
.undef
.abfd
;
9058 || (abfd
->flags
& DYNAMIC
) == 0
9059 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
9063 case bfd_link_hash_defined
:
9064 case bfd_link_hash_defweak
:
9065 abfd
= h
->root
.u
.def
.section
->owner
;
9068 case bfd_link_hash_common
:
9069 abfd
= h
->root
.u
.c
.p
->section
->owner
;
9072 BFD_ASSERT (abfd
!= NULL
);
9074 for (loaded
= elf_hash_table (info
)->loaded
;
9076 loaded
= loaded
->next
)
9079 Elf_Internal_Shdr
*hdr
;
9083 Elf_Internal_Shdr
*versymhdr
;
9084 Elf_Internal_Sym
*isym
;
9085 Elf_Internal_Sym
*isymend
;
9086 Elf_Internal_Sym
*isymbuf
;
9087 Elf_External_Versym
*ever
;
9088 Elf_External_Versym
*extversym
;
9090 input
= loaded
->abfd
;
9092 /* We check each DSO for a possible hidden versioned definition. */
9094 || (input
->flags
& DYNAMIC
) == 0
9095 || elf_dynversym (input
) == 0)
9098 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
9100 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9101 if (elf_bad_symtab (input
))
9103 extsymcount
= symcount
;
9108 extsymcount
= symcount
- hdr
->sh_info
;
9109 extsymoff
= hdr
->sh_info
;
9112 if (extsymcount
== 0)
9115 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
9117 if (isymbuf
== NULL
)
9120 /* Read in any version definitions. */
9121 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
9122 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
9123 if (extversym
== NULL
)
9126 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
9127 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
9128 != versymhdr
->sh_size
))
9136 ever
= extversym
+ extsymoff
;
9137 isymend
= isymbuf
+ extsymcount
;
9138 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
9141 Elf_Internal_Versym iver
;
9142 unsigned short version_index
;
9144 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
9145 || isym
->st_shndx
== SHN_UNDEF
)
9148 name
= bfd_elf_string_from_elf_section (input
,
9151 if (strcmp (name
, h
->root
.root
.string
) != 0)
9154 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
9156 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
9158 && h
->forced_local
))
9160 /* If we have a non-hidden versioned sym, then it should
9161 have provided a definition for the undefined sym unless
9162 it is defined in a non-shared object and forced local.
9167 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
9168 if (version_index
== 1 || version_index
== 2)
9170 /* This is the base or first version. We can use it. */
9184 /* Convert ELF common symbol TYPE. */
9187 elf_link_convert_common_type (struct bfd_link_info
*info
, int type
)
9189 /* Commom symbol can only appear in relocatable link. */
9190 if (!bfd_link_relocatable (info
))
9192 switch (info
->elf_stt_common
)
9196 case elf_stt_common
:
9199 case no_elf_stt_common
:
9206 /* Add an external symbol to the symbol table. This is called from
9207 the hash table traversal routine. When generating a shared object,
9208 we go through the symbol table twice. The first time we output
9209 anything that might have been forced to local scope in a version
9210 script. The second time we output the symbols that are still
9214 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
9216 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
9217 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
9218 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
9220 Elf_Internal_Sym sym
;
9221 asection
*input_sec
;
9222 const struct elf_backend_data
*bed
;
9226 /* A symbol is bound locally if it is forced local or it is locally
9227 defined, hidden versioned, not referenced by shared library and
9228 not exported when linking executable. */
9229 bfd_boolean local_bind
= (h
->forced_local
9230 || (bfd_link_executable (flinfo
->info
)
9231 && !flinfo
->info
->export_dynamic
9235 && h
->versioned
== versioned_hidden
));
9237 if (h
->root
.type
== bfd_link_hash_warning
)
9239 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9240 if (h
->root
.type
== bfd_link_hash_new
)
9244 /* Decide whether to output this symbol in this pass. */
9245 if (eoinfo
->localsyms
)
9256 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9258 if (h
->root
.type
== bfd_link_hash_undefined
)
9260 /* If we have an undefined symbol reference here then it must have
9261 come from a shared library that is being linked in. (Undefined
9262 references in regular files have already been handled unless
9263 they are in unreferenced sections which are removed by garbage
9265 bfd_boolean ignore_undef
= FALSE
;
9267 /* Some symbols may be special in that the fact that they're
9268 undefined can be safely ignored - let backend determine that. */
9269 if (bed
->elf_backend_ignore_undef_symbol
)
9270 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9272 /* If we are reporting errors for this situation then do so now. */
9275 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9276 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9277 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9278 (*flinfo
->info
->callbacks
->undefined_symbol
)
9279 (flinfo
->info
, h
->root
.root
.string
,
9280 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9282 flinfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
);
9284 /* Strip a global symbol defined in a discarded section. */
9289 /* We should also warn if a forced local symbol is referenced from
9290 shared libraries. */
9291 if (bfd_link_executable (flinfo
->info
)
9296 && h
->ref_dynamic_nonweak
9297 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9301 struct elf_link_hash_entry
*hi
= h
;
9303 /* Check indirect symbol. */
9304 while (hi
->root
.type
== bfd_link_hash_indirect
)
9305 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9307 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9308 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9309 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9310 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9312 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9313 def_bfd
= flinfo
->output_bfd
;
9314 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9315 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9316 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, def_bfd
,
9317 h
->root
.root
.string
);
9318 bfd_set_error (bfd_error_bad_value
);
9319 eoinfo
->failed
= TRUE
;
9323 /* We don't want to output symbols that have never been mentioned by
9324 a regular file, or that we have been told to strip. However, if
9325 h->indx is set to -2, the symbol is used by a reloc and we must
9330 else if ((h
->def_dynamic
9332 || h
->root
.type
== bfd_link_hash_new
)
9336 else if (flinfo
->info
->strip
== strip_all
)
9338 else if (flinfo
->info
->strip
== strip_some
9339 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9340 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9342 else if ((h
->root
.type
== bfd_link_hash_defined
9343 || h
->root
.type
== bfd_link_hash_defweak
)
9344 && ((flinfo
->info
->strip_discarded
9345 && discarded_section (h
->root
.u
.def
.section
))
9346 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9347 && h
->root
.u
.def
.section
->owner
!= NULL
9348 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9350 else if ((h
->root
.type
== bfd_link_hash_undefined
9351 || h
->root
.type
== bfd_link_hash_undefweak
)
9352 && h
->root
.u
.undef
.abfd
!= NULL
9353 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9358 /* If we're stripping it, and it's not a dynamic symbol, there's
9359 nothing else to do. However, if it is a forced local symbol or
9360 an ifunc symbol we need to give the backend finish_dynamic_symbol
9361 function a chance to make it dynamic. */
9364 && type
!= STT_GNU_IFUNC
9365 && !h
->forced_local
)
9369 sym
.st_size
= h
->size
;
9370 sym
.st_other
= h
->other
;
9371 switch (h
->root
.type
)
9374 case bfd_link_hash_new
:
9375 case bfd_link_hash_warning
:
9379 case bfd_link_hash_undefined
:
9380 case bfd_link_hash_undefweak
:
9381 input_sec
= bfd_und_section_ptr
;
9382 sym
.st_shndx
= SHN_UNDEF
;
9385 case bfd_link_hash_defined
:
9386 case bfd_link_hash_defweak
:
9388 input_sec
= h
->root
.u
.def
.section
;
9389 if (input_sec
->output_section
!= NULL
)
9392 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9393 input_sec
->output_section
);
9394 if (sym
.st_shndx
== SHN_BAD
)
9396 (*_bfd_error_handler
)
9397 (_("%B: could not find output section %A for input section %A"),
9398 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9399 bfd_set_error (bfd_error_nonrepresentable_section
);
9400 eoinfo
->failed
= TRUE
;
9404 /* ELF symbols in relocatable files are section relative,
9405 but in nonrelocatable files they are virtual
9407 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9408 if (!bfd_link_relocatable (flinfo
->info
))
9410 sym
.st_value
+= input_sec
->output_section
->vma
;
9411 if (h
->type
== STT_TLS
)
9413 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9414 if (tls_sec
!= NULL
)
9415 sym
.st_value
-= tls_sec
->vma
;
9421 BFD_ASSERT (input_sec
->owner
== NULL
9422 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9423 sym
.st_shndx
= SHN_UNDEF
;
9424 input_sec
= bfd_und_section_ptr
;
9429 case bfd_link_hash_common
:
9430 input_sec
= h
->root
.u
.c
.p
->section
;
9431 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9432 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9435 case bfd_link_hash_indirect
:
9436 /* These symbols are created by symbol versioning. They point
9437 to the decorated version of the name. For example, if the
9438 symbol foo@@GNU_1.2 is the default, which should be used when
9439 foo is used with no version, then we add an indirect symbol
9440 foo which points to foo@@GNU_1.2. We ignore these symbols,
9441 since the indirected symbol is already in the hash table. */
9445 if (type
== STT_COMMON
|| type
== STT_OBJECT
)
9446 switch (h
->root
.type
)
9448 case bfd_link_hash_common
:
9449 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9451 case bfd_link_hash_defined
:
9452 case bfd_link_hash_defweak
:
9453 if (bed
->common_definition (&sym
))
9454 type
= elf_link_convert_common_type (flinfo
->info
, type
);
9458 case bfd_link_hash_undefined
:
9459 case bfd_link_hash_undefweak
:
9467 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, type
);
9468 /* Turn off visibility on local symbol. */
9469 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9471 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9472 else if (h
->unique_global
&& h
->def_regular
)
9473 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, type
);
9474 else if (h
->root
.type
== bfd_link_hash_undefweak
9475 || h
->root
.type
== bfd_link_hash_defweak
)
9476 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, type
);
9478 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
9479 sym
.st_target_internal
= h
->target_internal
;
9481 /* Give the processor backend a chance to tweak the symbol value,
9482 and also to finish up anything that needs to be done for this
9483 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9484 forced local syms when non-shared is due to a historical quirk.
9485 STT_GNU_IFUNC symbol must go through PLT. */
9486 if ((h
->type
== STT_GNU_IFUNC
9488 && !bfd_link_relocatable (flinfo
->info
))
9489 || ((h
->dynindx
!= -1
9491 && ((bfd_link_pic (flinfo
->info
)
9492 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9493 || h
->root
.type
!= bfd_link_hash_undefweak
))
9494 || !h
->forced_local
)
9495 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9497 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9498 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9500 eoinfo
->failed
= TRUE
;
9505 /* If we are marking the symbol as undefined, and there are no
9506 non-weak references to this symbol from a regular object, then
9507 mark the symbol as weak undefined; if there are non-weak
9508 references, mark the symbol as strong. We can't do this earlier,
9509 because it might not be marked as undefined until the
9510 finish_dynamic_symbol routine gets through with it. */
9511 if (sym
.st_shndx
== SHN_UNDEF
9513 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9514 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9517 type
= ELF_ST_TYPE (sym
.st_info
);
9519 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9520 if (type
== STT_GNU_IFUNC
)
9523 if (h
->ref_regular_nonweak
)
9524 bindtype
= STB_GLOBAL
;
9526 bindtype
= STB_WEAK
;
9527 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9530 /* If this is a symbol defined in a dynamic library, don't use the
9531 symbol size from the dynamic library. Relinking an executable
9532 against a new library may introduce gratuitous changes in the
9533 executable's symbols if we keep the size. */
9534 if (sym
.st_shndx
== SHN_UNDEF
9539 /* If a non-weak symbol with non-default visibility is not defined
9540 locally, it is a fatal error. */
9541 if (!bfd_link_relocatable (flinfo
->info
)
9542 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9543 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9544 && h
->root
.type
== bfd_link_hash_undefined
9549 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9550 msg
= _("%B: protected symbol `%s' isn't defined");
9551 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9552 msg
= _("%B: internal symbol `%s' isn't defined");
9554 msg
= _("%B: hidden symbol `%s' isn't defined");
9555 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9556 bfd_set_error (bfd_error_bad_value
);
9557 eoinfo
->failed
= TRUE
;
9561 /* If this symbol should be put in the .dynsym section, then put it
9562 there now. We already know the symbol index. We also fill in
9563 the entry in the .hash section. */
9564 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9566 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9570 /* Since there is no version information in the dynamic string,
9571 if there is no version info in symbol version section, we will
9572 have a run-time problem if not linking executable, referenced
9573 by shared library, not locally defined, or not bound locally.
9575 if (h
->verinfo
.verdef
== NULL
9577 && (!bfd_link_executable (flinfo
->info
)
9579 || !h
->def_regular
))
9581 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9583 if (p
&& p
[1] != '\0')
9585 (*_bfd_error_handler
)
9586 (_("%B: No symbol version section for versioned symbol `%s'"),
9587 flinfo
->output_bfd
, h
->root
.root
.string
);
9588 eoinfo
->failed
= TRUE
;
9593 sym
.st_name
= h
->dynstr_index
;
9594 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9595 + h
->dynindx
* bed
->s
->sizeof_sym
);
9596 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9598 eoinfo
->failed
= TRUE
;
9601 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9603 if (flinfo
->hash_sec
!= NULL
)
9605 size_t hash_entry_size
;
9606 bfd_byte
*bucketpos
;
9611 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9612 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9615 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9616 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9617 + (bucket
+ 2) * hash_entry_size
);
9618 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9619 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9621 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9622 ((bfd_byte
*) flinfo
->hash_sec
->contents
9623 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9626 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9628 Elf_Internal_Versym iversym
;
9629 Elf_External_Versym
*eversym
;
9631 if (!h
->def_regular
)
9633 if (h
->verinfo
.verdef
== NULL
9634 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9635 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9636 iversym
.vs_vers
= 0;
9638 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9642 if (h
->verinfo
.vertree
== NULL
)
9643 iversym
.vs_vers
= 1;
9645 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9646 if (flinfo
->info
->create_default_symver
)
9650 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9652 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9653 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9655 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9656 eversym
+= h
->dynindx
;
9657 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9661 /* If the symbol is undefined, and we didn't output it to .dynsym,
9662 strip it from .symtab too. Obviously we can't do this for
9663 relocatable output or when needed for --emit-relocs. */
9664 else if (input_sec
== bfd_und_section_ptr
9666 && !bfd_link_relocatable (flinfo
->info
))
9668 /* Also strip others that we couldn't earlier due to dynamic symbol
9672 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9675 /* Output a FILE symbol so that following locals are not associated
9676 with the wrong input file. We need one for forced local symbols
9677 if we've seen more than one FILE symbol or when we have exactly
9678 one FILE symbol but global symbols are present in a file other
9679 than the one with the FILE symbol. We also need one if linker
9680 defined symbols are present. In practice these conditions are
9681 always met, so just emit the FILE symbol unconditionally. */
9682 if (eoinfo
->localsyms
9683 && !eoinfo
->file_sym_done
9684 && eoinfo
->flinfo
->filesym_count
!= 0)
9686 Elf_Internal_Sym fsym
;
9688 memset (&fsym
, 0, sizeof (fsym
));
9689 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9690 fsym
.st_shndx
= SHN_ABS
;
9691 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9692 bfd_und_section_ptr
, NULL
))
9695 eoinfo
->file_sym_done
= TRUE
;
9698 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9699 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9703 eoinfo
->failed
= TRUE
;
9708 else if (h
->indx
== -2)
9714 /* Return TRUE if special handling is done for relocs in SEC against
9715 symbols defined in discarded sections. */
9718 elf_section_ignore_discarded_relocs (asection
*sec
)
9720 const struct elf_backend_data
*bed
;
9722 switch (sec
->sec_info_type
)
9724 case SEC_INFO_TYPE_STABS
:
9725 case SEC_INFO_TYPE_EH_FRAME
:
9726 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9732 bed
= get_elf_backend_data (sec
->owner
);
9733 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9734 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9740 /* Return a mask saying how ld should treat relocations in SEC against
9741 symbols defined in discarded sections. If this function returns
9742 COMPLAIN set, ld will issue a warning message. If this function
9743 returns PRETEND set, and the discarded section was link-once and the
9744 same size as the kept link-once section, ld will pretend that the
9745 symbol was actually defined in the kept section. Otherwise ld will
9746 zero the reloc (at least that is the intent, but some cooperation by
9747 the target dependent code is needed, particularly for REL targets). */
9750 _bfd_elf_default_action_discarded (asection
*sec
)
9752 if (sec
->flags
& SEC_DEBUGGING
)
9755 if (strcmp (".eh_frame", sec
->name
) == 0)
9758 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9761 return COMPLAIN
| PRETEND
;
9764 /* Find a match between a section and a member of a section group. */
9767 match_group_member (asection
*sec
, asection
*group
,
9768 struct bfd_link_info
*info
)
9770 asection
*first
= elf_next_in_group (group
);
9771 asection
*s
= first
;
9775 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9778 s
= elf_next_in_group (s
);
9786 /* Check if the kept section of a discarded section SEC can be used
9787 to replace it. Return the replacement if it is OK. Otherwise return
9791 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9795 kept
= sec
->kept_section
;
9798 if ((kept
->flags
& SEC_GROUP
) != 0)
9799 kept
= match_group_member (sec
, kept
, info
);
9801 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9802 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9804 sec
->kept_section
= kept
;
9809 /* Link an input file into the linker output file. This function
9810 handles all the sections and relocations of the input file at once.
9811 This is so that we only have to read the local symbols once, and
9812 don't have to keep them in memory. */
9815 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9817 int (*relocate_section
)
9818 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9819 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9821 Elf_Internal_Shdr
*symtab_hdr
;
9824 Elf_Internal_Sym
*isymbuf
;
9825 Elf_Internal_Sym
*isym
;
9826 Elf_Internal_Sym
*isymend
;
9828 asection
**ppsection
;
9830 const struct elf_backend_data
*bed
;
9831 struct elf_link_hash_entry
**sym_hashes
;
9832 bfd_size_type address_size
;
9833 bfd_vma r_type_mask
;
9835 bfd_boolean have_file_sym
= FALSE
;
9837 output_bfd
= flinfo
->output_bfd
;
9838 bed
= get_elf_backend_data (output_bfd
);
9839 relocate_section
= bed
->elf_backend_relocate_section
;
9841 /* If this is a dynamic object, we don't want to do anything here:
9842 we don't want the local symbols, and we don't want the section
9844 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9847 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9848 if (elf_bad_symtab (input_bfd
))
9850 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9855 locsymcount
= symtab_hdr
->sh_info
;
9856 extsymoff
= symtab_hdr
->sh_info
;
9859 /* Read the local symbols. */
9860 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9861 if (isymbuf
== NULL
&& locsymcount
!= 0)
9863 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9864 flinfo
->internal_syms
,
9865 flinfo
->external_syms
,
9866 flinfo
->locsym_shndx
);
9867 if (isymbuf
== NULL
)
9871 /* Find local symbol sections and adjust values of symbols in
9872 SEC_MERGE sections. Write out those local symbols we know are
9873 going into the output file. */
9874 isymend
= isymbuf
+ locsymcount
;
9875 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9877 isym
++, pindex
++, ppsection
++)
9881 Elf_Internal_Sym osym
;
9887 if (elf_bad_symtab (input_bfd
))
9889 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9896 if (isym
->st_shndx
== SHN_UNDEF
)
9897 isec
= bfd_und_section_ptr
;
9898 else if (isym
->st_shndx
== SHN_ABS
)
9899 isec
= bfd_abs_section_ptr
;
9900 else if (isym
->st_shndx
== SHN_COMMON
)
9901 isec
= bfd_com_section_ptr
;
9904 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9907 /* Don't attempt to output symbols with st_shnx in the
9908 reserved range other than SHN_ABS and SHN_COMMON. */
9912 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9913 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9915 _bfd_merged_section_offset (output_bfd
, &isec
,
9916 elf_section_data (isec
)->sec_info
,
9922 /* Don't output the first, undefined, symbol. In fact, don't
9923 output any undefined local symbol. */
9924 if (isec
== bfd_und_section_ptr
)
9927 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9929 /* We never output section symbols. Instead, we use the
9930 section symbol of the corresponding section in the output
9935 /* If we are stripping all symbols, we don't want to output this
9937 if (flinfo
->info
->strip
== strip_all
)
9940 /* If we are discarding all local symbols, we don't want to
9941 output this one. If we are generating a relocatable output
9942 file, then some of the local symbols may be required by
9943 relocs; we output them below as we discover that they are
9945 if (flinfo
->info
->discard
== discard_all
)
9948 /* If this symbol is defined in a section which we are
9949 discarding, we don't need to keep it. */
9950 if (isym
->st_shndx
!= SHN_UNDEF
9951 && isym
->st_shndx
< SHN_LORESERVE
9952 && bfd_section_removed_from_list (output_bfd
,
9953 isec
->output_section
))
9956 /* Get the name of the symbol. */
9957 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9962 /* See if we are discarding symbols with this name. */
9963 if ((flinfo
->info
->strip
== strip_some
9964 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9966 || (((flinfo
->info
->discard
== discard_sec_merge
9967 && (isec
->flags
& SEC_MERGE
)
9968 && !bfd_link_relocatable (flinfo
->info
))
9969 || flinfo
->info
->discard
== discard_l
)
9970 && bfd_is_local_label_name (input_bfd
, name
)))
9973 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
9975 if (input_bfd
->lto_output
)
9976 /* -flto puts a temp file name here. This means builds
9977 are not reproducible. Discard the symbol. */
9979 have_file_sym
= TRUE
;
9980 flinfo
->filesym_count
+= 1;
9984 /* In the absence of debug info, bfd_find_nearest_line uses
9985 FILE symbols to determine the source file for local
9986 function symbols. Provide a FILE symbol here if input
9987 files lack such, so that their symbols won't be
9988 associated with a previous input file. It's not the
9989 source file, but the best we can do. */
9990 have_file_sym
= TRUE
;
9991 flinfo
->filesym_count
+= 1;
9992 memset (&osym
, 0, sizeof (osym
));
9993 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9994 osym
.st_shndx
= SHN_ABS
;
9995 if (!elf_link_output_symstrtab (flinfo
,
9996 (input_bfd
->lto_output
? NULL
9997 : input_bfd
->filename
),
9998 &osym
, bfd_abs_section_ptr
,
10005 /* Adjust the section index for the output file. */
10006 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10007 isec
->output_section
);
10008 if (osym
.st_shndx
== SHN_BAD
)
10011 /* ELF symbols in relocatable files are section relative, but
10012 in executable files they are virtual addresses. Note that
10013 this code assumes that all ELF sections have an associated
10014 BFD section with a reasonable value for output_offset; below
10015 we assume that they also have a reasonable value for
10016 output_section. Any special sections must be set up to meet
10017 these requirements. */
10018 osym
.st_value
+= isec
->output_offset
;
10019 if (!bfd_link_relocatable (flinfo
->info
))
10021 osym
.st_value
+= isec
->output_section
->vma
;
10022 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
10024 /* STT_TLS symbols are relative to PT_TLS segment base. */
10025 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
10026 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
10030 indx
= bfd_get_symcount (output_bfd
);
10031 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
10038 if (bed
->s
->arch_size
== 32)
10040 r_type_mask
= 0xff;
10046 r_type_mask
= 0xffffffff;
10051 /* Relocate the contents of each section. */
10052 sym_hashes
= elf_sym_hashes (input_bfd
);
10053 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
10055 bfd_byte
*contents
;
10057 if (! o
->linker_mark
)
10059 /* This section was omitted from the link. */
10063 if (bfd_link_relocatable (flinfo
->info
)
10064 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
10066 /* Deal with the group signature symbol. */
10067 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
10068 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
10069 asection
*osec
= o
->output_section
;
10071 if (symndx
>= locsymcount
10072 || (elf_bad_symtab (input_bfd
)
10073 && flinfo
->sections
[symndx
] == NULL
))
10075 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
10076 while (h
->root
.type
== bfd_link_hash_indirect
10077 || h
->root
.type
== bfd_link_hash_warning
)
10078 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10079 /* Arrange for symbol to be output. */
10081 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
10083 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
10085 /* We'll use the output section target_index. */
10086 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10087 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
10091 if (flinfo
->indices
[symndx
] == -1)
10093 /* Otherwise output the local symbol now. */
10094 Elf_Internal_Sym sym
= isymbuf
[symndx
];
10095 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
10100 name
= bfd_elf_string_from_elf_section (input_bfd
,
10101 symtab_hdr
->sh_link
,
10106 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
10108 if (sym
.st_shndx
== SHN_BAD
)
10111 sym
.st_value
+= o
->output_offset
;
10113 indx
= bfd_get_symcount (output_bfd
);
10114 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
10119 flinfo
->indices
[symndx
] = indx
;
10123 elf_section_data (osec
)->this_hdr
.sh_info
10124 = flinfo
->indices
[symndx
];
10128 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
10129 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
10132 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
10134 /* Section was created by _bfd_elf_link_create_dynamic_sections
10139 /* Get the contents of the section. They have been cached by a
10140 relaxation routine. Note that o is a section in an input
10141 file, so the contents field will not have been set by any of
10142 the routines which work on output files. */
10143 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
10145 contents
= elf_section_data (o
)->this_hdr
.contents
;
10146 if (bed
->caches_rawsize
10148 && o
->rawsize
< o
->size
)
10150 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
10151 contents
= flinfo
->contents
;
10156 contents
= flinfo
->contents
;
10157 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
10161 if ((o
->flags
& SEC_RELOC
) != 0)
10163 Elf_Internal_Rela
*internal_relocs
;
10164 Elf_Internal_Rela
*rel
, *relend
;
10165 int action_discarded
;
10168 /* Get the swapped relocs. */
10170 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
10171 flinfo
->internal_relocs
, FALSE
);
10172 if (internal_relocs
== NULL
10173 && o
->reloc_count
> 0)
10176 /* We need to reverse-copy input .ctors/.dtors sections if
10177 they are placed in .init_array/.finit_array for output. */
10178 if (o
->size
> address_size
10179 && ((strncmp (o
->name
, ".ctors", 6) == 0
10180 && strcmp (o
->output_section
->name
,
10181 ".init_array") == 0)
10182 || (strncmp (o
->name
, ".dtors", 6) == 0
10183 && strcmp (o
->output_section
->name
,
10184 ".fini_array") == 0))
10185 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
10187 if (o
->size
!= o
->reloc_count
* address_size
)
10189 (*_bfd_error_handler
)
10190 (_("error: %B: size of section %A is not "
10191 "multiple of address size"),
10193 bfd_set_error (bfd_error_on_input
);
10196 o
->flags
|= SEC_ELF_REVERSE_COPY
;
10199 action_discarded
= -1;
10200 if (!elf_section_ignore_discarded_relocs (o
))
10201 action_discarded
= (*bed
->action_discarded
) (o
);
10203 /* Run through the relocs evaluating complex reloc symbols and
10204 looking for relocs against symbols from discarded sections
10205 or section symbols from removed link-once sections.
10206 Complain about relocs against discarded sections. Zero
10207 relocs against removed link-once sections. */
10209 rel
= internal_relocs
;
10210 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10211 for ( ; rel
< relend
; rel
++)
10213 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
10214 unsigned int s_type
;
10215 asection
**ps
, *sec
;
10216 struct elf_link_hash_entry
*h
= NULL
;
10217 const char *sym_name
;
10219 if (r_symndx
== STN_UNDEF
)
10222 if (r_symndx
>= locsymcount
10223 || (elf_bad_symtab (input_bfd
)
10224 && flinfo
->sections
[r_symndx
] == NULL
))
10226 h
= sym_hashes
[r_symndx
- extsymoff
];
10228 /* Badly formatted input files can contain relocs that
10229 reference non-existant symbols. Check here so that
10230 we do not seg fault. */
10235 sprintf_vma (buffer
, rel
->r_info
);
10236 (*_bfd_error_handler
)
10237 (_("error: %B contains a reloc (0x%s) for section %A "
10238 "that references a non-existent global symbol"),
10239 input_bfd
, o
, buffer
);
10240 bfd_set_error (bfd_error_bad_value
);
10244 while (h
->root
.type
== bfd_link_hash_indirect
10245 || h
->root
.type
== bfd_link_hash_warning
)
10246 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10250 /* If a plugin symbol is referenced from a non-IR file,
10251 mark the symbol as undefined. Note that the
10252 linker may attach linker created dynamic sections
10253 to the plugin bfd. Symbols defined in linker
10254 created sections are not plugin symbols. */
10255 if (h
->root
.non_ir_ref
10256 && (h
->root
.type
== bfd_link_hash_defined
10257 || h
->root
.type
== bfd_link_hash_defweak
)
10258 && (h
->root
.u
.def
.section
->flags
10259 & SEC_LINKER_CREATED
) == 0
10260 && h
->root
.u
.def
.section
->owner
!= NULL
10261 && (h
->root
.u
.def
.section
->owner
->flags
10262 & BFD_PLUGIN
) != 0)
10264 h
->root
.type
= bfd_link_hash_undefined
;
10265 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10269 if (h
->root
.type
== bfd_link_hash_defined
10270 || h
->root
.type
== bfd_link_hash_defweak
)
10271 ps
= &h
->root
.u
.def
.section
;
10273 sym_name
= h
->root
.root
.string
;
10277 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10279 s_type
= ELF_ST_TYPE (sym
->st_info
);
10280 ps
= &flinfo
->sections
[r_symndx
];
10281 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10285 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10286 && !bfd_link_relocatable (flinfo
->info
))
10289 bfd_vma dot
= (rel
->r_offset
10290 + o
->output_offset
+ o
->output_section
->vma
);
10292 printf ("Encountered a complex symbol!");
10293 printf (" (input_bfd %s, section %s, reloc %ld\n",
10294 input_bfd
->filename
, o
->name
,
10295 (long) (rel
- internal_relocs
));
10296 printf (" symbol: idx %8.8lx, name %s\n",
10297 r_symndx
, sym_name
);
10298 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10299 (unsigned long) rel
->r_info
,
10300 (unsigned long) rel
->r_offset
);
10302 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10303 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10306 /* Symbol evaluated OK. Update to absolute value. */
10307 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10312 if (action_discarded
!= -1 && ps
!= NULL
)
10314 /* Complain if the definition comes from a
10315 discarded section. */
10316 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10318 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10319 if (action_discarded
& COMPLAIN
)
10320 (*flinfo
->info
->callbacks
->einfo
)
10321 (_("%X`%s' referenced in section `%A' of %B: "
10322 "defined in discarded section `%A' of %B\n"),
10323 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10325 /* Try to do the best we can to support buggy old
10326 versions of gcc. Pretend that the symbol is
10327 really defined in the kept linkonce section.
10328 FIXME: This is quite broken. Modifying the
10329 symbol here means we will be changing all later
10330 uses of the symbol, not just in this section. */
10331 if (action_discarded
& PRETEND
)
10335 kept
= _bfd_elf_check_kept_section (sec
,
10347 /* Relocate the section by invoking a back end routine.
10349 The back end routine is responsible for adjusting the
10350 section contents as necessary, and (if using Rela relocs
10351 and generating a relocatable output file) adjusting the
10352 reloc addend as necessary.
10354 The back end routine does not have to worry about setting
10355 the reloc address or the reloc symbol index.
10357 The back end routine is given a pointer to the swapped in
10358 internal symbols, and can access the hash table entries
10359 for the external symbols via elf_sym_hashes (input_bfd).
10361 When generating relocatable output, the back end routine
10362 must handle STB_LOCAL/STT_SECTION symbols specially. The
10363 output symbol is going to be a section symbol
10364 corresponding to the output section, which will require
10365 the addend to be adjusted. */
10367 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10368 input_bfd
, o
, contents
,
10376 || bfd_link_relocatable (flinfo
->info
)
10377 || flinfo
->info
->emitrelocations
)
10379 Elf_Internal_Rela
*irela
;
10380 Elf_Internal_Rela
*irelaend
, *irelamid
;
10381 bfd_vma last_offset
;
10382 struct elf_link_hash_entry
**rel_hash
;
10383 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10384 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10385 unsigned int next_erel
;
10386 bfd_boolean rela_normal
;
10387 struct bfd_elf_section_data
*esdi
, *esdo
;
10389 esdi
= elf_section_data (o
);
10390 esdo
= elf_section_data (o
->output_section
);
10391 rela_normal
= FALSE
;
10393 /* Adjust the reloc addresses and symbol indices. */
10395 irela
= internal_relocs
;
10396 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10397 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10398 /* We start processing the REL relocs, if any. When we reach
10399 IRELAMID in the loop, we switch to the RELA relocs. */
10401 if (esdi
->rel
.hdr
!= NULL
)
10402 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10403 * bed
->s
->int_rels_per_ext_rel
);
10404 rel_hash_list
= rel_hash
;
10405 rela_hash_list
= NULL
;
10406 last_offset
= o
->output_offset
;
10407 if (!bfd_link_relocatable (flinfo
->info
))
10408 last_offset
+= o
->output_section
->vma
;
10409 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10411 unsigned long r_symndx
;
10413 Elf_Internal_Sym sym
;
10415 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10421 if (irela
== irelamid
)
10423 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10424 rela_hash_list
= rel_hash
;
10425 rela_normal
= bed
->rela_normal
;
10428 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10431 if (irela
->r_offset
>= (bfd_vma
) -2)
10433 /* This is a reloc for a deleted entry or somesuch.
10434 Turn it into an R_*_NONE reloc, at the same
10435 offset as the last reloc. elf_eh_frame.c and
10436 bfd_elf_discard_info rely on reloc offsets
10438 irela
->r_offset
= last_offset
;
10440 irela
->r_addend
= 0;
10444 irela
->r_offset
+= o
->output_offset
;
10446 /* Relocs in an executable have to be virtual addresses. */
10447 if (!bfd_link_relocatable (flinfo
->info
))
10448 irela
->r_offset
+= o
->output_section
->vma
;
10450 last_offset
= irela
->r_offset
;
10452 r_symndx
= irela
->r_info
>> r_sym_shift
;
10453 if (r_symndx
== STN_UNDEF
)
10456 if (r_symndx
>= locsymcount
10457 || (elf_bad_symtab (input_bfd
)
10458 && flinfo
->sections
[r_symndx
] == NULL
))
10460 struct elf_link_hash_entry
*rh
;
10461 unsigned long indx
;
10463 /* This is a reloc against a global symbol. We
10464 have not yet output all the local symbols, so
10465 we do not know the symbol index of any global
10466 symbol. We set the rel_hash entry for this
10467 reloc to point to the global hash table entry
10468 for this symbol. The symbol index is then
10469 set at the end of bfd_elf_final_link. */
10470 indx
= r_symndx
- extsymoff
;
10471 rh
= elf_sym_hashes (input_bfd
)[indx
];
10472 while (rh
->root
.type
== bfd_link_hash_indirect
10473 || rh
->root
.type
== bfd_link_hash_warning
)
10474 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10476 /* Setting the index to -2 tells
10477 elf_link_output_extsym that this symbol is
10478 used by a reloc. */
10479 BFD_ASSERT (rh
->indx
< 0);
10487 /* This is a reloc against a local symbol. */
10490 sym
= isymbuf
[r_symndx
];
10491 sec
= flinfo
->sections
[r_symndx
];
10492 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10494 /* I suppose the backend ought to fill in the
10495 section of any STT_SECTION symbol against a
10496 processor specific section. */
10497 r_symndx
= STN_UNDEF
;
10498 if (bfd_is_abs_section (sec
))
10500 else if (sec
== NULL
|| sec
->owner
== NULL
)
10502 bfd_set_error (bfd_error_bad_value
);
10507 asection
*osec
= sec
->output_section
;
10509 /* If we have discarded a section, the output
10510 section will be the absolute section. In
10511 case of discarded SEC_MERGE sections, use
10512 the kept section. relocate_section should
10513 have already handled discarded linkonce
10515 if (bfd_is_abs_section (osec
)
10516 && sec
->kept_section
!= NULL
10517 && sec
->kept_section
->output_section
!= NULL
)
10519 osec
= sec
->kept_section
->output_section
;
10520 irela
->r_addend
-= osec
->vma
;
10523 if (!bfd_is_abs_section (osec
))
10525 r_symndx
= osec
->target_index
;
10526 if (r_symndx
== STN_UNDEF
)
10528 irela
->r_addend
+= osec
->vma
;
10529 osec
= _bfd_nearby_section (output_bfd
, osec
,
10531 irela
->r_addend
-= osec
->vma
;
10532 r_symndx
= osec
->target_index
;
10537 /* Adjust the addend according to where the
10538 section winds up in the output section. */
10540 irela
->r_addend
+= sec
->output_offset
;
10544 if (flinfo
->indices
[r_symndx
] == -1)
10546 unsigned long shlink
;
10551 if (flinfo
->info
->strip
== strip_all
)
10553 /* You can't do ld -r -s. */
10554 bfd_set_error (bfd_error_invalid_operation
);
10558 /* This symbol was skipped earlier, but
10559 since it is needed by a reloc, we
10560 must output it now. */
10561 shlink
= symtab_hdr
->sh_link
;
10562 name
= (bfd_elf_string_from_elf_section
10563 (input_bfd
, shlink
, sym
.st_name
));
10567 osec
= sec
->output_section
;
10569 _bfd_elf_section_from_bfd_section (output_bfd
,
10571 if (sym
.st_shndx
== SHN_BAD
)
10574 sym
.st_value
+= sec
->output_offset
;
10575 if (!bfd_link_relocatable (flinfo
->info
))
10577 sym
.st_value
+= osec
->vma
;
10578 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10580 /* STT_TLS symbols are relative to PT_TLS
10582 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10583 ->tls_sec
!= NULL
);
10584 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10589 indx
= bfd_get_symcount (output_bfd
);
10590 ret
= elf_link_output_symstrtab (flinfo
, name
,
10596 flinfo
->indices
[r_symndx
] = indx
;
10601 r_symndx
= flinfo
->indices
[r_symndx
];
10604 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10605 | (irela
->r_info
& r_type_mask
));
10608 /* Swap out the relocs. */
10609 input_rel_hdr
= esdi
->rel
.hdr
;
10610 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10612 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10617 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10618 * bed
->s
->int_rels_per_ext_rel
);
10619 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10622 input_rela_hdr
= esdi
->rela
.hdr
;
10623 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10625 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10634 /* Write out the modified section contents. */
10635 if (bed
->elf_backend_write_section
10636 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10639 /* Section written out. */
10641 else switch (o
->sec_info_type
)
10643 case SEC_INFO_TYPE_STABS
:
10644 if (! (_bfd_write_section_stabs
10646 &elf_hash_table (flinfo
->info
)->stab_info
,
10647 o
, &elf_section_data (o
)->sec_info
, contents
)))
10650 case SEC_INFO_TYPE_MERGE
:
10651 if (! _bfd_write_merged_section (output_bfd
, o
,
10652 elf_section_data (o
)->sec_info
))
10655 case SEC_INFO_TYPE_EH_FRAME
:
10657 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10662 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10664 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10672 if (! (o
->flags
& SEC_EXCLUDE
))
10674 file_ptr offset
= (file_ptr
) o
->output_offset
;
10675 bfd_size_type todo
= o
->size
;
10677 offset
*= bfd_octets_per_byte (output_bfd
);
10679 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10681 /* Reverse-copy input section to output. */
10684 todo
-= address_size
;
10685 if (! bfd_set_section_contents (output_bfd
,
10693 offset
+= address_size
;
10697 else if (! bfd_set_section_contents (output_bfd
,
10711 /* Generate a reloc when linking an ELF file. This is a reloc
10712 requested by the linker, and does not come from any input file. This
10713 is used to build constructor and destructor tables when linking
10717 elf_reloc_link_order (bfd
*output_bfd
,
10718 struct bfd_link_info
*info
,
10719 asection
*output_section
,
10720 struct bfd_link_order
*link_order
)
10722 reloc_howto_type
*howto
;
10726 struct bfd_elf_section_reloc_data
*reldata
;
10727 struct elf_link_hash_entry
**rel_hash_ptr
;
10728 Elf_Internal_Shdr
*rel_hdr
;
10729 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10730 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10733 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10735 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10738 bfd_set_error (bfd_error_bad_value
);
10742 addend
= link_order
->u
.reloc
.p
->addend
;
10745 reldata
= &esdo
->rel
;
10746 else if (esdo
->rela
.hdr
)
10747 reldata
= &esdo
->rela
;
10754 /* Figure out the symbol index. */
10755 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10756 if (link_order
->type
== bfd_section_reloc_link_order
)
10758 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10759 BFD_ASSERT (indx
!= 0);
10760 *rel_hash_ptr
= NULL
;
10764 struct elf_link_hash_entry
*h
;
10766 /* Treat a reloc against a defined symbol as though it were
10767 actually against the section. */
10768 h
= ((struct elf_link_hash_entry
*)
10769 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10770 link_order
->u
.reloc
.p
->u
.name
,
10771 FALSE
, FALSE
, TRUE
));
10773 && (h
->root
.type
== bfd_link_hash_defined
10774 || h
->root
.type
== bfd_link_hash_defweak
))
10778 section
= h
->root
.u
.def
.section
;
10779 indx
= section
->output_section
->target_index
;
10780 *rel_hash_ptr
= NULL
;
10781 /* It seems that we ought to add the symbol value to the
10782 addend here, but in practice it has already been added
10783 because it was passed to constructor_callback. */
10784 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10786 else if (h
!= NULL
)
10788 /* Setting the index to -2 tells elf_link_output_extsym that
10789 this symbol is used by a reloc. */
10796 (*info
->callbacks
->unattached_reloc
)
10797 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0);
10802 /* If this is an inplace reloc, we must write the addend into the
10804 if (howto
->partial_inplace
&& addend
!= 0)
10806 bfd_size_type size
;
10807 bfd_reloc_status_type rstat
;
10810 const char *sym_name
;
10812 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10813 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10814 if (buf
== NULL
&& size
!= 0)
10816 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10823 case bfd_reloc_outofrange
:
10826 case bfd_reloc_overflow
:
10827 if (link_order
->type
== bfd_section_reloc_link_order
)
10828 sym_name
= bfd_section_name (output_bfd
,
10829 link_order
->u
.reloc
.p
->u
.section
);
10831 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10832 (*info
->callbacks
->reloc_overflow
) (info
, NULL
, sym_name
,
10833 howto
->name
, addend
, NULL
, NULL
,
10838 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10840 * bfd_octets_per_byte (output_bfd
),
10847 /* The address of a reloc is relative to the section in a
10848 relocatable file, and is a virtual address in an executable
10850 offset
= link_order
->offset
;
10851 if (! bfd_link_relocatable (info
))
10852 offset
+= output_section
->vma
;
10854 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10856 irel
[i
].r_offset
= offset
;
10857 irel
[i
].r_info
= 0;
10858 irel
[i
].r_addend
= 0;
10860 if (bed
->s
->arch_size
== 32)
10861 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10863 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10865 rel_hdr
= reldata
->hdr
;
10866 erel
= rel_hdr
->contents
;
10867 if (rel_hdr
->sh_type
== SHT_REL
)
10869 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10870 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10874 irel
[0].r_addend
= addend
;
10875 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10876 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10885 /* Get the output vma of the section pointed to by the sh_link field. */
10888 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10890 Elf_Internal_Shdr
**elf_shdrp
;
10894 s
= p
->u
.indirect
.section
;
10895 elf_shdrp
= elf_elfsections (s
->owner
);
10896 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10897 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10899 The Intel C compiler generates SHT_IA_64_UNWIND with
10900 SHF_LINK_ORDER. But it doesn't set the sh_link or
10901 sh_info fields. Hence we could get the situation
10902 where elfsec is 0. */
10905 const struct elf_backend_data
*bed
10906 = get_elf_backend_data (s
->owner
);
10907 if (bed
->link_order_error_handler
)
10908 bed
->link_order_error_handler
10909 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10914 s
= elf_shdrp
[elfsec
]->bfd_section
;
10915 return s
->output_section
->vma
+ s
->output_offset
;
10920 /* Compare two sections based on the locations of the sections they are
10921 linked to. Used by elf_fixup_link_order. */
10924 compare_link_order (const void * a
, const void * b
)
10929 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10930 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10933 return apos
> bpos
;
10937 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10938 order as their linked sections. Returns false if this could not be done
10939 because an output section includes both ordered and unordered
10940 sections. Ideally we'd do this in the linker proper. */
10943 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10945 int seen_linkorder
;
10948 struct bfd_link_order
*p
;
10950 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10952 struct bfd_link_order
**sections
;
10953 asection
*s
, *other_sec
, *linkorder_sec
;
10957 linkorder_sec
= NULL
;
10959 seen_linkorder
= 0;
10960 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10962 if (p
->type
== bfd_indirect_link_order
)
10964 s
= p
->u
.indirect
.section
;
10966 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10967 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
10968 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
10969 && elfsec
< elf_numsections (sub
)
10970 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
10971 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
10985 if (seen_other
&& seen_linkorder
)
10987 if (other_sec
&& linkorder_sec
)
10988 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10990 linkorder_sec
->owner
, other_sec
,
10993 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
10995 bfd_set_error (bfd_error_bad_value
);
11000 if (!seen_linkorder
)
11003 sections
= (struct bfd_link_order
**)
11004 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
11005 if (sections
== NULL
)
11007 seen_linkorder
= 0;
11009 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11011 sections
[seen_linkorder
++] = p
;
11013 /* Sort the input sections in the order of their linked section. */
11014 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
11015 compare_link_order
);
11017 /* Change the offsets of the sections. */
11019 for (n
= 0; n
< seen_linkorder
; n
++)
11021 s
= sections
[n
]->u
.indirect
.section
;
11022 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
11023 s
->output_offset
= offset
/ bfd_octets_per_byte (abfd
);
11024 sections
[n
]->offset
= offset
;
11025 offset
+= sections
[n
]->size
;
11032 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11033 Returns TRUE upon success, FALSE otherwise. */
11036 elf_output_implib (bfd
*abfd
, struct bfd_link_info
*info
)
11038 bfd_boolean ret
= FALSE
;
11040 const struct elf_backend_data
*bed
;
11042 enum bfd_architecture arch
;
11044 asymbol
**sympp
= NULL
;
11048 elf_symbol_type
*osymbuf
;
11050 implib_bfd
= info
->out_implib_bfd
;
11051 bed
= get_elf_backend_data (abfd
);
11053 if (!bfd_set_format (implib_bfd
, bfd_object
))
11056 flags
= bfd_get_file_flags (abfd
);
11057 flags
&= ~HAS_RELOC
;
11058 if (!bfd_set_start_address (implib_bfd
, 0)
11059 || !bfd_set_file_flags (implib_bfd
, flags
))
11062 /* Copy architecture of output file to import library file. */
11063 arch
= bfd_get_arch (abfd
);
11064 mach
= bfd_get_mach (abfd
);
11065 if (!bfd_set_arch_mach (implib_bfd
, arch
, mach
)
11066 && (abfd
->target_defaulted
11067 || bfd_get_arch (abfd
) != bfd_get_arch (implib_bfd
)))
11070 /* Get symbol table size. */
11071 symsize
= bfd_get_symtab_upper_bound (abfd
);
11075 /* Read in the symbol table. */
11076 sympp
= (asymbol
**) xmalloc (symsize
);
11077 symcount
= bfd_canonicalize_symtab (abfd
, sympp
);
11081 /* Allow the BFD backend to copy any private header data it
11082 understands from the output BFD to the import library BFD. */
11083 if (! bfd_copy_private_header_data (abfd
, implib_bfd
))
11086 /* Filter symbols to appear in the import library. */
11087 if (bed
->elf_backend_filter_implib_symbols
)
11088 symcount
= bed
->elf_backend_filter_implib_symbols (abfd
, info
, sympp
,
11091 symcount
= _bfd_elf_filter_global_symbols (abfd
, info
, sympp
, symcount
);
11094 (*_bfd_error_handler
) (_("%B: no symbol found for import library"),
11100 /* Make symbols absolute. */
11101 osymbuf
= (elf_symbol_type
*) bfd_alloc2 (implib_bfd
, symcount
,
11102 sizeof (*osymbuf
));
11103 for (src_count
= 0; src_count
< symcount
; src_count
++)
11105 memcpy (&osymbuf
[src_count
], (elf_symbol_type
*) sympp
[src_count
],
11106 sizeof (*osymbuf
));
11107 osymbuf
[src_count
].symbol
.section
= bfd_abs_section_ptr
;
11108 osymbuf
[src_count
].internal_elf_sym
.st_shndx
= SHN_ABS
;
11109 osymbuf
[src_count
].symbol
.value
+= sympp
[src_count
]->section
->vma
;
11110 osymbuf
[src_count
].internal_elf_sym
.st_value
=
11111 osymbuf
[src_count
].symbol
.value
;
11112 sympp
[src_count
] = &osymbuf
[src_count
].symbol
;
11115 bfd_set_symtab (implib_bfd
, sympp
, symcount
);
11117 /* Allow the BFD backend to copy any private data it understands
11118 from the output BFD to the import library BFD. This is done last
11119 to permit the routine to look at the filtered symbol table. */
11120 if (! bfd_copy_private_bfd_data (abfd
, implib_bfd
))
11123 if (!bfd_close (implib_bfd
))
11134 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
11138 if (flinfo
->symstrtab
!= NULL
)
11139 _bfd_elf_strtab_free (flinfo
->symstrtab
);
11140 if (flinfo
->contents
!= NULL
)
11141 free (flinfo
->contents
);
11142 if (flinfo
->external_relocs
!= NULL
)
11143 free (flinfo
->external_relocs
);
11144 if (flinfo
->internal_relocs
!= NULL
)
11145 free (flinfo
->internal_relocs
);
11146 if (flinfo
->external_syms
!= NULL
)
11147 free (flinfo
->external_syms
);
11148 if (flinfo
->locsym_shndx
!= NULL
)
11149 free (flinfo
->locsym_shndx
);
11150 if (flinfo
->internal_syms
!= NULL
)
11151 free (flinfo
->internal_syms
);
11152 if (flinfo
->indices
!= NULL
)
11153 free (flinfo
->indices
);
11154 if (flinfo
->sections
!= NULL
)
11155 free (flinfo
->sections
);
11156 if (flinfo
->symshndxbuf
!= NULL
)
11157 free (flinfo
->symshndxbuf
);
11158 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
11160 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11161 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
11162 free (esdo
->rel
.hashes
);
11163 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
11164 free (esdo
->rela
.hashes
);
11168 /* Do the final step of an ELF link. */
11171 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
11173 bfd_boolean dynamic
;
11174 bfd_boolean emit_relocs
;
11176 struct elf_final_link_info flinfo
;
11178 struct bfd_link_order
*p
;
11180 bfd_size_type max_contents_size
;
11181 bfd_size_type max_external_reloc_size
;
11182 bfd_size_type max_internal_reloc_count
;
11183 bfd_size_type max_sym_count
;
11184 bfd_size_type max_sym_shndx_count
;
11185 Elf_Internal_Sym elfsym
;
11187 Elf_Internal_Shdr
*symtab_hdr
;
11188 Elf_Internal_Shdr
*symtab_shndx_hdr
;
11189 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11190 struct elf_outext_info eoinfo
;
11191 bfd_boolean merged
;
11192 size_t relativecount
= 0;
11193 asection
*reldyn
= 0;
11195 asection
*attr_section
= NULL
;
11196 bfd_vma attr_size
= 0;
11197 const char *std_attrs_section
;
11199 if (! is_elf_hash_table (info
->hash
))
11202 if (bfd_link_pic (info
))
11203 abfd
->flags
|= DYNAMIC
;
11205 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
11206 dynobj
= elf_hash_table (info
)->dynobj
;
11208 emit_relocs
= (bfd_link_relocatable (info
)
11209 || info
->emitrelocations
);
11211 flinfo
.info
= info
;
11212 flinfo
.output_bfd
= abfd
;
11213 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
11214 if (flinfo
.symstrtab
== NULL
)
11219 flinfo
.hash_sec
= NULL
;
11220 flinfo
.symver_sec
= NULL
;
11224 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
11225 /* Note that dynsym_sec can be NULL (on VMS). */
11226 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
11227 /* Note that it is OK if symver_sec is NULL. */
11230 flinfo
.contents
= NULL
;
11231 flinfo
.external_relocs
= NULL
;
11232 flinfo
.internal_relocs
= NULL
;
11233 flinfo
.external_syms
= NULL
;
11234 flinfo
.locsym_shndx
= NULL
;
11235 flinfo
.internal_syms
= NULL
;
11236 flinfo
.indices
= NULL
;
11237 flinfo
.sections
= NULL
;
11238 flinfo
.symshndxbuf
= NULL
;
11239 flinfo
.filesym_count
= 0;
11241 /* The object attributes have been merged. Remove the input
11242 sections from the link, and set the contents of the output
11244 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
11245 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11247 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
11248 || strcmp (o
->name
, ".gnu.attributes") == 0)
11250 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11252 asection
*input_section
;
11254 if (p
->type
!= bfd_indirect_link_order
)
11256 input_section
= p
->u
.indirect
.section
;
11257 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11258 elf_link_input_bfd ignores this section. */
11259 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
11262 attr_size
= bfd_elf_obj_attr_size (abfd
);
11265 bfd_set_section_size (abfd
, o
, attr_size
);
11267 /* Skip this section later on. */
11268 o
->map_head
.link_order
= NULL
;
11271 o
->flags
|= SEC_EXCLUDE
;
11275 /* Count up the number of relocations we will output for each output
11276 section, so that we know the sizes of the reloc sections. We
11277 also figure out some maximum sizes. */
11278 max_contents_size
= 0;
11279 max_external_reloc_size
= 0;
11280 max_internal_reloc_count
= 0;
11282 max_sym_shndx_count
= 0;
11284 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11286 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11287 o
->reloc_count
= 0;
11289 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11291 unsigned int reloc_count
= 0;
11292 unsigned int additional_reloc_count
= 0;
11293 struct bfd_elf_section_data
*esdi
= NULL
;
11295 if (p
->type
== bfd_section_reloc_link_order
11296 || p
->type
== bfd_symbol_reloc_link_order
)
11298 else if (p
->type
== bfd_indirect_link_order
)
11302 sec
= p
->u
.indirect
.section
;
11303 esdi
= elf_section_data (sec
);
11305 /* Mark all sections which are to be included in the
11306 link. This will normally be every section. We need
11307 to do this so that we can identify any sections which
11308 the linker has decided to not include. */
11309 sec
->linker_mark
= TRUE
;
11311 if (sec
->flags
& SEC_MERGE
)
11314 if (esdo
->this_hdr
.sh_type
== SHT_REL
11315 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
11316 /* Some backends use reloc_count in relocation sections
11317 to count particular types of relocs. Of course,
11318 reloc sections themselves can't have relocations. */
11320 else if (emit_relocs
)
11322 reloc_count
= sec
->reloc_count
;
11323 if (bed
->elf_backend_count_additional_relocs
)
11326 c
= (*bed
->elf_backend_count_additional_relocs
) (sec
);
11327 additional_reloc_count
+= c
;
11330 else if (bed
->elf_backend_count_relocs
)
11331 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
11333 if (sec
->rawsize
> max_contents_size
)
11334 max_contents_size
= sec
->rawsize
;
11335 if (sec
->size
> max_contents_size
)
11336 max_contents_size
= sec
->size
;
11338 /* We are interested in just local symbols, not all
11340 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
11341 && (sec
->owner
->flags
& DYNAMIC
) == 0)
11345 if (elf_bad_symtab (sec
->owner
))
11346 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
11347 / bed
->s
->sizeof_sym
);
11349 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
11351 if (sym_count
> max_sym_count
)
11352 max_sym_count
= sym_count
;
11354 if (sym_count
> max_sym_shndx_count
11355 && elf_symtab_shndx_list (sec
->owner
) != NULL
)
11356 max_sym_shndx_count
= sym_count
;
11358 if ((sec
->flags
& SEC_RELOC
) != 0)
11360 size_t ext_size
= 0;
11362 if (esdi
->rel
.hdr
!= NULL
)
11363 ext_size
= esdi
->rel
.hdr
->sh_size
;
11364 if (esdi
->rela
.hdr
!= NULL
)
11365 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11367 if (ext_size
> max_external_reloc_size
)
11368 max_external_reloc_size
= ext_size
;
11369 if (sec
->reloc_count
> max_internal_reloc_count
)
11370 max_internal_reloc_count
= sec
->reloc_count
;
11375 if (reloc_count
== 0)
11378 reloc_count
+= additional_reloc_count
;
11379 o
->reloc_count
+= reloc_count
;
11381 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11385 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11386 esdo
->rel
.count
+= additional_reloc_count
;
11388 if (esdi
->rela
.hdr
)
11390 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11391 esdo
->rela
.count
+= additional_reloc_count
;
11397 esdo
->rela
.count
+= reloc_count
;
11399 esdo
->rel
.count
+= reloc_count
;
11403 if (o
->reloc_count
> 0)
11404 o
->flags
|= SEC_RELOC
;
11407 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11408 set it (this is probably a bug) and if it is set
11409 assign_section_numbers will create a reloc section. */
11410 o
->flags
&=~ SEC_RELOC
;
11413 /* If the SEC_ALLOC flag is not set, force the section VMA to
11414 zero. This is done in elf_fake_sections as well, but forcing
11415 the VMA to 0 here will ensure that relocs against these
11416 sections are handled correctly. */
11417 if ((o
->flags
& SEC_ALLOC
) == 0
11418 && ! o
->user_set_vma
)
11422 if (! bfd_link_relocatable (info
) && merged
)
11423 elf_link_hash_traverse (elf_hash_table (info
),
11424 _bfd_elf_link_sec_merge_syms
, abfd
);
11426 /* Figure out the file positions for everything but the symbol table
11427 and the relocs. We set symcount to force assign_section_numbers
11428 to create a symbol table. */
11429 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11430 BFD_ASSERT (! abfd
->output_has_begun
);
11431 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11434 /* Set sizes, and assign file positions for reloc sections. */
11435 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11437 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11438 if ((o
->flags
& SEC_RELOC
) != 0)
11441 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11445 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11449 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11450 to count upwards while actually outputting the relocations. */
11451 esdo
->rel
.count
= 0;
11452 esdo
->rela
.count
= 0;
11454 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11456 /* Cache the section contents so that they can be compressed
11457 later. Use bfd_malloc since it will be freed by
11458 bfd_compress_section_contents. */
11459 unsigned char *contents
= esdo
->this_hdr
.contents
;
11460 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11463 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11464 if (contents
== NULL
)
11466 esdo
->this_hdr
.contents
= contents
;
11470 /* We have now assigned file positions for all the sections except
11471 .symtab, .strtab, and non-loaded reloc sections. We start the
11472 .symtab section at the current file position, and write directly
11473 to it. We build the .strtab section in memory. */
11474 bfd_get_symcount (abfd
) = 0;
11475 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11476 /* sh_name is set in prep_headers. */
11477 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11478 /* sh_flags, sh_addr and sh_size all start off zero. */
11479 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11480 /* sh_link is set in assign_section_numbers. */
11481 /* sh_info is set below. */
11482 /* sh_offset is set just below. */
11483 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11485 if (max_sym_count
< 20)
11486 max_sym_count
= 20;
11487 elf_hash_table (info
)->strtabsize
= max_sym_count
;
11488 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11489 elf_hash_table (info
)->strtab
11490 = (struct elf_sym_strtab
*) bfd_malloc (amt
);
11491 if (elf_hash_table (info
)->strtab
== NULL
)
11493 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11495 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11496 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11498 if (info
->strip
!= strip_all
|| emit_relocs
)
11500 file_ptr off
= elf_next_file_pos (abfd
);
11502 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11504 /* Note that at this point elf_next_file_pos (abfd) is
11505 incorrect. We do not yet know the size of the .symtab section.
11506 We correct next_file_pos below, after we do know the size. */
11508 /* Start writing out the symbol table. The first symbol is always a
11510 elfsym
.st_value
= 0;
11511 elfsym
.st_size
= 0;
11512 elfsym
.st_info
= 0;
11513 elfsym
.st_other
= 0;
11514 elfsym
.st_shndx
= SHN_UNDEF
;
11515 elfsym
.st_target_internal
= 0;
11516 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11517 bfd_und_section_ptr
, NULL
) != 1)
11520 /* Output a symbol for each section. We output these even if we are
11521 discarding local symbols, since they are used for relocs. These
11522 symbols have no names. We store the index of each one in the
11523 index field of the section, so that we can find it again when
11524 outputting relocs. */
11526 elfsym
.st_size
= 0;
11527 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11528 elfsym
.st_other
= 0;
11529 elfsym
.st_value
= 0;
11530 elfsym
.st_target_internal
= 0;
11531 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11533 o
= bfd_section_from_elf_index (abfd
, i
);
11536 o
->target_index
= bfd_get_symcount (abfd
);
11537 elfsym
.st_shndx
= i
;
11538 if (!bfd_link_relocatable (info
))
11539 elfsym
.st_value
= o
->vma
;
11540 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11547 /* Allocate some memory to hold information read in from the input
11549 if (max_contents_size
!= 0)
11551 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11552 if (flinfo
.contents
== NULL
)
11556 if (max_external_reloc_size
!= 0)
11558 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11559 if (flinfo
.external_relocs
== NULL
)
11563 if (max_internal_reloc_count
!= 0)
11565 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11566 amt
*= sizeof (Elf_Internal_Rela
);
11567 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11568 if (flinfo
.internal_relocs
== NULL
)
11572 if (max_sym_count
!= 0)
11574 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11575 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11576 if (flinfo
.external_syms
== NULL
)
11579 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11580 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11581 if (flinfo
.internal_syms
== NULL
)
11584 amt
= max_sym_count
* sizeof (long);
11585 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11586 if (flinfo
.indices
== NULL
)
11589 amt
= max_sym_count
* sizeof (asection
*);
11590 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11591 if (flinfo
.sections
== NULL
)
11595 if (max_sym_shndx_count
!= 0)
11597 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11598 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11599 if (flinfo
.locsym_shndx
== NULL
)
11603 if (elf_hash_table (info
)->tls_sec
)
11605 bfd_vma base
, end
= 0;
11608 for (sec
= elf_hash_table (info
)->tls_sec
;
11609 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11612 bfd_size_type size
= sec
->size
;
11615 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11617 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11620 size
= ord
->offset
+ ord
->size
;
11622 end
= sec
->vma
+ size
;
11624 base
= elf_hash_table (info
)->tls_sec
->vma
;
11625 /* Only align end of TLS section if static TLS doesn't have special
11626 alignment requirements. */
11627 if (bed
->static_tls_alignment
== 1)
11628 end
= align_power (end
,
11629 elf_hash_table (info
)->tls_sec
->alignment_power
);
11630 elf_hash_table (info
)->tls_size
= end
- base
;
11633 /* Reorder SHF_LINK_ORDER sections. */
11634 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11636 if (!elf_fixup_link_order (abfd
, o
))
11640 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11643 /* Since ELF permits relocations to be against local symbols, we
11644 must have the local symbols available when we do the relocations.
11645 Since we would rather only read the local symbols once, and we
11646 would rather not keep them in memory, we handle all the
11647 relocations for a single input file at the same time.
11649 Unfortunately, there is no way to know the total number of local
11650 symbols until we have seen all of them, and the local symbol
11651 indices precede the global symbol indices. This means that when
11652 we are generating relocatable output, and we see a reloc against
11653 a global symbol, we can not know the symbol index until we have
11654 finished examining all the local symbols to see which ones we are
11655 going to output. To deal with this, we keep the relocations in
11656 memory, and don't output them until the end of the link. This is
11657 an unfortunate waste of memory, but I don't see a good way around
11658 it. Fortunately, it only happens when performing a relocatable
11659 link, which is not the common case. FIXME: If keep_memory is set
11660 we could write the relocs out and then read them again; I don't
11661 know how bad the memory loss will be. */
11663 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11664 sub
->output_has_begun
= FALSE
;
11665 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11667 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11669 if (p
->type
== bfd_indirect_link_order
11670 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11671 == bfd_target_elf_flavour
)
11672 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11674 if (! sub
->output_has_begun
)
11676 if (! elf_link_input_bfd (&flinfo
, sub
))
11678 sub
->output_has_begun
= TRUE
;
11681 else if (p
->type
== bfd_section_reloc_link_order
11682 || p
->type
== bfd_symbol_reloc_link_order
)
11684 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11689 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11691 if (p
->type
== bfd_indirect_link_order
11692 && (bfd_get_flavour (sub
)
11693 == bfd_target_elf_flavour
)
11694 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11695 != bed
->s
->elfclass
))
11697 const char *iclass
, *oclass
;
11699 switch (bed
->s
->elfclass
)
11701 case ELFCLASS64
: oclass
= "ELFCLASS64"; break;
11702 case ELFCLASS32
: oclass
= "ELFCLASS32"; break;
11703 case ELFCLASSNONE
: oclass
= "ELFCLASSNONE"; break;
11707 switch (elf_elfheader (sub
)->e_ident
[EI_CLASS
])
11709 case ELFCLASS64
: iclass
= "ELFCLASS64"; break;
11710 case ELFCLASS32
: iclass
= "ELFCLASS32"; break;
11711 case ELFCLASSNONE
: iclass
= "ELFCLASSNONE"; break;
11715 bfd_set_error (bfd_error_wrong_format
);
11716 (*_bfd_error_handler
)
11717 (_("%B: file class %s incompatible with %s"),
11718 sub
, iclass
, oclass
);
11727 /* Free symbol buffer if needed. */
11728 if (!info
->reduce_memory_overheads
)
11730 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11731 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11732 && elf_tdata (sub
)->symbuf
)
11734 free (elf_tdata (sub
)->symbuf
);
11735 elf_tdata (sub
)->symbuf
= NULL
;
11739 /* Output any global symbols that got converted to local in a
11740 version script or due to symbol visibility. We do this in a
11741 separate step since ELF requires all local symbols to appear
11742 prior to any global symbols. FIXME: We should only do this if
11743 some global symbols were, in fact, converted to become local.
11744 FIXME: Will this work correctly with the Irix 5 linker? */
11745 eoinfo
.failed
= FALSE
;
11746 eoinfo
.flinfo
= &flinfo
;
11747 eoinfo
.localsyms
= TRUE
;
11748 eoinfo
.file_sym_done
= FALSE
;
11749 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11753 /* If backend needs to output some local symbols not present in the hash
11754 table, do it now. */
11755 if (bed
->elf_backend_output_arch_local_syms
11756 && (info
->strip
!= strip_all
|| emit_relocs
))
11758 typedef int (*out_sym_func
)
11759 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11760 struct elf_link_hash_entry
*);
11762 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11763 (abfd
, info
, &flinfo
,
11764 (out_sym_func
) elf_link_output_symstrtab
)))
11768 /* That wrote out all the local symbols. Finish up the symbol table
11769 with the global symbols. Even if we want to strip everything we
11770 can, we still need to deal with those global symbols that got
11771 converted to local in a version script. */
11773 /* The sh_info field records the index of the first non local symbol. */
11774 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11777 && elf_hash_table (info
)->dynsym
!= NULL
11778 && (elf_hash_table (info
)->dynsym
->output_section
11779 != bfd_abs_section_ptr
))
11781 Elf_Internal_Sym sym
;
11782 bfd_byte
*dynsym
= elf_hash_table (info
)->dynsym
->contents
;
11783 long last_local
= 0;
11785 /* Write out the section symbols for the output sections. */
11786 if (bfd_link_pic (info
)
11787 || elf_hash_table (info
)->is_relocatable_executable
)
11793 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11795 sym
.st_target_internal
= 0;
11797 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11803 dynindx
= elf_section_data (s
)->dynindx
;
11806 indx
= elf_section_data (s
)->this_idx
;
11807 BFD_ASSERT (indx
> 0);
11808 sym
.st_shndx
= indx
;
11809 if (! check_dynsym (abfd
, &sym
))
11811 sym
.st_value
= s
->vma
;
11812 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11813 if (last_local
< dynindx
)
11814 last_local
= dynindx
;
11815 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11819 /* Write out the local dynsyms. */
11820 if (elf_hash_table (info
)->dynlocal
)
11822 struct elf_link_local_dynamic_entry
*e
;
11823 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
11828 /* Copy the internal symbol and turn off visibility.
11829 Note that we saved a word of storage and overwrote
11830 the original st_name with the dynstr_index. */
11832 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11834 s
= bfd_section_from_elf_index (e
->input_bfd
,
11839 elf_section_data (s
->output_section
)->this_idx
;
11840 if (! check_dynsym (abfd
, &sym
))
11842 sym
.st_value
= (s
->output_section
->vma
11844 + e
->isym
.st_value
);
11847 if (last_local
< e
->dynindx
)
11848 last_local
= e
->dynindx
;
11850 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11851 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11855 elf_section_data (elf_hash_table (info
)->dynsym
->output_section
)->this_hdr
.sh_info
=
11859 /* We get the global symbols from the hash table. */
11860 eoinfo
.failed
= FALSE
;
11861 eoinfo
.localsyms
= FALSE
;
11862 eoinfo
.flinfo
= &flinfo
;
11863 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11867 /* If backend needs to output some symbols not present in the hash
11868 table, do it now. */
11869 if (bed
->elf_backend_output_arch_syms
11870 && (info
->strip
!= strip_all
|| emit_relocs
))
11872 typedef int (*out_sym_func
)
11873 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11874 struct elf_link_hash_entry
*);
11876 if (! ((*bed
->elf_backend_output_arch_syms
)
11877 (abfd
, info
, &flinfo
,
11878 (out_sym_func
) elf_link_output_symstrtab
)))
11882 /* Finalize the .strtab section. */
11883 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11885 /* Swap out the .strtab section. */
11886 if (!elf_link_swap_symbols_out (&flinfo
))
11889 /* Now we know the size of the symtab section. */
11890 if (bfd_get_symcount (abfd
) > 0)
11892 /* Finish up and write out the symbol string table (.strtab)
11894 Elf_Internal_Shdr
*symstrtab_hdr
;
11895 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11897 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
11898 if (symtab_shndx_hdr
!= NULL
&& symtab_shndx_hdr
->sh_name
!= 0)
11900 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11901 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11902 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11903 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11904 symtab_shndx_hdr
->sh_size
= amt
;
11906 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11909 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11910 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11914 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11915 /* sh_name was set in prep_headers. */
11916 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11917 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
11918 symstrtab_hdr
->sh_addr
= 0;
11919 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11920 symstrtab_hdr
->sh_entsize
= 0;
11921 symstrtab_hdr
->sh_link
= 0;
11922 symstrtab_hdr
->sh_info
= 0;
11923 /* sh_offset is set just below. */
11924 symstrtab_hdr
->sh_addralign
= 1;
11926 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11928 elf_next_file_pos (abfd
) = off
;
11930 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11931 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11935 if (info
->out_implib_bfd
&& !elf_output_implib (abfd
, info
))
11937 (*_bfd_error_handler
) (_("%B: failed to generate import library"),
11938 info
->out_implib_bfd
);
11942 /* Adjust the relocs to have the correct symbol indices. */
11943 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11945 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11947 if ((o
->flags
& SEC_RELOC
) == 0)
11950 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11951 if (esdo
->rel
.hdr
!= NULL
11952 && !elf_link_adjust_relocs (abfd
, &esdo
->rel
, sort
))
11954 if (esdo
->rela
.hdr
!= NULL
11955 && !elf_link_adjust_relocs (abfd
, &esdo
->rela
, sort
))
11958 /* Set the reloc_count field to 0 to prevent write_relocs from
11959 trying to swap the relocs out itself. */
11960 o
->reloc_count
= 0;
11963 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
11964 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
11966 /* If we are linking against a dynamic object, or generating a
11967 shared library, finish up the dynamic linking information. */
11970 bfd_byte
*dyncon
, *dynconend
;
11972 /* Fix up .dynamic entries. */
11973 o
= bfd_get_linker_section (dynobj
, ".dynamic");
11974 BFD_ASSERT (o
!= NULL
);
11976 dyncon
= o
->contents
;
11977 dynconend
= o
->contents
+ o
->size
;
11978 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
11980 Elf_Internal_Dyn dyn
;
11984 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
11991 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
11993 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
11995 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
11996 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
11999 dyn
.d_un
.d_val
= relativecount
;
12006 name
= info
->init_function
;
12009 name
= info
->fini_function
;
12012 struct elf_link_hash_entry
*h
;
12014 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
12015 FALSE
, FALSE
, TRUE
);
12017 && (h
->root
.type
== bfd_link_hash_defined
12018 || h
->root
.type
== bfd_link_hash_defweak
))
12020 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
12021 o
= h
->root
.u
.def
.section
;
12022 if (o
->output_section
!= NULL
)
12023 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
12024 + o
->output_offset
);
12027 /* The symbol is imported from another shared
12028 library and does not apply to this one. */
12029 dyn
.d_un
.d_ptr
= 0;
12036 case DT_PREINIT_ARRAYSZ
:
12037 name
= ".preinit_array";
12039 case DT_INIT_ARRAYSZ
:
12040 name
= ".init_array";
12042 case DT_FINI_ARRAYSZ
:
12043 name
= ".fini_array";
12045 o
= bfd_get_section_by_name (abfd
, name
);
12048 (*_bfd_error_handler
)
12049 (_("could not find section %s"), name
);
12053 (*_bfd_error_handler
)
12054 (_("warning: %s section has zero size"), name
);
12055 dyn
.d_un
.d_val
= o
->size
;
12058 case DT_PREINIT_ARRAY
:
12059 name
= ".preinit_array";
12061 case DT_INIT_ARRAY
:
12062 name
= ".init_array";
12064 case DT_FINI_ARRAY
:
12065 name
= ".fini_array";
12067 o
= bfd_get_section_by_name (abfd
, name
);
12074 name
= ".gnu.hash";
12083 name
= ".gnu.version_d";
12086 name
= ".gnu.version_r";
12089 name
= ".gnu.version";
12091 o
= bfd_get_linker_section (dynobj
, name
);
12095 (*_bfd_error_handler
)
12096 (_("could not find section %s"), name
);
12099 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
12101 (*_bfd_error_handler
)
12102 (_("warning: section '%s' is being made into a note"), name
);
12103 bfd_set_error (bfd_error_nonrepresentable_section
);
12106 dyn
.d_un
.d_ptr
= o
->output_section
->vma
+ o
->output_offset
;
12113 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
12117 dyn
.d_un
.d_val
= 0;
12118 dyn
.d_un
.d_ptr
= 0;
12119 for (i
= 1; i
< elf_numsections (abfd
); i
++)
12121 Elf_Internal_Shdr
*hdr
;
12123 hdr
= elf_elfsections (abfd
)[i
];
12124 if (hdr
->sh_type
== type
12125 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
12127 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
12128 dyn
.d_un
.d_val
+= hdr
->sh_size
;
12131 if (dyn
.d_un
.d_ptr
== 0
12132 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
12133 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
12139 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
12143 /* If we have created any dynamic sections, then output them. */
12144 if (dynobj
!= NULL
)
12146 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
12149 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12150 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
12151 || info
->error_textrel
)
12152 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
12154 bfd_byte
*dyncon
, *dynconend
;
12156 dyncon
= o
->contents
;
12157 dynconend
= o
->contents
+ o
->size
;
12158 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
12160 Elf_Internal_Dyn dyn
;
12162 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
12164 if (dyn
.d_tag
== DT_TEXTREL
)
12166 if (info
->error_textrel
)
12167 info
->callbacks
->einfo
12168 (_("%P%X: read-only segment has dynamic relocations.\n"));
12170 info
->callbacks
->einfo
12171 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12177 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
12179 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
12181 || o
->output_section
== bfd_abs_section_ptr
)
12183 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
12185 /* At this point, we are only interested in sections
12186 created by _bfd_elf_link_create_dynamic_sections. */
12189 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
12191 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
12193 if (strcmp (o
->name
, ".dynstr") != 0)
12195 if (! bfd_set_section_contents (abfd
, o
->output_section
,
12197 (file_ptr
) o
->output_offset
12198 * bfd_octets_per_byte (abfd
),
12204 /* The contents of the .dynstr section are actually in a
12208 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
12209 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
12210 || ! _bfd_elf_strtab_emit (abfd
,
12211 elf_hash_table (info
)->dynstr
))
12217 if (bfd_link_relocatable (info
))
12219 bfd_boolean failed
= FALSE
;
12221 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
12226 /* If we have optimized stabs strings, output them. */
12227 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
12229 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
12233 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
12236 elf_final_link_free (abfd
, &flinfo
);
12238 elf_linker (abfd
) = TRUE
;
12242 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
12243 if (contents
== NULL
)
12244 return FALSE
; /* Bail out and fail. */
12245 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
12246 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
12253 elf_final_link_free (abfd
, &flinfo
);
12257 /* Initialize COOKIE for input bfd ABFD. */
12260 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
12261 struct bfd_link_info
*info
, bfd
*abfd
)
12263 Elf_Internal_Shdr
*symtab_hdr
;
12264 const struct elf_backend_data
*bed
;
12266 bed
= get_elf_backend_data (abfd
);
12267 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12269 cookie
->abfd
= abfd
;
12270 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
12271 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
12272 if (cookie
->bad_symtab
)
12274 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
12275 cookie
->extsymoff
= 0;
12279 cookie
->locsymcount
= symtab_hdr
->sh_info
;
12280 cookie
->extsymoff
= symtab_hdr
->sh_info
;
12283 if (bed
->s
->arch_size
== 32)
12284 cookie
->r_sym_shift
= 8;
12286 cookie
->r_sym_shift
= 32;
12288 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
12289 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
12291 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
12292 cookie
->locsymcount
, 0,
12294 if (cookie
->locsyms
== NULL
)
12296 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
12299 if (info
->keep_memory
)
12300 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
12305 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12308 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
12310 Elf_Internal_Shdr
*symtab_hdr
;
12312 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
12313 if (cookie
->locsyms
!= NULL
12314 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
12315 free (cookie
->locsyms
);
12318 /* Initialize the relocation information in COOKIE for input section SEC
12319 of input bfd ABFD. */
12322 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12323 struct bfd_link_info
*info
, bfd
*abfd
,
12326 const struct elf_backend_data
*bed
;
12328 if (sec
->reloc_count
== 0)
12330 cookie
->rels
= NULL
;
12331 cookie
->relend
= NULL
;
12335 bed
= get_elf_backend_data (abfd
);
12337 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
12338 info
->keep_memory
);
12339 if (cookie
->rels
== NULL
)
12341 cookie
->rel
= cookie
->rels
;
12342 cookie
->relend
= (cookie
->rels
12343 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
12345 cookie
->rel
= cookie
->rels
;
12349 /* Free the memory allocated by init_reloc_cookie_rels,
12353 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
12356 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
12357 free (cookie
->rels
);
12360 /* Initialize the whole of COOKIE for input section SEC. */
12363 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12364 struct bfd_link_info
*info
,
12367 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
12369 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
12374 fini_reloc_cookie (cookie
, sec
->owner
);
12379 /* Free the memory allocated by init_reloc_cookie_for_section,
12383 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12386 fini_reloc_cookie_rels (cookie
, sec
);
12387 fini_reloc_cookie (cookie
, sec
->owner
);
12390 /* Garbage collect unused sections. */
12392 /* Default gc_mark_hook. */
12395 _bfd_elf_gc_mark_hook (asection
*sec
,
12396 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12397 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12398 struct elf_link_hash_entry
*h
,
12399 Elf_Internal_Sym
*sym
)
12403 switch (h
->root
.type
)
12405 case bfd_link_hash_defined
:
12406 case bfd_link_hash_defweak
:
12407 return h
->root
.u
.def
.section
;
12409 case bfd_link_hash_common
:
12410 return h
->root
.u
.c
.p
->section
;
12417 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12422 /* For undefined __start_<name> and __stop_<name> symbols, return the
12423 first input section matching <name>. Return NULL otherwise. */
12426 _bfd_elf_is_start_stop (const struct bfd_link_info
*info
,
12427 struct elf_link_hash_entry
*h
)
12430 const char *sec_name
;
12432 if (h
->root
.type
!= bfd_link_hash_undefined
12433 && h
->root
.type
!= bfd_link_hash_undefweak
)
12436 s
= h
->root
.u
.undef
.section
;
12439 if (s
== (asection
*) 0 - 1)
12445 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12446 sec_name
= h
->root
.root
.string
+ 8;
12447 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12448 sec_name
= h
->root
.root
.string
+ 7;
12450 if (sec_name
!= NULL
&& *sec_name
!= '\0')
12454 for (i
= info
->input_bfds
; i
!= NULL
; i
= i
->link
.next
)
12456 s
= bfd_get_section_by_name (i
, sec_name
);
12459 h
->root
.u
.undef
.section
= s
;
12466 h
->root
.u
.undef
.section
= (asection
*) 0 - 1;
12471 /* COOKIE->rel describes a relocation against section SEC, which is
12472 a section we've decided to keep. Return the section that contains
12473 the relocation symbol, or NULL if no section contains it. */
12476 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12477 elf_gc_mark_hook_fn gc_mark_hook
,
12478 struct elf_reloc_cookie
*cookie
,
12479 bfd_boolean
*start_stop
)
12481 unsigned long r_symndx
;
12482 struct elf_link_hash_entry
*h
;
12484 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12485 if (r_symndx
== STN_UNDEF
)
12488 if (r_symndx
>= cookie
->locsymcount
12489 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12491 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12494 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12498 while (h
->root
.type
== bfd_link_hash_indirect
12499 || h
->root
.type
== bfd_link_hash_warning
)
12500 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12502 /* If this symbol is weak and there is a non-weak definition, we
12503 keep the non-weak definition because many backends put
12504 dynamic reloc info on the non-weak definition for code
12505 handling copy relocs. */
12506 if (h
->u
.weakdef
!= NULL
)
12507 h
->u
.weakdef
->mark
= 1;
12509 if (start_stop
!= NULL
)
12511 /* To work around a glibc bug, mark all XXX input sections
12512 when there is an as yet undefined reference to __start_XXX
12513 or __stop_XXX symbols. The linker will later define such
12514 symbols for orphan input sections that have a name
12515 representable as a C identifier. */
12516 asection
*s
= _bfd_elf_is_start_stop (info
, h
);
12520 *start_stop
= !s
->gc_mark
;
12525 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12528 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12529 &cookie
->locsyms
[r_symndx
]);
12532 /* COOKIE->rel describes a relocation against section SEC, which is
12533 a section we've decided to keep. Mark the section that contains
12534 the relocation symbol. */
12537 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12539 elf_gc_mark_hook_fn gc_mark_hook
,
12540 struct elf_reloc_cookie
*cookie
)
12543 bfd_boolean start_stop
= FALSE
;
12545 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
, &start_stop
);
12546 while (rsec
!= NULL
)
12548 if (!rsec
->gc_mark
)
12550 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12551 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12553 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12558 rsec
= bfd_get_next_section_by_name (rsec
->owner
, rsec
);
12563 /* The mark phase of garbage collection. For a given section, mark
12564 it and any sections in this section's group, and all the sections
12565 which define symbols to which it refers. */
12568 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12570 elf_gc_mark_hook_fn gc_mark_hook
)
12573 asection
*group_sec
, *eh_frame
;
12577 /* Mark all the sections in the group. */
12578 group_sec
= elf_section_data (sec
)->next_in_group
;
12579 if (group_sec
&& !group_sec
->gc_mark
)
12580 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12583 /* Look through the section relocs. */
12585 eh_frame
= elf_eh_frame_section (sec
->owner
);
12586 if ((sec
->flags
& SEC_RELOC
) != 0
12587 && sec
->reloc_count
> 0
12588 && sec
!= eh_frame
)
12590 struct elf_reloc_cookie cookie
;
12592 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12596 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12597 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12602 fini_reloc_cookie_for_section (&cookie
, sec
);
12606 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12608 struct elf_reloc_cookie cookie
;
12610 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12614 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12615 gc_mark_hook
, &cookie
))
12617 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12621 eh_frame
= elf_section_eh_frame_entry (sec
);
12622 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12623 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12629 /* Scan and mark sections in a special or debug section group. */
12632 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12634 /* Point to first section of section group. */
12636 /* Used to iterate the section group. */
12639 bfd_boolean is_special_grp
= TRUE
;
12640 bfd_boolean is_debug_grp
= TRUE
;
12642 /* First scan to see if group contains any section other than debug
12643 and special section. */
12644 ssec
= msec
= elf_next_in_group (grp
);
12647 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12648 is_debug_grp
= FALSE
;
12650 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12651 is_special_grp
= FALSE
;
12653 msec
= elf_next_in_group (msec
);
12655 while (msec
!= ssec
);
12657 /* If this is a pure debug section group or pure special section group,
12658 keep all sections in this group. */
12659 if (is_debug_grp
|| is_special_grp
)
12664 msec
= elf_next_in_group (msec
);
12666 while (msec
!= ssec
);
12670 /* Keep debug and special sections. */
12673 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12674 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12678 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12681 bfd_boolean some_kept
;
12682 bfd_boolean debug_frag_seen
;
12684 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12687 /* Ensure all linker created sections are kept,
12688 see if any other section is already marked,
12689 and note if we have any fragmented debug sections. */
12690 debug_frag_seen
= some_kept
= FALSE
;
12691 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12693 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12695 else if (isec
->gc_mark
)
12698 if (debug_frag_seen
== FALSE
12699 && (isec
->flags
& SEC_DEBUGGING
)
12700 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12701 debug_frag_seen
= TRUE
;
12704 /* If no section in this file will be kept, then we can
12705 toss out the debug and special sections. */
12709 /* Keep debug and special sections like .comment when they are
12710 not part of a group. Also keep section groups that contain
12711 just debug sections or special sections. */
12712 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12714 if ((isec
->flags
& SEC_GROUP
) != 0)
12715 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12716 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12717 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12718 && elf_next_in_group (isec
) == NULL
)
12722 if (! debug_frag_seen
)
12725 /* Look for CODE sections which are going to be discarded,
12726 and find and discard any fragmented debug sections which
12727 are associated with that code section. */
12728 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12729 if ((isec
->flags
& SEC_CODE
) != 0
12730 && isec
->gc_mark
== 0)
12735 ilen
= strlen (isec
->name
);
12737 /* Association is determined by the name of the debug section
12738 containing the name of the code section as a suffix. For
12739 example .debug_line.text.foo is a debug section associated
12741 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12745 if (dsec
->gc_mark
== 0
12746 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12749 dlen
= strlen (dsec
->name
);
12752 && strncmp (dsec
->name
+ (dlen
- ilen
),
12753 isec
->name
, ilen
) == 0)
12763 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12765 struct elf_gc_sweep_symbol_info
12767 struct bfd_link_info
*info
;
12768 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12773 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12776 && (((h
->root
.type
== bfd_link_hash_defined
12777 || h
->root
.type
== bfd_link_hash_defweak
)
12778 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12779 && h
->root
.u
.def
.section
->gc_mark
))
12780 || h
->root
.type
== bfd_link_hash_undefined
12781 || h
->root
.type
== bfd_link_hash_undefweak
))
12783 struct elf_gc_sweep_symbol_info
*inf
;
12785 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12786 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12787 h
->def_regular
= 0;
12788 h
->ref_regular
= 0;
12789 h
->ref_regular_nonweak
= 0;
12795 /* The sweep phase of garbage collection. Remove all garbage sections. */
12797 typedef bfd_boolean (*gc_sweep_hook_fn
)
12798 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12801 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12804 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12805 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12806 unsigned long section_sym_count
;
12807 struct elf_gc_sweep_symbol_info sweep_info
;
12809 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12813 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12814 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12817 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12819 /* When any section in a section group is kept, we keep all
12820 sections in the section group. If the first member of
12821 the section group is excluded, we will also exclude the
12823 if (o
->flags
& SEC_GROUP
)
12825 asection
*first
= elf_next_in_group (o
);
12826 o
->gc_mark
= first
->gc_mark
;
12832 /* Skip sweeping sections already excluded. */
12833 if (o
->flags
& SEC_EXCLUDE
)
12836 /* Since this is early in the link process, it is simple
12837 to remove a section from the output. */
12838 o
->flags
|= SEC_EXCLUDE
;
12840 if (info
->print_gc_sections
&& o
->size
!= 0)
12841 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12843 /* But we also have to update some of the relocation
12844 info we collected before. */
12846 && (o
->flags
& SEC_RELOC
) != 0
12847 && o
->reloc_count
!= 0
12848 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12849 && (o
->flags
& SEC_DEBUGGING
) != 0)
12850 && !bfd_is_abs_section (o
->output_section
))
12852 Elf_Internal_Rela
*internal_relocs
;
12856 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12857 info
->keep_memory
);
12858 if (internal_relocs
== NULL
)
12861 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12863 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12864 free (internal_relocs
);
12872 /* Remove the symbols that were in the swept sections from the dynamic
12873 symbol table. GCFIXME: Anyone know how to get them out of the
12874 static symbol table as well? */
12875 sweep_info
.info
= info
;
12876 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12877 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12880 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12884 /* Propagate collected vtable information. This is called through
12885 elf_link_hash_traverse. */
12888 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12890 /* Those that are not vtables. */
12891 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12894 /* Those vtables that do not have parents, we cannot merge. */
12895 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12898 /* If we've already been done, exit. */
12899 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12902 /* Make sure the parent's table is up to date. */
12903 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12905 if (h
->vtable
->used
== NULL
)
12907 /* None of this table's entries were referenced. Re-use the
12909 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12910 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12915 bfd_boolean
*cu
, *pu
;
12917 /* Or the parent's entries into ours. */
12918 cu
= h
->vtable
->used
;
12920 pu
= h
->vtable
->parent
->vtable
->used
;
12923 const struct elf_backend_data
*bed
;
12924 unsigned int log_file_align
;
12926 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12927 log_file_align
= bed
->s
->log_file_align
;
12928 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12943 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12946 bfd_vma hstart
, hend
;
12947 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12948 const struct elf_backend_data
*bed
;
12949 unsigned int log_file_align
;
12951 /* Take care of both those symbols that do not describe vtables as
12952 well as those that are not loaded. */
12953 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12956 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
12957 || h
->root
.type
== bfd_link_hash_defweak
);
12959 sec
= h
->root
.u
.def
.section
;
12960 hstart
= h
->root
.u
.def
.value
;
12961 hend
= hstart
+ h
->size
;
12963 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
12965 return *(bfd_boolean
*) okp
= FALSE
;
12966 bed
= get_elf_backend_data (sec
->owner
);
12967 log_file_align
= bed
->s
->log_file_align
;
12969 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
12971 for (rel
= relstart
; rel
< relend
; ++rel
)
12972 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
12974 /* If the entry is in use, do nothing. */
12975 if (h
->vtable
->used
12976 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
12978 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
12979 if (h
->vtable
->used
[entry
])
12982 /* Otherwise, kill it. */
12983 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
12989 /* Mark sections containing dynamically referenced symbols. When
12990 building shared libraries, we must assume that any visible symbol is
12994 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
12996 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
12997 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
12999 if ((h
->root
.type
== bfd_link_hash_defined
13000 || h
->root
.type
== bfd_link_hash_defweak
)
13002 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
13003 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
13004 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
13005 && (!bfd_link_executable (info
)
13006 || info
->export_dynamic
13009 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
13010 && (h
->versioned
>= versioned
13011 || !bfd_hide_sym_by_version (info
->version_info
,
13012 h
->root
.root
.string
)))))
13013 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13018 /* Keep all sections containing symbols undefined on the command-line,
13019 and the section containing the entry symbol. */
13022 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
13024 struct bfd_sym_chain
*sym
;
13026 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
13028 struct elf_link_hash_entry
*h
;
13030 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
13031 FALSE
, FALSE
, FALSE
);
13034 && (h
->root
.type
== bfd_link_hash_defined
13035 || h
->root
.type
== bfd_link_hash_defweak
)
13036 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
13037 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
13042 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
13043 struct bfd_link_info
*info
)
13045 bfd
*ibfd
= info
->input_bfds
;
13047 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
13050 struct elf_reloc_cookie cookie
;
13052 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
13055 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
13058 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
13060 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
13061 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
13063 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
13064 fini_reloc_cookie_rels (&cookie
, sec
);
13071 /* Do mark and sweep of unused sections. */
13074 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
13076 bfd_boolean ok
= TRUE
;
13078 elf_gc_mark_hook_fn gc_mark_hook
;
13079 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13080 struct elf_link_hash_table
*htab
;
13082 if (!bed
->can_gc_sections
13083 || !is_elf_hash_table (info
->hash
))
13085 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
13089 bed
->gc_keep (info
);
13090 htab
= elf_hash_table (info
);
13092 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13093 at the .eh_frame section if we can mark the FDEs individually. */
13094 for (sub
= info
->input_bfds
;
13095 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
13096 sub
= sub
->link
.next
)
13099 struct elf_reloc_cookie cookie
;
13101 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
13102 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
13104 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
13105 if (elf_section_data (sec
)->sec_info
13106 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
13107 elf_eh_frame_section (sub
) = sec
;
13108 fini_reloc_cookie_for_section (&cookie
, sec
);
13109 sec
= bfd_get_next_section_by_name (NULL
, sec
);
13113 /* Apply transitive closure to the vtable entry usage info. */
13114 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
13118 /* Kill the vtable relocations that were not used. */
13119 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
13123 /* Mark dynamically referenced symbols. */
13124 if (htab
->dynamic_sections_created
)
13125 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
13127 /* Grovel through relocs to find out who stays ... */
13128 gc_mark_hook
= bed
->gc_mark_hook
;
13129 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
13133 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
13134 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
13137 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13138 Also treat note sections as a root, if the section is not part
13140 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
13142 && (o
->flags
& SEC_EXCLUDE
) == 0
13143 && ((o
->flags
& SEC_KEEP
) != 0
13144 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
13145 && elf_next_in_group (o
) == NULL
)))
13147 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
13152 /* Allow the backend to mark additional target specific sections. */
13153 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
13155 /* ... and mark SEC_EXCLUDE for those that go. */
13156 return elf_gc_sweep (abfd
, info
);
13159 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13162 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
13164 struct elf_link_hash_entry
*h
,
13167 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
13168 struct elf_link_hash_entry
**search
, *child
;
13169 size_t extsymcount
;
13170 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13172 /* The sh_info field of the symtab header tells us where the
13173 external symbols start. We don't care about the local symbols at
13175 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
13176 if (!elf_bad_symtab (abfd
))
13177 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
13179 sym_hashes
= elf_sym_hashes (abfd
);
13180 sym_hashes_end
= sym_hashes
+ extsymcount
;
13182 /* Hunt down the child symbol, which is in this section at the same
13183 offset as the relocation. */
13184 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
13186 if ((child
= *search
) != NULL
13187 && (child
->root
.type
== bfd_link_hash_defined
13188 || child
->root
.type
== bfd_link_hash_defweak
)
13189 && child
->root
.u
.def
.section
== sec
13190 && child
->root
.u
.def
.value
== offset
)
13194 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
13195 abfd
, sec
, (unsigned long) offset
);
13196 bfd_set_error (bfd_error_invalid_operation
);
13200 if (!child
->vtable
)
13202 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
13203 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
13204 if (!child
->vtable
)
13209 /* This *should* only be the absolute section. It could potentially
13210 be that someone has defined a non-global vtable though, which
13211 would be bad. It isn't worth paging in the local symbols to be
13212 sure though; that case should simply be handled by the assembler. */
13214 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
13217 child
->vtable
->parent
= h
;
13222 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13225 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
13226 asection
*sec ATTRIBUTE_UNUSED
,
13227 struct elf_link_hash_entry
*h
,
13230 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13231 unsigned int log_file_align
= bed
->s
->log_file_align
;
13235 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
13236 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
13241 if (addend
>= h
->vtable
->size
)
13243 size_t size
, bytes
, file_align
;
13244 bfd_boolean
*ptr
= h
->vtable
->used
;
13246 /* While the symbol is undefined, we have to be prepared to handle
13248 file_align
= 1 << log_file_align
;
13249 if (h
->root
.type
== bfd_link_hash_undefined
)
13250 size
= addend
+ file_align
;
13254 if (addend
>= size
)
13256 /* Oops! We've got a reference past the defined end of
13257 the table. This is probably a bug -- shall we warn? */
13258 size
= addend
+ file_align
;
13261 size
= (size
+ file_align
- 1) & -file_align
;
13263 /* Allocate one extra entry for use as a "done" flag for the
13264 consolidation pass. */
13265 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
13269 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
13275 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
13276 * sizeof (bfd_boolean
));
13277 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
13281 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
13286 /* And arrange for that done flag to be at index -1. */
13287 h
->vtable
->used
= ptr
+ 1;
13288 h
->vtable
->size
= size
;
13291 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
13296 /* Map an ELF section header flag to its corresponding string. */
13300 flagword flag_value
;
13301 } elf_flags_to_name_table
;
13303 static elf_flags_to_name_table elf_flags_to_names
[] =
13305 { "SHF_WRITE", SHF_WRITE
},
13306 { "SHF_ALLOC", SHF_ALLOC
},
13307 { "SHF_EXECINSTR", SHF_EXECINSTR
},
13308 { "SHF_MERGE", SHF_MERGE
},
13309 { "SHF_STRINGS", SHF_STRINGS
},
13310 { "SHF_INFO_LINK", SHF_INFO_LINK
},
13311 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
13312 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
13313 { "SHF_GROUP", SHF_GROUP
},
13314 { "SHF_TLS", SHF_TLS
},
13315 { "SHF_MASKOS", SHF_MASKOS
},
13316 { "SHF_EXCLUDE", SHF_EXCLUDE
},
13319 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13321 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
13322 struct flag_info
*flaginfo
,
13325 const bfd_vma sh_flags
= elf_section_flags (section
);
13327 if (!flaginfo
->flags_initialized
)
13329 bfd
*obfd
= info
->output_bfd
;
13330 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13331 struct flag_info_list
*tf
= flaginfo
->flag_list
;
13333 int without_hex
= 0;
13335 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
13338 flagword (*lookup
) (char *);
13340 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
13341 if (lookup
!= NULL
)
13343 flagword hexval
= (*lookup
) ((char *) tf
->name
);
13347 if (tf
->with
== with_flags
)
13348 with_hex
|= hexval
;
13349 else if (tf
->with
== without_flags
)
13350 without_hex
|= hexval
;
13355 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
13357 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
13359 if (tf
->with
== with_flags
)
13360 with_hex
|= elf_flags_to_names
[i
].flag_value
;
13361 else if (tf
->with
== without_flags
)
13362 without_hex
|= elf_flags_to_names
[i
].flag_value
;
13369 info
->callbacks
->einfo
13370 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
13374 flaginfo
->flags_initialized
= TRUE
;
13375 flaginfo
->only_with_flags
|= with_hex
;
13376 flaginfo
->not_with_flags
|= without_hex
;
13379 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
13382 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
13388 struct alloc_got_off_arg
{
13390 struct bfd_link_info
*info
;
13393 /* We need a special top-level link routine to convert got reference counts
13394 to real got offsets. */
13397 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
13399 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
13400 bfd
*obfd
= gofarg
->info
->output_bfd
;
13401 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
13403 if (h
->got
.refcount
> 0)
13405 h
->got
.offset
= gofarg
->gotoff
;
13406 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
13409 h
->got
.offset
= (bfd_vma
) -1;
13414 /* And an accompanying bit to work out final got entry offsets once
13415 we're done. Should be called from final_link. */
13418 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
13419 struct bfd_link_info
*info
)
13422 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13424 struct alloc_got_off_arg gofarg
;
13426 BFD_ASSERT (abfd
== info
->output_bfd
);
13428 if (! is_elf_hash_table (info
->hash
))
13431 /* The GOT offset is relative to the .got section, but the GOT header is
13432 put into the .got.plt section, if the backend uses it. */
13433 if (bed
->want_got_plt
)
13436 gotoff
= bed
->got_header_size
;
13438 /* Do the local .got entries first. */
13439 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13441 bfd_signed_vma
*local_got
;
13442 size_t j
, locsymcount
;
13443 Elf_Internal_Shdr
*symtab_hdr
;
13445 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13448 local_got
= elf_local_got_refcounts (i
);
13452 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13453 if (elf_bad_symtab (i
))
13454 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13456 locsymcount
= symtab_hdr
->sh_info
;
13458 for (j
= 0; j
< locsymcount
; ++j
)
13460 if (local_got
[j
] > 0)
13462 local_got
[j
] = gotoff
;
13463 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13466 local_got
[j
] = (bfd_vma
) -1;
13470 /* Then the global .got entries. .plt refcounts are handled by
13471 adjust_dynamic_symbol */
13472 gofarg
.gotoff
= gotoff
;
13473 gofarg
.info
= info
;
13474 elf_link_hash_traverse (elf_hash_table (info
),
13475 elf_gc_allocate_got_offsets
,
13480 /* Many folk need no more in the way of final link than this, once
13481 got entry reference counting is enabled. */
13484 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13486 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13489 /* Invoke the regular ELF backend linker to do all the work. */
13490 return bfd_elf_final_link (abfd
, info
);
13494 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13496 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13498 if (rcookie
->bad_symtab
)
13499 rcookie
->rel
= rcookie
->rels
;
13501 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13503 unsigned long r_symndx
;
13505 if (! rcookie
->bad_symtab
)
13506 if (rcookie
->rel
->r_offset
> offset
)
13508 if (rcookie
->rel
->r_offset
!= offset
)
13511 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13512 if (r_symndx
== STN_UNDEF
)
13515 if (r_symndx
>= rcookie
->locsymcount
13516 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13518 struct elf_link_hash_entry
*h
;
13520 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13522 while (h
->root
.type
== bfd_link_hash_indirect
13523 || h
->root
.type
== bfd_link_hash_warning
)
13524 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13526 if ((h
->root
.type
== bfd_link_hash_defined
13527 || h
->root
.type
== bfd_link_hash_defweak
)
13528 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13529 || h
->root
.u
.def
.section
->kept_section
!= NULL
13530 || discarded_section (h
->root
.u
.def
.section
)))
13535 /* It's not a relocation against a global symbol,
13536 but it could be a relocation against a local
13537 symbol for a discarded section. */
13539 Elf_Internal_Sym
*isym
;
13541 /* Need to: get the symbol; get the section. */
13542 isym
= &rcookie
->locsyms
[r_symndx
];
13543 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13545 && (isec
->kept_section
!= NULL
13546 || discarded_section (isec
)))
13554 /* Discard unneeded references to discarded sections.
13555 Returns -1 on error, 1 if any section's size was changed, 0 if
13556 nothing changed. This function assumes that the relocations are in
13557 sorted order, which is true for all known assemblers. */
13560 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13562 struct elf_reloc_cookie cookie
;
13567 if (info
->traditional_format
13568 || !is_elf_hash_table (info
->hash
))
13571 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13576 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13579 || i
->reloc_count
== 0
13580 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13584 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13587 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13590 if (_bfd_discard_section_stabs (abfd
, i
,
13591 elf_section_data (i
)->sec_info
,
13592 bfd_elf_reloc_symbol_deleted_p
,
13596 fini_reloc_cookie_for_section (&cookie
, i
);
13601 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13602 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13607 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13613 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13616 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13619 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13620 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13621 bfd_elf_reloc_symbol_deleted_p
,
13625 fini_reloc_cookie_for_section (&cookie
, i
);
13629 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13631 const struct elf_backend_data
*bed
;
13633 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13636 bed
= get_elf_backend_data (abfd
);
13638 if (bed
->elf_backend_discard_info
!= NULL
)
13640 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13643 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13646 fini_reloc_cookie (&cookie
, abfd
);
13650 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13651 _bfd_elf_end_eh_frame_parsing (info
);
13653 if (info
->eh_frame_hdr_type
13654 && !bfd_link_relocatable (info
)
13655 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13662 _bfd_elf_section_already_linked (bfd
*abfd
,
13664 struct bfd_link_info
*info
)
13667 const char *name
, *key
;
13668 struct bfd_section_already_linked
*l
;
13669 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13671 if (sec
->output_section
== bfd_abs_section_ptr
)
13674 flags
= sec
->flags
;
13676 /* Return if it isn't a linkonce section. A comdat group section
13677 also has SEC_LINK_ONCE set. */
13678 if ((flags
& SEC_LINK_ONCE
) == 0)
13681 /* Don't put group member sections on our list of already linked
13682 sections. They are handled as a group via their group section. */
13683 if (elf_sec_group (sec
) != NULL
)
13686 /* For a SHT_GROUP section, use the group signature as the key. */
13688 if ((flags
& SEC_GROUP
) != 0
13689 && elf_next_in_group (sec
) != NULL
13690 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13691 key
= elf_group_name (elf_next_in_group (sec
));
13694 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13695 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13696 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13699 /* Must be a user linkonce section that doesn't follow gcc's
13700 naming convention. In this case we won't be matching
13701 single member groups. */
13705 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13707 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13709 /* We may have 2 different types of sections on the list: group
13710 sections with a signature of <key> (<key> is some string),
13711 and linkonce sections named .gnu.linkonce.<type>.<key>.
13712 Match like sections. LTO plugin sections are an exception.
13713 They are always named .gnu.linkonce.t.<key> and match either
13714 type of section. */
13715 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13716 && ((flags
& SEC_GROUP
) != 0
13717 || strcmp (name
, l
->sec
->name
) == 0))
13718 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13720 /* The section has already been linked. See if we should
13721 issue a warning. */
13722 if (!_bfd_handle_already_linked (sec
, l
, info
))
13725 if (flags
& SEC_GROUP
)
13727 asection
*first
= elf_next_in_group (sec
);
13728 asection
*s
= first
;
13732 s
->output_section
= bfd_abs_section_ptr
;
13733 /* Record which group discards it. */
13734 s
->kept_section
= l
->sec
;
13735 s
= elf_next_in_group (s
);
13736 /* These lists are circular. */
13746 /* A single member comdat group section may be discarded by a
13747 linkonce section and vice versa. */
13748 if ((flags
& SEC_GROUP
) != 0)
13750 asection
*first
= elf_next_in_group (sec
);
13752 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13753 /* Check this single member group against linkonce sections. */
13754 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13755 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13756 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13758 first
->output_section
= bfd_abs_section_ptr
;
13759 first
->kept_section
= l
->sec
;
13760 sec
->output_section
= bfd_abs_section_ptr
;
13765 /* Check this linkonce section against single member groups. */
13766 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13767 if (l
->sec
->flags
& SEC_GROUP
)
13769 asection
*first
= elf_next_in_group (l
->sec
);
13772 && elf_next_in_group (first
) == first
13773 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13775 sec
->output_section
= bfd_abs_section_ptr
;
13776 sec
->kept_section
= first
;
13781 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13782 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13783 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13784 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13785 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13786 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13787 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13788 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13789 The reverse order cannot happen as there is never a bfd with only the
13790 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13791 matter as here were are looking only for cross-bfd sections. */
13793 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13794 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13795 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13796 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13798 if (abfd
!= l
->sec
->owner
)
13799 sec
->output_section
= bfd_abs_section_ptr
;
13803 /* This is the first section with this name. Record it. */
13804 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13805 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13806 return sec
->output_section
== bfd_abs_section_ptr
;
13810 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13812 return sym
->st_shndx
== SHN_COMMON
;
13816 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13822 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13824 return bfd_com_section_ptr
;
13828 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13829 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13830 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13831 bfd
*ibfd ATTRIBUTE_UNUSED
,
13832 unsigned long symndx ATTRIBUTE_UNUSED
)
13834 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13835 return bed
->s
->arch_size
/ 8;
13838 /* Routines to support the creation of dynamic relocs. */
13840 /* Returns the name of the dynamic reloc section associated with SEC. */
13842 static const char *
13843 get_dynamic_reloc_section_name (bfd
* abfd
,
13845 bfd_boolean is_rela
)
13848 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13849 const char *prefix
= is_rela
? ".rela" : ".rel";
13851 if (old_name
== NULL
)
13854 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13855 sprintf (name
, "%s%s", prefix
, old_name
);
13860 /* Returns the dynamic reloc section associated with SEC.
13861 If necessary compute the name of the dynamic reloc section based
13862 on SEC's name (looked up in ABFD's string table) and the setting
13866 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13868 bfd_boolean is_rela
)
13870 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13872 if (reloc_sec
== NULL
)
13874 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13878 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13880 if (reloc_sec
!= NULL
)
13881 elf_section_data (sec
)->sreloc
= reloc_sec
;
13888 /* Returns the dynamic reloc section associated with SEC. If the
13889 section does not exist it is created and attached to the DYNOBJ
13890 bfd and stored in the SRELOC field of SEC's elf_section_data
13893 ALIGNMENT is the alignment for the newly created section and
13894 IS_RELA defines whether the name should be .rela.<SEC's name>
13895 or .rel.<SEC's name>. The section name is looked up in the
13896 string table associated with ABFD. */
13899 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13901 unsigned int alignment
,
13903 bfd_boolean is_rela
)
13905 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13907 if (reloc_sec
== NULL
)
13909 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13914 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13916 if (reloc_sec
== NULL
)
13918 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13919 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13920 if ((sec
->flags
& SEC_ALLOC
) != 0)
13921 flags
|= SEC_ALLOC
| SEC_LOAD
;
13923 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13924 if (reloc_sec
!= NULL
)
13926 /* _bfd_elf_get_sec_type_attr chooses a section type by
13927 name. Override as it may be wrong, eg. for a user
13928 section named "auto" we'll get ".relauto" which is
13929 seen to be a .rela section. */
13930 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13931 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13936 elf_section_data (sec
)->sreloc
= reloc_sec
;
13942 /* Copy the ELF symbol type and other attributes for a linker script
13943 assignment from HSRC to HDEST. Generally this should be treated as
13944 if we found a strong non-dynamic definition for HDEST (except that
13945 ld ignores multiple definition errors). */
13947 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
13948 struct bfd_link_hash_entry
*hdest
,
13949 struct bfd_link_hash_entry
*hsrc
)
13951 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
13952 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
13953 Elf_Internal_Sym isym
;
13955 ehdest
->type
= ehsrc
->type
;
13956 ehdest
->target_internal
= ehsrc
->target_internal
;
13958 isym
.st_other
= ehsrc
->other
;
13959 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
13962 /* Append a RELA relocation REL to section S in BFD. */
13965 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13967 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13968 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
13969 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
13970 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
13973 /* Append a REL relocation REL to section S in BFD. */
13976 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13978 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13979 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
13980 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
13981 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);