1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2015 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"
32 /* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
35 struct elf_info_failed
37 struct bfd_link_info
*info
;
41 /* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
44 struct elf_find_verdep_info
46 /* General link information. */
47 struct bfd_link_info
*info
;
48 /* The number of dependencies. */
50 /* Whether we had a failure. */
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry
*, struct elf_info_failed
*);
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie
*cookie
,
59 unsigned long r_symndx
,
62 if (r_symndx
>= cookie
->locsymcount
63 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
65 struct elf_link_hash_entry
*h
;
67 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
69 while (h
->root
.type
== bfd_link_hash_indirect
70 || h
->root
.type
== bfd_link_hash_warning
)
71 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
73 if ((h
->root
.type
== bfd_link_hash_defined
74 || h
->root
.type
== bfd_link_hash_defweak
)
75 && discarded_section (h
->root
.u
.def
.section
))
76 return h
->root
.u
.def
.section
;
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
86 Elf_Internal_Sym
*isym
;
88 /* Need to: get the symbol; get the section. */
89 isym
= &cookie
->locsyms
[r_symndx
];
90 isec
= bfd_section_from_elf_index (cookie
->abfd
, isym
->st_shndx
);
92 && discard
? discarded_section (isec
) : 1)
98 /* Define a symbol in a dynamic linkage section. */
100 struct elf_link_hash_entry
*
101 _bfd_elf_define_linkage_sym (bfd
*abfd
,
102 struct bfd_link_info
*info
,
106 struct elf_link_hash_entry
*h
;
107 struct bfd_link_hash_entry
*bh
;
108 const struct elf_backend_data
*bed
;
110 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h
->root
.type
= bfd_link_hash_new
;
121 bed
= get_elf_backend_data (abfd
);
122 if (!_bfd_generic_link_add_one_symbol (info
, abfd
, name
, BSF_GLOBAL
,
123 sec
, 0, NULL
, FALSE
, bed
->collect
,
126 h
= (struct elf_link_hash_entry
*) bh
;
129 h
->root
.linker_def
= 1;
130 h
->type
= STT_OBJECT
;
131 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
132 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
134 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
139 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
143 struct elf_link_hash_entry
*h
;
144 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
145 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
147 /* This function may be called more than once. */
148 s
= bfd_get_linker_section (abfd
, ".got");
152 flags
= bed
->dynamic_sec_flags
;
154 s
= bfd_make_section_anyway_with_flags (abfd
,
155 (bed
->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed
->dynamic_sec_flags
160 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
164 s
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
166 || !bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
170 if (bed
->want_got_plt
)
172 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
174 || !bfd_set_section_alignment (abfd
, s
,
175 bed
->s
->log_file_align
))
180 /* The first bit of the global offset table is the header. */
181 s
->size
+= bed
->got_header_size
;
183 if (bed
->want_got_sym
)
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
189 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
190 "_GLOBAL_OFFSET_TABLE_");
191 elf_hash_table (info
)->hgot
= h
;
199 /* Create a strtab to hold the dynamic symbol names. */
201 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
203 struct elf_link_hash_table
*hash_table
;
205 hash_table
= elf_hash_table (info
);
206 if (hash_table
->dynobj
== NULL
)
207 hash_table
->dynobj
= abfd
;
209 if (hash_table
->dynstr
== NULL
)
211 hash_table
->dynstr
= _bfd_elf_strtab_init ();
212 if (hash_table
->dynstr
== NULL
)
218 /* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
226 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
230 const struct elf_backend_data
*bed
;
231 struct elf_link_hash_entry
*h
;
233 if (! is_elf_hash_table (info
->hash
))
236 if (elf_hash_table (info
)->dynamic_sections_created
)
239 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
242 abfd
= elf_hash_table (info
)->dynobj
;
243 bed
= get_elf_backend_data (abfd
);
245 flags
= bed
->dynamic_sec_flags
;
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
249 if (bfd_link_executable (info
))
251 s
= bfd_make_section_anyway_with_flags (abfd
, ".interp",
252 flags
| SEC_READONLY
);
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
259 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_d",
260 flags
| SEC_READONLY
);
262 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
265 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version",
266 flags
| SEC_READONLY
);
268 || ! bfd_set_section_alignment (abfd
, s
, 1))
271 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.version_r",
272 flags
| SEC_READONLY
);
274 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
277 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynsym",
278 flags
| SEC_READONLY
);
280 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
282 elf_hash_table (info
)->dynsym
= s
;
284 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynstr",
285 flags
| SEC_READONLY
);
289 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynamic", flags
);
291 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
294 /* The special symbol _DYNAMIC is always set to the start of the
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
300 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
, "_DYNAMIC");
301 elf_hash_table (info
)->hdynamic
= h
;
307 s
= bfd_make_section_anyway_with_flags (abfd
, ".hash",
308 flags
| SEC_READONLY
);
310 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
312 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
315 if (info
->emit_gnu_hash
)
317 s
= bfd_make_section_anyway_with_flags (abfd
, ".gnu.hash",
318 flags
| SEC_READONLY
);
320 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed
->s
->arch_size
== 64)
326 elf_section_data (s
)->this_hdr
.sh_entsize
= 0;
328 elf_section_data (s
)->this_hdr
.sh_entsize
= 4;
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
334 if (bed
->elf_backend_create_dynamic_sections
== NULL
335 || ! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
338 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
343 /* Create dynamic sections when linking against a dynamic object. */
346 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
348 flagword flags
, pltflags
;
349 struct elf_link_hash_entry
*h
;
351 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
352 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
356 flags
= bed
->dynamic_sec_flags
;
359 if (bed
->plt_not_loaded
)
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
363 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
365 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
366 if (bed
->plt_readonly
)
367 pltflags
|= SEC_READONLY
;
369 s
= bfd_make_section_anyway_with_flags (abfd
, ".plt", pltflags
);
371 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
377 if (bed
->want_plt_sym
)
379 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s
,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info
)->hplt
= h
;
386 s
= bfd_make_section_anyway_with_flags (abfd
,
387 (bed
->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags
| SEC_READONLY
);
391 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
395 if (! _bfd_elf_create_got_section (abfd
, info
))
398 if (bed
->want_dynbss
)
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
406 s
= bfd_make_section_anyway_with_flags (abfd
, ".dynbss",
407 (SEC_ALLOC
| SEC_LINKER_CREATED
));
411 /* The .rel[a].bss section holds copy relocs. This section is not
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
422 if (! bfd_link_pic (info
))
424 s
= bfd_make_section_anyway_with_flags (abfd
,
425 (bed
->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags
| SEC_READONLY
);
429 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
437 /* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
446 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
447 struct elf_link_hash_entry
*h
)
449 if (h
->dynindx
== -1)
451 struct elf_strtab_hash
*dynstr
;
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h
->other
))
464 if (h
->root
.type
!= bfd_link_hash_undefined
465 && h
->root
.type
!= bfd_link_hash_undefweak
)
468 if (!elf_hash_table (info
)->is_relocatable_executable
)
476 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
477 ++elf_hash_table (info
)->dynsymcount
;
479 dynstr
= elf_hash_table (info
)->dynstr
;
482 /* Create a strtab to hold the dynamic symbol names. */
483 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
488 /* We don't put any version information in the dynamic string
490 name
= h
->root
.root
.string
;
491 p
= strchr (name
, ELF_VER_CHR
);
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
500 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
505 if (indx
== (bfd_size_type
) -1)
507 h
->dynstr_index
= indx
;
513 /* Mark a symbol dynamic. */
516 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info
*info
,
517 struct elf_link_hash_entry
*h
,
518 Elf_Internal_Sym
*sym
)
520 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
522 /* It may be called more than once on the same H. */
523 if(h
->dynamic
|| bfd_link_relocatable (info
))
526 if ((info
->dynamic_data
527 && (h
->type
== STT_OBJECT
529 && ELF_ST_TYPE (sym
->st_info
) == STT_OBJECT
)))
531 && h
->root
.type
== bfd_link_hash_new
532 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
536 /* Record an assignment to a symbol made by a linker script. We need
537 this in case some dynamic object refers to this symbol. */
540 bfd_elf_record_link_assignment (bfd
*output_bfd
,
541 struct bfd_link_info
*info
,
546 struct elf_link_hash_entry
*h
, *hv
;
547 struct elf_link_hash_table
*htab
;
548 const struct elf_backend_data
*bed
;
550 if (!is_elf_hash_table (info
->hash
))
553 htab
= elf_hash_table (info
);
554 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
558 switch (h
->root
.type
)
560 case bfd_link_hash_defined
:
561 case bfd_link_hash_defweak
:
562 case bfd_link_hash_common
:
564 case bfd_link_hash_undefweak
:
565 case bfd_link_hash_undefined
:
566 /* Since we're defining the symbol, don't let it seem to have not
567 been defined. record_dynamic_symbol and size_dynamic_sections
568 may depend on this. */
569 h
->root
.type
= bfd_link_hash_new
;
570 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
571 bfd_link_repair_undef_list (&htab
->root
);
573 case bfd_link_hash_new
:
574 bfd_elf_link_mark_dynamic_symbol (info
, h
, NULL
);
577 case bfd_link_hash_indirect
:
578 /* We had a versioned symbol in a dynamic library. We make the
579 the versioned symbol point to this one. */
580 bed
= get_elf_backend_data (output_bfd
);
582 while (hv
->root
.type
== bfd_link_hash_indirect
583 || hv
->root
.type
== bfd_link_hash_warning
)
584 hv
= (struct elf_link_hash_entry
*) hv
->root
.u
.i
.link
;
585 /* We don't need to update h->root.u since linker will set them
587 h
->root
.type
= bfd_link_hash_undefined
;
588 hv
->root
.type
= bfd_link_hash_indirect
;
589 hv
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
590 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hv
);
592 case bfd_link_hash_warning
:
597 /* If this symbol is being provided by the linker script, and it is
598 currently defined by a dynamic object, but not by a regular
599 object, then mark it as undefined so that the generic linker will
600 force the correct value. */
604 h
->root
.type
= bfd_link_hash_undefined
;
606 /* If this symbol is not being provided by the linker script, and it is
607 currently defined by a dynamic object, but not by a regular object,
608 then clear out any version information because the symbol will not be
609 associated with the dynamic object any more. */
613 h
->verinfo
.verdef
= NULL
;
619 bed
= get_elf_backend_data (output_bfd
);
620 if (ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
)
621 h
->other
= (h
->other
& ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN
;
622 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
625 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
627 if (!bfd_link_relocatable (info
)
629 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
630 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
635 || bfd_link_pic (info
)
636 || (bfd_link_executable (info
)
637 && elf_hash_table (info
)->is_relocatable_executable
))
640 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
643 /* If this is a weak defined symbol, and we know a corresponding
644 real symbol from the same dynamic object, make sure the real
645 symbol is also made into a dynamic symbol. */
646 if (h
->u
.weakdef
!= NULL
647 && h
->u
.weakdef
->dynindx
== -1)
649 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
657 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
658 success, and 2 on a failure caused by attempting to record a symbol
659 in a discarded section, eg. a discarded link-once section symbol. */
662 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
667 struct elf_link_local_dynamic_entry
*entry
;
668 struct elf_link_hash_table
*eht
;
669 struct elf_strtab_hash
*dynstr
;
670 unsigned long dynstr_index
;
672 Elf_External_Sym_Shndx eshndx
;
673 char esym
[sizeof (Elf64_External_Sym
)];
675 if (! is_elf_hash_table (info
->hash
))
678 /* See if the entry exists already. */
679 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
680 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
683 amt
= sizeof (*entry
);
684 entry
= (struct elf_link_local_dynamic_entry
*) bfd_alloc (input_bfd
, amt
);
688 /* Go find the symbol, so that we can find it's name. */
689 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
690 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
692 bfd_release (input_bfd
, entry
);
696 if (entry
->isym
.st_shndx
!= SHN_UNDEF
697 && entry
->isym
.st_shndx
< SHN_LORESERVE
)
701 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
702 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
704 /* We can still bfd_release here as nothing has done another
705 bfd_alloc. We can't do this later in this function. */
706 bfd_release (input_bfd
, entry
);
711 name
= (bfd_elf_string_from_elf_section
712 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
713 entry
->isym
.st_name
));
715 dynstr
= elf_hash_table (info
)->dynstr
;
718 /* Create a strtab to hold the dynamic symbol names. */
719 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
724 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
725 if (dynstr_index
== (unsigned long) -1)
727 entry
->isym
.st_name
= dynstr_index
;
729 eht
= elf_hash_table (info
);
731 entry
->next
= eht
->dynlocal
;
732 eht
->dynlocal
= entry
;
733 entry
->input_bfd
= input_bfd
;
734 entry
->input_indx
= input_indx
;
737 /* Whatever binding the symbol had before, it's now local. */
739 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
741 /* The dynindx will be set at the end of size_dynamic_sections. */
746 /* Return the dynindex of a local dynamic symbol. */
749 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
753 struct elf_link_local_dynamic_entry
*e
;
755 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
756 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
761 /* This function is used to renumber the dynamic symbols, if some of
762 them are removed because they are marked as local. This is called
763 via elf_link_hash_traverse. */
766 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
769 size_t *count
= (size_t *) data
;
774 if (h
->dynindx
!= -1)
775 h
->dynindx
= ++(*count
);
781 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
782 STB_LOCAL binding. */
785 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
788 size_t *count
= (size_t *) data
;
790 if (!h
->forced_local
)
793 if (h
->dynindx
!= -1)
794 h
->dynindx
= ++(*count
);
799 /* Return true if the dynamic symbol for a given section should be
800 omitted when creating a shared library. */
802 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
803 struct bfd_link_info
*info
,
806 struct elf_link_hash_table
*htab
;
809 switch (elf_section_data (p
)->this_hdr
.sh_type
)
813 /* If sh_type is yet undecided, assume it could be
814 SHT_PROGBITS/SHT_NOBITS. */
816 htab
= elf_hash_table (info
);
817 if (p
== htab
->tls_sec
)
820 if (htab
->text_index_section
!= NULL
)
821 return p
!= htab
->text_index_section
&& p
!= htab
->data_index_section
;
823 return (htab
->dynobj
!= NULL
824 && (ip
= bfd_get_linker_section (htab
->dynobj
, p
->name
)) != NULL
825 && ip
->output_section
== p
);
827 /* There shouldn't be section relative relocations
828 against any other section. */
834 /* Assign dynsym indices. In a shared library we generate a section
835 symbol for each output section, which come first. Next come symbols
836 which have been forced to local binding. Then all of the back-end
837 allocated local dynamic syms, followed by the rest of the global
841 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
,
842 struct bfd_link_info
*info
,
843 unsigned long *section_sym_count
)
845 unsigned long dynsymcount
= 0;
847 if (bfd_link_pic (info
)
848 || elf_hash_table (info
)->is_relocatable_executable
)
850 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
852 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
853 if ((p
->flags
& SEC_EXCLUDE
) == 0
854 && (p
->flags
& SEC_ALLOC
) != 0
855 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
856 elf_section_data (p
)->dynindx
= ++dynsymcount
;
858 elf_section_data (p
)->dynindx
= 0;
860 *section_sym_count
= dynsymcount
;
862 elf_link_hash_traverse (elf_hash_table (info
),
863 elf_link_renumber_local_hash_table_dynsyms
,
866 if (elf_hash_table (info
)->dynlocal
)
868 struct elf_link_local_dynamic_entry
*p
;
869 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
870 p
->dynindx
= ++dynsymcount
;
873 elf_link_hash_traverse (elf_hash_table (info
),
874 elf_link_renumber_hash_table_dynsyms
,
877 /* There is an unused NULL entry at the head of the table which
878 we must account for in our count. Unless there weren't any
879 symbols, which means we'll have no table at all. */
880 if (dynsymcount
!= 0)
883 elf_hash_table (info
)->dynsymcount
= dynsymcount
;
887 /* Merge st_other field. */
890 elf_merge_st_other (bfd
*abfd
, struct elf_link_hash_entry
*h
,
891 const Elf_Internal_Sym
*isym
, asection
*sec
,
892 bfd_boolean definition
, bfd_boolean dynamic
)
894 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
896 /* If st_other has a processor-specific meaning, specific
897 code might be needed here. */
898 if (bed
->elf_backend_merge_symbol_attribute
)
899 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
904 unsigned symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
905 unsigned hvis
= ELF_ST_VISIBILITY (h
->other
);
907 /* Keep the most constraining visibility. Leave the remainder
908 of the st_other field to elf_backend_merge_symbol_attribute. */
909 if (symvis
- 1 < hvis
- 1)
910 h
->other
= symvis
| (h
->other
& ~ELF_ST_VISIBILITY (-1));
913 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_DEFAULT
914 && (sec
->flags
& SEC_READONLY
) == 0)
915 h
->protected_def
= 1;
918 /* This function is called when we want to merge a new symbol with an
919 existing symbol. It handles the various cases which arise when we
920 find a definition in a dynamic object, or when there is already a
921 definition in a dynamic object. The new symbol is described by
922 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
923 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
924 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
925 of an old common symbol. We set OVERRIDE if the old symbol is
926 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
927 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
928 to change. By OK to change, we mean that we shouldn't warn if the
929 type or size does change. */
932 _bfd_elf_merge_symbol (bfd
*abfd
,
933 struct bfd_link_info
*info
,
935 Elf_Internal_Sym
*sym
,
938 struct elf_link_hash_entry
**sym_hash
,
940 bfd_boolean
*pold_weak
,
941 unsigned int *pold_alignment
,
943 bfd_boolean
*override
,
944 bfd_boolean
*type_change_ok
,
945 bfd_boolean
*size_change_ok
,
946 bfd_boolean
*matched
)
948 asection
*sec
, *oldsec
;
949 struct elf_link_hash_entry
*h
;
950 struct elf_link_hash_entry
*hi
;
951 struct elf_link_hash_entry
*flip
;
954 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
955 bfd_boolean newweak
, oldweak
, newfunc
, oldfunc
;
956 const struct elf_backend_data
*bed
;
963 bind
= ELF_ST_BIND (sym
->st_info
);
965 if (! bfd_is_und_section (sec
))
966 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
968 h
= ((struct elf_link_hash_entry
*)
969 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
974 bed
= get_elf_backend_data (abfd
);
976 /* NEW_VERSION is the symbol version of the new symbol. */
977 if (h
->versioned
!= unversioned
)
979 /* Symbol version is unknown or versioned. */
980 new_version
= strrchr (name
, ELF_VER_CHR
);
983 if (h
->versioned
== unknown
)
985 if (new_version
> name
&& new_version
[-1] != ELF_VER_CHR
)
986 h
->versioned
= versioned_hidden
;
988 h
->versioned
= versioned
;
991 if (new_version
[0] == '\0')
995 h
->versioned
= unversioned
;
1000 /* For merging, we only care about real symbols. But we need to make
1001 sure that indirect symbol dynamic flags are updated. */
1003 while (h
->root
.type
== bfd_link_hash_indirect
1004 || h
->root
.type
== bfd_link_hash_warning
)
1005 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1009 if (hi
== h
|| h
->root
.type
== bfd_link_hash_new
)
1013 /* OLD_HIDDEN is true if the existing symbol is only visibile
1014 to the symbol with the same symbol version. NEW_HIDDEN is
1015 true if the new symbol is only visibile to the symbol with
1016 the same symbol version. */
1017 bfd_boolean old_hidden
= h
->versioned
== versioned_hidden
;
1018 bfd_boolean new_hidden
= hi
->versioned
== versioned_hidden
;
1019 if (!old_hidden
&& !new_hidden
)
1020 /* The new symbol matches the existing symbol if both
1025 /* OLD_VERSION is the symbol version of the existing
1029 if (h
->versioned
>= versioned
)
1030 old_version
= strrchr (h
->root
.root
.string
,
1035 /* The new symbol matches the existing symbol if they
1036 have the same symbol version. */
1037 *matched
= (old_version
== new_version
1038 || (old_version
!= NULL
1039 && new_version
!= NULL
1040 && strcmp (old_version
, new_version
) == 0));
1045 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1050 switch (h
->root
.type
)
1055 case bfd_link_hash_undefined
:
1056 case bfd_link_hash_undefweak
:
1057 oldbfd
= h
->root
.u
.undef
.abfd
;
1060 case bfd_link_hash_defined
:
1061 case bfd_link_hash_defweak
:
1062 oldbfd
= h
->root
.u
.def
.section
->owner
;
1063 oldsec
= h
->root
.u
.def
.section
;
1066 case bfd_link_hash_common
:
1067 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
1068 oldsec
= h
->root
.u
.c
.p
->section
;
1070 *pold_alignment
= h
->root
.u
.c
.p
->alignment_power
;
1073 if (poldbfd
&& *poldbfd
== NULL
)
1076 /* Differentiate strong and weak symbols. */
1077 newweak
= bind
== STB_WEAK
;
1078 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1079 || h
->root
.type
== bfd_link_hash_undefweak
);
1081 *pold_weak
= oldweak
;
1083 /* This code is for coping with dynamic objects, and is only useful
1084 if we are doing an ELF link. */
1085 if (!(*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
1088 /* We have to check it for every instance since the first few may be
1089 references and not all compilers emit symbol type for undefined
1091 bfd_elf_link_mark_dynamic_symbol (info
, h
, sym
);
1093 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1094 respectively, is from a dynamic object. */
1096 newdyn
= (abfd
->flags
& DYNAMIC
) != 0;
1098 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1099 syms and defined syms in dynamic libraries respectively.
1100 ref_dynamic on the other hand can be set for a symbol defined in
1101 a dynamic library, and def_dynamic may not be set; When the
1102 definition in a dynamic lib is overridden by a definition in the
1103 executable use of the symbol in the dynamic lib becomes a
1104 reference to the executable symbol. */
1107 if (bfd_is_und_section (sec
))
1109 if (bind
!= STB_WEAK
)
1111 h
->ref_dynamic_nonweak
= 1;
1112 hi
->ref_dynamic_nonweak
= 1;
1117 /* Update the existing symbol only if they match. */
1120 hi
->dynamic_def
= 1;
1124 /* If we just created the symbol, mark it as being an ELF symbol.
1125 Other than that, there is nothing to do--there is no merge issue
1126 with a newly defined symbol--so we just return. */
1128 if (h
->root
.type
== bfd_link_hash_new
)
1134 /* In cases involving weak versioned symbols, we may wind up trying
1135 to merge a symbol with itself. Catch that here, to avoid the
1136 confusion that results if we try to override a symbol with
1137 itself. The additional tests catch cases like
1138 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1139 dynamic object, which we do want to handle here. */
1141 && (newweak
|| oldweak
)
1142 && ((abfd
->flags
& DYNAMIC
) == 0
1143 || !h
->def_regular
))
1148 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
1149 else if (oldsec
!= NULL
)
1151 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1152 indices used by MIPS ELF. */
1153 olddyn
= (oldsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
1156 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1157 respectively, appear to be a definition rather than reference. */
1159 newdef
= !bfd_is_und_section (sec
) && !bfd_is_com_section (sec
);
1161 olddef
= (h
->root
.type
!= bfd_link_hash_undefined
1162 && h
->root
.type
!= bfd_link_hash_undefweak
1163 && h
->root
.type
!= bfd_link_hash_common
);
1165 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1166 respectively, appear to be a function. */
1168 newfunc
= (ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1169 && bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)));
1171 oldfunc
= (h
->type
!= STT_NOTYPE
1172 && bed
->is_function_type (h
->type
));
1174 /* When we try to create a default indirect symbol from the dynamic
1175 definition with the default version, we skip it if its type and
1176 the type of existing regular definition mismatch. */
1177 if (pold_alignment
== NULL
1181 && (((olddef
|| h
->root
.type
== bfd_link_hash_common
)
1182 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1183 && ELF_ST_TYPE (sym
->st_info
) != STT_NOTYPE
1184 && h
->type
!= STT_NOTYPE
1185 && !(newfunc
&& oldfunc
))
1187 && ((h
->type
== STT_GNU_IFUNC
)
1188 != (ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)))))
1194 /* Check TLS symbols. We don't check undefined symbols introduced
1195 by "ld -u" which have no type (and oldbfd NULL), and we don't
1196 check symbols from plugins because they also have no type. */
1198 && (oldbfd
->flags
& BFD_PLUGIN
) == 0
1199 && (abfd
->flags
& BFD_PLUGIN
) == 0
1200 && ELF_ST_TYPE (sym
->st_info
) != h
->type
1201 && (ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
))
1204 bfd_boolean ntdef
, tdef
;
1205 asection
*ntsec
, *tsec
;
1207 if (h
->type
== STT_TLS
)
1227 (*_bfd_error_handler
)
1228 (_("%s: TLS definition in %B section %A "
1229 "mismatches non-TLS definition in %B section %A"),
1230 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
1231 else if (!tdef
&& !ntdef
)
1232 (*_bfd_error_handler
)
1233 (_("%s: TLS reference in %B "
1234 "mismatches non-TLS reference in %B"),
1235 tbfd
, ntbfd
, h
->root
.root
.string
);
1237 (*_bfd_error_handler
)
1238 (_("%s: TLS definition in %B section %A "
1239 "mismatches non-TLS reference in %B"),
1240 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
1242 (*_bfd_error_handler
)
1243 (_("%s: TLS reference in %B "
1244 "mismatches non-TLS definition in %B section %A"),
1245 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
1247 bfd_set_error (bfd_error_bad_value
);
1251 /* If the old symbol has non-default visibility, we ignore the new
1252 definition from a dynamic object. */
1254 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1255 && !bfd_is_und_section (sec
))
1258 /* Make sure this symbol is dynamic. */
1260 hi
->ref_dynamic
= 1;
1261 /* A protected symbol has external availability. Make sure it is
1262 recorded as dynamic.
1264 FIXME: Should we check type and size for protected symbol? */
1265 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
1266 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1271 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1274 /* If the new symbol with non-default visibility comes from a
1275 relocatable file and the old definition comes from a dynamic
1276 object, we remove the old definition. */
1277 if (hi
->root
.type
== bfd_link_hash_indirect
)
1279 /* Handle the case where the old dynamic definition is
1280 default versioned. We need to copy the symbol info from
1281 the symbol with default version to the normal one if it
1282 was referenced before. */
1285 hi
->root
.type
= h
->root
.type
;
1286 h
->root
.type
= bfd_link_hash_indirect
;
1287 (*bed
->elf_backend_copy_indirect_symbol
) (info
, hi
, h
);
1289 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1290 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1292 /* If the new symbol is hidden or internal, completely undo
1293 any dynamic link state. */
1294 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1295 h
->forced_local
= 0;
1302 /* FIXME: Should we check type and size for protected symbol? */
1312 /* If the old symbol was undefined before, then it will still be
1313 on the undefs list. If the new symbol is undefined or
1314 common, we can't make it bfd_link_hash_new here, because new
1315 undefined or common symbols will be added to the undefs list
1316 by _bfd_generic_link_add_one_symbol. Symbols may not be
1317 added twice to the undefs list. Also, if the new symbol is
1318 undefweak then we don't want to lose the strong undef. */
1319 if (h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1321 h
->root
.type
= bfd_link_hash_undefined
;
1322 h
->root
.u
.undef
.abfd
= abfd
;
1326 h
->root
.type
= bfd_link_hash_new
;
1327 h
->root
.u
.undef
.abfd
= NULL
;
1330 if (ELF_ST_VISIBILITY (sym
->st_other
) != STV_PROTECTED
)
1332 /* If the new symbol is hidden or internal, completely undo
1333 any dynamic link state. */
1334 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1335 h
->forced_local
= 0;
1341 /* FIXME: Should we check type and size for protected symbol? */
1347 /* If a new weak symbol definition comes from a regular file and the
1348 old symbol comes from a dynamic library, we treat the new one as
1349 strong. Similarly, an old weak symbol definition from a regular
1350 file is treated as strong when the new symbol comes from a dynamic
1351 library. Further, an old weak symbol from a dynamic library is
1352 treated as strong if the new symbol is from a dynamic library.
1353 This reflects the way glibc's ld.so works.
1355 Do this before setting *type_change_ok or *size_change_ok so that
1356 we warn properly when dynamic library symbols are overridden. */
1358 if (newdef
&& !newdyn
&& olddyn
)
1360 if (olddef
&& newdyn
)
1363 /* Allow changes between different types of function symbol. */
1364 if (newfunc
&& oldfunc
)
1365 *type_change_ok
= TRUE
;
1367 /* It's OK to change the type if either the existing symbol or the
1368 new symbol is weak. A type change is also OK if the old symbol
1369 is undefined and the new symbol is defined. */
1374 && h
->root
.type
== bfd_link_hash_undefined
))
1375 *type_change_ok
= TRUE
;
1377 /* It's OK to change the size if either the existing symbol or the
1378 new symbol is weak, or if the old symbol is undefined. */
1381 || h
->root
.type
== bfd_link_hash_undefined
)
1382 *size_change_ok
= TRUE
;
1384 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1385 symbol, respectively, appears to be a common symbol in a dynamic
1386 object. If a symbol appears in an uninitialized section, and is
1387 not weak, and is not a function, then it may be a common symbol
1388 which was resolved when the dynamic object was created. We want
1389 to treat such symbols specially, because they raise special
1390 considerations when setting the symbol size: if the symbol
1391 appears as a common symbol in a regular object, and the size in
1392 the regular object is larger, we must make sure that we use the
1393 larger size. This problematic case can always be avoided in C,
1394 but it must be handled correctly when using Fortran shared
1397 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1398 likewise for OLDDYNCOMMON and OLDDEF.
1400 Note that this test is just a heuristic, and that it is quite
1401 possible to have an uninitialized symbol in a shared object which
1402 is really a definition, rather than a common symbol. This could
1403 lead to some minor confusion when the symbol really is a common
1404 symbol in some regular object. However, I think it will be
1410 && (sec
->flags
& SEC_ALLOC
) != 0
1411 && (sec
->flags
& SEC_LOAD
) == 0
1414 newdyncommon
= TRUE
;
1416 newdyncommon
= FALSE
;
1420 && h
->root
.type
== bfd_link_hash_defined
1422 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1423 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1426 olddyncommon
= TRUE
;
1428 olddyncommon
= FALSE
;
1430 /* We now know everything about the old and new symbols. We ask the
1431 backend to check if we can merge them. */
1432 if (bed
->merge_symbol
!= NULL
)
1434 if (!bed
->merge_symbol (h
, sym
, psec
, newdef
, olddef
, oldbfd
, oldsec
))
1439 /* If both the old and the new symbols look like common symbols in a
1440 dynamic object, set the size of the symbol to the larger of the
1445 && sym
->st_size
!= h
->size
)
1447 /* Since we think we have two common symbols, issue a multiple
1448 common warning if desired. Note that we only warn if the
1449 size is different. If the size is the same, we simply let
1450 the old symbol override the new one as normally happens with
1451 symbols defined in dynamic objects. */
1453 if (! ((*info
->callbacks
->multiple_common
)
1454 (info
, &h
->root
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1457 if (sym
->st_size
> h
->size
)
1458 h
->size
= sym
->st_size
;
1460 *size_change_ok
= TRUE
;
1463 /* If we are looking at a dynamic object, and we have found a
1464 definition, we need to see if the symbol was already defined by
1465 some other object. If so, we want to use the existing
1466 definition, and we do not want to report a multiple symbol
1467 definition error; we do this by clobbering *PSEC to be
1468 bfd_und_section_ptr.
1470 We treat a common symbol as a definition if the symbol in the
1471 shared library is a function, since common symbols always
1472 represent variables; this can cause confusion in principle, but
1473 any such confusion would seem to indicate an erroneous program or
1474 shared library. We also permit a common symbol in a regular
1475 object to override a weak symbol in a shared object. */
1480 || (h
->root
.type
== bfd_link_hash_common
1481 && (newweak
|| newfunc
))))
1485 newdyncommon
= FALSE
;
1487 *psec
= sec
= bfd_und_section_ptr
;
1488 *size_change_ok
= TRUE
;
1490 /* If we get here when the old symbol is a common symbol, then
1491 we are explicitly letting it override a weak symbol or
1492 function in a dynamic object, and we don't want to warn about
1493 a type change. If the old symbol is a defined symbol, a type
1494 change warning may still be appropriate. */
1496 if (h
->root
.type
== bfd_link_hash_common
)
1497 *type_change_ok
= TRUE
;
1500 /* Handle the special case of an old common symbol merging with a
1501 new symbol which looks like a common symbol in a shared object.
1502 We change *PSEC and *PVALUE to make the new symbol look like a
1503 common symbol, and let _bfd_generic_link_add_one_symbol do the
1507 && h
->root
.type
== bfd_link_hash_common
)
1511 newdyncommon
= FALSE
;
1512 *pvalue
= sym
->st_size
;
1513 *psec
= sec
= bed
->common_section (oldsec
);
1514 *size_change_ok
= TRUE
;
1517 /* Skip weak definitions of symbols that are already defined. */
1518 if (newdef
&& olddef
&& newweak
)
1520 /* Don't skip new non-IR weak syms. */
1521 if (!(oldbfd
!= NULL
1522 && (oldbfd
->flags
& BFD_PLUGIN
) != 0
1523 && (abfd
->flags
& BFD_PLUGIN
) == 0))
1529 /* Merge st_other. If the symbol already has a dynamic index,
1530 but visibility says it should not be visible, turn it into a
1532 elf_merge_st_other (abfd
, h
, sym
, sec
, newdef
, newdyn
);
1533 if (h
->dynindx
!= -1)
1534 switch (ELF_ST_VISIBILITY (h
->other
))
1538 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1543 /* If the old symbol is from a dynamic object, and the new symbol is
1544 a definition which is not from a dynamic object, then the new
1545 symbol overrides the old symbol. Symbols from regular files
1546 always take precedence over symbols from dynamic objects, even if
1547 they are defined after the dynamic object in the link.
1549 As above, we again permit a common symbol in a regular object to
1550 override a definition in a shared object if the shared object
1551 symbol is a function or is weak. */
1556 || (bfd_is_com_section (sec
)
1557 && (oldweak
|| oldfunc
)))
1562 /* Change the hash table entry to undefined, and let
1563 _bfd_generic_link_add_one_symbol do the right thing with the
1566 h
->root
.type
= bfd_link_hash_undefined
;
1567 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1568 *size_change_ok
= TRUE
;
1571 olddyncommon
= FALSE
;
1573 /* We again permit a type change when a common symbol may be
1574 overriding a function. */
1576 if (bfd_is_com_section (sec
))
1580 /* If a common symbol overrides a function, make sure
1581 that it isn't defined dynamically nor has type
1584 h
->type
= STT_NOTYPE
;
1586 *type_change_ok
= TRUE
;
1589 if (hi
->root
.type
== bfd_link_hash_indirect
)
1592 /* This union may have been set to be non-NULL when this symbol
1593 was seen in a dynamic object. We must force the union to be
1594 NULL, so that it is correct for a regular symbol. */
1595 h
->verinfo
.vertree
= NULL
;
1598 /* Handle the special case of a new common symbol merging with an
1599 old symbol that looks like it might be a common symbol defined in
1600 a shared object. Note that we have already handled the case in
1601 which a new common symbol should simply override the definition
1602 in the shared library. */
1605 && bfd_is_com_section (sec
)
1608 /* It would be best if we could set the hash table entry to a
1609 common symbol, but we don't know what to use for the section
1610 or the alignment. */
1611 if (! ((*info
->callbacks
->multiple_common
)
1612 (info
, &h
->root
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1615 /* If the presumed common symbol in the dynamic object is
1616 larger, pretend that the new symbol has its size. */
1618 if (h
->size
> *pvalue
)
1621 /* We need to remember the alignment required by the symbol
1622 in the dynamic object. */
1623 BFD_ASSERT (pold_alignment
);
1624 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1627 olddyncommon
= FALSE
;
1629 h
->root
.type
= bfd_link_hash_undefined
;
1630 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1632 *size_change_ok
= TRUE
;
1633 *type_change_ok
= TRUE
;
1635 if (hi
->root
.type
== bfd_link_hash_indirect
)
1638 h
->verinfo
.vertree
= NULL
;
1643 /* Handle the case where we had a versioned symbol in a dynamic
1644 library and now find a definition in a normal object. In this
1645 case, we make the versioned symbol point to the normal one. */
1646 flip
->root
.type
= h
->root
.type
;
1647 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1648 h
->root
.type
= bfd_link_hash_indirect
;
1649 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1650 (*bed
->elf_backend_copy_indirect_symbol
) (info
, flip
, h
);
1654 flip
->ref_dynamic
= 1;
1661 /* This function is called to create an indirect symbol from the
1662 default for the symbol with the default version if needed. The
1663 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1664 set DYNSYM if the new indirect symbol is dynamic. */
1667 _bfd_elf_add_default_symbol (bfd
*abfd
,
1668 struct bfd_link_info
*info
,
1669 struct elf_link_hash_entry
*h
,
1671 Elf_Internal_Sym
*sym
,
1675 bfd_boolean
*dynsym
)
1677 bfd_boolean type_change_ok
;
1678 bfd_boolean size_change_ok
;
1681 struct elf_link_hash_entry
*hi
;
1682 struct bfd_link_hash_entry
*bh
;
1683 const struct elf_backend_data
*bed
;
1684 bfd_boolean collect
;
1685 bfd_boolean dynamic
;
1686 bfd_boolean override
;
1688 size_t len
, shortlen
;
1690 bfd_boolean matched
;
1692 if (h
->versioned
== unversioned
|| h
->versioned
== versioned_hidden
)
1695 /* If this symbol has a version, and it is the default version, we
1696 create an indirect symbol from the default name to the fully
1697 decorated name. This will cause external references which do not
1698 specify a version to be bound to this version of the symbol. */
1699 p
= strchr (name
, ELF_VER_CHR
);
1700 if (h
->versioned
== unknown
)
1704 h
->versioned
= unversioned
;
1709 if (p
[1] != ELF_VER_CHR
)
1711 h
->versioned
= versioned_hidden
;
1715 h
->versioned
= versioned
;
1719 bed
= get_elf_backend_data (abfd
);
1720 collect
= bed
->collect
;
1721 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1723 shortlen
= p
- name
;
1724 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1725 if (shortname
== NULL
)
1727 memcpy (shortname
, name
, shortlen
);
1728 shortname
[shortlen
] = '\0';
1730 /* We are going to create a new symbol. Merge it with any existing
1731 symbol with this name. For the purposes of the merge, act as
1732 though we were defining the symbol we just defined, although we
1733 actually going to define an indirect symbol. */
1734 type_change_ok
= FALSE
;
1735 size_change_ok
= FALSE
;
1738 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1739 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1740 &type_change_ok
, &size_change_ok
, &matched
))
1748 /* Add the default symbol if not performing a relocatable link. */
1749 if (! bfd_link_relocatable (info
))
1752 if (! (_bfd_generic_link_add_one_symbol
1753 (info
, abfd
, shortname
, BSF_INDIRECT
,
1754 bfd_ind_section_ptr
,
1755 0, name
, FALSE
, collect
, &bh
)))
1757 hi
= (struct elf_link_hash_entry
*) bh
;
1762 /* In this case the symbol named SHORTNAME is overriding the
1763 indirect symbol we want to add. We were planning on making
1764 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1765 is the name without a version. NAME is the fully versioned
1766 name, and it is the default version.
1768 Overriding means that we already saw a definition for the
1769 symbol SHORTNAME in a regular object, and it is overriding
1770 the symbol defined in the dynamic object.
1772 When this happens, we actually want to change NAME, the
1773 symbol we just added, to refer to SHORTNAME. This will cause
1774 references to NAME in the shared object to become references
1775 to SHORTNAME in the regular object. This is what we expect
1776 when we override a function in a shared object: that the
1777 references in the shared object will be mapped to the
1778 definition in the regular object. */
1780 while (hi
->root
.type
== bfd_link_hash_indirect
1781 || hi
->root
.type
== bfd_link_hash_warning
)
1782 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1784 h
->root
.type
= bfd_link_hash_indirect
;
1785 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1789 hi
->ref_dynamic
= 1;
1793 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1798 /* Now set HI to H, so that the following code will set the
1799 other fields correctly. */
1803 /* Check if HI is a warning symbol. */
1804 if (hi
->root
.type
== bfd_link_hash_warning
)
1805 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1807 /* If there is a duplicate definition somewhere, then HI may not
1808 point to an indirect symbol. We will have reported an error to
1809 the user in that case. */
1811 if (hi
->root
.type
== bfd_link_hash_indirect
)
1813 struct elf_link_hash_entry
*ht
;
1815 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1816 (*bed
->elf_backend_copy_indirect_symbol
) (info
, ht
, hi
);
1818 /* A reference to the SHORTNAME symbol from a dynamic library
1819 will be satisfied by the versioned symbol at runtime. In
1820 effect, we have a reference to the versioned symbol. */
1821 ht
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1822 hi
->dynamic_def
|= ht
->dynamic_def
;
1824 /* See if the new flags lead us to realize that the symbol must
1830 if (! bfd_link_executable (info
)
1837 if (hi
->ref_regular
)
1843 /* We also need to define an indirection from the nondefault version
1847 len
= strlen (name
);
1848 shortname
= (char *) bfd_hash_allocate (&info
->hash
->table
, len
);
1849 if (shortname
== NULL
)
1851 memcpy (shortname
, name
, shortlen
);
1852 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1854 /* Once again, merge with any existing symbol. */
1855 type_change_ok
= FALSE
;
1856 size_change_ok
= FALSE
;
1858 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &tmp_sec
, &value
,
1859 &hi
, poldbfd
, NULL
, NULL
, &skip
, &override
,
1860 &type_change_ok
, &size_change_ok
, &matched
))
1868 /* Here SHORTNAME is a versioned name, so we don't expect to see
1869 the type of override we do in the case above unless it is
1870 overridden by a versioned definition. */
1871 if (hi
->root
.type
!= bfd_link_hash_defined
1872 && hi
->root
.type
!= bfd_link_hash_defweak
)
1873 (*_bfd_error_handler
)
1874 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1880 if (! (_bfd_generic_link_add_one_symbol
1881 (info
, abfd
, shortname
, BSF_INDIRECT
,
1882 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1884 hi
= (struct elf_link_hash_entry
*) bh
;
1886 /* If there is a duplicate definition somewhere, then HI may not
1887 point to an indirect symbol. We will have reported an error
1888 to the user in that case. */
1890 if (hi
->root
.type
== bfd_link_hash_indirect
)
1892 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
1893 h
->ref_dynamic_nonweak
|= hi
->ref_dynamic_nonweak
;
1894 hi
->dynamic_def
|= h
->dynamic_def
;
1896 /* See if the new flags lead us to realize that the symbol
1902 if (! bfd_link_executable (info
)
1908 if (hi
->ref_regular
)
1918 /* This routine is used to export all defined symbols into the dynamic
1919 symbol table. It is called via elf_link_hash_traverse. */
1922 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1924 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
1926 /* Ignore indirect symbols. These are added by the versioning code. */
1927 if (h
->root
.type
== bfd_link_hash_indirect
)
1930 /* Ignore this if we won't export it. */
1931 if (!eif
->info
->export_dynamic
&& !h
->dynamic
)
1934 if (h
->dynindx
== -1
1935 && (h
->def_regular
|| h
->ref_regular
)
1936 && ! bfd_hide_sym_by_version (eif
->info
->version_info
,
1937 h
->root
.root
.string
))
1939 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1949 /* Look through the symbols which are defined in other shared
1950 libraries and referenced here. Update the list of version
1951 dependencies. This will be put into the .gnu.version_r section.
1952 This function is called via elf_link_hash_traverse. */
1955 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1958 struct elf_find_verdep_info
*rinfo
= (struct elf_find_verdep_info
*) data
;
1959 Elf_Internal_Verneed
*t
;
1960 Elf_Internal_Vernaux
*a
;
1963 /* We only care about symbols defined in shared objects with version
1968 || h
->verinfo
.verdef
== NULL
1969 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
1970 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
1973 /* See if we already know about this version. */
1974 for (t
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
1978 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1981 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1982 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1988 /* This is a new version. Add it to tree we are building. */
1993 t
= (Elf_Internal_Verneed
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
1996 rinfo
->failed
= TRUE
;
2000 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
2001 t
->vn_nextref
= elf_tdata (rinfo
->info
->output_bfd
)->verref
;
2002 elf_tdata (rinfo
->info
->output_bfd
)->verref
= t
;
2006 a
= (Elf_Internal_Vernaux
*) bfd_zalloc (rinfo
->info
->output_bfd
, amt
);
2009 rinfo
->failed
= TRUE
;
2013 /* Note that we are copying a string pointer here, and testing it
2014 above. If bfd_elf_string_from_elf_section is ever changed to
2015 discard the string data when low in memory, this will have to be
2017 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
2019 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
2020 a
->vna_nextptr
= t
->vn_auxptr
;
2022 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
2025 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
2032 /* Figure out appropriate versions for all the symbols. We may not
2033 have the version number script until we have read all of the input
2034 files, so until that point we don't know which symbols should be
2035 local. This function is called via elf_link_hash_traverse. */
2038 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
2040 struct elf_info_failed
*sinfo
;
2041 struct bfd_link_info
*info
;
2042 const struct elf_backend_data
*bed
;
2043 struct elf_info_failed eif
;
2047 sinfo
= (struct elf_info_failed
*) data
;
2050 /* Fix the symbol flags. */
2053 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
2056 sinfo
->failed
= TRUE
;
2060 /* We only need version numbers for symbols defined in regular
2062 if (!h
->def_regular
)
2065 bed
= get_elf_backend_data (info
->output_bfd
);
2066 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
2067 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
2069 struct bfd_elf_version_tree
*t
;
2072 if (*p
== ELF_VER_CHR
)
2075 /* If there is no version string, we can just return out. */
2079 /* Look for the version. If we find it, it is no longer weak. */
2080 for (t
= sinfo
->info
->version_info
; t
!= NULL
; t
= t
->next
)
2082 if (strcmp (t
->name
, p
) == 0)
2086 struct bfd_elf_version_expr
*d
;
2088 len
= p
- h
->root
.root
.string
;
2089 alc
= (char *) bfd_malloc (len
);
2092 sinfo
->failed
= TRUE
;
2095 memcpy (alc
, h
->root
.root
.string
, len
- 1);
2096 alc
[len
- 1] = '\0';
2097 if (alc
[len
- 2] == ELF_VER_CHR
)
2098 alc
[len
- 2] = '\0';
2100 h
->verinfo
.vertree
= t
;
2104 if (t
->globals
.list
!= NULL
)
2105 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
2107 /* See if there is anything to force this symbol to
2109 if (d
== NULL
&& t
->locals
.list
!= NULL
)
2111 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
2114 && ! info
->export_dynamic
)
2115 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2123 /* If we are building an application, we need to create a
2124 version node for this version. */
2125 if (t
== NULL
&& bfd_link_executable (info
))
2127 struct bfd_elf_version_tree
**pp
;
2130 /* If we aren't going to export this symbol, we don't need
2131 to worry about it. */
2132 if (h
->dynindx
== -1)
2136 t
= (struct bfd_elf_version_tree
*) bfd_zalloc (info
->output_bfd
, amt
);
2139 sinfo
->failed
= TRUE
;
2144 t
->name_indx
= (unsigned int) -1;
2148 /* Don't count anonymous version tag. */
2149 if (sinfo
->info
->version_info
!= NULL
2150 && sinfo
->info
->version_info
->vernum
== 0)
2152 for (pp
= &sinfo
->info
->version_info
;
2156 t
->vernum
= version_index
;
2160 h
->verinfo
.vertree
= t
;
2164 /* We could not find the version for a symbol when
2165 generating a shared archive. Return an error. */
2166 (*_bfd_error_handler
)
2167 (_("%B: version node not found for symbol %s"),
2168 info
->output_bfd
, h
->root
.root
.string
);
2169 bfd_set_error (bfd_error_bad_value
);
2170 sinfo
->failed
= TRUE
;
2175 /* If we don't have a version for this symbol, see if we can find
2177 if (h
->verinfo
.vertree
== NULL
&& sinfo
->info
->version_info
!= NULL
)
2182 = bfd_find_version_for_sym (sinfo
->info
->version_info
,
2183 h
->root
.root
.string
, &hide
);
2184 if (h
->verinfo
.vertree
!= NULL
&& hide
)
2185 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
2191 /* Read and swap the relocs from the section indicated by SHDR. This
2192 may be either a REL or a RELA section. The relocations are
2193 translated into RELA relocations and stored in INTERNAL_RELOCS,
2194 which should have already been allocated to contain enough space.
2195 The EXTERNAL_RELOCS are a buffer where the external form of the
2196 relocations should be stored.
2198 Returns FALSE if something goes wrong. */
2201 elf_link_read_relocs_from_section (bfd
*abfd
,
2203 Elf_Internal_Shdr
*shdr
,
2204 void *external_relocs
,
2205 Elf_Internal_Rela
*internal_relocs
)
2207 const struct elf_backend_data
*bed
;
2208 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
2209 const bfd_byte
*erela
;
2210 const bfd_byte
*erelaend
;
2211 Elf_Internal_Rela
*irela
;
2212 Elf_Internal_Shdr
*symtab_hdr
;
2215 /* Position ourselves at the start of the section. */
2216 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
2219 /* Read the relocations. */
2220 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
2223 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2224 nsyms
= NUM_SHDR_ENTRIES (symtab_hdr
);
2226 bed
= get_elf_backend_data (abfd
);
2228 /* Convert the external relocations to the internal format. */
2229 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2230 swap_in
= bed
->s
->swap_reloc_in
;
2231 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2232 swap_in
= bed
->s
->swap_reloca_in
;
2235 bfd_set_error (bfd_error_wrong_format
);
2239 erela
= (const bfd_byte
*) external_relocs
;
2240 erelaend
= erela
+ shdr
->sh_size
;
2241 irela
= internal_relocs
;
2242 while (erela
< erelaend
)
2246 (*swap_in
) (abfd
, erela
, irela
);
2247 r_symndx
= ELF32_R_SYM (irela
->r_info
);
2248 if (bed
->s
->arch_size
== 64)
2252 if ((size_t) r_symndx
>= nsyms
)
2254 (*_bfd_error_handler
)
2255 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2256 " for offset 0x%lx in section `%A'"),
2258 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2259 bfd_set_error (bfd_error_bad_value
);
2263 else if (r_symndx
!= STN_UNDEF
)
2265 (*_bfd_error_handler
)
2266 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2267 " when the object file has no symbol table"),
2269 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
2270 bfd_set_error (bfd_error_bad_value
);
2273 irela
+= bed
->s
->int_rels_per_ext_rel
;
2274 erela
+= shdr
->sh_entsize
;
2280 /* Read and swap the relocs for a section O. They may have been
2281 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2282 not NULL, they are used as buffers to read into. They are known to
2283 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2284 the return value is allocated using either malloc or bfd_alloc,
2285 according to the KEEP_MEMORY argument. If O has two relocation
2286 sections (both REL and RELA relocations), then the REL_HDR
2287 relocations will appear first in INTERNAL_RELOCS, followed by the
2288 RELA_HDR relocations. */
2291 _bfd_elf_link_read_relocs (bfd
*abfd
,
2293 void *external_relocs
,
2294 Elf_Internal_Rela
*internal_relocs
,
2295 bfd_boolean keep_memory
)
2297 void *alloc1
= NULL
;
2298 Elf_Internal_Rela
*alloc2
= NULL
;
2299 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2300 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
2301 Elf_Internal_Rela
*internal_rela_relocs
;
2303 if (esdo
->relocs
!= NULL
)
2304 return esdo
->relocs
;
2306 if (o
->reloc_count
== 0)
2309 if (internal_relocs
== NULL
)
2313 size
= o
->reloc_count
;
2314 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2316 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_alloc (abfd
, size
);
2318 internal_relocs
= alloc2
= (Elf_Internal_Rela
*) bfd_malloc (size
);
2319 if (internal_relocs
== NULL
)
2323 if (external_relocs
== NULL
)
2325 bfd_size_type size
= 0;
2328 size
+= esdo
->rel
.hdr
->sh_size
;
2330 size
+= esdo
->rela
.hdr
->sh_size
;
2332 alloc1
= bfd_malloc (size
);
2335 external_relocs
= alloc1
;
2338 internal_rela_relocs
= internal_relocs
;
2341 if (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rel
.hdr
,
2345 external_relocs
= (((bfd_byte
*) external_relocs
)
2346 + esdo
->rel
.hdr
->sh_size
);
2347 internal_rela_relocs
+= (NUM_SHDR_ENTRIES (esdo
->rel
.hdr
)
2348 * bed
->s
->int_rels_per_ext_rel
);
2352 && (!elf_link_read_relocs_from_section (abfd
, o
, esdo
->rela
.hdr
,
2354 internal_rela_relocs
)))
2357 /* Cache the results for next time, if we can. */
2359 esdo
->relocs
= internal_relocs
;
2364 /* Don't free alloc2, since if it was allocated we are passing it
2365 back (under the name of internal_relocs). */
2367 return internal_relocs
;
2375 bfd_release (abfd
, alloc2
);
2382 /* Compute the size of, and allocate space for, REL_HDR which is the
2383 section header for a section containing relocations for O. */
2386 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2387 struct bfd_elf_section_reloc_data
*reldata
)
2389 Elf_Internal_Shdr
*rel_hdr
= reldata
->hdr
;
2391 /* That allows us to calculate the size of the section. */
2392 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reldata
->count
;
2394 /* The contents field must last into write_object_contents, so we
2395 allocate it with bfd_alloc rather than malloc. Also since we
2396 cannot be sure that the contents will actually be filled in,
2397 we zero the allocated space. */
2398 rel_hdr
->contents
= (unsigned char *) bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2399 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2402 if (reldata
->hashes
== NULL
&& reldata
->count
)
2404 struct elf_link_hash_entry
**p
;
2406 p
= ((struct elf_link_hash_entry
**)
2407 bfd_zmalloc (reldata
->count
* sizeof (*p
)));
2411 reldata
->hashes
= p
;
2417 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2418 originated from the section given by INPUT_REL_HDR) to the
2422 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2423 asection
*input_section
,
2424 Elf_Internal_Shdr
*input_rel_hdr
,
2425 Elf_Internal_Rela
*internal_relocs
,
2426 struct elf_link_hash_entry
**rel_hash
2429 Elf_Internal_Rela
*irela
;
2430 Elf_Internal_Rela
*irelaend
;
2432 struct bfd_elf_section_reloc_data
*output_reldata
;
2433 asection
*output_section
;
2434 const struct elf_backend_data
*bed
;
2435 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2436 struct bfd_elf_section_data
*esdo
;
2438 output_section
= input_section
->output_section
;
2440 bed
= get_elf_backend_data (output_bfd
);
2441 esdo
= elf_section_data (output_section
);
2442 if (esdo
->rel
.hdr
&& esdo
->rel
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2444 output_reldata
= &esdo
->rel
;
2445 swap_out
= bed
->s
->swap_reloc_out
;
2447 else if (esdo
->rela
.hdr
2448 && esdo
->rela
.hdr
->sh_entsize
== input_rel_hdr
->sh_entsize
)
2450 output_reldata
= &esdo
->rela
;
2451 swap_out
= bed
->s
->swap_reloca_out
;
2455 (*_bfd_error_handler
)
2456 (_("%B: relocation size mismatch in %B section %A"),
2457 output_bfd
, input_section
->owner
, input_section
);
2458 bfd_set_error (bfd_error_wrong_format
);
2462 erel
= output_reldata
->hdr
->contents
;
2463 erel
+= output_reldata
->count
* input_rel_hdr
->sh_entsize
;
2464 irela
= internal_relocs
;
2465 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2466 * bed
->s
->int_rels_per_ext_rel
);
2467 while (irela
< irelaend
)
2469 (*swap_out
) (output_bfd
, irela
, erel
);
2470 irela
+= bed
->s
->int_rels_per_ext_rel
;
2471 erel
+= input_rel_hdr
->sh_entsize
;
2474 /* Bump the counter, so that we know where to add the next set of
2476 output_reldata
->count
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2481 /* Make weak undefined symbols in PIE dynamic. */
2484 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info
*info
,
2485 struct elf_link_hash_entry
*h
)
2487 if (bfd_link_pie (info
)
2489 && h
->root
.type
== bfd_link_hash_undefweak
)
2490 return bfd_elf_link_record_dynamic_symbol (info
, h
);
2495 /* Fix up the flags for a symbol. This handles various cases which
2496 can only be fixed after all the input files are seen. This is
2497 currently called by both adjust_dynamic_symbol and
2498 assign_sym_version, which is unnecessary but perhaps more robust in
2499 the face of future changes. */
2502 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2503 struct elf_info_failed
*eif
)
2505 const struct elf_backend_data
*bed
;
2507 /* If this symbol was mentioned in a non-ELF file, try to set
2508 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2509 permit a non-ELF file to correctly refer to a symbol defined in
2510 an ELF dynamic object. */
2513 while (h
->root
.type
== bfd_link_hash_indirect
)
2514 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2516 if (h
->root
.type
!= bfd_link_hash_defined
2517 && h
->root
.type
!= bfd_link_hash_defweak
)
2520 h
->ref_regular_nonweak
= 1;
2524 if (h
->root
.u
.def
.section
->owner
!= NULL
2525 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2526 == bfd_target_elf_flavour
))
2529 h
->ref_regular_nonweak
= 1;
2535 if (h
->dynindx
== -1
2539 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2548 /* Unfortunately, NON_ELF is only correct if the symbol
2549 was first seen in a non-ELF file. Fortunately, if the symbol
2550 was first seen in an ELF file, we're probably OK unless the
2551 symbol was defined in a non-ELF file. Catch that case here.
2552 FIXME: We're still in trouble if the symbol was first seen in
2553 a dynamic object, and then later in a non-ELF regular object. */
2554 if ((h
->root
.type
== bfd_link_hash_defined
2555 || h
->root
.type
== bfd_link_hash_defweak
)
2557 && (h
->root
.u
.def
.section
->owner
!= NULL
2558 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2559 != bfd_target_elf_flavour
)
2560 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2561 && !h
->def_dynamic
)))
2565 /* Backend specific symbol fixup. */
2566 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2567 if (bed
->elf_backend_fixup_symbol
2568 && !(*bed
->elf_backend_fixup_symbol
) (eif
->info
, h
))
2571 /* If this is a final link, and the symbol was defined as a common
2572 symbol in a regular object file, and there was no definition in
2573 any dynamic object, then the linker will have allocated space for
2574 the symbol in a common section but the DEF_REGULAR
2575 flag will not have been set. */
2576 if (h
->root
.type
== bfd_link_hash_defined
2580 && (h
->root
.u
.def
.section
->owner
->flags
& (DYNAMIC
| BFD_PLUGIN
)) == 0)
2583 /* If -Bsymbolic was used (which means to bind references to global
2584 symbols to the definition within the shared object), and this
2585 symbol was defined in a regular object, then it actually doesn't
2586 need a PLT entry. Likewise, if the symbol has non-default
2587 visibility. If the symbol has hidden or internal visibility, we
2588 will force it local. */
2590 && bfd_link_pic (eif
->info
)
2591 && is_elf_hash_table (eif
->info
->hash
)
2592 && (SYMBOLIC_BIND (eif
->info
, h
)
2593 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2596 bfd_boolean force_local
;
2598 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2599 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2600 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2603 /* If a weak undefined symbol has non-default visibility, we also
2604 hide it from the dynamic linker. */
2605 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2606 && h
->root
.type
== bfd_link_hash_undefweak
)
2607 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2609 /* If this is a weak defined symbol in a dynamic object, and we know
2610 the real definition in the dynamic object, copy interesting flags
2611 over to the real definition. */
2612 if (h
->u
.weakdef
!= NULL
)
2614 /* If the real definition is defined by a regular object file,
2615 don't do anything special. See the longer description in
2616 _bfd_elf_adjust_dynamic_symbol, below. */
2617 if (h
->u
.weakdef
->def_regular
)
2618 h
->u
.weakdef
= NULL
;
2621 struct elf_link_hash_entry
*weakdef
= h
->u
.weakdef
;
2623 while (h
->root
.type
== bfd_link_hash_indirect
)
2624 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2626 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2627 || h
->root
.type
== bfd_link_hash_defweak
);
2628 BFD_ASSERT (weakdef
->def_dynamic
);
2629 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2630 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2631 (*bed
->elf_backend_copy_indirect_symbol
) (eif
->info
, weakdef
, h
);
2638 /* Make the backend pick a good value for a dynamic symbol. This is
2639 called via elf_link_hash_traverse, and also calls itself
2643 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2645 struct elf_info_failed
*eif
= (struct elf_info_failed
*) data
;
2647 const struct elf_backend_data
*bed
;
2649 if (! is_elf_hash_table (eif
->info
->hash
))
2652 /* Ignore indirect symbols. These are added by the versioning code. */
2653 if (h
->root
.type
== bfd_link_hash_indirect
)
2656 /* Fix the symbol flags. */
2657 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2660 /* If this symbol does not require a PLT entry, and it is not
2661 defined by a dynamic object, or is not referenced by a regular
2662 object, ignore it. We do have to handle a weak defined symbol,
2663 even if no regular object refers to it, if we decided to add it
2664 to the dynamic symbol table. FIXME: Do we normally need to worry
2665 about symbols which are defined by one dynamic object and
2666 referenced by another one? */
2668 && h
->type
!= STT_GNU_IFUNC
2672 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2674 h
->plt
= elf_hash_table (eif
->info
)->init_plt_offset
;
2678 /* If we've already adjusted this symbol, don't do it again. This
2679 can happen via a recursive call. */
2680 if (h
->dynamic_adjusted
)
2683 /* Don't look at this symbol again. Note that we must set this
2684 after checking the above conditions, because we may look at a
2685 symbol once, decide not to do anything, and then get called
2686 recursively later after REF_REGULAR is set below. */
2687 h
->dynamic_adjusted
= 1;
2689 /* If this is a weak definition, and we know a real definition, and
2690 the real symbol is not itself defined by a regular object file,
2691 then get a good value for the real definition. We handle the
2692 real symbol first, for the convenience of the backend routine.
2694 Note that there is a confusing case here. If the real definition
2695 is defined by a regular object file, we don't get the real symbol
2696 from the dynamic object, but we do get the weak symbol. If the
2697 processor backend uses a COPY reloc, then if some routine in the
2698 dynamic object changes the real symbol, we will not see that
2699 change in the corresponding weak symbol. This is the way other
2700 ELF linkers work as well, and seems to be a result of the shared
2703 I will clarify this issue. Most SVR4 shared libraries define the
2704 variable _timezone and define timezone as a weak synonym. The
2705 tzset call changes _timezone. If you write
2706 extern int timezone;
2708 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2709 you might expect that, since timezone is a synonym for _timezone,
2710 the same number will print both times. However, if the processor
2711 backend uses a COPY reloc, then actually timezone will be copied
2712 into your process image, and, since you define _timezone
2713 yourself, _timezone will not. Thus timezone and _timezone will
2714 wind up at different memory locations. The tzset call will set
2715 _timezone, leaving timezone unchanged. */
2717 if (h
->u
.weakdef
!= NULL
)
2719 /* If we get to this point, there is an implicit reference to
2720 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2721 h
->u
.weakdef
->ref_regular
= 1;
2723 /* Ensure that the backend adjust_dynamic_symbol function sees
2724 H->U.WEAKDEF before H by recursively calling ourselves. */
2725 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2729 /* If a symbol has no type and no size and does not require a PLT
2730 entry, then we are probably about to do the wrong thing here: we
2731 are probably going to create a COPY reloc for an empty object.
2732 This case can arise when a shared object is built with assembly
2733 code, and the assembly code fails to set the symbol type. */
2735 && h
->type
== STT_NOTYPE
2737 (*_bfd_error_handler
)
2738 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2739 h
->root
.root
.string
);
2741 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2742 bed
= get_elf_backend_data (dynobj
);
2744 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2753 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2757 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info
*info
,
2758 struct elf_link_hash_entry
*h
,
2761 unsigned int power_of_two
;
2763 asection
*sec
= h
->root
.u
.def
.section
;
2765 /* The section aligment of definition is the maximum alignment
2766 requirement of symbols defined in the section. Since we don't
2767 know the symbol alignment requirement, we start with the
2768 maximum alignment and check low bits of the symbol address
2769 for the minimum alignment. */
2770 power_of_two
= bfd_get_section_alignment (sec
->owner
, sec
);
2771 mask
= ((bfd_vma
) 1 << power_of_two
) - 1;
2772 while ((h
->root
.u
.def
.value
& mask
) != 0)
2778 if (power_of_two
> bfd_get_section_alignment (dynbss
->owner
,
2781 /* Adjust the section alignment if needed. */
2782 if (! bfd_set_section_alignment (dynbss
->owner
, dynbss
,
2787 /* We make sure that the symbol will be aligned properly. */
2788 dynbss
->size
= BFD_ALIGN (dynbss
->size
, mask
+ 1);
2790 /* Define the symbol as being at this point in DYNBSS. */
2791 h
->root
.u
.def
.section
= dynbss
;
2792 h
->root
.u
.def
.value
= dynbss
->size
;
2794 /* Increment the size of DYNBSS to make room for the symbol. */
2795 dynbss
->size
+= h
->size
;
2797 /* No error if extern_protected_data is true. */
2798 if (h
->protected_def
2799 && (!info
->extern_protected_data
2800 || (info
->extern_protected_data
< 0
2801 && !get_elf_backend_data (dynbss
->owner
)->extern_protected_data
)))
2802 info
->callbacks
->einfo
2803 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2804 h
->root
.root
.string
);
2809 /* Adjust all external symbols pointing into SEC_MERGE sections
2810 to reflect the object merging within the sections. */
2813 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2817 if ((h
->root
.type
== bfd_link_hash_defined
2818 || h
->root
.type
== bfd_link_hash_defweak
)
2819 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2820 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
2822 bfd
*output_bfd
= (bfd
*) data
;
2824 h
->root
.u
.def
.value
=
2825 _bfd_merged_section_offset (output_bfd
,
2826 &h
->root
.u
.def
.section
,
2827 elf_section_data (sec
)->sec_info
,
2828 h
->root
.u
.def
.value
);
2834 /* Returns false if the symbol referred to by H should be considered
2835 to resolve local to the current module, and true if it should be
2836 considered to bind dynamically. */
2839 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2840 struct bfd_link_info
*info
,
2841 bfd_boolean not_local_protected
)
2843 bfd_boolean binding_stays_local_p
;
2844 const struct elf_backend_data
*bed
;
2845 struct elf_link_hash_table
*hash_table
;
2850 while (h
->root
.type
== bfd_link_hash_indirect
2851 || h
->root
.type
== bfd_link_hash_warning
)
2852 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2854 /* If it was forced local, then clearly it's not dynamic. */
2855 if (h
->dynindx
== -1)
2857 if (h
->forced_local
)
2860 /* Identify the cases where name binding rules say that a
2861 visible symbol resolves locally. */
2862 binding_stays_local_p
= (bfd_link_executable (info
)
2863 || SYMBOLIC_BIND (info
, h
));
2865 switch (ELF_ST_VISIBILITY (h
->other
))
2872 hash_table
= elf_hash_table (info
);
2873 if (!is_elf_hash_table (hash_table
))
2876 bed
= get_elf_backend_data (hash_table
->dynobj
);
2878 /* Proper resolution for function pointer equality may require
2879 that these symbols perhaps be resolved dynamically, even though
2880 we should be resolving them to the current module. */
2881 if (!not_local_protected
|| !bed
->is_function_type (h
->type
))
2882 binding_stays_local_p
= TRUE
;
2889 /* If it isn't defined locally, then clearly it's dynamic. */
2890 if (!h
->def_regular
&& !ELF_COMMON_DEF_P (h
))
2893 /* Otherwise, the symbol is dynamic if binding rules don't tell
2894 us that it remains local. */
2895 return !binding_stays_local_p
;
2898 /* Return true if the symbol referred to by H should be considered
2899 to resolve local to the current module, and false otherwise. Differs
2900 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2901 undefined symbols. The two functions are virtually identical except
2902 for the place where forced_local and dynindx == -1 are tested. If
2903 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2904 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2905 the symbol is local only for defined symbols.
2906 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2907 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2908 treatment of undefined weak symbols. For those that do not make
2909 undefined weak symbols dynamic, both functions may return false. */
2912 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2913 struct bfd_link_info
*info
,
2914 bfd_boolean local_protected
)
2916 const struct elf_backend_data
*bed
;
2917 struct elf_link_hash_table
*hash_table
;
2919 /* If it's a local sym, of course we resolve locally. */
2923 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2924 if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
2925 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
2928 /* Common symbols that become definitions don't get the DEF_REGULAR
2929 flag set, so test it first, and don't bail out. */
2930 if (ELF_COMMON_DEF_P (h
))
2932 /* If we don't have a definition in a regular file, then we can't
2933 resolve locally. The sym is either undefined or dynamic. */
2934 else if (!h
->def_regular
)
2937 /* Forced local symbols resolve locally. */
2938 if (h
->forced_local
)
2941 /* As do non-dynamic symbols. */
2942 if (h
->dynindx
== -1)
2945 /* At this point, we know the symbol is defined and dynamic. In an
2946 executable it must resolve locally, likewise when building symbolic
2947 shared libraries. */
2948 if (bfd_link_executable (info
) || SYMBOLIC_BIND (info
, h
))
2951 /* Now deal with defined dynamic symbols in shared libraries. Ones
2952 with default visibility might not resolve locally. */
2953 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2956 hash_table
= elf_hash_table (info
);
2957 if (!is_elf_hash_table (hash_table
))
2960 bed
= get_elf_backend_data (hash_table
->dynobj
);
2962 /* If extern_protected_data is false, STV_PROTECTED non-function
2963 symbols are local. */
2964 if ((!info
->extern_protected_data
2965 || (info
->extern_protected_data
< 0
2966 && !bed
->extern_protected_data
))
2967 && !bed
->is_function_type (h
->type
))
2970 /* Function pointer equality tests may require that STV_PROTECTED
2971 symbols be treated as dynamic symbols. If the address of a
2972 function not defined in an executable is set to that function's
2973 plt entry in the executable, then the address of the function in
2974 a shared library must also be the plt entry in the executable. */
2975 return local_protected
;
2978 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2979 aligned. Returns the first TLS output section. */
2981 struct bfd_section
*
2982 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2984 struct bfd_section
*sec
, *tls
;
2985 unsigned int align
= 0;
2987 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2988 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2992 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2993 if (sec
->alignment_power
> align
)
2994 align
= sec
->alignment_power
;
2996 elf_hash_table (info
)->tls_sec
= tls
;
2998 /* Ensure the alignment of the first section is the largest alignment,
2999 so that the tls segment starts aligned. */
3001 tls
->alignment_power
= align
;
3006 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3008 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
3009 Elf_Internal_Sym
*sym
)
3011 const struct elf_backend_data
*bed
;
3013 /* Local symbols do not count, but target specific ones might. */
3014 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
3015 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
3018 bed
= get_elf_backend_data (abfd
);
3019 /* Function symbols do not count. */
3020 if (bed
->is_function_type (ELF_ST_TYPE (sym
->st_info
)))
3023 /* If the section is undefined, then so is the symbol. */
3024 if (sym
->st_shndx
== SHN_UNDEF
)
3027 /* If the symbol is defined in the common section, then
3028 it is a common definition and so does not count. */
3029 if (bed
->common_definition (sym
))
3032 /* If the symbol is in a target specific section then we
3033 must rely upon the backend to tell us what it is. */
3034 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
3035 /* FIXME - this function is not coded yet:
3037 return _bfd_is_global_symbol_definition (abfd, sym);
3039 Instead for now assume that the definition is not global,
3040 Even if this is wrong, at least the linker will behave
3041 in the same way that it used to do. */
3047 /* Search the symbol table of the archive element of the archive ABFD
3048 whose archive map contains a mention of SYMDEF, and determine if
3049 the symbol is defined in this element. */
3051 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
3053 Elf_Internal_Shdr
* hdr
;
3054 bfd_size_type symcount
;
3055 bfd_size_type extsymcount
;
3056 bfd_size_type extsymoff
;
3057 Elf_Internal_Sym
*isymbuf
;
3058 Elf_Internal_Sym
*isym
;
3059 Elf_Internal_Sym
*isymend
;
3062 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
3066 /* Return FALSE if the object has been claimed by plugin. */
3067 if (abfd
->plugin_format
== bfd_plugin_yes
)
3070 if (! bfd_check_format (abfd
, bfd_object
))
3073 /* Select the appropriate symbol table. */
3074 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
3075 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3077 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3079 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
3081 /* The sh_info field of the symtab header tells us where the
3082 external symbols start. We don't care about the local symbols. */
3083 if (elf_bad_symtab (abfd
))
3085 extsymcount
= symcount
;
3090 extsymcount
= symcount
- hdr
->sh_info
;
3091 extsymoff
= hdr
->sh_info
;
3094 if (extsymcount
== 0)
3097 /* Read in the symbol table. */
3098 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3100 if (isymbuf
== NULL
)
3103 /* Scan the symbol table looking for SYMDEF. */
3105 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
3109 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3114 if (strcmp (name
, symdef
->name
) == 0)
3116 result
= is_global_data_symbol_definition (abfd
, isym
);
3126 /* Add an entry to the .dynamic table. */
3129 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
3133 struct elf_link_hash_table
*hash_table
;
3134 const struct elf_backend_data
*bed
;
3136 bfd_size_type newsize
;
3137 bfd_byte
*newcontents
;
3138 Elf_Internal_Dyn dyn
;
3140 hash_table
= elf_hash_table (info
);
3141 if (! is_elf_hash_table (hash_table
))
3144 bed
= get_elf_backend_data (hash_table
->dynobj
);
3145 s
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3146 BFD_ASSERT (s
!= NULL
);
3148 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
3149 newcontents
= (bfd_byte
*) bfd_realloc (s
->contents
, newsize
);
3150 if (newcontents
== NULL
)
3154 dyn
.d_un
.d_val
= val
;
3155 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
3158 s
->contents
= newcontents
;
3163 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3164 otherwise just check whether one already exists. Returns -1 on error,
3165 1 if a DT_NEEDED tag already exists, and 0 on success. */
3168 elf_add_dt_needed_tag (bfd
*abfd
,
3169 struct bfd_link_info
*info
,
3173 struct elf_link_hash_table
*hash_table
;
3174 bfd_size_type strindex
;
3176 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
3179 hash_table
= elf_hash_table (info
);
3180 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
3181 if (strindex
== (bfd_size_type
) -1)
3184 if (_bfd_elf_strtab_refcount (hash_table
->dynstr
, strindex
) != 1)
3187 const struct elf_backend_data
*bed
;
3190 bed
= get_elf_backend_data (hash_table
->dynobj
);
3191 sdyn
= bfd_get_linker_section (hash_table
->dynobj
, ".dynamic");
3193 for (extdyn
= sdyn
->contents
;
3194 extdyn
< sdyn
->contents
+ sdyn
->size
;
3195 extdyn
+= bed
->s
->sizeof_dyn
)
3197 Elf_Internal_Dyn dyn
;
3199 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
3200 if (dyn
.d_tag
== DT_NEEDED
3201 && dyn
.d_un
.d_val
== strindex
)
3203 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3211 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
3214 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
3218 /* We were just checking for existence of the tag. */
3219 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
3225 on_needed_list (const char *soname
, struct bfd_link_needed_list
*needed
)
3227 for (; needed
!= NULL
; needed
= needed
->next
)
3228 if ((elf_dyn_lib_class (needed
->by
) & DYN_AS_NEEDED
) == 0
3229 && strcmp (soname
, needed
->name
) == 0)
3235 /* Sort symbol by value, section, and size. */
3237 elf_sort_symbol (const void *arg1
, const void *arg2
)
3239 const struct elf_link_hash_entry
*h1
;
3240 const struct elf_link_hash_entry
*h2
;
3241 bfd_signed_vma vdiff
;
3243 h1
= *(const struct elf_link_hash_entry
**) arg1
;
3244 h2
= *(const struct elf_link_hash_entry
**) arg2
;
3245 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
3247 return vdiff
> 0 ? 1 : -1;
3250 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
3252 return sdiff
> 0 ? 1 : -1;
3254 vdiff
= h1
->size
- h2
->size
;
3255 return vdiff
== 0 ? 0 : vdiff
> 0 ? 1 : -1;
3258 /* This function is used to adjust offsets into .dynstr for
3259 dynamic symbols. This is called via elf_link_hash_traverse. */
3262 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
3264 struct elf_strtab_hash
*dynstr
= (struct elf_strtab_hash
*) data
;
3266 if (h
->dynindx
!= -1)
3267 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
3271 /* Assign string offsets in .dynstr, update all structures referencing
3275 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3277 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3278 struct elf_link_local_dynamic_entry
*entry
;
3279 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3280 bfd
*dynobj
= hash_table
->dynobj
;
3283 const struct elf_backend_data
*bed
;
3286 _bfd_elf_strtab_finalize (dynstr
);
3287 size
= _bfd_elf_strtab_size (dynstr
);
3289 bed
= get_elf_backend_data (dynobj
);
3290 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3291 BFD_ASSERT (sdyn
!= NULL
);
3293 /* Update all .dynamic entries referencing .dynstr strings. */
3294 for (extdyn
= sdyn
->contents
;
3295 extdyn
< sdyn
->contents
+ sdyn
->size
;
3296 extdyn
+= bed
->s
->sizeof_dyn
)
3298 Elf_Internal_Dyn dyn
;
3300 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3304 dyn
.d_un
.d_val
= size
;
3314 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3319 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3322 /* Now update local dynamic symbols. */
3323 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3324 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3325 entry
->isym
.st_name
);
3327 /* And the rest of dynamic symbols. */
3328 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3330 /* Adjust version definitions. */
3331 if (elf_tdata (output_bfd
)->cverdefs
)
3336 Elf_Internal_Verdef def
;
3337 Elf_Internal_Verdaux defaux
;
3339 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
3343 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3345 p
+= sizeof (Elf_External_Verdef
);
3346 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3348 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3350 _bfd_elf_swap_verdaux_in (output_bfd
,
3351 (Elf_External_Verdaux
*) p
, &defaux
);
3352 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3354 _bfd_elf_swap_verdaux_out (output_bfd
,
3355 &defaux
, (Elf_External_Verdaux
*) p
);
3356 p
+= sizeof (Elf_External_Verdaux
);
3359 while (def
.vd_next
);
3362 /* Adjust version references. */
3363 if (elf_tdata (output_bfd
)->verref
)
3368 Elf_Internal_Verneed need
;
3369 Elf_Internal_Vernaux needaux
;
3371 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
3375 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3377 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3378 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3379 (Elf_External_Verneed
*) p
);
3380 p
+= sizeof (Elf_External_Verneed
);
3381 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3383 _bfd_elf_swap_vernaux_in (output_bfd
,
3384 (Elf_External_Vernaux
*) p
, &needaux
);
3385 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3387 _bfd_elf_swap_vernaux_out (output_bfd
,
3389 (Elf_External_Vernaux
*) p
);
3390 p
+= sizeof (Elf_External_Vernaux
);
3393 while (need
.vn_next
);
3399 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3400 The default is to only match when the INPUT and OUTPUT are exactly
3404 _bfd_elf_default_relocs_compatible (const bfd_target
*input
,
3405 const bfd_target
*output
)
3407 return input
== output
;
3410 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3411 This version is used when different targets for the same architecture
3412 are virtually identical. */
3415 _bfd_elf_relocs_compatible (const bfd_target
*input
,
3416 const bfd_target
*output
)
3418 const struct elf_backend_data
*obed
, *ibed
;
3420 if (input
== output
)
3423 ibed
= xvec_get_elf_backend_data (input
);
3424 obed
= xvec_get_elf_backend_data (output
);
3426 if (ibed
->arch
!= obed
->arch
)
3429 /* If both backends are using this function, deem them compatible. */
3430 return ibed
->relocs_compatible
== obed
->relocs_compatible
;
3433 /* Make a special call to the linker "notice" function to tell it that
3434 we are about to handle an as-needed lib, or have finished
3435 processing the lib. */
3438 _bfd_elf_notice_as_needed (bfd
*ibfd
,
3439 struct bfd_link_info
*info
,
3440 enum notice_asneeded_action act
)
3442 return (*info
->callbacks
->notice
) (info
, NULL
, NULL
, ibfd
, NULL
, act
, 0);
3445 /* Add symbols from an ELF object file to the linker hash table. */
3448 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3450 Elf_Internal_Ehdr
*ehdr
;
3451 Elf_Internal_Shdr
*hdr
;
3452 bfd_size_type symcount
;
3453 bfd_size_type extsymcount
;
3454 bfd_size_type extsymoff
;
3455 struct elf_link_hash_entry
**sym_hash
;
3456 bfd_boolean dynamic
;
3457 Elf_External_Versym
*extversym
= NULL
;
3458 Elf_External_Versym
*ever
;
3459 struct elf_link_hash_entry
*weaks
;
3460 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3461 bfd_size_type nondeflt_vers_cnt
= 0;
3462 Elf_Internal_Sym
*isymbuf
= NULL
;
3463 Elf_Internal_Sym
*isym
;
3464 Elf_Internal_Sym
*isymend
;
3465 const struct elf_backend_data
*bed
;
3466 bfd_boolean add_needed
;
3467 struct elf_link_hash_table
*htab
;
3469 void *alloc_mark
= NULL
;
3470 struct bfd_hash_entry
**old_table
= NULL
;
3471 unsigned int old_size
= 0;
3472 unsigned int old_count
= 0;
3473 void *old_tab
= NULL
;
3475 struct bfd_link_hash_entry
*old_undefs
= NULL
;
3476 struct bfd_link_hash_entry
*old_undefs_tail
= NULL
;
3477 long old_dynsymcount
= 0;
3478 bfd_size_type old_dynstr_size
= 0;
3481 bfd_boolean just_syms
;
3483 htab
= elf_hash_table (info
);
3484 bed
= get_elf_backend_data (abfd
);
3486 if ((abfd
->flags
& DYNAMIC
) == 0)
3492 /* You can't use -r against a dynamic object. Also, there's no
3493 hope of using a dynamic object which does not exactly match
3494 the format of the output file. */
3495 if (bfd_link_relocatable (info
)
3496 || !is_elf_hash_table (htab
)
3497 || info
->output_bfd
->xvec
!= abfd
->xvec
)
3499 if (bfd_link_relocatable (info
))
3500 bfd_set_error (bfd_error_invalid_operation
);
3502 bfd_set_error (bfd_error_wrong_format
);
3507 ehdr
= elf_elfheader (abfd
);
3508 if (info
->warn_alternate_em
3509 && bed
->elf_machine_code
!= ehdr
->e_machine
3510 && ((bed
->elf_machine_alt1
!= 0
3511 && ehdr
->e_machine
== bed
->elf_machine_alt1
)
3512 || (bed
->elf_machine_alt2
!= 0
3513 && ehdr
->e_machine
== bed
->elf_machine_alt2
)))
3514 info
->callbacks
->einfo
3515 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3516 ehdr
->e_machine
, abfd
, bed
->elf_machine_code
);
3518 /* As a GNU extension, any input sections which are named
3519 .gnu.warning.SYMBOL are treated as warning symbols for the given
3520 symbol. This differs from .gnu.warning sections, which generate
3521 warnings when they are included in an output file. */
3522 /* PR 12761: Also generate this warning when building shared libraries. */
3523 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3527 name
= bfd_get_section_name (abfd
, s
);
3528 if (CONST_STRNEQ (name
, ".gnu.warning."))
3533 name
+= sizeof ".gnu.warning." - 1;
3535 /* If this is a shared object, then look up the symbol
3536 in the hash table. If it is there, and it is already
3537 been defined, then we will not be using the entry
3538 from this shared object, so we don't need to warn.
3539 FIXME: If we see the definition in a regular object
3540 later on, we will warn, but we shouldn't. The only
3541 fix is to keep track of what warnings we are supposed
3542 to emit, and then handle them all at the end of the
3546 struct elf_link_hash_entry
*h
;
3548 h
= elf_link_hash_lookup (htab
, name
, FALSE
, FALSE
, TRUE
);
3550 /* FIXME: What about bfd_link_hash_common? */
3552 && (h
->root
.type
== bfd_link_hash_defined
3553 || h
->root
.type
== bfd_link_hash_defweak
))
3558 msg
= (char *) bfd_alloc (abfd
, sz
+ 1);
3562 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3567 if (! (_bfd_generic_link_add_one_symbol
3568 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3569 FALSE
, bed
->collect
, NULL
)))
3572 if (bfd_link_executable (info
))
3574 /* Clobber the section size so that the warning does
3575 not get copied into the output file. */
3578 /* Also set SEC_EXCLUDE, so that symbols defined in
3579 the warning section don't get copied to the output. */
3580 s
->flags
|= SEC_EXCLUDE
;
3585 just_syms
= ((s
= abfd
->sections
) != NULL
3586 && s
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
);
3591 /* If we are creating a shared library, create all the dynamic
3592 sections immediately. We need to attach them to something,
3593 so we attach them to this BFD, provided it is the right
3594 format and is not from ld --just-symbols. FIXME: If there
3595 are no input BFD's of the same format as the output, we can't
3596 make a shared library. */
3598 && bfd_link_pic (info
)
3599 && is_elf_hash_table (htab
)
3600 && info
->output_bfd
->xvec
== abfd
->xvec
3601 && !htab
->dynamic_sections_created
)
3603 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3607 else if (!is_elf_hash_table (htab
))
3611 const char *soname
= NULL
;
3613 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3616 /* ld --just-symbols and dynamic objects don't mix very well.
3617 ld shouldn't allow it. */
3621 /* If this dynamic lib was specified on the command line with
3622 --as-needed in effect, then we don't want to add a DT_NEEDED
3623 tag unless the lib is actually used. Similary for libs brought
3624 in by another lib's DT_NEEDED. When --no-add-needed is used
3625 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3626 any dynamic library in DT_NEEDED tags in the dynamic lib at
3628 add_needed
= (elf_dyn_lib_class (abfd
)
3629 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3630 | DYN_NO_NEEDED
)) == 0;
3632 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3637 unsigned int elfsec
;
3638 unsigned long shlink
;
3640 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3647 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3648 if (elfsec
== SHN_BAD
)
3649 goto error_free_dyn
;
3650 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3652 for (extdyn
= dynbuf
;
3653 extdyn
< dynbuf
+ s
->size
;
3654 extdyn
+= bed
->s
->sizeof_dyn
)
3656 Elf_Internal_Dyn dyn
;
3658 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3659 if (dyn
.d_tag
== DT_SONAME
)
3661 unsigned int tagv
= dyn
.d_un
.d_val
;
3662 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3664 goto error_free_dyn
;
3666 if (dyn
.d_tag
== DT_NEEDED
)
3668 struct bfd_link_needed_list
*n
, **pn
;
3670 unsigned int tagv
= dyn
.d_un
.d_val
;
3672 amt
= sizeof (struct bfd_link_needed_list
);
3673 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3674 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3675 if (n
== NULL
|| fnm
== NULL
)
3676 goto error_free_dyn
;
3677 amt
= strlen (fnm
) + 1;
3678 anm
= (char *) bfd_alloc (abfd
, amt
);
3680 goto error_free_dyn
;
3681 memcpy (anm
, fnm
, amt
);
3685 for (pn
= &htab
->needed
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3689 if (dyn
.d_tag
== DT_RUNPATH
)
3691 struct bfd_link_needed_list
*n
, **pn
;
3693 unsigned int tagv
= dyn
.d_un
.d_val
;
3695 amt
= sizeof (struct bfd_link_needed_list
);
3696 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3697 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3698 if (n
== NULL
|| fnm
== NULL
)
3699 goto error_free_dyn
;
3700 amt
= strlen (fnm
) + 1;
3701 anm
= (char *) bfd_alloc (abfd
, amt
);
3703 goto error_free_dyn
;
3704 memcpy (anm
, fnm
, amt
);
3708 for (pn
= & runpath
;
3714 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3715 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3717 struct bfd_link_needed_list
*n
, **pn
;
3719 unsigned int tagv
= dyn
.d_un
.d_val
;
3721 amt
= sizeof (struct bfd_link_needed_list
);
3722 n
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
3723 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3724 if (n
== NULL
|| fnm
== NULL
)
3725 goto error_free_dyn
;
3726 amt
= strlen (fnm
) + 1;
3727 anm
= (char *) bfd_alloc (abfd
, amt
);
3729 goto error_free_dyn
;
3730 memcpy (anm
, fnm
, amt
);
3740 if (dyn
.d_tag
== DT_AUDIT
)
3742 unsigned int tagv
= dyn
.d_un
.d_val
;
3743 audit
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3750 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3751 frees all more recently bfd_alloc'd blocks as well. */
3757 struct bfd_link_needed_list
**pn
;
3758 for (pn
= &htab
->runpath
; *pn
!= NULL
; pn
= &(*pn
)->next
)
3763 /* We do not want to include any of the sections in a dynamic
3764 object in the output file. We hack by simply clobbering the
3765 list of sections in the BFD. This could be handled more
3766 cleanly by, say, a new section flag; the existing
3767 SEC_NEVER_LOAD flag is not the one we want, because that one
3768 still implies that the section takes up space in the output
3770 bfd_section_list_clear (abfd
);
3772 /* Find the name to use in a DT_NEEDED entry that refers to this
3773 object. If the object has a DT_SONAME entry, we use it.
3774 Otherwise, if the generic linker stuck something in
3775 elf_dt_name, we use that. Otherwise, we just use the file
3777 if (soname
== NULL
|| *soname
== '\0')
3779 soname
= elf_dt_name (abfd
);
3780 if (soname
== NULL
|| *soname
== '\0')
3781 soname
= bfd_get_filename (abfd
);
3784 /* Save the SONAME because sometimes the linker emulation code
3785 will need to know it. */
3786 elf_dt_name (abfd
) = soname
;
3788 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3792 /* If we have already included this dynamic object in the
3793 link, just ignore it. There is no reason to include a
3794 particular dynamic object more than once. */
3798 /* Save the DT_AUDIT entry for the linker emulation code. */
3799 elf_dt_audit (abfd
) = audit
;
3802 /* If this is a dynamic object, we always link against the .dynsym
3803 symbol table, not the .symtab symbol table. The dynamic linker
3804 will only see the .dynsym symbol table, so there is no reason to
3805 look at .symtab for a dynamic object. */
3807 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3808 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3810 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3812 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3814 /* The sh_info field of the symtab header tells us where the
3815 external symbols start. We don't care about the local symbols at
3817 if (elf_bad_symtab (abfd
))
3819 extsymcount
= symcount
;
3824 extsymcount
= symcount
- hdr
->sh_info
;
3825 extsymoff
= hdr
->sh_info
;
3828 sym_hash
= elf_sym_hashes (abfd
);
3829 if (extsymcount
!= 0)
3831 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3833 if (isymbuf
== NULL
)
3836 if (sym_hash
== NULL
)
3838 /* We store a pointer to the hash table entry for each
3840 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3841 sym_hash
= (struct elf_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
3842 if (sym_hash
== NULL
)
3843 goto error_free_sym
;
3844 elf_sym_hashes (abfd
) = sym_hash
;
3850 /* Read in any version definitions. */
3851 if (!_bfd_elf_slurp_version_tables (abfd
,
3852 info
->default_imported_symver
))
3853 goto error_free_sym
;
3855 /* Read in the symbol versions, but don't bother to convert them
3856 to internal format. */
3857 if (elf_dynversym (abfd
) != 0)
3859 Elf_Internal_Shdr
*versymhdr
;
3861 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3862 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
3863 if (extversym
== NULL
)
3864 goto error_free_sym
;
3865 amt
= versymhdr
->sh_size
;
3866 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3867 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3868 goto error_free_vers
;
3872 /* If we are loading an as-needed shared lib, save the symbol table
3873 state before we start adding symbols. If the lib turns out
3874 to be unneeded, restore the state. */
3875 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
3880 for (entsize
= 0, i
= 0; i
< htab
->root
.table
.size
; i
++)
3882 struct bfd_hash_entry
*p
;
3883 struct elf_link_hash_entry
*h
;
3885 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3887 h
= (struct elf_link_hash_entry
*) p
;
3888 entsize
+= htab
->root
.table
.entsize
;
3889 if (h
->root
.type
== bfd_link_hash_warning
)
3890 entsize
+= htab
->root
.table
.entsize
;
3894 tabsize
= htab
->root
.table
.size
* sizeof (struct bfd_hash_entry
*);
3895 old_tab
= bfd_malloc (tabsize
+ entsize
);
3896 if (old_tab
== NULL
)
3897 goto error_free_vers
;
3899 /* Remember the current objalloc pointer, so that all mem for
3900 symbols added can later be reclaimed. */
3901 alloc_mark
= bfd_hash_allocate (&htab
->root
.table
, 1);
3902 if (alloc_mark
== NULL
)
3903 goto error_free_vers
;
3905 /* Make a special call to the linker "notice" function to
3906 tell it that we are about to handle an as-needed lib. */
3907 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_as_needed
))
3908 goto error_free_vers
;
3910 /* Clone the symbol table. Remember some pointers into the
3911 symbol table, and dynamic symbol count. */
3912 old_ent
= (char *) old_tab
+ tabsize
;
3913 memcpy (old_tab
, htab
->root
.table
.table
, tabsize
);
3914 old_undefs
= htab
->root
.undefs
;
3915 old_undefs_tail
= htab
->root
.undefs_tail
;
3916 old_table
= htab
->root
.table
.table
;
3917 old_size
= htab
->root
.table
.size
;
3918 old_count
= htab
->root
.table
.count
;
3919 old_dynsymcount
= htab
->dynsymcount
;
3920 old_dynstr_size
= _bfd_elf_strtab_size (htab
->dynstr
);
3922 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
3924 struct bfd_hash_entry
*p
;
3925 struct elf_link_hash_entry
*h
;
3927 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
3929 memcpy (old_ent
, p
, htab
->root
.table
.entsize
);
3930 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3931 h
= (struct elf_link_hash_entry
*) p
;
3932 if (h
->root
.type
== bfd_link_hash_warning
)
3934 memcpy (old_ent
, h
->root
.u
.i
.link
, htab
->root
.table
.entsize
);
3935 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
3942 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3943 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3945 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3949 asection
*sec
, *new_sec
;
3952 struct elf_link_hash_entry
*h
;
3953 struct elf_link_hash_entry
*hi
;
3954 bfd_boolean definition
;
3955 bfd_boolean size_change_ok
;
3956 bfd_boolean type_change_ok
;
3957 bfd_boolean new_weakdef
;
3958 bfd_boolean new_weak
;
3959 bfd_boolean old_weak
;
3960 bfd_boolean override
;
3962 unsigned int old_alignment
;
3964 bfd_boolean matched
;
3968 flags
= BSF_NO_FLAGS
;
3970 value
= isym
->st_value
;
3971 common
= bed
->common_definition (isym
);
3973 bind
= ELF_ST_BIND (isym
->st_info
);
3977 /* This should be impossible, since ELF requires that all
3978 global symbols follow all local symbols, and that sh_info
3979 point to the first global symbol. Unfortunately, Irix 5
3984 if (isym
->st_shndx
!= SHN_UNDEF
&& !common
)
3992 case STB_GNU_UNIQUE
:
3993 flags
= BSF_GNU_UNIQUE
;
3997 /* Leave it up to the processor backend. */
4001 if (isym
->st_shndx
== SHN_UNDEF
)
4002 sec
= bfd_und_section_ptr
;
4003 else if (isym
->st_shndx
== SHN_ABS
)
4004 sec
= bfd_abs_section_ptr
;
4005 else if (isym
->st_shndx
== SHN_COMMON
)
4007 sec
= bfd_com_section_ptr
;
4008 /* What ELF calls the size we call the value. What ELF
4009 calls the value we call the alignment. */
4010 value
= isym
->st_size
;
4014 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
4016 sec
= bfd_abs_section_ptr
;
4017 else if (discarded_section (sec
))
4019 /* Symbols from discarded section are undefined. We keep
4021 sec
= bfd_und_section_ptr
;
4022 isym
->st_shndx
= SHN_UNDEF
;
4024 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
4028 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4031 goto error_free_vers
;
4033 if (isym
->st_shndx
== SHN_COMMON
4034 && (abfd
->flags
& BFD_PLUGIN
) != 0)
4036 asection
*xc
= bfd_get_section_by_name (abfd
, "COMMON");
4040 flagword sflags
= (SEC_ALLOC
| SEC_IS_COMMON
| SEC_KEEP
4042 xc
= bfd_make_section_with_flags (abfd
, "COMMON", sflags
);
4044 goto error_free_vers
;
4048 else if (isym
->st_shndx
== SHN_COMMON
4049 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4050 && !bfd_link_relocatable (info
))
4052 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
4056 flagword sflags
= (SEC_ALLOC
| SEC_THREAD_LOCAL
| SEC_IS_COMMON
4057 | SEC_LINKER_CREATED
);
4058 tcomm
= bfd_make_section_with_flags (abfd
, ".tcommon", sflags
);
4060 goto error_free_vers
;
4064 else if (bed
->elf_add_symbol_hook
)
4066 if (! (*bed
->elf_add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
,
4068 goto error_free_vers
;
4070 /* The hook function sets the name to NULL if this symbol
4071 should be skipped for some reason. */
4076 /* Sanity check that all possibilities were handled. */
4079 bfd_set_error (bfd_error_bad_value
);
4080 goto error_free_vers
;
4083 /* Silently discard TLS symbols from --just-syms. There's
4084 no way to combine a static TLS block with a new TLS block
4085 for this executable. */
4086 if (ELF_ST_TYPE (isym
->st_info
) == STT_TLS
4087 && sec
->sec_info_type
== SEC_INFO_TYPE_JUST_SYMS
)
4090 if (bfd_is_und_section (sec
)
4091 || bfd_is_com_section (sec
))
4096 size_change_ok
= FALSE
;
4097 type_change_ok
= bed
->type_change_ok
;
4104 if (is_elf_hash_table (htab
))
4106 Elf_Internal_Versym iver
;
4107 unsigned int vernum
= 0;
4112 if (info
->default_imported_symver
)
4113 /* Use the default symbol version created earlier. */
4114 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
4119 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
4121 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
4123 /* If this is a hidden symbol, or if it is not version
4124 1, we append the version name to the symbol name.
4125 However, we do not modify a non-hidden absolute symbol
4126 if it is not a function, because it might be the version
4127 symbol itself. FIXME: What if it isn't? */
4128 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
4130 && (!bfd_is_abs_section (sec
)
4131 || bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
)))))
4134 size_t namelen
, verlen
, newlen
;
4137 if (isym
->st_shndx
!= SHN_UNDEF
)
4139 if (vernum
> elf_tdata (abfd
)->cverdefs
)
4141 else if (vernum
> 1)
4143 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
4149 (*_bfd_error_handler
)
4150 (_("%B: %s: invalid version %u (max %d)"),
4152 elf_tdata (abfd
)->cverdefs
);
4153 bfd_set_error (bfd_error_bad_value
);
4154 goto error_free_vers
;
4159 /* We cannot simply test for the number of
4160 entries in the VERNEED section since the
4161 numbers for the needed versions do not start
4163 Elf_Internal_Verneed
*t
;
4166 for (t
= elf_tdata (abfd
)->verref
;
4170 Elf_Internal_Vernaux
*a
;
4172 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
4174 if (a
->vna_other
== vernum
)
4176 verstr
= a
->vna_nodename
;
4185 (*_bfd_error_handler
)
4186 (_("%B: %s: invalid needed version %d"),
4187 abfd
, name
, vernum
);
4188 bfd_set_error (bfd_error_bad_value
);
4189 goto error_free_vers
;
4193 namelen
= strlen (name
);
4194 verlen
= strlen (verstr
);
4195 newlen
= namelen
+ verlen
+ 2;
4196 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4197 && isym
->st_shndx
!= SHN_UNDEF
)
4200 newname
= (char *) bfd_hash_allocate (&htab
->root
.table
, newlen
);
4201 if (newname
== NULL
)
4202 goto error_free_vers
;
4203 memcpy (newname
, name
, namelen
);
4204 p
= newname
+ namelen
;
4206 /* If this is a defined non-hidden version symbol,
4207 we add another @ to the name. This indicates the
4208 default version of the symbol. */
4209 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
4210 && isym
->st_shndx
!= SHN_UNDEF
)
4212 memcpy (p
, verstr
, verlen
+ 1);
4217 /* If this symbol has default visibility and the user has
4218 requested we not re-export it, then mark it as hidden. */
4222 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
4223 isym
->st_other
= (STV_HIDDEN
4224 | (isym
->st_other
& ~ELF_ST_VISIBILITY (-1)));
4226 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
4227 sym_hash
, &old_bfd
, &old_weak
,
4228 &old_alignment
, &skip
, &override
,
4229 &type_change_ok
, &size_change_ok
,
4231 goto error_free_vers
;
4236 /* Override a definition only if the new symbol matches the
4238 if (override
&& matched
)
4242 while (h
->root
.type
== bfd_link_hash_indirect
4243 || h
->root
.type
== bfd_link_hash_warning
)
4244 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4246 if (elf_tdata (abfd
)->verdef
!= NULL
4249 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
4252 if (! (_bfd_generic_link_add_one_symbol
4253 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, bed
->collect
,
4254 (struct bfd_link_hash_entry
**) sym_hash
)))
4255 goto error_free_vers
;
4258 /* We need to make sure that indirect symbol dynamic flags are
4261 while (h
->root
.type
== bfd_link_hash_indirect
4262 || h
->root
.type
== bfd_link_hash_warning
)
4263 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4267 new_weak
= (flags
& BSF_WEAK
) != 0;
4268 new_weakdef
= FALSE
;
4272 && !bed
->is_function_type (ELF_ST_TYPE (isym
->st_info
))
4273 && is_elf_hash_table (htab
)
4274 && h
->u
.weakdef
== NULL
)
4276 /* Keep a list of all weak defined non function symbols from
4277 a dynamic object, using the weakdef field. Later in this
4278 function we will set the weakdef field to the correct
4279 value. We only put non-function symbols from dynamic
4280 objects on this list, because that happens to be the only
4281 time we need to know the normal symbol corresponding to a
4282 weak symbol, and the information is time consuming to
4283 figure out. If the weakdef field is not already NULL,
4284 then this symbol was already defined by some previous
4285 dynamic object, and we will be using that previous
4286 definition anyhow. */
4288 h
->u
.weakdef
= weaks
;
4293 /* Set the alignment of a common symbol. */
4294 if ((common
|| bfd_is_com_section (sec
))
4295 && h
->root
.type
== bfd_link_hash_common
)
4300 align
= bfd_log2 (isym
->st_value
);
4303 /* The new symbol is a common symbol in a shared object.
4304 We need to get the alignment from the section. */
4305 align
= new_sec
->alignment_power
;
4307 if (align
> old_alignment
)
4308 h
->root
.u
.c
.p
->alignment_power
= align
;
4310 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
4313 if (is_elf_hash_table (htab
))
4315 /* Set a flag in the hash table entry indicating the type of
4316 reference or definition we just found. A dynamic symbol
4317 is one which is referenced or defined by both a regular
4318 object and a shared object. */
4319 bfd_boolean dynsym
= FALSE
;
4321 /* Plugin symbols aren't normal. Don't set def_regular or
4322 ref_regular for them, or make them dynamic. */
4323 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4330 if (bind
!= STB_WEAK
)
4331 h
->ref_regular_nonweak
= 1;
4343 /* If the indirect symbol has been forced local, don't
4344 make the real symbol dynamic. */
4345 if ((h
== hi
|| !hi
->forced_local
)
4346 && (bfd_link_dll (info
)
4356 hi
->ref_dynamic
= 1;
4361 hi
->def_dynamic
= 1;
4364 /* If the indirect symbol has been forced local, don't
4365 make the real symbol dynamic. */
4366 if ((h
== hi
|| !hi
->forced_local
)
4369 || (h
->u
.weakdef
!= NULL
4371 && h
->u
.weakdef
->dynindx
!= -1)))
4375 /* Check to see if we need to add an indirect symbol for
4376 the default name. */
4378 || (!override
&& h
->root
.type
== bfd_link_hash_common
))
4379 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4380 sec
, value
, &old_bfd
, &dynsym
))
4381 goto error_free_vers
;
4383 /* Check the alignment when a common symbol is involved. This
4384 can change when a common symbol is overridden by a normal
4385 definition or a common symbol is ignored due to the old
4386 normal definition. We need to make sure the maximum
4387 alignment is maintained. */
4388 if ((old_alignment
|| common
)
4389 && h
->root
.type
!= bfd_link_hash_common
)
4391 unsigned int common_align
;
4392 unsigned int normal_align
;
4393 unsigned int symbol_align
;
4397 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
4398 || h
->root
.type
== bfd_link_hash_defweak
);
4400 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
4401 if (h
->root
.u
.def
.section
->owner
!= NULL
4402 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
4404 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
4405 if (normal_align
> symbol_align
)
4406 normal_align
= symbol_align
;
4409 normal_align
= symbol_align
;
4413 common_align
= old_alignment
;
4414 common_bfd
= old_bfd
;
4419 common_align
= bfd_log2 (isym
->st_value
);
4421 normal_bfd
= old_bfd
;
4424 if (normal_align
< common_align
)
4426 /* PR binutils/2735 */
4427 if (normal_bfd
== NULL
)
4428 (*_bfd_error_handler
)
4429 (_("Warning: alignment %u of common symbol `%s' in %B is"
4430 " greater than the alignment (%u) of its section %A"),
4431 common_bfd
, h
->root
.u
.def
.section
,
4432 1 << common_align
, name
, 1 << normal_align
);
4434 (*_bfd_error_handler
)
4435 (_("Warning: alignment %u of symbol `%s' in %B"
4436 " is smaller than %u in %B"),
4437 normal_bfd
, common_bfd
,
4438 1 << normal_align
, name
, 1 << common_align
);
4442 /* Remember the symbol size if it isn't undefined. */
4443 if (isym
->st_size
!= 0
4444 && isym
->st_shndx
!= SHN_UNDEF
4445 && (definition
|| h
->size
== 0))
4448 && h
->size
!= isym
->st_size
4449 && ! size_change_ok
)
4450 (*_bfd_error_handler
)
4451 (_("Warning: size of symbol `%s' changed"
4452 " from %lu in %B to %lu in %B"),
4454 name
, (unsigned long) h
->size
,
4455 (unsigned long) isym
->st_size
);
4457 h
->size
= isym
->st_size
;
4460 /* If this is a common symbol, then we always want H->SIZE
4461 to be the size of the common symbol. The code just above
4462 won't fix the size if a common symbol becomes larger. We
4463 don't warn about a size change here, because that is
4464 covered by --warn-common. Allow changes between different
4466 if (h
->root
.type
== bfd_link_hash_common
)
4467 h
->size
= h
->root
.u
.c
.size
;
4469 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
4470 && ((definition
&& !new_weak
)
4471 || (old_weak
&& h
->root
.type
== bfd_link_hash_common
)
4472 || h
->type
== STT_NOTYPE
))
4474 unsigned int type
= ELF_ST_TYPE (isym
->st_info
);
4476 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4478 if (type
== STT_GNU_IFUNC
4479 && (abfd
->flags
& DYNAMIC
) != 0)
4482 if (h
->type
!= type
)
4484 if (h
->type
!= STT_NOTYPE
&& ! type_change_ok
)
4485 (*_bfd_error_handler
)
4486 (_("Warning: type of symbol `%s' changed"
4487 " from %d to %d in %B"),
4488 abfd
, name
, h
->type
, type
);
4494 /* Merge st_other field. */
4495 elf_merge_st_other (abfd
, h
, isym
, sec
, definition
, dynamic
);
4497 /* We don't want to make debug symbol dynamic. */
4499 && (sec
->flags
& SEC_DEBUGGING
)
4500 && !bfd_link_relocatable (info
))
4503 /* Nor should we make plugin symbols dynamic. */
4504 if ((abfd
->flags
& BFD_PLUGIN
) != 0)
4509 h
->target_internal
= isym
->st_target_internal
;
4510 h
->unique_global
= (flags
& BSF_GNU_UNIQUE
) != 0;
4513 if (definition
&& !dynamic
)
4515 char *p
= strchr (name
, ELF_VER_CHR
);
4516 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4518 /* Queue non-default versions so that .symver x, x@FOO
4519 aliases can be checked. */
4522 amt
= ((isymend
- isym
+ 1)
4523 * sizeof (struct elf_link_hash_entry
*));
4525 = (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4527 goto error_free_vers
;
4529 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4533 if (dynsym
&& h
->dynindx
== -1)
4535 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4536 goto error_free_vers
;
4537 if (h
->u
.weakdef
!= NULL
4539 && h
->u
.weakdef
->dynindx
== -1)
4541 if (!bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4542 goto error_free_vers
;
4545 else if (dynsym
&& h
->dynindx
!= -1)
4546 /* If the symbol already has a dynamic index, but
4547 visibility says it should not be visible, turn it into
4549 switch (ELF_ST_VISIBILITY (h
->other
))
4553 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4558 /* Don't add DT_NEEDED for references from the dummy bfd. */
4562 && h
->ref_regular_nonweak
4564 || (old_bfd
->flags
& BFD_PLUGIN
) == 0))
4565 || (h
->ref_dynamic_nonweak
4566 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0
4567 && !on_needed_list (elf_dt_name (abfd
), htab
->needed
))))
4570 const char *soname
= elf_dt_name (abfd
);
4572 info
->callbacks
->minfo ("%!", soname
, old_bfd
,
4573 h
->root
.root
.string
);
4575 /* A symbol from a library loaded via DT_NEEDED of some
4576 other library is referenced by a regular object.
4577 Add a DT_NEEDED entry for it. Issue an error if
4578 --no-add-needed is used and the reference was not
4581 && (elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4583 (*_bfd_error_handler
)
4584 (_("%B: undefined reference to symbol '%s'"),
4586 bfd_set_error (bfd_error_missing_dso
);
4587 goto error_free_vers
;
4590 elf_dyn_lib_class (abfd
) = (enum dynamic_lib_link_class
)
4591 (elf_dyn_lib_class (abfd
) & ~DYN_AS_NEEDED
);
4594 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4596 goto error_free_vers
;
4598 BFD_ASSERT (ret
== 0);
4603 if (extversym
!= NULL
)
4609 if (isymbuf
!= NULL
)
4615 if ((elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4619 /* Restore the symbol table. */
4620 old_ent
= (char *) old_tab
+ tabsize
;
4621 memset (elf_sym_hashes (abfd
), 0,
4622 extsymcount
* sizeof (struct elf_link_hash_entry
*));
4623 htab
->root
.table
.table
= old_table
;
4624 htab
->root
.table
.size
= old_size
;
4625 htab
->root
.table
.count
= old_count
;
4626 memcpy (htab
->root
.table
.table
, old_tab
, tabsize
);
4627 htab
->root
.undefs
= old_undefs
;
4628 htab
->root
.undefs_tail
= old_undefs_tail
;
4629 _bfd_elf_strtab_restore_size (htab
->dynstr
, old_dynstr_size
);
4630 for (i
= 0; i
< htab
->root
.table
.size
; i
++)
4632 struct bfd_hash_entry
*p
;
4633 struct elf_link_hash_entry
*h
;
4635 unsigned int alignment_power
;
4637 for (p
= htab
->root
.table
.table
[i
]; p
!= NULL
; p
= p
->next
)
4639 h
= (struct elf_link_hash_entry
*) p
;
4640 if (h
->root
.type
== bfd_link_hash_warning
)
4641 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4642 if (h
->dynindx
>= old_dynsymcount
4643 && h
->dynstr_index
< old_dynstr_size
)
4644 _bfd_elf_strtab_delref (htab
->dynstr
, h
->dynstr_index
);
4646 /* Preserve the maximum alignment and size for common
4647 symbols even if this dynamic lib isn't on DT_NEEDED
4648 since it can still be loaded at run time by another
4650 if (h
->root
.type
== bfd_link_hash_common
)
4652 size
= h
->root
.u
.c
.size
;
4653 alignment_power
= h
->root
.u
.c
.p
->alignment_power
;
4658 alignment_power
= 0;
4660 memcpy (p
, old_ent
, htab
->root
.table
.entsize
);
4661 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4662 h
= (struct elf_link_hash_entry
*) p
;
4663 if (h
->root
.type
== bfd_link_hash_warning
)
4665 memcpy (h
->root
.u
.i
.link
, old_ent
, htab
->root
.table
.entsize
);
4666 old_ent
= (char *) old_ent
+ htab
->root
.table
.entsize
;
4667 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4669 if (h
->root
.type
== bfd_link_hash_common
)
4671 if (size
> h
->root
.u
.c
.size
)
4672 h
->root
.u
.c
.size
= size
;
4673 if (alignment_power
> h
->root
.u
.c
.p
->alignment_power
)
4674 h
->root
.u
.c
.p
->alignment_power
= alignment_power
;
4679 /* Make a special call to the linker "notice" function to
4680 tell it that symbols added for crefs may need to be removed. */
4681 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_not_needed
))
4682 goto error_free_vers
;
4685 objalloc_free_block ((struct objalloc
*) htab
->root
.table
.memory
,
4687 if (nondeflt_vers
!= NULL
)
4688 free (nondeflt_vers
);
4692 if (old_tab
!= NULL
)
4694 if (!(*bed
->notice_as_needed
) (abfd
, info
, notice_needed
))
4695 goto error_free_vers
;
4700 /* Now that all the symbols from this input file are created, if
4701 not performing a relocatable link, handle .symver foo, foo@BAR
4702 such that any relocs against foo become foo@BAR. */
4703 if (!bfd_link_relocatable (info
) && nondeflt_vers
!= NULL
)
4705 bfd_size_type cnt
, symidx
;
4707 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4709 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4710 char *shortname
, *p
;
4712 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4714 || (h
->root
.type
!= bfd_link_hash_defined
4715 && h
->root
.type
!= bfd_link_hash_defweak
))
4718 amt
= p
- h
->root
.root
.string
;
4719 shortname
= (char *) bfd_malloc (amt
+ 1);
4721 goto error_free_vers
;
4722 memcpy (shortname
, h
->root
.root
.string
, amt
);
4723 shortname
[amt
] = '\0';
4725 hi
= (struct elf_link_hash_entry
*)
4726 bfd_link_hash_lookup (&htab
->root
, shortname
,
4727 FALSE
, FALSE
, FALSE
);
4729 && hi
->root
.type
== h
->root
.type
4730 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4731 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4733 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4734 hi
->root
.type
= bfd_link_hash_indirect
;
4735 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4736 (*bed
->elf_backend_copy_indirect_symbol
) (info
, h
, hi
);
4737 sym_hash
= elf_sym_hashes (abfd
);
4739 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4740 if (sym_hash
[symidx
] == hi
)
4742 sym_hash
[symidx
] = h
;
4748 free (nondeflt_vers
);
4749 nondeflt_vers
= NULL
;
4752 /* Now set the weakdefs field correctly for all the weak defined
4753 symbols we found. The only way to do this is to search all the
4754 symbols. Since we only need the information for non functions in
4755 dynamic objects, that's the only time we actually put anything on
4756 the list WEAKS. We need this information so that if a regular
4757 object refers to a symbol defined weakly in a dynamic object, the
4758 real symbol in the dynamic object is also put in the dynamic
4759 symbols; we also must arrange for both symbols to point to the
4760 same memory location. We could handle the general case of symbol
4761 aliasing, but a general symbol alias can only be generated in
4762 assembler code, handling it correctly would be very time
4763 consuming, and other ELF linkers don't handle general aliasing
4767 struct elf_link_hash_entry
**hpp
;
4768 struct elf_link_hash_entry
**hppend
;
4769 struct elf_link_hash_entry
**sorted_sym_hash
;
4770 struct elf_link_hash_entry
*h
;
4773 /* Since we have to search the whole symbol list for each weak
4774 defined symbol, search time for N weak defined symbols will be
4775 O(N^2). Binary search will cut it down to O(NlogN). */
4776 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4777 sorted_sym_hash
= (struct elf_link_hash_entry
**) bfd_malloc (amt
);
4778 if (sorted_sym_hash
== NULL
)
4780 sym_hash
= sorted_sym_hash
;
4781 hpp
= elf_sym_hashes (abfd
);
4782 hppend
= hpp
+ extsymcount
;
4784 for (; hpp
< hppend
; hpp
++)
4788 && h
->root
.type
== bfd_link_hash_defined
4789 && !bed
->is_function_type (h
->type
))
4797 qsort (sorted_sym_hash
, sym_count
,
4798 sizeof (struct elf_link_hash_entry
*),
4801 while (weaks
!= NULL
)
4803 struct elf_link_hash_entry
*hlook
;
4806 size_t i
, j
, idx
= 0;
4809 weaks
= hlook
->u
.weakdef
;
4810 hlook
->u
.weakdef
= NULL
;
4812 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4813 || hlook
->root
.type
== bfd_link_hash_defweak
4814 || hlook
->root
.type
== bfd_link_hash_common
4815 || hlook
->root
.type
== bfd_link_hash_indirect
);
4816 slook
= hlook
->root
.u
.def
.section
;
4817 vlook
= hlook
->root
.u
.def
.value
;
4823 bfd_signed_vma vdiff
;
4825 h
= sorted_sym_hash
[idx
];
4826 vdiff
= vlook
- h
->root
.u
.def
.value
;
4833 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4843 /* We didn't find a value/section match. */
4847 /* With multiple aliases, or when the weak symbol is already
4848 strongly defined, we have multiple matching symbols and
4849 the binary search above may land on any of them. Step
4850 one past the matching symbol(s). */
4853 h
= sorted_sym_hash
[idx
];
4854 if (h
->root
.u
.def
.section
!= slook
4855 || h
->root
.u
.def
.value
!= vlook
)
4859 /* Now look back over the aliases. Since we sorted by size
4860 as well as value and section, we'll choose the one with
4861 the largest size. */
4864 h
= sorted_sym_hash
[idx
];
4866 /* Stop if value or section doesn't match. */
4867 if (h
->root
.u
.def
.section
!= slook
4868 || h
->root
.u
.def
.value
!= vlook
)
4870 else if (h
!= hlook
)
4872 hlook
->u
.weakdef
= h
;
4874 /* If the weak definition is in the list of dynamic
4875 symbols, make sure the real definition is put
4877 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4879 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4882 free (sorted_sym_hash
);
4887 /* If the real definition is in the list of dynamic
4888 symbols, make sure the weak definition is put
4889 there as well. If we don't do this, then the
4890 dynamic loader might not merge the entries for the
4891 real definition and the weak definition. */
4892 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4894 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4895 goto err_free_sym_hash
;
4902 free (sorted_sym_hash
);
4905 if (bed
->check_directives
4906 && !(*bed
->check_directives
) (abfd
, info
))
4909 /* If this object is the same format as the output object, and it is
4910 not a shared library, then let the backend look through the
4913 This is required to build global offset table entries and to
4914 arrange for dynamic relocs. It is not required for the
4915 particular common case of linking non PIC code, even when linking
4916 against shared libraries, but unfortunately there is no way of
4917 knowing whether an object file has been compiled PIC or not.
4918 Looking through the relocs is not particularly time consuming.
4919 The problem is that we must either (1) keep the relocs in memory,
4920 which causes the linker to require additional runtime memory or
4921 (2) read the relocs twice from the input file, which wastes time.
4922 This would be a good case for using mmap.
4924 I have no idea how to handle linking PIC code into a file of a
4925 different format. It probably can't be done. */
4927 && is_elf_hash_table (htab
)
4928 && bed
->check_relocs
!= NULL
4929 && elf_object_id (abfd
) == elf_hash_table_id (htab
)
4930 && (*bed
->relocs_compatible
) (abfd
->xvec
, info
->output_bfd
->xvec
))
4934 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4936 Elf_Internal_Rela
*internal_relocs
;
4939 if ((o
->flags
& SEC_RELOC
) == 0
4940 || o
->reloc_count
== 0
4941 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4942 && (o
->flags
& SEC_DEBUGGING
) != 0)
4943 || bfd_is_abs_section (o
->output_section
))
4946 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4948 if (internal_relocs
== NULL
)
4951 ok
= (*bed
->check_relocs
) (abfd
, info
, o
, internal_relocs
);
4953 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4954 free (internal_relocs
);
4961 /* If this is a non-traditional link, try to optimize the handling
4962 of the .stab/.stabstr sections. */
4964 && ! info
->traditional_format
4965 && is_elf_hash_table (htab
)
4966 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4970 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4971 if (stabstr
!= NULL
)
4973 bfd_size_type string_offset
= 0;
4976 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4977 if (CONST_STRNEQ (stab
->name
, ".stab")
4978 && (!stab
->name
[5] ||
4979 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4980 && (stab
->flags
& SEC_MERGE
) == 0
4981 && !bfd_is_abs_section (stab
->output_section
))
4983 struct bfd_elf_section_data
*secdata
;
4985 secdata
= elf_section_data (stab
);
4986 if (! _bfd_link_section_stabs (abfd
, &htab
->stab_info
, stab
,
4987 stabstr
, &secdata
->sec_info
,
4990 if (secdata
->sec_info
)
4991 stab
->sec_info_type
= SEC_INFO_TYPE_STABS
;
4996 if (is_elf_hash_table (htab
) && add_needed
)
4998 /* Add this bfd to the loaded list. */
4999 struct elf_link_loaded_list
*n
;
5001 n
= (struct elf_link_loaded_list
*) bfd_alloc (abfd
, sizeof (*n
));
5005 n
->next
= htab
->loaded
;
5012 if (old_tab
!= NULL
)
5014 if (nondeflt_vers
!= NULL
)
5015 free (nondeflt_vers
);
5016 if (extversym
!= NULL
)
5019 if (isymbuf
!= NULL
)
5025 /* Return the linker hash table entry of a symbol that might be
5026 satisfied by an archive symbol. Return -1 on error. */
5028 struct elf_link_hash_entry
*
5029 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
5030 struct bfd_link_info
*info
,
5033 struct elf_link_hash_entry
*h
;
5037 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, TRUE
);
5041 /* If this is a default version (the name contains @@), look up the
5042 symbol again with only one `@' as well as without the version.
5043 The effect is that references to the symbol with and without the
5044 version will be matched by the default symbol in the archive. */
5046 p
= strchr (name
, ELF_VER_CHR
);
5047 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
5050 /* First check with only one `@'. */
5051 len
= strlen (name
);
5052 copy
= (char *) bfd_alloc (abfd
, len
);
5054 return (struct elf_link_hash_entry
*) 0 - 1;
5056 first
= p
- name
+ 1;
5057 memcpy (copy
, name
, first
);
5058 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
5060 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, TRUE
);
5063 /* We also need to check references to the symbol without the
5065 copy
[first
- 1] = '\0';
5066 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
5067 FALSE
, FALSE
, TRUE
);
5070 bfd_release (abfd
, copy
);
5074 /* Add symbols from an ELF archive file to the linker hash table. We
5075 don't use _bfd_generic_link_add_archive_symbols because we need to
5076 handle versioned symbols.
5078 Fortunately, ELF archive handling is simpler than that done by
5079 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5080 oddities. In ELF, if we find a symbol in the archive map, and the
5081 symbol is currently undefined, we know that we must pull in that
5084 Unfortunately, we do have to make multiple passes over the symbol
5085 table until nothing further is resolved. */
5088 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5091 unsigned char *included
= NULL
;
5095 const struct elf_backend_data
*bed
;
5096 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
5097 (bfd
*, struct bfd_link_info
*, const char *);
5099 if (! bfd_has_map (abfd
))
5101 /* An empty archive is a special case. */
5102 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
5104 bfd_set_error (bfd_error_no_armap
);
5108 /* Keep track of all symbols we know to be already defined, and all
5109 files we know to be already included. This is to speed up the
5110 second and subsequent passes. */
5111 c
= bfd_ardata (abfd
)->symdef_count
;
5115 amt
*= sizeof (*included
);
5116 included
= (unsigned char *) bfd_zmalloc (amt
);
5117 if (included
== NULL
)
5120 symdefs
= bfd_ardata (abfd
)->symdefs
;
5121 bed
= get_elf_backend_data (abfd
);
5122 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
5135 symdefend
= symdef
+ c
;
5136 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
5138 struct elf_link_hash_entry
*h
;
5140 struct bfd_link_hash_entry
*undefs_tail
;
5145 if (symdef
->file_offset
== last
)
5151 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
5152 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
5158 if (h
->root
.type
== bfd_link_hash_common
)
5160 /* We currently have a common symbol. The archive map contains
5161 a reference to this symbol, so we may want to include it. We
5162 only want to include it however, if this archive element
5163 contains a definition of the symbol, not just another common
5166 Unfortunately some archivers (including GNU ar) will put
5167 declarations of common symbols into their archive maps, as
5168 well as real definitions, so we cannot just go by the archive
5169 map alone. Instead we must read in the element's symbol
5170 table and check that to see what kind of symbol definition
5172 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
5175 else if (h
->root
.type
!= bfd_link_hash_undefined
)
5177 if (h
->root
.type
!= bfd_link_hash_undefweak
)
5178 /* Symbol must be defined. Don't check it again. */
5183 /* We need to include this archive member. */
5184 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
5185 if (element
== NULL
)
5188 if (! bfd_check_format (element
, bfd_object
))
5191 undefs_tail
= info
->hash
->undefs_tail
;
5193 if (!(*info
->callbacks
5194 ->add_archive_element
) (info
, element
, symdef
->name
, &element
))
5196 if (!bfd_link_add_symbols (element
, info
))
5199 /* If there are any new undefined symbols, we need to make
5200 another pass through the archive in order to see whether
5201 they can be defined. FIXME: This isn't perfect, because
5202 common symbols wind up on undefs_tail and because an
5203 undefined symbol which is defined later on in this pass
5204 does not require another pass. This isn't a bug, but it
5205 does make the code less efficient than it could be. */
5206 if (undefs_tail
!= info
->hash
->undefs_tail
)
5209 /* Look backward to mark all symbols from this object file
5210 which we have already seen in this pass. */
5214 included
[mark
] = TRUE
;
5219 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
5221 /* We mark subsequent symbols from this object file as we go
5222 on through the loop. */
5223 last
= symdef
->file_offset
;
5233 if (included
!= NULL
)
5238 /* Given an ELF BFD, add symbols to the global hash table as
5242 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
5244 switch (bfd_get_format (abfd
))
5247 return elf_link_add_object_symbols (abfd
, info
);
5249 return elf_link_add_archive_symbols (abfd
, info
);
5251 bfd_set_error (bfd_error_wrong_format
);
5256 struct hash_codes_info
5258 unsigned long *hashcodes
;
5262 /* This function will be called though elf_link_hash_traverse to store
5263 all hash value of the exported symbols in an array. */
5266 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5268 struct hash_codes_info
*inf
= (struct hash_codes_info
*) data
;
5273 /* Ignore indirect symbols. These are added by the versioning code. */
5274 if (h
->dynindx
== -1)
5277 name
= h
->root
.root
.string
;
5278 if (h
->versioned
>= versioned
)
5280 char *p
= strchr (name
, ELF_VER_CHR
);
5283 alc
= (char *) bfd_malloc (p
- name
+ 1);
5289 memcpy (alc
, name
, p
- name
);
5290 alc
[p
- name
] = '\0';
5295 /* Compute the hash value. */
5296 ha
= bfd_elf_hash (name
);
5298 /* Store the found hash value in the array given as the argument. */
5299 *(inf
->hashcodes
)++ = ha
;
5301 /* And store it in the struct so that we can put it in the hash table
5303 h
->u
.elf_hash_value
= ha
;
5311 struct collect_gnu_hash_codes
5314 const struct elf_backend_data
*bed
;
5315 unsigned long int nsyms
;
5316 unsigned long int maskbits
;
5317 unsigned long int *hashcodes
;
5318 unsigned long int *hashval
;
5319 unsigned long int *indx
;
5320 unsigned long int *counts
;
5323 long int min_dynindx
;
5324 unsigned long int bucketcount
;
5325 unsigned long int symindx
;
5326 long int local_indx
;
5327 long int shift1
, shift2
;
5328 unsigned long int mask
;
5332 /* This function will be called though elf_link_hash_traverse to store
5333 all hash value of the exported symbols in an array. */
5336 elf_collect_gnu_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
5338 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5343 /* Ignore indirect symbols. These are added by the versioning code. */
5344 if (h
->dynindx
== -1)
5347 /* Ignore also local symbols and undefined symbols. */
5348 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5351 name
= h
->root
.root
.string
;
5352 if (h
->versioned
>= versioned
)
5354 char *p
= strchr (name
, ELF_VER_CHR
);
5357 alc
= (char *) bfd_malloc (p
- name
+ 1);
5363 memcpy (alc
, name
, p
- name
);
5364 alc
[p
- name
] = '\0';
5369 /* Compute the hash value. */
5370 ha
= bfd_elf_gnu_hash (name
);
5372 /* Store the found hash value in the array for compute_bucket_count,
5373 and also for .dynsym reordering purposes. */
5374 s
->hashcodes
[s
->nsyms
] = ha
;
5375 s
->hashval
[h
->dynindx
] = ha
;
5377 if (s
->min_dynindx
< 0 || s
->min_dynindx
> h
->dynindx
)
5378 s
->min_dynindx
= h
->dynindx
;
5386 /* This function will be called though elf_link_hash_traverse to do
5387 final dynaminc symbol renumbering. */
5390 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry
*h
, void *data
)
5392 struct collect_gnu_hash_codes
*s
= (struct collect_gnu_hash_codes
*) data
;
5393 unsigned long int bucket
;
5394 unsigned long int val
;
5396 /* Ignore indirect symbols. */
5397 if (h
->dynindx
== -1)
5400 /* Ignore also local symbols and undefined symbols. */
5401 if (! (*s
->bed
->elf_hash_symbol
) (h
))
5403 if (h
->dynindx
>= s
->min_dynindx
)
5404 h
->dynindx
= s
->local_indx
++;
5408 bucket
= s
->hashval
[h
->dynindx
] % s
->bucketcount
;
5409 val
= (s
->hashval
[h
->dynindx
] >> s
->shift1
)
5410 & ((s
->maskbits
>> s
->shift1
) - 1);
5411 s
->bitmask
[val
] |= ((bfd_vma
) 1) << (s
->hashval
[h
->dynindx
] & s
->mask
);
5413 |= ((bfd_vma
) 1) << ((s
->hashval
[h
->dynindx
] >> s
->shift2
) & s
->mask
);
5414 val
= s
->hashval
[h
->dynindx
] & ~(unsigned long int) 1;
5415 if (s
->counts
[bucket
] == 1)
5416 /* Last element terminates the chain. */
5418 bfd_put_32 (s
->output_bfd
, val
,
5419 s
->contents
+ (s
->indx
[bucket
] - s
->symindx
) * 4);
5420 --s
->counts
[bucket
];
5421 h
->dynindx
= s
->indx
[bucket
]++;
5425 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5428 _bfd_elf_hash_symbol (struct elf_link_hash_entry
*h
)
5430 return !(h
->forced_local
5431 || h
->root
.type
== bfd_link_hash_undefined
5432 || h
->root
.type
== bfd_link_hash_undefweak
5433 || ((h
->root
.type
== bfd_link_hash_defined
5434 || h
->root
.type
== bfd_link_hash_defweak
)
5435 && h
->root
.u
.def
.section
->output_section
== NULL
));
5438 /* Array used to determine the number of hash table buckets to use
5439 based on the number of symbols there are. If there are fewer than
5440 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5441 fewer than 37 we use 17 buckets, and so forth. We never use more
5442 than 32771 buckets. */
5444 static const size_t elf_buckets
[] =
5446 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5450 /* Compute bucket count for hashing table. We do not use a static set
5451 of possible tables sizes anymore. Instead we determine for all
5452 possible reasonable sizes of the table the outcome (i.e., the
5453 number of collisions etc) and choose the best solution. The
5454 weighting functions are not too simple to allow the table to grow
5455 without bounds. Instead one of the weighting factors is the size.
5456 Therefore the result is always a good payoff between few collisions
5457 (= short chain lengths) and table size. */
5459 compute_bucket_count (struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
5460 unsigned long int *hashcodes ATTRIBUTE_UNUSED
,
5461 unsigned long int nsyms
,
5464 size_t best_size
= 0;
5465 unsigned long int i
;
5467 /* We have a problem here. The following code to optimize the table
5468 size requires an integer type with more the 32 bits. If
5469 BFD_HOST_U_64_BIT is set we know about such a type. */
5470 #ifdef BFD_HOST_U_64_BIT
5475 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
5476 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
5477 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
5478 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
5479 unsigned long int *counts
;
5481 unsigned int no_improvement_count
= 0;
5483 /* Possible optimization parameters: if we have NSYMS symbols we say
5484 that the hashing table must at least have NSYMS/4 and at most
5486 minsize
= nsyms
/ 4;
5489 best_size
= maxsize
= nsyms
* 2;
5494 if ((best_size
& 31) == 0)
5498 /* Create array where we count the collisions in. We must use bfd_malloc
5499 since the size could be large. */
5501 amt
*= sizeof (unsigned long int);
5502 counts
= (unsigned long int *) bfd_malloc (amt
);
5506 /* Compute the "optimal" size for the hash table. The criteria is a
5507 minimal chain length. The minor criteria is (of course) the size
5509 for (i
= minsize
; i
< maxsize
; ++i
)
5511 /* Walk through the array of hashcodes and count the collisions. */
5512 BFD_HOST_U_64_BIT max
;
5513 unsigned long int j
;
5514 unsigned long int fact
;
5516 if (gnu_hash
&& (i
& 31) == 0)
5519 memset (counts
, '\0', i
* sizeof (unsigned long int));
5521 /* Determine how often each hash bucket is used. */
5522 for (j
= 0; j
< nsyms
; ++j
)
5523 ++counts
[hashcodes
[j
] % i
];
5525 /* For the weight function we need some information about the
5526 pagesize on the target. This is information need not be 100%
5527 accurate. Since this information is not available (so far) we
5528 define it here to a reasonable default value. If it is crucial
5529 to have a better value some day simply define this value. */
5530 # ifndef BFD_TARGET_PAGESIZE
5531 # define BFD_TARGET_PAGESIZE (4096)
5534 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5536 max
= (2 + dynsymcount
) * bed
->s
->sizeof_hash_entry
;
5539 /* Variant 1: optimize for short chains. We add the squares
5540 of all the chain lengths (which favors many small chain
5541 over a few long chains). */
5542 for (j
= 0; j
< i
; ++j
)
5543 max
+= counts
[j
] * counts
[j
];
5545 /* This adds penalties for the overall size of the table. */
5546 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5549 /* Variant 2: Optimize a lot more for small table. Here we
5550 also add squares of the size but we also add penalties for
5551 empty slots (the +1 term). */
5552 for (j
= 0; j
< i
; ++j
)
5553 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
5555 /* The overall size of the table is considered, but not as
5556 strong as in variant 1, where it is squared. */
5557 fact
= i
/ (BFD_TARGET_PAGESIZE
/ bed
->s
->sizeof_hash_entry
) + 1;
5561 /* Compare with current best results. */
5562 if (max
< best_chlen
)
5566 no_improvement_count
= 0;
5568 /* PR 11843: Avoid futile long searches for the best bucket size
5569 when there are a large number of symbols. */
5570 else if (++no_improvement_count
== 100)
5577 #endif /* defined (BFD_HOST_U_64_BIT) */
5579 /* This is the fallback solution if no 64bit type is available or if we
5580 are not supposed to spend much time on optimizations. We select the
5581 bucket count using a fixed set of numbers. */
5582 for (i
= 0; elf_buckets
[i
] != 0; i
++)
5584 best_size
= elf_buckets
[i
];
5585 if (nsyms
< elf_buckets
[i
+ 1])
5588 if (gnu_hash
&& best_size
< 2)
5595 /* Size any SHT_GROUP section for ld -r. */
5598 _bfd_elf_size_group_sections (struct bfd_link_info
*info
)
5602 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
5603 if (bfd_get_flavour (ibfd
) == bfd_target_elf_flavour
5604 && !_bfd_elf_fixup_group_sections (ibfd
, bfd_abs_section_ptr
))
5609 /* Set a default stack segment size. The value in INFO wins. If it
5610 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5611 undefined it is initialized. */
5614 bfd_elf_stack_segment_size (bfd
*output_bfd
,
5615 struct bfd_link_info
*info
,
5616 const char *legacy_symbol
,
5617 bfd_vma default_size
)
5619 struct elf_link_hash_entry
*h
= NULL
;
5621 /* Look for legacy symbol. */
5623 h
= elf_link_hash_lookup (elf_hash_table (info
), legacy_symbol
,
5624 FALSE
, FALSE
, FALSE
);
5625 if (h
&& (h
->root
.type
== bfd_link_hash_defined
5626 || h
->root
.type
== bfd_link_hash_defweak
)
5628 && (h
->type
== STT_NOTYPE
|| h
->type
== STT_OBJECT
))
5630 /* The symbol has no type if specified on the command line. */
5631 h
->type
= STT_OBJECT
;
5632 if (info
->stacksize
)
5633 (*_bfd_error_handler
) (_("%B: stack size specified and %s set"),
5634 output_bfd
, legacy_symbol
);
5635 else if (h
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
5636 (*_bfd_error_handler
) (_("%B: %s not absolute"),
5637 output_bfd
, legacy_symbol
);
5639 info
->stacksize
= h
->root
.u
.def
.value
;
5642 if (!info
->stacksize
)
5643 /* If the user didn't set a size, or explicitly inhibit the
5644 size, set it now. */
5645 info
->stacksize
= default_size
;
5647 /* Provide the legacy symbol, if it is referenced. */
5648 if (h
&& (h
->root
.type
== bfd_link_hash_undefined
5649 || h
->root
.type
== bfd_link_hash_undefweak
))
5651 struct bfd_link_hash_entry
*bh
= NULL
;
5653 if (!(_bfd_generic_link_add_one_symbol
5654 (info
, output_bfd
, legacy_symbol
,
5655 BSF_GLOBAL
, bfd_abs_section_ptr
,
5656 info
->stacksize
>= 0 ? info
->stacksize
: 0,
5657 NULL
, FALSE
, get_elf_backend_data (output_bfd
)->collect
, &bh
)))
5660 h
= (struct elf_link_hash_entry
*) bh
;
5662 h
->type
= STT_OBJECT
;
5668 /* Set up the sizes and contents of the ELF dynamic sections. This is
5669 called by the ELF linker emulation before_allocation routine. We
5670 must set the sizes of the sections before the linker sets the
5671 addresses of the various sections. */
5674 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
5677 const char *filter_shlib
,
5679 const char *depaudit
,
5680 const char * const *auxiliary_filters
,
5681 struct bfd_link_info
*info
,
5682 asection
**sinterpptr
)
5684 bfd_size_type soname_indx
;
5686 const struct elf_backend_data
*bed
;
5687 struct elf_info_failed asvinfo
;
5691 soname_indx
= (bfd_size_type
) -1;
5693 if (!is_elf_hash_table (info
->hash
))
5696 bed
= get_elf_backend_data (output_bfd
);
5698 /* Any syms created from now on start with -1 in
5699 got.refcount/offset and plt.refcount/offset. */
5700 elf_hash_table (info
)->init_got_refcount
5701 = elf_hash_table (info
)->init_got_offset
;
5702 elf_hash_table (info
)->init_plt_refcount
5703 = elf_hash_table (info
)->init_plt_offset
;
5705 if (bfd_link_relocatable (info
)
5706 && !_bfd_elf_size_group_sections (info
))
5709 /* The backend may have to create some sections regardless of whether
5710 we're dynamic or not. */
5711 if (bed
->elf_backend_always_size_sections
5712 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5715 /* Determine any GNU_STACK segment requirements, after the backend
5716 has had a chance to set a default segment size. */
5717 if (info
->execstack
)
5718 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| PF_X
;
5719 else if (info
->noexecstack
)
5720 elf_stack_flags (output_bfd
) = PF_R
| PF_W
;
5724 asection
*notesec
= NULL
;
5727 for (inputobj
= info
->input_bfds
;
5729 inputobj
= inputobj
->link
.next
)
5734 & (DYNAMIC
| EXEC_P
| BFD_PLUGIN
| BFD_LINKER_CREATED
))
5736 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
5739 if (s
->flags
& SEC_CODE
)
5743 else if (bed
->default_execstack
)
5746 if (notesec
|| info
->stacksize
> 0)
5747 elf_stack_flags (output_bfd
) = PF_R
| PF_W
| exec
;
5748 if (notesec
&& exec
&& bfd_link_relocatable (info
)
5749 && notesec
->output_section
!= bfd_abs_section_ptr
)
5750 notesec
->output_section
->flags
|= SEC_CODE
;
5753 dynobj
= elf_hash_table (info
)->dynobj
;
5755 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
5757 struct elf_info_failed eif
;
5758 struct elf_link_hash_entry
*h
;
5760 struct bfd_elf_version_tree
*t
;
5761 struct bfd_elf_version_expr
*d
;
5763 bfd_boolean all_defined
;
5765 *sinterpptr
= bfd_get_linker_section (dynobj
, ".interp");
5766 BFD_ASSERT (*sinterpptr
!= NULL
|| !bfd_link_executable (info
));
5770 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5772 if (soname_indx
== (bfd_size_type
) -1
5773 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5779 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5781 info
->flags
|= DF_SYMBOLIC
;
5789 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5791 if (indx
== (bfd_size_type
) -1)
5794 tag
= info
->new_dtags
? DT_RUNPATH
: DT_RPATH
;
5795 if (!_bfd_elf_add_dynamic_entry (info
, tag
, indx
))
5799 if (filter_shlib
!= NULL
)
5803 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5804 filter_shlib
, TRUE
);
5805 if (indx
== (bfd_size_type
) -1
5806 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5810 if (auxiliary_filters
!= NULL
)
5812 const char * const *p
;
5814 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5818 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5820 if (indx
== (bfd_size_type
) -1
5821 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5830 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, audit
,
5832 if (indx
== (bfd_size_type
) -1
5833 || !_bfd_elf_add_dynamic_entry (info
, DT_AUDIT
, indx
))
5837 if (depaudit
!= NULL
)
5841 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, depaudit
,
5843 if (indx
== (bfd_size_type
) -1
5844 || !_bfd_elf_add_dynamic_entry (info
, DT_DEPAUDIT
, indx
))
5851 /* If we are supposed to export all symbols into the dynamic symbol
5852 table (this is not the normal case), then do so. */
5853 if (info
->export_dynamic
5854 || (bfd_link_executable (info
) && info
->dynamic
))
5856 elf_link_hash_traverse (elf_hash_table (info
),
5857 _bfd_elf_export_symbol
,
5863 /* Make all global versions with definition. */
5864 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
5865 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5866 if (!d
->symver
&& d
->literal
)
5868 const char *verstr
, *name
;
5869 size_t namelen
, verlen
, newlen
;
5870 char *newname
, *p
, leading_char
;
5871 struct elf_link_hash_entry
*newh
;
5873 leading_char
= bfd_get_symbol_leading_char (output_bfd
);
5875 namelen
= strlen (name
) + (leading_char
!= '\0');
5877 verlen
= strlen (verstr
);
5878 newlen
= namelen
+ verlen
+ 3;
5880 newname
= (char *) bfd_malloc (newlen
);
5881 if (newname
== NULL
)
5883 newname
[0] = leading_char
;
5884 memcpy (newname
+ (leading_char
!= '\0'), name
, namelen
);
5886 /* Check the hidden versioned definition. */
5887 p
= newname
+ namelen
;
5889 memcpy (p
, verstr
, verlen
+ 1);
5890 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5891 newname
, FALSE
, FALSE
,
5894 || (newh
->root
.type
!= bfd_link_hash_defined
5895 && newh
->root
.type
!= bfd_link_hash_defweak
))
5897 /* Check the default versioned definition. */
5899 memcpy (p
, verstr
, verlen
+ 1);
5900 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5901 newname
, FALSE
, FALSE
,
5906 /* Mark this version if there is a definition and it is
5907 not defined in a shared object. */
5909 && !newh
->def_dynamic
5910 && (newh
->root
.type
== bfd_link_hash_defined
5911 || newh
->root
.type
== bfd_link_hash_defweak
))
5915 /* Attach all the symbols to their version information. */
5916 asvinfo
.info
= info
;
5917 asvinfo
.failed
= FALSE
;
5919 elf_link_hash_traverse (elf_hash_table (info
),
5920 _bfd_elf_link_assign_sym_version
,
5925 if (!info
->allow_undefined_version
)
5927 /* Check if all global versions have a definition. */
5929 for (t
= info
->version_info
; t
!= NULL
; t
= t
->next
)
5930 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5931 if (d
->literal
&& !d
->symver
&& !d
->script
)
5933 (*_bfd_error_handler
)
5934 (_("%s: undefined version: %s"),
5935 d
->pattern
, t
->name
);
5936 all_defined
= FALSE
;
5941 bfd_set_error (bfd_error_bad_value
);
5946 /* Find all symbols which were defined in a dynamic object and make
5947 the backend pick a reasonable value for them. */
5948 elf_link_hash_traverse (elf_hash_table (info
),
5949 _bfd_elf_adjust_dynamic_symbol
,
5954 /* Add some entries to the .dynamic section. We fill in some of the
5955 values later, in bfd_elf_final_link, but we must add the entries
5956 now so that we know the final size of the .dynamic section. */
5958 /* If there are initialization and/or finalization functions to
5959 call then add the corresponding DT_INIT/DT_FINI entries. */
5960 h
= (info
->init_function
5961 ? elf_link_hash_lookup (elf_hash_table (info
),
5962 info
->init_function
, FALSE
,
5969 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5972 h
= (info
->fini_function
5973 ? elf_link_hash_lookup (elf_hash_table (info
),
5974 info
->fini_function
, FALSE
,
5981 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5985 s
= bfd_get_section_by_name (output_bfd
, ".preinit_array");
5986 if (s
!= NULL
&& s
->linker_has_input
)
5988 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5989 if (! bfd_link_executable (info
))
5994 for (sub
= info
->input_bfds
; sub
!= NULL
;
5995 sub
= sub
->link
.next
)
5996 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
)
5997 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5998 if (elf_section_data (o
)->this_hdr
.sh_type
5999 == SHT_PREINIT_ARRAY
)
6001 (*_bfd_error_handler
)
6002 (_("%B: .preinit_array section is not allowed in DSO"),
6007 bfd_set_error (bfd_error_nonrepresentable_section
);
6011 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
6012 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
6015 s
= bfd_get_section_by_name (output_bfd
, ".init_array");
6016 if (s
!= NULL
&& s
->linker_has_input
)
6018 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
6019 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
6022 s
= bfd_get_section_by_name (output_bfd
, ".fini_array");
6023 if (s
!= NULL
&& s
->linker_has_input
)
6025 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
6026 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
6030 dynstr
= bfd_get_linker_section (dynobj
, ".dynstr");
6031 /* If .dynstr is excluded from the link, we don't want any of
6032 these tags. Strictly, we should be checking each section
6033 individually; This quick check covers for the case where
6034 someone does a /DISCARD/ : { *(*) }. */
6035 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
6037 bfd_size_type strsize
;
6039 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6040 if ((info
->emit_hash
6041 && !_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0))
6042 || (info
->emit_gnu_hash
6043 && !_bfd_elf_add_dynamic_entry (info
, DT_GNU_HASH
, 0))
6044 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
6045 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
6046 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
6047 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
6048 bed
->s
->sizeof_sym
))
6053 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
6056 /* The backend must work out the sizes of all the other dynamic
6059 && bed
->elf_backend_size_dynamic_sections
!= NULL
6060 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
6063 if (dynobj
!= NULL
&& elf_hash_table (info
)->dynamic_sections_created
)
6065 unsigned long section_sym_count
;
6066 struct bfd_elf_version_tree
*verdefs
;
6069 /* Set up the version definition section. */
6070 s
= bfd_get_linker_section (dynobj
, ".gnu.version_d");
6071 BFD_ASSERT (s
!= NULL
);
6073 /* We may have created additional version definitions if we are
6074 just linking a regular application. */
6075 verdefs
= info
->version_info
;
6077 /* Skip anonymous version tag. */
6078 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
6079 verdefs
= verdefs
->next
;
6081 if (verdefs
== NULL
&& !info
->create_default_symver
)
6082 s
->flags
|= SEC_EXCLUDE
;
6087 struct bfd_elf_version_tree
*t
;
6089 Elf_Internal_Verdef def
;
6090 Elf_Internal_Verdaux defaux
;
6091 struct bfd_link_hash_entry
*bh
;
6092 struct elf_link_hash_entry
*h
;
6098 /* Make space for the base version. */
6099 size
+= sizeof (Elf_External_Verdef
);
6100 size
+= sizeof (Elf_External_Verdaux
);
6103 /* Make space for the default version. */
6104 if (info
->create_default_symver
)
6106 size
+= sizeof (Elf_External_Verdef
);
6110 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6112 struct bfd_elf_version_deps
*n
;
6114 /* Don't emit base version twice. */
6118 size
+= sizeof (Elf_External_Verdef
);
6119 size
+= sizeof (Elf_External_Verdaux
);
6122 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6123 size
+= sizeof (Elf_External_Verdaux
);
6127 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6128 if (s
->contents
== NULL
&& s
->size
!= 0)
6131 /* Fill in the version definition section. */
6135 def
.vd_version
= VER_DEF_CURRENT
;
6136 def
.vd_flags
= VER_FLG_BASE
;
6139 if (info
->create_default_symver
)
6141 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
6142 def
.vd_next
= sizeof (Elf_External_Verdef
);
6146 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6147 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6148 + sizeof (Elf_External_Verdaux
));
6151 if (soname_indx
!= (bfd_size_type
) -1)
6153 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6155 def
.vd_hash
= bfd_elf_hash (soname
);
6156 defaux
.vda_name
= soname_indx
;
6163 name
= lbasename (output_bfd
->filename
);
6164 def
.vd_hash
= bfd_elf_hash (name
);
6165 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6167 if (indx
== (bfd_size_type
) -1)
6169 defaux
.vda_name
= indx
;
6171 defaux
.vda_next
= 0;
6173 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6174 (Elf_External_Verdef
*) p
);
6175 p
+= sizeof (Elf_External_Verdef
);
6176 if (info
->create_default_symver
)
6178 /* Add a symbol representing this version. */
6180 if (! (_bfd_generic_link_add_one_symbol
6181 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6183 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6185 h
= (struct elf_link_hash_entry
*) bh
;
6188 h
->type
= STT_OBJECT
;
6189 h
->verinfo
.vertree
= NULL
;
6191 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6194 /* Create a duplicate of the base version with the same
6195 aux block, but different flags. */
6198 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6200 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6201 + sizeof (Elf_External_Verdaux
));
6204 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6205 (Elf_External_Verdef
*) p
);
6206 p
+= sizeof (Elf_External_Verdef
);
6208 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6209 (Elf_External_Verdaux
*) p
);
6210 p
+= sizeof (Elf_External_Verdaux
);
6212 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
6215 struct bfd_elf_version_deps
*n
;
6217 /* Don't emit the base version twice. */
6222 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6225 /* Add a symbol representing this version. */
6227 if (! (_bfd_generic_link_add_one_symbol
6228 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
6230 get_elf_backend_data (dynobj
)->collect
, &bh
)))
6232 h
= (struct elf_link_hash_entry
*) bh
;
6235 h
->type
= STT_OBJECT
;
6236 h
->verinfo
.vertree
= t
;
6238 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
6241 def
.vd_version
= VER_DEF_CURRENT
;
6243 if (t
->globals
.list
== NULL
6244 && t
->locals
.list
== NULL
6246 def
.vd_flags
|= VER_FLG_WEAK
;
6247 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
6248 def
.vd_cnt
= cdeps
+ 1;
6249 def
.vd_hash
= bfd_elf_hash (t
->name
);
6250 def
.vd_aux
= sizeof (Elf_External_Verdef
);
6253 /* If a basever node is next, it *must* be the last node in
6254 the chain, otherwise Verdef construction breaks. */
6255 if (t
->next
!= NULL
&& t
->next
->vernum
== 0)
6256 BFD_ASSERT (t
->next
->next
== NULL
);
6258 if (t
->next
!= NULL
&& t
->next
->vernum
!= 0)
6259 def
.vd_next
= (sizeof (Elf_External_Verdef
)
6260 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
6262 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
6263 (Elf_External_Verdef
*) p
);
6264 p
+= sizeof (Elf_External_Verdef
);
6266 defaux
.vda_name
= h
->dynstr_index
;
6267 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6269 defaux
.vda_next
= 0;
6270 if (t
->deps
!= NULL
)
6271 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6272 t
->name_indx
= defaux
.vda_name
;
6274 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6275 (Elf_External_Verdaux
*) p
);
6276 p
+= sizeof (Elf_External_Verdaux
);
6278 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
6280 if (n
->version_needed
== NULL
)
6282 /* This can happen if there was an error in the
6284 defaux
.vda_name
= 0;
6288 defaux
.vda_name
= n
->version_needed
->name_indx
;
6289 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
6292 if (n
->next
== NULL
)
6293 defaux
.vda_next
= 0;
6295 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
6297 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
6298 (Elf_External_Verdaux
*) p
);
6299 p
+= sizeof (Elf_External_Verdaux
);
6303 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
6304 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
6307 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
6310 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
6312 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
6315 else if (info
->flags
& DF_BIND_NOW
)
6317 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
6323 if (bfd_link_executable (info
))
6324 info
->flags_1
&= ~ (DF_1_INITFIRST
6327 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
6331 /* Work out the size of the version reference section. */
6333 s
= bfd_get_linker_section (dynobj
, ".gnu.version_r");
6334 BFD_ASSERT (s
!= NULL
);
6336 struct elf_find_verdep_info sinfo
;
6339 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
6340 if (sinfo
.vers
== 0)
6342 sinfo
.failed
= FALSE
;
6344 elf_link_hash_traverse (elf_hash_table (info
),
6345 _bfd_elf_link_find_version_dependencies
,
6350 if (elf_tdata (output_bfd
)->verref
== NULL
)
6351 s
->flags
|= SEC_EXCLUDE
;
6354 Elf_Internal_Verneed
*t
;
6359 /* Build the version dependency section. */
6362 for (t
= elf_tdata (output_bfd
)->verref
;
6366 Elf_Internal_Vernaux
*a
;
6368 size
+= sizeof (Elf_External_Verneed
);
6370 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6371 size
+= sizeof (Elf_External_Vernaux
);
6375 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6376 if (s
->contents
== NULL
)
6380 for (t
= elf_tdata (output_bfd
)->verref
;
6385 Elf_Internal_Vernaux
*a
;
6389 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6392 t
->vn_version
= VER_NEED_CURRENT
;
6394 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6395 elf_dt_name (t
->vn_bfd
) != NULL
6396 ? elf_dt_name (t
->vn_bfd
)
6397 : lbasename (t
->vn_bfd
->filename
),
6399 if (indx
== (bfd_size_type
) -1)
6402 t
->vn_aux
= sizeof (Elf_External_Verneed
);
6403 if (t
->vn_nextref
== NULL
)
6406 t
->vn_next
= (sizeof (Elf_External_Verneed
)
6407 + caux
* sizeof (Elf_External_Vernaux
));
6409 _bfd_elf_swap_verneed_out (output_bfd
, t
,
6410 (Elf_External_Verneed
*) p
);
6411 p
+= sizeof (Elf_External_Verneed
);
6413 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
6415 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
6416 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
6417 a
->vna_nodename
, FALSE
);
6418 if (indx
== (bfd_size_type
) -1)
6421 if (a
->vna_nextptr
== NULL
)
6424 a
->vna_next
= sizeof (Elf_External_Vernaux
);
6426 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
6427 (Elf_External_Vernaux
*) p
);
6428 p
+= sizeof (Elf_External_Vernaux
);
6432 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
6433 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
6436 elf_tdata (output_bfd
)->cverrefs
= crefs
;
6440 if ((elf_tdata (output_bfd
)->cverrefs
== 0
6441 && elf_tdata (output_bfd
)->cverdefs
== 0)
6442 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6443 §ion_sym_count
) == 0)
6445 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6446 s
->flags
|= SEC_EXCLUDE
;
6452 /* Find the first non-excluded output section. We'll use its
6453 section symbol for some emitted relocs. */
6455 _bfd_elf_init_1_index_section (bfd
*output_bfd
, struct bfd_link_info
*info
)
6459 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6460 if ((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
)) == SEC_ALLOC
6461 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6463 elf_hash_table (info
)->text_index_section
= s
;
6468 /* Find two non-excluded output sections, one for code, one for data.
6469 We'll use their section symbols for some emitted relocs. */
6471 _bfd_elf_init_2_index_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
6475 /* Data first, since setting text_index_section changes
6476 _bfd_elf_link_omit_section_dynsym. */
6477 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6478 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
)) == SEC_ALLOC
)
6479 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6481 elf_hash_table (info
)->data_index_section
= s
;
6485 for (s
= output_bfd
->sections
; s
!= NULL
; s
= s
->next
)
6486 if (((s
->flags
& (SEC_EXCLUDE
| SEC_ALLOC
| SEC_READONLY
))
6487 == (SEC_ALLOC
| SEC_READONLY
))
6488 && !_bfd_elf_link_omit_section_dynsym (output_bfd
, info
, s
))
6490 elf_hash_table (info
)->text_index_section
= s
;
6494 if (elf_hash_table (info
)->text_index_section
== NULL
)
6495 elf_hash_table (info
)->text_index_section
6496 = elf_hash_table (info
)->data_index_section
;
6500 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
6502 const struct elf_backend_data
*bed
;
6504 if (!is_elf_hash_table (info
->hash
))
6507 bed
= get_elf_backend_data (output_bfd
);
6508 (*bed
->elf_backend_init_index_section
) (output_bfd
, info
);
6510 if (elf_hash_table (info
)->dynamic_sections_created
)
6514 bfd_size_type dynsymcount
;
6515 unsigned long section_sym_count
;
6516 unsigned int dtagcount
;
6518 dynobj
= elf_hash_table (info
)->dynobj
;
6520 /* Assign dynsym indicies. In a shared library we generate a
6521 section symbol for each output section, which come first.
6522 Next come all of the back-end allocated local dynamic syms,
6523 followed by the rest of the global symbols. */
6525 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
6526 §ion_sym_count
);
6528 /* Work out the size of the symbol version section. */
6529 s
= bfd_get_linker_section (dynobj
, ".gnu.version");
6530 BFD_ASSERT (s
!= NULL
);
6531 if (dynsymcount
!= 0
6532 && (s
->flags
& SEC_EXCLUDE
) == 0)
6534 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
6535 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6536 if (s
->contents
== NULL
)
6539 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
6543 /* Set the size of the .dynsym and .hash sections. We counted
6544 the number of dynamic symbols in elf_link_add_object_symbols.
6545 We will build the contents of .dynsym and .hash when we build
6546 the final symbol table, because until then we do not know the
6547 correct value to give the symbols. We built the .dynstr
6548 section as we went along in elf_link_add_object_symbols. */
6549 s
= elf_hash_table (info
)->dynsym
;
6550 BFD_ASSERT (s
!= NULL
);
6551 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
6553 if (dynsymcount
!= 0)
6555 s
->contents
= (unsigned char *) bfd_alloc (output_bfd
, s
->size
);
6556 if (s
->contents
== NULL
)
6559 /* The first entry in .dynsym is a dummy symbol.
6560 Clear all the section syms, in case we don't output them all. */
6561 ++section_sym_count
;
6562 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
6565 elf_hash_table (info
)->bucketcount
= 0;
6567 /* Compute the size of the hashing table. As a side effect this
6568 computes the hash values for all the names we export. */
6569 if (info
->emit_hash
)
6571 unsigned long int *hashcodes
;
6572 struct hash_codes_info hashinf
;
6574 unsigned long int nsyms
;
6576 size_t hash_entry_size
;
6578 /* Compute the hash values for all exported symbols. At the same
6579 time store the values in an array so that we could use them for
6581 amt
= dynsymcount
* sizeof (unsigned long int);
6582 hashcodes
= (unsigned long int *) bfd_malloc (amt
);
6583 if (hashcodes
== NULL
)
6585 hashinf
.hashcodes
= hashcodes
;
6586 hashinf
.error
= FALSE
;
6588 /* Put all hash values in HASHCODES. */
6589 elf_link_hash_traverse (elf_hash_table (info
),
6590 elf_collect_hash_codes
, &hashinf
);
6597 nsyms
= hashinf
.hashcodes
- hashcodes
;
6599 = compute_bucket_count (info
, hashcodes
, nsyms
, 0);
6602 if (bucketcount
== 0)
6605 elf_hash_table (info
)->bucketcount
= bucketcount
;
6607 s
= bfd_get_linker_section (dynobj
, ".hash");
6608 BFD_ASSERT (s
!= NULL
);
6609 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
6610 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
6611 s
->contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6612 if (s
->contents
== NULL
)
6615 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
6616 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
6617 s
->contents
+ hash_entry_size
);
6620 if (info
->emit_gnu_hash
)
6623 unsigned char *contents
;
6624 struct collect_gnu_hash_codes cinfo
;
6628 memset (&cinfo
, 0, sizeof (cinfo
));
6630 /* Compute the hash values for all exported symbols. At the same
6631 time store the values in an array so that we could use them for
6633 amt
= dynsymcount
* 2 * sizeof (unsigned long int);
6634 cinfo
.hashcodes
= (long unsigned int *) bfd_malloc (amt
);
6635 if (cinfo
.hashcodes
== NULL
)
6638 cinfo
.hashval
= cinfo
.hashcodes
+ dynsymcount
;
6639 cinfo
.min_dynindx
= -1;
6640 cinfo
.output_bfd
= output_bfd
;
6643 /* Put all hash values in HASHCODES. */
6644 elf_link_hash_traverse (elf_hash_table (info
),
6645 elf_collect_gnu_hash_codes
, &cinfo
);
6648 free (cinfo
.hashcodes
);
6653 = compute_bucket_count (info
, cinfo
.hashcodes
, cinfo
.nsyms
, 1);
6655 if (bucketcount
== 0)
6657 free (cinfo
.hashcodes
);
6661 s
= bfd_get_linker_section (dynobj
, ".gnu.hash");
6662 BFD_ASSERT (s
!= NULL
);
6664 if (cinfo
.nsyms
== 0)
6666 /* Empty .gnu.hash section is special. */
6667 BFD_ASSERT (cinfo
.min_dynindx
== -1);
6668 free (cinfo
.hashcodes
);
6669 s
->size
= 5 * 4 + bed
->s
->arch_size
/ 8;
6670 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6671 if (contents
== NULL
)
6673 s
->contents
= contents
;
6674 /* 1 empty bucket. */
6675 bfd_put_32 (output_bfd
, 1, contents
);
6676 /* SYMIDX above the special symbol 0. */
6677 bfd_put_32 (output_bfd
, 1, contents
+ 4);
6678 /* Just one word for bitmask. */
6679 bfd_put_32 (output_bfd
, 1, contents
+ 8);
6680 /* Only hash fn bloom filter. */
6681 bfd_put_32 (output_bfd
, 0, contents
+ 12);
6682 /* No hashes are valid - empty bitmask. */
6683 bfd_put (bed
->s
->arch_size
, output_bfd
, 0, contents
+ 16);
6684 /* No hashes in the only bucket. */
6685 bfd_put_32 (output_bfd
, 0,
6686 contents
+ 16 + bed
->s
->arch_size
/ 8);
6690 unsigned long int maskwords
, maskbitslog2
, x
;
6691 BFD_ASSERT (cinfo
.min_dynindx
!= -1);
6695 while ((x
>>= 1) != 0)
6697 if (maskbitslog2
< 3)
6699 else if ((1 << (maskbitslog2
- 2)) & cinfo
.nsyms
)
6700 maskbitslog2
= maskbitslog2
+ 3;
6702 maskbitslog2
= maskbitslog2
+ 2;
6703 if (bed
->s
->arch_size
== 64)
6705 if (maskbitslog2
== 5)
6711 cinfo
.mask
= (1 << cinfo
.shift1
) - 1;
6712 cinfo
.shift2
= maskbitslog2
;
6713 cinfo
.maskbits
= 1 << maskbitslog2
;
6714 maskwords
= 1 << (maskbitslog2
- cinfo
.shift1
);
6715 amt
= bucketcount
* sizeof (unsigned long int) * 2;
6716 amt
+= maskwords
* sizeof (bfd_vma
);
6717 cinfo
.bitmask
= (bfd_vma
*) bfd_malloc (amt
);
6718 if (cinfo
.bitmask
== NULL
)
6720 free (cinfo
.hashcodes
);
6724 cinfo
.counts
= (long unsigned int *) (cinfo
.bitmask
+ maskwords
);
6725 cinfo
.indx
= cinfo
.counts
+ bucketcount
;
6726 cinfo
.symindx
= dynsymcount
- cinfo
.nsyms
;
6727 memset (cinfo
.bitmask
, 0, maskwords
* sizeof (bfd_vma
));
6729 /* Determine how often each hash bucket is used. */
6730 memset (cinfo
.counts
, 0, bucketcount
* sizeof (cinfo
.counts
[0]));
6731 for (i
= 0; i
< cinfo
.nsyms
; ++i
)
6732 ++cinfo
.counts
[cinfo
.hashcodes
[i
] % bucketcount
];
6734 for (i
= 0, cnt
= cinfo
.symindx
; i
< bucketcount
; ++i
)
6735 if (cinfo
.counts
[i
] != 0)
6737 cinfo
.indx
[i
] = cnt
;
6738 cnt
+= cinfo
.counts
[i
];
6740 BFD_ASSERT (cnt
== dynsymcount
);
6741 cinfo
.bucketcount
= bucketcount
;
6742 cinfo
.local_indx
= cinfo
.min_dynindx
;
6744 s
->size
= (4 + bucketcount
+ cinfo
.nsyms
) * 4;
6745 s
->size
+= cinfo
.maskbits
/ 8;
6746 contents
= (unsigned char *) bfd_zalloc (output_bfd
, s
->size
);
6747 if (contents
== NULL
)
6749 free (cinfo
.bitmask
);
6750 free (cinfo
.hashcodes
);
6754 s
->contents
= contents
;
6755 bfd_put_32 (output_bfd
, bucketcount
, contents
);
6756 bfd_put_32 (output_bfd
, cinfo
.symindx
, contents
+ 4);
6757 bfd_put_32 (output_bfd
, maskwords
, contents
+ 8);
6758 bfd_put_32 (output_bfd
, cinfo
.shift2
, contents
+ 12);
6759 contents
+= 16 + cinfo
.maskbits
/ 8;
6761 for (i
= 0; i
< bucketcount
; ++i
)
6763 if (cinfo
.counts
[i
] == 0)
6764 bfd_put_32 (output_bfd
, 0, contents
);
6766 bfd_put_32 (output_bfd
, cinfo
.indx
[i
], contents
);
6770 cinfo
.contents
= contents
;
6772 /* Renumber dynamic symbols, populate .gnu.hash section. */
6773 elf_link_hash_traverse (elf_hash_table (info
),
6774 elf_renumber_gnu_hash_syms
, &cinfo
);
6776 contents
= s
->contents
+ 16;
6777 for (i
= 0; i
< maskwords
; ++i
)
6779 bfd_put (bed
->s
->arch_size
, output_bfd
, cinfo
.bitmask
[i
],
6781 contents
+= bed
->s
->arch_size
/ 8;
6784 free (cinfo
.bitmask
);
6785 free (cinfo
.hashcodes
);
6789 s
= bfd_get_linker_section (dynobj
, ".dynstr");
6790 BFD_ASSERT (s
!= NULL
);
6792 elf_finalize_dynstr (output_bfd
, info
);
6794 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
6796 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
6797 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
6804 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6807 merge_sections_remove_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
6810 BFD_ASSERT (sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
);
6811 sec
->sec_info_type
= SEC_INFO_TYPE_NONE
;
6814 /* Finish SHF_MERGE section merging. */
6817 _bfd_elf_merge_sections (bfd
*abfd
, struct bfd_link_info
*info
)
6822 if (!is_elf_hash_table (info
->hash
))
6825 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
6826 if ((ibfd
->flags
& DYNAMIC
) == 0)
6827 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
6828 if ((sec
->flags
& SEC_MERGE
) != 0
6829 && !bfd_is_abs_section (sec
->output_section
))
6831 struct bfd_elf_section_data
*secdata
;
6833 secdata
= elf_section_data (sec
);
6834 if (! _bfd_add_merge_section (abfd
,
6835 &elf_hash_table (info
)->merge_info
,
6836 sec
, &secdata
->sec_info
))
6838 else if (secdata
->sec_info
)
6839 sec
->sec_info_type
= SEC_INFO_TYPE_MERGE
;
6842 if (elf_hash_table (info
)->merge_info
!= NULL
)
6843 _bfd_merge_sections (abfd
, info
, elf_hash_table (info
)->merge_info
,
6844 merge_sections_remove_hook
);
6848 /* Create an entry in an ELF linker hash table. */
6850 struct bfd_hash_entry
*
6851 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry
*entry
,
6852 struct bfd_hash_table
*table
,
6855 /* Allocate the structure if it has not already been allocated by a
6859 entry
= (struct bfd_hash_entry
*)
6860 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
6865 /* Call the allocation method of the superclass. */
6866 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
6869 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
6870 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
6872 /* Set local fields. */
6875 ret
->got
= htab
->init_got_refcount
;
6876 ret
->plt
= htab
->init_plt_refcount
;
6877 memset (&ret
->size
, 0, (sizeof (struct elf_link_hash_entry
)
6878 - offsetof (struct elf_link_hash_entry
, size
)));
6879 /* Assume that we have been called by a non-ELF symbol reader.
6880 This flag is then reset by the code which reads an ELF input
6881 file. This ensures that a symbol created by a non-ELF symbol
6882 reader will have the flag set correctly. */
6889 /* Copy data from an indirect symbol to its direct symbol, hiding the
6890 old indirect symbol. Also used for copying flags to a weakdef. */
6893 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info
*info
,
6894 struct elf_link_hash_entry
*dir
,
6895 struct elf_link_hash_entry
*ind
)
6897 struct elf_link_hash_table
*htab
;
6899 /* Copy down any references that we may have already seen to the
6900 symbol which just became indirect if DIR isn't a hidden versioned
6903 if (dir
->versioned
!= versioned_hidden
)
6905 dir
->ref_dynamic
|= ind
->ref_dynamic
;
6906 dir
->ref_regular
|= ind
->ref_regular
;
6907 dir
->ref_regular_nonweak
|= ind
->ref_regular_nonweak
;
6908 dir
->non_got_ref
|= ind
->non_got_ref
;
6909 dir
->needs_plt
|= ind
->needs_plt
;
6910 dir
->pointer_equality_needed
|= ind
->pointer_equality_needed
;
6913 if (ind
->root
.type
!= bfd_link_hash_indirect
)
6916 /* Copy over the global and procedure linkage table refcount entries.
6917 These may have been already set up by a check_relocs routine. */
6918 htab
= elf_hash_table (info
);
6919 if (ind
->got
.refcount
> htab
->init_got_refcount
.refcount
)
6921 if (dir
->got
.refcount
< 0)
6922 dir
->got
.refcount
= 0;
6923 dir
->got
.refcount
+= ind
->got
.refcount
;
6924 ind
->got
.refcount
= htab
->init_got_refcount
.refcount
;
6927 if (ind
->plt
.refcount
> htab
->init_plt_refcount
.refcount
)
6929 if (dir
->plt
.refcount
< 0)
6930 dir
->plt
.refcount
= 0;
6931 dir
->plt
.refcount
+= ind
->plt
.refcount
;
6932 ind
->plt
.refcount
= htab
->init_plt_refcount
.refcount
;
6935 if (ind
->dynindx
!= -1)
6937 if (dir
->dynindx
!= -1)
6938 _bfd_elf_strtab_delref (htab
->dynstr
, dir
->dynstr_index
);
6939 dir
->dynindx
= ind
->dynindx
;
6940 dir
->dynstr_index
= ind
->dynstr_index
;
6942 ind
->dynstr_index
= 0;
6947 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info
*info
,
6948 struct elf_link_hash_entry
*h
,
6949 bfd_boolean force_local
)
6951 /* STT_GNU_IFUNC symbol must go through PLT. */
6952 if (h
->type
!= STT_GNU_IFUNC
)
6954 h
->plt
= elf_hash_table (info
)->init_plt_offset
;
6959 h
->forced_local
= 1;
6960 if (h
->dynindx
!= -1)
6963 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
6969 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6973 _bfd_elf_link_hash_table_init
6974 (struct elf_link_hash_table
*table
,
6976 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
6977 struct bfd_hash_table
*,
6979 unsigned int entsize
,
6980 enum elf_target_id target_id
)
6983 int can_refcount
= get_elf_backend_data (abfd
)->can_refcount
;
6985 table
->init_got_refcount
.refcount
= can_refcount
- 1;
6986 table
->init_plt_refcount
.refcount
= can_refcount
- 1;
6987 table
->init_got_offset
.offset
= -(bfd_vma
) 1;
6988 table
->init_plt_offset
.offset
= -(bfd_vma
) 1;
6989 /* The first dynamic symbol is a dummy. */
6990 table
->dynsymcount
= 1;
6992 ret
= _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
6994 table
->root
.type
= bfd_link_elf_hash_table
;
6995 table
->hash_table_id
= target_id
;
7000 /* Create an ELF linker hash table. */
7002 struct bfd_link_hash_table
*
7003 _bfd_elf_link_hash_table_create (bfd
*abfd
)
7005 struct elf_link_hash_table
*ret
;
7006 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
7008 ret
= (struct elf_link_hash_table
*) bfd_zmalloc (amt
);
7012 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
,
7013 sizeof (struct elf_link_hash_entry
),
7019 ret
->root
.hash_table_free
= _bfd_elf_link_hash_table_free
;
7024 /* Destroy an ELF linker hash table. */
7027 _bfd_elf_link_hash_table_free (bfd
*obfd
)
7029 struct elf_link_hash_table
*htab
;
7031 htab
= (struct elf_link_hash_table
*) obfd
->link
.hash
;
7032 if (htab
->dynstr
!= NULL
)
7033 _bfd_elf_strtab_free (htab
->dynstr
);
7034 _bfd_merge_sections_free (htab
->merge_info
);
7035 _bfd_generic_link_hash_table_free (obfd
);
7038 /* This is a hook for the ELF emulation code in the generic linker to
7039 tell the backend linker what file name to use for the DT_NEEDED
7040 entry for a dynamic object. */
7043 bfd_elf_set_dt_needed_name (bfd
*abfd
, const char *name
)
7045 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7046 && bfd_get_format (abfd
) == bfd_object
)
7047 elf_dt_name (abfd
) = name
;
7051 bfd_elf_get_dyn_lib_class (bfd
*abfd
)
7054 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7055 && bfd_get_format (abfd
) == bfd_object
)
7056 lib_class
= elf_dyn_lib_class (abfd
);
7063 bfd_elf_set_dyn_lib_class (bfd
*abfd
, enum dynamic_lib_link_class lib_class
)
7065 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7066 && bfd_get_format (abfd
) == bfd_object
)
7067 elf_dyn_lib_class (abfd
) = lib_class
;
7070 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7071 the linker ELF emulation code. */
7073 struct bfd_link_needed_list
*
7074 bfd_elf_get_needed_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7075 struct bfd_link_info
*info
)
7077 if (! is_elf_hash_table (info
->hash
))
7079 return elf_hash_table (info
)->needed
;
7082 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7083 hook for the linker ELF emulation code. */
7085 struct bfd_link_needed_list
*
7086 bfd_elf_get_runpath_list (bfd
*abfd ATTRIBUTE_UNUSED
,
7087 struct bfd_link_info
*info
)
7089 if (! is_elf_hash_table (info
->hash
))
7091 return elf_hash_table (info
)->runpath
;
7094 /* Get the name actually used for a dynamic object for a link. This
7095 is the SONAME entry if there is one. Otherwise, it is the string
7096 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7099 bfd_elf_get_dt_soname (bfd
*abfd
)
7101 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
7102 && bfd_get_format (abfd
) == bfd_object
)
7103 return elf_dt_name (abfd
);
7107 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7108 the ELF linker emulation code. */
7111 bfd_elf_get_bfd_needed_list (bfd
*abfd
,
7112 struct bfd_link_needed_list
**pneeded
)
7115 bfd_byte
*dynbuf
= NULL
;
7116 unsigned int elfsec
;
7117 unsigned long shlink
;
7118 bfd_byte
*extdyn
, *extdynend
;
7120 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
7124 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
7125 || bfd_get_format (abfd
) != bfd_object
)
7128 s
= bfd_get_section_by_name (abfd
, ".dynamic");
7129 if (s
== NULL
|| s
->size
== 0)
7132 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
7135 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
7136 if (elfsec
== SHN_BAD
)
7139 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
7141 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
7142 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
7145 extdynend
= extdyn
+ s
->size
;
7146 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
7148 Elf_Internal_Dyn dyn
;
7150 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
7152 if (dyn
.d_tag
== DT_NULL
)
7155 if (dyn
.d_tag
== DT_NEEDED
)
7158 struct bfd_link_needed_list
*l
;
7159 unsigned int tagv
= dyn
.d_un
.d_val
;
7162 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
7167 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
7188 struct elf_symbuf_symbol
7190 unsigned long st_name
; /* Symbol name, index in string tbl */
7191 unsigned char st_info
; /* Type and binding attributes */
7192 unsigned char st_other
; /* Visibilty, and target specific */
7195 struct elf_symbuf_head
7197 struct elf_symbuf_symbol
*ssym
;
7198 bfd_size_type count
;
7199 unsigned int st_shndx
;
7206 Elf_Internal_Sym
*isym
;
7207 struct elf_symbuf_symbol
*ssym
;
7212 /* Sort references to symbols by ascending section number. */
7215 elf_sort_elf_symbol (const void *arg1
, const void *arg2
)
7217 const Elf_Internal_Sym
*s1
= *(const Elf_Internal_Sym
**) arg1
;
7218 const Elf_Internal_Sym
*s2
= *(const Elf_Internal_Sym
**) arg2
;
7220 return s1
->st_shndx
- s2
->st_shndx
;
7224 elf_sym_name_compare (const void *arg1
, const void *arg2
)
7226 const struct elf_symbol
*s1
= (const struct elf_symbol
*) arg1
;
7227 const struct elf_symbol
*s2
= (const struct elf_symbol
*) arg2
;
7228 return strcmp (s1
->name
, s2
->name
);
7231 static struct elf_symbuf_head
*
7232 elf_create_symbuf (bfd_size_type symcount
, Elf_Internal_Sym
*isymbuf
)
7234 Elf_Internal_Sym
**ind
, **indbufend
, **indbuf
;
7235 struct elf_symbuf_symbol
*ssym
;
7236 struct elf_symbuf_head
*ssymbuf
, *ssymhead
;
7237 bfd_size_type i
, shndx_count
, total_size
;
7239 indbuf
= (Elf_Internal_Sym
**) bfd_malloc2 (symcount
, sizeof (*indbuf
));
7243 for (ind
= indbuf
, i
= 0; i
< symcount
; i
++)
7244 if (isymbuf
[i
].st_shndx
!= SHN_UNDEF
)
7245 *ind
++ = &isymbuf
[i
];
7248 qsort (indbuf
, indbufend
- indbuf
, sizeof (Elf_Internal_Sym
*),
7249 elf_sort_elf_symbol
);
7252 if (indbufend
> indbuf
)
7253 for (ind
= indbuf
, shndx_count
++; ind
< indbufend
- 1; ind
++)
7254 if (ind
[0]->st_shndx
!= ind
[1]->st_shndx
)
7257 total_size
= ((shndx_count
+ 1) * sizeof (*ssymbuf
)
7258 + (indbufend
- indbuf
) * sizeof (*ssym
));
7259 ssymbuf
= (struct elf_symbuf_head
*) bfd_malloc (total_size
);
7260 if (ssymbuf
== NULL
)
7266 ssym
= (struct elf_symbuf_symbol
*) (ssymbuf
+ shndx_count
+ 1);
7267 ssymbuf
->ssym
= NULL
;
7268 ssymbuf
->count
= shndx_count
;
7269 ssymbuf
->st_shndx
= 0;
7270 for (ssymhead
= ssymbuf
, ind
= indbuf
; ind
< indbufend
; ssym
++, ind
++)
7272 if (ind
== indbuf
|| ssymhead
->st_shndx
!= (*ind
)->st_shndx
)
7275 ssymhead
->ssym
= ssym
;
7276 ssymhead
->count
= 0;
7277 ssymhead
->st_shndx
= (*ind
)->st_shndx
;
7279 ssym
->st_name
= (*ind
)->st_name
;
7280 ssym
->st_info
= (*ind
)->st_info
;
7281 ssym
->st_other
= (*ind
)->st_other
;
7284 BFD_ASSERT ((bfd_size_type
) (ssymhead
- ssymbuf
) == shndx_count
7285 && (((bfd_hostptr_t
) ssym
- (bfd_hostptr_t
) ssymbuf
)
7292 /* Check if 2 sections define the same set of local and global
7296 bfd_elf_match_symbols_in_sections (asection
*sec1
, asection
*sec2
,
7297 struct bfd_link_info
*info
)
7300 const struct elf_backend_data
*bed1
, *bed2
;
7301 Elf_Internal_Shdr
*hdr1
, *hdr2
;
7302 bfd_size_type symcount1
, symcount2
;
7303 Elf_Internal_Sym
*isymbuf1
, *isymbuf2
;
7304 struct elf_symbuf_head
*ssymbuf1
, *ssymbuf2
;
7305 Elf_Internal_Sym
*isym
, *isymend
;
7306 struct elf_symbol
*symtable1
= NULL
, *symtable2
= NULL
;
7307 bfd_size_type count1
, count2
, i
;
7308 unsigned int shndx1
, shndx2
;
7314 /* Both sections have to be in ELF. */
7315 if (bfd_get_flavour (bfd1
) != bfd_target_elf_flavour
7316 || bfd_get_flavour (bfd2
) != bfd_target_elf_flavour
)
7319 if (elf_section_type (sec1
) != elf_section_type (sec2
))
7322 shndx1
= _bfd_elf_section_from_bfd_section (bfd1
, sec1
);
7323 shndx2
= _bfd_elf_section_from_bfd_section (bfd2
, sec2
);
7324 if (shndx1
== SHN_BAD
|| shndx2
== SHN_BAD
)
7327 bed1
= get_elf_backend_data (bfd1
);
7328 bed2
= get_elf_backend_data (bfd2
);
7329 hdr1
= &elf_tdata (bfd1
)->symtab_hdr
;
7330 symcount1
= hdr1
->sh_size
/ bed1
->s
->sizeof_sym
;
7331 hdr2
= &elf_tdata (bfd2
)->symtab_hdr
;
7332 symcount2
= hdr2
->sh_size
/ bed2
->s
->sizeof_sym
;
7334 if (symcount1
== 0 || symcount2
== 0)
7340 ssymbuf1
= (struct elf_symbuf_head
*) elf_tdata (bfd1
)->symbuf
;
7341 ssymbuf2
= (struct elf_symbuf_head
*) elf_tdata (bfd2
)->symbuf
;
7343 if (ssymbuf1
== NULL
)
7345 isymbuf1
= bfd_elf_get_elf_syms (bfd1
, hdr1
, symcount1
, 0,
7347 if (isymbuf1
== NULL
)
7350 if (!info
->reduce_memory_overheads
)
7351 elf_tdata (bfd1
)->symbuf
= ssymbuf1
7352 = elf_create_symbuf (symcount1
, isymbuf1
);
7355 if (ssymbuf1
== NULL
|| ssymbuf2
== NULL
)
7357 isymbuf2
= bfd_elf_get_elf_syms (bfd2
, hdr2
, symcount2
, 0,
7359 if (isymbuf2
== NULL
)
7362 if (ssymbuf1
!= NULL
&& !info
->reduce_memory_overheads
)
7363 elf_tdata (bfd2
)->symbuf
= ssymbuf2
7364 = elf_create_symbuf (symcount2
, isymbuf2
);
7367 if (ssymbuf1
!= NULL
&& ssymbuf2
!= NULL
)
7369 /* Optimized faster version. */
7370 bfd_size_type lo
, hi
, mid
;
7371 struct elf_symbol
*symp
;
7372 struct elf_symbuf_symbol
*ssym
, *ssymend
;
7375 hi
= ssymbuf1
->count
;
7380 mid
= (lo
+ hi
) / 2;
7381 if (shndx1
< ssymbuf1
[mid
].st_shndx
)
7383 else if (shndx1
> ssymbuf1
[mid
].st_shndx
)
7387 count1
= ssymbuf1
[mid
].count
;
7394 hi
= ssymbuf2
->count
;
7399 mid
= (lo
+ hi
) / 2;
7400 if (shndx2
< ssymbuf2
[mid
].st_shndx
)
7402 else if (shndx2
> ssymbuf2
[mid
].st_shndx
)
7406 count2
= ssymbuf2
[mid
].count
;
7412 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7416 = (struct elf_symbol
*) bfd_malloc (count1
* sizeof (*symtable1
));
7418 = (struct elf_symbol
*) bfd_malloc (count2
* sizeof (*symtable2
));
7419 if (symtable1
== NULL
|| symtable2
== NULL
)
7423 for (ssym
= ssymbuf1
->ssym
, ssymend
= ssym
+ count1
;
7424 ssym
< ssymend
; ssym
++, symp
++)
7426 symp
->u
.ssym
= ssym
;
7427 symp
->name
= bfd_elf_string_from_elf_section (bfd1
,
7433 for (ssym
= ssymbuf2
->ssym
, ssymend
= ssym
+ count2
;
7434 ssym
< ssymend
; ssym
++, symp
++)
7436 symp
->u
.ssym
= ssym
;
7437 symp
->name
= bfd_elf_string_from_elf_section (bfd2
,
7442 /* Sort symbol by name. */
7443 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7444 elf_sym_name_compare
);
7445 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7446 elf_sym_name_compare
);
7448 for (i
= 0; i
< count1
; i
++)
7449 /* Two symbols must have the same binding, type and name. */
7450 if (symtable1
[i
].u
.ssym
->st_info
!= symtable2
[i
].u
.ssym
->st_info
7451 || symtable1
[i
].u
.ssym
->st_other
!= symtable2
[i
].u
.ssym
->st_other
7452 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7459 symtable1
= (struct elf_symbol
*)
7460 bfd_malloc (symcount1
* sizeof (struct elf_symbol
));
7461 symtable2
= (struct elf_symbol
*)
7462 bfd_malloc (symcount2
* sizeof (struct elf_symbol
));
7463 if (symtable1
== NULL
|| symtable2
== NULL
)
7466 /* Count definitions in the section. */
7468 for (isym
= isymbuf1
, isymend
= isym
+ symcount1
; isym
< isymend
; isym
++)
7469 if (isym
->st_shndx
== shndx1
)
7470 symtable1
[count1
++].u
.isym
= isym
;
7473 for (isym
= isymbuf2
, isymend
= isym
+ symcount2
; isym
< isymend
; isym
++)
7474 if (isym
->st_shndx
== shndx2
)
7475 symtable2
[count2
++].u
.isym
= isym
;
7477 if (count1
== 0 || count2
== 0 || count1
!= count2
)
7480 for (i
= 0; i
< count1
; i
++)
7482 = bfd_elf_string_from_elf_section (bfd1
, hdr1
->sh_link
,
7483 symtable1
[i
].u
.isym
->st_name
);
7485 for (i
= 0; i
< count2
; i
++)
7487 = bfd_elf_string_from_elf_section (bfd2
, hdr2
->sh_link
,
7488 symtable2
[i
].u
.isym
->st_name
);
7490 /* Sort symbol by name. */
7491 qsort (symtable1
, count1
, sizeof (struct elf_symbol
),
7492 elf_sym_name_compare
);
7493 qsort (symtable2
, count1
, sizeof (struct elf_symbol
),
7494 elf_sym_name_compare
);
7496 for (i
= 0; i
< count1
; i
++)
7497 /* Two symbols must have the same binding, type and name. */
7498 if (symtable1
[i
].u
.isym
->st_info
!= symtable2
[i
].u
.isym
->st_info
7499 || symtable1
[i
].u
.isym
->st_other
!= symtable2
[i
].u
.isym
->st_other
7500 || strcmp (symtable1
[i
].name
, symtable2
[i
].name
) != 0)
7518 /* Return TRUE if 2 section types are compatible. */
7521 _bfd_elf_match_sections_by_type (bfd
*abfd
, const asection
*asec
,
7522 bfd
*bbfd
, const asection
*bsec
)
7526 || abfd
->xvec
->flavour
!= bfd_target_elf_flavour
7527 || bbfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7530 return elf_section_type (asec
) == elf_section_type (bsec
);
7533 /* Final phase of ELF linker. */
7535 /* A structure we use to avoid passing large numbers of arguments. */
7537 struct elf_final_link_info
7539 /* General link information. */
7540 struct bfd_link_info
*info
;
7543 /* Symbol string table. */
7544 struct elf_strtab_hash
*symstrtab
;
7545 /* .hash section. */
7547 /* symbol version section (.gnu.version). */
7548 asection
*symver_sec
;
7549 /* Buffer large enough to hold contents of any section. */
7551 /* Buffer large enough to hold external relocs of any section. */
7552 void *external_relocs
;
7553 /* Buffer large enough to hold internal relocs of any section. */
7554 Elf_Internal_Rela
*internal_relocs
;
7555 /* Buffer large enough to hold external local symbols of any input
7557 bfd_byte
*external_syms
;
7558 /* And a buffer for symbol section indices. */
7559 Elf_External_Sym_Shndx
*locsym_shndx
;
7560 /* Buffer large enough to hold internal local symbols of any input
7562 Elf_Internal_Sym
*internal_syms
;
7563 /* Array large enough to hold a symbol index for each local symbol
7564 of any input BFD. */
7566 /* Array large enough to hold a section pointer for each local
7567 symbol of any input BFD. */
7568 asection
**sections
;
7569 /* Buffer for SHT_SYMTAB_SHNDX section. */
7570 Elf_External_Sym_Shndx
*symshndxbuf
;
7571 /* Number of STT_FILE syms seen. */
7572 size_t filesym_count
;
7575 /* This struct is used to pass information to elf_link_output_extsym. */
7577 struct elf_outext_info
7580 bfd_boolean localsyms
;
7581 bfd_boolean file_sym_done
;
7582 struct elf_final_link_info
*flinfo
;
7586 /* Support for evaluating a complex relocation.
7588 Complex relocations are generalized, self-describing relocations. The
7589 implementation of them consists of two parts: complex symbols, and the
7590 relocations themselves.
7592 The relocations are use a reserved elf-wide relocation type code (R_RELC
7593 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7594 information (start bit, end bit, word width, etc) into the addend. This
7595 information is extracted from CGEN-generated operand tables within gas.
7597 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7598 internal) representing prefix-notation expressions, including but not
7599 limited to those sorts of expressions normally encoded as addends in the
7600 addend field. The symbol mangling format is:
7603 | <unary-operator> ':' <node>
7604 | <binary-operator> ':' <node> ':' <node>
7607 <literal> := 's' <digits=N> ':' <N character symbol name>
7608 | 'S' <digits=N> ':' <N character section name>
7612 <binary-operator> := as in C
7613 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7616 set_symbol_value (bfd
*bfd_with_globals
,
7617 Elf_Internal_Sym
*isymbuf
,
7622 struct elf_link_hash_entry
**sym_hashes
;
7623 struct elf_link_hash_entry
*h
;
7624 size_t extsymoff
= locsymcount
;
7626 if (symidx
< locsymcount
)
7628 Elf_Internal_Sym
*sym
;
7630 sym
= isymbuf
+ symidx
;
7631 if (ELF_ST_BIND (sym
->st_info
) == STB_LOCAL
)
7633 /* It is a local symbol: move it to the
7634 "absolute" section and give it a value. */
7635 sym
->st_shndx
= SHN_ABS
;
7636 sym
->st_value
= val
;
7639 BFD_ASSERT (elf_bad_symtab (bfd_with_globals
));
7643 /* It is a global symbol: set its link type
7644 to "defined" and give it a value. */
7646 sym_hashes
= elf_sym_hashes (bfd_with_globals
);
7647 h
= sym_hashes
[symidx
- extsymoff
];
7648 while (h
->root
.type
== bfd_link_hash_indirect
7649 || h
->root
.type
== bfd_link_hash_warning
)
7650 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7651 h
->root
.type
= bfd_link_hash_defined
;
7652 h
->root
.u
.def
.value
= val
;
7653 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
7657 resolve_symbol (const char *name
,
7659 struct elf_final_link_info
*flinfo
,
7661 Elf_Internal_Sym
*isymbuf
,
7664 Elf_Internal_Sym
*sym
;
7665 struct bfd_link_hash_entry
*global_entry
;
7666 const char *candidate
= NULL
;
7667 Elf_Internal_Shdr
*symtab_hdr
;
7670 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
7672 for (i
= 0; i
< locsymcount
; ++ i
)
7676 if (ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
7679 candidate
= bfd_elf_string_from_elf_section (input_bfd
,
7680 symtab_hdr
->sh_link
,
7683 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7684 name
, candidate
, (unsigned long) sym
->st_value
);
7686 if (candidate
&& strcmp (candidate
, name
) == 0)
7688 asection
*sec
= flinfo
->sections
[i
];
7690 *result
= _bfd_elf_rel_local_sym (input_bfd
, sym
, &sec
, 0);
7691 *result
+= sec
->output_offset
+ sec
->output_section
->vma
;
7693 printf ("Found symbol with value %8.8lx\n",
7694 (unsigned long) *result
);
7700 /* Hmm, haven't found it yet. perhaps it is a global. */
7701 global_entry
= bfd_link_hash_lookup (flinfo
->info
->hash
, name
,
7702 FALSE
, FALSE
, TRUE
);
7706 if (global_entry
->type
== bfd_link_hash_defined
7707 || global_entry
->type
== bfd_link_hash_defweak
)
7709 *result
= (global_entry
->u
.def
.value
7710 + global_entry
->u
.def
.section
->output_section
->vma
7711 + global_entry
->u
.def
.section
->output_offset
);
7713 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7714 global_entry
->root
.string
, (unsigned long) *result
);
7723 resolve_section (const char *name
,
7730 for (curr
= sections
; curr
; curr
= curr
->next
)
7731 if (strcmp (curr
->name
, name
) == 0)
7733 *result
= curr
->vma
;
7737 /* Hmm. still haven't found it. try pseudo-section names. */
7738 for (curr
= sections
; curr
; curr
= curr
->next
)
7740 len
= strlen (curr
->name
);
7741 if (len
> strlen (name
))
7744 if (strncmp (curr
->name
, name
, len
) == 0)
7746 if (strncmp (".end", name
+ len
, 4) == 0)
7748 *result
= curr
->vma
+ curr
->size
;
7752 /* Insert more pseudo-section names here, if you like. */
7760 undefined_reference (const char *reftype
, const char *name
)
7762 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7767 eval_symbol (bfd_vma
*result
,
7770 struct elf_final_link_info
*flinfo
,
7772 Elf_Internal_Sym
*isymbuf
,
7781 const char *sym
= *symp
;
7783 bfd_boolean symbol_is_section
= FALSE
;
7788 if (len
< 1 || len
> sizeof (symbuf
))
7790 bfd_set_error (bfd_error_invalid_operation
);
7803 *result
= strtoul (sym
, (char **) symp
, 16);
7807 symbol_is_section
= TRUE
;
7810 symlen
= strtol (sym
, (char **) symp
, 10);
7811 sym
= *symp
+ 1; /* Skip the trailing ':'. */
7813 if (symend
< sym
|| symlen
+ 1 > sizeof (symbuf
))
7815 bfd_set_error (bfd_error_invalid_operation
);
7819 memcpy (symbuf
, sym
, symlen
);
7820 symbuf
[symlen
] = '\0';
7821 *symp
= sym
+ symlen
;
7823 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7824 the symbol as a section, or vice-versa. so we're pretty liberal in our
7825 interpretation here; section means "try section first", not "must be a
7826 section", and likewise with symbol. */
7828 if (symbol_is_section
)
7830 if (!resolve_section (symbuf
, flinfo
->output_bfd
->sections
, result
)
7831 && !resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7832 isymbuf
, locsymcount
))
7834 undefined_reference ("section", symbuf
);
7840 if (!resolve_symbol (symbuf
, input_bfd
, flinfo
, result
,
7841 isymbuf
, locsymcount
)
7842 && !resolve_section (symbuf
, flinfo
->output_bfd
->sections
,
7845 undefined_reference ("symbol", symbuf
);
7852 /* All that remains are operators. */
7854 #define UNARY_OP(op) \
7855 if (strncmp (sym, #op, strlen (#op)) == 0) \
7857 sym += strlen (#op); \
7861 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7862 isymbuf, locsymcount, signed_p)) \
7865 *result = op ((bfd_signed_vma) a); \
7871 #define BINARY_OP(op) \
7872 if (strncmp (sym, #op, strlen (#op)) == 0) \
7874 sym += strlen (#op); \
7878 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7879 isymbuf, locsymcount, signed_p)) \
7882 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7883 isymbuf, locsymcount, signed_p)) \
7886 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7916 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym
);
7917 bfd_set_error (bfd_error_invalid_operation
);
7923 put_value (bfd_vma size
,
7924 unsigned long chunksz
,
7929 location
+= (size
- chunksz
);
7931 for (; size
; size
-= chunksz
, location
-= chunksz
)
7936 bfd_put_8 (input_bfd
, x
, location
);
7940 bfd_put_16 (input_bfd
, x
, location
);
7944 bfd_put_32 (input_bfd
, x
, location
);
7945 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7951 bfd_put_64 (input_bfd
, x
, location
);
7952 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7965 get_value (bfd_vma size
,
7966 unsigned long chunksz
,
7973 /* Sanity checks. */
7974 BFD_ASSERT (chunksz
<= sizeof (x
)
7977 && (size
% chunksz
) == 0
7978 && input_bfd
!= NULL
7979 && location
!= NULL
);
7981 if (chunksz
== sizeof (x
))
7983 BFD_ASSERT (size
== chunksz
);
7985 /* Make sure that we do not perform an undefined shift operation.
7986 We know that size == chunksz so there will only be one iteration
7987 of the loop below. */
7991 shift
= 8 * chunksz
;
7993 for (; size
; size
-= chunksz
, location
+= chunksz
)
7998 x
= (x
<< shift
) | bfd_get_8 (input_bfd
, location
);
8001 x
= (x
<< shift
) | bfd_get_16 (input_bfd
, location
);
8004 x
= (x
<< shift
) | bfd_get_32 (input_bfd
, location
);
8008 x
= (x
<< shift
) | bfd_get_64 (input_bfd
, location
);
8019 decode_complex_addend (unsigned long *start
, /* in bits */
8020 unsigned long *oplen
, /* in bits */
8021 unsigned long *len
, /* in bits */
8022 unsigned long *wordsz
, /* in bytes */
8023 unsigned long *chunksz
, /* in bytes */
8024 unsigned long *lsb0_p
,
8025 unsigned long *signed_p
,
8026 unsigned long *trunc_p
,
8027 unsigned long encoded
)
8029 * start
= encoded
& 0x3F;
8030 * len
= (encoded
>> 6) & 0x3F;
8031 * oplen
= (encoded
>> 12) & 0x3F;
8032 * wordsz
= (encoded
>> 18) & 0xF;
8033 * chunksz
= (encoded
>> 22) & 0xF;
8034 * lsb0_p
= (encoded
>> 27) & 1;
8035 * signed_p
= (encoded
>> 28) & 1;
8036 * trunc_p
= (encoded
>> 29) & 1;
8039 bfd_reloc_status_type
8040 bfd_elf_perform_complex_relocation (bfd
*input_bfd
,
8041 asection
*input_section ATTRIBUTE_UNUSED
,
8043 Elf_Internal_Rela
*rel
,
8046 bfd_vma shift
, x
, mask
;
8047 unsigned long start
, oplen
, len
, wordsz
, chunksz
, lsb0_p
, signed_p
, trunc_p
;
8048 bfd_reloc_status_type r
;
8050 /* Perform this reloc, since it is complex.
8051 (this is not to say that it necessarily refers to a complex
8052 symbol; merely that it is a self-describing CGEN based reloc.
8053 i.e. the addend has the complete reloc information (bit start, end,
8054 word size, etc) encoded within it.). */
8056 decode_complex_addend (&start
, &oplen
, &len
, &wordsz
,
8057 &chunksz
, &lsb0_p
, &signed_p
,
8058 &trunc_p
, rel
->r_addend
);
8060 mask
= (((1L << (len
- 1)) - 1) << 1) | 1;
8063 shift
= (start
+ 1) - len
;
8065 shift
= (8 * wordsz
) - (start
+ len
);
8067 /* FIXME: octets_per_byte. */
8068 x
= get_value (wordsz
, chunksz
, input_bfd
, contents
+ rel
->r_offset
);
8071 printf ("Doing complex reloc: "
8072 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8073 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8074 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8075 lsb0_p
, signed_p
, trunc_p
, wordsz
, chunksz
, start
, len
,
8076 oplen
, (unsigned long) x
, (unsigned long) mask
,
8077 (unsigned long) relocation
);
8082 /* Now do an overflow check. */
8083 r
= bfd_check_overflow ((signed_p
8084 ? complain_overflow_signed
8085 : complain_overflow_unsigned
),
8086 len
, 0, (8 * wordsz
),
8090 x
= (x
& ~(mask
<< shift
)) | ((relocation
& mask
) << shift
);
8093 printf (" relocation: %8.8lx\n"
8094 " shifted mask: %8.8lx\n"
8095 " shifted/masked reloc: %8.8lx\n"
8096 " result: %8.8lx\n",
8097 (unsigned long) relocation
, (unsigned long) (mask
<< shift
),
8098 (unsigned long) ((relocation
& mask
) << shift
), (unsigned long) x
);
8100 /* FIXME: octets_per_byte. */
8101 put_value (wordsz
, chunksz
, input_bfd
, x
, contents
+ rel
->r_offset
);
8105 /* qsort comparison functions sorting external relocs by r_offset. */
8108 cmp_ext32l_r_offset (const void *p
, const void *q
)
8115 const union aligned32
*a
8116 = (const union aligned32
*) ((const Elf32_External_Rel
*) p
)->r_offset
;
8117 const union aligned32
*b
8118 = (const union aligned32
*) ((const Elf32_External_Rel
*) q
)->r_offset
;
8120 uint32_t aval
= ( (uint32_t) a
->c
[0]
8121 | (uint32_t) a
->c
[1] << 8
8122 | (uint32_t) a
->c
[2] << 16
8123 | (uint32_t) a
->c
[3] << 24);
8124 uint32_t bval
= ( (uint32_t) b
->c
[0]
8125 | (uint32_t) b
->c
[1] << 8
8126 | (uint32_t) b
->c
[2] << 16
8127 | (uint32_t) b
->c
[3] << 24);
8130 else if (aval
> bval
)
8136 cmp_ext32b_r_offset (const void *p
, const void *q
)
8143 const union aligned32
*a
8144 = (const union aligned32
*) ((const Elf32_External_Rel
*) p
)->r_offset
;
8145 const union aligned32
*b
8146 = (const union aligned32
*) ((const Elf32_External_Rel
*) q
)->r_offset
;
8148 uint32_t aval
= ( (uint32_t) a
->c
[0] << 24
8149 | (uint32_t) a
->c
[1] << 16
8150 | (uint32_t) a
->c
[2] << 8
8151 | (uint32_t) a
->c
[3]);
8152 uint32_t bval
= ( (uint32_t) b
->c
[0] << 24
8153 | (uint32_t) b
->c
[1] << 16
8154 | (uint32_t) b
->c
[2] << 8
8155 | (uint32_t) b
->c
[3]);
8158 else if (aval
> bval
)
8163 #ifdef BFD_HOST_64_BIT
8165 cmp_ext64l_r_offset (const void *p
, const void *q
)
8172 const union aligned64
*a
8173 = (const union aligned64
*) ((const Elf64_External_Rel
*) p
)->r_offset
;
8174 const union aligned64
*b
8175 = (const union aligned64
*) ((const Elf64_External_Rel
*) q
)->r_offset
;
8177 uint64_t aval
= ( (uint64_t) a
->c
[0]
8178 | (uint64_t) a
->c
[1] << 8
8179 | (uint64_t) a
->c
[2] << 16
8180 | (uint64_t) a
->c
[3] << 24
8181 | (uint64_t) a
->c
[4] << 32
8182 | (uint64_t) a
->c
[5] << 40
8183 | (uint64_t) a
->c
[6] << 48
8184 | (uint64_t) a
->c
[7] << 56);
8185 uint64_t bval
= ( (uint64_t) b
->c
[0]
8186 | (uint64_t) b
->c
[1] << 8
8187 | (uint64_t) b
->c
[2] << 16
8188 | (uint64_t) b
->c
[3] << 24
8189 | (uint64_t) b
->c
[4] << 32
8190 | (uint64_t) b
->c
[5] << 40
8191 | (uint64_t) b
->c
[6] << 48
8192 | (uint64_t) b
->c
[7] << 56);
8195 else if (aval
> bval
)
8201 cmp_ext64b_r_offset (const void *p
, const void *q
)
8208 const union aligned64
*a
8209 = (const union aligned64
*) ((const Elf64_External_Rel
*) p
)->r_offset
;
8210 const union aligned64
*b
8211 = (const union aligned64
*) ((const Elf64_External_Rel
*) q
)->r_offset
;
8213 uint64_t aval
= ( (uint64_t) a
->c
[0] << 56
8214 | (uint64_t) a
->c
[1] << 48
8215 | (uint64_t) a
->c
[2] << 40
8216 | (uint64_t) a
->c
[3] << 32
8217 | (uint64_t) a
->c
[4] << 24
8218 | (uint64_t) a
->c
[5] << 16
8219 | (uint64_t) a
->c
[6] << 8
8220 | (uint64_t) a
->c
[7]);
8221 uint64_t bval
= ( (uint64_t) b
->c
[0] << 56
8222 | (uint64_t) b
->c
[1] << 48
8223 | (uint64_t) b
->c
[2] << 40
8224 | (uint64_t) b
->c
[3] << 32
8225 | (uint64_t) b
->c
[4] << 24
8226 | (uint64_t) b
->c
[5] << 16
8227 | (uint64_t) b
->c
[6] << 8
8228 | (uint64_t) b
->c
[7]);
8231 else if (aval
> bval
)
8237 /* When performing a relocatable link, the input relocations are
8238 preserved. But, if they reference global symbols, the indices
8239 referenced must be updated. Update all the relocations found in
8243 elf_link_adjust_relocs (bfd
*abfd
,
8244 struct bfd_elf_section_reloc_data
*reldata
,
8248 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8250 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8251 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8252 bfd_vma r_type_mask
;
8254 unsigned int count
= reldata
->count
;
8255 struct elf_link_hash_entry
**rel_hash
= reldata
->hashes
;
8257 if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
8259 swap_in
= bed
->s
->swap_reloc_in
;
8260 swap_out
= bed
->s
->swap_reloc_out
;
8262 else if (reldata
->hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
8264 swap_in
= bed
->s
->swap_reloca_in
;
8265 swap_out
= bed
->s
->swap_reloca_out
;
8270 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
8273 if (bed
->s
->arch_size
== 32)
8280 r_type_mask
= 0xffffffff;
8284 erela
= reldata
->hdr
->contents
;
8285 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= reldata
->hdr
->sh_entsize
)
8287 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
8290 if (*rel_hash
== NULL
)
8293 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
8295 (*swap_in
) (abfd
, erela
, irela
);
8296 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
8297 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
8298 | (irela
[j
].r_info
& r_type_mask
));
8299 (*swap_out
) (abfd
, irela
, erela
);
8304 int (*compare
) (const void *, const void *);
8306 if (bed
->s
->arch_size
== 32)
8308 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8309 compare
= cmp_ext32l_r_offset
;
8310 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8311 compare
= cmp_ext32b_r_offset
;
8317 #ifdef BFD_HOST_64_BIT
8318 if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_LITTLE
)
8319 compare
= cmp_ext64l_r_offset
;
8320 else if (abfd
->xvec
->header_byteorder
== BFD_ENDIAN_BIG
)
8321 compare
= cmp_ext64b_r_offset
;
8326 qsort (reldata
->hdr
->contents
, count
, reldata
->hdr
->sh_entsize
, compare
);
8327 free (reldata
->hashes
);
8328 reldata
->hashes
= NULL
;
8332 struct elf_link_sort_rela
8338 enum elf_reloc_type_class type
;
8339 /* We use this as an array of size int_rels_per_ext_rel. */
8340 Elf_Internal_Rela rela
[1];
8344 elf_link_sort_cmp1 (const void *A
, const void *B
)
8346 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8347 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8348 int relativea
, relativeb
;
8350 relativea
= a
->type
== reloc_class_relative
;
8351 relativeb
= b
->type
== reloc_class_relative
;
8353 if (relativea
< relativeb
)
8355 if (relativea
> relativeb
)
8357 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
8359 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
8361 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8363 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8369 elf_link_sort_cmp2 (const void *A
, const void *B
)
8371 const struct elf_link_sort_rela
*a
= (const struct elf_link_sort_rela
*) A
;
8372 const struct elf_link_sort_rela
*b
= (const struct elf_link_sort_rela
*) B
;
8374 if (a
->type
< b
->type
)
8376 if (a
->type
> b
->type
)
8378 if (a
->u
.offset
< b
->u
.offset
)
8380 if (a
->u
.offset
> b
->u
.offset
)
8382 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
8384 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
8390 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
8392 asection
*dynamic_relocs
;
8395 bfd_size_type count
, size
;
8396 size_t i
, ret
, sort_elt
, ext_size
;
8397 bfd_byte
*sort
, *s_non_relative
, *p
;
8398 struct elf_link_sort_rela
*sq
;
8399 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8400 int i2e
= bed
->s
->int_rels_per_ext_rel
;
8401 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
8402 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
8403 struct bfd_link_order
*lo
;
8405 bfd_boolean use_rela
;
8407 /* Find a dynamic reloc section. */
8408 rela_dyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
8409 rel_dyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
8410 if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0
8411 && rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8413 bfd_boolean use_rela_initialised
= FALSE
;
8415 /* This is just here to stop gcc from complaining.
8416 It's initialization checking code is not perfect. */
8419 /* Both sections are present. Examine the sizes
8420 of the indirect sections to help us choose. */
8421 for (lo
= rela_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8422 if (lo
->type
== bfd_indirect_link_order
)
8424 asection
*o
= lo
->u
.indirect
.section
;
8426 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8428 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8429 /* Section size is divisible by both rel and rela sizes.
8430 It is of no help to us. */
8434 /* Section size is only divisible by rela. */
8435 if (use_rela_initialised
&& (use_rela
== FALSE
))
8438 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8439 bfd_set_error (bfd_error_invalid_operation
);
8445 use_rela_initialised
= TRUE
;
8449 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8451 /* Section size is only divisible by rel. */
8452 if (use_rela_initialised
&& (use_rela
== TRUE
))
8455 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8456 bfd_set_error (bfd_error_invalid_operation
);
8462 use_rela_initialised
= TRUE
;
8467 /* The section size is not divisible by either - something is wrong. */
8469 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8470 bfd_set_error (bfd_error_invalid_operation
);
8475 for (lo
= rel_dyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8476 if (lo
->type
== bfd_indirect_link_order
)
8478 asection
*o
= lo
->u
.indirect
.section
;
8480 if ((o
->size
% bed
->s
->sizeof_rela
) == 0)
8482 if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8483 /* Section size is divisible by both rel and rela sizes.
8484 It is of no help to us. */
8488 /* Section size is only divisible by rela. */
8489 if (use_rela_initialised
&& (use_rela
== FALSE
))
8492 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8493 bfd_set_error (bfd_error_invalid_operation
);
8499 use_rela_initialised
= TRUE
;
8503 else if ((o
->size
% bed
->s
->sizeof_rel
) == 0)
8505 /* Section size is only divisible by rel. */
8506 if (use_rela_initialised
&& (use_rela
== TRUE
))
8509 (_("%B: Unable to sort relocs - they are in more than one size"), abfd
);
8510 bfd_set_error (bfd_error_invalid_operation
);
8516 use_rela_initialised
= TRUE
;
8521 /* The section size is not divisible by either - something is wrong. */
8523 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd
);
8524 bfd_set_error (bfd_error_invalid_operation
);
8529 if (! use_rela_initialised
)
8533 else if (rela_dyn
!= NULL
&& rela_dyn
->size
> 0)
8535 else if (rel_dyn
!= NULL
&& rel_dyn
->size
> 0)
8542 dynamic_relocs
= rela_dyn
;
8543 ext_size
= bed
->s
->sizeof_rela
;
8544 swap_in
= bed
->s
->swap_reloca_in
;
8545 swap_out
= bed
->s
->swap_reloca_out
;
8549 dynamic_relocs
= rel_dyn
;
8550 ext_size
= bed
->s
->sizeof_rel
;
8551 swap_in
= bed
->s
->swap_reloc_in
;
8552 swap_out
= bed
->s
->swap_reloc_out
;
8556 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8557 if (lo
->type
== bfd_indirect_link_order
)
8558 size
+= lo
->u
.indirect
.section
->size
;
8560 if (size
!= dynamic_relocs
->size
)
8563 sort_elt
= (sizeof (struct elf_link_sort_rela
)
8564 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
8566 count
= dynamic_relocs
->size
/ ext_size
;
8569 sort
= (bfd_byte
*) bfd_zmalloc (sort_elt
* count
);
8573 (*info
->callbacks
->warning
)
8574 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
8578 if (bed
->s
->arch_size
== 32)
8579 r_sym_mask
= ~(bfd_vma
) 0xff;
8581 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
8583 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8584 if (lo
->type
== bfd_indirect_link_order
)
8586 bfd_byte
*erel
, *erelend
;
8587 asection
*o
= lo
->u
.indirect
.section
;
8589 if (o
->contents
== NULL
&& o
->size
!= 0)
8591 /* This is a reloc section that is being handled as a normal
8592 section. See bfd_section_from_shdr. We can't combine
8593 relocs in this case. */
8598 erelend
= o
->contents
+ o
->size
;
8599 /* FIXME: octets_per_byte. */
8600 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8602 while (erel
< erelend
)
8604 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8606 (*swap_in
) (abfd
, erel
, s
->rela
);
8607 s
->type
= (*bed
->elf_backend_reloc_type_class
) (info
, o
, s
->rela
);
8608 s
->u
.sym_mask
= r_sym_mask
;
8614 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
8616 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
8618 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8619 if (s
->type
!= reloc_class_relative
)
8625 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
8626 for (; i
< count
; i
++, p
+= sort_elt
)
8628 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
8629 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
8631 sp
->u
.offset
= sq
->rela
->r_offset
;
8634 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
8636 for (lo
= dynamic_relocs
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
8637 if (lo
->type
== bfd_indirect_link_order
)
8639 bfd_byte
*erel
, *erelend
;
8640 asection
*o
= lo
->u
.indirect
.section
;
8643 erelend
= o
->contents
+ o
->size
;
8644 /* FIXME: octets_per_byte. */
8645 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
8646 while (erel
< erelend
)
8648 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
8649 (*swap_out
) (abfd
, s
->rela
, erel
);
8656 *psec
= dynamic_relocs
;
8660 /* Add a symbol to the output symbol string table. */
8663 elf_link_output_symstrtab (struct elf_final_link_info
*flinfo
,
8665 Elf_Internal_Sym
*elfsym
,
8666 asection
*input_sec
,
8667 struct elf_link_hash_entry
*h
)
8669 int (*output_symbol_hook
)
8670 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
8671 struct elf_link_hash_entry
*);
8672 struct elf_link_hash_table
*hash_table
;
8673 const struct elf_backend_data
*bed
;
8674 bfd_size_type strtabsize
;
8676 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8678 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8679 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
8680 if (output_symbol_hook
!= NULL
)
8682 int ret
= (*output_symbol_hook
) (flinfo
->info
, name
, elfsym
, input_sec
, h
);
8689 || (input_sec
->flags
& SEC_EXCLUDE
))
8690 elfsym
->st_name
= (unsigned long) -1;
8693 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8694 to get the final offset for st_name. */
8696 = (unsigned long) _bfd_elf_strtab_add (flinfo
->symstrtab
,
8698 if (elfsym
->st_name
== (unsigned long) -1)
8702 hash_table
= elf_hash_table (flinfo
->info
);
8703 strtabsize
= hash_table
->strtabsize
;
8704 if (strtabsize
<= hash_table
->strtabcount
)
8706 strtabsize
+= strtabsize
;
8707 hash_table
->strtabsize
= strtabsize
;
8708 strtabsize
*= sizeof (*hash_table
->strtab
);
8710 = (struct elf_sym_strtab
*) bfd_realloc (hash_table
->strtab
,
8712 if (hash_table
->strtab
== NULL
)
8715 hash_table
->strtab
[hash_table
->strtabcount
].sym
= *elfsym
;
8716 hash_table
->strtab
[hash_table
->strtabcount
].dest_index
8717 = hash_table
->strtabcount
;
8718 hash_table
->strtab
[hash_table
->strtabcount
].destshndx_index
8719 = flinfo
->symshndxbuf
? bfd_get_symcount (flinfo
->output_bfd
) : 0;
8721 bfd_get_symcount (flinfo
->output_bfd
) += 1;
8722 hash_table
->strtabcount
+= 1;
8727 /* Swap symbols out to the symbol table and flush the output symbols to
8731 elf_link_swap_symbols_out (struct elf_final_link_info
*flinfo
)
8733 struct elf_link_hash_table
*hash_table
= elf_hash_table (flinfo
->info
);
8734 bfd_size_type amt
, i
;
8735 const struct elf_backend_data
*bed
;
8737 Elf_Internal_Shdr
*hdr
;
8741 if (!hash_table
->strtabcount
)
8744 BFD_ASSERT (elf_onesymtab (flinfo
->output_bfd
));
8746 bed
= get_elf_backend_data (flinfo
->output_bfd
);
8748 amt
= bed
->s
->sizeof_sym
* hash_table
->strtabcount
;
8749 symbuf
= (bfd_byte
*) bfd_malloc (amt
);
8753 if (flinfo
->symshndxbuf
)
8755 amt
= (sizeof (Elf_External_Sym_Shndx
)
8756 * (bfd_get_symcount (flinfo
->output_bfd
)));
8757 flinfo
->symshndxbuf
= (Elf_External_Sym_Shndx
*) bfd_zmalloc (amt
);
8758 if (flinfo
->symshndxbuf
== NULL
)
8765 for (i
= 0; i
< hash_table
->strtabcount
; i
++)
8767 struct elf_sym_strtab
*elfsym
= &hash_table
->strtab
[i
];
8768 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8769 elfsym
->sym
.st_name
= 0;
8772 = (unsigned long) _bfd_elf_strtab_offset (flinfo
->symstrtab
,
8773 elfsym
->sym
.st_name
);
8774 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &elfsym
->sym
,
8775 ((bfd_byte
*) symbuf
8776 + (elfsym
->dest_index
8777 * bed
->s
->sizeof_sym
)),
8778 (flinfo
->symshndxbuf
8779 + elfsym
->destshndx_index
));
8782 hdr
= &elf_tdata (flinfo
->output_bfd
)->symtab_hdr
;
8783 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
8784 amt
= hash_table
->strtabcount
* bed
->s
->sizeof_sym
;
8785 if (bfd_seek (flinfo
->output_bfd
, pos
, SEEK_SET
) == 0
8786 && bfd_bwrite (symbuf
, amt
, flinfo
->output_bfd
) == amt
)
8788 hdr
->sh_size
+= amt
;
8796 free (hash_table
->strtab
);
8797 hash_table
->strtab
= NULL
;
8802 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8805 check_dynsym (bfd
*abfd
, Elf_Internal_Sym
*sym
)
8807 if (sym
->st_shndx
>= (SHN_LORESERVE
& 0xffff)
8808 && sym
->st_shndx
< SHN_LORESERVE
)
8810 /* The gABI doesn't support dynamic symbols in output sections
8812 (*_bfd_error_handler
)
8813 (_("%B: Too many sections: %d (>= %d)"),
8814 abfd
, bfd_count_sections (abfd
), SHN_LORESERVE
& 0xffff);
8815 bfd_set_error (bfd_error_nonrepresentable_section
);
8821 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8822 allowing an unsatisfied unversioned symbol in the DSO to match a
8823 versioned symbol that would normally require an explicit version.
8824 We also handle the case that a DSO references a hidden symbol
8825 which may be satisfied by a versioned symbol in another DSO. */
8828 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
8829 const struct elf_backend_data
*bed
,
8830 struct elf_link_hash_entry
*h
)
8833 struct elf_link_loaded_list
*loaded
;
8835 if (!is_elf_hash_table (info
->hash
))
8838 /* Check indirect symbol. */
8839 while (h
->root
.type
== bfd_link_hash_indirect
)
8840 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8842 switch (h
->root
.type
)
8848 case bfd_link_hash_undefined
:
8849 case bfd_link_hash_undefweak
:
8850 abfd
= h
->root
.u
.undef
.abfd
;
8851 if ((abfd
->flags
& DYNAMIC
) == 0
8852 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
8856 case bfd_link_hash_defined
:
8857 case bfd_link_hash_defweak
:
8858 abfd
= h
->root
.u
.def
.section
->owner
;
8861 case bfd_link_hash_common
:
8862 abfd
= h
->root
.u
.c
.p
->section
->owner
;
8865 BFD_ASSERT (abfd
!= NULL
);
8867 for (loaded
= elf_hash_table (info
)->loaded
;
8869 loaded
= loaded
->next
)
8872 Elf_Internal_Shdr
*hdr
;
8873 bfd_size_type symcount
;
8874 bfd_size_type extsymcount
;
8875 bfd_size_type extsymoff
;
8876 Elf_Internal_Shdr
*versymhdr
;
8877 Elf_Internal_Sym
*isym
;
8878 Elf_Internal_Sym
*isymend
;
8879 Elf_Internal_Sym
*isymbuf
;
8880 Elf_External_Versym
*ever
;
8881 Elf_External_Versym
*extversym
;
8883 input
= loaded
->abfd
;
8885 /* We check each DSO for a possible hidden versioned definition. */
8887 || (input
->flags
& DYNAMIC
) == 0
8888 || elf_dynversym (input
) == 0)
8891 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
8893 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8894 if (elf_bad_symtab (input
))
8896 extsymcount
= symcount
;
8901 extsymcount
= symcount
- hdr
->sh_info
;
8902 extsymoff
= hdr
->sh_info
;
8905 if (extsymcount
== 0)
8908 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
8910 if (isymbuf
== NULL
)
8913 /* Read in any version definitions. */
8914 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
8915 extversym
= (Elf_External_Versym
*) bfd_malloc (versymhdr
->sh_size
);
8916 if (extversym
== NULL
)
8919 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
8920 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
8921 != versymhdr
->sh_size
))
8929 ever
= extversym
+ extsymoff
;
8930 isymend
= isymbuf
+ extsymcount
;
8931 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
8934 Elf_Internal_Versym iver
;
8935 unsigned short version_index
;
8937 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
8938 || isym
->st_shndx
== SHN_UNDEF
)
8941 name
= bfd_elf_string_from_elf_section (input
,
8944 if (strcmp (name
, h
->root
.root
.string
) != 0)
8947 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
8949 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
8951 && h
->forced_local
))
8953 /* If we have a non-hidden versioned sym, then it should
8954 have provided a definition for the undefined sym unless
8955 it is defined in a non-shared object and forced local.
8960 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
8961 if (version_index
== 1 || version_index
== 2)
8963 /* This is the base or first version. We can use it. */
8977 /* Add an external symbol to the symbol table. This is called from
8978 the hash table traversal routine. When generating a shared object,
8979 we go through the symbol table twice. The first time we output
8980 anything that might have been forced to local scope in a version
8981 script. The second time we output the symbols that are still
8985 elf_link_output_extsym (struct bfd_hash_entry
*bh
, void *data
)
8987 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) bh
;
8988 struct elf_outext_info
*eoinfo
= (struct elf_outext_info
*) data
;
8989 struct elf_final_link_info
*flinfo
= eoinfo
->flinfo
;
8991 Elf_Internal_Sym sym
;
8992 asection
*input_sec
;
8993 const struct elf_backend_data
*bed
;
8996 /* A symbol is bound locally if it is forced local or it is locally
8997 defined, hidden versioned, not referenced by shared library and
8998 not exported when linking executable. */
8999 bfd_boolean local_bind
= (h
->forced_local
9000 || (bfd_link_executable (flinfo
->info
)
9001 && !flinfo
->info
->export_dynamic
9005 && h
->versioned
== versioned_hidden
));
9007 if (h
->root
.type
== bfd_link_hash_warning
)
9009 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9010 if (h
->root
.type
== bfd_link_hash_new
)
9014 /* Decide whether to output this symbol in this pass. */
9015 if (eoinfo
->localsyms
)
9026 bed
= get_elf_backend_data (flinfo
->output_bfd
);
9028 if (h
->root
.type
== bfd_link_hash_undefined
)
9030 /* If we have an undefined symbol reference here then it must have
9031 come from a shared library that is being linked in. (Undefined
9032 references in regular files have already been handled unless
9033 they are in unreferenced sections which are removed by garbage
9035 bfd_boolean ignore_undef
= FALSE
;
9037 /* Some symbols may be special in that the fact that they're
9038 undefined can be safely ignored - let backend determine that. */
9039 if (bed
->elf_backend_ignore_undef_symbol
)
9040 ignore_undef
= bed
->elf_backend_ignore_undef_symbol (h
);
9042 /* If we are reporting errors for this situation then do so now. */
9045 && (!h
->ref_regular
|| flinfo
->info
->gc_sections
)
9046 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
)
9047 && flinfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
9049 if (!(flinfo
->info
->callbacks
->undefined_symbol
9050 (flinfo
->info
, h
->root
.root
.string
,
9051 h
->ref_regular
? NULL
: h
->root
.u
.undef
.abfd
,
9053 (flinfo
->info
->unresolved_syms_in_shared_libs
9054 == RM_GENERATE_ERROR
))))
9056 bfd_set_error (bfd_error_bad_value
);
9057 eoinfo
->failed
= TRUE
;
9063 /* We should also warn if a forced local symbol is referenced from
9064 shared libraries. */
9065 if (bfd_link_executable (flinfo
->info
)
9070 && h
->ref_dynamic_nonweak
9071 && !elf_link_check_versioned_symbol (flinfo
->info
, bed
, h
))
9075 struct elf_link_hash_entry
*hi
= h
;
9077 /* Check indirect symbol. */
9078 while (hi
->root
.type
== bfd_link_hash_indirect
)
9079 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
9081 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
)
9082 msg
= _("%B: internal symbol `%s' in %B is referenced by DSO");
9083 else if (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
9084 msg
= _("%B: hidden symbol `%s' in %B is referenced by DSO");
9086 msg
= _("%B: local symbol `%s' in %B is referenced by DSO");
9087 def_bfd
= flinfo
->output_bfd
;
9088 if (hi
->root
.u
.def
.section
!= bfd_abs_section_ptr
)
9089 def_bfd
= hi
->root
.u
.def
.section
->owner
;
9090 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, def_bfd
,
9091 h
->root
.root
.string
);
9092 bfd_set_error (bfd_error_bad_value
);
9093 eoinfo
->failed
= TRUE
;
9097 /* We don't want to output symbols that have never been mentioned by
9098 a regular file, or that we have been told to strip. However, if
9099 h->indx is set to -2, the symbol is used by a reloc and we must
9104 else if ((h
->def_dynamic
9106 || h
->root
.type
== bfd_link_hash_new
)
9110 else if (flinfo
->info
->strip
== strip_all
)
9112 else if (flinfo
->info
->strip
== strip_some
9113 && bfd_hash_lookup (flinfo
->info
->keep_hash
,
9114 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
9116 else if ((h
->root
.type
== bfd_link_hash_defined
9117 || h
->root
.type
== bfd_link_hash_defweak
)
9118 && ((flinfo
->info
->strip_discarded
9119 && discarded_section (h
->root
.u
.def
.section
))
9120 || ((h
->root
.u
.def
.section
->flags
& SEC_LINKER_CREATED
) == 0
9121 && h
->root
.u
.def
.section
->owner
!= NULL
9122 && (h
->root
.u
.def
.section
->owner
->flags
& BFD_PLUGIN
) != 0)))
9124 else if ((h
->root
.type
== bfd_link_hash_undefined
9125 || h
->root
.type
== bfd_link_hash_undefweak
)
9126 && h
->root
.u
.undef
.abfd
!= NULL
9127 && (h
->root
.u
.undef
.abfd
->flags
& BFD_PLUGIN
) != 0)
9130 /* If we're stripping it, and it's not a dynamic symbol, there's
9131 nothing else to do. However, if it is a forced local symbol or
9132 an ifunc symbol we need to give the backend finish_dynamic_symbol
9133 function a chance to make it dynamic. */
9136 && h
->type
!= STT_GNU_IFUNC
9137 && !h
->forced_local
)
9141 sym
.st_size
= h
->size
;
9142 sym
.st_other
= h
->other
;
9145 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
9146 /* Turn off visibility on local symbol. */
9147 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
9149 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9150 else if (h
->unique_global
&& h
->def_regular
)
9151 sym
.st_info
= ELF_ST_INFO (STB_GNU_UNIQUE
, h
->type
);
9152 else if (h
->root
.type
== bfd_link_hash_undefweak
9153 || h
->root
.type
== bfd_link_hash_defweak
)
9154 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
9156 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
9157 sym
.st_target_internal
= h
->target_internal
;
9159 switch (h
->root
.type
)
9162 case bfd_link_hash_new
:
9163 case bfd_link_hash_warning
:
9167 case bfd_link_hash_undefined
:
9168 case bfd_link_hash_undefweak
:
9169 input_sec
= bfd_und_section_ptr
;
9170 sym
.st_shndx
= SHN_UNDEF
;
9173 case bfd_link_hash_defined
:
9174 case bfd_link_hash_defweak
:
9176 input_sec
= h
->root
.u
.def
.section
;
9177 if (input_sec
->output_section
!= NULL
)
9180 _bfd_elf_section_from_bfd_section (flinfo
->output_bfd
,
9181 input_sec
->output_section
);
9182 if (sym
.st_shndx
== SHN_BAD
)
9184 (*_bfd_error_handler
)
9185 (_("%B: could not find output section %A for input section %A"),
9186 flinfo
->output_bfd
, input_sec
->output_section
, input_sec
);
9187 bfd_set_error (bfd_error_nonrepresentable_section
);
9188 eoinfo
->failed
= TRUE
;
9192 /* ELF symbols in relocatable files are section relative,
9193 but in nonrelocatable files they are virtual
9195 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
9196 if (!bfd_link_relocatable (flinfo
->info
))
9198 sym
.st_value
+= input_sec
->output_section
->vma
;
9199 if (h
->type
== STT_TLS
)
9201 asection
*tls_sec
= elf_hash_table (flinfo
->info
)->tls_sec
;
9202 if (tls_sec
!= NULL
)
9203 sym
.st_value
-= tls_sec
->vma
;
9209 BFD_ASSERT (input_sec
->owner
== NULL
9210 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
9211 sym
.st_shndx
= SHN_UNDEF
;
9212 input_sec
= bfd_und_section_ptr
;
9217 case bfd_link_hash_common
:
9218 input_sec
= h
->root
.u
.c
.p
->section
;
9219 sym
.st_shndx
= bed
->common_section_index (input_sec
);
9220 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
9223 case bfd_link_hash_indirect
:
9224 /* These symbols are created by symbol versioning. They point
9225 to the decorated version of the name. For example, if the
9226 symbol foo@@GNU_1.2 is the default, which should be used when
9227 foo is used with no version, then we add an indirect symbol
9228 foo which points to foo@@GNU_1.2. We ignore these symbols,
9229 since the indirected symbol is already in the hash table. */
9233 /* Give the processor backend a chance to tweak the symbol value,
9234 and also to finish up anything that needs to be done for this
9235 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9236 forced local syms when non-shared is due to a historical quirk.
9237 STT_GNU_IFUNC symbol must go through PLT. */
9238 if ((h
->type
== STT_GNU_IFUNC
9240 && !bfd_link_relocatable (flinfo
->info
))
9241 || ((h
->dynindx
!= -1
9243 && ((bfd_link_pic (flinfo
->info
)
9244 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
9245 || h
->root
.type
!= bfd_link_hash_undefweak
))
9246 || !h
->forced_local
)
9247 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
))
9249 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
9250 (flinfo
->output_bfd
, flinfo
->info
, h
, &sym
)))
9252 eoinfo
->failed
= TRUE
;
9257 /* If we are marking the symbol as undefined, and there are no
9258 non-weak references to this symbol from a regular object, then
9259 mark the symbol as weak undefined; if there are non-weak
9260 references, mark the symbol as strong. We can't do this earlier,
9261 because it might not be marked as undefined until the
9262 finish_dynamic_symbol routine gets through with it. */
9263 if (sym
.st_shndx
== SHN_UNDEF
9265 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
9266 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
9269 unsigned int type
= ELF_ST_TYPE (sym
.st_info
);
9271 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9272 if (type
== STT_GNU_IFUNC
)
9275 if (h
->ref_regular_nonweak
)
9276 bindtype
= STB_GLOBAL
;
9278 bindtype
= STB_WEAK
;
9279 sym
.st_info
= ELF_ST_INFO (bindtype
, type
);
9282 /* If this is a symbol defined in a dynamic library, don't use the
9283 symbol size from the dynamic library. Relinking an executable
9284 against a new library may introduce gratuitous changes in the
9285 executable's symbols if we keep the size. */
9286 if (sym
.st_shndx
== SHN_UNDEF
9291 /* If a non-weak symbol with non-default visibility is not defined
9292 locally, it is a fatal error. */
9293 if (!bfd_link_relocatable (flinfo
->info
)
9294 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
9295 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
9296 && h
->root
.type
== bfd_link_hash_undefined
9301 if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
)
9302 msg
= _("%B: protected symbol `%s' isn't defined");
9303 else if (ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
)
9304 msg
= _("%B: internal symbol `%s' isn't defined");
9306 msg
= _("%B: hidden symbol `%s' isn't defined");
9307 (*_bfd_error_handler
) (msg
, flinfo
->output_bfd
, h
->root
.root
.string
);
9308 bfd_set_error (bfd_error_bad_value
);
9309 eoinfo
->failed
= TRUE
;
9313 /* If this symbol should be put in the .dynsym section, then put it
9314 there now. We already know the symbol index. We also fill in
9315 the entry in the .hash section. */
9316 if (elf_hash_table (flinfo
->info
)->dynsym
!= NULL
9318 && elf_hash_table (flinfo
->info
)->dynamic_sections_created
)
9322 /* Since there is no version information in the dynamic string,
9323 if there is no version info in symbol version section, we will
9324 have a run-time problem if not linking executable, referenced
9325 by shared library, not locally defined, or not bound locally.
9327 if (h
->verinfo
.verdef
== NULL
9329 && (!bfd_link_executable (flinfo
->info
)
9331 || !h
->def_regular
))
9333 char *p
= strrchr (h
->root
.root
.string
, ELF_VER_CHR
);
9335 if (p
&& p
[1] != '\0')
9337 (*_bfd_error_handler
)
9338 (_("%B: No symbol version section for versioned symbol `%s'"),
9339 flinfo
->output_bfd
, h
->root
.root
.string
);
9340 eoinfo
->failed
= TRUE
;
9345 sym
.st_name
= h
->dynstr_index
;
9346 esym
= (elf_hash_table (flinfo
->info
)->dynsym
->contents
9347 + h
->dynindx
* bed
->s
->sizeof_sym
);
9348 if (!check_dynsym (flinfo
->output_bfd
, &sym
))
9350 eoinfo
->failed
= TRUE
;
9353 bed
->s
->swap_symbol_out (flinfo
->output_bfd
, &sym
, esym
, 0);
9355 if (flinfo
->hash_sec
!= NULL
)
9357 size_t hash_entry_size
;
9358 bfd_byte
*bucketpos
;
9363 bucketcount
= elf_hash_table (flinfo
->info
)->bucketcount
;
9364 bucket
= h
->u
.elf_hash_value
% bucketcount
;
9367 = elf_section_data (flinfo
->hash_sec
)->this_hdr
.sh_entsize
;
9368 bucketpos
= ((bfd_byte
*) flinfo
->hash_sec
->contents
9369 + (bucket
+ 2) * hash_entry_size
);
9370 chain
= bfd_get (8 * hash_entry_size
, flinfo
->output_bfd
, bucketpos
);
9371 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, h
->dynindx
,
9373 bfd_put (8 * hash_entry_size
, flinfo
->output_bfd
, chain
,
9374 ((bfd_byte
*) flinfo
->hash_sec
->contents
9375 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
9378 if (flinfo
->symver_sec
!= NULL
&& flinfo
->symver_sec
->contents
!= NULL
)
9380 Elf_Internal_Versym iversym
;
9381 Elf_External_Versym
*eversym
;
9383 if (!h
->def_regular
)
9385 if (h
->verinfo
.verdef
== NULL
9386 || (elf_dyn_lib_class (h
->verinfo
.verdef
->vd_bfd
)
9387 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
| DYN_NO_NEEDED
)))
9388 iversym
.vs_vers
= 0;
9390 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
9394 if (h
->verinfo
.vertree
== NULL
)
9395 iversym
.vs_vers
= 1;
9397 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
9398 if (flinfo
->info
->create_default_symver
)
9402 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9404 if (h
->versioned
== versioned_hidden
&& h
->def_regular
)
9405 iversym
.vs_vers
|= VERSYM_HIDDEN
;
9407 eversym
= (Elf_External_Versym
*) flinfo
->symver_sec
->contents
;
9408 eversym
+= h
->dynindx
;
9409 _bfd_elf_swap_versym_out (flinfo
->output_bfd
, &iversym
, eversym
);
9413 /* If the symbol is undefined, and we didn't output it to .dynsym,
9414 strip it from .symtab too. Obviously we can't do this for
9415 relocatable output or when needed for --emit-relocs. */
9416 else if (input_sec
== bfd_und_section_ptr
9418 && !bfd_link_relocatable (flinfo
->info
))
9420 /* Also strip others that we couldn't earlier due to dynamic symbol
9424 if ((input_sec
->flags
& SEC_EXCLUDE
) != 0)
9427 /* Output a FILE symbol so that following locals are not associated
9428 with the wrong input file. We need one for forced local symbols
9429 if we've seen more than one FILE symbol or when we have exactly
9430 one FILE symbol but global symbols are present in a file other
9431 than the one with the FILE symbol. We also need one if linker
9432 defined symbols are present. In practice these conditions are
9433 always met, so just emit the FILE symbol unconditionally. */
9434 if (eoinfo
->localsyms
9435 && !eoinfo
->file_sym_done
9436 && eoinfo
->flinfo
->filesym_count
!= 0)
9438 Elf_Internal_Sym fsym
;
9440 memset (&fsym
, 0, sizeof (fsym
));
9441 fsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9442 fsym
.st_shndx
= SHN_ABS
;
9443 if (!elf_link_output_symstrtab (eoinfo
->flinfo
, NULL
, &fsym
,
9444 bfd_und_section_ptr
, NULL
))
9447 eoinfo
->file_sym_done
= TRUE
;
9450 indx
= bfd_get_symcount (flinfo
->output_bfd
);
9451 ret
= elf_link_output_symstrtab (flinfo
, h
->root
.root
.string
, &sym
,
9455 eoinfo
->failed
= TRUE
;
9460 else if (h
->indx
== -2)
9466 /* Return TRUE if special handling is done for relocs in SEC against
9467 symbols defined in discarded sections. */
9470 elf_section_ignore_discarded_relocs (asection
*sec
)
9472 const struct elf_backend_data
*bed
;
9474 switch (sec
->sec_info_type
)
9476 case SEC_INFO_TYPE_STABS
:
9477 case SEC_INFO_TYPE_EH_FRAME
:
9478 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
9484 bed
= get_elf_backend_data (sec
->owner
);
9485 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
9486 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
9492 /* Return a mask saying how ld should treat relocations in SEC against
9493 symbols defined in discarded sections. If this function returns
9494 COMPLAIN set, ld will issue a warning message. If this function
9495 returns PRETEND set, and the discarded section was link-once and the
9496 same size as the kept link-once section, ld will pretend that the
9497 symbol was actually defined in the kept section. Otherwise ld will
9498 zero the reloc (at least that is the intent, but some cooperation by
9499 the target dependent code is needed, particularly for REL targets). */
9502 _bfd_elf_default_action_discarded (asection
*sec
)
9504 if (sec
->flags
& SEC_DEBUGGING
)
9507 if (strcmp (".eh_frame", sec
->name
) == 0)
9510 if (strcmp (".gcc_except_table", sec
->name
) == 0)
9513 return COMPLAIN
| PRETEND
;
9516 /* Find a match between a section and a member of a section group. */
9519 match_group_member (asection
*sec
, asection
*group
,
9520 struct bfd_link_info
*info
)
9522 asection
*first
= elf_next_in_group (group
);
9523 asection
*s
= first
;
9527 if (bfd_elf_match_symbols_in_sections (s
, sec
, info
))
9530 s
= elf_next_in_group (s
);
9538 /* Check if the kept section of a discarded section SEC can be used
9539 to replace it. Return the replacement if it is OK. Otherwise return
9543 _bfd_elf_check_kept_section (asection
*sec
, struct bfd_link_info
*info
)
9547 kept
= sec
->kept_section
;
9550 if ((kept
->flags
& SEC_GROUP
) != 0)
9551 kept
= match_group_member (sec
, kept
, info
);
9553 && ((sec
->rawsize
!= 0 ? sec
->rawsize
: sec
->size
)
9554 != (kept
->rawsize
!= 0 ? kept
->rawsize
: kept
->size
)))
9556 sec
->kept_section
= kept
;
9561 /* Link an input file into the linker output file. This function
9562 handles all the sections and relocations of the input file at once.
9563 This is so that we only have to read the local symbols once, and
9564 don't have to keep them in memory. */
9567 elf_link_input_bfd (struct elf_final_link_info
*flinfo
, bfd
*input_bfd
)
9569 int (*relocate_section
)
9570 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
9571 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
9573 Elf_Internal_Shdr
*symtab_hdr
;
9576 Elf_Internal_Sym
*isymbuf
;
9577 Elf_Internal_Sym
*isym
;
9578 Elf_Internal_Sym
*isymend
;
9580 asection
**ppsection
;
9582 const struct elf_backend_data
*bed
;
9583 struct elf_link_hash_entry
**sym_hashes
;
9584 bfd_size_type address_size
;
9585 bfd_vma r_type_mask
;
9587 bfd_boolean have_file_sym
= FALSE
;
9589 output_bfd
= flinfo
->output_bfd
;
9590 bed
= get_elf_backend_data (output_bfd
);
9591 relocate_section
= bed
->elf_backend_relocate_section
;
9593 /* If this is a dynamic object, we don't want to do anything here:
9594 we don't want the local symbols, and we don't want the section
9596 if ((input_bfd
->flags
& DYNAMIC
) != 0)
9599 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
9600 if (elf_bad_symtab (input_bfd
))
9602 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9607 locsymcount
= symtab_hdr
->sh_info
;
9608 extsymoff
= symtab_hdr
->sh_info
;
9611 /* Read the local symbols. */
9612 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9613 if (isymbuf
== NULL
&& locsymcount
!= 0)
9615 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
9616 flinfo
->internal_syms
,
9617 flinfo
->external_syms
,
9618 flinfo
->locsym_shndx
);
9619 if (isymbuf
== NULL
)
9623 /* Find local symbol sections and adjust values of symbols in
9624 SEC_MERGE sections. Write out those local symbols we know are
9625 going into the output file. */
9626 isymend
= isymbuf
+ locsymcount
;
9627 for (isym
= isymbuf
, pindex
= flinfo
->indices
, ppsection
= flinfo
->sections
;
9629 isym
++, pindex
++, ppsection
++)
9633 Elf_Internal_Sym osym
;
9639 if (elf_bad_symtab (input_bfd
))
9641 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
9648 if (isym
->st_shndx
== SHN_UNDEF
)
9649 isec
= bfd_und_section_ptr
;
9650 else if (isym
->st_shndx
== SHN_ABS
)
9651 isec
= bfd_abs_section_ptr
;
9652 else if (isym
->st_shndx
== SHN_COMMON
)
9653 isec
= bfd_com_section_ptr
;
9656 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
9659 /* Don't attempt to output symbols with st_shnx in the
9660 reserved range other than SHN_ABS and SHN_COMMON. */
9664 else if (isec
->sec_info_type
== SEC_INFO_TYPE_MERGE
9665 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
9667 _bfd_merged_section_offset (output_bfd
, &isec
,
9668 elf_section_data (isec
)->sec_info
,
9674 /* Don't output the first, undefined, symbol. In fact, don't
9675 output any undefined local symbol. */
9676 if (isec
== bfd_und_section_ptr
)
9679 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
9681 /* We never output section symbols. Instead, we use the
9682 section symbol of the corresponding section in the output
9687 /* If we are stripping all symbols, we don't want to output this
9689 if (flinfo
->info
->strip
== strip_all
)
9692 /* If we are discarding all local symbols, we don't want to
9693 output this one. If we are generating a relocatable output
9694 file, then some of the local symbols may be required by
9695 relocs; we output them below as we discover that they are
9697 if (flinfo
->info
->discard
== discard_all
)
9700 /* If this symbol is defined in a section which we are
9701 discarding, we don't need to keep it. */
9702 if (isym
->st_shndx
!= SHN_UNDEF
9703 && isym
->st_shndx
< SHN_LORESERVE
9704 && bfd_section_removed_from_list (output_bfd
,
9705 isec
->output_section
))
9708 /* Get the name of the symbol. */
9709 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
9714 /* See if we are discarding symbols with this name. */
9715 if ((flinfo
->info
->strip
== strip_some
9716 && (bfd_hash_lookup (flinfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
9718 || (((flinfo
->info
->discard
== discard_sec_merge
9719 && (isec
->flags
& SEC_MERGE
)
9720 && !bfd_link_relocatable (flinfo
->info
))
9721 || flinfo
->info
->discard
== discard_l
)
9722 && bfd_is_local_label_name (input_bfd
, name
)))
9725 if (ELF_ST_TYPE (isym
->st_info
) == STT_FILE
)
9727 if (input_bfd
->lto_output
)
9728 /* -flto puts a temp file name here. This means builds
9729 are not reproducible. Discard the symbol. */
9731 have_file_sym
= TRUE
;
9732 flinfo
->filesym_count
+= 1;
9736 /* In the absence of debug info, bfd_find_nearest_line uses
9737 FILE symbols to determine the source file for local
9738 function symbols. Provide a FILE symbol here if input
9739 files lack such, so that their symbols won't be
9740 associated with a previous input file. It's not the
9741 source file, but the best we can do. */
9742 have_file_sym
= TRUE
;
9743 flinfo
->filesym_count
+= 1;
9744 memset (&osym
, 0, sizeof (osym
));
9745 osym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
9746 osym
.st_shndx
= SHN_ABS
;
9747 if (!elf_link_output_symstrtab (flinfo
,
9748 (input_bfd
->lto_output
? NULL
9749 : input_bfd
->filename
),
9750 &osym
, bfd_abs_section_ptr
,
9757 /* Adjust the section index for the output file. */
9758 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9759 isec
->output_section
);
9760 if (osym
.st_shndx
== SHN_BAD
)
9763 /* ELF symbols in relocatable files are section relative, but
9764 in executable files they are virtual addresses. Note that
9765 this code assumes that all ELF sections have an associated
9766 BFD section with a reasonable value for output_offset; below
9767 we assume that they also have a reasonable value for
9768 output_section. Any special sections must be set up to meet
9769 these requirements. */
9770 osym
.st_value
+= isec
->output_offset
;
9771 if (!bfd_link_relocatable (flinfo
->info
))
9773 osym
.st_value
+= isec
->output_section
->vma
;
9774 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
9776 /* STT_TLS symbols are relative to PT_TLS segment base. */
9777 BFD_ASSERT (elf_hash_table (flinfo
->info
)->tls_sec
!= NULL
);
9778 osym
.st_value
-= elf_hash_table (flinfo
->info
)->tls_sec
->vma
;
9782 indx
= bfd_get_symcount (output_bfd
);
9783 ret
= elf_link_output_symstrtab (flinfo
, name
, &osym
, isec
, NULL
);
9790 if (bed
->s
->arch_size
== 32)
9798 r_type_mask
= 0xffffffff;
9803 /* Relocate the contents of each section. */
9804 sym_hashes
= elf_sym_hashes (input_bfd
);
9805 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
9809 if (! o
->linker_mark
)
9811 /* This section was omitted from the link. */
9815 if (bfd_link_relocatable (flinfo
->info
)
9816 && (o
->flags
& (SEC_LINKER_CREATED
| SEC_GROUP
)) == SEC_GROUP
)
9818 /* Deal with the group signature symbol. */
9819 struct bfd_elf_section_data
*sec_data
= elf_section_data (o
);
9820 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
9821 asection
*osec
= o
->output_section
;
9823 if (symndx
>= locsymcount
9824 || (elf_bad_symtab (input_bfd
)
9825 && flinfo
->sections
[symndx
] == NULL
))
9827 struct elf_link_hash_entry
*h
= sym_hashes
[symndx
- extsymoff
];
9828 while (h
->root
.type
== bfd_link_hash_indirect
9829 || h
->root
.type
== bfd_link_hash_warning
)
9830 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9831 /* Arrange for symbol to be output. */
9833 elf_section_data (osec
)->this_hdr
.sh_info
= -2;
9835 else if (ELF_ST_TYPE (isymbuf
[symndx
].st_info
) == STT_SECTION
)
9837 /* We'll use the output section target_index. */
9838 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
9839 elf_section_data (osec
)->this_hdr
.sh_info
= sec
->target_index
;
9843 if (flinfo
->indices
[symndx
] == -1)
9845 /* Otherwise output the local symbol now. */
9846 Elf_Internal_Sym sym
= isymbuf
[symndx
];
9847 asection
*sec
= flinfo
->sections
[symndx
]->output_section
;
9852 name
= bfd_elf_string_from_elf_section (input_bfd
,
9853 symtab_hdr
->sh_link
,
9858 sym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
9860 if (sym
.st_shndx
== SHN_BAD
)
9863 sym
.st_value
+= o
->output_offset
;
9865 indx
= bfd_get_symcount (output_bfd
);
9866 ret
= elf_link_output_symstrtab (flinfo
, name
, &sym
, o
,
9871 flinfo
->indices
[symndx
] = indx
;
9875 elf_section_data (osec
)->this_hdr
.sh_info
9876 = flinfo
->indices
[symndx
];
9880 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
9881 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
9884 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
9886 /* Section was created by _bfd_elf_link_create_dynamic_sections
9891 /* Get the contents of the section. They have been cached by a
9892 relaxation routine. Note that o is a section in an input
9893 file, so the contents field will not have been set by any of
9894 the routines which work on output files. */
9895 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
9897 contents
= elf_section_data (o
)->this_hdr
.contents
;
9898 if (bed
->caches_rawsize
9900 && o
->rawsize
< o
->size
)
9902 memcpy (flinfo
->contents
, contents
, o
->rawsize
);
9903 contents
= flinfo
->contents
;
9908 contents
= flinfo
->contents
;
9909 if (! bfd_get_full_section_contents (input_bfd
, o
, &contents
))
9913 if ((o
->flags
& SEC_RELOC
) != 0)
9915 Elf_Internal_Rela
*internal_relocs
;
9916 Elf_Internal_Rela
*rel
, *relend
;
9917 int action_discarded
;
9920 /* Get the swapped relocs. */
9922 = _bfd_elf_link_read_relocs (input_bfd
, o
, flinfo
->external_relocs
,
9923 flinfo
->internal_relocs
, FALSE
);
9924 if (internal_relocs
== NULL
9925 && o
->reloc_count
> 0)
9928 /* We need to reverse-copy input .ctors/.dtors sections if
9929 they are placed in .init_array/.finit_array for output. */
9930 if (o
->size
> address_size
9931 && ((strncmp (o
->name
, ".ctors", 6) == 0
9932 && strcmp (o
->output_section
->name
,
9933 ".init_array") == 0)
9934 || (strncmp (o
->name
, ".dtors", 6) == 0
9935 && strcmp (o
->output_section
->name
,
9936 ".fini_array") == 0))
9937 && (o
->name
[6] == 0 || o
->name
[6] == '.'))
9939 if (o
->size
!= o
->reloc_count
* address_size
)
9941 (*_bfd_error_handler
)
9942 (_("error: %B: size of section %A is not "
9943 "multiple of address size"),
9945 bfd_set_error (bfd_error_on_input
);
9948 o
->flags
|= SEC_ELF_REVERSE_COPY
;
9951 action_discarded
= -1;
9952 if (!elf_section_ignore_discarded_relocs (o
))
9953 action_discarded
= (*bed
->action_discarded
) (o
);
9955 /* Run through the relocs evaluating complex reloc symbols and
9956 looking for relocs against symbols from discarded sections
9957 or section symbols from removed link-once sections.
9958 Complain about relocs against discarded sections. Zero
9959 relocs against removed link-once sections. */
9961 rel
= internal_relocs
;
9962 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9963 for ( ; rel
< relend
; rel
++)
9965 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
9966 unsigned int s_type
;
9967 asection
**ps
, *sec
;
9968 struct elf_link_hash_entry
*h
= NULL
;
9969 const char *sym_name
;
9971 if (r_symndx
== STN_UNDEF
)
9974 if (r_symndx
>= locsymcount
9975 || (elf_bad_symtab (input_bfd
)
9976 && flinfo
->sections
[r_symndx
] == NULL
))
9978 h
= sym_hashes
[r_symndx
- extsymoff
];
9980 /* Badly formatted input files can contain relocs that
9981 reference non-existant symbols. Check here so that
9982 we do not seg fault. */
9987 sprintf_vma (buffer
, rel
->r_info
);
9988 (*_bfd_error_handler
)
9989 (_("error: %B contains a reloc (0x%s) for section %A "
9990 "that references a non-existent global symbol"),
9991 input_bfd
, o
, buffer
);
9992 bfd_set_error (bfd_error_bad_value
);
9996 while (h
->root
.type
== bfd_link_hash_indirect
9997 || h
->root
.type
== bfd_link_hash_warning
)
9998 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
10002 /* If a plugin symbol is referenced from a non-IR file,
10003 mark the symbol as undefined. Note that the
10004 linker may attach linker created dynamic sections
10005 to the plugin bfd. Symbols defined in linker
10006 created sections are not plugin symbols. */
10007 if (h
->root
.non_ir_ref
10008 && (h
->root
.type
== bfd_link_hash_defined
10009 || h
->root
.type
== bfd_link_hash_defweak
)
10010 && (h
->root
.u
.def
.section
->flags
10011 & SEC_LINKER_CREATED
) == 0
10012 && h
->root
.u
.def
.section
->owner
!= NULL
10013 && (h
->root
.u
.def
.section
->owner
->flags
10014 & BFD_PLUGIN
) != 0)
10016 h
->root
.type
= bfd_link_hash_undefined
;
10017 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
10021 if (h
->root
.type
== bfd_link_hash_defined
10022 || h
->root
.type
== bfd_link_hash_defweak
)
10023 ps
= &h
->root
.u
.def
.section
;
10025 sym_name
= h
->root
.root
.string
;
10029 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
10031 s_type
= ELF_ST_TYPE (sym
->st_info
);
10032 ps
= &flinfo
->sections
[r_symndx
];
10033 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
,
10037 if ((s_type
== STT_RELC
|| s_type
== STT_SRELC
)
10038 && !bfd_link_relocatable (flinfo
->info
))
10041 bfd_vma dot
= (rel
->r_offset
10042 + o
->output_offset
+ o
->output_section
->vma
);
10044 printf ("Encountered a complex symbol!");
10045 printf (" (input_bfd %s, section %s, reloc %ld\n",
10046 input_bfd
->filename
, o
->name
,
10047 (long) (rel
- internal_relocs
));
10048 printf (" symbol: idx %8.8lx, name %s\n",
10049 r_symndx
, sym_name
);
10050 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10051 (unsigned long) rel
->r_info
,
10052 (unsigned long) rel
->r_offset
);
10054 if (!eval_symbol (&val
, &sym_name
, input_bfd
, flinfo
, dot
,
10055 isymbuf
, locsymcount
, s_type
== STT_SRELC
))
10058 /* Symbol evaluated OK. Update to absolute value. */
10059 set_symbol_value (input_bfd
, isymbuf
, locsymcount
,
10064 if (action_discarded
!= -1 && ps
!= NULL
)
10066 /* Complain if the definition comes from a
10067 discarded section. */
10068 if ((sec
= *ps
) != NULL
&& discarded_section (sec
))
10070 BFD_ASSERT (r_symndx
!= STN_UNDEF
);
10071 if (action_discarded
& COMPLAIN
)
10072 (*flinfo
->info
->callbacks
->einfo
)
10073 (_("%X`%s' referenced in section `%A' of %B: "
10074 "defined in discarded section `%A' of %B\n"),
10075 sym_name
, o
, input_bfd
, sec
, sec
->owner
);
10077 /* Try to do the best we can to support buggy old
10078 versions of gcc. Pretend that the symbol is
10079 really defined in the kept linkonce section.
10080 FIXME: This is quite broken. Modifying the
10081 symbol here means we will be changing all later
10082 uses of the symbol, not just in this section. */
10083 if (action_discarded
& PRETEND
)
10087 kept
= _bfd_elf_check_kept_section (sec
,
10099 /* Relocate the section by invoking a back end routine.
10101 The back end routine is responsible for adjusting the
10102 section contents as necessary, and (if using Rela relocs
10103 and generating a relocatable output file) adjusting the
10104 reloc addend as necessary.
10106 The back end routine does not have to worry about setting
10107 the reloc address or the reloc symbol index.
10109 The back end routine is given a pointer to the swapped in
10110 internal symbols, and can access the hash table entries
10111 for the external symbols via elf_sym_hashes (input_bfd).
10113 When generating relocatable output, the back end routine
10114 must handle STB_LOCAL/STT_SECTION symbols specially. The
10115 output symbol is going to be a section symbol
10116 corresponding to the output section, which will require
10117 the addend to be adjusted. */
10119 ret
= (*relocate_section
) (output_bfd
, flinfo
->info
,
10120 input_bfd
, o
, contents
,
10128 || bfd_link_relocatable (flinfo
->info
)
10129 || flinfo
->info
->emitrelocations
)
10131 Elf_Internal_Rela
*irela
;
10132 Elf_Internal_Rela
*irelaend
, *irelamid
;
10133 bfd_vma last_offset
;
10134 struct elf_link_hash_entry
**rel_hash
;
10135 struct elf_link_hash_entry
**rel_hash_list
, **rela_hash_list
;
10136 Elf_Internal_Shdr
*input_rel_hdr
, *input_rela_hdr
;
10137 unsigned int next_erel
;
10138 bfd_boolean rela_normal
;
10139 struct bfd_elf_section_data
*esdi
, *esdo
;
10141 esdi
= elf_section_data (o
);
10142 esdo
= elf_section_data (o
->output_section
);
10143 rela_normal
= FALSE
;
10145 /* Adjust the reloc addresses and symbol indices. */
10147 irela
= internal_relocs
;
10148 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
10149 rel_hash
= esdo
->rel
.hashes
+ esdo
->rel
.count
;
10150 /* We start processing the REL relocs, if any. When we reach
10151 IRELAMID in the loop, we switch to the RELA relocs. */
10153 if (esdi
->rel
.hdr
!= NULL
)
10154 irelamid
+= (NUM_SHDR_ENTRIES (esdi
->rel
.hdr
)
10155 * bed
->s
->int_rels_per_ext_rel
);
10156 rel_hash_list
= rel_hash
;
10157 rela_hash_list
= NULL
;
10158 last_offset
= o
->output_offset
;
10159 if (!bfd_link_relocatable (flinfo
->info
))
10160 last_offset
+= o
->output_section
->vma
;
10161 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
10163 unsigned long r_symndx
;
10165 Elf_Internal_Sym sym
;
10167 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
10173 if (irela
== irelamid
)
10175 rel_hash
= esdo
->rela
.hashes
+ esdo
->rela
.count
;
10176 rela_hash_list
= rel_hash
;
10177 rela_normal
= bed
->rela_normal
;
10180 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
10183 if (irela
->r_offset
>= (bfd_vma
) -2)
10185 /* This is a reloc for a deleted entry or somesuch.
10186 Turn it into an R_*_NONE reloc, at the same
10187 offset as the last reloc. elf_eh_frame.c and
10188 bfd_elf_discard_info rely on reloc offsets
10190 irela
->r_offset
= last_offset
;
10192 irela
->r_addend
= 0;
10196 irela
->r_offset
+= o
->output_offset
;
10198 /* Relocs in an executable have to be virtual addresses. */
10199 if (!bfd_link_relocatable (flinfo
->info
))
10200 irela
->r_offset
+= o
->output_section
->vma
;
10202 last_offset
= irela
->r_offset
;
10204 r_symndx
= irela
->r_info
>> r_sym_shift
;
10205 if (r_symndx
== STN_UNDEF
)
10208 if (r_symndx
>= locsymcount
10209 || (elf_bad_symtab (input_bfd
)
10210 && flinfo
->sections
[r_symndx
] == NULL
))
10212 struct elf_link_hash_entry
*rh
;
10213 unsigned long indx
;
10215 /* This is a reloc against a global symbol. We
10216 have not yet output all the local symbols, so
10217 we do not know the symbol index of any global
10218 symbol. We set the rel_hash entry for this
10219 reloc to point to the global hash table entry
10220 for this symbol. The symbol index is then
10221 set at the end of bfd_elf_final_link. */
10222 indx
= r_symndx
- extsymoff
;
10223 rh
= elf_sym_hashes (input_bfd
)[indx
];
10224 while (rh
->root
.type
== bfd_link_hash_indirect
10225 || rh
->root
.type
== bfd_link_hash_warning
)
10226 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
10228 /* Setting the index to -2 tells
10229 elf_link_output_extsym that this symbol is
10230 used by a reloc. */
10231 BFD_ASSERT (rh
->indx
< 0);
10239 /* This is a reloc against a local symbol. */
10242 sym
= isymbuf
[r_symndx
];
10243 sec
= flinfo
->sections
[r_symndx
];
10244 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
10246 /* I suppose the backend ought to fill in the
10247 section of any STT_SECTION symbol against a
10248 processor specific section. */
10249 r_symndx
= STN_UNDEF
;
10250 if (bfd_is_abs_section (sec
))
10252 else if (sec
== NULL
|| sec
->owner
== NULL
)
10254 bfd_set_error (bfd_error_bad_value
);
10259 asection
*osec
= sec
->output_section
;
10261 /* If we have discarded a section, the output
10262 section will be the absolute section. In
10263 case of discarded SEC_MERGE sections, use
10264 the kept section. relocate_section should
10265 have already handled discarded linkonce
10267 if (bfd_is_abs_section (osec
)
10268 && sec
->kept_section
!= NULL
10269 && sec
->kept_section
->output_section
!= NULL
)
10271 osec
= sec
->kept_section
->output_section
;
10272 irela
->r_addend
-= osec
->vma
;
10275 if (!bfd_is_abs_section (osec
))
10277 r_symndx
= osec
->target_index
;
10278 if (r_symndx
== STN_UNDEF
)
10280 irela
->r_addend
+= osec
->vma
;
10281 osec
= _bfd_nearby_section (output_bfd
, osec
,
10283 irela
->r_addend
-= osec
->vma
;
10284 r_symndx
= osec
->target_index
;
10289 /* Adjust the addend according to where the
10290 section winds up in the output section. */
10292 irela
->r_addend
+= sec
->output_offset
;
10296 if (flinfo
->indices
[r_symndx
] == -1)
10298 unsigned long shlink
;
10303 if (flinfo
->info
->strip
== strip_all
)
10305 /* You can't do ld -r -s. */
10306 bfd_set_error (bfd_error_invalid_operation
);
10310 /* This symbol was skipped earlier, but
10311 since it is needed by a reloc, we
10312 must output it now. */
10313 shlink
= symtab_hdr
->sh_link
;
10314 name
= (bfd_elf_string_from_elf_section
10315 (input_bfd
, shlink
, sym
.st_name
));
10319 osec
= sec
->output_section
;
10321 _bfd_elf_section_from_bfd_section (output_bfd
,
10323 if (sym
.st_shndx
== SHN_BAD
)
10326 sym
.st_value
+= sec
->output_offset
;
10327 if (!bfd_link_relocatable (flinfo
->info
))
10329 sym
.st_value
+= osec
->vma
;
10330 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
10332 /* STT_TLS symbols are relative to PT_TLS
10334 BFD_ASSERT (elf_hash_table (flinfo
->info
)
10335 ->tls_sec
!= NULL
);
10336 sym
.st_value
-= (elf_hash_table (flinfo
->info
)
10341 indx
= bfd_get_symcount (output_bfd
);
10342 ret
= elf_link_output_symstrtab (flinfo
, name
,
10348 flinfo
->indices
[r_symndx
] = indx
;
10353 r_symndx
= flinfo
->indices
[r_symndx
];
10356 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
10357 | (irela
->r_info
& r_type_mask
));
10360 /* Swap out the relocs. */
10361 input_rel_hdr
= esdi
->rel
.hdr
;
10362 if (input_rel_hdr
&& input_rel_hdr
->sh_size
!= 0)
10364 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10369 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
10370 * bed
->s
->int_rels_per_ext_rel
);
10371 rel_hash_list
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
10374 input_rela_hdr
= esdi
->rela
.hdr
;
10375 if (input_rela_hdr
&& input_rela_hdr
->sh_size
!= 0)
10377 if (!bed
->elf_backend_emit_relocs (output_bfd
, o
,
10386 /* Write out the modified section contents. */
10387 if (bed
->elf_backend_write_section
10388 && (*bed
->elf_backend_write_section
) (output_bfd
, flinfo
->info
, o
,
10391 /* Section written out. */
10393 else switch (o
->sec_info_type
)
10395 case SEC_INFO_TYPE_STABS
:
10396 if (! (_bfd_write_section_stabs
10398 &elf_hash_table (flinfo
->info
)->stab_info
,
10399 o
, &elf_section_data (o
)->sec_info
, contents
)))
10402 case SEC_INFO_TYPE_MERGE
:
10403 if (! _bfd_write_merged_section (output_bfd
, o
,
10404 elf_section_data (o
)->sec_info
))
10407 case SEC_INFO_TYPE_EH_FRAME
:
10409 if (! _bfd_elf_write_section_eh_frame (output_bfd
, flinfo
->info
,
10414 case SEC_INFO_TYPE_EH_FRAME_ENTRY
:
10416 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd
,
10424 /* FIXME: octets_per_byte. */
10425 if (! (o
->flags
& SEC_EXCLUDE
))
10427 file_ptr offset
= (file_ptr
) o
->output_offset
;
10428 bfd_size_type todo
= o
->size
;
10429 if ((o
->flags
& SEC_ELF_REVERSE_COPY
))
10431 /* Reverse-copy input section to output. */
10434 todo
-= address_size
;
10435 if (! bfd_set_section_contents (output_bfd
,
10443 offset
+= address_size
;
10447 else if (! bfd_set_section_contents (output_bfd
,
10461 /* Generate a reloc when linking an ELF file. This is a reloc
10462 requested by the linker, and does not come from any input file. This
10463 is used to build constructor and destructor tables when linking
10467 elf_reloc_link_order (bfd
*output_bfd
,
10468 struct bfd_link_info
*info
,
10469 asection
*output_section
,
10470 struct bfd_link_order
*link_order
)
10472 reloc_howto_type
*howto
;
10476 struct bfd_elf_section_reloc_data
*reldata
;
10477 struct elf_link_hash_entry
**rel_hash_ptr
;
10478 Elf_Internal_Shdr
*rel_hdr
;
10479 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
10480 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
10483 struct bfd_elf_section_data
*esdo
= elf_section_data (output_section
);
10485 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
10488 bfd_set_error (bfd_error_bad_value
);
10492 addend
= link_order
->u
.reloc
.p
->addend
;
10495 reldata
= &esdo
->rel
;
10496 else if (esdo
->rela
.hdr
)
10497 reldata
= &esdo
->rela
;
10504 /* Figure out the symbol index. */
10505 rel_hash_ptr
= reldata
->hashes
+ reldata
->count
;
10506 if (link_order
->type
== bfd_section_reloc_link_order
)
10508 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
10509 BFD_ASSERT (indx
!= 0);
10510 *rel_hash_ptr
= NULL
;
10514 struct elf_link_hash_entry
*h
;
10516 /* Treat a reloc against a defined symbol as though it were
10517 actually against the section. */
10518 h
= ((struct elf_link_hash_entry
*)
10519 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
10520 link_order
->u
.reloc
.p
->u
.name
,
10521 FALSE
, FALSE
, TRUE
));
10523 && (h
->root
.type
== bfd_link_hash_defined
10524 || h
->root
.type
== bfd_link_hash_defweak
))
10528 section
= h
->root
.u
.def
.section
;
10529 indx
= section
->output_section
->target_index
;
10530 *rel_hash_ptr
= NULL
;
10531 /* It seems that we ought to add the symbol value to the
10532 addend here, but in practice it has already been added
10533 because it was passed to constructor_callback. */
10534 addend
+= section
->output_section
->vma
+ section
->output_offset
;
10536 else if (h
!= NULL
)
10538 /* Setting the index to -2 tells elf_link_output_extsym that
10539 this symbol is used by a reloc. */
10546 if (! ((*info
->callbacks
->unattached_reloc
)
10547 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
10553 /* If this is an inplace reloc, we must write the addend into the
10555 if (howto
->partial_inplace
&& addend
!= 0)
10557 bfd_size_type size
;
10558 bfd_reloc_status_type rstat
;
10561 const char *sym_name
;
10563 size
= (bfd_size_type
) bfd_get_reloc_size (howto
);
10564 buf
= (bfd_byte
*) bfd_zmalloc (size
);
10565 if (buf
== NULL
&& size
!= 0)
10567 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
10574 case bfd_reloc_outofrange
:
10577 case bfd_reloc_overflow
:
10578 if (link_order
->type
== bfd_section_reloc_link_order
)
10579 sym_name
= bfd_section_name (output_bfd
,
10580 link_order
->u
.reloc
.p
->u
.section
);
10582 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
10583 if (! ((*info
->callbacks
->reloc_overflow
)
10584 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
10585 NULL
, (bfd_vma
) 0)))
10592 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
10593 link_order
->offset
, size
);
10599 /* The address of a reloc is relative to the section in a
10600 relocatable file, and is a virtual address in an executable
10602 offset
= link_order
->offset
;
10603 if (! bfd_link_relocatable (info
))
10604 offset
+= output_section
->vma
;
10606 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
10608 irel
[i
].r_offset
= offset
;
10609 irel
[i
].r_info
= 0;
10610 irel
[i
].r_addend
= 0;
10612 if (bed
->s
->arch_size
== 32)
10613 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
10615 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
10617 rel_hdr
= reldata
->hdr
;
10618 erel
= rel_hdr
->contents
;
10619 if (rel_hdr
->sh_type
== SHT_REL
)
10621 erel
+= reldata
->count
* bed
->s
->sizeof_rel
;
10622 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
10626 irel
[0].r_addend
= addend
;
10627 erel
+= reldata
->count
* bed
->s
->sizeof_rela
;
10628 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
10637 /* Get the output vma of the section pointed to by the sh_link field. */
10640 elf_get_linked_section_vma (struct bfd_link_order
*p
)
10642 Elf_Internal_Shdr
**elf_shdrp
;
10646 s
= p
->u
.indirect
.section
;
10647 elf_shdrp
= elf_elfsections (s
->owner
);
10648 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
10649 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
10651 The Intel C compiler generates SHT_IA_64_UNWIND with
10652 SHF_LINK_ORDER. But it doesn't set the sh_link or
10653 sh_info fields. Hence we could get the situation
10654 where elfsec is 0. */
10657 const struct elf_backend_data
*bed
10658 = get_elf_backend_data (s
->owner
);
10659 if (bed
->link_order_error_handler
)
10660 bed
->link_order_error_handler
10661 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
10666 s
= elf_shdrp
[elfsec
]->bfd_section
;
10667 return s
->output_section
->vma
+ s
->output_offset
;
10672 /* Compare two sections based on the locations of the sections they are
10673 linked to. Used by elf_fixup_link_order. */
10676 compare_link_order (const void * a
, const void * b
)
10681 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
10682 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
10685 return apos
> bpos
;
10689 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10690 order as their linked sections. Returns false if this could not be done
10691 because an output section includes both ordered and unordered
10692 sections. Ideally we'd do this in the linker proper. */
10695 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
10697 int seen_linkorder
;
10700 struct bfd_link_order
*p
;
10702 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10704 struct bfd_link_order
**sections
;
10705 asection
*s
, *other_sec
, *linkorder_sec
;
10709 linkorder_sec
= NULL
;
10711 seen_linkorder
= 0;
10712 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10714 if (p
->type
== bfd_indirect_link_order
)
10716 s
= p
->u
.indirect
.section
;
10718 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
10719 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
10720 && (elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
))
10721 && elfsec
< elf_numsections (sub
)
10722 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
10723 && elf_elfsections (sub
)[elfsec
]->sh_link
< elf_numsections (sub
))
10737 if (seen_other
&& seen_linkorder
)
10739 if (other_sec
&& linkorder_sec
)
10740 (*_bfd_error_handler
) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10742 linkorder_sec
->owner
, other_sec
,
10745 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
10747 bfd_set_error (bfd_error_bad_value
);
10752 if (!seen_linkorder
)
10755 sections
= (struct bfd_link_order
**)
10756 bfd_malloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
10757 if (sections
== NULL
)
10759 seen_linkorder
= 0;
10761 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10763 sections
[seen_linkorder
++] = p
;
10765 /* Sort the input sections in the order of their linked section. */
10766 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
10767 compare_link_order
);
10769 /* Change the offsets of the sections. */
10771 for (n
= 0; n
< seen_linkorder
; n
++)
10773 s
= sections
[n
]->u
.indirect
.section
;
10774 offset
&= ~(bfd_vma
) 0 << s
->alignment_power
;
10775 s
->output_offset
= offset
;
10776 sections
[n
]->offset
= offset
;
10777 /* FIXME: octets_per_byte. */
10778 offset
+= sections
[n
]->size
;
10786 elf_final_link_free (bfd
*obfd
, struct elf_final_link_info
*flinfo
)
10790 if (flinfo
->symstrtab
!= NULL
)
10791 _bfd_elf_strtab_free (flinfo
->symstrtab
);
10792 if (flinfo
->contents
!= NULL
)
10793 free (flinfo
->contents
);
10794 if (flinfo
->external_relocs
!= NULL
)
10795 free (flinfo
->external_relocs
);
10796 if (flinfo
->internal_relocs
!= NULL
)
10797 free (flinfo
->internal_relocs
);
10798 if (flinfo
->external_syms
!= NULL
)
10799 free (flinfo
->external_syms
);
10800 if (flinfo
->locsym_shndx
!= NULL
)
10801 free (flinfo
->locsym_shndx
);
10802 if (flinfo
->internal_syms
!= NULL
)
10803 free (flinfo
->internal_syms
);
10804 if (flinfo
->indices
!= NULL
)
10805 free (flinfo
->indices
);
10806 if (flinfo
->sections
!= NULL
)
10807 free (flinfo
->sections
);
10808 if (flinfo
->symshndxbuf
!= NULL
)
10809 free (flinfo
->symshndxbuf
);
10810 for (o
= obfd
->sections
; o
!= NULL
; o
= o
->next
)
10812 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10813 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rel
.hashes
!= NULL
)
10814 free (esdo
->rel
.hashes
);
10815 if ((o
->flags
& SEC_RELOC
) != 0 && esdo
->rela
.hashes
!= NULL
)
10816 free (esdo
->rela
.hashes
);
10820 /* Do the final step of an ELF link. */
10823 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
10825 bfd_boolean dynamic
;
10826 bfd_boolean emit_relocs
;
10828 struct elf_final_link_info flinfo
;
10830 struct bfd_link_order
*p
;
10832 bfd_size_type max_contents_size
;
10833 bfd_size_type max_external_reloc_size
;
10834 bfd_size_type max_internal_reloc_count
;
10835 bfd_size_type max_sym_count
;
10836 bfd_size_type max_sym_shndx_count
;
10837 Elf_Internal_Sym elfsym
;
10839 Elf_Internal_Shdr
*symtab_hdr
;
10840 Elf_Internal_Shdr
*symtab_shndx_hdr
;
10841 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10842 struct elf_outext_info eoinfo
;
10843 bfd_boolean merged
;
10844 size_t relativecount
= 0;
10845 asection
*reldyn
= 0;
10847 asection
*attr_section
= NULL
;
10848 bfd_vma attr_size
= 0;
10849 const char *std_attrs_section
;
10851 if (! is_elf_hash_table (info
->hash
))
10854 if (bfd_link_pic (info
))
10855 abfd
->flags
|= DYNAMIC
;
10857 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
10858 dynobj
= elf_hash_table (info
)->dynobj
;
10860 emit_relocs
= (bfd_link_relocatable (info
)
10861 || info
->emitrelocations
);
10863 flinfo
.info
= info
;
10864 flinfo
.output_bfd
= abfd
;
10865 flinfo
.symstrtab
= _bfd_elf_strtab_init ();
10866 if (flinfo
.symstrtab
== NULL
)
10871 flinfo
.hash_sec
= NULL
;
10872 flinfo
.symver_sec
= NULL
;
10876 flinfo
.hash_sec
= bfd_get_linker_section (dynobj
, ".hash");
10877 /* Note that dynsym_sec can be NULL (on VMS). */
10878 flinfo
.symver_sec
= bfd_get_linker_section (dynobj
, ".gnu.version");
10879 /* Note that it is OK if symver_sec is NULL. */
10882 flinfo
.contents
= NULL
;
10883 flinfo
.external_relocs
= NULL
;
10884 flinfo
.internal_relocs
= NULL
;
10885 flinfo
.external_syms
= NULL
;
10886 flinfo
.locsym_shndx
= NULL
;
10887 flinfo
.internal_syms
= NULL
;
10888 flinfo
.indices
= NULL
;
10889 flinfo
.sections
= NULL
;
10890 flinfo
.symshndxbuf
= NULL
;
10891 flinfo
.filesym_count
= 0;
10893 /* The object attributes have been merged. Remove the input
10894 sections from the link, and set the contents of the output
10896 std_attrs_section
= get_elf_backend_data (abfd
)->obj_attrs_section
;
10897 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10899 if ((std_attrs_section
&& strcmp (o
->name
, std_attrs_section
) == 0)
10900 || strcmp (o
->name
, ".gnu.attributes") == 0)
10902 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10904 asection
*input_section
;
10906 if (p
->type
!= bfd_indirect_link_order
)
10908 input_section
= p
->u
.indirect
.section
;
10909 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10910 elf_link_input_bfd ignores this section. */
10911 input_section
->flags
&= ~SEC_HAS_CONTENTS
;
10914 attr_size
= bfd_elf_obj_attr_size (abfd
);
10917 bfd_set_section_size (abfd
, o
, attr_size
);
10919 /* Skip this section later on. */
10920 o
->map_head
.link_order
= NULL
;
10923 o
->flags
|= SEC_EXCLUDE
;
10927 /* Count up the number of relocations we will output for each output
10928 section, so that we know the sizes of the reloc sections. We
10929 also figure out some maximum sizes. */
10930 max_contents_size
= 0;
10931 max_external_reloc_size
= 0;
10932 max_internal_reloc_count
= 0;
10934 max_sym_shndx_count
= 0;
10936 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
10938 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
10939 o
->reloc_count
= 0;
10941 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
10943 unsigned int reloc_count
= 0;
10944 struct bfd_elf_section_data
*esdi
= NULL
;
10946 if (p
->type
== bfd_section_reloc_link_order
10947 || p
->type
== bfd_symbol_reloc_link_order
)
10949 else if (p
->type
== bfd_indirect_link_order
)
10953 sec
= p
->u
.indirect
.section
;
10954 esdi
= elf_section_data (sec
);
10956 /* Mark all sections which are to be included in the
10957 link. This will normally be every section. We need
10958 to do this so that we can identify any sections which
10959 the linker has decided to not include. */
10960 sec
->linker_mark
= TRUE
;
10962 if (sec
->flags
& SEC_MERGE
)
10965 if (esdo
->this_hdr
.sh_type
== SHT_REL
10966 || esdo
->this_hdr
.sh_type
== SHT_RELA
)
10967 /* Some backends use reloc_count in relocation sections
10968 to count particular types of relocs. Of course,
10969 reloc sections themselves can't have relocations. */
10971 else if (emit_relocs
)
10972 reloc_count
= sec
->reloc_count
;
10973 else if (bed
->elf_backend_count_relocs
)
10974 reloc_count
= (*bed
->elf_backend_count_relocs
) (info
, sec
);
10976 if (sec
->rawsize
> max_contents_size
)
10977 max_contents_size
= sec
->rawsize
;
10978 if (sec
->size
> max_contents_size
)
10979 max_contents_size
= sec
->size
;
10981 /* We are interested in just local symbols, not all
10983 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
10984 && (sec
->owner
->flags
& DYNAMIC
) == 0)
10988 if (elf_bad_symtab (sec
->owner
))
10989 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
10990 / bed
->s
->sizeof_sym
);
10992 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
10994 if (sym_count
> max_sym_count
)
10995 max_sym_count
= sym_count
;
10997 if (sym_count
> max_sym_shndx_count
10998 && elf_symtab_shndx (sec
->owner
) != 0)
10999 max_sym_shndx_count
= sym_count
;
11001 if ((sec
->flags
& SEC_RELOC
) != 0)
11003 size_t ext_size
= 0;
11005 if (esdi
->rel
.hdr
!= NULL
)
11006 ext_size
= esdi
->rel
.hdr
->sh_size
;
11007 if (esdi
->rela
.hdr
!= NULL
)
11008 ext_size
+= esdi
->rela
.hdr
->sh_size
;
11010 if (ext_size
> max_external_reloc_size
)
11011 max_external_reloc_size
= ext_size
;
11012 if (sec
->reloc_count
> max_internal_reloc_count
)
11013 max_internal_reloc_count
= sec
->reloc_count
;
11018 if (reloc_count
== 0)
11021 o
->reloc_count
+= reloc_count
;
11023 if (p
->type
== bfd_indirect_link_order
&& emit_relocs
)
11026 esdo
->rel
.count
+= NUM_SHDR_ENTRIES (esdi
->rel
.hdr
);
11027 if (esdi
->rela
.hdr
)
11028 esdo
->rela
.count
+= NUM_SHDR_ENTRIES (esdi
->rela
.hdr
);
11033 esdo
->rela
.count
+= reloc_count
;
11035 esdo
->rel
.count
+= reloc_count
;
11039 if (o
->reloc_count
> 0)
11040 o
->flags
|= SEC_RELOC
;
11043 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11044 set it (this is probably a bug) and if it is set
11045 assign_section_numbers will create a reloc section. */
11046 o
->flags
&=~ SEC_RELOC
;
11049 /* If the SEC_ALLOC flag is not set, force the section VMA to
11050 zero. This is done in elf_fake_sections as well, but forcing
11051 the VMA to 0 here will ensure that relocs against these
11052 sections are handled correctly. */
11053 if ((o
->flags
& SEC_ALLOC
) == 0
11054 && ! o
->user_set_vma
)
11058 if (! bfd_link_relocatable (info
) && merged
)
11059 elf_link_hash_traverse (elf_hash_table (info
),
11060 _bfd_elf_link_sec_merge_syms
, abfd
);
11062 /* Figure out the file positions for everything but the symbol table
11063 and the relocs. We set symcount to force assign_section_numbers
11064 to create a symbol table. */
11065 bfd_get_symcount (abfd
) = info
->strip
!= strip_all
|| emit_relocs
;
11066 BFD_ASSERT (! abfd
->output_has_begun
);
11067 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
11070 /* Set sizes, and assign file positions for reloc sections. */
11071 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11073 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11074 if ((o
->flags
& SEC_RELOC
) != 0)
11077 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rel
)))
11081 && !(_bfd_elf_link_size_reloc_section (abfd
, &esdo
->rela
)))
11085 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11086 to count upwards while actually outputting the relocations. */
11087 esdo
->rel
.count
= 0;
11088 esdo
->rela
.count
= 0;
11090 if (esdo
->this_hdr
.sh_offset
== (file_ptr
) -1)
11092 /* Cache the section contents so that they can be compressed
11093 later. Use bfd_malloc since it will be freed by
11094 bfd_compress_section_contents. */
11095 unsigned char *contents
= esdo
->this_hdr
.contents
;
11096 if ((o
->flags
& SEC_ELF_COMPRESS
) == 0 || contents
!= NULL
)
11099 = (unsigned char *) bfd_malloc (esdo
->this_hdr
.sh_size
);
11100 if (contents
== NULL
)
11102 esdo
->this_hdr
.contents
= contents
;
11106 /* We have now assigned file positions for all the sections except
11107 .symtab, .strtab, and non-loaded reloc sections. We start the
11108 .symtab section at the current file position, and write directly
11109 to it. We build the .strtab section in memory. */
11110 bfd_get_symcount (abfd
) = 0;
11111 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11112 /* sh_name is set in prep_headers. */
11113 symtab_hdr
->sh_type
= SHT_SYMTAB
;
11114 /* sh_flags, sh_addr and sh_size all start off zero. */
11115 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
11116 /* sh_link is set in assign_section_numbers. */
11117 /* sh_info is set below. */
11118 /* sh_offset is set just below. */
11119 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
11121 if (max_sym_count
< 20)
11122 max_sym_count
= 20;
11123 elf_hash_table (info
)->strtabsize
= max_sym_count
;
11124 amt
= max_sym_count
* sizeof (struct elf_sym_strtab
);
11125 elf_hash_table (info
)->strtab
11126 = (struct elf_sym_strtab
*) bfd_malloc (amt
);
11127 if (elf_hash_table (info
)->strtab
== NULL
)
11129 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11131 = (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF)
11132 ? (Elf_External_Sym_Shndx
*) -1 : NULL
);
11134 if (info
->strip
!= strip_all
|| emit_relocs
)
11136 file_ptr off
= elf_next_file_pos (abfd
);
11138 _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
11140 /* Note that at this point elf_next_file_pos (abfd) is
11141 incorrect. We do not yet know the size of the .symtab section.
11142 We correct next_file_pos below, after we do know the size. */
11144 /* Start writing out the symbol table. The first symbol is always a
11146 elfsym
.st_value
= 0;
11147 elfsym
.st_size
= 0;
11148 elfsym
.st_info
= 0;
11149 elfsym
.st_other
= 0;
11150 elfsym
.st_shndx
= SHN_UNDEF
;
11151 elfsym
.st_target_internal
= 0;
11152 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
,
11153 bfd_und_section_ptr
, NULL
) != 1)
11156 /* Output a symbol for each section. We output these even if we are
11157 discarding local symbols, since they are used for relocs. These
11158 symbols have no names. We store the index of each one in the
11159 index field of the section, so that we can find it again when
11160 outputting relocs. */
11162 elfsym
.st_size
= 0;
11163 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11164 elfsym
.st_other
= 0;
11165 elfsym
.st_value
= 0;
11166 elfsym
.st_target_internal
= 0;
11167 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11169 o
= bfd_section_from_elf_index (abfd
, i
);
11172 o
->target_index
= bfd_get_symcount (abfd
);
11173 elfsym
.st_shndx
= i
;
11174 if (!bfd_link_relocatable (info
))
11175 elfsym
.st_value
= o
->vma
;
11176 if (elf_link_output_symstrtab (&flinfo
, NULL
, &elfsym
, o
,
11183 /* Allocate some memory to hold information read in from the input
11185 if (max_contents_size
!= 0)
11187 flinfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
11188 if (flinfo
.contents
== NULL
)
11192 if (max_external_reloc_size
!= 0)
11194 flinfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
11195 if (flinfo
.external_relocs
== NULL
)
11199 if (max_internal_reloc_count
!= 0)
11201 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
11202 amt
*= sizeof (Elf_Internal_Rela
);
11203 flinfo
.internal_relocs
= (Elf_Internal_Rela
*) bfd_malloc (amt
);
11204 if (flinfo
.internal_relocs
== NULL
)
11208 if (max_sym_count
!= 0)
11210 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
11211 flinfo
.external_syms
= (bfd_byte
*) bfd_malloc (amt
);
11212 if (flinfo
.external_syms
== NULL
)
11215 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
11216 flinfo
.internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
11217 if (flinfo
.internal_syms
== NULL
)
11220 amt
= max_sym_count
* sizeof (long);
11221 flinfo
.indices
= (long int *) bfd_malloc (amt
);
11222 if (flinfo
.indices
== NULL
)
11225 amt
= max_sym_count
* sizeof (asection
*);
11226 flinfo
.sections
= (asection
**) bfd_malloc (amt
);
11227 if (flinfo
.sections
== NULL
)
11231 if (max_sym_shndx_count
!= 0)
11233 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
11234 flinfo
.locsym_shndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
11235 if (flinfo
.locsym_shndx
== NULL
)
11239 if (elf_hash_table (info
)->tls_sec
)
11241 bfd_vma base
, end
= 0;
11244 for (sec
= elf_hash_table (info
)->tls_sec
;
11245 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
11248 bfd_size_type size
= sec
->size
;
11251 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
11253 struct bfd_link_order
*ord
= sec
->map_tail
.link_order
;
11256 size
= ord
->offset
+ ord
->size
;
11258 end
= sec
->vma
+ size
;
11260 base
= elf_hash_table (info
)->tls_sec
->vma
;
11261 /* Only align end of TLS section if static TLS doesn't have special
11262 alignment requirements. */
11263 if (bed
->static_tls_alignment
== 1)
11264 end
= align_power (end
,
11265 elf_hash_table (info
)->tls_sec
->alignment_power
);
11266 elf_hash_table (info
)->tls_size
= end
- base
;
11269 /* Reorder SHF_LINK_ORDER sections. */
11270 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11272 if (!elf_fixup_link_order (abfd
, o
))
11276 if (!_bfd_elf_fixup_eh_frame_hdr (info
))
11279 /* Since ELF permits relocations to be against local symbols, we
11280 must have the local symbols available when we do the relocations.
11281 Since we would rather only read the local symbols once, and we
11282 would rather not keep them in memory, we handle all the
11283 relocations for a single input file at the same time.
11285 Unfortunately, there is no way to know the total number of local
11286 symbols until we have seen all of them, and the local symbol
11287 indices precede the global symbol indices. This means that when
11288 we are generating relocatable output, and we see a reloc against
11289 a global symbol, we can not know the symbol index until we have
11290 finished examining all the local symbols to see which ones we are
11291 going to output. To deal with this, we keep the relocations in
11292 memory, and don't output them until the end of the link. This is
11293 an unfortunate waste of memory, but I don't see a good way around
11294 it. Fortunately, it only happens when performing a relocatable
11295 link, which is not the common case. FIXME: If keep_memory is set
11296 we could write the relocs out and then read them again; I don't
11297 know how bad the memory loss will be. */
11299 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11300 sub
->output_has_begun
= FALSE
;
11301 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11303 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
11305 if (p
->type
== bfd_indirect_link_order
11306 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
11307 == bfd_target_elf_flavour
)
11308 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
11310 if (! sub
->output_has_begun
)
11312 if (! elf_link_input_bfd (&flinfo
, sub
))
11314 sub
->output_has_begun
= TRUE
;
11317 else if (p
->type
== bfd_section_reloc_link_order
11318 || p
->type
== bfd_symbol_reloc_link_order
)
11320 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
11325 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
11327 if (p
->type
== bfd_indirect_link_order
11328 && (bfd_get_flavour (sub
)
11329 == bfd_target_elf_flavour
)
11330 && (elf_elfheader (sub
)->e_ident
[EI_CLASS
]
11331 != bed
->s
->elfclass
))
11333 const char *iclass
, *oclass
;
11335 if (bed
->s
->elfclass
== ELFCLASS64
)
11337 iclass
= "ELFCLASS32";
11338 oclass
= "ELFCLASS64";
11342 iclass
= "ELFCLASS64";
11343 oclass
= "ELFCLASS32";
11346 bfd_set_error (bfd_error_wrong_format
);
11347 (*_bfd_error_handler
)
11348 (_("%B: file class %s incompatible with %s"),
11349 sub
, iclass
, oclass
);
11358 /* Free symbol buffer if needed. */
11359 if (!info
->reduce_memory_overheads
)
11361 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
11362 if (bfd_get_flavour (sub
) == bfd_target_elf_flavour
11363 && elf_tdata (sub
)->symbuf
)
11365 free (elf_tdata (sub
)->symbuf
);
11366 elf_tdata (sub
)->symbuf
= NULL
;
11370 /* Output any global symbols that got converted to local in a
11371 version script or due to symbol visibility. We do this in a
11372 separate step since ELF requires all local symbols to appear
11373 prior to any global symbols. FIXME: We should only do this if
11374 some global symbols were, in fact, converted to become local.
11375 FIXME: Will this work correctly with the Irix 5 linker? */
11376 eoinfo
.failed
= FALSE
;
11377 eoinfo
.flinfo
= &flinfo
;
11378 eoinfo
.localsyms
= TRUE
;
11379 eoinfo
.file_sym_done
= FALSE
;
11380 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11384 /* If backend needs to output some local symbols not present in the hash
11385 table, do it now. */
11386 if (bed
->elf_backend_output_arch_local_syms
11387 && (info
->strip
!= strip_all
|| emit_relocs
))
11389 typedef int (*out_sym_func
)
11390 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11391 struct elf_link_hash_entry
*);
11393 if (! ((*bed
->elf_backend_output_arch_local_syms
)
11394 (abfd
, info
, &flinfo
,
11395 (out_sym_func
) elf_link_output_symstrtab
)))
11399 /* That wrote out all the local symbols. Finish up the symbol table
11400 with the global symbols. Even if we want to strip everything we
11401 can, we still need to deal with those global symbols that got
11402 converted to local in a version script. */
11404 /* The sh_info field records the index of the first non local symbol. */
11405 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
11408 && elf_hash_table (info
)->dynsym
!= NULL
11409 && (elf_hash_table (info
)->dynsym
->output_section
11410 != bfd_abs_section_ptr
))
11412 Elf_Internal_Sym sym
;
11413 bfd_byte
*dynsym
= elf_hash_table (info
)->dynsym
->contents
;
11414 long last_local
= 0;
11416 /* Write out the section symbols for the output sections. */
11417 if (bfd_link_pic (info
)
11418 || elf_hash_table (info
)->is_relocatable_executable
)
11424 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
11426 sym
.st_target_internal
= 0;
11428 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
11434 dynindx
= elf_section_data (s
)->dynindx
;
11437 indx
= elf_section_data (s
)->this_idx
;
11438 BFD_ASSERT (indx
> 0);
11439 sym
.st_shndx
= indx
;
11440 if (! check_dynsym (abfd
, &sym
))
11442 sym
.st_value
= s
->vma
;
11443 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
11444 if (last_local
< dynindx
)
11445 last_local
= dynindx
;
11446 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11450 /* Write out the local dynsyms. */
11451 if (elf_hash_table (info
)->dynlocal
)
11453 struct elf_link_local_dynamic_entry
*e
;
11454 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
11459 /* Copy the internal symbol and turn off visibility.
11460 Note that we saved a word of storage and overwrote
11461 the original st_name with the dynstr_index. */
11463 sym
.st_other
&= ~ELF_ST_VISIBILITY (-1);
11465 s
= bfd_section_from_elf_index (e
->input_bfd
,
11470 elf_section_data (s
->output_section
)->this_idx
;
11471 if (! check_dynsym (abfd
, &sym
))
11473 sym
.st_value
= (s
->output_section
->vma
11475 + e
->isym
.st_value
);
11478 if (last_local
< e
->dynindx
)
11479 last_local
= e
->dynindx
;
11481 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
11482 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
11486 elf_section_data (elf_hash_table (info
)->dynsym
->output_section
)->this_hdr
.sh_info
=
11490 /* We get the global symbols from the hash table. */
11491 eoinfo
.failed
= FALSE
;
11492 eoinfo
.localsyms
= FALSE
;
11493 eoinfo
.flinfo
= &flinfo
;
11494 bfd_hash_traverse (&info
->hash
->table
, elf_link_output_extsym
, &eoinfo
);
11498 /* If backend needs to output some symbols not present in the hash
11499 table, do it now. */
11500 if (bed
->elf_backend_output_arch_syms
11501 && (info
->strip
!= strip_all
|| emit_relocs
))
11503 typedef int (*out_sym_func
)
11504 (void *, const char *, Elf_Internal_Sym
*, asection
*,
11505 struct elf_link_hash_entry
*);
11507 if (! ((*bed
->elf_backend_output_arch_syms
)
11508 (abfd
, info
, &flinfo
,
11509 (out_sym_func
) elf_link_output_symstrtab
)))
11513 /* Finalize the .strtab section. */
11514 _bfd_elf_strtab_finalize (flinfo
.symstrtab
);
11516 /* Swap out the .strtab section. */
11517 if (!elf_link_swap_symbols_out (&flinfo
))
11520 /* Now we know the size of the symtab section. */
11521 if (bfd_get_symcount (abfd
) > 0)
11523 /* Finish up and write out the symbol string table (.strtab)
11525 Elf_Internal_Shdr
*symstrtab_hdr
;
11526 file_ptr off
= symtab_hdr
->sh_offset
+ symtab_hdr
->sh_size
;
11528 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
11529 if (symtab_shndx_hdr
->sh_name
!= 0)
11531 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
11532 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
11533 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
11534 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
11535 symtab_shndx_hdr
->sh_size
= amt
;
11537 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
11540 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
11541 || (bfd_bwrite (flinfo
.symshndxbuf
, amt
, abfd
) != amt
))
11545 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
11546 /* sh_name was set in prep_headers. */
11547 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
11548 symstrtab_hdr
->sh_flags
= 0;
11549 symstrtab_hdr
->sh_addr
= 0;
11550 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (flinfo
.symstrtab
);
11551 symstrtab_hdr
->sh_entsize
= 0;
11552 symstrtab_hdr
->sh_link
= 0;
11553 symstrtab_hdr
->sh_info
= 0;
11554 /* sh_offset is set just below. */
11555 symstrtab_hdr
->sh_addralign
= 1;
11557 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
,
11559 elf_next_file_pos (abfd
) = off
;
11561 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
11562 || ! _bfd_elf_strtab_emit (abfd
, flinfo
.symstrtab
))
11566 /* Adjust the relocs to have the correct symbol indices. */
11567 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
11569 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
11571 if ((o
->flags
& SEC_RELOC
) == 0)
11574 sort
= bed
->sort_relocs_p
== NULL
|| (*bed
->sort_relocs_p
) (o
);
11575 if (esdo
->rel
.hdr
!= NULL
)
11576 elf_link_adjust_relocs (abfd
, &esdo
->rel
, sort
);
11577 if (esdo
->rela
.hdr
!= NULL
)
11578 elf_link_adjust_relocs (abfd
, &esdo
->rela
, sort
);
11580 /* Set the reloc_count field to 0 to prevent write_relocs from
11581 trying to swap the relocs out itself. */
11582 o
->reloc_count
= 0;
11585 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
11586 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
11588 /* If we are linking against a dynamic object, or generating a
11589 shared library, finish up the dynamic linking information. */
11592 bfd_byte
*dyncon
, *dynconend
;
11594 /* Fix up .dynamic entries. */
11595 o
= bfd_get_linker_section (dynobj
, ".dynamic");
11596 BFD_ASSERT (o
!= NULL
);
11598 dyncon
= o
->contents
;
11599 dynconend
= o
->contents
+ o
->size
;
11600 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
11602 Elf_Internal_Dyn dyn
;
11606 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
11613 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
11615 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
11617 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
11618 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
11621 dyn
.d_un
.d_val
= relativecount
;
11628 name
= info
->init_function
;
11631 name
= info
->fini_function
;
11634 struct elf_link_hash_entry
*h
;
11636 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
11637 FALSE
, FALSE
, TRUE
);
11639 && (h
->root
.type
== bfd_link_hash_defined
11640 || h
->root
.type
== bfd_link_hash_defweak
))
11642 dyn
.d_un
.d_ptr
= h
->root
.u
.def
.value
;
11643 o
= h
->root
.u
.def
.section
;
11644 if (o
->output_section
!= NULL
)
11645 dyn
.d_un
.d_ptr
+= (o
->output_section
->vma
11646 + o
->output_offset
);
11649 /* The symbol is imported from another shared
11650 library and does not apply to this one. */
11651 dyn
.d_un
.d_ptr
= 0;
11658 case DT_PREINIT_ARRAYSZ
:
11659 name
= ".preinit_array";
11661 case DT_INIT_ARRAYSZ
:
11662 name
= ".init_array";
11664 case DT_FINI_ARRAYSZ
:
11665 name
= ".fini_array";
11667 o
= bfd_get_section_by_name (abfd
, name
);
11670 (*_bfd_error_handler
)
11671 (_("%B: could not find output section %s"), abfd
, name
);
11675 (*_bfd_error_handler
)
11676 (_("warning: %s section has zero size"), name
);
11677 dyn
.d_un
.d_val
= o
->size
;
11680 case DT_PREINIT_ARRAY
:
11681 name
= ".preinit_array";
11683 case DT_INIT_ARRAY
:
11684 name
= ".init_array";
11686 case DT_FINI_ARRAY
:
11687 name
= ".fini_array";
11694 name
= ".gnu.hash";
11703 name
= ".gnu.version_d";
11706 name
= ".gnu.version_r";
11709 name
= ".gnu.version";
11711 o
= bfd_get_section_by_name (abfd
, name
);
11714 (*_bfd_error_handler
)
11715 (_("%B: could not find output section %s"), abfd
, name
);
11718 if (elf_section_data (o
->output_section
)->this_hdr
.sh_type
== SHT_NOTE
)
11720 (*_bfd_error_handler
)
11721 (_("warning: section '%s' is being made into a note"), name
);
11722 bfd_set_error (bfd_error_nonrepresentable_section
);
11725 dyn
.d_un
.d_ptr
= o
->vma
;
11732 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
11736 dyn
.d_un
.d_val
= 0;
11737 dyn
.d_un
.d_ptr
= 0;
11738 for (i
= 1; i
< elf_numsections (abfd
); i
++)
11740 Elf_Internal_Shdr
*hdr
;
11742 hdr
= elf_elfsections (abfd
)[i
];
11743 if (hdr
->sh_type
== type
11744 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
11746 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
11747 dyn
.d_un
.d_val
+= hdr
->sh_size
;
11750 if (dyn
.d_un
.d_ptr
== 0
11751 || hdr
->sh_addr
< dyn
.d_un
.d_ptr
)
11752 dyn
.d_un
.d_ptr
= hdr
->sh_addr
;
11758 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
11762 /* If we have created any dynamic sections, then output them. */
11763 if (dynobj
!= NULL
)
11765 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
11768 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11769 if (((info
->warn_shared_textrel
&& bfd_link_pic (info
))
11770 || info
->error_textrel
)
11771 && (o
= bfd_get_linker_section (dynobj
, ".dynamic")) != NULL
)
11773 bfd_byte
*dyncon
, *dynconend
;
11775 dyncon
= o
->contents
;
11776 dynconend
= o
->contents
+ o
->size
;
11777 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
11779 Elf_Internal_Dyn dyn
;
11781 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
11783 if (dyn
.d_tag
== DT_TEXTREL
)
11785 if (info
->error_textrel
)
11786 info
->callbacks
->einfo
11787 (_("%P%X: read-only segment has dynamic relocations.\n"));
11789 info
->callbacks
->einfo
11790 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11796 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
11798 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
11800 || o
->output_section
== bfd_abs_section_ptr
)
11802 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
11804 /* At this point, we are only interested in sections
11805 created by _bfd_elf_link_create_dynamic_sections. */
11808 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
11810 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
11812 if (strcmp (o
->name
, ".dynstr") != 0)
11814 /* FIXME: octets_per_byte. */
11815 if (! bfd_set_section_contents (abfd
, o
->output_section
,
11817 (file_ptr
) o
->output_offset
,
11823 /* The contents of the .dynstr section are actually in a
11827 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
11828 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
11829 || ! _bfd_elf_strtab_emit (abfd
,
11830 elf_hash_table (info
)->dynstr
))
11836 if (bfd_link_relocatable (info
))
11838 bfd_boolean failed
= FALSE
;
11840 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
11845 /* If we have optimized stabs strings, output them. */
11846 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
11848 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
11852 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
11855 elf_final_link_free (abfd
, &flinfo
);
11857 elf_linker (abfd
) = TRUE
;
11861 bfd_byte
*contents
= (bfd_byte
*) bfd_malloc (attr_size
);
11862 if (contents
== NULL
)
11863 return FALSE
; /* Bail out and fail. */
11864 bfd_elf_set_obj_attr_contents (abfd
, contents
, attr_size
);
11865 bfd_set_section_contents (abfd
, attr_section
, contents
, 0, attr_size
);
11872 elf_final_link_free (abfd
, &flinfo
);
11876 /* Initialize COOKIE for input bfd ABFD. */
11879 init_reloc_cookie (struct elf_reloc_cookie
*cookie
,
11880 struct bfd_link_info
*info
, bfd
*abfd
)
11882 Elf_Internal_Shdr
*symtab_hdr
;
11883 const struct elf_backend_data
*bed
;
11885 bed
= get_elf_backend_data (abfd
);
11886 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11888 cookie
->abfd
= abfd
;
11889 cookie
->sym_hashes
= elf_sym_hashes (abfd
);
11890 cookie
->bad_symtab
= elf_bad_symtab (abfd
);
11891 if (cookie
->bad_symtab
)
11893 cookie
->locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
11894 cookie
->extsymoff
= 0;
11898 cookie
->locsymcount
= symtab_hdr
->sh_info
;
11899 cookie
->extsymoff
= symtab_hdr
->sh_info
;
11902 if (bed
->s
->arch_size
== 32)
11903 cookie
->r_sym_shift
= 8;
11905 cookie
->r_sym_shift
= 32;
11907 cookie
->locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
11908 if (cookie
->locsyms
== NULL
&& cookie
->locsymcount
!= 0)
11910 cookie
->locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
11911 cookie
->locsymcount
, 0,
11913 if (cookie
->locsyms
== NULL
)
11915 info
->callbacks
->einfo (_("%P%X: can not read symbols: %E\n"));
11918 if (info
->keep_memory
)
11919 symtab_hdr
->contents
= (bfd_byte
*) cookie
->locsyms
;
11924 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11927 fini_reloc_cookie (struct elf_reloc_cookie
*cookie
, bfd
*abfd
)
11929 Elf_Internal_Shdr
*symtab_hdr
;
11931 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
11932 if (cookie
->locsyms
!= NULL
11933 && symtab_hdr
->contents
!= (unsigned char *) cookie
->locsyms
)
11934 free (cookie
->locsyms
);
11937 /* Initialize the relocation information in COOKIE for input section SEC
11938 of input bfd ABFD. */
11941 init_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11942 struct bfd_link_info
*info
, bfd
*abfd
,
11945 const struct elf_backend_data
*bed
;
11947 if (sec
->reloc_count
== 0)
11949 cookie
->rels
= NULL
;
11950 cookie
->relend
= NULL
;
11954 bed
= get_elf_backend_data (abfd
);
11956 cookie
->rels
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
11957 info
->keep_memory
);
11958 if (cookie
->rels
== NULL
)
11960 cookie
->rel
= cookie
->rels
;
11961 cookie
->relend
= (cookie
->rels
11962 + sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
);
11964 cookie
->rel
= cookie
->rels
;
11968 /* Free the memory allocated by init_reloc_cookie_rels,
11972 fini_reloc_cookie_rels (struct elf_reloc_cookie
*cookie
,
11975 if (cookie
->rels
&& elf_section_data (sec
)->relocs
!= cookie
->rels
)
11976 free (cookie
->rels
);
11979 /* Initialize the whole of COOKIE for input section SEC. */
11982 init_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
11983 struct bfd_link_info
*info
,
11986 if (!init_reloc_cookie (cookie
, info
, sec
->owner
))
11988 if (!init_reloc_cookie_rels (cookie
, info
, sec
->owner
, sec
))
11993 fini_reloc_cookie (cookie
, sec
->owner
);
11998 /* Free the memory allocated by init_reloc_cookie_for_section,
12002 fini_reloc_cookie_for_section (struct elf_reloc_cookie
*cookie
,
12005 fini_reloc_cookie_rels (cookie
, sec
);
12006 fini_reloc_cookie (cookie
, sec
->owner
);
12009 /* Garbage collect unused sections. */
12011 /* Default gc_mark_hook. */
12014 _bfd_elf_gc_mark_hook (asection
*sec
,
12015 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12016 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
12017 struct elf_link_hash_entry
*h
,
12018 Elf_Internal_Sym
*sym
)
12020 const char *sec_name
;
12024 switch (h
->root
.type
)
12026 case bfd_link_hash_defined
:
12027 case bfd_link_hash_defweak
:
12028 return h
->root
.u
.def
.section
;
12030 case bfd_link_hash_common
:
12031 return h
->root
.u
.c
.p
->section
;
12033 case bfd_link_hash_undefined
:
12034 case bfd_link_hash_undefweak
:
12035 /* To work around a glibc bug, keep all XXX input sections
12036 when there is an as yet undefined reference to __start_XXX
12037 or __stop_XXX symbols. The linker will later define such
12038 symbols for orphan input sections that have a name
12039 representable as a C identifier. */
12040 if (strncmp (h
->root
.root
.string
, "__start_", 8) == 0)
12041 sec_name
= h
->root
.root
.string
+ 8;
12042 else if (strncmp (h
->root
.root
.string
, "__stop_", 7) == 0)
12043 sec_name
= h
->root
.root
.string
+ 7;
12047 if (sec_name
&& *sec_name
!= '\0')
12051 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
12053 sec
= bfd_get_section_by_name (i
, sec_name
);
12055 sec
->flags
|= SEC_KEEP
;
12065 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
12070 /* COOKIE->rel describes a relocation against section SEC, which is
12071 a section we've decided to keep. Return the section that contains
12072 the relocation symbol, or NULL if no section contains it. */
12075 _bfd_elf_gc_mark_rsec (struct bfd_link_info
*info
, asection
*sec
,
12076 elf_gc_mark_hook_fn gc_mark_hook
,
12077 struct elf_reloc_cookie
*cookie
)
12079 unsigned long r_symndx
;
12080 struct elf_link_hash_entry
*h
;
12082 r_symndx
= cookie
->rel
->r_info
>> cookie
->r_sym_shift
;
12083 if (r_symndx
== STN_UNDEF
)
12086 if (r_symndx
>= cookie
->locsymcount
12087 || ELF_ST_BIND (cookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
12089 h
= cookie
->sym_hashes
[r_symndx
- cookie
->extsymoff
];
12092 info
->callbacks
->einfo (_("%F%P: corrupt input: %B\n"),
12096 while (h
->root
.type
== bfd_link_hash_indirect
12097 || h
->root
.type
== bfd_link_hash_warning
)
12098 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
12100 /* If this symbol is weak and there is a non-weak definition, we
12101 keep the non-weak definition because many backends put
12102 dynamic reloc info on the non-weak definition for code
12103 handling copy relocs. */
12104 if (h
->u
.weakdef
!= NULL
)
12105 h
->u
.weakdef
->mark
= 1;
12106 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, h
, NULL
);
12109 return (*gc_mark_hook
) (sec
, info
, cookie
->rel
, NULL
,
12110 &cookie
->locsyms
[r_symndx
]);
12113 /* COOKIE->rel describes a relocation against section SEC, which is
12114 a section we've decided to keep. Mark the section that contains
12115 the relocation symbol. */
12118 _bfd_elf_gc_mark_reloc (struct bfd_link_info
*info
,
12120 elf_gc_mark_hook_fn gc_mark_hook
,
12121 struct elf_reloc_cookie
*cookie
)
12125 rsec
= _bfd_elf_gc_mark_rsec (info
, sec
, gc_mark_hook
, cookie
);
12126 if (rsec
&& !rsec
->gc_mark
)
12128 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
12129 || (rsec
->owner
->flags
& DYNAMIC
) != 0)
12131 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
12137 /* The mark phase of garbage collection. For a given section, mark
12138 it and any sections in this section's group, and all the sections
12139 which define symbols to which it refers. */
12142 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
12144 elf_gc_mark_hook_fn gc_mark_hook
)
12147 asection
*group_sec
, *eh_frame
;
12151 /* Mark all the sections in the group. */
12152 group_sec
= elf_section_data (sec
)->next_in_group
;
12153 if (group_sec
&& !group_sec
->gc_mark
)
12154 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
12157 /* Look through the section relocs. */
12159 eh_frame
= elf_eh_frame_section (sec
->owner
);
12160 if ((sec
->flags
& SEC_RELOC
) != 0
12161 && sec
->reloc_count
> 0
12162 && sec
!= eh_frame
)
12164 struct elf_reloc_cookie cookie
;
12166 if (!init_reloc_cookie_for_section (&cookie
, info
, sec
))
12170 for (; cookie
.rel
< cookie
.relend
; cookie
.rel
++)
12171 if (!_bfd_elf_gc_mark_reloc (info
, sec
, gc_mark_hook
, &cookie
))
12176 fini_reloc_cookie_for_section (&cookie
, sec
);
12180 if (ret
&& eh_frame
&& elf_fde_list (sec
))
12182 struct elf_reloc_cookie cookie
;
12184 if (!init_reloc_cookie_for_section (&cookie
, info
, eh_frame
))
12188 if (!_bfd_elf_gc_mark_fdes (info
, sec
, eh_frame
,
12189 gc_mark_hook
, &cookie
))
12191 fini_reloc_cookie_for_section (&cookie
, eh_frame
);
12195 eh_frame
= elf_section_eh_frame_entry (sec
);
12196 if (ret
&& eh_frame
&& !eh_frame
->gc_mark
)
12197 if (!_bfd_elf_gc_mark (info
, eh_frame
, gc_mark_hook
))
12203 /* Scan and mark sections in a special or debug section group. */
12206 _bfd_elf_gc_mark_debug_special_section_group (asection
*grp
)
12208 /* Point to first section of section group. */
12210 /* Used to iterate the section group. */
12213 bfd_boolean is_special_grp
= TRUE
;
12214 bfd_boolean is_debug_grp
= TRUE
;
12216 /* First scan to see if group contains any section other than debug
12217 and special section. */
12218 ssec
= msec
= elf_next_in_group (grp
);
12221 if ((msec
->flags
& SEC_DEBUGGING
) == 0)
12222 is_debug_grp
= FALSE
;
12224 if ((msec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) != 0)
12225 is_special_grp
= FALSE
;
12227 msec
= elf_next_in_group (msec
);
12229 while (msec
!= ssec
);
12231 /* If this is a pure debug section group or pure special section group,
12232 keep all sections in this group. */
12233 if (is_debug_grp
|| is_special_grp
)
12238 msec
= elf_next_in_group (msec
);
12240 while (msec
!= ssec
);
12244 /* Keep debug and special sections. */
12247 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info
*info
,
12248 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED
)
12252 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12255 bfd_boolean some_kept
;
12256 bfd_boolean debug_frag_seen
;
12258 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12261 /* Ensure all linker created sections are kept,
12262 see if any other section is already marked,
12263 and note if we have any fragmented debug sections. */
12264 debug_frag_seen
= some_kept
= FALSE
;
12265 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12267 if ((isec
->flags
& SEC_LINKER_CREATED
) != 0)
12269 else if (isec
->gc_mark
)
12272 if (debug_frag_seen
== FALSE
12273 && (isec
->flags
& SEC_DEBUGGING
)
12274 && CONST_STRNEQ (isec
->name
, ".debug_line."))
12275 debug_frag_seen
= TRUE
;
12278 /* If no section in this file will be kept, then we can
12279 toss out the debug and special sections. */
12283 /* Keep debug and special sections like .comment when they are
12284 not part of a group. Also keep section groups that contain
12285 just debug sections or special sections. */
12286 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12288 if ((isec
->flags
& SEC_GROUP
) != 0)
12289 _bfd_elf_gc_mark_debug_special_section_group (isec
);
12290 else if (((isec
->flags
& SEC_DEBUGGING
) != 0
12291 || (isec
->flags
& (SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
)) == 0)
12292 && elf_next_in_group (isec
) == NULL
)
12296 if (! debug_frag_seen
)
12299 /* Look for CODE sections which are going to be discarded,
12300 and find and discard any fragmented debug sections which
12301 are associated with that code section. */
12302 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
12303 if ((isec
->flags
& SEC_CODE
) != 0
12304 && isec
->gc_mark
== 0)
12309 ilen
= strlen (isec
->name
);
12311 /* Association is determined by the name of the debug section
12312 containing the name of the code section as a suffix. For
12313 example .debug_line.text.foo is a debug section associated
12315 for (dsec
= ibfd
->sections
; dsec
!= NULL
; dsec
= dsec
->next
)
12319 if (dsec
->gc_mark
== 0
12320 || (dsec
->flags
& SEC_DEBUGGING
) == 0)
12323 dlen
= strlen (dsec
->name
);
12326 && strncmp (dsec
->name
+ (dlen
- ilen
),
12327 isec
->name
, ilen
) == 0)
12337 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12339 struct elf_gc_sweep_symbol_info
12341 struct bfd_link_info
*info
;
12342 void (*hide_symbol
) (struct bfd_link_info
*, struct elf_link_hash_entry
*,
12347 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *data
)
12350 && (((h
->root
.type
== bfd_link_hash_defined
12351 || h
->root
.type
== bfd_link_hash_defweak
)
12352 && !((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12353 && h
->root
.u
.def
.section
->gc_mark
))
12354 || h
->root
.type
== bfd_link_hash_undefined
12355 || h
->root
.type
== bfd_link_hash_undefweak
))
12357 struct elf_gc_sweep_symbol_info
*inf
;
12359 inf
= (struct elf_gc_sweep_symbol_info
*) data
;
12360 (*inf
->hide_symbol
) (inf
->info
, h
, TRUE
);
12361 h
->def_regular
= 0;
12362 h
->ref_regular
= 0;
12363 h
->ref_regular_nonweak
= 0;
12369 /* The sweep phase of garbage collection. Remove all garbage sections. */
12371 typedef bfd_boolean (*gc_sweep_hook_fn
)
12372 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
12375 elf_gc_sweep (bfd
*abfd
, struct bfd_link_info
*info
)
12378 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12379 gc_sweep_hook_fn gc_sweep_hook
= bed
->gc_sweep_hook
;
12380 unsigned long section_sym_count
;
12381 struct elf_gc_sweep_symbol_info sweep_info
;
12383 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12387 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12388 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12391 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12393 /* When any section in a section group is kept, we keep all
12394 sections in the section group. If the first member of
12395 the section group is excluded, we will also exclude the
12397 if (o
->flags
& SEC_GROUP
)
12399 asection
*first
= elf_next_in_group (o
);
12400 o
->gc_mark
= first
->gc_mark
;
12406 /* Skip sweeping sections already excluded. */
12407 if (o
->flags
& SEC_EXCLUDE
)
12410 /* Since this is early in the link process, it is simple
12411 to remove a section from the output. */
12412 o
->flags
|= SEC_EXCLUDE
;
12414 if (info
->print_gc_sections
&& o
->size
!= 0)
12415 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub
, o
->name
);
12417 /* But we also have to update some of the relocation
12418 info we collected before. */
12420 && (o
->flags
& SEC_RELOC
) != 0
12421 && o
->reloc_count
!= 0
12422 && !((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
12423 && (o
->flags
& SEC_DEBUGGING
) != 0)
12424 && !bfd_is_abs_section (o
->output_section
))
12426 Elf_Internal_Rela
*internal_relocs
;
12430 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
12431 info
->keep_memory
);
12432 if (internal_relocs
== NULL
)
12435 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
12437 if (elf_section_data (o
)->relocs
!= internal_relocs
)
12438 free (internal_relocs
);
12446 /* Remove the symbols that were in the swept sections from the dynamic
12447 symbol table. GCFIXME: Anyone know how to get them out of the
12448 static symbol table as well? */
12449 sweep_info
.info
= info
;
12450 sweep_info
.hide_symbol
= bed
->elf_backend_hide_symbol
;
12451 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
,
12454 _bfd_elf_link_renumber_dynsyms (abfd
, info
, §ion_sym_count
);
12458 /* Propagate collected vtable information. This is called through
12459 elf_link_hash_traverse. */
12462 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
12464 /* Those that are not vtables. */
12465 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12468 /* Those vtables that do not have parents, we cannot merge. */
12469 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
12472 /* If we've already been done, exit. */
12473 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
12476 /* Make sure the parent's table is up to date. */
12477 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
12479 if (h
->vtable
->used
== NULL
)
12481 /* None of this table's entries were referenced. Re-use the
12483 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
12484 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
12489 bfd_boolean
*cu
, *pu
;
12491 /* Or the parent's entries into ours. */
12492 cu
= h
->vtable
->used
;
12494 pu
= h
->vtable
->parent
->vtable
->used
;
12497 const struct elf_backend_data
*bed
;
12498 unsigned int log_file_align
;
12500 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
12501 log_file_align
= bed
->s
->log_file_align
;
12502 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
12517 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
12520 bfd_vma hstart
, hend
;
12521 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
12522 const struct elf_backend_data
*bed
;
12523 unsigned int log_file_align
;
12525 /* Take care of both those symbols that do not describe vtables as
12526 well as those that are not loaded. */
12527 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
12530 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
12531 || h
->root
.type
== bfd_link_hash_defweak
);
12533 sec
= h
->root
.u
.def
.section
;
12534 hstart
= h
->root
.u
.def
.value
;
12535 hend
= hstart
+ h
->size
;
12537 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
12539 return *(bfd_boolean
*) okp
= FALSE
;
12540 bed
= get_elf_backend_data (sec
->owner
);
12541 log_file_align
= bed
->s
->log_file_align
;
12543 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
12545 for (rel
= relstart
; rel
< relend
; ++rel
)
12546 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
12548 /* If the entry is in use, do nothing. */
12549 if (h
->vtable
->used
12550 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
12552 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
12553 if (h
->vtable
->used
[entry
])
12556 /* Otherwise, kill it. */
12557 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
12563 /* Mark sections containing dynamically referenced symbols. When
12564 building shared libraries, we must assume that any visible symbol is
12568 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
, void *inf
)
12570 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
12571 struct bfd_elf_dynamic_list
*d
= info
->dynamic_list
;
12573 if ((h
->root
.type
== bfd_link_hash_defined
12574 || h
->root
.type
== bfd_link_hash_defweak
)
12576 || ((h
->def_regular
|| ELF_COMMON_DEF_P (h
))
12577 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
12578 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
12579 && (!bfd_link_executable (info
)
12580 || info
->export_dynamic
12583 && (*d
->match
) (&d
->head
, NULL
, h
->root
.root
.string
)))
12584 && (h
->versioned
>= versioned
12585 || !bfd_hide_sym_by_version (info
->version_info
,
12586 h
->root
.root
.string
)))))
12587 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
12592 /* Keep all sections containing symbols undefined on the command-line,
12593 and the section containing the entry symbol. */
12596 _bfd_elf_gc_keep (struct bfd_link_info
*info
)
12598 struct bfd_sym_chain
*sym
;
12600 for (sym
= info
->gc_sym_list
; sym
!= NULL
; sym
= sym
->next
)
12602 struct elf_link_hash_entry
*h
;
12604 h
= elf_link_hash_lookup (elf_hash_table (info
), sym
->name
,
12605 FALSE
, FALSE
, FALSE
);
12608 && (h
->root
.type
== bfd_link_hash_defined
12609 || h
->root
.type
== bfd_link_hash_defweak
)
12610 && !bfd_is_abs_section (h
->root
.u
.def
.section
))
12611 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
12616 bfd_elf_parse_eh_frame_entries (bfd
*abfd ATTRIBUTE_UNUSED
,
12617 struct bfd_link_info
*info
)
12619 bfd
*ibfd
= info
->input_bfds
;
12621 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
12624 struct elf_reloc_cookie cookie
;
12626 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
12629 if (!init_reloc_cookie (&cookie
, info
, ibfd
))
12632 for (sec
= ibfd
->sections
; sec
; sec
= sec
->next
)
12634 if (CONST_STRNEQ (bfd_section_name (ibfd
, sec
), ".eh_frame_entry")
12635 && init_reloc_cookie_rels (&cookie
, info
, ibfd
, sec
))
12637 _bfd_elf_parse_eh_frame_entry (info
, sec
, &cookie
);
12638 fini_reloc_cookie_rels (&cookie
, sec
);
12645 /* Do mark and sweep of unused sections. */
12648 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
12650 bfd_boolean ok
= TRUE
;
12652 elf_gc_mark_hook_fn gc_mark_hook
;
12653 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12654 struct elf_link_hash_table
*htab
;
12656 if (!bed
->can_gc_sections
12657 || !is_elf_hash_table (info
->hash
))
12659 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
12663 bed
->gc_keep (info
);
12664 htab
= elf_hash_table (info
);
12666 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12667 at the .eh_frame section if we can mark the FDEs individually. */
12668 for (sub
= info
->input_bfds
;
12669 info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
&& sub
!= NULL
;
12670 sub
= sub
->link
.next
)
12673 struct elf_reloc_cookie cookie
;
12675 sec
= bfd_get_section_by_name (sub
, ".eh_frame");
12676 while (sec
&& init_reloc_cookie_for_section (&cookie
, info
, sec
))
12678 _bfd_elf_parse_eh_frame (sub
, info
, sec
, &cookie
);
12679 if (elf_section_data (sec
)->sec_info
12680 && (sec
->flags
& SEC_LINKER_CREATED
) == 0)
12681 elf_eh_frame_section (sub
) = sec
;
12682 fini_reloc_cookie_for_section (&cookie
, sec
);
12683 sec
= bfd_get_next_section_by_name (sec
);
12687 /* Apply transitive closure to the vtable entry usage info. */
12688 elf_link_hash_traverse (htab
, elf_gc_propagate_vtable_entries_used
, &ok
);
12692 /* Kill the vtable relocations that were not used. */
12693 elf_link_hash_traverse (htab
, elf_gc_smash_unused_vtentry_relocs
, &ok
);
12697 /* Mark dynamically referenced symbols. */
12698 if (htab
->dynamic_sections_created
)
12699 elf_link_hash_traverse (htab
, bed
->gc_mark_dynamic_ref
, info
);
12701 /* Grovel through relocs to find out who stays ... */
12702 gc_mark_hook
= bed
->gc_mark_hook
;
12703 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
12707 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
12708 || !(*bed
->relocs_compatible
) (sub
->xvec
, abfd
->xvec
))
12711 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12712 Also treat note sections as a root, if the section is not part
12714 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
12716 && (o
->flags
& SEC_EXCLUDE
) == 0
12717 && ((o
->flags
& SEC_KEEP
) != 0
12718 || (elf_section_data (o
)->this_hdr
.sh_type
== SHT_NOTE
12719 && elf_next_in_group (o
) == NULL
)))
12721 if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
12726 /* Allow the backend to mark additional target specific sections. */
12727 bed
->gc_mark_extra_sections (info
, gc_mark_hook
);
12729 /* ... and mark SEC_EXCLUDE for those that go. */
12730 return elf_gc_sweep (abfd
, info
);
12733 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12736 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
12738 struct elf_link_hash_entry
*h
,
12741 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
12742 struct elf_link_hash_entry
**search
, *child
;
12743 bfd_size_type extsymcount
;
12744 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12746 /* The sh_info field of the symtab header tells us where the
12747 external symbols start. We don't care about the local symbols at
12749 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
12750 if (!elf_bad_symtab (abfd
))
12751 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
12753 sym_hashes
= elf_sym_hashes (abfd
);
12754 sym_hashes_end
= sym_hashes
+ extsymcount
;
12756 /* Hunt down the child symbol, which is in this section at the same
12757 offset as the relocation. */
12758 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
12760 if ((child
= *search
) != NULL
12761 && (child
->root
.type
== bfd_link_hash_defined
12762 || child
->root
.type
== bfd_link_hash_defweak
)
12763 && child
->root
.u
.def
.section
== sec
12764 && child
->root
.u
.def
.value
== offset
)
12768 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
12769 abfd
, sec
, (unsigned long) offset
);
12770 bfd_set_error (bfd_error_invalid_operation
);
12774 if (!child
->vtable
)
12776 child
->vtable
= ((struct elf_link_virtual_table_entry
*)
12777 bfd_zalloc (abfd
, sizeof (*child
->vtable
)));
12778 if (!child
->vtable
)
12783 /* This *should* only be the absolute section. It could potentially
12784 be that someone has defined a non-global vtable though, which
12785 would be bad. It isn't worth paging in the local symbols to be
12786 sure though; that case should simply be handled by the assembler. */
12788 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
12791 child
->vtable
->parent
= h
;
12796 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12799 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
12800 asection
*sec ATTRIBUTE_UNUSED
,
12801 struct elf_link_hash_entry
*h
,
12804 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12805 unsigned int log_file_align
= bed
->s
->log_file_align
;
12809 h
->vtable
= ((struct elf_link_virtual_table_entry
*)
12810 bfd_zalloc (abfd
, sizeof (*h
->vtable
)));
12815 if (addend
>= h
->vtable
->size
)
12817 size_t size
, bytes
, file_align
;
12818 bfd_boolean
*ptr
= h
->vtable
->used
;
12820 /* While the symbol is undefined, we have to be prepared to handle
12822 file_align
= 1 << log_file_align
;
12823 if (h
->root
.type
== bfd_link_hash_undefined
)
12824 size
= addend
+ file_align
;
12828 if (addend
>= size
)
12830 /* Oops! We've got a reference past the defined end of
12831 the table. This is probably a bug -- shall we warn? */
12832 size
= addend
+ file_align
;
12835 size
= (size
+ file_align
- 1) & -file_align
;
12837 /* Allocate one extra entry for use as a "done" flag for the
12838 consolidation pass. */
12839 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
12843 ptr
= (bfd_boolean
*) bfd_realloc (ptr
- 1, bytes
);
12849 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
12850 * sizeof (bfd_boolean
));
12851 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
12855 ptr
= (bfd_boolean
*) bfd_zmalloc (bytes
);
12860 /* And arrange for that done flag to be at index -1. */
12861 h
->vtable
->used
= ptr
+ 1;
12862 h
->vtable
->size
= size
;
12865 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
12870 /* Map an ELF section header flag to its corresponding string. */
12874 flagword flag_value
;
12875 } elf_flags_to_name_table
;
12877 static elf_flags_to_name_table elf_flags_to_names
[] =
12879 { "SHF_WRITE", SHF_WRITE
},
12880 { "SHF_ALLOC", SHF_ALLOC
},
12881 { "SHF_EXECINSTR", SHF_EXECINSTR
},
12882 { "SHF_MERGE", SHF_MERGE
},
12883 { "SHF_STRINGS", SHF_STRINGS
},
12884 { "SHF_INFO_LINK", SHF_INFO_LINK
},
12885 { "SHF_LINK_ORDER", SHF_LINK_ORDER
},
12886 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING
},
12887 { "SHF_GROUP", SHF_GROUP
},
12888 { "SHF_TLS", SHF_TLS
},
12889 { "SHF_MASKOS", SHF_MASKOS
},
12890 { "SHF_EXCLUDE", SHF_EXCLUDE
},
12893 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12895 bfd_elf_lookup_section_flags (struct bfd_link_info
*info
,
12896 struct flag_info
*flaginfo
,
12899 const bfd_vma sh_flags
= elf_section_flags (section
);
12901 if (!flaginfo
->flags_initialized
)
12903 bfd
*obfd
= info
->output_bfd
;
12904 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
12905 struct flag_info_list
*tf
= flaginfo
->flag_list
;
12907 int without_hex
= 0;
12909 for (tf
= flaginfo
->flag_list
; tf
!= NULL
; tf
= tf
->next
)
12912 flagword (*lookup
) (char *);
12914 lookup
= bed
->elf_backend_lookup_section_flags_hook
;
12915 if (lookup
!= NULL
)
12917 flagword hexval
= (*lookup
) ((char *) tf
->name
);
12921 if (tf
->with
== with_flags
)
12922 with_hex
|= hexval
;
12923 else if (tf
->with
== without_flags
)
12924 without_hex
|= hexval
;
12929 for (i
= 0; i
< ARRAY_SIZE (elf_flags_to_names
); ++i
)
12931 if (strcmp (tf
->name
, elf_flags_to_names
[i
].flag_name
) == 0)
12933 if (tf
->with
== with_flags
)
12934 with_hex
|= elf_flags_to_names
[i
].flag_value
;
12935 else if (tf
->with
== without_flags
)
12936 without_hex
|= elf_flags_to_names
[i
].flag_value
;
12943 info
->callbacks
->einfo
12944 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf
->name
);
12948 flaginfo
->flags_initialized
= TRUE
;
12949 flaginfo
->only_with_flags
|= with_hex
;
12950 flaginfo
->not_with_flags
|= without_hex
;
12953 if ((flaginfo
->only_with_flags
& sh_flags
) != flaginfo
->only_with_flags
)
12956 if ((flaginfo
->not_with_flags
& sh_flags
) != 0)
12962 struct alloc_got_off_arg
{
12964 struct bfd_link_info
*info
;
12967 /* We need a special top-level link routine to convert got reference counts
12968 to real got offsets. */
12971 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
12973 struct alloc_got_off_arg
*gofarg
= (struct alloc_got_off_arg
*) arg
;
12974 bfd
*obfd
= gofarg
->info
->output_bfd
;
12975 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
12977 if (h
->got
.refcount
> 0)
12979 h
->got
.offset
= gofarg
->gotoff
;
12980 gofarg
->gotoff
+= bed
->got_elt_size (obfd
, gofarg
->info
, h
, NULL
, 0);
12983 h
->got
.offset
= (bfd_vma
) -1;
12988 /* And an accompanying bit to work out final got entry offsets once
12989 we're done. Should be called from final_link. */
12992 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
12993 struct bfd_link_info
*info
)
12996 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12998 struct alloc_got_off_arg gofarg
;
13000 BFD_ASSERT (abfd
== info
->output_bfd
);
13002 if (! is_elf_hash_table (info
->hash
))
13005 /* The GOT offset is relative to the .got section, but the GOT header is
13006 put into the .got.plt section, if the backend uses it. */
13007 if (bed
->want_got_plt
)
13010 gotoff
= bed
->got_header_size
;
13012 /* Do the local .got entries first. */
13013 for (i
= info
->input_bfds
; i
; i
= i
->link
.next
)
13015 bfd_signed_vma
*local_got
;
13016 bfd_size_type j
, locsymcount
;
13017 Elf_Internal_Shdr
*symtab_hdr
;
13019 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
13022 local_got
= elf_local_got_refcounts (i
);
13026 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
13027 if (elf_bad_symtab (i
))
13028 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
13030 locsymcount
= symtab_hdr
->sh_info
;
13032 for (j
= 0; j
< locsymcount
; ++j
)
13034 if (local_got
[j
] > 0)
13036 local_got
[j
] = gotoff
;
13037 gotoff
+= bed
->got_elt_size (abfd
, info
, NULL
, i
, j
);
13040 local_got
[j
] = (bfd_vma
) -1;
13044 /* Then the global .got entries. .plt refcounts are handled by
13045 adjust_dynamic_symbol */
13046 gofarg
.gotoff
= gotoff
;
13047 gofarg
.info
= info
;
13048 elf_link_hash_traverse (elf_hash_table (info
),
13049 elf_gc_allocate_got_offsets
,
13054 /* Many folk need no more in the way of final link than this, once
13055 got entry reference counting is enabled. */
13058 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
13060 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
13063 /* Invoke the regular ELF backend linker to do all the work. */
13064 return bfd_elf_final_link (abfd
, info
);
13068 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
13070 struct elf_reloc_cookie
*rcookie
= (struct elf_reloc_cookie
*) cookie
;
13072 if (rcookie
->bad_symtab
)
13073 rcookie
->rel
= rcookie
->rels
;
13075 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
13077 unsigned long r_symndx
;
13079 if (! rcookie
->bad_symtab
)
13080 if (rcookie
->rel
->r_offset
> offset
)
13082 if (rcookie
->rel
->r_offset
!= offset
)
13085 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
13086 if (r_symndx
== STN_UNDEF
)
13089 if (r_symndx
>= rcookie
->locsymcount
13090 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
13092 struct elf_link_hash_entry
*h
;
13094 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
13096 while (h
->root
.type
== bfd_link_hash_indirect
13097 || h
->root
.type
== bfd_link_hash_warning
)
13098 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
13100 if ((h
->root
.type
== bfd_link_hash_defined
13101 || h
->root
.type
== bfd_link_hash_defweak
)
13102 && (h
->root
.u
.def
.section
->owner
!= rcookie
->abfd
13103 || h
->root
.u
.def
.section
->kept_section
!= NULL
13104 || discarded_section (h
->root
.u
.def
.section
)))
13109 /* It's not a relocation against a global symbol,
13110 but it could be a relocation against a local
13111 symbol for a discarded section. */
13113 Elf_Internal_Sym
*isym
;
13115 /* Need to: get the symbol; get the section. */
13116 isym
= &rcookie
->locsyms
[r_symndx
];
13117 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
13119 && (isec
->kept_section
!= NULL
13120 || discarded_section (isec
)))
13128 /* Discard unneeded references to discarded sections.
13129 Returns -1 on error, 1 if any section's size was changed, 0 if
13130 nothing changed. This function assumes that the relocations are in
13131 sorted order, which is true for all known assemblers. */
13134 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
13136 struct elf_reloc_cookie cookie
;
13141 if (info
->traditional_format
13142 || !is_elf_hash_table (info
->hash
))
13145 o
= bfd_get_section_by_name (output_bfd
, ".stab");
13150 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13153 || i
->reloc_count
== 0
13154 || i
->sec_info_type
!= SEC_INFO_TYPE_STABS
)
13158 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13161 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13164 if (_bfd_discard_section_stabs (abfd
, i
,
13165 elf_section_data (i
)->sec_info
,
13166 bfd_elf_reloc_symbol_deleted_p
,
13170 fini_reloc_cookie_for_section (&cookie
, i
);
13175 if (info
->eh_frame_hdr_type
!= COMPACT_EH_HDR
)
13176 o
= bfd_get_section_by_name (output_bfd
, ".eh_frame");
13181 for (i
= o
->map_head
.s
; i
!= NULL
; i
= i
->map_head
.s
)
13187 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13190 if (!init_reloc_cookie_for_section (&cookie
, info
, i
))
13193 _bfd_elf_parse_eh_frame (abfd
, info
, i
, &cookie
);
13194 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, i
,
13195 bfd_elf_reloc_symbol_deleted_p
,
13199 fini_reloc_cookie_for_section (&cookie
, i
);
13203 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
13205 const struct elf_backend_data
*bed
;
13207 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
13210 bed
= get_elf_backend_data (abfd
);
13212 if (bed
->elf_backend_discard_info
!= NULL
)
13214 if (!init_reloc_cookie (&cookie
, info
, abfd
))
13217 if ((*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
13220 fini_reloc_cookie (&cookie
, abfd
);
13224 if (info
->eh_frame_hdr_type
== COMPACT_EH_HDR
)
13225 _bfd_elf_end_eh_frame_parsing (info
);
13227 if (info
->eh_frame_hdr_type
13228 && !bfd_link_relocatable (info
)
13229 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
13236 _bfd_elf_section_already_linked (bfd
*abfd
,
13238 struct bfd_link_info
*info
)
13241 const char *name
, *key
;
13242 struct bfd_section_already_linked
*l
;
13243 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
13245 if (sec
->output_section
== bfd_abs_section_ptr
)
13248 flags
= sec
->flags
;
13250 /* Return if it isn't a linkonce section. A comdat group section
13251 also has SEC_LINK_ONCE set. */
13252 if ((flags
& SEC_LINK_ONCE
) == 0)
13255 /* Don't put group member sections on our list of already linked
13256 sections. They are handled as a group via their group section. */
13257 if (elf_sec_group (sec
) != NULL
)
13260 /* For a SHT_GROUP section, use the group signature as the key. */
13262 if ((flags
& SEC_GROUP
) != 0
13263 && elf_next_in_group (sec
) != NULL
13264 && elf_group_name (elf_next_in_group (sec
)) != NULL
)
13265 key
= elf_group_name (elf_next_in_group (sec
));
13268 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13269 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
13270 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
13273 /* Must be a user linkonce section that doesn't follow gcc's
13274 naming convention. In this case we won't be matching
13275 single member groups. */
13279 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
13281 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13283 /* We may have 2 different types of sections on the list: group
13284 sections with a signature of <key> (<key> is some string),
13285 and linkonce sections named .gnu.linkonce.<type>.<key>.
13286 Match like sections. LTO plugin sections are an exception.
13287 They are always named .gnu.linkonce.t.<key> and match either
13288 type of section. */
13289 if (((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
13290 && ((flags
& SEC_GROUP
) != 0
13291 || strcmp (name
, l
->sec
->name
) == 0))
13292 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
13294 /* The section has already been linked. See if we should
13295 issue a warning. */
13296 if (!_bfd_handle_already_linked (sec
, l
, info
))
13299 if (flags
& SEC_GROUP
)
13301 asection
*first
= elf_next_in_group (sec
);
13302 asection
*s
= first
;
13306 s
->output_section
= bfd_abs_section_ptr
;
13307 /* Record which group discards it. */
13308 s
->kept_section
= l
->sec
;
13309 s
= elf_next_in_group (s
);
13310 /* These lists are circular. */
13320 /* A single member comdat group section may be discarded by a
13321 linkonce section and vice versa. */
13322 if ((flags
& SEC_GROUP
) != 0)
13324 asection
*first
= elf_next_in_group (sec
);
13326 if (first
!= NULL
&& elf_next_in_group (first
) == first
)
13327 /* Check this single member group against linkonce sections. */
13328 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13329 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13330 && bfd_elf_match_symbols_in_sections (l
->sec
, first
, info
))
13332 first
->output_section
= bfd_abs_section_ptr
;
13333 first
->kept_section
= l
->sec
;
13334 sec
->output_section
= bfd_abs_section_ptr
;
13339 /* Check this linkonce section against single member groups. */
13340 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13341 if (l
->sec
->flags
& SEC_GROUP
)
13343 asection
*first
= elf_next_in_group (l
->sec
);
13346 && elf_next_in_group (first
) == first
13347 && bfd_elf_match_symbols_in_sections (first
, sec
, info
))
13349 sec
->output_section
= bfd_abs_section_ptr
;
13350 sec
->kept_section
= first
;
13355 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13356 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13357 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13358 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13359 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13360 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13361 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13362 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13363 The reverse order cannot happen as there is never a bfd with only the
13364 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13365 matter as here were are looking only for cross-bfd sections. */
13367 if ((flags
& SEC_GROUP
) == 0 && CONST_STRNEQ (name
, ".gnu.linkonce.r."))
13368 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
13369 if ((l
->sec
->flags
& SEC_GROUP
) == 0
13370 && CONST_STRNEQ (l
->sec
->name
, ".gnu.linkonce.t."))
13372 if (abfd
!= l
->sec
->owner
)
13373 sec
->output_section
= bfd_abs_section_ptr
;
13377 /* This is the first section with this name. Record it. */
13378 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
13379 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));
13380 return sec
->output_section
== bfd_abs_section_ptr
;
13384 _bfd_elf_common_definition (Elf_Internal_Sym
*sym
)
13386 return sym
->st_shndx
== SHN_COMMON
;
13390 _bfd_elf_common_section_index (asection
*sec ATTRIBUTE_UNUSED
)
13396 _bfd_elf_common_section (asection
*sec ATTRIBUTE_UNUSED
)
13398 return bfd_com_section_ptr
;
13402 _bfd_elf_default_got_elt_size (bfd
*abfd
,
13403 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
13404 struct elf_link_hash_entry
*h ATTRIBUTE_UNUSED
,
13405 bfd
*ibfd ATTRIBUTE_UNUSED
,
13406 unsigned long symndx ATTRIBUTE_UNUSED
)
13408 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13409 return bed
->s
->arch_size
/ 8;
13412 /* Routines to support the creation of dynamic relocs. */
13414 /* Returns the name of the dynamic reloc section associated with SEC. */
13416 static const char *
13417 get_dynamic_reloc_section_name (bfd
* abfd
,
13419 bfd_boolean is_rela
)
13422 const char *old_name
= bfd_get_section_name (NULL
, sec
);
13423 const char *prefix
= is_rela
? ".rela" : ".rel";
13425 if (old_name
== NULL
)
13428 name
= bfd_alloc (abfd
, strlen (prefix
) + strlen (old_name
) + 1);
13429 sprintf (name
, "%s%s", prefix
, old_name
);
13434 /* Returns the dynamic reloc section associated with SEC.
13435 If necessary compute the name of the dynamic reloc section based
13436 on SEC's name (looked up in ABFD's string table) and the setting
13440 _bfd_elf_get_dynamic_reloc_section (bfd
* abfd
,
13442 bfd_boolean is_rela
)
13444 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13446 if (reloc_sec
== NULL
)
13448 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13452 reloc_sec
= bfd_get_linker_section (abfd
, name
);
13454 if (reloc_sec
!= NULL
)
13455 elf_section_data (sec
)->sreloc
= reloc_sec
;
13462 /* Returns the dynamic reloc section associated with SEC. If the
13463 section does not exist it is created and attached to the DYNOBJ
13464 bfd and stored in the SRELOC field of SEC's elf_section_data
13467 ALIGNMENT is the alignment for the newly created section and
13468 IS_RELA defines whether the name should be .rela.<SEC's name>
13469 or .rel.<SEC's name>. The section name is looked up in the
13470 string table associated with ABFD. */
13473 _bfd_elf_make_dynamic_reloc_section (asection
*sec
,
13475 unsigned int alignment
,
13477 bfd_boolean is_rela
)
13479 asection
* reloc_sec
= elf_section_data (sec
)->sreloc
;
13481 if (reloc_sec
== NULL
)
13483 const char * name
= get_dynamic_reloc_section_name (abfd
, sec
, is_rela
);
13488 reloc_sec
= bfd_get_linker_section (dynobj
, name
);
13490 if (reloc_sec
== NULL
)
13492 flagword flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
13493 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
13494 if ((sec
->flags
& SEC_ALLOC
) != 0)
13495 flags
|= SEC_ALLOC
| SEC_LOAD
;
13497 reloc_sec
= bfd_make_section_anyway_with_flags (dynobj
, name
, flags
);
13498 if (reloc_sec
!= NULL
)
13500 /* _bfd_elf_get_sec_type_attr chooses a section type by
13501 name. Override as it may be wrong, eg. for a user
13502 section named "auto" we'll get ".relauto" which is
13503 seen to be a .rela section. */
13504 elf_section_type (reloc_sec
) = is_rela
? SHT_RELA
: SHT_REL
;
13505 if (! bfd_set_section_alignment (dynobj
, reloc_sec
, alignment
))
13510 elf_section_data (sec
)->sreloc
= reloc_sec
;
13516 /* Copy the ELF symbol type and other attributes for a linker script
13517 assignment from HSRC to HDEST. Generally this should be treated as
13518 if we found a strong non-dynamic definition for HDEST (except that
13519 ld ignores multiple definition errors). */
13521 _bfd_elf_copy_link_hash_symbol_type (bfd
*abfd
,
13522 struct bfd_link_hash_entry
*hdest
,
13523 struct bfd_link_hash_entry
*hsrc
)
13525 struct elf_link_hash_entry
*ehdest
= (struct elf_link_hash_entry
*) hdest
;
13526 struct elf_link_hash_entry
*ehsrc
= (struct elf_link_hash_entry
*) hsrc
;
13527 Elf_Internal_Sym isym
;
13529 ehdest
->type
= ehsrc
->type
;
13530 ehdest
->target_internal
= ehsrc
->target_internal
;
13532 isym
.st_other
= ehsrc
->other
;
13533 elf_merge_st_other (abfd
, ehdest
, &isym
, NULL
, TRUE
, FALSE
);
13536 /* Append a RELA relocation REL to section S in BFD. */
13539 elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13541 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13542 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
13543 BFD_ASSERT (loc
+ bed
->s
->sizeof_rela
<= s
->contents
+ s
->size
);
13544 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
13547 /* Append a REL relocation REL to section S in BFD. */
13550 elf_append_rel (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
13552 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
13553 bfd_byte
*loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rel
);
13554 BFD_ASSERT (loc
+ bed
->s
->sizeof_rel
<= s
->contents
+ s
->size
);
13555 bed
->s
->swap_reloc_out (abfd
, rel
, loc
);