1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
31 _bfd_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
35 struct elf_link_hash_entry
*h
;
36 struct bfd_link_hash_entry
*bh
;
37 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
40 /* This function may be called more than once. */
41 s
= bfd_get_section_by_name (abfd
, ".got");
42 if (s
!= NULL
&& (s
->flags
& SEC_LINKER_CREATED
) != 0)
45 switch (bed
->s
->arch_size
)
56 bfd_set_error (bfd_error_bad_value
);
60 flags
= bed
->dynamic_sec_flags
;
62 s
= bfd_make_section (abfd
, ".got");
64 || !bfd_set_section_flags (abfd
, s
, flags
)
65 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
68 if (bed
->want_got_plt
)
70 s
= bfd_make_section (abfd
, ".got.plt");
72 || !bfd_set_section_flags (abfd
, s
, flags
)
73 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
77 if (bed
->want_got_sym
)
79 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
80 (or .got.plt) section. We don't do this in the linker script
81 because we don't want to define the symbol if we are not creating
82 a global offset table. */
84 if (!(_bfd_generic_link_add_one_symbol
85 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
86 bed
->got_symbol_offset
, NULL
, FALSE
, bed
->collect
, &bh
)))
88 h
= (struct elf_link_hash_entry
*) bh
;
91 h
->other
= STV_HIDDEN
;
93 if (! info
->executable
94 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
97 elf_hash_table (info
)->hgot
= h
;
100 /* The first bit of the global offset table is the header. */
101 s
->size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
106 /* Create a strtab to hold the dynamic symbol names. */
108 _bfd_elf_link_create_dynstrtab (bfd
*abfd
, struct bfd_link_info
*info
)
110 struct elf_link_hash_table
*hash_table
;
112 hash_table
= elf_hash_table (info
);
113 if (hash_table
->dynobj
== NULL
)
114 hash_table
->dynobj
= abfd
;
116 if (hash_table
->dynstr
== NULL
)
118 hash_table
->dynstr
= _bfd_elf_strtab_init ();
119 if (hash_table
->dynstr
== NULL
)
125 /* Create some sections which will be filled in with dynamic linking
126 information. ABFD is an input file which requires dynamic sections
127 to be created. The dynamic sections take up virtual memory space
128 when the final executable is run, so we need to create them before
129 addresses are assigned to the output sections. We work out the
130 actual contents and size of these sections later. */
133 _bfd_elf_link_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
136 register asection
*s
;
137 struct elf_link_hash_entry
*h
;
138 struct bfd_link_hash_entry
*bh
;
139 const struct elf_backend_data
*bed
;
141 if (! is_elf_hash_table (info
->hash
))
144 if (elf_hash_table (info
)->dynamic_sections_created
)
147 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
150 abfd
= elf_hash_table (info
)->dynobj
;
151 bed
= get_elf_backend_data (abfd
);
153 flags
= bed
->dynamic_sec_flags
;
155 /* A dynamically linked executable has a .interp section, but a
156 shared library does not. */
157 if (info
->executable
)
159 s
= bfd_make_section (abfd
, ".interp");
161 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
165 if (! info
->traditional_format
)
167 s
= bfd_make_section (abfd
, ".eh_frame_hdr");
169 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
170 || ! bfd_set_section_alignment (abfd
, s
, 2))
172 elf_hash_table (info
)->eh_info
.hdr_sec
= s
;
175 /* Create sections to hold version informations. These are removed
176 if they are not needed. */
177 s
= bfd_make_section (abfd
, ".gnu.version_d");
179 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
180 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
183 s
= bfd_make_section (abfd
, ".gnu.version");
185 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
186 || ! bfd_set_section_alignment (abfd
, s
, 1))
189 s
= bfd_make_section (abfd
, ".gnu.version_r");
191 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
192 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
195 s
= bfd_make_section (abfd
, ".dynsym");
197 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
198 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
201 s
= bfd_make_section (abfd
, ".dynstr");
203 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
))
206 s
= bfd_make_section (abfd
, ".dynamic");
208 || ! bfd_set_section_flags (abfd
, s
, flags
)
209 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
212 /* The special symbol _DYNAMIC is always set to the start of the
213 .dynamic section. We could set _DYNAMIC in a linker script, but we
214 only want to define it if we are, in fact, creating a .dynamic
215 section. We don't want to define it if there is no .dynamic
216 section, since on some ELF platforms the start up code examines it
217 to decide how to initialize the process. */
218 h
= elf_link_hash_lookup (elf_hash_table (info
), "_DYNAMIC",
219 FALSE
, FALSE
, FALSE
);
222 /* Zap symbol defined in an as-needed lib that wasn't linked.
223 This is a symptom of a larger problem: Absolute symbols
224 defined in shared libraries can't be overridden, because we
225 lose the link to the bfd which is via the symbol section. */
226 h
->root
.type
= bfd_link_hash_new
;
229 if (! (_bfd_generic_link_add_one_symbol
230 (info
, abfd
, "_DYNAMIC", BSF_GLOBAL
, s
, 0, NULL
, FALSE
,
231 get_elf_backend_data (abfd
)->collect
, &bh
)))
233 h
= (struct elf_link_hash_entry
*) bh
;
235 h
->type
= STT_OBJECT
;
237 if (! info
->executable
238 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
241 s
= bfd_make_section (abfd
, ".hash");
243 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
244 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
246 elf_section_data (s
)->this_hdr
.sh_entsize
= bed
->s
->sizeof_hash_entry
;
248 /* Let the backend create the rest of the sections. This lets the
249 backend set the right flags. The backend will normally create
250 the .got and .plt sections. */
251 if (! (*bed
->elf_backend_create_dynamic_sections
) (abfd
, info
))
254 elf_hash_table (info
)->dynamic_sections_created
= TRUE
;
259 /* Create dynamic sections when linking against a dynamic object. */
262 _bfd_elf_create_dynamic_sections (bfd
*abfd
, struct bfd_link_info
*info
)
264 flagword flags
, pltflags
;
266 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
268 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
269 .rel[a].bss sections. */
270 flags
= bed
->dynamic_sec_flags
;
273 if (bed
->plt_not_loaded
)
274 /* We do not clear SEC_ALLOC here because we still want the OS to
275 allocate space for the section; it's just that there's nothing
276 to read in from the object file. */
277 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
279 pltflags
|= SEC_ALLOC
| SEC_CODE
| SEC_LOAD
;
280 if (bed
->plt_readonly
)
281 pltflags
|= SEC_READONLY
;
283 s
= bfd_make_section (abfd
, ".plt");
285 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
286 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
289 if (bed
->want_plt_sym
)
291 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
293 struct elf_link_hash_entry
*h
;
294 struct bfd_link_hash_entry
*bh
= NULL
;
296 if (! (_bfd_generic_link_add_one_symbol
297 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
, 0, NULL
,
298 FALSE
, get_elf_backend_data (abfd
)->collect
, &bh
)))
300 h
= (struct elf_link_hash_entry
*) bh
;
302 h
->type
= STT_OBJECT
;
304 if (! info
->executable
305 && ! bfd_elf_link_record_dynamic_symbol (info
, h
))
309 s
= bfd_make_section (abfd
,
310 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
312 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
313 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
316 if (! _bfd_elf_create_got_section (abfd
, info
))
319 if (bed
->want_dynbss
)
321 /* The .dynbss section is a place to put symbols which are defined
322 by dynamic objects, are referenced by regular objects, and are
323 not functions. We must allocate space for them in the process
324 image and use a R_*_COPY reloc to tell the dynamic linker to
325 initialize them at run time. The linker script puts the .dynbss
326 section into the .bss section of the final image. */
327 s
= bfd_make_section (abfd
, ".dynbss");
329 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
| SEC_LINKER_CREATED
))
332 /* The .rel[a].bss section holds copy relocs. This section is not
333 normally needed. We need to create it here, though, so that the
334 linker will map it to an output section. We can't just create it
335 only if we need it, because we will not know whether we need it
336 until we have seen all the input files, and the first time the
337 main linker code calls BFD after examining all the input files
338 (size_dynamic_sections) the input sections have already been
339 mapped to the output sections. If the section turns out not to
340 be needed, we can discard it later. We will never need this
341 section when generating a shared object, since they do not use
345 s
= bfd_make_section (abfd
,
346 (bed
->default_use_rela_p
347 ? ".rela.bss" : ".rel.bss"));
349 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
350 || ! bfd_set_section_alignment (abfd
, s
, bed
->s
->log_file_align
))
358 /* Record a new dynamic symbol. We record the dynamic symbols as we
359 read the input files, since we need to have a list of all of them
360 before we can determine the final sizes of the output sections.
361 Note that we may actually call this function even though we are not
362 going to output any dynamic symbols; in some cases we know that a
363 symbol should be in the dynamic symbol table, but only if there is
367 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info
*info
,
368 struct elf_link_hash_entry
*h
)
370 if (h
->dynindx
== -1)
372 struct elf_strtab_hash
*dynstr
;
377 /* XXX: The ABI draft says the linker must turn hidden and
378 internal symbols into STB_LOCAL symbols when producing the
379 DSO. However, if ld.so honors st_other in the dynamic table,
380 this would not be necessary. */
381 switch (ELF_ST_VISIBILITY (h
->other
))
385 if (h
->root
.type
!= bfd_link_hash_undefined
386 && h
->root
.type
!= bfd_link_hash_undefweak
)
396 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
397 ++elf_hash_table (info
)->dynsymcount
;
399 dynstr
= elf_hash_table (info
)->dynstr
;
402 /* Create a strtab to hold the dynamic symbol names. */
403 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
408 /* We don't put any version information in the dynamic string
410 name
= h
->root
.root
.string
;
411 p
= strchr (name
, ELF_VER_CHR
);
413 /* We know that the p points into writable memory. In fact,
414 there are only a few symbols that have read-only names, being
415 those like _GLOBAL_OFFSET_TABLE_ that are created specially
416 by the backends. Most symbols will have names pointing into
417 an ELF string table read from a file, or to objalloc memory. */
420 indx
= _bfd_elf_strtab_add (dynstr
, name
, p
!= NULL
);
425 if (indx
== (bfd_size_type
) -1)
427 h
->dynstr_index
= indx
;
433 /* Record an assignment to a symbol made by a linker script. We need
434 this in case some dynamic object refers to this symbol. */
437 bfd_elf_record_link_assignment (bfd
*output_bfd ATTRIBUTE_UNUSED
,
438 struct bfd_link_info
*info
,
442 struct elf_link_hash_entry
*h
;
443 struct elf_link_hash_table
*htab
;
445 if (!is_elf_hash_table (info
->hash
))
448 htab
= elf_hash_table (info
);
449 h
= elf_link_hash_lookup (htab
, name
, !provide
, TRUE
, FALSE
);
453 /* Since we're defining the symbol, don't let it seem to have not
454 been defined. record_dynamic_symbol and size_dynamic_sections
455 may depend on this. */
456 if (h
->root
.type
== bfd_link_hash_undefweak
457 || h
->root
.type
== bfd_link_hash_undefined
)
459 h
->root
.type
= bfd_link_hash_new
;
460 if (h
->root
.u
.undef
.next
!= NULL
|| htab
->root
.undefs_tail
== &h
->root
)
461 bfd_link_repair_undef_list (&htab
->root
);
464 if (h
->root
.type
== bfd_link_hash_new
)
467 /* If this symbol is being provided by the linker script, and it is
468 currently defined by a dynamic object, but not by a regular
469 object, then mark it as undefined so that the generic linker will
470 force the correct value. */
474 h
->root
.type
= bfd_link_hash_undefined
;
476 /* If this symbol is not being provided by the linker script, and it is
477 currently defined by a dynamic object, but not by a regular object,
478 then clear out any version information because the symbol will not be
479 associated with the dynamic object any more. */
483 h
->verinfo
.verdef
= NULL
;
487 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
489 if (!info
->relocatable
491 && (ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
492 || ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
))
500 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
503 /* If this is a weak defined symbol, and we know a corresponding
504 real symbol from the same dynamic object, make sure the real
505 symbol is also made into a dynamic symbol. */
506 if (h
->u
.weakdef
!= NULL
507 && h
->u
.weakdef
->dynindx
== -1)
509 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
517 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
518 success, and 2 on a failure caused by attempting to record a symbol
519 in a discarded section, eg. a discarded link-once section symbol. */
522 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info
*info
,
527 struct elf_link_local_dynamic_entry
*entry
;
528 struct elf_link_hash_table
*eht
;
529 struct elf_strtab_hash
*dynstr
;
530 unsigned long dynstr_index
;
532 Elf_External_Sym_Shndx eshndx
;
533 char esym
[sizeof (Elf64_External_Sym
)];
535 if (! is_elf_hash_table (info
->hash
))
538 /* See if the entry exists already. */
539 for (entry
= elf_hash_table (info
)->dynlocal
; entry
; entry
= entry
->next
)
540 if (entry
->input_bfd
== input_bfd
&& entry
->input_indx
== input_indx
)
543 amt
= sizeof (*entry
);
544 entry
= bfd_alloc (input_bfd
, amt
);
548 /* Go find the symbol, so that we can find it's name. */
549 if (!bfd_elf_get_elf_syms (input_bfd
, &elf_tdata (input_bfd
)->symtab_hdr
,
550 1, input_indx
, &entry
->isym
, esym
, &eshndx
))
552 bfd_release (input_bfd
, entry
);
556 if (entry
->isym
.st_shndx
!= SHN_UNDEF
557 && (entry
->isym
.st_shndx
< SHN_LORESERVE
558 || entry
->isym
.st_shndx
> SHN_HIRESERVE
))
562 s
= bfd_section_from_elf_index (input_bfd
, entry
->isym
.st_shndx
);
563 if (s
== NULL
|| bfd_is_abs_section (s
->output_section
))
565 /* We can still bfd_release here as nothing has done another
566 bfd_alloc. We can't do this later in this function. */
567 bfd_release (input_bfd
, entry
);
572 name
= (bfd_elf_string_from_elf_section
573 (input_bfd
, elf_tdata (input_bfd
)->symtab_hdr
.sh_link
,
574 entry
->isym
.st_name
));
576 dynstr
= elf_hash_table (info
)->dynstr
;
579 /* Create a strtab to hold the dynamic symbol names. */
580 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
585 dynstr_index
= _bfd_elf_strtab_add (dynstr
, name
, FALSE
);
586 if (dynstr_index
== (unsigned long) -1)
588 entry
->isym
.st_name
= dynstr_index
;
590 eht
= elf_hash_table (info
);
592 entry
->next
= eht
->dynlocal
;
593 eht
->dynlocal
= entry
;
594 entry
->input_bfd
= input_bfd
;
595 entry
->input_indx
= input_indx
;
598 /* Whatever binding the symbol had before, it's now local. */
600 = ELF_ST_INFO (STB_LOCAL
, ELF_ST_TYPE (entry
->isym
.st_info
));
602 /* The dynindx will be set at the end of size_dynamic_sections. */
607 /* Return the dynindex of a local dynamic symbol. */
610 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info
*info
,
614 struct elf_link_local_dynamic_entry
*e
;
616 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
617 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
622 /* This function is used to renumber the dynamic symbols, if some of
623 them are removed because they are marked as local. This is called
624 via elf_link_hash_traverse. */
627 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
630 size_t *count
= data
;
632 if (h
->root
.type
== bfd_link_hash_warning
)
633 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
638 if (h
->dynindx
!= -1)
639 h
->dynindx
= ++(*count
);
645 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
646 STB_LOCAL binding. */
649 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry
*h
,
652 size_t *count
= data
;
654 if (h
->root
.type
== bfd_link_hash_warning
)
655 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
657 if (!h
->forced_local
)
660 if (h
->dynindx
!= -1)
661 h
->dynindx
= ++(*count
);
666 /* Return true if the dynamic symbol for a given section should be
667 omitted when creating a shared library. */
669 _bfd_elf_link_omit_section_dynsym (bfd
*output_bfd ATTRIBUTE_UNUSED
,
670 struct bfd_link_info
*info
,
673 switch (elf_section_data (p
)->this_hdr
.sh_type
)
677 /* If sh_type is yet undecided, assume it could be
678 SHT_PROGBITS/SHT_NOBITS. */
680 if (strcmp (p
->name
, ".got") == 0
681 || strcmp (p
->name
, ".got.plt") == 0
682 || strcmp (p
->name
, ".plt") == 0)
685 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
688 && (ip
= bfd_get_section_by_name (dynobj
, p
->name
)) != NULL
689 && (ip
->flags
& SEC_LINKER_CREATED
)
690 && ip
->output_section
== p
)
695 /* There shouldn't be section relative relocations
696 against any other section. */
702 /* Assign dynsym indices. In a shared library we generate a section
703 symbol for each output section, which come first. Next come symbols
704 which have been forced to local binding. Then all of the back-end
705 allocated local dynamic syms, followed by the rest of the global
709 _bfd_elf_link_renumber_dynsyms (bfd
*output_bfd
, struct bfd_link_info
*info
)
711 unsigned long dynsymcount
= 0;
715 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
717 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
718 if ((p
->flags
& SEC_EXCLUDE
) == 0
719 && (p
->flags
& SEC_ALLOC
) != 0
720 && !(*bed
->elf_backend_omit_section_dynsym
) (output_bfd
, info
, p
))
721 elf_section_data (p
)->dynindx
= ++dynsymcount
;
724 elf_link_hash_traverse (elf_hash_table (info
),
725 elf_link_renumber_local_hash_table_dynsyms
,
728 if (elf_hash_table (info
)->dynlocal
)
730 struct elf_link_local_dynamic_entry
*p
;
731 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
732 p
->dynindx
= ++dynsymcount
;
735 elf_link_hash_traverse (elf_hash_table (info
),
736 elf_link_renumber_hash_table_dynsyms
,
739 /* There is an unused NULL entry at the head of the table which
740 we must account for in our count. Unless there weren't any
741 symbols, which means we'll have no table at all. */
742 if (dynsymcount
!= 0)
745 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
748 /* This function is called when we want to define a new symbol. It
749 handles the various cases which arise when we find a definition in
750 a dynamic object, or when there is already a definition in a
751 dynamic object. The new symbol is described by NAME, SYM, PSEC,
752 and PVALUE. We set SYM_HASH to the hash table entry. We set
753 OVERRIDE if the old symbol is overriding a new definition. We set
754 TYPE_CHANGE_OK if it is OK for the type to change. We set
755 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
756 change, we mean that we shouldn't warn if the type or size does
760 _bfd_elf_merge_symbol (bfd
*abfd
,
761 struct bfd_link_info
*info
,
763 Elf_Internal_Sym
*sym
,
766 struct elf_link_hash_entry
**sym_hash
,
768 bfd_boolean
*override
,
769 bfd_boolean
*type_change_ok
,
770 bfd_boolean
*size_change_ok
)
772 asection
*sec
, *oldsec
;
773 struct elf_link_hash_entry
*h
;
774 struct elf_link_hash_entry
*flip
;
777 bfd_boolean newdyn
, olddyn
, olddef
, newdef
, newdyncommon
, olddyncommon
;
778 bfd_boolean newweak
, oldweak
;
784 bind
= ELF_ST_BIND (sym
->st_info
);
786 if (! bfd_is_und_section (sec
))
787 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
, FALSE
, FALSE
);
789 h
= ((struct elf_link_hash_entry
*)
790 bfd_wrapped_link_hash_lookup (abfd
, info
, name
, TRUE
, FALSE
, FALSE
));
795 /* This code is for coping with dynamic objects, and is only useful
796 if we are doing an ELF link. */
797 if (info
->hash
->creator
!= abfd
->xvec
)
800 /* For merging, we only care about real symbols. */
802 while (h
->root
.type
== bfd_link_hash_indirect
803 || h
->root
.type
== bfd_link_hash_warning
)
804 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
806 /* If we just created the symbol, mark it as being an ELF symbol.
807 Other than that, there is nothing to do--there is no merge issue
808 with a newly defined symbol--so we just return. */
810 if (h
->root
.type
== bfd_link_hash_new
)
816 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
819 switch (h
->root
.type
)
826 case bfd_link_hash_undefined
:
827 case bfd_link_hash_undefweak
:
828 oldbfd
= h
->root
.u
.undef
.abfd
;
832 case bfd_link_hash_defined
:
833 case bfd_link_hash_defweak
:
834 oldbfd
= h
->root
.u
.def
.section
->owner
;
835 oldsec
= h
->root
.u
.def
.section
;
838 case bfd_link_hash_common
:
839 oldbfd
= h
->root
.u
.c
.p
->section
->owner
;
840 oldsec
= h
->root
.u
.c
.p
->section
;
844 /* In cases involving weak versioned symbols, we may wind up trying
845 to merge a symbol with itself. Catch that here, to avoid the
846 confusion that results if we try to override a symbol with
847 itself. The additional tests catch cases like
848 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
849 dynamic object, which we do want to handle here. */
851 && ((abfd
->flags
& DYNAMIC
) == 0
855 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
856 respectively, is from a dynamic object. */
858 if ((abfd
->flags
& DYNAMIC
) != 0)
864 olddyn
= (oldbfd
->flags
& DYNAMIC
) != 0;
869 /* This code handles the special SHN_MIPS_{TEXT,DATA} section
870 indices used by MIPS ELF. */
871 switch (h
->root
.type
)
877 case bfd_link_hash_defined
:
878 case bfd_link_hash_defweak
:
879 hsec
= h
->root
.u
.def
.section
;
882 case bfd_link_hash_common
:
883 hsec
= h
->root
.u
.c
.p
->section
;
890 olddyn
= (hsec
->symbol
->flags
& BSF_DYNAMIC
) != 0;
893 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
894 respectively, appear to be a definition rather than reference. */
896 if (bfd_is_und_section (sec
) || bfd_is_com_section (sec
))
901 if (h
->root
.type
== bfd_link_hash_undefined
902 || h
->root
.type
== bfd_link_hash_undefweak
903 || h
->root
.type
== bfd_link_hash_common
)
908 /* Check TLS symbol. */
909 if ((ELF_ST_TYPE (sym
->st_info
) == STT_TLS
|| h
->type
== STT_TLS
)
910 && ELF_ST_TYPE (sym
->st_info
) != h
->type
)
913 bfd_boolean ntdef
, tdef
;
914 asection
*ntsec
, *tsec
;
916 if (h
->type
== STT_TLS
)
936 (*_bfd_error_handler
)
937 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
938 tbfd
, tsec
, ntbfd
, ntsec
, h
->root
.root
.string
);
939 else if (!tdef
&& !ntdef
)
940 (*_bfd_error_handler
)
941 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
942 tbfd
, ntbfd
, h
->root
.root
.string
);
944 (*_bfd_error_handler
)
945 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
946 tbfd
, tsec
, ntbfd
, h
->root
.root
.string
);
948 (*_bfd_error_handler
)
949 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
950 tbfd
, ntbfd
, ntsec
, h
->root
.root
.string
);
952 bfd_set_error (bfd_error_bad_value
);
956 /* We need to remember if a symbol has a definition in a dynamic
957 object or is weak in all dynamic objects. Internal and hidden
958 visibility will make it unavailable to dynamic objects. */
959 if (newdyn
&& !h
->dynamic_def
)
961 if (!bfd_is_und_section (sec
))
965 /* Check if this symbol is weak in all dynamic objects. If it
966 is the first time we see it in a dynamic object, we mark
967 if it is weak. Otherwise, we clear it. */
970 if (bind
== STB_WEAK
)
973 else if (bind
!= STB_WEAK
)
978 /* If the old symbol has non-default visibility, we ignore the new
979 definition from a dynamic object. */
981 && ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
982 && !bfd_is_und_section (sec
))
985 /* Make sure this symbol is dynamic. */
987 /* A protected symbol has external availability. Make sure it is
990 FIXME: Should we check type and size for protected symbol? */
991 if (ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
992 return bfd_elf_link_record_dynamic_symbol (info
, h
);
997 && ELF_ST_VISIBILITY (sym
->st_other
) != STV_DEFAULT
1000 /* If the new symbol with non-default visibility comes from a
1001 relocatable file and the old definition comes from a dynamic
1002 object, we remove the old definition. */
1003 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1006 if ((h
->root
.u
.undef
.next
|| info
->hash
->undefs_tail
== &h
->root
)
1007 && bfd_is_und_section (sec
))
1009 /* If the new symbol is undefined and the old symbol was
1010 also undefined before, we need to make sure
1011 _bfd_generic_link_add_one_symbol doesn't mess
1012 up the linker hash table undefs list. Since the old
1013 definition came from a dynamic object, it is still on the
1015 h
->root
.type
= bfd_link_hash_undefined
;
1016 h
->root
.u
.undef
.abfd
= abfd
;
1020 h
->root
.type
= bfd_link_hash_new
;
1021 h
->root
.u
.undef
.abfd
= NULL
;
1030 /* FIXME: Should we check type and size for protected symbol? */
1036 /* Differentiate strong and weak symbols. */
1037 newweak
= bind
== STB_WEAK
;
1038 oldweak
= (h
->root
.type
== bfd_link_hash_defweak
1039 || h
->root
.type
== bfd_link_hash_undefweak
);
1041 /* If a new weak symbol definition comes from a regular file and the
1042 old symbol comes from a dynamic library, we treat the new one as
1043 strong. Similarly, an old weak symbol definition from a regular
1044 file is treated as strong when the new symbol comes from a dynamic
1045 library. Further, an old weak symbol from a dynamic library is
1046 treated as strong if the new symbol is from a dynamic library.
1047 This reflects the way glibc's ld.so works.
1049 Do this before setting *type_change_ok or *size_change_ok so that
1050 we warn properly when dynamic library symbols are overridden. */
1052 if (newdef
&& !newdyn
&& olddyn
)
1054 if (olddef
&& newdyn
)
1057 /* It's OK to change the type if either the existing symbol or the
1058 new symbol is weak. A type change is also OK if the old symbol
1059 is undefined and the new symbol is defined. */
1064 && h
->root
.type
== bfd_link_hash_undefined
))
1065 *type_change_ok
= TRUE
;
1067 /* It's OK to change the size if either the existing symbol or the
1068 new symbol is weak, or if the old symbol is undefined. */
1071 || h
->root
.type
== bfd_link_hash_undefined
)
1072 *size_change_ok
= TRUE
;
1074 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1075 symbol, respectively, appears to be a common symbol in a dynamic
1076 object. If a symbol appears in an uninitialized section, and is
1077 not weak, and is not a function, then it may be a common symbol
1078 which was resolved when the dynamic object was created. We want
1079 to treat such symbols specially, because they raise special
1080 considerations when setting the symbol size: if the symbol
1081 appears as a common symbol in a regular object, and the size in
1082 the regular object is larger, we must make sure that we use the
1083 larger size. This problematic case can always be avoided in C,
1084 but it must be handled correctly when using Fortran shared
1087 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1088 likewise for OLDDYNCOMMON and OLDDEF.
1090 Note that this test is just a heuristic, and that it is quite
1091 possible to have an uninitialized symbol in a shared object which
1092 is really a definition, rather than a common symbol. This could
1093 lead to some minor confusion when the symbol really is a common
1094 symbol in some regular object. However, I think it will be
1100 && (sec
->flags
& SEC_ALLOC
) != 0
1101 && (sec
->flags
& SEC_LOAD
) == 0
1103 && ELF_ST_TYPE (sym
->st_info
) != STT_FUNC
)
1104 newdyncommon
= TRUE
;
1106 newdyncommon
= FALSE
;
1110 && h
->root
.type
== bfd_link_hash_defined
1112 && (h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0
1113 && (h
->root
.u
.def
.section
->flags
& SEC_LOAD
) == 0
1115 && h
->type
!= STT_FUNC
)
1116 olddyncommon
= TRUE
;
1118 olddyncommon
= FALSE
;
1120 /* If both the old and the new symbols look like common symbols in a
1121 dynamic object, set the size of the symbol to the larger of the
1126 && sym
->st_size
!= h
->size
)
1128 /* Since we think we have two common symbols, issue a multiple
1129 common warning if desired. Note that we only warn if the
1130 size is different. If the size is the same, we simply let
1131 the old symbol override the new one as normally happens with
1132 symbols defined in dynamic objects. */
1134 if (! ((*info
->callbacks
->multiple_common
)
1135 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1136 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1139 if (sym
->st_size
> h
->size
)
1140 h
->size
= sym
->st_size
;
1142 *size_change_ok
= TRUE
;
1145 /* If we are looking at a dynamic object, and we have found a
1146 definition, we need to see if the symbol was already defined by
1147 some other object. If so, we want to use the existing
1148 definition, and we do not want to report a multiple symbol
1149 definition error; we do this by clobbering *PSEC to be
1150 bfd_und_section_ptr.
1152 We treat a common symbol as a definition if the symbol in the
1153 shared library is a function, since common symbols always
1154 represent variables; this can cause confusion in principle, but
1155 any such confusion would seem to indicate an erroneous program or
1156 shared library. We also permit a common symbol in a regular
1157 object to override a weak symbol in a shared object. */
1162 || (h
->root
.type
== bfd_link_hash_common
1164 || ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
))))
1168 newdyncommon
= FALSE
;
1170 *psec
= sec
= bfd_und_section_ptr
;
1171 *size_change_ok
= TRUE
;
1173 /* If we get here when the old symbol is a common symbol, then
1174 we are explicitly letting it override a weak symbol or
1175 function in a dynamic object, and we don't want to warn about
1176 a type change. If the old symbol is a defined symbol, a type
1177 change warning may still be appropriate. */
1179 if (h
->root
.type
== bfd_link_hash_common
)
1180 *type_change_ok
= TRUE
;
1183 /* Handle the special case of an old common symbol merging with a
1184 new symbol which looks like a common symbol in a shared object.
1185 We change *PSEC and *PVALUE to make the new symbol look like a
1186 common symbol, and let _bfd_generic_link_add_one_symbol will do
1190 && h
->root
.type
== bfd_link_hash_common
)
1194 newdyncommon
= FALSE
;
1195 *pvalue
= sym
->st_size
;
1196 *psec
= sec
= bfd_com_section_ptr
;
1197 *size_change_ok
= TRUE
;
1200 /* If the old symbol is from a dynamic object, and the new symbol is
1201 a definition which is not from a dynamic object, then the new
1202 symbol overrides the old symbol. Symbols from regular files
1203 always take precedence over symbols from dynamic objects, even if
1204 they are defined after the dynamic object in the link.
1206 As above, we again permit a common symbol in a regular object to
1207 override a definition in a shared object if the shared object
1208 symbol is a function or is weak. */
1213 || (bfd_is_com_section (sec
)
1215 || h
->type
== STT_FUNC
)))
1220 /* Change the hash table entry to undefined, and let
1221 _bfd_generic_link_add_one_symbol do the right thing with the
1224 h
->root
.type
= bfd_link_hash_undefined
;
1225 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1226 *size_change_ok
= TRUE
;
1229 olddyncommon
= FALSE
;
1231 /* We again permit a type change when a common symbol may be
1232 overriding a function. */
1234 if (bfd_is_com_section (sec
))
1235 *type_change_ok
= TRUE
;
1237 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1240 /* This union may have been set to be non-NULL when this symbol
1241 was seen in a dynamic object. We must force the union to be
1242 NULL, so that it is correct for a regular symbol. */
1243 h
->verinfo
.vertree
= NULL
;
1246 /* Handle the special case of a new common symbol merging with an
1247 old symbol that looks like it might be a common symbol defined in
1248 a shared object. Note that we have already handled the case in
1249 which a new common symbol should simply override the definition
1250 in the shared library. */
1253 && bfd_is_com_section (sec
)
1256 /* It would be best if we could set the hash table entry to a
1257 common symbol, but we don't know what to use for the section
1258 or the alignment. */
1259 if (! ((*info
->callbacks
->multiple_common
)
1260 (info
, h
->root
.root
.string
, oldbfd
, bfd_link_hash_common
,
1261 h
->size
, abfd
, bfd_link_hash_common
, sym
->st_size
)))
1264 /* If the presumed common symbol in the dynamic object is
1265 larger, pretend that the new symbol has its size. */
1267 if (h
->size
> *pvalue
)
1270 /* FIXME: We no longer know the alignment required by the symbol
1271 in the dynamic object, so we just wind up using the one from
1272 the regular object. */
1275 olddyncommon
= FALSE
;
1277 h
->root
.type
= bfd_link_hash_undefined
;
1278 h
->root
.u
.undef
.abfd
= h
->root
.u
.def
.section
->owner
;
1280 *size_change_ok
= TRUE
;
1281 *type_change_ok
= TRUE
;
1283 if ((*sym_hash
)->root
.type
== bfd_link_hash_indirect
)
1286 h
->verinfo
.vertree
= NULL
;
1291 /* Handle the case where we had a versioned symbol in a dynamic
1292 library and now find a definition in a normal object. In this
1293 case, we make the versioned symbol point to the normal one. */
1294 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1295 flip
->root
.type
= h
->root
.type
;
1296 h
->root
.type
= bfd_link_hash_indirect
;
1297 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) flip
;
1298 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, flip
, h
);
1299 flip
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.abfd
;
1303 flip
->ref_dynamic
= 1;
1310 /* This function is called to create an indirect symbol from the
1311 default for the symbol with the default version if needed. The
1312 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1313 set DYNSYM if the new indirect symbol is dynamic. */
1316 _bfd_elf_add_default_symbol (bfd
*abfd
,
1317 struct bfd_link_info
*info
,
1318 struct elf_link_hash_entry
*h
,
1320 Elf_Internal_Sym
*sym
,
1323 bfd_boolean
*dynsym
,
1324 bfd_boolean override
)
1326 bfd_boolean type_change_ok
;
1327 bfd_boolean size_change_ok
;
1330 struct elf_link_hash_entry
*hi
;
1331 struct bfd_link_hash_entry
*bh
;
1332 const struct elf_backend_data
*bed
;
1333 bfd_boolean collect
;
1334 bfd_boolean dynamic
;
1336 size_t len
, shortlen
;
1339 /* If this symbol has a version, and it is the default version, we
1340 create an indirect symbol from the default name to the fully
1341 decorated name. This will cause external references which do not
1342 specify a version to be bound to this version of the symbol. */
1343 p
= strchr (name
, ELF_VER_CHR
);
1344 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
1349 /* We are overridden by an old definition. We need to check if we
1350 need to create the indirect symbol from the default name. */
1351 hi
= elf_link_hash_lookup (elf_hash_table (info
), name
, TRUE
,
1353 BFD_ASSERT (hi
!= NULL
);
1356 while (hi
->root
.type
== bfd_link_hash_indirect
1357 || hi
->root
.type
== bfd_link_hash_warning
)
1359 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1365 bed
= get_elf_backend_data (abfd
);
1366 collect
= bed
->collect
;
1367 dynamic
= (abfd
->flags
& DYNAMIC
) != 0;
1369 shortlen
= p
- name
;
1370 shortname
= bfd_hash_allocate (&info
->hash
->table
, shortlen
+ 1);
1371 if (shortname
== NULL
)
1373 memcpy (shortname
, name
, shortlen
);
1374 shortname
[shortlen
] = '\0';
1376 /* We are going to create a new symbol. Merge it with any existing
1377 symbol with this name. For the purposes of the merge, act as
1378 though we were defining the symbol we just defined, although we
1379 actually going to define an indirect symbol. */
1380 type_change_ok
= FALSE
;
1381 size_change_ok
= FALSE
;
1383 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1384 &hi
, &skip
, &override
, &type_change_ok
,
1394 if (! (_bfd_generic_link_add_one_symbol
1395 (info
, abfd
, shortname
, BSF_INDIRECT
, bfd_ind_section_ptr
,
1396 0, name
, FALSE
, collect
, &bh
)))
1398 hi
= (struct elf_link_hash_entry
*) bh
;
1402 /* In this case the symbol named SHORTNAME is overriding the
1403 indirect symbol we want to add. We were planning on making
1404 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1405 is the name without a version. NAME is the fully versioned
1406 name, and it is the default version.
1408 Overriding means that we already saw a definition for the
1409 symbol SHORTNAME in a regular object, and it is overriding
1410 the symbol defined in the dynamic object.
1412 When this happens, we actually want to change NAME, the
1413 symbol we just added, to refer to SHORTNAME. This will cause
1414 references to NAME in the shared object to become references
1415 to SHORTNAME in the regular object. This is what we expect
1416 when we override a function in a shared object: that the
1417 references in the shared object will be mapped to the
1418 definition in the regular object. */
1420 while (hi
->root
.type
== bfd_link_hash_indirect
1421 || hi
->root
.type
== bfd_link_hash_warning
)
1422 hi
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1424 h
->root
.type
= bfd_link_hash_indirect
;
1425 h
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) hi
;
1429 hi
->ref_dynamic
= 1;
1433 if (! bfd_elf_link_record_dynamic_symbol (info
, hi
))
1438 /* Now set HI to H, so that the following code will set the
1439 other fields correctly. */
1443 /* If there is a duplicate definition somewhere, then HI may not
1444 point to an indirect symbol. We will have reported an error to
1445 the user in that case. */
1447 if (hi
->root
.type
== bfd_link_hash_indirect
)
1449 struct elf_link_hash_entry
*ht
;
1451 ht
= (struct elf_link_hash_entry
*) hi
->root
.u
.i
.link
;
1452 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, ht
, hi
);
1454 /* See if the new flags lead us to realize that the symbol must
1466 if (hi
->ref_regular
)
1472 /* We also need to define an indirection from the nondefault version
1476 len
= strlen (name
);
1477 shortname
= bfd_hash_allocate (&info
->hash
->table
, len
);
1478 if (shortname
== NULL
)
1480 memcpy (shortname
, name
, shortlen
);
1481 memcpy (shortname
+ shortlen
, p
+ 1, len
- shortlen
);
1483 /* Once again, merge with any existing symbol. */
1484 type_change_ok
= FALSE
;
1485 size_change_ok
= FALSE
;
1487 if (!_bfd_elf_merge_symbol (abfd
, info
, shortname
, sym
, &sec
, value
,
1488 &hi
, &skip
, &override
, &type_change_ok
,
1497 /* Here SHORTNAME is a versioned name, so we don't expect to see
1498 the type of override we do in the case above unless it is
1499 overridden by a versioned definition. */
1500 if (hi
->root
.type
!= bfd_link_hash_defined
1501 && hi
->root
.type
!= bfd_link_hash_defweak
)
1502 (*_bfd_error_handler
)
1503 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1509 if (! (_bfd_generic_link_add_one_symbol
1510 (info
, abfd
, shortname
, BSF_INDIRECT
,
1511 bfd_ind_section_ptr
, 0, name
, FALSE
, collect
, &bh
)))
1513 hi
= (struct elf_link_hash_entry
*) bh
;
1515 /* If there is a duplicate definition somewhere, then HI may not
1516 point to an indirect symbol. We will have reported an error
1517 to the user in that case. */
1519 if (hi
->root
.type
== bfd_link_hash_indirect
)
1521 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
1523 /* See if the new flags lead us to realize that the symbol
1535 if (hi
->ref_regular
)
1545 /* This routine is used to export all defined symbols into the dynamic
1546 symbol table. It is called via elf_link_hash_traverse. */
1549 _bfd_elf_export_symbol (struct elf_link_hash_entry
*h
, void *data
)
1551 struct elf_info_failed
*eif
= data
;
1553 /* Ignore indirect symbols. These are added by the versioning code. */
1554 if (h
->root
.type
== bfd_link_hash_indirect
)
1557 if (h
->root
.type
== bfd_link_hash_warning
)
1558 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1560 if (h
->dynindx
== -1
1564 struct bfd_elf_version_tree
*t
;
1565 struct bfd_elf_version_expr
*d
;
1567 for (t
= eif
->verdefs
; t
!= NULL
; t
= t
->next
)
1569 if (t
->globals
.list
!= NULL
)
1571 d
= (*t
->match
) (&t
->globals
, NULL
, h
->root
.root
.string
);
1576 if (t
->locals
.list
!= NULL
)
1578 d
= (*t
->match
) (&t
->locals
, NULL
, h
->root
.root
.string
);
1587 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
1598 /* Look through the symbols which are defined in other shared
1599 libraries and referenced here. Update the list of version
1600 dependencies. This will be put into the .gnu.version_r section.
1601 This function is called via elf_link_hash_traverse. */
1604 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry
*h
,
1607 struct elf_find_verdep_info
*rinfo
= data
;
1608 Elf_Internal_Verneed
*t
;
1609 Elf_Internal_Vernaux
*a
;
1612 if (h
->root
.type
== bfd_link_hash_warning
)
1613 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1615 /* We only care about symbols defined in shared objects with version
1620 || h
->verinfo
.verdef
== NULL
)
1623 /* See if we already know about this version. */
1624 for (t
= elf_tdata (rinfo
->output_bfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1626 if (t
->vn_bfd
!= h
->verinfo
.verdef
->vd_bfd
)
1629 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1630 if (a
->vna_nodename
== h
->verinfo
.verdef
->vd_nodename
)
1636 /* This is a new version. Add it to tree we are building. */
1641 t
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1644 rinfo
->failed
= TRUE
;
1648 t
->vn_bfd
= h
->verinfo
.verdef
->vd_bfd
;
1649 t
->vn_nextref
= elf_tdata (rinfo
->output_bfd
)->verref
;
1650 elf_tdata (rinfo
->output_bfd
)->verref
= t
;
1654 a
= bfd_zalloc (rinfo
->output_bfd
, amt
);
1656 /* Note that we are copying a string pointer here, and testing it
1657 above. If bfd_elf_string_from_elf_section is ever changed to
1658 discard the string data when low in memory, this will have to be
1660 a
->vna_nodename
= h
->verinfo
.verdef
->vd_nodename
;
1662 a
->vna_flags
= h
->verinfo
.verdef
->vd_flags
;
1663 a
->vna_nextptr
= t
->vn_auxptr
;
1665 h
->verinfo
.verdef
->vd_exp_refno
= rinfo
->vers
;
1668 a
->vna_other
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
1675 /* Figure out appropriate versions for all the symbols. We may not
1676 have the version number script until we have read all of the input
1677 files, so until that point we don't know which symbols should be
1678 local. This function is called via elf_link_hash_traverse. */
1681 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry
*h
, void *data
)
1683 struct elf_assign_sym_version_info
*sinfo
;
1684 struct bfd_link_info
*info
;
1685 const struct elf_backend_data
*bed
;
1686 struct elf_info_failed eif
;
1693 if (h
->root
.type
== bfd_link_hash_warning
)
1694 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1696 /* Fix the symbol flags. */
1699 if (! _bfd_elf_fix_symbol_flags (h
, &eif
))
1702 sinfo
->failed
= TRUE
;
1706 /* We only need version numbers for symbols defined in regular
1708 if (!h
->def_regular
)
1711 bed
= get_elf_backend_data (sinfo
->output_bfd
);
1712 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
1713 if (p
!= NULL
&& h
->verinfo
.vertree
== NULL
)
1715 struct bfd_elf_version_tree
*t
;
1720 /* There are two consecutive ELF_VER_CHR characters if this is
1721 not a hidden symbol. */
1723 if (*p
== ELF_VER_CHR
)
1729 /* If there is no version string, we can just return out. */
1737 /* Look for the version. If we find it, it is no longer weak. */
1738 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1740 if (strcmp (t
->name
, p
) == 0)
1744 struct bfd_elf_version_expr
*d
;
1746 len
= p
- h
->root
.root
.string
;
1747 alc
= bfd_malloc (len
);
1750 memcpy (alc
, h
->root
.root
.string
, len
- 1);
1751 alc
[len
- 1] = '\0';
1752 if (alc
[len
- 2] == ELF_VER_CHR
)
1753 alc
[len
- 2] = '\0';
1755 h
->verinfo
.vertree
= t
;
1759 if (t
->globals
.list
!= NULL
)
1760 d
= (*t
->match
) (&t
->globals
, NULL
, alc
);
1762 /* See if there is anything to force this symbol to
1764 if (d
== NULL
&& t
->locals
.list
!= NULL
)
1766 d
= (*t
->match
) (&t
->locals
, NULL
, alc
);
1770 && ! info
->export_dynamic
)
1771 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1779 /* If we are building an application, we need to create a
1780 version node for this version. */
1781 if (t
== NULL
&& info
->executable
)
1783 struct bfd_elf_version_tree
**pp
;
1786 /* If we aren't going to export this symbol, we don't need
1787 to worry about it. */
1788 if (h
->dynindx
== -1)
1792 t
= bfd_zalloc (sinfo
->output_bfd
, amt
);
1795 sinfo
->failed
= TRUE
;
1800 t
->name_indx
= (unsigned int) -1;
1804 /* Don't count anonymous version tag. */
1805 if (sinfo
->verdefs
!= NULL
&& sinfo
->verdefs
->vernum
== 0)
1807 for (pp
= &sinfo
->verdefs
; *pp
!= NULL
; pp
= &(*pp
)->next
)
1809 t
->vernum
= version_index
;
1813 h
->verinfo
.vertree
= t
;
1817 /* We could not find the version for a symbol when
1818 generating a shared archive. Return an error. */
1819 (*_bfd_error_handler
)
1820 (_("%B: undefined versioned symbol name %s"),
1821 sinfo
->output_bfd
, h
->root
.root
.string
);
1822 bfd_set_error (bfd_error_bad_value
);
1823 sinfo
->failed
= TRUE
;
1831 /* If we don't have a version for this symbol, see if we can find
1833 if (h
->verinfo
.vertree
== NULL
&& sinfo
->verdefs
!= NULL
)
1835 struct bfd_elf_version_tree
*t
;
1836 struct bfd_elf_version_tree
*local_ver
;
1837 struct bfd_elf_version_expr
*d
;
1839 /* See if can find what version this symbol is in. If the
1840 symbol is supposed to be local, then don't actually register
1843 for (t
= sinfo
->verdefs
; t
!= NULL
; t
= t
->next
)
1845 if (t
->globals
.list
!= NULL
)
1847 bfd_boolean matched
;
1851 while ((d
= (*t
->match
) (&t
->globals
, d
,
1852 h
->root
.root
.string
)) != NULL
)
1857 /* There is a version without definition. Make
1858 the symbol the default definition for this
1860 h
->verinfo
.vertree
= t
;
1868 /* There is no undefined version for this symbol. Hide the
1870 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1873 if (t
->locals
.list
!= NULL
)
1876 while ((d
= (*t
->match
) (&t
->locals
, d
,
1877 h
->root
.root
.string
)) != NULL
)
1880 /* If the match is "*", keep looking for a more
1881 explicit, perhaps even global, match.
1882 XXX: Shouldn't this be !d->wildcard instead? */
1883 if (d
->pattern
[0] != '*' || d
->pattern
[1] != '\0')
1892 if (local_ver
!= NULL
)
1894 h
->verinfo
.vertree
= local_ver
;
1895 if (h
->dynindx
!= -1
1897 && ! info
->export_dynamic
)
1899 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
1907 /* Read and swap the relocs from the section indicated by SHDR. This
1908 may be either a REL or a RELA section. The relocations are
1909 translated into RELA relocations and stored in INTERNAL_RELOCS,
1910 which should have already been allocated to contain enough space.
1911 The EXTERNAL_RELOCS are a buffer where the external form of the
1912 relocations should be stored.
1914 Returns FALSE if something goes wrong. */
1917 elf_link_read_relocs_from_section (bfd
*abfd
,
1919 Elf_Internal_Shdr
*shdr
,
1920 void *external_relocs
,
1921 Elf_Internal_Rela
*internal_relocs
)
1923 const struct elf_backend_data
*bed
;
1924 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
1925 const bfd_byte
*erela
;
1926 const bfd_byte
*erelaend
;
1927 Elf_Internal_Rela
*irela
;
1928 Elf_Internal_Shdr
*symtab_hdr
;
1931 /* Position ourselves at the start of the section. */
1932 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0)
1935 /* Read the relocations. */
1936 if (bfd_bread (external_relocs
, shdr
->sh_size
, abfd
) != shdr
->sh_size
)
1939 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1940 nsyms
= symtab_hdr
->sh_size
/ symtab_hdr
->sh_entsize
;
1942 bed
= get_elf_backend_data (abfd
);
1944 /* Convert the external relocations to the internal format. */
1945 if (shdr
->sh_entsize
== bed
->s
->sizeof_rel
)
1946 swap_in
= bed
->s
->swap_reloc_in
;
1947 else if (shdr
->sh_entsize
== bed
->s
->sizeof_rela
)
1948 swap_in
= bed
->s
->swap_reloca_in
;
1951 bfd_set_error (bfd_error_wrong_format
);
1955 erela
= external_relocs
;
1956 erelaend
= erela
+ shdr
->sh_size
;
1957 irela
= internal_relocs
;
1958 while (erela
< erelaend
)
1962 (*swap_in
) (abfd
, erela
, irela
);
1963 r_symndx
= ELF32_R_SYM (irela
->r_info
);
1964 if (bed
->s
->arch_size
== 64)
1966 if ((size_t) r_symndx
>= nsyms
)
1968 (*_bfd_error_handler
)
1969 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1970 " for offset 0x%lx in section `%A'"),
1972 (unsigned long) r_symndx
, (unsigned long) nsyms
, irela
->r_offset
);
1973 bfd_set_error (bfd_error_bad_value
);
1976 irela
+= bed
->s
->int_rels_per_ext_rel
;
1977 erela
+= shdr
->sh_entsize
;
1983 /* Read and swap the relocs for a section O. They may have been
1984 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1985 not NULL, they are used as buffers to read into. They are known to
1986 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1987 the return value is allocated using either malloc or bfd_alloc,
1988 according to the KEEP_MEMORY argument. If O has two relocation
1989 sections (both REL and RELA relocations), then the REL_HDR
1990 relocations will appear first in INTERNAL_RELOCS, followed by the
1991 REL_HDR2 relocations. */
1994 _bfd_elf_link_read_relocs (bfd
*abfd
,
1996 void *external_relocs
,
1997 Elf_Internal_Rela
*internal_relocs
,
1998 bfd_boolean keep_memory
)
2000 Elf_Internal_Shdr
*rel_hdr
;
2001 void *alloc1
= NULL
;
2002 Elf_Internal_Rela
*alloc2
= NULL
;
2003 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2005 if (elf_section_data (o
)->relocs
!= NULL
)
2006 return elf_section_data (o
)->relocs
;
2008 if (o
->reloc_count
== 0)
2011 rel_hdr
= &elf_section_data (o
)->rel_hdr
;
2013 if (internal_relocs
== NULL
)
2017 size
= o
->reloc_count
;
2018 size
*= bed
->s
->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela
);
2020 internal_relocs
= bfd_alloc (abfd
, size
);
2022 internal_relocs
= alloc2
= bfd_malloc (size
);
2023 if (internal_relocs
== NULL
)
2027 if (external_relocs
== NULL
)
2029 bfd_size_type size
= rel_hdr
->sh_size
;
2031 if (elf_section_data (o
)->rel_hdr2
)
2032 size
+= elf_section_data (o
)->rel_hdr2
->sh_size
;
2033 alloc1
= bfd_malloc (size
);
2036 external_relocs
= alloc1
;
2039 if (!elf_link_read_relocs_from_section (abfd
, o
, rel_hdr
,
2043 if (elf_section_data (o
)->rel_hdr2
2044 && (!elf_link_read_relocs_from_section
2046 elf_section_data (o
)->rel_hdr2
,
2047 ((bfd_byte
*) external_relocs
) + rel_hdr
->sh_size
,
2048 internal_relocs
+ (NUM_SHDR_ENTRIES (rel_hdr
)
2049 * bed
->s
->int_rels_per_ext_rel
))))
2052 /* Cache the results for next time, if we can. */
2054 elf_section_data (o
)->relocs
= internal_relocs
;
2059 /* Don't free alloc2, since if it was allocated we are passing it
2060 back (under the name of internal_relocs). */
2062 return internal_relocs
;
2072 /* Compute the size of, and allocate space for, REL_HDR which is the
2073 section header for a section containing relocations for O. */
2076 _bfd_elf_link_size_reloc_section (bfd
*abfd
,
2077 Elf_Internal_Shdr
*rel_hdr
,
2080 bfd_size_type reloc_count
;
2081 bfd_size_type num_rel_hashes
;
2083 /* Figure out how many relocations there will be. */
2084 if (rel_hdr
== &elf_section_data (o
)->rel_hdr
)
2085 reloc_count
= elf_section_data (o
)->rel_count
;
2087 reloc_count
= elf_section_data (o
)->rel_count2
;
2089 num_rel_hashes
= o
->reloc_count
;
2090 if (num_rel_hashes
< reloc_count
)
2091 num_rel_hashes
= reloc_count
;
2093 /* That allows us to calculate the size of the section. */
2094 rel_hdr
->sh_size
= rel_hdr
->sh_entsize
* reloc_count
;
2096 /* The contents field must last into write_object_contents, so we
2097 allocate it with bfd_alloc rather than malloc. Also since we
2098 cannot be sure that the contents will actually be filled in,
2099 we zero the allocated space. */
2100 rel_hdr
->contents
= bfd_zalloc (abfd
, rel_hdr
->sh_size
);
2101 if (rel_hdr
->contents
== NULL
&& rel_hdr
->sh_size
!= 0)
2104 /* We only allocate one set of hash entries, so we only do it the
2105 first time we are called. */
2106 if (elf_section_data (o
)->rel_hashes
== NULL
2109 struct elf_link_hash_entry
**p
;
2111 p
= bfd_zmalloc (num_rel_hashes
* sizeof (struct elf_link_hash_entry
*));
2115 elf_section_data (o
)->rel_hashes
= p
;
2121 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2122 originated from the section given by INPUT_REL_HDR) to the
2126 _bfd_elf_link_output_relocs (bfd
*output_bfd
,
2127 asection
*input_section
,
2128 Elf_Internal_Shdr
*input_rel_hdr
,
2129 Elf_Internal_Rela
*internal_relocs
)
2131 Elf_Internal_Rela
*irela
;
2132 Elf_Internal_Rela
*irelaend
;
2134 Elf_Internal_Shdr
*output_rel_hdr
;
2135 asection
*output_section
;
2136 unsigned int *rel_countp
= NULL
;
2137 const struct elf_backend_data
*bed
;
2138 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
2140 output_section
= input_section
->output_section
;
2141 output_rel_hdr
= NULL
;
2143 if (elf_section_data (output_section
)->rel_hdr
.sh_entsize
2144 == input_rel_hdr
->sh_entsize
)
2146 output_rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
2147 rel_countp
= &elf_section_data (output_section
)->rel_count
;
2149 else if (elf_section_data (output_section
)->rel_hdr2
2150 && (elf_section_data (output_section
)->rel_hdr2
->sh_entsize
2151 == input_rel_hdr
->sh_entsize
))
2153 output_rel_hdr
= elf_section_data (output_section
)->rel_hdr2
;
2154 rel_countp
= &elf_section_data (output_section
)->rel_count2
;
2158 (*_bfd_error_handler
)
2159 (_("%B: relocation size mismatch in %B section %A"),
2160 output_bfd
, input_section
->owner
, input_section
);
2161 bfd_set_error (bfd_error_wrong_object_format
);
2165 bed
= get_elf_backend_data (output_bfd
);
2166 if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
2167 swap_out
= bed
->s
->swap_reloc_out
;
2168 else if (input_rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
2169 swap_out
= bed
->s
->swap_reloca_out
;
2173 erel
= output_rel_hdr
->contents
;
2174 erel
+= *rel_countp
* input_rel_hdr
->sh_entsize
;
2175 irela
= internal_relocs
;
2176 irelaend
= irela
+ (NUM_SHDR_ENTRIES (input_rel_hdr
)
2177 * bed
->s
->int_rels_per_ext_rel
);
2178 while (irela
< irelaend
)
2180 (*swap_out
) (output_bfd
, irela
, erel
);
2181 irela
+= bed
->s
->int_rels_per_ext_rel
;
2182 erel
+= input_rel_hdr
->sh_entsize
;
2185 /* Bump the counter, so that we know where to add the next set of
2187 *rel_countp
+= NUM_SHDR_ENTRIES (input_rel_hdr
);
2192 /* Fix up the flags for a symbol. This handles various cases which
2193 can only be fixed after all the input files are seen. This is
2194 currently called by both adjust_dynamic_symbol and
2195 assign_sym_version, which is unnecessary but perhaps more robust in
2196 the face of future changes. */
2199 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry
*h
,
2200 struct elf_info_failed
*eif
)
2202 /* If this symbol was mentioned in a non-ELF file, try to set
2203 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2204 permit a non-ELF file to correctly refer to a symbol defined in
2205 an ELF dynamic object. */
2208 while (h
->root
.type
== bfd_link_hash_indirect
)
2209 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2211 if (h
->root
.type
!= bfd_link_hash_defined
2212 && h
->root
.type
!= bfd_link_hash_defweak
)
2215 h
->ref_regular_nonweak
= 1;
2219 if (h
->root
.u
.def
.section
->owner
!= NULL
2220 && (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2221 == bfd_target_elf_flavour
))
2224 h
->ref_regular_nonweak
= 1;
2230 if (h
->dynindx
== -1
2234 if (! bfd_elf_link_record_dynamic_symbol (eif
->info
, h
))
2243 /* Unfortunately, NON_ELF is only correct if the symbol
2244 was first seen in a non-ELF file. Fortunately, if the symbol
2245 was first seen in an ELF file, we're probably OK unless the
2246 symbol was defined in a non-ELF file. Catch that case here.
2247 FIXME: We're still in trouble if the symbol was first seen in
2248 a dynamic object, and then later in a non-ELF regular object. */
2249 if ((h
->root
.type
== bfd_link_hash_defined
2250 || h
->root
.type
== bfd_link_hash_defweak
)
2252 && (h
->root
.u
.def
.section
->owner
!= NULL
2253 ? (bfd_get_flavour (h
->root
.u
.def
.section
->owner
)
2254 != bfd_target_elf_flavour
)
2255 : (bfd_is_abs_section (h
->root
.u
.def
.section
)
2256 && !h
->def_dynamic
)))
2260 /* If this is a final link, and the symbol was defined as a common
2261 symbol in a regular object file, and there was no definition in
2262 any dynamic object, then the linker will have allocated space for
2263 the symbol in a common section but the DEF_REGULAR
2264 flag will not have been set. */
2265 if (h
->root
.type
== bfd_link_hash_defined
2269 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
2272 /* If -Bsymbolic was used (which means to bind references to global
2273 symbols to the definition within the shared object), and this
2274 symbol was defined in a regular object, then it actually doesn't
2275 need a PLT entry. Likewise, if the symbol has non-default
2276 visibility. If the symbol has hidden or internal visibility, we
2277 will force it local. */
2279 && eif
->info
->shared
2280 && is_elf_hash_table (eif
->info
->hash
)
2281 && (eif
->info
->symbolic
2282 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2285 const struct elf_backend_data
*bed
;
2286 bfd_boolean force_local
;
2288 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2290 force_local
= (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
2291 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
);
2292 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, force_local
);
2295 /* If a weak undefined symbol has non-default visibility, we also
2296 hide it from the dynamic linker. */
2297 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
2298 && h
->root
.type
== bfd_link_hash_undefweak
)
2300 const struct elf_backend_data
*bed
;
2301 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2302 (*bed
->elf_backend_hide_symbol
) (eif
->info
, h
, TRUE
);
2305 /* If this is a weak defined symbol in a dynamic object, and we know
2306 the real definition in the dynamic object, copy interesting flags
2307 over to the real definition. */
2308 if (h
->u
.weakdef
!= NULL
)
2310 struct elf_link_hash_entry
*weakdef
;
2312 weakdef
= h
->u
.weakdef
;
2313 if (h
->root
.type
== bfd_link_hash_indirect
)
2314 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2316 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
2317 || h
->root
.type
== bfd_link_hash_defweak
);
2318 BFD_ASSERT (weakdef
->root
.type
== bfd_link_hash_defined
2319 || weakdef
->root
.type
== bfd_link_hash_defweak
);
2320 BFD_ASSERT (weakdef
->def_dynamic
);
2322 /* If the real definition is defined by a regular object file,
2323 don't do anything special. See the longer description in
2324 _bfd_elf_adjust_dynamic_symbol, below. */
2325 if (weakdef
->def_regular
)
2326 h
->u
.weakdef
= NULL
;
2329 const struct elf_backend_data
*bed
;
2331 bed
= get_elf_backend_data (elf_hash_table (eif
->info
)->dynobj
);
2332 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, weakdef
, h
);
2339 /* Make the backend pick a good value for a dynamic symbol. This is
2340 called via elf_link_hash_traverse, and also calls itself
2344 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry
*h
, void *data
)
2346 struct elf_info_failed
*eif
= data
;
2348 const struct elf_backend_data
*bed
;
2350 if (! is_elf_hash_table (eif
->info
->hash
))
2353 if (h
->root
.type
== bfd_link_hash_warning
)
2355 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2356 h
->got
= elf_hash_table (eif
->info
)->init_offset
;
2358 /* When warning symbols are created, they **replace** the "real"
2359 entry in the hash table, thus we never get to see the real
2360 symbol in a hash traversal. So look at it now. */
2361 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2364 /* Ignore indirect symbols. These are added by the versioning code. */
2365 if (h
->root
.type
== bfd_link_hash_indirect
)
2368 /* Fix the symbol flags. */
2369 if (! _bfd_elf_fix_symbol_flags (h
, eif
))
2372 /* If this symbol does not require a PLT entry, and it is not
2373 defined by a dynamic object, or is not referenced by a regular
2374 object, ignore it. We do have to handle a weak defined symbol,
2375 even if no regular object refers to it, if we decided to add it
2376 to the dynamic symbol table. FIXME: Do we normally need to worry
2377 about symbols which are defined by one dynamic object and
2378 referenced by another one? */
2383 && (h
->u
.weakdef
== NULL
|| h
->u
.weakdef
->dynindx
== -1))))
2385 h
->plt
= elf_hash_table (eif
->info
)->init_offset
;
2389 /* If we've already adjusted this symbol, don't do it again. This
2390 can happen via a recursive call. */
2391 if (h
->dynamic_adjusted
)
2394 /* Don't look at this symbol again. Note that we must set this
2395 after checking the above conditions, because we may look at a
2396 symbol once, decide not to do anything, and then get called
2397 recursively later after REF_REGULAR is set below. */
2398 h
->dynamic_adjusted
= 1;
2400 /* If this is a weak definition, and we know a real definition, and
2401 the real symbol is not itself defined by a regular object file,
2402 then get a good value for the real definition. We handle the
2403 real symbol first, for the convenience of the backend routine.
2405 Note that there is a confusing case here. If the real definition
2406 is defined by a regular object file, we don't get the real symbol
2407 from the dynamic object, but we do get the weak symbol. If the
2408 processor backend uses a COPY reloc, then if some routine in the
2409 dynamic object changes the real symbol, we will not see that
2410 change in the corresponding weak symbol. This is the way other
2411 ELF linkers work as well, and seems to be a result of the shared
2414 I will clarify this issue. Most SVR4 shared libraries define the
2415 variable _timezone and define timezone as a weak synonym. The
2416 tzset call changes _timezone. If you write
2417 extern int timezone;
2419 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2420 you might expect that, since timezone is a synonym for _timezone,
2421 the same number will print both times. However, if the processor
2422 backend uses a COPY reloc, then actually timezone will be copied
2423 into your process image, and, since you define _timezone
2424 yourself, _timezone will not. Thus timezone and _timezone will
2425 wind up at different memory locations. The tzset call will set
2426 _timezone, leaving timezone unchanged. */
2428 if (h
->u
.weakdef
!= NULL
)
2430 /* If we get to this point, we know there is an implicit
2431 reference by a regular object file via the weak symbol H.
2432 FIXME: Is this really true? What if the traversal finds
2433 H->U.WEAKDEF before it finds H? */
2434 h
->u
.weakdef
->ref_regular
= 1;
2436 if (! _bfd_elf_adjust_dynamic_symbol (h
->u
.weakdef
, eif
))
2440 /* If a symbol has no type and no size and does not require a PLT
2441 entry, then we are probably about to do the wrong thing here: we
2442 are probably going to create a COPY reloc for an empty object.
2443 This case can arise when a shared object is built with assembly
2444 code, and the assembly code fails to set the symbol type. */
2446 && h
->type
== STT_NOTYPE
2448 (*_bfd_error_handler
)
2449 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2450 h
->root
.root
.string
);
2452 dynobj
= elf_hash_table (eif
->info
)->dynobj
;
2453 bed
= get_elf_backend_data (dynobj
);
2454 if (! (*bed
->elf_backend_adjust_dynamic_symbol
) (eif
->info
, h
))
2463 /* Adjust all external symbols pointing into SEC_MERGE sections
2464 to reflect the object merging within the sections. */
2467 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry
*h
, void *data
)
2471 if (h
->root
.type
== bfd_link_hash_warning
)
2472 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2474 if ((h
->root
.type
== bfd_link_hash_defined
2475 || h
->root
.type
== bfd_link_hash_defweak
)
2476 && ((sec
= h
->root
.u
.def
.section
)->flags
& SEC_MERGE
)
2477 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
2479 bfd
*output_bfd
= data
;
2481 h
->root
.u
.def
.value
=
2482 _bfd_merged_section_offset (output_bfd
,
2483 &h
->root
.u
.def
.section
,
2484 elf_section_data (sec
)->sec_info
,
2485 h
->root
.u
.def
.value
);
2491 /* Returns false if the symbol referred to by H should be considered
2492 to resolve local to the current module, and true if it should be
2493 considered to bind dynamically. */
2496 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry
*h
,
2497 struct bfd_link_info
*info
,
2498 bfd_boolean ignore_protected
)
2500 bfd_boolean binding_stays_local_p
;
2505 while (h
->root
.type
== bfd_link_hash_indirect
2506 || h
->root
.type
== bfd_link_hash_warning
)
2507 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2509 /* If it was forced local, then clearly it's not dynamic. */
2510 if (h
->dynindx
== -1)
2512 if (h
->forced_local
)
2515 /* Identify the cases where name binding rules say that a
2516 visible symbol resolves locally. */
2517 binding_stays_local_p
= info
->executable
|| info
->symbolic
;
2519 switch (ELF_ST_VISIBILITY (h
->other
))
2526 /* Proper resolution for function pointer equality may require
2527 that these symbols perhaps be resolved dynamically, even though
2528 we should be resolving them to the current module. */
2529 if (!ignore_protected
|| h
->type
!= STT_FUNC
)
2530 binding_stays_local_p
= TRUE
;
2537 /* If it isn't defined locally, then clearly it's dynamic. */
2538 if (!h
->def_regular
)
2541 /* Otherwise, the symbol is dynamic if binding rules don't tell
2542 us that it remains local. */
2543 return !binding_stays_local_p
;
2546 /* Return true if the symbol referred to by H should be considered
2547 to resolve local to the current module, and false otherwise. Differs
2548 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2549 undefined symbols and weak symbols. */
2552 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry
*h
,
2553 struct bfd_link_info
*info
,
2554 bfd_boolean local_protected
)
2556 /* If it's a local sym, of course we resolve locally. */
2560 /* Common symbols that become definitions don't get the DEF_REGULAR
2561 flag set, so test it first, and don't bail out. */
2562 if (ELF_COMMON_DEF_P (h
))
2564 /* If we don't have a definition in a regular file, then we can't
2565 resolve locally. The sym is either undefined or dynamic. */
2566 else if (!h
->def_regular
)
2569 /* Forced local symbols resolve locally. */
2570 if (h
->forced_local
)
2573 /* As do non-dynamic symbols. */
2574 if (h
->dynindx
== -1)
2577 /* At this point, we know the symbol is defined and dynamic. In an
2578 executable it must resolve locally, likewise when building symbolic
2579 shared libraries. */
2580 if (info
->executable
|| info
->symbolic
)
2583 /* Now deal with defined dynamic symbols in shared libraries. Ones
2584 with default visibility might not resolve locally. */
2585 if (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2588 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2589 if (ELF_ST_VISIBILITY (h
->other
) != STV_PROTECTED
)
2592 /* STV_PROTECTED non-function symbols are local. */
2593 if (h
->type
!= STT_FUNC
)
2596 /* Function pointer equality tests may require that STV_PROTECTED
2597 symbols be treated as dynamic symbols, even when we know that the
2598 dynamic linker will resolve them locally. */
2599 return local_protected
;
2602 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2603 aligned. Returns the first TLS output section. */
2605 struct bfd_section
*
2606 _bfd_elf_tls_setup (bfd
*obfd
, struct bfd_link_info
*info
)
2608 struct bfd_section
*sec
, *tls
;
2609 unsigned int align
= 0;
2611 for (sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2612 if ((sec
->flags
& SEC_THREAD_LOCAL
) != 0)
2616 for (; sec
!= NULL
&& (sec
->flags
& SEC_THREAD_LOCAL
) != 0; sec
= sec
->next
)
2617 if (sec
->alignment_power
> align
)
2618 align
= sec
->alignment_power
;
2620 elf_hash_table (info
)->tls_sec
= tls
;
2622 /* Ensure the alignment of the first section is the largest alignment,
2623 so that the tls segment starts aligned. */
2625 tls
->alignment_power
= align
;
2630 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2632 is_global_data_symbol_definition (bfd
*abfd ATTRIBUTE_UNUSED
,
2633 Elf_Internal_Sym
*sym
)
2635 /* Local symbols do not count, but target specific ones might. */
2636 if (ELF_ST_BIND (sym
->st_info
) != STB_GLOBAL
2637 && ELF_ST_BIND (sym
->st_info
) < STB_LOOS
)
2640 /* Function symbols do not count. */
2641 if (ELF_ST_TYPE (sym
->st_info
) == STT_FUNC
)
2644 /* If the section is undefined, then so is the symbol. */
2645 if (sym
->st_shndx
== SHN_UNDEF
)
2648 /* If the symbol is defined in the common section, then
2649 it is a common definition and so does not count. */
2650 if (sym
->st_shndx
== SHN_COMMON
)
2653 /* If the symbol is in a target specific section then we
2654 must rely upon the backend to tell us what it is. */
2655 if (sym
->st_shndx
>= SHN_LORESERVE
&& sym
->st_shndx
< SHN_ABS
)
2656 /* FIXME - this function is not coded yet:
2658 return _bfd_is_global_symbol_definition (abfd, sym);
2660 Instead for now assume that the definition is not global,
2661 Even if this is wrong, at least the linker will behave
2662 in the same way that it used to do. */
2668 /* Search the symbol table of the archive element of the archive ABFD
2669 whose archive map contains a mention of SYMDEF, and determine if
2670 the symbol is defined in this element. */
2672 elf_link_is_defined_archive_symbol (bfd
* abfd
, carsym
* symdef
)
2674 Elf_Internal_Shdr
* hdr
;
2675 bfd_size_type symcount
;
2676 bfd_size_type extsymcount
;
2677 bfd_size_type extsymoff
;
2678 Elf_Internal_Sym
*isymbuf
;
2679 Elf_Internal_Sym
*isym
;
2680 Elf_Internal_Sym
*isymend
;
2683 abfd
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
2687 if (! bfd_check_format (abfd
, bfd_object
))
2690 /* If we have already included the element containing this symbol in the
2691 link then we do not need to include it again. Just claim that any symbol
2692 it contains is not a definition, so that our caller will not decide to
2693 (re)include this element. */
2694 if (abfd
->archive_pass
)
2697 /* Select the appropriate symbol table. */
2698 if ((abfd
->flags
& DYNAMIC
) == 0 || elf_dynsymtab (abfd
) == 0)
2699 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2701 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2703 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
2705 /* The sh_info field of the symtab header tells us where the
2706 external symbols start. We don't care about the local symbols. */
2707 if (elf_bad_symtab (abfd
))
2709 extsymcount
= symcount
;
2714 extsymcount
= symcount
- hdr
->sh_info
;
2715 extsymoff
= hdr
->sh_info
;
2718 if (extsymcount
== 0)
2721 /* Read in the symbol table. */
2722 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
2724 if (isymbuf
== NULL
)
2727 /* Scan the symbol table looking for SYMDEF. */
2729 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
; isym
< isymend
; isym
++)
2733 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
2738 if (strcmp (name
, symdef
->name
) == 0)
2740 result
= is_global_data_symbol_definition (abfd
, isym
);
2750 /* Add an entry to the .dynamic table. */
2753 _bfd_elf_add_dynamic_entry (struct bfd_link_info
*info
,
2757 struct elf_link_hash_table
*hash_table
;
2758 const struct elf_backend_data
*bed
;
2760 bfd_size_type newsize
;
2761 bfd_byte
*newcontents
;
2762 Elf_Internal_Dyn dyn
;
2764 hash_table
= elf_hash_table (info
);
2765 if (! is_elf_hash_table (hash_table
))
2768 if (info
->warn_shared_textrel
&& info
->shared
&& tag
== DT_TEXTREL
)
2770 (_("warning: creating a DT_TEXTREL in a shared object."));
2772 bed
= get_elf_backend_data (hash_table
->dynobj
);
2773 s
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2774 BFD_ASSERT (s
!= NULL
);
2776 newsize
= s
->size
+ bed
->s
->sizeof_dyn
;
2777 newcontents
= bfd_realloc (s
->contents
, newsize
);
2778 if (newcontents
== NULL
)
2782 dyn
.d_un
.d_val
= val
;
2783 bed
->s
->swap_dyn_out (hash_table
->dynobj
, &dyn
, newcontents
+ s
->size
);
2786 s
->contents
= newcontents
;
2791 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2792 otherwise just check whether one already exists. Returns -1 on error,
2793 1 if a DT_NEEDED tag already exists, and 0 on success. */
2796 elf_add_dt_needed_tag (bfd
*abfd
,
2797 struct bfd_link_info
*info
,
2801 struct elf_link_hash_table
*hash_table
;
2802 bfd_size_type oldsize
;
2803 bfd_size_type strindex
;
2805 if (!_bfd_elf_link_create_dynstrtab (abfd
, info
))
2808 hash_table
= elf_hash_table (info
);
2809 oldsize
= _bfd_elf_strtab_size (hash_table
->dynstr
);
2810 strindex
= _bfd_elf_strtab_add (hash_table
->dynstr
, soname
, FALSE
);
2811 if (strindex
== (bfd_size_type
) -1)
2814 if (oldsize
== _bfd_elf_strtab_size (hash_table
->dynstr
))
2817 const struct elf_backend_data
*bed
;
2820 bed
= get_elf_backend_data (hash_table
->dynobj
);
2821 sdyn
= bfd_get_section_by_name (hash_table
->dynobj
, ".dynamic");
2823 for (extdyn
= sdyn
->contents
;
2824 extdyn
< sdyn
->contents
+ sdyn
->size
;
2825 extdyn
+= bed
->s
->sizeof_dyn
)
2827 Elf_Internal_Dyn dyn
;
2829 bed
->s
->swap_dyn_in (hash_table
->dynobj
, extdyn
, &dyn
);
2830 if (dyn
.d_tag
== DT_NEEDED
2831 && dyn
.d_un
.d_val
== strindex
)
2833 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2841 if (!_bfd_elf_link_create_dynamic_sections (hash_table
->dynobj
, info
))
2844 if (!_bfd_elf_add_dynamic_entry (info
, DT_NEEDED
, strindex
))
2848 /* We were just checking for existence of the tag. */
2849 _bfd_elf_strtab_delref (hash_table
->dynstr
, strindex
);
2854 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2855 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2856 references from regular objects to these symbols.
2858 ??? Should we do something about references from other dynamic
2859 obects? If not, we potentially lose some warnings about undefined
2860 symbols. But how can we recover the initial undefined / undefweak
2863 struct elf_smash_syms_data
2866 struct elf_link_hash_table
*htab
;
2867 bfd_boolean twiddled
;
2871 elf_smash_syms (struct elf_link_hash_entry
*h
, void *data
)
2873 struct elf_smash_syms_data
*inf
= (struct elf_smash_syms_data
*) data
;
2874 struct bfd_link_hash_entry
*bh
;
2876 switch (h
->root
.type
)
2879 case bfd_link_hash_new
:
2882 case bfd_link_hash_undefined
:
2883 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2885 if (h
->root
.u
.undef
.weak
!= NULL
2886 && h
->root
.u
.undef
.weak
!= inf
->not_needed
)
2888 /* Symbol was undefweak in u.undef.weak bfd, and has become
2889 undefined in as-needed lib. Restore weak. */
2890 h
->root
.type
= bfd_link_hash_undefweak
;
2891 h
->root
.u
.undef
.abfd
= h
->root
.u
.undef
.weak
;
2892 if (h
->root
.u
.undef
.next
!= NULL
2893 || inf
->htab
->root
.undefs_tail
== &h
->root
)
2894 inf
->twiddled
= TRUE
;
2899 case bfd_link_hash_undefweak
:
2900 if (h
->root
.u
.undef
.abfd
!= inf
->not_needed
)
2904 case bfd_link_hash_defined
:
2905 case bfd_link_hash_defweak
:
2906 if (h
->root
.u
.def
.section
->owner
!= inf
->not_needed
)
2910 case bfd_link_hash_common
:
2911 if (h
->root
.u
.c
.p
->section
->owner
!= inf
->not_needed
)
2915 case bfd_link_hash_warning
:
2916 case bfd_link_hash_indirect
:
2917 elf_smash_syms ((struct elf_link_hash_entry
*) h
->root
.u
.i
.link
, data
);
2918 if (h
->root
.u
.i
.link
->type
!= bfd_link_hash_new
)
2920 if (h
->root
.u
.i
.link
->u
.undef
.abfd
!= inf
->not_needed
)
2925 /* There is no way we can undo symbol table state from defined or
2926 defweak back to undefined. */
2930 /* Set sym back to newly created state, but keep undefs list pointer. */
2931 bh
= h
->root
.u
.undef
.next
;
2932 if (bh
!= NULL
|| inf
->htab
->root
.undefs_tail
== &h
->root
)
2933 inf
->twiddled
= TRUE
;
2934 (*inf
->htab
->root
.table
.newfunc
) (&h
->root
.root
,
2935 &inf
->htab
->root
.table
,
2936 h
->root
.root
.string
);
2937 h
->root
.u
.undef
.next
= bh
;
2938 h
->root
.u
.undef
.abfd
= inf
->not_needed
;
2943 /* Sort symbol by value and section. */
2945 elf_sort_symbol (const void *arg1
, const void *arg2
)
2947 const struct elf_link_hash_entry
*h1
;
2948 const struct elf_link_hash_entry
*h2
;
2949 bfd_signed_vma vdiff
;
2951 h1
= *(const struct elf_link_hash_entry
**) arg1
;
2952 h2
= *(const struct elf_link_hash_entry
**) arg2
;
2953 vdiff
= h1
->root
.u
.def
.value
- h2
->root
.u
.def
.value
;
2955 return vdiff
> 0 ? 1 : -1;
2958 long sdiff
= h1
->root
.u
.def
.section
->id
- h2
->root
.u
.def
.section
->id
;
2960 return sdiff
> 0 ? 1 : -1;
2965 /* This function is used to adjust offsets into .dynstr for
2966 dynamic symbols. This is called via elf_link_hash_traverse. */
2969 elf_adjust_dynstr_offsets (struct elf_link_hash_entry
*h
, void *data
)
2971 struct elf_strtab_hash
*dynstr
= data
;
2973 if (h
->root
.type
== bfd_link_hash_warning
)
2974 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2976 if (h
->dynindx
!= -1)
2977 h
->dynstr_index
= _bfd_elf_strtab_offset (dynstr
, h
->dynstr_index
);
2981 /* Assign string offsets in .dynstr, update all structures referencing
2985 elf_finalize_dynstr (bfd
*output_bfd
, struct bfd_link_info
*info
)
2987 struct elf_link_hash_table
*hash_table
= elf_hash_table (info
);
2988 struct elf_link_local_dynamic_entry
*entry
;
2989 struct elf_strtab_hash
*dynstr
= hash_table
->dynstr
;
2990 bfd
*dynobj
= hash_table
->dynobj
;
2993 const struct elf_backend_data
*bed
;
2996 _bfd_elf_strtab_finalize (dynstr
);
2997 size
= _bfd_elf_strtab_size (dynstr
);
2999 bed
= get_elf_backend_data (dynobj
);
3000 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3001 BFD_ASSERT (sdyn
!= NULL
);
3003 /* Update all .dynamic entries referencing .dynstr strings. */
3004 for (extdyn
= sdyn
->contents
;
3005 extdyn
< sdyn
->contents
+ sdyn
->size
;
3006 extdyn
+= bed
->s
->sizeof_dyn
)
3008 Elf_Internal_Dyn dyn
;
3010 bed
->s
->swap_dyn_in (dynobj
, extdyn
, &dyn
);
3014 dyn
.d_un
.d_val
= size
;
3022 dyn
.d_un
.d_val
= _bfd_elf_strtab_offset (dynstr
, dyn
.d_un
.d_val
);
3027 bed
->s
->swap_dyn_out (dynobj
, &dyn
, extdyn
);
3030 /* Now update local dynamic symbols. */
3031 for (entry
= hash_table
->dynlocal
; entry
; entry
= entry
->next
)
3032 entry
->isym
.st_name
= _bfd_elf_strtab_offset (dynstr
,
3033 entry
->isym
.st_name
);
3035 /* And the rest of dynamic symbols. */
3036 elf_link_hash_traverse (hash_table
, elf_adjust_dynstr_offsets
, dynstr
);
3038 /* Adjust version definitions. */
3039 if (elf_tdata (output_bfd
)->cverdefs
)
3044 Elf_Internal_Verdef def
;
3045 Elf_Internal_Verdaux defaux
;
3047 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
3051 _bfd_elf_swap_verdef_in (output_bfd
, (Elf_External_Verdef
*) p
,
3053 p
+= sizeof (Elf_External_Verdef
);
3054 if (def
.vd_aux
!= sizeof (Elf_External_Verdef
))
3056 for (i
= 0; i
< def
.vd_cnt
; ++i
)
3058 _bfd_elf_swap_verdaux_in (output_bfd
,
3059 (Elf_External_Verdaux
*) p
, &defaux
);
3060 defaux
.vda_name
= _bfd_elf_strtab_offset (dynstr
,
3062 _bfd_elf_swap_verdaux_out (output_bfd
,
3063 &defaux
, (Elf_External_Verdaux
*) p
);
3064 p
+= sizeof (Elf_External_Verdaux
);
3067 while (def
.vd_next
);
3070 /* Adjust version references. */
3071 if (elf_tdata (output_bfd
)->verref
)
3076 Elf_Internal_Verneed need
;
3077 Elf_Internal_Vernaux needaux
;
3079 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
3083 _bfd_elf_swap_verneed_in (output_bfd
, (Elf_External_Verneed
*) p
,
3085 need
.vn_file
= _bfd_elf_strtab_offset (dynstr
, need
.vn_file
);
3086 _bfd_elf_swap_verneed_out (output_bfd
, &need
,
3087 (Elf_External_Verneed
*) p
);
3088 p
+= sizeof (Elf_External_Verneed
);
3089 for (i
= 0; i
< need
.vn_cnt
; ++i
)
3091 _bfd_elf_swap_vernaux_in (output_bfd
,
3092 (Elf_External_Vernaux
*) p
, &needaux
);
3093 needaux
.vna_name
= _bfd_elf_strtab_offset (dynstr
,
3095 _bfd_elf_swap_vernaux_out (output_bfd
,
3097 (Elf_External_Vernaux
*) p
);
3098 p
+= sizeof (Elf_External_Vernaux
);
3101 while (need
.vn_next
);
3107 /* Add symbols from an ELF object file to the linker hash table. */
3110 elf_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3112 bfd_boolean (*add_symbol_hook
)
3113 (bfd
*, struct bfd_link_info
*, Elf_Internal_Sym
*,
3114 const char **, flagword
*, asection
**, bfd_vma
*);
3115 bfd_boolean (*check_relocs
)
3116 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
3117 bfd_boolean (*check_directives
)
3118 (bfd
*, struct bfd_link_info
*);
3119 bfd_boolean collect
;
3120 Elf_Internal_Shdr
*hdr
;
3121 bfd_size_type symcount
;
3122 bfd_size_type extsymcount
;
3123 bfd_size_type extsymoff
;
3124 struct elf_link_hash_entry
**sym_hash
;
3125 bfd_boolean dynamic
;
3126 Elf_External_Versym
*extversym
= NULL
;
3127 Elf_External_Versym
*ever
;
3128 struct elf_link_hash_entry
*weaks
;
3129 struct elf_link_hash_entry
**nondeflt_vers
= NULL
;
3130 bfd_size_type nondeflt_vers_cnt
= 0;
3131 Elf_Internal_Sym
*isymbuf
= NULL
;
3132 Elf_Internal_Sym
*isym
;
3133 Elf_Internal_Sym
*isymend
;
3134 const struct elf_backend_data
*bed
;
3135 bfd_boolean add_needed
;
3136 struct elf_link_hash_table
* hash_table
;
3139 hash_table
= elf_hash_table (info
);
3141 bed
= get_elf_backend_data (abfd
);
3142 add_symbol_hook
= bed
->elf_add_symbol_hook
;
3143 collect
= bed
->collect
;
3145 if ((abfd
->flags
& DYNAMIC
) == 0)
3151 /* You can't use -r against a dynamic object. Also, there's no
3152 hope of using a dynamic object which does not exactly match
3153 the format of the output file. */
3154 if (info
->relocatable
3155 || !is_elf_hash_table (hash_table
)
3156 || hash_table
->root
.creator
!= abfd
->xvec
)
3158 if (info
->relocatable
)
3159 bfd_set_error (bfd_error_invalid_operation
);
3161 bfd_set_error (bfd_error_wrong_format
);
3166 /* As a GNU extension, any input sections which are named
3167 .gnu.warning.SYMBOL are treated as warning symbols for the given
3168 symbol. This differs from .gnu.warning sections, which generate
3169 warnings when they are included in an output file. */
3170 if (info
->executable
)
3174 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3178 name
= bfd_get_section_name (abfd
, s
);
3179 if (strncmp (name
, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3184 name
+= sizeof ".gnu.warning." - 1;
3186 /* If this is a shared object, then look up the symbol
3187 in the hash table. If it is there, and it is already
3188 been defined, then we will not be using the entry
3189 from this shared object, so we don't need to warn.
3190 FIXME: If we see the definition in a regular object
3191 later on, we will warn, but we shouldn't. The only
3192 fix is to keep track of what warnings we are supposed
3193 to emit, and then handle them all at the end of the
3197 struct elf_link_hash_entry
*h
;
3199 h
= elf_link_hash_lookup (hash_table
, name
,
3200 FALSE
, FALSE
, TRUE
);
3202 /* FIXME: What about bfd_link_hash_common? */
3204 && (h
->root
.type
== bfd_link_hash_defined
3205 || h
->root
.type
== bfd_link_hash_defweak
))
3207 /* We don't want to issue this warning. Clobber
3208 the section size so that the warning does not
3209 get copied into the output file. */
3216 msg
= bfd_alloc (abfd
, sz
+ 1);
3220 if (! bfd_get_section_contents (abfd
, s
, msg
, 0, sz
))
3225 if (! (_bfd_generic_link_add_one_symbol
3226 (info
, abfd
, name
, BSF_WARNING
, s
, 0, msg
,
3227 FALSE
, collect
, NULL
)))
3230 if (! info
->relocatable
)
3232 /* Clobber the section size so that the warning does
3233 not get copied into the output file. */
3243 /* If we are creating a shared library, create all the dynamic
3244 sections immediately. We need to attach them to something,
3245 so we attach them to this BFD, provided it is the right
3246 format. FIXME: If there are no input BFD's of the same
3247 format as the output, we can't make a shared library. */
3249 && is_elf_hash_table (hash_table
)
3250 && hash_table
->root
.creator
== abfd
->xvec
3251 && ! hash_table
->dynamic_sections_created
)
3253 if (! _bfd_elf_link_create_dynamic_sections (abfd
, info
))
3257 else if (!is_elf_hash_table (hash_table
))
3262 const char *soname
= NULL
;
3263 struct bfd_link_needed_list
*rpath
= NULL
, *runpath
= NULL
;
3266 /* ld --just-symbols and dynamic objects don't mix very well.
3267 Test for --just-symbols by looking at info set up by
3268 _bfd_elf_link_just_syms. */
3269 if ((s
= abfd
->sections
) != NULL
3270 && s
->sec_info_type
== ELF_INFO_TYPE_JUST_SYMS
)
3273 /* If this dynamic lib was specified on the command line with
3274 --as-needed in effect, then we don't want to add a DT_NEEDED
3275 tag unless the lib is actually used. Similary for libs brought
3276 in by another lib's DT_NEEDED. When --no-add-needed is used
3277 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3278 any dynamic library in DT_NEEDED tags in the dynamic lib at
3280 add_needed
= (elf_dyn_lib_class (abfd
)
3281 & (DYN_AS_NEEDED
| DYN_DT_NEEDED
3282 | DYN_NO_NEEDED
)) == 0;
3284 s
= bfd_get_section_by_name (abfd
, ".dynamic");
3290 unsigned long shlink
;
3292 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
3293 goto error_free_dyn
;
3295 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
3297 goto error_free_dyn
;
3298 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
3300 for (extdyn
= dynbuf
;
3301 extdyn
< dynbuf
+ s
->size
;
3302 extdyn
+= bed
->s
->sizeof_dyn
)
3304 Elf_Internal_Dyn dyn
;
3306 bed
->s
->swap_dyn_in (abfd
, extdyn
, &dyn
);
3307 if (dyn
.d_tag
== DT_SONAME
)
3309 unsigned int tagv
= dyn
.d_un
.d_val
;
3310 soname
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3312 goto error_free_dyn
;
3314 if (dyn
.d_tag
== DT_NEEDED
)
3316 struct bfd_link_needed_list
*n
, **pn
;
3318 unsigned int tagv
= dyn
.d_un
.d_val
;
3320 amt
= sizeof (struct bfd_link_needed_list
);
3321 n
= bfd_alloc (abfd
, amt
);
3322 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3323 if (n
== NULL
|| fnm
== NULL
)
3324 goto error_free_dyn
;
3325 amt
= strlen (fnm
) + 1;
3326 anm
= bfd_alloc (abfd
, amt
);
3328 goto error_free_dyn
;
3329 memcpy (anm
, fnm
, amt
);
3333 for (pn
= & hash_table
->needed
;
3339 if (dyn
.d_tag
== DT_RUNPATH
)
3341 struct bfd_link_needed_list
*n
, **pn
;
3343 unsigned int tagv
= dyn
.d_un
.d_val
;
3345 amt
= sizeof (struct bfd_link_needed_list
);
3346 n
= bfd_alloc (abfd
, amt
);
3347 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3348 if (n
== NULL
|| fnm
== NULL
)
3349 goto error_free_dyn
;
3350 amt
= strlen (fnm
) + 1;
3351 anm
= bfd_alloc (abfd
, amt
);
3353 goto error_free_dyn
;
3354 memcpy (anm
, fnm
, amt
);
3358 for (pn
= & runpath
;
3364 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3365 if (!runpath
&& dyn
.d_tag
== DT_RPATH
)
3367 struct bfd_link_needed_list
*n
, **pn
;
3369 unsigned int tagv
= dyn
.d_un
.d_val
;
3371 amt
= sizeof (struct bfd_link_needed_list
);
3372 n
= bfd_alloc (abfd
, amt
);
3373 fnm
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
3374 if (n
== NULL
|| fnm
== NULL
)
3375 goto error_free_dyn
;
3376 amt
= strlen (fnm
) + 1;
3377 anm
= bfd_alloc (abfd
, amt
);
3384 memcpy (anm
, fnm
, amt
);
3399 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3400 frees all more recently bfd_alloc'd blocks as well. */
3406 struct bfd_link_needed_list
**pn
;
3407 for (pn
= & hash_table
->runpath
;
3414 /* We do not want to include any of the sections in a dynamic
3415 object in the output file. We hack by simply clobbering the
3416 list of sections in the BFD. This could be handled more
3417 cleanly by, say, a new section flag; the existing
3418 SEC_NEVER_LOAD flag is not the one we want, because that one
3419 still implies that the section takes up space in the output
3421 bfd_section_list_clear (abfd
);
3423 /* Find the name to use in a DT_NEEDED entry that refers to this
3424 object. If the object has a DT_SONAME entry, we use it.
3425 Otherwise, if the generic linker stuck something in
3426 elf_dt_name, we use that. Otherwise, we just use the file
3428 if (soname
== NULL
|| *soname
== '\0')
3430 soname
= elf_dt_name (abfd
);
3431 if (soname
== NULL
|| *soname
== '\0')
3432 soname
= bfd_get_filename (abfd
);
3435 /* Save the SONAME because sometimes the linker emulation code
3436 will need to know it. */
3437 elf_dt_name (abfd
) = soname
;
3439 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
3443 /* If we have already included this dynamic object in the
3444 link, just ignore it. There is no reason to include a
3445 particular dynamic object more than once. */
3450 /* If this is a dynamic object, we always link against the .dynsym
3451 symbol table, not the .symtab symbol table. The dynamic linker
3452 will only see the .dynsym symbol table, so there is no reason to
3453 look at .symtab for a dynamic object. */
3455 if (! dynamic
|| elf_dynsymtab (abfd
) == 0)
3456 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3458 hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
3460 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
3462 /* The sh_info field of the symtab header tells us where the
3463 external symbols start. We don't care about the local symbols at
3465 if (elf_bad_symtab (abfd
))
3467 extsymcount
= symcount
;
3472 extsymcount
= symcount
- hdr
->sh_info
;
3473 extsymoff
= hdr
->sh_info
;
3477 if (extsymcount
!= 0)
3479 isymbuf
= bfd_elf_get_elf_syms (abfd
, hdr
, extsymcount
, extsymoff
,
3481 if (isymbuf
== NULL
)
3484 /* We store a pointer to the hash table entry for each external
3486 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
3487 sym_hash
= bfd_alloc (abfd
, amt
);
3488 if (sym_hash
== NULL
)
3489 goto error_free_sym
;
3490 elf_sym_hashes (abfd
) = sym_hash
;
3495 /* Read in any version definitions. */
3496 if (!_bfd_elf_slurp_version_tables (abfd
,
3497 info
->default_imported_symver
))
3498 goto error_free_sym
;
3500 /* Read in the symbol versions, but don't bother to convert them
3501 to internal format. */
3502 if (elf_dynversym (abfd
) != 0)
3504 Elf_Internal_Shdr
*versymhdr
;
3506 versymhdr
= &elf_tdata (abfd
)->dynversym_hdr
;
3507 extversym
= bfd_malloc (versymhdr
->sh_size
);
3508 if (extversym
== NULL
)
3509 goto error_free_sym
;
3510 amt
= versymhdr
->sh_size
;
3511 if (bfd_seek (abfd
, versymhdr
->sh_offset
, SEEK_SET
) != 0
3512 || bfd_bread (extversym
, amt
, abfd
) != amt
)
3513 goto error_free_vers
;
3519 ever
= extversym
!= NULL
? extversym
+ extsymoff
: NULL
;
3520 for (isym
= isymbuf
, isymend
= isymbuf
+ extsymcount
;
3522 isym
++, sym_hash
++, ever
= (ever
!= NULL
? ever
+ 1 : NULL
))
3529 struct elf_link_hash_entry
*h
;
3530 bfd_boolean definition
;
3531 bfd_boolean size_change_ok
;
3532 bfd_boolean type_change_ok
;
3533 bfd_boolean new_weakdef
;
3534 bfd_boolean override
;
3535 unsigned int old_alignment
;
3540 flags
= BSF_NO_FLAGS
;
3542 value
= isym
->st_value
;
3545 bind
= ELF_ST_BIND (isym
->st_info
);
3546 if (bind
== STB_LOCAL
)
3548 /* This should be impossible, since ELF requires that all
3549 global symbols follow all local symbols, and that sh_info
3550 point to the first global symbol. Unfortunately, Irix 5
3554 else if (bind
== STB_GLOBAL
)
3556 if (isym
->st_shndx
!= SHN_UNDEF
3557 && isym
->st_shndx
!= SHN_COMMON
)
3560 else if (bind
== STB_WEAK
)
3564 /* Leave it up to the processor backend. */
3567 if (isym
->st_shndx
== SHN_UNDEF
)
3568 sec
= bfd_und_section_ptr
;
3569 else if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
3571 sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
3573 sec
= bfd_abs_section_ptr
;
3574 else if (sec
->kept_section
)
3576 /* Symbols from discarded section are undefined. */
3577 sec
= bfd_und_section_ptr
;
3578 isym
->st_shndx
= SHN_UNDEF
;
3580 else if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
3583 else if (isym
->st_shndx
== SHN_ABS
)
3584 sec
= bfd_abs_section_ptr
;
3585 else if (isym
->st_shndx
== SHN_COMMON
)
3587 sec
= bfd_com_section_ptr
;
3588 /* What ELF calls the size we call the value. What ELF
3589 calls the value we call the alignment. */
3590 value
= isym
->st_size
;
3594 /* Leave it up to the processor backend. */
3597 name
= bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
3600 goto error_free_vers
;
3602 if (isym
->st_shndx
== SHN_COMMON
3603 && ELF_ST_TYPE (isym
->st_info
) == STT_TLS
)
3605 asection
*tcomm
= bfd_get_section_by_name (abfd
, ".tcommon");
3609 tcomm
= bfd_make_section (abfd
, ".tcommon");
3611 || !bfd_set_section_flags (abfd
, tcomm
, (SEC_ALLOC
3613 | SEC_LINKER_CREATED
3614 | SEC_THREAD_LOCAL
)))
3615 goto error_free_vers
;
3619 else if (add_symbol_hook
)
3621 if (! (*add_symbol_hook
) (abfd
, info
, isym
, &name
, &flags
, &sec
,
3623 goto error_free_vers
;
3625 /* The hook function sets the name to NULL if this symbol
3626 should be skipped for some reason. */
3631 /* Sanity check that all possibilities were handled. */
3634 bfd_set_error (bfd_error_bad_value
);
3635 goto error_free_vers
;
3638 if (bfd_is_und_section (sec
)
3639 || bfd_is_com_section (sec
))
3644 size_change_ok
= FALSE
;
3645 type_change_ok
= get_elf_backend_data (abfd
)->type_change_ok
;
3649 if (is_elf_hash_table (hash_table
))
3651 Elf_Internal_Versym iver
;
3652 unsigned int vernum
= 0;
3657 if (info
->default_imported_symver
)
3658 /* Use the default symbol version created earlier. */
3659 iver
.vs_vers
= elf_tdata (abfd
)->cverdefs
;
3664 _bfd_elf_swap_versym_in (abfd
, ever
, &iver
);
3666 vernum
= iver
.vs_vers
& VERSYM_VERSION
;
3668 /* If this is a hidden symbol, or if it is not version
3669 1, we append the version name to the symbol name.
3670 However, we do not modify a non-hidden absolute
3671 symbol, because it might be the version symbol
3672 itself. FIXME: What if it isn't? */
3673 if ((iver
.vs_vers
& VERSYM_HIDDEN
) != 0
3674 || (vernum
> 1 && ! bfd_is_abs_section (sec
)))
3677 size_t namelen
, verlen
, newlen
;
3680 if (isym
->st_shndx
!= SHN_UNDEF
)
3682 if (vernum
> elf_tdata (abfd
)->cverdefs
)
3684 else if (vernum
> 1)
3686 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
3692 (*_bfd_error_handler
)
3693 (_("%B: %s: invalid version %u (max %d)"),
3695 elf_tdata (abfd
)->cverdefs
);
3696 bfd_set_error (bfd_error_bad_value
);
3697 goto error_free_vers
;
3702 /* We cannot simply test for the number of
3703 entries in the VERNEED section since the
3704 numbers for the needed versions do not start
3706 Elf_Internal_Verneed
*t
;
3709 for (t
= elf_tdata (abfd
)->verref
;
3713 Elf_Internal_Vernaux
*a
;
3715 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
3717 if (a
->vna_other
== vernum
)
3719 verstr
= a
->vna_nodename
;
3728 (*_bfd_error_handler
)
3729 (_("%B: %s: invalid needed version %d"),
3730 abfd
, name
, vernum
);
3731 bfd_set_error (bfd_error_bad_value
);
3732 goto error_free_vers
;
3736 namelen
= strlen (name
);
3737 verlen
= strlen (verstr
);
3738 newlen
= namelen
+ verlen
+ 2;
3739 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3740 && isym
->st_shndx
!= SHN_UNDEF
)
3743 newname
= bfd_alloc (abfd
, newlen
);
3744 if (newname
== NULL
)
3745 goto error_free_vers
;
3746 memcpy (newname
, name
, namelen
);
3747 p
= newname
+ namelen
;
3749 /* If this is a defined non-hidden version symbol,
3750 we add another @ to the name. This indicates the
3751 default version of the symbol. */
3752 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0
3753 && isym
->st_shndx
!= SHN_UNDEF
)
3755 memcpy (p
, verstr
, verlen
+ 1);
3760 if (!_bfd_elf_merge_symbol (abfd
, info
, name
, isym
, &sec
, &value
,
3761 sym_hash
, &skip
, &override
,
3762 &type_change_ok
, &size_change_ok
))
3763 goto error_free_vers
;
3772 while (h
->root
.type
== bfd_link_hash_indirect
3773 || h
->root
.type
== bfd_link_hash_warning
)
3774 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3776 /* Remember the old alignment if this is a common symbol, so
3777 that we don't reduce the alignment later on. We can't
3778 check later, because _bfd_generic_link_add_one_symbol
3779 will set a default for the alignment which we want to
3780 override. We also remember the old bfd where the existing
3781 definition comes from. */
3782 switch (h
->root
.type
)
3787 case bfd_link_hash_defined
:
3788 case bfd_link_hash_defweak
:
3789 old_bfd
= h
->root
.u
.def
.section
->owner
;
3792 case bfd_link_hash_common
:
3793 old_bfd
= h
->root
.u
.c
.p
->section
->owner
;
3794 old_alignment
= h
->root
.u
.c
.p
->alignment_power
;
3798 if (elf_tdata (abfd
)->verdef
!= NULL
3802 h
->verinfo
.verdef
= &elf_tdata (abfd
)->verdef
[vernum
- 1];
3805 if (! (_bfd_generic_link_add_one_symbol
3806 (info
, abfd
, name
, flags
, sec
, value
, NULL
, FALSE
, collect
,
3807 (struct bfd_link_hash_entry
**) sym_hash
)))
3808 goto error_free_vers
;
3811 while (h
->root
.type
== bfd_link_hash_indirect
3812 || h
->root
.type
== bfd_link_hash_warning
)
3813 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3816 new_weakdef
= FALSE
;
3819 && (flags
& BSF_WEAK
) != 0
3820 && ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
3821 && is_elf_hash_table (hash_table
)
3822 && h
->u
.weakdef
== NULL
)
3824 /* Keep a list of all weak defined non function symbols from
3825 a dynamic object, using the weakdef field. Later in this
3826 function we will set the weakdef field to the correct
3827 value. We only put non-function symbols from dynamic
3828 objects on this list, because that happens to be the only
3829 time we need to know the normal symbol corresponding to a
3830 weak symbol, and the information is time consuming to
3831 figure out. If the weakdef field is not already NULL,
3832 then this symbol was already defined by some previous
3833 dynamic object, and we will be using that previous
3834 definition anyhow. */
3836 h
->u
.weakdef
= weaks
;
3841 /* Set the alignment of a common symbol. */
3842 if (isym
->st_shndx
== SHN_COMMON
3843 && h
->root
.type
== bfd_link_hash_common
)
3847 align
= bfd_log2 (isym
->st_value
);
3848 if (align
> old_alignment
3849 /* Permit an alignment power of zero if an alignment of one
3850 is specified and no other alignments have been specified. */
3851 || (isym
->st_value
== 1 && old_alignment
== 0))
3852 h
->root
.u
.c
.p
->alignment_power
= align
;
3854 h
->root
.u
.c
.p
->alignment_power
= old_alignment
;
3857 if (is_elf_hash_table (hash_table
))
3861 /* Check the alignment when a common symbol is involved. This
3862 can change when a common symbol is overridden by a normal
3863 definition or a common symbol is ignored due to the old
3864 normal definition. We need to make sure the maximum
3865 alignment is maintained. */
3866 if ((old_alignment
|| isym
->st_shndx
== SHN_COMMON
)
3867 && h
->root
.type
!= bfd_link_hash_common
)
3869 unsigned int common_align
;
3870 unsigned int normal_align
;
3871 unsigned int symbol_align
;
3875 symbol_align
= ffs (h
->root
.u
.def
.value
) - 1;
3876 if (h
->root
.u
.def
.section
->owner
!= NULL
3877 && (h
->root
.u
.def
.section
->owner
->flags
& DYNAMIC
) == 0)
3879 normal_align
= h
->root
.u
.def
.section
->alignment_power
;
3880 if (normal_align
> symbol_align
)
3881 normal_align
= symbol_align
;
3884 normal_align
= symbol_align
;
3888 common_align
= old_alignment
;
3889 common_bfd
= old_bfd
;
3894 common_align
= bfd_log2 (isym
->st_value
);
3896 normal_bfd
= old_bfd
;
3899 if (normal_align
< common_align
)
3900 (*_bfd_error_handler
)
3901 (_("Warning: alignment %u of symbol `%s' in %B"
3902 " is smaller than %u in %B"),
3903 normal_bfd
, common_bfd
,
3904 1 << normal_align
, name
, 1 << common_align
);
3907 /* Remember the symbol size and type. */
3908 if (isym
->st_size
!= 0
3909 && (definition
|| h
->size
== 0))
3911 if (h
->size
!= 0 && h
->size
!= isym
->st_size
&& ! size_change_ok
)
3912 (*_bfd_error_handler
)
3913 (_("Warning: size of symbol `%s' changed"
3914 " from %lu in %B to %lu in %B"),
3916 name
, (unsigned long) h
->size
,
3917 (unsigned long) isym
->st_size
);
3919 h
->size
= isym
->st_size
;
3922 /* If this is a common symbol, then we always want H->SIZE
3923 to be the size of the common symbol. The code just above
3924 won't fix the size if a common symbol becomes larger. We
3925 don't warn about a size change here, because that is
3926 covered by --warn-common. */
3927 if (h
->root
.type
== bfd_link_hash_common
)
3928 h
->size
= h
->root
.u
.c
.size
;
3930 if (ELF_ST_TYPE (isym
->st_info
) != STT_NOTYPE
3931 && (definition
|| h
->type
== STT_NOTYPE
))
3933 if (h
->type
!= STT_NOTYPE
3934 && h
->type
!= ELF_ST_TYPE (isym
->st_info
)
3935 && ! type_change_ok
)
3936 (*_bfd_error_handler
)
3937 (_("Warning: type of symbol `%s' changed"
3938 " from %d to %d in %B"),
3939 abfd
, name
, h
->type
, ELF_ST_TYPE (isym
->st_info
));
3941 h
->type
= ELF_ST_TYPE (isym
->st_info
);
3944 /* If st_other has a processor-specific meaning, specific
3945 code might be needed here. We never merge the visibility
3946 attribute with the one from a dynamic object. */
3947 if (bed
->elf_backend_merge_symbol_attribute
)
3948 (*bed
->elf_backend_merge_symbol_attribute
) (h
, isym
, definition
,
3951 /* If this symbol has default visibility and the user has requested
3952 we not re-export it, then mark it as hidden. */
3953 if (definition
&& !dynamic
3955 || (abfd
->my_archive
&& abfd
->my_archive
->no_export
))
3956 && ELF_ST_VISIBILITY (isym
->st_other
) != STV_INTERNAL
)
3957 isym
->st_other
= STV_HIDDEN
| (isym
->st_other
& ~ ELF_ST_VISIBILITY (-1));
3959 if (isym
->st_other
!= 0 && !dynamic
)
3961 unsigned char hvis
, symvis
, other
, nvis
;
3963 /* Take the balance of OTHER from the definition. */
3964 other
= (definition
? isym
->st_other
: h
->other
);
3965 other
&= ~ ELF_ST_VISIBILITY (-1);
3967 /* Combine visibilities, using the most constraining one. */
3968 hvis
= ELF_ST_VISIBILITY (h
->other
);
3969 symvis
= ELF_ST_VISIBILITY (isym
->st_other
);
3975 nvis
= hvis
< symvis
? hvis
: symvis
;
3977 h
->other
= other
| nvis
;
3980 /* Set a flag in the hash table entry indicating the type of
3981 reference or definition we just found. Keep a count of
3982 the number of dynamic symbols we find. A dynamic symbol
3983 is one which is referenced or defined by both a regular
3984 object and a shared object. */
3991 if (bind
!= STB_WEAK
)
3992 h
->ref_regular_nonweak
= 1;
3996 if (! info
->executable
4009 || (h
->u
.weakdef
!= NULL
4011 && h
->u
.weakdef
->dynindx
!= -1))
4015 /* Check to see if we need to add an indirect symbol for
4016 the default name. */
4017 if (definition
|| h
->root
.type
== bfd_link_hash_common
)
4018 if (!_bfd_elf_add_default_symbol (abfd
, info
, h
, name
, isym
,
4019 &sec
, &value
, &dynsym
,
4021 goto error_free_vers
;
4023 if (definition
&& !dynamic
)
4025 char *p
= strchr (name
, ELF_VER_CHR
);
4026 if (p
!= NULL
&& p
[1] != ELF_VER_CHR
)
4028 /* Queue non-default versions so that .symver x, x@FOO
4029 aliases can be checked. */
4030 if (! nondeflt_vers
)
4032 amt
= (isymend
- isym
+ 1)
4033 * sizeof (struct elf_link_hash_entry
*);
4034 nondeflt_vers
= bfd_malloc (amt
);
4036 nondeflt_vers
[nondeflt_vers_cnt
++] = h
;
4040 if (dynsym
&& h
->dynindx
== -1)
4042 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4043 goto error_free_vers
;
4044 if (h
->u
.weakdef
!= NULL
4046 && h
->u
.weakdef
->dynindx
== -1)
4048 if (! bfd_elf_link_record_dynamic_symbol (info
, h
->u
.weakdef
))
4049 goto error_free_vers
;
4052 else if (dynsym
&& h
->dynindx
!= -1)
4053 /* If the symbol already has a dynamic index, but
4054 visibility says it should not be visible, turn it into
4056 switch (ELF_ST_VISIBILITY (h
->other
))
4060 (*bed
->elf_backend_hide_symbol
) (info
, h
, TRUE
);
4071 const char *soname
= elf_dt_name (abfd
);
4073 /* A symbol from a library loaded via DT_NEEDED of some
4074 other library is referenced by a regular object.
4075 Add a DT_NEEDED entry for it. Issue an error if
4076 --no-add-needed is used. */
4077 if ((elf_dyn_lib_class (abfd
) & DYN_NO_NEEDED
) != 0)
4079 (*_bfd_error_handler
)
4080 (_("%s: invalid DSO for symbol `%s' definition"),
4082 bfd_set_error (bfd_error_bad_value
);
4083 goto error_free_vers
;
4086 elf_dyn_lib_class (abfd
) &= ~DYN_AS_NEEDED
;
4089 ret
= elf_add_dt_needed_tag (abfd
, info
, soname
, add_needed
);
4091 goto error_free_vers
;
4093 BFD_ASSERT (ret
== 0);
4098 /* Now that all the symbols from this input file are created, handle
4099 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4100 if (nondeflt_vers
!= NULL
)
4102 bfd_size_type cnt
, symidx
;
4104 for (cnt
= 0; cnt
< nondeflt_vers_cnt
; ++cnt
)
4106 struct elf_link_hash_entry
*h
= nondeflt_vers
[cnt
], *hi
;
4107 char *shortname
, *p
;
4109 p
= strchr (h
->root
.root
.string
, ELF_VER_CHR
);
4111 || (h
->root
.type
!= bfd_link_hash_defined
4112 && h
->root
.type
!= bfd_link_hash_defweak
))
4115 amt
= p
- h
->root
.root
.string
;
4116 shortname
= bfd_malloc (amt
+ 1);
4117 memcpy (shortname
, h
->root
.root
.string
, amt
);
4118 shortname
[amt
] = '\0';
4120 hi
= (struct elf_link_hash_entry
*)
4121 bfd_link_hash_lookup (&hash_table
->root
, shortname
,
4122 FALSE
, FALSE
, FALSE
);
4124 && hi
->root
.type
== h
->root
.type
4125 && hi
->root
.u
.def
.value
== h
->root
.u
.def
.value
4126 && hi
->root
.u
.def
.section
== h
->root
.u
.def
.section
)
4128 (*bed
->elf_backend_hide_symbol
) (info
, hi
, TRUE
);
4129 hi
->root
.type
= bfd_link_hash_indirect
;
4130 hi
->root
.u
.i
.link
= (struct bfd_link_hash_entry
*) h
;
4131 (*bed
->elf_backend_copy_indirect_symbol
) (bed
, h
, hi
);
4132 sym_hash
= elf_sym_hashes (abfd
);
4134 for (symidx
= 0; symidx
< extsymcount
; ++symidx
)
4135 if (sym_hash
[symidx
] == hi
)
4137 sym_hash
[symidx
] = h
;
4143 free (nondeflt_vers
);
4144 nondeflt_vers
= NULL
;
4147 if (extversym
!= NULL
)
4153 if (isymbuf
!= NULL
)
4158 && (elf_dyn_lib_class (abfd
) & DYN_AS_NEEDED
) != 0)
4160 /* Remove symbols defined in an as-needed shared lib that wasn't
4162 struct elf_smash_syms_data inf
;
4163 inf
.not_needed
= abfd
;
4164 inf
.htab
= hash_table
;
4165 inf
.twiddled
= FALSE
;
4166 elf_link_hash_traverse (hash_table
, elf_smash_syms
, &inf
);
4168 bfd_link_repair_undef_list (&hash_table
->root
);
4172 /* Now set the weakdefs field correctly for all the weak defined
4173 symbols we found. The only way to do this is to search all the
4174 symbols. Since we only need the information for non functions in
4175 dynamic objects, that's the only time we actually put anything on
4176 the list WEAKS. We need this information so that if a regular
4177 object refers to a symbol defined weakly in a dynamic object, the
4178 real symbol in the dynamic object is also put in the dynamic
4179 symbols; we also must arrange for both symbols to point to the
4180 same memory location. We could handle the general case of symbol
4181 aliasing, but a general symbol alias can only be generated in
4182 assembler code, handling it correctly would be very time
4183 consuming, and other ELF linkers don't handle general aliasing
4187 struct elf_link_hash_entry
**hpp
;
4188 struct elf_link_hash_entry
**hppend
;
4189 struct elf_link_hash_entry
**sorted_sym_hash
;
4190 struct elf_link_hash_entry
*h
;
4193 /* Since we have to search the whole symbol list for each weak
4194 defined symbol, search time for N weak defined symbols will be
4195 O(N^2). Binary search will cut it down to O(NlogN). */
4196 amt
= extsymcount
* sizeof (struct elf_link_hash_entry
*);
4197 sorted_sym_hash
= bfd_malloc (amt
);
4198 if (sorted_sym_hash
== NULL
)
4200 sym_hash
= sorted_sym_hash
;
4201 hpp
= elf_sym_hashes (abfd
);
4202 hppend
= hpp
+ extsymcount
;
4204 for (; hpp
< hppend
; hpp
++)
4208 && h
->root
.type
== bfd_link_hash_defined
4209 && h
->type
!= STT_FUNC
)
4217 qsort (sorted_sym_hash
, sym_count
,
4218 sizeof (struct elf_link_hash_entry
*),
4221 while (weaks
!= NULL
)
4223 struct elf_link_hash_entry
*hlook
;
4230 weaks
= hlook
->u
.weakdef
;
4231 hlook
->u
.weakdef
= NULL
;
4233 BFD_ASSERT (hlook
->root
.type
== bfd_link_hash_defined
4234 || hlook
->root
.type
== bfd_link_hash_defweak
4235 || hlook
->root
.type
== bfd_link_hash_common
4236 || hlook
->root
.type
== bfd_link_hash_indirect
);
4237 slook
= hlook
->root
.u
.def
.section
;
4238 vlook
= hlook
->root
.u
.def
.value
;
4245 bfd_signed_vma vdiff
;
4247 h
= sorted_sym_hash
[idx
];
4248 vdiff
= vlook
- h
->root
.u
.def
.value
;
4255 long sdiff
= slook
->id
- h
->root
.u
.def
.section
->id
;
4268 /* We didn't find a value/section match. */
4272 for (i
= ilook
; i
< sym_count
; i
++)
4274 h
= sorted_sym_hash
[i
];
4276 /* Stop if value or section doesn't match. */
4277 if (h
->root
.u
.def
.value
!= vlook
4278 || h
->root
.u
.def
.section
!= slook
)
4280 else if (h
!= hlook
)
4282 hlook
->u
.weakdef
= h
;
4284 /* If the weak definition is in the list of dynamic
4285 symbols, make sure the real definition is put
4287 if (hlook
->dynindx
!= -1 && h
->dynindx
== -1)
4289 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
4293 /* If the real definition is in the list of dynamic
4294 symbols, make sure the weak definition is put
4295 there as well. If we don't do this, then the
4296 dynamic loader might not merge the entries for the
4297 real definition and the weak definition. */
4298 if (h
->dynindx
!= -1 && hlook
->dynindx
== -1)
4300 if (! bfd_elf_link_record_dynamic_symbol (info
, hlook
))
4308 free (sorted_sym_hash
);
4311 check_directives
= get_elf_backend_data (abfd
)->check_directives
;
4312 if (check_directives
)
4313 check_directives (abfd
, info
);
4315 /* If this object is the same format as the output object, and it is
4316 not a shared library, then let the backend look through the
4319 This is required to build global offset table entries and to
4320 arrange for dynamic relocs. It is not required for the
4321 particular common case of linking non PIC code, even when linking
4322 against shared libraries, but unfortunately there is no way of
4323 knowing whether an object file has been compiled PIC or not.
4324 Looking through the relocs is not particularly time consuming.
4325 The problem is that we must either (1) keep the relocs in memory,
4326 which causes the linker to require additional runtime memory or
4327 (2) read the relocs twice from the input file, which wastes time.
4328 This would be a good case for using mmap.
4330 I have no idea how to handle linking PIC code into a file of a
4331 different format. It probably can't be done. */
4332 check_relocs
= get_elf_backend_data (abfd
)->check_relocs
;
4334 && is_elf_hash_table (hash_table
)
4335 && hash_table
->root
.creator
== abfd
->xvec
4336 && check_relocs
!= NULL
)
4340 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
4342 Elf_Internal_Rela
*internal_relocs
;
4345 if ((o
->flags
& SEC_RELOC
) == 0
4346 || o
->reloc_count
== 0
4347 || ((info
->strip
== strip_all
|| info
->strip
== strip_debugger
)
4348 && (o
->flags
& SEC_DEBUGGING
) != 0)
4349 || bfd_is_abs_section (o
->output_section
))
4352 internal_relocs
= _bfd_elf_link_read_relocs (abfd
, o
, NULL
, NULL
,
4354 if (internal_relocs
== NULL
)
4357 ok
= (*check_relocs
) (abfd
, info
, o
, internal_relocs
);
4359 if (elf_section_data (o
)->relocs
!= internal_relocs
)
4360 free (internal_relocs
);
4367 /* If this is a non-traditional link, try to optimize the handling
4368 of the .stab/.stabstr sections. */
4370 && ! info
->traditional_format
4371 && is_elf_hash_table (hash_table
)
4372 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
4376 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
4377 if (stabstr
!= NULL
)
4379 bfd_size_type string_offset
= 0;
4382 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
4383 if (strncmp (".stab", stab
->name
, 5) == 0
4384 && (!stab
->name
[5] ||
4385 (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6])))
4386 && (stab
->flags
& SEC_MERGE
) == 0
4387 && !bfd_is_abs_section (stab
->output_section
))
4389 struct bfd_elf_section_data
*secdata
;
4391 secdata
= elf_section_data (stab
);
4392 if (! _bfd_link_section_stabs (abfd
,
4393 &hash_table
->stab_info
,
4398 if (secdata
->sec_info
)
4399 stab
->sec_info_type
= ELF_INFO_TYPE_STABS
;
4404 if (is_elf_hash_table (hash_table
) && add_needed
)
4406 /* Add this bfd to the loaded list. */
4407 struct elf_link_loaded_list
*n
;
4409 n
= bfd_alloc (abfd
, sizeof (struct elf_link_loaded_list
));
4413 n
->next
= hash_table
->loaded
;
4414 hash_table
->loaded
= n
;
4420 if (nondeflt_vers
!= NULL
)
4421 free (nondeflt_vers
);
4422 if (extversym
!= NULL
)
4425 if (isymbuf
!= NULL
)
4431 /* Return the linker hash table entry of a symbol that might be
4432 satisfied by an archive symbol. Return -1 on error. */
4434 struct elf_link_hash_entry
*
4435 _bfd_elf_archive_symbol_lookup (bfd
*abfd
,
4436 struct bfd_link_info
*info
,
4439 struct elf_link_hash_entry
*h
;
4443 h
= elf_link_hash_lookup (elf_hash_table (info
), name
, FALSE
, FALSE
, FALSE
);
4447 /* If this is a default version (the name contains @@), look up the
4448 symbol again with only one `@' as well as without the version.
4449 The effect is that references to the symbol with and without the
4450 version will be matched by the default symbol in the archive. */
4452 p
= strchr (name
, ELF_VER_CHR
);
4453 if (p
== NULL
|| p
[1] != ELF_VER_CHR
)
4456 /* First check with only one `@'. */
4457 len
= strlen (name
);
4458 copy
= bfd_alloc (abfd
, len
);
4460 return (struct elf_link_hash_entry
*) 0 - 1;
4462 first
= p
- name
+ 1;
4463 memcpy (copy
, name
, first
);
4464 memcpy (copy
+ first
, name
+ first
+ 1, len
- first
);
4466 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
, FALSE
, FALSE
, FALSE
);
4469 /* We also need to check references to the symbol without the
4471 copy
[first
- 1] = '\0';
4472 h
= elf_link_hash_lookup (elf_hash_table (info
), copy
,
4473 FALSE
, FALSE
, FALSE
);
4476 bfd_release (abfd
, copy
);
4480 /* Add symbols from an ELF archive file to the linker hash table. We
4481 don't use _bfd_generic_link_add_archive_symbols because of a
4482 problem which arises on UnixWare. The UnixWare libc.so is an
4483 archive which includes an entry libc.so.1 which defines a bunch of
4484 symbols. The libc.so archive also includes a number of other
4485 object files, which also define symbols, some of which are the same
4486 as those defined in libc.so.1. Correct linking requires that we
4487 consider each object file in turn, and include it if it defines any
4488 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4489 this; it looks through the list of undefined symbols, and includes
4490 any object file which defines them. When this algorithm is used on
4491 UnixWare, it winds up pulling in libc.so.1 early and defining a
4492 bunch of symbols. This means that some of the other objects in the
4493 archive are not included in the link, which is incorrect since they
4494 precede libc.so.1 in the archive.
4496 Fortunately, ELF archive handling is simpler than that done by
4497 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4498 oddities. In ELF, if we find a symbol in the archive map, and the
4499 symbol is currently undefined, we know that we must pull in that
4502 Unfortunately, we do have to make multiple passes over the symbol
4503 table until nothing further is resolved. */
4506 elf_link_add_archive_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4509 bfd_boolean
*defined
= NULL
;
4510 bfd_boolean
*included
= NULL
;
4514 const struct elf_backend_data
*bed
;
4515 struct elf_link_hash_entry
* (*archive_symbol_lookup
)
4516 (bfd
*, struct bfd_link_info
*, const char *);
4518 if (! bfd_has_map (abfd
))
4520 /* An empty archive is a special case. */
4521 if (bfd_openr_next_archived_file (abfd
, NULL
) == NULL
)
4523 bfd_set_error (bfd_error_no_armap
);
4527 /* Keep track of all symbols we know to be already defined, and all
4528 files we know to be already included. This is to speed up the
4529 second and subsequent passes. */
4530 c
= bfd_ardata (abfd
)->symdef_count
;
4534 amt
*= sizeof (bfd_boolean
);
4535 defined
= bfd_zmalloc (amt
);
4536 included
= bfd_zmalloc (amt
);
4537 if (defined
== NULL
|| included
== NULL
)
4540 symdefs
= bfd_ardata (abfd
)->symdefs
;
4541 bed
= get_elf_backend_data (abfd
);
4542 archive_symbol_lookup
= bed
->elf_backend_archive_symbol_lookup
;
4555 symdefend
= symdef
+ c
;
4556 for (i
= 0; symdef
< symdefend
; symdef
++, i
++)
4558 struct elf_link_hash_entry
*h
;
4560 struct bfd_link_hash_entry
*undefs_tail
;
4563 if (defined
[i
] || included
[i
])
4565 if (symdef
->file_offset
== last
)
4571 h
= archive_symbol_lookup (abfd
, info
, symdef
->name
);
4572 if (h
== (struct elf_link_hash_entry
*) 0 - 1)
4578 if (h
->root
.type
== bfd_link_hash_common
)
4580 /* We currently have a common symbol. The archive map contains
4581 a reference to this symbol, so we may want to include it. We
4582 only want to include it however, if this archive element
4583 contains a definition of the symbol, not just another common
4586 Unfortunately some archivers (including GNU ar) will put
4587 declarations of common symbols into their archive maps, as
4588 well as real definitions, so we cannot just go by the archive
4589 map alone. Instead we must read in the element's symbol
4590 table and check that to see what kind of symbol definition
4592 if (! elf_link_is_defined_archive_symbol (abfd
, symdef
))
4595 else if (h
->root
.type
!= bfd_link_hash_undefined
)
4597 if (h
->root
.type
!= bfd_link_hash_undefweak
)
4602 /* We need to include this archive member. */
4603 element
= _bfd_get_elt_at_filepos (abfd
, symdef
->file_offset
);
4604 if (element
== NULL
)
4607 if (! bfd_check_format (element
, bfd_object
))
4610 /* Doublecheck that we have not included this object
4611 already--it should be impossible, but there may be
4612 something wrong with the archive. */
4613 if (element
->archive_pass
!= 0)
4615 bfd_set_error (bfd_error_bad_value
);
4618 element
->archive_pass
= 1;
4620 undefs_tail
= info
->hash
->undefs_tail
;
4622 if (! (*info
->callbacks
->add_archive_element
) (info
, element
,
4625 if (! bfd_link_add_symbols (element
, info
))
4628 /* If there are any new undefined symbols, we need to make
4629 another pass through the archive in order to see whether
4630 they can be defined. FIXME: This isn't perfect, because
4631 common symbols wind up on undefs_tail and because an
4632 undefined symbol which is defined later on in this pass
4633 does not require another pass. This isn't a bug, but it
4634 does make the code less efficient than it could be. */
4635 if (undefs_tail
!= info
->hash
->undefs_tail
)
4638 /* Look backward to mark all symbols from this object file
4639 which we have already seen in this pass. */
4643 included
[mark
] = TRUE
;
4648 while (symdefs
[mark
].file_offset
== symdef
->file_offset
);
4650 /* We mark subsequent symbols from this object file as we go
4651 on through the loop. */
4652 last
= symdef
->file_offset
;
4663 if (defined
!= NULL
)
4665 if (included
!= NULL
)
4670 /* Given an ELF BFD, add symbols to the global hash table as
4674 bfd_elf_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
4676 switch (bfd_get_format (abfd
))
4679 return elf_link_add_object_symbols (abfd
, info
);
4681 return elf_link_add_archive_symbols (abfd
, info
);
4683 bfd_set_error (bfd_error_wrong_format
);
4688 /* This function will be called though elf_link_hash_traverse to store
4689 all hash value of the exported symbols in an array. */
4692 elf_collect_hash_codes (struct elf_link_hash_entry
*h
, void *data
)
4694 unsigned long **valuep
= data
;
4700 if (h
->root
.type
== bfd_link_hash_warning
)
4701 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4703 /* Ignore indirect symbols. These are added by the versioning code. */
4704 if (h
->dynindx
== -1)
4707 name
= h
->root
.root
.string
;
4708 p
= strchr (name
, ELF_VER_CHR
);
4711 alc
= bfd_malloc (p
- name
+ 1);
4712 memcpy (alc
, name
, p
- name
);
4713 alc
[p
- name
] = '\0';
4717 /* Compute the hash value. */
4718 ha
= bfd_elf_hash (name
);
4720 /* Store the found hash value in the array given as the argument. */
4723 /* And store it in the struct so that we can put it in the hash table
4725 h
->u
.elf_hash_value
= ha
;
4733 /* Array used to determine the number of hash table buckets to use
4734 based on the number of symbols there are. If there are fewer than
4735 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4736 fewer than 37 we use 17 buckets, and so forth. We never use more
4737 than 32771 buckets. */
4739 static const size_t elf_buckets
[] =
4741 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4745 /* Compute bucket count for hashing table. We do not use a static set
4746 of possible tables sizes anymore. Instead we determine for all
4747 possible reasonable sizes of the table the outcome (i.e., the
4748 number of collisions etc) and choose the best solution. The
4749 weighting functions are not too simple to allow the table to grow
4750 without bounds. Instead one of the weighting factors is the size.
4751 Therefore the result is always a good payoff between few collisions
4752 (= short chain lengths) and table size. */
4754 compute_bucket_count (struct bfd_link_info
*info
)
4756 size_t dynsymcount
= elf_hash_table (info
)->dynsymcount
;
4757 size_t best_size
= 0;
4758 unsigned long int *hashcodes
;
4759 unsigned long int *hashcodesp
;
4760 unsigned long int i
;
4763 /* Compute the hash values for all exported symbols. At the same
4764 time store the values in an array so that we could use them for
4767 amt
*= sizeof (unsigned long int);
4768 hashcodes
= bfd_malloc (amt
);
4769 if (hashcodes
== NULL
)
4771 hashcodesp
= hashcodes
;
4773 /* Put all hash values in HASHCODES. */
4774 elf_link_hash_traverse (elf_hash_table (info
),
4775 elf_collect_hash_codes
, &hashcodesp
);
4777 /* We have a problem here. The following code to optimize the table
4778 size requires an integer type with more the 32 bits. If
4779 BFD_HOST_U_64_BIT is set we know about such a type. */
4780 #ifdef BFD_HOST_U_64_BIT
4783 unsigned long int nsyms
= hashcodesp
- hashcodes
;
4786 BFD_HOST_U_64_BIT best_chlen
= ~((BFD_HOST_U_64_BIT
) 0);
4787 unsigned long int *counts
;
4788 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
4789 const struct elf_backend_data
*bed
= get_elf_backend_data (dynobj
);
4791 /* Possible optimization parameters: if we have NSYMS symbols we say
4792 that the hashing table must at least have NSYMS/4 and at most
4794 minsize
= nsyms
/ 4;
4797 best_size
= maxsize
= nsyms
* 2;
4799 /* Create array where we count the collisions in. We must use bfd_malloc
4800 since the size could be large. */
4802 amt
*= sizeof (unsigned long int);
4803 counts
= bfd_malloc (amt
);
4810 /* Compute the "optimal" size for the hash table. The criteria is a
4811 minimal chain length. The minor criteria is (of course) the size
4813 for (i
= minsize
; i
< maxsize
; ++i
)
4815 /* Walk through the array of hashcodes and count the collisions. */
4816 BFD_HOST_U_64_BIT max
;
4817 unsigned long int j
;
4818 unsigned long int fact
;
4820 memset (counts
, '\0', i
* sizeof (unsigned long int));
4822 /* Determine how often each hash bucket is used. */
4823 for (j
= 0; j
< nsyms
; ++j
)
4824 ++counts
[hashcodes
[j
] % i
];
4826 /* For the weight function we need some information about the
4827 pagesize on the target. This is information need not be 100%
4828 accurate. Since this information is not available (so far) we
4829 define it here to a reasonable default value. If it is crucial
4830 to have a better value some day simply define this value. */
4831 # ifndef BFD_TARGET_PAGESIZE
4832 # define BFD_TARGET_PAGESIZE (4096)
4835 /* We in any case need 2 + NSYMS entries for the size values and
4837 max
= (2 + nsyms
) * (bed
->s
->arch_size
/ 8);
4840 /* Variant 1: optimize for short chains. We add the squares
4841 of all the chain lengths (which favors many small chain
4842 over a few long chains). */
4843 for (j
= 0; j
< i
; ++j
)
4844 max
+= counts
[j
] * counts
[j
];
4846 /* This adds penalties for the overall size of the table. */
4847 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4850 /* Variant 2: Optimize a lot more for small table. Here we
4851 also add squares of the size but we also add penalties for
4852 empty slots (the +1 term). */
4853 for (j
= 0; j
< i
; ++j
)
4854 max
+= (1 + counts
[j
]) * (1 + counts
[j
]);
4856 /* The overall size of the table is considered, but not as
4857 strong as in variant 1, where it is squared. */
4858 fact
= i
/ (BFD_TARGET_PAGESIZE
/ (bed
->s
->arch_size
/ 8)) + 1;
4862 /* Compare with current best results. */
4863 if (max
< best_chlen
)
4873 #endif /* defined (BFD_HOST_U_64_BIT) */
4875 /* This is the fallback solution if no 64bit type is available or if we
4876 are not supposed to spend much time on optimizations. We select the
4877 bucket count using a fixed set of numbers. */
4878 for (i
= 0; elf_buckets
[i
] != 0; i
++)
4880 best_size
= elf_buckets
[i
];
4881 if (dynsymcount
< elf_buckets
[i
+ 1])
4886 /* Free the arrays we needed. */
4892 /* Set up the sizes and contents of the ELF dynamic sections. This is
4893 called by the ELF linker emulation before_allocation routine. We
4894 must set the sizes of the sections before the linker sets the
4895 addresses of the various sections. */
4898 bfd_elf_size_dynamic_sections (bfd
*output_bfd
,
4901 const char *filter_shlib
,
4902 const char * const *auxiliary_filters
,
4903 struct bfd_link_info
*info
,
4904 asection
**sinterpptr
,
4905 struct bfd_elf_version_tree
*verdefs
)
4907 bfd_size_type soname_indx
;
4909 const struct elf_backend_data
*bed
;
4910 struct elf_assign_sym_version_info asvinfo
;
4914 soname_indx
= (bfd_size_type
) -1;
4916 if (!is_elf_hash_table (info
->hash
))
4919 elf_tdata (output_bfd
)->relro
= info
->relro
;
4920 if (info
->execstack
)
4921 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| PF_X
;
4922 else if (info
->noexecstack
)
4923 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
;
4927 asection
*notesec
= NULL
;
4930 for (inputobj
= info
->input_bfds
;
4932 inputobj
= inputobj
->link_next
)
4936 if (inputobj
->flags
& (DYNAMIC
| BFD_LINKER_CREATED
))
4938 s
= bfd_get_section_by_name (inputobj
, ".note.GNU-stack");
4941 if (s
->flags
& SEC_CODE
)
4950 elf_tdata (output_bfd
)->stack_flags
= PF_R
| PF_W
| exec
;
4951 if (exec
&& info
->relocatable
4952 && notesec
->output_section
!= bfd_abs_section_ptr
)
4953 notesec
->output_section
->flags
|= SEC_CODE
;
4957 /* Any syms created from now on start with -1 in
4958 got.refcount/offset and plt.refcount/offset. */
4959 elf_hash_table (info
)->init_refcount
= elf_hash_table (info
)->init_offset
;
4961 /* The backend may have to create some sections regardless of whether
4962 we're dynamic or not. */
4963 bed
= get_elf_backend_data (output_bfd
);
4964 if (bed
->elf_backend_always_size_sections
4965 && ! (*bed
->elf_backend_always_size_sections
) (output_bfd
, info
))
4968 dynobj
= elf_hash_table (info
)->dynobj
;
4970 /* If there were no dynamic objects in the link, there is nothing to
4975 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info
))
4978 if (elf_hash_table (info
)->dynamic_sections_created
)
4980 struct elf_info_failed eif
;
4981 struct elf_link_hash_entry
*h
;
4983 struct bfd_elf_version_tree
*t
;
4984 struct bfd_elf_version_expr
*d
;
4985 bfd_boolean all_defined
;
4987 *sinterpptr
= bfd_get_section_by_name (dynobj
, ".interp");
4988 BFD_ASSERT (*sinterpptr
!= NULL
|| !info
->executable
);
4992 soname_indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
4994 if (soname_indx
== (bfd_size_type
) -1
4995 || !_bfd_elf_add_dynamic_entry (info
, DT_SONAME
, soname_indx
))
5001 if (!_bfd_elf_add_dynamic_entry (info
, DT_SYMBOLIC
, 0))
5003 info
->flags
|= DF_SYMBOLIC
;
5010 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
, rpath
,
5012 if (indx
== (bfd_size_type
) -1
5013 || !_bfd_elf_add_dynamic_entry (info
, DT_RPATH
, indx
))
5016 if (info
->new_dtags
)
5018 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
, indx
);
5019 if (!_bfd_elf_add_dynamic_entry (info
, DT_RUNPATH
, indx
))
5024 if (filter_shlib
!= NULL
)
5028 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5029 filter_shlib
, TRUE
);
5030 if (indx
== (bfd_size_type
) -1
5031 || !_bfd_elf_add_dynamic_entry (info
, DT_FILTER
, indx
))
5035 if (auxiliary_filters
!= NULL
)
5037 const char * const *p
;
5039 for (p
= auxiliary_filters
; *p
!= NULL
; p
++)
5043 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5045 if (indx
== (bfd_size_type
) -1
5046 || !_bfd_elf_add_dynamic_entry (info
, DT_AUXILIARY
, indx
))
5052 eif
.verdefs
= verdefs
;
5055 /* If we are supposed to export all symbols into the dynamic symbol
5056 table (this is not the normal case), then do so. */
5057 if (info
->export_dynamic
)
5059 elf_link_hash_traverse (elf_hash_table (info
),
5060 _bfd_elf_export_symbol
,
5066 /* Make all global versions with definition. */
5067 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5068 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5069 if (!d
->symver
&& d
->symbol
)
5071 const char *verstr
, *name
;
5072 size_t namelen
, verlen
, newlen
;
5074 struct elf_link_hash_entry
*newh
;
5077 namelen
= strlen (name
);
5079 verlen
= strlen (verstr
);
5080 newlen
= namelen
+ verlen
+ 3;
5082 newname
= bfd_malloc (newlen
);
5083 if (newname
== NULL
)
5085 memcpy (newname
, name
, namelen
);
5087 /* Check the hidden versioned definition. */
5088 p
= newname
+ namelen
;
5090 memcpy (p
, verstr
, verlen
+ 1);
5091 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5092 newname
, FALSE
, FALSE
,
5095 || (newh
->root
.type
!= bfd_link_hash_defined
5096 && newh
->root
.type
!= bfd_link_hash_defweak
))
5098 /* Check the default versioned definition. */
5100 memcpy (p
, verstr
, verlen
+ 1);
5101 newh
= elf_link_hash_lookup (elf_hash_table (info
),
5102 newname
, FALSE
, FALSE
,
5107 /* Mark this version if there is a definition and it is
5108 not defined in a shared object. */
5110 && !newh
->def_dynamic
5111 && (newh
->root
.type
== bfd_link_hash_defined
5112 || newh
->root
.type
== bfd_link_hash_defweak
))
5116 /* Attach all the symbols to their version information. */
5117 asvinfo
.output_bfd
= output_bfd
;
5118 asvinfo
.info
= info
;
5119 asvinfo
.verdefs
= verdefs
;
5120 asvinfo
.failed
= FALSE
;
5122 elf_link_hash_traverse (elf_hash_table (info
),
5123 _bfd_elf_link_assign_sym_version
,
5128 if (!info
->allow_undefined_version
)
5130 /* Check if all global versions have a definition. */
5132 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5133 for (d
= t
->globals
.list
; d
!= NULL
; d
= d
->next
)
5134 if (!d
->symver
&& !d
->script
)
5136 (*_bfd_error_handler
)
5137 (_("%s: undefined version: %s"),
5138 d
->pattern
, t
->name
);
5139 all_defined
= FALSE
;
5144 bfd_set_error (bfd_error_bad_value
);
5149 /* Find all symbols which were defined in a dynamic object and make
5150 the backend pick a reasonable value for them. */
5151 elf_link_hash_traverse (elf_hash_table (info
),
5152 _bfd_elf_adjust_dynamic_symbol
,
5157 /* Add some entries to the .dynamic section. We fill in some of the
5158 values later, in bfd_elf_final_link, but we must add the entries
5159 now so that we know the final size of the .dynamic section. */
5161 /* If there are initialization and/or finalization functions to
5162 call then add the corresponding DT_INIT/DT_FINI entries. */
5163 h
= (info
->init_function
5164 ? elf_link_hash_lookup (elf_hash_table (info
),
5165 info
->init_function
, FALSE
,
5172 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT
, 0))
5175 h
= (info
->fini_function
5176 ? elf_link_hash_lookup (elf_hash_table (info
),
5177 info
->fini_function
, FALSE
,
5184 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI
, 0))
5188 if (bfd_get_section_by_name (output_bfd
, ".preinit_array") != NULL
)
5190 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5191 if (! info
->executable
)
5196 for (sub
= info
->input_bfds
; sub
!= NULL
;
5197 sub
= sub
->link_next
)
5198 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
5199 if (elf_section_data (o
)->this_hdr
.sh_type
5200 == SHT_PREINIT_ARRAY
)
5202 (*_bfd_error_handler
)
5203 (_("%B: .preinit_array section is not allowed in DSO"),
5208 bfd_set_error (bfd_error_nonrepresentable_section
);
5212 if (!_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAY
, 0)
5213 || !_bfd_elf_add_dynamic_entry (info
, DT_PREINIT_ARRAYSZ
, 0))
5216 if (bfd_get_section_by_name (output_bfd
, ".init_array") != NULL
)
5218 if (!_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAY
, 0)
5219 || !_bfd_elf_add_dynamic_entry (info
, DT_INIT_ARRAYSZ
, 0))
5222 if (bfd_get_section_by_name (output_bfd
, ".fini_array") != NULL
)
5224 if (!_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAY
, 0)
5225 || !_bfd_elf_add_dynamic_entry (info
, DT_FINI_ARRAYSZ
, 0))
5229 dynstr
= bfd_get_section_by_name (dynobj
, ".dynstr");
5230 /* If .dynstr is excluded from the link, we don't want any of
5231 these tags. Strictly, we should be checking each section
5232 individually; This quick check covers for the case where
5233 someone does a /DISCARD/ : { *(*) }. */
5234 if (dynstr
!= NULL
&& dynstr
->output_section
!= bfd_abs_section_ptr
)
5236 bfd_size_type strsize
;
5238 strsize
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5239 if (!_bfd_elf_add_dynamic_entry (info
, DT_HASH
, 0)
5240 || !_bfd_elf_add_dynamic_entry (info
, DT_STRTAB
, 0)
5241 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMTAB
, 0)
5242 || !_bfd_elf_add_dynamic_entry (info
, DT_STRSZ
, strsize
)
5243 || !_bfd_elf_add_dynamic_entry (info
, DT_SYMENT
,
5244 bed
->s
->sizeof_sym
))
5249 /* The backend must work out the sizes of all the other dynamic
5251 if (bed
->elf_backend_size_dynamic_sections
5252 && ! (*bed
->elf_backend_size_dynamic_sections
) (output_bfd
, info
))
5255 if (elf_hash_table (info
)->dynamic_sections_created
)
5257 bfd_size_type dynsymcount
;
5259 size_t bucketcount
= 0;
5260 size_t hash_entry_size
;
5261 unsigned int dtagcount
;
5263 /* Set up the version definition section. */
5264 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_d");
5265 BFD_ASSERT (s
!= NULL
);
5267 /* We may have created additional version definitions if we are
5268 just linking a regular application. */
5269 verdefs
= asvinfo
.verdefs
;
5271 /* Skip anonymous version tag. */
5272 if (verdefs
!= NULL
&& verdefs
->vernum
== 0)
5273 verdefs
= verdefs
->next
;
5275 if (verdefs
== NULL
&& !info
->create_default_symver
)
5276 _bfd_strip_section_from_output (info
, s
);
5281 struct bfd_elf_version_tree
*t
;
5283 Elf_Internal_Verdef def
;
5284 Elf_Internal_Verdaux defaux
;
5285 struct bfd_link_hash_entry
*bh
;
5286 struct elf_link_hash_entry
*h
;
5292 /* Make space for the base version. */
5293 size
+= sizeof (Elf_External_Verdef
);
5294 size
+= sizeof (Elf_External_Verdaux
);
5297 /* Make space for the default version. */
5298 if (info
->create_default_symver
)
5300 size
+= sizeof (Elf_External_Verdef
);
5304 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5306 struct bfd_elf_version_deps
*n
;
5308 size
+= sizeof (Elf_External_Verdef
);
5309 size
+= sizeof (Elf_External_Verdaux
);
5312 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5313 size
+= sizeof (Elf_External_Verdaux
);
5317 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5318 if (s
->contents
== NULL
&& s
->size
!= 0)
5321 /* Fill in the version definition section. */
5325 def
.vd_version
= VER_DEF_CURRENT
;
5326 def
.vd_flags
= VER_FLG_BASE
;
5329 if (info
->create_default_symver
)
5331 def
.vd_aux
= 2 * sizeof (Elf_External_Verdef
);
5332 def
.vd_next
= sizeof (Elf_External_Verdef
);
5336 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5337 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5338 + sizeof (Elf_External_Verdaux
));
5341 if (soname_indx
!= (bfd_size_type
) -1)
5343 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5345 def
.vd_hash
= bfd_elf_hash (soname
);
5346 defaux
.vda_name
= soname_indx
;
5353 name
= basename (output_bfd
->filename
);
5354 def
.vd_hash
= bfd_elf_hash (name
);
5355 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5357 if (indx
== (bfd_size_type
) -1)
5359 defaux
.vda_name
= indx
;
5361 defaux
.vda_next
= 0;
5363 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5364 (Elf_External_Verdef
*) p
);
5365 p
+= sizeof (Elf_External_Verdef
);
5366 if (info
->create_default_symver
)
5368 /* Add a symbol representing this version. */
5370 if (! (_bfd_generic_link_add_one_symbol
5371 (info
, dynobj
, name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5373 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5375 h
= (struct elf_link_hash_entry
*) bh
;
5378 h
->type
= STT_OBJECT
;
5379 h
->verinfo
.vertree
= NULL
;
5381 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5384 /* Create a duplicate of the base version with the same
5385 aux block, but different flags. */
5388 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5390 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5391 + sizeof (Elf_External_Verdaux
));
5394 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5395 (Elf_External_Verdef
*) p
);
5396 p
+= sizeof (Elf_External_Verdef
);
5398 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5399 (Elf_External_Verdaux
*) p
);
5400 p
+= sizeof (Elf_External_Verdaux
);
5402 for (t
= verdefs
; t
!= NULL
; t
= t
->next
)
5405 struct bfd_elf_version_deps
*n
;
5408 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5411 /* Add a symbol representing this version. */
5413 if (! (_bfd_generic_link_add_one_symbol
5414 (info
, dynobj
, t
->name
, BSF_GLOBAL
, bfd_abs_section_ptr
,
5416 get_elf_backend_data (dynobj
)->collect
, &bh
)))
5418 h
= (struct elf_link_hash_entry
*) bh
;
5421 h
->type
= STT_OBJECT
;
5422 h
->verinfo
.vertree
= t
;
5424 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
5427 def
.vd_version
= VER_DEF_CURRENT
;
5429 if (t
->globals
.list
== NULL
5430 && t
->locals
.list
== NULL
5432 def
.vd_flags
|= VER_FLG_WEAK
;
5433 def
.vd_ndx
= t
->vernum
+ (info
->create_default_symver
? 2 : 1);
5434 def
.vd_cnt
= cdeps
+ 1;
5435 def
.vd_hash
= bfd_elf_hash (t
->name
);
5436 def
.vd_aux
= sizeof (Elf_External_Verdef
);
5438 if (t
->next
!= NULL
)
5439 def
.vd_next
= (sizeof (Elf_External_Verdef
)
5440 + (cdeps
+ 1) * sizeof (Elf_External_Verdaux
));
5442 _bfd_elf_swap_verdef_out (output_bfd
, &def
,
5443 (Elf_External_Verdef
*) p
);
5444 p
+= sizeof (Elf_External_Verdef
);
5446 defaux
.vda_name
= h
->dynstr_index
;
5447 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5449 defaux
.vda_next
= 0;
5450 if (t
->deps
!= NULL
)
5451 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5452 t
->name_indx
= defaux
.vda_name
;
5454 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5455 (Elf_External_Verdaux
*) p
);
5456 p
+= sizeof (Elf_External_Verdaux
);
5458 for (n
= t
->deps
; n
!= NULL
; n
= n
->next
)
5460 if (n
->version_needed
== NULL
)
5462 /* This can happen if there was an error in the
5464 defaux
.vda_name
= 0;
5468 defaux
.vda_name
= n
->version_needed
->name_indx
;
5469 _bfd_elf_strtab_addref (elf_hash_table (info
)->dynstr
,
5472 if (n
->next
== NULL
)
5473 defaux
.vda_next
= 0;
5475 defaux
.vda_next
= sizeof (Elf_External_Verdaux
);
5477 _bfd_elf_swap_verdaux_out (output_bfd
, &defaux
,
5478 (Elf_External_Verdaux
*) p
);
5479 p
+= sizeof (Elf_External_Verdaux
);
5483 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERDEF
, 0)
5484 || !_bfd_elf_add_dynamic_entry (info
, DT_VERDEFNUM
, cdefs
))
5487 elf_tdata (output_bfd
)->cverdefs
= cdefs
;
5490 if ((info
->new_dtags
&& info
->flags
) || (info
->flags
& DF_STATIC_TLS
))
5492 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS
, info
->flags
))
5495 else if (info
->flags
& DF_BIND_NOW
)
5497 if (!_bfd_elf_add_dynamic_entry (info
, DT_BIND_NOW
, 0))
5503 if (info
->executable
)
5504 info
->flags_1
&= ~ (DF_1_INITFIRST
5507 if (!_bfd_elf_add_dynamic_entry (info
, DT_FLAGS_1
, info
->flags_1
))
5511 /* Work out the size of the version reference section. */
5513 s
= bfd_get_section_by_name (dynobj
, ".gnu.version_r");
5514 BFD_ASSERT (s
!= NULL
);
5516 struct elf_find_verdep_info sinfo
;
5518 sinfo
.output_bfd
= output_bfd
;
5520 sinfo
.vers
= elf_tdata (output_bfd
)->cverdefs
;
5521 if (sinfo
.vers
== 0)
5523 sinfo
.failed
= FALSE
;
5525 elf_link_hash_traverse (elf_hash_table (info
),
5526 _bfd_elf_link_find_version_dependencies
,
5529 if (elf_tdata (output_bfd
)->verref
== NULL
)
5530 _bfd_strip_section_from_output (info
, s
);
5533 Elf_Internal_Verneed
*t
;
5538 /* Build the version definition section. */
5541 for (t
= elf_tdata (output_bfd
)->verref
;
5545 Elf_Internal_Vernaux
*a
;
5547 size
+= sizeof (Elf_External_Verneed
);
5549 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5550 size
+= sizeof (Elf_External_Vernaux
);
5554 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5555 if (s
->contents
== NULL
)
5559 for (t
= elf_tdata (output_bfd
)->verref
;
5564 Elf_Internal_Vernaux
*a
;
5568 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5571 t
->vn_version
= VER_NEED_CURRENT
;
5573 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5574 elf_dt_name (t
->vn_bfd
) != NULL
5575 ? elf_dt_name (t
->vn_bfd
)
5576 : basename (t
->vn_bfd
->filename
),
5578 if (indx
== (bfd_size_type
) -1)
5581 t
->vn_aux
= sizeof (Elf_External_Verneed
);
5582 if (t
->vn_nextref
== NULL
)
5585 t
->vn_next
= (sizeof (Elf_External_Verneed
)
5586 + caux
* sizeof (Elf_External_Vernaux
));
5588 _bfd_elf_swap_verneed_out (output_bfd
, t
,
5589 (Elf_External_Verneed
*) p
);
5590 p
+= sizeof (Elf_External_Verneed
);
5592 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
5594 a
->vna_hash
= bfd_elf_hash (a
->vna_nodename
);
5595 indx
= _bfd_elf_strtab_add (elf_hash_table (info
)->dynstr
,
5596 a
->vna_nodename
, FALSE
);
5597 if (indx
== (bfd_size_type
) -1)
5600 if (a
->vna_nextptr
== NULL
)
5603 a
->vna_next
= sizeof (Elf_External_Vernaux
);
5605 _bfd_elf_swap_vernaux_out (output_bfd
, a
,
5606 (Elf_External_Vernaux
*) p
);
5607 p
+= sizeof (Elf_External_Vernaux
);
5611 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERNEED
, 0)
5612 || !_bfd_elf_add_dynamic_entry (info
, DT_VERNEEDNUM
, crefs
))
5615 elf_tdata (output_bfd
)->cverrefs
= crefs
;
5619 /* Assign dynsym indicies. In a shared library we generate a
5620 section symbol for each output section, which come first.
5621 Next come all of the back-end allocated local dynamic syms,
5622 followed by the rest of the global symbols. */
5624 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5626 /* Work out the size of the symbol version section. */
5627 s
= bfd_get_section_by_name (dynobj
, ".gnu.version");
5628 BFD_ASSERT (s
!= NULL
);
5629 if (dynsymcount
== 0
5630 || (verdefs
== NULL
&& elf_tdata (output_bfd
)->verref
== NULL
5631 && !info
->create_default_symver
))
5633 _bfd_strip_section_from_output (info
, s
);
5634 /* The DYNSYMCOUNT might have changed if we were going to
5635 output a dynamic symbol table entry for S. */
5636 dynsymcount
= _bfd_elf_link_renumber_dynsyms (output_bfd
, info
);
5640 s
->size
= dynsymcount
* sizeof (Elf_External_Versym
);
5641 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5642 if (s
->contents
== NULL
)
5645 if (!_bfd_elf_add_dynamic_entry (info
, DT_VERSYM
, 0))
5649 /* Set the size of the .dynsym and .hash sections. We counted
5650 the number of dynamic symbols in elf_link_add_object_symbols.
5651 We will build the contents of .dynsym and .hash when we build
5652 the final symbol table, because until then we do not know the
5653 correct value to give the symbols. We built the .dynstr
5654 section as we went along in elf_link_add_object_symbols. */
5655 s
= bfd_get_section_by_name (dynobj
, ".dynsym");
5656 BFD_ASSERT (s
!= NULL
);
5657 s
->size
= dynsymcount
* bed
->s
->sizeof_sym
;
5658 s
->contents
= bfd_alloc (output_bfd
, s
->size
);
5659 if (s
->contents
== NULL
&& s
->size
!= 0)
5662 if (dynsymcount
!= 0)
5664 Elf_Internal_Sym isym
;
5666 /* The first entry in .dynsym is a dummy symbol. */
5673 bed
->s
->swap_symbol_out (output_bfd
, &isym
, s
->contents
, 0);
5676 /* Compute the size of the hashing table. As a side effect this
5677 computes the hash values for all the names we export. */
5678 bucketcount
= compute_bucket_count (info
);
5680 s
= bfd_get_section_by_name (dynobj
, ".hash");
5681 BFD_ASSERT (s
!= NULL
);
5682 hash_entry_size
= elf_section_data (s
)->this_hdr
.sh_entsize
;
5683 s
->size
= ((2 + bucketcount
+ dynsymcount
) * hash_entry_size
);
5684 s
->contents
= bfd_zalloc (output_bfd
, s
->size
);
5685 if (s
->contents
== NULL
)
5688 bfd_put (8 * hash_entry_size
, output_bfd
, bucketcount
, s
->contents
);
5689 bfd_put (8 * hash_entry_size
, output_bfd
, dynsymcount
,
5690 s
->contents
+ hash_entry_size
);
5692 elf_hash_table (info
)->bucketcount
= bucketcount
;
5694 s
= bfd_get_section_by_name (dynobj
, ".dynstr");
5695 BFD_ASSERT (s
!= NULL
);
5697 elf_finalize_dynstr (output_bfd
, info
);
5699 s
->size
= _bfd_elf_strtab_size (elf_hash_table (info
)->dynstr
);
5701 for (dtagcount
= 0; dtagcount
<= info
->spare_dynamic_tags
; ++dtagcount
)
5702 if (!_bfd_elf_add_dynamic_entry (info
, DT_NULL
, 0))
5709 /* Final phase of ELF linker. */
5711 /* A structure we use to avoid passing large numbers of arguments. */
5713 struct elf_final_link_info
5715 /* General link information. */
5716 struct bfd_link_info
*info
;
5719 /* Symbol string table. */
5720 struct bfd_strtab_hash
*symstrtab
;
5721 /* .dynsym section. */
5722 asection
*dynsym_sec
;
5723 /* .hash section. */
5725 /* symbol version section (.gnu.version). */
5726 asection
*symver_sec
;
5727 /* Buffer large enough to hold contents of any section. */
5729 /* Buffer large enough to hold external relocs of any section. */
5730 void *external_relocs
;
5731 /* Buffer large enough to hold internal relocs of any section. */
5732 Elf_Internal_Rela
*internal_relocs
;
5733 /* Buffer large enough to hold external local symbols of any input
5735 bfd_byte
*external_syms
;
5736 /* And a buffer for symbol section indices. */
5737 Elf_External_Sym_Shndx
*locsym_shndx
;
5738 /* Buffer large enough to hold internal local symbols of any input
5740 Elf_Internal_Sym
*internal_syms
;
5741 /* Array large enough to hold a symbol index for each local symbol
5742 of any input BFD. */
5744 /* Array large enough to hold a section pointer for each local
5745 symbol of any input BFD. */
5746 asection
**sections
;
5747 /* Buffer to hold swapped out symbols. */
5749 /* And one for symbol section indices. */
5750 Elf_External_Sym_Shndx
*symshndxbuf
;
5751 /* Number of swapped out symbols in buffer. */
5752 size_t symbuf_count
;
5753 /* Number of symbols which fit in symbuf. */
5755 /* And same for symshndxbuf. */
5756 size_t shndxbuf_size
;
5759 /* This struct is used to pass information to elf_link_output_extsym. */
5761 struct elf_outext_info
5764 bfd_boolean localsyms
;
5765 struct elf_final_link_info
*finfo
;
5768 /* When performing a relocatable link, the input relocations are
5769 preserved. But, if they reference global symbols, the indices
5770 referenced must be updated. Update all the relocations in
5771 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5774 elf_link_adjust_relocs (bfd
*abfd
,
5775 Elf_Internal_Shdr
*rel_hdr
,
5777 struct elf_link_hash_entry
**rel_hash
)
5780 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5782 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5783 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5784 bfd_vma r_type_mask
;
5787 if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rel
)
5789 swap_in
= bed
->s
->swap_reloc_in
;
5790 swap_out
= bed
->s
->swap_reloc_out
;
5792 else if (rel_hdr
->sh_entsize
== bed
->s
->sizeof_rela
)
5794 swap_in
= bed
->s
->swap_reloca_in
;
5795 swap_out
= bed
->s
->swap_reloca_out
;
5800 if (bed
->s
->int_rels_per_ext_rel
> MAX_INT_RELS_PER_EXT_REL
)
5803 if (bed
->s
->arch_size
== 32)
5810 r_type_mask
= 0xffffffff;
5814 erela
= rel_hdr
->contents
;
5815 for (i
= 0; i
< count
; i
++, rel_hash
++, erela
+= rel_hdr
->sh_entsize
)
5817 Elf_Internal_Rela irela
[MAX_INT_RELS_PER_EXT_REL
];
5820 if (*rel_hash
== NULL
)
5823 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
5825 (*swap_in
) (abfd
, erela
, irela
);
5826 for (j
= 0; j
< bed
->s
->int_rels_per_ext_rel
; j
++)
5827 irela
[j
].r_info
= ((bfd_vma
) (*rel_hash
)->indx
<< r_sym_shift
5828 | (irela
[j
].r_info
& r_type_mask
));
5829 (*swap_out
) (abfd
, irela
, erela
);
5833 struct elf_link_sort_rela
5839 enum elf_reloc_type_class type
;
5840 /* We use this as an array of size int_rels_per_ext_rel. */
5841 Elf_Internal_Rela rela
[1];
5845 elf_link_sort_cmp1 (const void *A
, const void *B
)
5847 const struct elf_link_sort_rela
*a
= A
;
5848 const struct elf_link_sort_rela
*b
= B
;
5849 int relativea
, relativeb
;
5851 relativea
= a
->type
== reloc_class_relative
;
5852 relativeb
= b
->type
== reloc_class_relative
;
5854 if (relativea
< relativeb
)
5856 if (relativea
> relativeb
)
5858 if ((a
->rela
->r_info
& a
->u
.sym_mask
) < (b
->rela
->r_info
& b
->u
.sym_mask
))
5860 if ((a
->rela
->r_info
& a
->u
.sym_mask
) > (b
->rela
->r_info
& b
->u
.sym_mask
))
5862 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5864 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5870 elf_link_sort_cmp2 (const void *A
, const void *B
)
5872 const struct elf_link_sort_rela
*a
= A
;
5873 const struct elf_link_sort_rela
*b
= B
;
5876 if (a
->u
.offset
< b
->u
.offset
)
5878 if (a
->u
.offset
> b
->u
.offset
)
5880 copya
= (a
->type
== reloc_class_copy
) * 2 + (a
->type
== reloc_class_plt
);
5881 copyb
= (b
->type
== reloc_class_copy
) * 2 + (b
->type
== reloc_class_plt
);
5886 if (a
->rela
->r_offset
< b
->rela
->r_offset
)
5888 if (a
->rela
->r_offset
> b
->rela
->r_offset
)
5894 elf_link_sort_relocs (bfd
*abfd
, struct bfd_link_info
*info
, asection
**psec
)
5897 bfd_size_type count
, size
;
5898 size_t i
, ret
, sort_elt
, ext_size
;
5899 bfd_byte
*sort
, *s_non_relative
, *p
;
5900 struct elf_link_sort_rela
*sq
;
5901 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5902 int i2e
= bed
->s
->int_rels_per_ext_rel
;
5903 void (*swap_in
) (bfd
*, const bfd_byte
*, Elf_Internal_Rela
*);
5904 void (*swap_out
) (bfd
*, const Elf_Internal_Rela
*, bfd_byte
*);
5905 struct bfd_link_order
*lo
;
5908 reldyn
= bfd_get_section_by_name (abfd
, ".rela.dyn");
5909 if (reldyn
== NULL
|| reldyn
->size
== 0)
5911 reldyn
= bfd_get_section_by_name (abfd
, ".rel.dyn");
5912 if (reldyn
== NULL
|| reldyn
->size
== 0)
5914 ext_size
= bed
->s
->sizeof_rel
;
5915 swap_in
= bed
->s
->swap_reloc_in
;
5916 swap_out
= bed
->s
->swap_reloc_out
;
5920 ext_size
= bed
->s
->sizeof_rela
;
5921 swap_in
= bed
->s
->swap_reloca_in
;
5922 swap_out
= bed
->s
->swap_reloca_out
;
5924 count
= reldyn
->size
/ ext_size
;
5927 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5928 if (lo
->type
== bfd_indirect_link_order
)
5930 asection
*o
= lo
->u
.indirect
.section
;
5934 if (size
!= reldyn
->size
)
5937 sort_elt
= (sizeof (struct elf_link_sort_rela
)
5938 + (i2e
- 1) * sizeof (Elf_Internal_Rela
));
5939 sort
= bfd_zmalloc (sort_elt
* count
);
5942 (*info
->callbacks
->warning
)
5943 (info
, _("Not enough memory to sort relocations"), 0, abfd
, 0, 0);
5947 if (bed
->s
->arch_size
== 32)
5948 r_sym_mask
= ~(bfd_vma
) 0xff;
5950 r_sym_mask
= ~(bfd_vma
) 0xffffffff;
5952 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
5953 if (lo
->type
== bfd_indirect_link_order
)
5955 bfd_byte
*erel
, *erelend
;
5956 asection
*o
= lo
->u
.indirect
.section
;
5958 if (o
->contents
== NULL
&& o
->size
!= 0)
5960 /* This is a reloc section that is being handled as a normal
5961 section. See bfd_section_from_shdr. We can't combine
5962 relocs in this case. */
5967 erelend
= o
->contents
+ o
->size
;
5968 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
5969 while (erel
< erelend
)
5971 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5972 (*swap_in
) (abfd
, erel
, s
->rela
);
5973 s
->type
= (*bed
->elf_backend_reloc_type_class
) (s
->rela
);
5974 s
->u
.sym_mask
= r_sym_mask
;
5980 qsort (sort
, count
, sort_elt
, elf_link_sort_cmp1
);
5982 for (i
= 0, p
= sort
; i
< count
; i
++, p
+= sort_elt
)
5984 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
5985 if (s
->type
!= reloc_class_relative
)
5991 sq
= (struct elf_link_sort_rela
*) s_non_relative
;
5992 for (; i
< count
; i
++, p
+= sort_elt
)
5994 struct elf_link_sort_rela
*sp
= (struct elf_link_sort_rela
*) p
;
5995 if (((sp
->rela
->r_info
^ sq
->rela
->r_info
) & r_sym_mask
) != 0)
5997 sp
->u
.offset
= sq
->rela
->r_offset
;
6000 qsort (s_non_relative
, count
- ret
, sort_elt
, elf_link_sort_cmp2
);
6002 for (lo
= reldyn
->link_order_head
; lo
!= NULL
; lo
= lo
->next
)
6003 if (lo
->type
== bfd_indirect_link_order
)
6005 bfd_byte
*erel
, *erelend
;
6006 asection
*o
= lo
->u
.indirect
.section
;
6009 erelend
= o
->contents
+ o
->size
;
6010 p
= sort
+ o
->output_offset
/ ext_size
* sort_elt
;
6011 while (erel
< erelend
)
6013 struct elf_link_sort_rela
*s
= (struct elf_link_sort_rela
*) p
;
6014 (*swap_out
) (abfd
, s
->rela
, erel
);
6025 /* Flush the output symbols to the file. */
6028 elf_link_flush_output_syms (struct elf_final_link_info
*finfo
,
6029 const struct elf_backend_data
*bed
)
6031 if (finfo
->symbuf_count
> 0)
6033 Elf_Internal_Shdr
*hdr
;
6037 hdr
= &elf_tdata (finfo
->output_bfd
)->symtab_hdr
;
6038 pos
= hdr
->sh_offset
+ hdr
->sh_size
;
6039 amt
= finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6040 if (bfd_seek (finfo
->output_bfd
, pos
, SEEK_SET
) != 0
6041 || bfd_bwrite (finfo
->symbuf
, amt
, finfo
->output_bfd
) != amt
)
6044 hdr
->sh_size
+= amt
;
6045 finfo
->symbuf_count
= 0;
6051 /* Add a symbol to the output symbol table. */
6054 elf_link_output_sym (struct elf_final_link_info
*finfo
,
6056 Elf_Internal_Sym
*elfsym
,
6057 asection
*input_sec
,
6058 struct elf_link_hash_entry
*h
)
6061 Elf_External_Sym_Shndx
*destshndx
;
6062 bfd_boolean (*output_symbol_hook
)
6063 (struct bfd_link_info
*, const char *, Elf_Internal_Sym
*, asection
*,
6064 struct elf_link_hash_entry
*);
6065 const struct elf_backend_data
*bed
;
6067 bed
= get_elf_backend_data (finfo
->output_bfd
);
6068 output_symbol_hook
= bed
->elf_backend_link_output_symbol_hook
;
6069 if (output_symbol_hook
!= NULL
)
6071 if (! (*output_symbol_hook
) (finfo
->info
, name
, elfsym
, input_sec
, h
))
6075 if (name
== NULL
|| *name
== '\0')
6076 elfsym
->st_name
= 0;
6077 else if (input_sec
->flags
& SEC_EXCLUDE
)
6078 elfsym
->st_name
= 0;
6081 elfsym
->st_name
= (unsigned long) _bfd_stringtab_add (finfo
->symstrtab
,
6083 if (elfsym
->st_name
== (unsigned long) -1)
6087 if (finfo
->symbuf_count
>= finfo
->symbuf_size
)
6089 if (! elf_link_flush_output_syms (finfo
, bed
))
6093 dest
= finfo
->symbuf
+ finfo
->symbuf_count
* bed
->s
->sizeof_sym
;
6094 destshndx
= finfo
->symshndxbuf
;
6095 if (destshndx
!= NULL
)
6097 if (bfd_get_symcount (finfo
->output_bfd
) >= finfo
->shndxbuf_size
)
6101 amt
= finfo
->shndxbuf_size
* sizeof (Elf_External_Sym_Shndx
);
6102 finfo
->symshndxbuf
= destshndx
= bfd_realloc (destshndx
, amt
* 2);
6103 if (destshndx
== NULL
)
6105 memset ((char *) destshndx
+ amt
, 0, amt
);
6106 finfo
->shndxbuf_size
*= 2;
6108 destshndx
+= bfd_get_symcount (finfo
->output_bfd
);
6111 bed
->s
->swap_symbol_out (finfo
->output_bfd
, elfsym
, dest
, destshndx
);
6112 finfo
->symbuf_count
+= 1;
6113 bfd_get_symcount (finfo
->output_bfd
) += 1;
6118 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6119 allowing an unsatisfied unversioned symbol in the DSO to match a
6120 versioned symbol that would normally require an explicit version.
6121 We also handle the case that a DSO references a hidden symbol
6122 which may be satisfied by a versioned symbol in another DSO. */
6125 elf_link_check_versioned_symbol (struct bfd_link_info
*info
,
6126 const struct elf_backend_data
*bed
,
6127 struct elf_link_hash_entry
*h
)
6130 struct elf_link_loaded_list
*loaded
;
6132 if (!is_elf_hash_table (info
->hash
))
6135 switch (h
->root
.type
)
6141 case bfd_link_hash_undefined
:
6142 case bfd_link_hash_undefweak
:
6143 abfd
= h
->root
.u
.undef
.abfd
;
6144 if ((abfd
->flags
& DYNAMIC
) == 0
6145 || (elf_dyn_lib_class (abfd
) & DYN_DT_NEEDED
) == 0)
6149 case bfd_link_hash_defined
:
6150 case bfd_link_hash_defweak
:
6151 abfd
= h
->root
.u
.def
.section
->owner
;
6154 case bfd_link_hash_common
:
6155 abfd
= h
->root
.u
.c
.p
->section
->owner
;
6158 BFD_ASSERT (abfd
!= NULL
);
6160 for (loaded
= elf_hash_table (info
)->loaded
;
6162 loaded
= loaded
->next
)
6165 Elf_Internal_Shdr
*hdr
;
6166 bfd_size_type symcount
;
6167 bfd_size_type extsymcount
;
6168 bfd_size_type extsymoff
;
6169 Elf_Internal_Shdr
*versymhdr
;
6170 Elf_Internal_Sym
*isym
;
6171 Elf_Internal_Sym
*isymend
;
6172 Elf_Internal_Sym
*isymbuf
;
6173 Elf_External_Versym
*ever
;
6174 Elf_External_Versym
*extversym
;
6176 input
= loaded
->abfd
;
6178 /* We check each DSO for a possible hidden versioned definition. */
6180 || (input
->flags
& DYNAMIC
) == 0
6181 || elf_dynversym (input
) == 0)
6184 hdr
= &elf_tdata (input
)->dynsymtab_hdr
;
6186 symcount
= hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6187 if (elf_bad_symtab (input
))
6189 extsymcount
= symcount
;
6194 extsymcount
= symcount
- hdr
->sh_info
;
6195 extsymoff
= hdr
->sh_info
;
6198 if (extsymcount
== 0)
6201 isymbuf
= bfd_elf_get_elf_syms (input
, hdr
, extsymcount
, extsymoff
,
6203 if (isymbuf
== NULL
)
6206 /* Read in any version definitions. */
6207 versymhdr
= &elf_tdata (input
)->dynversym_hdr
;
6208 extversym
= bfd_malloc (versymhdr
->sh_size
);
6209 if (extversym
== NULL
)
6212 if (bfd_seek (input
, versymhdr
->sh_offset
, SEEK_SET
) != 0
6213 || (bfd_bread (extversym
, versymhdr
->sh_size
, input
)
6214 != versymhdr
->sh_size
))
6222 ever
= extversym
+ extsymoff
;
6223 isymend
= isymbuf
+ extsymcount
;
6224 for (isym
= isymbuf
; isym
< isymend
; isym
++, ever
++)
6227 Elf_Internal_Versym iver
;
6228 unsigned short version_index
;
6230 if (ELF_ST_BIND (isym
->st_info
) == STB_LOCAL
6231 || isym
->st_shndx
== SHN_UNDEF
)
6234 name
= bfd_elf_string_from_elf_section (input
,
6237 if (strcmp (name
, h
->root
.root
.string
) != 0)
6240 _bfd_elf_swap_versym_in (input
, ever
, &iver
);
6242 if ((iver
.vs_vers
& VERSYM_HIDDEN
) == 0)
6244 /* If we have a non-hidden versioned sym, then it should
6245 have provided a definition for the undefined sym. */
6249 version_index
= iver
.vs_vers
& VERSYM_VERSION
;
6250 if (version_index
== 1 || version_index
== 2)
6252 /* This is the base or first version. We can use it. */
6266 /* Add an external symbol to the symbol table. This is called from
6267 the hash table traversal routine. When generating a shared object,
6268 we go through the symbol table twice. The first time we output
6269 anything that might have been forced to local scope in a version
6270 script. The second time we output the symbols that are still
6274 elf_link_output_extsym (struct elf_link_hash_entry
*h
, void *data
)
6276 struct elf_outext_info
*eoinfo
= data
;
6277 struct elf_final_link_info
*finfo
= eoinfo
->finfo
;
6279 Elf_Internal_Sym sym
;
6280 asection
*input_sec
;
6281 const struct elf_backend_data
*bed
;
6283 if (h
->root
.type
== bfd_link_hash_warning
)
6285 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6286 if (h
->root
.type
== bfd_link_hash_new
)
6290 /* Decide whether to output this symbol in this pass. */
6291 if (eoinfo
->localsyms
)
6293 if (!h
->forced_local
)
6298 if (h
->forced_local
)
6302 bed
= get_elf_backend_data (finfo
->output_bfd
);
6304 /* If we have an undefined symbol reference here then it must have
6305 come from a shared library that is being linked in. (Undefined
6306 references in regular files have already been handled). If we
6307 are reporting errors for this situation then do so now. */
6308 if (h
->root
.type
== bfd_link_hash_undefined
6311 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
)
6312 && finfo
->info
->unresolved_syms_in_shared_libs
!= RM_IGNORE
)
6314 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
6315 (finfo
->info
, h
->root
.root
.string
, h
->root
.u
.undef
.abfd
,
6316 NULL
, 0, finfo
->info
->unresolved_syms_in_shared_libs
== RM_GENERATE_ERROR
)))
6318 eoinfo
->failed
= TRUE
;
6323 /* We should also warn if a forced local symbol is referenced from
6324 shared libraries. */
6325 if (! finfo
->info
->relocatable
6326 && (! finfo
->info
->shared
)
6331 && ! elf_link_check_versioned_symbol (finfo
->info
, bed
, h
))
6333 (*_bfd_error_handler
)
6334 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6335 finfo
->output_bfd
, h
->root
.u
.def
.section
->owner
,
6336 ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
6338 : ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
6339 ? "hidden" : "local",
6340 h
->root
.root
.string
);
6341 eoinfo
->failed
= TRUE
;
6345 /* We don't want to output symbols that have never been mentioned by
6346 a regular file, or that we have been told to strip. However, if
6347 h->indx is set to -2, the symbol is used by a reloc and we must
6351 else if ((h
->def_dynamic
6353 || h
->root
.type
== bfd_link_hash_new
)
6357 else if (finfo
->info
->strip
== strip_all
)
6359 else if (finfo
->info
->strip
== strip_some
6360 && bfd_hash_lookup (finfo
->info
->keep_hash
,
6361 h
->root
.root
.string
, FALSE
, FALSE
) == NULL
)
6363 else if (finfo
->info
->strip_discarded
6364 && (h
->root
.type
== bfd_link_hash_defined
6365 || h
->root
.type
== bfd_link_hash_defweak
)
6366 && elf_discarded_section (h
->root
.u
.def
.section
))
6371 /* If we're stripping it, and it's not a dynamic symbol, there's
6372 nothing else to do unless it is a forced local symbol. */
6375 && !h
->forced_local
)
6379 sym
.st_size
= h
->size
;
6380 sym
.st_other
= h
->other
;
6381 if (h
->forced_local
)
6382 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, h
->type
);
6383 else if (h
->root
.type
== bfd_link_hash_undefweak
6384 || h
->root
.type
== bfd_link_hash_defweak
)
6385 sym
.st_info
= ELF_ST_INFO (STB_WEAK
, h
->type
);
6387 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, h
->type
);
6389 switch (h
->root
.type
)
6392 case bfd_link_hash_new
:
6393 case bfd_link_hash_warning
:
6397 case bfd_link_hash_undefined
:
6398 case bfd_link_hash_undefweak
:
6399 input_sec
= bfd_und_section_ptr
;
6400 sym
.st_shndx
= SHN_UNDEF
;
6403 case bfd_link_hash_defined
:
6404 case bfd_link_hash_defweak
:
6406 input_sec
= h
->root
.u
.def
.section
;
6407 if (input_sec
->output_section
!= NULL
)
6410 _bfd_elf_section_from_bfd_section (finfo
->output_bfd
,
6411 input_sec
->output_section
);
6412 if (sym
.st_shndx
== SHN_BAD
)
6414 (*_bfd_error_handler
)
6415 (_("%B: could not find output section %A for input section %A"),
6416 finfo
->output_bfd
, input_sec
->output_section
, input_sec
);
6417 eoinfo
->failed
= TRUE
;
6421 /* ELF symbols in relocatable files are section relative,
6422 but in nonrelocatable files they are virtual
6424 sym
.st_value
= h
->root
.u
.def
.value
+ input_sec
->output_offset
;
6425 if (! finfo
->info
->relocatable
)
6427 sym
.st_value
+= input_sec
->output_section
->vma
;
6428 if (h
->type
== STT_TLS
)
6430 /* STT_TLS symbols are relative to PT_TLS segment
6432 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6433 sym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6439 BFD_ASSERT (input_sec
->owner
== NULL
6440 || (input_sec
->owner
->flags
& DYNAMIC
) != 0);
6441 sym
.st_shndx
= SHN_UNDEF
;
6442 input_sec
= bfd_und_section_ptr
;
6447 case bfd_link_hash_common
:
6448 input_sec
= h
->root
.u
.c
.p
->section
;
6449 sym
.st_shndx
= SHN_COMMON
;
6450 sym
.st_value
= 1 << h
->root
.u
.c
.p
->alignment_power
;
6453 case bfd_link_hash_indirect
:
6454 /* These symbols are created by symbol versioning. They point
6455 to the decorated version of the name. For example, if the
6456 symbol foo@@GNU_1.2 is the default, which should be used when
6457 foo is used with no version, then we add an indirect symbol
6458 foo which points to foo@@GNU_1.2. We ignore these symbols,
6459 since the indirected symbol is already in the hash table. */
6463 /* Give the processor backend a chance to tweak the symbol value,
6464 and also to finish up anything that needs to be done for this
6465 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6466 forced local syms when non-shared is due to a historical quirk. */
6467 if ((h
->dynindx
!= -1
6469 && ((finfo
->info
->shared
6470 && (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
6471 || h
->root
.type
!= bfd_link_hash_undefweak
))
6472 || !h
->forced_local
)
6473 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6475 if (! ((*bed
->elf_backend_finish_dynamic_symbol
)
6476 (finfo
->output_bfd
, finfo
->info
, h
, &sym
)))
6478 eoinfo
->failed
= TRUE
;
6483 /* If we are marking the symbol as undefined, and there are no
6484 non-weak references to this symbol from a regular object, then
6485 mark the symbol as weak undefined; if there are non-weak
6486 references, mark the symbol as strong. We can't do this earlier,
6487 because it might not be marked as undefined until the
6488 finish_dynamic_symbol routine gets through with it. */
6489 if (sym
.st_shndx
== SHN_UNDEF
6491 && (ELF_ST_BIND (sym
.st_info
) == STB_GLOBAL
6492 || ELF_ST_BIND (sym
.st_info
) == STB_WEAK
))
6496 if (h
->ref_regular_nonweak
)
6497 bindtype
= STB_GLOBAL
;
6499 bindtype
= STB_WEAK
;
6500 sym
.st_info
= ELF_ST_INFO (bindtype
, ELF_ST_TYPE (sym
.st_info
));
6503 /* If a non-weak symbol with non-default visibility is not defined
6504 locally, it is a fatal error. */
6505 if (! finfo
->info
->relocatable
6506 && ELF_ST_VISIBILITY (sym
.st_other
) != STV_DEFAULT
6507 && ELF_ST_BIND (sym
.st_info
) != STB_WEAK
6508 && h
->root
.type
== bfd_link_hash_undefined
6511 (*_bfd_error_handler
)
6512 (_("%B: %s symbol `%s' isn't defined"),
6514 ELF_ST_VISIBILITY (sym
.st_other
) == STV_PROTECTED
6516 : ELF_ST_VISIBILITY (sym
.st_other
) == STV_INTERNAL
6517 ? "internal" : "hidden",
6518 h
->root
.root
.string
);
6519 eoinfo
->failed
= TRUE
;
6523 /* If this symbol should be put in the .dynsym section, then put it
6524 there now. We already know the symbol index. We also fill in
6525 the entry in the .hash section. */
6526 if (h
->dynindx
!= -1
6527 && elf_hash_table (finfo
->info
)->dynamic_sections_created
)
6531 size_t hash_entry_size
;
6532 bfd_byte
*bucketpos
;
6536 sym
.st_name
= h
->dynstr_index
;
6537 esym
= finfo
->dynsym_sec
->contents
+ h
->dynindx
* bed
->s
->sizeof_sym
;
6538 bed
->s
->swap_symbol_out (finfo
->output_bfd
, &sym
, esym
, 0);
6540 bucketcount
= elf_hash_table (finfo
->info
)->bucketcount
;
6541 bucket
= h
->u
.elf_hash_value
% bucketcount
;
6543 = elf_section_data (finfo
->hash_sec
)->this_hdr
.sh_entsize
;
6544 bucketpos
= ((bfd_byte
*) finfo
->hash_sec
->contents
6545 + (bucket
+ 2) * hash_entry_size
);
6546 chain
= bfd_get (8 * hash_entry_size
, finfo
->output_bfd
, bucketpos
);
6547 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, h
->dynindx
, bucketpos
);
6548 bfd_put (8 * hash_entry_size
, finfo
->output_bfd
, chain
,
6549 ((bfd_byte
*) finfo
->hash_sec
->contents
6550 + (bucketcount
+ 2 + h
->dynindx
) * hash_entry_size
));
6552 if (finfo
->symver_sec
!= NULL
&& finfo
->symver_sec
->contents
!= NULL
)
6554 Elf_Internal_Versym iversym
;
6555 Elf_External_Versym
*eversym
;
6557 if (!h
->def_regular
)
6559 if (h
->verinfo
.verdef
== NULL
)
6560 iversym
.vs_vers
= 0;
6562 iversym
.vs_vers
= h
->verinfo
.verdef
->vd_exp_refno
+ 1;
6566 if (h
->verinfo
.vertree
== NULL
)
6567 iversym
.vs_vers
= 1;
6569 iversym
.vs_vers
= h
->verinfo
.vertree
->vernum
+ 1;
6570 if (finfo
->info
->create_default_symver
)
6575 iversym
.vs_vers
|= VERSYM_HIDDEN
;
6577 eversym
= (Elf_External_Versym
*) finfo
->symver_sec
->contents
;
6578 eversym
+= h
->dynindx
;
6579 _bfd_elf_swap_versym_out (finfo
->output_bfd
, &iversym
, eversym
);
6583 /* If we're stripping it, then it was just a dynamic symbol, and
6584 there's nothing else to do. */
6585 if (strip
|| (input_sec
->flags
& SEC_EXCLUDE
) != 0)
6588 h
->indx
= bfd_get_symcount (finfo
->output_bfd
);
6590 if (! elf_link_output_sym (finfo
, h
->root
.root
.string
, &sym
, input_sec
, h
))
6592 eoinfo
->failed
= TRUE
;
6599 /* Return TRUE if special handling is done for relocs in SEC against
6600 symbols defined in discarded sections. */
6603 elf_section_ignore_discarded_relocs (asection
*sec
)
6605 const struct elf_backend_data
*bed
;
6607 switch (sec
->sec_info_type
)
6609 case ELF_INFO_TYPE_STABS
:
6610 case ELF_INFO_TYPE_EH_FRAME
:
6616 bed
= get_elf_backend_data (sec
->owner
);
6617 if (bed
->elf_backend_ignore_discarded_relocs
!= NULL
6618 && (*bed
->elf_backend_ignore_discarded_relocs
) (sec
))
6624 enum action_discarded
6630 /* Return a mask saying how ld should treat relocations in SEC against
6631 symbols defined in discarded sections. If this function returns
6632 COMPLAIN set, ld will issue a warning message. If this function
6633 returns PRETEND set, and the discarded section was link-once and the
6634 same size as the kept link-once section, ld will pretend that the
6635 symbol was actually defined in the kept section. Otherwise ld will
6636 zero the reloc (at least that is the intent, but some cooperation by
6637 the target dependent code is needed, particularly for REL targets). */
6640 elf_action_discarded (asection
*sec
)
6642 if (sec
->flags
& SEC_DEBUGGING
)
6645 if (strcmp (".eh_frame", sec
->name
) == 0)
6648 if (strcmp (".gcc_except_table", sec
->name
) == 0)
6651 if (strcmp (".PARISC.unwind", sec
->name
) == 0)
6654 if (strcmp (".fixup", sec
->name
) == 0)
6657 return COMPLAIN
| PRETEND
;
6660 /* Find a match between a section and a member of a section group. */
6663 match_group_member (asection
*sec
, asection
*group
)
6665 asection
*first
= elf_next_in_group (group
);
6666 asection
*s
= first
;
6670 if (bfd_elf_match_symbols_in_sections (s
, sec
))
6680 /* Link an input file into the linker output file. This function
6681 handles all the sections and relocations of the input file at once.
6682 This is so that we only have to read the local symbols once, and
6683 don't have to keep them in memory. */
6686 elf_link_input_bfd (struct elf_final_link_info
*finfo
, bfd
*input_bfd
)
6688 bfd_boolean (*relocate_section
)
6689 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
6690 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
6692 Elf_Internal_Shdr
*symtab_hdr
;
6695 Elf_Internal_Sym
*isymbuf
;
6696 Elf_Internal_Sym
*isym
;
6697 Elf_Internal_Sym
*isymend
;
6699 asection
**ppsection
;
6701 const struct elf_backend_data
*bed
;
6702 bfd_boolean emit_relocs
;
6703 struct elf_link_hash_entry
**sym_hashes
;
6705 output_bfd
= finfo
->output_bfd
;
6706 bed
= get_elf_backend_data (output_bfd
);
6707 relocate_section
= bed
->elf_backend_relocate_section
;
6709 /* If this is a dynamic object, we don't want to do anything here:
6710 we don't want the local symbols, and we don't want the section
6712 if ((input_bfd
->flags
& DYNAMIC
) != 0)
6715 emit_relocs
= (finfo
->info
->relocatable
6716 || finfo
->info
->emitrelocations
6717 || bed
->elf_backend_emit_relocs
);
6719 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
6720 if (elf_bad_symtab (input_bfd
))
6722 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
6727 locsymcount
= symtab_hdr
->sh_info
;
6728 extsymoff
= symtab_hdr
->sh_info
;
6731 /* Read the local symbols. */
6732 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
6733 if (isymbuf
== NULL
&& locsymcount
!= 0)
6735 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
6736 finfo
->internal_syms
,
6737 finfo
->external_syms
,
6738 finfo
->locsym_shndx
);
6739 if (isymbuf
== NULL
)
6743 /* Find local symbol sections and adjust values of symbols in
6744 SEC_MERGE sections. Write out those local symbols we know are
6745 going into the output file. */
6746 isymend
= isymbuf
+ locsymcount
;
6747 for (isym
= isymbuf
, pindex
= finfo
->indices
, ppsection
= finfo
->sections
;
6749 isym
++, pindex
++, ppsection
++)
6753 Elf_Internal_Sym osym
;
6757 if (elf_bad_symtab (input_bfd
))
6759 if (ELF_ST_BIND (isym
->st_info
) != STB_LOCAL
)
6766 if (isym
->st_shndx
== SHN_UNDEF
)
6767 isec
= bfd_und_section_ptr
;
6768 else if (isym
->st_shndx
< SHN_LORESERVE
6769 || isym
->st_shndx
> SHN_HIRESERVE
)
6771 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
6773 && isec
->sec_info_type
== ELF_INFO_TYPE_MERGE
6774 && ELF_ST_TYPE (isym
->st_info
) != STT_SECTION
)
6776 _bfd_merged_section_offset (output_bfd
, &isec
,
6777 elf_section_data (isec
)->sec_info
,
6780 else if (isym
->st_shndx
== SHN_ABS
)
6781 isec
= bfd_abs_section_ptr
;
6782 else if (isym
->st_shndx
== SHN_COMMON
)
6783 isec
= bfd_com_section_ptr
;
6792 /* Don't output the first, undefined, symbol. */
6793 if (ppsection
== finfo
->sections
)
6796 if (ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
)
6798 /* We never output section symbols. Instead, we use the
6799 section symbol of the corresponding section in the output
6804 /* If we are stripping all symbols, we don't want to output this
6806 if (finfo
->info
->strip
== strip_all
)
6809 /* If we are discarding all local symbols, we don't want to
6810 output this one. If we are generating a relocatable output
6811 file, then some of the local symbols may be required by
6812 relocs; we output them below as we discover that they are
6814 if (finfo
->info
->discard
== discard_all
)
6817 /* If this symbol is defined in a section which we are
6818 discarding, we don't need to keep it, but note that
6819 linker_mark is only reliable for sections that have contents.
6820 For the benefit of the MIPS ELF linker, we check SEC_EXCLUDE
6821 as well as linker_mark. */
6822 if ((isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
6824 || (! isec
->linker_mark
&& (isec
->flags
& SEC_HAS_CONTENTS
) != 0)
6825 || (! finfo
->info
->relocatable
6826 && (isec
->flags
& SEC_EXCLUDE
) != 0)))
6829 /* Get the name of the symbol. */
6830 name
= bfd_elf_string_from_elf_section (input_bfd
, symtab_hdr
->sh_link
,
6835 /* See if we are discarding symbols with this name. */
6836 if ((finfo
->info
->strip
== strip_some
6837 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
, FALSE
)
6839 || (((finfo
->info
->discard
== discard_sec_merge
6840 && (isec
->flags
& SEC_MERGE
) && ! finfo
->info
->relocatable
)
6841 || finfo
->info
->discard
== discard_l
)
6842 && bfd_is_local_label_name (input_bfd
, name
)))
6845 /* If we get here, we are going to output this symbol. */
6849 /* Adjust the section index for the output file. */
6850 osym
.st_shndx
= _bfd_elf_section_from_bfd_section (output_bfd
,
6851 isec
->output_section
);
6852 if (osym
.st_shndx
== SHN_BAD
)
6855 *pindex
= bfd_get_symcount (output_bfd
);
6857 /* ELF symbols in relocatable files are section relative, but
6858 in executable files they are virtual addresses. Note that
6859 this code assumes that all ELF sections have an associated
6860 BFD section with a reasonable value for output_offset; below
6861 we assume that they also have a reasonable value for
6862 output_section. Any special sections must be set up to meet
6863 these requirements. */
6864 osym
.st_value
+= isec
->output_offset
;
6865 if (! finfo
->info
->relocatable
)
6867 osym
.st_value
+= isec
->output_section
->vma
;
6868 if (ELF_ST_TYPE (osym
.st_info
) == STT_TLS
)
6870 /* STT_TLS symbols are relative to PT_TLS segment base. */
6871 BFD_ASSERT (elf_hash_table (finfo
->info
)->tls_sec
!= NULL
);
6872 osym
.st_value
-= elf_hash_table (finfo
->info
)->tls_sec
->vma
;
6876 if (! elf_link_output_sym (finfo
, name
, &osym
, isec
, NULL
))
6880 /* Relocate the contents of each section. */
6881 sym_hashes
= elf_sym_hashes (input_bfd
);
6882 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
6886 if (! o
->linker_mark
)
6888 /* This section was omitted from the link. */
6892 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
6893 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
6896 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
6898 /* Section was created by _bfd_elf_link_create_dynamic_sections
6903 /* Get the contents of the section. They have been cached by a
6904 relaxation routine. Note that o is a section in an input
6905 file, so the contents field will not have been set by any of
6906 the routines which work on output files. */
6907 if (elf_section_data (o
)->this_hdr
.contents
!= NULL
)
6908 contents
= elf_section_data (o
)->this_hdr
.contents
;
6911 bfd_size_type amt
= o
->rawsize
? o
->rawsize
: o
->size
;
6913 contents
= finfo
->contents
;
6914 if (! bfd_get_section_contents (input_bfd
, o
, contents
, 0, amt
))
6918 if ((o
->flags
& SEC_RELOC
) != 0)
6920 Elf_Internal_Rela
*internal_relocs
;
6921 bfd_vma r_type_mask
;
6924 /* Get the swapped relocs. */
6926 = _bfd_elf_link_read_relocs (input_bfd
, o
, finfo
->external_relocs
,
6927 finfo
->internal_relocs
, FALSE
);
6928 if (internal_relocs
== NULL
6929 && o
->reloc_count
> 0)
6932 if (bed
->s
->arch_size
== 32)
6939 r_type_mask
= 0xffffffff;
6943 /* Run through the relocs looking for any against symbols
6944 from discarded sections and section symbols from
6945 removed link-once sections. Complain about relocs
6946 against discarded sections. Zero relocs against removed
6947 link-once sections. Preserve debug information as much
6949 if (!elf_section_ignore_discarded_relocs (o
))
6951 Elf_Internal_Rela
*rel
, *relend
;
6952 unsigned int action
= elf_action_discarded (o
);
6954 rel
= internal_relocs
;
6955 relend
= rel
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
6956 for ( ; rel
< relend
; rel
++)
6958 unsigned long r_symndx
= rel
->r_info
>> r_sym_shift
;
6959 asection
**ps
, *sec
;
6960 struct elf_link_hash_entry
*h
= NULL
;
6961 const char *sym_name
;
6963 if (r_symndx
== STN_UNDEF
)
6966 if (r_symndx
>= locsymcount
6967 || (elf_bad_symtab (input_bfd
)
6968 && finfo
->sections
[r_symndx
] == NULL
))
6970 h
= sym_hashes
[r_symndx
- extsymoff
];
6971 while (h
->root
.type
== bfd_link_hash_indirect
6972 || h
->root
.type
== bfd_link_hash_warning
)
6973 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
6975 if (h
->root
.type
!= bfd_link_hash_defined
6976 && h
->root
.type
!= bfd_link_hash_defweak
)
6979 ps
= &h
->root
.u
.def
.section
;
6980 sym_name
= h
->root
.root
.string
;
6984 Elf_Internal_Sym
*sym
= isymbuf
+ r_symndx
;
6985 ps
= &finfo
->sections
[r_symndx
];
6986 sym_name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
);
6989 /* Complain if the definition comes from a
6990 discarded section. */
6991 if ((sec
= *ps
) != NULL
&& elf_discarded_section (sec
))
6995 BFD_ASSERT (r_symndx
!= 0);
6996 if (action
& COMPLAIN
)
6998 (*_bfd_error_handler
)
6999 (_("`%s' referenced in section `%A' of %B: "
7000 "defined in discarded section `%A' of %B\n"),
7001 o
, input_bfd
, sec
, sec
->owner
, sym_name
);
7004 /* Try to do the best we can to support buggy old
7005 versions of gcc. If we've warned, or this is
7006 debugging info, pretend that the symbol is
7007 really defined in the kept linkonce section.
7008 FIXME: This is quite broken. Modifying the
7009 symbol here means we will be changing all later
7010 uses of the symbol, not just in this section.
7011 The only thing that makes this half reasonable
7012 is that we warn in non-debug sections, and
7013 debug sections tend to come after other
7015 kept
= sec
->kept_section
;
7016 if (kept
!= NULL
&& (action
& PRETEND
))
7018 if (elf_sec_group (sec
) != NULL
)
7019 kept
= match_group_member (sec
, kept
);
7021 && sec
->size
== kept
->size
)
7028 /* Remove the symbol reference from the reloc, but
7029 don't kill the reloc completely. This is so that
7030 a zero value will be written into the section,
7031 which may have non-zero contents put there by the
7032 assembler. Zero in things like an eh_frame fde
7033 pc_begin allows stack unwinders to recognize the
7035 rel
->r_info
&= r_type_mask
;
7041 /* Relocate the section by invoking a back end routine.
7043 The back end routine is responsible for adjusting the
7044 section contents as necessary, and (if using Rela relocs
7045 and generating a relocatable output file) adjusting the
7046 reloc addend as necessary.
7048 The back end routine does not have to worry about setting
7049 the reloc address or the reloc symbol index.
7051 The back end routine is given a pointer to the swapped in
7052 internal symbols, and can access the hash table entries
7053 for the external symbols via elf_sym_hashes (input_bfd).
7055 When generating relocatable output, the back end routine
7056 must handle STB_LOCAL/STT_SECTION symbols specially. The
7057 output symbol is going to be a section symbol
7058 corresponding to the output section, which will require
7059 the addend to be adjusted. */
7061 if (! (*relocate_section
) (output_bfd
, finfo
->info
,
7062 input_bfd
, o
, contents
,
7070 Elf_Internal_Rela
*irela
;
7071 Elf_Internal_Rela
*irelaend
;
7072 bfd_vma last_offset
;
7073 struct elf_link_hash_entry
**rel_hash
;
7074 Elf_Internal_Shdr
*input_rel_hdr
, *input_rel_hdr2
;
7075 unsigned int next_erel
;
7076 bfd_boolean (*reloc_emitter
)
7077 (bfd
*, asection
*, Elf_Internal_Shdr
*, Elf_Internal_Rela
*);
7078 bfd_boolean rela_normal
;
7080 input_rel_hdr
= &elf_section_data (o
)->rel_hdr
;
7081 rela_normal
= (bed
->rela_normal
7082 && (input_rel_hdr
->sh_entsize
7083 == bed
->s
->sizeof_rela
));
7085 /* Adjust the reloc addresses and symbol indices. */
7087 irela
= internal_relocs
;
7088 irelaend
= irela
+ o
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
7089 rel_hash
= (elf_section_data (o
->output_section
)->rel_hashes
7090 + elf_section_data (o
->output_section
)->rel_count
7091 + elf_section_data (o
->output_section
)->rel_count2
);
7092 last_offset
= o
->output_offset
;
7093 if (!finfo
->info
->relocatable
)
7094 last_offset
+= o
->output_section
->vma
;
7095 for (next_erel
= 0; irela
< irelaend
; irela
++, next_erel
++)
7097 unsigned long r_symndx
;
7099 Elf_Internal_Sym sym
;
7101 if (next_erel
== bed
->s
->int_rels_per_ext_rel
)
7107 irela
->r_offset
= _bfd_elf_section_offset (output_bfd
,
7110 if (irela
->r_offset
>= (bfd_vma
) -2)
7112 /* This is a reloc for a deleted entry or somesuch.
7113 Turn it into an R_*_NONE reloc, at the same
7114 offset as the last reloc. elf_eh_frame.c and
7115 elf_bfd_discard_info rely on reloc offsets
7117 irela
->r_offset
= last_offset
;
7119 irela
->r_addend
= 0;
7123 irela
->r_offset
+= o
->output_offset
;
7125 /* Relocs in an executable have to be virtual addresses. */
7126 if (!finfo
->info
->relocatable
)
7127 irela
->r_offset
+= o
->output_section
->vma
;
7129 last_offset
= irela
->r_offset
;
7131 r_symndx
= irela
->r_info
>> r_sym_shift
;
7132 if (r_symndx
== STN_UNDEF
)
7135 if (r_symndx
>= locsymcount
7136 || (elf_bad_symtab (input_bfd
)
7137 && finfo
->sections
[r_symndx
] == NULL
))
7139 struct elf_link_hash_entry
*rh
;
7142 /* This is a reloc against a global symbol. We
7143 have not yet output all the local symbols, so
7144 we do not know the symbol index of any global
7145 symbol. We set the rel_hash entry for this
7146 reloc to point to the global hash table entry
7147 for this symbol. The symbol index is then
7148 set at the end of bfd_elf_final_link. */
7149 indx
= r_symndx
- extsymoff
;
7150 rh
= elf_sym_hashes (input_bfd
)[indx
];
7151 while (rh
->root
.type
== bfd_link_hash_indirect
7152 || rh
->root
.type
== bfd_link_hash_warning
)
7153 rh
= (struct elf_link_hash_entry
*) rh
->root
.u
.i
.link
;
7155 /* Setting the index to -2 tells
7156 elf_link_output_extsym that this symbol is
7158 BFD_ASSERT (rh
->indx
< 0);
7166 /* This is a reloc against a local symbol. */
7169 sym
= isymbuf
[r_symndx
];
7170 sec
= finfo
->sections
[r_symndx
];
7171 if (ELF_ST_TYPE (sym
.st_info
) == STT_SECTION
)
7173 /* I suppose the backend ought to fill in the
7174 section of any STT_SECTION symbol against a
7175 processor specific section. */
7177 if (bfd_is_abs_section (sec
))
7179 else if (sec
== NULL
|| sec
->owner
== NULL
)
7181 bfd_set_error (bfd_error_bad_value
);
7186 asection
*osec
= sec
->output_section
;
7188 /* If we have discarded a section, the output
7189 section will be the absolute section. In
7190 case of discarded link-once and discarded
7191 SEC_MERGE sections, use the kept section. */
7192 if (bfd_is_abs_section (osec
)
7193 && sec
->kept_section
!= NULL
7194 && sec
->kept_section
->output_section
!= NULL
)
7196 osec
= sec
->kept_section
->output_section
;
7197 irela
->r_addend
-= osec
->vma
;
7200 if (!bfd_is_abs_section (osec
))
7202 r_symndx
= osec
->target_index
;
7203 BFD_ASSERT (r_symndx
!= 0);
7207 /* Adjust the addend according to where the
7208 section winds up in the output section. */
7210 irela
->r_addend
+= sec
->output_offset
;
7214 if (finfo
->indices
[r_symndx
] == -1)
7216 unsigned long shlink
;
7220 if (finfo
->info
->strip
== strip_all
)
7222 /* You can't do ld -r -s. */
7223 bfd_set_error (bfd_error_invalid_operation
);
7227 /* This symbol was skipped earlier, but
7228 since it is needed by a reloc, we
7229 must output it now. */
7230 shlink
= symtab_hdr
->sh_link
;
7231 name
= (bfd_elf_string_from_elf_section
7232 (input_bfd
, shlink
, sym
.st_name
));
7236 osec
= sec
->output_section
;
7238 _bfd_elf_section_from_bfd_section (output_bfd
,
7240 if (sym
.st_shndx
== SHN_BAD
)
7243 sym
.st_value
+= sec
->output_offset
;
7244 if (! finfo
->info
->relocatable
)
7246 sym
.st_value
+= osec
->vma
;
7247 if (ELF_ST_TYPE (sym
.st_info
) == STT_TLS
)
7249 /* STT_TLS symbols are relative to PT_TLS
7251 BFD_ASSERT (elf_hash_table (finfo
->info
)
7253 sym
.st_value
-= (elf_hash_table (finfo
->info
)
7258 finfo
->indices
[r_symndx
]
7259 = bfd_get_symcount (output_bfd
);
7261 if (! elf_link_output_sym (finfo
, name
, &sym
, sec
,
7266 r_symndx
= finfo
->indices
[r_symndx
];
7269 irela
->r_info
= ((bfd_vma
) r_symndx
<< r_sym_shift
7270 | (irela
->r_info
& r_type_mask
));
7273 /* Swap out the relocs. */
7274 if (bed
->elf_backend_emit_relocs
7275 && !(finfo
->info
->relocatable
7276 || finfo
->info
->emitrelocations
))
7277 reloc_emitter
= bed
->elf_backend_emit_relocs
;
7279 reloc_emitter
= _bfd_elf_link_output_relocs
;
7281 if (input_rel_hdr
->sh_size
!= 0
7282 && ! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr
,
7286 input_rel_hdr2
= elf_section_data (o
)->rel_hdr2
;
7287 if (input_rel_hdr2
&& input_rel_hdr2
->sh_size
!= 0)
7289 internal_relocs
+= (NUM_SHDR_ENTRIES (input_rel_hdr
)
7290 * bed
->s
->int_rels_per_ext_rel
);
7291 if (! (*reloc_emitter
) (output_bfd
, o
, input_rel_hdr2
,
7298 /* Write out the modified section contents. */
7299 if (bed
->elf_backend_write_section
7300 && (*bed
->elf_backend_write_section
) (output_bfd
, o
, contents
))
7302 /* Section written out. */
7304 else switch (o
->sec_info_type
)
7306 case ELF_INFO_TYPE_STABS
:
7307 if (! (_bfd_write_section_stabs
7309 &elf_hash_table (finfo
->info
)->stab_info
,
7310 o
, &elf_section_data (o
)->sec_info
, contents
)))
7313 case ELF_INFO_TYPE_MERGE
:
7314 if (! _bfd_write_merged_section (output_bfd
, o
,
7315 elf_section_data (o
)->sec_info
))
7318 case ELF_INFO_TYPE_EH_FRAME
:
7320 if (! _bfd_elf_write_section_eh_frame (output_bfd
, finfo
->info
,
7327 if (! (o
->flags
& SEC_EXCLUDE
)
7328 && ! bfd_set_section_contents (output_bfd
, o
->output_section
,
7330 (file_ptr
) o
->output_offset
,
7341 /* Generate a reloc when linking an ELF file. This is a reloc
7342 requested by the linker, and does come from any input file. This
7343 is used to build constructor and destructor tables when linking
7347 elf_reloc_link_order (bfd
*output_bfd
,
7348 struct bfd_link_info
*info
,
7349 asection
*output_section
,
7350 struct bfd_link_order
*link_order
)
7352 reloc_howto_type
*howto
;
7356 struct elf_link_hash_entry
**rel_hash_ptr
;
7357 Elf_Internal_Shdr
*rel_hdr
;
7358 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
7359 Elf_Internal_Rela irel
[MAX_INT_RELS_PER_EXT_REL
];
7363 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
7366 bfd_set_error (bfd_error_bad_value
);
7370 addend
= link_order
->u
.reloc
.p
->addend
;
7372 /* Figure out the symbol index. */
7373 rel_hash_ptr
= (elf_section_data (output_section
)->rel_hashes
7374 + elf_section_data (output_section
)->rel_count
7375 + elf_section_data (output_section
)->rel_count2
);
7376 if (link_order
->type
== bfd_section_reloc_link_order
)
7378 indx
= link_order
->u
.reloc
.p
->u
.section
->target_index
;
7379 BFD_ASSERT (indx
!= 0);
7380 *rel_hash_ptr
= NULL
;
7384 struct elf_link_hash_entry
*h
;
7386 /* Treat a reloc against a defined symbol as though it were
7387 actually against the section. */
7388 h
= ((struct elf_link_hash_entry
*)
7389 bfd_wrapped_link_hash_lookup (output_bfd
, info
,
7390 link_order
->u
.reloc
.p
->u
.name
,
7391 FALSE
, FALSE
, TRUE
));
7393 && (h
->root
.type
== bfd_link_hash_defined
7394 || h
->root
.type
== bfd_link_hash_defweak
))
7398 section
= h
->root
.u
.def
.section
;
7399 indx
= section
->output_section
->target_index
;
7400 *rel_hash_ptr
= NULL
;
7401 /* It seems that we ought to add the symbol value to the
7402 addend here, but in practice it has already been added
7403 because it was passed to constructor_callback. */
7404 addend
+= section
->output_section
->vma
+ section
->output_offset
;
7408 /* Setting the index to -2 tells elf_link_output_extsym that
7409 this symbol is used by a reloc. */
7416 if (! ((*info
->callbacks
->unattached_reloc
)
7417 (info
, link_order
->u
.reloc
.p
->u
.name
, NULL
, NULL
, 0)))
7423 /* If this is an inplace reloc, we must write the addend into the
7425 if (howto
->partial_inplace
&& addend
!= 0)
7428 bfd_reloc_status_type rstat
;
7431 const char *sym_name
;
7433 size
= bfd_get_reloc_size (howto
);
7434 buf
= bfd_zmalloc (size
);
7437 rstat
= _bfd_relocate_contents (howto
, output_bfd
, addend
, buf
);
7444 case bfd_reloc_outofrange
:
7447 case bfd_reloc_overflow
:
7448 if (link_order
->type
== bfd_section_reloc_link_order
)
7449 sym_name
= bfd_section_name (output_bfd
,
7450 link_order
->u
.reloc
.p
->u
.section
);
7452 sym_name
= link_order
->u
.reloc
.p
->u
.name
;
7453 if (! ((*info
->callbacks
->reloc_overflow
)
7454 (info
, NULL
, sym_name
, howto
->name
, addend
, NULL
,
7455 NULL
, (bfd_vma
) 0)))
7462 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
7463 link_order
->offset
, size
);
7469 /* The address of a reloc is relative to the section in a
7470 relocatable file, and is a virtual address in an executable
7472 offset
= link_order
->offset
;
7473 if (! info
->relocatable
)
7474 offset
+= output_section
->vma
;
7476 for (i
= 0; i
< bed
->s
->int_rels_per_ext_rel
; i
++)
7478 irel
[i
].r_offset
= offset
;
7480 irel
[i
].r_addend
= 0;
7482 if (bed
->s
->arch_size
== 32)
7483 irel
[0].r_info
= ELF32_R_INFO (indx
, howto
->type
);
7485 irel
[0].r_info
= ELF64_R_INFO (indx
, howto
->type
);
7487 rel_hdr
= &elf_section_data (output_section
)->rel_hdr
;
7488 erel
= rel_hdr
->contents
;
7489 if (rel_hdr
->sh_type
== SHT_REL
)
7491 erel
+= (elf_section_data (output_section
)->rel_count
7492 * bed
->s
->sizeof_rel
);
7493 (*bed
->s
->swap_reloc_out
) (output_bfd
, irel
, erel
);
7497 irel
[0].r_addend
= addend
;
7498 erel
+= (elf_section_data (output_section
)->rel_count
7499 * bed
->s
->sizeof_rela
);
7500 (*bed
->s
->swap_reloca_out
) (output_bfd
, irel
, erel
);
7503 ++elf_section_data (output_section
)->rel_count
;
7509 /* Get the output vma of the section pointed to by the sh_link field. */
7512 elf_get_linked_section_vma (struct bfd_link_order
*p
)
7514 Elf_Internal_Shdr
**elf_shdrp
;
7518 s
= p
->u
.indirect
.section
;
7519 elf_shdrp
= elf_elfsections (s
->owner
);
7520 elfsec
= _bfd_elf_section_from_bfd_section (s
->owner
, s
);
7521 elfsec
= elf_shdrp
[elfsec
]->sh_link
;
7523 The Intel C compiler generates SHT_IA_64_UNWIND with
7524 SHF_LINK_ORDER. But it doesn't set theh sh_link or
7525 sh_info fields. Hence we could get the situation
7526 where elfsec is 0. */
7529 const struct elf_backend_data
*bed
7530 = get_elf_backend_data (s
->owner
);
7531 if (bed
->link_order_error_handler
)
7532 bed
->link_order_error_handler
7533 (_("%B: warning: sh_link not set for section `%A'"), s
->owner
, s
);
7538 s
= elf_shdrp
[elfsec
]->bfd_section
;
7539 return s
->output_section
->vma
+ s
->output_offset
;
7544 /* Compare two sections based on the locations of the sections they are
7545 linked to. Used by elf_fixup_link_order. */
7548 compare_link_order (const void * a
, const void * b
)
7553 apos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)a
);
7554 bpos
= elf_get_linked_section_vma (*(struct bfd_link_order
**)b
);
7561 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7562 order as their linked sections. Returns false if this could not be done
7563 because an output section includes both ordered and unordered
7564 sections. Ideally we'd do this in the linker proper. */
7567 elf_fixup_link_order (bfd
*abfd
, asection
*o
)
7572 struct bfd_link_order
*p
;
7574 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7576 struct bfd_link_order
**sections
;
7582 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7584 if (p
->type
== bfd_indirect_link_order
7585 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
7586 == bfd_target_elf_flavour
)
7587 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
7589 s
= p
->u
.indirect
.section
;
7590 elfsec
= _bfd_elf_section_from_bfd_section (sub
, s
);
7592 && elf_elfsections (sub
)[elfsec
]->sh_flags
& SHF_LINK_ORDER
)
7601 if (!seen_linkorder
)
7604 if (seen_other
&& seen_linkorder
)
7606 (*_bfd_error_handler
) (_("%A has both ordered and unordered sections"),
7608 bfd_set_error (bfd_error_bad_value
);
7612 sections
= (struct bfd_link_order
**)
7613 xmalloc (seen_linkorder
* sizeof (struct bfd_link_order
*));
7616 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7618 sections
[seen_linkorder
++] = p
;
7620 /* Sort the input sections in the order of their linked section. */
7621 qsort (sections
, seen_linkorder
, sizeof (struct bfd_link_order
*),
7622 compare_link_order
);
7624 /* Change the offsets of the sections. */
7626 for (n
= 0; n
< seen_linkorder
; n
++)
7628 s
= sections
[n
]->u
.indirect
.section
;
7629 offset
&= ~(bfd_vma
)((1 << s
->alignment_power
) - 1);
7630 s
->output_offset
= offset
;
7631 sections
[n
]->offset
= offset
;
7632 offset
+= sections
[n
]->size
;
7639 /* Do the final step of an ELF link. */
7642 bfd_elf_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
7644 bfd_boolean dynamic
;
7645 bfd_boolean emit_relocs
;
7647 struct elf_final_link_info finfo
;
7648 register asection
*o
;
7649 register struct bfd_link_order
*p
;
7651 bfd_size_type max_contents_size
;
7652 bfd_size_type max_external_reloc_size
;
7653 bfd_size_type max_internal_reloc_count
;
7654 bfd_size_type max_sym_count
;
7655 bfd_size_type max_sym_shndx_count
;
7657 Elf_Internal_Sym elfsym
;
7659 Elf_Internal_Shdr
*symtab_hdr
;
7660 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7661 Elf_Internal_Shdr
*symstrtab_hdr
;
7662 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7663 struct elf_outext_info eoinfo
;
7665 size_t relativecount
= 0;
7666 asection
*reldyn
= 0;
7669 if (! is_elf_hash_table (info
->hash
))
7673 abfd
->flags
|= DYNAMIC
;
7675 dynamic
= elf_hash_table (info
)->dynamic_sections_created
;
7676 dynobj
= elf_hash_table (info
)->dynobj
;
7678 emit_relocs
= (info
->relocatable
7679 || info
->emitrelocations
7680 || bed
->elf_backend_emit_relocs
);
7683 finfo
.output_bfd
= abfd
;
7684 finfo
.symstrtab
= _bfd_elf_stringtab_init ();
7685 if (finfo
.symstrtab
== NULL
)
7690 finfo
.dynsym_sec
= NULL
;
7691 finfo
.hash_sec
= NULL
;
7692 finfo
.symver_sec
= NULL
;
7696 finfo
.dynsym_sec
= bfd_get_section_by_name (dynobj
, ".dynsym");
7697 finfo
.hash_sec
= bfd_get_section_by_name (dynobj
, ".hash");
7698 BFD_ASSERT (finfo
.dynsym_sec
!= NULL
&& finfo
.hash_sec
!= NULL
);
7699 finfo
.symver_sec
= bfd_get_section_by_name (dynobj
, ".gnu.version");
7700 /* Note that it is OK if symver_sec is NULL. */
7703 finfo
.contents
= NULL
;
7704 finfo
.external_relocs
= NULL
;
7705 finfo
.internal_relocs
= NULL
;
7706 finfo
.external_syms
= NULL
;
7707 finfo
.locsym_shndx
= NULL
;
7708 finfo
.internal_syms
= NULL
;
7709 finfo
.indices
= NULL
;
7710 finfo
.sections
= NULL
;
7711 finfo
.symbuf
= NULL
;
7712 finfo
.symshndxbuf
= NULL
;
7713 finfo
.symbuf_count
= 0;
7714 finfo
.shndxbuf_size
= 0;
7716 /* Count up the number of relocations we will output for each output
7717 section, so that we know the sizes of the reloc sections. We
7718 also figure out some maximum sizes. */
7719 max_contents_size
= 0;
7720 max_external_reloc_size
= 0;
7721 max_internal_reloc_count
= 0;
7723 max_sym_shndx_count
= 0;
7725 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7727 struct bfd_elf_section_data
*esdo
= elf_section_data (o
);
7730 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
7732 unsigned int reloc_count
= 0;
7733 struct bfd_elf_section_data
*esdi
= NULL
;
7734 unsigned int *rel_count1
;
7736 if (p
->type
== bfd_section_reloc_link_order
7737 || p
->type
== bfd_symbol_reloc_link_order
)
7739 else if (p
->type
== bfd_indirect_link_order
)
7743 sec
= p
->u
.indirect
.section
;
7744 esdi
= elf_section_data (sec
);
7746 /* Mark all sections which are to be included in the
7747 link. This will normally be every section. We need
7748 to do this so that we can identify any sections which
7749 the linker has decided to not include. */
7750 sec
->linker_mark
= TRUE
;
7752 if (sec
->flags
& SEC_MERGE
)
7755 if (info
->relocatable
|| info
->emitrelocations
)
7756 reloc_count
= sec
->reloc_count
;
7757 else if (bed
->elf_backend_count_relocs
)
7759 Elf_Internal_Rela
* relocs
;
7761 relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
7764 reloc_count
= (*bed
->elf_backend_count_relocs
) (sec
, relocs
);
7766 if (elf_section_data (o
)->relocs
!= relocs
)
7770 if (sec
->rawsize
> max_contents_size
)
7771 max_contents_size
= sec
->rawsize
;
7772 if (sec
->size
> max_contents_size
)
7773 max_contents_size
= sec
->size
;
7775 /* We are interested in just local symbols, not all
7777 if (bfd_get_flavour (sec
->owner
) == bfd_target_elf_flavour
7778 && (sec
->owner
->flags
& DYNAMIC
) == 0)
7782 if (elf_bad_symtab (sec
->owner
))
7783 sym_count
= (elf_tdata (sec
->owner
)->symtab_hdr
.sh_size
7784 / bed
->s
->sizeof_sym
);
7786 sym_count
= elf_tdata (sec
->owner
)->symtab_hdr
.sh_info
;
7788 if (sym_count
> max_sym_count
)
7789 max_sym_count
= sym_count
;
7791 if (sym_count
> max_sym_shndx_count
7792 && elf_symtab_shndx (sec
->owner
) != 0)
7793 max_sym_shndx_count
= sym_count
;
7795 if ((sec
->flags
& SEC_RELOC
) != 0)
7799 ext_size
= elf_section_data (sec
)->rel_hdr
.sh_size
;
7800 if (ext_size
> max_external_reloc_size
)
7801 max_external_reloc_size
= ext_size
;
7802 if (sec
->reloc_count
> max_internal_reloc_count
)
7803 max_internal_reloc_count
= sec
->reloc_count
;
7808 if (reloc_count
== 0)
7811 o
->reloc_count
+= reloc_count
;
7813 /* MIPS may have a mix of REL and RELA relocs on sections.
7814 To support this curious ABI we keep reloc counts in
7815 elf_section_data too. We must be careful to add the
7816 relocations from the input section to the right output
7817 count. FIXME: Get rid of one count. We have
7818 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7819 rel_count1
= &esdo
->rel_count
;
7822 bfd_boolean same_size
;
7823 bfd_size_type entsize1
;
7825 entsize1
= esdi
->rel_hdr
.sh_entsize
;
7826 BFD_ASSERT (entsize1
== bed
->s
->sizeof_rel
7827 || entsize1
== bed
->s
->sizeof_rela
);
7828 same_size
= !o
->use_rela_p
== (entsize1
== bed
->s
->sizeof_rel
);
7831 rel_count1
= &esdo
->rel_count2
;
7833 if (esdi
->rel_hdr2
!= NULL
)
7835 bfd_size_type entsize2
= esdi
->rel_hdr2
->sh_entsize
;
7836 unsigned int alt_count
;
7837 unsigned int *rel_count2
;
7839 BFD_ASSERT (entsize2
!= entsize1
7840 && (entsize2
== bed
->s
->sizeof_rel
7841 || entsize2
== bed
->s
->sizeof_rela
));
7843 rel_count2
= &esdo
->rel_count2
;
7845 rel_count2
= &esdo
->rel_count
;
7847 /* The following is probably too simplistic if the
7848 backend counts output relocs unusually. */
7849 BFD_ASSERT (bed
->elf_backend_count_relocs
== NULL
);
7850 alt_count
= NUM_SHDR_ENTRIES (esdi
->rel_hdr2
);
7851 *rel_count2
+= alt_count
;
7852 reloc_count
-= alt_count
;
7855 *rel_count1
+= reloc_count
;
7858 if (o
->reloc_count
> 0)
7859 o
->flags
|= SEC_RELOC
;
7862 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7863 set it (this is probably a bug) and if it is set
7864 assign_section_numbers will create a reloc section. */
7865 o
->flags
&=~ SEC_RELOC
;
7868 /* If the SEC_ALLOC flag is not set, force the section VMA to
7869 zero. This is done in elf_fake_sections as well, but forcing
7870 the VMA to 0 here will ensure that relocs against these
7871 sections are handled correctly. */
7872 if ((o
->flags
& SEC_ALLOC
) == 0
7873 && ! o
->user_set_vma
)
7877 if (! info
->relocatable
&& merged
)
7878 elf_link_hash_traverse (elf_hash_table (info
),
7879 _bfd_elf_link_sec_merge_syms
, abfd
);
7881 /* Figure out the file positions for everything but the symbol table
7882 and the relocs. We set symcount to force assign_section_numbers
7883 to create a symbol table. */
7884 bfd_get_symcount (abfd
) = info
->strip
== strip_all
? 0 : 1;
7885 BFD_ASSERT (! abfd
->output_has_begun
);
7886 if (! _bfd_elf_compute_section_file_positions (abfd
, info
))
7889 /* Set sizes, and assign file positions for reloc sections. */
7890 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
7892 if ((o
->flags
& SEC_RELOC
) != 0)
7894 if (!(_bfd_elf_link_size_reloc_section
7895 (abfd
, &elf_section_data (o
)->rel_hdr
, o
)))
7898 if (elf_section_data (o
)->rel_hdr2
7899 && !(_bfd_elf_link_size_reloc_section
7900 (abfd
, elf_section_data (o
)->rel_hdr2
, o
)))
7904 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7905 to count upwards while actually outputting the relocations. */
7906 elf_section_data (o
)->rel_count
= 0;
7907 elf_section_data (o
)->rel_count2
= 0;
7910 _bfd_elf_assign_file_positions_for_relocs (abfd
);
7912 /* We have now assigned file positions for all the sections except
7913 .symtab and .strtab. We start the .symtab section at the current
7914 file position, and write directly to it. We build the .strtab
7915 section in memory. */
7916 bfd_get_symcount (abfd
) = 0;
7917 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7918 /* sh_name is set in prep_headers. */
7919 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7920 /* sh_flags, sh_addr and sh_size all start off zero. */
7921 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7922 /* sh_link is set in assign_section_numbers. */
7923 /* sh_info is set below. */
7924 /* sh_offset is set just below. */
7925 symtab_hdr
->sh_addralign
= 1 << bed
->s
->log_file_align
;
7927 off
= elf_tdata (abfd
)->next_file_pos
;
7928 off
= _bfd_elf_assign_file_position_for_section (symtab_hdr
, off
, TRUE
);
7930 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7931 incorrect. We do not yet know the size of the .symtab section.
7932 We correct next_file_pos below, after we do know the size. */
7934 /* Allocate a buffer to hold swapped out symbols. This is to avoid
7935 continuously seeking to the right position in the file. */
7936 if (! info
->keep_memory
|| max_sym_count
< 20)
7937 finfo
.symbuf_size
= 20;
7939 finfo
.symbuf_size
= max_sym_count
;
7940 amt
= finfo
.symbuf_size
;
7941 amt
*= bed
->s
->sizeof_sym
;
7942 finfo
.symbuf
= bfd_malloc (amt
);
7943 if (finfo
.symbuf
== NULL
)
7945 if (elf_numsections (abfd
) > SHN_LORESERVE
)
7947 /* Wild guess at number of output symbols. realloc'd as needed. */
7948 amt
= 2 * max_sym_count
+ elf_numsections (abfd
) + 1000;
7949 finfo
.shndxbuf_size
= amt
;
7950 amt
*= sizeof (Elf_External_Sym_Shndx
);
7951 finfo
.symshndxbuf
= bfd_zmalloc (amt
);
7952 if (finfo
.symshndxbuf
== NULL
)
7956 /* Start writing out the symbol table. The first symbol is always a
7958 if (info
->strip
!= strip_all
7961 elfsym
.st_value
= 0;
7964 elfsym
.st_other
= 0;
7965 elfsym
.st_shndx
= SHN_UNDEF
;
7966 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, bfd_und_section_ptr
,
7971 /* Output a symbol for each section. We output these even if we are
7972 discarding local symbols, since they are used for relocs. These
7973 symbols have no names. We store the index of each one in the
7974 index field of the section, so that we can find it again when
7975 outputting relocs. */
7976 if (info
->strip
!= strip_all
7980 elfsym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7981 elfsym
.st_other
= 0;
7982 for (i
= 1; i
< elf_numsections (abfd
); i
++)
7984 o
= bfd_section_from_elf_index (abfd
, i
);
7986 o
->target_index
= bfd_get_symcount (abfd
);
7987 elfsym
.st_shndx
= i
;
7988 if (info
->relocatable
|| o
== NULL
)
7989 elfsym
.st_value
= 0;
7991 elfsym
.st_value
= o
->vma
;
7992 if (! elf_link_output_sym (&finfo
, NULL
, &elfsym
, o
, NULL
))
7994 if (i
== SHN_LORESERVE
- 1)
7995 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
7999 /* Allocate some memory to hold information read in from the input
8001 if (max_contents_size
!= 0)
8003 finfo
.contents
= bfd_malloc (max_contents_size
);
8004 if (finfo
.contents
== NULL
)
8008 if (max_external_reloc_size
!= 0)
8010 finfo
.external_relocs
= bfd_malloc (max_external_reloc_size
);
8011 if (finfo
.external_relocs
== NULL
)
8015 if (max_internal_reloc_count
!= 0)
8017 amt
= max_internal_reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8018 amt
*= sizeof (Elf_Internal_Rela
);
8019 finfo
.internal_relocs
= bfd_malloc (amt
);
8020 if (finfo
.internal_relocs
== NULL
)
8024 if (max_sym_count
!= 0)
8026 amt
= max_sym_count
* bed
->s
->sizeof_sym
;
8027 finfo
.external_syms
= bfd_malloc (amt
);
8028 if (finfo
.external_syms
== NULL
)
8031 amt
= max_sym_count
* sizeof (Elf_Internal_Sym
);
8032 finfo
.internal_syms
= bfd_malloc (amt
);
8033 if (finfo
.internal_syms
== NULL
)
8036 amt
= max_sym_count
* sizeof (long);
8037 finfo
.indices
= bfd_malloc (amt
);
8038 if (finfo
.indices
== NULL
)
8041 amt
= max_sym_count
* sizeof (asection
*);
8042 finfo
.sections
= bfd_malloc (amt
);
8043 if (finfo
.sections
== NULL
)
8047 if (max_sym_shndx_count
!= 0)
8049 amt
= max_sym_shndx_count
* sizeof (Elf_External_Sym_Shndx
);
8050 finfo
.locsym_shndx
= bfd_malloc (amt
);
8051 if (finfo
.locsym_shndx
== NULL
)
8055 if (elf_hash_table (info
)->tls_sec
)
8057 bfd_vma base
, end
= 0;
8060 for (sec
= elf_hash_table (info
)->tls_sec
;
8061 sec
&& (sec
->flags
& SEC_THREAD_LOCAL
);
8064 bfd_vma size
= sec
->size
;
8066 if (size
== 0 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
8068 struct bfd_link_order
*o
;
8070 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
8071 if (size
< o
->offset
+ o
->size
)
8072 size
= o
->offset
+ o
->size
;
8074 end
= sec
->vma
+ size
;
8076 base
= elf_hash_table (info
)->tls_sec
->vma
;
8077 end
= align_power (end
, elf_hash_table (info
)->tls_sec
->alignment_power
);
8078 elf_hash_table (info
)->tls_size
= end
- base
;
8081 /* Reorder SHF_LINK_ORDER sections. */
8082 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8084 if (!elf_fixup_link_order (abfd
, o
))
8088 /* Since ELF permits relocations to be against local symbols, we
8089 must have the local symbols available when we do the relocations.
8090 Since we would rather only read the local symbols once, and we
8091 would rather not keep them in memory, we handle all the
8092 relocations for a single input file at the same time.
8094 Unfortunately, there is no way to know the total number of local
8095 symbols until we have seen all of them, and the local symbol
8096 indices precede the global symbol indices. This means that when
8097 we are generating relocatable output, and we see a reloc against
8098 a global symbol, we can not know the symbol index until we have
8099 finished examining all the local symbols to see which ones we are
8100 going to output. To deal with this, we keep the relocations in
8101 memory, and don't output them until the end of the link. This is
8102 an unfortunate waste of memory, but I don't see a good way around
8103 it. Fortunately, it only happens when performing a relocatable
8104 link, which is not the common case. FIXME: If keep_memory is set
8105 we could write the relocs out and then read them again; I don't
8106 know how bad the memory loss will be. */
8108 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8109 sub
->output_has_begun
= FALSE
;
8110 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8112 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
8114 if (p
->type
== bfd_indirect_link_order
8115 && (bfd_get_flavour ((sub
= p
->u
.indirect
.section
->owner
))
8116 == bfd_target_elf_flavour
)
8117 && elf_elfheader (sub
)->e_ident
[EI_CLASS
] == bed
->s
->elfclass
)
8119 if (! sub
->output_has_begun
)
8121 if (! elf_link_input_bfd (&finfo
, sub
))
8123 sub
->output_has_begun
= TRUE
;
8126 else if (p
->type
== bfd_section_reloc_link_order
8127 || p
->type
== bfd_symbol_reloc_link_order
)
8129 if (! elf_reloc_link_order (abfd
, info
, o
, p
))
8134 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
8140 /* Output any global symbols that got converted to local in a
8141 version script or due to symbol visibility. We do this in a
8142 separate step since ELF requires all local symbols to appear
8143 prior to any global symbols. FIXME: We should only do this if
8144 some global symbols were, in fact, converted to become local.
8145 FIXME: Will this work correctly with the Irix 5 linker? */
8146 eoinfo
.failed
= FALSE
;
8147 eoinfo
.finfo
= &finfo
;
8148 eoinfo
.localsyms
= TRUE
;
8149 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8154 /* That wrote out all the local symbols. Finish up the symbol table
8155 with the global symbols. Even if we want to strip everything we
8156 can, we still need to deal with those global symbols that got
8157 converted to local in a version script. */
8159 /* The sh_info field records the index of the first non local symbol. */
8160 symtab_hdr
->sh_info
= bfd_get_symcount (abfd
);
8163 && finfo
.dynsym_sec
->output_section
!= bfd_abs_section_ptr
)
8165 Elf_Internal_Sym sym
;
8166 bfd_byte
*dynsym
= finfo
.dynsym_sec
->contents
;
8167 long last_local
= 0;
8169 /* Write out the section symbols for the output sections. */
8176 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8179 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8185 dynindx
= elf_section_data (s
)->dynindx
;
8188 indx
= elf_section_data (s
)->this_idx
;
8189 BFD_ASSERT (indx
> 0);
8190 sym
.st_shndx
= indx
;
8191 sym
.st_value
= s
->vma
;
8192 dest
= dynsym
+ dynindx
* bed
->s
->sizeof_sym
;
8193 if (last_local
< dynindx
)
8194 last_local
= dynindx
;
8195 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8199 /* Write out the local dynsyms. */
8200 if (elf_hash_table (info
)->dynlocal
)
8202 struct elf_link_local_dynamic_entry
*e
;
8203 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
8208 sym
.st_size
= e
->isym
.st_size
;
8209 sym
.st_other
= e
->isym
.st_other
;
8211 /* Copy the internal symbol as is.
8212 Note that we saved a word of storage and overwrote
8213 the original st_name with the dynstr_index. */
8216 if (e
->isym
.st_shndx
!= SHN_UNDEF
8217 && (e
->isym
.st_shndx
< SHN_LORESERVE
8218 || e
->isym
.st_shndx
> SHN_HIRESERVE
))
8220 s
= bfd_section_from_elf_index (e
->input_bfd
,
8224 elf_section_data (s
->output_section
)->this_idx
;
8225 sym
.st_value
= (s
->output_section
->vma
8227 + e
->isym
.st_value
);
8230 if (last_local
< e
->dynindx
)
8231 last_local
= e
->dynindx
;
8233 dest
= dynsym
+ e
->dynindx
* bed
->s
->sizeof_sym
;
8234 bed
->s
->swap_symbol_out (abfd
, &sym
, dest
, 0);
8238 elf_section_data (finfo
.dynsym_sec
->output_section
)->this_hdr
.sh_info
=
8242 /* We get the global symbols from the hash table. */
8243 eoinfo
.failed
= FALSE
;
8244 eoinfo
.localsyms
= FALSE
;
8245 eoinfo
.finfo
= &finfo
;
8246 elf_link_hash_traverse (elf_hash_table (info
), elf_link_output_extsym
,
8251 /* If backend needs to output some symbols not present in the hash
8252 table, do it now. */
8253 if (bed
->elf_backend_output_arch_syms
)
8255 typedef bfd_boolean (*out_sym_func
)
8256 (void *, const char *, Elf_Internal_Sym
*, asection
*,
8257 struct elf_link_hash_entry
*);
8259 if (! ((*bed
->elf_backend_output_arch_syms
)
8260 (abfd
, info
, &finfo
, (out_sym_func
) elf_link_output_sym
)))
8264 /* Flush all symbols to the file. */
8265 if (! elf_link_flush_output_syms (&finfo
, bed
))
8268 /* Now we know the size of the symtab section. */
8269 off
+= symtab_hdr
->sh_size
;
8271 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
8272 if (symtab_shndx_hdr
->sh_name
!= 0)
8274 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8275 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8276 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8277 amt
= bfd_get_symcount (abfd
) * sizeof (Elf_External_Sym_Shndx
);
8278 symtab_shndx_hdr
->sh_size
= amt
;
8280 off
= _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr
,
8283 if (bfd_seek (abfd
, symtab_shndx_hdr
->sh_offset
, SEEK_SET
) != 0
8284 || (bfd_bwrite (finfo
.symshndxbuf
, amt
, abfd
) != amt
))
8289 /* Finish up and write out the symbol string table (.strtab)
8291 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8292 /* sh_name was set in prep_headers. */
8293 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8294 symstrtab_hdr
->sh_flags
= 0;
8295 symstrtab_hdr
->sh_addr
= 0;
8296 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (finfo
.symstrtab
);
8297 symstrtab_hdr
->sh_entsize
= 0;
8298 symstrtab_hdr
->sh_link
= 0;
8299 symstrtab_hdr
->sh_info
= 0;
8300 /* sh_offset is set just below. */
8301 symstrtab_hdr
->sh_addralign
= 1;
8303 off
= _bfd_elf_assign_file_position_for_section (symstrtab_hdr
, off
, TRUE
);
8304 elf_tdata (abfd
)->next_file_pos
= off
;
8306 if (bfd_get_symcount (abfd
) > 0)
8308 if (bfd_seek (abfd
, symstrtab_hdr
->sh_offset
, SEEK_SET
) != 0
8309 || ! _bfd_stringtab_emit (abfd
, finfo
.symstrtab
))
8313 /* Adjust the relocs to have the correct symbol indices. */
8314 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8316 if ((o
->flags
& SEC_RELOC
) == 0)
8319 elf_link_adjust_relocs (abfd
, &elf_section_data (o
)->rel_hdr
,
8320 elf_section_data (o
)->rel_count
,
8321 elf_section_data (o
)->rel_hashes
);
8322 if (elf_section_data (o
)->rel_hdr2
!= NULL
)
8323 elf_link_adjust_relocs (abfd
, elf_section_data (o
)->rel_hdr2
,
8324 elf_section_data (o
)->rel_count2
,
8325 (elf_section_data (o
)->rel_hashes
8326 + elf_section_data (o
)->rel_count
));
8328 /* Set the reloc_count field to 0 to prevent write_relocs from
8329 trying to swap the relocs out itself. */
8333 if (dynamic
&& info
->combreloc
&& dynobj
!= NULL
)
8334 relativecount
= elf_link_sort_relocs (abfd
, info
, &reldyn
);
8336 /* If we are linking against a dynamic object, or generating a
8337 shared library, finish up the dynamic linking information. */
8340 bfd_byte
*dyncon
, *dynconend
;
8342 /* Fix up .dynamic entries. */
8343 o
= bfd_get_section_by_name (dynobj
, ".dynamic");
8344 BFD_ASSERT (o
!= NULL
);
8346 dyncon
= o
->contents
;
8347 dynconend
= o
->contents
+ o
->size
;
8348 for (; dyncon
< dynconend
; dyncon
+= bed
->s
->sizeof_dyn
)
8350 Elf_Internal_Dyn dyn
;
8354 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
8361 if (relativecount
> 0 && dyncon
+ bed
->s
->sizeof_dyn
< dynconend
)
8363 switch (elf_section_data (reldyn
)->this_hdr
.sh_type
)
8365 case SHT_REL
: dyn
.d_tag
= DT_RELCOUNT
; break;
8366 case SHT_RELA
: dyn
.d_tag
= DT_RELACOUNT
; break;
8369 dyn
.d_un
.d_val
= relativecount
;
8376 name
= info
->init_function
;
8379 name
= info
->fini_function
;
8382 struct elf_link_hash_entry
*h
;
8384 h
= elf_link_hash_lookup (elf_hash_table (info
), name
,
8385 FALSE
, FALSE
, TRUE
);
8387 && (h
->root
.type
== bfd_link_hash_defined
8388 || h
->root
.type
== bfd_link_hash_defweak
))
8390 dyn
.d_un
.d_val
= h
->root
.u
.def
.value
;
8391 o
= h
->root
.u
.def
.section
;
8392 if (o
->output_section
!= NULL
)
8393 dyn
.d_un
.d_val
+= (o
->output_section
->vma
8394 + o
->output_offset
);
8397 /* The symbol is imported from another shared
8398 library and does not apply to this one. */
8406 case DT_PREINIT_ARRAYSZ
:
8407 name
= ".preinit_array";
8409 case DT_INIT_ARRAYSZ
:
8410 name
= ".init_array";
8412 case DT_FINI_ARRAYSZ
:
8413 name
= ".fini_array";
8415 o
= bfd_get_section_by_name (abfd
, name
);
8418 (*_bfd_error_handler
)
8419 (_("%B: could not find output section %s"), abfd
, name
);
8423 (*_bfd_error_handler
)
8424 (_("warning: %s section has zero size"), name
);
8425 dyn
.d_un
.d_val
= o
->size
;
8428 case DT_PREINIT_ARRAY
:
8429 name
= ".preinit_array";
8432 name
= ".init_array";
8435 name
= ".fini_array";
8448 name
= ".gnu.version_d";
8451 name
= ".gnu.version_r";
8454 name
= ".gnu.version";
8456 o
= bfd_get_section_by_name (abfd
, name
);
8459 (*_bfd_error_handler
)
8460 (_("%B: could not find output section %s"), abfd
, name
);
8463 dyn
.d_un
.d_ptr
= o
->vma
;
8470 if (dyn
.d_tag
== DT_REL
|| dyn
.d_tag
== DT_RELSZ
)
8475 for (i
= 1; i
< elf_numsections (abfd
); i
++)
8477 Elf_Internal_Shdr
*hdr
;
8479 hdr
= elf_elfsections (abfd
)[i
];
8480 if (hdr
->sh_type
== type
8481 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
8483 if (dyn
.d_tag
== DT_RELSZ
|| dyn
.d_tag
== DT_RELASZ
)
8484 dyn
.d_un
.d_val
+= hdr
->sh_size
;
8487 if (dyn
.d_un
.d_val
== 0
8488 || hdr
->sh_addr
< dyn
.d_un
.d_val
)
8489 dyn
.d_un
.d_val
= hdr
->sh_addr
;
8495 bed
->s
->swap_dyn_out (dynobj
, &dyn
, dyncon
);
8499 /* If we have created any dynamic sections, then output them. */
8502 if (! (*bed
->elf_backend_finish_dynamic_sections
) (abfd
, info
))
8505 for (o
= dynobj
->sections
; o
!= NULL
; o
= o
->next
)
8507 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
8509 || o
->output_section
== bfd_abs_section_ptr
)
8511 if ((o
->flags
& SEC_LINKER_CREATED
) == 0)
8513 /* At this point, we are only interested in sections
8514 created by _bfd_elf_link_create_dynamic_sections. */
8517 if (elf_hash_table (info
)->stab_info
.stabstr
== o
)
8519 if (elf_hash_table (info
)->eh_info
.hdr_sec
== o
)
8521 if ((elf_section_data (o
->output_section
)->this_hdr
.sh_type
8523 || strcmp (bfd_get_section_name (abfd
, o
), ".dynstr") != 0)
8525 if (! bfd_set_section_contents (abfd
, o
->output_section
,
8527 (file_ptr
) o
->output_offset
,
8533 /* The contents of the .dynstr section are actually in a
8535 off
= elf_section_data (o
->output_section
)->this_hdr
.sh_offset
;
8536 if (bfd_seek (abfd
, off
, SEEK_SET
) != 0
8537 || ! _bfd_elf_strtab_emit (abfd
,
8538 elf_hash_table (info
)->dynstr
))
8544 if (info
->relocatable
)
8546 bfd_boolean failed
= FALSE
;
8548 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
8553 /* If we have optimized stabs strings, output them. */
8554 if (elf_hash_table (info
)->stab_info
.stabstr
!= NULL
)
8556 if (! _bfd_write_stab_strings (abfd
, &elf_hash_table (info
)->stab_info
))
8560 if (info
->eh_frame_hdr
)
8562 if (! _bfd_elf_write_section_eh_frame_hdr (abfd
, info
))
8566 if (finfo
.symstrtab
!= NULL
)
8567 _bfd_stringtab_free (finfo
.symstrtab
);
8568 if (finfo
.contents
!= NULL
)
8569 free (finfo
.contents
);
8570 if (finfo
.external_relocs
!= NULL
)
8571 free (finfo
.external_relocs
);
8572 if (finfo
.internal_relocs
!= NULL
)
8573 free (finfo
.internal_relocs
);
8574 if (finfo
.external_syms
!= NULL
)
8575 free (finfo
.external_syms
);
8576 if (finfo
.locsym_shndx
!= NULL
)
8577 free (finfo
.locsym_shndx
);
8578 if (finfo
.internal_syms
!= NULL
)
8579 free (finfo
.internal_syms
);
8580 if (finfo
.indices
!= NULL
)
8581 free (finfo
.indices
);
8582 if (finfo
.sections
!= NULL
)
8583 free (finfo
.sections
);
8584 if (finfo
.symbuf
!= NULL
)
8585 free (finfo
.symbuf
);
8586 if (finfo
.symshndxbuf
!= NULL
)
8587 free (finfo
.symshndxbuf
);
8588 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8590 if ((o
->flags
& SEC_RELOC
) != 0
8591 && elf_section_data (o
)->rel_hashes
!= NULL
)
8592 free (elf_section_data (o
)->rel_hashes
);
8595 elf_tdata (abfd
)->linker
= TRUE
;
8600 if (finfo
.symstrtab
!= NULL
)
8601 _bfd_stringtab_free (finfo
.symstrtab
);
8602 if (finfo
.contents
!= NULL
)
8603 free (finfo
.contents
);
8604 if (finfo
.external_relocs
!= NULL
)
8605 free (finfo
.external_relocs
);
8606 if (finfo
.internal_relocs
!= NULL
)
8607 free (finfo
.internal_relocs
);
8608 if (finfo
.external_syms
!= NULL
)
8609 free (finfo
.external_syms
);
8610 if (finfo
.locsym_shndx
!= NULL
)
8611 free (finfo
.locsym_shndx
);
8612 if (finfo
.internal_syms
!= NULL
)
8613 free (finfo
.internal_syms
);
8614 if (finfo
.indices
!= NULL
)
8615 free (finfo
.indices
);
8616 if (finfo
.sections
!= NULL
)
8617 free (finfo
.sections
);
8618 if (finfo
.symbuf
!= NULL
)
8619 free (finfo
.symbuf
);
8620 if (finfo
.symshndxbuf
!= NULL
)
8621 free (finfo
.symshndxbuf
);
8622 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
8624 if ((o
->flags
& SEC_RELOC
) != 0
8625 && elf_section_data (o
)->rel_hashes
!= NULL
)
8626 free (elf_section_data (o
)->rel_hashes
);
8632 /* Garbage collect unused sections. */
8634 /* The mark phase of garbage collection. For a given section, mark
8635 it and any sections in this section's group, and all the sections
8636 which define symbols to which it refers. */
8638 typedef asection
* (*gc_mark_hook_fn
)
8639 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8640 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
8643 _bfd_elf_gc_mark (struct bfd_link_info
*info
,
8645 gc_mark_hook_fn gc_mark_hook
)
8648 asection
*group_sec
;
8652 /* Mark all the sections in the group. */
8653 group_sec
= elf_section_data (sec
)->next_in_group
;
8654 if (group_sec
&& !group_sec
->gc_mark
)
8655 if (!_bfd_elf_gc_mark (info
, group_sec
, gc_mark_hook
))
8658 /* Look through the section relocs. */
8660 if ((sec
->flags
& SEC_RELOC
) != 0 && sec
->reloc_count
> 0)
8662 Elf_Internal_Rela
*relstart
, *rel
, *relend
;
8663 Elf_Internal_Shdr
*symtab_hdr
;
8664 struct elf_link_hash_entry
**sym_hashes
;
8667 bfd
*input_bfd
= sec
->owner
;
8668 const struct elf_backend_data
*bed
= get_elf_backend_data (input_bfd
);
8669 Elf_Internal_Sym
*isym
= NULL
;
8672 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
8673 sym_hashes
= elf_sym_hashes (input_bfd
);
8675 /* Read the local symbols. */
8676 if (elf_bad_symtab (input_bfd
))
8678 nlocsyms
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
8682 extsymoff
= nlocsyms
= symtab_hdr
->sh_info
;
8684 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
8685 if (isym
== NULL
&& nlocsyms
!= 0)
8687 isym
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, nlocsyms
, 0,
8693 /* Read the relocations. */
8694 relstart
= _bfd_elf_link_read_relocs (input_bfd
, sec
, NULL
, NULL
,
8696 if (relstart
== NULL
)
8701 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8703 if (bed
->s
->arch_size
== 32)
8708 for (rel
= relstart
; rel
< relend
; rel
++)
8710 unsigned long r_symndx
;
8712 struct elf_link_hash_entry
*h
;
8714 r_symndx
= rel
->r_info
>> r_sym_shift
;
8718 if (r_symndx
>= nlocsyms
8719 || ELF_ST_BIND (isym
[r_symndx
].st_info
) != STB_LOCAL
)
8721 h
= sym_hashes
[r_symndx
- extsymoff
];
8722 while (h
->root
.type
== bfd_link_hash_indirect
8723 || h
->root
.type
== bfd_link_hash_warning
)
8724 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8725 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, h
, NULL
);
8729 rsec
= (*gc_mark_hook
) (sec
, info
, rel
, NULL
, &isym
[r_symndx
]);
8732 if (rsec
&& !rsec
->gc_mark
)
8734 if (bfd_get_flavour (rsec
->owner
) != bfd_target_elf_flavour
)
8736 else if (!_bfd_elf_gc_mark (info
, rsec
, gc_mark_hook
))
8745 if (elf_section_data (sec
)->relocs
!= relstart
)
8748 if (isym
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isym
)
8750 if (! info
->keep_memory
)
8753 symtab_hdr
->contents
= (unsigned char *) isym
;
8760 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8763 elf_gc_sweep_symbol (struct elf_link_hash_entry
*h
, void *idxptr
)
8767 if (h
->root
.type
== bfd_link_hash_warning
)
8768 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8770 if (h
->dynindx
!= -1
8771 && ((h
->root
.type
!= bfd_link_hash_defined
8772 && h
->root
.type
!= bfd_link_hash_defweak
)
8773 || h
->root
.u
.def
.section
->gc_mark
))
8774 h
->dynindx
= (*idx
)++;
8779 /* The sweep phase of garbage collection. Remove all garbage sections. */
8781 typedef bfd_boolean (*gc_sweep_hook_fn
)
8782 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
8785 elf_gc_sweep (struct bfd_link_info
*info
, gc_sweep_hook_fn gc_sweep_hook
)
8789 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
8793 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
8796 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
8798 /* Keep debug and special sections. */
8799 if ((o
->flags
& (SEC_DEBUGGING
| SEC_LINKER_CREATED
)) != 0
8800 || (o
->flags
& (SEC_ALLOC
| SEC_LOAD
)) == 0)
8806 /* Skip sweeping sections already excluded. */
8807 if (o
->flags
& SEC_EXCLUDE
)
8810 /* Since this is early in the link process, it is simple
8811 to remove a section from the output. */
8812 o
->flags
|= SEC_EXCLUDE
;
8814 /* But we also have to update some of the relocation
8815 info we collected before. */
8817 && (o
->flags
& SEC_RELOC
) && o
->reloc_count
> 0)
8819 Elf_Internal_Rela
*internal_relocs
;
8823 = _bfd_elf_link_read_relocs (o
->owner
, o
, NULL
, NULL
,
8825 if (internal_relocs
== NULL
)
8828 r
= (*gc_sweep_hook
) (o
->owner
, info
, o
, internal_relocs
);
8830 if (elf_section_data (o
)->relocs
!= internal_relocs
)
8831 free (internal_relocs
);
8839 /* Remove the symbols that were in the swept sections from the dynamic
8840 symbol table. GCFIXME: Anyone know how to get them out of the
8841 static symbol table as well? */
8845 elf_link_hash_traverse (elf_hash_table (info
), elf_gc_sweep_symbol
, &i
);
8847 elf_hash_table (info
)->dynsymcount
= i
;
8853 /* Propagate collected vtable information. This is called through
8854 elf_link_hash_traverse. */
8857 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry
*h
, void *okp
)
8859 if (h
->root
.type
== bfd_link_hash_warning
)
8860 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8862 /* Those that are not vtables. */
8863 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8866 /* Those vtables that do not have parents, we cannot merge. */
8867 if (h
->vtable
->parent
== (struct elf_link_hash_entry
*) -1)
8870 /* If we've already been done, exit. */
8871 if (h
->vtable
->used
&& h
->vtable
->used
[-1])
8874 /* Make sure the parent's table is up to date. */
8875 elf_gc_propagate_vtable_entries_used (h
->vtable
->parent
, okp
);
8877 if (h
->vtable
->used
== NULL
)
8879 /* None of this table's entries were referenced. Re-use the
8881 h
->vtable
->used
= h
->vtable
->parent
->vtable
->used
;
8882 h
->vtable
->size
= h
->vtable
->parent
->vtable
->size
;
8887 bfd_boolean
*cu
, *pu
;
8889 /* Or the parent's entries into ours. */
8890 cu
= h
->vtable
->used
;
8892 pu
= h
->vtable
->parent
->vtable
->used
;
8895 const struct elf_backend_data
*bed
;
8896 unsigned int log_file_align
;
8898 bed
= get_elf_backend_data (h
->root
.u
.def
.section
->owner
);
8899 log_file_align
= bed
->s
->log_file_align
;
8900 n
= h
->vtable
->parent
->vtable
->size
>> log_file_align
;
8915 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry
*h
, void *okp
)
8918 bfd_vma hstart
, hend
;
8919 Elf_Internal_Rela
*relstart
, *relend
, *rel
;
8920 const struct elf_backend_data
*bed
;
8921 unsigned int log_file_align
;
8923 if (h
->root
.type
== bfd_link_hash_warning
)
8924 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8926 /* Take care of both those symbols that do not describe vtables as
8927 well as those that are not loaded. */
8928 if (h
->vtable
== NULL
|| h
->vtable
->parent
== NULL
)
8931 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
8932 || h
->root
.type
== bfd_link_hash_defweak
);
8934 sec
= h
->root
.u
.def
.section
;
8935 hstart
= h
->root
.u
.def
.value
;
8936 hend
= hstart
+ h
->size
;
8938 relstart
= _bfd_elf_link_read_relocs (sec
->owner
, sec
, NULL
, NULL
, TRUE
);
8940 return *(bfd_boolean
*) okp
= FALSE
;
8941 bed
= get_elf_backend_data (sec
->owner
);
8942 log_file_align
= bed
->s
->log_file_align
;
8944 relend
= relstart
+ sec
->reloc_count
* bed
->s
->int_rels_per_ext_rel
;
8946 for (rel
= relstart
; rel
< relend
; ++rel
)
8947 if (rel
->r_offset
>= hstart
&& rel
->r_offset
< hend
)
8949 /* If the entry is in use, do nothing. */
8951 && (rel
->r_offset
- hstart
) < h
->vtable
->size
)
8953 bfd_vma entry
= (rel
->r_offset
- hstart
) >> log_file_align
;
8954 if (h
->vtable
->used
[entry
])
8957 /* Otherwise, kill it. */
8958 rel
->r_offset
= rel
->r_info
= rel
->r_addend
= 0;
8964 /* Mark sections containing dynamically referenced symbols. This is called
8965 through elf_link_hash_traverse. */
8968 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry
*h
,
8969 void *okp ATTRIBUTE_UNUSED
)
8971 if (h
->root
.type
== bfd_link_hash_warning
)
8972 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
8974 if ((h
->root
.type
== bfd_link_hash_defined
8975 || h
->root
.type
== bfd_link_hash_defweak
)
8977 h
->root
.u
.def
.section
->flags
|= SEC_KEEP
;
8982 /* Do mark and sweep of unused sections. */
8985 bfd_elf_gc_sections (bfd
*abfd
, struct bfd_link_info
*info
)
8987 bfd_boolean ok
= TRUE
;
8989 asection
* (*gc_mark_hook
)
8990 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
8991 struct elf_link_hash_entry
*h
, Elf_Internal_Sym
*);
8993 if (!get_elf_backend_data (abfd
)->can_gc_sections
8994 || info
->relocatable
8995 || info
->emitrelocations
8997 || !is_elf_hash_table (info
->hash
))
8999 (*_bfd_error_handler
)(_("Warning: gc-sections option ignored"));
9003 /* Apply transitive closure to the vtable entry usage info. */
9004 elf_link_hash_traverse (elf_hash_table (info
),
9005 elf_gc_propagate_vtable_entries_used
,
9010 /* Kill the vtable relocations that were not used. */
9011 elf_link_hash_traverse (elf_hash_table (info
),
9012 elf_gc_smash_unused_vtentry_relocs
,
9017 /* Mark dynamically referenced symbols. */
9018 if (elf_hash_table (info
)->dynamic_sections_created
)
9019 elf_link_hash_traverse (elf_hash_table (info
),
9020 elf_gc_mark_dynamic_ref_symbol
,
9025 /* Grovel through relocs to find out who stays ... */
9026 gc_mark_hook
= get_elf_backend_data (abfd
)->gc_mark_hook
;
9027 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
9031 if (bfd_get_flavour (sub
) != bfd_target_elf_flavour
)
9034 for (o
= sub
->sections
; o
!= NULL
; o
= o
->next
)
9036 if (o
->flags
& SEC_KEEP
)
9038 /* _bfd_elf_discard_section_eh_frame knows how to discard
9039 orphaned FDEs so don't mark sections referenced by the
9040 EH frame section. */
9041 if (strcmp (o
->name
, ".eh_frame") == 0)
9043 else if (!_bfd_elf_gc_mark (info
, o
, gc_mark_hook
))
9049 /* ... and mark SEC_EXCLUDE for those that go. */
9050 if (!elf_gc_sweep (info
, get_elf_backend_data (abfd
)->gc_sweep_hook
))
9056 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9059 bfd_elf_gc_record_vtinherit (bfd
*abfd
,
9061 struct elf_link_hash_entry
*h
,
9064 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
9065 struct elf_link_hash_entry
**search
, *child
;
9066 bfd_size_type extsymcount
;
9067 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9069 /* The sh_info field of the symtab header tells us where the
9070 external symbols start. We don't care about the local symbols at
9072 extsymcount
= elf_tdata (abfd
)->symtab_hdr
.sh_size
/ bed
->s
->sizeof_sym
;
9073 if (!elf_bad_symtab (abfd
))
9074 extsymcount
-= elf_tdata (abfd
)->symtab_hdr
.sh_info
;
9076 sym_hashes
= elf_sym_hashes (abfd
);
9077 sym_hashes_end
= sym_hashes
+ extsymcount
;
9079 /* Hunt down the child symbol, which is in this section at the same
9080 offset as the relocation. */
9081 for (search
= sym_hashes
; search
!= sym_hashes_end
; ++search
)
9083 if ((child
= *search
) != NULL
9084 && (child
->root
.type
== bfd_link_hash_defined
9085 || child
->root
.type
== bfd_link_hash_defweak
)
9086 && child
->root
.u
.def
.section
== sec
9087 && child
->root
.u
.def
.value
== offset
)
9091 (*_bfd_error_handler
) ("%B: %A+%lu: No symbol found for INHERIT",
9092 abfd
, sec
, (unsigned long) offset
);
9093 bfd_set_error (bfd_error_invalid_operation
);
9099 child
->vtable
= bfd_zalloc (abfd
, sizeof (*child
->vtable
));
9105 /* This *should* only be the absolute section. It could potentially
9106 be that someone has defined a non-global vtable though, which
9107 would be bad. It isn't worth paging in the local symbols to be
9108 sure though; that case should simply be handled by the assembler. */
9110 child
->vtable
->parent
= (struct elf_link_hash_entry
*) -1;
9113 child
->vtable
->parent
= h
;
9118 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9121 bfd_elf_gc_record_vtentry (bfd
*abfd ATTRIBUTE_UNUSED
,
9122 asection
*sec ATTRIBUTE_UNUSED
,
9123 struct elf_link_hash_entry
*h
,
9126 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9127 unsigned int log_file_align
= bed
->s
->log_file_align
;
9131 h
->vtable
= bfd_zalloc (abfd
, sizeof (*h
->vtable
));
9136 if (addend
>= h
->vtable
->size
)
9138 size_t size
, bytes
, file_align
;
9139 bfd_boolean
*ptr
= h
->vtable
->used
;
9141 /* While the symbol is undefined, we have to be prepared to handle
9143 file_align
= 1 << log_file_align
;
9144 if (h
->root
.type
== bfd_link_hash_undefined
)
9145 size
= addend
+ file_align
;
9151 /* Oops! We've got a reference past the defined end of
9152 the table. This is probably a bug -- shall we warn? */
9153 size
= addend
+ file_align
;
9156 size
= (size
+ file_align
- 1) & -file_align
;
9158 /* Allocate one extra entry for use as a "done" flag for the
9159 consolidation pass. */
9160 bytes
= ((size
>> log_file_align
) + 1) * sizeof (bfd_boolean
);
9164 ptr
= bfd_realloc (ptr
- 1, bytes
);
9170 oldbytes
= (((h
->vtable
->size
>> log_file_align
) + 1)
9171 * sizeof (bfd_boolean
));
9172 memset (((char *) ptr
) + oldbytes
, 0, bytes
- oldbytes
);
9176 ptr
= bfd_zmalloc (bytes
);
9181 /* And arrange for that done flag to be at index -1. */
9182 h
->vtable
->used
= ptr
+ 1;
9183 h
->vtable
->size
= size
;
9186 h
->vtable
->used
[addend
>> log_file_align
] = TRUE
;
9191 struct alloc_got_off_arg
{
9193 unsigned int got_elt_size
;
9196 /* We need a special top-level link routine to convert got reference counts
9197 to real got offsets. */
9200 elf_gc_allocate_got_offsets (struct elf_link_hash_entry
*h
, void *arg
)
9202 struct alloc_got_off_arg
*gofarg
= arg
;
9204 if (h
->root
.type
== bfd_link_hash_warning
)
9205 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9207 if (h
->got
.refcount
> 0)
9209 h
->got
.offset
= gofarg
->gotoff
;
9210 gofarg
->gotoff
+= gofarg
->got_elt_size
;
9213 h
->got
.offset
= (bfd_vma
) -1;
9218 /* And an accompanying bit to work out final got entry offsets once
9219 we're done. Should be called from final_link. */
9222 bfd_elf_gc_common_finalize_got_offsets (bfd
*abfd
,
9223 struct bfd_link_info
*info
)
9226 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9228 unsigned int got_elt_size
= bed
->s
->arch_size
/ 8;
9229 struct alloc_got_off_arg gofarg
;
9231 if (! is_elf_hash_table (info
->hash
))
9234 /* The GOT offset is relative to the .got section, but the GOT header is
9235 put into the .got.plt section, if the backend uses it. */
9236 if (bed
->want_got_plt
)
9239 gotoff
= bed
->got_header_size
;
9241 /* Do the local .got entries first. */
9242 for (i
= info
->input_bfds
; i
; i
= i
->link_next
)
9244 bfd_signed_vma
*local_got
;
9245 bfd_size_type j
, locsymcount
;
9246 Elf_Internal_Shdr
*symtab_hdr
;
9248 if (bfd_get_flavour (i
) != bfd_target_elf_flavour
)
9251 local_got
= elf_local_got_refcounts (i
);
9255 symtab_hdr
= &elf_tdata (i
)->symtab_hdr
;
9256 if (elf_bad_symtab (i
))
9257 locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9259 locsymcount
= symtab_hdr
->sh_info
;
9261 for (j
= 0; j
< locsymcount
; ++j
)
9263 if (local_got
[j
] > 0)
9265 local_got
[j
] = gotoff
;
9266 gotoff
+= got_elt_size
;
9269 local_got
[j
] = (bfd_vma
) -1;
9273 /* Then the global .got entries. .plt refcounts are handled by
9274 adjust_dynamic_symbol */
9275 gofarg
.gotoff
= gotoff
;
9276 gofarg
.got_elt_size
= got_elt_size
;
9277 elf_link_hash_traverse (elf_hash_table (info
),
9278 elf_gc_allocate_got_offsets
,
9283 /* Many folk need no more in the way of final link than this, once
9284 got entry reference counting is enabled. */
9287 bfd_elf_gc_common_final_link (bfd
*abfd
, struct bfd_link_info
*info
)
9289 if (!bfd_elf_gc_common_finalize_got_offsets (abfd
, info
))
9292 /* Invoke the regular ELF backend linker to do all the work. */
9293 return bfd_elf_final_link (abfd
, info
);
9297 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset
, void *cookie
)
9299 struct elf_reloc_cookie
*rcookie
= cookie
;
9301 if (rcookie
->bad_symtab
)
9302 rcookie
->rel
= rcookie
->rels
;
9304 for (; rcookie
->rel
< rcookie
->relend
; rcookie
->rel
++)
9306 unsigned long r_symndx
;
9308 if (! rcookie
->bad_symtab
)
9309 if (rcookie
->rel
->r_offset
> offset
)
9311 if (rcookie
->rel
->r_offset
!= offset
)
9314 r_symndx
= rcookie
->rel
->r_info
>> rcookie
->r_sym_shift
;
9315 if (r_symndx
== SHN_UNDEF
)
9318 if (r_symndx
>= rcookie
->locsymcount
9319 || ELF_ST_BIND (rcookie
->locsyms
[r_symndx
].st_info
) != STB_LOCAL
)
9321 struct elf_link_hash_entry
*h
;
9323 h
= rcookie
->sym_hashes
[r_symndx
- rcookie
->extsymoff
];
9325 while (h
->root
.type
== bfd_link_hash_indirect
9326 || h
->root
.type
== bfd_link_hash_warning
)
9327 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
9329 if ((h
->root
.type
== bfd_link_hash_defined
9330 || h
->root
.type
== bfd_link_hash_defweak
)
9331 && elf_discarded_section (h
->root
.u
.def
.section
))
9338 /* It's not a relocation against a global symbol,
9339 but it could be a relocation against a local
9340 symbol for a discarded section. */
9342 Elf_Internal_Sym
*isym
;
9344 /* Need to: get the symbol; get the section. */
9345 isym
= &rcookie
->locsyms
[r_symndx
];
9346 if (isym
->st_shndx
< SHN_LORESERVE
|| isym
->st_shndx
> SHN_HIRESERVE
)
9348 isec
= bfd_section_from_elf_index (rcookie
->abfd
, isym
->st_shndx
);
9349 if (isec
!= NULL
&& elf_discarded_section (isec
))
9358 /* Discard unneeded references to discarded sections.
9359 Returns TRUE if any section's size was changed. */
9360 /* This function assumes that the relocations are in sorted order,
9361 which is true for all known assemblers. */
9364 bfd_elf_discard_info (bfd
*output_bfd
, struct bfd_link_info
*info
)
9366 struct elf_reloc_cookie cookie
;
9367 asection
*stab
, *eh
;
9368 Elf_Internal_Shdr
*symtab_hdr
;
9369 const struct elf_backend_data
*bed
;
9372 bfd_boolean ret
= FALSE
;
9374 if (info
->traditional_format
9375 || !is_elf_hash_table (info
->hash
))
9378 for (abfd
= info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link_next
)
9380 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
9383 bed
= get_elf_backend_data (abfd
);
9385 if ((abfd
->flags
& DYNAMIC
) != 0)
9388 eh
= bfd_get_section_by_name (abfd
, ".eh_frame");
9389 if (info
->relocatable
9392 || bfd_is_abs_section (eh
->output_section
))))
9395 stab
= bfd_get_section_by_name (abfd
, ".stab");
9398 || bfd_is_abs_section (stab
->output_section
)
9399 || stab
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
9404 && bed
->elf_backend_discard_info
== NULL
)
9407 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
9409 cookie
.sym_hashes
= elf_sym_hashes (abfd
);
9410 cookie
.bad_symtab
= elf_bad_symtab (abfd
);
9411 if (cookie
.bad_symtab
)
9413 cookie
.locsymcount
= symtab_hdr
->sh_size
/ bed
->s
->sizeof_sym
;
9414 cookie
.extsymoff
= 0;
9418 cookie
.locsymcount
= symtab_hdr
->sh_info
;
9419 cookie
.extsymoff
= symtab_hdr
->sh_info
;
9422 if (bed
->s
->arch_size
== 32)
9423 cookie
.r_sym_shift
= 8;
9425 cookie
.r_sym_shift
= 32;
9427 cookie
.locsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
9428 if (cookie
.locsyms
== NULL
&& cookie
.locsymcount
!= 0)
9430 cookie
.locsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
9431 cookie
.locsymcount
, 0,
9433 if (cookie
.locsyms
== NULL
)
9440 count
= stab
->reloc_count
;
9442 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, stab
, NULL
, NULL
,
9444 if (cookie
.rels
!= NULL
)
9446 cookie
.rel
= cookie
.rels
;
9447 cookie
.relend
= cookie
.rels
;
9448 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9449 if (_bfd_discard_section_stabs (abfd
, stab
,
9450 elf_section_data (stab
)->sec_info
,
9451 bfd_elf_reloc_symbol_deleted_p
,
9454 if (elf_section_data (stab
)->relocs
!= cookie
.rels
)
9462 count
= eh
->reloc_count
;
9464 cookie
.rels
= _bfd_elf_link_read_relocs (abfd
, eh
, NULL
, NULL
,
9466 cookie
.rel
= cookie
.rels
;
9467 cookie
.relend
= cookie
.rels
;
9468 if (cookie
.rels
!= NULL
)
9469 cookie
.relend
+= count
* bed
->s
->int_rels_per_ext_rel
;
9471 if (_bfd_elf_discard_section_eh_frame (abfd
, info
, eh
,
9472 bfd_elf_reloc_symbol_deleted_p
,
9476 if (cookie
.rels
!= NULL
9477 && elf_section_data (eh
)->relocs
!= cookie
.rels
)
9481 if (bed
->elf_backend_discard_info
!= NULL
9482 && (*bed
->elf_backend_discard_info
) (abfd
, &cookie
, info
))
9485 if (cookie
.locsyms
!= NULL
9486 && symtab_hdr
->contents
!= (unsigned char *) cookie
.locsyms
)
9488 if (! info
->keep_memory
)
9489 free (cookie
.locsyms
);
9491 symtab_hdr
->contents
= (unsigned char *) cookie
.locsyms
;
9495 if (info
->eh_frame_hdr
9496 && !info
->relocatable
9497 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd
, info
))
9504 _bfd_elf_section_already_linked (bfd
*abfd
, struct bfd_section
* sec
)
9507 const char *name
, *p
;
9508 struct bfd_section_already_linked
*l
;
9509 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
9512 /* A single member comdat group section may be discarded by a
9513 linkonce section. See below. */
9514 if (sec
->output_section
== bfd_abs_section_ptr
)
9519 /* Check if it belongs to a section group. */
9520 group
= elf_sec_group (sec
);
9522 /* Return if it isn't a linkonce section nor a member of a group. A
9523 comdat group section also has SEC_LINK_ONCE set. */
9524 if ((flags
& SEC_LINK_ONCE
) == 0 && group
== NULL
)
9529 /* If this is the member of a single member comdat group, check if
9530 the group should be discarded. */
9531 if (elf_next_in_group (sec
) == sec
9532 && (group
->flags
& SEC_LINK_ONCE
) != 0)
9538 /* FIXME: When doing a relocatable link, we may have trouble
9539 copying relocations in other sections that refer to local symbols
9540 in the section being discarded. Those relocations will have to
9541 be converted somehow; as of this writing I'm not sure that any of
9542 the backends handle that correctly.
9544 It is tempting to instead not discard link once sections when
9545 doing a relocatable link (technically, they should be discarded
9546 whenever we are building constructors). However, that fails,
9547 because the linker winds up combining all the link once sections
9548 into a single large link once section, which defeats the purpose
9549 of having link once sections in the first place.
9551 Also, not merging link once sections in a relocatable link
9552 causes trouble for MIPS ELF, which relies on link once semantics
9553 to handle the .reginfo section correctly. */
9555 name
= bfd_get_section_name (abfd
, sec
);
9557 if (strncmp (name
, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9558 && (p
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
9563 already_linked_list
= bfd_section_already_linked_table_lookup (p
);
9565 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9567 /* We may have 3 different sections on the list: group section,
9568 comdat section and linkonce section. SEC may be a linkonce or
9569 group section. We match a group section with a group section,
9570 a linkonce section with a linkonce section, and ignore comdat
9572 if ((flags
& SEC_GROUP
) == (l
->sec
->flags
& SEC_GROUP
)
9573 && strcmp (name
, l
->sec
->name
) == 0
9574 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
)
9576 /* The section has already been linked. See if we should
9578 switch (flags
& SEC_LINK_DUPLICATES
)
9583 case SEC_LINK_DUPLICATES_DISCARD
:
9586 case SEC_LINK_DUPLICATES_ONE_ONLY
:
9587 (*_bfd_error_handler
)
9588 (_("%B: ignoring duplicate section `%A'\n"),
9592 case SEC_LINK_DUPLICATES_SAME_SIZE
:
9593 if (sec
->size
!= l
->sec
->size
)
9594 (*_bfd_error_handler
)
9595 (_("%B: duplicate section `%A' has different size\n"),
9599 case SEC_LINK_DUPLICATES_SAME_CONTENTS
:
9600 if (sec
->size
!= l
->sec
->size
)
9601 (*_bfd_error_handler
)
9602 (_("%B: duplicate section `%A' has different size\n"),
9604 else if (sec
->size
!= 0)
9606 bfd_byte
*sec_contents
, *l_sec_contents
;
9608 if (!bfd_malloc_and_get_section (abfd
, sec
, &sec_contents
))
9609 (*_bfd_error_handler
)
9610 (_("%B: warning: could not read contents of section `%A'\n"),
9612 else if (!bfd_malloc_and_get_section (l
->sec
->owner
, l
->sec
,
9614 (*_bfd_error_handler
)
9615 (_("%B: warning: could not read contents of section `%A'\n"),
9616 l
->sec
->owner
, l
->sec
);
9617 else if (memcmp (sec_contents
, l_sec_contents
, sec
->size
) != 0)
9618 (*_bfd_error_handler
)
9619 (_("%B: warning: duplicate section `%A' has different contents\n"),
9623 free (sec_contents
);
9625 free (l_sec_contents
);
9630 /* Set the output_section field so that lang_add_section
9631 does not create a lang_input_section structure for this
9632 section. Since there might be a symbol in the section
9633 being discarded, we must retain a pointer to the section
9634 which we are really going to use. */
9635 sec
->output_section
= bfd_abs_section_ptr
;
9636 sec
->kept_section
= l
->sec
;
9638 if (flags
& SEC_GROUP
)
9640 asection
*first
= elf_next_in_group (sec
);
9641 asection
*s
= first
;
9645 s
->output_section
= bfd_abs_section_ptr
;
9646 /* Record which group discards it. */
9647 s
->kept_section
= l
->sec
;
9648 s
= elf_next_in_group (s
);
9649 /* These lists are circular. */
9661 /* If this is the member of a single member comdat group and the
9662 group hasn't be discarded, we check if it matches a linkonce
9663 section. We only record the discarded comdat group. Otherwise
9664 the undiscarded group will be discarded incorrectly later since
9665 itself has been recorded. */
9666 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9667 if ((l
->sec
->flags
& SEC_GROUP
) == 0
9668 && bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
) == NULL
9669 && bfd_elf_match_symbols_in_sections (l
->sec
,
9670 elf_next_in_group (sec
)))
9672 elf_next_in_group (sec
)->output_section
= bfd_abs_section_ptr
;
9673 elf_next_in_group (sec
)->kept_section
= l
->sec
;
9674 group
->output_section
= bfd_abs_section_ptr
;
9681 /* There is no direct match. But for linkonce section, we should
9682 check if there is a match with comdat group member. We always
9683 record the linkonce section, discarded or not. */
9684 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
9685 if (l
->sec
->flags
& SEC_GROUP
)
9687 asection
*first
= elf_next_in_group (l
->sec
);
9690 && elf_next_in_group (first
) == first
9691 && bfd_elf_match_symbols_in_sections (first
, sec
))
9693 sec
->output_section
= bfd_abs_section_ptr
;
9694 sec
->kept_section
= l
->sec
;
9699 /* This is the first section with this name. Record it. */
9700 bfd_section_already_linked_table_insert (already_linked_list
, sec
);