1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 /* Define a symbol in a dynamic linkage section. */
32 struct elf_link_hash_entry *
33 _bfd_elf_define_linkage_sym (bfd *abfd,
34 struct bfd_link_info *info,
38 struct elf_link_hash_entry *h;
39 struct bfd_link_hash_entry *bh;
40 const struct elf_backend_data *bed;
42 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
45 /* Zap symbol defined in an as-needed lib that wasn't linked.
46 This is a symptom of a larger problem: Absolute symbols
47 defined in shared libraries can't be overridden, because we
48 lose the link to the bfd which is via the symbol section. */
49 h->root.type = bfd_link_hash_new;
53 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
55 get_elf_backend_data (abfd)->collect,
58 h = (struct elf_link_hash_entry *) bh;
61 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
63 bed = get_elf_backend_data (abfd);
64 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
69 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
73 struct elf_link_hash_entry *h;
74 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
77 /* This function may be called more than once. */
78 s = bfd_get_section_by_name (abfd, ".got");
79 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
82 switch (bed->s->arch_size)
93 bfd_set_error (bfd_error_bad_value);
97 flags = bed->dynamic_sec_flags;
99 s = bfd_make_section_with_flags (abfd, ".got", flags);
101 || !bfd_set_section_alignment (abfd, s, ptralign))
104 if (bed->want_got_plt)
106 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
108 || !bfd_set_section_alignment (abfd, s, ptralign))
112 if (bed->want_got_sym)
114 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
115 (or .got.plt) section. We don't do this in the linker script
116 because we don't want to define the symbol if we are not creating
117 a global offset table. */
118 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
119 elf_hash_table (info)->hgot = h;
124 /* The first bit of the global offset table is the header. */
125 s->size += bed->got_header_size;
130 /* Create a strtab to hold the dynamic symbol names. */
132 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
134 struct elf_link_hash_table *hash_table;
136 hash_table = elf_hash_table (info);
137 if (hash_table->dynobj == NULL)
138 hash_table->dynobj = abfd;
140 if (hash_table->dynstr == NULL)
142 hash_table->dynstr = _bfd_elf_strtab_init ();
143 if (hash_table->dynstr == NULL)
149 /* Create some sections which will be filled in with dynamic linking
150 information. ABFD is an input file which requires dynamic sections
151 to be created. The dynamic sections take up virtual memory space
152 when the final executable is run, so we need to create them before
153 addresses are assigned to the output sections. We work out the
154 actual contents and size of these sections later. */
157 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
160 register asection *s;
161 const struct elf_backend_data *bed;
163 if (! is_elf_hash_table (info->hash))
166 if (elf_hash_table (info)->dynamic_sections_created)
169 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
172 abfd = elf_hash_table (info)->dynobj;
173 bed = get_elf_backend_data (abfd);
175 flags = bed->dynamic_sec_flags;
177 /* A dynamically linked executable has a .interp section, but a
178 shared library does not. */
179 if (info->executable)
181 s = bfd_make_section_with_flags (abfd, ".interp",
182 flags | SEC_READONLY);
187 if (! info->traditional_format)
189 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr",
190 flags | SEC_READONLY);
192 || ! bfd_set_section_alignment (abfd, s, 2))
194 elf_hash_table (info)->eh_info.hdr_sec = s;
197 /* Create sections to hold version informations. These are removed
198 if they are not needed. */
199 s = bfd_make_section_with_flags (abfd, ".gnu.version_d",
200 flags | SEC_READONLY);
202 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
205 s = bfd_make_section_with_flags (abfd, ".gnu.version",
206 flags | SEC_READONLY);
208 || ! bfd_set_section_alignment (abfd, s, 1))
211 s = bfd_make_section_with_flags (abfd, ".gnu.version_r",
212 flags | SEC_READONLY);
214 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
217 s = bfd_make_section_with_flags (abfd, ".dynsym",
218 flags | SEC_READONLY);
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
223 s = bfd_make_section_with_flags (abfd, ".dynstr",
224 flags | SEC_READONLY);
228 s = bfd_make_section_with_flags (abfd, ".dynamic", flags);
230 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
233 /* The special symbol _DYNAMIC is always set to the start of the
234 .dynamic section. We could set _DYNAMIC in a linker script, but we
235 only want to define it if we are, in fact, creating a .dynamic
236 section. We don't want to define it if there is no .dynamic
237 section, since on some ELF platforms the start up code examines it
238 to decide how to initialize the process. */
239 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
242 s = bfd_make_section_with_flags (abfd, ".hash",
243 flags | SEC_READONLY);
245 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
247 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
249 /* Let the backend create the rest of the sections. This lets the
250 backend set the right flags. The backend will normally create
251 the .got and .plt sections. */
252 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
255 elf_hash_table (info)->dynamic_sections_created = TRUE;
260 /* Create dynamic sections when linking against a dynamic object. */
263 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
265 flagword flags, pltflags;
267 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
269 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
270 .rel[a].bss sections. */
271 flags = bed->dynamic_sec_flags;
274 if (bed->plt_not_loaded)
275 /* We do not clear SEC_ALLOC here because we still want the OS to
276 allocate space for the section; it's just that there's nothing
277 to read in from the object file. */
278 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
280 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
281 if (bed->plt_readonly)
282 pltflags |= SEC_READONLY;
284 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
286 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
289 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
291 if (bed->want_plt_sym
292 && !_bfd_elf_define_linkage_sym (abfd, info, s,
293 "_PROCEDURE_LINKAGE_TABLE_"))
296 s = bfd_make_section_with_flags (abfd,
297 (bed->default_use_rela_p
298 ? ".rela.plt" : ".rel.plt"),
299 flags | SEC_READONLY);
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 if (! _bfd_elf_create_got_section (abfd, info))
307 if (bed->want_dynbss)
309 /* The .dynbss section is a place to put symbols which are defined
310 by dynamic objects, are referenced by regular objects, and are
311 not functions. We must allocate space for them in the process
312 image and use a R_*_COPY reloc to tell the dynamic linker to
313 initialize them at run time. The linker script puts the .dynbss
314 section into the .bss section of the final image. */
315 s = bfd_make_section_with_flags (abfd, ".dynbss",
317 | SEC_LINKER_CREATED));
321 /* The .rel[a].bss section holds copy relocs. This section is not
322 normally needed. We need to create it here, though, so that the
323 linker will map it to an output section. We can't just create it
324 only if we need it, because we will not know whether we need it
325 until we have seen all the input files, and the first time the
326 main linker code calls BFD after examining all the input files
327 (size_dynamic_sections) the input sections have already been
328 mapped to the output sections. If the section turns out not to
329 be needed, we can discard it later. We will never need this
330 section when generating a shared object, since they do not use
334 s = bfd_make_section_with_flags (abfd,
335 (bed->default_use_rela_p
336 ? ".rela.bss" : ".rel.bss"),
337 flags | SEC_READONLY);
339 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
347 /* Record a new dynamic symbol. We record the dynamic symbols as we
348 read the input files, since we need to have a list of all of them
349 before we can determine the final sizes of the output sections.
350 Note that we may actually call this function even though we are not
351 going to output any dynamic symbols; in some cases we know that a
352 symbol should be in the dynamic symbol table, but only if there is
356 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
357 struct elf_link_hash_entry *h)
359 if (h->dynindx == -1)
361 struct elf_strtab_hash *dynstr;
366 /* XXX: The ABI draft says the linker must turn hidden and
367 internal symbols into STB_LOCAL symbols when producing the
368 DSO. However, if ld.so honors st_other in the dynamic table,
369 this would not be necessary. */
370 switch (ELF_ST_VISIBILITY (h->other))
374 if (h->root.type != bfd_link_hash_undefined
375 && h->root.type != bfd_link_hash_undefweak)
378 if (!elf_hash_table (info)->is_relocatable_executable)
386 h->dynindx = elf_hash_table (info)->dynsymcount;
387 ++elf_hash_table (info)->dynsymcount;
389 dynstr = elf_hash_table (info)->dynstr;
392 /* Create a strtab to hold the dynamic symbol names. */
393 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
398 /* We don't put any version information in the dynamic string
400 name = h->root.root.string;
401 p = strchr (name, ELF_VER_CHR);
403 /* We know that the p points into writable memory. In fact,
404 there are only a few symbols that have read-only names, being
405 those like _GLOBAL_OFFSET_TABLE_ that are created specially
406 by the backends. Most symbols will have names pointing into
407 an ELF string table read from a file, or to objalloc memory. */
410 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
415 if (indx == (bfd_size_type) -1)
417 h->dynstr_index = indx;
423 /* Record an assignment to a symbol made by a linker script. We need
424 this in case some dynamic object refers to this symbol. */
427 bfd_elf_record_link_assignment (bfd *output_bfd,
428 struct bfd_link_info *info,
433 struct elf_link_hash_entry *h;
434 struct elf_link_hash_table *htab;
436 if (!is_elf_hash_table (info->hash))
439 htab = elf_hash_table (info);
440 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
444 /* Since we're defining the symbol, don't let it seem to have not
445 been defined. record_dynamic_symbol and size_dynamic_sections
446 may depend on this. */
447 if (h->root.type == bfd_link_hash_undefweak
448 || h->root.type == bfd_link_hash_undefined)
450 h->root.type = bfd_link_hash_new;
451 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
452 bfd_link_repair_undef_list (&htab->root);
455 if (h->root.type == bfd_link_hash_new)
458 /* If this symbol is being provided by the linker script, and it is
459 currently defined by a dynamic object, but not by a regular
460 object, then mark it as undefined so that the generic linker will
461 force the correct value. */
465 h->root.type = bfd_link_hash_undefined;
467 /* If this symbol is not being provided by the linker script, and it is
468 currently defined by a dynamic object, but not by a regular object,
469 then clear out any version information because the symbol will not be
470 associated with the dynamic object any more. */
474 h->verinfo.verdef = NULL;
478 if (provide && hidden)
480 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
482 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
483 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
486 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
488 if (!info->relocatable
490 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
491 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
497 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
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,
710 struct bfd_link_info *info,
711 unsigned long *section_sym_count)
713 unsigned long dynsymcount = 0;
715 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
717 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
719 for (p = output_bfd->sections; p ; p = p->next)
720 if ((p->flags & SEC_EXCLUDE) == 0
721 && (p->flags & SEC_ALLOC) != 0
722 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
723 elf_section_data (p)->dynindx = ++dynsymcount;
725 *section_sym_count = dynsymcount;
727 elf_link_hash_traverse (elf_hash_table (info),
728 elf_link_renumber_local_hash_table_dynsyms,
731 if (elf_hash_table (info)->dynlocal)
733 struct elf_link_local_dynamic_entry *p;
734 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
735 p->dynindx = ++dynsymcount;
738 elf_link_hash_traverse (elf_hash_table (info),
739 elf_link_renumber_hash_table_dynsyms,
742 /* There is an unused NULL entry at the head of the table which
743 we must account for in our count. Unless there weren't any
744 symbols, which means we'll have no table at all. */
745 if (dynsymcount != 0)
748 elf_hash_table (info)->dynsymcount = dynsymcount;
752 /* This function is called when we want to define a new symbol. It
753 handles the various cases which arise when we find a definition in
754 a dynamic object, or when there is already a definition in a
755 dynamic object. The new symbol is described by NAME, SYM, PSEC,
756 and PVALUE. We set SYM_HASH to the hash table entry. We set
757 OVERRIDE if the old symbol is overriding a new definition. We set
758 TYPE_CHANGE_OK if it is OK for the type to change. We set
759 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
760 change, we mean that we shouldn't warn if the type or size does
761 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
762 object is overridden by a regular object. */
765 _bfd_elf_merge_symbol (bfd *abfd,
766 struct bfd_link_info *info,
768 Elf_Internal_Sym *sym,
771 unsigned int *pold_alignment,
772 struct elf_link_hash_entry **sym_hash,
774 bfd_boolean *override,
775 bfd_boolean *type_change_ok,
776 bfd_boolean *size_change_ok)
778 asection *sec, *oldsec;
779 struct elf_link_hash_entry *h;
780 struct elf_link_hash_entry *flip;
783 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
784 bfd_boolean newweak, oldweak;
785 const struct elf_backend_data *bed;
791 bind = ELF_ST_BIND (sym->st_info);
793 if (! bfd_is_und_section (sec))
794 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
796 h = ((struct elf_link_hash_entry *)
797 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
802 /* This code is for coping with dynamic objects, and is only useful
803 if we are doing an ELF link. */
804 if (info->hash->creator != abfd->xvec)
807 /* For merging, we only care about real symbols. */
809 while (h->root.type == bfd_link_hash_indirect
810 || h->root.type == bfd_link_hash_warning)
811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
813 /* If we just created the symbol, mark it as being an ELF symbol.
814 Other than that, there is nothing to do--there is no merge issue
815 with a newly defined symbol--so we just return. */
817 if (h->root.type == bfd_link_hash_new)
823 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
826 switch (h->root.type)
833 case bfd_link_hash_undefined:
834 case bfd_link_hash_undefweak:
835 oldbfd = h->root.u.undef.abfd;
839 case bfd_link_hash_defined:
840 case bfd_link_hash_defweak:
841 oldbfd = h->root.u.def.section->owner;
842 oldsec = h->root.u.def.section;
845 case bfd_link_hash_common:
846 oldbfd = h->root.u.c.p->section->owner;
847 oldsec = h->root.u.c.p->section;
851 /* In cases involving weak versioned symbols, we may wind up trying
852 to merge a symbol with itself. Catch that here, to avoid the
853 confusion that results if we try to override a symbol with
854 itself. The additional tests catch cases like
855 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
856 dynamic object, which we do want to handle here. */
858 && ((abfd->flags & DYNAMIC) == 0
862 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
863 respectively, is from a dynamic object. */
865 newdyn = (abfd->flags & DYNAMIC) != 0;
869 olddyn = (oldbfd->flags & DYNAMIC) != 0;
870 else if (oldsec != NULL)
872 /* This handles the special SHN_MIPS_{TEXT,DATA} section
873 indices used by MIPS ELF. */
874 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
877 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
878 respectively, appear to be a definition rather than reference. */
880 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
882 olddef = (h->root.type != bfd_link_hash_undefined
883 && h->root.type != bfd_link_hash_undefweak
884 && h->root.type != bfd_link_hash_common);
886 /* Check TLS symbol. We don't check undefined symbol introduced by
888 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
889 && ELF_ST_TYPE (sym->st_info) != h->type
893 bfd_boolean ntdef, tdef;
894 asection *ntsec, *tsec;
896 if (h->type == STT_TLS)
916 (*_bfd_error_handler)
917 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
918 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
919 else if (!tdef && !ntdef)
920 (*_bfd_error_handler)
921 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
922 tbfd, ntbfd, h->root.root.string);
924 (*_bfd_error_handler)
925 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
926 tbfd, tsec, ntbfd, h->root.root.string);
928 (*_bfd_error_handler)
929 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
930 tbfd, ntbfd, ntsec, h->root.root.string);
932 bfd_set_error (bfd_error_bad_value);
936 /* We need to remember if a symbol has a definition in a dynamic
937 object or is weak in all dynamic objects. Internal and hidden
938 visibility will make it unavailable to dynamic objects. */
939 if (newdyn && !h->dynamic_def)
941 if (!bfd_is_und_section (sec))
945 /* Check if this symbol is weak in all dynamic objects. If it
946 is the first time we see it in a dynamic object, we mark
947 if it is weak. Otherwise, we clear it. */
950 if (bind == STB_WEAK)
953 else if (bind != STB_WEAK)
958 /* If the old symbol has non-default visibility, we ignore the new
959 definition from a dynamic object. */
961 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
962 && !bfd_is_und_section (sec))
965 /* Make sure this symbol is dynamic. */
967 /* A protected symbol has external availability. Make sure it is
970 FIXME: Should we check type and size for protected symbol? */
971 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
972 return bfd_elf_link_record_dynamic_symbol (info, h);
977 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
980 /* If the new symbol with non-default visibility comes from a
981 relocatable file and the old definition comes from a dynamic
982 object, we remove the old definition. */
983 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
986 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
987 && bfd_is_und_section (sec))
989 /* If the new symbol is undefined and the old symbol was
990 also undefined before, we need to make sure
991 _bfd_generic_link_add_one_symbol doesn't mess
992 up the linker hash table undefs list. Since the old
993 definition came from a dynamic object, it is still on the
995 h->root.type = bfd_link_hash_undefined;
996 h->root.u.undef.abfd = abfd;
1000 h->root.type = bfd_link_hash_new;
1001 h->root.u.undef.abfd = NULL;
1010 /* FIXME: Should we check type and size for protected symbol? */
1016 /* Differentiate strong and weak symbols. */
1017 newweak = bind == STB_WEAK;
1018 oldweak = (h->root.type == bfd_link_hash_defweak
1019 || h->root.type == bfd_link_hash_undefweak);
1021 /* If a new weak symbol definition comes from a regular file and the
1022 old symbol comes from a dynamic library, we treat the new one as
1023 strong. Similarly, an old weak symbol definition from a regular
1024 file is treated as strong when the new symbol comes from a dynamic
1025 library. Further, an old weak symbol from a dynamic library is
1026 treated as strong if the new symbol is from a dynamic library.
1027 This reflects the way glibc's ld.so works.
1029 Do this before setting *type_change_ok or *size_change_ok so that
1030 we warn properly when dynamic library symbols are overridden. */
1032 if (newdef && !newdyn && olddyn)
1034 if (olddef && newdyn)
1037 /* It's OK to change the type if either the existing symbol or the
1038 new symbol is weak. A type change is also OK if the old symbol
1039 is undefined and the new symbol is defined. */
1044 && h->root.type == bfd_link_hash_undefined))
1045 *type_change_ok = TRUE;
1047 /* It's OK to change the size if either the existing symbol or the
1048 new symbol is weak, or if the old symbol is undefined. */
1051 || h->root.type == bfd_link_hash_undefined)
1052 *size_change_ok = TRUE;
1054 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1055 symbol, respectively, appears to be a common symbol in a dynamic
1056 object. If a symbol appears in an uninitialized section, and is
1057 not weak, and is not a function, then it may be a common symbol
1058 which was resolved when the dynamic object was created. We want
1059 to treat such symbols specially, because they raise special
1060 considerations when setting the symbol size: if the symbol
1061 appears as a common symbol in a regular object, and the size in
1062 the regular object is larger, we must make sure that we use the
1063 larger size. This problematic case can always be avoided in C,
1064 but it must be handled correctly when using Fortran shared
1067 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1068 likewise for OLDDYNCOMMON and OLDDEF.
1070 Note that this test is just a heuristic, and that it is quite
1071 possible to have an uninitialized symbol in a shared object which
1072 is really a definition, rather than a common symbol. This could
1073 lead to some minor confusion when the symbol really is a common
1074 symbol in some regular object. However, I think it will be
1080 && (sec->flags & SEC_ALLOC) != 0
1081 && (sec->flags & SEC_LOAD) == 0
1083 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1084 newdyncommon = TRUE;
1086 newdyncommon = FALSE;
1090 && h->root.type == bfd_link_hash_defined
1092 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1093 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1095 && h->type != STT_FUNC)
1096 olddyncommon = TRUE;
1098 olddyncommon = FALSE;
1100 /* We now know everything about the old and new symbols. We ask the
1101 backend to check if we can merge them. */
1102 bed = get_elf_backend_data (abfd);
1103 if (bed->merge_symbol
1104 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1105 pold_alignment, skip, override,
1106 type_change_ok, size_change_ok,
1107 &newdyn, &newdef, &newdyncommon, &newweak,
1109 &olddyn, &olddef, &olddyncommon, &oldweak,
1113 /* If both the old and the new symbols look like common symbols in a
1114 dynamic object, set the size of the symbol to the larger of the
1119 && sym->st_size != h->size)
1121 /* Since we think we have two common symbols, issue a multiple
1122 common warning if desired. Note that we only warn if the
1123 size is different. If the size is the same, we simply let
1124 the old symbol override the new one as normally happens with
1125 symbols defined in dynamic objects. */
1127 if (! ((*info->callbacks->multiple_common)
1128 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1129 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1132 if (sym->st_size > h->size)
1133 h->size = sym->st_size;
1135 *size_change_ok = TRUE;
1138 /* If we are looking at a dynamic object, and we have found a
1139 definition, we need to see if the symbol was already defined by
1140 some other object. If so, we want to use the existing
1141 definition, and we do not want to report a multiple symbol
1142 definition error; we do this by clobbering *PSEC to be
1143 bfd_und_section_ptr.
1145 We treat a common symbol as a definition if the symbol in the
1146 shared library is a function, since common symbols always
1147 represent variables; this can cause confusion in principle, but
1148 any such confusion would seem to indicate an erroneous program or
1149 shared library. We also permit a common symbol in a regular
1150 object to override a weak symbol in a shared object. */
1155 || (h->root.type == bfd_link_hash_common
1157 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1161 newdyncommon = FALSE;
1163 *psec = sec = bfd_und_section_ptr;
1164 *size_change_ok = TRUE;
1166 /* If we get here when the old symbol is a common symbol, then
1167 we are explicitly letting it override a weak symbol or
1168 function in a dynamic object, and we don't want to warn about
1169 a type change. If the old symbol is a defined symbol, a type
1170 change warning may still be appropriate. */
1172 if (h->root.type == bfd_link_hash_common)
1173 *type_change_ok = TRUE;
1176 /* Handle the special case of an old common symbol merging with a
1177 new symbol which looks like a common symbol in a shared object.
1178 We change *PSEC and *PVALUE to make the new symbol look like a
1179 common symbol, and let _bfd_generic_link_add_one_symbol do the
1183 && h->root.type == bfd_link_hash_common)
1187 newdyncommon = FALSE;
1188 *pvalue = sym->st_size;
1189 *psec = sec = bed->common_section (oldsec);
1190 *size_change_ok = TRUE;
1193 /* Skip weak definitions of symbols that are already defined. */
1194 if (newdef && olddef && newweak)
1197 /* If the old symbol is from a dynamic object, and the new symbol is
1198 a definition which is not from a dynamic object, then the new
1199 symbol overrides the old symbol. Symbols from regular files
1200 always take precedence over symbols from dynamic objects, even if
1201 they are defined after the dynamic object in the link.
1203 As above, we again permit a common symbol in a regular object to
1204 override a definition in a shared object if the shared object
1205 symbol is a function or is weak. */
1210 || (bfd_is_com_section (sec)
1212 || h->type == STT_FUNC)))
1217 /* Change the hash table entry to undefined, and let
1218 _bfd_generic_link_add_one_symbol do the right thing with the
1221 h->root.type = bfd_link_hash_undefined;
1222 h->root.u.undef.abfd = h->root.u.def.section->owner;
1223 *size_change_ok = TRUE;
1226 olddyncommon = FALSE;
1228 /* We again permit a type change when a common symbol may be
1229 overriding a function. */
1231 if (bfd_is_com_section (sec))
1232 *type_change_ok = TRUE;
1234 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1237 /* This union may have been set to be non-NULL when this symbol
1238 was seen in a dynamic object. We must force the union to be
1239 NULL, so that it is correct for a regular symbol. */
1240 h->verinfo.vertree = NULL;
1243 /* Handle the special case of a new common symbol merging with an
1244 old symbol that looks like it might be a common symbol defined in
1245 a shared object. Note that we have already handled the case in
1246 which a new common symbol should simply override the definition
1247 in the shared library. */
1250 && bfd_is_com_section (sec)
1253 /* It would be best if we could set the hash table entry to a
1254 common symbol, but we don't know what to use for the section
1255 or the alignment. */
1256 if (! ((*info->callbacks->multiple_common)
1257 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1258 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1261 /* If the presumed common symbol in the dynamic object is
1262 larger, pretend that the new symbol has its size. */
1264 if (h->size > *pvalue)
1267 /* We need to remember the alignment required by the symbol
1268 in the dynamic object. */
1269 BFD_ASSERT (pold_alignment);
1270 *pold_alignment = h->root.u.def.section->alignment_power;
1273 olddyncommon = FALSE;
1275 h->root.type = bfd_link_hash_undefined;
1276 h->root.u.undef.abfd = h->root.u.def.section->owner;
1278 *size_change_ok = TRUE;
1279 *type_change_ok = TRUE;
1281 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1284 h->verinfo.vertree = NULL;
1289 /* Handle the case where we had a versioned symbol in a dynamic
1290 library and now find a definition in a normal object. In this
1291 case, we make the versioned symbol point to the normal one. */
1292 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1293 flip->root.type = h->root.type;
1294 h->root.type = bfd_link_hash_indirect;
1295 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1296 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1297 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1301 flip->ref_dynamic = 1;
1308 /* This function is called to create an indirect symbol from the
1309 default for the symbol with the default version if needed. The
1310 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1311 set DYNSYM if the new indirect symbol is dynamic. */
1314 _bfd_elf_add_default_symbol (bfd *abfd,
1315 struct bfd_link_info *info,
1316 struct elf_link_hash_entry *h,
1318 Elf_Internal_Sym *sym,
1321 bfd_boolean *dynsym,
1322 bfd_boolean override)
1324 bfd_boolean type_change_ok;
1325 bfd_boolean size_change_ok;
1328 struct elf_link_hash_entry *hi;
1329 struct bfd_link_hash_entry *bh;
1330 const struct elf_backend_data *bed;
1331 bfd_boolean collect;
1332 bfd_boolean dynamic;
1334 size_t len, shortlen;
1337 /* If this symbol has a version, and it is the default version, we
1338 create an indirect symbol from the default name to the fully
1339 decorated name. This will cause external references which do not
1340 specify a version to be bound to this version of the symbol. */
1341 p = strchr (name, ELF_VER_CHR);
1342 if (p == NULL || p[1] != ELF_VER_CHR)
1347 /* We are overridden by an old definition. We need to check if we
1348 need to create the indirect symbol from the default name. */
1349 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1351 BFD_ASSERT (hi != NULL);
1354 while (hi->root.type == bfd_link_hash_indirect
1355 || hi->root.type == bfd_link_hash_warning)
1357 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1363 bed = get_elf_backend_data (abfd);
1364 collect = bed->collect;
1365 dynamic = (abfd->flags & DYNAMIC) != 0;
1367 shortlen = p - name;
1368 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1369 if (shortname == NULL)
1371 memcpy (shortname, name, shortlen);
1372 shortname[shortlen] = '\0';
1374 /* We are going to create a new symbol. Merge it with any existing
1375 symbol with this name. For the purposes of the merge, act as
1376 though we were defining the symbol we just defined, although we
1377 actually going to define an indirect symbol. */
1378 type_change_ok = FALSE;
1379 size_change_ok = FALSE;
1381 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1382 NULL, &hi, &skip, &override,
1383 &type_change_ok, &size_change_ok))
1392 if (! (_bfd_generic_link_add_one_symbol
1393 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1394 0, name, FALSE, collect, &bh)))
1396 hi = (struct elf_link_hash_entry *) bh;
1400 /* In this case the symbol named SHORTNAME is overriding the
1401 indirect symbol we want to add. We were planning on making
1402 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1403 is the name without a version. NAME is the fully versioned
1404 name, and it is the default version.
1406 Overriding means that we already saw a definition for the
1407 symbol SHORTNAME in a regular object, and it is overriding
1408 the symbol defined in the dynamic object.
1410 When this happens, we actually want to change NAME, the
1411 symbol we just added, to refer to SHORTNAME. This will cause
1412 references to NAME in the shared object to become references
1413 to SHORTNAME in the regular object. This is what we expect
1414 when we override a function in a shared object: that the
1415 references in the shared object will be mapped to the
1416 definition in the regular object. */
1418 while (hi->root.type == bfd_link_hash_indirect
1419 || hi->root.type == bfd_link_hash_warning)
1420 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1422 h->root.type = bfd_link_hash_indirect;
1423 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 hi->ref_dynamic = 1;
1431 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1436 /* Now set HI to H, so that the following code will set the
1437 other fields correctly. */
1441 /* If there is a duplicate definition somewhere, then HI may not
1442 point to an indirect symbol. We will have reported an error to
1443 the user in that case. */
1445 if (hi->root.type == bfd_link_hash_indirect)
1447 struct elf_link_hash_entry *ht;
1449 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1450 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1452 /* See if the new flags lead us to realize that the symbol must
1464 if (hi->ref_regular)
1470 /* We also need to define an indirection from the nondefault version
1474 len = strlen (name);
1475 shortname = bfd_hash_allocate (&info->hash->table, len);
1476 if (shortname == NULL)
1478 memcpy (shortname, name, shortlen);
1479 memcpy (shortname + shortlen, p + 1, len - shortlen);
1481 /* Once again, merge with any existing symbol. */
1482 type_change_ok = FALSE;
1483 size_change_ok = FALSE;
1485 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1486 NULL, &hi, &skip, &override,
1487 &type_change_ok, &size_change_ok))
1495 /* Here SHORTNAME is a versioned name, so we don't expect to see
1496 the type of override we do in the case above unless it is
1497 overridden by a versioned definition. */
1498 if (hi->root.type != bfd_link_hash_defined
1499 && hi->root.type != bfd_link_hash_defweak)
1500 (*_bfd_error_handler)
1501 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1507 if (! (_bfd_generic_link_add_one_symbol
1508 (info, abfd, shortname, BSF_INDIRECT,
1509 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1511 hi = (struct elf_link_hash_entry *) bh;
1513 /* If there is a duplicate definition somewhere, then HI may not
1514 point to an indirect symbol. We will have reported an error
1515 to the user in that case. */
1517 if (hi->root.type == bfd_link_hash_indirect)
1519 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1521 /* See if the new flags lead us to realize that the symbol
1533 if (hi->ref_regular)
1543 /* This routine is used to export all defined symbols into the dynamic
1544 symbol table. It is called via elf_link_hash_traverse. */
1547 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1549 struct elf_info_failed *eif = data;
1551 /* Ignore indirect symbols. These are added by the versioning code. */
1552 if (h->root.type == bfd_link_hash_indirect)
1555 if (h->root.type == bfd_link_hash_warning)
1556 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1558 if (h->dynindx == -1
1562 struct bfd_elf_version_tree *t;
1563 struct bfd_elf_version_expr *d;
1565 for (t = eif->verdefs; t != NULL; t = t->next)
1567 if (t->globals.list != NULL)
1569 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1574 if (t->locals.list != NULL)
1576 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1585 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1596 /* Look through the symbols which are defined in other shared
1597 libraries and referenced here. Update the list of version
1598 dependencies. This will be put into the .gnu.version_r section.
1599 This function is called via elf_link_hash_traverse. */
1602 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1605 struct elf_find_verdep_info *rinfo = data;
1606 Elf_Internal_Verneed *t;
1607 Elf_Internal_Vernaux *a;
1610 if (h->root.type == bfd_link_hash_warning)
1611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1613 /* We only care about symbols defined in shared objects with version
1618 || h->verinfo.verdef == NULL)
1621 /* See if we already know about this version. */
1622 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1624 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1627 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1628 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1634 /* This is a new version. Add it to tree we are building. */
1639 t = bfd_zalloc (rinfo->output_bfd, amt);
1642 rinfo->failed = TRUE;
1646 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1647 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1648 elf_tdata (rinfo->output_bfd)->verref = t;
1652 a = bfd_zalloc (rinfo->output_bfd, amt);
1654 /* Note that we are copying a string pointer here, and testing it
1655 above. If bfd_elf_string_from_elf_section is ever changed to
1656 discard the string data when low in memory, this will have to be
1658 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1660 a->vna_flags = h->verinfo.verdef->vd_flags;
1661 a->vna_nextptr = t->vn_auxptr;
1663 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1666 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1673 /* Figure out appropriate versions for all the symbols. We may not
1674 have the version number script until we have read all of the input
1675 files, so until that point we don't know which symbols should be
1676 local. This function is called via elf_link_hash_traverse. */
1679 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1681 struct elf_assign_sym_version_info *sinfo;
1682 struct bfd_link_info *info;
1683 const struct elf_backend_data *bed;
1684 struct elf_info_failed eif;
1691 if (h->root.type == bfd_link_hash_warning)
1692 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1694 /* Fix the symbol flags. */
1697 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1700 sinfo->failed = TRUE;
1704 /* We only need version numbers for symbols defined in regular
1706 if (!h->def_regular)
1709 bed = get_elf_backend_data (sinfo->output_bfd);
1710 p = strchr (h->root.root.string, ELF_VER_CHR);
1711 if (p != NULL && h->verinfo.vertree == NULL)
1713 struct bfd_elf_version_tree *t;
1718 /* There are two consecutive ELF_VER_CHR characters if this is
1719 not a hidden symbol. */
1721 if (*p == ELF_VER_CHR)
1727 /* If there is no version string, we can just return out. */
1735 /* Look for the version. If we find it, it is no longer weak. */
1736 for (t = sinfo->verdefs; t != NULL; t = t->next)
1738 if (strcmp (t->name, p) == 0)
1742 struct bfd_elf_version_expr *d;
1744 len = p - h->root.root.string;
1745 alc = bfd_malloc (len);
1748 memcpy (alc, h->root.root.string, len - 1);
1749 alc[len - 1] = '\0';
1750 if (alc[len - 2] == ELF_VER_CHR)
1751 alc[len - 2] = '\0';
1753 h->verinfo.vertree = t;
1757 if (t->globals.list != NULL)
1758 d = (*t->match) (&t->globals, NULL, alc);
1760 /* See if there is anything to force this symbol to
1762 if (d == NULL && t->locals.list != NULL)
1764 d = (*t->match) (&t->locals, NULL, alc);
1767 && ! info->export_dynamic)
1768 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1776 /* If we are building an application, we need to create a
1777 version node for this version. */
1778 if (t == NULL && info->executable)
1780 struct bfd_elf_version_tree **pp;
1783 /* If we aren't going to export this symbol, we don't need
1784 to worry about it. */
1785 if (h->dynindx == -1)
1789 t = bfd_zalloc (sinfo->output_bfd, amt);
1792 sinfo->failed = TRUE;
1797 t->name_indx = (unsigned int) -1;
1801 /* Don't count anonymous version tag. */
1802 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1804 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1806 t->vernum = version_index;
1810 h->verinfo.vertree = t;
1814 /* We could not find the version for a symbol when
1815 generating a shared archive. Return an error. */
1816 (*_bfd_error_handler)
1817 (_("%B: undefined versioned symbol name %s"),
1818 sinfo->output_bfd, h->root.root.string);
1819 bfd_set_error (bfd_error_bad_value);
1820 sinfo->failed = TRUE;
1828 /* If we don't have a version for this symbol, see if we can find
1830 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1832 struct bfd_elf_version_tree *t;
1833 struct bfd_elf_version_tree *local_ver;
1834 struct bfd_elf_version_expr *d;
1836 /* See if can find what version this symbol is in. If the
1837 symbol is supposed to be local, then don't actually register
1840 for (t = sinfo->verdefs; t != NULL; t = t->next)
1842 if (t->globals.list != NULL)
1844 bfd_boolean matched;
1848 while ((d = (*t->match) (&t->globals, d,
1849 h->root.root.string)) != NULL)
1854 /* There is a version without definition. Make
1855 the symbol the default definition for this
1857 h->verinfo.vertree = t;
1865 /* There is no undefined version for this symbol. Hide the
1867 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1870 if (t->locals.list != NULL)
1873 while ((d = (*t->match) (&t->locals, d,
1874 h->root.root.string)) != NULL)
1877 /* If the match is "*", keep looking for a more
1878 explicit, perhaps even global, match.
1879 XXX: Shouldn't this be !d->wildcard instead? */
1880 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1889 if (local_ver != NULL)
1891 h->verinfo.vertree = local_ver;
1892 if (h->dynindx != -1
1893 && ! info->export_dynamic)
1895 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1903 /* Read and swap the relocs from the section indicated by SHDR. This
1904 may be either a REL or a RELA section. The relocations are
1905 translated into RELA relocations and stored in INTERNAL_RELOCS,
1906 which should have already been allocated to contain enough space.
1907 The EXTERNAL_RELOCS are a buffer where the external form of the
1908 relocations should be stored.
1910 Returns FALSE if something goes wrong. */
1913 elf_link_read_relocs_from_section (bfd *abfd,
1915 Elf_Internal_Shdr *shdr,
1916 void *external_relocs,
1917 Elf_Internal_Rela *internal_relocs)
1919 const struct elf_backend_data *bed;
1920 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1921 const bfd_byte *erela;
1922 const bfd_byte *erelaend;
1923 Elf_Internal_Rela *irela;
1924 Elf_Internal_Shdr *symtab_hdr;
1927 /* Position ourselves at the start of the section. */
1928 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1931 /* Read the relocations. */
1932 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1935 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1936 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1938 bed = get_elf_backend_data (abfd);
1940 /* Convert the external relocations to the internal format. */
1941 if (shdr->sh_entsize == bed->s->sizeof_rel)
1942 swap_in = bed->s->swap_reloc_in;
1943 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1944 swap_in = bed->s->swap_reloca_in;
1947 bfd_set_error (bfd_error_wrong_format);
1951 erela = external_relocs;
1952 erelaend = erela + shdr->sh_size;
1953 irela = internal_relocs;
1954 while (erela < erelaend)
1958 (*swap_in) (abfd, erela, irela);
1959 r_symndx = ELF32_R_SYM (irela->r_info);
1960 if (bed->s->arch_size == 64)
1962 if ((size_t) r_symndx >= nsyms)
1964 (*_bfd_error_handler)
1965 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1966 " for offset 0x%lx in section `%A'"),
1968 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1969 bfd_set_error (bfd_error_bad_value);
1972 irela += bed->s->int_rels_per_ext_rel;
1973 erela += shdr->sh_entsize;
1979 /* Read and swap the relocs for a section O. They may have been
1980 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1981 not NULL, they are used as buffers to read into. They are known to
1982 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1983 the return value is allocated using either malloc or bfd_alloc,
1984 according to the KEEP_MEMORY argument. If O has two relocation
1985 sections (both REL and RELA relocations), then the REL_HDR
1986 relocations will appear first in INTERNAL_RELOCS, followed by the
1987 REL_HDR2 relocations. */
1990 _bfd_elf_link_read_relocs (bfd *abfd,
1992 void *external_relocs,
1993 Elf_Internal_Rela *internal_relocs,
1994 bfd_boolean keep_memory)
1996 Elf_Internal_Shdr *rel_hdr;
1997 void *alloc1 = NULL;
1998 Elf_Internal_Rela *alloc2 = NULL;
1999 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2001 if (elf_section_data (o)->relocs != NULL)
2002 return elf_section_data (o)->relocs;
2004 if (o->reloc_count == 0)
2007 rel_hdr = &elf_section_data (o)->rel_hdr;
2009 if (internal_relocs == NULL)
2013 size = o->reloc_count;
2014 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2016 internal_relocs = bfd_alloc (abfd, size);
2018 internal_relocs = alloc2 = bfd_malloc (size);
2019 if (internal_relocs == NULL)
2023 if (external_relocs == NULL)
2025 bfd_size_type size = rel_hdr->sh_size;
2027 if (elf_section_data (o)->rel_hdr2)
2028 size += elf_section_data (o)->rel_hdr2->sh_size;
2029 alloc1 = bfd_malloc (size);
2032 external_relocs = alloc1;
2035 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2039 if (elf_section_data (o)->rel_hdr2
2040 && (!elf_link_read_relocs_from_section
2042 elf_section_data (o)->rel_hdr2,
2043 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2044 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2045 * bed->s->int_rels_per_ext_rel))))
2048 /* Cache the results for next time, if we can. */
2050 elf_section_data (o)->relocs = internal_relocs;
2055 /* Don't free alloc2, since if it was allocated we are passing it
2056 back (under the name of internal_relocs). */
2058 return internal_relocs;
2068 /* Compute the size of, and allocate space for, REL_HDR which is the
2069 section header for a section containing relocations for O. */
2072 _bfd_elf_link_size_reloc_section (bfd *abfd,
2073 Elf_Internal_Shdr *rel_hdr,
2076 bfd_size_type reloc_count;
2077 bfd_size_type num_rel_hashes;
2079 /* Figure out how many relocations there will be. */
2080 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2081 reloc_count = elf_section_data (o)->rel_count;
2083 reloc_count = elf_section_data (o)->rel_count2;
2085 num_rel_hashes = o->reloc_count;
2086 if (num_rel_hashes < reloc_count)
2087 num_rel_hashes = reloc_count;
2089 /* That allows us to calculate the size of the section. */
2090 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2092 /* The contents field must last into write_object_contents, so we
2093 allocate it with bfd_alloc rather than malloc. Also since we
2094 cannot be sure that the contents will actually be filled in,
2095 we zero the allocated space. */
2096 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2097 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2100 /* We only allocate one set of hash entries, so we only do it the
2101 first time we are called. */
2102 if (elf_section_data (o)->rel_hashes == NULL
2105 struct elf_link_hash_entry **p;
2107 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2111 elf_section_data (o)->rel_hashes = p;
2117 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2118 originated from the section given by INPUT_REL_HDR) to the
2122 _bfd_elf_link_output_relocs (bfd *output_bfd,
2123 asection *input_section,
2124 Elf_Internal_Shdr *input_rel_hdr,
2125 Elf_Internal_Rela *internal_relocs,
2126 struct elf_link_hash_entry **rel_hash
2129 Elf_Internal_Rela *irela;
2130 Elf_Internal_Rela *irelaend;
2132 Elf_Internal_Shdr *output_rel_hdr;
2133 asection *output_section;
2134 unsigned int *rel_countp = NULL;
2135 const struct elf_backend_data *bed;
2136 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2138 output_section = input_section->output_section;
2139 output_rel_hdr = NULL;
2141 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2142 == input_rel_hdr->sh_entsize)
2144 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2145 rel_countp = &elf_section_data (output_section)->rel_count;
2147 else if (elf_section_data (output_section)->rel_hdr2
2148 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2149 == input_rel_hdr->sh_entsize))
2151 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2152 rel_countp = &elf_section_data (output_section)->rel_count2;
2156 (*_bfd_error_handler)
2157 (_("%B: relocation size mismatch in %B section %A"),
2158 output_bfd, input_section->owner, input_section);
2159 bfd_set_error (bfd_error_wrong_object_format);
2163 bed = get_elf_backend_data (output_bfd);
2164 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2165 swap_out = bed->s->swap_reloc_out;
2166 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2167 swap_out = bed->s->swap_reloca_out;
2171 erel = output_rel_hdr->contents;
2172 erel += *rel_countp * input_rel_hdr->sh_entsize;
2173 irela = internal_relocs;
2174 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2175 * bed->s->int_rels_per_ext_rel);
2176 while (irela < irelaend)
2178 (*swap_out) (output_bfd, irela, erel);
2179 irela += bed->s->int_rels_per_ext_rel;
2180 erel += input_rel_hdr->sh_entsize;
2183 /* Bump the counter, so that we know where to add the next set of
2185 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2190 /* Fix up the flags for a symbol. This handles various cases which
2191 can only be fixed after all the input files are seen. This is
2192 currently called by both adjust_dynamic_symbol and
2193 assign_sym_version, which is unnecessary but perhaps more robust in
2194 the face of future changes. */
2197 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2198 struct elf_info_failed *eif)
2200 /* If this symbol was mentioned in a non-ELF file, try to set
2201 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2202 permit a non-ELF file to correctly refer to a symbol defined in
2203 an ELF dynamic object. */
2206 while (h->root.type == bfd_link_hash_indirect)
2207 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2209 if (h->root.type != bfd_link_hash_defined
2210 && h->root.type != bfd_link_hash_defweak)
2213 h->ref_regular_nonweak = 1;
2217 if (h->root.u.def.section->owner != NULL
2218 && (bfd_get_flavour (h->root.u.def.section->owner)
2219 == bfd_target_elf_flavour))
2222 h->ref_regular_nonweak = 1;
2228 if (h->dynindx == -1
2232 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2241 /* Unfortunately, NON_ELF is only correct if the symbol
2242 was first seen in a non-ELF file. Fortunately, if the symbol
2243 was first seen in an ELF file, we're probably OK unless the
2244 symbol was defined in a non-ELF file. Catch that case here.
2245 FIXME: We're still in trouble if the symbol was first seen in
2246 a dynamic object, and then later in a non-ELF regular object. */
2247 if ((h->root.type == bfd_link_hash_defined
2248 || h->root.type == bfd_link_hash_defweak)
2250 && (h->root.u.def.section->owner != NULL
2251 ? (bfd_get_flavour (h->root.u.def.section->owner)
2252 != bfd_target_elf_flavour)
2253 : (bfd_is_abs_section (h->root.u.def.section)
2254 && !h->def_dynamic)))
2258 /* If this is a final link, and the symbol was defined as a common
2259 symbol in a regular object file, and there was no definition in
2260 any dynamic object, then the linker will have allocated space for
2261 the symbol in a common section but the DEF_REGULAR
2262 flag will not have been set. */
2263 if (h->root.type == bfd_link_hash_defined
2267 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2270 /* If -Bsymbolic was used (which means to bind references to global
2271 symbols to the definition within the shared object), and this
2272 symbol was defined in a regular object, then it actually doesn't
2273 need a PLT entry. Likewise, if the symbol has non-default
2274 visibility. If the symbol has hidden or internal visibility, we
2275 will force it local. */
2277 && eif->info->shared
2278 && is_elf_hash_table (eif->info->hash)
2279 && (eif->info->symbolic
2280 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2283 const struct elf_backend_data *bed;
2284 bfd_boolean force_local;
2286 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2288 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2289 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2290 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2293 /* If a weak undefined symbol has non-default visibility, we also
2294 hide it from the dynamic linker. */
2295 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2296 && h->root.type == bfd_link_hash_undefweak)
2298 const struct elf_backend_data *bed;
2299 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2300 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2303 /* If this is a weak defined symbol in a dynamic object, and we know
2304 the real definition in the dynamic object, copy interesting flags
2305 over to the real definition. */
2306 if (h->u.weakdef != NULL)
2308 struct elf_link_hash_entry *weakdef;
2310 weakdef = h->u.weakdef;
2311 if (h->root.type == bfd_link_hash_indirect)
2312 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2314 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2315 || h->root.type == bfd_link_hash_defweak);
2316 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2317 || weakdef->root.type == bfd_link_hash_defweak);
2318 BFD_ASSERT (weakdef->def_dynamic);
2320 /* If the real definition is defined by a regular object file,
2321 don't do anything special. See the longer description in
2322 _bfd_elf_adjust_dynamic_symbol, below. */
2323 if (weakdef->def_regular)
2324 h->u.weakdef = NULL;
2327 const struct elf_backend_data *bed;
2329 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2330 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2337 /* Make the backend pick a good value for a dynamic symbol. This is
2338 called via elf_link_hash_traverse, and also calls itself
2342 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2344 struct elf_info_failed *eif = data;
2346 const struct elf_backend_data *bed;
2348 if (! is_elf_hash_table (eif->info->hash))
2351 if (h->root.type == bfd_link_hash_warning)
2353 h->got = elf_hash_table (eif->info)->init_got_offset;
2354 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2356 /* When warning symbols are created, they **replace** the "real"
2357 entry in the hash table, thus we never get to see the real
2358 symbol in a hash traversal. So look at it now. */
2359 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2362 /* Ignore indirect symbols. These are added by the versioning code. */
2363 if (h->root.type == bfd_link_hash_indirect)
2366 /* Fix the symbol flags. */
2367 if (! _bfd_elf_fix_symbol_flags (h, eif))
2370 /* If this symbol does not require a PLT entry, and it is not
2371 defined by a dynamic object, or is not referenced by a regular
2372 object, ignore it. We do have to handle a weak defined symbol,
2373 even if no regular object refers to it, if we decided to add it
2374 to the dynamic symbol table. FIXME: Do we normally need to worry
2375 about symbols which are defined by one dynamic object and
2376 referenced by another one? */
2381 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2383 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2387 /* If we've already adjusted this symbol, don't do it again. This
2388 can happen via a recursive call. */
2389 if (h->dynamic_adjusted)
2392 /* Don't look at this symbol again. Note that we must set this
2393 after checking the above conditions, because we may look at a
2394 symbol once, decide not to do anything, and then get called
2395 recursively later after REF_REGULAR is set below. */
2396 h->dynamic_adjusted = 1;
2398 /* If this is a weak definition, and we know a real definition, and
2399 the real symbol is not itself defined by a regular object file,
2400 then get a good value for the real definition. We handle the
2401 real symbol first, for the convenience of the backend routine.
2403 Note that there is a confusing case here. If the real definition
2404 is defined by a regular object file, we don't get the real symbol
2405 from the dynamic object, but we do get the weak symbol. If the
2406 processor backend uses a COPY reloc, then if some routine in the
2407 dynamic object changes the real symbol, we will not see that
2408 change in the corresponding weak symbol. This is the way other
2409 ELF linkers work as well, and seems to be a result of the shared
2412 I will clarify this issue. Most SVR4 shared libraries define the
2413 variable _timezone and define timezone as a weak synonym. The
2414 tzset call changes _timezone. If you write
2415 extern int timezone;
2417 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2418 you might expect that, since timezone is a synonym for _timezone,
2419 the same number will print both times. However, if the processor
2420 backend uses a COPY reloc, then actually timezone will be copied
2421 into your process image, and, since you define _timezone
2422 yourself, _timezone will not. Thus timezone and _timezone will
2423 wind up at different memory locations. The tzset call will set
2424 _timezone, leaving timezone unchanged. */
2426 if (h->u.weakdef != NULL)
2428 /* If we get to this point, we know there is an implicit
2429 reference by a regular object file via the weak symbol H.
2430 FIXME: Is this really true? What if the traversal finds
2431 H->U.WEAKDEF before it finds H? */
2432 h->u.weakdef->ref_regular = 1;
2434 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2438 /* If a symbol has no type and no size and does not require a PLT
2439 entry, then we are probably about to do the wrong thing here: we
2440 are probably going to create a COPY reloc for an empty object.
2441 This case can arise when a shared object is built with assembly
2442 code, and the assembly code fails to set the symbol type. */
2444 && h->type == STT_NOTYPE
2446 (*_bfd_error_handler)
2447 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2448 h->root.root.string);
2450 dynobj = elf_hash_table (eif->info)->dynobj;
2451 bed = get_elf_backend_data (dynobj);
2452 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2461 /* Adjust all external symbols pointing into SEC_MERGE sections
2462 to reflect the object merging within the sections. */
2465 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2469 if (h->root.type == bfd_link_hash_warning)
2470 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2472 if ((h->root.type == bfd_link_hash_defined
2473 || h->root.type == bfd_link_hash_defweak)
2474 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2475 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2477 bfd *output_bfd = data;
2479 h->root.u.def.value =
2480 _bfd_merged_section_offset (output_bfd,
2481 &h->root.u.def.section,
2482 elf_section_data (sec)->sec_info,
2483 h->root.u.def.value);
2489 /* Returns false if the symbol referred to by H should be considered
2490 to resolve local to the current module, and true if it should be
2491 considered to bind dynamically. */
2494 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2495 struct bfd_link_info *info,
2496 bfd_boolean ignore_protected)
2498 bfd_boolean binding_stays_local_p;
2503 while (h->root.type == bfd_link_hash_indirect
2504 || h->root.type == bfd_link_hash_warning)
2505 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2507 /* If it was forced local, then clearly it's not dynamic. */
2508 if (h->dynindx == -1)
2510 if (h->forced_local)
2513 /* Identify the cases where name binding rules say that a
2514 visible symbol resolves locally. */
2515 binding_stays_local_p = info->executable || info->symbolic;
2517 switch (ELF_ST_VISIBILITY (h->other))
2524 /* Proper resolution for function pointer equality may require
2525 that these symbols perhaps be resolved dynamically, even though
2526 we should be resolving them to the current module. */
2527 if (!ignore_protected || h->type != STT_FUNC)
2528 binding_stays_local_p = TRUE;
2535 /* If it isn't defined locally, then clearly it's dynamic. */
2536 if (!h->def_regular)
2539 /* Otherwise, the symbol is dynamic if binding rules don't tell
2540 us that it remains local. */
2541 return !binding_stays_local_p;
2544 /* Return true if the symbol referred to by H should be considered
2545 to resolve local to the current module, and false otherwise. Differs
2546 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2547 undefined symbols and weak symbols. */
2550 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2551 struct bfd_link_info *info,
2552 bfd_boolean local_protected)
2554 /* If it's a local sym, of course we resolve locally. */
2558 /* Common symbols that become definitions don't get the DEF_REGULAR
2559 flag set, so test it first, and don't bail out. */
2560 if (ELF_COMMON_DEF_P (h))
2562 /* If we don't have a definition in a regular file, then we can't
2563 resolve locally. The sym is either undefined or dynamic. */
2564 else if (!h->def_regular)
2567 /* Forced local symbols resolve locally. */
2568 if (h->forced_local)
2571 /* As do non-dynamic symbols. */
2572 if (h->dynindx == -1)
2575 /* At this point, we know the symbol is defined and dynamic. In an
2576 executable it must resolve locally, likewise when building symbolic
2577 shared libraries. */
2578 if (info->executable || info->symbolic)
2581 /* Now deal with defined dynamic symbols in shared libraries. Ones
2582 with default visibility might not resolve locally. */
2583 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2586 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2587 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2590 /* STV_PROTECTED non-function symbols are local. */
2591 if (h->type != STT_FUNC)
2594 /* Function pointer equality tests may require that STV_PROTECTED
2595 symbols be treated as dynamic symbols, even when we know that the
2596 dynamic linker will resolve them locally. */
2597 return local_protected;
2600 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2601 aligned. Returns the first TLS output section. */
2603 struct bfd_section *
2604 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2606 struct bfd_section *sec, *tls;
2607 unsigned int align = 0;
2609 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2610 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2614 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2615 if (sec->alignment_power > align)
2616 align = sec->alignment_power;
2618 elf_hash_table (info)->tls_sec = tls;
2620 /* Ensure the alignment of the first section is the largest alignment,
2621 so that the tls segment starts aligned. */
2623 tls->alignment_power = align;
2628 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2630 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2631 Elf_Internal_Sym *sym)
2633 const struct elf_backend_data *bed;
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 bed = get_elf_backend_data (abfd);
2651 if (bed->common_definition (sym))
2654 /* If the symbol is in a target specific section then we
2655 must rely upon the backend to tell us what it is. */
2656 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2657 /* FIXME - this function is not coded yet:
2659 return _bfd_is_global_symbol_definition (abfd, sym);
2661 Instead for now assume that the definition is not global,
2662 Even if this is wrong, at least the linker will behave
2663 in the same way that it used to do. */
2669 /* Search the symbol table of the archive element of the archive ABFD
2670 whose archive map contains a mention of SYMDEF, and determine if
2671 the symbol is defined in this element. */
2673 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2675 Elf_Internal_Shdr * hdr;
2676 bfd_size_type symcount;
2677 bfd_size_type extsymcount;
2678 bfd_size_type extsymoff;
2679 Elf_Internal_Sym *isymbuf;
2680 Elf_Internal_Sym *isym;
2681 Elf_Internal_Sym *isymend;
2684 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2688 if (! bfd_check_format (abfd, bfd_object))
2691 /* If we have already included the element containing this symbol in the
2692 link then we do not need to include it again. Just claim that any symbol
2693 it contains is not a definition, so that our caller will not decide to
2694 (re)include this element. */
2695 if (abfd->archive_pass)
2698 /* Select the appropriate symbol table. */
2699 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2700 hdr = &elf_tdata (abfd)->symtab_hdr;
2702 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2704 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2706 /* The sh_info field of the symtab header tells us where the
2707 external symbols start. We don't care about the local symbols. */
2708 if (elf_bad_symtab (abfd))
2710 extsymcount = symcount;
2715 extsymcount = symcount - hdr->sh_info;
2716 extsymoff = hdr->sh_info;
2719 if (extsymcount == 0)
2722 /* Read in the symbol table. */
2723 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2725 if (isymbuf == NULL)
2728 /* Scan the symbol table looking for SYMDEF. */
2730 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2734 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2739 if (strcmp (name, symdef->name) == 0)
2741 result = is_global_data_symbol_definition (abfd, isym);
2751 /* Add an entry to the .dynamic table. */
2754 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2758 struct elf_link_hash_table *hash_table;
2759 const struct elf_backend_data *bed;
2761 bfd_size_type newsize;
2762 bfd_byte *newcontents;
2763 Elf_Internal_Dyn dyn;
2765 hash_table = elf_hash_table (info);
2766 if (! is_elf_hash_table (hash_table))
2769 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2771 (_("warning: creating a DT_TEXTREL in a shared object."));
2773 bed = get_elf_backend_data (hash_table->dynobj);
2774 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2775 BFD_ASSERT (s != NULL);
2777 newsize = s->size + bed->s->sizeof_dyn;
2778 newcontents = bfd_realloc (s->contents, newsize);
2779 if (newcontents == NULL)
2783 dyn.d_un.d_val = val;
2784 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2787 s->contents = newcontents;
2792 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2793 otherwise just check whether one already exists. Returns -1 on error,
2794 1 if a DT_NEEDED tag already exists, and 0 on success. */
2797 elf_add_dt_needed_tag (bfd *abfd,
2798 struct bfd_link_info *info,
2802 struct elf_link_hash_table *hash_table;
2803 bfd_size_type oldsize;
2804 bfd_size_type strindex;
2806 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2809 hash_table = elf_hash_table (info);
2810 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2811 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2812 if (strindex == (bfd_size_type) -1)
2815 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2818 const struct elf_backend_data *bed;
2821 bed = get_elf_backend_data (hash_table->dynobj);
2822 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2824 for (extdyn = sdyn->contents;
2825 extdyn < sdyn->contents + sdyn->size;
2826 extdyn += bed->s->sizeof_dyn)
2828 Elf_Internal_Dyn dyn;
2830 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2831 if (dyn.d_tag == DT_NEEDED
2832 && dyn.d_un.d_val == strindex)
2834 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2842 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2845 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2849 /* We were just checking for existence of the tag. */
2850 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2855 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2856 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2857 references from regular objects to these symbols.
2859 ??? Should we do something about references from other dynamic
2860 obects? If not, we potentially lose some warnings about undefined
2861 symbols. But how can we recover the initial undefined / undefweak
2864 struct elf_smash_syms_data
2867 struct elf_link_hash_table *htab;
2868 bfd_boolean twiddled;
2872 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2874 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2875 struct bfd_link_hash_entry *bh;
2877 switch (h->root.type)
2880 case bfd_link_hash_new:
2883 case bfd_link_hash_undefined:
2884 if (h->root.u.undef.abfd != inf->not_needed)
2886 if (h->root.u.undef.weak != NULL
2887 && h->root.u.undef.weak != inf->not_needed)
2889 /* Symbol was undefweak in u.undef.weak bfd, and has become
2890 undefined in as-needed lib. Restore weak. */
2891 h->root.type = bfd_link_hash_undefweak;
2892 h->root.u.undef.abfd = h->root.u.undef.weak;
2893 if (h->root.u.undef.next != NULL
2894 || inf->htab->root.undefs_tail == &h->root)
2895 inf->twiddled = TRUE;
2900 case bfd_link_hash_undefweak:
2901 if (h->root.u.undef.abfd != inf->not_needed)
2905 case bfd_link_hash_defined:
2906 case bfd_link_hash_defweak:
2907 if (h->root.u.def.section->owner != inf->not_needed)
2911 case bfd_link_hash_common:
2912 if (h->root.u.c.p->section->owner != inf->not_needed)
2916 case bfd_link_hash_warning:
2917 case bfd_link_hash_indirect:
2918 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2919 if (h->root.u.i.link->type != bfd_link_hash_new)
2921 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2926 /* There is no way we can undo symbol table state from defined or
2927 defweak back to undefined. */
2931 /* Set sym back to newly created state, but keep undef.next if it is
2932 being used as a list pointer. */
2933 bh = h->root.u.undef.next;
2936 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2937 inf->twiddled = TRUE;
2938 (*inf->htab->root.table.newfunc) (&h->root.root,
2939 &inf->htab->root.table,
2940 h->root.root.string);
2941 h->root.u.undef.next = bh;
2942 h->root.u.undef.abfd = inf->not_needed;
2947 /* Sort symbol by value and section. */
2949 elf_sort_symbol (const void *arg1, const void *arg2)
2951 const struct elf_link_hash_entry *h1;
2952 const struct elf_link_hash_entry *h2;
2953 bfd_signed_vma vdiff;
2955 h1 = *(const struct elf_link_hash_entry **) arg1;
2956 h2 = *(const struct elf_link_hash_entry **) arg2;
2957 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2959 return vdiff > 0 ? 1 : -1;
2962 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2964 return sdiff > 0 ? 1 : -1;
2969 /* This function is used to adjust offsets into .dynstr for
2970 dynamic symbols. This is called via elf_link_hash_traverse. */
2973 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2975 struct elf_strtab_hash *dynstr = data;
2977 if (h->root.type == bfd_link_hash_warning)
2978 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2980 if (h->dynindx != -1)
2981 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2985 /* Assign string offsets in .dynstr, update all structures referencing
2989 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2991 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2992 struct elf_link_local_dynamic_entry *entry;
2993 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2994 bfd *dynobj = hash_table->dynobj;
2997 const struct elf_backend_data *bed;
3000 _bfd_elf_strtab_finalize (dynstr);
3001 size = _bfd_elf_strtab_size (dynstr);
3003 bed = get_elf_backend_data (dynobj);
3004 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3005 BFD_ASSERT (sdyn != NULL);
3007 /* Update all .dynamic entries referencing .dynstr strings. */
3008 for (extdyn = sdyn->contents;
3009 extdyn < sdyn->contents + sdyn->size;
3010 extdyn += bed->s->sizeof_dyn)
3012 Elf_Internal_Dyn dyn;
3014 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3018 dyn.d_un.d_val = size;
3026 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3031 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3034 /* Now update local dynamic symbols. */
3035 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3036 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3037 entry->isym.st_name);
3039 /* And the rest of dynamic symbols. */
3040 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3042 /* Adjust version definitions. */
3043 if (elf_tdata (output_bfd)->cverdefs)
3048 Elf_Internal_Verdef def;
3049 Elf_Internal_Verdaux defaux;
3051 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3055 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3057 p += sizeof (Elf_External_Verdef);
3058 if (def.vd_aux != sizeof (Elf_External_Verdef))
3060 for (i = 0; i < def.vd_cnt; ++i)
3062 _bfd_elf_swap_verdaux_in (output_bfd,
3063 (Elf_External_Verdaux *) p, &defaux);
3064 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3066 _bfd_elf_swap_verdaux_out (output_bfd,
3067 &defaux, (Elf_External_Verdaux *) p);
3068 p += sizeof (Elf_External_Verdaux);
3071 while (def.vd_next);
3074 /* Adjust version references. */
3075 if (elf_tdata (output_bfd)->verref)
3080 Elf_Internal_Verneed need;
3081 Elf_Internal_Vernaux needaux;
3083 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3087 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3089 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3090 _bfd_elf_swap_verneed_out (output_bfd, &need,
3091 (Elf_External_Verneed *) p);
3092 p += sizeof (Elf_External_Verneed);
3093 for (i = 0; i < need.vn_cnt; ++i)
3095 _bfd_elf_swap_vernaux_in (output_bfd,
3096 (Elf_External_Vernaux *) p, &needaux);
3097 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3099 _bfd_elf_swap_vernaux_out (output_bfd,
3101 (Elf_External_Vernaux *) p);
3102 p += sizeof (Elf_External_Vernaux);
3105 while (need.vn_next);
3111 /* Add symbols from an ELF object file to the linker hash table. */
3114 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3116 bfd_boolean (*add_symbol_hook)
3117 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3118 const char **, flagword *, asection **, bfd_vma *);
3119 bfd_boolean (*check_relocs)
3120 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3121 bfd_boolean (*check_directives)
3122 (bfd *, struct bfd_link_info *);
3123 bfd_boolean collect;
3124 Elf_Internal_Shdr *hdr;
3125 bfd_size_type symcount;
3126 bfd_size_type extsymcount;
3127 bfd_size_type extsymoff;
3128 struct elf_link_hash_entry **sym_hash;
3129 bfd_boolean dynamic;
3130 Elf_External_Versym *extversym = NULL;
3131 Elf_External_Versym *ever;
3132 struct elf_link_hash_entry *weaks;
3133 struct elf_link_hash_entry **nondeflt_vers = NULL;
3134 bfd_size_type nondeflt_vers_cnt = 0;
3135 Elf_Internal_Sym *isymbuf = NULL;
3136 Elf_Internal_Sym *isym;
3137 Elf_Internal_Sym *isymend;
3138 const struct elf_backend_data *bed;
3139 bfd_boolean add_needed;
3140 struct elf_link_hash_table * hash_table;
3143 hash_table = elf_hash_table (info);
3145 bed = get_elf_backend_data (abfd);
3146 add_symbol_hook = bed->elf_add_symbol_hook;
3147 collect = bed->collect;
3149 if ((abfd->flags & DYNAMIC) == 0)
3155 /* You can't use -r against a dynamic object. Also, there's no
3156 hope of using a dynamic object which does not exactly match
3157 the format of the output file. */
3158 if (info->relocatable
3159 || !is_elf_hash_table (hash_table)
3160 || hash_table->root.creator != abfd->xvec)
3162 if (info->relocatable)
3163 bfd_set_error (bfd_error_invalid_operation);
3165 bfd_set_error (bfd_error_wrong_format);
3170 /* As a GNU extension, any input sections which are named
3171 .gnu.warning.SYMBOL are treated as warning symbols for the given
3172 symbol. This differs from .gnu.warning sections, which generate
3173 warnings when they are included in an output file. */
3174 if (info->executable)
3178 for (s = abfd->sections; s != NULL; s = s->next)
3182 name = bfd_get_section_name (abfd, s);
3183 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3188 name += sizeof ".gnu.warning." - 1;
3190 /* If this is a shared object, then look up the symbol
3191 in the hash table. If it is there, and it is already
3192 been defined, then we will not be using the entry
3193 from this shared object, so we don't need to warn.
3194 FIXME: If we see the definition in a regular object
3195 later on, we will warn, but we shouldn't. The only
3196 fix is to keep track of what warnings we are supposed
3197 to emit, and then handle them all at the end of the
3201 struct elf_link_hash_entry *h;
3203 h = elf_link_hash_lookup (hash_table, name,
3204 FALSE, FALSE, TRUE);
3206 /* FIXME: What about bfd_link_hash_common? */
3208 && (h->root.type == bfd_link_hash_defined
3209 || h->root.type == bfd_link_hash_defweak))
3211 /* We don't want to issue this warning. Clobber
3212 the section size so that the warning does not
3213 get copied into the output file. */
3220 msg = bfd_alloc (abfd, sz + 1);
3224 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3229 if (! (_bfd_generic_link_add_one_symbol
3230 (info, abfd, name, BSF_WARNING, s, 0, msg,
3231 FALSE, collect, NULL)))
3234 if (! info->relocatable)
3236 /* Clobber the section size so that the warning does
3237 not get copied into the output file. */
3240 /* Also set SEC_EXCLUDE, so that symbols defined in
3241 the warning section don't get copied to the output. */
3242 s->flags |= SEC_EXCLUDE;
3251 /* If we are creating a shared library, create all the dynamic
3252 sections immediately. We need to attach them to something,
3253 so we attach them to this BFD, provided it is the right
3254 format. FIXME: If there are no input BFD's of the same
3255 format as the output, we can't make a shared library. */
3257 && is_elf_hash_table (hash_table)
3258 && hash_table->root.creator == abfd->xvec
3259 && ! hash_table->dynamic_sections_created)
3261 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3265 else if (!is_elf_hash_table (hash_table))
3270 const char *soname = NULL;
3271 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3274 /* ld --just-symbols and dynamic objects don't mix very well.
3275 ld shouldn't allow it. */
3276 if ((s = abfd->sections) != NULL
3277 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3280 /* If this dynamic lib was specified on the command line with
3281 --as-needed in effect, then we don't want to add a DT_NEEDED
3282 tag unless the lib is actually used. Similary for libs brought
3283 in by another lib's DT_NEEDED. When --no-add-needed is used
3284 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3285 any dynamic library in DT_NEEDED tags in the dynamic lib at
3287 add_needed = (elf_dyn_lib_class (abfd)
3288 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3289 | DYN_NO_NEEDED)) == 0;
3291 s = bfd_get_section_by_name (abfd, ".dynamic");
3297 unsigned long shlink;
3299 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3300 goto error_free_dyn;
3302 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3304 goto error_free_dyn;
3305 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3307 for (extdyn = dynbuf;
3308 extdyn < dynbuf + s->size;
3309 extdyn += bed->s->sizeof_dyn)
3311 Elf_Internal_Dyn dyn;
3313 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3314 if (dyn.d_tag == DT_SONAME)
3316 unsigned int tagv = dyn.d_un.d_val;
3317 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3319 goto error_free_dyn;
3321 if (dyn.d_tag == DT_NEEDED)
3323 struct bfd_link_needed_list *n, **pn;
3325 unsigned int tagv = dyn.d_un.d_val;
3327 amt = sizeof (struct bfd_link_needed_list);
3328 n = bfd_alloc (abfd, amt);
3329 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3330 if (n == NULL || fnm == NULL)
3331 goto error_free_dyn;
3332 amt = strlen (fnm) + 1;
3333 anm = bfd_alloc (abfd, amt);
3335 goto error_free_dyn;
3336 memcpy (anm, fnm, amt);
3340 for (pn = & hash_table->needed;
3346 if (dyn.d_tag == DT_RUNPATH)
3348 struct bfd_link_needed_list *n, **pn;
3350 unsigned int tagv = dyn.d_un.d_val;
3352 amt = sizeof (struct bfd_link_needed_list);
3353 n = bfd_alloc (abfd, amt);
3354 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3355 if (n == NULL || fnm == NULL)
3356 goto error_free_dyn;
3357 amt = strlen (fnm) + 1;
3358 anm = bfd_alloc (abfd, amt);
3360 goto error_free_dyn;
3361 memcpy (anm, fnm, amt);
3365 for (pn = & runpath;
3371 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3372 if (!runpath && dyn.d_tag == DT_RPATH)
3374 struct bfd_link_needed_list *n, **pn;
3376 unsigned int tagv = dyn.d_un.d_val;
3378 amt = sizeof (struct bfd_link_needed_list);
3379 n = bfd_alloc (abfd, amt);
3380 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3381 if (n == NULL || fnm == NULL)
3382 goto error_free_dyn;
3383 amt = strlen (fnm) + 1;
3384 anm = bfd_alloc (abfd, amt);
3391 memcpy (anm, fnm, amt);
3406 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3407 frees all more recently bfd_alloc'd blocks as well. */
3413 struct bfd_link_needed_list **pn;
3414 for (pn = & hash_table->runpath;
3421 /* We do not want to include any of the sections in a dynamic
3422 object in the output file. We hack by simply clobbering the
3423 list of sections in the BFD. This could be handled more
3424 cleanly by, say, a new section flag; the existing
3425 SEC_NEVER_LOAD flag is not the one we want, because that one
3426 still implies that the section takes up space in the output
3428 bfd_section_list_clear (abfd);
3430 /* Find the name to use in a DT_NEEDED entry that refers to this
3431 object. If the object has a DT_SONAME entry, we use it.
3432 Otherwise, if the generic linker stuck something in
3433 elf_dt_name, we use that. Otherwise, we just use the file
3435 if (soname == NULL || *soname == '\0')
3437 soname = elf_dt_name (abfd);
3438 if (soname == NULL || *soname == '\0')
3439 soname = bfd_get_filename (abfd);
3442 /* Save the SONAME because sometimes the linker emulation code
3443 will need to know it. */
3444 elf_dt_name (abfd) = soname;
3446 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3450 /* If we have already included this dynamic object in the
3451 link, just ignore it. There is no reason to include a
3452 particular dynamic object more than once. */
3457 /* If this is a dynamic object, we always link against the .dynsym
3458 symbol table, not the .symtab symbol table. The dynamic linker
3459 will only see the .dynsym symbol table, so there is no reason to
3460 look at .symtab for a dynamic object. */
3462 if (! dynamic || elf_dynsymtab (abfd) == 0)
3463 hdr = &elf_tdata (abfd)->symtab_hdr;
3465 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3467 symcount = hdr->sh_size / bed->s->sizeof_sym;
3469 /* The sh_info field of the symtab header tells us where the
3470 external symbols start. We don't care about the local symbols at
3472 if (elf_bad_symtab (abfd))
3474 extsymcount = symcount;
3479 extsymcount = symcount - hdr->sh_info;
3480 extsymoff = hdr->sh_info;
3484 if (extsymcount != 0)
3486 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3488 if (isymbuf == NULL)
3491 /* We store a pointer to the hash table entry for each external
3493 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3494 sym_hash = bfd_alloc (abfd, amt);
3495 if (sym_hash == NULL)
3496 goto error_free_sym;
3497 elf_sym_hashes (abfd) = sym_hash;
3502 /* Read in any version definitions. */
3503 if (!_bfd_elf_slurp_version_tables (abfd,
3504 info->default_imported_symver))
3505 goto error_free_sym;
3507 /* Read in the symbol versions, but don't bother to convert them
3508 to internal format. */
3509 if (elf_dynversym (abfd) != 0)
3511 Elf_Internal_Shdr *versymhdr;
3513 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3514 extversym = bfd_malloc (versymhdr->sh_size);
3515 if (extversym == NULL)
3516 goto error_free_sym;
3517 amt = versymhdr->sh_size;
3518 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3519 || bfd_bread (extversym, amt, abfd) != amt)
3520 goto error_free_vers;
3526 ever = extversym != NULL ? extversym + extsymoff : NULL;
3527 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3529 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3533 asection *sec, *new_sec;
3536 struct elf_link_hash_entry *h;
3537 bfd_boolean definition;
3538 bfd_boolean size_change_ok;
3539 bfd_boolean type_change_ok;
3540 bfd_boolean new_weakdef;
3541 bfd_boolean override;
3543 unsigned int old_alignment;
3548 flags = BSF_NO_FLAGS;
3550 value = isym->st_value;
3552 common = bed->common_definition (isym);
3554 bind = ELF_ST_BIND (isym->st_info);
3555 if (bind == STB_LOCAL)
3557 /* This should be impossible, since ELF requires that all
3558 global symbols follow all local symbols, and that sh_info
3559 point to the first global symbol. Unfortunately, Irix 5
3563 else if (bind == STB_GLOBAL)
3565 if (isym->st_shndx != SHN_UNDEF && !common)
3568 else if (bind == STB_WEAK)
3572 /* Leave it up to the processor backend. */
3575 if (isym->st_shndx == SHN_UNDEF)
3576 sec = bfd_und_section_ptr;
3577 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3579 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3581 sec = bfd_abs_section_ptr;
3582 else if (sec->kept_section)
3584 /* Symbols from discarded section are undefined, and have
3585 default visibility. */
3586 sec = bfd_und_section_ptr;
3587 isym->st_shndx = SHN_UNDEF;
3588 isym->st_other = STV_DEFAULT
3589 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3591 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3594 else if (isym->st_shndx == SHN_ABS)
3595 sec = bfd_abs_section_ptr;
3596 else if (isym->st_shndx == SHN_COMMON)
3598 sec = bfd_com_section_ptr;
3599 /* What ELF calls the size we call the value. What ELF
3600 calls the value we call the alignment. */
3601 value = isym->st_size;
3605 /* Leave it up to the processor backend. */
3608 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3611 goto error_free_vers;
3613 if (isym->st_shndx == SHN_COMMON
3614 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3616 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3620 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3623 | SEC_LINKER_CREATED
3624 | SEC_THREAD_LOCAL));
3626 goto error_free_vers;
3630 else if (add_symbol_hook)
3632 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3634 goto error_free_vers;
3636 /* The hook function sets the name to NULL if this symbol
3637 should be skipped for some reason. */
3642 /* Sanity check that all possibilities were handled. */
3645 bfd_set_error (bfd_error_bad_value);
3646 goto error_free_vers;
3649 if (bfd_is_und_section (sec)
3650 || bfd_is_com_section (sec))
3655 size_change_ok = FALSE;
3656 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3661 if (is_elf_hash_table (hash_table))
3663 Elf_Internal_Versym iver;
3664 unsigned int vernum = 0;
3669 if (info->default_imported_symver)
3670 /* Use the default symbol version created earlier. */
3671 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3676 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3678 vernum = iver.vs_vers & VERSYM_VERSION;
3680 /* If this is a hidden symbol, or if it is not version
3681 1, we append the version name to the symbol name.
3682 However, we do not modify a non-hidden absolute symbol
3683 if it is not a function, because it might be the version
3684 symbol itself. FIXME: What if it isn't? */
3685 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3686 || (vernum > 1 && (! bfd_is_abs_section (sec)
3687 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3690 size_t namelen, verlen, newlen;
3693 if (isym->st_shndx != SHN_UNDEF)
3695 if (vernum > elf_tdata (abfd)->cverdefs)
3697 else if (vernum > 1)
3699 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3705 (*_bfd_error_handler)
3706 (_("%B: %s: invalid version %u (max %d)"),
3708 elf_tdata (abfd)->cverdefs);
3709 bfd_set_error (bfd_error_bad_value);
3710 goto error_free_vers;
3715 /* We cannot simply test for the number of
3716 entries in the VERNEED section since the
3717 numbers for the needed versions do not start
3719 Elf_Internal_Verneed *t;
3722 for (t = elf_tdata (abfd)->verref;
3726 Elf_Internal_Vernaux *a;
3728 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3730 if (a->vna_other == vernum)
3732 verstr = a->vna_nodename;
3741 (*_bfd_error_handler)
3742 (_("%B: %s: invalid needed version %d"),
3743 abfd, name, vernum);
3744 bfd_set_error (bfd_error_bad_value);
3745 goto error_free_vers;
3749 namelen = strlen (name);
3750 verlen = strlen (verstr);
3751 newlen = namelen + verlen + 2;
3752 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3753 && isym->st_shndx != SHN_UNDEF)
3756 newname = bfd_alloc (abfd, newlen);
3757 if (newname == NULL)
3758 goto error_free_vers;
3759 memcpy (newname, name, namelen);
3760 p = newname + namelen;
3762 /* If this is a defined non-hidden version symbol,
3763 we add another @ to the name. This indicates the
3764 default version of the symbol. */
3765 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3766 && isym->st_shndx != SHN_UNDEF)
3768 memcpy (p, verstr, verlen + 1);
3773 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3774 &value, &old_alignment,
3775 sym_hash, &skip, &override,
3776 &type_change_ok, &size_change_ok))
3777 goto error_free_vers;
3786 while (h->root.type == bfd_link_hash_indirect
3787 || h->root.type == bfd_link_hash_warning)
3788 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3790 /* Remember the old alignment if this is a common symbol, so
3791 that we don't reduce the alignment later on. We can't
3792 check later, because _bfd_generic_link_add_one_symbol
3793 will set a default for the alignment which we want to
3794 override. We also remember the old bfd where the existing
3795 definition comes from. */
3796 switch (h->root.type)
3801 case bfd_link_hash_defined:
3802 case bfd_link_hash_defweak:
3803 old_bfd = h->root.u.def.section->owner;
3806 case bfd_link_hash_common:
3807 old_bfd = h->root.u.c.p->section->owner;
3808 old_alignment = h->root.u.c.p->alignment_power;
3812 if (elf_tdata (abfd)->verdef != NULL
3816 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3819 if (! (_bfd_generic_link_add_one_symbol
3820 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3821 (struct bfd_link_hash_entry **) sym_hash)))
3822 goto error_free_vers;
3825 while (h->root.type == bfd_link_hash_indirect
3826 || h->root.type == bfd_link_hash_warning)
3827 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3830 new_weakdef = FALSE;
3833 && (flags & BSF_WEAK) != 0
3834 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3835 && is_elf_hash_table (hash_table)
3836 && h->u.weakdef == NULL)
3838 /* Keep a list of all weak defined non function symbols from
3839 a dynamic object, using the weakdef field. Later in this
3840 function we will set the weakdef field to the correct
3841 value. We only put non-function symbols from dynamic
3842 objects on this list, because that happens to be the only
3843 time we need to know the normal symbol corresponding to a
3844 weak symbol, and the information is time consuming to
3845 figure out. If the weakdef field is not already NULL,
3846 then this symbol was already defined by some previous
3847 dynamic object, and we will be using that previous
3848 definition anyhow. */
3850 h->u.weakdef = weaks;
3855 /* Set the alignment of a common symbol. */
3856 if ((common || bfd_is_com_section (sec))
3857 && h->root.type == bfd_link_hash_common)
3862 align = bfd_log2 (isym->st_value);
3865 /* The new symbol is a common symbol in a shared object.
3866 We need to get the alignment from the section. */
3867 align = new_sec->alignment_power;
3869 if (align > old_alignment
3870 /* Permit an alignment power of zero if an alignment of one
3871 is specified and no other alignments have been specified. */
3872 || (isym->st_value == 1 && old_alignment == 0))
3873 h->root.u.c.p->alignment_power = align;
3875 h->root.u.c.p->alignment_power = old_alignment;
3878 if (is_elf_hash_table (hash_table))
3882 /* Check the alignment when a common symbol is involved. This
3883 can change when a common symbol is overridden by a normal
3884 definition or a common symbol is ignored due to the old
3885 normal definition. We need to make sure the maximum
3886 alignment is maintained. */
3887 if ((old_alignment || common)
3888 && h->root.type != bfd_link_hash_common)
3890 unsigned int common_align;
3891 unsigned int normal_align;
3892 unsigned int symbol_align;
3896 symbol_align = ffs (h->root.u.def.value) - 1;
3897 if (h->root.u.def.section->owner != NULL
3898 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3900 normal_align = h->root.u.def.section->alignment_power;
3901 if (normal_align > symbol_align)
3902 normal_align = symbol_align;
3905 normal_align = symbol_align;
3909 common_align = old_alignment;
3910 common_bfd = old_bfd;
3915 common_align = bfd_log2 (isym->st_value);
3917 normal_bfd = old_bfd;
3920 if (normal_align < common_align)
3921 (*_bfd_error_handler)
3922 (_("Warning: alignment %u of symbol `%s' in %B"
3923 " is smaller than %u in %B"),
3924 normal_bfd, common_bfd,
3925 1 << normal_align, name, 1 << common_align);
3928 /* Remember the symbol size and type. */
3929 if (isym->st_size != 0
3930 && (definition || h->size == 0))
3932 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3933 (*_bfd_error_handler)
3934 (_("Warning: size of symbol `%s' changed"
3935 " from %lu in %B to %lu in %B"),
3937 name, (unsigned long) h->size,
3938 (unsigned long) isym->st_size);
3940 h->size = isym->st_size;
3943 /* If this is a common symbol, then we always want H->SIZE
3944 to be the size of the common symbol. The code just above
3945 won't fix the size if a common symbol becomes larger. We
3946 don't warn about a size change here, because that is
3947 covered by --warn-common. */
3948 if (h->root.type == bfd_link_hash_common)
3949 h->size = h->root.u.c.size;
3951 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3952 && (definition || h->type == STT_NOTYPE))
3954 if (h->type != STT_NOTYPE
3955 && h->type != ELF_ST_TYPE (isym->st_info)
3956 && ! type_change_ok)
3957 (*_bfd_error_handler)
3958 (_("Warning: type of symbol `%s' changed"
3959 " from %d to %d in %B"),
3960 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3962 h->type = ELF_ST_TYPE (isym->st_info);
3965 /* If st_other has a processor-specific meaning, specific
3966 code might be needed here. We never merge the visibility
3967 attribute with the one from a dynamic object. */
3968 if (bed->elf_backend_merge_symbol_attribute)
3969 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3972 /* If this symbol has default visibility and the user has requested
3973 we not re-export it, then mark it as hidden. */
3974 if (definition && !dynamic
3976 || (abfd->my_archive && abfd->my_archive->no_export))
3977 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3978 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3980 if (isym->st_other != 0 && !dynamic)
3982 unsigned char hvis, symvis, other, nvis;
3984 /* Take the balance of OTHER from the definition. */
3985 other = (definition ? isym->st_other : h->other);
3986 other &= ~ ELF_ST_VISIBILITY (-1);
3988 /* Combine visibilities, using the most constraining one. */
3989 hvis = ELF_ST_VISIBILITY (h->other);
3990 symvis = ELF_ST_VISIBILITY (isym->st_other);
3996 nvis = hvis < symvis ? hvis : symvis;
3998 h->other = other | nvis;
4001 /* Set a flag in the hash table entry indicating the type of
4002 reference or definition we just found. Keep a count of
4003 the number of dynamic symbols we find. A dynamic symbol
4004 is one which is referenced or defined by both a regular
4005 object and a shared object. */
4012 if (bind != STB_WEAK)
4013 h->ref_regular_nonweak = 1;
4017 if (! info->executable
4030 || (h->u.weakdef != NULL
4032 && h->u.weakdef->dynindx != -1))
4036 /* Check to see if we need to add an indirect symbol for
4037 the default name. */
4038 if (definition || h->root.type == bfd_link_hash_common)
4039 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4040 &sec, &value, &dynsym,
4042 goto error_free_vers;
4044 if (definition && !dynamic)
4046 char *p = strchr (name, ELF_VER_CHR);
4047 if (p != NULL && p[1] != ELF_VER_CHR)
4049 /* Queue non-default versions so that .symver x, x@FOO
4050 aliases can be checked. */
4051 if (! nondeflt_vers)
4053 amt = (isymend - isym + 1)
4054 * sizeof (struct elf_link_hash_entry *);
4055 nondeflt_vers = bfd_malloc (amt);
4057 nondeflt_vers [nondeflt_vers_cnt++] = h;
4061 if (dynsym && h->dynindx == -1)
4063 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4064 goto error_free_vers;
4065 if (h->u.weakdef != NULL
4067 && h->u.weakdef->dynindx == -1)
4069 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4070 goto error_free_vers;
4073 else if (dynsym && h->dynindx != -1)
4074 /* If the symbol already has a dynamic index, but
4075 visibility says it should not be visible, turn it into
4077 switch (ELF_ST_VISIBILITY (h->other))
4081 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4092 const char *soname = elf_dt_name (abfd);
4094 /* A symbol from a library loaded via DT_NEEDED of some
4095 other library is referenced by a regular object.
4096 Add a DT_NEEDED entry for it. Issue an error if
4097 --no-add-needed is used. */
4098 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4100 (*_bfd_error_handler)
4101 (_("%s: invalid DSO for symbol `%s' definition"),
4103 bfd_set_error (bfd_error_bad_value);
4104 goto error_free_vers;
4107 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4110 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4112 goto error_free_vers;
4114 BFD_ASSERT (ret == 0);
4119 /* Now that all the symbols from this input file are created, handle
4120 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4121 if (nondeflt_vers != NULL)
4123 bfd_size_type cnt, symidx;
4125 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4127 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4128 char *shortname, *p;
4130 p = strchr (h->root.root.string, ELF_VER_CHR);
4132 || (h->root.type != bfd_link_hash_defined
4133 && h->root.type != bfd_link_hash_defweak))
4136 amt = p - h->root.root.string;
4137 shortname = bfd_malloc (amt + 1);
4138 memcpy (shortname, h->root.root.string, amt);
4139 shortname[amt] = '\0';
4141 hi = (struct elf_link_hash_entry *)
4142 bfd_link_hash_lookup (&hash_table->root, shortname,
4143 FALSE, FALSE, FALSE);
4145 && hi->root.type == h->root.type
4146 && hi->root.u.def.value == h->root.u.def.value
4147 && hi->root.u.def.section == h->root.u.def.section)
4149 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4150 hi->root.type = bfd_link_hash_indirect;
4151 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4152 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4153 sym_hash = elf_sym_hashes (abfd);
4155 for (symidx = 0; symidx < extsymcount; ++symidx)
4156 if (sym_hash[symidx] == hi)
4158 sym_hash[symidx] = h;
4164 free (nondeflt_vers);
4165 nondeflt_vers = NULL;
4168 if (extversym != NULL)
4174 if (isymbuf != NULL)
4179 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4181 /* Remove symbols defined in an as-needed shared lib that wasn't
4183 struct elf_smash_syms_data inf;
4184 inf.not_needed = abfd;
4185 inf.htab = hash_table;
4186 inf.twiddled = FALSE;
4187 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4189 bfd_link_repair_undef_list (&hash_table->root);
4193 /* Now set the weakdefs field correctly for all the weak defined
4194 symbols we found. The only way to do this is to search all the
4195 symbols. Since we only need the information for non functions in
4196 dynamic objects, that's the only time we actually put anything on
4197 the list WEAKS. We need this information so that if a regular
4198 object refers to a symbol defined weakly in a dynamic object, the
4199 real symbol in the dynamic object is also put in the dynamic
4200 symbols; we also must arrange for both symbols to point to the
4201 same memory location. We could handle the general case of symbol
4202 aliasing, but a general symbol alias can only be generated in
4203 assembler code, handling it correctly would be very time
4204 consuming, and other ELF linkers don't handle general aliasing
4208 struct elf_link_hash_entry **hpp;
4209 struct elf_link_hash_entry **hppend;
4210 struct elf_link_hash_entry **sorted_sym_hash;
4211 struct elf_link_hash_entry *h;
4214 /* Since we have to search the whole symbol list for each weak
4215 defined symbol, search time for N weak defined symbols will be
4216 O(N^2). Binary search will cut it down to O(NlogN). */
4217 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4218 sorted_sym_hash = bfd_malloc (amt);
4219 if (sorted_sym_hash == NULL)
4221 sym_hash = sorted_sym_hash;
4222 hpp = elf_sym_hashes (abfd);
4223 hppend = hpp + extsymcount;
4225 for (; hpp < hppend; hpp++)
4229 && h->root.type == bfd_link_hash_defined
4230 && h->type != STT_FUNC)
4238 qsort (sorted_sym_hash, sym_count,
4239 sizeof (struct elf_link_hash_entry *),
4242 while (weaks != NULL)
4244 struct elf_link_hash_entry *hlook;
4251 weaks = hlook->u.weakdef;
4252 hlook->u.weakdef = NULL;
4254 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4255 || hlook->root.type == bfd_link_hash_defweak
4256 || hlook->root.type == bfd_link_hash_common
4257 || hlook->root.type == bfd_link_hash_indirect);
4258 slook = hlook->root.u.def.section;
4259 vlook = hlook->root.u.def.value;
4266 bfd_signed_vma vdiff;
4268 h = sorted_sym_hash [idx];
4269 vdiff = vlook - h->root.u.def.value;
4276 long sdiff = slook->id - h->root.u.def.section->id;
4289 /* We didn't find a value/section match. */
4293 for (i = ilook; i < sym_count; i++)
4295 h = sorted_sym_hash [i];
4297 /* Stop if value or section doesn't match. */
4298 if (h->root.u.def.value != vlook
4299 || h->root.u.def.section != slook)
4301 else if (h != hlook)
4303 hlook->u.weakdef = h;
4305 /* If the weak definition is in the list of dynamic
4306 symbols, make sure the real definition is put
4308 if (hlook->dynindx != -1 && h->dynindx == -1)
4310 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4314 /* If the real definition is in the list of dynamic
4315 symbols, make sure the weak definition is put
4316 there as well. If we don't do this, then the
4317 dynamic loader might not merge the entries for the
4318 real definition and the weak definition. */
4319 if (h->dynindx != -1 && hlook->dynindx == -1)
4321 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4329 free (sorted_sym_hash);
4332 check_directives = get_elf_backend_data (abfd)->check_directives;
4333 if (check_directives)
4334 check_directives (abfd, info);
4336 /* If this object is the same format as the output object, and it is
4337 not a shared library, then let the backend look through the
4340 This is required to build global offset table entries and to
4341 arrange for dynamic relocs. It is not required for the
4342 particular common case of linking non PIC code, even when linking
4343 against shared libraries, but unfortunately there is no way of
4344 knowing whether an object file has been compiled PIC or not.
4345 Looking through the relocs is not particularly time consuming.
4346 The problem is that we must either (1) keep the relocs in memory,
4347 which causes the linker to require additional runtime memory or
4348 (2) read the relocs twice from the input file, which wastes time.
4349 This would be a good case for using mmap.
4351 I have no idea how to handle linking PIC code into a file of a
4352 different format. It probably can't be done. */
4353 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4355 && is_elf_hash_table (hash_table)
4356 && hash_table->root.creator == abfd->xvec
4357 && check_relocs != NULL)
4361 for (o = abfd->sections; o != NULL; o = o->next)
4363 Elf_Internal_Rela *internal_relocs;
4366 if ((o->flags & SEC_RELOC) == 0
4367 || o->reloc_count == 0
4368 || ((info->strip == strip_all || info->strip == strip_debugger)
4369 && (o->flags & SEC_DEBUGGING) != 0)
4370 || bfd_is_abs_section (o->output_section))
4373 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4375 if (internal_relocs == NULL)
4378 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4380 if (elf_section_data (o)->relocs != internal_relocs)
4381 free (internal_relocs);
4388 /* If this is a non-traditional link, try to optimize the handling
4389 of the .stab/.stabstr sections. */
4391 && ! info->traditional_format
4392 && is_elf_hash_table (hash_table)
4393 && (info->strip != strip_all && info->strip != strip_debugger))
4397 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4398 if (stabstr != NULL)
4400 bfd_size_type string_offset = 0;
4403 for (stab = abfd->sections; stab; stab = stab->next)
4404 if (strncmp (".stab", stab->name, 5) == 0
4405 && (!stab->name[5] ||
4406 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4407 && (stab->flags & SEC_MERGE) == 0
4408 && !bfd_is_abs_section (stab->output_section))
4410 struct bfd_elf_section_data *secdata;
4412 secdata = elf_section_data (stab);
4413 if (! _bfd_link_section_stabs (abfd,
4414 &hash_table->stab_info,
4419 if (secdata->sec_info)
4420 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4425 if (is_elf_hash_table (hash_table) && add_needed)
4427 /* Add this bfd to the loaded list. */
4428 struct elf_link_loaded_list *n;
4430 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4434 n->next = hash_table->loaded;
4435 hash_table->loaded = n;
4441 if (nondeflt_vers != NULL)
4442 free (nondeflt_vers);
4443 if (extversym != NULL)
4446 if (isymbuf != NULL)
4452 /* Return the linker hash table entry of a symbol that might be
4453 satisfied by an archive symbol. Return -1 on error. */
4455 struct elf_link_hash_entry *
4456 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4457 struct bfd_link_info *info,
4460 struct elf_link_hash_entry *h;
4464 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4468 /* If this is a default version (the name contains @@), look up the
4469 symbol again with only one `@' as well as without the version.
4470 The effect is that references to the symbol with and without the
4471 version will be matched by the default symbol in the archive. */
4473 p = strchr (name, ELF_VER_CHR);
4474 if (p == NULL || p[1] != ELF_VER_CHR)
4477 /* First check with only one `@'. */
4478 len = strlen (name);
4479 copy = bfd_alloc (abfd, len);
4481 return (struct elf_link_hash_entry *) 0 - 1;
4483 first = p - name + 1;
4484 memcpy (copy, name, first);
4485 memcpy (copy + first, name + first + 1, len - first);
4487 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4490 /* We also need to check references to the symbol without the
4492 copy[first - 1] = '\0';
4493 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4494 FALSE, FALSE, FALSE);
4497 bfd_release (abfd, copy);
4501 /* Add symbols from an ELF archive file to the linker hash table. We
4502 don't use _bfd_generic_link_add_archive_symbols because of a
4503 problem which arises on UnixWare. The UnixWare libc.so is an
4504 archive which includes an entry libc.so.1 which defines a bunch of
4505 symbols. The libc.so archive also includes a number of other
4506 object files, which also define symbols, some of which are the same
4507 as those defined in libc.so.1. Correct linking requires that we
4508 consider each object file in turn, and include it if it defines any
4509 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4510 this; it looks through the list of undefined symbols, and includes
4511 any object file which defines them. When this algorithm is used on
4512 UnixWare, it winds up pulling in libc.so.1 early and defining a
4513 bunch of symbols. This means that some of the other objects in the
4514 archive are not included in the link, which is incorrect since they
4515 precede libc.so.1 in the archive.
4517 Fortunately, ELF archive handling is simpler than that done by
4518 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4519 oddities. In ELF, if we find a symbol in the archive map, and the
4520 symbol is currently undefined, we know that we must pull in that
4523 Unfortunately, we do have to make multiple passes over the symbol
4524 table until nothing further is resolved. */
4527 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4530 bfd_boolean *defined = NULL;
4531 bfd_boolean *included = NULL;
4535 const struct elf_backend_data *bed;
4536 struct elf_link_hash_entry * (*archive_symbol_lookup)
4537 (bfd *, struct bfd_link_info *, const char *);
4539 if (! bfd_has_map (abfd))
4541 /* An empty archive is a special case. */
4542 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4544 bfd_set_error (bfd_error_no_armap);
4548 /* Keep track of all symbols we know to be already defined, and all
4549 files we know to be already included. This is to speed up the
4550 second and subsequent passes. */
4551 c = bfd_ardata (abfd)->symdef_count;
4555 amt *= sizeof (bfd_boolean);
4556 defined = bfd_zmalloc (amt);
4557 included = bfd_zmalloc (amt);
4558 if (defined == NULL || included == NULL)
4561 symdefs = bfd_ardata (abfd)->symdefs;
4562 bed = get_elf_backend_data (abfd);
4563 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4576 symdefend = symdef + c;
4577 for (i = 0; symdef < symdefend; symdef++, i++)
4579 struct elf_link_hash_entry *h;
4581 struct bfd_link_hash_entry *undefs_tail;
4584 if (defined[i] || included[i])
4586 if (symdef->file_offset == last)
4592 h = archive_symbol_lookup (abfd, info, symdef->name);
4593 if (h == (struct elf_link_hash_entry *) 0 - 1)
4599 if (h->root.type == bfd_link_hash_common)
4601 /* We currently have a common symbol. The archive map contains
4602 a reference to this symbol, so we may want to include it. We
4603 only want to include it however, if this archive element
4604 contains a definition of the symbol, not just another common
4607 Unfortunately some archivers (including GNU ar) will put
4608 declarations of common symbols into their archive maps, as
4609 well as real definitions, so we cannot just go by the archive
4610 map alone. Instead we must read in the element's symbol
4611 table and check that to see what kind of symbol definition
4613 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4616 else if (h->root.type != bfd_link_hash_undefined)
4618 if (h->root.type != bfd_link_hash_undefweak)
4623 /* We need to include this archive member. */
4624 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4625 if (element == NULL)
4628 if (! bfd_check_format (element, bfd_object))
4631 /* Doublecheck that we have not included this object
4632 already--it should be impossible, but there may be
4633 something wrong with the archive. */
4634 if (element->archive_pass != 0)
4636 bfd_set_error (bfd_error_bad_value);
4639 element->archive_pass = 1;
4641 undefs_tail = info->hash->undefs_tail;
4643 if (! (*info->callbacks->add_archive_element) (info, element,
4646 if (! bfd_link_add_symbols (element, info))
4649 /* If there are any new undefined symbols, we need to make
4650 another pass through the archive in order to see whether
4651 they can be defined. FIXME: This isn't perfect, because
4652 common symbols wind up on undefs_tail and because an
4653 undefined symbol which is defined later on in this pass
4654 does not require another pass. This isn't a bug, but it
4655 does make the code less efficient than it could be. */
4656 if (undefs_tail != info->hash->undefs_tail)
4659 /* Look backward to mark all symbols from this object file
4660 which we have already seen in this pass. */
4664 included[mark] = TRUE;
4669 while (symdefs[mark].file_offset == symdef->file_offset);
4671 /* We mark subsequent symbols from this object file as we go
4672 on through the loop. */
4673 last = symdef->file_offset;
4684 if (defined != NULL)
4686 if (included != NULL)
4691 /* Given an ELF BFD, add symbols to the global hash table as
4695 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4697 switch (bfd_get_format (abfd))
4700 return elf_link_add_object_symbols (abfd, info);
4702 return elf_link_add_archive_symbols (abfd, info);
4704 bfd_set_error (bfd_error_wrong_format);
4709 /* This function will be called though elf_link_hash_traverse to store
4710 all hash value of the exported symbols in an array. */
4713 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4715 unsigned long **valuep = data;
4721 if (h->root.type == bfd_link_hash_warning)
4722 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4724 /* Ignore indirect symbols. These are added by the versioning code. */
4725 if (h->dynindx == -1)
4728 name = h->root.root.string;
4729 p = strchr (name, ELF_VER_CHR);
4732 alc = bfd_malloc (p - name + 1);
4733 memcpy (alc, name, p - name);
4734 alc[p - name] = '\0';
4738 /* Compute the hash value. */
4739 ha = bfd_elf_hash (name);
4741 /* Store the found hash value in the array given as the argument. */
4744 /* And store it in the struct so that we can put it in the hash table
4746 h->u.elf_hash_value = ha;
4754 /* Array used to determine the number of hash table buckets to use
4755 based on the number of symbols there are. If there are fewer than
4756 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4757 fewer than 37 we use 17 buckets, and so forth. We never use more
4758 than 32771 buckets. */
4760 static const size_t elf_buckets[] =
4762 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4766 /* Compute bucket count for hashing table. We do not use a static set
4767 of possible tables sizes anymore. Instead we determine for all
4768 possible reasonable sizes of the table the outcome (i.e., the
4769 number of collisions etc) and choose the best solution. The
4770 weighting functions are not too simple to allow the table to grow
4771 without bounds. Instead one of the weighting factors is the size.
4772 Therefore the result is always a good payoff between few collisions
4773 (= short chain lengths) and table size. */
4775 compute_bucket_count (struct bfd_link_info *info)
4777 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4778 size_t best_size = 0;
4779 unsigned long int *hashcodes;
4780 unsigned long int *hashcodesp;
4781 unsigned long int i;
4784 /* Compute the hash values for all exported symbols. At the same
4785 time store the values in an array so that we could use them for
4788 amt *= sizeof (unsigned long int);
4789 hashcodes = bfd_malloc (amt);
4790 if (hashcodes == NULL)
4792 hashcodesp = hashcodes;
4794 /* Put all hash values in HASHCODES. */
4795 elf_link_hash_traverse (elf_hash_table (info),
4796 elf_collect_hash_codes, &hashcodesp);
4798 /* We have a problem here. The following code to optimize the table
4799 size requires an integer type with more the 32 bits. If
4800 BFD_HOST_U_64_BIT is set we know about such a type. */
4801 #ifdef BFD_HOST_U_64_BIT
4804 unsigned long int nsyms = hashcodesp - hashcodes;
4807 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4808 unsigned long int *counts ;
4809 bfd *dynobj = elf_hash_table (info)->dynobj;
4810 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4812 /* Possible optimization parameters: if we have NSYMS symbols we say
4813 that the hashing table must at least have NSYMS/4 and at most
4815 minsize = nsyms / 4;
4818 best_size = maxsize = nsyms * 2;
4820 /* Create array where we count the collisions in. We must use bfd_malloc
4821 since the size could be large. */
4823 amt *= sizeof (unsigned long int);
4824 counts = bfd_malloc (amt);
4831 /* Compute the "optimal" size for the hash table. The criteria is a
4832 minimal chain length. The minor criteria is (of course) the size
4834 for (i = minsize; i < maxsize; ++i)
4836 /* Walk through the array of hashcodes and count the collisions. */
4837 BFD_HOST_U_64_BIT max;
4838 unsigned long int j;
4839 unsigned long int fact;
4841 memset (counts, '\0', i * sizeof (unsigned long int));
4843 /* Determine how often each hash bucket is used. */
4844 for (j = 0; j < nsyms; ++j)
4845 ++counts[hashcodes[j] % i];
4847 /* For the weight function we need some information about the
4848 pagesize on the target. This is information need not be 100%
4849 accurate. Since this information is not available (so far) we
4850 define it here to a reasonable default value. If it is crucial
4851 to have a better value some day simply define this value. */
4852 # ifndef BFD_TARGET_PAGESIZE
4853 # define BFD_TARGET_PAGESIZE (4096)
4856 /* We in any case need 2 + NSYMS entries for the size values and
4858 max = (2 + nsyms) * (bed->s->arch_size / 8);
4861 /* Variant 1: optimize for short chains. We add the squares
4862 of all the chain lengths (which favors many small chain
4863 over a few long chains). */
4864 for (j = 0; j < i; ++j)
4865 max += counts[j] * counts[j];
4867 /* This adds penalties for the overall size of the table. */
4868 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4871 /* Variant 2: Optimize a lot more for small table. Here we
4872 also add squares of the size but we also add penalties for
4873 empty slots (the +1 term). */
4874 for (j = 0; j < i; ++j)
4875 max += (1 + counts[j]) * (1 + counts[j]);
4877 /* The overall size of the table is considered, but not as
4878 strong as in variant 1, where it is squared. */
4879 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4883 /* Compare with current best results. */
4884 if (max < best_chlen)
4894 #endif /* defined (BFD_HOST_U_64_BIT) */
4896 /* This is the fallback solution if no 64bit type is available or if we
4897 are not supposed to spend much time on optimizations. We select the
4898 bucket count using a fixed set of numbers. */
4899 for (i = 0; elf_buckets[i] != 0; i++)
4901 best_size = elf_buckets[i];
4902 if (dynsymcount < elf_buckets[i + 1])
4907 /* Free the arrays we needed. */
4913 /* Set up the sizes and contents of the ELF dynamic sections. This is
4914 called by the ELF linker emulation before_allocation routine. We
4915 must set the sizes of the sections before the linker sets the
4916 addresses of the various sections. */
4919 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4922 const char *filter_shlib,
4923 const char * const *auxiliary_filters,
4924 struct bfd_link_info *info,
4925 asection **sinterpptr,
4926 struct bfd_elf_version_tree *verdefs)
4928 bfd_size_type soname_indx;
4930 const struct elf_backend_data *bed;
4931 struct elf_assign_sym_version_info asvinfo;
4935 soname_indx = (bfd_size_type) -1;
4937 if (!is_elf_hash_table (info->hash))
4940 elf_tdata (output_bfd)->relro = info->relro;
4941 if (info->execstack)
4942 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4943 else if (info->noexecstack)
4944 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4948 asection *notesec = NULL;
4951 for (inputobj = info->input_bfds;
4953 inputobj = inputobj->link_next)
4957 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4959 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4962 if (s->flags & SEC_CODE)
4971 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4972 if (exec && info->relocatable
4973 && notesec->output_section != bfd_abs_section_ptr)
4974 notesec->output_section->flags |= SEC_CODE;
4978 /* Any syms created from now on start with -1 in
4979 got.refcount/offset and plt.refcount/offset. */
4980 elf_hash_table (info)->init_got_refcount
4981 = elf_hash_table (info)->init_got_offset;
4982 elf_hash_table (info)->init_plt_refcount
4983 = elf_hash_table (info)->init_plt_offset;
4985 /* The backend may have to create some sections regardless of whether
4986 we're dynamic or not. */
4987 bed = get_elf_backend_data (output_bfd);
4988 if (bed->elf_backend_always_size_sections
4989 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4992 dynobj = elf_hash_table (info)->dynobj;
4994 /* If there were no dynamic objects in the link, there is nothing to
4999 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5002 if (elf_hash_table (info)->dynamic_sections_created)
5004 struct elf_info_failed eif;
5005 struct elf_link_hash_entry *h;
5007 struct bfd_elf_version_tree *t;
5008 struct bfd_elf_version_expr *d;
5010 bfd_boolean all_defined;
5012 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5013 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5017 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5019 if (soname_indx == (bfd_size_type) -1
5020 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5026 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5028 info->flags |= DF_SYMBOLIC;
5035 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5037 if (indx == (bfd_size_type) -1
5038 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5041 if (info->new_dtags)
5043 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5044 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5049 if (filter_shlib != NULL)
5053 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5054 filter_shlib, TRUE);
5055 if (indx == (bfd_size_type) -1
5056 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5060 if (auxiliary_filters != NULL)
5062 const char * const *p;
5064 for (p = auxiliary_filters; *p != NULL; p++)
5068 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5070 if (indx == (bfd_size_type) -1
5071 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5077 eif.verdefs = verdefs;
5080 /* If we are supposed to export all symbols into the dynamic symbol
5081 table (this is not the normal case), then do so. */
5082 if (info->export_dynamic)
5084 elf_link_hash_traverse (elf_hash_table (info),
5085 _bfd_elf_export_symbol,
5091 /* Make all global versions with definition. */
5092 for (t = verdefs; t != NULL; t = t->next)
5093 for (d = t->globals.list; d != NULL; d = d->next)
5094 if (!d->symver && d->symbol)
5096 const char *verstr, *name;
5097 size_t namelen, verlen, newlen;
5099 struct elf_link_hash_entry *newh;
5102 namelen = strlen (name);
5104 verlen = strlen (verstr);
5105 newlen = namelen + verlen + 3;
5107 newname = bfd_malloc (newlen);
5108 if (newname == NULL)
5110 memcpy (newname, name, namelen);
5112 /* Check the hidden versioned definition. */
5113 p = newname + namelen;
5115 memcpy (p, verstr, verlen + 1);
5116 newh = elf_link_hash_lookup (elf_hash_table (info),
5117 newname, FALSE, FALSE,
5120 || (newh->root.type != bfd_link_hash_defined
5121 && newh->root.type != bfd_link_hash_defweak))
5123 /* Check the default versioned definition. */
5125 memcpy (p, verstr, verlen + 1);
5126 newh = elf_link_hash_lookup (elf_hash_table (info),
5127 newname, FALSE, FALSE,
5132 /* Mark this version if there is a definition and it is
5133 not defined in a shared object. */
5135 && !newh->def_dynamic
5136 && (newh->root.type == bfd_link_hash_defined
5137 || newh->root.type == bfd_link_hash_defweak))
5141 /* Attach all the symbols to their version information. */
5142 asvinfo.output_bfd = output_bfd;
5143 asvinfo.info = info;
5144 asvinfo.verdefs = verdefs;
5145 asvinfo.failed = FALSE;
5147 elf_link_hash_traverse (elf_hash_table (info),
5148 _bfd_elf_link_assign_sym_version,
5153 if (!info->allow_undefined_version)
5155 /* Check if all global versions have a definition. */
5157 for (t = verdefs; t != NULL; t = t->next)
5158 for (d = t->globals.list; d != NULL; d = d->next)
5159 if (!d->symver && !d->script)
5161 (*_bfd_error_handler)
5162 (_("%s: undefined version: %s"),
5163 d->pattern, t->name);
5164 all_defined = FALSE;
5169 bfd_set_error (bfd_error_bad_value);
5174 /* Find all symbols which were defined in a dynamic object and make
5175 the backend pick a reasonable value for them. */
5176 elf_link_hash_traverse (elf_hash_table (info),
5177 _bfd_elf_adjust_dynamic_symbol,
5182 /* Add some entries to the .dynamic section. We fill in some of the
5183 values later, in bfd_elf_final_link, but we must add the entries
5184 now so that we know the final size of the .dynamic section. */
5186 /* If there are initialization and/or finalization functions to
5187 call then add the corresponding DT_INIT/DT_FINI entries. */
5188 h = (info->init_function
5189 ? elf_link_hash_lookup (elf_hash_table (info),
5190 info->init_function, FALSE,
5197 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5200 h = (info->fini_function
5201 ? elf_link_hash_lookup (elf_hash_table (info),
5202 info->fini_function, FALSE,
5209 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5213 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5214 if (s != NULL && s->linker_has_input)
5216 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5217 if (! info->executable)
5222 for (sub = info->input_bfds; sub != NULL;
5223 sub = sub->link_next)
5224 for (o = sub->sections; o != NULL; o = o->next)
5225 if (elf_section_data (o)->this_hdr.sh_type
5226 == SHT_PREINIT_ARRAY)
5228 (*_bfd_error_handler)
5229 (_("%B: .preinit_array section is not allowed in DSO"),
5234 bfd_set_error (bfd_error_nonrepresentable_section);
5238 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5239 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5242 s = bfd_get_section_by_name (output_bfd, ".init_array");
5243 if (s != NULL && s->linker_has_input)
5245 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5246 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5249 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5250 if (s != NULL && s->linker_has_input)
5252 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5253 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5257 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5258 /* If .dynstr is excluded from the link, we don't want any of
5259 these tags. Strictly, we should be checking each section
5260 individually; This quick check covers for the case where
5261 someone does a /DISCARD/ : { *(*) }. */
5262 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5264 bfd_size_type strsize;
5266 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5267 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5268 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5269 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5270 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5271 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5272 bed->s->sizeof_sym))
5277 /* The backend must work out the sizes of all the other dynamic
5279 if (bed->elf_backend_size_dynamic_sections
5280 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5283 if (elf_hash_table (info)->dynamic_sections_created)
5285 unsigned long section_sym_count;
5288 /* Set up the version definition section. */
5289 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5290 BFD_ASSERT (s != NULL);
5292 /* We may have created additional version definitions if we are
5293 just linking a regular application. */
5294 verdefs = asvinfo.verdefs;
5296 /* Skip anonymous version tag. */
5297 if (verdefs != NULL && verdefs->vernum == 0)
5298 verdefs = verdefs->next;
5300 if (verdefs == NULL && !info->create_default_symver)
5301 s->flags |= SEC_EXCLUDE;
5306 struct bfd_elf_version_tree *t;
5308 Elf_Internal_Verdef def;
5309 Elf_Internal_Verdaux defaux;
5310 struct bfd_link_hash_entry *bh;
5311 struct elf_link_hash_entry *h;
5317 /* Make space for the base version. */
5318 size += sizeof (Elf_External_Verdef);
5319 size += sizeof (Elf_External_Verdaux);
5322 /* Make space for the default version. */
5323 if (info->create_default_symver)
5325 size += sizeof (Elf_External_Verdef);
5329 for (t = verdefs; t != NULL; t = t->next)
5331 struct bfd_elf_version_deps *n;
5333 size += sizeof (Elf_External_Verdef);
5334 size += sizeof (Elf_External_Verdaux);
5337 for (n = t->deps; n != NULL; n = n->next)
5338 size += sizeof (Elf_External_Verdaux);
5342 s->contents = bfd_alloc (output_bfd, s->size);
5343 if (s->contents == NULL && s->size != 0)
5346 /* Fill in the version definition section. */
5350 def.vd_version = VER_DEF_CURRENT;
5351 def.vd_flags = VER_FLG_BASE;
5354 if (info->create_default_symver)
5356 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5357 def.vd_next = sizeof (Elf_External_Verdef);
5361 def.vd_aux = sizeof (Elf_External_Verdef);
5362 def.vd_next = (sizeof (Elf_External_Verdef)
5363 + sizeof (Elf_External_Verdaux));
5366 if (soname_indx != (bfd_size_type) -1)
5368 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5370 def.vd_hash = bfd_elf_hash (soname);
5371 defaux.vda_name = soname_indx;
5378 name = lbasename (output_bfd->filename);
5379 def.vd_hash = bfd_elf_hash (name);
5380 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5382 if (indx == (bfd_size_type) -1)
5384 defaux.vda_name = indx;
5386 defaux.vda_next = 0;
5388 _bfd_elf_swap_verdef_out (output_bfd, &def,
5389 (Elf_External_Verdef *) p);
5390 p += sizeof (Elf_External_Verdef);
5391 if (info->create_default_symver)
5393 /* Add a symbol representing this version. */
5395 if (! (_bfd_generic_link_add_one_symbol
5396 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5398 get_elf_backend_data (dynobj)->collect, &bh)))
5400 h = (struct elf_link_hash_entry *) bh;
5403 h->type = STT_OBJECT;
5404 h->verinfo.vertree = NULL;
5406 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5409 /* Create a duplicate of the base version with the same
5410 aux block, but different flags. */
5413 def.vd_aux = sizeof (Elf_External_Verdef);
5415 def.vd_next = (sizeof (Elf_External_Verdef)
5416 + sizeof (Elf_External_Verdaux));
5419 _bfd_elf_swap_verdef_out (output_bfd, &def,
5420 (Elf_External_Verdef *) p);
5421 p += sizeof (Elf_External_Verdef);
5423 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5424 (Elf_External_Verdaux *) p);
5425 p += sizeof (Elf_External_Verdaux);
5427 for (t = verdefs; t != NULL; t = t->next)
5430 struct bfd_elf_version_deps *n;
5433 for (n = t->deps; n != NULL; n = n->next)
5436 /* Add a symbol representing this version. */
5438 if (! (_bfd_generic_link_add_one_symbol
5439 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5441 get_elf_backend_data (dynobj)->collect, &bh)))
5443 h = (struct elf_link_hash_entry *) bh;
5446 h->type = STT_OBJECT;
5447 h->verinfo.vertree = t;
5449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5452 def.vd_version = VER_DEF_CURRENT;
5454 if (t->globals.list == NULL
5455 && t->locals.list == NULL
5457 def.vd_flags |= VER_FLG_WEAK;
5458 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5459 def.vd_cnt = cdeps + 1;
5460 def.vd_hash = bfd_elf_hash (t->name);
5461 def.vd_aux = sizeof (Elf_External_Verdef);
5463 if (t->next != NULL)
5464 def.vd_next = (sizeof (Elf_External_Verdef)
5465 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5467 _bfd_elf_swap_verdef_out (output_bfd, &def,
5468 (Elf_External_Verdef *) p);
5469 p += sizeof (Elf_External_Verdef);
5471 defaux.vda_name = h->dynstr_index;
5472 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5474 defaux.vda_next = 0;
5475 if (t->deps != NULL)
5476 defaux.vda_next = sizeof (Elf_External_Verdaux);
5477 t->name_indx = defaux.vda_name;
5479 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5480 (Elf_External_Verdaux *) p);
5481 p += sizeof (Elf_External_Verdaux);
5483 for (n = t->deps; n != NULL; n = n->next)
5485 if (n->version_needed == NULL)
5487 /* This can happen if there was an error in the
5489 defaux.vda_name = 0;
5493 defaux.vda_name = n->version_needed->name_indx;
5494 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5497 if (n->next == NULL)
5498 defaux.vda_next = 0;
5500 defaux.vda_next = sizeof (Elf_External_Verdaux);
5502 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5503 (Elf_External_Verdaux *) p);
5504 p += sizeof (Elf_External_Verdaux);
5508 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5509 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5512 elf_tdata (output_bfd)->cverdefs = cdefs;
5515 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5517 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5520 else if (info->flags & DF_BIND_NOW)
5522 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5528 if (info->executable)
5529 info->flags_1 &= ~ (DF_1_INITFIRST
5532 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5536 /* Work out the size of the version reference section. */
5538 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5539 BFD_ASSERT (s != NULL);
5541 struct elf_find_verdep_info sinfo;
5543 sinfo.output_bfd = output_bfd;
5545 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5546 if (sinfo.vers == 0)
5548 sinfo.failed = FALSE;
5550 elf_link_hash_traverse (elf_hash_table (info),
5551 _bfd_elf_link_find_version_dependencies,
5554 if (elf_tdata (output_bfd)->verref == NULL)
5555 s->flags |= SEC_EXCLUDE;
5558 Elf_Internal_Verneed *t;
5563 /* Build the version definition section. */
5566 for (t = elf_tdata (output_bfd)->verref;
5570 Elf_Internal_Vernaux *a;
5572 size += sizeof (Elf_External_Verneed);
5574 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5575 size += sizeof (Elf_External_Vernaux);
5579 s->contents = bfd_alloc (output_bfd, s->size);
5580 if (s->contents == NULL)
5584 for (t = elf_tdata (output_bfd)->verref;
5589 Elf_Internal_Vernaux *a;
5593 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5596 t->vn_version = VER_NEED_CURRENT;
5598 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5599 elf_dt_name (t->vn_bfd) != NULL
5600 ? elf_dt_name (t->vn_bfd)
5601 : lbasename (t->vn_bfd->filename),
5603 if (indx == (bfd_size_type) -1)
5606 t->vn_aux = sizeof (Elf_External_Verneed);
5607 if (t->vn_nextref == NULL)
5610 t->vn_next = (sizeof (Elf_External_Verneed)
5611 + caux * sizeof (Elf_External_Vernaux));
5613 _bfd_elf_swap_verneed_out (output_bfd, t,
5614 (Elf_External_Verneed *) p);
5615 p += sizeof (Elf_External_Verneed);
5617 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5619 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5620 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5621 a->vna_nodename, FALSE);
5622 if (indx == (bfd_size_type) -1)
5625 if (a->vna_nextptr == NULL)
5628 a->vna_next = sizeof (Elf_External_Vernaux);
5630 _bfd_elf_swap_vernaux_out (output_bfd, a,
5631 (Elf_External_Vernaux *) p);
5632 p += sizeof (Elf_External_Vernaux);
5636 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5637 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5640 elf_tdata (output_bfd)->cverrefs = crefs;
5644 if ((elf_tdata (output_bfd)->cverrefs == 0
5645 && elf_tdata (output_bfd)->cverdefs == 0)
5646 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5647 §ion_sym_count) == 0)
5649 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5650 s->flags |= SEC_EXCLUDE;
5657 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5659 if (!is_elf_hash_table (info->hash))
5662 if (elf_hash_table (info)->dynamic_sections_created)
5665 const struct elf_backend_data *bed;
5667 bfd_size_type dynsymcount;
5668 unsigned long section_sym_count;
5669 size_t bucketcount = 0;
5670 size_t hash_entry_size;
5671 unsigned int dtagcount;
5673 dynobj = elf_hash_table (info)->dynobj;
5675 /* Assign dynsym indicies. In a shared library we generate a
5676 section symbol for each output section, which come first.
5677 Next come all of the back-end allocated local dynamic syms,
5678 followed by the rest of the global symbols. */
5680 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5681 §ion_sym_count);
5683 /* Work out the size of the symbol version section. */
5684 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5685 BFD_ASSERT (s != NULL);
5686 if (dynsymcount != 0
5687 && (s->flags & SEC_EXCLUDE) == 0)
5689 s->size = dynsymcount * sizeof (Elf_External_Versym);
5690 s->contents = bfd_zalloc (output_bfd, s->size);
5691 if (s->contents == NULL)
5694 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5698 /* Set the size of the .dynsym and .hash sections. We counted
5699 the number of dynamic symbols in elf_link_add_object_symbols.
5700 We will build the contents of .dynsym and .hash when we build
5701 the final symbol table, because until then we do not know the
5702 correct value to give the symbols. We built the .dynstr
5703 section as we went along in elf_link_add_object_symbols. */
5704 s = bfd_get_section_by_name (dynobj, ".dynsym");
5705 BFD_ASSERT (s != NULL);
5706 bed = get_elf_backend_data (output_bfd);
5707 s->size = dynsymcount * bed->s->sizeof_sym;
5709 if (dynsymcount != 0)
5711 s->contents = bfd_alloc (output_bfd, s->size);
5712 if (s->contents == NULL)
5715 /* The first entry in .dynsym is a dummy symbol.
5716 Clear all the section syms, in case we don't output them all. */
5717 ++section_sym_count;
5718 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5721 /* Compute the size of the hashing table. As a side effect this
5722 computes the hash values for all the names we export. */
5723 bucketcount = compute_bucket_count (info);
5725 s = bfd_get_section_by_name (dynobj, ".hash");
5726 BFD_ASSERT (s != NULL);
5727 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5728 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5729 s->contents = bfd_zalloc (output_bfd, s->size);
5730 if (s->contents == NULL)
5733 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5734 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5735 s->contents + hash_entry_size);
5737 elf_hash_table (info)->bucketcount = bucketcount;
5739 s = bfd_get_section_by_name (dynobj, ".dynstr");
5740 BFD_ASSERT (s != NULL);
5742 elf_finalize_dynstr (output_bfd, info);
5744 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5746 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5747 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5754 /* Final phase of ELF linker. */
5756 /* A structure we use to avoid passing large numbers of arguments. */
5758 struct elf_final_link_info
5760 /* General link information. */
5761 struct bfd_link_info *info;
5764 /* Symbol string table. */
5765 struct bfd_strtab_hash *symstrtab;
5766 /* .dynsym section. */
5767 asection *dynsym_sec;
5768 /* .hash section. */
5770 /* symbol version section (.gnu.version). */
5771 asection *symver_sec;
5772 /* Buffer large enough to hold contents of any section. */
5774 /* Buffer large enough to hold external relocs of any section. */
5775 void *external_relocs;
5776 /* Buffer large enough to hold internal relocs of any section. */
5777 Elf_Internal_Rela *internal_relocs;
5778 /* Buffer large enough to hold external local symbols of any input
5780 bfd_byte *external_syms;
5781 /* And a buffer for symbol section indices. */
5782 Elf_External_Sym_Shndx *locsym_shndx;
5783 /* Buffer large enough to hold internal local symbols of any input
5785 Elf_Internal_Sym *internal_syms;
5786 /* Array large enough to hold a symbol index for each local symbol
5787 of any input BFD. */
5789 /* Array large enough to hold a section pointer for each local
5790 symbol of any input BFD. */
5791 asection **sections;
5792 /* Buffer to hold swapped out symbols. */
5794 /* And one for symbol section indices. */
5795 Elf_External_Sym_Shndx *symshndxbuf;
5796 /* Number of swapped out symbols in buffer. */
5797 size_t symbuf_count;
5798 /* Number of symbols which fit in symbuf. */
5800 /* And same for symshndxbuf. */
5801 size_t shndxbuf_size;
5804 /* This struct is used to pass information to elf_link_output_extsym. */
5806 struct elf_outext_info
5809 bfd_boolean localsyms;
5810 struct elf_final_link_info *finfo;
5813 /* When performing a relocatable link, the input relocations are
5814 preserved. But, if they reference global symbols, the indices
5815 referenced must be updated. Update all the relocations in
5816 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5819 elf_link_adjust_relocs (bfd *abfd,
5820 Elf_Internal_Shdr *rel_hdr,
5822 struct elf_link_hash_entry **rel_hash)
5825 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5827 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5828 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5829 bfd_vma r_type_mask;
5832 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5834 swap_in = bed->s->swap_reloc_in;
5835 swap_out = bed->s->swap_reloc_out;
5837 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5839 swap_in = bed->s->swap_reloca_in;
5840 swap_out = bed->s->swap_reloca_out;
5845 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5848 if (bed->s->arch_size == 32)
5855 r_type_mask = 0xffffffff;
5859 erela = rel_hdr->contents;
5860 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5862 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5865 if (*rel_hash == NULL)
5868 BFD_ASSERT ((*rel_hash)->indx >= 0);
5870 (*swap_in) (abfd, erela, irela);
5871 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5872 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5873 | (irela[j].r_info & r_type_mask));
5874 (*swap_out) (abfd, irela, erela);
5878 struct elf_link_sort_rela
5884 enum elf_reloc_type_class type;
5885 /* We use this as an array of size int_rels_per_ext_rel. */
5886 Elf_Internal_Rela rela[1];
5890 elf_link_sort_cmp1 (const void *A, const void *B)
5892 const struct elf_link_sort_rela *a = A;
5893 const struct elf_link_sort_rela *b = B;
5894 int relativea, relativeb;
5896 relativea = a->type == reloc_class_relative;
5897 relativeb = b->type == reloc_class_relative;
5899 if (relativea < relativeb)
5901 if (relativea > relativeb)
5903 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5905 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5907 if (a->rela->r_offset < b->rela->r_offset)
5909 if (a->rela->r_offset > b->rela->r_offset)
5915 elf_link_sort_cmp2 (const void *A, const void *B)
5917 const struct elf_link_sort_rela *a = A;
5918 const struct elf_link_sort_rela *b = B;
5921 if (a->u.offset < b->u.offset)
5923 if (a->u.offset > b->u.offset)
5925 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5926 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5931 if (a->rela->r_offset < b->rela->r_offset)
5933 if (a->rela->r_offset > b->rela->r_offset)
5939 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5942 bfd_size_type count, size;
5943 size_t i, ret, sort_elt, ext_size;
5944 bfd_byte *sort, *s_non_relative, *p;
5945 struct elf_link_sort_rela *sq;
5946 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5947 int i2e = bed->s->int_rels_per_ext_rel;
5948 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5949 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5950 struct bfd_link_order *lo;
5953 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5954 if (reldyn == NULL || reldyn->size == 0)
5956 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5957 if (reldyn == NULL || reldyn->size == 0)
5959 ext_size = bed->s->sizeof_rel;
5960 swap_in = bed->s->swap_reloc_in;
5961 swap_out = bed->s->swap_reloc_out;
5965 ext_size = bed->s->sizeof_rela;
5966 swap_in = bed->s->swap_reloca_in;
5967 swap_out = bed->s->swap_reloca_out;
5969 count = reldyn->size / ext_size;
5972 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5973 if (lo->type == bfd_indirect_link_order)
5975 asection *o = lo->u.indirect.section;
5979 if (size != reldyn->size)
5982 sort_elt = (sizeof (struct elf_link_sort_rela)
5983 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5984 sort = bfd_zmalloc (sort_elt * count);
5987 (*info->callbacks->warning)
5988 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5992 if (bed->s->arch_size == 32)
5993 r_sym_mask = ~(bfd_vma) 0xff;
5995 r_sym_mask = ~(bfd_vma) 0xffffffff;
5997 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5998 if (lo->type == bfd_indirect_link_order)
6000 bfd_byte *erel, *erelend;
6001 asection *o = lo->u.indirect.section;
6003 if (o->contents == NULL && o->size != 0)
6005 /* This is a reloc section that is being handled as a normal
6006 section. See bfd_section_from_shdr. We can't combine
6007 relocs in this case. */
6012 erelend = o->contents + o->size;
6013 p = sort + o->output_offset / ext_size * sort_elt;
6014 while (erel < erelend)
6016 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6017 (*swap_in) (abfd, erel, s->rela);
6018 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6019 s->u.sym_mask = r_sym_mask;
6025 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6027 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6029 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6030 if (s->type != reloc_class_relative)
6036 sq = (struct elf_link_sort_rela *) s_non_relative;
6037 for (; i < count; i++, p += sort_elt)
6039 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6040 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6042 sp->u.offset = sq->rela->r_offset;
6045 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6047 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6048 if (lo->type == bfd_indirect_link_order)
6050 bfd_byte *erel, *erelend;
6051 asection *o = lo->u.indirect.section;
6054 erelend = o->contents + o->size;
6055 p = sort + o->output_offset / ext_size * sort_elt;
6056 while (erel < erelend)
6058 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6059 (*swap_out) (abfd, s->rela, erel);
6070 /* Flush the output symbols to the file. */
6073 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6074 const struct elf_backend_data *bed)
6076 if (finfo->symbuf_count > 0)
6078 Elf_Internal_Shdr *hdr;
6082 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6083 pos = hdr->sh_offset + hdr->sh_size;
6084 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6085 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6086 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6089 hdr->sh_size += amt;
6090 finfo->symbuf_count = 0;
6096 /* Add a symbol to the output symbol table. */
6099 elf_link_output_sym (struct elf_final_link_info *finfo,
6101 Elf_Internal_Sym *elfsym,
6102 asection *input_sec,
6103 struct elf_link_hash_entry *h)
6106 Elf_External_Sym_Shndx *destshndx;
6107 bfd_boolean (*output_symbol_hook)
6108 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6109 struct elf_link_hash_entry *);
6110 const struct elf_backend_data *bed;
6112 bed = get_elf_backend_data (finfo->output_bfd);
6113 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6114 if (output_symbol_hook != NULL)
6116 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6120 if (name == NULL || *name == '\0')
6121 elfsym->st_name = 0;
6122 else if (input_sec->flags & SEC_EXCLUDE)
6123 elfsym->st_name = 0;
6126 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6128 if (elfsym->st_name == (unsigned long) -1)
6132 if (finfo->symbuf_count >= finfo->symbuf_size)
6134 if (! elf_link_flush_output_syms (finfo, bed))
6138 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6139 destshndx = finfo->symshndxbuf;
6140 if (destshndx != NULL)
6142 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6146 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6147 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6148 if (destshndx == NULL)
6150 memset ((char *) destshndx + amt, 0, amt);
6151 finfo->shndxbuf_size *= 2;
6153 destshndx += bfd_get_symcount (finfo->output_bfd);
6156 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6157 finfo->symbuf_count += 1;
6158 bfd_get_symcount (finfo->output_bfd) += 1;
6163 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6164 allowing an unsatisfied unversioned symbol in the DSO to match a
6165 versioned symbol that would normally require an explicit version.
6166 We also handle the case that a DSO references a hidden symbol
6167 which may be satisfied by a versioned symbol in another DSO. */
6170 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6171 const struct elf_backend_data *bed,
6172 struct elf_link_hash_entry *h)
6175 struct elf_link_loaded_list *loaded;
6177 if (!is_elf_hash_table (info->hash))
6180 switch (h->root.type)
6186 case bfd_link_hash_undefined:
6187 case bfd_link_hash_undefweak:
6188 abfd = h->root.u.undef.abfd;
6189 if ((abfd->flags & DYNAMIC) == 0
6190 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6194 case bfd_link_hash_defined:
6195 case bfd_link_hash_defweak:
6196 abfd = h->root.u.def.section->owner;
6199 case bfd_link_hash_common:
6200 abfd = h->root.u.c.p->section->owner;
6203 BFD_ASSERT (abfd != NULL);
6205 for (loaded = elf_hash_table (info)->loaded;
6207 loaded = loaded->next)
6210 Elf_Internal_Shdr *hdr;
6211 bfd_size_type symcount;
6212 bfd_size_type extsymcount;
6213 bfd_size_type extsymoff;
6214 Elf_Internal_Shdr *versymhdr;
6215 Elf_Internal_Sym *isym;
6216 Elf_Internal_Sym *isymend;
6217 Elf_Internal_Sym *isymbuf;
6218 Elf_External_Versym *ever;
6219 Elf_External_Versym *extversym;
6221 input = loaded->abfd;
6223 /* We check each DSO for a possible hidden versioned definition. */
6225 || (input->flags & DYNAMIC) == 0
6226 || elf_dynversym (input) == 0)
6229 hdr = &elf_tdata (input)->dynsymtab_hdr;
6231 symcount = hdr->sh_size / bed->s->sizeof_sym;
6232 if (elf_bad_symtab (input))
6234 extsymcount = symcount;
6239 extsymcount = symcount - hdr->sh_info;
6240 extsymoff = hdr->sh_info;
6243 if (extsymcount == 0)
6246 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6248 if (isymbuf == NULL)
6251 /* Read in any version definitions. */
6252 versymhdr = &elf_tdata (input)->dynversym_hdr;
6253 extversym = bfd_malloc (versymhdr->sh_size);
6254 if (extversym == NULL)
6257 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6258 || (bfd_bread (extversym, versymhdr->sh_size, input)
6259 != versymhdr->sh_size))
6267 ever = extversym + extsymoff;
6268 isymend = isymbuf + extsymcount;
6269 for (isym = isymbuf; isym < isymend; isym++, ever++)
6272 Elf_Internal_Versym iver;
6273 unsigned short version_index;
6275 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6276 || isym->st_shndx == SHN_UNDEF)
6279 name = bfd_elf_string_from_elf_section (input,
6282 if (strcmp (name, h->root.root.string) != 0)
6285 _bfd_elf_swap_versym_in (input, ever, &iver);
6287 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6289 /* If we have a non-hidden versioned sym, then it should
6290 have provided a definition for the undefined sym. */
6294 version_index = iver.vs_vers & VERSYM_VERSION;
6295 if (version_index == 1 || version_index == 2)
6297 /* This is the base or first version. We can use it. */
6311 /* Add an external symbol to the symbol table. This is called from
6312 the hash table traversal routine. When generating a shared object,
6313 we go through the symbol table twice. The first time we output
6314 anything that might have been forced to local scope in a version
6315 script. The second time we output the symbols that are still
6319 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6321 struct elf_outext_info *eoinfo = data;
6322 struct elf_final_link_info *finfo = eoinfo->finfo;
6324 Elf_Internal_Sym sym;
6325 asection *input_sec;
6326 const struct elf_backend_data *bed;
6328 if (h->root.type == bfd_link_hash_warning)
6330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6331 if (h->root.type == bfd_link_hash_new)
6335 /* Decide whether to output this symbol in this pass. */
6336 if (eoinfo->localsyms)
6338 if (!h->forced_local)
6343 if (h->forced_local)
6347 bed = get_elf_backend_data (finfo->output_bfd);
6349 if (h->root.type == bfd_link_hash_undefined)
6351 /* If we have an undefined symbol reference here then it must have
6352 come from a shared library that is being linked in. (Undefined
6353 references in regular files have already been handled). */
6354 bfd_boolean ignore_undef = FALSE;
6356 /* Some symbols may be special in that the fact that they're
6357 undefined can be safely ignored - let backend determine that. */
6358 if (bed->elf_backend_ignore_undef_symbol)
6359 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
6361 /* If we are reporting errors for this situation then do so now. */
6362 if (ignore_undef == FALSE
6365 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6366 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6368 if (! (finfo->info->callbacks->undefined_symbol
6369 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6370 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6372 eoinfo->failed = TRUE;
6378 /* We should also warn if a forced local symbol is referenced from
6379 shared libraries. */
6380 if (! finfo->info->relocatable
6381 && (! finfo->info->shared)
6386 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6388 (*_bfd_error_handler)
6389 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6391 h->root.u.def.section == bfd_abs_section_ptr
6392 ? finfo->output_bfd : h->root.u.def.section->owner,
6393 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6395 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6396 ? "hidden" : "local",
6397 h->root.root.string);
6398 eoinfo->failed = TRUE;
6402 /* We don't want to output symbols that have never been mentioned by
6403 a regular file, or that we have been told to strip. However, if
6404 h->indx is set to -2, the symbol is used by a reloc and we must
6408 else if ((h->def_dynamic
6410 || h->root.type == bfd_link_hash_new)
6414 else if (finfo->info->strip == strip_all)
6416 else if (finfo->info->strip == strip_some
6417 && bfd_hash_lookup (finfo->info->keep_hash,
6418 h->root.root.string, FALSE, FALSE) == NULL)
6420 else if (finfo->info->strip_discarded
6421 && (h->root.type == bfd_link_hash_defined
6422 || h->root.type == bfd_link_hash_defweak)
6423 && elf_discarded_section (h->root.u.def.section))
6428 /* If we're stripping it, and it's not a dynamic symbol, there's
6429 nothing else to do unless it is a forced local symbol. */
6432 && !h->forced_local)
6436 sym.st_size = h->size;
6437 sym.st_other = h->other;
6438 if (h->forced_local)
6439 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6440 else if (h->root.type == bfd_link_hash_undefweak
6441 || h->root.type == bfd_link_hash_defweak)
6442 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6444 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6446 switch (h->root.type)
6449 case bfd_link_hash_new:
6450 case bfd_link_hash_warning:
6454 case bfd_link_hash_undefined:
6455 case bfd_link_hash_undefweak:
6456 input_sec = bfd_und_section_ptr;
6457 sym.st_shndx = SHN_UNDEF;
6460 case bfd_link_hash_defined:
6461 case bfd_link_hash_defweak:
6463 input_sec = h->root.u.def.section;
6464 if (input_sec->output_section != NULL)
6467 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6468 input_sec->output_section);
6469 if (sym.st_shndx == SHN_BAD)
6471 (*_bfd_error_handler)
6472 (_("%B: could not find output section %A for input section %A"),
6473 finfo->output_bfd, input_sec->output_section, input_sec);
6474 eoinfo->failed = TRUE;
6478 /* ELF symbols in relocatable files are section relative,
6479 but in nonrelocatable files they are virtual
6481 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6482 if (! finfo->info->relocatable)
6484 sym.st_value += input_sec->output_section->vma;
6485 if (h->type == STT_TLS)
6487 /* STT_TLS symbols are relative to PT_TLS segment
6489 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6490 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6496 BFD_ASSERT (input_sec->owner == NULL
6497 || (input_sec->owner->flags & DYNAMIC) != 0);
6498 sym.st_shndx = SHN_UNDEF;
6499 input_sec = bfd_und_section_ptr;
6504 case bfd_link_hash_common:
6505 input_sec = h->root.u.c.p->section;
6506 sym.st_shndx = bed->common_section_index (input_sec);
6507 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6510 case bfd_link_hash_indirect:
6511 /* These symbols are created by symbol versioning. They point
6512 to the decorated version of the name. For example, if the
6513 symbol foo@@GNU_1.2 is the default, which should be used when
6514 foo is used with no version, then we add an indirect symbol
6515 foo which points to foo@@GNU_1.2. We ignore these symbols,
6516 since the indirected symbol is already in the hash table. */
6520 /* Give the processor backend a chance to tweak the symbol value,
6521 and also to finish up anything that needs to be done for this
6522 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6523 forced local syms when non-shared is due to a historical quirk. */
6524 if ((h->dynindx != -1
6526 && ((finfo->info->shared
6527 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6528 || h->root.type != bfd_link_hash_undefweak))
6529 || !h->forced_local)
6530 && elf_hash_table (finfo->info)->dynamic_sections_created)
6532 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6533 (finfo->output_bfd, finfo->info, h, &sym)))
6535 eoinfo->failed = TRUE;
6540 /* If we are marking the symbol as undefined, and there are no
6541 non-weak references to this symbol from a regular object, then
6542 mark the symbol as weak undefined; if there are non-weak
6543 references, mark the symbol as strong. We can't do this earlier,
6544 because it might not be marked as undefined until the
6545 finish_dynamic_symbol routine gets through with it. */
6546 if (sym.st_shndx == SHN_UNDEF
6548 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6549 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6553 if (h->ref_regular_nonweak)
6554 bindtype = STB_GLOBAL;
6556 bindtype = STB_WEAK;
6557 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6560 /* If a non-weak symbol with non-default visibility is not defined
6561 locally, it is a fatal error. */
6562 if (! finfo->info->relocatable
6563 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6564 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6565 && h->root.type == bfd_link_hash_undefined
6568 (*_bfd_error_handler)
6569 (_("%B: %s symbol `%s' isn't defined"),
6571 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6573 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6574 ? "internal" : "hidden",
6575 h->root.root.string);
6576 eoinfo->failed = TRUE;
6580 /* If this symbol should be put in the .dynsym section, then put it
6581 there now. We already know the symbol index. We also fill in
6582 the entry in the .hash section. */
6583 if (h->dynindx != -1
6584 && elf_hash_table (finfo->info)->dynamic_sections_created)
6588 size_t hash_entry_size;
6589 bfd_byte *bucketpos;
6593 sym.st_name = h->dynstr_index;
6594 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6595 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6597 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6598 bucket = h->u.elf_hash_value % bucketcount;
6600 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6601 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6602 + (bucket + 2) * hash_entry_size);
6603 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6604 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6605 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6606 ((bfd_byte *) finfo->hash_sec->contents
6607 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6609 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6611 Elf_Internal_Versym iversym;
6612 Elf_External_Versym *eversym;
6614 if (!h->def_regular)
6616 if (h->verinfo.verdef == NULL)
6617 iversym.vs_vers = 0;
6619 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6623 if (h->verinfo.vertree == NULL)
6624 iversym.vs_vers = 1;
6626 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6627 if (finfo->info->create_default_symver)
6632 iversym.vs_vers |= VERSYM_HIDDEN;
6634 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6635 eversym += h->dynindx;
6636 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6640 /* If we're stripping it, then it was just a dynamic symbol, and
6641 there's nothing else to do. */
6642 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6645 h->indx = bfd_get_symcount (finfo->output_bfd);
6647 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6649 eoinfo->failed = TRUE;
6656 /* Return TRUE if special handling is done for relocs in SEC against
6657 symbols defined in discarded sections. */
6660 elf_section_ignore_discarded_relocs (asection *sec)
6662 const struct elf_backend_data *bed;
6664 switch (sec->sec_info_type)
6666 case ELF_INFO_TYPE_STABS:
6667 case ELF_INFO_TYPE_EH_FRAME:
6673 bed = get_elf_backend_data (sec->owner);
6674 if (bed->elf_backend_ignore_discarded_relocs != NULL
6675 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6681 /* Return a mask saying how ld should treat relocations in SEC against
6682 symbols defined in discarded sections. If this function returns
6683 COMPLAIN set, ld will issue a warning message. If this function
6684 returns PRETEND set, and the discarded section was link-once and the
6685 same size as the kept link-once section, ld will pretend that the
6686 symbol was actually defined in the kept section. Otherwise ld will
6687 zero the reloc (at least that is the intent, but some cooperation by
6688 the target dependent code is needed, particularly for REL targets). */
6691 _bfd_elf_default_action_discarded (asection *sec)
6693 if (sec->flags & SEC_DEBUGGING)
6696 if (strcmp (".eh_frame", sec->name) == 0)
6699 if (strcmp (".gcc_except_table", sec->name) == 0)
6702 return COMPLAIN | PRETEND;
6705 /* Find a match between a section and a member of a section group. */
6708 match_group_member (asection *sec, asection *group)
6710 asection *first = elf_next_in_group (group);
6711 asection *s = first;
6715 if (bfd_elf_match_symbols_in_sections (s, sec))
6725 /* Check if the kept section of a discarded section SEC can be used
6726 to replace it. Return the replacement if it is OK. Otherwise return
6730 _bfd_elf_check_kept_section (asection *sec)
6734 kept = sec->kept_section;
6737 if (elf_sec_group (sec) != NULL)
6738 kept = match_group_member (sec, kept);
6739 if (kept != NULL && sec->size != kept->size)
6745 /* Link an input file into the linker output file. This function
6746 handles all the sections and relocations of the input file at once.
6747 This is so that we only have to read the local symbols once, and
6748 don't have to keep them in memory. */
6751 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6753 bfd_boolean (*relocate_section)
6754 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6755 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6757 Elf_Internal_Shdr *symtab_hdr;
6760 Elf_Internal_Sym *isymbuf;
6761 Elf_Internal_Sym *isym;
6762 Elf_Internal_Sym *isymend;
6764 asection **ppsection;
6766 const struct elf_backend_data *bed;
6767 bfd_boolean emit_relocs;
6768 struct elf_link_hash_entry **sym_hashes;
6770 output_bfd = finfo->output_bfd;
6771 bed = get_elf_backend_data (output_bfd);
6772 relocate_section = bed->elf_backend_relocate_section;
6774 /* If this is a dynamic object, we don't want to do anything here:
6775 we don't want the local symbols, and we don't want the section
6777 if ((input_bfd->flags & DYNAMIC) != 0)
6780 emit_relocs = (finfo->info->relocatable
6781 || finfo->info->emitrelocations);
6783 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6784 if (elf_bad_symtab (input_bfd))
6786 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6791 locsymcount = symtab_hdr->sh_info;
6792 extsymoff = symtab_hdr->sh_info;
6795 /* Read the local symbols. */
6796 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6797 if (isymbuf == NULL && locsymcount != 0)
6799 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6800 finfo->internal_syms,
6801 finfo->external_syms,
6802 finfo->locsym_shndx);
6803 if (isymbuf == NULL)
6807 /* Find local symbol sections and adjust values of symbols in
6808 SEC_MERGE sections. Write out those local symbols we know are
6809 going into the output file. */
6810 isymend = isymbuf + locsymcount;
6811 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6813 isym++, pindex++, ppsection++)
6817 Elf_Internal_Sym osym;
6821 if (elf_bad_symtab (input_bfd))
6823 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6830 if (isym->st_shndx == SHN_UNDEF)
6831 isec = bfd_und_section_ptr;
6832 else if (isym->st_shndx < SHN_LORESERVE
6833 || isym->st_shndx > SHN_HIRESERVE)
6835 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6837 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6838 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6840 _bfd_merged_section_offset (output_bfd, &isec,
6841 elf_section_data (isec)->sec_info,
6844 else if (isym->st_shndx == SHN_ABS)
6845 isec = bfd_abs_section_ptr;
6846 else if (isym->st_shndx == SHN_COMMON)
6847 isec = bfd_com_section_ptr;
6850 /* Don't attempt to output symbols with st_shnx in the
6851 reserved range other than SHN_ABS and SHN_COMMON. */
6858 /* Don't output the first, undefined, symbol. */
6859 if (ppsection == finfo->sections)
6862 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6864 /* We never output section symbols. Instead, we use the
6865 section symbol of the corresponding section in the output
6870 /* If we are stripping all symbols, we don't want to output this
6872 if (finfo->info->strip == strip_all)
6875 /* If we are discarding all local symbols, we don't want to
6876 output this one. If we are generating a relocatable output
6877 file, then some of the local symbols may be required by
6878 relocs; we output them below as we discover that they are
6880 if (finfo->info->discard == discard_all)
6883 /* If this symbol is defined in a section which we are
6884 discarding, we don't need to keep it. */
6885 if (isym->st_shndx != SHN_UNDEF
6886 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6888 || bfd_section_removed_from_list (output_bfd,
6889 isec->output_section)))
6892 /* Get the name of the symbol. */
6893 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6898 /* See if we are discarding symbols with this name. */
6899 if ((finfo->info->strip == strip_some
6900 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6902 || (((finfo->info->discard == discard_sec_merge
6903 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6904 || finfo->info->discard == discard_l)
6905 && bfd_is_local_label_name (input_bfd, name)))
6908 /* If we get here, we are going to output this symbol. */
6912 /* Adjust the section index for the output file. */
6913 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6914 isec->output_section);
6915 if (osym.st_shndx == SHN_BAD)
6918 *pindex = bfd_get_symcount (output_bfd);
6920 /* ELF symbols in relocatable files are section relative, but
6921 in executable files they are virtual addresses. Note that
6922 this code assumes that all ELF sections have an associated
6923 BFD section with a reasonable value for output_offset; below
6924 we assume that they also have a reasonable value for
6925 output_section. Any special sections must be set up to meet
6926 these requirements. */
6927 osym.st_value += isec->output_offset;
6928 if (! finfo->info->relocatable)
6930 osym.st_value += isec->output_section->vma;
6931 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6933 /* STT_TLS symbols are relative to PT_TLS segment base. */
6934 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6935 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6939 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6943 /* Relocate the contents of each section. */
6944 sym_hashes = elf_sym_hashes (input_bfd);
6945 for (o = input_bfd->sections; o != NULL; o = o->next)
6949 if (! o->linker_mark)
6951 /* This section was omitted from the link. */
6955 if ((o->flags & SEC_HAS_CONTENTS) == 0
6956 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6959 if ((o->flags & SEC_LINKER_CREATED) != 0)
6961 /* Section was created by _bfd_elf_link_create_dynamic_sections
6966 /* Get the contents of the section. They have been cached by a
6967 relaxation routine. Note that o is a section in an input
6968 file, so the contents field will not have been set by any of
6969 the routines which work on output files. */
6970 if (elf_section_data (o)->this_hdr.contents != NULL)
6971 contents = elf_section_data (o)->this_hdr.contents;
6974 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6976 contents = finfo->contents;
6977 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6981 if ((o->flags & SEC_RELOC) != 0)
6983 Elf_Internal_Rela *internal_relocs;
6984 bfd_vma r_type_mask;
6987 /* Get the swapped relocs. */
6989 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6990 finfo->internal_relocs, FALSE);
6991 if (internal_relocs == NULL
6992 && o->reloc_count > 0)
6995 if (bed->s->arch_size == 32)
7002 r_type_mask = 0xffffffff;
7006 /* Run through the relocs looking for any against symbols
7007 from discarded sections and section symbols from
7008 removed link-once sections. Complain about relocs
7009 against discarded sections. Zero relocs against removed
7010 link-once sections. Preserve debug information as much
7012 if (!elf_section_ignore_discarded_relocs (o))
7014 Elf_Internal_Rela *rel, *relend;
7015 unsigned int action = (*bed->action_discarded) (o);
7017 rel = internal_relocs;
7018 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
7019 for ( ; rel < relend; rel++)
7021 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7022 asection **ps, *sec;
7023 struct elf_link_hash_entry *h = NULL;
7024 const char *sym_name;
7026 if (r_symndx == STN_UNDEF)
7029 if (r_symndx >= locsymcount
7030 || (elf_bad_symtab (input_bfd)
7031 && finfo->sections[r_symndx] == NULL))
7033 h = sym_hashes[r_symndx - extsymoff];
7035 /* Badly formatted input files can contain relocs that
7036 reference non-existant symbols. Check here so that
7037 we do not seg fault. */
7042 sprintf_vma (buffer, rel->r_info);
7043 (*_bfd_error_handler)
7044 (_("error: %B contains a reloc (0x%s) for section %A "
7045 "that references a non-existent global symbol"),
7046 input_bfd, o, buffer);
7047 bfd_set_error (bfd_error_bad_value);
7051 while (h->root.type == bfd_link_hash_indirect
7052 || h->root.type == bfd_link_hash_warning)
7053 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7055 if (h->root.type != bfd_link_hash_defined
7056 && h->root.type != bfd_link_hash_defweak)
7059 ps = &h->root.u.def.section;
7060 sym_name = h->root.root.string;
7064 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7065 ps = &finfo->sections[r_symndx];
7066 sym_name = bfd_elf_sym_name (input_bfd,
7071 /* Complain if the definition comes from a
7072 discarded section. */
7073 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7075 BFD_ASSERT (r_symndx != 0);
7076 if (action & COMPLAIN)
7077 (*finfo->info->callbacks->einfo)
7078 (_("%X`%s' referenced in section `%A' of %B: "
7079 "defined in discarded section `%A' of %B\n"),
7080 sym_name, o, input_bfd, sec, sec->owner);
7082 /* Try to do the best we can to support buggy old
7083 versions of gcc. If we've warned, or this is
7084 debugging info, pretend that the symbol is
7085 really defined in the kept linkonce section.
7086 FIXME: This is quite broken. Modifying the
7087 symbol here means we will be changing all later
7088 uses of the symbol, not just in this section.
7089 The only thing that makes this half reasonable
7090 is that we warn in non-debug sections, and
7091 debug sections tend to come after other
7093 if (action & PRETEND)
7097 kept = _bfd_elf_check_kept_section (sec);
7105 /* Remove the symbol reference from the reloc, but
7106 don't kill the reloc completely. This is so that
7107 a zero value will be written into the section,
7108 which may have non-zero contents put there by the
7109 assembler. Zero in things like an eh_frame fde
7110 pc_begin allows stack unwinders to recognize the
7112 rel->r_info &= r_type_mask;
7118 /* Relocate the section by invoking a back end routine.
7120 The back end routine is responsible for adjusting the
7121 section contents as necessary, and (if using Rela relocs
7122 and generating a relocatable output file) adjusting the
7123 reloc addend as necessary.
7125 The back end routine does not have to worry about setting
7126 the reloc address or the reloc symbol index.
7128 The back end routine is given a pointer to the swapped in
7129 internal symbols, and can access the hash table entries
7130 for the external symbols via elf_sym_hashes (input_bfd).
7132 When generating relocatable output, the back end routine
7133 must handle STB_LOCAL/STT_SECTION symbols specially. The
7134 output symbol is going to be a section symbol
7135 corresponding to the output section, which will require
7136 the addend to be adjusted. */
7138 if (! (*relocate_section) (output_bfd, finfo->info,
7139 input_bfd, o, contents,
7147 Elf_Internal_Rela *irela;
7148 Elf_Internal_Rela *irelaend;
7149 bfd_vma last_offset;
7150 struct elf_link_hash_entry **rel_hash;
7151 struct elf_link_hash_entry **rel_hash_list;
7152 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7153 unsigned int next_erel;
7154 bfd_boolean rela_normal;
7156 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7157 rela_normal = (bed->rela_normal
7158 && (input_rel_hdr->sh_entsize
7159 == bed->s->sizeof_rela));
7161 /* Adjust the reloc addresses and symbol indices. */
7163 irela = internal_relocs;
7164 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7165 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7166 + elf_section_data (o->output_section)->rel_count
7167 + elf_section_data (o->output_section)->rel_count2);
7168 rel_hash_list = rel_hash;
7169 last_offset = o->output_offset;
7170 if (!finfo->info->relocatable)
7171 last_offset += o->output_section->vma;
7172 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7174 unsigned long r_symndx;
7176 Elf_Internal_Sym sym;
7178 if (next_erel == bed->s->int_rels_per_ext_rel)
7184 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7187 if (irela->r_offset >= (bfd_vma) -2)
7189 /* This is a reloc for a deleted entry or somesuch.
7190 Turn it into an R_*_NONE reloc, at the same
7191 offset as the last reloc. elf_eh_frame.c and
7192 elf_bfd_discard_info rely on reloc offsets
7194 irela->r_offset = last_offset;
7196 irela->r_addend = 0;
7200 irela->r_offset += o->output_offset;
7202 /* Relocs in an executable have to be virtual addresses. */
7203 if (!finfo->info->relocatable)
7204 irela->r_offset += o->output_section->vma;
7206 last_offset = irela->r_offset;
7208 r_symndx = irela->r_info >> r_sym_shift;
7209 if (r_symndx == STN_UNDEF)
7212 if (r_symndx >= locsymcount
7213 || (elf_bad_symtab (input_bfd)
7214 && finfo->sections[r_symndx] == NULL))
7216 struct elf_link_hash_entry *rh;
7219 /* This is a reloc against a global symbol. We
7220 have not yet output all the local symbols, so
7221 we do not know the symbol index of any global
7222 symbol. We set the rel_hash entry for this
7223 reloc to point to the global hash table entry
7224 for this symbol. The symbol index is then
7225 set at the end of bfd_elf_final_link. */
7226 indx = r_symndx - extsymoff;
7227 rh = elf_sym_hashes (input_bfd)[indx];
7228 while (rh->root.type == bfd_link_hash_indirect
7229 || rh->root.type == bfd_link_hash_warning)
7230 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7232 /* Setting the index to -2 tells
7233 elf_link_output_extsym that this symbol is
7235 BFD_ASSERT (rh->indx < 0);
7243 /* This is a reloc against a local symbol. */
7246 sym = isymbuf[r_symndx];
7247 sec = finfo->sections[r_symndx];
7248 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7250 /* I suppose the backend ought to fill in the
7251 section of any STT_SECTION symbol against a
7252 processor specific section. */
7254 if (bfd_is_abs_section (sec))
7256 else if (sec == NULL || sec->owner == NULL)
7258 bfd_set_error (bfd_error_bad_value);
7263 asection *osec = sec->output_section;
7265 /* If we have discarded a section, the output
7266 section will be the absolute section. In
7267 case of discarded link-once and discarded
7268 SEC_MERGE sections, use the kept section. */
7269 if (bfd_is_abs_section (osec)
7270 && sec->kept_section != NULL
7271 && sec->kept_section->output_section != NULL)
7273 osec = sec->kept_section->output_section;
7274 irela->r_addend -= osec->vma;
7277 if (!bfd_is_abs_section (osec))
7279 r_symndx = osec->target_index;
7280 BFD_ASSERT (r_symndx != 0);
7284 /* Adjust the addend according to where the
7285 section winds up in the output section. */
7287 irela->r_addend += sec->output_offset;
7291 if (finfo->indices[r_symndx] == -1)
7293 unsigned long shlink;
7297 if (finfo->info->strip == strip_all)
7299 /* You can't do ld -r -s. */
7300 bfd_set_error (bfd_error_invalid_operation);
7304 /* This symbol was skipped earlier, but
7305 since it is needed by a reloc, we
7306 must output it now. */
7307 shlink = symtab_hdr->sh_link;
7308 name = (bfd_elf_string_from_elf_section
7309 (input_bfd, shlink, sym.st_name));
7313 osec = sec->output_section;
7315 _bfd_elf_section_from_bfd_section (output_bfd,
7317 if (sym.st_shndx == SHN_BAD)
7320 sym.st_value += sec->output_offset;
7321 if (! finfo->info->relocatable)
7323 sym.st_value += osec->vma;
7324 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7326 /* STT_TLS symbols are relative to PT_TLS
7328 BFD_ASSERT (elf_hash_table (finfo->info)
7330 sym.st_value -= (elf_hash_table (finfo->info)
7335 finfo->indices[r_symndx]
7336 = bfd_get_symcount (output_bfd);
7338 if (! elf_link_output_sym (finfo, name, &sym, sec,
7343 r_symndx = finfo->indices[r_symndx];
7346 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7347 | (irela->r_info & r_type_mask));
7350 /* Swap out the relocs. */
7351 if (input_rel_hdr->sh_size != 0
7352 && !bed->elf_backend_emit_relocs (output_bfd, o,
7358 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7359 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7361 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7362 * bed->s->int_rels_per_ext_rel);
7363 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7364 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7373 /* Write out the modified section contents. */
7374 if (bed->elf_backend_write_section
7375 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7377 /* Section written out. */
7379 else switch (o->sec_info_type)
7381 case ELF_INFO_TYPE_STABS:
7382 if (! (_bfd_write_section_stabs
7384 &elf_hash_table (finfo->info)->stab_info,
7385 o, &elf_section_data (o)->sec_info, contents)))
7388 case ELF_INFO_TYPE_MERGE:
7389 if (! _bfd_write_merged_section (output_bfd, o,
7390 elf_section_data (o)->sec_info))
7393 case ELF_INFO_TYPE_EH_FRAME:
7395 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7402 if (! (o->flags & SEC_EXCLUDE)
7403 && ! bfd_set_section_contents (output_bfd, o->output_section,
7405 (file_ptr) o->output_offset,
7416 /* Generate a reloc when linking an ELF file. This is a reloc
7417 requested by the linker, and does not come from any input file. This
7418 is used to build constructor and destructor tables when linking
7422 elf_reloc_link_order (bfd *output_bfd,
7423 struct bfd_link_info *info,
7424 asection *output_section,
7425 struct bfd_link_order *link_order)
7427 reloc_howto_type *howto;
7431 struct elf_link_hash_entry **rel_hash_ptr;
7432 Elf_Internal_Shdr *rel_hdr;
7433 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7434 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7438 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7441 bfd_set_error (bfd_error_bad_value);
7445 addend = link_order->u.reloc.p->addend;
7447 /* Figure out the symbol index. */
7448 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7449 + elf_section_data (output_section)->rel_count
7450 + elf_section_data (output_section)->rel_count2);
7451 if (link_order->type == bfd_section_reloc_link_order)
7453 indx = link_order->u.reloc.p->u.section->target_index;
7454 BFD_ASSERT (indx != 0);
7455 *rel_hash_ptr = NULL;
7459 struct elf_link_hash_entry *h;
7461 /* Treat a reloc against a defined symbol as though it were
7462 actually against the section. */
7463 h = ((struct elf_link_hash_entry *)
7464 bfd_wrapped_link_hash_lookup (output_bfd, info,
7465 link_order->u.reloc.p->u.name,
7466 FALSE, FALSE, TRUE));
7468 && (h->root.type == bfd_link_hash_defined
7469 || h->root.type == bfd_link_hash_defweak))
7473 section = h->root.u.def.section;
7474 indx = section->output_section->target_index;
7475 *rel_hash_ptr = NULL;
7476 /* It seems that we ought to add the symbol value to the
7477 addend here, but in practice it has already been added
7478 because it was passed to constructor_callback. */
7479 addend += section->output_section->vma + section->output_offset;
7483 /* Setting the index to -2 tells elf_link_output_extsym that
7484 this symbol is used by a reloc. */
7491 if (! ((*info->callbacks->unattached_reloc)
7492 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7498 /* If this is an inplace reloc, we must write the addend into the
7500 if (howto->partial_inplace && addend != 0)
7503 bfd_reloc_status_type rstat;
7506 const char *sym_name;
7508 size = bfd_get_reloc_size (howto);
7509 buf = bfd_zmalloc (size);
7512 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7519 case bfd_reloc_outofrange:
7522 case bfd_reloc_overflow:
7523 if (link_order->type == bfd_section_reloc_link_order)
7524 sym_name = bfd_section_name (output_bfd,
7525 link_order->u.reloc.p->u.section);
7527 sym_name = link_order->u.reloc.p->u.name;
7528 if (! ((*info->callbacks->reloc_overflow)
7529 (info, NULL, sym_name, howto->name, addend, NULL,
7530 NULL, (bfd_vma) 0)))
7537 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7538 link_order->offset, size);
7544 /* The address of a reloc is relative to the section in a
7545 relocatable file, and is a virtual address in an executable
7547 offset = link_order->offset;
7548 if (! info->relocatable)
7549 offset += output_section->vma;
7551 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7553 irel[i].r_offset = offset;
7555 irel[i].r_addend = 0;
7557 if (bed->s->arch_size == 32)
7558 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7560 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7562 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7563 erel = rel_hdr->contents;
7564 if (rel_hdr->sh_type == SHT_REL)
7566 erel += (elf_section_data (output_section)->rel_count
7567 * bed->s->sizeof_rel);
7568 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7572 irel[0].r_addend = addend;
7573 erel += (elf_section_data (output_section)->rel_count
7574 * bed->s->sizeof_rela);
7575 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7578 ++elf_section_data (output_section)->rel_count;
7584 /* Get the output vma of the section pointed to by the sh_link field. */
7587 elf_get_linked_section_vma (struct bfd_link_order *p)
7589 Elf_Internal_Shdr **elf_shdrp;
7593 s = p->u.indirect.section;
7594 elf_shdrp = elf_elfsections (s->owner);
7595 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7596 elfsec = elf_shdrp[elfsec]->sh_link;
7598 The Intel C compiler generates SHT_IA_64_UNWIND with
7599 SHF_LINK_ORDER. But it doesn't set the sh_link or
7600 sh_info fields. Hence we could get the situation
7601 where elfsec is 0. */
7604 const struct elf_backend_data *bed
7605 = get_elf_backend_data (s->owner);
7606 if (bed->link_order_error_handler)
7607 bed->link_order_error_handler
7608 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7613 s = elf_shdrp[elfsec]->bfd_section;
7614 return s->output_section->vma + s->output_offset;
7619 /* Compare two sections based on the locations of the sections they are
7620 linked to. Used by elf_fixup_link_order. */
7623 compare_link_order (const void * a, const void * b)
7628 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7629 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7636 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7637 order as their linked sections. Returns false if this could not be done
7638 because an output section includes both ordered and unordered
7639 sections. Ideally we'd do this in the linker proper. */
7642 elf_fixup_link_order (bfd *abfd, asection *o)
7647 struct bfd_link_order *p;
7649 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7651 struct bfd_link_order **sections;
7652 asection *s, *other_sec, *linkorder_sec;
7656 linkorder_sec = NULL;
7659 for (p = o->map_head.link_order; p != NULL; p = p->next)
7661 if (p->type == bfd_indirect_link_order)
7663 s = p->u.indirect.section;
7665 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7666 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
7667 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) != -1
7668 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7682 if (seen_other && seen_linkorder)
7684 if (other_sec && linkorder_sec)
7685 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7687 linkorder_sec->owner, other_sec,
7690 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7692 bfd_set_error (bfd_error_bad_value);
7697 if (!seen_linkorder)
7700 sections = (struct bfd_link_order **)
7701 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7704 for (p = o->map_head.link_order; p != NULL; p = p->next)
7706 sections[seen_linkorder++] = p;
7708 /* Sort the input sections in the order of their linked section. */
7709 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7710 compare_link_order);
7712 /* Change the offsets of the sections. */
7714 for (n = 0; n < seen_linkorder; n++)
7716 s = sections[n]->u.indirect.section;
7717 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7718 s->output_offset = offset;
7719 sections[n]->offset = offset;
7720 offset += sections[n]->size;
7727 /* Do the final step of an ELF link. */
7730 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7732 bfd_boolean dynamic;
7733 bfd_boolean emit_relocs;
7735 struct elf_final_link_info finfo;
7736 register asection *o;
7737 register struct bfd_link_order *p;
7739 bfd_size_type max_contents_size;
7740 bfd_size_type max_external_reloc_size;
7741 bfd_size_type max_internal_reloc_count;
7742 bfd_size_type max_sym_count;
7743 bfd_size_type max_sym_shndx_count;
7745 Elf_Internal_Sym elfsym;
7747 Elf_Internal_Shdr *symtab_hdr;
7748 Elf_Internal_Shdr *symtab_shndx_hdr;
7749 Elf_Internal_Shdr *symstrtab_hdr;
7750 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7751 struct elf_outext_info eoinfo;
7753 size_t relativecount = 0;
7754 asection *reldyn = 0;
7757 if (! is_elf_hash_table (info->hash))
7761 abfd->flags |= DYNAMIC;
7763 dynamic = elf_hash_table (info)->dynamic_sections_created;
7764 dynobj = elf_hash_table (info)->dynobj;
7766 emit_relocs = (info->relocatable
7767 || info->emitrelocations
7768 || bed->elf_backend_emit_relocs);
7771 finfo.output_bfd = abfd;
7772 finfo.symstrtab = _bfd_elf_stringtab_init ();
7773 if (finfo.symstrtab == NULL)
7778 finfo.dynsym_sec = NULL;
7779 finfo.hash_sec = NULL;
7780 finfo.symver_sec = NULL;
7784 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7785 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7786 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7787 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7788 /* Note that it is OK if symver_sec is NULL. */
7791 finfo.contents = NULL;
7792 finfo.external_relocs = NULL;
7793 finfo.internal_relocs = NULL;
7794 finfo.external_syms = NULL;
7795 finfo.locsym_shndx = NULL;
7796 finfo.internal_syms = NULL;
7797 finfo.indices = NULL;
7798 finfo.sections = NULL;
7799 finfo.symbuf = NULL;
7800 finfo.symshndxbuf = NULL;
7801 finfo.symbuf_count = 0;
7802 finfo.shndxbuf_size = 0;
7804 /* Count up the number of relocations we will output for each output
7805 section, so that we know the sizes of the reloc sections. We
7806 also figure out some maximum sizes. */
7807 max_contents_size = 0;
7808 max_external_reloc_size = 0;
7809 max_internal_reloc_count = 0;
7811 max_sym_shndx_count = 0;
7813 for (o = abfd->sections; o != NULL; o = o->next)
7815 struct bfd_elf_section_data *esdo = elf_section_data (o);
7818 for (p = o->map_head.link_order; p != NULL; p = p->next)
7820 unsigned int reloc_count = 0;
7821 struct bfd_elf_section_data *esdi = NULL;
7822 unsigned int *rel_count1;
7824 if (p->type == bfd_section_reloc_link_order
7825 || p->type == bfd_symbol_reloc_link_order)
7827 else if (p->type == bfd_indirect_link_order)
7831 sec = p->u.indirect.section;
7832 esdi = elf_section_data (sec);
7834 /* Mark all sections which are to be included in the
7835 link. This will normally be every section. We need
7836 to do this so that we can identify any sections which
7837 the linker has decided to not include. */
7838 sec->linker_mark = TRUE;
7840 if (sec->flags & SEC_MERGE)
7843 if (info->relocatable || info->emitrelocations)
7844 reloc_count = sec->reloc_count;
7845 else if (bed->elf_backend_count_relocs)
7847 Elf_Internal_Rela * relocs;
7849 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7852 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7854 if (elf_section_data (o)->relocs != relocs)
7858 if (sec->rawsize > max_contents_size)
7859 max_contents_size = sec->rawsize;
7860 if (sec->size > max_contents_size)
7861 max_contents_size = sec->size;
7863 /* We are interested in just local symbols, not all
7865 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7866 && (sec->owner->flags & DYNAMIC) == 0)
7870 if (elf_bad_symtab (sec->owner))
7871 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7872 / bed->s->sizeof_sym);
7874 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7876 if (sym_count > max_sym_count)
7877 max_sym_count = sym_count;
7879 if (sym_count > max_sym_shndx_count
7880 && elf_symtab_shndx (sec->owner) != 0)
7881 max_sym_shndx_count = sym_count;
7883 if ((sec->flags & SEC_RELOC) != 0)
7887 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7888 if (ext_size > max_external_reloc_size)
7889 max_external_reloc_size = ext_size;
7890 if (sec->reloc_count > max_internal_reloc_count)
7891 max_internal_reloc_count = sec->reloc_count;
7896 if (reloc_count == 0)
7899 o->reloc_count += reloc_count;
7901 /* MIPS may have a mix of REL and RELA relocs on sections.
7902 To support this curious ABI we keep reloc counts in
7903 elf_section_data too. We must be careful to add the
7904 relocations from the input section to the right output
7905 count. FIXME: Get rid of one count. We have
7906 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7907 rel_count1 = &esdo->rel_count;
7910 bfd_boolean same_size;
7911 bfd_size_type entsize1;
7913 entsize1 = esdi->rel_hdr.sh_entsize;
7914 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7915 || entsize1 == bed->s->sizeof_rela);
7916 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7919 rel_count1 = &esdo->rel_count2;
7921 if (esdi->rel_hdr2 != NULL)
7923 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7924 unsigned int alt_count;
7925 unsigned int *rel_count2;
7927 BFD_ASSERT (entsize2 != entsize1
7928 && (entsize2 == bed->s->sizeof_rel
7929 || entsize2 == bed->s->sizeof_rela));
7931 rel_count2 = &esdo->rel_count2;
7933 rel_count2 = &esdo->rel_count;
7935 /* The following is probably too simplistic if the
7936 backend counts output relocs unusually. */
7937 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7938 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7939 *rel_count2 += alt_count;
7940 reloc_count -= alt_count;
7943 *rel_count1 += reloc_count;
7946 if (o->reloc_count > 0)
7947 o->flags |= SEC_RELOC;
7950 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7951 set it (this is probably a bug) and if it is set
7952 assign_section_numbers will create a reloc section. */
7953 o->flags &=~ SEC_RELOC;
7956 /* If the SEC_ALLOC flag is not set, force the section VMA to
7957 zero. This is done in elf_fake_sections as well, but forcing
7958 the VMA to 0 here will ensure that relocs against these
7959 sections are handled correctly. */
7960 if ((o->flags & SEC_ALLOC) == 0
7961 && ! o->user_set_vma)
7965 if (! info->relocatable && merged)
7966 elf_link_hash_traverse (elf_hash_table (info),
7967 _bfd_elf_link_sec_merge_syms, abfd);
7969 /* Figure out the file positions for everything but the symbol table
7970 and the relocs. We set symcount to force assign_section_numbers
7971 to create a symbol table. */
7972 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7973 BFD_ASSERT (! abfd->output_has_begun);
7974 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7977 /* Set sizes, and assign file positions for reloc sections. */
7978 for (o = abfd->sections; o != NULL; o = o->next)
7980 if ((o->flags & SEC_RELOC) != 0)
7982 if (!(_bfd_elf_link_size_reloc_section
7983 (abfd, &elf_section_data (o)->rel_hdr, o)))
7986 if (elf_section_data (o)->rel_hdr2
7987 && !(_bfd_elf_link_size_reloc_section
7988 (abfd, elf_section_data (o)->rel_hdr2, o)))
7992 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7993 to count upwards while actually outputting the relocations. */
7994 elf_section_data (o)->rel_count = 0;
7995 elf_section_data (o)->rel_count2 = 0;
7998 _bfd_elf_assign_file_positions_for_relocs (abfd);
8000 /* We have now assigned file positions for all the sections except
8001 .symtab and .strtab. We start the .symtab section at the current
8002 file position, and write directly to it. We build the .strtab
8003 section in memory. */
8004 bfd_get_symcount (abfd) = 0;
8005 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8006 /* sh_name is set in prep_headers. */
8007 symtab_hdr->sh_type = SHT_SYMTAB;
8008 /* sh_flags, sh_addr and sh_size all start off zero. */
8009 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8010 /* sh_link is set in assign_section_numbers. */
8011 /* sh_info is set below. */
8012 /* sh_offset is set just below. */
8013 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
8015 off = elf_tdata (abfd)->next_file_pos;
8016 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
8018 /* Note that at this point elf_tdata (abfd)->next_file_pos is
8019 incorrect. We do not yet know the size of the .symtab section.
8020 We correct next_file_pos below, after we do know the size. */
8022 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8023 continuously seeking to the right position in the file. */
8024 if (! info->keep_memory || max_sym_count < 20)
8025 finfo.symbuf_size = 20;
8027 finfo.symbuf_size = max_sym_count;
8028 amt = finfo.symbuf_size;
8029 amt *= bed->s->sizeof_sym;
8030 finfo.symbuf = bfd_malloc (amt);
8031 if (finfo.symbuf == NULL)
8033 if (elf_numsections (abfd) > SHN_LORESERVE)
8035 /* Wild guess at number of output symbols. realloc'd as needed. */
8036 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8037 finfo.shndxbuf_size = amt;
8038 amt *= sizeof (Elf_External_Sym_Shndx);
8039 finfo.symshndxbuf = bfd_zmalloc (amt);
8040 if (finfo.symshndxbuf == NULL)
8044 /* Start writing out the symbol table. The first symbol is always a
8046 if (info->strip != strip_all
8049 elfsym.st_value = 0;
8052 elfsym.st_other = 0;
8053 elfsym.st_shndx = SHN_UNDEF;
8054 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8059 /* Output a symbol for each section. We output these even if we are
8060 discarding local symbols, since they are used for relocs. These
8061 symbols have no names. We store the index of each one in the
8062 index field of the section, so that we can find it again when
8063 outputting relocs. */
8064 if (info->strip != strip_all
8068 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8069 elfsym.st_other = 0;
8070 for (i = 1; i < elf_numsections (abfd); i++)
8072 o = bfd_section_from_elf_index (abfd, i);
8074 o->target_index = bfd_get_symcount (abfd);
8075 elfsym.st_shndx = i;
8076 if (info->relocatable || o == NULL)
8077 elfsym.st_value = 0;
8079 elfsym.st_value = o->vma;
8080 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8082 if (i == SHN_LORESERVE - 1)
8083 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8087 /* Allocate some memory to hold information read in from the input
8089 if (max_contents_size != 0)
8091 finfo.contents = bfd_malloc (max_contents_size);
8092 if (finfo.contents == NULL)
8096 if (max_external_reloc_size != 0)
8098 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8099 if (finfo.external_relocs == NULL)
8103 if (max_internal_reloc_count != 0)
8105 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8106 amt *= sizeof (Elf_Internal_Rela);
8107 finfo.internal_relocs = bfd_malloc (amt);
8108 if (finfo.internal_relocs == NULL)
8112 if (max_sym_count != 0)
8114 amt = max_sym_count * bed->s->sizeof_sym;
8115 finfo.external_syms = bfd_malloc (amt);
8116 if (finfo.external_syms == NULL)
8119 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8120 finfo.internal_syms = bfd_malloc (amt);
8121 if (finfo.internal_syms == NULL)
8124 amt = max_sym_count * sizeof (long);
8125 finfo.indices = bfd_malloc (amt);
8126 if (finfo.indices == NULL)
8129 amt = max_sym_count * sizeof (asection *);
8130 finfo.sections = bfd_malloc (amt);
8131 if (finfo.sections == NULL)
8135 if (max_sym_shndx_count != 0)
8137 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8138 finfo.locsym_shndx = bfd_malloc (amt);
8139 if (finfo.locsym_shndx == NULL)
8143 if (elf_hash_table (info)->tls_sec)
8145 bfd_vma base, end = 0;
8148 for (sec = elf_hash_table (info)->tls_sec;
8149 sec && (sec->flags & SEC_THREAD_LOCAL);
8152 bfd_size_type size = sec->size;
8155 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8157 struct bfd_link_order *o = sec->map_tail.link_order;
8159 size = o->offset + o->size;
8161 end = sec->vma + size;
8163 base = elf_hash_table (info)->tls_sec->vma;
8164 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8165 elf_hash_table (info)->tls_size = end - base;
8168 /* Reorder SHF_LINK_ORDER sections. */
8169 for (o = abfd->sections; o != NULL; o = o->next)
8171 if (!elf_fixup_link_order (abfd, o))
8175 /* Since ELF permits relocations to be against local symbols, we
8176 must have the local symbols available when we do the relocations.
8177 Since we would rather only read the local symbols once, and we
8178 would rather not keep them in memory, we handle all the
8179 relocations for a single input file at the same time.
8181 Unfortunately, there is no way to know the total number of local
8182 symbols until we have seen all of them, and the local symbol
8183 indices precede the global symbol indices. This means that when
8184 we are generating relocatable output, and we see a reloc against
8185 a global symbol, we can not know the symbol index until we have
8186 finished examining all the local symbols to see which ones we are
8187 going to output. To deal with this, we keep the relocations in
8188 memory, and don't output them until the end of the link. This is
8189 an unfortunate waste of memory, but I don't see a good way around
8190 it. Fortunately, it only happens when performing a relocatable
8191 link, which is not the common case. FIXME: If keep_memory is set
8192 we could write the relocs out and then read them again; I don't
8193 know how bad the memory loss will be. */
8195 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8196 sub->output_has_begun = FALSE;
8197 for (o = abfd->sections; o != NULL; o = o->next)
8199 for (p = o->map_head.link_order; p != NULL; p = p->next)
8201 if (p->type == bfd_indirect_link_order
8202 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8203 == bfd_target_elf_flavour)
8204 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8206 if (! sub->output_has_begun)
8208 if (! elf_link_input_bfd (&finfo, sub))
8210 sub->output_has_begun = TRUE;
8213 else if (p->type == bfd_section_reloc_link_order
8214 || p->type == bfd_symbol_reloc_link_order)
8216 if (! elf_reloc_link_order (abfd, info, o, p))
8221 if (! _bfd_default_link_order (abfd, info, o, p))
8227 /* Output any global symbols that got converted to local in a
8228 version script or due to symbol visibility. We do this in a
8229 separate step since ELF requires all local symbols to appear
8230 prior to any global symbols. FIXME: We should only do this if
8231 some global symbols were, in fact, converted to become local.
8232 FIXME: Will this work correctly with the Irix 5 linker? */
8233 eoinfo.failed = FALSE;
8234 eoinfo.finfo = &finfo;
8235 eoinfo.localsyms = TRUE;
8236 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8241 /* That wrote out all the local symbols. Finish up the symbol table
8242 with the global symbols. Even if we want to strip everything we
8243 can, we still need to deal with those global symbols that got
8244 converted to local in a version script. */
8246 /* The sh_info field records the index of the first non local symbol. */
8247 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8250 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8252 Elf_Internal_Sym sym;
8253 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8254 long last_local = 0;
8256 /* Write out the section symbols for the output sections. */
8257 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8263 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8266 for (s = abfd->sections; s != NULL; s = s->next)
8272 dynindx = elf_section_data (s)->dynindx;
8275 indx = elf_section_data (s)->this_idx;
8276 BFD_ASSERT (indx > 0);
8277 sym.st_shndx = indx;
8278 sym.st_value = s->vma;
8279 dest = dynsym + dynindx * bed->s->sizeof_sym;
8280 if (last_local < dynindx)
8281 last_local = dynindx;
8282 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8286 /* Write out the local dynsyms. */
8287 if (elf_hash_table (info)->dynlocal)
8289 struct elf_link_local_dynamic_entry *e;
8290 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8295 sym.st_size = e->isym.st_size;
8296 sym.st_other = e->isym.st_other;
8298 /* Copy the internal symbol as is.
8299 Note that we saved a word of storage and overwrote
8300 the original st_name with the dynstr_index. */
8303 if (e->isym.st_shndx != SHN_UNDEF
8304 && (e->isym.st_shndx < SHN_LORESERVE
8305 || e->isym.st_shndx > SHN_HIRESERVE))
8307 s = bfd_section_from_elf_index (e->input_bfd,
8311 elf_section_data (s->output_section)->this_idx;
8312 sym.st_value = (s->output_section->vma
8314 + e->isym.st_value);
8317 if (last_local < e->dynindx)
8318 last_local = e->dynindx;
8320 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8321 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8325 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8329 /* We get the global symbols from the hash table. */
8330 eoinfo.failed = FALSE;
8331 eoinfo.localsyms = FALSE;
8332 eoinfo.finfo = &finfo;
8333 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8338 /* If backend needs to output some symbols not present in the hash
8339 table, do it now. */
8340 if (bed->elf_backend_output_arch_syms)
8342 typedef bfd_boolean (*out_sym_func)
8343 (void *, const char *, Elf_Internal_Sym *, asection *,
8344 struct elf_link_hash_entry *);
8346 if (! ((*bed->elf_backend_output_arch_syms)
8347 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8351 /* Flush all symbols to the file. */
8352 if (! elf_link_flush_output_syms (&finfo, bed))
8355 /* Now we know the size of the symtab section. */
8356 off += symtab_hdr->sh_size;
8358 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8359 if (symtab_shndx_hdr->sh_name != 0)
8361 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8362 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8363 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8364 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8365 symtab_shndx_hdr->sh_size = amt;
8367 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8370 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8371 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8376 /* Finish up and write out the symbol string table (.strtab)
8378 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8379 /* sh_name was set in prep_headers. */
8380 symstrtab_hdr->sh_type = SHT_STRTAB;
8381 symstrtab_hdr->sh_flags = 0;
8382 symstrtab_hdr->sh_addr = 0;
8383 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8384 symstrtab_hdr->sh_entsize = 0;
8385 symstrtab_hdr->sh_link = 0;
8386 symstrtab_hdr->sh_info = 0;
8387 /* sh_offset is set just below. */
8388 symstrtab_hdr->sh_addralign = 1;
8390 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8391 elf_tdata (abfd)->next_file_pos = off;
8393 if (bfd_get_symcount (abfd) > 0)
8395 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8396 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8400 /* Adjust the relocs to have the correct symbol indices. */
8401 for (o = abfd->sections; o != NULL; o = o->next)
8403 if ((o->flags & SEC_RELOC) == 0)
8406 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8407 elf_section_data (o)->rel_count,
8408 elf_section_data (o)->rel_hashes);
8409 if (elf_section_data (o)->rel_hdr2 != NULL)
8410 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8411 elf_section_data (o)->rel_count2,
8412 (elf_section_data (o)->rel_hashes
8413 + elf_section_data (o)->rel_count));
8415 /* Set the reloc_count field to 0 to prevent write_relocs from
8416 trying to swap the relocs out itself. */
8420 if (dynamic && info->combreloc && dynobj != NULL)
8421 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8423 /* If we are linking against a dynamic object, or generating a
8424 shared library, finish up the dynamic linking information. */
8427 bfd_byte *dyncon, *dynconend;
8429 /* Fix up .dynamic entries. */
8430 o = bfd_get_section_by_name (dynobj, ".dynamic");
8431 BFD_ASSERT (o != NULL);
8433 dyncon = o->contents;
8434 dynconend = o->contents + o->size;
8435 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8437 Elf_Internal_Dyn dyn;
8441 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8448 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8450 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8452 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8453 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8456 dyn.d_un.d_val = relativecount;
8463 name = info->init_function;
8466 name = info->fini_function;
8469 struct elf_link_hash_entry *h;
8471 h = elf_link_hash_lookup (elf_hash_table (info), name,
8472 FALSE, FALSE, TRUE);
8474 && (h->root.type == bfd_link_hash_defined
8475 || h->root.type == bfd_link_hash_defweak))
8477 dyn.d_un.d_val = h->root.u.def.value;
8478 o = h->root.u.def.section;
8479 if (o->output_section != NULL)
8480 dyn.d_un.d_val += (o->output_section->vma
8481 + o->output_offset);
8484 /* The symbol is imported from another shared
8485 library and does not apply to this one. */
8493 case DT_PREINIT_ARRAYSZ:
8494 name = ".preinit_array";
8496 case DT_INIT_ARRAYSZ:
8497 name = ".init_array";
8499 case DT_FINI_ARRAYSZ:
8500 name = ".fini_array";
8502 o = bfd_get_section_by_name (abfd, name);
8505 (*_bfd_error_handler)
8506 (_("%B: could not find output section %s"), abfd, name);
8510 (*_bfd_error_handler)
8511 (_("warning: %s section has zero size"), name);
8512 dyn.d_un.d_val = o->size;
8515 case DT_PREINIT_ARRAY:
8516 name = ".preinit_array";
8519 name = ".init_array";
8522 name = ".fini_array";
8535 name = ".gnu.version_d";
8538 name = ".gnu.version_r";
8541 name = ".gnu.version";
8543 o = bfd_get_section_by_name (abfd, name);
8546 (*_bfd_error_handler)
8547 (_("%B: could not find output section %s"), abfd, name);
8550 dyn.d_un.d_ptr = o->vma;
8557 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8562 for (i = 1; i < elf_numsections (abfd); i++)
8564 Elf_Internal_Shdr *hdr;
8566 hdr = elf_elfsections (abfd)[i];
8567 if (hdr->sh_type == type
8568 && (hdr->sh_flags & SHF_ALLOC) != 0)
8570 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8571 dyn.d_un.d_val += hdr->sh_size;
8574 if (dyn.d_un.d_val == 0
8575 || hdr->sh_addr < dyn.d_un.d_val)
8576 dyn.d_un.d_val = hdr->sh_addr;
8582 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8586 /* If we have created any dynamic sections, then output them. */
8589 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8592 for (o = dynobj->sections; o != NULL; o = o->next)
8594 if ((o->flags & SEC_HAS_CONTENTS) == 0
8596 || o->output_section == bfd_abs_section_ptr)
8598 if ((o->flags & SEC_LINKER_CREATED) == 0)
8600 /* At this point, we are only interested in sections
8601 created by _bfd_elf_link_create_dynamic_sections. */
8604 if (elf_hash_table (info)->stab_info.stabstr == o)
8606 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8608 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8610 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8612 if (! bfd_set_section_contents (abfd, o->output_section,
8614 (file_ptr) o->output_offset,
8620 /* The contents of the .dynstr section are actually in a
8622 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8623 if (bfd_seek (abfd, off, SEEK_SET) != 0
8624 || ! _bfd_elf_strtab_emit (abfd,
8625 elf_hash_table (info)->dynstr))
8631 if (info->relocatable)
8633 bfd_boolean failed = FALSE;
8635 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8640 /* If we have optimized stabs strings, output them. */
8641 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8643 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8647 if (info->eh_frame_hdr)
8649 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8653 if (finfo.symstrtab != NULL)
8654 _bfd_stringtab_free (finfo.symstrtab);
8655 if (finfo.contents != NULL)
8656 free (finfo.contents);
8657 if (finfo.external_relocs != NULL)
8658 free (finfo.external_relocs);
8659 if (finfo.internal_relocs != NULL)
8660 free (finfo.internal_relocs);
8661 if (finfo.external_syms != NULL)
8662 free (finfo.external_syms);
8663 if (finfo.locsym_shndx != NULL)
8664 free (finfo.locsym_shndx);
8665 if (finfo.internal_syms != NULL)
8666 free (finfo.internal_syms);
8667 if (finfo.indices != NULL)
8668 free (finfo.indices);
8669 if (finfo.sections != NULL)
8670 free (finfo.sections);
8671 if (finfo.symbuf != NULL)
8672 free (finfo.symbuf);
8673 if (finfo.symshndxbuf != NULL)
8674 free (finfo.symshndxbuf);
8675 for (o = abfd->sections; o != NULL; o = o->next)
8677 if ((o->flags & SEC_RELOC) != 0
8678 && elf_section_data (o)->rel_hashes != NULL)
8679 free (elf_section_data (o)->rel_hashes);
8682 elf_tdata (abfd)->linker = TRUE;
8687 if (finfo.symstrtab != NULL)
8688 _bfd_stringtab_free (finfo.symstrtab);
8689 if (finfo.contents != NULL)
8690 free (finfo.contents);
8691 if (finfo.external_relocs != NULL)
8692 free (finfo.external_relocs);
8693 if (finfo.internal_relocs != NULL)
8694 free (finfo.internal_relocs);
8695 if (finfo.external_syms != NULL)
8696 free (finfo.external_syms);
8697 if (finfo.locsym_shndx != NULL)
8698 free (finfo.locsym_shndx);
8699 if (finfo.internal_syms != NULL)
8700 free (finfo.internal_syms);
8701 if (finfo.indices != NULL)
8702 free (finfo.indices);
8703 if (finfo.sections != NULL)
8704 free (finfo.sections);
8705 if (finfo.symbuf != NULL)
8706 free (finfo.symbuf);
8707 if (finfo.symshndxbuf != NULL)
8708 free (finfo.symshndxbuf);
8709 for (o = abfd->sections; o != NULL; o = o->next)
8711 if ((o->flags & SEC_RELOC) != 0
8712 && elf_section_data (o)->rel_hashes != NULL)
8713 free (elf_section_data (o)->rel_hashes);
8719 /* Garbage collect unused sections. */
8721 /* The mark phase of garbage collection. For a given section, mark
8722 it and any sections in this section's group, and all the sections
8723 which define symbols to which it refers. */
8725 typedef asection * (*gc_mark_hook_fn)
8726 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8727 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8730 _bfd_elf_gc_mark (struct bfd_link_info *info,
8732 gc_mark_hook_fn gc_mark_hook)
8736 asection *group_sec;
8740 /* Mark all the sections in the group. */
8741 group_sec = elf_section_data (sec)->next_in_group;
8742 if (group_sec && !group_sec->gc_mark)
8743 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8746 /* Look through the section relocs. */
8748 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8749 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8751 Elf_Internal_Rela *relstart, *rel, *relend;
8752 Elf_Internal_Shdr *symtab_hdr;
8753 struct elf_link_hash_entry **sym_hashes;
8756 bfd *input_bfd = sec->owner;
8757 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8758 Elf_Internal_Sym *isym = NULL;
8761 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8762 sym_hashes = elf_sym_hashes (input_bfd);
8764 /* Read the local symbols. */
8765 if (elf_bad_symtab (input_bfd))
8767 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8771 extsymoff = nlocsyms = symtab_hdr->sh_info;
8773 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8774 if (isym == NULL && nlocsyms != 0)
8776 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8782 /* Read the relocations. */
8783 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8785 if (relstart == NULL)
8790 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8792 if (bed->s->arch_size == 32)
8797 for (rel = relstart; rel < relend; rel++)
8799 unsigned long r_symndx;
8801 struct elf_link_hash_entry *h;
8803 r_symndx = rel->r_info >> r_sym_shift;
8807 if (r_symndx >= nlocsyms
8808 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8810 h = sym_hashes[r_symndx - extsymoff];
8811 while (h->root.type == bfd_link_hash_indirect
8812 || h->root.type == bfd_link_hash_warning)
8813 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8814 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8818 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8821 if (rsec && !rsec->gc_mark)
8823 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8826 rsec->gc_mark_from_eh = 1;
8827 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8836 if (elf_section_data (sec)->relocs != relstart)
8839 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8841 if (! info->keep_memory)
8844 symtab_hdr->contents = (unsigned char *) isym;
8851 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8853 struct elf_gc_sweep_symbol_info {
8854 struct bfd_link_info *info;
8855 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8860 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8862 if (h->root.type == bfd_link_hash_warning)
8863 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8865 if ((h->root.type == bfd_link_hash_defined
8866 || h->root.type == bfd_link_hash_defweak)
8867 && !h->root.u.def.section->gc_mark
8868 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8870 struct elf_gc_sweep_symbol_info *inf = data;
8871 (*inf->hide_symbol) (inf->info, h, TRUE);
8877 /* The sweep phase of garbage collection. Remove all garbage sections. */
8879 typedef bfd_boolean (*gc_sweep_hook_fn)
8880 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8883 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8886 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8887 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8888 unsigned long section_sym_count;
8889 struct elf_gc_sweep_symbol_info sweep_info;
8891 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8895 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8898 for (o = sub->sections; o != NULL; o = o->next)
8900 /* Keep debug and special sections. */
8901 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8902 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8908 /* Skip sweeping sections already excluded. */
8909 if (o->flags & SEC_EXCLUDE)
8912 /* Since this is early in the link process, it is simple
8913 to remove a section from the output. */
8914 o->flags |= SEC_EXCLUDE;
8916 /* But we also have to update some of the relocation
8917 info we collected before. */
8919 && (o->flags & SEC_RELOC) != 0
8920 && o->reloc_count > 0
8921 && !bfd_is_abs_section (o->output_section))
8923 Elf_Internal_Rela *internal_relocs;
8927 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8929 if (internal_relocs == NULL)
8932 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8934 if (elf_section_data (o)->relocs != internal_relocs)
8935 free (internal_relocs);
8943 /* Remove the symbols that were in the swept sections from the dynamic
8944 symbol table. GCFIXME: Anyone know how to get them out of the
8945 static symbol table as well? */
8946 sweep_info.info = info;
8947 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
8948 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
8951 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
8955 /* Propagate collected vtable information. This is called through
8956 elf_link_hash_traverse. */
8959 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8961 if (h->root.type == bfd_link_hash_warning)
8962 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8964 /* Those that are not vtables. */
8965 if (h->vtable == NULL || h->vtable->parent == NULL)
8968 /* Those vtables that do not have parents, we cannot merge. */
8969 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8972 /* If we've already been done, exit. */
8973 if (h->vtable->used && h->vtable->used[-1])
8976 /* Make sure the parent's table is up to date. */
8977 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8979 if (h->vtable->used == NULL)
8981 /* None of this table's entries were referenced. Re-use the
8983 h->vtable->used = h->vtable->parent->vtable->used;
8984 h->vtable->size = h->vtable->parent->vtable->size;
8989 bfd_boolean *cu, *pu;
8991 /* Or the parent's entries into ours. */
8992 cu = h->vtable->used;
8994 pu = h->vtable->parent->vtable->used;
8997 const struct elf_backend_data *bed;
8998 unsigned int log_file_align;
9000 bed = get_elf_backend_data (h->root.u.def.section->owner);
9001 log_file_align = bed->s->log_file_align;
9002 n = h->vtable->parent->vtable->size >> log_file_align;
9017 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9020 bfd_vma hstart, hend;
9021 Elf_Internal_Rela *relstart, *relend, *rel;
9022 const struct elf_backend_data *bed;
9023 unsigned int log_file_align;
9025 if (h->root.type == bfd_link_hash_warning)
9026 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9028 /* Take care of both those symbols that do not describe vtables as
9029 well as those that are not loaded. */
9030 if (h->vtable == NULL || h->vtable->parent == NULL)
9033 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9034 || h->root.type == bfd_link_hash_defweak);
9036 sec = h->root.u.def.section;
9037 hstart = h->root.u.def.value;
9038 hend = hstart + h->size;
9040 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9042 return *(bfd_boolean *) okp = FALSE;
9043 bed = get_elf_backend_data (sec->owner);
9044 log_file_align = bed->s->log_file_align;
9046 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9048 for (rel = relstart; rel < relend; ++rel)
9049 if (rel->r_offset >= hstart && rel->r_offset < hend)
9051 /* If the entry is in use, do nothing. */
9053 && (rel->r_offset - hstart) < h->vtable->size)
9055 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9056 if (h->vtable->used[entry])
9059 /* Otherwise, kill it. */
9060 rel->r_offset = rel->r_info = rel->r_addend = 0;
9066 /* Mark sections containing dynamically referenced symbols. When
9067 building shared libraries, we must assume that any visible symbol is
9071 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9073 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9075 if (h->root.type == bfd_link_hash_warning)
9076 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9078 if ((h->root.type == bfd_link_hash_defined
9079 || h->root.type == bfd_link_hash_defweak)
9081 || (!info->executable
9083 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9084 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9085 h->root.u.def.section->flags |= SEC_KEEP;
9090 /* Do mark and sweep of unused sections. */
9093 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9095 bfd_boolean ok = TRUE;
9097 asection * (*gc_mark_hook)
9098 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9099 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9100 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9102 if (!bed->can_gc_sections
9103 || info->relocatable
9104 || info->emitrelocations
9105 || !is_elf_hash_table (info->hash))
9107 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9111 /* Apply transitive closure to the vtable entry usage info. */
9112 elf_link_hash_traverse (elf_hash_table (info),
9113 elf_gc_propagate_vtable_entries_used,
9118 /* Kill the vtable relocations that were not used. */
9119 elf_link_hash_traverse (elf_hash_table (info),
9120 elf_gc_smash_unused_vtentry_relocs,
9125 /* Mark dynamically referenced symbols. */
9126 if (elf_hash_table (info)->dynamic_sections_created)
9127 elf_link_hash_traverse (elf_hash_table (info),
9128 bed->gc_mark_dynamic_ref,
9131 /* Grovel through relocs to find out who stays ... */
9132 gc_mark_hook = bed->gc_mark_hook;
9133 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9137 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9140 for (o = sub->sections; o != NULL; o = o->next)
9141 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9142 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9146 /* ... again for sections marked from eh_frame. */
9147 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9151 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9154 /* Keep .gcc_except_table.* if the associated .text.* is
9155 marked. This isn't very nice, but the proper solution,
9156 splitting .eh_frame up and using comdat doesn't pan out
9157 easily due to needing special relocs to handle the
9158 difference of two symbols in separate sections.
9159 Don't keep code sections referenced by .eh_frame. */
9160 for (o = sub->sections; o != NULL; o = o->next)
9161 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9163 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9169 len = strlen (o->name + 18) + 1;
9170 fn_name = bfd_malloc (len + 6);
9171 if (fn_name == NULL)
9173 memcpy (fn_name, ".text.", 6);
9174 memcpy (fn_name + 6, o->name + 18, len);
9175 fn_text = bfd_get_section_by_name (sub, fn_name);
9177 if (fn_text == NULL || !fn_text->gc_mark)
9181 /* If not using specially named exception table section,
9182 then keep whatever we are using. */
9183 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9188 /* ... and mark SEC_EXCLUDE for those that go. */
9189 return elf_gc_sweep (abfd, info);
9192 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9195 bfd_elf_gc_record_vtinherit (bfd *abfd,
9197 struct elf_link_hash_entry *h,
9200 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9201 struct elf_link_hash_entry **search, *child;
9202 bfd_size_type extsymcount;
9203 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9205 /* The sh_info field of the symtab header tells us where the
9206 external symbols start. We don't care about the local symbols at
9208 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9209 if (!elf_bad_symtab (abfd))
9210 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9212 sym_hashes = elf_sym_hashes (abfd);
9213 sym_hashes_end = sym_hashes + extsymcount;
9215 /* Hunt down the child symbol, which is in this section at the same
9216 offset as the relocation. */
9217 for (search = sym_hashes; search != sym_hashes_end; ++search)
9219 if ((child = *search) != NULL
9220 && (child->root.type == bfd_link_hash_defined
9221 || child->root.type == bfd_link_hash_defweak)
9222 && child->root.u.def.section == sec
9223 && child->root.u.def.value == offset)
9227 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9228 abfd, sec, (unsigned long) offset);
9229 bfd_set_error (bfd_error_invalid_operation);
9235 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9241 /* This *should* only be the absolute section. It could potentially
9242 be that someone has defined a non-global vtable though, which
9243 would be bad. It isn't worth paging in the local symbols to be
9244 sure though; that case should simply be handled by the assembler. */
9246 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9249 child->vtable->parent = h;
9254 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9257 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9258 asection *sec ATTRIBUTE_UNUSED,
9259 struct elf_link_hash_entry *h,
9262 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9263 unsigned int log_file_align = bed->s->log_file_align;
9267 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9272 if (addend >= h->vtable->size)
9274 size_t size, bytes, file_align;
9275 bfd_boolean *ptr = h->vtable->used;
9277 /* While the symbol is undefined, we have to be prepared to handle
9279 file_align = 1 << log_file_align;
9280 if (h->root.type == bfd_link_hash_undefined)
9281 size = addend + file_align;
9287 /* Oops! We've got a reference past the defined end of
9288 the table. This is probably a bug -- shall we warn? */
9289 size = addend + file_align;
9292 size = (size + file_align - 1) & -file_align;
9294 /* Allocate one extra entry for use as a "done" flag for the
9295 consolidation pass. */
9296 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9300 ptr = bfd_realloc (ptr - 1, bytes);
9306 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9307 * sizeof (bfd_boolean));
9308 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9312 ptr = bfd_zmalloc (bytes);
9317 /* And arrange for that done flag to be at index -1. */
9318 h->vtable->used = ptr + 1;
9319 h->vtable->size = size;
9322 h->vtable->used[addend >> log_file_align] = TRUE;
9327 struct alloc_got_off_arg {
9329 unsigned int got_elt_size;
9332 /* We need a special top-level link routine to convert got reference counts
9333 to real got offsets. */
9336 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9338 struct alloc_got_off_arg *gofarg = arg;
9340 if (h->root.type == bfd_link_hash_warning)
9341 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9343 if (h->got.refcount > 0)
9345 h->got.offset = gofarg->gotoff;
9346 gofarg->gotoff += gofarg->got_elt_size;
9349 h->got.offset = (bfd_vma) -1;
9354 /* And an accompanying bit to work out final got entry offsets once
9355 we're done. Should be called from final_link. */
9358 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9359 struct bfd_link_info *info)
9362 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9364 unsigned int got_elt_size = bed->s->arch_size / 8;
9365 struct alloc_got_off_arg gofarg;
9367 if (! is_elf_hash_table (info->hash))
9370 /* The GOT offset is relative to the .got section, but the GOT header is
9371 put into the .got.plt section, if the backend uses it. */
9372 if (bed->want_got_plt)
9375 gotoff = bed->got_header_size;
9377 /* Do the local .got entries first. */
9378 for (i = info->input_bfds; i; i = i->link_next)
9380 bfd_signed_vma *local_got;
9381 bfd_size_type j, locsymcount;
9382 Elf_Internal_Shdr *symtab_hdr;
9384 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9387 local_got = elf_local_got_refcounts (i);
9391 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9392 if (elf_bad_symtab (i))
9393 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9395 locsymcount = symtab_hdr->sh_info;
9397 for (j = 0; j < locsymcount; ++j)
9399 if (local_got[j] > 0)
9401 local_got[j] = gotoff;
9402 gotoff += got_elt_size;
9405 local_got[j] = (bfd_vma) -1;
9409 /* Then the global .got entries. .plt refcounts are handled by
9410 adjust_dynamic_symbol */
9411 gofarg.gotoff = gotoff;
9412 gofarg.got_elt_size = got_elt_size;
9413 elf_link_hash_traverse (elf_hash_table (info),
9414 elf_gc_allocate_got_offsets,
9419 /* Many folk need no more in the way of final link than this, once
9420 got entry reference counting is enabled. */
9423 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9425 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9428 /* Invoke the regular ELF backend linker to do all the work. */
9429 return bfd_elf_final_link (abfd, info);
9433 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9435 struct elf_reloc_cookie *rcookie = cookie;
9437 if (rcookie->bad_symtab)
9438 rcookie->rel = rcookie->rels;
9440 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9442 unsigned long r_symndx;
9444 if (! rcookie->bad_symtab)
9445 if (rcookie->rel->r_offset > offset)
9447 if (rcookie->rel->r_offset != offset)
9450 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9451 if (r_symndx == SHN_UNDEF)
9454 if (r_symndx >= rcookie->locsymcount
9455 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9457 struct elf_link_hash_entry *h;
9459 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9461 while (h->root.type == bfd_link_hash_indirect
9462 || h->root.type == bfd_link_hash_warning)
9463 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9465 if ((h->root.type == bfd_link_hash_defined
9466 || h->root.type == bfd_link_hash_defweak)
9467 && elf_discarded_section (h->root.u.def.section))
9474 /* It's not a relocation against a global symbol,
9475 but it could be a relocation against a local
9476 symbol for a discarded section. */
9478 Elf_Internal_Sym *isym;
9480 /* Need to: get the symbol; get the section. */
9481 isym = &rcookie->locsyms[r_symndx];
9482 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9484 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9485 if (isec != NULL && elf_discarded_section (isec))
9494 /* Discard unneeded references to discarded sections.
9495 Returns TRUE if any section's size was changed. */
9496 /* This function assumes that the relocations are in sorted order,
9497 which is true for all known assemblers. */
9500 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9502 struct elf_reloc_cookie cookie;
9503 asection *stab, *eh;
9504 Elf_Internal_Shdr *symtab_hdr;
9505 const struct elf_backend_data *bed;
9508 bfd_boolean ret = FALSE;
9510 if (info->traditional_format
9511 || !is_elf_hash_table (info->hash))
9514 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9516 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9519 bed = get_elf_backend_data (abfd);
9521 if ((abfd->flags & DYNAMIC) != 0)
9524 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9525 if (info->relocatable
9528 || bfd_is_abs_section (eh->output_section))))
9531 stab = bfd_get_section_by_name (abfd, ".stab");
9534 || bfd_is_abs_section (stab->output_section)
9535 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9540 && bed->elf_backend_discard_info == NULL)
9543 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9545 cookie.sym_hashes = elf_sym_hashes (abfd);
9546 cookie.bad_symtab = elf_bad_symtab (abfd);
9547 if (cookie.bad_symtab)
9549 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9550 cookie.extsymoff = 0;
9554 cookie.locsymcount = symtab_hdr->sh_info;
9555 cookie.extsymoff = symtab_hdr->sh_info;
9558 if (bed->s->arch_size == 32)
9559 cookie.r_sym_shift = 8;
9561 cookie.r_sym_shift = 32;
9563 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9564 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9566 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9567 cookie.locsymcount, 0,
9569 if (cookie.locsyms == NULL)
9576 count = stab->reloc_count;
9578 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9580 if (cookie.rels != NULL)
9582 cookie.rel = cookie.rels;
9583 cookie.relend = cookie.rels;
9584 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9585 if (_bfd_discard_section_stabs (abfd, stab,
9586 elf_section_data (stab)->sec_info,
9587 bfd_elf_reloc_symbol_deleted_p,
9590 if (elf_section_data (stab)->relocs != cookie.rels)
9598 count = eh->reloc_count;
9600 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9602 cookie.rel = cookie.rels;
9603 cookie.relend = cookie.rels;
9604 if (cookie.rels != NULL)
9605 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9607 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9608 bfd_elf_reloc_symbol_deleted_p,
9612 if (cookie.rels != NULL
9613 && elf_section_data (eh)->relocs != cookie.rels)
9617 if (bed->elf_backend_discard_info != NULL
9618 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9621 if (cookie.locsyms != NULL
9622 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9624 if (! info->keep_memory)
9625 free (cookie.locsyms);
9627 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9631 if (info->eh_frame_hdr
9632 && !info->relocatable
9633 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9640 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9643 const char *name, *p;
9644 struct bfd_section_already_linked *l;
9645 struct bfd_section_already_linked_hash_entry *already_linked_list;
9648 /* A single member comdat group section may be discarded by a
9649 linkonce section. See below. */
9650 if (sec->output_section == bfd_abs_section_ptr)
9655 /* Check if it belongs to a section group. */
9656 group = elf_sec_group (sec);
9658 /* Return if it isn't a linkonce section nor a member of a group. A
9659 comdat group section also has SEC_LINK_ONCE set. */
9660 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9665 /* If this is the member of a single member comdat group, check if
9666 the group should be discarded. */
9667 if (elf_next_in_group (sec) == sec
9668 && (group->flags & SEC_LINK_ONCE) != 0)
9674 /* FIXME: When doing a relocatable link, we may have trouble
9675 copying relocations in other sections that refer to local symbols
9676 in the section being discarded. Those relocations will have to
9677 be converted somehow; as of this writing I'm not sure that any of
9678 the backends handle that correctly.
9680 It is tempting to instead not discard link once sections when
9681 doing a relocatable link (technically, they should be discarded
9682 whenever we are building constructors). However, that fails,
9683 because the linker winds up combining all the link once sections
9684 into a single large link once section, which defeats the purpose
9685 of having link once sections in the first place.
9687 Also, not merging link once sections in a relocatable link
9688 causes trouble for MIPS ELF, which relies on link once semantics
9689 to handle the .reginfo section correctly. */
9691 name = bfd_get_section_name (abfd, sec);
9693 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9694 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9699 already_linked_list = bfd_section_already_linked_table_lookup (p);
9701 for (l = already_linked_list->entry; l != NULL; l = l->next)
9703 /* We may have 3 different sections on the list: group section,
9704 comdat section and linkonce section. SEC may be a linkonce or
9705 group section. We match a group section with a group section,
9706 a linkonce section with a linkonce section, and ignore comdat
9708 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9709 && strcmp (name, l->sec->name) == 0
9710 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9712 /* The section has already been linked. See if we should
9714 switch (flags & SEC_LINK_DUPLICATES)
9719 case SEC_LINK_DUPLICATES_DISCARD:
9722 case SEC_LINK_DUPLICATES_ONE_ONLY:
9723 (*_bfd_error_handler)
9724 (_("%B: ignoring duplicate section `%A'"),
9728 case SEC_LINK_DUPLICATES_SAME_SIZE:
9729 if (sec->size != l->sec->size)
9730 (*_bfd_error_handler)
9731 (_("%B: duplicate section `%A' has different size"),
9735 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9736 if (sec->size != l->sec->size)
9737 (*_bfd_error_handler)
9738 (_("%B: duplicate section `%A' has different size"),
9740 else if (sec->size != 0)
9742 bfd_byte *sec_contents, *l_sec_contents;
9744 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9745 (*_bfd_error_handler)
9746 (_("%B: warning: could not read contents of section `%A'"),
9748 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9750 (*_bfd_error_handler)
9751 (_("%B: warning: could not read contents of section `%A'"),
9752 l->sec->owner, l->sec);
9753 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9754 (*_bfd_error_handler)
9755 (_("%B: warning: duplicate section `%A' has different contents"),
9759 free (sec_contents);
9761 free (l_sec_contents);
9766 /* Set the output_section field so that lang_add_section
9767 does not create a lang_input_section structure for this
9768 section. Since there might be a symbol in the section
9769 being discarded, we must retain a pointer to the section
9770 which we are really going to use. */
9771 sec->output_section = bfd_abs_section_ptr;
9772 sec->kept_section = l->sec;
9774 if (flags & SEC_GROUP)
9776 asection *first = elf_next_in_group (sec);
9777 asection *s = first;
9781 s->output_section = bfd_abs_section_ptr;
9782 /* Record which group discards it. */
9783 s->kept_section = l->sec;
9784 s = elf_next_in_group (s);
9785 /* These lists are circular. */
9797 /* If this is the member of a single member comdat group and the
9798 group hasn't be discarded, we check if it matches a linkonce
9799 section. We only record the discarded comdat group. Otherwise
9800 the undiscarded group will be discarded incorrectly later since
9801 itself has been recorded. */
9802 for (l = already_linked_list->entry; l != NULL; l = l->next)
9803 if ((l->sec->flags & SEC_GROUP) == 0
9804 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9805 && bfd_elf_match_symbols_in_sections (l->sec,
9806 elf_next_in_group (sec)))
9808 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9809 elf_next_in_group (sec)->kept_section = l->sec;
9810 group->output_section = bfd_abs_section_ptr;
9817 /* There is no direct match. But for linkonce section, we should
9818 check if there is a match with comdat group member. We always
9819 record the linkonce section, discarded or not. */
9820 for (l = already_linked_list->entry; l != NULL; l = l->next)
9821 if (l->sec->flags & SEC_GROUP)
9823 asection *first = elf_next_in_group (l->sec);
9826 && elf_next_in_group (first) == first
9827 && bfd_elf_match_symbols_in_sections (first, sec))
9829 sec->output_section = bfd_abs_section_ptr;
9830 sec->kept_section = l->sec;
9835 /* This is the first section with this name. Record it. */
9836 bfd_section_already_linked_table_insert (already_linked_list, sec);
9840 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9842 return sym->st_shndx == SHN_COMMON;
9846 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9852 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9854 return bfd_com_section_ptr;