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 goto error_free_sym
;
3353 /* Read in the symbol versions, but don't bother to convert them
3354 to internal format. */
3355 if (elf_dynversym (abfd
) != 0)
3357 Elf_Internal_Shdr
*versymhdr
;
3359 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3360 extversym
= bfd_malloc (versymhdr
->sh_size
);
3361 if (extversym
== NULL
)
3362 goto error_free_sym
;
3363 amt
= versymhdr
->sh_size
;
3364 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3365 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3366 goto error_free_vers
;
3372 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3373 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3375 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3382 struct elf_link_hash_entry
*h
;
3383 bfd_boolean definition
;
3384 bfd_boolean size_change_ok
;
3385 bfd_boolean type_change_ok
;
3386 bfd_boolean new_weakdef
;
3387 bfd_boolean override
;
3388 unsigned int old_alignment
;
3393 flags
= BSF_NO_FLAGS
;
3395 value
= isym
->st_value
;
3398 bind
= ELF_ST_BIND (isym
->st_info
);
3399 if (bind
== STB_LOCAL
)
3401 /* This should be impossible, since ELF requires that all
3402 global symbols follow all local symbols, and that sh_info
3403 point to the first global symbol. Unfortunately, Irix 5
3407 else if (bind
== STB_GLOBAL
)
3409 if (isym
->st_shndx
!= SHN_UNDEF
3410 && isym
->st_shndx
!= SHN_COMMON
)
3413 else if (bind
== STB_WEAK
)
3417 /* Leave it up to the processor backend. */
3420 if (isym
->st_shndx
== SHN_UNDEF
)
3421 sec
= bfd_und_section_ptr
;
3422 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3424 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3426 sec
= bfd_abs_section_ptr
;
3427 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3430 else if (isym
->st_shndx
== SHN_ABS
)
3431 sec
= bfd_abs_section_ptr
;
3432 else if (isym
->st_shndx
== SHN_COMMON
)
3434 sec
= bfd_com_section_ptr
;
3435 /* What ELF calls the size we call the value. What ELF
3436 calls the value we call the alignment. */
3437 value
= isym
->st_size
;
3441 /* Leave it up to the processor backend. */
3444 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3447 goto error_free_vers
;
3449 if (isym
->st_shndx
== SHN_COMMON
3450 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3452 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3456 tcomm
= bfd_make_section (abfd
, ".tcommon");
3458 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3460 | SEC_LINKER_CREATED
3461 | SEC_THREAD_LOCAL
)))
3462 goto error_free_vers
;
3466 else if (add_symbol_hook
)
3468 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3470 goto error_free_vers
;
3472 /* The hook function sets the name to NULL if this symbol
3473 should be skipped for some reason. */
3478 /* Sanity check that all possibilities were handled. */
3481 bfd_set_error (bfd_error_bad_value
);
3482 goto error_free_vers
;
3485 if (bfd_is_und_section (sec
)
3486 || bfd_is_com_section (sec
))
3491 size_change_ok
= FALSE
;
3492 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3496 if (is_elf_hash_table (hash_table
))
3498 Elf_Internal_Versym iver
;
3499 unsigned int vernum
= 0;
3504 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3505 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3507 /* If this is a hidden symbol, or if it is not version
3508 1, we append the version name to the symbol name.
3509 However, we do not modify a non-hidden absolute
3510 symbol, because it might be the version symbol
3511 itself. FIXME: What if it isn't? */
3512 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3513 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3516 size_t namelen
, verlen
, newlen
;
3519 if (isym
->st_shndx
!= SHN_UNDEF
)
3521 if (vernum
> elf_tdata (abfd
)->dynverdef_hdr
.sh_info
)
3523 (*_bfd_error_handler
)
3524 (_("%B: %s: invalid version %u (max %d)"),
3526 elf_tdata (abfd
)->dynverdef_hdr
.sh_info
);
3527 bfd_set_error (bfd_error_bad_value
);
3528 goto error_free_vers
;
3530 else if (vernum
> 1)
3532 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3538 /* We cannot simply test for the number of
3539 entries in the VERNEED section since the
3540 numbers for the needed versions do not start
3542 Elf_Internal_Verneed
*t
;
3545 for (t
= elf_tdata (abfd
)->verref
;
3549 Elf_Internal_Vernaux
*a
;
3551 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3553 if (a
->vna_other
== vernum
)
3555 verstr
= a
->vna_nodename
;
3564 (*_bfd_error_handler
)
3565 (_("%B: %s: invalid needed version %d"),
3566 abfd
, name
, vernum
);
3567 bfd_set_error (bfd_error_bad_value
);
3568 goto error_free_vers
;
3572 namelen
= strlen (name
);
3573 verlen
= strlen (verstr
);
3574 newlen
= namelen
+ verlen
+ 2;
3575 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3576 && isym
->st_shndx
!= SHN_UNDEF
)
3579 newname
= bfd_alloc (abfd
, newlen
);
3580 if (newname
== NULL
)
3581 goto error_free_vers
;
3582 memcpy (newname
, name
, namelen
);
3583 p
= newname
+ namelen
;
3585 /* If this is a defined non-hidden version symbol,
3586 we add another @ to the name. This indicates the
3587 default version of the symbol. */
3588 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3589 && isym
->st_shndx
!= SHN_UNDEF
)
3591 memcpy (p
, verstr
, verlen
+ 1);
3597 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3598 sym_hash
, &skip
, &override
,
3599 &type_change_ok
, &size_change_ok
))
3600 goto error_free_vers
;
3609 while (h
->root
.type
== bfd_link_hash_indirect
3610 || h
->root
.type
== bfd_link_hash_warning
)
3611 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3613 /* Remember the old alignment if this is a common symbol, so
3614 that we don't reduce the alignment later on. We can't
3615 check later, because _bfd_generic_link_add_one_symbol
3616 will set a default for the alignment which we want to
3617 override. We also remember the old bfd where the existing
3618 definition comes from. */
3619 switch (h
->root
.type
)
3624 case bfd_link_hash_defined
:
3625 case bfd_link_hash_defweak
:
3626 old_bfd
= h
->root
.u
.def
.section
->owner
;
3629 case bfd_link_hash_common
:
3630 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3631 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3635 if (elf_tdata (abfd
)->verdef
!= NULL
3639 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3642 if (! (_bfd_generic_link_add_one_symbol
3643 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3644 (struct bfd_link_hash_entry
**) sym_hash
)))
3645 goto error_free_vers
;
3648 while (h
->root
.type
== bfd_link_hash_indirect
3649 || h
->root
.type
== bfd_link_hash_warning
)
3650 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3653 new_weakdef
= FALSE
;
3656 && (flags
& BSF_WEAK
) != 0
3657 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3658 && is_elf_hash_table (hash_table
)
3659 && h
->u
.weakdef
== NULL
)
3661 /* Keep a list of all weak defined non function symbols from
3662 a dynamic object, using the weakdef field. Later in this
3663 function we will set the weakdef field to the correct
3664 value. We only put non-function symbols from dynamic
3665 objects on this list, because that happens to be the only
3666 time we need to know the normal symbol corresponding to a
3667 weak symbol, and the information is time consuming to
3668 figure out. If the weakdef field is not already NULL,
3669 then this symbol was already defined by some previous
3670 dynamic object, and we will be using that previous
3671 definition anyhow. */
3673 h
->u
.weakdef
= weaks
;
3678 /* Set the alignment of a common symbol. */
3679 if (isym
->st_shndx
== SHN_COMMON
3680 && h
->root
.type
== bfd_link_hash_common
)
3684 align
= bfd_log2 (isym
->st_value
);
3685 if (align
> old_alignment
3686 /* Permit an alignment power of zero if an alignment of one
3687 is specified and no other alignments have been specified. */
3688 || (isym
->st_value
== 1 && old_alignment
== 0))
3689 h
->root
.u
.c
.p
->alignment_power
= align
;
3691 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3694 if (is_elf_hash_table (hash_table
))
3698 /* Check the alignment when a common symbol is involved. This
3699 can change when a common symbol is overridden by a normal
3700 definition or a common symbol is ignored due to the old
3701 normal definition. We need to make sure the maximum
3702 alignment is maintained. */
3703 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3704 && h
->root
.type
!= bfd_link_hash_common
)
3706 unsigned int common_align
;
3707 unsigned int normal_align
;
3708 unsigned int symbol_align
;
3712 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3713 if (h
->root
.u
.def
.section
->owner
!= NULL
3714 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3716 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3717 if (normal_align
> symbol_align
)
3718 normal_align
= symbol_align
;
3721 normal_align
= symbol_align
;
3725 common_align
= old_alignment
;
3726 common_bfd
= old_bfd
;
3731 common_align
= bfd_log2 (isym
->st_value
);
3733 normal_bfd
= old_bfd
;
3736 if (normal_align
< common_align
)
3737 (*_bfd_error_handler
)
3738 (_("Warning: alignment %u of symbol `%s' in %B"
3739 " is smaller than %u in %B"),
3740 normal_bfd
, common_bfd
,
3741 1 << normal_align
, name
, 1 << common_align
);
3744 /* Remember the symbol size and type. */
3745 if (isym
->st_size
!= 0
3746 && (definition
|| h
->size
== 0))
3748 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3749 (*_bfd_error_handler
)
3750 (_("Warning: size of symbol `%s' changed"
3751 " from %lu in %B to %lu in %B"),
3753 name
, (unsigned long) h
->size
,
3754 (unsigned long) isym
->st_size
);
3756 h
->size
= isym
->st_size
;
3759 /* If this is a common symbol, then we always want H->SIZE
3760 to be the size of the common symbol. The code just above
3761 won't fix the size if a common symbol becomes larger. We
3762 don't warn about a size change here, because that is
3763 covered by --warn-common. */
3764 if (h
->root
.type
== bfd_link_hash_common
)
3765 h
->size
= h
->root
.u
.c
.size
;
3767 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3768 && (definition
|| h
->type
== STT_NOTYPE
))
3770 if (h
->type
!= STT_NOTYPE
3771 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3772 && ! type_change_ok
)
3773 (*_bfd_error_handler
)
3774 (_("Warning: type of symbol `%s' changed"
3775 " from %d to %d in %B"),
3776 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3778 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3781 /* If st_other has a processor-specific meaning, specific
3782 code might be needed here. We never merge the visibility
3783 attribute with the one from a dynamic object. */
3784 if (bed
->elf_backend_merge_symbol_attribute
)
3785 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3788 /* If this symbol has default visibility and the user has requested
3789 we not re-export it, then mark it as hidden. */
3790 if (definition
&& !dynamic
3792 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3793 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3794 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3796 if (isym
->st_other
!= 0 && !dynamic
)
3798 unsigned char hvis
, symvis
, other
, nvis
;
3800 /* Take the balance of OTHER from the definition. */
3801 other
= (definition
? isym
->st_other
: h
->other
);
3802 other
&= ~ ELF_ST_VISIBILITY (-1);
3804 /* Combine visibilities, using the most constraining one. */
3805 hvis
= ELF_ST_VISIBILITY (h
->other
);
3806 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3812 nvis
= hvis
< symvis
? hvis
: symvis
;
3814 h
->other
= other
| nvis
;
3817 /* Set a flag in the hash table entry indicating the type of
3818 reference or definition we just found. Keep a count of
3819 the number of dynamic symbols we find. A dynamic symbol
3820 is one which is referenced or defined by both a regular
3821 object and a shared object. */
3828 if (bind
!= STB_WEAK
)
3829 h
->ref_regular_nonweak
= 1;
3833 if (! info
->executable
3846 || (h
->u
.weakdef
!= NULL
3848 && h
->u
.weakdef
->dynindx
!= -1))
3852 /* Check to see if we need to add an indirect symbol for
3853 the default name. */
3854 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3855 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3856 &sec
, &value
, &dynsym
,
3858 goto error_free_vers
;
3860 if (definition
&& !dynamic
)
3862 char *p
= strchr (name
, ELF_VER_CHR
);
3863 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3865 /* Queue non-default versions so that .symver x, x@FOO
3866 aliases can be checked. */
3867 if (! nondeflt_vers
)
3869 amt
= (isymend
- isym
+ 1)
3870 * sizeof (struct elf_link_hash_entry
*);
3871 nondeflt_vers
= bfd_malloc (amt
);
3873 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3877 if (dynsym
&& h
->dynindx
== -1)
3879 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3880 goto error_free_vers
;
3881 if (h
->u
.weakdef
!= NULL
3883 && h
->u
.weakdef
->dynindx
== -1)
3885 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3886 goto error_free_vers
;
3889 else if (dynsym
&& h
->dynindx
!= -1)
3890 /* If the symbol already has a dynamic index, but
3891 visibility says it should not be visible, turn it into
3893 switch (ELF_ST_VISIBILITY (h
->other
))
3897 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3908 const char *soname
= elf_dt_name (abfd
);
3910 /* A symbol from a library loaded via DT_NEEDED of some
3911 other library is referenced by a regular object.
3912 Add a DT_NEEDED entry for it. Issue an error if
3913 --no-add-needed is used. */
3914 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3916 (*_bfd_error_handler
)
3917 (_("%s: invalid DSO for symbol `%s' definition"),
3919 bfd_set_error (bfd_error_bad_value
);
3920 goto error_free_vers
;
3924 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3926 goto error_free_vers
;
3928 BFD_ASSERT (ret
== 0);
3933 /* Now that all the symbols from this input file are created, handle
3934 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3935 if (nondeflt_vers
!= NULL
)
3937 bfd_size_type cnt
, symidx
;
3939 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3941 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3942 char *shortname
, *p
;
3944 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3946 || (h
->root
.type
!= bfd_link_hash_defined
3947 && h
->root
.type
!= bfd_link_hash_defweak
))
3950 amt
= p
- h
->root
.root
.string
;
3951 shortname
= bfd_malloc (amt
+ 1);
3952 memcpy (shortname
, h
->root
.root
.string
, amt
);
3953 shortname
[amt
] = '\0';
3955 hi
= (struct elf_link_hash_entry
*)
3956 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3957 FALSE
, FALSE
, FALSE
);
3959 && hi
->root
.type
== h
->root
.type
3960 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3961 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3963 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3964 hi
->root
.type
= bfd_link_hash_indirect
;
3965 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3966 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3967 sym_hash
= elf_sym_hashes (abfd
);
3969 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3970 if (sym_hash
[symidx
] == hi
)
3972 sym_hash
[symidx
] = h
;
3978 free (nondeflt_vers
);
3979 nondeflt_vers
= NULL
;
3982 if (extversym
!= NULL
)
3988 if (isymbuf
!= NULL
)
3992 /* Now set the weakdefs field correctly for all the weak defined
3993 symbols we found. The only way to do this is to search all the
3994 symbols. Since we only need the information for non functions in
3995 dynamic objects, that's the only time we actually put anything on
3996 the list WEAKS. We need this information so that if a regular
3997 object refers to a symbol defined weakly in a dynamic object, the
3998 real symbol in the dynamic object is also put in the dynamic
3999 symbols; we also must arrange for both symbols to point to the
4000 same memory location. We could handle the general case of symbol
4001 aliasing, but a general symbol alias can only be generated in
4002 assembler code, handling it correctly would be very time
4003 consuming, and other ELF linkers don't handle general aliasing
4007 struct elf_link_hash_entry
**hpp
;
4008 struct elf_link_hash_entry
**hppend
;
4009 struct elf_link_hash_entry
**sorted_sym_hash
;
4010 struct elf_link_hash_entry
*h
;
4013 /* Since we have to search the whole symbol list for each weak
4014 defined symbol, search time for N weak defined symbols will be
4015 O(N^2). Binary search will cut it down to O(NlogN). */
4016 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4017 sorted_sym_hash
= bfd_malloc (amt
);
4018 if (sorted_sym_hash
== NULL
)
4020 sym_hash
= sorted_sym_hash
;
4021 hpp
= elf_sym_hashes (abfd
);
4022 hppend
= hpp
+ extsymcount
;
4024 for (; hpp
< hppend
; hpp
++)
4028 && h
->root
.type
== bfd_link_hash_defined
4029 && h
->type
!= STT_FUNC
)
4037 qsort (sorted_sym_hash
, sym_count
,
4038 sizeof (struct elf_link_hash_entry
*),
4041 while (weaks
!= NULL
)
4043 struct elf_link_hash_entry
*hlook
;
4050 weaks
= hlook
->u
.weakdef
;
4051 hlook
->u
.weakdef
= NULL
;
4053 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4054 || hlook
->root
.type
== bfd_link_hash_defweak
4055 || hlook
->root
.type
== bfd_link_hash_common
4056 || hlook
->root
.type
== bfd_link_hash_indirect
);
4057 slook
= hlook
->root
.u
.def
.section
;
4058 vlook
= hlook
->root
.u
.def
.value
;
4065 bfd_signed_vma vdiff
;
4067 h
= sorted_sym_hash
[idx
];
4068 vdiff
= vlook
- h
->root
.u
.def
.value
;
4075 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4088 /* We didn't find a value/section match. */
4092 for (i
= ilook
; i
< sym_count
; i
++)
4094 h
= sorted_sym_hash
[i
];
4096 /* Stop if value or section doesn't match. */
4097 if (h
->root
.u
.def
.value
!= vlook
4098 || h
->root
.u
.def
.section
!= slook
)
4100 else if (h
!= hlook
)
4102 hlook
->u
.weakdef
= h
;
4104 /* If the weak definition is in the list of dynamic
4105 symbols, make sure the real definition is put
4107 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4109 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4113 /* If the real definition is in the list of dynamic
4114 symbols, make sure the weak definition is put
4115 there as well. If we don't do this, then the
4116 dynamic loader might not merge the entries for the
4117 real definition and the weak definition. */
4118 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4120 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4128 free (sorted_sym_hash
);
4131 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4132 if (check_directives
)
4133 check_directives (abfd
, info
);
4135 /* If this object is the same format as the output object, and it is
4136 not a shared library, then let the backend look through the
4139 This is required to build global offset table entries and to
4140 arrange for dynamic relocs. It is not required for the
4141 particular common case of linking non PIC code, even when linking
4142 against shared libraries, but unfortunately there is no way of
4143 knowing whether an object file has been compiled PIC or not.
4144 Looking through the relocs is not particularly time consuming.
4145 The problem is that we must either (1) keep the relocs in memory,
4146 which causes the linker to require additional runtime memory or
4147 (2) read the relocs twice from the input file, which wastes time.
4148 This would be a good case for using mmap.
4150 I have no idea how to handle linking PIC code into a file of a
4151 different format. It probably can't be done. */
4152 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4154 && is_elf_hash_table (hash_table
)
4155 && hash_table
->root
.creator
== abfd
->xvec
4156 && check_relocs
!= NULL
)
4160 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4162 Elf_Internal_Rela
*internal_relocs
;
4165 if ((o
->flags
& SEC_RELOC
) == 0
4166 || o
->reloc_count
== 0
4167 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4168 && (o
->flags
& SEC_DEBUGGING
) != 0)
4169 || bfd_is_abs_section (o
->output_section
))
4172 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4174 if (internal_relocs
== NULL
)
4177 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4179 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4180 free (internal_relocs
);
4187 /* If this is a non-traditional link, try to optimize the handling
4188 of the .stab/.stabstr sections. */
4190 && ! info
->traditional_format
4191 && is_elf_hash_table (hash_table
)
4192 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4196 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4197 if (stabstr
!= NULL
)
4199 bfd_size_type string_offset
= 0;
4202 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4203 if (strncmp (".stab", stab
->name
, 5) == 0
4204 && (!stab
->name
[5] ||
4205 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4206 && (stab
->flags
& SEC_MERGE
) == 0
4207 && !bfd_is_abs_section (stab
->output_section
))
4209 struct bfd_elf_section_data
*secdata
;
4211 secdata
= elf_section_data (stab
);
4212 if (! _bfd_link_section_stabs (abfd
,
4213 &hash_table
->stab_info
,
4218 if (secdata
->sec_info
)
4219 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4224 if (is_elf_hash_table (hash_table
))
4226 /* Add this bfd to the loaded list. */
4227 struct elf_link_loaded_list
*n
;
4229 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4233 n
->next
= hash_table
->loaded
;
4234 hash_table
->loaded
= n
;
4240 if (nondeflt_vers
!= NULL
)
4241 free (nondeflt_vers
);
4242 if (extversym
!= NULL
)
4245 if (isymbuf
!= NULL
)
4251 /* Return the linker hash table entry of a symbol that might be
4252 satisfied by an archive symbol. Return -1 on error. */
4254 struct elf_link_hash_entry
*
4255 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4256 struct bfd_link_info
*info
,
4259 struct elf_link_hash_entry
*h
;
4263 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4267 /* If this is a default version (the name contains @@), look up the
4268 symbol again with only one `@' as well as without the version.
4269 The effect is that references to the symbol with and without the
4270 version will be matched by the default symbol in the archive. */
4272 p
= strchr (name
, ELF_VER_CHR
);
4273 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4276 /* First check with only one `@'. */
4277 len
= strlen (name
);
4278 copy
= bfd_alloc (abfd
, len
);
4280 return (struct elf_link_hash_entry
*) 0 - 1;
4282 first
= p
- name
+ 1;
4283 memcpy (copy
, name
, first
);
4284 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4286 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4289 /* We also need to check references to the symbol without the
4291 copy
[first
- 1] = '\0';
4292 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4293 FALSE
, FALSE
, FALSE
);
4296 bfd_release (abfd
, copy
);
4300 /* Add symbols from an ELF archive file to the linker hash table. We
4301 don't use _bfd_generic_link_add_archive_symbols because of a
4302 problem which arises on UnixWare. The UnixWare libc.so is an
4303 archive which includes an entry libc.so.1 which defines a bunch of
4304 symbols. The libc.so archive also includes a number of other
4305 object files, which also define symbols, some of which are the same
4306 as those defined in libc.so.1. Correct linking requires that we
4307 consider each object file in turn, and include it if it defines any
4308 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4309 this; it looks through the list of undefined symbols, and includes
4310 any object file which defines them. When this algorithm is used on
4311 UnixWare, it winds up pulling in libc.so.1 early and defining a
4312 bunch of symbols. This means that some of the other objects in the
4313 archive are not included in the link, which is incorrect since they
4314 precede libc.so.1 in the archive.
4316 Fortunately, ELF archive handling is simpler than that done by
4317 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4318 oddities. In ELF, if we find a symbol in the archive map, and the
4319 symbol is currently undefined, we know that we must pull in that
4322 Unfortunately, we do have to make multiple passes over the symbol
4323 table until nothing further is resolved. */
4326 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4329 bfd_boolean
*defined
= NULL
;
4330 bfd_boolean
*included
= NULL
;
4334 const struct elf_backend_data
*bed
;
4335 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4336 (bfd
*, struct bfd_link_info
*, const char *);
4338 if (! bfd_has_map (abfd
))
4340 /* An empty archive is a special case. */
4341 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4343 bfd_set_error (bfd_error_no_armap
);
4347 /* Keep track of all symbols we know to be already defined, and all
4348 files we know to be already included. This is to speed up the
4349 second and subsequent passes. */
4350 c
= bfd_ardata (abfd
)->symdef_count
;
4354 amt
*= sizeof (bfd_boolean
);
4355 defined
= bfd_zmalloc (amt
);
4356 included
= bfd_zmalloc (amt
);
4357 if (defined
== NULL
|| included
== NULL
)
4360 symdefs
= bfd_ardata (abfd
)->symdefs
;
4361 bed
= get_elf_backend_data (abfd
);
4362 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4375 symdefend
= symdef
+ c
;
4376 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4378 struct elf_link_hash_entry
*h
;
4380 struct bfd_link_hash_entry
*undefs_tail
;
4383 if (defined
[i
] || included
[i
])
4385 if (symdef
->file_offset
== last
)
4391 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4392 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4398 if (h
->root
.type
== bfd_link_hash_common
)
4400 /* We currently have a common symbol. The archive map contains
4401 a reference to this symbol, so we may want to include it. We
4402 only want to include it however, if this archive element
4403 contains a definition of the symbol, not just another common
4406 Unfortunately some archivers (including GNU ar) will put
4407 declarations of common symbols into their archive maps, as
4408 well as real definitions, so we cannot just go by the archive
4409 map alone. Instead we must read in the element's symbol
4410 table and check that to see what kind of symbol definition
4412 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4415 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4417 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4422 /* We need to include this archive member. */
4423 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4424 if (element
== NULL
)
4427 if (! bfd_check_format (element
, bfd_object
))
4430 /* Doublecheck that we have not included this object
4431 already--it should be impossible, but there may be
4432 something wrong with the archive. */
4433 if (element
->archive_pass
!= 0)
4435 bfd_set_error (bfd_error_bad_value
);
4438 element
->archive_pass
= 1;
4440 undefs_tail
= info
->hash
->undefs_tail
;
4442 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4445 if (! bfd_link_add_symbols (element
, info
))
4448 /* If there are any new undefined symbols, we need to make
4449 another pass through the archive in order to see whether
4450 they can be defined. FIXME: This isn't perfect, because
4451 common symbols wind up on undefs_tail and because an
4452 undefined symbol which is defined later on in this pass
4453 does not require another pass. This isn't a bug, but it
4454 does make the code less efficient than it could be. */
4455 if (undefs_tail
!= info
->hash
->undefs_tail
)
4458 /* Look backward to mark all symbols from this object file
4459 which we have already seen in this pass. */
4463 included
[mark
] = TRUE
;
4468 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4470 /* We mark subsequent symbols from this object file as we go
4471 on through the loop. */
4472 last
= symdef
->file_offset
;
4483 if (defined
!= NULL
)
4485 if (included
!= NULL
)
4490 /* Given an ELF BFD, add symbols to the global hash table as
4494 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4496 switch (bfd_get_format (abfd
))
4499 return elf_link_add_object_symbols (abfd
, info
);
4501 return elf_link_add_archive_symbols (abfd
, info
);
4503 bfd_set_error (bfd_error_wrong_format
);
4508 /* This function will be called though elf_link_hash_traverse to store
4509 all hash value of the exported symbols in an array. */
4512 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4514 unsigned long **valuep
= data
;
4520 if (h
->root
.type
== bfd_link_hash_warning
)
4521 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4523 /* Ignore indirect symbols. These are added by the versioning code. */
4524 if (h
->dynindx
== -1)
4527 name
= h
->root
.root
.string
;
4528 p
= strchr (name
, ELF_VER_CHR
);
4531 alc
= bfd_malloc (p
- name
+ 1);
4532 memcpy (alc
, name
, p
- name
);
4533 alc
[p
- name
] = '\0';
4537 /* Compute the hash value. */
4538 ha
= bfd_elf_hash (name
);
4540 /* Store the found hash value in the array given as the argument. */
4543 /* And store it in the struct so that we can put it in the hash table
4545 h
->u
.elf_hash_value
= ha
;
4553 /* Array used to determine the number of hash table buckets to use
4554 based on the number of symbols there are. If there are fewer than
4555 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4556 fewer than 37 we use 17 buckets, and so forth. We never use more
4557 than 32771 buckets. */
4559 static const size_t elf_buckets
[] =
4561 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4565 /* Compute bucket count for hashing table. We do not use a static set
4566 of possible tables sizes anymore. Instead we determine for all
4567 possible reasonable sizes of the table the outcome (i.e., the
4568 number of collisions etc) and choose the best solution. The
4569 weighting functions are not too simple to allow the table to grow
4570 without bounds. Instead one of the weighting factors is the size.
4571 Therefore the result is always a good payoff between few collisions
4572 (= short chain lengths) and table size. */
4574 compute_bucket_count (struct bfd_link_info
*info
)
4576 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4577 size_t best_size
= 0;
4578 unsigned long int *hashcodes
;
4579 unsigned long int *hashcodesp
;
4580 unsigned long int i
;
4583 /* Compute the hash values for all exported symbols. At the same
4584 time store the values in an array so that we could use them for
4587 amt
*= sizeof (unsigned long int);
4588 hashcodes
= bfd_malloc (amt
);
4589 if (hashcodes
== NULL
)
4591 hashcodesp
= hashcodes
;
4593 /* Put all hash values in HASHCODES. */
4594 elf_link_hash_traverse (elf_hash_table (info
),
4595 elf_collect_hash_codes
, &hashcodesp
);
4597 /* We have a problem here. The following code to optimize the table
4598 size requires an integer type with more the 32 bits. If
4599 BFD_HOST_U_64_BIT is set we know about such a type. */
4600 #ifdef BFD_HOST_U_64_BIT
4603 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4606 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4607 unsigned long int *counts
;
4608 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4609 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4611 /* Possible optimization parameters: if we have NSYMS symbols we say
4612 that the hashing table must at least have NSYMS/4 and at most
4614 minsize
= nsyms
/ 4;
4617 best_size
= maxsize
= nsyms
* 2;
4619 /* Create array where we count the collisions in. We must use bfd_malloc
4620 since the size could be large. */
4622 amt
*= sizeof (unsigned long int);
4623 counts
= bfd_malloc (amt
);
4630 /* Compute the "optimal" size for the hash table. The criteria is a
4631 minimal chain length. The minor criteria is (of course) the size
4633 for (i
= minsize
; i
< maxsize
; ++i
)
4635 /* Walk through the array of hashcodes and count the collisions. */
4636 BFD_HOST_U_64_BIT max
;
4637 unsigned long int j
;
4638 unsigned long int fact
;
4640 memset (counts
, '\0', i
* sizeof (unsigned long int));
4642 /* Determine how often each hash bucket is used. */
4643 for (j
= 0; j
< nsyms
; ++j
)
4644 ++counts
[hashcodes
[j
] % i
];
4646 /* For the weight function we need some information about the
4647 pagesize on the target. This is information need not be 100%
4648 accurate. Since this information is not available (so far) we
4649 define it here to a reasonable default value. If it is crucial
4650 to have a better value some day simply define this value. */
4651 # ifndef BFD_TARGET_PAGESIZE
4652 # define BFD_TARGET_PAGESIZE (4096)
4655 /* We in any case need 2 + NSYMS entries for the size values and
4657 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4660 /* Variant 1: optimize for short chains. We add the squares
4661 of all the chain lengths (which favors many small chain
4662 over a few long chains). */
4663 for (j
= 0; j
< i
; ++j
)
4664 max
+= counts
[j
] * counts
[j
];
4666 /* This adds penalties for the overall size of the table. */
4667 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4670 /* Variant 2: Optimize a lot more for small table. Here we
4671 also add squares of the size but we also add penalties for
4672 empty slots (the +1 term). */
4673 for (j
= 0; j
< i
; ++j
)
4674 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4676 /* The overall size of the table is considered, but not as
4677 strong as in variant 1, where it is squared. */
4678 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4682 /* Compare with current best results. */
4683 if (max
< best_chlen
)
4693 #endif /* defined (BFD_HOST_U_64_BIT) */
4695 /* This is the fallback solution if no 64bit type is available or if we
4696 are not supposed to spend much time on optimizations. We select the
4697 bucket count using a fixed set of numbers. */
4698 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4700 best_size
= elf_buckets
[i
];
4701 if (dynsymcount
< elf_buckets
[i
+ 1])
4706 /* Free the arrays we needed. */
4712 /* Set up the sizes and contents of the ELF dynamic sections. This is
4713 called by the ELF linker emulation before_allocation routine. We
4714 must set the sizes of the sections before the linker sets the
4715 addresses of the various sections. */
4718 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4721 const char *filter_shlib
,
4722 const char * const *auxiliary_filters
,
4723 struct bfd_link_info
*info
,
4724 asection
**sinterpptr
,
4725 struct bfd_elf_version_tree
*verdefs
)
4727 bfd_size_type soname_indx
;
4729 const struct elf_backend_data
*bed
;
4730 struct elf_assign_sym_version_info asvinfo
;
4734 soname_indx
= (bfd_size_type
) -1;
4736 if (!is_elf_hash_table (info
->hash
))
4739 elf_tdata (output_bfd
)->relro
= info
->relro
;
4740 if (info
->execstack
)
4741 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4742 else if (info
->noexecstack
)
4743 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4747 asection
*notesec
= NULL
;
4750 for (inputobj
= info
->input_bfds
;
4752 inputobj
= inputobj
->link_next
)
4756 if (inputobj
->flags
& DYNAMIC
)
4758 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4761 if (s
->flags
& SEC_CODE
)
4770 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4771 if (exec
&& info
->relocatable
4772 && notesec
->output_section
!= bfd_abs_section_ptr
)
4773 notesec
->output_section
->flags
|= SEC_CODE
;
4777 /* Any syms created from now on start with -1 in
4778 got.refcount/offset and plt.refcount/offset. */
4779 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4781 /* The backend may have to create some sections regardless of whether
4782 we're dynamic or not. */
4783 bed
= get_elf_backend_data (output_bfd
);
4784 if (bed
->elf_backend_always_size_sections
4785 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4788 dynobj
= elf_hash_table (info
)->dynobj
;
4790 /* If there were no dynamic objects in the link, there is nothing to
4795 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4798 if (elf_hash_table (info
)->dynamic_sections_created
)
4800 struct elf_info_failed eif
;
4801 struct elf_link_hash_entry
*h
;
4803 struct bfd_elf_version_tree
*t
;
4804 struct bfd_elf_version_expr
*d
;
4805 bfd_boolean all_defined
;
4807 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4808 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4812 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4814 if (soname_indx
== (bfd_size_type
) -1
4815 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4821 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4823 info
->flags
|= DF_SYMBOLIC
;
4830 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4832 if (indx
== (bfd_size_type
) -1
4833 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4836 if (info
->new_dtags
)
4838 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4839 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4844 if (filter_shlib
!= NULL
)
4848 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4849 filter_shlib
, TRUE
);
4850 if (indx
== (bfd_size_type
) -1
4851 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4855 if (auxiliary_filters
!= NULL
)
4857 const char * const *p
;
4859 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4863 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4865 if (indx
== (bfd_size_type
) -1
4866 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4872 eif
.verdefs
= verdefs
;
4875 /* If we are supposed to export all symbols into the dynamic symbol
4876 table (this is not the normal case), then do so. */
4877 if (info
->export_dynamic
)
4879 elf_link_hash_traverse (elf_hash_table (info
),
4880 _bfd_elf_export_symbol
,
4886 /* Make all global versions with definition. */
4887 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4888 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4889 if (!d
->symver
&& d
->symbol
)
4891 const char *verstr
, *name
;
4892 size_t namelen
, verlen
, newlen
;
4894 struct elf_link_hash_entry
*newh
;
4897 namelen
= strlen (name
);
4899 verlen
= strlen (verstr
);
4900 newlen
= namelen
+ verlen
+ 3;
4902 newname
= bfd_malloc (newlen
);
4903 if (newname
== NULL
)
4905 memcpy (newname
, name
, namelen
);
4907 /* Check the hidden versioned definition. */
4908 p
= newname
+ namelen
;
4910 memcpy (p
, verstr
, verlen
+ 1);
4911 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4912 newname
, FALSE
, FALSE
,
4915 || (newh
->root
.type
!= bfd_link_hash_defined
4916 && newh
->root
.type
!= bfd_link_hash_defweak
))
4918 /* Check the default versioned definition. */
4920 memcpy (p
, verstr
, verlen
+ 1);
4921 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4922 newname
, FALSE
, FALSE
,
4927 /* Mark this version if there is a definition and it is
4928 not defined in a shared object. */
4930 && !newh
->def_dynamic
4931 && (newh
->root
.type
== bfd_link_hash_defined
4932 || newh
->root
.type
== bfd_link_hash_defweak
))
4936 /* Attach all the symbols to their version information. */
4937 asvinfo
.output_bfd
= output_bfd
;
4938 asvinfo
.info
= info
;
4939 asvinfo
.verdefs
= verdefs
;
4940 asvinfo
.failed
= FALSE
;
4942 elf_link_hash_traverse (elf_hash_table (info
),
4943 _bfd_elf_link_assign_sym_version
,
4948 if (!info
->allow_undefined_version
)
4950 /* Check if all global versions have a definition. */
4952 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4953 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4954 if (!d
->symver
&& !d
->script
)
4956 (*_bfd_error_handler
)
4957 (_("%s: undefined version: %s"),
4958 d
->pattern
, t
->name
);
4959 all_defined
= FALSE
;
4964 bfd_set_error (bfd_error_bad_value
);
4969 /* Find all symbols which were defined in a dynamic object and make
4970 the backend pick a reasonable value for them. */
4971 elf_link_hash_traverse (elf_hash_table (info
),
4972 _bfd_elf_adjust_dynamic_symbol
,
4977 /* Add some entries to the .dynamic section. We fill in some of the
4978 values later, in bfd_elf_final_link, but we must add the entries
4979 now so that we know the final size of the .dynamic section. */
4981 /* If there are initialization and/or finalization functions to
4982 call then add the corresponding DT_INIT/DT_FINI entries. */
4983 h
= (info
->init_function
4984 ? elf_link_hash_lookup (elf_hash_table (info
),
4985 info
->init_function
, FALSE
,
4992 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
4995 h
= (info
->fini_function
4996 ? elf_link_hash_lookup (elf_hash_table (info
),
4997 info
->fini_function
, FALSE
,
5004 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5008 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5010 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5011 if (! info
->executable
)
5016 for (sub
= info
->input_bfds
; sub
!= NULL
;
5017 sub
= sub
->link_next
)
5018 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5019 if (elf_section_data (o
)->this_hdr
.sh_type
5020 == SHT_PREINIT_ARRAY
)
5022 (*_bfd_error_handler
)
5023 (_("%B: .preinit_array section is not allowed in DSO"),
5028 bfd_set_error (bfd_error_nonrepresentable_section
);
5032 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5033 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5036 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5038 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5039 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5042 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5044 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5045 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5049 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5050 /* If .dynstr is excluded from the link, we don't want any of
5051 these tags. Strictly, we should be checking each section
5052 individually; This quick check covers for the case where
5053 someone does a /DISCARD/ : { *(*) }. */
5054 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5056 bfd_size_type strsize
;
5058 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5059 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5060 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5061 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5062 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5063 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5064 bed
->s
->sizeof_sym
))
5069 /* The backend must work out the sizes of all the other dynamic
5071 if (bed
->elf_backend_size_dynamic_sections
5072 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5075 if (elf_hash_table (info
)->dynamic_sections_created
)
5077 bfd_size_type dynsymcount
;
5079 size_t bucketcount
= 0;
5080 size_t hash_entry_size
;
5081 unsigned int dtagcount
;
5083 /* Set up the version definition section. */
5084 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5085 BFD_ASSERT (s
!= NULL
);
5087 /* We may have created additional version definitions if we are
5088 just linking a regular application. */
5089 verdefs
= asvinfo
.verdefs
;
5091 /* Skip anonymous version tag. */
5092 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5093 verdefs
= verdefs
->next
;
5095 if (verdefs
== NULL
&& !info
->create_default_symver
)
5096 _bfd_strip_section_from_output (info
, s
);
5101 struct bfd_elf_version_tree
*t
;
5103 Elf_Internal_Verdef def
;
5104 Elf_Internal_Verdaux defaux
;
5105 struct bfd_link_hash_entry
*bh
;
5106 struct elf_link_hash_entry
*h
;
5112 /* Make space for the base version. */
5113 size
+= sizeof (Elf_External_Verdef
);
5114 size
+= sizeof (Elf_External_Verdaux
);
5117 /* Make space for the default version. */
5118 if (info
->create_default_symver
)
5120 size
+= sizeof (Elf_External_Verdef
);
5124 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5126 struct bfd_elf_version_deps
*n
;
5128 size
+= sizeof (Elf_External_Verdef
);
5129 size
+= sizeof (Elf_External_Verdaux
);
5132 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5133 size
+= sizeof (Elf_External_Verdaux
);
5137 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5138 if (s
->contents
== NULL
&& s
->size
!= 0)
5141 /* Fill in the version definition section. */
5145 def
.vd_version
= VER_DEF_CURRENT
;
5146 def
.vd_flags
= VER_FLG_BASE
;
5149 if (info
->create_default_symver
)
5151 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5152 def
.vd_next
= sizeof (Elf_External_Verdef
);
5156 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5157 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5158 + sizeof (Elf_External_Verdaux
));
5161 if (soname_indx
!= (bfd_size_type
) -1)
5163 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5165 def
.vd_hash
= bfd_elf_hash (soname
);
5166 defaux
.vda_name
= soname_indx
;
5173 name
= basename (output_bfd
->filename
);
5174 def
.vd_hash
= bfd_elf_hash (name
);
5175 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5177 if (indx
== (bfd_size_type
) -1)
5179 defaux
.vda_name
= indx
;
5181 defaux
.vda_next
= 0;
5183 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5184 (Elf_External_Verdef
*) p
);
5185 p
+= sizeof (Elf_External_Verdef
);
5186 if (info
->create_default_symver
)
5188 /* Add a symbol representing this version. */
5190 if (! (_bfd_generic_link_add_one_symbol
5191 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5193 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5195 h
= (struct elf_link_hash_entry
*) bh
;
5198 h
->type
= STT_OBJECT
;
5199 h
->verinfo
.vertree
= NULL
;
5201 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5204 /* Create a duplicate of the base version with the same
5205 aux block, but different flags. */
5208 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5210 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5211 + sizeof (Elf_External_Verdaux
));
5214 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5215 (Elf_External_Verdef
*) p
);
5216 p
+= sizeof (Elf_External_Verdef
);
5218 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5219 (Elf_External_Verdaux
*) p
);
5220 p
+= sizeof (Elf_External_Verdaux
);
5222 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5225 struct bfd_elf_version_deps
*n
;
5228 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5231 /* Add a symbol representing this version. */
5233 if (! (_bfd_generic_link_add_one_symbol
5234 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5236 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5238 h
= (struct elf_link_hash_entry
*) bh
;
5241 h
->type
= STT_OBJECT
;
5242 h
->verinfo
.vertree
= t
;
5244 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5247 def
.vd_version
= VER_DEF_CURRENT
;
5249 if (t
->globals
.list
== NULL
5250 && t
->locals
.list
== NULL
5252 def
.vd_flags
|= VER_FLG_WEAK
;
5253 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5254 def
.vd_cnt
= cdeps
+ 1;
5255 def
.vd_hash
= bfd_elf_hash (t
->name
);
5256 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5258 if (t
->next
!= NULL
)
5259 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5260 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5262 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5263 (Elf_External_Verdef
*) p
);
5264 p
+= sizeof (Elf_External_Verdef
);
5266 defaux
.vda_name
= h
->dynstr_index
;
5267 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5269 defaux
.vda_next
= 0;
5270 if (t
->deps
!= NULL
)
5271 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5272 t
->name_indx
= defaux
.vda_name
;
5274 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5275 (Elf_External_Verdaux
*) p
);
5276 p
+= sizeof (Elf_External_Verdaux
);
5278 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5280 if (n
->version_needed
== NULL
)
5282 /* This can happen if there was an error in the
5284 defaux
.vda_name
= 0;
5288 defaux
.vda_name
= n
->version_needed
->name_indx
;
5289 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5292 if (n
->next
== NULL
)
5293 defaux
.vda_next
= 0;
5295 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5297 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5298 (Elf_External_Verdaux
*) p
);
5299 p
+= sizeof (Elf_External_Verdaux
);
5303 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5304 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5307 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5310 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5312 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5315 else if (info
->flags
& DF_BIND_NOW
)
5317 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5323 if (info
->executable
)
5324 info
->flags_1
&= ~ (DF_1_INITFIRST
5327 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5331 /* Work out the size of the version reference section. */
5333 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5334 BFD_ASSERT (s
!= NULL
);
5336 struct elf_find_verdep_info sinfo
;
5338 sinfo
.output_bfd
= output_bfd
;
5340 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5341 if (sinfo
.vers
== 0)
5343 sinfo
.failed
= FALSE
;
5345 elf_link_hash_traverse (elf_hash_table (info
),
5346 _bfd_elf_link_find_version_dependencies
,
5349 if (elf_tdata (output_bfd
)->verref
== NULL
)
5350 _bfd_strip_section_from_output (info
, s
);
5353 Elf_Internal_Verneed
*t
;
5358 /* Build the version definition section. */
5361 for (t
= elf_tdata (output_bfd
)->verref
;
5365 Elf_Internal_Vernaux
*a
;
5367 size
+= sizeof (Elf_External_Verneed
);
5369 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5370 size
+= sizeof (Elf_External_Vernaux
);
5374 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5375 if (s
->contents
== NULL
)
5379 for (t
= elf_tdata (output_bfd
)->verref
;
5384 Elf_Internal_Vernaux
*a
;
5388 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5391 t
->vn_version
= VER_NEED_CURRENT
;
5393 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5394 elf_dt_name (t
->vn_bfd
) != NULL
5395 ? elf_dt_name (t
->vn_bfd
)
5396 : basename (t
->vn_bfd
->filename
),
5398 if (indx
== (bfd_size_type
) -1)
5401 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5402 if (t
->vn_nextref
== NULL
)
5405 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5406 + caux
* sizeof (Elf_External_Vernaux
));
5408 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5409 (Elf_External_Verneed
*) p
);
5410 p
+= sizeof (Elf_External_Verneed
);
5412 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5414 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5415 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5416 a
->vna_nodename
, FALSE
);
5417 if (indx
== (bfd_size_type
) -1)
5420 if (a
->vna_nextptr
== NULL
)
5423 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5425 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5426 (Elf_External_Vernaux
*) p
);
5427 p
+= sizeof (Elf_External_Vernaux
);
5431 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5432 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5435 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5439 /* Assign dynsym indicies. In a shared library we generate a
5440 section symbol for each output section, which come first.
5441 Next come all of the back-end allocated local dynamic syms,
5442 followed by the rest of the global symbols. */
5444 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5446 /* Work out the size of the symbol version section. */
5447 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5448 BFD_ASSERT (s
!= NULL
);
5449 if (dynsymcount
== 0
5450 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
5451 && !info
->create_default_symver
))
5453 _bfd_strip_section_from_output (info
, s
);
5454 /* The DYNSYMCOUNT might have changed if we were going to
5455 output a dynamic symbol table entry for S. */
5456 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5460 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5461 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5462 if (s
->contents
== NULL
)
5465 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5469 /* Set the size of the .dynsym and .hash sections. We counted
5470 the number of dynamic symbols in elf_link_add_object_symbols.
5471 We will build the contents of .dynsym and .hash when we build
5472 the final symbol table, because until then we do not know the
5473 correct value to give the symbols. We built the .dynstr
5474 section as we went along in elf_link_add_object_symbols. */
5475 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5476 BFD_ASSERT (s
!= NULL
);
5477 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5478 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5479 if (s
->contents
== NULL
&& s
->size
!= 0)
5482 if (dynsymcount
!= 0)
5484 Elf_Internal_Sym isym
;
5486 /* The first entry in .dynsym is a dummy symbol. */
5493 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5496 /* Compute the size of the hashing table. As a side effect this
5497 computes the hash values for all the names we export. */
5498 bucketcount
= compute_bucket_count (info
);
5500 s
= bfd_get_section_by_name (dynobj
, ".hash");
5501 BFD_ASSERT (s
!= NULL
);
5502 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5503 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5504 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5505 if (s
->contents
== NULL
)
5508 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5509 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5510 s
->contents
+ hash_entry_size
);
5512 elf_hash_table (info
)->bucketcount
= bucketcount
;
5514 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5515 BFD_ASSERT (s
!= NULL
);
5517 elf_finalize_dynstr (output_bfd
, info
);
5519 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5521 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5522 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5529 /* Final phase of ELF linker. */
5531 /* A structure we use to avoid passing large numbers of arguments. */
5533 struct elf_final_link_info
5535 /* General link information. */
5536 struct bfd_link_info
*info
;
5539 /* Symbol string table. */
5540 struct bfd_strtab_hash
*symstrtab
;
5541 /* .dynsym section. */
5542 asection
*dynsym_sec
;
5543 /* .hash section. */
5545 /* symbol version section (.gnu.version). */
5546 asection
*symver_sec
;
5547 /* Buffer large enough to hold contents of any section. */
5549 /* Buffer large enough to hold external relocs of any section. */
5550 void *external_relocs
;
5551 /* Buffer large enough to hold internal relocs of any section. */
5552 Elf_Internal_Rela
*internal_relocs
;
5553 /* Buffer large enough to hold external local symbols of any input
5555 bfd_byte
*external_syms
;
5556 /* And a buffer for symbol section indices. */
5557 Elf_External_Sym_Shndx
*locsym_shndx
;
5558 /* Buffer large enough to hold internal local symbols of any input
5560 Elf_Internal_Sym
*internal_syms
;
5561 /* Array large enough to hold a symbol index for each local symbol
5562 of any input BFD. */
5564 /* Array large enough to hold a section pointer for each local
5565 symbol of any input BFD. */
5566 asection
**sections
;
5567 /* Buffer to hold swapped out symbols. */
5569 /* And one for symbol section indices. */
5570 Elf_External_Sym_Shndx
*symshndxbuf
;
5571 /* Number of swapped out symbols in buffer. */
5572 size_t symbuf_count
;
5573 /* Number of symbols which fit in symbuf. */
5575 /* And same for symshndxbuf. */
5576 size_t shndxbuf_size
;
5579 /* This struct is used to pass information to elf_link_output_extsym. */
5581 struct elf_outext_info
5584 bfd_boolean localsyms
;
5585 struct elf_final_link_info
*finfo
;
5588 /* When performing a relocatable link, the input relocations are
5589 preserved. But, if they reference global symbols, the indices
5590 referenced must be updated. Update all the relocations in
5591 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5594 elf_link_adjust_relocs (bfd
*abfd
,
5595 Elf_Internal_Shdr
*rel_hdr
,
5597 struct elf_link_hash_entry
**rel_hash
)
5600 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5602 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5603 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5604 bfd_vma r_type_mask
;
5607 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5609 swap_in
= bed
->s
->swap_reloc_in
;
5610 swap_out
= bed
->s
->swap_reloc_out
;
5612 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5614 swap_in
= bed
->s
->swap_reloca_in
;
5615 swap_out
= bed
->s
->swap_reloca_out
;
5620 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5623 if (bed
->s
->arch_size
== 32)
5630 r_type_mask
= 0xffffffff;
5634 erela
= rel_hdr
->contents
;
5635 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5637 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5640 if (*rel_hash
== NULL
)
5643 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5645 (*swap_in
) (abfd
, erela
, irela
);
5646 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5647 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5648 | (irela
[j
].r_info
& r_type_mask
));
5649 (*swap_out
) (abfd
, irela
, erela
);
5653 struct elf_link_sort_rela
5659 enum elf_reloc_type_class type
;
5660 /* We use this as an array of size int_rels_per_ext_rel. */
5661 Elf_Internal_Rela rela
[1];
5665 elf_link_sort_cmp1 (const void *A
, const void *B
)
5667 const struct elf_link_sort_rela
*a
= A
;
5668 const struct elf_link_sort_rela
*b
= B
;
5669 int relativea
, relativeb
;
5671 relativea
= a
->type
== reloc_class_relative
;
5672 relativeb
= b
->type
== reloc_class_relative
;
5674 if (relativea
< relativeb
)
5676 if (relativea
> relativeb
)
5678 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5680 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5682 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5684 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5690 elf_link_sort_cmp2 (const void *A
, const void *B
)
5692 const struct elf_link_sort_rela
*a
= A
;
5693 const struct elf_link_sort_rela
*b
= B
;
5696 if (a
->u
.offset
< b
->u
.offset
)
5698 if (a
->u
.offset
> b
->u
.offset
)
5700 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5701 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5706 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5708 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5714 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5717 bfd_size_type count
, size
;
5718 size_t i
, ret
, sort_elt
, ext_size
;
5719 bfd_byte
*sort
, *s_non_relative
, *p
;
5720 struct elf_link_sort_rela
*sq
;
5721 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5722 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5723 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5724 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5725 struct bfd_link_order
*lo
;
5728 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5729 if (reldyn
== NULL
|| reldyn
->size
== 0)
5731 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5732 if (reldyn
== NULL
|| reldyn
->size
== 0)
5734 ext_size
= bed
->s
->sizeof_rel
;
5735 swap_in
= bed
->s
->swap_reloc_in
;
5736 swap_out
= bed
->s
->swap_reloc_out
;
5740 ext_size
= bed
->s
->sizeof_rela
;
5741 swap_in
= bed
->s
->swap_reloca_in
;
5742 swap_out
= bed
->s
->swap_reloca_out
;
5744 count
= reldyn
->size
/ ext_size
;
5747 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5748 if (lo
->type
== bfd_indirect_link_order
)
5750 asection
*o
= lo
->u
.indirect
.section
;
5754 if (size
!= reldyn
->size
)
5757 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5758 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5759 sort
= bfd_zmalloc (sort_elt
* count
);
5762 (*info
->callbacks
->warning
)
5763 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5767 if (bed
->s
->arch_size
== 32)
5768 r_sym_mask
= ~(bfd_vma
) 0xff;
5770 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5772 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5773 if (lo
->type
== bfd_indirect_link_order
)
5775 bfd_byte
*erel
, *erelend
;
5776 asection
*o
= lo
->u
.indirect
.section
;
5778 if (o
->contents
== NULL
&& o
->size
!= 0)
5780 /* This is a reloc section that is being handled as a normal
5781 section. See bfd_section_from_shdr. We can't combine
5782 relocs in this case. */
5787 erelend
= o
->contents
+ o
->size
;
5788 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5789 while (erel
< erelend
)
5791 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5792 (*swap_in
) (abfd
, erel
, s
->rela
);
5793 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5794 s
->u
.sym_mask
= r_sym_mask
;
5800 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5802 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5804 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5805 if (s
->type
!= reloc_class_relative
)
5811 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5812 for (; i
< count
; i
++, p
+= sort_elt
)
5814 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5815 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5817 sp
->u
.offset
= sq
->rela
->r_offset
;
5820 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5822 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5823 if (lo
->type
== bfd_indirect_link_order
)
5825 bfd_byte
*erel
, *erelend
;
5826 asection
*o
= lo
->u
.indirect
.section
;
5829 erelend
= o
->contents
+ o
->size
;
5830 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5831 while (erel
< erelend
)
5833 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5834 (*swap_out
) (abfd
, s
->rela
, erel
);
5845 /* Flush the output symbols to the file. */
5848 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5849 const struct elf_backend_data
*bed
)
5851 if (finfo
->symbuf_count
> 0)
5853 Elf_Internal_Shdr
*hdr
;
5857 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5858 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5859 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5860 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5861 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5864 hdr
->sh_size
+= amt
;
5865 finfo
->symbuf_count
= 0;
5871 /* Add a symbol to the output symbol table. */
5874 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5876 Elf_Internal_Sym
*elfsym
,
5877 asection
*input_sec
,
5878 struct elf_link_hash_entry
*h
)
5881 Elf_External_Sym_Shndx
*destshndx
;
5882 bfd_boolean (*output_symbol_hook
)
5883 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5884 struct elf_link_hash_entry
*);
5885 const struct elf_backend_data
*bed
;
5887 bed
= get_elf_backend_data (finfo
->output_bfd
);
5888 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5889 if (output_symbol_hook
!= NULL
)
5891 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5895 if (name
== NULL
|| *name
== '\0')
5896 elfsym
->st_name
= 0;
5897 else if (input_sec
->flags
& SEC_EXCLUDE
)
5898 elfsym
->st_name
= 0;
5901 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5903 if (elfsym
->st_name
== (unsigned long) -1)
5907 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5909 if (! elf_link_flush_output_syms (finfo
, bed
))
5913 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5914 destshndx
= finfo
->symshndxbuf
;
5915 if (destshndx
!= NULL
)
5917 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5921 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5922 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5923 if (destshndx
== NULL
)
5925 memset ((char *) destshndx
+ amt
, 0, amt
);
5926 finfo
->shndxbuf_size
*= 2;
5928 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5931 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5932 finfo
->symbuf_count
+= 1;
5933 bfd_get_symcount (finfo
->output_bfd
) += 1;
5938 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5939 allowing an unsatisfied unversioned symbol in the DSO to match a
5940 versioned symbol that would normally require an explicit version.
5941 We also handle the case that a DSO references a hidden symbol
5942 which may be satisfied by a versioned symbol in another DSO. */
5945 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5946 const struct elf_backend_data
*bed
,
5947 struct elf_link_hash_entry
*h
)
5950 struct elf_link_loaded_list
*loaded
;
5952 if (!is_elf_hash_table (info
->hash
))
5955 switch (h
->root
.type
)
5961 case bfd_link_hash_undefined
:
5962 case bfd_link_hash_undefweak
:
5963 abfd
= h
->root
.u
.undef
.abfd
;
5964 if ((abfd
->flags
& DYNAMIC
) == 0
5965 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5969 case bfd_link_hash_defined
:
5970 case bfd_link_hash_defweak
:
5971 abfd
= h
->root
.u
.def
.section
->owner
;
5974 case bfd_link_hash_common
:
5975 abfd
= h
->root
.u
.c
.p
->section
->owner
;
5978 BFD_ASSERT (abfd
!= NULL
);
5980 for (loaded
= elf_hash_table (info
)->loaded
;
5982 loaded
= loaded
->next
)
5985 Elf_Internal_Shdr
*hdr
;
5986 bfd_size_type symcount
;
5987 bfd_size_type extsymcount
;
5988 bfd_size_type extsymoff
;
5989 Elf_Internal_Shdr
*versymhdr
;
5990 Elf_Internal_Sym
*isym
;
5991 Elf_Internal_Sym
*isymend
;
5992 Elf_Internal_Sym
*isymbuf
;
5993 Elf_External_Versym
*ever
;
5994 Elf_External_Versym
*extversym
;
5996 input
= loaded
->abfd
;
5998 /* We check each DSO for a possible hidden versioned definition. */
6000 || (input
->flags
& DYNAMIC
) == 0
6001 || elf_dynversym (input
) == 0)
6004 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6006 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6007 if (elf_bad_symtab (input
))
6009 extsymcount
= symcount
;
6014 extsymcount
= symcount
- hdr
->sh_info
;
6015 extsymoff
= hdr
->sh_info
;
6018 if (extsymcount
== 0)
6021 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6023 if (isymbuf
== NULL
)
6026 /* Read in any version definitions. */
6027 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6028 extversym
= bfd_malloc (versymhdr
->sh_size
);
6029 if (extversym
== NULL
)
6032 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6033 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6034 != versymhdr
->sh_size
))
6042 ever
= extversym
+ extsymoff
;
6043 isymend
= isymbuf
+ extsymcount
;
6044 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6047 Elf_Internal_Versym iver
;
6048 unsigned short version_index
;
6050 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6051 || isym
->st_shndx
== SHN_UNDEF
)
6054 name
= bfd_elf_string_from_elf_section (input
,
6057 if (strcmp (name
, h
->root
.root
.string
) != 0)
6060 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6062 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6064 /* If we have a non-hidden versioned sym, then it should
6065 have provided a definition for the undefined sym. */
6069 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6070 if (version_index
== 1 || version_index
== 2)
6072 /* This is the base or first version. We can use it. */
6086 /* Add an external symbol to the symbol table. This is called from
6087 the hash table traversal routine. When generating a shared object,
6088 we go through the symbol table twice. The first time we output
6089 anything that might have been forced to local scope in a version
6090 script. The second time we output the symbols that are still
6094 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6096 struct elf_outext_info
*eoinfo
= data
;
6097 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6099 Elf_Internal_Sym sym
;
6100 asection
*input_sec
;
6101 const struct elf_backend_data
*bed
;
6103 if (h
->root
.type
== bfd_link_hash_warning
)
6105 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6106 if (h
->root
.type
== bfd_link_hash_new
)
6110 /* Decide whether to output this symbol in this pass. */
6111 if (eoinfo
->localsyms
)
6113 if (!h
->forced_local
)
6118 if (h
->forced_local
)
6122 bed
= get_elf_backend_data (finfo
->output_bfd
);
6124 /* If we have an undefined symbol reference here then it must have
6125 come from a shared library that is being linked in. (Undefined
6126 references in regular files have already been handled). If we
6127 are reporting errors for this situation then do so now. */
6128 if (h
->root
.type
== bfd_link_hash_undefined
6131 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6132 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6134 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6135 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6136 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6138 eoinfo
->failed
= TRUE
;
6143 /* We should also warn if a forced local symbol is referenced from
6144 shared libraries. */
6145 if (! finfo
->info
->relocatable
6146 && (! finfo
->info
->shared
)
6151 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6153 (*_bfd_error_handler
)
6154 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6155 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6156 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6158 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6159 ? "hidden" : "local",
6160 h
->root
.root
.string
);
6161 eoinfo
->failed
= TRUE
;
6165 /* We don't want to output symbols that have never been mentioned by
6166 a regular file, or that we have been told to strip. However, if
6167 h->indx is set to -2, the symbol is used by a reloc and we must
6171 else if ((h
->def_dynamic
6176 else if (finfo
->info
->strip
== strip_all
)
6178 else if (finfo
->info
->strip
== strip_some
6179 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6180 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6182 else if (finfo
->info
->strip_discarded
6183 && (h
->root
.type
== bfd_link_hash_defined
6184 || h
->root
.type
== bfd_link_hash_defweak
)
6185 && elf_discarded_section (h
->root
.u
.def
.section
))
6190 /* If we're stripping it, and it's not a dynamic symbol, there's
6191 nothing else to do unless it is a forced local symbol. */
6194 && !h
->forced_local
)
6198 sym
.st_size
= h
->size
;
6199 sym
.st_other
= h
->other
;
6200 if (h
->forced_local
)
6201 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6202 else if (h
->root
.type
== bfd_link_hash_undefweak
6203 || h
->root
.type
== bfd_link_hash_defweak
)
6204 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6206 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6208 switch (h
->root
.type
)
6211 case bfd_link_hash_new
:
6212 case bfd_link_hash_warning
:
6216 case bfd_link_hash_undefined
:
6217 case bfd_link_hash_undefweak
:
6218 input_sec
= bfd_und_section_ptr
;
6219 sym
.st_shndx
= SHN_UNDEF
;
6222 case bfd_link_hash_defined
:
6223 case bfd_link_hash_defweak
:
6225 input_sec
= h
->root
.u
.def
.section
;
6226 if (input_sec
->output_section
!= NULL
)
6229 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6230 input_sec
->output_section
);
6231 if (sym
.st_shndx
== SHN_BAD
)
6233 (*_bfd_error_handler
)
6234 (_("%B: could not find output section %A for input section %A"),
6235 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6236 eoinfo
->failed
= TRUE
;
6240 /* ELF symbols in relocatable files are section relative,
6241 but in nonrelocatable files they are virtual
6243 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6244 if (! finfo
->info
->relocatable
)
6246 sym
.st_value
+= input_sec
->output_section
->vma
;
6247 if (h
->type
== STT_TLS
)
6249 /* STT_TLS symbols are relative to PT_TLS segment
6251 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6252 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6258 BFD_ASSERT (input_sec
->owner
== NULL
6259 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6260 sym
.st_shndx
= SHN_UNDEF
;
6261 input_sec
= bfd_und_section_ptr
;
6266 case bfd_link_hash_common
:
6267 input_sec
= h
->root
.u
.c
.p
->section
;
6268 sym
.st_shndx
= SHN_COMMON
;
6269 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6272 case bfd_link_hash_indirect
:
6273 /* These symbols are created by symbol versioning. They point
6274 to the decorated version of the name. For example, if the
6275 symbol foo@@GNU_1.2 is the default, which should be used when
6276 foo is used with no version, then we add an indirect symbol
6277 foo which points to foo@@GNU_1.2. We ignore these symbols,
6278 since the indirected symbol is already in the hash table. */
6282 /* Give the processor backend a chance to tweak the symbol value,
6283 and also to finish up anything that needs to be done for this
6284 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6285 forced local syms when non-shared is due to a historical quirk. */
6286 if ((h
->dynindx
!= -1
6288 && ((finfo
->info
->shared
6289 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6290 || h
->root
.type
!= bfd_link_hash_undefweak
))
6291 || !h
->forced_local
)
6292 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6294 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6295 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6297 eoinfo
->failed
= TRUE
;
6302 /* If we are marking the symbol as undefined, and there are no
6303 non-weak references to this symbol from a regular object, then
6304 mark the symbol as weak undefined; if there are non-weak
6305 references, mark the symbol as strong. We can't do this earlier,
6306 because it might not be marked as undefined until the
6307 finish_dynamic_symbol routine gets through with it. */
6308 if (sym
.st_shndx
== SHN_UNDEF
6310 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6311 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6315 if (h
->ref_regular_nonweak
)
6316 bindtype
= STB_GLOBAL
;
6318 bindtype
= STB_WEAK
;
6319 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6322 /* If a non-weak symbol with non-default visibility is not defined
6323 locally, it is a fatal error. */
6324 if (! finfo
->info
->relocatable
6325 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6326 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6327 && h
->root
.type
== bfd_link_hash_undefined
6330 (*_bfd_error_handler
)
6331 (_("%B: %s symbol `%s' isn't defined"),
6333 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6335 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6336 ? "internal" : "hidden",
6337 h
->root
.root
.string
);
6338 eoinfo
->failed
= TRUE
;
6342 /* If this symbol should be put in the .dynsym section, then put it
6343 there now. We already know the symbol index. We also fill in
6344 the entry in the .hash section. */
6345 if (h
->dynindx
!= -1
6346 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6350 size_t hash_entry_size
;
6351 bfd_byte
*bucketpos
;
6355 sym
.st_name
= h
->dynstr_index
;
6356 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6357 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6359 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6360 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6362 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6363 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6364 + (bucket
+ 2) * hash_entry_size
);
6365 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6366 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6367 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6368 ((bfd_byte
*) finfo
->hash_sec
->contents
6369 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6371 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6373 Elf_Internal_Versym iversym
;
6374 Elf_External_Versym
*eversym
;
6376 if (!h
->def_regular
)
6378 if (h
->verinfo
.verdef
== NULL
)
6379 iversym
.vs_vers
= 0;
6381 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6385 if (h
->verinfo
.vertree
== NULL
)
6386 iversym
.vs_vers
= 1;
6388 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6389 if (finfo
->info
->create_default_symver
)
6394 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6396 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6397 eversym
+= h
->dynindx
;
6398 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6402 /* If we're stripping it, then it was just a dynamic symbol, and
6403 there's nothing else to do. */
6404 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6407 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6409 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6411 eoinfo
->failed
= TRUE
;
6418 /* Return TRUE if special handling is done for relocs in SEC against
6419 symbols defined in discarded sections. */
6422 elf_section_ignore_discarded_relocs (asection
*sec
)
6424 const struct elf_backend_data
*bed
;
6426 switch (sec
->sec_info_type
)
6428 case ELF_INFO_TYPE_STABS
:
6429 case ELF_INFO_TYPE_EH_FRAME
:
6435 bed
= get_elf_backend_data (sec
->owner
);
6436 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6437 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6443 enum action_discarded
6449 /* Return a mask saying how ld should treat relocations in SEC against
6450 symbols defined in discarded sections. If this function returns
6451 COMPLAIN set, ld will issue a warning message. If this function
6452 returns PRETEND set, and the discarded section was link-once and the
6453 same size as the kept link-once section, ld will pretend that the
6454 symbol was actually defined in the kept section. Otherwise ld will
6455 zero the reloc (at least that is the intent, but some cooperation by
6456 the target dependent code is needed, particularly for REL targets). */
6459 elf_action_discarded (asection
*sec
)
6461 if (sec
->flags
& SEC_DEBUGGING
)
6464 if (strcmp (".eh_frame", sec
->name
) == 0)
6467 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6470 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6473 if (strcmp (".fixup", sec
->name
) == 0)
6476 return COMPLAIN
| PRETEND
;
6479 /* Find a match between a section and a member of a section group. */
6482 match_group_member (asection
*sec
, asection
*group
)
6484 asection
*first
= elf_next_in_group (group
);
6485 asection
*s
= first
;
6489 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6499 /* Link an input file into the linker output file. This function
6500 handles all the sections and relocations of the input file at once.
6501 This is so that we only have to read the local symbols once, and
6502 don't have to keep them in memory. */
6505 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6507 bfd_boolean (*relocate_section
)
6508 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6509 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6511 Elf_Internal_Shdr
*symtab_hdr
;
6514 Elf_Internal_Sym
*isymbuf
;
6515 Elf_Internal_Sym
*isym
;
6516 Elf_Internal_Sym
*isymend
;
6518 asection
**ppsection
;
6520 const struct elf_backend_data
*bed
;
6521 bfd_boolean emit_relocs
;
6522 struct elf_link_hash_entry
**sym_hashes
;
6524 output_bfd
= finfo
->output_bfd
;
6525 bed
= get_elf_backend_data (output_bfd
);
6526 relocate_section
= bed
->elf_backend_relocate_section
;
6528 /* If this is a dynamic object, we don't want to do anything here:
6529 we don't want the local symbols, and we don't want the section
6531 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6534 emit_relocs
= (finfo
->info
->relocatable
6535 || finfo
->info
->emitrelocations
6536 || bed
->elf_backend_emit_relocs
);
6538 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6539 if (elf_bad_symtab (input_bfd
))
6541 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6546 locsymcount
= symtab_hdr
->sh_info
;
6547 extsymoff
= symtab_hdr
->sh_info
;
6550 /* Read the local symbols. */
6551 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6552 if (isymbuf
== NULL
&& locsymcount
!= 0)
6554 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6555 finfo
->internal_syms
,
6556 finfo
->external_syms
,
6557 finfo
->locsym_shndx
);
6558 if (isymbuf
== NULL
)
6562 /* Find local symbol sections and adjust values of symbols in
6563 SEC_MERGE sections. Write out those local symbols we know are
6564 going into the output file. */
6565 isymend
= isymbuf
+ locsymcount
;
6566 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6568 isym
++, pindex
++, ppsection
++)
6572 Elf_Internal_Sym osym
;
6576 if (elf_bad_symtab (input_bfd
))
6578 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6585 if (isym
->st_shndx
== SHN_UNDEF
)
6586 isec
= bfd_und_section_ptr
;
6587 else if (isym
->st_shndx
< SHN_LORESERVE
6588 || isym
->st_shndx
> SHN_HIRESERVE
)
6590 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6592 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6593 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6595 _bfd_merged_section_offset (output_bfd
, &isec
,
6596 elf_section_data (isec
)->sec_info
,
6599 else if (isym
->st_shndx
== SHN_ABS
)
6600 isec
= bfd_abs_section_ptr
;
6601 else if (isym
->st_shndx
== SHN_COMMON
)
6602 isec
= bfd_com_section_ptr
;
6611 /* Don't output the first, undefined, symbol. */
6612 if (ppsection
== finfo
->sections
)
6615 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6617 /* We never output section symbols. Instead, we use the
6618 section symbol of the corresponding section in the output
6623 /* If we are stripping all symbols, we don't want to output this
6625 if (finfo
->info
->strip
== strip_all
)
6628 /* If we are discarding all local symbols, we don't want to
6629 output this one. If we are generating a relocatable output
6630 file, then some of the local symbols may be required by
6631 relocs; we output them below as we discover that they are
6633 if (finfo
->info
->discard
== discard_all
)
6636 /* If this symbol is defined in a section which we are
6637 discarding, we don't need to keep it, but note that
6638 linker_mark is only reliable for sections that have contents.
6639 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6640 as well as linker_mark. */
6641 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6643 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6644 || (! finfo
->info
->relocatable
6645 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6648 /* Get the name of the symbol. */
6649 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6654 /* See if we are discarding symbols with this name. */
6655 if ((finfo
->info
->strip
== strip_some
6656 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6658 || (((finfo
->info
->discard
== discard_sec_merge
6659 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6660 || finfo
->info
->discard
== discard_l
)
6661 && bfd_is_local_label_name (input_bfd
, name
)))
6664 /* If we get here, we are going to output this symbol. */
6668 /* Adjust the section index for the output file. */
6669 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6670 isec
->output_section
);
6671 if (osym
.st_shndx
== SHN_BAD
)
6674 *pindex
= bfd_get_symcount (output_bfd
);
6676 /* ELF symbols in relocatable files are section relative, but
6677 in executable files they are virtual addresses. Note that
6678 this code assumes that all ELF sections have an associated
6679 BFD section with a reasonable value for output_offset; below
6680 we assume that they also have a reasonable value for
6681 output_section. Any special sections must be set up to meet
6682 these requirements. */
6683 osym
.st_value
+= isec
->output_offset
;
6684 if (! finfo
->info
->relocatable
)
6686 osym
.st_value
+= isec
->output_section
->vma
;
6687 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6689 /* STT_TLS symbols are relative to PT_TLS segment base. */
6690 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6691 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6695 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6699 /* Relocate the contents of each section. */
6700 sym_hashes
= elf_sym_hashes (input_bfd
);
6701 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6705 if (! o
->linker_mark
)
6707 /* This section was omitted from the link. */
6711 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6712 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6715 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6717 /* Section was created by _bfd_elf_link_create_dynamic_sections
6722 /* Get the contents of the section. They have been cached by a
6723 relaxation routine. Note that o is a section in an input
6724 file, so the contents field will not have been set by any of
6725 the routines which work on output files. */
6726 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6727 contents
= elf_section_data (o
)->this_hdr
.contents
;
6730 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6732 contents
= finfo
->contents
;
6733 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6737 if ((o
->flags
& SEC_RELOC
) != 0)
6739 Elf_Internal_Rela
*internal_relocs
;
6740 bfd_vma r_type_mask
;
6743 /* Get the swapped relocs. */
6745 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6746 finfo
->internal_relocs
, FALSE
);
6747 if (internal_relocs
== NULL
6748 && o
->reloc_count
> 0)
6751 if (bed
->s
->arch_size
== 32)
6758 r_type_mask
= 0xffffffff;
6762 /* Run through the relocs looking for any against symbols
6763 from discarded sections and section symbols from
6764 removed link-once sections. Complain about relocs
6765 against discarded sections. Zero relocs against removed
6766 link-once sections. Preserve debug information as much
6768 if (!elf_section_ignore_discarded_relocs (o
))
6770 Elf_Internal_Rela
*rel
, *relend
;
6771 unsigned int action
= elf_action_discarded (o
);
6773 rel
= internal_relocs
;
6774 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6775 for ( ; rel
< relend
; rel
++)
6777 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6778 asection
**ps
, *sec
;
6779 struct elf_link_hash_entry
*h
= NULL
;
6780 const char *sym_name
;
6782 if (r_symndx
== STN_UNDEF
)
6785 if (r_symndx
>= locsymcount
6786 || (elf_bad_symtab (input_bfd
)
6787 && finfo
->sections
[r_symndx
] == NULL
))
6789 h
= sym_hashes
[r_symndx
- extsymoff
];
6790 while (h
->root
.type
== bfd_link_hash_indirect
6791 || h
->root
.type
== bfd_link_hash_warning
)
6792 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6794 if (h
->root
.type
!= bfd_link_hash_defined
6795 && h
->root
.type
!= bfd_link_hash_defweak
)
6798 ps
= &h
->root
.u
.def
.section
;
6799 sym_name
= h
->root
.root
.string
;
6803 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6804 ps
= &finfo
->sections
[r_symndx
];
6805 sym_name
= bfd_elf_local_sym_name (input_bfd
, sym
);
6808 /* Complain if the definition comes from a
6809 discarded section. */
6810 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6814 BFD_ASSERT (r_symndx
!= 0);
6815 if (action
& COMPLAIN
)
6817 (*_bfd_error_handler
)
6818 (_("`%s' referenced in section `%A' of %B: "
6819 "defined in discarded section `%A' of %B\n"),
6820 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6823 /* Try to do the best we can to support buggy old
6824 versions of gcc. If we've warned, or this is
6825 debugging info, pretend that the symbol is
6826 really defined in the kept linkonce section.
6827 FIXME: This is quite broken. Modifying the
6828 symbol here means we will be changing all later
6829 uses of the symbol, not just in this section.
6830 The only thing that makes this half reasonable
6831 is that we warn in non-debug sections, and
6832 debug sections tend to come after other
6834 kept
= sec
->kept_section
;
6835 if (kept
!= NULL
&& (action
& PRETEND
))
6837 if (elf_sec_group (sec
) != NULL
)
6838 kept
= match_group_member (sec
, kept
);
6840 && sec
->size
== kept
->size
)
6847 /* Remove the symbol reference from the reloc, but
6848 don't kill the reloc completely. This is so that
6849 a zero value will be written into the section,
6850 which may have non-zero contents put there by the
6851 assembler. Zero in things like an eh_frame fde
6852 pc_begin allows stack unwinders to recognize the
6854 rel
->r_info
&= r_type_mask
;
6860 /* Relocate the section by invoking a back end routine.
6862 The back end routine is responsible for adjusting the
6863 section contents as necessary, and (if using Rela relocs
6864 and generating a relocatable output file) adjusting the
6865 reloc addend as necessary.
6867 The back end routine does not have to worry about setting
6868 the reloc address or the reloc symbol index.
6870 The back end routine is given a pointer to the swapped in
6871 internal symbols, and can access the hash table entries
6872 for the external symbols via elf_sym_hashes (input_bfd).
6874 When generating relocatable output, the back end routine
6875 must handle STB_LOCAL/STT_SECTION symbols specially. The
6876 output symbol is going to be a section symbol
6877 corresponding to the output section, which will require
6878 the addend to be adjusted. */
6880 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6881 input_bfd
, o
, contents
,
6889 Elf_Internal_Rela
*irela
;
6890 Elf_Internal_Rela
*irelaend
;
6891 bfd_vma last_offset
;
6892 struct elf_link_hash_entry
**rel_hash
;
6893 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6894 unsigned int next_erel
;
6895 bfd_boolean (*reloc_emitter
)
6896 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6897 bfd_boolean rela_normal
;
6899 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6900 rela_normal
= (bed
->rela_normal
6901 && (input_rel_hdr
->sh_entsize
6902 == bed
->s
->sizeof_rela
));
6904 /* Adjust the reloc addresses and symbol indices. */
6906 irela
= internal_relocs
;
6907 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6908 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6909 + elf_section_data (o
->output_section
)->rel_count
6910 + elf_section_data (o
->output_section
)->rel_count2
);
6911 last_offset
= o
->output_offset
;
6912 if (!finfo
->info
->relocatable
)
6913 last_offset
+= o
->output_section
->vma
;
6914 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6916 unsigned long r_symndx
;
6918 Elf_Internal_Sym sym
;
6920 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6926 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6929 if (irela
->r_offset
>= (bfd_vma
) -2)
6931 /* This is a reloc for a deleted entry or somesuch.
6932 Turn it into an R_*_NONE reloc, at the same
6933 offset as the last reloc. elf_eh_frame.c and
6934 elf_bfd_discard_info rely on reloc offsets
6936 irela
->r_offset
= last_offset
;
6938 irela
->r_addend
= 0;
6942 irela
->r_offset
+= o
->output_offset
;
6944 /* Relocs in an executable have to be virtual addresses. */
6945 if (!finfo
->info
->relocatable
)
6946 irela
->r_offset
+= o
->output_section
->vma
;
6948 last_offset
= irela
->r_offset
;
6950 r_symndx
= irela
->r_info
>> r_sym_shift
;
6951 if (r_symndx
== STN_UNDEF
)
6954 if (r_symndx
>= locsymcount
6955 || (elf_bad_symtab (input_bfd
)
6956 && finfo
->sections
[r_symndx
] == NULL
))
6958 struct elf_link_hash_entry
*rh
;
6961 /* This is a reloc against a global symbol. We
6962 have not yet output all the local symbols, so
6963 we do not know the symbol index of any global
6964 symbol. We set the rel_hash entry for this
6965 reloc to point to the global hash table entry
6966 for this symbol. The symbol index is then
6967 set at the end of bfd_elf_final_link. */
6968 indx
= r_symndx
- extsymoff
;
6969 rh
= elf_sym_hashes (input_bfd
)[indx
];
6970 while (rh
->root
.type
== bfd_link_hash_indirect
6971 || rh
->root
.type
== bfd_link_hash_warning
)
6972 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
6974 /* Setting the index to -2 tells
6975 elf_link_output_extsym that this symbol is
6977 BFD_ASSERT (rh
->indx
< 0);
6985 /* This is a reloc against a local symbol. */
6988 sym
= isymbuf
[r_symndx
];
6989 sec
= finfo
->sections
[r_symndx
];
6990 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
6992 /* I suppose the backend ought to fill in the
6993 section of any STT_SECTION symbol against a
6994 processor specific section. */
6996 if (bfd_is_abs_section (sec
))
6998 else if (sec
== NULL
|| sec
->owner
== NULL
)
7000 bfd_set_error (bfd_error_bad_value
);
7005 asection
*osec
= sec
->output_section
;
7007 /* If we have discarded a section, the output
7008 section will be the absolute section. In
7009 case of discarded link-once and discarded
7010 SEC_MERGE sections, use the kept section. */
7011 if (bfd_is_abs_section (osec
)
7012 && sec
->kept_section
!= NULL
7013 && sec
->kept_section
->output_section
!= NULL
)
7015 osec
= sec
->kept_section
->output_section
;
7016 irela
->r_addend
-= osec
->vma
;
7019 if (!bfd_is_abs_section (osec
))
7021 r_symndx
= osec
->target_index
;
7022 BFD_ASSERT (r_symndx
!= 0);
7026 /* Adjust the addend according to where the
7027 section winds up in the output section. */
7029 irela
->r_addend
+= sec
->output_offset
;
7033 if (finfo
->indices
[r_symndx
] == -1)
7035 unsigned long shlink
;
7039 if (finfo
->info
->strip
== strip_all
)
7041 /* You can't do ld -r -s. */
7042 bfd_set_error (bfd_error_invalid_operation
);
7046 /* This symbol was skipped earlier, but
7047 since it is needed by a reloc, we
7048 must output it now. */
7049 shlink
= symtab_hdr
->sh_link
;
7050 name
= (bfd_elf_string_from_elf_section
7051 (input_bfd
, shlink
, sym
.st_name
));
7055 osec
= sec
->output_section
;
7057 _bfd_elf_section_from_bfd_section (output_bfd
,
7059 if (sym
.st_shndx
== SHN_BAD
)
7062 sym
.st_value
+= sec
->output_offset
;
7063 if (! finfo
->info
->relocatable
)
7065 sym
.st_value
+= osec
->vma
;
7066 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7068 /* STT_TLS symbols are relative to PT_TLS
7070 BFD_ASSERT (elf_hash_table (finfo
->info
)
7072 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7077 finfo
->indices
[r_symndx
]
7078 = bfd_get_symcount (output_bfd
);
7080 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7085 r_symndx
= finfo
->indices
[r_symndx
];
7088 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7089 | (irela
->r_info
& r_type_mask
));
7092 /* Swap out the relocs. */
7093 if (bed
->elf_backend_emit_relocs
7094 && !(finfo
->info
->relocatable
7095 || finfo
->info
->emitrelocations
))
7096 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7098 reloc_emitter
= _bfd_elf_link_output_relocs
;
7100 if (input_rel_hdr
->sh_size
!= 0
7101 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7105 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7106 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7108 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7109 * bed
->s
->int_rels_per_ext_rel
);
7110 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7117 /* Write out the modified section contents. */
7118 if (bed
->elf_backend_write_section
7119 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7121 /* Section written out. */
7123 else switch (o
->sec_info_type
)
7125 case ELF_INFO_TYPE_STABS
:
7126 if (! (_bfd_write_section_stabs
7128 &elf_hash_table (finfo
->info
)->stab_info
,
7129 o
, &elf_section_data (o
)->sec_info
, contents
)))
7132 case ELF_INFO_TYPE_MERGE
:
7133 if (! _bfd_write_merged_section (output_bfd
, o
,
7134 elf_section_data (o
)->sec_info
))
7137 case ELF_INFO_TYPE_EH_FRAME
:
7139 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7146 if (! (o
->flags
& SEC_EXCLUDE
)
7147 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7149 (file_ptr
) o
->output_offset
,
7160 /* Generate a reloc when linking an ELF file. This is a reloc
7161 requested by the linker, and does come from any input file. This
7162 is used to build constructor and destructor tables when linking
7166 elf_reloc_link_order (bfd
*output_bfd
,
7167 struct bfd_link_info
*info
,
7168 asection
*output_section
,
7169 struct bfd_link_order
*link_order
)
7171 reloc_howto_type
*howto
;
7175 struct elf_link_hash_entry
**rel_hash_ptr
;
7176 Elf_Internal_Shdr
*rel_hdr
;
7177 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7178 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7182 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7185 bfd_set_error (bfd_error_bad_value
);
7189 addend
= link_order
->u
.reloc
.p
->addend
;
7191 /* Figure out the symbol index. */
7192 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7193 + elf_section_data (output_section
)->rel_count
7194 + elf_section_data (output_section
)->rel_count2
);
7195 if (link_order
->type
== bfd_section_reloc_link_order
)
7197 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7198 BFD_ASSERT (indx
!= 0);
7199 *rel_hash_ptr
= NULL
;
7203 struct elf_link_hash_entry
*h
;
7205 /* Treat a reloc against a defined symbol as though it were
7206 actually against the section. */
7207 h
= ((struct elf_link_hash_entry
*)
7208 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7209 link_order
->u
.reloc
.p
->u
.name
,
7210 FALSE
, FALSE
, TRUE
));
7212 && (h
->root
.type
== bfd_link_hash_defined
7213 || h
->root
.type
== bfd_link_hash_defweak
))
7217 section
= h
->root
.u
.def
.section
;
7218 indx
= section
->output_section
->target_index
;
7219 *rel_hash_ptr
= NULL
;
7220 /* It seems that we ought to add the symbol value to the
7221 addend here, but in practice it has already been added
7222 because it was passed to constructor_callback. */
7223 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7227 /* Setting the index to -2 tells elf_link_output_extsym that
7228 this symbol is used by a reloc. */
7235 if (! ((*info
->callbacks
->unattached_reloc
)
7236 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7242 /* If this is an inplace reloc, we must write the addend into the
7244 if (howto
->partial_inplace
&& addend
!= 0)
7247 bfd_reloc_status_type rstat
;
7250 const char *sym_name
;
7252 size
= bfd_get_reloc_size (howto
);
7253 buf
= bfd_zmalloc (size
);
7256 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7263 case bfd_reloc_outofrange
:
7266 case bfd_reloc_overflow
:
7267 if (link_order
->type
== bfd_section_reloc_link_order
)
7268 sym_name
= bfd_section_name (output_bfd
,
7269 link_order
->u
.reloc
.p
->u
.section
);
7271 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7272 if (! ((*info
->callbacks
->reloc_overflow
)
7273 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7274 NULL
, (bfd_vma
) 0)))
7281 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7282 link_order
->offset
, size
);
7288 /* The address of a reloc is relative to the section in a
7289 relocatable file, and is a virtual address in an executable
7291 offset
= link_order
->offset
;
7292 if (! info
->relocatable
)
7293 offset
+= output_section
->vma
;
7295 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7297 irel
[i
].r_offset
= offset
;
7299 irel
[i
].r_addend
= 0;
7301 if (bed
->s
->arch_size
== 32)
7302 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7304 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7306 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7307 erel
= rel_hdr
->contents
;
7308 if (rel_hdr
->sh_type
== SHT_REL
)
7310 erel
+= (elf_section_data (output_section
)->rel_count
7311 * bed
->s
->sizeof_rel
);
7312 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7316 irel
[0].r_addend
= addend
;
7317 erel
+= (elf_section_data (output_section
)->rel_count
7318 * bed
->s
->sizeof_rela
);
7319 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7322 ++elf_section_data (output_section
)->rel_count
;
7328 /* Get the output vma of the section pointed to by the sh_link field. */
7331 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7333 Elf_Internal_Shdr
**elf_shdrp
;
7337 s
= p
->u
.indirect
.section
;
7338 elf_shdrp
= elf_elfsections (s
->owner
);
7339 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7340 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7342 The Intel C compiler generates SHT_IA_64_UNWIND with
7343 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7344 sh_info fields. Hence we could get the situation
7345 where elfsec is 0. */
7348 const struct elf_backend_data
*bed
7349 = get_elf_backend_data (s
->owner
);
7350 if (bed
->link_order_error_handler
)
7351 bed
->link_order_error_handler
7352 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7357 s
= elf_shdrp
[elfsec
]->bfd_section
;
7358 return s
->output_section
->vma
+ s
->output_offset
;
7363 /* Compare two sections based on the locations of the sections they are
7364 linked to. Used by elf_fixup_link_order. */
7367 compare_link_order (const void * a
, const void * b
)
7372 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7373 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7380 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7381 order as their linked sections. Returns false if this could not be done
7382 because an output section includes both ordered and unordered
7383 sections. Ideally we'd do this in the linker proper. */
7386 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7391 struct bfd_link_order
*p
;
7393 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7395 struct bfd_link_order
**sections
;
7401 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7403 if (p
->type
== bfd_indirect_link_order
7404 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7405 == bfd_target_elf_flavour
)
7406 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7408 s
= p
->u
.indirect
.section
;
7409 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7411 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7420 if (!seen_linkorder
)
7423 if (seen_other
&& seen_linkorder
)
7425 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7427 bfd_set_error (bfd_error_bad_value
);
7431 sections
= (struct bfd_link_order
**)
7432 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7435 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7437 sections
[seen_linkorder
++] = p
;
7439 /* Sort the input sections in the order of their linked section. */
7440 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7441 compare_link_order
);
7443 /* Change the offsets of the sections. */
7445 for (n
= 0; n
< seen_linkorder
; n
++)
7447 s
= sections
[n
]->u
.indirect
.section
;
7448 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7449 s
->output_offset
= offset
;
7450 sections
[n
]->offset
= offset
;
7451 offset
+= sections
[n
]->size
;
7458 /* Do the final step of an ELF link. */
7461 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7463 bfd_boolean dynamic
;
7464 bfd_boolean emit_relocs
;
7466 struct elf_final_link_info finfo
;
7467 register asection
*o
;
7468 register struct bfd_link_order
*p
;
7470 bfd_size_type max_contents_size
;
7471 bfd_size_type max_external_reloc_size
;
7472 bfd_size_type max_internal_reloc_count
;
7473 bfd_size_type max_sym_count
;
7474 bfd_size_type max_sym_shndx_count
;
7476 Elf_Internal_Sym elfsym
;
7478 Elf_Internal_Shdr
*symtab_hdr
;
7479 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7480 Elf_Internal_Shdr
*symstrtab_hdr
;
7481 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7482 struct elf_outext_info eoinfo
;
7484 size_t relativecount
= 0;
7485 asection
*reldyn
= 0;
7488 if (! is_elf_hash_table (info
->hash
))
7492 abfd
->flags
|= DYNAMIC
;
7494 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7495 dynobj
= elf_hash_table (info
)->dynobj
;
7497 emit_relocs
= (info
->relocatable
7498 || info
->emitrelocations
7499 || bed
->elf_backend_emit_relocs
);
7502 finfo
.output_bfd
= abfd
;
7503 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7504 if (finfo
.symstrtab
== NULL
)
7509 finfo
.dynsym_sec
= NULL
;
7510 finfo
.hash_sec
= NULL
;
7511 finfo
.symver_sec
= NULL
;
7515 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7516 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7517 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7518 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7519 /* Note that it is OK if symver_sec is NULL. */
7522 finfo
.contents
= NULL
;
7523 finfo
.external_relocs
= NULL
;
7524 finfo
.internal_relocs
= NULL
;
7525 finfo
.external_syms
= NULL
;
7526 finfo
.locsym_shndx
= NULL
;
7527 finfo
.internal_syms
= NULL
;
7528 finfo
.indices
= NULL
;
7529 finfo
.sections
= NULL
;
7530 finfo
.symbuf
= NULL
;
7531 finfo
.symshndxbuf
= NULL
;
7532 finfo
.symbuf_count
= 0;
7533 finfo
.shndxbuf_size
= 0;
7535 /* Count up the number of relocations we will output for each output
7536 section, so that we know the sizes of the reloc sections. We
7537 also figure out some maximum sizes. */
7538 max_contents_size
= 0;
7539 max_external_reloc_size
= 0;
7540 max_internal_reloc_count
= 0;
7542 max_sym_shndx_count
= 0;
7544 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7546 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7549 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7551 unsigned int reloc_count
= 0;
7552 struct bfd_elf_section_data
*esdi
= NULL
;
7553 unsigned int *rel_count1
;
7555 if (p
->type
== bfd_section_reloc_link_order
7556 || p
->type
== bfd_symbol_reloc_link_order
)
7558 else if (p
->type
== bfd_indirect_link_order
)
7562 sec
= p
->u
.indirect
.section
;
7563 esdi
= elf_section_data (sec
);
7565 /* Mark all sections which are to be included in the
7566 link. This will normally be every section. We need
7567 to do this so that we can identify any sections which
7568 the linker has decided to not include. */
7569 sec
->linker_mark
= TRUE
;
7571 if (sec
->flags
& SEC_MERGE
)
7574 if (info
->relocatable
|| info
->emitrelocations
)
7575 reloc_count
= sec
->reloc_count
;
7576 else if (bed
->elf_backend_count_relocs
)
7578 Elf_Internal_Rela
* relocs
;
7580 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7583 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7585 if (elf_section_data (o
)->relocs
!= relocs
)
7589 if (sec
->rawsize
> max_contents_size
)
7590 max_contents_size
= sec
->rawsize
;
7591 if (sec
->size
> max_contents_size
)
7592 max_contents_size
= sec
->size
;
7594 /* We are interested in just local symbols, not all
7596 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7597 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7601 if (elf_bad_symtab (sec
->owner
))
7602 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7603 / bed
->s
->sizeof_sym
);
7605 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7607 if (sym_count
> max_sym_count
)
7608 max_sym_count
= sym_count
;
7610 if (sym_count
> max_sym_shndx_count
7611 && elf_symtab_shndx (sec
->owner
) != 0)
7612 max_sym_shndx_count
= sym_count
;
7614 if ((sec
->flags
& SEC_RELOC
) != 0)
7618 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7619 if (ext_size
> max_external_reloc_size
)
7620 max_external_reloc_size
= ext_size
;
7621 if (sec
->reloc_count
> max_internal_reloc_count
)
7622 max_internal_reloc_count
= sec
->reloc_count
;
7627 if (reloc_count
== 0)
7630 o
->reloc_count
+= reloc_count
;
7632 /* MIPS may have a mix of REL and RELA relocs on sections.
7633 To support this curious ABI we keep reloc counts in
7634 elf_section_data too. We must be careful to add the
7635 relocations from the input section to the right output
7636 count. FIXME: Get rid of one count. We have
7637 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7638 rel_count1
= &esdo
->rel_count
;
7641 bfd_boolean same_size
;
7642 bfd_size_type entsize1
;
7644 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7645 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7646 || entsize1
== bed
->s
->sizeof_rela
);
7647 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7650 rel_count1
= &esdo
->rel_count2
;
7652 if (esdi
->rel_hdr2
!= NULL
)
7654 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7655 unsigned int alt_count
;
7656 unsigned int *rel_count2
;
7658 BFD_ASSERT (entsize2
!= entsize1
7659 && (entsize2
== bed
->s
->sizeof_rel
7660 || entsize2
== bed
->s
->sizeof_rela
));
7662 rel_count2
= &esdo
->rel_count2
;
7664 rel_count2
= &esdo
->rel_count
;
7666 /* The following is probably too simplistic if the
7667 backend counts output relocs unusually. */
7668 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7669 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7670 *rel_count2
+= alt_count
;
7671 reloc_count
-= alt_count
;
7674 *rel_count1
+= reloc_count
;
7677 if (o
->reloc_count
> 0)
7678 o
->flags
|= SEC_RELOC
;
7681 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7682 set it (this is probably a bug) and if it is set
7683 assign_section_numbers will create a reloc section. */
7684 o
->flags
&=~ SEC_RELOC
;
7687 /* If the SEC_ALLOC flag is not set, force the section VMA to
7688 zero. This is done in elf_fake_sections as well, but forcing
7689 the VMA to 0 here will ensure that relocs against these
7690 sections are handled correctly. */
7691 if ((o
->flags
& SEC_ALLOC
) == 0
7692 && ! o
->user_set_vma
)
7696 if (! info
->relocatable
&& merged
)
7697 elf_link_hash_traverse (elf_hash_table (info
),
7698 _bfd_elf_link_sec_merge_syms
, abfd
);
7700 /* Figure out the file positions for everything but the symbol table
7701 and the relocs. We set symcount to force assign_section_numbers
7702 to create a symbol table. */
7703 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7704 BFD_ASSERT (! abfd
->output_has_begun
);
7705 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7708 /* Set sizes, and assign file positions for reloc sections. */
7709 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7711 if ((o
->flags
& SEC_RELOC
) != 0)
7713 if (!(_bfd_elf_link_size_reloc_section
7714 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7717 if (elf_section_data (o
)->rel_hdr2
7718 && !(_bfd_elf_link_size_reloc_section
7719 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7723 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7724 to count upwards while actually outputting the relocations. */
7725 elf_section_data (o
)->rel_count
= 0;
7726 elf_section_data (o
)->rel_count2
= 0;
7729 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7731 /* We have now assigned file positions for all the sections except
7732 .symtab and .strtab. We start the .symtab section at the current
7733 file position, and write directly to it. We build the .strtab
7734 section in memory. */
7735 bfd_get_symcount (abfd
) = 0;
7736 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7737 /* sh_name is set in prep_headers. */
7738 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7739 /* sh_flags, sh_addr and sh_size all start off zero. */
7740 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7741 /* sh_link is set in assign_section_numbers. */
7742 /* sh_info is set below. */
7743 /* sh_offset is set just below. */
7744 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7746 off
= elf_tdata (abfd
)->next_file_pos
;
7747 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7749 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7750 incorrect. We do not yet know the size of the .symtab section.
7751 We correct next_file_pos below, after we do know the size. */
7753 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7754 continuously seeking to the right position in the file. */
7755 if (! info
->keep_memory
|| max_sym_count
< 20)
7756 finfo
.symbuf_size
= 20;
7758 finfo
.symbuf_size
= max_sym_count
;
7759 amt
= finfo
.symbuf_size
;
7760 amt
*= bed
->s
->sizeof_sym
;
7761 finfo
.symbuf
= bfd_malloc (amt
);
7762 if (finfo
.symbuf
== NULL
)
7764 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7766 /* Wild guess at number of output symbols. realloc'd as needed. */
7767 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7768 finfo
.shndxbuf_size
= amt
;
7769 amt
*= sizeof (Elf_External_Sym_Shndx
);
7770 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7771 if (finfo
.symshndxbuf
== NULL
)
7775 /* Start writing out the symbol table. The first symbol is always a
7777 if (info
->strip
!= strip_all
7780 elfsym
.st_value
= 0;
7783 elfsym
.st_other
= 0;
7784 elfsym
.st_shndx
= SHN_UNDEF
;
7785 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7791 /* Some standard ELF linkers do this, but we don't because it causes
7792 bootstrap comparison failures. */
7793 /* Output a file symbol for the output file as the second symbol.
7794 We output this even if we are discarding local symbols, although
7795 I'm not sure if this is correct. */
7796 elfsym
.st_value
= 0;
7798 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7799 elfsym
.st_other
= 0;
7800 elfsym
.st_shndx
= SHN_ABS
;
7801 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7802 &elfsym
, bfd_abs_section_ptr
, NULL
))
7806 /* Output a symbol for each section. We output these even if we are
7807 discarding local symbols, since they are used for relocs. These
7808 symbols have no names. We store the index of each one in the
7809 index field of the section, so that we can find it again when
7810 outputting relocs. */
7811 if (info
->strip
!= strip_all
7815 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7816 elfsym
.st_other
= 0;
7817 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7819 o
= bfd_section_from_elf_index (abfd
, i
);
7821 o
->target_index
= bfd_get_symcount (abfd
);
7822 elfsym
.st_shndx
= i
;
7823 if (info
->relocatable
|| o
== NULL
)
7824 elfsym
.st_value
= 0;
7826 elfsym
.st_value
= o
->vma
;
7827 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7829 if (i
== SHN_LORESERVE
- 1)
7830 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7834 /* Allocate some memory to hold information read in from the input
7836 if (max_contents_size
!= 0)
7838 finfo
.contents
= bfd_malloc (max_contents_size
);
7839 if (finfo
.contents
== NULL
)
7843 if (max_external_reloc_size
!= 0)
7845 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7846 if (finfo
.external_relocs
== NULL
)
7850 if (max_internal_reloc_count
!= 0)
7852 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7853 amt
*= sizeof (Elf_Internal_Rela
);
7854 finfo
.internal_relocs
= bfd_malloc (amt
);
7855 if (finfo
.internal_relocs
== NULL
)
7859 if (max_sym_count
!= 0)
7861 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7862 finfo
.external_syms
= bfd_malloc (amt
);
7863 if (finfo
.external_syms
== NULL
)
7866 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7867 finfo
.internal_syms
= bfd_malloc (amt
);
7868 if (finfo
.internal_syms
== NULL
)
7871 amt
= max_sym_count
* sizeof (long);
7872 finfo
.indices
= bfd_malloc (amt
);
7873 if (finfo
.indices
== NULL
)
7876 amt
= max_sym_count
* sizeof (asection
*);
7877 finfo
.sections
= bfd_malloc (amt
);
7878 if (finfo
.sections
== NULL
)
7882 if (max_sym_shndx_count
!= 0)
7884 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7885 finfo
.locsym_shndx
= bfd_malloc (amt
);
7886 if (finfo
.locsym_shndx
== NULL
)
7890 if (elf_hash_table (info
)->tls_sec
)
7892 bfd_vma base
, end
= 0;
7895 for (sec
= elf_hash_table (info
)->tls_sec
;
7896 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7899 bfd_vma size
= sec
->size
;
7901 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7903 struct bfd_link_order
*o
;
7905 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7906 if (size
< o
->offset
+ o
->size
)
7907 size
= o
->offset
+ o
->size
;
7909 end
= sec
->vma
+ size
;
7911 base
= elf_hash_table (info
)->tls_sec
->vma
;
7912 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7913 elf_hash_table (info
)->tls_size
= end
- base
;
7916 /* Reorder SHF_LINK_ORDER sections. */
7917 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7919 if (!elf_fixup_link_order (abfd
, o
))
7923 /* Since ELF permits relocations to be against local symbols, we
7924 must have the local symbols available when we do the relocations.
7925 Since we would rather only read the local symbols once, and we
7926 would rather not keep them in memory, we handle all the
7927 relocations for a single input file at the same time.
7929 Unfortunately, there is no way to know the total number of local
7930 symbols until we have seen all of them, and the local symbol
7931 indices precede the global symbol indices. This means that when
7932 we are generating relocatable output, and we see a reloc against
7933 a global symbol, we can not know the symbol index until we have
7934 finished examining all the local symbols to see which ones we are
7935 going to output. To deal with this, we keep the relocations in
7936 memory, and don't output them until the end of the link. This is
7937 an unfortunate waste of memory, but I don't see a good way around
7938 it. Fortunately, it only happens when performing a relocatable
7939 link, which is not the common case. FIXME: If keep_memory is set
7940 we could write the relocs out and then read them again; I don't
7941 know how bad the memory loss will be. */
7943 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7944 sub
->output_has_begun
= FALSE
;
7945 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7947 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7949 if (p
->type
== bfd_indirect_link_order
7950 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7951 == bfd_target_elf_flavour
)
7952 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7954 if (! sub
->output_has_begun
)
7956 if (! elf_link_input_bfd (&finfo
, sub
))
7958 sub
->output_has_begun
= TRUE
;
7961 else if (p
->type
== bfd_section_reloc_link_order
7962 || p
->type
== bfd_symbol_reloc_link_order
)
7964 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7969 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
7975 /* Output any global symbols that got converted to local in a
7976 version script or due to symbol visibility. We do this in a
7977 separate step since ELF requires all local symbols to appear
7978 prior to any global symbols. FIXME: We should only do this if
7979 some global symbols were, in fact, converted to become local.
7980 FIXME: Will this work correctly with the Irix 5 linker? */
7981 eoinfo
.failed
= FALSE
;
7982 eoinfo
.finfo
= &finfo
;
7983 eoinfo
.localsyms
= TRUE
;
7984 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
7989 /* That wrote out all the local symbols. Finish up the symbol table
7990 with the global symbols. Even if we want to strip everything we
7991 can, we still need to deal with those global symbols that got
7992 converted to local in a version script. */
7994 /* The sh_info field records the index of the first non local symbol. */
7995 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
7998 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8000 Elf_Internal_Sym sym
;
8001 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8002 long last_local
= 0;
8004 /* Write out the section symbols for the output sections. */
8011 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8014 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8020 dynindx
= elf_section_data (s
)->dynindx
;
8023 indx
= elf_section_data (s
)->this_idx
;
8024 BFD_ASSERT (indx
> 0);
8025 sym
.st_shndx
= indx
;
8026 sym
.st_value
= s
->vma
;
8027 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8028 if (last_local
< dynindx
)
8029 last_local
= dynindx
;
8030 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8034 /* Write out the local dynsyms. */
8035 if (elf_hash_table (info
)->dynlocal
)
8037 struct elf_link_local_dynamic_entry
*e
;
8038 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8043 sym
.st_size
= e
->isym
.st_size
;
8044 sym
.st_other
= e
->isym
.st_other
;
8046 /* Copy the internal symbol as is.
8047 Note that we saved a word of storage and overwrote
8048 the original st_name with the dynstr_index. */
8051 if (e
->isym
.st_shndx
!= SHN_UNDEF
8052 && (e
->isym
.st_shndx
< SHN_LORESERVE
8053 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8055 s
= bfd_section_from_elf_index (e
->input_bfd
,
8059 elf_section_data (s
->output_section
)->this_idx
;
8060 sym
.st_value
= (s
->output_section
->vma
8062 + e
->isym
.st_value
);
8065 if (last_local
< e
->dynindx
)
8066 last_local
= e
->dynindx
;
8068 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8069 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8073 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8077 /* We get the global symbols from the hash table. */
8078 eoinfo
.failed
= FALSE
;
8079 eoinfo
.localsyms
= FALSE
;
8080 eoinfo
.finfo
= &finfo
;
8081 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8086 /* If backend needs to output some symbols not present in the hash
8087 table, do it now. */
8088 if (bed
->elf_backend_output_arch_syms
)
8090 typedef bfd_boolean (*out_sym_func
)
8091 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8092 struct elf_link_hash_entry
*);
8094 if (! ((*bed
->elf_backend_output_arch_syms
)
8095 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8099 /* Flush all symbols to the file. */
8100 if (! elf_link_flush_output_syms (&finfo
, bed
))
8103 /* Now we know the size of the symtab section. */
8104 off
+= symtab_hdr
->sh_size
;
8106 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8107 if (symtab_shndx_hdr
->sh_name
!= 0)
8109 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8110 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8111 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8112 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8113 symtab_shndx_hdr
->sh_size
= amt
;
8115 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8118 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8119 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8124 /* Finish up and write out the symbol string table (.strtab)
8126 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8127 /* sh_name was set in prep_headers. */
8128 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8129 symstrtab_hdr
->sh_flags
= 0;
8130 symstrtab_hdr
->sh_addr
= 0;
8131 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8132 symstrtab_hdr
->sh_entsize
= 0;
8133 symstrtab_hdr
->sh_link
= 0;
8134 symstrtab_hdr
->sh_info
= 0;
8135 /* sh_offset is set just below. */
8136 symstrtab_hdr
->sh_addralign
= 1;
8138 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8139 elf_tdata (abfd
)->next_file_pos
= off
;
8141 if (bfd_get_symcount (abfd
) > 0)
8143 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8144 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8148 /* Adjust the relocs to have the correct symbol indices. */
8149 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8151 if ((o
->flags
& SEC_RELOC
) == 0)
8154 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8155 elf_section_data (o
)->rel_count
,
8156 elf_section_data (o
)->rel_hashes
);
8157 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8158 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8159 elf_section_data (o
)->rel_count2
,
8160 (elf_section_data (o
)->rel_hashes
8161 + elf_section_data (o
)->rel_count
));
8163 /* Set the reloc_count field to 0 to prevent write_relocs from
8164 trying to swap the relocs out itself. */
8168 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8169 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8171 /* If we are linking against a dynamic object, or generating a
8172 shared library, finish up the dynamic linking information. */
8175 bfd_byte
*dyncon
, *dynconend
;
8177 /* Fix up .dynamic entries. */
8178 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8179 BFD_ASSERT (o
!= NULL
);
8181 dyncon
= o
->contents
;
8182 dynconend
= o
->contents
+ o
->size
;
8183 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8185 Elf_Internal_Dyn dyn
;
8189 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8196 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8198 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8200 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8201 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8204 dyn
.d_un
.d_val
= relativecount
;
8211 name
= info
->init_function
;
8214 name
= info
->fini_function
;
8217 struct elf_link_hash_entry
*h
;
8219 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8220 FALSE
, FALSE
, TRUE
);
8222 && (h
->root
.type
== bfd_link_hash_defined
8223 || h
->root
.type
== bfd_link_hash_defweak
))
8225 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8226 o
= h
->root
.u
.def
.section
;
8227 if (o
->output_section
!= NULL
)
8228 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8229 + o
->output_offset
);
8232 /* The symbol is imported from another shared
8233 library and does not apply to this one. */
8241 case DT_PREINIT_ARRAYSZ
:
8242 name
= ".preinit_array";
8244 case DT_INIT_ARRAYSZ
:
8245 name
= ".init_array";
8247 case DT_FINI_ARRAYSZ
:
8248 name
= ".fini_array";
8250 o
= bfd_get_section_by_name (abfd
, name
);
8253 (*_bfd_error_handler
)
8254 (_("%B: could not find output section %s"), abfd
, name
);
8258 (*_bfd_error_handler
)
8259 (_("warning: %s section has zero size"), name
);
8260 dyn
.d_un
.d_val
= o
->size
;
8263 case DT_PREINIT_ARRAY
:
8264 name
= ".preinit_array";
8267 name
= ".init_array";
8270 name
= ".fini_array";
8283 name
= ".gnu.version_d";
8286 name
= ".gnu.version_r";
8289 name
= ".gnu.version";
8291 o
= bfd_get_section_by_name (abfd
, name
);
8294 (*_bfd_error_handler
)
8295 (_("%B: could not find output section %s"), abfd
, name
);
8298 dyn
.d_un
.d_ptr
= o
->vma
;
8305 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8310 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8312 Elf_Internal_Shdr
*hdr
;
8314 hdr
= elf_elfsections (abfd
)[i
];
8315 if (hdr
->sh_type
== type
8316 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8318 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8319 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8322 if (dyn
.d_un
.d_val
== 0
8323 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8324 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8330 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8334 /* If we have created any dynamic sections, then output them. */
8337 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8340 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8342 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8344 || o
->output_section
== bfd_abs_section_ptr
)
8346 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8348 /* At this point, we are only interested in sections
8349 created by _bfd_elf_link_create_dynamic_sections. */
8352 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8354 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8356 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8358 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8360 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8362 (file_ptr
) o
->output_offset
,
8368 /* The contents of the .dynstr section are actually in a
8370 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8371 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8372 || ! _bfd_elf_strtab_emit (abfd
,
8373 elf_hash_table (info
)->dynstr
))
8379 if (info
->relocatable
)
8381 bfd_boolean failed
= FALSE
;
8383 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8388 /* If we have optimized stabs strings, output them. */
8389 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8391 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8395 if (info
->eh_frame_hdr
)
8397 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8401 if (finfo
.symstrtab
!= NULL
)
8402 _bfd_stringtab_free (finfo
.symstrtab
);
8403 if (finfo
.contents
!= NULL
)
8404 free (finfo
.contents
);
8405 if (finfo
.external_relocs
!= NULL
)
8406 free (finfo
.external_relocs
);
8407 if (finfo
.internal_relocs
!= NULL
)
8408 free (finfo
.internal_relocs
);
8409 if (finfo
.external_syms
!= NULL
)
8410 free (finfo
.external_syms
);
8411 if (finfo
.locsym_shndx
!= NULL
)
8412 free (finfo
.locsym_shndx
);
8413 if (finfo
.internal_syms
!= NULL
)
8414 free (finfo
.internal_syms
);
8415 if (finfo
.indices
!= NULL
)
8416 free (finfo
.indices
);
8417 if (finfo
.sections
!= NULL
)
8418 free (finfo
.sections
);
8419 if (finfo
.symbuf
!= NULL
)
8420 free (finfo
.symbuf
);
8421 if (finfo
.symshndxbuf
!= NULL
)
8422 free (finfo
.symshndxbuf
);
8423 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8425 if ((o
->flags
& SEC_RELOC
) != 0
8426 && elf_section_data (o
)->rel_hashes
!= NULL
)
8427 free (elf_section_data (o
)->rel_hashes
);
8430 elf_tdata (abfd
)->linker
= TRUE
;
8435 if (finfo
.symstrtab
!= NULL
)
8436 _bfd_stringtab_free (finfo
.symstrtab
);
8437 if (finfo
.contents
!= NULL
)
8438 free (finfo
.contents
);
8439 if (finfo
.external_relocs
!= NULL
)
8440 free (finfo
.external_relocs
);
8441 if (finfo
.internal_relocs
!= NULL
)
8442 free (finfo
.internal_relocs
);
8443 if (finfo
.external_syms
!= NULL
)
8444 free (finfo
.external_syms
);
8445 if (finfo
.locsym_shndx
!= NULL
)
8446 free (finfo
.locsym_shndx
);
8447 if (finfo
.internal_syms
!= NULL
)
8448 free (finfo
.internal_syms
);
8449 if (finfo
.indices
!= NULL
)
8450 free (finfo
.indices
);
8451 if (finfo
.sections
!= NULL
)
8452 free (finfo
.sections
);
8453 if (finfo
.symbuf
!= NULL
)
8454 free (finfo
.symbuf
);
8455 if (finfo
.symshndxbuf
!= NULL
)
8456 free (finfo
.symshndxbuf
);
8457 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8459 if ((o
->flags
& SEC_RELOC
) != 0
8460 && elf_section_data (o
)->rel_hashes
!= NULL
)
8461 free (elf_section_data (o
)->rel_hashes
);
8467 /* Garbage collect unused sections. */
8469 /* The mark phase of garbage collection. For a given section, mark
8470 it and any sections in this section's group, and all the sections
8471 which define symbols to which it refers. */
8473 typedef asection
* (*gc_mark_hook_fn
)
8474 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8475 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8478 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8480 gc_mark_hook_fn gc_mark_hook
)
8483 asection
*group_sec
;
8487 /* Mark all the sections in the group. */
8488 group_sec
= elf_section_data (sec
)->next_in_group
;
8489 if (group_sec
&& !group_sec
->gc_mark
)
8490 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8493 /* Look through the section relocs. */
8495 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8497 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8498 Elf_Internal_Shdr
*symtab_hdr
;
8499 struct elf_link_hash_entry
**sym_hashes
;
8502 bfd
*input_bfd
= sec
->owner
;
8503 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8504 Elf_Internal_Sym
*isym
= NULL
;
8507 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8508 sym_hashes
= elf_sym_hashes (input_bfd
);
8510 /* Read the local symbols. */
8511 if (elf_bad_symtab (input_bfd
))
8513 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8517 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8519 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8520 if (isym
== NULL
&& nlocsyms
!= 0)
8522 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8528 /* Read the relocations. */
8529 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8531 if (relstart
== NULL
)
8536 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8538 if (bed
->s
->arch_size
== 32)
8543 for (rel
= relstart
; rel
< relend
; rel
++)
8545 unsigned long r_symndx
;
8547 struct elf_link_hash_entry
*h
;
8549 r_symndx
= rel
->r_info
>> r_sym_shift
;
8553 if (r_symndx
>= nlocsyms
8554 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8556 h
= sym_hashes
[r_symndx
- extsymoff
];
8557 while (h
->root
.type
== bfd_link_hash_indirect
8558 || h
->root
.type
== bfd_link_hash_warning
)
8559 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8560 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8564 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8567 if (rsec
&& !rsec
->gc_mark
)
8569 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8571 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8580 if (elf_section_data (sec
)->relocs
!= relstart
)
8583 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8585 if (! info
->keep_memory
)
8588 symtab_hdr
->contents
= (unsigned char *) isym
;
8595 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8598 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8602 if (h
->root
.type
== bfd_link_hash_warning
)
8603 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8605 if (h
->dynindx
!= -1
8606 && ((h
->root
.type
!= bfd_link_hash_defined
8607 && h
->root
.type
!= bfd_link_hash_defweak
)
8608 || h
->root
.u
.def
.section
->gc_mark
))
8609 h
->dynindx
= (*idx
)++;
8614 /* The sweep phase of garbage collection. Remove all garbage sections. */
8616 typedef bfd_boolean (*gc_sweep_hook_fn
)
8617 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8620 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8624 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8628 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8631 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8633 /* Keep debug and special sections. */
8634 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8635 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8641 /* Skip sweeping sections already excluded. */
8642 if (o
->flags
& SEC_EXCLUDE
)
8645 /* Since this is early in the link process, it is simple
8646 to remove a section from the output. */
8647 o
->flags
|= SEC_EXCLUDE
;
8649 /* But we also have to update some of the relocation
8650 info we collected before. */
8652 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8654 Elf_Internal_Rela
*internal_relocs
;
8658 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8660 if (internal_relocs
== NULL
)
8663 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8665 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8666 free (internal_relocs
);
8674 /* Remove the symbols that were in the swept sections from the dynamic
8675 symbol table. GCFIXME: Anyone know how to get them out of the
8676 static symbol table as well? */
8680 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8682 elf_hash_table (info
)->dynsymcount
= i
;
8688 /* Propagate collected vtable information. This is called through
8689 elf_link_hash_traverse. */
8692 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8694 if (h
->root
.type
== bfd_link_hash_warning
)
8695 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8697 /* Those that are not vtables. */
8698 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8701 /* Those vtables that do not have parents, we cannot merge. */
8702 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8705 /* If we've already been done, exit. */
8706 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8709 /* Make sure the parent's table is up to date. */
8710 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8712 if (h
->vtable
->used
== NULL
)
8714 /* None of this table's entries were referenced. Re-use the
8716 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8717 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8722 bfd_boolean
*cu
, *pu
;
8724 /* Or the parent's entries into ours. */
8725 cu
= h
->vtable
->used
;
8727 pu
= h
->vtable
->parent
->vtable
->used
;
8730 const struct elf_backend_data
*bed
;
8731 unsigned int log_file_align
;
8733 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8734 log_file_align
= bed
->s
->log_file_align
;
8735 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8750 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8753 bfd_vma hstart
, hend
;
8754 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8755 const struct elf_backend_data
*bed
;
8756 unsigned int log_file_align
;
8758 if (h
->root
.type
== bfd_link_hash_warning
)
8759 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8761 /* Take care of both those symbols that do not describe vtables as
8762 well as those that are not loaded. */
8763 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8766 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8767 || h
->root
.type
== bfd_link_hash_defweak
);
8769 sec
= h
->root
.u
.def
.section
;
8770 hstart
= h
->root
.u
.def
.value
;
8771 hend
= hstart
+ h
->size
;
8773 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8775 return *(bfd_boolean
*) okp
= FALSE
;
8776 bed
= get_elf_backend_data (sec
->owner
);
8777 log_file_align
= bed
->s
->log_file_align
;
8779 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8781 for (rel
= relstart
; rel
< relend
; ++rel
)
8782 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8784 /* If the entry is in use, do nothing. */
8786 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8788 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8789 if (h
->vtable
->used
[entry
])
8792 /* Otherwise, kill it. */
8793 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8799 /* Mark sections containing dynamically referenced symbols. This is called
8800 through elf_link_hash_traverse. */
8803 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8804 void *okp ATTRIBUTE_UNUSED
)
8806 if (h
->root
.type
== bfd_link_hash_warning
)
8807 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8809 if ((h
->root
.type
== bfd_link_hash_defined
8810 || h
->root
.type
== bfd_link_hash_defweak
)
8812 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8817 /* Do mark and sweep of unused sections. */
8820 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8822 bfd_boolean ok
= TRUE
;
8824 asection
* (*gc_mark_hook
)
8825 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8826 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8828 if (!get_elf_backend_data (abfd
)->can_gc_sections
8829 || info
->relocatable
8830 || info
->emitrelocations
8832 || !is_elf_hash_table (info
->hash
))
8834 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8838 /* Apply transitive closure to the vtable entry usage info. */
8839 elf_link_hash_traverse (elf_hash_table (info
),
8840 elf_gc_propagate_vtable_entries_used
,
8845 /* Kill the vtable relocations that were not used. */
8846 elf_link_hash_traverse (elf_hash_table (info
),
8847 elf_gc_smash_unused_vtentry_relocs
,
8852 /* Mark dynamically referenced symbols. */
8853 if (elf_hash_table (info
)->dynamic_sections_created
)
8854 elf_link_hash_traverse (elf_hash_table (info
),
8855 elf_gc_mark_dynamic_ref_symbol
,
8860 /* Grovel through relocs to find out who stays ... */
8861 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8862 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8866 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8869 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8871 if (o
->flags
& SEC_KEEP
)
8873 /* _bfd_elf_discard_section_eh_frame knows how to discard
8874 orphaned FDEs so don't mark sections referenced by the
8875 EH frame section. */
8876 if (strcmp (o
->name
, ".eh_frame") == 0)
8878 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8884 /* ... and mark SEC_EXCLUDE for those that go. */
8885 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8891 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8894 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8896 struct elf_link_hash_entry
*h
,
8899 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8900 struct elf_link_hash_entry
**search
, *child
;
8901 bfd_size_type extsymcount
;
8902 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8904 /* The sh_info field of the symtab header tells us where the
8905 external symbols start. We don't care about the local symbols at
8907 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8908 if (!elf_bad_symtab (abfd
))
8909 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8911 sym_hashes
= elf_sym_hashes (abfd
);
8912 sym_hashes_end
= sym_hashes
+ extsymcount
;
8914 /* Hunt down the child symbol, which is in this section at the same
8915 offset as the relocation. */
8916 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8918 if ((child
= *search
) != NULL
8919 && (child
->root
.type
== bfd_link_hash_defined
8920 || child
->root
.type
== bfd_link_hash_defweak
)
8921 && child
->root
.u
.def
.section
== sec
8922 && child
->root
.u
.def
.value
== offset
)
8926 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8927 abfd
, sec
, (unsigned long) offset
);
8928 bfd_set_error (bfd_error_invalid_operation
);
8934 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8940 /* This *should* only be the absolute section. It could potentially
8941 be that someone has defined a non-global vtable though, which
8942 would be bad. It isn't worth paging in the local symbols to be
8943 sure though; that case should simply be handled by the assembler. */
8945 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8948 child
->vtable
->parent
= h
;
8953 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8956 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8957 asection
*sec ATTRIBUTE_UNUSED
,
8958 struct elf_link_hash_entry
*h
,
8961 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8962 unsigned int log_file_align
= bed
->s
->log_file_align
;
8966 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8971 if (addend
>= h
->vtable
->size
)
8973 size_t size
, bytes
, file_align
;
8974 bfd_boolean
*ptr
= h
->vtable
->used
;
8976 /* While the symbol is undefined, we have to be prepared to handle
8978 file_align
= 1 << log_file_align
;
8979 if (h
->root
.type
== bfd_link_hash_undefined
)
8980 size
= addend
+ file_align
;
8986 /* Oops! We've got a reference past the defined end of
8987 the table. This is probably a bug -- shall we warn? */
8988 size
= addend
+ file_align
;
8991 size
= (size
+ file_align
- 1) & -file_align
;
8993 /* Allocate one extra entry for use as a "done" flag for the
8994 consolidation pass. */
8995 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
8999 ptr
= bfd_realloc (ptr
- 1, bytes
);
9005 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9006 * sizeof (bfd_boolean
));
9007 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9011 ptr
= bfd_zmalloc (bytes
);
9016 /* And arrange for that done flag to be at index -1. */
9017 h
->vtable
->used
= ptr
+ 1;
9018 h
->vtable
->size
= size
;
9021 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9026 struct alloc_got_off_arg
{
9028 unsigned int got_elt_size
;
9031 /* We need a special top-level link routine to convert got reference counts
9032 to real got offsets. */
9035 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9037 struct alloc_got_off_arg
*gofarg
= arg
;
9039 if (h
->root
.type
== bfd_link_hash_warning
)
9040 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9042 if (h
->got
.refcount
> 0)
9044 h
->got
.offset
= gofarg
->gotoff
;
9045 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9048 h
->got
.offset
= (bfd_vma
) -1;
9053 /* And an accompanying bit to work out final got entry offsets once
9054 we're done. Should be called from final_link. */
9057 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9058 struct bfd_link_info
*info
)
9061 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9063 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9064 struct alloc_got_off_arg gofarg
;
9066 if (! is_elf_hash_table (info
->hash
))
9069 /* The GOT offset is relative to the .got section, but the GOT header is
9070 put into the .got.plt section, if the backend uses it. */
9071 if (bed
->want_got_plt
)
9074 gotoff
= bed
->got_header_size
;
9076 /* Do the local .got entries first. */
9077 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9079 bfd_signed_vma
*local_got
;
9080 bfd_size_type j
, locsymcount
;
9081 Elf_Internal_Shdr
*symtab_hdr
;
9083 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9086 local_got
= elf_local_got_refcounts (i
);
9090 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9091 if (elf_bad_symtab (i
))
9092 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9094 locsymcount
= symtab_hdr
->sh_info
;
9096 for (j
= 0; j
< locsymcount
; ++j
)
9098 if (local_got
[j
] > 0)
9100 local_got
[j
] = gotoff
;
9101 gotoff
+= got_elt_size
;
9104 local_got
[j
] = (bfd_vma
) -1;
9108 /* Then the global .got entries. .plt refcounts are handled by
9109 adjust_dynamic_symbol */
9110 gofarg
.gotoff
= gotoff
;
9111 gofarg
.got_elt_size
= got_elt_size
;
9112 elf_link_hash_traverse (elf_hash_table (info
),
9113 elf_gc_allocate_got_offsets
,
9118 /* Many folk need no more in the way of final link than this, once
9119 got entry reference counting is enabled. */
9122 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9124 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9127 /* Invoke the regular ELF backend linker to do all the work. */
9128 return bfd_elf_final_link (abfd
, info
);
9132 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9134 struct elf_reloc_cookie
*rcookie
= cookie
;
9136 if (rcookie
->bad_symtab
)
9137 rcookie
->rel
= rcookie
->rels
;
9139 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9141 unsigned long r_symndx
;
9143 if (! rcookie
->bad_symtab
)
9144 if (rcookie
->rel
->r_offset
> offset
)
9146 if (rcookie
->rel
->r_offset
!= offset
)
9149 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9150 if (r_symndx
== SHN_UNDEF
)
9153 if (r_symndx
>= rcookie
->locsymcount
9154 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9156 struct elf_link_hash_entry
*h
;
9158 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9160 while (h
->root
.type
== bfd_link_hash_indirect
9161 || h
->root
.type
== bfd_link_hash_warning
)
9162 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9164 if ((h
->root
.type
== bfd_link_hash_defined
9165 || h
->root
.type
== bfd_link_hash_defweak
)
9166 && elf_discarded_section (h
->root
.u
.def
.section
))
9173 /* It's not a relocation against a global symbol,
9174 but it could be a relocation against a local
9175 symbol for a discarded section. */
9177 Elf_Internal_Sym
*isym
;
9179 /* Need to: get the symbol; get the section. */
9180 isym
= &rcookie
->locsyms
[r_symndx
];
9181 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9183 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9184 if (isec
!= NULL
&& elf_discarded_section (isec
))
9193 /* Discard unneeded references to discarded sections.
9194 Returns TRUE if any section's size was changed. */
9195 /* This function assumes that the relocations are in sorted order,
9196 which is true for all known assemblers. */
9199 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9201 struct elf_reloc_cookie cookie
;
9202 asection
*stab
, *eh
;
9203 Elf_Internal_Shdr
*symtab_hdr
;
9204 const struct elf_backend_data
*bed
;
9207 bfd_boolean ret
= FALSE
;
9209 if (info
->traditional_format
9210 || !is_elf_hash_table (info
->hash
))
9213 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9215 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9218 bed
= get_elf_backend_data (abfd
);
9220 if ((abfd
->flags
& DYNAMIC
) != 0)
9223 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9224 if (info
->relocatable
9227 || bfd_is_abs_section (eh
->output_section
))))
9230 stab
= bfd_get_section_by_name (abfd
, ".stab");
9233 || bfd_is_abs_section (stab
->output_section
)
9234 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9239 && bed
->elf_backend_discard_info
== NULL
)
9242 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9244 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9245 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9246 if (cookie
.bad_symtab
)
9248 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9249 cookie
.extsymoff
= 0;
9253 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9254 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9257 if (bed
->s
->arch_size
== 32)
9258 cookie
.r_sym_shift
= 8;
9260 cookie
.r_sym_shift
= 32;
9262 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9263 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9265 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9266 cookie
.locsymcount
, 0,
9268 if (cookie
.locsyms
== NULL
)
9275 count
= stab
->reloc_count
;
9277 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9279 if (cookie
.rels
!= NULL
)
9281 cookie
.rel
= cookie
.rels
;
9282 cookie
.relend
= cookie
.rels
;
9283 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9284 if (_bfd_discard_section_stabs (abfd
, stab
,
9285 elf_section_data (stab
)->sec_info
,
9286 bfd_elf_reloc_symbol_deleted_p
,
9289 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9297 count
= eh
->reloc_count
;
9299 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9301 cookie
.rel
= cookie
.rels
;
9302 cookie
.relend
= cookie
.rels
;
9303 if (cookie
.rels
!= NULL
)
9304 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9306 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9307 bfd_elf_reloc_symbol_deleted_p
,
9311 if (cookie
.rels
!= NULL
9312 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9316 if (bed
->elf_backend_discard_info
!= NULL
9317 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9320 if (cookie
.locsyms
!= NULL
9321 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9323 if (! info
->keep_memory
)
9324 free (cookie
.locsyms
);
9326 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9330 if (info
->eh_frame_hdr
9331 && !info
->relocatable
9332 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9339 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9342 const char *name
, *p
;
9343 struct bfd_section_already_linked
*l
;
9344 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9347 /* A single member comdat group section may be discarded by a
9348 linkonce section. See below. */
9349 if (sec
->output_section
== bfd_abs_section_ptr
)
9354 /* Check if it belongs to a section group. */
9355 group
= elf_sec_group (sec
);
9357 /* Return if it isn't a linkonce section nor a member of a group. A
9358 comdat group section also has SEC_LINK_ONCE set. */
9359 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9364 /* If this is the member of a single member comdat group, check if
9365 the group should be discarded. */
9366 if (elf_next_in_group (sec
) == sec
9367 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9373 /* FIXME: When doing a relocatable link, we may have trouble
9374 copying relocations in other sections that refer to local symbols
9375 in the section being discarded. Those relocations will have to
9376 be converted somehow; as of this writing I'm not sure that any of
9377 the backends handle that correctly.
9379 It is tempting to instead not discard link once sections when
9380 doing a relocatable link (technically, they should be discarded
9381 whenever we are building constructors). However, that fails,
9382 because the linker winds up combining all the link once sections
9383 into a single large link once section, which defeats the purpose
9384 of having link once sections in the first place.
9386 Also, not merging link once sections in a relocatable link
9387 causes trouble for MIPS ELF, which relies on link once semantics
9388 to handle the .reginfo section correctly. */
9390 name
= bfd_get_section_name (abfd
, sec
);
9392 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9393 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9398 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9400 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9402 /* We may have 3 different sections on the list: group section,
9403 comdat section and linkonce section. SEC may be a linkonce or
9404 group section. We match a group section with a group section,
9405 a linkonce section with a linkonce section, and ignore comdat
9407 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9408 && strcmp (name
, l
->sec
->name
) == 0
9409 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9411 /* The section has already been linked. See if we should
9413 switch (flags
& SEC_LINK_DUPLICATES
)
9418 case SEC_LINK_DUPLICATES_DISCARD
:
9421 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9422 (*_bfd_error_handler
)
9423 (_("%B: ignoring duplicate section `%A'\n"),
9427 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9428 if (sec
->size
!= l
->sec
->size
)
9429 (*_bfd_error_handler
)
9430 (_("%B: duplicate section `%A' has different size\n"),
9434 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9435 if (sec
->size
!= l
->sec
->size
)
9436 (*_bfd_error_handler
)
9437 (_("%B: duplicate section `%A' has different size\n"),
9439 else if (sec
->size
!= 0)
9441 bfd_byte
*sec_contents
, *l_sec_contents
;
9443 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9444 (*_bfd_error_handler
)
9445 (_("%B: warning: could not read contents of section `%A'\n"),
9447 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9449 (*_bfd_error_handler
)
9450 (_("%B: warning: could not read contents of section `%A'\n"),
9451 l
->sec
->owner
, l
->sec
);
9452 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9453 (*_bfd_error_handler
)
9454 (_("%B: warning: duplicate section `%A' has different contents\n"),
9458 free (sec_contents
);
9460 free (l_sec_contents
);
9465 /* Set the output_section field so that lang_add_section
9466 does not create a lang_input_section structure for this
9467 section. Since there might be a symbol in the section
9468 being discarded, we must retain a pointer to the section
9469 which we are really going to use. */
9470 sec
->output_section
= bfd_abs_section_ptr
;
9471 sec
->kept_section
= l
->sec
;
9473 if (flags
& SEC_GROUP
)
9475 asection
*first
= elf_next_in_group (sec
);
9476 asection
*s
= first
;
9480 s
->output_section
= bfd_abs_section_ptr
;
9481 /* Record which group discards it. */
9482 s
->kept_section
= l
->sec
;
9483 s
= elf_next_in_group (s
);
9484 /* These lists are circular. */
9496 /* If this is the member of a single member comdat group and the
9497 group hasn't be discarded, we check if it matches a linkonce
9498 section. We only record the discarded comdat group. Otherwise
9499 the undiscarded group will be discarded incorrectly later since
9500 itself has been recorded. */
9501 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9502 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9503 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9504 && bfd_elf_match_symbols_in_sections (l
->sec
,
9505 elf_next_in_group (sec
)))
9507 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9508 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9509 group
->output_section
= bfd_abs_section_ptr
;
9516 /* There is no direct match. But for linkonce section, we should
9517 check if there is a match with comdat group member. We always
9518 record the linkonce section, discarded or not. */
9519 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9520 if (l
->sec
->flags
& SEC_GROUP
)
9522 asection
*first
= elf_next_in_group (l
->sec
);
9525 && elf_next_in_group (first
) == first
9526 && bfd_elf_match_symbols_in_sections (first
, sec
))
9528 sec
->output_section
= bfd_abs_section_ptr
;
9529 sec
->kept_section
= l
->sec
;
9534 /* This is the first section with this name. Record it. */
9535 bfd_section_already_linked_table_insert (already_linked_list
, sec
);