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
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_section_by_name (abfd, ".got");
108 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
111 flags = bed->dynamic_sec_flags;
113 s = bfd_make_section_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_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_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_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_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_with_flags (abfd, ".gnu.version",
224 flags | SEC_READONLY);
226 || ! bfd_set_section_alignment (abfd, s, 1))
229 s = bfd_make_section_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_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_with_flags (abfd, ".dynstr",
242 flags | SEC_READONLY);
246 s = bfd_make_section_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_with_flags (abfd, ".hash", flags | SEC_READONLY);
264 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
266 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
269 if (info->emit_gnu_hash)
271 s = bfd_make_section_with_flags (abfd, ".gnu.hash",
272 flags | SEC_READONLY);
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
276 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
277 4 32-bit words followed by variable count of 64-bit words, then
278 variable count of 32-bit words. */
279 if (bed->s->arch_size == 64)
280 elf_section_data (s)->this_hdr.sh_entsize = 0;
282 elf_section_data (s)->this_hdr.sh_entsize = 4;
285 /* Let the backend create the rest of the sections. This lets the
286 backend set the right flags. The backend will normally create
287 the .got and .plt sections. */
288 if (bed->elf_backend_create_dynamic_sections == NULL
289 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
292 elf_hash_table (info)->dynamic_sections_created = TRUE;
297 /* Create dynamic sections when linking against a dynamic object. */
300 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
302 flagword flags, pltflags;
303 struct elf_link_hash_entry *h;
305 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
306 struct elf_link_hash_table *htab = elf_hash_table (info);
308 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
309 .rel[a].bss sections. */
310 flags = bed->dynamic_sec_flags;
313 if (bed->plt_not_loaded)
314 /* We do not clear SEC_ALLOC here because we still want the OS to
315 allocate space for the section; it's just that there's nothing
316 to read in from the object file. */
317 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
319 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
320 if (bed->plt_readonly)
321 pltflags |= SEC_READONLY;
323 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
325 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
329 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
331 if (bed->want_plt_sym)
333 h = _bfd_elf_define_linkage_sym (abfd, info, s,
334 "_PROCEDURE_LINKAGE_TABLE_");
335 elf_hash_table (info)->hplt = h;
340 s = bfd_make_section_with_flags (abfd,
341 (bed->rela_plts_and_copies_p
342 ? ".rela.plt" : ".rel.plt"),
343 flags | SEC_READONLY);
345 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 if (! _bfd_elf_create_got_section (abfd, info))
352 if (bed->want_dynbss)
354 /* The .dynbss section is a place to put symbols which are defined
355 by dynamic objects, are referenced by regular objects, and are
356 not functions. We must allocate space for them in the process
357 image and use a R_*_COPY reloc to tell the dynamic linker to
358 initialize them at run time. The linker script puts the .dynbss
359 section into the .bss section of the final image. */
360 s = bfd_make_section_with_flags (abfd, ".dynbss",
362 | 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_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;
572 if (provide && hidden)
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_section_by_name (htab->dynobj, p->name)) != NULL
783 && (ip->flags & SEC_LINKER_CREATED)
784 && ip->output_section == p)
789 /* There shouldn't be section relative relocations
790 against any other section. */
796 /* Assign dynsym indices. In a shared library we generate a section
797 symbol for each output section, which come first. Next come symbols
798 which have been forced to local binding. Then all of the back-end
799 allocated local dynamic syms, followed by the rest of the global
803 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
804 struct bfd_link_info *info,
805 unsigned long *section_sym_count)
807 unsigned long dynsymcount = 0;
809 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
811 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
813 for (p = output_bfd->sections; p ; p = p->next)
814 if ((p->flags & SEC_EXCLUDE) == 0
815 && (p->flags & SEC_ALLOC) != 0
816 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
817 elf_section_data (p)->dynindx = ++dynsymcount;
819 elf_section_data (p)->dynindx = 0;
821 *section_sym_count = dynsymcount;
823 elf_link_hash_traverse (elf_hash_table (info),
824 elf_link_renumber_local_hash_table_dynsyms,
827 if (elf_hash_table (info)->dynlocal)
829 struct elf_link_local_dynamic_entry *p;
830 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
831 p->dynindx = ++dynsymcount;
834 elf_link_hash_traverse (elf_hash_table (info),
835 elf_link_renumber_hash_table_dynsyms,
838 /* There is an unused NULL entry at the head of the table which
839 we must account for in our count. Unless there weren't any
840 symbols, which means we'll have no table at all. */
841 if (dynsymcount != 0)
844 elf_hash_table (info)->dynsymcount = dynsymcount;
848 /* Merge st_other field. */
851 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
852 Elf_Internal_Sym *isym, bfd_boolean definition,
855 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
857 /* If st_other has a processor-specific meaning, specific
858 code might be needed here. We never merge the visibility
859 attribute with the one from a dynamic object. */
860 if (bed->elf_backend_merge_symbol_attribute)
861 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
864 /* If this symbol has default visibility and the user has requested
865 we not re-export it, then mark it as hidden. */
869 || (abfd->my_archive && abfd->my_archive->no_export))
870 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
871 isym->st_other = (STV_HIDDEN
872 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
874 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0)
876 unsigned char hvis, symvis, other, nvis;
878 /* Only merge the visibility. Leave the remainder of the
879 st_other field to elf_backend_merge_symbol_attribute. */
880 other = h->other & ~ELF_ST_VISIBILITY (-1);
882 /* Combine visibilities, using the most constraining one. */
883 hvis = ELF_ST_VISIBILITY (h->other);
884 symvis = ELF_ST_VISIBILITY (isym->st_other);
890 nvis = hvis < symvis ? hvis : symvis;
892 h->other = other | nvis;
896 /* This function is called when we want to define a new symbol. It
897 handles the various cases which arise when we find a definition in
898 a dynamic object, or when there is already a definition in a
899 dynamic object. The new symbol is described by NAME, SYM, PSEC,
900 and PVALUE. We set SYM_HASH to the hash table entry. We set
901 OVERRIDE if the old symbol is overriding a new definition. We set
902 TYPE_CHANGE_OK if it is OK for the type to change. We set
903 SIZE_CHANGE_OK if it is OK for the size to change. By OK to
904 change, we mean that we shouldn't warn if the type or size does
905 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic
906 object is overridden by a regular object. */
909 _bfd_elf_merge_symbol (bfd *abfd,
910 struct bfd_link_info *info,
912 Elf_Internal_Sym *sym,
915 unsigned int *pold_alignment,
916 struct elf_link_hash_entry **sym_hash,
918 bfd_boolean *override,
919 bfd_boolean *type_change_ok,
920 bfd_boolean *size_change_ok)
922 asection *sec, *oldsec;
923 struct elf_link_hash_entry *h;
924 struct elf_link_hash_entry *flip;
927 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
928 bfd_boolean newweak, oldweak, newfunc, oldfunc;
929 const struct elf_backend_data *bed;
935 bind = ELF_ST_BIND (sym->st_info);
937 /* Silently discard TLS symbols from --just-syms. There's no way to
938 combine a static TLS block with a new TLS block for this executable. */
939 if (ELF_ST_TYPE (sym->st_info) == STT_TLS
940 && sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
946 if (! bfd_is_und_section (sec))
947 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
949 h = ((struct elf_link_hash_entry *)
950 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
955 bed = get_elf_backend_data (abfd);
957 /* This code is for coping with dynamic objects, and is only useful
958 if we are doing an ELF link. */
959 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
962 /* For merging, we only care about real symbols. */
964 while (h->root.type == bfd_link_hash_indirect
965 || h->root.type == bfd_link_hash_warning)
966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
968 /* We have to check it for every instance since the first few may be
969 refereences and not all compilers emit symbol type for undefined
971 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
973 /* If we just created the symbol, mark it as being an ELF symbol.
974 Other than that, there is nothing to do--there is no merge issue
975 with a newly defined symbol--so we just return. */
977 if (h->root.type == bfd_link_hash_new)
983 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
986 switch (h->root.type)
993 case bfd_link_hash_undefined:
994 case bfd_link_hash_undefweak:
995 oldbfd = h->root.u.undef.abfd;
999 case bfd_link_hash_defined:
1000 case bfd_link_hash_defweak:
1001 oldbfd = h->root.u.def.section->owner;
1002 oldsec = h->root.u.def.section;
1005 case bfd_link_hash_common:
1006 oldbfd = h->root.u.c.p->section->owner;
1007 oldsec = h->root.u.c.p->section;
1011 /* Differentiate strong and weak symbols. */
1012 newweak = bind == STB_WEAK;
1013 oldweak = (h->root.type == bfd_link_hash_defweak
1014 || h->root.type == bfd_link_hash_undefweak);
1016 /* In cases involving weak versioned symbols, we may wind up trying
1017 to merge a symbol with itself. Catch that here, to avoid the
1018 confusion that results if we try to override a symbol with
1019 itself. The additional tests catch cases like
1020 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1021 dynamic object, which we do want to handle here. */
1023 && (newweak || oldweak)
1024 && ((abfd->flags & DYNAMIC) == 0
1025 || !h->def_regular))
1028 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1029 respectively, is from a dynamic object. */
1031 newdyn = (abfd->flags & DYNAMIC) != 0;
1035 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1036 else if (oldsec != NULL)
1038 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1039 indices used by MIPS ELF. */
1040 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1043 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1044 respectively, appear to be a definition rather than reference. */
1046 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1048 olddef = (h->root.type != bfd_link_hash_undefined
1049 && h->root.type != bfd_link_hash_undefweak
1050 && h->root.type != bfd_link_hash_common);
1052 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1053 respectively, appear to be a function. */
1055 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1056 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1058 oldfunc = (h->type != STT_NOTYPE
1059 && bed->is_function_type (h->type));
1061 /* When we try to create a default indirect symbol from the dynamic
1062 definition with the default version, we skip it if its type and
1063 the type of existing regular definition mismatch. We only do it
1064 if the existing regular definition won't be dynamic. */
1065 if (pold_alignment == NULL
1067 && !info->export_dynamic
1072 && (olddef || h->root.type == bfd_link_hash_common)
1073 && ELF_ST_TYPE (sym->st_info) != h->type
1074 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1075 && h->type != STT_NOTYPE
1076 && !(newfunc && oldfunc))
1082 /* Plugin symbol type isn't currently set. Stop bogus errors. */
1083 if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0)
1084 *type_change_ok = TRUE;
1086 /* Check TLS symbol. We don't check undefined symbol introduced by
1088 else if (oldbfd != NULL
1089 && ELF_ST_TYPE (sym->st_info) != h->type
1090 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1093 bfd_boolean ntdef, tdef;
1094 asection *ntsec, *tsec;
1096 if (h->type == STT_TLS)
1116 (*_bfd_error_handler)
1117 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"),
1118 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1119 else if (!tdef && !ntdef)
1120 (*_bfd_error_handler)
1121 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"),
1122 tbfd, ntbfd, h->root.root.string);
1124 (*_bfd_error_handler)
1125 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"),
1126 tbfd, tsec, ntbfd, h->root.root.string);
1128 (*_bfd_error_handler)
1129 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"),
1130 tbfd, ntbfd, ntsec, h->root.root.string);
1132 bfd_set_error (bfd_error_bad_value);
1136 /* We need to remember if a symbol has a definition in a dynamic
1137 object or is weak in all dynamic objects. Internal and hidden
1138 visibility will make it unavailable to dynamic objects. */
1139 if (newdyn && !h->dynamic_def)
1141 if (!bfd_is_und_section (sec))
1145 /* Check if this symbol is weak in all dynamic objects. If it
1146 is the first time we see it in a dynamic object, we mark
1147 if it is weak. Otherwise, we clear it. */
1148 if (!h->ref_dynamic)
1150 if (bind == STB_WEAK)
1151 h->dynamic_weak = 1;
1153 else if (bind != STB_WEAK)
1154 h->dynamic_weak = 0;
1158 /* If the old symbol has non-default visibility, we ignore the new
1159 definition from a dynamic object. */
1161 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1162 && !bfd_is_und_section (sec))
1165 /* Make sure this symbol is dynamic. */
1167 /* A protected symbol has external availability. Make sure it is
1168 recorded as dynamic.
1170 FIXME: Should we check type and size for protected symbol? */
1171 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1172 return bfd_elf_link_record_dynamic_symbol (info, h);
1177 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1180 /* If the new symbol with non-default visibility comes from a
1181 relocatable file and the old definition comes from a dynamic
1182 object, we remove the old definition. */
1183 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1185 /* Handle the case where the old dynamic definition is
1186 default versioned. We need to copy the symbol info from
1187 the symbol with default version to the normal one if it
1188 was referenced before. */
1191 struct elf_link_hash_entry *vh = *sym_hash;
1193 vh->root.type = h->root.type;
1194 h->root.type = bfd_link_hash_indirect;
1195 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h);
1196 /* Protected symbols will override the dynamic definition
1197 with default version. */
1198 if (ELF_ST_VISIBILITY (sym->st_other) == STV_PROTECTED)
1200 h->root.u.i.link = (struct bfd_link_hash_entry *) vh;
1201 vh->dynamic_def = 1;
1202 vh->ref_dynamic = 1;
1206 h->root.type = vh->root.type;
1207 vh->ref_dynamic = 0;
1208 /* We have to hide it here since it was made dynamic
1209 global with extra bits when the symbol info was
1210 copied from the old dynamic definition. */
1211 (*bed->elf_backend_hide_symbol) (info, vh, TRUE);
1219 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1220 && bfd_is_und_section (sec))
1222 /* If the new symbol is undefined and the old symbol was
1223 also undefined before, we need to make sure
1224 _bfd_generic_link_add_one_symbol doesn't mess
1225 up the linker hash table undefs list. Since the old
1226 definition came from a dynamic object, it is still on the
1228 h->root.type = bfd_link_hash_undefined;
1229 h->root.u.undef.abfd = abfd;
1233 h->root.type = bfd_link_hash_new;
1234 h->root.u.undef.abfd = NULL;
1242 /* FIXME: Should we check type and size for protected symbol? */
1248 if (bind == STB_GNU_UNIQUE)
1249 h->unique_global = 1;
1251 /* If a new weak symbol definition comes from a regular file and the
1252 old symbol comes from a dynamic library, we treat the new one as
1253 strong. Similarly, an old weak symbol definition from a regular
1254 file is treated as strong when the new symbol comes from a dynamic
1255 library. Further, an old weak symbol from a dynamic library is
1256 treated as strong if the new symbol is from a dynamic library.
1257 This reflects the way glibc's ld.so works.
1259 Do this before setting *type_change_ok or *size_change_ok so that
1260 we warn properly when dynamic library symbols are overridden. */
1262 if (newdef && !newdyn && olddyn)
1264 if (olddef && newdyn)
1267 /* Allow changes between different types of function symbol. */
1268 if (newfunc && oldfunc)
1269 *type_change_ok = TRUE;
1271 /* It's OK to change the type if either the existing symbol or the
1272 new symbol is weak. A type change is also OK if the old symbol
1273 is undefined and the new symbol is defined. */
1278 && h->root.type == bfd_link_hash_undefined))
1279 *type_change_ok = TRUE;
1281 /* It's OK to change the size if either the existing symbol or the
1282 new symbol is weak, or if the old symbol is undefined. */
1285 || h->root.type == bfd_link_hash_undefined)
1286 *size_change_ok = TRUE;
1288 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1289 symbol, respectively, appears to be a common symbol in a dynamic
1290 object. If a symbol appears in an uninitialized section, and is
1291 not weak, and is not a function, then it may be a common symbol
1292 which was resolved when the dynamic object was created. We want
1293 to treat such symbols specially, because they raise special
1294 considerations when setting the symbol size: if the symbol
1295 appears as a common symbol in a regular object, and the size in
1296 the regular object is larger, we must make sure that we use the
1297 larger size. This problematic case can always be avoided in C,
1298 but it must be handled correctly when using Fortran shared
1301 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1302 likewise for OLDDYNCOMMON and OLDDEF.
1304 Note that this test is just a heuristic, and that it is quite
1305 possible to have an uninitialized symbol in a shared object which
1306 is really a definition, rather than a common symbol. This could
1307 lead to some minor confusion when the symbol really is a common
1308 symbol in some regular object. However, I think it will be
1314 && (sec->flags & SEC_ALLOC) != 0
1315 && (sec->flags & SEC_LOAD) == 0
1318 newdyncommon = TRUE;
1320 newdyncommon = FALSE;
1324 && h->root.type == bfd_link_hash_defined
1326 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1327 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1330 olddyncommon = TRUE;
1332 olddyncommon = FALSE;
1334 /* We now know everything about the old and new symbols. We ask the
1335 backend to check if we can merge them. */
1336 if (bed->merge_symbol
1337 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue,
1338 pold_alignment, skip, override,
1339 type_change_ok, size_change_ok,
1340 &newdyn, &newdef, &newdyncommon, &newweak,
1342 &olddyn, &olddef, &olddyncommon, &oldweak,
1346 /* If both the old and the new symbols look like common symbols in a
1347 dynamic object, set the size of the symbol to the larger of the
1352 && sym->st_size != h->size)
1354 /* Since we think we have two common symbols, issue a multiple
1355 common warning if desired. Note that we only warn if the
1356 size is different. If the size is the same, we simply let
1357 the old symbol override the new one as normally happens with
1358 symbols defined in dynamic objects. */
1360 if (! ((*info->callbacks->multiple_common)
1361 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1364 if (sym->st_size > h->size)
1365 h->size = sym->st_size;
1367 *size_change_ok = TRUE;
1370 /* If we are looking at a dynamic object, and we have found a
1371 definition, we need to see if the symbol was already defined by
1372 some other object. If so, we want to use the existing
1373 definition, and we do not want to report a multiple symbol
1374 definition error; we do this by clobbering *PSEC to be
1375 bfd_und_section_ptr.
1377 We treat a common symbol as a definition if the symbol in the
1378 shared library is a function, since common symbols always
1379 represent variables; this can cause confusion in principle, but
1380 any such confusion would seem to indicate an erroneous program or
1381 shared library. We also permit a common symbol in a regular
1382 object to override a weak symbol in a shared object. */
1387 || (h->root.type == bfd_link_hash_common
1388 && (newweak || newfunc))))
1392 newdyncommon = FALSE;
1394 *psec = sec = bfd_und_section_ptr;
1395 *size_change_ok = TRUE;
1397 /* If we get here when the old symbol is a common symbol, then
1398 we are explicitly letting it override a weak symbol or
1399 function in a dynamic object, and we don't want to warn about
1400 a type change. If the old symbol is a defined symbol, a type
1401 change warning may still be appropriate. */
1403 if (h->root.type == bfd_link_hash_common)
1404 *type_change_ok = TRUE;
1407 /* Handle the special case of an old common symbol merging with a
1408 new symbol which looks like a common symbol in a shared object.
1409 We change *PSEC and *PVALUE to make the new symbol look like a
1410 common symbol, and let _bfd_generic_link_add_one_symbol do the
1414 && h->root.type == bfd_link_hash_common)
1418 newdyncommon = FALSE;
1419 *pvalue = sym->st_size;
1420 *psec = sec = bed->common_section (oldsec);
1421 *size_change_ok = TRUE;
1424 /* Skip weak definitions of symbols that are already defined. */
1425 if (newdef && olddef && newweak)
1427 /* Don't skip new non-IR weak syms. */
1428 if (!(oldbfd != NULL
1429 && (oldbfd->flags & BFD_PLUGIN) != 0
1430 && (abfd->flags & BFD_PLUGIN) == 0))
1433 /* Merge st_other. If the symbol already has a dynamic index,
1434 but visibility says it should not be visible, turn it into a
1436 elf_merge_st_other (abfd, h, sym, newdef, newdyn);
1437 if (h->dynindx != -1)
1438 switch (ELF_ST_VISIBILITY (h->other))
1442 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1447 /* If the old symbol is from a dynamic object, and the new symbol is
1448 a definition which is not from a dynamic object, then the new
1449 symbol overrides the old symbol. Symbols from regular files
1450 always take precedence over symbols from dynamic objects, even if
1451 they are defined after the dynamic object in the link.
1453 As above, we again permit a common symbol in a regular object to
1454 override a definition in a shared object if the shared object
1455 symbol is a function or is weak. */
1460 || (bfd_is_com_section (sec)
1461 && (oldweak || oldfunc)))
1466 /* Change the hash table entry to undefined, and let
1467 _bfd_generic_link_add_one_symbol do the right thing with the
1470 h->root.type = bfd_link_hash_undefined;
1471 h->root.u.undef.abfd = h->root.u.def.section->owner;
1472 *size_change_ok = TRUE;
1475 olddyncommon = FALSE;
1477 /* We again permit a type change when a common symbol may be
1478 overriding a function. */
1480 if (bfd_is_com_section (sec))
1484 /* If a common symbol overrides a function, make sure
1485 that it isn't defined dynamically nor has type
1488 h->type = STT_NOTYPE;
1490 *type_change_ok = TRUE;
1493 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1496 /* This union may have been set to be non-NULL when this symbol
1497 was seen in a dynamic object. We must force the union to be
1498 NULL, so that it is correct for a regular symbol. */
1499 h->verinfo.vertree = NULL;
1502 /* Handle the special case of a new common symbol merging with an
1503 old symbol that looks like it might be a common symbol defined in
1504 a shared object. Note that we have already handled the case in
1505 which a new common symbol should simply override the definition
1506 in the shared library. */
1509 && bfd_is_com_section (sec)
1512 /* It would be best if we could set the hash table entry to a
1513 common symbol, but we don't know what to use for the section
1514 or the alignment. */
1515 if (! ((*info->callbacks->multiple_common)
1516 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1519 /* If the presumed common symbol in the dynamic object is
1520 larger, pretend that the new symbol has its size. */
1522 if (h->size > *pvalue)
1525 /* We need to remember the alignment required by the symbol
1526 in the dynamic object. */
1527 BFD_ASSERT (pold_alignment);
1528 *pold_alignment = h->root.u.def.section->alignment_power;
1531 olddyncommon = FALSE;
1533 h->root.type = bfd_link_hash_undefined;
1534 h->root.u.undef.abfd = h->root.u.def.section->owner;
1536 *size_change_ok = TRUE;
1537 *type_change_ok = TRUE;
1539 if ((*sym_hash)->root.type == bfd_link_hash_indirect)
1542 h->verinfo.vertree = NULL;
1547 /* Handle the case where we had a versioned symbol in a dynamic
1548 library and now find a definition in a normal object. In this
1549 case, we make the versioned symbol point to the normal one. */
1550 flip->root.type = h->root.type;
1551 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1552 h->root.type = bfd_link_hash_indirect;
1553 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1554 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1558 flip->ref_dynamic = 1;
1565 /* This function is called to create an indirect symbol from the
1566 default for the symbol with the default version if needed. The
1567 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We
1568 set DYNSYM if the new indirect symbol is dynamic. */
1571 _bfd_elf_add_default_symbol (bfd *abfd,
1572 struct bfd_link_info *info,
1573 struct elf_link_hash_entry *h,
1575 Elf_Internal_Sym *sym,
1578 bfd_boolean *dynsym,
1579 bfd_boolean override)
1581 bfd_boolean type_change_ok;
1582 bfd_boolean size_change_ok;
1585 struct elf_link_hash_entry *hi;
1586 struct bfd_link_hash_entry *bh;
1587 const struct elf_backend_data *bed;
1588 bfd_boolean collect;
1589 bfd_boolean dynamic;
1591 size_t len, shortlen;
1594 /* If this symbol has a version, and it is the default version, we
1595 create an indirect symbol from the default name to the fully
1596 decorated name. This will cause external references which do not
1597 specify a version to be bound to this version of the symbol. */
1598 p = strchr (name, ELF_VER_CHR);
1599 if (p == NULL || p[1] != ELF_VER_CHR)
1604 /* We are overridden by an old definition. We need to check if we
1605 need to create the indirect symbol from the default name. */
1606 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE,
1608 BFD_ASSERT (hi != NULL);
1611 while (hi->root.type == bfd_link_hash_indirect
1612 || hi->root.type == bfd_link_hash_warning)
1614 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1620 bed = get_elf_backend_data (abfd);
1621 collect = bed->collect;
1622 dynamic = (abfd->flags & DYNAMIC) != 0;
1624 shortlen = p - name;
1625 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1626 if (shortname == NULL)
1628 memcpy (shortname, name, shortlen);
1629 shortname[shortlen] = '\0';
1631 /* We are going to create a new symbol. Merge it with any existing
1632 symbol with this name. For the purposes of the merge, act as
1633 though we were defining the symbol we just defined, although we
1634 actually going to define an indirect symbol. */
1635 type_change_ok = FALSE;
1636 size_change_ok = FALSE;
1638 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1639 NULL, &hi, &skip, &override,
1640 &type_change_ok, &size_change_ok))
1649 if (! (_bfd_generic_link_add_one_symbol
1650 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr,
1651 0, name, FALSE, collect, &bh)))
1653 hi = (struct elf_link_hash_entry *) bh;
1657 /* In this case the symbol named SHORTNAME is overriding the
1658 indirect symbol we want to add. We were planning on making
1659 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1660 is the name without a version. NAME is the fully versioned
1661 name, and it is the default version.
1663 Overriding means that we already saw a definition for the
1664 symbol SHORTNAME in a regular object, and it is overriding
1665 the symbol defined in the dynamic object.
1667 When this happens, we actually want to change NAME, the
1668 symbol we just added, to refer to SHORTNAME. This will cause
1669 references to NAME in the shared object to become references
1670 to SHORTNAME in the regular object. This is what we expect
1671 when we override a function in a shared object: that the
1672 references in the shared object will be mapped to the
1673 definition in the regular object. */
1675 while (hi->root.type == bfd_link_hash_indirect
1676 || hi->root.type == bfd_link_hash_warning)
1677 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1679 h->root.type = bfd_link_hash_indirect;
1680 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1684 hi->ref_dynamic = 1;
1688 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1693 /* Now set HI to H, so that the following code will set the
1694 other fields correctly. */
1698 /* Check if HI is a warning symbol. */
1699 if (hi->root.type == bfd_link_hash_warning)
1700 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1702 /* If there is a duplicate definition somewhere, then HI may not
1703 point to an indirect symbol. We will have reported an error to
1704 the user in that case. */
1706 if (hi->root.type == bfd_link_hash_indirect)
1708 struct elf_link_hash_entry *ht;
1710 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1711 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1713 /* See if the new flags lead us to realize that the symbol must
1719 if (! info->executable
1725 if (hi->ref_regular)
1731 /* We also need to define an indirection from the nondefault version
1735 len = strlen (name);
1736 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1737 if (shortname == NULL)
1739 memcpy (shortname, name, shortlen);
1740 memcpy (shortname + shortlen, p + 1, len - shortlen);
1742 /* Once again, merge with any existing symbol. */
1743 type_change_ok = FALSE;
1744 size_change_ok = FALSE;
1746 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value,
1747 NULL, &hi, &skip, &override,
1748 &type_change_ok, &size_change_ok))
1756 /* Here SHORTNAME is a versioned name, so we don't expect to see
1757 the type of override we do in the case above unless it is
1758 overridden by a versioned definition. */
1759 if (hi->root.type != bfd_link_hash_defined
1760 && hi->root.type != bfd_link_hash_defweak)
1761 (*_bfd_error_handler)
1762 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1768 if (! (_bfd_generic_link_add_one_symbol
1769 (info, abfd, shortname, BSF_INDIRECT,
1770 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1772 hi = (struct elf_link_hash_entry *) bh;
1774 /* If there is a duplicate definition somewhere, then HI may not
1775 point to an indirect symbol. We will have reported an error
1776 to the user in that case. */
1778 if (hi->root.type == bfd_link_hash_indirect)
1780 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1782 /* See if the new flags lead us to realize that the symbol
1788 if (! info->executable
1794 if (hi->ref_regular)
1804 /* This routine is used to export all defined symbols into the dynamic
1805 symbol table. It is called via elf_link_hash_traverse. */
1808 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1810 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1812 /* Ignore indirect symbols. These are added by the versioning code. */
1813 if (h->root.type == bfd_link_hash_indirect)
1816 /* Ignore this if we won't export it. */
1817 if (!eif->info->export_dynamic && !h->dynamic)
1820 if (h->dynindx == -1
1821 && (h->def_regular || h->ref_regular)
1822 && ! bfd_hide_sym_by_version (eif->info->version_info,
1823 h->root.root.string))
1825 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1835 /* Look through the symbols which are defined in other shared
1836 libraries and referenced here. Update the list of version
1837 dependencies. This will be put into the .gnu.version_r section.
1838 This function is called via elf_link_hash_traverse. */
1841 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1844 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1845 Elf_Internal_Verneed *t;
1846 Elf_Internal_Vernaux *a;
1849 /* We only care about symbols defined in shared objects with version
1854 || h->verinfo.verdef == NULL)
1857 /* See if we already know about this version. */
1858 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1862 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1865 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1866 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1872 /* This is a new version. Add it to tree we are building. */
1877 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1880 rinfo->failed = TRUE;
1884 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1885 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1886 elf_tdata (rinfo->info->output_bfd)->verref = t;
1890 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1893 rinfo->failed = TRUE;
1897 /* Note that we are copying a string pointer here, and testing it
1898 above. If bfd_elf_string_from_elf_section is ever changed to
1899 discard the string data when low in memory, this will have to be
1901 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1903 a->vna_flags = h->verinfo.verdef->vd_flags;
1904 a->vna_nextptr = t->vn_auxptr;
1906 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1909 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1916 /* Figure out appropriate versions for all the symbols. We may not
1917 have the version number script until we have read all of the input
1918 files, so until that point we don't know which symbols should be
1919 local. This function is called via elf_link_hash_traverse. */
1922 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1924 struct elf_info_failed *sinfo;
1925 struct bfd_link_info *info;
1926 const struct elf_backend_data *bed;
1927 struct elf_info_failed eif;
1931 sinfo = (struct elf_info_failed *) data;
1934 /* Fix the symbol flags. */
1937 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1940 sinfo->failed = TRUE;
1944 /* We only need version numbers for symbols defined in regular
1946 if (!h->def_regular)
1949 bed = get_elf_backend_data (info->output_bfd);
1950 p = strchr (h->root.root.string, ELF_VER_CHR);
1951 if (p != NULL && h->verinfo.vertree == NULL)
1953 struct bfd_elf_version_tree *t;
1958 /* There are two consecutive ELF_VER_CHR characters if this is
1959 not a hidden symbol. */
1961 if (*p == ELF_VER_CHR)
1967 /* If there is no version string, we can just return out. */
1975 /* Look for the version. If we find it, it is no longer weak. */
1976 for (t = sinfo->info->version_info; t != NULL; t = t->next)
1978 if (strcmp (t->name, p) == 0)
1982 struct bfd_elf_version_expr *d;
1984 len = p - h->root.root.string;
1985 alc = (char *) bfd_malloc (len);
1988 sinfo->failed = TRUE;
1991 memcpy (alc, h->root.root.string, len - 1);
1992 alc[len - 1] = '\0';
1993 if (alc[len - 2] == ELF_VER_CHR)
1994 alc[len - 2] = '\0';
1996 h->verinfo.vertree = t;
2000 if (t->globals.list != NULL)
2001 d = (*t->match) (&t->globals, NULL, alc);
2003 /* See if there is anything to force this symbol to
2005 if (d == NULL && t->locals.list != NULL)
2007 d = (*t->match) (&t->locals, NULL, alc);
2010 && ! info->export_dynamic)
2011 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2019 /* If we are building an application, we need to create a
2020 version node for this version. */
2021 if (t == NULL && info->executable)
2023 struct bfd_elf_version_tree **pp;
2026 /* If we aren't going to export this symbol, we don't need
2027 to worry about it. */
2028 if (h->dynindx == -1)
2032 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2035 sinfo->failed = TRUE;
2040 t->name_indx = (unsigned int) -1;
2044 /* Don't count anonymous version tag. */
2045 if (sinfo->info->version_info != NULL
2046 && sinfo->info->version_info->vernum == 0)
2048 for (pp = &sinfo->info->version_info;
2052 t->vernum = version_index;
2056 h->verinfo.vertree = t;
2060 /* We could not find the version for a symbol when
2061 generating a shared archive. Return an error. */
2062 (*_bfd_error_handler)
2063 (_("%B: version node not found for symbol %s"),
2064 info->output_bfd, h->root.root.string);
2065 bfd_set_error (bfd_error_bad_value);
2066 sinfo->failed = TRUE;
2074 /* If we don't have a version for this symbol, see if we can find
2076 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2081 = bfd_find_version_for_sym (sinfo->info->version_info,
2082 h->root.root.string, &hide);
2083 if (h->verinfo.vertree != NULL && hide)
2084 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2090 /* Read and swap the relocs from the section indicated by SHDR. This
2091 may be either a REL or a RELA section. The relocations are
2092 translated into RELA relocations and stored in INTERNAL_RELOCS,
2093 which should have already been allocated to contain enough space.
2094 The EXTERNAL_RELOCS are a buffer where the external form of the
2095 relocations should be stored.
2097 Returns FALSE if something goes wrong. */
2100 elf_link_read_relocs_from_section (bfd *abfd,
2102 Elf_Internal_Shdr *shdr,
2103 void *external_relocs,
2104 Elf_Internal_Rela *internal_relocs)
2106 const struct elf_backend_data *bed;
2107 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2108 const bfd_byte *erela;
2109 const bfd_byte *erelaend;
2110 Elf_Internal_Rela *irela;
2111 Elf_Internal_Shdr *symtab_hdr;
2114 /* Position ourselves at the start of the section. */
2115 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2118 /* Read the relocations. */
2119 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2122 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2123 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2125 bed = get_elf_backend_data (abfd);
2127 /* Convert the external relocations to the internal format. */
2128 if (shdr->sh_entsize == bed->s->sizeof_rel)
2129 swap_in = bed->s->swap_reloc_in;
2130 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2131 swap_in = bed->s->swap_reloca_in;
2134 bfd_set_error (bfd_error_wrong_format);
2138 erela = (const bfd_byte *) external_relocs;
2139 erelaend = erela + shdr->sh_size;
2140 irela = internal_relocs;
2141 while (erela < erelaend)
2145 (*swap_in) (abfd, erela, irela);
2146 r_symndx = ELF32_R_SYM (irela->r_info);
2147 if (bed->s->arch_size == 64)
2151 if ((size_t) r_symndx >= nsyms)
2153 (*_bfd_error_handler)
2154 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2155 " for offset 0x%lx in section `%A'"),
2157 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2158 bfd_set_error (bfd_error_bad_value);
2162 else if (r_symndx != STN_UNDEF)
2164 (*_bfd_error_handler)
2165 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2166 " when the object file has no symbol table"),
2168 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2169 bfd_set_error (bfd_error_bad_value);
2172 irela += bed->s->int_rels_per_ext_rel;
2173 erela += shdr->sh_entsize;
2179 /* Read and swap the relocs for a section O. They may have been
2180 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2181 not NULL, they are used as buffers to read into. They are known to
2182 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2183 the return value is allocated using either malloc or bfd_alloc,
2184 according to the KEEP_MEMORY argument. If O has two relocation
2185 sections (both REL and RELA relocations), then the REL_HDR
2186 relocations will appear first in INTERNAL_RELOCS, followed by the
2187 RELA_HDR relocations. */
2190 _bfd_elf_link_read_relocs (bfd *abfd,
2192 void *external_relocs,
2193 Elf_Internal_Rela *internal_relocs,
2194 bfd_boolean keep_memory)
2196 void *alloc1 = NULL;
2197 Elf_Internal_Rela *alloc2 = NULL;
2198 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2199 struct bfd_elf_section_data *esdo = elf_section_data (o);
2200 Elf_Internal_Rela *internal_rela_relocs;
2202 if (esdo->relocs != NULL)
2203 return esdo->relocs;
2205 if (o->reloc_count == 0)
2208 if (internal_relocs == NULL)
2212 size = o->reloc_count;
2213 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2215 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2217 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2218 if (internal_relocs == NULL)
2222 if (external_relocs == NULL)
2224 bfd_size_type size = 0;
2227 size += esdo->rel.hdr->sh_size;
2229 size += esdo->rela.hdr->sh_size;
2231 alloc1 = bfd_malloc (size);
2234 external_relocs = alloc1;
2237 internal_rela_relocs = internal_relocs;
2240 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2244 external_relocs = (((bfd_byte *) external_relocs)
2245 + esdo->rel.hdr->sh_size);
2246 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2247 * bed->s->int_rels_per_ext_rel);
2251 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2253 internal_rela_relocs)))
2256 /* Cache the results for next time, if we can. */
2258 esdo->relocs = internal_relocs;
2263 /* Don't free alloc2, since if it was allocated we are passing it
2264 back (under the name of internal_relocs). */
2266 return internal_relocs;
2274 bfd_release (abfd, alloc2);
2281 /* Compute the size of, and allocate space for, REL_HDR which is the
2282 section header for a section containing relocations for O. */
2285 _bfd_elf_link_size_reloc_section (bfd *abfd,
2286 struct bfd_elf_section_reloc_data *reldata)
2288 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2290 /* That allows us to calculate the size of the section. */
2291 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2293 /* The contents field must last into write_object_contents, so we
2294 allocate it with bfd_alloc rather than malloc. Also since we
2295 cannot be sure that the contents will actually be filled in,
2296 we zero the allocated space. */
2297 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2298 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2301 if (reldata->hashes == NULL && reldata->count)
2303 struct elf_link_hash_entry **p;
2305 p = (struct elf_link_hash_entry **)
2306 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *));
2310 reldata->hashes = p;
2316 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2317 originated from the section given by INPUT_REL_HDR) to the
2321 _bfd_elf_link_output_relocs (bfd *output_bfd,
2322 asection *input_section,
2323 Elf_Internal_Shdr *input_rel_hdr,
2324 Elf_Internal_Rela *internal_relocs,
2325 struct elf_link_hash_entry **rel_hash
2328 Elf_Internal_Rela *irela;
2329 Elf_Internal_Rela *irelaend;
2331 struct bfd_elf_section_reloc_data *output_reldata;
2332 asection *output_section;
2333 const struct elf_backend_data *bed;
2334 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2335 struct bfd_elf_section_data *esdo;
2337 output_section = input_section->output_section;
2339 bed = get_elf_backend_data (output_bfd);
2340 esdo = elf_section_data (output_section);
2341 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2343 output_reldata = &esdo->rel;
2344 swap_out = bed->s->swap_reloc_out;
2346 else if (esdo->rela.hdr
2347 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2349 output_reldata = &esdo->rela;
2350 swap_out = bed->s->swap_reloca_out;
2354 (*_bfd_error_handler)
2355 (_("%B: relocation size mismatch in %B section %A"),
2356 output_bfd, input_section->owner, input_section);
2357 bfd_set_error (bfd_error_wrong_format);
2361 erel = output_reldata->hdr->contents;
2362 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2363 irela = internal_relocs;
2364 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2365 * bed->s->int_rels_per_ext_rel);
2366 while (irela < irelaend)
2368 (*swap_out) (output_bfd, irela, erel);
2369 irela += bed->s->int_rels_per_ext_rel;
2370 erel += input_rel_hdr->sh_entsize;
2373 /* Bump the counter, so that we know where to add the next set of
2375 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2380 /* Make weak undefined symbols in PIE dynamic. */
2383 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2384 struct elf_link_hash_entry *h)
2388 && h->root.type == bfd_link_hash_undefweak)
2389 return bfd_elf_link_record_dynamic_symbol (info, h);
2394 /* Fix up the flags for a symbol. This handles various cases which
2395 can only be fixed after all the input files are seen. This is
2396 currently called by both adjust_dynamic_symbol and
2397 assign_sym_version, which is unnecessary but perhaps more robust in
2398 the face of future changes. */
2401 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2402 struct elf_info_failed *eif)
2404 const struct elf_backend_data *bed;
2406 /* If this symbol was mentioned in a non-ELF file, try to set
2407 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2408 permit a non-ELF file to correctly refer to a symbol defined in
2409 an ELF dynamic object. */
2412 while (h->root.type == bfd_link_hash_indirect)
2413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2415 if (h->root.type != bfd_link_hash_defined
2416 && h->root.type != bfd_link_hash_defweak)
2419 h->ref_regular_nonweak = 1;
2423 if (h->root.u.def.section->owner != NULL
2424 && (bfd_get_flavour (h->root.u.def.section->owner)
2425 == bfd_target_elf_flavour))
2428 h->ref_regular_nonweak = 1;
2434 if (h->dynindx == -1
2438 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2447 /* Unfortunately, NON_ELF is only correct if the symbol
2448 was first seen in a non-ELF file. Fortunately, if the symbol
2449 was first seen in an ELF file, we're probably OK unless the
2450 symbol was defined in a non-ELF file. Catch that case here.
2451 FIXME: We're still in trouble if the symbol was first seen in
2452 a dynamic object, and then later in a non-ELF regular object. */
2453 if ((h->root.type == bfd_link_hash_defined
2454 || h->root.type == bfd_link_hash_defweak)
2456 && (h->root.u.def.section->owner != NULL
2457 ? (bfd_get_flavour (h->root.u.def.section->owner)
2458 != bfd_target_elf_flavour)
2459 : (bfd_is_abs_section (h->root.u.def.section)
2460 && !h->def_dynamic)))
2464 /* Backend specific symbol fixup. */
2465 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2466 if (bed->elf_backend_fixup_symbol
2467 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2470 /* If this is a final link, and the symbol was defined as a common
2471 symbol in a regular object file, and there was no definition in
2472 any dynamic object, then the linker will have allocated space for
2473 the symbol in a common section but the DEF_REGULAR
2474 flag will not have been set. */
2475 if (h->root.type == bfd_link_hash_defined
2479 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
2482 /* If -Bsymbolic was used (which means to bind references to global
2483 symbols to the definition within the shared object), and this
2484 symbol was defined in a regular object, then it actually doesn't
2485 need a PLT entry. Likewise, if the symbol has non-default
2486 visibility. If the symbol has hidden or internal visibility, we
2487 will force it local. */
2489 && eif->info->shared
2490 && is_elf_hash_table (eif->info->hash)
2491 && (SYMBOLIC_BIND (eif->info, h)
2492 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2495 bfd_boolean force_local;
2497 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2498 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2499 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2502 /* If a weak undefined symbol has non-default visibility, we also
2503 hide it from the dynamic linker. */
2504 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2505 && h->root.type == bfd_link_hash_undefweak)
2506 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2508 /* If this is a weak defined symbol in a dynamic object, and we know
2509 the real definition in the dynamic object, copy interesting flags
2510 over to the real definition. */
2511 if (h->u.weakdef != NULL)
2513 struct elf_link_hash_entry *weakdef;
2515 weakdef = h->u.weakdef;
2516 while (h->root.type == bfd_link_hash_indirect)
2517 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2519 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2520 || h->root.type == bfd_link_hash_defweak);
2521 BFD_ASSERT (weakdef->def_dynamic);
2523 /* If the real definition is defined by a regular object file,
2524 don't do anything special. See the longer description in
2525 _bfd_elf_adjust_dynamic_symbol, below. */
2526 if (weakdef->def_regular)
2527 h->u.weakdef = NULL;
2530 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2531 || weakdef->root.type == bfd_link_hash_defweak);
2532 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2539 /* Make the backend pick a good value for a dynamic symbol. This is
2540 called via elf_link_hash_traverse, and also calls itself
2544 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2546 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2548 const struct elf_backend_data *bed;
2550 if (! is_elf_hash_table (eif->info->hash))
2553 /* Ignore indirect symbols. These are added by the versioning code. */
2554 if (h->root.type == bfd_link_hash_indirect)
2557 /* Fix the symbol flags. */
2558 if (! _bfd_elf_fix_symbol_flags (h, eif))
2561 /* If this symbol does not require a PLT entry, and it is not
2562 defined by a dynamic object, or is not referenced by a regular
2563 object, ignore it. We do have to handle a weak defined symbol,
2564 even if no regular object refers to it, if we decided to add it
2565 to the dynamic symbol table. FIXME: Do we normally need to worry
2566 about symbols which are defined by one dynamic object and
2567 referenced by another one? */
2569 && h->type != STT_GNU_IFUNC
2573 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2575 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2579 /* If we've already adjusted this symbol, don't do it again. This
2580 can happen via a recursive call. */
2581 if (h->dynamic_adjusted)
2584 /* Don't look at this symbol again. Note that we must set this
2585 after checking the above conditions, because we may look at a
2586 symbol once, decide not to do anything, and then get called
2587 recursively later after REF_REGULAR is set below. */
2588 h->dynamic_adjusted = 1;
2590 /* If this is a weak definition, and we know a real definition, and
2591 the real symbol is not itself defined by a regular object file,
2592 then get a good value for the real definition. We handle the
2593 real symbol first, for the convenience of the backend routine.
2595 Note that there is a confusing case here. If the real definition
2596 is defined by a regular object file, we don't get the real symbol
2597 from the dynamic object, but we do get the weak symbol. If the
2598 processor backend uses a COPY reloc, then if some routine in the
2599 dynamic object changes the real symbol, we will not see that
2600 change in the corresponding weak symbol. This is the way other
2601 ELF linkers work as well, and seems to be a result of the shared
2604 I will clarify this issue. Most SVR4 shared libraries define the
2605 variable _timezone and define timezone as a weak synonym. The
2606 tzset call changes _timezone. If you write
2607 extern int timezone;
2609 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2610 you might expect that, since timezone is a synonym for _timezone,
2611 the same number will print both times. However, if the processor
2612 backend uses a COPY reloc, then actually timezone will be copied
2613 into your process image, and, since you define _timezone
2614 yourself, _timezone will not. Thus timezone and _timezone will
2615 wind up at different memory locations. The tzset call will set
2616 _timezone, leaving timezone unchanged. */
2618 if (h->u.weakdef != NULL)
2620 /* If we get to this point, there is an implicit reference to
2621 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2622 h->u.weakdef->ref_regular = 1;
2624 /* Ensure that the backend adjust_dynamic_symbol function sees
2625 H->U.WEAKDEF before H by recursively calling ourselves. */
2626 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2630 /* If a symbol has no type and no size and does not require a PLT
2631 entry, then we are probably about to do the wrong thing here: we
2632 are probably going to create a COPY reloc for an empty object.
2633 This case can arise when a shared object is built with assembly
2634 code, and the assembly code fails to set the symbol type. */
2636 && h->type == STT_NOTYPE
2638 (*_bfd_error_handler)
2639 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2640 h->root.root.string);
2642 dynobj = elf_hash_table (eif->info)->dynobj;
2643 bed = get_elf_backend_data (dynobj);
2645 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2654 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2658 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h,
2661 unsigned int power_of_two;
2663 asection *sec = h->root.u.def.section;
2665 /* The section aligment of definition is the maximum alignment
2666 requirement of symbols defined in the section. Since we don't
2667 know the symbol alignment requirement, we start with the
2668 maximum alignment and check low bits of the symbol address
2669 for the minimum alignment. */
2670 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2671 mask = ((bfd_vma) 1 << power_of_two) - 1;
2672 while ((h->root.u.def.value & mask) != 0)
2678 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2681 /* Adjust the section alignment if needed. */
2682 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2687 /* We make sure that the symbol will be aligned properly. */
2688 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2690 /* Define the symbol as being at this point in DYNBSS. */
2691 h->root.u.def.section = dynbss;
2692 h->root.u.def.value = dynbss->size;
2694 /* Increment the size of DYNBSS to make room for the symbol. */
2695 dynbss->size += h->size;
2700 /* Adjust all external symbols pointing into SEC_MERGE sections
2701 to reflect the object merging within the sections. */
2704 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2708 if ((h->root.type == bfd_link_hash_defined
2709 || h->root.type == bfd_link_hash_defweak)
2710 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2711 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2713 bfd *output_bfd = (bfd *) data;
2715 h->root.u.def.value =
2716 _bfd_merged_section_offset (output_bfd,
2717 &h->root.u.def.section,
2718 elf_section_data (sec)->sec_info,
2719 h->root.u.def.value);
2725 /* Returns false if the symbol referred to by H should be considered
2726 to resolve local to the current module, and true if it should be
2727 considered to bind dynamically. */
2730 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2731 struct bfd_link_info *info,
2732 bfd_boolean not_local_protected)
2734 bfd_boolean binding_stays_local_p;
2735 const struct elf_backend_data *bed;
2736 struct elf_link_hash_table *hash_table;
2741 while (h->root.type == bfd_link_hash_indirect
2742 || h->root.type == bfd_link_hash_warning)
2743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2745 /* If it was forced local, then clearly it's not dynamic. */
2746 if (h->dynindx == -1)
2748 if (h->forced_local)
2751 /* Identify the cases where name binding rules say that a
2752 visible symbol resolves locally. */
2753 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2755 switch (ELF_ST_VISIBILITY (h->other))
2762 hash_table = elf_hash_table (info);
2763 if (!is_elf_hash_table (hash_table))
2766 bed = get_elf_backend_data (hash_table->dynobj);
2768 /* Proper resolution for function pointer equality may require
2769 that these symbols perhaps be resolved dynamically, even though
2770 we should be resolving them to the current module. */
2771 if (!not_local_protected || !bed->is_function_type (h->type))
2772 binding_stays_local_p = TRUE;
2779 /* If it isn't defined locally, then clearly it's dynamic. */
2780 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2783 /* Otherwise, the symbol is dynamic if binding rules don't tell
2784 us that it remains local. */
2785 return !binding_stays_local_p;
2788 /* Return true if the symbol referred to by H should be considered
2789 to resolve local to the current module, and false otherwise. Differs
2790 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2791 undefined symbols. The two functions are virtually identical except
2792 for the place where forced_local and dynindx == -1 are tested. If
2793 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2794 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2795 the symbol is local only for defined symbols.
2796 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2797 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2798 treatment of undefined weak symbols. For those that do not make
2799 undefined weak symbols dynamic, both functions may return false. */
2802 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2803 struct bfd_link_info *info,
2804 bfd_boolean local_protected)
2806 const struct elf_backend_data *bed;
2807 struct elf_link_hash_table *hash_table;
2809 /* If it's a local sym, of course we resolve locally. */
2813 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2814 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2815 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2818 /* Common symbols that become definitions don't get the DEF_REGULAR
2819 flag set, so test it first, and don't bail out. */
2820 if (ELF_COMMON_DEF_P (h))
2822 /* If we don't have a definition in a regular file, then we can't
2823 resolve locally. The sym is either undefined or dynamic. */
2824 else if (!h->def_regular)
2827 /* Forced local symbols resolve locally. */
2828 if (h->forced_local)
2831 /* As do non-dynamic symbols. */
2832 if (h->dynindx == -1)
2835 /* At this point, we know the symbol is defined and dynamic. In an
2836 executable it must resolve locally, likewise when building symbolic
2837 shared libraries. */
2838 if (info->executable || SYMBOLIC_BIND (info, h))
2841 /* Now deal with defined dynamic symbols in shared libraries. Ones
2842 with default visibility might not resolve locally. */
2843 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2846 hash_table = elf_hash_table (info);
2847 if (!is_elf_hash_table (hash_table))
2850 bed = get_elf_backend_data (hash_table->dynobj);
2852 /* STV_PROTECTED non-function symbols are local. */
2853 if (!bed->is_function_type (h->type))
2856 /* Function pointer equality tests may require that STV_PROTECTED
2857 symbols be treated as dynamic symbols. If the address of a
2858 function not defined in an executable is set to that function's
2859 plt entry in the executable, then the address of the function in
2860 a shared library must also be the plt entry in the executable. */
2861 return local_protected;
2864 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2865 aligned. Returns the first TLS output section. */
2867 struct bfd_section *
2868 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2870 struct bfd_section *sec, *tls;
2871 unsigned int align = 0;
2873 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2874 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2878 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2879 if (sec->alignment_power > align)
2880 align = sec->alignment_power;
2882 elf_hash_table (info)->tls_sec = tls;
2884 /* Ensure the alignment of the first section is the largest alignment,
2885 so that the tls segment starts aligned. */
2887 tls->alignment_power = align;
2892 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2894 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2895 Elf_Internal_Sym *sym)
2897 const struct elf_backend_data *bed;
2899 /* Local symbols do not count, but target specific ones might. */
2900 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2901 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2904 bed = get_elf_backend_data (abfd);
2905 /* Function symbols do not count. */
2906 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2909 /* If the section is undefined, then so is the symbol. */
2910 if (sym->st_shndx == SHN_UNDEF)
2913 /* If the symbol is defined in the common section, then
2914 it is a common definition and so does not count. */
2915 if (bed->common_definition (sym))
2918 /* If the symbol is in a target specific section then we
2919 must rely upon the backend to tell us what it is. */
2920 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2921 /* FIXME - this function is not coded yet:
2923 return _bfd_is_global_symbol_definition (abfd, sym);
2925 Instead for now assume that the definition is not global,
2926 Even if this is wrong, at least the linker will behave
2927 in the same way that it used to do. */
2933 /* Search the symbol table of the archive element of the archive ABFD
2934 whose archive map contains a mention of SYMDEF, and determine if
2935 the symbol is defined in this element. */
2937 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2939 Elf_Internal_Shdr * hdr;
2940 bfd_size_type symcount;
2941 bfd_size_type extsymcount;
2942 bfd_size_type extsymoff;
2943 Elf_Internal_Sym *isymbuf;
2944 Elf_Internal_Sym *isym;
2945 Elf_Internal_Sym *isymend;
2948 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2952 if (! bfd_check_format (abfd, bfd_object))
2955 /* If we have already included the element containing this symbol in the
2956 link then we do not need to include it again. Just claim that any symbol
2957 it contains is not a definition, so that our caller will not decide to
2958 (re)include this element. */
2959 if (abfd->archive_pass)
2962 /* Select the appropriate symbol table. */
2963 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2964 hdr = &elf_tdata (abfd)->symtab_hdr;
2966 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2968 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2970 /* The sh_info field of the symtab header tells us where the
2971 external symbols start. We don't care about the local symbols. */
2972 if (elf_bad_symtab (abfd))
2974 extsymcount = symcount;
2979 extsymcount = symcount - hdr->sh_info;
2980 extsymoff = hdr->sh_info;
2983 if (extsymcount == 0)
2986 /* Read in the symbol table. */
2987 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
2989 if (isymbuf == NULL)
2992 /* Scan the symbol table looking for SYMDEF. */
2994 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
2998 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3003 if (strcmp (name, symdef->name) == 0)
3005 result = is_global_data_symbol_definition (abfd, isym);
3015 /* Add an entry to the .dynamic table. */
3018 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3022 struct elf_link_hash_table *hash_table;
3023 const struct elf_backend_data *bed;
3025 bfd_size_type newsize;
3026 bfd_byte *newcontents;
3027 Elf_Internal_Dyn dyn;
3029 hash_table = elf_hash_table (info);
3030 if (! is_elf_hash_table (hash_table))
3033 bed = get_elf_backend_data (hash_table->dynobj);
3034 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3035 BFD_ASSERT (s != NULL);
3037 newsize = s->size + bed->s->sizeof_dyn;
3038 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3039 if (newcontents == NULL)
3043 dyn.d_un.d_val = val;
3044 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3047 s->contents = newcontents;
3052 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3053 otherwise just check whether one already exists. Returns -1 on error,
3054 1 if a DT_NEEDED tag already exists, and 0 on success. */
3057 elf_add_dt_needed_tag (bfd *abfd,
3058 struct bfd_link_info *info,
3062 struct elf_link_hash_table *hash_table;
3063 bfd_size_type oldsize;
3064 bfd_size_type strindex;
3066 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3069 hash_table = elf_hash_table (info);
3070 oldsize = _bfd_elf_strtab_size (hash_table->dynstr);
3071 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3072 if (strindex == (bfd_size_type) -1)
3075 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr))
3078 const struct elf_backend_data *bed;
3081 bed = get_elf_backend_data (hash_table->dynobj);
3082 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic");
3084 for (extdyn = sdyn->contents;
3085 extdyn < sdyn->contents + sdyn->size;
3086 extdyn += bed->s->sizeof_dyn)
3088 Elf_Internal_Dyn dyn;
3090 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3091 if (dyn.d_tag == DT_NEEDED
3092 && dyn.d_un.d_val == strindex)
3094 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3102 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3105 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3109 /* We were just checking for existence of the tag. */
3110 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3116 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3118 for (; needed != NULL; needed = needed->next)
3119 if (strcmp (soname, needed->name) == 0)
3125 /* Sort symbol by value and section. */
3127 elf_sort_symbol (const void *arg1, const void *arg2)
3129 const struct elf_link_hash_entry *h1;
3130 const struct elf_link_hash_entry *h2;
3131 bfd_signed_vma vdiff;
3133 h1 = *(const struct elf_link_hash_entry **) arg1;
3134 h2 = *(const struct elf_link_hash_entry **) arg2;
3135 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3137 return vdiff > 0 ? 1 : -1;
3140 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3142 return sdiff > 0 ? 1 : -1;
3147 /* This function is used to adjust offsets into .dynstr for
3148 dynamic symbols. This is called via elf_link_hash_traverse. */
3151 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3153 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3155 if (h->dynindx != -1)
3156 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3160 /* Assign string offsets in .dynstr, update all structures referencing
3164 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3166 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3167 struct elf_link_local_dynamic_entry *entry;
3168 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3169 bfd *dynobj = hash_table->dynobj;
3172 const struct elf_backend_data *bed;
3175 _bfd_elf_strtab_finalize (dynstr);
3176 size = _bfd_elf_strtab_size (dynstr);
3178 bed = get_elf_backend_data (dynobj);
3179 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3180 BFD_ASSERT (sdyn != NULL);
3182 /* Update all .dynamic entries referencing .dynstr strings. */
3183 for (extdyn = sdyn->contents;
3184 extdyn < sdyn->contents + sdyn->size;
3185 extdyn += bed->s->sizeof_dyn)
3187 Elf_Internal_Dyn dyn;
3189 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3193 dyn.d_un.d_val = size;
3203 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3208 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3211 /* Now update local dynamic symbols. */
3212 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3213 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3214 entry->isym.st_name);
3216 /* And the rest of dynamic symbols. */
3217 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3219 /* Adjust version definitions. */
3220 if (elf_tdata (output_bfd)->cverdefs)
3225 Elf_Internal_Verdef def;
3226 Elf_Internal_Verdaux defaux;
3228 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
3232 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3234 p += sizeof (Elf_External_Verdef);
3235 if (def.vd_aux != sizeof (Elf_External_Verdef))
3237 for (i = 0; i < def.vd_cnt; ++i)
3239 _bfd_elf_swap_verdaux_in (output_bfd,
3240 (Elf_External_Verdaux *) p, &defaux);
3241 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3243 _bfd_elf_swap_verdaux_out (output_bfd,
3244 &defaux, (Elf_External_Verdaux *) p);
3245 p += sizeof (Elf_External_Verdaux);
3248 while (def.vd_next);
3251 /* Adjust version references. */
3252 if (elf_tdata (output_bfd)->verref)
3257 Elf_Internal_Verneed need;
3258 Elf_Internal_Vernaux needaux;
3260 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
3264 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3266 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3267 _bfd_elf_swap_verneed_out (output_bfd, &need,
3268 (Elf_External_Verneed *) p);
3269 p += sizeof (Elf_External_Verneed);
3270 for (i = 0; i < need.vn_cnt; ++i)
3272 _bfd_elf_swap_vernaux_in (output_bfd,
3273 (Elf_External_Vernaux *) p, &needaux);
3274 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3276 _bfd_elf_swap_vernaux_out (output_bfd,
3278 (Elf_External_Vernaux *) p);
3279 p += sizeof (Elf_External_Vernaux);
3282 while (need.vn_next);
3288 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3289 The default is to only match when the INPUT and OUTPUT are exactly
3293 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3294 const bfd_target *output)
3296 return input == output;
3299 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3300 This version is used when different targets for the same architecture
3301 are virtually identical. */
3304 _bfd_elf_relocs_compatible (const bfd_target *input,
3305 const bfd_target *output)
3307 const struct elf_backend_data *obed, *ibed;
3309 if (input == output)
3312 ibed = xvec_get_elf_backend_data (input);
3313 obed = xvec_get_elf_backend_data (output);
3315 if (ibed->arch != obed->arch)
3318 /* If both backends are using this function, deem them compatible. */
3319 return ibed->relocs_compatible == obed->relocs_compatible;
3322 /* Add symbols from an ELF object file to the linker hash table. */
3325 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3327 Elf_Internal_Ehdr *ehdr;
3328 Elf_Internal_Shdr *hdr;
3329 bfd_size_type symcount;
3330 bfd_size_type extsymcount;
3331 bfd_size_type extsymoff;
3332 struct elf_link_hash_entry **sym_hash;
3333 bfd_boolean dynamic;
3334 Elf_External_Versym *extversym = NULL;
3335 Elf_External_Versym *ever;
3336 struct elf_link_hash_entry *weaks;
3337 struct elf_link_hash_entry **nondeflt_vers = NULL;
3338 bfd_size_type nondeflt_vers_cnt = 0;
3339 Elf_Internal_Sym *isymbuf = NULL;
3340 Elf_Internal_Sym *isym;
3341 Elf_Internal_Sym *isymend;
3342 const struct elf_backend_data *bed;
3343 bfd_boolean add_needed;
3344 struct elf_link_hash_table *htab;
3346 void *alloc_mark = NULL;
3347 struct bfd_hash_entry **old_table = NULL;
3348 unsigned int old_size = 0;
3349 unsigned int old_count = 0;
3350 void *old_tab = NULL;
3353 struct bfd_link_hash_entry *old_undefs = NULL;
3354 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3355 long old_dynsymcount = 0;
3357 size_t hashsize = 0;
3359 htab = elf_hash_table (info);
3360 bed = get_elf_backend_data (abfd);
3362 if ((abfd->flags & DYNAMIC) == 0)
3368 /* You can't use -r against a dynamic object. Also, there's no
3369 hope of using a dynamic object which does not exactly match
3370 the format of the output file. */
3371 if (info->relocatable
3372 || !is_elf_hash_table (htab)
3373 || info->output_bfd->xvec != abfd->xvec)
3375 if (info->relocatable)
3376 bfd_set_error (bfd_error_invalid_operation);
3378 bfd_set_error (bfd_error_wrong_format);
3383 ehdr = elf_elfheader (abfd);
3384 if (info->warn_alternate_em
3385 && bed->elf_machine_code != ehdr->e_machine
3386 && ((bed->elf_machine_alt1 != 0
3387 && ehdr->e_machine == bed->elf_machine_alt1)
3388 || (bed->elf_machine_alt2 != 0
3389 && ehdr->e_machine == bed->elf_machine_alt2)))
3390 info->callbacks->einfo
3391 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3392 ehdr->e_machine, abfd, bed->elf_machine_code);
3394 /* As a GNU extension, any input sections which are named
3395 .gnu.warning.SYMBOL are treated as warning symbols for the given
3396 symbol. This differs from .gnu.warning sections, which generate
3397 warnings when they are included in an output file. */
3398 /* PR 12761: Also generate this warning when building shared libraries. */
3399 if (info->executable || info->shared)
3403 for (s = abfd->sections; s != NULL; s = s->next)
3407 name = bfd_get_section_name (abfd, s);
3408 if (CONST_STRNEQ (name, ".gnu.warning."))
3413 name += sizeof ".gnu.warning." - 1;
3415 /* If this is a shared object, then look up the symbol
3416 in the hash table. If it is there, and it is already
3417 been defined, then we will not be using the entry
3418 from this shared object, so we don't need to warn.
3419 FIXME: If we see the definition in a regular object
3420 later on, we will warn, but we shouldn't. The only
3421 fix is to keep track of what warnings we are supposed
3422 to emit, and then handle them all at the end of the
3426 struct elf_link_hash_entry *h;
3428 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3430 /* FIXME: What about bfd_link_hash_common? */
3432 && (h->root.type == bfd_link_hash_defined
3433 || h->root.type == bfd_link_hash_defweak))
3435 /* We don't want to issue this warning. Clobber
3436 the section size so that the warning does not
3437 get copied into the output file. */
3444 msg = (char *) bfd_alloc (abfd, sz + 1);
3448 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3453 if (! (_bfd_generic_link_add_one_symbol
3454 (info, abfd, name, BSF_WARNING, s, 0, msg,
3455 FALSE, bed->collect, NULL)))
3458 if (! info->relocatable)
3460 /* Clobber the section size so that the warning does
3461 not get copied into the output file. */
3464 /* Also set SEC_EXCLUDE, so that symbols defined in
3465 the warning section don't get copied to the output. */
3466 s->flags |= SEC_EXCLUDE;
3475 /* If we are creating a shared library, create all the dynamic
3476 sections immediately. We need to attach them to something,
3477 so we attach them to this BFD, provided it is the right
3478 format. FIXME: If there are no input BFD's of the same
3479 format as the output, we can't make a shared library. */
3481 && is_elf_hash_table (htab)
3482 && info->output_bfd->xvec == abfd->xvec
3483 && !htab->dynamic_sections_created)
3485 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3489 else if (!is_elf_hash_table (htab))
3494 const char *soname = NULL;
3496 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3499 /* ld --just-symbols and dynamic objects don't mix very well.
3500 ld shouldn't allow it. */
3501 if ((s = abfd->sections) != NULL
3502 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
3505 /* If this dynamic lib was specified on the command line with
3506 --as-needed in effect, then we don't want to add a DT_NEEDED
3507 tag unless the lib is actually used. Similary for libs brought
3508 in by another lib's DT_NEEDED. When --no-add-needed is used
3509 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3510 any dynamic library in DT_NEEDED tags in the dynamic lib at
3512 add_needed = (elf_dyn_lib_class (abfd)
3513 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3514 | DYN_NO_NEEDED)) == 0;
3516 s = bfd_get_section_by_name (abfd, ".dynamic");
3521 unsigned int elfsec;
3522 unsigned long shlink;
3524 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3531 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3532 if (elfsec == SHN_BAD)
3533 goto error_free_dyn;
3534 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3536 for (extdyn = dynbuf;
3537 extdyn < dynbuf + s->size;
3538 extdyn += bed->s->sizeof_dyn)
3540 Elf_Internal_Dyn dyn;
3542 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3543 if (dyn.d_tag == DT_SONAME)
3545 unsigned int tagv = dyn.d_un.d_val;
3546 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3548 goto error_free_dyn;
3550 if (dyn.d_tag == DT_NEEDED)
3552 struct bfd_link_needed_list *n, **pn;
3554 unsigned int tagv = dyn.d_un.d_val;
3556 amt = sizeof (struct bfd_link_needed_list);
3557 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3558 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3559 if (n == NULL || fnm == NULL)
3560 goto error_free_dyn;
3561 amt = strlen (fnm) + 1;
3562 anm = (char *) bfd_alloc (abfd, amt);
3564 goto error_free_dyn;
3565 memcpy (anm, fnm, amt);
3569 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3573 if (dyn.d_tag == DT_RUNPATH)
3575 struct bfd_link_needed_list *n, **pn;
3577 unsigned int tagv = dyn.d_un.d_val;
3579 amt = sizeof (struct bfd_link_needed_list);
3580 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3581 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3582 if (n == NULL || fnm == NULL)
3583 goto error_free_dyn;
3584 amt = strlen (fnm) + 1;
3585 anm = (char *) bfd_alloc (abfd, amt);
3587 goto error_free_dyn;
3588 memcpy (anm, fnm, amt);
3592 for (pn = & runpath;
3598 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3599 if (!runpath && dyn.d_tag == DT_RPATH)
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);
3624 if (dyn.d_tag == DT_AUDIT)
3626 unsigned int tagv = dyn.d_un.d_val;
3627 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3634 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3635 frees all more recently bfd_alloc'd blocks as well. */
3641 struct bfd_link_needed_list **pn;
3642 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3647 /* We do not want to include any of the sections in a dynamic
3648 object in the output file. We hack by simply clobbering the
3649 list of sections in the BFD. This could be handled more
3650 cleanly by, say, a new section flag; the existing
3651 SEC_NEVER_LOAD flag is not the one we want, because that one
3652 still implies that the section takes up space in the output
3654 bfd_section_list_clear (abfd);
3656 /* Find the name to use in a DT_NEEDED entry that refers to this
3657 object. If the object has a DT_SONAME entry, we use it.
3658 Otherwise, if the generic linker stuck something in
3659 elf_dt_name, we use that. Otherwise, we just use the file
3661 if (soname == NULL || *soname == '\0')
3663 soname = elf_dt_name (abfd);
3664 if (soname == NULL || *soname == '\0')
3665 soname = bfd_get_filename (abfd);
3668 /* Save the SONAME because sometimes the linker emulation code
3669 will need to know it. */
3670 elf_dt_name (abfd) = soname;
3672 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3676 /* If we have already included this dynamic object in the
3677 link, just ignore it. There is no reason to include a
3678 particular dynamic object more than once. */
3682 /* Save the DT_AUDIT entry for the linker emulation code. */
3683 elf_dt_audit (abfd) = audit;
3686 /* If this is a dynamic object, we always link against the .dynsym
3687 symbol table, not the .symtab symbol table. The dynamic linker
3688 will only see the .dynsym symbol table, so there is no reason to
3689 look at .symtab for a dynamic object. */
3691 if (! dynamic || elf_dynsymtab (abfd) == 0)
3692 hdr = &elf_tdata (abfd)->symtab_hdr;
3694 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3696 symcount = hdr->sh_size / bed->s->sizeof_sym;
3698 /* The sh_info field of the symtab header tells us where the
3699 external symbols start. We don't care about the local symbols at
3701 if (elf_bad_symtab (abfd))
3703 extsymcount = symcount;
3708 extsymcount = symcount - hdr->sh_info;
3709 extsymoff = hdr->sh_info;
3713 if (extsymcount != 0)
3715 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3717 if (isymbuf == NULL)
3720 /* We store a pointer to the hash table entry for each external
3722 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3723 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt);
3724 if (sym_hash == NULL)
3725 goto error_free_sym;
3726 elf_sym_hashes (abfd) = sym_hash;
3731 /* Read in any version definitions. */
3732 if (!_bfd_elf_slurp_version_tables (abfd,
3733 info->default_imported_symver))
3734 goto error_free_sym;
3736 /* Read in the symbol versions, but don't bother to convert them
3737 to internal format. */
3738 if (elf_dynversym (abfd) != 0)
3740 Elf_Internal_Shdr *versymhdr;
3742 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3743 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3744 if (extversym == NULL)
3745 goto error_free_sym;
3746 amt = versymhdr->sh_size;
3747 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3748 || bfd_bread (extversym, amt, abfd) != amt)
3749 goto error_free_vers;
3753 /* If we are loading an as-needed shared lib, save the symbol table
3754 state before we start adding symbols. If the lib turns out
3755 to be unneeded, restore the state. */
3756 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3761 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3763 struct bfd_hash_entry *p;
3764 struct elf_link_hash_entry *h;
3766 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3768 h = (struct elf_link_hash_entry *) p;
3769 entsize += htab->root.table.entsize;
3770 if (h->root.type == bfd_link_hash_warning)
3771 entsize += htab->root.table.entsize;
3775 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3776 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *);
3777 old_tab = bfd_malloc (tabsize + entsize + hashsize);
3778 if (old_tab == NULL)
3779 goto error_free_vers;
3781 /* Remember the current objalloc pointer, so that all mem for
3782 symbols added can later be reclaimed. */
3783 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3784 if (alloc_mark == NULL)
3785 goto error_free_vers;
3787 /* Make a special call to the linker "notice" function to
3788 tell it that we are about to handle an as-needed lib. */
3789 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
3790 notice_as_needed, 0, NULL))
3791 goto error_free_vers;
3793 /* Clone the symbol table and sym hashes. Remember some
3794 pointers into the symbol table, and dynamic symbol count. */
3795 old_hash = (char *) old_tab + tabsize;
3796 old_ent = (char *) old_hash + hashsize;
3797 memcpy (old_tab, htab->root.table.table, tabsize);
3798 memcpy (old_hash, sym_hash, hashsize);
3799 old_undefs = htab->root.undefs;
3800 old_undefs_tail = htab->root.undefs_tail;
3801 old_table = htab->root.table.table;
3802 old_size = htab->root.table.size;
3803 old_count = htab->root.table.count;
3804 old_dynsymcount = htab->dynsymcount;
3806 for (i = 0; i < htab->root.table.size; i++)
3808 struct bfd_hash_entry *p;
3809 struct elf_link_hash_entry *h;
3811 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3813 memcpy (old_ent, p, htab->root.table.entsize);
3814 old_ent = (char *) old_ent + htab->root.table.entsize;
3815 h = (struct elf_link_hash_entry *) p;
3816 if (h->root.type == bfd_link_hash_warning)
3818 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3819 old_ent = (char *) old_ent + htab->root.table.entsize;
3826 ever = extversym != NULL ? extversym + extsymoff : NULL;
3827 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3829 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3833 asection *sec, *new_sec;
3836 struct elf_link_hash_entry *h;
3837 bfd_boolean definition;
3838 bfd_boolean size_change_ok;
3839 bfd_boolean type_change_ok;
3840 bfd_boolean new_weakdef;
3841 bfd_boolean override;
3843 unsigned int old_alignment;
3845 bfd * undef_bfd = NULL;
3849 flags = BSF_NO_FLAGS;
3851 value = isym->st_value;
3853 common = bed->common_definition (isym);
3855 bind = ELF_ST_BIND (isym->st_info);
3859 /* This should be impossible, since ELF requires that all
3860 global symbols follow all local symbols, and that sh_info
3861 point to the first global symbol. Unfortunately, Irix 5
3866 if (isym->st_shndx != SHN_UNDEF && !common)
3874 case STB_GNU_UNIQUE:
3875 flags = BSF_GNU_UNIQUE;
3879 /* Leave it up to the processor backend. */
3883 if (isym->st_shndx == SHN_UNDEF)
3884 sec = bfd_und_section_ptr;
3885 else if (isym->st_shndx == SHN_ABS)
3886 sec = bfd_abs_section_ptr;
3887 else if (isym->st_shndx == SHN_COMMON)
3889 sec = bfd_com_section_ptr;
3890 /* What ELF calls the size we call the value. What ELF
3891 calls the value we call the alignment. */
3892 value = isym->st_size;
3896 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3898 sec = bfd_abs_section_ptr;
3899 else if (elf_discarded_section (sec))
3901 /* Symbols from discarded section are undefined. We keep
3903 sec = bfd_und_section_ptr;
3904 isym->st_shndx = SHN_UNDEF;
3906 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3910 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3913 goto error_free_vers;
3915 if (isym->st_shndx == SHN_COMMON
3916 && (abfd->flags & BFD_PLUGIN) != 0)
3918 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3922 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3924 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3926 goto error_free_vers;
3930 else if (isym->st_shndx == SHN_COMMON
3931 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3932 && !info->relocatable)
3934 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3938 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3939 | SEC_LINKER_CREATED);
3940 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3942 goto error_free_vers;
3946 else if (bed->elf_add_symbol_hook)
3948 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3950 goto error_free_vers;
3952 /* The hook function sets the name to NULL if this symbol
3953 should be skipped for some reason. */
3958 /* Sanity check that all possibilities were handled. */
3961 bfd_set_error (bfd_error_bad_value);
3962 goto error_free_vers;
3965 if (bfd_is_und_section (sec)
3966 || bfd_is_com_section (sec))
3971 size_change_ok = FALSE;
3972 type_change_ok = bed->type_change_ok;
3977 if (is_elf_hash_table (htab))
3979 Elf_Internal_Versym iver;
3980 unsigned int vernum = 0;
3983 /* If this is a definition of a symbol which was previously
3984 referenced in a non-weak manner then make a note of the bfd
3985 that contained the reference. This is used if we need to
3986 refer to the source of the reference later on. */
3987 if (! bfd_is_und_section (sec))
3989 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
3992 && h->root.type == bfd_link_hash_undefined
3993 && h->root.u.undef.abfd)
3994 undef_bfd = h->root.u.undef.abfd;
3999 if (info->default_imported_symver)
4000 /* Use the default symbol version created earlier. */
4001 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4006 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4008 vernum = iver.vs_vers & VERSYM_VERSION;
4010 /* If this is a hidden symbol, or if it is not version
4011 1, we append the version name to the symbol name.
4012 However, we do not modify a non-hidden absolute symbol
4013 if it is not a function, because it might be the version
4014 symbol itself. FIXME: What if it isn't? */
4015 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4017 && (!bfd_is_abs_section (sec)
4018 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4021 size_t namelen, verlen, newlen;
4024 if (isym->st_shndx != SHN_UNDEF)
4026 if (vernum > elf_tdata (abfd)->cverdefs)
4028 else if (vernum > 1)
4030 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4036 (*_bfd_error_handler)
4037 (_("%B: %s: invalid version %u (max %d)"),
4039 elf_tdata (abfd)->cverdefs);
4040 bfd_set_error (bfd_error_bad_value);
4041 goto error_free_vers;
4046 /* We cannot simply test for the number of
4047 entries in the VERNEED section since the
4048 numbers for the needed versions do not start
4050 Elf_Internal_Verneed *t;
4053 for (t = elf_tdata (abfd)->verref;
4057 Elf_Internal_Vernaux *a;
4059 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4061 if (a->vna_other == vernum)
4063 verstr = a->vna_nodename;
4072 (*_bfd_error_handler)
4073 (_("%B: %s: invalid needed version %d"),
4074 abfd, name, vernum);
4075 bfd_set_error (bfd_error_bad_value);
4076 goto error_free_vers;
4080 namelen = strlen (name);
4081 verlen = strlen (verstr);
4082 newlen = namelen + verlen + 2;
4083 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4084 && isym->st_shndx != SHN_UNDEF)
4087 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4088 if (newname == NULL)
4089 goto error_free_vers;
4090 memcpy (newname, name, namelen);
4091 p = newname + namelen;
4093 /* If this is a defined non-hidden version symbol,
4094 we add another @ to the name. This indicates the
4095 default version of the symbol. */
4096 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4097 && isym->st_shndx != SHN_UNDEF)
4099 memcpy (p, verstr, verlen + 1);
4104 /* If necessary, make a second attempt to locate the bfd
4105 containing an unresolved, non-weak reference to the
4107 if (! bfd_is_und_section (sec) && undef_bfd == NULL)
4109 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
4112 && h->root.type == bfd_link_hash_undefined
4113 && h->root.u.undef.abfd)
4114 undef_bfd = h->root.u.undef.abfd;
4117 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec,
4118 &value, &old_alignment,
4119 sym_hash, &skip, &override,
4120 &type_change_ok, &size_change_ok))
4121 goto error_free_vers;
4130 while (h->root.type == bfd_link_hash_indirect
4131 || h->root.type == bfd_link_hash_warning)
4132 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4134 /* Remember the old alignment if this is a common symbol, so
4135 that we don't reduce the alignment later on. We can't
4136 check later, because _bfd_generic_link_add_one_symbol
4137 will set a default for the alignment which we want to
4138 override. We also remember the old bfd where the existing
4139 definition comes from. */
4140 switch (h->root.type)
4145 case bfd_link_hash_defined:
4146 case bfd_link_hash_defweak:
4147 old_bfd = h->root.u.def.section->owner;
4150 case bfd_link_hash_common:
4151 old_bfd = h->root.u.c.p->section->owner;
4152 old_alignment = h->root.u.c.p->alignment_power;
4156 if (elf_tdata (abfd)->verdef != NULL
4160 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4163 if (! (_bfd_generic_link_add_one_symbol
4164 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4165 (struct bfd_link_hash_entry **) sym_hash)))
4166 goto error_free_vers;
4169 while (h->root.type == bfd_link_hash_indirect
4170 || h->root.type == bfd_link_hash_warning)
4171 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4174 if (is_elf_hash_table (htab))
4175 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4177 new_weakdef = FALSE;
4180 && (flags & BSF_WEAK) != 0
4181 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4182 && is_elf_hash_table (htab)
4183 && h->u.weakdef == NULL)
4185 /* Keep a list of all weak defined non function symbols from
4186 a dynamic object, using the weakdef field. Later in this
4187 function we will set the weakdef field to the correct
4188 value. We only put non-function symbols from dynamic
4189 objects on this list, because that happens to be the only
4190 time we need to know the normal symbol corresponding to a
4191 weak symbol, and the information is time consuming to
4192 figure out. If the weakdef field is not already NULL,
4193 then this symbol was already defined by some previous
4194 dynamic object, and we will be using that previous
4195 definition anyhow. */
4197 h->u.weakdef = weaks;
4202 /* Set the alignment of a common symbol. */
4203 if ((common || bfd_is_com_section (sec))
4204 && h->root.type == bfd_link_hash_common)
4209 align = bfd_log2 (isym->st_value);
4212 /* The new symbol is a common symbol in a shared object.
4213 We need to get the alignment from the section. */
4214 align = new_sec->alignment_power;
4216 if (align > old_alignment)
4217 h->root.u.c.p->alignment_power = align;
4219 h->root.u.c.p->alignment_power = old_alignment;
4222 if (is_elf_hash_table (htab))
4226 /* Check the alignment when a common symbol is involved. This
4227 can change when a common symbol is overridden by a normal
4228 definition or a common symbol is ignored due to the old
4229 normal definition. We need to make sure the maximum
4230 alignment is maintained. */
4231 if ((old_alignment || common)
4232 && h->root.type != bfd_link_hash_common)
4234 unsigned int common_align;
4235 unsigned int normal_align;
4236 unsigned int symbol_align;
4240 symbol_align = ffs (h->root.u.def.value) - 1;
4241 if (h->root.u.def.section->owner != NULL
4242 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4244 normal_align = h->root.u.def.section->alignment_power;
4245 if (normal_align > symbol_align)
4246 normal_align = symbol_align;
4249 normal_align = symbol_align;
4253 common_align = old_alignment;
4254 common_bfd = old_bfd;
4259 common_align = bfd_log2 (isym->st_value);
4261 normal_bfd = old_bfd;
4264 if (normal_align < common_align)
4266 /* PR binutils/2735 */
4267 if (normal_bfd == NULL)
4268 (*_bfd_error_handler)
4269 (_("Warning: alignment %u of common symbol `%s' in %B"
4270 " is greater than the alignment (%u) of its section %A"),
4271 common_bfd, h->root.u.def.section,
4272 1 << common_align, name, 1 << normal_align);
4274 (*_bfd_error_handler)
4275 (_("Warning: alignment %u of symbol `%s' in %B"
4276 " is smaller than %u in %B"),
4277 normal_bfd, common_bfd,
4278 1 << normal_align, name, 1 << common_align);
4282 /* Remember the symbol size if it isn't undefined. */
4283 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF)
4284 && (definition || h->size == 0))
4287 && h->size != isym->st_size
4288 && ! size_change_ok)
4289 (*_bfd_error_handler)
4290 (_("Warning: size of symbol `%s' changed"
4291 " from %lu in %B to %lu in %B"),
4293 name, (unsigned long) h->size,
4294 (unsigned long) isym->st_size);
4296 h->size = isym->st_size;
4299 /* If this is a common symbol, then we always want H->SIZE
4300 to be the size of the common symbol. The code just above
4301 won't fix the size if a common symbol becomes larger. We
4302 don't warn about a size change here, because that is
4303 covered by --warn-common. Allow changed between different
4305 if (h->root.type == bfd_link_hash_common)
4306 h->size = h->root.u.c.size;
4308 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4309 && (definition || h->type == STT_NOTYPE))
4311 unsigned int type = ELF_ST_TYPE (isym->st_info);
4313 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4315 if (type == STT_GNU_IFUNC
4316 && (abfd->flags & DYNAMIC) != 0)
4319 if (h->type != type)
4321 if (h->type != STT_NOTYPE && ! type_change_ok)
4322 (*_bfd_error_handler)
4323 (_("Warning: type of symbol `%s' changed"
4324 " from %d to %d in %B"),
4325 abfd, name, h->type, type);
4331 /* Merge st_other field. */
4332 elf_merge_st_other (abfd, h, isym, definition, dynamic);
4334 /* Set a flag in the hash table entry indicating the type of
4335 reference or definition we just found. Keep a count of
4336 the number of dynamic symbols we find. A dynamic symbol
4337 is one which is referenced or defined by both a regular
4338 object and a shared object. */
4345 if (bind != STB_WEAK)
4346 h->ref_regular_nonweak = 1;
4357 if (! info->executable
4373 || (h->u.weakdef != NULL
4375 && h->u.weakdef->dynindx != -1))
4379 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4381 /* We don't want to make debug symbol dynamic. */
4386 h->target_internal = isym->st_target_internal;
4388 /* Check to see if we need to add an indirect symbol for
4389 the default name. */
4390 if (definition || h->root.type == bfd_link_hash_common)
4391 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4392 &sec, &value, &dynsym,
4394 goto error_free_vers;
4396 if (definition && !dynamic)
4398 char *p = strchr (name, ELF_VER_CHR);
4399 if (p != NULL && p[1] != ELF_VER_CHR)
4401 /* Queue non-default versions so that .symver x, x@FOO
4402 aliases can be checked. */
4405 amt = ((isymend - isym + 1)
4406 * sizeof (struct elf_link_hash_entry *));
4408 (struct elf_link_hash_entry **) bfd_malloc (amt);
4410 goto error_free_vers;
4412 nondeflt_vers[nondeflt_vers_cnt++] = h;
4416 if (dynsym && h->dynindx == -1)
4418 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4419 goto error_free_vers;
4420 if (h->u.weakdef != NULL
4422 && h->u.weakdef->dynindx == -1)
4424 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4425 goto error_free_vers;
4428 else if (dynsym && h->dynindx != -1)
4429 /* If the symbol already has a dynamic index, but
4430 visibility says it should not be visible, turn it into
4432 switch (ELF_ST_VISIBILITY (h->other))
4436 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4446 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4447 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4450 const char *soname = elf_dt_name (abfd);
4452 /* A symbol from a library loaded via DT_NEEDED of some
4453 other library is referenced by a regular object.
4454 Add a DT_NEEDED entry for it. Issue an error if
4455 --no-add-needed is used and the reference was not
4457 if (undef_bfd != NULL
4458 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4460 (*_bfd_error_handler)
4461 (_("%B: undefined reference to symbol '%s'"),
4463 (*_bfd_error_handler)
4464 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"),
4466 bfd_set_error (bfd_error_invalid_operation);
4467 goto error_free_vers;
4470 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4471 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4474 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4476 goto error_free_vers;
4478 BFD_ASSERT (ret == 0);
4483 if (extversym != NULL)
4489 if (isymbuf != NULL)
4495 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4499 /* Restore the symbol table. */
4500 if (bed->as_needed_cleanup)
4501 (*bed->as_needed_cleanup) (abfd, info);
4502 old_hash = (char *) old_tab + tabsize;
4503 old_ent = (char *) old_hash + hashsize;
4504 sym_hash = elf_sym_hashes (abfd);
4505 htab->root.table.table = old_table;
4506 htab->root.table.size = old_size;
4507 htab->root.table.count = old_count;
4508 memcpy (htab->root.table.table, old_tab, tabsize);
4509 memcpy (sym_hash, old_hash, hashsize);
4510 htab->root.undefs = old_undefs;
4511 htab->root.undefs_tail = old_undefs_tail;
4512 for (i = 0; i < htab->root.table.size; i++)
4514 struct bfd_hash_entry *p;
4515 struct elf_link_hash_entry *h;
4517 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4519 h = (struct elf_link_hash_entry *) p;
4520 if (h->root.type == bfd_link_hash_warning)
4521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4522 if (h->dynindx >= old_dynsymcount)
4523 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4525 memcpy (p, old_ent, htab->root.table.entsize);
4526 old_ent = (char *) old_ent + htab->root.table.entsize;
4527 h = (struct elf_link_hash_entry *) p;
4528 if (h->root.type == bfd_link_hash_warning)
4530 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4531 old_ent = (char *) old_ent + htab->root.table.entsize;
4536 /* Make a special call to the linker "notice" function to
4537 tell it that symbols added for crefs may need to be removed. */
4538 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4539 notice_not_needed, 0, NULL))
4540 goto error_free_vers;
4543 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4545 if (nondeflt_vers != NULL)
4546 free (nondeflt_vers);
4550 if (old_tab != NULL)
4552 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL,
4553 notice_needed, 0, NULL))
4554 goto error_free_vers;
4559 /* Now that all the symbols from this input file are created, handle
4560 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */
4561 if (nondeflt_vers != NULL)
4563 bfd_size_type cnt, symidx;
4565 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4567 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4568 char *shortname, *p;
4570 p = strchr (h->root.root.string, ELF_VER_CHR);
4572 || (h->root.type != bfd_link_hash_defined
4573 && h->root.type != bfd_link_hash_defweak))
4576 amt = p - h->root.root.string;
4577 shortname = (char *) bfd_malloc (amt + 1);
4579 goto error_free_vers;
4580 memcpy (shortname, h->root.root.string, amt);
4581 shortname[amt] = '\0';
4583 hi = (struct elf_link_hash_entry *)
4584 bfd_link_hash_lookup (&htab->root, shortname,
4585 FALSE, FALSE, FALSE);
4587 && hi->root.type == h->root.type
4588 && hi->root.u.def.value == h->root.u.def.value
4589 && hi->root.u.def.section == h->root.u.def.section)
4591 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4592 hi->root.type = bfd_link_hash_indirect;
4593 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4594 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4595 sym_hash = elf_sym_hashes (abfd);
4597 for (symidx = 0; symidx < extsymcount; ++symidx)
4598 if (sym_hash[symidx] == hi)
4600 sym_hash[symidx] = h;
4606 free (nondeflt_vers);
4607 nondeflt_vers = NULL;
4610 /* Now set the weakdefs field correctly for all the weak defined
4611 symbols we found. The only way to do this is to search all the
4612 symbols. Since we only need the information for non functions in
4613 dynamic objects, that's the only time we actually put anything on
4614 the list WEAKS. We need this information so that if a regular
4615 object refers to a symbol defined weakly in a dynamic object, the
4616 real symbol in the dynamic object is also put in the dynamic
4617 symbols; we also must arrange for both symbols to point to the
4618 same memory location. We could handle the general case of symbol
4619 aliasing, but a general symbol alias can only be generated in
4620 assembler code, handling it correctly would be very time
4621 consuming, and other ELF linkers don't handle general aliasing
4625 struct elf_link_hash_entry **hpp;
4626 struct elf_link_hash_entry **hppend;
4627 struct elf_link_hash_entry **sorted_sym_hash;
4628 struct elf_link_hash_entry *h;
4631 /* Since we have to search the whole symbol list for each weak
4632 defined symbol, search time for N weak defined symbols will be
4633 O(N^2). Binary search will cut it down to O(NlogN). */
4634 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4635 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4636 if (sorted_sym_hash == NULL)
4638 sym_hash = sorted_sym_hash;
4639 hpp = elf_sym_hashes (abfd);
4640 hppend = hpp + extsymcount;
4642 for (; hpp < hppend; hpp++)
4646 && h->root.type == bfd_link_hash_defined
4647 && !bed->is_function_type (h->type))
4655 qsort (sorted_sym_hash, sym_count,
4656 sizeof (struct elf_link_hash_entry *),
4659 while (weaks != NULL)
4661 struct elf_link_hash_entry *hlook;
4668 weaks = hlook->u.weakdef;
4669 hlook->u.weakdef = NULL;
4671 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4672 || hlook->root.type == bfd_link_hash_defweak
4673 || hlook->root.type == bfd_link_hash_common
4674 || hlook->root.type == bfd_link_hash_indirect);
4675 slook = hlook->root.u.def.section;
4676 vlook = hlook->root.u.def.value;
4683 bfd_signed_vma vdiff;
4685 h = sorted_sym_hash [idx];
4686 vdiff = vlook - h->root.u.def.value;
4693 long sdiff = slook->id - h->root.u.def.section->id;
4706 /* We didn't find a value/section match. */
4710 for (i = ilook; i < sym_count; i++)
4712 h = sorted_sym_hash [i];
4714 /* Stop if value or section doesn't match. */
4715 if (h->root.u.def.value != vlook
4716 || h->root.u.def.section != slook)
4718 else if (h != hlook)
4720 hlook->u.weakdef = h;
4722 /* If the weak definition is in the list of dynamic
4723 symbols, make sure the real definition is put
4725 if (hlook->dynindx != -1 && h->dynindx == -1)
4727 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4730 free (sorted_sym_hash);
4735 /* If the real definition is in the list of dynamic
4736 symbols, make sure the weak definition is put
4737 there as well. If we don't do this, then the
4738 dynamic loader might not merge the entries for the
4739 real definition and the weak definition. */
4740 if (h->dynindx != -1 && hlook->dynindx == -1)
4742 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4743 goto err_free_sym_hash;
4750 free (sorted_sym_hash);
4753 if (bed->check_directives
4754 && !(*bed->check_directives) (abfd, info))
4757 /* If this object is the same format as the output object, and it is
4758 not a shared library, then let the backend look through the
4761 This is required to build global offset table entries and to
4762 arrange for dynamic relocs. It is not required for the
4763 particular common case of linking non PIC code, even when linking
4764 against shared libraries, but unfortunately there is no way of
4765 knowing whether an object file has been compiled PIC or not.
4766 Looking through the relocs is not particularly time consuming.
4767 The problem is that we must either (1) keep the relocs in memory,
4768 which causes the linker to require additional runtime memory or
4769 (2) read the relocs twice from the input file, which wastes time.
4770 This would be a good case for using mmap.
4772 I have no idea how to handle linking PIC code into a file of a
4773 different format. It probably can't be done. */
4775 && is_elf_hash_table (htab)
4776 && bed->check_relocs != NULL
4777 && elf_object_id (abfd) == elf_hash_table_id (htab)
4778 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4782 for (o = abfd->sections; o != NULL; o = o->next)
4784 Elf_Internal_Rela *internal_relocs;
4787 if ((o->flags & SEC_RELOC) == 0
4788 || o->reloc_count == 0
4789 || ((info->strip == strip_all || info->strip == strip_debugger)
4790 && (o->flags & SEC_DEBUGGING) != 0)
4791 || bfd_is_abs_section (o->output_section))
4794 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4796 if (internal_relocs == NULL)
4799 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4801 if (elf_section_data (o)->relocs != internal_relocs)
4802 free (internal_relocs);
4809 /* If this is a non-traditional link, try to optimize the handling
4810 of the .stab/.stabstr sections. */
4812 && ! info->traditional_format
4813 && is_elf_hash_table (htab)
4814 && (info->strip != strip_all && info->strip != strip_debugger))
4818 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4819 if (stabstr != NULL)
4821 bfd_size_type string_offset = 0;
4824 for (stab = abfd->sections; stab; stab = stab->next)
4825 if (CONST_STRNEQ (stab->name, ".stab")
4826 && (!stab->name[5] ||
4827 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4828 && (stab->flags & SEC_MERGE) == 0
4829 && !bfd_is_abs_section (stab->output_section))
4831 struct bfd_elf_section_data *secdata;
4833 secdata = elf_section_data (stab);
4834 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4835 stabstr, &secdata->sec_info,
4838 if (secdata->sec_info)
4839 stab->sec_info_type = ELF_INFO_TYPE_STABS;
4844 if (is_elf_hash_table (htab) && add_needed)
4846 /* Add this bfd to the loaded list. */
4847 struct elf_link_loaded_list *n;
4849 n = (struct elf_link_loaded_list *)
4850 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list));
4854 n->next = htab->loaded;
4861 if (old_tab != NULL)
4863 if (nondeflt_vers != NULL)
4864 free (nondeflt_vers);
4865 if (extversym != NULL)
4868 if (isymbuf != NULL)
4874 /* Return the linker hash table entry of a symbol that might be
4875 satisfied by an archive symbol. Return -1 on error. */
4877 struct elf_link_hash_entry *
4878 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4879 struct bfd_link_info *info,
4882 struct elf_link_hash_entry *h;
4886 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4890 /* If this is a default version (the name contains @@), look up the
4891 symbol again with only one `@' as well as without the version.
4892 The effect is that references to the symbol with and without the
4893 version will be matched by the default symbol in the archive. */
4895 p = strchr (name, ELF_VER_CHR);
4896 if (p == NULL || p[1] != ELF_VER_CHR)
4899 /* First check with only one `@'. */
4900 len = strlen (name);
4901 copy = (char *) bfd_alloc (abfd, len);
4903 return (struct elf_link_hash_entry *) 0 - 1;
4905 first = p - name + 1;
4906 memcpy (copy, name, first);
4907 memcpy (copy + first, name + first + 1, len - first);
4909 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4912 /* We also need to check references to the symbol without the
4914 copy[first - 1] = '\0';
4915 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4916 FALSE, FALSE, TRUE);
4919 bfd_release (abfd, copy);
4923 /* Add symbols from an ELF archive file to the linker hash table. We
4924 don't use _bfd_generic_link_add_archive_symbols because of a
4925 problem which arises on UnixWare. The UnixWare libc.so is an
4926 archive which includes an entry libc.so.1 which defines a bunch of
4927 symbols. The libc.so archive also includes a number of other
4928 object files, which also define symbols, some of which are the same
4929 as those defined in libc.so.1. Correct linking requires that we
4930 consider each object file in turn, and include it if it defines any
4931 symbols we need. _bfd_generic_link_add_archive_symbols does not do
4932 this; it looks through the list of undefined symbols, and includes
4933 any object file which defines them. When this algorithm is used on
4934 UnixWare, it winds up pulling in libc.so.1 early and defining a
4935 bunch of symbols. This means that some of the other objects in the
4936 archive are not included in the link, which is incorrect since they
4937 precede libc.so.1 in the archive.
4939 Fortunately, ELF archive handling is simpler than that done by
4940 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4941 oddities. In ELF, if we find a symbol in the archive map, and the
4942 symbol is currently undefined, we know that we must pull in that
4945 Unfortunately, we do have to make multiple passes over the symbol
4946 table until nothing further is resolved. */
4949 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
4952 bfd_boolean *defined = NULL;
4953 bfd_boolean *included = NULL;
4957 const struct elf_backend_data *bed;
4958 struct elf_link_hash_entry * (*archive_symbol_lookup)
4959 (bfd *, struct bfd_link_info *, const char *);
4961 if (! bfd_has_map (abfd))
4963 /* An empty archive is a special case. */
4964 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
4966 bfd_set_error (bfd_error_no_armap);
4970 /* Keep track of all symbols we know to be already defined, and all
4971 files we know to be already included. This is to speed up the
4972 second and subsequent passes. */
4973 c = bfd_ardata (abfd)->symdef_count;
4977 amt *= sizeof (bfd_boolean);
4978 defined = (bfd_boolean *) bfd_zmalloc (amt);
4979 included = (bfd_boolean *) bfd_zmalloc (amt);
4980 if (defined == NULL || included == NULL)
4983 symdefs = bfd_ardata (abfd)->symdefs;
4984 bed = get_elf_backend_data (abfd);
4985 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
4998 symdefend = symdef + c;
4999 for (i = 0; symdef < symdefend; symdef++, i++)
5001 struct elf_link_hash_entry *h;
5003 struct bfd_link_hash_entry *undefs_tail;
5006 if (defined[i] || included[i])
5008 if (symdef->file_offset == last)
5014 h = archive_symbol_lookup (abfd, info, symdef->name);
5015 if (h == (struct elf_link_hash_entry *) 0 - 1)
5021 if (h->root.type == bfd_link_hash_common)
5023 /* We currently have a common symbol. The archive map contains
5024 a reference to this symbol, so we may want to include it. We
5025 only want to include it however, if this archive element
5026 contains a definition of the symbol, not just another common
5029 Unfortunately some archivers (including GNU ar) will put
5030 declarations of common symbols into their archive maps, as
5031 well as real definitions, so we cannot just go by the archive
5032 map alone. Instead we must read in the element's symbol
5033 table and check that to see what kind of symbol definition
5035 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5038 else if (h->root.type != bfd_link_hash_undefined)
5040 if (h->root.type != bfd_link_hash_undefweak)
5045 /* We need to include this archive member. */
5046 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5047 if (element == NULL)
5050 if (! bfd_check_format (element, bfd_object))
5053 /* Doublecheck that we have not included this object
5054 already--it should be impossible, but there may be
5055 something wrong with the archive. */
5056 if (element->archive_pass != 0)
5058 bfd_set_error (bfd_error_bad_value);
5061 element->archive_pass = 1;
5063 undefs_tail = info->hash->undefs_tail;
5065 if (!(*info->callbacks
5066 ->add_archive_element) (info, element, symdef->name, &element))
5068 if (!bfd_link_add_symbols (element, info))
5071 /* If there are any new undefined symbols, we need to make
5072 another pass through the archive in order to see whether
5073 they can be defined. FIXME: This isn't perfect, because
5074 common symbols wind up on undefs_tail and because an
5075 undefined symbol which is defined later on in this pass
5076 does not require another pass. This isn't a bug, but it
5077 does make the code less efficient than it could be. */
5078 if (undefs_tail != info->hash->undefs_tail)
5081 /* Look backward to mark all symbols from this object file
5082 which we have already seen in this pass. */
5086 included[mark] = TRUE;
5091 while (symdefs[mark].file_offset == symdef->file_offset);
5093 /* We mark subsequent symbols from this object file as we go
5094 on through the loop. */
5095 last = symdef->file_offset;
5106 if (defined != NULL)
5108 if (included != NULL)
5113 /* Given an ELF BFD, add symbols to the global hash table as
5117 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5119 switch (bfd_get_format (abfd))
5122 return elf_link_add_object_symbols (abfd, info);
5124 return elf_link_add_archive_symbols (abfd, info);
5126 bfd_set_error (bfd_error_wrong_format);
5131 struct hash_codes_info
5133 unsigned long *hashcodes;
5137 /* This function will be called though elf_link_hash_traverse to store
5138 all hash value of the exported symbols in an array. */
5141 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5143 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5149 /* Ignore indirect symbols. These are added by the versioning code. */
5150 if (h->dynindx == -1)
5153 name = h->root.root.string;
5154 p = strchr (name, ELF_VER_CHR);
5157 alc = (char *) bfd_malloc (p - name + 1);
5163 memcpy (alc, name, p - name);
5164 alc[p - name] = '\0';
5168 /* Compute the hash value. */
5169 ha = bfd_elf_hash (name);
5171 /* Store the found hash value in the array given as the argument. */
5172 *(inf->hashcodes)++ = ha;
5174 /* And store it in the struct so that we can put it in the hash table
5176 h->u.elf_hash_value = ha;
5184 struct collect_gnu_hash_codes
5187 const struct elf_backend_data *bed;
5188 unsigned long int nsyms;
5189 unsigned long int maskbits;
5190 unsigned long int *hashcodes;
5191 unsigned long int *hashval;
5192 unsigned long int *indx;
5193 unsigned long int *counts;
5196 long int min_dynindx;
5197 unsigned long int bucketcount;
5198 unsigned long int symindx;
5199 long int local_indx;
5200 long int shift1, shift2;
5201 unsigned long int mask;
5205 /* This function will be called though elf_link_hash_traverse to store
5206 all hash value of the exported symbols in an array. */
5209 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5211 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5217 /* Ignore indirect symbols. These are added by the versioning code. */
5218 if (h->dynindx == -1)
5221 /* Ignore also local symbols and undefined symbols. */
5222 if (! (*s->bed->elf_hash_symbol) (h))
5225 name = h->root.root.string;
5226 p = strchr (name, ELF_VER_CHR);
5229 alc = (char *) bfd_malloc (p - name + 1);
5235 memcpy (alc, name, p - name);
5236 alc[p - name] = '\0';
5240 /* Compute the hash value. */
5241 ha = bfd_elf_gnu_hash (name);
5243 /* Store the found hash value in the array for compute_bucket_count,
5244 and also for .dynsym reordering purposes. */
5245 s->hashcodes[s->nsyms] = ha;
5246 s->hashval[h->dynindx] = ha;
5248 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5249 s->min_dynindx = h->dynindx;
5257 /* This function will be called though elf_link_hash_traverse to do
5258 final dynaminc symbol renumbering. */
5261 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5263 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5264 unsigned long int bucket;
5265 unsigned long int val;
5267 /* Ignore indirect symbols. */
5268 if (h->dynindx == -1)
5271 /* Ignore also local symbols and undefined symbols. */
5272 if (! (*s->bed->elf_hash_symbol) (h))
5274 if (h->dynindx >= s->min_dynindx)
5275 h->dynindx = s->local_indx++;
5279 bucket = s->hashval[h->dynindx] % s->bucketcount;
5280 val = (s->hashval[h->dynindx] >> s->shift1)
5281 & ((s->maskbits >> s->shift1) - 1);
5282 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5284 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5285 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5286 if (s->counts[bucket] == 1)
5287 /* Last element terminates the chain. */
5289 bfd_put_32 (s->output_bfd, val,
5290 s->contents + (s->indx[bucket] - s->symindx) * 4);
5291 --s->counts[bucket];
5292 h->dynindx = s->indx[bucket]++;
5296 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5299 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5301 return !(h->forced_local
5302 || h->root.type == bfd_link_hash_undefined
5303 || h->root.type == bfd_link_hash_undefweak
5304 || ((h->root.type == bfd_link_hash_defined
5305 || h->root.type == bfd_link_hash_defweak)
5306 && h->root.u.def.section->output_section == NULL));
5309 /* Array used to determine the number of hash table buckets to use
5310 based on the number of symbols there are. If there are fewer than
5311 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5312 fewer than 37 we use 17 buckets, and so forth. We never use more
5313 than 32771 buckets. */
5315 static const size_t elf_buckets[] =
5317 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5321 /* Compute bucket count for hashing table. We do not use a static set
5322 of possible tables sizes anymore. Instead we determine for all
5323 possible reasonable sizes of the table the outcome (i.e., the
5324 number of collisions etc) and choose the best solution. The
5325 weighting functions are not too simple to allow the table to grow
5326 without bounds. Instead one of the weighting factors is the size.
5327 Therefore the result is always a good payoff between few collisions
5328 (= short chain lengths) and table size. */
5330 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5331 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5332 unsigned long int nsyms,
5335 size_t best_size = 0;
5336 unsigned long int i;
5338 /* We have a problem here. The following code to optimize the table
5339 size requires an integer type with more the 32 bits. If
5340 BFD_HOST_U_64_BIT is set we know about such a type. */
5341 #ifdef BFD_HOST_U_64_BIT
5346 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5347 bfd *dynobj = elf_hash_table (info)->dynobj;
5348 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5349 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5350 unsigned long int *counts;
5352 unsigned int no_improvement_count = 0;
5354 /* Possible optimization parameters: if we have NSYMS symbols we say
5355 that the hashing table must at least have NSYMS/4 and at most
5357 minsize = nsyms / 4;
5360 best_size = maxsize = nsyms * 2;
5365 if ((best_size & 31) == 0)
5369 /* Create array where we count the collisions in. We must use bfd_malloc
5370 since the size could be large. */
5372 amt *= sizeof (unsigned long int);
5373 counts = (unsigned long int *) bfd_malloc (amt);
5377 /* Compute the "optimal" size for the hash table. The criteria is a
5378 minimal chain length. The minor criteria is (of course) the size
5380 for (i = minsize; i < maxsize; ++i)
5382 /* Walk through the array of hashcodes and count the collisions. */
5383 BFD_HOST_U_64_BIT max;
5384 unsigned long int j;
5385 unsigned long int fact;
5387 if (gnu_hash && (i & 31) == 0)
5390 memset (counts, '\0', i * sizeof (unsigned long int));
5392 /* Determine how often each hash bucket is used. */
5393 for (j = 0; j < nsyms; ++j)
5394 ++counts[hashcodes[j] % i];
5396 /* For the weight function we need some information about the
5397 pagesize on the target. This is information need not be 100%
5398 accurate. Since this information is not available (so far) we
5399 define it here to a reasonable default value. If it is crucial
5400 to have a better value some day simply define this value. */
5401 # ifndef BFD_TARGET_PAGESIZE
5402 # define BFD_TARGET_PAGESIZE (4096)
5405 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5407 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5410 /* Variant 1: optimize for short chains. We add the squares
5411 of all the chain lengths (which favors many small chain
5412 over a few long chains). */
5413 for (j = 0; j < i; ++j)
5414 max += counts[j] * counts[j];
5416 /* This adds penalties for the overall size of the table. */
5417 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5420 /* Variant 2: Optimize a lot more for small table. Here we
5421 also add squares of the size but we also add penalties for
5422 empty slots (the +1 term). */
5423 for (j = 0; j < i; ++j)
5424 max += (1 + counts[j]) * (1 + counts[j]);
5426 /* The overall size of the table is considered, but not as
5427 strong as in variant 1, where it is squared. */
5428 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5432 /* Compare with current best results. */
5433 if (max < best_chlen)
5437 no_improvement_count = 0;
5439 /* PR 11843: Avoid futile long searches for the best bucket size
5440 when there are a large number of symbols. */
5441 else if (++no_improvement_count == 100)
5448 #endif /* defined (BFD_HOST_U_64_BIT) */
5450 /* This is the fallback solution if no 64bit type is available or if we
5451 are not supposed to spend much time on optimizations. We select the
5452 bucket count using a fixed set of numbers. */
5453 for (i = 0; elf_buckets[i] != 0; i++)
5455 best_size = elf_buckets[i];
5456 if (nsyms < elf_buckets[i + 1])
5459 if (gnu_hash && best_size < 2)
5466 /* Size any SHT_GROUP section for ld -r. */
5469 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5473 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5474 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5475 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5480 /* Set up the sizes and contents of the ELF dynamic sections. This is
5481 called by the ELF linker emulation before_allocation routine. We
5482 must set the sizes of the sections before the linker sets the
5483 addresses of the various sections. */
5486 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5489 const char *filter_shlib,
5491 const char *depaudit,
5492 const char * const *auxiliary_filters,
5493 struct bfd_link_info *info,
5494 asection **sinterpptr)
5496 bfd_size_type soname_indx;
5498 const struct elf_backend_data *bed;
5499 struct elf_info_failed asvinfo;
5503 soname_indx = (bfd_size_type) -1;
5505 if (!is_elf_hash_table (info->hash))
5508 bed = get_elf_backend_data (output_bfd);
5509 if (info->execstack)
5510 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X;
5511 else if (info->noexecstack)
5512 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W;
5516 asection *notesec = NULL;
5519 for (inputobj = info->input_bfds;
5521 inputobj = inputobj->link_next)
5526 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5528 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5531 if (s->flags & SEC_CODE)
5535 else if (bed->default_execstack)
5540 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec;
5541 if (exec && info->relocatable
5542 && notesec->output_section != bfd_abs_section_ptr)
5543 notesec->output_section->flags |= SEC_CODE;
5547 /* Any syms created from now on start with -1 in
5548 got.refcount/offset and plt.refcount/offset. */
5549 elf_hash_table (info)->init_got_refcount
5550 = elf_hash_table (info)->init_got_offset;
5551 elf_hash_table (info)->init_plt_refcount
5552 = elf_hash_table (info)->init_plt_offset;
5554 if (info->relocatable
5555 && !_bfd_elf_size_group_sections (info))
5558 /* The backend may have to create some sections regardless of whether
5559 we're dynamic or not. */
5560 if (bed->elf_backend_always_size_sections
5561 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5564 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5567 dynobj = elf_hash_table (info)->dynobj;
5569 /* If there were no dynamic objects in the link, there is nothing to
5574 if (elf_hash_table (info)->dynamic_sections_created)
5576 struct elf_info_failed eif;
5577 struct elf_link_hash_entry *h;
5579 struct bfd_elf_version_tree *t;
5580 struct bfd_elf_version_expr *d;
5582 bfd_boolean all_defined;
5584 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp");
5585 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5589 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5591 if (soname_indx == (bfd_size_type) -1
5592 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5598 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5600 info->flags |= DF_SYMBOLIC;
5607 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5609 if (indx == (bfd_size_type) -1
5610 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx))
5613 if (info->new_dtags)
5615 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx);
5616 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx))
5621 if (filter_shlib != NULL)
5625 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5626 filter_shlib, TRUE);
5627 if (indx == (bfd_size_type) -1
5628 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5632 if (auxiliary_filters != NULL)
5634 const char * const *p;
5636 for (p = auxiliary_filters; *p != NULL; p++)
5640 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5642 if (indx == (bfd_size_type) -1
5643 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5652 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5654 if (indx == (bfd_size_type) -1
5655 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5659 if (depaudit != NULL)
5663 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5665 if (indx == (bfd_size_type) -1
5666 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5673 /* If we are supposed to export all symbols into the dynamic symbol
5674 table (this is not the normal case), then do so. */
5675 if (info->export_dynamic
5676 || (info->executable && info->dynamic))
5678 elf_link_hash_traverse (elf_hash_table (info),
5679 _bfd_elf_export_symbol,
5685 /* Make all global versions with definition. */
5686 for (t = info->version_info; t != NULL; t = t->next)
5687 for (d = t->globals.list; d != NULL; d = d->next)
5688 if (!d->symver && d->literal)
5690 const char *verstr, *name;
5691 size_t namelen, verlen, newlen;
5692 char *newname, *p, leading_char;
5693 struct elf_link_hash_entry *newh;
5695 leading_char = bfd_get_symbol_leading_char (output_bfd);
5697 namelen = strlen (name) + (leading_char != '\0');
5699 verlen = strlen (verstr);
5700 newlen = namelen + verlen + 3;
5702 newname = (char *) bfd_malloc (newlen);
5703 if (newname == NULL)
5705 newname[0] = leading_char;
5706 memcpy (newname + (leading_char != '\0'), name, namelen);
5708 /* Check the hidden versioned definition. */
5709 p = newname + namelen;
5711 memcpy (p, verstr, verlen + 1);
5712 newh = elf_link_hash_lookup (elf_hash_table (info),
5713 newname, FALSE, FALSE,
5716 || (newh->root.type != bfd_link_hash_defined
5717 && newh->root.type != bfd_link_hash_defweak))
5719 /* Check the default versioned definition. */
5721 memcpy (p, verstr, verlen + 1);
5722 newh = elf_link_hash_lookup (elf_hash_table (info),
5723 newname, FALSE, FALSE,
5728 /* Mark this version if there is a definition and it is
5729 not defined in a shared object. */
5731 && !newh->def_dynamic
5732 && (newh->root.type == bfd_link_hash_defined
5733 || newh->root.type == bfd_link_hash_defweak))
5737 /* Attach all the symbols to their version information. */
5738 asvinfo.info = info;
5739 asvinfo.failed = FALSE;
5741 elf_link_hash_traverse (elf_hash_table (info),
5742 _bfd_elf_link_assign_sym_version,
5747 if (!info->allow_undefined_version)
5749 /* Check if all global versions have a definition. */
5751 for (t = info->version_info; t != NULL; t = t->next)
5752 for (d = t->globals.list; d != NULL; d = d->next)
5753 if (d->literal && !d->symver && !d->script)
5755 (*_bfd_error_handler)
5756 (_("%s: undefined version: %s"),
5757 d->pattern, t->name);
5758 all_defined = FALSE;
5763 bfd_set_error (bfd_error_bad_value);
5768 /* Find all symbols which were defined in a dynamic object and make
5769 the backend pick a reasonable value for them. */
5770 elf_link_hash_traverse (elf_hash_table (info),
5771 _bfd_elf_adjust_dynamic_symbol,
5776 /* Add some entries to the .dynamic section. We fill in some of the
5777 values later, in bfd_elf_final_link, but we must add the entries
5778 now so that we know the final size of the .dynamic section. */
5780 /* If there are initialization and/or finalization functions to
5781 call then add the corresponding DT_INIT/DT_FINI entries. */
5782 h = (info->init_function
5783 ? elf_link_hash_lookup (elf_hash_table (info),
5784 info->init_function, FALSE,
5791 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5794 h = (info->fini_function
5795 ? elf_link_hash_lookup (elf_hash_table (info),
5796 info->fini_function, FALSE,
5803 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5807 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5808 if (s != NULL && s->linker_has_input)
5810 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5811 if (! info->executable)
5816 for (sub = info->input_bfds; sub != NULL;
5817 sub = sub->link_next)
5818 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5819 for (o = sub->sections; o != NULL; o = o->next)
5820 if (elf_section_data (o)->this_hdr.sh_type
5821 == SHT_PREINIT_ARRAY)
5823 (*_bfd_error_handler)
5824 (_("%B: .preinit_array section is not allowed in DSO"),
5829 bfd_set_error (bfd_error_nonrepresentable_section);
5833 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5834 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5837 s = bfd_get_section_by_name (output_bfd, ".init_array");
5838 if (s != NULL && s->linker_has_input)
5840 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5841 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5844 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5845 if (s != NULL && s->linker_has_input)
5847 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5848 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5852 dynstr = bfd_get_section_by_name (dynobj, ".dynstr");
5853 /* If .dynstr is excluded from the link, we don't want any of
5854 these tags. Strictly, we should be checking each section
5855 individually; This quick check covers for the case where
5856 someone does a /DISCARD/ : { *(*) }. */
5857 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5859 bfd_size_type strsize;
5861 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5862 if ((info->emit_hash
5863 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5864 || (info->emit_gnu_hash
5865 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5866 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5867 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5868 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5869 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5870 bed->s->sizeof_sym))
5875 /* The backend must work out the sizes of all the other dynamic
5877 if (bed->elf_backend_size_dynamic_sections
5878 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5881 if (elf_hash_table (info)->dynamic_sections_created)
5883 unsigned long section_sym_count;
5884 struct bfd_elf_version_tree *verdefs;
5887 /* Set up the version definition section. */
5888 s = bfd_get_section_by_name (dynobj, ".gnu.version_d");
5889 BFD_ASSERT (s != NULL);
5891 /* We may have created additional version definitions if we are
5892 just linking a regular application. */
5893 verdefs = info->version_info;
5895 /* Skip anonymous version tag. */
5896 if (verdefs != NULL && verdefs->vernum == 0)
5897 verdefs = verdefs->next;
5899 if (verdefs == NULL && !info->create_default_symver)
5900 s->flags |= SEC_EXCLUDE;
5905 struct bfd_elf_version_tree *t;
5907 Elf_Internal_Verdef def;
5908 Elf_Internal_Verdaux defaux;
5909 struct bfd_link_hash_entry *bh;
5910 struct elf_link_hash_entry *h;
5916 /* Make space for the base version. */
5917 size += sizeof (Elf_External_Verdef);
5918 size += sizeof (Elf_External_Verdaux);
5921 /* Make space for the default version. */
5922 if (info->create_default_symver)
5924 size += sizeof (Elf_External_Verdef);
5928 for (t = verdefs; t != NULL; t = t->next)
5930 struct bfd_elf_version_deps *n;
5932 /* Don't emit base version twice. */
5936 size += sizeof (Elf_External_Verdef);
5937 size += sizeof (Elf_External_Verdaux);
5940 for (n = t->deps; n != NULL; n = n->next)
5941 size += sizeof (Elf_External_Verdaux);
5945 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
5946 if (s->contents == NULL && s->size != 0)
5949 /* Fill in the version definition section. */
5953 def.vd_version = VER_DEF_CURRENT;
5954 def.vd_flags = VER_FLG_BASE;
5957 if (info->create_default_symver)
5959 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
5960 def.vd_next = sizeof (Elf_External_Verdef);
5964 def.vd_aux = sizeof (Elf_External_Verdef);
5965 def.vd_next = (sizeof (Elf_External_Verdef)
5966 + sizeof (Elf_External_Verdaux));
5969 if (soname_indx != (bfd_size_type) -1)
5971 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
5973 def.vd_hash = bfd_elf_hash (soname);
5974 defaux.vda_name = soname_indx;
5981 name = lbasename (output_bfd->filename);
5982 def.vd_hash = bfd_elf_hash (name);
5983 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5985 if (indx == (bfd_size_type) -1)
5987 defaux.vda_name = indx;
5989 defaux.vda_next = 0;
5991 _bfd_elf_swap_verdef_out (output_bfd, &def,
5992 (Elf_External_Verdef *) p);
5993 p += sizeof (Elf_External_Verdef);
5994 if (info->create_default_symver)
5996 /* Add a symbol representing this version. */
5998 if (! (_bfd_generic_link_add_one_symbol
5999 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6001 get_elf_backend_data (dynobj)->collect, &bh)))
6003 h = (struct elf_link_hash_entry *) bh;
6006 h->type = STT_OBJECT;
6007 h->verinfo.vertree = NULL;
6009 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6012 /* Create a duplicate of the base version with the same
6013 aux block, but different flags. */
6016 def.vd_aux = sizeof (Elf_External_Verdef);
6018 def.vd_next = (sizeof (Elf_External_Verdef)
6019 + sizeof (Elf_External_Verdaux));
6022 _bfd_elf_swap_verdef_out (output_bfd, &def,
6023 (Elf_External_Verdef *) p);
6024 p += sizeof (Elf_External_Verdef);
6026 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6027 (Elf_External_Verdaux *) p);
6028 p += sizeof (Elf_External_Verdaux);
6030 for (t = verdefs; t != NULL; t = t->next)
6033 struct bfd_elf_version_deps *n;
6035 /* Don't emit the base version twice. */
6040 for (n = t->deps; n != NULL; n = n->next)
6043 /* Add a symbol representing this version. */
6045 if (! (_bfd_generic_link_add_one_symbol
6046 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6048 get_elf_backend_data (dynobj)->collect, &bh)))
6050 h = (struct elf_link_hash_entry *) bh;
6053 h->type = STT_OBJECT;
6054 h->verinfo.vertree = t;
6056 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6059 def.vd_version = VER_DEF_CURRENT;
6061 if (t->globals.list == NULL
6062 && t->locals.list == NULL
6064 def.vd_flags |= VER_FLG_WEAK;
6065 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6066 def.vd_cnt = cdeps + 1;
6067 def.vd_hash = bfd_elf_hash (t->name);
6068 def.vd_aux = sizeof (Elf_External_Verdef);
6071 /* If a basever node is next, it *must* be the last node in
6072 the chain, otherwise Verdef construction breaks. */
6073 if (t->next != NULL && t->next->vernum == 0)
6074 BFD_ASSERT (t->next->next == NULL);
6076 if (t->next != NULL && t->next->vernum != 0)
6077 def.vd_next = (sizeof (Elf_External_Verdef)
6078 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6080 _bfd_elf_swap_verdef_out (output_bfd, &def,
6081 (Elf_External_Verdef *) p);
6082 p += sizeof (Elf_External_Verdef);
6084 defaux.vda_name = h->dynstr_index;
6085 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6087 defaux.vda_next = 0;
6088 if (t->deps != NULL)
6089 defaux.vda_next = sizeof (Elf_External_Verdaux);
6090 t->name_indx = defaux.vda_name;
6092 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6093 (Elf_External_Verdaux *) p);
6094 p += sizeof (Elf_External_Verdaux);
6096 for (n = t->deps; n != NULL; n = n->next)
6098 if (n->version_needed == NULL)
6100 /* This can happen if there was an error in the
6102 defaux.vda_name = 0;
6106 defaux.vda_name = n->version_needed->name_indx;
6107 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6110 if (n->next == NULL)
6111 defaux.vda_next = 0;
6113 defaux.vda_next = sizeof (Elf_External_Verdaux);
6115 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6116 (Elf_External_Verdaux *) p);
6117 p += sizeof (Elf_External_Verdaux);
6121 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6122 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6125 elf_tdata (output_bfd)->cverdefs = cdefs;
6128 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6130 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6133 else if (info->flags & DF_BIND_NOW)
6135 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6141 if (info->executable)
6142 info->flags_1 &= ~ (DF_1_INITFIRST
6145 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6149 /* Work out the size of the version reference section. */
6151 s = bfd_get_section_by_name (dynobj, ".gnu.version_r");
6152 BFD_ASSERT (s != NULL);
6154 struct elf_find_verdep_info sinfo;
6157 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6158 if (sinfo.vers == 0)
6160 sinfo.failed = FALSE;
6162 elf_link_hash_traverse (elf_hash_table (info),
6163 _bfd_elf_link_find_version_dependencies,
6168 if (elf_tdata (output_bfd)->verref == NULL)
6169 s->flags |= SEC_EXCLUDE;
6172 Elf_Internal_Verneed *t;
6177 /* Build the version dependency section. */
6180 for (t = elf_tdata (output_bfd)->verref;
6184 Elf_Internal_Vernaux *a;
6186 size += sizeof (Elf_External_Verneed);
6188 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6189 size += sizeof (Elf_External_Vernaux);
6193 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6194 if (s->contents == NULL)
6198 for (t = elf_tdata (output_bfd)->verref;
6203 Elf_Internal_Vernaux *a;
6207 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6210 t->vn_version = VER_NEED_CURRENT;
6212 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6213 elf_dt_name (t->vn_bfd) != NULL
6214 ? elf_dt_name (t->vn_bfd)
6215 : lbasename (t->vn_bfd->filename),
6217 if (indx == (bfd_size_type) -1)
6220 t->vn_aux = sizeof (Elf_External_Verneed);
6221 if (t->vn_nextref == NULL)
6224 t->vn_next = (sizeof (Elf_External_Verneed)
6225 + caux * sizeof (Elf_External_Vernaux));
6227 _bfd_elf_swap_verneed_out (output_bfd, t,
6228 (Elf_External_Verneed *) p);
6229 p += sizeof (Elf_External_Verneed);
6231 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6233 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6234 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6235 a->vna_nodename, FALSE);
6236 if (indx == (bfd_size_type) -1)
6239 if (a->vna_nextptr == NULL)
6242 a->vna_next = sizeof (Elf_External_Vernaux);
6244 _bfd_elf_swap_vernaux_out (output_bfd, a,
6245 (Elf_External_Vernaux *) p);
6246 p += sizeof (Elf_External_Vernaux);
6250 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6251 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6254 elf_tdata (output_bfd)->cverrefs = crefs;
6258 if ((elf_tdata (output_bfd)->cverrefs == 0
6259 && elf_tdata (output_bfd)->cverdefs == 0)
6260 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6261 §ion_sym_count) == 0)
6263 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6264 s->flags |= SEC_EXCLUDE;
6270 /* Find the first non-excluded output section. We'll use its
6271 section symbol for some emitted relocs. */
6273 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6277 for (s = output_bfd->sections; s != NULL; s = s->next)
6278 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6279 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6281 elf_hash_table (info)->text_index_section = s;
6286 /* Find two non-excluded output sections, one for code, one for data.
6287 We'll use their section symbols for some emitted relocs. */
6289 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6293 /* Data first, since setting text_index_section changes
6294 _bfd_elf_link_omit_section_dynsym. */
6295 for (s = output_bfd->sections; s != NULL; s = s->next)
6296 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6297 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6299 elf_hash_table (info)->data_index_section = s;
6303 for (s = output_bfd->sections; s != NULL; s = s->next)
6304 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6305 == (SEC_ALLOC | SEC_READONLY))
6306 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6308 elf_hash_table (info)->text_index_section = s;
6312 if (elf_hash_table (info)->text_index_section == NULL)
6313 elf_hash_table (info)->text_index_section
6314 = elf_hash_table (info)->data_index_section;
6318 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6320 const struct elf_backend_data *bed;
6322 if (!is_elf_hash_table (info->hash))
6325 bed = get_elf_backend_data (output_bfd);
6326 (*bed->elf_backend_init_index_section) (output_bfd, info);
6328 if (elf_hash_table (info)->dynamic_sections_created)
6332 bfd_size_type dynsymcount;
6333 unsigned long section_sym_count;
6334 unsigned int dtagcount;
6336 dynobj = elf_hash_table (info)->dynobj;
6338 /* Assign dynsym indicies. In a shared library we generate a
6339 section symbol for each output section, which come first.
6340 Next come all of the back-end allocated local dynamic syms,
6341 followed by the rest of the global symbols. */
6343 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6344 §ion_sym_count);
6346 /* Work out the size of the symbol version section. */
6347 s = bfd_get_section_by_name (dynobj, ".gnu.version");
6348 BFD_ASSERT (s != NULL);
6349 if (dynsymcount != 0
6350 && (s->flags & SEC_EXCLUDE) == 0)
6352 s->size = dynsymcount * sizeof (Elf_External_Versym);
6353 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6354 if (s->contents == NULL)
6357 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6361 /* Set the size of the .dynsym and .hash sections. We counted
6362 the number of dynamic symbols in elf_link_add_object_symbols.
6363 We will build the contents of .dynsym and .hash when we build
6364 the final symbol table, because until then we do not know the
6365 correct value to give the symbols. We built the .dynstr
6366 section as we went along in elf_link_add_object_symbols. */
6367 s = bfd_get_section_by_name (dynobj, ".dynsym");
6368 BFD_ASSERT (s != NULL);
6369 s->size = dynsymcount * bed->s->sizeof_sym;
6371 if (dynsymcount != 0)
6373 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6374 if (s->contents == NULL)
6377 /* The first entry in .dynsym is a dummy symbol.
6378 Clear all the section syms, in case we don't output them all. */
6379 ++section_sym_count;
6380 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6383 elf_hash_table (info)->bucketcount = 0;
6385 /* Compute the size of the hashing table. As a side effect this
6386 computes the hash values for all the names we export. */
6387 if (info->emit_hash)
6389 unsigned long int *hashcodes;
6390 struct hash_codes_info hashinf;
6392 unsigned long int nsyms;
6394 size_t hash_entry_size;
6396 /* Compute the hash values for all exported symbols. At the same
6397 time store the values in an array so that we could use them for
6399 amt = dynsymcount * sizeof (unsigned long int);
6400 hashcodes = (unsigned long int *) bfd_malloc (amt);
6401 if (hashcodes == NULL)
6403 hashinf.hashcodes = hashcodes;
6404 hashinf.error = FALSE;
6406 /* Put all hash values in HASHCODES. */
6407 elf_link_hash_traverse (elf_hash_table (info),
6408 elf_collect_hash_codes, &hashinf);
6415 nsyms = hashinf.hashcodes - hashcodes;
6417 = compute_bucket_count (info, hashcodes, nsyms, 0);
6420 if (bucketcount == 0)
6423 elf_hash_table (info)->bucketcount = bucketcount;
6425 s = bfd_get_section_by_name (dynobj, ".hash");
6426 BFD_ASSERT (s != NULL);
6427 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6428 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6429 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6430 if (s->contents == NULL)
6433 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6434 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6435 s->contents + hash_entry_size);
6438 if (info->emit_gnu_hash)
6441 unsigned char *contents;
6442 struct collect_gnu_hash_codes cinfo;
6446 memset (&cinfo, 0, sizeof (cinfo));
6448 /* Compute the hash values for all exported symbols. At the same
6449 time store the values in an array so that we could use them for
6451 amt = dynsymcount * 2 * sizeof (unsigned long int);
6452 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6453 if (cinfo.hashcodes == NULL)
6456 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6457 cinfo.min_dynindx = -1;
6458 cinfo.output_bfd = output_bfd;
6461 /* Put all hash values in HASHCODES. */
6462 elf_link_hash_traverse (elf_hash_table (info),
6463 elf_collect_gnu_hash_codes, &cinfo);
6466 free (cinfo.hashcodes);
6471 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6473 if (bucketcount == 0)
6475 free (cinfo.hashcodes);
6479 s = bfd_get_section_by_name (dynobj, ".gnu.hash");
6480 BFD_ASSERT (s != NULL);
6482 if (cinfo.nsyms == 0)
6484 /* Empty .gnu.hash section is special. */
6485 BFD_ASSERT (cinfo.min_dynindx == -1);
6486 free (cinfo.hashcodes);
6487 s->size = 5 * 4 + bed->s->arch_size / 8;
6488 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6489 if (contents == NULL)
6491 s->contents = contents;
6492 /* 1 empty bucket. */
6493 bfd_put_32 (output_bfd, 1, contents);
6494 /* SYMIDX above the special symbol 0. */
6495 bfd_put_32 (output_bfd, 1, contents + 4);
6496 /* Just one word for bitmask. */
6497 bfd_put_32 (output_bfd, 1, contents + 8);
6498 /* Only hash fn bloom filter. */
6499 bfd_put_32 (output_bfd, 0, contents + 12);
6500 /* No hashes are valid - empty bitmask. */
6501 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6502 /* No hashes in the only bucket. */
6503 bfd_put_32 (output_bfd, 0,
6504 contents + 16 + bed->s->arch_size / 8);
6508 unsigned long int maskwords, maskbitslog2, x;
6509 BFD_ASSERT (cinfo.min_dynindx != -1);
6513 while ((x >>= 1) != 0)
6515 if (maskbitslog2 < 3)
6517 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6518 maskbitslog2 = maskbitslog2 + 3;
6520 maskbitslog2 = maskbitslog2 + 2;
6521 if (bed->s->arch_size == 64)
6523 if (maskbitslog2 == 5)
6529 cinfo.mask = (1 << cinfo.shift1) - 1;
6530 cinfo.shift2 = maskbitslog2;
6531 cinfo.maskbits = 1 << maskbitslog2;
6532 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6533 amt = bucketcount * sizeof (unsigned long int) * 2;
6534 amt += maskwords * sizeof (bfd_vma);
6535 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6536 if (cinfo.bitmask == NULL)
6538 free (cinfo.hashcodes);
6542 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6543 cinfo.indx = cinfo.counts + bucketcount;
6544 cinfo.symindx = dynsymcount - cinfo.nsyms;
6545 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6547 /* Determine how often each hash bucket is used. */
6548 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6549 for (i = 0; i < cinfo.nsyms; ++i)
6550 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6552 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6553 if (cinfo.counts[i] != 0)
6555 cinfo.indx[i] = cnt;
6556 cnt += cinfo.counts[i];
6558 BFD_ASSERT (cnt == dynsymcount);
6559 cinfo.bucketcount = bucketcount;
6560 cinfo.local_indx = cinfo.min_dynindx;
6562 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6563 s->size += cinfo.maskbits / 8;
6564 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6565 if (contents == NULL)
6567 free (cinfo.bitmask);
6568 free (cinfo.hashcodes);
6572 s->contents = contents;
6573 bfd_put_32 (output_bfd, bucketcount, contents);
6574 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6575 bfd_put_32 (output_bfd, maskwords, contents + 8);
6576 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6577 contents += 16 + cinfo.maskbits / 8;
6579 for (i = 0; i < bucketcount; ++i)
6581 if (cinfo.counts[i] == 0)
6582 bfd_put_32 (output_bfd, 0, contents);
6584 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6588 cinfo.contents = contents;
6590 /* Renumber dynamic symbols, populate .gnu.hash section. */
6591 elf_link_hash_traverse (elf_hash_table (info),
6592 elf_renumber_gnu_hash_syms, &cinfo);
6594 contents = s->contents + 16;
6595 for (i = 0; i < maskwords; ++i)
6597 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6599 contents += bed->s->arch_size / 8;
6602 free (cinfo.bitmask);
6603 free (cinfo.hashcodes);
6607 s = bfd_get_section_by_name (dynobj, ".dynstr");
6608 BFD_ASSERT (s != NULL);
6610 elf_finalize_dynstr (output_bfd, info);
6612 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6614 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6615 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6622 /* Indicate that we are only retrieving symbol values from this
6626 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
6628 if (is_elf_hash_table (info->hash))
6629 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
6630 _bfd_generic_link_just_syms (sec, info);
6633 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6636 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6639 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
6640 sec->sec_info_type = ELF_INFO_TYPE_NONE;
6643 /* Finish SHF_MERGE section merging. */
6646 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6651 if (!is_elf_hash_table (info->hash))
6654 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6655 if ((ibfd->flags & DYNAMIC) == 0)
6656 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6657 if ((sec->flags & SEC_MERGE) != 0
6658 && !bfd_is_abs_section (sec->output_section))
6660 struct bfd_elf_section_data *secdata;
6662 secdata = elf_section_data (sec);
6663 if (! _bfd_add_merge_section (abfd,
6664 &elf_hash_table (info)->merge_info,
6665 sec, &secdata->sec_info))
6667 else if (secdata->sec_info)
6668 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
6671 if (elf_hash_table (info)->merge_info != NULL)
6672 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6673 merge_sections_remove_hook);
6677 /* Create an entry in an ELF linker hash table. */
6679 struct bfd_hash_entry *
6680 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6681 struct bfd_hash_table *table,
6684 /* Allocate the structure if it has not already been allocated by a
6688 entry = (struct bfd_hash_entry *)
6689 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6694 /* Call the allocation method of the superclass. */
6695 entry = _bfd_link_hash_newfunc (entry, table, string);
6698 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6699 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6701 /* Set local fields. */
6704 ret->got = htab->init_got_refcount;
6705 ret->plt = htab->init_plt_refcount;
6706 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6707 - offsetof (struct elf_link_hash_entry, size)));
6708 /* Assume that we have been called by a non-ELF symbol reader.
6709 This flag is then reset by the code which reads an ELF input
6710 file. This ensures that a symbol created by a non-ELF symbol
6711 reader will have the flag set correctly. */
6718 /* Copy data from an indirect symbol to its direct symbol, hiding the
6719 old indirect symbol. Also used for copying flags to a weakdef. */
6722 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6723 struct elf_link_hash_entry *dir,
6724 struct elf_link_hash_entry *ind)
6726 struct elf_link_hash_table *htab;
6728 /* Copy down any references that we may have already seen to the
6729 symbol which just became indirect. */
6731 dir->ref_dynamic |= ind->ref_dynamic;
6732 dir->ref_regular |= ind->ref_regular;
6733 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6734 dir->non_got_ref |= ind->non_got_ref;
6735 dir->needs_plt |= ind->needs_plt;
6736 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6738 if (ind->root.type != bfd_link_hash_indirect)
6741 /* Copy over the global and procedure linkage table refcount entries.
6742 These may have been already set up by a check_relocs routine. */
6743 htab = elf_hash_table (info);
6744 if (ind->got.refcount > htab->init_got_refcount.refcount)
6746 if (dir->got.refcount < 0)
6747 dir->got.refcount = 0;
6748 dir->got.refcount += ind->got.refcount;
6749 ind->got.refcount = htab->init_got_refcount.refcount;
6752 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6754 if (dir->plt.refcount < 0)
6755 dir->plt.refcount = 0;
6756 dir->plt.refcount += ind->plt.refcount;
6757 ind->plt.refcount = htab->init_plt_refcount.refcount;
6760 if (ind->dynindx != -1)
6762 if (dir->dynindx != -1)
6763 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6764 dir->dynindx = ind->dynindx;
6765 dir->dynstr_index = ind->dynstr_index;
6767 ind->dynstr_index = 0;
6772 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6773 struct elf_link_hash_entry *h,
6774 bfd_boolean force_local)
6776 /* STT_GNU_IFUNC symbol must go through PLT. */
6777 if (h->type != STT_GNU_IFUNC)
6779 h->plt = elf_hash_table (info)->init_plt_offset;
6784 h->forced_local = 1;
6785 if (h->dynindx != -1)
6788 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6794 /* Initialize an ELF linker hash table. */
6797 _bfd_elf_link_hash_table_init
6798 (struct elf_link_hash_table *table,
6800 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6801 struct bfd_hash_table *,
6803 unsigned int entsize,
6804 enum elf_target_id target_id)
6807 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6809 memset (table, 0, sizeof * table);
6810 table->init_got_refcount.refcount = can_refcount - 1;
6811 table->init_plt_refcount.refcount = can_refcount - 1;
6812 table->init_got_offset.offset = -(bfd_vma) 1;
6813 table->init_plt_offset.offset = -(bfd_vma) 1;
6814 /* The first dynamic symbol is a dummy. */
6815 table->dynsymcount = 1;
6817 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6819 table->root.type = bfd_link_elf_hash_table;
6820 table->hash_table_id = target_id;
6825 /* Create an ELF linker hash table. */
6827 struct bfd_link_hash_table *
6828 _bfd_elf_link_hash_table_create (bfd *abfd)
6830 struct elf_link_hash_table *ret;
6831 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6833 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
6837 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6838 sizeof (struct elf_link_hash_entry),
6848 /* This is a hook for the ELF emulation code in the generic linker to
6849 tell the backend linker what file name to use for the DT_NEEDED
6850 entry for a dynamic object. */
6853 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6855 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6856 && bfd_get_format (abfd) == bfd_object)
6857 elf_dt_name (abfd) = name;
6861 bfd_elf_get_dyn_lib_class (bfd *abfd)
6864 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6865 && bfd_get_format (abfd) == bfd_object)
6866 lib_class = elf_dyn_lib_class (abfd);
6873 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6875 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6876 && bfd_get_format (abfd) == bfd_object)
6877 elf_dyn_lib_class (abfd) = lib_class;
6880 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6881 the linker ELF emulation code. */
6883 struct bfd_link_needed_list *
6884 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6885 struct bfd_link_info *info)
6887 if (! is_elf_hash_table (info->hash))
6889 return elf_hash_table (info)->needed;
6892 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6893 hook for the linker ELF emulation code. */
6895 struct bfd_link_needed_list *
6896 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6897 struct bfd_link_info *info)
6899 if (! is_elf_hash_table (info->hash))
6901 return elf_hash_table (info)->runpath;
6904 /* Get the name actually used for a dynamic object for a link. This
6905 is the SONAME entry if there is one. Otherwise, it is the string
6906 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
6909 bfd_elf_get_dt_soname (bfd *abfd)
6911 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6912 && bfd_get_format (abfd) == bfd_object)
6913 return elf_dt_name (abfd);
6917 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
6918 the ELF linker emulation code. */
6921 bfd_elf_get_bfd_needed_list (bfd *abfd,
6922 struct bfd_link_needed_list **pneeded)
6925 bfd_byte *dynbuf = NULL;
6926 unsigned int elfsec;
6927 unsigned long shlink;
6928 bfd_byte *extdyn, *extdynend;
6930 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
6934 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
6935 || bfd_get_format (abfd) != bfd_object)
6938 s = bfd_get_section_by_name (abfd, ".dynamic");
6939 if (s == NULL || s->size == 0)
6942 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
6945 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
6946 if (elfsec == SHN_BAD)
6949 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
6951 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
6952 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
6955 extdynend = extdyn + s->size;
6956 for (; extdyn < extdynend; extdyn += extdynsize)
6958 Elf_Internal_Dyn dyn;
6960 (*swap_dyn_in) (abfd, extdyn, &dyn);
6962 if (dyn.d_tag == DT_NULL)
6965 if (dyn.d_tag == DT_NEEDED)
6968 struct bfd_link_needed_list *l;
6969 unsigned int tagv = dyn.d_un.d_val;
6972 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
6977 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
6998 struct elf_symbuf_symbol
7000 unsigned long st_name; /* Symbol name, index in string tbl */
7001 unsigned char st_info; /* Type and binding attributes */
7002 unsigned char st_other; /* Visibilty, and target specific */
7005 struct elf_symbuf_head
7007 struct elf_symbuf_symbol *ssym;
7008 bfd_size_type count;
7009 unsigned int st_shndx;
7016 Elf_Internal_Sym *isym;
7017 struct elf_symbuf_symbol *ssym;
7022 /* Sort references to symbols by ascending section number. */
7025 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7027 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7028 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7030 return s1->st_shndx - s2->st_shndx;
7034 elf_sym_name_compare (const void *arg1, const void *arg2)
7036 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7037 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7038 return strcmp (s1->name, s2->name);
7041 static struct elf_symbuf_head *
7042 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7044 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7045 struct elf_symbuf_symbol *ssym;
7046 struct elf_symbuf_head *ssymbuf, *ssymhead;
7047 bfd_size_type i, shndx_count, total_size;
7049 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7053 for (ind = indbuf, i = 0; i < symcount; i++)
7054 if (isymbuf[i].st_shndx != SHN_UNDEF)
7055 *ind++ = &isymbuf[i];
7058 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7059 elf_sort_elf_symbol);
7062 if (indbufend > indbuf)
7063 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7064 if (ind[0]->st_shndx != ind[1]->st_shndx)
7067 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7068 + (indbufend - indbuf) * sizeof (*ssym));
7069 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7070 if (ssymbuf == NULL)
7076 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7077 ssymbuf->ssym = NULL;
7078 ssymbuf->count = shndx_count;
7079 ssymbuf->st_shndx = 0;
7080 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7082 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7085 ssymhead->ssym = ssym;
7086 ssymhead->count = 0;
7087 ssymhead->st_shndx = (*ind)->st_shndx;
7089 ssym->st_name = (*ind)->st_name;
7090 ssym->st_info = (*ind)->st_info;
7091 ssym->st_other = (*ind)->st_other;
7094 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7095 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7102 /* Check if 2 sections define the same set of local and global
7106 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7107 struct bfd_link_info *info)
7110 const struct elf_backend_data *bed1, *bed2;
7111 Elf_Internal_Shdr *hdr1, *hdr2;
7112 bfd_size_type symcount1, symcount2;
7113 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7114 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7115 Elf_Internal_Sym *isym, *isymend;
7116 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7117 bfd_size_type count1, count2, i;
7118 unsigned int shndx1, shndx2;
7124 /* Both sections have to be in ELF. */
7125 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7126 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7129 if (elf_section_type (sec1) != elf_section_type (sec2))
7132 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7133 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7134 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7137 bed1 = get_elf_backend_data (bfd1);
7138 bed2 = get_elf_backend_data (bfd2);
7139 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7140 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7141 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7142 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7144 if (symcount1 == 0 || symcount2 == 0)
7150 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7151 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7153 if (ssymbuf1 == NULL)
7155 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7157 if (isymbuf1 == NULL)
7160 if (!info->reduce_memory_overheads)
7161 elf_tdata (bfd1)->symbuf = ssymbuf1
7162 = elf_create_symbuf (symcount1, isymbuf1);
7165 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7167 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7169 if (isymbuf2 == NULL)
7172 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7173 elf_tdata (bfd2)->symbuf = ssymbuf2
7174 = elf_create_symbuf (symcount2, isymbuf2);
7177 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7179 /* Optimized faster version. */
7180 bfd_size_type lo, hi, mid;
7181 struct elf_symbol *symp;
7182 struct elf_symbuf_symbol *ssym, *ssymend;
7185 hi = ssymbuf1->count;
7190 mid = (lo + hi) / 2;
7191 if (shndx1 < ssymbuf1[mid].st_shndx)
7193 else if (shndx1 > ssymbuf1[mid].st_shndx)
7197 count1 = ssymbuf1[mid].count;
7204 hi = ssymbuf2->count;
7209 mid = (lo + hi) / 2;
7210 if (shndx2 < ssymbuf2[mid].st_shndx)
7212 else if (shndx2 > ssymbuf2[mid].st_shndx)
7216 count2 = ssymbuf2[mid].count;
7222 if (count1 == 0 || count2 == 0 || count1 != count2)
7225 symtable1 = (struct elf_symbol *)
7226 bfd_malloc (count1 * sizeof (struct elf_symbol));
7227 symtable2 = (struct elf_symbol *)
7228 bfd_malloc (count2 * sizeof (struct elf_symbol));
7229 if (symtable1 == NULL || symtable2 == NULL)
7233 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7234 ssym < ssymend; ssym++, symp++)
7236 symp->u.ssym = ssym;
7237 symp->name = bfd_elf_string_from_elf_section (bfd1,
7243 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7244 ssym < ssymend; ssym++, symp++)
7246 symp->u.ssym = ssym;
7247 symp->name = bfd_elf_string_from_elf_section (bfd2,
7252 /* Sort symbol by name. */
7253 qsort (symtable1, count1, sizeof (struct elf_symbol),
7254 elf_sym_name_compare);
7255 qsort (symtable2, count1, sizeof (struct elf_symbol),
7256 elf_sym_name_compare);
7258 for (i = 0; i < count1; i++)
7259 /* Two symbols must have the same binding, type and name. */
7260 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7261 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7262 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7269 symtable1 = (struct elf_symbol *)
7270 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7271 symtable2 = (struct elf_symbol *)
7272 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7273 if (symtable1 == NULL || symtable2 == NULL)
7276 /* Count definitions in the section. */
7278 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7279 if (isym->st_shndx == shndx1)
7280 symtable1[count1++].u.isym = isym;
7283 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7284 if (isym->st_shndx == shndx2)
7285 symtable2[count2++].u.isym = isym;
7287 if (count1 == 0 || count2 == 0 || count1 != count2)
7290 for (i = 0; i < count1; i++)
7292 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7293 symtable1[i].u.isym->st_name);
7295 for (i = 0; i < count2; i++)
7297 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7298 symtable2[i].u.isym->st_name);
7300 /* Sort symbol by name. */
7301 qsort (symtable1, count1, sizeof (struct elf_symbol),
7302 elf_sym_name_compare);
7303 qsort (symtable2, count1, sizeof (struct elf_symbol),
7304 elf_sym_name_compare);
7306 for (i = 0; i < count1; i++)
7307 /* Two symbols must have the same binding, type and name. */
7308 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7309 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7310 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7328 /* Return TRUE if 2 section types are compatible. */
7331 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7332 bfd *bbfd, const asection *bsec)
7336 || abfd->xvec->flavour != bfd_target_elf_flavour
7337 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7340 return elf_section_type (asec) == elf_section_type (bsec);
7343 /* Final phase of ELF linker. */
7345 /* A structure we use to avoid passing large numbers of arguments. */
7347 struct elf_final_link_info
7349 /* General link information. */
7350 struct bfd_link_info *info;
7353 /* Symbol string table. */
7354 struct bfd_strtab_hash *symstrtab;
7355 /* .dynsym section. */
7356 asection *dynsym_sec;
7357 /* .hash section. */
7359 /* symbol version section (.gnu.version). */
7360 asection *symver_sec;
7361 /* Buffer large enough to hold contents of any section. */
7363 /* Buffer large enough to hold external relocs of any section. */
7364 void *external_relocs;
7365 /* Buffer large enough to hold internal relocs of any section. */
7366 Elf_Internal_Rela *internal_relocs;
7367 /* Buffer large enough to hold external local symbols of any input
7369 bfd_byte *external_syms;
7370 /* And a buffer for symbol section indices. */
7371 Elf_External_Sym_Shndx *locsym_shndx;
7372 /* Buffer large enough to hold internal local symbols of any input
7374 Elf_Internal_Sym *internal_syms;
7375 /* Array large enough to hold a symbol index for each local symbol
7376 of any input BFD. */
7378 /* Array large enough to hold a section pointer for each local
7379 symbol of any input BFD. */
7380 asection **sections;
7381 /* Buffer to hold swapped out symbols. */
7383 /* And one for symbol section indices. */
7384 Elf_External_Sym_Shndx *symshndxbuf;
7385 /* Number of swapped out symbols in buffer. */
7386 size_t symbuf_count;
7387 /* Number of symbols which fit in symbuf. */
7389 /* And same for symshndxbuf. */
7390 size_t shndxbuf_size;
7393 /* This struct is used to pass information to elf_link_output_extsym. */
7395 struct elf_outext_info
7398 bfd_boolean localsyms;
7399 struct elf_final_link_info *finfo;
7403 /* Support for evaluating a complex relocation.
7405 Complex relocations are generalized, self-describing relocations. The
7406 implementation of them consists of two parts: complex symbols, and the
7407 relocations themselves.
7409 The relocations are use a reserved elf-wide relocation type code (R_RELC
7410 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7411 information (start bit, end bit, word width, etc) into the addend. This
7412 information is extracted from CGEN-generated operand tables within gas.
7414 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7415 internal) representing prefix-notation expressions, including but not
7416 limited to those sorts of expressions normally encoded as addends in the
7417 addend field. The symbol mangling format is:
7420 | <unary-operator> ':' <node>
7421 | <binary-operator> ':' <node> ':' <node>
7424 <literal> := 's' <digits=N> ':' <N character symbol name>
7425 | 'S' <digits=N> ':' <N character section name>
7429 <binary-operator> := as in C
7430 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7433 set_symbol_value (bfd *bfd_with_globals,
7434 Elf_Internal_Sym *isymbuf,
7439 struct elf_link_hash_entry **sym_hashes;
7440 struct elf_link_hash_entry *h;
7441 size_t extsymoff = locsymcount;
7443 if (symidx < locsymcount)
7445 Elf_Internal_Sym *sym;
7447 sym = isymbuf + symidx;
7448 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7450 /* It is a local symbol: move it to the
7451 "absolute" section and give it a value. */
7452 sym->st_shndx = SHN_ABS;
7453 sym->st_value = val;
7456 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7460 /* It is a global symbol: set its link type
7461 to "defined" and give it a value. */
7463 sym_hashes = elf_sym_hashes (bfd_with_globals);
7464 h = sym_hashes [symidx - extsymoff];
7465 while (h->root.type == bfd_link_hash_indirect
7466 || h->root.type == bfd_link_hash_warning)
7467 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7468 h->root.type = bfd_link_hash_defined;
7469 h->root.u.def.value = val;
7470 h->root.u.def.section = bfd_abs_section_ptr;
7474 resolve_symbol (const char *name,
7476 struct elf_final_link_info *finfo,
7478 Elf_Internal_Sym *isymbuf,
7481 Elf_Internal_Sym *sym;
7482 struct bfd_link_hash_entry *global_entry;
7483 const char *candidate = NULL;
7484 Elf_Internal_Shdr *symtab_hdr;
7487 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7489 for (i = 0; i < locsymcount; ++ i)
7493 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7496 candidate = bfd_elf_string_from_elf_section (input_bfd,
7497 symtab_hdr->sh_link,
7500 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7501 name, candidate, (unsigned long) sym->st_value);
7503 if (candidate && strcmp (candidate, name) == 0)
7505 asection *sec = finfo->sections [i];
7507 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7508 *result += sec->output_offset + sec->output_section->vma;
7510 printf ("Found symbol with value %8.8lx\n",
7511 (unsigned long) *result);
7517 /* Hmm, haven't found it yet. perhaps it is a global. */
7518 global_entry = bfd_link_hash_lookup (finfo->info->hash, name,
7519 FALSE, FALSE, TRUE);
7523 if (global_entry->type == bfd_link_hash_defined
7524 || global_entry->type == bfd_link_hash_defweak)
7526 *result = (global_entry->u.def.value
7527 + global_entry->u.def.section->output_section->vma
7528 + global_entry->u.def.section->output_offset);
7530 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7531 global_entry->root.string, (unsigned long) *result);
7540 resolve_section (const char *name,
7547 for (curr = sections; curr; curr = curr->next)
7548 if (strcmp (curr->name, name) == 0)
7550 *result = curr->vma;
7554 /* Hmm. still haven't found it. try pseudo-section names. */
7555 for (curr = sections; curr; curr = curr->next)
7557 len = strlen (curr->name);
7558 if (len > strlen (name))
7561 if (strncmp (curr->name, name, len) == 0)
7563 if (strncmp (".end", name + len, 4) == 0)
7565 *result = curr->vma + curr->size;
7569 /* Insert more pseudo-section names here, if you like. */
7577 undefined_reference (const char *reftype, const char *name)
7579 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7584 eval_symbol (bfd_vma *result,
7587 struct elf_final_link_info *finfo,
7589 Elf_Internal_Sym *isymbuf,
7598 const char *sym = *symp;
7600 bfd_boolean symbol_is_section = FALSE;
7605 if (len < 1 || len > sizeof (symbuf))
7607 bfd_set_error (bfd_error_invalid_operation);
7620 *result = strtoul (sym, (char **) symp, 16);
7624 symbol_is_section = TRUE;
7627 symlen = strtol (sym, (char **) symp, 10);
7628 sym = *symp + 1; /* Skip the trailing ':'. */
7630 if (symend < sym || symlen + 1 > sizeof (symbuf))
7632 bfd_set_error (bfd_error_invalid_operation);
7636 memcpy (symbuf, sym, symlen);
7637 symbuf[symlen] = '\0';
7638 *symp = sym + symlen;
7640 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7641 the symbol as a section, or vice-versa. so we're pretty liberal in our
7642 interpretation here; section means "try section first", not "must be a
7643 section", and likewise with symbol. */
7645 if (symbol_is_section)
7647 if (!resolve_section (symbuf, finfo->output_bfd->sections, result)
7648 && !resolve_symbol (symbuf, input_bfd, finfo, result,
7649 isymbuf, locsymcount))
7651 undefined_reference ("section", symbuf);
7657 if (!resolve_symbol (symbuf, input_bfd, finfo, result,
7658 isymbuf, locsymcount)
7659 && !resolve_section (symbuf, finfo->output_bfd->sections,
7662 undefined_reference ("symbol", symbuf);
7669 /* All that remains are operators. */
7671 #define UNARY_OP(op) \
7672 if (strncmp (sym, #op, strlen (#op)) == 0) \
7674 sym += strlen (#op); \
7678 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7679 isymbuf, locsymcount, signed_p)) \
7682 *result = op ((bfd_signed_vma) a); \
7688 #define BINARY_OP(op) \
7689 if (strncmp (sym, #op, strlen (#op)) == 0) \
7691 sym += strlen (#op); \
7695 if (!eval_symbol (&a, symp, input_bfd, finfo, dot, \
7696 isymbuf, locsymcount, signed_p)) \
7699 if (!eval_symbol (&b, symp, input_bfd, finfo, dot, \
7700 isymbuf, locsymcount, signed_p)) \
7703 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7733 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7734 bfd_set_error (bfd_error_invalid_operation);
7740 put_value (bfd_vma size,
7741 unsigned long chunksz,
7746 location += (size - chunksz);
7748 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
7756 bfd_put_8 (input_bfd, x, location);
7759 bfd_put_16 (input_bfd, x, location);
7762 bfd_put_32 (input_bfd, x, location);
7766 bfd_put_64 (input_bfd, x, location);
7776 get_value (bfd_vma size,
7777 unsigned long chunksz,
7783 for (; size; size -= chunksz, location += chunksz)
7791 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location);
7794 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location);
7797 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location);
7801 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location);
7812 decode_complex_addend (unsigned long *start, /* in bits */
7813 unsigned long *oplen, /* in bits */
7814 unsigned long *len, /* in bits */
7815 unsigned long *wordsz, /* in bytes */
7816 unsigned long *chunksz, /* in bytes */
7817 unsigned long *lsb0_p,
7818 unsigned long *signed_p,
7819 unsigned long *trunc_p,
7820 unsigned long encoded)
7822 * start = encoded & 0x3F;
7823 * len = (encoded >> 6) & 0x3F;
7824 * oplen = (encoded >> 12) & 0x3F;
7825 * wordsz = (encoded >> 18) & 0xF;
7826 * chunksz = (encoded >> 22) & 0xF;
7827 * lsb0_p = (encoded >> 27) & 1;
7828 * signed_p = (encoded >> 28) & 1;
7829 * trunc_p = (encoded >> 29) & 1;
7832 bfd_reloc_status_type
7833 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7834 asection *input_section ATTRIBUTE_UNUSED,
7836 Elf_Internal_Rela *rel,
7839 bfd_vma shift, x, mask;
7840 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7841 bfd_reloc_status_type r;
7843 /* Perform this reloc, since it is complex.
7844 (this is not to say that it necessarily refers to a complex
7845 symbol; merely that it is a self-describing CGEN based reloc.
7846 i.e. the addend has the complete reloc information (bit start, end,
7847 word size, etc) encoded within it.). */
7849 decode_complex_addend (&start, &oplen, &len, &wordsz,
7850 &chunksz, &lsb0_p, &signed_p,
7851 &trunc_p, rel->r_addend);
7853 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7856 shift = (start + 1) - len;
7858 shift = (8 * wordsz) - (start + len);
7860 /* FIXME: octets_per_byte. */
7861 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7864 printf ("Doing complex reloc: "
7865 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7866 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7867 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7868 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7869 oplen, (unsigned long) x, (unsigned long) mask,
7870 (unsigned long) relocation);
7875 /* Now do an overflow check. */
7876 r = bfd_check_overflow ((signed_p
7877 ? complain_overflow_signed
7878 : complain_overflow_unsigned),
7879 len, 0, (8 * wordsz),
7883 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
7886 printf (" relocation: %8.8lx\n"
7887 " shifted mask: %8.8lx\n"
7888 " shifted/masked reloc: %8.8lx\n"
7889 " result: %8.8lx\n",
7890 (unsigned long) relocation, (unsigned long) (mask << shift),
7891 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
7893 /* FIXME: octets_per_byte. */
7894 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
7898 /* When performing a relocatable link, the input relocations are
7899 preserved. But, if they reference global symbols, the indices
7900 referenced must be updated. Update all the relocations found in
7904 elf_link_adjust_relocs (bfd *abfd,
7905 struct bfd_elf_section_reloc_data *reldata)
7908 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7910 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
7911 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
7912 bfd_vma r_type_mask;
7914 unsigned int count = reldata->count;
7915 struct elf_link_hash_entry **rel_hash = reldata->hashes;
7917 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
7919 swap_in = bed->s->swap_reloc_in;
7920 swap_out = bed->s->swap_reloc_out;
7922 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
7924 swap_in = bed->s->swap_reloca_in;
7925 swap_out = bed->s->swap_reloca_out;
7930 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
7933 if (bed->s->arch_size == 32)
7940 r_type_mask = 0xffffffff;
7944 erela = reldata->hdr->contents;
7945 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
7947 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
7950 if (*rel_hash == NULL)
7953 BFD_ASSERT ((*rel_hash)->indx >= 0);
7955 (*swap_in) (abfd, erela, irela);
7956 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
7957 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
7958 | (irela[j].r_info & r_type_mask));
7959 (*swap_out) (abfd, irela, erela);
7963 struct elf_link_sort_rela
7969 enum elf_reloc_type_class type;
7970 /* We use this as an array of size int_rels_per_ext_rel. */
7971 Elf_Internal_Rela rela[1];
7975 elf_link_sort_cmp1 (const void *A, const void *B)
7977 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
7978 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
7979 int relativea, relativeb;
7981 relativea = a->type == reloc_class_relative;
7982 relativeb = b->type == reloc_class_relative;
7984 if (relativea < relativeb)
7986 if (relativea > relativeb)
7988 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
7990 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
7992 if (a->rela->r_offset < b->rela->r_offset)
7994 if (a->rela->r_offset > b->rela->r_offset)
8000 elf_link_sort_cmp2 (const void *A, const void *B)
8002 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8003 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8006 if (a->u.offset < b->u.offset)
8008 if (a->u.offset > b->u.offset)
8010 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt);
8011 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt);
8016 if (a->rela->r_offset < b->rela->r_offset)
8018 if (a->rela->r_offset > b->rela->r_offset)
8024 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8026 asection *dynamic_relocs;
8029 bfd_size_type count, size;
8030 size_t i, ret, sort_elt, ext_size;
8031 bfd_byte *sort, *s_non_relative, *p;
8032 struct elf_link_sort_rela *sq;
8033 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8034 int i2e = bed->s->int_rels_per_ext_rel;
8035 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8036 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8037 struct bfd_link_order *lo;
8039 bfd_boolean use_rela;
8041 /* Find a dynamic reloc section. */
8042 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8043 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8044 if (rela_dyn != NULL && rela_dyn->size > 0
8045 && rel_dyn != NULL && rel_dyn->size > 0)
8047 bfd_boolean use_rela_initialised = FALSE;
8049 /* This is just here to stop gcc from complaining.
8050 It's initialization checking code is not perfect. */
8053 /* Both sections are present. Examine the sizes
8054 of the indirect sections to help us choose. */
8055 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8056 if (lo->type == bfd_indirect_link_order)
8058 asection *o = lo->u.indirect.section;
8060 if ((o->size % bed->s->sizeof_rela) == 0)
8062 if ((o->size % bed->s->sizeof_rel) == 0)
8063 /* Section size is divisible by both rel and rela sizes.
8064 It is of no help to us. */
8068 /* Section size is only divisible by rela. */
8069 if (use_rela_initialised && (use_rela == FALSE))
8072 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8073 bfd_set_error (bfd_error_invalid_operation);
8079 use_rela_initialised = TRUE;
8083 else if ((o->size % bed->s->sizeof_rel) == 0)
8085 /* Section size is only divisible by rel. */
8086 if (use_rela_initialised && (use_rela == TRUE))
8089 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8090 bfd_set_error (bfd_error_invalid_operation);
8096 use_rela_initialised = TRUE;
8101 /* The section size is not divisible by either - something is wrong. */
8103 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8104 bfd_set_error (bfd_error_invalid_operation);
8109 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8110 if (lo->type == bfd_indirect_link_order)
8112 asection *o = lo->u.indirect.section;
8114 if ((o->size % bed->s->sizeof_rela) == 0)
8116 if ((o->size % bed->s->sizeof_rel) == 0)
8117 /* Section size is divisible by both rel and rela sizes.
8118 It is of no help to us. */
8122 /* Section size is only divisible by rela. */
8123 if (use_rela_initialised && (use_rela == FALSE))
8126 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8127 bfd_set_error (bfd_error_invalid_operation);
8133 use_rela_initialised = TRUE;
8137 else if ((o->size % bed->s->sizeof_rel) == 0)
8139 /* Section size is only divisible by rel. */
8140 if (use_rela_initialised && (use_rela == TRUE))
8143 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8144 bfd_set_error (bfd_error_invalid_operation);
8150 use_rela_initialised = TRUE;
8155 /* The section size is not divisible by either - something is wrong. */
8157 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8158 bfd_set_error (bfd_error_invalid_operation);
8163 if (! use_rela_initialised)
8167 else if (rela_dyn != NULL && rela_dyn->size > 0)
8169 else if (rel_dyn != NULL && rel_dyn->size > 0)
8176 dynamic_relocs = rela_dyn;
8177 ext_size = bed->s->sizeof_rela;
8178 swap_in = bed->s->swap_reloca_in;
8179 swap_out = bed->s->swap_reloca_out;
8183 dynamic_relocs = rel_dyn;
8184 ext_size = bed->s->sizeof_rel;
8185 swap_in = bed->s->swap_reloc_in;
8186 swap_out = bed->s->swap_reloc_out;
8190 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8191 if (lo->type == bfd_indirect_link_order)
8192 size += lo->u.indirect.section->size;
8194 if (size != dynamic_relocs->size)
8197 sort_elt = (sizeof (struct elf_link_sort_rela)
8198 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8200 count = dynamic_relocs->size / ext_size;
8203 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8207 (*info->callbacks->warning)
8208 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8212 if (bed->s->arch_size == 32)
8213 r_sym_mask = ~(bfd_vma) 0xff;
8215 r_sym_mask = ~(bfd_vma) 0xffffffff;
8217 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8218 if (lo->type == bfd_indirect_link_order)
8220 bfd_byte *erel, *erelend;
8221 asection *o = lo->u.indirect.section;
8223 if (o->contents == NULL && o->size != 0)
8225 /* This is a reloc section that is being handled as a normal
8226 section. See bfd_section_from_shdr. We can't combine
8227 relocs in this case. */
8232 erelend = o->contents + o->size;
8233 /* FIXME: octets_per_byte. */
8234 p = sort + o->output_offset / ext_size * sort_elt;
8236 while (erel < erelend)
8238 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8240 (*swap_in) (abfd, erel, s->rela);
8241 s->type = (*bed->elf_backend_reloc_type_class) (s->rela);
8242 s->u.sym_mask = r_sym_mask;
8248 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8250 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8252 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8253 if (s->type != reloc_class_relative)
8259 sq = (struct elf_link_sort_rela *) s_non_relative;
8260 for (; i < count; i++, p += sort_elt)
8262 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8263 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8265 sp->u.offset = sq->rela->r_offset;
8268 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8270 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8271 if (lo->type == bfd_indirect_link_order)
8273 bfd_byte *erel, *erelend;
8274 asection *o = lo->u.indirect.section;
8277 erelend = o->contents + o->size;
8278 /* FIXME: octets_per_byte. */
8279 p = sort + o->output_offset / ext_size * sort_elt;
8280 while (erel < erelend)
8282 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8283 (*swap_out) (abfd, s->rela, erel);
8290 *psec = dynamic_relocs;
8294 /* Flush the output symbols to the file. */
8297 elf_link_flush_output_syms (struct elf_final_link_info *finfo,
8298 const struct elf_backend_data *bed)
8300 if (finfo->symbuf_count > 0)
8302 Elf_Internal_Shdr *hdr;
8306 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr;
8307 pos = hdr->sh_offset + hdr->sh_size;
8308 amt = finfo->symbuf_count * bed->s->sizeof_sym;
8309 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0
8310 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt)
8313 hdr->sh_size += amt;
8314 finfo->symbuf_count = 0;
8320 /* Add a symbol to the output symbol table. */
8323 elf_link_output_sym (struct elf_final_link_info *finfo,
8325 Elf_Internal_Sym *elfsym,
8326 asection *input_sec,
8327 struct elf_link_hash_entry *h)
8330 Elf_External_Sym_Shndx *destshndx;
8331 int (*output_symbol_hook)
8332 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8333 struct elf_link_hash_entry *);
8334 const struct elf_backend_data *bed;
8336 bed = get_elf_backend_data (finfo->output_bfd);
8337 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8338 if (output_symbol_hook != NULL)
8340 int ret = (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h);
8345 if (name == NULL || *name == '\0')
8346 elfsym->st_name = 0;
8347 else if (input_sec->flags & SEC_EXCLUDE)
8348 elfsym->st_name = 0;
8351 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab,
8353 if (elfsym->st_name == (unsigned long) -1)
8357 if (finfo->symbuf_count >= finfo->symbuf_size)
8359 if (! elf_link_flush_output_syms (finfo, bed))
8363 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym;
8364 destshndx = finfo->symshndxbuf;
8365 if (destshndx != NULL)
8367 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size)
8371 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx);
8372 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx,
8374 if (destshndx == NULL)
8376 finfo->symshndxbuf = destshndx;
8377 memset ((char *) destshndx + amt, 0, amt);
8378 finfo->shndxbuf_size *= 2;
8380 destshndx += bfd_get_symcount (finfo->output_bfd);
8383 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx);
8384 finfo->symbuf_count += 1;
8385 bfd_get_symcount (finfo->output_bfd) += 1;
8390 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8393 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8395 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8396 && sym->st_shndx < SHN_LORESERVE)
8398 /* The gABI doesn't support dynamic symbols in output sections
8400 (*_bfd_error_handler)
8401 (_("%B: Too many sections: %d (>= %d)"),
8402 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8403 bfd_set_error (bfd_error_nonrepresentable_section);
8409 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8410 allowing an unsatisfied unversioned symbol in the DSO to match a
8411 versioned symbol that would normally require an explicit version.
8412 We also handle the case that a DSO references a hidden symbol
8413 which may be satisfied by a versioned symbol in another DSO. */
8416 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8417 const struct elf_backend_data *bed,
8418 struct elf_link_hash_entry *h)
8421 struct elf_link_loaded_list *loaded;
8423 if (!is_elf_hash_table (info->hash))
8426 switch (h->root.type)
8432 case bfd_link_hash_undefined:
8433 case bfd_link_hash_undefweak:
8434 abfd = h->root.u.undef.abfd;
8435 if ((abfd->flags & DYNAMIC) == 0
8436 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8440 case bfd_link_hash_defined:
8441 case bfd_link_hash_defweak:
8442 abfd = h->root.u.def.section->owner;
8445 case bfd_link_hash_common:
8446 abfd = h->root.u.c.p->section->owner;
8449 BFD_ASSERT (abfd != NULL);
8451 for (loaded = elf_hash_table (info)->loaded;
8453 loaded = loaded->next)
8456 Elf_Internal_Shdr *hdr;
8457 bfd_size_type symcount;
8458 bfd_size_type extsymcount;
8459 bfd_size_type extsymoff;
8460 Elf_Internal_Shdr *versymhdr;
8461 Elf_Internal_Sym *isym;
8462 Elf_Internal_Sym *isymend;
8463 Elf_Internal_Sym *isymbuf;
8464 Elf_External_Versym *ever;
8465 Elf_External_Versym *extversym;
8467 input = loaded->abfd;
8469 /* We check each DSO for a possible hidden versioned definition. */
8471 || (input->flags & DYNAMIC) == 0
8472 || elf_dynversym (input) == 0)
8475 hdr = &elf_tdata (input)->dynsymtab_hdr;
8477 symcount = hdr->sh_size / bed->s->sizeof_sym;
8478 if (elf_bad_symtab (input))
8480 extsymcount = symcount;
8485 extsymcount = symcount - hdr->sh_info;
8486 extsymoff = hdr->sh_info;
8489 if (extsymcount == 0)
8492 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8494 if (isymbuf == NULL)
8497 /* Read in any version definitions. */
8498 versymhdr = &elf_tdata (input)->dynversym_hdr;
8499 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8500 if (extversym == NULL)
8503 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8504 || (bfd_bread (extversym, versymhdr->sh_size, input)
8505 != versymhdr->sh_size))
8513 ever = extversym + extsymoff;
8514 isymend = isymbuf + extsymcount;
8515 for (isym = isymbuf; isym < isymend; isym++, ever++)
8518 Elf_Internal_Versym iver;
8519 unsigned short version_index;
8521 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8522 || isym->st_shndx == SHN_UNDEF)
8525 name = bfd_elf_string_from_elf_section (input,
8528 if (strcmp (name, h->root.root.string) != 0)
8531 _bfd_elf_swap_versym_in (input, ever, &iver);
8533 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8535 && h->forced_local))
8537 /* If we have a non-hidden versioned sym, then it should
8538 have provided a definition for the undefined sym unless
8539 it is defined in a non-shared object and forced local.
8544 version_index = iver.vs_vers & VERSYM_VERSION;
8545 if (version_index == 1 || version_index == 2)
8547 /* This is the base or first version. We can use it. */
8561 /* Add an external symbol to the symbol table. This is called from
8562 the hash table traversal routine. When generating a shared object,
8563 we go through the symbol table twice. The first time we output
8564 anything that might have been forced to local scope in a version
8565 script. The second time we output the symbols that are still
8569 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8571 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8572 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8573 struct elf_final_link_info *finfo = eoinfo->finfo;
8575 Elf_Internal_Sym sym;
8576 asection *input_sec;
8577 const struct elf_backend_data *bed;
8581 if (h->root.type == bfd_link_hash_warning)
8583 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8584 if (h->root.type == bfd_link_hash_new)
8588 /* Decide whether to output this symbol in this pass. */
8589 if (eoinfo->localsyms)
8591 if (!h->forced_local)
8596 if (h->forced_local)
8600 bed = get_elf_backend_data (finfo->output_bfd);
8602 if (h->root.type == bfd_link_hash_undefined)
8604 /* If we have an undefined symbol reference here then it must have
8605 come from a shared library that is being linked in. (Undefined
8606 references in regular files have already been handled unless
8607 they are in unreferenced sections which are removed by garbage
8609 bfd_boolean ignore_undef = FALSE;
8611 /* Some symbols may be special in that the fact that they're
8612 undefined can be safely ignored - let backend determine that. */
8613 if (bed->elf_backend_ignore_undef_symbol)
8614 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8616 /* If we are reporting errors for this situation then do so now. */
8619 && (!h->ref_regular || finfo->info->gc_sections)
8620 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)
8621 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8623 if (! (finfo->info->callbacks->undefined_symbol
8624 (finfo->info, h->root.root.string,
8625 h->ref_regular ? NULL : h->root.u.undef.abfd,
8626 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR)))
8628 bfd_set_error (bfd_error_bad_value);
8629 eoinfo->failed = TRUE;
8635 /* We should also warn if a forced local symbol is referenced from
8636 shared libraries. */
8637 if (! finfo->info->relocatable
8638 && (! finfo->info->shared)
8643 && ! elf_link_check_versioned_symbol (finfo->info, bed, h))
8648 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8649 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8650 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8651 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8653 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8654 def_bfd = finfo->output_bfd;
8655 if (h->root.u.def.section != bfd_abs_section_ptr)
8656 def_bfd = h->root.u.def.section->owner;
8657 (*_bfd_error_handler) (msg, finfo->output_bfd, def_bfd,
8658 h->root.root.string);
8659 bfd_set_error (bfd_error_bad_value);
8660 eoinfo->failed = TRUE;
8664 /* We don't want to output symbols that have never been mentioned by
8665 a regular file, or that we have been told to strip. However, if
8666 h->indx is set to -2, the symbol is used by a reloc and we must
8670 else if ((h->def_dynamic
8672 || h->root.type == bfd_link_hash_new)
8676 else if (finfo->info->strip == strip_all)
8678 else if (finfo->info->strip == strip_some
8679 && bfd_hash_lookup (finfo->info->keep_hash,
8680 h->root.root.string, FALSE, FALSE) == NULL)
8682 else if ((h->root.type == bfd_link_hash_defined
8683 || h->root.type == bfd_link_hash_defweak)
8684 && ((finfo->info->strip_discarded
8685 && elf_discarded_section (h->root.u.def.section))
8686 || (h->root.u.def.section->owner != NULL
8687 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
8689 else if ((h->root.type == bfd_link_hash_undefined
8690 || h->root.type == bfd_link_hash_undefweak)
8691 && h->root.u.undef.abfd != NULL
8692 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
8697 /* If we're stripping it, and it's not a dynamic symbol, there's
8698 nothing else to do unless it is a forced local symbol or a
8699 STT_GNU_IFUNC symbol. */
8702 && h->type != STT_GNU_IFUNC
8703 && !h->forced_local)
8707 sym.st_size = h->size;
8708 sym.st_other = h->other;
8709 if (h->forced_local)
8711 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
8712 /* Turn off visibility on local symbol. */
8713 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
8715 else if (h->unique_global)
8716 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
8717 else if (h->root.type == bfd_link_hash_undefweak
8718 || h->root.type == bfd_link_hash_defweak)
8719 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
8721 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
8722 sym.st_target_internal = h->target_internal;
8724 switch (h->root.type)
8727 case bfd_link_hash_new:
8728 case bfd_link_hash_warning:
8732 case bfd_link_hash_undefined:
8733 case bfd_link_hash_undefweak:
8734 input_sec = bfd_und_section_ptr;
8735 sym.st_shndx = SHN_UNDEF;
8738 case bfd_link_hash_defined:
8739 case bfd_link_hash_defweak:
8741 input_sec = h->root.u.def.section;
8742 if (input_sec->output_section != NULL)
8745 _bfd_elf_section_from_bfd_section (finfo->output_bfd,
8746 input_sec->output_section);
8747 if (sym.st_shndx == SHN_BAD)
8749 (*_bfd_error_handler)
8750 (_("%B: could not find output section %A for input section %A"),
8751 finfo->output_bfd, input_sec->output_section, input_sec);
8752 bfd_set_error (bfd_error_nonrepresentable_section);
8753 eoinfo->failed = TRUE;
8757 /* ELF symbols in relocatable files are section relative,
8758 but in nonrelocatable files they are virtual
8760 sym.st_value = h->root.u.def.value + input_sec->output_offset;
8761 if (! finfo->info->relocatable)
8763 sym.st_value += input_sec->output_section->vma;
8764 if (h->type == STT_TLS)
8766 asection *tls_sec = elf_hash_table (finfo->info)->tls_sec;
8767 if (tls_sec != NULL)
8768 sym.st_value -= tls_sec->vma;
8771 /* The TLS section may have been garbage collected. */
8772 BFD_ASSERT (finfo->info->gc_sections
8773 && !input_sec->gc_mark);
8780 BFD_ASSERT (input_sec->owner == NULL
8781 || (input_sec->owner->flags & DYNAMIC) != 0);
8782 sym.st_shndx = SHN_UNDEF;
8783 input_sec = bfd_und_section_ptr;
8788 case bfd_link_hash_common:
8789 input_sec = h->root.u.c.p->section;
8790 sym.st_shndx = bed->common_section_index (input_sec);
8791 sym.st_value = 1 << h->root.u.c.p->alignment_power;
8794 case bfd_link_hash_indirect:
8795 /* These symbols are created by symbol versioning. They point
8796 to the decorated version of the name. For example, if the
8797 symbol foo@@GNU_1.2 is the default, which should be used when
8798 foo is used with no version, then we add an indirect symbol
8799 foo which points to foo@@GNU_1.2. We ignore these symbols,
8800 since the indirected symbol is already in the hash table. */
8804 /* Give the processor backend a chance to tweak the symbol value,
8805 and also to finish up anything that needs to be done for this
8806 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
8807 forced local syms when non-shared is due to a historical quirk.
8808 STT_GNU_IFUNC symbol must go through PLT. */
8809 if ((h->type == STT_GNU_IFUNC
8811 && !finfo->info->relocatable)
8812 || ((h->dynindx != -1
8814 && ((finfo->info->shared
8815 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8816 || h->root.type != bfd_link_hash_undefweak))
8817 || !h->forced_local)
8818 && elf_hash_table (finfo->info)->dynamic_sections_created))
8820 if (! ((*bed->elf_backend_finish_dynamic_symbol)
8821 (finfo->output_bfd, finfo->info, h, &sym)))
8823 eoinfo->failed = TRUE;
8828 /* If we are marking the symbol as undefined, and there are no
8829 non-weak references to this symbol from a regular object, then
8830 mark the symbol as weak undefined; if there are non-weak
8831 references, mark the symbol as strong. We can't do this earlier,
8832 because it might not be marked as undefined until the
8833 finish_dynamic_symbol routine gets through with it. */
8834 if (sym.st_shndx == SHN_UNDEF
8836 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
8837 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
8840 unsigned int type = ELF_ST_TYPE (sym.st_info);
8842 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
8843 if (type == STT_GNU_IFUNC)
8846 if (h->ref_regular_nonweak)
8847 bindtype = STB_GLOBAL;
8849 bindtype = STB_WEAK;
8850 sym.st_info = ELF_ST_INFO (bindtype, type);
8853 /* If this is a symbol defined in a dynamic library, don't use the
8854 symbol size from the dynamic library. Relinking an executable
8855 against a new library may introduce gratuitous changes in the
8856 executable's symbols if we keep the size. */
8857 if (sym.st_shndx == SHN_UNDEF
8862 /* If a non-weak symbol with non-default visibility is not defined
8863 locally, it is a fatal error. */
8864 if (! finfo->info->relocatable
8865 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
8866 && ELF_ST_BIND (sym.st_info) != STB_WEAK
8867 && h->root.type == bfd_link_hash_undefined
8872 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
8873 msg = _("%B: protected symbol `%s' isn't defined");
8874 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
8875 msg = _("%B: internal symbol `%s' isn't defined");
8877 msg = _("%B: hidden symbol `%s' isn't defined");
8878 (*_bfd_error_handler) (msg, finfo->output_bfd, h->root.root.string);
8879 bfd_set_error (bfd_error_bad_value);
8880 eoinfo->failed = TRUE;
8884 /* If this symbol should be put in the .dynsym section, then put it
8885 there now. We already know the symbol index. We also fill in
8886 the entry in the .hash section. */
8887 if (h->dynindx != -1
8888 && elf_hash_table (finfo->info)->dynamic_sections_created)
8892 sym.st_name = h->dynstr_index;
8893 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
8894 if (! check_dynsym (finfo->output_bfd, &sym))
8896 eoinfo->failed = TRUE;
8899 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0);
8901 if (finfo->hash_sec != NULL)
8903 size_t hash_entry_size;
8904 bfd_byte *bucketpos;
8909 bucketcount = elf_hash_table (finfo->info)->bucketcount;
8910 bucket = h->u.elf_hash_value % bucketcount;
8913 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize;
8914 bucketpos = ((bfd_byte *) finfo->hash_sec->contents
8915 + (bucket + 2) * hash_entry_size);
8916 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos);
8917 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos);
8918 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain,
8919 ((bfd_byte *) finfo->hash_sec->contents
8920 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
8923 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL)
8925 Elf_Internal_Versym iversym;
8926 Elf_External_Versym *eversym;
8928 if (!h->def_regular)
8930 if (h->verinfo.verdef == NULL)
8931 iversym.vs_vers = 0;
8933 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
8937 if (h->verinfo.vertree == NULL)
8938 iversym.vs_vers = 1;
8940 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
8941 if (finfo->info->create_default_symver)
8946 iversym.vs_vers |= VERSYM_HIDDEN;
8948 eversym = (Elf_External_Versym *) finfo->symver_sec->contents;
8949 eversym += h->dynindx;
8950 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym);
8954 /* If we're stripping it, then it was just a dynamic symbol, and
8955 there's nothing else to do. */
8956 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
8959 indx = bfd_get_symcount (finfo->output_bfd);
8960 ret = elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h);
8963 eoinfo->failed = TRUE;
8968 else if (h->indx == -2)
8974 /* Return TRUE if special handling is done for relocs in SEC against
8975 symbols defined in discarded sections. */
8978 elf_section_ignore_discarded_relocs (asection *sec)
8980 const struct elf_backend_data *bed;
8982 switch (sec->sec_info_type)
8984 case ELF_INFO_TYPE_STABS:
8985 case ELF_INFO_TYPE_EH_FRAME:
8991 bed = get_elf_backend_data (sec->owner);
8992 if (bed->elf_backend_ignore_discarded_relocs != NULL
8993 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
8999 /* Return a mask saying how ld should treat relocations in SEC against
9000 symbols defined in discarded sections. If this function returns
9001 COMPLAIN set, ld will issue a warning message. If this function
9002 returns PRETEND set, and the discarded section was link-once and the
9003 same size as the kept link-once section, ld will pretend that the
9004 symbol was actually defined in the kept section. Otherwise ld will
9005 zero the reloc (at least that is the intent, but some cooperation by
9006 the target dependent code is needed, particularly for REL targets). */
9009 _bfd_elf_default_action_discarded (asection *sec)
9011 if (sec->flags & SEC_DEBUGGING)
9014 if (strcmp (".eh_frame", sec->name) == 0)
9017 if (strcmp (".gcc_except_table", sec->name) == 0)
9020 return COMPLAIN | PRETEND;
9023 /* Find a match between a section and a member of a section group. */
9026 match_group_member (asection *sec, asection *group,
9027 struct bfd_link_info *info)
9029 asection *first = elf_next_in_group (group);
9030 asection *s = first;
9034 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9037 s = elf_next_in_group (s);
9045 /* Check if the kept section of a discarded section SEC can be used
9046 to replace it. Return the replacement if it is OK. Otherwise return
9050 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9054 kept = sec->kept_section;
9057 if ((kept->flags & SEC_GROUP) != 0)
9058 kept = match_group_member (sec, kept, info);
9060 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9061 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9063 sec->kept_section = kept;
9068 /* Link an input file into the linker output file. This function
9069 handles all the sections and relocations of the input file at once.
9070 This is so that we only have to read the local symbols once, and
9071 don't have to keep them in memory. */
9074 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd)
9076 int (*relocate_section)
9077 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9078 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9080 Elf_Internal_Shdr *symtab_hdr;
9083 Elf_Internal_Sym *isymbuf;
9084 Elf_Internal_Sym *isym;
9085 Elf_Internal_Sym *isymend;
9087 asection **ppsection;
9089 const struct elf_backend_data *bed;
9090 struct elf_link_hash_entry **sym_hashes;
9091 bfd_size_type address_size;
9092 bfd_vma r_type_mask;
9095 output_bfd = finfo->output_bfd;
9096 bed = get_elf_backend_data (output_bfd);
9097 relocate_section = bed->elf_backend_relocate_section;
9099 /* If this is a dynamic object, we don't want to do anything here:
9100 we don't want the local symbols, and we don't want the section
9102 if ((input_bfd->flags & DYNAMIC) != 0)
9105 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9106 if (elf_bad_symtab (input_bfd))
9108 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9113 locsymcount = symtab_hdr->sh_info;
9114 extsymoff = symtab_hdr->sh_info;
9117 /* Read the local symbols. */
9118 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9119 if (isymbuf == NULL && locsymcount != 0)
9121 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9122 finfo->internal_syms,
9123 finfo->external_syms,
9124 finfo->locsym_shndx);
9125 if (isymbuf == NULL)
9129 /* Find local symbol sections and adjust values of symbols in
9130 SEC_MERGE sections. Write out those local symbols we know are
9131 going into the output file. */
9132 isymend = isymbuf + locsymcount;
9133 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections;
9135 isym++, pindex++, ppsection++)
9139 Elf_Internal_Sym osym;
9145 if (elf_bad_symtab (input_bfd))
9147 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9154 if (isym->st_shndx == SHN_UNDEF)
9155 isec = bfd_und_section_ptr;
9156 else if (isym->st_shndx == SHN_ABS)
9157 isec = bfd_abs_section_ptr;
9158 else if (isym->st_shndx == SHN_COMMON)
9159 isec = bfd_com_section_ptr;
9162 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9165 /* Don't attempt to output symbols with st_shnx in the
9166 reserved range other than SHN_ABS and SHN_COMMON. */
9170 else if (isec->sec_info_type == ELF_INFO_TYPE_MERGE
9171 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9173 _bfd_merged_section_offset (output_bfd, &isec,
9174 elf_section_data (isec)->sec_info,
9180 /* Don't output the first, undefined, symbol. */
9181 if (ppsection == finfo->sections)
9184 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9186 /* We never output section symbols. Instead, we use the
9187 section symbol of the corresponding section in the output
9192 /* If we are stripping all symbols, we don't want to output this
9194 if (finfo->info->strip == strip_all)
9197 /* If we are discarding all local symbols, we don't want to
9198 output this one. If we are generating a relocatable output
9199 file, then some of the local symbols may be required by
9200 relocs; we output them below as we discover that they are
9202 if (finfo->info->discard == discard_all)
9205 /* If this symbol is defined in a section which we are
9206 discarding, we don't need to keep it. */
9207 if (isym->st_shndx != SHN_UNDEF
9208 && isym->st_shndx < SHN_LORESERVE
9209 && bfd_section_removed_from_list (output_bfd,
9210 isec->output_section))
9213 /* Get the name of the symbol. */
9214 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9219 /* See if we are discarding symbols with this name. */
9220 if ((finfo->info->strip == strip_some
9221 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
9223 || (((finfo->info->discard == discard_sec_merge
9224 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable)
9225 || finfo->info->discard == discard_l)
9226 && bfd_is_local_label_name (input_bfd, name)))
9231 /* Adjust the section index for the output file. */
9232 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9233 isec->output_section);
9234 if (osym.st_shndx == SHN_BAD)
9237 /* ELF symbols in relocatable files are section relative, but
9238 in executable files they are virtual addresses. Note that
9239 this code assumes that all ELF sections have an associated
9240 BFD section with a reasonable value for output_offset; below
9241 we assume that they also have a reasonable value for
9242 output_section. Any special sections must be set up to meet
9243 these requirements. */
9244 osym.st_value += isec->output_offset;
9245 if (! finfo->info->relocatable)
9247 osym.st_value += isec->output_section->vma;
9248 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9250 /* STT_TLS symbols are relative to PT_TLS segment base. */
9251 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL);
9252 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma;
9256 indx = bfd_get_symcount (output_bfd);
9257 ret = elf_link_output_sym (finfo, name, &osym, isec, NULL);
9264 if (bed->s->arch_size == 32)
9272 r_type_mask = 0xffffffff;
9277 /* Relocate the contents of each section. */
9278 sym_hashes = elf_sym_hashes (input_bfd);
9279 for (o = input_bfd->sections; o != NULL; o = o->next)
9283 if (! o->linker_mark)
9285 /* This section was omitted from the link. */
9289 if (finfo->info->relocatable
9290 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9292 /* Deal with the group signature symbol. */
9293 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9294 unsigned long symndx = sec_data->this_hdr.sh_info;
9295 asection *osec = o->output_section;
9297 if (symndx >= locsymcount
9298 || (elf_bad_symtab (input_bfd)
9299 && finfo->sections[symndx] == NULL))
9301 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9302 while (h->root.type == bfd_link_hash_indirect
9303 || h->root.type == bfd_link_hash_warning)
9304 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9305 /* Arrange for symbol to be output. */
9307 elf_section_data (osec)->this_hdr.sh_info = -2;
9309 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9311 /* We'll use the output section target_index. */
9312 asection *sec = finfo->sections[symndx]->output_section;
9313 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9317 if (finfo->indices[symndx] == -1)
9319 /* Otherwise output the local symbol now. */
9320 Elf_Internal_Sym sym = isymbuf[symndx];
9321 asection *sec = finfo->sections[symndx]->output_section;
9326 name = bfd_elf_string_from_elf_section (input_bfd,
9327 symtab_hdr->sh_link,
9332 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9334 if (sym.st_shndx == SHN_BAD)
9337 sym.st_value += o->output_offset;
9339 indx = bfd_get_symcount (output_bfd);
9340 ret = elf_link_output_sym (finfo, name, &sym, o, NULL);
9344 finfo->indices[symndx] = indx;
9348 elf_section_data (osec)->this_hdr.sh_info
9349 = finfo->indices[symndx];
9353 if ((o->flags & SEC_HAS_CONTENTS) == 0
9354 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9357 if ((o->flags & SEC_LINKER_CREATED) != 0)
9359 /* Section was created by _bfd_elf_link_create_dynamic_sections
9364 /* Get the contents of the section. They have been cached by a
9365 relaxation routine. Note that o is a section in an input
9366 file, so the contents field will not have been set by any of
9367 the routines which work on output files. */
9368 if (elf_section_data (o)->this_hdr.contents != NULL)
9369 contents = elf_section_data (o)->this_hdr.contents;
9372 contents = finfo->contents;
9373 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9377 if ((o->flags & SEC_RELOC) != 0)
9379 Elf_Internal_Rela *internal_relocs;
9380 Elf_Internal_Rela *rel, *relend;
9381 int action_discarded;
9384 /* Get the swapped relocs. */
9386 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs,
9387 finfo->internal_relocs, FALSE);
9388 if (internal_relocs == NULL
9389 && o->reloc_count > 0)
9392 /* We need to reverse-copy input .ctors/.dtors sections if
9393 they are placed in .init_array/.finit_array for output. */
9394 if (o->size > address_size
9395 && ((strncmp (o->name, ".ctors", 6) == 0
9396 && strcmp (o->output_section->name,
9397 ".init_array") == 0)
9398 || (strncmp (o->name, ".dtors", 6) == 0
9399 && strcmp (o->output_section->name,
9400 ".fini_array") == 0))
9401 && (o->name[6] == 0 || o->name[6] == '.'))
9403 if (o->size != o->reloc_count * address_size)
9405 (*_bfd_error_handler)
9406 (_("error: %B: size of section %A is not "
9407 "multiple of address size"),
9409 bfd_set_error (bfd_error_on_input);
9412 o->flags |= SEC_ELF_REVERSE_COPY;
9415 action_discarded = -1;
9416 if (!elf_section_ignore_discarded_relocs (o))
9417 action_discarded = (*bed->action_discarded) (o);
9419 /* Run through the relocs evaluating complex reloc symbols and
9420 looking for relocs against symbols from discarded sections
9421 or section symbols from removed link-once sections.
9422 Complain about relocs against discarded sections. Zero
9423 relocs against removed link-once sections. */
9425 rel = internal_relocs;
9426 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9427 for ( ; rel < relend; rel++)
9429 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9430 unsigned int s_type;
9431 asection **ps, *sec;
9432 struct elf_link_hash_entry *h = NULL;
9433 const char *sym_name;
9435 if (r_symndx == STN_UNDEF)
9438 if (r_symndx >= locsymcount
9439 || (elf_bad_symtab (input_bfd)
9440 && finfo->sections[r_symndx] == NULL))
9442 h = sym_hashes[r_symndx - extsymoff];
9444 /* Badly formatted input files can contain relocs that
9445 reference non-existant symbols. Check here so that
9446 we do not seg fault. */
9451 sprintf_vma (buffer, rel->r_info);
9452 (*_bfd_error_handler)
9453 (_("error: %B contains a reloc (0x%s) for section %A "
9454 "that references a non-existent global symbol"),
9455 input_bfd, o, buffer);
9456 bfd_set_error (bfd_error_bad_value);
9460 while (h->root.type == bfd_link_hash_indirect
9461 || h->root.type == bfd_link_hash_warning)
9462 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9467 if (h->root.type == bfd_link_hash_defined
9468 || h->root.type == bfd_link_hash_defweak)
9469 ps = &h->root.u.def.section;
9471 sym_name = h->root.root.string;
9475 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9477 s_type = ELF_ST_TYPE (sym->st_info);
9478 ps = &finfo->sections[r_symndx];
9479 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9483 if ((s_type == STT_RELC || s_type == STT_SRELC)
9484 && !finfo->info->relocatable)
9487 bfd_vma dot = (rel->r_offset
9488 + o->output_offset + o->output_section->vma);
9490 printf ("Encountered a complex symbol!");
9491 printf (" (input_bfd %s, section %s, reloc %ld\n",
9492 input_bfd->filename, o->name,
9493 (long) (rel - internal_relocs));
9494 printf (" symbol: idx %8.8lx, name %s\n",
9495 r_symndx, sym_name);
9496 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9497 (unsigned long) rel->r_info,
9498 (unsigned long) rel->r_offset);
9500 if (!eval_symbol (&val, &sym_name, input_bfd, finfo, dot,
9501 isymbuf, locsymcount, s_type == STT_SRELC))
9504 /* Symbol evaluated OK. Update to absolute value. */
9505 set_symbol_value (input_bfd, isymbuf, locsymcount,
9510 if (action_discarded != -1 && ps != NULL)
9512 /* Complain if the definition comes from a
9513 discarded section. */
9514 if ((sec = *ps) != NULL && elf_discarded_section (sec))
9516 BFD_ASSERT (r_symndx != STN_UNDEF);
9517 if (action_discarded & COMPLAIN)
9518 (*finfo->info->callbacks->einfo)
9519 (_("%X`%s' referenced in section `%A' of %B: "
9520 "defined in discarded section `%A' of %B\n"),
9521 sym_name, o, input_bfd, sec, sec->owner);
9523 /* Try to do the best we can to support buggy old
9524 versions of gcc. Pretend that the symbol is
9525 really defined in the kept linkonce section.
9526 FIXME: This is quite broken. Modifying the
9527 symbol here means we will be changing all later
9528 uses of the symbol, not just in this section. */
9529 if (action_discarded & PRETEND)
9533 kept = _bfd_elf_check_kept_section (sec,
9545 /* Relocate the section by invoking a back end routine.
9547 The back end routine is responsible for adjusting the
9548 section contents as necessary, and (if using Rela relocs
9549 and generating a relocatable output file) adjusting the
9550 reloc addend as necessary.
9552 The back end routine does not have to worry about setting
9553 the reloc address or the reloc symbol index.
9555 The back end routine is given a pointer to the swapped in
9556 internal symbols, and can access the hash table entries
9557 for the external symbols via elf_sym_hashes (input_bfd).
9559 When generating relocatable output, the back end routine
9560 must handle STB_LOCAL/STT_SECTION symbols specially. The
9561 output symbol is going to be a section symbol
9562 corresponding to the output section, which will require
9563 the addend to be adjusted. */
9565 ret = (*relocate_section) (output_bfd, finfo->info,
9566 input_bfd, o, contents,
9574 || finfo->info->relocatable
9575 || finfo->info->emitrelocations)
9577 Elf_Internal_Rela *irela;
9578 Elf_Internal_Rela *irelaend, *irelamid;
9579 bfd_vma last_offset;
9580 struct elf_link_hash_entry **rel_hash;
9581 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
9582 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
9583 unsigned int next_erel;
9584 bfd_boolean rela_normal;
9585 struct bfd_elf_section_data *esdi, *esdo;
9587 esdi = elf_section_data (o);
9588 esdo = elf_section_data (o->output_section);
9589 rela_normal = FALSE;
9591 /* Adjust the reloc addresses and symbol indices. */
9593 irela = internal_relocs;
9594 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
9595 rel_hash = esdo->rel.hashes + esdo->rel.count;
9596 /* We start processing the REL relocs, if any. When we reach
9597 IRELAMID in the loop, we switch to the RELA relocs. */
9599 if (esdi->rel.hdr != NULL)
9600 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
9601 * bed->s->int_rels_per_ext_rel);
9602 rel_hash_list = rel_hash;
9603 rela_hash_list = NULL;
9604 last_offset = o->output_offset;
9605 if (!finfo->info->relocatable)
9606 last_offset += o->output_section->vma;
9607 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
9609 unsigned long r_symndx;
9611 Elf_Internal_Sym sym;
9613 if (next_erel == bed->s->int_rels_per_ext_rel)
9619 if (irela == irelamid)
9621 rel_hash = esdo->rela.hashes + esdo->rela.count;
9622 rela_hash_list = rel_hash;
9623 rela_normal = bed->rela_normal;
9626 irela->r_offset = _bfd_elf_section_offset (output_bfd,
9629 if (irela->r_offset >= (bfd_vma) -2)
9631 /* This is a reloc for a deleted entry or somesuch.
9632 Turn it into an R_*_NONE reloc, at the same
9633 offset as the last reloc. elf_eh_frame.c and
9634 bfd_elf_discard_info rely on reloc offsets
9636 irela->r_offset = last_offset;
9638 irela->r_addend = 0;
9642 irela->r_offset += o->output_offset;
9644 /* Relocs in an executable have to be virtual addresses. */
9645 if (!finfo->info->relocatable)
9646 irela->r_offset += o->output_section->vma;
9648 last_offset = irela->r_offset;
9650 r_symndx = irela->r_info >> r_sym_shift;
9651 if (r_symndx == STN_UNDEF)
9654 if (r_symndx >= locsymcount
9655 || (elf_bad_symtab (input_bfd)
9656 && finfo->sections[r_symndx] == NULL))
9658 struct elf_link_hash_entry *rh;
9661 /* This is a reloc against a global symbol. We
9662 have not yet output all the local symbols, so
9663 we do not know the symbol index of any global
9664 symbol. We set the rel_hash entry for this
9665 reloc to point to the global hash table entry
9666 for this symbol. The symbol index is then
9667 set at the end of bfd_elf_final_link. */
9668 indx = r_symndx - extsymoff;
9669 rh = elf_sym_hashes (input_bfd)[indx];
9670 while (rh->root.type == bfd_link_hash_indirect
9671 || rh->root.type == bfd_link_hash_warning)
9672 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
9674 /* Setting the index to -2 tells
9675 elf_link_output_extsym that this symbol is
9677 BFD_ASSERT (rh->indx < 0);
9685 /* This is a reloc against a local symbol. */
9688 sym = isymbuf[r_symndx];
9689 sec = finfo->sections[r_symndx];
9690 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
9692 /* I suppose the backend ought to fill in the
9693 section of any STT_SECTION symbol against a
9694 processor specific section. */
9695 r_symndx = STN_UNDEF;
9696 if (bfd_is_abs_section (sec))
9698 else if (sec == NULL || sec->owner == NULL)
9700 bfd_set_error (bfd_error_bad_value);
9705 asection *osec = sec->output_section;
9707 /* If we have discarded a section, the output
9708 section will be the absolute section. In
9709 case of discarded SEC_MERGE sections, use
9710 the kept section. relocate_section should
9711 have already handled discarded linkonce
9713 if (bfd_is_abs_section (osec)
9714 && sec->kept_section != NULL
9715 && sec->kept_section->output_section != NULL)
9717 osec = sec->kept_section->output_section;
9718 irela->r_addend -= osec->vma;
9721 if (!bfd_is_abs_section (osec))
9723 r_symndx = osec->target_index;
9724 if (r_symndx == STN_UNDEF)
9726 struct elf_link_hash_table *htab;
9729 htab = elf_hash_table (finfo->info);
9730 oi = htab->text_index_section;
9731 if ((osec->flags & SEC_READONLY) == 0
9732 && htab->data_index_section != NULL)
9733 oi = htab->data_index_section;
9737 irela->r_addend += osec->vma - oi->vma;
9738 r_symndx = oi->target_index;
9742 BFD_ASSERT (r_symndx != STN_UNDEF);
9746 /* Adjust the addend according to where the
9747 section winds up in the output section. */
9749 irela->r_addend += sec->output_offset;
9753 if (finfo->indices[r_symndx] == -1)
9755 unsigned long shlink;
9760 if (finfo->info->strip == strip_all)
9762 /* You can't do ld -r -s. */
9763 bfd_set_error (bfd_error_invalid_operation);
9767 /* This symbol was skipped earlier, but
9768 since it is needed by a reloc, we
9769 must output it now. */
9770 shlink = symtab_hdr->sh_link;
9771 name = (bfd_elf_string_from_elf_section
9772 (input_bfd, shlink, sym.st_name));
9776 osec = sec->output_section;
9778 _bfd_elf_section_from_bfd_section (output_bfd,
9780 if (sym.st_shndx == SHN_BAD)
9783 sym.st_value += sec->output_offset;
9784 if (! finfo->info->relocatable)
9786 sym.st_value += osec->vma;
9787 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
9789 /* STT_TLS symbols are relative to PT_TLS
9791 BFD_ASSERT (elf_hash_table (finfo->info)
9793 sym.st_value -= (elf_hash_table (finfo->info)
9798 indx = bfd_get_symcount (output_bfd);
9799 ret = elf_link_output_sym (finfo, name, &sym, sec,
9804 finfo->indices[r_symndx] = indx;
9809 r_symndx = finfo->indices[r_symndx];
9812 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
9813 | (irela->r_info & r_type_mask));
9816 /* Swap out the relocs. */
9817 input_rel_hdr = esdi->rel.hdr;
9818 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
9820 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9825 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
9826 * bed->s->int_rels_per_ext_rel);
9827 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
9830 input_rela_hdr = esdi->rela.hdr;
9831 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
9833 if (!bed->elf_backend_emit_relocs (output_bfd, o,
9842 /* Write out the modified section contents. */
9843 if (bed->elf_backend_write_section
9844 && (*bed->elf_backend_write_section) (output_bfd, finfo->info, o,
9847 /* Section written out. */
9849 else switch (o->sec_info_type)
9851 case ELF_INFO_TYPE_STABS:
9852 if (! (_bfd_write_section_stabs
9854 &elf_hash_table (finfo->info)->stab_info,
9855 o, &elf_section_data (o)->sec_info, contents)))
9858 case ELF_INFO_TYPE_MERGE:
9859 if (! _bfd_write_merged_section (output_bfd, o,
9860 elf_section_data (o)->sec_info))
9863 case ELF_INFO_TYPE_EH_FRAME:
9865 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info,
9872 /* FIXME: octets_per_byte. */
9873 if (! (o->flags & SEC_EXCLUDE))
9875 file_ptr offset = (file_ptr) o->output_offset;
9876 bfd_size_type todo = o->size;
9877 if ((o->flags & SEC_ELF_REVERSE_COPY))
9879 /* Reverse-copy input section to output. */
9882 todo -= address_size;
9883 if (! bfd_set_section_contents (output_bfd,
9891 offset += address_size;
9895 else if (! bfd_set_section_contents (output_bfd,
9909 /* Generate a reloc when linking an ELF file. This is a reloc
9910 requested by the linker, and does not come from any input file. This
9911 is used to build constructor and destructor tables when linking
9915 elf_reloc_link_order (bfd *output_bfd,
9916 struct bfd_link_info *info,
9917 asection *output_section,
9918 struct bfd_link_order *link_order)
9920 reloc_howto_type *howto;
9924 struct bfd_elf_section_reloc_data *reldata;
9925 struct elf_link_hash_entry **rel_hash_ptr;
9926 Elf_Internal_Shdr *rel_hdr;
9927 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
9928 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
9931 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
9933 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
9936 bfd_set_error (bfd_error_bad_value);
9940 addend = link_order->u.reloc.p->addend;
9943 reldata = &esdo->rel;
9944 else if (esdo->rela.hdr)
9945 reldata = &esdo->rela;
9952 /* Figure out the symbol index. */
9953 rel_hash_ptr = reldata->hashes + reldata->count;
9954 if (link_order->type == bfd_section_reloc_link_order)
9956 indx = link_order->u.reloc.p->u.section->target_index;
9957 BFD_ASSERT (indx != 0);
9958 *rel_hash_ptr = NULL;
9962 struct elf_link_hash_entry *h;
9964 /* Treat a reloc against a defined symbol as though it were
9965 actually against the section. */
9966 h = ((struct elf_link_hash_entry *)
9967 bfd_wrapped_link_hash_lookup (output_bfd, info,
9968 link_order->u.reloc.p->u.name,
9969 FALSE, FALSE, TRUE));
9971 && (h->root.type == bfd_link_hash_defined
9972 || h->root.type == bfd_link_hash_defweak))
9976 section = h->root.u.def.section;
9977 indx = section->output_section->target_index;
9978 *rel_hash_ptr = NULL;
9979 /* It seems that we ought to add the symbol value to the
9980 addend here, but in practice it has already been added
9981 because it was passed to constructor_callback. */
9982 addend += section->output_section->vma + section->output_offset;
9986 /* Setting the index to -2 tells elf_link_output_extsym that
9987 this symbol is used by a reloc. */
9994 if (! ((*info->callbacks->unattached_reloc)
9995 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10001 /* If this is an inplace reloc, we must write the addend into the
10003 if (howto->partial_inplace && addend != 0)
10005 bfd_size_type size;
10006 bfd_reloc_status_type rstat;
10009 const char *sym_name;
10011 size = (bfd_size_type) bfd_get_reloc_size (howto);
10012 buf = (bfd_byte *) bfd_zmalloc (size);
10015 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10022 case bfd_reloc_outofrange:
10025 case bfd_reloc_overflow:
10026 if (link_order->type == bfd_section_reloc_link_order)
10027 sym_name = bfd_section_name (output_bfd,
10028 link_order->u.reloc.p->u.section);
10030 sym_name = link_order->u.reloc.p->u.name;
10031 if (! ((*info->callbacks->reloc_overflow)
10032 (info, NULL, sym_name, howto->name, addend, NULL,
10033 NULL, (bfd_vma) 0)))
10040 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10041 link_order->offset, size);
10047 /* The address of a reloc is relative to the section in a
10048 relocatable file, and is a virtual address in an executable
10050 offset = link_order->offset;
10051 if (! info->relocatable)
10052 offset += output_section->vma;
10054 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10056 irel[i].r_offset = offset;
10057 irel[i].r_info = 0;
10058 irel[i].r_addend = 0;
10060 if (bed->s->arch_size == 32)
10061 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10063 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10065 rel_hdr = reldata->hdr;
10066 erel = rel_hdr->contents;
10067 if (rel_hdr->sh_type == SHT_REL)
10069 erel += reldata->count * bed->s->sizeof_rel;
10070 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10074 irel[0].r_addend = addend;
10075 erel += reldata->count * bed->s->sizeof_rela;
10076 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10085 /* Get the output vma of the section pointed to by the sh_link field. */
10088 elf_get_linked_section_vma (struct bfd_link_order *p)
10090 Elf_Internal_Shdr **elf_shdrp;
10094 s = p->u.indirect.section;
10095 elf_shdrp = elf_elfsections (s->owner);
10096 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10097 elfsec = elf_shdrp[elfsec]->sh_link;
10099 The Intel C compiler generates SHT_IA_64_UNWIND with
10100 SHF_LINK_ORDER. But it doesn't set the sh_link or
10101 sh_info fields. Hence we could get the situation
10102 where elfsec is 0. */
10105 const struct elf_backend_data *bed
10106 = get_elf_backend_data (s->owner);
10107 if (bed->link_order_error_handler)
10108 bed->link_order_error_handler
10109 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10114 s = elf_shdrp[elfsec]->bfd_section;
10115 return s->output_section->vma + s->output_offset;
10120 /* Compare two sections based on the locations of the sections they are
10121 linked to. Used by elf_fixup_link_order. */
10124 compare_link_order (const void * a, const void * b)
10129 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10130 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10133 return apos > bpos;
10137 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10138 order as their linked sections. Returns false if this could not be done
10139 because an output section includes both ordered and unordered
10140 sections. Ideally we'd do this in the linker proper. */
10143 elf_fixup_link_order (bfd *abfd, asection *o)
10145 int seen_linkorder;
10148 struct bfd_link_order *p;
10150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10152 struct bfd_link_order **sections;
10153 asection *s, *other_sec, *linkorder_sec;
10157 linkorder_sec = NULL;
10159 seen_linkorder = 0;
10160 for (p = o->map_head.link_order; p != NULL; p = p->next)
10162 if (p->type == bfd_indirect_link_order)
10164 s = p->u.indirect.section;
10166 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10167 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10168 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10169 && elfsec < elf_numsections (sub)
10170 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10171 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10185 if (seen_other && seen_linkorder)
10187 if (other_sec && linkorder_sec)
10188 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10190 linkorder_sec->owner, other_sec,
10193 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10195 bfd_set_error (bfd_error_bad_value);
10200 if (!seen_linkorder)
10203 sections = (struct bfd_link_order **)
10204 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10205 if (sections == NULL)
10207 seen_linkorder = 0;
10209 for (p = o->map_head.link_order; p != NULL; p = p->next)
10211 sections[seen_linkorder++] = p;
10213 /* Sort the input sections in the order of their linked section. */
10214 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10215 compare_link_order);
10217 /* Change the offsets of the sections. */
10219 for (n = 0; n < seen_linkorder; n++)
10221 s = sections[n]->u.indirect.section;
10222 offset &= ~(bfd_vma) 0 << s->alignment_power;
10223 s->output_offset = offset;
10224 sections[n]->offset = offset;
10225 /* FIXME: octets_per_byte. */
10226 offset += sections[n]->size;
10234 /* Do the final step of an ELF link. */
10237 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10239 bfd_boolean dynamic;
10240 bfd_boolean emit_relocs;
10242 struct elf_final_link_info finfo;
10244 struct bfd_link_order *p;
10246 bfd_size_type max_contents_size;
10247 bfd_size_type max_external_reloc_size;
10248 bfd_size_type max_internal_reloc_count;
10249 bfd_size_type max_sym_count;
10250 bfd_size_type max_sym_shndx_count;
10252 Elf_Internal_Sym elfsym;
10254 Elf_Internal_Shdr *symtab_hdr;
10255 Elf_Internal_Shdr *symtab_shndx_hdr;
10256 Elf_Internal_Shdr *symstrtab_hdr;
10257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10258 struct elf_outext_info eoinfo;
10259 bfd_boolean merged;
10260 size_t relativecount = 0;
10261 asection *reldyn = 0;
10263 asection *attr_section = NULL;
10264 bfd_vma attr_size = 0;
10265 const char *std_attrs_section;
10267 if (! is_elf_hash_table (info->hash))
10271 abfd->flags |= DYNAMIC;
10273 dynamic = elf_hash_table (info)->dynamic_sections_created;
10274 dynobj = elf_hash_table (info)->dynobj;
10276 emit_relocs = (info->relocatable
10277 || info->emitrelocations);
10280 finfo.output_bfd = abfd;
10281 finfo.symstrtab = _bfd_elf_stringtab_init ();
10282 if (finfo.symstrtab == NULL)
10287 finfo.dynsym_sec = NULL;
10288 finfo.hash_sec = NULL;
10289 finfo.symver_sec = NULL;
10293 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym");
10294 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash");
10295 BFD_ASSERT (finfo.dynsym_sec != NULL);
10296 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version");
10297 /* Note that it is OK if symver_sec is NULL. */
10300 finfo.contents = NULL;
10301 finfo.external_relocs = NULL;
10302 finfo.internal_relocs = NULL;
10303 finfo.external_syms = NULL;
10304 finfo.locsym_shndx = NULL;
10305 finfo.internal_syms = NULL;
10306 finfo.indices = NULL;
10307 finfo.sections = NULL;
10308 finfo.symbuf = NULL;
10309 finfo.symshndxbuf = NULL;
10310 finfo.symbuf_count = 0;
10311 finfo.shndxbuf_size = 0;
10313 /* The object attributes have been merged. Remove the input
10314 sections from the link, and set the contents of the output
10316 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10317 for (o = abfd->sections; o != NULL; o = o->next)
10319 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10320 || strcmp (o->name, ".gnu.attributes") == 0)
10322 for (p = o->map_head.link_order; p != NULL; p = p->next)
10324 asection *input_section;
10326 if (p->type != bfd_indirect_link_order)
10328 input_section = p->u.indirect.section;
10329 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10330 elf_link_input_bfd ignores this section. */
10331 input_section->flags &= ~SEC_HAS_CONTENTS;
10334 attr_size = bfd_elf_obj_attr_size (abfd);
10337 bfd_set_section_size (abfd, o, attr_size);
10339 /* Skip this section later on. */
10340 o->map_head.link_order = NULL;
10343 o->flags |= SEC_EXCLUDE;
10347 /* Count up the number of relocations we will output for each output
10348 section, so that we know the sizes of the reloc sections. We
10349 also figure out some maximum sizes. */
10350 max_contents_size = 0;
10351 max_external_reloc_size = 0;
10352 max_internal_reloc_count = 0;
10354 max_sym_shndx_count = 0;
10356 for (o = abfd->sections; o != NULL; o = o->next)
10358 struct bfd_elf_section_data *esdo = elf_section_data (o);
10359 o->reloc_count = 0;
10361 for (p = o->map_head.link_order; p != NULL; p = p->next)
10363 unsigned int reloc_count = 0;
10364 struct bfd_elf_section_data *esdi = NULL;
10366 if (p->type == bfd_section_reloc_link_order
10367 || p->type == bfd_symbol_reloc_link_order)
10369 else if (p->type == bfd_indirect_link_order)
10373 sec = p->u.indirect.section;
10374 esdi = elf_section_data (sec);
10376 /* Mark all sections which are to be included in the
10377 link. This will normally be every section. We need
10378 to do this so that we can identify any sections which
10379 the linker has decided to not include. */
10380 sec->linker_mark = TRUE;
10382 if (sec->flags & SEC_MERGE)
10385 if (info->relocatable || info->emitrelocations)
10386 reloc_count = sec->reloc_count;
10387 else if (bed->elf_backend_count_relocs)
10388 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10390 if (sec->rawsize > max_contents_size)
10391 max_contents_size = sec->rawsize;
10392 if (sec->size > max_contents_size)
10393 max_contents_size = sec->size;
10395 /* We are interested in just local symbols, not all
10397 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10398 && (sec->owner->flags & DYNAMIC) == 0)
10402 if (elf_bad_symtab (sec->owner))
10403 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10404 / bed->s->sizeof_sym);
10406 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10408 if (sym_count > max_sym_count)
10409 max_sym_count = sym_count;
10411 if (sym_count > max_sym_shndx_count
10412 && elf_symtab_shndx (sec->owner) != 0)
10413 max_sym_shndx_count = sym_count;
10415 if ((sec->flags & SEC_RELOC) != 0)
10417 size_t ext_size = 0;
10419 if (esdi->rel.hdr != NULL)
10420 ext_size = esdi->rel.hdr->sh_size;
10421 if (esdi->rela.hdr != NULL)
10422 ext_size += esdi->rela.hdr->sh_size;
10424 if (ext_size > max_external_reloc_size)
10425 max_external_reloc_size = ext_size;
10426 if (sec->reloc_count > max_internal_reloc_count)
10427 max_internal_reloc_count = sec->reloc_count;
10432 if (reloc_count == 0)
10435 o->reloc_count += reloc_count;
10437 if (p->type == bfd_indirect_link_order
10438 && (info->relocatable || info->emitrelocations))
10441 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10442 if (esdi->rela.hdr)
10443 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10448 esdo->rela.count += reloc_count;
10450 esdo->rel.count += reloc_count;
10454 if (o->reloc_count > 0)
10455 o->flags |= SEC_RELOC;
10458 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10459 set it (this is probably a bug) and if it is set
10460 assign_section_numbers will create a reloc section. */
10461 o->flags &=~ SEC_RELOC;
10464 /* If the SEC_ALLOC flag is not set, force the section VMA to
10465 zero. This is done in elf_fake_sections as well, but forcing
10466 the VMA to 0 here will ensure that relocs against these
10467 sections are handled correctly. */
10468 if ((o->flags & SEC_ALLOC) == 0
10469 && ! o->user_set_vma)
10473 if (! info->relocatable && merged)
10474 elf_link_hash_traverse (elf_hash_table (info),
10475 _bfd_elf_link_sec_merge_syms, abfd);
10477 /* Figure out the file positions for everything but the symbol table
10478 and the relocs. We set symcount to force assign_section_numbers
10479 to create a symbol table. */
10480 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1;
10481 BFD_ASSERT (! abfd->output_has_begun);
10482 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10485 /* Set sizes, and assign file positions for reloc sections. */
10486 for (o = abfd->sections; o != NULL; o = o->next)
10488 struct bfd_elf_section_data *esdo = elf_section_data (o);
10489 if ((o->flags & SEC_RELOC) != 0)
10492 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10496 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10500 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10501 to count upwards while actually outputting the relocations. */
10502 esdo->rel.count = 0;
10503 esdo->rela.count = 0;
10506 _bfd_elf_assign_file_positions_for_relocs (abfd);
10508 /* We have now assigned file positions for all the sections except
10509 .symtab and .strtab. We start the .symtab section at the current
10510 file position, and write directly to it. We build the .strtab
10511 section in memory. */
10512 bfd_get_symcount (abfd) = 0;
10513 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
10514 /* sh_name is set in prep_headers. */
10515 symtab_hdr->sh_type = SHT_SYMTAB;
10516 /* sh_flags, sh_addr and sh_size all start off zero. */
10517 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
10518 /* sh_link is set in assign_section_numbers. */
10519 /* sh_info is set below. */
10520 /* sh_offset is set just below. */
10521 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
10523 off = elf_tdata (abfd)->next_file_pos;
10524 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
10526 /* Note that at this point elf_tdata (abfd)->next_file_pos is
10527 incorrect. We do not yet know the size of the .symtab section.
10528 We correct next_file_pos below, after we do know the size. */
10530 /* Allocate a buffer to hold swapped out symbols. This is to avoid
10531 continuously seeking to the right position in the file. */
10532 if (! info->keep_memory || max_sym_count < 20)
10533 finfo.symbuf_size = 20;
10535 finfo.symbuf_size = max_sym_count;
10536 amt = finfo.symbuf_size;
10537 amt *= bed->s->sizeof_sym;
10538 finfo.symbuf = (bfd_byte *) bfd_malloc (amt);
10539 if (finfo.symbuf == NULL)
10541 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
10543 /* Wild guess at number of output symbols. realloc'd as needed. */
10544 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000;
10545 finfo.shndxbuf_size = amt;
10546 amt *= sizeof (Elf_External_Sym_Shndx);
10547 finfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
10548 if (finfo.symshndxbuf == NULL)
10552 /* Start writing out the symbol table. The first symbol is always a
10554 if (info->strip != strip_all
10557 elfsym.st_value = 0;
10558 elfsym.st_size = 0;
10559 elfsym.st_info = 0;
10560 elfsym.st_other = 0;
10561 elfsym.st_shndx = SHN_UNDEF;
10562 elfsym.st_target_internal = 0;
10563 if (elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr,
10568 /* Output a symbol for each section. We output these even if we are
10569 discarding local symbols, since they are used for relocs. These
10570 symbols have no names. We store the index of each one in the
10571 index field of the section, so that we can find it again when
10572 outputting relocs. */
10573 if (info->strip != strip_all
10576 elfsym.st_size = 0;
10577 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10578 elfsym.st_other = 0;
10579 elfsym.st_value = 0;
10580 elfsym.st_target_internal = 0;
10581 for (i = 1; i < elf_numsections (abfd); i++)
10583 o = bfd_section_from_elf_index (abfd, i);
10586 o->target_index = bfd_get_symcount (abfd);
10587 elfsym.st_shndx = i;
10588 if (!info->relocatable)
10589 elfsym.st_value = o->vma;
10590 if (elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL) != 1)
10596 /* Allocate some memory to hold information read in from the input
10598 if (max_contents_size != 0)
10600 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
10601 if (finfo.contents == NULL)
10605 if (max_external_reloc_size != 0)
10607 finfo.external_relocs = bfd_malloc (max_external_reloc_size);
10608 if (finfo.external_relocs == NULL)
10612 if (max_internal_reloc_count != 0)
10614 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
10615 amt *= sizeof (Elf_Internal_Rela);
10616 finfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
10617 if (finfo.internal_relocs == NULL)
10621 if (max_sym_count != 0)
10623 amt = max_sym_count * bed->s->sizeof_sym;
10624 finfo.external_syms = (bfd_byte *) bfd_malloc (amt);
10625 if (finfo.external_syms == NULL)
10628 amt = max_sym_count * sizeof (Elf_Internal_Sym);
10629 finfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
10630 if (finfo.internal_syms == NULL)
10633 amt = max_sym_count * sizeof (long);
10634 finfo.indices = (long int *) bfd_malloc (amt);
10635 if (finfo.indices == NULL)
10638 amt = max_sym_count * sizeof (asection *);
10639 finfo.sections = (asection **) bfd_malloc (amt);
10640 if (finfo.sections == NULL)
10644 if (max_sym_shndx_count != 0)
10646 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
10647 finfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
10648 if (finfo.locsym_shndx == NULL)
10652 if (elf_hash_table (info)->tls_sec)
10654 bfd_vma base, end = 0;
10657 for (sec = elf_hash_table (info)->tls_sec;
10658 sec && (sec->flags & SEC_THREAD_LOCAL);
10661 bfd_size_type size = sec->size;
10664 && (sec->flags & SEC_HAS_CONTENTS) == 0)
10666 struct bfd_link_order *ord = sec->map_tail.link_order;
10669 size = ord->offset + ord->size;
10671 end = sec->vma + size;
10673 base = elf_hash_table (info)->tls_sec->vma;
10674 /* Only align end of TLS section if static TLS doesn't have special
10675 alignment requirements. */
10676 if (bed->static_tls_alignment == 1)
10677 end = align_power (end,
10678 elf_hash_table (info)->tls_sec->alignment_power);
10679 elf_hash_table (info)->tls_size = end - base;
10682 /* Reorder SHF_LINK_ORDER sections. */
10683 for (o = abfd->sections; o != NULL; o = o->next)
10685 if (!elf_fixup_link_order (abfd, o))
10689 /* Since ELF permits relocations to be against local symbols, we
10690 must have the local symbols available when we do the relocations.
10691 Since we would rather only read the local symbols once, and we
10692 would rather not keep them in memory, we handle all the
10693 relocations for a single input file at the same time.
10695 Unfortunately, there is no way to know the total number of local
10696 symbols until we have seen all of them, and the local symbol
10697 indices precede the global symbol indices. This means that when
10698 we are generating relocatable output, and we see a reloc against
10699 a global symbol, we can not know the symbol index until we have
10700 finished examining all the local symbols to see which ones we are
10701 going to output. To deal with this, we keep the relocations in
10702 memory, and don't output them until the end of the link. This is
10703 an unfortunate waste of memory, but I don't see a good way around
10704 it. Fortunately, it only happens when performing a relocatable
10705 link, which is not the common case. FIXME: If keep_memory is set
10706 we could write the relocs out and then read them again; I don't
10707 know how bad the memory loss will be. */
10709 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10710 sub->output_has_begun = FALSE;
10711 for (o = abfd->sections; o != NULL; o = o->next)
10713 for (p = o->map_head.link_order; p != NULL; p = p->next)
10715 if (p->type == bfd_indirect_link_order
10716 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
10717 == bfd_target_elf_flavour)
10718 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
10720 if (! sub->output_has_begun)
10722 if (! elf_link_input_bfd (&finfo, sub))
10724 sub->output_has_begun = TRUE;
10727 else if (p->type == bfd_section_reloc_link_order
10728 || p->type == bfd_symbol_reloc_link_order)
10730 if (! elf_reloc_link_order (abfd, info, o, p))
10735 if (! _bfd_default_link_order (abfd, info, o, p))
10737 if (p->type == bfd_indirect_link_order
10738 && (bfd_get_flavour (sub)
10739 == bfd_target_elf_flavour)
10740 && (elf_elfheader (sub)->e_ident[EI_CLASS]
10741 != bed->s->elfclass))
10743 const char *iclass, *oclass;
10745 if (bed->s->elfclass == ELFCLASS64)
10747 iclass = "ELFCLASS32";
10748 oclass = "ELFCLASS64";
10752 iclass = "ELFCLASS64";
10753 oclass = "ELFCLASS32";
10756 bfd_set_error (bfd_error_wrong_format);
10757 (*_bfd_error_handler)
10758 (_("%B: file class %s incompatible with %s"),
10759 sub, iclass, oclass);
10768 /* Free symbol buffer if needed. */
10769 if (!info->reduce_memory_overheads)
10771 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
10772 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10773 && elf_tdata (sub)->symbuf)
10775 free (elf_tdata (sub)->symbuf);
10776 elf_tdata (sub)->symbuf = NULL;
10780 /* Output any global symbols that got converted to local in a
10781 version script or due to symbol visibility. We do this in a
10782 separate step since ELF requires all local symbols to appear
10783 prior to any global symbols. FIXME: We should only do this if
10784 some global symbols were, in fact, converted to become local.
10785 FIXME: Will this work correctly with the Irix 5 linker? */
10786 eoinfo.failed = FALSE;
10787 eoinfo.finfo = &finfo;
10788 eoinfo.localsyms = TRUE;
10789 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10793 /* If backend needs to output some local symbols not present in the hash
10794 table, do it now. */
10795 if (bed->elf_backend_output_arch_local_syms)
10797 typedef int (*out_sym_func)
10798 (void *, const char *, Elf_Internal_Sym *, asection *,
10799 struct elf_link_hash_entry *);
10801 if (! ((*bed->elf_backend_output_arch_local_syms)
10802 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10806 /* That wrote out all the local symbols. Finish up the symbol table
10807 with the global symbols. Even if we want to strip everything we
10808 can, we still need to deal with those global symbols that got
10809 converted to local in a version script. */
10811 /* The sh_info field records the index of the first non local symbol. */
10812 symtab_hdr->sh_info = bfd_get_symcount (abfd);
10815 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr)
10817 Elf_Internal_Sym sym;
10818 bfd_byte *dynsym = finfo.dynsym_sec->contents;
10819 long last_local = 0;
10821 /* Write out the section symbols for the output sections. */
10822 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
10828 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
10830 sym.st_target_internal = 0;
10832 for (s = abfd->sections; s != NULL; s = s->next)
10838 dynindx = elf_section_data (s)->dynindx;
10841 indx = elf_section_data (s)->this_idx;
10842 BFD_ASSERT (indx > 0);
10843 sym.st_shndx = indx;
10844 if (! check_dynsym (abfd, &sym))
10846 sym.st_value = s->vma;
10847 dest = dynsym + dynindx * bed->s->sizeof_sym;
10848 if (last_local < dynindx)
10849 last_local = dynindx;
10850 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10854 /* Write out the local dynsyms. */
10855 if (elf_hash_table (info)->dynlocal)
10857 struct elf_link_local_dynamic_entry *e;
10858 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
10863 /* Copy the internal symbol and turn off visibility.
10864 Note that we saved a word of storage and overwrote
10865 the original st_name with the dynstr_index. */
10867 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10869 s = bfd_section_from_elf_index (e->input_bfd,
10874 elf_section_data (s->output_section)->this_idx;
10875 if (! check_dynsym (abfd, &sym))
10877 sym.st_value = (s->output_section->vma
10879 + e->isym.st_value);
10882 if (last_local < e->dynindx)
10883 last_local = e->dynindx;
10885 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
10886 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
10890 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info =
10894 /* We get the global symbols from the hash table. */
10895 eoinfo.failed = FALSE;
10896 eoinfo.localsyms = FALSE;
10897 eoinfo.finfo = &finfo;
10898 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
10902 /* If backend needs to output some symbols not present in the hash
10903 table, do it now. */
10904 if (bed->elf_backend_output_arch_syms)
10906 typedef int (*out_sym_func)
10907 (void *, const char *, Elf_Internal_Sym *, asection *,
10908 struct elf_link_hash_entry *);
10910 if (! ((*bed->elf_backend_output_arch_syms)
10911 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym)))
10915 /* Flush all symbols to the file. */
10916 if (! elf_link_flush_output_syms (&finfo, bed))
10919 /* Now we know the size of the symtab section. */
10920 off += symtab_hdr->sh_size;
10922 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
10923 if (symtab_shndx_hdr->sh_name != 0)
10925 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
10926 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
10927 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
10928 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
10929 symtab_shndx_hdr->sh_size = amt;
10931 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
10934 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
10935 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt))
10940 /* Finish up and write out the symbol string table (.strtab)
10942 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
10943 /* sh_name was set in prep_headers. */
10944 symstrtab_hdr->sh_type = SHT_STRTAB;
10945 symstrtab_hdr->sh_flags = 0;
10946 symstrtab_hdr->sh_addr = 0;
10947 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab);
10948 symstrtab_hdr->sh_entsize = 0;
10949 symstrtab_hdr->sh_link = 0;
10950 symstrtab_hdr->sh_info = 0;
10951 /* sh_offset is set just below. */
10952 symstrtab_hdr->sh_addralign = 1;
10954 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE);
10955 elf_tdata (abfd)->next_file_pos = off;
10957 if (bfd_get_symcount (abfd) > 0)
10959 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
10960 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab))
10964 /* Adjust the relocs to have the correct symbol indices. */
10965 for (o = abfd->sections; o != NULL; o = o->next)
10967 struct bfd_elf_section_data *esdo = elf_section_data (o);
10968 if ((o->flags & SEC_RELOC) == 0)
10971 if (esdo->rel.hdr != NULL)
10972 elf_link_adjust_relocs (abfd, &esdo->rel);
10973 if (esdo->rela.hdr != NULL)
10974 elf_link_adjust_relocs (abfd, &esdo->rela);
10976 /* Set the reloc_count field to 0 to prevent write_relocs from
10977 trying to swap the relocs out itself. */
10978 o->reloc_count = 0;
10981 if (dynamic && info->combreloc && dynobj != NULL)
10982 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
10984 /* If we are linking against a dynamic object, or generating a
10985 shared library, finish up the dynamic linking information. */
10988 bfd_byte *dyncon, *dynconend;
10990 /* Fix up .dynamic entries. */
10991 o = bfd_get_section_by_name (dynobj, ".dynamic");
10992 BFD_ASSERT (o != NULL);
10994 dyncon = o->contents;
10995 dynconend = o->contents + o->size;
10996 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
10998 Elf_Internal_Dyn dyn;
11002 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11009 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11011 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11013 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11014 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11017 dyn.d_un.d_val = relativecount;
11024 name = info->init_function;
11027 name = info->fini_function;
11030 struct elf_link_hash_entry *h;
11032 h = elf_link_hash_lookup (elf_hash_table (info), name,
11033 FALSE, FALSE, TRUE);
11035 && (h->root.type == bfd_link_hash_defined
11036 || h->root.type == bfd_link_hash_defweak))
11038 dyn.d_un.d_ptr = h->root.u.def.value;
11039 o = h->root.u.def.section;
11040 if (o->output_section != NULL)
11041 dyn.d_un.d_ptr += (o->output_section->vma
11042 + o->output_offset);
11045 /* The symbol is imported from another shared
11046 library and does not apply to this one. */
11047 dyn.d_un.d_ptr = 0;
11054 case DT_PREINIT_ARRAYSZ:
11055 name = ".preinit_array";
11057 case DT_INIT_ARRAYSZ:
11058 name = ".init_array";
11060 case DT_FINI_ARRAYSZ:
11061 name = ".fini_array";
11063 o = bfd_get_section_by_name (abfd, name);
11066 (*_bfd_error_handler)
11067 (_("%B: could not find output section %s"), abfd, name);
11071 (*_bfd_error_handler)
11072 (_("warning: %s section has zero size"), name);
11073 dyn.d_un.d_val = o->size;
11076 case DT_PREINIT_ARRAY:
11077 name = ".preinit_array";
11079 case DT_INIT_ARRAY:
11080 name = ".init_array";
11082 case DT_FINI_ARRAY:
11083 name = ".fini_array";
11090 name = ".gnu.hash";
11099 name = ".gnu.version_d";
11102 name = ".gnu.version_r";
11105 name = ".gnu.version";
11107 o = bfd_get_section_by_name (abfd, name);
11110 (*_bfd_error_handler)
11111 (_("%B: could not find output section %s"), abfd, name);
11114 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11116 (*_bfd_error_handler)
11117 (_("warning: section '%s' is being made into a note"), name);
11118 bfd_set_error (bfd_error_nonrepresentable_section);
11121 dyn.d_un.d_ptr = o->vma;
11128 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11132 dyn.d_un.d_val = 0;
11133 dyn.d_un.d_ptr = 0;
11134 for (i = 1; i < elf_numsections (abfd); i++)
11136 Elf_Internal_Shdr *hdr;
11138 hdr = elf_elfsections (abfd)[i];
11139 if (hdr->sh_type == type
11140 && (hdr->sh_flags & SHF_ALLOC) != 0)
11142 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11143 dyn.d_un.d_val += hdr->sh_size;
11146 if (dyn.d_un.d_ptr == 0
11147 || hdr->sh_addr < dyn.d_un.d_ptr)
11148 dyn.d_un.d_ptr = hdr->sh_addr;
11154 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11158 /* If we have created any dynamic sections, then output them. */
11159 if (dynobj != NULL)
11161 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11164 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11165 if (info->warn_shared_textrel && info->shared)
11167 bfd_byte *dyncon, *dynconend;
11169 /* Fix up .dynamic entries. */
11170 o = bfd_get_section_by_name (dynobj, ".dynamic");
11171 BFD_ASSERT (o != NULL);
11173 dyncon = o->contents;
11174 dynconend = o->contents + o->size;
11175 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11177 Elf_Internal_Dyn dyn;
11179 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11181 if (dyn.d_tag == DT_TEXTREL)
11183 info->callbacks->einfo
11184 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11190 for (o = dynobj->sections; o != NULL; o = o->next)
11192 if ((o->flags & SEC_HAS_CONTENTS) == 0
11194 || o->output_section == bfd_abs_section_ptr)
11196 if ((o->flags & SEC_LINKER_CREATED) == 0)
11198 /* At this point, we are only interested in sections
11199 created by _bfd_elf_link_create_dynamic_sections. */
11202 if (elf_hash_table (info)->stab_info.stabstr == o)
11204 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11206 if ((elf_section_data (o->output_section)->this_hdr.sh_type
11208 && (strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0))
11210 /* FIXME: octets_per_byte. */
11211 if (! bfd_set_section_contents (abfd, o->output_section,
11213 (file_ptr) o->output_offset,
11219 /* The contents of the .dynstr section are actually in a
11221 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11222 if (bfd_seek (abfd, off, SEEK_SET) != 0
11223 || ! _bfd_elf_strtab_emit (abfd,
11224 elf_hash_table (info)->dynstr))
11230 if (info->relocatable)
11232 bfd_boolean failed = FALSE;
11234 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11239 /* If we have optimized stabs strings, output them. */
11240 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11242 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11246 if (info->eh_frame_hdr)
11248 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11252 if (finfo.symstrtab != NULL)
11253 _bfd_stringtab_free (finfo.symstrtab);
11254 if (finfo.contents != NULL)
11255 free (finfo.contents);
11256 if (finfo.external_relocs != NULL)
11257 free (finfo.external_relocs);
11258 if (finfo.internal_relocs != NULL)
11259 free (finfo.internal_relocs);
11260 if (finfo.external_syms != NULL)
11261 free (finfo.external_syms);
11262 if (finfo.locsym_shndx != NULL)
11263 free (finfo.locsym_shndx);
11264 if (finfo.internal_syms != NULL)
11265 free (finfo.internal_syms);
11266 if (finfo.indices != NULL)
11267 free (finfo.indices);
11268 if (finfo.sections != NULL)
11269 free (finfo.sections);
11270 if (finfo.symbuf != NULL)
11271 free (finfo.symbuf);
11272 if (finfo.symshndxbuf != NULL)
11273 free (finfo.symshndxbuf);
11274 for (o = abfd->sections; o != NULL; o = o->next)
11276 struct bfd_elf_section_data *esdo = elf_section_data (o);
11277 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11278 free (esdo->rel.hashes);
11279 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11280 free (esdo->rela.hashes);
11283 elf_tdata (abfd)->linker = TRUE;
11287 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11288 if (contents == NULL)
11289 return FALSE; /* Bail out and fail. */
11290 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11291 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11298 if (finfo.symstrtab != NULL)
11299 _bfd_stringtab_free (finfo.symstrtab);
11300 if (finfo.contents != NULL)
11301 free (finfo.contents);
11302 if (finfo.external_relocs != NULL)
11303 free (finfo.external_relocs);
11304 if (finfo.internal_relocs != NULL)
11305 free (finfo.internal_relocs);
11306 if (finfo.external_syms != NULL)
11307 free (finfo.external_syms);
11308 if (finfo.locsym_shndx != NULL)
11309 free (finfo.locsym_shndx);
11310 if (finfo.internal_syms != NULL)
11311 free (finfo.internal_syms);
11312 if (finfo.indices != NULL)
11313 free (finfo.indices);
11314 if (finfo.sections != NULL)
11315 free (finfo.sections);
11316 if (finfo.symbuf != NULL)
11317 free (finfo.symbuf);
11318 if (finfo.symshndxbuf != NULL)
11319 free (finfo.symshndxbuf);
11320 for (o = abfd->sections; o != NULL; o = o->next)
11322 struct bfd_elf_section_data *esdo = elf_section_data (o);
11323 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11324 free (esdo->rel.hashes);
11325 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11326 free (esdo->rela.hashes);
11332 /* Initialize COOKIE for input bfd ABFD. */
11335 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11336 struct bfd_link_info *info, bfd *abfd)
11338 Elf_Internal_Shdr *symtab_hdr;
11339 const struct elf_backend_data *bed;
11341 bed = get_elf_backend_data (abfd);
11342 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11344 cookie->abfd = abfd;
11345 cookie->sym_hashes = elf_sym_hashes (abfd);
11346 cookie->bad_symtab = elf_bad_symtab (abfd);
11347 if (cookie->bad_symtab)
11349 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11350 cookie->extsymoff = 0;
11354 cookie->locsymcount = symtab_hdr->sh_info;
11355 cookie->extsymoff = symtab_hdr->sh_info;
11358 if (bed->s->arch_size == 32)
11359 cookie->r_sym_shift = 8;
11361 cookie->r_sym_shift = 32;
11363 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11364 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11366 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11367 cookie->locsymcount, 0,
11369 if (cookie->locsyms == NULL)
11371 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11374 if (info->keep_memory)
11375 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11380 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11383 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11385 Elf_Internal_Shdr *symtab_hdr;
11387 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11388 if (cookie->locsyms != NULL
11389 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11390 free (cookie->locsyms);
11393 /* Initialize the relocation information in COOKIE for input section SEC
11394 of input bfd ABFD. */
11397 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11398 struct bfd_link_info *info, bfd *abfd,
11401 const struct elf_backend_data *bed;
11403 if (sec->reloc_count == 0)
11405 cookie->rels = NULL;
11406 cookie->relend = NULL;
11410 bed = get_elf_backend_data (abfd);
11412 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11413 info->keep_memory);
11414 if (cookie->rels == NULL)
11416 cookie->rel = cookie->rels;
11417 cookie->relend = (cookie->rels
11418 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11420 cookie->rel = cookie->rels;
11424 /* Free the memory allocated by init_reloc_cookie_rels,
11428 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11431 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11432 free (cookie->rels);
11435 /* Initialize the whole of COOKIE for input section SEC. */
11438 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11439 struct bfd_link_info *info,
11442 if (!init_reloc_cookie (cookie, info, sec->owner))
11444 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11449 fini_reloc_cookie (cookie, sec->owner);
11454 /* Free the memory allocated by init_reloc_cookie_for_section,
11458 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11461 fini_reloc_cookie_rels (cookie, sec);
11462 fini_reloc_cookie (cookie, sec->owner);
11465 /* Garbage collect unused sections. */
11467 /* Default gc_mark_hook. */
11470 _bfd_elf_gc_mark_hook (asection *sec,
11471 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11472 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11473 struct elf_link_hash_entry *h,
11474 Elf_Internal_Sym *sym)
11476 const char *sec_name;
11480 switch (h->root.type)
11482 case bfd_link_hash_defined:
11483 case bfd_link_hash_defweak:
11484 return h->root.u.def.section;
11486 case bfd_link_hash_common:
11487 return h->root.u.c.p->section;
11489 case bfd_link_hash_undefined:
11490 case bfd_link_hash_undefweak:
11491 /* To work around a glibc bug, keep all XXX input sections
11492 when there is an as yet undefined reference to __start_XXX
11493 or __stop_XXX symbols. The linker will later define such
11494 symbols for orphan input sections that have a name
11495 representable as a C identifier. */
11496 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11497 sec_name = h->root.root.string + 8;
11498 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11499 sec_name = h->root.root.string + 7;
11503 if (sec_name && *sec_name != '\0')
11507 for (i = info->input_bfds; i; i = i->link_next)
11509 sec = bfd_get_section_by_name (i, sec_name);
11511 sec->flags |= SEC_KEEP;
11521 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11526 /* COOKIE->rel describes a relocation against section SEC, which is
11527 a section we've decided to keep. Return the section that contains
11528 the relocation symbol, or NULL if no section contains it. */
11531 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11532 elf_gc_mark_hook_fn gc_mark_hook,
11533 struct elf_reloc_cookie *cookie)
11535 unsigned long r_symndx;
11536 struct elf_link_hash_entry *h;
11538 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11539 if (r_symndx == STN_UNDEF)
11542 if (r_symndx >= cookie->locsymcount
11543 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11545 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11546 while (h->root.type == bfd_link_hash_indirect
11547 || h->root.type == bfd_link_hash_warning)
11548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11549 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
11552 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
11553 &cookie->locsyms[r_symndx]);
11556 /* COOKIE->rel describes a relocation against section SEC, which is
11557 a section we've decided to keep. Mark the section that contains
11558 the relocation symbol. */
11561 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
11563 elf_gc_mark_hook_fn gc_mark_hook,
11564 struct elf_reloc_cookie *cookie)
11568 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
11569 if (rsec && !rsec->gc_mark)
11571 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour)
11573 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
11579 /* The mark phase of garbage collection. For a given section, mark
11580 it and any sections in this section's group, and all the sections
11581 which define symbols to which it refers. */
11584 _bfd_elf_gc_mark (struct bfd_link_info *info,
11586 elf_gc_mark_hook_fn gc_mark_hook)
11589 asection *group_sec, *eh_frame;
11593 /* Mark all the sections in the group. */
11594 group_sec = elf_section_data (sec)->next_in_group;
11595 if (group_sec && !group_sec->gc_mark)
11596 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
11599 /* Look through the section relocs. */
11601 eh_frame = elf_eh_frame_section (sec->owner);
11602 if ((sec->flags & SEC_RELOC) != 0
11603 && sec->reloc_count > 0
11604 && sec != eh_frame)
11606 struct elf_reloc_cookie cookie;
11608 if (!init_reloc_cookie_for_section (&cookie, info, sec))
11612 for (; cookie.rel < cookie.relend; cookie.rel++)
11613 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
11618 fini_reloc_cookie_for_section (&cookie, sec);
11622 if (ret && eh_frame && elf_fde_list (sec))
11624 struct elf_reloc_cookie cookie;
11626 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
11630 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
11631 gc_mark_hook, &cookie))
11633 fini_reloc_cookie_for_section (&cookie, eh_frame);
11640 /* Keep debug and special sections. */
11643 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
11644 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
11648 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11651 bfd_boolean some_kept;
11653 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
11656 /* Ensure all linker created sections are kept, and see whether
11657 any other section is already marked. */
11659 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11661 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11663 else if (isec->gc_mark)
11667 /* If no section in this file will be kept, then we can
11668 toss out debug sections. */
11672 /* Keep debug and special sections like .comment when they are
11673 not part of a group, or when we have single-member groups. */
11674 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
11675 if ((elf_next_in_group (isec) == NULL
11676 || elf_next_in_group (isec) == isec)
11677 && ((isec->flags & SEC_DEBUGGING) != 0
11678 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0))
11684 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
11686 struct elf_gc_sweep_symbol_info
11688 struct bfd_link_info *info;
11689 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
11694 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
11696 if (((h->root.type == bfd_link_hash_defined
11697 || h->root.type == bfd_link_hash_defweak)
11698 && !h->root.u.def.section->gc_mark
11699 && (!(h->root.u.def.section->owner->flags & DYNAMIC)
11700 || (h->plt.refcount <= 0
11701 && h->got.refcount <= 0)))
11702 || (h->root.type == bfd_link_hash_undefined
11703 && h->plt.refcount <= 0
11704 && h->got.refcount <= 0))
11706 struct elf_gc_sweep_symbol_info *inf =
11707 (struct elf_gc_sweep_symbol_info *) data;
11708 (*inf->hide_symbol) (inf->info, h, TRUE);
11714 /* The sweep phase of garbage collection. Remove all garbage sections. */
11716 typedef bfd_boolean (*gc_sweep_hook_fn)
11717 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
11720 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
11723 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11724 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
11725 unsigned long section_sym_count;
11726 struct elf_gc_sweep_symbol_info sweep_info;
11728 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11732 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
11735 for (o = sub->sections; o != NULL; o = o->next)
11737 /* When any section in a section group is kept, we keep all
11738 sections in the section group. If the first member of
11739 the section group is excluded, we will also exclude the
11741 if (o->flags & SEC_GROUP)
11743 asection *first = elf_next_in_group (o);
11744 o->gc_mark = first->gc_mark;
11750 /* Skip sweeping sections already excluded. */
11751 if (o->flags & SEC_EXCLUDE)
11754 /* Since this is early in the link process, it is simple
11755 to remove a section from the output. */
11756 o->flags |= SEC_EXCLUDE;
11758 if (info->print_gc_sections && o->size != 0)
11759 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
11761 /* But we also have to update some of the relocation
11762 info we collected before. */
11764 && (o->flags & SEC_RELOC) != 0
11765 && o->reloc_count > 0
11766 && !bfd_is_abs_section (o->output_section))
11768 Elf_Internal_Rela *internal_relocs;
11772 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
11773 info->keep_memory);
11774 if (internal_relocs == NULL)
11777 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
11779 if (elf_section_data (o)->relocs != internal_relocs)
11780 free (internal_relocs);
11788 /* Remove the symbols that were in the swept sections from the dynamic
11789 symbol table. GCFIXME: Anyone know how to get them out of the
11790 static symbol table as well? */
11791 sweep_info.info = info;
11792 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
11793 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
11796 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
11800 /* Propagate collected vtable information. This is called through
11801 elf_link_hash_traverse. */
11804 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
11806 /* Those that are not vtables. */
11807 if (h->vtable == NULL || h->vtable->parent == NULL)
11810 /* Those vtables that do not have parents, we cannot merge. */
11811 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
11814 /* If we've already been done, exit. */
11815 if (h->vtable->used && h->vtable->used[-1])
11818 /* Make sure the parent's table is up to date. */
11819 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
11821 if (h->vtable->used == NULL)
11823 /* None of this table's entries were referenced. Re-use the
11825 h->vtable->used = h->vtable->parent->vtable->used;
11826 h->vtable->size = h->vtable->parent->vtable->size;
11831 bfd_boolean *cu, *pu;
11833 /* Or the parent's entries into ours. */
11834 cu = h->vtable->used;
11836 pu = h->vtable->parent->vtable->used;
11839 const struct elf_backend_data *bed;
11840 unsigned int log_file_align;
11842 bed = get_elf_backend_data (h->root.u.def.section->owner);
11843 log_file_align = bed->s->log_file_align;
11844 n = h->vtable->parent->vtable->size >> log_file_align;
11859 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
11862 bfd_vma hstart, hend;
11863 Elf_Internal_Rela *relstart, *relend, *rel;
11864 const struct elf_backend_data *bed;
11865 unsigned int log_file_align;
11867 /* Take care of both those symbols that do not describe vtables as
11868 well as those that are not loaded. */
11869 if (h->vtable == NULL || h->vtable->parent == NULL)
11872 BFD_ASSERT (h->root.type == bfd_link_hash_defined
11873 || h->root.type == bfd_link_hash_defweak);
11875 sec = h->root.u.def.section;
11876 hstart = h->root.u.def.value;
11877 hend = hstart + h->size;
11879 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
11881 return *(bfd_boolean *) okp = FALSE;
11882 bed = get_elf_backend_data (sec->owner);
11883 log_file_align = bed->s->log_file_align;
11885 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
11887 for (rel = relstart; rel < relend; ++rel)
11888 if (rel->r_offset >= hstart && rel->r_offset < hend)
11890 /* If the entry is in use, do nothing. */
11891 if (h->vtable->used
11892 && (rel->r_offset - hstart) < h->vtable->size)
11894 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
11895 if (h->vtable->used[entry])
11898 /* Otherwise, kill it. */
11899 rel->r_offset = rel->r_info = rel->r_addend = 0;
11905 /* Mark sections containing dynamically referenced symbols. When
11906 building shared libraries, we must assume that any visible symbol is
11910 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
11912 struct bfd_link_info *info = (struct bfd_link_info *) inf;
11914 if ((h->root.type == bfd_link_hash_defined
11915 || h->root.type == bfd_link_hash_defweak)
11917 || (!info->executable
11919 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
11920 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
11921 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
11922 || !bfd_hide_sym_by_version (info->version_info,
11923 h->root.root.string)))))
11924 h->root.u.def.section->flags |= SEC_KEEP;
11929 /* Keep all sections containing symbols undefined on the command-line,
11930 and the section containing the entry symbol. */
11933 _bfd_elf_gc_keep (struct bfd_link_info *info)
11935 struct bfd_sym_chain *sym;
11937 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
11939 struct elf_link_hash_entry *h;
11941 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
11942 FALSE, FALSE, FALSE);
11945 && (h->root.type == bfd_link_hash_defined
11946 || h->root.type == bfd_link_hash_defweak)
11947 && !bfd_is_abs_section (h->root.u.def.section))
11948 h->root.u.def.section->flags |= SEC_KEEP;
11952 /* Do mark and sweep of unused sections. */
11955 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
11957 bfd_boolean ok = TRUE;
11959 elf_gc_mark_hook_fn gc_mark_hook;
11960 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11962 if (!bed->can_gc_sections
11963 || !is_elf_hash_table (info->hash))
11965 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
11969 bed->gc_keep (info);
11971 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
11972 at the .eh_frame section if we can mark the FDEs individually. */
11973 _bfd_elf_begin_eh_frame_parsing (info);
11974 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
11977 struct elf_reloc_cookie cookie;
11979 sec = bfd_get_section_by_name (sub, ".eh_frame");
11980 if (sec && init_reloc_cookie_for_section (&cookie, info, sec))
11982 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
11983 if (elf_section_data (sec)->sec_info)
11984 elf_eh_frame_section (sub) = sec;
11985 fini_reloc_cookie_for_section (&cookie, sec);
11988 _bfd_elf_end_eh_frame_parsing (info);
11990 /* Apply transitive closure to the vtable entry usage info. */
11991 elf_link_hash_traverse (elf_hash_table (info),
11992 elf_gc_propagate_vtable_entries_used,
11997 /* Kill the vtable relocations that were not used. */
11998 elf_link_hash_traverse (elf_hash_table (info),
11999 elf_gc_smash_unused_vtentry_relocs,
12004 /* Mark dynamically referenced symbols. */
12005 if (elf_hash_table (info)->dynamic_sections_created)
12006 elf_link_hash_traverse (elf_hash_table (info),
12007 bed->gc_mark_dynamic_ref,
12010 /* Grovel through relocs to find out who stays ... */
12011 gc_mark_hook = bed->gc_mark_hook;
12012 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
12016 if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
12019 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12020 Also treat note sections as a root, if the section is not part
12022 for (o = sub->sections; o != NULL; o = o->next)
12024 && (o->flags & SEC_EXCLUDE) == 0
12025 && ((o->flags & SEC_KEEP) != 0
12026 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12027 && elf_next_in_group (o) == NULL )))
12029 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12034 /* Allow the backend to mark additional target specific sections. */
12035 bed->gc_mark_extra_sections (info, gc_mark_hook);
12037 /* ... and mark SEC_EXCLUDE for those that go. */
12038 return elf_gc_sweep (abfd, info);
12041 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12044 bfd_elf_gc_record_vtinherit (bfd *abfd,
12046 struct elf_link_hash_entry *h,
12049 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12050 struct elf_link_hash_entry **search, *child;
12051 bfd_size_type extsymcount;
12052 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12054 /* The sh_info field of the symtab header tells us where the
12055 external symbols start. We don't care about the local symbols at
12057 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12058 if (!elf_bad_symtab (abfd))
12059 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12061 sym_hashes = elf_sym_hashes (abfd);
12062 sym_hashes_end = sym_hashes + extsymcount;
12064 /* Hunt down the child symbol, which is in this section at the same
12065 offset as the relocation. */
12066 for (search = sym_hashes; search != sym_hashes_end; ++search)
12068 if ((child = *search) != NULL
12069 && (child->root.type == bfd_link_hash_defined
12070 || child->root.type == bfd_link_hash_defweak)
12071 && child->root.u.def.section == sec
12072 && child->root.u.def.value == offset)
12076 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12077 abfd, sec, (unsigned long) offset);
12078 bfd_set_error (bfd_error_invalid_operation);
12082 if (!child->vtable)
12084 child->vtable = (struct elf_link_virtual_table_entry *)
12085 bfd_zalloc (abfd, sizeof (*child->vtable));
12086 if (!child->vtable)
12091 /* This *should* only be the absolute section. It could potentially
12092 be that someone has defined a non-global vtable though, which
12093 would be bad. It isn't worth paging in the local symbols to be
12094 sure though; that case should simply be handled by the assembler. */
12096 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12099 child->vtable->parent = h;
12104 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12107 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12108 asection *sec ATTRIBUTE_UNUSED,
12109 struct elf_link_hash_entry *h,
12112 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12113 unsigned int log_file_align = bed->s->log_file_align;
12117 h->vtable = (struct elf_link_virtual_table_entry *)
12118 bfd_zalloc (abfd, sizeof (*h->vtable));
12123 if (addend >= h->vtable->size)
12125 size_t size, bytes, file_align;
12126 bfd_boolean *ptr = h->vtable->used;
12128 /* While the symbol is undefined, we have to be prepared to handle
12130 file_align = 1 << log_file_align;
12131 if (h->root.type == bfd_link_hash_undefined)
12132 size = addend + file_align;
12136 if (addend >= size)
12138 /* Oops! We've got a reference past the defined end of
12139 the table. This is probably a bug -- shall we warn? */
12140 size = addend + file_align;
12143 size = (size + file_align - 1) & -file_align;
12145 /* Allocate one extra entry for use as a "done" flag for the
12146 consolidation pass. */
12147 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12151 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12157 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12158 * sizeof (bfd_boolean));
12159 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12163 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12168 /* And arrange for that done flag to be at index -1. */
12169 h->vtable->used = ptr + 1;
12170 h->vtable->size = size;
12173 h->vtable->used[addend >> log_file_align] = TRUE;
12178 /* Map an ELF section header flag to its corresponding string. */
12182 flagword flag_value;
12183 } elf_flags_to_name_table;
12185 static elf_flags_to_name_table elf_flags_to_names [] =
12187 { "SHF_WRITE", SHF_WRITE },
12188 { "SHF_ALLOC", SHF_ALLOC },
12189 { "SHF_EXECINSTR", SHF_EXECINSTR },
12190 { "SHF_MERGE", SHF_MERGE },
12191 { "SHF_STRINGS", SHF_STRINGS },
12192 { "SHF_INFO_LINK", SHF_INFO_LINK},
12193 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12194 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12195 { "SHF_GROUP", SHF_GROUP },
12196 { "SHF_TLS", SHF_TLS },
12197 { "SHF_MASKOS", SHF_MASKOS },
12198 { "SHF_EXCLUDE", SHF_EXCLUDE },
12202 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12203 struct flag_info *finfo)
12205 bfd *output_bfd = info->output_bfd;
12206 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
12207 struct flag_info_list *tf = finfo->flag_list;
12209 int without_hex = 0;
12211 for (tf = finfo->flag_list; tf != NULL; tf = tf->next)
12214 if (bed->elf_backend_lookup_section_flags_hook)
12217 (*bed->elf_backend_lookup_section_flags_hook) ((char *) tf->name);
12221 if (tf->with == with_flags)
12222 with_hex |= hexval;
12223 else if (tf->with == without_flags)
12224 without_hex |= hexval;
12229 for (i = 0; i < 12; i++)
12231 if (!strcmp (tf->name, elf_flags_to_names[i].flag_name))
12233 if (tf->with == with_flags)
12234 with_hex |= elf_flags_to_names[i].flag_value;
12235 else if (tf->with == without_flags)
12236 without_hex |= elf_flags_to_names[i].flag_value;
12241 if (tf->valid == FALSE)
12243 info->callbacks->einfo
12244 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12248 finfo->flags_initialized = TRUE;
12249 finfo->only_with_flags |= with_hex;
12250 finfo->not_with_flags |= without_hex;
12255 struct alloc_got_off_arg {
12257 struct bfd_link_info *info;
12260 /* We need a special top-level link routine to convert got reference counts
12261 to real got offsets. */
12264 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12266 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12267 bfd *obfd = gofarg->info->output_bfd;
12268 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12270 if (h->got.refcount > 0)
12272 h->got.offset = gofarg->gotoff;
12273 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12276 h->got.offset = (bfd_vma) -1;
12281 /* And an accompanying bit to work out final got entry offsets once
12282 we're done. Should be called from final_link. */
12285 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12286 struct bfd_link_info *info)
12289 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12291 struct alloc_got_off_arg gofarg;
12293 BFD_ASSERT (abfd == info->output_bfd);
12295 if (! is_elf_hash_table (info->hash))
12298 /* The GOT offset is relative to the .got section, but the GOT header is
12299 put into the .got.plt section, if the backend uses it. */
12300 if (bed->want_got_plt)
12303 gotoff = bed->got_header_size;
12305 /* Do the local .got entries first. */
12306 for (i = info->input_bfds; i; i = i->link_next)
12308 bfd_signed_vma *local_got;
12309 bfd_size_type j, locsymcount;
12310 Elf_Internal_Shdr *symtab_hdr;
12312 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12315 local_got = elf_local_got_refcounts (i);
12319 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12320 if (elf_bad_symtab (i))
12321 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12323 locsymcount = symtab_hdr->sh_info;
12325 for (j = 0; j < locsymcount; ++j)
12327 if (local_got[j] > 0)
12329 local_got[j] = gotoff;
12330 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12333 local_got[j] = (bfd_vma) -1;
12337 /* Then the global .got entries. .plt refcounts are handled by
12338 adjust_dynamic_symbol */
12339 gofarg.gotoff = gotoff;
12340 gofarg.info = info;
12341 elf_link_hash_traverse (elf_hash_table (info),
12342 elf_gc_allocate_got_offsets,
12347 /* Many folk need no more in the way of final link than this, once
12348 got entry reference counting is enabled. */
12351 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12353 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12356 /* Invoke the regular ELF backend linker to do all the work. */
12357 return bfd_elf_final_link (abfd, info);
12361 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12363 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12365 if (rcookie->bad_symtab)
12366 rcookie->rel = rcookie->rels;
12368 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12370 unsigned long r_symndx;
12372 if (! rcookie->bad_symtab)
12373 if (rcookie->rel->r_offset > offset)
12375 if (rcookie->rel->r_offset != offset)
12378 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12379 if (r_symndx == STN_UNDEF)
12382 if (r_symndx >= rcookie->locsymcount
12383 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12385 struct elf_link_hash_entry *h;
12387 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12389 while (h->root.type == bfd_link_hash_indirect
12390 || h->root.type == bfd_link_hash_warning)
12391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12393 if ((h->root.type == bfd_link_hash_defined
12394 || h->root.type == bfd_link_hash_defweak)
12395 && elf_discarded_section (h->root.u.def.section))
12402 /* It's not a relocation against a global symbol,
12403 but it could be a relocation against a local
12404 symbol for a discarded section. */
12406 Elf_Internal_Sym *isym;
12408 /* Need to: get the symbol; get the section. */
12409 isym = &rcookie->locsyms[r_symndx];
12410 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
12411 if (isec != NULL && elf_discarded_section (isec))
12419 /* Discard unneeded references to discarded sections.
12420 Returns TRUE if any section's size was changed. */
12421 /* This function assumes that the relocations are in sorted order,
12422 which is true for all known assemblers. */
12425 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
12427 struct elf_reloc_cookie cookie;
12428 asection *stab, *eh;
12429 const struct elf_backend_data *bed;
12431 bfd_boolean ret = FALSE;
12433 if (info->traditional_format
12434 || !is_elf_hash_table (info->hash))
12437 _bfd_elf_begin_eh_frame_parsing (info);
12438 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
12440 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
12443 bed = get_elf_backend_data (abfd);
12445 if ((abfd->flags & DYNAMIC) != 0)
12449 if (!info->relocatable)
12451 eh = bfd_get_section_by_name (abfd, ".eh_frame");
12454 || bfd_is_abs_section (eh->output_section)))
12458 stab = bfd_get_section_by_name (abfd, ".stab");
12460 && (stab->size == 0
12461 || bfd_is_abs_section (stab->output_section)
12462 || stab->sec_info_type != ELF_INFO_TYPE_STABS))
12467 && bed->elf_backend_discard_info == NULL)
12470 if (!init_reloc_cookie (&cookie, info, abfd))
12474 && stab->reloc_count > 0
12475 && init_reloc_cookie_rels (&cookie, info, abfd, stab))
12477 if (_bfd_discard_section_stabs (abfd, stab,
12478 elf_section_data (stab)->sec_info,
12479 bfd_elf_reloc_symbol_deleted_p,
12482 fini_reloc_cookie_rels (&cookie, stab);
12486 && init_reloc_cookie_rels (&cookie, info, abfd, eh))
12488 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie);
12489 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh,
12490 bfd_elf_reloc_symbol_deleted_p,
12493 fini_reloc_cookie_rels (&cookie, eh);
12496 if (bed->elf_backend_discard_info != NULL
12497 && (*bed->elf_backend_discard_info) (abfd, &cookie, info))
12500 fini_reloc_cookie (&cookie, abfd);
12502 _bfd_elf_end_eh_frame_parsing (info);
12504 if (info->eh_frame_hdr
12505 && !info->relocatable
12506 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
12513 _bfd_elf_section_already_linked (bfd *abfd,
12515 struct bfd_link_info *info)
12518 const char *name, *key;
12519 struct bfd_section_already_linked *l;
12520 struct bfd_section_already_linked_hash_entry *already_linked_list;
12522 if (sec->output_section == bfd_abs_section_ptr)
12525 flags = sec->flags;
12527 /* Return if it isn't a linkonce section. A comdat group section
12528 also has SEC_LINK_ONCE set. */
12529 if ((flags & SEC_LINK_ONCE) == 0)
12532 /* Don't put group member sections on our list of already linked
12533 sections. They are handled as a group via their group section. */
12534 if (elf_sec_group (sec) != NULL)
12537 /* For a SHT_GROUP section, use the group signature as the key. */
12539 if ((flags & SEC_GROUP) != 0
12540 && elf_next_in_group (sec) != NULL
12541 && elf_group_name (elf_next_in_group (sec)) != NULL)
12542 key = elf_group_name (elf_next_in_group (sec));
12545 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
12546 if (CONST_STRNEQ (name, ".gnu.linkonce.")
12547 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
12550 /* Must be a user linkonce section that doesn't follow gcc's
12551 naming convention. In this case we won't be matching
12552 single member groups. */
12556 already_linked_list = bfd_section_already_linked_table_lookup (key);
12558 for (l = already_linked_list->entry; l != NULL; l = l->next)
12560 /* We may have 2 different types of sections on the list: group
12561 sections with a signature of <key> (<key> is some string),
12562 and linkonce sections named .gnu.linkonce.<type>.<key>.
12563 Match like sections. LTO plugin sections are an exception.
12564 They are always named .gnu.linkonce.t.<key> and match either
12565 type of section. */
12566 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
12567 && ((flags & SEC_GROUP) != 0
12568 || strcmp (name, l->sec->name) == 0))
12569 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
12571 /* The section has already been linked. See if we should
12572 issue a warning. */
12573 if (!_bfd_handle_already_linked (sec, l, info))
12576 if (flags & SEC_GROUP)
12578 asection *first = elf_next_in_group (sec);
12579 asection *s = first;
12583 s->output_section = bfd_abs_section_ptr;
12584 /* Record which group discards it. */
12585 s->kept_section = l->sec;
12586 s = elf_next_in_group (s);
12587 /* These lists are circular. */
12597 /* A single member comdat group section may be discarded by a
12598 linkonce section and vice versa. */
12599 if ((flags & SEC_GROUP) != 0)
12601 asection *first = elf_next_in_group (sec);
12603 if (first != NULL && elf_next_in_group (first) == first)
12604 /* Check this single member group against linkonce sections. */
12605 for (l = already_linked_list->entry; l != NULL; l = l->next)
12606 if ((l->sec->flags & SEC_GROUP) == 0
12607 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
12609 first->output_section = bfd_abs_section_ptr;
12610 first->kept_section = l->sec;
12611 sec->output_section = bfd_abs_section_ptr;
12616 /* Check this linkonce section against single member groups. */
12617 for (l = already_linked_list->entry; l != NULL; l = l->next)
12618 if (l->sec->flags & SEC_GROUP)
12620 asection *first = elf_next_in_group (l->sec);
12623 && elf_next_in_group (first) == first
12624 && bfd_elf_match_symbols_in_sections (first, sec, info))
12626 sec->output_section = bfd_abs_section_ptr;
12627 sec->kept_section = first;
12632 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
12633 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
12634 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
12635 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
12636 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
12637 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
12638 `.gnu.linkonce.t.F' section from a different bfd not requiring any
12639 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
12640 The reverse order cannot happen as there is never a bfd with only the
12641 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
12642 matter as here were are looking only for cross-bfd sections. */
12644 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
12645 for (l = already_linked_list->entry; l != NULL; l = l->next)
12646 if ((l->sec->flags & SEC_GROUP) == 0
12647 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
12649 if (abfd != l->sec->owner)
12650 sec->output_section = bfd_abs_section_ptr;
12654 /* This is the first section with this name. Record it. */
12655 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
12656 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
12657 return sec->output_section == bfd_abs_section_ptr;
12661 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
12663 return sym->st_shndx == SHN_COMMON;
12667 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
12673 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
12675 return bfd_com_section_ptr;
12679 _bfd_elf_default_got_elt_size (bfd *abfd,
12680 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12681 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
12682 bfd *ibfd ATTRIBUTE_UNUSED,
12683 unsigned long symndx ATTRIBUTE_UNUSED)
12685 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12686 return bed->s->arch_size / 8;
12689 /* Routines to support the creation of dynamic relocs. */
12691 /* Returns the name of the dynamic reloc section associated with SEC. */
12693 static const char *
12694 get_dynamic_reloc_section_name (bfd * abfd,
12696 bfd_boolean is_rela)
12699 const char *old_name = bfd_get_section_name (NULL, sec);
12700 const char *prefix = is_rela ? ".rela" : ".rel";
12702 if (old_name == NULL)
12705 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
12706 sprintf (name, "%s%s", prefix, old_name);
12711 /* Returns the dynamic reloc section associated with SEC.
12712 If necessary compute the name of the dynamic reloc section based
12713 on SEC's name (looked up in ABFD's string table) and the setting
12717 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
12719 bfd_boolean is_rela)
12721 asection * reloc_sec = elf_section_data (sec)->sreloc;
12723 if (reloc_sec == NULL)
12725 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12729 reloc_sec = bfd_get_section_by_name (abfd, name);
12731 if (reloc_sec != NULL)
12732 elf_section_data (sec)->sreloc = reloc_sec;
12739 /* Returns the dynamic reloc section associated with SEC. If the
12740 section does not exist it is created and attached to the DYNOBJ
12741 bfd and stored in the SRELOC field of SEC's elf_section_data
12744 ALIGNMENT is the alignment for the newly created section and
12745 IS_RELA defines whether the name should be .rela.<SEC's name>
12746 or .rel.<SEC's name>. The section name is looked up in the
12747 string table associated with ABFD. */
12750 _bfd_elf_make_dynamic_reloc_section (asection * sec,
12752 unsigned int alignment,
12754 bfd_boolean is_rela)
12756 asection * reloc_sec = elf_section_data (sec)->sreloc;
12758 if (reloc_sec == NULL)
12760 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
12765 reloc_sec = bfd_get_section_by_name (dynobj, name);
12767 if (reloc_sec == NULL)
12771 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED);
12772 if ((sec->flags & SEC_ALLOC) != 0)
12773 flags |= SEC_ALLOC | SEC_LOAD;
12775 reloc_sec = bfd_make_section_with_flags (dynobj, name, flags);
12776 if (reloc_sec != NULL)
12778 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
12783 elf_section_data (sec)->sreloc = reloc_sec;
12789 /* Copy the ELF symbol type associated with a linker hash entry. */
12791 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
12792 struct bfd_link_hash_entry * hdest,
12793 struct bfd_link_hash_entry * hsrc)
12795 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest;
12796 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc;
12798 ehdest->type = ehsrc->type;
12799 ehdest->target_internal = ehsrc->target_internal;
12802 /* Append a RELA relocation REL to section S in BFD. */
12805 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12807 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12808 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
12809 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
12810 bed->s->swap_reloca_out (abfd, rel, loc);
12813 /* Append a REL relocation REL to section S in BFD. */
12816 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
12818 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12819 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
12820 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
12821 bed->s->swap_reloca_out (abfd, rel, loc);