1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
29 #include "safe-ctype.h"
30 #include "libiberty.h"
33 /* This struct is used to pass information to routines called via
34 elf_link_hash_traverse which must return failure. */
36 struct elf_info_failed
38 struct bfd_link_info *info;
42 /* This structure is used to pass information to
43 _bfd_elf_link_find_version_dependencies. */
45 struct elf_find_verdep_info
47 /* General link information. */
48 struct bfd_link_info *info;
49 /* The number of dependencies. */
51 /* Whether we had a failure. */
55 static bfd_boolean _bfd_elf_fix_symbol_flags
56 (struct elf_link_hash_entry *, struct elf_info_failed *);
58 /* Define a symbol in a dynamic linkage section. */
60 struct elf_link_hash_entry *
61 _bfd_elf_define_linkage_sym (bfd *abfd,
62 struct bfd_link_info *info,
66 struct elf_link_hash_entry *h;
67 struct bfd_link_hash_entry *bh;
68 const struct elf_backend_data *bed;
70 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
73 /* Zap symbol defined in an as-needed lib that wasn't linked.
74 This is a symptom of a larger problem: Absolute symbols
75 defined in shared libraries can't be overridden, because we
76 lose the link to the bfd which is via the symbol section. */
77 h->root.type = bfd_link_hash_new;
81 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
83 get_elf_backend_data (abfd)->collect,
86 h = (struct elf_link_hash_entry *) bh;
90 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
92 bed = get_elf_backend_data (abfd);
93 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
98 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
102 struct elf_link_hash_entry *h;
103 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
104 struct elf_link_hash_table *htab = elf_hash_table (info);
106 /* This function may be called more than once. */
107 s = bfd_get_linker_section (abfd, ".got");
111 flags = bed->dynamic_sec_flags;
113 s = bfd_make_section_anyway_with_flags (abfd,
114 (bed->rela_plts_and_copies_p
115 ? ".rela.got" : ".rel.got"),
116 (bed->dynamic_sec_flags
119 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
123 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
125 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
129 if (bed->want_got_plt)
131 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
133 || !bfd_set_section_alignment (abfd, s,
134 bed->s->log_file_align))
139 /* The first bit of the global offset table is the header. */
140 s->size += bed->got_header_size;
142 if (bed->want_got_sym)
144 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
145 (or .got.plt) section. We don't do this in the linker script
146 because we don't want to define the symbol if we are not creating
147 a global offset table. */
148 h = _bfd_elf_define_linkage_sym (abfd, info, s,
149 "_GLOBAL_OFFSET_TABLE_");
150 elf_hash_table (info)->hgot = h;
158 /* Create a strtab to hold the dynamic symbol names. */
160 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
162 struct elf_link_hash_table *hash_table;
164 hash_table = elf_hash_table (info);
165 if (hash_table->dynobj == NULL)
166 hash_table->dynobj = abfd;
168 if (hash_table->dynstr == NULL)
170 hash_table->dynstr = _bfd_elf_strtab_init ();
171 if (hash_table->dynstr == NULL)
177 /* Create some sections which will be filled in with dynamic linking
178 information. ABFD is an input file which requires dynamic sections
179 to be created. The dynamic sections take up virtual memory space
180 when the final executable is run, so we need to create them before
181 addresses are assigned to the output sections. We work out the
182 actual contents and size of these sections later. */
185 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
189 const struct elf_backend_data *bed;
191 if (! is_elf_hash_table (info->hash))
194 if (elf_hash_table (info)->dynamic_sections_created)
197 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
200 abfd = elf_hash_table (info)->dynobj;
201 bed = get_elf_backend_data (abfd);
203 flags = bed->dynamic_sec_flags;
205 /* A dynamically linked executable has a .interp section, but a
206 shared library does not. */
207 if (info->executable)
209 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
210 flags | SEC_READONLY);
215 /* Create sections to hold version informations. These are removed
216 if they are not needed. */
217 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
218 flags | SEC_READONLY);
220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
223 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
224 flags | SEC_READONLY);
226 || ! bfd_set_section_alignment (abfd, s, 1))
229 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
230 flags | SEC_READONLY);
232 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
235 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
236 flags | SEC_READONLY);
238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
241 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
242 flags | SEC_READONLY);
246 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
251 /* The special symbol _DYNAMIC is always set to the start of the
252 .dynamic section. We could set _DYNAMIC in a linker script, but we
253 only want to define it if we are, in fact, creating a .dynamic
254 section. We don't want to define it if there is no .dynamic
255 section, since on some ELF platforms the start up code examines it
256 to decide how to initialize the process. */
257 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"))
262 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
263 flags | SEC_READONLY);
265 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
267 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
270 if (info->emit_gnu_hash)
272 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
273 flags | SEC_READONLY);
275 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
277 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
278 4 32-bit words followed by variable count of 64-bit words, then
279 variable count of 32-bit words. */
280 if (bed->s->arch_size == 64)
281 elf_section_data (s)->this_hdr.sh_entsize = 0;
283 elf_section_data (s)->this_hdr.sh_entsize = 4;
286 /* Let the backend create the rest of the sections. This lets the
287 backend set the right flags. The backend will normally create
288 the .got and .plt sections. */
289 if (bed->elf_backend_create_dynamic_sections == NULL
290 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
293 elf_hash_table (info)->dynamic_sections_created = TRUE;
298 /* Create dynamic sections when linking against a dynamic object. */
301 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
303 flagword flags, pltflags;
304 struct elf_link_hash_entry *h;
306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
307 struct elf_link_hash_table *htab = elf_hash_table (info);
309 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
310 .rel[a].bss sections. */
311 flags = bed->dynamic_sec_flags;
314 if (bed->plt_not_loaded)
315 /* We do not clear SEC_ALLOC here because we still want the OS to
316 allocate space for the section; it's just that there's nothing
317 to read in from the object file. */
318 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
320 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
321 if (bed->plt_readonly)
322 pltflags |= SEC_READONLY;
324 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
326 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
330 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
332 if (bed->want_plt_sym)
334 h = _bfd_elf_define_linkage_sym (abfd, info, s,
335 "_PROCEDURE_LINKAGE_TABLE_");
336 elf_hash_table (info)->hplt = h;
341 s = bfd_make_section_anyway_with_flags (abfd,
342 (bed->rela_plts_and_copies_p
343 ? ".rela.plt" : ".rel.plt"),
344 flags | SEC_READONLY);
346 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
350 if (! _bfd_elf_create_got_section (abfd, info))
353 if (bed->want_dynbss)
355 /* The .dynbss section is a place to put symbols which are defined
356 by dynamic objects, are referenced by regular objects, and are
357 not functions. We must allocate space for them in the process
358 image and use a R_*_COPY reloc to tell the dynamic linker to
359 initialize them at run time. The linker script puts the .dynbss
360 section into the .bss section of the final image. */
361 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
362 (SEC_ALLOC | SEC_LINKER_CREATED));
366 /* The .rel[a].bss section holds copy relocs. This section is not
367 normally needed. We need to create it here, though, so that the
368 linker will map it to an output section. We can't just create it
369 only if we need it, because we will not know whether we need it
370 until we have seen all the input files, and the first time the
371 main linker code calls BFD after examining all the input files
372 (size_dynamic_sections) the input sections have already been
373 mapped to the output sections. If the section turns out not to
374 be needed, we can discard it later. We will never need this
375 section when generating a shared object, since they do not use
379 s = bfd_make_section_anyway_with_flags (abfd,
380 (bed->rela_plts_and_copies_p
381 ? ".rela.bss" : ".rel.bss"),
382 flags | SEC_READONLY);
384 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
392 /* Record a new dynamic symbol. We record the dynamic symbols as we
393 read the input files, since we need to have a list of all of them
394 before we can determine the final sizes of the output sections.
395 Note that we may actually call this function even though we are not
396 going to output any dynamic symbols; in some cases we know that a
397 symbol should be in the dynamic symbol table, but only if there is
401 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
402 struct elf_link_hash_entry *h)
404 if (h->dynindx == -1)
406 struct elf_strtab_hash *dynstr;
411 /* XXX: The ABI draft says the linker must turn hidden and
412 internal symbols into STB_LOCAL symbols when producing the
413 DSO. However, if ld.so honors st_other in the dynamic table,
414 this would not be necessary. */
415 switch (ELF_ST_VISIBILITY (h->other))
419 if (h->root.type != bfd_link_hash_undefined
420 && h->root.type != bfd_link_hash_undefweak)
423 if (!elf_hash_table (info)->is_relocatable_executable)
431 h->dynindx = elf_hash_table (info)->dynsymcount;
432 ++elf_hash_table (info)->dynsymcount;
434 dynstr = elf_hash_table (info)->dynstr;
437 /* Create a strtab to hold the dynamic symbol names. */
438 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
443 /* We don't put any version information in the dynamic string
445 name = h->root.root.string;
446 p = strchr (name, ELF_VER_CHR);
448 /* We know that the p points into writable memory. In fact,
449 there are only a few symbols that have read-only names, being
450 those like _GLOBAL_OFFSET_TABLE_ that are created specially
451 by the backends. Most symbols will have names pointing into
452 an ELF string table read from a file, or to objalloc memory. */
455 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
460 if (indx == (bfd_size_type) -1)
462 h->dynstr_index = indx;
468 /* Mark a symbol dynamic. */
471 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
472 struct elf_link_hash_entry *h,
473 Elf_Internal_Sym *sym)
475 struct bfd_elf_dynamic_list *d = info->dynamic_list;
477 /* It may be called more than once on the same H. */
478 if(h->dynamic || info->relocatable)
481 if ((info->dynamic_data
482 && (h->type == STT_OBJECT
484 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
486 && h->root.type == bfd_link_hash_new
487 && (*d->match) (&d->head, NULL, h->root.root.string)))
491 /* Record an assignment to a symbol made by a linker script. We need
492 this in case some dynamic object refers to this symbol. */
495 bfd_elf_record_link_assignment (bfd *output_bfd,
496 struct bfd_link_info *info,
501 struct elf_link_hash_entry *h, *hv;
502 struct elf_link_hash_table *htab;
503 const struct elf_backend_data *bed;
505 if (!is_elf_hash_table (info->hash))
508 htab = elf_hash_table (info);
509 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
513 switch (h->root.type)
515 case bfd_link_hash_defined:
516 case bfd_link_hash_defweak:
517 case bfd_link_hash_common:
519 case bfd_link_hash_undefweak:
520 case bfd_link_hash_undefined:
521 /* Since we're defining the symbol, don't let it seem to have not
522 been defined. record_dynamic_symbol and size_dynamic_sections
523 may depend on this. */
524 h->root.type = bfd_link_hash_new;
525 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
526 bfd_link_repair_undef_list (&htab->root);
528 case bfd_link_hash_new:
529 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
532 case bfd_link_hash_indirect:
533 /* We had a versioned symbol in a dynamic library. We make the
534 the versioned symbol point to this one. */
535 bed = get_elf_backend_data (output_bfd);
537 while (hv->root.type == bfd_link_hash_indirect
538 || hv->root.type == bfd_link_hash_warning)
539 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
540 /* We don't need to update h->root.u since linker will set them
542 h->root.type = bfd_link_hash_undefined;
543 hv->root.type = bfd_link_hash_indirect;
544 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
547 case bfd_link_hash_warning:
552 /* If this symbol is being provided by the linker script, and it is
553 currently defined by a dynamic object, but not by a regular
554 object, then mark it as undefined so that the generic linker will
555 force the correct value. */
559 h->root.type = bfd_link_hash_undefined;
561 /* If this symbol is not being provided by the linker script, and it is
562 currently defined by a dynamic object, but not by a regular object,
563 then clear out any version information because the symbol will not be
564 associated with the dynamic object any more. */
568 h->verinfo.verdef = NULL;
574 bed = get_elf_backend_data (output_bfd);
575 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
576 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
579 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
581 if (!info->relocatable
583 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
584 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
590 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
593 if (! bfd_elf_link_record_dynamic_symbol (info, h))
596 /* If this is a weak defined symbol, and we know a corresponding
597 real symbol from the same dynamic object, make sure the real
598 symbol is also made into a dynamic symbol. */
599 if (h->u.weakdef != NULL
600 && h->u.weakdef->dynindx == -1)
602 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
610 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
611 success, and 2 on a failure caused by attempting to record a symbol
612 in a discarded section, eg. a discarded link-once section symbol. */
615 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
620 struct elf_link_local_dynamic_entry *entry;
621 struct elf_link_hash_table *eht;
622 struct elf_strtab_hash *dynstr;
623 unsigned long dynstr_index;
625 Elf_External_Sym_Shndx eshndx;
626 char esym[sizeof (Elf64_External_Sym)];
628 if (! is_elf_hash_table (info->hash))
631 /* See if the entry exists already. */
632 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
633 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
636 amt = sizeof (*entry);
637 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
641 /* Go find the symbol, so that we can find it's name. */
642 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
643 1, input_indx, &entry->isym, esym, &eshndx))
645 bfd_release (input_bfd, entry);
649 if (entry->isym.st_shndx != SHN_UNDEF
650 && entry->isym.st_shndx < SHN_LORESERVE)
654 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
655 if (s == NULL || bfd_is_abs_section (s->output_section))
657 /* We can still bfd_release here as nothing has done another
658 bfd_alloc. We can't do this later in this function. */
659 bfd_release (input_bfd, entry);
664 name = (bfd_elf_string_from_elf_section
665 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
666 entry->isym.st_name));
668 dynstr = elf_hash_table (info)->dynstr;
671 /* Create a strtab to hold the dynamic symbol names. */
672 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
677 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
678 if (dynstr_index == (unsigned long) -1)
680 entry->isym.st_name = dynstr_index;
682 eht = elf_hash_table (info);
684 entry->next = eht->dynlocal;
685 eht->dynlocal = entry;
686 entry->input_bfd = input_bfd;
687 entry->input_indx = input_indx;
690 /* Whatever binding the symbol had before, it's now local. */
692 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
694 /* The dynindx will be set at the end of size_dynamic_sections. */
699 /* Return the dynindex of a local dynamic symbol. */
702 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
706 struct elf_link_local_dynamic_entry *e;
708 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
709 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
714 /* This function is used to renumber the dynamic symbols, if some of
715 them are removed because they are marked as local. This is called
716 via elf_link_hash_traverse. */
719 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
722 size_t *count = (size_t *) data;
727 if (h->dynindx != -1)
728 h->dynindx = ++(*count);
734 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
735 STB_LOCAL binding. */
738 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
741 size_t *count = (size_t *) data;
743 if (!h->forced_local)
746 if (h->dynindx != -1)
747 h->dynindx = ++(*count);
752 /* Return true if the dynamic symbol for a given section should be
753 omitted when creating a shared library. */
755 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
756 struct bfd_link_info *info,
759 struct elf_link_hash_table *htab;
761 switch (elf_section_data (p)->this_hdr.sh_type)
765 /* If sh_type is yet undecided, assume it could be
766 SHT_PROGBITS/SHT_NOBITS. */
768 htab = elf_hash_table (info);
769 if (p == htab->tls_sec)
772 if (htab->text_index_section != NULL)
773 return p != htab->text_index_section && p != htab->data_index_section;
775 if (strcmp (p->name, ".got") == 0
776 || strcmp (p->name, ".got.plt") == 0
777 || strcmp (p->name, ".plt") == 0)
781 if (htab->dynobj != NULL
782 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
783 && ip->output_section == p)
788 /* There shouldn't be section relative relocations
789 against any other section. */
795 /* Assign dynsym indices. In a shared library we generate a section
796 symbol for each output section, which come first. Next come symbols
797 which have been forced to local binding. Then all of the back-end
798 allocated local dynamic syms, followed by the rest of the global
802 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
803 struct bfd_link_info *info,
804 unsigned long *section_sym_count)
806 unsigned long dynsymcount = 0;
808 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
810 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
812 for (p = output_bfd->sections; p ; p = p->next)
813 if ((p->flags & SEC_EXCLUDE) == 0
814 && (p->flags & SEC_ALLOC) != 0
815 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
816 elf_section_data (p)->dynindx = ++dynsymcount;
818 elf_section_data (p)->dynindx = 0;
820 *section_sym_count = dynsymcount;
822 elf_link_hash_traverse (elf_hash_table (info),
823 elf_link_renumber_local_hash_table_dynsyms,
826 if (elf_hash_table (info)->dynlocal)
828 struct elf_link_local_dynamic_entry *p;
829 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
830 p->dynindx = ++dynsymcount;
833 elf_link_hash_traverse (elf_hash_table (info),
834 elf_link_renumber_hash_table_dynsyms,
837 /* There is an unused NULL entry at the head of the table which
838 we must account for in our count. Unless there weren't any
839 symbols, which means we'll have no table at all. */
840 if (dynsymcount != 0)
843 elf_hash_table (info)->dynsymcount = dynsymcount;
847 /* Merge st_other field. */
850 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
851 Elf_Internal_Sym *isym, bfd_boolean definition,
854 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
856 /* If st_other has a processor-specific meaning, specific
857 code might be needed here. We never merge the visibility
858 attribute with the one from a dynamic object. */
859 if (bed->elf_backend_merge_symbol_attribute)
860 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
863 /* If this symbol has default visibility and the user has requested
864 we not re-export it, then mark it as hidden. */
868 || (abfd->my_archive && abfd->my_archive->no_export))
869 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
870 isym->st_other = (STV_HIDDEN
871 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
873 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
875 unsigned char hvis, symvis, other, nvis;
877 /* Only merge the visibility. Leave the remainder of the
878 st_other field to elf_backend_merge_symbol_attribute. */
879 other = h->other & ~ELF_ST_VISIBILITY (-1);
881 /* Combine visibilities, using the most constraining one. */
882 hvis = ELF_ST_VISIBILITY (h->other);
883 symvis = ELF_ST_VISIBILITY (isym->st_other);
889 nvis = hvis < symvis ? hvis : symvis;
891 h->other = other | nvis;
895 /* Mark if a symbol has a definition in a dynamic object or is
896 weak in all dynamic objects. */
899 _bfd_elf_mark_dynamic_def_weak (struct elf_link_hash_entry *h,
900 asection *sec, int bind)
904 if (!bfd_is_und_section (sec))
908 /* Check if this symbol is weak in all dynamic objects. If it
909 is the first time we see it in a dynamic object, we mark
910 if it is weak. Otherwise, we clear it. */
913 if (bind == STB_WEAK)
916 else if (bind != STB_WEAK)
922 /* This function is called when we want to define a new symbol. It
923 handles the various cases which arise when we find a definition in
924 a dynamic object, or when there is already a definition in a
925 dynamic object. The new symbol is described by NAME, SYM, PSEC,
926 and PVALUE. We set SYM_HASH to the hash table entry. We set
927 OVERRIDE if the old symbol is overriding a new definition. We set
928 TYPE_CHANGE_OK if it is OK for the type to change. We set
929 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
930 change, we mean that we shouldn't warn if the type or size does
931 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
932 object is overridden by a regular object. */
935 _bfd_elf_merge_symbol (bfd *abfd,
936 struct bfd_link_info *info,
938 Elf_Internal_Sym *sym,
941 unsigned int *pold_alignment,
942 struct elf_link_hash_entry **sym_hash,
944 bfd_boolean *override,
945 bfd_boolean *type_change_ok,
946 bfd_boolean *size_change_ok)
948 asection *sec, *oldsec;
949 struct elf_link_hash_entry *h;
950 struct elf_link_hash_entry *hi;
951 struct elf_link_hash_entry *flip;
954 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
955 bfd_boolean newweak, oldweak, newfunc, oldfunc;
956 const struct elf_backend_data *bed;
962 bind = ELF_ST_BIND (sym->st_info);
964 /* Silently discard TLS symbols from --just-syms. There's no way to
965 combine a static TLS block with a new TLS block for this executable. */
966 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
967 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
973 if (! bfd_is_und_section (sec))
974 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
976 h = ((struct elf_link_hash_entry *)
977 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
982 bed = get_elf_backend_data (abfd);
984 /* This code is for coping with dynamic objects, and is only useful
985 if we are doing an ELF link. */
986 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
989 /* For merging, we only care about real symbols. But we need to make
990 sure that indirect symbol dynamic flags are updated. */
992 while (h->root.type == bfd_link_hash_indirect
993 || h->root.type == bfd_link_hash_warning)
994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
996 /* We have to check it for every instance since the first few may be
997 refereences and not all compilers emit symbol type for undefined
999 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1001 /* If we just created the symbol, mark it as being an ELF symbol.
1002 Other than that, there is nothing to do--there is no merge issue
1003 with a newly defined symbol--so we just return. */
1005 if (h->root.type == bfd_link_hash_new)
1011 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1014 switch (h->root.type)
1021 case bfd_link_hash_undefined:
1022 case bfd_link_hash_undefweak:
1023 oldbfd = h->root.u.undef.abfd;
1027 case bfd_link_hash_defined:
1028 case bfd_link_hash_defweak:
1029 oldbfd = h->root.u.def.section->owner;
1030 oldsec = h->root.u.def.section;
1033 case bfd_link_hash_common:
1034 oldbfd = h->root.u.c.p->section->owner;
1035 oldsec = h->root.u.c.p->section;
1039 /* Differentiate strong and weak symbols. */
1040 newweak = bind == STB_WEAK;
1041 oldweak = (h->root.type == bfd_link_hash_defweak
1042 || h->root.type == bfd_link_hash_undefweak);
1044 /* In cases involving weak versioned symbols, we may wind up trying
1045 to merge a symbol with itself. Catch that here, to avoid the
1046 confusion that results if we try to override a symbol with
1047 itself. The additional tests catch cases like
1048 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1049 dynamic object, which we do want to handle here. */
1051 && (newweak || oldweak)
1052 && ((abfd->flags & DYNAMIC) == 0
1053 || !h->def_regular))
1056 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1057 respectively, is from a dynamic object. */
1059 newdyn = (abfd->flags & DYNAMIC) != 0;
1063 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1064 else if (oldsec != NULL)
1066 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1067 indices used by MIPS ELF. */
1068 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1071 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1072 respectively, appear to be a definition rather than reference. */
1074 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1076 olddef = (h->root.type != bfd_link_hash_undefined
1077 && h->root.type != bfd_link_hash_undefweak
1078 && h->root.type != bfd_link_hash_common);
1080 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1081 respectively, appear to be a function. */
1083 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1084 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1086 oldfunc = (h->type != STT_NOTYPE
1087 && bed->is_function_type (h->type));
1089 /* When we try to create a default indirect symbol from the dynamic
1090 definition with the default version, we skip it if its type and
1091 the type of existing regular definition mismatch. We only do it
1092 if the existing regular definition won't be dynamic. */
1093 if (pold_alignment == NULL
1095 && !info->export_dynamic
1100 && (olddef || h->root.type == bfd_link_hash_common)
1101 && ELF_ST_TYPE (sym->st_info) != h->type
1102 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1103 && h->type != STT_NOTYPE
1104 && !(newfunc && oldfunc))
1110 /* Plugin symbol type isn't currently set. Stop bogus errors. */
1111 if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0)
1112 *type_change_ok = TRUE;
1114 /* Check TLS symbol. We don't check undefined symbol introduced by
1116 else if (oldbfd != NULL
1117 && ELF_ST_TYPE (sym->st_info) != h->type
1118 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1121 bfd_boolean ntdef, tdef;
1122 asection *ntsec, *tsec;
1124 if (h->type == STT_TLS)
1144 (*_bfd_error_handler)
1145 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1146 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1147 else if (!tdef && !ntdef)
1148 (*_bfd_error_handler)
1149 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1150 tbfd, ntbfd, h->root.root.string);
1152 (*_bfd_error_handler)
1153 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1154 tbfd, tsec, ntbfd, h->root.root.string);
1156 (*_bfd_error_handler)
1157 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1158 tbfd, ntbfd, ntsec, h->root.root.string);
1160 bfd_set_error (bfd_error_bad_value);
1164 /* We need to remember if a symbol has a definition in a dynamic
1165 object or is weak in all dynamic objects. Internal and hidden
1166 visibility will make it unavailable to dynamic objects. */
1169 _bfd_elf_mark_dynamic_def_weak (h, sec, bind);
1171 _bfd_elf_mark_dynamic_def_weak (hi, sec, bind);
1174 /* If the old symbol has non-default visibility, we ignore the new
1175 definition from a dynamic object. */
1177 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1178 && !bfd_is_und_section (sec))
1181 /* Make sure this symbol is dynamic. */
1183 hi->ref_dynamic = 1;
1184 /* A protected symbol has external availability. Make sure it is
1185 recorded as dynamic.
1187 FIXME: Should we check type and size for protected symbol? */
1188 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1189 return bfd_elf_link_record_dynamic_symbol (info, h);
1194 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1197 /* If the new symbol with non-default visibility comes from a
1198 relocatable file and the old definition comes from a dynamic
1199 object, we remove the old definition. */
1200 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1202 /* Handle the case where the old dynamic definition is
1203 default versioned. We need to copy the symbol info from
1204 the symbol with default version to the normal one if it
1205 was referenced before. */
1208 struct elf_link_hash_entry *vh = *sym_hash;
1210 vh->root.type = h->root.type;
1211 h->root.type = bfd_link_hash_indirect;
1212 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1214 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1215 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1217 /* If the new symbol is hidden or internal, completely undo
1218 any dynamic link state. */
1219 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1220 h->forced_local = 0;
1228 /* FIXME: Should we check type and size for protected symbol? */
1238 /* If the old symbol was undefined before, then it will still be
1239 on the undefs list. If the new symbol is undefined or
1240 common, we can't make it bfd_link_hash_new here, because new
1241 undefined or common symbols will be added to the undefs list
1242 by _bfd_generic_link_add_one_symbol. Symbols may not be
1243 added twice to the undefs list. Also, if the new symbol is
1244 undefweak then we don't want to lose the strong undef. */
1245 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1247 h->root.type = bfd_link_hash_undefined;
1248 h->root.u.undef.abfd = abfd;
1252 h->root.type = bfd_link_hash_new;
1253 h->root.u.undef.abfd = NULL;
1256 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1258 /* If the new symbol is hidden or internal, completely undo
1259 any dynamic link state. */
1260 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1261 h->forced_local = 0;
1268 /* FIXME: Should we check type and size for protected symbol? */
1274 if (bind == STB_GNU_UNIQUE)
1275 h->unique_global = 1;
1277 /* If a new weak symbol definition comes from a regular file and the
1278 old symbol comes from a dynamic library, we treat the new one as
1279 strong. Similarly, an old weak symbol definition from a regular
1280 file is treated as strong when the new symbol comes from a dynamic
1281 library. Further, an old weak symbol from a dynamic library is
1282 treated as strong if the new symbol is from a dynamic library.
1283 This reflects the way glibc's ld.so works.
1285 Do this before setting *type_change_ok or *size_change_ok so that
1286 we warn properly when dynamic library symbols are overridden. */
1288 if (newdef && !newdyn && olddyn)
1290 if (olddef && newdyn)
1293 /* Allow changes between different types of function symbol. */
1294 if (newfunc && oldfunc)
1295 *type_change_ok = TRUE;
1297 /* It's OK to change the type if either the existing symbol or the
1298 new symbol is weak. A type change is also OK if the old symbol
1299 is undefined and the new symbol is defined. */
1304 && h->root.type == bfd_link_hash_undefined))
1305 *type_change_ok = TRUE;
1307 /* It's OK to change the size if either the existing symbol or the
1308 new symbol is weak, or if the old symbol is undefined. */
1311 || h->root.type == bfd_link_hash_undefined)
1312 *size_change_ok = TRUE;
1314 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1315 symbol, respectively, appears to be a common symbol in a dynamic
1316 object. If a symbol appears in an uninitialized section, and is
1317 not weak, and is not a function, then it may be a common symbol
1318 which was resolved when the dynamic object was created. We want
1319 to treat such symbols specially, because they raise special
1320 considerations when setting the symbol size: if the symbol
1321 appears as a common symbol in a regular object, and the size in
1322 the regular object is larger, we must make sure that we use the
1323 larger size. This problematic case can always be avoided in C,
1324 but it must be handled correctly when using Fortran shared
1327 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1328 likewise for OLDDYNCOMMON and OLDDEF.
1330 Note that this test is just a heuristic, and that it is quite
1331 possible to have an uninitialized symbol in a shared object which
1332 is really a definition, rather than a common symbol. This could
1333 lead to some minor confusion when the symbol really is a common
1334 symbol in some regular object. However, I think it will be
1340 && (sec->flags & SEC_ALLOC) != 0
1341 && (sec->flags & SEC_LOAD) == 0
1344 newdyncommon = TRUE;
1346 newdyncommon = FALSE;
1350 && h->root.type == bfd_link_hash_defined
1352 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1353 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1356 olddyncommon = TRUE;
1358 olddyncommon = FALSE;
1360 /* We now know everything about the old and new symbols. We ask the
1361 backend to check if we can merge them. */
1362 if (bed->merge_symbol
1363 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1364 pold_alignment, skip, override,
1365 type_change_ok, size_change_ok,
1366 &newdyn, &newdef, &newdyncommon, &newweak,
1368 &olddyn, &olddef, &olddyncommon, &oldweak,
1372 /* If both the old and the new symbols look like common symbols in a
1373 dynamic object, set the size of the symbol to the larger of the
1378 && sym->st_size != h->size)
1380 /* Since we think we have two common symbols, issue a multiple
1381 common warning if desired. Note that we only warn if the
1382 size is different. If the size is the same, we simply let
1383 the old symbol override the new one as normally happens with
1384 symbols defined in dynamic objects. */
1386 if (! ((*info->callbacks->multiple_common)
1387 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1390 if (sym->st_size > h->size)
1391 h->size = sym->st_size;
1393 *size_change_ok = TRUE;
1396 /* If we are looking at a dynamic object, and we have found a
1397 definition, we need to see if the symbol was already defined by
1398 some other object. If so, we want to use the existing
1399 definition, and we do not want to report a multiple symbol
1400 definition error; we do this by clobbering *PSEC to be
1401 bfd_und_section_ptr.
1403 We treat a common symbol as a definition if the symbol in the
1404 shared library is a function, since common symbols always
1405 represent variables; this can cause confusion in principle, but
1406 any such confusion would seem to indicate an erroneous program or
1407 shared library. We also permit a common symbol in a regular
1408 object to override a weak symbol in a shared object. */
1413 || (h->root.type == bfd_link_hash_common
1414 && (newweak || newfunc))))
1418 newdyncommon = FALSE;
1420 *psec = sec = bfd_und_section_ptr;
1421 *size_change_ok = TRUE;
1423 /* If we get here when the old symbol is a common symbol, then
1424 we are explicitly letting it override a weak symbol or
1425 function in a dynamic object, and we don't want to warn about
1426 a type change. If the old symbol is a defined symbol, a type
1427 change warning may still be appropriate. */
1429 if (h->root.type == bfd_link_hash_common)
1430 *type_change_ok = TRUE;
1433 /* Handle the special case of an old common symbol merging with a
1434 new symbol which looks like a common symbol in a shared object.
1435 We change *PSEC and *PVALUE to make the new symbol look like a
1436 common symbol, and let _bfd_generic_link_add_one_symbol do the
1440 && h->root.type == bfd_link_hash_common)
1444 newdyncommon = FALSE;
1445 *pvalue = sym->st_size;
1446 *psec = sec = bed->common_section (oldsec);
1447 *size_change_ok = TRUE;
1450 /* Skip weak definitions of symbols that are already defined. */
1451 if (newdef && olddef && newweak)
1453 /* Don't skip new non-IR weak syms. */
1454 if (!(oldbfd != NULL
1455 && (oldbfd->flags & BFD_PLUGIN) != 0
1456 && (abfd->flags & BFD_PLUGIN) == 0))
1459 /* Merge st_other. If the symbol already has a dynamic index,
1460 but visibility says it should not be visible, turn it into a
1462 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1463 if (h->dynindx != -1)
1464 switch (ELF_ST_VISIBILITY (h->other))
1468 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1473 /* If the old symbol is from a dynamic object, and the new symbol is
1474 a definition which is not from a dynamic object, then the new
1475 symbol overrides the old symbol. Symbols from regular files
1476 always take precedence over symbols from dynamic objects, even if
1477 they are defined after the dynamic object in the link.
1479 As above, we again permit a common symbol in a regular object to
1480 override a definition in a shared object if the shared object
1481 symbol is a function or is weak. */
1486 || (bfd_is_com_section (sec)
1487 && (oldweak || oldfunc)))
1492 /* Change the hash table entry to undefined, and let
1493 _bfd_generic_link_add_one_symbol do the right thing with the
1496 h->root.type = bfd_link_hash_undefined;
1497 h->root.u.undef.abfd = h->root.u.def.section->owner;
1498 *size_change_ok = TRUE;
1501 olddyncommon = FALSE;
1503 /* We again permit a type change when a common symbol may be
1504 overriding a function. */
1506 if (bfd_is_com_section (sec))
1510 /* If a common symbol overrides a function, make sure
1511 that it isn't defined dynamically nor has type
1514 h->type = STT_NOTYPE;
1516 *type_change_ok = TRUE;
1519 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1522 /* This union may have been set to be non-NULL when this symbol
1523 was seen in a dynamic object. We must force the union to be
1524 NULL, so that it is correct for a regular symbol. */
1525 h->verinfo.vertree = NULL;
1528 /* Handle the special case of a new common symbol merging with an
1529 old symbol that looks like it might be a common symbol defined in
1530 a shared object. Note that we have already handled the case in
1531 which a new common symbol should simply override the definition
1532 in the shared library. */
1535 && bfd_is_com_section (sec)
1538 /* It would be best if we could set the hash table entry to a
1539 common symbol, but we don't know what to use for the section
1540 or the alignment. */
1541 if (! ((*info->callbacks->multiple_common)
1542 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1545 /* If the presumed common symbol in the dynamic object is
1546 larger, pretend that the new symbol has its size. */
1548 if (h->size > *pvalue)
1551 /* We need to remember the alignment required by the symbol
1552 in the dynamic object. */
1553 BFD_ASSERT (pold_alignment);
1554 *pold_alignment = h->root.u.def.section->alignment_power;
1557 olddyncommon = FALSE;
1559 h->root.type = bfd_link_hash_undefined;
1560 h->root.u.undef.abfd = h->root.u.def.section->owner;
1562 *size_change_ok = TRUE;
1563 *type_change_ok = TRUE;
1565 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1568 h->verinfo.vertree = NULL;
1573 /* Handle the case where we had a versioned symbol in a dynamic
1574 library and now find a definition in a normal object. In this
1575 case, we make the versioned symbol point to the normal one. */
1576 flip->root.type = h->root.type;
1577 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1578 h->root.type = bfd_link_hash_indirect;
1579 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1580 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1584 flip->ref_dynamic = 1;
1591 /* This function is called to create an indirect symbol from the
1592 default for the symbol with the default version if needed. The
1593 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1594 set DYNSYM if the new indirect symbol is dynamic. */
1597 _bfd_elf_add_default_symbol (bfd *abfd,
1598 struct bfd_link_info *info,
1599 struct elf_link_hash_entry *h,
1601 Elf_Internal_Sym *sym,
1604 bfd_boolean *dynsym,
1605 bfd_boolean override)
1607 bfd_boolean type_change_ok;
1608 bfd_boolean size_change_ok;
1611 struct elf_link_hash_entry *hi;
1612 struct bfd_link_hash_entry *bh;
1613 const struct elf_backend_data *bed;
1614 bfd_boolean collect;
1615 bfd_boolean dynamic;
1617 size_t len, shortlen;
1620 /* If this symbol has a version, and it is the default version, we
1621 create an indirect symbol from the default name to the fully
1622 decorated name. This will cause external references which do not
1623 specify a version to be bound to this version of the symbol. */
1624 p = strchr (name, ELF_VER_CHR);
1625 if (p == NULL || p[1] != ELF_VER_CHR)
1630 /* We are overridden by an old definition. We need to check if we
1631 need to create the indirect symbol from the default name. */
1632 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1634 BFD_ASSERT (hi != NULL);
1637 while (hi->root.type == bfd_link_hash_indirect
1638 || hi->root.type == bfd_link_hash_warning)
1640 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1646 bed = get_elf_backend_data (abfd);
1647 collect = bed->collect;
1648 dynamic = (abfd->flags & DYNAMIC) != 0;
1650 shortlen = p - name;
1651 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1652 if (shortname == NULL)
1654 memcpy (shortname, name, shortlen);
1655 shortname[shortlen] = '\0';
1657 /* We are going to create a new symbol. Merge it with any existing
1658 symbol with this name. For the purposes of the merge, act as
1659 though we were defining the symbol we just defined, although we
1660 actually going to define an indirect symbol. */
1661 type_change_ok = FALSE;
1662 size_change_ok = FALSE;
1664 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1665 NULL, &hi, &skip, &override,
1666 &type_change_ok, &size_change_ok))
1675 if (! (_bfd_generic_link_add_one_symbol
1676 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1677 0, name, FALSE, collect, &bh)))
1679 hi = (struct elf_link_hash_entry *) bh;
1683 /* In this case the symbol named SHORTNAME is overriding the
1684 indirect symbol we want to add. We were planning on making
1685 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1686 is the name without a version. NAME is the fully versioned
1687 name, and it is the default version.
1689 Overriding means that we already saw a definition for the
1690 symbol SHORTNAME in a regular object, and it is overriding
1691 the symbol defined in the dynamic object.
1693 When this happens, we actually want to change NAME, the
1694 symbol we just added, to refer to SHORTNAME. This will cause
1695 references to NAME in the shared object to become references
1696 to SHORTNAME in the regular object. This is what we expect
1697 when we override a function in a shared object: that the
1698 references in the shared object will be mapped to the
1699 definition in the regular object. */
1701 while (hi->root.type == bfd_link_hash_indirect
1702 || hi->root.type == bfd_link_hash_warning)
1703 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1705 h->root.type = bfd_link_hash_indirect;
1706 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1710 hi->ref_dynamic = 1;
1714 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1719 /* Now set HI to H, so that the following code will set the
1720 other fields correctly. */
1724 /* Check if HI is a warning symbol. */
1725 if (hi->root.type == bfd_link_hash_warning)
1726 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1728 /* If there is a duplicate definition somewhere, then HI may not
1729 point to an indirect symbol. We will have reported an error to
1730 the user in that case. */
1732 if (hi->root.type == bfd_link_hash_indirect)
1734 struct elf_link_hash_entry *ht;
1736 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1737 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1739 /* See if the new flags lead us to realize that the symbol must
1745 if (! info->executable
1752 if (hi->ref_regular)
1758 /* We also need to define an indirection from the nondefault version
1762 len = strlen (name);
1763 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1764 if (shortname == NULL)
1766 memcpy (shortname, name, shortlen);
1767 memcpy (shortname + shortlen, p + 1, len - shortlen);
1769 /* Once again, merge with any existing symbol. */
1770 type_change_ok = FALSE;
1771 size_change_ok = FALSE;
1773 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1774 NULL, &hi, &skip, &override,
1775 &type_change_ok, &size_change_ok))
1783 /* Here SHORTNAME is a versioned name, so we don't expect to see
1784 the type of override we do in the case above unless it is
1785 overridden by a versioned definition. */
1786 if (hi->root.type != bfd_link_hash_defined
1787 && hi->root.type != bfd_link_hash_defweak)
1788 (*_bfd_error_handler)
1789 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1795 if (! (_bfd_generic_link_add_one_symbol
1796 (info, abfd, shortname, BSF_INDIRECT,
1797 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1799 hi = (struct elf_link_hash_entry *) bh;
1801 /* If there is a duplicate definition somewhere, then HI may not
1802 point to an indirect symbol. We will have reported an error
1803 to the user in that case. */
1805 if (hi->root.type == bfd_link_hash_indirect)
1807 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1809 /* See if the new flags lead us to realize that the symbol
1815 if (! info->executable
1821 if (hi->ref_regular)
1831 /* This routine is used to export all defined symbols into the dynamic
1832 symbol table. It is called via elf_link_hash_traverse. */
1835 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1837 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1839 /* Ignore indirect symbols. These are added by the versioning code. */
1840 if (h->root.type == bfd_link_hash_indirect)
1843 /* Ignore this if we won't export it. */
1844 if (!eif->info->export_dynamic && !h->dynamic)
1847 if (h->dynindx == -1
1848 && (h->def_regular || h->ref_regular)
1849 && ! bfd_hide_sym_by_version (eif->info->version_info,
1850 h->root.root.string))
1852 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1862 /* Look through the symbols which are defined in other shared
1863 libraries and referenced here. Update the list of version
1864 dependencies. This will be put into the .gnu.version_r section.
1865 This function is called via elf_link_hash_traverse. */
1868 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1871 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1872 Elf_Internal_Verneed *t;
1873 Elf_Internal_Vernaux *a;
1876 /* We only care about symbols defined in shared objects with version
1881 || h->verinfo.verdef == NULL)
1884 /* See if we already know about this version. */
1885 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1889 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1892 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1893 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1899 /* This is a new version. Add it to tree we are building. */
1904 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1907 rinfo->failed = TRUE;
1911 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1912 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1913 elf_tdata (rinfo->info->output_bfd)->verref = t;
1917 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1920 rinfo->failed = TRUE;
1924 /* Note that we are copying a string pointer here, and testing it
1925 above. If bfd_elf_string_from_elf_section is ever changed to
1926 discard the string data when low in memory, this will have to be
1928 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1930 a->vna_flags = h->verinfo.verdef->vd_flags;
1931 a->vna_nextptr = t->vn_auxptr;
1933 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1936 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1943 /* Figure out appropriate versions for all the symbols. We may not
1944 have the version number script until we have read all of the input
1945 files, so until that point we don't know which symbols should be
1946 local. This function is called via elf_link_hash_traverse. */
1949 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1951 struct elf_info_failed *sinfo;
1952 struct bfd_link_info *info;
1953 const struct elf_backend_data *bed;
1954 struct elf_info_failed eif;
1958 sinfo = (struct elf_info_failed *) data;
1961 /* Fix the symbol flags. */
1964 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1967 sinfo->failed = TRUE;
1971 /* We only need version numbers for symbols defined in regular
1973 if (!h->def_regular)
1976 bed = get_elf_backend_data (info->output_bfd);
1977 p = strchr (h->root.root.string, ELF_VER_CHR);
1978 if (p != NULL && h->verinfo.vertree == NULL)
1980 struct bfd_elf_version_tree *t;
1985 /* There are two consecutive ELF_VER_CHR characters if this is
1986 not a hidden symbol. */
1988 if (*p == ELF_VER_CHR)
1994 /* If there is no version string, we can just return out. */
2002 /* Look for the version. If we find it, it is no longer weak. */
2003 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2005 if (strcmp (t->name, p) == 0)
2009 struct bfd_elf_version_expr *d;
2011 len = p - h->root.root.string;
2012 alc = (char *) bfd_malloc (len);
2015 sinfo->failed = TRUE;
2018 memcpy (alc, h->root.root.string, len - 1);
2019 alc[len - 1] = '\0';
2020 if (alc[len - 2] == ELF_VER_CHR)
2021 alc[len - 2] = '\0';
2023 h->verinfo.vertree = t;
2027 if (t->globals.list != NULL)
2028 d = (*t->match) (&t->globals, NULL, alc);
2030 /* See if there is anything to force this symbol to
2032 if (d == NULL && t->locals.list != NULL)
2034 d = (*t->match) (&t->locals, NULL, alc);
2037 && ! info->export_dynamic)
2038 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2046 /* If we are building an application, we need to create a
2047 version node for this version. */
2048 if (t == NULL && info->executable)
2050 struct bfd_elf_version_tree **pp;
2053 /* If we aren't going to export this symbol, we don't need
2054 to worry about it. */
2055 if (h->dynindx == -1)
2059 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2062 sinfo->failed = TRUE;
2067 t->name_indx = (unsigned int) -1;
2071 /* Don't count anonymous version tag. */
2072 if (sinfo->info->version_info != NULL
2073 && sinfo->info->version_info->vernum == 0)
2075 for (pp = &sinfo->info->version_info;
2079 t->vernum = version_index;
2083 h->verinfo.vertree = t;
2087 /* We could not find the version for a symbol when
2088 generating a shared archive. Return an error. */
2089 (*_bfd_error_handler)
2090 (_("%B: version node not found for symbol %s"),
2091 info->output_bfd, h->root.root.string);
2092 bfd_set_error (bfd_error_bad_value);
2093 sinfo->failed = TRUE;
2101 /* If we don't have a version for this symbol, see if we can find
2103 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2108 = bfd_find_version_for_sym (sinfo->info->version_info,
2109 h->root.root.string, &hide);
2110 if (h->verinfo.vertree != NULL && hide)
2111 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2117 /* Read and swap the relocs from the section indicated by SHDR. This
2118 may be either a REL or a RELA section. The relocations are
2119 translated into RELA relocations and stored in INTERNAL_RELOCS,
2120 which should have already been allocated to contain enough space.
2121 The EXTERNAL_RELOCS are a buffer where the external form of the
2122 relocations should be stored.
2124 Returns FALSE if something goes wrong. */
2127 elf_link_read_relocs_from_section (bfd *abfd,
2129 Elf_Internal_Shdr *shdr,
2130 void *external_relocs,
2131 Elf_Internal_Rela *internal_relocs)
2133 const struct elf_backend_data *bed;
2134 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2135 const bfd_byte *erela;
2136 const bfd_byte *erelaend;
2137 Elf_Internal_Rela *irela;
2138 Elf_Internal_Shdr *symtab_hdr;
2141 /* Position ourselves at the start of the section. */
2142 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2145 /* Read the relocations. */
2146 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2149 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2150 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2152 bed = get_elf_backend_data (abfd);
2154 /* Convert the external relocations to the internal format. */
2155 if (shdr->sh_entsize == bed->s->sizeof_rel)
2156 swap_in = bed->s->swap_reloc_in;
2157 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2158 swap_in = bed->s->swap_reloca_in;
2161 bfd_set_error (bfd_error_wrong_format);
2165 erela = (const bfd_byte *) external_relocs;
2166 erelaend = erela + shdr->sh_size;
2167 irela = internal_relocs;
2168 while (erela < erelaend)
2172 (*swap_in) (abfd, erela, irela);
2173 r_symndx = ELF32_R_SYM (irela->r_info);
2174 if (bed->s->arch_size == 64)
2178 if ((size_t) r_symndx >= nsyms)
2180 (*_bfd_error_handler)
2181 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2182 " for offset 0x%lx in section `%A'"),
2184 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2185 bfd_set_error (bfd_error_bad_value);
2189 else if (r_symndx != STN_UNDEF)
2191 (*_bfd_error_handler)
2192 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2193 " when the object file has no symbol table"),
2195 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2196 bfd_set_error (bfd_error_bad_value);
2199 irela += bed->s->int_rels_per_ext_rel;
2200 erela += shdr->sh_entsize;
2206 /* Read and swap the relocs for a section O. They may have been
2207 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2208 not NULL, they are used as buffers to read into. They are known to
2209 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2210 the return value is allocated using either malloc or bfd_alloc,
2211 according to the KEEP_MEMORY argument. If O has two relocation
2212 sections (both REL and RELA relocations), then the REL_HDR
2213 relocations will appear first in INTERNAL_RELOCS, followed by the
2214 RELA_HDR relocations. */
2217 _bfd_elf_link_read_relocs (bfd *abfd,
2219 void *external_relocs,
2220 Elf_Internal_Rela *internal_relocs,
2221 bfd_boolean keep_memory)
2223 void *alloc1 = NULL;
2224 Elf_Internal_Rela *alloc2 = NULL;
2225 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2226 struct bfd_elf_section_data *esdo = elf_section_data (o);
2227 Elf_Internal_Rela *internal_rela_relocs;
2229 if (esdo->relocs != NULL)
2230 return esdo->relocs;
2232 if (o->reloc_count == 0)
2235 if (internal_relocs == NULL)
2239 size = o->reloc_count;
2240 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2242 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2244 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2245 if (internal_relocs == NULL)
2249 if (external_relocs == NULL)
2251 bfd_size_type size = 0;
2254 size += esdo->rel.hdr->sh_size;
2256 size += esdo->rela.hdr->sh_size;
2258 alloc1 = bfd_malloc (size);
2261 external_relocs = alloc1;
2264 internal_rela_relocs = internal_relocs;
2267 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2271 external_relocs = (((bfd_byte *) external_relocs)
2272 + esdo->rel.hdr->sh_size);
2273 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2274 * bed->s->int_rels_per_ext_rel);
2278 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2280 internal_rela_relocs)))
2283 /* Cache the results for next time, if we can. */
2285 esdo->relocs = internal_relocs;
2290 /* Don't free alloc2, since if it was allocated we are passing it
2291 back (under the name of internal_relocs). */
2293 return internal_relocs;
2301 bfd_release (abfd, alloc2);
2308 /* Compute the size of, and allocate space for, REL_HDR which is the
2309 section header for a section containing relocations for O. */
2312 _bfd_elf_link_size_reloc_section (bfd *abfd,
2313 struct bfd_elf_section_reloc_data *reldata)
2315 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2317 /* That allows us to calculate the size of the section. */
2318 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2320 /* The contents field must last into write_object_contents, so we
2321 allocate it with bfd_alloc rather than malloc. Also since we
2322 cannot be sure that the contents will actually be filled in,
2323 we zero the allocated space. */
2324 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2325 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2328 if (reldata->hashes == NULL && reldata->count)
2330 struct elf_link_hash_entry **p;
2332 p = (struct elf_link_hash_entry **)
2333 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
2337 reldata->hashes = p;
2343 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2344 originated from the section given by INPUT_REL_HDR) to the
2348 _bfd_elf_link_output_relocs (bfd *output_bfd,
2349 asection *input_section,
2350 Elf_Internal_Shdr *input_rel_hdr,
2351 Elf_Internal_Rela *internal_relocs,
2352 struct elf_link_hash_entry **rel_hash
2355 Elf_Internal_Rela *irela;
2356 Elf_Internal_Rela *irelaend;
2358 struct bfd_elf_section_reloc_data *output_reldata;
2359 asection *output_section;
2360 const struct elf_backend_data *bed;
2361 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2362 struct bfd_elf_section_data *esdo;
2364 output_section = input_section->output_section;
2366 bed = get_elf_backend_data (output_bfd);
2367 esdo = elf_section_data (output_section);
2368 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2370 output_reldata = &esdo->rel;
2371 swap_out = bed->s->swap_reloc_out;
2373 else if (esdo->rela.hdr
2374 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2376 output_reldata = &esdo->rela;
2377 swap_out = bed->s->swap_reloca_out;
2381 (*_bfd_error_handler)
2382 (_("%B: relocation size mismatch in %B section %A"),
2383 output_bfd, input_section->owner, input_section);
2384 bfd_set_error (bfd_error_wrong_format);
2388 erel = output_reldata->hdr->contents;
2389 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2390 irela = internal_relocs;
2391 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2392 * bed->s->int_rels_per_ext_rel);
2393 while (irela < irelaend)
2395 (*swap_out) (output_bfd, irela, erel);
2396 irela += bed->s->int_rels_per_ext_rel;
2397 erel += input_rel_hdr->sh_entsize;
2400 /* Bump the counter, so that we know where to add the next set of
2402 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2407 /* Make weak undefined symbols in PIE dynamic. */
2410 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2411 struct elf_link_hash_entry *h)
2415 && h->root.type == bfd_link_hash_undefweak)
2416 return bfd_elf_link_record_dynamic_symbol (info, h);
2421 /* Fix up the flags for a symbol. This handles various cases which
2422 can only be fixed after all the input files are seen. This is
2423 currently called by both adjust_dynamic_symbol and
2424 assign_sym_version, which is unnecessary but perhaps more robust in
2425 the face of future changes. */
2428 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2429 struct elf_info_failed *eif)
2431 const struct elf_backend_data *bed;
2433 /* If this symbol was mentioned in a non-ELF file, try to set
2434 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2435 permit a non-ELF file to correctly refer to a symbol defined in
2436 an ELF dynamic object. */
2439 while (h->root.type == bfd_link_hash_indirect)
2440 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2442 if (h->root.type != bfd_link_hash_defined
2443 && h->root.type != bfd_link_hash_defweak)
2446 h->ref_regular_nonweak = 1;
2450 if (h->root.u.def.section->owner != NULL
2451 && (bfd_get_flavour (h->root.u.def.section->owner)
2452 == bfd_target_elf_flavour))
2455 h->ref_regular_nonweak = 1;
2461 if (h->dynindx == -1
2465 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2474 /* Unfortunately, NON_ELF is only correct if the symbol
2475 was first seen in a non-ELF file. Fortunately, if the symbol
2476 was first seen in an ELF file, we're probably OK unless the
2477 symbol was defined in a non-ELF file. Catch that case here.
2478 FIXME: We're still in trouble if the symbol was first seen in
2479 a dynamic object, and then later in a non-ELF regular object. */
2480 if ((h->root.type == bfd_link_hash_defined
2481 || h->root.type == bfd_link_hash_defweak)
2483 && (h->root.u.def.section->owner != NULL
2484 ? (bfd_get_flavour (h->root.u.def.section->owner)
2485 != bfd_target_elf_flavour)
2486 : (bfd_is_abs_section (h->root.u.def.section)
2487 && !h->def_dynamic)))
2491 /* Backend specific symbol fixup. */
2492 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2493 if (bed->elf_backend_fixup_symbol
2494 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2497 /* If this is a final link, and the symbol was defined as a common
2498 symbol in a regular object file, and there was no definition in
2499 any dynamic object, then the linker will have allocated space for
2500 the symbol in a common section but the DEF_REGULAR
2501 flag will not have been set. */
2502 if (h->root.type == bfd_link_hash_defined
2506 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2509 /* If -Bsymbolic was used (which means to bind references to global
2510 symbols to the definition within the shared object), and this
2511 symbol was defined in a regular object, then it actually doesn't
2512 need a PLT entry. Likewise, if the symbol has non-default
2513 visibility. If the symbol has hidden or internal visibility, we
2514 will force it local. */
2516 && eif->info->shared
2517 && is_elf_hash_table (eif->info->hash)
2518 && (SYMBOLIC_BIND (eif->info, h)
2519 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2522 bfd_boolean force_local;
2524 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2525 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2526 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2529 /* If a weak undefined symbol has non-default visibility, we also
2530 hide it from the dynamic linker. */
2531 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2532 && h->root.type == bfd_link_hash_undefweak)
2533 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2535 /* If this is a weak defined symbol in a dynamic object, and we know
2536 the real definition in the dynamic object, copy interesting flags
2537 over to the real definition. */
2538 if (h->u.weakdef != NULL)
2540 /* If the real definition is defined by a regular object file,
2541 don't do anything special. See the longer description in
2542 _bfd_elf_adjust_dynamic_symbol, below. */
2543 if (h->u.weakdef->def_regular)
2544 h->u.weakdef = NULL;
2547 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2549 while (h->root.type == bfd_link_hash_indirect)
2550 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2552 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2553 || h->root.type == bfd_link_hash_defweak);
2554 BFD_ASSERT (weakdef->def_dynamic);
2555 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2556 || weakdef->root.type == bfd_link_hash_defweak);
2557 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2564 /* Make the backend pick a good value for a dynamic symbol. This is
2565 called via elf_link_hash_traverse, and also calls itself
2569 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2571 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2573 const struct elf_backend_data *bed;
2575 if (! is_elf_hash_table (eif->info->hash))
2578 /* Ignore indirect symbols. These are added by the versioning code. */
2579 if (h->root.type == bfd_link_hash_indirect)
2582 /* Fix the symbol flags. */
2583 if (! _bfd_elf_fix_symbol_flags (h, eif))
2586 /* If this symbol does not require a PLT entry, and it is not
2587 defined by a dynamic object, or is not referenced by a regular
2588 object, ignore it. We do have to handle a weak defined symbol,
2589 even if no regular object refers to it, if we decided to add it
2590 to the dynamic symbol table. FIXME: Do we normally need to worry
2591 about symbols which are defined by one dynamic object and
2592 referenced by another one? */
2594 && h->type != STT_GNU_IFUNC
2598 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2600 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2604 /* If we've already adjusted this symbol, don't do it again. This
2605 can happen via a recursive call. */
2606 if (h->dynamic_adjusted)
2609 /* Don't look at this symbol again. Note that we must set this
2610 after checking the above conditions, because we may look at a
2611 symbol once, decide not to do anything, and then get called
2612 recursively later after REF_REGULAR is set below. */
2613 h->dynamic_adjusted = 1;
2615 /* If this is a weak definition, and we know a real definition, and
2616 the real symbol is not itself defined by a regular object file,
2617 then get a good value for the real definition. We handle the
2618 real symbol first, for the convenience of the backend routine.
2620 Note that there is a confusing case here. If the real definition
2621 is defined by a regular object file, we don't get the real symbol
2622 from the dynamic object, but we do get the weak symbol. If the
2623 processor backend uses a COPY reloc, then if some routine in the
2624 dynamic object changes the real symbol, we will not see that
2625 change in the corresponding weak symbol. This is the way other
2626 ELF linkers work as well, and seems to be a result of the shared
2629 I will clarify this issue. Most SVR4 shared libraries define the
2630 variable _timezone and define timezone as a weak synonym. The
2631 tzset call changes _timezone. If you write
2632 extern int timezone;
2634 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2635 you might expect that, since timezone is a synonym for _timezone,
2636 the same number will print both times. However, if the processor
2637 backend uses a COPY reloc, then actually timezone will be copied
2638 into your process image, and, since you define _timezone
2639 yourself, _timezone will not. Thus timezone and _timezone will
2640 wind up at different memory locations. The tzset call will set
2641 _timezone, leaving timezone unchanged. */
2643 if (h->u.weakdef != NULL)
2645 /* If we get to this point, there is an implicit reference to
2646 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2647 h->u.weakdef->ref_regular = 1;
2649 /* Ensure that the backend adjust_dynamic_symbol function sees
2650 H->U.WEAKDEF before H by recursively calling ourselves. */
2651 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2655 /* If a symbol has no type and no size and does not require a PLT
2656 entry, then we are probably about to do the wrong thing here: we
2657 are probably going to create a COPY reloc for an empty object.
2658 This case can arise when a shared object is built with assembly
2659 code, and the assembly code fails to set the symbol type. */
2661 && h->type == STT_NOTYPE
2663 (*_bfd_error_handler)
2664 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2665 h->root.root.string);
2667 dynobj = elf_hash_table (eif->info)->dynobj;
2668 bed = get_elf_backend_data (dynobj);
2670 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2679 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2683 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2686 unsigned int power_of_two;
2688 asection *sec = h->root.u.def.section;
2690 /* The section aligment of definition is the maximum alignment
2691 requirement of symbols defined in the section. Since we don't
2692 know the symbol alignment requirement, we start with the
2693 maximum alignment and check low bits of the symbol address
2694 for the minimum alignment. */
2695 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2696 mask = ((bfd_vma) 1 << power_of_two) - 1;
2697 while ((h->root.u.def.value & mask) != 0)
2703 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2706 /* Adjust the section alignment if needed. */
2707 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2712 /* We make sure that the symbol will be aligned properly. */
2713 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2715 /* Define the symbol as being at this point in DYNBSS. */
2716 h->root.u.def.section = dynbss;
2717 h->root.u.def.value = dynbss->size;
2719 /* Increment the size of DYNBSS to make room for the symbol. */
2720 dynbss->size += h->size;
2725 /* Adjust all external symbols pointing into SEC_MERGE sections
2726 to reflect the object merging within the sections. */
2729 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2733 if ((h->root.type == bfd_link_hash_defined
2734 || h->root.type == bfd_link_hash_defweak)
2735 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2736 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2738 bfd *output_bfd = (bfd *) data;
2740 h->root.u.def.value =
2741 _bfd_merged_section_offset (output_bfd,
2742 &h->root.u.def.section,
2743 elf_section_data (sec)->sec_info,
2744 h->root.u.def.value);
2750 /* Returns false if the symbol referred to by H should be considered
2751 to resolve local to the current module, and true if it should be
2752 considered to bind dynamically. */
2755 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2756 struct bfd_link_info *info,
2757 bfd_boolean not_local_protected)
2759 bfd_boolean binding_stays_local_p;
2760 const struct elf_backend_data *bed;
2761 struct elf_link_hash_table *hash_table;
2766 while (h->root.type == bfd_link_hash_indirect
2767 || h->root.type == bfd_link_hash_warning)
2768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2770 /* If it was forced local, then clearly it's not dynamic. */
2771 if (h->dynindx == -1)
2773 if (h->forced_local)
2776 /* Identify the cases where name binding rules say that a
2777 visible symbol resolves locally. */
2778 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2780 switch (ELF_ST_VISIBILITY (h->other))
2787 hash_table = elf_hash_table (info);
2788 if (!is_elf_hash_table (hash_table))
2791 bed = get_elf_backend_data (hash_table->dynobj);
2793 /* Proper resolution for function pointer equality may require
2794 that these symbols perhaps be resolved dynamically, even though
2795 we should be resolving them to the current module. */
2796 if (!not_local_protected || !bed->is_function_type (h->type))
2797 binding_stays_local_p = TRUE;
2804 /* If it isn't defined locally, then clearly it's dynamic. */
2805 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2808 /* Otherwise, the symbol is dynamic if binding rules don't tell
2809 us that it remains local. */
2810 return !binding_stays_local_p;
2813 /* Return true if the symbol referred to by H should be considered
2814 to resolve local to the current module, and false otherwise. Differs
2815 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2816 undefined symbols. The two functions are virtually identical except
2817 for the place where forced_local and dynindx == -1 are tested. If
2818 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2819 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2820 the symbol is local only for defined symbols.
2821 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2822 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2823 treatment of undefined weak symbols. For those that do not make
2824 undefined weak symbols dynamic, both functions may return false. */
2827 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2828 struct bfd_link_info *info,
2829 bfd_boolean local_protected)
2831 const struct elf_backend_data *bed;
2832 struct elf_link_hash_table *hash_table;
2834 /* If it's a local sym, of course we resolve locally. */
2838 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2839 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2840 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2843 /* Common symbols that become definitions don't get the DEF_REGULAR
2844 flag set, so test it first, and don't bail out. */
2845 if (ELF_COMMON_DEF_P (h))
2847 /* If we don't have a definition in a regular file, then we can't
2848 resolve locally. The sym is either undefined or dynamic. */
2849 else if (!h->def_regular)
2852 /* Forced local symbols resolve locally. */
2853 if (h->forced_local)
2856 /* As do non-dynamic symbols. */
2857 if (h->dynindx == -1)
2860 /* At this point, we know the symbol is defined and dynamic. In an
2861 executable it must resolve locally, likewise when building symbolic
2862 shared libraries. */
2863 if (info->executable || SYMBOLIC_BIND (info, h))
2866 /* Now deal with defined dynamic symbols in shared libraries. Ones
2867 with default visibility might not resolve locally. */
2868 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2871 hash_table = elf_hash_table (info);
2872 if (!is_elf_hash_table (hash_table))
2875 bed = get_elf_backend_data (hash_table->dynobj);
2877 /* STV_PROTECTED non-function symbols are local. */
2878 if (!bed->is_function_type (h->type))
2881 /* Function pointer equality tests may require that STV_PROTECTED
2882 symbols be treated as dynamic symbols. If the address of a
2883 function not defined in an executable is set to that function's
2884 plt entry in the executable, then the address of the function in
2885 a shared library must also be the plt entry in the executable. */
2886 return local_protected;
2889 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2890 aligned. Returns the first TLS output section. */
2892 struct bfd_section *
2893 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2895 struct bfd_section *sec, *tls;
2896 unsigned int align = 0;
2898 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2899 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2903 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2904 if (sec->alignment_power > align)
2905 align = sec->alignment_power;
2907 elf_hash_table (info)->tls_sec = tls;
2909 /* Ensure the alignment of the first section is the largest alignment,
2910 so that the tls segment starts aligned. */
2912 tls->alignment_power = align;
2917 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2919 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2920 Elf_Internal_Sym *sym)
2922 const struct elf_backend_data *bed;
2924 /* Local symbols do not count, but target specific ones might. */
2925 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2926 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2929 bed = get_elf_backend_data (abfd);
2930 /* Function symbols do not count. */
2931 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2934 /* If the section is undefined, then so is the symbol. */
2935 if (sym->st_shndx == SHN_UNDEF)
2938 /* If the symbol is defined in the common section, then
2939 it is a common definition and so does not count. */
2940 if (bed->common_definition (sym))
2943 /* If the symbol is in a target specific section then we
2944 must rely upon the backend to tell us what it is. */
2945 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2946 /* FIXME - this function is not coded yet:
2948 return _bfd_is_global_symbol_definition (abfd, sym);
2950 Instead for now assume that the definition is not global,
2951 Even if this is wrong, at least the linker will behave
2952 in the same way that it used to do. */
2958 /* Search the symbol table of the archive element of the archive ABFD
2959 whose archive map contains a mention of SYMDEF, and determine if
2960 the symbol is defined in this element. */
2962 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2964 Elf_Internal_Shdr * hdr;
2965 bfd_size_type symcount;
2966 bfd_size_type extsymcount;
2967 bfd_size_type extsymoff;
2968 Elf_Internal_Sym *isymbuf;
2969 Elf_Internal_Sym *isym;
2970 Elf_Internal_Sym *isymend;
2973 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2977 if (! bfd_check_format (abfd, bfd_object))
2980 /* If we have already included the element containing this symbol in the
2981 link then we do not need to include it again. Just claim that any symbol
2982 it contains is not a definition, so that our caller will not decide to
2983 (re)include this element. */
2984 if (abfd->archive_pass)
2987 /* Select the appropriate symbol table. */
2988 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2989 hdr = &elf_tdata (abfd)->symtab_hdr;
2991 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2993 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2995 /* The sh_info field of the symtab header tells us where the
2996 external symbols start. We don't care about the local symbols. */
2997 if (elf_bad_symtab (abfd))
2999 extsymcount = symcount;
3004 extsymcount = symcount - hdr->sh_info;
3005 extsymoff = hdr->sh_info;
3008 if (extsymcount == 0)
3011 /* Read in the symbol table. */
3012 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3014 if (isymbuf == NULL)
3017 /* Scan the symbol table looking for SYMDEF. */
3019 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3023 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3028 if (strcmp (name, symdef->name) == 0)
3030 result = is_global_data_symbol_definition (abfd, isym);
3040 /* Add an entry to the .dynamic table. */
3043 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3047 struct elf_link_hash_table *hash_table;
3048 const struct elf_backend_data *bed;
3050 bfd_size_type newsize;
3051 bfd_byte *newcontents;
3052 Elf_Internal_Dyn dyn;
3054 hash_table = elf_hash_table (info);
3055 if (! is_elf_hash_table (hash_table))
3058 bed = get_elf_backend_data (hash_table->dynobj);
3059 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3060 BFD_ASSERT (s != NULL);
3062 newsize = s->size + bed->s->sizeof_dyn;
3063 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3064 if (newcontents == NULL)
3068 dyn.d_un.d_val = val;
3069 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3072 s->contents = newcontents;
3077 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3078 otherwise just check whether one already exists. Returns -1 on error,
3079 1 if a DT_NEEDED tag already exists, and 0 on success. */
3082 elf_add_dt_needed_tag (bfd *abfd,
3083 struct bfd_link_info *info,
3087 struct elf_link_hash_table *hash_table;
3088 bfd_size_type oldsize;
3089 bfd_size_type strindex;
3091 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3094 hash_table = elf_hash_table (info);
3095 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3096 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3097 if (strindex == (bfd_size_type) -1)
3100 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3103 const struct elf_backend_data *bed;
3106 bed = get_elf_backend_data (hash_table->dynobj);
3107 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3109 for (extdyn = sdyn->contents;
3110 extdyn < sdyn->contents + sdyn->size;
3111 extdyn += bed->s->sizeof_dyn)
3113 Elf_Internal_Dyn dyn;
3115 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3116 if (dyn.d_tag == DT_NEEDED
3117 && dyn.d_un.d_val == strindex)
3119 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3127 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3130 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3134 /* We were just checking for existence of the tag. */
3135 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3141 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3143 for (; needed != NULL; needed = needed->next)
3144 if (strcmp (soname, needed->name) == 0)
3150 /* Sort symbol by value, section, and size. */
3152 elf_sort_symbol (const void *arg1, const void *arg2)
3154 const struct elf_link_hash_entry *h1;
3155 const struct elf_link_hash_entry *h2;
3156 bfd_signed_vma vdiff;
3158 h1 = *(const struct elf_link_hash_entry **) arg1;
3159 h2 = *(const struct elf_link_hash_entry **) arg2;
3160 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3162 return vdiff > 0 ? 1 : -1;
3165 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3167 return sdiff > 0 ? 1 : -1;
3169 vdiff = h1->size - h2->size;
3170 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3173 /* This function is used to adjust offsets into .dynstr for
3174 dynamic symbols. This is called via elf_link_hash_traverse. */
3177 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3179 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3181 if (h->dynindx != -1)
3182 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3186 /* Assign string offsets in .dynstr, update all structures referencing
3190 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3192 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3193 struct elf_link_local_dynamic_entry *entry;
3194 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3195 bfd *dynobj = hash_table->dynobj;
3198 const struct elf_backend_data *bed;
3201 _bfd_elf_strtab_finalize (dynstr);
3202 size = _bfd_elf_strtab_size (dynstr);
3204 bed = get_elf_backend_data (dynobj);
3205 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3206 BFD_ASSERT (sdyn != NULL);
3208 /* Update all .dynamic entries referencing .dynstr strings. */
3209 for (extdyn = sdyn->contents;
3210 extdyn < sdyn->contents + sdyn->size;
3211 extdyn += bed->s->sizeof_dyn)
3213 Elf_Internal_Dyn dyn;
3215 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3219 dyn.d_un.d_val = size;
3229 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3234 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3237 /* Now update local dynamic symbols. */
3238 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3239 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3240 entry->isym.st_name);
3242 /* And the rest of dynamic symbols. */
3243 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3245 /* Adjust version definitions. */
3246 if (elf_tdata (output_bfd)->cverdefs)
3251 Elf_Internal_Verdef def;
3252 Elf_Internal_Verdaux defaux;
3254 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3258 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3260 p += sizeof (Elf_External_Verdef);
3261 if (def.vd_aux != sizeof (Elf_External_Verdef))
3263 for (i = 0; i < def.vd_cnt; ++i)
3265 _bfd_elf_swap_verdaux_in (output_bfd,
3266 (Elf_External_Verdaux *) p, &defaux);
3267 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3269 _bfd_elf_swap_verdaux_out (output_bfd,
3270 &defaux, (Elf_External_Verdaux *) p);
3271 p += sizeof (Elf_External_Verdaux);
3274 while (def.vd_next);
3277 /* Adjust version references. */
3278 if (elf_tdata (output_bfd)->verref)
3283 Elf_Internal_Verneed need;
3284 Elf_Internal_Vernaux needaux;
3286 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3290 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3292 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3293 _bfd_elf_swap_verneed_out (output_bfd, &need,
3294 (Elf_External_Verneed *) p);
3295 p += sizeof (Elf_External_Verneed);
3296 for (i = 0; i < need.vn_cnt; ++i)
3298 _bfd_elf_swap_vernaux_in (output_bfd,
3299 (Elf_External_Vernaux *) p, &needaux);
3300 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3302 _bfd_elf_swap_vernaux_out (output_bfd,
3304 (Elf_External_Vernaux *) p);
3305 p += sizeof (Elf_External_Vernaux);
3308 while (need.vn_next);
3314 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3315 The default is to only match when the INPUT and OUTPUT are exactly
3319 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3320 const bfd_target *output)
3322 return input == output;
3325 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3326 This version is used when different targets for the same architecture
3327 are virtually identical. */
3330 _bfd_elf_relocs_compatible (const bfd_target *input,
3331 const bfd_target *output)
3333 const struct elf_backend_data *obed, *ibed;
3335 if (input == output)
3338 ibed = xvec_get_elf_backend_data (input);
3339 obed = xvec_get_elf_backend_data (output);
3341 if (ibed->arch != obed->arch)
3344 /* If both backends are using this function, deem them compatible. */
3345 return ibed->relocs_compatible == obed->relocs_compatible;
3348 /* Add symbols from an ELF object file to the linker hash table. */
3351 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3353 Elf_Internal_Ehdr *ehdr;
3354 Elf_Internal_Shdr *hdr;
3355 bfd_size_type symcount;
3356 bfd_size_type extsymcount;
3357 bfd_size_type extsymoff;
3358 struct elf_link_hash_entry **sym_hash;
3359 bfd_boolean dynamic;
3360 Elf_External_Versym *extversym = NULL;
3361 Elf_External_Versym *ever;
3362 struct elf_link_hash_entry *weaks;
3363 struct elf_link_hash_entry **nondeflt_vers = NULL;
3364 bfd_size_type nondeflt_vers_cnt = 0;
3365 Elf_Internal_Sym *isymbuf = NULL;
3366 Elf_Internal_Sym *isym;
3367 Elf_Internal_Sym *isymend;
3368 const struct elf_backend_data *bed;
3369 bfd_boolean add_needed;
3370 struct elf_link_hash_table *htab;
3372 void *alloc_mark = NULL;
3373 struct bfd_hash_entry **old_table = NULL;
3374 unsigned int old_size = 0;
3375 unsigned int old_count = 0;
3376 void *old_tab = NULL;
3379 struct bfd_link_hash_entry *old_undefs = NULL;
3380 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3381 long old_dynsymcount = 0;
3383 size_t hashsize = 0;
3385 htab = elf_hash_table (info);
3386 bed = get_elf_backend_data (abfd);
3388 if ((abfd->flags & DYNAMIC) == 0)
3394 /* You can't use -r against a dynamic object. Also, there's no
3395 hope of using a dynamic object which does not exactly match
3396 the format of the output file. */
3397 if (info->relocatable
3398 || !is_elf_hash_table (htab)
3399 || info->output_bfd->xvec != abfd->xvec)
3401 if (info->relocatable)
3402 bfd_set_error (bfd_error_invalid_operation);
3404 bfd_set_error (bfd_error_wrong_format);
3409 ehdr = elf_elfheader (abfd);
3410 if (info->warn_alternate_em
3411 && bed->elf_machine_code != ehdr->e_machine
3412 && ((bed->elf_machine_alt1 != 0
3413 && ehdr->e_machine == bed->elf_machine_alt1)
3414 || (bed->elf_machine_alt2 != 0
3415 && ehdr->e_machine == bed->elf_machine_alt2)))
3416 info->callbacks->einfo
3417 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3418 ehdr->e_machine, abfd, bed->elf_machine_code);
3420 /* As a GNU extension, any input sections which are named
3421 .gnu.warning.SYMBOL are treated as warning symbols for the given
3422 symbol. This differs from .gnu.warning sections, which generate
3423 warnings when they are included in an output file. */
3424 /* PR 12761: Also generate this warning when building shared libraries. */
3425 if (info->executable || info->shared)
3429 for (s = abfd->sections; s != NULL; s = s->next)
3433 name = bfd_get_section_name (abfd, s);
3434 if (CONST_STRNEQ (name, ".gnu.warning."))
3439 name += sizeof ".gnu.warning." - 1;
3441 /* If this is a shared object, then look up the symbol
3442 in the hash table. If it is there, and it is already
3443 been defined, then we will not be using the entry
3444 from this shared object, so we don't need to warn.
3445 FIXME: If we see the definition in a regular object
3446 later on, we will warn, but we shouldn't. The only
3447 fix is to keep track of what warnings we are supposed
3448 to emit, and then handle them all at the end of the
3452 struct elf_link_hash_entry *h;
3454 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3456 /* FIXME: What about bfd_link_hash_common? */
3458 && (h->root.type == bfd_link_hash_defined
3459 || h->root.type == bfd_link_hash_defweak))
3461 /* We don't want to issue this warning. Clobber
3462 the section size so that the warning does not
3463 get copied into the output file. */
3470 msg = (char *) bfd_alloc (abfd, sz + 1);
3474 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3479 if (! (_bfd_generic_link_add_one_symbol
3480 (info, abfd, name, BSF_WARNING, s, 0, msg,
3481 FALSE, bed->collect, NULL)))
3484 if (! info->relocatable)
3486 /* Clobber the section size so that the warning does
3487 not get copied into the output file. */
3490 /* Also set SEC_EXCLUDE, so that symbols defined in
3491 the warning section don't get copied to the output. */
3492 s->flags |= SEC_EXCLUDE;
3501 /* If we are creating a shared library, create all the dynamic
3502 sections immediately. We need to attach them to something,
3503 so we attach them to this BFD, provided it is the right
3504 format. FIXME: If there are no input BFD's of the same
3505 format as the output, we can't make a shared library. */
3507 && is_elf_hash_table (htab)
3508 && info->output_bfd->xvec == abfd->xvec
3509 && !htab->dynamic_sections_created)
3511 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3515 else if (!is_elf_hash_table (htab))
3520 const char *soname = NULL;
3522 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3525 /* ld --just-symbols and dynamic objects don't mix very well.
3526 ld shouldn't allow it. */
3527 if ((s = abfd->sections) != NULL
3528 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
3531 /* If this dynamic lib was specified on the command line with
3532 --as-needed in effect, then we don't want to add a DT_NEEDED
3533 tag unless the lib is actually used. Similary for libs brought
3534 in by another lib's DT_NEEDED. When --no-add-needed is used
3535 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3536 any dynamic library in DT_NEEDED tags in the dynamic lib at
3538 add_needed = (elf_dyn_lib_class (abfd)
3539 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3540 | DYN_NO_NEEDED)) == 0;
3542 s = bfd_get_section_by_name (abfd, ".dynamic");
3547 unsigned int elfsec;
3548 unsigned long shlink;
3550 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3557 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3558 if (elfsec == SHN_BAD)
3559 goto error_free_dyn;
3560 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3562 for (extdyn = dynbuf;
3563 extdyn < dynbuf + s->size;
3564 extdyn += bed->s->sizeof_dyn)
3566 Elf_Internal_Dyn dyn;
3568 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3569 if (dyn.d_tag == DT_SONAME)
3571 unsigned int tagv = dyn.d_un.d_val;
3572 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3574 goto error_free_dyn;
3576 if (dyn.d_tag == DT_NEEDED)
3578 struct bfd_link_needed_list *n, **pn;
3580 unsigned int tagv = dyn.d_un.d_val;
3582 amt = sizeof (struct bfd_link_needed_list);
3583 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3584 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3585 if (n == NULL || fnm == NULL)
3586 goto error_free_dyn;
3587 amt = strlen (fnm) + 1;
3588 anm = (char *) bfd_alloc (abfd, amt);
3590 goto error_free_dyn;
3591 memcpy (anm, fnm, amt);
3595 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3599 if (dyn.d_tag == DT_RUNPATH)
3601 struct bfd_link_needed_list *n, **pn;
3603 unsigned int tagv = dyn.d_un.d_val;
3605 amt = sizeof (struct bfd_link_needed_list);
3606 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3607 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3608 if (n == NULL || fnm == NULL)
3609 goto error_free_dyn;
3610 amt = strlen (fnm) + 1;
3611 anm = (char *) bfd_alloc (abfd, amt);
3613 goto error_free_dyn;
3614 memcpy (anm, fnm, amt);
3618 for (pn = & runpath;
3624 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3625 if (!runpath && dyn.d_tag == DT_RPATH)
3627 struct bfd_link_needed_list *n, **pn;
3629 unsigned int tagv = dyn.d_un.d_val;
3631 amt = sizeof (struct bfd_link_needed_list);
3632 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3633 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3634 if (n == NULL || fnm == NULL)
3635 goto error_free_dyn;
3636 amt = strlen (fnm) + 1;
3637 anm = (char *) bfd_alloc (abfd, amt);
3639 goto error_free_dyn;
3640 memcpy (anm, fnm, amt);
3650 if (dyn.d_tag == DT_AUDIT)
3652 unsigned int tagv = dyn.d_un.d_val;
3653 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3660 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3661 frees all more recently bfd_alloc'd blocks as well. */
3667 struct bfd_link_needed_list **pn;
3668 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3673 /* We do not want to include any of the sections in a dynamic
3674 object in the output file. We hack by simply clobbering the
3675 list of sections in the BFD. This could be handled more
3676 cleanly by, say, a new section flag; the existing
3677 SEC_NEVER_LOAD flag is not the one we want, because that one
3678 still implies that the section takes up space in the output
3680 bfd_section_list_clear (abfd);
3682 /* Find the name to use in a DT_NEEDED entry that refers to this
3683 object. If the object has a DT_SONAME entry, we use it.
3684 Otherwise, if the generic linker stuck something in
3685 elf_dt_name, we use that. Otherwise, we just use the file
3687 if (soname == NULL || *soname == '\0')
3689 soname = elf_dt_name (abfd);
3690 if (soname == NULL || *soname == '\0')
3691 soname = bfd_get_filename (abfd);
3694 /* Save the SONAME because sometimes the linker emulation code
3695 will need to know it. */
3696 elf_dt_name (abfd) = soname;
3698 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3702 /* If we have already included this dynamic object in the
3703 link, just ignore it. There is no reason to include a
3704 particular dynamic object more than once. */
3708 /* Save the DT_AUDIT entry for the linker emulation code. */
3709 elf_dt_audit (abfd) = audit;
3712 /* If this is a dynamic object, we always link against the .dynsym
3713 symbol table, not the .symtab symbol table. The dynamic linker
3714 will only see the .dynsym symbol table, so there is no reason to
3715 look at .symtab for a dynamic object. */
3717 if (! dynamic || elf_dynsymtab (abfd) == 0)
3718 hdr = &elf_tdata (abfd)->symtab_hdr;
3720 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3722 symcount = hdr->sh_size / bed->s->sizeof_sym;
3724 /* The sh_info field of the symtab header tells us where the
3725 external symbols start. We don't care about the local symbols at
3727 if (elf_bad_symtab (abfd))
3729 extsymcount = symcount;
3734 extsymcount = symcount - hdr->sh_info;
3735 extsymoff = hdr->sh_info;
3739 if (extsymcount != 0)
3741 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3743 if (isymbuf == NULL)
3746 /* We store a pointer to the hash table entry for each external
3748 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3749 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
3750 if (sym_hash == NULL)
3751 goto error_free_sym;
3752 elf_sym_hashes (abfd) = sym_hash;
3757 /* Read in any version definitions. */
3758 if (!_bfd_elf_slurp_version_tables (abfd,
3759 info->default_imported_symver))
3760 goto error_free_sym;
3762 /* Read in the symbol versions, but don't bother to convert them
3763 to internal format. */
3764 if (elf_dynversym (abfd) != 0)
3766 Elf_Internal_Shdr *versymhdr;
3768 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3769 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3770 if (extversym == NULL)
3771 goto error_free_sym;
3772 amt = versymhdr->sh_size;
3773 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3774 || bfd_bread (extversym, amt, abfd) != amt)
3775 goto error_free_vers;
3779 /* If we are loading an as-needed shared lib, save the symbol table
3780 state before we start adding symbols. If the lib turns out
3781 to be unneeded, restore the state. */
3782 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3787 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3789 struct bfd_hash_entry *p;
3790 struct elf_link_hash_entry *h;
3792 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3794 h = (struct elf_link_hash_entry *) p;
3795 entsize += htab->root.table.entsize;
3796 if (h->root.type == bfd_link_hash_warning)
3797 entsize += htab->root.table.entsize;
3801 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3802 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3803 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3804 if (old_tab == NULL)
3805 goto error_free_vers;
3807 /* Remember the current objalloc pointer, so that all mem for
3808 symbols added can later be reclaimed. */
3809 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3810 if (alloc_mark == NULL)
3811 goto error_free_vers;
3813 /* Make a special call to the linker "notice" function to
3814 tell it that we are about to handle an as-needed lib. */
3815 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3816 notice_as_needed, 0, NULL))
3817 goto error_free_vers;
3819 /* Clone the symbol table and sym hashes. Remember some
3820 pointers into the symbol table, and dynamic symbol count. */
3821 old_hash = (char *) old_tab + tabsize;
3822 old_ent = (char *) old_hash + hashsize;
3823 memcpy (old_tab, htab->root.table.table, tabsize);
3824 memcpy (old_hash, sym_hash, hashsize);
3825 old_undefs = htab->root.undefs;
3826 old_undefs_tail = htab->root.undefs_tail;
3827 old_table = htab->root.table.table;
3828 old_size = htab->root.table.size;
3829 old_count = htab->root.table.count;
3830 old_dynsymcount = htab->dynsymcount;
3832 for (i = 0; i < htab->root.table.size; i++)
3834 struct bfd_hash_entry *p;
3835 struct elf_link_hash_entry *h;
3837 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3839 memcpy (old_ent, p, htab->root.table.entsize);
3840 old_ent = (char *) old_ent + htab->root.table.entsize;
3841 h = (struct elf_link_hash_entry *) p;
3842 if (h->root.type == bfd_link_hash_warning)
3844 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3845 old_ent = (char *) old_ent + htab->root.table.entsize;
3852 ever = extversym != NULL ? extversym + extsymoff : NULL;
3853 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3855 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3859 asection *sec, *new_sec;
3862 struct elf_link_hash_entry *h;
3863 struct elf_link_hash_entry *hi;
3864 bfd_boolean definition;
3865 bfd_boolean size_change_ok;
3866 bfd_boolean type_change_ok;
3867 bfd_boolean new_weakdef;
3868 bfd_boolean override;
3870 unsigned int old_alignment;
3872 bfd * undef_bfd = NULL;
3876 flags = BSF_NO_FLAGS;
3878 value = isym->st_value;
3880 common = bed->common_definition (isym);
3882 bind = ELF_ST_BIND (isym->st_info);
3886 /* This should be impossible, since ELF requires that all
3887 global symbols follow all local symbols, and that sh_info
3888 point to the first global symbol. Unfortunately, Irix 5
3893 if (isym->st_shndx != SHN_UNDEF && !common)
3901 case STB_GNU_UNIQUE:
3902 flags = BSF_GNU_UNIQUE;
3906 /* Leave it up to the processor backend. */
3910 if (isym->st_shndx == SHN_UNDEF)
3911 sec = bfd_und_section_ptr;
3912 else if (isym->st_shndx == SHN_ABS)
3913 sec = bfd_abs_section_ptr;
3914 else if (isym->st_shndx == SHN_COMMON)
3916 sec = bfd_com_section_ptr;
3917 /* What ELF calls the size we call the value. What ELF
3918 calls the value we call the alignment. */
3919 value = isym->st_size;
3923 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3925 sec = bfd_abs_section_ptr;
3926 else if (discarded_section (sec))
3928 /* Symbols from discarded section are undefined. We keep
3930 sec = bfd_und_section_ptr;
3931 isym->st_shndx = SHN_UNDEF;
3933 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3937 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3940 goto error_free_vers;
3942 if (isym->st_shndx == SHN_COMMON
3943 && (abfd->flags & BFD_PLUGIN) != 0)
3945 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3949 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3951 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3953 goto error_free_vers;
3957 else if (isym->st_shndx == SHN_COMMON
3958 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3959 && !info->relocatable)
3961 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3965 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3966 | SEC_LINKER_CREATED);
3967 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3969 goto error_free_vers;
3973 else if (bed->elf_add_symbol_hook)
3975 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3977 goto error_free_vers;
3979 /* The hook function sets the name to NULL if this symbol
3980 should be skipped for some reason. */
3985 /* Sanity check that all possibilities were handled. */
3988 bfd_set_error (bfd_error_bad_value);
3989 goto error_free_vers;
3992 if (bfd_is_und_section (sec)
3993 || bfd_is_com_section (sec))
3998 size_change_ok = FALSE;
3999 type_change_ok = bed->type_change_ok;
4004 if (is_elf_hash_table (htab))
4006 Elf_Internal_Versym iver;
4007 unsigned int vernum = 0;
4010 /* If this is a definition of a symbol which was previously
4011 referenced in a non-weak manner then make a note of the bfd
4012 that contained the reference. This is used if we need to
4013 refer to the source of the reference later on. */
4014 if (! bfd_is_und_section (sec))
4016 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4019 && h->root.type == bfd_link_hash_undefined
4020 && h->root.u.undef.abfd)
4021 undef_bfd = h->root.u.undef.abfd;
4026 if (info->default_imported_symver)
4027 /* Use the default symbol version created earlier. */
4028 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4033 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4035 vernum = iver.vs_vers & VERSYM_VERSION;
4037 /* If this is a hidden symbol, or if it is not version
4038 1, we append the version name to the symbol name.
4039 However, we do not modify a non-hidden absolute symbol
4040 if it is not a function, because it might be the version
4041 symbol itself. FIXME: What if it isn't? */
4042 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4044 && (!bfd_is_abs_section (sec)
4045 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4048 size_t namelen, verlen, newlen;
4051 if (isym->st_shndx != SHN_UNDEF)
4053 if (vernum > elf_tdata (abfd)->cverdefs)
4055 else if (vernum > 1)
4057 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4063 (*_bfd_error_handler)
4064 (_("%B: %s: invalid version %u (max %d)"),
4066 elf_tdata (abfd)->cverdefs);
4067 bfd_set_error (bfd_error_bad_value);
4068 goto error_free_vers;
4073 /* We cannot simply test for the number of
4074 entries in the VERNEED section since the
4075 numbers for the needed versions do not start
4077 Elf_Internal_Verneed *t;
4080 for (t = elf_tdata (abfd)->verref;
4084 Elf_Internal_Vernaux *a;
4086 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4088 if (a->vna_other == vernum)
4090 verstr = a->vna_nodename;
4099 (*_bfd_error_handler)
4100 (_("%B: %s: invalid needed version %d"),
4101 abfd, name, vernum);
4102 bfd_set_error (bfd_error_bad_value);
4103 goto error_free_vers;
4107 namelen = strlen (name);
4108 verlen = strlen (verstr);
4109 newlen = namelen + verlen + 2;
4110 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4111 && isym->st_shndx != SHN_UNDEF)
4114 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4115 if (newname == NULL)
4116 goto error_free_vers;
4117 memcpy (newname, name, namelen);
4118 p = newname + namelen;
4120 /* If this is a defined non-hidden version symbol,
4121 we add another @ to the name. This indicates the
4122 default version of the symbol. */
4123 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4124 && isym->st_shndx != SHN_UNDEF)
4126 memcpy (p, verstr, verlen + 1);
4131 /* If necessary, make a second attempt to locate the bfd
4132 containing an unresolved, non-weak reference to the
4134 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
4136 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4139 && h->root.type == bfd_link_hash_undefined
4140 && h->root.u.undef.abfd)
4141 undef_bfd = h->root.u.undef.abfd;
4144 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4145 &value, &old_alignment,
4146 sym_hash, &skip, &override,
4147 &type_change_ok, &size_change_ok))
4148 goto error_free_vers;
4157 while (h->root.type == bfd_link_hash_indirect
4158 || h->root.type == bfd_link_hash_warning)
4159 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4161 /* Remember the old alignment if this is a common symbol, so
4162 that we don't reduce the alignment later on. We can't
4163 check later, because _bfd_generic_link_add_one_symbol
4164 will set a default for the alignment which we want to
4165 override. We also remember the old bfd where the existing
4166 definition comes from. */
4167 switch (h->root.type)
4172 case bfd_link_hash_defined:
4173 case bfd_link_hash_defweak:
4174 old_bfd = h->root.u.def.section->owner;
4177 case bfd_link_hash_common:
4178 old_bfd = h->root.u.c.p->section->owner;
4179 old_alignment = h->root.u.c.p->alignment_power;
4183 if (elf_tdata (abfd)->verdef != NULL
4187 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4190 if (! (_bfd_generic_link_add_one_symbol
4191 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4192 (struct bfd_link_hash_entry **) sym_hash)))
4193 goto error_free_vers;
4196 /* We need to make sure that indirect symbol dynamic flags are
4199 while (h->root.type == bfd_link_hash_indirect
4200 || h->root.type == bfd_link_hash_warning)
4201 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4204 if (is_elf_hash_table (htab))
4205 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4207 new_weakdef = FALSE;
4210 && (flags & BSF_WEAK) != 0
4211 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4212 && is_elf_hash_table (htab)
4213 && h->u.weakdef == NULL)
4215 /* Keep a list of all weak defined non function symbols from
4216 a dynamic object, using the weakdef field. Later in this
4217 function we will set the weakdef field to the correct
4218 value. We only put non-function symbols from dynamic
4219 objects on this list, because that happens to be the only
4220 time we need to know the normal symbol corresponding to a
4221 weak symbol, and the information is time consuming to
4222 figure out. If the weakdef field is not already NULL,
4223 then this symbol was already defined by some previous
4224 dynamic object, and we will be using that previous
4225 definition anyhow. */
4227 h->u.weakdef = weaks;
4232 /* Set the alignment of a common symbol. */
4233 if ((common || bfd_is_com_section (sec))
4234 && h->root.type == bfd_link_hash_common)
4239 align = bfd_log2 (isym->st_value);
4242 /* The new symbol is a common symbol in a shared object.
4243 We need to get the alignment from the section. */
4244 align = new_sec->alignment_power;
4246 if (align > old_alignment)
4247 h->root.u.c.p->alignment_power = align;
4249 h->root.u.c.p->alignment_power = old_alignment;
4252 if (is_elf_hash_table (htab))
4256 /* Check the alignment when a common symbol is involved. This
4257 can change when a common symbol is overridden by a normal
4258 definition or a common symbol is ignored due to the old
4259 normal definition. We need to make sure the maximum
4260 alignment is maintained. */
4261 if ((old_alignment || common)
4262 && h->root.type != bfd_link_hash_common)
4264 unsigned int common_align;
4265 unsigned int normal_align;
4266 unsigned int symbol_align;
4270 symbol_align = ffs (h->root.u.def.value) - 1;
4271 if (h->root.u.def.section->owner != NULL
4272 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4274 normal_align = h->root.u.def.section->alignment_power;
4275 if (normal_align > symbol_align)
4276 normal_align = symbol_align;
4279 normal_align = symbol_align;
4283 common_align = old_alignment;
4284 common_bfd = old_bfd;
4289 common_align = bfd_log2 (isym->st_value);
4291 normal_bfd = old_bfd;
4294 if (normal_align < common_align)
4296 /* PR binutils/2735 */
4297 if (normal_bfd == NULL)
4298 (*_bfd_error_handler)
4299 (_("Warning: alignment %u of common symbol `%s' in %B"
4300 " is greater than the alignment (%u) of its section %A"),
4301 common_bfd, h->root.u.def.section,
4302 1 << common_align, name, 1 << normal_align);
4304 (*_bfd_error_handler)
4305 (_("Warning: alignment %u of symbol `%s' in %B"
4306 " is smaller than %u in %B"),
4307 normal_bfd, common_bfd,
4308 1 << normal_align, name, 1 << common_align);
4312 /* Remember the symbol size if it isn't undefined. */
4313 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4314 && (definition || h->size == 0))
4317 && h->size != isym->st_size
4318 && ! size_change_ok)
4319 (*_bfd_error_handler)
4320 (_("Warning: size of symbol `%s' changed"
4321 " from %lu in %B to %lu in %B"),
4323 name, (unsigned long) h->size,
4324 (unsigned long) isym->st_size);
4326 h->size = isym->st_size;
4329 /* If this is a common symbol, then we always want H->SIZE
4330 to be the size of the common symbol. The code just above
4331 won't fix the size if a common symbol becomes larger. We
4332 don't warn about a size change here, because that is
4333 covered by --warn-common. Allow changed between different
4335 if (h->root.type == bfd_link_hash_common)
4336 h->size = h->root.u.c.size;
4338 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4339 && (definition || h->type == STT_NOTYPE))
4341 unsigned int type = ELF_ST_TYPE (isym->st_info);
4343 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4345 if (type == STT_GNU_IFUNC
4346 && (abfd->flags & DYNAMIC) != 0)
4349 if (h->type != type)
4351 if (h->type != STT_NOTYPE && ! type_change_ok)
4352 (*_bfd_error_handler)
4353 (_("Warning: type of symbol `%s' changed"
4354 " from %d to %d in %B"),
4355 abfd, name, h->type, type);
4361 /* Merge st_other field. */
4362 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4364 /* Set a flag in the hash table entry indicating the type of
4365 reference or definition we just found. Keep a count of
4366 the number of dynamic symbols we find. A dynamic symbol
4367 is one which is referenced or defined by both a regular
4368 object and a shared object. */
4375 if (bind != STB_WEAK)
4376 h->ref_regular_nonweak = 1;
4388 /* If the indirect symbol has been forced local, don't
4389 make the real symbol dynamic. */
4390 if ((h == hi || !hi->forced_local)
4391 && (! info->executable
4401 hi->ref_dynamic = 1;
4407 hi->def_dynamic = 1;
4408 hi->dynamic_def = 1;
4411 /* If the indirect symbol has been forced local, don't
4412 make the real symbol dynamic. */
4413 if ((h == hi || !hi->forced_local)
4416 || (h->u.weakdef != NULL
4418 && h->u.weakdef->dynindx != -1)))
4422 /* We don't want to make debug symbol dynamic. */
4423 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4426 /* Nor should we make plugin symbols dynamic. */
4427 if ((abfd->flags & BFD_PLUGIN) != 0)
4431 h->target_internal = isym->st_target_internal;
4433 /* Check to see if we need to add an indirect symbol for
4434 the default name. */
4435 if (definition || h->root.type == bfd_link_hash_common)
4436 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4437 &sec, &value, &dynsym,
4439 goto error_free_vers;
4441 if (definition && !dynamic)
4443 char *p = strchr (name, ELF_VER_CHR);
4444 if (p != NULL && p[1] != ELF_VER_CHR)
4446 /* Queue non-default versions so that .symver x, x@FOO
4447 aliases can be checked. */
4450 amt = ((isymend - isym + 1)
4451 * sizeof (struct elf_link_hash_entry *));
4453 (struct elf_link_hash_entry **) bfd_malloc (amt);
4455 goto error_free_vers;
4457 nondeflt_vers[nondeflt_vers_cnt++] = h;
4461 if (dynsym && h->dynindx == -1)
4463 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4464 goto error_free_vers;
4465 if (h->u.weakdef != NULL
4467 && h->u.weakdef->dynindx == -1)
4469 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4470 goto error_free_vers;
4473 else if (dynsym && h->dynindx != -1)
4474 /* If the symbol already has a dynamic index, but
4475 visibility says it should not be visible, turn it into
4477 switch (ELF_ST_VISIBILITY (h->other))
4481 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4491 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4492 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4495 const char *soname = elf_dt_name (abfd);
4497 /* A symbol from a library loaded via DT_NEEDED of some
4498 other library is referenced by a regular object.
4499 Add a DT_NEEDED entry for it. Issue an error if
4500 --no-add-needed is used and the reference was not
4502 if (undef_bfd != NULL
4503 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4505 (*_bfd_error_handler)
4506 (_("%B: undefined reference to symbol '%s'"),
4508 (*_bfd_error_handler)
4509 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
4511 bfd_set_error (bfd_error_invalid_operation);
4512 goto error_free_vers;
4515 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4516 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4519 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4521 goto error_free_vers;
4523 BFD_ASSERT (ret == 0);
4528 if (extversym != NULL)
4534 if (isymbuf != NULL)
4540 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4544 /* Restore the symbol table. */
4545 if (bed->as_needed_cleanup)
4546 (*bed->as_needed_cleanup) (abfd, info);
4547 old_hash = (char *) old_tab + tabsize;
4548 old_ent = (char *) old_hash + hashsize;
4549 sym_hash = elf_sym_hashes (abfd);
4550 htab->root.table.table = old_table;
4551 htab->root.table.size = old_size;
4552 htab->root.table.count = old_count;
4553 memcpy (htab->root.table.table, old_tab, tabsize);
4554 memcpy (sym_hash, old_hash, hashsize);
4555 htab->root.undefs = old_undefs;
4556 htab->root.undefs_tail = old_undefs_tail;
4557 for (i = 0; i < htab->root.table.size; i++)
4559 struct bfd_hash_entry *p;
4560 struct elf_link_hash_entry *h;
4562 unsigned int alignment_power;
4564 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4566 h = (struct elf_link_hash_entry *) p;
4567 if (h->root.type == bfd_link_hash_warning)
4568 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4569 if (h->dynindx >= old_dynsymcount)
4570 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4572 /* Preserve the maximum alignment and size for common
4573 symbols even if this dynamic lib isn't on DT_NEEDED
4574 since it can still be loaded at the run-time by another
4576 if (h->root.type == bfd_link_hash_common)
4578 size = h->root.u.c.size;
4579 alignment_power = h->root.u.c.p->alignment_power;
4584 alignment_power = 0;
4586 memcpy (p, old_ent, htab->root.table.entsize);
4587 old_ent = (char *) old_ent + htab->root.table.entsize;
4588 h = (struct elf_link_hash_entry *) p;
4589 if (h->root.type == bfd_link_hash_warning)
4591 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4592 old_ent = (char *) old_ent + htab->root.table.entsize;
4594 else if (h->root.type == bfd_link_hash_common)
4596 if (size > h->root.u.c.size)
4597 h->root.u.c.size = size;
4598 if (alignment_power > h->root.u.c.p->alignment_power)
4599 h->root.u.c.p->alignment_power = alignment_power;
4604 /* Make a special call to the linker "notice" function to
4605 tell it that symbols added for crefs may need to be removed. */
4606 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4607 notice_not_needed, 0, NULL))
4608 goto error_free_vers;
4611 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4613 if (nondeflt_vers != NULL)
4614 free (nondeflt_vers);
4618 if (old_tab != NULL)
4620 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4621 notice_needed, 0, NULL))
4622 goto error_free_vers;
4627 /* Now that all the symbols from this input file are created, handle
4628 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4629 if (nondeflt_vers != NULL)
4631 bfd_size_type cnt, symidx;
4633 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4635 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4636 char *shortname, *p;
4638 p = strchr (h->root.root.string, ELF_VER_CHR);
4640 || (h->root.type != bfd_link_hash_defined
4641 && h->root.type != bfd_link_hash_defweak))
4644 amt = p - h->root.root.string;
4645 shortname = (char *) bfd_malloc (amt + 1);
4647 goto error_free_vers;
4648 memcpy (shortname, h->root.root.string, amt);
4649 shortname[amt] = '\0';
4651 hi = (struct elf_link_hash_entry *)
4652 bfd_link_hash_lookup (&htab->root, shortname,
4653 FALSE, FALSE, FALSE);
4655 && hi->root.type == h->root.type
4656 && hi->root.u.def.value == h->root.u.def.value
4657 && hi->root.u.def.section == h->root.u.def.section)
4659 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4660 hi->root.type = bfd_link_hash_indirect;
4661 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4662 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4663 sym_hash = elf_sym_hashes (abfd);
4665 for (symidx = 0; symidx < extsymcount; ++symidx)
4666 if (sym_hash[symidx] == hi)
4668 sym_hash[symidx] = h;
4674 free (nondeflt_vers);
4675 nondeflt_vers = NULL;
4678 /* Now set the weakdefs field correctly for all the weak defined
4679 symbols we found. The only way to do this is to search all the
4680 symbols. Since we only need the information for non functions in
4681 dynamic objects, that's the only time we actually put anything on
4682 the list WEAKS. We need this information so that if a regular
4683 object refers to a symbol defined weakly in a dynamic object, the
4684 real symbol in the dynamic object is also put in the dynamic
4685 symbols; we also must arrange for both symbols to point to the
4686 same memory location. We could handle the general case of symbol
4687 aliasing, but a general symbol alias can only be generated in
4688 assembler code, handling it correctly would be very time
4689 consuming, and other ELF linkers don't handle general aliasing
4693 struct elf_link_hash_entry **hpp;
4694 struct elf_link_hash_entry **hppend;
4695 struct elf_link_hash_entry **sorted_sym_hash;
4696 struct elf_link_hash_entry *h;
4699 /* Since we have to search the whole symbol list for each weak
4700 defined symbol, search time for N weak defined symbols will be
4701 O(N^2). Binary search will cut it down to O(NlogN). */
4702 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4703 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4704 if (sorted_sym_hash == NULL)
4706 sym_hash = sorted_sym_hash;
4707 hpp = elf_sym_hashes (abfd);
4708 hppend = hpp + extsymcount;
4710 for (; hpp < hppend; hpp++)
4714 && h->root.type == bfd_link_hash_defined
4715 && !bed->is_function_type (h->type))
4723 qsort (sorted_sym_hash, sym_count,
4724 sizeof (struct elf_link_hash_entry *),
4727 while (weaks != NULL)
4729 struct elf_link_hash_entry *hlook;
4735 weaks = hlook->u.weakdef;
4736 hlook->u.weakdef = NULL;
4738 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4739 || hlook->root.type == bfd_link_hash_defweak
4740 || hlook->root.type == bfd_link_hash_common
4741 || hlook->root.type == bfd_link_hash_indirect);
4742 slook = hlook->root.u.def.section;
4743 vlook = hlook->root.u.def.value;
4749 bfd_signed_vma vdiff;
4751 h = sorted_sym_hash[idx];
4752 vdiff = vlook - h->root.u.def.value;
4759 long sdiff = slook->id - h->root.u.def.section->id;
4769 /* We didn't find a value/section match. */
4773 /* With multiple aliases, or when the weak symbol is already
4774 strongly defined, we have multiple matching symbols and
4775 the binary search above may land on any of them. Step
4776 one past the matching symbol(s). */
4779 h = sorted_sym_hash[idx];
4780 if (h->root.u.def.section != slook
4781 || h->root.u.def.value != vlook)
4785 /* Now look back over the aliases. Since we sorted by size
4786 as well as value and section, we'll choose the one with
4787 the largest size. */
4790 h = sorted_sym_hash[idx];
4792 /* Stop if value or section doesn't match. */
4793 if (h->root.u.def.section != slook
4794 || h->root.u.def.value != vlook)
4796 else if (h != hlook)
4798 hlook->u.weakdef = h;
4800 /* If the weak definition is in the list of dynamic
4801 symbols, make sure the real definition is put
4803 if (hlook->dynindx != -1 && h->dynindx == -1)
4805 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4808 free (sorted_sym_hash);
4813 /* If the real definition is in the list of dynamic
4814 symbols, make sure the weak definition is put
4815 there as well. If we don't do this, then the
4816 dynamic loader might not merge the entries for the
4817 real definition and the weak definition. */
4818 if (h->dynindx != -1 && hlook->dynindx == -1)
4820 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4821 goto err_free_sym_hash;
4828 free (sorted_sym_hash);
4831 if (bed->check_directives
4832 && !(*bed->check_directives) (abfd, info))
4835 /* If this object is the same format as the output object, and it is
4836 not a shared library, then let the backend look through the
4839 This is required to build global offset table entries and to
4840 arrange for dynamic relocs. It is not required for the
4841 particular common case of linking non PIC code, even when linking
4842 against shared libraries, but unfortunately there is no way of
4843 knowing whether an object file has been compiled PIC or not.
4844 Looking through the relocs is not particularly time consuming.
4845 The problem is that we must either (1) keep the relocs in memory,
4846 which causes the linker to require additional runtime memory or
4847 (2) read the relocs twice from the input file, which wastes time.
4848 This would be a good case for using mmap.
4850 I have no idea how to handle linking PIC code into a file of a
4851 different format. It probably can't be done. */
4853 && is_elf_hash_table (htab)
4854 && bed->check_relocs != NULL
4855 && elf_object_id (abfd) == elf_hash_table_id (htab)
4856 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4860 for (o = abfd->sections; o != NULL; o = o->next)
4862 Elf_Internal_Rela *internal_relocs;
4865 if ((o->flags & SEC_RELOC) == 0
4866 || o->reloc_count == 0
4867 || ((info->strip == strip_all || info->strip == strip_debugger)
4868 && (o->flags & SEC_DEBUGGING) != 0)
4869 || bfd_is_abs_section (o->output_section))
4872 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4874 if (internal_relocs == NULL)
4877 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4879 if (elf_section_data (o)->relocs != internal_relocs)
4880 free (internal_relocs);
4887 /* If this is a non-traditional link, try to optimize the handling
4888 of the .stab/.stabstr sections. */
4890 && ! info->traditional_format
4891 && is_elf_hash_table (htab)
4892 && (info->strip != strip_all && info->strip != strip_debugger))
4896 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4897 if (stabstr != NULL)
4899 bfd_size_type string_offset = 0;
4902 for (stab = abfd->sections; stab; stab = stab->next)
4903 if (CONST_STRNEQ (stab->name, ".stab")
4904 && (!stab->name[5] ||
4905 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4906 && (stab->flags & SEC_MERGE) == 0
4907 && !bfd_is_abs_section (stab->output_section))
4909 struct bfd_elf_section_data *secdata;
4911 secdata = elf_section_data (stab);
4912 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4913 stabstr, &secdata->sec_info,
4916 if (secdata->sec_info)
4917 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4922 if (is_elf_hash_table (htab) && add_needed)
4924 /* Add this bfd to the loaded list. */
4925 struct elf_link_loaded_list *n;
4927 n = (struct elf_link_loaded_list *)
4928 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4932 n->next = htab->loaded;
4939 if (old_tab != NULL)
4941 if (nondeflt_vers != NULL)
4942 free (nondeflt_vers);
4943 if (extversym != NULL)
4946 if (isymbuf != NULL)
4952 /* Return the linker hash table entry of a symbol that might be
4953 satisfied by an archive symbol. Return -1 on error. */
4955 struct elf_link_hash_entry *
4956 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4957 struct bfd_link_info *info,
4960 struct elf_link_hash_entry *h;
4964 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4968 /* If this is a default version (the name contains @@), look up the
4969 symbol again with only one `@' as well as without the version.
4970 The effect is that references to the symbol with and without the
4971 version will be matched by the default symbol in the archive. */
4973 p = strchr (name, ELF_VER_CHR);
4974 if (p == NULL || p[1] != ELF_VER_CHR)
4977 /* First check with only one `@'. */
4978 len = strlen (name);
4979 copy = (char *) bfd_alloc (abfd, len);
4981 return (struct elf_link_hash_entry *) 0 - 1;
4983 first = p - name + 1;
4984 memcpy (copy, name, first);
4985 memcpy (copy + first, name + first + 1, len - first);
4987 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4990 /* We also need to check references to the symbol without the
4992 copy[first - 1] = '\0';
4993 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4994 FALSE, FALSE, TRUE);
4997 bfd_release (abfd, copy);
5001 /* Add symbols from an ELF archive file to the linker hash table. We
5002 don't use _bfd_generic_link_add_archive_symbols because of a
5003 problem which arises on UnixWare. The UnixWare libc.so is an
5004 archive which includes an entry libc.so.1 which defines a bunch of
5005 symbols. The libc.so archive also includes a number of other
5006 object files, which also define symbols, some of which are the same
5007 as those defined in libc.so.1. Correct linking requires that we
5008 consider each object file in turn, and include it if it defines any
5009 symbols we need. _bfd_generic_link_add_archive_symbols does not do
5010 this; it looks through the list of undefined symbols, and includes
5011 any object file which defines them. When this algorithm is used on
5012 UnixWare, it winds up pulling in libc.so.1 early and defining a
5013 bunch of symbols. This means that some of the other objects in the
5014 archive are not included in the link, which is incorrect since they
5015 precede libc.so.1 in the archive.
5017 Fortunately, ELF archive handling is simpler than that done by
5018 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5019 oddities. In ELF, if we find a symbol in the archive map, and the
5020 symbol is currently undefined, we know that we must pull in that
5023 Unfortunately, we do have to make multiple passes over the symbol
5024 table until nothing further is resolved. */
5027 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5030 bfd_boolean *defined = NULL;
5031 bfd_boolean *included = NULL;
5035 const struct elf_backend_data *bed;
5036 struct elf_link_hash_entry * (*archive_symbol_lookup)
5037 (bfd *, struct bfd_link_info *, const char *);
5039 if (! bfd_has_map (abfd))
5041 /* An empty archive is a special case. */
5042 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5044 bfd_set_error (bfd_error_no_armap);
5048 /* Keep track of all symbols we know to be already defined, and all
5049 files we know to be already included. This is to speed up the
5050 second and subsequent passes. */
5051 c = bfd_ardata (abfd)->symdef_count;
5055 amt *= sizeof (bfd_boolean);
5056 defined = (bfd_boolean *) bfd_zmalloc (amt);
5057 included = (bfd_boolean *) bfd_zmalloc (amt);
5058 if (defined == NULL || included == NULL)
5061 symdefs = bfd_ardata (abfd)->symdefs;
5062 bed = get_elf_backend_data (abfd);
5063 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5076 symdefend = symdef + c;
5077 for (i = 0; symdef < symdefend; symdef++, i++)
5079 struct elf_link_hash_entry *h;
5081 struct bfd_link_hash_entry *undefs_tail;
5084 if (defined[i] || included[i])
5086 if (symdef->file_offset == last)
5092 h = archive_symbol_lookup (abfd, info, symdef->name);
5093 if (h == (struct elf_link_hash_entry *) 0 - 1)
5099 if (h->root.type == bfd_link_hash_common)
5101 /* We currently have a common symbol. The archive map contains
5102 a reference to this symbol, so we may want to include it. We
5103 only want to include it however, if this archive element
5104 contains a definition of the symbol, not just another common
5107 Unfortunately some archivers (including GNU ar) will put
5108 declarations of common symbols into their archive maps, as
5109 well as real definitions, so we cannot just go by the archive
5110 map alone. Instead we must read in the element's symbol
5111 table and check that to see what kind of symbol definition
5113 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5116 else if (h->root.type != bfd_link_hash_undefined)
5118 if (h->root.type != bfd_link_hash_undefweak)
5123 /* We need to include this archive member. */
5124 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5125 if (element == NULL)
5128 if (! bfd_check_format (element, bfd_object))
5131 /* Doublecheck that we have not included this object
5132 already--it should be impossible, but there may be
5133 something wrong with the archive. */
5134 if (element->archive_pass != 0)
5136 bfd_set_error (bfd_error_bad_value);
5139 element->archive_pass = 1;
5141 undefs_tail = info->hash->undefs_tail;
5143 if (!(*info->callbacks
5144 ->add_archive_element) (info, element, symdef->name, &element))
5146 if (!bfd_link_add_symbols (element, info))
5149 /* If there are any new undefined symbols, we need to make
5150 another pass through the archive in order to see whether
5151 they can be defined. FIXME: This isn't perfect, because
5152 common symbols wind up on undefs_tail and because an
5153 undefined symbol which is defined later on in this pass
5154 does not require another pass. This isn't a bug, but it
5155 does make the code less efficient than it could be. */
5156 if (undefs_tail != info->hash->undefs_tail)
5159 /* Look backward to mark all symbols from this object file
5160 which we have already seen in this pass. */
5164 included[mark] = TRUE;
5169 while (symdefs[mark].file_offset == symdef->file_offset);
5171 /* We mark subsequent symbols from this object file as we go
5172 on through the loop. */
5173 last = symdef->file_offset;
5184 if (defined != NULL)
5186 if (included != NULL)
5191 /* Given an ELF BFD, add symbols to the global hash table as
5195 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5197 switch (bfd_get_format (abfd))
5200 return elf_link_add_object_symbols (abfd, info);
5202 return elf_link_add_archive_symbols (abfd, info);
5204 bfd_set_error (bfd_error_wrong_format);
5209 struct hash_codes_info
5211 unsigned long *hashcodes;
5215 /* This function will be called though elf_link_hash_traverse to store
5216 all hash value of the exported symbols in an array. */
5219 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5221 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5227 /* Ignore indirect symbols. These are added by the versioning code. */
5228 if (h->dynindx == -1)
5231 name = h->root.root.string;
5232 p = strchr (name, ELF_VER_CHR);
5235 alc = (char *) bfd_malloc (p - name + 1);
5241 memcpy (alc, name, p - name);
5242 alc[p - name] = '\0';
5246 /* Compute the hash value. */
5247 ha = bfd_elf_hash (name);
5249 /* Store the found hash value in the array given as the argument. */
5250 *(inf->hashcodes)++ = ha;
5252 /* And store it in the struct so that we can put it in the hash table
5254 h->u.elf_hash_value = ha;
5262 struct collect_gnu_hash_codes
5265 const struct elf_backend_data *bed;
5266 unsigned long int nsyms;
5267 unsigned long int maskbits;
5268 unsigned long int *hashcodes;
5269 unsigned long int *hashval;
5270 unsigned long int *indx;
5271 unsigned long int *counts;
5274 long int min_dynindx;
5275 unsigned long int bucketcount;
5276 unsigned long int symindx;
5277 long int local_indx;
5278 long int shift1, shift2;
5279 unsigned long int mask;
5283 /* This function will be called though elf_link_hash_traverse to store
5284 all hash value of the exported symbols in an array. */
5287 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5289 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5295 /* Ignore indirect symbols. These are added by the versioning code. */
5296 if (h->dynindx == -1)
5299 /* Ignore also local symbols and undefined symbols. */
5300 if (! (*s->bed->elf_hash_symbol) (h))
5303 name = h->root.root.string;
5304 p = strchr (name, ELF_VER_CHR);
5307 alc = (char *) bfd_malloc (p - name + 1);
5313 memcpy (alc, name, p - name);
5314 alc[p - name] = '\0';
5318 /* Compute the hash value. */
5319 ha = bfd_elf_gnu_hash (name);
5321 /* Store the found hash value in the array for compute_bucket_count,
5322 and also for .dynsym reordering purposes. */
5323 s->hashcodes[s->nsyms] = ha;
5324 s->hashval[h->dynindx] = ha;
5326 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5327 s->min_dynindx = h->dynindx;
5335 /* This function will be called though elf_link_hash_traverse to do
5336 final dynaminc symbol renumbering. */
5339 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5341 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5342 unsigned long int bucket;
5343 unsigned long int val;
5345 /* Ignore indirect symbols. */
5346 if (h->dynindx == -1)
5349 /* Ignore also local symbols and undefined symbols. */
5350 if (! (*s->bed->elf_hash_symbol) (h))
5352 if (h->dynindx >= s->min_dynindx)
5353 h->dynindx = s->local_indx++;
5357 bucket = s->hashval[h->dynindx] % s->bucketcount;
5358 val = (s->hashval[h->dynindx] >> s->shift1)
5359 & ((s->maskbits >> s->shift1) - 1);
5360 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5362 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5363 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5364 if (s->counts[bucket] == 1)
5365 /* Last element terminates the chain. */
5367 bfd_put_32 (s->output_bfd, val,
5368 s->contents + (s->indx[bucket] - s->symindx) * 4);
5369 --s->counts[bucket];
5370 h->dynindx = s->indx[bucket]++;
5374 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5377 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5379 return !(h->forced_local
5380 || h->root.type == bfd_link_hash_undefined
5381 || h->root.type == bfd_link_hash_undefweak
5382 || ((h->root.type == bfd_link_hash_defined
5383 || h->root.type == bfd_link_hash_defweak)
5384 && h->root.u.def.section->output_section == NULL));
5387 /* Array used to determine the number of hash table buckets to use
5388 based on the number of symbols there are. If there are fewer than
5389 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5390 fewer than 37 we use 17 buckets, and so forth. We never use more
5391 than 32771 buckets. */
5393 static const size_t elf_buckets[] =
5395 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5399 /* Compute bucket count for hashing table. We do not use a static set
5400 of possible tables sizes anymore. Instead we determine for all
5401 possible reasonable sizes of the table the outcome (i.e., the
5402 number of collisions etc) and choose the best solution. The
5403 weighting functions are not too simple to allow the table to grow
5404 without bounds. Instead one of the weighting factors is the size.
5405 Therefore the result is always a good payoff between few collisions
5406 (= short chain lengths) and table size. */
5408 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5409 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5410 unsigned long int nsyms,
5413 size_t best_size = 0;
5414 unsigned long int i;
5416 /* We have a problem here. The following code to optimize the table
5417 size requires an integer type with more the 32 bits. If
5418 BFD_HOST_U_64_BIT is set we know about such a type. */
5419 #ifdef BFD_HOST_U_64_BIT
5424 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5425 bfd *dynobj = elf_hash_table (info)->dynobj;
5426 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5427 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5428 unsigned long int *counts;
5430 unsigned int no_improvement_count = 0;
5432 /* Possible optimization parameters: if we have NSYMS symbols we say
5433 that the hashing table must at least have NSYMS/4 and at most
5435 minsize = nsyms / 4;
5438 best_size = maxsize = nsyms * 2;
5443 if ((best_size & 31) == 0)
5447 /* Create array where we count the collisions in. We must use bfd_malloc
5448 since the size could be large. */
5450 amt *= sizeof (unsigned long int);
5451 counts = (unsigned long int *) bfd_malloc (amt);
5455 /* Compute the "optimal" size for the hash table. The criteria is a
5456 minimal chain length. The minor criteria is (of course) the size
5458 for (i = minsize; i < maxsize; ++i)
5460 /* Walk through the array of hashcodes and count the collisions. */
5461 BFD_HOST_U_64_BIT max;
5462 unsigned long int j;
5463 unsigned long int fact;
5465 if (gnu_hash && (i & 31) == 0)
5468 memset (counts, '\0', i * sizeof (unsigned long int));
5470 /* Determine how often each hash bucket is used. */
5471 for (j = 0; j < nsyms; ++j)
5472 ++counts[hashcodes[j] % i];
5474 /* For the weight function we need some information about the
5475 pagesize on the target. This is information need not be 100%
5476 accurate. Since this information is not available (so far) we
5477 define it here to a reasonable default value. If it is crucial
5478 to have a better value some day simply define this value. */
5479 # ifndef BFD_TARGET_PAGESIZE
5480 # define BFD_TARGET_PAGESIZE (4096)
5483 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5485 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5488 /* Variant 1: optimize for short chains. We add the squares
5489 of all the chain lengths (which favors many small chain
5490 over a few long chains). */
5491 for (j = 0; j < i; ++j)
5492 max += counts[j] * counts[j];
5494 /* This adds penalties for the overall size of the table. */
5495 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5498 /* Variant 2: Optimize a lot more for small table. Here we
5499 also add squares of the size but we also add penalties for
5500 empty slots (the +1 term). */
5501 for (j = 0; j < i; ++j)
5502 max += (1 + counts[j]) * (1 + counts[j]);
5504 /* The overall size of the table is considered, but not as
5505 strong as in variant 1, where it is squared. */
5506 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5510 /* Compare with current best results. */
5511 if (max < best_chlen)
5515 no_improvement_count = 0;
5517 /* PR 11843: Avoid futile long searches for the best bucket size
5518 when there are a large number of symbols. */
5519 else if (++no_improvement_count == 100)
5526 #endif /* defined (BFD_HOST_U_64_BIT) */
5528 /* This is the fallback solution if no 64bit type is available or if we
5529 are not supposed to spend much time on optimizations. We select the
5530 bucket count using a fixed set of numbers. */
5531 for (i = 0; elf_buckets[i] != 0; i++)
5533 best_size = elf_buckets[i];
5534 if (nsyms < elf_buckets[i + 1])
5537 if (gnu_hash && best_size < 2)
5544 /* Size any SHT_GROUP section for ld -r. */
5547 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5551 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5552 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5553 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5558 /* Set up the sizes and contents of the ELF dynamic sections. This is
5559 called by the ELF linker emulation before_allocation routine. We
5560 must set the sizes of the sections before the linker sets the
5561 addresses of the various sections. */
5564 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5567 const char *filter_shlib,
5569 const char *depaudit,
5570 const char * const *auxiliary_filters,
5571 struct bfd_link_info *info,
5572 asection **sinterpptr)
5574 bfd_size_type soname_indx;
5576 const struct elf_backend_data *bed;
5577 struct elf_info_failed asvinfo;
5581 soname_indx = (bfd_size_type) -1;
5583 if (!is_elf_hash_table (info->hash))
5586 bed = get_elf_backend_data (output_bfd);
5587 if (info->execstack)
5588 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5589 else if (info->noexecstack)
5590 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5594 asection *notesec = NULL;
5597 for (inputobj = info->input_bfds;
5599 inputobj = inputobj->link_next)
5604 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5606 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5609 if (s->flags & SEC_CODE)
5613 else if (bed->default_execstack)
5618 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5619 if (exec && info->relocatable
5620 && notesec->output_section != bfd_abs_section_ptr)
5621 notesec->output_section->flags |= SEC_CODE;
5625 /* Any syms created from now on start with -1 in
5626 got.refcount/offset and plt.refcount/offset. */
5627 elf_hash_table (info)->init_got_refcount
5628 = elf_hash_table (info)->init_got_offset;
5629 elf_hash_table (info)->init_plt_refcount
5630 = elf_hash_table (info)->init_plt_offset;
5632 if (info->relocatable
5633 && !_bfd_elf_size_group_sections (info))
5636 /* The backend may have to create some sections regardless of whether
5637 we're dynamic or not. */
5638 if (bed->elf_backend_always_size_sections
5639 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5642 dynobj = elf_hash_table (info)->dynobj;
5644 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5646 struct elf_info_failed eif;
5647 struct elf_link_hash_entry *h;
5649 struct bfd_elf_version_tree *t;
5650 struct bfd_elf_version_expr *d;
5652 bfd_boolean all_defined;
5654 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5655 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5659 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5661 if (soname_indx == (bfd_size_type) -1
5662 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5668 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5670 info->flags |= DF_SYMBOLIC;
5677 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5679 if (indx == (bfd_size_type) -1
5680 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5683 if (info->new_dtags)
5685 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5686 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5691 if (filter_shlib != NULL)
5695 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5696 filter_shlib, TRUE);
5697 if (indx == (bfd_size_type) -1
5698 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5702 if (auxiliary_filters != NULL)
5704 const char * const *p;
5706 for (p = auxiliary_filters; *p != NULL; p++)
5710 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5712 if (indx == (bfd_size_type) -1
5713 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5722 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5724 if (indx == (bfd_size_type) -1
5725 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5729 if (depaudit != NULL)
5733 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5735 if (indx == (bfd_size_type) -1
5736 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5743 /* If we are supposed to export all symbols into the dynamic symbol
5744 table (this is not the normal case), then do so. */
5745 if (info->export_dynamic
5746 || (info->executable && info->dynamic))
5748 elf_link_hash_traverse (elf_hash_table (info),
5749 _bfd_elf_export_symbol,
5755 /* Make all global versions with definition. */
5756 for (t = info->version_info; t != NULL; t = t->next)
5757 for (d = t->globals.list; d != NULL; d = d->next)
5758 if (!d->symver && d->literal)
5760 const char *verstr, *name;
5761 size_t namelen, verlen, newlen;
5762 char *newname, *p, leading_char;
5763 struct elf_link_hash_entry *newh;
5765 leading_char = bfd_get_symbol_leading_char (output_bfd);
5767 namelen = strlen (name) + (leading_char != '\0');
5769 verlen = strlen (verstr);
5770 newlen = namelen + verlen + 3;
5772 newname = (char *) bfd_malloc (newlen);
5773 if (newname == NULL)
5775 newname[0] = leading_char;
5776 memcpy (newname + (leading_char != '\0'), name, namelen);
5778 /* Check the hidden versioned definition. */
5779 p = newname + namelen;
5781 memcpy (p, verstr, verlen + 1);
5782 newh = elf_link_hash_lookup (elf_hash_table (info),
5783 newname, FALSE, FALSE,
5786 || (newh->root.type != bfd_link_hash_defined
5787 && newh->root.type != bfd_link_hash_defweak))
5789 /* Check the default versioned definition. */
5791 memcpy (p, verstr, verlen + 1);
5792 newh = elf_link_hash_lookup (elf_hash_table (info),
5793 newname, FALSE, FALSE,
5798 /* Mark this version if there is a definition and it is
5799 not defined in a shared object. */
5801 && !newh->def_dynamic
5802 && (newh->root.type == bfd_link_hash_defined
5803 || newh->root.type == bfd_link_hash_defweak))
5807 /* Attach all the symbols to their version information. */
5808 asvinfo.info = info;
5809 asvinfo.failed = FALSE;
5811 elf_link_hash_traverse (elf_hash_table (info),
5812 _bfd_elf_link_assign_sym_version,
5817 if (!info->allow_undefined_version)
5819 /* Check if all global versions have a definition. */
5821 for (t = info->version_info; t != NULL; t = t->next)
5822 for (d = t->globals.list; d != NULL; d = d->next)
5823 if (d->literal && !d->symver && !d->script)
5825 (*_bfd_error_handler)
5826 (_("%s: undefined version: %s"),
5827 d->pattern, t->name);
5828 all_defined = FALSE;
5833 bfd_set_error (bfd_error_bad_value);
5838 /* Find all symbols which were defined in a dynamic object and make
5839 the backend pick a reasonable value for them. */
5840 elf_link_hash_traverse (elf_hash_table (info),
5841 _bfd_elf_adjust_dynamic_symbol,
5846 /* Add some entries to the .dynamic section. We fill in some of the
5847 values later, in bfd_elf_final_link, but we must add the entries
5848 now so that we know the final size of the .dynamic section. */
5850 /* If there are initialization and/or finalization functions to
5851 call then add the corresponding DT_INIT/DT_FINI entries. */
5852 h = (info->init_function
5853 ? elf_link_hash_lookup (elf_hash_table (info),
5854 info->init_function, FALSE,
5861 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5864 h = (info->fini_function
5865 ? elf_link_hash_lookup (elf_hash_table (info),
5866 info->fini_function, FALSE,
5873 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5877 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5878 if (s != NULL && s->linker_has_input)
5880 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5881 if (! info->executable)
5886 for (sub = info->input_bfds; sub != NULL;
5887 sub = sub->link_next)
5888 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5889 for (o = sub->sections; o != NULL; o = o->next)
5890 if (elf_section_data (o)->this_hdr.sh_type
5891 == SHT_PREINIT_ARRAY)
5893 (*_bfd_error_handler)
5894 (_("%B: .preinit_array section is not allowed in DSO"),
5899 bfd_set_error (bfd_error_nonrepresentable_section);
5903 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5904 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5907 s = bfd_get_section_by_name (output_bfd, ".init_array");
5908 if (s != NULL && s->linker_has_input)
5910 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5911 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5914 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5915 if (s != NULL && s->linker_has_input)
5917 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5918 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5922 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5923 /* If .dynstr is excluded from the link, we don't want any of
5924 these tags. Strictly, we should be checking each section
5925 individually; This quick check covers for the case where
5926 someone does a /DISCARD/ : { *(*) }. */
5927 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5929 bfd_size_type strsize;
5931 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5932 if ((info->emit_hash
5933 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5934 || (info->emit_gnu_hash
5935 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5936 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5937 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5938 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5939 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5940 bed->s->sizeof_sym))
5945 /* The backend must work out the sizes of all the other dynamic
5948 && bed->elf_backend_size_dynamic_sections != NULL
5949 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5952 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5955 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5957 unsigned long section_sym_count;
5958 struct bfd_elf_version_tree *verdefs;
5961 /* Set up the version definition section. */
5962 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5963 BFD_ASSERT (s != NULL);
5965 /* We may have created additional version definitions if we are
5966 just linking a regular application. */
5967 verdefs = info->version_info;
5969 /* Skip anonymous version tag. */
5970 if (verdefs != NULL && verdefs->vernum == 0)
5971 verdefs = verdefs->next;
5973 if (verdefs == NULL && !info->create_default_symver)
5974 s->flags |= SEC_EXCLUDE;
5979 struct bfd_elf_version_tree *t;
5981 Elf_Internal_Verdef def;
5982 Elf_Internal_Verdaux defaux;
5983 struct bfd_link_hash_entry *bh;
5984 struct elf_link_hash_entry *h;
5990 /* Make space for the base version. */
5991 size += sizeof (Elf_External_Verdef);
5992 size += sizeof (Elf_External_Verdaux);
5995 /* Make space for the default version. */
5996 if (info->create_default_symver)
5998 size += sizeof (Elf_External_Verdef);
6002 for (t = verdefs; t != NULL; t = t->next)
6004 struct bfd_elf_version_deps *n;
6006 /* Don't emit base version twice. */
6010 size += sizeof (Elf_External_Verdef);
6011 size += sizeof (Elf_External_Verdaux);
6014 for (n = t->deps; n != NULL; n = n->next)
6015 size += sizeof (Elf_External_Verdaux);
6019 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6020 if (s->contents == NULL && s->size != 0)
6023 /* Fill in the version definition section. */
6027 def.vd_version = VER_DEF_CURRENT;
6028 def.vd_flags = VER_FLG_BASE;
6031 if (info->create_default_symver)
6033 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6034 def.vd_next = sizeof (Elf_External_Verdef);
6038 def.vd_aux = sizeof (Elf_External_Verdef);
6039 def.vd_next = (sizeof (Elf_External_Verdef)
6040 + sizeof (Elf_External_Verdaux));
6043 if (soname_indx != (bfd_size_type) -1)
6045 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6047 def.vd_hash = bfd_elf_hash (soname);
6048 defaux.vda_name = soname_indx;
6055 name = lbasename (output_bfd->filename);
6056 def.vd_hash = bfd_elf_hash (name);
6057 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6059 if (indx == (bfd_size_type) -1)
6061 defaux.vda_name = indx;
6063 defaux.vda_next = 0;
6065 _bfd_elf_swap_verdef_out (output_bfd, &def,
6066 (Elf_External_Verdef *) p);
6067 p += sizeof (Elf_External_Verdef);
6068 if (info->create_default_symver)
6070 /* Add a symbol representing this version. */
6072 if (! (_bfd_generic_link_add_one_symbol
6073 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6075 get_elf_backend_data (dynobj)->collect, &bh)))
6077 h = (struct elf_link_hash_entry *) bh;
6080 h->type = STT_OBJECT;
6081 h->verinfo.vertree = NULL;
6083 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6086 /* Create a duplicate of the base version with the same
6087 aux block, but different flags. */
6090 def.vd_aux = sizeof (Elf_External_Verdef);
6092 def.vd_next = (sizeof (Elf_External_Verdef)
6093 + sizeof (Elf_External_Verdaux));
6096 _bfd_elf_swap_verdef_out (output_bfd, &def,
6097 (Elf_External_Verdef *) p);
6098 p += sizeof (Elf_External_Verdef);
6100 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6101 (Elf_External_Verdaux *) p);
6102 p += sizeof (Elf_External_Verdaux);
6104 for (t = verdefs; t != NULL; t = t->next)
6107 struct bfd_elf_version_deps *n;
6109 /* Don't emit the base version twice. */
6114 for (n = t->deps; n != NULL; n = n->next)
6117 /* Add a symbol representing this version. */
6119 if (! (_bfd_generic_link_add_one_symbol
6120 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6122 get_elf_backend_data (dynobj)->collect, &bh)))
6124 h = (struct elf_link_hash_entry *) bh;
6127 h->type = STT_OBJECT;
6128 h->verinfo.vertree = t;
6130 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6133 def.vd_version = VER_DEF_CURRENT;
6135 if (t->globals.list == NULL
6136 && t->locals.list == NULL
6138 def.vd_flags |= VER_FLG_WEAK;
6139 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6140 def.vd_cnt = cdeps + 1;
6141 def.vd_hash = bfd_elf_hash (t->name);
6142 def.vd_aux = sizeof (Elf_External_Verdef);
6145 /* If a basever node is next, it *must* be the last node in
6146 the chain, otherwise Verdef construction breaks. */
6147 if (t->next != NULL && t->next->vernum == 0)
6148 BFD_ASSERT (t->next->next == NULL);
6150 if (t->next != NULL && t->next->vernum != 0)
6151 def.vd_next = (sizeof (Elf_External_Verdef)
6152 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6154 _bfd_elf_swap_verdef_out (output_bfd, &def,
6155 (Elf_External_Verdef *) p);
6156 p += sizeof (Elf_External_Verdef);
6158 defaux.vda_name = h->dynstr_index;
6159 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6161 defaux.vda_next = 0;
6162 if (t->deps != NULL)
6163 defaux.vda_next = sizeof (Elf_External_Verdaux);
6164 t->name_indx = defaux.vda_name;
6166 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6167 (Elf_External_Verdaux *) p);
6168 p += sizeof (Elf_External_Verdaux);
6170 for (n = t->deps; n != NULL; n = n->next)
6172 if (n->version_needed == NULL)
6174 /* This can happen if there was an error in the
6176 defaux.vda_name = 0;
6180 defaux.vda_name = n->version_needed->name_indx;
6181 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6184 if (n->next == NULL)
6185 defaux.vda_next = 0;
6187 defaux.vda_next = sizeof (Elf_External_Verdaux);
6189 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6190 (Elf_External_Verdaux *) p);
6191 p += sizeof (Elf_External_Verdaux);
6195 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6196 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6199 elf_tdata (output_bfd)->cverdefs = cdefs;
6202 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6204 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6207 else if (info->flags & DF_BIND_NOW)
6209 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6215 if (info->executable)
6216 info->flags_1 &= ~ (DF_1_INITFIRST
6219 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6223 /* Work out the size of the version reference section. */
6225 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6226 BFD_ASSERT (s != NULL);
6228 struct elf_find_verdep_info sinfo;
6231 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6232 if (sinfo.vers == 0)
6234 sinfo.failed = FALSE;
6236 elf_link_hash_traverse (elf_hash_table (info),
6237 _bfd_elf_link_find_version_dependencies,
6242 if (elf_tdata (output_bfd)->verref == NULL)
6243 s->flags |= SEC_EXCLUDE;
6246 Elf_Internal_Verneed *t;
6251 /* Build the version dependency section. */
6254 for (t = elf_tdata (output_bfd)->verref;
6258 Elf_Internal_Vernaux *a;
6260 size += sizeof (Elf_External_Verneed);
6262 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6263 size += sizeof (Elf_External_Vernaux);
6267 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6268 if (s->contents == NULL)
6272 for (t = elf_tdata (output_bfd)->verref;
6277 Elf_Internal_Vernaux *a;
6281 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6284 t->vn_version = VER_NEED_CURRENT;
6286 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6287 elf_dt_name (t->vn_bfd) != NULL
6288 ? elf_dt_name (t->vn_bfd)
6289 : lbasename (t->vn_bfd->filename),
6291 if (indx == (bfd_size_type) -1)
6294 t->vn_aux = sizeof (Elf_External_Verneed);
6295 if (t->vn_nextref == NULL)
6298 t->vn_next = (sizeof (Elf_External_Verneed)
6299 + caux * sizeof (Elf_External_Vernaux));
6301 _bfd_elf_swap_verneed_out (output_bfd, t,
6302 (Elf_External_Verneed *) p);
6303 p += sizeof (Elf_External_Verneed);
6305 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6307 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6308 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6309 a->vna_nodename, FALSE);
6310 if (indx == (bfd_size_type) -1)
6313 if (a->vna_nextptr == NULL)
6316 a->vna_next = sizeof (Elf_External_Vernaux);
6318 _bfd_elf_swap_vernaux_out (output_bfd, a,
6319 (Elf_External_Vernaux *) p);
6320 p += sizeof (Elf_External_Vernaux);
6324 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6325 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6328 elf_tdata (output_bfd)->cverrefs = crefs;
6332 if ((elf_tdata (output_bfd)->cverrefs == 0
6333 && elf_tdata (output_bfd)->cverdefs == 0)
6334 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6335 §ion_sym_count) == 0)
6337 s = bfd_get_linker_section (dynobj, ".gnu.version");
6338 s->flags |= SEC_EXCLUDE;
6344 /* Find the first non-excluded output section. We'll use its
6345 section symbol for some emitted relocs. */
6347 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6351 for (s = output_bfd->sections; s != NULL; s = s->next)
6352 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6353 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6355 elf_hash_table (info)->text_index_section = s;
6360 /* Find two non-excluded output sections, one for code, one for data.
6361 We'll use their section symbols for some emitted relocs. */
6363 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6367 /* Data first, since setting text_index_section changes
6368 _bfd_elf_link_omit_section_dynsym. */
6369 for (s = output_bfd->sections; s != NULL; s = s->next)
6370 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6371 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6373 elf_hash_table (info)->data_index_section = s;
6377 for (s = output_bfd->sections; s != NULL; s = s->next)
6378 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6379 == (SEC_ALLOC | SEC_READONLY))
6380 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6382 elf_hash_table (info)->text_index_section = s;
6386 if (elf_hash_table (info)->text_index_section == NULL)
6387 elf_hash_table (info)->text_index_section
6388 = elf_hash_table (info)->data_index_section;
6392 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6394 const struct elf_backend_data *bed;
6396 if (!is_elf_hash_table (info->hash))
6399 bed = get_elf_backend_data (output_bfd);
6400 (*bed->elf_backend_init_index_section) (output_bfd, info);
6402 if (elf_hash_table (info)->dynamic_sections_created)
6406 bfd_size_type dynsymcount;
6407 unsigned long section_sym_count;
6408 unsigned int dtagcount;
6410 dynobj = elf_hash_table (info)->dynobj;
6412 /* Assign dynsym indicies. In a shared library we generate a
6413 section symbol for each output section, which come first.
6414 Next come all of the back-end allocated local dynamic syms,
6415 followed by the rest of the global symbols. */
6417 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6418 §ion_sym_count);
6420 /* Work out the size of the symbol version section. */
6421 s = bfd_get_linker_section (dynobj, ".gnu.version");
6422 BFD_ASSERT (s != NULL);
6423 if (dynsymcount != 0
6424 && (s->flags & SEC_EXCLUDE) == 0)
6426 s->size = dynsymcount * sizeof (Elf_External_Versym);
6427 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6428 if (s->contents == NULL)
6431 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6435 /* Set the size of the .dynsym and .hash sections. We counted
6436 the number of dynamic symbols in elf_link_add_object_symbols.
6437 We will build the contents of .dynsym and .hash when we build
6438 the final symbol table, because until then we do not know the
6439 correct value to give the symbols. We built the .dynstr
6440 section as we went along in elf_link_add_object_symbols. */
6441 s = bfd_get_linker_section (dynobj, ".dynsym");
6442 BFD_ASSERT (s != NULL);
6443 s->size = dynsymcount * bed->s->sizeof_sym;
6445 if (dynsymcount != 0)
6447 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6448 if (s->contents == NULL)
6451 /* The first entry in .dynsym is a dummy symbol.
6452 Clear all the section syms, in case we don't output them all. */
6453 ++section_sym_count;
6454 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6457 elf_hash_table (info)->bucketcount = 0;
6459 /* Compute the size of the hashing table. As a side effect this
6460 computes the hash values for all the names we export. */
6461 if (info->emit_hash)
6463 unsigned long int *hashcodes;
6464 struct hash_codes_info hashinf;
6466 unsigned long int nsyms;
6468 size_t hash_entry_size;
6470 /* Compute the hash values for all exported symbols. At the same
6471 time store the values in an array so that we could use them for
6473 amt = dynsymcount * sizeof (unsigned long int);
6474 hashcodes = (unsigned long int *) bfd_malloc (amt);
6475 if (hashcodes == NULL)
6477 hashinf.hashcodes = hashcodes;
6478 hashinf.error = FALSE;
6480 /* Put all hash values in HASHCODES. */
6481 elf_link_hash_traverse (elf_hash_table (info),
6482 elf_collect_hash_codes, &hashinf);
6489 nsyms = hashinf.hashcodes - hashcodes;
6491 = compute_bucket_count (info, hashcodes, nsyms, 0);
6494 if (bucketcount == 0)
6497 elf_hash_table (info)->bucketcount = bucketcount;
6499 s = bfd_get_linker_section (dynobj, ".hash");
6500 BFD_ASSERT (s != NULL);
6501 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6502 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6503 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6504 if (s->contents == NULL)
6507 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6508 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6509 s->contents + hash_entry_size);
6512 if (info->emit_gnu_hash)
6515 unsigned char *contents;
6516 struct collect_gnu_hash_codes cinfo;
6520 memset (&cinfo, 0, sizeof (cinfo));
6522 /* Compute the hash values for all exported symbols. At the same
6523 time store the values in an array so that we could use them for
6525 amt = dynsymcount * 2 * sizeof (unsigned long int);
6526 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6527 if (cinfo.hashcodes == NULL)
6530 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6531 cinfo.min_dynindx = -1;
6532 cinfo.output_bfd = output_bfd;
6535 /* Put all hash values in HASHCODES. */
6536 elf_link_hash_traverse (elf_hash_table (info),
6537 elf_collect_gnu_hash_codes, &cinfo);
6540 free (cinfo.hashcodes);
6545 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6547 if (bucketcount == 0)
6549 free (cinfo.hashcodes);
6553 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6554 BFD_ASSERT (s != NULL);
6556 if (cinfo.nsyms == 0)
6558 /* Empty .gnu.hash section is special. */
6559 BFD_ASSERT (cinfo.min_dynindx == -1);
6560 free (cinfo.hashcodes);
6561 s->size = 5 * 4 + bed->s->arch_size / 8;
6562 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6563 if (contents == NULL)
6565 s->contents = contents;
6566 /* 1 empty bucket. */
6567 bfd_put_32 (output_bfd, 1, contents);
6568 /* SYMIDX above the special symbol 0. */
6569 bfd_put_32 (output_bfd, 1, contents + 4);
6570 /* Just one word for bitmask. */
6571 bfd_put_32 (output_bfd, 1, contents + 8);
6572 /* Only hash fn bloom filter. */
6573 bfd_put_32 (output_bfd, 0, contents + 12);
6574 /* No hashes are valid - empty bitmask. */
6575 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6576 /* No hashes in the only bucket. */
6577 bfd_put_32 (output_bfd, 0,
6578 contents + 16 + bed->s->arch_size / 8);
6582 unsigned long int maskwords, maskbitslog2, x;
6583 BFD_ASSERT (cinfo.min_dynindx != -1);
6587 while ((x >>= 1) != 0)
6589 if (maskbitslog2 < 3)
6591 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6592 maskbitslog2 = maskbitslog2 + 3;
6594 maskbitslog2 = maskbitslog2 + 2;
6595 if (bed->s->arch_size == 64)
6597 if (maskbitslog2 == 5)
6603 cinfo.mask = (1 << cinfo.shift1) - 1;
6604 cinfo.shift2 = maskbitslog2;
6605 cinfo.maskbits = 1 << maskbitslog2;
6606 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6607 amt = bucketcount * sizeof (unsigned long int) * 2;
6608 amt += maskwords * sizeof (bfd_vma);
6609 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6610 if (cinfo.bitmask == NULL)
6612 free (cinfo.hashcodes);
6616 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6617 cinfo.indx = cinfo.counts + bucketcount;
6618 cinfo.symindx = dynsymcount - cinfo.nsyms;
6619 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6621 /* Determine how often each hash bucket is used. */
6622 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6623 for (i = 0; i < cinfo.nsyms; ++i)
6624 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6626 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6627 if (cinfo.counts[i] != 0)
6629 cinfo.indx[i] = cnt;
6630 cnt += cinfo.counts[i];
6632 BFD_ASSERT (cnt == dynsymcount);
6633 cinfo.bucketcount = bucketcount;
6634 cinfo.local_indx = cinfo.min_dynindx;
6636 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6637 s->size += cinfo.maskbits / 8;
6638 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6639 if (contents == NULL)
6641 free (cinfo.bitmask);
6642 free (cinfo.hashcodes);
6646 s->contents = contents;
6647 bfd_put_32 (output_bfd, bucketcount, contents);
6648 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6649 bfd_put_32 (output_bfd, maskwords, contents + 8);
6650 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6651 contents += 16 + cinfo.maskbits / 8;
6653 for (i = 0; i < bucketcount; ++i)
6655 if (cinfo.counts[i] == 0)
6656 bfd_put_32 (output_bfd, 0, contents);
6658 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6662 cinfo.contents = contents;
6664 /* Renumber dynamic symbols, populate .gnu.hash section. */
6665 elf_link_hash_traverse (elf_hash_table (info),
6666 elf_renumber_gnu_hash_syms, &cinfo);
6668 contents = s->contents + 16;
6669 for (i = 0; i < maskwords; ++i)
6671 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6673 contents += bed->s->arch_size / 8;
6676 free (cinfo.bitmask);
6677 free (cinfo.hashcodes);
6681 s = bfd_get_linker_section (dynobj, ".dynstr");
6682 BFD_ASSERT (s != NULL);
6684 elf_finalize_dynstr (output_bfd, info);
6686 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6688 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6689 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6696 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6699 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6702 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6703 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6706 /* Finish SHF_MERGE section merging. */
6709 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6714 if (!is_elf_hash_table (info->hash))
6717 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6718 if ((ibfd->flags & DYNAMIC) == 0)
6719 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6720 if ((sec->flags & SEC_MERGE) != 0
6721 && !bfd_is_abs_section (sec->output_section))
6723 struct bfd_elf_section_data *secdata;
6725 secdata = elf_section_data (sec);
6726 if (! _bfd_add_merge_section (abfd,
6727 &elf_hash_table (info)->merge_info,
6728 sec, &secdata->sec_info))
6730 else if (secdata->sec_info)
6731 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6734 if (elf_hash_table (info)->merge_info != NULL)
6735 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6736 merge_sections_remove_hook);
6740 /* Create an entry in an ELF linker hash table. */
6742 struct bfd_hash_entry *
6743 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6744 struct bfd_hash_table *table,
6747 /* Allocate the structure if it has not already been allocated by a
6751 entry = (struct bfd_hash_entry *)
6752 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6757 /* Call the allocation method of the superclass. */
6758 entry = _bfd_link_hash_newfunc (entry, table, string);
6761 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6762 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6764 /* Set local fields. */
6767 ret->got = htab->init_got_refcount;
6768 ret->plt = htab->init_plt_refcount;
6769 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6770 - offsetof (struct elf_link_hash_entry, size)));
6771 /* Assume that we have been called by a non-ELF symbol reader.
6772 This flag is then reset by the code which reads an ELF input
6773 file. This ensures that a symbol created by a non-ELF symbol
6774 reader will have the flag set correctly. */
6781 /* Copy data from an indirect symbol to its direct symbol, hiding the
6782 old indirect symbol. Also used for copying flags to a weakdef. */
6785 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6786 struct elf_link_hash_entry *dir,
6787 struct elf_link_hash_entry *ind)
6789 struct elf_link_hash_table *htab;
6791 /* Copy down any references that we may have already seen to the
6792 symbol which just became indirect. */
6794 dir->ref_dynamic |= ind->ref_dynamic;
6795 dir->ref_regular |= ind->ref_regular;
6796 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6797 dir->non_got_ref |= ind->non_got_ref;
6798 dir->needs_plt |= ind->needs_plt;
6799 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6801 if (ind->root.type != bfd_link_hash_indirect)
6804 /* Copy over the global and procedure linkage table refcount entries.
6805 These may have been already set up by a check_relocs routine. */
6806 htab = elf_hash_table (info);
6807 if (ind->got.refcount > htab->init_got_refcount.refcount)
6809 if (dir->got.refcount < 0)
6810 dir->got.refcount = 0;
6811 dir->got.refcount += ind->got.refcount;
6812 ind->got.refcount = htab->init_got_refcount.refcount;
6815 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6817 if (dir->plt.refcount < 0)
6818 dir->plt.refcount = 0;
6819 dir->plt.refcount += ind->plt.refcount;
6820 ind->plt.refcount = htab->init_plt_refcount.refcount;
6823 if (ind->dynindx != -1)
6825 if (dir->dynindx != -1)
6826 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6827 dir->dynindx = ind->dynindx;
6828 dir->dynstr_index = ind->dynstr_index;
6830 ind->dynstr_index = 0;
6835 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6836 struct elf_link_hash_entry *h,
6837 bfd_boolean force_local)
6839 /* STT_GNU_IFUNC symbol must go through PLT. */
6840 if (h->type != STT_GNU_IFUNC)
6842 h->plt = elf_hash_table (info)->init_plt_offset;
6847 h->forced_local = 1;
6848 if (h->dynindx != -1)
6851 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6857 /* Initialize an ELF linker hash table. */
6860 _bfd_elf_link_hash_table_init
6861 (struct elf_link_hash_table *table,
6863 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6864 struct bfd_hash_table *,
6866 unsigned int entsize,
6867 enum elf_target_id target_id)
6870 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6872 memset (table, 0, sizeof * table);
6873 table->init_got_refcount.refcount = can_refcount - 1;
6874 table->init_plt_refcount.refcount = can_refcount - 1;
6875 table->init_got_offset.offset = -(bfd_vma) 1;
6876 table->init_plt_offset.offset = -(bfd_vma) 1;
6877 /* The first dynamic symbol is a dummy. */
6878 table->dynsymcount = 1;
6880 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6882 table->root.type = bfd_link_elf_hash_table;
6883 table->hash_table_id = target_id;
6888 /* Create an ELF linker hash table. */
6890 struct bfd_link_hash_table *
6891 _bfd_elf_link_hash_table_create (bfd *abfd)
6893 struct elf_link_hash_table *ret;
6894 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6896 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
6900 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6901 sizeof (struct elf_link_hash_entry),
6911 /* This is a hook for the ELF emulation code in the generic linker to
6912 tell the backend linker what file name to use for the DT_NEEDED
6913 entry for a dynamic object. */
6916 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6918 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6919 && bfd_get_format (abfd) == bfd_object)
6920 elf_dt_name (abfd) = name;
6924 bfd_elf_get_dyn_lib_class (bfd *abfd)
6927 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6928 && bfd_get_format (abfd) == bfd_object)
6929 lib_class = elf_dyn_lib_class (abfd);
6936 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6938 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6939 && bfd_get_format (abfd) == bfd_object)
6940 elf_dyn_lib_class (abfd) = lib_class;
6943 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6944 the linker ELF emulation code. */
6946 struct bfd_link_needed_list *
6947 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6948 struct bfd_link_info *info)
6950 if (! is_elf_hash_table (info->hash))
6952 return elf_hash_table (info)->needed;
6955 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6956 hook for the linker ELF emulation code. */
6958 struct bfd_link_needed_list *
6959 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6960 struct bfd_link_info *info)
6962 if (! is_elf_hash_table (info->hash))
6964 return elf_hash_table (info)->runpath;
6967 /* Get the name actually used for a dynamic object for a link. This
6968 is the SONAME entry if there is one. Otherwise, it is the string
6969 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6972 bfd_elf_get_dt_soname (bfd *abfd)
6974 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6975 && bfd_get_format (abfd) == bfd_object)
6976 return elf_dt_name (abfd);
6980 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6981 the ELF linker emulation code. */
6984 bfd_elf_get_bfd_needed_list (bfd *abfd,
6985 struct bfd_link_needed_list **pneeded)
6988 bfd_byte *dynbuf = NULL;
6989 unsigned int elfsec;
6990 unsigned long shlink;
6991 bfd_byte *extdyn, *extdynend;
6993 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6997 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6998 || bfd_get_format (abfd) != bfd_object)
7001 s = bfd_get_section_by_name (abfd, ".dynamic");
7002 if (s == NULL || s->size == 0)
7005 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7008 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7009 if (elfsec == SHN_BAD)
7012 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7014 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7015 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7018 extdynend = extdyn + s->size;
7019 for (; extdyn < extdynend; extdyn += extdynsize)
7021 Elf_Internal_Dyn dyn;
7023 (*swap_dyn_in) (abfd, extdyn, &dyn);
7025 if (dyn.d_tag == DT_NULL)
7028 if (dyn.d_tag == DT_NEEDED)
7031 struct bfd_link_needed_list *l;
7032 unsigned int tagv = dyn.d_un.d_val;
7035 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7040 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7061 struct elf_symbuf_symbol
7063 unsigned long st_name; /* Symbol name, index in string tbl */
7064 unsigned char st_info; /* Type and binding attributes */
7065 unsigned char st_other; /* Visibilty, and target specific */
7068 struct elf_symbuf_head
7070 struct elf_symbuf_symbol *ssym;
7071 bfd_size_type count;
7072 unsigned int st_shndx;
7079 Elf_Internal_Sym *isym;
7080 struct elf_symbuf_symbol *ssym;
7085 /* Sort references to symbols by ascending section number. */
7088 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7090 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7091 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7093 return s1->st_shndx - s2->st_shndx;
7097 elf_sym_name_compare (const void *arg1, const void *arg2)
7099 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7100 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7101 return strcmp (s1->name, s2->name);
7104 static struct elf_symbuf_head *
7105 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7107 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7108 struct elf_symbuf_symbol *ssym;
7109 struct elf_symbuf_head *ssymbuf, *ssymhead;
7110 bfd_size_type i, shndx_count, total_size;
7112 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7116 for (ind = indbuf, i = 0; i < symcount; i++)
7117 if (isymbuf[i].st_shndx != SHN_UNDEF)
7118 *ind++ = &isymbuf[i];
7121 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7122 elf_sort_elf_symbol);
7125 if (indbufend > indbuf)
7126 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7127 if (ind[0]->st_shndx != ind[1]->st_shndx)
7130 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7131 + (indbufend - indbuf) * sizeof (*ssym));
7132 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7133 if (ssymbuf == NULL)
7139 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7140 ssymbuf->ssym = NULL;
7141 ssymbuf->count = shndx_count;
7142 ssymbuf->st_shndx = 0;
7143 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7145 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7148 ssymhead->ssym = ssym;
7149 ssymhead->count = 0;
7150 ssymhead->st_shndx = (*ind)->st_shndx;
7152 ssym->st_name = (*ind)->st_name;
7153 ssym->st_info = (*ind)->st_info;
7154 ssym->st_other = (*ind)->st_other;
7157 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7158 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7165 /* Check if 2 sections define the same set of local and global
7169 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7170 struct bfd_link_info *info)
7173 const struct elf_backend_data *bed1, *bed2;
7174 Elf_Internal_Shdr *hdr1, *hdr2;
7175 bfd_size_type symcount1, symcount2;
7176 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7177 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7178 Elf_Internal_Sym *isym, *isymend;
7179 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7180 bfd_size_type count1, count2, i;
7181 unsigned int shndx1, shndx2;
7187 /* Both sections have to be in ELF. */
7188 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7189 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7192 if (elf_section_type (sec1) != elf_section_type (sec2))
7195 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7196 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7197 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7200 bed1 = get_elf_backend_data (bfd1);
7201 bed2 = get_elf_backend_data (bfd2);
7202 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7203 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7204 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7205 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7207 if (symcount1 == 0 || symcount2 == 0)
7213 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7214 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7216 if (ssymbuf1 == NULL)
7218 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7220 if (isymbuf1 == NULL)
7223 if (!info->reduce_memory_overheads)
7224 elf_tdata (bfd1)->symbuf = ssymbuf1
7225 = elf_create_symbuf (symcount1, isymbuf1);
7228 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7230 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7232 if (isymbuf2 == NULL)
7235 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7236 elf_tdata (bfd2)->symbuf = ssymbuf2
7237 = elf_create_symbuf (symcount2, isymbuf2);
7240 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7242 /* Optimized faster version. */
7243 bfd_size_type lo, hi, mid;
7244 struct elf_symbol *symp;
7245 struct elf_symbuf_symbol *ssym, *ssymend;
7248 hi = ssymbuf1->count;
7253 mid = (lo + hi) / 2;
7254 if (shndx1 < ssymbuf1[mid].st_shndx)
7256 else if (shndx1 > ssymbuf1[mid].st_shndx)
7260 count1 = ssymbuf1[mid].count;
7267 hi = ssymbuf2->count;
7272 mid = (lo + hi) / 2;
7273 if (shndx2 < ssymbuf2[mid].st_shndx)
7275 else if (shndx2 > ssymbuf2[mid].st_shndx)
7279 count2 = ssymbuf2[mid].count;
7285 if (count1 == 0 || count2 == 0 || count1 != count2)
7288 symtable1 = (struct elf_symbol *)
7289 bfd_malloc (count1 * sizeof (struct elf_symbol));
7290 symtable2 = (struct elf_symbol *)
7291 bfd_malloc (count2 * sizeof (struct elf_symbol));
7292 if (symtable1 == NULL || symtable2 == NULL)
7296 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7297 ssym < ssymend; ssym++, symp++)
7299 symp->u.ssym = ssym;
7300 symp->name = bfd_elf_string_from_elf_section (bfd1,
7306 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7307 ssym < ssymend; ssym++, symp++)
7309 symp->u.ssym = ssym;
7310 symp->name = bfd_elf_string_from_elf_section (bfd2,
7315 /* Sort symbol by name. */
7316 qsort (symtable1, count1, sizeof (struct elf_symbol),
7317 elf_sym_name_compare);
7318 qsort (symtable2, count1, sizeof (struct elf_symbol),
7319 elf_sym_name_compare);
7321 for (i = 0; i < count1; i++)
7322 /* Two symbols must have the same binding, type and name. */
7323 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7324 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7325 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7332 symtable1 = (struct elf_symbol *)
7333 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7334 symtable2 = (struct elf_symbol *)
7335 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7336 if (symtable1 == NULL || symtable2 == NULL)
7339 /* Count definitions in the section. */
7341 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7342 if (isym->st_shndx == shndx1)
7343 symtable1[count1++].u.isym = isym;
7346 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7347 if (isym->st_shndx == shndx2)
7348 symtable2[count2++].u.isym = isym;
7350 if (count1 == 0 || count2 == 0 || count1 != count2)
7353 for (i = 0; i < count1; i++)
7355 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7356 symtable1[i].u.isym->st_name);
7358 for (i = 0; i < count2; i++)
7360 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7361 symtable2[i].u.isym->st_name);
7363 /* Sort symbol by name. */
7364 qsort (symtable1, count1, sizeof (struct elf_symbol),
7365 elf_sym_name_compare);
7366 qsort (symtable2, count1, sizeof (struct elf_symbol),
7367 elf_sym_name_compare);
7369 for (i = 0; i < count1; i++)
7370 /* Two symbols must have the same binding, type and name. */
7371 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7372 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7373 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7391 /* Return TRUE if 2 section types are compatible. */
7394 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7395 bfd *bbfd, const asection *bsec)
7399 || abfd->xvec->flavour != bfd_target_elf_flavour
7400 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7403 return elf_section_type (asec) == elf_section_type (bsec);
7406 /* Final phase of ELF linker. */
7408 /* A structure we use to avoid passing large numbers of arguments. */
7410 struct elf_final_link_info
7412 /* General link information. */
7413 struct bfd_link_info *info;
7416 /* Symbol string table. */
7417 struct bfd_strtab_hash *symstrtab;
7418 /* .dynsym section. */
7419 asection *dynsym_sec;
7420 /* .hash section. */
7422 /* symbol version section (.gnu.version). */
7423 asection *symver_sec;
7424 /* Buffer large enough to hold contents of any section. */
7426 /* Buffer large enough to hold external relocs of any section. */
7427 void *external_relocs;
7428 /* Buffer large enough to hold internal relocs of any section. */
7429 Elf_Internal_Rela *internal_relocs;
7430 /* Buffer large enough to hold external local symbols of any input
7432 bfd_byte *external_syms;
7433 /* And a buffer for symbol section indices. */
7434 Elf_External_Sym_Shndx *locsym_shndx;
7435 /* Buffer large enough to hold internal local symbols of any input
7437 Elf_Internal_Sym *internal_syms;
7438 /* Array large enough to hold a symbol index for each local symbol
7439 of any input BFD. */
7441 /* Array large enough to hold a section pointer for each local
7442 symbol of any input BFD. */
7443 asection **sections;
7444 /* Buffer to hold swapped out symbols. */
7446 /* And one for symbol section indices. */
7447 Elf_External_Sym_Shndx *symshndxbuf;
7448 /* Number of swapped out symbols in buffer. */
7449 size_t symbuf_count;
7450 /* Number of symbols which fit in symbuf. */
7452 /* And same for symshndxbuf. */
7453 size_t shndxbuf_size;
7454 /* Number of STT_FILE syms seen. */
7455 size_t filesym_count;
7458 /* This struct is used to pass information to elf_link_output_extsym. */
7460 struct elf_outext_info
7463 bfd_boolean localsyms;
7464 bfd_boolean need_second_pass;
7465 bfd_boolean second_pass;
7466 struct elf_final_link_info *flinfo;
7470 /* Support for evaluating a complex relocation.
7472 Complex relocations are generalized, self-describing relocations. The
7473 implementation of them consists of two parts: complex symbols, and the
7474 relocations themselves.
7476 The relocations are use a reserved elf-wide relocation type code (R_RELC
7477 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7478 information (start bit, end bit, word width, etc) into the addend. This
7479 information is extracted from CGEN-generated operand tables within gas.
7481 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7482 internal) representing prefix-notation expressions, including but not
7483 limited to those sorts of expressions normally encoded as addends in the
7484 addend field. The symbol mangling format is:
7487 | <unary-operator> ':' <node>
7488 | <binary-operator> ':' <node> ':' <node>
7491 <literal> := 's' <digits=N> ':' <N character symbol name>
7492 | 'S' <digits=N> ':' <N character section name>
7496 <binary-operator> := as in C
7497 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7500 set_symbol_value (bfd *bfd_with_globals,
7501 Elf_Internal_Sym *isymbuf,
7506 struct elf_link_hash_entry **sym_hashes;
7507 struct elf_link_hash_entry *h;
7508 size_t extsymoff = locsymcount;
7510 if (symidx < locsymcount)
7512 Elf_Internal_Sym *sym;
7514 sym = isymbuf + symidx;
7515 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7517 /* It is a local symbol: move it to the
7518 "absolute" section and give it a value. */
7519 sym->st_shndx = SHN_ABS;
7520 sym->st_value = val;
7523 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7527 /* It is a global symbol: set its link type
7528 to "defined" and give it a value. */
7530 sym_hashes = elf_sym_hashes (bfd_with_globals);
7531 h = sym_hashes [symidx - extsymoff];
7532 while (h->root.type == bfd_link_hash_indirect
7533 || h->root.type == bfd_link_hash_warning)
7534 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7535 h->root.type = bfd_link_hash_defined;
7536 h->root.u.def.value = val;
7537 h->root.u.def.section = bfd_abs_section_ptr;
7541 resolve_symbol (const char *name,
7543 struct elf_final_link_info *flinfo,
7545 Elf_Internal_Sym *isymbuf,
7548 Elf_Internal_Sym *sym;
7549 struct bfd_link_hash_entry *global_entry;
7550 const char *candidate = NULL;
7551 Elf_Internal_Shdr *symtab_hdr;
7554 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7556 for (i = 0; i < locsymcount; ++ i)
7560 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7563 candidate = bfd_elf_string_from_elf_section (input_bfd,
7564 symtab_hdr->sh_link,
7567 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7568 name, candidate, (unsigned long) sym->st_value);
7570 if (candidate && strcmp (candidate, name) == 0)
7572 asection *sec = flinfo->sections [i];
7574 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7575 *result += sec->output_offset + sec->output_section->vma;
7577 printf ("Found symbol with value %8.8lx\n",
7578 (unsigned long) *result);
7584 /* Hmm, haven't found it yet. perhaps it is a global. */
7585 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7586 FALSE, FALSE, TRUE);
7590 if (global_entry->type == bfd_link_hash_defined
7591 || global_entry->type == bfd_link_hash_defweak)
7593 *result = (global_entry->u.def.value
7594 + global_entry->u.def.section->output_section->vma
7595 + global_entry->u.def.section->output_offset);
7597 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7598 global_entry->root.string, (unsigned long) *result);
7607 resolve_section (const char *name,
7614 for (curr = sections; curr; curr = curr->next)
7615 if (strcmp (curr->name, name) == 0)
7617 *result = curr->vma;
7621 /* Hmm. still haven't found it. try pseudo-section names. */
7622 for (curr = sections; curr; curr = curr->next)
7624 len = strlen (curr->name);
7625 if (len > strlen (name))
7628 if (strncmp (curr->name, name, len) == 0)
7630 if (strncmp (".end", name + len, 4) == 0)
7632 *result = curr->vma + curr->size;
7636 /* Insert more pseudo-section names here, if you like. */
7644 undefined_reference (const char *reftype, const char *name)
7646 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7651 eval_symbol (bfd_vma *result,
7654 struct elf_final_link_info *flinfo,
7656 Elf_Internal_Sym *isymbuf,
7665 const char *sym = *symp;
7667 bfd_boolean symbol_is_section = FALSE;
7672 if (len < 1 || len > sizeof (symbuf))
7674 bfd_set_error (bfd_error_invalid_operation);
7687 *result = strtoul (sym, (char **) symp, 16);
7691 symbol_is_section = TRUE;
7694 symlen = strtol (sym, (char **) symp, 10);
7695 sym = *symp + 1; /* Skip the trailing ':'. */
7697 if (symend < sym || symlen + 1 > sizeof (symbuf))
7699 bfd_set_error (bfd_error_invalid_operation);
7703 memcpy (symbuf, sym, symlen);
7704 symbuf[symlen] = '\0';
7705 *symp = sym + symlen;
7707 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7708 the symbol as a section, or vice-versa. so we're pretty liberal in our
7709 interpretation here; section means "try section first", not "must be a
7710 section", and likewise with symbol. */
7712 if (symbol_is_section)
7714 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7715 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7716 isymbuf, locsymcount))
7718 undefined_reference ("section", symbuf);
7724 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7725 isymbuf, locsymcount)
7726 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7729 undefined_reference ("symbol", symbuf);
7736 /* All that remains are operators. */
7738 #define UNARY_OP(op) \
7739 if (strncmp (sym, #op, strlen (#op)) == 0) \
7741 sym += strlen (#op); \
7745 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7746 isymbuf, locsymcount, signed_p)) \
7749 *result = op ((bfd_signed_vma) a); \
7755 #define BINARY_OP(op) \
7756 if (strncmp (sym, #op, strlen (#op)) == 0) \
7758 sym += strlen (#op); \
7762 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7763 isymbuf, locsymcount, signed_p)) \
7766 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7767 isymbuf, locsymcount, signed_p)) \
7770 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7800 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7801 bfd_set_error (bfd_error_invalid_operation);
7807 put_value (bfd_vma size,
7808 unsigned long chunksz,
7813 location += (size - chunksz);
7815 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7823 bfd_put_8 (input_bfd, x, location);
7826 bfd_put_16 (input_bfd, x, location);
7829 bfd_put_32 (input_bfd, x, location);
7833 bfd_put_64 (input_bfd, x, location);
7843 get_value (bfd_vma size,
7844 unsigned long chunksz,
7850 for (; size; size -= chunksz, location += chunksz)
7858 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7861 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7864 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7868 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7879 decode_complex_addend (unsigned long *start, /* in bits */
7880 unsigned long *oplen, /* in bits */
7881 unsigned long *len, /* in bits */
7882 unsigned long *wordsz, /* in bytes */
7883 unsigned long *chunksz, /* in bytes */
7884 unsigned long *lsb0_p,
7885 unsigned long *signed_p,
7886 unsigned long *trunc_p,
7887 unsigned long encoded)
7889 * start = encoded & 0x3F;
7890 * len = (encoded >> 6) & 0x3F;
7891 * oplen = (encoded >> 12) & 0x3F;
7892 * wordsz = (encoded >> 18) & 0xF;
7893 * chunksz = (encoded >> 22) & 0xF;
7894 * lsb0_p = (encoded >> 27) & 1;
7895 * signed_p = (encoded >> 28) & 1;
7896 * trunc_p = (encoded >> 29) & 1;
7899 bfd_reloc_status_type
7900 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7901 asection *input_section ATTRIBUTE_UNUSED,
7903 Elf_Internal_Rela *rel,
7906 bfd_vma shift, x, mask;
7907 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7908 bfd_reloc_status_type r;
7910 /* Perform this reloc, since it is complex.
7911 (this is not to say that it necessarily refers to a complex
7912 symbol; merely that it is a self-describing CGEN based reloc.
7913 i.e. the addend has the complete reloc information (bit start, end,
7914 word size, etc) encoded within it.). */
7916 decode_complex_addend (&start, &oplen, &len, &wordsz,
7917 &chunksz, &lsb0_p, &signed_p,
7918 &trunc_p, rel->r_addend);
7920 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7923 shift = (start + 1) - len;
7925 shift = (8 * wordsz) - (start + len);
7927 /* FIXME: octets_per_byte. */
7928 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7931 printf ("Doing complex reloc: "
7932 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7933 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7934 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7935 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7936 oplen, (unsigned long) x, (unsigned long) mask,
7937 (unsigned long) relocation);
7942 /* Now do an overflow check. */
7943 r = bfd_check_overflow ((signed_p
7944 ? complain_overflow_signed
7945 : complain_overflow_unsigned),
7946 len, 0, (8 * wordsz),
7950 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7953 printf (" relocation: %8.8lx\n"
7954 " shifted mask: %8.8lx\n"
7955 " shifted/masked reloc: %8.8lx\n"
7956 " result: %8.8lx\n",
7957 (unsigned long) relocation, (unsigned long) (mask << shift),
7958 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
7960 /* FIXME: octets_per_byte. */
7961 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7965 /* When performing a relocatable link, the input relocations are
7966 preserved. But, if they reference global symbols, the indices
7967 referenced must be updated. Update all the relocations found in
7971 elf_link_adjust_relocs (bfd *abfd,
7972 struct bfd_elf_section_reloc_data *reldata)
7975 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7977 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7978 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7979 bfd_vma r_type_mask;
7981 unsigned int count = reldata->count;
7982 struct elf_link_hash_entry **rel_hash = reldata->hashes;
7984 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
7986 swap_in = bed->s->swap_reloc_in;
7987 swap_out = bed->s->swap_reloc_out;
7989 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
7991 swap_in = bed->s->swap_reloca_in;
7992 swap_out = bed->s->swap_reloca_out;
7997 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8000 if (bed->s->arch_size == 32)
8007 r_type_mask = 0xffffffff;
8011 erela = reldata->hdr->contents;
8012 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8014 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8017 if (*rel_hash == NULL)
8020 BFD_ASSERT ((*rel_hash)->indx >= 0);
8022 (*swap_in) (abfd, erela, irela);
8023 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8024 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8025 | (irela[j].r_info & r_type_mask));
8026 (*swap_out) (abfd, irela, erela);
8030 struct elf_link_sort_rela
8036 enum elf_reloc_type_class type;
8037 /* We use this as an array of size int_rels_per_ext_rel. */
8038 Elf_Internal_Rela rela[1];
8042 elf_link_sort_cmp1 (const void *A, const void *B)
8044 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8045 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8046 int relativea, relativeb;
8048 relativea = a->type == reloc_class_relative;
8049 relativeb = b->type == reloc_class_relative;
8051 if (relativea < relativeb)
8053 if (relativea > relativeb)
8055 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8057 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8059 if (a->rela->r_offset < b->rela->r_offset)
8061 if (a->rela->r_offset > b->rela->r_offset)
8067 elf_link_sort_cmp2 (const void *A, const void *B)
8069 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8070 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8073 if (a->u.offset < b->u.offset)
8075 if (a->u.offset > b->u.offset)
8077 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8078 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8083 if (a->rela->r_offset < b->rela->r_offset)
8085 if (a->rela->r_offset > b->rela->r_offset)
8091 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8093 asection *dynamic_relocs;
8096 bfd_size_type count, size;
8097 size_t i, ret, sort_elt, ext_size;
8098 bfd_byte *sort, *s_non_relative, *p;
8099 struct elf_link_sort_rela *sq;
8100 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8101 int i2e = bed->s->int_rels_per_ext_rel;
8102 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8103 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8104 struct bfd_link_order *lo;
8106 bfd_boolean use_rela;
8108 /* Find a dynamic reloc section. */
8109 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8110 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8111 if (rela_dyn != NULL && rela_dyn->size > 0
8112 && rel_dyn != NULL && rel_dyn->size > 0)
8114 bfd_boolean use_rela_initialised = FALSE;
8116 /* This is just here to stop gcc from complaining.
8117 It's initialization checking code is not perfect. */
8120 /* Both sections are present. Examine the sizes
8121 of the indirect sections to help us choose. */
8122 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8123 if (lo->type == bfd_indirect_link_order)
8125 asection *o = lo->u.indirect.section;
8127 if ((o->size % bed->s->sizeof_rela) == 0)
8129 if ((o->size % bed->s->sizeof_rel) == 0)
8130 /* Section size is divisible by both rel and rela sizes.
8131 It is of no help to us. */
8135 /* Section size is only divisible by rela. */
8136 if (use_rela_initialised && (use_rela == FALSE))
8139 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8140 bfd_set_error (bfd_error_invalid_operation);
8146 use_rela_initialised = TRUE;
8150 else if ((o->size % bed->s->sizeof_rel) == 0)
8152 /* Section size is only divisible by rel. */
8153 if (use_rela_initialised && (use_rela == TRUE))
8156 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8157 bfd_set_error (bfd_error_invalid_operation);
8163 use_rela_initialised = TRUE;
8168 /* The section size is not divisible by either - something is wrong. */
8170 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8171 bfd_set_error (bfd_error_invalid_operation);
8176 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8177 if (lo->type == bfd_indirect_link_order)
8179 asection *o = lo->u.indirect.section;
8181 if ((o->size % bed->s->sizeof_rela) == 0)
8183 if ((o->size % bed->s->sizeof_rel) == 0)
8184 /* Section size is divisible by both rel and rela sizes.
8185 It is of no help to us. */
8189 /* Section size is only divisible by rela. */
8190 if (use_rela_initialised && (use_rela == FALSE))
8193 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8194 bfd_set_error (bfd_error_invalid_operation);
8200 use_rela_initialised = TRUE;
8204 else if ((o->size % bed->s->sizeof_rel) == 0)
8206 /* Section size is only divisible by rel. */
8207 if (use_rela_initialised && (use_rela == TRUE))
8210 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8211 bfd_set_error (bfd_error_invalid_operation);
8217 use_rela_initialised = TRUE;
8222 /* The section size is not divisible by either - something is wrong. */
8224 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8225 bfd_set_error (bfd_error_invalid_operation);
8230 if (! use_rela_initialised)
8234 else if (rela_dyn != NULL && rela_dyn->size > 0)
8236 else if (rel_dyn != NULL && rel_dyn->size > 0)
8243 dynamic_relocs = rela_dyn;
8244 ext_size = bed->s->sizeof_rela;
8245 swap_in = bed->s->swap_reloca_in;
8246 swap_out = bed->s->swap_reloca_out;
8250 dynamic_relocs = rel_dyn;
8251 ext_size = bed->s->sizeof_rel;
8252 swap_in = bed->s->swap_reloc_in;
8253 swap_out = bed->s->swap_reloc_out;
8257 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8258 if (lo->type == bfd_indirect_link_order)
8259 size += lo->u.indirect.section->size;
8261 if (size != dynamic_relocs->size)
8264 sort_elt = (sizeof (struct elf_link_sort_rela)
8265 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8267 count = dynamic_relocs->size / ext_size;
8270 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8274 (*info->callbacks->warning)
8275 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8279 if (bed->s->arch_size == 32)
8280 r_sym_mask = ~(bfd_vma) 0xff;
8282 r_sym_mask = ~(bfd_vma) 0xffffffff;
8284 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8285 if (lo->type == bfd_indirect_link_order)
8287 bfd_byte *erel, *erelend;
8288 asection *o = lo->u.indirect.section;
8290 if (o->contents == NULL && o->size != 0)
8292 /* This is a reloc section that is being handled as a normal
8293 section. See bfd_section_from_shdr. We can't combine
8294 relocs in this case. */
8299 erelend = o->contents + o->size;
8300 /* FIXME: octets_per_byte. */
8301 p = sort + o->output_offset / ext_size * sort_elt;
8303 while (erel < erelend)
8305 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8307 (*swap_in) (abfd, erel, s->rela);
8308 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8309 s->u.sym_mask = r_sym_mask;
8315 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8317 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8319 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8320 if (s->type != reloc_class_relative)
8326 sq = (struct elf_link_sort_rela *) s_non_relative;
8327 for (; i < count; i++, p += sort_elt)
8329 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8330 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8332 sp->u.offset = sq->rela->r_offset;
8335 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8337 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8338 if (lo->type == bfd_indirect_link_order)
8340 bfd_byte *erel, *erelend;
8341 asection *o = lo->u.indirect.section;
8344 erelend = o->contents + o->size;
8345 /* FIXME: octets_per_byte. */
8346 p = sort + o->output_offset / ext_size * sort_elt;
8347 while (erel < erelend)
8349 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8350 (*swap_out) (abfd, s->rela, erel);
8357 *psec = dynamic_relocs;
8361 /* Flush the output symbols to the file. */
8364 elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
8365 const struct elf_backend_data *bed)
8367 if (flinfo->symbuf_count > 0)
8369 Elf_Internal_Shdr *hdr;
8373 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8374 pos = hdr->sh_offset + hdr->sh_size;
8375 amt = flinfo->symbuf_count * bed->s->sizeof_sym;
8376 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
8377 || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
8380 hdr->sh_size += amt;
8381 flinfo->symbuf_count = 0;
8387 /* Add a symbol to the output symbol table. */
8390 elf_link_output_sym (struct elf_final_link_info *flinfo,
8392 Elf_Internal_Sym *elfsym,
8393 asection *input_sec,
8394 struct elf_link_hash_entry *h)
8397 Elf_External_Sym_Shndx *destshndx;
8398 int (*output_symbol_hook)
8399 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8400 struct elf_link_hash_entry *);
8401 const struct elf_backend_data *bed;
8403 bed = get_elf_backend_data (flinfo->output_bfd);
8404 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8405 if (output_symbol_hook != NULL)
8407 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8412 if (name == NULL || *name == '\0')
8413 elfsym->st_name = 0;
8414 else if (input_sec->flags & SEC_EXCLUDE)
8415 elfsym->st_name = 0;
8418 elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab,
8420 if (elfsym->st_name == (unsigned long) -1)
8424 if (flinfo->symbuf_count >= flinfo->symbuf_size)
8426 if (! elf_link_flush_output_syms (flinfo, bed))
8430 dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym;
8431 destshndx = flinfo->symshndxbuf;
8432 if (destshndx != NULL)
8434 if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size)
8438 amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8439 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8441 if (destshndx == NULL)
8443 flinfo->symshndxbuf = destshndx;
8444 memset ((char *) destshndx + amt, 0, amt);
8445 flinfo->shndxbuf_size *= 2;
8447 destshndx += bfd_get_symcount (flinfo->output_bfd);
8450 bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx);
8451 flinfo->symbuf_count += 1;
8452 bfd_get_symcount (flinfo->output_bfd) += 1;
8457 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8460 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8462 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8463 && sym->st_shndx < SHN_LORESERVE)
8465 /* The gABI doesn't support dynamic symbols in output sections
8467 (*_bfd_error_handler)
8468 (_("%B: Too many sections: %d (>= %d)"),
8469 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8470 bfd_set_error (bfd_error_nonrepresentable_section);
8476 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8477 allowing an unsatisfied unversioned symbol in the DSO to match a
8478 versioned symbol that would normally require an explicit version.
8479 We also handle the case that a DSO references a hidden symbol
8480 which may be satisfied by a versioned symbol in another DSO. */
8483 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8484 const struct elf_backend_data *bed,
8485 struct elf_link_hash_entry *h)
8488 struct elf_link_loaded_list *loaded;
8490 if (!is_elf_hash_table (info->hash))
8493 /* Check indirect symbol. */
8494 while (h->root.type == bfd_link_hash_indirect)
8495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8497 switch (h->root.type)
8503 case bfd_link_hash_undefined:
8504 case bfd_link_hash_undefweak:
8505 abfd = h->root.u.undef.abfd;
8506 if ((abfd->flags & DYNAMIC) == 0
8507 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8511 case bfd_link_hash_defined:
8512 case bfd_link_hash_defweak:
8513 abfd = h->root.u.def.section->owner;
8516 case bfd_link_hash_common:
8517 abfd = h->root.u.c.p->section->owner;
8520 BFD_ASSERT (abfd != NULL);
8522 for (loaded = elf_hash_table (info)->loaded;
8524 loaded = loaded->next)
8527 Elf_Internal_Shdr *hdr;
8528 bfd_size_type symcount;
8529 bfd_size_type extsymcount;
8530 bfd_size_type extsymoff;
8531 Elf_Internal_Shdr *versymhdr;
8532 Elf_Internal_Sym *isym;
8533 Elf_Internal_Sym *isymend;
8534 Elf_Internal_Sym *isymbuf;
8535 Elf_External_Versym *ever;
8536 Elf_External_Versym *extversym;
8538 input = loaded->abfd;
8540 /* We check each DSO for a possible hidden versioned definition. */
8542 || (input->flags & DYNAMIC) == 0
8543 || elf_dynversym (input) == 0)
8546 hdr = &elf_tdata (input)->dynsymtab_hdr;
8548 symcount = hdr->sh_size / bed->s->sizeof_sym;
8549 if (elf_bad_symtab (input))
8551 extsymcount = symcount;
8556 extsymcount = symcount - hdr->sh_info;
8557 extsymoff = hdr->sh_info;
8560 if (extsymcount == 0)
8563 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8565 if (isymbuf == NULL)
8568 /* Read in any version definitions. */
8569 versymhdr = &elf_tdata (input)->dynversym_hdr;
8570 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8571 if (extversym == NULL)
8574 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8575 || (bfd_bread (extversym, versymhdr->sh_size, input)
8576 != versymhdr->sh_size))
8584 ever = extversym + extsymoff;
8585 isymend = isymbuf + extsymcount;
8586 for (isym = isymbuf; isym < isymend; isym++, ever++)
8589 Elf_Internal_Versym iver;
8590 unsigned short version_index;
8592 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8593 || isym->st_shndx == SHN_UNDEF)
8596 name = bfd_elf_string_from_elf_section (input,
8599 if (strcmp (name, h->root.root.string) != 0)
8602 _bfd_elf_swap_versym_in (input, ever, &iver);
8604 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8606 && h->forced_local))
8608 /* If we have a non-hidden versioned sym, then it should
8609 have provided a definition for the undefined sym unless
8610 it is defined in a non-shared object and forced local.
8615 version_index = iver.vs_vers & VERSYM_VERSION;
8616 if (version_index == 1 || version_index == 2)
8618 /* This is the base or first version. We can use it. */
8632 /* Add an external symbol to the symbol table. This is called from
8633 the hash table traversal routine. When generating a shared object,
8634 we go through the symbol table twice. The first time we output
8635 anything that might have been forced to local scope in a version
8636 script. The second time we output the symbols that are still
8640 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8642 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8643 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8644 struct elf_final_link_info *flinfo = eoinfo->flinfo;
8646 Elf_Internal_Sym sym;
8647 asection *input_sec;
8648 const struct elf_backend_data *bed;
8652 if (h->root.type == bfd_link_hash_warning)
8654 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8655 if (h->root.type == bfd_link_hash_new)
8659 /* Decide whether to output this symbol in this pass. */
8660 if (eoinfo->localsyms)
8662 if (!h->forced_local)
8664 if (eoinfo->second_pass
8665 && !((h->root.type == bfd_link_hash_defined
8666 || h->root.type == bfd_link_hash_defweak)
8667 && h->root.u.def.section->output_section != NULL))
8672 if (h->forced_local)
8676 bed = get_elf_backend_data (flinfo->output_bfd);
8678 if (h->root.type == bfd_link_hash_undefined)
8680 /* If we have an undefined symbol reference here then it must have
8681 come from a shared library that is being linked in. (Undefined
8682 references in regular files have already been handled unless
8683 they are in unreferenced sections which are removed by garbage
8685 bfd_boolean ignore_undef = FALSE;
8687 /* Some symbols may be special in that the fact that they're
8688 undefined can be safely ignored - let backend determine that. */
8689 if (bed->elf_backend_ignore_undef_symbol)
8690 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8692 /* If we are reporting errors for this situation then do so now. */
8695 && (!h->ref_regular || flinfo->info->gc_sections)
8696 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
8697 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8699 if (!(flinfo->info->callbacks->undefined_symbol
8700 (flinfo->info, h->root.root.string,
8701 h->ref_regular ? NULL : h->root.u.undef.abfd,
8703 (flinfo->info->unresolved_syms_in_shared_libs
8704 == RM_GENERATE_ERROR))))
8706 bfd_set_error (bfd_error_bad_value);
8707 eoinfo->failed = TRUE;
8713 /* We should also warn if a forced local symbol is referenced from
8714 shared libraries. */
8715 if (!flinfo->info->relocatable
8716 && flinfo->info->executable
8722 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
8726 struct elf_link_hash_entry *hi = h;
8728 /* Check indirect symbol. */
8729 while (hi->root.type == bfd_link_hash_indirect)
8730 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
8732 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8733 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8734 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8735 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8737 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8738 def_bfd = flinfo->output_bfd;
8739 if (hi->root.u.def.section != bfd_abs_section_ptr)
8740 def_bfd = hi->root.u.def.section->owner;
8741 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
8742 h->root.root.string);
8743 bfd_set_error (bfd_error_bad_value);
8744 eoinfo->failed = TRUE;
8748 /* We don't want to output symbols that have never been mentioned by
8749 a regular file, or that we have been told to strip. However, if
8750 h->indx is set to -2, the symbol is used by a reloc and we must
8754 else if ((h->def_dynamic
8756 || h->root.type == bfd_link_hash_new)
8760 else if (flinfo->info->strip == strip_all)
8762 else if (flinfo->info->strip == strip_some
8763 && bfd_hash_lookup (flinfo->info->keep_hash,
8764 h->root.root.string, FALSE, FALSE) == NULL)
8766 else if ((h->root.type == bfd_link_hash_defined
8767 || h->root.type == bfd_link_hash_defweak)
8768 && ((flinfo->info->strip_discarded
8769 && discarded_section (h->root.u.def.section))
8770 || (h->root.u.def.section->owner != NULL
8771 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
8773 else if ((h->root.type == bfd_link_hash_undefined
8774 || h->root.type == bfd_link_hash_undefweak)
8775 && h->root.u.undef.abfd != NULL
8776 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8781 /* If we're stripping it, and it's not a dynamic symbol, there's
8782 nothing else to do unless it is a forced local symbol or a
8783 STT_GNU_IFUNC symbol. */
8786 && h->type != STT_GNU_IFUNC
8787 && !h->forced_local)
8791 sym.st_size = h->size;
8792 sym.st_other = h->other;
8793 if (h->forced_local)
8795 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8796 /* Turn off visibility on local symbol. */
8797 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8799 else if (h->unique_global)
8800 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
8801 else if (h->root.type == bfd_link_hash_undefweak
8802 || h->root.type == bfd_link_hash_defweak)
8803 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8805 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8806 sym.st_target_internal = h->target_internal;
8808 switch (h->root.type)
8811 case bfd_link_hash_new:
8812 case bfd_link_hash_warning:
8816 case bfd_link_hash_undefined:
8817 case bfd_link_hash_undefweak:
8818 input_sec = bfd_und_section_ptr;
8819 sym.st_shndx = SHN_UNDEF;
8822 case bfd_link_hash_defined:
8823 case bfd_link_hash_defweak:
8825 input_sec = h->root.u.def.section;
8826 if (input_sec->output_section != NULL)
8828 if (eoinfo->localsyms && flinfo->filesym_count == 1)
8830 bfd_boolean second_pass_sym
8831 = (input_sec->owner == flinfo->output_bfd
8832 || input_sec->owner == NULL
8833 || (input_sec->flags & SEC_LINKER_CREATED) != 0
8834 || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0);
8836 eoinfo->need_second_pass |= second_pass_sym;
8837 if (eoinfo->second_pass != second_pass_sym)
8842 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
8843 input_sec->output_section);
8844 if (sym.st_shndx == SHN_BAD)
8846 (*_bfd_error_handler)
8847 (_("%B: could not find output section %A for input section %A"),
8848 flinfo->output_bfd, input_sec->output_section, input_sec);
8849 bfd_set_error (bfd_error_nonrepresentable_section);
8850 eoinfo->failed = TRUE;
8854 /* ELF symbols in relocatable files are section relative,
8855 but in nonrelocatable files they are virtual
8857 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8858 if (!flinfo->info->relocatable)
8860 sym.st_value += input_sec->output_section->vma;
8861 if (h->type == STT_TLS)
8863 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
8864 if (tls_sec != NULL)
8865 sym.st_value -= tls_sec->vma;
8868 /* The TLS section may have been garbage collected. */
8869 BFD_ASSERT (flinfo->info->gc_sections
8870 && !input_sec->gc_mark);
8877 BFD_ASSERT (input_sec->owner == NULL
8878 || (input_sec->owner->flags & DYNAMIC) != 0);
8879 sym.st_shndx = SHN_UNDEF;
8880 input_sec = bfd_und_section_ptr;
8885 case bfd_link_hash_common:
8886 input_sec = h->root.u.c.p->section;
8887 sym.st_shndx = bed->common_section_index (input_sec);
8888 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8891 case bfd_link_hash_indirect:
8892 /* These symbols are created by symbol versioning. They point
8893 to the decorated version of the name. For example, if the
8894 symbol foo@@GNU_1.2 is the default, which should be used when
8895 foo is used with no version, then we add an indirect symbol
8896 foo which points to foo@@GNU_1.2. We ignore these symbols,
8897 since the indirected symbol is already in the hash table. */
8901 /* Give the processor backend a chance to tweak the symbol value,
8902 and also to finish up anything that needs to be done for this
8903 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8904 forced local syms when non-shared is due to a historical quirk.
8905 STT_GNU_IFUNC symbol must go through PLT. */
8906 if ((h->type == STT_GNU_IFUNC
8908 && !flinfo->info->relocatable)
8909 || ((h->dynindx != -1
8911 && ((flinfo->info->shared
8912 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8913 || h->root.type != bfd_link_hash_undefweak))
8914 || !h->forced_local)
8915 && elf_hash_table (flinfo->info)->dynamic_sections_created))
8917 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8918 (flinfo->output_bfd, flinfo->info, h, &sym)))
8920 eoinfo->failed = TRUE;
8925 /* If we are marking the symbol as undefined, and there are no
8926 non-weak references to this symbol from a regular object, then
8927 mark the symbol as weak undefined; if there are non-weak
8928 references, mark the symbol as strong. We can't do this earlier,
8929 because it might not be marked as undefined until the
8930 finish_dynamic_symbol routine gets through with it. */
8931 if (sym.st_shndx == SHN_UNDEF
8933 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8934 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8937 unsigned int type = ELF_ST_TYPE (sym.st_info);
8939 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8940 if (type == STT_GNU_IFUNC)
8943 if (h->ref_regular_nonweak)
8944 bindtype = STB_GLOBAL;
8946 bindtype = STB_WEAK;
8947 sym.st_info = ELF_ST_INFO (bindtype, type);
8950 /* If this is a symbol defined in a dynamic library, don't use the
8951 symbol size from the dynamic library. Relinking an executable
8952 against a new library may introduce gratuitous changes in the
8953 executable's symbols if we keep the size. */
8954 if (sym.st_shndx == SHN_UNDEF
8959 /* If a non-weak symbol with non-default visibility is not defined
8960 locally, it is a fatal error. */
8961 if (!flinfo->info->relocatable
8962 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8963 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8964 && h->root.type == bfd_link_hash_undefined
8969 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8970 msg = _("%B: protected symbol `%s' isn't defined");
8971 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8972 msg = _("%B: internal symbol `%s' isn't defined");
8974 msg = _("%B: hidden symbol `%s' isn't defined");
8975 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
8976 bfd_set_error (bfd_error_bad_value);
8977 eoinfo->failed = TRUE;
8981 /* If this symbol should be put in the .dynsym section, then put it
8982 there now. We already know the symbol index. We also fill in
8983 the entry in the .hash section. */
8984 if (flinfo->dynsym_sec != NULL
8986 && elf_hash_table (flinfo->info)->dynamic_sections_created)
8990 /* Since there is no version information in the dynamic string,
8991 if there is no version info in symbol version section, we will
8992 have a run-time problem. */
8993 if (h->verinfo.verdef == NULL)
8995 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
8997 if (p && p [1] != '\0')
8999 (*_bfd_error_handler)
9000 (_("%B: No symbol version section for versioned symbol `%s'"),
9001 flinfo->output_bfd, h->root.root.string);
9002 eoinfo->failed = TRUE;
9007 sym.st_name = h->dynstr_index;
9008 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
9009 if (!check_dynsym (flinfo->output_bfd, &sym))
9011 eoinfo->failed = TRUE;
9014 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9016 if (flinfo->hash_sec != NULL)
9018 size_t hash_entry_size;
9019 bfd_byte *bucketpos;
9024 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9025 bucket = h->u.elf_hash_value % bucketcount;
9028 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9029 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9030 + (bucket + 2) * hash_entry_size);
9031 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9032 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9034 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9035 ((bfd_byte *) flinfo->hash_sec->contents
9036 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9039 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9041 Elf_Internal_Versym iversym;
9042 Elf_External_Versym *eversym;
9044 if (!h->def_regular)
9046 if (h->verinfo.verdef == NULL)
9047 iversym.vs_vers = 0;
9049 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9053 if (h->verinfo.vertree == NULL)
9054 iversym.vs_vers = 1;
9056 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9057 if (flinfo->info->create_default_symver)
9062 iversym.vs_vers |= VERSYM_HIDDEN;
9064 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9065 eversym += h->dynindx;
9066 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9070 /* If we're stripping it, then it was just a dynamic symbol, and
9071 there's nothing else to do. */
9072 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
9075 indx = bfd_get_symcount (flinfo->output_bfd);
9076 ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
9079 eoinfo->failed = TRUE;
9084 else if (h->indx == -2)
9090 /* Return TRUE if special handling is done for relocs in SEC against
9091 symbols defined in discarded sections. */
9094 elf_section_ignore_discarded_relocs (asection *sec)
9096 const struct elf_backend_data *bed;
9098 switch (sec->sec_info_type)
9100 case SEC_INFO_TYPE_STABS:
9101 case SEC_INFO_TYPE_EH_FRAME:
9107 bed = get_elf_backend_data (sec->owner);
9108 if (bed->elf_backend_ignore_discarded_relocs != NULL
9109 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9115 /* Return a mask saying how ld should treat relocations in SEC against
9116 symbols defined in discarded sections. If this function returns
9117 COMPLAIN set, ld will issue a warning message. If this function
9118 returns PRETEND set, and the discarded section was link-once and the
9119 same size as the kept link-once section, ld will pretend that the
9120 symbol was actually defined in the kept section. Otherwise ld will
9121 zero the reloc (at least that is the intent, but some cooperation by
9122 the target dependent code is needed, particularly for REL targets). */
9125 _bfd_elf_default_action_discarded (asection *sec)
9127 if (sec->flags & SEC_DEBUGGING)
9130 if (strcmp (".eh_frame", sec->name) == 0)
9133 if (strcmp (".gcc_except_table", sec->name) == 0)
9136 return COMPLAIN | PRETEND;
9139 /* Find a match between a section and a member of a section group. */
9142 match_group_member (asection *sec, asection *group,
9143 struct bfd_link_info *info)
9145 asection *first = elf_next_in_group (group);
9146 asection *s = first;
9150 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9153 s = elf_next_in_group (s);
9161 /* Check if the kept section of a discarded section SEC can be used
9162 to replace it. Return the replacement if it is OK. Otherwise return
9166 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9170 kept = sec->kept_section;
9173 if ((kept->flags & SEC_GROUP) != 0)
9174 kept = match_group_member (sec, kept, info);
9176 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9177 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9179 sec->kept_section = kept;
9184 /* Link an input file into the linker output file. This function
9185 handles all the sections and relocations of the input file at once.
9186 This is so that we only have to read the local symbols once, and
9187 don't have to keep them in memory. */
9190 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9192 int (*relocate_section)
9193 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9194 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9196 Elf_Internal_Shdr *symtab_hdr;
9199 Elf_Internal_Sym *isymbuf;
9200 Elf_Internal_Sym *isym;
9201 Elf_Internal_Sym *isymend;
9203 asection **ppsection;
9205 const struct elf_backend_data *bed;
9206 struct elf_link_hash_entry **sym_hashes;
9207 bfd_size_type address_size;
9208 bfd_vma r_type_mask;
9210 bfd_boolean have_file_sym = FALSE;
9212 output_bfd = flinfo->output_bfd;
9213 bed = get_elf_backend_data (output_bfd);
9214 relocate_section = bed->elf_backend_relocate_section;
9216 /* If this is a dynamic object, we don't want to do anything here:
9217 we don't want the local symbols, and we don't want the section
9219 if ((input_bfd->flags & DYNAMIC) != 0)
9222 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9223 if (elf_bad_symtab (input_bfd))
9225 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9230 locsymcount = symtab_hdr->sh_info;
9231 extsymoff = symtab_hdr->sh_info;
9234 /* Read the local symbols. */
9235 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9236 if (isymbuf == NULL && locsymcount != 0)
9238 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9239 flinfo->internal_syms,
9240 flinfo->external_syms,
9241 flinfo->locsym_shndx);
9242 if (isymbuf == NULL)
9246 /* Find local symbol sections and adjust values of symbols in
9247 SEC_MERGE sections. Write out those local symbols we know are
9248 going into the output file. */
9249 isymend = isymbuf + locsymcount;
9250 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9252 isym++, pindex++, ppsection++)
9256 Elf_Internal_Sym osym;
9262 if (elf_bad_symtab (input_bfd))
9264 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9271 if (isym->st_shndx == SHN_UNDEF)
9272 isec = bfd_und_section_ptr;
9273 else if (isym->st_shndx == SHN_ABS)
9274 isec = bfd_abs_section_ptr;
9275 else if (isym->st_shndx == SHN_COMMON)
9276 isec = bfd_com_section_ptr;
9279 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9282 /* Don't attempt to output symbols with st_shnx in the
9283 reserved range other than SHN_ABS and SHN_COMMON. */
9287 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9288 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9290 _bfd_merged_section_offset (output_bfd, &isec,
9291 elf_section_data (isec)->sec_info,
9297 /* Don't output the first, undefined, symbol. */
9298 if (ppsection == flinfo->sections)
9301 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9303 /* We never output section symbols. Instead, we use the
9304 section symbol of the corresponding section in the output
9309 /* If we are stripping all symbols, we don't want to output this
9311 if (flinfo->info->strip == strip_all)
9314 /* If we are discarding all local symbols, we don't want to
9315 output this one. If we are generating a relocatable output
9316 file, then some of the local symbols may be required by
9317 relocs; we output them below as we discover that they are
9319 if (flinfo->info->discard == discard_all)
9322 /* If this symbol is defined in a section which we are
9323 discarding, we don't need to keep it. */
9324 if (isym->st_shndx != SHN_UNDEF
9325 && isym->st_shndx < SHN_LORESERVE
9326 && bfd_section_removed_from_list (output_bfd,
9327 isec->output_section))
9330 /* Get the name of the symbol. */
9331 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9336 /* See if we are discarding symbols with this name. */
9337 if ((flinfo->info->strip == strip_some
9338 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9340 || (((flinfo->info->discard == discard_sec_merge
9341 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
9342 || flinfo->info->discard == discard_l)
9343 && bfd_is_local_label_name (input_bfd, name)))
9346 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9348 have_file_sym = TRUE;
9349 flinfo->filesym_count += 1;
9353 /* In the absence of debug info, bfd_find_nearest_line uses
9354 FILE symbols to determine the source file for local
9355 function symbols. Provide a FILE symbol here if input
9356 files lack such, so that their symbols won't be
9357 associated with a previous input file. It's not the
9358 source file, but the best we can do. */
9359 have_file_sym = TRUE;
9360 flinfo->filesym_count += 1;
9361 memset (&osym, 0, sizeof (osym));
9362 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9363 osym.st_shndx = SHN_ABS;
9364 if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym,
9365 bfd_abs_section_ptr, NULL))
9371 /* Adjust the section index for the output file. */
9372 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9373 isec->output_section);
9374 if (osym.st_shndx == SHN_BAD)
9377 /* ELF symbols in relocatable files are section relative, but
9378 in executable files they are virtual addresses. Note that
9379 this code assumes that all ELF sections have an associated
9380 BFD section with a reasonable value for output_offset; below
9381 we assume that they also have a reasonable value for
9382 output_section. Any special sections must be set up to meet
9383 these requirements. */
9384 osym.st_value += isec->output_offset;
9385 if (!flinfo->info->relocatable)
9387 osym.st_value += isec->output_section->vma;
9388 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9390 /* STT_TLS symbols are relative to PT_TLS segment base. */
9391 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9392 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9396 indx = bfd_get_symcount (output_bfd);
9397 ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL);
9404 if (bed->s->arch_size == 32)
9412 r_type_mask = 0xffffffff;
9417 /* Relocate the contents of each section. */
9418 sym_hashes = elf_sym_hashes (input_bfd);
9419 for (o = input_bfd->sections; o != NULL; o = o->next)
9423 if (! o->linker_mark)
9425 /* This section was omitted from the link. */
9429 if (flinfo->info->relocatable
9430 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9432 /* Deal with the group signature symbol. */
9433 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9434 unsigned long symndx = sec_data->this_hdr.sh_info;
9435 asection *osec = o->output_section;
9437 if (symndx >= locsymcount
9438 || (elf_bad_symtab (input_bfd)
9439 && flinfo->sections[symndx] == NULL))
9441 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9442 while (h->root.type == bfd_link_hash_indirect
9443 || h->root.type == bfd_link_hash_warning)
9444 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9445 /* Arrange for symbol to be output. */
9447 elf_section_data (osec)->this_hdr.sh_info = -2;
9449 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9451 /* We'll use the output section target_index. */
9452 asection *sec = flinfo->sections[symndx]->output_section;
9453 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9457 if (flinfo->indices[symndx] == -1)
9459 /* Otherwise output the local symbol now. */
9460 Elf_Internal_Sym sym = isymbuf[symndx];
9461 asection *sec = flinfo->sections[symndx]->output_section;
9466 name = bfd_elf_string_from_elf_section (input_bfd,
9467 symtab_hdr->sh_link,
9472 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9474 if (sym.st_shndx == SHN_BAD)
9477 sym.st_value += o->output_offset;
9479 indx = bfd_get_symcount (output_bfd);
9480 ret = elf_link_output_sym (flinfo, name, &sym, o, NULL);
9484 flinfo->indices[symndx] = indx;
9488 elf_section_data (osec)->this_hdr.sh_info
9489 = flinfo->indices[symndx];
9493 if ((o->flags & SEC_HAS_CONTENTS) == 0
9494 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9497 if ((o->flags & SEC_LINKER_CREATED) != 0)
9499 /* Section was created by _bfd_elf_link_create_dynamic_sections
9504 /* Get the contents of the section. They have been cached by a
9505 relaxation routine. Note that o is a section in an input
9506 file, so the contents field will not have been set by any of
9507 the routines which work on output files. */
9508 if (elf_section_data (o)->this_hdr.contents != NULL)
9509 contents = elf_section_data (o)->this_hdr.contents;
9512 contents = flinfo->contents;
9513 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9517 if ((o->flags & SEC_RELOC) != 0)
9519 Elf_Internal_Rela *internal_relocs;
9520 Elf_Internal_Rela *rel, *relend;
9521 int action_discarded;
9524 /* Get the swapped relocs. */
9526 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9527 flinfo->internal_relocs, FALSE);
9528 if (internal_relocs == NULL
9529 && o->reloc_count > 0)
9532 /* We need to reverse-copy input .ctors/.dtors sections if
9533 they are placed in .init_array/.finit_array for output. */
9534 if (o->size > address_size
9535 && ((strncmp (o->name, ".ctors", 6) == 0
9536 && strcmp (o->output_section->name,
9537 ".init_array") == 0)
9538 || (strncmp (o->name, ".dtors", 6) == 0
9539 && strcmp (o->output_section->name,
9540 ".fini_array") == 0))
9541 && (o->name[6] == 0 || o->name[6] == '.'))
9543 if (o->size != o->reloc_count * address_size)
9545 (*_bfd_error_handler)
9546 (_("error: %B: size of section %A is not "
9547 "multiple of address size"),
9549 bfd_set_error (bfd_error_on_input);
9552 o->flags |= SEC_ELF_REVERSE_COPY;
9555 action_discarded = -1;
9556 if (!elf_section_ignore_discarded_relocs (o))
9557 action_discarded = (*bed->action_discarded) (o);
9559 /* Run through the relocs evaluating complex reloc symbols and
9560 looking for relocs against symbols from discarded sections
9561 or section symbols from removed link-once sections.
9562 Complain about relocs against discarded sections. Zero
9563 relocs against removed link-once sections. */
9565 rel = internal_relocs;
9566 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9567 for ( ; rel < relend; rel++)
9569 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9570 unsigned int s_type;
9571 asection **ps, *sec;
9572 struct elf_link_hash_entry *h = NULL;
9573 const char *sym_name;
9575 if (r_symndx == STN_UNDEF)
9578 if (r_symndx >= locsymcount
9579 || (elf_bad_symtab (input_bfd)
9580 && flinfo->sections[r_symndx] == NULL))
9582 h = sym_hashes[r_symndx - extsymoff];
9584 /* Badly formatted input files can contain relocs that
9585 reference non-existant symbols. Check here so that
9586 we do not seg fault. */
9591 sprintf_vma (buffer, rel->r_info);
9592 (*_bfd_error_handler)
9593 (_("error: %B contains a reloc (0x%s) for section %A "
9594 "that references a non-existent global symbol"),
9595 input_bfd, o, buffer);
9596 bfd_set_error (bfd_error_bad_value);
9600 while (h->root.type == bfd_link_hash_indirect
9601 || h->root.type == bfd_link_hash_warning)
9602 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9607 if (h->root.type == bfd_link_hash_defined
9608 || h->root.type == bfd_link_hash_defweak)
9609 ps = &h->root.u.def.section;
9611 sym_name = h->root.root.string;
9615 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9617 s_type = ELF_ST_TYPE (sym->st_info);
9618 ps = &flinfo->sections[r_symndx];
9619 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9623 if ((s_type == STT_RELC || s_type == STT_SRELC)
9624 && !flinfo->info->relocatable)
9627 bfd_vma dot = (rel->r_offset
9628 + o->output_offset + o->output_section->vma);
9630 printf ("Encountered a complex symbol!");
9631 printf (" (input_bfd %s, section %s, reloc %ld\n",
9632 input_bfd->filename, o->name,
9633 (long) (rel - internal_relocs));
9634 printf (" symbol: idx %8.8lx, name %s\n",
9635 r_symndx, sym_name);
9636 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9637 (unsigned long) rel->r_info,
9638 (unsigned long) rel->r_offset);
9640 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
9641 isymbuf, locsymcount, s_type == STT_SRELC))
9644 /* Symbol evaluated OK. Update to absolute value. */
9645 set_symbol_value (input_bfd, isymbuf, locsymcount,
9650 if (action_discarded != -1 && ps != NULL)
9652 /* Complain if the definition comes from a
9653 discarded section. */
9654 if ((sec = *ps) != NULL && discarded_section (sec))
9656 BFD_ASSERT (r_symndx != STN_UNDEF);
9657 if (action_discarded & COMPLAIN)
9658 (*flinfo->info->callbacks->einfo)
9659 (_("%X`%s' referenced in section `%A' of %B: "
9660 "defined in discarded section `%A' of %B\n"),
9661 sym_name, o, input_bfd, sec, sec->owner);
9663 /* Try to do the best we can to support buggy old
9664 versions of gcc. Pretend that the symbol is
9665 really defined in the kept linkonce section.
9666 FIXME: This is quite broken. Modifying the
9667 symbol here means we will be changing all later
9668 uses of the symbol, not just in this section. */
9669 if (action_discarded & PRETEND)
9673 kept = _bfd_elf_check_kept_section (sec,
9685 /* Relocate the section by invoking a back end routine.
9687 The back end routine is responsible for adjusting the
9688 section contents as necessary, and (if using Rela relocs
9689 and generating a relocatable output file) adjusting the
9690 reloc addend as necessary.
9692 The back end routine does not have to worry about setting
9693 the reloc address or the reloc symbol index.
9695 The back end routine is given a pointer to the swapped in
9696 internal symbols, and can access the hash table entries
9697 for the external symbols via elf_sym_hashes (input_bfd).
9699 When generating relocatable output, the back end routine
9700 must handle STB_LOCAL/STT_SECTION symbols specially. The
9701 output symbol is going to be a section symbol
9702 corresponding to the output section, which will require
9703 the addend to be adjusted. */
9705 ret = (*relocate_section) (output_bfd, flinfo->info,
9706 input_bfd, o, contents,
9714 || flinfo->info->relocatable
9715 || flinfo->info->emitrelocations)
9717 Elf_Internal_Rela *irela;
9718 Elf_Internal_Rela *irelaend, *irelamid;
9719 bfd_vma last_offset;
9720 struct elf_link_hash_entry **rel_hash;
9721 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9722 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
9723 unsigned int next_erel;
9724 bfd_boolean rela_normal;
9725 struct bfd_elf_section_data *esdi, *esdo;
9727 esdi = elf_section_data (o);
9728 esdo = elf_section_data (o->output_section);
9729 rela_normal = FALSE;
9731 /* Adjust the reloc addresses and symbol indices. */
9733 irela = internal_relocs;
9734 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9735 rel_hash = esdo->rel.hashes + esdo->rel.count;
9736 /* We start processing the REL relocs, if any. When we reach
9737 IRELAMID in the loop, we switch to the RELA relocs. */
9739 if (esdi->rel.hdr != NULL)
9740 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9741 * bed->s->int_rels_per_ext_rel);
9742 rel_hash_list = rel_hash;
9743 rela_hash_list = NULL;
9744 last_offset = o->output_offset;
9745 if (!flinfo->info->relocatable)
9746 last_offset += o->output_section->vma;
9747 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9749 unsigned long r_symndx;
9751 Elf_Internal_Sym sym;
9753 if (next_erel == bed->s->int_rels_per_ext_rel)
9759 if (irela == irelamid)
9761 rel_hash = esdo->rela.hashes + esdo->rela.count;
9762 rela_hash_list = rel_hash;
9763 rela_normal = bed->rela_normal;
9766 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9769 if (irela->r_offset >= (bfd_vma) -2)
9771 /* This is a reloc for a deleted entry or somesuch.
9772 Turn it into an R_*_NONE reloc, at the same
9773 offset as the last reloc. elf_eh_frame.c and
9774 bfd_elf_discard_info rely on reloc offsets
9776 irela->r_offset = last_offset;
9778 irela->r_addend = 0;
9782 irela->r_offset += o->output_offset;
9784 /* Relocs in an executable have to be virtual addresses. */
9785 if (!flinfo->info->relocatable)
9786 irela->r_offset += o->output_section->vma;
9788 last_offset = irela->r_offset;
9790 r_symndx = irela->r_info >> r_sym_shift;
9791 if (r_symndx == STN_UNDEF)
9794 if (r_symndx >= locsymcount
9795 || (elf_bad_symtab (input_bfd)
9796 && flinfo->sections[r_symndx] == NULL))
9798 struct elf_link_hash_entry *rh;
9801 /* This is a reloc against a global symbol. We
9802 have not yet output all the local symbols, so
9803 we do not know the symbol index of any global
9804 symbol. We set the rel_hash entry for this
9805 reloc to point to the global hash table entry
9806 for this symbol. The symbol index is then
9807 set at the end of bfd_elf_final_link. */
9808 indx = r_symndx - extsymoff;
9809 rh = elf_sym_hashes (input_bfd)[indx];
9810 while (rh->root.type == bfd_link_hash_indirect
9811 || rh->root.type == bfd_link_hash_warning)
9812 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9814 /* Setting the index to -2 tells
9815 elf_link_output_extsym that this symbol is
9817 BFD_ASSERT (rh->indx < 0);
9825 /* This is a reloc against a local symbol. */
9828 sym = isymbuf[r_symndx];
9829 sec = flinfo->sections[r_symndx];
9830 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9832 /* I suppose the backend ought to fill in the
9833 section of any STT_SECTION symbol against a
9834 processor specific section. */
9835 r_symndx = STN_UNDEF;
9836 if (bfd_is_abs_section (sec))
9838 else if (sec == NULL || sec->owner == NULL)
9840 bfd_set_error (bfd_error_bad_value);
9845 asection *osec = sec->output_section;
9847 /* If we have discarded a section, the output
9848 section will be the absolute section. In
9849 case of discarded SEC_MERGE sections, use
9850 the kept section. relocate_section should
9851 have already handled discarded linkonce
9853 if (bfd_is_abs_section (osec)
9854 && sec->kept_section != NULL
9855 && sec->kept_section->output_section != NULL)
9857 osec = sec->kept_section->output_section;
9858 irela->r_addend -= osec->vma;
9861 if (!bfd_is_abs_section (osec))
9863 r_symndx = osec->target_index;
9864 if (r_symndx == STN_UNDEF)
9866 irela->r_addend += osec->vma;
9867 osec = _bfd_nearby_section (output_bfd, osec,
9869 irela->r_addend -= osec->vma;
9870 r_symndx = osec->target_index;
9875 /* Adjust the addend according to where the
9876 section winds up in the output section. */
9878 irela->r_addend += sec->output_offset;
9882 if (flinfo->indices[r_symndx] == -1)
9884 unsigned long shlink;
9889 if (flinfo->info->strip == strip_all)
9891 /* You can't do ld -r -s. */
9892 bfd_set_error (bfd_error_invalid_operation);
9896 /* This symbol was skipped earlier, but
9897 since it is needed by a reloc, we
9898 must output it now. */
9899 shlink = symtab_hdr->sh_link;
9900 name = (bfd_elf_string_from_elf_section
9901 (input_bfd, shlink, sym.st_name));
9905 osec = sec->output_section;
9907 _bfd_elf_section_from_bfd_section (output_bfd,
9909 if (sym.st_shndx == SHN_BAD)
9912 sym.st_value += sec->output_offset;
9913 if (!flinfo->info->relocatable)
9915 sym.st_value += osec->vma;
9916 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9918 /* STT_TLS symbols are relative to PT_TLS
9920 BFD_ASSERT (elf_hash_table (flinfo->info)
9922 sym.st_value -= (elf_hash_table (flinfo->info)
9927 indx = bfd_get_symcount (output_bfd);
9928 ret = elf_link_output_sym (flinfo, name, &sym, sec,
9933 flinfo->indices[r_symndx] = indx;
9938 r_symndx = flinfo->indices[r_symndx];
9941 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9942 | (irela->r_info & r_type_mask));
9945 /* Swap out the relocs. */
9946 input_rel_hdr = esdi->rel.hdr;
9947 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
9949 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9954 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9955 * bed->s->int_rels_per_ext_rel);
9956 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9959 input_rela_hdr = esdi->rela.hdr;
9960 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9962 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9971 /* Write out the modified section contents. */
9972 if (bed->elf_backend_write_section
9973 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
9976 /* Section written out. */
9978 else switch (o->sec_info_type)
9980 case SEC_INFO_TYPE_STABS:
9981 if (! (_bfd_write_section_stabs
9983 &elf_hash_table (flinfo->info)->stab_info,
9984 o, &elf_section_data (o)->sec_info, contents)))
9987 case SEC_INFO_TYPE_MERGE:
9988 if (! _bfd_write_merged_section (output_bfd, o,
9989 elf_section_data (o)->sec_info))
9992 case SEC_INFO_TYPE_EH_FRAME:
9994 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10001 /* FIXME: octets_per_byte. */
10002 if (! (o->flags & SEC_EXCLUDE))
10004 file_ptr offset = (file_ptr) o->output_offset;
10005 bfd_size_type todo = o->size;
10006 if ((o->flags & SEC_ELF_REVERSE_COPY))
10008 /* Reverse-copy input section to output. */
10011 todo -= address_size;
10012 if (! bfd_set_section_contents (output_bfd,
10020 offset += address_size;
10024 else if (! bfd_set_section_contents (output_bfd,
10038 /* Generate a reloc when linking an ELF file. This is a reloc
10039 requested by the linker, and does not come from any input file. This
10040 is used to build constructor and destructor tables when linking
10044 elf_reloc_link_order (bfd *output_bfd,
10045 struct bfd_link_info *info,
10046 asection *output_section,
10047 struct bfd_link_order *link_order)
10049 reloc_howto_type *howto;
10053 struct bfd_elf_section_reloc_data *reldata;
10054 struct elf_link_hash_entry **rel_hash_ptr;
10055 Elf_Internal_Shdr *rel_hdr;
10056 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10057 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10060 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10062 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10065 bfd_set_error (bfd_error_bad_value);
10069 addend = link_order->u.reloc.p->addend;
10072 reldata = &esdo->rel;
10073 else if (esdo->rela.hdr)
10074 reldata = &esdo->rela;
10081 /* Figure out the symbol index. */
10082 rel_hash_ptr = reldata->hashes + reldata->count;
10083 if (link_order->type == bfd_section_reloc_link_order)
10085 indx = link_order->u.reloc.p->u.section->target_index;
10086 BFD_ASSERT (indx != 0);
10087 *rel_hash_ptr = NULL;
10091 struct elf_link_hash_entry *h;
10093 /* Treat a reloc against a defined symbol as though it were
10094 actually against the section. */
10095 h = ((struct elf_link_hash_entry *)
10096 bfd_wrapped_link_hash_lookup (output_bfd, info,
10097 link_order->u.reloc.p->u.name,
10098 FALSE, FALSE, TRUE));
10100 && (h->root.type == bfd_link_hash_defined
10101 || h->root.type == bfd_link_hash_defweak))
10105 section = h->root.u.def.section;
10106 indx = section->output_section->target_index;
10107 *rel_hash_ptr = NULL;
10108 /* It seems that we ought to add the symbol value to the
10109 addend here, but in practice it has already been added
10110 because it was passed to constructor_callback. */
10111 addend += section->output_section->vma + section->output_offset;
10113 else if (h != NULL)
10115 /* Setting the index to -2 tells elf_link_output_extsym that
10116 this symbol is used by a reloc. */
10123 if (! ((*info->callbacks->unattached_reloc)
10124 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10130 /* If this is an inplace reloc, we must write the addend into the
10132 if (howto->partial_inplace && addend != 0)
10134 bfd_size_type size;
10135 bfd_reloc_status_type rstat;
10138 const char *sym_name;
10140 size = (bfd_size_type) bfd_get_reloc_size (howto);
10141 buf = (bfd_byte *) bfd_zmalloc (size);
10144 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10151 case bfd_reloc_outofrange:
10154 case bfd_reloc_overflow:
10155 if (link_order->type == bfd_section_reloc_link_order)
10156 sym_name = bfd_section_name (output_bfd,
10157 link_order->u.reloc.p->u.section);
10159 sym_name = link_order->u.reloc.p->u.name;
10160 if (! ((*info->callbacks->reloc_overflow)
10161 (info, NULL, sym_name, howto->name, addend, NULL,
10162 NULL, (bfd_vma) 0)))
10169 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10170 link_order->offset, size);
10176 /* The address of a reloc is relative to the section in a
10177 relocatable file, and is a virtual address in an executable
10179 offset = link_order->offset;
10180 if (! info->relocatable)
10181 offset += output_section->vma;
10183 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10185 irel[i].r_offset = offset;
10186 irel[i].r_info = 0;
10187 irel[i].r_addend = 0;
10189 if (bed->s->arch_size == 32)
10190 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10192 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10194 rel_hdr = reldata->hdr;
10195 erel = rel_hdr->contents;
10196 if (rel_hdr->sh_type == SHT_REL)
10198 erel += reldata->count * bed->s->sizeof_rel;
10199 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10203 irel[0].r_addend = addend;
10204 erel += reldata->count * bed->s->sizeof_rela;
10205 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10214 /* Get the output vma of the section pointed to by the sh_link field. */
10217 elf_get_linked_section_vma (struct bfd_link_order *p)
10219 Elf_Internal_Shdr **elf_shdrp;
10223 s = p->u.indirect.section;
10224 elf_shdrp = elf_elfsections (s->owner);
10225 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10226 elfsec = elf_shdrp[elfsec]->sh_link;
10228 The Intel C compiler generates SHT_IA_64_UNWIND with
10229 SHF_LINK_ORDER. But it doesn't set the sh_link or
10230 sh_info fields. Hence we could get the situation
10231 where elfsec is 0. */
10234 const struct elf_backend_data *bed
10235 = get_elf_backend_data (s->owner);
10236 if (bed->link_order_error_handler)
10237 bed->link_order_error_handler
10238 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10243 s = elf_shdrp[elfsec]->bfd_section;
10244 return s->output_section->vma + s->output_offset;
10249 /* Compare two sections based on the locations of the sections they are
10250 linked to. Used by elf_fixup_link_order. */
10253 compare_link_order (const void * a, const void * b)
10258 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10259 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10262 return apos > bpos;
10266 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10267 order as their linked sections. Returns false if this could not be done
10268 because an output section includes both ordered and unordered
10269 sections. Ideally we'd do this in the linker proper. */
10272 elf_fixup_link_order (bfd *abfd, asection *o)
10274 int seen_linkorder;
10277 struct bfd_link_order *p;
10279 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10281 struct bfd_link_order **sections;
10282 asection *s, *other_sec, *linkorder_sec;
10286 linkorder_sec = NULL;
10288 seen_linkorder = 0;
10289 for (p = o->map_head.link_order; p != NULL; p = p->next)
10291 if (p->type == bfd_indirect_link_order)
10293 s = p->u.indirect.section;
10295 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10296 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10297 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10298 && elfsec < elf_numsections (sub)
10299 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10300 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10314 if (seen_other && seen_linkorder)
10316 if (other_sec && linkorder_sec)
10317 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10319 linkorder_sec->owner, other_sec,
10322 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10324 bfd_set_error (bfd_error_bad_value);
10329 if (!seen_linkorder)
10332 sections = (struct bfd_link_order **)
10333 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10334 if (sections == NULL)
10336 seen_linkorder = 0;
10338 for (p = o->map_head.link_order; p != NULL; p = p->next)
10340 sections[seen_linkorder++] = p;
10342 /* Sort the input sections in the order of their linked section. */
10343 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10344 compare_link_order);
10346 /* Change the offsets of the sections. */
10348 for (n = 0; n < seen_linkorder; n++)
10350 s = sections[n]->u.indirect.section;
10351 offset &= ~(bfd_vma) 0 << s->alignment_power;
10352 s->output_offset = offset;
10353 sections[n]->offset = offset;
10354 /* FIXME: octets_per_byte. */
10355 offset += sections[n]->size;
10363 /* Do the final step of an ELF link. */
10366 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10368 bfd_boolean dynamic;
10369 bfd_boolean emit_relocs;
10371 struct elf_final_link_info flinfo;
10373 struct bfd_link_order *p;
10375 bfd_size_type max_contents_size;
10376 bfd_size_type max_external_reloc_size;
10377 bfd_size_type max_internal_reloc_count;
10378 bfd_size_type max_sym_count;
10379 bfd_size_type max_sym_shndx_count;
10381 Elf_Internal_Sym elfsym;
10383 Elf_Internal_Shdr *symtab_hdr;
10384 Elf_Internal_Shdr *symtab_shndx_hdr;
10385 Elf_Internal_Shdr *symstrtab_hdr;
10386 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10387 struct elf_outext_info eoinfo;
10388 bfd_boolean merged;
10389 size_t relativecount = 0;
10390 asection *reldyn = 0;
10392 asection *attr_section = NULL;
10393 bfd_vma attr_size = 0;
10394 const char *std_attrs_section;
10396 if (! is_elf_hash_table (info->hash))
10400 abfd->flags |= DYNAMIC;
10402 dynamic = elf_hash_table (info)->dynamic_sections_created;
10403 dynobj = elf_hash_table (info)->dynobj;
10405 emit_relocs = (info->relocatable
10406 || info->emitrelocations);
10408 flinfo.info = info;
10409 flinfo.output_bfd = abfd;
10410 flinfo.symstrtab = _bfd_elf_stringtab_init ();
10411 if (flinfo.symstrtab == NULL)
10416 flinfo.dynsym_sec = NULL;
10417 flinfo.hash_sec = NULL;
10418 flinfo.symver_sec = NULL;
10422 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10423 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10424 /* Note that dynsym_sec can be NULL (on VMS). */
10425 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10426 /* Note that it is OK if symver_sec is NULL. */
10429 flinfo.contents = NULL;
10430 flinfo.external_relocs = NULL;
10431 flinfo.internal_relocs = NULL;
10432 flinfo.external_syms = NULL;
10433 flinfo.locsym_shndx = NULL;
10434 flinfo.internal_syms = NULL;
10435 flinfo.indices = NULL;
10436 flinfo.sections = NULL;
10437 flinfo.symbuf = NULL;
10438 flinfo.symshndxbuf = NULL;
10439 flinfo.symbuf_count = 0;
10440 flinfo.shndxbuf_size = 0;
10441 flinfo.filesym_count = 0;
10443 /* The object attributes have been merged. Remove the input
10444 sections from the link, and set the contents of the output
10446 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10447 for (o = abfd->sections; o != NULL; o = o->next)
10449 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10450 || strcmp (o->name, ".gnu.attributes") == 0)
10452 for (p = o->map_head.link_order; p != NULL; p = p->next)
10454 asection *input_section;
10456 if (p->type != bfd_indirect_link_order)
10458 input_section = p->u.indirect.section;
10459 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10460 elf_link_input_bfd ignores this section. */
10461 input_section->flags &= ~SEC_HAS_CONTENTS;
10464 attr_size = bfd_elf_obj_attr_size (abfd);
10467 bfd_set_section_size (abfd, o, attr_size);
10469 /* Skip this section later on. */
10470 o->map_head.link_order = NULL;
10473 o->flags |= SEC_EXCLUDE;
10477 /* Count up the number of relocations we will output for each output
10478 section, so that we know the sizes of the reloc sections. We
10479 also figure out some maximum sizes. */
10480 max_contents_size = 0;
10481 max_external_reloc_size = 0;
10482 max_internal_reloc_count = 0;
10484 max_sym_shndx_count = 0;
10486 for (o = abfd->sections; o != NULL; o = o->next)
10488 struct bfd_elf_section_data *esdo = elf_section_data (o);
10489 o->reloc_count = 0;
10491 for (p = o->map_head.link_order; p != NULL; p = p->next)
10493 unsigned int reloc_count = 0;
10494 struct bfd_elf_section_data *esdi = NULL;
10496 if (p->type == bfd_section_reloc_link_order
10497 || p->type == bfd_symbol_reloc_link_order)
10499 else if (p->type == bfd_indirect_link_order)
10503 sec = p->u.indirect.section;
10504 esdi = elf_section_data (sec);
10506 /* Mark all sections which are to be included in the
10507 link. This will normally be every section. We need
10508 to do this so that we can identify any sections which
10509 the linker has decided to not include. */
10510 sec->linker_mark = TRUE;
10512 if (sec->flags & SEC_MERGE)
10515 if (esdo->this_hdr.sh_type == SHT_REL
10516 || esdo->this_hdr.sh_type == SHT_RELA)
10517 /* Some backends use reloc_count in relocation sections
10518 to count particular types of relocs. Of course,
10519 reloc sections themselves can't have relocations. */
10521 else if (info->relocatable || info->emitrelocations)
10522 reloc_count = sec->reloc_count;
10523 else if (bed->elf_backend_count_relocs)
10524 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10526 if (sec->rawsize > max_contents_size)
10527 max_contents_size = sec->rawsize;
10528 if (sec->size > max_contents_size)
10529 max_contents_size = sec->size;
10531 /* We are interested in just local symbols, not all
10533 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10534 && (sec->owner->flags & DYNAMIC) == 0)
10538 if (elf_bad_symtab (sec->owner))
10539 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10540 / bed->s->sizeof_sym);
10542 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10544 if (sym_count > max_sym_count)
10545 max_sym_count = sym_count;
10547 if (sym_count > max_sym_shndx_count
10548 && elf_symtab_shndx (sec->owner) != 0)
10549 max_sym_shndx_count = sym_count;
10551 if ((sec->flags & SEC_RELOC) != 0)
10553 size_t ext_size = 0;
10555 if (esdi->rel.hdr != NULL)
10556 ext_size = esdi->rel.hdr->sh_size;
10557 if (esdi->rela.hdr != NULL)
10558 ext_size += esdi->rela.hdr->sh_size;
10560 if (ext_size > max_external_reloc_size)
10561 max_external_reloc_size = ext_size;
10562 if (sec->reloc_count > max_internal_reloc_count)
10563 max_internal_reloc_count = sec->reloc_count;
10568 if (reloc_count == 0)
10571 o->reloc_count += reloc_count;
10573 if (p->type == bfd_indirect_link_order
10574 && (info->relocatable || info->emitrelocations))
10577 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10578 if (esdi->rela.hdr)
10579 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10584 esdo->rela.count += reloc_count;
10586 esdo->rel.count += reloc_count;
10590 if (o->reloc_count > 0)
10591 o->flags |= SEC_RELOC;
10594 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10595 set it (this is probably a bug) and if it is set
10596 assign_section_numbers will create a reloc section. */
10597 o->flags &=~ SEC_RELOC;
10600 /* If the SEC_ALLOC flag is not set, force the section VMA to
10601 zero. This is done in elf_fake_sections as well, but forcing
10602 the VMA to 0 here will ensure that relocs against these
10603 sections are handled correctly. */
10604 if ((o->flags & SEC_ALLOC) == 0
10605 && ! o->user_set_vma)
10609 if (! info->relocatable && merged)
10610 elf_link_hash_traverse (elf_hash_table (info),
10611 _bfd_elf_link_sec_merge_syms, abfd);
10613 /* Figure out the file positions for everything but the symbol table
10614 and the relocs. We set symcount to force assign_section_numbers
10615 to create a symbol table. */
10616 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10617 BFD_ASSERT (! abfd->output_has_begun);
10618 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10621 /* Set sizes, and assign file positions for reloc sections. */
10622 for (o = abfd->sections; o != NULL; o = o->next)
10624 struct bfd_elf_section_data *esdo = elf_section_data (o);
10625 if ((o->flags & SEC_RELOC) != 0)
10628 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10632 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10636 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10637 to count upwards while actually outputting the relocations. */
10638 esdo->rel.count = 0;
10639 esdo->rela.count = 0;
10642 _bfd_elf_assign_file_positions_for_relocs (abfd);
10644 /* We have now assigned file positions for all the sections except
10645 .symtab and .strtab. We start the .symtab section at the current
10646 file position, and write directly to it. We build the .strtab
10647 section in memory. */
10648 bfd_get_symcount (abfd) = 0;
10649 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10650 /* sh_name is set in prep_headers. */
10651 symtab_hdr->sh_type = SHT_SYMTAB;
10652 /* sh_flags, sh_addr and sh_size all start off zero. */
10653 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10654 /* sh_link is set in assign_section_numbers. */
10655 /* sh_info is set below. */
10656 /* sh_offset is set just below. */
10657 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10659 off = elf_tdata (abfd)->next_file_pos;
10660 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10662 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10663 incorrect. We do not yet know the size of the .symtab section.
10664 We correct next_file_pos below, after we do know the size. */
10666 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10667 continuously seeking to the right position in the file. */
10668 if (! info->keep_memory || max_sym_count < 20)
10669 flinfo.symbuf_size = 20;
10671 flinfo.symbuf_size = max_sym_count;
10672 amt = flinfo.symbuf_size;
10673 amt *= bed->s->sizeof_sym;
10674 flinfo.symbuf = (bfd_byte *) bfd_malloc (amt);
10675 if (flinfo.symbuf == NULL)
10677 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10679 /* Wild guess at number of output symbols. realloc'd as needed. */
10680 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10681 flinfo.shndxbuf_size = amt;
10682 amt *= sizeof (Elf_External_Sym_Shndx);
10683 flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10684 if (flinfo.symshndxbuf == NULL)
10688 /* Start writing out the symbol table. The first symbol is always a
10690 if (info->strip != strip_all
10693 elfsym.st_value = 0;
10694 elfsym.st_size = 0;
10695 elfsym.st_info = 0;
10696 elfsym.st_other = 0;
10697 elfsym.st_shndx = SHN_UNDEF;
10698 elfsym.st_target_internal = 0;
10699 if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr,
10704 /* Output a symbol for each section. We output these even if we are
10705 discarding local symbols, since they are used for relocs. These
10706 symbols have no names. We store the index of each one in the
10707 index field of the section, so that we can find it again when
10708 outputting relocs. */
10709 if (info->strip != strip_all
10712 elfsym.st_size = 0;
10713 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10714 elfsym.st_other = 0;
10715 elfsym.st_value = 0;
10716 elfsym.st_target_internal = 0;
10717 for (i = 1; i < elf_numsections (abfd); i++)
10719 o = bfd_section_from_elf_index (abfd, i);
10722 o->target_index = bfd_get_symcount (abfd);
10723 elfsym.st_shndx = i;
10724 if (!info->relocatable)
10725 elfsym.st_value = o->vma;
10726 if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1)
10732 /* Allocate some memory to hold information read in from the input
10734 if (max_contents_size != 0)
10736 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
10737 if (flinfo.contents == NULL)
10741 if (max_external_reloc_size != 0)
10743 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
10744 if (flinfo.external_relocs == NULL)
10748 if (max_internal_reloc_count != 0)
10750 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10751 amt *= sizeof (Elf_Internal_Rela);
10752 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
10753 if (flinfo.internal_relocs == NULL)
10757 if (max_sym_count != 0)
10759 amt = max_sym_count * bed->s->sizeof_sym;
10760 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
10761 if (flinfo.external_syms == NULL)
10764 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10765 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
10766 if (flinfo.internal_syms == NULL)
10769 amt = max_sym_count * sizeof (long);
10770 flinfo.indices = (long int *) bfd_malloc (amt);
10771 if (flinfo.indices == NULL)
10774 amt = max_sym_count * sizeof (asection *);
10775 flinfo.sections = (asection **) bfd_malloc (amt);
10776 if (flinfo.sections == NULL)
10780 if (max_sym_shndx_count != 0)
10782 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10783 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
10784 if (flinfo.locsym_shndx == NULL)
10788 if (elf_hash_table (info)->tls_sec)
10790 bfd_vma base, end = 0;
10793 for (sec = elf_hash_table (info)->tls_sec;
10794 sec && (sec->flags & SEC_THREAD_LOCAL);
10797 bfd_size_type size = sec->size;
10800 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10802 struct bfd_link_order *ord = sec->map_tail.link_order;
10805 size = ord->offset + ord->size;
10807 end = sec->vma + size;
10809 base = elf_hash_table (info)->tls_sec->vma;
10810 /* Only align end of TLS section if static TLS doesn't have special
10811 alignment requirements. */
10812 if (bed->static_tls_alignment == 1)
10813 end = align_power (end,
10814 elf_hash_table (info)->tls_sec->alignment_power);
10815 elf_hash_table (info)->tls_size = end - base;
10818 /* Reorder SHF_LINK_ORDER sections. */
10819 for (o = abfd->sections; o != NULL; o = o->next)
10821 if (!elf_fixup_link_order (abfd, o))
10825 /* Since ELF permits relocations to be against local symbols, we
10826 must have the local symbols available when we do the relocations.
10827 Since we would rather only read the local symbols once, and we
10828 would rather not keep them in memory, we handle all the
10829 relocations for a single input file at the same time.
10831 Unfortunately, there is no way to know the total number of local
10832 symbols until we have seen all of them, and the local symbol
10833 indices precede the global symbol indices. This means that when
10834 we are generating relocatable output, and we see a reloc against
10835 a global symbol, we can not know the symbol index until we have
10836 finished examining all the local symbols to see which ones we are
10837 going to output. To deal with this, we keep the relocations in
10838 memory, and don't output them until the end of the link. This is
10839 an unfortunate waste of memory, but I don't see a good way around
10840 it. Fortunately, it only happens when performing a relocatable
10841 link, which is not the common case. FIXME: If keep_memory is set
10842 we could write the relocs out and then read them again; I don't
10843 know how bad the memory loss will be. */
10845 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10846 sub->output_has_begun = FALSE;
10847 for (o = abfd->sections; o != NULL; o = o->next)
10849 for (p = o->map_head.link_order; p != NULL; p = p->next)
10851 if (p->type == bfd_indirect_link_order
10852 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10853 == bfd_target_elf_flavour)
10854 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10856 if (! sub->output_has_begun)
10858 if (! elf_link_input_bfd (&flinfo, sub))
10860 sub->output_has_begun = TRUE;
10863 else if (p->type == bfd_section_reloc_link_order
10864 || p->type == bfd_symbol_reloc_link_order)
10866 if (! elf_reloc_link_order (abfd, info, o, p))
10871 if (! _bfd_default_link_order (abfd, info, o, p))
10873 if (p->type == bfd_indirect_link_order
10874 && (bfd_get_flavour (sub)
10875 == bfd_target_elf_flavour)
10876 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10877 != bed->s->elfclass))
10879 const char *iclass, *oclass;
10881 if (bed->s->elfclass == ELFCLASS64)
10883 iclass = "ELFCLASS32";
10884 oclass = "ELFCLASS64";
10888 iclass = "ELFCLASS64";
10889 oclass = "ELFCLASS32";
10892 bfd_set_error (bfd_error_wrong_format);
10893 (*_bfd_error_handler)
10894 (_("%B: file class %s incompatible with %s"),
10895 sub, iclass, oclass);
10904 /* Free symbol buffer if needed. */
10905 if (!info->reduce_memory_overheads)
10907 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10908 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10909 && elf_tdata (sub)->symbuf)
10911 free (elf_tdata (sub)->symbuf);
10912 elf_tdata (sub)->symbuf = NULL;
10916 /* Output a FILE symbol so that following locals are not associated
10917 with the wrong input file. */
10918 memset (&elfsym, 0, sizeof (elfsym));
10919 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10920 elfsym.st_shndx = SHN_ABS;
10922 if (flinfo.filesym_count > 1
10923 && !elf_link_output_sym (&flinfo, NULL, &elfsym,
10924 bfd_und_section_ptr, NULL))
10927 /* Output any global symbols that got converted to local in a
10928 version script or due to symbol visibility. We do this in a
10929 separate step since ELF requires all local symbols to appear
10930 prior to any global symbols. FIXME: We should only do this if
10931 some global symbols were, in fact, converted to become local.
10932 FIXME: Will this work correctly with the Irix 5 linker? */
10933 eoinfo.failed = FALSE;
10934 eoinfo.flinfo = &flinfo;
10935 eoinfo.localsyms = TRUE;
10936 eoinfo.need_second_pass = FALSE;
10937 eoinfo.second_pass = FALSE;
10938 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10942 if (flinfo.filesym_count == 1
10943 && !elf_link_output_sym (&flinfo, NULL, &elfsym,
10944 bfd_und_section_ptr, NULL))
10947 if (eoinfo.need_second_pass)
10949 eoinfo.second_pass = TRUE;
10950 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10955 /* If backend needs to output some local symbols not present in the hash
10956 table, do it now. */
10957 if (bed->elf_backend_output_arch_local_syms)
10959 typedef int (*out_sym_func)
10960 (void *, const char *, Elf_Internal_Sym *, asection *,
10961 struct elf_link_hash_entry *);
10963 if (! ((*bed->elf_backend_output_arch_local_syms)
10964 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
10968 /* That wrote out all the local symbols. Finish up the symbol table
10969 with the global symbols. Even if we want to strip everything we
10970 can, we still need to deal with those global symbols that got
10971 converted to local in a version script. */
10973 /* The sh_info field records the index of the first non local symbol. */
10974 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10977 && flinfo.dynsym_sec != NULL
10978 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10980 Elf_Internal_Sym sym;
10981 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
10982 long last_local = 0;
10984 /* Write out the section symbols for the output sections. */
10985 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10991 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10993 sym.st_target_internal = 0;
10995 for (s = abfd->sections; s != NULL; s = s->next)
11001 dynindx = elf_section_data (s)->dynindx;
11004 indx = elf_section_data (s)->this_idx;
11005 BFD_ASSERT (indx > 0);
11006 sym.st_shndx = indx;
11007 if (! check_dynsym (abfd, &sym))
11009 sym.st_value = s->vma;
11010 dest = dynsym + dynindx * bed->s->sizeof_sym;
11011 if (last_local < dynindx)
11012 last_local = dynindx;
11013 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11017 /* Write out the local dynsyms. */
11018 if (elf_hash_table (info)->dynlocal)
11020 struct elf_link_local_dynamic_entry *e;
11021 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11026 /* Copy the internal symbol and turn off visibility.
11027 Note that we saved a word of storage and overwrote
11028 the original st_name with the dynstr_index. */
11030 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11032 s = bfd_section_from_elf_index (e->input_bfd,
11037 elf_section_data (s->output_section)->this_idx;
11038 if (! check_dynsym (abfd, &sym))
11040 sym.st_value = (s->output_section->vma
11042 + e->isym.st_value);
11045 if (last_local < e->dynindx)
11046 last_local = e->dynindx;
11048 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11049 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11053 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
11057 /* We get the global symbols from the hash table. */
11058 eoinfo.failed = FALSE;
11059 eoinfo.localsyms = FALSE;
11060 eoinfo.flinfo = &flinfo;
11061 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11065 /* If backend needs to output some symbols not present in the hash
11066 table, do it now. */
11067 if (bed->elf_backend_output_arch_syms)
11069 typedef int (*out_sym_func)
11070 (void *, const char *, Elf_Internal_Sym *, asection *,
11071 struct elf_link_hash_entry *);
11073 if (! ((*bed->elf_backend_output_arch_syms)
11074 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym)))
11078 /* Flush all symbols to the file. */
11079 if (! elf_link_flush_output_syms (&flinfo, bed))
11082 /* Now we know the size of the symtab section. */
11083 off += symtab_hdr->sh_size;
11085 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11086 if (symtab_shndx_hdr->sh_name != 0)
11088 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11089 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11090 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11091 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11092 symtab_shndx_hdr->sh_size = amt;
11094 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11097 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11098 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11103 /* Finish up and write out the symbol string table (.strtab)
11105 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11106 /* sh_name was set in prep_headers. */
11107 symstrtab_hdr->sh_type = SHT_STRTAB;
11108 symstrtab_hdr->sh_flags = 0;
11109 symstrtab_hdr->sh_addr = 0;
11110 symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab);
11111 symstrtab_hdr->sh_entsize = 0;
11112 symstrtab_hdr->sh_link = 0;
11113 symstrtab_hdr->sh_info = 0;
11114 /* sh_offset is set just below. */
11115 symstrtab_hdr->sh_addralign = 1;
11117 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
11118 elf_tdata (abfd)->next_file_pos = off;
11120 if (bfd_get_symcount (abfd) > 0)
11122 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11123 || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab))
11127 /* Adjust the relocs to have the correct symbol indices. */
11128 for (o = abfd->sections; o != NULL; o = o->next)
11130 struct bfd_elf_section_data *esdo = elf_section_data (o);
11131 if ((o->flags & SEC_RELOC) == 0)
11134 if (esdo->rel.hdr != NULL)
11135 elf_link_adjust_relocs (abfd, &esdo->rel);
11136 if (esdo->rela.hdr != NULL)
11137 elf_link_adjust_relocs (abfd, &esdo->rela);
11139 /* Set the reloc_count field to 0 to prevent write_relocs from
11140 trying to swap the relocs out itself. */
11141 o->reloc_count = 0;
11144 if (dynamic && info->combreloc && dynobj != NULL)
11145 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11147 /* If we are linking against a dynamic object, or generating a
11148 shared library, finish up the dynamic linking information. */
11151 bfd_byte *dyncon, *dynconend;
11153 /* Fix up .dynamic entries. */
11154 o = bfd_get_linker_section (dynobj, ".dynamic");
11155 BFD_ASSERT (o != NULL);
11157 dyncon = o->contents;
11158 dynconend = o->contents + o->size;
11159 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11161 Elf_Internal_Dyn dyn;
11165 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11172 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11174 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11176 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11177 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11180 dyn.d_un.d_val = relativecount;
11187 name = info->init_function;
11190 name = info->fini_function;
11193 struct elf_link_hash_entry *h;
11195 h = elf_link_hash_lookup (elf_hash_table (info), name,
11196 FALSE, FALSE, TRUE);
11198 && (h->root.type == bfd_link_hash_defined
11199 || h->root.type == bfd_link_hash_defweak))
11201 dyn.d_un.d_ptr = h->root.u.def.value;
11202 o = h->root.u.def.section;
11203 if (o->output_section != NULL)
11204 dyn.d_un.d_ptr += (o->output_section->vma
11205 + o->output_offset);
11208 /* The symbol is imported from another shared
11209 library and does not apply to this one. */
11210 dyn.d_un.d_ptr = 0;
11217 case DT_PREINIT_ARRAYSZ:
11218 name = ".preinit_array";
11220 case DT_INIT_ARRAYSZ:
11221 name = ".init_array";
11223 case DT_FINI_ARRAYSZ:
11224 name = ".fini_array";
11226 o = bfd_get_section_by_name (abfd, name);
11229 (*_bfd_error_handler)
11230 (_("%B: could not find output section %s"), abfd, name);
11234 (*_bfd_error_handler)
11235 (_("warning: %s section has zero size"), name);
11236 dyn.d_un.d_val = o->size;
11239 case DT_PREINIT_ARRAY:
11240 name = ".preinit_array";
11242 case DT_INIT_ARRAY:
11243 name = ".init_array";
11245 case DT_FINI_ARRAY:
11246 name = ".fini_array";
11253 name = ".gnu.hash";
11262 name = ".gnu.version_d";
11265 name = ".gnu.version_r";
11268 name = ".gnu.version";
11270 o = bfd_get_section_by_name (abfd, name);
11273 (*_bfd_error_handler)
11274 (_("%B: could not find output section %s"), abfd, name);
11277 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11279 (*_bfd_error_handler)
11280 (_("warning: section '%s' is being made into a note"), name);
11281 bfd_set_error (bfd_error_nonrepresentable_section);
11284 dyn.d_un.d_ptr = o->vma;
11291 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11295 dyn.d_un.d_val = 0;
11296 dyn.d_un.d_ptr = 0;
11297 for (i = 1; i < elf_numsections (abfd); i++)
11299 Elf_Internal_Shdr *hdr;
11301 hdr = elf_elfsections (abfd)[i];
11302 if (hdr->sh_type == type
11303 && (hdr->sh_flags & SHF_ALLOC) != 0)
11305 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11306 dyn.d_un.d_val += hdr->sh_size;
11309 if (dyn.d_un.d_ptr == 0
11310 || hdr->sh_addr < dyn.d_un.d_ptr)
11311 dyn.d_un.d_ptr = hdr->sh_addr;
11317 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11321 /* If we have created any dynamic sections, then output them. */
11322 if (dynobj != NULL)
11324 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11327 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11328 if (((info->warn_shared_textrel && info->shared)
11329 || info->error_textrel)
11330 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11332 bfd_byte *dyncon, *dynconend;
11334 dyncon = o->contents;
11335 dynconend = o->contents + o->size;
11336 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11338 Elf_Internal_Dyn dyn;
11340 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11342 if (dyn.d_tag == DT_TEXTREL)
11344 if (info->error_textrel)
11345 info->callbacks->einfo
11346 (_("%P%X: read-only segment has dynamic relocations.\n"));
11348 info->callbacks->einfo
11349 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11355 for (o = dynobj->sections; o != NULL; o = o->next)
11357 if ((o->flags & SEC_HAS_CONTENTS) == 0
11359 || o->output_section == bfd_abs_section_ptr)
11361 if ((o->flags & SEC_LINKER_CREATED) == 0)
11363 /* At this point, we are only interested in sections
11364 created by _bfd_elf_link_create_dynamic_sections. */
11367 if (elf_hash_table (info)->stab_info.stabstr == o)
11369 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11371 if (strcmp (o->name, ".dynstr") != 0)
11373 /* FIXME: octets_per_byte. */
11374 if (! bfd_set_section_contents (abfd, o->output_section,
11376 (file_ptr) o->output_offset,
11382 /* The contents of the .dynstr section are actually in a
11384 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11385 if (bfd_seek (abfd, off, SEEK_SET) != 0
11386 || ! _bfd_elf_strtab_emit (abfd,
11387 elf_hash_table (info)->dynstr))
11393 if (info->relocatable)
11395 bfd_boolean failed = FALSE;
11397 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11402 /* If we have optimized stabs strings, output them. */
11403 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11405 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11409 if (info->eh_frame_hdr)
11411 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11415 if (flinfo.symstrtab != NULL)
11416 _bfd_stringtab_free (flinfo.symstrtab);
11417 if (flinfo.contents != NULL)
11418 free (flinfo.contents);
11419 if (flinfo.external_relocs != NULL)
11420 free (flinfo.external_relocs);
11421 if (flinfo.internal_relocs != NULL)
11422 free (flinfo.internal_relocs);
11423 if (flinfo.external_syms != NULL)
11424 free (flinfo.external_syms);
11425 if (flinfo.locsym_shndx != NULL)
11426 free (flinfo.locsym_shndx);
11427 if (flinfo.internal_syms != NULL)
11428 free (flinfo.internal_syms);
11429 if (flinfo.indices != NULL)
11430 free (flinfo.indices);
11431 if (flinfo.sections != NULL)
11432 free (flinfo.sections);
11433 if (flinfo.symbuf != NULL)
11434 free (flinfo.symbuf);
11435 if (flinfo.symshndxbuf != NULL)
11436 free (flinfo.symshndxbuf);
11437 for (o = abfd->sections; o != NULL; o = o->next)
11439 struct bfd_elf_section_data *esdo = elf_section_data (o);
11440 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11441 free (esdo->rel.hashes);
11442 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11443 free (esdo->rela.hashes);
11446 elf_tdata (abfd)->linker = TRUE;
11450 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11451 if (contents == NULL)
11452 return FALSE; /* Bail out and fail. */
11453 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11454 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11461 if (flinfo.symstrtab != NULL)
11462 _bfd_stringtab_free (flinfo.symstrtab);
11463 if (flinfo.contents != NULL)
11464 free (flinfo.contents);
11465 if (flinfo.external_relocs != NULL)
11466 free (flinfo.external_relocs);
11467 if (flinfo.internal_relocs != NULL)
11468 free (flinfo.internal_relocs);
11469 if (flinfo.external_syms != NULL)
11470 free (flinfo.external_syms);
11471 if (flinfo.locsym_shndx != NULL)
11472 free (flinfo.locsym_shndx);
11473 if (flinfo.internal_syms != NULL)
11474 free (flinfo.internal_syms);
11475 if (flinfo.indices != NULL)
11476 free (flinfo.indices);
11477 if (flinfo.sections != NULL)
11478 free (flinfo.sections);
11479 if (flinfo.symbuf != NULL)
11480 free (flinfo.symbuf);
11481 if (flinfo.symshndxbuf != NULL)
11482 free (flinfo.symshndxbuf);
11483 for (o = abfd->sections; o != NULL; o = o->next)
11485 struct bfd_elf_section_data *esdo = elf_section_data (o);
11486 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11487 free (esdo->rel.hashes);
11488 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11489 free (esdo->rela.hashes);
11495 /* Initialize COOKIE for input bfd ABFD. */
11498 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11499 struct bfd_link_info *info, bfd *abfd)
11501 Elf_Internal_Shdr *symtab_hdr;
11502 const struct elf_backend_data *bed;
11504 bed = get_elf_backend_data (abfd);
11505 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11507 cookie->abfd = abfd;
11508 cookie->sym_hashes = elf_sym_hashes (abfd);
11509 cookie->bad_symtab = elf_bad_symtab (abfd);
11510 if (cookie->bad_symtab)
11512 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11513 cookie->extsymoff = 0;
11517 cookie->locsymcount = symtab_hdr->sh_info;
11518 cookie->extsymoff = symtab_hdr->sh_info;
11521 if (bed->s->arch_size == 32)
11522 cookie->r_sym_shift = 8;
11524 cookie->r_sym_shift = 32;
11526 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11527 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11529 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11530 cookie->locsymcount, 0,
11532 if (cookie->locsyms == NULL)
11534 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11537 if (info->keep_memory)
11538 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11543 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11546 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11548 Elf_Internal_Shdr *symtab_hdr;
11550 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11551 if (cookie->locsyms != NULL
11552 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11553 free (cookie->locsyms);
11556 /* Initialize the relocation information in COOKIE for input section SEC
11557 of input bfd ABFD. */
11560 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11561 struct bfd_link_info *info, bfd *abfd,
11564 const struct elf_backend_data *bed;
11566 if (sec->reloc_count == 0)
11568 cookie->rels = NULL;
11569 cookie->relend = NULL;
11573 bed = get_elf_backend_data (abfd);
11575 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11576 info->keep_memory);
11577 if (cookie->rels == NULL)
11579 cookie->rel = cookie->rels;
11580 cookie->relend = (cookie->rels
11581 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11583 cookie->rel = cookie->rels;
11587 /* Free the memory allocated by init_reloc_cookie_rels,
11591 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11594 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11595 free (cookie->rels);
11598 /* Initialize the whole of COOKIE for input section SEC. */
11601 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11602 struct bfd_link_info *info,
11605 if (!init_reloc_cookie (cookie, info, sec->owner))
11607 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11612 fini_reloc_cookie (cookie, sec->owner);
11617 /* Free the memory allocated by init_reloc_cookie_for_section,
11621 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11624 fini_reloc_cookie_rels (cookie, sec);
11625 fini_reloc_cookie (cookie, sec->owner);
11628 /* Garbage collect unused sections. */
11630 /* Default gc_mark_hook. */
11633 _bfd_elf_gc_mark_hook (asection *sec,
11634 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11635 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11636 struct elf_link_hash_entry *h,
11637 Elf_Internal_Sym *sym)
11639 const char *sec_name;
11643 switch (h->root.type)
11645 case bfd_link_hash_defined:
11646 case bfd_link_hash_defweak:
11647 return h->root.u.def.section;
11649 case bfd_link_hash_common:
11650 return h->root.u.c.p->section;
11652 case bfd_link_hash_undefined:
11653 case bfd_link_hash_undefweak:
11654 /* To work around a glibc bug, keep all XXX input sections
11655 when there is an as yet undefined reference to __start_XXX
11656 or __stop_XXX symbols. The linker will later define such
11657 symbols for orphan input sections that have a name
11658 representable as a C identifier. */
11659 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11660 sec_name = h->root.root.string + 8;
11661 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11662 sec_name = h->root.root.string + 7;
11666 if (sec_name && *sec_name != '\0')
11670 for (i = info->input_bfds; i; i = i->link_next)
11672 sec = bfd_get_section_by_name (i, sec_name);
11674 sec->flags |= SEC_KEEP;
11684 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11689 /* COOKIE->rel describes a relocation against section SEC, which is
11690 a section we've decided to keep. Return the section that contains
11691 the relocation symbol, or NULL if no section contains it. */
11694 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11695 elf_gc_mark_hook_fn gc_mark_hook,
11696 struct elf_reloc_cookie *cookie)
11698 unsigned long r_symndx;
11699 struct elf_link_hash_entry *h;
11701 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11702 if (r_symndx == STN_UNDEF)
11705 if (r_symndx >= cookie->locsymcount
11706 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11708 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11709 while (h->root.type == bfd_link_hash_indirect
11710 || h->root.type == bfd_link_hash_warning)
11711 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11713 /* If this symbol is weak and there is a non-weak definition, we
11714 keep the non-weak definition because many backends put
11715 dynamic reloc info on the non-weak definition for code
11716 handling copy relocs. */
11717 if (h->u.weakdef != NULL)
11718 h->u.weakdef->mark = 1;
11719 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11722 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11723 &cookie->locsyms[r_symndx]);
11726 /* COOKIE->rel describes a relocation against section SEC, which is
11727 a section we've decided to keep. Mark the section that contains
11728 the relocation symbol. */
11731 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11733 elf_gc_mark_hook_fn gc_mark_hook,
11734 struct elf_reloc_cookie *cookie)
11738 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11739 if (rsec && !rsec->gc_mark)
11741 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
11742 || (rsec->owner->flags & DYNAMIC) != 0)
11744 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11750 /* The mark phase of garbage collection. For a given section, mark
11751 it and any sections in this section's group, and all the sections
11752 which define symbols to which it refers. */
11755 _bfd_elf_gc_mark (struct bfd_link_info *info,
11757 elf_gc_mark_hook_fn gc_mark_hook)
11760 asection *group_sec, *eh_frame;
11764 /* Mark all the sections in the group. */
11765 group_sec = elf_section_data (sec)->next_in_group;
11766 if (group_sec && !group_sec->gc_mark)
11767 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11770 /* Look through the section relocs. */
11772 eh_frame = elf_eh_frame_section (sec->owner);
11773 if ((sec->flags & SEC_RELOC) != 0
11774 && sec->reloc_count > 0
11775 && sec != eh_frame)
11777 struct elf_reloc_cookie cookie;
11779 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11783 for (; cookie.rel < cookie.relend; cookie.rel++)
11784 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11789 fini_reloc_cookie_for_section (&cookie, sec);
11793 if (ret && eh_frame && elf_fde_list (sec))
11795 struct elf_reloc_cookie cookie;
11797 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11801 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11802 gc_mark_hook, &cookie))
11804 fini_reloc_cookie_for_section (&cookie, eh_frame);
11811 /* Keep debug and special sections. */
11814 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
11815 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
11819 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11822 bfd_boolean some_kept;
11824 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
11827 /* Ensure all linker created sections are kept, and see whether
11828 any other section is already marked. */
11830 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11832 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11834 else if (isec->gc_mark)
11838 /* If no section in this file will be kept, then we can
11839 toss out debug sections. */
11843 /* Keep debug and special sections like .comment when they are
11844 not part of a group, or when we have single-member groups. */
11845 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11846 if ((elf_next_in_group (isec) == NULL
11847 || elf_next_in_group (isec) == isec)
11848 && ((isec->flags & SEC_DEBUGGING) != 0
11849 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
11855 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11857 struct elf_gc_sweep_symbol_info
11859 struct bfd_link_info *info;
11860 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11865 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11868 && (((h->root.type == bfd_link_hash_defined
11869 || h->root.type == bfd_link_hash_defweak)
11870 && !(h->def_regular
11871 && h->root.u.def.section->gc_mark))
11872 || h->root.type == bfd_link_hash_undefined
11873 || h->root.type == bfd_link_hash_undefweak))
11875 struct elf_gc_sweep_symbol_info *inf;
11877 inf = (struct elf_gc_sweep_symbol_info *) data;
11878 (*inf->hide_symbol) (inf->info, h, TRUE);
11879 h->def_regular = 0;
11880 h->ref_regular = 0;
11881 h->ref_regular_nonweak = 0;
11887 /* The sweep phase of garbage collection. Remove all garbage sections. */
11889 typedef bfd_boolean (*gc_sweep_hook_fn)
11890 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11893 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11896 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11897 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11898 unsigned long section_sym_count;
11899 struct elf_gc_sweep_symbol_info sweep_info;
11901 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11905 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11908 for (o = sub->sections; o != NULL; o = o->next)
11910 /* When any section in a section group is kept, we keep all
11911 sections in the section group. If the first member of
11912 the section group is excluded, we will also exclude the
11914 if (o->flags & SEC_GROUP)
11916 asection *first = elf_next_in_group (o);
11917 o->gc_mark = first->gc_mark;
11923 /* Skip sweeping sections already excluded. */
11924 if (o->flags & SEC_EXCLUDE)
11927 /* Since this is early in the link process, it is simple
11928 to remove a section from the output. */
11929 o->flags |= SEC_EXCLUDE;
11931 if (info->print_gc_sections && o->size != 0)
11932 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11934 /* But we also have to update some of the relocation
11935 info we collected before. */
11937 && (o->flags & SEC_RELOC) != 0
11938 && o->reloc_count > 0
11939 && !bfd_is_abs_section (o->output_section))
11941 Elf_Internal_Rela *internal_relocs;
11945 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11946 info->keep_memory);
11947 if (internal_relocs == NULL)
11950 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11952 if (elf_section_data (o)->relocs != internal_relocs)
11953 free (internal_relocs);
11961 /* Remove the symbols that were in the swept sections from the dynamic
11962 symbol table. GCFIXME: Anyone know how to get them out of the
11963 static symbol table as well? */
11964 sweep_info.info = info;
11965 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11966 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11969 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
11973 /* Propagate collected vtable information. This is called through
11974 elf_link_hash_traverse. */
11977 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11979 /* Those that are not vtables. */
11980 if (h->vtable == NULL || h->vtable->parent == NULL)
11983 /* Those vtables that do not have parents, we cannot merge. */
11984 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11987 /* If we've already been done, exit. */
11988 if (h->vtable->used && h->vtable->used[-1])
11991 /* Make sure the parent's table is up to date. */
11992 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11994 if (h->vtable->used == NULL)
11996 /* None of this table's entries were referenced. Re-use the
11998 h->vtable->used = h->vtable->parent->vtable->used;
11999 h->vtable->size = h->vtable->parent->vtable->size;
12004 bfd_boolean *cu, *pu;
12006 /* Or the parent's entries into ours. */
12007 cu = h->vtable->used;
12009 pu = h->vtable->parent->vtable->used;
12012 const struct elf_backend_data *bed;
12013 unsigned int log_file_align;
12015 bed = get_elf_backend_data (h->root.u.def.section->owner);
12016 log_file_align = bed->s->log_file_align;
12017 n = h->vtable->parent->vtable->size >> log_file_align;
12032 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12035 bfd_vma hstart, hend;
12036 Elf_Internal_Rela *relstart, *relend, *rel;
12037 const struct elf_backend_data *bed;
12038 unsigned int log_file_align;
12040 /* Take care of both those symbols that do not describe vtables as
12041 well as those that are not loaded. */
12042 if (h->vtable == NULL || h->vtable->parent == NULL)
12045 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12046 || h->root.type == bfd_link_hash_defweak);
12048 sec = h->root.u.def.section;
12049 hstart = h->root.u.def.value;
12050 hend = hstart + h->size;
12052 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12054 return *(bfd_boolean *) okp = FALSE;
12055 bed = get_elf_backend_data (sec->owner);
12056 log_file_align = bed->s->log_file_align;
12058 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12060 for (rel = relstart; rel < relend; ++rel)
12061 if (rel->r_offset >= hstart && rel->r_offset < hend)
12063 /* If the entry is in use, do nothing. */
12064 if (h->vtable->used
12065 && (rel->r_offset - hstart) < h->vtable->size)
12067 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12068 if (h->vtable->used[entry])
12071 /* Otherwise, kill it. */
12072 rel->r_offset = rel->r_info = rel->r_addend = 0;
12078 /* Mark sections containing dynamically referenced symbols. When
12079 building shared libraries, we must assume that any visible symbol is
12083 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12085 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12087 if ((h->root.type == bfd_link_hash_defined
12088 || h->root.type == bfd_link_hash_defweak)
12090 || ((!info->executable || info->export_dynamic)
12092 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12093 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12094 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
12095 || !bfd_hide_sym_by_version (info->version_info,
12096 h->root.root.string)))))
12097 h->root.u.def.section->flags |= SEC_KEEP;
12102 /* Keep all sections containing symbols undefined on the command-line,
12103 and the section containing the entry symbol. */
12106 _bfd_elf_gc_keep (struct bfd_link_info *info)
12108 struct bfd_sym_chain *sym;
12110 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12112 struct elf_link_hash_entry *h;
12114 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12115 FALSE, FALSE, FALSE);
12118 && (h->root.type == bfd_link_hash_defined
12119 || h->root.type == bfd_link_hash_defweak)
12120 && !bfd_is_abs_section (h->root.u.def.section))
12121 h->root.u.def.section->flags |= SEC_KEEP;
12125 /* Do mark and sweep of unused sections. */
12128 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12130 bfd_boolean ok = TRUE;
12132 elf_gc_mark_hook_fn gc_mark_hook;
12133 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12135 if (!bed->can_gc_sections
12136 || !is_elf_hash_table (info->hash))
12138 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12142 bed->gc_keep (info);
12144 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12145 at the .eh_frame section if we can mark the FDEs individually. */
12146 _bfd_elf_begin_eh_frame_parsing (info);
12147 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12150 struct elf_reloc_cookie cookie;
12152 sec = bfd_get_section_by_name (sub, ".eh_frame");
12153 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12155 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12156 if (elf_section_data (sec)->sec_info
12157 && (sec->flags & SEC_LINKER_CREATED) == 0)
12158 elf_eh_frame_section (sub) = sec;
12159 fini_reloc_cookie_for_section (&cookie, sec);
12160 sec = bfd_get_next_section_by_name (sec);
12163 _bfd_elf_end_eh_frame_parsing (info);
12165 /* Apply transitive closure to the vtable entry usage info. */
12166 elf_link_hash_traverse (elf_hash_table (info),
12167 elf_gc_propagate_vtable_entries_used,
12172 /* Kill the vtable relocations that were not used. */
12173 elf_link_hash_traverse (elf_hash_table (info),
12174 elf_gc_smash_unused_vtentry_relocs,
12179 /* Mark dynamically referenced symbols. */
12180 if (elf_hash_table (info)->dynamic_sections_created)
12181 elf_link_hash_traverse (elf_hash_table (info),
12182 bed->gc_mark_dynamic_ref,
12185 /* Grovel through relocs to find out who stays ... */
12186 gc_mark_hook = bed->gc_mark_hook;
12187 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12191 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12194 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12195 Also treat note sections as a root, if the section is not part
12197 for (o = sub->sections; o != NULL; o = o->next)
12199 && (o->flags & SEC_EXCLUDE) == 0
12200 && ((o->flags & SEC_KEEP) != 0
12201 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12202 && elf_next_in_group (o) == NULL )))
12204 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12209 /* Allow the backend to mark additional target specific sections. */
12210 bed->gc_mark_extra_sections (info, gc_mark_hook);
12212 /* ... and mark SEC_EXCLUDE for those that go. */
12213 return elf_gc_sweep (abfd, info);
12216 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12219 bfd_elf_gc_record_vtinherit (bfd *abfd,
12221 struct elf_link_hash_entry *h,
12224 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12225 struct elf_link_hash_entry **search, *child;
12226 bfd_size_type extsymcount;
12227 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12229 /* The sh_info field of the symtab header tells us where the
12230 external symbols start. We don't care about the local symbols at
12232 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12233 if (!elf_bad_symtab (abfd))
12234 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12236 sym_hashes = elf_sym_hashes (abfd);
12237 sym_hashes_end = sym_hashes + extsymcount;
12239 /* Hunt down the child symbol, which is in this section at the same
12240 offset as the relocation. */
12241 for (search = sym_hashes; search != sym_hashes_end; ++search)
12243 if ((child = *search) != NULL
12244 && (child->root.type == bfd_link_hash_defined
12245 || child->root.type == bfd_link_hash_defweak)
12246 && child->root.u.def.section == sec
12247 && child->root.u.def.value == offset)
12251 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12252 abfd, sec, (unsigned long) offset);
12253 bfd_set_error (bfd_error_invalid_operation);
12257 if (!child->vtable)
12259 child->vtable = (struct elf_link_virtual_table_entry *)
12260 bfd_zalloc (abfd, sizeof (*child->vtable));
12261 if (!child->vtable)
12266 /* This *should* only be the absolute section. It could potentially
12267 be that someone has defined a non-global vtable though, which
12268 would be bad. It isn't worth paging in the local symbols to be
12269 sure though; that case should simply be handled by the assembler. */
12271 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12274 child->vtable->parent = h;
12279 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12282 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12283 asection *sec ATTRIBUTE_UNUSED,
12284 struct elf_link_hash_entry *h,
12287 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12288 unsigned int log_file_align = bed->s->log_file_align;
12292 h->vtable = (struct elf_link_virtual_table_entry *)
12293 bfd_zalloc (abfd, sizeof (*h->vtable));
12298 if (addend >= h->vtable->size)
12300 size_t size, bytes, file_align;
12301 bfd_boolean *ptr = h->vtable->used;
12303 /* While the symbol is undefined, we have to be prepared to handle
12305 file_align = 1 << log_file_align;
12306 if (h->root.type == bfd_link_hash_undefined)
12307 size = addend + file_align;
12311 if (addend >= size)
12313 /* Oops! We've got a reference past the defined end of
12314 the table. This is probably a bug -- shall we warn? */
12315 size = addend + file_align;
12318 size = (size + file_align - 1) & -file_align;
12320 /* Allocate one extra entry for use as a "done" flag for the
12321 consolidation pass. */
12322 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12326 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12332 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12333 * sizeof (bfd_boolean));
12334 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12338 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12343 /* And arrange for that done flag to be at index -1. */
12344 h->vtable->used = ptr + 1;
12345 h->vtable->size = size;
12348 h->vtable->used[addend >> log_file_align] = TRUE;
12353 /* Map an ELF section header flag to its corresponding string. */
12357 flagword flag_value;
12358 } elf_flags_to_name_table;
12360 static elf_flags_to_name_table elf_flags_to_names [] =
12362 { "SHF_WRITE", SHF_WRITE },
12363 { "SHF_ALLOC", SHF_ALLOC },
12364 { "SHF_EXECINSTR", SHF_EXECINSTR },
12365 { "SHF_MERGE", SHF_MERGE },
12366 { "SHF_STRINGS", SHF_STRINGS },
12367 { "SHF_INFO_LINK", SHF_INFO_LINK},
12368 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12369 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12370 { "SHF_GROUP", SHF_GROUP },
12371 { "SHF_TLS", SHF_TLS },
12372 { "SHF_MASKOS", SHF_MASKOS },
12373 { "SHF_EXCLUDE", SHF_EXCLUDE },
12376 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12378 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12379 struct flag_info *flaginfo,
12382 const bfd_vma sh_flags = elf_section_flags (section);
12384 if (!flaginfo->flags_initialized)
12386 bfd *obfd = info->output_bfd;
12387 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12388 struct flag_info_list *tf = flaginfo->flag_list;
12390 int without_hex = 0;
12392 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12395 flagword (*lookup) (char *);
12397 lookup = bed->elf_backend_lookup_section_flags_hook;
12398 if (lookup != NULL)
12400 flagword hexval = (*lookup) ((char *) tf->name);
12404 if (tf->with == with_flags)
12405 with_hex |= hexval;
12406 else if (tf->with == without_flags)
12407 without_hex |= hexval;
12412 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12414 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12416 if (tf->with == with_flags)
12417 with_hex |= elf_flags_to_names[i].flag_value;
12418 else if (tf->with == without_flags)
12419 without_hex |= elf_flags_to_names[i].flag_value;
12426 info->callbacks->einfo
12427 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12431 flaginfo->flags_initialized = TRUE;
12432 flaginfo->only_with_flags |= with_hex;
12433 flaginfo->not_with_flags |= without_hex;
12436 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
12439 if ((flaginfo->not_with_flags & sh_flags) != 0)
12445 struct alloc_got_off_arg {
12447 struct bfd_link_info *info;
12450 /* We need a special top-level link routine to convert got reference counts
12451 to real got offsets. */
12454 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12456 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12457 bfd *obfd = gofarg->info->output_bfd;
12458 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12460 if (h->got.refcount > 0)
12462 h->got.offset = gofarg->gotoff;
12463 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12466 h->got.offset = (bfd_vma) -1;
12471 /* And an accompanying bit to work out final got entry offsets once
12472 we're done. Should be called from final_link. */
12475 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12476 struct bfd_link_info *info)
12479 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12481 struct alloc_got_off_arg gofarg;
12483 BFD_ASSERT (abfd == info->output_bfd);
12485 if (! is_elf_hash_table (info->hash))
12488 /* The GOT offset is relative to the .got section, but the GOT header is
12489 put into the .got.plt section, if the backend uses it. */
12490 if (bed->want_got_plt)
12493 gotoff = bed->got_header_size;
12495 /* Do the local .got entries first. */
12496 for (i = info->input_bfds; i; i = i->link_next)
12498 bfd_signed_vma *local_got;
12499 bfd_size_type j, locsymcount;
12500 Elf_Internal_Shdr *symtab_hdr;
12502 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12505 local_got = elf_local_got_refcounts (i);
12509 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12510 if (elf_bad_symtab (i))
12511 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12513 locsymcount = symtab_hdr->sh_info;
12515 for (j = 0; j < locsymcount; ++j)
12517 if (local_got[j] > 0)
12519 local_got[j] = gotoff;
12520 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12523 local_got[j] = (bfd_vma) -1;
12527 /* Then the global .got entries. .plt refcounts are handled by
12528 adjust_dynamic_symbol */
12529 gofarg.gotoff = gotoff;
12530 gofarg.info = info;
12531 elf_link_hash_traverse (elf_hash_table (info),
12532 elf_gc_allocate_got_offsets,
12537 /* Many folk need no more in the way of final link than this, once
12538 got entry reference counting is enabled. */
12541 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12543 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12546 /* Invoke the regular ELF backend linker to do all the work. */
12547 return bfd_elf_final_link (abfd, info);
12551 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12553 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12555 if (rcookie->bad_symtab)
12556 rcookie->rel = rcookie->rels;
12558 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12560 unsigned long r_symndx;
12562 if (! rcookie->bad_symtab)
12563 if (rcookie->rel->r_offset > offset)
12565 if (rcookie->rel->r_offset != offset)
12568 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12569 if (r_symndx == STN_UNDEF)
12572 if (r_symndx >= rcookie->locsymcount
12573 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12575 struct elf_link_hash_entry *h;
12577 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12579 while (h->root.type == bfd_link_hash_indirect
12580 || h->root.type == bfd_link_hash_warning)
12581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12583 if ((h->root.type == bfd_link_hash_defined
12584 || h->root.type == bfd_link_hash_defweak)
12585 && discarded_section (h->root.u.def.section))
12592 /* It's not a relocation against a global symbol,
12593 but it could be a relocation against a local
12594 symbol for a discarded section. */
12596 Elf_Internal_Sym *isym;
12598 /* Need to: get the symbol; get the section. */
12599 isym = &rcookie->locsyms[r_symndx];
12600 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12601 if (isec != NULL && discarded_section (isec))
12609 /* Discard unneeded references to discarded sections.
12610 Returns TRUE if any section's size was changed. */
12611 /* This function assumes that the relocations are in sorted order,
12612 which is true for all known assemblers. */
12615 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12617 struct elf_reloc_cookie cookie;
12618 asection *stab, *eh;
12619 const struct elf_backend_data *bed;
12621 bfd_boolean ret = FALSE;
12623 if (info->traditional_format
12624 || !is_elf_hash_table (info->hash))
12627 _bfd_elf_begin_eh_frame_parsing (info);
12628 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12630 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12633 bed = get_elf_backend_data (abfd);
12636 if (!info->relocatable)
12638 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12641 || bfd_is_abs_section (eh->output_section)))
12642 eh = bfd_get_next_section_by_name (eh);
12645 stab = bfd_get_section_by_name (abfd, ".stab");
12647 && (stab->size == 0
12648 || bfd_is_abs_section (stab->output_section)
12649 || stab->sec_info_type != SEC_INFO_TYPE_STABS))
12654 && bed->elf_backend_discard_info == NULL)
12657 if (!init_reloc_cookie (&cookie, info, abfd))
12661 && stab->reloc_count > 0
12662 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12664 if (_bfd_discard_section_stabs (abfd, stab,
12665 elf_section_data (stab)->sec_info,
12666 bfd_elf_reloc_symbol_deleted_p,
12669 fini_reloc_cookie_rels (&cookie, stab);
12673 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12675 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12676 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12677 bfd_elf_reloc_symbol_deleted_p,
12680 fini_reloc_cookie_rels (&cookie, eh);
12681 eh = bfd_get_next_section_by_name (eh);
12684 if (bed->elf_backend_discard_info != NULL
12685 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12688 fini_reloc_cookie (&cookie, abfd);
12690 _bfd_elf_end_eh_frame_parsing (info);
12692 if (info->eh_frame_hdr
12693 && !info->relocatable
12694 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12701 _bfd_elf_section_already_linked (bfd *abfd,
12703 struct bfd_link_info *info)
12706 const char *name, *key;
12707 struct bfd_section_already_linked *l;
12708 struct bfd_section_already_linked_hash_entry *already_linked_list;
12710 if (sec->output_section == bfd_abs_section_ptr)
12713 flags = sec->flags;
12715 /* Return if it isn't a linkonce section. A comdat group section
12716 also has SEC_LINK_ONCE set. */
12717 if ((flags & SEC_LINK_ONCE) == 0)
12720 /* Don't put group member sections on our list of already linked
12721 sections. They are handled as a group via their group section. */
12722 if (elf_sec_group (sec) != NULL)
12725 /* For a SHT_GROUP section, use the group signature as the key. */
12727 if ((flags & SEC_GROUP) != 0
12728 && elf_next_in_group (sec) != NULL
12729 && elf_group_name (elf_next_in_group (sec)) != NULL)
12730 key = elf_group_name (elf_next_in_group (sec));
12733 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
12734 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12735 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12738 /* Must be a user linkonce section that doesn't follow gcc's
12739 naming convention. In this case we won't be matching
12740 single member groups. */
12744 already_linked_list = bfd_section_already_linked_table_lookup (key);
12746 for (l = already_linked_list->entry; l != NULL; l = l->next)
12748 /* We may have 2 different types of sections on the list: group
12749 sections with a signature of <key> (<key> is some string),
12750 and linkonce sections named .gnu.linkonce.<type>.<key>.
12751 Match like sections. LTO plugin sections are an exception.
12752 They are always named .gnu.linkonce.t.<key> and match either
12753 type of section. */
12754 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12755 && ((flags & SEC_GROUP) != 0
12756 || strcmp (name, l->sec->name) == 0))
12757 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
12759 /* The section has already been linked. See if we should
12760 issue a warning. */
12761 if (!_bfd_handle_already_linked (sec, l, info))
12764 if (flags & SEC_GROUP)
12766 asection *first = elf_next_in_group (sec);
12767 asection *s = first;
12771 s->output_section = bfd_abs_section_ptr;
12772 /* Record which group discards it. */
12773 s->kept_section = l->sec;
12774 s = elf_next_in_group (s);
12775 /* These lists are circular. */
12785 /* A single member comdat group section may be discarded by a
12786 linkonce section and vice versa. */
12787 if ((flags & SEC_GROUP) != 0)
12789 asection *first = elf_next_in_group (sec);
12791 if (first != NULL && elf_next_in_group (first) == first)
12792 /* Check this single member group against linkonce sections. */
12793 for (l = already_linked_list->entry; l != NULL; l = l->next)
12794 if ((l->sec->flags & SEC_GROUP) == 0
12795 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12797 first->output_section = bfd_abs_section_ptr;
12798 first->kept_section = l->sec;
12799 sec->output_section = bfd_abs_section_ptr;
12804 /* Check this linkonce section against single member groups. */
12805 for (l = already_linked_list->entry; l != NULL; l = l->next)
12806 if (l->sec->flags & SEC_GROUP)
12808 asection *first = elf_next_in_group (l->sec);
12811 && elf_next_in_group (first) == first
12812 && bfd_elf_match_symbols_in_sections (first, sec, info))
12814 sec->output_section = bfd_abs_section_ptr;
12815 sec->kept_section = first;
12820 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12821 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12822 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12823 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12824 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12825 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12826 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12827 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12828 The reverse order cannot happen as there is never a bfd with only the
12829 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12830 matter as here were are looking only for cross-bfd sections. */
12832 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12833 for (l = already_linked_list->entry; l != NULL; l = l->next)
12834 if ((l->sec->flags & SEC_GROUP) == 0
12835 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12837 if (abfd != l->sec->owner)
12838 sec->output_section = bfd_abs_section_ptr;
12842 /* This is the first section with this name. Record it. */
12843 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
12844 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
12845 return sec->output_section == bfd_abs_section_ptr;
12849 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12851 return sym->st_shndx == SHN_COMMON;
12855 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12861 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12863 return bfd_com_section_ptr;
12867 _bfd_elf_default_got_elt_size (bfd *abfd,
12868 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12869 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12870 bfd *ibfd ATTRIBUTE_UNUSED,
12871 unsigned long symndx ATTRIBUTE_UNUSED)
12873 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12874 return bed->s->arch_size / 8;
12877 /* Routines to support the creation of dynamic relocs. */
12879 /* Returns the name of the dynamic reloc section associated with SEC. */
12881 static const char *
12882 get_dynamic_reloc_section_name (bfd * abfd,
12884 bfd_boolean is_rela)
12887 const char *old_name = bfd_get_section_name (NULL, sec);
12888 const char *prefix = is_rela ? ".rela" : ".rel";
12890 if (old_name == NULL)
12893 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12894 sprintf (name, "%s%s", prefix, old_name);
12899 /* Returns the dynamic reloc section associated with SEC.
12900 If necessary compute the name of the dynamic reloc section based
12901 on SEC's name (looked up in ABFD's string table) and the setting
12905 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12907 bfd_boolean is_rela)
12909 asection * reloc_sec = elf_section_data (sec)->sreloc;
12911 if (reloc_sec == NULL)
12913 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12917 reloc_sec = bfd_get_linker_section (abfd, name);
12919 if (reloc_sec != NULL)
12920 elf_section_data (sec)->sreloc = reloc_sec;
12927 /* Returns the dynamic reloc section associated with SEC. If the
12928 section does not exist it is created and attached to the DYNOBJ
12929 bfd and stored in the SRELOC field of SEC's elf_section_data
12932 ALIGNMENT is the alignment for the newly created section and
12933 IS_RELA defines whether the name should be .rela.<SEC's name>
12934 or .rel.<SEC's name>. The section name is looked up in the
12935 string table associated with ABFD. */
12938 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12940 unsigned int alignment,
12942 bfd_boolean is_rela)
12944 asection * reloc_sec = elf_section_data (sec)->sreloc;
12946 if (reloc_sec == NULL)
12948 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12953 reloc_sec = bfd_get_linker_section (dynobj, name);
12955 if (reloc_sec == NULL)
12957 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
12958 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12959 if ((sec->flags & SEC_ALLOC) != 0)
12960 flags |= SEC_ALLOC | SEC_LOAD;
12962 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
12963 if (reloc_sec != NULL)
12965 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12970 elf_section_data (sec)->sreloc = reloc_sec;
12976 /* Copy the ELF symbol type associated with a linker hash entry. */
12978 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12979 struct bfd_link_hash_entry * hdest,
12980 struct bfd_link_hash_entry * hsrc)
12982 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12983 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12985 ehdest->type = ehsrc->type;
12986 ehdest->target_internal = ehsrc->target_internal;
12989 /* Append a RELA relocation REL to section S in BFD. */
12992 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12994 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12995 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12996 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12997 bed->s->swap_reloca_out (abfd, rel, loc);
13000 /* Append a REL relocation REL to section S in BFD. */
13003 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13005 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13006 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13007 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13008 bed->s->swap_reloca_out (abfd, rel, loc);