1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
35 struct elf_link_hash_entry
*h
;
36 struct bfd_link_hash_entry
*bh
;
37 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
40 /* This function may be called more than once. */
41 s
= bfd_get_section_by_name (abfd
, ".got");
42 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
45 switch (bed
->s
->arch_size
)
56 bfd_set_error (bfd_error_bad_value
);
60 flags
= bed
->dynamic_sec_flags
;
62 s
= bfd_make_section (abfd
, ".got");
64 || !bfd_set_section_flags (abfd
, s
, flags
)
65 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
68 if (bed
->want_got_plt
)
70 s
= bfd_make_section (abfd
, ".got.plt");
72 || !bfd_set_section_flags (abfd
, s
, flags
)
73 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
77 if (bed
->want_got_sym
)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
86 bed
->got_symbol_offset
, NULL
, FALSE
, bed
->collect
, &bh
)))
88 h
= (struct elf_link_hash_entry
*) bh
;
91 h
->other
= STV_HIDDEN
;
93 if (! info
->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
97 elf_hash_table (info
)->hgot
= h
;
100 /* The first bit of the global offset table is the header. */
101 s
->size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
106 /* Create a strtab to hold the dynamic symbol names. */
108 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
110 struct elf_link_hash_table
*hash_table
;
112 hash_table
= elf_hash_table (info
);
113 if (hash_table
->dynobj
== NULL
)
114 hash_table
->dynobj
= abfd
;
116 if (hash_table
->dynstr
== NULL
)
118 hash_table
->dynstr
= _bfd_elf_strtab_init ();
119 if (hash_table
->dynstr
== NULL
)
125 /* Create some sections which will be filled in with dynamic linking
126 information. ABFD is an input file which requires dynamic sections
127 to be created. The dynamic sections take up virtual memory space
128 when the final executable is run, so we need to create them before
129 addresses are assigned to the output sections. We work out the
130 actual contents and size of these sections later. */
133 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
136 register asection
*s
;
137 struct elf_link_hash_entry
*h
;
138 struct bfd_link_hash_entry
*bh
;
139 const struct elf_backend_data
*bed
;
141 if (! is_elf_hash_table (info
->hash
))
144 if (elf_hash_table (info
)->dynamic_sections_created
)
147 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
150 abfd
= elf_hash_table (info
)->dynobj
;
151 bed
= get_elf_backend_data (abfd
);
153 flags
= bed
->dynamic_sec_flags
;
155 /* A dynamically linked executable has a .interp section, but a
156 shared library does not. */
157 if (info
->executable
)
159 s
= bfd_make_section (abfd
, ".interp");
161 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
165 if (! info
->traditional_format
)
167 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
169 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
170 || ! bfd_set_section_alignment (abfd
, s
, 2))
172 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
175 /* Create sections to hold version informations. These are removed
176 if they are not needed. */
177 s
= bfd_make_section (abfd
, ".gnu.version_d");
179 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
180 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
183 s
= bfd_make_section (abfd
, ".gnu.version");
185 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
186 || ! bfd_set_section_alignment (abfd
, s
, 1))
189 s
= bfd_make_section (abfd
, ".gnu.version_r");
191 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
192 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
195 s
= bfd_make_section (abfd
, ".dynsym");
197 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
198 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
201 s
= bfd_make_section (abfd
, ".dynstr");
203 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
206 s
= bfd_make_section (abfd
, ".dynamic");
208 || ! bfd_set_section_flags (abfd
, s
, flags
)
209 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
212 /* The special symbol _DYNAMIC is always set to the start of the
213 .dynamic section. We could set _DYNAMIC in a linker script, but we
214 only want to define it if we are, in fact, creating a .dynamic
215 section. We don't want to define it if there is no .dynamic
216 section, since on some ELF platforms the start up code examines it
217 to decide how to initialize the process. */
218 h
= elf_link_hash_lookup (elf_hash_table (info
), "_DYNAMIC",
219 FALSE
, FALSE
, FALSE
);
222 /* Zap symbol defined in an as-needed lib that wasn't linked.
223 This is a symptom of a larger problem: Absolute symbols
224 defined in shared libraries can't be overridden, because we
225 lose the link to the bfd which is via the symbol section. */
226 h
->root
.type
= bfd_link_hash_new
;
229 if (! (_bfd_generic_link_add_one_symbol
230 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
231 get_elf_backend_data (abfd
)->collect
, &bh
)))
233 h
= (struct elf_link_hash_entry
*) bh
;
235 h
->type
= STT_OBJECT
;
237 if (! info
->executable
238 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
241 s
= bfd_make_section (abfd
, ".hash");
243 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
244 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
246 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
248 /* Let the backend create the rest of the sections. This lets the
249 backend set the right flags. The backend will normally create
250 the .got and .plt sections. */
251 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
254 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
259 /* Create dynamic sections when linking against a dynamic object. */
262 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
264 flagword flags
, pltflags
;
266 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
268 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
269 .rel[a].bss sections. */
270 flags
= bed
->dynamic_sec_flags
;
273 if (bed
->plt_not_loaded
)
274 /* We do not clear SEC_ALLOC here because we still want the OS to
275 allocate space for the section; it's just that there's nothing
276 to read in from the object file. */
277 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
279 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
280 if (bed
->plt_readonly
)
281 pltflags
|= SEC_READONLY
;
283 s
= bfd_make_section (abfd
, ".plt");
285 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
286 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
289 if (bed
->want_plt_sym
)
291 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
293 struct elf_link_hash_entry
*h
;
294 struct bfd_link_hash_entry
*bh
= NULL
;
296 if (! (_bfd_generic_link_add_one_symbol
297 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
298 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
300 h
= (struct elf_link_hash_entry
*) bh
;
302 h
->type
= STT_OBJECT
;
304 if (! info
->executable
305 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
309 s
= bfd_make_section (abfd
,
310 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
312 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
313 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
316 if (! _bfd_elf_create_got_section (abfd
, info
))
319 if (bed
->want_dynbss
)
321 /* The .dynbss section is a place to put symbols which are defined
322 by dynamic objects, are referenced by regular objects, and are
323 not functions. We must allocate space for them in the process
324 image and use a R_*_COPY reloc to tell the dynamic linker to
325 initialize them at run time. The linker script puts the .dynbss
326 section into the .bss section of the final image. */
327 s
= bfd_make_section (abfd
, ".dynbss");
329 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
332 /* The .rel[a].bss section holds copy relocs. This section is not
333 normally needed. We need to create it here, though, so that the
334 linker will map it to an output section. We can't just create it
335 only if we need it, because we will not know whether we need it
336 until we have seen all the input files, and the first time the
337 main linker code calls BFD after examining all the input files
338 (size_dynamic_sections) the input sections have already been
339 mapped to the output sections. If the section turns out not to
340 be needed, we can discard it later. We will never need this
341 section when generating a shared object, since they do not use
345 s
= bfd_make_section (abfd
,
346 (bed
->default_use_rela_p
347 ? ".rela.bss" : ".rel.bss"));
349 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
350 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
358 /* Record a new dynamic symbol. We record the dynamic symbols as we
359 read the input files, since we need to have a list of all of them
360 before we can determine the final sizes of the output sections.
361 Note that we may actually call this function even though we are not
362 going to output any dynamic symbols; in some cases we know that a
363 symbol should be in the dynamic symbol table, but only if there is
367 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
368 struct elf_link_hash_entry
*h
)
370 if (h
->dynindx
== -1)
372 struct elf_strtab_hash
*dynstr
;
377 /* XXX: The ABI draft says the linker must turn hidden and
378 internal symbols into STB_LOCAL symbols when producing the
379 DSO. However, if ld.so honors st_other in the dynamic table,
380 this would not be necessary. */
381 switch (ELF_ST_VISIBILITY (h
->other
))
385 if (h
->root
.type
!= bfd_link_hash_undefined
386 && h
->root
.type
!= bfd_link_hash_undefweak
)
389 if (!elf_hash_table (info
)->is_relocatable_executable
)
397 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
398 ++elf_hash_table (info
)->dynsymcount
;
400 dynstr
= elf_hash_table (info
)->dynstr
;
403 /* Create a strtab to hold the dynamic symbol names. */
404 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
409 /* We don't put any version information in the dynamic string
411 name
= h
->root
.root
.string
;
412 p
= strchr (name
, ELF_VER_CHR
);
414 /* We know that the p points into writable memory. In fact,
415 there are only a few symbols that have read-only names, being
416 those like _GLOBAL_OFFSET_TABLE_ that are created specially
417 by the backends. Most symbols will have names pointing into
418 an ELF string table read from a file, or to objalloc memory. */
421 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
426 if (indx
== (bfd_size_type
) -1)
428 h
->dynstr_index
= indx
;
434 /* Record an assignment to a symbol made by a linker script. We need
435 this in case some dynamic object refers to this symbol. */
438 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
439 struct bfd_link_info
*info
,
443 struct elf_link_hash_entry
*h
;
444 struct elf_link_hash_table
*htab
;
446 if (!is_elf_hash_table (info
->hash
))
449 htab
= elf_hash_table (info
);
450 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
454 /* Since we're defining the symbol, don't let it seem to have not
455 been defined. record_dynamic_symbol and size_dynamic_sections
456 may depend on this. */
457 if (h
->root
.type
== bfd_link_hash_undefweak
458 || h
->root
.type
== bfd_link_hash_undefined
)
460 h
->root
.type
= bfd_link_hash_new
;
461 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
462 bfd_link_repair_undef_list (&htab
->root
);
465 if (h
->root
.type
== bfd_link_hash_new
)
468 /* If this symbol is being provided by the linker script, and it is
469 currently defined by a dynamic object, but not by a regular
470 object, then mark it as undefined so that the generic linker will
471 force the correct value. */
475 h
->root
.type
= bfd_link_hash_undefined
;
477 /* If this symbol is not being provided by the linker script, and it is
478 currently defined by a dynamic object, but not by a regular object,
479 then clear out any version information because the symbol will not be
480 associated with the dynamic object any more. */
484 h
->verinfo
.verdef
= NULL
;
488 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
490 if (!info
->relocatable
492 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
493 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
499 || (info
->executable
&& elf_hash_table (info
)->is_relocatable_executable
))
502 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
505 /* If this is a weak defined symbol, and we know a corresponding
506 real symbol from the same dynamic object, make sure the real
507 symbol is also made into a dynamic symbol. */
508 if (h
->u
.weakdef
!= NULL
509 && h
->u
.weakdef
->dynindx
== -1)
511 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
519 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
520 success, and 2 on a failure caused by attempting to record a symbol
521 in a discarded section, eg. a discarded link-once section symbol. */
524 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
529 struct elf_link_local_dynamic_entry
*entry
;
530 struct elf_link_hash_table
*eht
;
531 struct elf_strtab_hash
*dynstr
;
532 unsigned long dynstr_index
;
534 Elf_External_Sym_Shndx eshndx
;
535 char esym
[sizeof (Elf64_External_Sym
)];
537 if (! is_elf_hash_table (info
->hash
))
540 /* See if the entry exists already. */
541 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
542 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
545 amt
= sizeof (*entry
);
546 entry
= bfd_alloc (input_bfd
, amt
);
550 /* Go find the symbol, so that we can find it's name. */
551 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
552 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
554 bfd_release (input_bfd
, entry
);
558 if (entry
->isym
.st_shndx
!= SHN_UNDEF
559 && (entry
->isym
.st_shndx
< SHN_LORESERVE
560 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
564 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
565 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
567 /* We can still bfd_release here as nothing has done another
568 bfd_alloc. We can't do this later in this function. */
569 bfd_release (input_bfd
, entry
);
574 name
= (bfd_elf_string_from_elf_section
575 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
576 entry
->isym
.st_name
));
578 dynstr
= elf_hash_table (info
)->dynstr
;
581 /* Create a strtab to hold the dynamic symbol names. */
582 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
587 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
588 if (dynstr_index
== (unsigned long) -1)
590 entry
->isym
.st_name
= dynstr_index
;
592 eht
= elf_hash_table (info
);
594 entry
->next
= eht
->dynlocal
;
595 eht
->dynlocal
= entry
;
596 entry
->input_bfd
= input_bfd
;
597 entry
->input_indx
= input_indx
;
600 /* Whatever binding the symbol had before, it's now local. */
602 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
604 /* The dynindx will be set at the end of size_dynamic_sections. */
609 /* Return the dynindex of a local dynamic symbol. */
612 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
616 struct elf_link_local_dynamic_entry
*e
;
618 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
619 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
624 /* This function is used to renumber the dynamic symbols, if some of
625 them are removed because they are marked as local. This is called
626 via elf_link_hash_traverse. */
629 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
632 size_t *count
= data
;
634 if (h
->root
.type
== bfd_link_hash_warning
)
635 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
640 if (h
->dynindx
!= -1)
641 h
->dynindx
= ++(*count
);
647 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
648 STB_LOCAL binding. */
651 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
654 size_t *count
= data
;
656 if (h
->root
.type
== bfd_link_hash_warning
)
657 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
659 if (!h
->forced_local
)
662 if (h
->dynindx
!= -1)
663 h
->dynindx
= ++(*count
);
668 /* Return true if the dynamic symbol for a given section should be
669 omitted when creating a shared library. */
671 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
672 struct bfd_link_info
*info
,
675 switch (elf_section_data (p
)->this_hdr
.sh_type
)
679 /* If sh_type is yet undecided, assume it could be
680 SHT_PROGBITS/SHT_NOBITS. */
682 if (strcmp (p
->name
, ".got") == 0
683 || strcmp (p
->name
, ".got.plt") == 0
684 || strcmp (p
->name
, ".plt") == 0)
687 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
690 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
691 && (ip
->flags
& SEC_LINKER_CREATED
)
692 && ip
->output_section
== p
)
697 /* There shouldn't be section relative relocations
698 against any other section. */
704 /* Assign dynsym indices. In a shared library we generate a section
705 symbol for each output section, which come first. Next come symbols
706 which have been forced to local binding. Then all of the back-end
707 allocated local dynamic syms, followed by the rest of the global
711 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
713 unsigned long dynsymcount
= 0;
715 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
717 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
719 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
720 if ((p
->flags
& SEC_EXCLUDE
) == 0
721 && (p
->flags
& SEC_ALLOC
) != 0
722 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
723 elf_section_data (p
)->dynindx
= ++dynsymcount
;
726 elf_link_hash_traverse (elf_hash_table (info
),
727 elf_link_renumber_local_hash_table_dynsyms
,
730 if (elf_hash_table (info
)->dynlocal
)
732 struct elf_link_local_dynamic_entry
*p
;
733 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
734 p
->dynindx
= ++dynsymcount
;
737 elf_link_hash_traverse (elf_hash_table (info
),
738 elf_link_renumber_hash_table_dynsyms
,
741 /* There is an unused NULL entry at the head of the table which
742 we must account for in our count. Unless there weren't any
743 symbols, which means we'll have no table at all. */
744 if (dynsymcount
!= 0)
747 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
750 /* This function is called when we want to define a new symbol. It
751 handles the various cases which arise when we find a definition in
752 a dynamic object, or when there is already a definition in a
753 dynamic object. The new symbol is described by NAME, SYM, PSEC,
754 and PVALUE. We set SYM_HASH to the hash table entry. We set
755 OVERRIDE if the old symbol is overriding a new definition. We set
756 TYPE_CHANGE_OK if it is OK for the type to change. We set
757 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
758 change, we mean that we shouldn't warn if the type or size does
762 _bfd_elf_merge_symbol (bfd
*abfd
,
763 struct bfd_link_info
*info
,
765 Elf_Internal_Sym
*sym
,
768 struct elf_link_hash_entry
**sym_hash
,
770 bfd_boolean
*override
,
771 bfd_boolean
*type_change_ok
,
772 bfd_boolean
*size_change_ok
)
774 asection
*sec
, *oldsec
;
775 struct elf_link_hash_entry
*h
;
776 struct elf_link_hash_entry
*flip
;
779 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
780 bfd_boolean newweak
, oldweak
;
786 bind
= ELF_ST_BIND (sym
->st_info
);
788 if (! bfd_is_und_section (sec
))
789 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
791 h
= ((struct elf_link_hash_entry
*)
792 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
797 /* This code is for coping with dynamic objects, and is only useful
798 if we are doing an ELF link. */
799 if (info
->hash
->creator
!= abfd
->xvec
)
802 /* For merging, we only care about real symbols. */
804 while (h
->root
.type
== bfd_link_hash_indirect
805 || h
->root
.type
== bfd_link_hash_warning
)
806 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
808 /* If we just created the symbol, mark it as being an ELF symbol.
809 Other than that, there is nothing to do--there is no merge issue
810 with a newly defined symbol--so we just return. */
812 if (h
->root
.type
== bfd_link_hash_new
)
818 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
821 switch (h
->root
.type
)
828 case bfd_link_hash_undefined
:
829 case bfd_link_hash_undefweak
:
830 oldbfd
= h
->root
.u
.undef
.abfd
;
834 case bfd_link_hash_defined
:
835 case bfd_link_hash_defweak
:
836 oldbfd
= h
->root
.u
.def
.section
->owner
;
837 oldsec
= h
->root
.u
.def
.section
;
840 case bfd_link_hash_common
:
841 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
842 oldsec
= h
->root
.u
.c
.p
->section
;
846 /* In cases involving weak versioned symbols, we may wind up trying
847 to merge a symbol with itself. Catch that here, to avoid the
848 confusion that results if we try to override a symbol with
849 itself. The additional tests catch cases like
850 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
851 dynamic object, which we do want to handle here. */
853 && ((abfd
->flags
& DYNAMIC
) == 0
857 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
858 respectively, is from a dynamic object. */
860 if ((abfd
->flags
& DYNAMIC
) != 0)
866 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
871 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
872 indices used by MIPS ELF. */
873 switch (h
->root
.type
)
879 case bfd_link_hash_defined
:
880 case bfd_link_hash_defweak
:
881 hsec
= h
->root
.u
.def
.section
;
884 case bfd_link_hash_common
:
885 hsec
= h
->root
.u
.c
.p
->section
;
892 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
895 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
896 respectively, appear to be a definition rather than reference. */
898 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
903 if (h
->root
.type
== bfd_link_hash_undefined
904 || h
->root
.type
== bfd_link_hash_undefweak
905 || h
->root
.type
== bfd_link_hash_common
)
910 /* Check TLS symbol. */
911 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
912 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
915 bfd_boolean ntdef
, tdef
;
916 asection
*ntsec
, *tsec
;
918 if (h
->type
== STT_TLS
)
938 (*_bfd_error_handler
)
939 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
940 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
941 else if (!tdef
&& !ntdef
)
942 (*_bfd_error_handler
)
943 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
944 tbfd
, ntbfd
, h
->root
.root
.string
);
946 (*_bfd_error_handler
)
947 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
948 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
950 (*_bfd_error_handler
)
951 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
952 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
954 bfd_set_error (bfd_error_bad_value
);
958 /* We need to remember if a symbol has a definition in a dynamic
959 object or is weak in all dynamic objects. Internal and hidden
960 visibility will make it unavailable to dynamic objects. */
961 if (newdyn
&& !h
->dynamic_def
)
963 if (!bfd_is_und_section (sec
))
967 /* Check if this symbol is weak in all dynamic objects. If it
968 is the first time we see it in a dynamic object, we mark
969 if it is weak. Otherwise, we clear it. */
972 if (bind
== STB_WEAK
)
975 else if (bind
!= STB_WEAK
)
980 /* If the old symbol has non-default visibility, we ignore the new
981 definition from a dynamic object. */
983 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
984 && !bfd_is_und_section (sec
))
987 /* Make sure this symbol is dynamic. */
989 /* A protected symbol has external availability. Make sure it is
992 FIXME: Should we check type and size for protected symbol? */
993 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
994 return bfd_elf_link_record_dynamic_symbol (info
, h
);
999 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1002 /* If the new symbol with non-default visibility comes from a
1003 relocatable file and the old definition comes from a dynamic
1004 object, we remove the old definition. */
1005 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1008 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1009 && bfd_is_und_section (sec
))
1011 /* If the new symbol is undefined and the old symbol was
1012 also undefined before, we need to make sure
1013 _bfd_generic_link_add_one_symbol doesn't mess
1014 up the linker hash table undefs list. Since the old
1015 definition came from a dynamic object, it is still on the
1017 h
->root
.type
= bfd_link_hash_undefined
;
1018 h
->root
.u
.undef
.abfd
= abfd
;
1022 h
->root
.type
= bfd_link_hash_new
;
1023 h
->root
.u
.undef
.abfd
= NULL
;
1032 /* FIXME: Should we check type and size for protected symbol? */
1038 /* Differentiate strong and weak symbols. */
1039 newweak
= bind
== STB_WEAK
;
1040 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1041 || h
->root
.type
== bfd_link_hash_undefweak
);
1043 /* If a new weak symbol definition comes from a regular file and the
1044 old symbol comes from a dynamic library, we treat the new one as
1045 strong. Similarly, an old weak symbol definition from a regular
1046 file is treated as strong when the new symbol comes from a dynamic
1047 library. Further, an old weak symbol from a dynamic library is
1048 treated as strong if the new symbol is from a dynamic library.
1049 This reflects the way glibc's ld.so works.
1051 Do this before setting *type_change_ok or *size_change_ok so that
1052 we warn properly when dynamic library symbols are overridden. */
1054 if (newdef
&& !newdyn
&& olddyn
)
1056 if (olddef
&& newdyn
)
1059 /* It's OK to change the type if either the existing symbol or the
1060 new symbol is weak. A type change is also OK if the old symbol
1061 is undefined and the new symbol is defined. */
1066 && h
->root
.type
== bfd_link_hash_undefined
))
1067 *type_change_ok
= TRUE
;
1069 /* It's OK to change the size if either the existing symbol or the
1070 new symbol is weak, or if the old symbol is undefined. */
1073 || h
->root
.type
== bfd_link_hash_undefined
)
1074 *size_change_ok
= TRUE
;
1076 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1077 symbol, respectively, appears to be a common symbol in a dynamic
1078 object. If a symbol appears in an uninitialized section, and is
1079 not weak, and is not a function, then it may be a common symbol
1080 which was resolved when the dynamic object was created. We want
1081 to treat such symbols specially, because they raise special
1082 considerations when setting the symbol size: if the symbol
1083 appears as a common symbol in a regular object, and the size in
1084 the regular object is larger, we must make sure that we use the
1085 larger size. This problematic case can always be avoided in C,
1086 but it must be handled correctly when using Fortran shared
1089 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1090 likewise for OLDDYNCOMMON and OLDDEF.
1092 Note that this test is just a heuristic, and that it is quite
1093 possible to have an uninitialized symbol in a shared object which
1094 is really a definition, rather than a common symbol. This could
1095 lead to some minor confusion when the symbol really is a common
1096 symbol in some regular object. However, I think it will be
1102 && (sec
->flags
& SEC_ALLOC
) != 0
1103 && (sec
->flags
& SEC_LOAD
) == 0
1105 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1106 newdyncommon
= TRUE
;
1108 newdyncommon
= FALSE
;
1112 && h
->root
.type
== bfd_link_hash_defined
1114 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1115 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1117 && h
->type
!= STT_FUNC
)
1118 olddyncommon
= TRUE
;
1120 olddyncommon
= FALSE
;
1122 /* If both the old and the new symbols look like common symbols in a
1123 dynamic object, set the size of the symbol to the larger of the
1128 && sym
->st_size
!= h
->size
)
1130 /* Since we think we have two common symbols, issue a multiple
1131 common warning if desired. Note that we only warn if the
1132 size is different. If the size is the same, we simply let
1133 the old symbol override the new one as normally happens with
1134 symbols defined in dynamic objects. */
1136 if (! ((*info
->callbacks
->multiple_common
)
1137 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1138 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1141 if (sym
->st_size
> h
->size
)
1142 h
->size
= sym
->st_size
;
1144 *size_change_ok
= TRUE
;
1147 /* If we are looking at a dynamic object, and we have found a
1148 definition, we need to see if the symbol was already defined by
1149 some other object. If so, we want to use the existing
1150 definition, and we do not want to report a multiple symbol
1151 definition error; we do this by clobbering *PSEC to be
1152 bfd_und_section_ptr.
1154 We treat a common symbol as a definition if the symbol in the
1155 shared library is a function, since common symbols always
1156 represent variables; this can cause confusion in principle, but
1157 any such confusion would seem to indicate an erroneous program or
1158 shared library. We also permit a common symbol in a regular
1159 object to override a weak symbol in a shared object. */
1164 || (h
->root
.type
== bfd_link_hash_common
1166 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1170 newdyncommon
= FALSE
;
1172 *psec
= sec
= bfd_und_section_ptr
;
1173 *size_change_ok
= TRUE
;
1175 /* If we get here when the old symbol is a common symbol, then
1176 we are explicitly letting it override a weak symbol or
1177 function in a dynamic object, and we don't want to warn about
1178 a type change. If the old symbol is a defined symbol, a type
1179 change warning may still be appropriate. */
1181 if (h
->root
.type
== bfd_link_hash_common
)
1182 *type_change_ok
= TRUE
;
1185 /* Handle the special case of an old common symbol merging with a
1186 new symbol which looks like a common symbol in a shared object.
1187 We change *PSEC and *PVALUE to make the new symbol look like a
1188 common symbol, and let _bfd_generic_link_add_one_symbol will do
1192 && h
->root
.type
== bfd_link_hash_common
)
1196 newdyncommon
= FALSE
;
1197 *pvalue
= sym
->st_size
;
1198 *psec
= sec
= bfd_com_section_ptr
;
1199 *size_change_ok
= TRUE
;
1202 /* If the old symbol is from a dynamic object, and the new symbol is
1203 a definition which is not from a dynamic object, then the new
1204 symbol overrides the old symbol. Symbols from regular files
1205 always take precedence over symbols from dynamic objects, even if
1206 they are defined after the dynamic object in the link.
1208 As above, we again permit a common symbol in a regular object to
1209 override a definition in a shared object if the shared object
1210 symbol is a function or is weak. */
1215 || (bfd_is_com_section (sec
)
1217 || h
->type
== STT_FUNC
)))
1222 /* Change the hash table entry to undefined, and let
1223 _bfd_generic_link_add_one_symbol do the right thing with the
1226 h
->root
.type
= bfd_link_hash_undefined
;
1227 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1228 *size_change_ok
= TRUE
;
1231 olddyncommon
= FALSE
;
1233 /* We again permit a type change when a common symbol may be
1234 overriding a function. */
1236 if (bfd_is_com_section (sec
))
1237 *type_change_ok
= TRUE
;
1239 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1242 /* This union may have been set to be non-NULL when this symbol
1243 was seen in a dynamic object. We must force the union to be
1244 NULL, so that it is correct for a regular symbol. */
1245 h
->verinfo
.vertree
= NULL
;
1248 /* Handle the special case of a new common symbol merging with an
1249 old symbol that looks like it might be a common symbol defined in
1250 a shared object. Note that we have already handled the case in
1251 which a new common symbol should simply override the definition
1252 in the shared library. */
1255 && bfd_is_com_section (sec
)
1258 /* It would be best if we could set the hash table entry to a
1259 common symbol, but we don't know what to use for the section
1260 or the alignment. */
1261 if (! ((*info
->callbacks
->multiple_common
)
1262 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1263 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1266 /* If the presumed common symbol in the dynamic object is
1267 larger, pretend that the new symbol has its size. */
1269 if (h
->size
> *pvalue
)
1272 /* FIXME: We no longer know the alignment required by the symbol
1273 in the dynamic object, so we just wind up using the one from
1274 the regular object. */
1277 olddyncommon
= FALSE
;
1279 h
->root
.type
= bfd_link_hash_undefined
;
1280 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1282 *size_change_ok
= TRUE
;
1283 *type_change_ok
= TRUE
;
1285 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1288 h
->verinfo
.vertree
= NULL
;
1293 /* Handle the case where we had a versioned symbol in a dynamic
1294 library and now find a definition in a normal object. In this
1295 case, we make the versioned symbol point to the normal one. */
1296 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1297 flip
->root
.type
= h
->root
.type
;
1298 h
->root
.type
= bfd_link_hash_indirect
;
1299 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1300 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1301 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1305 flip
->ref_dynamic
= 1;
1312 /* This function is called to create an indirect symbol from the
1313 default for the symbol with the default version if needed. The
1314 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1315 set DYNSYM if the new indirect symbol is dynamic. */
1318 _bfd_elf_add_default_symbol (bfd
*abfd
,
1319 struct bfd_link_info
*info
,
1320 struct elf_link_hash_entry
*h
,
1322 Elf_Internal_Sym
*sym
,
1325 bfd_boolean
*dynsym
,
1326 bfd_boolean override
)
1328 bfd_boolean type_change_ok
;
1329 bfd_boolean size_change_ok
;
1332 struct elf_link_hash_entry
*hi
;
1333 struct bfd_link_hash_entry
*bh
;
1334 const struct elf_backend_data
*bed
;
1335 bfd_boolean collect
;
1336 bfd_boolean dynamic
;
1338 size_t len
, shortlen
;
1341 /* If this symbol has a version, and it is the default version, we
1342 create an indirect symbol from the default name to the fully
1343 decorated name. This will cause external references which do not
1344 specify a version to be bound to this version of the symbol. */
1345 p
= strchr (name
, ELF_VER_CHR
);
1346 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1351 /* We are overridden by an old definition. We need to check if we
1352 need to create the indirect symbol from the default name. */
1353 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1355 BFD_ASSERT (hi
!= NULL
);
1358 while (hi
->root
.type
== bfd_link_hash_indirect
1359 || hi
->root
.type
== bfd_link_hash_warning
)
1361 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1367 bed
= get_elf_backend_data (abfd
);
1368 collect
= bed
->collect
;
1369 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1371 shortlen
= p
- name
;
1372 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1373 if (shortname
== NULL
)
1375 memcpy (shortname
, name
, shortlen
);
1376 shortname
[shortlen
] = '\0';
1378 /* We are going to create a new symbol. Merge it with any existing
1379 symbol with this name. For the purposes of the merge, act as
1380 though we were defining the symbol we just defined, although we
1381 actually going to define an indirect symbol. */
1382 type_change_ok
= FALSE
;
1383 size_change_ok
= FALSE
;
1385 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1386 &hi
, &skip
, &override
, &type_change_ok
,
1396 if (! (_bfd_generic_link_add_one_symbol
1397 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1398 0, name
, FALSE
, collect
, &bh
)))
1400 hi
= (struct elf_link_hash_entry
*) bh
;
1404 /* In this case the symbol named SHORTNAME is overriding the
1405 indirect symbol we want to add. We were planning on making
1406 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1407 is the name without a version. NAME is the fully versioned
1408 name, and it is the default version.
1410 Overriding means that we already saw a definition for the
1411 symbol SHORTNAME in a regular object, and it is overriding
1412 the symbol defined in the dynamic object.
1414 When this happens, we actually want to change NAME, the
1415 symbol we just added, to refer to SHORTNAME. This will cause
1416 references to NAME in the shared object to become references
1417 to SHORTNAME in the regular object. This is what we expect
1418 when we override a function in a shared object: that the
1419 references in the shared object will be mapped to the
1420 definition in the regular object. */
1422 while (hi
->root
.type
== bfd_link_hash_indirect
1423 || hi
->root
.type
== bfd_link_hash_warning
)
1424 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1426 h
->root
.type
= bfd_link_hash_indirect
;
1427 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1431 hi
->ref_dynamic
= 1;
1435 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1440 /* Now set HI to H, so that the following code will set the
1441 other fields correctly. */
1445 /* If there is a duplicate definition somewhere, then HI may not
1446 point to an indirect symbol. We will have reported an error to
1447 the user in that case. */
1449 if (hi
->root
.type
== bfd_link_hash_indirect
)
1451 struct elf_link_hash_entry
*ht
;
1453 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1454 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1456 /* See if the new flags lead us to realize that the symbol must
1468 if (hi
->ref_regular
)
1474 /* We also need to define an indirection from the nondefault version
1478 len
= strlen (name
);
1479 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1480 if (shortname
== NULL
)
1482 memcpy (shortname
, name
, shortlen
);
1483 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1485 /* Once again, merge with any existing symbol. */
1486 type_change_ok
= FALSE
;
1487 size_change_ok
= FALSE
;
1489 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1490 &hi
, &skip
, &override
, &type_change_ok
,
1499 /* Here SHORTNAME is a versioned name, so we don't expect to see
1500 the type of override we do in the case above unless it is
1501 overridden by a versioned definition. */
1502 if (hi
->root
.type
!= bfd_link_hash_defined
1503 && hi
->root
.type
!= bfd_link_hash_defweak
)
1504 (*_bfd_error_handler
)
1505 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1511 if (! (_bfd_generic_link_add_one_symbol
1512 (info
, abfd
, shortname
, BSF_INDIRECT
,
1513 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1515 hi
= (struct elf_link_hash_entry
*) bh
;
1517 /* If there is a duplicate definition somewhere, then HI may not
1518 point to an indirect symbol. We will have reported an error
1519 to the user in that case. */
1521 if (hi
->root
.type
== bfd_link_hash_indirect
)
1523 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1525 /* See if the new flags lead us to realize that the symbol
1537 if (hi
->ref_regular
)
1547 /* This routine is used to export all defined symbols into the dynamic
1548 symbol table. It is called via elf_link_hash_traverse. */
1551 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1553 struct elf_info_failed
*eif
= data
;
1555 /* Ignore indirect symbols. These are added by the versioning code. */
1556 if (h
->root
.type
== bfd_link_hash_indirect
)
1559 if (h
->root
.type
== bfd_link_hash_warning
)
1560 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1562 if (h
->dynindx
== -1
1566 struct bfd_elf_version_tree
*t
;
1567 struct bfd_elf_version_expr
*d
;
1569 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1571 if (t
->globals
.list
!= NULL
)
1573 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1578 if (t
->locals
.list
!= NULL
)
1580 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1589 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1600 /* Look through the symbols which are defined in other shared
1601 libraries and referenced here. Update the list of version
1602 dependencies. This will be put into the .gnu.version_r section.
1603 This function is called via elf_link_hash_traverse. */
1606 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1609 struct elf_find_verdep_info
*rinfo
= data
;
1610 Elf_Internal_Verneed
*t
;
1611 Elf_Internal_Vernaux
*a
;
1614 if (h
->root
.type
== bfd_link_hash_warning
)
1615 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1617 /* We only care about symbols defined in shared objects with version
1622 || h
->verinfo
.verdef
== NULL
)
1625 /* See if we already know about this version. */
1626 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1628 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1631 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1632 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1638 /* This is a new version. Add it to tree we are building. */
1643 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1646 rinfo
->failed
= TRUE
;
1650 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1651 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1652 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1656 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1658 /* Note that we are copying a string pointer here, and testing it
1659 above. If bfd_elf_string_from_elf_section is ever changed to
1660 discard the string data when low in memory, this will have to be
1662 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1664 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1665 a
->vna_nextptr
= t
->vn_auxptr
;
1667 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1670 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1677 /* Figure out appropriate versions for all the symbols. We may not
1678 have the version number script until we have read all of the input
1679 files, so until that point we don't know which symbols should be
1680 local. This function is called via elf_link_hash_traverse. */
1683 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1685 struct elf_assign_sym_version_info
*sinfo
;
1686 struct bfd_link_info
*info
;
1687 const struct elf_backend_data
*bed
;
1688 struct elf_info_failed eif
;
1695 if (h
->root
.type
== bfd_link_hash_warning
)
1696 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1698 /* Fix the symbol flags. */
1701 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1704 sinfo
->failed
= TRUE
;
1708 /* We only need version numbers for symbols defined in regular
1710 if (!h
->def_regular
)
1713 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1714 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1715 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1717 struct bfd_elf_version_tree
*t
;
1722 /* There are two consecutive ELF_VER_CHR characters if this is
1723 not a hidden symbol. */
1725 if (*p
== ELF_VER_CHR
)
1731 /* If there is no version string, we can just return out. */
1739 /* Look for the version. If we find it, it is no longer weak. */
1740 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1742 if (strcmp (t
->name
, p
) == 0)
1746 struct bfd_elf_version_expr
*d
;
1748 len
= p
- h
->root
.root
.string
;
1749 alc
= bfd_malloc (len
);
1752 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1753 alc
[len
- 1] = '\0';
1754 if (alc
[len
- 2] == ELF_VER_CHR
)
1755 alc
[len
- 2] = '\0';
1757 h
->verinfo
.vertree
= t
;
1761 if (t
->globals
.list
!= NULL
)
1762 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1764 /* See if there is anything to force this symbol to
1766 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1768 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1772 && ! info
->export_dynamic
)
1773 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1781 /* If we are building an application, we need to create a
1782 version node for this version. */
1783 if (t
== NULL
&& info
->executable
)
1785 struct bfd_elf_version_tree
**pp
;
1788 /* If we aren't going to export this symbol, we don't need
1789 to worry about it. */
1790 if (h
->dynindx
== -1)
1794 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1797 sinfo
->failed
= TRUE
;
1802 t
->name_indx
= (unsigned int) -1;
1806 /* Don't count anonymous version tag. */
1807 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1809 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1811 t
->vernum
= version_index
;
1815 h
->verinfo
.vertree
= t
;
1819 /* We could not find the version for a symbol when
1820 generating a shared archive. Return an error. */
1821 (*_bfd_error_handler
)
1822 (_("%B: undefined versioned symbol name %s"),
1823 sinfo
->output_bfd
, h
->root
.root
.string
);
1824 bfd_set_error (bfd_error_bad_value
);
1825 sinfo
->failed
= TRUE
;
1833 /* If we don't have a version for this symbol, see if we can find
1835 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1837 struct bfd_elf_version_tree
*t
;
1838 struct bfd_elf_version_tree
*local_ver
;
1839 struct bfd_elf_version_expr
*d
;
1841 /* See if can find what version this symbol is in. If the
1842 symbol is supposed to be local, then don't actually register
1845 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1847 if (t
->globals
.list
!= NULL
)
1849 bfd_boolean matched
;
1853 while ((d
= (*t
->match
) (&t
->globals
, d
,
1854 h
->root
.root
.string
)) != NULL
)
1859 /* There is a version without definition. Make
1860 the symbol the default definition for this
1862 h
->verinfo
.vertree
= t
;
1870 /* There is no undefined version for this symbol. Hide the
1872 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1875 if (t
->locals
.list
!= NULL
)
1878 while ((d
= (*t
->match
) (&t
->locals
, d
,
1879 h
->root
.root
.string
)) != NULL
)
1882 /* If the match is "*", keep looking for a more
1883 explicit, perhaps even global, match.
1884 XXX: Shouldn't this be !d->wildcard instead? */
1885 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1894 if (local_ver
!= NULL
)
1896 h
->verinfo
.vertree
= local_ver
;
1897 if (h
->dynindx
!= -1
1899 && ! info
->export_dynamic
)
1901 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1909 /* Read and swap the relocs from the section indicated by SHDR. This
1910 may be either a REL or a RELA section. The relocations are
1911 translated into RELA relocations and stored in INTERNAL_RELOCS,
1912 which should have already been allocated to contain enough space.
1913 The EXTERNAL_RELOCS are a buffer where the external form of the
1914 relocations should be stored.
1916 Returns FALSE if something goes wrong. */
1919 elf_link_read_relocs_from_section (bfd
*abfd
,
1921 Elf_Internal_Shdr
*shdr
,
1922 void *external_relocs
,
1923 Elf_Internal_Rela
*internal_relocs
)
1925 const struct elf_backend_data
*bed
;
1926 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1927 const bfd_byte
*erela
;
1928 const bfd_byte
*erelaend
;
1929 Elf_Internal_Rela
*irela
;
1930 Elf_Internal_Shdr
*symtab_hdr
;
1933 /* Position ourselves at the start of the section. */
1934 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1937 /* Read the relocations. */
1938 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1941 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1942 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1944 bed
= get_elf_backend_data (abfd
);
1946 /* Convert the external relocations to the internal format. */
1947 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1948 swap_in
= bed
->s
->swap_reloc_in
;
1949 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1950 swap_in
= bed
->s
->swap_reloca_in
;
1953 bfd_set_error (bfd_error_wrong_format
);
1957 erela
= external_relocs
;
1958 erelaend
= erela
+ shdr
->sh_size
;
1959 irela
= internal_relocs
;
1960 while (erela
< erelaend
)
1964 (*swap_in
) (abfd
, erela
, irela
);
1965 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1966 if (bed
->s
->arch_size
== 64)
1968 if ((size_t) r_symndx
>= nsyms
)
1970 (*_bfd_error_handler
)
1971 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1972 " for offset 0x%lx in section `%A'"),
1974 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1975 bfd_set_error (bfd_error_bad_value
);
1978 irela
+= bed
->s
->int_rels_per_ext_rel
;
1979 erela
+= shdr
->sh_entsize
;
1985 /* Read and swap the relocs for a section O. They may have been
1986 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1987 not NULL, they are used as buffers to read into. They are known to
1988 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1989 the return value is allocated using either malloc or bfd_alloc,
1990 according to the KEEP_MEMORY argument. If O has two relocation
1991 sections (both REL and RELA relocations), then the REL_HDR
1992 relocations will appear first in INTERNAL_RELOCS, followed by the
1993 REL_HDR2 relocations. */
1996 _bfd_elf_link_read_relocs (bfd
*abfd
,
1998 void *external_relocs
,
1999 Elf_Internal_Rela
*internal_relocs
,
2000 bfd_boolean keep_memory
)
2002 Elf_Internal_Shdr
*rel_hdr
;
2003 void *alloc1
= NULL
;
2004 Elf_Internal_Rela
*alloc2
= NULL
;
2005 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2007 if (elf_section_data (o
)->relocs
!= NULL
)
2008 return elf_section_data (o
)->relocs
;
2010 if (o
->reloc_count
== 0)
2013 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2015 if (internal_relocs
== NULL
)
2019 size
= o
->reloc_count
;
2020 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2022 internal_relocs
= bfd_alloc (abfd
, size
);
2024 internal_relocs
= alloc2
= bfd_malloc (size
);
2025 if (internal_relocs
== NULL
)
2029 if (external_relocs
== NULL
)
2031 bfd_size_type size
= rel_hdr
->sh_size
;
2033 if (elf_section_data (o
)->rel_hdr2
)
2034 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2035 alloc1
= bfd_malloc (size
);
2038 external_relocs
= alloc1
;
2041 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2045 if (elf_section_data (o
)->rel_hdr2
2046 && (!elf_link_read_relocs_from_section
2048 elf_section_data (o
)->rel_hdr2
,
2049 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2050 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2051 * bed
->s
->int_rels_per_ext_rel
))))
2054 /* Cache the results for next time, if we can. */
2056 elf_section_data (o
)->relocs
= internal_relocs
;
2061 /* Don't free alloc2, since if it was allocated we are passing it
2062 back (under the name of internal_relocs). */
2064 return internal_relocs
;
2074 /* Compute the size of, and allocate space for, REL_HDR which is the
2075 section header for a section containing relocations for O. */
2078 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2079 Elf_Internal_Shdr
*rel_hdr
,
2082 bfd_size_type reloc_count
;
2083 bfd_size_type num_rel_hashes
;
2085 /* Figure out how many relocations there will be. */
2086 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2087 reloc_count
= elf_section_data (o
)->rel_count
;
2089 reloc_count
= elf_section_data (o
)->rel_count2
;
2091 num_rel_hashes
= o
->reloc_count
;
2092 if (num_rel_hashes
< reloc_count
)
2093 num_rel_hashes
= reloc_count
;
2095 /* That allows us to calculate the size of the section. */
2096 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2098 /* The contents field must last into write_object_contents, so we
2099 allocate it with bfd_alloc rather than malloc. Also since we
2100 cannot be sure that the contents will actually be filled in,
2101 we zero the allocated space. */
2102 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2103 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2106 /* We only allocate one set of hash entries, so we only do it the
2107 first time we are called. */
2108 if (elf_section_data (o
)->rel_hashes
== NULL
2111 struct elf_link_hash_entry
**p
;
2113 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2117 elf_section_data (o
)->rel_hashes
= p
;
2123 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2124 originated from the section given by INPUT_REL_HDR) to the
2128 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2129 asection
*input_section
,
2130 Elf_Internal_Shdr
*input_rel_hdr
,
2131 Elf_Internal_Rela
*internal_relocs
)
2133 Elf_Internal_Rela
*irela
;
2134 Elf_Internal_Rela
*irelaend
;
2136 Elf_Internal_Shdr
*output_rel_hdr
;
2137 asection
*output_section
;
2138 unsigned int *rel_countp
= NULL
;
2139 const struct elf_backend_data
*bed
;
2140 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2142 output_section
= input_section
->output_section
;
2143 output_rel_hdr
= NULL
;
2145 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2146 == input_rel_hdr
->sh_entsize
)
2148 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2149 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2151 else if (elf_section_data (output_section
)->rel_hdr2
2152 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2153 == input_rel_hdr
->sh_entsize
))
2155 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2156 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2160 (*_bfd_error_handler
)
2161 (_("%B: relocation size mismatch in %B section %A"),
2162 output_bfd
, input_section
->owner
, input_section
);
2163 bfd_set_error (bfd_error_wrong_object_format
);
2167 bed
= get_elf_backend_data (output_bfd
);
2168 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2169 swap_out
= bed
->s
->swap_reloc_out
;
2170 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2171 swap_out
= bed
->s
->swap_reloca_out
;
2175 erel
= output_rel_hdr
->contents
;
2176 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2177 irela
= internal_relocs
;
2178 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2179 * bed
->s
->int_rels_per_ext_rel
);
2180 while (irela
< irelaend
)
2182 (*swap_out
) (output_bfd
, irela
, erel
);
2183 irela
+= bed
->s
->int_rels_per_ext_rel
;
2184 erel
+= input_rel_hdr
->sh_entsize
;
2187 /* Bump the counter, so that we know where to add the next set of
2189 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2194 /* Fix up the flags for a symbol. This handles various cases which
2195 can only be fixed after all the input files are seen. This is
2196 currently called by both adjust_dynamic_symbol and
2197 assign_sym_version, which is unnecessary but perhaps more robust in
2198 the face of future changes. */
2201 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2202 struct elf_info_failed
*eif
)
2204 /* If this symbol was mentioned in a non-ELF file, try to set
2205 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2206 permit a non-ELF file to correctly refer to a symbol defined in
2207 an ELF dynamic object. */
2210 while (h
->root
.type
== bfd_link_hash_indirect
)
2211 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2213 if (h
->root
.type
!= bfd_link_hash_defined
2214 && h
->root
.type
!= bfd_link_hash_defweak
)
2217 h
->ref_regular_nonweak
= 1;
2221 if (h
->root
.u
.def
.section
->owner
!= NULL
2222 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2223 == bfd_target_elf_flavour
))
2226 h
->ref_regular_nonweak
= 1;
2232 if (h
->dynindx
== -1
2236 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2245 /* Unfortunately, NON_ELF is only correct if the symbol
2246 was first seen in a non-ELF file. Fortunately, if the symbol
2247 was first seen in an ELF file, we're probably OK unless the
2248 symbol was defined in a non-ELF file. Catch that case here.
2249 FIXME: We're still in trouble if the symbol was first seen in
2250 a dynamic object, and then later in a non-ELF regular object. */
2251 if ((h
->root
.type
== bfd_link_hash_defined
2252 || h
->root
.type
== bfd_link_hash_defweak
)
2254 && (h
->root
.u
.def
.section
->owner
!= NULL
2255 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2256 != bfd_target_elf_flavour
)
2257 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2258 && !h
->def_dynamic
)))
2262 /* If this is a final link, and the symbol was defined as a common
2263 symbol in a regular object file, and there was no definition in
2264 any dynamic object, then the linker will have allocated space for
2265 the symbol in a common section but the DEF_REGULAR
2266 flag will not have been set. */
2267 if (h
->root
.type
== bfd_link_hash_defined
2271 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2274 /* If -Bsymbolic was used (which means to bind references to global
2275 symbols to the definition within the shared object), and this
2276 symbol was defined in a regular object, then it actually doesn't
2277 need a PLT entry. Likewise, if the symbol has non-default
2278 visibility. If the symbol has hidden or internal visibility, we
2279 will force it local. */
2281 && eif
->info
->shared
2282 && is_elf_hash_table (eif
->info
->hash
)
2283 && (eif
->info
->symbolic
2284 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2287 const struct elf_backend_data
*bed
;
2288 bfd_boolean force_local
;
2290 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2292 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2293 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2294 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2297 /* If a weak undefined symbol has non-default visibility, we also
2298 hide it from the dynamic linker. */
2299 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2300 && h
->root
.type
== bfd_link_hash_undefweak
)
2302 const struct elf_backend_data
*bed
;
2303 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2304 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2307 /* If this is a weak defined symbol in a dynamic object, and we know
2308 the real definition in the dynamic object, copy interesting flags
2309 over to the real definition. */
2310 if (h
->u
.weakdef
!= NULL
)
2312 struct elf_link_hash_entry
*weakdef
;
2314 weakdef
= h
->u
.weakdef
;
2315 if (h
->root
.type
== bfd_link_hash_indirect
)
2316 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2318 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2319 || h
->root
.type
== bfd_link_hash_defweak
);
2320 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2321 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2322 BFD_ASSERT (weakdef
->def_dynamic
);
2324 /* If the real definition is defined by a regular object file,
2325 don't do anything special. See the longer description in
2326 _bfd_elf_adjust_dynamic_symbol, below. */
2327 if (weakdef
->def_regular
)
2328 h
->u
.weakdef
= NULL
;
2331 const struct elf_backend_data
*bed
;
2333 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2334 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2341 /* Make the backend pick a good value for a dynamic symbol. This is
2342 called via elf_link_hash_traverse, and also calls itself
2346 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2348 struct elf_info_failed
*eif
= data
;
2350 const struct elf_backend_data
*bed
;
2352 if (! is_elf_hash_table (eif
->info
->hash
))
2355 if (h
->root
.type
== bfd_link_hash_warning
)
2357 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2358 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2360 /* When warning symbols are created, they **replace** the "real"
2361 entry in the hash table, thus we never get to see the real
2362 symbol in a hash traversal. So look at it now. */
2363 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2366 /* Ignore indirect symbols. These are added by the versioning code. */
2367 if (h
->root
.type
== bfd_link_hash_indirect
)
2370 /* Fix the symbol flags. */
2371 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2374 /* If this symbol does not require a PLT entry, and it is not
2375 defined by a dynamic object, or is not referenced by a regular
2376 object, ignore it. We do have to handle a weak defined symbol,
2377 even if no regular object refers to it, if we decided to add it
2378 to the dynamic symbol table. FIXME: Do we normally need to worry
2379 about symbols which are defined by one dynamic object and
2380 referenced by another one? */
2385 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2387 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2391 /* If we've already adjusted this symbol, don't do it again. This
2392 can happen via a recursive call. */
2393 if (h
->dynamic_adjusted
)
2396 /* Don't look at this symbol again. Note that we must set this
2397 after checking the above conditions, because we may look at a
2398 symbol once, decide not to do anything, and then get called
2399 recursively later after REF_REGULAR is set below. */
2400 h
->dynamic_adjusted
= 1;
2402 /* If this is a weak definition, and we know a real definition, and
2403 the real symbol is not itself defined by a regular object file,
2404 then get a good value for the real definition. We handle the
2405 real symbol first, for the convenience of the backend routine.
2407 Note that there is a confusing case here. If the real definition
2408 is defined by a regular object file, we don't get the real symbol
2409 from the dynamic object, but we do get the weak symbol. If the
2410 processor backend uses a COPY reloc, then if some routine in the
2411 dynamic object changes the real symbol, we will not see that
2412 change in the corresponding weak symbol. This is the way other
2413 ELF linkers work as well, and seems to be a result of the shared
2416 I will clarify this issue. Most SVR4 shared libraries define the
2417 variable _timezone and define timezone as a weak synonym. The
2418 tzset call changes _timezone. If you write
2419 extern int timezone;
2421 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2422 you might expect that, since timezone is a synonym for _timezone,
2423 the same number will print both times. However, if the processor
2424 backend uses a COPY reloc, then actually timezone will be copied
2425 into your process image, and, since you define _timezone
2426 yourself, _timezone will not. Thus timezone and _timezone will
2427 wind up at different memory locations. The tzset call will set
2428 _timezone, leaving timezone unchanged. */
2430 if (h
->u
.weakdef
!= NULL
)
2432 /* If we get to this point, we know there is an implicit
2433 reference by a regular object file via the weak symbol H.
2434 FIXME: Is this really true? What if the traversal finds
2435 H->U.WEAKDEF before it finds H? */
2436 h
->u
.weakdef
->ref_regular
= 1;
2438 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2442 /* If a symbol has no type and no size and does not require a PLT
2443 entry, then we are probably about to do the wrong thing here: we
2444 are probably going to create a COPY reloc for an empty object.
2445 This case can arise when a shared object is built with assembly
2446 code, and the assembly code fails to set the symbol type. */
2448 && h
->type
== STT_NOTYPE
2450 (*_bfd_error_handler
)
2451 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2452 h
->root
.root
.string
);
2454 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2455 bed
= get_elf_backend_data (dynobj
);
2456 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2465 /* Adjust all external symbols pointing into SEC_MERGE sections
2466 to reflect the object merging within the sections. */
2469 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2473 if (h
->root
.type
== bfd_link_hash_warning
)
2474 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2476 if ((h
->root
.type
== bfd_link_hash_defined
2477 || h
->root
.type
== bfd_link_hash_defweak
)
2478 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2479 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2481 bfd
*output_bfd
= data
;
2483 h
->root
.u
.def
.value
=
2484 _bfd_merged_section_offset (output_bfd
,
2485 &h
->root
.u
.def
.section
,
2486 elf_section_data (sec
)->sec_info
,
2487 h
->root
.u
.def
.value
);
2493 /* Returns false if the symbol referred to by H should be considered
2494 to resolve local to the current module, and true if it should be
2495 considered to bind dynamically. */
2498 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2499 struct bfd_link_info
*info
,
2500 bfd_boolean ignore_protected
)
2502 bfd_boolean binding_stays_local_p
;
2507 while (h
->root
.type
== bfd_link_hash_indirect
2508 || h
->root
.type
== bfd_link_hash_warning
)
2509 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2511 /* If it was forced local, then clearly it's not dynamic. */
2512 if (h
->dynindx
== -1)
2514 if (h
->forced_local
)
2517 /* Identify the cases where name binding rules say that a
2518 visible symbol resolves locally. */
2519 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2521 switch (ELF_ST_VISIBILITY (h
->other
))
2528 /* Proper resolution for function pointer equality may require
2529 that these symbols perhaps be resolved dynamically, even though
2530 we should be resolving them to the current module. */
2531 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2532 binding_stays_local_p
= TRUE
;
2539 /* If it isn't defined locally, then clearly it's dynamic. */
2540 if (!h
->def_regular
)
2543 /* Otherwise, the symbol is dynamic if binding rules don't tell
2544 us that it remains local. */
2545 return !binding_stays_local_p
;
2548 /* Return true if the symbol referred to by H should be considered
2549 to resolve local to the current module, and false otherwise. Differs
2550 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2551 undefined symbols and weak symbols. */
2554 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2555 struct bfd_link_info
*info
,
2556 bfd_boolean local_protected
)
2558 /* If it's a local sym, of course we resolve locally. */
2562 /* Common symbols that become definitions don't get the DEF_REGULAR
2563 flag set, so test it first, and don't bail out. */
2564 if (ELF_COMMON_DEF_P (h
))
2566 /* If we don't have a definition in a regular file, then we can't
2567 resolve locally. The sym is either undefined or dynamic. */
2568 else if (!h
->def_regular
)
2571 /* Forced local symbols resolve locally. */
2572 if (h
->forced_local
)
2575 /* As do non-dynamic symbols. */
2576 if (h
->dynindx
== -1)
2579 /* At this point, we know the symbol is defined and dynamic. In an
2580 executable it must resolve locally, likewise when building symbolic
2581 shared libraries. */
2582 if (info
->executable
|| info
->symbolic
)
2585 /* Now deal with defined dynamic symbols in shared libraries. Ones
2586 with default visibility might not resolve locally. */
2587 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2590 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2591 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2594 /* STV_PROTECTED non-function symbols are local. */
2595 if (h
->type
!= STT_FUNC
)
2598 /* Function pointer equality tests may require that STV_PROTECTED
2599 symbols be treated as dynamic symbols, even when we know that the
2600 dynamic linker will resolve them locally. */
2601 return local_protected
;
2604 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2605 aligned. Returns the first TLS output section. */
2607 struct bfd_section
*
2608 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2610 struct bfd_section
*sec
, *tls
;
2611 unsigned int align
= 0;
2613 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2614 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2618 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2619 if (sec
->alignment_power
> align
)
2620 align
= sec
->alignment_power
;
2622 elf_hash_table (info
)->tls_sec
= tls
;
2624 /* Ensure the alignment of the first section is the largest alignment,
2625 so that the tls segment starts aligned. */
2627 tls
->alignment_power
= align
;
2632 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2634 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2635 Elf_Internal_Sym
*sym
)
2637 /* Local symbols do not count, but target specific ones might. */
2638 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2639 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2642 /* Function symbols do not count. */
2643 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2646 /* If the section is undefined, then so is the symbol. */
2647 if (sym
->st_shndx
== SHN_UNDEF
)
2650 /* If the symbol is defined in the common section, then
2651 it is a common definition and so does not count. */
2652 if (sym
->st_shndx
== SHN_COMMON
)
2655 /* If the symbol is in a target specific section then we
2656 must rely upon the backend to tell us what it is. */
2657 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2658 /* FIXME - this function is not coded yet:
2660 return _bfd_is_global_symbol_definition (abfd, sym);
2662 Instead for now assume that the definition is not global,
2663 Even if this is wrong, at least the linker will behave
2664 in the same way that it used to do. */
2670 /* Search the symbol table of the archive element of the archive ABFD
2671 whose archive map contains a mention of SYMDEF, and determine if
2672 the symbol is defined in this element. */
2674 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2676 Elf_Internal_Shdr
* hdr
;
2677 bfd_size_type symcount
;
2678 bfd_size_type extsymcount
;
2679 bfd_size_type extsymoff
;
2680 Elf_Internal_Sym
*isymbuf
;
2681 Elf_Internal_Sym
*isym
;
2682 Elf_Internal_Sym
*isymend
;
2685 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2689 if (! bfd_check_format (abfd
, bfd_object
))
2692 /* If we have already included the element containing this symbol in the
2693 link then we do not need to include it again. Just claim that any symbol
2694 it contains is not a definition, so that our caller will not decide to
2695 (re)include this element. */
2696 if (abfd
->archive_pass
)
2699 /* Select the appropriate symbol table. */
2700 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2701 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2703 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2705 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2707 /* The sh_info field of the symtab header tells us where the
2708 external symbols start. We don't care about the local symbols. */
2709 if (elf_bad_symtab (abfd
))
2711 extsymcount
= symcount
;
2716 extsymcount
= symcount
- hdr
->sh_info
;
2717 extsymoff
= hdr
->sh_info
;
2720 if (extsymcount
== 0)
2723 /* Read in the symbol table. */
2724 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2726 if (isymbuf
== NULL
)
2729 /* Scan the symbol table looking for SYMDEF. */
2731 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2735 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2740 if (strcmp (name
, symdef
->name
) == 0)
2742 result
= is_global_data_symbol_definition (abfd
, isym
);
2752 /* Add an entry to the .dynamic table. */
2755 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2759 struct elf_link_hash_table
*hash_table
;
2760 const struct elf_backend_data
*bed
;
2762 bfd_size_type newsize
;
2763 bfd_byte
*newcontents
;
2764 Elf_Internal_Dyn dyn
;
2766 hash_table
= elf_hash_table (info
);
2767 if (! is_elf_hash_table (hash_table
))
2770 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2772 (_("warning: creating a DT_TEXTREL in a shared object."));
2774 bed
= get_elf_backend_data (hash_table
->dynobj
);
2775 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2776 BFD_ASSERT (s
!= NULL
);
2778 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2779 newcontents
= bfd_realloc (s
->contents
, newsize
);
2780 if (newcontents
== NULL
)
2784 dyn
.d_un
.d_val
= val
;
2785 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2788 s
->contents
= newcontents
;
2793 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2794 otherwise just check whether one already exists. Returns -1 on error,
2795 1 if a DT_NEEDED tag already exists, and 0 on success. */
2798 elf_add_dt_needed_tag (bfd
*abfd
,
2799 struct bfd_link_info
*info
,
2803 struct elf_link_hash_table
*hash_table
;
2804 bfd_size_type oldsize
;
2805 bfd_size_type strindex
;
2807 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2810 hash_table
= elf_hash_table (info
);
2811 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2812 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2813 if (strindex
== (bfd_size_type
) -1)
2816 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2819 const struct elf_backend_data
*bed
;
2822 bed
= get_elf_backend_data (hash_table
->dynobj
);
2823 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2825 for (extdyn
= sdyn
->contents
;
2826 extdyn
< sdyn
->contents
+ sdyn
->size
;
2827 extdyn
+= bed
->s
->sizeof_dyn
)
2829 Elf_Internal_Dyn dyn
;
2831 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2832 if (dyn
.d_tag
== DT_NEEDED
2833 && dyn
.d_un
.d_val
== strindex
)
2835 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2843 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2846 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2850 /* We were just checking for existence of the tag. */
2851 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2856 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2857 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2858 references from regular objects to these symbols.
2860 ??? Should we do something about references from other dynamic
2861 obects? If not, we potentially lose some warnings about undefined
2862 symbols. But how can we recover the initial undefined / undefweak
2865 struct elf_smash_syms_data
2868 struct elf_link_hash_table
*htab
;
2869 bfd_boolean twiddled
;
2873 elf_smash_syms (struct elf_link_hash_entry
*h
, void *data
)
2875 struct elf_smash_syms_data
*inf
= (struct elf_smash_syms_data
*) data
;
2876 struct bfd_link_hash_entry
*bh
;
2878 switch (h
->root
.type
)
2881 case bfd_link_hash_new
:
2884 case bfd_link_hash_undefined
:
2885 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2887 if (h
->root
.u
.undef
.weak
!= NULL
2888 && h
->root
.u
.undef
.weak
!= inf
->not_needed
)
2890 /* Symbol was undefweak in u.undef.weak bfd, and has become
2891 undefined in as-needed lib. Restore weak. */
2892 h
->root
.type
= bfd_link_hash_undefweak
;
2893 h
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.weak
;
2894 if (h
->root
.u
.undef
.next
!= NULL
2895 || inf
->htab
->root
.undefs_tail
== &h
->root
)
2896 inf
->twiddled
= TRUE
;
2901 case bfd_link_hash_undefweak
:
2902 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2906 case bfd_link_hash_defined
:
2907 case bfd_link_hash_defweak
:
2908 if (h
->root
.u
.def
.section
->owner
!= inf
->not_needed
)
2912 case bfd_link_hash_common
:
2913 if (h
->root
.u
.c
.p
->section
->owner
!= inf
->not_needed
)
2917 case bfd_link_hash_warning
:
2918 case bfd_link_hash_indirect
:
2919 elf_smash_syms ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
);
2920 if (h
->root
.u
.i
.link
->type
!= bfd_link_hash_new
)
2922 if (h
->root
.u
.i
.link
->u
.undef
.abfd
!= inf
->not_needed
)
2927 /* There is no way we can undo symbol table state from defined or
2928 defweak back to undefined. */
2932 /* Set sym back to newly created state, but keep undefs list pointer. */
2933 bh
= h
->root
.u
.undef
.next
;
2934 if (bh
!= NULL
|| inf
->htab
->root
.undefs_tail
== &h
->root
)
2935 inf
->twiddled
= TRUE
;
2936 (*inf
->htab
->root
.table
.newfunc
) (&h
->root
.root
,
2937 &inf
->htab
->root
.table
,
2938 h
->root
.root
.string
);
2939 h
->root
.u
.undef
.next
= bh
;
2940 h
->root
.u
.undef
.abfd
= inf
->not_needed
;
2945 /* Sort symbol by value and section. */
2947 elf_sort_symbol (const void *arg1
, const void *arg2
)
2949 const struct elf_link_hash_entry
*h1
;
2950 const struct elf_link_hash_entry
*h2
;
2951 bfd_signed_vma vdiff
;
2953 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2954 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2955 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2957 return vdiff
> 0 ? 1 : -1;
2960 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2962 return sdiff
> 0 ? 1 : -1;
2967 /* This function is used to adjust offsets into .dynstr for
2968 dynamic symbols. This is called via elf_link_hash_traverse. */
2971 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2973 struct elf_strtab_hash
*dynstr
= data
;
2975 if (h
->root
.type
== bfd_link_hash_warning
)
2976 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2978 if (h
->dynindx
!= -1)
2979 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2983 /* Assign string offsets in .dynstr, update all structures referencing
2987 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2989 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2990 struct elf_link_local_dynamic_entry
*entry
;
2991 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2992 bfd
*dynobj
= hash_table
->dynobj
;
2995 const struct elf_backend_data
*bed
;
2998 _bfd_elf_strtab_finalize (dynstr
);
2999 size
= _bfd_elf_strtab_size (dynstr
);
3001 bed
= get_elf_backend_data (dynobj
);
3002 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3003 BFD_ASSERT (sdyn
!= NULL
);
3005 /* Update all .dynamic entries referencing .dynstr strings. */
3006 for (extdyn
= sdyn
->contents
;
3007 extdyn
< sdyn
->contents
+ sdyn
->size
;
3008 extdyn
+= bed
->s
->sizeof_dyn
)
3010 Elf_Internal_Dyn dyn
;
3012 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3016 dyn
.d_un
.d_val
= size
;
3024 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3029 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3032 /* Now update local dynamic symbols. */
3033 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3034 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3035 entry
->isym
.st_name
);
3037 /* And the rest of dynamic symbols. */
3038 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3040 /* Adjust version definitions. */
3041 if (elf_tdata (output_bfd
)->cverdefs
)
3046 Elf_Internal_Verdef def
;
3047 Elf_Internal_Verdaux defaux
;
3049 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3053 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3055 p
+= sizeof (Elf_External_Verdef
);
3056 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3058 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3060 _bfd_elf_swap_verdaux_in (output_bfd
,
3061 (Elf_External_Verdaux
*) p
, &defaux
);
3062 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3064 _bfd_elf_swap_verdaux_out (output_bfd
,
3065 &defaux
, (Elf_External_Verdaux
*) p
);
3066 p
+= sizeof (Elf_External_Verdaux
);
3069 while (def
.vd_next
);
3072 /* Adjust version references. */
3073 if (elf_tdata (output_bfd
)->verref
)
3078 Elf_Internal_Verneed need
;
3079 Elf_Internal_Vernaux needaux
;
3081 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3085 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3087 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3088 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3089 (Elf_External_Verneed
*) p
);
3090 p
+= sizeof (Elf_External_Verneed
);
3091 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3093 _bfd_elf_swap_vernaux_in (output_bfd
,
3094 (Elf_External_Vernaux
*) p
, &needaux
);
3095 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3097 _bfd_elf_swap_vernaux_out (output_bfd
,
3099 (Elf_External_Vernaux
*) p
);
3100 p
+= sizeof (Elf_External_Vernaux
);
3103 while (need
.vn_next
);
3109 /* Add symbols from an ELF object file to the linker hash table. */
3112 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3114 bfd_boolean (*add_symbol_hook
)
3115 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
3116 const char **, flagword
*, asection
**, bfd_vma
*);
3117 bfd_boolean (*check_relocs
)
3118 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
3119 bfd_boolean (*check_directives
)
3120 (bfd
*, struct bfd_link_info
*);
3121 bfd_boolean collect
;
3122 Elf_Internal_Shdr
*hdr
;
3123 bfd_size_type symcount
;
3124 bfd_size_type extsymcount
;
3125 bfd_size_type extsymoff
;
3126 struct elf_link_hash_entry
**sym_hash
;
3127 bfd_boolean dynamic
;
3128 Elf_External_Versym
*extversym
= NULL
;
3129 Elf_External_Versym
*ever
;
3130 struct elf_link_hash_entry
*weaks
;
3131 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3132 bfd_size_type nondeflt_vers_cnt
= 0;
3133 Elf_Internal_Sym
*isymbuf
= NULL
;
3134 Elf_Internal_Sym
*isym
;
3135 Elf_Internal_Sym
*isymend
;
3136 const struct elf_backend_data
*bed
;
3137 bfd_boolean add_needed
;
3138 struct elf_link_hash_table
* hash_table
;
3141 hash_table
= elf_hash_table (info
);
3143 bed
= get_elf_backend_data (abfd
);
3144 add_symbol_hook
= bed
->elf_add_symbol_hook
;
3145 collect
= bed
->collect
;
3147 if ((abfd
->flags
& DYNAMIC
) == 0)
3153 /* You can't use -r against a dynamic object. Also, there's no
3154 hope of using a dynamic object which does not exactly match
3155 the format of the output file. */
3156 if (info
->relocatable
3157 || !is_elf_hash_table (hash_table
)
3158 || hash_table
->root
.creator
!= abfd
->xvec
)
3160 if (info
->relocatable
)
3161 bfd_set_error (bfd_error_invalid_operation
);
3163 bfd_set_error (bfd_error_wrong_format
);
3168 /* As a GNU extension, any input sections which are named
3169 .gnu.warning.SYMBOL are treated as warning symbols for the given
3170 symbol. This differs from .gnu.warning sections, which generate
3171 warnings when they are included in an output file. */
3172 if (info
->executable
)
3176 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3180 name
= bfd_get_section_name (abfd
, s
);
3181 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3186 name
+= sizeof ".gnu.warning." - 1;
3188 /* If this is a shared object, then look up the symbol
3189 in the hash table. If it is there, and it is already
3190 been defined, then we will not be using the entry
3191 from this shared object, so we don't need to warn.
3192 FIXME: If we see the definition in a regular object
3193 later on, we will warn, but we shouldn't. The only
3194 fix is to keep track of what warnings we are supposed
3195 to emit, and then handle them all at the end of the
3199 struct elf_link_hash_entry
*h
;
3201 h
= elf_link_hash_lookup (hash_table
, name
,
3202 FALSE
, FALSE
, TRUE
);
3204 /* FIXME: What about bfd_link_hash_common? */
3206 && (h
->root
.type
== bfd_link_hash_defined
3207 || h
->root
.type
== bfd_link_hash_defweak
))
3209 /* We don't want to issue this warning. Clobber
3210 the section size so that the warning does not
3211 get copied into the output file. */
3218 msg
= bfd_alloc (abfd
, sz
+ 1);
3222 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3227 if (! (_bfd_generic_link_add_one_symbol
3228 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3229 FALSE
, collect
, NULL
)))
3232 if (! info
->relocatable
)
3234 /* Clobber the section size so that the warning does
3235 not get copied into the output file. */
3245 /* If we are creating a shared library, create all the dynamic
3246 sections immediately. We need to attach them to something,
3247 so we attach them to this BFD, provided it is the right
3248 format. FIXME: If there are no input BFD's of the same
3249 format as the output, we can't make a shared library. */
3251 && is_elf_hash_table (hash_table
)
3252 && hash_table
->root
.creator
== abfd
->xvec
3253 && ! hash_table
->dynamic_sections_created
)
3255 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3259 else if (!is_elf_hash_table (hash_table
))
3264 const char *soname
= NULL
;
3265 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3268 /* ld --just-symbols and dynamic objects don't mix very well.
3269 Test for --just-symbols by looking at info set up by
3270 _bfd_elf_link_just_syms. */
3271 if ((s
= abfd
->sections
) != NULL
3272 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3275 /* If this dynamic lib was specified on the command line with
3276 --as-needed in effect, then we don't want to add a DT_NEEDED
3277 tag unless the lib is actually used. Similary for libs brought
3278 in by another lib's DT_NEEDED. When --no-add-needed is used
3279 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3280 any dynamic library in DT_NEEDED tags in the dynamic lib at
3282 add_needed
= (elf_dyn_lib_class (abfd
)
3283 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3284 | DYN_NO_NEEDED
)) == 0;
3286 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3292 unsigned long shlink
;
3294 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3295 goto error_free_dyn
;
3297 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3299 goto error_free_dyn
;
3300 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3302 for (extdyn
= dynbuf
;
3303 extdyn
< dynbuf
+ s
->size
;
3304 extdyn
+= bed
->s
->sizeof_dyn
)
3306 Elf_Internal_Dyn dyn
;
3308 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3309 if (dyn
.d_tag
== DT_SONAME
)
3311 unsigned int tagv
= dyn
.d_un
.d_val
;
3312 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3314 goto error_free_dyn
;
3316 if (dyn
.d_tag
== DT_NEEDED
)
3318 struct bfd_link_needed_list
*n
, **pn
;
3320 unsigned int tagv
= dyn
.d_un
.d_val
;
3322 amt
= sizeof (struct bfd_link_needed_list
);
3323 n
= bfd_alloc (abfd
, amt
);
3324 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3325 if (n
== NULL
|| fnm
== NULL
)
3326 goto error_free_dyn
;
3327 amt
= strlen (fnm
) + 1;
3328 anm
= bfd_alloc (abfd
, amt
);
3330 goto error_free_dyn
;
3331 memcpy (anm
, fnm
, amt
);
3335 for (pn
= & hash_table
->needed
;
3341 if (dyn
.d_tag
== DT_RUNPATH
)
3343 struct bfd_link_needed_list
*n
, **pn
;
3345 unsigned int tagv
= dyn
.d_un
.d_val
;
3347 amt
= sizeof (struct bfd_link_needed_list
);
3348 n
= bfd_alloc (abfd
, amt
);
3349 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3350 if (n
== NULL
|| fnm
== NULL
)
3351 goto error_free_dyn
;
3352 amt
= strlen (fnm
) + 1;
3353 anm
= bfd_alloc (abfd
, amt
);
3355 goto error_free_dyn
;
3356 memcpy (anm
, fnm
, amt
);
3360 for (pn
= & runpath
;
3366 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3367 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3369 struct bfd_link_needed_list
*n
, **pn
;
3371 unsigned int tagv
= dyn
.d_un
.d_val
;
3373 amt
= sizeof (struct bfd_link_needed_list
);
3374 n
= bfd_alloc (abfd
, amt
);
3375 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3376 if (n
== NULL
|| fnm
== NULL
)
3377 goto error_free_dyn
;
3378 amt
= strlen (fnm
) + 1;
3379 anm
= bfd_alloc (abfd
, amt
);
3386 memcpy (anm
, fnm
, amt
);
3401 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3402 frees all more recently bfd_alloc'd blocks as well. */
3408 struct bfd_link_needed_list
**pn
;
3409 for (pn
= & hash_table
->runpath
;
3416 /* We do not want to include any of the sections in a dynamic
3417 object in the output file. We hack by simply clobbering the
3418 list of sections in the BFD. This could be handled more
3419 cleanly by, say, a new section flag; the existing
3420 SEC_NEVER_LOAD flag is not the one we want, because that one
3421 still implies that the section takes up space in the output
3423 bfd_section_list_clear (abfd
);
3425 /* Find the name to use in a DT_NEEDED entry that refers to this
3426 object. If the object has a DT_SONAME entry, we use it.
3427 Otherwise, if the generic linker stuck something in
3428 elf_dt_name, we use that. Otherwise, we just use the file
3430 if (soname
== NULL
|| *soname
== '\0')
3432 soname
= elf_dt_name (abfd
);
3433 if (soname
== NULL
|| *soname
== '\0')
3434 soname
= bfd_get_filename (abfd
);
3437 /* Save the SONAME because sometimes the linker emulation code
3438 will need to know it. */
3439 elf_dt_name (abfd
) = soname
;
3441 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3445 /* If we have already included this dynamic object in the
3446 link, just ignore it. There is no reason to include a
3447 particular dynamic object more than once. */
3452 /* If this is a dynamic object, we always link against the .dynsym
3453 symbol table, not the .symtab symbol table. The dynamic linker
3454 will only see the .dynsym symbol table, so there is no reason to
3455 look at .symtab for a dynamic object. */
3457 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3458 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3460 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3462 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3464 /* The sh_info field of the symtab header tells us where the
3465 external symbols start. We don't care about the local symbols at
3467 if (elf_bad_symtab (abfd
))
3469 extsymcount
= symcount
;
3474 extsymcount
= symcount
- hdr
->sh_info
;
3475 extsymoff
= hdr
->sh_info
;
3479 if (extsymcount
!= 0)
3481 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3483 if (isymbuf
== NULL
)
3486 /* We store a pointer to the hash table entry for each external
3488 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3489 sym_hash
= bfd_alloc (abfd
, amt
);
3490 if (sym_hash
== NULL
)
3491 goto error_free_sym
;
3492 elf_sym_hashes (abfd
) = sym_hash
;
3497 /* Read in any version definitions. */
3498 if (!_bfd_elf_slurp_version_tables (abfd
,
3499 info
->default_imported_symver
))
3500 goto error_free_sym
;
3502 /* Read in the symbol versions, but don't bother to convert them
3503 to internal format. */
3504 if (elf_dynversym (abfd
) != 0)
3506 Elf_Internal_Shdr
*versymhdr
;
3508 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3509 extversym
= bfd_malloc (versymhdr
->sh_size
);
3510 if (extversym
== NULL
)
3511 goto error_free_sym
;
3512 amt
= versymhdr
->sh_size
;
3513 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3514 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3515 goto error_free_vers
;
3521 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3522 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3524 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3531 struct elf_link_hash_entry
*h
;
3532 bfd_boolean definition
;
3533 bfd_boolean size_change_ok
;
3534 bfd_boolean type_change_ok
;
3535 bfd_boolean new_weakdef
;
3536 bfd_boolean override
;
3537 unsigned int old_alignment
;
3542 flags
= BSF_NO_FLAGS
;
3544 value
= isym
->st_value
;
3547 bind
= ELF_ST_BIND (isym
->st_info
);
3548 if (bind
== STB_LOCAL
)
3550 /* This should be impossible, since ELF requires that all
3551 global symbols follow all local symbols, and that sh_info
3552 point to the first global symbol. Unfortunately, Irix 5
3556 else if (bind
== STB_GLOBAL
)
3558 if (isym
->st_shndx
!= SHN_UNDEF
3559 && isym
->st_shndx
!= SHN_COMMON
)
3562 else if (bind
== STB_WEAK
)
3566 /* Leave it up to the processor backend. */
3569 if (isym
->st_shndx
== SHN_UNDEF
)
3570 sec
= bfd_und_section_ptr
;
3571 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3573 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3575 sec
= bfd_abs_section_ptr
;
3576 else if (sec
->kept_section
)
3578 /* Symbols from discarded section are undefined, and have
3579 default visibility. */
3580 sec
= bfd_und_section_ptr
;
3581 isym
->st_shndx
= SHN_UNDEF
;
3582 isym
->st_other
= STV_DEFAULT
3583 | (isym
->st_other
& ~ ELF_ST_VISIBILITY(-1));
3585 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3588 else if (isym
->st_shndx
== SHN_ABS
)
3589 sec
= bfd_abs_section_ptr
;
3590 else if (isym
->st_shndx
== SHN_COMMON
)
3592 sec
= bfd_com_section_ptr
;
3593 /* What ELF calls the size we call the value. What ELF
3594 calls the value we call the alignment. */
3595 value
= isym
->st_size
;
3599 /* Leave it up to the processor backend. */
3602 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3605 goto error_free_vers
;
3607 if (isym
->st_shndx
== SHN_COMMON
3608 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3610 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3614 tcomm
= bfd_make_section (abfd
, ".tcommon");
3616 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3618 | SEC_LINKER_CREATED
3619 | SEC_THREAD_LOCAL
)))
3620 goto error_free_vers
;
3624 else if (add_symbol_hook
)
3626 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3628 goto error_free_vers
;
3630 /* The hook function sets the name to NULL if this symbol
3631 should be skipped for some reason. */
3636 /* Sanity check that all possibilities were handled. */
3639 bfd_set_error (bfd_error_bad_value
);
3640 goto error_free_vers
;
3643 if (bfd_is_und_section (sec
)
3644 || bfd_is_com_section (sec
))
3649 size_change_ok
= FALSE
;
3650 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3654 if (is_elf_hash_table (hash_table
))
3656 Elf_Internal_Versym iver
;
3657 unsigned int vernum
= 0;
3662 if (info
->default_imported_symver
)
3663 /* Use the default symbol version created earlier. */
3664 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3669 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3671 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3673 /* If this is a hidden symbol, or if it is not version
3674 1, we append the version name to the symbol name.
3675 However, we do not modify a non-hidden absolute
3676 symbol, because it might be the version symbol
3677 itself. FIXME: What if it isn't? */
3678 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3679 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3682 size_t namelen
, verlen
, newlen
;
3685 if (isym
->st_shndx
!= SHN_UNDEF
)
3687 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3689 else if (vernum
> 1)
3691 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3697 (*_bfd_error_handler
)
3698 (_("%B: %s: invalid version %u (max %d)"),
3700 elf_tdata (abfd
)->cverdefs
);
3701 bfd_set_error (bfd_error_bad_value
);
3702 goto error_free_vers
;
3707 /* We cannot simply test for the number of
3708 entries in the VERNEED section since the
3709 numbers for the needed versions do not start
3711 Elf_Internal_Verneed
*t
;
3714 for (t
= elf_tdata (abfd
)->verref
;
3718 Elf_Internal_Vernaux
*a
;
3720 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3722 if (a
->vna_other
== vernum
)
3724 verstr
= a
->vna_nodename
;
3733 (*_bfd_error_handler
)
3734 (_("%B: %s: invalid needed version %d"),
3735 abfd
, name
, vernum
);
3736 bfd_set_error (bfd_error_bad_value
);
3737 goto error_free_vers
;
3741 namelen
= strlen (name
);
3742 verlen
= strlen (verstr
);
3743 newlen
= namelen
+ verlen
+ 2;
3744 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3745 && isym
->st_shndx
!= SHN_UNDEF
)
3748 newname
= bfd_alloc (abfd
, newlen
);
3749 if (newname
== NULL
)
3750 goto error_free_vers
;
3751 memcpy (newname
, name
, namelen
);
3752 p
= newname
+ namelen
;
3754 /* If this is a defined non-hidden version symbol,
3755 we add another @ to the name. This indicates the
3756 default version of the symbol. */
3757 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3758 && isym
->st_shndx
!= SHN_UNDEF
)
3760 memcpy (p
, verstr
, verlen
+ 1);
3765 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3766 sym_hash
, &skip
, &override
,
3767 &type_change_ok
, &size_change_ok
))
3768 goto error_free_vers
;
3777 while (h
->root
.type
== bfd_link_hash_indirect
3778 || h
->root
.type
== bfd_link_hash_warning
)
3779 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3781 /* Remember the old alignment if this is a common symbol, so
3782 that we don't reduce the alignment later on. We can't
3783 check later, because _bfd_generic_link_add_one_symbol
3784 will set a default for the alignment which we want to
3785 override. We also remember the old bfd where the existing
3786 definition comes from. */
3787 switch (h
->root
.type
)
3792 case bfd_link_hash_defined
:
3793 case bfd_link_hash_defweak
:
3794 old_bfd
= h
->root
.u
.def
.section
->owner
;
3797 case bfd_link_hash_common
:
3798 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3799 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3803 if (elf_tdata (abfd
)->verdef
!= NULL
3807 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3810 if (! (_bfd_generic_link_add_one_symbol
3811 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3812 (struct bfd_link_hash_entry
**) sym_hash
)))
3813 goto error_free_vers
;
3816 while (h
->root
.type
== bfd_link_hash_indirect
3817 || h
->root
.type
== bfd_link_hash_warning
)
3818 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3821 new_weakdef
= FALSE
;
3824 && (flags
& BSF_WEAK
) != 0
3825 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3826 && is_elf_hash_table (hash_table
)
3827 && h
->u
.weakdef
== NULL
)
3829 /* Keep a list of all weak defined non function symbols from
3830 a dynamic object, using the weakdef field. Later in this
3831 function we will set the weakdef field to the correct
3832 value. We only put non-function symbols from dynamic
3833 objects on this list, because that happens to be the only
3834 time we need to know the normal symbol corresponding to a
3835 weak symbol, and the information is time consuming to
3836 figure out. If the weakdef field is not already NULL,
3837 then this symbol was already defined by some previous
3838 dynamic object, and we will be using that previous
3839 definition anyhow. */
3841 h
->u
.weakdef
= weaks
;
3846 /* Set the alignment of a common symbol. */
3847 if (isym
->st_shndx
== SHN_COMMON
3848 && h
->root
.type
== bfd_link_hash_common
)
3852 align
= bfd_log2 (isym
->st_value
);
3853 if (align
> old_alignment
3854 /* Permit an alignment power of zero if an alignment of one
3855 is specified and no other alignments have been specified. */
3856 || (isym
->st_value
== 1 && old_alignment
== 0))
3857 h
->root
.u
.c
.p
->alignment_power
= align
;
3859 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3862 if (is_elf_hash_table (hash_table
))
3866 /* Check the alignment when a common symbol is involved. This
3867 can change when a common symbol is overridden by a normal
3868 definition or a common symbol is ignored due to the old
3869 normal definition. We need to make sure the maximum
3870 alignment is maintained. */
3871 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3872 && h
->root
.type
!= bfd_link_hash_common
)
3874 unsigned int common_align
;
3875 unsigned int normal_align
;
3876 unsigned int symbol_align
;
3880 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3881 if (h
->root
.u
.def
.section
->owner
!= NULL
3882 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3884 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3885 if (normal_align
> symbol_align
)
3886 normal_align
= symbol_align
;
3889 normal_align
= symbol_align
;
3893 common_align
= old_alignment
;
3894 common_bfd
= old_bfd
;
3899 common_align
= bfd_log2 (isym
->st_value
);
3901 normal_bfd
= old_bfd
;
3904 if (normal_align
< common_align
)
3905 (*_bfd_error_handler
)
3906 (_("Warning: alignment %u of symbol `%s' in %B"
3907 " is smaller than %u in %B"),
3908 normal_bfd
, common_bfd
,
3909 1 << normal_align
, name
, 1 << common_align
);
3912 /* Remember the symbol size and type. */
3913 if (isym
->st_size
!= 0
3914 && (definition
|| h
->size
== 0))
3916 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3917 (*_bfd_error_handler
)
3918 (_("Warning: size of symbol `%s' changed"
3919 " from %lu in %B to %lu in %B"),
3921 name
, (unsigned long) h
->size
,
3922 (unsigned long) isym
->st_size
);
3924 h
->size
= isym
->st_size
;
3927 /* If this is a common symbol, then we always want H->SIZE
3928 to be the size of the common symbol. The code just above
3929 won't fix the size if a common symbol becomes larger. We
3930 don't warn about a size change here, because that is
3931 covered by --warn-common. */
3932 if (h
->root
.type
== bfd_link_hash_common
)
3933 h
->size
= h
->root
.u
.c
.size
;
3935 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3936 && (definition
|| h
->type
== STT_NOTYPE
))
3938 if (h
->type
!= STT_NOTYPE
3939 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3940 && ! type_change_ok
)
3941 (*_bfd_error_handler
)
3942 (_("Warning: type of symbol `%s' changed"
3943 " from %d to %d in %B"),
3944 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3946 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3949 /* If st_other has a processor-specific meaning, specific
3950 code might be needed here. We never merge the visibility
3951 attribute with the one from a dynamic object. */
3952 if (bed
->elf_backend_merge_symbol_attribute
)
3953 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3956 /* If this symbol has default visibility and the user has requested
3957 we not re-export it, then mark it as hidden. */
3958 if (definition
&& !dynamic
3960 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3961 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3962 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3964 if (isym
->st_other
!= 0 && !dynamic
)
3966 unsigned char hvis
, symvis
, other
, nvis
;
3968 /* Take the balance of OTHER from the definition. */
3969 other
= (definition
? isym
->st_other
: h
->other
);
3970 other
&= ~ ELF_ST_VISIBILITY (-1);
3972 /* Combine visibilities, using the most constraining one. */
3973 hvis
= ELF_ST_VISIBILITY (h
->other
);
3974 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3980 nvis
= hvis
< symvis
? hvis
: symvis
;
3982 h
->other
= other
| nvis
;
3985 /* Set a flag in the hash table entry indicating the type of
3986 reference or definition we just found. Keep a count of
3987 the number of dynamic symbols we find. A dynamic symbol
3988 is one which is referenced or defined by both a regular
3989 object and a shared object. */
3996 if (bind
!= STB_WEAK
)
3997 h
->ref_regular_nonweak
= 1;
4001 if (! info
->executable
4014 || (h
->u
.weakdef
!= NULL
4016 && h
->u
.weakdef
->dynindx
!= -1))
4020 /* Check to see if we need to add an indirect symbol for
4021 the default name. */
4022 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4023 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4024 &sec
, &value
, &dynsym
,
4026 goto error_free_vers
;
4028 if (definition
&& !dynamic
)
4030 char *p
= strchr (name
, ELF_VER_CHR
);
4031 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4033 /* Queue non-default versions so that .symver x, x@FOO
4034 aliases can be checked. */
4035 if (! nondeflt_vers
)
4037 amt
= (isymend
- isym
+ 1)
4038 * sizeof (struct elf_link_hash_entry
*);
4039 nondeflt_vers
= bfd_malloc (amt
);
4041 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4045 if (dynsym
&& h
->dynindx
== -1)
4047 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4048 goto error_free_vers
;
4049 if (h
->u
.weakdef
!= NULL
4051 && h
->u
.weakdef
->dynindx
== -1)
4053 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4054 goto error_free_vers
;
4057 else if (dynsym
&& h
->dynindx
!= -1)
4058 /* If the symbol already has a dynamic index, but
4059 visibility says it should not be visible, turn it into
4061 switch (ELF_ST_VISIBILITY (h
->other
))
4065 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4076 const char *soname
= elf_dt_name (abfd
);
4078 /* A symbol from a library loaded via DT_NEEDED of some
4079 other library is referenced by a regular object.
4080 Add a DT_NEEDED entry for it. Issue an error if
4081 --no-add-needed is used. */
4082 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4084 (*_bfd_error_handler
)
4085 (_("%s: invalid DSO for symbol `%s' definition"),
4087 bfd_set_error (bfd_error_bad_value
);
4088 goto error_free_vers
;
4091 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4094 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4096 goto error_free_vers
;
4098 BFD_ASSERT (ret
== 0);
4103 /* Now that all the symbols from this input file are created, handle
4104 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4105 if (nondeflt_vers
!= NULL
)
4107 bfd_size_type cnt
, symidx
;
4109 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4111 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4112 char *shortname
, *p
;
4114 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4116 || (h
->root
.type
!= bfd_link_hash_defined
4117 && h
->root
.type
!= bfd_link_hash_defweak
))
4120 amt
= p
- h
->root
.root
.string
;
4121 shortname
= bfd_malloc (amt
+ 1);
4122 memcpy (shortname
, h
->root
.root
.string
, amt
);
4123 shortname
[amt
] = '\0';
4125 hi
= (struct elf_link_hash_entry
*)
4126 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
4127 FALSE
, FALSE
, FALSE
);
4129 && hi
->root
.type
== h
->root
.type
4130 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4131 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4133 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4134 hi
->root
.type
= bfd_link_hash_indirect
;
4135 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4136 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
4137 sym_hash
= elf_sym_hashes (abfd
);
4139 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4140 if (sym_hash
[symidx
] == hi
)
4142 sym_hash
[symidx
] = h
;
4148 free (nondeflt_vers
);
4149 nondeflt_vers
= NULL
;
4152 if (extversym
!= NULL
)
4158 if (isymbuf
!= NULL
)
4163 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4165 /* Remove symbols defined in an as-needed shared lib that wasn't
4167 struct elf_smash_syms_data inf
;
4168 inf
.not_needed
= abfd
;
4169 inf
.htab
= hash_table
;
4170 inf
.twiddled
= FALSE
;
4171 elf_link_hash_traverse (hash_table
, elf_smash_syms
, &inf
);
4173 bfd_link_repair_undef_list (&hash_table
->root
);
4177 /* Now set the weakdefs field correctly for all the weak defined
4178 symbols we found. The only way to do this is to search all the
4179 symbols. Since we only need the information for non functions in
4180 dynamic objects, that's the only time we actually put anything on
4181 the list WEAKS. We need this information so that if a regular
4182 object refers to a symbol defined weakly in a dynamic object, the
4183 real symbol in the dynamic object is also put in the dynamic
4184 symbols; we also must arrange for both symbols to point to the
4185 same memory location. We could handle the general case of symbol
4186 aliasing, but a general symbol alias can only be generated in
4187 assembler code, handling it correctly would be very time
4188 consuming, and other ELF linkers don't handle general aliasing
4192 struct elf_link_hash_entry
**hpp
;
4193 struct elf_link_hash_entry
**hppend
;
4194 struct elf_link_hash_entry
**sorted_sym_hash
;
4195 struct elf_link_hash_entry
*h
;
4198 /* Since we have to search the whole symbol list for each weak
4199 defined symbol, search time for N weak defined symbols will be
4200 O(N^2). Binary search will cut it down to O(NlogN). */
4201 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4202 sorted_sym_hash
= bfd_malloc (amt
);
4203 if (sorted_sym_hash
== NULL
)
4205 sym_hash
= sorted_sym_hash
;
4206 hpp
= elf_sym_hashes (abfd
);
4207 hppend
= hpp
+ extsymcount
;
4209 for (; hpp
< hppend
; hpp
++)
4213 && h
->root
.type
== bfd_link_hash_defined
4214 && h
->type
!= STT_FUNC
)
4222 qsort (sorted_sym_hash
, sym_count
,
4223 sizeof (struct elf_link_hash_entry
*),
4226 while (weaks
!= NULL
)
4228 struct elf_link_hash_entry
*hlook
;
4235 weaks
= hlook
->u
.weakdef
;
4236 hlook
->u
.weakdef
= NULL
;
4238 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4239 || hlook
->root
.type
== bfd_link_hash_defweak
4240 || hlook
->root
.type
== bfd_link_hash_common
4241 || hlook
->root
.type
== bfd_link_hash_indirect
);
4242 slook
= hlook
->root
.u
.def
.section
;
4243 vlook
= hlook
->root
.u
.def
.value
;
4250 bfd_signed_vma vdiff
;
4252 h
= sorted_sym_hash
[idx
];
4253 vdiff
= vlook
- h
->root
.u
.def
.value
;
4260 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4273 /* We didn't find a value/section match. */
4277 for (i
= ilook
; i
< sym_count
; i
++)
4279 h
= sorted_sym_hash
[i
];
4281 /* Stop if value or section doesn't match. */
4282 if (h
->root
.u
.def
.value
!= vlook
4283 || h
->root
.u
.def
.section
!= slook
)
4285 else if (h
!= hlook
)
4287 hlook
->u
.weakdef
= h
;
4289 /* If the weak definition is in the list of dynamic
4290 symbols, make sure the real definition is put
4292 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4294 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4298 /* If the real definition is in the list of dynamic
4299 symbols, make sure the weak definition is put
4300 there as well. If we don't do this, then the
4301 dynamic loader might not merge the entries for the
4302 real definition and the weak definition. */
4303 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4305 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4313 free (sorted_sym_hash
);
4316 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4317 if (check_directives
)
4318 check_directives (abfd
, info
);
4320 /* If this object is the same format as the output object, and it is
4321 not a shared library, then let the backend look through the
4324 This is required to build global offset table entries and to
4325 arrange for dynamic relocs. It is not required for the
4326 particular common case of linking non PIC code, even when linking
4327 against shared libraries, but unfortunately there is no way of
4328 knowing whether an object file has been compiled PIC or not.
4329 Looking through the relocs is not particularly time consuming.
4330 The problem is that we must either (1) keep the relocs in memory,
4331 which causes the linker to require additional runtime memory or
4332 (2) read the relocs twice from the input file, which wastes time.
4333 This would be a good case for using mmap.
4335 I have no idea how to handle linking PIC code into a file of a
4336 different format. It probably can't be done. */
4337 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4339 && is_elf_hash_table (hash_table
)
4340 && hash_table
->root
.creator
== abfd
->xvec
4341 && check_relocs
!= NULL
)
4345 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4347 Elf_Internal_Rela
*internal_relocs
;
4350 if ((o
->flags
& SEC_RELOC
) == 0
4351 || o
->reloc_count
== 0
4352 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4353 && (o
->flags
& SEC_DEBUGGING
) != 0)
4354 || bfd_is_abs_section (o
->output_section
))
4357 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4359 if (internal_relocs
== NULL
)
4362 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4364 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4365 free (internal_relocs
);
4372 /* If this is a non-traditional link, try to optimize the handling
4373 of the .stab/.stabstr sections. */
4375 && ! info
->traditional_format
4376 && is_elf_hash_table (hash_table
)
4377 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4381 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4382 if (stabstr
!= NULL
)
4384 bfd_size_type string_offset
= 0;
4387 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4388 if (strncmp (".stab", stab
->name
, 5) == 0
4389 && (!stab
->name
[5] ||
4390 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4391 && (stab
->flags
& SEC_MERGE
) == 0
4392 && !bfd_is_abs_section (stab
->output_section
))
4394 struct bfd_elf_section_data
*secdata
;
4396 secdata
= elf_section_data (stab
);
4397 if (! _bfd_link_section_stabs (abfd
,
4398 &hash_table
->stab_info
,
4403 if (secdata
->sec_info
)
4404 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4409 if (is_elf_hash_table (hash_table
) && add_needed
)
4411 /* Add this bfd to the loaded list. */
4412 struct elf_link_loaded_list
*n
;
4414 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4418 n
->next
= hash_table
->loaded
;
4419 hash_table
->loaded
= n
;
4425 if (nondeflt_vers
!= NULL
)
4426 free (nondeflt_vers
);
4427 if (extversym
!= NULL
)
4430 if (isymbuf
!= NULL
)
4436 /* Return the linker hash table entry of a symbol that might be
4437 satisfied by an archive symbol. Return -1 on error. */
4439 struct elf_link_hash_entry
*
4440 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4441 struct bfd_link_info
*info
,
4444 struct elf_link_hash_entry
*h
;
4448 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4452 /* If this is a default version (the name contains @@), look up the
4453 symbol again with only one `@' as well as without the version.
4454 The effect is that references to the symbol with and without the
4455 version will be matched by the default symbol in the archive. */
4457 p
= strchr (name
, ELF_VER_CHR
);
4458 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4461 /* First check with only one `@'. */
4462 len
= strlen (name
);
4463 copy
= bfd_alloc (abfd
, len
);
4465 return (struct elf_link_hash_entry
*) 0 - 1;
4467 first
= p
- name
+ 1;
4468 memcpy (copy
, name
, first
);
4469 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4471 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4474 /* We also need to check references to the symbol without the
4476 copy
[first
- 1] = '\0';
4477 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4478 FALSE
, FALSE
, FALSE
);
4481 bfd_release (abfd
, copy
);
4485 /* Add symbols from an ELF archive file to the linker hash table. We
4486 don't use _bfd_generic_link_add_archive_symbols because of a
4487 problem which arises on UnixWare. The UnixWare libc.so is an
4488 archive which includes an entry libc.so.1 which defines a bunch of
4489 symbols. The libc.so archive also includes a number of other
4490 object files, which also define symbols, some of which are the same
4491 as those defined in libc.so.1. Correct linking requires that we
4492 consider each object file in turn, and include it if it defines any
4493 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4494 this; it looks through the list of undefined symbols, and includes
4495 any object file which defines them. When this algorithm is used on
4496 UnixWare, it winds up pulling in libc.so.1 early and defining a
4497 bunch of symbols. This means that some of the other objects in the
4498 archive are not included in the link, which is incorrect since they
4499 precede libc.so.1 in the archive.
4501 Fortunately, ELF archive handling is simpler than that done by
4502 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4503 oddities. In ELF, if we find a symbol in the archive map, and the
4504 symbol is currently undefined, we know that we must pull in that
4507 Unfortunately, we do have to make multiple passes over the symbol
4508 table until nothing further is resolved. */
4511 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4514 bfd_boolean
*defined
= NULL
;
4515 bfd_boolean
*included
= NULL
;
4519 const struct elf_backend_data
*bed
;
4520 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4521 (bfd
*, struct bfd_link_info
*, const char *);
4523 if (! bfd_has_map (abfd
))
4525 /* An empty archive is a special case. */
4526 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4528 bfd_set_error (bfd_error_no_armap
);
4532 /* Keep track of all symbols we know to be already defined, and all
4533 files we know to be already included. This is to speed up the
4534 second and subsequent passes. */
4535 c
= bfd_ardata (abfd
)->symdef_count
;
4539 amt
*= sizeof (bfd_boolean
);
4540 defined
= bfd_zmalloc (amt
);
4541 included
= bfd_zmalloc (amt
);
4542 if (defined
== NULL
|| included
== NULL
)
4545 symdefs
= bfd_ardata (abfd
)->symdefs
;
4546 bed
= get_elf_backend_data (abfd
);
4547 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4560 symdefend
= symdef
+ c
;
4561 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4563 struct elf_link_hash_entry
*h
;
4565 struct bfd_link_hash_entry
*undefs_tail
;
4568 if (defined
[i
] || included
[i
])
4570 if (symdef
->file_offset
== last
)
4576 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4577 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4583 if (h
->root
.type
== bfd_link_hash_common
)
4585 /* We currently have a common symbol. The archive map contains
4586 a reference to this symbol, so we may want to include it. We
4587 only want to include it however, if this archive element
4588 contains a definition of the symbol, not just another common
4591 Unfortunately some archivers (including GNU ar) will put
4592 declarations of common symbols into their archive maps, as
4593 well as real definitions, so we cannot just go by the archive
4594 map alone. Instead we must read in the element's symbol
4595 table and check that to see what kind of symbol definition
4597 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4600 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4602 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4607 /* We need to include this archive member. */
4608 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4609 if (element
== NULL
)
4612 if (! bfd_check_format (element
, bfd_object
))
4615 /* Doublecheck that we have not included this object
4616 already--it should be impossible, but there may be
4617 something wrong with the archive. */
4618 if (element
->archive_pass
!= 0)
4620 bfd_set_error (bfd_error_bad_value
);
4623 element
->archive_pass
= 1;
4625 undefs_tail
= info
->hash
->undefs_tail
;
4627 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4630 if (! bfd_link_add_symbols (element
, info
))
4633 /* If there are any new undefined symbols, we need to make
4634 another pass through the archive in order to see whether
4635 they can be defined. FIXME: This isn't perfect, because
4636 common symbols wind up on undefs_tail and because an
4637 undefined symbol which is defined later on in this pass
4638 does not require another pass. This isn't a bug, but it
4639 does make the code less efficient than it could be. */
4640 if (undefs_tail
!= info
->hash
->undefs_tail
)
4643 /* Look backward to mark all symbols from this object file
4644 which we have already seen in this pass. */
4648 included
[mark
] = TRUE
;
4653 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4655 /* We mark subsequent symbols from this object file as we go
4656 on through the loop. */
4657 last
= symdef
->file_offset
;
4668 if (defined
!= NULL
)
4670 if (included
!= NULL
)
4675 /* Given an ELF BFD, add symbols to the global hash table as
4679 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4681 switch (bfd_get_format (abfd
))
4684 return elf_link_add_object_symbols (abfd
, info
);
4686 return elf_link_add_archive_symbols (abfd
, info
);
4688 bfd_set_error (bfd_error_wrong_format
);
4693 /* This function will be called though elf_link_hash_traverse to store
4694 all hash value of the exported symbols in an array. */
4697 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4699 unsigned long **valuep
= data
;
4705 if (h
->root
.type
== bfd_link_hash_warning
)
4706 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4708 /* Ignore indirect symbols. These are added by the versioning code. */
4709 if (h
->dynindx
== -1)
4712 name
= h
->root
.root
.string
;
4713 p
= strchr (name
, ELF_VER_CHR
);
4716 alc
= bfd_malloc (p
- name
+ 1);
4717 memcpy (alc
, name
, p
- name
);
4718 alc
[p
- name
] = '\0';
4722 /* Compute the hash value. */
4723 ha
= bfd_elf_hash (name
);
4725 /* Store the found hash value in the array given as the argument. */
4728 /* And store it in the struct so that we can put it in the hash table
4730 h
->u
.elf_hash_value
= ha
;
4738 /* Array used to determine the number of hash table buckets to use
4739 based on the number of symbols there are. If there are fewer than
4740 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4741 fewer than 37 we use 17 buckets, and so forth. We never use more
4742 than 32771 buckets. */
4744 static const size_t elf_buckets
[] =
4746 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4750 /* Compute bucket count for hashing table. We do not use a static set
4751 of possible tables sizes anymore. Instead we determine for all
4752 possible reasonable sizes of the table the outcome (i.e., the
4753 number of collisions etc) and choose the best solution. The
4754 weighting functions are not too simple to allow the table to grow
4755 without bounds. Instead one of the weighting factors is the size.
4756 Therefore the result is always a good payoff between few collisions
4757 (= short chain lengths) and table size. */
4759 compute_bucket_count (struct bfd_link_info
*info
)
4761 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4762 size_t best_size
= 0;
4763 unsigned long int *hashcodes
;
4764 unsigned long int *hashcodesp
;
4765 unsigned long int i
;
4768 /* Compute the hash values for all exported symbols. At the same
4769 time store the values in an array so that we could use them for
4772 amt
*= sizeof (unsigned long int);
4773 hashcodes
= bfd_malloc (amt
);
4774 if (hashcodes
== NULL
)
4776 hashcodesp
= hashcodes
;
4778 /* Put all hash values in HASHCODES. */
4779 elf_link_hash_traverse (elf_hash_table (info
),
4780 elf_collect_hash_codes
, &hashcodesp
);
4782 /* We have a problem here. The following code to optimize the table
4783 size requires an integer type with more the 32 bits. If
4784 BFD_HOST_U_64_BIT is set we know about such a type. */
4785 #ifdef BFD_HOST_U_64_BIT
4788 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4791 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4792 unsigned long int *counts
;
4793 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4794 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4796 /* Possible optimization parameters: if we have NSYMS symbols we say
4797 that the hashing table must at least have NSYMS/4 and at most
4799 minsize
= nsyms
/ 4;
4802 best_size
= maxsize
= nsyms
* 2;
4804 /* Create array where we count the collisions in. We must use bfd_malloc
4805 since the size could be large. */
4807 amt
*= sizeof (unsigned long int);
4808 counts
= bfd_malloc (amt
);
4815 /* Compute the "optimal" size for the hash table. The criteria is a
4816 minimal chain length. The minor criteria is (of course) the size
4818 for (i
= minsize
; i
< maxsize
; ++i
)
4820 /* Walk through the array of hashcodes and count the collisions. */
4821 BFD_HOST_U_64_BIT max
;
4822 unsigned long int j
;
4823 unsigned long int fact
;
4825 memset (counts
, '\0', i
* sizeof (unsigned long int));
4827 /* Determine how often each hash bucket is used. */
4828 for (j
= 0; j
< nsyms
; ++j
)
4829 ++counts
[hashcodes
[j
] % i
];
4831 /* For the weight function we need some information about the
4832 pagesize on the target. This is information need not be 100%
4833 accurate. Since this information is not available (so far) we
4834 define it here to a reasonable default value. If it is crucial
4835 to have a better value some day simply define this value. */
4836 # ifndef BFD_TARGET_PAGESIZE
4837 # define BFD_TARGET_PAGESIZE (4096)
4840 /* We in any case need 2 + NSYMS entries for the size values and
4842 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4845 /* Variant 1: optimize for short chains. We add the squares
4846 of all the chain lengths (which favors many small chain
4847 over a few long chains). */
4848 for (j
= 0; j
< i
; ++j
)
4849 max
+= counts
[j
] * counts
[j
];
4851 /* This adds penalties for the overall size of the table. */
4852 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4855 /* Variant 2: Optimize a lot more for small table. Here we
4856 also add squares of the size but we also add penalties for
4857 empty slots (the +1 term). */
4858 for (j
= 0; j
< i
; ++j
)
4859 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4861 /* The overall size of the table is considered, but not as
4862 strong as in variant 1, where it is squared. */
4863 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4867 /* Compare with current best results. */
4868 if (max
< best_chlen
)
4878 #endif /* defined (BFD_HOST_U_64_BIT) */
4880 /* This is the fallback solution if no 64bit type is available or if we
4881 are not supposed to spend much time on optimizations. We select the
4882 bucket count using a fixed set of numbers. */
4883 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4885 best_size
= elf_buckets
[i
];
4886 if (dynsymcount
< elf_buckets
[i
+ 1])
4891 /* Free the arrays we needed. */
4897 /* Set up the sizes and contents of the ELF dynamic sections. This is
4898 called by the ELF linker emulation before_allocation routine. We
4899 must set the sizes of the sections before the linker sets the
4900 addresses of the various sections. */
4903 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4906 const char *filter_shlib
,
4907 const char * const *auxiliary_filters
,
4908 struct bfd_link_info
*info
,
4909 asection
**sinterpptr
,
4910 struct bfd_elf_version_tree
*verdefs
)
4912 bfd_size_type soname_indx
;
4914 const struct elf_backend_data
*bed
;
4915 struct elf_assign_sym_version_info asvinfo
;
4919 soname_indx
= (bfd_size_type
) -1;
4921 if (!is_elf_hash_table (info
->hash
))
4924 elf_tdata (output_bfd
)->relro
= info
->relro
;
4925 if (info
->execstack
)
4926 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4927 else if (info
->noexecstack
)
4928 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4932 asection
*notesec
= NULL
;
4935 for (inputobj
= info
->input_bfds
;
4937 inputobj
= inputobj
->link_next
)
4941 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4943 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4946 if (s
->flags
& SEC_CODE
)
4955 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4956 if (exec
&& info
->relocatable
4957 && notesec
->output_section
!= bfd_abs_section_ptr
)
4958 notesec
->output_section
->flags
|= SEC_CODE
;
4962 /* Any syms created from now on start with -1 in
4963 got.refcount/offset and plt.refcount/offset. */
4964 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4966 /* The backend may have to create some sections regardless of whether
4967 we're dynamic or not. */
4968 bed
= get_elf_backend_data (output_bfd
);
4969 if (bed
->elf_backend_always_size_sections
4970 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4973 dynobj
= elf_hash_table (info
)->dynobj
;
4975 /* If there were no dynamic objects in the link, there is nothing to
4980 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4983 if (elf_hash_table (info
)->dynamic_sections_created
)
4985 struct elf_info_failed eif
;
4986 struct elf_link_hash_entry
*h
;
4988 struct bfd_elf_version_tree
*t
;
4989 struct bfd_elf_version_expr
*d
;
4990 bfd_boolean all_defined
;
4992 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4993 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4997 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4999 if (soname_indx
== (bfd_size_type
) -1
5000 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5006 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5008 info
->flags
|= DF_SYMBOLIC
;
5015 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5017 if (indx
== (bfd_size_type
) -1
5018 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5021 if (info
->new_dtags
)
5023 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5024 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5029 if (filter_shlib
!= NULL
)
5033 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5034 filter_shlib
, TRUE
);
5035 if (indx
== (bfd_size_type
) -1
5036 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5040 if (auxiliary_filters
!= NULL
)
5042 const char * const *p
;
5044 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5048 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5050 if (indx
== (bfd_size_type
) -1
5051 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5057 eif
.verdefs
= verdefs
;
5060 /* If we are supposed to export all symbols into the dynamic symbol
5061 table (this is not the normal case), then do so. */
5062 if (info
->export_dynamic
)
5064 elf_link_hash_traverse (elf_hash_table (info
),
5065 _bfd_elf_export_symbol
,
5071 /* Make all global versions with definition. */
5072 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5073 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5074 if (!d
->symver
&& d
->symbol
)
5076 const char *verstr
, *name
;
5077 size_t namelen
, verlen
, newlen
;
5079 struct elf_link_hash_entry
*newh
;
5082 namelen
= strlen (name
);
5084 verlen
= strlen (verstr
);
5085 newlen
= namelen
+ verlen
+ 3;
5087 newname
= bfd_malloc (newlen
);
5088 if (newname
== NULL
)
5090 memcpy (newname
, name
, namelen
);
5092 /* Check the hidden versioned definition. */
5093 p
= newname
+ namelen
;
5095 memcpy (p
, verstr
, verlen
+ 1);
5096 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5097 newname
, FALSE
, FALSE
,
5100 || (newh
->root
.type
!= bfd_link_hash_defined
5101 && newh
->root
.type
!= bfd_link_hash_defweak
))
5103 /* Check the default versioned definition. */
5105 memcpy (p
, verstr
, verlen
+ 1);
5106 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5107 newname
, FALSE
, FALSE
,
5112 /* Mark this version if there is a definition and it is
5113 not defined in a shared object. */
5115 && !newh
->def_dynamic
5116 && (newh
->root
.type
== bfd_link_hash_defined
5117 || newh
->root
.type
== bfd_link_hash_defweak
))
5121 /* Attach all the symbols to their version information. */
5122 asvinfo
.output_bfd
= output_bfd
;
5123 asvinfo
.info
= info
;
5124 asvinfo
.verdefs
= verdefs
;
5125 asvinfo
.failed
= FALSE
;
5127 elf_link_hash_traverse (elf_hash_table (info
),
5128 _bfd_elf_link_assign_sym_version
,
5133 if (!info
->allow_undefined_version
)
5135 /* Check if all global versions have a definition. */
5137 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5138 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5139 if (!d
->symver
&& !d
->script
)
5141 (*_bfd_error_handler
)
5142 (_("%s: undefined version: %s"),
5143 d
->pattern
, t
->name
);
5144 all_defined
= FALSE
;
5149 bfd_set_error (bfd_error_bad_value
);
5154 /* Find all symbols which were defined in a dynamic object and make
5155 the backend pick a reasonable value for them. */
5156 elf_link_hash_traverse (elf_hash_table (info
),
5157 _bfd_elf_adjust_dynamic_symbol
,
5162 /* Add some entries to the .dynamic section. We fill in some of the
5163 values later, in bfd_elf_final_link, but we must add the entries
5164 now so that we know the final size of the .dynamic section. */
5166 /* If there are initialization and/or finalization functions to
5167 call then add the corresponding DT_INIT/DT_FINI entries. */
5168 h
= (info
->init_function
5169 ? elf_link_hash_lookup (elf_hash_table (info
),
5170 info
->init_function
, FALSE
,
5177 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5180 h
= (info
->fini_function
5181 ? elf_link_hash_lookup (elf_hash_table (info
),
5182 info
->fini_function
, FALSE
,
5189 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5193 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5195 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5196 if (! info
->executable
)
5201 for (sub
= info
->input_bfds
; sub
!= NULL
;
5202 sub
= sub
->link_next
)
5203 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5204 if (elf_section_data (o
)->this_hdr
.sh_type
5205 == SHT_PREINIT_ARRAY
)
5207 (*_bfd_error_handler
)
5208 (_("%B: .preinit_array section is not allowed in DSO"),
5213 bfd_set_error (bfd_error_nonrepresentable_section
);
5217 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5218 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5221 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5223 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5224 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5227 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5229 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5230 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5234 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5235 /* If .dynstr is excluded from the link, we don't want any of
5236 these tags. Strictly, we should be checking each section
5237 individually; This quick check covers for the case where
5238 someone does a /DISCARD/ : { *(*) }. */
5239 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5241 bfd_size_type strsize
;
5243 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5244 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5245 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5246 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5247 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5248 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5249 bed
->s
->sizeof_sym
))
5254 /* The backend must work out the sizes of all the other dynamic
5256 if (bed
->elf_backend_size_dynamic_sections
5257 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5260 if (elf_hash_table (info
)->dynamic_sections_created
)
5262 bfd_size_type dynsymcount
;
5264 size_t bucketcount
= 0;
5265 size_t hash_entry_size
;
5266 unsigned int dtagcount
;
5268 /* Set up the version definition section. */
5269 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5270 BFD_ASSERT (s
!= NULL
);
5272 /* We may have created additional version definitions if we are
5273 just linking a regular application. */
5274 verdefs
= asvinfo
.verdefs
;
5276 /* Skip anonymous version tag. */
5277 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5278 verdefs
= verdefs
->next
;
5280 if (verdefs
== NULL
&& !info
->create_default_symver
)
5281 _bfd_strip_section_from_output (info
, s
);
5286 struct bfd_elf_version_tree
*t
;
5288 Elf_Internal_Verdef def
;
5289 Elf_Internal_Verdaux defaux
;
5290 struct bfd_link_hash_entry
*bh
;
5291 struct elf_link_hash_entry
*h
;
5297 /* Make space for the base version. */
5298 size
+= sizeof (Elf_External_Verdef
);
5299 size
+= sizeof (Elf_External_Verdaux
);
5302 /* Make space for the default version. */
5303 if (info
->create_default_symver
)
5305 size
+= sizeof (Elf_External_Verdef
);
5309 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5311 struct bfd_elf_version_deps
*n
;
5313 size
+= sizeof (Elf_External_Verdef
);
5314 size
+= sizeof (Elf_External_Verdaux
);
5317 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5318 size
+= sizeof (Elf_External_Verdaux
);
5322 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5323 if (s
->contents
== NULL
&& s
->size
!= 0)
5326 /* Fill in the version definition section. */
5330 def
.vd_version
= VER_DEF_CURRENT
;
5331 def
.vd_flags
= VER_FLG_BASE
;
5334 if (info
->create_default_symver
)
5336 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5337 def
.vd_next
= sizeof (Elf_External_Verdef
);
5341 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5342 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5343 + sizeof (Elf_External_Verdaux
));
5346 if (soname_indx
!= (bfd_size_type
) -1)
5348 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5350 def
.vd_hash
= bfd_elf_hash (soname
);
5351 defaux
.vda_name
= soname_indx
;
5358 name
= basename (output_bfd
->filename
);
5359 def
.vd_hash
= bfd_elf_hash (name
);
5360 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5362 if (indx
== (bfd_size_type
) -1)
5364 defaux
.vda_name
= indx
;
5366 defaux
.vda_next
= 0;
5368 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5369 (Elf_External_Verdef
*) p
);
5370 p
+= sizeof (Elf_External_Verdef
);
5371 if (info
->create_default_symver
)
5373 /* Add a symbol representing this version. */
5375 if (! (_bfd_generic_link_add_one_symbol
5376 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5378 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5380 h
= (struct elf_link_hash_entry
*) bh
;
5383 h
->type
= STT_OBJECT
;
5384 h
->verinfo
.vertree
= NULL
;
5386 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5389 /* Create a duplicate of the base version with the same
5390 aux block, but different flags. */
5393 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5395 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5396 + sizeof (Elf_External_Verdaux
));
5399 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5400 (Elf_External_Verdef
*) p
);
5401 p
+= sizeof (Elf_External_Verdef
);
5403 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5404 (Elf_External_Verdaux
*) p
);
5405 p
+= sizeof (Elf_External_Verdaux
);
5407 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5410 struct bfd_elf_version_deps
*n
;
5413 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5416 /* Add a symbol representing this version. */
5418 if (! (_bfd_generic_link_add_one_symbol
5419 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5421 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5423 h
= (struct elf_link_hash_entry
*) bh
;
5426 h
->type
= STT_OBJECT
;
5427 h
->verinfo
.vertree
= t
;
5429 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5432 def
.vd_version
= VER_DEF_CURRENT
;
5434 if (t
->globals
.list
== NULL
5435 && t
->locals
.list
== NULL
5437 def
.vd_flags
|= VER_FLG_WEAK
;
5438 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5439 def
.vd_cnt
= cdeps
+ 1;
5440 def
.vd_hash
= bfd_elf_hash (t
->name
);
5441 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5443 if (t
->next
!= NULL
)
5444 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5445 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5447 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5448 (Elf_External_Verdef
*) p
);
5449 p
+= sizeof (Elf_External_Verdef
);
5451 defaux
.vda_name
= h
->dynstr_index
;
5452 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5454 defaux
.vda_next
= 0;
5455 if (t
->deps
!= NULL
)
5456 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5457 t
->name_indx
= defaux
.vda_name
;
5459 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5460 (Elf_External_Verdaux
*) p
);
5461 p
+= sizeof (Elf_External_Verdaux
);
5463 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5465 if (n
->version_needed
== NULL
)
5467 /* This can happen if there was an error in the
5469 defaux
.vda_name
= 0;
5473 defaux
.vda_name
= n
->version_needed
->name_indx
;
5474 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5477 if (n
->next
== NULL
)
5478 defaux
.vda_next
= 0;
5480 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5482 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5483 (Elf_External_Verdaux
*) p
);
5484 p
+= sizeof (Elf_External_Verdaux
);
5488 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5489 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5492 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5495 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5497 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5500 else if (info
->flags
& DF_BIND_NOW
)
5502 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5508 if (info
->executable
)
5509 info
->flags_1
&= ~ (DF_1_INITFIRST
5512 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5516 /* Work out the size of the version reference section. */
5518 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5519 BFD_ASSERT (s
!= NULL
);
5521 struct elf_find_verdep_info sinfo
;
5523 sinfo
.output_bfd
= output_bfd
;
5525 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5526 if (sinfo
.vers
== 0)
5528 sinfo
.failed
= FALSE
;
5530 elf_link_hash_traverse (elf_hash_table (info
),
5531 _bfd_elf_link_find_version_dependencies
,
5534 if (elf_tdata (output_bfd
)->verref
== NULL
)
5535 _bfd_strip_section_from_output (info
, s
);
5538 Elf_Internal_Verneed
*t
;
5543 /* Build the version definition section. */
5546 for (t
= elf_tdata (output_bfd
)->verref
;
5550 Elf_Internal_Vernaux
*a
;
5552 size
+= sizeof (Elf_External_Verneed
);
5554 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5555 size
+= sizeof (Elf_External_Vernaux
);
5559 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5560 if (s
->contents
== NULL
)
5564 for (t
= elf_tdata (output_bfd
)->verref
;
5569 Elf_Internal_Vernaux
*a
;
5573 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5576 t
->vn_version
= VER_NEED_CURRENT
;
5578 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5579 elf_dt_name (t
->vn_bfd
) != NULL
5580 ? elf_dt_name (t
->vn_bfd
)
5581 : basename (t
->vn_bfd
->filename
),
5583 if (indx
== (bfd_size_type
) -1)
5586 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5587 if (t
->vn_nextref
== NULL
)
5590 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5591 + caux
* sizeof (Elf_External_Vernaux
));
5593 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5594 (Elf_External_Verneed
*) p
);
5595 p
+= sizeof (Elf_External_Verneed
);
5597 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5599 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5600 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5601 a
->vna_nodename
, FALSE
);
5602 if (indx
== (bfd_size_type
) -1)
5605 if (a
->vna_nextptr
== NULL
)
5608 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5610 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5611 (Elf_External_Vernaux
*) p
);
5612 p
+= sizeof (Elf_External_Vernaux
);
5616 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5617 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5620 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5624 /* Assign dynsym indicies. In a shared library we generate a
5625 section symbol for each output section, which come first.
5626 Next come all of the back-end allocated local dynamic syms,
5627 followed by the rest of the global symbols. */
5629 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5631 /* Work out the size of the symbol version section. */
5632 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5633 BFD_ASSERT (s
!= NULL
);
5634 if (dynsymcount
== 0
5635 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
5636 && !info
->create_default_symver
))
5638 _bfd_strip_section_from_output (info
, s
);
5639 /* The DYNSYMCOUNT might have changed if we were going to
5640 output a dynamic symbol table entry for S. */
5641 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5645 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5646 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5647 if (s
->contents
== NULL
)
5650 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5654 /* Set the size of the .dynsym and .hash sections. We counted
5655 the number of dynamic symbols in elf_link_add_object_symbols.
5656 We will build the contents of .dynsym and .hash when we build
5657 the final symbol table, because until then we do not know the
5658 correct value to give the symbols. We built the .dynstr
5659 section as we went along in elf_link_add_object_symbols. */
5660 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5661 BFD_ASSERT (s
!= NULL
);
5662 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5663 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5664 if (s
->contents
== NULL
&& s
->size
!= 0)
5667 if (dynsymcount
!= 0)
5669 Elf_Internal_Sym isym
;
5671 /* The first entry in .dynsym is a dummy symbol. */
5678 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5681 /* Compute the size of the hashing table. As a side effect this
5682 computes the hash values for all the names we export. */
5683 bucketcount
= compute_bucket_count (info
);
5685 s
= bfd_get_section_by_name (dynobj
, ".hash");
5686 BFD_ASSERT (s
!= NULL
);
5687 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5688 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5689 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5690 if (s
->contents
== NULL
)
5693 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5694 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5695 s
->contents
+ hash_entry_size
);
5697 elf_hash_table (info
)->bucketcount
= bucketcount
;
5699 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5700 BFD_ASSERT (s
!= NULL
);
5702 elf_finalize_dynstr (output_bfd
, info
);
5704 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5706 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5707 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5714 /* Final phase of ELF linker. */
5716 /* A structure we use to avoid passing large numbers of arguments. */
5718 struct elf_final_link_info
5720 /* General link information. */
5721 struct bfd_link_info
*info
;
5724 /* Symbol string table. */
5725 struct bfd_strtab_hash
*symstrtab
;
5726 /* .dynsym section. */
5727 asection
*dynsym_sec
;
5728 /* .hash section. */
5730 /* symbol version section (.gnu.version). */
5731 asection
*symver_sec
;
5732 /* Buffer large enough to hold contents of any section. */
5734 /* Buffer large enough to hold external relocs of any section. */
5735 void *external_relocs
;
5736 /* Buffer large enough to hold internal relocs of any section. */
5737 Elf_Internal_Rela
*internal_relocs
;
5738 /* Buffer large enough to hold external local symbols of any input
5740 bfd_byte
*external_syms
;
5741 /* And a buffer for symbol section indices. */
5742 Elf_External_Sym_Shndx
*locsym_shndx
;
5743 /* Buffer large enough to hold internal local symbols of any input
5745 Elf_Internal_Sym
*internal_syms
;
5746 /* Array large enough to hold a symbol index for each local symbol
5747 of any input BFD. */
5749 /* Array large enough to hold a section pointer for each local
5750 symbol of any input BFD. */
5751 asection
**sections
;
5752 /* Buffer to hold swapped out symbols. */
5754 /* And one for symbol section indices. */
5755 Elf_External_Sym_Shndx
*symshndxbuf
;
5756 /* Number of swapped out symbols in buffer. */
5757 size_t symbuf_count
;
5758 /* Number of symbols which fit in symbuf. */
5760 /* And same for symshndxbuf. */
5761 size_t shndxbuf_size
;
5764 /* This struct is used to pass information to elf_link_output_extsym. */
5766 struct elf_outext_info
5769 bfd_boolean localsyms
;
5770 struct elf_final_link_info
*finfo
;
5773 /* When performing a relocatable link, the input relocations are
5774 preserved. But, if they reference global symbols, the indices
5775 referenced must be updated. Update all the relocations in
5776 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5779 elf_link_adjust_relocs (bfd
*abfd
,
5780 Elf_Internal_Shdr
*rel_hdr
,
5782 struct elf_link_hash_entry
**rel_hash
)
5785 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5787 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5788 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5789 bfd_vma r_type_mask
;
5792 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5794 swap_in
= bed
->s
->swap_reloc_in
;
5795 swap_out
= bed
->s
->swap_reloc_out
;
5797 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5799 swap_in
= bed
->s
->swap_reloca_in
;
5800 swap_out
= bed
->s
->swap_reloca_out
;
5805 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5808 if (bed
->s
->arch_size
== 32)
5815 r_type_mask
= 0xffffffff;
5819 erela
= rel_hdr
->contents
;
5820 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5822 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5825 if (*rel_hash
== NULL
)
5828 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5830 (*swap_in
) (abfd
, erela
, irela
);
5831 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5832 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5833 | (irela
[j
].r_info
& r_type_mask
));
5834 (*swap_out
) (abfd
, irela
, erela
);
5838 struct elf_link_sort_rela
5844 enum elf_reloc_type_class type
;
5845 /* We use this as an array of size int_rels_per_ext_rel. */
5846 Elf_Internal_Rela rela
[1];
5850 elf_link_sort_cmp1 (const void *A
, const void *B
)
5852 const struct elf_link_sort_rela
*a
= A
;
5853 const struct elf_link_sort_rela
*b
= B
;
5854 int relativea
, relativeb
;
5856 relativea
= a
->type
== reloc_class_relative
;
5857 relativeb
= b
->type
== reloc_class_relative
;
5859 if (relativea
< relativeb
)
5861 if (relativea
> relativeb
)
5863 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5865 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5867 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5869 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5875 elf_link_sort_cmp2 (const void *A
, const void *B
)
5877 const struct elf_link_sort_rela
*a
= A
;
5878 const struct elf_link_sort_rela
*b
= B
;
5881 if (a
->u
.offset
< b
->u
.offset
)
5883 if (a
->u
.offset
> b
->u
.offset
)
5885 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5886 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5891 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5893 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5899 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5902 bfd_size_type count
, size
;
5903 size_t i
, ret
, sort_elt
, ext_size
;
5904 bfd_byte
*sort
, *s_non_relative
, *p
;
5905 struct elf_link_sort_rela
*sq
;
5906 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5907 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5908 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5909 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5910 struct bfd_link_order
*lo
;
5913 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5914 if (reldyn
== NULL
|| reldyn
->size
== 0)
5916 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5917 if (reldyn
== NULL
|| reldyn
->size
== 0)
5919 ext_size
= bed
->s
->sizeof_rel
;
5920 swap_in
= bed
->s
->swap_reloc_in
;
5921 swap_out
= bed
->s
->swap_reloc_out
;
5925 ext_size
= bed
->s
->sizeof_rela
;
5926 swap_in
= bed
->s
->swap_reloca_in
;
5927 swap_out
= bed
->s
->swap_reloca_out
;
5929 count
= reldyn
->size
/ ext_size
;
5932 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5933 if (lo
->type
== bfd_indirect_link_order
)
5935 asection
*o
= lo
->u
.indirect
.section
;
5939 if (size
!= reldyn
->size
)
5942 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5943 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5944 sort
= bfd_zmalloc (sort_elt
* count
);
5947 (*info
->callbacks
->warning
)
5948 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5952 if (bed
->s
->arch_size
== 32)
5953 r_sym_mask
= ~(bfd_vma
) 0xff;
5955 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5957 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5958 if (lo
->type
== bfd_indirect_link_order
)
5960 bfd_byte
*erel
, *erelend
;
5961 asection
*o
= lo
->u
.indirect
.section
;
5963 if (o
->contents
== NULL
&& o
->size
!= 0)
5965 /* This is a reloc section that is being handled as a normal
5966 section. See bfd_section_from_shdr. We can't combine
5967 relocs in this case. */
5972 erelend
= o
->contents
+ o
->size
;
5973 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5974 while (erel
< erelend
)
5976 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5977 (*swap_in
) (abfd
, erel
, s
->rela
);
5978 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5979 s
->u
.sym_mask
= r_sym_mask
;
5985 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5987 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5989 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5990 if (s
->type
!= reloc_class_relative
)
5996 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5997 for (; i
< count
; i
++, p
+= sort_elt
)
5999 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6000 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6002 sp
->u
.offset
= sq
->rela
->r_offset
;
6005 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6007 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
6008 if (lo
->type
== bfd_indirect_link_order
)
6010 bfd_byte
*erel
, *erelend
;
6011 asection
*o
= lo
->u
.indirect
.section
;
6014 erelend
= o
->contents
+ o
->size
;
6015 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6016 while (erel
< erelend
)
6018 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6019 (*swap_out
) (abfd
, s
->rela
, erel
);
6030 /* Flush the output symbols to the file. */
6033 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6034 const struct elf_backend_data
*bed
)
6036 if (finfo
->symbuf_count
> 0)
6038 Elf_Internal_Shdr
*hdr
;
6042 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6043 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6044 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6045 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6046 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6049 hdr
->sh_size
+= amt
;
6050 finfo
->symbuf_count
= 0;
6056 /* Add a symbol to the output symbol table. */
6059 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6061 Elf_Internal_Sym
*elfsym
,
6062 asection
*input_sec
,
6063 struct elf_link_hash_entry
*h
)
6066 Elf_External_Sym_Shndx
*destshndx
;
6067 bfd_boolean (*output_symbol_hook
)
6068 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6069 struct elf_link_hash_entry
*);
6070 const struct elf_backend_data
*bed
;
6072 bed
= get_elf_backend_data (finfo
->output_bfd
);
6073 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6074 if (output_symbol_hook
!= NULL
)
6076 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6080 if (name
== NULL
|| *name
== '\0')
6081 elfsym
->st_name
= 0;
6082 else if (input_sec
->flags
& SEC_EXCLUDE
)
6083 elfsym
->st_name
= 0;
6086 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6088 if (elfsym
->st_name
== (unsigned long) -1)
6092 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6094 if (! elf_link_flush_output_syms (finfo
, bed
))
6098 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6099 destshndx
= finfo
->symshndxbuf
;
6100 if (destshndx
!= NULL
)
6102 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6106 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6107 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6108 if (destshndx
== NULL
)
6110 memset ((char *) destshndx
+ amt
, 0, amt
);
6111 finfo
->shndxbuf_size
*= 2;
6113 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6116 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6117 finfo
->symbuf_count
+= 1;
6118 bfd_get_symcount (finfo
->output_bfd
) += 1;
6123 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6124 allowing an unsatisfied unversioned symbol in the DSO to match a
6125 versioned symbol that would normally require an explicit version.
6126 We also handle the case that a DSO references a hidden symbol
6127 which may be satisfied by a versioned symbol in another DSO. */
6130 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6131 const struct elf_backend_data
*bed
,
6132 struct elf_link_hash_entry
*h
)
6135 struct elf_link_loaded_list
*loaded
;
6137 if (!is_elf_hash_table (info
->hash
))
6140 switch (h
->root
.type
)
6146 case bfd_link_hash_undefined
:
6147 case bfd_link_hash_undefweak
:
6148 abfd
= h
->root
.u
.undef
.abfd
;
6149 if ((abfd
->flags
& DYNAMIC
) == 0
6150 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6154 case bfd_link_hash_defined
:
6155 case bfd_link_hash_defweak
:
6156 abfd
= h
->root
.u
.def
.section
->owner
;
6159 case bfd_link_hash_common
:
6160 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6163 BFD_ASSERT (abfd
!= NULL
);
6165 for (loaded
= elf_hash_table (info
)->loaded
;
6167 loaded
= loaded
->next
)
6170 Elf_Internal_Shdr
*hdr
;
6171 bfd_size_type symcount
;
6172 bfd_size_type extsymcount
;
6173 bfd_size_type extsymoff
;
6174 Elf_Internal_Shdr
*versymhdr
;
6175 Elf_Internal_Sym
*isym
;
6176 Elf_Internal_Sym
*isymend
;
6177 Elf_Internal_Sym
*isymbuf
;
6178 Elf_External_Versym
*ever
;
6179 Elf_External_Versym
*extversym
;
6181 input
= loaded
->abfd
;
6183 /* We check each DSO for a possible hidden versioned definition. */
6185 || (input
->flags
& DYNAMIC
) == 0
6186 || elf_dynversym (input
) == 0)
6189 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6191 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6192 if (elf_bad_symtab (input
))
6194 extsymcount
= symcount
;
6199 extsymcount
= symcount
- hdr
->sh_info
;
6200 extsymoff
= hdr
->sh_info
;
6203 if (extsymcount
== 0)
6206 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6208 if (isymbuf
== NULL
)
6211 /* Read in any version definitions. */
6212 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6213 extversym
= bfd_malloc (versymhdr
->sh_size
);
6214 if (extversym
== NULL
)
6217 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6218 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6219 != versymhdr
->sh_size
))
6227 ever
= extversym
+ extsymoff
;
6228 isymend
= isymbuf
+ extsymcount
;
6229 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6232 Elf_Internal_Versym iver
;
6233 unsigned short version_index
;
6235 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6236 || isym
->st_shndx
== SHN_UNDEF
)
6239 name
= bfd_elf_string_from_elf_section (input
,
6242 if (strcmp (name
, h
->root
.root
.string
) != 0)
6245 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6247 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6249 /* If we have a non-hidden versioned sym, then it should
6250 have provided a definition for the undefined sym. */
6254 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6255 if (version_index
== 1 || version_index
== 2)
6257 /* This is the base or first version. We can use it. */
6271 /* Add an external symbol to the symbol table. This is called from
6272 the hash table traversal routine. When generating a shared object,
6273 we go through the symbol table twice. The first time we output
6274 anything that might have been forced to local scope in a version
6275 script. The second time we output the symbols that are still
6279 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6281 struct elf_outext_info
*eoinfo
= data
;
6282 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6284 Elf_Internal_Sym sym
;
6285 asection
*input_sec
;
6286 const struct elf_backend_data
*bed
;
6288 if (h
->root
.type
== bfd_link_hash_warning
)
6290 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6291 if (h
->root
.type
== bfd_link_hash_new
)
6295 /* Decide whether to output this symbol in this pass. */
6296 if (eoinfo
->localsyms
)
6298 if (!h
->forced_local
)
6303 if (h
->forced_local
)
6307 bed
= get_elf_backend_data (finfo
->output_bfd
);
6309 /* If we have an undefined symbol reference here then it must have
6310 come from a shared library that is being linked in. (Undefined
6311 references in regular files have already been handled). If we
6312 are reporting errors for this situation then do so now. */
6313 if (h
->root
.type
== bfd_link_hash_undefined
6316 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6317 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6319 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6320 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6321 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6323 eoinfo
->failed
= TRUE
;
6328 /* We should also warn if a forced local symbol is referenced from
6329 shared libraries. */
6330 if (! finfo
->info
->relocatable
6331 && (! finfo
->info
->shared
)
6336 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6338 (*_bfd_error_handler
)
6339 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6340 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6341 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6343 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6344 ? "hidden" : "local",
6345 h
->root
.root
.string
);
6346 eoinfo
->failed
= TRUE
;
6350 /* We don't want to output symbols that have never been mentioned by
6351 a regular file, or that we have been told to strip. However, if
6352 h->indx is set to -2, the symbol is used by a reloc and we must
6356 else if ((h
->def_dynamic
6358 || h
->root
.type
== bfd_link_hash_new
)
6362 else if (finfo
->info
->strip
== strip_all
)
6364 else if (finfo
->info
->strip
== strip_some
6365 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6366 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6368 else if (finfo
->info
->strip_discarded
6369 && (h
->root
.type
== bfd_link_hash_defined
6370 || h
->root
.type
== bfd_link_hash_defweak
)
6371 && elf_discarded_section (h
->root
.u
.def
.section
))
6376 /* If we're stripping it, and it's not a dynamic symbol, there's
6377 nothing else to do unless it is a forced local symbol. */
6380 && !h
->forced_local
)
6384 sym
.st_size
= h
->size
;
6385 sym
.st_other
= h
->other
;
6386 if (h
->forced_local
)
6387 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6388 else if (h
->root
.type
== bfd_link_hash_undefweak
6389 || h
->root
.type
== bfd_link_hash_defweak
)
6390 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6392 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6394 switch (h
->root
.type
)
6397 case bfd_link_hash_new
:
6398 case bfd_link_hash_warning
:
6402 case bfd_link_hash_undefined
:
6403 case bfd_link_hash_undefweak
:
6404 input_sec
= bfd_und_section_ptr
;
6405 sym
.st_shndx
= SHN_UNDEF
;
6408 case bfd_link_hash_defined
:
6409 case bfd_link_hash_defweak
:
6411 input_sec
= h
->root
.u
.def
.section
;
6412 if (input_sec
->output_section
!= NULL
)
6415 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6416 input_sec
->output_section
);
6417 if (sym
.st_shndx
== SHN_BAD
)
6419 (*_bfd_error_handler
)
6420 (_("%B: could not find output section %A for input section %A"),
6421 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6422 eoinfo
->failed
= TRUE
;
6426 /* ELF symbols in relocatable files are section relative,
6427 but in nonrelocatable files they are virtual
6429 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6430 if (! finfo
->info
->relocatable
)
6432 sym
.st_value
+= input_sec
->output_section
->vma
;
6433 if (h
->type
== STT_TLS
)
6435 /* STT_TLS symbols are relative to PT_TLS segment
6437 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6438 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6444 BFD_ASSERT (input_sec
->owner
== NULL
6445 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6446 sym
.st_shndx
= SHN_UNDEF
;
6447 input_sec
= bfd_und_section_ptr
;
6452 case bfd_link_hash_common
:
6453 input_sec
= h
->root
.u
.c
.p
->section
;
6454 sym
.st_shndx
= SHN_COMMON
;
6455 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6458 case bfd_link_hash_indirect
:
6459 /* These symbols are created by symbol versioning. They point
6460 to the decorated version of the name. For example, if the
6461 symbol foo@@GNU_1.2 is the default, which should be used when
6462 foo is used with no version, then we add an indirect symbol
6463 foo which points to foo@@GNU_1.2. We ignore these symbols,
6464 since the indirected symbol is already in the hash table. */
6468 /* Give the processor backend a chance to tweak the symbol value,
6469 and also to finish up anything that needs to be done for this
6470 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6471 forced local syms when non-shared is due to a historical quirk. */
6472 if ((h
->dynindx
!= -1
6474 && ((finfo
->info
->shared
6475 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6476 || h
->root
.type
!= bfd_link_hash_undefweak
))
6477 || !h
->forced_local
)
6478 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6480 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6481 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6483 eoinfo
->failed
= TRUE
;
6488 /* If we are marking the symbol as undefined, and there are no
6489 non-weak references to this symbol from a regular object, then
6490 mark the symbol as weak undefined; if there are non-weak
6491 references, mark the symbol as strong. We can't do this earlier,
6492 because it might not be marked as undefined until the
6493 finish_dynamic_symbol routine gets through with it. */
6494 if (sym
.st_shndx
== SHN_UNDEF
6496 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6497 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6501 if (h
->ref_regular_nonweak
)
6502 bindtype
= STB_GLOBAL
;
6504 bindtype
= STB_WEAK
;
6505 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6508 /* If a non-weak symbol with non-default visibility is not defined
6509 locally, it is a fatal error. */
6510 if (! finfo
->info
->relocatable
6511 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6512 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6513 && h
->root
.type
== bfd_link_hash_undefined
6516 (*_bfd_error_handler
)
6517 (_("%B: %s symbol `%s' isn't defined"),
6519 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6521 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6522 ? "internal" : "hidden",
6523 h
->root
.root
.string
);
6524 eoinfo
->failed
= TRUE
;
6528 /* If this symbol should be put in the .dynsym section, then put it
6529 there now. We already know the symbol index. We also fill in
6530 the entry in the .hash section. */
6531 if (h
->dynindx
!= -1
6532 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6536 size_t hash_entry_size
;
6537 bfd_byte
*bucketpos
;
6541 sym
.st_name
= h
->dynstr_index
;
6542 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6543 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6545 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6546 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6548 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6549 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6550 + (bucket
+ 2) * hash_entry_size
);
6551 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6552 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6553 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6554 ((bfd_byte
*) finfo
->hash_sec
->contents
6555 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6557 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6559 Elf_Internal_Versym iversym
;
6560 Elf_External_Versym
*eversym
;
6562 if (!h
->def_regular
)
6564 if (h
->verinfo
.verdef
== NULL
)
6565 iversym
.vs_vers
= 0;
6567 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6571 if (h
->verinfo
.vertree
== NULL
)
6572 iversym
.vs_vers
= 1;
6574 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6575 if (finfo
->info
->create_default_symver
)
6580 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6582 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6583 eversym
+= h
->dynindx
;
6584 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6588 /* If we're stripping it, then it was just a dynamic symbol, and
6589 there's nothing else to do. */
6590 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6593 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6595 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6597 eoinfo
->failed
= TRUE
;
6604 /* Return TRUE if special handling is done for relocs in SEC against
6605 symbols defined in discarded sections. */
6608 elf_section_ignore_discarded_relocs (asection
*sec
)
6610 const struct elf_backend_data
*bed
;
6612 switch (sec
->sec_info_type
)
6614 case ELF_INFO_TYPE_STABS
:
6615 case ELF_INFO_TYPE_EH_FRAME
:
6621 bed
= get_elf_backend_data (sec
->owner
);
6622 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6623 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6629 enum action_discarded
6635 /* Return a mask saying how ld should treat relocations in SEC against
6636 symbols defined in discarded sections. If this function returns
6637 COMPLAIN set, ld will issue a warning message. If this function
6638 returns PRETEND set, and the discarded section was link-once and the
6639 same size as the kept link-once section, ld will pretend that the
6640 symbol was actually defined in the kept section. Otherwise ld will
6641 zero the reloc (at least that is the intent, but some cooperation by
6642 the target dependent code is needed, particularly for REL targets). */
6645 elf_action_discarded (asection
*sec
)
6647 if (sec
->flags
& SEC_DEBUGGING
)
6650 if (strcmp (".eh_frame", sec
->name
) == 0)
6653 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6656 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6659 if (strcmp (".fixup", sec
->name
) == 0)
6662 return COMPLAIN
| PRETEND
;
6665 /* Find a match between a section and a member of a section group. */
6668 match_group_member (asection
*sec
, asection
*group
)
6670 asection
*first
= elf_next_in_group (group
);
6671 asection
*s
= first
;
6675 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6685 /* Link an input file into the linker output file. This function
6686 handles all the sections and relocations of the input file at once.
6687 This is so that we only have to read the local symbols once, and
6688 don't have to keep them in memory. */
6691 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6693 bfd_boolean (*relocate_section
)
6694 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6695 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6697 Elf_Internal_Shdr
*symtab_hdr
;
6700 Elf_Internal_Sym
*isymbuf
;
6701 Elf_Internal_Sym
*isym
;
6702 Elf_Internal_Sym
*isymend
;
6704 asection
**ppsection
;
6706 const struct elf_backend_data
*bed
;
6707 bfd_boolean emit_relocs
;
6708 struct elf_link_hash_entry
**sym_hashes
;
6710 output_bfd
= finfo
->output_bfd
;
6711 bed
= get_elf_backend_data (output_bfd
);
6712 relocate_section
= bed
->elf_backend_relocate_section
;
6714 /* If this is a dynamic object, we don't want to do anything here:
6715 we don't want the local symbols, and we don't want the section
6717 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6720 emit_relocs
= (finfo
->info
->relocatable
6721 || finfo
->info
->emitrelocations
6722 || bed
->elf_backend_emit_relocs
);
6724 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6725 if (elf_bad_symtab (input_bfd
))
6727 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6732 locsymcount
= symtab_hdr
->sh_info
;
6733 extsymoff
= symtab_hdr
->sh_info
;
6736 /* Read the local symbols. */
6737 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6738 if (isymbuf
== NULL
&& locsymcount
!= 0)
6740 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6741 finfo
->internal_syms
,
6742 finfo
->external_syms
,
6743 finfo
->locsym_shndx
);
6744 if (isymbuf
== NULL
)
6748 /* Find local symbol sections and adjust values of symbols in
6749 SEC_MERGE sections. Write out those local symbols we know are
6750 going into the output file. */
6751 isymend
= isymbuf
+ locsymcount
;
6752 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6754 isym
++, pindex
++, ppsection
++)
6758 Elf_Internal_Sym osym
;
6762 if (elf_bad_symtab (input_bfd
))
6764 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6771 if (isym
->st_shndx
== SHN_UNDEF
)
6772 isec
= bfd_und_section_ptr
;
6773 else if (isym
->st_shndx
< SHN_LORESERVE
6774 || isym
->st_shndx
> SHN_HIRESERVE
)
6776 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6778 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6779 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6781 _bfd_merged_section_offset (output_bfd
, &isec
,
6782 elf_section_data (isec
)->sec_info
,
6785 else if (isym
->st_shndx
== SHN_ABS
)
6786 isec
= bfd_abs_section_ptr
;
6787 else if (isym
->st_shndx
== SHN_COMMON
)
6788 isec
= bfd_com_section_ptr
;
6797 /* Don't output the first, undefined, symbol. */
6798 if (ppsection
== finfo
->sections
)
6801 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6803 /* We never output section symbols. Instead, we use the
6804 section symbol of the corresponding section in the output
6809 /* If we are stripping all symbols, we don't want to output this
6811 if (finfo
->info
->strip
== strip_all
)
6814 /* If we are discarding all local symbols, we don't want to
6815 output this one. If we are generating a relocatable output
6816 file, then some of the local symbols may be required by
6817 relocs; we output them below as we discover that they are
6819 if (finfo
->info
->discard
== discard_all
)
6822 /* If this symbol is defined in a section which we are
6823 discarding, we don't need to keep it, but note that
6824 linker_mark is only reliable for sections that have contents.
6825 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6826 as well as linker_mark. */
6827 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6829 || (! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6830 || (! finfo
->info
->relocatable
6831 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6834 /* Get the name of the symbol. */
6835 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6840 /* See if we are discarding symbols with this name. */
6841 if ((finfo
->info
->strip
== strip_some
6842 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6844 || (((finfo
->info
->discard
== discard_sec_merge
6845 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6846 || finfo
->info
->discard
== discard_l
)
6847 && bfd_is_local_label_name (input_bfd
, name
)))
6850 /* If we get here, we are going to output this symbol. */
6854 /* Adjust the section index for the output file. */
6855 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6856 isec
->output_section
);
6857 if (osym
.st_shndx
== SHN_BAD
)
6860 *pindex
= bfd_get_symcount (output_bfd
);
6862 /* ELF symbols in relocatable files are section relative, but
6863 in executable files they are virtual addresses. Note that
6864 this code assumes that all ELF sections have an associated
6865 BFD section with a reasonable value for output_offset; below
6866 we assume that they also have a reasonable value for
6867 output_section. Any special sections must be set up to meet
6868 these requirements. */
6869 osym
.st_value
+= isec
->output_offset
;
6870 if (! finfo
->info
->relocatable
)
6872 osym
.st_value
+= isec
->output_section
->vma
;
6873 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6875 /* STT_TLS symbols are relative to PT_TLS segment base. */
6876 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6877 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6881 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6885 /* Relocate the contents of each section. */
6886 sym_hashes
= elf_sym_hashes (input_bfd
);
6887 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6891 if (! o
->linker_mark
)
6893 /* This section was omitted from the link. */
6897 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6898 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6901 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6903 /* Section was created by _bfd_elf_link_create_dynamic_sections
6908 /* Get the contents of the section. They have been cached by a
6909 relaxation routine. Note that o is a section in an input
6910 file, so the contents field will not have been set by any of
6911 the routines which work on output files. */
6912 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6913 contents
= elf_section_data (o
)->this_hdr
.contents
;
6916 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6918 contents
= finfo
->contents
;
6919 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6923 if ((o
->flags
& SEC_RELOC
) != 0)
6925 Elf_Internal_Rela
*internal_relocs
;
6926 bfd_vma r_type_mask
;
6929 /* Get the swapped relocs. */
6931 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6932 finfo
->internal_relocs
, FALSE
);
6933 if (internal_relocs
== NULL
6934 && o
->reloc_count
> 0)
6937 if (bed
->s
->arch_size
== 32)
6944 r_type_mask
= 0xffffffff;
6948 /* Run through the relocs looking for any against symbols
6949 from discarded sections and section symbols from
6950 removed link-once sections. Complain about relocs
6951 against discarded sections. Zero relocs against removed
6952 link-once sections. Preserve debug information as much
6954 if (!elf_section_ignore_discarded_relocs (o
))
6956 Elf_Internal_Rela
*rel
, *relend
;
6957 unsigned int action
= elf_action_discarded (o
);
6959 rel
= internal_relocs
;
6960 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6961 for ( ; rel
< relend
; rel
++)
6963 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6964 asection
**ps
, *sec
;
6965 struct elf_link_hash_entry
*h
= NULL
;
6966 const char *sym_name
;
6968 if (r_symndx
== STN_UNDEF
)
6971 if (r_symndx
>= locsymcount
6972 || (elf_bad_symtab (input_bfd
)
6973 && finfo
->sections
[r_symndx
] == NULL
))
6975 h
= sym_hashes
[r_symndx
- extsymoff
];
6976 while (h
->root
.type
== bfd_link_hash_indirect
6977 || h
->root
.type
== bfd_link_hash_warning
)
6978 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6980 if (h
->root
.type
!= bfd_link_hash_defined
6981 && h
->root
.type
!= bfd_link_hash_defweak
)
6984 ps
= &h
->root
.u
.def
.section
;
6985 sym_name
= h
->root
.root
.string
;
6989 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6990 ps
= &finfo
->sections
[r_symndx
];
6991 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
);
6994 /* Complain if the definition comes from a
6995 discarded section. */
6996 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7000 BFD_ASSERT (r_symndx
!= 0);
7001 if (action
& COMPLAIN
)
7003 (*_bfd_error_handler
)
7004 (_("`%s' referenced in section `%A' of %B: "
7005 "defined in discarded section `%A' of %B\n"),
7006 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
7009 /* Try to do the best we can to support buggy old
7010 versions of gcc. If we've warned, or this is
7011 debugging info, pretend that the symbol is
7012 really defined in the kept linkonce section.
7013 FIXME: This is quite broken. Modifying the
7014 symbol here means we will be changing all later
7015 uses of the symbol, not just in this section.
7016 The only thing that makes this half reasonable
7017 is that we warn in non-debug sections, and
7018 debug sections tend to come after other
7020 kept
= sec
->kept_section
;
7021 if (kept
!= NULL
&& (action
& PRETEND
))
7023 if (elf_sec_group (sec
) != NULL
)
7024 kept
= match_group_member (sec
, kept
);
7026 && sec
->size
== kept
->size
)
7033 /* Remove the symbol reference from the reloc, but
7034 don't kill the reloc completely. This is so that
7035 a zero value will be written into the section,
7036 which may have non-zero contents put there by the
7037 assembler. Zero in things like an eh_frame fde
7038 pc_begin allows stack unwinders to recognize the
7040 rel
->r_info
&= r_type_mask
;
7046 /* Relocate the section by invoking a back end routine.
7048 The back end routine is responsible for adjusting the
7049 section contents as necessary, and (if using Rela relocs
7050 and generating a relocatable output file) adjusting the
7051 reloc addend as necessary.
7053 The back end routine does not have to worry about setting
7054 the reloc address or the reloc symbol index.
7056 The back end routine is given a pointer to the swapped in
7057 internal symbols, and can access the hash table entries
7058 for the external symbols via elf_sym_hashes (input_bfd).
7060 When generating relocatable output, the back end routine
7061 must handle STB_LOCAL/STT_SECTION symbols specially. The
7062 output symbol is going to be a section symbol
7063 corresponding to the output section, which will require
7064 the addend to be adjusted. */
7066 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7067 input_bfd
, o
, contents
,
7075 Elf_Internal_Rela
*irela
;
7076 Elf_Internal_Rela
*irelaend
;
7077 bfd_vma last_offset
;
7078 struct elf_link_hash_entry
**rel_hash
;
7079 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7080 unsigned int next_erel
;
7081 bfd_boolean (*reloc_emitter
)
7082 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
7083 bfd_boolean rela_normal
;
7085 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7086 rela_normal
= (bed
->rela_normal
7087 && (input_rel_hdr
->sh_entsize
7088 == bed
->s
->sizeof_rela
));
7090 /* Adjust the reloc addresses and symbol indices. */
7092 irela
= internal_relocs
;
7093 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7094 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7095 + elf_section_data (o
->output_section
)->rel_count
7096 + elf_section_data (o
->output_section
)->rel_count2
);
7097 last_offset
= o
->output_offset
;
7098 if (!finfo
->info
->relocatable
)
7099 last_offset
+= o
->output_section
->vma
;
7100 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7102 unsigned long r_symndx
;
7104 Elf_Internal_Sym sym
;
7106 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7112 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7115 if (irela
->r_offset
>= (bfd_vma
) -2)
7117 /* This is a reloc for a deleted entry or somesuch.
7118 Turn it into an R_*_NONE reloc, at the same
7119 offset as the last reloc. elf_eh_frame.c and
7120 elf_bfd_discard_info rely on reloc offsets
7122 irela
->r_offset
= last_offset
;
7124 irela
->r_addend
= 0;
7128 irela
->r_offset
+= o
->output_offset
;
7130 /* Relocs in an executable have to be virtual addresses. */
7131 if (!finfo
->info
->relocatable
)
7132 irela
->r_offset
+= o
->output_section
->vma
;
7134 last_offset
= irela
->r_offset
;
7136 r_symndx
= irela
->r_info
>> r_sym_shift
;
7137 if (r_symndx
== STN_UNDEF
)
7140 if (r_symndx
>= locsymcount
7141 || (elf_bad_symtab (input_bfd
)
7142 && finfo
->sections
[r_symndx
] == NULL
))
7144 struct elf_link_hash_entry
*rh
;
7147 /* This is a reloc against a global symbol. We
7148 have not yet output all the local symbols, so
7149 we do not know the symbol index of any global
7150 symbol. We set the rel_hash entry for this
7151 reloc to point to the global hash table entry
7152 for this symbol. The symbol index is then
7153 set at the end of bfd_elf_final_link. */
7154 indx
= r_symndx
- extsymoff
;
7155 rh
= elf_sym_hashes (input_bfd
)[indx
];
7156 while (rh
->root
.type
== bfd_link_hash_indirect
7157 || rh
->root
.type
== bfd_link_hash_warning
)
7158 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7160 /* Setting the index to -2 tells
7161 elf_link_output_extsym that this symbol is
7163 BFD_ASSERT (rh
->indx
< 0);
7171 /* This is a reloc against a local symbol. */
7174 sym
= isymbuf
[r_symndx
];
7175 sec
= finfo
->sections
[r_symndx
];
7176 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7178 /* I suppose the backend ought to fill in the
7179 section of any STT_SECTION symbol against a
7180 processor specific section. */
7182 if (bfd_is_abs_section (sec
))
7184 else if (sec
== NULL
|| sec
->owner
== NULL
)
7186 bfd_set_error (bfd_error_bad_value
);
7191 asection
*osec
= sec
->output_section
;
7193 /* If we have discarded a section, the output
7194 section will be the absolute section. In
7195 case of discarded link-once and discarded
7196 SEC_MERGE sections, use the kept section. */
7197 if (bfd_is_abs_section (osec
)
7198 && sec
->kept_section
!= NULL
7199 && sec
->kept_section
->output_section
!= NULL
)
7201 osec
= sec
->kept_section
->output_section
;
7202 irela
->r_addend
-= osec
->vma
;
7205 if (!bfd_is_abs_section (osec
))
7207 r_symndx
= osec
->target_index
;
7208 BFD_ASSERT (r_symndx
!= 0);
7212 /* Adjust the addend according to where the
7213 section winds up in the output section. */
7215 irela
->r_addend
+= sec
->output_offset
;
7219 if (finfo
->indices
[r_symndx
] == -1)
7221 unsigned long shlink
;
7225 if (finfo
->info
->strip
== strip_all
)
7227 /* You can't do ld -r -s. */
7228 bfd_set_error (bfd_error_invalid_operation
);
7232 /* This symbol was skipped earlier, but
7233 since it is needed by a reloc, we
7234 must output it now. */
7235 shlink
= symtab_hdr
->sh_link
;
7236 name
= (bfd_elf_string_from_elf_section
7237 (input_bfd
, shlink
, sym
.st_name
));
7241 osec
= sec
->output_section
;
7243 _bfd_elf_section_from_bfd_section (output_bfd
,
7245 if (sym
.st_shndx
== SHN_BAD
)
7248 sym
.st_value
+= sec
->output_offset
;
7249 if (! finfo
->info
->relocatable
)
7251 sym
.st_value
+= osec
->vma
;
7252 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7254 /* STT_TLS symbols are relative to PT_TLS
7256 BFD_ASSERT (elf_hash_table (finfo
->info
)
7258 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7263 finfo
->indices
[r_symndx
]
7264 = bfd_get_symcount (output_bfd
);
7266 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7271 r_symndx
= finfo
->indices
[r_symndx
];
7274 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7275 | (irela
->r_info
& r_type_mask
));
7278 /* Swap out the relocs. */
7279 if (bed
->elf_backend_emit_relocs
7280 && !(finfo
->info
->relocatable
7281 || finfo
->info
->emitrelocations
))
7282 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7284 reloc_emitter
= _bfd_elf_link_output_relocs
;
7286 if (input_rel_hdr
->sh_size
!= 0
7287 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7291 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7292 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7294 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7295 * bed
->s
->int_rels_per_ext_rel
);
7296 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7303 /* Write out the modified section contents. */
7304 if (bed
->elf_backend_write_section
7305 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7307 /* Section written out. */
7309 else switch (o
->sec_info_type
)
7311 case ELF_INFO_TYPE_STABS
:
7312 if (! (_bfd_write_section_stabs
7314 &elf_hash_table (finfo
->info
)->stab_info
,
7315 o
, &elf_section_data (o
)->sec_info
, contents
)))
7318 case ELF_INFO_TYPE_MERGE
:
7319 if (! _bfd_write_merged_section (output_bfd
, o
,
7320 elf_section_data (o
)->sec_info
))
7323 case ELF_INFO_TYPE_EH_FRAME
:
7325 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7332 if (! (o
->flags
& SEC_EXCLUDE
)
7333 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7335 (file_ptr
) o
->output_offset
,
7346 /* Generate a reloc when linking an ELF file. This is a reloc
7347 requested by the linker, and does come from any input file. This
7348 is used to build constructor and destructor tables when linking
7352 elf_reloc_link_order (bfd
*output_bfd
,
7353 struct bfd_link_info
*info
,
7354 asection
*output_section
,
7355 struct bfd_link_order
*link_order
)
7357 reloc_howto_type
*howto
;
7361 struct elf_link_hash_entry
**rel_hash_ptr
;
7362 Elf_Internal_Shdr
*rel_hdr
;
7363 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7364 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7368 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7371 bfd_set_error (bfd_error_bad_value
);
7375 addend
= link_order
->u
.reloc
.p
->addend
;
7377 /* Figure out the symbol index. */
7378 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7379 + elf_section_data (output_section
)->rel_count
7380 + elf_section_data (output_section
)->rel_count2
);
7381 if (link_order
->type
== bfd_section_reloc_link_order
)
7383 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7384 BFD_ASSERT (indx
!= 0);
7385 *rel_hash_ptr
= NULL
;
7389 struct elf_link_hash_entry
*h
;
7391 /* Treat a reloc against a defined symbol as though it were
7392 actually against the section. */
7393 h
= ((struct elf_link_hash_entry
*)
7394 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7395 link_order
->u
.reloc
.p
->u
.name
,
7396 FALSE
, FALSE
, TRUE
));
7398 && (h
->root
.type
== bfd_link_hash_defined
7399 || h
->root
.type
== bfd_link_hash_defweak
))
7403 section
= h
->root
.u
.def
.section
;
7404 indx
= section
->output_section
->target_index
;
7405 *rel_hash_ptr
= NULL
;
7406 /* It seems that we ought to add the symbol value to the
7407 addend here, but in practice it has already been added
7408 because it was passed to constructor_callback. */
7409 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7413 /* Setting the index to -2 tells elf_link_output_extsym that
7414 this symbol is used by a reloc. */
7421 if (! ((*info
->callbacks
->unattached_reloc
)
7422 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7428 /* If this is an inplace reloc, we must write the addend into the
7430 if (howto
->partial_inplace
&& addend
!= 0)
7433 bfd_reloc_status_type rstat
;
7436 const char *sym_name
;
7438 size
= bfd_get_reloc_size (howto
);
7439 buf
= bfd_zmalloc (size
);
7442 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7449 case bfd_reloc_outofrange
:
7452 case bfd_reloc_overflow
:
7453 if (link_order
->type
== bfd_section_reloc_link_order
)
7454 sym_name
= bfd_section_name (output_bfd
,
7455 link_order
->u
.reloc
.p
->u
.section
);
7457 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7458 if (! ((*info
->callbacks
->reloc_overflow
)
7459 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7460 NULL
, (bfd_vma
) 0)))
7467 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7468 link_order
->offset
, size
);
7474 /* The address of a reloc is relative to the section in a
7475 relocatable file, and is a virtual address in an executable
7477 offset
= link_order
->offset
;
7478 if (! info
->relocatable
)
7479 offset
+= output_section
->vma
;
7481 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7483 irel
[i
].r_offset
= offset
;
7485 irel
[i
].r_addend
= 0;
7487 if (bed
->s
->arch_size
== 32)
7488 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7490 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7492 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7493 erel
= rel_hdr
->contents
;
7494 if (rel_hdr
->sh_type
== SHT_REL
)
7496 erel
+= (elf_section_data (output_section
)->rel_count
7497 * bed
->s
->sizeof_rel
);
7498 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7502 irel
[0].r_addend
= addend
;
7503 erel
+= (elf_section_data (output_section
)->rel_count
7504 * bed
->s
->sizeof_rela
);
7505 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7508 ++elf_section_data (output_section
)->rel_count
;
7514 /* Get the output vma of the section pointed to by the sh_link field. */
7517 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7519 Elf_Internal_Shdr
**elf_shdrp
;
7523 s
= p
->u
.indirect
.section
;
7524 elf_shdrp
= elf_elfsections (s
->owner
);
7525 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7526 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7528 The Intel C compiler generates SHT_IA_64_UNWIND with
7529 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7530 sh_info fields. Hence we could get the situation
7531 where elfsec is 0. */
7534 const struct elf_backend_data
*bed
7535 = get_elf_backend_data (s
->owner
);
7536 if (bed
->link_order_error_handler
)
7537 bed
->link_order_error_handler
7538 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7543 s
= elf_shdrp
[elfsec
]->bfd_section
;
7544 return s
->output_section
->vma
+ s
->output_offset
;
7549 /* Compare two sections based on the locations of the sections they are
7550 linked to. Used by elf_fixup_link_order. */
7553 compare_link_order (const void * a
, const void * b
)
7558 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7559 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7566 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7567 order as their linked sections. Returns false if this could not be done
7568 because an output section includes both ordered and unordered
7569 sections. Ideally we'd do this in the linker proper. */
7572 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7577 struct bfd_link_order
*p
;
7579 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7581 struct bfd_link_order
**sections
;
7587 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7589 if (p
->type
== bfd_indirect_link_order
7590 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7591 == bfd_target_elf_flavour
)
7592 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7594 s
= p
->u
.indirect
.section
;
7595 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7597 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7606 if (!seen_linkorder
)
7609 if (seen_other
&& seen_linkorder
)
7611 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7613 bfd_set_error (bfd_error_bad_value
);
7617 sections
= (struct bfd_link_order
**)
7618 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7621 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7623 sections
[seen_linkorder
++] = p
;
7625 /* Sort the input sections in the order of their linked section. */
7626 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7627 compare_link_order
);
7629 /* Change the offsets of the sections. */
7631 for (n
= 0; n
< seen_linkorder
; n
++)
7633 s
= sections
[n
]->u
.indirect
.section
;
7634 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7635 s
->output_offset
= offset
;
7636 sections
[n
]->offset
= offset
;
7637 offset
+= sections
[n
]->size
;
7644 /* Do the final step of an ELF link. */
7647 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7649 bfd_boolean dynamic
;
7650 bfd_boolean emit_relocs
;
7652 struct elf_final_link_info finfo
;
7653 register asection
*o
;
7654 register struct bfd_link_order
*p
;
7656 bfd_size_type max_contents_size
;
7657 bfd_size_type max_external_reloc_size
;
7658 bfd_size_type max_internal_reloc_count
;
7659 bfd_size_type max_sym_count
;
7660 bfd_size_type max_sym_shndx_count
;
7662 Elf_Internal_Sym elfsym
;
7664 Elf_Internal_Shdr
*symtab_hdr
;
7665 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7666 Elf_Internal_Shdr
*symstrtab_hdr
;
7667 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7668 struct elf_outext_info eoinfo
;
7670 size_t relativecount
= 0;
7671 asection
*reldyn
= 0;
7674 if (! is_elf_hash_table (info
->hash
))
7678 abfd
->flags
|= DYNAMIC
;
7680 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7681 dynobj
= elf_hash_table (info
)->dynobj
;
7683 emit_relocs
= (info
->relocatable
7684 || info
->emitrelocations
7685 || bed
->elf_backend_emit_relocs
);
7688 finfo
.output_bfd
= abfd
;
7689 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7690 if (finfo
.symstrtab
== NULL
)
7695 finfo
.dynsym_sec
= NULL
;
7696 finfo
.hash_sec
= NULL
;
7697 finfo
.symver_sec
= NULL
;
7701 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7702 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7703 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7704 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7705 /* Note that it is OK if symver_sec is NULL. */
7708 finfo
.contents
= NULL
;
7709 finfo
.external_relocs
= NULL
;
7710 finfo
.internal_relocs
= NULL
;
7711 finfo
.external_syms
= NULL
;
7712 finfo
.locsym_shndx
= NULL
;
7713 finfo
.internal_syms
= NULL
;
7714 finfo
.indices
= NULL
;
7715 finfo
.sections
= NULL
;
7716 finfo
.symbuf
= NULL
;
7717 finfo
.symshndxbuf
= NULL
;
7718 finfo
.symbuf_count
= 0;
7719 finfo
.shndxbuf_size
= 0;
7721 /* Count up the number of relocations we will output for each output
7722 section, so that we know the sizes of the reloc sections. We
7723 also figure out some maximum sizes. */
7724 max_contents_size
= 0;
7725 max_external_reloc_size
= 0;
7726 max_internal_reloc_count
= 0;
7728 max_sym_shndx_count
= 0;
7730 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7732 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7735 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7737 unsigned int reloc_count
= 0;
7738 struct bfd_elf_section_data
*esdi
= NULL
;
7739 unsigned int *rel_count1
;
7741 if (p
->type
== bfd_section_reloc_link_order
7742 || p
->type
== bfd_symbol_reloc_link_order
)
7744 else if (p
->type
== bfd_indirect_link_order
)
7748 sec
= p
->u
.indirect
.section
;
7749 esdi
= elf_section_data (sec
);
7751 /* Mark all sections which are to be included in the
7752 link. This will normally be every section. We need
7753 to do this so that we can identify any sections which
7754 the linker has decided to not include. */
7755 sec
->linker_mark
= TRUE
;
7757 if (sec
->flags
& SEC_MERGE
)
7760 if (info
->relocatable
|| info
->emitrelocations
)
7761 reloc_count
= sec
->reloc_count
;
7762 else if (bed
->elf_backend_count_relocs
)
7764 Elf_Internal_Rela
* relocs
;
7766 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7769 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7771 if (elf_section_data (o
)->relocs
!= relocs
)
7775 if (sec
->rawsize
> max_contents_size
)
7776 max_contents_size
= sec
->rawsize
;
7777 if (sec
->size
> max_contents_size
)
7778 max_contents_size
= sec
->size
;
7780 /* We are interested in just local symbols, not all
7782 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7783 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7787 if (elf_bad_symtab (sec
->owner
))
7788 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7789 / bed
->s
->sizeof_sym
);
7791 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7793 if (sym_count
> max_sym_count
)
7794 max_sym_count
= sym_count
;
7796 if (sym_count
> max_sym_shndx_count
7797 && elf_symtab_shndx (sec
->owner
) != 0)
7798 max_sym_shndx_count
= sym_count
;
7800 if ((sec
->flags
& SEC_RELOC
) != 0)
7804 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7805 if (ext_size
> max_external_reloc_size
)
7806 max_external_reloc_size
= ext_size
;
7807 if (sec
->reloc_count
> max_internal_reloc_count
)
7808 max_internal_reloc_count
= sec
->reloc_count
;
7813 if (reloc_count
== 0)
7816 o
->reloc_count
+= reloc_count
;
7818 /* MIPS may have a mix of REL and RELA relocs on sections.
7819 To support this curious ABI we keep reloc counts in
7820 elf_section_data too. We must be careful to add the
7821 relocations from the input section to the right output
7822 count. FIXME: Get rid of one count. We have
7823 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7824 rel_count1
= &esdo
->rel_count
;
7827 bfd_boolean same_size
;
7828 bfd_size_type entsize1
;
7830 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7831 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7832 || entsize1
== bed
->s
->sizeof_rela
);
7833 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7836 rel_count1
= &esdo
->rel_count2
;
7838 if (esdi
->rel_hdr2
!= NULL
)
7840 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7841 unsigned int alt_count
;
7842 unsigned int *rel_count2
;
7844 BFD_ASSERT (entsize2
!= entsize1
7845 && (entsize2
== bed
->s
->sizeof_rel
7846 || entsize2
== bed
->s
->sizeof_rela
));
7848 rel_count2
= &esdo
->rel_count2
;
7850 rel_count2
= &esdo
->rel_count
;
7852 /* The following is probably too simplistic if the
7853 backend counts output relocs unusually. */
7854 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7855 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7856 *rel_count2
+= alt_count
;
7857 reloc_count
-= alt_count
;
7860 *rel_count1
+= reloc_count
;
7863 if (o
->reloc_count
> 0)
7864 o
->flags
|= SEC_RELOC
;
7867 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7868 set it (this is probably a bug) and if it is set
7869 assign_section_numbers will create a reloc section. */
7870 o
->flags
&=~ SEC_RELOC
;
7873 /* If the SEC_ALLOC flag is not set, force the section VMA to
7874 zero. This is done in elf_fake_sections as well, but forcing
7875 the VMA to 0 here will ensure that relocs against these
7876 sections are handled correctly. */
7877 if ((o
->flags
& SEC_ALLOC
) == 0
7878 && ! o
->user_set_vma
)
7882 if (! info
->relocatable
&& merged
)
7883 elf_link_hash_traverse (elf_hash_table (info
),
7884 _bfd_elf_link_sec_merge_syms
, abfd
);
7886 /* Figure out the file positions for everything but the symbol table
7887 and the relocs. We set symcount to force assign_section_numbers
7888 to create a symbol table. */
7889 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7890 BFD_ASSERT (! abfd
->output_has_begun
);
7891 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7894 /* Set sizes, and assign file positions for reloc sections. */
7895 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7897 if ((o
->flags
& SEC_RELOC
) != 0)
7899 if (!(_bfd_elf_link_size_reloc_section
7900 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7903 if (elf_section_data (o
)->rel_hdr2
7904 && !(_bfd_elf_link_size_reloc_section
7905 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7909 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7910 to count upwards while actually outputting the relocations. */
7911 elf_section_data (o
)->rel_count
= 0;
7912 elf_section_data (o
)->rel_count2
= 0;
7915 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7917 /* We have now assigned file positions for all the sections except
7918 .symtab and .strtab. We start the .symtab section at the current
7919 file position, and write directly to it. We build the .strtab
7920 section in memory. */
7921 bfd_get_symcount (abfd
) = 0;
7922 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7923 /* sh_name is set in prep_headers. */
7924 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7925 /* sh_flags, sh_addr and sh_size all start off zero. */
7926 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7927 /* sh_link is set in assign_section_numbers. */
7928 /* sh_info is set below. */
7929 /* sh_offset is set just below. */
7930 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7932 off
= elf_tdata (abfd
)->next_file_pos
;
7933 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7935 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7936 incorrect. We do not yet know the size of the .symtab section.
7937 We correct next_file_pos below, after we do know the size. */
7939 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7940 continuously seeking to the right position in the file. */
7941 if (! info
->keep_memory
|| max_sym_count
< 20)
7942 finfo
.symbuf_size
= 20;
7944 finfo
.symbuf_size
= max_sym_count
;
7945 amt
= finfo
.symbuf_size
;
7946 amt
*= bed
->s
->sizeof_sym
;
7947 finfo
.symbuf
= bfd_malloc (amt
);
7948 if (finfo
.symbuf
== NULL
)
7950 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7952 /* Wild guess at number of output symbols. realloc'd as needed. */
7953 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7954 finfo
.shndxbuf_size
= amt
;
7955 amt
*= sizeof (Elf_External_Sym_Shndx
);
7956 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7957 if (finfo
.symshndxbuf
== NULL
)
7961 /* Start writing out the symbol table. The first symbol is always a
7963 if (info
->strip
!= strip_all
7966 elfsym
.st_value
= 0;
7969 elfsym
.st_other
= 0;
7970 elfsym
.st_shndx
= SHN_UNDEF
;
7971 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7976 /* Output a symbol for each section. We output these even if we are
7977 discarding local symbols, since they are used for relocs. These
7978 symbols have no names. We store the index of each one in the
7979 index field of the section, so that we can find it again when
7980 outputting relocs. */
7981 if (info
->strip
!= strip_all
7985 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7986 elfsym
.st_other
= 0;
7987 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7989 o
= bfd_section_from_elf_index (abfd
, i
);
7991 o
->target_index
= bfd_get_symcount (abfd
);
7992 elfsym
.st_shndx
= i
;
7993 if (info
->relocatable
|| o
== NULL
)
7994 elfsym
.st_value
= 0;
7996 elfsym
.st_value
= o
->vma
;
7997 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7999 if (i
== SHN_LORESERVE
- 1)
8000 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8004 /* Allocate some memory to hold information read in from the input
8006 if (max_contents_size
!= 0)
8008 finfo
.contents
= bfd_malloc (max_contents_size
);
8009 if (finfo
.contents
== NULL
)
8013 if (max_external_reloc_size
!= 0)
8015 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8016 if (finfo
.external_relocs
== NULL
)
8020 if (max_internal_reloc_count
!= 0)
8022 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8023 amt
*= sizeof (Elf_Internal_Rela
);
8024 finfo
.internal_relocs
= bfd_malloc (amt
);
8025 if (finfo
.internal_relocs
== NULL
)
8029 if (max_sym_count
!= 0)
8031 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8032 finfo
.external_syms
= bfd_malloc (amt
);
8033 if (finfo
.external_syms
== NULL
)
8036 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8037 finfo
.internal_syms
= bfd_malloc (amt
);
8038 if (finfo
.internal_syms
== NULL
)
8041 amt
= max_sym_count
* sizeof (long);
8042 finfo
.indices
= bfd_malloc (amt
);
8043 if (finfo
.indices
== NULL
)
8046 amt
= max_sym_count
* sizeof (asection
*);
8047 finfo
.sections
= bfd_malloc (amt
);
8048 if (finfo
.sections
== NULL
)
8052 if (max_sym_shndx_count
!= 0)
8054 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8055 finfo
.locsym_shndx
= bfd_malloc (amt
);
8056 if (finfo
.locsym_shndx
== NULL
)
8060 if (elf_hash_table (info
)->tls_sec
)
8062 bfd_vma base
, end
= 0;
8065 for (sec
= elf_hash_table (info
)->tls_sec
;
8066 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8069 bfd_vma size
= sec
->size
;
8071 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8073 struct bfd_link_order
*o
;
8075 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
8076 if (size
< o
->offset
+ o
->size
)
8077 size
= o
->offset
+ o
->size
;
8079 end
= sec
->vma
+ size
;
8081 base
= elf_hash_table (info
)->tls_sec
->vma
;
8082 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8083 elf_hash_table (info
)->tls_size
= end
- base
;
8086 /* Reorder SHF_LINK_ORDER sections. */
8087 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8089 if (!elf_fixup_link_order (abfd
, o
))
8093 /* Since ELF permits relocations to be against local symbols, we
8094 must have the local symbols available when we do the relocations.
8095 Since we would rather only read the local symbols once, and we
8096 would rather not keep them in memory, we handle all the
8097 relocations for a single input file at the same time.
8099 Unfortunately, there is no way to know the total number of local
8100 symbols until we have seen all of them, and the local symbol
8101 indices precede the global symbol indices. This means that when
8102 we are generating relocatable output, and we see a reloc against
8103 a global symbol, we can not know the symbol index until we have
8104 finished examining all the local symbols to see which ones we are
8105 going to output. To deal with this, we keep the relocations in
8106 memory, and don't output them until the end of the link. This is
8107 an unfortunate waste of memory, but I don't see a good way around
8108 it. Fortunately, it only happens when performing a relocatable
8109 link, which is not the common case. FIXME: If keep_memory is set
8110 we could write the relocs out and then read them again; I don't
8111 know how bad the memory loss will be. */
8113 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8114 sub
->output_has_begun
= FALSE
;
8115 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8117 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
8119 if (p
->type
== bfd_indirect_link_order
8120 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8121 == bfd_target_elf_flavour
)
8122 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8124 if (! sub
->output_has_begun
)
8126 if (! elf_link_input_bfd (&finfo
, sub
))
8128 sub
->output_has_begun
= TRUE
;
8131 else if (p
->type
== bfd_section_reloc_link_order
8132 || p
->type
== bfd_symbol_reloc_link_order
)
8134 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8139 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8145 /* Output any global symbols that got converted to local in a
8146 version script or due to symbol visibility. We do this in a
8147 separate step since ELF requires all local symbols to appear
8148 prior to any global symbols. FIXME: We should only do this if
8149 some global symbols were, in fact, converted to become local.
8150 FIXME: Will this work correctly with the Irix 5 linker? */
8151 eoinfo
.failed
= FALSE
;
8152 eoinfo
.finfo
= &finfo
;
8153 eoinfo
.localsyms
= TRUE
;
8154 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8159 /* That wrote out all the local symbols. Finish up the symbol table
8160 with the global symbols. Even if we want to strip everything we
8161 can, we still need to deal with those global symbols that got
8162 converted to local in a version script. */
8164 /* The sh_info field records the index of the first non local symbol. */
8165 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8168 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8170 Elf_Internal_Sym sym
;
8171 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8172 long last_local
= 0;
8174 /* Write out the section symbols for the output sections. */
8175 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8181 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8184 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8190 dynindx
= elf_section_data (s
)->dynindx
;
8193 indx
= elf_section_data (s
)->this_idx
;
8194 BFD_ASSERT (indx
> 0);
8195 sym
.st_shndx
= indx
;
8196 sym
.st_value
= s
->vma
;
8197 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8198 if (last_local
< dynindx
)
8199 last_local
= dynindx
;
8200 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8204 /* Write out the local dynsyms. */
8205 if (elf_hash_table (info
)->dynlocal
)
8207 struct elf_link_local_dynamic_entry
*e
;
8208 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8213 sym
.st_size
= e
->isym
.st_size
;
8214 sym
.st_other
= e
->isym
.st_other
;
8216 /* Copy the internal symbol as is.
8217 Note that we saved a word of storage and overwrote
8218 the original st_name with the dynstr_index. */
8221 if (e
->isym
.st_shndx
!= SHN_UNDEF
8222 && (e
->isym
.st_shndx
< SHN_LORESERVE
8223 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8225 s
= bfd_section_from_elf_index (e
->input_bfd
,
8229 elf_section_data (s
->output_section
)->this_idx
;
8230 sym
.st_value
= (s
->output_section
->vma
8232 + e
->isym
.st_value
);
8235 if (last_local
< e
->dynindx
)
8236 last_local
= e
->dynindx
;
8238 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8239 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8243 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8247 /* We get the global symbols from the hash table. */
8248 eoinfo
.failed
= FALSE
;
8249 eoinfo
.localsyms
= FALSE
;
8250 eoinfo
.finfo
= &finfo
;
8251 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8256 /* If backend needs to output some symbols not present in the hash
8257 table, do it now. */
8258 if (bed
->elf_backend_output_arch_syms
)
8260 typedef bfd_boolean (*out_sym_func
)
8261 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8262 struct elf_link_hash_entry
*);
8264 if (! ((*bed
->elf_backend_output_arch_syms
)
8265 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8269 /* Flush all symbols to the file. */
8270 if (! elf_link_flush_output_syms (&finfo
, bed
))
8273 /* Now we know the size of the symtab section. */
8274 off
+= symtab_hdr
->sh_size
;
8276 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8277 if (symtab_shndx_hdr
->sh_name
!= 0)
8279 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8280 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8281 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8282 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8283 symtab_shndx_hdr
->sh_size
= amt
;
8285 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8288 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8289 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8294 /* Finish up and write out the symbol string table (.strtab)
8296 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8297 /* sh_name was set in prep_headers. */
8298 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8299 symstrtab_hdr
->sh_flags
= 0;
8300 symstrtab_hdr
->sh_addr
= 0;
8301 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8302 symstrtab_hdr
->sh_entsize
= 0;
8303 symstrtab_hdr
->sh_link
= 0;
8304 symstrtab_hdr
->sh_info
= 0;
8305 /* sh_offset is set just below. */
8306 symstrtab_hdr
->sh_addralign
= 1;
8308 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8309 elf_tdata (abfd
)->next_file_pos
= off
;
8311 if (bfd_get_symcount (abfd
) > 0)
8313 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8314 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8318 /* Adjust the relocs to have the correct symbol indices. */
8319 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8321 if ((o
->flags
& SEC_RELOC
) == 0)
8324 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8325 elf_section_data (o
)->rel_count
,
8326 elf_section_data (o
)->rel_hashes
);
8327 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8328 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8329 elf_section_data (o
)->rel_count2
,
8330 (elf_section_data (o
)->rel_hashes
8331 + elf_section_data (o
)->rel_count
));
8333 /* Set the reloc_count field to 0 to prevent write_relocs from
8334 trying to swap the relocs out itself. */
8338 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8339 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8341 /* If we are linking against a dynamic object, or generating a
8342 shared library, finish up the dynamic linking information. */
8345 bfd_byte
*dyncon
, *dynconend
;
8347 /* Fix up .dynamic entries. */
8348 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8349 BFD_ASSERT (o
!= NULL
);
8351 dyncon
= o
->contents
;
8352 dynconend
= o
->contents
+ o
->size
;
8353 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8355 Elf_Internal_Dyn dyn
;
8359 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8366 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8368 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8370 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8371 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8374 dyn
.d_un
.d_val
= relativecount
;
8381 name
= info
->init_function
;
8384 name
= info
->fini_function
;
8387 struct elf_link_hash_entry
*h
;
8389 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8390 FALSE
, FALSE
, TRUE
);
8392 && (h
->root
.type
== bfd_link_hash_defined
8393 || h
->root
.type
== bfd_link_hash_defweak
))
8395 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8396 o
= h
->root
.u
.def
.section
;
8397 if (o
->output_section
!= NULL
)
8398 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8399 + o
->output_offset
);
8402 /* The symbol is imported from another shared
8403 library and does not apply to this one. */
8411 case DT_PREINIT_ARRAYSZ
:
8412 name
= ".preinit_array";
8414 case DT_INIT_ARRAYSZ
:
8415 name
= ".init_array";
8417 case DT_FINI_ARRAYSZ
:
8418 name
= ".fini_array";
8420 o
= bfd_get_section_by_name (abfd
, name
);
8423 (*_bfd_error_handler
)
8424 (_("%B: could not find output section %s"), abfd
, name
);
8428 (*_bfd_error_handler
)
8429 (_("warning: %s section has zero size"), name
);
8430 dyn
.d_un
.d_val
= o
->size
;
8433 case DT_PREINIT_ARRAY
:
8434 name
= ".preinit_array";
8437 name
= ".init_array";
8440 name
= ".fini_array";
8453 name
= ".gnu.version_d";
8456 name
= ".gnu.version_r";
8459 name
= ".gnu.version";
8461 o
= bfd_get_section_by_name (abfd
, name
);
8464 (*_bfd_error_handler
)
8465 (_("%B: could not find output section %s"), abfd
, name
);
8468 dyn
.d_un
.d_ptr
= o
->vma
;
8475 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8480 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8482 Elf_Internal_Shdr
*hdr
;
8484 hdr
= elf_elfsections (abfd
)[i
];
8485 if (hdr
->sh_type
== type
8486 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8488 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8489 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8492 if (dyn
.d_un
.d_val
== 0
8493 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8494 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8500 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8504 /* If we have created any dynamic sections, then output them. */
8507 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8510 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8512 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8514 || o
->output_section
== bfd_abs_section_ptr
)
8516 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8518 /* At this point, we are only interested in sections
8519 created by _bfd_elf_link_create_dynamic_sections. */
8522 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8524 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8526 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8528 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8530 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8532 (file_ptr
) o
->output_offset
,
8538 /* The contents of the .dynstr section are actually in a
8540 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8541 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8542 || ! _bfd_elf_strtab_emit (abfd
,
8543 elf_hash_table (info
)->dynstr
))
8549 if (info
->relocatable
)
8551 bfd_boolean failed
= FALSE
;
8553 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8558 /* If we have optimized stabs strings, output them. */
8559 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8561 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8565 if (info
->eh_frame_hdr
)
8567 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8571 if (finfo
.symstrtab
!= NULL
)
8572 _bfd_stringtab_free (finfo
.symstrtab
);
8573 if (finfo
.contents
!= NULL
)
8574 free (finfo
.contents
);
8575 if (finfo
.external_relocs
!= NULL
)
8576 free (finfo
.external_relocs
);
8577 if (finfo
.internal_relocs
!= NULL
)
8578 free (finfo
.internal_relocs
);
8579 if (finfo
.external_syms
!= NULL
)
8580 free (finfo
.external_syms
);
8581 if (finfo
.locsym_shndx
!= NULL
)
8582 free (finfo
.locsym_shndx
);
8583 if (finfo
.internal_syms
!= NULL
)
8584 free (finfo
.internal_syms
);
8585 if (finfo
.indices
!= NULL
)
8586 free (finfo
.indices
);
8587 if (finfo
.sections
!= NULL
)
8588 free (finfo
.sections
);
8589 if (finfo
.symbuf
!= NULL
)
8590 free (finfo
.symbuf
);
8591 if (finfo
.symshndxbuf
!= NULL
)
8592 free (finfo
.symshndxbuf
);
8593 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8595 if ((o
->flags
& SEC_RELOC
) != 0
8596 && elf_section_data (o
)->rel_hashes
!= NULL
)
8597 free (elf_section_data (o
)->rel_hashes
);
8600 elf_tdata (abfd
)->linker
= TRUE
;
8605 if (finfo
.symstrtab
!= NULL
)
8606 _bfd_stringtab_free (finfo
.symstrtab
);
8607 if (finfo
.contents
!= NULL
)
8608 free (finfo
.contents
);
8609 if (finfo
.external_relocs
!= NULL
)
8610 free (finfo
.external_relocs
);
8611 if (finfo
.internal_relocs
!= NULL
)
8612 free (finfo
.internal_relocs
);
8613 if (finfo
.external_syms
!= NULL
)
8614 free (finfo
.external_syms
);
8615 if (finfo
.locsym_shndx
!= NULL
)
8616 free (finfo
.locsym_shndx
);
8617 if (finfo
.internal_syms
!= NULL
)
8618 free (finfo
.internal_syms
);
8619 if (finfo
.indices
!= NULL
)
8620 free (finfo
.indices
);
8621 if (finfo
.sections
!= NULL
)
8622 free (finfo
.sections
);
8623 if (finfo
.symbuf
!= NULL
)
8624 free (finfo
.symbuf
);
8625 if (finfo
.symshndxbuf
!= NULL
)
8626 free (finfo
.symshndxbuf
);
8627 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8629 if ((o
->flags
& SEC_RELOC
) != 0
8630 && elf_section_data (o
)->rel_hashes
!= NULL
)
8631 free (elf_section_data (o
)->rel_hashes
);
8637 /* Garbage collect unused sections. */
8639 /* The mark phase of garbage collection. For a given section, mark
8640 it and any sections in this section's group, and all the sections
8641 which define symbols to which it refers. */
8643 typedef asection
* (*gc_mark_hook_fn
)
8644 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8645 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8648 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8650 gc_mark_hook_fn gc_mark_hook
)
8653 asection
*group_sec
;
8657 /* Mark all the sections in the group. */
8658 group_sec
= elf_section_data (sec
)->next_in_group
;
8659 if (group_sec
&& !group_sec
->gc_mark
)
8660 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8663 /* Look through the section relocs. */
8665 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8667 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8668 Elf_Internal_Shdr
*symtab_hdr
;
8669 struct elf_link_hash_entry
**sym_hashes
;
8672 bfd
*input_bfd
= sec
->owner
;
8673 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8674 Elf_Internal_Sym
*isym
= NULL
;
8677 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8678 sym_hashes
= elf_sym_hashes (input_bfd
);
8680 /* Read the local symbols. */
8681 if (elf_bad_symtab (input_bfd
))
8683 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8687 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8689 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8690 if (isym
== NULL
&& nlocsyms
!= 0)
8692 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8698 /* Read the relocations. */
8699 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8701 if (relstart
== NULL
)
8706 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8708 if (bed
->s
->arch_size
== 32)
8713 for (rel
= relstart
; rel
< relend
; rel
++)
8715 unsigned long r_symndx
;
8717 struct elf_link_hash_entry
*h
;
8719 r_symndx
= rel
->r_info
>> r_sym_shift
;
8723 if (r_symndx
>= nlocsyms
8724 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8726 h
= sym_hashes
[r_symndx
- extsymoff
];
8727 while (h
->root
.type
== bfd_link_hash_indirect
8728 || h
->root
.type
== bfd_link_hash_warning
)
8729 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8730 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8734 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8737 if (rsec
&& !rsec
->gc_mark
)
8739 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8741 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8750 if (elf_section_data (sec
)->relocs
!= relstart
)
8753 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8755 if (! info
->keep_memory
)
8758 symtab_hdr
->contents
= (unsigned char *) isym
;
8765 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8768 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8772 if (h
->root
.type
== bfd_link_hash_warning
)
8773 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8775 if (h
->dynindx
!= -1
8776 && ((h
->root
.type
!= bfd_link_hash_defined
8777 && h
->root
.type
!= bfd_link_hash_defweak
)
8778 || h
->root
.u
.def
.section
->gc_mark
))
8779 h
->dynindx
= (*idx
)++;
8784 /* The sweep phase of garbage collection. Remove all garbage sections. */
8786 typedef bfd_boolean (*gc_sweep_hook_fn
)
8787 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8790 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8794 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8798 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8801 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8803 /* Keep debug and special sections. */
8804 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8805 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8811 /* Skip sweeping sections already excluded. */
8812 if (o
->flags
& SEC_EXCLUDE
)
8815 /* Since this is early in the link process, it is simple
8816 to remove a section from the output. */
8817 o
->flags
|= SEC_EXCLUDE
;
8819 /* But we also have to update some of the relocation
8820 info we collected before. */
8822 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8824 Elf_Internal_Rela
*internal_relocs
;
8828 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8830 if (internal_relocs
== NULL
)
8833 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8835 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8836 free (internal_relocs
);
8844 /* Remove the symbols that were in the swept sections from the dynamic
8845 symbol table. GCFIXME: Anyone know how to get them out of the
8846 static symbol table as well? */
8850 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8852 elf_hash_table (info
)->dynsymcount
= i
;
8858 /* Propagate collected vtable information. This is called through
8859 elf_link_hash_traverse. */
8862 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8864 if (h
->root
.type
== bfd_link_hash_warning
)
8865 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8867 /* Those that are not vtables. */
8868 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8871 /* Those vtables that do not have parents, we cannot merge. */
8872 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8875 /* If we've already been done, exit. */
8876 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8879 /* Make sure the parent's table is up to date. */
8880 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8882 if (h
->vtable
->used
== NULL
)
8884 /* None of this table's entries were referenced. Re-use the
8886 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8887 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8892 bfd_boolean
*cu
, *pu
;
8894 /* Or the parent's entries into ours. */
8895 cu
= h
->vtable
->used
;
8897 pu
= h
->vtable
->parent
->vtable
->used
;
8900 const struct elf_backend_data
*bed
;
8901 unsigned int log_file_align
;
8903 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8904 log_file_align
= bed
->s
->log_file_align
;
8905 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8920 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8923 bfd_vma hstart
, hend
;
8924 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8925 const struct elf_backend_data
*bed
;
8926 unsigned int log_file_align
;
8928 if (h
->root
.type
== bfd_link_hash_warning
)
8929 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8931 /* Take care of both those symbols that do not describe vtables as
8932 well as those that are not loaded. */
8933 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8936 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8937 || h
->root
.type
== bfd_link_hash_defweak
);
8939 sec
= h
->root
.u
.def
.section
;
8940 hstart
= h
->root
.u
.def
.value
;
8941 hend
= hstart
+ h
->size
;
8943 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8945 return *(bfd_boolean
*) okp
= FALSE
;
8946 bed
= get_elf_backend_data (sec
->owner
);
8947 log_file_align
= bed
->s
->log_file_align
;
8949 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8951 for (rel
= relstart
; rel
< relend
; ++rel
)
8952 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8954 /* If the entry is in use, do nothing. */
8956 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8958 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8959 if (h
->vtable
->used
[entry
])
8962 /* Otherwise, kill it. */
8963 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8969 /* Mark sections containing dynamically referenced symbols. This is called
8970 through elf_link_hash_traverse. */
8973 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8974 void *okp ATTRIBUTE_UNUSED
)
8976 if (h
->root
.type
== bfd_link_hash_warning
)
8977 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8979 if ((h
->root
.type
== bfd_link_hash_defined
8980 || h
->root
.type
== bfd_link_hash_defweak
)
8982 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8987 /* Do mark and sweep of unused sections. */
8990 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8992 bfd_boolean ok
= TRUE
;
8994 asection
* (*gc_mark_hook
)
8995 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8996 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8998 if (!get_elf_backend_data (abfd
)->can_gc_sections
8999 || info
->relocatable
9000 || info
->emitrelocations
9002 || !is_elf_hash_table (info
->hash
))
9004 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9008 /* Apply transitive closure to the vtable entry usage info. */
9009 elf_link_hash_traverse (elf_hash_table (info
),
9010 elf_gc_propagate_vtable_entries_used
,
9015 /* Kill the vtable relocations that were not used. */
9016 elf_link_hash_traverse (elf_hash_table (info
),
9017 elf_gc_smash_unused_vtentry_relocs
,
9022 /* Mark dynamically referenced symbols. */
9023 if (elf_hash_table (info
)->dynamic_sections_created
)
9024 elf_link_hash_traverse (elf_hash_table (info
),
9025 elf_gc_mark_dynamic_ref_symbol
,
9030 /* Grovel through relocs to find out who stays ... */
9031 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
9032 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9036 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9039 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9041 if (o
->flags
& SEC_KEEP
)
9043 /* _bfd_elf_discard_section_eh_frame knows how to discard
9044 orphaned FDEs so don't mark sections referenced by the
9045 EH frame section. */
9046 if (strcmp (o
->name
, ".eh_frame") == 0)
9048 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9054 /* ... and mark SEC_EXCLUDE for those that go. */
9055 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
9061 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9064 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9066 struct elf_link_hash_entry
*h
,
9069 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9070 struct elf_link_hash_entry
**search
, *child
;
9071 bfd_size_type extsymcount
;
9072 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9074 /* The sh_info field of the symtab header tells us where the
9075 external symbols start. We don't care about the local symbols at
9077 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9078 if (!elf_bad_symtab (abfd
))
9079 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9081 sym_hashes
= elf_sym_hashes (abfd
);
9082 sym_hashes_end
= sym_hashes
+ extsymcount
;
9084 /* Hunt down the child symbol, which is in this section at the same
9085 offset as the relocation. */
9086 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9088 if ((child
= *search
) != NULL
9089 && (child
->root
.type
== bfd_link_hash_defined
9090 || child
->root
.type
== bfd_link_hash_defweak
)
9091 && child
->root
.u
.def
.section
== sec
9092 && child
->root
.u
.def
.value
== offset
)
9096 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9097 abfd
, sec
, (unsigned long) offset
);
9098 bfd_set_error (bfd_error_invalid_operation
);
9104 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9110 /* This *should* only be the absolute section. It could potentially
9111 be that someone has defined a non-global vtable though, which
9112 would be bad. It isn't worth paging in the local symbols to be
9113 sure though; that case should simply be handled by the assembler. */
9115 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9118 child
->vtable
->parent
= h
;
9123 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9126 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9127 asection
*sec ATTRIBUTE_UNUSED
,
9128 struct elf_link_hash_entry
*h
,
9131 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9132 unsigned int log_file_align
= bed
->s
->log_file_align
;
9136 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9141 if (addend
>= h
->vtable
->size
)
9143 size_t size
, bytes
, file_align
;
9144 bfd_boolean
*ptr
= h
->vtable
->used
;
9146 /* While the symbol is undefined, we have to be prepared to handle
9148 file_align
= 1 << log_file_align
;
9149 if (h
->root
.type
== bfd_link_hash_undefined
)
9150 size
= addend
+ file_align
;
9156 /* Oops! We've got a reference past the defined end of
9157 the table. This is probably a bug -- shall we warn? */
9158 size
= addend
+ file_align
;
9161 size
= (size
+ file_align
- 1) & -file_align
;
9163 /* Allocate one extra entry for use as a "done" flag for the
9164 consolidation pass. */
9165 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9169 ptr
= bfd_realloc (ptr
- 1, bytes
);
9175 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9176 * sizeof (bfd_boolean
));
9177 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9181 ptr
= bfd_zmalloc (bytes
);
9186 /* And arrange for that done flag to be at index -1. */
9187 h
->vtable
->used
= ptr
+ 1;
9188 h
->vtable
->size
= size
;
9191 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9196 struct alloc_got_off_arg
{
9198 unsigned int got_elt_size
;
9201 /* We need a special top-level link routine to convert got reference counts
9202 to real got offsets. */
9205 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9207 struct alloc_got_off_arg
*gofarg
= arg
;
9209 if (h
->root
.type
== bfd_link_hash_warning
)
9210 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9212 if (h
->got
.refcount
> 0)
9214 h
->got
.offset
= gofarg
->gotoff
;
9215 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9218 h
->got
.offset
= (bfd_vma
) -1;
9223 /* And an accompanying bit to work out final got entry offsets once
9224 we're done. Should be called from final_link. */
9227 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9228 struct bfd_link_info
*info
)
9231 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9233 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9234 struct alloc_got_off_arg gofarg
;
9236 if (! is_elf_hash_table (info
->hash
))
9239 /* The GOT offset is relative to the .got section, but the GOT header is
9240 put into the .got.plt section, if the backend uses it. */
9241 if (bed
->want_got_plt
)
9244 gotoff
= bed
->got_header_size
;
9246 /* Do the local .got entries first. */
9247 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9249 bfd_signed_vma
*local_got
;
9250 bfd_size_type j
, locsymcount
;
9251 Elf_Internal_Shdr
*symtab_hdr
;
9253 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9256 local_got
= elf_local_got_refcounts (i
);
9260 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9261 if (elf_bad_symtab (i
))
9262 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9264 locsymcount
= symtab_hdr
->sh_info
;
9266 for (j
= 0; j
< locsymcount
; ++j
)
9268 if (local_got
[j
] > 0)
9270 local_got
[j
] = gotoff
;
9271 gotoff
+= got_elt_size
;
9274 local_got
[j
] = (bfd_vma
) -1;
9278 /* Then the global .got entries. .plt refcounts are handled by
9279 adjust_dynamic_symbol */
9280 gofarg
.gotoff
= gotoff
;
9281 gofarg
.got_elt_size
= got_elt_size
;
9282 elf_link_hash_traverse (elf_hash_table (info
),
9283 elf_gc_allocate_got_offsets
,
9288 /* Many folk need no more in the way of final link than this, once
9289 got entry reference counting is enabled. */
9292 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9294 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9297 /* Invoke the regular ELF backend linker to do all the work. */
9298 return bfd_elf_final_link (abfd
, info
);
9302 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9304 struct elf_reloc_cookie
*rcookie
= cookie
;
9306 if (rcookie
->bad_symtab
)
9307 rcookie
->rel
= rcookie
->rels
;
9309 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9311 unsigned long r_symndx
;
9313 if (! rcookie
->bad_symtab
)
9314 if (rcookie
->rel
->r_offset
> offset
)
9316 if (rcookie
->rel
->r_offset
!= offset
)
9319 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9320 if (r_symndx
== SHN_UNDEF
)
9323 if (r_symndx
>= rcookie
->locsymcount
9324 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9326 struct elf_link_hash_entry
*h
;
9328 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9330 while (h
->root
.type
== bfd_link_hash_indirect
9331 || h
->root
.type
== bfd_link_hash_warning
)
9332 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9334 if ((h
->root
.type
== bfd_link_hash_defined
9335 || h
->root
.type
== bfd_link_hash_defweak
)
9336 && elf_discarded_section (h
->root
.u
.def
.section
))
9343 /* It's not a relocation against a global symbol,
9344 but it could be a relocation against a local
9345 symbol for a discarded section. */
9347 Elf_Internal_Sym
*isym
;
9349 /* Need to: get the symbol; get the section. */
9350 isym
= &rcookie
->locsyms
[r_symndx
];
9351 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9353 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9354 if (isec
!= NULL
&& elf_discarded_section (isec
))
9363 /* Discard unneeded references to discarded sections.
9364 Returns TRUE if any section's size was changed. */
9365 /* This function assumes that the relocations are in sorted order,
9366 which is true for all known assemblers. */
9369 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9371 struct elf_reloc_cookie cookie
;
9372 asection
*stab
, *eh
;
9373 Elf_Internal_Shdr
*symtab_hdr
;
9374 const struct elf_backend_data
*bed
;
9377 bfd_boolean ret
= FALSE
;
9379 if (info
->traditional_format
9380 || !is_elf_hash_table (info
->hash
))
9383 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9385 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9388 bed
= get_elf_backend_data (abfd
);
9390 if ((abfd
->flags
& DYNAMIC
) != 0)
9393 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9394 if (info
->relocatable
9397 || bfd_is_abs_section (eh
->output_section
))))
9400 stab
= bfd_get_section_by_name (abfd
, ".stab");
9403 || bfd_is_abs_section (stab
->output_section
)
9404 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9409 && bed
->elf_backend_discard_info
== NULL
)
9412 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9414 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9415 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9416 if (cookie
.bad_symtab
)
9418 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9419 cookie
.extsymoff
= 0;
9423 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9424 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9427 if (bed
->s
->arch_size
== 32)
9428 cookie
.r_sym_shift
= 8;
9430 cookie
.r_sym_shift
= 32;
9432 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9433 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9435 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9436 cookie
.locsymcount
, 0,
9438 if (cookie
.locsyms
== NULL
)
9445 count
= stab
->reloc_count
;
9447 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9449 if (cookie
.rels
!= NULL
)
9451 cookie
.rel
= cookie
.rels
;
9452 cookie
.relend
= cookie
.rels
;
9453 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9454 if (_bfd_discard_section_stabs (abfd
, stab
,
9455 elf_section_data (stab
)->sec_info
,
9456 bfd_elf_reloc_symbol_deleted_p
,
9459 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9467 count
= eh
->reloc_count
;
9469 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9471 cookie
.rel
= cookie
.rels
;
9472 cookie
.relend
= cookie
.rels
;
9473 if (cookie
.rels
!= NULL
)
9474 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9476 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9477 bfd_elf_reloc_symbol_deleted_p
,
9481 if (cookie
.rels
!= NULL
9482 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9486 if (bed
->elf_backend_discard_info
!= NULL
9487 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9490 if (cookie
.locsyms
!= NULL
9491 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9493 if (! info
->keep_memory
)
9494 free (cookie
.locsyms
);
9496 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9500 if (info
->eh_frame_hdr
9501 && !info
->relocatable
9502 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9509 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9512 const char *name
, *p
;
9513 struct bfd_section_already_linked
*l
;
9514 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9517 /* A single member comdat group section may be discarded by a
9518 linkonce section. See below. */
9519 if (sec
->output_section
== bfd_abs_section_ptr
)
9524 /* Check if it belongs to a section group. */
9525 group
= elf_sec_group (sec
);
9527 /* Return if it isn't a linkonce section nor a member of a group. A
9528 comdat group section also has SEC_LINK_ONCE set. */
9529 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9534 /* If this is the member of a single member comdat group, check if
9535 the group should be discarded. */
9536 if (elf_next_in_group (sec
) == sec
9537 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9543 /* FIXME: When doing a relocatable link, we may have trouble
9544 copying relocations in other sections that refer to local symbols
9545 in the section being discarded. Those relocations will have to
9546 be converted somehow; as of this writing I'm not sure that any of
9547 the backends handle that correctly.
9549 It is tempting to instead not discard link once sections when
9550 doing a relocatable link (technically, they should be discarded
9551 whenever we are building constructors). However, that fails,
9552 because the linker winds up combining all the link once sections
9553 into a single large link once section, which defeats the purpose
9554 of having link once sections in the first place.
9556 Also, not merging link once sections in a relocatable link
9557 causes trouble for MIPS ELF, which relies on link once semantics
9558 to handle the .reginfo section correctly. */
9560 name
= bfd_get_section_name (abfd
, sec
);
9562 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9563 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9568 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9570 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9572 /* We may have 3 different sections on the list: group section,
9573 comdat section and linkonce section. SEC may be a linkonce or
9574 group section. We match a group section with a group section,
9575 a linkonce section with a linkonce section, and ignore comdat
9577 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9578 && strcmp (name
, l
->sec
->name
) == 0
9579 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9581 /* The section has already been linked. See if we should
9583 switch (flags
& SEC_LINK_DUPLICATES
)
9588 case SEC_LINK_DUPLICATES_DISCARD
:
9591 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9592 (*_bfd_error_handler
)
9593 (_("%B: ignoring duplicate section `%A'\n"),
9597 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9598 if (sec
->size
!= l
->sec
->size
)
9599 (*_bfd_error_handler
)
9600 (_("%B: duplicate section `%A' has different size\n"),
9604 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9605 if (sec
->size
!= l
->sec
->size
)
9606 (*_bfd_error_handler
)
9607 (_("%B: duplicate section `%A' has different size\n"),
9609 else if (sec
->size
!= 0)
9611 bfd_byte
*sec_contents
, *l_sec_contents
;
9613 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9614 (*_bfd_error_handler
)
9615 (_("%B: warning: could not read contents of section `%A'\n"),
9617 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9619 (*_bfd_error_handler
)
9620 (_("%B: warning: could not read contents of section `%A'\n"),
9621 l
->sec
->owner
, l
->sec
);
9622 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9623 (*_bfd_error_handler
)
9624 (_("%B: warning: duplicate section `%A' has different contents\n"),
9628 free (sec_contents
);
9630 free (l_sec_contents
);
9635 /* Set the output_section field so that lang_add_section
9636 does not create a lang_input_section structure for this
9637 section. Since there might be a symbol in the section
9638 being discarded, we must retain a pointer to the section
9639 which we are really going to use. */
9640 sec
->output_section
= bfd_abs_section_ptr
;
9641 sec
->kept_section
= l
->sec
;
9643 if (flags
& SEC_GROUP
)
9645 asection
*first
= elf_next_in_group (sec
);
9646 asection
*s
= first
;
9650 s
->output_section
= bfd_abs_section_ptr
;
9651 /* Record which group discards it. */
9652 s
->kept_section
= l
->sec
;
9653 s
= elf_next_in_group (s
);
9654 /* These lists are circular. */
9666 /* If this is the member of a single member comdat group and the
9667 group hasn't be discarded, we check if it matches a linkonce
9668 section. We only record the discarded comdat group. Otherwise
9669 the undiscarded group will be discarded incorrectly later since
9670 itself has been recorded. */
9671 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9672 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9673 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9674 && bfd_elf_match_symbols_in_sections (l
->sec
,
9675 elf_next_in_group (sec
)))
9677 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9678 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9679 group
->output_section
= bfd_abs_section_ptr
;
9686 /* There is no direct match. But for linkonce section, we should
9687 check if there is a match with comdat group member. We always
9688 record the linkonce section, discarded or not. */
9689 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9690 if (l
->sec
->flags
& SEC_GROUP
)
9692 asection
*first
= elf_next_in_group (l
->sec
);
9695 && elf_next_in_group (first
) == first
9696 && bfd_elf_match_symbols_in_sections (first
, sec
))
9698 sec
->output_section
= bfd_abs_section_ptr
;
9699 sec
->kept_section
= l
->sec
;
9704 /* This is the first section with this name. Record it. */
9705 bfd_section_already_linked_table_insert (already_linked_list
, sec
);