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 02110-1301, 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
,
712 struct bfd_link_info
*info
,
713 unsigned long *section_sym_count
)
715 unsigned long dynsymcount
= 0;
717 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
719 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
721 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
722 if ((p
->flags
& SEC_EXCLUDE
) == 0
723 && (p
->flags
& SEC_ALLOC
) != 0
724 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
725 elf_section_data (p
)->dynindx
= ++dynsymcount
;
727 *section_sym_count
= dynsymcount
;
729 elf_link_hash_traverse (elf_hash_table (info
),
730 elf_link_renumber_local_hash_table_dynsyms
,
733 if (elf_hash_table (info
)->dynlocal
)
735 struct elf_link_local_dynamic_entry
*p
;
736 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
737 p
->dynindx
= ++dynsymcount
;
740 elf_link_hash_traverse (elf_hash_table (info
),
741 elf_link_renumber_hash_table_dynsyms
,
744 /* There is an unused NULL entry at the head of the table which
745 we must account for in our count. Unless there weren't any
746 symbols, which means we'll have no table at all. */
747 if (dynsymcount
!= 0)
750 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
753 /* This function is called when we want to define a new symbol. It
754 handles the various cases which arise when we find a definition in
755 a dynamic object, or when there is already a definition in a
756 dynamic object. The new symbol is described by NAME, SYM, PSEC,
757 and PVALUE. We set SYM_HASH to the hash table entry. We set
758 OVERRIDE if the old symbol is overriding a new definition. We set
759 TYPE_CHANGE_OK if it is OK for the type to change. We set
760 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
761 change, we mean that we shouldn't warn if the type or size does
762 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
763 object is overridden by a regular object. */
766 _bfd_elf_merge_symbol (bfd
*abfd
,
767 struct bfd_link_info
*info
,
769 Elf_Internal_Sym
*sym
,
772 unsigned int *pold_alignment
,
773 struct elf_link_hash_entry
**sym_hash
,
775 bfd_boolean
*override
,
776 bfd_boolean
*type_change_ok
,
777 bfd_boolean
*size_change_ok
)
779 asection
*sec
, *oldsec
;
780 struct elf_link_hash_entry
*h
;
781 struct elf_link_hash_entry
*flip
;
784 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
785 bfd_boolean newweak
, oldweak
;
791 bind
= ELF_ST_BIND (sym
->st_info
);
793 if (! bfd_is_und_section (sec
))
794 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
796 h
= ((struct elf_link_hash_entry
*)
797 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
802 /* This code is for coping with dynamic objects, and is only useful
803 if we are doing an ELF link. */
804 if (info
->hash
->creator
!= abfd
->xvec
)
807 /* For merging, we only care about real symbols. */
809 while (h
->root
.type
== bfd_link_hash_indirect
810 || h
->root
.type
== bfd_link_hash_warning
)
811 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
813 /* If we just created the symbol, mark it as being an ELF symbol.
814 Other than that, there is nothing to do--there is no merge issue
815 with a newly defined symbol--so we just return. */
817 if (h
->root
.type
== bfd_link_hash_new
)
823 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
826 switch (h
->root
.type
)
833 case bfd_link_hash_undefined
:
834 case bfd_link_hash_undefweak
:
835 oldbfd
= h
->root
.u
.undef
.abfd
;
839 case bfd_link_hash_defined
:
840 case bfd_link_hash_defweak
:
841 oldbfd
= h
->root
.u
.def
.section
->owner
;
842 oldsec
= h
->root
.u
.def
.section
;
845 case bfd_link_hash_common
:
846 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
847 oldsec
= h
->root
.u
.c
.p
->section
;
851 /* In cases involving weak versioned symbols, we may wind up trying
852 to merge a symbol with itself. Catch that here, to avoid the
853 confusion that results if we try to override a symbol with
854 itself. The additional tests catch cases like
855 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
856 dynamic object, which we do want to handle here. */
858 && ((abfd
->flags
& DYNAMIC
) == 0
862 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
863 respectively, is from a dynamic object. */
865 if ((abfd
->flags
& DYNAMIC
) != 0)
871 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
876 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
877 indices used by MIPS ELF. */
878 switch (h
->root
.type
)
884 case bfd_link_hash_defined
:
885 case bfd_link_hash_defweak
:
886 hsec
= h
->root
.u
.def
.section
;
889 case bfd_link_hash_common
:
890 hsec
= h
->root
.u
.c
.p
->section
;
897 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
900 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
901 respectively, appear to be a definition rather than reference. */
903 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
908 if (h
->root
.type
== bfd_link_hash_undefined
909 || h
->root
.type
== bfd_link_hash_undefweak
910 || h
->root
.type
== bfd_link_hash_common
)
915 /* Check TLS symbol. */
916 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
917 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
920 bfd_boolean ntdef
, tdef
;
921 asection
*ntsec
, *tsec
;
923 if (h
->type
== STT_TLS
)
943 (*_bfd_error_handler
)
944 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
945 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
946 else if (!tdef
&& !ntdef
)
947 (*_bfd_error_handler
)
948 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
949 tbfd
, ntbfd
, h
->root
.root
.string
);
951 (*_bfd_error_handler
)
952 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
953 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
955 (*_bfd_error_handler
)
956 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
957 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
959 bfd_set_error (bfd_error_bad_value
);
963 /* We need to remember if a symbol has a definition in a dynamic
964 object or is weak in all dynamic objects. Internal and hidden
965 visibility will make it unavailable to dynamic objects. */
966 if (newdyn
&& !h
->dynamic_def
)
968 if (!bfd_is_und_section (sec
))
972 /* Check if this symbol is weak in all dynamic objects. If it
973 is the first time we see it in a dynamic object, we mark
974 if it is weak. Otherwise, we clear it. */
977 if (bind
== STB_WEAK
)
980 else if (bind
!= STB_WEAK
)
985 /* If the old symbol has non-default visibility, we ignore the new
986 definition from a dynamic object. */
988 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
989 && !bfd_is_und_section (sec
))
992 /* Make sure this symbol is dynamic. */
994 /* A protected symbol has external availability. Make sure it is
997 FIXME: Should we check type and size for protected symbol? */
998 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
999 return bfd_elf_link_record_dynamic_symbol (info
, h
);
1004 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1007 /* If the new symbol with non-default visibility comes from a
1008 relocatable file and the old definition comes from a dynamic
1009 object, we remove the old definition. */
1010 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1013 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1014 && bfd_is_und_section (sec
))
1016 /* If the new symbol is undefined and the old symbol was
1017 also undefined before, we need to make sure
1018 _bfd_generic_link_add_one_symbol doesn't mess
1019 up the linker hash table undefs list. Since the old
1020 definition came from a dynamic object, it is still on the
1022 h
->root
.type
= bfd_link_hash_undefined
;
1023 h
->root
.u
.undef
.abfd
= abfd
;
1027 h
->root
.type
= bfd_link_hash_new
;
1028 h
->root
.u
.undef
.abfd
= NULL
;
1037 /* FIXME: Should we check type and size for protected symbol? */
1043 /* Differentiate strong and weak symbols. */
1044 newweak
= bind
== STB_WEAK
;
1045 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1046 || h
->root
.type
== bfd_link_hash_undefweak
);
1048 /* If a new weak symbol definition comes from a regular file and the
1049 old symbol comes from a dynamic library, we treat the new one as
1050 strong. Similarly, an old weak symbol definition from a regular
1051 file is treated as strong when the new symbol comes from a dynamic
1052 library. Further, an old weak symbol from a dynamic library is
1053 treated as strong if the new symbol is from a dynamic library.
1054 This reflects the way glibc's ld.so works.
1056 Do this before setting *type_change_ok or *size_change_ok so that
1057 we warn properly when dynamic library symbols are overridden. */
1059 if (newdef
&& !newdyn
&& olddyn
)
1061 if (olddef
&& newdyn
)
1064 /* It's OK to change the type if either the existing symbol or the
1065 new symbol is weak. A type change is also OK if the old symbol
1066 is undefined and the new symbol is defined. */
1071 && h
->root
.type
== bfd_link_hash_undefined
))
1072 *type_change_ok
= TRUE
;
1074 /* It's OK to change the size if either the existing symbol or the
1075 new symbol is weak, or if the old symbol is undefined. */
1078 || h
->root
.type
== bfd_link_hash_undefined
)
1079 *size_change_ok
= TRUE
;
1081 /* Skip weak definitions of symbols that are already defined. */
1082 if (newdef
&& olddef
&& newweak
&& !oldweak
)
1088 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1089 symbol, respectively, appears to be a common symbol in a dynamic
1090 object. If a symbol appears in an uninitialized section, and is
1091 not weak, and is not a function, then it may be a common symbol
1092 which was resolved when the dynamic object was created. We want
1093 to treat such symbols specially, because they raise special
1094 considerations when setting the symbol size: if the symbol
1095 appears as a common symbol in a regular object, and the size in
1096 the regular object is larger, we must make sure that we use the
1097 larger size. This problematic case can always be avoided in C,
1098 but it must be handled correctly when using Fortran shared
1101 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1102 likewise for OLDDYNCOMMON and OLDDEF.
1104 Note that this test is just a heuristic, and that it is quite
1105 possible to have an uninitialized symbol in a shared object which
1106 is really a definition, rather than a common symbol. This could
1107 lead to some minor confusion when the symbol really is a common
1108 symbol in some regular object. However, I think it will be
1114 && (sec
->flags
& SEC_ALLOC
) != 0
1115 && (sec
->flags
& SEC_LOAD
) == 0
1117 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1118 newdyncommon
= TRUE
;
1120 newdyncommon
= FALSE
;
1124 && h
->root
.type
== bfd_link_hash_defined
1126 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1127 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1129 && h
->type
!= STT_FUNC
)
1130 olddyncommon
= TRUE
;
1132 olddyncommon
= FALSE
;
1134 /* If both the old and the new symbols look like common symbols in a
1135 dynamic object, set the size of the symbol to the larger of the
1140 && sym
->st_size
!= h
->size
)
1142 /* Since we think we have two common symbols, issue a multiple
1143 common warning if desired. Note that we only warn if the
1144 size is different. If the size is the same, we simply let
1145 the old symbol override the new one as normally happens with
1146 symbols defined in dynamic objects. */
1148 if (! ((*info
->callbacks
->multiple_common
)
1149 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1150 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1153 if (sym
->st_size
> h
->size
)
1154 h
->size
= sym
->st_size
;
1156 *size_change_ok
= TRUE
;
1159 /* If we are looking at a dynamic object, and we have found a
1160 definition, we need to see if the symbol was already defined by
1161 some other object. If so, we want to use the existing
1162 definition, and we do not want to report a multiple symbol
1163 definition error; we do this by clobbering *PSEC to be
1164 bfd_und_section_ptr.
1166 We treat a common symbol as a definition if the symbol in the
1167 shared library is a function, since common symbols always
1168 represent variables; this can cause confusion in principle, but
1169 any such confusion would seem to indicate an erroneous program or
1170 shared library. We also permit a common symbol in a regular
1171 object to override a weak symbol in a shared object. */
1176 || (h
->root
.type
== bfd_link_hash_common
1178 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1182 newdyncommon
= FALSE
;
1184 *psec
= sec
= bfd_und_section_ptr
;
1185 *size_change_ok
= TRUE
;
1187 /* If we get here when the old symbol is a common symbol, then
1188 we are explicitly letting it override a weak symbol or
1189 function in a dynamic object, and we don't want to warn about
1190 a type change. If the old symbol is a defined symbol, a type
1191 change warning may still be appropriate. */
1193 if (h
->root
.type
== bfd_link_hash_common
)
1194 *type_change_ok
= TRUE
;
1197 /* Handle the special case of an old common symbol merging with a
1198 new symbol which looks like a common symbol in a shared object.
1199 We change *PSEC and *PVALUE to make the new symbol look like a
1200 common symbol, and let _bfd_generic_link_add_one_symbol will do
1204 && h
->root
.type
== bfd_link_hash_common
)
1208 newdyncommon
= FALSE
;
1209 *pvalue
= sym
->st_size
;
1210 *psec
= sec
= bfd_com_section_ptr
;
1211 *size_change_ok
= TRUE
;
1214 /* If the old symbol is from a dynamic object, and the new symbol is
1215 a definition which is not from a dynamic object, then the new
1216 symbol overrides the old symbol. Symbols from regular files
1217 always take precedence over symbols from dynamic objects, even if
1218 they are defined after the dynamic object in the link.
1220 As above, we again permit a common symbol in a regular object to
1221 override a definition in a shared object if the shared object
1222 symbol is a function or is weak. */
1227 || (bfd_is_com_section (sec
)
1229 || h
->type
== STT_FUNC
)))
1234 /* Change the hash table entry to undefined, and let
1235 _bfd_generic_link_add_one_symbol do the right thing with the
1238 h
->root
.type
= bfd_link_hash_undefined
;
1239 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1240 *size_change_ok
= TRUE
;
1243 olddyncommon
= FALSE
;
1245 /* We again permit a type change when a common symbol may be
1246 overriding a function. */
1248 if (bfd_is_com_section (sec
))
1249 *type_change_ok
= TRUE
;
1251 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1254 /* This union may have been set to be non-NULL when this symbol
1255 was seen in a dynamic object. We must force the union to be
1256 NULL, so that it is correct for a regular symbol. */
1257 h
->verinfo
.vertree
= NULL
;
1260 /* Handle the special case of a new common symbol merging with an
1261 old symbol that looks like it might be a common symbol defined in
1262 a shared object. Note that we have already handled the case in
1263 which a new common symbol should simply override the definition
1264 in the shared library. */
1267 && bfd_is_com_section (sec
)
1270 /* It would be best if we could set the hash table entry to a
1271 common symbol, but we don't know what to use for the section
1272 or the alignment. */
1273 if (! ((*info
->callbacks
->multiple_common
)
1274 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1275 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1278 /* If the presumed common symbol in the dynamic object is
1279 larger, pretend that the new symbol has its size. */
1281 if (h
->size
> *pvalue
)
1284 /* We need to remember the alignment required by the symbol
1285 in the dynamic object. */
1286 BFD_ASSERT (pold_alignment
);
1287 *pold_alignment
= h
->root
.u
.def
.section
->alignment_power
;
1290 olddyncommon
= FALSE
;
1292 h
->root
.type
= bfd_link_hash_undefined
;
1293 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1295 *size_change_ok
= TRUE
;
1296 *type_change_ok
= TRUE
;
1298 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1301 h
->verinfo
.vertree
= NULL
;
1306 /* Handle the case where we had a versioned symbol in a dynamic
1307 library and now find a definition in a normal object. In this
1308 case, we make the versioned symbol point to the normal one. */
1309 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1310 flip
->root
.type
= h
->root
.type
;
1311 h
->root
.type
= bfd_link_hash_indirect
;
1312 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1313 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1314 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1318 flip
->ref_dynamic
= 1;
1325 /* This function is called to create an indirect symbol from the
1326 default for the symbol with the default version if needed. The
1327 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1328 set DYNSYM if the new indirect symbol is dynamic. */
1331 _bfd_elf_add_default_symbol (bfd
*abfd
,
1332 struct bfd_link_info
*info
,
1333 struct elf_link_hash_entry
*h
,
1335 Elf_Internal_Sym
*sym
,
1338 bfd_boolean
*dynsym
,
1339 bfd_boolean override
)
1341 bfd_boolean type_change_ok
;
1342 bfd_boolean size_change_ok
;
1345 struct elf_link_hash_entry
*hi
;
1346 struct bfd_link_hash_entry
*bh
;
1347 const struct elf_backend_data
*bed
;
1348 bfd_boolean collect
;
1349 bfd_boolean dynamic
;
1351 size_t len
, shortlen
;
1354 /* If this symbol has a version, and it is the default version, we
1355 create an indirect symbol from the default name to the fully
1356 decorated name. This will cause external references which do not
1357 specify a version to be bound to this version of the symbol. */
1358 p
= strchr (name
, ELF_VER_CHR
);
1359 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1364 /* We are overridden by an old definition. We need to check if we
1365 need to create the indirect symbol from the default name. */
1366 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1368 BFD_ASSERT (hi
!= NULL
);
1371 while (hi
->root
.type
== bfd_link_hash_indirect
1372 || hi
->root
.type
== bfd_link_hash_warning
)
1374 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1380 bed
= get_elf_backend_data (abfd
);
1381 collect
= bed
->collect
;
1382 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1384 shortlen
= p
- name
;
1385 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1386 if (shortname
== NULL
)
1388 memcpy (shortname
, name
, shortlen
);
1389 shortname
[shortlen
] = '\0';
1391 /* We are going to create a new symbol. Merge it with any existing
1392 symbol with this name. For the purposes of the merge, act as
1393 though we were defining the symbol we just defined, although we
1394 actually going to define an indirect symbol. */
1395 type_change_ok
= FALSE
;
1396 size_change_ok
= FALSE
;
1398 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1399 NULL
, &hi
, &skip
, &override
,
1400 &type_change_ok
, &size_change_ok
))
1409 if (! (_bfd_generic_link_add_one_symbol
1410 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1411 0, name
, FALSE
, collect
, &bh
)))
1413 hi
= (struct elf_link_hash_entry
*) bh
;
1417 /* In this case the symbol named SHORTNAME is overriding the
1418 indirect symbol we want to add. We were planning on making
1419 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1420 is the name without a version. NAME is the fully versioned
1421 name, and it is the default version.
1423 Overriding means that we already saw a definition for the
1424 symbol SHORTNAME in a regular object, and it is overriding
1425 the symbol defined in the dynamic object.
1427 When this happens, we actually want to change NAME, the
1428 symbol we just added, to refer to SHORTNAME. This will cause
1429 references to NAME in the shared object to become references
1430 to SHORTNAME in the regular object. This is what we expect
1431 when we override a function in a shared object: that the
1432 references in the shared object will be mapped to the
1433 definition in the regular object. */
1435 while (hi
->root
.type
== bfd_link_hash_indirect
1436 || hi
->root
.type
== bfd_link_hash_warning
)
1437 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1439 h
->root
.type
= bfd_link_hash_indirect
;
1440 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1444 hi
->ref_dynamic
= 1;
1448 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1453 /* Now set HI to H, so that the following code will set the
1454 other fields correctly. */
1458 /* If there is a duplicate definition somewhere, then HI may not
1459 point to an indirect symbol. We will have reported an error to
1460 the user in that case. */
1462 if (hi
->root
.type
== bfd_link_hash_indirect
)
1464 struct elf_link_hash_entry
*ht
;
1466 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1467 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1469 /* See if the new flags lead us to realize that the symbol must
1481 if (hi
->ref_regular
)
1487 /* We also need to define an indirection from the nondefault version
1491 len
= strlen (name
);
1492 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1493 if (shortname
== NULL
)
1495 memcpy (shortname
, name
, shortlen
);
1496 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1498 /* Once again, merge with any existing symbol. */
1499 type_change_ok
= FALSE
;
1500 size_change_ok
= FALSE
;
1502 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1503 NULL
, &hi
, &skip
, &override
,
1504 &type_change_ok
, &size_change_ok
))
1512 /* Here SHORTNAME is a versioned name, so we don't expect to see
1513 the type of override we do in the case above unless it is
1514 overridden by a versioned definition. */
1515 if (hi
->root
.type
!= bfd_link_hash_defined
1516 && hi
->root
.type
!= bfd_link_hash_defweak
)
1517 (*_bfd_error_handler
)
1518 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1524 if (! (_bfd_generic_link_add_one_symbol
1525 (info
, abfd
, shortname
, BSF_INDIRECT
,
1526 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1528 hi
= (struct elf_link_hash_entry
*) bh
;
1530 /* If there is a duplicate definition somewhere, then HI may not
1531 point to an indirect symbol. We will have reported an error
1532 to the user in that case. */
1534 if (hi
->root
.type
== bfd_link_hash_indirect
)
1536 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1538 /* See if the new flags lead us to realize that the symbol
1550 if (hi
->ref_regular
)
1560 /* This routine is used to export all defined symbols into the dynamic
1561 symbol table. It is called via elf_link_hash_traverse. */
1564 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1566 struct elf_info_failed
*eif
= data
;
1568 /* Ignore indirect symbols. These are added by the versioning code. */
1569 if (h
->root
.type
== bfd_link_hash_indirect
)
1572 if (h
->root
.type
== bfd_link_hash_warning
)
1573 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1575 if (h
->dynindx
== -1
1579 struct bfd_elf_version_tree
*t
;
1580 struct bfd_elf_version_expr
*d
;
1582 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1584 if (t
->globals
.list
!= NULL
)
1586 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1591 if (t
->locals
.list
!= NULL
)
1593 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1602 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1613 /* Look through the symbols which are defined in other shared
1614 libraries and referenced here. Update the list of version
1615 dependencies. This will be put into the .gnu.version_r section.
1616 This function is called via elf_link_hash_traverse. */
1619 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1622 struct elf_find_verdep_info
*rinfo
= data
;
1623 Elf_Internal_Verneed
*t
;
1624 Elf_Internal_Vernaux
*a
;
1627 if (h
->root
.type
== bfd_link_hash_warning
)
1628 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1630 /* We only care about symbols defined in shared objects with version
1635 || h
->verinfo
.verdef
== NULL
)
1638 /* See if we already know about this version. */
1639 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1641 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1644 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1645 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1651 /* This is a new version. Add it to tree we are building. */
1656 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1659 rinfo
->failed
= TRUE
;
1663 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1664 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1665 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1669 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1671 /* Note that we are copying a string pointer here, and testing it
1672 above. If bfd_elf_string_from_elf_section is ever changed to
1673 discard the string data when low in memory, this will have to be
1675 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1677 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1678 a
->vna_nextptr
= t
->vn_auxptr
;
1680 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1683 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1690 /* Figure out appropriate versions for all the symbols. We may not
1691 have the version number script until we have read all of the input
1692 files, so until that point we don't know which symbols should be
1693 local. This function is called via elf_link_hash_traverse. */
1696 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1698 struct elf_assign_sym_version_info
*sinfo
;
1699 struct bfd_link_info
*info
;
1700 const struct elf_backend_data
*bed
;
1701 struct elf_info_failed eif
;
1708 if (h
->root
.type
== bfd_link_hash_warning
)
1709 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1711 /* Fix the symbol flags. */
1714 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1717 sinfo
->failed
= TRUE
;
1721 /* We only need version numbers for symbols defined in regular
1723 if (!h
->def_regular
)
1726 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1727 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1728 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1730 struct bfd_elf_version_tree
*t
;
1735 /* There are two consecutive ELF_VER_CHR characters if this is
1736 not a hidden symbol. */
1738 if (*p
== ELF_VER_CHR
)
1744 /* If there is no version string, we can just return out. */
1752 /* Look for the version. If we find it, it is no longer weak. */
1753 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1755 if (strcmp (t
->name
, p
) == 0)
1759 struct bfd_elf_version_expr
*d
;
1761 len
= p
- h
->root
.root
.string
;
1762 alc
= bfd_malloc (len
);
1765 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1766 alc
[len
- 1] = '\0';
1767 if (alc
[len
- 2] == ELF_VER_CHR
)
1768 alc
[len
- 2] = '\0';
1770 h
->verinfo
.vertree
= t
;
1774 if (t
->globals
.list
!= NULL
)
1775 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1777 /* See if there is anything to force this symbol to
1779 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1781 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1785 && ! info
->export_dynamic
)
1786 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1794 /* If we are building an application, we need to create a
1795 version node for this version. */
1796 if (t
== NULL
&& info
->executable
)
1798 struct bfd_elf_version_tree
**pp
;
1801 /* If we aren't going to export this symbol, we don't need
1802 to worry about it. */
1803 if (h
->dynindx
== -1)
1807 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1810 sinfo
->failed
= TRUE
;
1815 t
->name_indx
= (unsigned int) -1;
1819 /* Don't count anonymous version tag. */
1820 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1822 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1824 t
->vernum
= version_index
;
1828 h
->verinfo
.vertree
= t
;
1832 /* We could not find the version for a symbol when
1833 generating a shared archive. Return an error. */
1834 (*_bfd_error_handler
)
1835 (_("%B: undefined versioned symbol name %s"),
1836 sinfo
->output_bfd
, h
->root
.root
.string
);
1837 bfd_set_error (bfd_error_bad_value
);
1838 sinfo
->failed
= TRUE
;
1846 /* If we don't have a version for this symbol, see if we can find
1848 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1850 struct bfd_elf_version_tree
*t
;
1851 struct bfd_elf_version_tree
*local_ver
;
1852 struct bfd_elf_version_expr
*d
;
1854 /* See if can find what version this symbol is in. If the
1855 symbol is supposed to be local, then don't actually register
1858 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1860 if (t
->globals
.list
!= NULL
)
1862 bfd_boolean matched
;
1866 while ((d
= (*t
->match
) (&t
->globals
, d
,
1867 h
->root
.root
.string
)) != NULL
)
1872 /* There is a version without definition. Make
1873 the symbol the default definition for this
1875 h
->verinfo
.vertree
= t
;
1883 /* There is no undefined version for this symbol. Hide the
1885 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1888 if (t
->locals
.list
!= NULL
)
1891 while ((d
= (*t
->match
) (&t
->locals
, d
,
1892 h
->root
.root
.string
)) != NULL
)
1895 /* If the match is "*", keep looking for a more
1896 explicit, perhaps even global, match.
1897 XXX: Shouldn't this be !d->wildcard instead? */
1898 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1907 if (local_ver
!= NULL
)
1909 h
->verinfo
.vertree
= local_ver
;
1910 if (h
->dynindx
!= -1
1912 && ! info
->export_dynamic
)
1914 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1922 /* Read and swap the relocs from the section indicated by SHDR. This
1923 may be either a REL or a RELA section. The relocations are
1924 translated into RELA relocations and stored in INTERNAL_RELOCS,
1925 which should have already been allocated to contain enough space.
1926 The EXTERNAL_RELOCS are a buffer where the external form of the
1927 relocations should be stored.
1929 Returns FALSE if something goes wrong. */
1932 elf_link_read_relocs_from_section (bfd
*abfd
,
1934 Elf_Internal_Shdr
*shdr
,
1935 void *external_relocs
,
1936 Elf_Internal_Rela
*internal_relocs
)
1938 const struct elf_backend_data
*bed
;
1939 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1940 const bfd_byte
*erela
;
1941 const bfd_byte
*erelaend
;
1942 Elf_Internal_Rela
*irela
;
1943 Elf_Internal_Shdr
*symtab_hdr
;
1946 /* Position ourselves at the start of the section. */
1947 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1950 /* Read the relocations. */
1951 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1954 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1955 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1957 bed
= get_elf_backend_data (abfd
);
1959 /* Convert the external relocations to the internal format. */
1960 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1961 swap_in
= bed
->s
->swap_reloc_in
;
1962 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1963 swap_in
= bed
->s
->swap_reloca_in
;
1966 bfd_set_error (bfd_error_wrong_format
);
1970 erela
= external_relocs
;
1971 erelaend
= erela
+ shdr
->sh_size
;
1972 irela
= internal_relocs
;
1973 while (erela
< erelaend
)
1977 (*swap_in
) (abfd
, erela
, irela
);
1978 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1979 if (bed
->s
->arch_size
== 64)
1981 if ((size_t) r_symndx
>= nsyms
)
1983 (*_bfd_error_handler
)
1984 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1985 " for offset 0x%lx in section `%A'"),
1987 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1988 bfd_set_error (bfd_error_bad_value
);
1991 irela
+= bed
->s
->int_rels_per_ext_rel
;
1992 erela
+= shdr
->sh_entsize
;
1998 /* Read and swap the relocs for a section O. They may have been
1999 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2000 not NULL, they are used as buffers to read into. They are known to
2001 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2002 the return value is allocated using either malloc or bfd_alloc,
2003 according to the KEEP_MEMORY argument. If O has two relocation
2004 sections (both REL and RELA relocations), then the REL_HDR
2005 relocations will appear first in INTERNAL_RELOCS, followed by the
2006 REL_HDR2 relocations. */
2009 _bfd_elf_link_read_relocs (bfd
*abfd
,
2011 void *external_relocs
,
2012 Elf_Internal_Rela
*internal_relocs
,
2013 bfd_boolean keep_memory
)
2015 Elf_Internal_Shdr
*rel_hdr
;
2016 void *alloc1
= NULL
;
2017 Elf_Internal_Rela
*alloc2
= NULL
;
2018 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2020 if (elf_section_data (o
)->relocs
!= NULL
)
2021 return elf_section_data (o
)->relocs
;
2023 if (o
->reloc_count
== 0)
2026 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2028 if (internal_relocs
== NULL
)
2032 size
= o
->reloc_count
;
2033 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2035 internal_relocs
= bfd_alloc (abfd
, size
);
2037 internal_relocs
= alloc2
= bfd_malloc (size
);
2038 if (internal_relocs
== NULL
)
2042 if (external_relocs
== NULL
)
2044 bfd_size_type size
= rel_hdr
->sh_size
;
2046 if (elf_section_data (o
)->rel_hdr2
)
2047 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2048 alloc1
= bfd_malloc (size
);
2051 external_relocs
= alloc1
;
2054 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2058 if (elf_section_data (o
)->rel_hdr2
2059 && (!elf_link_read_relocs_from_section
2061 elf_section_data (o
)->rel_hdr2
,
2062 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2063 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2064 * bed
->s
->int_rels_per_ext_rel
))))
2067 /* Cache the results for next time, if we can. */
2069 elf_section_data (o
)->relocs
= internal_relocs
;
2074 /* Don't free alloc2, since if it was allocated we are passing it
2075 back (under the name of internal_relocs). */
2077 return internal_relocs
;
2087 /* Compute the size of, and allocate space for, REL_HDR which is the
2088 section header for a section containing relocations for O. */
2091 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2092 Elf_Internal_Shdr
*rel_hdr
,
2095 bfd_size_type reloc_count
;
2096 bfd_size_type num_rel_hashes
;
2098 /* Figure out how many relocations there will be. */
2099 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2100 reloc_count
= elf_section_data (o
)->rel_count
;
2102 reloc_count
= elf_section_data (o
)->rel_count2
;
2104 num_rel_hashes
= o
->reloc_count
;
2105 if (num_rel_hashes
< reloc_count
)
2106 num_rel_hashes
= reloc_count
;
2108 /* That allows us to calculate the size of the section. */
2109 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2111 /* The contents field must last into write_object_contents, so we
2112 allocate it with bfd_alloc rather than malloc. Also since we
2113 cannot be sure that the contents will actually be filled in,
2114 we zero the allocated space. */
2115 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2116 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2119 /* We only allocate one set of hash entries, so we only do it the
2120 first time we are called. */
2121 if (elf_section_data (o
)->rel_hashes
== NULL
2124 struct elf_link_hash_entry
**p
;
2126 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2130 elf_section_data (o
)->rel_hashes
= p
;
2136 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2137 originated from the section given by INPUT_REL_HDR) to the
2141 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2142 asection
*input_section
,
2143 Elf_Internal_Shdr
*input_rel_hdr
,
2144 Elf_Internal_Rela
*internal_relocs
)
2146 Elf_Internal_Rela
*irela
;
2147 Elf_Internal_Rela
*irelaend
;
2149 Elf_Internal_Shdr
*output_rel_hdr
;
2150 asection
*output_section
;
2151 unsigned int *rel_countp
= NULL
;
2152 const struct elf_backend_data
*bed
;
2153 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2155 output_section
= input_section
->output_section
;
2156 output_rel_hdr
= NULL
;
2158 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2159 == input_rel_hdr
->sh_entsize
)
2161 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2162 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2164 else if (elf_section_data (output_section
)->rel_hdr2
2165 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2166 == input_rel_hdr
->sh_entsize
))
2168 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2169 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2173 (*_bfd_error_handler
)
2174 (_("%B: relocation size mismatch in %B section %A"),
2175 output_bfd
, input_section
->owner
, input_section
);
2176 bfd_set_error (bfd_error_wrong_object_format
);
2180 bed
= get_elf_backend_data (output_bfd
);
2181 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2182 swap_out
= bed
->s
->swap_reloc_out
;
2183 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2184 swap_out
= bed
->s
->swap_reloca_out
;
2188 erel
= output_rel_hdr
->contents
;
2189 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2190 irela
= internal_relocs
;
2191 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2192 * bed
->s
->int_rels_per_ext_rel
);
2193 while (irela
< irelaend
)
2195 (*swap_out
) (output_bfd
, irela
, erel
);
2196 irela
+= bed
->s
->int_rels_per_ext_rel
;
2197 erel
+= input_rel_hdr
->sh_entsize
;
2200 /* Bump the counter, so that we know where to add the next set of
2202 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2207 /* Fix up the flags for a symbol. This handles various cases which
2208 can only be fixed after all the input files are seen. This is
2209 currently called by both adjust_dynamic_symbol and
2210 assign_sym_version, which is unnecessary but perhaps more robust in
2211 the face of future changes. */
2214 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2215 struct elf_info_failed
*eif
)
2217 /* If this symbol was mentioned in a non-ELF file, try to set
2218 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2219 permit a non-ELF file to correctly refer to a symbol defined in
2220 an ELF dynamic object. */
2223 while (h
->root
.type
== bfd_link_hash_indirect
)
2224 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2226 if (h
->root
.type
!= bfd_link_hash_defined
2227 && h
->root
.type
!= bfd_link_hash_defweak
)
2230 h
->ref_regular_nonweak
= 1;
2234 if (h
->root
.u
.def
.section
->owner
!= NULL
2235 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2236 == bfd_target_elf_flavour
))
2239 h
->ref_regular_nonweak
= 1;
2245 if (h
->dynindx
== -1
2249 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2258 /* Unfortunately, NON_ELF is only correct if the symbol
2259 was first seen in a non-ELF file. Fortunately, if the symbol
2260 was first seen in an ELF file, we're probably OK unless the
2261 symbol was defined in a non-ELF file. Catch that case here.
2262 FIXME: We're still in trouble if the symbol was first seen in
2263 a dynamic object, and then later in a non-ELF regular object. */
2264 if ((h
->root
.type
== bfd_link_hash_defined
2265 || h
->root
.type
== bfd_link_hash_defweak
)
2267 && (h
->root
.u
.def
.section
->owner
!= NULL
2268 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2269 != bfd_target_elf_flavour
)
2270 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2271 && !h
->def_dynamic
)))
2275 /* If this is a final link, and the symbol was defined as a common
2276 symbol in a regular object file, and there was no definition in
2277 any dynamic object, then the linker will have allocated space for
2278 the symbol in a common section but the DEF_REGULAR
2279 flag will not have been set. */
2280 if (h
->root
.type
== bfd_link_hash_defined
2284 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2287 /* If -Bsymbolic was used (which means to bind references to global
2288 symbols to the definition within the shared object), and this
2289 symbol was defined in a regular object, then it actually doesn't
2290 need a PLT entry. Likewise, if the symbol has non-default
2291 visibility. If the symbol has hidden or internal visibility, we
2292 will force it local. */
2294 && eif
->info
->shared
2295 && is_elf_hash_table (eif
->info
->hash
)
2296 && (eif
->info
->symbolic
2297 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2300 const struct elf_backend_data
*bed
;
2301 bfd_boolean force_local
;
2303 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2305 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2306 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2307 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2310 /* If a weak undefined symbol has non-default visibility, we also
2311 hide it from the dynamic linker. */
2312 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2313 && h
->root
.type
== bfd_link_hash_undefweak
)
2315 const struct elf_backend_data
*bed
;
2316 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2317 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2320 /* If this is a weak defined symbol in a dynamic object, and we know
2321 the real definition in the dynamic object, copy interesting flags
2322 over to the real definition. */
2323 if (h
->u
.weakdef
!= NULL
)
2325 struct elf_link_hash_entry
*weakdef
;
2327 weakdef
= h
->u
.weakdef
;
2328 if (h
->root
.type
== bfd_link_hash_indirect
)
2329 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2331 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2332 || h
->root
.type
== bfd_link_hash_defweak
);
2333 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2334 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2335 BFD_ASSERT (weakdef
->def_dynamic
);
2337 /* If the real definition is defined by a regular object file,
2338 don't do anything special. See the longer description in
2339 _bfd_elf_adjust_dynamic_symbol, below. */
2340 if (weakdef
->def_regular
)
2341 h
->u
.weakdef
= NULL
;
2344 const struct elf_backend_data
*bed
;
2346 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2347 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2354 /* Make the backend pick a good value for a dynamic symbol. This is
2355 called via elf_link_hash_traverse, and also calls itself
2359 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2361 struct elf_info_failed
*eif
= data
;
2363 const struct elf_backend_data
*bed
;
2365 if (! is_elf_hash_table (eif
->info
->hash
))
2368 if (h
->root
.type
== bfd_link_hash_warning
)
2370 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2371 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2373 /* When warning symbols are created, they **replace** the "real"
2374 entry in the hash table, thus we never get to see the real
2375 symbol in a hash traversal. So look at it now. */
2376 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2379 /* Ignore indirect symbols. These are added by the versioning code. */
2380 if (h
->root
.type
== bfd_link_hash_indirect
)
2383 /* Fix the symbol flags. */
2384 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2387 /* If this symbol does not require a PLT entry, and it is not
2388 defined by a dynamic object, or is not referenced by a regular
2389 object, ignore it. We do have to handle a weak defined symbol,
2390 even if no regular object refers to it, if we decided to add it
2391 to the dynamic symbol table. FIXME: Do we normally need to worry
2392 about symbols which are defined by one dynamic object and
2393 referenced by another one? */
2398 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2400 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2404 /* If we've already adjusted this symbol, don't do it again. This
2405 can happen via a recursive call. */
2406 if (h
->dynamic_adjusted
)
2409 /* Don't look at this symbol again. Note that we must set this
2410 after checking the above conditions, because we may look at a
2411 symbol once, decide not to do anything, and then get called
2412 recursively later after REF_REGULAR is set below. */
2413 h
->dynamic_adjusted
= 1;
2415 /* If this is a weak definition, and we know a real definition, and
2416 the real symbol is not itself defined by a regular object file,
2417 then get a good value for the real definition. We handle the
2418 real symbol first, for the convenience of the backend routine.
2420 Note that there is a confusing case here. If the real definition
2421 is defined by a regular object file, we don't get the real symbol
2422 from the dynamic object, but we do get the weak symbol. If the
2423 processor backend uses a COPY reloc, then if some routine in the
2424 dynamic object changes the real symbol, we will not see that
2425 change in the corresponding weak symbol. This is the way other
2426 ELF linkers work as well, and seems to be a result of the shared
2429 I will clarify this issue. Most SVR4 shared libraries define the
2430 variable _timezone and define timezone as a weak synonym. The
2431 tzset call changes _timezone. If you write
2432 extern int timezone;
2434 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2435 you might expect that, since timezone is a synonym for _timezone,
2436 the same number will print both times. However, if the processor
2437 backend uses a COPY reloc, then actually timezone will be copied
2438 into your process image, and, since you define _timezone
2439 yourself, _timezone will not. Thus timezone and _timezone will
2440 wind up at different memory locations. The tzset call will set
2441 _timezone, leaving timezone unchanged. */
2443 if (h
->u
.weakdef
!= NULL
)
2445 /* If we get to this point, we know there is an implicit
2446 reference by a regular object file via the weak symbol H.
2447 FIXME: Is this really true? What if the traversal finds
2448 H->U.WEAKDEF before it finds H? */
2449 h
->u
.weakdef
->ref_regular
= 1;
2451 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2455 /* If a symbol has no type and no size and does not require a PLT
2456 entry, then we are probably about to do the wrong thing here: we
2457 are probably going to create a COPY reloc for an empty object.
2458 This case can arise when a shared object is built with assembly
2459 code, and the assembly code fails to set the symbol type. */
2461 && h
->type
== STT_NOTYPE
2463 (*_bfd_error_handler
)
2464 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2465 h
->root
.root
.string
);
2467 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2468 bed
= get_elf_backend_data (dynobj
);
2469 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2478 /* Adjust all external symbols pointing into SEC_MERGE sections
2479 to reflect the object merging within the sections. */
2482 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2486 if (h
->root
.type
== bfd_link_hash_warning
)
2487 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2489 if ((h
->root
.type
== bfd_link_hash_defined
2490 || h
->root
.type
== bfd_link_hash_defweak
)
2491 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2492 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2494 bfd
*output_bfd
= data
;
2496 h
->root
.u
.def
.value
=
2497 _bfd_merged_section_offset (output_bfd
,
2498 &h
->root
.u
.def
.section
,
2499 elf_section_data (sec
)->sec_info
,
2500 h
->root
.u
.def
.value
);
2506 /* Returns false if the symbol referred to by H should be considered
2507 to resolve local to the current module, and true if it should be
2508 considered to bind dynamically. */
2511 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2512 struct bfd_link_info
*info
,
2513 bfd_boolean ignore_protected
)
2515 bfd_boolean binding_stays_local_p
;
2520 while (h
->root
.type
== bfd_link_hash_indirect
2521 || h
->root
.type
== bfd_link_hash_warning
)
2522 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2524 /* If it was forced local, then clearly it's not dynamic. */
2525 if (h
->dynindx
== -1)
2527 if (h
->forced_local
)
2530 /* Identify the cases where name binding rules say that a
2531 visible symbol resolves locally. */
2532 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2534 switch (ELF_ST_VISIBILITY (h
->other
))
2541 /* Proper resolution for function pointer equality may require
2542 that these symbols perhaps be resolved dynamically, even though
2543 we should be resolving them to the current module. */
2544 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2545 binding_stays_local_p
= TRUE
;
2552 /* If it isn't defined locally, then clearly it's dynamic. */
2553 if (!h
->def_regular
)
2556 /* Otherwise, the symbol is dynamic if binding rules don't tell
2557 us that it remains local. */
2558 return !binding_stays_local_p
;
2561 /* Return true if the symbol referred to by H should be considered
2562 to resolve local to the current module, and false otherwise. Differs
2563 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2564 undefined symbols and weak symbols. */
2567 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2568 struct bfd_link_info
*info
,
2569 bfd_boolean local_protected
)
2571 /* If it's a local sym, of course we resolve locally. */
2575 /* Common symbols that become definitions don't get the DEF_REGULAR
2576 flag set, so test it first, and don't bail out. */
2577 if (ELF_COMMON_DEF_P (h
))
2579 /* If we don't have a definition in a regular file, then we can't
2580 resolve locally. The sym is either undefined or dynamic. */
2581 else if (!h
->def_regular
)
2584 /* Forced local symbols resolve locally. */
2585 if (h
->forced_local
)
2588 /* As do non-dynamic symbols. */
2589 if (h
->dynindx
== -1)
2592 /* At this point, we know the symbol is defined and dynamic. In an
2593 executable it must resolve locally, likewise when building symbolic
2594 shared libraries. */
2595 if (info
->executable
|| info
->symbolic
)
2598 /* Now deal with defined dynamic symbols in shared libraries. Ones
2599 with default visibility might not resolve locally. */
2600 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2603 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2604 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2607 /* STV_PROTECTED non-function symbols are local. */
2608 if (h
->type
!= STT_FUNC
)
2611 /* Function pointer equality tests may require that STV_PROTECTED
2612 symbols be treated as dynamic symbols, even when we know that the
2613 dynamic linker will resolve them locally. */
2614 return local_protected
;
2617 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2618 aligned. Returns the first TLS output section. */
2620 struct bfd_section
*
2621 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2623 struct bfd_section
*sec
, *tls
;
2624 unsigned int align
= 0;
2626 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2627 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2631 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2632 if (sec
->alignment_power
> align
)
2633 align
= sec
->alignment_power
;
2635 elf_hash_table (info
)->tls_sec
= tls
;
2637 /* Ensure the alignment of the first section is the largest alignment,
2638 so that the tls segment starts aligned. */
2640 tls
->alignment_power
= align
;
2645 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2647 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2648 Elf_Internal_Sym
*sym
)
2650 /* Local symbols do not count, but target specific ones might. */
2651 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2652 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2655 /* Function symbols do not count. */
2656 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2659 /* If the section is undefined, then so is the symbol. */
2660 if (sym
->st_shndx
== SHN_UNDEF
)
2663 /* If the symbol is defined in the common section, then
2664 it is a common definition and so does not count. */
2665 if (sym
->st_shndx
== SHN_COMMON
)
2668 /* If the symbol is in a target specific section then we
2669 must rely upon the backend to tell us what it is. */
2670 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2671 /* FIXME - this function is not coded yet:
2673 return _bfd_is_global_symbol_definition (abfd, sym);
2675 Instead for now assume that the definition is not global,
2676 Even if this is wrong, at least the linker will behave
2677 in the same way that it used to do. */
2683 /* Search the symbol table of the archive element of the archive ABFD
2684 whose archive map contains a mention of SYMDEF, and determine if
2685 the symbol is defined in this element. */
2687 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2689 Elf_Internal_Shdr
* hdr
;
2690 bfd_size_type symcount
;
2691 bfd_size_type extsymcount
;
2692 bfd_size_type extsymoff
;
2693 Elf_Internal_Sym
*isymbuf
;
2694 Elf_Internal_Sym
*isym
;
2695 Elf_Internal_Sym
*isymend
;
2698 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2702 if (! bfd_check_format (abfd
, bfd_object
))
2705 /* If we have already included the element containing this symbol in the
2706 link then we do not need to include it again. Just claim that any symbol
2707 it contains is not a definition, so that our caller will not decide to
2708 (re)include this element. */
2709 if (abfd
->archive_pass
)
2712 /* Select the appropriate symbol table. */
2713 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2714 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2716 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2718 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2720 /* The sh_info field of the symtab header tells us where the
2721 external symbols start. We don't care about the local symbols. */
2722 if (elf_bad_symtab (abfd
))
2724 extsymcount
= symcount
;
2729 extsymcount
= symcount
- hdr
->sh_info
;
2730 extsymoff
= hdr
->sh_info
;
2733 if (extsymcount
== 0)
2736 /* Read in the symbol table. */
2737 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2739 if (isymbuf
== NULL
)
2742 /* Scan the symbol table looking for SYMDEF. */
2744 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2748 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2753 if (strcmp (name
, symdef
->name
) == 0)
2755 result
= is_global_data_symbol_definition (abfd
, isym
);
2765 /* Add an entry to the .dynamic table. */
2768 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2772 struct elf_link_hash_table
*hash_table
;
2773 const struct elf_backend_data
*bed
;
2775 bfd_size_type newsize
;
2776 bfd_byte
*newcontents
;
2777 Elf_Internal_Dyn dyn
;
2779 hash_table
= elf_hash_table (info
);
2780 if (! is_elf_hash_table (hash_table
))
2783 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2785 (_("warning: creating a DT_TEXTREL in a shared object."));
2787 bed
= get_elf_backend_data (hash_table
->dynobj
);
2788 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2789 BFD_ASSERT (s
!= NULL
);
2791 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2792 newcontents
= bfd_realloc (s
->contents
, newsize
);
2793 if (newcontents
== NULL
)
2797 dyn
.d_un
.d_val
= val
;
2798 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2801 s
->contents
= newcontents
;
2806 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2807 otherwise just check whether one already exists. Returns -1 on error,
2808 1 if a DT_NEEDED tag already exists, and 0 on success. */
2811 elf_add_dt_needed_tag (bfd
*abfd
,
2812 struct bfd_link_info
*info
,
2816 struct elf_link_hash_table
*hash_table
;
2817 bfd_size_type oldsize
;
2818 bfd_size_type strindex
;
2820 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2823 hash_table
= elf_hash_table (info
);
2824 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2825 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2826 if (strindex
== (bfd_size_type
) -1)
2829 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2832 const struct elf_backend_data
*bed
;
2835 bed
= get_elf_backend_data (hash_table
->dynobj
);
2836 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2838 for (extdyn
= sdyn
->contents
;
2839 extdyn
< sdyn
->contents
+ sdyn
->size
;
2840 extdyn
+= bed
->s
->sizeof_dyn
)
2842 Elf_Internal_Dyn dyn
;
2844 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2845 if (dyn
.d_tag
== DT_NEEDED
2846 && dyn
.d_un
.d_val
== strindex
)
2848 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2856 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2859 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2863 /* We were just checking for existence of the tag. */
2864 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2869 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2870 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2871 references from regular objects to these symbols.
2873 ??? Should we do something about references from other dynamic
2874 obects? If not, we potentially lose some warnings about undefined
2875 symbols. But how can we recover the initial undefined / undefweak
2878 struct elf_smash_syms_data
2881 struct elf_link_hash_table
*htab
;
2882 bfd_boolean twiddled
;
2886 elf_smash_syms (struct elf_link_hash_entry
*h
, void *data
)
2888 struct elf_smash_syms_data
*inf
= (struct elf_smash_syms_data
*) data
;
2889 struct bfd_link_hash_entry
*bh
;
2891 switch (h
->root
.type
)
2894 case bfd_link_hash_new
:
2897 case bfd_link_hash_undefined
:
2898 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2900 if (h
->root
.u
.undef
.weak
!= NULL
2901 && h
->root
.u
.undef
.weak
!= inf
->not_needed
)
2903 /* Symbol was undefweak in u.undef.weak bfd, and has become
2904 undefined in as-needed lib. Restore weak. */
2905 h
->root
.type
= bfd_link_hash_undefweak
;
2906 h
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.weak
;
2907 if (h
->root
.u
.undef
.next
!= NULL
2908 || inf
->htab
->root
.undefs_tail
== &h
->root
)
2909 inf
->twiddled
= TRUE
;
2914 case bfd_link_hash_undefweak
:
2915 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2919 case bfd_link_hash_defined
:
2920 case bfd_link_hash_defweak
:
2921 if (h
->root
.u
.def
.section
->owner
!= inf
->not_needed
)
2925 case bfd_link_hash_common
:
2926 if (h
->root
.u
.c
.p
->section
->owner
!= inf
->not_needed
)
2930 case bfd_link_hash_warning
:
2931 case bfd_link_hash_indirect
:
2932 elf_smash_syms ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
);
2933 if (h
->root
.u
.i
.link
->type
!= bfd_link_hash_new
)
2935 if (h
->root
.u
.i
.link
->u
.undef
.abfd
!= inf
->not_needed
)
2940 /* There is no way we can undo symbol table state from defined or
2941 defweak back to undefined. */
2945 /* Set sym back to newly created state, but keep undef.next if it is
2946 being used as a list pointer. */
2947 bh
= h
->root
.u
.undef
.next
;
2950 if (bh
!= NULL
|| inf
->htab
->root
.undefs_tail
== &h
->root
)
2951 inf
->twiddled
= TRUE
;
2952 (*inf
->htab
->root
.table
.newfunc
) (&h
->root
.root
,
2953 &inf
->htab
->root
.table
,
2954 h
->root
.root
.string
);
2955 h
->root
.u
.undef
.next
= bh
;
2956 h
->root
.u
.undef
.abfd
= inf
->not_needed
;
2961 /* Sort symbol by value and section. */
2963 elf_sort_symbol (const void *arg1
, const void *arg2
)
2965 const struct elf_link_hash_entry
*h1
;
2966 const struct elf_link_hash_entry
*h2
;
2967 bfd_signed_vma vdiff
;
2969 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2970 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2971 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2973 return vdiff
> 0 ? 1 : -1;
2976 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2978 return sdiff
> 0 ? 1 : -1;
2983 /* This function is used to adjust offsets into .dynstr for
2984 dynamic symbols. This is called via elf_link_hash_traverse. */
2987 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2989 struct elf_strtab_hash
*dynstr
= data
;
2991 if (h
->root
.type
== bfd_link_hash_warning
)
2992 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2994 if (h
->dynindx
!= -1)
2995 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2999 /* Assign string offsets in .dynstr, update all structures referencing
3003 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
3005 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
3006 struct elf_link_local_dynamic_entry
*entry
;
3007 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
3008 bfd
*dynobj
= hash_table
->dynobj
;
3011 const struct elf_backend_data
*bed
;
3014 _bfd_elf_strtab_finalize (dynstr
);
3015 size
= _bfd_elf_strtab_size (dynstr
);
3017 bed
= get_elf_backend_data (dynobj
);
3018 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3019 BFD_ASSERT (sdyn
!= NULL
);
3021 /* Update all .dynamic entries referencing .dynstr strings. */
3022 for (extdyn
= sdyn
->contents
;
3023 extdyn
< sdyn
->contents
+ sdyn
->size
;
3024 extdyn
+= bed
->s
->sizeof_dyn
)
3026 Elf_Internal_Dyn dyn
;
3028 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3032 dyn
.d_un
.d_val
= size
;
3040 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3045 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3048 /* Now update local dynamic symbols. */
3049 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3050 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3051 entry
->isym
.st_name
);
3053 /* And the rest of dynamic symbols. */
3054 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3056 /* Adjust version definitions. */
3057 if (elf_tdata (output_bfd
)->cverdefs
)
3062 Elf_Internal_Verdef def
;
3063 Elf_Internal_Verdaux defaux
;
3065 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3069 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3071 p
+= sizeof (Elf_External_Verdef
);
3072 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3074 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3076 _bfd_elf_swap_verdaux_in (output_bfd
,
3077 (Elf_External_Verdaux
*) p
, &defaux
);
3078 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3080 _bfd_elf_swap_verdaux_out (output_bfd
,
3081 &defaux
, (Elf_External_Verdaux
*) p
);
3082 p
+= sizeof (Elf_External_Verdaux
);
3085 while (def
.vd_next
);
3088 /* Adjust version references. */
3089 if (elf_tdata (output_bfd
)->verref
)
3094 Elf_Internal_Verneed need
;
3095 Elf_Internal_Vernaux needaux
;
3097 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3101 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3103 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3104 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3105 (Elf_External_Verneed
*) p
);
3106 p
+= sizeof (Elf_External_Verneed
);
3107 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3109 _bfd_elf_swap_vernaux_in (output_bfd
,
3110 (Elf_External_Vernaux
*) p
, &needaux
);
3111 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3113 _bfd_elf_swap_vernaux_out (output_bfd
,
3115 (Elf_External_Vernaux
*) p
);
3116 p
+= sizeof (Elf_External_Vernaux
);
3119 while (need
.vn_next
);
3125 /* Add symbols from an ELF object file to the linker hash table. */
3128 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3130 bfd_boolean (*add_symbol_hook
)
3131 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
3132 const char **, flagword
*, asection
**, bfd_vma
*);
3133 bfd_boolean (*check_relocs
)
3134 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
3135 bfd_boolean (*check_directives
)
3136 (bfd
*, struct bfd_link_info
*);
3137 bfd_boolean collect
;
3138 Elf_Internal_Shdr
*hdr
;
3139 bfd_size_type symcount
;
3140 bfd_size_type extsymcount
;
3141 bfd_size_type extsymoff
;
3142 struct elf_link_hash_entry
**sym_hash
;
3143 bfd_boolean dynamic
;
3144 Elf_External_Versym
*extversym
= NULL
;
3145 Elf_External_Versym
*ever
;
3146 struct elf_link_hash_entry
*weaks
;
3147 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3148 bfd_size_type nondeflt_vers_cnt
= 0;
3149 Elf_Internal_Sym
*isymbuf
= NULL
;
3150 Elf_Internal_Sym
*isym
;
3151 Elf_Internal_Sym
*isymend
;
3152 const struct elf_backend_data
*bed
;
3153 bfd_boolean add_needed
;
3154 struct elf_link_hash_table
* hash_table
;
3157 hash_table
= elf_hash_table (info
);
3159 bed
= get_elf_backend_data (abfd
);
3160 add_symbol_hook
= bed
->elf_add_symbol_hook
;
3161 collect
= bed
->collect
;
3163 if ((abfd
->flags
& DYNAMIC
) == 0)
3169 /* You can't use -r against a dynamic object. Also, there's no
3170 hope of using a dynamic object which does not exactly match
3171 the format of the output file. */
3172 if (info
->relocatable
3173 || !is_elf_hash_table (hash_table
)
3174 || hash_table
->root
.creator
!= abfd
->xvec
)
3176 if (info
->relocatable
)
3177 bfd_set_error (bfd_error_invalid_operation
);
3179 bfd_set_error (bfd_error_wrong_format
);
3184 /* As a GNU extension, any input sections which are named
3185 .gnu.warning.SYMBOL are treated as warning symbols for the given
3186 symbol. This differs from .gnu.warning sections, which generate
3187 warnings when they are included in an output file. */
3188 if (info
->executable
)
3192 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3196 name
= bfd_get_section_name (abfd
, s
);
3197 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3202 name
+= sizeof ".gnu.warning." - 1;
3204 /* If this is a shared object, then look up the symbol
3205 in the hash table. If it is there, and it is already
3206 been defined, then we will not be using the entry
3207 from this shared object, so we don't need to warn.
3208 FIXME: If we see the definition in a regular object
3209 later on, we will warn, but we shouldn't. The only
3210 fix is to keep track of what warnings we are supposed
3211 to emit, and then handle them all at the end of the
3215 struct elf_link_hash_entry
*h
;
3217 h
= elf_link_hash_lookup (hash_table
, name
,
3218 FALSE
, FALSE
, TRUE
);
3220 /* FIXME: What about bfd_link_hash_common? */
3222 && (h
->root
.type
== bfd_link_hash_defined
3223 || h
->root
.type
== bfd_link_hash_defweak
))
3225 /* We don't want to issue this warning. Clobber
3226 the section size so that the warning does not
3227 get copied into the output file. */
3234 msg
= bfd_alloc (abfd
, sz
+ 1);
3238 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3243 if (! (_bfd_generic_link_add_one_symbol
3244 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3245 FALSE
, collect
, NULL
)))
3248 if (! info
->relocatable
)
3250 /* Clobber the section size so that the warning does
3251 not get copied into the output file. */
3254 /* Also set SEC_EXCLUDE, so that symbols defined in
3255 the warning section don't get copied to the output. */
3256 s
->flags
|= SEC_EXCLUDE
;
3265 /* If we are creating a shared library, create all the dynamic
3266 sections immediately. We need to attach them to something,
3267 so we attach them to this BFD, provided it is the right
3268 format. FIXME: If there are no input BFD's of the same
3269 format as the output, we can't make a shared library. */
3271 && is_elf_hash_table (hash_table
)
3272 && hash_table
->root
.creator
== abfd
->xvec
3273 && ! hash_table
->dynamic_sections_created
)
3275 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3279 else if (!is_elf_hash_table (hash_table
))
3284 const char *soname
= NULL
;
3285 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3288 /* ld --just-symbols and dynamic objects don't mix very well.
3289 Test for --just-symbols by looking at info set up by
3290 _bfd_elf_link_just_syms. */
3291 if ((s
= abfd
->sections
) != NULL
3292 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3295 /* If this dynamic lib was specified on the command line with
3296 --as-needed in effect, then we don't want to add a DT_NEEDED
3297 tag unless the lib is actually used. Similary for libs brought
3298 in by another lib's DT_NEEDED. When --no-add-needed is used
3299 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3300 any dynamic library in DT_NEEDED tags in the dynamic lib at
3302 add_needed
= (elf_dyn_lib_class (abfd
)
3303 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3304 | DYN_NO_NEEDED
)) == 0;
3306 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3312 unsigned long shlink
;
3314 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3315 goto error_free_dyn
;
3317 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3319 goto error_free_dyn
;
3320 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3322 for (extdyn
= dynbuf
;
3323 extdyn
< dynbuf
+ s
->size
;
3324 extdyn
+= bed
->s
->sizeof_dyn
)
3326 Elf_Internal_Dyn dyn
;
3328 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3329 if (dyn
.d_tag
== DT_SONAME
)
3331 unsigned int tagv
= dyn
.d_un
.d_val
;
3332 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3334 goto error_free_dyn
;
3336 if (dyn
.d_tag
== DT_NEEDED
)
3338 struct bfd_link_needed_list
*n
, **pn
;
3340 unsigned int tagv
= dyn
.d_un
.d_val
;
3342 amt
= sizeof (struct bfd_link_needed_list
);
3343 n
= bfd_alloc (abfd
, amt
);
3344 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3345 if (n
== NULL
|| fnm
== NULL
)
3346 goto error_free_dyn
;
3347 amt
= strlen (fnm
) + 1;
3348 anm
= bfd_alloc (abfd
, amt
);
3350 goto error_free_dyn
;
3351 memcpy (anm
, fnm
, amt
);
3355 for (pn
= & hash_table
->needed
;
3361 if (dyn
.d_tag
== DT_RUNPATH
)
3363 struct bfd_link_needed_list
*n
, **pn
;
3365 unsigned int tagv
= dyn
.d_un
.d_val
;
3367 amt
= sizeof (struct bfd_link_needed_list
);
3368 n
= bfd_alloc (abfd
, amt
);
3369 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3370 if (n
== NULL
|| fnm
== NULL
)
3371 goto error_free_dyn
;
3372 amt
= strlen (fnm
) + 1;
3373 anm
= bfd_alloc (abfd
, amt
);
3375 goto error_free_dyn
;
3376 memcpy (anm
, fnm
, amt
);
3380 for (pn
= & runpath
;
3386 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3387 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3389 struct bfd_link_needed_list
*n
, **pn
;
3391 unsigned int tagv
= dyn
.d_un
.d_val
;
3393 amt
= sizeof (struct bfd_link_needed_list
);
3394 n
= bfd_alloc (abfd
, amt
);
3395 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3396 if (n
== NULL
|| fnm
== NULL
)
3397 goto error_free_dyn
;
3398 amt
= strlen (fnm
) + 1;
3399 anm
= bfd_alloc (abfd
, amt
);
3406 memcpy (anm
, fnm
, amt
);
3421 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3422 frees all more recently bfd_alloc'd blocks as well. */
3428 struct bfd_link_needed_list
**pn
;
3429 for (pn
= & hash_table
->runpath
;
3436 /* We do not want to include any of the sections in a dynamic
3437 object in the output file. We hack by simply clobbering the
3438 list of sections in the BFD. This could be handled more
3439 cleanly by, say, a new section flag; the existing
3440 SEC_NEVER_LOAD flag is not the one we want, because that one
3441 still implies that the section takes up space in the output
3443 bfd_section_list_clear (abfd
);
3445 /* Find the name to use in a DT_NEEDED entry that refers to this
3446 object. If the object has a DT_SONAME entry, we use it.
3447 Otherwise, if the generic linker stuck something in
3448 elf_dt_name, we use that. Otherwise, we just use the file
3450 if (soname
== NULL
|| *soname
== '\0')
3452 soname
= elf_dt_name (abfd
);
3453 if (soname
== NULL
|| *soname
== '\0')
3454 soname
= bfd_get_filename (abfd
);
3457 /* Save the SONAME because sometimes the linker emulation code
3458 will need to know it. */
3459 elf_dt_name (abfd
) = soname
;
3461 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3465 /* If we have already included this dynamic object in the
3466 link, just ignore it. There is no reason to include a
3467 particular dynamic object more than once. */
3472 /* If this is a dynamic object, we always link against the .dynsym
3473 symbol table, not the .symtab symbol table. The dynamic linker
3474 will only see the .dynsym symbol table, so there is no reason to
3475 look at .symtab for a dynamic object. */
3477 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3478 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3480 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3482 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3484 /* The sh_info field of the symtab header tells us where the
3485 external symbols start. We don't care about the local symbols at
3487 if (elf_bad_symtab (abfd
))
3489 extsymcount
= symcount
;
3494 extsymcount
= symcount
- hdr
->sh_info
;
3495 extsymoff
= hdr
->sh_info
;
3499 if (extsymcount
!= 0)
3501 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3503 if (isymbuf
== NULL
)
3506 /* We store a pointer to the hash table entry for each external
3508 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3509 sym_hash
= bfd_alloc (abfd
, amt
);
3510 if (sym_hash
== NULL
)
3511 goto error_free_sym
;
3512 elf_sym_hashes (abfd
) = sym_hash
;
3517 /* Read in any version definitions. */
3518 if (!_bfd_elf_slurp_version_tables (abfd
,
3519 info
->default_imported_symver
))
3520 goto error_free_sym
;
3522 /* Read in the symbol versions, but don't bother to convert them
3523 to internal format. */
3524 if (elf_dynversym (abfd
) != 0)
3526 Elf_Internal_Shdr
*versymhdr
;
3528 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3529 extversym
= bfd_malloc (versymhdr
->sh_size
);
3530 if (extversym
== NULL
)
3531 goto error_free_sym
;
3532 amt
= versymhdr
->sh_size
;
3533 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3534 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3535 goto error_free_vers
;
3541 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3542 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3544 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3548 asection
*sec
, *new_sec
;
3551 struct elf_link_hash_entry
*h
;
3552 bfd_boolean definition
;
3553 bfd_boolean size_change_ok
;
3554 bfd_boolean type_change_ok
;
3555 bfd_boolean new_weakdef
;
3556 bfd_boolean override
;
3557 unsigned int old_alignment
;
3562 flags
= BSF_NO_FLAGS
;
3564 value
= isym
->st_value
;
3567 bind
= ELF_ST_BIND (isym
->st_info
);
3568 if (bind
== STB_LOCAL
)
3570 /* This should be impossible, since ELF requires that all
3571 global symbols follow all local symbols, and that sh_info
3572 point to the first global symbol. Unfortunately, Irix 5
3576 else if (bind
== STB_GLOBAL
)
3578 if (isym
->st_shndx
!= SHN_UNDEF
3579 && isym
->st_shndx
!= SHN_COMMON
)
3582 else if (bind
== STB_WEAK
)
3586 /* Leave it up to the processor backend. */
3589 if (isym
->st_shndx
== SHN_UNDEF
)
3590 sec
= bfd_und_section_ptr
;
3591 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3593 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3595 sec
= bfd_abs_section_ptr
;
3596 else if (sec
->kept_section
)
3598 /* Symbols from discarded section are undefined, and have
3599 default visibility. */
3600 sec
= bfd_und_section_ptr
;
3601 isym
->st_shndx
= SHN_UNDEF
;
3602 isym
->st_other
= STV_DEFAULT
3603 | (isym
->st_other
& ~ ELF_ST_VISIBILITY(-1));
3605 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3608 else if (isym
->st_shndx
== SHN_ABS
)
3609 sec
= bfd_abs_section_ptr
;
3610 else if (isym
->st_shndx
== SHN_COMMON
)
3612 sec
= bfd_com_section_ptr
;
3613 /* What ELF calls the size we call the value. What ELF
3614 calls the value we call the alignment. */
3615 value
= isym
->st_size
;
3619 /* Leave it up to the processor backend. */
3622 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3625 goto error_free_vers
;
3627 if (isym
->st_shndx
== SHN_COMMON
3628 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3630 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3634 tcomm
= bfd_make_section (abfd
, ".tcommon");
3636 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3638 | SEC_LINKER_CREATED
3639 | SEC_THREAD_LOCAL
)))
3640 goto error_free_vers
;
3644 else if (add_symbol_hook
)
3646 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3648 goto error_free_vers
;
3650 /* The hook function sets the name to NULL if this symbol
3651 should be skipped for some reason. */
3656 /* Sanity check that all possibilities were handled. */
3659 bfd_set_error (bfd_error_bad_value
);
3660 goto error_free_vers
;
3663 if (bfd_is_und_section (sec
)
3664 || bfd_is_com_section (sec
))
3669 size_change_ok
= FALSE
;
3670 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3675 if (is_elf_hash_table (hash_table
))
3677 Elf_Internal_Versym iver
;
3678 unsigned int vernum
= 0;
3683 if (info
->default_imported_symver
)
3684 /* Use the default symbol version created earlier. */
3685 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3690 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3692 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3694 /* If this is a hidden symbol, or if it is not version
3695 1, we append the version name to the symbol name.
3696 However, we do not modify a non-hidden absolute
3697 symbol, because it might be the version symbol
3698 itself. FIXME: What if it isn't? */
3699 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3700 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3703 size_t namelen
, verlen
, newlen
;
3706 if (isym
->st_shndx
!= SHN_UNDEF
)
3708 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3710 else if (vernum
> 1)
3712 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3718 (*_bfd_error_handler
)
3719 (_("%B: %s: invalid version %u (max %d)"),
3721 elf_tdata (abfd
)->cverdefs
);
3722 bfd_set_error (bfd_error_bad_value
);
3723 goto error_free_vers
;
3728 /* We cannot simply test for the number of
3729 entries in the VERNEED section since the
3730 numbers for the needed versions do not start
3732 Elf_Internal_Verneed
*t
;
3735 for (t
= elf_tdata (abfd
)->verref
;
3739 Elf_Internal_Vernaux
*a
;
3741 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3743 if (a
->vna_other
== vernum
)
3745 verstr
= a
->vna_nodename
;
3754 (*_bfd_error_handler
)
3755 (_("%B: %s: invalid needed version %d"),
3756 abfd
, name
, vernum
);
3757 bfd_set_error (bfd_error_bad_value
);
3758 goto error_free_vers
;
3762 namelen
= strlen (name
);
3763 verlen
= strlen (verstr
);
3764 newlen
= namelen
+ verlen
+ 2;
3765 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3766 && isym
->st_shndx
!= SHN_UNDEF
)
3769 newname
= bfd_alloc (abfd
, newlen
);
3770 if (newname
== NULL
)
3771 goto error_free_vers
;
3772 memcpy (newname
, name
, namelen
);
3773 p
= newname
+ namelen
;
3775 /* If this is a defined non-hidden version symbol,
3776 we add another @ to the name. This indicates the
3777 default version of the symbol. */
3778 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3779 && isym
->st_shndx
!= SHN_UNDEF
)
3781 memcpy (p
, verstr
, verlen
+ 1);
3786 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
,
3787 &value
, &old_alignment
,
3788 sym_hash
, &skip
, &override
,
3789 &type_change_ok
, &size_change_ok
))
3790 goto error_free_vers
;
3799 while (h
->root
.type
== bfd_link_hash_indirect
3800 || h
->root
.type
== bfd_link_hash_warning
)
3801 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3803 /* Remember the old alignment if this is a common symbol, so
3804 that we don't reduce the alignment later on. We can't
3805 check later, because _bfd_generic_link_add_one_symbol
3806 will set a default for the alignment which we want to
3807 override. We also remember the old bfd where the existing
3808 definition comes from. */
3809 switch (h
->root
.type
)
3814 case bfd_link_hash_defined
:
3815 case bfd_link_hash_defweak
:
3816 old_bfd
= h
->root
.u
.def
.section
->owner
;
3819 case bfd_link_hash_common
:
3820 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3821 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3825 if (elf_tdata (abfd
)->verdef
!= NULL
3829 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3832 if (! (_bfd_generic_link_add_one_symbol
3833 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3834 (struct bfd_link_hash_entry
**) sym_hash
)))
3835 goto error_free_vers
;
3838 while (h
->root
.type
== bfd_link_hash_indirect
3839 || h
->root
.type
== bfd_link_hash_warning
)
3840 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3843 new_weakdef
= FALSE
;
3846 && (flags
& BSF_WEAK
) != 0
3847 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3848 && is_elf_hash_table (hash_table
)
3849 && h
->u
.weakdef
== NULL
)
3851 /* Keep a list of all weak defined non function symbols from
3852 a dynamic object, using the weakdef field. Later in this
3853 function we will set the weakdef field to the correct
3854 value. We only put non-function symbols from dynamic
3855 objects on this list, because that happens to be the only
3856 time we need to know the normal symbol corresponding to a
3857 weak symbol, and the information is time consuming to
3858 figure out. If the weakdef field is not already NULL,
3859 then this symbol was already defined by some previous
3860 dynamic object, and we will be using that previous
3861 definition anyhow. */
3863 h
->u
.weakdef
= weaks
;
3868 /* Set the alignment of a common symbol. */
3869 if ((isym
->st_shndx
== SHN_COMMON
3870 || bfd_is_com_section (sec
))
3871 && h
->root
.type
== bfd_link_hash_common
)
3875 if (isym
->st_shndx
== SHN_COMMON
)
3876 align
= bfd_log2 (isym
->st_value
);
3879 /* The new symbol is a common symbol in a shared object.
3880 We need to get the alignment from the section. */
3881 align
= new_sec
->alignment_power
;
3883 if (align
> old_alignment
3884 /* Permit an alignment power of zero if an alignment of one
3885 is specified and no other alignments have been specified. */
3886 || (isym
->st_value
== 1 && old_alignment
== 0))
3887 h
->root
.u
.c
.p
->alignment_power
= align
;
3889 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3892 if (is_elf_hash_table (hash_table
))
3896 /* Check the alignment when a common symbol is involved. This
3897 can change when a common symbol is overridden by a normal
3898 definition or a common symbol is ignored due to the old
3899 normal definition. We need to make sure the maximum
3900 alignment is maintained. */
3901 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3902 && h
->root
.type
!= bfd_link_hash_common
)
3904 unsigned int common_align
;
3905 unsigned int normal_align
;
3906 unsigned int symbol_align
;
3910 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3911 if (h
->root
.u
.def
.section
->owner
!= NULL
3912 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3914 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3915 if (normal_align
> symbol_align
)
3916 normal_align
= symbol_align
;
3919 normal_align
= symbol_align
;
3923 common_align
= old_alignment
;
3924 common_bfd
= old_bfd
;
3929 common_align
= bfd_log2 (isym
->st_value
);
3931 normal_bfd
= old_bfd
;
3934 if (normal_align
< common_align
)
3935 (*_bfd_error_handler
)
3936 (_("Warning: alignment %u of symbol `%s' in %B"
3937 " is smaller than %u in %B"),
3938 normal_bfd
, common_bfd
,
3939 1 << normal_align
, name
, 1 << common_align
);
3942 /* Remember the symbol size and type. */
3943 if (isym
->st_size
!= 0
3944 && (definition
|| h
->size
== 0))
3946 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3947 (*_bfd_error_handler
)
3948 (_("Warning: size of symbol `%s' changed"
3949 " from %lu in %B to %lu in %B"),
3951 name
, (unsigned long) h
->size
,
3952 (unsigned long) isym
->st_size
);
3954 h
->size
= isym
->st_size
;
3957 /* If this is a common symbol, then we always want H->SIZE
3958 to be the size of the common symbol. The code just above
3959 won't fix the size if a common symbol becomes larger. We
3960 don't warn about a size change here, because that is
3961 covered by --warn-common. */
3962 if (h
->root
.type
== bfd_link_hash_common
)
3963 h
->size
= h
->root
.u
.c
.size
;
3965 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3966 && (definition
|| h
->type
== STT_NOTYPE
))
3968 if (h
->type
!= STT_NOTYPE
3969 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3970 && ! type_change_ok
)
3971 (*_bfd_error_handler
)
3972 (_("Warning: type of symbol `%s' changed"
3973 " from %d to %d in %B"),
3974 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3976 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3979 /* If st_other has a processor-specific meaning, specific
3980 code might be needed here. We never merge the visibility
3981 attribute with the one from a dynamic object. */
3982 if (bed
->elf_backend_merge_symbol_attribute
)
3983 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3986 /* If this symbol has default visibility and the user has requested
3987 we not re-export it, then mark it as hidden. */
3988 if (definition
&& !dynamic
3990 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3991 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3992 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3994 if (isym
->st_other
!= 0 && !dynamic
)
3996 unsigned char hvis
, symvis
, other
, nvis
;
3998 /* Take the balance of OTHER from the definition. */
3999 other
= (definition
? isym
->st_other
: h
->other
);
4000 other
&= ~ ELF_ST_VISIBILITY (-1);
4002 /* Combine visibilities, using the most constraining one. */
4003 hvis
= ELF_ST_VISIBILITY (h
->other
);
4004 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
4010 nvis
= hvis
< symvis
? hvis
: symvis
;
4012 h
->other
= other
| nvis
;
4015 /* Set a flag in the hash table entry indicating the type of
4016 reference or definition we just found. Keep a count of
4017 the number of dynamic symbols we find. A dynamic symbol
4018 is one which is referenced or defined by both a regular
4019 object and a shared object. */
4026 if (bind
!= STB_WEAK
)
4027 h
->ref_regular_nonweak
= 1;
4031 if (! info
->executable
4044 || (h
->u
.weakdef
!= NULL
4046 && h
->u
.weakdef
->dynindx
!= -1))
4050 /* Check to see if we need to add an indirect symbol for
4051 the default name. */
4052 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4053 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4054 &sec
, &value
, &dynsym
,
4056 goto error_free_vers
;
4058 if (definition
&& !dynamic
)
4060 char *p
= strchr (name
, ELF_VER_CHR
);
4061 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4063 /* Queue non-default versions so that .symver x, x@FOO
4064 aliases can be checked. */
4065 if (! nondeflt_vers
)
4067 amt
= (isymend
- isym
+ 1)
4068 * sizeof (struct elf_link_hash_entry
*);
4069 nondeflt_vers
= bfd_malloc (amt
);
4071 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4075 if (dynsym
&& h
->dynindx
== -1)
4077 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4078 goto error_free_vers
;
4079 if (h
->u
.weakdef
!= NULL
4081 && h
->u
.weakdef
->dynindx
== -1)
4083 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4084 goto error_free_vers
;
4087 else if (dynsym
&& h
->dynindx
!= -1)
4088 /* If the symbol already has a dynamic index, but
4089 visibility says it should not be visible, turn it into
4091 switch (ELF_ST_VISIBILITY (h
->other
))
4095 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4106 const char *soname
= elf_dt_name (abfd
);
4108 /* A symbol from a library loaded via DT_NEEDED of some
4109 other library is referenced by a regular object.
4110 Add a DT_NEEDED entry for it. Issue an error if
4111 --no-add-needed is used. */
4112 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4114 (*_bfd_error_handler
)
4115 (_("%s: invalid DSO for symbol `%s' definition"),
4117 bfd_set_error (bfd_error_bad_value
);
4118 goto error_free_vers
;
4121 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4124 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4126 goto error_free_vers
;
4128 BFD_ASSERT (ret
== 0);
4133 /* Now that all the symbols from this input file are created, handle
4134 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4135 if (nondeflt_vers
!= NULL
)
4137 bfd_size_type cnt
, symidx
;
4139 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4141 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4142 char *shortname
, *p
;
4144 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4146 || (h
->root
.type
!= bfd_link_hash_defined
4147 && h
->root
.type
!= bfd_link_hash_defweak
))
4150 amt
= p
- h
->root
.root
.string
;
4151 shortname
= bfd_malloc (amt
+ 1);
4152 memcpy (shortname
, h
->root
.root
.string
, amt
);
4153 shortname
[amt
] = '\0';
4155 hi
= (struct elf_link_hash_entry
*)
4156 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
4157 FALSE
, FALSE
, FALSE
);
4159 && hi
->root
.type
== h
->root
.type
4160 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4161 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4163 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4164 hi
->root
.type
= bfd_link_hash_indirect
;
4165 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4166 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
4167 sym_hash
= elf_sym_hashes (abfd
);
4169 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4170 if (sym_hash
[symidx
] == hi
)
4172 sym_hash
[symidx
] = h
;
4178 free (nondeflt_vers
);
4179 nondeflt_vers
= NULL
;
4182 if (extversym
!= NULL
)
4188 if (isymbuf
!= NULL
)
4193 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4195 /* Remove symbols defined in an as-needed shared lib that wasn't
4197 struct elf_smash_syms_data inf
;
4198 inf
.not_needed
= abfd
;
4199 inf
.htab
= hash_table
;
4200 inf
.twiddled
= FALSE
;
4201 elf_link_hash_traverse (hash_table
, elf_smash_syms
, &inf
);
4203 bfd_link_repair_undef_list (&hash_table
->root
);
4207 /* Now set the weakdefs field correctly for all the weak defined
4208 symbols we found. The only way to do this is to search all the
4209 symbols. Since we only need the information for non functions in
4210 dynamic objects, that's the only time we actually put anything on
4211 the list WEAKS. We need this information so that if a regular
4212 object refers to a symbol defined weakly in a dynamic object, the
4213 real symbol in the dynamic object is also put in the dynamic
4214 symbols; we also must arrange for both symbols to point to the
4215 same memory location. We could handle the general case of symbol
4216 aliasing, but a general symbol alias can only be generated in
4217 assembler code, handling it correctly would be very time
4218 consuming, and other ELF linkers don't handle general aliasing
4222 struct elf_link_hash_entry
**hpp
;
4223 struct elf_link_hash_entry
**hppend
;
4224 struct elf_link_hash_entry
**sorted_sym_hash
;
4225 struct elf_link_hash_entry
*h
;
4228 /* Since we have to search the whole symbol list for each weak
4229 defined symbol, search time for N weak defined symbols will be
4230 O(N^2). Binary search will cut it down to O(NlogN). */
4231 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4232 sorted_sym_hash
= bfd_malloc (amt
);
4233 if (sorted_sym_hash
== NULL
)
4235 sym_hash
= sorted_sym_hash
;
4236 hpp
= elf_sym_hashes (abfd
);
4237 hppend
= hpp
+ extsymcount
;
4239 for (; hpp
< hppend
; hpp
++)
4243 && h
->root
.type
== bfd_link_hash_defined
4244 && h
->type
!= STT_FUNC
)
4252 qsort (sorted_sym_hash
, sym_count
,
4253 sizeof (struct elf_link_hash_entry
*),
4256 while (weaks
!= NULL
)
4258 struct elf_link_hash_entry
*hlook
;
4265 weaks
= hlook
->u
.weakdef
;
4266 hlook
->u
.weakdef
= NULL
;
4268 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4269 || hlook
->root
.type
== bfd_link_hash_defweak
4270 || hlook
->root
.type
== bfd_link_hash_common
4271 || hlook
->root
.type
== bfd_link_hash_indirect
);
4272 slook
= hlook
->root
.u
.def
.section
;
4273 vlook
= hlook
->root
.u
.def
.value
;
4280 bfd_signed_vma vdiff
;
4282 h
= sorted_sym_hash
[idx
];
4283 vdiff
= vlook
- h
->root
.u
.def
.value
;
4290 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4303 /* We didn't find a value/section match. */
4307 for (i
= ilook
; i
< sym_count
; i
++)
4309 h
= sorted_sym_hash
[i
];
4311 /* Stop if value or section doesn't match. */
4312 if (h
->root
.u
.def
.value
!= vlook
4313 || h
->root
.u
.def
.section
!= slook
)
4315 else if (h
!= hlook
)
4317 hlook
->u
.weakdef
= h
;
4319 /* If the weak definition is in the list of dynamic
4320 symbols, make sure the real definition is put
4322 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4324 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4328 /* If the real definition is in the list of dynamic
4329 symbols, make sure the weak definition is put
4330 there as well. If we don't do this, then the
4331 dynamic loader might not merge the entries for the
4332 real definition and the weak definition. */
4333 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4335 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4343 free (sorted_sym_hash
);
4346 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4347 if (check_directives
)
4348 check_directives (abfd
, info
);
4350 /* If this object is the same format as the output object, and it is
4351 not a shared library, then let the backend look through the
4354 This is required to build global offset table entries and to
4355 arrange for dynamic relocs. It is not required for the
4356 particular common case of linking non PIC code, even when linking
4357 against shared libraries, but unfortunately there is no way of
4358 knowing whether an object file has been compiled PIC or not.
4359 Looking through the relocs is not particularly time consuming.
4360 The problem is that we must either (1) keep the relocs in memory,
4361 which causes the linker to require additional runtime memory or
4362 (2) read the relocs twice from the input file, which wastes time.
4363 This would be a good case for using mmap.
4365 I have no idea how to handle linking PIC code into a file of a
4366 different format. It probably can't be done. */
4367 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4369 && is_elf_hash_table (hash_table
)
4370 && hash_table
->root
.creator
== abfd
->xvec
4371 && check_relocs
!= NULL
)
4375 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4377 Elf_Internal_Rela
*internal_relocs
;
4380 if ((o
->flags
& SEC_RELOC
) == 0
4381 || o
->reloc_count
== 0
4382 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4383 && (o
->flags
& SEC_DEBUGGING
) != 0)
4384 || bfd_is_abs_section (o
->output_section
))
4387 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4389 if (internal_relocs
== NULL
)
4392 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4394 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4395 free (internal_relocs
);
4402 /* If this is a non-traditional link, try to optimize the handling
4403 of the .stab/.stabstr sections. */
4405 && ! info
->traditional_format
4406 && is_elf_hash_table (hash_table
)
4407 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4411 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4412 if (stabstr
!= NULL
)
4414 bfd_size_type string_offset
= 0;
4417 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4418 if (strncmp (".stab", stab
->name
, 5) == 0
4419 && (!stab
->name
[5] ||
4420 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4421 && (stab
->flags
& SEC_MERGE
) == 0
4422 && !bfd_is_abs_section (stab
->output_section
))
4424 struct bfd_elf_section_data
*secdata
;
4426 secdata
= elf_section_data (stab
);
4427 if (! _bfd_link_section_stabs (abfd
,
4428 &hash_table
->stab_info
,
4433 if (secdata
->sec_info
)
4434 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4439 if (is_elf_hash_table (hash_table
) && add_needed
)
4441 /* Add this bfd to the loaded list. */
4442 struct elf_link_loaded_list
*n
;
4444 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4448 n
->next
= hash_table
->loaded
;
4449 hash_table
->loaded
= n
;
4455 if (nondeflt_vers
!= NULL
)
4456 free (nondeflt_vers
);
4457 if (extversym
!= NULL
)
4460 if (isymbuf
!= NULL
)
4466 /* Return the linker hash table entry of a symbol that might be
4467 satisfied by an archive symbol. Return -1 on error. */
4469 struct elf_link_hash_entry
*
4470 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4471 struct bfd_link_info
*info
,
4474 struct elf_link_hash_entry
*h
;
4478 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4482 /* If this is a default version (the name contains @@), look up the
4483 symbol again with only one `@' as well as without the version.
4484 The effect is that references to the symbol with and without the
4485 version will be matched by the default symbol in the archive. */
4487 p
= strchr (name
, ELF_VER_CHR
);
4488 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4491 /* First check with only one `@'. */
4492 len
= strlen (name
);
4493 copy
= bfd_alloc (abfd
, len
);
4495 return (struct elf_link_hash_entry
*) 0 - 1;
4497 first
= p
- name
+ 1;
4498 memcpy (copy
, name
, first
);
4499 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4501 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4504 /* We also need to check references to the symbol without the
4506 copy
[first
- 1] = '\0';
4507 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4508 FALSE
, FALSE
, FALSE
);
4511 bfd_release (abfd
, copy
);
4515 /* Add symbols from an ELF archive file to the linker hash table. We
4516 don't use _bfd_generic_link_add_archive_symbols because of a
4517 problem which arises on UnixWare. The UnixWare libc.so is an
4518 archive which includes an entry libc.so.1 which defines a bunch of
4519 symbols. The libc.so archive also includes a number of other
4520 object files, which also define symbols, some of which are the same
4521 as those defined in libc.so.1. Correct linking requires that we
4522 consider each object file in turn, and include it if it defines any
4523 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4524 this; it looks through the list of undefined symbols, and includes
4525 any object file which defines them. When this algorithm is used on
4526 UnixWare, it winds up pulling in libc.so.1 early and defining a
4527 bunch of symbols. This means that some of the other objects in the
4528 archive are not included in the link, which is incorrect since they
4529 precede libc.so.1 in the archive.
4531 Fortunately, ELF archive handling is simpler than that done by
4532 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4533 oddities. In ELF, if we find a symbol in the archive map, and the
4534 symbol is currently undefined, we know that we must pull in that
4537 Unfortunately, we do have to make multiple passes over the symbol
4538 table until nothing further is resolved. */
4541 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4544 bfd_boolean
*defined
= NULL
;
4545 bfd_boolean
*included
= NULL
;
4549 const struct elf_backend_data
*bed
;
4550 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4551 (bfd
*, struct bfd_link_info
*, const char *);
4553 if (! bfd_has_map (abfd
))
4555 /* An empty archive is a special case. */
4556 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4558 bfd_set_error (bfd_error_no_armap
);
4562 /* Keep track of all symbols we know to be already defined, and all
4563 files we know to be already included. This is to speed up the
4564 second and subsequent passes. */
4565 c
= bfd_ardata (abfd
)->symdef_count
;
4569 amt
*= sizeof (bfd_boolean
);
4570 defined
= bfd_zmalloc (amt
);
4571 included
= bfd_zmalloc (amt
);
4572 if (defined
== NULL
|| included
== NULL
)
4575 symdefs
= bfd_ardata (abfd
)->symdefs
;
4576 bed
= get_elf_backend_data (abfd
);
4577 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4590 symdefend
= symdef
+ c
;
4591 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4593 struct elf_link_hash_entry
*h
;
4595 struct bfd_link_hash_entry
*undefs_tail
;
4598 if (defined
[i
] || included
[i
])
4600 if (symdef
->file_offset
== last
)
4606 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4607 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4613 if (h
->root
.type
== bfd_link_hash_common
)
4615 /* We currently have a common symbol. The archive map contains
4616 a reference to this symbol, so we may want to include it. We
4617 only want to include it however, if this archive element
4618 contains a definition of the symbol, not just another common
4621 Unfortunately some archivers (including GNU ar) will put
4622 declarations of common symbols into their archive maps, as
4623 well as real definitions, so we cannot just go by the archive
4624 map alone. Instead we must read in the element's symbol
4625 table and check that to see what kind of symbol definition
4627 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4630 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4632 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4637 /* We need to include this archive member. */
4638 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4639 if (element
== NULL
)
4642 if (! bfd_check_format (element
, bfd_object
))
4645 /* Doublecheck that we have not included this object
4646 already--it should be impossible, but there may be
4647 something wrong with the archive. */
4648 if (element
->archive_pass
!= 0)
4650 bfd_set_error (bfd_error_bad_value
);
4653 element
->archive_pass
= 1;
4655 undefs_tail
= info
->hash
->undefs_tail
;
4657 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4660 if (! bfd_link_add_symbols (element
, info
))
4663 /* If there are any new undefined symbols, we need to make
4664 another pass through the archive in order to see whether
4665 they can be defined. FIXME: This isn't perfect, because
4666 common symbols wind up on undefs_tail and because an
4667 undefined symbol which is defined later on in this pass
4668 does not require another pass. This isn't a bug, but it
4669 does make the code less efficient than it could be. */
4670 if (undefs_tail
!= info
->hash
->undefs_tail
)
4673 /* Look backward to mark all symbols from this object file
4674 which we have already seen in this pass. */
4678 included
[mark
] = TRUE
;
4683 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4685 /* We mark subsequent symbols from this object file as we go
4686 on through the loop. */
4687 last
= symdef
->file_offset
;
4698 if (defined
!= NULL
)
4700 if (included
!= NULL
)
4705 /* Given an ELF BFD, add symbols to the global hash table as
4709 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4711 switch (bfd_get_format (abfd
))
4714 return elf_link_add_object_symbols (abfd
, info
);
4716 return elf_link_add_archive_symbols (abfd
, info
);
4718 bfd_set_error (bfd_error_wrong_format
);
4723 /* This function will be called though elf_link_hash_traverse to store
4724 all hash value of the exported symbols in an array. */
4727 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4729 unsigned long **valuep
= data
;
4735 if (h
->root
.type
== bfd_link_hash_warning
)
4736 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4738 /* Ignore indirect symbols. These are added by the versioning code. */
4739 if (h
->dynindx
== -1)
4742 name
= h
->root
.root
.string
;
4743 p
= strchr (name
, ELF_VER_CHR
);
4746 alc
= bfd_malloc (p
- name
+ 1);
4747 memcpy (alc
, name
, p
- name
);
4748 alc
[p
- name
] = '\0';
4752 /* Compute the hash value. */
4753 ha
= bfd_elf_hash (name
);
4755 /* Store the found hash value in the array given as the argument. */
4758 /* And store it in the struct so that we can put it in the hash table
4760 h
->u
.elf_hash_value
= ha
;
4768 /* Array used to determine the number of hash table buckets to use
4769 based on the number of symbols there are. If there are fewer than
4770 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4771 fewer than 37 we use 17 buckets, and so forth. We never use more
4772 than 32771 buckets. */
4774 static const size_t elf_buckets
[] =
4776 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4780 /* Compute bucket count for hashing table. We do not use a static set
4781 of possible tables sizes anymore. Instead we determine for all
4782 possible reasonable sizes of the table the outcome (i.e., the
4783 number of collisions etc) and choose the best solution. The
4784 weighting functions are not too simple to allow the table to grow
4785 without bounds. Instead one of the weighting factors is the size.
4786 Therefore the result is always a good payoff between few collisions
4787 (= short chain lengths) and table size. */
4789 compute_bucket_count (struct bfd_link_info
*info
)
4791 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4792 size_t best_size
= 0;
4793 unsigned long int *hashcodes
;
4794 unsigned long int *hashcodesp
;
4795 unsigned long int i
;
4798 /* Compute the hash values for all exported symbols. At the same
4799 time store the values in an array so that we could use them for
4802 amt
*= sizeof (unsigned long int);
4803 hashcodes
= bfd_malloc (amt
);
4804 if (hashcodes
== NULL
)
4806 hashcodesp
= hashcodes
;
4808 /* Put all hash values in HASHCODES. */
4809 elf_link_hash_traverse (elf_hash_table (info
),
4810 elf_collect_hash_codes
, &hashcodesp
);
4812 /* We have a problem here. The following code to optimize the table
4813 size requires an integer type with more the 32 bits. If
4814 BFD_HOST_U_64_BIT is set we know about such a type. */
4815 #ifdef BFD_HOST_U_64_BIT
4818 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4821 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4822 unsigned long int *counts
;
4823 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4824 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4826 /* Possible optimization parameters: if we have NSYMS symbols we say
4827 that the hashing table must at least have NSYMS/4 and at most
4829 minsize
= nsyms
/ 4;
4832 best_size
= maxsize
= nsyms
* 2;
4834 /* Create array where we count the collisions in. We must use bfd_malloc
4835 since the size could be large. */
4837 amt
*= sizeof (unsigned long int);
4838 counts
= bfd_malloc (amt
);
4845 /* Compute the "optimal" size for the hash table. The criteria is a
4846 minimal chain length. The minor criteria is (of course) the size
4848 for (i
= minsize
; i
< maxsize
; ++i
)
4850 /* Walk through the array of hashcodes and count the collisions. */
4851 BFD_HOST_U_64_BIT max
;
4852 unsigned long int j
;
4853 unsigned long int fact
;
4855 memset (counts
, '\0', i
* sizeof (unsigned long int));
4857 /* Determine how often each hash bucket is used. */
4858 for (j
= 0; j
< nsyms
; ++j
)
4859 ++counts
[hashcodes
[j
] % i
];
4861 /* For the weight function we need some information about the
4862 pagesize on the target. This is information need not be 100%
4863 accurate. Since this information is not available (so far) we
4864 define it here to a reasonable default value. If it is crucial
4865 to have a better value some day simply define this value. */
4866 # ifndef BFD_TARGET_PAGESIZE
4867 # define BFD_TARGET_PAGESIZE (4096)
4870 /* We in any case need 2 + NSYMS entries for the size values and
4872 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4875 /* Variant 1: optimize for short chains. We add the squares
4876 of all the chain lengths (which favors many small chain
4877 over a few long chains). */
4878 for (j
= 0; j
< i
; ++j
)
4879 max
+= counts
[j
] * counts
[j
];
4881 /* This adds penalties for the overall size of the table. */
4882 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4885 /* Variant 2: Optimize a lot more for small table. Here we
4886 also add squares of the size but we also add penalties for
4887 empty slots (the +1 term). */
4888 for (j
= 0; j
< i
; ++j
)
4889 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4891 /* The overall size of the table is considered, but not as
4892 strong as in variant 1, where it is squared. */
4893 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4897 /* Compare with current best results. */
4898 if (max
< best_chlen
)
4908 #endif /* defined (BFD_HOST_U_64_BIT) */
4910 /* This is the fallback solution if no 64bit type is available or if we
4911 are not supposed to spend much time on optimizations. We select the
4912 bucket count using a fixed set of numbers. */
4913 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4915 best_size
= elf_buckets
[i
];
4916 if (dynsymcount
< elf_buckets
[i
+ 1])
4921 /* Free the arrays we needed. */
4927 /* Set up the sizes and contents of the ELF dynamic sections. This is
4928 called by the ELF linker emulation before_allocation routine. We
4929 must set the sizes of the sections before the linker sets the
4930 addresses of the various sections. */
4933 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4936 const char *filter_shlib
,
4937 const char * const *auxiliary_filters
,
4938 struct bfd_link_info
*info
,
4939 asection
**sinterpptr
,
4940 struct bfd_elf_version_tree
*verdefs
)
4942 bfd_size_type soname_indx
;
4944 const struct elf_backend_data
*bed
;
4945 struct elf_assign_sym_version_info asvinfo
;
4949 soname_indx
= (bfd_size_type
) -1;
4951 if (!is_elf_hash_table (info
->hash
))
4954 elf_tdata (output_bfd
)->relro
= info
->relro
;
4955 if (info
->execstack
)
4956 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4957 else if (info
->noexecstack
)
4958 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4962 asection
*notesec
= NULL
;
4965 for (inputobj
= info
->input_bfds
;
4967 inputobj
= inputobj
->link_next
)
4971 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4973 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4976 if (s
->flags
& SEC_CODE
)
4985 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4986 if (exec
&& info
->relocatable
4987 && notesec
->output_section
!= bfd_abs_section_ptr
)
4988 notesec
->output_section
->flags
|= SEC_CODE
;
4992 /* Any syms created from now on start with -1 in
4993 got.refcount/offset and plt.refcount/offset. */
4994 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4996 /* The backend may have to create some sections regardless of whether
4997 we're dynamic or not. */
4998 bed
= get_elf_backend_data (output_bfd
);
4999 if (bed
->elf_backend_always_size_sections
5000 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
5003 dynobj
= elf_hash_table (info
)->dynobj
;
5005 /* If there were no dynamic objects in the link, there is nothing to
5010 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
5013 if (elf_hash_table (info
)->dynamic_sections_created
)
5015 struct elf_info_failed eif
;
5016 struct elf_link_hash_entry
*h
;
5018 struct bfd_elf_version_tree
*t
;
5019 struct bfd_elf_version_expr
*d
;
5020 bfd_boolean all_defined
;
5022 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
5023 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
5027 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5029 if (soname_indx
== (bfd_size_type
) -1
5030 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5036 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5038 info
->flags
|= DF_SYMBOLIC
;
5045 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5047 if (indx
== (bfd_size_type
) -1
5048 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5051 if (info
->new_dtags
)
5053 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5054 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5059 if (filter_shlib
!= NULL
)
5063 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5064 filter_shlib
, TRUE
);
5065 if (indx
== (bfd_size_type
) -1
5066 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5070 if (auxiliary_filters
!= NULL
)
5072 const char * const *p
;
5074 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5078 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5080 if (indx
== (bfd_size_type
) -1
5081 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5087 eif
.verdefs
= verdefs
;
5090 /* If we are supposed to export all symbols into the dynamic symbol
5091 table (this is not the normal case), then do so. */
5092 if (info
->export_dynamic
)
5094 elf_link_hash_traverse (elf_hash_table (info
),
5095 _bfd_elf_export_symbol
,
5101 /* Make all global versions with definition. */
5102 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5103 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5104 if (!d
->symver
&& d
->symbol
)
5106 const char *verstr
, *name
;
5107 size_t namelen
, verlen
, newlen
;
5109 struct elf_link_hash_entry
*newh
;
5112 namelen
= strlen (name
);
5114 verlen
= strlen (verstr
);
5115 newlen
= namelen
+ verlen
+ 3;
5117 newname
= bfd_malloc (newlen
);
5118 if (newname
== NULL
)
5120 memcpy (newname
, name
, namelen
);
5122 /* Check the hidden versioned definition. */
5123 p
= newname
+ namelen
;
5125 memcpy (p
, verstr
, verlen
+ 1);
5126 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5127 newname
, FALSE
, FALSE
,
5130 || (newh
->root
.type
!= bfd_link_hash_defined
5131 && newh
->root
.type
!= bfd_link_hash_defweak
))
5133 /* Check the default versioned definition. */
5135 memcpy (p
, verstr
, verlen
+ 1);
5136 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5137 newname
, FALSE
, FALSE
,
5142 /* Mark this version if there is a definition and it is
5143 not defined in a shared object. */
5145 && !newh
->def_dynamic
5146 && (newh
->root
.type
== bfd_link_hash_defined
5147 || newh
->root
.type
== bfd_link_hash_defweak
))
5151 /* Attach all the symbols to their version information. */
5152 asvinfo
.output_bfd
= output_bfd
;
5153 asvinfo
.info
= info
;
5154 asvinfo
.verdefs
= verdefs
;
5155 asvinfo
.failed
= FALSE
;
5157 elf_link_hash_traverse (elf_hash_table (info
),
5158 _bfd_elf_link_assign_sym_version
,
5163 if (!info
->allow_undefined_version
)
5165 /* Check if all global versions have a definition. */
5167 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5168 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5169 if (!d
->symver
&& !d
->script
)
5171 (*_bfd_error_handler
)
5172 (_("%s: undefined version: %s"),
5173 d
->pattern
, t
->name
);
5174 all_defined
= FALSE
;
5179 bfd_set_error (bfd_error_bad_value
);
5184 /* Find all symbols which were defined in a dynamic object and make
5185 the backend pick a reasonable value for them. */
5186 elf_link_hash_traverse (elf_hash_table (info
),
5187 _bfd_elf_adjust_dynamic_symbol
,
5192 /* Add some entries to the .dynamic section. We fill in some of the
5193 values later, in bfd_elf_final_link, but we must add the entries
5194 now so that we know the final size of the .dynamic section. */
5196 /* If there are initialization and/or finalization functions to
5197 call then add the corresponding DT_INIT/DT_FINI entries. */
5198 h
= (info
->init_function
5199 ? elf_link_hash_lookup (elf_hash_table (info
),
5200 info
->init_function
, FALSE
,
5207 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5210 h
= (info
->fini_function
5211 ? elf_link_hash_lookup (elf_hash_table (info
),
5212 info
->fini_function
, FALSE
,
5219 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5223 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5225 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5226 if (! info
->executable
)
5231 for (sub
= info
->input_bfds
; sub
!= NULL
;
5232 sub
= sub
->link_next
)
5233 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5234 if (elf_section_data (o
)->this_hdr
.sh_type
5235 == SHT_PREINIT_ARRAY
)
5237 (*_bfd_error_handler
)
5238 (_("%B: .preinit_array section is not allowed in DSO"),
5243 bfd_set_error (bfd_error_nonrepresentable_section
);
5247 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5248 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5251 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5253 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5254 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5257 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5259 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5260 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5264 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5265 /* If .dynstr is excluded from the link, we don't want any of
5266 these tags. Strictly, we should be checking each section
5267 individually; This quick check covers for the case where
5268 someone does a /DISCARD/ : { *(*) }. */
5269 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5271 bfd_size_type strsize
;
5273 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5274 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5275 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5276 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5277 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5278 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5279 bed
->s
->sizeof_sym
))
5284 /* The backend must work out the sizes of all the other dynamic
5286 if (bed
->elf_backend_size_dynamic_sections
5287 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5290 if (elf_hash_table (info
)->dynamic_sections_created
)
5292 unsigned long section_sym_count
;
5295 /* Set up the version definition section. */
5296 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5297 BFD_ASSERT (s
!= NULL
);
5299 /* We may have created additional version definitions if we are
5300 just linking a regular application. */
5301 verdefs
= asvinfo
.verdefs
;
5303 /* Skip anonymous version tag. */
5304 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5305 verdefs
= verdefs
->next
;
5307 if (verdefs
== NULL
&& !info
->create_default_symver
)
5308 s
->flags
|= SEC_EXCLUDE
;
5313 struct bfd_elf_version_tree
*t
;
5315 Elf_Internal_Verdef def
;
5316 Elf_Internal_Verdaux defaux
;
5317 struct bfd_link_hash_entry
*bh
;
5318 struct elf_link_hash_entry
*h
;
5324 /* Make space for the base version. */
5325 size
+= sizeof (Elf_External_Verdef
);
5326 size
+= sizeof (Elf_External_Verdaux
);
5329 /* Make space for the default version. */
5330 if (info
->create_default_symver
)
5332 size
+= sizeof (Elf_External_Verdef
);
5336 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5338 struct bfd_elf_version_deps
*n
;
5340 size
+= sizeof (Elf_External_Verdef
);
5341 size
+= sizeof (Elf_External_Verdaux
);
5344 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5345 size
+= sizeof (Elf_External_Verdaux
);
5349 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5350 if (s
->contents
== NULL
&& s
->size
!= 0)
5353 /* Fill in the version definition section. */
5357 def
.vd_version
= VER_DEF_CURRENT
;
5358 def
.vd_flags
= VER_FLG_BASE
;
5361 if (info
->create_default_symver
)
5363 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5364 def
.vd_next
= sizeof (Elf_External_Verdef
);
5368 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5369 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5370 + sizeof (Elf_External_Verdaux
));
5373 if (soname_indx
!= (bfd_size_type
) -1)
5375 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5377 def
.vd_hash
= bfd_elf_hash (soname
);
5378 defaux
.vda_name
= soname_indx
;
5385 name
= basename (output_bfd
->filename
);
5386 def
.vd_hash
= bfd_elf_hash (name
);
5387 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5389 if (indx
== (bfd_size_type
) -1)
5391 defaux
.vda_name
= indx
;
5393 defaux
.vda_next
= 0;
5395 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5396 (Elf_External_Verdef
*) p
);
5397 p
+= sizeof (Elf_External_Verdef
);
5398 if (info
->create_default_symver
)
5400 /* Add a symbol representing this version. */
5402 if (! (_bfd_generic_link_add_one_symbol
5403 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5405 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5407 h
= (struct elf_link_hash_entry
*) bh
;
5410 h
->type
= STT_OBJECT
;
5411 h
->verinfo
.vertree
= NULL
;
5413 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5416 /* Create a duplicate of the base version with the same
5417 aux block, but different flags. */
5420 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5422 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5423 + sizeof (Elf_External_Verdaux
));
5426 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5427 (Elf_External_Verdef
*) p
);
5428 p
+= sizeof (Elf_External_Verdef
);
5430 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5431 (Elf_External_Verdaux
*) p
);
5432 p
+= sizeof (Elf_External_Verdaux
);
5434 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5437 struct bfd_elf_version_deps
*n
;
5440 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5443 /* Add a symbol representing this version. */
5445 if (! (_bfd_generic_link_add_one_symbol
5446 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5448 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5450 h
= (struct elf_link_hash_entry
*) bh
;
5453 h
->type
= STT_OBJECT
;
5454 h
->verinfo
.vertree
= t
;
5456 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5459 def
.vd_version
= VER_DEF_CURRENT
;
5461 if (t
->globals
.list
== NULL
5462 && t
->locals
.list
== NULL
5464 def
.vd_flags
|= VER_FLG_WEAK
;
5465 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5466 def
.vd_cnt
= cdeps
+ 1;
5467 def
.vd_hash
= bfd_elf_hash (t
->name
);
5468 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5470 if (t
->next
!= NULL
)
5471 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5472 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5474 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5475 (Elf_External_Verdef
*) p
);
5476 p
+= sizeof (Elf_External_Verdef
);
5478 defaux
.vda_name
= h
->dynstr_index
;
5479 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5481 defaux
.vda_next
= 0;
5482 if (t
->deps
!= NULL
)
5483 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5484 t
->name_indx
= defaux
.vda_name
;
5486 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5487 (Elf_External_Verdaux
*) p
);
5488 p
+= sizeof (Elf_External_Verdaux
);
5490 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5492 if (n
->version_needed
== NULL
)
5494 /* This can happen if there was an error in the
5496 defaux
.vda_name
= 0;
5500 defaux
.vda_name
= n
->version_needed
->name_indx
;
5501 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5504 if (n
->next
== NULL
)
5505 defaux
.vda_next
= 0;
5507 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5509 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5510 (Elf_External_Verdaux
*) p
);
5511 p
+= sizeof (Elf_External_Verdaux
);
5515 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5516 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5519 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5522 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5524 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5527 else if (info
->flags
& DF_BIND_NOW
)
5529 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5535 if (info
->executable
)
5536 info
->flags_1
&= ~ (DF_1_INITFIRST
5539 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5543 /* Work out the size of the version reference section. */
5545 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5546 BFD_ASSERT (s
!= NULL
);
5548 struct elf_find_verdep_info sinfo
;
5550 sinfo
.output_bfd
= output_bfd
;
5552 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5553 if (sinfo
.vers
== 0)
5555 sinfo
.failed
= FALSE
;
5557 elf_link_hash_traverse (elf_hash_table (info
),
5558 _bfd_elf_link_find_version_dependencies
,
5561 if (elf_tdata (output_bfd
)->verref
== NULL
)
5562 s
->flags
|= SEC_EXCLUDE
;
5565 Elf_Internal_Verneed
*t
;
5570 /* Build the version definition section. */
5573 for (t
= elf_tdata (output_bfd
)->verref
;
5577 Elf_Internal_Vernaux
*a
;
5579 size
+= sizeof (Elf_External_Verneed
);
5581 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5582 size
+= sizeof (Elf_External_Vernaux
);
5586 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5587 if (s
->contents
== NULL
)
5591 for (t
= elf_tdata (output_bfd
)->verref
;
5596 Elf_Internal_Vernaux
*a
;
5600 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5603 t
->vn_version
= VER_NEED_CURRENT
;
5605 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5606 elf_dt_name (t
->vn_bfd
) != NULL
5607 ? elf_dt_name (t
->vn_bfd
)
5608 : basename (t
->vn_bfd
->filename
),
5610 if (indx
== (bfd_size_type
) -1)
5613 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5614 if (t
->vn_nextref
== NULL
)
5617 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5618 + caux
* sizeof (Elf_External_Vernaux
));
5620 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5621 (Elf_External_Verneed
*) p
);
5622 p
+= sizeof (Elf_External_Verneed
);
5624 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5626 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5627 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5628 a
->vna_nodename
, FALSE
);
5629 if (indx
== (bfd_size_type
) -1)
5632 if (a
->vna_nextptr
== NULL
)
5635 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5637 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5638 (Elf_External_Vernaux
*) p
);
5639 p
+= sizeof (Elf_External_Vernaux
);
5643 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5644 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5647 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5651 if ((elf_tdata (output_bfd
)->cverrefs
== 0
5652 && elf_tdata (output_bfd
)->cverdefs
== 0)
5653 || _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5654 §ion_sym_count
) == 0)
5656 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5657 s
->flags
|= SEC_EXCLUDE
;
5664 bfd_elf_size_dynsym_hash_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
5666 if (!is_elf_hash_table (info
->hash
))
5669 if (elf_hash_table (info
)->dynamic_sections_created
)
5672 const struct elf_backend_data
*bed
;
5674 bfd_size_type dynsymcount
;
5675 unsigned long section_sym_count
;
5676 size_t bucketcount
= 0;
5677 size_t hash_entry_size
;
5678 unsigned int dtagcount
;
5680 dynobj
= elf_hash_table (info
)->dynobj
;
5682 /* Assign dynsym indicies. In a shared library we generate a
5683 section symbol for each output section, which come first.
5684 Next come all of the back-end allocated local dynamic syms,
5685 followed by the rest of the global symbols. */
5687 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
,
5688 §ion_sym_count
);
5690 /* Work out the size of the symbol version section. */
5691 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5692 BFD_ASSERT (s
!= NULL
);
5693 if (dynsymcount
!= 0
5694 && (s
->flags
& SEC_EXCLUDE
) == 0)
5696 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5697 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5698 if (s
->contents
== NULL
)
5701 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5705 /* Set the size of the .dynsym and .hash sections. We counted
5706 the number of dynamic symbols in elf_link_add_object_symbols.
5707 We will build the contents of .dynsym and .hash when we build
5708 the final symbol table, because until then we do not know the
5709 correct value to give the symbols. We built the .dynstr
5710 section as we went along in elf_link_add_object_symbols. */
5711 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5712 BFD_ASSERT (s
!= NULL
);
5713 bed
= get_elf_backend_data (output_bfd
);
5714 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5716 if (dynsymcount
!= 0)
5718 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5719 if (s
->contents
== NULL
)
5722 /* The first entry in .dynsym is a dummy symbol.
5723 Clear all the section syms, in case we don't output them all. */
5724 ++section_sym_count
;
5725 memset (s
->contents
, 0, section_sym_count
* bed
->s
->sizeof_sym
);
5728 /* Compute the size of the hashing table. As a side effect this
5729 computes the hash values for all the names we export. */
5730 bucketcount
= compute_bucket_count (info
);
5732 s
= bfd_get_section_by_name (dynobj
, ".hash");
5733 BFD_ASSERT (s
!= NULL
);
5734 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5735 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5736 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5737 if (s
->contents
== NULL
)
5740 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5741 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5742 s
->contents
+ hash_entry_size
);
5744 elf_hash_table (info
)->bucketcount
= bucketcount
;
5746 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5747 BFD_ASSERT (s
!= NULL
);
5749 elf_finalize_dynstr (output_bfd
, info
);
5751 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5753 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5754 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5761 /* Final phase of ELF linker. */
5763 /* A structure we use to avoid passing large numbers of arguments. */
5765 struct elf_final_link_info
5767 /* General link information. */
5768 struct bfd_link_info
*info
;
5771 /* Symbol string table. */
5772 struct bfd_strtab_hash
*symstrtab
;
5773 /* .dynsym section. */
5774 asection
*dynsym_sec
;
5775 /* .hash section. */
5777 /* symbol version section (.gnu.version). */
5778 asection
*symver_sec
;
5779 /* Buffer large enough to hold contents of any section. */
5781 /* Buffer large enough to hold external relocs of any section. */
5782 void *external_relocs
;
5783 /* Buffer large enough to hold internal relocs of any section. */
5784 Elf_Internal_Rela
*internal_relocs
;
5785 /* Buffer large enough to hold external local symbols of any input
5787 bfd_byte
*external_syms
;
5788 /* And a buffer for symbol section indices. */
5789 Elf_External_Sym_Shndx
*locsym_shndx
;
5790 /* Buffer large enough to hold internal local symbols of any input
5792 Elf_Internal_Sym
*internal_syms
;
5793 /* Array large enough to hold a symbol index for each local symbol
5794 of any input BFD. */
5796 /* Array large enough to hold a section pointer for each local
5797 symbol of any input BFD. */
5798 asection
**sections
;
5799 /* Buffer to hold swapped out symbols. */
5801 /* And one for symbol section indices. */
5802 Elf_External_Sym_Shndx
*symshndxbuf
;
5803 /* Number of swapped out symbols in buffer. */
5804 size_t symbuf_count
;
5805 /* Number of symbols which fit in symbuf. */
5807 /* And same for symshndxbuf. */
5808 size_t shndxbuf_size
;
5811 /* This struct is used to pass information to elf_link_output_extsym. */
5813 struct elf_outext_info
5816 bfd_boolean localsyms
;
5817 struct elf_final_link_info
*finfo
;
5820 /* When performing a relocatable link, the input relocations are
5821 preserved. But, if they reference global symbols, the indices
5822 referenced must be updated. Update all the relocations in
5823 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5826 elf_link_adjust_relocs (bfd
*abfd
,
5827 Elf_Internal_Shdr
*rel_hdr
,
5829 struct elf_link_hash_entry
**rel_hash
)
5832 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5834 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5835 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5836 bfd_vma r_type_mask
;
5839 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5841 swap_in
= bed
->s
->swap_reloc_in
;
5842 swap_out
= bed
->s
->swap_reloc_out
;
5844 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5846 swap_in
= bed
->s
->swap_reloca_in
;
5847 swap_out
= bed
->s
->swap_reloca_out
;
5852 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5855 if (bed
->s
->arch_size
== 32)
5862 r_type_mask
= 0xffffffff;
5866 erela
= rel_hdr
->contents
;
5867 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5869 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5872 if (*rel_hash
== NULL
)
5875 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5877 (*swap_in
) (abfd
, erela
, irela
);
5878 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5879 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5880 | (irela
[j
].r_info
& r_type_mask
));
5881 (*swap_out
) (abfd
, irela
, erela
);
5885 struct elf_link_sort_rela
5891 enum elf_reloc_type_class type
;
5892 /* We use this as an array of size int_rels_per_ext_rel. */
5893 Elf_Internal_Rela rela
[1];
5897 elf_link_sort_cmp1 (const void *A
, const void *B
)
5899 const struct elf_link_sort_rela
*a
= A
;
5900 const struct elf_link_sort_rela
*b
= B
;
5901 int relativea
, relativeb
;
5903 relativea
= a
->type
== reloc_class_relative
;
5904 relativeb
= b
->type
== reloc_class_relative
;
5906 if (relativea
< relativeb
)
5908 if (relativea
> relativeb
)
5910 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5912 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5914 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5916 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5922 elf_link_sort_cmp2 (const void *A
, const void *B
)
5924 const struct elf_link_sort_rela
*a
= A
;
5925 const struct elf_link_sort_rela
*b
= B
;
5928 if (a
->u
.offset
< b
->u
.offset
)
5930 if (a
->u
.offset
> b
->u
.offset
)
5932 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5933 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5938 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5940 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5946 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5949 bfd_size_type count
, size
;
5950 size_t i
, ret
, sort_elt
, ext_size
;
5951 bfd_byte
*sort
, *s_non_relative
, *p
;
5952 struct elf_link_sort_rela
*sq
;
5953 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5954 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5955 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5956 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5957 struct bfd_link_order
*lo
;
5960 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5961 if (reldyn
== NULL
|| reldyn
->size
== 0)
5963 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5964 if (reldyn
== NULL
|| reldyn
->size
== 0)
5966 ext_size
= bed
->s
->sizeof_rel
;
5967 swap_in
= bed
->s
->swap_reloc_in
;
5968 swap_out
= bed
->s
->swap_reloc_out
;
5972 ext_size
= bed
->s
->sizeof_rela
;
5973 swap_in
= bed
->s
->swap_reloca_in
;
5974 swap_out
= bed
->s
->swap_reloca_out
;
5976 count
= reldyn
->size
/ ext_size
;
5979 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
5980 if (lo
->type
== bfd_indirect_link_order
)
5982 asection
*o
= lo
->u
.indirect
.section
;
5986 if (size
!= reldyn
->size
)
5989 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5990 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5991 sort
= bfd_zmalloc (sort_elt
* count
);
5994 (*info
->callbacks
->warning
)
5995 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5999 if (bed
->s
->arch_size
== 32)
6000 r_sym_mask
= ~(bfd_vma
) 0xff;
6002 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
6004 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6005 if (lo
->type
== bfd_indirect_link_order
)
6007 bfd_byte
*erel
, *erelend
;
6008 asection
*o
= lo
->u
.indirect
.section
;
6010 if (o
->contents
== NULL
&& o
->size
!= 0)
6012 /* This is a reloc section that is being handled as a normal
6013 section. See bfd_section_from_shdr. We can't combine
6014 relocs in this case. */
6019 erelend
= o
->contents
+ o
->size
;
6020 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6021 while (erel
< erelend
)
6023 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6024 (*swap_in
) (abfd
, erel
, s
->rela
);
6025 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
6026 s
->u
.sym_mask
= r_sym_mask
;
6032 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
6034 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
6036 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6037 if (s
->type
!= reloc_class_relative
)
6043 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
6044 for (; i
< count
; i
++, p
+= sort_elt
)
6046 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
6047 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
6049 sp
->u
.offset
= sq
->rela
->r_offset
;
6052 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6054 for (lo
= reldyn
->map_head
.link_order
; lo
!= NULL
; lo
= lo
->next
)
6055 if (lo
->type
== bfd_indirect_link_order
)
6057 bfd_byte
*erel
, *erelend
;
6058 asection
*o
= lo
->u
.indirect
.section
;
6061 erelend
= o
->contents
+ o
->size
;
6062 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6063 while (erel
< erelend
)
6065 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6066 (*swap_out
) (abfd
, s
->rela
, erel
);
6077 /* Flush the output symbols to the file. */
6080 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6081 const struct elf_backend_data
*bed
)
6083 if (finfo
->symbuf_count
> 0)
6085 Elf_Internal_Shdr
*hdr
;
6089 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6090 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6091 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6092 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6093 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6096 hdr
->sh_size
+= amt
;
6097 finfo
->symbuf_count
= 0;
6103 /* Add a symbol to the output symbol table. */
6106 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6108 Elf_Internal_Sym
*elfsym
,
6109 asection
*input_sec
,
6110 struct elf_link_hash_entry
*h
)
6113 Elf_External_Sym_Shndx
*destshndx
;
6114 bfd_boolean (*output_symbol_hook
)
6115 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6116 struct elf_link_hash_entry
*);
6117 const struct elf_backend_data
*bed
;
6119 bed
= get_elf_backend_data (finfo
->output_bfd
);
6120 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6121 if (output_symbol_hook
!= NULL
)
6123 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6127 if (name
== NULL
|| *name
== '\0')
6128 elfsym
->st_name
= 0;
6129 else if (input_sec
->flags
& SEC_EXCLUDE
)
6130 elfsym
->st_name
= 0;
6133 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6135 if (elfsym
->st_name
== (unsigned long) -1)
6139 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6141 if (! elf_link_flush_output_syms (finfo
, bed
))
6145 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6146 destshndx
= finfo
->symshndxbuf
;
6147 if (destshndx
!= NULL
)
6149 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6153 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6154 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6155 if (destshndx
== NULL
)
6157 memset ((char *) destshndx
+ amt
, 0, amt
);
6158 finfo
->shndxbuf_size
*= 2;
6160 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6163 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6164 finfo
->symbuf_count
+= 1;
6165 bfd_get_symcount (finfo
->output_bfd
) += 1;
6170 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6171 allowing an unsatisfied unversioned symbol in the DSO to match a
6172 versioned symbol that would normally require an explicit version.
6173 We also handle the case that a DSO references a hidden symbol
6174 which may be satisfied by a versioned symbol in another DSO. */
6177 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6178 const struct elf_backend_data
*bed
,
6179 struct elf_link_hash_entry
*h
)
6182 struct elf_link_loaded_list
*loaded
;
6184 if (!is_elf_hash_table (info
->hash
))
6187 switch (h
->root
.type
)
6193 case bfd_link_hash_undefined
:
6194 case bfd_link_hash_undefweak
:
6195 abfd
= h
->root
.u
.undef
.abfd
;
6196 if ((abfd
->flags
& DYNAMIC
) == 0
6197 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6201 case bfd_link_hash_defined
:
6202 case bfd_link_hash_defweak
:
6203 abfd
= h
->root
.u
.def
.section
->owner
;
6206 case bfd_link_hash_common
:
6207 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6210 BFD_ASSERT (abfd
!= NULL
);
6212 for (loaded
= elf_hash_table (info
)->loaded
;
6214 loaded
= loaded
->next
)
6217 Elf_Internal_Shdr
*hdr
;
6218 bfd_size_type symcount
;
6219 bfd_size_type extsymcount
;
6220 bfd_size_type extsymoff
;
6221 Elf_Internal_Shdr
*versymhdr
;
6222 Elf_Internal_Sym
*isym
;
6223 Elf_Internal_Sym
*isymend
;
6224 Elf_Internal_Sym
*isymbuf
;
6225 Elf_External_Versym
*ever
;
6226 Elf_External_Versym
*extversym
;
6228 input
= loaded
->abfd
;
6230 /* We check each DSO for a possible hidden versioned definition. */
6232 || (input
->flags
& DYNAMIC
) == 0
6233 || elf_dynversym (input
) == 0)
6236 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6238 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6239 if (elf_bad_symtab (input
))
6241 extsymcount
= symcount
;
6246 extsymcount
= symcount
- hdr
->sh_info
;
6247 extsymoff
= hdr
->sh_info
;
6250 if (extsymcount
== 0)
6253 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6255 if (isymbuf
== NULL
)
6258 /* Read in any version definitions. */
6259 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6260 extversym
= bfd_malloc (versymhdr
->sh_size
);
6261 if (extversym
== NULL
)
6264 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6265 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6266 != versymhdr
->sh_size
))
6274 ever
= extversym
+ extsymoff
;
6275 isymend
= isymbuf
+ extsymcount
;
6276 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6279 Elf_Internal_Versym iver
;
6280 unsigned short version_index
;
6282 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6283 || isym
->st_shndx
== SHN_UNDEF
)
6286 name
= bfd_elf_string_from_elf_section (input
,
6289 if (strcmp (name
, h
->root
.root
.string
) != 0)
6292 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6294 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6296 /* If we have a non-hidden versioned sym, then it should
6297 have provided a definition for the undefined sym. */
6301 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6302 if (version_index
== 1 || version_index
== 2)
6304 /* This is the base or first version. We can use it. */
6318 /* Add an external symbol to the symbol table. This is called from
6319 the hash table traversal routine. When generating a shared object,
6320 we go through the symbol table twice. The first time we output
6321 anything that might have been forced to local scope in a version
6322 script. The second time we output the symbols that are still
6326 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6328 struct elf_outext_info
*eoinfo
= data
;
6329 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6331 Elf_Internal_Sym sym
;
6332 asection
*input_sec
;
6333 const struct elf_backend_data
*bed
;
6335 if (h
->root
.type
== bfd_link_hash_warning
)
6337 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6338 if (h
->root
.type
== bfd_link_hash_new
)
6342 /* Decide whether to output this symbol in this pass. */
6343 if (eoinfo
->localsyms
)
6345 if (!h
->forced_local
)
6350 if (h
->forced_local
)
6354 bed
= get_elf_backend_data (finfo
->output_bfd
);
6356 /* If we have an undefined symbol reference here then it must have
6357 come from a shared library that is being linked in. (Undefined
6358 references in regular files have already been handled). If we
6359 are reporting errors for this situation then do so now. */
6360 if (h
->root
.type
== bfd_link_hash_undefined
6363 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6364 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6366 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6367 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6368 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6370 eoinfo
->failed
= TRUE
;
6375 /* We should also warn if a forced local symbol is referenced from
6376 shared libraries. */
6377 if (! finfo
->info
->relocatable
6378 && (! finfo
->info
->shared
)
6383 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6385 (*_bfd_error_handler
)
6386 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6387 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6388 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6390 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6391 ? "hidden" : "local",
6392 h
->root
.root
.string
);
6393 eoinfo
->failed
= TRUE
;
6397 /* We don't want to output symbols that have never been mentioned by
6398 a regular file, or that we have been told to strip. However, if
6399 h->indx is set to -2, the symbol is used by a reloc and we must
6403 else if ((h
->def_dynamic
6405 || h
->root
.type
== bfd_link_hash_new
)
6409 else if (finfo
->info
->strip
== strip_all
)
6411 else if (finfo
->info
->strip
== strip_some
6412 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6413 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6415 else if (finfo
->info
->strip_discarded
6416 && (h
->root
.type
== bfd_link_hash_defined
6417 || h
->root
.type
== bfd_link_hash_defweak
)
6418 && elf_discarded_section (h
->root
.u
.def
.section
))
6423 /* If we're stripping it, and it's not a dynamic symbol, there's
6424 nothing else to do unless it is a forced local symbol. */
6427 && !h
->forced_local
)
6431 sym
.st_size
= h
->size
;
6432 sym
.st_other
= h
->other
;
6433 if (h
->forced_local
)
6434 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6435 else if (h
->root
.type
== bfd_link_hash_undefweak
6436 || h
->root
.type
== bfd_link_hash_defweak
)
6437 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6439 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6441 switch (h
->root
.type
)
6444 case bfd_link_hash_new
:
6445 case bfd_link_hash_warning
:
6449 case bfd_link_hash_undefined
:
6450 case bfd_link_hash_undefweak
:
6451 input_sec
= bfd_und_section_ptr
;
6452 sym
.st_shndx
= SHN_UNDEF
;
6455 case bfd_link_hash_defined
:
6456 case bfd_link_hash_defweak
:
6458 input_sec
= h
->root
.u
.def
.section
;
6459 if (input_sec
->output_section
!= NULL
)
6462 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6463 input_sec
->output_section
);
6464 if (sym
.st_shndx
== SHN_BAD
)
6466 (*_bfd_error_handler
)
6467 (_("%B: could not find output section %A for input section %A"),
6468 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6469 eoinfo
->failed
= TRUE
;
6473 /* ELF symbols in relocatable files are section relative,
6474 but in nonrelocatable files they are virtual
6476 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6477 if (! finfo
->info
->relocatable
)
6479 sym
.st_value
+= input_sec
->output_section
->vma
;
6480 if (h
->type
== STT_TLS
)
6482 /* STT_TLS symbols are relative to PT_TLS segment
6484 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6485 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6491 BFD_ASSERT (input_sec
->owner
== NULL
6492 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6493 sym
.st_shndx
= SHN_UNDEF
;
6494 input_sec
= bfd_und_section_ptr
;
6499 case bfd_link_hash_common
:
6500 input_sec
= h
->root
.u
.c
.p
->section
;
6501 sym
.st_shndx
= SHN_COMMON
;
6502 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6505 case bfd_link_hash_indirect
:
6506 /* These symbols are created by symbol versioning. They point
6507 to the decorated version of the name. For example, if the
6508 symbol foo@@GNU_1.2 is the default, which should be used when
6509 foo is used with no version, then we add an indirect symbol
6510 foo which points to foo@@GNU_1.2. We ignore these symbols,
6511 since the indirected symbol is already in the hash table. */
6515 /* Give the processor backend a chance to tweak the symbol value,
6516 and also to finish up anything that needs to be done for this
6517 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6518 forced local syms when non-shared is due to a historical quirk. */
6519 if ((h
->dynindx
!= -1
6521 && ((finfo
->info
->shared
6522 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6523 || h
->root
.type
!= bfd_link_hash_undefweak
))
6524 || !h
->forced_local
)
6525 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6527 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6528 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6530 eoinfo
->failed
= TRUE
;
6535 /* If we are marking the symbol as undefined, and there are no
6536 non-weak references to this symbol from a regular object, then
6537 mark the symbol as weak undefined; if there are non-weak
6538 references, mark the symbol as strong. We can't do this earlier,
6539 because it might not be marked as undefined until the
6540 finish_dynamic_symbol routine gets through with it. */
6541 if (sym
.st_shndx
== SHN_UNDEF
6543 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6544 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6548 if (h
->ref_regular_nonweak
)
6549 bindtype
= STB_GLOBAL
;
6551 bindtype
= STB_WEAK
;
6552 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6555 /* If a non-weak symbol with non-default visibility is not defined
6556 locally, it is a fatal error. */
6557 if (! finfo
->info
->relocatable
6558 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6559 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6560 && h
->root
.type
== bfd_link_hash_undefined
6563 (*_bfd_error_handler
)
6564 (_("%B: %s symbol `%s' isn't defined"),
6566 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6568 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6569 ? "internal" : "hidden",
6570 h
->root
.root
.string
);
6571 eoinfo
->failed
= TRUE
;
6575 /* If this symbol should be put in the .dynsym section, then put it
6576 there now. We already know the symbol index. We also fill in
6577 the entry in the .hash section. */
6578 if (h
->dynindx
!= -1
6579 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6583 size_t hash_entry_size
;
6584 bfd_byte
*bucketpos
;
6588 sym
.st_name
= h
->dynstr_index
;
6589 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6590 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6592 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6593 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6595 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6596 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6597 + (bucket
+ 2) * hash_entry_size
);
6598 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6599 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6600 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6601 ((bfd_byte
*) finfo
->hash_sec
->contents
6602 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6604 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6606 Elf_Internal_Versym iversym
;
6607 Elf_External_Versym
*eversym
;
6609 if (!h
->def_regular
)
6611 if (h
->verinfo
.verdef
== NULL
)
6612 iversym
.vs_vers
= 0;
6614 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6618 if (h
->verinfo
.vertree
== NULL
)
6619 iversym
.vs_vers
= 1;
6621 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6622 if (finfo
->info
->create_default_symver
)
6627 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6629 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6630 eversym
+= h
->dynindx
;
6631 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6635 /* If we're stripping it, then it was just a dynamic symbol, and
6636 there's nothing else to do. */
6637 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6640 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6642 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6644 eoinfo
->failed
= TRUE
;
6651 /* Return TRUE if special handling is done for relocs in SEC against
6652 symbols defined in discarded sections. */
6655 elf_section_ignore_discarded_relocs (asection
*sec
)
6657 const struct elf_backend_data
*bed
;
6659 switch (sec
->sec_info_type
)
6661 case ELF_INFO_TYPE_STABS
:
6662 case ELF_INFO_TYPE_EH_FRAME
:
6668 bed
= get_elf_backend_data (sec
->owner
);
6669 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6670 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6676 enum action_discarded
6682 /* Return a mask saying how ld should treat relocations in SEC against
6683 symbols defined in discarded sections. If this function returns
6684 COMPLAIN set, ld will issue a warning message. If this function
6685 returns PRETEND set, and the discarded section was link-once and the
6686 same size as the kept link-once section, ld will pretend that the
6687 symbol was actually defined in the kept section. Otherwise ld will
6688 zero the reloc (at least that is the intent, but some cooperation by
6689 the target dependent code is needed, particularly for REL targets). */
6692 elf_action_discarded (asection
*sec
)
6694 if (sec
->flags
& SEC_DEBUGGING
)
6697 if (strcmp (".eh_frame", sec
->name
) == 0)
6700 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6703 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6706 if (strcmp (".fixup", sec
->name
) == 0)
6709 return COMPLAIN
| PRETEND
;
6712 /* Find a match between a section and a member of a section group. */
6715 match_group_member (asection
*sec
, asection
*group
)
6717 asection
*first
= elf_next_in_group (group
);
6718 asection
*s
= first
;
6722 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6732 /* Check if the kept section of a discarded section SEC can be used
6733 to replace it. Return the replacement if it is OK. Otherwise return
6737 _bfd_elf_check_kept_section (asection
*sec
)
6741 kept
= sec
->kept_section
;
6744 if (elf_sec_group (sec
) != NULL
)
6745 kept
= match_group_member (sec
, kept
);
6746 if (kept
!= NULL
&& sec
->size
!= kept
->size
)
6752 /* Link an input file into the linker output file. This function
6753 handles all the sections and relocations of the input file at once.
6754 This is so that we only have to read the local symbols once, and
6755 don't have to keep them in memory. */
6758 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6760 bfd_boolean (*relocate_section
)
6761 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6762 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6764 Elf_Internal_Shdr
*symtab_hdr
;
6767 Elf_Internal_Sym
*isymbuf
;
6768 Elf_Internal_Sym
*isym
;
6769 Elf_Internal_Sym
*isymend
;
6771 asection
**ppsection
;
6773 const struct elf_backend_data
*bed
;
6774 bfd_boolean emit_relocs
;
6775 struct elf_link_hash_entry
**sym_hashes
;
6777 output_bfd
= finfo
->output_bfd
;
6778 bed
= get_elf_backend_data (output_bfd
);
6779 relocate_section
= bed
->elf_backend_relocate_section
;
6781 /* If this is a dynamic object, we don't want to do anything here:
6782 we don't want the local symbols, and we don't want the section
6784 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6787 emit_relocs
= (finfo
->info
->relocatable
6788 || finfo
->info
->emitrelocations
6789 || bed
->elf_backend_emit_relocs
);
6791 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6792 if (elf_bad_symtab (input_bfd
))
6794 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6799 locsymcount
= symtab_hdr
->sh_info
;
6800 extsymoff
= symtab_hdr
->sh_info
;
6803 /* Read the local symbols. */
6804 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6805 if (isymbuf
== NULL
&& locsymcount
!= 0)
6807 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6808 finfo
->internal_syms
,
6809 finfo
->external_syms
,
6810 finfo
->locsym_shndx
);
6811 if (isymbuf
== NULL
)
6815 /* Find local symbol sections and adjust values of symbols in
6816 SEC_MERGE sections. Write out those local symbols we know are
6817 going into the output file. */
6818 isymend
= isymbuf
+ locsymcount
;
6819 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6821 isym
++, pindex
++, ppsection
++)
6825 Elf_Internal_Sym osym
;
6829 if (elf_bad_symtab (input_bfd
))
6831 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6838 if (isym
->st_shndx
== SHN_UNDEF
)
6839 isec
= bfd_und_section_ptr
;
6840 else if (isym
->st_shndx
< SHN_LORESERVE
6841 || isym
->st_shndx
> SHN_HIRESERVE
)
6843 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6845 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6846 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6848 _bfd_merged_section_offset (output_bfd
, &isec
,
6849 elf_section_data (isec
)->sec_info
,
6852 else if (isym
->st_shndx
== SHN_ABS
)
6853 isec
= bfd_abs_section_ptr
;
6854 else if (isym
->st_shndx
== SHN_COMMON
)
6855 isec
= bfd_com_section_ptr
;
6864 /* Don't output the first, undefined, symbol. */
6865 if (ppsection
== finfo
->sections
)
6868 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6870 /* We never output section symbols. Instead, we use the
6871 section symbol of the corresponding section in the output
6876 /* If we are stripping all symbols, we don't want to output this
6878 if (finfo
->info
->strip
== strip_all
)
6881 /* If we are discarding all local symbols, we don't want to
6882 output this one. If we are generating a relocatable output
6883 file, then some of the local symbols may be required by
6884 relocs; we output them below as we discover that they are
6886 if (finfo
->info
->discard
== discard_all
)
6889 /* If this symbol is defined in a section which we are
6890 discarding, we don't need to keep it, but note that
6891 linker_mark is only reliable for sections that have contents.
6892 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6893 as well as linker_mark. */
6894 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6896 || (! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6897 || (! finfo
->info
->relocatable
6898 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6901 /* If the section is not in the output BFD's section list, it is not
6903 if (bfd_section_removed_from_list (output_bfd
, isec
->output_section
))
6906 /* Get the name of the symbol. */
6907 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6912 /* See if we are discarding symbols with this name. */
6913 if ((finfo
->info
->strip
== strip_some
6914 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6916 || (((finfo
->info
->discard
== discard_sec_merge
6917 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6918 || finfo
->info
->discard
== discard_l
)
6919 && bfd_is_local_label_name (input_bfd
, name
)))
6922 /* If we get here, we are going to output this symbol. */
6926 /* Adjust the section index for the output file. */
6927 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6928 isec
->output_section
);
6929 if (osym
.st_shndx
== SHN_BAD
)
6932 *pindex
= bfd_get_symcount (output_bfd
);
6934 /* ELF symbols in relocatable files are section relative, but
6935 in executable files they are virtual addresses. Note that
6936 this code assumes that all ELF sections have an associated
6937 BFD section with a reasonable value for output_offset; below
6938 we assume that they also have a reasonable value for
6939 output_section. Any special sections must be set up to meet
6940 these requirements. */
6941 osym
.st_value
+= isec
->output_offset
;
6942 if (! finfo
->info
->relocatable
)
6944 osym
.st_value
+= isec
->output_section
->vma
;
6945 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6947 /* STT_TLS symbols are relative to PT_TLS segment base. */
6948 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6949 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6953 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6957 /* Relocate the contents of each section. */
6958 sym_hashes
= elf_sym_hashes (input_bfd
);
6959 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6963 if (! o
->linker_mark
)
6965 /* This section was omitted from the link. */
6969 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6970 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6973 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6975 /* Section was created by _bfd_elf_link_create_dynamic_sections
6980 /* Get the contents of the section. They have been cached by a
6981 relaxation routine. Note that o is a section in an input
6982 file, so the contents field will not have been set by any of
6983 the routines which work on output files. */
6984 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6985 contents
= elf_section_data (o
)->this_hdr
.contents
;
6988 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6990 contents
= finfo
->contents
;
6991 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6995 if ((o
->flags
& SEC_RELOC
) != 0)
6997 Elf_Internal_Rela
*internal_relocs
;
6998 bfd_vma r_type_mask
;
7001 /* Get the swapped relocs. */
7003 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
7004 finfo
->internal_relocs
, FALSE
);
7005 if (internal_relocs
== NULL
7006 && o
->reloc_count
> 0)
7009 if (bed
->s
->arch_size
== 32)
7016 r_type_mask
= 0xffffffff;
7020 /* Run through the relocs looking for any against symbols
7021 from discarded sections and section symbols from
7022 removed link-once sections. Complain about relocs
7023 against discarded sections. Zero relocs against removed
7024 link-once sections. Preserve debug information as much
7026 if (!elf_section_ignore_discarded_relocs (o
))
7028 Elf_Internal_Rela
*rel
, *relend
;
7029 unsigned int action
= elf_action_discarded (o
);
7031 rel
= internal_relocs
;
7032 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7033 for ( ; rel
< relend
; rel
++)
7035 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
7036 asection
**ps
, *sec
;
7037 struct elf_link_hash_entry
*h
= NULL
;
7038 const char *sym_name
;
7040 if (r_symndx
== STN_UNDEF
)
7043 if (r_symndx
>= locsymcount
7044 || (elf_bad_symtab (input_bfd
)
7045 && finfo
->sections
[r_symndx
] == NULL
))
7047 h
= sym_hashes
[r_symndx
- extsymoff
];
7049 /* Badly formatted input files can contain relocs that
7050 reference non-existant symbols. Check here so that
7051 we do not seg fault. */
7056 sprintf_vma (buffer
, rel
->r_info
);
7057 (*_bfd_error_handler
)
7058 (_("error: %B contains a reloc (0x%s) for section %A "
7059 "that references a non-existent global symbol"),
7060 input_bfd
, o
, buffer
);
7061 bfd_set_error (bfd_error_bad_value
);
7065 while (h
->root
.type
== bfd_link_hash_indirect
7066 || h
->root
.type
== bfd_link_hash_warning
)
7067 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
7069 if (h
->root
.type
!= bfd_link_hash_defined
7070 && h
->root
.type
!= bfd_link_hash_defweak
)
7073 ps
= &h
->root
.u
.def
.section
;
7074 sym_name
= h
->root
.root
.string
;
7078 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
7079 ps
= &finfo
->sections
[r_symndx
];
7080 sym_name
= bfd_elf_sym_name (input_bfd
,
7085 /* Complain if the definition comes from a
7086 discarded section. */
7087 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
7089 BFD_ASSERT (r_symndx
!= 0);
7090 if (action
& COMPLAIN
)
7092 (*_bfd_error_handler
)
7093 (_("`%s' referenced in section `%A' of %B: "
7094 "defined in discarded section `%A' of %B"),
7095 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
7096 bfd_set_error (bfd_error_bad_value
);
7100 /* Try to do the best we can to support buggy old
7101 versions of gcc. If we've warned, or this is
7102 debugging info, pretend that the symbol is
7103 really defined in the kept linkonce section.
7104 FIXME: This is quite broken. Modifying the
7105 symbol here means we will be changing all later
7106 uses of the symbol, not just in this section.
7107 The only thing that makes this half reasonable
7108 is that we warn in non-debug sections, and
7109 debug sections tend to come after other
7111 if (action
& PRETEND
)
7115 kept
= _bfd_elf_check_kept_section (sec
);
7123 /* Remove the symbol reference from the reloc, but
7124 don't kill the reloc completely. This is so that
7125 a zero value will be written into the section,
7126 which may have non-zero contents put there by the
7127 assembler. Zero in things like an eh_frame fde
7128 pc_begin allows stack unwinders to recognize the
7130 rel
->r_info
&= r_type_mask
;
7136 /* Relocate the section by invoking a back end routine.
7138 The back end routine is responsible for adjusting the
7139 section contents as necessary, and (if using Rela relocs
7140 and generating a relocatable output file) adjusting the
7141 reloc addend as necessary.
7143 The back end routine does not have to worry about setting
7144 the reloc address or the reloc symbol index.
7146 The back end routine is given a pointer to the swapped in
7147 internal symbols, and can access the hash table entries
7148 for the external symbols via elf_sym_hashes (input_bfd).
7150 When generating relocatable output, the back end routine
7151 must handle STB_LOCAL/STT_SECTION symbols specially. The
7152 output symbol is going to be a section symbol
7153 corresponding to the output section, which will require
7154 the addend to be adjusted. */
7156 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7157 input_bfd
, o
, contents
,
7165 Elf_Internal_Rela
*irela
;
7166 Elf_Internal_Rela
*irelaend
;
7167 bfd_vma last_offset
;
7168 struct elf_link_hash_entry
**rel_hash
;
7169 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7170 unsigned int next_erel
;
7171 bfd_boolean (*reloc_emitter
)
7172 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
7173 bfd_boolean rela_normal
;
7175 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7176 rela_normal
= (bed
->rela_normal
7177 && (input_rel_hdr
->sh_entsize
7178 == bed
->s
->sizeof_rela
));
7180 /* Adjust the reloc addresses and symbol indices. */
7182 irela
= internal_relocs
;
7183 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7184 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7185 + elf_section_data (o
->output_section
)->rel_count
7186 + elf_section_data (o
->output_section
)->rel_count2
);
7187 last_offset
= o
->output_offset
;
7188 if (!finfo
->info
->relocatable
)
7189 last_offset
+= o
->output_section
->vma
;
7190 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7192 unsigned long r_symndx
;
7194 Elf_Internal_Sym sym
;
7196 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7202 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7205 if (irela
->r_offset
>= (bfd_vma
) -2)
7207 /* This is a reloc for a deleted entry or somesuch.
7208 Turn it into an R_*_NONE reloc, at the same
7209 offset as the last reloc. elf_eh_frame.c and
7210 elf_bfd_discard_info rely on reloc offsets
7212 irela
->r_offset
= last_offset
;
7214 irela
->r_addend
= 0;
7218 irela
->r_offset
+= o
->output_offset
;
7220 /* Relocs in an executable have to be virtual addresses. */
7221 if (!finfo
->info
->relocatable
)
7222 irela
->r_offset
+= o
->output_section
->vma
;
7224 last_offset
= irela
->r_offset
;
7226 r_symndx
= irela
->r_info
>> r_sym_shift
;
7227 if (r_symndx
== STN_UNDEF
)
7230 if (r_symndx
>= locsymcount
7231 || (elf_bad_symtab (input_bfd
)
7232 && finfo
->sections
[r_symndx
] == NULL
))
7234 struct elf_link_hash_entry
*rh
;
7237 /* This is a reloc against a global symbol. We
7238 have not yet output all the local symbols, so
7239 we do not know the symbol index of any global
7240 symbol. We set the rel_hash entry for this
7241 reloc to point to the global hash table entry
7242 for this symbol. The symbol index is then
7243 set at the end of bfd_elf_final_link. */
7244 indx
= r_symndx
- extsymoff
;
7245 rh
= elf_sym_hashes (input_bfd
)[indx
];
7246 while (rh
->root
.type
== bfd_link_hash_indirect
7247 || rh
->root
.type
== bfd_link_hash_warning
)
7248 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7250 /* Setting the index to -2 tells
7251 elf_link_output_extsym that this symbol is
7253 BFD_ASSERT (rh
->indx
< 0);
7261 /* This is a reloc against a local symbol. */
7264 sym
= isymbuf
[r_symndx
];
7265 sec
= finfo
->sections
[r_symndx
];
7266 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7268 /* I suppose the backend ought to fill in the
7269 section of any STT_SECTION symbol against a
7270 processor specific section. */
7272 if (bfd_is_abs_section (sec
))
7274 else if (sec
== NULL
|| sec
->owner
== NULL
)
7276 bfd_set_error (bfd_error_bad_value
);
7281 asection
*osec
= sec
->output_section
;
7283 /* If we have discarded a section, the output
7284 section will be the absolute section. In
7285 case of discarded link-once and discarded
7286 SEC_MERGE sections, use the kept section. */
7287 if (bfd_is_abs_section (osec
)
7288 && sec
->kept_section
!= NULL
7289 && sec
->kept_section
->output_section
!= NULL
)
7291 osec
= sec
->kept_section
->output_section
;
7292 irela
->r_addend
-= osec
->vma
;
7295 if (!bfd_is_abs_section (osec
))
7297 r_symndx
= osec
->target_index
;
7298 BFD_ASSERT (r_symndx
!= 0);
7302 /* Adjust the addend according to where the
7303 section winds up in the output section. */
7305 irela
->r_addend
+= sec
->output_offset
;
7309 if (finfo
->indices
[r_symndx
] == -1)
7311 unsigned long shlink
;
7315 if (finfo
->info
->strip
== strip_all
)
7317 /* You can't do ld -r -s. */
7318 bfd_set_error (bfd_error_invalid_operation
);
7322 /* This symbol was skipped earlier, but
7323 since it is needed by a reloc, we
7324 must output it now. */
7325 shlink
= symtab_hdr
->sh_link
;
7326 name
= (bfd_elf_string_from_elf_section
7327 (input_bfd
, shlink
, sym
.st_name
));
7331 osec
= sec
->output_section
;
7333 _bfd_elf_section_from_bfd_section (output_bfd
,
7335 if (sym
.st_shndx
== SHN_BAD
)
7338 sym
.st_value
+= sec
->output_offset
;
7339 if (! finfo
->info
->relocatable
)
7341 sym
.st_value
+= osec
->vma
;
7342 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7344 /* STT_TLS symbols are relative to PT_TLS
7346 BFD_ASSERT (elf_hash_table (finfo
->info
)
7348 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7353 finfo
->indices
[r_symndx
]
7354 = bfd_get_symcount (output_bfd
);
7356 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7361 r_symndx
= finfo
->indices
[r_symndx
];
7364 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7365 | (irela
->r_info
& r_type_mask
));
7368 /* Swap out the relocs. */
7369 if (bed
->elf_backend_emit_relocs
7370 && !(finfo
->info
->relocatable
7371 || finfo
->info
->emitrelocations
))
7372 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7374 reloc_emitter
= _bfd_elf_link_output_relocs
;
7376 if (input_rel_hdr
->sh_size
!= 0
7377 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7381 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7382 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7384 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7385 * bed
->s
->int_rels_per_ext_rel
);
7386 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7393 /* Write out the modified section contents. */
7394 if (bed
->elf_backend_write_section
7395 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7397 /* Section written out. */
7399 else switch (o
->sec_info_type
)
7401 case ELF_INFO_TYPE_STABS
:
7402 if (! (_bfd_write_section_stabs
7404 &elf_hash_table (finfo
->info
)->stab_info
,
7405 o
, &elf_section_data (o
)->sec_info
, contents
)))
7408 case ELF_INFO_TYPE_MERGE
:
7409 if (! _bfd_write_merged_section (output_bfd
, o
,
7410 elf_section_data (o
)->sec_info
))
7413 case ELF_INFO_TYPE_EH_FRAME
:
7415 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7422 if (! (o
->flags
& SEC_EXCLUDE
)
7423 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7425 (file_ptr
) o
->output_offset
,
7436 /* Generate a reloc when linking an ELF file. This is a reloc
7437 requested by the linker, and does come from any input file. This
7438 is used to build constructor and destructor tables when linking
7442 elf_reloc_link_order (bfd
*output_bfd
,
7443 struct bfd_link_info
*info
,
7444 asection
*output_section
,
7445 struct bfd_link_order
*link_order
)
7447 reloc_howto_type
*howto
;
7451 struct elf_link_hash_entry
**rel_hash_ptr
;
7452 Elf_Internal_Shdr
*rel_hdr
;
7453 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7454 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7458 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7461 bfd_set_error (bfd_error_bad_value
);
7465 addend
= link_order
->u
.reloc
.p
->addend
;
7467 /* Figure out the symbol index. */
7468 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7469 + elf_section_data (output_section
)->rel_count
7470 + elf_section_data (output_section
)->rel_count2
);
7471 if (link_order
->type
== bfd_section_reloc_link_order
)
7473 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7474 BFD_ASSERT (indx
!= 0);
7475 *rel_hash_ptr
= NULL
;
7479 struct elf_link_hash_entry
*h
;
7481 /* Treat a reloc against a defined symbol as though it were
7482 actually against the section. */
7483 h
= ((struct elf_link_hash_entry
*)
7484 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7485 link_order
->u
.reloc
.p
->u
.name
,
7486 FALSE
, FALSE
, TRUE
));
7488 && (h
->root
.type
== bfd_link_hash_defined
7489 || h
->root
.type
== bfd_link_hash_defweak
))
7493 section
= h
->root
.u
.def
.section
;
7494 indx
= section
->output_section
->target_index
;
7495 *rel_hash_ptr
= NULL
;
7496 /* It seems that we ought to add the symbol value to the
7497 addend here, but in practice it has already been added
7498 because it was passed to constructor_callback. */
7499 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7503 /* Setting the index to -2 tells elf_link_output_extsym that
7504 this symbol is used by a reloc. */
7511 if (! ((*info
->callbacks
->unattached_reloc
)
7512 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7518 /* If this is an inplace reloc, we must write the addend into the
7520 if (howto
->partial_inplace
&& addend
!= 0)
7523 bfd_reloc_status_type rstat
;
7526 const char *sym_name
;
7528 size
= bfd_get_reloc_size (howto
);
7529 buf
= bfd_zmalloc (size
);
7532 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7539 case bfd_reloc_outofrange
:
7542 case bfd_reloc_overflow
:
7543 if (link_order
->type
== bfd_section_reloc_link_order
)
7544 sym_name
= bfd_section_name (output_bfd
,
7545 link_order
->u
.reloc
.p
->u
.section
);
7547 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7548 if (! ((*info
->callbacks
->reloc_overflow
)
7549 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7550 NULL
, (bfd_vma
) 0)))
7557 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7558 link_order
->offset
, size
);
7564 /* The address of a reloc is relative to the section in a
7565 relocatable file, and is a virtual address in an executable
7567 offset
= link_order
->offset
;
7568 if (! info
->relocatable
)
7569 offset
+= output_section
->vma
;
7571 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7573 irel
[i
].r_offset
= offset
;
7575 irel
[i
].r_addend
= 0;
7577 if (bed
->s
->arch_size
== 32)
7578 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7580 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7582 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7583 erel
= rel_hdr
->contents
;
7584 if (rel_hdr
->sh_type
== SHT_REL
)
7586 erel
+= (elf_section_data (output_section
)->rel_count
7587 * bed
->s
->sizeof_rel
);
7588 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7592 irel
[0].r_addend
= addend
;
7593 erel
+= (elf_section_data (output_section
)->rel_count
7594 * bed
->s
->sizeof_rela
);
7595 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7598 ++elf_section_data (output_section
)->rel_count
;
7604 /* Get the output vma of the section pointed to by the sh_link field. */
7607 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7609 Elf_Internal_Shdr
**elf_shdrp
;
7613 s
= p
->u
.indirect
.section
;
7614 elf_shdrp
= elf_elfsections (s
->owner
);
7615 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7616 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7618 The Intel C compiler generates SHT_IA_64_UNWIND with
7619 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7620 sh_info fields. Hence we could get the situation
7621 where elfsec is 0. */
7624 const struct elf_backend_data
*bed
7625 = get_elf_backend_data (s
->owner
);
7626 if (bed
->link_order_error_handler
)
7627 bed
->link_order_error_handler
7628 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7633 s
= elf_shdrp
[elfsec
]->bfd_section
;
7634 return s
->output_section
->vma
+ s
->output_offset
;
7639 /* Compare two sections based on the locations of the sections they are
7640 linked to. Used by elf_fixup_link_order. */
7643 compare_link_order (const void * a
, const void * b
)
7648 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7649 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7656 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7657 order as their linked sections. Returns false if this could not be done
7658 because an output section includes both ordered and unordered
7659 sections. Ideally we'd do this in the linker proper. */
7662 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7667 struct bfd_link_order
*p
;
7669 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7671 struct bfd_link_order
**sections
;
7677 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7679 if (p
->type
== bfd_indirect_link_order
7680 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7681 == bfd_target_elf_flavour
)
7682 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7684 s
= p
->u
.indirect
.section
;
7685 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7687 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7696 if (!seen_linkorder
)
7699 if (seen_other
&& seen_linkorder
)
7701 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7703 bfd_set_error (bfd_error_bad_value
);
7707 sections
= (struct bfd_link_order
**)
7708 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7711 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7713 sections
[seen_linkorder
++] = p
;
7715 /* Sort the input sections in the order of their linked section. */
7716 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7717 compare_link_order
);
7719 /* Change the offsets of the sections. */
7721 for (n
= 0; n
< seen_linkorder
; n
++)
7723 s
= sections
[n
]->u
.indirect
.section
;
7724 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7725 s
->output_offset
= offset
;
7726 sections
[n
]->offset
= offset
;
7727 offset
+= sections
[n
]->size
;
7734 /* Do the final step of an ELF link. */
7737 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7739 bfd_boolean dynamic
;
7740 bfd_boolean emit_relocs
;
7742 struct elf_final_link_info finfo
;
7743 register asection
*o
;
7744 register struct bfd_link_order
*p
;
7746 bfd_size_type max_contents_size
;
7747 bfd_size_type max_external_reloc_size
;
7748 bfd_size_type max_internal_reloc_count
;
7749 bfd_size_type max_sym_count
;
7750 bfd_size_type max_sym_shndx_count
;
7752 Elf_Internal_Sym elfsym
;
7754 Elf_Internal_Shdr
*symtab_hdr
;
7755 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7756 Elf_Internal_Shdr
*symstrtab_hdr
;
7757 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7758 struct elf_outext_info eoinfo
;
7760 size_t relativecount
= 0;
7761 asection
*reldyn
= 0;
7764 if (! is_elf_hash_table (info
->hash
))
7768 abfd
->flags
|= DYNAMIC
;
7770 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7771 dynobj
= elf_hash_table (info
)->dynobj
;
7773 emit_relocs
= (info
->relocatable
7774 || info
->emitrelocations
7775 || bed
->elf_backend_emit_relocs
);
7778 finfo
.output_bfd
= abfd
;
7779 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7780 if (finfo
.symstrtab
== NULL
)
7785 finfo
.dynsym_sec
= NULL
;
7786 finfo
.hash_sec
= NULL
;
7787 finfo
.symver_sec
= NULL
;
7791 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7792 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7793 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7794 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7795 /* Note that it is OK if symver_sec is NULL. */
7798 finfo
.contents
= NULL
;
7799 finfo
.external_relocs
= NULL
;
7800 finfo
.internal_relocs
= NULL
;
7801 finfo
.external_syms
= NULL
;
7802 finfo
.locsym_shndx
= NULL
;
7803 finfo
.internal_syms
= NULL
;
7804 finfo
.indices
= NULL
;
7805 finfo
.sections
= NULL
;
7806 finfo
.symbuf
= NULL
;
7807 finfo
.symshndxbuf
= NULL
;
7808 finfo
.symbuf_count
= 0;
7809 finfo
.shndxbuf_size
= 0;
7811 /* Count up the number of relocations we will output for each output
7812 section, so that we know the sizes of the reloc sections. We
7813 also figure out some maximum sizes. */
7814 max_contents_size
= 0;
7815 max_external_reloc_size
= 0;
7816 max_internal_reloc_count
= 0;
7818 max_sym_shndx_count
= 0;
7820 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7822 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7825 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
7827 unsigned int reloc_count
= 0;
7828 struct bfd_elf_section_data
*esdi
= NULL
;
7829 unsigned int *rel_count1
;
7831 if (p
->type
== bfd_section_reloc_link_order
7832 || p
->type
== bfd_symbol_reloc_link_order
)
7834 else if (p
->type
== bfd_indirect_link_order
)
7838 sec
= p
->u
.indirect
.section
;
7839 esdi
= elf_section_data (sec
);
7841 /* Mark all sections which are to be included in the
7842 link. This will normally be every section. We need
7843 to do this so that we can identify any sections which
7844 the linker has decided to not include. */
7845 sec
->linker_mark
= TRUE
;
7847 if (sec
->flags
& SEC_MERGE
)
7850 if (info
->relocatable
|| info
->emitrelocations
)
7851 reloc_count
= sec
->reloc_count
;
7852 else if (bed
->elf_backend_count_relocs
)
7854 Elf_Internal_Rela
* relocs
;
7856 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7859 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7861 if (elf_section_data (o
)->relocs
!= relocs
)
7865 if (sec
->rawsize
> max_contents_size
)
7866 max_contents_size
= sec
->rawsize
;
7867 if (sec
->size
> max_contents_size
)
7868 max_contents_size
= sec
->size
;
7870 /* We are interested in just local symbols, not all
7872 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7873 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7877 if (elf_bad_symtab (sec
->owner
))
7878 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7879 / bed
->s
->sizeof_sym
);
7881 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7883 if (sym_count
> max_sym_count
)
7884 max_sym_count
= sym_count
;
7886 if (sym_count
> max_sym_shndx_count
7887 && elf_symtab_shndx (sec
->owner
) != 0)
7888 max_sym_shndx_count
= sym_count
;
7890 if ((sec
->flags
& SEC_RELOC
) != 0)
7894 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7895 if (ext_size
> max_external_reloc_size
)
7896 max_external_reloc_size
= ext_size
;
7897 if (sec
->reloc_count
> max_internal_reloc_count
)
7898 max_internal_reloc_count
= sec
->reloc_count
;
7903 if (reloc_count
== 0)
7906 o
->reloc_count
+= reloc_count
;
7908 /* MIPS may have a mix of REL and RELA relocs on sections.
7909 To support this curious ABI we keep reloc counts in
7910 elf_section_data too. We must be careful to add the
7911 relocations from the input section to the right output
7912 count. FIXME: Get rid of one count. We have
7913 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7914 rel_count1
= &esdo
->rel_count
;
7917 bfd_boolean same_size
;
7918 bfd_size_type entsize1
;
7920 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7921 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7922 || entsize1
== bed
->s
->sizeof_rela
);
7923 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7926 rel_count1
= &esdo
->rel_count2
;
7928 if (esdi
->rel_hdr2
!= NULL
)
7930 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7931 unsigned int alt_count
;
7932 unsigned int *rel_count2
;
7934 BFD_ASSERT (entsize2
!= entsize1
7935 && (entsize2
== bed
->s
->sizeof_rel
7936 || entsize2
== bed
->s
->sizeof_rela
));
7938 rel_count2
= &esdo
->rel_count2
;
7940 rel_count2
= &esdo
->rel_count
;
7942 /* The following is probably too simplistic if the
7943 backend counts output relocs unusually. */
7944 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7945 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7946 *rel_count2
+= alt_count
;
7947 reloc_count
-= alt_count
;
7950 *rel_count1
+= reloc_count
;
7953 if (o
->reloc_count
> 0)
7954 o
->flags
|= SEC_RELOC
;
7957 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7958 set it (this is probably a bug) and if it is set
7959 assign_section_numbers will create a reloc section. */
7960 o
->flags
&=~ SEC_RELOC
;
7963 /* If the SEC_ALLOC flag is not set, force the section VMA to
7964 zero. This is done in elf_fake_sections as well, but forcing
7965 the VMA to 0 here will ensure that relocs against these
7966 sections are handled correctly. */
7967 if ((o
->flags
& SEC_ALLOC
) == 0
7968 && ! o
->user_set_vma
)
7972 if (! info
->relocatable
&& merged
)
7973 elf_link_hash_traverse (elf_hash_table (info
),
7974 _bfd_elf_link_sec_merge_syms
, abfd
);
7976 /* Figure out the file positions for everything but the symbol table
7977 and the relocs. We set symcount to force assign_section_numbers
7978 to create a symbol table. */
7979 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7980 BFD_ASSERT (! abfd
->output_has_begun
);
7981 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7984 /* Set sizes, and assign file positions for reloc sections. */
7985 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7987 if ((o
->flags
& SEC_RELOC
) != 0)
7989 if (!(_bfd_elf_link_size_reloc_section
7990 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7993 if (elf_section_data (o
)->rel_hdr2
7994 && !(_bfd_elf_link_size_reloc_section
7995 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7999 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
8000 to count upwards while actually outputting the relocations. */
8001 elf_section_data (o
)->rel_count
= 0;
8002 elf_section_data (o
)->rel_count2
= 0;
8005 _bfd_elf_assign_file_positions_for_relocs (abfd
);
8007 /* We have now assigned file positions for all the sections except
8008 .symtab and .strtab. We start the .symtab section at the current
8009 file position, and write directly to it. We build the .strtab
8010 section in memory. */
8011 bfd_get_symcount (abfd
) = 0;
8012 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8013 /* sh_name is set in prep_headers. */
8014 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8015 /* sh_flags, sh_addr and sh_size all start off zero. */
8016 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8017 /* sh_link is set in assign_section_numbers. */
8018 /* sh_info is set below. */
8019 /* sh_offset is set just below. */
8020 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
8022 off
= elf_tdata (abfd
)->next_file_pos
;
8023 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
8025 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8026 incorrect. We do not yet know the size of the .symtab section.
8027 We correct next_file_pos below, after we do know the size. */
8029 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8030 continuously seeking to the right position in the file. */
8031 if (! info
->keep_memory
|| max_sym_count
< 20)
8032 finfo
.symbuf_size
= 20;
8034 finfo
.symbuf_size
= max_sym_count
;
8035 amt
= finfo
.symbuf_size
;
8036 amt
*= bed
->s
->sizeof_sym
;
8037 finfo
.symbuf
= bfd_malloc (amt
);
8038 if (finfo
.symbuf
== NULL
)
8040 if (elf_numsections (abfd
) > SHN_LORESERVE
)
8042 /* Wild guess at number of output symbols. realloc'd as needed. */
8043 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
8044 finfo
.shndxbuf_size
= amt
;
8045 amt
*= sizeof (Elf_External_Sym_Shndx
);
8046 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
8047 if (finfo
.symshndxbuf
== NULL
)
8051 /* Start writing out the symbol table. The first symbol is always a
8053 if (info
->strip
!= strip_all
8056 elfsym
.st_value
= 0;
8059 elfsym
.st_other
= 0;
8060 elfsym
.st_shndx
= SHN_UNDEF
;
8061 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
8066 /* Output a symbol for each section. We output these even if we are
8067 discarding local symbols, since they are used for relocs. These
8068 symbols have no names. We store the index of each one in the
8069 index field of the section, so that we can find it again when
8070 outputting relocs. */
8071 if (info
->strip
!= strip_all
8075 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8076 elfsym
.st_other
= 0;
8077 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8079 o
= bfd_section_from_elf_index (abfd
, i
);
8081 o
->target_index
= bfd_get_symcount (abfd
);
8082 elfsym
.st_shndx
= i
;
8083 if (info
->relocatable
|| o
== NULL
)
8084 elfsym
.st_value
= 0;
8086 elfsym
.st_value
= o
->vma
;
8087 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
8089 if (i
== SHN_LORESERVE
- 1)
8090 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
8094 /* Allocate some memory to hold information read in from the input
8096 if (max_contents_size
!= 0)
8098 finfo
.contents
= bfd_malloc (max_contents_size
);
8099 if (finfo
.contents
== NULL
)
8103 if (max_external_reloc_size
!= 0)
8105 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8106 if (finfo
.external_relocs
== NULL
)
8110 if (max_internal_reloc_count
!= 0)
8112 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8113 amt
*= sizeof (Elf_Internal_Rela
);
8114 finfo
.internal_relocs
= bfd_malloc (amt
);
8115 if (finfo
.internal_relocs
== NULL
)
8119 if (max_sym_count
!= 0)
8121 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8122 finfo
.external_syms
= bfd_malloc (amt
);
8123 if (finfo
.external_syms
== NULL
)
8126 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8127 finfo
.internal_syms
= bfd_malloc (amt
);
8128 if (finfo
.internal_syms
== NULL
)
8131 amt
= max_sym_count
* sizeof (long);
8132 finfo
.indices
= bfd_malloc (amt
);
8133 if (finfo
.indices
== NULL
)
8136 amt
= max_sym_count
* sizeof (asection
*);
8137 finfo
.sections
= bfd_malloc (amt
);
8138 if (finfo
.sections
== NULL
)
8142 if (max_sym_shndx_count
!= 0)
8144 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8145 finfo
.locsym_shndx
= bfd_malloc (amt
);
8146 if (finfo
.locsym_shndx
== NULL
)
8150 if (elf_hash_table (info
)->tls_sec
)
8152 bfd_vma base
, end
= 0;
8155 for (sec
= elf_hash_table (info
)->tls_sec
;
8156 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8159 bfd_vma size
= sec
->size
;
8161 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8163 struct bfd_link_order
*o
;
8165 for (o
= sec
->map_head
.link_order
; o
!= NULL
; o
= o
->next
)
8166 if (size
< o
->offset
+ o
->size
)
8167 size
= o
->offset
+ o
->size
;
8169 end
= sec
->vma
+ size
;
8171 base
= elf_hash_table (info
)->tls_sec
->vma
;
8172 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8173 elf_hash_table (info
)->tls_size
= end
- base
;
8176 /* Reorder SHF_LINK_ORDER sections. */
8177 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8179 if (!elf_fixup_link_order (abfd
, o
))
8183 /* Since ELF permits relocations to be against local symbols, we
8184 must have the local symbols available when we do the relocations.
8185 Since we would rather only read the local symbols once, and we
8186 would rather not keep them in memory, we handle all the
8187 relocations for a single input file at the same time.
8189 Unfortunately, there is no way to know the total number of local
8190 symbols until we have seen all of them, and the local symbol
8191 indices precede the global symbol indices. This means that when
8192 we are generating relocatable output, and we see a reloc against
8193 a global symbol, we can not know the symbol index until we have
8194 finished examining all the local symbols to see which ones we are
8195 going to output. To deal with this, we keep the relocations in
8196 memory, and don't output them until the end of the link. This is
8197 an unfortunate waste of memory, but I don't see a good way around
8198 it. Fortunately, it only happens when performing a relocatable
8199 link, which is not the common case. FIXME: If keep_memory is set
8200 we could write the relocs out and then read them again; I don't
8201 know how bad the memory loss will be. */
8203 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8204 sub
->output_has_begun
= FALSE
;
8205 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8207 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
8209 if (p
->type
== bfd_indirect_link_order
8210 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8211 == bfd_target_elf_flavour
)
8212 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8214 if (! sub
->output_has_begun
)
8216 if (! elf_link_input_bfd (&finfo
, sub
))
8218 sub
->output_has_begun
= TRUE
;
8221 else if (p
->type
== bfd_section_reloc_link_order
8222 || p
->type
== bfd_symbol_reloc_link_order
)
8224 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8229 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8235 /* Output any global symbols that got converted to local in a
8236 version script or due to symbol visibility. We do this in a
8237 separate step since ELF requires all local symbols to appear
8238 prior to any global symbols. FIXME: We should only do this if
8239 some global symbols were, in fact, converted to become local.
8240 FIXME: Will this work correctly with the Irix 5 linker? */
8241 eoinfo
.failed
= FALSE
;
8242 eoinfo
.finfo
= &finfo
;
8243 eoinfo
.localsyms
= TRUE
;
8244 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8249 /* That wrote out all the local symbols. Finish up the symbol table
8250 with the global symbols. Even if we want to strip everything we
8251 can, we still need to deal with those global symbols that got
8252 converted to local in a version script. */
8254 /* The sh_info field records the index of the first non local symbol. */
8255 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8258 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8260 Elf_Internal_Sym sym
;
8261 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8262 long last_local
= 0;
8264 /* Write out the section symbols for the output sections. */
8265 if (info
->shared
|| elf_hash_table (info
)->is_relocatable_executable
)
8271 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8274 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8280 dynindx
= elf_section_data (s
)->dynindx
;
8283 indx
= elf_section_data (s
)->this_idx
;
8284 BFD_ASSERT (indx
> 0);
8285 sym
.st_shndx
= indx
;
8286 sym
.st_value
= s
->vma
;
8287 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8288 if (last_local
< dynindx
)
8289 last_local
= dynindx
;
8290 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8294 /* Write out the local dynsyms. */
8295 if (elf_hash_table (info
)->dynlocal
)
8297 struct elf_link_local_dynamic_entry
*e
;
8298 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8303 sym
.st_size
= e
->isym
.st_size
;
8304 sym
.st_other
= e
->isym
.st_other
;
8306 /* Copy the internal symbol as is.
8307 Note that we saved a word of storage and overwrote
8308 the original st_name with the dynstr_index. */
8311 if (e
->isym
.st_shndx
!= SHN_UNDEF
8312 && (e
->isym
.st_shndx
< SHN_LORESERVE
8313 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8315 s
= bfd_section_from_elf_index (e
->input_bfd
,
8319 elf_section_data (s
->output_section
)->this_idx
;
8320 sym
.st_value
= (s
->output_section
->vma
8322 + e
->isym
.st_value
);
8325 if (last_local
< e
->dynindx
)
8326 last_local
= e
->dynindx
;
8328 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8329 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8333 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8337 /* We get the global symbols from the hash table. */
8338 eoinfo
.failed
= FALSE
;
8339 eoinfo
.localsyms
= FALSE
;
8340 eoinfo
.finfo
= &finfo
;
8341 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8346 /* If backend needs to output some symbols not present in the hash
8347 table, do it now. */
8348 if (bed
->elf_backend_output_arch_syms
)
8350 typedef bfd_boolean (*out_sym_func
)
8351 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8352 struct elf_link_hash_entry
*);
8354 if (! ((*bed
->elf_backend_output_arch_syms
)
8355 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8359 /* Flush all symbols to the file. */
8360 if (! elf_link_flush_output_syms (&finfo
, bed
))
8363 /* Now we know the size of the symtab section. */
8364 off
+= symtab_hdr
->sh_size
;
8366 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8367 if (symtab_shndx_hdr
->sh_name
!= 0)
8369 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8370 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8371 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8372 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8373 symtab_shndx_hdr
->sh_size
= amt
;
8375 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8378 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8379 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8384 /* Finish up and write out the symbol string table (.strtab)
8386 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8387 /* sh_name was set in prep_headers. */
8388 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8389 symstrtab_hdr
->sh_flags
= 0;
8390 symstrtab_hdr
->sh_addr
= 0;
8391 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8392 symstrtab_hdr
->sh_entsize
= 0;
8393 symstrtab_hdr
->sh_link
= 0;
8394 symstrtab_hdr
->sh_info
= 0;
8395 /* sh_offset is set just below. */
8396 symstrtab_hdr
->sh_addralign
= 1;
8398 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8399 elf_tdata (abfd
)->next_file_pos
= off
;
8401 if (bfd_get_symcount (abfd
) > 0)
8403 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8404 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8408 /* Adjust the relocs to have the correct symbol indices. */
8409 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8411 if ((o
->flags
& SEC_RELOC
) == 0)
8414 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8415 elf_section_data (o
)->rel_count
,
8416 elf_section_data (o
)->rel_hashes
);
8417 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8418 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8419 elf_section_data (o
)->rel_count2
,
8420 (elf_section_data (o
)->rel_hashes
8421 + elf_section_data (o
)->rel_count
));
8423 /* Set the reloc_count field to 0 to prevent write_relocs from
8424 trying to swap the relocs out itself. */
8428 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8429 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8431 /* If we are linking against a dynamic object, or generating a
8432 shared library, finish up the dynamic linking information. */
8435 bfd_byte
*dyncon
, *dynconend
;
8437 /* Fix up .dynamic entries. */
8438 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8439 BFD_ASSERT (o
!= NULL
);
8441 dyncon
= o
->contents
;
8442 dynconend
= o
->contents
+ o
->size
;
8443 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8445 Elf_Internal_Dyn dyn
;
8449 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8456 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8458 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8460 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8461 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8464 dyn
.d_un
.d_val
= relativecount
;
8471 name
= info
->init_function
;
8474 name
= info
->fini_function
;
8477 struct elf_link_hash_entry
*h
;
8479 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8480 FALSE
, FALSE
, TRUE
);
8482 && (h
->root
.type
== bfd_link_hash_defined
8483 || h
->root
.type
== bfd_link_hash_defweak
))
8485 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8486 o
= h
->root
.u
.def
.section
;
8487 if (o
->output_section
!= NULL
)
8488 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8489 + o
->output_offset
);
8492 /* The symbol is imported from another shared
8493 library and does not apply to this one. */
8501 case DT_PREINIT_ARRAYSZ
:
8502 name
= ".preinit_array";
8504 case DT_INIT_ARRAYSZ
:
8505 name
= ".init_array";
8507 case DT_FINI_ARRAYSZ
:
8508 name
= ".fini_array";
8510 o
= bfd_get_section_by_name (abfd
, name
);
8513 (*_bfd_error_handler
)
8514 (_("%B: could not find output section %s"), abfd
, name
);
8518 (*_bfd_error_handler
)
8519 (_("warning: %s section has zero size"), name
);
8520 dyn
.d_un
.d_val
= o
->size
;
8523 case DT_PREINIT_ARRAY
:
8524 name
= ".preinit_array";
8527 name
= ".init_array";
8530 name
= ".fini_array";
8543 name
= ".gnu.version_d";
8546 name
= ".gnu.version_r";
8549 name
= ".gnu.version";
8551 o
= bfd_get_section_by_name (abfd
, name
);
8554 (*_bfd_error_handler
)
8555 (_("%B: could not find output section %s"), abfd
, name
);
8558 dyn
.d_un
.d_ptr
= o
->vma
;
8565 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8570 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8572 Elf_Internal_Shdr
*hdr
;
8574 hdr
= elf_elfsections (abfd
)[i
];
8575 if (hdr
->sh_type
== type
8576 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8578 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8579 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8582 if (dyn
.d_un
.d_val
== 0
8583 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8584 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8590 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8594 /* If we have created any dynamic sections, then output them. */
8597 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8600 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8602 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8604 || o
->output_section
== bfd_abs_section_ptr
)
8606 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8608 /* At this point, we are only interested in sections
8609 created by _bfd_elf_link_create_dynamic_sections. */
8612 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8614 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8616 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8618 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8620 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8622 (file_ptr
) o
->output_offset
,
8628 /* The contents of the .dynstr section are actually in a
8630 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8631 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8632 || ! _bfd_elf_strtab_emit (abfd
,
8633 elf_hash_table (info
)->dynstr
))
8639 if (info
->relocatable
)
8641 bfd_boolean failed
= FALSE
;
8643 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8648 /* If we have optimized stabs strings, output them. */
8649 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8651 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8655 if (info
->eh_frame_hdr
)
8657 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8661 if (finfo
.symstrtab
!= NULL
)
8662 _bfd_stringtab_free (finfo
.symstrtab
);
8663 if (finfo
.contents
!= NULL
)
8664 free (finfo
.contents
);
8665 if (finfo
.external_relocs
!= NULL
)
8666 free (finfo
.external_relocs
);
8667 if (finfo
.internal_relocs
!= NULL
)
8668 free (finfo
.internal_relocs
);
8669 if (finfo
.external_syms
!= NULL
)
8670 free (finfo
.external_syms
);
8671 if (finfo
.locsym_shndx
!= NULL
)
8672 free (finfo
.locsym_shndx
);
8673 if (finfo
.internal_syms
!= NULL
)
8674 free (finfo
.internal_syms
);
8675 if (finfo
.indices
!= NULL
)
8676 free (finfo
.indices
);
8677 if (finfo
.sections
!= NULL
)
8678 free (finfo
.sections
);
8679 if (finfo
.symbuf
!= NULL
)
8680 free (finfo
.symbuf
);
8681 if (finfo
.symshndxbuf
!= NULL
)
8682 free (finfo
.symshndxbuf
);
8683 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8685 if ((o
->flags
& SEC_RELOC
) != 0
8686 && elf_section_data (o
)->rel_hashes
!= NULL
)
8687 free (elf_section_data (o
)->rel_hashes
);
8690 elf_tdata (abfd
)->linker
= TRUE
;
8695 if (finfo
.symstrtab
!= NULL
)
8696 _bfd_stringtab_free (finfo
.symstrtab
);
8697 if (finfo
.contents
!= NULL
)
8698 free (finfo
.contents
);
8699 if (finfo
.external_relocs
!= NULL
)
8700 free (finfo
.external_relocs
);
8701 if (finfo
.internal_relocs
!= NULL
)
8702 free (finfo
.internal_relocs
);
8703 if (finfo
.external_syms
!= NULL
)
8704 free (finfo
.external_syms
);
8705 if (finfo
.locsym_shndx
!= NULL
)
8706 free (finfo
.locsym_shndx
);
8707 if (finfo
.internal_syms
!= NULL
)
8708 free (finfo
.internal_syms
);
8709 if (finfo
.indices
!= NULL
)
8710 free (finfo
.indices
);
8711 if (finfo
.sections
!= NULL
)
8712 free (finfo
.sections
);
8713 if (finfo
.symbuf
!= NULL
)
8714 free (finfo
.symbuf
);
8715 if (finfo
.symshndxbuf
!= NULL
)
8716 free (finfo
.symshndxbuf
);
8717 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8719 if ((o
->flags
& SEC_RELOC
) != 0
8720 && elf_section_data (o
)->rel_hashes
!= NULL
)
8721 free (elf_section_data (o
)->rel_hashes
);
8727 /* Garbage collect unused sections. */
8729 /* The mark phase of garbage collection. For a given section, mark
8730 it and any sections in this section's group, and all the sections
8731 which define symbols to which it refers. */
8733 typedef asection
* (*gc_mark_hook_fn
)
8734 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8735 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8738 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8740 gc_mark_hook_fn gc_mark_hook
)
8743 asection
*group_sec
;
8747 /* Mark all the sections in the group. */
8748 group_sec
= elf_section_data (sec
)->next_in_group
;
8749 if (group_sec
&& !group_sec
->gc_mark
)
8750 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8753 /* Look through the section relocs. */
8755 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8757 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8758 Elf_Internal_Shdr
*symtab_hdr
;
8759 struct elf_link_hash_entry
**sym_hashes
;
8762 bfd
*input_bfd
= sec
->owner
;
8763 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8764 Elf_Internal_Sym
*isym
= NULL
;
8767 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8768 sym_hashes
= elf_sym_hashes (input_bfd
);
8770 /* Read the local symbols. */
8771 if (elf_bad_symtab (input_bfd
))
8773 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8777 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8779 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8780 if (isym
== NULL
&& nlocsyms
!= 0)
8782 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8788 /* Read the relocations. */
8789 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8791 if (relstart
== NULL
)
8796 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8798 if (bed
->s
->arch_size
== 32)
8803 for (rel
= relstart
; rel
< relend
; rel
++)
8805 unsigned long r_symndx
;
8807 struct elf_link_hash_entry
*h
;
8809 r_symndx
= rel
->r_info
>> r_sym_shift
;
8813 if (r_symndx
>= nlocsyms
8814 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8816 h
= sym_hashes
[r_symndx
- extsymoff
];
8817 while (h
->root
.type
== bfd_link_hash_indirect
8818 || h
->root
.type
== bfd_link_hash_warning
)
8819 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8820 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8824 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8827 if (rsec
&& !rsec
->gc_mark
)
8829 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8831 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8840 if (elf_section_data (sec
)->relocs
!= relstart
)
8843 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8845 if (! info
->keep_memory
)
8848 symtab_hdr
->contents
= (unsigned char *) isym
;
8855 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8858 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8862 if (h
->root
.type
== bfd_link_hash_warning
)
8863 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8865 if (h
->dynindx
!= -1
8866 && ((h
->root
.type
!= bfd_link_hash_defined
8867 && h
->root
.type
!= bfd_link_hash_defweak
)
8868 || h
->root
.u
.def
.section
->gc_mark
))
8869 h
->dynindx
= (*idx
)++;
8874 /* The sweep phase of garbage collection. Remove all garbage sections. */
8876 typedef bfd_boolean (*gc_sweep_hook_fn
)
8877 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8880 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8884 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8888 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8891 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8893 /* Keep debug and special sections. */
8894 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8895 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8901 /* Skip sweeping sections already excluded. */
8902 if (o
->flags
& SEC_EXCLUDE
)
8905 /* Since this is early in the link process, it is simple
8906 to remove a section from the output. */
8907 o
->flags
|= SEC_EXCLUDE
;
8909 /* But we also have to update some of the relocation
8910 info we collected before. */
8912 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8914 Elf_Internal_Rela
*internal_relocs
;
8918 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8920 if (internal_relocs
== NULL
)
8923 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8925 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8926 free (internal_relocs
);
8934 /* Remove the symbols that were in the swept sections from the dynamic
8935 symbol table. GCFIXME: Anyone know how to get them out of the
8936 static symbol table as well? */
8940 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8942 elf_hash_table (info
)->dynsymcount
= i
;
8948 /* Propagate collected vtable information. This is called through
8949 elf_link_hash_traverse. */
8952 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8954 if (h
->root
.type
== bfd_link_hash_warning
)
8955 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8957 /* Those that are not vtables. */
8958 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8961 /* Those vtables that do not have parents, we cannot merge. */
8962 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8965 /* If we've already been done, exit. */
8966 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8969 /* Make sure the parent's table is up to date. */
8970 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8972 if (h
->vtable
->used
== NULL
)
8974 /* None of this table's entries were referenced. Re-use the
8976 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8977 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8982 bfd_boolean
*cu
, *pu
;
8984 /* Or the parent's entries into ours. */
8985 cu
= h
->vtable
->used
;
8987 pu
= h
->vtable
->parent
->vtable
->used
;
8990 const struct elf_backend_data
*bed
;
8991 unsigned int log_file_align
;
8993 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8994 log_file_align
= bed
->s
->log_file_align
;
8995 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
9010 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
9013 bfd_vma hstart
, hend
;
9014 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
9015 const struct elf_backend_data
*bed
;
9016 unsigned int log_file_align
;
9018 if (h
->root
.type
== bfd_link_hash_warning
)
9019 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9021 /* Take care of both those symbols that do not describe vtables as
9022 well as those that are not loaded. */
9023 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
9026 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
9027 || h
->root
.type
== bfd_link_hash_defweak
);
9029 sec
= h
->root
.u
.def
.section
;
9030 hstart
= h
->root
.u
.def
.value
;
9031 hend
= hstart
+ h
->size
;
9033 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
9035 return *(bfd_boolean
*) okp
= FALSE
;
9036 bed
= get_elf_backend_data (sec
->owner
);
9037 log_file_align
= bed
->s
->log_file_align
;
9039 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
9041 for (rel
= relstart
; rel
< relend
; ++rel
)
9042 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
9044 /* If the entry is in use, do nothing. */
9046 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
9048 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
9049 if (h
->vtable
->used
[entry
])
9052 /* Otherwise, kill it. */
9053 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
9059 /* Mark sections containing dynamically referenced symbols. This is called
9060 through elf_link_hash_traverse. */
9063 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
9064 void *okp ATTRIBUTE_UNUSED
)
9066 if (h
->root
.type
== bfd_link_hash_warning
)
9067 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9069 if ((h
->root
.type
== bfd_link_hash_defined
9070 || h
->root
.type
== bfd_link_hash_defweak
)
9072 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
9077 /* Mark sections containing global symbols. This is called through
9078 elf_link_hash_traverse. */
9081 elf_mark_used_section (struct elf_link_hash_entry
*h
,
9082 void *data ATTRIBUTE_UNUSED
)
9084 if (h
->root
.type
== bfd_link_hash_warning
)
9085 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9087 if (h
->root
.type
== bfd_link_hash_defined
9088 || h
->root
.type
== bfd_link_hash_defweak
)
9090 asection
*s
= h
->root
.u
.def
.section
;
9091 if (s
!= NULL
&& s
->output_section
!= NULL
)
9092 s
->output_section
->flags
|= SEC_KEEP
;
9098 /* Do mark and sweep of unused sections. */
9101 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
9103 bfd_boolean ok
= TRUE
;
9105 asection
* (*gc_mark_hook
)
9106 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
9107 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
9109 if (!info
->gc_sections
)
9111 /* If we are called when info->gc_sections is 0, we will mark
9112 all sections containing global symbols for non-relocatable
9114 if (!info
->relocatable
)
9115 elf_link_hash_traverse (elf_hash_table (info
),
9116 elf_mark_used_section
, NULL
);
9120 if (!get_elf_backend_data (abfd
)->can_gc_sections
9121 || info
->relocatable
9122 || info
->emitrelocations
9124 || !is_elf_hash_table (info
->hash
))
9126 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9130 /* Apply transitive closure to the vtable entry usage info. */
9131 elf_link_hash_traverse (elf_hash_table (info
),
9132 elf_gc_propagate_vtable_entries_used
,
9137 /* Kill the vtable relocations that were not used. */
9138 elf_link_hash_traverse (elf_hash_table (info
),
9139 elf_gc_smash_unused_vtentry_relocs
,
9144 /* Mark dynamically referenced symbols. */
9145 if (elf_hash_table (info
)->dynamic_sections_created
)
9146 elf_link_hash_traverse (elf_hash_table (info
),
9147 elf_gc_mark_dynamic_ref_symbol
,
9152 /* Grovel through relocs to find out who stays ... */
9153 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
9154 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9158 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9161 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9163 if (o
->flags
& SEC_KEEP
)
9165 /* _bfd_elf_discard_section_eh_frame knows how to discard
9166 orphaned FDEs so don't mark sections referenced by the
9167 EH frame section. */
9168 if (strcmp (o
->name
, ".eh_frame") == 0)
9170 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9176 /* ... and mark SEC_EXCLUDE for those that go. */
9177 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
9183 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9186 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9188 struct elf_link_hash_entry
*h
,
9191 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9192 struct elf_link_hash_entry
**search
, *child
;
9193 bfd_size_type extsymcount
;
9194 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9196 /* The sh_info field of the symtab header tells us where the
9197 external symbols start. We don't care about the local symbols at
9199 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9200 if (!elf_bad_symtab (abfd
))
9201 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9203 sym_hashes
= elf_sym_hashes (abfd
);
9204 sym_hashes_end
= sym_hashes
+ extsymcount
;
9206 /* Hunt down the child symbol, which is in this section at the same
9207 offset as the relocation. */
9208 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9210 if ((child
= *search
) != NULL
9211 && (child
->root
.type
== bfd_link_hash_defined
9212 || child
->root
.type
== bfd_link_hash_defweak
)
9213 && child
->root
.u
.def
.section
== sec
9214 && child
->root
.u
.def
.value
== offset
)
9218 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9219 abfd
, sec
, (unsigned long) offset
);
9220 bfd_set_error (bfd_error_invalid_operation
);
9226 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9232 /* This *should* only be the absolute section. It could potentially
9233 be that someone has defined a non-global vtable though, which
9234 would be bad. It isn't worth paging in the local symbols to be
9235 sure though; that case should simply be handled by the assembler. */
9237 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9240 child
->vtable
->parent
= h
;
9245 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9248 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9249 asection
*sec ATTRIBUTE_UNUSED
,
9250 struct elf_link_hash_entry
*h
,
9253 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9254 unsigned int log_file_align
= bed
->s
->log_file_align
;
9258 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9263 if (addend
>= h
->vtable
->size
)
9265 size_t size
, bytes
, file_align
;
9266 bfd_boolean
*ptr
= h
->vtable
->used
;
9268 /* While the symbol is undefined, we have to be prepared to handle
9270 file_align
= 1 << log_file_align
;
9271 if (h
->root
.type
== bfd_link_hash_undefined
)
9272 size
= addend
+ file_align
;
9278 /* Oops! We've got a reference past the defined end of
9279 the table. This is probably a bug -- shall we warn? */
9280 size
= addend
+ file_align
;
9283 size
= (size
+ file_align
- 1) & -file_align
;
9285 /* Allocate one extra entry for use as a "done" flag for the
9286 consolidation pass. */
9287 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9291 ptr
= bfd_realloc (ptr
- 1, bytes
);
9297 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9298 * sizeof (bfd_boolean
));
9299 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9303 ptr
= bfd_zmalloc (bytes
);
9308 /* And arrange for that done flag to be at index -1. */
9309 h
->vtable
->used
= ptr
+ 1;
9310 h
->vtable
->size
= size
;
9313 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9318 struct alloc_got_off_arg
{
9320 unsigned int got_elt_size
;
9323 /* We need a special top-level link routine to convert got reference counts
9324 to real got offsets. */
9327 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9329 struct alloc_got_off_arg
*gofarg
= arg
;
9331 if (h
->root
.type
== bfd_link_hash_warning
)
9332 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9334 if (h
->got
.refcount
> 0)
9336 h
->got
.offset
= gofarg
->gotoff
;
9337 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9340 h
->got
.offset
= (bfd_vma
) -1;
9345 /* And an accompanying bit to work out final got entry offsets once
9346 we're done. Should be called from final_link. */
9349 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9350 struct bfd_link_info
*info
)
9353 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9355 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9356 struct alloc_got_off_arg gofarg
;
9358 if (! is_elf_hash_table (info
->hash
))
9361 /* The GOT offset is relative to the .got section, but the GOT header is
9362 put into the .got.plt section, if the backend uses it. */
9363 if (bed
->want_got_plt
)
9366 gotoff
= bed
->got_header_size
;
9368 /* Do the local .got entries first. */
9369 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9371 bfd_signed_vma
*local_got
;
9372 bfd_size_type j
, locsymcount
;
9373 Elf_Internal_Shdr
*symtab_hdr
;
9375 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9378 local_got
= elf_local_got_refcounts (i
);
9382 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9383 if (elf_bad_symtab (i
))
9384 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9386 locsymcount
= symtab_hdr
->sh_info
;
9388 for (j
= 0; j
< locsymcount
; ++j
)
9390 if (local_got
[j
] > 0)
9392 local_got
[j
] = gotoff
;
9393 gotoff
+= got_elt_size
;
9396 local_got
[j
] = (bfd_vma
) -1;
9400 /* Then the global .got entries. .plt refcounts are handled by
9401 adjust_dynamic_symbol */
9402 gofarg
.gotoff
= gotoff
;
9403 gofarg
.got_elt_size
= got_elt_size
;
9404 elf_link_hash_traverse (elf_hash_table (info
),
9405 elf_gc_allocate_got_offsets
,
9410 /* Many folk need no more in the way of final link than this, once
9411 got entry reference counting is enabled. */
9414 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9416 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9419 /* Invoke the regular ELF backend linker to do all the work. */
9420 return bfd_elf_final_link (abfd
, info
);
9424 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9426 struct elf_reloc_cookie
*rcookie
= cookie
;
9428 if (rcookie
->bad_symtab
)
9429 rcookie
->rel
= rcookie
->rels
;
9431 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9433 unsigned long r_symndx
;
9435 if (! rcookie
->bad_symtab
)
9436 if (rcookie
->rel
->r_offset
> offset
)
9438 if (rcookie
->rel
->r_offset
!= offset
)
9441 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9442 if (r_symndx
== SHN_UNDEF
)
9445 if (r_symndx
>= rcookie
->locsymcount
9446 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9448 struct elf_link_hash_entry
*h
;
9450 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9452 while (h
->root
.type
== bfd_link_hash_indirect
9453 || h
->root
.type
== bfd_link_hash_warning
)
9454 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9456 if ((h
->root
.type
== bfd_link_hash_defined
9457 || h
->root
.type
== bfd_link_hash_defweak
)
9458 && elf_discarded_section (h
->root
.u
.def
.section
))
9465 /* It's not a relocation against a global symbol,
9466 but it could be a relocation against a local
9467 symbol for a discarded section. */
9469 Elf_Internal_Sym
*isym
;
9471 /* Need to: get the symbol; get the section. */
9472 isym
= &rcookie
->locsyms
[r_symndx
];
9473 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9475 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9476 if (isec
!= NULL
&& elf_discarded_section (isec
))
9485 /* Discard unneeded references to discarded sections.
9486 Returns TRUE if any section's size was changed. */
9487 /* This function assumes that the relocations are in sorted order,
9488 which is true for all known assemblers. */
9491 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9493 struct elf_reloc_cookie cookie
;
9494 asection
*stab
, *eh
;
9495 Elf_Internal_Shdr
*symtab_hdr
;
9496 const struct elf_backend_data
*bed
;
9499 bfd_boolean ret
= FALSE
;
9501 if (info
->traditional_format
9502 || !is_elf_hash_table (info
->hash
))
9505 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9507 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9510 bed
= get_elf_backend_data (abfd
);
9512 if ((abfd
->flags
& DYNAMIC
) != 0)
9515 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9516 if (info
->relocatable
9519 || bfd_is_abs_section (eh
->output_section
))))
9522 stab
= bfd_get_section_by_name (abfd
, ".stab");
9525 || bfd_is_abs_section (stab
->output_section
)
9526 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9531 && bed
->elf_backend_discard_info
== NULL
)
9534 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9536 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9537 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9538 if (cookie
.bad_symtab
)
9540 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9541 cookie
.extsymoff
= 0;
9545 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9546 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9549 if (bed
->s
->arch_size
== 32)
9550 cookie
.r_sym_shift
= 8;
9552 cookie
.r_sym_shift
= 32;
9554 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9555 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9557 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9558 cookie
.locsymcount
, 0,
9560 if (cookie
.locsyms
== NULL
)
9567 count
= stab
->reloc_count
;
9569 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9571 if (cookie
.rels
!= NULL
)
9573 cookie
.rel
= cookie
.rels
;
9574 cookie
.relend
= cookie
.rels
;
9575 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9576 if (_bfd_discard_section_stabs (abfd
, stab
,
9577 elf_section_data (stab
)->sec_info
,
9578 bfd_elf_reloc_symbol_deleted_p
,
9581 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9589 count
= eh
->reloc_count
;
9591 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9593 cookie
.rel
= cookie
.rels
;
9594 cookie
.relend
= cookie
.rels
;
9595 if (cookie
.rels
!= NULL
)
9596 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9598 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9599 bfd_elf_reloc_symbol_deleted_p
,
9603 if (cookie
.rels
!= NULL
9604 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9608 if (bed
->elf_backend_discard_info
!= NULL
9609 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9612 if (cookie
.locsyms
!= NULL
9613 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9615 if (! info
->keep_memory
)
9616 free (cookie
.locsyms
);
9618 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9622 if (info
->eh_frame_hdr
9623 && !info
->relocatable
9624 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9631 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9634 const char *name
, *p
;
9635 struct bfd_section_already_linked
*l
;
9636 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9639 /* A single member comdat group section may be discarded by a
9640 linkonce section. See below. */
9641 if (sec
->output_section
== bfd_abs_section_ptr
)
9646 /* Check if it belongs to a section group. */
9647 group
= elf_sec_group (sec
);
9649 /* Return if it isn't a linkonce section nor a member of a group. A
9650 comdat group section also has SEC_LINK_ONCE set. */
9651 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9656 /* If this is the member of a single member comdat group, check if
9657 the group should be discarded. */
9658 if (elf_next_in_group (sec
) == sec
9659 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9665 /* FIXME: When doing a relocatable link, we may have trouble
9666 copying relocations in other sections that refer to local symbols
9667 in the section being discarded. Those relocations will have to
9668 be converted somehow; as of this writing I'm not sure that any of
9669 the backends handle that correctly.
9671 It is tempting to instead not discard link once sections when
9672 doing a relocatable link (technically, they should be discarded
9673 whenever we are building constructors). However, that fails,
9674 because the linker winds up combining all the link once sections
9675 into a single large link once section, which defeats the purpose
9676 of having link once sections in the first place.
9678 Also, not merging link once sections in a relocatable link
9679 causes trouble for MIPS ELF, which relies on link once semantics
9680 to handle the .reginfo section correctly. */
9682 name
= bfd_get_section_name (abfd
, sec
);
9684 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9685 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9690 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9692 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9694 /* We may have 3 different sections on the list: group section,
9695 comdat section and linkonce section. SEC may be a linkonce or
9696 group section. We match a group section with a group section,
9697 a linkonce section with a linkonce section, and ignore comdat
9699 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9700 && strcmp (name
, l
->sec
->name
) == 0
9701 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9703 /* The section has already been linked. See if we should
9705 switch (flags
& SEC_LINK_DUPLICATES
)
9710 case SEC_LINK_DUPLICATES_DISCARD
:
9713 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9714 (*_bfd_error_handler
)
9715 (_("%B: ignoring duplicate section `%A'"),
9719 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9720 if (sec
->size
!= l
->sec
->size
)
9721 (*_bfd_error_handler
)
9722 (_("%B: duplicate section `%A' has different size"),
9726 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9727 if (sec
->size
!= l
->sec
->size
)
9728 (*_bfd_error_handler
)
9729 (_("%B: duplicate section `%A' has different size"),
9731 else if (sec
->size
!= 0)
9733 bfd_byte
*sec_contents
, *l_sec_contents
;
9735 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9736 (*_bfd_error_handler
)
9737 (_("%B: warning: could not read contents of section `%A'"),
9739 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9741 (*_bfd_error_handler
)
9742 (_("%B: warning: could not read contents of section `%A'"),
9743 l
->sec
->owner
, l
->sec
);
9744 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9745 (*_bfd_error_handler
)
9746 (_("%B: warning: duplicate section `%A' has different contents"),
9750 free (sec_contents
);
9752 free (l_sec_contents
);
9757 /* Set the output_section field so that lang_add_section
9758 does not create a lang_input_section structure for this
9759 section. Since there might be a symbol in the section
9760 being discarded, we must retain a pointer to the section
9761 which we are really going to use. */
9762 sec
->output_section
= bfd_abs_section_ptr
;
9763 sec
->kept_section
= l
->sec
;
9765 if (flags
& SEC_GROUP
)
9767 asection
*first
= elf_next_in_group (sec
);
9768 asection
*s
= first
;
9772 s
->output_section
= bfd_abs_section_ptr
;
9773 /* Record which group discards it. */
9774 s
->kept_section
= l
->sec
;
9775 s
= elf_next_in_group (s
);
9776 /* These lists are circular. */
9788 /* If this is the member of a single member comdat group and the
9789 group hasn't be discarded, we check if it matches a linkonce
9790 section. We only record the discarded comdat group. Otherwise
9791 the undiscarded group will be discarded incorrectly later since
9792 itself has been recorded. */
9793 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9794 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9795 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9796 && bfd_elf_match_symbols_in_sections (l
->sec
,
9797 elf_next_in_group (sec
)))
9799 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9800 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9801 group
->output_section
= bfd_abs_section_ptr
;
9808 /* There is no direct match. But for linkonce section, we should
9809 check if there is a match with comdat group member. We always
9810 record the linkonce section, discarded or not. */
9811 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9812 if (l
->sec
->flags
& SEC_GROUP
)
9814 asection
*first
= elf_next_in_group (l
->sec
);
9817 && elf_next_in_group (first
) == first
9818 && bfd_elf_match_symbols_in_sections (first
, sec
))
9820 sec
->output_section
= bfd_abs_section_ptr
;
9821 sec
->kept_section
= l
->sec
;
9826 /* This is the first section with this name. Record it. */
9827 bfd_section_already_linked_table_insert (already_linked_list
, sec
);
9830 /* Set NAME to VAL if the symbol exists and is undefined. */
9833 _bfd_elf_provide_symbol (struct bfd_link_info
*info
, const char *name
,
9836 struct elf_link_hash_entry
*h
;
9838 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
,
9840 if (h
!= NULL
&& (h
->root
.type
== bfd_link_hash_undefined
9841 || h
->root
.type
== bfd_link_hash_undefweak
))
9843 h
->root
.type
= bfd_link_hash_defined
;
9844 h
->root
.u
.def
.section
= bfd_abs_section_ptr
;
9845 h
->root
.u
.def
.value
= val
;
9847 h
->type
= STT_OBJECT
;
9848 h
->other
= STV_HIDDEN
| (h
->other
& ~ ELF_ST_VISIBILITY (-1));
9849 h
->forced_local
= 1;