1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 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 (struct bfd_link_info *info,
431 struct elf_link_hash_entry *h;
432 struct elf_link_hash_table *htab;
434 if (!is_elf_hash_table (info->hash))
437 htab = elf_hash_table (info);
438 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
442 /* Since we're defining the symbol, don't let it seem to have not
443 been defined. record_dynamic_symbol and size_dynamic_sections
444 may depend on this. */
445 if (h->root.type == bfd_link_hash_undefweak
446 || h->root.type == bfd_link_hash_undefined)
448 h->root.type = bfd_link_hash_new;
449 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
450 bfd_link_repair_undef_list (&htab->root);
453 if (h->root.type == bfd_link_hash_new)
456 /* If this symbol is being provided by the linker script, and it is
457 currently defined by a dynamic object, but not by a regular
458 object, then mark it as undefined so that the generic linker will
459 force the correct value. */
463 h->root.type = bfd_link_hash_undefined;
465 /* If this symbol is not being provided by the linker script, and it is
466 currently defined by a dynamic object, but not by a regular object,
467 then clear out any version information because the symbol will not be
468 associated with the dynamic object any more. */
472 h->verinfo.verdef = NULL;
476 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
478 if (!info->relocatable
480 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
481 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
487 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
490 if (! bfd_elf_link_record_dynamic_symbol (info, h))
493 /* If this is a weak defined symbol, and we know a corresponding
494 real symbol from the same dynamic object, make sure the real
495 symbol is also made into a dynamic symbol. */
496 if (h->u.weakdef != NULL
497 && h->u.weakdef->dynindx == -1)
499 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
507 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
508 success, and 2 on a failure caused by attempting to record a symbol
509 in a discarded section, eg. a discarded link-once section symbol. */
512 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
517 struct elf_link_local_dynamic_entry *entry;
518 struct elf_link_hash_table *eht;
519 struct elf_strtab_hash *dynstr;
520 unsigned long dynstr_index;
522 Elf_External_Sym_Shndx eshndx;
523 char esym[sizeof (Elf64_External_Sym)];
525 if (! is_elf_hash_table (info->hash))
528 /* See if the entry exists already. */
529 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
530 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
533 amt = sizeof (*entry);
534 entry = bfd_alloc (input_bfd, amt);
538 /* Go find the symbol, so that we can find it's name. */
539 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
540 1, input_indx, &entry->isym, esym, &eshndx))
542 bfd_release (input_bfd, entry);
546 if (entry->isym.st_shndx != SHN_UNDEF
547 && (entry->isym.st_shndx < SHN_LORESERVE
548 || entry->isym.st_shndx > SHN_HIRESERVE))
552 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
553 if (s == NULL || bfd_is_abs_section (s->output_section))
555 /* We can still bfd_release here as nothing has done another
556 bfd_alloc. We can't do this later in this function. */
557 bfd_release (input_bfd, entry);
562 name = (bfd_elf_string_from_elf_section
563 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
564 entry->isym.st_name));
566 dynstr = elf_hash_table (info)->dynstr;
569 /* Create a strtab to hold the dynamic symbol names. */
570 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
575 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
576 if (dynstr_index == (unsigned long) -1)
578 entry->isym.st_name = dynstr_index;
580 eht = elf_hash_table (info);
582 entry->next = eht->dynlocal;
583 eht->dynlocal = entry;
584 entry->input_bfd = input_bfd;
585 entry->input_indx = input_indx;
588 /* Whatever binding the symbol had before, it's now local. */
590 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
592 /* The dynindx will be set at the end of size_dynamic_sections. */
597 /* Return the dynindex of a local dynamic symbol. */
600 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
604 struct elf_link_local_dynamic_entry *e;
606 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
607 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
612 /* This function is used to renumber the dynamic symbols, if some of
613 them are removed because they are marked as local. This is called
614 via elf_link_hash_traverse. */
617 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
620 size_t *count = data;
622 if (h->root.type == bfd_link_hash_warning)
623 h = (struct elf_link_hash_entry *) h->root.u.i.link;
628 if (h->dynindx != -1)
629 h->dynindx = ++(*count);
635 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
636 STB_LOCAL binding. */
639 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
642 size_t *count = data;
644 if (h->root.type == bfd_link_hash_warning)
645 h = (struct elf_link_hash_entry *) h->root.u.i.link;
647 if (!h->forced_local)
650 if (h->dynindx != -1)
651 h->dynindx = ++(*count);
656 /* Return true if the dynamic symbol for a given section should be
657 omitted when creating a shared library. */
659 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
660 struct bfd_link_info *info,
663 switch (elf_section_data (p)->this_hdr.sh_type)
667 /* If sh_type is yet undecided, assume it could be
668 SHT_PROGBITS/SHT_NOBITS. */
670 if (strcmp (p->name, ".got") == 0
671 || strcmp (p->name, ".got.plt") == 0
672 || strcmp (p->name, ".plt") == 0)
675 bfd *dynobj = elf_hash_table (info)->dynobj;
678 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL
679 && (ip->flags & SEC_LINKER_CREATED)
680 && ip->output_section == p)
685 /* There shouldn't be section relative relocations
686 against any other section. */
692 /* Assign dynsym indices. In a shared library we generate a section
693 symbol for each output section, which come first. Next come symbols
694 which have been forced to local binding. Then all of the back-end
695 allocated local dynamic syms, followed by the rest of the global
699 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
700 struct bfd_link_info *info,
701 unsigned long *section_sym_count)
703 unsigned long dynsymcount = 0;
705 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
707 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
709 for (p = output_bfd->sections; p ; p = p->next)
710 if ((p->flags & SEC_EXCLUDE) == 0
711 && (p->flags & SEC_ALLOC) != 0
712 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
713 elf_section_data (p)->dynindx = ++dynsymcount;
715 *section_sym_count = dynsymcount;
717 elf_link_hash_traverse (elf_hash_table (info),
718 elf_link_renumber_local_hash_table_dynsyms,
721 if (elf_hash_table (info)->dynlocal)
723 struct elf_link_local_dynamic_entry *p;
724 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
725 p->dynindx = ++dynsymcount;
728 elf_link_hash_traverse (elf_hash_table (info),
729 elf_link_renumber_hash_table_dynsyms,
732 /* There is an unused NULL entry at the head of the table which
733 we must account for in our count. Unless there weren't any
734 symbols, which means we'll have no table at all. */
735 if (dynsymcount != 0)
738 elf_hash_table (info)->dynsymcount = dynsymcount;
742 /* This function is called when we want to define a new symbol. It
743 handles the various cases which arise when we find a definition in
744 a dynamic object, or when there is already a definition in a
745 dynamic object. The new symbol is described by NAME, SYM, PSEC,
746 and PVALUE. We set SYM_HASH to the hash table entry. We set
747 OVERRIDE if the old symbol is overriding a new definition. We set
748 TYPE_CHANGE_OK if it is OK for the type to change. We set
749 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
750 change, we mean that we shouldn't warn if the type or size does
751 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
752 object is overridden by a regular object. */
755 _bfd_elf_merge_symbol (bfd *abfd,
756 struct bfd_link_info *info,
758 Elf_Internal_Sym *sym,
761 unsigned int *pold_alignment,
762 struct elf_link_hash_entry **sym_hash,
764 bfd_boolean *override,
765 bfd_boolean *type_change_ok,
766 bfd_boolean *size_change_ok)
768 asection *sec, *oldsec;
769 struct elf_link_hash_entry *h;
770 struct elf_link_hash_entry *flip;
773 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
774 bfd_boolean newweak, oldweak;
775 const struct elf_backend_data *bed;
781 bind = ELF_ST_BIND (sym->st_info);
783 if (! bfd_is_und_section (sec))
784 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
786 h = ((struct elf_link_hash_entry *)
787 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
792 /* This code is for coping with dynamic objects, and is only useful
793 if we are doing an ELF link. */
794 if (info->hash->creator != abfd->xvec)
797 /* For merging, we only care about real symbols. */
799 while (h->root.type == bfd_link_hash_indirect
800 || h->root.type == bfd_link_hash_warning)
801 h = (struct elf_link_hash_entry *) h->root.u.i.link;
803 /* If we just created the symbol, mark it as being an ELF symbol.
804 Other than that, there is nothing to do--there is no merge issue
805 with a newly defined symbol--so we just return. */
807 if (h->root.type == bfd_link_hash_new)
813 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
816 switch (h->root.type)
823 case bfd_link_hash_undefined:
824 case bfd_link_hash_undefweak:
825 oldbfd = h->root.u.undef.abfd;
829 case bfd_link_hash_defined:
830 case bfd_link_hash_defweak:
831 oldbfd = h->root.u.def.section->owner;
832 oldsec = h->root.u.def.section;
835 case bfd_link_hash_common:
836 oldbfd = h->root.u.c.p->section->owner;
837 oldsec = h->root.u.c.p->section;
841 /* In cases involving weak versioned symbols, we may wind up trying
842 to merge a symbol with itself. Catch that here, to avoid the
843 confusion that results if we try to override a symbol with
844 itself. The additional tests catch cases like
845 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
846 dynamic object, which we do want to handle here. */
848 && ((abfd->flags & DYNAMIC) == 0
852 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
853 respectively, is from a dynamic object. */
855 newdyn = (abfd->flags & DYNAMIC) != 0;
859 olddyn = (oldbfd->flags & DYNAMIC) != 0;
860 else if (oldsec != NULL)
862 /* This handles the special SHN_MIPS_{TEXT,DATA} section
863 indices used by MIPS ELF. */
864 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
867 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
868 respectively, appear to be a definition rather than reference. */
870 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
872 olddef = (h->root.type != bfd_link_hash_undefined
873 && h->root.type != bfd_link_hash_undefweak
874 && h->root.type != bfd_link_hash_common);
876 /* Check TLS symbol. We don't check undefined symbol introduced by
878 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)
879 && ELF_ST_TYPE (sym->st_info) != h->type
883 bfd_boolean ntdef, tdef;
884 asection *ntsec, *tsec;
886 if (h->type == STT_TLS)
906 (*_bfd_error_handler)
907 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
908 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
909 else if (!tdef && !ntdef)
910 (*_bfd_error_handler)
911 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
912 tbfd, ntbfd, h->root.root.string);
914 (*_bfd_error_handler)
915 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
916 tbfd, tsec, ntbfd, h->root.root.string);
918 (*_bfd_error_handler)
919 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
920 tbfd, ntbfd, ntsec, h->root.root.string);
922 bfd_set_error (bfd_error_bad_value);
926 /* We need to remember if a symbol has a definition in a dynamic
927 object or is weak in all dynamic objects. Internal and hidden
928 visibility will make it unavailable to dynamic objects. */
929 if (newdyn && !h->dynamic_def)
931 if (!bfd_is_und_section (sec))
935 /* Check if this symbol is weak in all dynamic objects. If it
936 is the first time we see it in a dynamic object, we mark
937 if it is weak. Otherwise, we clear it. */
940 if (bind == STB_WEAK)
943 else if (bind != STB_WEAK)
948 /* If the old symbol has non-default visibility, we ignore the new
949 definition from a dynamic object. */
951 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
952 && !bfd_is_und_section (sec))
955 /* Make sure this symbol is dynamic. */
957 /* A protected symbol has external availability. Make sure it is
960 FIXME: Should we check type and size for protected symbol? */
961 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
962 return bfd_elf_link_record_dynamic_symbol (info, h);
967 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
970 /* If the new symbol with non-default visibility comes from a
971 relocatable file and the old definition comes from a dynamic
972 object, we remove the old definition. */
973 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
976 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
977 && bfd_is_und_section (sec))
979 /* If the new symbol is undefined and the old symbol was
980 also undefined before, we need to make sure
981 _bfd_generic_link_add_one_symbol doesn't mess
982 up the linker hash table undefs list. Since the old
983 definition came from a dynamic object, it is still on the
985 h->root.type = bfd_link_hash_undefined;
986 h->root.u.undef.abfd = abfd;
990 h->root.type = bfd_link_hash_new;
991 h->root.u.undef.abfd = NULL;
1000 /* FIXME: Should we check type and size for protected symbol? */
1006 /* Differentiate strong and weak symbols. */
1007 newweak = bind == STB_WEAK;
1008 oldweak = (h->root.type == bfd_link_hash_defweak
1009 || h->root.type == bfd_link_hash_undefweak);
1011 /* If a new weak symbol definition comes from a regular file and the
1012 old symbol comes from a dynamic library, we treat the new one as
1013 strong. Similarly, an old weak symbol definition from a regular
1014 file is treated as strong when the new symbol comes from a dynamic
1015 library. Further, an old weak symbol from a dynamic library is
1016 treated as strong if the new symbol is from a dynamic library.
1017 This reflects the way glibc's ld.so works.
1019 Do this before setting *type_change_ok or *size_change_ok so that
1020 we warn properly when dynamic library symbols are overridden. */
1022 if (newdef && !newdyn && olddyn)
1024 if (olddef && newdyn)
1027 /* It's OK to change the type if either the existing symbol or the
1028 new symbol is weak. A type change is also OK if the old symbol
1029 is undefined and the new symbol is defined. */
1034 && h->root.type == bfd_link_hash_undefined))
1035 *type_change_ok = TRUE;
1037 /* It's OK to change the size if either the existing symbol or the
1038 new symbol is weak, or if the old symbol is undefined. */
1041 || h->root.type == bfd_link_hash_undefined)
1042 *size_change_ok = TRUE;
1044 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1045 symbol, respectively, appears to be a common symbol in a dynamic
1046 object. If a symbol appears in an uninitialized section, and is
1047 not weak, and is not a function, then it may be a common symbol
1048 which was resolved when the dynamic object was created. We want
1049 to treat such symbols specially, because they raise special
1050 considerations when setting the symbol size: if the symbol
1051 appears as a common symbol in a regular object, and the size in
1052 the regular object is larger, we must make sure that we use the
1053 larger size. This problematic case can always be avoided in C,
1054 but it must be handled correctly when using Fortran shared
1057 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1058 likewise for OLDDYNCOMMON and OLDDEF.
1060 Note that this test is just a heuristic, and that it is quite
1061 possible to have an uninitialized symbol in a shared object which
1062 is really a definition, rather than a common symbol. This could
1063 lead to some minor confusion when the symbol really is a common
1064 symbol in some regular object. However, I think it will be
1070 && (sec->flags & SEC_ALLOC) != 0
1071 && (sec->flags & SEC_LOAD) == 0
1073 && ELF_ST_TYPE (sym->st_info) != STT_FUNC)
1074 newdyncommon = TRUE;
1076 newdyncommon = FALSE;
1080 && h->root.type == bfd_link_hash_defined
1082 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1083 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1085 && h->type != STT_FUNC)
1086 olddyncommon = TRUE;
1088 olddyncommon = FALSE;
1090 /* We now know everything about the old and new symbols. We ask the
1091 backend to check if we can merge them. */
1092 bed = get_elf_backend_data (abfd);
1093 if (bed->merge_symbol
1094 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1095 pold_alignment, skip, override,
1096 type_change_ok, size_change_ok,
1097 &newdyn, &newdef, &newdyncommon, &newweak,
1099 &olddyn, &olddef, &olddyncommon, &oldweak,
1103 /* If both the old and the new symbols look like common symbols in a
1104 dynamic object, set the size of the symbol to the larger of the
1109 && sym->st_size != h->size)
1111 /* Since we think we have two common symbols, issue a multiple
1112 common warning if desired. Note that we only warn if the
1113 size is different. If the size is the same, we simply let
1114 the old symbol override the new one as normally happens with
1115 symbols defined in dynamic objects. */
1117 if (! ((*info->callbacks->multiple_common)
1118 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1119 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1122 if (sym->st_size > h->size)
1123 h->size = sym->st_size;
1125 *size_change_ok = TRUE;
1128 /* If we are looking at a dynamic object, and we have found a
1129 definition, we need to see if the symbol was already defined by
1130 some other object. If so, we want to use the existing
1131 definition, and we do not want to report a multiple symbol
1132 definition error; we do this by clobbering *PSEC to be
1133 bfd_und_section_ptr.
1135 We treat a common symbol as a definition if the symbol in the
1136 shared library is a function, since common symbols always
1137 represent variables; this can cause confusion in principle, but
1138 any such confusion would seem to indicate an erroneous program or
1139 shared library. We also permit a common symbol in a regular
1140 object to override a weak symbol in a shared object. */
1145 || (h->root.type == bfd_link_hash_common
1147 || ELF_ST_TYPE (sym->st_info) == STT_FUNC))))
1151 newdyncommon = FALSE;
1153 *psec = sec = bfd_und_section_ptr;
1154 *size_change_ok = TRUE;
1156 /* If we get here when the old symbol is a common symbol, then
1157 we are explicitly letting it override a weak symbol or
1158 function in a dynamic object, and we don't want to warn about
1159 a type change. If the old symbol is a defined symbol, a type
1160 change warning may still be appropriate. */
1162 if (h->root.type == bfd_link_hash_common)
1163 *type_change_ok = TRUE;
1166 /* Handle the special case of an old common symbol merging with a
1167 new symbol which looks like a common symbol in a shared object.
1168 We change *PSEC and *PVALUE to make the new symbol look like a
1169 common symbol, and let _bfd_generic_link_add_one_symbol do the
1173 && h->root.type == bfd_link_hash_common)
1177 newdyncommon = FALSE;
1178 *pvalue = sym->st_size;
1179 *psec = sec = bed->common_section (oldsec);
1180 *size_change_ok = TRUE;
1183 /* Skip weak definitions of symbols that are already defined. */
1184 if (newdef && olddef && newweak)
1187 /* If the old symbol is from a dynamic object, and the new symbol is
1188 a definition which is not from a dynamic object, then the new
1189 symbol overrides the old symbol. Symbols from regular files
1190 always take precedence over symbols from dynamic objects, even if
1191 they are defined after the dynamic object in the link.
1193 As above, we again permit a common symbol in a regular object to
1194 override a definition in a shared object if the shared object
1195 symbol is a function or is weak. */
1200 || (bfd_is_com_section (sec)
1202 || h->type == STT_FUNC)))
1207 /* Change the hash table entry to undefined, and let
1208 _bfd_generic_link_add_one_symbol do the right thing with the
1211 h->root.type = bfd_link_hash_undefined;
1212 h->root.u.undef.abfd = h->root.u.def.section->owner;
1213 *size_change_ok = TRUE;
1216 olddyncommon = FALSE;
1218 /* We again permit a type change when a common symbol may be
1219 overriding a function. */
1221 if (bfd_is_com_section (sec))
1222 *type_change_ok = TRUE;
1224 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1227 /* This union may have been set to be non-NULL when this symbol
1228 was seen in a dynamic object. We must force the union to be
1229 NULL, so that it is correct for a regular symbol. */
1230 h->verinfo.vertree = NULL;
1233 /* Handle the special case of a new common symbol merging with an
1234 old symbol that looks like it might be a common symbol defined in
1235 a shared object. Note that we have already handled the case in
1236 which a new common symbol should simply override the definition
1237 in the shared library. */
1240 && bfd_is_com_section (sec)
1243 /* It would be best if we could set the hash table entry to a
1244 common symbol, but we don't know what to use for the section
1245 or the alignment. */
1246 if (! ((*info->callbacks->multiple_common)
1247 (info, h->root.root.string, oldbfd, bfd_link_hash_common,
1248 h->size, abfd, bfd_link_hash_common, sym->st_size)))
1251 /* If the presumed common symbol in the dynamic object is
1252 larger, pretend that the new symbol has its size. */
1254 if (h->size > *pvalue)
1257 /* We need to remember the alignment required by the symbol
1258 in the dynamic object. */
1259 BFD_ASSERT (pold_alignment);
1260 *pold_alignment = h->root.u.def.section->alignment_power;
1263 olddyncommon = FALSE;
1265 h->root.type = bfd_link_hash_undefined;
1266 h->root.u.undef.abfd = h->root.u.def.section->owner;
1268 *size_change_ok = TRUE;
1269 *type_change_ok = TRUE;
1271 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1274 h->verinfo.vertree = NULL;
1279 /* Handle the case where we had a versioned symbol in a dynamic
1280 library and now find a definition in a normal object. In this
1281 case, we make the versioned symbol point to the normal one. */
1282 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1283 flip->root.type = h->root.type;
1284 h->root.type = bfd_link_hash_indirect;
1285 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1286 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1287 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1291 flip->ref_dynamic = 1;
1298 /* This function is called to create an indirect symbol from the
1299 default for the symbol with the default version if needed. The
1300 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1301 set DYNSYM if the new indirect symbol is dynamic. */
1304 _bfd_elf_add_default_symbol (bfd *abfd,
1305 struct bfd_link_info *info,
1306 struct elf_link_hash_entry *h,
1308 Elf_Internal_Sym *sym,
1311 bfd_boolean *dynsym,
1312 bfd_boolean override)
1314 bfd_boolean type_change_ok;
1315 bfd_boolean size_change_ok;
1318 struct elf_link_hash_entry *hi;
1319 struct bfd_link_hash_entry *bh;
1320 const struct elf_backend_data *bed;
1321 bfd_boolean collect;
1322 bfd_boolean dynamic;
1324 size_t len, shortlen;
1327 /* If this symbol has a version, and it is the default version, we
1328 create an indirect symbol from the default name to the fully
1329 decorated name. This will cause external references which do not
1330 specify a version to be bound to this version of the symbol. */
1331 p = strchr (name, ELF_VER_CHR);
1332 if (p == NULL || p[1] != ELF_VER_CHR)
1337 /* We are overridden by an old definition. We need to check if we
1338 need to create the indirect symbol from the default name. */
1339 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1341 BFD_ASSERT (hi != NULL);
1344 while (hi->root.type == bfd_link_hash_indirect
1345 || hi->root.type == bfd_link_hash_warning)
1347 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1353 bed = get_elf_backend_data (abfd);
1354 collect = bed->collect;
1355 dynamic = (abfd->flags & DYNAMIC) != 0;
1357 shortlen = p - name;
1358 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1);
1359 if (shortname == NULL)
1361 memcpy (shortname, name, shortlen);
1362 shortname[shortlen] = '\0';
1364 /* We are going to create a new symbol. Merge it with any existing
1365 symbol with this name. For the purposes of the merge, act as
1366 though we were defining the symbol we just defined, although we
1367 actually going to define an indirect symbol. */
1368 type_change_ok = FALSE;
1369 size_change_ok = FALSE;
1371 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1372 NULL, &hi, &skip, &override,
1373 &type_change_ok, &size_change_ok))
1382 if (! (_bfd_generic_link_add_one_symbol
1383 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1384 0, name, FALSE, collect, &bh)))
1386 hi = (struct elf_link_hash_entry *) bh;
1390 /* In this case the symbol named SHORTNAME is overriding the
1391 indirect symbol we want to add. We were planning on making
1392 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1393 is the name without a version. NAME is the fully versioned
1394 name, and it is the default version.
1396 Overriding means that we already saw a definition for the
1397 symbol SHORTNAME in a regular object, and it is overriding
1398 the symbol defined in the dynamic object.
1400 When this happens, we actually want to change NAME, the
1401 symbol we just added, to refer to SHORTNAME. This will cause
1402 references to NAME in the shared object to become references
1403 to SHORTNAME in the regular object. This is what we expect
1404 when we override a function in a shared object: that the
1405 references in the shared object will be mapped to the
1406 definition in the regular object. */
1408 while (hi->root.type == bfd_link_hash_indirect
1409 || hi->root.type == bfd_link_hash_warning)
1410 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1412 h->root.type = bfd_link_hash_indirect;
1413 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1417 hi->ref_dynamic = 1;
1421 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1426 /* Now set HI to H, so that the following code will set the
1427 other fields correctly. */
1431 /* If there is a duplicate definition somewhere, then HI may not
1432 point to an indirect symbol. We will have reported an error to
1433 the user in that case. */
1435 if (hi->root.type == bfd_link_hash_indirect)
1437 struct elf_link_hash_entry *ht;
1439 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1440 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1442 /* See if the new flags lead us to realize that the symbol must
1454 if (hi->ref_regular)
1460 /* We also need to define an indirection from the nondefault version
1464 len = strlen (name);
1465 shortname = bfd_hash_allocate (&info->hash->table, len);
1466 if (shortname == NULL)
1468 memcpy (shortname, name, shortlen);
1469 memcpy (shortname + shortlen, p + 1, len - shortlen);
1471 /* Once again, merge with any existing symbol. */
1472 type_change_ok = FALSE;
1473 size_change_ok = FALSE;
1475 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1476 NULL, &hi, &skip, &override,
1477 &type_change_ok, &size_change_ok))
1485 /* Here SHORTNAME is a versioned name, so we don't expect to see
1486 the type of override we do in the case above unless it is
1487 overridden by a versioned definition. */
1488 if (hi->root.type != bfd_link_hash_defined
1489 && hi->root.type != bfd_link_hash_defweak)
1490 (*_bfd_error_handler)
1491 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1497 if (! (_bfd_generic_link_add_one_symbol
1498 (info, abfd, shortname, BSF_INDIRECT,
1499 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1501 hi = (struct elf_link_hash_entry *) bh;
1503 /* If there is a duplicate definition somewhere, then HI may not
1504 point to an indirect symbol. We will have reported an error
1505 to the user in that case. */
1507 if (hi->root.type == bfd_link_hash_indirect)
1509 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1511 /* See if the new flags lead us to realize that the symbol
1523 if (hi->ref_regular)
1533 /* This routine is used to export all defined symbols into the dynamic
1534 symbol table. It is called via elf_link_hash_traverse. */
1537 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1539 struct elf_info_failed *eif = data;
1541 /* Ignore indirect symbols. These are added by the versioning code. */
1542 if (h->root.type == bfd_link_hash_indirect)
1545 if (h->root.type == bfd_link_hash_warning)
1546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1548 if (h->dynindx == -1
1552 struct bfd_elf_version_tree *t;
1553 struct bfd_elf_version_expr *d;
1555 for (t = eif->verdefs; t != NULL; t = t->next)
1557 if (t->globals.list != NULL)
1559 d = (*t->match) (&t->globals, NULL, h->root.root.string);
1564 if (t->locals.list != NULL)
1566 d = (*t->match) (&t->locals, NULL, h->root.root.string);
1575 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1586 /* Look through the symbols which are defined in other shared
1587 libraries and referenced here. Update the list of version
1588 dependencies. This will be put into the .gnu.version_r section.
1589 This function is called via elf_link_hash_traverse. */
1592 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1595 struct elf_find_verdep_info *rinfo = data;
1596 Elf_Internal_Verneed *t;
1597 Elf_Internal_Vernaux *a;
1600 if (h->root.type == bfd_link_hash_warning)
1601 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1603 /* We only care about symbols defined in shared objects with version
1608 || h->verinfo.verdef == NULL)
1611 /* See if we already know about this version. */
1612 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref)
1614 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1617 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1618 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1624 /* This is a new version. Add it to tree we are building. */
1629 t = bfd_zalloc (rinfo->output_bfd, amt);
1632 rinfo->failed = TRUE;
1636 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1637 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref;
1638 elf_tdata (rinfo->output_bfd)->verref = t;
1642 a = bfd_zalloc (rinfo->output_bfd, amt);
1644 /* Note that we are copying a string pointer here, and testing it
1645 above. If bfd_elf_string_from_elf_section is ever changed to
1646 discard the string data when low in memory, this will have to be
1648 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1650 a->vna_flags = h->verinfo.verdef->vd_flags;
1651 a->vna_nextptr = t->vn_auxptr;
1653 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1656 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1663 /* Figure out appropriate versions for all the symbols. We may not
1664 have the version number script until we have read all of the input
1665 files, so until that point we don't know which symbols should be
1666 local. This function is called via elf_link_hash_traverse. */
1669 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1671 struct elf_assign_sym_version_info *sinfo;
1672 struct bfd_link_info *info;
1673 const struct elf_backend_data *bed;
1674 struct elf_info_failed eif;
1681 if (h->root.type == bfd_link_hash_warning)
1682 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1684 /* Fix the symbol flags. */
1687 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1690 sinfo->failed = TRUE;
1694 /* We only need version numbers for symbols defined in regular
1696 if (!h->def_regular)
1699 bed = get_elf_backend_data (sinfo->output_bfd);
1700 p = strchr (h->root.root.string, ELF_VER_CHR);
1701 if (p != NULL && h->verinfo.vertree == NULL)
1703 struct bfd_elf_version_tree *t;
1708 /* There are two consecutive ELF_VER_CHR characters if this is
1709 not a hidden symbol. */
1711 if (*p == ELF_VER_CHR)
1717 /* If there is no version string, we can just return out. */
1725 /* Look for the version. If we find it, it is no longer weak. */
1726 for (t = sinfo->verdefs; t != NULL; t = t->next)
1728 if (strcmp (t->name, p) == 0)
1732 struct bfd_elf_version_expr *d;
1734 len = p - h->root.root.string;
1735 alc = bfd_malloc (len);
1738 memcpy (alc, h->root.root.string, len - 1);
1739 alc[len - 1] = '\0';
1740 if (alc[len - 2] == ELF_VER_CHR)
1741 alc[len - 2] = '\0';
1743 h->verinfo.vertree = t;
1747 if (t->globals.list != NULL)
1748 d = (*t->match) (&t->globals, NULL, alc);
1750 /* See if there is anything to force this symbol to
1752 if (d == NULL && t->locals.list != NULL)
1754 d = (*t->match) (&t->locals, NULL, alc);
1757 && ! info->export_dynamic)
1758 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1766 /* If we are building an application, we need to create a
1767 version node for this version. */
1768 if (t == NULL && info->executable)
1770 struct bfd_elf_version_tree **pp;
1773 /* If we aren't going to export this symbol, we don't need
1774 to worry about it. */
1775 if (h->dynindx == -1)
1779 t = bfd_zalloc (sinfo->output_bfd, amt);
1782 sinfo->failed = TRUE;
1787 t->name_indx = (unsigned int) -1;
1791 /* Don't count anonymous version tag. */
1792 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0)
1794 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next)
1796 t->vernum = version_index;
1800 h->verinfo.vertree = t;
1804 /* We could not find the version for a symbol when
1805 generating a shared archive. Return an error. */
1806 (*_bfd_error_handler)
1807 (_("%B: undefined versioned symbol name %s"),
1808 sinfo->output_bfd, h->root.root.string);
1809 bfd_set_error (bfd_error_bad_value);
1810 sinfo->failed = TRUE;
1818 /* If we don't have a version for this symbol, see if we can find
1820 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL)
1822 struct bfd_elf_version_tree *t;
1823 struct bfd_elf_version_tree *local_ver;
1824 struct bfd_elf_version_expr *d;
1826 /* See if can find what version this symbol is in. If the
1827 symbol is supposed to be local, then don't actually register
1830 for (t = sinfo->verdefs; t != NULL; t = t->next)
1832 if (t->globals.list != NULL)
1834 bfd_boolean matched;
1838 while ((d = (*t->match) (&t->globals, d,
1839 h->root.root.string)) != NULL)
1844 /* There is a version without definition. Make
1845 the symbol the default definition for this
1847 h->verinfo.vertree = t;
1855 /* There is no undefined version for this symbol. Hide the
1857 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1860 if (t->locals.list != NULL)
1863 while ((d = (*t->match) (&t->locals, d,
1864 h->root.root.string)) != NULL)
1867 /* If the match is "*", keep looking for a more
1868 explicit, perhaps even global, match.
1869 XXX: Shouldn't this be !d->wildcard instead? */
1870 if (d->pattern[0] != '*' || d->pattern[1] != '\0')
1879 if (local_ver != NULL)
1881 h->verinfo.vertree = local_ver;
1882 if (h->dynindx != -1
1883 && ! info->export_dynamic)
1885 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1893 /* Read and swap the relocs from the section indicated by SHDR. This
1894 may be either a REL or a RELA section. The relocations are
1895 translated into RELA relocations and stored in INTERNAL_RELOCS,
1896 which should have already been allocated to contain enough space.
1897 The EXTERNAL_RELOCS are a buffer where the external form of the
1898 relocations should be stored.
1900 Returns FALSE if something goes wrong. */
1903 elf_link_read_relocs_from_section (bfd *abfd,
1905 Elf_Internal_Shdr *shdr,
1906 void *external_relocs,
1907 Elf_Internal_Rela *internal_relocs)
1909 const struct elf_backend_data *bed;
1910 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
1911 const bfd_byte *erela;
1912 const bfd_byte *erelaend;
1913 Elf_Internal_Rela *irela;
1914 Elf_Internal_Shdr *symtab_hdr;
1917 /* Position ourselves at the start of the section. */
1918 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
1921 /* Read the relocations. */
1922 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
1925 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1926 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
1928 bed = get_elf_backend_data (abfd);
1930 /* Convert the external relocations to the internal format. */
1931 if (shdr->sh_entsize == bed->s->sizeof_rel)
1932 swap_in = bed->s->swap_reloc_in;
1933 else if (shdr->sh_entsize == bed->s->sizeof_rela)
1934 swap_in = bed->s->swap_reloca_in;
1937 bfd_set_error (bfd_error_wrong_format);
1941 erela = external_relocs;
1942 erelaend = erela + shdr->sh_size;
1943 irela = internal_relocs;
1944 while (erela < erelaend)
1948 (*swap_in) (abfd, erela, irela);
1949 r_symndx = ELF32_R_SYM (irela->r_info);
1950 if (bed->s->arch_size == 64)
1952 if ((size_t) r_symndx >= nsyms)
1954 (*_bfd_error_handler)
1955 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
1956 " for offset 0x%lx in section `%A'"),
1958 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
1959 bfd_set_error (bfd_error_bad_value);
1962 irela += bed->s->int_rels_per_ext_rel;
1963 erela += shdr->sh_entsize;
1969 /* Read and swap the relocs for a section O. They may have been
1970 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
1971 not NULL, they are used as buffers to read into. They are known to
1972 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
1973 the return value is allocated using either malloc or bfd_alloc,
1974 according to the KEEP_MEMORY argument. If O has two relocation
1975 sections (both REL and RELA relocations), then the REL_HDR
1976 relocations will appear first in INTERNAL_RELOCS, followed by the
1977 REL_HDR2 relocations. */
1980 _bfd_elf_link_read_relocs (bfd *abfd,
1982 void *external_relocs,
1983 Elf_Internal_Rela *internal_relocs,
1984 bfd_boolean keep_memory)
1986 Elf_Internal_Shdr *rel_hdr;
1987 void *alloc1 = NULL;
1988 Elf_Internal_Rela *alloc2 = NULL;
1989 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1991 if (elf_section_data (o)->relocs != NULL)
1992 return elf_section_data (o)->relocs;
1994 if (o->reloc_count == 0)
1997 rel_hdr = &elf_section_data (o)->rel_hdr;
1999 if (internal_relocs == NULL)
2003 size = o->reloc_count;
2004 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2006 internal_relocs = bfd_alloc (abfd, size);
2008 internal_relocs = alloc2 = bfd_malloc (size);
2009 if (internal_relocs == NULL)
2013 if (external_relocs == NULL)
2015 bfd_size_type size = rel_hdr->sh_size;
2017 if (elf_section_data (o)->rel_hdr2)
2018 size += elf_section_data (o)->rel_hdr2->sh_size;
2019 alloc1 = bfd_malloc (size);
2022 external_relocs = alloc1;
2025 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr,
2029 if (elf_section_data (o)->rel_hdr2
2030 && (!elf_link_read_relocs_from_section
2032 elf_section_data (o)->rel_hdr2,
2033 ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
2034 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
2035 * bed->s->int_rels_per_ext_rel))))
2038 /* Cache the results for next time, if we can. */
2040 elf_section_data (o)->relocs = internal_relocs;
2045 /* Don't free alloc2, since if it was allocated we are passing it
2046 back (under the name of internal_relocs). */
2048 return internal_relocs;
2058 /* Compute the size of, and allocate space for, REL_HDR which is the
2059 section header for a section containing relocations for O. */
2062 _bfd_elf_link_size_reloc_section (bfd *abfd,
2063 Elf_Internal_Shdr *rel_hdr,
2066 bfd_size_type reloc_count;
2067 bfd_size_type num_rel_hashes;
2069 /* Figure out how many relocations there will be. */
2070 if (rel_hdr == &elf_section_data (o)->rel_hdr)
2071 reloc_count = elf_section_data (o)->rel_count;
2073 reloc_count = elf_section_data (o)->rel_count2;
2075 num_rel_hashes = o->reloc_count;
2076 if (num_rel_hashes < reloc_count)
2077 num_rel_hashes = reloc_count;
2079 /* That allows us to calculate the size of the section. */
2080 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count;
2082 /* The contents field must last into write_object_contents, so we
2083 allocate it with bfd_alloc rather than malloc. Also since we
2084 cannot be sure that the contents will actually be filled in,
2085 we zero the allocated space. */
2086 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size);
2087 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2090 /* We only allocate one set of hash entries, so we only do it the
2091 first time we are called. */
2092 if (elf_section_data (o)->rel_hashes == NULL
2095 struct elf_link_hash_entry **p;
2097 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *));
2101 elf_section_data (o)->rel_hashes = p;
2107 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2108 originated from the section given by INPUT_REL_HDR) to the
2112 _bfd_elf_link_output_relocs (bfd *output_bfd,
2113 asection *input_section,
2114 Elf_Internal_Shdr *input_rel_hdr,
2115 Elf_Internal_Rela *internal_relocs,
2116 struct elf_link_hash_entry **rel_hash
2119 Elf_Internal_Rela *irela;
2120 Elf_Internal_Rela *irelaend;
2122 Elf_Internal_Shdr *output_rel_hdr;
2123 asection *output_section;
2124 unsigned int *rel_countp = NULL;
2125 const struct elf_backend_data *bed;
2126 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2128 output_section = input_section->output_section;
2129 output_rel_hdr = NULL;
2131 if (elf_section_data (output_section)->rel_hdr.sh_entsize
2132 == input_rel_hdr->sh_entsize)
2134 output_rel_hdr = &elf_section_data (output_section)->rel_hdr;
2135 rel_countp = &elf_section_data (output_section)->rel_count;
2137 else if (elf_section_data (output_section)->rel_hdr2
2138 && (elf_section_data (output_section)->rel_hdr2->sh_entsize
2139 == input_rel_hdr->sh_entsize))
2141 output_rel_hdr = elf_section_data (output_section)->rel_hdr2;
2142 rel_countp = &elf_section_data (output_section)->rel_count2;
2146 (*_bfd_error_handler)
2147 (_("%B: relocation size mismatch in %B section %A"),
2148 output_bfd, input_section->owner, input_section);
2149 bfd_set_error (bfd_error_wrong_object_format);
2153 bed = get_elf_backend_data (output_bfd);
2154 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel)
2155 swap_out = bed->s->swap_reloc_out;
2156 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela)
2157 swap_out = bed->s->swap_reloca_out;
2161 erel = output_rel_hdr->contents;
2162 erel += *rel_countp * input_rel_hdr->sh_entsize;
2163 irela = internal_relocs;
2164 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2165 * bed->s->int_rels_per_ext_rel);
2166 while (irela < irelaend)
2168 (*swap_out) (output_bfd, irela, erel);
2169 irela += bed->s->int_rels_per_ext_rel;
2170 erel += input_rel_hdr->sh_entsize;
2173 /* Bump the counter, so that we know where to add the next set of
2175 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
2180 /* Fix up the flags for a symbol. This handles various cases which
2181 can only be fixed after all the input files are seen. This is
2182 currently called by both adjust_dynamic_symbol and
2183 assign_sym_version, which is unnecessary but perhaps more robust in
2184 the face of future changes. */
2187 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2188 struct elf_info_failed *eif)
2190 /* If this symbol was mentioned in a non-ELF file, try to set
2191 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2192 permit a non-ELF file to correctly refer to a symbol defined in
2193 an ELF dynamic object. */
2196 while (h->root.type == bfd_link_hash_indirect)
2197 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2199 if (h->root.type != bfd_link_hash_defined
2200 && h->root.type != bfd_link_hash_defweak)
2203 h->ref_regular_nonweak = 1;
2207 if (h->root.u.def.section->owner != NULL
2208 && (bfd_get_flavour (h->root.u.def.section->owner)
2209 == bfd_target_elf_flavour))
2212 h->ref_regular_nonweak = 1;
2218 if (h->dynindx == -1
2222 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2231 /* Unfortunately, NON_ELF is only correct if the symbol
2232 was first seen in a non-ELF file. Fortunately, if the symbol
2233 was first seen in an ELF file, we're probably OK unless the
2234 symbol was defined in a non-ELF file. Catch that case here.
2235 FIXME: We're still in trouble if the symbol was first seen in
2236 a dynamic object, and then later in a non-ELF regular object. */
2237 if ((h->root.type == bfd_link_hash_defined
2238 || h->root.type == bfd_link_hash_defweak)
2240 && (h->root.u.def.section->owner != NULL
2241 ? (bfd_get_flavour (h->root.u.def.section->owner)
2242 != bfd_target_elf_flavour)
2243 : (bfd_is_abs_section (h->root.u.def.section)
2244 && !h->def_dynamic)))
2248 /* If this is a final link, and the symbol was defined as a common
2249 symbol in a regular object file, and there was no definition in
2250 any dynamic object, then the linker will have allocated space for
2251 the symbol in a common section but the DEF_REGULAR
2252 flag will not have been set. */
2253 if (h->root.type == bfd_link_hash_defined
2257 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2260 /* If -Bsymbolic was used (which means to bind references to global
2261 symbols to the definition within the shared object), and this
2262 symbol was defined in a regular object, then it actually doesn't
2263 need a PLT entry. Likewise, if the symbol has non-default
2264 visibility. If the symbol has hidden or internal visibility, we
2265 will force it local. */
2267 && eif->info->shared
2268 && is_elf_hash_table (eif->info->hash)
2269 && (eif->info->symbolic
2270 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2273 const struct elf_backend_data *bed;
2274 bfd_boolean force_local;
2276 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2278 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2279 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2280 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2283 /* If a weak undefined symbol has non-default visibility, we also
2284 hide it from the dynamic linker. */
2285 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2286 && h->root.type == bfd_link_hash_undefweak)
2288 const struct elf_backend_data *bed;
2289 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2290 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2293 /* If this is a weak defined symbol in a dynamic object, and we know
2294 the real definition in the dynamic object, copy interesting flags
2295 over to the real definition. */
2296 if (h->u.weakdef != NULL)
2298 struct elf_link_hash_entry *weakdef;
2300 weakdef = h->u.weakdef;
2301 if (h->root.type == bfd_link_hash_indirect)
2302 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2304 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2305 || h->root.type == bfd_link_hash_defweak);
2306 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2307 || weakdef->root.type == bfd_link_hash_defweak);
2308 BFD_ASSERT (weakdef->def_dynamic);
2310 /* If the real definition is defined by a regular object file,
2311 don't do anything special. See the longer description in
2312 _bfd_elf_adjust_dynamic_symbol, below. */
2313 if (weakdef->def_regular)
2314 h->u.weakdef = NULL;
2317 const struct elf_backend_data *bed;
2319 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2320 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2327 /* Make the backend pick a good value for a dynamic symbol. This is
2328 called via elf_link_hash_traverse, and also calls itself
2332 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2334 struct elf_info_failed *eif = data;
2336 const struct elf_backend_data *bed;
2338 if (! is_elf_hash_table (eif->info->hash))
2341 if (h->root.type == bfd_link_hash_warning)
2343 h->got = elf_hash_table (eif->info)->init_got_offset;
2344 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2346 /* When warning symbols are created, they **replace** the "real"
2347 entry in the hash table, thus we never get to see the real
2348 symbol in a hash traversal. So look at it now. */
2349 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2352 /* Ignore indirect symbols. These are added by the versioning code. */
2353 if (h->root.type == bfd_link_hash_indirect)
2356 /* Fix the symbol flags. */
2357 if (! _bfd_elf_fix_symbol_flags (h, eif))
2360 /* If this symbol does not require a PLT entry, and it is not
2361 defined by a dynamic object, or is not referenced by a regular
2362 object, ignore it. We do have to handle a weak defined symbol,
2363 even if no regular object refers to it, if we decided to add it
2364 to the dynamic symbol table. FIXME: Do we normally need to worry
2365 about symbols which are defined by one dynamic object and
2366 referenced by another one? */
2371 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2373 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2377 /* If we've already adjusted this symbol, don't do it again. This
2378 can happen via a recursive call. */
2379 if (h->dynamic_adjusted)
2382 /* Don't look at this symbol again. Note that we must set this
2383 after checking the above conditions, because we may look at a
2384 symbol once, decide not to do anything, and then get called
2385 recursively later after REF_REGULAR is set below. */
2386 h->dynamic_adjusted = 1;
2388 /* If this is a weak definition, and we know a real definition, and
2389 the real symbol is not itself defined by a regular object file,
2390 then get a good value for the real definition. We handle the
2391 real symbol first, for the convenience of the backend routine.
2393 Note that there is a confusing case here. If the real definition
2394 is defined by a regular object file, we don't get the real symbol
2395 from the dynamic object, but we do get the weak symbol. If the
2396 processor backend uses a COPY reloc, then if some routine in the
2397 dynamic object changes the real symbol, we will not see that
2398 change in the corresponding weak symbol. This is the way other
2399 ELF linkers work as well, and seems to be a result of the shared
2402 I will clarify this issue. Most SVR4 shared libraries define the
2403 variable _timezone and define timezone as a weak synonym. The
2404 tzset call changes _timezone. If you write
2405 extern int timezone;
2407 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2408 you might expect that, since timezone is a synonym for _timezone,
2409 the same number will print both times. However, if the processor
2410 backend uses a COPY reloc, then actually timezone will be copied
2411 into your process image, and, since you define _timezone
2412 yourself, _timezone will not. Thus timezone and _timezone will
2413 wind up at different memory locations. The tzset call will set
2414 _timezone, leaving timezone unchanged. */
2416 if (h->u.weakdef != NULL)
2418 /* If we get to this point, we know there is an implicit
2419 reference by a regular object file via the weak symbol H.
2420 FIXME: Is this really true? What if the traversal finds
2421 H->U.WEAKDEF before it finds H? */
2422 h->u.weakdef->ref_regular = 1;
2424 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2428 /* If a symbol has no type and no size and does not require a PLT
2429 entry, then we are probably about to do the wrong thing here: we
2430 are probably going to create a COPY reloc for an empty object.
2431 This case can arise when a shared object is built with assembly
2432 code, and the assembly code fails to set the symbol type. */
2434 && h->type == STT_NOTYPE
2436 (*_bfd_error_handler)
2437 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2438 h->root.root.string);
2440 dynobj = elf_hash_table (eif->info)->dynobj;
2441 bed = get_elf_backend_data (dynobj);
2442 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2451 /* Adjust all external symbols pointing into SEC_MERGE sections
2452 to reflect the object merging within the sections. */
2455 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2459 if (h->root.type == bfd_link_hash_warning)
2460 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2462 if ((h->root.type == bfd_link_hash_defined
2463 || h->root.type == bfd_link_hash_defweak)
2464 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2465 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2467 bfd *output_bfd = data;
2469 h->root.u.def.value =
2470 _bfd_merged_section_offset (output_bfd,
2471 &h->root.u.def.section,
2472 elf_section_data (sec)->sec_info,
2473 h->root.u.def.value);
2479 /* Returns false if the symbol referred to by H should be considered
2480 to resolve local to the current module, and true if it should be
2481 considered to bind dynamically. */
2484 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2485 struct bfd_link_info *info,
2486 bfd_boolean ignore_protected)
2488 bfd_boolean binding_stays_local_p;
2493 while (h->root.type == bfd_link_hash_indirect
2494 || h->root.type == bfd_link_hash_warning)
2495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2497 /* If it was forced local, then clearly it's not dynamic. */
2498 if (h->dynindx == -1)
2500 if (h->forced_local)
2503 /* Identify the cases where name binding rules say that a
2504 visible symbol resolves locally. */
2505 binding_stays_local_p = info->executable || info->symbolic;
2507 switch (ELF_ST_VISIBILITY (h->other))
2514 /* Proper resolution for function pointer equality may require
2515 that these symbols perhaps be resolved dynamically, even though
2516 we should be resolving them to the current module. */
2517 if (!ignore_protected || h->type != STT_FUNC)
2518 binding_stays_local_p = TRUE;
2525 /* If it isn't defined locally, then clearly it's dynamic. */
2526 if (!h->def_regular)
2529 /* Otherwise, the symbol is dynamic if binding rules don't tell
2530 us that it remains local. */
2531 return !binding_stays_local_p;
2534 /* Return true if the symbol referred to by H should be considered
2535 to resolve local to the current module, and false otherwise. Differs
2536 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2537 undefined symbols and weak symbols. */
2540 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2541 struct bfd_link_info *info,
2542 bfd_boolean local_protected)
2544 /* If it's a local sym, of course we resolve locally. */
2548 /* Common symbols that become definitions don't get the DEF_REGULAR
2549 flag set, so test it first, and don't bail out. */
2550 if (ELF_COMMON_DEF_P (h))
2552 /* If we don't have a definition in a regular file, then we can't
2553 resolve locally. The sym is either undefined or dynamic. */
2554 else if (!h->def_regular)
2557 /* Forced local symbols resolve locally. */
2558 if (h->forced_local)
2561 /* As do non-dynamic symbols. */
2562 if (h->dynindx == -1)
2565 /* At this point, we know the symbol is defined and dynamic. In an
2566 executable it must resolve locally, likewise when building symbolic
2567 shared libraries. */
2568 if (info->executable || info->symbolic)
2571 /* Now deal with defined dynamic symbols in shared libraries. Ones
2572 with default visibility might not resolve locally. */
2573 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2576 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */
2577 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED)
2580 /* STV_PROTECTED non-function symbols are local. */
2581 if (h->type != STT_FUNC)
2584 /* Function pointer equality tests may require that STV_PROTECTED
2585 symbols be treated as dynamic symbols, even when we know that the
2586 dynamic linker will resolve them locally. */
2587 return local_protected;
2590 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2591 aligned. Returns the first TLS output section. */
2593 struct bfd_section *
2594 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2596 struct bfd_section *sec, *tls;
2597 unsigned int align = 0;
2599 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2600 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2604 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2605 if (sec->alignment_power > align)
2606 align = sec->alignment_power;
2608 elf_hash_table (info)->tls_sec = tls;
2610 /* Ensure the alignment of the first section is the largest alignment,
2611 so that the tls segment starts aligned. */
2613 tls->alignment_power = align;
2618 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2620 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2621 Elf_Internal_Sym *sym)
2623 const struct elf_backend_data *bed;
2625 /* Local symbols do not count, but target specific ones might. */
2626 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2627 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2630 /* Function symbols do not count. */
2631 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
2634 /* If the section is undefined, then so is the symbol. */
2635 if (sym->st_shndx == SHN_UNDEF)
2638 /* If the symbol is defined in the common section, then
2639 it is a common definition and so does not count. */
2640 bed = get_elf_backend_data (abfd);
2641 if (bed->common_definition (sym))
2644 /* If the symbol is in a target specific section then we
2645 must rely upon the backend to tell us what it is. */
2646 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2647 /* FIXME - this function is not coded yet:
2649 return _bfd_is_global_symbol_definition (abfd, sym);
2651 Instead for now assume that the definition is not global,
2652 Even if this is wrong, at least the linker will behave
2653 in the same way that it used to do. */
2659 /* Search the symbol table of the archive element of the archive ABFD
2660 whose archive map contains a mention of SYMDEF, and determine if
2661 the symbol is defined in this element. */
2663 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2665 Elf_Internal_Shdr * hdr;
2666 bfd_size_type symcount;
2667 bfd_size_type extsymcount;
2668 bfd_size_type extsymoff;
2669 Elf_Internal_Sym *isymbuf;
2670 Elf_Internal_Sym *isym;
2671 Elf_Internal_Sym *isymend;
2674 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2678 if (! bfd_check_format (abfd, bfd_object))
2681 /* If we have already included the element containing this symbol in the
2682 link then we do not need to include it again. Just claim that any symbol
2683 it contains is not a definition, so that our caller will not decide to
2684 (re)include this element. */
2685 if (abfd->archive_pass)
2688 /* Select the appropriate symbol table. */
2689 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2690 hdr = &elf_tdata (abfd)->symtab_hdr;
2692 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2694 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2696 /* The sh_info field of the symtab header tells us where the
2697 external symbols start. We don't care about the local symbols. */
2698 if (elf_bad_symtab (abfd))
2700 extsymcount = symcount;
2705 extsymcount = symcount - hdr->sh_info;
2706 extsymoff = hdr->sh_info;
2709 if (extsymcount == 0)
2712 /* Read in the symbol table. */
2713 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2715 if (isymbuf == NULL)
2718 /* Scan the symbol table looking for SYMDEF. */
2720 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2724 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
2729 if (strcmp (name, symdef->name) == 0)
2731 result = is_global_data_symbol_definition (abfd, isym);
2741 /* Add an entry to the .dynamic table. */
2744 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
2748 struct elf_link_hash_table *hash_table;
2749 const struct elf_backend_data *bed;
2751 bfd_size_type newsize;
2752 bfd_byte *newcontents;
2753 Elf_Internal_Dyn dyn;
2755 hash_table = elf_hash_table (info);
2756 if (! is_elf_hash_table (hash_table))
2759 if (info->warn_shared_textrel && info->shared && tag == DT_TEXTREL)
2761 (_("warning: creating a DT_TEXTREL in a shared object."));
2763 bed = get_elf_backend_data (hash_table->dynobj);
2764 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2765 BFD_ASSERT (s != NULL);
2767 newsize = s->size + bed->s->sizeof_dyn;
2768 newcontents = bfd_realloc (s->contents, newsize);
2769 if (newcontents == NULL)
2773 dyn.d_un.d_val = val;
2774 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
2777 s->contents = newcontents;
2782 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
2783 otherwise just check whether one already exists. Returns -1 on error,
2784 1 if a DT_NEEDED tag already exists, and 0 on success. */
2787 elf_add_dt_needed_tag (bfd *abfd,
2788 struct bfd_link_info *info,
2792 struct elf_link_hash_table *hash_table;
2793 bfd_size_type oldsize;
2794 bfd_size_type strindex;
2796 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
2799 hash_table = elf_hash_table (info);
2800 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
2801 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
2802 if (strindex == (bfd_size_type) -1)
2805 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
2808 const struct elf_backend_data *bed;
2811 bed = get_elf_backend_data (hash_table->dynobj);
2812 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
2814 for (extdyn = sdyn->contents;
2815 extdyn < sdyn->contents + sdyn->size;
2816 extdyn += bed->s->sizeof_dyn)
2818 Elf_Internal_Dyn dyn;
2820 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
2821 if (dyn.d_tag == DT_NEEDED
2822 && dyn.d_un.d_val == strindex)
2824 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2832 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
2835 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
2839 /* We were just checking for existence of the tag. */
2840 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
2845 /* Called via elf_link_hash_traverse, elf_smash_syms sets all symbols
2846 belonging to NOT_NEEDED to bfd_link_hash_new. We know there are no
2847 references from regular objects to these symbols.
2849 ??? Should we do something about references from other dynamic
2850 obects? If not, we potentially lose some warnings about undefined
2851 symbols. But how can we recover the initial undefined / undefweak
2854 struct elf_smash_syms_data
2857 struct elf_link_hash_table *htab;
2858 bfd_boolean twiddled;
2862 elf_smash_syms (struct elf_link_hash_entry *h, void *data)
2864 struct elf_smash_syms_data *inf = (struct elf_smash_syms_data *) data;
2865 struct bfd_link_hash_entry *bh;
2867 switch (h->root.type)
2870 case bfd_link_hash_new:
2873 case bfd_link_hash_undefined:
2874 if (h->root.u.undef.abfd != inf->not_needed)
2876 if (h->root.u.undef.weak != NULL
2877 && h->root.u.undef.weak != inf->not_needed)
2879 /* Symbol was undefweak in u.undef.weak bfd, and has become
2880 undefined in as-needed lib. Restore weak. */
2881 h->root.type = bfd_link_hash_undefweak;
2882 h->root.u.undef.abfd = h->root.u.undef.weak;
2883 if (h->root.u.undef.next != NULL
2884 || inf->htab->root.undefs_tail == &h->root)
2885 inf->twiddled = TRUE;
2890 case bfd_link_hash_undefweak:
2891 if (h->root.u.undef.abfd != inf->not_needed)
2895 case bfd_link_hash_defined:
2896 case bfd_link_hash_defweak:
2897 if (h->root.u.def.section->owner != inf->not_needed)
2901 case bfd_link_hash_common:
2902 if (h->root.u.c.p->section->owner != inf->not_needed)
2906 case bfd_link_hash_warning:
2907 case bfd_link_hash_indirect:
2908 elf_smash_syms ((struct elf_link_hash_entry *) h->root.u.i.link, data);
2909 if (h->root.u.i.link->type != bfd_link_hash_new)
2911 if (h->root.u.i.link->u.undef.abfd != inf->not_needed)
2916 /* There is no way we can undo symbol table state from defined or
2917 defweak back to undefined. */
2921 /* Set sym back to newly created state, but keep undef.next if it is
2922 being used as a list pointer. */
2923 bh = h->root.u.undef.next;
2926 if (bh != NULL || inf->htab->root.undefs_tail == &h->root)
2927 inf->twiddled = TRUE;
2928 (*inf->htab->root.table.newfunc) (&h->root.root,
2929 &inf->htab->root.table,
2930 h->root.root.string);
2931 h->root.u.undef.next = bh;
2932 h->root.u.undef.abfd = inf->not_needed;
2937 /* Sort symbol by value and section. */
2939 elf_sort_symbol (const void *arg1, const void *arg2)
2941 const struct elf_link_hash_entry *h1;
2942 const struct elf_link_hash_entry *h2;
2943 bfd_signed_vma vdiff;
2945 h1 = *(const struct elf_link_hash_entry **) arg1;
2946 h2 = *(const struct elf_link_hash_entry **) arg2;
2947 vdiff = h1->root.u.def.value - h2->root.u.def.value;
2949 return vdiff > 0 ? 1 : -1;
2952 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
2954 return sdiff > 0 ? 1 : -1;
2959 /* This function is used to adjust offsets into .dynstr for
2960 dynamic symbols. This is called via elf_link_hash_traverse. */
2963 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
2965 struct elf_strtab_hash *dynstr = data;
2967 if (h->root.type == bfd_link_hash_warning)
2968 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2970 if (h->dynindx != -1)
2971 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
2975 /* Assign string offsets in .dynstr, update all structures referencing
2979 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
2981 struct elf_link_hash_table *hash_table = elf_hash_table (info);
2982 struct elf_link_local_dynamic_entry *entry;
2983 struct elf_strtab_hash *dynstr = hash_table->dynstr;
2984 bfd *dynobj = hash_table->dynobj;
2987 const struct elf_backend_data *bed;
2990 _bfd_elf_strtab_finalize (dynstr);
2991 size = _bfd_elf_strtab_size (dynstr);
2993 bed = get_elf_backend_data (dynobj);
2994 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2995 BFD_ASSERT (sdyn != NULL);
2997 /* Update all .dynamic entries referencing .dynstr strings. */
2998 for (extdyn = sdyn->contents;
2999 extdyn < sdyn->contents + sdyn->size;
3000 extdyn += bed->s->sizeof_dyn)
3002 Elf_Internal_Dyn dyn;
3004 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3008 dyn.d_un.d_val = size;
3016 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3021 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3024 /* Now update local dynamic symbols. */
3025 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3026 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3027 entry->isym.st_name);
3029 /* And the rest of dynamic symbols. */
3030 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3032 /* Adjust version definitions. */
3033 if (elf_tdata (output_bfd)->cverdefs)
3038 Elf_Internal_Verdef def;
3039 Elf_Internal_Verdaux defaux;
3041 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3045 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3047 p += sizeof (Elf_External_Verdef);
3048 if (def.vd_aux != sizeof (Elf_External_Verdef))
3050 for (i = 0; i < def.vd_cnt; ++i)
3052 _bfd_elf_swap_verdaux_in (output_bfd,
3053 (Elf_External_Verdaux *) p, &defaux);
3054 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3056 _bfd_elf_swap_verdaux_out (output_bfd,
3057 &defaux, (Elf_External_Verdaux *) p);
3058 p += sizeof (Elf_External_Verdaux);
3061 while (def.vd_next);
3064 /* Adjust version references. */
3065 if (elf_tdata (output_bfd)->verref)
3070 Elf_Internal_Verneed need;
3071 Elf_Internal_Vernaux needaux;
3073 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3077 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3079 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3080 _bfd_elf_swap_verneed_out (output_bfd, &need,
3081 (Elf_External_Verneed *) p);
3082 p += sizeof (Elf_External_Verneed);
3083 for (i = 0; i < need.vn_cnt; ++i)
3085 _bfd_elf_swap_vernaux_in (output_bfd,
3086 (Elf_External_Vernaux *) p, &needaux);
3087 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3089 _bfd_elf_swap_vernaux_out (output_bfd,
3091 (Elf_External_Vernaux *) p);
3092 p += sizeof (Elf_External_Vernaux);
3095 while (need.vn_next);
3101 /* Add symbols from an ELF object file to the linker hash table. */
3104 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3106 bfd_boolean (*add_symbol_hook)
3107 (bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
3108 const char **, flagword *, asection **, bfd_vma *);
3109 bfd_boolean (*check_relocs)
3110 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
3111 bfd_boolean (*check_directives)
3112 (bfd *, struct bfd_link_info *);
3113 bfd_boolean collect;
3114 Elf_Internal_Shdr *hdr;
3115 bfd_size_type symcount;
3116 bfd_size_type extsymcount;
3117 bfd_size_type extsymoff;
3118 struct elf_link_hash_entry **sym_hash;
3119 bfd_boolean dynamic;
3120 Elf_External_Versym *extversym = NULL;
3121 Elf_External_Versym *ever;
3122 struct elf_link_hash_entry *weaks;
3123 struct elf_link_hash_entry **nondeflt_vers = NULL;
3124 bfd_size_type nondeflt_vers_cnt = 0;
3125 Elf_Internal_Sym *isymbuf = NULL;
3126 Elf_Internal_Sym *isym;
3127 Elf_Internal_Sym *isymend;
3128 const struct elf_backend_data *bed;
3129 bfd_boolean add_needed;
3130 struct elf_link_hash_table * hash_table;
3133 hash_table = elf_hash_table (info);
3135 bed = get_elf_backend_data (abfd);
3136 add_symbol_hook = bed->elf_add_symbol_hook;
3137 collect = bed->collect;
3139 if ((abfd->flags & DYNAMIC) == 0)
3145 /* You can't use -r against a dynamic object. Also, there's no
3146 hope of using a dynamic object which does not exactly match
3147 the format of the output file. */
3148 if (info->relocatable
3149 || !is_elf_hash_table (hash_table)
3150 || hash_table->root.creator != abfd->xvec)
3152 if (info->relocatable)
3153 bfd_set_error (bfd_error_invalid_operation);
3155 bfd_set_error (bfd_error_wrong_format);
3160 /* As a GNU extension, any input sections which are named
3161 .gnu.warning.SYMBOL are treated as warning symbols for the given
3162 symbol. This differs from .gnu.warning sections, which generate
3163 warnings when they are included in an output file. */
3164 if (info->executable)
3168 for (s = abfd->sections; s != NULL; s = s->next)
3172 name = bfd_get_section_name (abfd, s);
3173 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
3178 name += sizeof ".gnu.warning." - 1;
3180 /* If this is a shared object, then look up the symbol
3181 in the hash table. If it is there, and it is already
3182 been defined, then we will not be using the entry
3183 from this shared object, so we don't need to warn.
3184 FIXME: If we see the definition in a regular object
3185 later on, we will warn, but we shouldn't. The only
3186 fix is to keep track of what warnings we are supposed
3187 to emit, and then handle them all at the end of the
3191 struct elf_link_hash_entry *h;
3193 h = elf_link_hash_lookup (hash_table, name,
3194 FALSE, FALSE, TRUE);
3196 /* FIXME: What about bfd_link_hash_common? */
3198 && (h->root.type == bfd_link_hash_defined
3199 || h->root.type == bfd_link_hash_defweak))
3201 /* We don't want to issue this warning. Clobber
3202 the section size so that the warning does not
3203 get copied into the output file. */
3210 msg = bfd_alloc (abfd, sz + 1);
3214 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3219 if (! (_bfd_generic_link_add_one_symbol
3220 (info, abfd, name, BSF_WARNING, s, 0, msg,
3221 FALSE, collect, NULL)))
3224 if (! info->relocatable)
3226 /* Clobber the section size so that the warning does
3227 not get copied into the output file. */
3230 /* Also set SEC_EXCLUDE, so that symbols defined in
3231 the warning section don't get copied to the output. */
3232 s->flags |= SEC_EXCLUDE;
3241 /* If we are creating a shared library, create all the dynamic
3242 sections immediately. We need to attach them to something,
3243 so we attach them to this BFD, provided it is the right
3244 format. FIXME: If there are no input BFD's of the same
3245 format as the output, we can't make a shared library. */
3247 && is_elf_hash_table (hash_table)
3248 && hash_table->root.creator == abfd->xvec
3249 && ! hash_table->dynamic_sections_created)
3251 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3255 else if (!is_elf_hash_table (hash_table))
3260 const char *soname = NULL;
3261 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3264 /* ld --just-symbols and dynamic objects don't mix very well.
3265 ld shouldn't allow it. */
3266 if ((s = abfd->sections) != NULL
3267 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3270 /* If this dynamic lib was specified on the command line with
3271 --as-needed in effect, then we don't want to add a DT_NEEDED
3272 tag unless the lib is actually used. Similary for libs brought
3273 in by another lib's DT_NEEDED. When --no-add-needed is used
3274 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3275 any dynamic library in DT_NEEDED tags in the dynamic lib at
3277 add_needed = (elf_dyn_lib_class (abfd)
3278 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3279 | DYN_NO_NEEDED)) == 0;
3281 s = bfd_get_section_by_name (abfd, ".dynamic");
3287 unsigned long shlink;
3289 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3290 goto error_free_dyn;
3292 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3294 goto error_free_dyn;
3295 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3297 for (extdyn = dynbuf;
3298 extdyn < dynbuf + s->size;
3299 extdyn += bed->s->sizeof_dyn)
3301 Elf_Internal_Dyn dyn;
3303 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3304 if (dyn.d_tag == DT_SONAME)
3306 unsigned int tagv = dyn.d_un.d_val;
3307 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3309 goto error_free_dyn;
3311 if (dyn.d_tag == DT_NEEDED)
3313 struct bfd_link_needed_list *n, **pn;
3315 unsigned int tagv = dyn.d_un.d_val;
3317 amt = sizeof (struct bfd_link_needed_list);
3318 n = bfd_alloc (abfd, amt);
3319 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3320 if (n == NULL || fnm == NULL)
3321 goto error_free_dyn;
3322 amt = strlen (fnm) + 1;
3323 anm = bfd_alloc (abfd, amt);
3325 goto error_free_dyn;
3326 memcpy (anm, fnm, amt);
3330 for (pn = & hash_table->needed;
3336 if (dyn.d_tag == DT_RUNPATH)
3338 struct bfd_link_needed_list *n, **pn;
3340 unsigned int tagv = dyn.d_un.d_val;
3342 amt = sizeof (struct bfd_link_needed_list);
3343 n = bfd_alloc (abfd, amt);
3344 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3345 if (n == NULL || fnm == NULL)
3346 goto error_free_dyn;
3347 amt = strlen (fnm) + 1;
3348 anm = bfd_alloc (abfd, amt);
3350 goto error_free_dyn;
3351 memcpy (anm, fnm, amt);
3355 for (pn = & runpath;
3361 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3362 if (!runpath && dyn.d_tag == DT_RPATH)
3364 struct bfd_link_needed_list *n, **pn;
3366 unsigned int tagv = dyn.d_un.d_val;
3368 amt = sizeof (struct bfd_link_needed_list);
3369 n = bfd_alloc (abfd, amt);
3370 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3371 if (n == NULL || fnm == NULL)
3372 goto error_free_dyn;
3373 amt = strlen (fnm) + 1;
3374 anm = bfd_alloc (abfd, amt);
3381 memcpy (anm, fnm, amt);
3396 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3397 frees all more recently bfd_alloc'd blocks as well. */
3403 struct bfd_link_needed_list **pn;
3404 for (pn = & hash_table->runpath;
3411 /* We do not want to include any of the sections in a dynamic
3412 object in the output file. We hack by simply clobbering the
3413 list of sections in the BFD. This could be handled more
3414 cleanly by, say, a new section flag; the existing
3415 SEC_NEVER_LOAD flag is not the one we want, because that one
3416 still implies that the section takes up space in the output
3418 bfd_section_list_clear (abfd);
3420 /* Find the name to use in a DT_NEEDED entry that refers to this
3421 object. If the object has a DT_SONAME entry, we use it.
3422 Otherwise, if the generic linker stuck something in
3423 elf_dt_name, we use that. Otherwise, we just use the file
3425 if (soname == NULL || *soname == '\0')
3427 soname = elf_dt_name (abfd);
3428 if (soname == NULL || *soname == '\0')
3429 soname = bfd_get_filename (abfd);
3432 /* Save the SONAME because sometimes the linker emulation code
3433 will need to know it. */
3434 elf_dt_name (abfd) = soname;
3436 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3440 /* If we have already included this dynamic object in the
3441 link, just ignore it. There is no reason to include a
3442 particular dynamic object more than once. */
3447 /* If this is a dynamic object, we always link against the .dynsym
3448 symbol table, not the .symtab symbol table. The dynamic linker
3449 will only see the .dynsym symbol table, so there is no reason to
3450 look at .symtab for a dynamic object. */
3452 if (! dynamic || elf_dynsymtab (abfd) == 0)
3453 hdr = &elf_tdata (abfd)->symtab_hdr;
3455 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3457 symcount = hdr->sh_size / bed->s->sizeof_sym;
3459 /* The sh_info field of the symtab header tells us where the
3460 external symbols start. We don't care about the local symbols at
3462 if (elf_bad_symtab (abfd))
3464 extsymcount = symcount;
3469 extsymcount = symcount - hdr->sh_info;
3470 extsymoff = hdr->sh_info;
3474 if (extsymcount != 0)
3476 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3478 if (isymbuf == NULL)
3481 /* We store a pointer to the hash table entry for each external
3483 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3484 sym_hash = bfd_alloc (abfd, amt);
3485 if (sym_hash == NULL)
3486 goto error_free_sym;
3487 elf_sym_hashes (abfd) = sym_hash;
3492 /* Read in any version definitions. */
3493 if (!_bfd_elf_slurp_version_tables (abfd,
3494 info->default_imported_symver))
3495 goto error_free_sym;
3497 /* Read in the symbol versions, but don't bother to convert them
3498 to internal format. */
3499 if (elf_dynversym (abfd) != 0)
3501 Elf_Internal_Shdr *versymhdr;
3503 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3504 extversym = bfd_malloc (versymhdr->sh_size);
3505 if (extversym == NULL)
3506 goto error_free_sym;
3507 amt = versymhdr->sh_size;
3508 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3509 || bfd_bread (extversym, amt, abfd) != amt)
3510 goto error_free_vers;
3516 ever = extversym != NULL ? extversym + extsymoff : NULL;
3517 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3519 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3523 asection *sec, *new_sec;
3526 struct elf_link_hash_entry *h;
3527 bfd_boolean definition;
3528 bfd_boolean size_change_ok;
3529 bfd_boolean type_change_ok;
3530 bfd_boolean new_weakdef;
3531 bfd_boolean override;
3533 unsigned int old_alignment;
3538 flags = BSF_NO_FLAGS;
3540 value = isym->st_value;
3542 common = bed->common_definition (isym);
3544 bind = ELF_ST_BIND (isym->st_info);
3545 if (bind == STB_LOCAL)
3547 /* This should be impossible, since ELF requires that all
3548 global symbols follow all local symbols, and that sh_info
3549 point to the first global symbol. Unfortunately, Irix 5
3553 else if (bind == STB_GLOBAL)
3555 if (isym->st_shndx != SHN_UNDEF && !common)
3558 else if (bind == STB_WEAK)
3562 /* Leave it up to the processor backend. */
3565 if (isym->st_shndx == SHN_UNDEF)
3566 sec = bfd_und_section_ptr;
3567 else if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
3569 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3571 sec = bfd_abs_section_ptr;
3572 else if (sec->kept_section)
3574 /* Symbols from discarded section are undefined, and have
3575 default visibility. */
3576 sec = bfd_und_section_ptr;
3577 isym->st_shndx = SHN_UNDEF;
3578 isym->st_other = STV_DEFAULT
3579 | (isym->st_other & ~ ELF_ST_VISIBILITY(-1));
3581 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3584 else if (isym->st_shndx == SHN_ABS)
3585 sec = bfd_abs_section_ptr;
3586 else if (isym->st_shndx == SHN_COMMON)
3588 sec = bfd_com_section_ptr;
3589 /* What ELF calls the size we call the value. What ELF
3590 calls the value we call the alignment. */
3591 value = isym->st_size;
3595 /* Leave it up to the processor backend. */
3598 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3601 goto error_free_vers;
3603 if (isym->st_shndx == SHN_COMMON
3604 && ELF_ST_TYPE (isym->st_info) == STT_TLS)
3606 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3610 tcomm = bfd_make_section_with_flags (abfd, ".tcommon",
3613 | SEC_LINKER_CREATED
3614 | SEC_THREAD_LOCAL));
3616 goto error_free_vers;
3620 else if (add_symbol_hook)
3622 if (! (*add_symbol_hook) (abfd, info, isym, &name, &flags, &sec,
3624 goto error_free_vers;
3626 /* The hook function sets the name to NULL if this symbol
3627 should be skipped for some reason. */
3632 /* Sanity check that all possibilities were handled. */
3635 bfd_set_error (bfd_error_bad_value);
3636 goto error_free_vers;
3639 if (bfd_is_und_section (sec)
3640 || bfd_is_com_section (sec))
3645 size_change_ok = FALSE;
3646 type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
3651 if (is_elf_hash_table (hash_table))
3653 Elf_Internal_Versym iver;
3654 unsigned int vernum = 0;
3659 if (info->default_imported_symver)
3660 /* Use the default symbol version created earlier. */
3661 iver.vs_vers = elf_tdata (abfd)->cverdefs;
3666 _bfd_elf_swap_versym_in (abfd, ever, &iver);
3668 vernum = iver.vs_vers & VERSYM_VERSION;
3670 /* If this is a hidden symbol, or if it is not version
3671 1, we append the version name to the symbol name.
3672 However, we do not modify a non-hidden absolute symbol
3673 if it is not a function, because it might be the version
3674 symbol itself. FIXME: What if it isn't? */
3675 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
3676 || (vernum > 1 && (! bfd_is_abs_section (sec)
3677 || ELF_ST_TYPE (isym->st_info) == STT_FUNC)))
3680 size_t namelen, verlen, newlen;
3683 if (isym->st_shndx != SHN_UNDEF)
3685 if (vernum > elf_tdata (abfd)->cverdefs)
3687 else if (vernum > 1)
3689 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
3695 (*_bfd_error_handler)
3696 (_("%B: %s: invalid version %u (max %d)"),
3698 elf_tdata (abfd)->cverdefs);
3699 bfd_set_error (bfd_error_bad_value);
3700 goto error_free_vers;
3705 /* We cannot simply test for the number of
3706 entries in the VERNEED section since the
3707 numbers for the needed versions do not start
3709 Elf_Internal_Verneed *t;
3712 for (t = elf_tdata (abfd)->verref;
3716 Elf_Internal_Vernaux *a;
3718 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
3720 if (a->vna_other == vernum)
3722 verstr = a->vna_nodename;
3731 (*_bfd_error_handler)
3732 (_("%B: %s: invalid needed version %d"),
3733 abfd, name, vernum);
3734 bfd_set_error (bfd_error_bad_value);
3735 goto error_free_vers;
3739 namelen = strlen (name);
3740 verlen = strlen (verstr);
3741 newlen = namelen + verlen + 2;
3742 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3743 && isym->st_shndx != SHN_UNDEF)
3746 newname = bfd_alloc (abfd, newlen);
3747 if (newname == NULL)
3748 goto error_free_vers;
3749 memcpy (newname, name, namelen);
3750 p = newname + namelen;
3752 /* If this is a defined non-hidden version symbol,
3753 we add another @ to the name. This indicates the
3754 default version of the symbol. */
3755 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
3756 && isym->st_shndx != SHN_UNDEF)
3758 memcpy (p, verstr, verlen + 1);
3763 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
3764 &value, &old_alignment,
3765 sym_hash, &skip, &override,
3766 &type_change_ok, &size_change_ok))
3767 goto error_free_vers;
3776 while (h->root.type == bfd_link_hash_indirect
3777 || h->root.type == bfd_link_hash_warning)
3778 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3780 /* Remember the old alignment if this is a common symbol, so
3781 that we don't reduce the alignment later on. We can't
3782 check later, because _bfd_generic_link_add_one_symbol
3783 will set a default for the alignment which we want to
3784 override. We also remember the old bfd where the existing
3785 definition comes from. */
3786 switch (h->root.type)
3791 case bfd_link_hash_defined:
3792 case bfd_link_hash_defweak:
3793 old_bfd = h->root.u.def.section->owner;
3796 case bfd_link_hash_common:
3797 old_bfd = h->root.u.c.p->section->owner;
3798 old_alignment = h->root.u.c.p->alignment_power;
3802 if (elf_tdata (abfd)->verdef != NULL
3806 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
3809 if (! (_bfd_generic_link_add_one_symbol
3810 (info, abfd, name, flags, sec, value, NULL, FALSE, collect,
3811 (struct bfd_link_hash_entry **) sym_hash)))
3812 goto error_free_vers;
3815 while (h->root.type == bfd_link_hash_indirect
3816 || h->root.type == bfd_link_hash_warning)
3817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3820 new_weakdef = FALSE;
3823 && (flags & BSF_WEAK) != 0
3824 && ELF_ST_TYPE (isym->st_info) != STT_FUNC
3825 && is_elf_hash_table (hash_table)
3826 && h->u.weakdef == NULL)
3828 /* Keep a list of all weak defined non function symbols from
3829 a dynamic object, using the weakdef field. Later in this
3830 function we will set the weakdef field to the correct
3831 value. We only put non-function symbols from dynamic
3832 objects on this list, because that happens to be the only
3833 time we need to know the normal symbol corresponding to a
3834 weak symbol, and the information is time consuming to
3835 figure out. If the weakdef field is not already NULL,
3836 then this symbol was already defined by some previous
3837 dynamic object, and we will be using that previous
3838 definition anyhow. */
3840 h->u.weakdef = weaks;
3845 /* Set the alignment of a common symbol. */
3846 if ((common || bfd_is_com_section (sec))
3847 && h->root.type == bfd_link_hash_common)
3852 align = bfd_log2 (isym->st_value);
3855 /* The new symbol is a common symbol in a shared object.
3856 We need to get the alignment from the section. */
3857 align = new_sec->alignment_power;
3859 if (align > old_alignment
3860 /* Permit an alignment power of zero if an alignment of one
3861 is specified and no other alignments have been specified. */
3862 || (isym->st_value == 1 && old_alignment == 0))
3863 h->root.u.c.p->alignment_power = align;
3865 h->root.u.c.p->alignment_power = old_alignment;
3868 if (is_elf_hash_table (hash_table))
3872 /* Check the alignment when a common symbol is involved. This
3873 can change when a common symbol is overridden by a normal
3874 definition or a common symbol is ignored due to the old
3875 normal definition. We need to make sure the maximum
3876 alignment is maintained. */
3877 if ((old_alignment || common)
3878 && h->root.type != bfd_link_hash_common)
3880 unsigned int common_align;
3881 unsigned int normal_align;
3882 unsigned int symbol_align;
3886 symbol_align = ffs (h->root.u.def.value) - 1;
3887 if (h->root.u.def.section->owner != NULL
3888 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
3890 normal_align = h->root.u.def.section->alignment_power;
3891 if (normal_align > symbol_align)
3892 normal_align = symbol_align;
3895 normal_align = symbol_align;
3899 common_align = old_alignment;
3900 common_bfd = old_bfd;
3905 common_align = bfd_log2 (isym->st_value);
3907 normal_bfd = old_bfd;
3910 if (normal_align < common_align)
3911 (*_bfd_error_handler)
3912 (_("Warning: alignment %u of symbol `%s' in %B"
3913 " is smaller than %u in %B"),
3914 normal_bfd, common_bfd,
3915 1 << normal_align, name, 1 << common_align);
3918 /* Remember the symbol size and type. */
3919 if (isym->st_size != 0
3920 && (definition || h->size == 0))
3922 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok)
3923 (*_bfd_error_handler)
3924 (_("Warning: size of symbol `%s' changed"
3925 " from %lu in %B to %lu in %B"),
3927 name, (unsigned long) h->size,
3928 (unsigned long) isym->st_size);
3930 h->size = isym->st_size;
3933 /* If this is a common symbol, then we always want H->SIZE
3934 to be the size of the common symbol. The code just above
3935 won't fix the size if a common symbol becomes larger. We
3936 don't warn about a size change here, because that is
3937 covered by --warn-common. */
3938 if (h->root.type == bfd_link_hash_common)
3939 h->size = h->root.u.c.size;
3941 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
3942 && (definition || h->type == STT_NOTYPE))
3944 if (h->type != STT_NOTYPE
3945 && h->type != ELF_ST_TYPE (isym->st_info)
3946 && ! type_change_ok)
3947 (*_bfd_error_handler)
3948 (_("Warning: type of symbol `%s' changed"
3949 " from %d to %d in %B"),
3950 abfd, name, h->type, ELF_ST_TYPE (isym->st_info));
3952 h->type = ELF_ST_TYPE (isym->st_info);
3955 /* If st_other has a processor-specific meaning, specific
3956 code might be needed here. We never merge the visibility
3957 attribute with the one from a dynamic object. */
3958 if (bed->elf_backend_merge_symbol_attribute)
3959 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
3962 /* If this symbol has default visibility and the user has requested
3963 we not re-export it, then mark it as hidden. */
3964 if (definition && !dynamic
3966 || (abfd->my_archive && abfd->my_archive->no_export))
3967 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
3968 isym->st_other = STV_HIDDEN | (isym->st_other & ~ ELF_ST_VISIBILITY (-1));
3970 if (isym->st_other != 0 && !dynamic)
3972 unsigned char hvis, symvis, other, nvis;
3974 /* Take the balance of OTHER from the definition. */
3975 other = (definition ? isym->st_other : h->other);
3976 other &= ~ ELF_ST_VISIBILITY (-1);
3978 /* Combine visibilities, using the most constraining one. */
3979 hvis = ELF_ST_VISIBILITY (h->other);
3980 symvis = ELF_ST_VISIBILITY (isym->st_other);
3986 nvis = hvis < symvis ? hvis : symvis;
3988 h->other = other | nvis;
3991 /* Set a flag in the hash table entry indicating the type of
3992 reference or definition we just found. Keep a count of
3993 the number of dynamic symbols we find. A dynamic symbol
3994 is one which is referenced or defined by both a regular
3995 object and a shared object. */
4002 if (bind != STB_WEAK)
4003 h->ref_regular_nonweak = 1;
4007 if (! info->executable
4020 || (h->u.weakdef != NULL
4022 && h->u.weakdef->dynindx != -1))
4026 /* Check to see if we need to add an indirect symbol for
4027 the default name. */
4028 if (definition || h->root.type == bfd_link_hash_common)
4029 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4030 &sec, &value, &dynsym,
4032 goto error_free_vers;
4034 if (definition && !dynamic)
4036 char *p = strchr (name, ELF_VER_CHR);
4037 if (p != NULL && p[1] != ELF_VER_CHR)
4039 /* Queue non-default versions so that .symver x, x@FOO
4040 aliases can be checked. */
4041 if (! nondeflt_vers)
4043 amt = (isymend - isym + 1)
4044 * sizeof (struct elf_link_hash_entry *);
4045 nondeflt_vers = bfd_malloc (amt);
4047 nondeflt_vers [nondeflt_vers_cnt++] = h;
4051 if (dynsym && h->dynindx == -1)
4053 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4054 goto error_free_vers;
4055 if (h->u.weakdef != NULL
4057 && h->u.weakdef->dynindx == -1)
4059 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4060 goto error_free_vers;
4063 else if (dynsym && h->dynindx != -1)
4064 /* If the symbol already has a dynamic index, but
4065 visibility says it should not be visible, turn it into
4067 switch (ELF_ST_VISIBILITY (h->other))
4071 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4082 const char *soname = elf_dt_name (abfd);
4084 /* A symbol from a library loaded via DT_NEEDED of some
4085 other library is referenced by a regular object.
4086 Add a DT_NEEDED entry for it. Issue an error if
4087 --no-add-needed is used. */
4088 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4090 (*_bfd_error_handler)
4091 (_("%s: invalid DSO for symbol `%s' definition"),
4093 bfd_set_error (bfd_error_bad_value);
4094 goto error_free_vers;
4097 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED;
4100 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4102 goto error_free_vers;
4104 BFD_ASSERT (ret == 0);
4109 /* Now that all the symbols from this input file are created, handle
4110 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4111 if (nondeflt_vers != NULL)
4113 bfd_size_type cnt, symidx;
4115 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4117 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4118 char *shortname, *p;
4120 p = strchr (h->root.root.string, ELF_VER_CHR);
4122 || (h->root.type != bfd_link_hash_defined
4123 && h->root.type != bfd_link_hash_defweak))
4126 amt = p - h->root.root.string;
4127 shortname = bfd_malloc (amt + 1);
4128 memcpy (shortname, h->root.root.string, amt);
4129 shortname[amt] = '\0';
4131 hi = (struct elf_link_hash_entry *)
4132 bfd_link_hash_lookup (&hash_table->root, shortname,
4133 FALSE, FALSE, FALSE);
4135 && hi->root.type == h->root.type
4136 && hi->root.u.def.value == h->root.u.def.value
4137 && hi->root.u.def.section == h->root.u.def.section)
4139 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4140 hi->root.type = bfd_link_hash_indirect;
4141 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4142 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4143 sym_hash = elf_sym_hashes (abfd);
4145 for (symidx = 0; symidx < extsymcount; ++symidx)
4146 if (sym_hash[symidx] == hi)
4148 sym_hash[symidx] = h;
4154 free (nondeflt_vers);
4155 nondeflt_vers = NULL;
4158 if (extversym != NULL)
4164 if (isymbuf != NULL)
4169 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4171 /* Remove symbols defined in an as-needed shared lib that wasn't
4173 struct elf_smash_syms_data inf;
4174 inf.not_needed = abfd;
4175 inf.htab = hash_table;
4176 inf.twiddled = FALSE;
4177 elf_link_hash_traverse (hash_table, elf_smash_syms, &inf);
4179 bfd_link_repair_undef_list (&hash_table->root);
4183 /* Now set the weakdefs field correctly for all the weak defined
4184 symbols we found. The only way to do this is to search all the
4185 symbols. Since we only need the information for non functions in
4186 dynamic objects, that's the only time we actually put anything on
4187 the list WEAKS. We need this information so that if a regular
4188 object refers to a symbol defined weakly in a dynamic object, the
4189 real symbol in the dynamic object is also put in the dynamic
4190 symbols; we also must arrange for both symbols to point to the
4191 same memory location. We could handle the general case of symbol
4192 aliasing, but a general symbol alias can only be generated in
4193 assembler code, handling it correctly would be very time
4194 consuming, and other ELF linkers don't handle general aliasing
4198 struct elf_link_hash_entry **hpp;
4199 struct elf_link_hash_entry **hppend;
4200 struct elf_link_hash_entry **sorted_sym_hash;
4201 struct elf_link_hash_entry *h;
4204 /* Since we have to search the whole symbol list for each weak
4205 defined symbol, search time for N weak defined symbols will be
4206 O(N^2). Binary search will cut it down to O(NlogN). */
4207 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4208 sorted_sym_hash = bfd_malloc (amt);
4209 if (sorted_sym_hash == NULL)
4211 sym_hash = sorted_sym_hash;
4212 hpp = elf_sym_hashes (abfd);
4213 hppend = hpp + extsymcount;
4215 for (; hpp < hppend; hpp++)
4219 && h->root.type == bfd_link_hash_defined
4220 && h->type != STT_FUNC)
4228 qsort (sorted_sym_hash, sym_count,
4229 sizeof (struct elf_link_hash_entry *),
4232 while (weaks != NULL)
4234 struct elf_link_hash_entry *hlook;
4241 weaks = hlook->u.weakdef;
4242 hlook->u.weakdef = NULL;
4244 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4245 || hlook->root.type == bfd_link_hash_defweak
4246 || hlook->root.type == bfd_link_hash_common
4247 || hlook->root.type == bfd_link_hash_indirect);
4248 slook = hlook->root.u.def.section;
4249 vlook = hlook->root.u.def.value;
4256 bfd_signed_vma vdiff;
4258 h = sorted_sym_hash [idx];
4259 vdiff = vlook - h->root.u.def.value;
4266 long sdiff = slook->id - h->root.u.def.section->id;
4279 /* We didn't find a value/section match. */
4283 for (i = ilook; i < sym_count; i++)
4285 h = sorted_sym_hash [i];
4287 /* Stop if value or section doesn't match. */
4288 if (h->root.u.def.value != vlook
4289 || h->root.u.def.section != slook)
4291 else if (h != hlook)
4293 hlook->u.weakdef = h;
4295 /* If the weak definition is in the list of dynamic
4296 symbols, make sure the real definition is put
4298 if (hlook->dynindx != -1 && h->dynindx == -1)
4300 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4304 /* If the real definition is in the list of dynamic
4305 symbols, make sure the weak definition is put
4306 there as well. If we don't do this, then the
4307 dynamic loader might not merge the entries for the
4308 real definition and the weak definition. */
4309 if (h->dynindx != -1 && hlook->dynindx == -1)
4311 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4319 free (sorted_sym_hash);
4322 check_directives = get_elf_backend_data (abfd)->check_directives;
4323 if (check_directives)
4324 check_directives (abfd, info);
4326 /* If this object is the same format as the output object, and it is
4327 not a shared library, then let the backend look through the
4330 This is required to build global offset table entries and to
4331 arrange for dynamic relocs. It is not required for the
4332 particular common case of linking non PIC code, even when linking
4333 against shared libraries, but unfortunately there is no way of
4334 knowing whether an object file has been compiled PIC or not.
4335 Looking through the relocs is not particularly time consuming.
4336 The problem is that we must either (1) keep the relocs in memory,
4337 which causes the linker to require additional runtime memory or
4338 (2) read the relocs twice from the input file, which wastes time.
4339 This would be a good case for using mmap.
4341 I have no idea how to handle linking PIC code into a file of a
4342 different format. It probably can't be done. */
4343 check_relocs = get_elf_backend_data (abfd)->check_relocs;
4345 && is_elf_hash_table (hash_table)
4346 && hash_table->root.creator == abfd->xvec
4347 && check_relocs != NULL)
4351 for (o = abfd->sections; o != NULL; o = o->next)
4353 Elf_Internal_Rela *internal_relocs;
4356 if ((o->flags & SEC_RELOC) == 0
4357 || o->reloc_count == 0
4358 || ((info->strip == strip_all || info->strip == strip_debugger)
4359 && (o->flags & SEC_DEBUGGING) != 0)
4360 || bfd_is_abs_section (o->output_section))
4363 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4365 if (internal_relocs == NULL)
4368 ok = (*check_relocs) (abfd, info, o, internal_relocs);
4370 if (elf_section_data (o)->relocs != internal_relocs)
4371 free (internal_relocs);
4378 /* If this is a non-traditional link, try to optimize the handling
4379 of the .stab/.stabstr sections. */
4381 && ! info->traditional_format
4382 && is_elf_hash_table (hash_table)
4383 && (info->strip != strip_all && info->strip != strip_debugger))
4387 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4388 if (stabstr != NULL)
4390 bfd_size_type string_offset = 0;
4393 for (stab = abfd->sections; stab; stab = stab->next)
4394 if (strncmp (".stab", stab->name, 5) == 0
4395 && (!stab->name[5] ||
4396 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4397 && (stab->flags & SEC_MERGE) == 0
4398 && !bfd_is_abs_section (stab->output_section))
4400 struct bfd_elf_section_data *secdata;
4402 secdata = elf_section_data (stab);
4403 if (! _bfd_link_section_stabs (abfd,
4404 &hash_table->stab_info,
4409 if (secdata->sec_info)
4410 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4415 if (is_elf_hash_table (hash_table) && add_needed)
4417 /* Add this bfd to the loaded list. */
4418 struct elf_link_loaded_list *n;
4420 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4424 n->next = hash_table->loaded;
4425 hash_table->loaded = n;
4431 if (nondeflt_vers != NULL)
4432 free (nondeflt_vers);
4433 if (extversym != NULL)
4436 if (isymbuf != NULL)
4442 /* Return the linker hash table entry of a symbol that might be
4443 satisfied by an archive symbol. Return -1 on error. */
4445 struct elf_link_hash_entry *
4446 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4447 struct bfd_link_info *info,
4450 struct elf_link_hash_entry *h;
4454 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4458 /* If this is a default version (the name contains @@), look up the
4459 symbol again with only one `@' as well as without the version.
4460 The effect is that references to the symbol with and without the
4461 version will be matched by the default symbol in the archive. */
4463 p = strchr (name, ELF_VER_CHR);
4464 if (p == NULL || p[1] != ELF_VER_CHR)
4467 /* First check with only one `@'. */
4468 len = strlen (name);
4469 copy = bfd_alloc (abfd, len);
4471 return (struct elf_link_hash_entry *) 0 - 1;
4473 first = p - name + 1;
4474 memcpy (copy, name, first);
4475 memcpy (copy + first, name + first + 1, len - first);
4477 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE);
4480 /* We also need to check references to the symbol without the
4482 copy[first - 1] = '\0';
4483 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4484 FALSE, FALSE, FALSE);
4487 bfd_release (abfd, copy);
4491 /* Add symbols from an ELF archive file to the linker hash table. We
4492 don't use _bfd_generic_link_add_archive_symbols because of a
4493 problem which arises on UnixWare. The UnixWare libc.so is an
4494 archive which includes an entry libc.so.1 which defines a bunch of
4495 symbols. The libc.so archive also includes a number of other
4496 object files, which also define symbols, some of which are the same
4497 as those defined in libc.so.1. Correct linking requires that we
4498 consider each object file in turn, and include it if it defines any
4499 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4500 this; it looks through the list of undefined symbols, and includes
4501 any object file which defines them. When this algorithm is used on
4502 UnixWare, it winds up pulling in libc.so.1 early and defining a
4503 bunch of symbols. This means that some of the other objects in the
4504 archive are not included in the link, which is incorrect since they
4505 precede libc.so.1 in the archive.
4507 Fortunately, ELF archive handling is simpler than that done by
4508 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4509 oddities. In ELF, if we find a symbol in the archive map, and the
4510 symbol is currently undefined, we know that we must pull in that
4513 Unfortunately, we do have to make multiple passes over the symbol
4514 table until nothing further is resolved. */
4517 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4520 bfd_boolean *defined = NULL;
4521 bfd_boolean *included = NULL;
4525 const struct elf_backend_data *bed;
4526 struct elf_link_hash_entry * (*archive_symbol_lookup)
4527 (bfd *, struct bfd_link_info *, const char *);
4529 if (! bfd_has_map (abfd))
4531 /* An empty archive is a special case. */
4532 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4534 bfd_set_error (bfd_error_no_armap);
4538 /* Keep track of all symbols we know to be already defined, and all
4539 files we know to be already included. This is to speed up the
4540 second and subsequent passes. */
4541 c = bfd_ardata (abfd)->symdef_count;
4545 amt *= sizeof (bfd_boolean);
4546 defined = bfd_zmalloc (amt);
4547 included = bfd_zmalloc (amt);
4548 if (defined == NULL || included == NULL)
4551 symdefs = bfd_ardata (abfd)->symdefs;
4552 bed = get_elf_backend_data (abfd);
4553 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4566 symdefend = symdef + c;
4567 for (i = 0; symdef < symdefend; symdef++, i++)
4569 struct elf_link_hash_entry *h;
4571 struct bfd_link_hash_entry *undefs_tail;
4574 if (defined[i] || included[i])
4576 if (symdef->file_offset == last)
4582 h = archive_symbol_lookup (abfd, info, symdef->name);
4583 if (h == (struct elf_link_hash_entry *) 0 - 1)
4589 if (h->root.type == bfd_link_hash_common)
4591 /* We currently have a common symbol. The archive map contains
4592 a reference to this symbol, so we may want to include it. We
4593 only want to include it however, if this archive element
4594 contains a definition of the symbol, not just another common
4597 Unfortunately some archivers (including GNU ar) will put
4598 declarations of common symbols into their archive maps, as
4599 well as real definitions, so we cannot just go by the archive
4600 map alone. Instead we must read in the element's symbol
4601 table and check that to see what kind of symbol definition
4603 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
4606 else if (h->root.type != bfd_link_hash_undefined)
4608 if (h->root.type != bfd_link_hash_undefweak)
4613 /* We need to include this archive member. */
4614 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
4615 if (element == NULL)
4618 if (! bfd_check_format (element, bfd_object))
4621 /* Doublecheck that we have not included this object
4622 already--it should be impossible, but there may be
4623 something wrong with the archive. */
4624 if (element->archive_pass != 0)
4626 bfd_set_error (bfd_error_bad_value);
4629 element->archive_pass = 1;
4631 undefs_tail = info->hash->undefs_tail;
4633 if (! (*info->callbacks->add_archive_element) (info, element,
4636 if (! bfd_link_add_symbols (element, info))
4639 /* If there are any new undefined symbols, we need to make
4640 another pass through the archive in order to see whether
4641 they can be defined. FIXME: This isn't perfect, because
4642 common symbols wind up on undefs_tail and because an
4643 undefined symbol which is defined later on in this pass
4644 does not require another pass. This isn't a bug, but it
4645 does make the code less efficient than it could be. */
4646 if (undefs_tail != info->hash->undefs_tail)
4649 /* Look backward to mark all symbols from this object file
4650 which we have already seen in this pass. */
4654 included[mark] = TRUE;
4659 while (symdefs[mark].file_offset == symdef->file_offset);
4661 /* We mark subsequent symbols from this object file as we go
4662 on through the loop. */
4663 last = symdef->file_offset;
4674 if (defined != NULL)
4676 if (included != NULL)
4681 /* Given an ELF BFD, add symbols to the global hash table as
4685 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
4687 switch (bfd_get_format (abfd))
4690 return elf_link_add_object_symbols (abfd, info);
4692 return elf_link_add_archive_symbols (abfd, info);
4694 bfd_set_error (bfd_error_wrong_format);
4699 /* This function will be called though elf_link_hash_traverse to store
4700 all hash value of the exported symbols in an array. */
4703 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
4705 unsigned long **valuep = data;
4711 if (h->root.type == bfd_link_hash_warning)
4712 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4714 /* Ignore indirect symbols. These are added by the versioning code. */
4715 if (h->dynindx == -1)
4718 name = h->root.root.string;
4719 p = strchr (name, ELF_VER_CHR);
4722 alc = bfd_malloc (p - name + 1);
4723 memcpy (alc, name, p - name);
4724 alc[p - name] = '\0';
4728 /* Compute the hash value. */
4729 ha = bfd_elf_hash (name);
4731 /* Store the found hash value in the array given as the argument. */
4734 /* And store it in the struct so that we can put it in the hash table
4736 h->u.elf_hash_value = ha;
4744 /* Array used to determine the number of hash table buckets to use
4745 based on the number of symbols there are. If there are fewer than
4746 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
4747 fewer than 37 we use 17 buckets, and so forth. We never use more
4748 than 32771 buckets. */
4750 static const size_t elf_buckets[] =
4752 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
4756 /* Compute bucket count for hashing table. We do not use a static set
4757 of possible tables sizes anymore. Instead we determine for all
4758 possible reasonable sizes of the table the outcome (i.e., the
4759 number of collisions etc) and choose the best solution. The
4760 weighting functions are not too simple to allow the table to grow
4761 without bounds. Instead one of the weighting factors is the size.
4762 Therefore the result is always a good payoff between few collisions
4763 (= short chain lengths) and table size. */
4765 compute_bucket_count (struct bfd_link_info *info)
4767 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
4768 size_t best_size = 0;
4769 unsigned long int *hashcodes;
4770 unsigned long int *hashcodesp;
4771 unsigned long int i;
4774 /* Compute the hash values for all exported symbols. At the same
4775 time store the values in an array so that we could use them for
4778 amt *= sizeof (unsigned long int);
4779 hashcodes = bfd_malloc (amt);
4780 if (hashcodes == NULL)
4782 hashcodesp = hashcodes;
4784 /* Put all hash values in HASHCODES. */
4785 elf_link_hash_traverse (elf_hash_table (info),
4786 elf_collect_hash_codes, &hashcodesp);
4788 /* We have a problem here. The following code to optimize the table
4789 size requires an integer type with more the 32 bits. If
4790 BFD_HOST_U_64_BIT is set we know about such a type. */
4791 #ifdef BFD_HOST_U_64_BIT
4794 unsigned long int nsyms = hashcodesp - hashcodes;
4797 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
4798 unsigned long int *counts ;
4799 bfd *dynobj = elf_hash_table (info)->dynobj;
4800 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
4802 /* Possible optimization parameters: if we have NSYMS symbols we say
4803 that the hashing table must at least have NSYMS/4 and at most
4805 minsize = nsyms / 4;
4808 best_size = maxsize = nsyms * 2;
4810 /* Create array where we count the collisions in. We must use bfd_malloc
4811 since the size could be large. */
4813 amt *= sizeof (unsigned long int);
4814 counts = bfd_malloc (amt);
4821 /* Compute the "optimal" size for the hash table. The criteria is a
4822 minimal chain length. The minor criteria is (of course) the size
4824 for (i = minsize; i < maxsize; ++i)
4826 /* Walk through the array of hashcodes and count the collisions. */
4827 BFD_HOST_U_64_BIT max;
4828 unsigned long int j;
4829 unsigned long int fact;
4831 memset (counts, '\0', i * sizeof (unsigned long int));
4833 /* Determine how often each hash bucket is used. */
4834 for (j = 0; j < nsyms; ++j)
4835 ++counts[hashcodes[j] % i];
4837 /* For the weight function we need some information about the
4838 pagesize on the target. This is information need not be 100%
4839 accurate. Since this information is not available (so far) we
4840 define it here to a reasonable default value. If it is crucial
4841 to have a better value some day simply define this value. */
4842 # ifndef BFD_TARGET_PAGESIZE
4843 # define BFD_TARGET_PAGESIZE (4096)
4846 /* We in any case need 2 + NSYMS entries for the size values and
4848 max = (2 + nsyms) * (bed->s->arch_size / 8);
4851 /* Variant 1: optimize for short chains. We add the squares
4852 of all the chain lengths (which favors many small chain
4853 over a few long chains). */
4854 for (j = 0; j < i; ++j)
4855 max += counts[j] * counts[j];
4857 /* This adds penalties for the overall size of the table. */
4858 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4861 /* Variant 2: Optimize a lot more for small table. Here we
4862 also add squares of the size but we also add penalties for
4863 empty slots (the +1 term). */
4864 for (j = 0; j < i; ++j)
4865 max += (1 + counts[j]) * (1 + counts[j]);
4867 /* The overall size of the table is considered, but not as
4868 strong as in variant 1, where it is squared. */
4869 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1;
4873 /* Compare with current best results. */
4874 if (max < best_chlen)
4884 #endif /* defined (BFD_HOST_U_64_BIT) */
4886 /* This is the fallback solution if no 64bit type is available or if we
4887 are not supposed to spend much time on optimizations. We select the
4888 bucket count using a fixed set of numbers. */
4889 for (i = 0; elf_buckets[i] != 0; i++)
4891 best_size = elf_buckets[i];
4892 if (dynsymcount < elf_buckets[i + 1])
4897 /* Free the arrays we needed. */
4903 /* Set up the sizes and contents of the ELF dynamic sections. This is
4904 called by the ELF linker emulation before_allocation routine. We
4905 must set the sizes of the sections before the linker sets the
4906 addresses of the various sections. */
4909 bfd_elf_size_dynamic_sections (bfd *output_bfd,
4912 const char *filter_shlib,
4913 const char * const *auxiliary_filters,
4914 struct bfd_link_info *info,
4915 asection **sinterpptr,
4916 struct bfd_elf_version_tree *verdefs)
4918 bfd_size_type soname_indx;
4920 const struct elf_backend_data *bed;
4921 struct elf_assign_sym_version_info asvinfo;
4925 soname_indx = (bfd_size_type) -1;
4927 if (!is_elf_hash_table (info->hash))
4930 elf_tdata (output_bfd)->relro = info->relro;
4931 if (info->execstack)
4932 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
4933 else if (info->noexecstack)
4934 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
4938 asection *notesec = NULL;
4941 for (inputobj = info->input_bfds;
4943 inputobj = inputobj->link_next)
4947 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED))
4949 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
4952 if (s->flags & SEC_CODE)
4961 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
4962 if (exec && info->relocatable
4963 && notesec->output_section != bfd_abs_section_ptr)
4964 notesec->output_section->flags |= SEC_CODE;
4968 /* Any syms created from now on start with -1 in
4969 got.refcount/offset and plt.refcount/offset. */
4970 elf_hash_table (info)->init_got_refcount
4971 = elf_hash_table (info)->init_got_offset;
4972 elf_hash_table (info)->init_plt_refcount
4973 = elf_hash_table (info)->init_plt_offset;
4975 /* The backend may have to create some sections regardless of whether
4976 we're dynamic or not. */
4977 bed = get_elf_backend_data (output_bfd);
4978 if (bed->elf_backend_always_size_sections
4979 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
4982 dynobj = elf_hash_table (info)->dynobj;
4984 /* If there were no dynamic objects in the link, there is nothing to
4989 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
4992 if (elf_hash_table (info)->dynamic_sections_created)
4994 struct elf_info_failed eif;
4995 struct elf_link_hash_entry *h;
4997 struct bfd_elf_version_tree *t;
4998 struct bfd_elf_version_expr *d;
5000 bfd_boolean all_defined;
5002 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5003 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5007 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5009 if (soname_indx == (bfd_size_type) -1
5010 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5016 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5018 info->flags |= DF_SYMBOLIC;
5025 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5027 if (indx == (bfd_size_type) -1
5028 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5031 if (info->new_dtags)
5033 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5034 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5039 if (filter_shlib != NULL)
5043 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5044 filter_shlib, TRUE);
5045 if (indx == (bfd_size_type) -1
5046 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5050 if (auxiliary_filters != NULL)
5052 const char * const *p;
5054 for (p = auxiliary_filters; *p != NULL; p++)
5058 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5060 if (indx == (bfd_size_type) -1
5061 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5067 eif.verdefs = verdefs;
5070 /* If we are supposed to export all symbols into the dynamic symbol
5071 table (this is not the normal case), then do so. */
5072 if (info->export_dynamic)
5074 elf_link_hash_traverse (elf_hash_table (info),
5075 _bfd_elf_export_symbol,
5081 /* Make all global versions with definition. */
5082 for (t = verdefs; t != NULL; t = t->next)
5083 for (d = t->globals.list; d != NULL; d = d->next)
5084 if (!d->symver && d->symbol)
5086 const char *verstr, *name;
5087 size_t namelen, verlen, newlen;
5089 struct elf_link_hash_entry *newh;
5092 namelen = strlen (name);
5094 verlen = strlen (verstr);
5095 newlen = namelen + verlen + 3;
5097 newname = bfd_malloc (newlen);
5098 if (newname == NULL)
5100 memcpy (newname, name, namelen);
5102 /* Check the hidden versioned definition. */
5103 p = newname + namelen;
5105 memcpy (p, verstr, verlen + 1);
5106 newh = elf_link_hash_lookup (elf_hash_table (info),
5107 newname, FALSE, FALSE,
5110 || (newh->root.type != bfd_link_hash_defined
5111 && newh->root.type != bfd_link_hash_defweak))
5113 /* Check the default versioned definition. */
5115 memcpy (p, verstr, verlen + 1);
5116 newh = elf_link_hash_lookup (elf_hash_table (info),
5117 newname, FALSE, FALSE,
5122 /* Mark this version if there is a definition and it is
5123 not defined in a shared object. */
5125 && !newh->def_dynamic
5126 && (newh->root.type == bfd_link_hash_defined
5127 || newh->root.type == bfd_link_hash_defweak))
5131 /* Attach all the symbols to their version information. */
5132 asvinfo.output_bfd = output_bfd;
5133 asvinfo.info = info;
5134 asvinfo.verdefs = verdefs;
5135 asvinfo.failed = FALSE;
5137 elf_link_hash_traverse (elf_hash_table (info),
5138 _bfd_elf_link_assign_sym_version,
5143 if (!info->allow_undefined_version)
5145 /* Check if all global versions have a definition. */
5147 for (t = verdefs; t != NULL; t = t->next)
5148 for (d = t->globals.list; d != NULL; d = d->next)
5149 if (!d->symver && !d->script)
5151 (*_bfd_error_handler)
5152 (_("%s: undefined version: %s"),
5153 d->pattern, t->name);
5154 all_defined = FALSE;
5159 bfd_set_error (bfd_error_bad_value);
5164 /* Find all symbols which were defined in a dynamic object and make
5165 the backend pick a reasonable value for them. */
5166 elf_link_hash_traverse (elf_hash_table (info),
5167 _bfd_elf_adjust_dynamic_symbol,
5172 /* Add some entries to the .dynamic section. We fill in some of the
5173 values later, in bfd_elf_final_link, but we must add the entries
5174 now so that we know the final size of the .dynamic section. */
5176 /* If there are initialization and/or finalization functions to
5177 call then add the corresponding DT_INIT/DT_FINI entries. */
5178 h = (info->init_function
5179 ? elf_link_hash_lookup (elf_hash_table (info),
5180 info->init_function, FALSE,
5187 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5190 h = (info->fini_function
5191 ? elf_link_hash_lookup (elf_hash_table (info),
5192 info->fini_function, FALSE,
5199 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5203 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5204 if (s != NULL && s->linker_has_input)
5206 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5207 if (! info->executable)
5212 for (sub = info->input_bfds; sub != NULL;
5213 sub = sub->link_next)
5214 for (o = sub->sections; o != NULL; o = o->next)
5215 if (elf_section_data (o)->this_hdr.sh_type
5216 == SHT_PREINIT_ARRAY)
5218 (*_bfd_error_handler)
5219 (_("%B: .preinit_array section is not allowed in DSO"),
5224 bfd_set_error (bfd_error_nonrepresentable_section);
5228 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5229 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5232 s = bfd_get_section_by_name (output_bfd, ".init_array");
5233 if (s != NULL && s->linker_has_input)
5235 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5236 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5239 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5240 if (s != NULL && s->linker_has_input)
5242 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5243 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5247 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5248 /* If .dynstr is excluded from the link, we don't want any of
5249 these tags. Strictly, we should be checking each section
5250 individually; This quick check covers for the case where
5251 someone does a /DISCARD/ : { *(*) }. */
5252 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5254 bfd_size_type strsize;
5256 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5257 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)
5258 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5259 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5260 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5261 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5262 bed->s->sizeof_sym))
5267 /* The backend must work out the sizes of all the other dynamic
5269 if (bed->elf_backend_size_dynamic_sections
5270 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5273 if (elf_hash_table (info)->dynamic_sections_created)
5275 unsigned long section_sym_count;
5278 /* Set up the version definition section. */
5279 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5280 BFD_ASSERT (s != NULL);
5282 /* We may have created additional version definitions if we are
5283 just linking a regular application. */
5284 verdefs = asvinfo.verdefs;
5286 /* Skip anonymous version tag. */
5287 if (verdefs != NULL && verdefs->vernum == 0)
5288 verdefs = verdefs->next;
5290 if (verdefs == NULL && !info->create_default_symver)
5291 s->flags |= SEC_EXCLUDE;
5296 struct bfd_elf_version_tree *t;
5298 Elf_Internal_Verdef def;
5299 Elf_Internal_Verdaux defaux;
5300 struct bfd_link_hash_entry *bh;
5301 struct elf_link_hash_entry *h;
5307 /* Make space for the base version. */
5308 size += sizeof (Elf_External_Verdef);
5309 size += sizeof (Elf_External_Verdaux);
5312 /* Make space for the default version. */
5313 if (info->create_default_symver)
5315 size += sizeof (Elf_External_Verdef);
5319 for (t = verdefs; t != NULL; t = t->next)
5321 struct bfd_elf_version_deps *n;
5323 size += sizeof (Elf_External_Verdef);
5324 size += sizeof (Elf_External_Verdaux);
5327 for (n = t->deps; n != NULL; n = n->next)
5328 size += sizeof (Elf_External_Verdaux);
5332 s->contents = bfd_alloc (output_bfd, s->size);
5333 if (s->contents == NULL && s->size != 0)
5336 /* Fill in the version definition section. */
5340 def.vd_version = VER_DEF_CURRENT;
5341 def.vd_flags = VER_FLG_BASE;
5344 if (info->create_default_symver)
5346 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5347 def.vd_next = sizeof (Elf_External_Verdef);
5351 def.vd_aux = sizeof (Elf_External_Verdef);
5352 def.vd_next = (sizeof (Elf_External_Verdef)
5353 + sizeof (Elf_External_Verdaux));
5356 if (soname_indx != (bfd_size_type) -1)
5358 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5360 def.vd_hash = bfd_elf_hash (soname);
5361 defaux.vda_name = soname_indx;
5368 name = lbasename (output_bfd->filename);
5369 def.vd_hash = bfd_elf_hash (name);
5370 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5372 if (indx == (bfd_size_type) -1)
5374 defaux.vda_name = indx;
5376 defaux.vda_next = 0;
5378 _bfd_elf_swap_verdef_out (output_bfd, &def,
5379 (Elf_External_Verdef *) p);
5380 p += sizeof (Elf_External_Verdef);
5381 if (info->create_default_symver)
5383 /* Add a symbol representing this version. */
5385 if (! (_bfd_generic_link_add_one_symbol
5386 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
5388 get_elf_backend_data (dynobj)->collect, &bh)))
5390 h = (struct elf_link_hash_entry *) bh;
5393 h->type = STT_OBJECT;
5394 h->verinfo.vertree = NULL;
5396 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5399 /* Create a duplicate of the base version with the same
5400 aux block, but different flags. */
5403 def.vd_aux = sizeof (Elf_External_Verdef);
5405 def.vd_next = (sizeof (Elf_External_Verdef)
5406 + sizeof (Elf_External_Verdaux));
5409 _bfd_elf_swap_verdef_out (output_bfd, &def,
5410 (Elf_External_Verdef *) p);
5411 p += sizeof (Elf_External_Verdef);
5413 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5414 (Elf_External_Verdaux *) p);
5415 p += sizeof (Elf_External_Verdaux);
5417 for (t = verdefs; t != NULL; t = t->next)
5420 struct bfd_elf_version_deps *n;
5423 for (n = t->deps; n != NULL; n = n->next)
5426 /* Add a symbol representing this version. */
5428 if (! (_bfd_generic_link_add_one_symbol
5429 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
5431 get_elf_backend_data (dynobj)->collect, &bh)))
5433 h = (struct elf_link_hash_entry *) bh;
5436 h->type = STT_OBJECT;
5437 h->verinfo.vertree = t;
5439 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5442 def.vd_version = VER_DEF_CURRENT;
5444 if (t->globals.list == NULL
5445 && t->locals.list == NULL
5447 def.vd_flags |= VER_FLG_WEAK;
5448 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
5449 def.vd_cnt = cdeps + 1;
5450 def.vd_hash = bfd_elf_hash (t->name);
5451 def.vd_aux = sizeof (Elf_External_Verdef);
5453 if (t->next != NULL)
5454 def.vd_next = (sizeof (Elf_External_Verdef)
5455 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
5457 _bfd_elf_swap_verdef_out (output_bfd, &def,
5458 (Elf_External_Verdef *) p);
5459 p += sizeof (Elf_External_Verdef);
5461 defaux.vda_name = h->dynstr_index;
5462 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5464 defaux.vda_next = 0;
5465 if (t->deps != NULL)
5466 defaux.vda_next = sizeof (Elf_External_Verdaux);
5467 t->name_indx = defaux.vda_name;
5469 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5470 (Elf_External_Verdaux *) p);
5471 p += sizeof (Elf_External_Verdaux);
5473 for (n = t->deps; n != NULL; n = n->next)
5475 if (n->version_needed == NULL)
5477 /* This can happen if there was an error in the
5479 defaux.vda_name = 0;
5483 defaux.vda_name = n->version_needed->name_indx;
5484 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5487 if (n->next == NULL)
5488 defaux.vda_next = 0;
5490 defaux.vda_next = sizeof (Elf_External_Verdaux);
5492 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
5493 (Elf_External_Verdaux *) p);
5494 p += sizeof (Elf_External_Verdaux);
5498 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
5499 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
5502 elf_tdata (output_bfd)->cverdefs = cdefs;
5505 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
5507 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
5510 else if (info->flags & DF_BIND_NOW)
5512 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
5518 if (info->executable)
5519 info->flags_1 &= ~ (DF_1_INITFIRST
5522 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
5526 /* Work out the size of the version reference section. */
5528 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
5529 BFD_ASSERT (s != NULL);
5531 struct elf_find_verdep_info sinfo;
5533 sinfo.output_bfd = output_bfd;
5535 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
5536 if (sinfo.vers == 0)
5538 sinfo.failed = FALSE;
5540 elf_link_hash_traverse (elf_hash_table (info),
5541 _bfd_elf_link_find_version_dependencies,
5544 if (elf_tdata (output_bfd)->verref == NULL)
5545 s->flags |= SEC_EXCLUDE;
5548 Elf_Internal_Verneed *t;
5553 /* Build the version definition section. */
5556 for (t = elf_tdata (output_bfd)->verref;
5560 Elf_Internal_Vernaux *a;
5562 size += sizeof (Elf_External_Verneed);
5564 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5565 size += sizeof (Elf_External_Vernaux);
5569 s->contents = bfd_alloc (output_bfd, s->size);
5570 if (s->contents == NULL)
5574 for (t = elf_tdata (output_bfd)->verref;
5579 Elf_Internal_Vernaux *a;
5583 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5586 t->vn_version = VER_NEED_CURRENT;
5588 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5589 elf_dt_name (t->vn_bfd) != NULL
5590 ? elf_dt_name (t->vn_bfd)
5591 : lbasename (t->vn_bfd->filename),
5593 if (indx == (bfd_size_type) -1)
5596 t->vn_aux = sizeof (Elf_External_Verneed);
5597 if (t->vn_nextref == NULL)
5600 t->vn_next = (sizeof (Elf_External_Verneed)
5601 + caux * sizeof (Elf_External_Vernaux));
5603 _bfd_elf_swap_verneed_out (output_bfd, t,
5604 (Elf_External_Verneed *) p);
5605 p += sizeof (Elf_External_Verneed);
5607 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
5609 a->vna_hash = bfd_elf_hash (a->vna_nodename);
5610 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5611 a->vna_nodename, FALSE);
5612 if (indx == (bfd_size_type) -1)
5615 if (a->vna_nextptr == NULL)
5618 a->vna_next = sizeof (Elf_External_Vernaux);
5620 _bfd_elf_swap_vernaux_out (output_bfd, a,
5621 (Elf_External_Vernaux *) p);
5622 p += sizeof (Elf_External_Vernaux);
5626 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
5627 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
5630 elf_tdata (output_bfd)->cverrefs = crefs;
5634 if ((elf_tdata (output_bfd)->cverrefs == 0
5635 && elf_tdata (output_bfd)->cverdefs == 0)
5636 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5637 §ion_sym_count) == 0)
5639 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5640 s->flags |= SEC_EXCLUDE;
5647 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
5649 if (!is_elf_hash_table (info->hash))
5652 if (elf_hash_table (info)->dynamic_sections_created)
5655 const struct elf_backend_data *bed;
5657 bfd_size_type dynsymcount;
5658 unsigned long section_sym_count;
5659 size_t bucketcount = 0;
5660 size_t hash_entry_size;
5661 unsigned int dtagcount;
5663 dynobj = elf_hash_table (info)->dynobj;
5665 /* Assign dynsym indicies. In a shared library we generate a
5666 section symbol for each output section, which come first.
5667 Next come all of the back-end allocated local dynamic syms,
5668 followed by the rest of the global symbols. */
5670 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
5671 §ion_sym_count);
5673 /* Work out the size of the symbol version section. */
5674 s = bfd_get_section_by_name (dynobj, ".gnu.version");
5675 BFD_ASSERT (s != NULL);
5676 if (dynsymcount != 0
5677 && (s->flags & SEC_EXCLUDE) == 0)
5679 s->size = dynsymcount * sizeof (Elf_External_Versym);
5680 s->contents = bfd_zalloc (output_bfd, s->size);
5681 if (s->contents == NULL)
5684 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
5688 /* Set the size of the .dynsym and .hash sections. We counted
5689 the number of dynamic symbols in elf_link_add_object_symbols.
5690 We will build the contents of .dynsym and .hash when we build
5691 the final symbol table, because until then we do not know the
5692 correct value to give the symbols. We built the .dynstr
5693 section as we went along in elf_link_add_object_symbols. */
5694 s = bfd_get_section_by_name (dynobj, ".dynsym");
5695 BFD_ASSERT (s != NULL);
5696 bed = get_elf_backend_data (output_bfd);
5697 s->size = dynsymcount * bed->s->sizeof_sym;
5699 if (dynsymcount != 0)
5701 s->contents = bfd_alloc (output_bfd, s->size);
5702 if (s->contents == NULL)
5705 /* The first entry in .dynsym is a dummy symbol.
5706 Clear all the section syms, in case we don't output them all. */
5707 ++section_sym_count;
5708 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
5711 /* Compute the size of the hashing table. As a side effect this
5712 computes the hash values for all the names we export. */
5713 bucketcount = compute_bucket_count (info);
5715 s = bfd_get_section_by_name (dynobj, ".hash");
5716 BFD_ASSERT (s != NULL);
5717 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
5718 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
5719 s->contents = bfd_zalloc (output_bfd, s->size);
5720 if (s->contents == NULL)
5723 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
5724 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
5725 s->contents + hash_entry_size);
5727 elf_hash_table (info)->bucketcount = bucketcount;
5729 s = bfd_get_section_by_name (dynobj, ".dynstr");
5730 BFD_ASSERT (s != NULL);
5732 elf_finalize_dynstr (output_bfd, info);
5734 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5736 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
5737 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
5744 /* Final phase of ELF linker. */
5746 /* A structure we use to avoid passing large numbers of arguments. */
5748 struct elf_final_link_info
5750 /* General link information. */
5751 struct bfd_link_info *info;
5754 /* Symbol string table. */
5755 struct bfd_strtab_hash *symstrtab;
5756 /* .dynsym section. */
5757 asection *dynsym_sec;
5758 /* .hash section. */
5760 /* symbol version section (.gnu.version). */
5761 asection *symver_sec;
5762 /* Buffer large enough to hold contents of any section. */
5764 /* Buffer large enough to hold external relocs of any section. */
5765 void *external_relocs;
5766 /* Buffer large enough to hold internal relocs of any section. */
5767 Elf_Internal_Rela *internal_relocs;
5768 /* Buffer large enough to hold external local symbols of any input
5770 bfd_byte *external_syms;
5771 /* And a buffer for symbol section indices. */
5772 Elf_External_Sym_Shndx *locsym_shndx;
5773 /* Buffer large enough to hold internal local symbols of any input
5775 Elf_Internal_Sym *internal_syms;
5776 /* Array large enough to hold a symbol index for each local symbol
5777 of any input BFD. */
5779 /* Array large enough to hold a section pointer for each local
5780 symbol of any input BFD. */
5781 asection **sections;
5782 /* Buffer to hold swapped out symbols. */
5784 /* And one for symbol section indices. */
5785 Elf_External_Sym_Shndx *symshndxbuf;
5786 /* Number of swapped out symbols in buffer. */
5787 size_t symbuf_count;
5788 /* Number of symbols which fit in symbuf. */
5790 /* And same for symshndxbuf. */
5791 size_t shndxbuf_size;
5794 /* This struct is used to pass information to elf_link_output_extsym. */
5796 struct elf_outext_info
5799 bfd_boolean localsyms;
5800 struct elf_final_link_info *finfo;
5803 /* When performing a relocatable link, the input relocations are
5804 preserved. But, if they reference global symbols, the indices
5805 referenced must be updated. Update all the relocations in
5806 REL_HDR (there are COUNT of them), using the data in REL_HASH. */
5809 elf_link_adjust_relocs (bfd *abfd,
5810 Elf_Internal_Shdr *rel_hdr,
5812 struct elf_link_hash_entry **rel_hash)
5815 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5817 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5818 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5819 bfd_vma r_type_mask;
5822 if (rel_hdr->sh_entsize == bed->s->sizeof_rel)
5824 swap_in = bed->s->swap_reloc_in;
5825 swap_out = bed->s->swap_reloc_out;
5827 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela)
5829 swap_in = bed->s->swap_reloca_in;
5830 swap_out = bed->s->swap_reloca_out;
5835 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
5838 if (bed->s->arch_size == 32)
5845 r_type_mask = 0xffffffff;
5849 erela = rel_hdr->contents;
5850 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize)
5852 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
5855 if (*rel_hash == NULL)
5858 BFD_ASSERT ((*rel_hash)->indx >= 0);
5860 (*swap_in) (abfd, erela, irela);
5861 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
5862 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
5863 | (irela[j].r_info & r_type_mask));
5864 (*swap_out) (abfd, irela, erela);
5868 struct elf_link_sort_rela
5874 enum elf_reloc_type_class type;
5875 /* We use this as an array of size int_rels_per_ext_rel. */
5876 Elf_Internal_Rela rela[1];
5880 elf_link_sort_cmp1 (const void *A, const void *B)
5882 const struct elf_link_sort_rela *a = A;
5883 const struct elf_link_sort_rela *b = B;
5884 int relativea, relativeb;
5886 relativea = a->type == reloc_class_relative;
5887 relativeb = b->type == reloc_class_relative;
5889 if (relativea < relativeb)
5891 if (relativea > relativeb)
5893 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
5895 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
5897 if (a->rela->r_offset < b->rela->r_offset)
5899 if (a->rela->r_offset > b->rela->r_offset)
5905 elf_link_sort_cmp2 (const void *A, const void *B)
5907 const struct elf_link_sort_rela *a = A;
5908 const struct elf_link_sort_rela *b = B;
5911 if (a->u.offset < b->u.offset)
5913 if (a->u.offset > b->u.offset)
5915 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
5916 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
5921 if (a->rela->r_offset < b->rela->r_offset)
5923 if (a->rela->r_offset > b->rela->r_offset)
5929 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
5932 bfd_size_type count, size;
5933 size_t i, ret, sort_elt, ext_size;
5934 bfd_byte *sort, *s_non_relative, *p;
5935 struct elf_link_sort_rela *sq;
5936 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5937 int i2e = bed->s->int_rels_per_ext_rel;
5938 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
5939 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
5940 struct bfd_link_order *lo;
5943 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn");
5944 if (reldyn == NULL || reldyn->size == 0)
5946 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn");
5947 if (reldyn == NULL || reldyn->size == 0)
5949 ext_size = bed->s->sizeof_rel;
5950 swap_in = bed->s->swap_reloc_in;
5951 swap_out = bed->s->swap_reloc_out;
5955 ext_size = bed->s->sizeof_rela;
5956 swap_in = bed->s->swap_reloca_in;
5957 swap_out = bed->s->swap_reloca_out;
5959 count = reldyn->size / ext_size;
5962 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5963 if (lo->type == bfd_indirect_link_order)
5965 asection *o = lo->u.indirect.section;
5969 if (size != reldyn->size)
5972 sort_elt = (sizeof (struct elf_link_sort_rela)
5973 + (i2e - 1) * sizeof (Elf_Internal_Rela));
5974 sort = bfd_zmalloc (sort_elt * count);
5977 (*info->callbacks->warning)
5978 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
5982 if (bed->s->arch_size == 32)
5983 r_sym_mask = ~(bfd_vma) 0xff;
5985 r_sym_mask = ~(bfd_vma) 0xffffffff;
5987 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
5988 if (lo->type == bfd_indirect_link_order)
5990 bfd_byte *erel, *erelend;
5991 asection *o = lo->u.indirect.section;
5993 if (o->contents == NULL && o->size != 0)
5995 /* This is a reloc section that is being handled as a normal
5996 section. See bfd_section_from_shdr. We can't combine
5997 relocs in this case. */
6002 erelend = o->contents + o->size;
6003 p = sort + o->output_offset / ext_size * sort_elt;
6004 while (erel < erelend)
6006 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6007 (*swap_in) (abfd, erel, s->rela);
6008 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
6009 s->u.sym_mask = r_sym_mask;
6015 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
6017 for (i = 0, p = sort; i < count; i++, p += sort_elt)
6019 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6020 if (s->type != reloc_class_relative)
6026 sq = (struct elf_link_sort_rela *) s_non_relative;
6027 for (; i < count; i++, p += sort_elt)
6029 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
6030 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
6032 sp->u.offset = sq->rela->r_offset;
6035 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
6037 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next)
6038 if (lo->type == bfd_indirect_link_order)
6040 bfd_byte *erel, *erelend;
6041 asection *o = lo->u.indirect.section;
6044 erelend = o->contents + o->size;
6045 p = sort + o->output_offset / ext_size * sort_elt;
6046 while (erel < erelend)
6048 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
6049 (*swap_out) (abfd, s->rela, erel);
6060 /* Flush the output symbols to the file. */
6063 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
6064 const struct elf_backend_data *bed)
6066 if (finfo->symbuf_count > 0)
6068 Elf_Internal_Shdr *hdr;
6072 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
6073 pos = hdr->sh_offset + hdr->sh_size;
6074 amt = finfo->symbuf_count * bed->s->sizeof_sym;
6075 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
6076 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
6079 hdr->sh_size += amt;
6080 finfo->symbuf_count = 0;
6086 /* Add a symbol to the output symbol table. */
6089 elf_link_output_sym (struct elf_final_link_info *finfo,
6091 Elf_Internal_Sym *elfsym,
6092 asection *input_sec,
6093 struct elf_link_hash_entry *h)
6096 Elf_External_Sym_Shndx *destshndx;
6097 bfd_boolean (*output_symbol_hook)
6098 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
6099 struct elf_link_hash_entry *);
6100 const struct elf_backend_data *bed;
6102 bed = get_elf_backend_data (finfo->output_bfd);
6103 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
6104 if (output_symbol_hook != NULL)
6106 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h))
6110 if (name == NULL || *name == '\0')
6111 elfsym->st_name = 0;
6112 else if (input_sec->flags & SEC_EXCLUDE)
6113 elfsym->st_name = 0;
6116 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
6118 if (elfsym->st_name == (unsigned long) -1)
6122 if (finfo->symbuf_count >= finfo->symbuf_size)
6124 if (! elf_link_flush_output_syms (finfo, bed))
6128 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
6129 destshndx = finfo->symshndxbuf;
6130 if (destshndx != NULL)
6132 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
6136 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
6137 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2);
6138 if (destshndx == NULL)
6140 memset ((char *) destshndx + amt, 0, amt);
6141 finfo->shndxbuf_size *= 2;
6143 destshndx += bfd_get_symcount (finfo->output_bfd);
6146 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
6147 finfo->symbuf_count += 1;
6148 bfd_get_symcount (finfo->output_bfd) += 1;
6153 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
6154 allowing an unsatisfied unversioned symbol in the DSO to match a
6155 versioned symbol that would normally require an explicit version.
6156 We also handle the case that a DSO references a hidden symbol
6157 which may be satisfied by a versioned symbol in another DSO. */
6160 elf_link_check_versioned_symbol (struct bfd_link_info *info,
6161 const struct elf_backend_data *bed,
6162 struct elf_link_hash_entry *h)
6165 struct elf_link_loaded_list *loaded;
6167 if (!is_elf_hash_table (info->hash))
6170 switch (h->root.type)
6176 case bfd_link_hash_undefined:
6177 case bfd_link_hash_undefweak:
6178 abfd = h->root.u.undef.abfd;
6179 if ((abfd->flags & DYNAMIC) == 0
6180 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
6184 case bfd_link_hash_defined:
6185 case bfd_link_hash_defweak:
6186 abfd = h->root.u.def.section->owner;
6189 case bfd_link_hash_common:
6190 abfd = h->root.u.c.p->section->owner;
6193 BFD_ASSERT (abfd != NULL);
6195 for (loaded = elf_hash_table (info)->loaded;
6197 loaded = loaded->next)
6200 Elf_Internal_Shdr *hdr;
6201 bfd_size_type symcount;
6202 bfd_size_type extsymcount;
6203 bfd_size_type extsymoff;
6204 Elf_Internal_Shdr *versymhdr;
6205 Elf_Internal_Sym *isym;
6206 Elf_Internal_Sym *isymend;
6207 Elf_Internal_Sym *isymbuf;
6208 Elf_External_Versym *ever;
6209 Elf_External_Versym *extversym;
6211 input = loaded->abfd;
6213 /* We check each DSO for a possible hidden versioned definition. */
6215 || (input->flags & DYNAMIC) == 0
6216 || elf_dynversym (input) == 0)
6219 hdr = &elf_tdata (input)->dynsymtab_hdr;
6221 symcount = hdr->sh_size / bed->s->sizeof_sym;
6222 if (elf_bad_symtab (input))
6224 extsymcount = symcount;
6229 extsymcount = symcount - hdr->sh_info;
6230 extsymoff = hdr->sh_info;
6233 if (extsymcount == 0)
6236 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
6238 if (isymbuf == NULL)
6241 /* Read in any version definitions. */
6242 versymhdr = &elf_tdata (input)->dynversym_hdr;
6243 extversym = bfd_malloc (versymhdr->sh_size);
6244 if (extversym == NULL)
6247 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
6248 || (bfd_bread (extversym, versymhdr->sh_size, input)
6249 != versymhdr->sh_size))
6257 ever = extversym + extsymoff;
6258 isymend = isymbuf + extsymcount;
6259 for (isym = isymbuf; isym < isymend; isym++, ever++)
6262 Elf_Internal_Versym iver;
6263 unsigned short version_index;
6265 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
6266 || isym->st_shndx == SHN_UNDEF)
6269 name = bfd_elf_string_from_elf_section (input,
6272 if (strcmp (name, h->root.root.string) != 0)
6275 _bfd_elf_swap_versym_in (input, ever, &iver);
6277 if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
6279 /* If we have a non-hidden versioned sym, then it should
6280 have provided a definition for the undefined sym. */
6284 version_index = iver.vs_vers & VERSYM_VERSION;
6285 if (version_index == 1 || version_index == 2)
6287 /* This is the base or first version. We can use it. */
6301 /* Add an external symbol to the symbol table. This is called from
6302 the hash table traversal routine. When generating a shared object,
6303 we go through the symbol table twice. The first time we output
6304 anything that might have been forced to local scope in a version
6305 script. The second time we output the symbols that are still
6309 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data)
6311 struct elf_outext_info *eoinfo = data;
6312 struct elf_final_link_info *finfo = eoinfo->finfo;
6314 Elf_Internal_Sym sym;
6315 asection *input_sec;
6316 const struct elf_backend_data *bed;
6318 if (h->root.type == bfd_link_hash_warning)
6320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6321 if (h->root.type == bfd_link_hash_new)
6325 /* Decide whether to output this symbol in this pass. */
6326 if (eoinfo->localsyms)
6328 if (!h->forced_local)
6333 if (h->forced_local)
6337 bed = get_elf_backend_data (finfo->output_bfd);
6339 /* If we have an undefined symbol reference here then it must have
6340 come from a shared library that is being linked in. (Undefined
6341 references in regular files have already been handled). If we
6342 are reporting errors for this situation then do so now. */
6343 if (h->root.type == bfd_link_hash_undefined
6346 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
6347 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
6349 if (! ((*finfo->info->callbacks->undefined_symbol)
6350 (finfo->info, h->root.root.string, h->root.u.undef.abfd,
6351 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
6353 eoinfo->failed = TRUE;
6358 /* We should also warn if a forced local symbol is referenced from
6359 shared libraries. */
6360 if (! finfo->info->relocatable
6361 && (! finfo->info->shared)
6366 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
6368 (*_bfd_error_handler)
6369 (_("%B: %s symbol `%s' in %B is referenced by DSO"),
6371 h->root.u.def.section == bfd_abs_section_ptr
6372 ? finfo->output_bfd : h->root.u.def.section->owner,
6373 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
6375 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
6376 ? "hidden" : "local",
6377 h->root.root.string);
6378 eoinfo->failed = TRUE;
6382 /* We don't want to output symbols that have never been mentioned by
6383 a regular file, or that we have been told to strip. However, if
6384 h->indx is set to -2, the symbol is used by a reloc and we must
6388 else if ((h->def_dynamic
6390 || h->root.type == bfd_link_hash_new)
6394 else if (finfo->info->strip == strip_all)
6396 else if (finfo->info->strip == strip_some
6397 && bfd_hash_lookup (finfo->info->keep_hash,
6398 h->root.root.string, FALSE, FALSE) == NULL)
6400 else if (finfo->info->strip_discarded
6401 && (h->root.type == bfd_link_hash_defined
6402 || h->root.type == bfd_link_hash_defweak)
6403 && elf_discarded_section (h->root.u.def.section))
6408 /* If we're stripping it, and it's not a dynamic symbol, there's
6409 nothing else to do unless it is a forced local symbol. */
6412 && !h->forced_local)
6416 sym.st_size = h->size;
6417 sym.st_other = h->other;
6418 if (h->forced_local)
6419 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
6420 else if (h->root.type == bfd_link_hash_undefweak
6421 || h->root.type == bfd_link_hash_defweak)
6422 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
6424 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
6426 switch (h->root.type)
6429 case bfd_link_hash_new:
6430 case bfd_link_hash_warning:
6434 case bfd_link_hash_undefined:
6435 case bfd_link_hash_undefweak:
6436 input_sec = bfd_und_section_ptr;
6437 sym.st_shndx = SHN_UNDEF;
6440 case bfd_link_hash_defined:
6441 case bfd_link_hash_defweak:
6443 input_sec = h->root.u.def.section;
6444 if (input_sec->output_section != NULL)
6447 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
6448 input_sec->output_section);
6449 if (sym.st_shndx == SHN_BAD)
6451 (*_bfd_error_handler)
6452 (_("%B: could not find output section %A for input section %A"),
6453 finfo->output_bfd, input_sec->output_section, input_sec);
6454 eoinfo->failed = TRUE;
6458 /* ELF symbols in relocatable files are section relative,
6459 but in nonrelocatable files they are virtual
6461 sym.st_value = h->root.u.def.value + input_sec->output_offset;
6462 if (! finfo->info->relocatable)
6464 sym.st_value += input_sec->output_section->vma;
6465 if (h->type == STT_TLS)
6467 /* STT_TLS symbols are relative to PT_TLS segment
6469 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6470 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6476 BFD_ASSERT (input_sec->owner == NULL
6477 || (input_sec->owner->flags & DYNAMIC) != 0);
6478 sym.st_shndx = SHN_UNDEF;
6479 input_sec = bfd_und_section_ptr;
6484 case bfd_link_hash_common:
6485 input_sec = h->root.u.c.p->section;
6486 sym.st_shndx = bed->common_section_index (input_sec);
6487 sym.st_value = 1 << h->root.u.c.p->alignment_power;
6490 case bfd_link_hash_indirect:
6491 /* These symbols are created by symbol versioning. They point
6492 to the decorated version of the name. For example, if the
6493 symbol foo@@GNU_1.2 is the default, which should be used when
6494 foo is used with no version, then we add an indirect symbol
6495 foo which points to foo@@GNU_1.2. We ignore these symbols,
6496 since the indirected symbol is already in the hash table. */
6500 /* Give the processor backend a chance to tweak the symbol value,
6501 and also to finish up anything that needs to be done for this
6502 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
6503 forced local syms when non-shared is due to a historical quirk. */
6504 if ((h->dynindx != -1
6506 && ((finfo->info->shared
6507 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6508 || h->root.type != bfd_link_hash_undefweak))
6509 || !h->forced_local)
6510 && elf_hash_table (finfo->info)->dynamic_sections_created)
6512 if (! ((*bed->elf_backend_finish_dynamic_symbol)
6513 (finfo->output_bfd, finfo->info, h, &sym)))
6515 eoinfo->failed = TRUE;
6520 /* If we are marking the symbol as undefined, and there are no
6521 non-weak references to this symbol from a regular object, then
6522 mark the symbol as weak undefined; if there are non-weak
6523 references, mark the symbol as strong. We can't do this earlier,
6524 because it might not be marked as undefined until the
6525 finish_dynamic_symbol routine gets through with it. */
6526 if (sym.st_shndx == SHN_UNDEF
6528 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
6529 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
6533 if (h->ref_regular_nonweak)
6534 bindtype = STB_GLOBAL;
6536 bindtype = STB_WEAK;
6537 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info));
6540 /* If a non-weak symbol with non-default visibility is not defined
6541 locally, it is a fatal error. */
6542 if (! finfo->info->relocatable
6543 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
6544 && ELF_ST_BIND (sym.st_info) != STB_WEAK
6545 && h->root.type == bfd_link_hash_undefined
6548 (*_bfd_error_handler)
6549 (_("%B: %s symbol `%s' isn't defined"),
6551 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED
6553 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL
6554 ? "internal" : "hidden",
6555 h->root.root.string);
6556 eoinfo->failed = TRUE;
6560 /* If this symbol should be put in the .dynsym section, then put it
6561 there now. We already know the symbol index. We also fill in
6562 the entry in the .hash section. */
6563 if (h->dynindx != -1
6564 && elf_hash_table (finfo->info)->dynamic_sections_created)
6568 size_t hash_entry_size;
6569 bfd_byte *bucketpos;
6573 sym.st_name = h->dynstr_index;
6574 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
6575 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
6577 bucketcount = elf_hash_table (finfo->info)->bucketcount;
6578 bucket = h->u.elf_hash_value % bucketcount;
6580 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
6581 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
6582 + (bucket + 2) * hash_entry_size);
6583 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
6584 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
6585 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
6586 ((bfd_byte *) finfo->hash_sec->contents
6587 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
6589 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
6591 Elf_Internal_Versym iversym;
6592 Elf_External_Versym *eversym;
6594 if (!h->def_regular)
6596 if (h->verinfo.verdef == NULL)
6597 iversym.vs_vers = 0;
6599 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
6603 if (h->verinfo.vertree == NULL)
6604 iversym.vs_vers = 1;
6606 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
6607 if (finfo->info->create_default_symver)
6612 iversym.vs_vers |= VERSYM_HIDDEN;
6614 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
6615 eversym += h->dynindx;
6616 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
6620 /* If we're stripping it, then it was just a dynamic symbol, and
6621 there's nothing else to do. */
6622 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
6625 h->indx = bfd_get_symcount (finfo->output_bfd);
6627 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h))
6629 eoinfo->failed = TRUE;
6636 /* Return TRUE if special handling is done for relocs in SEC against
6637 symbols defined in discarded sections. */
6640 elf_section_ignore_discarded_relocs (asection *sec)
6642 const struct elf_backend_data *bed;
6644 switch (sec->sec_info_type)
6646 case ELF_INFO_TYPE_STABS:
6647 case ELF_INFO_TYPE_EH_FRAME:
6653 bed = get_elf_backend_data (sec->owner);
6654 if (bed->elf_backend_ignore_discarded_relocs != NULL
6655 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
6661 /* Return a mask saying how ld should treat relocations in SEC against
6662 symbols defined in discarded sections. If this function returns
6663 COMPLAIN set, ld will issue a warning message. If this function
6664 returns PRETEND set, and the discarded section was link-once and the
6665 same size as the kept link-once section, ld will pretend that the
6666 symbol was actually defined in the kept section. Otherwise ld will
6667 zero the reloc (at least that is the intent, but some cooperation by
6668 the target dependent code is needed, particularly for REL targets). */
6671 _bfd_elf_default_action_discarded (asection *sec)
6673 if (sec->flags & SEC_DEBUGGING)
6676 if (strcmp (".eh_frame", sec->name) == 0)
6679 if (strcmp (".gcc_except_table", sec->name) == 0)
6682 return COMPLAIN | PRETEND;
6685 /* Find a match between a section and a member of a section group. */
6688 match_group_member (asection *sec, asection *group)
6690 asection *first = elf_next_in_group (group);
6691 asection *s = first;
6695 if (bfd_elf_match_symbols_in_sections (s, sec))
6705 /* Check if the kept section of a discarded section SEC can be used
6706 to replace it. Return the replacement if it is OK. Otherwise return
6710 _bfd_elf_check_kept_section (asection *sec)
6714 kept = sec->kept_section;
6717 if (elf_sec_group (sec) != NULL)
6718 kept = match_group_member (sec, kept);
6719 if (kept != NULL && sec->size != kept->size)
6725 /* Link an input file into the linker output file. This function
6726 handles all the sections and relocations of the input file at once.
6727 This is so that we only have to read the local symbols once, and
6728 don't have to keep them in memory. */
6731 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
6733 bfd_boolean (*relocate_section)
6734 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
6735 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
6737 Elf_Internal_Shdr *symtab_hdr;
6740 Elf_Internal_Sym *isymbuf;
6741 Elf_Internal_Sym *isym;
6742 Elf_Internal_Sym *isymend;
6744 asection **ppsection;
6746 const struct elf_backend_data *bed;
6747 bfd_boolean emit_relocs;
6748 struct elf_link_hash_entry **sym_hashes;
6750 output_bfd = finfo->output_bfd;
6751 bed = get_elf_backend_data (output_bfd);
6752 relocate_section = bed->elf_backend_relocate_section;
6754 /* If this is a dynamic object, we don't want to do anything here:
6755 we don't want the local symbols, and we don't want the section
6757 if ((input_bfd->flags & DYNAMIC) != 0)
6760 emit_relocs = (finfo->info->relocatable
6761 || finfo->info->emitrelocations);
6763 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6764 if (elf_bad_symtab (input_bfd))
6766 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
6771 locsymcount = symtab_hdr->sh_info;
6772 extsymoff = symtab_hdr->sh_info;
6775 /* Read the local symbols. */
6776 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6777 if (isymbuf == NULL && locsymcount != 0)
6779 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
6780 finfo->internal_syms,
6781 finfo->external_syms,
6782 finfo->locsym_shndx);
6783 if (isymbuf == NULL)
6787 /* Find local symbol sections and adjust values of symbols in
6788 SEC_MERGE sections. Write out those local symbols we know are
6789 going into the output file. */
6790 isymend = isymbuf + locsymcount;
6791 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
6793 isym++, pindex++, ppsection++)
6797 Elf_Internal_Sym osym;
6801 if (elf_bad_symtab (input_bfd))
6803 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
6810 if (isym->st_shndx == SHN_UNDEF)
6811 isec = bfd_und_section_ptr;
6812 else if (isym->st_shndx < SHN_LORESERVE
6813 || isym->st_shndx > SHN_HIRESERVE)
6815 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
6817 && isec->sec_info_type == ELF_INFO_TYPE_MERGE
6818 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
6820 _bfd_merged_section_offset (output_bfd, &isec,
6821 elf_section_data (isec)->sec_info,
6824 else if (isym->st_shndx == SHN_ABS)
6825 isec = bfd_abs_section_ptr;
6826 else if (isym->st_shndx == SHN_COMMON)
6827 isec = bfd_com_section_ptr;
6830 /* Don't attempt to output symbols with st_shnx in the
6831 reserved range other than SHN_ABS and SHN_COMMON. */
6838 /* Don't output the first, undefined, symbol. */
6839 if (ppsection == finfo->sections)
6842 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
6844 /* We never output section symbols. Instead, we use the
6845 section symbol of the corresponding section in the output
6850 /* If we are stripping all symbols, we don't want to output this
6852 if (finfo->info->strip == strip_all)
6855 /* If we are discarding all local symbols, we don't want to
6856 output this one. If we are generating a relocatable output
6857 file, then some of the local symbols may be required by
6858 relocs; we output them below as we discover that they are
6860 if (finfo->info->discard == discard_all)
6863 /* If this symbol is defined in a section which we are
6864 discarding, we don't need to keep it. */
6865 if (isym->st_shndx != SHN_UNDEF
6866 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
6868 || bfd_section_removed_from_list (output_bfd,
6869 isec->output_section)))
6872 /* Get the name of the symbol. */
6873 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
6878 /* See if we are discarding symbols with this name. */
6879 if ((finfo->info->strip == strip_some
6880 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
6882 || (((finfo->info->discard == discard_sec_merge
6883 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
6884 || finfo->info->discard == discard_l)
6885 && bfd_is_local_label_name (input_bfd, name)))
6888 /* If we get here, we are going to output this symbol. */
6892 /* Adjust the section index for the output file. */
6893 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
6894 isec->output_section);
6895 if (osym.st_shndx == SHN_BAD)
6898 *pindex = bfd_get_symcount (output_bfd);
6900 /* ELF symbols in relocatable files are section relative, but
6901 in executable files they are virtual addresses. Note that
6902 this code assumes that all ELF sections have an associated
6903 BFD section with a reasonable value for output_offset; below
6904 we assume that they also have a reasonable value for
6905 output_section. Any special sections must be set up to meet
6906 these requirements. */
6907 osym.st_value += isec->output_offset;
6908 if (! finfo->info->relocatable)
6910 osym.st_value += isec->output_section->vma;
6911 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
6913 /* STT_TLS symbols are relative to PT_TLS segment base. */
6914 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
6915 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
6919 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL))
6923 /* Relocate the contents of each section. */
6924 sym_hashes = elf_sym_hashes (input_bfd);
6925 for (o = input_bfd->sections; o != NULL; o = o->next)
6929 if (! o->linker_mark)
6931 /* This section was omitted from the link. */
6935 if ((o->flags & SEC_HAS_CONTENTS) == 0
6936 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
6939 if ((o->flags & SEC_LINKER_CREATED) != 0)
6941 /* Section was created by _bfd_elf_link_create_dynamic_sections
6946 /* Get the contents of the section. They have been cached by a
6947 relaxation routine. Note that o is a section in an input
6948 file, so the contents field will not have been set by any of
6949 the routines which work on output files. */
6950 if (elf_section_data (o)->this_hdr.contents != NULL)
6951 contents = elf_section_data (o)->this_hdr.contents;
6954 bfd_size_type amt = o->rawsize ? o->rawsize : o->size;
6956 contents = finfo->contents;
6957 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt))
6961 if ((o->flags & SEC_RELOC) != 0)
6963 Elf_Internal_Rela *internal_relocs;
6964 bfd_vma r_type_mask;
6967 /* Get the swapped relocs. */
6969 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
6970 finfo->internal_relocs, FALSE);
6971 if (internal_relocs == NULL
6972 && o->reloc_count > 0)
6975 if (bed->s->arch_size == 32)
6982 r_type_mask = 0xffffffff;
6986 /* Run through the relocs looking for any against symbols
6987 from discarded sections and section symbols from
6988 removed link-once sections. Complain about relocs
6989 against discarded sections. Zero relocs against removed
6990 link-once sections. Preserve debug information as much
6992 if (!elf_section_ignore_discarded_relocs (o))
6994 Elf_Internal_Rela *rel, *relend;
6995 unsigned int action = (*bed->action_discarded) (o);
6997 rel = internal_relocs;
6998 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
6999 for ( ; rel < relend; rel++)
7001 unsigned long r_symndx = rel->r_info >> r_sym_shift;
7002 asection **ps, *sec;
7003 struct elf_link_hash_entry *h = NULL;
7004 const char *sym_name;
7006 if (r_symndx == STN_UNDEF)
7009 if (r_symndx >= locsymcount
7010 || (elf_bad_symtab (input_bfd)
7011 && finfo->sections[r_symndx] == NULL))
7013 h = sym_hashes[r_symndx - extsymoff];
7015 /* Badly formatted input files can contain relocs that
7016 reference non-existant symbols. Check here so that
7017 we do not seg fault. */
7022 sprintf_vma (buffer, rel->r_info);
7023 (*_bfd_error_handler)
7024 (_("error: %B contains a reloc (0x%s) for section %A "
7025 "that references a non-existent global symbol"),
7026 input_bfd, o, buffer);
7027 bfd_set_error (bfd_error_bad_value);
7031 while (h->root.type == bfd_link_hash_indirect
7032 || h->root.type == bfd_link_hash_warning)
7033 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7035 if (h->root.type != bfd_link_hash_defined
7036 && h->root.type != bfd_link_hash_defweak)
7039 ps = &h->root.u.def.section;
7040 sym_name = h->root.root.string;
7044 Elf_Internal_Sym *sym = isymbuf + r_symndx;
7045 ps = &finfo->sections[r_symndx];
7046 sym_name = bfd_elf_sym_name (input_bfd,
7051 /* Complain if the definition comes from a
7052 discarded section. */
7053 if ((sec = *ps) != NULL && elf_discarded_section (sec))
7055 BFD_ASSERT (r_symndx != 0);
7056 if (action & COMPLAIN)
7057 (*finfo->info->callbacks->einfo)
7058 (_("%X`%s' referenced in section `%A' of %B: "
7059 "defined in discarded section `%A' of %B\n"),
7060 sym_name, o, input_bfd, sec, sec->owner);
7062 /* Try to do the best we can to support buggy old
7063 versions of gcc. If we've warned, or this is
7064 debugging info, pretend that the symbol is
7065 really defined in the kept linkonce section.
7066 FIXME: This is quite broken. Modifying the
7067 symbol here means we will be changing all later
7068 uses of the symbol, not just in this section.
7069 The only thing that makes this half reasonable
7070 is that we warn in non-debug sections, and
7071 debug sections tend to come after other
7073 if (action & PRETEND)
7077 kept = _bfd_elf_check_kept_section (sec);
7085 /* Remove the symbol reference from the reloc, but
7086 don't kill the reloc completely. This is so that
7087 a zero value will be written into the section,
7088 which may have non-zero contents put there by the
7089 assembler. Zero in things like an eh_frame fde
7090 pc_begin allows stack unwinders to recognize the
7092 rel->r_info &= r_type_mask;
7098 /* Relocate the section by invoking a back end routine.
7100 The back end routine is responsible for adjusting the
7101 section contents as necessary, and (if using Rela relocs
7102 and generating a relocatable output file) adjusting the
7103 reloc addend as necessary.
7105 The back end routine does not have to worry about setting
7106 the reloc address or the reloc symbol index.
7108 The back end routine is given a pointer to the swapped in
7109 internal symbols, and can access the hash table entries
7110 for the external symbols via elf_sym_hashes (input_bfd).
7112 When generating relocatable output, the back end routine
7113 must handle STB_LOCAL/STT_SECTION symbols specially. The
7114 output symbol is going to be a section symbol
7115 corresponding to the output section, which will require
7116 the addend to be adjusted. */
7118 if (! (*relocate_section) (output_bfd, finfo->info,
7119 input_bfd, o, contents,
7127 Elf_Internal_Rela *irela;
7128 Elf_Internal_Rela *irelaend;
7129 bfd_vma last_offset;
7130 struct elf_link_hash_entry **rel_hash;
7131 struct elf_link_hash_entry **rel_hash_list;
7132 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2;
7133 unsigned int next_erel;
7134 bfd_boolean rela_normal;
7136 input_rel_hdr = &elf_section_data (o)->rel_hdr;
7137 rela_normal = (bed->rela_normal
7138 && (input_rel_hdr->sh_entsize
7139 == bed->s->sizeof_rela));
7141 /* Adjust the reloc addresses and symbol indices. */
7143 irela = internal_relocs;
7144 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
7145 rel_hash = (elf_section_data (o->output_section)->rel_hashes
7146 + elf_section_data (o->output_section)->rel_count
7147 + elf_section_data (o->output_section)->rel_count2);
7148 rel_hash_list = rel_hash;
7149 last_offset = o->output_offset;
7150 if (!finfo->info->relocatable)
7151 last_offset += o->output_section->vma;
7152 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
7154 unsigned long r_symndx;
7156 Elf_Internal_Sym sym;
7158 if (next_erel == bed->s->int_rels_per_ext_rel)
7164 irela->r_offset = _bfd_elf_section_offset (output_bfd,
7167 if (irela->r_offset >= (bfd_vma) -2)
7169 /* This is a reloc for a deleted entry or somesuch.
7170 Turn it into an R_*_NONE reloc, at the same
7171 offset as the last reloc. elf_eh_frame.c and
7172 elf_bfd_discard_info rely on reloc offsets
7174 irela->r_offset = last_offset;
7176 irela->r_addend = 0;
7180 irela->r_offset += o->output_offset;
7182 /* Relocs in an executable have to be virtual addresses. */
7183 if (!finfo->info->relocatable)
7184 irela->r_offset += o->output_section->vma;
7186 last_offset = irela->r_offset;
7188 r_symndx = irela->r_info >> r_sym_shift;
7189 if (r_symndx == STN_UNDEF)
7192 if (r_symndx >= locsymcount
7193 || (elf_bad_symtab (input_bfd)
7194 && finfo->sections[r_symndx] == NULL))
7196 struct elf_link_hash_entry *rh;
7199 /* This is a reloc against a global symbol. We
7200 have not yet output all the local symbols, so
7201 we do not know the symbol index of any global
7202 symbol. We set the rel_hash entry for this
7203 reloc to point to the global hash table entry
7204 for this symbol. The symbol index is then
7205 set at the end of bfd_elf_final_link. */
7206 indx = r_symndx - extsymoff;
7207 rh = elf_sym_hashes (input_bfd)[indx];
7208 while (rh->root.type == bfd_link_hash_indirect
7209 || rh->root.type == bfd_link_hash_warning)
7210 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
7212 /* Setting the index to -2 tells
7213 elf_link_output_extsym that this symbol is
7215 BFD_ASSERT (rh->indx < 0);
7223 /* This is a reloc against a local symbol. */
7226 sym = isymbuf[r_symndx];
7227 sec = finfo->sections[r_symndx];
7228 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
7230 /* I suppose the backend ought to fill in the
7231 section of any STT_SECTION symbol against a
7232 processor specific section. */
7234 if (bfd_is_abs_section (sec))
7236 else if (sec == NULL || sec->owner == NULL)
7238 bfd_set_error (bfd_error_bad_value);
7243 asection *osec = sec->output_section;
7245 /* If we have discarded a section, the output
7246 section will be the absolute section. In
7247 case of discarded link-once and discarded
7248 SEC_MERGE sections, use the kept section. */
7249 if (bfd_is_abs_section (osec)
7250 && sec->kept_section != NULL
7251 && sec->kept_section->output_section != NULL)
7253 osec = sec->kept_section->output_section;
7254 irela->r_addend -= osec->vma;
7257 if (!bfd_is_abs_section (osec))
7259 r_symndx = osec->target_index;
7260 BFD_ASSERT (r_symndx != 0);
7264 /* Adjust the addend according to where the
7265 section winds up in the output section. */
7267 irela->r_addend += sec->output_offset;
7271 if (finfo->indices[r_symndx] == -1)
7273 unsigned long shlink;
7277 if (finfo->info->strip == strip_all)
7279 /* You can't do ld -r -s. */
7280 bfd_set_error (bfd_error_invalid_operation);
7284 /* This symbol was skipped earlier, but
7285 since it is needed by a reloc, we
7286 must output it now. */
7287 shlink = symtab_hdr->sh_link;
7288 name = (bfd_elf_string_from_elf_section
7289 (input_bfd, shlink, sym.st_name));
7293 osec = sec->output_section;
7295 _bfd_elf_section_from_bfd_section (output_bfd,
7297 if (sym.st_shndx == SHN_BAD)
7300 sym.st_value += sec->output_offset;
7301 if (! finfo->info->relocatable)
7303 sym.st_value += osec->vma;
7304 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
7306 /* STT_TLS symbols are relative to PT_TLS
7308 BFD_ASSERT (elf_hash_table (finfo->info)
7310 sym.st_value -= (elf_hash_table (finfo->info)
7315 finfo->indices[r_symndx]
7316 = bfd_get_symcount (output_bfd);
7318 if (! elf_link_output_sym (finfo, name, &sym, sec,
7323 r_symndx = finfo->indices[r_symndx];
7326 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
7327 | (irela->r_info & r_type_mask));
7330 /* Swap out the relocs. */
7331 if (input_rel_hdr->sh_size != 0
7332 && !bed->elf_backend_emit_relocs (output_bfd, o,
7338 input_rel_hdr2 = elf_section_data (o)->rel_hdr2;
7339 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0)
7341 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
7342 * bed->s->int_rels_per_ext_rel);
7343 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
7344 if (!bed->elf_backend_emit_relocs (output_bfd, o,
7353 /* Write out the modified section contents. */
7354 if (bed->elf_backend_write_section
7355 && (*bed->elf_backend_write_section) (output_bfd, o, contents))
7357 /* Section written out. */
7359 else switch (o->sec_info_type)
7361 case ELF_INFO_TYPE_STABS:
7362 if (! (_bfd_write_section_stabs
7364 &elf_hash_table (finfo->info)->stab_info,
7365 o, &elf_section_data (o)->sec_info, contents)))
7368 case ELF_INFO_TYPE_MERGE:
7369 if (! _bfd_write_merged_section (output_bfd, o,
7370 elf_section_data (o)->sec_info))
7373 case ELF_INFO_TYPE_EH_FRAME:
7375 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
7382 if (! (o->flags & SEC_EXCLUDE)
7383 && ! bfd_set_section_contents (output_bfd, o->output_section,
7385 (file_ptr) o->output_offset,
7396 /* Generate a reloc when linking an ELF file. This is a reloc
7397 requested by the linker, and does not come from any input file. This
7398 is used to build constructor and destructor tables when linking
7402 elf_reloc_link_order (bfd *output_bfd,
7403 struct bfd_link_info *info,
7404 asection *output_section,
7405 struct bfd_link_order *link_order)
7407 reloc_howto_type *howto;
7411 struct elf_link_hash_entry **rel_hash_ptr;
7412 Elf_Internal_Shdr *rel_hdr;
7413 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
7414 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
7418 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
7421 bfd_set_error (bfd_error_bad_value);
7425 addend = link_order->u.reloc.p->addend;
7427 /* Figure out the symbol index. */
7428 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes
7429 + elf_section_data (output_section)->rel_count
7430 + elf_section_data (output_section)->rel_count2);
7431 if (link_order->type == bfd_section_reloc_link_order)
7433 indx = link_order->u.reloc.p->u.section->target_index;
7434 BFD_ASSERT (indx != 0);
7435 *rel_hash_ptr = NULL;
7439 struct elf_link_hash_entry *h;
7441 /* Treat a reloc against a defined symbol as though it were
7442 actually against the section. */
7443 h = ((struct elf_link_hash_entry *)
7444 bfd_wrapped_link_hash_lookup (output_bfd, info,
7445 link_order->u.reloc.p->u.name,
7446 FALSE, FALSE, TRUE));
7448 && (h->root.type == bfd_link_hash_defined
7449 || h->root.type == bfd_link_hash_defweak))
7453 section = h->root.u.def.section;
7454 indx = section->output_section->target_index;
7455 *rel_hash_ptr = NULL;
7456 /* It seems that we ought to add the symbol value to the
7457 addend here, but in practice it has already been added
7458 because it was passed to constructor_callback. */
7459 addend += section->output_section->vma + section->output_offset;
7463 /* Setting the index to -2 tells elf_link_output_extsym that
7464 this symbol is used by a reloc. */
7471 if (! ((*info->callbacks->unattached_reloc)
7472 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
7478 /* If this is an inplace reloc, we must write the addend into the
7480 if (howto->partial_inplace && addend != 0)
7483 bfd_reloc_status_type rstat;
7486 const char *sym_name;
7488 size = bfd_get_reloc_size (howto);
7489 buf = bfd_zmalloc (size);
7492 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
7499 case bfd_reloc_outofrange:
7502 case bfd_reloc_overflow:
7503 if (link_order->type == bfd_section_reloc_link_order)
7504 sym_name = bfd_section_name (output_bfd,
7505 link_order->u.reloc.p->u.section);
7507 sym_name = link_order->u.reloc.p->u.name;
7508 if (! ((*info->callbacks->reloc_overflow)
7509 (info, NULL, sym_name, howto->name, addend, NULL,
7510 NULL, (bfd_vma) 0)))
7517 ok = bfd_set_section_contents (output_bfd, output_section, buf,
7518 link_order->offset, size);
7524 /* The address of a reloc is relative to the section in a
7525 relocatable file, and is a virtual address in an executable
7527 offset = link_order->offset;
7528 if (! info->relocatable)
7529 offset += output_section->vma;
7531 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
7533 irel[i].r_offset = offset;
7535 irel[i].r_addend = 0;
7537 if (bed->s->arch_size == 32)
7538 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
7540 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
7542 rel_hdr = &elf_section_data (output_section)->rel_hdr;
7543 erel = rel_hdr->contents;
7544 if (rel_hdr->sh_type == SHT_REL)
7546 erel += (elf_section_data (output_section)->rel_count
7547 * bed->s->sizeof_rel);
7548 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
7552 irel[0].r_addend = addend;
7553 erel += (elf_section_data (output_section)->rel_count
7554 * bed->s->sizeof_rela);
7555 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
7558 ++elf_section_data (output_section)->rel_count;
7564 /* Get the output vma of the section pointed to by the sh_link field. */
7567 elf_get_linked_section_vma (struct bfd_link_order *p)
7569 Elf_Internal_Shdr **elf_shdrp;
7573 s = p->u.indirect.section;
7574 elf_shdrp = elf_elfsections (s->owner);
7575 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
7576 elfsec = elf_shdrp[elfsec]->sh_link;
7578 The Intel C compiler generates SHT_IA_64_UNWIND with
7579 SHF_LINK_ORDER. But it doesn't set the sh_link or
7580 sh_info fields. Hence we could get the situation
7581 where elfsec is 0. */
7584 const struct elf_backend_data *bed
7585 = get_elf_backend_data (s->owner);
7586 if (bed->link_order_error_handler)
7587 bed->link_order_error_handler
7588 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
7593 s = elf_shdrp[elfsec]->bfd_section;
7594 return s->output_section->vma + s->output_offset;
7599 /* Compare two sections based on the locations of the sections they are
7600 linked to. Used by elf_fixup_link_order. */
7603 compare_link_order (const void * a, const void * b)
7608 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
7609 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
7616 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
7617 order as their linked sections. Returns false if this could not be done
7618 because an output section includes both ordered and unordered
7619 sections. Ideally we'd do this in the linker proper. */
7622 elf_fixup_link_order (bfd *abfd, asection *o)
7627 struct bfd_link_order *p;
7629 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7631 struct bfd_link_order **sections;
7632 asection *s, *other_sec, *linkorder_sec;
7636 linkorder_sec = NULL;
7639 for (p = o->map_head.link_order; p != NULL; p = p->next)
7641 if (p->type == bfd_indirect_link_order)
7643 s = p->u.indirect.section;
7645 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
7646 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
7647 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) != -1
7648 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER)
7662 if (seen_other && seen_linkorder)
7664 if (other_sec && linkorder_sec)
7665 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
7667 linkorder_sec->owner, other_sec,
7670 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
7672 bfd_set_error (bfd_error_bad_value);
7677 if (!seen_linkorder)
7680 sections = (struct bfd_link_order **)
7681 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *));
7684 for (p = o->map_head.link_order; p != NULL; p = p->next)
7686 sections[seen_linkorder++] = p;
7688 /* Sort the input sections in the order of their linked section. */
7689 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
7690 compare_link_order);
7692 /* Change the offsets of the sections. */
7694 for (n = 0; n < seen_linkorder; n++)
7696 s = sections[n]->u.indirect.section;
7697 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1);
7698 s->output_offset = offset;
7699 sections[n]->offset = offset;
7700 offset += sections[n]->size;
7707 /* Do the final step of an ELF link. */
7710 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
7712 bfd_boolean dynamic;
7713 bfd_boolean emit_relocs;
7715 struct elf_final_link_info finfo;
7716 register asection *o;
7717 register struct bfd_link_order *p;
7719 bfd_size_type max_contents_size;
7720 bfd_size_type max_external_reloc_size;
7721 bfd_size_type max_internal_reloc_count;
7722 bfd_size_type max_sym_count;
7723 bfd_size_type max_sym_shndx_count;
7725 Elf_Internal_Sym elfsym;
7727 Elf_Internal_Shdr *symtab_hdr;
7728 Elf_Internal_Shdr *symtab_shndx_hdr;
7729 Elf_Internal_Shdr *symstrtab_hdr;
7730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7731 struct elf_outext_info eoinfo;
7733 size_t relativecount = 0;
7734 asection *reldyn = 0;
7737 if (! is_elf_hash_table (info->hash))
7741 abfd->flags |= DYNAMIC;
7743 dynamic = elf_hash_table (info)->dynamic_sections_created;
7744 dynobj = elf_hash_table (info)->dynobj;
7746 emit_relocs = (info->relocatable
7747 || info->emitrelocations
7748 || bed->elf_backend_emit_relocs);
7751 finfo.output_bfd = abfd;
7752 finfo.symstrtab = _bfd_elf_stringtab_init ();
7753 if (finfo.symstrtab == NULL)
7758 finfo.dynsym_sec = NULL;
7759 finfo.hash_sec = NULL;
7760 finfo.symver_sec = NULL;
7764 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
7765 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
7766 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL);
7767 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
7768 /* Note that it is OK if symver_sec is NULL. */
7771 finfo.contents = NULL;
7772 finfo.external_relocs = NULL;
7773 finfo.internal_relocs = NULL;
7774 finfo.external_syms = NULL;
7775 finfo.locsym_shndx = NULL;
7776 finfo.internal_syms = NULL;
7777 finfo.indices = NULL;
7778 finfo.sections = NULL;
7779 finfo.symbuf = NULL;
7780 finfo.symshndxbuf = NULL;
7781 finfo.symbuf_count = 0;
7782 finfo.shndxbuf_size = 0;
7784 /* Count up the number of relocations we will output for each output
7785 section, so that we know the sizes of the reloc sections. We
7786 also figure out some maximum sizes. */
7787 max_contents_size = 0;
7788 max_external_reloc_size = 0;
7789 max_internal_reloc_count = 0;
7791 max_sym_shndx_count = 0;
7793 for (o = abfd->sections; o != NULL; o = o->next)
7795 struct bfd_elf_section_data *esdo = elf_section_data (o);
7798 for (p = o->map_head.link_order; p != NULL; p = p->next)
7800 unsigned int reloc_count = 0;
7801 struct bfd_elf_section_data *esdi = NULL;
7802 unsigned int *rel_count1;
7804 if (p->type == bfd_section_reloc_link_order
7805 || p->type == bfd_symbol_reloc_link_order)
7807 else if (p->type == bfd_indirect_link_order)
7811 sec = p->u.indirect.section;
7812 esdi = elf_section_data (sec);
7814 /* Mark all sections which are to be included in the
7815 link. This will normally be every section. We need
7816 to do this so that we can identify any sections which
7817 the linker has decided to not include. */
7818 sec->linker_mark = TRUE;
7820 if (sec->flags & SEC_MERGE)
7823 if (info->relocatable || info->emitrelocations)
7824 reloc_count = sec->reloc_count;
7825 else if (bed->elf_backend_count_relocs)
7827 Elf_Internal_Rela * relocs;
7829 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
7832 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs);
7834 if (elf_section_data (o)->relocs != relocs)
7838 if (sec->rawsize > max_contents_size)
7839 max_contents_size = sec->rawsize;
7840 if (sec->size > max_contents_size)
7841 max_contents_size = sec->size;
7843 /* We are interested in just local symbols, not all
7845 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
7846 && (sec->owner->flags & DYNAMIC) == 0)
7850 if (elf_bad_symtab (sec->owner))
7851 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
7852 / bed->s->sizeof_sym);
7854 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
7856 if (sym_count > max_sym_count)
7857 max_sym_count = sym_count;
7859 if (sym_count > max_sym_shndx_count
7860 && elf_symtab_shndx (sec->owner) != 0)
7861 max_sym_shndx_count = sym_count;
7863 if ((sec->flags & SEC_RELOC) != 0)
7867 ext_size = elf_section_data (sec)->rel_hdr.sh_size;
7868 if (ext_size > max_external_reloc_size)
7869 max_external_reloc_size = ext_size;
7870 if (sec->reloc_count > max_internal_reloc_count)
7871 max_internal_reloc_count = sec->reloc_count;
7876 if (reloc_count == 0)
7879 o->reloc_count += reloc_count;
7881 /* MIPS may have a mix of REL and RELA relocs on sections.
7882 To support this curious ABI we keep reloc counts in
7883 elf_section_data too. We must be careful to add the
7884 relocations from the input section to the right output
7885 count. FIXME: Get rid of one count. We have
7886 o->reloc_count == esdo->rel_count + esdo->rel_count2. */
7887 rel_count1 = &esdo->rel_count;
7890 bfd_boolean same_size;
7891 bfd_size_type entsize1;
7893 entsize1 = esdi->rel_hdr.sh_entsize;
7894 BFD_ASSERT (entsize1 == bed->s->sizeof_rel
7895 || entsize1 == bed->s->sizeof_rela);
7896 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel);
7899 rel_count1 = &esdo->rel_count2;
7901 if (esdi->rel_hdr2 != NULL)
7903 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize;
7904 unsigned int alt_count;
7905 unsigned int *rel_count2;
7907 BFD_ASSERT (entsize2 != entsize1
7908 && (entsize2 == bed->s->sizeof_rel
7909 || entsize2 == bed->s->sizeof_rela));
7911 rel_count2 = &esdo->rel_count2;
7913 rel_count2 = &esdo->rel_count;
7915 /* The following is probably too simplistic if the
7916 backend counts output relocs unusually. */
7917 BFD_ASSERT (bed->elf_backend_count_relocs == NULL);
7918 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2);
7919 *rel_count2 += alt_count;
7920 reloc_count -= alt_count;
7923 *rel_count1 += reloc_count;
7926 if (o->reloc_count > 0)
7927 o->flags |= SEC_RELOC;
7930 /* Explicitly clear the SEC_RELOC flag. The linker tends to
7931 set it (this is probably a bug) and if it is set
7932 assign_section_numbers will create a reloc section. */
7933 o->flags &=~ SEC_RELOC;
7936 /* If the SEC_ALLOC flag is not set, force the section VMA to
7937 zero. This is done in elf_fake_sections as well, but forcing
7938 the VMA to 0 here will ensure that relocs against these
7939 sections are handled correctly. */
7940 if ((o->flags & SEC_ALLOC) == 0
7941 && ! o->user_set_vma)
7945 if (! info->relocatable && merged)
7946 elf_link_hash_traverse (elf_hash_table (info),
7947 _bfd_elf_link_sec_merge_syms, abfd);
7949 /* Figure out the file positions for everything but the symbol table
7950 and the relocs. We set symcount to force assign_section_numbers
7951 to create a symbol table. */
7952 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
7953 BFD_ASSERT (! abfd->output_has_begun);
7954 if (! _bfd_elf_compute_section_file_positions (abfd, info))
7957 /* Set sizes, and assign file positions for reloc sections. */
7958 for (o = abfd->sections; o != NULL; o = o->next)
7960 if ((o->flags & SEC_RELOC) != 0)
7962 if (!(_bfd_elf_link_size_reloc_section
7963 (abfd, &elf_section_data (o)->rel_hdr, o)))
7966 if (elf_section_data (o)->rel_hdr2
7967 && !(_bfd_elf_link_size_reloc_section
7968 (abfd, elf_section_data (o)->rel_hdr2, o)))
7972 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
7973 to count upwards while actually outputting the relocations. */
7974 elf_section_data (o)->rel_count = 0;
7975 elf_section_data (o)->rel_count2 = 0;
7978 _bfd_elf_assign_file_positions_for_relocs (abfd);
7980 /* We have now assigned file positions for all the sections except
7981 .symtab and .strtab. We start the .symtab section at the current
7982 file position, and write directly to it. We build the .strtab
7983 section in memory. */
7984 bfd_get_symcount (abfd) = 0;
7985 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7986 /* sh_name is set in prep_headers. */
7987 symtab_hdr->sh_type = SHT_SYMTAB;
7988 /* sh_flags, sh_addr and sh_size all start off zero. */
7989 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7990 /* sh_link is set in assign_section_numbers. */
7991 /* sh_info is set below. */
7992 /* sh_offset is set just below. */
7993 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
7995 off = elf_tdata (abfd)->next_file_pos;
7996 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
7998 /* Note that at this point elf_tdata (abfd)->next_file_pos is
7999 incorrect. We do not yet know the size of the .symtab section.
8000 We correct next_file_pos below, after we do know the size. */
8002 /* Allocate a buffer to hold swapped out symbols. This is to avoid
8003 continuously seeking to the right position in the file. */
8004 if (! info->keep_memory || max_sym_count < 20)
8005 finfo.symbuf_size = 20;
8007 finfo.symbuf_size = max_sym_count;
8008 amt = finfo.symbuf_size;
8009 amt *= bed->s->sizeof_sym;
8010 finfo.symbuf = bfd_malloc (amt);
8011 if (finfo.symbuf == NULL)
8013 if (elf_numsections (abfd) > SHN_LORESERVE)
8015 /* Wild guess at number of output symbols. realloc'd as needed. */
8016 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
8017 finfo.shndxbuf_size = amt;
8018 amt *= sizeof (Elf_External_Sym_Shndx);
8019 finfo.symshndxbuf = bfd_zmalloc (amt);
8020 if (finfo.symshndxbuf == NULL)
8024 /* Start writing out the symbol table. The first symbol is always a
8026 if (info->strip != strip_all
8029 elfsym.st_value = 0;
8032 elfsym.st_other = 0;
8033 elfsym.st_shndx = SHN_UNDEF;
8034 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
8039 /* Output a symbol for each section. We output these even if we are
8040 discarding local symbols, since they are used for relocs. These
8041 symbols have no names. We store the index of each one in the
8042 index field of the section, so that we can find it again when
8043 outputting relocs. */
8044 if (info->strip != strip_all
8048 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8049 elfsym.st_other = 0;
8050 for (i = 1; i < elf_numsections (abfd); i++)
8052 o = bfd_section_from_elf_index (abfd, i);
8054 o->target_index = bfd_get_symcount (abfd);
8055 elfsym.st_shndx = i;
8056 if (info->relocatable || o == NULL)
8057 elfsym.st_value = 0;
8059 elfsym.st_value = o->vma;
8060 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL))
8062 if (i == SHN_LORESERVE - 1)
8063 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
8067 /* Allocate some memory to hold information read in from the input
8069 if (max_contents_size != 0)
8071 finfo.contents = bfd_malloc (max_contents_size);
8072 if (finfo.contents == NULL)
8076 if (max_external_reloc_size != 0)
8078 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
8079 if (finfo.external_relocs == NULL)
8083 if (max_internal_reloc_count != 0)
8085 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
8086 amt *= sizeof (Elf_Internal_Rela);
8087 finfo.internal_relocs = bfd_malloc (amt);
8088 if (finfo.internal_relocs == NULL)
8092 if (max_sym_count != 0)
8094 amt = max_sym_count * bed->s->sizeof_sym;
8095 finfo.external_syms = bfd_malloc (amt);
8096 if (finfo.external_syms == NULL)
8099 amt = max_sym_count * sizeof (Elf_Internal_Sym);
8100 finfo.internal_syms = bfd_malloc (amt);
8101 if (finfo.internal_syms == NULL)
8104 amt = max_sym_count * sizeof (long);
8105 finfo.indices = bfd_malloc (amt);
8106 if (finfo.indices == NULL)
8109 amt = max_sym_count * sizeof (asection *);
8110 finfo.sections = bfd_malloc (amt);
8111 if (finfo.sections == NULL)
8115 if (max_sym_shndx_count != 0)
8117 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
8118 finfo.locsym_shndx = bfd_malloc (amt);
8119 if (finfo.locsym_shndx == NULL)
8123 if (elf_hash_table (info)->tls_sec)
8125 bfd_vma base, end = 0;
8128 for (sec = elf_hash_table (info)->tls_sec;
8129 sec && (sec->flags & SEC_THREAD_LOCAL);
8132 bfd_size_type size = sec->size;
8135 && (sec->flags & SEC_HAS_CONTENTS) == 0)
8137 struct bfd_link_order *o = sec->map_tail.link_order;
8139 size = o->offset + o->size;
8141 end = sec->vma + size;
8143 base = elf_hash_table (info)->tls_sec->vma;
8144 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power);
8145 elf_hash_table (info)->tls_size = end - base;
8148 /* Reorder SHF_LINK_ORDER sections. */
8149 for (o = abfd->sections; o != NULL; o = o->next)
8151 if (!elf_fixup_link_order (abfd, o))
8155 /* Since ELF permits relocations to be against local symbols, we
8156 must have the local symbols available when we do the relocations.
8157 Since we would rather only read the local symbols once, and we
8158 would rather not keep them in memory, we handle all the
8159 relocations for a single input file at the same time.
8161 Unfortunately, there is no way to know the total number of local
8162 symbols until we have seen all of them, and the local symbol
8163 indices precede the global symbol indices. This means that when
8164 we are generating relocatable output, and we see a reloc against
8165 a global symbol, we can not know the symbol index until we have
8166 finished examining all the local symbols to see which ones we are
8167 going to output. To deal with this, we keep the relocations in
8168 memory, and don't output them until the end of the link. This is
8169 an unfortunate waste of memory, but I don't see a good way around
8170 it. Fortunately, it only happens when performing a relocatable
8171 link, which is not the common case. FIXME: If keep_memory is set
8172 we could write the relocs out and then read them again; I don't
8173 know how bad the memory loss will be. */
8175 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8176 sub->output_has_begun = FALSE;
8177 for (o = abfd->sections; o != NULL; o = o->next)
8179 for (p = o->map_head.link_order; p != NULL; p = p->next)
8181 if (p->type == bfd_indirect_link_order
8182 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
8183 == bfd_target_elf_flavour)
8184 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
8186 if (! sub->output_has_begun)
8188 if (! elf_link_input_bfd (&finfo, sub))
8190 sub->output_has_begun = TRUE;
8193 else if (p->type == bfd_section_reloc_link_order
8194 || p->type == bfd_symbol_reloc_link_order)
8196 if (! elf_reloc_link_order (abfd, info, o, p))
8201 if (! _bfd_default_link_order (abfd, info, o, p))
8207 /* Output any global symbols that got converted to local in a
8208 version script or due to symbol visibility. We do this in a
8209 separate step since ELF requires all local symbols to appear
8210 prior to any global symbols. FIXME: We should only do this if
8211 some global symbols were, in fact, converted to become local.
8212 FIXME: Will this work correctly with the Irix 5 linker? */
8213 eoinfo.failed = FALSE;
8214 eoinfo.finfo = &finfo;
8215 eoinfo.localsyms = TRUE;
8216 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8221 /* That wrote out all the local symbols. Finish up the symbol table
8222 with the global symbols. Even if we want to strip everything we
8223 can, we still need to deal with those global symbols that got
8224 converted to local in a version script. */
8226 /* The sh_info field records the index of the first non local symbol. */
8227 symtab_hdr->sh_info = bfd_get_symcount (abfd);
8230 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
8232 Elf_Internal_Sym sym;
8233 bfd_byte *dynsym = finfo.dynsym_sec->contents;
8234 long last_local = 0;
8236 /* Write out the section symbols for the output sections. */
8237 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
8243 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8246 for (s = abfd->sections; s != NULL; s = s->next)
8252 dynindx = elf_section_data (s)->dynindx;
8255 indx = elf_section_data (s)->this_idx;
8256 BFD_ASSERT (indx > 0);
8257 sym.st_shndx = indx;
8258 sym.st_value = s->vma;
8259 dest = dynsym + dynindx * bed->s->sizeof_sym;
8260 if (last_local < dynindx)
8261 last_local = dynindx;
8262 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8266 /* Write out the local dynsyms. */
8267 if (elf_hash_table (info)->dynlocal)
8269 struct elf_link_local_dynamic_entry *e;
8270 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
8275 sym.st_size = e->isym.st_size;
8276 sym.st_other = e->isym.st_other;
8278 /* Copy the internal symbol as is.
8279 Note that we saved a word of storage and overwrote
8280 the original st_name with the dynstr_index. */
8283 if (e->isym.st_shndx != SHN_UNDEF
8284 && (e->isym.st_shndx < SHN_LORESERVE
8285 || e->isym.st_shndx > SHN_HIRESERVE))
8287 s = bfd_section_from_elf_index (e->input_bfd,
8291 elf_section_data (s->output_section)->this_idx;
8292 sym.st_value = (s->output_section->vma
8294 + e->isym.st_value);
8297 if (last_local < e->dynindx)
8298 last_local = e->dynindx;
8300 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
8301 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
8305 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
8309 /* We get the global symbols from the hash table. */
8310 eoinfo.failed = FALSE;
8311 eoinfo.localsyms = FALSE;
8312 eoinfo.finfo = &finfo;
8313 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym,
8318 /* If backend needs to output some symbols not present in the hash
8319 table, do it now. */
8320 if (bed->elf_backend_output_arch_syms)
8322 typedef bfd_boolean (*out_sym_func)
8323 (void *, const char *, Elf_Internal_Sym *, asection *,
8324 struct elf_link_hash_entry *);
8326 if (! ((*bed->elf_backend_output_arch_syms)
8327 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
8331 /* Flush all symbols to the file. */
8332 if (! elf_link_flush_output_syms (&finfo, bed))
8335 /* Now we know the size of the symtab section. */
8336 off += symtab_hdr->sh_size;
8338 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
8339 if (symtab_shndx_hdr->sh_name != 0)
8341 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8342 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8343 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8344 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
8345 symtab_shndx_hdr->sh_size = amt;
8347 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
8350 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
8351 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
8356 /* Finish up and write out the symbol string table (.strtab)
8358 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8359 /* sh_name was set in prep_headers. */
8360 symstrtab_hdr->sh_type = SHT_STRTAB;
8361 symstrtab_hdr->sh_flags = 0;
8362 symstrtab_hdr->sh_addr = 0;
8363 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
8364 symstrtab_hdr->sh_entsize = 0;
8365 symstrtab_hdr->sh_link = 0;
8366 symstrtab_hdr->sh_info = 0;
8367 /* sh_offset is set just below. */
8368 symstrtab_hdr->sh_addralign = 1;
8370 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
8371 elf_tdata (abfd)->next_file_pos = off;
8373 if (bfd_get_symcount (abfd) > 0)
8375 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
8376 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
8380 /* Adjust the relocs to have the correct symbol indices. */
8381 for (o = abfd->sections; o != NULL; o = o->next)
8383 if ((o->flags & SEC_RELOC) == 0)
8386 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr,
8387 elf_section_data (o)->rel_count,
8388 elf_section_data (o)->rel_hashes);
8389 if (elf_section_data (o)->rel_hdr2 != NULL)
8390 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2,
8391 elf_section_data (o)->rel_count2,
8392 (elf_section_data (o)->rel_hashes
8393 + elf_section_data (o)->rel_count));
8395 /* Set the reloc_count field to 0 to prevent write_relocs from
8396 trying to swap the relocs out itself. */
8400 if (dynamic && info->combreloc && dynobj != NULL)
8401 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
8403 /* If we are linking against a dynamic object, or generating a
8404 shared library, finish up the dynamic linking information. */
8407 bfd_byte *dyncon, *dynconend;
8409 /* Fix up .dynamic entries. */
8410 o = bfd_get_section_by_name (dynobj, ".dynamic");
8411 BFD_ASSERT (o != NULL);
8413 dyncon = o->contents;
8414 dynconend = o->contents + o->size;
8415 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
8417 Elf_Internal_Dyn dyn;
8421 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
8428 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
8430 switch (elf_section_data (reldyn)->this_hdr.sh_type)
8432 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
8433 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
8436 dyn.d_un.d_val = relativecount;
8443 name = info->init_function;
8446 name = info->fini_function;
8449 struct elf_link_hash_entry *h;
8451 h = elf_link_hash_lookup (elf_hash_table (info), name,
8452 FALSE, FALSE, TRUE);
8454 && (h->root.type == bfd_link_hash_defined
8455 || h->root.type == bfd_link_hash_defweak))
8457 dyn.d_un.d_val = h->root.u.def.value;
8458 o = h->root.u.def.section;
8459 if (o->output_section != NULL)
8460 dyn.d_un.d_val += (o->output_section->vma
8461 + o->output_offset);
8464 /* The symbol is imported from another shared
8465 library and does not apply to this one. */
8473 case DT_PREINIT_ARRAYSZ:
8474 name = ".preinit_array";
8476 case DT_INIT_ARRAYSZ:
8477 name = ".init_array";
8479 case DT_FINI_ARRAYSZ:
8480 name = ".fini_array";
8482 o = bfd_get_section_by_name (abfd, name);
8485 (*_bfd_error_handler)
8486 (_("%B: could not find output section %s"), abfd, name);
8490 (*_bfd_error_handler)
8491 (_("warning: %s section has zero size"), name);
8492 dyn.d_un.d_val = o->size;
8495 case DT_PREINIT_ARRAY:
8496 name = ".preinit_array";
8499 name = ".init_array";
8502 name = ".fini_array";
8515 name = ".gnu.version_d";
8518 name = ".gnu.version_r";
8521 name = ".gnu.version";
8523 o = bfd_get_section_by_name (abfd, name);
8526 (*_bfd_error_handler)
8527 (_("%B: could not find output section %s"), abfd, name);
8530 dyn.d_un.d_ptr = o->vma;
8537 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
8542 for (i = 1; i < elf_numsections (abfd); i++)
8544 Elf_Internal_Shdr *hdr;
8546 hdr = elf_elfsections (abfd)[i];
8547 if (hdr->sh_type == type
8548 && (hdr->sh_flags & SHF_ALLOC) != 0)
8550 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
8551 dyn.d_un.d_val += hdr->sh_size;
8554 if (dyn.d_un.d_val == 0
8555 || hdr->sh_addr < dyn.d_un.d_val)
8556 dyn.d_un.d_val = hdr->sh_addr;
8562 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
8566 /* If we have created any dynamic sections, then output them. */
8569 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
8572 for (o = dynobj->sections; o != NULL; o = o->next)
8574 if ((o->flags & SEC_HAS_CONTENTS) == 0
8576 || o->output_section == bfd_abs_section_ptr)
8578 if ((o->flags & SEC_LINKER_CREATED) == 0)
8580 /* At this point, we are only interested in sections
8581 created by _bfd_elf_link_create_dynamic_sections. */
8584 if (elf_hash_table (info)->stab_info.stabstr == o)
8586 if (elf_hash_table (info)->eh_info.hdr_sec == o)
8588 if ((elf_section_data (o->output_section)->this_hdr.sh_type
8590 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0)
8592 if (! bfd_set_section_contents (abfd, o->output_section,
8594 (file_ptr) o->output_offset,
8600 /* The contents of the .dynstr section are actually in a
8602 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
8603 if (bfd_seek (abfd, off, SEEK_SET) != 0
8604 || ! _bfd_elf_strtab_emit (abfd,
8605 elf_hash_table (info)->dynstr))
8611 if (info->relocatable)
8613 bfd_boolean failed = FALSE;
8615 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
8620 /* If we have optimized stabs strings, output them. */
8621 if (elf_hash_table (info)->stab_info.stabstr != NULL)
8623 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
8627 if (info->eh_frame_hdr)
8629 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
8633 if (finfo.symstrtab != NULL)
8634 _bfd_stringtab_free (finfo.symstrtab);
8635 if (finfo.contents != NULL)
8636 free (finfo.contents);
8637 if (finfo.external_relocs != NULL)
8638 free (finfo.external_relocs);
8639 if (finfo.internal_relocs != NULL)
8640 free (finfo.internal_relocs);
8641 if (finfo.external_syms != NULL)
8642 free (finfo.external_syms);
8643 if (finfo.locsym_shndx != NULL)
8644 free (finfo.locsym_shndx);
8645 if (finfo.internal_syms != NULL)
8646 free (finfo.internal_syms);
8647 if (finfo.indices != NULL)
8648 free (finfo.indices);
8649 if (finfo.sections != NULL)
8650 free (finfo.sections);
8651 if (finfo.symbuf != NULL)
8652 free (finfo.symbuf);
8653 if (finfo.symshndxbuf != NULL)
8654 free (finfo.symshndxbuf);
8655 for (o = abfd->sections; o != NULL; o = o->next)
8657 if ((o->flags & SEC_RELOC) != 0
8658 && elf_section_data (o)->rel_hashes != NULL)
8659 free (elf_section_data (o)->rel_hashes);
8662 elf_tdata (abfd)->linker = TRUE;
8667 if (finfo.symstrtab != NULL)
8668 _bfd_stringtab_free (finfo.symstrtab);
8669 if (finfo.contents != NULL)
8670 free (finfo.contents);
8671 if (finfo.external_relocs != NULL)
8672 free (finfo.external_relocs);
8673 if (finfo.internal_relocs != NULL)
8674 free (finfo.internal_relocs);
8675 if (finfo.external_syms != NULL)
8676 free (finfo.external_syms);
8677 if (finfo.locsym_shndx != NULL)
8678 free (finfo.locsym_shndx);
8679 if (finfo.internal_syms != NULL)
8680 free (finfo.internal_syms);
8681 if (finfo.indices != NULL)
8682 free (finfo.indices);
8683 if (finfo.sections != NULL)
8684 free (finfo.sections);
8685 if (finfo.symbuf != NULL)
8686 free (finfo.symbuf);
8687 if (finfo.symshndxbuf != NULL)
8688 free (finfo.symshndxbuf);
8689 for (o = abfd->sections; o != NULL; o = o->next)
8691 if ((o->flags & SEC_RELOC) != 0
8692 && elf_section_data (o)->rel_hashes != NULL)
8693 free (elf_section_data (o)->rel_hashes);
8699 /* Garbage collect unused sections. */
8701 /* The mark phase of garbage collection. For a given section, mark
8702 it and any sections in this section's group, and all the sections
8703 which define symbols to which it refers. */
8705 typedef asection * (*gc_mark_hook_fn)
8706 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
8707 struct elf_link_hash_entry *, Elf_Internal_Sym *);
8710 _bfd_elf_gc_mark (struct bfd_link_info *info,
8712 gc_mark_hook_fn gc_mark_hook)
8716 asection *group_sec;
8720 /* Mark all the sections in the group. */
8721 group_sec = elf_section_data (sec)->next_in_group;
8722 if (group_sec && !group_sec->gc_mark)
8723 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
8726 /* Look through the section relocs. */
8728 is_eh = strcmp (sec->name, ".eh_frame") == 0;
8729 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0)
8731 Elf_Internal_Rela *relstart, *rel, *relend;
8732 Elf_Internal_Shdr *symtab_hdr;
8733 struct elf_link_hash_entry **sym_hashes;
8736 bfd *input_bfd = sec->owner;
8737 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd);
8738 Elf_Internal_Sym *isym = NULL;
8741 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8742 sym_hashes = elf_sym_hashes (input_bfd);
8744 /* Read the local symbols. */
8745 if (elf_bad_symtab (input_bfd))
8747 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym;
8751 extsymoff = nlocsyms = symtab_hdr->sh_info;
8753 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
8754 if (isym == NULL && nlocsyms != 0)
8756 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0,
8762 /* Read the relocations. */
8763 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL,
8765 if (relstart == NULL)
8770 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
8772 if (bed->s->arch_size == 32)
8777 for (rel = relstart; rel < relend; rel++)
8779 unsigned long r_symndx;
8781 struct elf_link_hash_entry *h;
8783 r_symndx = rel->r_info >> r_sym_shift;
8787 if (r_symndx >= nlocsyms
8788 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL)
8790 h = sym_hashes[r_symndx - extsymoff];
8791 while (h->root.type == bfd_link_hash_indirect
8792 || h->root.type == bfd_link_hash_warning)
8793 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8794 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL);
8798 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]);
8801 if (rsec && !rsec->gc_mark)
8803 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
8806 rsec->gc_mark_from_eh = 1;
8807 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
8816 if (elf_section_data (sec)->relocs != relstart)
8819 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym)
8821 if (! info->keep_memory)
8824 symtab_hdr->contents = (unsigned char *) isym;
8831 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
8833 struct elf_gc_sweep_symbol_info {
8834 struct bfd_link_info *info;
8835 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
8840 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
8842 if (h->root.type == bfd_link_hash_warning)
8843 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8845 if ((h->root.type == bfd_link_hash_defined
8846 || h->root.type == bfd_link_hash_defweak)
8847 && !h->root.u.def.section->gc_mark
8848 && !(h->root.u.def.section->owner->flags & DYNAMIC))
8850 struct elf_gc_sweep_symbol_info *inf = data;
8851 (*inf->hide_symbol) (inf->info, h, TRUE);
8857 /* The sweep phase of garbage collection. Remove all garbage sections. */
8859 typedef bfd_boolean (*gc_sweep_hook_fn)
8860 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
8863 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
8866 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8867 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
8868 unsigned long section_sym_count;
8869 struct elf_gc_sweep_symbol_info sweep_info;
8871 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
8875 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
8878 for (o = sub->sections; o != NULL; o = o->next)
8880 /* Keep debug and special sections. */
8881 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
8882 || (o->flags & (SEC_ALLOC | SEC_LOAD)) == 0)
8888 /* Skip sweeping sections already excluded. */
8889 if (o->flags & SEC_EXCLUDE)
8892 /* Since this is early in the link process, it is simple
8893 to remove a section from the output. */
8894 o->flags |= SEC_EXCLUDE;
8896 /* But we also have to update some of the relocation
8897 info we collected before. */
8899 && (o->flags & SEC_RELOC) != 0
8900 && o->reloc_count > 0
8901 && !bfd_is_abs_section (o->output_section))
8903 Elf_Internal_Rela *internal_relocs;
8907 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
8909 if (internal_relocs == NULL)
8912 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
8914 if (elf_section_data (o)->relocs != internal_relocs)
8915 free (internal_relocs);
8923 /* Remove the symbols that were in the swept sections from the dynamic
8924 symbol table. GCFIXME: Anyone know how to get them out of the
8925 static symbol table as well? */
8926 sweep_info.info = info;
8927 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
8928 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
8931 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
8935 /* Propagate collected vtable information. This is called through
8936 elf_link_hash_traverse. */
8939 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
8941 if (h->root.type == bfd_link_hash_warning)
8942 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8944 /* Those that are not vtables. */
8945 if (h->vtable == NULL || h->vtable->parent == NULL)
8948 /* Those vtables that do not have parents, we cannot merge. */
8949 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
8952 /* If we've already been done, exit. */
8953 if (h->vtable->used && h->vtable->used[-1])
8956 /* Make sure the parent's table is up to date. */
8957 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
8959 if (h->vtable->used == NULL)
8961 /* None of this table's entries were referenced. Re-use the
8963 h->vtable->used = h->vtable->parent->vtable->used;
8964 h->vtable->size = h->vtable->parent->vtable->size;
8969 bfd_boolean *cu, *pu;
8971 /* Or the parent's entries into ours. */
8972 cu = h->vtable->used;
8974 pu = h->vtable->parent->vtable->used;
8977 const struct elf_backend_data *bed;
8978 unsigned int log_file_align;
8980 bed = get_elf_backend_data (h->root.u.def.section->owner);
8981 log_file_align = bed->s->log_file_align;
8982 n = h->vtable->parent->vtable->size >> log_file_align;
8997 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
9000 bfd_vma hstart, hend;
9001 Elf_Internal_Rela *relstart, *relend, *rel;
9002 const struct elf_backend_data *bed;
9003 unsigned int log_file_align;
9005 if (h->root.type == bfd_link_hash_warning)
9006 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9008 /* Take care of both those symbols that do not describe vtables as
9009 well as those that are not loaded. */
9010 if (h->vtable == NULL || h->vtable->parent == NULL)
9013 BFD_ASSERT (h->root.type == bfd_link_hash_defined
9014 || h->root.type == bfd_link_hash_defweak);
9016 sec = h->root.u.def.section;
9017 hstart = h->root.u.def.value;
9018 hend = hstart + h->size;
9020 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
9022 return *(bfd_boolean *) okp = FALSE;
9023 bed = get_elf_backend_data (sec->owner);
9024 log_file_align = bed->s->log_file_align;
9026 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
9028 for (rel = relstart; rel < relend; ++rel)
9029 if (rel->r_offset >= hstart && rel->r_offset < hend)
9031 /* If the entry is in use, do nothing. */
9033 && (rel->r_offset - hstart) < h->vtable->size)
9035 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
9036 if (h->vtable->used[entry])
9039 /* Otherwise, kill it. */
9040 rel->r_offset = rel->r_info = rel->r_addend = 0;
9046 /* Mark sections containing dynamically referenced symbols. When
9047 building shared libraries, we must assume that any visible symbol is
9051 elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
9053 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9055 if (h->root.type == bfd_link_hash_warning)
9056 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9058 if ((h->root.type == bfd_link_hash_defined
9059 || h->root.type == bfd_link_hash_defweak)
9063 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
9064 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN)))
9065 h->root.u.def.section->flags |= SEC_KEEP;
9070 /* Do mark and sweep of unused sections. */
9073 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
9075 bfd_boolean ok = TRUE;
9077 asection * (*gc_mark_hook)
9078 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
9079 struct elf_link_hash_entry *h, Elf_Internal_Sym *);
9081 if (!get_elf_backend_data (abfd)->can_gc_sections
9082 || info->relocatable
9083 || info->emitrelocations
9084 || !is_elf_hash_table (info->hash))
9086 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
9090 /* Apply transitive closure to the vtable entry usage info. */
9091 elf_link_hash_traverse (elf_hash_table (info),
9092 elf_gc_propagate_vtable_entries_used,
9097 /* Kill the vtable relocations that were not used. */
9098 elf_link_hash_traverse (elf_hash_table (info),
9099 elf_gc_smash_unused_vtentry_relocs,
9104 /* Mark dynamically referenced symbols. */
9105 if (elf_hash_table (info)->dynamic_sections_created)
9106 elf_link_hash_traverse (elf_hash_table (info),
9107 elf_gc_mark_dynamic_ref_symbol,
9110 /* Grovel through relocs to find out who stays ... */
9111 gc_mark_hook = get_elf_backend_data (abfd)->gc_mark_hook;
9112 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9116 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9119 for (o = sub->sections; o != NULL; o = o->next)
9120 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark)
9121 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9125 /* ... again for sections marked from eh_frame. */
9126 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
9130 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
9133 /* Keep .gcc_except_table.* if the associated .text.* is
9134 marked. This isn't very nice, but the proper solution,
9135 splitting .eh_frame up and using comdat doesn't pan out
9136 easily due to needing special relocs to handle the
9137 difference of two symbols in separate sections.
9138 Don't keep code sections referenced by .eh_frame. */
9139 for (o = sub->sections; o != NULL; o = o->next)
9140 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0)
9142 if (strncmp (o->name, ".gcc_except_table.", 18) == 0)
9148 len = strlen (o->name + 18) + 1;
9149 fn_name = bfd_malloc (len + 6);
9150 if (fn_name == NULL)
9152 memcpy (fn_name, ".text.", 6);
9153 memcpy (fn_name + 6, o->name + 18, len);
9154 fn_text = bfd_get_section_by_name (sub, fn_name);
9156 if (fn_text == NULL || !fn_text->gc_mark)
9160 /* If not using specially named exception table section,
9161 then keep whatever we are using. */
9162 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
9167 /* ... and mark SEC_EXCLUDE for those that go. */
9168 return elf_gc_sweep (abfd, info);
9171 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
9174 bfd_elf_gc_record_vtinherit (bfd *abfd,
9176 struct elf_link_hash_entry *h,
9179 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
9180 struct elf_link_hash_entry **search, *child;
9181 bfd_size_type extsymcount;
9182 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9184 /* The sh_info field of the symtab header tells us where the
9185 external symbols start. We don't care about the local symbols at
9187 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
9188 if (!elf_bad_symtab (abfd))
9189 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
9191 sym_hashes = elf_sym_hashes (abfd);
9192 sym_hashes_end = sym_hashes + extsymcount;
9194 /* Hunt down the child symbol, which is in this section at the same
9195 offset as the relocation. */
9196 for (search = sym_hashes; search != sym_hashes_end; ++search)
9198 if ((child = *search) != NULL
9199 && (child->root.type == bfd_link_hash_defined
9200 || child->root.type == bfd_link_hash_defweak)
9201 && child->root.u.def.section == sec
9202 && child->root.u.def.value == offset)
9206 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
9207 abfd, sec, (unsigned long) offset);
9208 bfd_set_error (bfd_error_invalid_operation);
9214 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable));
9220 /* This *should* only be the absolute section. It could potentially
9221 be that someone has defined a non-global vtable though, which
9222 would be bad. It isn't worth paging in the local symbols to be
9223 sure though; that case should simply be handled by the assembler. */
9225 child->vtable->parent = (struct elf_link_hash_entry *) -1;
9228 child->vtable->parent = h;
9233 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
9236 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
9237 asection *sec ATTRIBUTE_UNUSED,
9238 struct elf_link_hash_entry *h,
9241 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9242 unsigned int log_file_align = bed->s->log_file_align;
9246 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable));
9251 if (addend >= h->vtable->size)
9253 size_t size, bytes, file_align;
9254 bfd_boolean *ptr = h->vtable->used;
9256 /* While the symbol is undefined, we have to be prepared to handle
9258 file_align = 1 << log_file_align;
9259 if (h->root.type == bfd_link_hash_undefined)
9260 size = addend + file_align;
9266 /* Oops! We've got a reference past the defined end of
9267 the table. This is probably a bug -- shall we warn? */
9268 size = addend + file_align;
9271 size = (size + file_align - 1) & -file_align;
9273 /* Allocate one extra entry for use as a "done" flag for the
9274 consolidation pass. */
9275 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
9279 ptr = bfd_realloc (ptr - 1, bytes);
9285 oldbytes = (((h->vtable->size >> log_file_align) + 1)
9286 * sizeof (bfd_boolean));
9287 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
9291 ptr = bfd_zmalloc (bytes);
9296 /* And arrange for that done flag to be at index -1. */
9297 h->vtable->used = ptr + 1;
9298 h->vtable->size = size;
9301 h->vtable->used[addend >> log_file_align] = TRUE;
9306 struct alloc_got_off_arg {
9308 unsigned int got_elt_size;
9311 /* We need a special top-level link routine to convert got reference counts
9312 to real got offsets. */
9315 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
9317 struct alloc_got_off_arg *gofarg = arg;
9319 if (h->root.type == bfd_link_hash_warning)
9320 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9322 if (h->got.refcount > 0)
9324 h->got.offset = gofarg->gotoff;
9325 gofarg->gotoff += gofarg->got_elt_size;
9328 h->got.offset = (bfd_vma) -1;
9333 /* And an accompanying bit to work out final got entry offsets once
9334 we're done. Should be called from final_link. */
9337 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
9338 struct bfd_link_info *info)
9341 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9343 unsigned int got_elt_size = bed->s->arch_size / 8;
9344 struct alloc_got_off_arg gofarg;
9346 if (! is_elf_hash_table (info->hash))
9349 /* The GOT offset is relative to the .got section, but the GOT header is
9350 put into the .got.plt section, if the backend uses it. */
9351 if (bed->want_got_plt)
9354 gotoff = bed->got_header_size;
9356 /* Do the local .got entries first. */
9357 for (i = info->input_bfds; i; i = i->link_next)
9359 bfd_signed_vma *local_got;
9360 bfd_size_type j, locsymcount;
9361 Elf_Internal_Shdr *symtab_hdr;
9363 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
9366 local_got = elf_local_got_refcounts (i);
9370 symtab_hdr = &elf_tdata (i)->symtab_hdr;
9371 if (elf_bad_symtab (i))
9372 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9374 locsymcount = symtab_hdr->sh_info;
9376 for (j = 0; j < locsymcount; ++j)
9378 if (local_got[j] > 0)
9380 local_got[j] = gotoff;
9381 gotoff += got_elt_size;
9384 local_got[j] = (bfd_vma) -1;
9388 /* Then the global .got entries. .plt refcounts are handled by
9389 adjust_dynamic_symbol */
9390 gofarg.gotoff = gotoff;
9391 gofarg.got_elt_size = got_elt_size;
9392 elf_link_hash_traverse (elf_hash_table (info),
9393 elf_gc_allocate_got_offsets,
9398 /* Many folk need no more in the way of final link than this, once
9399 got entry reference counting is enabled. */
9402 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
9404 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
9407 /* Invoke the regular ELF backend linker to do all the work. */
9408 return bfd_elf_final_link (abfd, info);
9412 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
9414 struct elf_reloc_cookie *rcookie = cookie;
9416 if (rcookie->bad_symtab)
9417 rcookie->rel = rcookie->rels;
9419 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
9421 unsigned long r_symndx;
9423 if (! rcookie->bad_symtab)
9424 if (rcookie->rel->r_offset > offset)
9426 if (rcookie->rel->r_offset != offset)
9429 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
9430 if (r_symndx == SHN_UNDEF)
9433 if (r_symndx >= rcookie->locsymcount
9434 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
9436 struct elf_link_hash_entry *h;
9438 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
9440 while (h->root.type == bfd_link_hash_indirect
9441 || h->root.type == bfd_link_hash_warning)
9442 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9444 if ((h->root.type == bfd_link_hash_defined
9445 || h->root.type == bfd_link_hash_defweak)
9446 && elf_discarded_section (h->root.u.def.section))
9453 /* It's not a relocation against a global symbol,
9454 but it could be a relocation against a local
9455 symbol for a discarded section. */
9457 Elf_Internal_Sym *isym;
9459 /* Need to: get the symbol; get the section. */
9460 isym = &rcookie->locsyms[r_symndx];
9461 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE)
9463 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
9464 if (isec != NULL && elf_discarded_section (isec))
9473 /* Discard unneeded references to discarded sections.
9474 Returns TRUE if any section's size was changed. */
9475 /* This function assumes that the relocations are in sorted order,
9476 which is true for all known assemblers. */
9479 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
9481 struct elf_reloc_cookie cookie;
9482 asection *stab, *eh;
9483 Elf_Internal_Shdr *symtab_hdr;
9484 const struct elf_backend_data *bed;
9487 bfd_boolean ret = FALSE;
9489 if (info->traditional_format
9490 || !is_elf_hash_table (info->hash))
9493 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
9495 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
9498 bed = get_elf_backend_data (abfd);
9500 if ((abfd->flags & DYNAMIC) != 0)
9503 eh = bfd_get_section_by_name (abfd, ".eh_frame");
9504 if (info->relocatable
9507 || bfd_is_abs_section (eh->output_section))))
9510 stab = bfd_get_section_by_name (abfd, ".stab");
9513 || bfd_is_abs_section (stab->output_section)
9514 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
9519 && bed->elf_backend_discard_info == NULL)
9522 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9524 cookie.sym_hashes = elf_sym_hashes (abfd);
9525 cookie.bad_symtab = elf_bad_symtab (abfd);
9526 if (cookie.bad_symtab)
9528 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9529 cookie.extsymoff = 0;
9533 cookie.locsymcount = symtab_hdr->sh_info;
9534 cookie.extsymoff = symtab_hdr->sh_info;
9537 if (bed->s->arch_size == 32)
9538 cookie.r_sym_shift = 8;
9540 cookie.r_sym_shift = 32;
9542 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
9543 if (cookie.locsyms == NULL && cookie.locsymcount != 0)
9545 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9546 cookie.locsymcount, 0,
9548 if (cookie.locsyms == NULL)
9555 count = stab->reloc_count;
9557 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL,
9559 if (cookie.rels != NULL)
9561 cookie.rel = cookie.rels;
9562 cookie.relend = cookie.rels;
9563 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9564 if (_bfd_discard_section_stabs (abfd, stab,
9565 elf_section_data (stab)->sec_info,
9566 bfd_elf_reloc_symbol_deleted_p,
9569 if (elf_section_data (stab)->relocs != cookie.rels)
9577 count = eh->reloc_count;
9579 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL,
9581 cookie.rel = cookie.rels;
9582 cookie.relend = cookie.rels;
9583 if (cookie.rels != NULL)
9584 cookie.relend += count * bed->s->int_rels_per_ext_rel;
9586 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
9587 bfd_elf_reloc_symbol_deleted_p,
9591 if (cookie.rels != NULL
9592 && elf_section_data (eh)->relocs != cookie.rels)
9596 if (bed->elf_backend_discard_info != NULL
9597 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
9600 if (cookie.locsyms != NULL
9601 && symtab_hdr->contents != (unsigned char *) cookie.locsyms)
9603 if (! info->keep_memory)
9604 free (cookie.locsyms);
9606 symtab_hdr->contents = (unsigned char *) cookie.locsyms;
9610 if (info->eh_frame_hdr
9611 && !info->relocatable
9612 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
9619 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section * sec)
9622 const char *name, *p;
9623 struct bfd_section_already_linked *l;
9624 struct bfd_section_already_linked_hash_entry *already_linked_list;
9627 /* A single member comdat group section may be discarded by a
9628 linkonce section. See below. */
9629 if (sec->output_section == bfd_abs_section_ptr)
9634 /* Check if it belongs to a section group. */
9635 group = elf_sec_group (sec);
9637 /* Return if it isn't a linkonce section nor a member of a group. A
9638 comdat group section also has SEC_LINK_ONCE set. */
9639 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL)
9644 /* If this is the member of a single member comdat group, check if
9645 the group should be discarded. */
9646 if (elf_next_in_group (sec) == sec
9647 && (group->flags & SEC_LINK_ONCE) != 0)
9653 /* FIXME: When doing a relocatable link, we may have trouble
9654 copying relocations in other sections that refer to local symbols
9655 in the section being discarded. Those relocations will have to
9656 be converted somehow; as of this writing I'm not sure that any of
9657 the backends handle that correctly.
9659 It is tempting to instead not discard link once sections when
9660 doing a relocatable link (technically, they should be discarded
9661 whenever we are building constructors). However, that fails,
9662 because the linker winds up combining all the link once sections
9663 into a single large link once section, which defeats the purpose
9664 of having link once sections in the first place.
9666 Also, not merging link once sections in a relocatable link
9667 causes trouble for MIPS ELF, which relies on link once semantics
9668 to handle the .reginfo section correctly. */
9670 name = bfd_get_section_name (abfd, sec);
9672 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0
9673 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
9678 already_linked_list = bfd_section_already_linked_table_lookup (p);
9680 for (l = already_linked_list->entry; l != NULL; l = l->next)
9682 /* We may have 3 different sections on the list: group section,
9683 comdat section and linkonce section. SEC may be a linkonce or
9684 group section. We match a group section with a group section,
9685 a linkonce section with a linkonce section, and ignore comdat
9687 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
9688 && strcmp (name, l->sec->name) == 0
9689 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL)
9691 /* The section has already been linked. See if we should
9693 switch (flags & SEC_LINK_DUPLICATES)
9698 case SEC_LINK_DUPLICATES_DISCARD:
9701 case SEC_LINK_DUPLICATES_ONE_ONLY:
9702 (*_bfd_error_handler)
9703 (_("%B: ignoring duplicate section `%A'"),
9707 case SEC_LINK_DUPLICATES_SAME_SIZE:
9708 if (sec->size != l->sec->size)
9709 (*_bfd_error_handler)
9710 (_("%B: duplicate section `%A' has different size"),
9714 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
9715 if (sec->size != l->sec->size)
9716 (*_bfd_error_handler)
9717 (_("%B: duplicate section `%A' has different size"),
9719 else if (sec->size != 0)
9721 bfd_byte *sec_contents, *l_sec_contents;
9723 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents))
9724 (*_bfd_error_handler)
9725 (_("%B: warning: could not read contents of section `%A'"),
9727 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
9729 (*_bfd_error_handler)
9730 (_("%B: warning: could not read contents of section `%A'"),
9731 l->sec->owner, l->sec);
9732 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
9733 (*_bfd_error_handler)
9734 (_("%B: warning: duplicate section `%A' has different contents"),
9738 free (sec_contents);
9740 free (l_sec_contents);
9745 /* Set the output_section field so that lang_add_section
9746 does not create a lang_input_section structure for this
9747 section. Since there might be a symbol in the section
9748 being discarded, we must retain a pointer to the section
9749 which we are really going to use. */
9750 sec->output_section = bfd_abs_section_ptr;
9751 sec->kept_section = l->sec;
9753 if (flags & SEC_GROUP)
9755 asection *first = elf_next_in_group (sec);
9756 asection *s = first;
9760 s->output_section = bfd_abs_section_ptr;
9761 /* Record which group discards it. */
9762 s->kept_section = l->sec;
9763 s = elf_next_in_group (s);
9764 /* These lists are circular. */
9776 /* If this is the member of a single member comdat group and the
9777 group hasn't be discarded, we check if it matches a linkonce
9778 section. We only record the discarded comdat group. Otherwise
9779 the undiscarded group will be discarded incorrectly later since
9780 itself has been recorded. */
9781 for (l = already_linked_list->entry; l != NULL; l = l->next)
9782 if ((l->sec->flags & SEC_GROUP) == 0
9783 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL
9784 && bfd_elf_match_symbols_in_sections (l->sec,
9785 elf_next_in_group (sec)))
9787 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr;
9788 elf_next_in_group (sec)->kept_section = l->sec;
9789 group->output_section = bfd_abs_section_ptr;
9796 /* There is no direct match. But for linkonce section, we should
9797 check if there is a match with comdat group member. We always
9798 record the linkonce section, discarded or not. */
9799 for (l = already_linked_list->entry; l != NULL; l = l->next)
9800 if (l->sec->flags & SEC_GROUP)
9802 asection *first = elf_next_in_group (l->sec);
9805 && elf_next_in_group (first) == first
9806 && bfd_elf_match_symbols_in_sections (first, sec))
9808 sec->output_section = bfd_abs_section_ptr;
9809 sec->kept_section = l->sec;
9814 /* This is the first section with this name. Record it. */
9815 bfd_section_already_linked_table_insert (already_linked_list, sec);
9819 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
9821 return sym->st_shndx == SHN_COMMON;
9825 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
9831 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
9833 return bfd_com_section_ptr;