1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 some sections which will be filled in with dynamic linking
107 information. ABFD is an input file which requires dynamic sections
108 to be created. The dynamic sections take up virtual memory space
109 when the final executable is run, so we need to create them before
110 addresses are assigned to the output sections. We work out the
111 actual contents and size of these sections later. */
114 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
117 register asection
*s
;
118 struct elf_link_hash_entry
*h
;
119 struct bfd_link_hash_entry
*bh
;
120 const struct elf_backend_data
*bed
;
122 if (! is_elf_hash_table (info
->hash
))
125 if (elf_hash_table (info
)->dynamic_sections_created
)
128 /* Make sure that all dynamic sections use the same input BFD. */
129 if (elf_hash_table (info
)->dynobj
== NULL
)
130 elf_hash_table (info
)->dynobj
= abfd
;
132 abfd
= elf_hash_table (info
)->dynobj
;
134 bed
= get_elf_backend_data (abfd
);
136 flags
= bed
->dynamic_sec_flags
;
138 /* A dynamically linked executable has a .interp section, but a
139 shared library does not. */
140 if (info
->executable
)
142 s
= bfd_make_section (abfd
, ".interp");
144 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
148 if (! info
->traditional_format
)
150 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
152 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
153 || ! bfd_set_section_alignment (abfd
, s
, 2))
155 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
158 /* Create sections to hold version informations. These are removed
159 if they are not needed. */
160 s
= bfd_make_section (abfd
, ".gnu.version_d");
162 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
163 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
166 s
= bfd_make_section (abfd
, ".gnu.version");
168 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
169 || ! bfd_set_section_alignment (abfd
, s
, 1))
172 s
= bfd_make_section (abfd
, ".gnu.version_r");
174 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
175 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
178 s
= bfd_make_section (abfd
, ".dynsym");
180 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
181 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
184 s
= bfd_make_section (abfd
, ".dynstr");
186 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
189 /* Create a strtab to hold the dynamic symbol names. */
190 if (elf_hash_table (info
)->dynstr
== NULL
)
192 elf_hash_table (info
)->dynstr
= _bfd_elf_strtab_init ();
193 if (elf_hash_table (info
)->dynstr
== NULL
)
197 s
= bfd_make_section (abfd
, ".dynamic");
199 || ! bfd_set_section_flags (abfd
, s
, flags
)
200 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
203 /* The special symbol _DYNAMIC is always set to the start of the
204 .dynamic section. This call occurs before we have processed the
205 symbols for any dynamic object, so we don't have to worry about
206 overriding a dynamic definition. We could set _DYNAMIC in a
207 linker script, but we only want to define it if we are, in fact,
208 creating a .dynamic section. We don't want to define it if there
209 is no .dynamic section, since on some ELF platforms the start up
210 code examines it to decide how to initialize the process. */
212 if (! (_bfd_generic_link_add_one_symbol
213 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
214 get_elf_backend_data (abfd
)->collect
, &bh
)))
216 h
= (struct elf_link_hash_entry
*) bh
;
218 h
->type
= STT_OBJECT
;
220 if (! info
->executable
221 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
224 s
= bfd_make_section (abfd
, ".hash");
226 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
227 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
229 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
231 /* Let the backend create the rest of the sections. This lets the
232 backend set the right flags. The backend will normally create
233 the .got and .plt sections. */
234 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
237 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
242 /* Create dynamic sections when linking against a dynamic object. */
245 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
247 flagword flags
, pltflags
;
249 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
251 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
252 .rel[a].bss sections. */
253 flags
= bed
->dynamic_sec_flags
;
256 if (bed
->plt_not_loaded
)
257 /* We do not clear SEC_ALLOC here because we still want the OS to
258 allocate space for the section; it's just that there's nothing
259 to read in from the object file. */
260 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
262 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
263 if (bed
->plt_readonly
)
264 pltflags
|= SEC_READONLY
;
266 s
= bfd_make_section (abfd
, ".plt");
268 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
269 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
272 if (bed
->want_plt_sym
)
274 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
276 struct elf_link_hash_entry
*h
;
277 struct bfd_link_hash_entry
*bh
= NULL
;
279 if (! (_bfd_generic_link_add_one_symbol
280 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
281 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
283 h
= (struct elf_link_hash_entry
*) bh
;
285 h
->type
= STT_OBJECT
;
287 if (! info
->executable
288 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
292 s
= bfd_make_section (abfd
,
293 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
295 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
296 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
299 if (! _bfd_elf_create_got_section (abfd
, info
))
302 if (bed
->want_dynbss
)
304 /* The .dynbss section is a place to put symbols which are defined
305 by dynamic objects, are referenced by regular objects, and are
306 not functions. We must allocate space for them in the process
307 image and use a R_*_COPY reloc to tell the dynamic linker to
308 initialize them at run time. The linker script puts the .dynbss
309 section into the .bss section of the final image. */
310 s
= bfd_make_section (abfd
, ".dynbss");
312 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
315 /* The .rel[a].bss section holds copy relocs. This section is not
316 normally needed. We need to create it here, though, so that the
317 linker will map it to an output section. We can't just create it
318 only if we need it, because we will not know whether we need it
319 until we have seen all the input files, and the first time the
320 main linker code calls BFD after examining all the input files
321 (size_dynamic_sections) the input sections have already been
322 mapped to the output sections. If the section turns out not to
323 be needed, we can discard it later. We will never need this
324 section when generating a shared object, since they do not use
328 s
= bfd_make_section (abfd
,
329 (bed
->default_use_rela_p
330 ? ".rela.bss" : ".rel.bss"));
332 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
333 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
341 /* Record a new dynamic symbol. We record the dynamic symbols as we
342 read the input files, since we need to have a list of all of them
343 before we can determine the final sizes of the output sections.
344 Note that we may actually call this function even though we are not
345 going to output any dynamic symbols; in some cases we know that a
346 symbol should be in the dynamic symbol table, but only if there is
350 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
351 struct elf_link_hash_entry
*h
)
353 if (h
->dynindx
== -1)
355 struct elf_strtab_hash
*dynstr
;
360 /* XXX: The ABI draft says the linker must turn hidden and
361 internal symbols into STB_LOCAL symbols when producing the
362 DSO. However, if ld.so honors st_other in the dynamic table,
363 this would not be necessary. */
364 switch (ELF_ST_VISIBILITY (h
->other
))
368 if (h
->root
.type
!= bfd_link_hash_undefined
369 && h
->root
.type
!= bfd_link_hash_undefweak
)
379 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
380 ++elf_hash_table (info
)->dynsymcount
;
382 dynstr
= elf_hash_table (info
)->dynstr
;
385 /* Create a strtab to hold the dynamic symbol names. */
386 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
391 /* We don't put any version information in the dynamic string
393 name
= h
->root
.root
.string
;
394 p
= strchr (name
, ELF_VER_CHR
);
396 /* We know that the p points into writable memory. In fact,
397 there are only a few symbols that have read-only names, being
398 those like _GLOBAL_OFFSET_TABLE_ that are created specially
399 by the backends. Most symbols will have names pointing into
400 an ELF string table read from a file, or to objalloc memory. */
403 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
408 if (indx
== (bfd_size_type
) -1)
410 h
->dynstr_index
= indx
;
416 /* Record an assignment to a symbol made by a linker script. We need
417 this in case some dynamic object refers to this symbol. */
420 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
421 struct bfd_link_info
*info
,
425 struct elf_link_hash_entry
*h
;
427 if (!is_elf_hash_table (info
->hash
))
430 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, TRUE
, FALSE
);
434 /* Since we're defining the symbol, don't let it seem to have not
435 been defined. record_dynamic_symbol and size_dynamic_sections
437 ??? Changing bfd_link_hash_undefined to bfd_link_hash_new (or
438 to bfd_link_hash_undefweak, see linker.c:link_action) runs the risk
439 of some later symbol manipulation setting the symbol back to
440 bfd_link_hash_undefined, and the linker trying to add the symbol to
441 the undefs list twice. */
442 if (h
->root
.type
== bfd_link_hash_undefweak
443 || h
->root
.type
== bfd_link_hash_undefined
)
444 h
->root
.type
= bfd_link_hash_new
;
446 if (h
->root
.type
== bfd_link_hash_new
)
449 /* If this symbol is being provided by the linker script, and it is
450 currently defined by a dynamic object, but not by a regular
451 object, then mark it as undefined so that the generic linker will
452 force the correct value. */
456 h
->root
.type
= bfd_link_hash_undefined
;
458 /* If this symbol is not being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular object,
460 then clear out any version information because the symbol will not be
461 associated with the dynamic object any more. */
465 h
->verinfo
.verdef
= NULL
;
474 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
477 /* If this is a weak defined symbol, and we know a corresponding
478 real symbol from the same dynamic object, make sure the real
479 symbol is also made into a dynamic symbol. */
480 if (h
->u
.weakdef
!= NULL
481 && h
->u
.weakdef
->dynindx
== -1)
483 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
491 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
492 success, and 2 on a failure caused by attempting to record a symbol
493 in a discarded section, eg. a discarded link-once section symbol. */
496 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
501 struct elf_link_local_dynamic_entry
*entry
;
502 struct elf_link_hash_table
*eht
;
503 struct elf_strtab_hash
*dynstr
;
504 unsigned long dynstr_index
;
506 Elf_External_Sym_Shndx eshndx
;
507 char esym
[sizeof (Elf64_External_Sym
)];
509 if (! is_elf_hash_table (info
->hash
))
512 /* See if the entry exists already. */
513 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
514 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
517 amt
= sizeof (*entry
);
518 entry
= bfd_alloc (input_bfd
, amt
);
522 /* Go find the symbol, so that we can find it's name. */
523 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
524 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
526 bfd_release (input_bfd
, entry
);
530 if (entry
->isym
.st_shndx
!= SHN_UNDEF
531 && (entry
->isym
.st_shndx
< SHN_LORESERVE
532 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
536 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
537 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
539 /* We can still bfd_release here as nothing has done another
540 bfd_alloc. We can't do this later in this function. */
541 bfd_release (input_bfd
, entry
);
546 name
= (bfd_elf_string_from_elf_section
547 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
548 entry
->isym
.st_name
));
550 dynstr
= elf_hash_table (info
)->dynstr
;
553 /* Create a strtab to hold the dynamic symbol names. */
554 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
559 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
560 if (dynstr_index
== (unsigned long) -1)
562 entry
->isym
.st_name
= dynstr_index
;
564 eht
= elf_hash_table (info
);
566 entry
->next
= eht
->dynlocal
;
567 eht
->dynlocal
= entry
;
568 entry
->input_bfd
= input_bfd
;
569 entry
->input_indx
= input_indx
;
572 /* Whatever binding the symbol had before, it's now local. */
574 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
576 /* The dynindx will be set at the end of size_dynamic_sections. */
581 /* Return the dynindex of a local dynamic symbol. */
584 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
588 struct elf_link_local_dynamic_entry
*e
;
590 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
591 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
596 /* This function is used to renumber the dynamic symbols, if some of
597 them are removed because they are marked as local. This is called
598 via elf_link_hash_traverse. */
601 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
604 size_t *count
= data
;
606 if (h
->root
.type
== bfd_link_hash_warning
)
607 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
609 if (h
->dynindx
!= -1)
610 h
->dynindx
= ++(*count
);
615 /* Return true if the dynamic symbol for a given section should be
616 omitted when creating a shared library. */
618 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
619 struct bfd_link_info
*info
,
622 switch (elf_section_data (p
)->this_hdr
.sh_type
)
626 /* If sh_type is yet undecided, assume it could be
627 SHT_PROGBITS/SHT_NOBITS. */
629 if (strcmp (p
->name
, ".got") == 0
630 || strcmp (p
->name
, ".got.plt") == 0
631 || strcmp (p
->name
, ".plt") == 0)
634 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
637 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
638 && (ip
->flags
& SEC_LINKER_CREATED
)
639 && ip
->output_section
== p
)
644 /* There shouldn't be section relative relocations
645 against any other section. */
651 /* Assign dynsym indices. In a shared library we generate a section
652 symbol for each output section, which come first. Next come all of
653 the back-end allocated local dynamic syms, followed by the rest of
654 the global symbols. */
657 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
659 unsigned long dynsymcount
= 0;
663 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
665 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
666 if ((p
->flags
& SEC_EXCLUDE
) == 0
667 && (p
->flags
& SEC_ALLOC
) != 0
668 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
669 elf_section_data (p
)->dynindx
= ++dynsymcount
;
672 if (elf_hash_table (info
)->dynlocal
)
674 struct elf_link_local_dynamic_entry
*p
;
675 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
676 p
->dynindx
= ++dynsymcount
;
679 elf_link_hash_traverse (elf_hash_table (info
),
680 elf_link_renumber_hash_table_dynsyms
,
683 /* There is an unused NULL entry at the head of the table which
684 we must account for in our count. Unless there weren't any
685 symbols, which means we'll have no table at all. */
686 if (dynsymcount
!= 0)
689 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
692 /* This function is called when we want to define a new symbol. It
693 handles the various cases which arise when we find a definition in
694 a dynamic object, or when there is already a definition in a
695 dynamic object. The new symbol is described by NAME, SYM, PSEC,
696 and PVALUE. We set SYM_HASH to the hash table entry. We set
697 OVERRIDE if the old symbol is overriding a new definition. We set
698 TYPE_CHANGE_OK if it is OK for the type to change. We set
699 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
700 change, we mean that we shouldn't warn if the type or size does
704 _bfd_elf_merge_symbol (bfd
*abfd
,
705 struct bfd_link_info
*info
,
707 Elf_Internal_Sym
*sym
,
710 struct elf_link_hash_entry
**sym_hash
,
712 bfd_boolean
*override
,
713 bfd_boolean
*type_change_ok
,
714 bfd_boolean
*size_change_ok
)
716 asection
*sec
, *oldsec
;
717 struct elf_link_hash_entry
*h
;
718 struct elf_link_hash_entry
*flip
;
721 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
722 bfd_boolean newweak
, oldweak
;
728 bind
= ELF_ST_BIND (sym
->st_info
);
730 if (! bfd_is_und_section (sec
))
731 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
733 h
= ((struct elf_link_hash_entry
*)
734 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
739 /* This code is for coping with dynamic objects, and is only useful
740 if we are doing an ELF link. */
741 if (info
->hash
->creator
!= abfd
->xvec
)
744 /* For merging, we only care about real symbols. */
746 while (h
->root
.type
== bfd_link_hash_indirect
747 || h
->root
.type
== bfd_link_hash_warning
)
748 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
750 /* If we just created the symbol, mark it as being an ELF symbol.
751 Other than that, there is nothing to do--there is no merge issue
752 with a newly defined symbol--so we just return. */
754 if (h
->root
.type
== bfd_link_hash_new
)
760 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
763 switch (h
->root
.type
)
770 case bfd_link_hash_undefined
:
771 case bfd_link_hash_undefweak
:
772 oldbfd
= h
->root
.u
.undef
.abfd
;
776 case bfd_link_hash_defined
:
777 case bfd_link_hash_defweak
:
778 oldbfd
= h
->root
.u
.def
.section
->owner
;
779 oldsec
= h
->root
.u
.def
.section
;
782 case bfd_link_hash_common
:
783 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
784 oldsec
= h
->root
.u
.c
.p
->section
;
788 /* In cases involving weak versioned symbols, we may wind up trying
789 to merge a symbol with itself. Catch that here, to avoid the
790 confusion that results if we try to override a symbol with
791 itself. The additional tests catch cases like
792 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
793 dynamic object, which we do want to handle here. */
795 && ((abfd
->flags
& DYNAMIC
) == 0
799 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
800 respectively, is from a dynamic object. */
802 if ((abfd
->flags
& DYNAMIC
) != 0)
808 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
813 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
814 indices used by MIPS ELF. */
815 switch (h
->root
.type
)
821 case bfd_link_hash_defined
:
822 case bfd_link_hash_defweak
:
823 hsec
= h
->root
.u
.def
.section
;
826 case bfd_link_hash_common
:
827 hsec
= h
->root
.u
.c
.p
->section
;
834 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
837 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
838 respectively, appear to be a definition rather than reference. */
840 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
845 if (h
->root
.type
== bfd_link_hash_undefined
846 || h
->root
.type
== bfd_link_hash_undefweak
847 || h
->root
.type
== bfd_link_hash_common
)
852 /* Check TLS symbol. */
853 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
854 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
857 bfd_boolean ntdef
, tdef
;
858 asection
*ntsec
, *tsec
;
860 if (h
->type
== STT_TLS
)
880 (*_bfd_error_handler
)
881 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
882 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
883 else if (!tdef
&& !ntdef
)
884 (*_bfd_error_handler
)
885 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
886 tbfd
, ntbfd
, h
->root
.root
.string
);
888 (*_bfd_error_handler
)
889 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
890 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
892 (*_bfd_error_handler
)
893 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
894 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
896 bfd_set_error (bfd_error_bad_value
);
900 /* We need to remember if a symbol has a definition in a dynamic
901 object or is weak in all dynamic objects. Internal and hidden
902 visibility will make it unavailable to dynamic objects. */
903 if (newdyn
&& !h
->dynamic_def
)
905 if (!bfd_is_und_section (sec
))
909 /* Check if this symbol is weak in all dynamic objects. If it
910 is the first time we see it in a dynamic object, we mark
911 if it is weak. Otherwise, we clear it. */
914 if (bind
== STB_WEAK
)
917 else if (bind
!= STB_WEAK
)
922 /* If the old symbol has non-default visibility, we ignore the new
923 definition from a dynamic object. */
925 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
926 && !bfd_is_und_section (sec
))
929 /* Make sure this symbol is dynamic. */
931 /* A protected symbol has external availability. Make sure it is
934 FIXME: Should we check type and size for protected symbol? */
935 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
936 return bfd_elf_link_record_dynamic_symbol (info
, h
);
941 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
944 /* If the new symbol with non-default visibility comes from a
945 relocatable file and the old definition comes from a dynamic
946 object, we remove the old definition. */
947 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
950 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
951 && bfd_is_und_section (sec
))
953 /* If the new symbol is undefined and the old symbol was
954 also undefined before, we need to make sure
955 _bfd_generic_link_add_one_symbol doesn't mess
956 up the linker hash table undefs list. Since the old
957 definition came from a dynamic object, it is still on the
959 h
->root
.type
= bfd_link_hash_undefined
;
960 h
->root
.u
.undef
.abfd
= abfd
;
964 h
->root
.type
= bfd_link_hash_new
;
965 h
->root
.u
.undef
.abfd
= NULL
;
974 /* FIXME: Should we check type and size for protected symbol? */
980 /* Differentiate strong and weak symbols. */
981 newweak
= bind
== STB_WEAK
;
982 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
983 || h
->root
.type
== bfd_link_hash_undefweak
);
985 /* If a new weak symbol definition comes from a regular file and the
986 old symbol comes from a dynamic library, we treat the new one as
987 strong. Similarly, an old weak symbol definition from a regular
988 file is treated as strong when the new symbol comes from a dynamic
989 library. Further, an old weak symbol from a dynamic library is
990 treated as strong if the new symbol is from a dynamic library.
991 This reflects the way glibc's ld.so works.
993 Do this before setting *type_change_ok or *size_change_ok so that
994 we warn properly when dynamic library symbols are overridden. */
996 if (newdef
&& !newdyn
&& olddyn
)
998 if (olddef
&& newdyn
)
1001 /* It's OK to change the type if either the existing symbol or the
1002 new symbol is weak. A type change is also OK if the old symbol
1003 is undefined and the new symbol is defined. */
1008 && h
->root
.type
== bfd_link_hash_undefined
))
1009 *type_change_ok
= TRUE
;
1011 /* It's OK to change the size if either the existing symbol or the
1012 new symbol is weak, or if the old symbol is undefined. */
1015 || h
->root
.type
== bfd_link_hash_undefined
)
1016 *size_change_ok
= TRUE
;
1018 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1019 symbol, respectively, appears to be a common symbol in a dynamic
1020 object. If a symbol appears in an uninitialized section, and is
1021 not weak, and is not a function, then it may be a common symbol
1022 which was resolved when the dynamic object was created. We want
1023 to treat such symbols specially, because they raise special
1024 considerations when setting the symbol size: if the symbol
1025 appears as a common symbol in a regular object, and the size in
1026 the regular object is larger, we must make sure that we use the
1027 larger size. This problematic case can always be avoided in C,
1028 but it must be handled correctly when using Fortran shared
1031 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1032 likewise for OLDDYNCOMMON and OLDDEF.
1034 Note that this test is just a heuristic, and that it is quite
1035 possible to have an uninitialized symbol in a shared object which
1036 is really a definition, rather than a common symbol. This could
1037 lead to some minor confusion when the symbol really is a common
1038 symbol in some regular object. However, I think it will be
1044 && (sec
->flags
& SEC_ALLOC
) != 0
1045 && (sec
->flags
& SEC_LOAD
) == 0
1047 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1048 newdyncommon
= TRUE
;
1050 newdyncommon
= FALSE
;
1054 && h
->root
.type
== bfd_link_hash_defined
1056 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1057 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1059 && h
->type
!= STT_FUNC
)
1060 olddyncommon
= TRUE
;
1062 olddyncommon
= FALSE
;
1064 /* If both the old and the new symbols look like common symbols in a
1065 dynamic object, set the size of the symbol to the larger of the
1070 && sym
->st_size
!= h
->size
)
1072 /* Since we think we have two common symbols, issue a multiple
1073 common warning if desired. Note that we only warn if the
1074 size is different. If the size is the same, we simply let
1075 the old symbol override the new one as normally happens with
1076 symbols defined in dynamic objects. */
1078 if (! ((*info
->callbacks
->multiple_common
)
1079 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1080 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1083 if (sym
->st_size
> h
->size
)
1084 h
->size
= sym
->st_size
;
1086 *size_change_ok
= TRUE
;
1089 /* If we are looking at a dynamic object, and we have found a
1090 definition, we need to see if the symbol was already defined by
1091 some other object. If so, we want to use the existing
1092 definition, and we do not want to report a multiple symbol
1093 definition error; we do this by clobbering *PSEC to be
1094 bfd_und_section_ptr.
1096 We treat a common symbol as a definition if the symbol in the
1097 shared library is a function, since common symbols always
1098 represent variables; this can cause confusion in principle, but
1099 any such confusion would seem to indicate an erroneous program or
1100 shared library. We also permit a common symbol in a regular
1101 object to override a weak symbol in a shared object. */
1106 || (h
->root
.type
== bfd_link_hash_common
1108 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1112 newdyncommon
= FALSE
;
1114 *psec
= sec
= bfd_und_section_ptr
;
1115 *size_change_ok
= TRUE
;
1117 /* If we get here when the old symbol is a common symbol, then
1118 we are explicitly letting it override a weak symbol or
1119 function in a dynamic object, and we don't want to warn about
1120 a type change. If the old symbol is a defined symbol, a type
1121 change warning may still be appropriate. */
1123 if (h
->root
.type
== bfd_link_hash_common
)
1124 *type_change_ok
= TRUE
;
1127 /* Handle the special case of an old common symbol merging with a
1128 new symbol which looks like a common symbol in a shared object.
1129 We change *PSEC and *PVALUE to make the new symbol look like a
1130 common symbol, and let _bfd_generic_link_add_one_symbol will do
1134 && h
->root
.type
== bfd_link_hash_common
)
1138 newdyncommon
= FALSE
;
1139 *pvalue
= sym
->st_size
;
1140 *psec
= sec
= bfd_com_section_ptr
;
1141 *size_change_ok
= TRUE
;
1144 /* If the old symbol is from a dynamic object, and the new symbol is
1145 a definition which is not from a dynamic object, then the new
1146 symbol overrides the old symbol. Symbols from regular files
1147 always take precedence over symbols from dynamic objects, even if
1148 they are defined after the dynamic object in the link.
1150 As above, we again permit a common symbol in a regular object to
1151 override a definition in a shared object if the shared object
1152 symbol is a function or is weak. */
1157 || (bfd_is_com_section (sec
)
1159 || h
->type
== STT_FUNC
)))
1164 /* Change the hash table entry to undefined, and let
1165 _bfd_generic_link_add_one_symbol do the right thing with the
1168 h
->root
.type
= bfd_link_hash_undefined
;
1169 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1170 *size_change_ok
= TRUE
;
1173 olddyncommon
= FALSE
;
1175 /* We again permit a type change when a common symbol may be
1176 overriding a function. */
1178 if (bfd_is_com_section (sec
))
1179 *type_change_ok
= TRUE
;
1181 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1184 /* This union may have been set to be non-NULL when this symbol
1185 was seen in a dynamic object. We must force the union to be
1186 NULL, so that it is correct for a regular symbol. */
1187 h
->verinfo
.vertree
= NULL
;
1190 /* Handle the special case of a new common symbol merging with an
1191 old symbol that looks like it might be a common symbol defined in
1192 a shared object. Note that we have already handled the case in
1193 which a new common symbol should simply override the definition
1194 in the shared library. */
1197 && bfd_is_com_section (sec
)
1200 /* It would be best if we could set the hash table entry to a
1201 common symbol, but we don't know what to use for the section
1202 or the alignment. */
1203 if (! ((*info
->callbacks
->multiple_common
)
1204 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1205 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1208 /* If the presumed common symbol in the dynamic object is
1209 larger, pretend that the new symbol has its size. */
1211 if (h
->size
> *pvalue
)
1214 /* FIXME: We no longer know the alignment required by the symbol
1215 in the dynamic object, so we just wind up using the one from
1216 the regular object. */
1219 olddyncommon
= FALSE
;
1221 h
->root
.type
= bfd_link_hash_undefined
;
1222 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1224 *size_change_ok
= TRUE
;
1225 *type_change_ok
= TRUE
;
1227 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1230 h
->verinfo
.vertree
= NULL
;
1235 /* Handle the case where we had a versioned symbol in a dynamic
1236 library and now find a definition in a normal object. In this
1237 case, we make the versioned symbol point to the normal one. */
1238 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1239 flip
->root
.type
= h
->root
.type
;
1240 h
->root
.type
= bfd_link_hash_indirect
;
1241 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1242 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1243 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1247 flip
->ref_dynamic
= 1;
1254 /* This function is called to create an indirect symbol from the
1255 default for the symbol with the default version if needed. The
1256 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1257 set DYNSYM if the new indirect symbol is dynamic. */
1260 _bfd_elf_add_default_symbol (bfd
*abfd
,
1261 struct bfd_link_info
*info
,
1262 struct elf_link_hash_entry
*h
,
1264 Elf_Internal_Sym
*sym
,
1267 bfd_boolean
*dynsym
,
1268 bfd_boolean override
)
1270 bfd_boolean type_change_ok
;
1271 bfd_boolean size_change_ok
;
1274 struct elf_link_hash_entry
*hi
;
1275 struct bfd_link_hash_entry
*bh
;
1276 const struct elf_backend_data
*bed
;
1277 bfd_boolean collect
;
1278 bfd_boolean dynamic
;
1280 size_t len
, shortlen
;
1283 /* If this symbol has a version, and it is the default version, we
1284 create an indirect symbol from the default name to the fully
1285 decorated name. This will cause external references which do not
1286 specify a version to be bound to this version of the symbol. */
1287 p
= strchr (name
, ELF_VER_CHR
);
1288 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1293 /* We are overridden by an old definition. We need to check if we
1294 need to create the indirect symbol from the default name. */
1295 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1297 BFD_ASSERT (hi
!= NULL
);
1300 while (hi
->root
.type
== bfd_link_hash_indirect
1301 || hi
->root
.type
== bfd_link_hash_warning
)
1303 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1309 bed
= get_elf_backend_data (abfd
);
1310 collect
= bed
->collect
;
1311 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1313 shortlen
= p
- name
;
1314 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1315 if (shortname
== NULL
)
1317 memcpy (shortname
, name
, shortlen
);
1318 shortname
[shortlen
] = '\0';
1320 /* We are going to create a new symbol. Merge it with any existing
1321 symbol with this name. For the purposes of the merge, act as
1322 though we were defining the symbol we just defined, although we
1323 actually going to define an indirect symbol. */
1324 type_change_ok
= FALSE
;
1325 size_change_ok
= FALSE
;
1327 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1328 &hi
, &skip
, &override
, &type_change_ok
,
1338 if (! (_bfd_generic_link_add_one_symbol
1339 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1340 0, name
, FALSE
, collect
, &bh
)))
1342 hi
= (struct elf_link_hash_entry
*) bh
;
1346 /* In this case the symbol named SHORTNAME is overriding the
1347 indirect symbol we want to add. We were planning on making
1348 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1349 is the name without a version. NAME is the fully versioned
1350 name, and it is the default version.
1352 Overriding means that we already saw a definition for the
1353 symbol SHORTNAME in a regular object, and it is overriding
1354 the symbol defined in the dynamic object.
1356 When this happens, we actually want to change NAME, the
1357 symbol we just added, to refer to SHORTNAME. This will cause
1358 references to NAME in the shared object to become references
1359 to SHORTNAME in the regular object. This is what we expect
1360 when we override a function in a shared object: that the
1361 references in the shared object will be mapped to the
1362 definition in the regular object. */
1364 while (hi
->root
.type
== bfd_link_hash_indirect
1365 || hi
->root
.type
== bfd_link_hash_warning
)
1366 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1368 h
->root
.type
= bfd_link_hash_indirect
;
1369 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1373 hi
->ref_dynamic
= 1;
1377 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1382 /* Now set HI to H, so that the following code will set the
1383 other fields correctly. */
1387 /* If there is a duplicate definition somewhere, then HI may not
1388 point to an indirect symbol. We will have reported an error to
1389 the user in that case. */
1391 if (hi
->root
.type
== bfd_link_hash_indirect
)
1393 struct elf_link_hash_entry
*ht
;
1395 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1396 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1398 /* See if the new flags lead us to realize that the symbol must
1410 if (hi
->ref_regular
)
1416 /* We also need to define an indirection from the nondefault version
1420 len
= strlen (name
);
1421 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1422 if (shortname
== NULL
)
1424 memcpy (shortname
, name
, shortlen
);
1425 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1427 /* Once again, merge with any existing symbol. */
1428 type_change_ok
= FALSE
;
1429 size_change_ok
= FALSE
;
1431 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1432 &hi
, &skip
, &override
, &type_change_ok
,
1441 /* Here SHORTNAME is a versioned name, so we don't expect to see
1442 the type of override we do in the case above unless it is
1443 overridden by a versioned definition. */
1444 if (hi
->root
.type
!= bfd_link_hash_defined
1445 && hi
->root
.type
!= bfd_link_hash_defweak
)
1446 (*_bfd_error_handler
)
1447 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1453 if (! (_bfd_generic_link_add_one_symbol
1454 (info
, abfd
, shortname
, BSF_INDIRECT
,
1455 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1457 hi
= (struct elf_link_hash_entry
*) bh
;
1459 /* If there is a duplicate definition somewhere, then HI may not
1460 point to an indirect symbol. We will have reported an error
1461 to the user in that case. */
1463 if (hi
->root
.type
== bfd_link_hash_indirect
)
1465 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1467 /* See if the new flags lead us to realize that the symbol
1479 if (hi
->ref_regular
)
1489 /* This routine is used to export all defined symbols into the dynamic
1490 symbol table. It is called via elf_link_hash_traverse. */
1493 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1495 struct elf_info_failed
*eif
= data
;
1497 /* Ignore indirect symbols. These are added by the versioning code. */
1498 if (h
->root
.type
== bfd_link_hash_indirect
)
1501 if (h
->root
.type
== bfd_link_hash_warning
)
1502 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1504 if (h
->dynindx
== -1
1508 struct bfd_elf_version_tree
*t
;
1509 struct bfd_elf_version_expr
*d
;
1511 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1513 if (t
->globals
.list
!= NULL
)
1515 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1520 if (t
->locals
.list
!= NULL
)
1522 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1531 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1542 /* Look through the symbols which are defined in other shared
1543 libraries and referenced here. Update the list of version
1544 dependencies. This will be put into the .gnu.version_r section.
1545 This function is called via elf_link_hash_traverse. */
1548 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1551 struct elf_find_verdep_info
*rinfo
= data
;
1552 Elf_Internal_Verneed
*t
;
1553 Elf_Internal_Vernaux
*a
;
1556 if (h
->root
.type
== bfd_link_hash_warning
)
1557 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1559 /* We only care about symbols defined in shared objects with version
1564 || h
->verinfo
.verdef
== NULL
)
1567 /* See if we already know about this version. */
1568 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1570 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1573 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1574 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1580 /* This is a new version. Add it to tree we are building. */
1585 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1588 rinfo
->failed
= TRUE
;
1592 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1593 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1594 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1598 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1600 /* Note that we are copying a string pointer here, and testing it
1601 above. If bfd_elf_string_from_elf_section is ever changed to
1602 discard the string data when low in memory, this will have to be
1604 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1606 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1607 a
->vna_nextptr
= t
->vn_auxptr
;
1609 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1612 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1619 /* Figure out appropriate versions for all the symbols. We may not
1620 have the version number script until we have read all of the input
1621 files, so until that point we don't know which symbols should be
1622 local. This function is called via elf_link_hash_traverse. */
1625 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1627 struct elf_assign_sym_version_info
*sinfo
;
1628 struct bfd_link_info
*info
;
1629 const struct elf_backend_data
*bed
;
1630 struct elf_info_failed eif
;
1637 if (h
->root
.type
== bfd_link_hash_warning
)
1638 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1640 /* Fix the symbol flags. */
1643 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1646 sinfo
->failed
= TRUE
;
1650 /* We only need version numbers for symbols defined in regular
1652 if (!h
->def_regular
)
1655 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1656 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1657 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1659 struct bfd_elf_version_tree
*t
;
1664 /* There are two consecutive ELF_VER_CHR characters if this is
1665 not a hidden symbol. */
1667 if (*p
== ELF_VER_CHR
)
1673 /* If there is no version string, we can just return out. */
1681 /* Look for the version. If we find it, it is no longer weak. */
1682 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1684 if (strcmp (t
->name
, p
) == 0)
1688 struct bfd_elf_version_expr
*d
;
1690 len
= p
- h
->root
.root
.string
;
1691 alc
= bfd_malloc (len
);
1694 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1695 alc
[len
- 1] = '\0';
1696 if (alc
[len
- 2] == ELF_VER_CHR
)
1697 alc
[len
- 2] = '\0';
1699 h
->verinfo
.vertree
= t
;
1703 if (t
->globals
.list
!= NULL
)
1704 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1706 /* See if there is anything to force this symbol to
1708 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1710 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1714 && ! info
->export_dynamic
)
1715 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1723 /* If we are building an application, we need to create a
1724 version node for this version. */
1725 if (t
== NULL
&& info
->executable
)
1727 struct bfd_elf_version_tree
**pp
;
1730 /* If we aren't going to export this symbol, we don't need
1731 to worry about it. */
1732 if (h
->dynindx
== -1)
1736 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1739 sinfo
->failed
= TRUE
;
1744 t
->name_indx
= (unsigned int) -1;
1748 /* Don't count anonymous version tag. */
1749 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1751 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1753 t
->vernum
= version_index
;
1757 h
->verinfo
.vertree
= t
;
1761 /* We could not find the version for a symbol when
1762 generating a shared archive. Return an error. */
1763 (*_bfd_error_handler
)
1764 (_("%B: undefined versioned symbol name %s"),
1765 sinfo
->output_bfd
, h
->root
.root
.string
);
1766 bfd_set_error (bfd_error_bad_value
);
1767 sinfo
->failed
= TRUE
;
1775 /* If we don't have a version for this symbol, see if we can find
1777 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1779 struct bfd_elf_version_tree
*t
;
1780 struct bfd_elf_version_tree
*local_ver
;
1781 struct bfd_elf_version_expr
*d
;
1783 /* See if can find what version this symbol is in. If the
1784 symbol is supposed to be local, then don't actually register
1787 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1789 if (t
->globals
.list
!= NULL
)
1791 bfd_boolean matched
;
1795 while ((d
= (*t
->match
) (&t
->globals
, d
,
1796 h
->root
.root
.string
)) != NULL
)
1801 /* There is a version without definition. Make
1802 the symbol the default definition for this
1804 h
->verinfo
.vertree
= t
;
1812 /* There is no undefined version for this symbol. Hide the
1814 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1817 if (t
->locals
.list
!= NULL
)
1820 while ((d
= (*t
->match
) (&t
->locals
, d
,
1821 h
->root
.root
.string
)) != NULL
)
1824 /* If the match is "*", keep looking for a more
1825 explicit, perhaps even global, match.
1826 XXX: Shouldn't this be !d->wildcard instead? */
1827 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1836 if (local_ver
!= NULL
)
1838 h
->verinfo
.vertree
= local_ver
;
1839 if (h
->dynindx
!= -1
1841 && ! info
->export_dynamic
)
1843 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1851 /* Read and swap the relocs from the section indicated by SHDR. This
1852 may be either a REL or a RELA section. The relocations are
1853 translated into RELA relocations and stored in INTERNAL_RELOCS,
1854 which should have already been allocated to contain enough space.
1855 The EXTERNAL_RELOCS are a buffer where the external form of the
1856 relocations should be stored.
1858 Returns FALSE if something goes wrong. */
1861 elf_link_read_relocs_from_section (bfd
*abfd
,
1863 Elf_Internal_Shdr
*shdr
,
1864 void *external_relocs
,
1865 Elf_Internal_Rela
*internal_relocs
)
1867 const struct elf_backend_data
*bed
;
1868 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1869 const bfd_byte
*erela
;
1870 const bfd_byte
*erelaend
;
1871 Elf_Internal_Rela
*irela
;
1872 Elf_Internal_Shdr
*symtab_hdr
;
1875 /* Position ourselves at the start of the section. */
1876 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1879 /* Read the relocations. */
1880 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1883 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1884 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1886 bed
= get_elf_backend_data (abfd
);
1888 /* Convert the external relocations to the internal format. */
1889 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1890 swap_in
= bed
->s
->swap_reloc_in
;
1891 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1892 swap_in
= bed
->s
->swap_reloca_in
;
1895 bfd_set_error (bfd_error_wrong_format
);
1899 erela
= external_relocs
;
1900 erelaend
= erela
+ shdr
->sh_size
;
1901 irela
= internal_relocs
;
1902 while (erela
< erelaend
)
1906 (*swap_in
) (abfd
, erela
, irela
);
1907 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1908 if (bed
->s
->arch_size
== 64)
1910 if ((size_t) r_symndx
>= nsyms
)
1912 (*_bfd_error_handler
)
1913 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1914 " for offset 0x%lx in section `%A'"),
1916 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1917 bfd_set_error (bfd_error_bad_value
);
1920 irela
+= bed
->s
->int_rels_per_ext_rel
;
1921 erela
+= shdr
->sh_entsize
;
1927 /* Read and swap the relocs for a section O. They may have been
1928 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1929 not NULL, they are used as buffers to read into. They are known to
1930 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1931 the return value is allocated using either malloc or bfd_alloc,
1932 according to the KEEP_MEMORY argument. If O has two relocation
1933 sections (both REL and RELA relocations), then the REL_HDR
1934 relocations will appear first in INTERNAL_RELOCS, followed by the
1935 REL_HDR2 relocations. */
1938 _bfd_elf_link_read_relocs (bfd
*abfd
,
1940 void *external_relocs
,
1941 Elf_Internal_Rela
*internal_relocs
,
1942 bfd_boolean keep_memory
)
1944 Elf_Internal_Shdr
*rel_hdr
;
1945 void *alloc1
= NULL
;
1946 Elf_Internal_Rela
*alloc2
= NULL
;
1947 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1949 if (elf_section_data (o
)->relocs
!= NULL
)
1950 return elf_section_data (o
)->relocs
;
1952 if (o
->reloc_count
== 0)
1955 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1957 if (internal_relocs
== NULL
)
1961 size
= o
->reloc_count
;
1962 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1964 internal_relocs
= bfd_alloc (abfd
, size
);
1966 internal_relocs
= alloc2
= bfd_malloc (size
);
1967 if (internal_relocs
== NULL
)
1971 if (external_relocs
== NULL
)
1973 bfd_size_type size
= rel_hdr
->sh_size
;
1975 if (elf_section_data (o
)->rel_hdr2
)
1976 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1977 alloc1
= bfd_malloc (size
);
1980 external_relocs
= alloc1
;
1983 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1987 if (elf_section_data (o
)->rel_hdr2
1988 && (!elf_link_read_relocs_from_section
1990 elf_section_data (o
)->rel_hdr2
,
1991 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
1992 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
1993 * bed
->s
->int_rels_per_ext_rel
))))
1996 /* Cache the results for next time, if we can. */
1998 elf_section_data (o
)->relocs
= internal_relocs
;
2003 /* Don't free alloc2, since if it was allocated we are passing it
2004 back (under the name of internal_relocs). */
2006 return internal_relocs
;
2016 /* Compute the size of, and allocate space for, REL_HDR which is the
2017 section header for a section containing relocations for O. */
2020 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2021 Elf_Internal_Shdr
*rel_hdr
,
2024 bfd_size_type reloc_count
;
2025 bfd_size_type num_rel_hashes
;
2027 /* Figure out how many relocations there will be. */
2028 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2029 reloc_count
= elf_section_data (o
)->rel_count
;
2031 reloc_count
= elf_section_data (o
)->rel_count2
;
2033 num_rel_hashes
= o
->reloc_count
;
2034 if (num_rel_hashes
< reloc_count
)
2035 num_rel_hashes
= reloc_count
;
2037 /* That allows us to calculate the size of the section. */
2038 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2040 /* The contents field must last into write_object_contents, so we
2041 allocate it with bfd_alloc rather than malloc. Also since we
2042 cannot be sure that the contents will actually be filled in,
2043 we zero the allocated space. */
2044 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2045 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2048 /* We only allocate one set of hash entries, so we only do it the
2049 first time we are called. */
2050 if (elf_section_data (o
)->rel_hashes
== NULL
2053 struct elf_link_hash_entry
**p
;
2055 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2059 elf_section_data (o
)->rel_hashes
= p
;
2065 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2066 originated from the section given by INPUT_REL_HDR) to the
2070 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2071 asection
*input_section
,
2072 Elf_Internal_Shdr
*input_rel_hdr
,
2073 Elf_Internal_Rela
*internal_relocs
)
2075 Elf_Internal_Rela
*irela
;
2076 Elf_Internal_Rela
*irelaend
;
2078 Elf_Internal_Shdr
*output_rel_hdr
;
2079 asection
*output_section
;
2080 unsigned int *rel_countp
= NULL
;
2081 const struct elf_backend_data
*bed
;
2082 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2084 output_section
= input_section
->output_section
;
2085 output_rel_hdr
= NULL
;
2087 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2088 == input_rel_hdr
->sh_entsize
)
2090 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2091 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2093 else if (elf_section_data (output_section
)->rel_hdr2
2094 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2095 == input_rel_hdr
->sh_entsize
))
2097 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2098 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2102 (*_bfd_error_handler
)
2103 (_("%B: relocation size mismatch in %B section %A"),
2104 output_bfd
, input_section
->owner
, input_section
);
2105 bfd_set_error (bfd_error_wrong_object_format
);
2109 bed
= get_elf_backend_data (output_bfd
);
2110 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2111 swap_out
= bed
->s
->swap_reloc_out
;
2112 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2113 swap_out
= bed
->s
->swap_reloca_out
;
2117 erel
= output_rel_hdr
->contents
;
2118 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2119 irela
= internal_relocs
;
2120 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2121 * bed
->s
->int_rels_per_ext_rel
);
2122 while (irela
< irelaend
)
2124 (*swap_out
) (output_bfd
, irela
, erel
);
2125 irela
+= bed
->s
->int_rels_per_ext_rel
;
2126 erel
+= input_rel_hdr
->sh_entsize
;
2129 /* Bump the counter, so that we know where to add the next set of
2131 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2136 /* Fix up the flags for a symbol. This handles various cases which
2137 can only be fixed after all the input files are seen. This is
2138 currently called by both adjust_dynamic_symbol and
2139 assign_sym_version, which is unnecessary but perhaps more robust in
2140 the face of future changes. */
2143 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2144 struct elf_info_failed
*eif
)
2146 /* If this symbol was mentioned in a non-ELF file, try to set
2147 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2148 permit a non-ELF file to correctly refer to a symbol defined in
2149 an ELF dynamic object. */
2152 while (h
->root
.type
== bfd_link_hash_indirect
)
2153 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2155 if (h
->root
.type
!= bfd_link_hash_defined
2156 && h
->root
.type
!= bfd_link_hash_defweak
)
2159 h
->ref_regular_nonweak
= 1;
2163 if (h
->root
.u
.def
.section
->owner
!= NULL
2164 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2165 == bfd_target_elf_flavour
))
2168 h
->ref_regular_nonweak
= 1;
2174 if (h
->dynindx
== -1
2178 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2187 /* Unfortunately, NON_ELF is only correct if the symbol
2188 was first seen in a non-ELF file. Fortunately, if the symbol
2189 was first seen in an ELF file, we're probably OK unless the
2190 symbol was defined in a non-ELF file. Catch that case here.
2191 FIXME: We're still in trouble if the symbol was first seen in
2192 a dynamic object, and then later in a non-ELF regular object. */
2193 if ((h
->root
.type
== bfd_link_hash_defined
2194 || h
->root
.type
== bfd_link_hash_defweak
)
2196 && (h
->root
.u
.def
.section
->owner
!= NULL
2197 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2198 != bfd_target_elf_flavour
)
2199 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2200 && !h
->def_dynamic
)))
2204 /* If this is a final link, and the symbol was defined as a common
2205 symbol in a regular object file, and there was no definition in
2206 any dynamic object, then the linker will have allocated space for
2207 the symbol in a common section but the DEF_REGULAR
2208 flag will not have been set. */
2209 if (h
->root
.type
== bfd_link_hash_defined
2213 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2216 /* If -Bsymbolic was used (which means to bind references to global
2217 symbols to the definition within the shared object), and this
2218 symbol was defined in a regular object, then it actually doesn't
2219 need a PLT entry. Likewise, if the symbol has non-default
2220 visibility. If the symbol has hidden or internal visibility, we
2221 will force it local. */
2223 && eif
->info
->shared
2224 && is_elf_hash_table (eif
->info
->hash
)
2225 && (eif
->info
->symbolic
2226 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2229 const struct elf_backend_data
*bed
;
2230 bfd_boolean force_local
;
2232 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2234 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2235 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2236 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2239 /* If a weak undefined symbol has non-default visibility, we also
2240 hide it from the dynamic linker. */
2241 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2242 && h
->root
.type
== bfd_link_hash_undefweak
)
2244 const struct elf_backend_data
*bed
;
2245 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2246 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2249 /* If this is a weak defined symbol in a dynamic object, and we know
2250 the real definition in the dynamic object, copy interesting flags
2251 over to the real definition. */
2252 if (h
->u
.weakdef
!= NULL
)
2254 struct elf_link_hash_entry
*weakdef
;
2256 weakdef
= h
->u
.weakdef
;
2257 if (h
->root
.type
== bfd_link_hash_indirect
)
2258 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2260 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2261 || h
->root
.type
== bfd_link_hash_defweak
);
2262 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2263 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2264 BFD_ASSERT (weakdef
->def_dynamic
);
2266 /* If the real definition is defined by a regular object file,
2267 don't do anything special. See the longer description in
2268 _bfd_elf_adjust_dynamic_symbol, below. */
2269 if (weakdef
->def_regular
)
2270 h
->u
.weakdef
= NULL
;
2273 const struct elf_backend_data
*bed
;
2275 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2276 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2283 /* Make the backend pick a good value for a dynamic symbol. This is
2284 called via elf_link_hash_traverse, and also calls itself
2288 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2290 struct elf_info_failed
*eif
= data
;
2292 const struct elf_backend_data
*bed
;
2294 if (! is_elf_hash_table (eif
->info
->hash
))
2297 if (h
->root
.type
== bfd_link_hash_warning
)
2299 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2300 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2302 /* When warning symbols are created, they **replace** the "real"
2303 entry in the hash table, thus we never get to see the real
2304 symbol in a hash traversal. So look at it now. */
2305 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2308 /* Ignore indirect symbols. These are added by the versioning code. */
2309 if (h
->root
.type
== bfd_link_hash_indirect
)
2312 /* Fix the symbol flags. */
2313 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2316 /* If this symbol does not require a PLT entry, and it is not
2317 defined by a dynamic object, or is not referenced by a regular
2318 object, ignore it. We do have to handle a weak defined symbol,
2319 even if no regular object refers to it, if we decided to add it
2320 to the dynamic symbol table. FIXME: Do we normally need to worry
2321 about symbols which are defined by one dynamic object and
2322 referenced by another one? */
2327 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2329 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2333 /* If we've already adjusted this symbol, don't do it again. This
2334 can happen via a recursive call. */
2335 if (h
->dynamic_adjusted
)
2338 /* Don't look at this symbol again. Note that we must set this
2339 after checking the above conditions, because we may look at a
2340 symbol once, decide not to do anything, and then get called
2341 recursively later after REF_REGULAR is set below. */
2342 h
->dynamic_adjusted
= 1;
2344 /* If this is a weak definition, and we know a real definition, and
2345 the real symbol is not itself defined by a regular object file,
2346 then get a good value for the real definition. We handle the
2347 real symbol first, for the convenience of the backend routine.
2349 Note that there is a confusing case here. If the real definition
2350 is defined by a regular object file, we don't get the real symbol
2351 from the dynamic object, but we do get the weak symbol. If the
2352 processor backend uses a COPY reloc, then if some routine in the
2353 dynamic object changes the real symbol, we will not see that
2354 change in the corresponding weak symbol. This is the way other
2355 ELF linkers work as well, and seems to be a result of the shared
2358 I will clarify this issue. Most SVR4 shared libraries define the
2359 variable _timezone and define timezone as a weak synonym. The
2360 tzset call changes _timezone. If you write
2361 extern int timezone;
2363 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2364 you might expect that, since timezone is a synonym for _timezone,
2365 the same number will print both times. However, if the processor
2366 backend uses a COPY reloc, then actually timezone will be copied
2367 into your process image, and, since you define _timezone
2368 yourself, _timezone will not. Thus timezone and _timezone will
2369 wind up at different memory locations. The tzset call will set
2370 _timezone, leaving timezone unchanged. */
2372 if (h
->u
.weakdef
!= NULL
)
2374 /* If we get to this point, we know there is an implicit
2375 reference by a regular object file via the weak symbol H.
2376 FIXME: Is this really true? What if the traversal finds
2377 H->U.WEAKDEF before it finds H? */
2378 h
->u
.weakdef
->ref_regular
= 1;
2380 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2384 /* If a symbol has no type and no size and does not require a PLT
2385 entry, then we are probably about to do the wrong thing here: we
2386 are probably going to create a COPY reloc for an empty object.
2387 This case can arise when a shared object is built with assembly
2388 code, and the assembly code fails to set the symbol type. */
2390 && h
->type
== STT_NOTYPE
2392 (*_bfd_error_handler
)
2393 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2394 h
->root
.root
.string
);
2396 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2397 bed
= get_elf_backend_data (dynobj
);
2398 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2407 /* Adjust all external symbols pointing into SEC_MERGE sections
2408 to reflect the object merging within the sections. */
2411 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2415 if (h
->root
.type
== bfd_link_hash_warning
)
2416 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2418 if ((h
->root
.type
== bfd_link_hash_defined
2419 || h
->root
.type
== bfd_link_hash_defweak
)
2420 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2421 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2423 bfd
*output_bfd
= data
;
2425 h
->root
.u
.def
.value
=
2426 _bfd_merged_section_offset (output_bfd
,
2427 &h
->root
.u
.def
.section
,
2428 elf_section_data (sec
)->sec_info
,
2429 h
->root
.u
.def
.value
);
2435 /* Returns false if the symbol referred to by H should be considered
2436 to resolve local to the current module, and true if it should be
2437 considered to bind dynamically. */
2440 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2441 struct bfd_link_info
*info
,
2442 bfd_boolean ignore_protected
)
2444 bfd_boolean binding_stays_local_p
;
2449 while (h
->root
.type
== bfd_link_hash_indirect
2450 || h
->root
.type
== bfd_link_hash_warning
)
2451 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2453 /* If it was forced local, then clearly it's not dynamic. */
2454 if (h
->dynindx
== -1)
2456 if (h
->forced_local
)
2459 /* Identify the cases where name binding rules say that a
2460 visible symbol resolves locally. */
2461 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2463 switch (ELF_ST_VISIBILITY (h
->other
))
2470 /* Proper resolution for function pointer equality may require
2471 that these symbols perhaps be resolved dynamically, even though
2472 we should be resolving them to the current module. */
2473 if (!ignore_protected
)
2474 binding_stays_local_p
= TRUE
;
2481 /* If it isn't defined locally, then clearly it's dynamic. */
2482 if (!h
->def_regular
)
2485 /* Otherwise, the symbol is dynamic if binding rules don't tell
2486 us that it remains local. */
2487 return !binding_stays_local_p
;
2490 /* Return true if the symbol referred to by H should be considered
2491 to resolve local to the current module, and false otherwise. Differs
2492 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2493 undefined symbols and weak symbols. */
2496 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2497 struct bfd_link_info
*info
,
2498 bfd_boolean local_protected
)
2500 /* If it's a local sym, of course we resolve locally. */
2504 /* Common symbols that become definitions don't get the DEF_REGULAR
2505 flag set, so test it first, and don't bail out. */
2506 if (ELF_COMMON_DEF_P (h
))
2508 /* If we don't have a definition in a regular file, then we can't
2509 resolve locally. The sym is either undefined or dynamic. */
2510 else if (!h
->def_regular
)
2513 /* Forced local symbols resolve locally. */
2514 if (h
->forced_local
)
2517 /* As do non-dynamic symbols. */
2518 if (h
->dynindx
== -1)
2521 /* At this point, we know the symbol is defined and dynamic. In an
2522 executable it must resolve locally, likewise when building symbolic
2523 shared libraries. */
2524 if (info
->executable
|| info
->symbolic
)
2527 /* Now deal with defined dynamic symbols in shared libraries. Ones
2528 with default visibility might not resolve locally. */
2529 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2532 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2533 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2536 /* Function pointer equality tests may require that STV_PROTECTED
2537 symbols be treated as dynamic symbols, even when we know that the
2538 dynamic linker will resolve them locally. */
2539 return local_protected
;
2542 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2543 aligned. Returns the first TLS output section. */
2545 struct bfd_section
*
2546 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2548 struct bfd_section
*sec
, *tls
;
2549 unsigned int align
= 0;
2551 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2552 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2556 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2557 if (sec
->alignment_power
> align
)
2558 align
= sec
->alignment_power
;
2560 elf_hash_table (info
)->tls_sec
= tls
;
2562 /* Ensure the alignment of the first section is the largest alignment,
2563 so that the tls segment starts aligned. */
2565 tls
->alignment_power
= align
;
2570 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2572 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2573 Elf_Internal_Sym
*sym
)
2575 /* Local symbols do not count, but target specific ones might. */
2576 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2577 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2580 /* Function symbols do not count. */
2581 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2584 /* If the section is undefined, then so is the symbol. */
2585 if (sym
->st_shndx
== SHN_UNDEF
)
2588 /* If the symbol is defined in the common section, then
2589 it is a common definition and so does not count. */
2590 if (sym
->st_shndx
== SHN_COMMON
)
2593 /* If the symbol is in a target specific section then we
2594 must rely upon the backend to tell us what it is. */
2595 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2596 /* FIXME - this function is not coded yet:
2598 return _bfd_is_global_symbol_definition (abfd, sym);
2600 Instead for now assume that the definition is not global,
2601 Even if this is wrong, at least the linker will behave
2602 in the same way that it used to do. */
2608 /* Search the symbol table of the archive element of the archive ABFD
2609 whose archive map contains a mention of SYMDEF, and determine if
2610 the symbol is defined in this element. */
2612 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2614 Elf_Internal_Shdr
* hdr
;
2615 bfd_size_type symcount
;
2616 bfd_size_type extsymcount
;
2617 bfd_size_type extsymoff
;
2618 Elf_Internal_Sym
*isymbuf
;
2619 Elf_Internal_Sym
*isym
;
2620 Elf_Internal_Sym
*isymend
;
2623 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2627 if (! bfd_check_format (abfd
, bfd_object
))
2630 /* If we have already included the element containing this symbol in the
2631 link then we do not need to include it again. Just claim that any symbol
2632 it contains is not a definition, so that our caller will not decide to
2633 (re)include this element. */
2634 if (abfd
->archive_pass
)
2637 /* Select the appropriate symbol table. */
2638 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2639 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2641 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2643 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2645 /* The sh_info field of the symtab header tells us where the
2646 external symbols start. We don't care about the local symbols. */
2647 if (elf_bad_symtab (abfd
))
2649 extsymcount
= symcount
;
2654 extsymcount
= symcount
- hdr
->sh_info
;
2655 extsymoff
= hdr
->sh_info
;
2658 if (extsymcount
== 0)
2661 /* Read in the symbol table. */
2662 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2664 if (isymbuf
== NULL
)
2667 /* Scan the symbol table looking for SYMDEF. */
2669 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2673 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2678 if (strcmp (name
, symdef
->name
) == 0)
2680 result
= is_global_data_symbol_definition (abfd
, isym
);
2690 /* Add an entry to the .dynamic table. */
2693 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2697 struct elf_link_hash_table
*hash_table
;
2698 const struct elf_backend_data
*bed
;
2700 bfd_size_type newsize
;
2701 bfd_byte
*newcontents
;
2702 Elf_Internal_Dyn dyn
;
2704 hash_table
= elf_hash_table (info
);
2705 if (! is_elf_hash_table (hash_table
))
2708 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2710 (_("warning: creating a DT_TEXTREL in a shared object."));
2712 bed
= get_elf_backend_data (hash_table
->dynobj
);
2713 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2714 BFD_ASSERT (s
!= NULL
);
2716 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2717 newcontents
= bfd_realloc (s
->contents
, newsize
);
2718 if (newcontents
== NULL
)
2722 dyn
.d_un
.d_val
= val
;
2723 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2726 s
->contents
= newcontents
;
2731 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2732 otherwise just check whether one already exists. Returns -1 on error,
2733 1 if a DT_NEEDED tag already exists, and 0 on success. */
2736 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2740 struct elf_link_hash_table
*hash_table
;
2741 bfd_size_type oldsize
;
2742 bfd_size_type strindex
;
2744 hash_table
= elf_hash_table (info
);
2745 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2746 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2747 if (strindex
== (bfd_size_type
) -1)
2750 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2753 const struct elf_backend_data
*bed
;
2756 bed
= get_elf_backend_data (hash_table
->dynobj
);
2757 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2758 BFD_ASSERT (sdyn
!= NULL
);
2760 for (extdyn
= sdyn
->contents
;
2761 extdyn
< sdyn
->contents
+ sdyn
->size
;
2762 extdyn
+= bed
->s
->sizeof_dyn
)
2764 Elf_Internal_Dyn dyn
;
2766 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2767 if (dyn
.d_tag
== DT_NEEDED
2768 && dyn
.d_un
.d_val
== strindex
)
2770 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2778 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2782 /* We were just checking for existence of the tag. */
2783 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2788 /* Sort symbol by value and section. */
2790 elf_sort_symbol (const void *arg1
, const void *arg2
)
2792 const struct elf_link_hash_entry
*h1
;
2793 const struct elf_link_hash_entry
*h2
;
2794 bfd_signed_vma vdiff
;
2796 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2797 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2798 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2800 return vdiff
> 0 ? 1 : -1;
2803 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2805 return sdiff
> 0 ? 1 : -1;
2810 /* This function is used to adjust offsets into .dynstr for
2811 dynamic symbols. This is called via elf_link_hash_traverse. */
2814 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2816 struct elf_strtab_hash
*dynstr
= data
;
2818 if (h
->root
.type
== bfd_link_hash_warning
)
2819 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2821 if (h
->dynindx
!= -1)
2822 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2826 /* Assign string offsets in .dynstr, update all structures referencing
2830 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2832 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2833 struct elf_link_local_dynamic_entry
*entry
;
2834 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2835 bfd
*dynobj
= hash_table
->dynobj
;
2838 const struct elf_backend_data
*bed
;
2841 _bfd_elf_strtab_finalize (dynstr
);
2842 size
= _bfd_elf_strtab_size (dynstr
);
2844 bed
= get_elf_backend_data (dynobj
);
2845 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2846 BFD_ASSERT (sdyn
!= NULL
);
2848 /* Update all .dynamic entries referencing .dynstr strings. */
2849 for (extdyn
= sdyn
->contents
;
2850 extdyn
< sdyn
->contents
+ sdyn
->size
;
2851 extdyn
+= bed
->s
->sizeof_dyn
)
2853 Elf_Internal_Dyn dyn
;
2855 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2859 dyn
.d_un
.d_val
= size
;
2867 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2872 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2875 /* Now update local dynamic symbols. */
2876 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2877 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2878 entry
->isym
.st_name
);
2880 /* And the rest of dynamic symbols. */
2881 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2883 /* Adjust version definitions. */
2884 if (elf_tdata (output_bfd
)->cverdefs
)
2889 Elf_Internal_Verdef def
;
2890 Elf_Internal_Verdaux defaux
;
2892 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2896 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2898 p
+= sizeof (Elf_External_Verdef
);
2899 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
2901 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2903 _bfd_elf_swap_verdaux_in (output_bfd
,
2904 (Elf_External_Verdaux
*) p
, &defaux
);
2905 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2907 _bfd_elf_swap_verdaux_out (output_bfd
,
2908 &defaux
, (Elf_External_Verdaux
*) p
);
2909 p
+= sizeof (Elf_External_Verdaux
);
2912 while (def
.vd_next
);
2915 /* Adjust version references. */
2916 if (elf_tdata (output_bfd
)->verref
)
2921 Elf_Internal_Verneed need
;
2922 Elf_Internal_Vernaux needaux
;
2924 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2928 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2930 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2931 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2932 (Elf_External_Verneed
*) p
);
2933 p
+= sizeof (Elf_External_Verneed
);
2934 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2936 _bfd_elf_swap_vernaux_in (output_bfd
,
2937 (Elf_External_Vernaux
*) p
, &needaux
);
2938 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2940 _bfd_elf_swap_vernaux_out (output_bfd
,
2942 (Elf_External_Vernaux
*) p
);
2943 p
+= sizeof (Elf_External_Vernaux
);
2946 while (need
.vn_next
);
2952 /* Add symbols from an ELF object file to the linker hash table. */
2955 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2957 bfd_boolean (*add_symbol_hook
)
2958 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2959 const char **, flagword
*, asection
**, bfd_vma
*);
2960 bfd_boolean (*check_relocs
)
2961 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2962 bfd_boolean (*check_directives
)
2963 (bfd
*, struct bfd_link_info
*);
2964 bfd_boolean collect
;
2965 Elf_Internal_Shdr
*hdr
;
2966 bfd_size_type symcount
;
2967 bfd_size_type extsymcount
;
2968 bfd_size_type extsymoff
;
2969 struct elf_link_hash_entry
**sym_hash
;
2970 bfd_boolean dynamic
;
2971 Elf_External_Versym
*extversym
= NULL
;
2972 Elf_External_Versym
*ever
;
2973 struct elf_link_hash_entry
*weaks
;
2974 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2975 bfd_size_type nondeflt_vers_cnt
= 0;
2976 Elf_Internal_Sym
*isymbuf
= NULL
;
2977 Elf_Internal_Sym
*isym
;
2978 Elf_Internal_Sym
*isymend
;
2979 const struct elf_backend_data
*bed
;
2980 bfd_boolean add_needed
;
2981 struct elf_link_hash_table
* hash_table
;
2984 hash_table
= elf_hash_table (info
);
2986 bed
= get_elf_backend_data (abfd
);
2987 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2988 collect
= bed
->collect
;
2990 if ((abfd
->flags
& DYNAMIC
) == 0)
2996 /* You can't use -r against a dynamic object. Also, there's no
2997 hope of using a dynamic object which does not exactly match
2998 the format of the output file. */
2999 if (info
->relocatable
3000 || !is_elf_hash_table (hash_table
)
3001 || hash_table
->root
.creator
!= abfd
->xvec
)
3003 if (info
->relocatable
)
3004 bfd_set_error (bfd_error_invalid_operation
);
3006 bfd_set_error (bfd_error_wrong_format
);
3011 /* As a GNU extension, any input sections which are named
3012 .gnu.warning.SYMBOL are treated as warning symbols for the given
3013 symbol. This differs from .gnu.warning sections, which generate
3014 warnings when they are included in an output file. */
3015 if (info
->executable
)
3019 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3023 name
= bfd_get_section_name (abfd
, s
);
3024 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3028 bfd_size_type prefix_len
;
3029 const char * gnu_warning_prefix
= _("warning: ");
3031 name
+= sizeof ".gnu.warning." - 1;
3033 /* If this is a shared object, then look up the symbol
3034 in the hash table. If it is there, and it is already
3035 been defined, then we will not be using the entry
3036 from this shared object, so we don't need to warn.
3037 FIXME: If we see the definition in a regular object
3038 later on, we will warn, but we shouldn't. The only
3039 fix is to keep track of what warnings we are supposed
3040 to emit, and then handle them all at the end of the
3044 struct elf_link_hash_entry
*h
;
3046 h
= elf_link_hash_lookup (hash_table
, name
,
3047 FALSE
, FALSE
, TRUE
);
3049 /* FIXME: What about bfd_link_hash_common? */
3051 && (h
->root
.type
== bfd_link_hash_defined
3052 || h
->root
.type
== bfd_link_hash_defweak
))
3054 /* We don't want to issue this warning. Clobber
3055 the section size so that the warning does not
3056 get copied into the output file. */
3063 prefix_len
= strlen (gnu_warning_prefix
);
3064 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3068 strcpy (msg
, gnu_warning_prefix
);
3069 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3072 msg
[prefix_len
+ sz
] = '\0';
3074 if (! (_bfd_generic_link_add_one_symbol
3075 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3076 FALSE
, collect
, NULL
)))
3079 if (! info
->relocatable
)
3081 /* Clobber the section size so that the warning does
3082 not get copied into the output file. */
3092 /* If we are creating a shared library, create all the dynamic
3093 sections immediately. We need to attach them to something,
3094 so we attach them to this BFD, provided it is the right
3095 format. FIXME: If there are no input BFD's of the same
3096 format as the output, we can't make a shared library. */
3098 && is_elf_hash_table (hash_table
)
3099 && hash_table
->root
.creator
== abfd
->xvec
3100 && ! hash_table
->dynamic_sections_created
)
3102 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3106 else if (!is_elf_hash_table (hash_table
))
3111 const char *soname
= NULL
;
3112 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3115 /* ld --just-symbols and dynamic objects don't mix very well.
3116 Test for --just-symbols by looking at info set up by
3117 _bfd_elf_link_just_syms. */
3118 if ((s
= abfd
->sections
) != NULL
3119 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3122 /* If this dynamic lib was specified on the command line with
3123 --as-needed in effect, then we don't want to add a DT_NEEDED
3124 tag unless the lib is actually used. Similary for libs brought
3125 in by another lib's DT_NEEDED. When --no-add-needed is used
3126 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3127 any dynamic library in DT_NEEDED tags in the dynamic lib at
3129 add_needed
= (elf_dyn_lib_class (abfd
)
3130 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3131 | DYN_NO_NEEDED
)) == 0;
3133 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3139 unsigned long shlink
;
3141 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3142 goto error_free_dyn
;
3144 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3146 goto error_free_dyn
;
3147 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3149 for (extdyn
= dynbuf
;
3150 extdyn
< dynbuf
+ s
->size
;
3151 extdyn
+= bed
->s
->sizeof_dyn
)
3153 Elf_Internal_Dyn dyn
;
3155 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3156 if (dyn
.d_tag
== DT_SONAME
)
3158 unsigned int tagv
= dyn
.d_un
.d_val
;
3159 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3161 goto error_free_dyn
;
3163 if (dyn
.d_tag
== DT_NEEDED
)
3165 struct bfd_link_needed_list
*n
, **pn
;
3167 unsigned int tagv
= dyn
.d_un
.d_val
;
3169 amt
= sizeof (struct bfd_link_needed_list
);
3170 n
= bfd_alloc (abfd
, amt
);
3171 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3172 if (n
== NULL
|| fnm
== NULL
)
3173 goto error_free_dyn
;
3174 amt
= strlen (fnm
) + 1;
3175 anm
= bfd_alloc (abfd
, amt
);
3177 goto error_free_dyn
;
3178 memcpy (anm
, fnm
, amt
);
3182 for (pn
= & hash_table
->needed
;
3188 if (dyn
.d_tag
== DT_RUNPATH
)
3190 struct bfd_link_needed_list
*n
, **pn
;
3192 unsigned int tagv
= dyn
.d_un
.d_val
;
3194 amt
= sizeof (struct bfd_link_needed_list
);
3195 n
= bfd_alloc (abfd
, amt
);
3196 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3197 if (n
== NULL
|| fnm
== NULL
)
3198 goto error_free_dyn
;
3199 amt
= strlen (fnm
) + 1;
3200 anm
= bfd_alloc (abfd
, amt
);
3202 goto error_free_dyn
;
3203 memcpy (anm
, fnm
, amt
);
3207 for (pn
= & runpath
;
3213 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3214 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3216 struct bfd_link_needed_list
*n
, **pn
;
3218 unsigned int tagv
= dyn
.d_un
.d_val
;
3220 amt
= sizeof (struct bfd_link_needed_list
);
3221 n
= bfd_alloc (abfd
, amt
);
3222 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3223 if (n
== NULL
|| fnm
== NULL
)
3224 goto error_free_dyn
;
3225 amt
= strlen (fnm
) + 1;
3226 anm
= bfd_alloc (abfd
, amt
);
3233 memcpy (anm
, fnm
, amt
);
3248 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3249 frees all more recently bfd_alloc'd blocks as well. */
3255 struct bfd_link_needed_list
**pn
;
3256 for (pn
= & hash_table
->runpath
;
3263 /* We do not want to include any of the sections in a dynamic
3264 object in the output file. We hack by simply clobbering the
3265 list of sections in the BFD. This could be handled more
3266 cleanly by, say, a new section flag; the existing
3267 SEC_NEVER_LOAD flag is not the one we want, because that one
3268 still implies that the section takes up space in the output
3270 bfd_section_list_clear (abfd
);
3272 /* If this is the first dynamic object found in the link, create
3273 the special sections required for dynamic linking. */
3274 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3277 /* Find the name to use in a DT_NEEDED entry that refers to this
3278 object. If the object has a DT_SONAME entry, we use it.
3279 Otherwise, if the generic linker stuck something in
3280 elf_dt_name, we use that. Otherwise, we just use the file
3282 if (soname
== NULL
|| *soname
== '\0')
3284 soname
= elf_dt_name (abfd
);
3285 if (soname
== NULL
|| *soname
== '\0')
3286 soname
= bfd_get_filename (abfd
);
3289 /* Save the SONAME because sometimes the linker emulation code
3290 will need to know it. */
3291 elf_dt_name (abfd
) = soname
;
3293 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3297 /* If we have already included this dynamic object in the
3298 link, just ignore it. There is no reason to include a
3299 particular dynamic object more than once. */
3304 /* If this is a dynamic object, we always link against the .dynsym
3305 symbol table, not the .symtab symbol table. The dynamic linker
3306 will only see the .dynsym symbol table, so there is no reason to
3307 look at .symtab for a dynamic object. */
3309 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3310 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3312 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3314 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3316 /* The sh_info field of the symtab header tells us where the
3317 external symbols start. We don't care about the local symbols at
3319 if (elf_bad_symtab (abfd
))
3321 extsymcount
= symcount
;
3326 extsymcount
= symcount
- hdr
->sh_info
;
3327 extsymoff
= hdr
->sh_info
;
3331 if (extsymcount
!= 0)
3333 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3335 if (isymbuf
== NULL
)
3338 /* We store a pointer to the hash table entry for each external
3340 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3341 sym_hash
= bfd_alloc (abfd
, amt
);
3342 if (sym_hash
== NULL
)
3343 goto error_free_sym
;
3344 elf_sym_hashes (abfd
) = sym_hash
;
3349 /* Read in any version definitions. */
3350 if (!_bfd_elf_slurp_version_tables (abfd
,
3351 info
->default_imported_symver
))
3352 goto error_free_sym
;
3354 /* Read in the symbol versions, but don't bother to convert them
3355 to internal format. */
3356 if (elf_dynversym (abfd
) != 0)
3358 Elf_Internal_Shdr
*versymhdr
;
3360 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3361 extversym
= bfd_malloc (versymhdr
->sh_size
);
3362 if (extversym
== NULL
)
3363 goto error_free_sym
;
3364 amt
= versymhdr
->sh_size
;
3365 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3366 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3367 goto error_free_vers
;
3373 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3374 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3376 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3383 struct elf_link_hash_entry
*h
;
3384 bfd_boolean definition
;
3385 bfd_boolean size_change_ok
;
3386 bfd_boolean type_change_ok
;
3387 bfd_boolean new_weakdef
;
3388 bfd_boolean override
;
3389 unsigned int old_alignment
;
3394 flags
= BSF_NO_FLAGS
;
3396 value
= isym
->st_value
;
3399 bind
= ELF_ST_BIND (isym
->st_info
);
3400 if (bind
== STB_LOCAL
)
3402 /* This should be impossible, since ELF requires that all
3403 global symbols follow all local symbols, and that sh_info
3404 point to the first global symbol. Unfortunately, Irix 5
3408 else if (bind
== STB_GLOBAL
)
3410 if (isym
->st_shndx
!= SHN_UNDEF
3411 && isym
->st_shndx
!= SHN_COMMON
)
3414 else if (bind
== STB_WEAK
)
3418 /* Leave it up to the processor backend. */
3421 if (isym
->st_shndx
== SHN_UNDEF
)
3422 sec
= bfd_und_section_ptr
;
3423 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3425 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3427 sec
= bfd_abs_section_ptr
;
3428 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3431 else if (isym
->st_shndx
== SHN_ABS
)
3432 sec
= bfd_abs_section_ptr
;
3433 else if (isym
->st_shndx
== SHN_COMMON
)
3435 sec
= bfd_com_section_ptr
;
3436 /* What ELF calls the size we call the value. What ELF
3437 calls the value we call the alignment. */
3438 value
= isym
->st_size
;
3442 /* Leave it up to the processor backend. */
3445 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3448 goto error_free_vers
;
3450 if (isym
->st_shndx
== SHN_COMMON
3451 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3453 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3457 tcomm
= bfd_make_section (abfd
, ".tcommon");
3459 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3461 | SEC_LINKER_CREATED
3462 | SEC_THREAD_LOCAL
)))
3463 goto error_free_vers
;
3467 else if (add_symbol_hook
)
3469 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3471 goto error_free_vers
;
3473 /* The hook function sets the name to NULL if this symbol
3474 should be skipped for some reason. */
3479 /* Sanity check that all possibilities were handled. */
3482 bfd_set_error (bfd_error_bad_value
);
3483 goto error_free_vers
;
3486 if (bfd_is_und_section (sec
)
3487 || bfd_is_com_section (sec
))
3492 size_change_ok
= FALSE
;
3493 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3497 if (is_elf_hash_table (hash_table
))
3499 Elf_Internal_Versym iver
;
3500 unsigned int vernum
= 0;
3505 if (info
->default_imported_symver
)
3506 /* Use the default symbol version created earlier. */
3507 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3512 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3514 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3516 /* If this is a hidden symbol, or if it is not version
3517 1, we append the version name to the symbol name.
3518 However, we do not modify a non-hidden absolute
3519 symbol, because it might be the version symbol
3520 itself. FIXME: What if it isn't? */
3521 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3522 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3525 size_t namelen
, verlen
, newlen
;
3528 if (isym
->st_shndx
!= SHN_UNDEF
)
3530 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3532 else if (vernum
> 1)
3534 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3540 (*_bfd_error_handler
)
3541 (_("%B: %s: invalid version %u (max %d)"),
3543 elf_tdata (abfd
)->cverdefs
);
3544 bfd_set_error (bfd_error_bad_value
);
3545 goto error_free_vers
;
3550 /* We cannot simply test for the number of
3551 entries in the VERNEED section since the
3552 numbers for the needed versions do not start
3554 Elf_Internal_Verneed
*t
;
3557 for (t
= elf_tdata (abfd
)->verref
;
3561 Elf_Internal_Vernaux
*a
;
3563 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3565 if (a
->vna_other
== vernum
)
3567 verstr
= a
->vna_nodename
;
3576 (*_bfd_error_handler
)
3577 (_("%B: %s: invalid needed version %d"),
3578 abfd
, name
, vernum
);
3579 bfd_set_error (bfd_error_bad_value
);
3580 goto error_free_vers
;
3584 namelen
= strlen (name
);
3585 verlen
= strlen (verstr
);
3586 newlen
= namelen
+ verlen
+ 2;
3587 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3588 && isym
->st_shndx
!= SHN_UNDEF
)
3591 newname
= bfd_alloc (abfd
, newlen
);
3592 if (newname
== NULL
)
3593 goto error_free_vers
;
3594 memcpy (newname
, name
, namelen
);
3595 p
= newname
+ namelen
;
3597 /* If this is a defined non-hidden version symbol,
3598 we add another @ to the name. This indicates the
3599 default version of the symbol. */
3600 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3601 && isym
->st_shndx
!= SHN_UNDEF
)
3603 memcpy (p
, verstr
, verlen
+ 1);
3608 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3609 sym_hash
, &skip
, &override
,
3610 &type_change_ok
, &size_change_ok
))
3611 goto error_free_vers
;
3620 while (h
->root
.type
== bfd_link_hash_indirect
3621 || h
->root
.type
== bfd_link_hash_warning
)
3622 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3624 /* Remember the old alignment if this is a common symbol, so
3625 that we don't reduce the alignment later on. We can't
3626 check later, because _bfd_generic_link_add_one_symbol
3627 will set a default for the alignment which we want to
3628 override. We also remember the old bfd where the existing
3629 definition comes from. */
3630 switch (h
->root
.type
)
3635 case bfd_link_hash_defined
:
3636 case bfd_link_hash_defweak
:
3637 old_bfd
= h
->root
.u
.def
.section
->owner
;
3640 case bfd_link_hash_common
:
3641 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3642 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3646 if (elf_tdata (abfd
)->verdef
!= NULL
3650 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3653 if (! (_bfd_generic_link_add_one_symbol
3654 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3655 (struct bfd_link_hash_entry
**) sym_hash
)))
3656 goto error_free_vers
;
3659 while (h
->root
.type
== bfd_link_hash_indirect
3660 || h
->root
.type
== bfd_link_hash_warning
)
3661 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3664 new_weakdef
= FALSE
;
3667 && (flags
& BSF_WEAK
) != 0
3668 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3669 && is_elf_hash_table (hash_table
)
3670 && h
->u
.weakdef
== NULL
)
3672 /* Keep a list of all weak defined non function symbols from
3673 a dynamic object, using the weakdef field. Later in this
3674 function we will set the weakdef field to the correct
3675 value. We only put non-function symbols from dynamic
3676 objects on this list, because that happens to be the only
3677 time we need to know the normal symbol corresponding to a
3678 weak symbol, and the information is time consuming to
3679 figure out. If the weakdef field is not already NULL,
3680 then this symbol was already defined by some previous
3681 dynamic object, and we will be using that previous
3682 definition anyhow. */
3684 h
->u
.weakdef
= weaks
;
3689 /* Set the alignment of a common symbol. */
3690 if (isym
->st_shndx
== SHN_COMMON
3691 && h
->root
.type
== bfd_link_hash_common
)
3695 align
= bfd_log2 (isym
->st_value
);
3696 if (align
> old_alignment
3697 /* Permit an alignment power of zero if an alignment of one
3698 is specified and no other alignments have been specified. */
3699 || (isym
->st_value
== 1 && old_alignment
== 0))
3700 h
->root
.u
.c
.p
->alignment_power
= align
;
3702 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3705 if (is_elf_hash_table (hash_table
))
3709 /* Check the alignment when a common symbol is involved. This
3710 can change when a common symbol is overridden by a normal
3711 definition or a common symbol is ignored due to the old
3712 normal definition. We need to make sure the maximum
3713 alignment is maintained. */
3714 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3715 && h
->root
.type
!= bfd_link_hash_common
)
3717 unsigned int common_align
;
3718 unsigned int normal_align
;
3719 unsigned int symbol_align
;
3723 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3724 if (h
->root
.u
.def
.section
->owner
!= NULL
3725 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3727 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3728 if (normal_align
> symbol_align
)
3729 normal_align
= symbol_align
;
3732 normal_align
= symbol_align
;
3736 common_align
= old_alignment
;
3737 common_bfd
= old_bfd
;
3742 common_align
= bfd_log2 (isym
->st_value
);
3744 normal_bfd
= old_bfd
;
3747 if (normal_align
< common_align
)
3748 (*_bfd_error_handler
)
3749 (_("Warning: alignment %u of symbol `%s' in %B"
3750 " is smaller than %u in %B"),
3751 normal_bfd
, common_bfd
,
3752 1 << normal_align
, name
, 1 << common_align
);
3755 /* Remember the symbol size and type. */
3756 if (isym
->st_size
!= 0
3757 && (definition
|| h
->size
== 0))
3759 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3760 (*_bfd_error_handler
)
3761 (_("Warning: size of symbol `%s' changed"
3762 " from %lu in %B to %lu in %B"),
3764 name
, (unsigned long) h
->size
,
3765 (unsigned long) isym
->st_size
);
3767 h
->size
= isym
->st_size
;
3770 /* If this is a common symbol, then we always want H->SIZE
3771 to be the size of the common symbol. The code just above
3772 won't fix the size if a common symbol becomes larger. We
3773 don't warn about a size change here, because that is
3774 covered by --warn-common. */
3775 if (h
->root
.type
== bfd_link_hash_common
)
3776 h
->size
= h
->root
.u
.c
.size
;
3778 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3779 && (definition
|| h
->type
== STT_NOTYPE
))
3781 if (h
->type
!= STT_NOTYPE
3782 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3783 && ! type_change_ok
)
3784 (*_bfd_error_handler
)
3785 (_("Warning: type of symbol `%s' changed"
3786 " from %d to %d in %B"),
3787 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3789 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3792 /* If st_other has a processor-specific meaning, specific
3793 code might be needed here. We never merge the visibility
3794 attribute with the one from a dynamic object. */
3795 if (bed
->elf_backend_merge_symbol_attribute
)
3796 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3799 /* If this symbol has default visibility and the user has requested
3800 we not re-export it, then mark it as hidden. */
3801 if (definition
&& !dynamic
3803 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3804 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3805 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3807 if (isym
->st_other
!= 0 && !dynamic
)
3809 unsigned char hvis
, symvis
, other
, nvis
;
3811 /* Take the balance of OTHER from the definition. */
3812 other
= (definition
? isym
->st_other
: h
->other
);
3813 other
&= ~ ELF_ST_VISIBILITY (-1);
3815 /* Combine visibilities, using the most constraining one. */
3816 hvis
= ELF_ST_VISIBILITY (h
->other
);
3817 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3823 nvis
= hvis
< symvis
? hvis
: symvis
;
3825 h
->other
= other
| nvis
;
3828 /* Set a flag in the hash table entry indicating the type of
3829 reference or definition we just found. Keep a count of
3830 the number of dynamic symbols we find. A dynamic symbol
3831 is one which is referenced or defined by both a regular
3832 object and a shared object. */
3839 if (bind
!= STB_WEAK
)
3840 h
->ref_regular_nonweak
= 1;
3844 if (! info
->executable
3857 || (h
->u
.weakdef
!= NULL
3859 && h
->u
.weakdef
->dynindx
!= -1))
3863 /* Check to see if we need to add an indirect symbol for
3864 the default name. */
3865 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3866 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3867 &sec
, &value
, &dynsym
,
3869 goto error_free_vers
;
3871 if (definition
&& !dynamic
)
3873 char *p
= strchr (name
, ELF_VER_CHR
);
3874 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3876 /* Queue non-default versions so that .symver x, x@FOO
3877 aliases can be checked. */
3878 if (! nondeflt_vers
)
3880 amt
= (isymend
- isym
+ 1)
3881 * sizeof (struct elf_link_hash_entry
*);
3882 nondeflt_vers
= bfd_malloc (amt
);
3884 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3888 if (dynsym
&& h
->dynindx
== -1)
3890 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3891 goto error_free_vers
;
3892 if (h
->u
.weakdef
!= NULL
3894 && h
->u
.weakdef
->dynindx
== -1)
3896 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3897 goto error_free_vers
;
3900 else if (dynsym
&& h
->dynindx
!= -1)
3901 /* If the symbol already has a dynamic index, but
3902 visibility says it should not be visible, turn it into
3904 switch (ELF_ST_VISIBILITY (h
->other
))
3908 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3919 const char *soname
= elf_dt_name (abfd
);
3921 /* A symbol from a library loaded via DT_NEEDED of some
3922 other library is referenced by a regular object.
3923 Add a DT_NEEDED entry for it. Issue an error if
3924 --no-add-needed is used. */
3925 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3927 (*_bfd_error_handler
)
3928 (_("%s: invalid DSO for symbol `%s' definition"),
3930 bfd_set_error (bfd_error_bad_value
);
3931 goto error_free_vers
;
3935 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3937 goto error_free_vers
;
3939 BFD_ASSERT (ret
== 0);
3944 /* Now that all the symbols from this input file are created, handle
3945 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3946 if (nondeflt_vers
!= NULL
)
3948 bfd_size_type cnt
, symidx
;
3950 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3952 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3953 char *shortname
, *p
;
3955 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3957 || (h
->root
.type
!= bfd_link_hash_defined
3958 && h
->root
.type
!= bfd_link_hash_defweak
))
3961 amt
= p
- h
->root
.root
.string
;
3962 shortname
= bfd_malloc (amt
+ 1);
3963 memcpy (shortname
, h
->root
.root
.string
, amt
);
3964 shortname
[amt
] = '\0';
3966 hi
= (struct elf_link_hash_entry
*)
3967 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3968 FALSE
, FALSE
, FALSE
);
3970 && hi
->root
.type
== h
->root
.type
3971 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3972 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3974 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3975 hi
->root
.type
= bfd_link_hash_indirect
;
3976 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3977 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3978 sym_hash
= elf_sym_hashes (abfd
);
3980 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3981 if (sym_hash
[symidx
] == hi
)
3983 sym_hash
[symidx
] = h
;
3989 free (nondeflt_vers
);
3990 nondeflt_vers
= NULL
;
3993 if (extversym
!= NULL
)
3999 if (isymbuf
!= NULL
)
4003 /* Now set the weakdefs field correctly for all the weak defined
4004 symbols we found. The only way to do this is to search all the
4005 symbols. Since we only need the information for non functions in
4006 dynamic objects, that's the only time we actually put anything on
4007 the list WEAKS. We need this information so that if a regular
4008 object refers to a symbol defined weakly in a dynamic object, the
4009 real symbol in the dynamic object is also put in the dynamic
4010 symbols; we also must arrange for both symbols to point to the
4011 same memory location. We could handle the general case of symbol
4012 aliasing, but a general symbol alias can only be generated in
4013 assembler code, handling it correctly would be very time
4014 consuming, and other ELF linkers don't handle general aliasing
4018 struct elf_link_hash_entry
**hpp
;
4019 struct elf_link_hash_entry
**hppend
;
4020 struct elf_link_hash_entry
**sorted_sym_hash
;
4021 struct elf_link_hash_entry
*h
;
4024 /* Since we have to search the whole symbol list for each weak
4025 defined symbol, search time for N weak defined symbols will be
4026 O(N^2). Binary search will cut it down to O(NlogN). */
4027 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4028 sorted_sym_hash
= bfd_malloc (amt
);
4029 if (sorted_sym_hash
== NULL
)
4031 sym_hash
= sorted_sym_hash
;
4032 hpp
= elf_sym_hashes (abfd
);
4033 hppend
= hpp
+ extsymcount
;
4035 for (; hpp
< hppend
; hpp
++)
4039 && h
->root
.type
== bfd_link_hash_defined
4040 && h
->type
!= STT_FUNC
)
4048 qsort (sorted_sym_hash
, sym_count
,
4049 sizeof (struct elf_link_hash_entry
*),
4052 while (weaks
!= NULL
)
4054 struct elf_link_hash_entry
*hlook
;
4061 weaks
= hlook
->u
.weakdef
;
4062 hlook
->u
.weakdef
= NULL
;
4064 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4065 || hlook
->root
.type
== bfd_link_hash_defweak
4066 || hlook
->root
.type
== bfd_link_hash_common
4067 || hlook
->root
.type
== bfd_link_hash_indirect
);
4068 slook
= hlook
->root
.u
.def
.section
;
4069 vlook
= hlook
->root
.u
.def
.value
;
4076 bfd_signed_vma vdiff
;
4078 h
= sorted_sym_hash
[idx
];
4079 vdiff
= vlook
- h
->root
.u
.def
.value
;
4086 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4099 /* We didn't find a value/section match. */
4103 for (i
= ilook
; i
< sym_count
; i
++)
4105 h
= sorted_sym_hash
[i
];
4107 /* Stop if value or section doesn't match. */
4108 if (h
->root
.u
.def
.value
!= vlook
4109 || h
->root
.u
.def
.section
!= slook
)
4111 else if (h
!= hlook
)
4113 hlook
->u
.weakdef
= h
;
4115 /* If the weak definition is in the list of dynamic
4116 symbols, make sure the real definition is put
4118 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4120 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4124 /* If the real definition is in the list of dynamic
4125 symbols, make sure the weak definition is put
4126 there as well. If we don't do this, then the
4127 dynamic loader might not merge the entries for the
4128 real definition and the weak definition. */
4129 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4131 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4139 free (sorted_sym_hash
);
4142 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4143 if (check_directives
)
4144 check_directives (abfd
, info
);
4146 /* If this object is the same format as the output object, and it is
4147 not a shared library, then let the backend look through the
4150 This is required to build global offset table entries and to
4151 arrange for dynamic relocs. It is not required for the
4152 particular common case of linking non PIC code, even when linking
4153 against shared libraries, but unfortunately there is no way of
4154 knowing whether an object file has been compiled PIC or not.
4155 Looking through the relocs is not particularly time consuming.
4156 The problem is that we must either (1) keep the relocs in memory,
4157 which causes the linker to require additional runtime memory or
4158 (2) read the relocs twice from the input file, which wastes time.
4159 This would be a good case for using mmap.
4161 I have no idea how to handle linking PIC code into a file of a
4162 different format. It probably can't be done. */
4163 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4165 && is_elf_hash_table (hash_table
)
4166 && hash_table
->root
.creator
== abfd
->xvec
4167 && check_relocs
!= NULL
)
4171 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4173 Elf_Internal_Rela
*internal_relocs
;
4176 if ((o
->flags
& SEC_RELOC
) == 0
4177 || o
->reloc_count
== 0
4178 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4179 && (o
->flags
& SEC_DEBUGGING
) != 0)
4180 || bfd_is_abs_section (o
->output_section
))
4183 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4185 if (internal_relocs
== NULL
)
4188 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4190 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4191 free (internal_relocs
);
4198 /* If this is a non-traditional link, try to optimize the handling
4199 of the .stab/.stabstr sections. */
4201 && ! info
->traditional_format
4202 && is_elf_hash_table (hash_table
)
4203 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4207 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4208 if (stabstr
!= NULL
)
4210 bfd_size_type string_offset
= 0;
4213 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4214 if (strncmp (".stab", stab
->name
, 5) == 0
4215 && (!stab
->name
[5] ||
4216 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4217 && (stab
->flags
& SEC_MERGE
) == 0
4218 && !bfd_is_abs_section (stab
->output_section
))
4220 struct bfd_elf_section_data
*secdata
;
4222 secdata
= elf_section_data (stab
);
4223 if (! _bfd_link_section_stabs (abfd
,
4224 &hash_table
->stab_info
,
4229 if (secdata
->sec_info
)
4230 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4235 if (is_elf_hash_table (hash_table
))
4237 /* Add this bfd to the loaded list. */
4238 struct elf_link_loaded_list
*n
;
4240 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4244 n
->next
= hash_table
->loaded
;
4245 hash_table
->loaded
= n
;
4251 if (nondeflt_vers
!= NULL
)
4252 free (nondeflt_vers
);
4253 if (extversym
!= NULL
)
4256 if (isymbuf
!= NULL
)
4262 /* Return the linker hash table entry of a symbol that might be
4263 satisfied by an archive symbol. Return -1 on error. */
4265 struct elf_link_hash_entry
*
4266 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4267 struct bfd_link_info
*info
,
4270 struct elf_link_hash_entry
*h
;
4274 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4278 /* If this is a default version (the name contains @@), look up the
4279 symbol again with only one `@' as well as without the version.
4280 The effect is that references to the symbol with and without the
4281 version will be matched by the default symbol in the archive. */
4283 p
= strchr (name
, ELF_VER_CHR
);
4284 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4287 /* First check with only one `@'. */
4288 len
= strlen (name
);
4289 copy
= bfd_alloc (abfd
, len
);
4291 return (struct elf_link_hash_entry
*) 0 - 1;
4293 first
= p
- name
+ 1;
4294 memcpy (copy
, name
, first
);
4295 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4297 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4300 /* We also need to check references to the symbol without the
4302 copy
[first
- 1] = '\0';
4303 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4304 FALSE
, FALSE
, FALSE
);
4307 bfd_release (abfd
, copy
);
4311 /* Add symbols from an ELF archive file to the linker hash table. We
4312 don't use _bfd_generic_link_add_archive_symbols because of a
4313 problem which arises on UnixWare. The UnixWare libc.so is an
4314 archive which includes an entry libc.so.1 which defines a bunch of
4315 symbols. The libc.so archive also includes a number of other
4316 object files, which also define symbols, some of which are the same
4317 as those defined in libc.so.1. Correct linking requires that we
4318 consider each object file in turn, and include it if it defines any
4319 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4320 this; it looks through the list of undefined symbols, and includes
4321 any object file which defines them. When this algorithm is used on
4322 UnixWare, it winds up pulling in libc.so.1 early and defining a
4323 bunch of symbols. This means that some of the other objects in the
4324 archive are not included in the link, which is incorrect since they
4325 precede libc.so.1 in the archive.
4327 Fortunately, ELF archive handling is simpler than that done by
4328 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4329 oddities. In ELF, if we find a symbol in the archive map, and the
4330 symbol is currently undefined, we know that we must pull in that
4333 Unfortunately, we do have to make multiple passes over the symbol
4334 table until nothing further is resolved. */
4337 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4340 bfd_boolean
*defined
= NULL
;
4341 bfd_boolean
*included
= NULL
;
4345 const struct elf_backend_data
*bed
;
4346 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4347 (bfd
*, struct bfd_link_info
*, const char *);
4349 if (! bfd_has_map (abfd
))
4351 /* An empty archive is a special case. */
4352 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4354 bfd_set_error (bfd_error_no_armap
);
4358 /* Keep track of all symbols we know to be already defined, and all
4359 files we know to be already included. This is to speed up the
4360 second and subsequent passes. */
4361 c
= bfd_ardata (abfd
)->symdef_count
;
4365 amt
*= sizeof (bfd_boolean
);
4366 defined
= bfd_zmalloc (amt
);
4367 included
= bfd_zmalloc (amt
);
4368 if (defined
== NULL
|| included
== NULL
)
4371 symdefs
= bfd_ardata (abfd
)->symdefs
;
4372 bed
= get_elf_backend_data (abfd
);
4373 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4386 symdefend
= symdef
+ c
;
4387 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4389 struct elf_link_hash_entry
*h
;
4391 struct bfd_link_hash_entry
*undefs_tail
;
4394 if (defined
[i
] || included
[i
])
4396 if (symdef
->file_offset
== last
)
4402 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4403 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4409 if (h
->root
.type
== bfd_link_hash_common
)
4411 /* We currently have a common symbol. The archive map contains
4412 a reference to this symbol, so we may want to include it. We
4413 only want to include it however, if this archive element
4414 contains a definition of the symbol, not just another common
4417 Unfortunately some archivers (including GNU ar) will put
4418 declarations of common symbols into their archive maps, as
4419 well as real definitions, so we cannot just go by the archive
4420 map alone. Instead we must read in the element's symbol
4421 table and check that to see what kind of symbol definition
4423 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4426 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4428 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4433 /* We need to include this archive member. */
4434 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4435 if (element
== NULL
)
4438 if (! bfd_check_format (element
, bfd_object
))
4441 /* Doublecheck that we have not included this object
4442 already--it should be impossible, but there may be
4443 something wrong with the archive. */
4444 if (element
->archive_pass
!= 0)
4446 bfd_set_error (bfd_error_bad_value
);
4449 element
->archive_pass
= 1;
4451 undefs_tail
= info
->hash
->undefs_tail
;
4453 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4456 if (! bfd_link_add_symbols (element
, info
))
4459 /* If there are any new undefined symbols, we need to make
4460 another pass through the archive in order to see whether
4461 they can be defined. FIXME: This isn't perfect, because
4462 common symbols wind up on undefs_tail and because an
4463 undefined symbol which is defined later on in this pass
4464 does not require another pass. This isn't a bug, but it
4465 does make the code less efficient than it could be. */
4466 if (undefs_tail
!= info
->hash
->undefs_tail
)
4469 /* Look backward to mark all symbols from this object file
4470 which we have already seen in this pass. */
4474 included
[mark
] = TRUE
;
4479 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4481 /* We mark subsequent symbols from this object file as we go
4482 on through the loop. */
4483 last
= symdef
->file_offset
;
4494 if (defined
!= NULL
)
4496 if (included
!= NULL
)
4501 /* Given an ELF BFD, add symbols to the global hash table as
4505 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4507 switch (bfd_get_format (abfd
))
4510 return elf_link_add_object_symbols (abfd
, info
);
4512 return elf_link_add_archive_symbols (abfd
, info
);
4514 bfd_set_error (bfd_error_wrong_format
);
4519 /* This function will be called though elf_link_hash_traverse to store
4520 all hash value of the exported symbols in an array. */
4523 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4525 unsigned long **valuep
= data
;
4531 if (h
->root
.type
== bfd_link_hash_warning
)
4532 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4534 /* Ignore indirect symbols. These are added by the versioning code. */
4535 if (h
->dynindx
== -1)
4538 name
= h
->root
.root
.string
;
4539 p
= strchr (name
, ELF_VER_CHR
);
4542 alc
= bfd_malloc (p
- name
+ 1);
4543 memcpy (alc
, name
, p
- name
);
4544 alc
[p
- name
] = '\0';
4548 /* Compute the hash value. */
4549 ha
= bfd_elf_hash (name
);
4551 /* Store the found hash value in the array given as the argument. */
4554 /* And store it in the struct so that we can put it in the hash table
4556 h
->u
.elf_hash_value
= ha
;
4564 /* Array used to determine the number of hash table buckets to use
4565 based on the number of symbols there are. If there are fewer than
4566 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4567 fewer than 37 we use 17 buckets, and so forth. We never use more
4568 than 32771 buckets. */
4570 static const size_t elf_buckets
[] =
4572 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4576 /* Compute bucket count for hashing table. We do not use a static set
4577 of possible tables sizes anymore. Instead we determine for all
4578 possible reasonable sizes of the table the outcome (i.e., the
4579 number of collisions etc) and choose the best solution. The
4580 weighting functions are not too simple to allow the table to grow
4581 without bounds. Instead one of the weighting factors is the size.
4582 Therefore the result is always a good payoff between few collisions
4583 (= short chain lengths) and table size. */
4585 compute_bucket_count (struct bfd_link_info
*info
)
4587 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4588 size_t best_size
= 0;
4589 unsigned long int *hashcodes
;
4590 unsigned long int *hashcodesp
;
4591 unsigned long int i
;
4594 /* Compute the hash values for all exported symbols. At the same
4595 time store the values in an array so that we could use them for
4598 amt
*= sizeof (unsigned long int);
4599 hashcodes
= bfd_malloc (amt
);
4600 if (hashcodes
== NULL
)
4602 hashcodesp
= hashcodes
;
4604 /* Put all hash values in HASHCODES. */
4605 elf_link_hash_traverse (elf_hash_table (info
),
4606 elf_collect_hash_codes
, &hashcodesp
);
4608 /* We have a problem here. The following code to optimize the table
4609 size requires an integer type with more the 32 bits. If
4610 BFD_HOST_U_64_BIT is set we know about such a type. */
4611 #ifdef BFD_HOST_U_64_BIT
4614 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4617 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4618 unsigned long int *counts
;
4619 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4620 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4622 /* Possible optimization parameters: if we have NSYMS symbols we say
4623 that the hashing table must at least have NSYMS/4 and at most
4625 minsize
= nsyms
/ 4;
4628 best_size
= maxsize
= nsyms
* 2;
4630 /* Create array where we count the collisions in. We must use bfd_malloc
4631 since the size could be large. */
4633 amt
*= sizeof (unsigned long int);
4634 counts
= bfd_malloc (amt
);
4641 /* Compute the "optimal" size for the hash table. The criteria is a
4642 minimal chain length. The minor criteria is (of course) the size
4644 for (i
= minsize
; i
< maxsize
; ++i
)
4646 /* Walk through the array of hashcodes and count the collisions. */
4647 BFD_HOST_U_64_BIT max
;
4648 unsigned long int j
;
4649 unsigned long int fact
;
4651 memset (counts
, '\0', i
* sizeof (unsigned long int));
4653 /* Determine how often each hash bucket is used. */
4654 for (j
= 0; j
< nsyms
; ++j
)
4655 ++counts
[hashcodes
[j
] % i
];
4657 /* For the weight function we need some information about the
4658 pagesize on the target. This is information need not be 100%
4659 accurate. Since this information is not available (so far) we
4660 define it here to a reasonable default value. If it is crucial
4661 to have a better value some day simply define this value. */
4662 # ifndef BFD_TARGET_PAGESIZE
4663 # define BFD_TARGET_PAGESIZE (4096)
4666 /* We in any case need 2 + NSYMS entries for the size values and
4668 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4671 /* Variant 1: optimize for short chains. We add the squares
4672 of all the chain lengths (which favors many small chain
4673 over a few long chains). */
4674 for (j
= 0; j
< i
; ++j
)
4675 max
+= counts
[j
] * counts
[j
];
4677 /* This adds penalties for the overall size of the table. */
4678 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4681 /* Variant 2: Optimize a lot more for small table. Here we
4682 also add squares of the size but we also add penalties for
4683 empty slots (the +1 term). */
4684 for (j
= 0; j
< i
; ++j
)
4685 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4687 /* The overall size of the table is considered, but not as
4688 strong as in variant 1, where it is squared. */
4689 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4693 /* Compare with current best results. */
4694 if (max
< best_chlen
)
4704 #endif /* defined (BFD_HOST_U_64_BIT) */
4706 /* This is the fallback solution if no 64bit type is available or if we
4707 are not supposed to spend much time on optimizations. We select the
4708 bucket count using a fixed set of numbers. */
4709 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4711 best_size
= elf_buckets
[i
];
4712 if (dynsymcount
< elf_buckets
[i
+ 1])
4717 /* Free the arrays we needed. */
4723 /* Set up the sizes and contents of the ELF dynamic sections. This is
4724 called by the ELF linker emulation before_allocation routine. We
4725 must set the sizes of the sections before the linker sets the
4726 addresses of the various sections. */
4729 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4732 const char *filter_shlib
,
4733 const char * const *auxiliary_filters
,
4734 struct bfd_link_info
*info
,
4735 asection
**sinterpptr
,
4736 struct bfd_elf_version_tree
*verdefs
)
4738 bfd_size_type soname_indx
;
4740 const struct elf_backend_data
*bed
;
4741 struct elf_assign_sym_version_info asvinfo
;
4745 soname_indx
= (bfd_size_type
) -1;
4747 if (!is_elf_hash_table (info
->hash
))
4750 elf_tdata (output_bfd
)->relro
= info
->relro
;
4751 if (info
->execstack
)
4752 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4753 else if (info
->noexecstack
)
4754 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4758 asection
*notesec
= NULL
;
4761 for (inputobj
= info
->input_bfds
;
4763 inputobj
= inputobj
->link_next
)
4767 if (inputobj
->flags
& DYNAMIC
)
4769 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4772 if (s
->flags
& SEC_CODE
)
4781 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4782 if (exec
&& info
->relocatable
4783 && notesec
->output_section
!= bfd_abs_section_ptr
)
4784 notesec
->output_section
->flags
|= SEC_CODE
;
4788 /* Any syms created from now on start with -1 in
4789 got.refcount/offset and plt.refcount/offset. */
4790 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4792 /* The backend may have to create some sections regardless of whether
4793 we're dynamic or not. */
4794 bed
= get_elf_backend_data (output_bfd
);
4795 if (bed
->elf_backend_always_size_sections
4796 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4799 dynobj
= elf_hash_table (info
)->dynobj
;
4801 /* If there were no dynamic objects in the link, there is nothing to
4806 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4809 if (elf_hash_table (info
)->dynamic_sections_created
)
4811 struct elf_info_failed eif
;
4812 struct elf_link_hash_entry
*h
;
4814 struct bfd_elf_version_tree
*t
;
4815 struct bfd_elf_version_expr
*d
;
4816 bfd_boolean all_defined
;
4818 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4819 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4823 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4825 if (soname_indx
== (bfd_size_type
) -1
4826 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4832 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4834 info
->flags
|= DF_SYMBOLIC
;
4841 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4843 if (indx
== (bfd_size_type
) -1
4844 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4847 if (info
->new_dtags
)
4849 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4850 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4855 if (filter_shlib
!= NULL
)
4859 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4860 filter_shlib
, TRUE
);
4861 if (indx
== (bfd_size_type
) -1
4862 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4866 if (auxiliary_filters
!= NULL
)
4868 const char * const *p
;
4870 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4874 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4876 if (indx
== (bfd_size_type
) -1
4877 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4883 eif
.verdefs
= verdefs
;
4886 /* If we are supposed to export all symbols into the dynamic symbol
4887 table (this is not the normal case), then do so. */
4888 if (info
->export_dynamic
)
4890 elf_link_hash_traverse (elf_hash_table (info
),
4891 _bfd_elf_export_symbol
,
4897 /* Make all global versions with definition. */
4898 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4899 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4900 if (!d
->symver
&& d
->symbol
)
4902 const char *verstr
, *name
;
4903 size_t namelen
, verlen
, newlen
;
4905 struct elf_link_hash_entry
*newh
;
4908 namelen
= strlen (name
);
4910 verlen
= strlen (verstr
);
4911 newlen
= namelen
+ verlen
+ 3;
4913 newname
= bfd_malloc (newlen
);
4914 if (newname
== NULL
)
4916 memcpy (newname
, name
, namelen
);
4918 /* Check the hidden versioned definition. */
4919 p
= newname
+ namelen
;
4921 memcpy (p
, verstr
, verlen
+ 1);
4922 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4923 newname
, FALSE
, FALSE
,
4926 || (newh
->root
.type
!= bfd_link_hash_defined
4927 && newh
->root
.type
!= bfd_link_hash_defweak
))
4929 /* Check the default versioned definition. */
4931 memcpy (p
, verstr
, verlen
+ 1);
4932 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4933 newname
, FALSE
, FALSE
,
4938 /* Mark this version if there is a definition and it is
4939 not defined in a shared object. */
4941 && !newh
->def_dynamic
4942 && (newh
->root
.type
== bfd_link_hash_defined
4943 || newh
->root
.type
== bfd_link_hash_defweak
))
4947 /* Attach all the symbols to their version information. */
4948 asvinfo
.output_bfd
= output_bfd
;
4949 asvinfo
.info
= info
;
4950 asvinfo
.verdefs
= verdefs
;
4951 asvinfo
.failed
= FALSE
;
4953 elf_link_hash_traverse (elf_hash_table (info
),
4954 _bfd_elf_link_assign_sym_version
,
4959 if (!info
->allow_undefined_version
)
4961 /* Check if all global versions have a definition. */
4963 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4964 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4965 if (!d
->symver
&& !d
->script
)
4967 (*_bfd_error_handler
)
4968 (_("%s: undefined version: %s"),
4969 d
->pattern
, t
->name
);
4970 all_defined
= FALSE
;
4975 bfd_set_error (bfd_error_bad_value
);
4980 /* Find all symbols which were defined in a dynamic object and make
4981 the backend pick a reasonable value for them. */
4982 elf_link_hash_traverse (elf_hash_table (info
),
4983 _bfd_elf_adjust_dynamic_symbol
,
4988 /* Add some entries to the .dynamic section. We fill in some of the
4989 values later, in bfd_elf_final_link, but we must add the entries
4990 now so that we know the final size of the .dynamic section. */
4992 /* If there are initialization and/or finalization functions to
4993 call then add the corresponding DT_INIT/DT_FINI entries. */
4994 h
= (info
->init_function
4995 ? elf_link_hash_lookup (elf_hash_table (info
),
4996 info
->init_function
, FALSE
,
5003 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5006 h
= (info
->fini_function
5007 ? elf_link_hash_lookup (elf_hash_table (info
),
5008 info
->fini_function
, FALSE
,
5015 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5019 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5021 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5022 if (! info
->executable
)
5027 for (sub
= info
->input_bfds
; sub
!= NULL
;
5028 sub
= sub
->link_next
)
5029 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5030 if (elf_section_data (o
)->this_hdr
.sh_type
5031 == SHT_PREINIT_ARRAY
)
5033 (*_bfd_error_handler
)
5034 (_("%B: .preinit_array section is not allowed in DSO"),
5039 bfd_set_error (bfd_error_nonrepresentable_section
);
5043 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5044 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5047 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5049 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5050 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5053 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5055 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5056 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5060 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5061 /* If .dynstr is excluded from the link, we don't want any of
5062 these tags. Strictly, we should be checking each section
5063 individually; This quick check covers for the case where
5064 someone does a /DISCARD/ : { *(*) }. */
5065 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5067 bfd_size_type strsize
;
5069 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5070 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5071 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5072 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5073 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5074 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5075 bed
->s
->sizeof_sym
))
5080 /* The backend must work out the sizes of all the other dynamic
5082 if (bed
->elf_backend_size_dynamic_sections
5083 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5086 if (elf_hash_table (info
)->dynamic_sections_created
)
5088 bfd_size_type dynsymcount
;
5090 size_t bucketcount
= 0;
5091 size_t hash_entry_size
;
5092 unsigned int dtagcount
;
5094 /* Set up the version definition section. */
5095 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5096 BFD_ASSERT (s
!= NULL
);
5098 /* We may have created additional version definitions if we are
5099 just linking a regular application. */
5100 verdefs
= asvinfo
.verdefs
;
5102 /* Skip anonymous version tag. */
5103 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5104 verdefs
= verdefs
->next
;
5106 if (verdefs
== NULL
&& !info
->create_default_symver
)
5107 _bfd_strip_section_from_output (info
, s
);
5112 struct bfd_elf_version_tree
*t
;
5114 Elf_Internal_Verdef def
;
5115 Elf_Internal_Verdaux defaux
;
5116 struct bfd_link_hash_entry
*bh
;
5117 struct elf_link_hash_entry
*h
;
5123 /* Make space for the base version. */
5124 size
+= sizeof (Elf_External_Verdef
);
5125 size
+= sizeof (Elf_External_Verdaux
);
5128 /* Make space for the default version. */
5129 if (info
->create_default_symver
)
5131 size
+= sizeof (Elf_External_Verdef
);
5135 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5137 struct bfd_elf_version_deps
*n
;
5139 size
+= sizeof (Elf_External_Verdef
);
5140 size
+= sizeof (Elf_External_Verdaux
);
5143 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5144 size
+= sizeof (Elf_External_Verdaux
);
5148 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5149 if (s
->contents
== NULL
&& s
->size
!= 0)
5152 /* Fill in the version definition section. */
5156 def
.vd_version
= VER_DEF_CURRENT
;
5157 def
.vd_flags
= VER_FLG_BASE
;
5160 if (info
->create_default_symver
)
5162 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5163 def
.vd_next
= sizeof (Elf_External_Verdef
);
5167 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5168 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5169 + sizeof (Elf_External_Verdaux
));
5172 if (soname_indx
!= (bfd_size_type
) -1)
5174 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5176 def
.vd_hash
= bfd_elf_hash (soname
);
5177 defaux
.vda_name
= soname_indx
;
5184 name
= basename (output_bfd
->filename
);
5185 def
.vd_hash
= bfd_elf_hash (name
);
5186 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5188 if (indx
== (bfd_size_type
) -1)
5190 defaux
.vda_name
= indx
;
5192 defaux
.vda_next
= 0;
5194 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5195 (Elf_External_Verdef
*) p
);
5196 p
+= sizeof (Elf_External_Verdef
);
5197 if (info
->create_default_symver
)
5199 /* Add a symbol representing this version. */
5201 if (! (_bfd_generic_link_add_one_symbol
5202 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5204 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5206 h
= (struct elf_link_hash_entry
*) bh
;
5209 h
->type
= STT_OBJECT
;
5210 h
->verinfo
.vertree
= NULL
;
5212 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5215 /* Create a duplicate of the base version with the same
5216 aux block, but different flags. */
5219 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5221 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5222 + sizeof (Elf_External_Verdaux
));
5225 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5226 (Elf_External_Verdef
*) p
);
5227 p
+= sizeof (Elf_External_Verdef
);
5229 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5230 (Elf_External_Verdaux
*) p
);
5231 p
+= sizeof (Elf_External_Verdaux
);
5233 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5236 struct bfd_elf_version_deps
*n
;
5239 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5242 /* Add a symbol representing this version. */
5244 if (! (_bfd_generic_link_add_one_symbol
5245 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5247 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5249 h
= (struct elf_link_hash_entry
*) bh
;
5252 h
->type
= STT_OBJECT
;
5253 h
->verinfo
.vertree
= t
;
5255 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5258 def
.vd_version
= VER_DEF_CURRENT
;
5260 if (t
->globals
.list
== NULL
5261 && t
->locals
.list
== NULL
5263 def
.vd_flags
|= VER_FLG_WEAK
;
5264 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5265 def
.vd_cnt
= cdeps
+ 1;
5266 def
.vd_hash
= bfd_elf_hash (t
->name
);
5267 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5269 if (t
->next
!= NULL
)
5270 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5271 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5273 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5274 (Elf_External_Verdef
*) p
);
5275 p
+= sizeof (Elf_External_Verdef
);
5277 defaux
.vda_name
= h
->dynstr_index
;
5278 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5280 defaux
.vda_next
= 0;
5281 if (t
->deps
!= NULL
)
5282 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5283 t
->name_indx
= defaux
.vda_name
;
5285 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5286 (Elf_External_Verdaux
*) p
);
5287 p
+= sizeof (Elf_External_Verdaux
);
5289 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5291 if (n
->version_needed
== NULL
)
5293 /* This can happen if there was an error in the
5295 defaux
.vda_name
= 0;
5299 defaux
.vda_name
= n
->version_needed
->name_indx
;
5300 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5303 if (n
->next
== NULL
)
5304 defaux
.vda_next
= 0;
5306 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5308 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5309 (Elf_External_Verdaux
*) p
);
5310 p
+= sizeof (Elf_External_Verdaux
);
5314 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5315 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5318 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5321 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5323 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5326 else if (info
->flags
& DF_BIND_NOW
)
5328 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5334 if (info
->executable
)
5335 info
->flags_1
&= ~ (DF_1_INITFIRST
5338 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5342 /* Work out the size of the version reference section. */
5344 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5345 BFD_ASSERT (s
!= NULL
);
5347 struct elf_find_verdep_info sinfo
;
5349 sinfo
.output_bfd
= output_bfd
;
5351 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5352 if (sinfo
.vers
== 0)
5354 sinfo
.failed
= FALSE
;
5356 elf_link_hash_traverse (elf_hash_table (info
),
5357 _bfd_elf_link_find_version_dependencies
,
5360 if (elf_tdata (output_bfd
)->verref
== NULL
)
5361 _bfd_strip_section_from_output (info
, s
);
5364 Elf_Internal_Verneed
*t
;
5369 /* Build the version definition section. */
5372 for (t
= elf_tdata (output_bfd
)->verref
;
5376 Elf_Internal_Vernaux
*a
;
5378 size
+= sizeof (Elf_External_Verneed
);
5380 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5381 size
+= sizeof (Elf_External_Vernaux
);
5385 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5386 if (s
->contents
== NULL
)
5390 for (t
= elf_tdata (output_bfd
)->verref
;
5395 Elf_Internal_Vernaux
*a
;
5399 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5402 t
->vn_version
= VER_NEED_CURRENT
;
5404 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5405 elf_dt_name (t
->vn_bfd
) != NULL
5406 ? elf_dt_name (t
->vn_bfd
)
5407 : basename (t
->vn_bfd
->filename
),
5409 if (indx
== (bfd_size_type
) -1)
5412 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5413 if (t
->vn_nextref
== NULL
)
5416 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5417 + caux
* sizeof (Elf_External_Vernaux
));
5419 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5420 (Elf_External_Verneed
*) p
);
5421 p
+= sizeof (Elf_External_Verneed
);
5423 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5425 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5426 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5427 a
->vna_nodename
, FALSE
);
5428 if (indx
== (bfd_size_type
) -1)
5431 if (a
->vna_nextptr
== NULL
)
5434 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5436 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5437 (Elf_External_Vernaux
*) p
);
5438 p
+= sizeof (Elf_External_Vernaux
);
5442 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5443 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5446 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5450 /* Assign dynsym indicies. In a shared library we generate a
5451 section symbol for each output section, which come first.
5452 Next come all of the back-end allocated local dynamic syms,
5453 followed by the rest of the global symbols. */
5455 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5457 /* Work out the size of the symbol version section. */
5458 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5459 BFD_ASSERT (s
!= NULL
);
5460 if (dynsymcount
== 0
5461 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
5462 && !info
->create_default_symver
))
5464 _bfd_strip_section_from_output (info
, s
);
5465 /* The DYNSYMCOUNT might have changed if we were going to
5466 output a dynamic symbol table entry for S. */
5467 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5471 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5472 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5473 if (s
->contents
== NULL
)
5476 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5480 /* Set the size of the .dynsym and .hash sections. We counted
5481 the number of dynamic symbols in elf_link_add_object_symbols.
5482 We will build the contents of .dynsym and .hash when we build
5483 the final symbol table, because until then we do not know the
5484 correct value to give the symbols. We built the .dynstr
5485 section as we went along in elf_link_add_object_symbols. */
5486 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5487 BFD_ASSERT (s
!= NULL
);
5488 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5489 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5490 if (s
->contents
== NULL
&& s
->size
!= 0)
5493 if (dynsymcount
!= 0)
5495 Elf_Internal_Sym isym
;
5497 /* The first entry in .dynsym is a dummy symbol. */
5504 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5507 /* Compute the size of the hashing table. As a side effect this
5508 computes the hash values for all the names we export. */
5509 bucketcount
= compute_bucket_count (info
);
5511 s
= bfd_get_section_by_name (dynobj
, ".hash");
5512 BFD_ASSERT (s
!= NULL
);
5513 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5514 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5515 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5516 if (s
->contents
== NULL
)
5519 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5520 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5521 s
->contents
+ hash_entry_size
);
5523 elf_hash_table (info
)->bucketcount
= bucketcount
;
5525 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5526 BFD_ASSERT (s
!= NULL
);
5528 elf_finalize_dynstr (output_bfd
, info
);
5530 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5532 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5533 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5540 /* Final phase of ELF linker. */
5542 /* A structure we use to avoid passing large numbers of arguments. */
5544 struct elf_final_link_info
5546 /* General link information. */
5547 struct bfd_link_info
*info
;
5550 /* Symbol string table. */
5551 struct bfd_strtab_hash
*symstrtab
;
5552 /* .dynsym section. */
5553 asection
*dynsym_sec
;
5554 /* .hash section. */
5556 /* symbol version section (.gnu.version). */
5557 asection
*symver_sec
;
5558 /* Buffer large enough to hold contents of any section. */
5560 /* Buffer large enough to hold external relocs of any section. */
5561 void *external_relocs
;
5562 /* Buffer large enough to hold internal relocs of any section. */
5563 Elf_Internal_Rela
*internal_relocs
;
5564 /* Buffer large enough to hold external local symbols of any input
5566 bfd_byte
*external_syms
;
5567 /* And a buffer for symbol section indices. */
5568 Elf_External_Sym_Shndx
*locsym_shndx
;
5569 /* Buffer large enough to hold internal local symbols of any input
5571 Elf_Internal_Sym
*internal_syms
;
5572 /* Array large enough to hold a symbol index for each local symbol
5573 of any input BFD. */
5575 /* Array large enough to hold a section pointer for each local
5576 symbol of any input BFD. */
5577 asection
**sections
;
5578 /* Buffer to hold swapped out symbols. */
5580 /* And one for symbol section indices. */
5581 Elf_External_Sym_Shndx
*symshndxbuf
;
5582 /* Number of swapped out symbols in buffer. */
5583 size_t symbuf_count
;
5584 /* Number of symbols which fit in symbuf. */
5586 /* And same for symshndxbuf. */
5587 size_t shndxbuf_size
;
5590 /* This struct is used to pass information to elf_link_output_extsym. */
5592 struct elf_outext_info
5595 bfd_boolean localsyms
;
5596 struct elf_final_link_info
*finfo
;
5599 /* When performing a relocatable link, the input relocations are
5600 preserved. But, if they reference global symbols, the indices
5601 referenced must be updated. Update all the relocations in
5602 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5605 elf_link_adjust_relocs (bfd
*abfd
,
5606 Elf_Internal_Shdr
*rel_hdr
,
5608 struct elf_link_hash_entry
**rel_hash
)
5611 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5613 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5614 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5615 bfd_vma r_type_mask
;
5618 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5620 swap_in
= bed
->s
->swap_reloc_in
;
5621 swap_out
= bed
->s
->swap_reloc_out
;
5623 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5625 swap_in
= bed
->s
->swap_reloca_in
;
5626 swap_out
= bed
->s
->swap_reloca_out
;
5631 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5634 if (bed
->s
->arch_size
== 32)
5641 r_type_mask
= 0xffffffff;
5645 erela
= rel_hdr
->contents
;
5646 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5648 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5651 if (*rel_hash
== NULL
)
5654 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5656 (*swap_in
) (abfd
, erela
, irela
);
5657 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5658 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5659 | (irela
[j
].r_info
& r_type_mask
));
5660 (*swap_out
) (abfd
, irela
, erela
);
5664 struct elf_link_sort_rela
5670 enum elf_reloc_type_class type
;
5671 /* We use this as an array of size int_rels_per_ext_rel. */
5672 Elf_Internal_Rela rela
[1];
5676 elf_link_sort_cmp1 (const void *A
, const void *B
)
5678 const struct elf_link_sort_rela
*a
= A
;
5679 const struct elf_link_sort_rela
*b
= B
;
5680 int relativea
, relativeb
;
5682 relativea
= a
->type
== reloc_class_relative
;
5683 relativeb
= b
->type
== reloc_class_relative
;
5685 if (relativea
< relativeb
)
5687 if (relativea
> relativeb
)
5689 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5691 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5693 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5695 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5701 elf_link_sort_cmp2 (const void *A
, const void *B
)
5703 const struct elf_link_sort_rela
*a
= A
;
5704 const struct elf_link_sort_rela
*b
= B
;
5707 if (a
->u
.offset
< b
->u
.offset
)
5709 if (a
->u
.offset
> b
->u
.offset
)
5711 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5712 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5717 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5719 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5725 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5728 bfd_size_type count
, size
;
5729 size_t i
, ret
, sort_elt
, ext_size
;
5730 bfd_byte
*sort
, *s_non_relative
, *p
;
5731 struct elf_link_sort_rela
*sq
;
5732 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5733 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5734 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5735 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5736 struct bfd_link_order
*lo
;
5739 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5740 if (reldyn
== NULL
|| reldyn
->size
== 0)
5742 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5743 if (reldyn
== NULL
|| reldyn
->size
== 0)
5745 ext_size
= bed
->s
->sizeof_rel
;
5746 swap_in
= bed
->s
->swap_reloc_in
;
5747 swap_out
= bed
->s
->swap_reloc_out
;
5751 ext_size
= bed
->s
->sizeof_rela
;
5752 swap_in
= bed
->s
->swap_reloca_in
;
5753 swap_out
= bed
->s
->swap_reloca_out
;
5755 count
= reldyn
->size
/ ext_size
;
5758 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5759 if (lo
->type
== bfd_indirect_link_order
)
5761 asection
*o
= lo
->u
.indirect
.section
;
5765 if (size
!= reldyn
->size
)
5768 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5769 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5770 sort
= bfd_zmalloc (sort_elt
* count
);
5773 (*info
->callbacks
->warning
)
5774 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5778 if (bed
->s
->arch_size
== 32)
5779 r_sym_mask
= ~(bfd_vma
) 0xff;
5781 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5783 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5784 if (lo
->type
== bfd_indirect_link_order
)
5786 bfd_byte
*erel
, *erelend
;
5787 asection
*o
= lo
->u
.indirect
.section
;
5789 if (o
->contents
== NULL
&& o
->size
!= 0)
5791 /* This is a reloc section that is being handled as a normal
5792 section. See bfd_section_from_shdr. We can't combine
5793 relocs in this case. */
5798 erelend
= o
->contents
+ o
->size
;
5799 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5800 while (erel
< erelend
)
5802 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5803 (*swap_in
) (abfd
, erel
, s
->rela
);
5804 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5805 s
->u
.sym_mask
= r_sym_mask
;
5811 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5813 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5815 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5816 if (s
->type
!= reloc_class_relative
)
5822 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5823 for (; i
< count
; i
++, p
+= sort_elt
)
5825 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5826 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5828 sp
->u
.offset
= sq
->rela
->r_offset
;
5831 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5833 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5834 if (lo
->type
== bfd_indirect_link_order
)
5836 bfd_byte
*erel
, *erelend
;
5837 asection
*o
= lo
->u
.indirect
.section
;
5840 erelend
= o
->contents
+ o
->size
;
5841 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5842 while (erel
< erelend
)
5844 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5845 (*swap_out
) (abfd
, s
->rela
, erel
);
5856 /* Flush the output symbols to the file. */
5859 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5860 const struct elf_backend_data
*bed
)
5862 if (finfo
->symbuf_count
> 0)
5864 Elf_Internal_Shdr
*hdr
;
5868 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5869 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5870 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5871 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5872 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5875 hdr
->sh_size
+= amt
;
5876 finfo
->symbuf_count
= 0;
5882 /* Add a symbol to the output symbol table. */
5885 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5887 Elf_Internal_Sym
*elfsym
,
5888 asection
*input_sec
,
5889 struct elf_link_hash_entry
*h
)
5892 Elf_External_Sym_Shndx
*destshndx
;
5893 bfd_boolean (*output_symbol_hook
)
5894 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5895 struct elf_link_hash_entry
*);
5896 const struct elf_backend_data
*bed
;
5898 bed
= get_elf_backend_data (finfo
->output_bfd
);
5899 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5900 if (output_symbol_hook
!= NULL
)
5902 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5906 if (name
== NULL
|| *name
== '\0')
5907 elfsym
->st_name
= 0;
5908 else if (input_sec
->flags
& SEC_EXCLUDE
)
5909 elfsym
->st_name
= 0;
5912 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5914 if (elfsym
->st_name
== (unsigned long) -1)
5918 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5920 if (! elf_link_flush_output_syms (finfo
, bed
))
5924 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5925 destshndx
= finfo
->symshndxbuf
;
5926 if (destshndx
!= NULL
)
5928 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5932 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5933 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5934 if (destshndx
== NULL
)
5936 memset ((char *) destshndx
+ amt
, 0, amt
);
5937 finfo
->shndxbuf_size
*= 2;
5939 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5942 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5943 finfo
->symbuf_count
+= 1;
5944 bfd_get_symcount (finfo
->output_bfd
) += 1;
5949 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5950 allowing an unsatisfied unversioned symbol in the DSO to match a
5951 versioned symbol that would normally require an explicit version.
5952 We also handle the case that a DSO references a hidden symbol
5953 which may be satisfied by a versioned symbol in another DSO. */
5956 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5957 const struct elf_backend_data
*bed
,
5958 struct elf_link_hash_entry
*h
)
5961 struct elf_link_loaded_list
*loaded
;
5963 if (!is_elf_hash_table (info
->hash
))
5966 switch (h
->root
.type
)
5972 case bfd_link_hash_undefined
:
5973 case bfd_link_hash_undefweak
:
5974 abfd
= h
->root
.u
.undef
.abfd
;
5975 if ((abfd
->flags
& DYNAMIC
) == 0
5976 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5980 case bfd_link_hash_defined
:
5981 case bfd_link_hash_defweak
:
5982 abfd
= h
->root
.u
.def
.section
->owner
;
5985 case bfd_link_hash_common
:
5986 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5989 BFD_ASSERT (abfd
!= NULL
);
5991 for (loaded
= elf_hash_table (info
)->loaded
;
5993 loaded
= loaded
->next
)
5996 Elf_Internal_Shdr
*hdr
;
5997 bfd_size_type symcount
;
5998 bfd_size_type extsymcount
;
5999 bfd_size_type extsymoff
;
6000 Elf_Internal_Shdr
*versymhdr
;
6001 Elf_Internal_Sym
*isym
;
6002 Elf_Internal_Sym
*isymend
;
6003 Elf_Internal_Sym
*isymbuf
;
6004 Elf_External_Versym
*ever
;
6005 Elf_External_Versym
*extversym
;
6007 input
= loaded
->abfd
;
6009 /* We check each DSO for a possible hidden versioned definition. */
6011 || (input
->flags
& DYNAMIC
) == 0
6012 || elf_dynversym (input
) == 0)
6015 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6017 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6018 if (elf_bad_symtab (input
))
6020 extsymcount
= symcount
;
6025 extsymcount
= symcount
- hdr
->sh_info
;
6026 extsymoff
= hdr
->sh_info
;
6029 if (extsymcount
== 0)
6032 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6034 if (isymbuf
== NULL
)
6037 /* Read in any version definitions. */
6038 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6039 extversym
= bfd_malloc (versymhdr
->sh_size
);
6040 if (extversym
== NULL
)
6043 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6044 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6045 != versymhdr
->sh_size
))
6053 ever
= extversym
+ extsymoff
;
6054 isymend
= isymbuf
+ extsymcount
;
6055 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6058 Elf_Internal_Versym iver
;
6059 unsigned short version_index
;
6061 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6062 || isym
->st_shndx
== SHN_UNDEF
)
6065 name
= bfd_elf_string_from_elf_section (input
,
6068 if (strcmp (name
, h
->root
.root
.string
) != 0)
6071 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6073 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6075 /* If we have a non-hidden versioned sym, then it should
6076 have provided a definition for the undefined sym. */
6080 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6081 if (version_index
== 1 || version_index
== 2)
6083 /* This is the base or first version. We can use it. */
6097 /* Add an external symbol to the symbol table. This is called from
6098 the hash table traversal routine. When generating a shared object,
6099 we go through the symbol table twice. The first time we output
6100 anything that might have been forced to local scope in a version
6101 script. The second time we output the symbols that are still
6105 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6107 struct elf_outext_info
*eoinfo
= data
;
6108 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6110 Elf_Internal_Sym sym
;
6111 asection
*input_sec
;
6112 const struct elf_backend_data
*bed
;
6114 if (h
->root
.type
== bfd_link_hash_warning
)
6116 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6117 if (h
->root
.type
== bfd_link_hash_new
)
6121 /* Decide whether to output this symbol in this pass. */
6122 if (eoinfo
->localsyms
)
6124 if (!h
->forced_local
)
6129 if (h
->forced_local
)
6133 bed
= get_elf_backend_data (finfo
->output_bfd
);
6135 /* If we have an undefined symbol reference here then it must have
6136 come from a shared library that is being linked in. (Undefined
6137 references in regular files have already been handled). If we
6138 are reporting errors for this situation then do so now. */
6139 if (h
->root
.type
== bfd_link_hash_undefined
6142 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6143 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6145 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6146 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6147 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6149 eoinfo
->failed
= TRUE
;
6154 /* We should also warn if a forced local symbol is referenced from
6155 shared libraries. */
6156 if (! finfo
->info
->relocatable
6157 && (! finfo
->info
->shared
)
6162 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6164 (*_bfd_error_handler
)
6165 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6166 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6167 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6169 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6170 ? "hidden" : "local",
6171 h
->root
.root
.string
);
6172 eoinfo
->failed
= TRUE
;
6176 /* We don't want to output symbols that have never been mentioned by
6177 a regular file, or that we have been told to strip. However, if
6178 h->indx is set to -2, the symbol is used by a reloc and we must
6182 else if ((h
->def_dynamic
6187 else if (finfo
->info
->strip
== strip_all
)
6189 else if (finfo
->info
->strip
== strip_some
6190 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6191 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6193 else if (finfo
->info
->strip_discarded
6194 && (h
->root
.type
== bfd_link_hash_defined
6195 || h
->root
.type
== bfd_link_hash_defweak
)
6196 && elf_discarded_section (h
->root
.u
.def
.section
))
6201 /* If we're stripping it, and it's not a dynamic symbol, there's
6202 nothing else to do unless it is a forced local symbol. */
6205 && !h
->forced_local
)
6209 sym
.st_size
= h
->size
;
6210 sym
.st_other
= h
->other
;
6211 if (h
->forced_local
)
6212 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6213 else if (h
->root
.type
== bfd_link_hash_undefweak
6214 || h
->root
.type
== bfd_link_hash_defweak
)
6215 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6217 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6219 switch (h
->root
.type
)
6222 case bfd_link_hash_new
:
6223 case bfd_link_hash_warning
:
6227 case bfd_link_hash_undefined
:
6228 case bfd_link_hash_undefweak
:
6229 input_sec
= bfd_und_section_ptr
;
6230 sym
.st_shndx
= SHN_UNDEF
;
6233 case bfd_link_hash_defined
:
6234 case bfd_link_hash_defweak
:
6236 input_sec
= h
->root
.u
.def
.section
;
6237 if (input_sec
->output_section
!= NULL
)
6240 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6241 input_sec
->output_section
);
6242 if (sym
.st_shndx
== SHN_BAD
)
6244 (*_bfd_error_handler
)
6245 (_("%B: could not find output section %A for input section %A"),
6246 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6247 eoinfo
->failed
= TRUE
;
6251 /* ELF symbols in relocatable files are section relative,
6252 but in nonrelocatable files they are virtual
6254 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6255 if (! finfo
->info
->relocatable
)
6257 sym
.st_value
+= input_sec
->output_section
->vma
;
6258 if (h
->type
== STT_TLS
)
6260 /* STT_TLS symbols are relative to PT_TLS segment
6262 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6263 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6269 BFD_ASSERT (input_sec
->owner
== NULL
6270 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6271 sym
.st_shndx
= SHN_UNDEF
;
6272 input_sec
= bfd_und_section_ptr
;
6277 case bfd_link_hash_common
:
6278 input_sec
= h
->root
.u
.c
.p
->section
;
6279 sym
.st_shndx
= SHN_COMMON
;
6280 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6283 case bfd_link_hash_indirect
:
6284 /* These symbols are created by symbol versioning. They point
6285 to the decorated version of the name. For example, if the
6286 symbol foo@@GNU_1.2 is the default, which should be used when
6287 foo is used with no version, then we add an indirect symbol
6288 foo which points to foo@@GNU_1.2. We ignore these symbols,
6289 since the indirected symbol is already in the hash table. */
6293 /* Give the processor backend a chance to tweak the symbol value,
6294 and also to finish up anything that needs to be done for this
6295 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6296 forced local syms when non-shared is due to a historical quirk. */
6297 if ((h
->dynindx
!= -1
6299 && ((finfo
->info
->shared
6300 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6301 || h
->root
.type
!= bfd_link_hash_undefweak
))
6302 || !h
->forced_local
)
6303 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6305 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6306 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6308 eoinfo
->failed
= TRUE
;
6313 /* If we are marking the symbol as undefined, and there are no
6314 non-weak references to this symbol from a regular object, then
6315 mark the symbol as weak undefined; if there are non-weak
6316 references, mark the symbol as strong. We can't do this earlier,
6317 because it might not be marked as undefined until the
6318 finish_dynamic_symbol routine gets through with it. */
6319 if (sym
.st_shndx
== SHN_UNDEF
6321 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6322 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6326 if (h
->ref_regular_nonweak
)
6327 bindtype
= STB_GLOBAL
;
6329 bindtype
= STB_WEAK
;
6330 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6333 /* If a non-weak symbol with non-default visibility is not defined
6334 locally, it is a fatal error. */
6335 if (! finfo
->info
->relocatable
6336 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6337 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6338 && h
->root
.type
== bfd_link_hash_undefined
6341 (*_bfd_error_handler
)
6342 (_("%B: %s symbol `%s' isn't defined"),
6344 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6346 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6347 ? "internal" : "hidden",
6348 h
->root
.root
.string
);
6349 eoinfo
->failed
= TRUE
;
6353 /* If this symbol should be put in the .dynsym section, then put it
6354 there now. We already know the symbol index. We also fill in
6355 the entry in the .hash section. */
6356 if (h
->dynindx
!= -1
6357 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6361 size_t hash_entry_size
;
6362 bfd_byte
*bucketpos
;
6366 sym
.st_name
= h
->dynstr_index
;
6367 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6368 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6370 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6371 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6373 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6374 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6375 + (bucket
+ 2) * hash_entry_size
);
6376 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6377 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6378 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6379 ((bfd_byte
*) finfo
->hash_sec
->contents
6380 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6382 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6384 Elf_Internal_Versym iversym
;
6385 Elf_External_Versym
*eversym
;
6387 if (!h
->def_regular
)
6389 if (h
->verinfo
.verdef
== NULL
)
6390 iversym
.vs_vers
= 0;
6392 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6396 if (h
->verinfo
.vertree
== NULL
)
6397 iversym
.vs_vers
= 1;
6399 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6400 if (finfo
->info
->create_default_symver
)
6405 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6407 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6408 eversym
+= h
->dynindx
;
6409 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6413 /* If we're stripping it, then it was just a dynamic symbol, and
6414 there's nothing else to do. */
6415 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6418 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6420 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6422 eoinfo
->failed
= TRUE
;
6429 /* Return TRUE if special handling is done for relocs in SEC against
6430 symbols defined in discarded sections. */
6433 elf_section_ignore_discarded_relocs (asection
*sec
)
6435 const struct elf_backend_data
*bed
;
6437 switch (sec
->sec_info_type
)
6439 case ELF_INFO_TYPE_STABS
:
6440 case ELF_INFO_TYPE_EH_FRAME
:
6446 bed
= get_elf_backend_data (sec
->owner
);
6447 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6448 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6454 enum action_discarded
6460 /* Return a mask saying how ld should treat relocations in SEC against
6461 symbols defined in discarded sections. If this function returns
6462 COMPLAIN set, ld will issue a warning message. If this function
6463 returns PRETEND set, and the discarded section was link-once and the
6464 same size as the kept link-once section, ld will pretend that the
6465 symbol was actually defined in the kept section. Otherwise ld will
6466 zero the reloc (at least that is the intent, but some cooperation by
6467 the target dependent code is needed, particularly for REL targets). */
6470 elf_action_discarded (asection
*sec
)
6472 if (sec
->flags
& SEC_DEBUGGING
)
6475 if (strcmp (".eh_frame", sec
->name
) == 0)
6478 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6481 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6484 if (strcmp (".fixup", sec
->name
) == 0)
6487 return COMPLAIN
| PRETEND
;
6490 /* Find a match between a section and a member of a section group. */
6493 match_group_member (asection
*sec
, asection
*group
)
6495 asection
*first
= elf_next_in_group (group
);
6496 asection
*s
= first
;
6500 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6510 /* Link an input file into the linker output file. This function
6511 handles all the sections and relocations of the input file at once.
6512 This is so that we only have to read the local symbols once, and
6513 don't have to keep them in memory. */
6516 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6518 bfd_boolean (*relocate_section
)
6519 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6520 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6522 Elf_Internal_Shdr
*symtab_hdr
;
6525 Elf_Internal_Sym
*isymbuf
;
6526 Elf_Internal_Sym
*isym
;
6527 Elf_Internal_Sym
*isymend
;
6529 asection
**ppsection
;
6531 const struct elf_backend_data
*bed
;
6532 bfd_boolean emit_relocs
;
6533 struct elf_link_hash_entry
**sym_hashes
;
6535 output_bfd
= finfo
->output_bfd
;
6536 bed
= get_elf_backend_data (output_bfd
);
6537 relocate_section
= bed
->elf_backend_relocate_section
;
6539 /* If this is a dynamic object, we don't want to do anything here:
6540 we don't want the local symbols, and we don't want the section
6542 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6545 emit_relocs
= (finfo
->info
->relocatable
6546 || finfo
->info
->emitrelocations
6547 || bed
->elf_backend_emit_relocs
);
6549 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6550 if (elf_bad_symtab (input_bfd
))
6552 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6557 locsymcount
= symtab_hdr
->sh_info
;
6558 extsymoff
= symtab_hdr
->sh_info
;
6561 /* Read the local symbols. */
6562 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6563 if (isymbuf
== NULL
&& locsymcount
!= 0)
6565 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6566 finfo
->internal_syms
,
6567 finfo
->external_syms
,
6568 finfo
->locsym_shndx
);
6569 if (isymbuf
== NULL
)
6573 /* Find local symbol sections and adjust values of symbols in
6574 SEC_MERGE sections. Write out those local symbols we know are
6575 going into the output file. */
6576 isymend
= isymbuf
+ locsymcount
;
6577 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6579 isym
++, pindex
++, ppsection
++)
6583 Elf_Internal_Sym osym
;
6587 if (elf_bad_symtab (input_bfd
))
6589 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6596 if (isym
->st_shndx
== SHN_UNDEF
)
6597 isec
= bfd_und_section_ptr
;
6598 else if (isym
->st_shndx
< SHN_LORESERVE
6599 || isym
->st_shndx
> SHN_HIRESERVE
)
6601 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6603 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6604 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6606 _bfd_merged_section_offset (output_bfd
, &isec
,
6607 elf_section_data (isec
)->sec_info
,
6610 else if (isym
->st_shndx
== SHN_ABS
)
6611 isec
= bfd_abs_section_ptr
;
6612 else if (isym
->st_shndx
== SHN_COMMON
)
6613 isec
= bfd_com_section_ptr
;
6622 /* Don't output the first, undefined, symbol. */
6623 if (ppsection
== finfo
->sections
)
6626 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6628 /* We never output section symbols. Instead, we use the
6629 section symbol of the corresponding section in the output
6634 /* If we are stripping all symbols, we don't want to output this
6636 if (finfo
->info
->strip
== strip_all
)
6639 /* If we are discarding all local symbols, we don't want to
6640 output this one. If we are generating a relocatable output
6641 file, then some of the local symbols may be required by
6642 relocs; we output them below as we discover that they are
6644 if (finfo
->info
->discard
== discard_all
)
6647 /* If this symbol is defined in a section which we are
6648 discarding, we don't need to keep it, but note that
6649 linker_mark is only reliable for sections that have contents.
6650 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6651 as well as linker_mark. */
6652 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6654 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6655 || (! finfo
->info
->relocatable
6656 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6659 /* Get the name of the symbol. */
6660 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6665 /* See if we are discarding symbols with this name. */
6666 if ((finfo
->info
->strip
== strip_some
6667 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6669 || (((finfo
->info
->discard
== discard_sec_merge
6670 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6671 || finfo
->info
->discard
== discard_l
)
6672 && bfd_is_local_label_name (input_bfd
, name
)))
6675 /* If we get here, we are going to output this symbol. */
6679 /* Adjust the section index for the output file. */
6680 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6681 isec
->output_section
);
6682 if (osym
.st_shndx
== SHN_BAD
)
6685 *pindex
= bfd_get_symcount (output_bfd
);
6687 /* ELF symbols in relocatable files are section relative, but
6688 in executable files they are virtual addresses. Note that
6689 this code assumes that all ELF sections have an associated
6690 BFD section with a reasonable value for output_offset; below
6691 we assume that they also have a reasonable value for
6692 output_section. Any special sections must be set up to meet
6693 these requirements. */
6694 osym
.st_value
+= isec
->output_offset
;
6695 if (! finfo
->info
->relocatable
)
6697 osym
.st_value
+= isec
->output_section
->vma
;
6698 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6700 /* STT_TLS symbols are relative to PT_TLS segment base. */
6701 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6702 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6706 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6710 /* Relocate the contents of each section. */
6711 sym_hashes
= elf_sym_hashes (input_bfd
);
6712 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6716 if (! o
->linker_mark
)
6718 /* This section was omitted from the link. */
6722 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6723 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6726 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6728 /* Section was created by _bfd_elf_link_create_dynamic_sections
6733 /* Get the contents of the section. They have been cached by a
6734 relaxation routine. Note that o is a section in an input
6735 file, so the contents field will not have been set by any of
6736 the routines which work on output files. */
6737 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6738 contents
= elf_section_data (o
)->this_hdr
.contents
;
6741 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6743 contents
= finfo
->contents
;
6744 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6748 if ((o
->flags
& SEC_RELOC
) != 0)
6750 Elf_Internal_Rela
*internal_relocs
;
6751 bfd_vma r_type_mask
;
6754 /* Get the swapped relocs. */
6756 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6757 finfo
->internal_relocs
, FALSE
);
6758 if (internal_relocs
== NULL
6759 && o
->reloc_count
> 0)
6762 if (bed
->s
->arch_size
== 32)
6769 r_type_mask
= 0xffffffff;
6773 /* Run through the relocs looking for any against symbols
6774 from discarded sections and section symbols from
6775 removed link-once sections. Complain about relocs
6776 against discarded sections. Zero relocs against removed
6777 link-once sections. Preserve debug information as much
6779 if (!elf_section_ignore_discarded_relocs (o
))
6781 Elf_Internal_Rela
*rel
, *relend
;
6782 unsigned int action
= elf_action_discarded (o
);
6784 rel
= internal_relocs
;
6785 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6786 for ( ; rel
< relend
; rel
++)
6788 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6789 asection
**ps
, *sec
;
6790 struct elf_link_hash_entry
*h
= NULL
;
6791 const char *sym_name
;
6793 if (r_symndx
== STN_UNDEF
)
6796 if (r_symndx
>= locsymcount
6797 || (elf_bad_symtab (input_bfd
)
6798 && finfo
->sections
[r_symndx
] == NULL
))
6800 h
= sym_hashes
[r_symndx
- extsymoff
];
6801 while (h
->root
.type
== bfd_link_hash_indirect
6802 || h
->root
.type
== bfd_link_hash_warning
)
6803 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6805 if (h
->root
.type
!= bfd_link_hash_defined
6806 && h
->root
.type
!= bfd_link_hash_defweak
)
6809 ps
= &h
->root
.u
.def
.section
;
6810 sym_name
= h
->root
.root
.string
;
6814 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6815 ps
= &finfo
->sections
[r_symndx
];
6816 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6819 /* Complain if the definition comes from a
6820 discarded section. */
6821 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6825 BFD_ASSERT (r_symndx
!= 0);
6826 if (action
& COMPLAIN
)
6828 (*_bfd_error_handler
)
6829 (_("`%s' referenced in section `%A' of %B: "
6830 "defined in discarded section `%A' of %B\n"),
6831 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6834 /* Try to do the best we can to support buggy old
6835 versions of gcc. If we've warned, or this is
6836 debugging info, pretend that the symbol is
6837 really defined in the kept linkonce section.
6838 FIXME: This is quite broken. Modifying the
6839 symbol here means we will be changing all later
6840 uses of the symbol, not just in this section.
6841 The only thing that makes this half reasonable
6842 is that we warn in non-debug sections, and
6843 debug sections tend to come after other
6845 kept
= sec
->kept_section
;
6846 if (kept
!= NULL
&& (action
& PRETEND
))
6848 if (elf_sec_group (sec
) != NULL
)
6849 kept
= match_group_member (sec
, kept
);
6851 && sec
->size
== kept
->size
)
6858 /* Remove the symbol reference from the reloc, but
6859 don't kill the reloc completely. This is so that
6860 a zero value will be written into the section,
6861 which may have non-zero contents put there by the
6862 assembler. Zero in things like an eh_frame fde
6863 pc_begin allows stack unwinders to recognize the
6865 rel
->r_info
&= r_type_mask
;
6871 /* Relocate the section by invoking a back end routine.
6873 The back end routine is responsible for adjusting the
6874 section contents as necessary, and (if using Rela relocs
6875 and generating a relocatable output file) adjusting the
6876 reloc addend as necessary.
6878 The back end routine does not have to worry about setting
6879 the reloc address or the reloc symbol index.
6881 The back end routine is given a pointer to the swapped in
6882 internal symbols, and can access the hash table entries
6883 for the external symbols via elf_sym_hashes (input_bfd).
6885 When generating relocatable output, the back end routine
6886 must handle STB_LOCAL/STT_SECTION symbols specially. The
6887 output symbol is going to be a section symbol
6888 corresponding to the output section, which will require
6889 the addend to be adjusted. */
6891 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6892 input_bfd
, o
, contents
,
6900 Elf_Internal_Rela
*irela
;
6901 Elf_Internal_Rela
*irelaend
;
6902 bfd_vma last_offset
;
6903 struct elf_link_hash_entry
**rel_hash
;
6904 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6905 unsigned int next_erel
;
6906 bfd_boolean (*reloc_emitter
)
6907 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6908 bfd_boolean rela_normal
;
6910 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6911 rela_normal
= (bed
->rela_normal
6912 && (input_rel_hdr
->sh_entsize
6913 == bed
->s
->sizeof_rela
));
6915 /* Adjust the reloc addresses and symbol indices. */
6917 irela
= internal_relocs
;
6918 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6919 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6920 + elf_section_data (o
->output_section
)->rel_count
6921 + elf_section_data (o
->output_section
)->rel_count2
);
6922 last_offset
= o
->output_offset
;
6923 if (!finfo
->info
->relocatable
)
6924 last_offset
+= o
->output_section
->vma
;
6925 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6927 unsigned long r_symndx
;
6929 Elf_Internal_Sym sym
;
6931 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6937 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6940 if (irela
->r_offset
>= (bfd_vma
) -2)
6942 /* This is a reloc for a deleted entry or somesuch.
6943 Turn it into an R_*_NONE reloc, at the same
6944 offset as the last reloc. elf_eh_frame.c and
6945 elf_bfd_discard_info rely on reloc offsets
6947 irela
->r_offset
= last_offset
;
6949 irela
->r_addend
= 0;
6953 irela
->r_offset
+= o
->output_offset
;
6955 /* Relocs in an executable have to be virtual addresses. */
6956 if (!finfo
->info
->relocatable
)
6957 irela
->r_offset
+= o
->output_section
->vma
;
6959 last_offset
= irela
->r_offset
;
6961 r_symndx
= irela
->r_info
>> r_sym_shift
;
6962 if (r_symndx
== STN_UNDEF
)
6965 if (r_symndx
>= locsymcount
6966 || (elf_bad_symtab (input_bfd
)
6967 && finfo
->sections
[r_symndx
] == NULL
))
6969 struct elf_link_hash_entry
*rh
;
6972 /* This is a reloc against a global symbol. We
6973 have not yet output all the local symbols, so
6974 we do not know the symbol index of any global
6975 symbol. We set the rel_hash entry for this
6976 reloc to point to the global hash table entry
6977 for this symbol. The symbol index is then
6978 set at the end of bfd_elf_final_link. */
6979 indx
= r_symndx
- extsymoff
;
6980 rh
= elf_sym_hashes (input_bfd
)[indx
];
6981 while (rh
->root
.type
== bfd_link_hash_indirect
6982 || rh
->root
.type
== bfd_link_hash_warning
)
6983 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6985 /* Setting the index to -2 tells
6986 elf_link_output_extsym that this symbol is
6988 BFD_ASSERT (rh
->indx
< 0);
6996 /* This is a reloc against a local symbol. */
6999 sym
= isymbuf
[r_symndx
];
7000 sec
= finfo
->sections
[r_symndx
];
7001 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7003 /* I suppose the backend ought to fill in the
7004 section of any STT_SECTION symbol against a
7005 processor specific section. */
7007 if (bfd_is_abs_section (sec
))
7009 else if (sec
== NULL
|| sec
->owner
== NULL
)
7011 bfd_set_error (bfd_error_bad_value
);
7016 asection
*osec
= sec
->output_section
;
7018 /* If we have discarded a section, the output
7019 section will be the absolute section. In
7020 case of discarded link-once and discarded
7021 SEC_MERGE sections, use the kept section. */
7022 if (bfd_is_abs_section (osec
)
7023 && sec
->kept_section
!= NULL
7024 && sec
->kept_section
->output_section
!= NULL
)
7026 osec
= sec
->kept_section
->output_section
;
7027 irela
->r_addend
-= osec
->vma
;
7030 if (!bfd_is_abs_section (osec
))
7032 r_symndx
= osec
->target_index
;
7033 BFD_ASSERT (r_symndx
!= 0);
7037 /* Adjust the addend according to where the
7038 section winds up in the output section. */
7040 irela
->r_addend
+= sec
->output_offset
;
7044 if (finfo
->indices
[r_symndx
] == -1)
7046 unsigned long shlink
;
7050 if (finfo
->info
->strip
== strip_all
)
7052 /* You can't do ld -r -s. */
7053 bfd_set_error (bfd_error_invalid_operation
);
7057 /* This symbol was skipped earlier, but
7058 since it is needed by a reloc, we
7059 must output it now. */
7060 shlink
= symtab_hdr
->sh_link
;
7061 name
= (bfd_elf_string_from_elf_section
7062 (input_bfd
, shlink
, sym
.st_name
));
7066 osec
= sec
->output_section
;
7068 _bfd_elf_section_from_bfd_section (output_bfd
,
7070 if (sym
.st_shndx
== SHN_BAD
)
7073 sym
.st_value
+= sec
->output_offset
;
7074 if (! finfo
->info
->relocatable
)
7076 sym
.st_value
+= osec
->vma
;
7077 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7079 /* STT_TLS symbols are relative to PT_TLS
7081 BFD_ASSERT (elf_hash_table (finfo
->info
)
7083 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7088 finfo
->indices
[r_symndx
]
7089 = bfd_get_symcount (output_bfd
);
7091 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7096 r_symndx
= finfo
->indices
[r_symndx
];
7099 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7100 | (irela
->r_info
& r_type_mask
));
7103 /* Swap out the relocs. */
7104 if (bed
->elf_backend_emit_relocs
7105 && !(finfo
->info
->relocatable
7106 || finfo
->info
->emitrelocations
))
7107 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7109 reloc_emitter
= _bfd_elf_link_output_relocs
;
7111 if (input_rel_hdr
->sh_size
!= 0
7112 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7116 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7117 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7119 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7120 * bed
->s
->int_rels_per_ext_rel
);
7121 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7128 /* Write out the modified section contents. */
7129 if (bed
->elf_backend_write_section
7130 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7132 /* Section written out. */
7134 else switch (o
->sec_info_type
)
7136 case ELF_INFO_TYPE_STABS
:
7137 if (! (_bfd_write_section_stabs
7139 &elf_hash_table (finfo
->info
)->stab_info
,
7140 o
, &elf_section_data (o
)->sec_info
, contents
)))
7143 case ELF_INFO_TYPE_MERGE
:
7144 if (! _bfd_write_merged_section (output_bfd
, o
,
7145 elf_section_data (o
)->sec_info
))
7148 case ELF_INFO_TYPE_EH_FRAME
:
7150 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7157 if (! (o
->flags
& SEC_EXCLUDE
)
7158 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7160 (file_ptr
) o
->output_offset
,
7171 /* Generate a reloc when linking an ELF file. This is a reloc
7172 requested by the linker, and does come from any input file. This
7173 is used to build constructor and destructor tables when linking
7177 elf_reloc_link_order (bfd
*output_bfd
,
7178 struct bfd_link_info
*info
,
7179 asection
*output_section
,
7180 struct bfd_link_order
*link_order
)
7182 reloc_howto_type
*howto
;
7186 struct elf_link_hash_entry
**rel_hash_ptr
;
7187 Elf_Internal_Shdr
*rel_hdr
;
7188 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7189 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7193 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7196 bfd_set_error (bfd_error_bad_value
);
7200 addend
= link_order
->u
.reloc
.p
->addend
;
7202 /* Figure out the symbol index. */
7203 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7204 + elf_section_data (output_section
)->rel_count
7205 + elf_section_data (output_section
)->rel_count2
);
7206 if (link_order
->type
== bfd_section_reloc_link_order
)
7208 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7209 BFD_ASSERT (indx
!= 0);
7210 *rel_hash_ptr
= NULL
;
7214 struct elf_link_hash_entry
*h
;
7216 /* Treat a reloc against a defined symbol as though it were
7217 actually against the section. */
7218 h
= ((struct elf_link_hash_entry
*)
7219 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7220 link_order
->u
.reloc
.p
->u
.name
,
7221 FALSE
, FALSE
, TRUE
));
7223 && (h
->root
.type
== bfd_link_hash_defined
7224 || h
->root
.type
== bfd_link_hash_defweak
))
7228 section
= h
->root
.u
.def
.section
;
7229 indx
= section
->output_section
->target_index
;
7230 *rel_hash_ptr
= NULL
;
7231 /* It seems that we ought to add the symbol value to the
7232 addend here, but in practice it has already been added
7233 because it was passed to constructor_callback. */
7234 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7238 /* Setting the index to -2 tells elf_link_output_extsym that
7239 this symbol is used by a reloc. */
7246 if (! ((*info
->callbacks
->unattached_reloc
)
7247 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7253 /* If this is an inplace reloc, we must write the addend into the
7255 if (howto
->partial_inplace
&& addend
!= 0)
7258 bfd_reloc_status_type rstat
;
7261 const char *sym_name
;
7263 size
= bfd_get_reloc_size (howto
);
7264 buf
= bfd_zmalloc (size
);
7267 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7274 case bfd_reloc_outofrange
:
7277 case bfd_reloc_overflow
:
7278 if (link_order
->type
== bfd_section_reloc_link_order
)
7279 sym_name
= bfd_section_name (output_bfd
,
7280 link_order
->u
.reloc
.p
->u
.section
);
7282 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7283 if (! ((*info
->callbacks
->reloc_overflow
)
7284 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7285 NULL
, (bfd_vma
) 0)))
7292 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7293 link_order
->offset
, size
);
7299 /* The address of a reloc is relative to the section in a
7300 relocatable file, and is a virtual address in an executable
7302 offset
= link_order
->offset
;
7303 if (! info
->relocatable
)
7304 offset
+= output_section
->vma
;
7306 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7308 irel
[i
].r_offset
= offset
;
7310 irel
[i
].r_addend
= 0;
7312 if (bed
->s
->arch_size
== 32)
7313 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7315 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7317 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7318 erel
= rel_hdr
->contents
;
7319 if (rel_hdr
->sh_type
== SHT_REL
)
7321 erel
+= (elf_section_data (output_section
)->rel_count
7322 * bed
->s
->sizeof_rel
);
7323 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7327 irel
[0].r_addend
= addend
;
7328 erel
+= (elf_section_data (output_section
)->rel_count
7329 * bed
->s
->sizeof_rela
);
7330 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7333 ++elf_section_data (output_section
)->rel_count
;
7339 /* Get the output vma of the section pointed to by the sh_link field. */
7342 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7344 Elf_Internal_Shdr
**elf_shdrp
;
7348 s
= p
->u
.indirect
.section
;
7349 elf_shdrp
= elf_elfsections (s
->owner
);
7350 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7351 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7353 The Intel C compiler generates SHT_IA_64_UNWIND with
7354 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7355 sh_info fields. Hence we could get the situation
7356 where elfsec is 0. */
7359 const struct elf_backend_data
*bed
7360 = get_elf_backend_data (s
->owner
);
7361 if (bed
->link_order_error_handler
)
7362 bed
->link_order_error_handler
7363 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7368 s
= elf_shdrp
[elfsec
]->bfd_section
;
7369 return s
->output_section
->vma
+ s
->output_offset
;
7374 /* Compare two sections based on the locations of the sections they are
7375 linked to. Used by elf_fixup_link_order. */
7378 compare_link_order (const void * a
, const void * b
)
7383 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7384 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7391 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7392 order as their linked sections. Returns false if this could not be done
7393 because an output section includes both ordered and unordered
7394 sections. Ideally we'd do this in the linker proper. */
7397 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7402 struct bfd_link_order
*p
;
7404 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7406 struct bfd_link_order
**sections
;
7412 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7414 if (p
->type
== bfd_indirect_link_order
7415 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7416 == bfd_target_elf_flavour
)
7417 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7419 s
= p
->u
.indirect
.section
;
7420 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7422 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7431 if (!seen_linkorder
)
7434 if (seen_other
&& seen_linkorder
)
7436 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7438 bfd_set_error (bfd_error_bad_value
);
7442 sections
= (struct bfd_link_order
**)
7443 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7446 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7448 sections
[seen_linkorder
++] = p
;
7450 /* Sort the input sections in the order of their linked section. */
7451 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7452 compare_link_order
);
7454 /* Change the offsets of the sections. */
7456 for (n
= 0; n
< seen_linkorder
; n
++)
7458 s
= sections
[n
]->u
.indirect
.section
;
7459 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7460 s
->output_offset
= offset
;
7461 sections
[n
]->offset
= offset
;
7462 offset
+= sections
[n
]->size
;
7469 /* Do the final step of an ELF link. */
7472 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7474 bfd_boolean dynamic
;
7475 bfd_boolean emit_relocs
;
7477 struct elf_final_link_info finfo
;
7478 register asection
*o
;
7479 register struct bfd_link_order
*p
;
7481 bfd_size_type max_contents_size
;
7482 bfd_size_type max_external_reloc_size
;
7483 bfd_size_type max_internal_reloc_count
;
7484 bfd_size_type max_sym_count
;
7485 bfd_size_type max_sym_shndx_count
;
7487 Elf_Internal_Sym elfsym
;
7489 Elf_Internal_Shdr
*symtab_hdr
;
7490 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7491 Elf_Internal_Shdr
*symstrtab_hdr
;
7492 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7493 struct elf_outext_info eoinfo
;
7495 size_t relativecount
= 0;
7496 asection
*reldyn
= 0;
7499 if (! is_elf_hash_table (info
->hash
))
7503 abfd
->flags
|= DYNAMIC
;
7505 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7506 dynobj
= elf_hash_table (info
)->dynobj
;
7508 emit_relocs
= (info
->relocatable
7509 || info
->emitrelocations
7510 || bed
->elf_backend_emit_relocs
);
7513 finfo
.output_bfd
= abfd
;
7514 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7515 if (finfo
.symstrtab
== NULL
)
7520 finfo
.dynsym_sec
= NULL
;
7521 finfo
.hash_sec
= NULL
;
7522 finfo
.symver_sec
= NULL
;
7526 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7527 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7528 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7529 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7530 /* Note that it is OK if symver_sec is NULL. */
7533 finfo
.contents
= NULL
;
7534 finfo
.external_relocs
= NULL
;
7535 finfo
.internal_relocs
= NULL
;
7536 finfo
.external_syms
= NULL
;
7537 finfo
.locsym_shndx
= NULL
;
7538 finfo
.internal_syms
= NULL
;
7539 finfo
.indices
= NULL
;
7540 finfo
.sections
= NULL
;
7541 finfo
.symbuf
= NULL
;
7542 finfo
.symshndxbuf
= NULL
;
7543 finfo
.symbuf_count
= 0;
7544 finfo
.shndxbuf_size
= 0;
7546 /* Count up the number of relocations we will output for each output
7547 section, so that we know the sizes of the reloc sections. We
7548 also figure out some maximum sizes. */
7549 max_contents_size
= 0;
7550 max_external_reloc_size
= 0;
7551 max_internal_reloc_count
= 0;
7553 max_sym_shndx_count
= 0;
7555 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7557 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7560 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7562 unsigned int reloc_count
= 0;
7563 struct bfd_elf_section_data
*esdi
= NULL
;
7564 unsigned int *rel_count1
;
7566 if (p
->type
== bfd_section_reloc_link_order
7567 || p
->type
== bfd_symbol_reloc_link_order
)
7569 else if (p
->type
== bfd_indirect_link_order
)
7573 sec
= p
->u
.indirect
.section
;
7574 esdi
= elf_section_data (sec
);
7576 /* Mark all sections which are to be included in the
7577 link. This will normally be every section. We need
7578 to do this so that we can identify any sections which
7579 the linker has decided to not include. */
7580 sec
->linker_mark
= TRUE
;
7582 if (sec
->flags
& SEC_MERGE
)
7585 if (info
->relocatable
|| info
->emitrelocations
)
7586 reloc_count
= sec
->reloc_count
;
7587 else if (bed
->elf_backend_count_relocs
)
7589 Elf_Internal_Rela
* relocs
;
7591 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7594 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7596 if (elf_section_data (o
)->relocs
!= relocs
)
7600 if (sec
->rawsize
> max_contents_size
)
7601 max_contents_size
= sec
->rawsize
;
7602 if (sec
->size
> max_contents_size
)
7603 max_contents_size
= sec
->size
;
7605 /* We are interested in just local symbols, not all
7607 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7608 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7612 if (elf_bad_symtab (sec
->owner
))
7613 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7614 / bed
->s
->sizeof_sym
);
7616 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7618 if (sym_count
> max_sym_count
)
7619 max_sym_count
= sym_count
;
7621 if (sym_count
> max_sym_shndx_count
7622 && elf_symtab_shndx (sec
->owner
) != 0)
7623 max_sym_shndx_count
= sym_count
;
7625 if ((sec
->flags
& SEC_RELOC
) != 0)
7629 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7630 if (ext_size
> max_external_reloc_size
)
7631 max_external_reloc_size
= ext_size
;
7632 if (sec
->reloc_count
> max_internal_reloc_count
)
7633 max_internal_reloc_count
= sec
->reloc_count
;
7638 if (reloc_count
== 0)
7641 o
->reloc_count
+= reloc_count
;
7643 /* MIPS may have a mix of REL and RELA relocs on sections.
7644 To support this curious ABI we keep reloc counts in
7645 elf_section_data too. We must be careful to add the
7646 relocations from the input section to the right output
7647 count. FIXME: Get rid of one count. We have
7648 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7649 rel_count1
= &esdo
->rel_count
;
7652 bfd_boolean same_size
;
7653 bfd_size_type entsize1
;
7655 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7656 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7657 || entsize1
== bed
->s
->sizeof_rela
);
7658 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7661 rel_count1
= &esdo
->rel_count2
;
7663 if (esdi
->rel_hdr2
!= NULL
)
7665 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7666 unsigned int alt_count
;
7667 unsigned int *rel_count2
;
7669 BFD_ASSERT (entsize2
!= entsize1
7670 && (entsize2
== bed
->s
->sizeof_rel
7671 || entsize2
== bed
->s
->sizeof_rela
));
7673 rel_count2
= &esdo
->rel_count2
;
7675 rel_count2
= &esdo
->rel_count
;
7677 /* The following is probably too simplistic if the
7678 backend counts output relocs unusually. */
7679 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7680 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7681 *rel_count2
+= alt_count
;
7682 reloc_count
-= alt_count
;
7685 *rel_count1
+= reloc_count
;
7688 if (o
->reloc_count
> 0)
7689 o
->flags
|= SEC_RELOC
;
7692 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7693 set it (this is probably a bug) and if it is set
7694 assign_section_numbers will create a reloc section. */
7695 o
->flags
&=~ SEC_RELOC
;
7698 /* If the SEC_ALLOC flag is not set, force the section VMA to
7699 zero. This is done in elf_fake_sections as well, but forcing
7700 the VMA to 0 here will ensure that relocs against these
7701 sections are handled correctly. */
7702 if ((o
->flags
& SEC_ALLOC
) == 0
7703 && ! o
->user_set_vma
)
7707 if (! info
->relocatable
&& merged
)
7708 elf_link_hash_traverse (elf_hash_table (info
),
7709 _bfd_elf_link_sec_merge_syms
, abfd
);
7711 /* Figure out the file positions for everything but the symbol table
7712 and the relocs. We set symcount to force assign_section_numbers
7713 to create a symbol table. */
7714 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7715 BFD_ASSERT (! abfd
->output_has_begun
);
7716 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7719 /* Set sizes, and assign file positions for reloc sections. */
7720 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7722 if ((o
->flags
& SEC_RELOC
) != 0)
7724 if (!(_bfd_elf_link_size_reloc_section
7725 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7728 if (elf_section_data (o
)->rel_hdr2
7729 && !(_bfd_elf_link_size_reloc_section
7730 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7734 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7735 to count upwards while actually outputting the relocations. */
7736 elf_section_data (o
)->rel_count
= 0;
7737 elf_section_data (o
)->rel_count2
= 0;
7740 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7742 /* We have now assigned file positions for all the sections except
7743 .symtab and .strtab. We start the .symtab section at the current
7744 file position, and write directly to it. We build the .strtab
7745 section in memory. */
7746 bfd_get_symcount (abfd
) = 0;
7747 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7748 /* sh_name is set in prep_headers. */
7749 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7750 /* sh_flags, sh_addr and sh_size all start off zero. */
7751 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7752 /* sh_link is set in assign_section_numbers. */
7753 /* sh_info is set below. */
7754 /* sh_offset is set just below. */
7755 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7757 off
= elf_tdata (abfd
)->next_file_pos
;
7758 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7760 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7761 incorrect. We do not yet know the size of the .symtab section.
7762 We correct next_file_pos below, after we do know the size. */
7764 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7765 continuously seeking to the right position in the file. */
7766 if (! info
->keep_memory
|| max_sym_count
< 20)
7767 finfo
.symbuf_size
= 20;
7769 finfo
.symbuf_size
= max_sym_count
;
7770 amt
= finfo
.symbuf_size
;
7771 amt
*= bed
->s
->sizeof_sym
;
7772 finfo
.symbuf
= bfd_malloc (amt
);
7773 if (finfo
.symbuf
== NULL
)
7775 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7777 /* Wild guess at number of output symbols. realloc'd as needed. */
7778 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7779 finfo
.shndxbuf_size
= amt
;
7780 amt
*= sizeof (Elf_External_Sym_Shndx
);
7781 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7782 if (finfo
.symshndxbuf
== NULL
)
7786 /* Start writing out the symbol table. The first symbol is always a
7788 if (info
->strip
!= strip_all
7791 elfsym
.st_value
= 0;
7794 elfsym
.st_other
= 0;
7795 elfsym
.st_shndx
= SHN_UNDEF
;
7796 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7802 /* Some standard ELF linkers do this, but we don't because it causes
7803 bootstrap comparison failures. */
7804 /* Output a file symbol for the output file as the second symbol.
7805 We output this even if we are discarding local symbols, although
7806 I'm not sure if this is correct. */
7807 elfsym
.st_value
= 0;
7809 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7810 elfsym
.st_other
= 0;
7811 elfsym
.st_shndx
= SHN_ABS
;
7812 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7813 &elfsym
, bfd_abs_section_ptr
, NULL
))
7817 /* Output a symbol for each section. We output these even if we are
7818 discarding local symbols, since they are used for relocs. These
7819 symbols have no names. We store the index of each one in the
7820 index field of the section, so that we can find it again when
7821 outputting relocs. */
7822 if (info
->strip
!= strip_all
7826 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7827 elfsym
.st_other
= 0;
7828 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7830 o
= bfd_section_from_elf_index (abfd
, i
);
7832 o
->target_index
= bfd_get_symcount (abfd
);
7833 elfsym
.st_shndx
= i
;
7834 if (info
->relocatable
|| o
== NULL
)
7835 elfsym
.st_value
= 0;
7837 elfsym
.st_value
= o
->vma
;
7838 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7840 if (i
== SHN_LORESERVE
- 1)
7841 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7845 /* Allocate some memory to hold information read in from the input
7847 if (max_contents_size
!= 0)
7849 finfo
.contents
= bfd_malloc (max_contents_size
);
7850 if (finfo
.contents
== NULL
)
7854 if (max_external_reloc_size
!= 0)
7856 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7857 if (finfo
.external_relocs
== NULL
)
7861 if (max_internal_reloc_count
!= 0)
7863 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7864 amt
*= sizeof (Elf_Internal_Rela
);
7865 finfo
.internal_relocs
= bfd_malloc (amt
);
7866 if (finfo
.internal_relocs
== NULL
)
7870 if (max_sym_count
!= 0)
7872 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7873 finfo
.external_syms
= bfd_malloc (amt
);
7874 if (finfo
.external_syms
== NULL
)
7877 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7878 finfo
.internal_syms
= bfd_malloc (amt
);
7879 if (finfo
.internal_syms
== NULL
)
7882 amt
= max_sym_count
* sizeof (long);
7883 finfo
.indices
= bfd_malloc (amt
);
7884 if (finfo
.indices
== NULL
)
7887 amt
= max_sym_count
* sizeof (asection
*);
7888 finfo
.sections
= bfd_malloc (amt
);
7889 if (finfo
.sections
== NULL
)
7893 if (max_sym_shndx_count
!= 0)
7895 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7896 finfo
.locsym_shndx
= bfd_malloc (amt
);
7897 if (finfo
.locsym_shndx
== NULL
)
7901 if (elf_hash_table (info
)->tls_sec
)
7903 bfd_vma base
, end
= 0;
7906 for (sec
= elf_hash_table (info
)->tls_sec
;
7907 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7910 bfd_vma size
= sec
->size
;
7912 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7914 struct bfd_link_order
*o
;
7916 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7917 if (size
< o
->offset
+ o
->size
)
7918 size
= o
->offset
+ o
->size
;
7920 end
= sec
->vma
+ size
;
7922 base
= elf_hash_table (info
)->tls_sec
->vma
;
7923 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7924 elf_hash_table (info
)->tls_size
= end
- base
;
7927 /* Reorder SHF_LINK_ORDER sections. */
7928 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7930 if (!elf_fixup_link_order (abfd
, o
))
7934 /* Since ELF permits relocations to be against local symbols, we
7935 must have the local symbols available when we do the relocations.
7936 Since we would rather only read the local symbols once, and we
7937 would rather not keep them in memory, we handle all the
7938 relocations for a single input file at the same time.
7940 Unfortunately, there is no way to know the total number of local
7941 symbols until we have seen all of them, and the local symbol
7942 indices precede the global symbol indices. This means that when
7943 we are generating relocatable output, and we see a reloc against
7944 a global symbol, we can not know the symbol index until we have
7945 finished examining all the local symbols to see which ones we are
7946 going to output. To deal with this, we keep the relocations in
7947 memory, and don't output them until the end of the link. This is
7948 an unfortunate waste of memory, but I don't see a good way around
7949 it. Fortunately, it only happens when performing a relocatable
7950 link, which is not the common case. FIXME: If keep_memory is set
7951 we could write the relocs out and then read them again; I don't
7952 know how bad the memory loss will be. */
7954 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7955 sub
->output_has_begun
= FALSE
;
7956 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7958 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7960 if (p
->type
== bfd_indirect_link_order
7961 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7962 == bfd_target_elf_flavour
)
7963 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7965 if (! sub
->output_has_begun
)
7967 if (! elf_link_input_bfd (&finfo
, sub
))
7969 sub
->output_has_begun
= TRUE
;
7972 else if (p
->type
== bfd_section_reloc_link_order
7973 || p
->type
== bfd_symbol_reloc_link_order
)
7975 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7980 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7986 /* Output any global symbols that got converted to local in a
7987 version script or due to symbol visibility. We do this in a
7988 separate step since ELF requires all local symbols to appear
7989 prior to any global symbols. FIXME: We should only do this if
7990 some global symbols were, in fact, converted to become local.
7991 FIXME: Will this work correctly with the Irix 5 linker? */
7992 eoinfo
.failed
= FALSE
;
7993 eoinfo
.finfo
= &finfo
;
7994 eoinfo
.localsyms
= TRUE
;
7995 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8000 /* That wrote out all the local symbols. Finish up the symbol table
8001 with the global symbols. Even if we want to strip everything we
8002 can, we still need to deal with those global symbols that got
8003 converted to local in a version script. */
8005 /* The sh_info field records the index of the first non local symbol. */
8006 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8009 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8011 Elf_Internal_Sym sym
;
8012 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8013 long last_local
= 0;
8015 /* Write out the section symbols for the output sections. */
8022 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8025 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8031 dynindx
= elf_section_data (s
)->dynindx
;
8034 indx
= elf_section_data (s
)->this_idx
;
8035 BFD_ASSERT (indx
> 0);
8036 sym
.st_shndx
= indx
;
8037 sym
.st_value
= s
->vma
;
8038 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8039 if (last_local
< dynindx
)
8040 last_local
= dynindx
;
8041 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8045 /* Write out the local dynsyms. */
8046 if (elf_hash_table (info
)->dynlocal
)
8048 struct elf_link_local_dynamic_entry
*e
;
8049 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8054 sym
.st_size
= e
->isym
.st_size
;
8055 sym
.st_other
= e
->isym
.st_other
;
8057 /* Copy the internal symbol as is.
8058 Note that we saved a word of storage and overwrote
8059 the original st_name with the dynstr_index. */
8062 if (e
->isym
.st_shndx
!= SHN_UNDEF
8063 && (e
->isym
.st_shndx
< SHN_LORESERVE
8064 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8066 s
= bfd_section_from_elf_index (e
->input_bfd
,
8070 elf_section_data (s
->output_section
)->this_idx
;
8071 sym
.st_value
= (s
->output_section
->vma
8073 + e
->isym
.st_value
);
8076 if (last_local
< e
->dynindx
)
8077 last_local
= e
->dynindx
;
8079 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8080 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8084 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8088 /* We get the global symbols from the hash table. */
8089 eoinfo
.failed
= FALSE
;
8090 eoinfo
.localsyms
= FALSE
;
8091 eoinfo
.finfo
= &finfo
;
8092 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8097 /* If backend needs to output some symbols not present in the hash
8098 table, do it now. */
8099 if (bed
->elf_backend_output_arch_syms
)
8101 typedef bfd_boolean (*out_sym_func
)
8102 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8103 struct elf_link_hash_entry
*);
8105 if (! ((*bed
->elf_backend_output_arch_syms
)
8106 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8110 /* Flush all symbols to the file. */
8111 if (! elf_link_flush_output_syms (&finfo
, bed
))
8114 /* Now we know the size of the symtab section. */
8115 off
+= symtab_hdr
->sh_size
;
8117 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8118 if (symtab_shndx_hdr
->sh_name
!= 0)
8120 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8121 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8122 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8123 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8124 symtab_shndx_hdr
->sh_size
= amt
;
8126 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8129 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8130 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8135 /* Finish up and write out the symbol string table (.strtab)
8137 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8138 /* sh_name was set in prep_headers. */
8139 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8140 symstrtab_hdr
->sh_flags
= 0;
8141 symstrtab_hdr
->sh_addr
= 0;
8142 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8143 symstrtab_hdr
->sh_entsize
= 0;
8144 symstrtab_hdr
->sh_link
= 0;
8145 symstrtab_hdr
->sh_info
= 0;
8146 /* sh_offset is set just below. */
8147 symstrtab_hdr
->sh_addralign
= 1;
8149 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8150 elf_tdata (abfd
)->next_file_pos
= off
;
8152 if (bfd_get_symcount (abfd
) > 0)
8154 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8155 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8159 /* Adjust the relocs to have the correct symbol indices. */
8160 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8162 if ((o
->flags
& SEC_RELOC
) == 0)
8165 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8166 elf_section_data (o
)->rel_count
,
8167 elf_section_data (o
)->rel_hashes
);
8168 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8169 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8170 elf_section_data (o
)->rel_count2
,
8171 (elf_section_data (o
)->rel_hashes
8172 + elf_section_data (o
)->rel_count
));
8174 /* Set the reloc_count field to 0 to prevent write_relocs from
8175 trying to swap the relocs out itself. */
8179 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8180 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8182 /* If we are linking against a dynamic object, or generating a
8183 shared library, finish up the dynamic linking information. */
8186 bfd_byte
*dyncon
, *dynconend
;
8188 /* Fix up .dynamic entries. */
8189 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8190 BFD_ASSERT (o
!= NULL
);
8192 dyncon
= o
->contents
;
8193 dynconend
= o
->contents
+ o
->size
;
8194 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8196 Elf_Internal_Dyn dyn
;
8200 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8207 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8209 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8211 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8212 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8215 dyn
.d_un
.d_val
= relativecount
;
8222 name
= info
->init_function
;
8225 name
= info
->fini_function
;
8228 struct elf_link_hash_entry
*h
;
8230 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8231 FALSE
, FALSE
, TRUE
);
8233 && (h
->root
.type
== bfd_link_hash_defined
8234 || h
->root
.type
== bfd_link_hash_defweak
))
8236 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8237 o
= h
->root
.u
.def
.section
;
8238 if (o
->output_section
!= NULL
)
8239 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8240 + o
->output_offset
);
8243 /* The symbol is imported from another shared
8244 library and does not apply to this one. */
8252 case DT_PREINIT_ARRAYSZ
:
8253 name
= ".preinit_array";
8255 case DT_INIT_ARRAYSZ
:
8256 name
= ".init_array";
8258 case DT_FINI_ARRAYSZ
:
8259 name
= ".fini_array";
8261 o
= bfd_get_section_by_name (abfd
, name
);
8264 (*_bfd_error_handler
)
8265 (_("%B: could not find output section %s"), abfd
, name
);
8269 (*_bfd_error_handler
)
8270 (_("warning: %s section has zero size"), name
);
8271 dyn
.d_un
.d_val
= o
->size
;
8274 case DT_PREINIT_ARRAY
:
8275 name
= ".preinit_array";
8278 name
= ".init_array";
8281 name
= ".fini_array";
8294 name
= ".gnu.version_d";
8297 name
= ".gnu.version_r";
8300 name
= ".gnu.version";
8302 o
= bfd_get_section_by_name (abfd
, name
);
8305 (*_bfd_error_handler
)
8306 (_("%B: could not find output section %s"), abfd
, name
);
8309 dyn
.d_un
.d_ptr
= o
->vma
;
8316 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8321 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8323 Elf_Internal_Shdr
*hdr
;
8325 hdr
= elf_elfsections (abfd
)[i
];
8326 if (hdr
->sh_type
== type
8327 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8329 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8330 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8333 if (dyn
.d_un
.d_val
== 0
8334 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8335 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8341 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8345 /* If we have created any dynamic sections, then output them. */
8348 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8351 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8353 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8355 || o
->output_section
== bfd_abs_section_ptr
)
8357 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8359 /* At this point, we are only interested in sections
8360 created by _bfd_elf_link_create_dynamic_sections. */
8363 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8365 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8367 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8369 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8371 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8373 (file_ptr
) o
->output_offset
,
8379 /* The contents of the .dynstr section are actually in a
8381 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8382 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8383 || ! _bfd_elf_strtab_emit (abfd
,
8384 elf_hash_table (info
)->dynstr
))
8390 if (info
->relocatable
)
8392 bfd_boolean failed
= FALSE
;
8394 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8399 /* If we have optimized stabs strings, output them. */
8400 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8402 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8406 if (info
->eh_frame_hdr
)
8408 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8412 if (finfo
.symstrtab
!= NULL
)
8413 _bfd_stringtab_free (finfo
.symstrtab
);
8414 if (finfo
.contents
!= NULL
)
8415 free (finfo
.contents
);
8416 if (finfo
.external_relocs
!= NULL
)
8417 free (finfo
.external_relocs
);
8418 if (finfo
.internal_relocs
!= NULL
)
8419 free (finfo
.internal_relocs
);
8420 if (finfo
.external_syms
!= NULL
)
8421 free (finfo
.external_syms
);
8422 if (finfo
.locsym_shndx
!= NULL
)
8423 free (finfo
.locsym_shndx
);
8424 if (finfo
.internal_syms
!= NULL
)
8425 free (finfo
.internal_syms
);
8426 if (finfo
.indices
!= NULL
)
8427 free (finfo
.indices
);
8428 if (finfo
.sections
!= NULL
)
8429 free (finfo
.sections
);
8430 if (finfo
.symbuf
!= NULL
)
8431 free (finfo
.symbuf
);
8432 if (finfo
.symshndxbuf
!= NULL
)
8433 free (finfo
.symshndxbuf
);
8434 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8436 if ((o
->flags
& SEC_RELOC
) != 0
8437 && elf_section_data (o
)->rel_hashes
!= NULL
)
8438 free (elf_section_data (o
)->rel_hashes
);
8441 elf_tdata (abfd
)->linker
= TRUE
;
8446 if (finfo
.symstrtab
!= NULL
)
8447 _bfd_stringtab_free (finfo
.symstrtab
);
8448 if (finfo
.contents
!= NULL
)
8449 free (finfo
.contents
);
8450 if (finfo
.external_relocs
!= NULL
)
8451 free (finfo
.external_relocs
);
8452 if (finfo
.internal_relocs
!= NULL
)
8453 free (finfo
.internal_relocs
);
8454 if (finfo
.external_syms
!= NULL
)
8455 free (finfo
.external_syms
);
8456 if (finfo
.locsym_shndx
!= NULL
)
8457 free (finfo
.locsym_shndx
);
8458 if (finfo
.internal_syms
!= NULL
)
8459 free (finfo
.internal_syms
);
8460 if (finfo
.indices
!= NULL
)
8461 free (finfo
.indices
);
8462 if (finfo
.sections
!= NULL
)
8463 free (finfo
.sections
);
8464 if (finfo
.symbuf
!= NULL
)
8465 free (finfo
.symbuf
);
8466 if (finfo
.symshndxbuf
!= NULL
)
8467 free (finfo
.symshndxbuf
);
8468 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8470 if ((o
->flags
& SEC_RELOC
) != 0
8471 && elf_section_data (o
)->rel_hashes
!= NULL
)
8472 free (elf_section_data (o
)->rel_hashes
);
8478 /* Garbage collect unused sections. */
8480 /* The mark phase of garbage collection. For a given section, mark
8481 it and any sections in this section's group, and all the sections
8482 which define symbols to which it refers. */
8484 typedef asection
* (*gc_mark_hook_fn
)
8485 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8486 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8489 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8491 gc_mark_hook_fn gc_mark_hook
)
8494 asection
*group_sec
;
8498 /* Mark all the sections in the group. */
8499 group_sec
= elf_section_data (sec
)->next_in_group
;
8500 if (group_sec
&& !group_sec
->gc_mark
)
8501 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8504 /* Look through the section relocs. */
8506 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8508 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8509 Elf_Internal_Shdr
*symtab_hdr
;
8510 struct elf_link_hash_entry
**sym_hashes
;
8513 bfd
*input_bfd
= sec
->owner
;
8514 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8515 Elf_Internal_Sym
*isym
= NULL
;
8518 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8519 sym_hashes
= elf_sym_hashes (input_bfd
);
8521 /* Read the local symbols. */
8522 if (elf_bad_symtab (input_bfd
))
8524 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8528 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8530 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8531 if (isym
== NULL
&& nlocsyms
!= 0)
8533 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8539 /* Read the relocations. */
8540 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8542 if (relstart
== NULL
)
8547 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8549 if (bed
->s
->arch_size
== 32)
8554 for (rel
= relstart
; rel
< relend
; rel
++)
8556 unsigned long r_symndx
;
8558 struct elf_link_hash_entry
*h
;
8560 r_symndx
= rel
->r_info
>> r_sym_shift
;
8564 if (r_symndx
>= nlocsyms
8565 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8567 h
= sym_hashes
[r_symndx
- extsymoff
];
8568 while (h
->root
.type
== bfd_link_hash_indirect
8569 || h
->root
.type
== bfd_link_hash_warning
)
8570 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8571 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8575 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8578 if (rsec
&& !rsec
->gc_mark
)
8580 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8582 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8591 if (elf_section_data (sec
)->relocs
!= relstart
)
8594 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8596 if (! info
->keep_memory
)
8599 symtab_hdr
->contents
= (unsigned char *) isym
;
8606 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8609 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8613 if (h
->root
.type
== bfd_link_hash_warning
)
8614 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8616 if (h
->dynindx
!= -1
8617 && ((h
->root
.type
!= bfd_link_hash_defined
8618 && h
->root
.type
!= bfd_link_hash_defweak
)
8619 || h
->root
.u
.def
.section
->gc_mark
))
8620 h
->dynindx
= (*idx
)++;
8625 /* The sweep phase of garbage collection. Remove all garbage sections. */
8627 typedef bfd_boolean (*gc_sweep_hook_fn
)
8628 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8631 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8635 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8639 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8642 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8644 /* Keep debug and special sections. */
8645 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8646 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8652 /* Skip sweeping sections already excluded. */
8653 if (o
->flags
& SEC_EXCLUDE
)
8656 /* Since this is early in the link process, it is simple
8657 to remove a section from the output. */
8658 o
->flags
|= SEC_EXCLUDE
;
8660 /* But we also have to update some of the relocation
8661 info we collected before. */
8663 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8665 Elf_Internal_Rela
*internal_relocs
;
8669 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8671 if (internal_relocs
== NULL
)
8674 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8676 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8677 free (internal_relocs
);
8685 /* Remove the symbols that were in the swept sections from the dynamic
8686 symbol table. GCFIXME: Anyone know how to get them out of the
8687 static symbol table as well? */
8691 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8693 elf_hash_table (info
)->dynsymcount
= i
;
8699 /* Propagate collected vtable information. This is called through
8700 elf_link_hash_traverse. */
8703 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8705 if (h
->root
.type
== bfd_link_hash_warning
)
8706 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8708 /* Those that are not vtables. */
8709 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8712 /* Those vtables that do not have parents, we cannot merge. */
8713 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8716 /* If we've already been done, exit. */
8717 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8720 /* Make sure the parent's table is up to date. */
8721 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8723 if (h
->vtable
->used
== NULL
)
8725 /* None of this table's entries were referenced. Re-use the
8727 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8728 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8733 bfd_boolean
*cu
, *pu
;
8735 /* Or the parent's entries into ours. */
8736 cu
= h
->vtable
->used
;
8738 pu
= h
->vtable
->parent
->vtable
->used
;
8741 const struct elf_backend_data
*bed
;
8742 unsigned int log_file_align
;
8744 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8745 log_file_align
= bed
->s
->log_file_align
;
8746 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8761 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8764 bfd_vma hstart
, hend
;
8765 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8766 const struct elf_backend_data
*bed
;
8767 unsigned int log_file_align
;
8769 if (h
->root
.type
== bfd_link_hash_warning
)
8770 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8772 /* Take care of both those symbols that do not describe vtables as
8773 well as those that are not loaded. */
8774 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8777 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8778 || h
->root
.type
== bfd_link_hash_defweak
);
8780 sec
= h
->root
.u
.def
.section
;
8781 hstart
= h
->root
.u
.def
.value
;
8782 hend
= hstart
+ h
->size
;
8784 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8786 return *(bfd_boolean
*) okp
= FALSE
;
8787 bed
= get_elf_backend_data (sec
->owner
);
8788 log_file_align
= bed
->s
->log_file_align
;
8790 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8792 for (rel
= relstart
; rel
< relend
; ++rel
)
8793 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8795 /* If the entry is in use, do nothing. */
8797 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8799 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8800 if (h
->vtable
->used
[entry
])
8803 /* Otherwise, kill it. */
8804 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8810 /* Mark sections containing dynamically referenced symbols. This is called
8811 through elf_link_hash_traverse. */
8814 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8815 void *okp ATTRIBUTE_UNUSED
)
8817 if (h
->root
.type
== bfd_link_hash_warning
)
8818 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8820 if ((h
->root
.type
== bfd_link_hash_defined
8821 || h
->root
.type
== bfd_link_hash_defweak
)
8823 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8828 /* Do mark and sweep of unused sections. */
8831 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8833 bfd_boolean ok
= TRUE
;
8835 asection
* (*gc_mark_hook
)
8836 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8837 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8839 if (!get_elf_backend_data (abfd
)->can_gc_sections
8840 || info
->relocatable
8841 || info
->emitrelocations
8843 || !is_elf_hash_table (info
->hash
))
8845 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8849 /* Apply transitive closure to the vtable entry usage info. */
8850 elf_link_hash_traverse (elf_hash_table (info
),
8851 elf_gc_propagate_vtable_entries_used
,
8856 /* Kill the vtable relocations that were not used. */
8857 elf_link_hash_traverse (elf_hash_table (info
),
8858 elf_gc_smash_unused_vtentry_relocs
,
8863 /* Mark dynamically referenced symbols. */
8864 if (elf_hash_table (info
)->dynamic_sections_created
)
8865 elf_link_hash_traverse (elf_hash_table (info
),
8866 elf_gc_mark_dynamic_ref_symbol
,
8871 /* Grovel through relocs to find out who stays ... */
8872 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8873 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8877 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8880 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8882 if (o
->flags
& SEC_KEEP
)
8884 /* _bfd_elf_discard_section_eh_frame knows how to discard
8885 orphaned FDEs so don't mark sections referenced by the
8886 EH frame section. */
8887 if (strcmp (o
->name
, ".eh_frame") == 0)
8889 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8895 /* ... and mark SEC_EXCLUDE for those that go. */
8896 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8902 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8905 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8907 struct elf_link_hash_entry
*h
,
8910 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8911 struct elf_link_hash_entry
**search
, *child
;
8912 bfd_size_type extsymcount
;
8913 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8915 /* The sh_info field of the symtab header tells us where the
8916 external symbols start. We don't care about the local symbols at
8918 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8919 if (!elf_bad_symtab (abfd
))
8920 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8922 sym_hashes
= elf_sym_hashes (abfd
);
8923 sym_hashes_end
= sym_hashes
+ extsymcount
;
8925 /* Hunt down the child symbol, which is in this section at the same
8926 offset as the relocation. */
8927 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8929 if ((child
= *search
) != NULL
8930 && (child
->root
.type
== bfd_link_hash_defined
8931 || child
->root
.type
== bfd_link_hash_defweak
)
8932 && child
->root
.u
.def
.section
== sec
8933 && child
->root
.u
.def
.value
== offset
)
8937 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8938 abfd
, sec
, (unsigned long) offset
);
8939 bfd_set_error (bfd_error_invalid_operation
);
8945 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8951 /* This *should* only be the absolute section. It could potentially
8952 be that someone has defined a non-global vtable though, which
8953 would be bad. It isn't worth paging in the local symbols to be
8954 sure though; that case should simply be handled by the assembler. */
8956 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8959 child
->vtable
->parent
= h
;
8964 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8967 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8968 asection
*sec ATTRIBUTE_UNUSED
,
8969 struct elf_link_hash_entry
*h
,
8972 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8973 unsigned int log_file_align
= bed
->s
->log_file_align
;
8977 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8982 if (addend
>= h
->vtable
->size
)
8984 size_t size
, bytes
, file_align
;
8985 bfd_boolean
*ptr
= h
->vtable
->used
;
8987 /* While the symbol is undefined, we have to be prepared to handle
8989 file_align
= 1 << log_file_align
;
8990 if (h
->root
.type
== bfd_link_hash_undefined
)
8991 size
= addend
+ file_align
;
8997 /* Oops! We've got a reference past the defined end of
8998 the table. This is probably a bug -- shall we warn? */
8999 size
= addend
+ file_align
;
9002 size
= (size
+ file_align
- 1) & -file_align
;
9004 /* Allocate one extra entry for use as a "done" flag for the
9005 consolidation pass. */
9006 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9010 ptr
= bfd_realloc (ptr
- 1, bytes
);
9016 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9017 * sizeof (bfd_boolean
));
9018 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9022 ptr
= bfd_zmalloc (bytes
);
9027 /* And arrange for that done flag to be at index -1. */
9028 h
->vtable
->used
= ptr
+ 1;
9029 h
->vtable
->size
= size
;
9032 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9037 struct alloc_got_off_arg
{
9039 unsigned int got_elt_size
;
9042 /* We need a special top-level link routine to convert got reference counts
9043 to real got offsets. */
9046 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9048 struct alloc_got_off_arg
*gofarg
= arg
;
9050 if (h
->root
.type
== bfd_link_hash_warning
)
9051 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9053 if (h
->got
.refcount
> 0)
9055 h
->got
.offset
= gofarg
->gotoff
;
9056 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9059 h
->got
.offset
= (bfd_vma
) -1;
9064 /* And an accompanying bit to work out final got entry offsets once
9065 we're done. Should be called from final_link. */
9068 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9069 struct bfd_link_info
*info
)
9072 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9074 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9075 struct alloc_got_off_arg gofarg
;
9077 if (! is_elf_hash_table (info
->hash
))
9080 /* The GOT offset is relative to the .got section, but the GOT header is
9081 put into the .got.plt section, if the backend uses it. */
9082 if (bed
->want_got_plt
)
9085 gotoff
= bed
->got_header_size
;
9087 /* Do the local .got entries first. */
9088 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9090 bfd_signed_vma
*local_got
;
9091 bfd_size_type j
, locsymcount
;
9092 Elf_Internal_Shdr
*symtab_hdr
;
9094 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9097 local_got
= elf_local_got_refcounts (i
);
9101 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9102 if (elf_bad_symtab (i
))
9103 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9105 locsymcount
= symtab_hdr
->sh_info
;
9107 for (j
= 0; j
< locsymcount
; ++j
)
9109 if (local_got
[j
] > 0)
9111 local_got
[j
] = gotoff
;
9112 gotoff
+= got_elt_size
;
9115 local_got
[j
] = (bfd_vma
) -1;
9119 /* Then the global .got entries. .plt refcounts are handled by
9120 adjust_dynamic_symbol */
9121 gofarg
.gotoff
= gotoff
;
9122 gofarg
.got_elt_size
= got_elt_size
;
9123 elf_link_hash_traverse (elf_hash_table (info
),
9124 elf_gc_allocate_got_offsets
,
9129 /* Many folk need no more in the way of final link than this, once
9130 got entry reference counting is enabled. */
9133 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9135 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9138 /* Invoke the regular ELF backend linker to do all the work. */
9139 return bfd_elf_final_link (abfd
, info
);
9143 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9145 struct elf_reloc_cookie
*rcookie
= cookie
;
9147 if (rcookie
->bad_symtab
)
9148 rcookie
->rel
= rcookie
->rels
;
9150 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9152 unsigned long r_symndx
;
9154 if (! rcookie
->bad_symtab
)
9155 if (rcookie
->rel
->r_offset
> offset
)
9157 if (rcookie
->rel
->r_offset
!= offset
)
9160 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9161 if (r_symndx
== SHN_UNDEF
)
9164 if (r_symndx
>= rcookie
->locsymcount
9165 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9167 struct elf_link_hash_entry
*h
;
9169 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9171 while (h
->root
.type
== bfd_link_hash_indirect
9172 || h
->root
.type
== bfd_link_hash_warning
)
9173 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9175 if ((h
->root
.type
== bfd_link_hash_defined
9176 || h
->root
.type
== bfd_link_hash_defweak
)
9177 && elf_discarded_section (h
->root
.u
.def
.section
))
9184 /* It's not a relocation against a global symbol,
9185 but it could be a relocation against a local
9186 symbol for a discarded section. */
9188 Elf_Internal_Sym
*isym
;
9190 /* Need to: get the symbol; get the section. */
9191 isym
= &rcookie
->locsyms
[r_symndx
];
9192 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9194 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9195 if (isec
!= NULL
&& elf_discarded_section (isec
))
9204 /* Discard unneeded references to discarded sections.
9205 Returns TRUE if any section's size was changed. */
9206 /* This function assumes that the relocations are in sorted order,
9207 which is true for all known assemblers. */
9210 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9212 struct elf_reloc_cookie cookie
;
9213 asection
*stab
, *eh
;
9214 Elf_Internal_Shdr
*symtab_hdr
;
9215 const struct elf_backend_data
*bed
;
9218 bfd_boolean ret
= FALSE
;
9220 if (info
->traditional_format
9221 || !is_elf_hash_table (info
->hash
))
9224 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9226 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9229 bed
= get_elf_backend_data (abfd
);
9231 if ((abfd
->flags
& DYNAMIC
) != 0)
9234 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9235 if (info
->relocatable
9238 || bfd_is_abs_section (eh
->output_section
))))
9241 stab
= bfd_get_section_by_name (abfd
, ".stab");
9244 || bfd_is_abs_section (stab
->output_section
)
9245 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9250 && bed
->elf_backend_discard_info
== NULL
)
9253 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9255 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9256 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9257 if (cookie
.bad_symtab
)
9259 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9260 cookie
.extsymoff
= 0;
9264 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9265 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9268 if (bed
->s
->arch_size
== 32)
9269 cookie
.r_sym_shift
= 8;
9271 cookie
.r_sym_shift
= 32;
9273 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9274 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9276 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9277 cookie
.locsymcount
, 0,
9279 if (cookie
.locsyms
== NULL
)
9286 count
= stab
->reloc_count
;
9288 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9290 if (cookie
.rels
!= NULL
)
9292 cookie
.rel
= cookie
.rels
;
9293 cookie
.relend
= cookie
.rels
;
9294 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9295 if (_bfd_discard_section_stabs (abfd
, stab
,
9296 elf_section_data (stab
)->sec_info
,
9297 bfd_elf_reloc_symbol_deleted_p
,
9300 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9308 count
= eh
->reloc_count
;
9310 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9312 cookie
.rel
= cookie
.rels
;
9313 cookie
.relend
= cookie
.rels
;
9314 if (cookie
.rels
!= NULL
)
9315 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9317 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9318 bfd_elf_reloc_symbol_deleted_p
,
9322 if (cookie
.rels
!= NULL
9323 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9327 if (bed
->elf_backend_discard_info
!= NULL
9328 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9331 if (cookie
.locsyms
!= NULL
9332 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9334 if (! info
->keep_memory
)
9335 free (cookie
.locsyms
);
9337 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9341 if (info
->eh_frame_hdr
9342 && !info
->relocatable
9343 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9350 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9353 const char *name
, *p
;
9354 struct bfd_section_already_linked
*l
;
9355 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9358 /* A single member comdat group section may be discarded by a
9359 linkonce section. See below. */
9360 if (sec
->output_section
== bfd_abs_section_ptr
)
9365 /* Check if it belongs to a section group. */
9366 group
= elf_sec_group (sec
);
9368 /* Return if it isn't a linkonce section nor a member of a group. A
9369 comdat group section also has SEC_LINK_ONCE set. */
9370 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9375 /* If this is the member of a single member comdat group, check if
9376 the group should be discarded. */
9377 if (elf_next_in_group (sec
) == sec
9378 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9384 /* FIXME: When doing a relocatable link, we may have trouble
9385 copying relocations in other sections that refer to local symbols
9386 in the section being discarded. Those relocations will have to
9387 be converted somehow; as of this writing I'm not sure that any of
9388 the backends handle that correctly.
9390 It is tempting to instead not discard link once sections when
9391 doing a relocatable link (technically, they should be discarded
9392 whenever we are building constructors). However, that fails,
9393 because the linker winds up combining all the link once sections
9394 into a single large link once section, which defeats the purpose
9395 of having link once sections in the first place.
9397 Also, not merging link once sections in a relocatable link
9398 causes trouble for MIPS ELF, which relies on link once semantics
9399 to handle the .reginfo section correctly. */
9401 name
= bfd_get_section_name (abfd
, sec
);
9403 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9404 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9409 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9411 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9413 /* We may have 3 different sections on the list: group section,
9414 comdat section and linkonce section. SEC may be a linkonce or
9415 group section. We match a group section with a group section,
9416 a linkonce section with a linkonce section, and ignore comdat
9418 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9419 && strcmp (name
, l
->sec
->name
) == 0
9420 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9422 /* The section has already been linked. See if we should
9424 switch (flags
& SEC_LINK_DUPLICATES
)
9429 case SEC_LINK_DUPLICATES_DISCARD
:
9432 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9433 (*_bfd_error_handler
)
9434 (_("%B: ignoring duplicate section `%A'\n"),
9438 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9439 if (sec
->size
!= l
->sec
->size
)
9440 (*_bfd_error_handler
)
9441 (_("%B: duplicate section `%A' has different size\n"),
9445 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9446 if (sec
->size
!= l
->sec
->size
)
9447 (*_bfd_error_handler
)
9448 (_("%B: duplicate section `%A' has different size\n"),
9450 else if (sec
->size
!= 0)
9452 bfd_byte
*sec_contents
, *l_sec_contents
;
9454 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9455 (*_bfd_error_handler
)
9456 (_("%B: warning: could not read contents of section `%A'\n"),
9458 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9460 (*_bfd_error_handler
)
9461 (_("%B: warning: could not read contents of section `%A'\n"),
9462 l
->sec
->owner
, l
->sec
);
9463 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9464 (*_bfd_error_handler
)
9465 (_("%B: warning: duplicate section `%A' has different contents\n"),
9469 free (sec_contents
);
9471 free (l_sec_contents
);
9476 /* Set the output_section field so that lang_add_section
9477 does not create a lang_input_section structure for this
9478 section. Since there might be a symbol in the section
9479 being discarded, we must retain a pointer to the section
9480 which we are really going to use. */
9481 sec
->output_section
= bfd_abs_section_ptr
;
9482 sec
->kept_section
= l
->sec
;
9484 if (flags
& SEC_GROUP
)
9486 asection
*first
= elf_next_in_group (sec
);
9487 asection
*s
= first
;
9491 s
->output_section
= bfd_abs_section_ptr
;
9492 /* Record which group discards it. */
9493 s
->kept_section
= l
->sec
;
9494 s
= elf_next_in_group (s
);
9495 /* These lists are circular. */
9507 /* If this is the member of a single member comdat group and the
9508 group hasn't be discarded, we check if it matches a linkonce
9509 section. We only record the discarded comdat group. Otherwise
9510 the undiscarded group will be discarded incorrectly later since
9511 itself has been recorded. */
9512 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9513 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9514 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9515 && bfd_elf_match_symbols_in_sections (l
->sec
,
9516 elf_next_in_group (sec
)))
9518 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9519 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9520 group
->output_section
= bfd_abs_section_ptr
;
9527 /* There is no direct match. But for linkonce section, we should
9528 check if there is a match with comdat group member. We always
9529 record the linkonce section, discarded or not. */
9530 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9531 if (l
->sec
->flags
& SEC_GROUP
)
9533 asection
*first
= elf_next_in_group (l
->sec
);
9536 && elf_next_in_group (first
) == first
9537 && bfd_elf_match_symbols_in_sections (first
, sec
))
9539 sec
->output_section
= bfd_abs_section_ptr
;
9540 sec
->kept_section
= l
->sec
;
9545 /* This is the first section with this name. Record it. */
9546 bfd_section_already_linked_table_insert (already_linked_list
, sec
);