1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
35 struct elf_link_hash_entry
*h
;
36 struct bfd_link_hash_entry
*bh
;
37 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
40 /* This function may be called more than once. */
41 s
= bfd_get_section_by_name (abfd
, ".got");
42 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
45 switch (bed
->s
->arch_size
)
56 bfd_set_error (bfd_error_bad_value
);
60 flags
= bed
->dynamic_sec_flags
;
62 s
= bfd_make_section (abfd
, ".got");
64 || !bfd_set_section_flags (abfd
, s
, flags
)
65 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
68 if (bed
->want_got_plt
)
70 s
= bfd_make_section (abfd
, ".got.plt");
72 || !bfd_set_section_flags (abfd
, s
, flags
)
73 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
77 if (bed
->want_got_sym
)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
86 bed
->got_symbol_offset
, NULL
, FALSE
, bed
->collect
, &bh
)))
88 h
= (struct elf_link_hash_entry
*) bh
;
91 h
->other
= STV_HIDDEN
;
93 if (! info
->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
97 elf_hash_table (info
)->hgot
= h
;
100 /* The first bit of the global offset table is the header. */
101 s
->size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
106 /* Create a strtab to hold the dynamic symbol names. */
108 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
110 struct elf_link_hash_table
*hash_table
;
112 hash_table
= elf_hash_table (info
);
113 if (hash_table
->dynobj
== NULL
)
114 hash_table
->dynobj
= abfd
;
116 if (hash_table
->dynstr
== NULL
)
118 hash_table
->dynstr
= _bfd_elf_strtab_init ();
119 if (hash_table
->dynstr
== NULL
)
125 /* Create some sections which will be filled in with dynamic linking
126 information. ABFD is an input file which requires dynamic sections
127 to be created. The dynamic sections take up virtual memory space
128 when the final executable is run, so we need to create them before
129 addresses are assigned to the output sections. We work out the
130 actual contents and size of these sections later. */
133 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
136 register asection
*s
;
137 struct elf_link_hash_entry
*h
;
138 struct bfd_link_hash_entry
*bh
;
139 const struct elf_backend_data
*bed
;
141 if (! is_elf_hash_table (info
->hash
))
144 if (elf_hash_table (info
)->dynamic_sections_created
)
147 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
150 abfd
= elf_hash_table (info
)->dynobj
;
151 bed
= get_elf_backend_data (abfd
);
153 flags
= bed
->dynamic_sec_flags
;
155 /* A dynamically linked executable has a .interp section, but a
156 shared library does not. */
157 if (info
->executable
)
159 s
= bfd_make_section (abfd
, ".interp");
161 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
165 if (! info
->traditional_format
)
167 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
169 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
170 || ! bfd_set_section_alignment (abfd
, s
, 2))
172 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
175 /* Create sections to hold version informations. These are removed
176 if they are not needed. */
177 s
= bfd_make_section (abfd
, ".gnu.version_d");
179 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
180 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
183 s
= bfd_make_section (abfd
, ".gnu.version");
185 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
186 || ! bfd_set_section_alignment (abfd
, s
, 1))
189 s
= bfd_make_section (abfd
, ".gnu.version_r");
191 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
192 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
195 s
= bfd_make_section (abfd
, ".dynsym");
197 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
198 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
201 s
= bfd_make_section (abfd
, ".dynstr");
203 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
206 s
= bfd_make_section (abfd
, ".dynamic");
208 || ! bfd_set_section_flags (abfd
, s
, flags
)
209 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
212 /* The special symbol _DYNAMIC is always set to the start of the
213 .dynamic section. We could set _DYNAMIC in a linker script, but we
214 only want to define it if we are, in fact, creating a .dynamic
215 section. We don't want to define it if there is no .dynamic
216 section, since on some ELF platforms the start up code examines it
217 to decide how to initialize the process. */
218 h
= elf_link_hash_lookup (elf_hash_table (info
), "_DYNAMIC",
219 FALSE
, FALSE
, FALSE
);
222 /* Zap symbol defined in an as-needed lib that wasn't linked.
223 This is a symptom of a larger problem: Absolute symbols
224 defined in shared libraries can't be overridden, because we
225 lose the link to the bfd which is via the symbol section. */
226 h
->root
.type
= bfd_link_hash_new
;
229 if (! (_bfd_generic_link_add_one_symbol
230 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
231 get_elf_backend_data (abfd
)->collect
, &bh
)))
233 h
= (struct elf_link_hash_entry
*) bh
;
235 h
->type
= STT_OBJECT
;
237 if (! info
->executable
238 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
241 s
= bfd_make_section (abfd
, ".hash");
243 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
244 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
246 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
248 /* Let the backend create the rest of the sections. This lets the
249 backend set the right flags. The backend will normally create
250 the .got and .plt sections. */
251 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
254 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
259 /* Create dynamic sections when linking against a dynamic object. */
262 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
264 flagword flags
, pltflags
;
266 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
268 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
269 .rel[a].bss sections. */
270 flags
= bed
->dynamic_sec_flags
;
273 if (bed
->plt_not_loaded
)
274 /* We do not clear SEC_ALLOC here because we still want the OS to
275 allocate space for the section; it's just that there's nothing
276 to read in from the object file. */
277 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
279 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
280 if (bed
->plt_readonly
)
281 pltflags
|= SEC_READONLY
;
283 s
= bfd_make_section (abfd
, ".plt");
285 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
286 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
289 if (bed
->want_plt_sym
)
291 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
293 struct elf_link_hash_entry
*h
;
294 struct bfd_link_hash_entry
*bh
= NULL
;
296 if (! (_bfd_generic_link_add_one_symbol
297 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
298 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
300 h
= (struct elf_link_hash_entry
*) bh
;
302 h
->type
= STT_OBJECT
;
304 if (! info
->executable
305 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
309 s
= bfd_make_section (abfd
,
310 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
312 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
313 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
316 if (! _bfd_elf_create_got_section (abfd
, info
))
319 if (bed
->want_dynbss
)
321 /* The .dynbss section is a place to put symbols which are defined
322 by dynamic objects, are referenced by regular objects, and are
323 not functions. We must allocate space for them in the process
324 image and use a R_*_COPY reloc to tell the dynamic linker to
325 initialize them at run time. The linker script puts the .dynbss
326 section into the .bss section of the final image. */
327 s
= bfd_make_section (abfd
, ".dynbss");
329 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
332 /* The .rel[a].bss section holds copy relocs. This section is not
333 normally needed. We need to create it here, though, so that the
334 linker will map it to an output section. We can't just create it
335 only if we need it, because we will not know whether we need it
336 until we have seen all the input files, and the first time the
337 main linker code calls BFD after examining all the input files
338 (size_dynamic_sections) the input sections have already been
339 mapped to the output sections. If the section turns out not to
340 be needed, we can discard it later. We will never need this
341 section when generating a shared object, since they do not use
345 s
= bfd_make_section (abfd
,
346 (bed
->default_use_rela_p
347 ? ".rela.bss" : ".rel.bss"));
349 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
350 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
358 /* Record a new dynamic symbol. We record the dynamic symbols as we
359 read the input files, since we need to have a list of all of them
360 before we can determine the final sizes of the output sections.
361 Note that we may actually call this function even though we are not
362 going to output any dynamic symbols; in some cases we know that a
363 symbol should be in the dynamic symbol table, but only if there is
367 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
368 struct elf_link_hash_entry
*h
)
370 if (h
->dynindx
== -1)
372 struct elf_strtab_hash
*dynstr
;
377 /* XXX: The ABI draft says the linker must turn hidden and
378 internal symbols into STB_LOCAL symbols when producing the
379 DSO. However, if ld.so honors st_other in the dynamic table,
380 this would not be necessary. */
381 switch (ELF_ST_VISIBILITY (h
->other
))
385 if (h
->root
.type
!= bfd_link_hash_undefined
386 && h
->root
.type
!= bfd_link_hash_undefweak
)
396 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
397 ++elf_hash_table (info
)->dynsymcount
;
399 dynstr
= elf_hash_table (info
)->dynstr
;
402 /* Create a strtab to hold the dynamic symbol names. */
403 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
408 /* We don't put any version information in the dynamic string
410 name
= h
->root
.root
.string
;
411 p
= strchr (name
, ELF_VER_CHR
);
413 /* We know that the p points into writable memory. In fact,
414 there are only a few symbols that have read-only names, being
415 those like _GLOBAL_OFFSET_TABLE_ that are created specially
416 by the backends. Most symbols will have names pointing into
417 an ELF string table read from a file, or to objalloc memory. */
420 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
425 if (indx
== (bfd_size_type
) -1)
427 h
->dynstr_index
= indx
;
433 /* Record an assignment to a symbol made by a linker script. We need
434 this in case some dynamic object refers to this symbol. */
437 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
438 struct bfd_link_info
*info
,
442 struct elf_link_hash_entry
*h
;
444 if (!is_elf_hash_table (info
->hash
))
447 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
451 /* Since we're defining the symbol, don't let it seem to have not
452 been defined. record_dynamic_symbol and size_dynamic_sections
453 may depend on this. */
454 if (h
->root
.type
== bfd_link_hash_undefweak
455 || h
->root
.type
== bfd_link_hash_undefined
)
457 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
459 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
460 bfd_link_repair_undef_list (&htab
->root
);
461 h
->root
.type
= bfd_link_hash_new
;
464 if (h
->root
.type
== bfd_link_hash_new
)
467 /* If this symbol is being provided by the linker script, and it is
468 currently defined by a dynamic object, but not by a regular
469 object, then mark it as undefined so that the generic linker will
470 force the correct value. */
474 h
->root
.type
= bfd_link_hash_undefined
;
476 /* If this symbol is not being provided by the linker script, and it is
477 currently defined by a dynamic object, but not by a regular object,
478 then clear out any version information because the symbol will not be
479 associated with the dynamic object any more. */
483 h
->verinfo
.verdef
= NULL
;
492 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
495 /* If this is a weak defined symbol, and we know a corresponding
496 real symbol from the same dynamic object, make sure the real
497 symbol is also made into a dynamic symbol. */
498 if (h
->u
.weakdef
!= NULL
499 && h
->u
.weakdef
->dynindx
== -1)
501 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
509 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
510 success, and 2 on a failure caused by attempting to record a symbol
511 in a discarded section, eg. a discarded link-once section symbol. */
514 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
519 struct elf_link_local_dynamic_entry
*entry
;
520 struct elf_link_hash_table
*eht
;
521 struct elf_strtab_hash
*dynstr
;
522 unsigned long dynstr_index
;
524 Elf_External_Sym_Shndx eshndx
;
525 char esym
[sizeof (Elf64_External_Sym
)];
527 if (! is_elf_hash_table (info
->hash
))
530 /* See if the entry exists already. */
531 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
532 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
535 amt
= sizeof (*entry
);
536 entry
= bfd_alloc (input_bfd
, amt
);
540 /* Go find the symbol, so that we can find it's name. */
541 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
542 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
544 bfd_release (input_bfd
, entry
);
548 if (entry
->isym
.st_shndx
!= SHN_UNDEF
549 && (entry
->isym
.st_shndx
< SHN_LORESERVE
550 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
554 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
555 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
557 /* We can still bfd_release here as nothing has done another
558 bfd_alloc. We can't do this later in this function. */
559 bfd_release (input_bfd
, entry
);
564 name
= (bfd_elf_string_from_elf_section
565 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
566 entry
->isym
.st_name
));
568 dynstr
= elf_hash_table (info
)->dynstr
;
571 /* Create a strtab to hold the dynamic symbol names. */
572 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
577 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
578 if (dynstr_index
== (unsigned long) -1)
580 entry
->isym
.st_name
= dynstr_index
;
582 eht
= elf_hash_table (info
);
584 entry
->next
= eht
->dynlocal
;
585 eht
->dynlocal
= entry
;
586 entry
->input_bfd
= input_bfd
;
587 entry
->input_indx
= input_indx
;
590 /* Whatever binding the symbol had before, it's now local. */
592 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
594 /* The dynindx will be set at the end of size_dynamic_sections. */
599 /* Return the dynindex of a local dynamic symbol. */
602 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
606 struct elf_link_local_dynamic_entry
*e
;
608 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
609 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
614 /* This function is used to renumber the dynamic symbols, if some of
615 them are removed because they are marked as local. This is called
616 via elf_link_hash_traverse. */
619 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
622 size_t *count
= data
;
624 if (h
->root
.type
== bfd_link_hash_warning
)
625 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
627 if (h
->dynindx
!= -1)
628 h
->dynindx
= ++(*count
);
633 /* Return true if the dynamic symbol for a given section should be
634 omitted when creating a shared library. */
636 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
637 struct bfd_link_info
*info
,
640 switch (elf_section_data (p
)->this_hdr
.sh_type
)
644 /* If sh_type is yet undecided, assume it could be
645 SHT_PROGBITS/SHT_NOBITS. */
647 if (strcmp (p
->name
, ".got") == 0
648 || strcmp (p
->name
, ".got.plt") == 0
649 || strcmp (p
->name
, ".plt") == 0)
652 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
655 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
656 && (ip
->flags
& SEC_LINKER_CREATED
)
657 && ip
->output_section
== p
)
662 /* There shouldn't be section relative relocations
663 against any other section. */
669 /* Assign dynsym indices. In a shared library we generate a section
670 symbol for each output section, which come first. Next come all of
671 the back-end allocated local dynamic syms, followed by the rest of
672 the global symbols. */
675 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
677 unsigned long dynsymcount
= 0;
681 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
683 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
684 if ((p
->flags
& SEC_EXCLUDE
) == 0
685 && (p
->flags
& SEC_ALLOC
) != 0
686 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
687 elf_section_data (p
)->dynindx
= ++dynsymcount
;
690 if (elf_hash_table (info
)->dynlocal
)
692 struct elf_link_local_dynamic_entry
*p
;
693 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
694 p
->dynindx
= ++dynsymcount
;
697 elf_link_hash_traverse (elf_hash_table (info
),
698 elf_link_renumber_hash_table_dynsyms
,
701 /* There is an unused NULL entry at the head of the table which
702 we must account for in our count. Unless there weren't any
703 symbols, which means we'll have no table at all. */
704 if (dynsymcount
!= 0)
707 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
710 /* This function is called when we want to define a new symbol. It
711 handles the various cases which arise when we find a definition in
712 a dynamic object, or when there is already a definition in a
713 dynamic object. The new symbol is described by NAME, SYM, PSEC,
714 and PVALUE. We set SYM_HASH to the hash table entry. We set
715 OVERRIDE if the old symbol is overriding a new definition. We set
716 TYPE_CHANGE_OK if it is OK for the type to change. We set
717 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
718 change, we mean that we shouldn't warn if the type or size does
722 _bfd_elf_merge_symbol (bfd
*abfd
,
723 struct bfd_link_info
*info
,
725 Elf_Internal_Sym
*sym
,
728 struct elf_link_hash_entry
**sym_hash
,
730 bfd_boolean
*override
,
731 bfd_boolean
*type_change_ok
,
732 bfd_boolean
*size_change_ok
)
734 asection
*sec
, *oldsec
;
735 struct elf_link_hash_entry
*h
;
736 struct elf_link_hash_entry
*flip
;
739 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
740 bfd_boolean newweak
, oldweak
;
746 bind
= ELF_ST_BIND (sym
->st_info
);
748 if (! bfd_is_und_section (sec
))
749 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
751 h
= ((struct elf_link_hash_entry
*)
752 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
757 /* This code is for coping with dynamic objects, and is only useful
758 if we are doing an ELF link. */
759 if (info
->hash
->creator
!= abfd
->xvec
)
762 /* For merging, we only care about real symbols. */
764 while (h
->root
.type
== bfd_link_hash_indirect
765 || h
->root
.type
== bfd_link_hash_warning
)
766 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
768 /* If we just created the symbol, mark it as being an ELF symbol.
769 Other than that, there is nothing to do--there is no merge issue
770 with a newly defined symbol--so we just return. */
772 if (h
->root
.type
== bfd_link_hash_new
)
778 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
781 switch (h
->root
.type
)
788 case bfd_link_hash_undefined
:
789 case bfd_link_hash_undefweak
:
790 oldbfd
= h
->root
.u
.undef
.abfd
;
794 case bfd_link_hash_defined
:
795 case bfd_link_hash_defweak
:
796 oldbfd
= h
->root
.u
.def
.section
->owner
;
797 oldsec
= h
->root
.u
.def
.section
;
800 case bfd_link_hash_common
:
801 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
802 oldsec
= h
->root
.u
.c
.p
->section
;
806 /* In cases involving weak versioned symbols, we may wind up trying
807 to merge a symbol with itself. Catch that here, to avoid the
808 confusion that results if we try to override a symbol with
809 itself. The additional tests catch cases like
810 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
811 dynamic object, which we do want to handle here. */
813 && ((abfd
->flags
& DYNAMIC
) == 0
817 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
818 respectively, is from a dynamic object. */
820 if ((abfd
->flags
& DYNAMIC
) != 0)
826 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
831 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
832 indices used by MIPS ELF. */
833 switch (h
->root
.type
)
839 case bfd_link_hash_defined
:
840 case bfd_link_hash_defweak
:
841 hsec
= h
->root
.u
.def
.section
;
844 case bfd_link_hash_common
:
845 hsec
= h
->root
.u
.c
.p
->section
;
852 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
855 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
856 respectively, appear to be a definition rather than reference. */
858 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
863 if (h
->root
.type
== bfd_link_hash_undefined
864 || h
->root
.type
== bfd_link_hash_undefweak
865 || h
->root
.type
== bfd_link_hash_common
)
870 /* Check TLS symbol. */
871 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
872 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
875 bfd_boolean ntdef
, tdef
;
876 asection
*ntsec
, *tsec
;
878 if (h
->type
== STT_TLS
)
898 (*_bfd_error_handler
)
899 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
900 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
901 else if (!tdef
&& !ntdef
)
902 (*_bfd_error_handler
)
903 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
904 tbfd
, ntbfd
, h
->root
.root
.string
);
906 (*_bfd_error_handler
)
907 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
908 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
910 (*_bfd_error_handler
)
911 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
912 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
914 bfd_set_error (bfd_error_bad_value
);
918 /* We need to remember if a symbol has a definition in a dynamic
919 object or is weak in all dynamic objects. Internal and hidden
920 visibility will make it unavailable to dynamic objects. */
921 if (newdyn
&& !h
->dynamic_def
)
923 if (!bfd_is_und_section (sec
))
927 /* Check if this symbol is weak in all dynamic objects. If it
928 is the first time we see it in a dynamic object, we mark
929 if it is weak. Otherwise, we clear it. */
932 if (bind
== STB_WEAK
)
935 else if (bind
!= STB_WEAK
)
940 /* If the old symbol has non-default visibility, we ignore the new
941 definition from a dynamic object. */
943 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
944 && !bfd_is_und_section (sec
))
947 /* Make sure this symbol is dynamic. */
949 /* A protected symbol has external availability. Make sure it is
952 FIXME: Should we check type and size for protected symbol? */
953 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
954 return bfd_elf_link_record_dynamic_symbol (info
, h
);
959 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
962 /* If the new symbol with non-default visibility comes from a
963 relocatable file and the old definition comes from a dynamic
964 object, we remove the old definition. */
965 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
968 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
969 && bfd_is_und_section (sec
))
971 /* If the new symbol is undefined and the old symbol was
972 also undefined before, we need to make sure
973 _bfd_generic_link_add_one_symbol doesn't mess
974 up the linker hash table undefs list. Since the old
975 definition came from a dynamic object, it is still on the
977 h
->root
.type
= bfd_link_hash_undefined
;
978 h
->root
.u
.undef
.abfd
= abfd
;
982 h
->root
.type
= bfd_link_hash_new
;
983 h
->root
.u
.undef
.abfd
= NULL
;
992 /* FIXME: Should we check type and size for protected symbol? */
998 /* Differentiate strong and weak symbols. */
999 newweak
= bind
== STB_WEAK
;
1000 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1001 || h
->root
.type
== bfd_link_hash_undefweak
);
1003 /* If a new weak symbol definition comes from a regular file and the
1004 old symbol comes from a dynamic library, we treat the new one as
1005 strong. Similarly, an old weak symbol definition from a regular
1006 file is treated as strong when the new symbol comes from a dynamic
1007 library. Further, an old weak symbol from a dynamic library is
1008 treated as strong if the new symbol is from a dynamic library.
1009 This reflects the way glibc's ld.so works.
1011 Do this before setting *type_change_ok or *size_change_ok so that
1012 we warn properly when dynamic library symbols are overridden. */
1014 if (newdef
&& !newdyn
&& olddyn
)
1016 if (olddef
&& newdyn
)
1019 /* It's OK to change the type if either the existing symbol or the
1020 new symbol is weak. A type change is also OK if the old symbol
1021 is undefined and the new symbol is defined. */
1026 && h
->root
.type
== bfd_link_hash_undefined
))
1027 *type_change_ok
= TRUE
;
1029 /* It's OK to change the size if either the existing symbol or the
1030 new symbol is weak, or if the old symbol is undefined. */
1033 || h
->root
.type
== bfd_link_hash_undefined
)
1034 *size_change_ok
= TRUE
;
1036 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1037 symbol, respectively, appears to be a common symbol in a dynamic
1038 object. If a symbol appears in an uninitialized section, and is
1039 not weak, and is not a function, then it may be a common symbol
1040 which was resolved when the dynamic object was created. We want
1041 to treat such symbols specially, because they raise special
1042 considerations when setting the symbol size: if the symbol
1043 appears as a common symbol in a regular object, and the size in
1044 the regular object is larger, we must make sure that we use the
1045 larger size. This problematic case can always be avoided in C,
1046 but it must be handled correctly when using Fortran shared
1049 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1050 likewise for OLDDYNCOMMON and OLDDEF.
1052 Note that this test is just a heuristic, and that it is quite
1053 possible to have an uninitialized symbol in a shared object which
1054 is really a definition, rather than a common symbol. This could
1055 lead to some minor confusion when the symbol really is a common
1056 symbol in some regular object. However, I think it will be
1062 && (sec
->flags
& SEC_ALLOC
) != 0
1063 && (sec
->flags
& SEC_LOAD
) == 0
1065 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1066 newdyncommon
= TRUE
;
1068 newdyncommon
= FALSE
;
1072 && h
->root
.type
== bfd_link_hash_defined
1074 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1075 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1077 && h
->type
!= STT_FUNC
)
1078 olddyncommon
= TRUE
;
1080 olddyncommon
= FALSE
;
1082 /* If both the old and the new symbols look like common symbols in a
1083 dynamic object, set the size of the symbol to the larger of the
1088 && sym
->st_size
!= h
->size
)
1090 /* Since we think we have two common symbols, issue a multiple
1091 common warning if desired. Note that we only warn if the
1092 size is different. If the size is the same, we simply let
1093 the old symbol override the new one as normally happens with
1094 symbols defined in dynamic objects. */
1096 if (! ((*info
->callbacks
->multiple_common
)
1097 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1098 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1101 if (sym
->st_size
> h
->size
)
1102 h
->size
= sym
->st_size
;
1104 *size_change_ok
= TRUE
;
1107 /* If we are looking at a dynamic object, and we have found a
1108 definition, we need to see if the symbol was already defined by
1109 some other object. If so, we want to use the existing
1110 definition, and we do not want to report a multiple symbol
1111 definition error; we do this by clobbering *PSEC to be
1112 bfd_und_section_ptr.
1114 We treat a common symbol as a definition if the symbol in the
1115 shared library is a function, since common symbols always
1116 represent variables; this can cause confusion in principle, but
1117 any such confusion would seem to indicate an erroneous program or
1118 shared library. We also permit a common symbol in a regular
1119 object to override a weak symbol in a shared object. */
1124 || (h
->root
.type
== bfd_link_hash_common
1126 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1130 newdyncommon
= FALSE
;
1132 *psec
= sec
= bfd_und_section_ptr
;
1133 *size_change_ok
= TRUE
;
1135 /* If we get here when the old symbol is a common symbol, then
1136 we are explicitly letting it override a weak symbol or
1137 function in a dynamic object, and we don't want to warn about
1138 a type change. If the old symbol is a defined symbol, a type
1139 change warning may still be appropriate. */
1141 if (h
->root
.type
== bfd_link_hash_common
)
1142 *type_change_ok
= TRUE
;
1145 /* Handle the special case of an old common symbol merging with a
1146 new symbol which looks like a common symbol in a shared object.
1147 We change *PSEC and *PVALUE to make the new symbol look like a
1148 common symbol, and let _bfd_generic_link_add_one_symbol will do
1152 && h
->root
.type
== bfd_link_hash_common
)
1156 newdyncommon
= FALSE
;
1157 *pvalue
= sym
->st_size
;
1158 *psec
= sec
= bfd_com_section_ptr
;
1159 *size_change_ok
= TRUE
;
1162 /* If the old symbol is from a dynamic object, and the new symbol is
1163 a definition which is not from a dynamic object, then the new
1164 symbol overrides the old symbol. Symbols from regular files
1165 always take precedence over symbols from dynamic objects, even if
1166 they are defined after the dynamic object in the link.
1168 As above, we again permit a common symbol in a regular object to
1169 override a definition in a shared object if the shared object
1170 symbol is a function or is weak. */
1175 || (bfd_is_com_section (sec
)
1177 || h
->type
== STT_FUNC
)))
1182 /* Change the hash table entry to undefined, and let
1183 _bfd_generic_link_add_one_symbol do the right thing with the
1186 h
->root
.type
= bfd_link_hash_undefined
;
1187 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1188 *size_change_ok
= TRUE
;
1191 olddyncommon
= FALSE
;
1193 /* We again permit a type change when a common symbol may be
1194 overriding a function. */
1196 if (bfd_is_com_section (sec
))
1197 *type_change_ok
= TRUE
;
1199 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1202 /* This union may have been set to be non-NULL when this symbol
1203 was seen in a dynamic object. We must force the union to be
1204 NULL, so that it is correct for a regular symbol. */
1205 h
->verinfo
.vertree
= NULL
;
1208 /* Handle the special case of a new common symbol merging with an
1209 old symbol that looks like it might be a common symbol defined in
1210 a shared object. Note that we have already handled the case in
1211 which a new common symbol should simply override the definition
1212 in the shared library. */
1215 && bfd_is_com_section (sec
)
1218 /* It would be best if we could set the hash table entry to a
1219 common symbol, but we don't know what to use for the section
1220 or the alignment. */
1221 if (! ((*info
->callbacks
->multiple_common
)
1222 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1223 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1226 /* If the presumed common symbol in the dynamic object is
1227 larger, pretend that the new symbol has its size. */
1229 if (h
->size
> *pvalue
)
1232 /* FIXME: We no longer know the alignment required by the symbol
1233 in the dynamic object, so we just wind up using the one from
1234 the regular object. */
1237 olddyncommon
= FALSE
;
1239 h
->root
.type
= bfd_link_hash_undefined
;
1240 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1242 *size_change_ok
= TRUE
;
1243 *type_change_ok
= TRUE
;
1245 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1248 h
->verinfo
.vertree
= NULL
;
1253 /* Handle the case where we had a versioned symbol in a dynamic
1254 library and now find a definition in a normal object. In this
1255 case, we make the versioned symbol point to the normal one. */
1256 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1257 flip
->root
.type
= h
->root
.type
;
1258 h
->root
.type
= bfd_link_hash_indirect
;
1259 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1260 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1261 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1265 flip
->ref_dynamic
= 1;
1272 /* This function is called to create an indirect symbol from the
1273 default for the symbol with the default version if needed. The
1274 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1275 set DYNSYM if the new indirect symbol is dynamic. */
1278 _bfd_elf_add_default_symbol (bfd
*abfd
,
1279 struct bfd_link_info
*info
,
1280 struct elf_link_hash_entry
*h
,
1282 Elf_Internal_Sym
*sym
,
1285 bfd_boolean
*dynsym
,
1286 bfd_boolean override
)
1288 bfd_boolean type_change_ok
;
1289 bfd_boolean size_change_ok
;
1292 struct elf_link_hash_entry
*hi
;
1293 struct bfd_link_hash_entry
*bh
;
1294 const struct elf_backend_data
*bed
;
1295 bfd_boolean collect
;
1296 bfd_boolean dynamic
;
1298 size_t len
, shortlen
;
1301 /* If this symbol has a version, and it is the default version, we
1302 create an indirect symbol from the default name to the fully
1303 decorated name. This will cause external references which do not
1304 specify a version to be bound to this version of the symbol. */
1305 p
= strchr (name
, ELF_VER_CHR
);
1306 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1311 /* We are overridden by an old definition. We need to check if we
1312 need to create the indirect symbol from the default name. */
1313 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1315 BFD_ASSERT (hi
!= NULL
);
1318 while (hi
->root
.type
== bfd_link_hash_indirect
1319 || hi
->root
.type
== bfd_link_hash_warning
)
1321 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1327 bed
= get_elf_backend_data (abfd
);
1328 collect
= bed
->collect
;
1329 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1331 shortlen
= p
- name
;
1332 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1333 if (shortname
== NULL
)
1335 memcpy (shortname
, name
, shortlen
);
1336 shortname
[shortlen
] = '\0';
1338 /* We are going to create a new symbol. Merge it with any existing
1339 symbol with this name. For the purposes of the merge, act as
1340 though we were defining the symbol we just defined, although we
1341 actually going to define an indirect symbol. */
1342 type_change_ok
= FALSE
;
1343 size_change_ok
= FALSE
;
1345 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1346 &hi
, &skip
, &override
, &type_change_ok
,
1356 if (! (_bfd_generic_link_add_one_symbol
1357 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1358 0, name
, FALSE
, collect
, &bh
)))
1360 hi
= (struct elf_link_hash_entry
*) bh
;
1364 /* In this case the symbol named SHORTNAME is overriding the
1365 indirect symbol we want to add. We were planning on making
1366 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1367 is the name without a version. NAME is the fully versioned
1368 name, and it is the default version.
1370 Overriding means that we already saw a definition for the
1371 symbol SHORTNAME in a regular object, and it is overriding
1372 the symbol defined in the dynamic object.
1374 When this happens, we actually want to change NAME, the
1375 symbol we just added, to refer to SHORTNAME. This will cause
1376 references to NAME in the shared object to become references
1377 to SHORTNAME in the regular object. This is what we expect
1378 when we override a function in a shared object: that the
1379 references in the shared object will be mapped to the
1380 definition in the regular object. */
1382 while (hi
->root
.type
== bfd_link_hash_indirect
1383 || hi
->root
.type
== bfd_link_hash_warning
)
1384 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1386 h
->root
.type
= bfd_link_hash_indirect
;
1387 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1391 hi
->ref_dynamic
= 1;
1395 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1400 /* Now set HI to H, so that the following code will set the
1401 other fields correctly. */
1405 /* If there is a duplicate definition somewhere, then HI may not
1406 point to an indirect symbol. We will have reported an error to
1407 the user in that case. */
1409 if (hi
->root
.type
== bfd_link_hash_indirect
)
1411 struct elf_link_hash_entry
*ht
;
1413 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1414 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1416 /* See if the new flags lead us to realize that the symbol must
1428 if (hi
->ref_regular
)
1434 /* We also need to define an indirection from the nondefault version
1438 len
= strlen (name
);
1439 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1440 if (shortname
== NULL
)
1442 memcpy (shortname
, name
, shortlen
);
1443 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1445 /* Once again, merge with any existing symbol. */
1446 type_change_ok
= FALSE
;
1447 size_change_ok
= FALSE
;
1449 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1450 &hi
, &skip
, &override
, &type_change_ok
,
1459 /* Here SHORTNAME is a versioned name, so we don't expect to see
1460 the type of override we do in the case above unless it is
1461 overridden by a versioned definition. */
1462 if (hi
->root
.type
!= bfd_link_hash_defined
1463 && hi
->root
.type
!= bfd_link_hash_defweak
)
1464 (*_bfd_error_handler
)
1465 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1471 if (! (_bfd_generic_link_add_one_symbol
1472 (info
, abfd
, shortname
, BSF_INDIRECT
,
1473 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1475 hi
= (struct elf_link_hash_entry
*) bh
;
1477 /* If there is a duplicate definition somewhere, then HI may not
1478 point to an indirect symbol. We will have reported an error
1479 to the user in that case. */
1481 if (hi
->root
.type
== bfd_link_hash_indirect
)
1483 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1485 /* See if the new flags lead us to realize that the symbol
1497 if (hi
->ref_regular
)
1507 /* This routine is used to export all defined symbols into the dynamic
1508 symbol table. It is called via elf_link_hash_traverse. */
1511 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1513 struct elf_info_failed
*eif
= data
;
1515 /* Ignore indirect symbols. These are added by the versioning code. */
1516 if (h
->root
.type
== bfd_link_hash_indirect
)
1519 if (h
->root
.type
== bfd_link_hash_warning
)
1520 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1522 if (h
->dynindx
== -1
1526 struct bfd_elf_version_tree
*t
;
1527 struct bfd_elf_version_expr
*d
;
1529 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1531 if (t
->globals
.list
!= NULL
)
1533 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1538 if (t
->locals
.list
!= NULL
)
1540 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1549 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1560 /* Look through the symbols which are defined in other shared
1561 libraries and referenced here. Update the list of version
1562 dependencies. This will be put into the .gnu.version_r section.
1563 This function is called via elf_link_hash_traverse. */
1566 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1569 struct elf_find_verdep_info
*rinfo
= data
;
1570 Elf_Internal_Verneed
*t
;
1571 Elf_Internal_Vernaux
*a
;
1574 if (h
->root
.type
== bfd_link_hash_warning
)
1575 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1577 /* We only care about symbols defined in shared objects with version
1582 || h
->verinfo
.verdef
== NULL
)
1585 /* See if we already know about this version. */
1586 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1588 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1591 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1592 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1598 /* This is a new version. Add it to tree we are building. */
1603 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1606 rinfo
->failed
= TRUE
;
1610 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1611 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1612 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1616 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1618 /* Note that we are copying a string pointer here, and testing it
1619 above. If bfd_elf_string_from_elf_section is ever changed to
1620 discard the string data when low in memory, this will have to be
1622 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1624 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1625 a
->vna_nextptr
= t
->vn_auxptr
;
1627 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1630 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1637 /* Figure out appropriate versions for all the symbols. We may not
1638 have the version number script until we have read all of the input
1639 files, so until that point we don't know which symbols should be
1640 local. This function is called via elf_link_hash_traverse. */
1643 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1645 struct elf_assign_sym_version_info
*sinfo
;
1646 struct bfd_link_info
*info
;
1647 const struct elf_backend_data
*bed
;
1648 struct elf_info_failed eif
;
1655 if (h
->root
.type
== bfd_link_hash_warning
)
1656 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1658 /* Fix the symbol flags. */
1661 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1664 sinfo
->failed
= TRUE
;
1668 /* We only need version numbers for symbols defined in regular
1670 if (!h
->def_regular
)
1673 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1674 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1675 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1677 struct bfd_elf_version_tree
*t
;
1682 /* There are two consecutive ELF_VER_CHR characters if this is
1683 not a hidden symbol. */
1685 if (*p
== ELF_VER_CHR
)
1691 /* If there is no version string, we can just return out. */
1699 /* Look for the version. If we find it, it is no longer weak. */
1700 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1702 if (strcmp (t
->name
, p
) == 0)
1706 struct bfd_elf_version_expr
*d
;
1708 len
= p
- h
->root
.root
.string
;
1709 alc
= bfd_malloc (len
);
1712 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1713 alc
[len
- 1] = '\0';
1714 if (alc
[len
- 2] == ELF_VER_CHR
)
1715 alc
[len
- 2] = '\0';
1717 h
->verinfo
.vertree
= t
;
1721 if (t
->globals
.list
!= NULL
)
1722 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1724 /* See if there is anything to force this symbol to
1726 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1728 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1732 && ! info
->export_dynamic
)
1733 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1741 /* If we are building an application, we need to create a
1742 version node for this version. */
1743 if (t
== NULL
&& info
->executable
)
1745 struct bfd_elf_version_tree
**pp
;
1748 /* If we aren't going to export this symbol, we don't need
1749 to worry about it. */
1750 if (h
->dynindx
== -1)
1754 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1757 sinfo
->failed
= TRUE
;
1762 t
->name_indx
= (unsigned int) -1;
1766 /* Don't count anonymous version tag. */
1767 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1769 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1771 t
->vernum
= version_index
;
1775 h
->verinfo
.vertree
= t
;
1779 /* We could not find the version for a symbol when
1780 generating a shared archive. Return an error. */
1781 (*_bfd_error_handler
)
1782 (_("%B: undefined versioned symbol name %s"),
1783 sinfo
->output_bfd
, h
->root
.root
.string
);
1784 bfd_set_error (bfd_error_bad_value
);
1785 sinfo
->failed
= TRUE
;
1793 /* If we don't have a version for this symbol, see if we can find
1795 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1797 struct bfd_elf_version_tree
*t
;
1798 struct bfd_elf_version_tree
*local_ver
;
1799 struct bfd_elf_version_expr
*d
;
1801 /* See if can find what version this symbol is in. If the
1802 symbol is supposed to be local, then don't actually register
1805 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1807 if (t
->globals
.list
!= NULL
)
1809 bfd_boolean matched
;
1813 while ((d
= (*t
->match
) (&t
->globals
, d
,
1814 h
->root
.root
.string
)) != NULL
)
1819 /* There is a version without definition. Make
1820 the symbol the default definition for this
1822 h
->verinfo
.vertree
= t
;
1830 /* There is no undefined version for this symbol. Hide the
1832 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1835 if (t
->locals
.list
!= NULL
)
1838 while ((d
= (*t
->match
) (&t
->locals
, d
,
1839 h
->root
.root
.string
)) != NULL
)
1842 /* If the match is "*", keep looking for a more
1843 explicit, perhaps even global, match.
1844 XXX: Shouldn't this be !d->wildcard instead? */
1845 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1854 if (local_ver
!= NULL
)
1856 h
->verinfo
.vertree
= local_ver
;
1857 if (h
->dynindx
!= -1
1859 && ! info
->export_dynamic
)
1861 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1869 /* Read and swap the relocs from the section indicated by SHDR. This
1870 may be either a REL or a RELA section. The relocations are
1871 translated into RELA relocations and stored in INTERNAL_RELOCS,
1872 which should have already been allocated to contain enough space.
1873 The EXTERNAL_RELOCS are a buffer where the external form of the
1874 relocations should be stored.
1876 Returns FALSE if something goes wrong. */
1879 elf_link_read_relocs_from_section (bfd
*abfd
,
1881 Elf_Internal_Shdr
*shdr
,
1882 void *external_relocs
,
1883 Elf_Internal_Rela
*internal_relocs
)
1885 const struct elf_backend_data
*bed
;
1886 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1887 const bfd_byte
*erela
;
1888 const bfd_byte
*erelaend
;
1889 Elf_Internal_Rela
*irela
;
1890 Elf_Internal_Shdr
*symtab_hdr
;
1893 /* Position ourselves at the start of the section. */
1894 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1897 /* Read the relocations. */
1898 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1901 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1902 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1904 bed
= get_elf_backend_data (abfd
);
1906 /* Convert the external relocations to the internal format. */
1907 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1908 swap_in
= bed
->s
->swap_reloc_in
;
1909 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1910 swap_in
= bed
->s
->swap_reloca_in
;
1913 bfd_set_error (bfd_error_wrong_format
);
1917 erela
= external_relocs
;
1918 erelaend
= erela
+ shdr
->sh_size
;
1919 irela
= internal_relocs
;
1920 while (erela
< erelaend
)
1924 (*swap_in
) (abfd
, erela
, irela
);
1925 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1926 if (bed
->s
->arch_size
== 64)
1928 if ((size_t) r_symndx
>= nsyms
)
1930 (*_bfd_error_handler
)
1931 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1932 " for offset 0x%lx in section `%A'"),
1934 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1935 bfd_set_error (bfd_error_bad_value
);
1938 irela
+= bed
->s
->int_rels_per_ext_rel
;
1939 erela
+= shdr
->sh_entsize
;
1945 /* Read and swap the relocs for a section O. They may have been
1946 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1947 not NULL, they are used as buffers to read into. They are known to
1948 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1949 the return value is allocated using either malloc or bfd_alloc,
1950 according to the KEEP_MEMORY argument. If O has two relocation
1951 sections (both REL and RELA relocations), then the REL_HDR
1952 relocations will appear first in INTERNAL_RELOCS, followed by the
1953 REL_HDR2 relocations. */
1956 _bfd_elf_link_read_relocs (bfd
*abfd
,
1958 void *external_relocs
,
1959 Elf_Internal_Rela
*internal_relocs
,
1960 bfd_boolean keep_memory
)
1962 Elf_Internal_Shdr
*rel_hdr
;
1963 void *alloc1
= NULL
;
1964 Elf_Internal_Rela
*alloc2
= NULL
;
1965 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1967 if (elf_section_data (o
)->relocs
!= NULL
)
1968 return elf_section_data (o
)->relocs
;
1970 if (o
->reloc_count
== 0)
1973 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1975 if (internal_relocs
== NULL
)
1979 size
= o
->reloc_count
;
1980 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1982 internal_relocs
= bfd_alloc (abfd
, size
);
1984 internal_relocs
= alloc2
= bfd_malloc (size
);
1985 if (internal_relocs
== NULL
)
1989 if (external_relocs
== NULL
)
1991 bfd_size_type size
= rel_hdr
->sh_size
;
1993 if (elf_section_data (o
)->rel_hdr2
)
1994 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1995 alloc1
= bfd_malloc (size
);
1998 external_relocs
= alloc1
;
2001 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2005 if (elf_section_data (o
)->rel_hdr2
2006 && (!elf_link_read_relocs_from_section
2008 elf_section_data (o
)->rel_hdr2
,
2009 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2010 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2011 * bed
->s
->int_rels_per_ext_rel
))))
2014 /* Cache the results for next time, if we can. */
2016 elf_section_data (o
)->relocs
= internal_relocs
;
2021 /* Don't free alloc2, since if it was allocated we are passing it
2022 back (under the name of internal_relocs). */
2024 return internal_relocs
;
2034 /* Compute the size of, and allocate space for, REL_HDR which is the
2035 section header for a section containing relocations for O. */
2038 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2039 Elf_Internal_Shdr
*rel_hdr
,
2042 bfd_size_type reloc_count
;
2043 bfd_size_type num_rel_hashes
;
2045 /* Figure out how many relocations there will be. */
2046 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2047 reloc_count
= elf_section_data (o
)->rel_count
;
2049 reloc_count
= elf_section_data (o
)->rel_count2
;
2051 num_rel_hashes
= o
->reloc_count
;
2052 if (num_rel_hashes
< reloc_count
)
2053 num_rel_hashes
= reloc_count
;
2055 /* That allows us to calculate the size of the section. */
2056 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2058 /* The contents field must last into write_object_contents, so we
2059 allocate it with bfd_alloc rather than malloc. Also since we
2060 cannot be sure that the contents will actually be filled in,
2061 we zero the allocated space. */
2062 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2063 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2066 /* We only allocate one set of hash entries, so we only do it the
2067 first time we are called. */
2068 if (elf_section_data (o
)->rel_hashes
== NULL
2071 struct elf_link_hash_entry
**p
;
2073 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2077 elf_section_data (o
)->rel_hashes
= p
;
2083 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2084 originated from the section given by INPUT_REL_HDR) to the
2088 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2089 asection
*input_section
,
2090 Elf_Internal_Shdr
*input_rel_hdr
,
2091 Elf_Internal_Rela
*internal_relocs
)
2093 Elf_Internal_Rela
*irela
;
2094 Elf_Internal_Rela
*irelaend
;
2096 Elf_Internal_Shdr
*output_rel_hdr
;
2097 asection
*output_section
;
2098 unsigned int *rel_countp
= NULL
;
2099 const struct elf_backend_data
*bed
;
2100 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2102 output_section
= input_section
->output_section
;
2103 output_rel_hdr
= NULL
;
2105 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2106 == input_rel_hdr
->sh_entsize
)
2108 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2109 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2111 else if (elf_section_data (output_section
)->rel_hdr2
2112 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2113 == input_rel_hdr
->sh_entsize
))
2115 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2116 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2120 (*_bfd_error_handler
)
2121 (_("%B: relocation size mismatch in %B section %A"),
2122 output_bfd
, input_section
->owner
, input_section
);
2123 bfd_set_error (bfd_error_wrong_object_format
);
2127 bed
= get_elf_backend_data (output_bfd
);
2128 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2129 swap_out
= bed
->s
->swap_reloc_out
;
2130 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2131 swap_out
= bed
->s
->swap_reloca_out
;
2135 erel
= output_rel_hdr
->contents
;
2136 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2137 irela
= internal_relocs
;
2138 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2139 * bed
->s
->int_rels_per_ext_rel
);
2140 while (irela
< irelaend
)
2142 (*swap_out
) (output_bfd
, irela
, erel
);
2143 irela
+= bed
->s
->int_rels_per_ext_rel
;
2144 erel
+= input_rel_hdr
->sh_entsize
;
2147 /* Bump the counter, so that we know where to add the next set of
2149 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2154 /* Fix up the flags for a symbol. This handles various cases which
2155 can only be fixed after all the input files are seen. This is
2156 currently called by both adjust_dynamic_symbol and
2157 assign_sym_version, which is unnecessary but perhaps more robust in
2158 the face of future changes. */
2161 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2162 struct elf_info_failed
*eif
)
2164 /* If this symbol was mentioned in a non-ELF file, try to set
2165 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2166 permit a non-ELF file to correctly refer to a symbol defined in
2167 an ELF dynamic object. */
2170 while (h
->root
.type
== bfd_link_hash_indirect
)
2171 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2173 if (h
->root
.type
!= bfd_link_hash_defined
2174 && h
->root
.type
!= bfd_link_hash_defweak
)
2177 h
->ref_regular_nonweak
= 1;
2181 if (h
->root
.u
.def
.section
->owner
!= NULL
2182 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2183 == bfd_target_elf_flavour
))
2186 h
->ref_regular_nonweak
= 1;
2192 if (h
->dynindx
== -1
2196 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2205 /* Unfortunately, NON_ELF is only correct if the symbol
2206 was first seen in a non-ELF file. Fortunately, if the symbol
2207 was first seen in an ELF file, we're probably OK unless the
2208 symbol was defined in a non-ELF file. Catch that case here.
2209 FIXME: We're still in trouble if the symbol was first seen in
2210 a dynamic object, and then later in a non-ELF regular object. */
2211 if ((h
->root
.type
== bfd_link_hash_defined
2212 || h
->root
.type
== bfd_link_hash_defweak
)
2214 && (h
->root
.u
.def
.section
->owner
!= NULL
2215 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2216 != bfd_target_elf_flavour
)
2217 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2218 && !h
->def_dynamic
)))
2222 /* If this is a final link, and the symbol was defined as a common
2223 symbol in a regular object file, and there was no definition in
2224 any dynamic object, then the linker will have allocated space for
2225 the symbol in a common section but the DEF_REGULAR
2226 flag will not have been set. */
2227 if (h
->root
.type
== bfd_link_hash_defined
2231 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2234 /* If -Bsymbolic was used (which means to bind references to global
2235 symbols to the definition within the shared object), and this
2236 symbol was defined in a regular object, then it actually doesn't
2237 need a PLT entry. Likewise, if the symbol has non-default
2238 visibility. If the symbol has hidden or internal visibility, we
2239 will force it local. */
2241 && eif
->info
->shared
2242 && is_elf_hash_table (eif
->info
->hash
)
2243 && (eif
->info
->symbolic
2244 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2247 const struct elf_backend_data
*bed
;
2248 bfd_boolean force_local
;
2250 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2252 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2253 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2254 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2257 /* If a weak undefined symbol has non-default visibility, we also
2258 hide it from the dynamic linker. */
2259 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2260 && h
->root
.type
== bfd_link_hash_undefweak
)
2262 const struct elf_backend_data
*bed
;
2263 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2264 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2267 /* If this is a weak defined symbol in a dynamic object, and we know
2268 the real definition in the dynamic object, copy interesting flags
2269 over to the real definition. */
2270 if (h
->u
.weakdef
!= NULL
)
2272 struct elf_link_hash_entry
*weakdef
;
2274 weakdef
= h
->u
.weakdef
;
2275 if (h
->root
.type
== bfd_link_hash_indirect
)
2276 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2278 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2279 || h
->root
.type
== bfd_link_hash_defweak
);
2280 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2281 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2282 BFD_ASSERT (weakdef
->def_dynamic
);
2284 /* If the real definition is defined by a regular object file,
2285 don't do anything special. See the longer description in
2286 _bfd_elf_adjust_dynamic_symbol, below. */
2287 if (weakdef
->def_regular
)
2288 h
->u
.weakdef
= NULL
;
2291 const struct elf_backend_data
*bed
;
2293 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2294 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2301 /* Make the backend pick a good value for a dynamic symbol. This is
2302 called via elf_link_hash_traverse, and also calls itself
2306 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2308 struct elf_info_failed
*eif
= data
;
2310 const struct elf_backend_data
*bed
;
2312 if (! is_elf_hash_table (eif
->info
->hash
))
2315 if (h
->root
.type
== bfd_link_hash_warning
)
2317 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2318 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2320 /* When warning symbols are created, they **replace** the "real"
2321 entry in the hash table, thus we never get to see the real
2322 symbol in a hash traversal. So look at it now. */
2323 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2326 /* Ignore indirect symbols. These are added by the versioning code. */
2327 if (h
->root
.type
== bfd_link_hash_indirect
)
2330 /* Fix the symbol flags. */
2331 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2334 /* If this symbol does not require a PLT entry, and it is not
2335 defined by a dynamic object, or is not referenced by a regular
2336 object, ignore it. We do have to handle a weak defined symbol,
2337 even if no regular object refers to it, if we decided to add it
2338 to the dynamic symbol table. FIXME: Do we normally need to worry
2339 about symbols which are defined by one dynamic object and
2340 referenced by another one? */
2345 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2347 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2351 /* If we've already adjusted this symbol, don't do it again. This
2352 can happen via a recursive call. */
2353 if (h
->dynamic_adjusted
)
2356 /* Don't look at this symbol again. Note that we must set this
2357 after checking the above conditions, because we may look at a
2358 symbol once, decide not to do anything, and then get called
2359 recursively later after REF_REGULAR is set below. */
2360 h
->dynamic_adjusted
= 1;
2362 /* If this is a weak definition, and we know a real definition, and
2363 the real symbol is not itself defined by a regular object file,
2364 then get a good value for the real definition. We handle the
2365 real symbol first, for the convenience of the backend routine.
2367 Note that there is a confusing case here. If the real definition
2368 is defined by a regular object file, we don't get the real symbol
2369 from the dynamic object, but we do get the weak symbol. If the
2370 processor backend uses a COPY reloc, then if some routine in the
2371 dynamic object changes the real symbol, we will not see that
2372 change in the corresponding weak symbol. This is the way other
2373 ELF linkers work as well, and seems to be a result of the shared
2376 I will clarify this issue. Most SVR4 shared libraries define the
2377 variable _timezone and define timezone as a weak synonym. The
2378 tzset call changes _timezone. If you write
2379 extern int timezone;
2381 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2382 you might expect that, since timezone is a synonym for _timezone,
2383 the same number will print both times. However, if the processor
2384 backend uses a COPY reloc, then actually timezone will be copied
2385 into your process image, and, since you define _timezone
2386 yourself, _timezone will not. Thus timezone and _timezone will
2387 wind up at different memory locations. The tzset call will set
2388 _timezone, leaving timezone unchanged. */
2390 if (h
->u
.weakdef
!= NULL
)
2392 /* If we get to this point, we know there is an implicit
2393 reference by a regular object file via the weak symbol H.
2394 FIXME: Is this really true? What if the traversal finds
2395 H->U.WEAKDEF before it finds H? */
2396 h
->u
.weakdef
->ref_regular
= 1;
2398 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2402 /* If a symbol has no type and no size and does not require a PLT
2403 entry, then we are probably about to do the wrong thing here: we
2404 are probably going to create a COPY reloc for an empty object.
2405 This case can arise when a shared object is built with assembly
2406 code, and the assembly code fails to set the symbol type. */
2408 && h
->type
== STT_NOTYPE
2410 (*_bfd_error_handler
)
2411 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2412 h
->root
.root
.string
);
2414 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2415 bed
= get_elf_backend_data (dynobj
);
2416 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2425 /* Adjust all external symbols pointing into SEC_MERGE sections
2426 to reflect the object merging within the sections. */
2429 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2433 if (h
->root
.type
== bfd_link_hash_warning
)
2434 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2436 if ((h
->root
.type
== bfd_link_hash_defined
2437 || h
->root
.type
== bfd_link_hash_defweak
)
2438 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2439 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2441 bfd
*output_bfd
= data
;
2443 h
->root
.u
.def
.value
=
2444 _bfd_merged_section_offset (output_bfd
,
2445 &h
->root
.u
.def
.section
,
2446 elf_section_data (sec
)->sec_info
,
2447 h
->root
.u
.def
.value
);
2453 /* Returns false if the symbol referred to by H should be considered
2454 to resolve local to the current module, and true if it should be
2455 considered to bind dynamically. */
2458 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2459 struct bfd_link_info
*info
,
2460 bfd_boolean ignore_protected
)
2462 bfd_boolean binding_stays_local_p
;
2467 while (h
->root
.type
== bfd_link_hash_indirect
2468 || h
->root
.type
== bfd_link_hash_warning
)
2469 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2471 /* If it was forced local, then clearly it's not dynamic. */
2472 if (h
->dynindx
== -1)
2474 if (h
->forced_local
)
2477 /* Identify the cases where name binding rules say that a
2478 visible symbol resolves locally. */
2479 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2481 switch (ELF_ST_VISIBILITY (h
->other
))
2488 /* Proper resolution for function pointer equality may require
2489 that these symbols perhaps be resolved dynamically, even though
2490 we should be resolving them to the current module. */
2491 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2492 binding_stays_local_p
= TRUE
;
2499 /* If it isn't defined locally, then clearly it's dynamic. */
2500 if (!h
->def_regular
)
2503 /* Otherwise, the symbol is dynamic if binding rules don't tell
2504 us that it remains local. */
2505 return !binding_stays_local_p
;
2508 /* Return true if the symbol referred to by H should be considered
2509 to resolve local to the current module, and false otherwise. Differs
2510 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2511 undefined symbols and weak symbols. */
2514 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2515 struct bfd_link_info
*info
,
2516 bfd_boolean local_protected
)
2518 /* If it's a local sym, of course we resolve locally. */
2522 /* Common symbols that become definitions don't get the DEF_REGULAR
2523 flag set, so test it first, and don't bail out. */
2524 if (ELF_COMMON_DEF_P (h
))
2526 /* If we don't have a definition in a regular file, then we can't
2527 resolve locally. The sym is either undefined or dynamic. */
2528 else if (!h
->def_regular
)
2531 /* Forced local symbols resolve locally. */
2532 if (h
->forced_local
)
2535 /* As do non-dynamic symbols. */
2536 if (h
->dynindx
== -1)
2539 /* At this point, we know the symbol is defined and dynamic. In an
2540 executable it must resolve locally, likewise when building symbolic
2541 shared libraries. */
2542 if (info
->executable
|| info
->symbolic
)
2545 /* Now deal with defined dynamic symbols in shared libraries. Ones
2546 with default visibility might not resolve locally. */
2547 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2550 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2551 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2554 /* STV_PROTECTED non-function symbols are local. */
2555 if (h
->type
!= STT_FUNC
)
2558 /* Function pointer equality tests may require that STV_PROTECTED
2559 symbols be treated as dynamic symbols, even when we know that the
2560 dynamic linker will resolve them locally. */
2561 return local_protected
;
2564 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2565 aligned. Returns the first TLS output section. */
2567 struct bfd_section
*
2568 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2570 struct bfd_section
*sec
, *tls
;
2571 unsigned int align
= 0;
2573 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2574 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2578 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2579 if (sec
->alignment_power
> align
)
2580 align
= sec
->alignment_power
;
2582 elf_hash_table (info
)->tls_sec
= tls
;
2584 /* Ensure the alignment of the first section is the largest alignment,
2585 so that the tls segment starts aligned. */
2587 tls
->alignment_power
= align
;
2592 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2594 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2595 Elf_Internal_Sym
*sym
)
2597 /* Local symbols do not count, but target specific ones might. */
2598 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2599 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2602 /* Function symbols do not count. */
2603 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2606 /* If the section is undefined, then so is the symbol. */
2607 if (sym
->st_shndx
== SHN_UNDEF
)
2610 /* If the symbol is defined in the common section, then
2611 it is a common definition and so does not count. */
2612 if (sym
->st_shndx
== SHN_COMMON
)
2615 /* If the symbol is in a target specific section then we
2616 must rely upon the backend to tell us what it is. */
2617 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2618 /* FIXME - this function is not coded yet:
2620 return _bfd_is_global_symbol_definition (abfd, sym);
2622 Instead for now assume that the definition is not global,
2623 Even if this is wrong, at least the linker will behave
2624 in the same way that it used to do. */
2630 /* Search the symbol table of the archive element of the archive ABFD
2631 whose archive map contains a mention of SYMDEF, and determine if
2632 the symbol is defined in this element. */
2634 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2636 Elf_Internal_Shdr
* hdr
;
2637 bfd_size_type symcount
;
2638 bfd_size_type extsymcount
;
2639 bfd_size_type extsymoff
;
2640 Elf_Internal_Sym
*isymbuf
;
2641 Elf_Internal_Sym
*isym
;
2642 Elf_Internal_Sym
*isymend
;
2645 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2649 if (! bfd_check_format (abfd
, bfd_object
))
2652 /* If we have already included the element containing this symbol in the
2653 link then we do not need to include it again. Just claim that any symbol
2654 it contains is not a definition, so that our caller will not decide to
2655 (re)include this element. */
2656 if (abfd
->archive_pass
)
2659 /* Select the appropriate symbol table. */
2660 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2661 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2663 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2665 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2667 /* The sh_info field of the symtab header tells us where the
2668 external symbols start. We don't care about the local symbols. */
2669 if (elf_bad_symtab (abfd
))
2671 extsymcount
= symcount
;
2676 extsymcount
= symcount
- hdr
->sh_info
;
2677 extsymoff
= hdr
->sh_info
;
2680 if (extsymcount
== 0)
2683 /* Read in the symbol table. */
2684 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2686 if (isymbuf
== NULL
)
2689 /* Scan the symbol table looking for SYMDEF. */
2691 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2695 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2700 if (strcmp (name
, symdef
->name
) == 0)
2702 result
= is_global_data_symbol_definition (abfd
, isym
);
2712 /* Add an entry to the .dynamic table. */
2715 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2719 struct elf_link_hash_table
*hash_table
;
2720 const struct elf_backend_data
*bed
;
2722 bfd_size_type newsize
;
2723 bfd_byte
*newcontents
;
2724 Elf_Internal_Dyn dyn
;
2726 hash_table
= elf_hash_table (info
);
2727 if (! is_elf_hash_table (hash_table
))
2730 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2732 (_("warning: creating a DT_TEXTREL in a shared object."));
2734 bed
= get_elf_backend_data (hash_table
->dynobj
);
2735 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2736 BFD_ASSERT (s
!= NULL
);
2738 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2739 newcontents
= bfd_realloc (s
->contents
, newsize
);
2740 if (newcontents
== NULL
)
2744 dyn
.d_un
.d_val
= val
;
2745 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2748 s
->contents
= newcontents
;
2753 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2754 otherwise just check whether one already exists. Returns -1 on error,
2755 1 if a DT_NEEDED tag already exists, and 0 on success. */
2758 elf_add_dt_needed_tag (bfd
*abfd
,
2759 struct bfd_link_info
*info
,
2763 struct elf_link_hash_table
*hash_table
;
2764 bfd_size_type oldsize
;
2765 bfd_size_type strindex
;
2767 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2770 hash_table
= elf_hash_table (info
);
2771 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2772 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2773 if (strindex
== (bfd_size_type
) -1)
2776 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2779 const struct elf_backend_data
*bed
;
2782 bed
= get_elf_backend_data (hash_table
->dynobj
);
2783 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2785 for (extdyn
= sdyn
->contents
;
2786 extdyn
< sdyn
->contents
+ sdyn
->size
;
2787 extdyn
+= bed
->s
->sizeof_dyn
)
2789 Elf_Internal_Dyn dyn
;
2791 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2792 if (dyn
.d_tag
== DT_NEEDED
2793 && dyn
.d_un
.d_val
== strindex
)
2795 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2803 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2806 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2810 /* We were just checking for existence of the tag. */
2811 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2816 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2817 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2818 references to these symbols. */
2820 struct elf_smash_syms_data
2823 struct elf_link_hash_table
*htab
;
2824 bfd_boolean twiddled
;
2828 elf_smash_syms (struct elf_link_hash_entry
*h
, void *data
)
2830 struct elf_smash_syms_data
*inf
= (struct elf_smash_syms_data
*) data
;
2831 struct bfd_link_hash_entry
*bh
;
2833 switch (h
->root
.type
)
2836 case bfd_link_hash_new
:
2839 case bfd_link_hash_undefined
:
2840 case bfd_link_hash_undefweak
:
2841 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2845 case bfd_link_hash_defined
:
2846 case bfd_link_hash_defweak
:
2847 if (h
->root
.u
.def
.section
->owner
!= inf
->not_needed
)
2851 case bfd_link_hash_common
:
2852 if (h
->root
.u
.c
.p
->section
->owner
!= inf
->not_needed
)
2856 case bfd_link_hash_warning
:
2857 case bfd_link_hash_indirect
:
2858 elf_smash_syms ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
);
2859 if (h
->root
.u
.i
.link
->type
!= bfd_link_hash_new
)
2861 if (h
->root
.u
.i
.link
->u
.undef
.abfd
!= inf
->not_needed
)
2866 /* Set sym back to newly created state, but keep undefs list pointer. */
2867 bh
= h
->root
.u
.undef
.next
;
2868 if (bh
!= NULL
|| inf
->htab
->root
.undefs_tail
== &h
->root
)
2869 inf
->twiddled
= TRUE
;
2870 (*inf
->htab
->root
.table
.newfunc
) (&h
->root
.root
,
2871 &inf
->htab
->root
.table
,
2872 h
->root
.root
.string
);
2873 h
->root
.u
.undef
.next
= bh
;
2874 h
->root
.u
.undef
.abfd
= inf
->not_needed
;
2879 /* Sort symbol by value and section. */
2881 elf_sort_symbol (const void *arg1
, const void *arg2
)
2883 const struct elf_link_hash_entry
*h1
;
2884 const struct elf_link_hash_entry
*h2
;
2885 bfd_signed_vma vdiff
;
2887 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2888 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2889 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2891 return vdiff
> 0 ? 1 : -1;
2894 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2896 return sdiff
> 0 ? 1 : -1;
2901 /* This function is used to adjust offsets into .dynstr for
2902 dynamic symbols. This is called via elf_link_hash_traverse. */
2905 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2907 struct elf_strtab_hash
*dynstr
= data
;
2909 if (h
->root
.type
== bfd_link_hash_warning
)
2910 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2912 if (h
->dynindx
!= -1)
2913 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2917 /* Assign string offsets in .dynstr, update all structures referencing
2921 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2923 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2924 struct elf_link_local_dynamic_entry
*entry
;
2925 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2926 bfd
*dynobj
= hash_table
->dynobj
;
2929 const struct elf_backend_data
*bed
;
2932 _bfd_elf_strtab_finalize (dynstr
);
2933 size
= _bfd_elf_strtab_size (dynstr
);
2935 bed
= get_elf_backend_data (dynobj
);
2936 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2937 BFD_ASSERT (sdyn
!= NULL
);
2939 /* Update all .dynamic entries referencing .dynstr strings. */
2940 for (extdyn
= sdyn
->contents
;
2941 extdyn
< sdyn
->contents
+ sdyn
->size
;
2942 extdyn
+= bed
->s
->sizeof_dyn
)
2944 Elf_Internal_Dyn dyn
;
2946 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2950 dyn
.d_un
.d_val
= size
;
2958 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2963 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2966 /* Now update local dynamic symbols. */
2967 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2968 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2969 entry
->isym
.st_name
);
2971 /* And the rest of dynamic symbols. */
2972 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2974 /* Adjust version definitions. */
2975 if (elf_tdata (output_bfd
)->cverdefs
)
2980 Elf_Internal_Verdef def
;
2981 Elf_Internal_Verdaux defaux
;
2983 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2987 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2989 p
+= sizeof (Elf_External_Verdef
);
2990 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
2992 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2994 _bfd_elf_swap_verdaux_in (output_bfd
,
2995 (Elf_External_Verdaux
*) p
, &defaux
);
2996 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2998 _bfd_elf_swap_verdaux_out (output_bfd
,
2999 &defaux
, (Elf_External_Verdaux
*) p
);
3000 p
+= sizeof (Elf_External_Verdaux
);
3003 while (def
.vd_next
);
3006 /* Adjust version references. */
3007 if (elf_tdata (output_bfd
)->verref
)
3012 Elf_Internal_Verneed need
;
3013 Elf_Internal_Vernaux needaux
;
3015 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3019 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3021 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3022 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3023 (Elf_External_Verneed
*) p
);
3024 p
+= sizeof (Elf_External_Verneed
);
3025 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3027 _bfd_elf_swap_vernaux_in (output_bfd
,
3028 (Elf_External_Vernaux
*) p
, &needaux
);
3029 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3031 _bfd_elf_swap_vernaux_out (output_bfd
,
3033 (Elf_External_Vernaux
*) p
);
3034 p
+= sizeof (Elf_External_Vernaux
);
3037 while (need
.vn_next
);
3043 /* Add symbols from an ELF object file to the linker hash table. */
3046 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3048 bfd_boolean (*add_symbol_hook
)
3049 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
3050 const char **, flagword
*, asection
**, bfd_vma
*);
3051 bfd_boolean (*check_relocs
)
3052 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
3053 bfd_boolean (*check_directives
)
3054 (bfd
*, struct bfd_link_info
*);
3055 bfd_boolean collect
;
3056 Elf_Internal_Shdr
*hdr
;
3057 bfd_size_type symcount
;
3058 bfd_size_type extsymcount
;
3059 bfd_size_type extsymoff
;
3060 struct elf_link_hash_entry
**sym_hash
;
3061 bfd_boolean dynamic
;
3062 Elf_External_Versym
*extversym
= NULL
;
3063 Elf_External_Versym
*ever
;
3064 struct elf_link_hash_entry
*weaks
;
3065 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3066 bfd_size_type nondeflt_vers_cnt
= 0;
3067 Elf_Internal_Sym
*isymbuf
= NULL
;
3068 Elf_Internal_Sym
*isym
;
3069 Elf_Internal_Sym
*isymend
;
3070 const struct elf_backend_data
*bed
;
3071 bfd_boolean add_needed
;
3072 struct elf_link_hash_table
* hash_table
;
3075 hash_table
= elf_hash_table (info
);
3077 bed
= get_elf_backend_data (abfd
);
3078 add_symbol_hook
= bed
->elf_add_symbol_hook
;
3079 collect
= bed
->collect
;
3081 if ((abfd
->flags
& DYNAMIC
) == 0)
3087 /* You can't use -r against a dynamic object. Also, there's no
3088 hope of using a dynamic object which does not exactly match
3089 the format of the output file. */
3090 if (info
->relocatable
3091 || !is_elf_hash_table (hash_table
)
3092 || hash_table
->root
.creator
!= abfd
->xvec
)
3094 if (info
->relocatable
)
3095 bfd_set_error (bfd_error_invalid_operation
);
3097 bfd_set_error (bfd_error_wrong_format
);
3102 /* As a GNU extension, any input sections which are named
3103 .gnu.warning.SYMBOL are treated as warning symbols for the given
3104 symbol. This differs from .gnu.warning sections, which generate
3105 warnings when they are included in an output file. */
3106 if (info
->executable
)
3110 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3114 name
= bfd_get_section_name (abfd
, s
);
3115 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3119 bfd_size_type prefix_len
;
3120 const char * gnu_warning_prefix
= _("warning: ");
3122 name
+= sizeof ".gnu.warning." - 1;
3124 /* If this is a shared object, then look up the symbol
3125 in the hash table. If it is there, and it is already
3126 been defined, then we will not be using the entry
3127 from this shared object, so we don't need to warn.
3128 FIXME: If we see the definition in a regular object
3129 later on, we will warn, but we shouldn't. The only
3130 fix is to keep track of what warnings we are supposed
3131 to emit, and then handle them all at the end of the
3135 struct elf_link_hash_entry
*h
;
3137 h
= elf_link_hash_lookup (hash_table
, name
,
3138 FALSE
, FALSE
, TRUE
);
3140 /* FIXME: What about bfd_link_hash_common? */
3142 && (h
->root
.type
== bfd_link_hash_defined
3143 || h
->root
.type
== bfd_link_hash_defweak
))
3145 /* We don't want to issue this warning. Clobber
3146 the section size so that the warning does not
3147 get copied into the output file. */
3154 prefix_len
= strlen (gnu_warning_prefix
);
3155 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3159 strcpy (msg
, gnu_warning_prefix
);
3160 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3163 msg
[prefix_len
+ sz
] = '\0';
3165 if (! (_bfd_generic_link_add_one_symbol
3166 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3167 FALSE
, collect
, NULL
)))
3170 if (! info
->relocatable
)
3172 /* Clobber the section size so that the warning does
3173 not get copied into the output file. */
3183 /* If we are creating a shared library, create all the dynamic
3184 sections immediately. We need to attach them to something,
3185 so we attach them to this BFD, provided it is the right
3186 format. FIXME: If there are no input BFD's of the same
3187 format as the output, we can't make a shared library. */
3189 && is_elf_hash_table (hash_table
)
3190 && hash_table
->root
.creator
== abfd
->xvec
3191 && ! hash_table
->dynamic_sections_created
)
3193 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3197 else if (!is_elf_hash_table (hash_table
))
3202 const char *soname
= NULL
;
3203 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3206 /* ld --just-symbols and dynamic objects don't mix very well.
3207 Test for --just-symbols by looking at info set up by
3208 _bfd_elf_link_just_syms. */
3209 if ((s
= abfd
->sections
) != NULL
3210 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3213 /* If this dynamic lib was specified on the command line with
3214 --as-needed in effect, then we don't want to add a DT_NEEDED
3215 tag unless the lib is actually used. Similary for libs brought
3216 in by another lib's DT_NEEDED. When --no-add-needed is used
3217 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3218 any dynamic library in DT_NEEDED tags in the dynamic lib at
3220 add_needed
= (elf_dyn_lib_class (abfd
)
3221 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3222 | DYN_NO_NEEDED
)) == 0;
3224 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3230 unsigned long shlink
;
3232 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3233 goto error_free_dyn
;
3235 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3237 goto error_free_dyn
;
3238 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3240 for (extdyn
= dynbuf
;
3241 extdyn
< dynbuf
+ s
->size
;
3242 extdyn
+= bed
->s
->sizeof_dyn
)
3244 Elf_Internal_Dyn dyn
;
3246 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3247 if (dyn
.d_tag
== DT_SONAME
)
3249 unsigned int tagv
= dyn
.d_un
.d_val
;
3250 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3252 goto error_free_dyn
;
3254 if (dyn
.d_tag
== DT_NEEDED
)
3256 struct bfd_link_needed_list
*n
, **pn
;
3258 unsigned int tagv
= dyn
.d_un
.d_val
;
3260 amt
= sizeof (struct bfd_link_needed_list
);
3261 n
= bfd_alloc (abfd
, amt
);
3262 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3263 if (n
== NULL
|| fnm
== NULL
)
3264 goto error_free_dyn
;
3265 amt
= strlen (fnm
) + 1;
3266 anm
= bfd_alloc (abfd
, amt
);
3268 goto error_free_dyn
;
3269 memcpy (anm
, fnm
, amt
);
3273 for (pn
= & hash_table
->needed
;
3279 if (dyn
.d_tag
== DT_RUNPATH
)
3281 struct bfd_link_needed_list
*n
, **pn
;
3283 unsigned int tagv
= dyn
.d_un
.d_val
;
3285 amt
= sizeof (struct bfd_link_needed_list
);
3286 n
= bfd_alloc (abfd
, amt
);
3287 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3288 if (n
== NULL
|| fnm
== NULL
)
3289 goto error_free_dyn
;
3290 amt
= strlen (fnm
) + 1;
3291 anm
= bfd_alloc (abfd
, amt
);
3293 goto error_free_dyn
;
3294 memcpy (anm
, fnm
, amt
);
3298 for (pn
= & runpath
;
3304 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3305 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3307 struct bfd_link_needed_list
*n
, **pn
;
3309 unsigned int tagv
= dyn
.d_un
.d_val
;
3311 amt
= sizeof (struct bfd_link_needed_list
);
3312 n
= bfd_alloc (abfd
, amt
);
3313 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3314 if (n
== NULL
|| fnm
== NULL
)
3315 goto error_free_dyn
;
3316 amt
= strlen (fnm
) + 1;
3317 anm
= bfd_alloc (abfd
, amt
);
3324 memcpy (anm
, fnm
, amt
);
3339 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3340 frees all more recently bfd_alloc'd blocks as well. */
3346 struct bfd_link_needed_list
**pn
;
3347 for (pn
= & hash_table
->runpath
;
3354 /* We do not want to include any of the sections in a dynamic
3355 object in the output file. We hack by simply clobbering the
3356 list of sections in the BFD. This could be handled more
3357 cleanly by, say, a new section flag; the existing
3358 SEC_NEVER_LOAD flag is not the one we want, because that one
3359 still implies that the section takes up space in the output
3361 bfd_section_list_clear (abfd
);
3363 /* Find the name to use in a DT_NEEDED entry that refers to this
3364 object. If the object has a DT_SONAME entry, we use it.
3365 Otherwise, if the generic linker stuck something in
3366 elf_dt_name, we use that. Otherwise, we just use the file
3368 if (soname
== NULL
|| *soname
== '\0')
3370 soname
= elf_dt_name (abfd
);
3371 if (soname
== NULL
|| *soname
== '\0')
3372 soname
= bfd_get_filename (abfd
);
3375 /* Save the SONAME because sometimes the linker emulation code
3376 will need to know it. */
3377 elf_dt_name (abfd
) = soname
;
3379 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3383 /* If we have already included this dynamic object in the
3384 link, just ignore it. There is no reason to include a
3385 particular dynamic object more than once. */
3390 /* If this is a dynamic object, we always link against the .dynsym
3391 symbol table, not the .symtab symbol table. The dynamic linker
3392 will only see the .dynsym symbol table, so there is no reason to
3393 look at .symtab for a dynamic object. */
3395 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3396 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3398 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3400 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3402 /* The sh_info field of the symtab header tells us where the
3403 external symbols start. We don't care about the local symbols at
3405 if (elf_bad_symtab (abfd
))
3407 extsymcount
= symcount
;
3412 extsymcount
= symcount
- hdr
->sh_info
;
3413 extsymoff
= hdr
->sh_info
;
3417 if (extsymcount
!= 0)
3419 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3421 if (isymbuf
== NULL
)
3424 /* We store a pointer to the hash table entry for each external
3426 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3427 sym_hash
= bfd_alloc (abfd
, amt
);
3428 if (sym_hash
== NULL
)
3429 goto error_free_sym
;
3430 elf_sym_hashes (abfd
) = sym_hash
;
3435 /* Read in any version definitions. */
3436 if (!_bfd_elf_slurp_version_tables (abfd
,
3437 info
->default_imported_symver
))
3438 goto error_free_sym
;
3440 /* Read in the symbol versions, but don't bother to convert them
3441 to internal format. */
3442 if (elf_dynversym (abfd
) != 0)
3444 Elf_Internal_Shdr
*versymhdr
;
3446 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3447 extversym
= bfd_malloc (versymhdr
->sh_size
);
3448 if (extversym
== NULL
)
3449 goto error_free_sym
;
3450 amt
= versymhdr
->sh_size
;
3451 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3452 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3453 goto error_free_vers
;
3459 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3460 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3462 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3469 struct elf_link_hash_entry
*h
;
3470 bfd_boolean definition
;
3471 bfd_boolean size_change_ok
;
3472 bfd_boolean type_change_ok
;
3473 bfd_boolean new_weakdef
;
3474 bfd_boolean override
;
3475 unsigned int old_alignment
;
3480 flags
= BSF_NO_FLAGS
;
3482 value
= isym
->st_value
;
3485 bind
= ELF_ST_BIND (isym
->st_info
);
3486 if (bind
== STB_LOCAL
)
3488 /* This should be impossible, since ELF requires that all
3489 global symbols follow all local symbols, and that sh_info
3490 point to the first global symbol. Unfortunately, Irix 5
3494 else if (bind
== STB_GLOBAL
)
3496 if (isym
->st_shndx
!= SHN_UNDEF
3497 && isym
->st_shndx
!= SHN_COMMON
)
3500 else if (bind
== STB_WEAK
)
3504 /* Leave it up to the processor backend. */
3507 if (isym
->st_shndx
== SHN_UNDEF
)
3508 sec
= bfd_und_section_ptr
;
3509 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3511 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3513 sec
= bfd_abs_section_ptr
;
3514 else if (sec
->kept_section
)
3516 /* Symbols from discarded section are undefined. */
3517 sec
= bfd_und_section_ptr
;
3518 isym
->st_shndx
= SHN_UNDEF
;
3520 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3523 else if (isym
->st_shndx
== SHN_ABS
)
3524 sec
= bfd_abs_section_ptr
;
3525 else if (isym
->st_shndx
== SHN_COMMON
)
3527 sec
= bfd_com_section_ptr
;
3528 /* What ELF calls the size we call the value. What ELF
3529 calls the value we call the alignment. */
3530 value
= isym
->st_size
;
3534 /* Leave it up to the processor backend. */
3537 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3540 goto error_free_vers
;
3542 if (isym
->st_shndx
== SHN_COMMON
3543 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3545 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3549 tcomm
= bfd_make_section (abfd
, ".tcommon");
3551 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3553 | SEC_LINKER_CREATED
3554 | SEC_THREAD_LOCAL
)))
3555 goto error_free_vers
;
3559 else if (add_symbol_hook
)
3561 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3563 goto error_free_vers
;
3565 /* The hook function sets the name to NULL if this symbol
3566 should be skipped for some reason. */
3571 /* Sanity check that all possibilities were handled. */
3574 bfd_set_error (bfd_error_bad_value
);
3575 goto error_free_vers
;
3578 if (bfd_is_und_section (sec
)
3579 || bfd_is_com_section (sec
))
3584 size_change_ok
= FALSE
;
3585 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3589 if (is_elf_hash_table (hash_table
))
3591 Elf_Internal_Versym iver
;
3592 unsigned int vernum
= 0;
3597 if (info
->default_imported_symver
)
3598 /* Use the default symbol version created earlier. */
3599 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3604 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3606 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3608 /* If this is a hidden symbol, or if it is not version
3609 1, we append the version name to the symbol name.
3610 However, we do not modify a non-hidden absolute
3611 symbol, because it might be the version symbol
3612 itself. FIXME: What if it isn't? */
3613 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3614 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3617 size_t namelen
, verlen
, newlen
;
3620 if (isym
->st_shndx
!= SHN_UNDEF
)
3622 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3624 else if (vernum
> 1)
3626 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3632 (*_bfd_error_handler
)
3633 (_("%B: %s: invalid version %u (max %d)"),
3635 elf_tdata (abfd
)->cverdefs
);
3636 bfd_set_error (bfd_error_bad_value
);
3637 goto error_free_vers
;
3642 /* We cannot simply test for the number of
3643 entries in the VERNEED section since the
3644 numbers for the needed versions do not start
3646 Elf_Internal_Verneed
*t
;
3649 for (t
= elf_tdata (abfd
)->verref
;
3653 Elf_Internal_Vernaux
*a
;
3655 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3657 if (a
->vna_other
== vernum
)
3659 verstr
= a
->vna_nodename
;
3668 (*_bfd_error_handler
)
3669 (_("%B: %s: invalid needed version %d"),
3670 abfd
, name
, vernum
);
3671 bfd_set_error (bfd_error_bad_value
);
3672 goto error_free_vers
;
3676 namelen
= strlen (name
);
3677 verlen
= strlen (verstr
);
3678 newlen
= namelen
+ verlen
+ 2;
3679 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3680 && isym
->st_shndx
!= SHN_UNDEF
)
3683 newname
= bfd_alloc (abfd
, newlen
);
3684 if (newname
== NULL
)
3685 goto error_free_vers
;
3686 memcpy (newname
, name
, namelen
);
3687 p
= newname
+ namelen
;
3689 /* If this is a defined non-hidden version symbol,
3690 we add another @ to the name. This indicates the
3691 default version of the symbol. */
3692 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3693 && isym
->st_shndx
!= SHN_UNDEF
)
3695 memcpy (p
, verstr
, verlen
+ 1);
3700 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3701 sym_hash
, &skip
, &override
,
3702 &type_change_ok
, &size_change_ok
))
3703 goto error_free_vers
;
3712 while (h
->root
.type
== bfd_link_hash_indirect
3713 || h
->root
.type
== bfd_link_hash_warning
)
3714 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3716 /* Remember the old alignment if this is a common symbol, so
3717 that we don't reduce the alignment later on. We can't
3718 check later, because _bfd_generic_link_add_one_symbol
3719 will set a default for the alignment which we want to
3720 override. We also remember the old bfd where the existing
3721 definition comes from. */
3722 switch (h
->root
.type
)
3727 case bfd_link_hash_defined
:
3728 case bfd_link_hash_defweak
:
3729 old_bfd
= h
->root
.u
.def
.section
->owner
;
3732 case bfd_link_hash_common
:
3733 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3734 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3738 if (elf_tdata (abfd
)->verdef
!= NULL
3742 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3745 if (! (_bfd_generic_link_add_one_symbol
3746 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3747 (struct bfd_link_hash_entry
**) sym_hash
)))
3748 goto error_free_vers
;
3751 while (h
->root
.type
== bfd_link_hash_indirect
3752 || h
->root
.type
== bfd_link_hash_warning
)
3753 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3756 new_weakdef
= FALSE
;
3759 && (flags
& BSF_WEAK
) != 0
3760 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3761 && is_elf_hash_table (hash_table
)
3762 && h
->u
.weakdef
== NULL
)
3764 /* Keep a list of all weak defined non function symbols from
3765 a dynamic object, using the weakdef field. Later in this
3766 function we will set the weakdef field to the correct
3767 value. We only put non-function symbols from dynamic
3768 objects on this list, because that happens to be the only
3769 time we need to know the normal symbol corresponding to a
3770 weak symbol, and the information is time consuming to
3771 figure out. If the weakdef field is not already NULL,
3772 then this symbol was already defined by some previous
3773 dynamic object, and we will be using that previous
3774 definition anyhow. */
3776 h
->u
.weakdef
= weaks
;
3781 /* Set the alignment of a common symbol. */
3782 if (isym
->st_shndx
== SHN_COMMON
3783 && h
->root
.type
== bfd_link_hash_common
)
3787 align
= bfd_log2 (isym
->st_value
);
3788 if (align
> old_alignment
3789 /* Permit an alignment power of zero if an alignment of one
3790 is specified and no other alignments have been specified. */
3791 || (isym
->st_value
== 1 && old_alignment
== 0))
3792 h
->root
.u
.c
.p
->alignment_power
= align
;
3794 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3797 if (is_elf_hash_table (hash_table
))
3801 /* Check the alignment when a common symbol is involved. This
3802 can change when a common symbol is overridden by a normal
3803 definition or a common symbol is ignored due to the old
3804 normal definition. We need to make sure the maximum
3805 alignment is maintained. */
3806 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3807 && h
->root
.type
!= bfd_link_hash_common
)
3809 unsigned int common_align
;
3810 unsigned int normal_align
;
3811 unsigned int symbol_align
;
3815 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3816 if (h
->root
.u
.def
.section
->owner
!= NULL
3817 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3819 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3820 if (normal_align
> symbol_align
)
3821 normal_align
= symbol_align
;
3824 normal_align
= symbol_align
;
3828 common_align
= old_alignment
;
3829 common_bfd
= old_bfd
;
3834 common_align
= bfd_log2 (isym
->st_value
);
3836 normal_bfd
= old_bfd
;
3839 if (normal_align
< common_align
)
3840 (*_bfd_error_handler
)
3841 (_("Warning: alignment %u of symbol `%s' in %B"
3842 " is smaller than %u in %B"),
3843 normal_bfd
, common_bfd
,
3844 1 << normal_align
, name
, 1 << common_align
);
3847 /* Remember the symbol size and type. */
3848 if (isym
->st_size
!= 0
3849 && (definition
|| h
->size
== 0))
3851 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3852 (*_bfd_error_handler
)
3853 (_("Warning: size of symbol `%s' changed"
3854 " from %lu in %B to %lu in %B"),
3856 name
, (unsigned long) h
->size
,
3857 (unsigned long) isym
->st_size
);
3859 h
->size
= isym
->st_size
;
3862 /* If this is a common symbol, then we always want H->SIZE
3863 to be the size of the common symbol. The code just above
3864 won't fix the size if a common symbol becomes larger. We
3865 don't warn about a size change here, because that is
3866 covered by --warn-common. */
3867 if (h
->root
.type
== bfd_link_hash_common
)
3868 h
->size
= h
->root
.u
.c
.size
;
3870 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3871 && (definition
|| h
->type
== STT_NOTYPE
))
3873 if (h
->type
!= STT_NOTYPE
3874 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3875 && ! type_change_ok
)
3876 (*_bfd_error_handler
)
3877 (_("Warning: type of symbol `%s' changed"
3878 " from %d to %d in %B"),
3879 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3881 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3884 /* If st_other has a processor-specific meaning, specific
3885 code might be needed here. We never merge the visibility
3886 attribute with the one from a dynamic object. */
3887 if (bed
->elf_backend_merge_symbol_attribute
)
3888 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3891 /* If this symbol has default visibility and the user has requested
3892 we not re-export it, then mark it as hidden. */
3893 if (definition
&& !dynamic
3895 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3896 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3897 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3899 if (isym
->st_other
!= 0 && !dynamic
)
3901 unsigned char hvis
, symvis
, other
, nvis
;
3903 /* Take the balance of OTHER from the definition. */
3904 other
= (definition
? isym
->st_other
: h
->other
);
3905 other
&= ~ ELF_ST_VISIBILITY (-1);
3907 /* Combine visibilities, using the most constraining one. */
3908 hvis
= ELF_ST_VISIBILITY (h
->other
);
3909 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3915 nvis
= hvis
< symvis
? hvis
: symvis
;
3917 h
->other
= other
| nvis
;
3920 /* Set a flag in the hash table entry indicating the type of
3921 reference or definition we just found. Keep a count of
3922 the number of dynamic symbols we find. A dynamic symbol
3923 is one which is referenced or defined by both a regular
3924 object and a shared object. */
3931 if (bind
!= STB_WEAK
)
3932 h
->ref_regular_nonweak
= 1;
3936 if (! info
->executable
3949 || (h
->u
.weakdef
!= NULL
3951 && h
->u
.weakdef
->dynindx
!= -1))
3955 /* Check to see if we need to add an indirect symbol for
3956 the default name. */
3957 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3958 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3959 &sec
, &value
, &dynsym
,
3961 goto error_free_vers
;
3963 if (definition
&& !dynamic
)
3965 char *p
= strchr (name
, ELF_VER_CHR
);
3966 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3968 /* Queue non-default versions so that .symver x, x@FOO
3969 aliases can be checked. */
3970 if (! nondeflt_vers
)
3972 amt
= (isymend
- isym
+ 1)
3973 * sizeof (struct elf_link_hash_entry
*);
3974 nondeflt_vers
= bfd_malloc (amt
);
3976 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3980 if (dynsym
&& h
->dynindx
== -1)
3982 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3983 goto error_free_vers
;
3984 if (h
->u
.weakdef
!= NULL
3986 && h
->u
.weakdef
->dynindx
== -1)
3988 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3989 goto error_free_vers
;
3992 else if (dynsym
&& h
->dynindx
!= -1)
3993 /* If the symbol already has a dynamic index, but
3994 visibility says it should not be visible, turn it into
3996 switch (ELF_ST_VISIBILITY (h
->other
))
4000 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4011 const char *soname
= elf_dt_name (abfd
);
4013 /* A symbol from a library loaded via DT_NEEDED of some
4014 other library is referenced by a regular object.
4015 Add a DT_NEEDED entry for it. Issue an error if
4016 --no-add-needed is used. */
4017 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4019 (*_bfd_error_handler
)
4020 (_("%s: invalid DSO for symbol `%s' definition"),
4022 bfd_set_error (bfd_error_bad_value
);
4023 goto error_free_vers
;
4026 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4029 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4031 goto error_free_vers
;
4033 BFD_ASSERT (ret
== 0);
4038 /* Now that all the symbols from this input file are created, handle
4039 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4040 if (nondeflt_vers
!= NULL
)
4042 bfd_size_type cnt
, symidx
;
4044 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4046 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4047 char *shortname
, *p
;
4049 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4051 || (h
->root
.type
!= bfd_link_hash_defined
4052 && h
->root
.type
!= bfd_link_hash_defweak
))
4055 amt
= p
- h
->root
.root
.string
;
4056 shortname
= bfd_malloc (amt
+ 1);
4057 memcpy (shortname
, h
->root
.root
.string
, amt
);
4058 shortname
[amt
] = '\0';
4060 hi
= (struct elf_link_hash_entry
*)
4061 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
4062 FALSE
, FALSE
, FALSE
);
4064 && hi
->root
.type
== h
->root
.type
4065 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4066 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4068 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4069 hi
->root
.type
= bfd_link_hash_indirect
;
4070 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4071 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
4072 sym_hash
= elf_sym_hashes (abfd
);
4074 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4075 if (sym_hash
[symidx
] == hi
)
4077 sym_hash
[symidx
] = h
;
4083 free (nondeflt_vers
);
4084 nondeflt_vers
= NULL
;
4087 if (extversym
!= NULL
)
4093 if (isymbuf
!= NULL
)
4099 struct elf_smash_syms_data inf
;
4100 inf
.not_needed
= abfd
;
4101 inf
.htab
= hash_table
;
4102 inf
.twiddled
= FALSE
;
4103 elf_link_hash_traverse (hash_table
, elf_smash_syms
, &inf
);
4105 bfd_link_repair_undef_list (&hash_table
->root
);
4109 /* Now set the weakdefs field correctly for all the weak defined
4110 symbols we found. The only way to do this is to search all the
4111 symbols. Since we only need the information for non functions in
4112 dynamic objects, that's the only time we actually put anything on
4113 the list WEAKS. We need this information so that if a regular
4114 object refers to a symbol defined weakly in a dynamic object, the
4115 real symbol in the dynamic object is also put in the dynamic
4116 symbols; we also must arrange for both symbols to point to the
4117 same memory location. We could handle the general case of symbol
4118 aliasing, but a general symbol alias can only be generated in
4119 assembler code, handling it correctly would be very time
4120 consuming, and other ELF linkers don't handle general aliasing
4124 struct elf_link_hash_entry
**hpp
;
4125 struct elf_link_hash_entry
**hppend
;
4126 struct elf_link_hash_entry
**sorted_sym_hash
;
4127 struct elf_link_hash_entry
*h
;
4130 /* Since we have to search the whole symbol list for each weak
4131 defined symbol, search time for N weak defined symbols will be
4132 O(N^2). Binary search will cut it down to O(NlogN). */
4133 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4134 sorted_sym_hash
= bfd_malloc (amt
);
4135 if (sorted_sym_hash
== NULL
)
4137 sym_hash
= sorted_sym_hash
;
4138 hpp
= elf_sym_hashes (abfd
);
4139 hppend
= hpp
+ extsymcount
;
4141 for (; hpp
< hppend
; hpp
++)
4145 && h
->root
.type
== bfd_link_hash_defined
4146 && h
->type
!= STT_FUNC
)
4154 qsort (sorted_sym_hash
, sym_count
,
4155 sizeof (struct elf_link_hash_entry
*),
4158 while (weaks
!= NULL
)
4160 struct elf_link_hash_entry
*hlook
;
4167 weaks
= hlook
->u
.weakdef
;
4168 hlook
->u
.weakdef
= NULL
;
4170 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4171 || hlook
->root
.type
== bfd_link_hash_defweak
4172 || hlook
->root
.type
== bfd_link_hash_common
4173 || hlook
->root
.type
== bfd_link_hash_indirect
);
4174 slook
= hlook
->root
.u
.def
.section
;
4175 vlook
= hlook
->root
.u
.def
.value
;
4182 bfd_signed_vma vdiff
;
4184 h
= sorted_sym_hash
[idx
];
4185 vdiff
= vlook
- h
->root
.u
.def
.value
;
4192 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4205 /* We didn't find a value/section match. */
4209 for (i
= ilook
; i
< sym_count
; i
++)
4211 h
= sorted_sym_hash
[i
];
4213 /* Stop if value or section doesn't match. */
4214 if (h
->root
.u
.def
.value
!= vlook
4215 || h
->root
.u
.def
.section
!= slook
)
4217 else if (h
!= hlook
)
4219 hlook
->u
.weakdef
= h
;
4221 /* If the weak definition is in the list of dynamic
4222 symbols, make sure the real definition is put
4224 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4226 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4230 /* If the real definition is in the list of dynamic
4231 symbols, make sure the weak definition is put
4232 there as well. If we don't do this, then the
4233 dynamic loader might not merge the entries for the
4234 real definition and the weak definition. */
4235 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4237 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4245 free (sorted_sym_hash
);
4248 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4249 if (check_directives
)
4250 check_directives (abfd
, info
);
4252 /* If this object is the same format as the output object, and it is
4253 not a shared library, then let the backend look through the
4256 This is required to build global offset table entries and to
4257 arrange for dynamic relocs. It is not required for the
4258 particular common case of linking non PIC code, even when linking
4259 against shared libraries, but unfortunately there is no way of
4260 knowing whether an object file has been compiled PIC or not.
4261 Looking through the relocs is not particularly time consuming.
4262 The problem is that we must either (1) keep the relocs in memory,
4263 which causes the linker to require additional runtime memory or
4264 (2) read the relocs twice from the input file, which wastes time.
4265 This would be a good case for using mmap.
4267 I have no idea how to handle linking PIC code into a file of a
4268 different format. It probably can't be done. */
4269 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4271 && is_elf_hash_table (hash_table
)
4272 && hash_table
->root
.creator
== abfd
->xvec
4273 && check_relocs
!= NULL
)
4277 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4279 Elf_Internal_Rela
*internal_relocs
;
4282 if ((o
->flags
& SEC_RELOC
) == 0
4283 || o
->reloc_count
== 0
4284 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4285 && (o
->flags
& SEC_DEBUGGING
) != 0)
4286 || bfd_is_abs_section (o
->output_section
))
4289 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4291 if (internal_relocs
== NULL
)
4294 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4296 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4297 free (internal_relocs
);
4304 /* If this is a non-traditional link, try to optimize the handling
4305 of the .stab/.stabstr sections. */
4307 && ! info
->traditional_format
4308 && is_elf_hash_table (hash_table
)
4309 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4313 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4314 if (stabstr
!= NULL
)
4316 bfd_size_type string_offset
= 0;
4319 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4320 if (strncmp (".stab", stab
->name
, 5) == 0
4321 && (!stab
->name
[5] ||
4322 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4323 && (stab
->flags
& SEC_MERGE
) == 0
4324 && !bfd_is_abs_section (stab
->output_section
))
4326 struct bfd_elf_section_data
*secdata
;
4328 secdata
= elf_section_data (stab
);
4329 if (! _bfd_link_section_stabs (abfd
,
4330 &hash_table
->stab_info
,
4335 if (secdata
->sec_info
)
4336 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4341 if (is_elf_hash_table (hash_table
) && add_needed
)
4343 /* Add this bfd to the loaded list. */
4344 struct elf_link_loaded_list
*n
;
4346 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4350 n
->next
= hash_table
->loaded
;
4351 hash_table
->loaded
= n
;
4357 if (nondeflt_vers
!= NULL
)
4358 free (nondeflt_vers
);
4359 if (extversym
!= NULL
)
4362 if (isymbuf
!= NULL
)
4368 /* Return the linker hash table entry of a symbol that might be
4369 satisfied by an archive symbol. Return -1 on error. */
4371 struct elf_link_hash_entry
*
4372 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4373 struct bfd_link_info
*info
,
4376 struct elf_link_hash_entry
*h
;
4380 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4384 /* If this is a default version (the name contains @@), look up the
4385 symbol again with only one `@' as well as without the version.
4386 The effect is that references to the symbol with and without the
4387 version will be matched by the default symbol in the archive. */
4389 p
= strchr (name
, ELF_VER_CHR
);
4390 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4393 /* First check with only one `@'. */
4394 len
= strlen (name
);
4395 copy
= bfd_alloc (abfd
, len
);
4397 return (struct elf_link_hash_entry
*) 0 - 1;
4399 first
= p
- name
+ 1;
4400 memcpy (copy
, name
, first
);
4401 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4403 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4406 /* We also need to check references to the symbol without the
4408 copy
[first
- 1] = '\0';
4409 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4410 FALSE
, FALSE
, FALSE
);
4413 bfd_release (abfd
, copy
);
4417 /* Add symbols from an ELF archive file to the linker hash table. We
4418 don't use _bfd_generic_link_add_archive_symbols because of a
4419 problem which arises on UnixWare. The UnixWare libc.so is an
4420 archive which includes an entry libc.so.1 which defines a bunch of
4421 symbols. The libc.so archive also includes a number of other
4422 object files, which also define symbols, some of which are the same
4423 as those defined in libc.so.1. Correct linking requires that we
4424 consider each object file in turn, and include it if it defines any
4425 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4426 this; it looks through the list of undefined symbols, and includes
4427 any object file which defines them. When this algorithm is used on
4428 UnixWare, it winds up pulling in libc.so.1 early and defining a
4429 bunch of symbols. This means that some of the other objects in the
4430 archive are not included in the link, which is incorrect since they
4431 precede libc.so.1 in the archive.
4433 Fortunately, ELF archive handling is simpler than that done by
4434 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4435 oddities. In ELF, if we find a symbol in the archive map, and the
4436 symbol is currently undefined, we know that we must pull in that
4439 Unfortunately, we do have to make multiple passes over the symbol
4440 table until nothing further is resolved. */
4443 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4446 bfd_boolean
*defined
= NULL
;
4447 bfd_boolean
*included
= NULL
;
4451 const struct elf_backend_data
*bed
;
4452 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4453 (bfd
*, struct bfd_link_info
*, const char *);
4455 if (! bfd_has_map (abfd
))
4457 /* An empty archive is a special case. */
4458 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4460 bfd_set_error (bfd_error_no_armap
);
4464 /* Keep track of all symbols we know to be already defined, and all
4465 files we know to be already included. This is to speed up the
4466 second and subsequent passes. */
4467 c
= bfd_ardata (abfd
)->symdef_count
;
4471 amt
*= sizeof (bfd_boolean
);
4472 defined
= bfd_zmalloc (amt
);
4473 included
= bfd_zmalloc (amt
);
4474 if (defined
== NULL
|| included
== NULL
)
4477 symdefs
= bfd_ardata (abfd
)->symdefs
;
4478 bed
= get_elf_backend_data (abfd
);
4479 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4492 symdefend
= symdef
+ c
;
4493 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4495 struct elf_link_hash_entry
*h
;
4497 struct bfd_link_hash_entry
*undefs_tail
;
4500 if (defined
[i
] || included
[i
])
4502 if (symdef
->file_offset
== last
)
4508 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4509 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4515 if (h
->root
.type
== bfd_link_hash_common
)
4517 /* We currently have a common symbol. The archive map contains
4518 a reference to this symbol, so we may want to include it. We
4519 only want to include it however, if this archive element
4520 contains a definition of the symbol, not just another common
4523 Unfortunately some archivers (including GNU ar) will put
4524 declarations of common symbols into their archive maps, as
4525 well as real definitions, so we cannot just go by the archive
4526 map alone. Instead we must read in the element's symbol
4527 table and check that to see what kind of symbol definition
4529 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4532 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4534 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4539 /* We need to include this archive member. */
4540 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4541 if (element
== NULL
)
4544 if (! bfd_check_format (element
, bfd_object
))
4547 /* Doublecheck that we have not included this object
4548 already--it should be impossible, but there may be
4549 something wrong with the archive. */
4550 if (element
->archive_pass
!= 0)
4552 bfd_set_error (bfd_error_bad_value
);
4555 element
->archive_pass
= 1;
4557 undefs_tail
= info
->hash
->undefs_tail
;
4559 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4562 if (! bfd_link_add_symbols (element
, info
))
4565 /* If there are any new undefined symbols, we need to make
4566 another pass through the archive in order to see whether
4567 they can be defined. FIXME: This isn't perfect, because
4568 common symbols wind up on undefs_tail and because an
4569 undefined symbol which is defined later on in this pass
4570 does not require another pass. This isn't a bug, but it
4571 does make the code less efficient than it could be. */
4572 if (undefs_tail
!= info
->hash
->undefs_tail
)
4575 /* Look backward to mark all symbols from this object file
4576 which we have already seen in this pass. */
4580 included
[mark
] = TRUE
;
4585 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4587 /* We mark subsequent symbols from this object file as we go
4588 on through the loop. */
4589 last
= symdef
->file_offset
;
4600 if (defined
!= NULL
)
4602 if (included
!= NULL
)
4607 /* Given an ELF BFD, add symbols to the global hash table as
4611 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4613 switch (bfd_get_format (abfd
))
4616 return elf_link_add_object_symbols (abfd
, info
);
4618 return elf_link_add_archive_symbols (abfd
, info
);
4620 bfd_set_error (bfd_error_wrong_format
);
4625 /* This function will be called though elf_link_hash_traverse to store
4626 all hash value of the exported symbols in an array. */
4629 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4631 unsigned long **valuep
= data
;
4637 if (h
->root
.type
== bfd_link_hash_warning
)
4638 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4640 /* Ignore indirect symbols. These are added by the versioning code. */
4641 if (h
->dynindx
== -1)
4644 name
= h
->root
.root
.string
;
4645 p
= strchr (name
, ELF_VER_CHR
);
4648 alc
= bfd_malloc (p
- name
+ 1);
4649 memcpy (alc
, name
, p
- name
);
4650 alc
[p
- name
] = '\0';
4654 /* Compute the hash value. */
4655 ha
= bfd_elf_hash (name
);
4657 /* Store the found hash value in the array given as the argument. */
4660 /* And store it in the struct so that we can put it in the hash table
4662 h
->u
.elf_hash_value
= ha
;
4670 /* Array used to determine the number of hash table buckets to use
4671 based on the number of symbols there are. If there are fewer than
4672 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4673 fewer than 37 we use 17 buckets, and so forth. We never use more
4674 than 32771 buckets. */
4676 static const size_t elf_buckets
[] =
4678 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4682 /* Compute bucket count for hashing table. We do not use a static set
4683 of possible tables sizes anymore. Instead we determine for all
4684 possible reasonable sizes of the table the outcome (i.e., the
4685 number of collisions etc) and choose the best solution. The
4686 weighting functions are not too simple to allow the table to grow
4687 without bounds. Instead one of the weighting factors is the size.
4688 Therefore the result is always a good payoff between few collisions
4689 (= short chain lengths) and table size. */
4691 compute_bucket_count (struct bfd_link_info
*info
)
4693 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4694 size_t best_size
= 0;
4695 unsigned long int *hashcodes
;
4696 unsigned long int *hashcodesp
;
4697 unsigned long int i
;
4700 /* Compute the hash values for all exported symbols. At the same
4701 time store the values in an array so that we could use them for
4704 amt
*= sizeof (unsigned long int);
4705 hashcodes
= bfd_malloc (amt
);
4706 if (hashcodes
== NULL
)
4708 hashcodesp
= hashcodes
;
4710 /* Put all hash values in HASHCODES. */
4711 elf_link_hash_traverse (elf_hash_table (info
),
4712 elf_collect_hash_codes
, &hashcodesp
);
4714 /* We have a problem here. The following code to optimize the table
4715 size requires an integer type with more the 32 bits. If
4716 BFD_HOST_U_64_BIT is set we know about such a type. */
4717 #ifdef BFD_HOST_U_64_BIT
4720 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4723 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4724 unsigned long int *counts
;
4725 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4726 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4728 /* Possible optimization parameters: if we have NSYMS symbols we say
4729 that the hashing table must at least have NSYMS/4 and at most
4731 minsize
= nsyms
/ 4;
4734 best_size
= maxsize
= nsyms
* 2;
4736 /* Create array where we count the collisions in. We must use bfd_malloc
4737 since the size could be large. */
4739 amt
*= sizeof (unsigned long int);
4740 counts
= bfd_malloc (amt
);
4747 /* Compute the "optimal" size for the hash table. The criteria is a
4748 minimal chain length. The minor criteria is (of course) the size
4750 for (i
= minsize
; i
< maxsize
; ++i
)
4752 /* Walk through the array of hashcodes and count the collisions. */
4753 BFD_HOST_U_64_BIT max
;
4754 unsigned long int j
;
4755 unsigned long int fact
;
4757 memset (counts
, '\0', i
* sizeof (unsigned long int));
4759 /* Determine how often each hash bucket is used. */
4760 for (j
= 0; j
< nsyms
; ++j
)
4761 ++counts
[hashcodes
[j
] % i
];
4763 /* For the weight function we need some information about the
4764 pagesize on the target. This is information need not be 100%
4765 accurate. Since this information is not available (so far) we
4766 define it here to a reasonable default value. If it is crucial
4767 to have a better value some day simply define this value. */
4768 # ifndef BFD_TARGET_PAGESIZE
4769 # define BFD_TARGET_PAGESIZE (4096)
4772 /* We in any case need 2 + NSYMS entries for the size values and
4774 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4777 /* Variant 1: optimize for short chains. We add the squares
4778 of all the chain lengths (which favors many small chain
4779 over a few long chains). */
4780 for (j
= 0; j
< i
; ++j
)
4781 max
+= counts
[j
] * counts
[j
];
4783 /* This adds penalties for the overall size of the table. */
4784 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4787 /* Variant 2: Optimize a lot more for small table. Here we
4788 also add squares of the size but we also add penalties for
4789 empty slots (the +1 term). */
4790 for (j
= 0; j
< i
; ++j
)
4791 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4793 /* The overall size of the table is considered, but not as
4794 strong as in variant 1, where it is squared. */
4795 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4799 /* Compare with current best results. */
4800 if (max
< best_chlen
)
4810 #endif /* defined (BFD_HOST_U_64_BIT) */
4812 /* This is the fallback solution if no 64bit type is available or if we
4813 are not supposed to spend much time on optimizations. We select the
4814 bucket count using a fixed set of numbers. */
4815 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4817 best_size
= elf_buckets
[i
];
4818 if (dynsymcount
< elf_buckets
[i
+ 1])
4823 /* Free the arrays we needed. */
4829 /* Set up the sizes and contents of the ELF dynamic sections. This is
4830 called by the ELF linker emulation before_allocation routine. We
4831 must set the sizes of the sections before the linker sets the
4832 addresses of the various sections. */
4835 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4838 const char *filter_shlib
,
4839 const char * const *auxiliary_filters
,
4840 struct bfd_link_info
*info
,
4841 asection
**sinterpptr
,
4842 struct bfd_elf_version_tree
*verdefs
)
4844 bfd_size_type soname_indx
;
4846 const struct elf_backend_data
*bed
;
4847 struct elf_assign_sym_version_info asvinfo
;
4851 soname_indx
= (bfd_size_type
) -1;
4853 if (!is_elf_hash_table (info
->hash
))
4856 elf_tdata (output_bfd
)->relro
= info
->relro
;
4857 if (info
->execstack
)
4858 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4859 else if (info
->noexecstack
)
4860 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4864 asection
*notesec
= NULL
;
4867 for (inputobj
= info
->input_bfds
;
4869 inputobj
= inputobj
->link_next
)
4873 if (inputobj
->flags
& DYNAMIC
)
4875 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4878 if (s
->flags
& SEC_CODE
)
4887 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4888 if (exec
&& info
->relocatable
4889 && notesec
->output_section
!= bfd_abs_section_ptr
)
4890 notesec
->output_section
->flags
|= SEC_CODE
;
4894 /* Any syms created from now on start with -1 in
4895 got.refcount/offset and plt.refcount/offset. */
4896 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4898 /* The backend may have to create some sections regardless of whether
4899 we're dynamic or not. */
4900 bed
= get_elf_backend_data (output_bfd
);
4901 if (bed
->elf_backend_always_size_sections
4902 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4905 dynobj
= elf_hash_table (info
)->dynobj
;
4907 /* If there were no dynamic objects in the link, there is nothing to
4912 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4915 if (elf_hash_table (info
)->dynamic_sections_created
)
4917 struct elf_info_failed eif
;
4918 struct elf_link_hash_entry
*h
;
4920 struct bfd_elf_version_tree
*t
;
4921 struct bfd_elf_version_expr
*d
;
4922 bfd_boolean all_defined
;
4924 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4925 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4929 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4931 if (soname_indx
== (bfd_size_type
) -1
4932 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4938 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4940 info
->flags
|= DF_SYMBOLIC
;
4947 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4949 if (indx
== (bfd_size_type
) -1
4950 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4953 if (info
->new_dtags
)
4955 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4956 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4961 if (filter_shlib
!= NULL
)
4965 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4966 filter_shlib
, TRUE
);
4967 if (indx
== (bfd_size_type
) -1
4968 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4972 if (auxiliary_filters
!= NULL
)
4974 const char * const *p
;
4976 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4980 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4982 if (indx
== (bfd_size_type
) -1
4983 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4989 eif
.verdefs
= verdefs
;
4992 /* If we are supposed to export all symbols into the dynamic symbol
4993 table (this is not the normal case), then do so. */
4994 if (info
->export_dynamic
)
4996 elf_link_hash_traverse (elf_hash_table (info
),
4997 _bfd_elf_export_symbol
,
5003 /* Make all global versions with definition. */
5004 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5005 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5006 if (!d
->symver
&& d
->symbol
)
5008 const char *verstr
, *name
;
5009 size_t namelen
, verlen
, newlen
;
5011 struct elf_link_hash_entry
*newh
;
5014 namelen
= strlen (name
);
5016 verlen
= strlen (verstr
);
5017 newlen
= namelen
+ verlen
+ 3;
5019 newname
= bfd_malloc (newlen
);
5020 if (newname
== NULL
)
5022 memcpy (newname
, name
, namelen
);
5024 /* Check the hidden versioned definition. */
5025 p
= newname
+ namelen
;
5027 memcpy (p
, verstr
, verlen
+ 1);
5028 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5029 newname
, FALSE
, FALSE
,
5032 || (newh
->root
.type
!= bfd_link_hash_defined
5033 && newh
->root
.type
!= bfd_link_hash_defweak
))
5035 /* Check the default versioned definition. */
5037 memcpy (p
, verstr
, verlen
+ 1);
5038 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5039 newname
, FALSE
, FALSE
,
5044 /* Mark this version if there is a definition and it is
5045 not defined in a shared object. */
5047 && !newh
->def_dynamic
5048 && (newh
->root
.type
== bfd_link_hash_defined
5049 || newh
->root
.type
== bfd_link_hash_defweak
))
5053 /* Attach all the symbols to their version information. */
5054 asvinfo
.output_bfd
= output_bfd
;
5055 asvinfo
.info
= info
;
5056 asvinfo
.verdefs
= verdefs
;
5057 asvinfo
.failed
= FALSE
;
5059 elf_link_hash_traverse (elf_hash_table (info
),
5060 _bfd_elf_link_assign_sym_version
,
5065 if (!info
->allow_undefined_version
)
5067 /* Check if all global versions have a definition. */
5069 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5070 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5071 if (!d
->symver
&& !d
->script
)
5073 (*_bfd_error_handler
)
5074 (_("%s: undefined version: %s"),
5075 d
->pattern
, t
->name
);
5076 all_defined
= FALSE
;
5081 bfd_set_error (bfd_error_bad_value
);
5086 /* Find all symbols which were defined in a dynamic object and make
5087 the backend pick a reasonable value for them. */
5088 elf_link_hash_traverse (elf_hash_table (info
),
5089 _bfd_elf_adjust_dynamic_symbol
,
5094 /* Add some entries to the .dynamic section. We fill in some of the
5095 values later, in bfd_elf_final_link, but we must add the entries
5096 now so that we know the final size of the .dynamic section. */
5098 /* If there are initialization and/or finalization functions to
5099 call then add the corresponding DT_INIT/DT_FINI entries. */
5100 h
= (info
->init_function
5101 ? elf_link_hash_lookup (elf_hash_table (info
),
5102 info
->init_function
, FALSE
,
5109 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5112 h
= (info
->fini_function
5113 ? elf_link_hash_lookup (elf_hash_table (info
),
5114 info
->fini_function
, FALSE
,
5121 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5125 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5127 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5128 if (! info
->executable
)
5133 for (sub
= info
->input_bfds
; sub
!= NULL
;
5134 sub
= sub
->link_next
)
5135 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5136 if (elf_section_data (o
)->this_hdr
.sh_type
5137 == SHT_PREINIT_ARRAY
)
5139 (*_bfd_error_handler
)
5140 (_("%B: .preinit_array section is not allowed in DSO"),
5145 bfd_set_error (bfd_error_nonrepresentable_section
);
5149 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5150 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5153 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5155 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5156 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5159 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5161 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5162 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5166 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5167 /* If .dynstr is excluded from the link, we don't want any of
5168 these tags. Strictly, we should be checking each section
5169 individually; This quick check covers for the case where
5170 someone does a /DISCARD/ : { *(*) }. */
5171 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5173 bfd_size_type strsize
;
5175 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5176 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5177 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5178 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5179 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5180 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5181 bed
->s
->sizeof_sym
))
5186 /* The backend must work out the sizes of all the other dynamic
5188 if (bed
->elf_backend_size_dynamic_sections
5189 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5192 if (elf_hash_table (info
)->dynamic_sections_created
)
5194 bfd_size_type dynsymcount
;
5196 size_t bucketcount
= 0;
5197 size_t hash_entry_size
;
5198 unsigned int dtagcount
;
5200 /* Set up the version definition section. */
5201 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5202 BFD_ASSERT (s
!= NULL
);
5204 /* We may have created additional version definitions if we are
5205 just linking a regular application. */
5206 verdefs
= asvinfo
.verdefs
;
5208 /* Skip anonymous version tag. */
5209 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5210 verdefs
= verdefs
->next
;
5212 if (verdefs
== NULL
&& !info
->create_default_symver
)
5213 _bfd_strip_section_from_output (info
, s
);
5218 struct bfd_elf_version_tree
*t
;
5220 Elf_Internal_Verdef def
;
5221 Elf_Internal_Verdaux defaux
;
5222 struct bfd_link_hash_entry
*bh
;
5223 struct elf_link_hash_entry
*h
;
5229 /* Make space for the base version. */
5230 size
+= sizeof (Elf_External_Verdef
);
5231 size
+= sizeof (Elf_External_Verdaux
);
5234 /* Make space for the default version. */
5235 if (info
->create_default_symver
)
5237 size
+= sizeof (Elf_External_Verdef
);
5241 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5243 struct bfd_elf_version_deps
*n
;
5245 size
+= sizeof (Elf_External_Verdef
);
5246 size
+= sizeof (Elf_External_Verdaux
);
5249 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5250 size
+= sizeof (Elf_External_Verdaux
);
5254 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5255 if (s
->contents
== NULL
&& s
->size
!= 0)
5258 /* Fill in the version definition section. */
5262 def
.vd_version
= VER_DEF_CURRENT
;
5263 def
.vd_flags
= VER_FLG_BASE
;
5266 if (info
->create_default_symver
)
5268 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5269 def
.vd_next
= sizeof (Elf_External_Verdef
);
5273 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5274 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5275 + sizeof (Elf_External_Verdaux
));
5278 if (soname_indx
!= (bfd_size_type
) -1)
5280 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5282 def
.vd_hash
= bfd_elf_hash (soname
);
5283 defaux
.vda_name
= soname_indx
;
5290 name
= basename (output_bfd
->filename
);
5291 def
.vd_hash
= bfd_elf_hash (name
);
5292 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5294 if (indx
== (bfd_size_type
) -1)
5296 defaux
.vda_name
= indx
;
5298 defaux
.vda_next
= 0;
5300 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5301 (Elf_External_Verdef
*) p
);
5302 p
+= sizeof (Elf_External_Verdef
);
5303 if (info
->create_default_symver
)
5305 /* Add a symbol representing this version. */
5307 if (! (_bfd_generic_link_add_one_symbol
5308 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5310 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5312 h
= (struct elf_link_hash_entry
*) bh
;
5315 h
->type
= STT_OBJECT
;
5316 h
->verinfo
.vertree
= NULL
;
5318 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5321 /* Create a duplicate of the base version with the same
5322 aux block, but different flags. */
5325 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5327 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5328 + sizeof (Elf_External_Verdaux
));
5331 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5332 (Elf_External_Verdef
*) p
);
5333 p
+= sizeof (Elf_External_Verdef
);
5335 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5336 (Elf_External_Verdaux
*) p
);
5337 p
+= sizeof (Elf_External_Verdaux
);
5339 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5342 struct bfd_elf_version_deps
*n
;
5345 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5348 /* Add a symbol representing this version. */
5350 if (! (_bfd_generic_link_add_one_symbol
5351 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5353 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5355 h
= (struct elf_link_hash_entry
*) bh
;
5358 h
->type
= STT_OBJECT
;
5359 h
->verinfo
.vertree
= t
;
5361 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5364 def
.vd_version
= VER_DEF_CURRENT
;
5366 if (t
->globals
.list
== NULL
5367 && t
->locals
.list
== NULL
5369 def
.vd_flags
|= VER_FLG_WEAK
;
5370 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5371 def
.vd_cnt
= cdeps
+ 1;
5372 def
.vd_hash
= bfd_elf_hash (t
->name
);
5373 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5375 if (t
->next
!= NULL
)
5376 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5377 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5379 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5380 (Elf_External_Verdef
*) p
);
5381 p
+= sizeof (Elf_External_Verdef
);
5383 defaux
.vda_name
= h
->dynstr_index
;
5384 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5386 defaux
.vda_next
= 0;
5387 if (t
->deps
!= NULL
)
5388 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5389 t
->name_indx
= defaux
.vda_name
;
5391 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5392 (Elf_External_Verdaux
*) p
);
5393 p
+= sizeof (Elf_External_Verdaux
);
5395 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5397 if (n
->version_needed
== NULL
)
5399 /* This can happen if there was an error in the
5401 defaux
.vda_name
= 0;
5405 defaux
.vda_name
= n
->version_needed
->name_indx
;
5406 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5409 if (n
->next
== NULL
)
5410 defaux
.vda_next
= 0;
5412 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5414 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5415 (Elf_External_Verdaux
*) p
);
5416 p
+= sizeof (Elf_External_Verdaux
);
5420 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5421 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5424 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5427 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5429 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5432 else if (info
->flags
& DF_BIND_NOW
)
5434 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5440 if (info
->executable
)
5441 info
->flags_1
&= ~ (DF_1_INITFIRST
5444 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5448 /* Work out the size of the version reference section. */
5450 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5451 BFD_ASSERT (s
!= NULL
);
5453 struct elf_find_verdep_info sinfo
;
5455 sinfo
.output_bfd
= output_bfd
;
5457 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5458 if (sinfo
.vers
== 0)
5460 sinfo
.failed
= FALSE
;
5462 elf_link_hash_traverse (elf_hash_table (info
),
5463 _bfd_elf_link_find_version_dependencies
,
5466 if (elf_tdata (output_bfd
)->verref
== NULL
)
5467 _bfd_strip_section_from_output (info
, s
);
5470 Elf_Internal_Verneed
*t
;
5475 /* Build the version definition section. */
5478 for (t
= elf_tdata (output_bfd
)->verref
;
5482 Elf_Internal_Vernaux
*a
;
5484 size
+= sizeof (Elf_External_Verneed
);
5486 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5487 size
+= sizeof (Elf_External_Vernaux
);
5491 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5492 if (s
->contents
== NULL
)
5496 for (t
= elf_tdata (output_bfd
)->verref
;
5501 Elf_Internal_Vernaux
*a
;
5505 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5508 t
->vn_version
= VER_NEED_CURRENT
;
5510 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5511 elf_dt_name (t
->vn_bfd
) != NULL
5512 ? elf_dt_name (t
->vn_bfd
)
5513 : basename (t
->vn_bfd
->filename
),
5515 if (indx
== (bfd_size_type
) -1)
5518 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5519 if (t
->vn_nextref
== NULL
)
5522 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5523 + caux
* sizeof (Elf_External_Vernaux
));
5525 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5526 (Elf_External_Verneed
*) p
);
5527 p
+= sizeof (Elf_External_Verneed
);
5529 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5531 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5532 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5533 a
->vna_nodename
, FALSE
);
5534 if (indx
== (bfd_size_type
) -1)
5537 if (a
->vna_nextptr
== NULL
)
5540 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5542 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5543 (Elf_External_Vernaux
*) p
);
5544 p
+= sizeof (Elf_External_Vernaux
);
5548 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5549 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5552 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5556 /* Assign dynsym indicies. In a shared library we generate a
5557 section symbol for each output section, which come first.
5558 Next come all of the back-end allocated local dynamic syms,
5559 followed by the rest of the global symbols. */
5561 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5563 /* Work out the size of the symbol version section. */
5564 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5565 BFD_ASSERT (s
!= NULL
);
5566 if (dynsymcount
== 0
5567 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
5568 && !info
->create_default_symver
))
5570 _bfd_strip_section_from_output (info
, s
);
5571 /* The DYNSYMCOUNT might have changed if we were going to
5572 output a dynamic symbol table entry for S. */
5573 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5577 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5578 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5579 if (s
->contents
== NULL
)
5582 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5586 /* Set the size of the .dynsym and .hash sections. We counted
5587 the number of dynamic symbols in elf_link_add_object_symbols.
5588 We will build the contents of .dynsym and .hash when we build
5589 the final symbol table, because until then we do not know the
5590 correct value to give the symbols. We built the .dynstr
5591 section as we went along in elf_link_add_object_symbols. */
5592 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5593 BFD_ASSERT (s
!= NULL
);
5594 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5595 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5596 if (s
->contents
== NULL
&& s
->size
!= 0)
5599 if (dynsymcount
!= 0)
5601 Elf_Internal_Sym isym
;
5603 /* The first entry in .dynsym is a dummy symbol. */
5610 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5613 /* Compute the size of the hashing table. As a side effect this
5614 computes the hash values for all the names we export. */
5615 bucketcount
= compute_bucket_count (info
);
5617 s
= bfd_get_section_by_name (dynobj
, ".hash");
5618 BFD_ASSERT (s
!= NULL
);
5619 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5620 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5621 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5622 if (s
->contents
== NULL
)
5625 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5626 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5627 s
->contents
+ hash_entry_size
);
5629 elf_hash_table (info
)->bucketcount
= bucketcount
;
5631 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5632 BFD_ASSERT (s
!= NULL
);
5634 elf_finalize_dynstr (output_bfd
, info
);
5636 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5638 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5639 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5646 /* Final phase of ELF linker. */
5648 /* A structure we use to avoid passing large numbers of arguments. */
5650 struct elf_final_link_info
5652 /* General link information. */
5653 struct bfd_link_info
*info
;
5656 /* Symbol string table. */
5657 struct bfd_strtab_hash
*symstrtab
;
5658 /* .dynsym section. */
5659 asection
*dynsym_sec
;
5660 /* .hash section. */
5662 /* symbol version section (.gnu.version). */
5663 asection
*symver_sec
;
5664 /* Buffer large enough to hold contents of any section. */
5666 /* Buffer large enough to hold external relocs of any section. */
5667 void *external_relocs
;
5668 /* Buffer large enough to hold internal relocs of any section. */
5669 Elf_Internal_Rela
*internal_relocs
;
5670 /* Buffer large enough to hold external local symbols of any input
5672 bfd_byte
*external_syms
;
5673 /* And a buffer for symbol section indices. */
5674 Elf_External_Sym_Shndx
*locsym_shndx
;
5675 /* Buffer large enough to hold internal local symbols of any input
5677 Elf_Internal_Sym
*internal_syms
;
5678 /* Array large enough to hold a symbol index for each local symbol
5679 of any input BFD. */
5681 /* Array large enough to hold a section pointer for each local
5682 symbol of any input BFD. */
5683 asection
**sections
;
5684 /* Buffer to hold swapped out symbols. */
5686 /* And one for symbol section indices. */
5687 Elf_External_Sym_Shndx
*symshndxbuf
;
5688 /* Number of swapped out symbols in buffer. */
5689 size_t symbuf_count
;
5690 /* Number of symbols which fit in symbuf. */
5692 /* And same for symshndxbuf. */
5693 size_t shndxbuf_size
;
5696 /* This struct is used to pass information to elf_link_output_extsym. */
5698 struct elf_outext_info
5701 bfd_boolean localsyms
;
5702 struct elf_final_link_info
*finfo
;
5705 /* When performing a relocatable link, the input relocations are
5706 preserved. But, if they reference global symbols, the indices
5707 referenced must be updated. Update all the relocations in
5708 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5711 elf_link_adjust_relocs (bfd
*abfd
,
5712 Elf_Internal_Shdr
*rel_hdr
,
5714 struct elf_link_hash_entry
**rel_hash
)
5717 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5719 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5720 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5721 bfd_vma r_type_mask
;
5724 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5726 swap_in
= bed
->s
->swap_reloc_in
;
5727 swap_out
= bed
->s
->swap_reloc_out
;
5729 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5731 swap_in
= bed
->s
->swap_reloca_in
;
5732 swap_out
= bed
->s
->swap_reloca_out
;
5737 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5740 if (bed
->s
->arch_size
== 32)
5747 r_type_mask
= 0xffffffff;
5751 erela
= rel_hdr
->contents
;
5752 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5754 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5757 if (*rel_hash
== NULL
)
5760 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5762 (*swap_in
) (abfd
, erela
, irela
);
5763 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5764 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5765 | (irela
[j
].r_info
& r_type_mask
));
5766 (*swap_out
) (abfd
, irela
, erela
);
5770 struct elf_link_sort_rela
5776 enum elf_reloc_type_class type
;
5777 /* We use this as an array of size int_rels_per_ext_rel. */
5778 Elf_Internal_Rela rela
[1];
5782 elf_link_sort_cmp1 (const void *A
, const void *B
)
5784 const struct elf_link_sort_rela
*a
= A
;
5785 const struct elf_link_sort_rela
*b
= B
;
5786 int relativea
, relativeb
;
5788 relativea
= a
->type
== reloc_class_relative
;
5789 relativeb
= b
->type
== reloc_class_relative
;
5791 if (relativea
< relativeb
)
5793 if (relativea
> relativeb
)
5795 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5797 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5799 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5801 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5807 elf_link_sort_cmp2 (const void *A
, const void *B
)
5809 const struct elf_link_sort_rela
*a
= A
;
5810 const struct elf_link_sort_rela
*b
= B
;
5813 if (a
->u
.offset
< b
->u
.offset
)
5815 if (a
->u
.offset
> b
->u
.offset
)
5817 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5818 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5823 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5825 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5831 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5834 bfd_size_type count
, size
;
5835 size_t i
, ret
, sort_elt
, ext_size
;
5836 bfd_byte
*sort
, *s_non_relative
, *p
;
5837 struct elf_link_sort_rela
*sq
;
5838 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5839 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5840 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5841 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5842 struct bfd_link_order
*lo
;
5845 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5846 if (reldyn
== NULL
|| reldyn
->size
== 0)
5848 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5849 if (reldyn
== NULL
|| reldyn
->size
== 0)
5851 ext_size
= bed
->s
->sizeof_rel
;
5852 swap_in
= bed
->s
->swap_reloc_in
;
5853 swap_out
= bed
->s
->swap_reloc_out
;
5857 ext_size
= bed
->s
->sizeof_rela
;
5858 swap_in
= bed
->s
->swap_reloca_in
;
5859 swap_out
= bed
->s
->swap_reloca_out
;
5861 count
= reldyn
->size
/ ext_size
;
5864 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5865 if (lo
->type
== bfd_indirect_link_order
)
5867 asection
*o
= lo
->u
.indirect
.section
;
5871 if (size
!= reldyn
->size
)
5874 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5875 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5876 sort
= bfd_zmalloc (sort_elt
* count
);
5879 (*info
->callbacks
->warning
)
5880 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5884 if (bed
->s
->arch_size
== 32)
5885 r_sym_mask
= ~(bfd_vma
) 0xff;
5887 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5889 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5890 if (lo
->type
== bfd_indirect_link_order
)
5892 bfd_byte
*erel
, *erelend
;
5893 asection
*o
= lo
->u
.indirect
.section
;
5895 if (o
->contents
== NULL
&& o
->size
!= 0)
5897 /* This is a reloc section that is being handled as a normal
5898 section. See bfd_section_from_shdr. We can't combine
5899 relocs in this case. */
5904 erelend
= o
->contents
+ o
->size
;
5905 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5906 while (erel
< erelend
)
5908 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5909 (*swap_in
) (abfd
, erel
, s
->rela
);
5910 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5911 s
->u
.sym_mask
= r_sym_mask
;
5917 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5919 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5921 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5922 if (s
->type
!= reloc_class_relative
)
5928 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5929 for (; i
< count
; i
++, p
+= sort_elt
)
5931 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5932 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5934 sp
->u
.offset
= sq
->rela
->r_offset
;
5937 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5939 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5940 if (lo
->type
== bfd_indirect_link_order
)
5942 bfd_byte
*erel
, *erelend
;
5943 asection
*o
= lo
->u
.indirect
.section
;
5946 erelend
= o
->contents
+ o
->size
;
5947 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5948 while (erel
< erelend
)
5950 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5951 (*swap_out
) (abfd
, s
->rela
, erel
);
5962 /* Flush the output symbols to the file. */
5965 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5966 const struct elf_backend_data
*bed
)
5968 if (finfo
->symbuf_count
> 0)
5970 Elf_Internal_Shdr
*hdr
;
5974 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5975 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5976 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5977 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5978 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5981 hdr
->sh_size
+= amt
;
5982 finfo
->symbuf_count
= 0;
5988 /* Add a symbol to the output symbol table. */
5991 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5993 Elf_Internal_Sym
*elfsym
,
5994 asection
*input_sec
,
5995 struct elf_link_hash_entry
*h
)
5998 Elf_External_Sym_Shndx
*destshndx
;
5999 bfd_boolean (*output_symbol_hook
)
6000 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6001 struct elf_link_hash_entry
*);
6002 const struct elf_backend_data
*bed
;
6004 bed
= get_elf_backend_data (finfo
->output_bfd
);
6005 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6006 if (output_symbol_hook
!= NULL
)
6008 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6012 if (name
== NULL
|| *name
== '\0')
6013 elfsym
->st_name
= 0;
6014 else if (input_sec
->flags
& SEC_EXCLUDE
)
6015 elfsym
->st_name
= 0;
6018 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6020 if (elfsym
->st_name
== (unsigned long) -1)
6024 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6026 if (! elf_link_flush_output_syms (finfo
, bed
))
6030 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6031 destshndx
= finfo
->symshndxbuf
;
6032 if (destshndx
!= NULL
)
6034 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6038 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6039 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6040 if (destshndx
== NULL
)
6042 memset ((char *) destshndx
+ amt
, 0, amt
);
6043 finfo
->shndxbuf_size
*= 2;
6045 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6048 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6049 finfo
->symbuf_count
+= 1;
6050 bfd_get_symcount (finfo
->output_bfd
) += 1;
6055 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6056 allowing an unsatisfied unversioned symbol in the DSO to match a
6057 versioned symbol that would normally require an explicit version.
6058 We also handle the case that a DSO references a hidden symbol
6059 which may be satisfied by a versioned symbol in another DSO. */
6062 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6063 const struct elf_backend_data
*bed
,
6064 struct elf_link_hash_entry
*h
)
6067 struct elf_link_loaded_list
*loaded
;
6069 if (!is_elf_hash_table (info
->hash
))
6072 switch (h
->root
.type
)
6078 case bfd_link_hash_undefined
:
6079 case bfd_link_hash_undefweak
:
6080 abfd
= h
->root
.u
.undef
.abfd
;
6081 if ((abfd
->flags
& DYNAMIC
) == 0
6082 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6086 case bfd_link_hash_defined
:
6087 case bfd_link_hash_defweak
:
6088 abfd
= h
->root
.u
.def
.section
->owner
;
6091 case bfd_link_hash_common
:
6092 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6095 BFD_ASSERT (abfd
!= NULL
);
6097 for (loaded
= elf_hash_table (info
)->loaded
;
6099 loaded
= loaded
->next
)
6102 Elf_Internal_Shdr
*hdr
;
6103 bfd_size_type symcount
;
6104 bfd_size_type extsymcount
;
6105 bfd_size_type extsymoff
;
6106 Elf_Internal_Shdr
*versymhdr
;
6107 Elf_Internal_Sym
*isym
;
6108 Elf_Internal_Sym
*isymend
;
6109 Elf_Internal_Sym
*isymbuf
;
6110 Elf_External_Versym
*ever
;
6111 Elf_External_Versym
*extversym
;
6113 input
= loaded
->abfd
;
6115 /* We check each DSO for a possible hidden versioned definition. */
6117 || (input
->flags
& DYNAMIC
) == 0
6118 || elf_dynversym (input
) == 0)
6121 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6123 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6124 if (elf_bad_symtab (input
))
6126 extsymcount
= symcount
;
6131 extsymcount
= symcount
- hdr
->sh_info
;
6132 extsymoff
= hdr
->sh_info
;
6135 if (extsymcount
== 0)
6138 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6140 if (isymbuf
== NULL
)
6143 /* Read in any version definitions. */
6144 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6145 extversym
= bfd_malloc (versymhdr
->sh_size
);
6146 if (extversym
== NULL
)
6149 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6150 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6151 != versymhdr
->sh_size
))
6159 ever
= extversym
+ extsymoff
;
6160 isymend
= isymbuf
+ extsymcount
;
6161 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6164 Elf_Internal_Versym iver
;
6165 unsigned short version_index
;
6167 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6168 || isym
->st_shndx
== SHN_UNDEF
)
6171 name
= bfd_elf_string_from_elf_section (input
,
6174 if (strcmp (name
, h
->root
.root
.string
) != 0)
6177 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6179 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6181 /* If we have a non-hidden versioned sym, then it should
6182 have provided a definition for the undefined sym. */
6186 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6187 if (version_index
== 1 || version_index
== 2)
6189 /* This is the base or first version. We can use it. */
6203 /* Add an external symbol to the symbol table. This is called from
6204 the hash table traversal routine. When generating a shared object,
6205 we go through the symbol table twice. The first time we output
6206 anything that might have been forced to local scope in a version
6207 script. The second time we output the symbols that are still
6211 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6213 struct elf_outext_info
*eoinfo
= data
;
6214 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6216 Elf_Internal_Sym sym
;
6217 asection
*input_sec
;
6218 const struct elf_backend_data
*bed
;
6220 if (h
->root
.type
== bfd_link_hash_warning
)
6222 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6223 if (h
->root
.type
== bfd_link_hash_new
)
6227 /* Decide whether to output this symbol in this pass. */
6228 if (eoinfo
->localsyms
)
6230 if (!h
->forced_local
)
6235 if (h
->forced_local
)
6239 bed
= get_elf_backend_data (finfo
->output_bfd
);
6241 /* If we have an undefined symbol reference here then it must have
6242 come from a shared library that is being linked in. (Undefined
6243 references in regular files have already been handled). If we
6244 are reporting errors for this situation then do so now. */
6245 if (h
->root
.type
== bfd_link_hash_undefined
6248 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6249 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6251 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6252 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6253 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6255 eoinfo
->failed
= TRUE
;
6260 /* We should also warn if a forced local symbol is referenced from
6261 shared libraries. */
6262 if (! finfo
->info
->relocatable
6263 && (! finfo
->info
->shared
)
6268 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6270 (*_bfd_error_handler
)
6271 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6272 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6273 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6275 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6276 ? "hidden" : "local",
6277 h
->root
.root
.string
);
6278 eoinfo
->failed
= TRUE
;
6282 /* We don't want to output symbols that have never been mentioned by
6283 a regular file, or that we have been told to strip. However, if
6284 h->indx is set to -2, the symbol is used by a reloc and we must
6288 else if ((h
->def_dynamic
6290 || h
->root
.type
== bfd_link_hash_new
)
6294 else if (finfo
->info
->strip
== strip_all
)
6296 else if (finfo
->info
->strip
== strip_some
6297 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6298 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6300 else if (finfo
->info
->strip_discarded
6301 && (h
->root
.type
== bfd_link_hash_defined
6302 || h
->root
.type
== bfd_link_hash_defweak
)
6303 && elf_discarded_section (h
->root
.u
.def
.section
))
6308 /* If we're stripping it, and it's not a dynamic symbol, there's
6309 nothing else to do unless it is a forced local symbol. */
6312 && !h
->forced_local
)
6316 sym
.st_size
= h
->size
;
6317 sym
.st_other
= h
->other
;
6318 if (h
->forced_local
)
6319 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6320 else if (h
->root
.type
== bfd_link_hash_undefweak
6321 || h
->root
.type
== bfd_link_hash_defweak
)
6322 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6324 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6326 switch (h
->root
.type
)
6329 case bfd_link_hash_new
:
6330 case bfd_link_hash_warning
:
6334 case bfd_link_hash_undefined
:
6335 case bfd_link_hash_undefweak
:
6336 input_sec
= bfd_und_section_ptr
;
6337 sym
.st_shndx
= SHN_UNDEF
;
6340 case bfd_link_hash_defined
:
6341 case bfd_link_hash_defweak
:
6343 input_sec
= h
->root
.u
.def
.section
;
6344 if (input_sec
->output_section
!= NULL
)
6347 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6348 input_sec
->output_section
);
6349 if (sym
.st_shndx
== SHN_BAD
)
6351 (*_bfd_error_handler
)
6352 (_("%B: could not find output section %A for input section %A"),
6353 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6354 eoinfo
->failed
= TRUE
;
6358 /* ELF symbols in relocatable files are section relative,
6359 but in nonrelocatable files they are virtual
6361 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6362 if (! finfo
->info
->relocatable
)
6364 sym
.st_value
+= input_sec
->output_section
->vma
;
6365 if (h
->type
== STT_TLS
)
6367 /* STT_TLS symbols are relative to PT_TLS segment
6369 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6370 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6376 BFD_ASSERT (input_sec
->owner
== NULL
6377 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6378 sym
.st_shndx
= SHN_UNDEF
;
6379 input_sec
= bfd_und_section_ptr
;
6384 case bfd_link_hash_common
:
6385 input_sec
= h
->root
.u
.c
.p
->section
;
6386 sym
.st_shndx
= SHN_COMMON
;
6387 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6390 case bfd_link_hash_indirect
:
6391 /* These symbols are created by symbol versioning. They point
6392 to the decorated version of the name. For example, if the
6393 symbol foo@@GNU_1.2 is the default, which should be used when
6394 foo is used with no version, then we add an indirect symbol
6395 foo which points to foo@@GNU_1.2. We ignore these symbols,
6396 since the indirected symbol is already in the hash table. */
6400 /* Give the processor backend a chance to tweak the symbol value,
6401 and also to finish up anything that needs to be done for this
6402 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6403 forced local syms when non-shared is due to a historical quirk. */
6404 if ((h
->dynindx
!= -1
6406 && ((finfo
->info
->shared
6407 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6408 || h
->root
.type
!= bfd_link_hash_undefweak
))
6409 || !h
->forced_local
)
6410 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6412 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6413 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6415 eoinfo
->failed
= TRUE
;
6420 /* If we are marking the symbol as undefined, and there are no
6421 non-weak references to this symbol from a regular object, then
6422 mark the symbol as weak undefined; if there are non-weak
6423 references, mark the symbol as strong. We can't do this earlier,
6424 because it might not be marked as undefined until the
6425 finish_dynamic_symbol routine gets through with it. */
6426 if (sym
.st_shndx
== SHN_UNDEF
6428 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6429 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6433 if (h
->ref_regular_nonweak
)
6434 bindtype
= STB_GLOBAL
;
6436 bindtype
= STB_WEAK
;
6437 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6440 /* If a non-weak symbol with non-default visibility is not defined
6441 locally, it is a fatal error. */
6442 if (! finfo
->info
->relocatable
6443 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6444 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6445 && h
->root
.type
== bfd_link_hash_undefined
6448 (*_bfd_error_handler
)
6449 (_("%B: %s symbol `%s' isn't defined"),
6451 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6453 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6454 ? "internal" : "hidden",
6455 h
->root
.root
.string
);
6456 eoinfo
->failed
= TRUE
;
6460 /* If this symbol should be put in the .dynsym section, then put it
6461 there now. We already know the symbol index. We also fill in
6462 the entry in the .hash section. */
6463 if (h
->dynindx
!= -1
6464 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6468 size_t hash_entry_size
;
6469 bfd_byte
*bucketpos
;
6473 sym
.st_name
= h
->dynstr_index
;
6474 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6475 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6477 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6478 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6480 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6481 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6482 + (bucket
+ 2) * hash_entry_size
);
6483 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6484 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6485 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6486 ((bfd_byte
*) finfo
->hash_sec
->contents
6487 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6489 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6491 Elf_Internal_Versym iversym
;
6492 Elf_External_Versym
*eversym
;
6494 if (!h
->def_regular
)
6496 if (h
->verinfo
.verdef
== NULL
)
6497 iversym
.vs_vers
= 0;
6499 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6503 if (h
->verinfo
.vertree
== NULL
)
6504 iversym
.vs_vers
= 1;
6506 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6507 if (finfo
->info
->create_default_symver
)
6512 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6514 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6515 eversym
+= h
->dynindx
;
6516 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6520 /* If we're stripping it, then it was just a dynamic symbol, and
6521 there's nothing else to do. */
6522 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6525 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6527 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6529 eoinfo
->failed
= TRUE
;
6536 /* Return TRUE if special handling is done for relocs in SEC against
6537 symbols defined in discarded sections. */
6540 elf_section_ignore_discarded_relocs (asection
*sec
)
6542 const struct elf_backend_data
*bed
;
6544 switch (sec
->sec_info_type
)
6546 case ELF_INFO_TYPE_STABS
:
6547 case ELF_INFO_TYPE_EH_FRAME
:
6553 bed
= get_elf_backend_data (sec
->owner
);
6554 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6555 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6561 enum action_discarded
6567 /* Return a mask saying how ld should treat relocations in SEC against
6568 symbols defined in discarded sections. If this function returns
6569 COMPLAIN set, ld will issue a warning message. If this function
6570 returns PRETEND set, and the discarded section was link-once and the
6571 same size as the kept link-once section, ld will pretend that the
6572 symbol was actually defined in the kept section. Otherwise ld will
6573 zero the reloc (at least that is the intent, but some cooperation by
6574 the target dependent code is needed, particularly for REL targets). */
6577 elf_action_discarded (asection
*sec
)
6579 if (sec
->flags
& SEC_DEBUGGING
)
6582 if (strcmp (".eh_frame", sec
->name
) == 0)
6585 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6588 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6591 if (strcmp (".fixup", sec
->name
) == 0)
6594 return COMPLAIN
| PRETEND
;
6597 /* Find a match between a section and a member of a section group. */
6600 match_group_member (asection
*sec
, asection
*group
)
6602 asection
*first
= elf_next_in_group (group
);
6603 asection
*s
= first
;
6607 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6617 /* Link an input file into the linker output file. This function
6618 handles all the sections and relocations of the input file at once.
6619 This is so that we only have to read the local symbols once, and
6620 don't have to keep them in memory. */
6623 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6625 bfd_boolean (*relocate_section
)
6626 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6627 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6629 Elf_Internal_Shdr
*symtab_hdr
;
6632 Elf_Internal_Sym
*isymbuf
;
6633 Elf_Internal_Sym
*isym
;
6634 Elf_Internal_Sym
*isymend
;
6636 asection
**ppsection
;
6638 const struct elf_backend_data
*bed
;
6639 bfd_boolean emit_relocs
;
6640 struct elf_link_hash_entry
**sym_hashes
;
6642 output_bfd
= finfo
->output_bfd
;
6643 bed
= get_elf_backend_data (output_bfd
);
6644 relocate_section
= bed
->elf_backend_relocate_section
;
6646 /* If this is a dynamic object, we don't want to do anything here:
6647 we don't want the local symbols, and we don't want the section
6649 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6652 emit_relocs
= (finfo
->info
->relocatable
6653 || finfo
->info
->emitrelocations
6654 || bed
->elf_backend_emit_relocs
);
6656 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6657 if (elf_bad_symtab (input_bfd
))
6659 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6664 locsymcount
= symtab_hdr
->sh_info
;
6665 extsymoff
= symtab_hdr
->sh_info
;
6668 /* Read the local symbols. */
6669 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6670 if (isymbuf
== NULL
&& locsymcount
!= 0)
6672 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6673 finfo
->internal_syms
,
6674 finfo
->external_syms
,
6675 finfo
->locsym_shndx
);
6676 if (isymbuf
== NULL
)
6680 /* Find local symbol sections and adjust values of symbols in
6681 SEC_MERGE sections. Write out those local symbols we know are
6682 going into the output file. */
6683 isymend
= isymbuf
+ locsymcount
;
6684 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6686 isym
++, pindex
++, ppsection
++)
6690 Elf_Internal_Sym osym
;
6694 if (elf_bad_symtab (input_bfd
))
6696 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6703 if (isym
->st_shndx
== SHN_UNDEF
)
6704 isec
= bfd_und_section_ptr
;
6705 else if (isym
->st_shndx
< SHN_LORESERVE
6706 || isym
->st_shndx
> SHN_HIRESERVE
)
6708 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6710 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6711 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6713 _bfd_merged_section_offset (output_bfd
, &isec
,
6714 elf_section_data (isec
)->sec_info
,
6717 else if (isym
->st_shndx
== SHN_ABS
)
6718 isec
= bfd_abs_section_ptr
;
6719 else if (isym
->st_shndx
== SHN_COMMON
)
6720 isec
= bfd_com_section_ptr
;
6729 /* Don't output the first, undefined, symbol. */
6730 if (ppsection
== finfo
->sections
)
6733 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6735 /* We never output section symbols. Instead, we use the
6736 section symbol of the corresponding section in the output
6741 /* If we are stripping all symbols, we don't want to output this
6743 if (finfo
->info
->strip
== strip_all
)
6746 /* If we are discarding all local symbols, we don't want to
6747 output this one. If we are generating a relocatable output
6748 file, then some of the local symbols may be required by
6749 relocs; we output them below as we discover that they are
6751 if (finfo
->info
->discard
== discard_all
)
6754 /* If this symbol is defined in a section which we are
6755 discarding, we don't need to keep it, but note that
6756 linker_mark is only reliable for sections that have contents.
6757 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6758 as well as linker_mark. */
6759 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6761 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6762 || (! finfo
->info
->relocatable
6763 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6766 /* Get the name of the symbol. */
6767 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6772 /* See if we are discarding symbols with this name. */
6773 if ((finfo
->info
->strip
== strip_some
6774 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6776 || (((finfo
->info
->discard
== discard_sec_merge
6777 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6778 || finfo
->info
->discard
== discard_l
)
6779 && bfd_is_local_label_name (input_bfd
, name
)))
6782 /* If we get here, we are going to output this symbol. */
6786 /* Adjust the section index for the output file. */
6787 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6788 isec
->output_section
);
6789 if (osym
.st_shndx
== SHN_BAD
)
6792 *pindex
= bfd_get_symcount (output_bfd
);
6794 /* ELF symbols in relocatable files are section relative, but
6795 in executable files they are virtual addresses. Note that
6796 this code assumes that all ELF sections have an associated
6797 BFD section with a reasonable value for output_offset; below
6798 we assume that they also have a reasonable value for
6799 output_section. Any special sections must be set up to meet
6800 these requirements. */
6801 osym
.st_value
+= isec
->output_offset
;
6802 if (! finfo
->info
->relocatable
)
6804 osym
.st_value
+= isec
->output_section
->vma
;
6805 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6807 /* STT_TLS symbols are relative to PT_TLS segment base. */
6808 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6809 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6813 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6817 /* Relocate the contents of each section. */
6818 sym_hashes
= elf_sym_hashes (input_bfd
);
6819 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6823 if (! o
->linker_mark
)
6825 /* This section was omitted from the link. */
6829 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6830 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6833 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6835 /* Section was created by _bfd_elf_link_create_dynamic_sections
6840 /* Get the contents of the section. They have been cached by a
6841 relaxation routine. Note that o is a section in an input
6842 file, so the contents field will not have been set by any of
6843 the routines which work on output files. */
6844 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6845 contents
= elf_section_data (o
)->this_hdr
.contents
;
6848 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6850 contents
= finfo
->contents
;
6851 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6855 if ((o
->flags
& SEC_RELOC
) != 0)
6857 Elf_Internal_Rela
*internal_relocs
;
6858 bfd_vma r_type_mask
;
6861 /* Get the swapped relocs. */
6863 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6864 finfo
->internal_relocs
, FALSE
);
6865 if (internal_relocs
== NULL
6866 && o
->reloc_count
> 0)
6869 if (bed
->s
->arch_size
== 32)
6876 r_type_mask
= 0xffffffff;
6880 /* Run through the relocs looking for any against symbols
6881 from discarded sections and section symbols from
6882 removed link-once sections. Complain about relocs
6883 against discarded sections. Zero relocs against removed
6884 link-once sections. Preserve debug information as much
6886 if (!elf_section_ignore_discarded_relocs (o
))
6888 Elf_Internal_Rela
*rel
, *relend
;
6889 unsigned int action
= elf_action_discarded (o
);
6891 rel
= internal_relocs
;
6892 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6893 for ( ; rel
< relend
; rel
++)
6895 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6896 asection
**ps
, *sec
;
6897 struct elf_link_hash_entry
*h
= NULL
;
6898 const char *sym_name
;
6900 if (r_symndx
== STN_UNDEF
)
6903 if (r_symndx
>= locsymcount
6904 || (elf_bad_symtab (input_bfd
)
6905 && finfo
->sections
[r_symndx
] == NULL
))
6907 h
= sym_hashes
[r_symndx
- extsymoff
];
6908 while (h
->root
.type
== bfd_link_hash_indirect
6909 || h
->root
.type
== bfd_link_hash_warning
)
6910 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6912 if (h
->root
.type
!= bfd_link_hash_defined
6913 && h
->root
.type
!= bfd_link_hash_defweak
)
6916 ps
= &h
->root
.u
.def
.section
;
6917 sym_name
= h
->root
.root
.string
;
6921 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6922 ps
= &finfo
->sections
[r_symndx
];
6923 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
);
6926 /* Complain if the definition comes from a
6927 discarded section. */
6928 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6932 BFD_ASSERT (r_symndx
!= 0);
6933 if (action
& COMPLAIN
)
6935 (*_bfd_error_handler
)
6936 (_("`%s' referenced in section `%A' of %B: "
6937 "defined in discarded section `%A' of %B\n"),
6938 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6941 /* Try to do the best we can to support buggy old
6942 versions of gcc. If we've warned, or this is
6943 debugging info, pretend that the symbol is
6944 really defined in the kept linkonce section.
6945 FIXME: This is quite broken. Modifying the
6946 symbol here means we will be changing all later
6947 uses of the symbol, not just in this section.
6948 The only thing that makes this half reasonable
6949 is that we warn in non-debug sections, and
6950 debug sections tend to come after other
6952 kept
= sec
->kept_section
;
6953 if (kept
!= NULL
&& (action
& PRETEND
))
6955 if (elf_sec_group (sec
) != NULL
)
6956 kept
= match_group_member (sec
, kept
);
6958 && sec
->size
== kept
->size
)
6965 /* Remove the symbol reference from the reloc, but
6966 don't kill the reloc completely. This is so that
6967 a zero value will be written into the section,
6968 which may have non-zero contents put there by the
6969 assembler. Zero in things like an eh_frame fde
6970 pc_begin allows stack unwinders to recognize the
6972 rel
->r_info
&= r_type_mask
;
6978 /* Relocate the section by invoking a back end routine.
6980 The back end routine is responsible for adjusting the
6981 section contents as necessary, and (if using Rela relocs
6982 and generating a relocatable output file) adjusting the
6983 reloc addend as necessary.
6985 The back end routine does not have to worry about setting
6986 the reloc address or the reloc symbol index.
6988 The back end routine is given a pointer to the swapped in
6989 internal symbols, and can access the hash table entries
6990 for the external symbols via elf_sym_hashes (input_bfd).
6992 When generating relocatable output, the back end routine
6993 must handle STB_LOCAL/STT_SECTION symbols specially. The
6994 output symbol is going to be a section symbol
6995 corresponding to the output section, which will require
6996 the addend to be adjusted. */
6998 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6999 input_bfd
, o
, contents
,
7007 Elf_Internal_Rela
*irela
;
7008 Elf_Internal_Rela
*irelaend
;
7009 bfd_vma last_offset
;
7010 struct elf_link_hash_entry
**rel_hash
;
7011 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7012 unsigned int next_erel
;
7013 bfd_boolean (*reloc_emitter
)
7014 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
7015 bfd_boolean rela_normal
;
7017 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7018 rela_normal
= (bed
->rela_normal
7019 && (input_rel_hdr
->sh_entsize
7020 == bed
->s
->sizeof_rela
));
7022 /* Adjust the reloc addresses and symbol indices. */
7024 irela
= internal_relocs
;
7025 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7026 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7027 + elf_section_data (o
->output_section
)->rel_count
7028 + elf_section_data (o
->output_section
)->rel_count2
);
7029 last_offset
= o
->output_offset
;
7030 if (!finfo
->info
->relocatable
)
7031 last_offset
+= o
->output_section
->vma
;
7032 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7034 unsigned long r_symndx
;
7036 Elf_Internal_Sym sym
;
7038 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7044 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7047 if (irela
->r_offset
>= (bfd_vma
) -2)
7049 /* This is a reloc for a deleted entry or somesuch.
7050 Turn it into an R_*_NONE reloc, at the same
7051 offset as the last reloc. elf_eh_frame.c and
7052 elf_bfd_discard_info rely on reloc offsets
7054 irela
->r_offset
= last_offset
;
7056 irela
->r_addend
= 0;
7060 irela
->r_offset
+= o
->output_offset
;
7062 /* Relocs in an executable have to be virtual addresses. */
7063 if (!finfo
->info
->relocatable
)
7064 irela
->r_offset
+= o
->output_section
->vma
;
7066 last_offset
= irela
->r_offset
;
7068 r_symndx
= irela
->r_info
>> r_sym_shift
;
7069 if (r_symndx
== STN_UNDEF
)
7072 if (r_symndx
>= locsymcount
7073 || (elf_bad_symtab (input_bfd
)
7074 && finfo
->sections
[r_symndx
] == NULL
))
7076 struct elf_link_hash_entry
*rh
;
7079 /* This is a reloc against a global symbol. We
7080 have not yet output all the local symbols, so
7081 we do not know the symbol index of any global
7082 symbol. We set the rel_hash entry for this
7083 reloc to point to the global hash table entry
7084 for this symbol. The symbol index is then
7085 set at the end of bfd_elf_final_link. */
7086 indx
= r_symndx
- extsymoff
;
7087 rh
= elf_sym_hashes (input_bfd
)[indx
];
7088 while (rh
->root
.type
== bfd_link_hash_indirect
7089 || rh
->root
.type
== bfd_link_hash_warning
)
7090 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7092 /* Setting the index to -2 tells
7093 elf_link_output_extsym that this symbol is
7095 BFD_ASSERT (rh
->indx
< 0);
7103 /* This is a reloc against a local symbol. */
7106 sym
= isymbuf
[r_symndx
];
7107 sec
= finfo
->sections
[r_symndx
];
7108 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7110 /* I suppose the backend ought to fill in the
7111 section of any STT_SECTION symbol against a
7112 processor specific section. */
7114 if (bfd_is_abs_section (sec
))
7116 else if (sec
== NULL
|| sec
->owner
== NULL
)
7118 bfd_set_error (bfd_error_bad_value
);
7123 asection
*osec
= sec
->output_section
;
7125 /* If we have discarded a section, the output
7126 section will be the absolute section. In
7127 case of discarded link-once and discarded
7128 SEC_MERGE sections, use the kept section. */
7129 if (bfd_is_abs_section (osec
)
7130 && sec
->kept_section
!= NULL
7131 && sec
->kept_section
->output_section
!= NULL
)
7133 osec
= sec
->kept_section
->output_section
;
7134 irela
->r_addend
-= osec
->vma
;
7137 if (!bfd_is_abs_section (osec
))
7139 r_symndx
= osec
->target_index
;
7140 BFD_ASSERT (r_symndx
!= 0);
7144 /* Adjust the addend according to where the
7145 section winds up in the output section. */
7147 irela
->r_addend
+= sec
->output_offset
;
7151 if (finfo
->indices
[r_symndx
] == -1)
7153 unsigned long shlink
;
7157 if (finfo
->info
->strip
== strip_all
)
7159 /* You can't do ld -r -s. */
7160 bfd_set_error (bfd_error_invalid_operation
);
7164 /* This symbol was skipped earlier, but
7165 since it is needed by a reloc, we
7166 must output it now. */
7167 shlink
= symtab_hdr
->sh_link
;
7168 name
= (bfd_elf_string_from_elf_section
7169 (input_bfd
, shlink
, sym
.st_name
));
7173 osec
= sec
->output_section
;
7175 _bfd_elf_section_from_bfd_section (output_bfd
,
7177 if (sym
.st_shndx
== SHN_BAD
)
7180 sym
.st_value
+= sec
->output_offset
;
7181 if (! finfo
->info
->relocatable
)
7183 sym
.st_value
+= osec
->vma
;
7184 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7186 /* STT_TLS symbols are relative to PT_TLS
7188 BFD_ASSERT (elf_hash_table (finfo
->info
)
7190 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7195 finfo
->indices
[r_symndx
]
7196 = bfd_get_symcount (output_bfd
);
7198 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7203 r_symndx
= finfo
->indices
[r_symndx
];
7206 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7207 | (irela
->r_info
& r_type_mask
));
7210 /* Swap out the relocs. */
7211 if (bed
->elf_backend_emit_relocs
7212 && !(finfo
->info
->relocatable
7213 || finfo
->info
->emitrelocations
))
7214 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7216 reloc_emitter
= _bfd_elf_link_output_relocs
;
7218 if (input_rel_hdr
->sh_size
!= 0
7219 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7223 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7224 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7226 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7227 * bed
->s
->int_rels_per_ext_rel
);
7228 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7235 /* Write out the modified section contents. */
7236 if (bed
->elf_backend_write_section
7237 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7239 /* Section written out. */
7241 else switch (o
->sec_info_type
)
7243 case ELF_INFO_TYPE_STABS
:
7244 if (! (_bfd_write_section_stabs
7246 &elf_hash_table (finfo
->info
)->stab_info
,
7247 o
, &elf_section_data (o
)->sec_info
, contents
)))
7250 case ELF_INFO_TYPE_MERGE
:
7251 if (! _bfd_write_merged_section (output_bfd
, o
,
7252 elf_section_data (o
)->sec_info
))
7255 case ELF_INFO_TYPE_EH_FRAME
:
7257 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7264 if (! (o
->flags
& SEC_EXCLUDE
)
7265 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7267 (file_ptr
) o
->output_offset
,
7278 /* Generate a reloc when linking an ELF file. This is a reloc
7279 requested by the linker, and does come from any input file. This
7280 is used to build constructor and destructor tables when linking
7284 elf_reloc_link_order (bfd
*output_bfd
,
7285 struct bfd_link_info
*info
,
7286 asection
*output_section
,
7287 struct bfd_link_order
*link_order
)
7289 reloc_howto_type
*howto
;
7293 struct elf_link_hash_entry
**rel_hash_ptr
;
7294 Elf_Internal_Shdr
*rel_hdr
;
7295 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7296 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7300 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7303 bfd_set_error (bfd_error_bad_value
);
7307 addend
= link_order
->u
.reloc
.p
->addend
;
7309 /* Figure out the symbol index. */
7310 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7311 + elf_section_data (output_section
)->rel_count
7312 + elf_section_data (output_section
)->rel_count2
);
7313 if (link_order
->type
== bfd_section_reloc_link_order
)
7315 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7316 BFD_ASSERT (indx
!= 0);
7317 *rel_hash_ptr
= NULL
;
7321 struct elf_link_hash_entry
*h
;
7323 /* Treat a reloc against a defined symbol as though it were
7324 actually against the section. */
7325 h
= ((struct elf_link_hash_entry
*)
7326 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7327 link_order
->u
.reloc
.p
->u
.name
,
7328 FALSE
, FALSE
, TRUE
));
7330 && (h
->root
.type
== bfd_link_hash_defined
7331 || h
->root
.type
== bfd_link_hash_defweak
))
7335 section
= h
->root
.u
.def
.section
;
7336 indx
= section
->output_section
->target_index
;
7337 *rel_hash_ptr
= NULL
;
7338 /* It seems that we ought to add the symbol value to the
7339 addend here, but in practice it has already been added
7340 because it was passed to constructor_callback. */
7341 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7345 /* Setting the index to -2 tells elf_link_output_extsym that
7346 this symbol is used by a reloc. */
7353 if (! ((*info
->callbacks
->unattached_reloc
)
7354 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7360 /* If this is an inplace reloc, we must write the addend into the
7362 if (howto
->partial_inplace
&& addend
!= 0)
7365 bfd_reloc_status_type rstat
;
7368 const char *sym_name
;
7370 size
= bfd_get_reloc_size (howto
);
7371 buf
= bfd_zmalloc (size
);
7374 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7381 case bfd_reloc_outofrange
:
7384 case bfd_reloc_overflow
:
7385 if (link_order
->type
== bfd_section_reloc_link_order
)
7386 sym_name
= bfd_section_name (output_bfd
,
7387 link_order
->u
.reloc
.p
->u
.section
);
7389 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7390 if (! ((*info
->callbacks
->reloc_overflow
)
7391 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7392 NULL
, (bfd_vma
) 0)))
7399 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7400 link_order
->offset
, size
);
7406 /* The address of a reloc is relative to the section in a
7407 relocatable file, and is a virtual address in an executable
7409 offset
= link_order
->offset
;
7410 if (! info
->relocatable
)
7411 offset
+= output_section
->vma
;
7413 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7415 irel
[i
].r_offset
= offset
;
7417 irel
[i
].r_addend
= 0;
7419 if (bed
->s
->arch_size
== 32)
7420 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7422 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7424 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7425 erel
= rel_hdr
->contents
;
7426 if (rel_hdr
->sh_type
== SHT_REL
)
7428 erel
+= (elf_section_data (output_section
)->rel_count
7429 * bed
->s
->sizeof_rel
);
7430 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7434 irel
[0].r_addend
= addend
;
7435 erel
+= (elf_section_data (output_section
)->rel_count
7436 * bed
->s
->sizeof_rela
);
7437 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7440 ++elf_section_data (output_section
)->rel_count
;
7446 /* Get the output vma of the section pointed to by the sh_link field. */
7449 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7451 Elf_Internal_Shdr
**elf_shdrp
;
7455 s
= p
->u
.indirect
.section
;
7456 elf_shdrp
= elf_elfsections (s
->owner
);
7457 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7458 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7460 The Intel C compiler generates SHT_IA_64_UNWIND with
7461 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7462 sh_info fields. Hence we could get the situation
7463 where elfsec is 0. */
7466 const struct elf_backend_data
*bed
7467 = get_elf_backend_data (s
->owner
);
7468 if (bed
->link_order_error_handler
)
7469 bed
->link_order_error_handler
7470 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7475 s
= elf_shdrp
[elfsec
]->bfd_section
;
7476 return s
->output_section
->vma
+ s
->output_offset
;
7481 /* Compare two sections based on the locations of the sections they are
7482 linked to. Used by elf_fixup_link_order. */
7485 compare_link_order (const void * a
, const void * b
)
7490 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7491 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7498 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7499 order as their linked sections. Returns false if this could not be done
7500 because an output section includes both ordered and unordered
7501 sections. Ideally we'd do this in the linker proper. */
7504 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7509 struct bfd_link_order
*p
;
7511 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7513 struct bfd_link_order
**sections
;
7519 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7521 if (p
->type
== bfd_indirect_link_order
7522 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7523 == bfd_target_elf_flavour
)
7524 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7526 s
= p
->u
.indirect
.section
;
7527 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7529 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7538 if (!seen_linkorder
)
7541 if (seen_other
&& seen_linkorder
)
7543 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7545 bfd_set_error (bfd_error_bad_value
);
7549 sections
= (struct bfd_link_order
**)
7550 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7553 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7555 sections
[seen_linkorder
++] = p
;
7557 /* Sort the input sections in the order of their linked section. */
7558 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7559 compare_link_order
);
7561 /* Change the offsets of the sections. */
7563 for (n
= 0; n
< seen_linkorder
; n
++)
7565 s
= sections
[n
]->u
.indirect
.section
;
7566 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7567 s
->output_offset
= offset
;
7568 sections
[n
]->offset
= offset
;
7569 offset
+= sections
[n
]->size
;
7576 /* Do the final step of an ELF link. */
7579 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7581 bfd_boolean dynamic
;
7582 bfd_boolean emit_relocs
;
7584 struct elf_final_link_info finfo
;
7585 register asection
*o
;
7586 register struct bfd_link_order
*p
;
7588 bfd_size_type max_contents_size
;
7589 bfd_size_type max_external_reloc_size
;
7590 bfd_size_type max_internal_reloc_count
;
7591 bfd_size_type max_sym_count
;
7592 bfd_size_type max_sym_shndx_count
;
7594 Elf_Internal_Sym elfsym
;
7596 Elf_Internal_Shdr
*symtab_hdr
;
7597 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7598 Elf_Internal_Shdr
*symstrtab_hdr
;
7599 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7600 struct elf_outext_info eoinfo
;
7602 size_t relativecount
= 0;
7603 asection
*reldyn
= 0;
7606 if (! is_elf_hash_table (info
->hash
))
7610 abfd
->flags
|= DYNAMIC
;
7612 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7613 dynobj
= elf_hash_table (info
)->dynobj
;
7615 emit_relocs
= (info
->relocatable
7616 || info
->emitrelocations
7617 || bed
->elf_backend_emit_relocs
);
7620 finfo
.output_bfd
= abfd
;
7621 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7622 if (finfo
.symstrtab
== NULL
)
7627 finfo
.dynsym_sec
= NULL
;
7628 finfo
.hash_sec
= NULL
;
7629 finfo
.symver_sec
= NULL
;
7633 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7634 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7635 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7636 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7637 /* Note that it is OK if symver_sec is NULL. */
7640 finfo
.contents
= NULL
;
7641 finfo
.external_relocs
= NULL
;
7642 finfo
.internal_relocs
= NULL
;
7643 finfo
.external_syms
= NULL
;
7644 finfo
.locsym_shndx
= NULL
;
7645 finfo
.internal_syms
= NULL
;
7646 finfo
.indices
= NULL
;
7647 finfo
.sections
= NULL
;
7648 finfo
.symbuf
= NULL
;
7649 finfo
.symshndxbuf
= NULL
;
7650 finfo
.symbuf_count
= 0;
7651 finfo
.shndxbuf_size
= 0;
7653 /* Count up the number of relocations we will output for each output
7654 section, so that we know the sizes of the reloc sections. We
7655 also figure out some maximum sizes. */
7656 max_contents_size
= 0;
7657 max_external_reloc_size
= 0;
7658 max_internal_reloc_count
= 0;
7660 max_sym_shndx_count
= 0;
7662 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7664 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7667 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7669 unsigned int reloc_count
= 0;
7670 struct bfd_elf_section_data
*esdi
= NULL
;
7671 unsigned int *rel_count1
;
7673 if (p
->type
== bfd_section_reloc_link_order
7674 || p
->type
== bfd_symbol_reloc_link_order
)
7676 else if (p
->type
== bfd_indirect_link_order
)
7680 sec
= p
->u
.indirect
.section
;
7681 esdi
= elf_section_data (sec
);
7683 /* Mark all sections which are to be included in the
7684 link. This will normally be every section. We need
7685 to do this so that we can identify any sections which
7686 the linker has decided to not include. */
7687 sec
->linker_mark
= TRUE
;
7689 if (sec
->flags
& SEC_MERGE
)
7692 if (info
->relocatable
|| info
->emitrelocations
)
7693 reloc_count
= sec
->reloc_count
;
7694 else if (bed
->elf_backend_count_relocs
)
7696 Elf_Internal_Rela
* relocs
;
7698 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7701 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7703 if (elf_section_data (o
)->relocs
!= relocs
)
7707 if (sec
->rawsize
> max_contents_size
)
7708 max_contents_size
= sec
->rawsize
;
7709 if (sec
->size
> max_contents_size
)
7710 max_contents_size
= sec
->size
;
7712 /* We are interested in just local symbols, not all
7714 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7715 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7719 if (elf_bad_symtab (sec
->owner
))
7720 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7721 / bed
->s
->sizeof_sym
);
7723 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7725 if (sym_count
> max_sym_count
)
7726 max_sym_count
= sym_count
;
7728 if (sym_count
> max_sym_shndx_count
7729 && elf_symtab_shndx (sec
->owner
) != 0)
7730 max_sym_shndx_count
= sym_count
;
7732 if ((sec
->flags
& SEC_RELOC
) != 0)
7736 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7737 if (ext_size
> max_external_reloc_size
)
7738 max_external_reloc_size
= ext_size
;
7739 if (sec
->reloc_count
> max_internal_reloc_count
)
7740 max_internal_reloc_count
= sec
->reloc_count
;
7745 if (reloc_count
== 0)
7748 o
->reloc_count
+= reloc_count
;
7750 /* MIPS may have a mix of REL and RELA relocs on sections.
7751 To support this curious ABI we keep reloc counts in
7752 elf_section_data too. We must be careful to add the
7753 relocations from the input section to the right output
7754 count. FIXME: Get rid of one count. We have
7755 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7756 rel_count1
= &esdo
->rel_count
;
7759 bfd_boolean same_size
;
7760 bfd_size_type entsize1
;
7762 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7763 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7764 || entsize1
== bed
->s
->sizeof_rela
);
7765 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7768 rel_count1
= &esdo
->rel_count2
;
7770 if (esdi
->rel_hdr2
!= NULL
)
7772 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7773 unsigned int alt_count
;
7774 unsigned int *rel_count2
;
7776 BFD_ASSERT (entsize2
!= entsize1
7777 && (entsize2
== bed
->s
->sizeof_rel
7778 || entsize2
== bed
->s
->sizeof_rela
));
7780 rel_count2
= &esdo
->rel_count2
;
7782 rel_count2
= &esdo
->rel_count
;
7784 /* The following is probably too simplistic if the
7785 backend counts output relocs unusually. */
7786 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7787 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7788 *rel_count2
+= alt_count
;
7789 reloc_count
-= alt_count
;
7792 *rel_count1
+= reloc_count
;
7795 if (o
->reloc_count
> 0)
7796 o
->flags
|= SEC_RELOC
;
7799 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7800 set it (this is probably a bug) and if it is set
7801 assign_section_numbers will create a reloc section. */
7802 o
->flags
&=~ SEC_RELOC
;
7805 /* If the SEC_ALLOC flag is not set, force the section VMA to
7806 zero. This is done in elf_fake_sections as well, but forcing
7807 the VMA to 0 here will ensure that relocs against these
7808 sections are handled correctly. */
7809 if ((o
->flags
& SEC_ALLOC
) == 0
7810 && ! o
->user_set_vma
)
7814 if (! info
->relocatable
&& merged
)
7815 elf_link_hash_traverse (elf_hash_table (info
),
7816 _bfd_elf_link_sec_merge_syms
, abfd
);
7818 /* Figure out the file positions for everything but the symbol table
7819 and the relocs. We set symcount to force assign_section_numbers
7820 to create a symbol table. */
7821 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7822 BFD_ASSERT (! abfd
->output_has_begun
);
7823 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7826 /* Set sizes, and assign file positions for reloc sections. */
7827 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7829 if ((o
->flags
& SEC_RELOC
) != 0)
7831 if (!(_bfd_elf_link_size_reloc_section
7832 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7835 if (elf_section_data (o
)->rel_hdr2
7836 && !(_bfd_elf_link_size_reloc_section
7837 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7841 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7842 to count upwards while actually outputting the relocations. */
7843 elf_section_data (o
)->rel_count
= 0;
7844 elf_section_data (o
)->rel_count2
= 0;
7847 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7849 /* We have now assigned file positions for all the sections except
7850 .symtab and .strtab. We start the .symtab section at the current
7851 file position, and write directly to it. We build the .strtab
7852 section in memory. */
7853 bfd_get_symcount (abfd
) = 0;
7854 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7855 /* sh_name is set in prep_headers. */
7856 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7857 /* sh_flags, sh_addr and sh_size all start off zero. */
7858 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7859 /* sh_link is set in assign_section_numbers. */
7860 /* sh_info is set below. */
7861 /* sh_offset is set just below. */
7862 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7864 off
= elf_tdata (abfd
)->next_file_pos
;
7865 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7867 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7868 incorrect. We do not yet know the size of the .symtab section.
7869 We correct next_file_pos below, after we do know the size. */
7871 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7872 continuously seeking to the right position in the file. */
7873 if (! info
->keep_memory
|| max_sym_count
< 20)
7874 finfo
.symbuf_size
= 20;
7876 finfo
.symbuf_size
= max_sym_count
;
7877 amt
= finfo
.symbuf_size
;
7878 amt
*= bed
->s
->sizeof_sym
;
7879 finfo
.symbuf
= bfd_malloc (amt
);
7880 if (finfo
.symbuf
== NULL
)
7882 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7884 /* Wild guess at number of output symbols. realloc'd as needed. */
7885 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7886 finfo
.shndxbuf_size
= amt
;
7887 amt
*= sizeof (Elf_External_Sym_Shndx
);
7888 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7889 if (finfo
.symshndxbuf
== NULL
)
7893 /* Start writing out the symbol table. The first symbol is always a
7895 if (info
->strip
!= strip_all
7898 elfsym
.st_value
= 0;
7901 elfsym
.st_other
= 0;
7902 elfsym
.st_shndx
= SHN_UNDEF
;
7903 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7908 /* Output a symbol for each section. We output these even if we are
7909 discarding local symbols, since they are used for relocs. These
7910 symbols have no names. We store the index of each one in the
7911 index field of the section, so that we can find it again when
7912 outputting relocs. */
7913 if (info
->strip
!= strip_all
7917 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7918 elfsym
.st_other
= 0;
7919 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7921 o
= bfd_section_from_elf_index (abfd
, i
);
7923 o
->target_index
= bfd_get_symcount (abfd
);
7924 elfsym
.st_shndx
= i
;
7925 if (info
->relocatable
|| o
== NULL
)
7926 elfsym
.st_value
= 0;
7928 elfsym
.st_value
= o
->vma
;
7929 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7931 if (i
== SHN_LORESERVE
- 1)
7932 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7936 /* Allocate some memory to hold information read in from the input
7938 if (max_contents_size
!= 0)
7940 finfo
.contents
= bfd_malloc (max_contents_size
);
7941 if (finfo
.contents
== NULL
)
7945 if (max_external_reloc_size
!= 0)
7947 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7948 if (finfo
.external_relocs
== NULL
)
7952 if (max_internal_reloc_count
!= 0)
7954 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7955 amt
*= sizeof (Elf_Internal_Rela
);
7956 finfo
.internal_relocs
= bfd_malloc (amt
);
7957 if (finfo
.internal_relocs
== NULL
)
7961 if (max_sym_count
!= 0)
7963 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7964 finfo
.external_syms
= bfd_malloc (amt
);
7965 if (finfo
.external_syms
== NULL
)
7968 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7969 finfo
.internal_syms
= bfd_malloc (amt
);
7970 if (finfo
.internal_syms
== NULL
)
7973 amt
= max_sym_count
* sizeof (long);
7974 finfo
.indices
= bfd_malloc (amt
);
7975 if (finfo
.indices
== NULL
)
7978 amt
= max_sym_count
* sizeof (asection
*);
7979 finfo
.sections
= bfd_malloc (amt
);
7980 if (finfo
.sections
== NULL
)
7984 if (max_sym_shndx_count
!= 0)
7986 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7987 finfo
.locsym_shndx
= bfd_malloc (amt
);
7988 if (finfo
.locsym_shndx
== NULL
)
7992 if (elf_hash_table (info
)->tls_sec
)
7994 bfd_vma base
, end
= 0;
7997 for (sec
= elf_hash_table (info
)->tls_sec
;
7998 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8001 bfd_vma size
= sec
->size
;
8003 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8005 struct bfd_link_order
*o
;
8007 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
8008 if (size
< o
->offset
+ o
->size
)
8009 size
= o
->offset
+ o
->size
;
8011 end
= sec
->vma
+ size
;
8013 base
= elf_hash_table (info
)->tls_sec
->vma
;
8014 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8015 elf_hash_table (info
)->tls_size
= end
- base
;
8018 /* Reorder SHF_LINK_ORDER sections. */
8019 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8021 if (!elf_fixup_link_order (abfd
, o
))
8025 /* Since ELF permits relocations to be against local symbols, we
8026 must have the local symbols available when we do the relocations.
8027 Since we would rather only read the local symbols once, and we
8028 would rather not keep them in memory, we handle all the
8029 relocations for a single input file at the same time.
8031 Unfortunately, there is no way to know the total number of local
8032 symbols until we have seen all of them, and the local symbol
8033 indices precede the global symbol indices. This means that when
8034 we are generating relocatable output, and we see a reloc against
8035 a global symbol, we can not know the symbol index until we have
8036 finished examining all the local symbols to see which ones we are
8037 going to output. To deal with this, we keep the relocations in
8038 memory, and don't output them until the end of the link. This is
8039 an unfortunate waste of memory, but I don't see a good way around
8040 it. Fortunately, it only happens when performing a relocatable
8041 link, which is not the common case. FIXME: If keep_memory is set
8042 we could write the relocs out and then read them again; I don't
8043 know how bad the memory loss will be. */
8045 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8046 sub
->output_has_begun
= FALSE
;
8047 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8049 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
8051 if (p
->type
== bfd_indirect_link_order
8052 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8053 == bfd_target_elf_flavour
)
8054 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8056 if (! sub
->output_has_begun
)
8058 if (! elf_link_input_bfd (&finfo
, sub
))
8060 sub
->output_has_begun
= TRUE
;
8063 else if (p
->type
== bfd_section_reloc_link_order
8064 || p
->type
== bfd_symbol_reloc_link_order
)
8066 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8071 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8077 /* Output any global symbols that got converted to local in a
8078 version script or due to symbol visibility. We do this in a
8079 separate step since ELF requires all local symbols to appear
8080 prior to any global symbols. FIXME: We should only do this if
8081 some global symbols were, in fact, converted to become local.
8082 FIXME: Will this work correctly with the Irix 5 linker? */
8083 eoinfo
.failed
= FALSE
;
8084 eoinfo
.finfo
= &finfo
;
8085 eoinfo
.localsyms
= TRUE
;
8086 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8091 /* That wrote out all the local symbols. Finish up the symbol table
8092 with the global symbols. Even if we want to strip everything we
8093 can, we still need to deal with those global symbols that got
8094 converted to local in a version script. */
8096 /* The sh_info field records the index of the first non local symbol. */
8097 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8100 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8102 Elf_Internal_Sym sym
;
8103 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8104 long last_local
= 0;
8106 /* Write out the section symbols for the output sections. */
8113 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8116 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8122 dynindx
= elf_section_data (s
)->dynindx
;
8125 indx
= elf_section_data (s
)->this_idx
;
8126 BFD_ASSERT (indx
> 0);
8127 sym
.st_shndx
= indx
;
8128 sym
.st_value
= s
->vma
;
8129 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8130 if (last_local
< dynindx
)
8131 last_local
= dynindx
;
8132 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8136 /* Write out the local dynsyms. */
8137 if (elf_hash_table (info
)->dynlocal
)
8139 struct elf_link_local_dynamic_entry
*e
;
8140 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8145 sym
.st_size
= e
->isym
.st_size
;
8146 sym
.st_other
= e
->isym
.st_other
;
8148 /* Copy the internal symbol as is.
8149 Note that we saved a word of storage and overwrote
8150 the original st_name with the dynstr_index. */
8153 if (e
->isym
.st_shndx
!= SHN_UNDEF
8154 && (e
->isym
.st_shndx
< SHN_LORESERVE
8155 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8157 s
= bfd_section_from_elf_index (e
->input_bfd
,
8161 elf_section_data (s
->output_section
)->this_idx
;
8162 sym
.st_value
= (s
->output_section
->vma
8164 + e
->isym
.st_value
);
8167 if (last_local
< e
->dynindx
)
8168 last_local
= e
->dynindx
;
8170 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8171 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8175 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8179 /* We get the global symbols from the hash table. */
8180 eoinfo
.failed
= FALSE
;
8181 eoinfo
.localsyms
= FALSE
;
8182 eoinfo
.finfo
= &finfo
;
8183 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8188 /* If backend needs to output some symbols not present in the hash
8189 table, do it now. */
8190 if (bed
->elf_backend_output_arch_syms
)
8192 typedef bfd_boolean (*out_sym_func
)
8193 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8194 struct elf_link_hash_entry
*);
8196 if (! ((*bed
->elf_backend_output_arch_syms
)
8197 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8201 /* Flush all symbols to the file. */
8202 if (! elf_link_flush_output_syms (&finfo
, bed
))
8205 /* Now we know the size of the symtab section. */
8206 off
+= symtab_hdr
->sh_size
;
8208 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8209 if (symtab_shndx_hdr
->sh_name
!= 0)
8211 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8212 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8213 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8214 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8215 symtab_shndx_hdr
->sh_size
= amt
;
8217 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8220 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8221 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8226 /* Finish up and write out the symbol string table (.strtab)
8228 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8229 /* sh_name was set in prep_headers. */
8230 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8231 symstrtab_hdr
->sh_flags
= 0;
8232 symstrtab_hdr
->sh_addr
= 0;
8233 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8234 symstrtab_hdr
->sh_entsize
= 0;
8235 symstrtab_hdr
->sh_link
= 0;
8236 symstrtab_hdr
->sh_info
= 0;
8237 /* sh_offset is set just below. */
8238 symstrtab_hdr
->sh_addralign
= 1;
8240 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8241 elf_tdata (abfd
)->next_file_pos
= off
;
8243 if (bfd_get_symcount (abfd
) > 0)
8245 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8246 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8250 /* Adjust the relocs to have the correct symbol indices. */
8251 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8253 if ((o
->flags
& SEC_RELOC
) == 0)
8256 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8257 elf_section_data (o
)->rel_count
,
8258 elf_section_data (o
)->rel_hashes
);
8259 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8260 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8261 elf_section_data (o
)->rel_count2
,
8262 (elf_section_data (o
)->rel_hashes
8263 + elf_section_data (o
)->rel_count
));
8265 /* Set the reloc_count field to 0 to prevent write_relocs from
8266 trying to swap the relocs out itself. */
8270 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8271 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8273 /* If we are linking against a dynamic object, or generating a
8274 shared library, finish up the dynamic linking information. */
8277 bfd_byte
*dyncon
, *dynconend
;
8279 /* Fix up .dynamic entries. */
8280 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8281 BFD_ASSERT (o
!= NULL
);
8283 dyncon
= o
->contents
;
8284 dynconend
= o
->contents
+ o
->size
;
8285 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8287 Elf_Internal_Dyn dyn
;
8291 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8298 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8300 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8302 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8303 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8306 dyn
.d_un
.d_val
= relativecount
;
8313 name
= info
->init_function
;
8316 name
= info
->fini_function
;
8319 struct elf_link_hash_entry
*h
;
8321 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8322 FALSE
, FALSE
, TRUE
);
8324 && (h
->root
.type
== bfd_link_hash_defined
8325 || h
->root
.type
== bfd_link_hash_defweak
))
8327 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8328 o
= h
->root
.u
.def
.section
;
8329 if (o
->output_section
!= NULL
)
8330 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8331 + o
->output_offset
);
8334 /* The symbol is imported from another shared
8335 library and does not apply to this one. */
8343 case DT_PREINIT_ARRAYSZ
:
8344 name
= ".preinit_array";
8346 case DT_INIT_ARRAYSZ
:
8347 name
= ".init_array";
8349 case DT_FINI_ARRAYSZ
:
8350 name
= ".fini_array";
8352 o
= bfd_get_section_by_name (abfd
, name
);
8355 (*_bfd_error_handler
)
8356 (_("%B: could not find output section %s"), abfd
, name
);
8360 (*_bfd_error_handler
)
8361 (_("warning: %s section has zero size"), name
);
8362 dyn
.d_un
.d_val
= o
->size
;
8365 case DT_PREINIT_ARRAY
:
8366 name
= ".preinit_array";
8369 name
= ".init_array";
8372 name
= ".fini_array";
8385 name
= ".gnu.version_d";
8388 name
= ".gnu.version_r";
8391 name
= ".gnu.version";
8393 o
= bfd_get_section_by_name (abfd
, name
);
8396 (*_bfd_error_handler
)
8397 (_("%B: could not find output section %s"), abfd
, name
);
8400 dyn
.d_un
.d_ptr
= o
->vma
;
8407 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8412 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8414 Elf_Internal_Shdr
*hdr
;
8416 hdr
= elf_elfsections (abfd
)[i
];
8417 if (hdr
->sh_type
== type
8418 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8420 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8421 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8424 if (dyn
.d_un
.d_val
== 0
8425 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8426 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8432 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8436 /* If we have created any dynamic sections, then output them. */
8439 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8442 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8444 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8446 || o
->output_section
== bfd_abs_section_ptr
)
8448 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8450 /* At this point, we are only interested in sections
8451 created by _bfd_elf_link_create_dynamic_sections. */
8454 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8456 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8458 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8460 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8462 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8464 (file_ptr
) o
->output_offset
,
8470 /* The contents of the .dynstr section are actually in a
8472 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8473 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8474 || ! _bfd_elf_strtab_emit (abfd
,
8475 elf_hash_table (info
)->dynstr
))
8481 if (info
->relocatable
)
8483 bfd_boolean failed
= FALSE
;
8485 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8490 /* If we have optimized stabs strings, output them. */
8491 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8493 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8497 if (info
->eh_frame_hdr
)
8499 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8503 if (finfo
.symstrtab
!= NULL
)
8504 _bfd_stringtab_free (finfo
.symstrtab
);
8505 if (finfo
.contents
!= NULL
)
8506 free (finfo
.contents
);
8507 if (finfo
.external_relocs
!= NULL
)
8508 free (finfo
.external_relocs
);
8509 if (finfo
.internal_relocs
!= NULL
)
8510 free (finfo
.internal_relocs
);
8511 if (finfo
.external_syms
!= NULL
)
8512 free (finfo
.external_syms
);
8513 if (finfo
.locsym_shndx
!= NULL
)
8514 free (finfo
.locsym_shndx
);
8515 if (finfo
.internal_syms
!= NULL
)
8516 free (finfo
.internal_syms
);
8517 if (finfo
.indices
!= NULL
)
8518 free (finfo
.indices
);
8519 if (finfo
.sections
!= NULL
)
8520 free (finfo
.sections
);
8521 if (finfo
.symbuf
!= NULL
)
8522 free (finfo
.symbuf
);
8523 if (finfo
.symshndxbuf
!= NULL
)
8524 free (finfo
.symshndxbuf
);
8525 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8527 if ((o
->flags
& SEC_RELOC
) != 0
8528 && elf_section_data (o
)->rel_hashes
!= NULL
)
8529 free (elf_section_data (o
)->rel_hashes
);
8532 elf_tdata (abfd
)->linker
= TRUE
;
8537 if (finfo
.symstrtab
!= NULL
)
8538 _bfd_stringtab_free (finfo
.symstrtab
);
8539 if (finfo
.contents
!= NULL
)
8540 free (finfo
.contents
);
8541 if (finfo
.external_relocs
!= NULL
)
8542 free (finfo
.external_relocs
);
8543 if (finfo
.internal_relocs
!= NULL
)
8544 free (finfo
.internal_relocs
);
8545 if (finfo
.external_syms
!= NULL
)
8546 free (finfo
.external_syms
);
8547 if (finfo
.locsym_shndx
!= NULL
)
8548 free (finfo
.locsym_shndx
);
8549 if (finfo
.internal_syms
!= NULL
)
8550 free (finfo
.internal_syms
);
8551 if (finfo
.indices
!= NULL
)
8552 free (finfo
.indices
);
8553 if (finfo
.sections
!= NULL
)
8554 free (finfo
.sections
);
8555 if (finfo
.symbuf
!= NULL
)
8556 free (finfo
.symbuf
);
8557 if (finfo
.symshndxbuf
!= NULL
)
8558 free (finfo
.symshndxbuf
);
8559 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8561 if ((o
->flags
& SEC_RELOC
) != 0
8562 && elf_section_data (o
)->rel_hashes
!= NULL
)
8563 free (elf_section_data (o
)->rel_hashes
);
8569 /* Garbage collect unused sections. */
8571 /* The mark phase of garbage collection. For a given section, mark
8572 it and any sections in this section's group, and all the sections
8573 which define symbols to which it refers. */
8575 typedef asection
* (*gc_mark_hook_fn
)
8576 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8577 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8580 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8582 gc_mark_hook_fn gc_mark_hook
)
8585 asection
*group_sec
;
8589 /* Mark all the sections in the group. */
8590 group_sec
= elf_section_data (sec
)->next_in_group
;
8591 if (group_sec
&& !group_sec
->gc_mark
)
8592 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8595 /* Look through the section relocs. */
8597 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8599 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8600 Elf_Internal_Shdr
*symtab_hdr
;
8601 struct elf_link_hash_entry
**sym_hashes
;
8604 bfd
*input_bfd
= sec
->owner
;
8605 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8606 Elf_Internal_Sym
*isym
= NULL
;
8609 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8610 sym_hashes
= elf_sym_hashes (input_bfd
);
8612 /* Read the local symbols. */
8613 if (elf_bad_symtab (input_bfd
))
8615 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8619 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8621 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8622 if (isym
== NULL
&& nlocsyms
!= 0)
8624 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8630 /* Read the relocations. */
8631 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8633 if (relstart
== NULL
)
8638 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8640 if (bed
->s
->arch_size
== 32)
8645 for (rel
= relstart
; rel
< relend
; rel
++)
8647 unsigned long r_symndx
;
8649 struct elf_link_hash_entry
*h
;
8651 r_symndx
= rel
->r_info
>> r_sym_shift
;
8655 if (r_symndx
>= nlocsyms
8656 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8658 h
= sym_hashes
[r_symndx
- extsymoff
];
8659 while (h
->root
.type
== bfd_link_hash_indirect
8660 || h
->root
.type
== bfd_link_hash_warning
)
8661 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8662 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8666 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8669 if (rsec
&& !rsec
->gc_mark
)
8671 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8673 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8682 if (elf_section_data (sec
)->relocs
!= relstart
)
8685 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8687 if (! info
->keep_memory
)
8690 symtab_hdr
->contents
= (unsigned char *) isym
;
8697 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8700 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8704 if (h
->root
.type
== bfd_link_hash_warning
)
8705 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8707 if (h
->dynindx
!= -1
8708 && ((h
->root
.type
!= bfd_link_hash_defined
8709 && h
->root
.type
!= bfd_link_hash_defweak
)
8710 || h
->root
.u
.def
.section
->gc_mark
))
8711 h
->dynindx
= (*idx
)++;
8716 /* The sweep phase of garbage collection. Remove all garbage sections. */
8718 typedef bfd_boolean (*gc_sweep_hook_fn
)
8719 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8722 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8726 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8730 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8733 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8735 /* Keep debug and special sections. */
8736 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8737 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8743 /* Skip sweeping sections already excluded. */
8744 if (o
->flags
& SEC_EXCLUDE
)
8747 /* Since this is early in the link process, it is simple
8748 to remove a section from the output. */
8749 o
->flags
|= SEC_EXCLUDE
;
8751 /* But we also have to update some of the relocation
8752 info we collected before. */
8754 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8756 Elf_Internal_Rela
*internal_relocs
;
8760 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8762 if (internal_relocs
== NULL
)
8765 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8767 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8768 free (internal_relocs
);
8776 /* Remove the symbols that were in the swept sections from the dynamic
8777 symbol table. GCFIXME: Anyone know how to get them out of the
8778 static symbol table as well? */
8782 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8784 elf_hash_table (info
)->dynsymcount
= i
;
8790 /* Propagate collected vtable information. This is called through
8791 elf_link_hash_traverse. */
8794 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8796 if (h
->root
.type
== bfd_link_hash_warning
)
8797 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8799 /* Those that are not vtables. */
8800 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8803 /* Those vtables that do not have parents, we cannot merge. */
8804 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8807 /* If we've already been done, exit. */
8808 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8811 /* Make sure the parent's table is up to date. */
8812 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8814 if (h
->vtable
->used
== NULL
)
8816 /* None of this table's entries were referenced. Re-use the
8818 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8819 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8824 bfd_boolean
*cu
, *pu
;
8826 /* Or the parent's entries into ours. */
8827 cu
= h
->vtable
->used
;
8829 pu
= h
->vtable
->parent
->vtable
->used
;
8832 const struct elf_backend_data
*bed
;
8833 unsigned int log_file_align
;
8835 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8836 log_file_align
= bed
->s
->log_file_align
;
8837 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8852 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8855 bfd_vma hstart
, hend
;
8856 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8857 const struct elf_backend_data
*bed
;
8858 unsigned int log_file_align
;
8860 if (h
->root
.type
== bfd_link_hash_warning
)
8861 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8863 /* Take care of both those symbols that do not describe vtables as
8864 well as those that are not loaded. */
8865 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8868 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8869 || h
->root
.type
== bfd_link_hash_defweak
);
8871 sec
= h
->root
.u
.def
.section
;
8872 hstart
= h
->root
.u
.def
.value
;
8873 hend
= hstart
+ h
->size
;
8875 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8877 return *(bfd_boolean
*) okp
= FALSE
;
8878 bed
= get_elf_backend_data (sec
->owner
);
8879 log_file_align
= bed
->s
->log_file_align
;
8881 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8883 for (rel
= relstart
; rel
< relend
; ++rel
)
8884 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8886 /* If the entry is in use, do nothing. */
8888 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8890 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8891 if (h
->vtable
->used
[entry
])
8894 /* Otherwise, kill it. */
8895 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8901 /* Mark sections containing dynamically referenced symbols. This is called
8902 through elf_link_hash_traverse. */
8905 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8906 void *okp ATTRIBUTE_UNUSED
)
8908 if (h
->root
.type
== bfd_link_hash_warning
)
8909 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8911 if ((h
->root
.type
== bfd_link_hash_defined
8912 || h
->root
.type
== bfd_link_hash_defweak
)
8914 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8919 /* Do mark and sweep of unused sections. */
8922 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8924 bfd_boolean ok
= TRUE
;
8926 asection
* (*gc_mark_hook
)
8927 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8928 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8930 if (!get_elf_backend_data (abfd
)->can_gc_sections
8931 || info
->relocatable
8932 || info
->emitrelocations
8934 || !is_elf_hash_table (info
->hash
))
8936 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8940 /* Apply transitive closure to the vtable entry usage info. */
8941 elf_link_hash_traverse (elf_hash_table (info
),
8942 elf_gc_propagate_vtable_entries_used
,
8947 /* Kill the vtable relocations that were not used. */
8948 elf_link_hash_traverse (elf_hash_table (info
),
8949 elf_gc_smash_unused_vtentry_relocs
,
8954 /* Mark dynamically referenced symbols. */
8955 if (elf_hash_table (info
)->dynamic_sections_created
)
8956 elf_link_hash_traverse (elf_hash_table (info
),
8957 elf_gc_mark_dynamic_ref_symbol
,
8962 /* Grovel through relocs to find out who stays ... */
8963 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8964 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8968 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8971 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8973 if (o
->flags
& SEC_KEEP
)
8975 /* _bfd_elf_discard_section_eh_frame knows how to discard
8976 orphaned FDEs so don't mark sections referenced by the
8977 EH frame section. */
8978 if (strcmp (o
->name
, ".eh_frame") == 0)
8980 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8986 /* ... and mark SEC_EXCLUDE for those that go. */
8987 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8993 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8996 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8998 struct elf_link_hash_entry
*h
,
9001 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9002 struct elf_link_hash_entry
**search
, *child
;
9003 bfd_size_type extsymcount
;
9004 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9006 /* The sh_info field of the symtab header tells us where the
9007 external symbols start. We don't care about the local symbols at
9009 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9010 if (!elf_bad_symtab (abfd
))
9011 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9013 sym_hashes
= elf_sym_hashes (abfd
);
9014 sym_hashes_end
= sym_hashes
+ extsymcount
;
9016 /* Hunt down the child symbol, which is in this section at the same
9017 offset as the relocation. */
9018 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9020 if ((child
= *search
) != NULL
9021 && (child
->root
.type
== bfd_link_hash_defined
9022 || child
->root
.type
== bfd_link_hash_defweak
)
9023 && child
->root
.u
.def
.section
== sec
9024 && child
->root
.u
.def
.value
== offset
)
9028 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9029 abfd
, sec
, (unsigned long) offset
);
9030 bfd_set_error (bfd_error_invalid_operation
);
9036 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9042 /* This *should* only be the absolute section. It could potentially
9043 be that someone has defined a non-global vtable though, which
9044 would be bad. It isn't worth paging in the local symbols to be
9045 sure though; that case should simply be handled by the assembler. */
9047 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9050 child
->vtable
->parent
= h
;
9055 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9058 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9059 asection
*sec ATTRIBUTE_UNUSED
,
9060 struct elf_link_hash_entry
*h
,
9063 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9064 unsigned int log_file_align
= bed
->s
->log_file_align
;
9068 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9073 if (addend
>= h
->vtable
->size
)
9075 size_t size
, bytes
, file_align
;
9076 bfd_boolean
*ptr
= h
->vtable
->used
;
9078 /* While the symbol is undefined, we have to be prepared to handle
9080 file_align
= 1 << log_file_align
;
9081 if (h
->root
.type
== bfd_link_hash_undefined
)
9082 size
= addend
+ file_align
;
9088 /* Oops! We've got a reference past the defined end of
9089 the table. This is probably a bug -- shall we warn? */
9090 size
= addend
+ file_align
;
9093 size
= (size
+ file_align
- 1) & -file_align
;
9095 /* Allocate one extra entry for use as a "done" flag for the
9096 consolidation pass. */
9097 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9101 ptr
= bfd_realloc (ptr
- 1, bytes
);
9107 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9108 * sizeof (bfd_boolean
));
9109 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9113 ptr
= bfd_zmalloc (bytes
);
9118 /* And arrange for that done flag to be at index -1. */
9119 h
->vtable
->used
= ptr
+ 1;
9120 h
->vtable
->size
= size
;
9123 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9128 struct alloc_got_off_arg
{
9130 unsigned int got_elt_size
;
9133 /* We need a special top-level link routine to convert got reference counts
9134 to real got offsets. */
9137 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9139 struct alloc_got_off_arg
*gofarg
= arg
;
9141 if (h
->root
.type
== bfd_link_hash_warning
)
9142 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9144 if (h
->got
.refcount
> 0)
9146 h
->got
.offset
= gofarg
->gotoff
;
9147 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9150 h
->got
.offset
= (bfd_vma
) -1;
9155 /* And an accompanying bit to work out final got entry offsets once
9156 we're done. Should be called from final_link. */
9159 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9160 struct bfd_link_info
*info
)
9163 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9165 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9166 struct alloc_got_off_arg gofarg
;
9168 if (! is_elf_hash_table (info
->hash
))
9171 /* The GOT offset is relative to the .got section, but the GOT header is
9172 put into the .got.plt section, if the backend uses it. */
9173 if (bed
->want_got_plt
)
9176 gotoff
= bed
->got_header_size
;
9178 /* Do the local .got entries first. */
9179 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9181 bfd_signed_vma
*local_got
;
9182 bfd_size_type j
, locsymcount
;
9183 Elf_Internal_Shdr
*symtab_hdr
;
9185 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9188 local_got
= elf_local_got_refcounts (i
);
9192 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9193 if (elf_bad_symtab (i
))
9194 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9196 locsymcount
= symtab_hdr
->sh_info
;
9198 for (j
= 0; j
< locsymcount
; ++j
)
9200 if (local_got
[j
] > 0)
9202 local_got
[j
] = gotoff
;
9203 gotoff
+= got_elt_size
;
9206 local_got
[j
] = (bfd_vma
) -1;
9210 /* Then the global .got entries. .plt refcounts are handled by
9211 adjust_dynamic_symbol */
9212 gofarg
.gotoff
= gotoff
;
9213 gofarg
.got_elt_size
= got_elt_size
;
9214 elf_link_hash_traverse (elf_hash_table (info
),
9215 elf_gc_allocate_got_offsets
,
9220 /* Many folk need no more in the way of final link than this, once
9221 got entry reference counting is enabled. */
9224 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9226 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9229 /* Invoke the regular ELF backend linker to do all the work. */
9230 return bfd_elf_final_link (abfd
, info
);
9234 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9236 struct elf_reloc_cookie
*rcookie
= cookie
;
9238 if (rcookie
->bad_symtab
)
9239 rcookie
->rel
= rcookie
->rels
;
9241 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9243 unsigned long r_symndx
;
9245 if (! rcookie
->bad_symtab
)
9246 if (rcookie
->rel
->r_offset
> offset
)
9248 if (rcookie
->rel
->r_offset
!= offset
)
9251 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9252 if (r_symndx
== SHN_UNDEF
)
9255 if (r_symndx
>= rcookie
->locsymcount
9256 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9258 struct elf_link_hash_entry
*h
;
9260 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9262 while (h
->root
.type
== bfd_link_hash_indirect
9263 || h
->root
.type
== bfd_link_hash_warning
)
9264 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9266 if ((h
->root
.type
== bfd_link_hash_defined
9267 || h
->root
.type
== bfd_link_hash_defweak
)
9268 && elf_discarded_section (h
->root
.u
.def
.section
))
9275 /* It's not a relocation against a global symbol,
9276 but it could be a relocation against a local
9277 symbol for a discarded section. */
9279 Elf_Internal_Sym
*isym
;
9281 /* Need to: get the symbol; get the section. */
9282 isym
= &rcookie
->locsyms
[r_symndx
];
9283 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9285 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9286 if (isec
!= NULL
&& elf_discarded_section (isec
))
9295 /* Discard unneeded references to discarded sections.
9296 Returns TRUE if any section's size was changed. */
9297 /* This function assumes that the relocations are in sorted order,
9298 which is true for all known assemblers. */
9301 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9303 struct elf_reloc_cookie cookie
;
9304 asection
*stab
, *eh
;
9305 Elf_Internal_Shdr
*symtab_hdr
;
9306 const struct elf_backend_data
*bed
;
9309 bfd_boolean ret
= FALSE
;
9311 if (info
->traditional_format
9312 || !is_elf_hash_table (info
->hash
))
9315 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9317 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9320 bed
= get_elf_backend_data (abfd
);
9322 if ((abfd
->flags
& DYNAMIC
) != 0)
9325 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9326 if (info
->relocatable
9329 || bfd_is_abs_section (eh
->output_section
))))
9332 stab
= bfd_get_section_by_name (abfd
, ".stab");
9335 || bfd_is_abs_section (stab
->output_section
)
9336 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9341 && bed
->elf_backend_discard_info
== NULL
)
9344 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9346 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9347 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9348 if (cookie
.bad_symtab
)
9350 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9351 cookie
.extsymoff
= 0;
9355 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9356 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9359 if (bed
->s
->arch_size
== 32)
9360 cookie
.r_sym_shift
= 8;
9362 cookie
.r_sym_shift
= 32;
9364 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9365 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9367 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9368 cookie
.locsymcount
, 0,
9370 if (cookie
.locsyms
== NULL
)
9377 count
= stab
->reloc_count
;
9379 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9381 if (cookie
.rels
!= NULL
)
9383 cookie
.rel
= cookie
.rels
;
9384 cookie
.relend
= cookie
.rels
;
9385 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9386 if (_bfd_discard_section_stabs (abfd
, stab
,
9387 elf_section_data (stab
)->sec_info
,
9388 bfd_elf_reloc_symbol_deleted_p
,
9391 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9399 count
= eh
->reloc_count
;
9401 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9403 cookie
.rel
= cookie
.rels
;
9404 cookie
.relend
= cookie
.rels
;
9405 if (cookie
.rels
!= NULL
)
9406 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9408 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9409 bfd_elf_reloc_symbol_deleted_p
,
9413 if (cookie
.rels
!= NULL
9414 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9418 if (bed
->elf_backend_discard_info
!= NULL
9419 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9422 if (cookie
.locsyms
!= NULL
9423 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9425 if (! info
->keep_memory
)
9426 free (cookie
.locsyms
);
9428 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9432 if (info
->eh_frame_hdr
9433 && !info
->relocatable
9434 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9441 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9444 const char *name
, *p
;
9445 struct bfd_section_already_linked
*l
;
9446 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9449 /* A single member comdat group section may be discarded by a
9450 linkonce section. See below. */
9451 if (sec
->output_section
== bfd_abs_section_ptr
)
9456 /* Check if it belongs to a section group. */
9457 group
= elf_sec_group (sec
);
9459 /* Return if it isn't a linkonce section nor a member of a group. A
9460 comdat group section also has SEC_LINK_ONCE set. */
9461 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9466 /* If this is the member of a single member comdat group, check if
9467 the group should be discarded. */
9468 if (elf_next_in_group (sec
) == sec
9469 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9475 /* FIXME: When doing a relocatable link, we may have trouble
9476 copying relocations in other sections that refer to local symbols
9477 in the section being discarded. Those relocations will have to
9478 be converted somehow; as of this writing I'm not sure that any of
9479 the backends handle that correctly.
9481 It is tempting to instead not discard link once sections when
9482 doing a relocatable link (technically, they should be discarded
9483 whenever we are building constructors). However, that fails,
9484 because the linker winds up combining all the link once sections
9485 into a single large link once section, which defeats the purpose
9486 of having link once sections in the first place.
9488 Also, not merging link once sections in a relocatable link
9489 causes trouble for MIPS ELF, which relies on link once semantics
9490 to handle the .reginfo section correctly. */
9492 name
= bfd_get_section_name (abfd
, sec
);
9494 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9495 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9500 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9502 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9504 /* We may have 3 different sections on the list: group section,
9505 comdat section and linkonce section. SEC may be a linkonce or
9506 group section. We match a group section with a group section,
9507 a linkonce section with a linkonce section, and ignore comdat
9509 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9510 && strcmp (name
, l
->sec
->name
) == 0
9511 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9513 /* The section has already been linked. See if we should
9515 switch (flags
& SEC_LINK_DUPLICATES
)
9520 case SEC_LINK_DUPLICATES_DISCARD
:
9523 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9524 (*_bfd_error_handler
)
9525 (_("%B: ignoring duplicate section `%A'\n"),
9529 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9530 if (sec
->size
!= l
->sec
->size
)
9531 (*_bfd_error_handler
)
9532 (_("%B: duplicate section `%A' has different size\n"),
9536 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9537 if (sec
->size
!= l
->sec
->size
)
9538 (*_bfd_error_handler
)
9539 (_("%B: duplicate section `%A' has different size\n"),
9541 else if (sec
->size
!= 0)
9543 bfd_byte
*sec_contents
, *l_sec_contents
;
9545 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9546 (*_bfd_error_handler
)
9547 (_("%B: warning: could not read contents of section `%A'\n"),
9549 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9551 (*_bfd_error_handler
)
9552 (_("%B: warning: could not read contents of section `%A'\n"),
9553 l
->sec
->owner
, l
->sec
);
9554 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9555 (*_bfd_error_handler
)
9556 (_("%B: warning: duplicate section `%A' has different contents\n"),
9560 free (sec_contents
);
9562 free (l_sec_contents
);
9567 /* Set the output_section field so that lang_add_section
9568 does not create a lang_input_section structure for this
9569 section. Since there might be a symbol in the section
9570 being discarded, we must retain a pointer to the section
9571 which we are really going to use. */
9572 sec
->output_section
= bfd_abs_section_ptr
;
9573 sec
->kept_section
= l
->sec
;
9575 if (flags
& SEC_GROUP
)
9577 asection
*first
= elf_next_in_group (sec
);
9578 asection
*s
= first
;
9582 s
->output_section
= bfd_abs_section_ptr
;
9583 /* Record which group discards it. */
9584 s
->kept_section
= l
->sec
;
9585 s
= elf_next_in_group (s
);
9586 /* These lists are circular. */
9598 /* If this is the member of a single member comdat group and the
9599 group hasn't be discarded, we check if it matches a linkonce
9600 section. We only record the discarded comdat group. Otherwise
9601 the undiscarded group will be discarded incorrectly later since
9602 itself has been recorded. */
9603 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9604 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9605 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9606 && bfd_elf_match_symbols_in_sections (l
->sec
,
9607 elf_next_in_group (sec
)))
9609 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9610 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9611 group
->output_section
= bfd_abs_section_ptr
;
9618 /* There is no direct match. But for linkonce section, we should
9619 check if there is a match with comdat group member. We always
9620 record the linkonce section, discarded or not. */
9621 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9622 if (l
->sec
->flags
& SEC_GROUP
)
9624 asection
*first
= elf_next_in_group (l
->sec
);
9627 && elf_next_in_group (first
) == first
9628 && bfd_elf_match_symbols_in_sections (first
, sec
))
9630 sec
->output_section
= bfd_abs_section_ptr
;
9631 sec
->kept_section
= l
->sec
;
9636 /* This is the first section with this name. Record it. */
9637 bfd_section_already_linked_table_insert (already_linked_list
, sec
);