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
, old_asneeded
;
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 /* If the old definition came from an as-needed dynamic library which
853 wasn't found to be needed, treat the sym as undefined. */
854 old_asneeded
= FALSE
;
857 && (elf_dyn_lib_class (oldbfd
) & DYN_AS_NEEDED
) != 0)
860 /* Check TLS symbol. */
861 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
862 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
865 bfd_boolean ntdef
, tdef
;
866 asection
*ntsec
, *tsec
;
868 if (h
->type
== STT_TLS
)
888 (*_bfd_error_handler
)
889 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
890 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
891 else if (!tdef
&& !ntdef
)
892 (*_bfd_error_handler
)
893 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
894 tbfd
, ntbfd
, h
->root
.root
.string
);
896 (*_bfd_error_handler
)
897 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
898 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
900 (*_bfd_error_handler
)
901 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
902 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
904 bfd_set_error (bfd_error_bad_value
);
908 /* We need to remember if a symbol has a definition in a dynamic
909 object or is weak in all dynamic objects. Internal and hidden
910 visibility will make it unavailable to dynamic objects. */
911 if (newdyn
&& !h
->dynamic_def
)
913 if (!bfd_is_und_section (sec
))
917 /* Check if this symbol is weak in all dynamic objects. If it
918 is the first time we see it in a dynamic object, we mark
919 if it is weak. Otherwise, we clear it. */
922 if (bind
== STB_WEAK
)
925 else if (bind
!= STB_WEAK
)
930 /* If the old symbol has non-default visibility, we ignore the new
931 definition from a dynamic object. */
933 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
934 && !bfd_is_und_section (sec
))
937 /* Make sure this symbol is dynamic. */
939 /* A protected symbol has external availability. Make sure it is
942 FIXME: Should we check type and size for protected symbol? */
943 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
944 return bfd_elf_link_record_dynamic_symbol (info
, h
);
949 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
952 /* If the new symbol with non-default visibility comes from a
953 relocatable file and the old definition comes from a dynamic
954 object, we remove the old definition. */
955 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
958 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
959 && bfd_is_und_section (sec
))
961 /* If the new symbol is undefined and the old symbol was
962 also undefined before, we need to make sure
963 _bfd_generic_link_add_one_symbol doesn't mess
964 up the linker hash table undefs list. Since the old
965 definition came from a dynamic object, it is still on the
967 h
->root
.type
= bfd_link_hash_undefined
;
968 h
->root
.u
.undef
.abfd
= abfd
;
972 h
->root
.type
= bfd_link_hash_new
;
973 h
->root
.u
.undef
.abfd
= NULL
;
982 /* FIXME: Should we check type and size for protected symbol? */
988 /* Differentiate strong and weak symbols. */
989 newweak
= bind
== STB_WEAK
;
990 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
991 || h
->root
.type
== bfd_link_hash_undefweak
);
993 /* If a new weak symbol definition comes from a regular file and the
994 old symbol comes from a dynamic library, we treat the new one as
995 strong. Similarly, an old weak symbol definition from a regular
996 file is treated as strong when the new symbol comes from a dynamic
997 library. Further, an old weak symbol from a dynamic library is
998 treated as strong if the new symbol is from a dynamic library.
999 This reflects the way glibc's ld.so works.
1001 Do this before setting *type_change_ok or *size_change_ok so that
1002 we warn properly when dynamic library symbols are overridden. */
1004 if (newdef
&& !newdyn
&& olddyn
)
1006 if (olddef
&& newdyn
)
1009 /* It's OK to change the type if either the existing symbol or the
1010 new symbol is weak. A type change is also OK if the old symbol
1011 is undefined and the new symbol is defined. */
1016 && h
->root
.type
== bfd_link_hash_undefined
))
1017 *type_change_ok
= TRUE
;
1019 /* It's OK to change the size if either the existing symbol or the
1020 new symbol is weak, or if the old symbol is undefined. */
1023 || h
->root
.type
== bfd_link_hash_undefined
)
1024 *size_change_ok
= TRUE
;
1026 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1027 symbol, respectively, appears to be a common symbol in a dynamic
1028 object. If a symbol appears in an uninitialized section, and is
1029 not weak, and is not a function, then it may be a common symbol
1030 which was resolved when the dynamic object was created. We want
1031 to treat such symbols specially, because they raise special
1032 considerations when setting the symbol size: if the symbol
1033 appears as a common symbol in a regular object, and the size in
1034 the regular object is larger, we must make sure that we use the
1035 larger size. This problematic case can always be avoided in C,
1036 but it must be handled correctly when using Fortran shared
1039 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1040 likewise for OLDDYNCOMMON and OLDDEF.
1042 Note that this test is just a heuristic, and that it is quite
1043 possible to have an uninitialized symbol in a shared object which
1044 is really a definition, rather than a common symbol. This could
1045 lead to some minor confusion when the symbol really is a common
1046 symbol in some regular object. However, I think it will be
1052 && (sec
->flags
& SEC_ALLOC
) != 0
1053 && (sec
->flags
& SEC_LOAD
) == 0
1055 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1056 newdyncommon
= TRUE
;
1058 newdyncommon
= FALSE
;
1063 && h
->root
.type
== bfd_link_hash_defined
1065 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1066 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1068 && h
->type
!= STT_FUNC
)
1069 olddyncommon
= TRUE
;
1071 olddyncommon
= FALSE
;
1073 /* If both the old and the new symbols look like common symbols in a
1074 dynamic object, set the size of the symbol to the larger of the
1079 && sym
->st_size
!= h
->size
)
1081 /* Since we think we have two common symbols, issue a multiple
1082 common warning if desired. Note that we only warn if the
1083 size is different. If the size is the same, we simply let
1084 the old symbol override the new one as normally happens with
1085 symbols defined in dynamic objects. */
1087 if (! ((*info
->callbacks
->multiple_common
)
1088 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1089 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1092 if (sym
->st_size
> h
->size
)
1093 h
->size
= sym
->st_size
;
1095 *size_change_ok
= TRUE
;
1098 /* If we are looking at a dynamic object, and we have found a
1099 definition, we need to see if the symbol was already defined by
1100 some other object. If so, we want to use the existing
1101 definition, and we do not want to report a multiple symbol
1102 definition error; we do this by clobbering *PSEC to be
1103 bfd_und_section_ptr.
1105 We treat a common symbol as a definition if the symbol in the
1106 shared library is a function, since common symbols always
1107 represent variables; this can cause confusion in principle, but
1108 any such confusion would seem to indicate an erroneous program or
1109 shared library. We also permit a common symbol in a regular
1110 object to override a weak symbol in a shared object. */
1114 && ((olddef
&& !old_asneeded
)
1115 || (h
->root
.type
== bfd_link_hash_common
1117 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1121 newdyncommon
= FALSE
;
1123 *psec
= sec
= bfd_und_section_ptr
;
1124 *size_change_ok
= TRUE
;
1126 /* If we get here when the old symbol is a common symbol, then
1127 we are explicitly letting it override a weak symbol or
1128 function in a dynamic object, and we don't want to warn about
1129 a type change. If the old symbol is a defined symbol, a type
1130 change warning may still be appropriate. */
1132 if (h
->root
.type
== bfd_link_hash_common
)
1133 *type_change_ok
= TRUE
;
1136 /* Handle the special case of an old common symbol merging with a
1137 new symbol which looks like a common symbol in a shared object.
1138 We change *PSEC and *PVALUE to make the new symbol look like a
1139 common symbol, and let _bfd_generic_link_add_one_symbol will do
1143 && h
->root
.type
== bfd_link_hash_common
)
1147 newdyncommon
= FALSE
;
1148 *pvalue
= sym
->st_size
;
1149 *psec
= sec
= bfd_com_section_ptr
;
1150 *size_change_ok
= TRUE
;
1153 /* If the old symbol is from a dynamic object, and the new symbol is
1154 a definition which is not from a dynamic object, then the new
1155 symbol overrides the old symbol. Symbols from regular files
1156 always take precedence over symbols from dynamic objects, even if
1157 they are defined after the dynamic object in the link.
1159 As above, we again permit a common symbol in a regular object to
1160 override a definition in a shared object if the shared object
1161 symbol is a function or is weak. */
1164 if ((!newdyn
|| old_asneeded
)
1166 || (bfd_is_com_section (sec
)
1168 || h
->type
== STT_FUNC
)))
1173 /* Change the hash table entry to undefined, and let
1174 _bfd_generic_link_add_one_symbol do the right thing with the
1177 h
->root
.type
= bfd_link_hash_undefined
;
1178 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1179 *size_change_ok
= TRUE
;
1182 olddyncommon
= FALSE
;
1184 /* We again permit a type change when a common symbol may be
1185 overriding a function. */
1187 if (bfd_is_com_section (sec
))
1188 *type_change_ok
= TRUE
;
1190 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1193 /* This union may have been set to be non-NULL when this symbol
1194 was seen in a dynamic object. We must force the union to be
1195 NULL, so that it is correct for a regular symbol. */
1196 h
->verinfo
.vertree
= NULL
;
1199 /* Handle the special case of a new common symbol merging with an
1200 old symbol that looks like it might be a common symbol defined in
1201 a shared object. Note that we have already handled the case in
1202 which a new common symbol should simply override the definition
1203 in the shared library. */
1206 && bfd_is_com_section (sec
)
1209 /* It would be best if we could set the hash table entry to a
1210 common symbol, but we don't know what to use for the section
1211 or the alignment. */
1212 if (! ((*info
->callbacks
->multiple_common
)
1213 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1214 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1217 /* If the presumed common symbol in the dynamic object is
1218 larger, pretend that the new symbol has its size. */
1220 if (h
->size
> *pvalue
)
1223 /* FIXME: We no longer know the alignment required by the symbol
1224 in the dynamic object, so we just wind up using the one from
1225 the regular object. */
1228 olddyncommon
= FALSE
;
1230 h
->root
.type
= bfd_link_hash_undefined
;
1231 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1233 *size_change_ok
= TRUE
;
1234 *type_change_ok
= TRUE
;
1236 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1239 h
->verinfo
.vertree
= NULL
;
1244 /* Handle the case where we had a versioned symbol in a dynamic
1245 library and now find a definition in a normal object. In this
1246 case, we make the versioned symbol point to the normal one. */
1247 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1248 flip
->root
.type
= h
->root
.type
;
1249 h
->root
.type
= bfd_link_hash_indirect
;
1250 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1251 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1252 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1256 flip
->ref_dynamic
= 1;
1263 /* This function is called to create an indirect symbol from the
1264 default for the symbol with the default version if needed. The
1265 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1266 set DYNSYM if the new indirect symbol is dynamic. */
1269 _bfd_elf_add_default_symbol (bfd
*abfd
,
1270 struct bfd_link_info
*info
,
1271 struct elf_link_hash_entry
*h
,
1273 Elf_Internal_Sym
*sym
,
1276 bfd_boolean
*dynsym
,
1277 bfd_boolean override
)
1279 bfd_boolean type_change_ok
;
1280 bfd_boolean size_change_ok
;
1283 struct elf_link_hash_entry
*hi
;
1284 struct bfd_link_hash_entry
*bh
;
1285 const struct elf_backend_data
*bed
;
1286 bfd_boolean collect
;
1287 bfd_boolean dynamic
;
1289 size_t len
, shortlen
;
1292 /* If this symbol has a version, and it is the default version, we
1293 create an indirect symbol from the default name to the fully
1294 decorated name. This will cause external references which do not
1295 specify a version to be bound to this version of the symbol. */
1296 p
= strchr (name
, ELF_VER_CHR
);
1297 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1302 /* We are overridden by an old definition. We need to check if we
1303 need to create the indirect symbol from the default name. */
1304 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1306 BFD_ASSERT (hi
!= NULL
);
1309 while (hi
->root
.type
== bfd_link_hash_indirect
1310 || hi
->root
.type
== bfd_link_hash_warning
)
1312 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1318 bed
= get_elf_backend_data (abfd
);
1319 collect
= bed
->collect
;
1320 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1322 shortlen
= p
- name
;
1323 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1324 if (shortname
== NULL
)
1326 memcpy (shortname
, name
, shortlen
);
1327 shortname
[shortlen
] = '\0';
1329 /* We are going to create a new symbol. Merge it with any existing
1330 symbol with this name. For the purposes of the merge, act as
1331 though we were defining the symbol we just defined, although we
1332 actually going to define an indirect symbol. */
1333 type_change_ok
= FALSE
;
1334 size_change_ok
= FALSE
;
1336 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1337 &hi
, &skip
, &override
, &type_change_ok
,
1347 if (! (_bfd_generic_link_add_one_symbol
1348 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1349 0, name
, FALSE
, collect
, &bh
)))
1351 hi
= (struct elf_link_hash_entry
*) bh
;
1355 /* In this case the symbol named SHORTNAME is overriding the
1356 indirect symbol we want to add. We were planning on making
1357 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1358 is the name without a version. NAME is the fully versioned
1359 name, and it is the default version.
1361 Overriding means that we already saw a definition for the
1362 symbol SHORTNAME in a regular object, and it is overriding
1363 the symbol defined in the dynamic object.
1365 When this happens, we actually want to change NAME, the
1366 symbol we just added, to refer to SHORTNAME. This will cause
1367 references to NAME in the shared object to become references
1368 to SHORTNAME in the regular object. This is what we expect
1369 when we override a function in a shared object: that the
1370 references in the shared object will be mapped to the
1371 definition in the regular object. */
1373 while (hi
->root
.type
== bfd_link_hash_indirect
1374 || hi
->root
.type
== bfd_link_hash_warning
)
1375 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1377 h
->root
.type
= bfd_link_hash_indirect
;
1378 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1382 hi
->ref_dynamic
= 1;
1386 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1391 /* Now set HI to H, so that the following code will set the
1392 other fields correctly. */
1396 /* If there is a duplicate definition somewhere, then HI may not
1397 point to an indirect symbol. We will have reported an error to
1398 the user in that case. */
1400 if (hi
->root
.type
== bfd_link_hash_indirect
)
1402 struct elf_link_hash_entry
*ht
;
1404 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1405 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1407 /* See if the new flags lead us to realize that the symbol must
1419 if (hi
->ref_regular
)
1425 /* We also need to define an indirection from the nondefault version
1429 len
= strlen (name
);
1430 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1431 if (shortname
== NULL
)
1433 memcpy (shortname
, name
, shortlen
);
1434 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1436 /* Once again, merge with any existing symbol. */
1437 type_change_ok
= FALSE
;
1438 size_change_ok
= FALSE
;
1440 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1441 &hi
, &skip
, &override
, &type_change_ok
,
1450 /* Here SHORTNAME is a versioned name, so we don't expect to see
1451 the type of override we do in the case above unless it is
1452 overridden by a versioned definition. */
1453 if (hi
->root
.type
!= bfd_link_hash_defined
1454 && hi
->root
.type
!= bfd_link_hash_defweak
)
1455 (*_bfd_error_handler
)
1456 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1462 if (! (_bfd_generic_link_add_one_symbol
1463 (info
, abfd
, shortname
, BSF_INDIRECT
,
1464 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1466 hi
= (struct elf_link_hash_entry
*) bh
;
1468 /* If there is a duplicate definition somewhere, then HI may not
1469 point to an indirect symbol. We will have reported an error
1470 to the user in that case. */
1472 if (hi
->root
.type
== bfd_link_hash_indirect
)
1474 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1476 /* See if the new flags lead us to realize that the symbol
1488 if (hi
->ref_regular
)
1498 /* This routine is used to export all defined symbols into the dynamic
1499 symbol table. It is called via elf_link_hash_traverse. */
1502 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1504 struct elf_info_failed
*eif
= data
;
1506 /* Ignore indirect symbols. These are added by the versioning code. */
1507 if (h
->root
.type
== bfd_link_hash_indirect
)
1510 if (h
->root
.type
== bfd_link_hash_warning
)
1511 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1513 if (h
->dynindx
== -1
1517 struct bfd_elf_version_tree
*t
;
1518 struct bfd_elf_version_expr
*d
;
1520 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1522 if (t
->globals
.list
!= NULL
)
1524 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1529 if (t
->locals
.list
!= NULL
)
1531 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1540 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1551 /* Look through the symbols which are defined in other shared
1552 libraries and referenced here. Update the list of version
1553 dependencies. This will be put into the .gnu.version_r section.
1554 This function is called via elf_link_hash_traverse. */
1557 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1560 struct elf_find_verdep_info
*rinfo
= data
;
1561 Elf_Internal_Verneed
*t
;
1562 Elf_Internal_Vernaux
*a
;
1565 if (h
->root
.type
== bfd_link_hash_warning
)
1566 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1568 /* We only care about symbols defined in shared objects with version
1573 || h
->verinfo
.verdef
== NULL
)
1576 /* See if we already know about this version. */
1577 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1579 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1582 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1583 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1589 /* This is a new version. Add it to tree we are building. */
1594 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1597 rinfo
->failed
= TRUE
;
1601 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1602 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1603 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1607 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1609 /* Note that we are copying a string pointer here, and testing it
1610 above. If bfd_elf_string_from_elf_section is ever changed to
1611 discard the string data when low in memory, this will have to be
1613 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1615 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1616 a
->vna_nextptr
= t
->vn_auxptr
;
1618 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1621 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1628 /* Figure out appropriate versions for all the symbols. We may not
1629 have the version number script until we have read all of the input
1630 files, so until that point we don't know which symbols should be
1631 local. This function is called via elf_link_hash_traverse. */
1634 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1636 struct elf_assign_sym_version_info
*sinfo
;
1637 struct bfd_link_info
*info
;
1638 const struct elf_backend_data
*bed
;
1639 struct elf_info_failed eif
;
1646 if (h
->root
.type
== bfd_link_hash_warning
)
1647 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1649 /* Fix the symbol flags. */
1652 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1655 sinfo
->failed
= TRUE
;
1659 /* We only need version numbers for symbols defined in regular
1661 if (!h
->def_regular
)
1664 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1665 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1666 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1668 struct bfd_elf_version_tree
*t
;
1673 /* There are two consecutive ELF_VER_CHR characters if this is
1674 not a hidden symbol. */
1676 if (*p
== ELF_VER_CHR
)
1682 /* If there is no version string, we can just return out. */
1690 /* Look for the version. If we find it, it is no longer weak. */
1691 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1693 if (strcmp (t
->name
, p
) == 0)
1697 struct bfd_elf_version_expr
*d
;
1699 len
= p
- h
->root
.root
.string
;
1700 alc
= bfd_malloc (len
);
1703 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1704 alc
[len
- 1] = '\0';
1705 if (alc
[len
- 2] == ELF_VER_CHR
)
1706 alc
[len
- 2] = '\0';
1708 h
->verinfo
.vertree
= t
;
1712 if (t
->globals
.list
!= NULL
)
1713 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1715 /* See if there is anything to force this symbol to
1717 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1719 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1723 && ! info
->export_dynamic
)
1724 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1732 /* If we are building an application, we need to create a
1733 version node for this version. */
1734 if (t
== NULL
&& info
->executable
)
1736 struct bfd_elf_version_tree
**pp
;
1739 /* If we aren't going to export this symbol, we don't need
1740 to worry about it. */
1741 if (h
->dynindx
== -1)
1745 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1748 sinfo
->failed
= TRUE
;
1753 t
->name_indx
= (unsigned int) -1;
1757 /* Don't count anonymous version tag. */
1758 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1760 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1762 t
->vernum
= version_index
;
1766 h
->verinfo
.vertree
= t
;
1770 /* We could not find the version for a symbol when
1771 generating a shared archive. Return an error. */
1772 (*_bfd_error_handler
)
1773 (_("%B: undefined versioned symbol name %s"),
1774 sinfo
->output_bfd
, h
->root
.root
.string
);
1775 bfd_set_error (bfd_error_bad_value
);
1776 sinfo
->failed
= TRUE
;
1784 /* If we don't have a version for this symbol, see if we can find
1786 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1788 struct bfd_elf_version_tree
*t
;
1789 struct bfd_elf_version_tree
*local_ver
;
1790 struct bfd_elf_version_expr
*d
;
1792 /* See if can find what version this symbol is in. If the
1793 symbol is supposed to be local, then don't actually register
1796 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1798 if (t
->globals
.list
!= NULL
)
1800 bfd_boolean matched
;
1804 while ((d
= (*t
->match
) (&t
->globals
, d
,
1805 h
->root
.root
.string
)) != NULL
)
1810 /* There is a version without definition. Make
1811 the symbol the default definition for this
1813 h
->verinfo
.vertree
= t
;
1821 /* There is no undefined version for this symbol. Hide the
1823 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1826 if (t
->locals
.list
!= NULL
)
1829 while ((d
= (*t
->match
) (&t
->locals
, d
,
1830 h
->root
.root
.string
)) != NULL
)
1833 /* If the match is "*", keep looking for a more
1834 explicit, perhaps even global, match.
1835 XXX: Shouldn't this be !d->wildcard instead? */
1836 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1845 if (local_ver
!= NULL
)
1847 h
->verinfo
.vertree
= local_ver
;
1848 if (h
->dynindx
!= -1
1850 && ! info
->export_dynamic
)
1852 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1860 /* Read and swap the relocs from the section indicated by SHDR. This
1861 may be either a REL or a RELA section. The relocations are
1862 translated into RELA relocations and stored in INTERNAL_RELOCS,
1863 which should have already been allocated to contain enough space.
1864 The EXTERNAL_RELOCS are a buffer where the external form of the
1865 relocations should be stored.
1867 Returns FALSE if something goes wrong. */
1870 elf_link_read_relocs_from_section (bfd
*abfd
,
1872 Elf_Internal_Shdr
*shdr
,
1873 void *external_relocs
,
1874 Elf_Internal_Rela
*internal_relocs
)
1876 const struct elf_backend_data
*bed
;
1877 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1878 const bfd_byte
*erela
;
1879 const bfd_byte
*erelaend
;
1880 Elf_Internal_Rela
*irela
;
1881 Elf_Internal_Shdr
*symtab_hdr
;
1884 /* Position ourselves at the start of the section. */
1885 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1888 /* Read the relocations. */
1889 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1892 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1893 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1895 bed
= get_elf_backend_data (abfd
);
1897 /* Convert the external relocations to the internal format. */
1898 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1899 swap_in
= bed
->s
->swap_reloc_in
;
1900 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1901 swap_in
= bed
->s
->swap_reloca_in
;
1904 bfd_set_error (bfd_error_wrong_format
);
1908 erela
= external_relocs
;
1909 erelaend
= erela
+ shdr
->sh_size
;
1910 irela
= internal_relocs
;
1911 while (erela
< erelaend
)
1915 (*swap_in
) (abfd
, erela
, irela
);
1916 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1917 if (bed
->s
->arch_size
== 64)
1919 if ((size_t) r_symndx
>= nsyms
)
1921 (*_bfd_error_handler
)
1922 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1923 " for offset 0x%lx in section `%A'"),
1925 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1926 bfd_set_error (bfd_error_bad_value
);
1929 irela
+= bed
->s
->int_rels_per_ext_rel
;
1930 erela
+= shdr
->sh_entsize
;
1936 /* Read and swap the relocs for a section O. They may have been
1937 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1938 not NULL, they are used as buffers to read into. They are known to
1939 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1940 the return value is allocated using either malloc or bfd_alloc,
1941 according to the KEEP_MEMORY argument. If O has two relocation
1942 sections (both REL and RELA relocations), then the REL_HDR
1943 relocations will appear first in INTERNAL_RELOCS, followed by the
1944 REL_HDR2 relocations. */
1947 _bfd_elf_link_read_relocs (bfd
*abfd
,
1949 void *external_relocs
,
1950 Elf_Internal_Rela
*internal_relocs
,
1951 bfd_boolean keep_memory
)
1953 Elf_Internal_Shdr
*rel_hdr
;
1954 void *alloc1
= NULL
;
1955 Elf_Internal_Rela
*alloc2
= NULL
;
1956 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1958 if (elf_section_data (o
)->relocs
!= NULL
)
1959 return elf_section_data (o
)->relocs
;
1961 if (o
->reloc_count
== 0)
1964 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
1966 if (internal_relocs
== NULL
)
1970 size
= o
->reloc_count
;
1971 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
1973 internal_relocs
= bfd_alloc (abfd
, size
);
1975 internal_relocs
= alloc2
= bfd_malloc (size
);
1976 if (internal_relocs
== NULL
)
1980 if (external_relocs
== NULL
)
1982 bfd_size_type size
= rel_hdr
->sh_size
;
1984 if (elf_section_data (o
)->rel_hdr2
)
1985 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
1986 alloc1
= bfd_malloc (size
);
1989 external_relocs
= alloc1
;
1992 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
1996 if (elf_section_data (o
)->rel_hdr2
1997 && (!elf_link_read_relocs_from_section
1999 elf_section_data (o
)->rel_hdr2
,
2000 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2001 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2002 * bed
->s
->int_rels_per_ext_rel
))))
2005 /* Cache the results for next time, if we can. */
2007 elf_section_data (o
)->relocs
= internal_relocs
;
2012 /* Don't free alloc2, since if it was allocated we are passing it
2013 back (under the name of internal_relocs). */
2015 return internal_relocs
;
2025 /* Compute the size of, and allocate space for, REL_HDR which is the
2026 section header for a section containing relocations for O. */
2029 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2030 Elf_Internal_Shdr
*rel_hdr
,
2033 bfd_size_type reloc_count
;
2034 bfd_size_type num_rel_hashes
;
2036 /* Figure out how many relocations there will be. */
2037 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2038 reloc_count
= elf_section_data (o
)->rel_count
;
2040 reloc_count
= elf_section_data (o
)->rel_count2
;
2042 num_rel_hashes
= o
->reloc_count
;
2043 if (num_rel_hashes
< reloc_count
)
2044 num_rel_hashes
= reloc_count
;
2046 /* That allows us to calculate the size of the section. */
2047 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2049 /* The contents field must last into write_object_contents, so we
2050 allocate it with bfd_alloc rather than malloc. Also since we
2051 cannot be sure that the contents will actually be filled in,
2052 we zero the allocated space. */
2053 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2054 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2057 /* We only allocate one set of hash entries, so we only do it the
2058 first time we are called. */
2059 if (elf_section_data (o
)->rel_hashes
== NULL
2062 struct elf_link_hash_entry
**p
;
2064 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2068 elf_section_data (o
)->rel_hashes
= p
;
2074 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2075 originated from the section given by INPUT_REL_HDR) to the
2079 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2080 asection
*input_section
,
2081 Elf_Internal_Shdr
*input_rel_hdr
,
2082 Elf_Internal_Rela
*internal_relocs
)
2084 Elf_Internal_Rela
*irela
;
2085 Elf_Internal_Rela
*irelaend
;
2087 Elf_Internal_Shdr
*output_rel_hdr
;
2088 asection
*output_section
;
2089 unsigned int *rel_countp
= NULL
;
2090 const struct elf_backend_data
*bed
;
2091 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2093 output_section
= input_section
->output_section
;
2094 output_rel_hdr
= NULL
;
2096 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2097 == input_rel_hdr
->sh_entsize
)
2099 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2100 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2102 else if (elf_section_data (output_section
)->rel_hdr2
2103 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2104 == input_rel_hdr
->sh_entsize
))
2106 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2107 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2111 (*_bfd_error_handler
)
2112 (_("%B: relocation size mismatch in %B section %A"),
2113 output_bfd
, input_section
->owner
, input_section
);
2114 bfd_set_error (bfd_error_wrong_object_format
);
2118 bed
= get_elf_backend_data (output_bfd
);
2119 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2120 swap_out
= bed
->s
->swap_reloc_out
;
2121 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2122 swap_out
= bed
->s
->swap_reloca_out
;
2126 erel
= output_rel_hdr
->contents
;
2127 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2128 irela
= internal_relocs
;
2129 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2130 * bed
->s
->int_rels_per_ext_rel
);
2131 while (irela
< irelaend
)
2133 (*swap_out
) (output_bfd
, irela
, erel
);
2134 irela
+= bed
->s
->int_rels_per_ext_rel
;
2135 erel
+= input_rel_hdr
->sh_entsize
;
2138 /* Bump the counter, so that we know where to add the next set of
2140 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2145 /* Fix up the flags for a symbol. This handles various cases which
2146 can only be fixed after all the input files are seen. This is
2147 currently called by both adjust_dynamic_symbol and
2148 assign_sym_version, which is unnecessary but perhaps more robust in
2149 the face of future changes. */
2152 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2153 struct elf_info_failed
*eif
)
2155 /* If this symbol was mentioned in a non-ELF file, try to set
2156 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2157 permit a non-ELF file to correctly refer to a symbol defined in
2158 an ELF dynamic object. */
2161 while (h
->root
.type
== bfd_link_hash_indirect
)
2162 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2164 if (h
->root
.type
!= bfd_link_hash_defined
2165 && h
->root
.type
!= bfd_link_hash_defweak
)
2168 h
->ref_regular_nonweak
= 1;
2172 if (h
->root
.u
.def
.section
->owner
!= NULL
2173 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2174 == bfd_target_elf_flavour
))
2177 h
->ref_regular_nonweak
= 1;
2183 if (h
->dynindx
== -1
2187 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2196 /* Unfortunately, NON_ELF is only correct if the symbol
2197 was first seen in a non-ELF file. Fortunately, if the symbol
2198 was first seen in an ELF file, we're probably OK unless the
2199 symbol was defined in a non-ELF file. Catch that case here.
2200 FIXME: We're still in trouble if the symbol was first seen in
2201 a dynamic object, and then later in a non-ELF regular object. */
2202 if ((h
->root
.type
== bfd_link_hash_defined
2203 || h
->root
.type
== bfd_link_hash_defweak
)
2205 && (h
->root
.u
.def
.section
->owner
!= NULL
2206 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2207 != bfd_target_elf_flavour
)
2208 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2209 && !h
->def_dynamic
)))
2213 /* If this is a final link, and the symbol was defined as a common
2214 symbol in a regular object file, and there was no definition in
2215 any dynamic object, then the linker will have allocated space for
2216 the symbol in a common section but the DEF_REGULAR
2217 flag will not have been set. */
2218 if (h
->root
.type
== bfd_link_hash_defined
2222 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2225 /* If -Bsymbolic was used (which means to bind references to global
2226 symbols to the definition within the shared object), and this
2227 symbol was defined in a regular object, then it actually doesn't
2228 need a PLT entry. Likewise, if the symbol has non-default
2229 visibility. If the symbol has hidden or internal visibility, we
2230 will force it local. */
2232 && eif
->info
->shared
2233 && is_elf_hash_table (eif
->info
->hash
)
2234 && (eif
->info
->symbolic
2235 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2238 const struct elf_backend_data
*bed
;
2239 bfd_boolean force_local
;
2241 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2243 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2244 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2245 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2248 /* If a weak undefined symbol has non-default visibility, we also
2249 hide it from the dynamic linker. */
2250 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2251 && h
->root
.type
== bfd_link_hash_undefweak
)
2253 const struct elf_backend_data
*bed
;
2254 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2255 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2258 /* If this is a weak defined symbol in a dynamic object, and we know
2259 the real definition in the dynamic object, copy interesting flags
2260 over to the real definition. */
2261 if (h
->u
.weakdef
!= NULL
)
2263 struct elf_link_hash_entry
*weakdef
;
2265 weakdef
= h
->u
.weakdef
;
2266 if (h
->root
.type
== bfd_link_hash_indirect
)
2267 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2269 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2270 || h
->root
.type
== bfd_link_hash_defweak
);
2271 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2272 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2273 BFD_ASSERT (weakdef
->def_dynamic
);
2275 /* If the real definition is defined by a regular object file,
2276 don't do anything special. See the longer description in
2277 _bfd_elf_adjust_dynamic_symbol, below. */
2278 if (weakdef
->def_regular
)
2279 h
->u
.weakdef
= NULL
;
2282 const struct elf_backend_data
*bed
;
2284 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2285 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2292 /* Make the backend pick a good value for a dynamic symbol. This is
2293 called via elf_link_hash_traverse, and also calls itself
2297 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2299 struct elf_info_failed
*eif
= data
;
2301 const struct elf_backend_data
*bed
;
2303 if (! is_elf_hash_table (eif
->info
->hash
))
2306 if (h
->root
.type
== bfd_link_hash_warning
)
2308 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2309 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2311 /* When warning symbols are created, they **replace** the "real"
2312 entry in the hash table, thus we never get to see the real
2313 symbol in a hash traversal. So look at it now. */
2314 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2317 /* Ignore indirect symbols. These are added by the versioning code. */
2318 if (h
->root
.type
== bfd_link_hash_indirect
)
2321 /* Fix the symbol flags. */
2322 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2325 /* If this symbol does not require a PLT entry, and it is not
2326 defined by a dynamic object, or is not referenced by a regular
2327 object, ignore it. We do have to handle a weak defined symbol,
2328 even if no regular object refers to it, if we decided to add it
2329 to the dynamic symbol table. FIXME: Do we normally need to worry
2330 about symbols which are defined by one dynamic object and
2331 referenced by another one? */
2336 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2338 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2342 /* If we've already adjusted this symbol, don't do it again. This
2343 can happen via a recursive call. */
2344 if (h
->dynamic_adjusted
)
2347 /* Don't look at this symbol again. Note that we must set this
2348 after checking the above conditions, because we may look at a
2349 symbol once, decide not to do anything, and then get called
2350 recursively later after REF_REGULAR is set below. */
2351 h
->dynamic_adjusted
= 1;
2353 /* If this is a weak definition, and we know a real definition, and
2354 the real symbol is not itself defined by a regular object file,
2355 then get a good value for the real definition. We handle the
2356 real symbol first, for the convenience of the backend routine.
2358 Note that there is a confusing case here. If the real definition
2359 is defined by a regular object file, we don't get the real symbol
2360 from the dynamic object, but we do get the weak symbol. If the
2361 processor backend uses a COPY reloc, then if some routine in the
2362 dynamic object changes the real symbol, we will not see that
2363 change in the corresponding weak symbol. This is the way other
2364 ELF linkers work as well, and seems to be a result of the shared
2367 I will clarify this issue. Most SVR4 shared libraries define the
2368 variable _timezone and define timezone as a weak synonym. The
2369 tzset call changes _timezone. If you write
2370 extern int timezone;
2372 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2373 you might expect that, since timezone is a synonym for _timezone,
2374 the same number will print both times. However, if the processor
2375 backend uses a COPY reloc, then actually timezone will be copied
2376 into your process image, and, since you define _timezone
2377 yourself, _timezone will not. Thus timezone and _timezone will
2378 wind up at different memory locations. The tzset call will set
2379 _timezone, leaving timezone unchanged. */
2381 if (h
->u
.weakdef
!= NULL
)
2383 /* If we get to this point, we know there is an implicit
2384 reference by a regular object file via the weak symbol H.
2385 FIXME: Is this really true? What if the traversal finds
2386 H->U.WEAKDEF before it finds H? */
2387 h
->u
.weakdef
->ref_regular
= 1;
2389 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2393 /* If a symbol has no type and no size and does not require a PLT
2394 entry, then we are probably about to do the wrong thing here: we
2395 are probably going to create a COPY reloc for an empty object.
2396 This case can arise when a shared object is built with assembly
2397 code, and the assembly code fails to set the symbol type. */
2399 && h
->type
== STT_NOTYPE
2401 (*_bfd_error_handler
)
2402 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2403 h
->root
.root
.string
);
2405 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2406 bed
= get_elf_backend_data (dynobj
);
2407 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2416 /* Adjust all external symbols pointing into SEC_MERGE sections
2417 to reflect the object merging within the sections. */
2420 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2424 if (h
->root
.type
== bfd_link_hash_warning
)
2425 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2427 if ((h
->root
.type
== bfd_link_hash_defined
2428 || h
->root
.type
== bfd_link_hash_defweak
)
2429 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2430 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2432 bfd
*output_bfd
= data
;
2434 h
->root
.u
.def
.value
=
2435 _bfd_merged_section_offset (output_bfd
,
2436 &h
->root
.u
.def
.section
,
2437 elf_section_data (sec
)->sec_info
,
2438 h
->root
.u
.def
.value
);
2444 /* Returns false if the symbol referred to by H should be considered
2445 to resolve local to the current module, and true if it should be
2446 considered to bind dynamically. */
2449 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2450 struct bfd_link_info
*info
,
2451 bfd_boolean ignore_protected
)
2453 bfd_boolean binding_stays_local_p
;
2458 while (h
->root
.type
== bfd_link_hash_indirect
2459 || h
->root
.type
== bfd_link_hash_warning
)
2460 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2462 /* If it was forced local, then clearly it's not dynamic. */
2463 if (h
->dynindx
== -1)
2465 if (h
->forced_local
)
2468 /* Identify the cases where name binding rules say that a
2469 visible symbol resolves locally. */
2470 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2472 switch (ELF_ST_VISIBILITY (h
->other
))
2479 /* Proper resolution for function pointer equality may require
2480 that these symbols perhaps be resolved dynamically, even though
2481 we should be resolving them to the current module. */
2482 if (!ignore_protected
)
2483 binding_stays_local_p
= TRUE
;
2490 /* If it isn't defined locally, then clearly it's dynamic. */
2491 if (!h
->def_regular
)
2494 /* Otherwise, the symbol is dynamic if binding rules don't tell
2495 us that it remains local. */
2496 return !binding_stays_local_p
;
2499 /* Return true if the symbol referred to by H should be considered
2500 to resolve local to the current module, and false otherwise. Differs
2501 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2502 undefined symbols and weak symbols. */
2505 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2506 struct bfd_link_info
*info
,
2507 bfd_boolean local_protected
)
2509 /* If it's a local sym, of course we resolve locally. */
2513 /* Common symbols that become definitions don't get the DEF_REGULAR
2514 flag set, so test it first, and don't bail out. */
2515 if (ELF_COMMON_DEF_P (h
))
2517 /* If we don't have a definition in a regular file, then we can't
2518 resolve locally. The sym is either undefined or dynamic. */
2519 else if (!h
->def_regular
)
2522 /* Forced local symbols resolve locally. */
2523 if (h
->forced_local
)
2526 /* As do non-dynamic symbols. */
2527 if (h
->dynindx
== -1)
2530 /* At this point, we know the symbol is defined and dynamic. In an
2531 executable it must resolve locally, likewise when building symbolic
2532 shared libraries. */
2533 if (info
->executable
|| info
->symbolic
)
2536 /* Now deal with defined dynamic symbols in shared libraries. Ones
2537 with default visibility might not resolve locally. */
2538 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2541 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2542 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2545 /* Function pointer equality tests may require that STV_PROTECTED
2546 symbols be treated as dynamic symbols, even when we know that the
2547 dynamic linker will resolve them locally. */
2548 return local_protected
;
2551 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2552 aligned. Returns the first TLS output section. */
2554 struct bfd_section
*
2555 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2557 struct bfd_section
*sec
, *tls
;
2558 unsigned int align
= 0;
2560 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2561 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2565 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2566 if (sec
->alignment_power
> align
)
2567 align
= sec
->alignment_power
;
2569 elf_hash_table (info
)->tls_sec
= tls
;
2571 /* Ensure the alignment of the first section is the largest alignment,
2572 so that the tls segment starts aligned. */
2574 tls
->alignment_power
= align
;
2579 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2581 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2582 Elf_Internal_Sym
*sym
)
2584 /* Local symbols do not count, but target specific ones might. */
2585 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2586 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2589 /* Function symbols do not count. */
2590 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2593 /* If the section is undefined, then so is the symbol. */
2594 if (sym
->st_shndx
== SHN_UNDEF
)
2597 /* If the symbol is defined in the common section, then
2598 it is a common definition and so does not count. */
2599 if (sym
->st_shndx
== SHN_COMMON
)
2602 /* If the symbol is in a target specific section then we
2603 must rely upon the backend to tell us what it is. */
2604 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2605 /* FIXME - this function is not coded yet:
2607 return _bfd_is_global_symbol_definition (abfd, sym);
2609 Instead for now assume that the definition is not global,
2610 Even if this is wrong, at least the linker will behave
2611 in the same way that it used to do. */
2617 /* Search the symbol table of the archive element of the archive ABFD
2618 whose archive map contains a mention of SYMDEF, and determine if
2619 the symbol is defined in this element. */
2621 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2623 Elf_Internal_Shdr
* hdr
;
2624 bfd_size_type symcount
;
2625 bfd_size_type extsymcount
;
2626 bfd_size_type extsymoff
;
2627 Elf_Internal_Sym
*isymbuf
;
2628 Elf_Internal_Sym
*isym
;
2629 Elf_Internal_Sym
*isymend
;
2632 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2636 if (! bfd_check_format (abfd
, bfd_object
))
2639 /* If we have already included the element containing this symbol in the
2640 link then we do not need to include it again. Just claim that any symbol
2641 it contains is not a definition, so that our caller will not decide to
2642 (re)include this element. */
2643 if (abfd
->archive_pass
)
2646 /* Select the appropriate symbol table. */
2647 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2648 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2650 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2652 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2654 /* The sh_info field of the symtab header tells us where the
2655 external symbols start. We don't care about the local symbols. */
2656 if (elf_bad_symtab (abfd
))
2658 extsymcount
= symcount
;
2663 extsymcount
= symcount
- hdr
->sh_info
;
2664 extsymoff
= hdr
->sh_info
;
2667 if (extsymcount
== 0)
2670 /* Read in the symbol table. */
2671 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2673 if (isymbuf
== NULL
)
2676 /* Scan the symbol table looking for SYMDEF. */
2678 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2682 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2687 if (strcmp (name
, symdef
->name
) == 0)
2689 result
= is_global_data_symbol_definition (abfd
, isym
);
2699 /* Add an entry to the .dynamic table. */
2702 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2706 struct elf_link_hash_table
*hash_table
;
2707 const struct elf_backend_data
*bed
;
2709 bfd_size_type newsize
;
2710 bfd_byte
*newcontents
;
2711 Elf_Internal_Dyn dyn
;
2713 hash_table
= elf_hash_table (info
);
2714 if (! is_elf_hash_table (hash_table
))
2717 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2719 (_("warning: creating a DT_TEXTREL in a shared object."));
2721 bed
= get_elf_backend_data (hash_table
->dynobj
);
2722 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2723 BFD_ASSERT (s
!= NULL
);
2725 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2726 newcontents
= bfd_realloc (s
->contents
, newsize
);
2727 if (newcontents
== NULL
)
2731 dyn
.d_un
.d_val
= val
;
2732 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2735 s
->contents
= newcontents
;
2740 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2741 otherwise just check whether one already exists. Returns -1 on error,
2742 1 if a DT_NEEDED tag already exists, and 0 on success. */
2745 elf_add_dt_needed_tag (struct bfd_link_info
*info
,
2749 struct elf_link_hash_table
*hash_table
;
2750 bfd_size_type oldsize
;
2751 bfd_size_type strindex
;
2753 hash_table
= elf_hash_table (info
);
2754 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2755 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2756 if (strindex
== (bfd_size_type
) -1)
2759 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2762 const struct elf_backend_data
*bed
;
2765 bed
= get_elf_backend_data (hash_table
->dynobj
);
2766 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2767 BFD_ASSERT (sdyn
!= NULL
);
2769 for (extdyn
= sdyn
->contents
;
2770 extdyn
< sdyn
->contents
+ sdyn
->size
;
2771 extdyn
+= bed
->s
->sizeof_dyn
)
2773 Elf_Internal_Dyn dyn
;
2775 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2776 if (dyn
.d_tag
== DT_NEEDED
2777 && dyn
.d_un
.d_val
== strindex
)
2779 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2787 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2791 /* We were just checking for existence of the tag. */
2792 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2797 /* Sort symbol by value and section. */
2799 elf_sort_symbol (const void *arg1
, const void *arg2
)
2801 const struct elf_link_hash_entry
*h1
;
2802 const struct elf_link_hash_entry
*h2
;
2803 bfd_signed_vma vdiff
;
2805 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2806 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2807 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2809 return vdiff
> 0 ? 1 : -1;
2812 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2814 return sdiff
> 0 ? 1 : -1;
2819 /* This function is used to adjust offsets into .dynstr for
2820 dynamic symbols. This is called via elf_link_hash_traverse. */
2823 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2825 struct elf_strtab_hash
*dynstr
= data
;
2827 if (h
->root
.type
== bfd_link_hash_warning
)
2828 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2830 if (h
->dynindx
!= -1)
2831 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2835 /* Assign string offsets in .dynstr, update all structures referencing
2839 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2841 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2842 struct elf_link_local_dynamic_entry
*entry
;
2843 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2844 bfd
*dynobj
= hash_table
->dynobj
;
2847 const struct elf_backend_data
*bed
;
2850 _bfd_elf_strtab_finalize (dynstr
);
2851 size
= _bfd_elf_strtab_size (dynstr
);
2853 bed
= get_elf_backend_data (dynobj
);
2854 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2855 BFD_ASSERT (sdyn
!= NULL
);
2857 /* Update all .dynamic entries referencing .dynstr strings. */
2858 for (extdyn
= sdyn
->contents
;
2859 extdyn
< sdyn
->contents
+ sdyn
->size
;
2860 extdyn
+= bed
->s
->sizeof_dyn
)
2862 Elf_Internal_Dyn dyn
;
2864 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
2868 dyn
.d_un
.d_val
= size
;
2876 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
2881 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
2884 /* Now update local dynamic symbols. */
2885 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
2886 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
2887 entry
->isym
.st_name
);
2889 /* And the rest of dynamic symbols. */
2890 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
2892 /* Adjust version definitions. */
2893 if (elf_tdata (output_bfd
)->cverdefs
)
2898 Elf_Internal_Verdef def
;
2899 Elf_Internal_Verdaux defaux
;
2901 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
2905 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
2907 p
+= sizeof (Elf_External_Verdef
);
2908 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
2910 for (i
= 0; i
< def
.vd_cnt
; ++i
)
2912 _bfd_elf_swap_verdaux_in (output_bfd
,
2913 (Elf_External_Verdaux
*) p
, &defaux
);
2914 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
2916 _bfd_elf_swap_verdaux_out (output_bfd
,
2917 &defaux
, (Elf_External_Verdaux
*) p
);
2918 p
+= sizeof (Elf_External_Verdaux
);
2921 while (def
.vd_next
);
2924 /* Adjust version references. */
2925 if (elf_tdata (output_bfd
)->verref
)
2930 Elf_Internal_Verneed need
;
2931 Elf_Internal_Vernaux needaux
;
2933 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
2937 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
2939 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
2940 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
2941 (Elf_External_Verneed
*) p
);
2942 p
+= sizeof (Elf_External_Verneed
);
2943 for (i
= 0; i
< need
.vn_cnt
; ++i
)
2945 _bfd_elf_swap_vernaux_in (output_bfd
,
2946 (Elf_External_Vernaux
*) p
, &needaux
);
2947 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
2949 _bfd_elf_swap_vernaux_out (output_bfd
,
2951 (Elf_External_Vernaux
*) p
);
2952 p
+= sizeof (Elf_External_Vernaux
);
2955 while (need
.vn_next
);
2961 /* Add symbols from an ELF object file to the linker hash table. */
2964 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2966 bfd_boolean (*add_symbol_hook
)
2967 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
2968 const char **, flagword
*, asection
**, bfd_vma
*);
2969 bfd_boolean (*check_relocs
)
2970 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
2971 bfd_boolean (*check_directives
)
2972 (bfd
*, struct bfd_link_info
*);
2973 bfd_boolean collect
;
2974 Elf_Internal_Shdr
*hdr
;
2975 bfd_size_type symcount
;
2976 bfd_size_type extsymcount
;
2977 bfd_size_type extsymoff
;
2978 struct elf_link_hash_entry
**sym_hash
;
2979 bfd_boolean dynamic
;
2980 Elf_External_Versym
*extversym
= NULL
;
2981 Elf_External_Versym
*ever
;
2982 struct elf_link_hash_entry
*weaks
;
2983 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
2984 bfd_size_type nondeflt_vers_cnt
= 0;
2985 Elf_Internal_Sym
*isymbuf
= NULL
;
2986 Elf_Internal_Sym
*isym
;
2987 Elf_Internal_Sym
*isymend
;
2988 const struct elf_backend_data
*bed
;
2989 bfd_boolean add_needed
;
2990 struct elf_link_hash_table
* hash_table
;
2993 hash_table
= elf_hash_table (info
);
2995 bed
= get_elf_backend_data (abfd
);
2996 add_symbol_hook
= bed
->elf_add_symbol_hook
;
2997 collect
= bed
->collect
;
2999 if ((abfd
->flags
& DYNAMIC
) == 0)
3005 /* You can't use -r against a dynamic object. Also, there's no
3006 hope of using a dynamic object which does not exactly match
3007 the format of the output file. */
3008 if (info
->relocatable
3009 || !is_elf_hash_table (hash_table
)
3010 || hash_table
->root
.creator
!= abfd
->xvec
)
3012 if (info
->relocatable
)
3013 bfd_set_error (bfd_error_invalid_operation
);
3015 bfd_set_error (bfd_error_wrong_format
);
3020 /* As a GNU extension, any input sections which are named
3021 .gnu.warning.SYMBOL are treated as warning symbols for the given
3022 symbol. This differs from .gnu.warning sections, which generate
3023 warnings when they are included in an output file. */
3024 if (info
->executable
)
3028 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3032 name
= bfd_get_section_name (abfd
, s
);
3033 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3037 bfd_size_type prefix_len
;
3038 const char * gnu_warning_prefix
= _("warning: ");
3040 name
+= sizeof ".gnu.warning." - 1;
3042 /* If this is a shared object, then look up the symbol
3043 in the hash table. If it is there, and it is already
3044 been defined, then we will not be using the entry
3045 from this shared object, so we don't need to warn.
3046 FIXME: If we see the definition in a regular object
3047 later on, we will warn, but we shouldn't. The only
3048 fix is to keep track of what warnings we are supposed
3049 to emit, and then handle them all at the end of the
3053 struct elf_link_hash_entry
*h
;
3055 h
= elf_link_hash_lookup (hash_table
, name
,
3056 FALSE
, FALSE
, TRUE
);
3058 /* FIXME: What about bfd_link_hash_common? */
3060 && (h
->root
.type
== bfd_link_hash_defined
3061 || h
->root
.type
== bfd_link_hash_defweak
))
3063 /* We don't want to issue this warning. Clobber
3064 the section size so that the warning does not
3065 get copied into the output file. */
3072 prefix_len
= strlen (gnu_warning_prefix
);
3073 msg
= bfd_alloc (abfd
, prefix_len
+ sz
+ 1);
3077 strcpy (msg
, gnu_warning_prefix
);
3078 if (! bfd_get_section_contents (abfd
, s
, msg
+ prefix_len
, 0, sz
))
3081 msg
[prefix_len
+ sz
] = '\0';
3083 if (! (_bfd_generic_link_add_one_symbol
3084 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3085 FALSE
, collect
, NULL
)))
3088 if (! info
->relocatable
)
3090 /* Clobber the section size so that the warning does
3091 not get copied into the output file. */
3101 /* If we are creating a shared library, create all the dynamic
3102 sections immediately. We need to attach them to something,
3103 so we attach them to this BFD, provided it is the right
3104 format. FIXME: If there are no input BFD's of the same
3105 format as the output, we can't make a shared library. */
3107 && is_elf_hash_table (hash_table
)
3108 && hash_table
->root
.creator
== abfd
->xvec
3109 && ! hash_table
->dynamic_sections_created
)
3111 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3115 else if (!is_elf_hash_table (hash_table
))
3120 const char *soname
= NULL
;
3121 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3124 /* ld --just-symbols and dynamic objects don't mix very well.
3125 Test for --just-symbols by looking at info set up by
3126 _bfd_elf_link_just_syms. */
3127 if ((s
= abfd
->sections
) != NULL
3128 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3131 /* If this dynamic lib was specified on the command line with
3132 --as-needed in effect, then we don't want to add a DT_NEEDED
3133 tag unless the lib is actually used. Similary for libs brought
3134 in by another lib's DT_NEEDED. When --no-add-needed is used
3135 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3136 any dynamic library in DT_NEEDED tags in the dynamic lib at
3138 add_needed
= (elf_dyn_lib_class (abfd
)
3139 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3140 | DYN_NO_NEEDED
)) == 0;
3142 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3148 unsigned long shlink
;
3150 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3151 goto error_free_dyn
;
3153 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3155 goto error_free_dyn
;
3156 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3158 for (extdyn
= dynbuf
;
3159 extdyn
< dynbuf
+ s
->size
;
3160 extdyn
+= bed
->s
->sizeof_dyn
)
3162 Elf_Internal_Dyn dyn
;
3164 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3165 if (dyn
.d_tag
== DT_SONAME
)
3167 unsigned int tagv
= dyn
.d_un
.d_val
;
3168 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3170 goto error_free_dyn
;
3172 if (dyn
.d_tag
== DT_NEEDED
)
3174 struct bfd_link_needed_list
*n
, **pn
;
3176 unsigned int tagv
= dyn
.d_un
.d_val
;
3178 amt
= sizeof (struct bfd_link_needed_list
);
3179 n
= bfd_alloc (abfd
, amt
);
3180 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3181 if (n
== NULL
|| fnm
== NULL
)
3182 goto error_free_dyn
;
3183 amt
= strlen (fnm
) + 1;
3184 anm
= bfd_alloc (abfd
, amt
);
3186 goto error_free_dyn
;
3187 memcpy (anm
, fnm
, amt
);
3191 for (pn
= & hash_table
->needed
;
3197 if (dyn
.d_tag
== DT_RUNPATH
)
3199 struct bfd_link_needed_list
*n
, **pn
;
3201 unsigned int tagv
= dyn
.d_un
.d_val
;
3203 amt
= sizeof (struct bfd_link_needed_list
);
3204 n
= bfd_alloc (abfd
, amt
);
3205 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3206 if (n
== NULL
|| fnm
== NULL
)
3207 goto error_free_dyn
;
3208 amt
= strlen (fnm
) + 1;
3209 anm
= bfd_alloc (abfd
, amt
);
3211 goto error_free_dyn
;
3212 memcpy (anm
, fnm
, amt
);
3216 for (pn
= & runpath
;
3222 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3223 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3225 struct bfd_link_needed_list
*n
, **pn
;
3227 unsigned int tagv
= dyn
.d_un
.d_val
;
3229 amt
= sizeof (struct bfd_link_needed_list
);
3230 n
= bfd_alloc (abfd
, amt
);
3231 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3232 if (n
== NULL
|| fnm
== NULL
)
3233 goto error_free_dyn
;
3234 amt
= strlen (fnm
) + 1;
3235 anm
= bfd_alloc (abfd
, amt
);
3242 memcpy (anm
, fnm
, amt
);
3257 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3258 frees all more recently bfd_alloc'd blocks as well. */
3264 struct bfd_link_needed_list
**pn
;
3265 for (pn
= & hash_table
->runpath
;
3272 /* We do not want to include any of the sections in a dynamic
3273 object in the output file. We hack by simply clobbering the
3274 list of sections in the BFD. This could be handled more
3275 cleanly by, say, a new section flag; the existing
3276 SEC_NEVER_LOAD flag is not the one we want, because that one
3277 still implies that the section takes up space in the output
3279 bfd_section_list_clear (abfd
);
3281 /* If this is the first dynamic object found in the link, create
3282 the special sections required for dynamic linking. */
3283 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3286 /* Find the name to use in a DT_NEEDED entry that refers to this
3287 object. If the object has a DT_SONAME entry, we use it.
3288 Otherwise, if the generic linker stuck something in
3289 elf_dt_name, we use that. Otherwise, we just use the file
3291 if (soname
== NULL
|| *soname
== '\0')
3293 soname
= elf_dt_name (abfd
);
3294 if (soname
== NULL
|| *soname
== '\0')
3295 soname
= bfd_get_filename (abfd
);
3298 /* Save the SONAME because sometimes the linker emulation code
3299 will need to know it. */
3300 elf_dt_name (abfd
) = soname
;
3302 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3306 /* If we have already included this dynamic object in the
3307 link, just ignore it. There is no reason to include a
3308 particular dynamic object more than once. */
3313 /* If this is a dynamic object, we always link against the .dynsym
3314 symbol table, not the .symtab symbol table. The dynamic linker
3315 will only see the .dynsym symbol table, so there is no reason to
3316 look at .symtab for a dynamic object. */
3318 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3319 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3321 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3323 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3325 /* The sh_info field of the symtab header tells us where the
3326 external symbols start. We don't care about the local symbols at
3328 if (elf_bad_symtab (abfd
))
3330 extsymcount
= symcount
;
3335 extsymcount
= symcount
- hdr
->sh_info
;
3336 extsymoff
= hdr
->sh_info
;
3340 if (extsymcount
!= 0)
3342 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3344 if (isymbuf
== NULL
)
3347 /* We store a pointer to the hash table entry for each external
3349 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3350 sym_hash
= bfd_alloc (abfd
, amt
);
3351 if (sym_hash
== NULL
)
3352 goto error_free_sym
;
3353 elf_sym_hashes (abfd
) = sym_hash
;
3358 /* Read in any version definitions. */
3359 if (!_bfd_elf_slurp_version_tables (abfd
,
3360 info
->default_imported_symver
))
3361 goto error_free_sym
;
3363 /* Read in the symbol versions, but don't bother to convert them
3364 to internal format. */
3365 if (elf_dynversym (abfd
) != 0)
3367 Elf_Internal_Shdr
*versymhdr
;
3369 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3370 extversym
= bfd_malloc (versymhdr
->sh_size
);
3371 if (extversym
== NULL
)
3372 goto error_free_sym
;
3373 amt
= versymhdr
->sh_size
;
3374 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3375 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3376 goto error_free_vers
;
3382 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3383 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3385 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3392 struct elf_link_hash_entry
*h
;
3393 bfd_boolean definition
;
3394 bfd_boolean size_change_ok
;
3395 bfd_boolean type_change_ok
;
3396 bfd_boolean new_weakdef
;
3397 bfd_boolean override
;
3398 unsigned int old_alignment
;
3403 flags
= BSF_NO_FLAGS
;
3405 value
= isym
->st_value
;
3408 bind
= ELF_ST_BIND (isym
->st_info
);
3409 if (bind
== STB_LOCAL
)
3411 /* This should be impossible, since ELF requires that all
3412 global symbols follow all local symbols, and that sh_info
3413 point to the first global symbol. Unfortunately, Irix 5
3417 else if (bind
== STB_GLOBAL
)
3419 if (isym
->st_shndx
!= SHN_UNDEF
3420 && isym
->st_shndx
!= SHN_COMMON
)
3423 else if (bind
== STB_WEAK
)
3427 /* Leave it up to the processor backend. */
3430 if (isym
->st_shndx
== SHN_UNDEF
)
3431 sec
= bfd_und_section_ptr
;
3432 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3434 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3436 sec
= bfd_abs_section_ptr
;
3437 else if (sec
->kept_section
)
3439 /* Symbols from discarded section are undefined. */
3440 sec
= bfd_und_section_ptr
;
3441 isym
->st_shndx
= SHN_UNDEF
;
3443 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3446 else if (isym
->st_shndx
== SHN_ABS
)
3447 sec
= bfd_abs_section_ptr
;
3448 else if (isym
->st_shndx
== SHN_COMMON
)
3450 sec
= bfd_com_section_ptr
;
3451 /* What ELF calls the size we call the value. What ELF
3452 calls the value we call the alignment. */
3453 value
= isym
->st_size
;
3457 /* Leave it up to the processor backend. */
3460 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3463 goto error_free_vers
;
3465 if (isym
->st_shndx
== SHN_COMMON
3466 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3468 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3472 tcomm
= bfd_make_section (abfd
, ".tcommon");
3474 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3476 | SEC_LINKER_CREATED
3477 | SEC_THREAD_LOCAL
)))
3478 goto error_free_vers
;
3482 else if (add_symbol_hook
)
3484 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3486 goto error_free_vers
;
3488 /* The hook function sets the name to NULL if this symbol
3489 should be skipped for some reason. */
3494 /* Sanity check that all possibilities were handled. */
3497 bfd_set_error (bfd_error_bad_value
);
3498 goto error_free_vers
;
3501 if (bfd_is_und_section (sec
)
3502 || bfd_is_com_section (sec
))
3507 size_change_ok
= FALSE
;
3508 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3512 if (is_elf_hash_table (hash_table
))
3514 Elf_Internal_Versym iver
;
3515 unsigned int vernum
= 0;
3520 if (info
->default_imported_symver
)
3521 /* Use the default symbol version created earlier. */
3522 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3527 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3529 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3531 /* If this is a hidden symbol, or if it is not version
3532 1, we append the version name to the symbol name.
3533 However, we do not modify a non-hidden absolute
3534 symbol, because it might be the version symbol
3535 itself. FIXME: What if it isn't? */
3536 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3537 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3540 size_t namelen
, verlen
, newlen
;
3543 if (isym
->st_shndx
!= SHN_UNDEF
)
3545 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3547 else if (vernum
> 1)
3549 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3555 (*_bfd_error_handler
)
3556 (_("%B: %s: invalid version %u (max %d)"),
3558 elf_tdata (abfd
)->cverdefs
);
3559 bfd_set_error (bfd_error_bad_value
);
3560 goto error_free_vers
;
3565 /* We cannot simply test for the number of
3566 entries in the VERNEED section since the
3567 numbers for the needed versions do not start
3569 Elf_Internal_Verneed
*t
;
3572 for (t
= elf_tdata (abfd
)->verref
;
3576 Elf_Internal_Vernaux
*a
;
3578 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3580 if (a
->vna_other
== vernum
)
3582 verstr
= a
->vna_nodename
;
3591 (*_bfd_error_handler
)
3592 (_("%B: %s: invalid needed version %d"),
3593 abfd
, name
, vernum
);
3594 bfd_set_error (bfd_error_bad_value
);
3595 goto error_free_vers
;
3599 namelen
= strlen (name
);
3600 verlen
= strlen (verstr
);
3601 newlen
= namelen
+ verlen
+ 2;
3602 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3603 && isym
->st_shndx
!= SHN_UNDEF
)
3606 newname
= bfd_alloc (abfd
, newlen
);
3607 if (newname
== NULL
)
3608 goto error_free_vers
;
3609 memcpy (newname
, name
, namelen
);
3610 p
= newname
+ namelen
;
3612 /* If this is a defined non-hidden version symbol,
3613 we add another @ to the name. This indicates the
3614 default version of the symbol. */
3615 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3616 && isym
->st_shndx
!= SHN_UNDEF
)
3618 memcpy (p
, verstr
, verlen
+ 1);
3623 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3624 sym_hash
, &skip
, &override
,
3625 &type_change_ok
, &size_change_ok
))
3626 goto error_free_vers
;
3635 while (h
->root
.type
== bfd_link_hash_indirect
3636 || h
->root
.type
== bfd_link_hash_warning
)
3637 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3639 /* Remember the old alignment if this is a common symbol, so
3640 that we don't reduce the alignment later on. We can't
3641 check later, because _bfd_generic_link_add_one_symbol
3642 will set a default for the alignment which we want to
3643 override. We also remember the old bfd where the existing
3644 definition comes from. */
3645 switch (h
->root
.type
)
3650 case bfd_link_hash_defined
:
3651 case bfd_link_hash_defweak
:
3652 old_bfd
= h
->root
.u
.def
.section
->owner
;
3655 case bfd_link_hash_common
:
3656 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3657 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3661 if (elf_tdata (abfd
)->verdef
!= NULL
3665 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3668 if (! (_bfd_generic_link_add_one_symbol
3669 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3670 (struct bfd_link_hash_entry
**) sym_hash
)))
3671 goto error_free_vers
;
3674 while (h
->root
.type
== bfd_link_hash_indirect
3675 || h
->root
.type
== bfd_link_hash_warning
)
3676 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3679 new_weakdef
= FALSE
;
3682 && (flags
& BSF_WEAK
) != 0
3683 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3684 && is_elf_hash_table (hash_table
)
3685 && h
->u
.weakdef
== NULL
)
3687 /* Keep a list of all weak defined non function symbols from
3688 a dynamic object, using the weakdef field. Later in this
3689 function we will set the weakdef field to the correct
3690 value. We only put non-function symbols from dynamic
3691 objects on this list, because that happens to be the only
3692 time we need to know the normal symbol corresponding to a
3693 weak symbol, and the information is time consuming to
3694 figure out. If the weakdef field is not already NULL,
3695 then this symbol was already defined by some previous
3696 dynamic object, and we will be using that previous
3697 definition anyhow. */
3699 h
->u
.weakdef
= weaks
;
3704 /* Set the alignment of a common symbol. */
3705 if (isym
->st_shndx
== SHN_COMMON
3706 && h
->root
.type
== bfd_link_hash_common
)
3710 align
= bfd_log2 (isym
->st_value
);
3711 if (align
> old_alignment
3712 /* Permit an alignment power of zero if an alignment of one
3713 is specified and no other alignments have been specified. */
3714 || (isym
->st_value
== 1 && old_alignment
== 0))
3715 h
->root
.u
.c
.p
->alignment_power
= align
;
3717 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3720 if (is_elf_hash_table (hash_table
))
3724 /* Check the alignment when a common symbol is involved. This
3725 can change when a common symbol is overridden by a normal
3726 definition or a common symbol is ignored due to the old
3727 normal definition. We need to make sure the maximum
3728 alignment is maintained. */
3729 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3730 && h
->root
.type
!= bfd_link_hash_common
)
3732 unsigned int common_align
;
3733 unsigned int normal_align
;
3734 unsigned int symbol_align
;
3738 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3739 if (h
->root
.u
.def
.section
->owner
!= NULL
3740 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3742 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3743 if (normal_align
> symbol_align
)
3744 normal_align
= symbol_align
;
3747 normal_align
= symbol_align
;
3751 common_align
= old_alignment
;
3752 common_bfd
= old_bfd
;
3757 common_align
= bfd_log2 (isym
->st_value
);
3759 normal_bfd
= old_bfd
;
3762 if (normal_align
< common_align
)
3763 (*_bfd_error_handler
)
3764 (_("Warning: alignment %u of symbol `%s' in %B"
3765 " is smaller than %u in %B"),
3766 normal_bfd
, common_bfd
,
3767 1 << normal_align
, name
, 1 << common_align
);
3770 /* Remember the symbol size and type. */
3771 if (isym
->st_size
!= 0
3772 && (definition
|| h
->size
== 0))
3774 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3775 (*_bfd_error_handler
)
3776 (_("Warning: size of symbol `%s' changed"
3777 " from %lu in %B to %lu in %B"),
3779 name
, (unsigned long) h
->size
,
3780 (unsigned long) isym
->st_size
);
3782 h
->size
= isym
->st_size
;
3785 /* If this is a common symbol, then we always want H->SIZE
3786 to be the size of the common symbol. The code just above
3787 won't fix the size if a common symbol becomes larger. We
3788 don't warn about a size change here, because that is
3789 covered by --warn-common. */
3790 if (h
->root
.type
== bfd_link_hash_common
)
3791 h
->size
= h
->root
.u
.c
.size
;
3793 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3794 && (definition
|| h
->type
== STT_NOTYPE
))
3796 if (h
->type
!= STT_NOTYPE
3797 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3798 && ! type_change_ok
)
3799 (*_bfd_error_handler
)
3800 (_("Warning: type of symbol `%s' changed"
3801 " from %d to %d in %B"),
3802 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3804 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3807 /* If st_other has a processor-specific meaning, specific
3808 code might be needed here. We never merge the visibility
3809 attribute with the one from a dynamic object. */
3810 if (bed
->elf_backend_merge_symbol_attribute
)
3811 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3814 /* If this symbol has default visibility and the user has requested
3815 we not re-export it, then mark it as hidden. */
3816 if (definition
&& !dynamic
3818 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3819 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3820 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3822 if (isym
->st_other
!= 0 && !dynamic
)
3824 unsigned char hvis
, symvis
, other
, nvis
;
3826 /* Take the balance of OTHER from the definition. */
3827 other
= (definition
? isym
->st_other
: h
->other
);
3828 other
&= ~ ELF_ST_VISIBILITY (-1);
3830 /* Combine visibilities, using the most constraining one. */
3831 hvis
= ELF_ST_VISIBILITY (h
->other
);
3832 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3838 nvis
= hvis
< symvis
? hvis
: symvis
;
3840 h
->other
= other
| nvis
;
3843 /* Set a flag in the hash table entry indicating the type of
3844 reference or definition we just found. Keep a count of
3845 the number of dynamic symbols we find. A dynamic symbol
3846 is one which is referenced or defined by both a regular
3847 object and a shared object. */
3854 if (bind
!= STB_WEAK
)
3855 h
->ref_regular_nonweak
= 1;
3859 if (! info
->executable
3872 || (h
->u
.weakdef
!= NULL
3874 && h
->u
.weakdef
->dynindx
!= -1))
3878 /* Check to see if we need to add an indirect symbol for
3879 the default name. */
3880 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
3881 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
3882 &sec
, &value
, &dynsym
,
3884 goto error_free_vers
;
3886 if (definition
&& !dynamic
)
3888 char *p
= strchr (name
, ELF_VER_CHR
);
3889 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
3891 /* Queue non-default versions so that .symver x, x@FOO
3892 aliases can be checked. */
3893 if (! nondeflt_vers
)
3895 amt
= (isymend
- isym
+ 1)
3896 * sizeof (struct elf_link_hash_entry
*);
3897 nondeflt_vers
= bfd_malloc (amt
);
3899 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
3903 if (dynsym
&& h
->dynindx
== -1)
3905 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
3906 goto error_free_vers
;
3907 if (h
->u
.weakdef
!= NULL
3909 && h
->u
.weakdef
->dynindx
== -1)
3911 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
3912 goto error_free_vers
;
3915 else if (dynsym
&& h
->dynindx
!= -1)
3916 /* If the symbol already has a dynamic index, but
3917 visibility says it should not be visible, turn it into
3919 switch (ELF_ST_VISIBILITY (h
->other
))
3923 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
3934 const char *soname
= elf_dt_name (abfd
);
3936 /* A symbol from a library loaded via DT_NEEDED of some
3937 other library is referenced by a regular object.
3938 Add a DT_NEEDED entry for it. Issue an error if
3939 --no-add-needed is used. */
3940 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
3942 (*_bfd_error_handler
)
3943 (_("%s: invalid DSO for symbol `%s' definition"),
3945 bfd_set_error (bfd_error_bad_value
);
3946 goto error_free_vers
;
3949 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
3952 ret
= elf_add_dt_needed_tag (info
, soname
, add_needed
);
3954 goto error_free_vers
;
3956 BFD_ASSERT (ret
== 0);
3961 /* Now that all the symbols from this input file are created, handle
3962 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
3963 if (nondeflt_vers
!= NULL
)
3965 bfd_size_type cnt
, symidx
;
3967 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
3969 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
3970 char *shortname
, *p
;
3972 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
3974 || (h
->root
.type
!= bfd_link_hash_defined
3975 && h
->root
.type
!= bfd_link_hash_defweak
))
3978 amt
= p
- h
->root
.root
.string
;
3979 shortname
= bfd_malloc (amt
+ 1);
3980 memcpy (shortname
, h
->root
.root
.string
, amt
);
3981 shortname
[amt
] = '\0';
3983 hi
= (struct elf_link_hash_entry
*)
3984 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
3985 FALSE
, FALSE
, FALSE
);
3987 && hi
->root
.type
== h
->root
.type
3988 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
3989 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
3991 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
3992 hi
->root
.type
= bfd_link_hash_indirect
;
3993 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
3994 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
3995 sym_hash
= elf_sym_hashes (abfd
);
3997 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
3998 if (sym_hash
[symidx
] == hi
)
4000 sym_hash
[symidx
] = h
;
4006 free (nondeflt_vers
);
4007 nondeflt_vers
= NULL
;
4010 if (extversym
!= NULL
)
4016 if (isymbuf
!= NULL
)
4020 /* Now set the weakdefs field correctly for all the weak defined
4021 symbols we found. The only way to do this is to search all the
4022 symbols. Since we only need the information for non functions in
4023 dynamic objects, that's the only time we actually put anything on
4024 the list WEAKS. We need this information so that if a regular
4025 object refers to a symbol defined weakly in a dynamic object, the
4026 real symbol in the dynamic object is also put in the dynamic
4027 symbols; we also must arrange for both symbols to point to the
4028 same memory location. We could handle the general case of symbol
4029 aliasing, but a general symbol alias can only be generated in
4030 assembler code, handling it correctly would be very time
4031 consuming, and other ELF linkers don't handle general aliasing
4035 struct elf_link_hash_entry
**hpp
;
4036 struct elf_link_hash_entry
**hppend
;
4037 struct elf_link_hash_entry
**sorted_sym_hash
;
4038 struct elf_link_hash_entry
*h
;
4041 /* Since we have to search the whole symbol list for each weak
4042 defined symbol, search time for N weak defined symbols will be
4043 O(N^2). Binary search will cut it down to O(NlogN). */
4044 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4045 sorted_sym_hash
= bfd_malloc (amt
);
4046 if (sorted_sym_hash
== NULL
)
4048 sym_hash
= sorted_sym_hash
;
4049 hpp
= elf_sym_hashes (abfd
);
4050 hppend
= hpp
+ extsymcount
;
4052 for (; hpp
< hppend
; hpp
++)
4056 && h
->root
.type
== bfd_link_hash_defined
4057 && h
->type
!= STT_FUNC
)
4065 qsort (sorted_sym_hash
, sym_count
,
4066 sizeof (struct elf_link_hash_entry
*),
4069 while (weaks
!= NULL
)
4071 struct elf_link_hash_entry
*hlook
;
4078 weaks
= hlook
->u
.weakdef
;
4079 hlook
->u
.weakdef
= NULL
;
4081 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4082 || hlook
->root
.type
== bfd_link_hash_defweak
4083 || hlook
->root
.type
== bfd_link_hash_common
4084 || hlook
->root
.type
== bfd_link_hash_indirect
);
4085 slook
= hlook
->root
.u
.def
.section
;
4086 vlook
= hlook
->root
.u
.def
.value
;
4093 bfd_signed_vma vdiff
;
4095 h
= sorted_sym_hash
[idx
];
4096 vdiff
= vlook
- h
->root
.u
.def
.value
;
4103 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4116 /* We didn't find a value/section match. */
4120 for (i
= ilook
; i
< sym_count
; i
++)
4122 h
= sorted_sym_hash
[i
];
4124 /* Stop if value or section doesn't match. */
4125 if (h
->root
.u
.def
.value
!= vlook
4126 || h
->root
.u
.def
.section
!= slook
)
4128 else if (h
!= hlook
)
4130 hlook
->u
.weakdef
= h
;
4132 /* If the weak definition is in the list of dynamic
4133 symbols, make sure the real definition is put
4135 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4137 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4141 /* If the real definition is in the list of dynamic
4142 symbols, make sure the weak definition is put
4143 there as well. If we don't do this, then the
4144 dynamic loader might not merge the entries for the
4145 real definition and the weak definition. */
4146 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4148 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4156 free (sorted_sym_hash
);
4159 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4160 if (check_directives
)
4161 check_directives (abfd
, info
);
4163 /* If this object is the same format as the output object, and it is
4164 not a shared library, then let the backend look through the
4167 This is required to build global offset table entries and to
4168 arrange for dynamic relocs. It is not required for the
4169 particular common case of linking non PIC code, even when linking
4170 against shared libraries, but unfortunately there is no way of
4171 knowing whether an object file has been compiled PIC or not.
4172 Looking through the relocs is not particularly time consuming.
4173 The problem is that we must either (1) keep the relocs in memory,
4174 which causes the linker to require additional runtime memory or
4175 (2) read the relocs twice from the input file, which wastes time.
4176 This would be a good case for using mmap.
4178 I have no idea how to handle linking PIC code into a file of a
4179 different format. It probably can't be done. */
4180 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4182 && is_elf_hash_table (hash_table
)
4183 && hash_table
->root
.creator
== abfd
->xvec
4184 && check_relocs
!= NULL
)
4188 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4190 Elf_Internal_Rela
*internal_relocs
;
4193 if ((o
->flags
& SEC_RELOC
) == 0
4194 || o
->reloc_count
== 0
4195 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4196 && (o
->flags
& SEC_DEBUGGING
) != 0)
4197 || bfd_is_abs_section (o
->output_section
))
4200 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4202 if (internal_relocs
== NULL
)
4205 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4207 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4208 free (internal_relocs
);
4215 /* If this is a non-traditional link, try to optimize the handling
4216 of the .stab/.stabstr sections. */
4218 && ! info
->traditional_format
4219 && is_elf_hash_table (hash_table
)
4220 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4224 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4225 if (stabstr
!= NULL
)
4227 bfd_size_type string_offset
= 0;
4230 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4231 if (strncmp (".stab", stab
->name
, 5) == 0
4232 && (!stab
->name
[5] ||
4233 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4234 && (stab
->flags
& SEC_MERGE
) == 0
4235 && !bfd_is_abs_section (stab
->output_section
))
4237 struct bfd_elf_section_data
*secdata
;
4239 secdata
= elf_section_data (stab
);
4240 if (! _bfd_link_section_stabs (abfd
,
4241 &hash_table
->stab_info
,
4246 if (secdata
->sec_info
)
4247 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4252 if (is_elf_hash_table (hash_table
))
4254 /* Add this bfd to the loaded list. */
4255 struct elf_link_loaded_list
*n
;
4257 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4261 n
->next
= hash_table
->loaded
;
4262 hash_table
->loaded
= n
;
4268 if (nondeflt_vers
!= NULL
)
4269 free (nondeflt_vers
);
4270 if (extversym
!= NULL
)
4273 if (isymbuf
!= NULL
)
4279 /* Return the linker hash table entry of a symbol that might be
4280 satisfied by an archive symbol. Return -1 on error. */
4282 struct elf_link_hash_entry
*
4283 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4284 struct bfd_link_info
*info
,
4287 struct elf_link_hash_entry
*h
;
4291 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4295 /* If this is a default version (the name contains @@), look up the
4296 symbol again with only one `@' as well as without the version.
4297 The effect is that references to the symbol with and without the
4298 version will be matched by the default symbol in the archive. */
4300 p
= strchr (name
, ELF_VER_CHR
);
4301 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4304 /* First check with only one `@'. */
4305 len
= strlen (name
);
4306 copy
= bfd_alloc (abfd
, len
);
4308 return (struct elf_link_hash_entry
*) 0 - 1;
4310 first
= p
- name
+ 1;
4311 memcpy (copy
, name
, first
);
4312 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4314 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4317 /* We also need to check references to the symbol without the
4319 copy
[first
- 1] = '\0';
4320 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4321 FALSE
, FALSE
, FALSE
);
4324 bfd_release (abfd
, copy
);
4328 /* Add symbols from an ELF archive file to the linker hash table. We
4329 don't use _bfd_generic_link_add_archive_symbols because of a
4330 problem which arises on UnixWare. The UnixWare libc.so is an
4331 archive which includes an entry libc.so.1 which defines a bunch of
4332 symbols. The libc.so archive also includes a number of other
4333 object files, which also define symbols, some of which are the same
4334 as those defined in libc.so.1. Correct linking requires that we
4335 consider each object file in turn, and include it if it defines any
4336 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4337 this; it looks through the list of undefined symbols, and includes
4338 any object file which defines them. When this algorithm is used on
4339 UnixWare, it winds up pulling in libc.so.1 early and defining a
4340 bunch of symbols. This means that some of the other objects in the
4341 archive are not included in the link, which is incorrect since they
4342 precede libc.so.1 in the archive.
4344 Fortunately, ELF archive handling is simpler than that done by
4345 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4346 oddities. In ELF, if we find a symbol in the archive map, and the
4347 symbol is currently undefined, we know that we must pull in that
4350 Unfortunately, we do have to make multiple passes over the symbol
4351 table until nothing further is resolved. */
4354 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4357 bfd_boolean
*defined
= NULL
;
4358 bfd_boolean
*included
= NULL
;
4362 const struct elf_backend_data
*bed
;
4363 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4364 (bfd
*, struct bfd_link_info
*, const char *);
4366 if (! bfd_has_map (abfd
))
4368 /* An empty archive is a special case. */
4369 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4371 bfd_set_error (bfd_error_no_armap
);
4375 /* Keep track of all symbols we know to be already defined, and all
4376 files we know to be already included. This is to speed up the
4377 second and subsequent passes. */
4378 c
= bfd_ardata (abfd
)->symdef_count
;
4382 amt
*= sizeof (bfd_boolean
);
4383 defined
= bfd_zmalloc (amt
);
4384 included
= bfd_zmalloc (amt
);
4385 if (defined
== NULL
|| included
== NULL
)
4388 symdefs
= bfd_ardata (abfd
)->symdefs
;
4389 bed
= get_elf_backend_data (abfd
);
4390 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4403 symdefend
= symdef
+ c
;
4404 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4406 struct elf_link_hash_entry
*h
;
4408 struct bfd_link_hash_entry
*undefs_tail
;
4411 if (defined
[i
] || included
[i
])
4413 if (symdef
->file_offset
== last
)
4419 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4420 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4426 if (h
->root
.type
== bfd_link_hash_common
)
4428 /* We currently have a common symbol. The archive map contains
4429 a reference to this symbol, so we may want to include it. We
4430 only want to include it however, if this archive element
4431 contains a definition of the symbol, not just another common
4434 Unfortunately some archivers (including GNU ar) will put
4435 declarations of common symbols into their archive maps, as
4436 well as real definitions, so we cannot just go by the archive
4437 map alone. Instead we must read in the element's symbol
4438 table and check that to see what kind of symbol definition
4440 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4443 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4445 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4450 /* We need to include this archive member. */
4451 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4452 if (element
== NULL
)
4455 if (! bfd_check_format (element
, bfd_object
))
4458 /* Doublecheck that we have not included this object
4459 already--it should be impossible, but there may be
4460 something wrong with the archive. */
4461 if (element
->archive_pass
!= 0)
4463 bfd_set_error (bfd_error_bad_value
);
4466 element
->archive_pass
= 1;
4468 undefs_tail
= info
->hash
->undefs_tail
;
4470 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4473 if (! bfd_link_add_symbols (element
, info
))
4476 /* If there are any new undefined symbols, we need to make
4477 another pass through the archive in order to see whether
4478 they can be defined. FIXME: This isn't perfect, because
4479 common symbols wind up on undefs_tail and because an
4480 undefined symbol which is defined later on in this pass
4481 does not require another pass. This isn't a bug, but it
4482 does make the code less efficient than it could be. */
4483 if (undefs_tail
!= info
->hash
->undefs_tail
)
4486 /* Look backward to mark all symbols from this object file
4487 which we have already seen in this pass. */
4491 included
[mark
] = TRUE
;
4496 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4498 /* We mark subsequent symbols from this object file as we go
4499 on through the loop. */
4500 last
= symdef
->file_offset
;
4511 if (defined
!= NULL
)
4513 if (included
!= NULL
)
4518 /* Given an ELF BFD, add symbols to the global hash table as
4522 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4524 switch (bfd_get_format (abfd
))
4527 return elf_link_add_object_symbols (abfd
, info
);
4529 return elf_link_add_archive_symbols (abfd
, info
);
4531 bfd_set_error (bfd_error_wrong_format
);
4536 /* This function will be called though elf_link_hash_traverse to store
4537 all hash value of the exported symbols in an array. */
4540 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4542 unsigned long **valuep
= data
;
4548 if (h
->root
.type
== bfd_link_hash_warning
)
4549 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4551 /* Ignore indirect symbols. These are added by the versioning code. */
4552 if (h
->dynindx
== -1)
4555 name
= h
->root
.root
.string
;
4556 p
= strchr (name
, ELF_VER_CHR
);
4559 alc
= bfd_malloc (p
- name
+ 1);
4560 memcpy (alc
, name
, p
- name
);
4561 alc
[p
- name
] = '\0';
4565 /* Compute the hash value. */
4566 ha
= bfd_elf_hash (name
);
4568 /* Store the found hash value in the array given as the argument. */
4571 /* And store it in the struct so that we can put it in the hash table
4573 h
->u
.elf_hash_value
= ha
;
4581 /* Array used to determine the number of hash table buckets to use
4582 based on the number of symbols there are. If there are fewer than
4583 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4584 fewer than 37 we use 17 buckets, and so forth. We never use more
4585 than 32771 buckets. */
4587 static const size_t elf_buckets
[] =
4589 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4593 /* Compute bucket count for hashing table. We do not use a static set
4594 of possible tables sizes anymore. Instead we determine for all
4595 possible reasonable sizes of the table the outcome (i.e., the
4596 number of collisions etc) and choose the best solution. The
4597 weighting functions are not too simple to allow the table to grow
4598 without bounds. Instead one of the weighting factors is the size.
4599 Therefore the result is always a good payoff between few collisions
4600 (= short chain lengths) and table size. */
4602 compute_bucket_count (struct bfd_link_info
*info
)
4604 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4605 size_t best_size
= 0;
4606 unsigned long int *hashcodes
;
4607 unsigned long int *hashcodesp
;
4608 unsigned long int i
;
4611 /* Compute the hash values for all exported symbols. At the same
4612 time store the values in an array so that we could use them for
4615 amt
*= sizeof (unsigned long int);
4616 hashcodes
= bfd_malloc (amt
);
4617 if (hashcodes
== NULL
)
4619 hashcodesp
= hashcodes
;
4621 /* Put all hash values in HASHCODES. */
4622 elf_link_hash_traverse (elf_hash_table (info
),
4623 elf_collect_hash_codes
, &hashcodesp
);
4625 /* We have a problem here. The following code to optimize the table
4626 size requires an integer type with more the 32 bits. If
4627 BFD_HOST_U_64_BIT is set we know about such a type. */
4628 #ifdef BFD_HOST_U_64_BIT
4631 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4634 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4635 unsigned long int *counts
;
4636 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4637 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4639 /* Possible optimization parameters: if we have NSYMS symbols we say
4640 that the hashing table must at least have NSYMS/4 and at most
4642 minsize
= nsyms
/ 4;
4645 best_size
= maxsize
= nsyms
* 2;
4647 /* Create array where we count the collisions in. We must use bfd_malloc
4648 since the size could be large. */
4650 amt
*= sizeof (unsigned long int);
4651 counts
= bfd_malloc (amt
);
4658 /* Compute the "optimal" size for the hash table. The criteria is a
4659 minimal chain length. The minor criteria is (of course) the size
4661 for (i
= minsize
; i
< maxsize
; ++i
)
4663 /* Walk through the array of hashcodes and count the collisions. */
4664 BFD_HOST_U_64_BIT max
;
4665 unsigned long int j
;
4666 unsigned long int fact
;
4668 memset (counts
, '\0', i
* sizeof (unsigned long int));
4670 /* Determine how often each hash bucket is used. */
4671 for (j
= 0; j
< nsyms
; ++j
)
4672 ++counts
[hashcodes
[j
] % i
];
4674 /* For the weight function we need some information about the
4675 pagesize on the target. This is information need not be 100%
4676 accurate. Since this information is not available (so far) we
4677 define it here to a reasonable default value. If it is crucial
4678 to have a better value some day simply define this value. */
4679 # ifndef BFD_TARGET_PAGESIZE
4680 # define BFD_TARGET_PAGESIZE (4096)
4683 /* We in any case need 2 + NSYMS entries for the size values and
4685 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4688 /* Variant 1: optimize for short chains. We add the squares
4689 of all the chain lengths (which favors many small chain
4690 over a few long chains). */
4691 for (j
= 0; j
< i
; ++j
)
4692 max
+= counts
[j
] * counts
[j
];
4694 /* This adds penalties for the overall size of the table. */
4695 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4698 /* Variant 2: Optimize a lot more for small table. Here we
4699 also add squares of the size but we also add penalties for
4700 empty slots (the +1 term). */
4701 for (j
= 0; j
< i
; ++j
)
4702 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4704 /* The overall size of the table is considered, but not as
4705 strong as in variant 1, where it is squared. */
4706 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4710 /* Compare with current best results. */
4711 if (max
< best_chlen
)
4721 #endif /* defined (BFD_HOST_U_64_BIT) */
4723 /* This is the fallback solution if no 64bit type is available or if we
4724 are not supposed to spend much time on optimizations. We select the
4725 bucket count using a fixed set of numbers. */
4726 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4728 best_size
= elf_buckets
[i
];
4729 if (dynsymcount
< elf_buckets
[i
+ 1])
4734 /* Free the arrays we needed. */
4740 /* Set up the sizes and contents of the ELF dynamic sections. This is
4741 called by the ELF linker emulation before_allocation routine. We
4742 must set the sizes of the sections before the linker sets the
4743 addresses of the various sections. */
4746 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4749 const char *filter_shlib
,
4750 const char * const *auxiliary_filters
,
4751 struct bfd_link_info
*info
,
4752 asection
**sinterpptr
,
4753 struct bfd_elf_version_tree
*verdefs
)
4755 bfd_size_type soname_indx
;
4757 const struct elf_backend_data
*bed
;
4758 struct elf_assign_sym_version_info asvinfo
;
4762 soname_indx
= (bfd_size_type
) -1;
4764 if (!is_elf_hash_table (info
->hash
))
4767 elf_tdata (output_bfd
)->relro
= info
->relro
;
4768 if (info
->execstack
)
4769 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4770 else if (info
->noexecstack
)
4771 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4775 asection
*notesec
= NULL
;
4778 for (inputobj
= info
->input_bfds
;
4780 inputobj
= inputobj
->link_next
)
4784 if (inputobj
->flags
& DYNAMIC
)
4786 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4789 if (s
->flags
& SEC_CODE
)
4798 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4799 if (exec
&& info
->relocatable
4800 && notesec
->output_section
!= bfd_abs_section_ptr
)
4801 notesec
->output_section
->flags
|= SEC_CODE
;
4805 /* Any syms created from now on start with -1 in
4806 got.refcount/offset and plt.refcount/offset. */
4807 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4809 /* The backend may have to create some sections regardless of whether
4810 we're dynamic or not. */
4811 bed
= get_elf_backend_data (output_bfd
);
4812 if (bed
->elf_backend_always_size_sections
4813 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4816 dynobj
= elf_hash_table (info
)->dynobj
;
4818 /* If there were no dynamic objects in the link, there is nothing to
4823 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4826 if (elf_hash_table (info
)->dynamic_sections_created
)
4828 struct elf_info_failed eif
;
4829 struct elf_link_hash_entry
*h
;
4831 struct bfd_elf_version_tree
*t
;
4832 struct bfd_elf_version_expr
*d
;
4833 bfd_boolean all_defined
;
4835 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4836 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4840 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4842 if (soname_indx
== (bfd_size_type
) -1
4843 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
4849 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
4851 info
->flags
|= DF_SYMBOLIC
;
4858 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
4860 if (indx
== (bfd_size_type
) -1
4861 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
4864 if (info
->new_dtags
)
4866 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
4867 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
4872 if (filter_shlib
!= NULL
)
4876 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4877 filter_shlib
, TRUE
);
4878 if (indx
== (bfd_size_type
) -1
4879 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
4883 if (auxiliary_filters
!= NULL
)
4885 const char * const *p
;
4887 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
4891 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4893 if (indx
== (bfd_size_type
) -1
4894 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
4900 eif
.verdefs
= verdefs
;
4903 /* If we are supposed to export all symbols into the dynamic symbol
4904 table (this is not the normal case), then do so. */
4905 if (info
->export_dynamic
)
4907 elf_link_hash_traverse (elf_hash_table (info
),
4908 _bfd_elf_export_symbol
,
4914 /* Make all global versions with definition. */
4915 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4916 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4917 if (!d
->symver
&& d
->symbol
)
4919 const char *verstr
, *name
;
4920 size_t namelen
, verlen
, newlen
;
4922 struct elf_link_hash_entry
*newh
;
4925 namelen
= strlen (name
);
4927 verlen
= strlen (verstr
);
4928 newlen
= namelen
+ verlen
+ 3;
4930 newname
= bfd_malloc (newlen
);
4931 if (newname
== NULL
)
4933 memcpy (newname
, name
, namelen
);
4935 /* Check the hidden versioned definition. */
4936 p
= newname
+ namelen
;
4938 memcpy (p
, verstr
, verlen
+ 1);
4939 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4940 newname
, FALSE
, FALSE
,
4943 || (newh
->root
.type
!= bfd_link_hash_defined
4944 && newh
->root
.type
!= bfd_link_hash_defweak
))
4946 /* Check the default versioned definition. */
4948 memcpy (p
, verstr
, verlen
+ 1);
4949 newh
= elf_link_hash_lookup (elf_hash_table (info
),
4950 newname
, FALSE
, FALSE
,
4955 /* Mark this version if there is a definition and it is
4956 not defined in a shared object. */
4958 && !newh
->def_dynamic
4959 && (newh
->root
.type
== bfd_link_hash_defined
4960 || newh
->root
.type
== bfd_link_hash_defweak
))
4964 /* Attach all the symbols to their version information. */
4965 asvinfo
.output_bfd
= output_bfd
;
4966 asvinfo
.info
= info
;
4967 asvinfo
.verdefs
= verdefs
;
4968 asvinfo
.failed
= FALSE
;
4970 elf_link_hash_traverse (elf_hash_table (info
),
4971 _bfd_elf_link_assign_sym_version
,
4976 if (!info
->allow_undefined_version
)
4978 /* Check if all global versions have a definition. */
4980 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
4981 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
4982 if (!d
->symver
&& !d
->script
)
4984 (*_bfd_error_handler
)
4985 (_("%s: undefined version: %s"),
4986 d
->pattern
, t
->name
);
4987 all_defined
= FALSE
;
4992 bfd_set_error (bfd_error_bad_value
);
4997 /* Find all symbols which were defined in a dynamic object and make
4998 the backend pick a reasonable value for them. */
4999 elf_link_hash_traverse (elf_hash_table (info
),
5000 _bfd_elf_adjust_dynamic_symbol
,
5005 /* Add some entries to the .dynamic section. We fill in some of the
5006 values later, in bfd_elf_final_link, but we must add the entries
5007 now so that we know the final size of the .dynamic section. */
5009 /* If there are initialization and/or finalization functions to
5010 call then add the corresponding DT_INIT/DT_FINI entries. */
5011 h
= (info
->init_function
5012 ? elf_link_hash_lookup (elf_hash_table (info
),
5013 info
->init_function
, FALSE
,
5020 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5023 h
= (info
->fini_function
5024 ? elf_link_hash_lookup (elf_hash_table (info
),
5025 info
->fini_function
, FALSE
,
5032 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5036 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5038 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5039 if (! info
->executable
)
5044 for (sub
= info
->input_bfds
; sub
!= NULL
;
5045 sub
= sub
->link_next
)
5046 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5047 if (elf_section_data (o
)->this_hdr
.sh_type
5048 == SHT_PREINIT_ARRAY
)
5050 (*_bfd_error_handler
)
5051 (_("%B: .preinit_array section is not allowed in DSO"),
5056 bfd_set_error (bfd_error_nonrepresentable_section
);
5060 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5061 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5064 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5066 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5067 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5070 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5072 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5073 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5077 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5078 /* If .dynstr is excluded from the link, we don't want any of
5079 these tags. Strictly, we should be checking each section
5080 individually; This quick check covers for the case where
5081 someone does a /DISCARD/ : { *(*) }. */
5082 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5084 bfd_size_type strsize
;
5086 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5087 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5088 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5089 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5090 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5091 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5092 bed
->s
->sizeof_sym
))
5097 /* The backend must work out the sizes of all the other dynamic
5099 if (bed
->elf_backend_size_dynamic_sections
5100 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5103 if (elf_hash_table (info
)->dynamic_sections_created
)
5105 bfd_size_type dynsymcount
;
5107 size_t bucketcount
= 0;
5108 size_t hash_entry_size
;
5109 unsigned int dtagcount
;
5111 /* Set up the version definition section. */
5112 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5113 BFD_ASSERT (s
!= NULL
);
5115 /* We may have created additional version definitions if we are
5116 just linking a regular application. */
5117 verdefs
= asvinfo
.verdefs
;
5119 /* Skip anonymous version tag. */
5120 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5121 verdefs
= verdefs
->next
;
5123 if (verdefs
== NULL
&& !info
->create_default_symver
)
5124 _bfd_strip_section_from_output (info
, s
);
5129 struct bfd_elf_version_tree
*t
;
5131 Elf_Internal_Verdef def
;
5132 Elf_Internal_Verdaux defaux
;
5133 struct bfd_link_hash_entry
*bh
;
5134 struct elf_link_hash_entry
*h
;
5140 /* Make space for the base version. */
5141 size
+= sizeof (Elf_External_Verdef
);
5142 size
+= sizeof (Elf_External_Verdaux
);
5145 /* Make space for the default version. */
5146 if (info
->create_default_symver
)
5148 size
+= sizeof (Elf_External_Verdef
);
5152 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5154 struct bfd_elf_version_deps
*n
;
5156 size
+= sizeof (Elf_External_Verdef
);
5157 size
+= sizeof (Elf_External_Verdaux
);
5160 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5161 size
+= sizeof (Elf_External_Verdaux
);
5165 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5166 if (s
->contents
== NULL
&& s
->size
!= 0)
5169 /* Fill in the version definition section. */
5173 def
.vd_version
= VER_DEF_CURRENT
;
5174 def
.vd_flags
= VER_FLG_BASE
;
5177 if (info
->create_default_symver
)
5179 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5180 def
.vd_next
= sizeof (Elf_External_Verdef
);
5184 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5185 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5186 + sizeof (Elf_External_Verdaux
));
5189 if (soname_indx
!= (bfd_size_type
) -1)
5191 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5193 def
.vd_hash
= bfd_elf_hash (soname
);
5194 defaux
.vda_name
= soname_indx
;
5201 name
= basename (output_bfd
->filename
);
5202 def
.vd_hash
= bfd_elf_hash (name
);
5203 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5205 if (indx
== (bfd_size_type
) -1)
5207 defaux
.vda_name
= indx
;
5209 defaux
.vda_next
= 0;
5211 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5212 (Elf_External_Verdef
*) p
);
5213 p
+= sizeof (Elf_External_Verdef
);
5214 if (info
->create_default_symver
)
5216 /* Add a symbol representing this version. */
5218 if (! (_bfd_generic_link_add_one_symbol
5219 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5221 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5223 h
= (struct elf_link_hash_entry
*) bh
;
5226 h
->type
= STT_OBJECT
;
5227 h
->verinfo
.vertree
= NULL
;
5229 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5232 /* Create a duplicate of the base version with the same
5233 aux block, but different flags. */
5236 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5238 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5239 + sizeof (Elf_External_Verdaux
));
5242 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5243 (Elf_External_Verdef
*) p
);
5244 p
+= sizeof (Elf_External_Verdef
);
5246 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5247 (Elf_External_Verdaux
*) p
);
5248 p
+= sizeof (Elf_External_Verdaux
);
5250 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5253 struct bfd_elf_version_deps
*n
;
5256 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5259 /* Add a symbol representing this version. */
5261 if (! (_bfd_generic_link_add_one_symbol
5262 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5264 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5266 h
= (struct elf_link_hash_entry
*) bh
;
5269 h
->type
= STT_OBJECT
;
5270 h
->verinfo
.vertree
= t
;
5272 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5275 def
.vd_version
= VER_DEF_CURRENT
;
5277 if (t
->globals
.list
== NULL
5278 && t
->locals
.list
== NULL
5280 def
.vd_flags
|= VER_FLG_WEAK
;
5281 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5282 def
.vd_cnt
= cdeps
+ 1;
5283 def
.vd_hash
= bfd_elf_hash (t
->name
);
5284 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5286 if (t
->next
!= NULL
)
5287 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5288 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5290 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5291 (Elf_External_Verdef
*) p
);
5292 p
+= sizeof (Elf_External_Verdef
);
5294 defaux
.vda_name
= h
->dynstr_index
;
5295 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5297 defaux
.vda_next
= 0;
5298 if (t
->deps
!= NULL
)
5299 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5300 t
->name_indx
= defaux
.vda_name
;
5302 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5303 (Elf_External_Verdaux
*) p
);
5304 p
+= sizeof (Elf_External_Verdaux
);
5306 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5308 if (n
->version_needed
== NULL
)
5310 /* This can happen if there was an error in the
5312 defaux
.vda_name
= 0;
5316 defaux
.vda_name
= n
->version_needed
->name_indx
;
5317 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5320 if (n
->next
== NULL
)
5321 defaux
.vda_next
= 0;
5323 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5325 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5326 (Elf_External_Verdaux
*) p
);
5327 p
+= sizeof (Elf_External_Verdaux
);
5331 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5332 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5335 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5338 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5340 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5343 else if (info
->flags
& DF_BIND_NOW
)
5345 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5351 if (info
->executable
)
5352 info
->flags_1
&= ~ (DF_1_INITFIRST
5355 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5359 /* Work out the size of the version reference section. */
5361 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5362 BFD_ASSERT (s
!= NULL
);
5364 struct elf_find_verdep_info sinfo
;
5366 sinfo
.output_bfd
= output_bfd
;
5368 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5369 if (sinfo
.vers
== 0)
5371 sinfo
.failed
= FALSE
;
5373 elf_link_hash_traverse (elf_hash_table (info
),
5374 _bfd_elf_link_find_version_dependencies
,
5377 if (elf_tdata (output_bfd
)->verref
== NULL
)
5378 _bfd_strip_section_from_output (info
, s
);
5381 Elf_Internal_Verneed
*t
;
5386 /* Build the version definition section. */
5389 for (t
= elf_tdata (output_bfd
)->verref
;
5393 Elf_Internal_Vernaux
*a
;
5395 size
+= sizeof (Elf_External_Verneed
);
5397 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5398 size
+= sizeof (Elf_External_Vernaux
);
5402 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5403 if (s
->contents
== NULL
)
5407 for (t
= elf_tdata (output_bfd
)->verref
;
5412 Elf_Internal_Vernaux
*a
;
5416 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5419 t
->vn_version
= VER_NEED_CURRENT
;
5421 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5422 elf_dt_name (t
->vn_bfd
) != NULL
5423 ? elf_dt_name (t
->vn_bfd
)
5424 : basename (t
->vn_bfd
->filename
),
5426 if (indx
== (bfd_size_type
) -1)
5429 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5430 if (t
->vn_nextref
== NULL
)
5433 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5434 + caux
* sizeof (Elf_External_Vernaux
));
5436 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5437 (Elf_External_Verneed
*) p
);
5438 p
+= sizeof (Elf_External_Verneed
);
5440 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5442 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5443 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5444 a
->vna_nodename
, FALSE
);
5445 if (indx
== (bfd_size_type
) -1)
5448 if (a
->vna_nextptr
== NULL
)
5451 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5453 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5454 (Elf_External_Vernaux
*) p
);
5455 p
+= sizeof (Elf_External_Vernaux
);
5459 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5460 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5463 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5467 /* Assign dynsym indicies. In a shared library we generate a
5468 section symbol for each output section, which come first.
5469 Next come all of the back-end allocated local dynamic syms,
5470 followed by the rest of the global symbols. */
5472 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5474 /* Work out the size of the symbol version section. */
5475 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5476 BFD_ASSERT (s
!= NULL
);
5477 if (dynsymcount
== 0
5478 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
5479 && !info
->create_default_symver
))
5481 _bfd_strip_section_from_output (info
, s
);
5482 /* The DYNSYMCOUNT might have changed if we were going to
5483 output a dynamic symbol table entry for S. */
5484 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5488 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5489 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5490 if (s
->contents
== NULL
)
5493 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5497 /* Set the size of the .dynsym and .hash sections. We counted
5498 the number of dynamic symbols in elf_link_add_object_symbols.
5499 We will build the contents of .dynsym and .hash when we build
5500 the final symbol table, because until then we do not know the
5501 correct value to give the symbols. We built the .dynstr
5502 section as we went along in elf_link_add_object_symbols. */
5503 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5504 BFD_ASSERT (s
!= NULL
);
5505 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5506 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5507 if (s
->contents
== NULL
&& s
->size
!= 0)
5510 if (dynsymcount
!= 0)
5512 Elf_Internal_Sym isym
;
5514 /* The first entry in .dynsym is a dummy symbol. */
5521 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5524 /* Compute the size of the hashing table. As a side effect this
5525 computes the hash values for all the names we export. */
5526 bucketcount
= compute_bucket_count (info
);
5528 s
= bfd_get_section_by_name (dynobj
, ".hash");
5529 BFD_ASSERT (s
!= NULL
);
5530 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5531 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5532 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5533 if (s
->contents
== NULL
)
5536 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5537 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5538 s
->contents
+ hash_entry_size
);
5540 elf_hash_table (info
)->bucketcount
= bucketcount
;
5542 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5543 BFD_ASSERT (s
!= NULL
);
5545 elf_finalize_dynstr (output_bfd
, info
);
5547 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5549 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5550 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5557 /* Final phase of ELF linker. */
5559 /* A structure we use to avoid passing large numbers of arguments. */
5561 struct elf_final_link_info
5563 /* General link information. */
5564 struct bfd_link_info
*info
;
5567 /* Symbol string table. */
5568 struct bfd_strtab_hash
*symstrtab
;
5569 /* .dynsym section. */
5570 asection
*dynsym_sec
;
5571 /* .hash section. */
5573 /* symbol version section (.gnu.version). */
5574 asection
*symver_sec
;
5575 /* Buffer large enough to hold contents of any section. */
5577 /* Buffer large enough to hold external relocs of any section. */
5578 void *external_relocs
;
5579 /* Buffer large enough to hold internal relocs of any section. */
5580 Elf_Internal_Rela
*internal_relocs
;
5581 /* Buffer large enough to hold external local symbols of any input
5583 bfd_byte
*external_syms
;
5584 /* And a buffer for symbol section indices. */
5585 Elf_External_Sym_Shndx
*locsym_shndx
;
5586 /* Buffer large enough to hold internal local symbols of any input
5588 Elf_Internal_Sym
*internal_syms
;
5589 /* Array large enough to hold a symbol index for each local symbol
5590 of any input BFD. */
5592 /* Array large enough to hold a section pointer for each local
5593 symbol of any input BFD. */
5594 asection
**sections
;
5595 /* Buffer to hold swapped out symbols. */
5597 /* And one for symbol section indices. */
5598 Elf_External_Sym_Shndx
*symshndxbuf
;
5599 /* Number of swapped out symbols in buffer. */
5600 size_t symbuf_count
;
5601 /* Number of symbols which fit in symbuf. */
5603 /* And same for symshndxbuf. */
5604 size_t shndxbuf_size
;
5607 /* This struct is used to pass information to elf_link_output_extsym. */
5609 struct elf_outext_info
5612 bfd_boolean localsyms
;
5613 struct elf_final_link_info
*finfo
;
5616 /* When performing a relocatable link, the input relocations are
5617 preserved. But, if they reference global symbols, the indices
5618 referenced must be updated. Update all the relocations in
5619 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5622 elf_link_adjust_relocs (bfd
*abfd
,
5623 Elf_Internal_Shdr
*rel_hdr
,
5625 struct elf_link_hash_entry
**rel_hash
)
5628 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5630 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5631 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5632 bfd_vma r_type_mask
;
5635 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5637 swap_in
= bed
->s
->swap_reloc_in
;
5638 swap_out
= bed
->s
->swap_reloc_out
;
5640 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5642 swap_in
= bed
->s
->swap_reloca_in
;
5643 swap_out
= bed
->s
->swap_reloca_out
;
5648 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5651 if (bed
->s
->arch_size
== 32)
5658 r_type_mask
= 0xffffffff;
5662 erela
= rel_hdr
->contents
;
5663 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5665 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5668 if (*rel_hash
== NULL
)
5671 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5673 (*swap_in
) (abfd
, erela
, irela
);
5674 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5675 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5676 | (irela
[j
].r_info
& r_type_mask
));
5677 (*swap_out
) (abfd
, irela
, erela
);
5681 struct elf_link_sort_rela
5687 enum elf_reloc_type_class type
;
5688 /* We use this as an array of size int_rels_per_ext_rel. */
5689 Elf_Internal_Rela rela
[1];
5693 elf_link_sort_cmp1 (const void *A
, const void *B
)
5695 const struct elf_link_sort_rela
*a
= A
;
5696 const struct elf_link_sort_rela
*b
= B
;
5697 int relativea
, relativeb
;
5699 relativea
= a
->type
== reloc_class_relative
;
5700 relativeb
= b
->type
== reloc_class_relative
;
5702 if (relativea
< relativeb
)
5704 if (relativea
> relativeb
)
5706 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5708 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5710 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5712 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5718 elf_link_sort_cmp2 (const void *A
, const void *B
)
5720 const struct elf_link_sort_rela
*a
= A
;
5721 const struct elf_link_sort_rela
*b
= B
;
5724 if (a
->u
.offset
< b
->u
.offset
)
5726 if (a
->u
.offset
> b
->u
.offset
)
5728 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5729 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5734 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5736 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5742 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5745 bfd_size_type count
, size
;
5746 size_t i
, ret
, sort_elt
, ext_size
;
5747 bfd_byte
*sort
, *s_non_relative
, *p
;
5748 struct elf_link_sort_rela
*sq
;
5749 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5750 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5751 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5752 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5753 struct bfd_link_order
*lo
;
5756 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5757 if (reldyn
== NULL
|| reldyn
->size
== 0)
5759 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5760 if (reldyn
== NULL
|| reldyn
->size
== 0)
5762 ext_size
= bed
->s
->sizeof_rel
;
5763 swap_in
= bed
->s
->swap_reloc_in
;
5764 swap_out
= bed
->s
->swap_reloc_out
;
5768 ext_size
= bed
->s
->sizeof_rela
;
5769 swap_in
= bed
->s
->swap_reloca_in
;
5770 swap_out
= bed
->s
->swap_reloca_out
;
5772 count
= reldyn
->size
/ ext_size
;
5775 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5776 if (lo
->type
== bfd_indirect_link_order
)
5778 asection
*o
= lo
->u
.indirect
.section
;
5782 if (size
!= reldyn
->size
)
5785 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5786 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5787 sort
= bfd_zmalloc (sort_elt
* count
);
5790 (*info
->callbacks
->warning
)
5791 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5795 if (bed
->s
->arch_size
== 32)
5796 r_sym_mask
= ~(bfd_vma
) 0xff;
5798 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5800 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5801 if (lo
->type
== bfd_indirect_link_order
)
5803 bfd_byte
*erel
, *erelend
;
5804 asection
*o
= lo
->u
.indirect
.section
;
5806 if (o
->contents
== NULL
&& o
->size
!= 0)
5808 /* This is a reloc section that is being handled as a normal
5809 section. See bfd_section_from_shdr. We can't combine
5810 relocs in this case. */
5815 erelend
= o
->contents
+ o
->size
;
5816 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5817 while (erel
< erelend
)
5819 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5820 (*swap_in
) (abfd
, erel
, s
->rela
);
5821 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5822 s
->u
.sym_mask
= r_sym_mask
;
5828 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5830 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5832 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5833 if (s
->type
!= reloc_class_relative
)
5839 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5840 for (; i
< count
; i
++, p
+= sort_elt
)
5842 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5843 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5845 sp
->u
.offset
= sq
->rela
->r_offset
;
5848 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
5850 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5851 if (lo
->type
== bfd_indirect_link_order
)
5853 bfd_byte
*erel
, *erelend
;
5854 asection
*o
= lo
->u
.indirect
.section
;
5857 erelend
= o
->contents
+ o
->size
;
5858 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5859 while (erel
< erelend
)
5861 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5862 (*swap_out
) (abfd
, s
->rela
, erel
);
5873 /* Flush the output symbols to the file. */
5876 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
5877 const struct elf_backend_data
*bed
)
5879 if (finfo
->symbuf_count
> 0)
5881 Elf_Internal_Shdr
*hdr
;
5885 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
5886 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
5887 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5888 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
5889 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
5892 hdr
->sh_size
+= amt
;
5893 finfo
->symbuf_count
= 0;
5899 /* Add a symbol to the output symbol table. */
5902 elf_link_output_sym (struct elf_final_link_info
*finfo
,
5904 Elf_Internal_Sym
*elfsym
,
5905 asection
*input_sec
,
5906 struct elf_link_hash_entry
*h
)
5909 Elf_External_Sym_Shndx
*destshndx
;
5910 bfd_boolean (*output_symbol_hook
)
5911 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
5912 struct elf_link_hash_entry
*);
5913 const struct elf_backend_data
*bed
;
5915 bed
= get_elf_backend_data (finfo
->output_bfd
);
5916 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
5917 if (output_symbol_hook
!= NULL
)
5919 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
5923 if (name
== NULL
|| *name
== '\0')
5924 elfsym
->st_name
= 0;
5925 else if (input_sec
->flags
& SEC_EXCLUDE
)
5926 elfsym
->st_name
= 0;
5929 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
5931 if (elfsym
->st_name
== (unsigned long) -1)
5935 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
5937 if (! elf_link_flush_output_syms (finfo
, bed
))
5941 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
5942 destshndx
= finfo
->symshndxbuf
;
5943 if (destshndx
!= NULL
)
5945 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
5949 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
5950 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
5951 if (destshndx
== NULL
)
5953 memset ((char *) destshndx
+ amt
, 0, amt
);
5954 finfo
->shndxbuf_size
*= 2;
5956 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
5959 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
5960 finfo
->symbuf_count
+= 1;
5961 bfd_get_symcount (finfo
->output_bfd
) += 1;
5966 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
5967 allowing an unsatisfied unversioned symbol in the DSO to match a
5968 versioned symbol that would normally require an explicit version.
5969 We also handle the case that a DSO references a hidden symbol
5970 which may be satisfied by a versioned symbol in another DSO. */
5973 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
5974 const struct elf_backend_data
*bed
,
5975 struct elf_link_hash_entry
*h
)
5978 struct elf_link_loaded_list
*loaded
;
5980 if (!is_elf_hash_table (info
->hash
))
5983 switch (h
->root
.type
)
5989 case bfd_link_hash_undefined
:
5990 case bfd_link_hash_undefweak
:
5991 abfd
= h
->root
.u
.undef
.abfd
;
5992 if ((abfd
->flags
& DYNAMIC
) == 0
5993 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
5997 case bfd_link_hash_defined
:
5998 case bfd_link_hash_defweak
:
5999 abfd
= h
->root
.u
.def
.section
->owner
;
6002 case bfd_link_hash_common
:
6003 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6006 BFD_ASSERT (abfd
!= NULL
);
6008 for (loaded
= elf_hash_table (info
)->loaded
;
6010 loaded
= loaded
->next
)
6013 Elf_Internal_Shdr
*hdr
;
6014 bfd_size_type symcount
;
6015 bfd_size_type extsymcount
;
6016 bfd_size_type extsymoff
;
6017 Elf_Internal_Shdr
*versymhdr
;
6018 Elf_Internal_Sym
*isym
;
6019 Elf_Internal_Sym
*isymend
;
6020 Elf_Internal_Sym
*isymbuf
;
6021 Elf_External_Versym
*ever
;
6022 Elf_External_Versym
*extversym
;
6024 input
= loaded
->abfd
;
6026 /* We check each DSO for a possible hidden versioned definition. */
6028 || (input
->flags
& DYNAMIC
) == 0
6029 || elf_dynversym (input
) == 0)
6032 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6034 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6035 if (elf_bad_symtab (input
))
6037 extsymcount
= symcount
;
6042 extsymcount
= symcount
- hdr
->sh_info
;
6043 extsymoff
= hdr
->sh_info
;
6046 if (extsymcount
== 0)
6049 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6051 if (isymbuf
== NULL
)
6054 /* Read in any version definitions. */
6055 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6056 extversym
= bfd_malloc (versymhdr
->sh_size
);
6057 if (extversym
== NULL
)
6060 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6061 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6062 != versymhdr
->sh_size
))
6070 ever
= extversym
+ extsymoff
;
6071 isymend
= isymbuf
+ extsymcount
;
6072 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6075 Elf_Internal_Versym iver
;
6076 unsigned short version_index
;
6078 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6079 || isym
->st_shndx
== SHN_UNDEF
)
6082 name
= bfd_elf_string_from_elf_section (input
,
6085 if (strcmp (name
, h
->root
.root
.string
) != 0)
6088 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6090 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6092 /* If we have a non-hidden versioned sym, then it should
6093 have provided a definition for the undefined sym. */
6097 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6098 if (version_index
== 1 || version_index
== 2)
6100 /* This is the base or first version. We can use it. */
6114 /* Add an external symbol to the symbol table. This is called from
6115 the hash table traversal routine. When generating a shared object,
6116 we go through the symbol table twice. The first time we output
6117 anything that might have been forced to local scope in a version
6118 script. The second time we output the symbols that are still
6122 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6124 struct elf_outext_info
*eoinfo
= data
;
6125 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6127 Elf_Internal_Sym sym
;
6128 asection
*input_sec
;
6129 const struct elf_backend_data
*bed
;
6131 if (h
->root
.type
== bfd_link_hash_warning
)
6133 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6134 if (h
->root
.type
== bfd_link_hash_new
)
6138 /* Decide whether to output this symbol in this pass. */
6139 if (eoinfo
->localsyms
)
6141 if (!h
->forced_local
)
6146 if (h
->forced_local
)
6150 bed
= get_elf_backend_data (finfo
->output_bfd
);
6152 /* If we have an undefined symbol reference here then it must have
6153 come from a shared library that is being linked in. (Undefined
6154 references in regular files have already been handled). If we
6155 are reporting errors for this situation then do so now. */
6156 if (h
->root
.type
== bfd_link_hash_undefined
6159 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6160 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6162 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6163 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6164 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6166 eoinfo
->failed
= TRUE
;
6171 /* We should also warn if a forced local symbol is referenced from
6172 shared libraries. */
6173 if (! finfo
->info
->relocatable
6174 && (! finfo
->info
->shared
)
6179 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6181 (*_bfd_error_handler
)
6182 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6183 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6184 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6186 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6187 ? "hidden" : "local",
6188 h
->root
.root
.string
);
6189 eoinfo
->failed
= TRUE
;
6193 /* We don't want to output symbols that have never been mentioned by
6194 a regular file, or that we have been told to strip. However, if
6195 h->indx is set to -2, the symbol is used by a reloc and we must
6199 else if ((h
->def_dynamic
6204 else if (finfo
->info
->strip
== strip_all
)
6206 else if (finfo
->info
->strip
== strip_some
6207 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6208 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6210 else if (finfo
->info
->strip_discarded
6211 && (h
->root
.type
== bfd_link_hash_defined
6212 || h
->root
.type
== bfd_link_hash_defweak
)
6213 && elf_discarded_section (h
->root
.u
.def
.section
))
6218 /* If we're stripping it, and it's not a dynamic symbol, there's
6219 nothing else to do unless it is a forced local symbol. */
6222 && !h
->forced_local
)
6226 sym
.st_size
= h
->size
;
6227 sym
.st_other
= h
->other
;
6228 if (h
->forced_local
)
6229 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6230 else if (h
->root
.type
== bfd_link_hash_undefweak
6231 || h
->root
.type
== bfd_link_hash_defweak
)
6232 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6234 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6236 switch (h
->root
.type
)
6239 case bfd_link_hash_new
:
6240 case bfd_link_hash_warning
:
6244 case bfd_link_hash_undefined
:
6245 case bfd_link_hash_undefweak
:
6246 input_sec
= bfd_und_section_ptr
;
6247 sym
.st_shndx
= SHN_UNDEF
;
6250 case bfd_link_hash_defined
:
6251 case bfd_link_hash_defweak
:
6253 input_sec
= h
->root
.u
.def
.section
;
6254 if (input_sec
->output_section
!= NULL
)
6257 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6258 input_sec
->output_section
);
6259 if (sym
.st_shndx
== SHN_BAD
)
6261 (*_bfd_error_handler
)
6262 (_("%B: could not find output section %A for input section %A"),
6263 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6264 eoinfo
->failed
= TRUE
;
6268 /* ELF symbols in relocatable files are section relative,
6269 but in nonrelocatable files they are virtual
6271 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6272 if (! finfo
->info
->relocatable
)
6274 sym
.st_value
+= input_sec
->output_section
->vma
;
6275 if (h
->type
== STT_TLS
)
6277 /* STT_TLS symbols are relative to PT_TLS segment
6279 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6280 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6286 BFD_ASSERT (input_sec
->owner
== NULL
6287 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6288 sym
.st_shndx
= SHN_UNDEF
;
6289 input_sec
= bfd_und_section_ptr
;
6294 case bfd_link_hash_common
:
6295 input_sec
= h
->root
.u
.c
.p
->section
;
6296 sym
.st_shndx
= SHN_COMMON
;
6297 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6300 case bfd_link_hash_indirect
:
6301 /* These symbols are created by symbol versioning. They point
6302 to the decorated version of the name. For example, if the
6303 symbol foo@@GNU_1.2 is the default, which should be used when
6304 foo is used with no version, then we add an indirect symbol
6305 foo which points to foo@@GNU_1.2. We ignore these symbols,
6306 since the indirected symbol is already in the hash table. */
6310 /* Give the processor backend a chance to tweak the symbol value,
6311 and also to finish up anything that needs to be done for this
6312 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6313 forced local syms when non-shared is due to a historical quirk. */
6314 if ((h
->dynindx
!= -1
6316 && ((finfo
->info
->shared
6317 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6318 || h
->root
.type
!= bfd_link_hash_undefweak
))
6319 || !h
->forced_local
)
6320 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6322 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6323 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6325 eoinfo
->failed
= TRUE
;
6330 /* If we are marking the symbol as undefined, and there are no
6331 non-weak references to this symbol from a regular object, then
6332 mark the symbol as weak undefined; if there are non-weak
6333 references, mark the symbol as strong. We can't do this earlier,
6334 because it might not be marked as undefined until the
6335 finish_dynamic_symbol routine gets through with it. */
6336 if (sym
.st_shndx
== SHN_UNDEF
6338 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6339 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6343 if (h
->ref_regular_nonweak
)
6344 bindtype
= STB_GLOBAL
;
6346 bindtype
= STB_WEAK
;
6347 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6350 /* If a non-weak symbol with non-default visibility is not defined
6351 locally, it is a fatal error. */
6352 if (! finfo
->info
->relocatable
6353 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6354 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6355 && h
->root
.type
== bfd_link_hash_undefined
6358 (*_bfd_error_handler
)
6359 (_("%B: %s symbol `%s' isn't defined"),
6361 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6363 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6364 ? "internal" : "hidden",
6365 h
->root
.root
.string
);
6366 eoinfo
->failed
= TRUE
;
6370 /* If this symbol should be put in the .dynsym section, then put it
6371 there now. We already know the symbol index. We also fill in
6372 the entry in the .hash section. */
6373 if (h
->dynindx
!= -1
6374 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6378 size_t hash_entry_size
;
6379 bfd_byte
*bucketpos
;
6383 sym
.st_name
= h
->dynstr_index
;
6384 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6385 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6387 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6388 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6390 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6391 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6392 + (bucket
+ 2) * hash_entry_size
);
6393 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6394 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6395 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6396 ((bfd_byte
*) finfo
->hash_sec
->contents
6397 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6399 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6401 Elf_Internal_Versym iversym
;
6402 Elf_External_Versym
*eversym
;
6404 if (!h
->def_regular
)
6406 if (h
->verinfo
.verdef
== NULL
)
6407 iversym
.vs_vers
= 0;
6409 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6413 if (h
->verinfo
.vertree
== NULL
)
6414 iversym
.vs_vers
= 1;
6416 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6417 if (finfo
->info
->create_default_symver
)
6422 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6424 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6425 eversym
+= h
->dynindx
;
6426 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6430 /* If we're stripping it, then it was just a dynamic symbol, and
6431 there's nothing else to do. */
6432 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6435 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6437 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6439 eoinfo
->failed
= TRUE
;
6446 /* Return TRUE if special handling is done for relocs in SEC against
6447 symbols defined in discarded sections. */
6450 elf_section_ignore_discarded_relocs (asection
*sec
)
6452 const struct elf_backend_data
*bed
;
6454 switch (sec
->sec_info_type
)
6456 case ELF_INFO_TYPE_STABS
:
6457 case ELF_INFO_TYPE_EH_FRAME
:
6463 bed
= get_elf_backend_data (sec
->owner
);
6464 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6465 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6471 enum action_discarded
6477 /* Return a mask saying how ld should treat relocations in SEC against
6478 symbols defined in discarded sections. If this function returns
6479 COMPLAIN set, ld will issue a warning message. If this function
6480 returns PRETEND set, and the discarded section was link-once and the
6481 same size as the kept link-once section, ld will pretend that the
6482 symbol was actually defined in the kept section. Otherwise ld will
6483 zero the reloc (at least that is the intent, but some cooperation by
6484 the target dependent code is needed, particularly for REL targets). */
6487 elf_action_discarded (asection
*sec
)
6489 if (sec
->flags
& SEC_DEBUGGING
)
6492 if (strcmp (".eh_frame", sec
->name
) == 0)
6495 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6498 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6501 if (strcmp (".fixup", sec
->name
) == 0)
6504 return COMPLAIN
| PRETEND
;
6507 /* Find a match between a section and a member of a section group. */
6510 match_group_member (asection
*sec
, asection
*group
)
6512 asection
*first
= elf_next_in_group (group
);
6513 asection
*s
= first
;
6517 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6527 /* Link an input file into the linker output file. This function
6528 handles all the sections and relocations of the input file at once.
6529 This is so that we only have to read the local symbols once, and
6530 don't have to keep them in memory. */
6533 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6535 bfd_boolean (*relocate_section
)
6536 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6537 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6539 Elf_Internal_Shdr
*symtab_hdr
;
6542 Elf_Internal_Sym
*isymbuf
;
6543 Elf_Internal_Sym
*isym
;
6544 Elf_Internal_Sym
*isymend
;
6546 asection
**ppsection
;
6548 const struct elf_backend_data
*bed
;
6549 bfd_boolean emit_relocs
;
6550 struct elf_link_hash_entry
**sym_hashes
;
6552 output_bfd
= finfo
->output_bfd
;
6553 bed
= get_elf_backend_data (output_bfd
);
6554 relocate_section
= bed
->elf_backend_relocate_section
;
6556 /* If this is a dynamic object, we don't want to do anything here:
6557 we don't want the local symbols, and we don't want the section
6559 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6562 emit_relocs
= (finfo
->info
->relocatable
6563 || finfo
->info
->emitrelocations
6564 || bed
->elf_backend_emit_relocs
);
6566 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6567 if (elf_bad_symtab (input_bfd
))
6569 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6574 locsymcount
= symtab_hdr
->sh_info
;
6575 extsymoff
= symtab_hdr
->sh_info
;
6578 /* Read the local symbols. */
6579 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6580 if (isymbuf
== NULL
&& locsymcount
!= 0)
6582 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6583 finfo
->internal_syms
,
6584 finfo
->external_syms
,
6585 finfo
->locsym_shndx
);
6586 if (isymbuf
== NULL
)
6590 /* Find local symbol sections and adjust values of symbols in
6591 SEC_MERGE sections. Write out those local symbols we know are
6592 going into the output file. */
6593 isymend
= isymbuf
+ locsymcount
;
6594 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6596 isym
++, pindex
++, ppsection
++)
6600 Elf_Internal_Sym osym
;
6604 if (elf_bad_symtab (input_bfd
))
6606 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6613 if (isym
->st_shndx
== SHN_UNDEF
)
6614 isec
= bfd_und_section_ptr
;
6615 else if (isym
->st_shndx
< SHN_LORESERVE
6616 || isym
->st_shndx
> SHN_HIRESERVE
)
6618 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6620 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6621 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6623 _bfd_merged_section_offset (output_bfd
, &isec
,
6624 elf_section_data (isec
)->sec_info
,
6627 else if (isym
->st_shndx
== SHN_ABS
)
6628 isec
= bfd_abs_section_ptr
;
6629 else if (isym
->st_shndx
== SHN_COMMON
)
6630 isec
= bfd_com_section_ptr
;
6639 /* Don't output the first, undefined, symbol. */
6640 if (ppsection
== finfo
->sections
)
6643 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6645 /* We never output section symbols. Instead, we use the
6646 section symbol of the corresponding section in the output
6651 /* If we are stripping all symbols, we don't want to output this
6653 if (finfo
->info
->strip
== strip_all
)
6656 /* If we are discarding all local symbols, we don't want to
6657 output this one. If we are generating a relocatable output
6658 file, then some of the local symbols may be required by
6659 relocs; we output them below as we discover that they are
6661 if (finfo
->info
->discard
== discard_all
)
6664 /* If this symbol is defined in a section which we are
6665 discarding, we don't need to keep it, but note that
6666 linker_mark is only reliable for sections that have contents.
6667 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6668 as well as linker_mark. */
6669 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6671 && ((! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6672 || (! finfo
->info
->relocatable
6673 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6676 /* Get the name of the symbol. */
6677 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6682 /* See if we are discarding symbols with this name. */
6683 if ((finfo
->info
->strip
== strip_some
6684 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6686 || (((finfo
->info
->discard
== discard_sec_merge
6687 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6688 || finfo
->info
->discard
== discard_l
)
6689 && bfd_is_local_label_name (input_bfd
, name
)))
6692 /* If we get here, we are going to output this symbol. */
6696 /* Adjust the section index for the output file. */
6697 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6698 isec
->output_section
);
6699 if (osym
.st_shndx
== SHN_BAD
)
6702 *pindex
= bfd_get_symcount (output_bfd
);
6704 /* ELF symbols in relocatable files are section relative, but
6705 in executable files they are virtual addresses. Note that
6706 this code assumes that all ELF sections have an associated
6707 BFD section with a reasonable value for output_offset; below
6708 we assume that they also have a reasonable value for
6709 output_section. Any special sections must be set up to meet
6710 these requirements. */
6711 osym
.st_value
+= isec
->output_offset
;
6712 if (! finfo
->info
->relocatable
)
6714 osym
.st_value
+= isec
->output_section
->vma
;
6715 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6717 /* STT_TLS symbols are relative to PT_TLS segment base. */
6718 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6719 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6723 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6727 /* Relocate the contents of each section. */
6728 sym_hashes
= elf_sym_hashes (input_bfd
);
6729 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6733 if (! o
->linker_mark
)
6735 /* This section was omitted from the link. */
6739 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6740 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6743 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6745 /* Section was created by _bfd_elf_link_create_dynamic_sections
6750 /* Get the contents of the section. They have been cached by a
6751 relaxation routine. Note that o is a section in an input
6752 file, so the contents field will not have been set by any of
6753 the routines which work on output files. */
6754 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6755 contents
= elf_section_data (o
)->this_hdr
.contents
;
6758 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6760 contents
= finfo
->contents
;
6761 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6765 if ((o
->flags
& SEC_RELOC
) != 0)
6767 Elf_Internal_Rela
*internal_relocs
;
6768 bfd_vma r_type_mask
;
6771 /* Get the swapped relocs. */
6773 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6774 finfo
->internal_relocs
, FALSE
);
6775 if (internal_relocs
== NULL
6776 && o
->reloc_count
> 0)
6779 if (bed
->s
->arch_size
== 32)
6786 r_type_mask
= 0xffffffff;
6790 /* Run through the relocs looking for any against symbols
6791 from discarded sections and section symbols from
6792 removed link-once sections. Complain about relocs
6793 against discarded sections. Zero relocs against removed
6794 link-once sections. Preserve debug information as much
6796 if (!elf_section_ignore_discarded_relocs (o
))
6798 Elf_Internal_Rela
*rel
, *relend
;
6799 unsigned int action
= elf_action_discarded (o
);
6801 rel
= internal_relocs
;
6802 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6803 for ( ; rel
< relend
; rel
++)
6805 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6806 asection
**ps
, *sec
;
6807 struct elf_link_hash_entry
*h
= NULL
;
6808 const char *sym_name
;
6810 if (r_symndx
== STN_UNDEF
)
6813 if (r_symndx
>= locsymcount
6814 || (elf_bad_symtab (input_bfd
)
6815 && finfo
->sections
[r_symndx
] == NULL
))
6817 h
= sym_hashes
[r_symndx
- extsymoff
];
6818 while (h
->root
.type
== bfd_link_hash_indirect
6819 || h
->root
.type
== bfd_link_hash_warning
)
6820 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6822 if (h
->root
.type
!= bfd_link_hash_defined
6823 && h
->root
.type
!= bfd_link_hash_defweak
)
6826 ps
= &h
->root
.u
.def
.section
;
6827 sym_name
= h
->root
.root
.string
;
6831 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6832 ps
= &finfo
->sections
[r_symndx
];
6833 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
);
6836 /* Complain if the definition comes from a
6837 discarded section. */
6838 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6842 BFD_ASSERT (r_symndx
!= 0);
6843 if (action
& COMPLAIN
)
6845 (*_bfd_error_handler
)
6846 (_("`%s' referenced in section `%A' of %B: "
6847 "defined in discarded section `%A' of %B\n"),
6848 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
6851 /* Try to do the best we can to support buggy old
6852 versions of gcc. If we've warned, or this is
6853 debugging info, pretend that the symbol is
6854 really defined in the kept linkonce section.
6855 FIXME: This is quite broken. Modifying the
6856 symbol here means we will be changing all later
6857 uses of the symbol, not just in this section.
6858 The only thing that makes this half reasonable
6859 is that we warn in non-debug sections, and
6860 debug sections tend to come after other
6862 kept
= sec
->kept_section
;
6863 if (kept
!= NULL
&& (action
& PRETEND
))
6865 if (elf_sec_group (sec
) != NULL
)
6866 kept
= match_group_member (sec
, kept
);
6868 && sec
->size
== kept
->size
)
6875 /* Remove the symbol reference from the reloc, but
6876 don't kill the reloc completely. This is so that
6877 a zero value will be written into the section,
6878 which may have non-zero contents put there by the
6879 assembler. Zero in things like an eh_frame fde
6880 pc_begin allows stack unwinders to recognize the
6882 rel
->r_info
&= r_type_mask
;
6888 /* Relocate the section by invoking a back end routine.
6890 The back end routine is responsible for adjusting the
6891 section contents as necessary, and (if using Rela relocs
6892 and generating a relocatable output file) adjusting the
6893 reloc addend as necessary.
6895 The back end routine does not have to worry about setting
6896 the reloc address or the reloc symbol index.
6898 The back end routine is given a pointer to the swapped in
6899 internal symbols, and can access the hash table entries
6900 for the external symbols via elf_sym_hashes (input_bfd).
6902 When generating relocatable output, the back end routine
6903 must handle STB_LOCAL/STT_SECTION symbols specially. The
6904 output symbol is going to be a section symbol
6905 corresponding to the output section, which will require
6906 the addend to be adjusted. */
6908 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
6909 input_bfd
, o
, contents
,
6917 Elf_Internal_Rela
*irela
;
6918 Elf_Internal_Rela
*irelaend
;
6919 bfd_vma last_offset
;
6920 struct elf_link_hash_entry
**rel_hash
;
6921 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
6922 unsigned int next_erel
;
6923 bfd_boolean (*reloc_emitter
)
6924 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
6925 bfd_boolean rela_normal
;
6927 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
6928 rela_normal
= (bed
->rela_normal
6929 && (input_rel_hdr
->sh_entsize
6930 == bed
->s
->sizeof_rela
));
6932 /* Adjust the reloc addresses and symbol indices. */
6934 irela
= internal_relocs
;
6935 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6936 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
6937 + elf_section_data (o
->output_section
)->rel_count
6938 + elf_section_data (o
->output_section
)->rel_count2
);
6939 last_offset
= o
->output_offset
;
6940 if (!finfo
->info
->relocatable
)
6941 last_offset
+= o
->output_section
->vma
;
6942 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
6944 unsigned long r_symndx
;
6946 Elf_Internal_Sym sym
;
6948 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
6954 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
6957 if (irela
->r_offset
>= (bfd_vma
) -2)
6959 /* This is a reloc for a deleted entry or somesuch.
6960 Turn it into an R_*_NONE reloc, at the same
6961 offset as the last reloc. elf_eh_frame.c and
6962 elf_bfd_discard_info rely on reloc offsets
6964 irela
->r_offset
= last_offset
;
6966 irela
->r_addend
= 0;
6970 irela
->r_offset
+= o
->output_offset
;
6972 /* Relocs in an executable have to be virtual addresses. */
6973 if (!finfo
->info
->relocatable
)
6974 irela
->r_offset
+= o
->output_section
->vma
;
6976 last_offset
= irela
->r_offset
;
6978 r_symndx
= irela
->r_info
>> r_sym_shift
;
6979 if (r_symndx
== STN_UNDEF
)
6982 if (r_symndx
>= locsymcount
6983 || (elf_bad_symtab (input_bfd
)
6984 && finfo
->sections
[r_symndx
] == NULL
))
6986 struct elf_link_hash_entry
*rh
;
6989 /* This is a reloc against a global symbol. We
6990 have not yet output all the local symbols, so
6991 we do not know the symbol index of any global
6992 symbol. We set the rel_hash entry for this
6993 reloc to point to the global hash table entry
6994 for this symbol. The symbol index is then
6995 set at the end of bfd_elf_final_link. */
6996 indx
= r_symndx
- extsymoff
;
6997 rh
= elf_sym_hashes (input_bfd
)[indx
];
6998 while (rh
->root
.type
== bfd_link_hash_indirect
6999 || rh
->root
.type
== bfd_link_hash_warning
)
7000 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7002 /* Setting the index to -2 tells
7003 elf_link_output_extsym that this symbol is
7005 BFD_ASSERT (rh
->indx
< 0);
7013 /* This is a reloc against a local symbol. */
7016 sym
= isymbuf
[r_symndx
];
7017 sec
= finfo
->sections
[r_symndx
];
7018 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7020 /* I suppose the backend ought to fill in the
7021 section of any STT_SECTION symbol against a
7022 processor specific section. */
7024 if (bfd_is_abs_section (sec
))
7026 else if (sec
== NULL
|| sec
->owner
== NULL
)
7028 bfd_set_error (bfd_error_bad_value
);
7033 asection
*osec
= sec
->output_section
;
7035 /* If we have discarded a section, the output
7036 section will be the absolute section. In
7037 case of discarded link-once and discarded
7038 SEC_MERGE sections, use the kept section. */
7039 if (bfd_is_abs_section (osec
)
7040 && sec
->kept_section
!= NULL
7041 && sec
->kept_section
->output_section
!= NULL
)
7043 osec
= sec
->kept_section
->output_section
;
7044 irela
->r_addend
-= osec
->vma
;
7047 if (!bfd_is_abs_section (osec
))
7049 r_symndx
= osec
->target_index
;
7050 BFD_ASSERT (r_symndx
!= 0);
7054 /* Adjust the addend according to where the
7055 section winds up in the output section. */
7057 irela
->r_addend
+= sec
->output_offset
;
7061 if (finfo
->indices
[r_symndx
] == -1)
7063 unsigned long shlink
;
7067 if (finfo
->info
->strip
== strip_all
)
7069 /* You can't do ld -r -s. */
7070 bfd_set_error (bfd_error_invalid_operation
);
7074 /* This symbol was skipped earlier, but
7075 since it is needed by a reloc, we
7076 must output it now. */
7077 shlink
= symtab_hdr
->sh_link
;
7078 name
= (bfd_elf_string_from_elf_section
7079 (input_bfd
, shlink
, sym
.st_name
));
7083 osec
= sec
->output_section
;
7085 _bfd_elf_section_from_bfd_section (output_bfd
,
7087 if (sym
.st_shndx
== SHN_BAD
)
7090 sym
.st_value
+= sec
->output_offset
;
7091 if (! finfo
->info
->relocatable
)
7093 sym
.st_value
+= osec
->vma
;
7094 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7096 /* STT_TLS symbols are relative to PT_TLS
7098 BFD_ASSERT (elf_hash_table (finfo
->info
)
7100 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7105 finfo
->indices
[r_symndx
]
7106 = bfd_get_symcount (output_bfd
);
7108 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7113 r_symndx
= finfo
->indices
[r_symndx
];
7116 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7117 | (irela
->r_info
& r_type_mask
));
7120 /* Swap out the relocs. */
7121 if (bed
->elf_backend_emit_relocs
7122 && !(finfo
->info
->relocatable
7123 || finfo
->info
->emitrelocations
))
7124 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7126 reloc_emitter
= _bfd_elf_link_output_relocs
;
7128 if (input_rel_hdr
->sh_size
!= 0
7129 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7133 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7134 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7136 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7137 * bed
->s
->int_rels_per_ext_rel
);
7138 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7145 /* Write out the modified section contents. */
7146 if (bed
->elf_backend_write_section
7147 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7149 /* Section written out. */
7151 else switch (o
->sec_info_type
)
7153 case ELF_INFO_TYPE_STABS
:
7154 if (! (_bfd_write_section_stabs
7156 &elf_hash_table (finfo
->info
)->stab_info
,
7157 o
, &elf_section_data (o
)->sec_info
, contents
)))
7160 case ELF_INFO_TYPE_MERGE
:
7161 if (! _bfd_write_merged_section (output_bfd
, o
,
7162 elf_section_data (o
)->sec_info
))
7165 case ELF_INFO_TYPE_EH_FRAME
:
7167 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7174 if (! (o
->flags
& SEC_EXCLUDE
)
7175 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7177 (file_ptr
) o
->output_offset
,
7188 /* Generate a reloc when linking an ELF file. This is a reloc
7189 requested by the linker, and does come from any input file. This
7190 is used to build constructor and destructor tables when linking
7194 elf_reloc_link_order (bfd
*output_bfd
,
7195 struct bfd_link_info
*info
,
7196 asection
*output_section
,
7197 struct bfd_link_order
*link_order
)
7199 reloc_howto_type
*howto
;
7203 struct elf_link_hash_entry
**rel_hash_ptr
;
7204 Elf_Internal_Shdr
*rel_hdr
;
7205 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7206 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7210 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7213 bfd_set_error (bfd_error_bad_value
);
7217 addend
= link_order
->u
.reloc
.p
->addend
;
7219 /* Figure out the symbol index. */
7220 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7221 + elf_section_data (output_section
)->rel_count
7222 + elf_section_data (output_section
)->rel_count2
);
7223 if (link_order
->type
== bfd_section_reloc_link_order
)
7225 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7226 BFD_ASSERT (indx
!= 0);
7227 *rel_hash_ptr
= NULL
;
7231 struct elf_link_hash_entry
*h
;
7233 /* Treat a reloc against a defined symbol as though it were
7234 actually against the section. */
7235 h
= ((struct elf_link_hash_entry
*)
7236 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7237 link_order
->u
.reloc
.p
->u
.name
,
7238 FALSE
, FALSE
, TRUE
));
7240 && (h
->root
.type
== bfd_link_hash_defined
7241 || h
->root
.type
== bfd_link_hash_defweak
))
7245 section
= h
->root
.u
.def
.section
;
7246 indx
= section
->output_section
->target_index
;
7247 *rel_hash_ptr
= NULL
;
7248 /* It seems that we ought to add the symbol value to the
7249 addend here, but in practice it has already been added
7250 because it was passed to constructor_callback. */
7251 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7255 /* Setting the index to -2 tells elf_link_output_extsym that
7256 this symbol is used by a reloc. */
7263 if (! ((*info
->callbacks
->unattached_reloc
)
7264 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7270 /* If this is an inplace reloc, we must write the addend into the
7272 if (howto
->partial_inplace
&& addend
!= 0)
7275 bfd_reloc_status_type rstat
;
7278 const char *sym_name
;
7280 size
= bfd_get_reloc_size (howto
);
7281 buf
= bfd_zmalloc (size
);
7284 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7291 case bfd_reloc_outofrange
:
7294 case bfd_reloc_overflow
:
7295 if (link_order
->type
== bfd_section_reloc_link_order
)
7296 sym_name
= bfd_section_name (output_bfd
,
7297 link_order
->u
.reloc
.p
->u
.section
);
7299 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7300 if (! ((*info
->callbacks
->reloc_overflow
)
7301 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7302 NULL
, (bfd_vma
) 0)))
7309 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7310 link_order
->offset
, size
);
7316 /* The address of a reloc is relative to the section in a
7317 relocatable file, and is a virtual address in an executable
7319 offset
= link_order
->offset
;
7320 if (! info
->relocatable
)
7321 offset
+= output_section
->vma
;
7323 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7325 irel
[i
].r_offset
= offset
;
7327 irel
[i
].r_addend
= 0;
7329 if (bed
->s
->arch_size
== 32)
7330 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7332 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7334 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7335 erel
= rel_hdr
->contents
;
7336 if (rel_hdr
->sh_type
== SHT_REL
)
7338 erel
+= (elf_section_data (output_section
)->rel_count
7339 * bed
->s
->sizeof_rel
);
7340 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7344 irel
[0].r_addend
= addend
;
7345 erel
+= (elf_section_data (output_section
)->rel_count
7346 * bed
->s
->sizeof_rela
);
7347 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7350 ++elf_section_data (output_section
)->rel_count
;
7356 /* Get the output vma of the section pointed to by the sh_link field. */
7359 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7361 Elf_Internal_Shdr
**elf_shdrp
;
7365 s
= p
->u
.indirect
.section
;
7366 elf_shdrp
= elf_elfsections (s
->owner
);
7367 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7368 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7370 The Intel C compiler generates SHT_IA_64_UNWIND with
7371 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7372 sh_info fields. Hence we could get the situation
7373 where elfsec is 0. */
7376 const struct elf_backend_data
*bed
7377 = get_elf_backend_data (s
->owner
);
7378 if (bed
->link_order_error_handler
)
7379 bed
->link_order_error_handler
7380 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7385 s
= elf_shdrp
[elfsec
]->bfd_section
;
7386 return s
->output_section
->vma
+ s
->output_offset
;
7391 /* Compare two sections based on the locations of the sections they are
7392 linked to. Used by elf_fixup_link_order. */
7395 compare_link_order (const void * a
, const void * b
)
7400 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7401 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7408 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7409 order as their linked sections. Returns false if this could not be done
7410 because an output section includes both ordered and unordered
7411 sections. Ideally we'd do this in the linker proper. */
7414 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7419 struct bfd_link_order
*p
;
7421 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7423 struct bfd_link_order
**sections
;
7429 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7431 if (p
->type
== bfd_indirect_link_order
7432 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7433 == bfd_target_elf_flavour
)
7434 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7436 s
= p
->u
.indirect
.section
;
7437 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7439 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7448 if (!seen_linkorder
)
7451 if (seen_other
&& seen_linkorder
)
7453 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7455 bfd_set_error (bfd_error_bad_value
);
7459 sections
= (struct bfd_link_order
**)
7460 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7463 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7465 sections
[seen_linkorder
++] = p
;
7467 /* Sort the input sections in the order of their linked section. */
7468 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7469 compare_link_order
);
7471 /* Change the offsets of the sections. */
7473 for (n
= 0; n
< seen_linkorder
; n
++)
7475 s
= sections
[n
]->u
.indirect
.section
;
7476 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7477 s
->output_offset
= offset
;
7478 sections
[n
]->offset
= offset
;
7479 offset
+= sections
[n
]->size
;
7486 /* Do the final step of an ELF link. */
7489 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7491 bfd_boolean dynamic
;
7492 bfd_boolean emit_relocs
;
7494 struct elf_final_link_info finfo
;
7495 register asection
*o
;
7496 register struct bfd_link_order
*p
;
7498 bfd_size_type max_contents_size
;
7499 bfd_size_type max_external_reloc_size
;
7500 bfd_size_type max_internal_reloc_count
;
7501 bfd_size_type max_sym_count
;
7502 bfd_size_type max_sym_shndx_count
;
7504 Elf_Internal_Sym elfsym
;
7506 Elf_Internal_Shdr
*symtab_hdr
;
7507 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7508 Elf_Internal_Shdr
*symstrtab_hdr
;
7509 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7510 struct elf_outext_info eoinfo
;
7512 size_t relativecount
= 0;
7513 asection
*reldyn
= 0;
7516 if (! is_elf_hash_table (info
->hash
))
7520 abfd
->flags
|= DYNAMIC
;
7522 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7523 dynobj
= elf_hash_table (info
)->dynobj
;
7525 emit_relocs
= (info
->relocatable
7526 || info
->emitrelocations
7527 || bed
->elf_backend_emit_relocs
);
7530 finfo
.output_bfd
= abfd
;
7531 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7532 if (finfo
.symstrtab
== NULL
)
7537 finfo
.dynsym_sec
= NULL
;
7538 finfo
.hash_sec
= NULL
;
7539 finfo
.symver_sec
= NULL
;
7543 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7544 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7545 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7546 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7547 /* Note that it is OK if symver_sec is NULL. */
7550 finfo
.contents
= NULL
;
7551 finfo
.external_relocs
= NULL
;
7552 finfo
.internal_relocs
= NULL
;
7553 finfo
.external_syms
= NULL
;
7554 finfo
.locsym_shndx
= NULL
;
7555 finfo
.internal_syms
= NULL
;
7556 finfo
.indices
= NULL
;
7557 finfo
.sections
= NULL
;
7558 finfo
.symbuf
= NULL
;
7559 finfo
.symshndxbuf
= NULL
;
7560 finfo
.symbuf_count
= 0;
7561 finfo
.shndxbuf_size
= 0;
7563 /* Count up the number of relocations we will output for each output
7564 section, so that we know the sizes of the reloc sections. We
7565 also figure out some maximum sizes. */
7566 max_contents_size
= 0;
7567 max_external_reloc_size
= 0;
7568 max_internal_reloc_count
= 0;
7570 max_sym_shndx_count
= 0;
7572 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7574 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7577 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7579 unsigned int reloc_count
= 0;
7580 struct bfd_elf_section_data
*esdi
= NULL
;
7581 unsigned int *rel_count1
;
7583 if (p
->type
== bfd_section_reloc_link_order
7584 || p
->type
== bfd_symbol_reloc_link_order
)
7586 else if (p
->type
== bfd_indirect_link_order
)
7590 sec
= p
->u
.indirect
.section
;
7591 esdi
= elf_section_data (sec
);
7593 /* Mark all sections which are to be included in the
7594 link. This will normally be every section. We need
7595 to do this so that we can identify any sections which
7596 the linker has decided to not include. */
7597 sec
->linker_mark
= TRUE
;
7599 if (sec
->flags
& SEC_MERGE
)
7602 if (info
->relocatable
|| info
->emitrelocations
)
7603 reloc_count
= sec
->reloc_count
;
7604 else if (bed
->elf_backend_count_relocs
)
7606 Elf_Internal_Rela
* relocs
;
7608 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7611 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7613 if (elf_section_data (o
)->relocs
!= relocs
)
7617 if (sec
->rawsize
> max_contents_size
)
7618 max_contents_size
= sec
->rawsize
;
7619 if (sec
->size
> max_contents_size
)
7620 max_contents_size
= sec
->size
;
7622 /* We are interested in just local symbols, not all
7624 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7625 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7629 if (elf_bad_symtab (sec
->owner
))
7630 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7631 / bed
->s
->sizeof_sym
);
7633 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7635 if (sym_count
> max_sym_count
)
7636 max_sym_count
= sym_count
;
7638 if (sym_count
> max_sym_shndx_count
7639 && elf_symtab_shndx (sec
->owner
) != 0)
7640 max_sym_shndx_count
= sym_count
;
7642 if ((sec
->flags
& SEC_RELOC
) != 0)
7646 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7647 if (ext_size
> max_external_reloc_size
)
7648 max_external_reloc_size
= ext_size
;
7649 if (sec
->reloc_count
> max_internal_reloc_count
)
7650 max_internal_reloc_count
= sec
->reloc_count
;
7655 if (reloc_count
== 0)
7658 o
->reloc_count
+= reloc_count
;
7660 /* MIPS may have a mix of REL and RELA relocs on sections.
7661 To support this curious ABI we keep reloc counts in
7662 elf_section_data too. We must be careful to add the
7663 relocations from the input section to the right output
7664 count. FIXME: Get rid of one count. We have
7665 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7666 rel_count1
= &esdo
->rel_count
;
7669 bfd_boolean same_size
;
7670 bfd_size_type entsize1
;
7672 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7673 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7674 || entsize1
== bed
->s
->sizeof_rela
);
7675 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7678 rel_count1
= &esdo
->rel_count2
;
7680 if (esdi
->rel_hdr2
!= NULL
)
7682 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7683 unsigned int alt_count
;
7684 unsigned int *rel_count2
;
7686 BFD_ASSERT (entsize2
!= entsize1
7687 && (entsize2
== bed
->s
->sizeof_rel
7688 || entsize2
== bed
->s
->sizeof_rela
));
7690 rel_count2
= &esdo
->rel_count2
;
7692 rel_count2
= &esdo
->rel_count
;
7694 /* The following is probably too simplistic if the
7695 backend counts output relocs unusually. */
7696 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7697 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7698 *rel_count2
+= alt_count
;
7699 reloc_count
-= alt_count
;
7702 *rel_count1
+= reloc_count
;
7705 if (o
->reloc_count
> 0)
7706 o
->flags
|= SEC_RELOC
;
7709 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7710 set it (this is probably a bug) and if it is set
7711 assign_section_numbers will create a reloc section. */
7712 o
->flags
&=~ SEC_RELOC
;
7715 /* If the SEC_ALLOC flag is not set, force the section VMA to
7716 zero. This is done in elf_fake_sections as well, but forcing
7717 the VMA to 0 here will ensure that relocs against these
7718 sections are handled correctly. */
7719 if ((o
->flags
& SEC_ALLOC
) == 0
7720 && ! o
->user_set_vma
)
7724 if (! info
->relocatable
&& merged
)
7725 elf_link_hash_traverse (elf_hash_table (info
),
7726 _bfd_elf_link_sec_merge_syms
, abfd
);
7728 /* Figure out the file positions for everything but the symbol table
7729 and the relocs. We set symcount to force assign_section_numbers
7730 to create a symbol table. */
7731 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7732 BFD_ASSERT (! abfd
->output_has_begun
);
7733 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7736 /* Set sizes, and assign file positions for reloc sections. */
7737 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7739 if ((o
->flags
& SEC_RELOC
) != 0)
7741 if (!(_bfd_elf_link_size_reloc_section
7742 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7745 if (elf_section_data (o
)->rel_hdr2
7746 && !(_bfd_elf_link_size_reloc_section
7747 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7751 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7752 to count upwards while actually outputting the relocations. */
7753 elf_section_data (o
)->rel_count
= 0;
7754 elf_section_data (o
)->rel_count2
= 0;
7757 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7759 /* We have now assigned file positions for all the sections except
7760 .symtab and .strtab. We start the .symtab section at the current
7761 file position, and write directly to it. We build the .strtab
7762 section in memory. */
7763 bfd_get_symcount (abfd
) = 0;
7764 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7765 /* sh_name is set in prep_headers. */
7766 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7767 /* sh_flags, sh_addr and sh_size all start off zero. */
7768 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7769 /* sh_link is set in assign_section_numbers. */
7770 /* sh_info is set below. */
7771 /* sh_offset is set just below. */
7772 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7774 off
= elf_tdata (abfd
)->next_file_pos
;
7775 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7777 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7778 incorrect. We do not yet know the size of the .symtab section.
7779 We correct next_file_pos below, after we do know the size. */
7781 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7782 continuously seeking to the right position in the file. */
7783 if (! info
->keep_memory
|| max_sym_count
< 20)
7784 finfo
.symbuf_size
= 20;
7786 finfo
.symbuf_size
= max_sym_count
;
7787 amt
= finfo
.symbuf_size
;
7788 amt
*= bed
->s
->sizeof_sym
;
7789 finfo
.symbuf
= bfd_malloc (amt
);
7790 if (finfo
.symbuf
== NULL
)
7792 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7794 /* Wild guess at number of output symbols. realloc'd as needed. */
7795 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7796 finfo
.shndxbuf_size
= amt
;
7797 amt
*= sizeof (Elf_External_Sym_Shndx
);
7798 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7799 if (finfo
.symshndxbuf
== NULL
)
7803 /* Start writing out the symbol table. The first symbol is always a
7805 if (info
->strip
!= strip_all
7808 elfsym
.st_value
= 0;
7811 elfsym
.st_other
= 0;
7812 elfsym
.st_shndx
= SHN_UNDEF
;
7813 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7819 /* Some standard ELF linkers do this, but we don't because it causes
7820 bootstrap comparison failures. */
7821 /* Output a file symbol for the output file as the second symbol.
7822 We output this even if we are discarding local symbols, although
7823 I'm not sure if this is correct. */
7824 elfsym
.st_value
= 0;
7826 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7827 elfsym
.st_other
= 0;
7828 elfsym
.st_shndx
= SHN_ABS
;
7829 if (! elf_link_output_sym (&finfo
, bfd_get_filename (abfd
),
7830 &elfsym
, bfd_abs_section_ptr
, NULL
))
7834 /* Output a symbol for each section. We output these even if we are
7835 discarding local symbols, since they are used for relocs. These
7836 symbols have no names. We store the index of each one in the
7837 index field of the section, so that we can find it again when
7838 outputting relocs. */
7839 if (info
->strip
!= strip_all
7843 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7844 elfsym
.st_other
= 0;
7845 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7847 o
= bfd_section_from_elf_index (abfd
, i
);
7849 o
->target_index
= bfd_get_symcount (abfd
);
7850 elfsym
.st_shndx
= i
;
7851 if (info
->relocatable
|| o
== NULL
)
7852 elfsym
.st_value
= 0;
7854 elfsym
.st_value
= o
->vma
;
7855 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7857 if (i
== SHN_LORESERVE
- 1)
7858 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7862 /* Allocate some memory to hold information read in from the input
7864 if (max_contents_size
!= 0)
7866 finfo
.contents
= bfd_malloc (max_contents_size
);
7867 if (finfo
.contents
== NULL
)
7871 if (max_external_reloc_size
!= 0)
7873 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
7874 if (finfo
.external_relocs
== NULL
)
7878 if (max_internal_reloc_count
!= 0)
7880 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7881 amt
*= sizeof (Elf_Internal_Rela
);
7882 finfo
.internal_relocs
= bfd_malloc (amt
);
7883 if (finfo
.internal_relocs
== NULL
)
7887 if (max_sym_count
!= 0)
7889 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
7890 finfo
.external_syms
= bfd_malloc (amt
);
7891 if (finfo
.external_syms
== NULL
)
7894 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
7895 finfo
.internal_syms
= bfd_malloc (amt
);
7896 if (finfo
.internal_syms
== NULL
)
7899 amt
= max_sym_count
* sizeof (long);
7900 finfo
.indices
= bfd_malloc (amt
);
7901 if (finfo
.indices
== NULL
)
7904 amt
= max_sym_count
* sizeof (asection
*);
7905 finfo
.sections
= bfd_malloc (amt
);
7906 if (finfo
.sections
== NULL
)
7910 if (max_sym_shndx_count
!= 0)
7912 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
7913 finfo
.locsym_shndx
= bfd_malloc (amt
);
7914 if (finfo
.locsym_shndx
== NULL
)
7918 if (elf_hash_table (info
)->tls_sec
)
7920 bfd_vma base
, end
= 0;
7923 for (sec
= elf_hash_table (info
)->tls_sec
;
7924 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
7927 bfd_vma size
= sec
->size
;
7929 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
7931 struct bfd_link_order
*o
;
7933 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
7934 if (size
< o
->offset
+ o
->size
)
7935 size
= o
->offset
+ o
->size
;
7937 end
= sec
->vma
+ size
;
7939 base
= elf_hash_table (info
)->tls_sec
->vma
;
7940 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
7941 elf_hash_table (info
)->tls_size
= end
- base
;
7944 /* Reorder SHF_LINK_ORDER sections. */
7945 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7947 if (!elf_fixup_link_order (abfd
, o
))
7951 /* Since ELF permits relocations to be against local symbols, we
7952 must have the local symbols available when we do the relocations.
7953 Since we would rather only read the local symbols once, and we
7954 would rather not keep them in memory, we handle all the
7955 relocations for a single input file at the same time.
7957 Unfortunately, there is no way to know the total number of local
7958 symbols until we have seen all of them, and the local symbol
7959 indices precede the global symbol indices. This means that when
7960 we are generating relocatable output, and we see a reloc against
7961 a global symbol, we can not know the symbol index until we have
7962 finished examining all the local symbols to see which ones we are
7963 going to output. To deal with this, we keep the relocations in
7964 memory, and don't output them until the end of the link. This is
7965 an unfortunate waste of memory, but I don't see a good way around
7966 it. Fortunately, it only happens when performing a relocatable
7967 link, which is not the common case. FIXME: If keep_memory is set
7968 we could write the relocs out and then read them again; I don't
7969 know how bad the memory loss will be. */
7971 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
7972 sub
->output_has_begun
= FALSE
;
7973 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7975 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7977 if (p
->type
== bfd_indirect_link_order
7978 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7979 == bfd_target_elf_flavour
)
7980 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7982 if (! sub
->output_has_begun
)
7984 if (! elf_link_input_bfd (&finfo
, sub
))
7986 sub
->output_has_begun
= TRUE
;
7989 else if (p
->type
== bfd_section_reloc_link_order
7990 || p
->type
== bfd_symbol_reloc_link_order
)
7992 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
7997 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8003 /* Output any global symbols that got converted to local in a
8004 version script or due to symbol visibility. We do this in a
8005 separate step since ELF requires all local symbols to appear
8006 prior to any global symbols. FIXME: We should only do this if
8007 some global symbols were, in fact, converted to become local.
8008 FIXME: Will this work correctly with the Irix 5 linker? */
8009 eoinfo
.failed
= FALSE
;
8010 eoinfo
.finfo
= &finfo
;
8011 eoinfo
.localsyms
= TRUE
;
8012 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8017 /* That wrote out all the local symbols. Finish up the symbol table
8018 with the global symbols. Even if we want to strip everything we
8019 can, we still need to deal with those global symbols that got
8020 converted to local in a version script. */
8022 /* The sh_info field records the index of the first non local symbol. */
8023 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8026 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8028 Elf_Internal_Sym sym
;
8029 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8030 long last_local
= 0;
8032 /* Write out the section symbols for the output sections. */
8039 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8042 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8048 dynindx
= elf_section_data (s
)->dynindx
;
8051 indx
= elf_section_data (s
)->this_idx
;
8052 BFD_ASSERT (indx
> 0);
8053 sym
.st_shndx
= indx
;
8054 sym
.st_value
= s
->vma
;
8055 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8056 if (last_local
< dynindx
)
8057 last_local
= dynindx
;
8058 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8062 /* Write out the local dynsyms. */
8063 if (elf_hash_table (info
)->dynlocal
)
8065 struct elf_link_local_dynamic_entry
*e
;
8066 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8071 sym
.st_size
= e
->isym
.st_size
;
8072 sym
.st_other
= e
->isym
.st_other
;
8074 /* Copy the internal symbol as is.
8075 Note that we saved a word of storage and overwrote
8076 the original st_name with the dynstr_index. */
8079 if (e
->isym
.st_shndx
!= SHN_UNDEF
8080 && (e
->isym
.st_shndx
< SHN_LORESERVE
8081 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8083 s
= bfd_section_from_elf_index (e
->input_bfd
,
8087 elf_section_data (s
->output_section
)->this_idx
;
8088 sym
.st_value
= (s
->output_section
->vma
8090 + e
->isym
.st_value
);
8093 if (last_local
< e
->dynindx
)
8094 last_local
= e
->dynindx
;
8096 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8097 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8101 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8105 /* We get the global symbols from the hash table. */
8106 eoinfo
.failed
= FALSE
;
8107 eoinfo
.localsyms
= FALSE
;
8108 eoinfo
.finfo
= &finfo
;
8109 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8114 /* If backend needs to output some symbols not present in the hash
8115 table, do it now. */
8116 if (bed
->elf_backend_output_arch_syms
)
8118 typedef bfd_boolean (*out_sym_func
)
8119 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8120 struct elf_link_hash_entry
*);
8122 if (! ((*bed
->elf_backend_output_arch_syms
)
8123 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8127 /* Flush all symbols to the file. */
8128 if (! elf_link_flush_output_syms (&finfo
, bed
))
8131 /* Now we know the size of the symtab section. */
8132 off
+= symtab_hdr
->sh_size
;
8134 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8135 if (symtab_shndx_hdr
->sh_name
!= 0)
8137 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8138 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8139 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8140 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8141 symtab_shndx_hdr
->sh_size
= amt
;
8143 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8146 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8147 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8152 /* Finish up and write out the symbol string table (.strtab)
8154 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8155 /* sh_name was set in prep_headers. */
8156 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8157 symstrtab_hdr
->sh_flags
= 0;
8158 symstrtab_hdr
->sh_addr
= 0;
8159 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8160 symstrtab_hdr
->sh_entsize
= 0;
8161 symstrtab_hdr
->sh_link
= 0;
8162 symstrtab_hdr
->sh_info
= 0;
8163 /* sh_offset is set just below. */
8164 symstrtab_hdr
->sh_addralign
= 1;
8166 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8167 elf_tdata (abfd
)->next_file_pos
= off
;
8169 if (bfd_get_symcount (abfd
) > 0)
8171 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8172 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8176 /* Adjust the relocs to have the correct symbol indices. */
8177 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8179 if ((o
->flags
& SEC_RELOC
) == 0)
8182 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8183 elf_section_data (o
)->rel_count
,
8184 elf_section_data (o
)->rel_hashes
);
8185 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8186 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8187 elf_section_data (o
)->rel_count2
,
8188 (elf_section_data (o
)->rel_hashes
8189 + elf_section_data (o
)->rel_count
));
8191 /* Set the reloc_count field to 0 to prevent write_relocs from
8192 trying to swap the relocs out itself. */
8196 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8197 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8199 /* If we are linking against a dynamic object, or generating a
8200 shared library, finish up the dynamic linking information. */
8203 bfd_byte
*dyncon
, *dynconend
;
8205 /* Fix up .dynamic entries. */
8206 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8207 BFD_ASSERT (o
!= NULL
);
8209 dyncon
= o
->contents
;
8210 dynconend
= o
->contents
+ o
->size
;
8211 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8213 Elf_Internal_Dyn dyn
;
8217 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8224 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8226 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8228 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8229 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8232 dyn
.d_un
.d_val
= relativecount
;
8239 name
= info
->init_function
;
8242 name
= info
->fini_function
;
8245 struct elf_link_hash_entry
*h
;
8247 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8248 FALSE
, FALSE
, TRUE
);
8250 && (h
->root
.type
== bfd_link_hash_defined
8251 || h
->root
.type
== bfd_link_hash_defweak
))
8253 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8254 o
= h
->root
.u
.def
.section
;
8255 if (o
->output_section
!= NULL
)
8256 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8257 + o
->output_offset
);
8260 /* The symbol is imported from another shared
8261 library and does not apply to this one. */
8269 case DT_PREINIT_ARRAYSZ
:
8270 name
= ".preinit_array";
8272 case DT_INIT_ARRAYSZ
:
8273 name
= ".init_array";
8275 case DT_FINI_ARRAYSZ
:
8276 name
= ".fini_array";
8278 o
= bfd_get_section_by_name (abfd
, name
);
8281 (*_bfd_error_handler
)
8282 (_("%B: could not find output section %s"), abfd
, name
);
8286 (*_bfd_error_handler
)
8287 (_("warning: %s section has zero size"), name
);
8288 dyn
.d_un
.d_val
= o
->size
;
8291 case DT_PREINIT_ARRAY
:
8292 name
= ".preinit_array";
8295 name
= ".init_array";
8298 name
= ".fini_array";
8311 name
= ".gnu.version_d";
8314 name
= ".gnu.version_r";
8317 name
= ".gnu.version";
8319 o
= bfd_get_section_by_name (abfd
, name
);
8322 (*_bfd_error_handler
)
8323 (_("%B: could not find output section %s"), abfd
, name
);
8326 dyn
.d_un
.d_ptr
= o
->vma
;
8333 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8338 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8340 Elf_Internal_Shdr
*hdr
;
8342 hdr
= elf_elfsections (abfd
)[i
];
8343 if (hdr
->sh_type
== type
8344 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8346 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8347 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8350 if (dyn
.d_un
.d_val
== 0
8351 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8352 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8358 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8362 /* If we have created any dynamic sections, then output them. */
8365 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8368 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8370 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8372 || o
->output_section
== bfd_abs_section_ptr
)
8374 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8376 /* At this point, we are only interested in sections
8377 created by _bfd_elf_link_create_dynamic_sections. */
8380 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8382 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8384 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8386 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8388 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8390 (file_ptr
) o
->output_offset
,
8396 /* The contents of the .dynstr section are actually in a
8398 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8399 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8400 || ! _bfd_elf_strtab_emit (abfd
,
8401 elf_hash_table (info
)->dynstr
))
8407 if (info
->relocatable
)
8409 bfd_boolean failed
= FALSE
;
8411 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8416 /* If we have optimized stabs strings, output them. */
8417 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8419 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8423 if (info
->eh_frame_hdr
)
8425 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8429 if (finfo
.symstrtab
!= NULL
)
8430 _bfd_stringtab_free (finfo
.symstrtab
);
8431 if (finfo
.contents
!= NULL
)
8432 free (finfo
.contents
);
8433 if (finfo
.external_relocs
!= NULL
)
8434 free (finfo
.external_relocs
);
8435 if (finfo
.internal_relocs
!= NULL
)
8436 free (finfo
.internal_relocs
);
8437 if (finfo
.external_syms
!= NULL
)
8438 free (finfo
.external_syms
);
8439 if (finfo
.locsym_shndx
!= NULL
)
8440 free (finfo
.locsym_shndx
);
8441 if (finfo
.internal_syms
!= NULL
)
8442 free (finfo
.internal_syms
);
8443 if (finfo
.indices
!= NULL
)
8444 free (finfo
.indices
);
8445 if (finfo
.sections
!= NULL
)
8446 free (finfo
.sections
);
8447 if (finfo
.symbuf
!= NULL
)
8448 free (finfo
.symbuf
);
8449 if (finfo
.symshndxbuf
!= NULL
)
8450 free (finfo
.symshndxbuf
);
8451 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8453 if ((o
->flags
& SEC_RELOC
) != 0
8454 && elf_section_data (o
)->rel_hashes
!= NULL
)
8455 free (elf_section_data (o
)->rel_hashes
);
8458 elf_tdata (abfd
)->linker
= TRUE
;
8463 if (finfo
.symstrtab
!= NULL
)
8464 _bfd_stringtab_free (finfo
.symstrtab
);
8465 if (finfo
.contents
!= NULL
)
8466 free (finfo
.contents
);
8467 if (finfo
.external_relocs
!= NULL
)
8468 free (finfo
.external_relocs
);
8469 if (finfo
.internal_relocs
!= NULL
)
8470 free (finfo
.internal_relocs
);
8471 if (finfo
.external_syms
!= NULL
)
8472 free (finfo
.external_syms
);
8473 if (finfo
.locsym_shndx
!= NULL
)
8474 free (finfo
.locsym_shndx
);
8475 if (finfo
.internal_syms
!= NULL
)
8476 free (finfo
.internal_syms
);
8477 if (finfo
.indices
!= NULL
)
8478 free (finfo
.indices
);
8479 if (finfo
.sections
!= NULL
)
8480 free (finfo
.sections
);
8481 if (finfo
.symbuf
!= NULL
)
8482 free (finfo
.symbuf
);
8483 if (finfo
.symshndxbuf
!= NULL
)
8484 free (finfo
.symshndxbuf
);
8485 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8487 if ((o
->flags
& SEC_RELOC
) != 0
8488 && elf_section_data (o
)->rel_hashes
!= NULL
)
8489 free (elf_section_data (o
)->rel_hashes
);
8495 /* Garbage collect unused sections. */
8497 /* The mark phase of garbage collection. For a given section, mark
8498 it and any sections in this section's group, and all the sections
8499 which define symbols to which it refers. */
8501 typedef asection
* (*gc_mark_hook_fn
)
8502 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8503 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8506 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8508 gc_mark_hook_fn gc_mark_hook
)
8511 asection
*group_sec
;
8515 /* Mark all the sections in the group. */
8516 group_sec
= elf_section_data (sec
)->next_in_group
;
8517 if (group_sec
&& !group_sec
->gc_mark
)
8518 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8521 /* Look through the section relocs. */
8523 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8525 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8526 Elf_Internal_Shdr
*symtab_hdr
;
8527 struct elf_link_hash_entry
**sym_hashes
;
8530 bfd
*input_bfd
= sec
->owner
;
8531 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8532 Elf_Internal_Sym
*isym
= NULL
;
8535 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8536 sym_hashes
= elf_sym_hashes (input_bfd
);
8538 /* Read the local symbols. */
8539 if (elf_bad_symtab (input_bfd
))
8541 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8545 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8547 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8548 if (isym
== NULL
&& nlocsyms
!= 0)
8550 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8556 /* Read the relocations. */
8557 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8559 if (relstart
== NULL
)
8564 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8566 if (bed
->s
->arch_size
== 32)
8571 for (rel
= relstart
; rel
< relend
; rel
++)
8573 unsigned long r_symndx
;
8575 struct elf_link_hash_entry
*h
;
8577 r_symndx
= rel
->r_info
>> r_sym_shift
;
8581 if (r_symndx
>= nlocsyms
8582 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8584 h
= sym_hashes
[r_symndx
- extsymoff
];
8585 while (h
->root
.type
== bfd_link_hash_indirect
8586 || h
->root
.type
== bfd_link_hash_warning
)
8587 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8588 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8592 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8595 if (rsec
&& !rsec
->gc_mark
)
8597 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8599 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8608 if (elf_section_data (sec
)->relocs
!= relstart
)
8611 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8613 if (! info
->keep_memory
)
8616 symtab_hdr
->contents
= (unsigned char *) isym
;
8623 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8626 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8630 if (h
->root
.type
== bfd_link_hash_warning
)
8631 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8633 if (h
->dynindx
!= -1
8634 && ((h
->root
.type
!= bfd_link_hash_defined
8635 && h
->root
.type
!= bfd_link_hash_defweak
)
8636 || h
->root
.u
.def
.section
->gc_mark
))
8637 h
->dynindx
= (*idx
)++;
8642 /* The sweep phase of garbage collection. Remove all garbage sections. */
8644 typedef bfd_boolean (*gc_sweep_hook_fn
)
8645 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8648 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8652 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8656 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8659 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8661 /* Keep debug and special sections. */
8662 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8663 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8669 /* Skip sweeping sections already excluded. */
8670 if (o
->flags
& SEC_EXCLUDE
)
8673 /* Since this is early in the link process, it is simple
8674 to remove a section from the output. */
8675 o
->flags
|= SEC_EXCLUDE
;
8677 /* But we also have to update some of the relocation
8678 info we collected before. */
8680 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8682 Elf_Internal_Rela
*internal_relocs
;
8686 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8688 if (internal_relocs
== NULL
)
8691 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8693 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8694 free (internal_relocs
);
8702 /* Remove the symbols that were in the swept sections from the dynamic
8703 symbol table. GCFIXME: Anyone know how to get them out of the
8704 static symbol table as well? */
8708 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8710 elf_hash_table (info
)->dynsymcount
= i
;
8716 /* Propagate collected vtable information. This is called through
8717 elf_link_hash_traverse. */
8720 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8722 if (h
->root
.type
== bfd_link_hash_warning
)
8723 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8725 /* Those that are not vtables. */
8726 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8729 /* Those vtables that do not have parents, we cannot merge. */
8730 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8733 /* If we've already been done, exit. */
8734 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8737 /* Make sure the parent's table is up to date. */
8738 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8740 if (h
->vtable
->used
== NULL
)
8742 /* None of this table's entries were referenced. Re-use the
8744 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8745 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8750 bfd_boolean
*cu
, *pu
;
8752 /* Or the parent's entries into ours. */
8753 cu
= h
->vtable
->used
;
8755 pu
= h
->vtable
->parent
->vtable
->used
;
8758 const struct elf_backend_data
*bed
;
8759 unsigned int log_file_align
;
8761 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8762 log_file_align
= bed
->s
->log_file_align
;
8763 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8778 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8781 bfd_vma hstart
, hend
;
8782 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8783 const struct elf_backend_data
*bed
;
8784 unsigned int log_file_align
;
8786 if (h
->root
.type
== bfd_link_hash_warning
)
8787 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8789 /* Take care of both those symbols that do not describe vtables as
8790 well as those that are not loaded. */
8791 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8794 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8795 || h
->root
.type
== bfd_link_hash_defweak
);
8797 sec
= h
->root
.u
.def
.section
;
8798 hstart
= h
->root
.u
.def
.value
;
8799 hend
= hstart
+ h
->size
;
8801 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8803 return *(bfd_boolean
*) okp
= FALSE
;
8804 bed
= get_elf_backend_data (sec
->owner
);
8805 log_file_align
= bed
->s
->log_file_align
;
8807 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8809 for (rel
= relstart
; rel
< relend
; ++rel
)
8810 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8812 /* If the entry is in use, do nothing. */
8814 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8816 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8817 if (h
->vtable
->used
[entry
])
8820 /* Otherwise, kill it. */
8821 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8827 /* Mark sections containing dynamically referenced symbols. This is called
8828 through elf_link_hash_traverse. */
8831 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8832 void *okp ATTRIBUTE_UNUSED
)
8834 if (h
->root
.type
== bfd_link_hash_warning
)
8835 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8837 if ((h
->root
.type
== bfd_link_hash_defined
8838 || h
->root
.type
== bfd_link_hash_defweak
)
8840 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8845 /* Do mark and sweep of unused sections. */
8848 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8850 bfd_boolean ok
= TRUE
;
8852 asection
* (*gc_mark_hook
)
8853 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8854 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8856 if (!get_elf_backend_data (abfd
)->can_gc_sections
8857 || info
->relocatable
8858 || info
->emitrelocations
8860 || !is_elf_hash_table (info
->hash
))
8862 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
8866 /* Apply transitive closure to the vtable entry usage info. */
8867 elf_link_hash_traverse (elf_hash_table (info
),
8868 elf_gc_propagate_vtable_entries_used
,
8873 /* Kill the vtable relocations that were not used. */
8874 elf_link_hash_traverse (elf_hash_table (info
),
8875 elf_gc_smash_unused_vtentry_relocs
,
8880 /* Mark dynamically referenced symbols. */
8881 if (elf_hash_table (info
)->dynamic_sections_created
)
8882 elf_link_hash_traverse (elf_hash_table (info
),
8883 elf_gc_mark_dynamic_ref_symbol
,
8888 /* Grovel through relocs to find out who stays ... */
8889 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
8890 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8894 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8897 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8899 if (o
->flags
& SEC_KEEP
)
8901 /* _bfd_elf_discard_section_eh_frame knows how to discard
8902 orphaned FDEs so don't mark sections referenced by the
8903 EH frame section. */
8904 if (strcmp (o
->name
, ".eh_frame") == 0)
8906 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
8912 /* ... and mark SEC_EXCLUDE for those that go. */
8913 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
8919 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
8922 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
8924 struct elf_link_hash_entry
*h
,
8927 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
8928 struct elf_link_hash_entry
**search
, *child
;
8929 bfd_size_type extsymcount
;
8930 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8932 /* The sh_info field of the symtab header tells us where the
8933 external symbols start. We don't care about the local symbols at
8935 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
8936 if (!elf_bad_symtab (abfd
))
8937 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
8939 sym_hashes
= elf_sym_hashes (abfd
);
8940 sym_hashes_end
= sym_hashes
+ extsymcount
;
8942 /* Hunt down the child symbol, which is in this section at the same
8943 offset as the relocation. */
8944 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
8946 if ((child
= *search
) != NULL
8947 && (child
->root
.type
== bfd_link_hash_defined
8948 || child
->root
.type
== bfd_link_hash_defweak
)
8949 && child
->root
.u
.def
.section
== sec
8950 && child
->root
.u
.def
.value
== offset
)
8954 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
8955 abfd
, sec
, (unsigned long) offset
);
8956 bfd_set_error (bfd_error_invalid_operation
);
8962 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
8968 /* This *should* only be the absolute section. It could potentially
8969 be that someone has defined a non-global vtable though, which
8970 would be bad. It isn't worth paging in the local symbols to be
8971 sure though; that case should simply be handled by the assembler. */
8973 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
8976 child
->vtable
->parent
= h
;
8981 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
8984 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
8985 asection
*sec ATTRIBUTE_UNUSED
,
8986 struct elf_link_hash_entry
*h
,
8989 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8990 unsigned int log_file_align
= bed
->s
->log_file_align
;
8994 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
8999 if (addend
>= h
->vtable
->size
)
9001 size_t size
, bytes
, file_align
;
9002 bfd_boolean
*ptr
= h
->vtable
->used
;
9004 /* While the symbol is undefined, we have to be prepared to handle
9006 file_align
= 1 << log_file_align
;
9007 if (h
->root
.type
== bfd_link_hash_undefined
)
9008 size
= addend
+ file_align
;
9014 /* Oops! We've got a reference past the defined end of
9015 the table. This is probably a bug -- shall we warn? */
9016 size
= addend
+ file_align
;
9019 size
= (size
+ file_align
- 1) & -file_align
;
9021 /* Allocate one extra entry for use as a "done" flag for the
9022 consolidation pass. */
9023 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9027 ptr
= bfd_realloc (ptr
- 1, bytes
);
9033 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9034 * sizeof (bfd_boolean
));
9035 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9039 ptr
= bfd_zmalloc (bytes
);
9044 /* And arrange for that done flag to be at index -1. */
9045 h
->vtable
->used
= ptr
+ 1;
9046 h
->vtable
->size
= size
;
9049 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9054 struct alloc_got_off_arg
{
9056 unsigned int got_elt_size
;
9059 /* We need a special top-level link routine to convert got reference counts
9060 to real got offsets. */
9063 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9065 struct alloc_got_off_arg
*gofarg
= arg
;
9067 if (h
->root
.type
== bfd_link_hash_warning
)
9068 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9070 if (h
->got
.refcount
> 0)
9072 h
->got
.offset
= gofarg
->gotoff
;
9073 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9076 h
->got
.offset
= (bfd_vma
) -1;
9081 /* And an accompanying bit to work out final got entry offsets once
9082 we're done. Should be called from final_link. */
9085 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9086 struct bfd_link_info
*info
)
9089 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9091 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9092 struct alloc_got_off_arg gofarg
;
9094 if (! is_elf_hash_table (info
->hash
))
9097 /* The GOT offset is relative to the .got section, but the GOT header is
9098 put into the .got.plt section, if the backend uses it. */
9099 if (bed
->want_got_plt
)
9102 gotoff
= bed
->got_header_size
;
9104 /* Do the local .got entries first. */
9105 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9107 bfd_signed_vma
*local_got
;
9108 bfd_size_type j
, locsymcount
;
9109 Elf_Internal_Shdr
*symtab_hdr
;
9111 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9114 local_got
= elf_local_got_refcounts (i
);
9118 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9119 if (elf_bad_symtab (i
))
9120 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9122 locsymcount
= symtab_hdr
->sh_info
;
9124 for (j
= 0; j
< locsymcount
; ++j
)
9126 if (local_got
[j
] > 0)
9128 local_got
[j
] = gotoff
;
9129 gotoff
+= got_elt_size
;
9132 local_got
[j
] = (bfd_vma
) -1;
9136 /* Then the global .got entries. .plt refcounts are handled by
9137 adjust_dynamic_symbol */
9138 gofarg
.gotoff
= gotoff
;
9139 gofarg
.got_elt_size
= got_elt_size
;
9140 elf_link_hash_traverse (elf_hash_table (info
),
9141 elf_gc_allocate_got_offsets
,
9146 /* Many folk need no more in the way of final link than this, once
9147 got entry reference counting is enabled. */
9150 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9152 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9155 /* Invoke the regular ELF backend linker to do all the work. */
9156 return bfd_elf_final_link (abfd
, info
);
9160 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9162 struct elf_reloc_cookie
*rcookie
= cookie
;
9164 if (rcookie
->bad_symtab
)
9165 rcookie
->rel
= rcookie
->rels
;
9167 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9169 unsigned long r_symndx
;
9171 if (! rcookie
->bad_symtab
)
9172 if (rcookie
->rel
->r_offset
> offset
)
9174 if (rcookie
->rel
->r_offset
!= offset
)
9177 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9178 if (r_symndx
== SHN_UNDEF
)
9181 if (r_symndx
>= rcookie
->locsymcount
9182 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9184 struct elf_link_hash_entry
*h
;
9186 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9188 while (h
->root
.type
== bfd_link_hash_indirect
9189 || h
->root
.type
== bfd_link_hash_warning
)
9190 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9192 if ((h
->root
.type
== bfd_link_hash_defined
9193 || h
->root
.type
== bfd_link_hash_defweak
)
9194 && elf_discarded_section (h
->root
.u
.def
.section
))
9201 /* It's not a relocation against a global symbol,
9202 but it could be a relocation against a local
9203 symbol for a discarded section. */
9205 Elf_Internal_Sym
*isym
;
9207 /* Need to: get the symbol; get the section. */
9208 isym
= &rcookie
->locsyms
[r_symndx
];
9209 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9211 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9212 if (isec
!= NULL
&& elf_discarded_section (isec
))
9221 /* Discard unneeded references to discarded sections.
9222 Returns TRUE if any section's size was changed. */
9223 /* This function assumes that the relocations are in sorted order,
9224 which is true for all known assemblers. */
9227 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9229 struct elf_reloc_cookie cookie
;
9230 asection
*stab
, *eh
;
9231 Elf_Internal_Shdr
*symtab_hdr
;
9232 const struct elf_backend_data
*bed
;
9235 bfd_boolean ret
= FALSE
;
9237 if (info
->traditional_format
9238 || !is_elf_hash_table (info
->hash
))
9241 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9243 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9246 bed
= get_elf_backend_data (abfd
);
9248 if ((abfd
->flags
& DYNAMIC
) != 0)
9251 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9252 if (info
->relocatable
9255 || bfd_is_abs_section (eh
->output_section
))))
9258 stab
= bfd_get_section_by_name (abfd
, ".stab");
9261 || bfd_is_abs_section (stab
->output_section
)
9262 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9267 && bed
->elf_backend_discard_info
== NULL
)
9270 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9272 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9273 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9274 if (cookie
.bad_symtab
)
9276 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9277 cookie
.extsymoff
= 0;
9281 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9282 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9285 if (bed
->s
->arch_size
== 32)
9286 cookie
.r_sym_shift
= 8;
9288 cookie
.r_sym_shift
= 32;
9290 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9291 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9293 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9294 cookie
.locsymcount
, 0,
9296 if (cookie
.locsyms
== NULL
)
9303 count
= stab
->reloc_count
;
9305 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9307 if (cookie
.rels
!= NULL
)
9309 cookie
.rel
= cookie
.rels
;
9310 cookie
.relend
= cookie
.rels
;
9311 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9312 if (_bfd_discard_section_stabs (abfd
, stab
,
9313 elf_section_data (stab
)->sec_info
,
9314 bfd_elf_reloc_symbol_deleted_p
,
9317 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9325 count
= eh
->reloc_count
;
9327 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9329 cookie
.rel
= cookie
.rels
;
9330 cookie
.relend
= cookie
.rels
;
9331 if (cookie
.rels
!= NULL
)
9332 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9334 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9335 bfd_elf_reloc_symbol_deleted_p
,
9339 if (cookie
.rels
!= NULL
9340 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9344 if (bed
->elf_backend_discard_info
!= NULL
9345 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9348 if (cookie
.locsyms
!= NULL
9349 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9351 if (! info
->keep_memory
)
9352 free (cookie
.locsyms
);
9354 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9358 if (info
->eh_frame_hdr
9359 && !info
->relocatable
9360 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9367 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9370 const char *name
, *p
;
9371 struct bfd_section_already_linked
*l
;
9372 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9375 /* A single member comdat group section may be discarded by a
9376 linkonce section. See below. */
9377 if (sec
->output_section
== bfd_abs_section_ptr
)
9382 /* Check if it belongs to a section group. */
9383 group
= elf_sec_group (sec
);
9385 /* Return if it isn't a linkonce section nor a member of a group. A
9386 comdat group section also has SEC_LINK_ONCE set. */
9387 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9392 /* If this is the member of a single member comdat group, check if
9393 the group should be discarded. */
9394 if (elf_next_in_group (sec
) == sec
9395 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9401 /* FIXME: When doing a relocatable link, we may have trouble
9402 copying relocations in other sections that refer to local symbols
9403 in the section being discarded. Those relocations will have to
9404 be converted somehow; as of this writing I'm not sure that any of
9405 the backends handle that correctly.
9407 It is tempting to instead not discard link once sections when
9408 doing a relocatable link (technically, they should be discarded
9409 whenever we are building constructors). However, that fails,
9410 because the linker winds up combining all the link once sections
9411 into a single large link once section, which defeats the purpose
9412 of having link once sections in the first place.
9414 Also, not merging link once sections in a relocatable link
9415 causes trouble for MIPS ELF, which relies on link once semantics
9416 to handle the .reginfo section correctly. */
9418 name
= bfd_get_section_name (abfd
, sec
);
9420 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9421 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9426 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9428 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9430 /* We may have 3 different sections on the list: group section,
9431 comdat section and linkonce section. SEC may be a linkonce or
9432 group section. We match a group section with a group section,
9433 a linkonce section with a linkonce section, and ignore comdat
9435 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9436 && strcmp (name
, l
->sec
->name
) == 0
9437 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9439 /* The section has already been linked. See if we should
9441 switch (flags
& SEC_LINK_DUPLICATES
)
9446 case SEC_LINK_DUPLICATES_DISCARD
:
9449 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9450 (*_bfd_error_handler
)
9451 (_("%B: ignoring duplicate section `%A'\n"),
9455 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9456 if (sec
->size
!= l
->sec
->size
)
9457 (*_bfd_error_handler
)
9458 (_("%B: duplicate section `%A' has different size\n"),
9462 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9463 if (sec
->size
!= l
->sec
->size
)
9464 (*_bfd_error_handler
)
9465 (_("%B: duplicate section `%A' has different size\n"),
9467 else if (sec
->size
!= 0)
9469 bfd_byte
*sec_contents
, *l_sec_contents
;
9471 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9472 (*_bfd_error_handler
)
9473 (_("%B: warning: could not read contents of section `%A'\n"),
9475 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9477 (*_bfd_error_handler
)
9478 (_("%B: warning: could not read contents of section `%A'\n"),
9479 l
->sec
->owner
, l
->sec
);
9480 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9481 (*_bfd_error_handler
)
9482 (_("%B: warning: duplicate section `%A' has different contents\n"),
9486 free (sec_contents
);
9488 free (l_sec_contents
);
9493 /* Set the output_section field so that lang_add_section
9494 does not create a lang_input_section structure for this
9495 section. Since there might be a symbol in the section
9496 being discarded, we must retain a pointer to the section
9497 which we are really going to use. */
9498 sec
->output_section
= bfd_abs_section_ptr
;
9499 sec
->kept_section
= l
->sec
;
9501 if (flags
& SEC_GROUP
)
9503 asection
*first
= elf_next_in_group (sec
);
9504 asection
*s
= first
;
9508 s
->output_section
= bfd_abs_section_ptr
;
9509 /* Record which group discards it. */
9510 s
->kept_section
= l
->sec
;
9511 s
= elf_next_in_group (s
);
9512 /* These lists are circular. */
9524 /* If this is the member of a single member comdat group and the
9525 group hasn't be discarded, we check if it matches a linkonce
9526 section. We only record the discarded comdat group. Otherwise
9527 the undiscarded group will be discarded incorrectly later since
9528 itself has been recorded. */
9529 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9530 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9531 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9532 && bfd_elf_match_symbols_in_sections (l
->sec
,
9533 elf_next_in_group (sec
)))
9535 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9536 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9537 group
->output_section
= bfd_abs_section_ptr
;
9544 /* There is no direct match. But for linkonce section, we should
9545 check if there is a match with comdat group member. We always
9546 record the linkonce section, discarded or not. */
9547 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9548 if (l
->sec
->flags
& SEC_GROUP
)
9550 asection
*first
= elf_next_in_group (l
->sec
);
9553 && elf_next_in_group (first
) == first
9554 && bfd_elf_match_symbols_in_sections (first
, sec
))
9556 sec
->output_section
= bfd_abs_section_ptr
;
9557 sec
->kept_section
= l
->sec
;
9562 /* This is the first section with this name. Record it. */
9563 bfd_section_already_linked_table_insert (already_linked_list
, sec
);