1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
96 && discard ? discarded_section (isec) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
233 hash_table->dynobj = abfd;
236 if (hash_table->dynstr == NULL)
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
260 if (! is_elf_hash_table (info->hash))
263 if (elf_hash_table (info)->dynamic_sections_created)
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
272 flags = bed->dynamic_sec_flags;
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
295 || ! bfd_set_section_alignment (abfd, s, 1))
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 elf_hash_table (info)->dynsym = s;
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
342 if (info->emit_gnu_hash)
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
370 /* Create dynamic sections when linking against a dynamic object. */
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 if (bed->want_plt_sym)
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
422 if (! _bfd_elf_create_got_section (abfd, info))
425 if (bed->want_dynbss)
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (bed->want_dynrelro)
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
462 if (bfd_link_executable (info))
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
473 if (bed->want_dynrelro)
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
483 htab->sreldynrelro = s;
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
503 if (h->dynindx == -1)
505 struct elf_strtab_hash *dynstr;
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
522 if (!elf_hash_table (info)->is_relocatable_executable)
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
533 dynstr = elf_hash_table (info)->dynstr;
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
542 /* We don't put any version information in the dynamic string
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
559 if (indx == (size_t) -1)
561 h->dynstr_index = indx;
567 /* Mark a symbol dynamic. */
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
596 bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
602 struct elf_link_hash_entry *h, *hv;
603 struct elf_link_hash_table *htab;
604 const struct elf_backend_data *bed;
606 if (!is_elf_hash_table (info->hash))
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
617 if (h->versioned == unknown)
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
626 h->versioned = versioned;
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 switch (h->root.type)
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h->root.type = bfd_link_hash_new;
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
653 case bfd_link_hash_new:
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed = get_elf_backend_data (output_bfd);
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
682 h->root.type = bfd_link_hash_undefined;
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
691 h->verinfo.verdef = NULL;
693 /* Make sure this symbol is not garbage collected. */
700 bed = get_elf_backend_data (output_bfd);
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 if (!bfd_link_relocatable (info)
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
728 struct elf_link_hash_entry *def = weakdef (h);
730 if (def->dynindx == -1
731 && !bfd_elf_link_record_dynamic_symbol (info, def))
739 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
740 success, and 2 on a failure caused by attempting to record a symbol
741 in a discarded section, eg. a discarded link-once section symbol. */
744 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 struct elf_link_local_dynamic_entry *entry;
750 struct elf_link_hash_table *eht;
751 struct elf_strtab_hash *dynstr;
754 Elf_External_Sym_Shndx eshndx;
755 char esym[sizeof (Elf64_External_Sym)];
757 if (! is_elf_hash_table (info->hash))
760 /* See if the entry exists already. */
761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
762 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 amt = sizeof (*entry);
766 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
770 /* Go find the symbol, so that we can find it's name. */
771 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
772 1, input_indx, &entry->isym, esym, &eshndx))
774 bfd_release (input_bfd, entry);
778 if (entry->isym.st_shndx != SHN_UNDEF
779 && entry->isym.st_shndx < SHN_LORESERVE)
783 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
784 if (s == NULL || bfd_is_abs_section (s->output_section))
786 /* We can still bfd_release here as nothing has done another
787 bfd_alloc. We can't do this later in this function. */
788 bfd_release (input_bfd, entry);
793 name = (bfd_elf_string_from_elf_section
794 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
795 entry->isym.st_name));
797 dynstr = elf_hash_table (info)->dynstr;
800 /* Create a strtab to hold the dynamic symbol names. */
801 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
807 if (dynstr_index == (size_t) -1)
809 entry->isym.st_name = dynstr_index;
811 eht = elf_hash_table (info);
813 entry->next = eht->dynlocal;
814 eht->dynlocal = entry;
815 entry->input_bfd = input_bfd;
816 entry->input_indx = input_indx;
819 /* Whatever binding the symbol had before, it's now local. */
821 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
823 /* The dynindx will be set at the end of size_dynamic_sections. */
828 /* Return the dynindex of a local dynamic symbol. */
831 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
835 struct elf_link_local_dynamic_entry *e;
837 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
838 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 /* This function is used to renumber the dynamic symbols, if some of
844 them are removed because they are marked as local. This is called
845 via elf_link_hash_traverse. */
848 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 size_t *count = (size_t *) data;
856 if (h->dynindx != -1)
857 h->dynindx = ++(*count);
863 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
864 STB_LOCAL binding. */
867 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 size_t *count = (size_t *) data;
872 if (!h->forced_local)
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
881 /* Return true if the dynamic symbol for a given section should be
882 omitted when creating a shared library. */
884 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
885 struct bfd_link_info *info,
888 struct elf_link_hash_table *htab;
891 switch (elf_section_data (p)->this_hdr.sh_type)
895 /* If sh_type is yet undecided, assume it could be
896 SHT_PROGBITS/SHT_NOBITS. */
898 htab = elf_hash_table (info);
899 if (p == htab->tls_sec)
902 if (htab->text_index_section != NULL)
903 return p != htab->text_index_section && p != htab->data_index_section;
905 return (htab->dynobj != NULL
906 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
907 && ip->output_section == p);
909 /* There shouldn't be section relative relocations
910 against any other section. */
916 /* Assign dynsym indices. In a shared library we generate a section
917 symbol for each output section, which come first. Next come symbols
918 which have been forced to local binding. Then all of the back-end
919 allocated local dynamic syms, followed by the rest of the global
920 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
921 (This prevents the early call before elf_backend_init_index_section
922 and strip_excluded_output_sections setting dynindx for sections
923 that are stripped.) */
926 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
927 struct bfd_link_info *info,
928 unsigned long *section_sym_count)
930 unsigned long dynsymcount = 0;
931 bfd_boolean do_sec = section_sym_count != NULL;
933 if (bfd_link_pic (info)
934 || elf_hash_table (info)->is_relocatable_executable)
936 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
938 for (p = output_bfd->sections; p ; p = p->next)
939 if ((p->flags & SEC_EXCLUDE) == 0
940 && (p->flags & SEC_ALLOC) != 0
941 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
945 elf_section_data (p)->dynindx = dynsymcount;
948 elf_section_data (p)->dynindx = 0;
951 *section_sym_count = dynsymcount;
953 elf_link_hash_traverse (elf_hash_table (info),
954 elf_link_renumber_local_hash_table_dynsyms,
957 if (elf_hash_table (info)->dynlocal)
959 struct elf_link_local_dynamic_entry *p;
960 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
961 p->dynindx = ++dynsymcount;
963 elf_hash_table (info)->local_dynsymcount = dynsymcount;
965 elf_link_hash_traverse (elf_hash_table (info),
966 elf_link_renumber_hash_table_dynsyms,
969 /* There is an unused NULL entry at the head of the table which we
970 must account for in our count even if the table is empty since it
971 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
975 elf_hash_table (info)->dynsymcount = dynsymcount;
979 /* Merge st_other field. */
982 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
983 const Elf_Internal_Sym *isym, asection *sec,
984 bfd_boolean definition, bfd_boolean dynamic)
986 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
988 /* If st_other has a processor-specific meaning, specific
989 code might be needed here. */
990 if (bed->elf_backend_merge_symbol_attribute)
991 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
996 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
997 unsigned hvis = ELF_ST_VISIBILITY (h->other);
999 /* Keep the most constraining visibility. Leave the remainder
1000 of the st_other field to elf_backend_merge_symbol_attribute. */
1001 if (symvis - 1 < hvis - 1)
1002 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1005 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1006 && (sec->flags & SEC_READONLY) == 0)
1007 h->protected_def = 1;
1010 /* This function is called when we want to merge a new symbol with an
1011 existing symbol. It handles the various cases which arise when we
1012 find a definition in a dynamic object, or when there is already a
1013 definition in a dynamic object. The new symbol is described by
1014 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1015 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1016 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1017 of an old common symbol. We set OVERRIDE if the old symbol is
1018 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1019 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1020 to change. By OK to change, we mean that we shouldn't warn if the
1021 type or size does change. */
1024 _bfd_elf_merge_symbol (bfd *abfd,
1025 struct bfd_link_info *info,
1027 Elf_Internal_Sym *sym,
1030 struct elf_link_hash_entry **sym_hash,
1032 bfd_boolean *pold_weak,
1033 unsigned int *pold_alignment,
1035 bfd_boolean *override,
1036 bfd_boolean *type_change_ok,
1037 bfd_boolean *size_change_ok,
1038 bfd_boolean *matched)
1040 asection *sec, *oldsec;
1041 struct elf_link_hash_entry *h;
1042 struct elf_link_hash_entry *hi;
1043 struct elf_link_hash_entry *flip;
1046 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1047 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1048 const struct elf_backend_data *bed;
1050 bfd_boolean default_sym = *matched;
1056 bind = ELF_ST_BIND (sym->st_info);
1058 if (! bfd_is_und_section (sec))
1059 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1061 h = ((struct elf_link_hash_entry *)
1062 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1067 bed = get_elf_backend_data (abfd);
1069 /* NEW_VERSION is the symbol version of the new symbol. */
1070 if (h->versioned != unversioned)
1072 /* Symbol version is unknown or versioned. */
1073 new_version = strrchr (name, ELF_VER_CHR);
1076 if (h->versioned == unknown)
1078 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1079 h->versioned = versioned_hidden;
1081 h->versioned = versioned;
1084 if (new_version[0] == '\0')
1088 h->versioned = unversioned;
1093 /* For merging, we only care about real symbols. But we need to make
1094 sure that indirect symbol dynamic flags are updated. */
1096 while (h->root.type == bfd_link_hash_indirect
1097 || h->root.type == bfd_link_hash_warning)
1098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1102 if (hi == h || h->root.type == bfd_link_hash_new)
1106 /* OLD_HIDDEN is true if the existing symbol is only visible
1107 to the symbol with the same symbol version. NEW_HIDDEN is
1108 true if the new symbol is only visible to the symbol with
1109 the same symbol version. */
1110 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1111 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1112 if (!old_hidden && !new_hidden)
1113 /* The new symbol matches the existing symbol if both
1118 /* OLD_VERSION is the symbol version of the existing
1122 if (h->versioned >= versioned)
1123 old_version = strrchr (h->root.root.string,
1128 /* The new symbol matches the existing symbol if they
1129 have the same symbol version. */
1130 *matched = (old_version == new_version
1131 || (old_version != NULL
1132 && new_version != NULL
1133 && strcmp (old_version, new_version) == 0));
1138 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1143 switch (h->root.type)
1148 case bfd_link_hash_undefined:
1149 case bfd_link_hash_undefweak:
1150 oldbfd = h->root.u.undef.abfd;
1153 case bfd_link_hash_defined:
1154 case bfd_link_hash_defweak:
1155 oldbfd = h->root.u.def.section->owner;
1156 oldsec = h->root.u.def.section;
1159 case bfd_link_hash_common:
1160 oldbfd = h->root.u.c.p->section->owner;
1161 oldsec = h->root.u.c.p->section;
1163 *pold_alignment = h->root.u.c.p->alignment_power;
1166 if (poldbfd && *poldbfd == NULL)
1169 /* Differentiate strong and weak symbols. */
1170 newweak = bind == STB_WEAK;
1171 oldweak = (h->root.type == bfd_link_hash_defweak
1172 || h->root.type == bfd_link_hash_undefweak);
1174 *pold_weak = oldweak;
1176 /* We have to check it for every instance since the first few may be
1177 references and not all compilers emit symbol type for undefined
1179 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1181 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1182 respectively, is from a dynamic object. */
1184 newdyn = (abfd->flags & DYNAMIC) != 0;
1186 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1187 syms and defined syms in dynamic libraries respectively.
1188 ref_dynamic on the other hand can be set for a symbol defined in
1189 a dynamic library, and def_dynamic may not be set; When the
1190 definition in a dynamic lib is overridden by a definition in the
1191 executable use of the symbol in the dynamic lib becomes a
1192 reference to the executable symbol. */
1195 if (bfd_is_und_section (sec))
1197 if (bind != STB_WEAK)
1199 h->ref_dynamic_nonweak = 1;
1200 hi->ref_dynamic_nonweak = 1;
1205 /* Update the existing symbol only if they match. */
1208 hi->dynamic_def = 1;
1212 /* If we just created the symbol, mark it as being an ELF symbol.
1213 Other than that, there is nothing to do--there is no merge issue
1214 with a newly defined symbol--so we just return. */
1216 if (h->root.type == bfd_link_hash_new)
1222 /* In cases involving weak versioned symbols, we may wind up trying
1223 to merge a symbol with itself. Catch that here, to avoid the
1224 confusion that results if we try to override a symbol with
1225 itself. The additional tests catch cases like
1226 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1227 dynamic object, which we do want to handle here. */
1229 && (newweak || oldweak)
1230 && ((abfd->flags & DYNAMIC) == 0
1231 || !h->def_regular))
1236 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1237 else if (oldsec != NULL)
1239 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1240 indices used by MIPS ELF. */
1241 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1244 /* Handle a case where plugin_notice won't be called and thus won't
1245 set the non_ir_ref flags on the first pass over symbols. */
1247 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1248 && newdyn != olddyn)
1250 h->root.non_ir_ref_dynamic = TRUE;
1251 hi->root.non_ir_ref_dynamic = TRUE;
1254 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1255 respectively, appear to be a definition rather than reference. */
1257 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1259 olddef = (h->root.type != bfd_link_hash_undefined
1260 && h->root.type != bfd_link_hash_undefweak
1261 && h->root.type != bfd_link_hash_common);
1263 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1264 respectively, appear to be a function. */
1266 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1267 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1269 oldfunc = (h->type != STT_NOTYPE
1270 && bed->is_function_type (h->type));
1272 if (!(newfunc && oldfunc)
1273 && ELF_ST_TYPE (sym->st_info) != h->type
1274 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1275 && h->type != STT_NOTYPE
1276 && (newdef || bfd_is_com_section (sec))
1277 && (olddef || h->root.type == bfd_link_hash_common))
1279 /* If creating a default indirect symbol ("foo" or "foo@") from
1280 a dynamic versioned definition ("foo@@") skip doing so if
1281 there is an existing regular definition with a different
1282 type. We don't want, for example, a "time" variable in the
1283 executable overriding a "time" function in a shared library. */
1291 /* When adding a symbol from a regular object file after we have
1292 created indirect symbols, undo the indirection and any
1299 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1300 h->forced_local = 0;
1304 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1306 h->root.type = bfd_link_hash_undefined;
1307 h->root.u.undef.abfd = abfd;
1311 h->root.type = bfd_link_hash_new;
1312 h->root.u.undef.abfd = NULL;
1318 /* Check TLS symbols. We don't check undefined symbols introduced
1319 by "ld -u" which have no type (and oldbfd NULL), and we don't
1320 check symbols from plugins because they also have no type. */
1322 && (oldbfd->flags & BFD_PLUGIN) == 0
1323 && (abfd->flags & BFD_PLUGIN) == 0
1324 && ELF_ST_TYPE (sym->st_info) != h->type
1325 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1328 bfd_boolean ntdef, tdef;
1329 asection *ntsec, *tsec;
1331 if (h->type == STT_TLS)
1352 /* xgettext:c-format */
1353 (_("%s: TLS definition in %B section %A "
1354 "mismatches non-TLS definition in %B section %A"),
1355 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1356 else if (!tdef && !ntdef)
1358 /* xgettext:c-format */
1359 (_("%s: TLS reference in %B "
1360 "mismatches non-TLS reference in %B"),
1361 h->root.root.string, tbfd, ntbfd);
1364 /* xgettext:c-format */
1365 (_("%s: TLS definition in %B section %A "
1366 "mismatches non-TLS reference in %B"),
1367 h->root.root.string, tbfd, tsec, ntbfd);
1370 /* xgettext:c-format */
1371 (_("%s: TLS reference in %B "
1372 "mismatches non-TLS definition in %B section %A"),
1373 h->root.root.string, tbfd, ntbfd, ntsec);
1375 bfd_set_error (bfd_error_bad_value);
1379 /* If the old symbol has non-default visibility, we ignore the new
1380 definition from a dynamic object. */
1382 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1383 && !bfd_is_und_section (sec))
1386 /* Make sure this symbol is dynamic. */
1388 hi->ref_dynamic = 1;
1389 /* A protected symbol has external availability. Make sure it is
1390 recorded as dynamic.
1392 FIXME: Should we check type and size for protected symbol? */
1393 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1394 return bfd_elf_link_record_dynamic_symbol (info, h);
1399 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1402 /* If the new symbol with non-default visibility comes from a
1403 relocatable file and the old definition comes from a dynamic
1404 object, we remove the old definition. */
1405 if (hi->root.type == bfd_link_hash_indirect)
1407 /* Handle the case where the old dynamic definition is
1408 default versioned. We need to copy the symbol info from
1409 the symbol with default version to the normal one if it
1410 was referenced before. */
1413 hi->root.type = h->root.type;
1414 h->root.type = bfd_link_hash_indirect;
1415 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1417 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1418 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1420 /* If the new symbol is hidden or internal, completely undo
1421 any dynamic link state. */
1422 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1423 h->forced_local = 0;
1430 /* FIXME: Should we check type and size for protected symbol? */
1440 /* If the old symbol was undefined before, then it will still be
1441 on the undefs list. If the new symbol is undefined or
1442 common, we can't make it bfd_link_hash_new here, because new
1443 undefined or common symbols will be added to the undefs list
1444 by _bfd_generic_link_add_one_symbol. Symbols may not be
1445 added twice to the undefs list. Also, if the new symbol is
1446 undefweak then we don't want to lose the strong undef. */
1447 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1449 h->root.type = bfd_link_hash_undefined;
1450 h->root.u.undef.abfd = abfd;
1454 h->root.type = bfd_link_hash_new;
1455 h->root.u.undef.abfd = NULL;
1458 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1460 /* If the new symbol is hidden or internal, completely undo
1461 any dynamic link state. */
1462 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1463 h->forced_local = 0;
1469 /* FIXME: Should we check type and size for protected symbol? */
1475 /* If a new weak symbol definition comes from a regular file and the
1476 old symbol comes from a dynamic library, we treat the new one as
1477 strong. Similarly, an old weak symbol definition from a regular
1478 file is treated as strong when the new symbol comes from a dynamic
1479 library. Further, an old weak symbol from a dynamic library is
1480 treated as strong if the new symbol is from a dynamic library.
1481 This reflects the way glibc's ld.so works.
1483 Also allow a weak symbol to override a linker script symbol
1484 defined by an early pass over the script. This is done so the
1485 linker knows the symbol is defined in an object file, for the
1486 DEFINED script function.
1488 Do this before setting *type_change_ok or *size_change_ok so that
1489 we warn properly when dynamic library symbols are overridden. */
1491 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1493 if (olddef && newdyn)
1496 /* Allow changes between different types of function symbol. */
1497 if (newfunc && oldfunc)
1498 *type_change_ok = TRUE;
1500 /* It's OK to change the type if either the existing symbol or the
1501 new symbol is weak. A type change is also OK if the old symbol
1502 is undefined and the new symbol is defined. */
1507 && h->root.type == bfd_link_hash_undefined))
1508 *type_change_ok = TRUE;
1510 /* It's OK to change the size if either the existing symbol or the
1511 new symbol is weak, or if the old symbol is undefined. */
1514 || h->root.type == bfd_link_hash_undefined)
1515 *size_change_ok = TRUE;
1517 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1518 symbol, respectively, appears to be a common symbol in a dynamic
1519 object. If a symbol appears in an uninitialized section, and is
1520 not weak, and is not a function, then it may be a common symbol
1521 which was resolved when the dynamic object was created. We want
1522 to treat such symbols specially, because they raise special
1523 considerations when setting the symbol size: if the symbol
1524 appears as a common symbol in a regular object, and the size in
1525 the regular object is larger, we must make sure that we use the
1526 larger size. This problematic case can always be avoided in C,
1527 but it must be handled correctly when using Fortran shared
1530 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1531 likewise for OLDDYNCOMMON and OLDDEF.
1533 Note that this test is just a heuristic, and that it is quite
1534 possible to have an uninitialized symbol in a shared object which
1535 is really a definition, rather than a common symbol. This could
1536 lead to some minor confusion when the symbol really is a common
1537 symbol in some regular object. However, I think it will be
1543 && (sec->flags & SEC_ALLOC) != 0
1544 && (sec->flags & SEC_LOAD) == 0
1547 newdyncommon = TRUE;
1549 newdyncommon = FALSE;
1553 && h->root.type == bfd_link_hash_defined
1555 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1556 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1559 olddyncommon = TRUE;
1561 olddyncommon = FALSE;
1563 /* We now know everything about the old and new symbols. We ask the
1564 backend to check if we can merge them. */
1565 if (bed->merge_symbol != NULL)
1567 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1572 /* There are multiple definitions of a normal symbol. Skip the
1573 default symbol as well as definition from an IR object. */
1574 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1575 && !default_sym && h->def_regular
1577 && (oldbfd->flags & BFD_PLUGIN) != 0
1578 && (abfd->flags & BFD_PLUGIN) == 0))
1580 /* Handle a multiple definition. */
1581 (*info->callbacks->multiple_definition) (info, &h->root,
1582 abfd, sec, *pvalue);
1587 /* If both the old and the new symbols look like common symbols in a
1588 dynamic object, set the size of the symbol to the larger of the
1593 && sym->st_size != h->size)
1595 /* Since we think we have two common symbols, issue a multiple
1596 common warning if desired. Note that we only warn if the
1597 size is different. If the size is the same, we simply let
1598 the old symbol override the new one as normally happens with
1599 symbols defined in dynamic objects. */
1601 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1602 bfd_link_hash_common, sym->st_size);
1603 if (sym->st_size > h->size)
1604 h->size = sym->st_size;
1606 *size_change_ok = TRUE;
1609 /* If we are looking at a dynamic object, and we have found a
1610 definition, we need to see if the symbol was already defined by
1611 some other object. If so, we want to use the existing
1612 definition, and we do not want to report a multiple symbol
1613 definition error; we do this by clobbering *PSEC to be
1614 bfd_und_section_ptr.
1616 We treat a common symbol as a definition if the symbol in the
1617 shared library is a function, since common symbols always
1618 represent variables; this can cause confusion in principle, but
1619 any such confusion would seem to indicate an erroneous program or
1620 shared library. We also permit a common symbol in a regular
1621 object to override a weak symbol in a shared object. */
1626 || (h->root.type == bfd_link_hash_common
1627 && (newweak || newfunc))))
1631 newdyncommon = FALSE;
1633 *psec = sec = bfd_und_section_ptr;
1634 *size_change_ok = TRUE;
1636 /* If we get here when the old symbol is a common symbol, then
1637 we are explicitly letting it override a weak symbol or
1638 function in a dynamic object, and we don't want to warn about
1639 a type change. If the old symbol is a defined symbol, a type
1640 change warning may still be appropriate. */
1642 if (h->root.type == bfd_link_hash_common)
1643 *type_change_ok = TRUE;
1646 /* Handle the special case of an old common symbol merging with a
1647 new symbol which looks like a common symbol in a shared object.
1648 We change *PSEC and *PVALUE to make the new symbol look like a
1649 common symbol, and let _bfd_generic_link_add_one_symbol do the
1653 && h->root.type == bfd_link_hash_common)
1657 newdyncommon = FALSE;
1658 *pvalue = sym->st_size;
1659 *psec = sec = bed->common_section (oldsec);
1660 *size_change_ok = TRUE;
1663 /* Skip weak definitions of symbols that are already defined. */
1664 if (newdef && olddef && newweak)
1666 /* Don't skip new non-IR weak syms. */
1667 if (!(oldbfd != NULL
1668 && (oldbfd->flags & BFD_PLUGIN) != 0
1669 && (abfd->flags & BFD_PLUGIN) == 0))
1675 /* Merge st_other. If the symbol already has a dynamic index,
1676 but visibility says it should not be visible, turn it into a
1678 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1679 if (h->dynindx != -1)
1680 switch (ELF_ST_VISIBILITY (h->other))
1684 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1689 /* If the old symbol is from a dynamic object, and the new symbol is
1690 a definition which is not from a dynamic object, then the new
1691 symbol overrides the old symbol. Symbols from regular files
1692 always take precedence over symbols from dynamic objects, even if
1693 they are defined after the dynamic object in the link.
1695 As above, we again permit a common symbol in a regular object to
1696 override a definition in a shared object if the shared object
1697 symbol is a function or is weak. */
1702 || (bfd_is_com_section (sec)
1703 && (oldweak || oldfunc)))
1708 /* Change the hash table entry to undefined, and let
1709 _bfd_generic_link_add_one_symbol do the right thing with the
1712 h->root.type = bfd_link_hash_undefined;
1713 h->root.u.undef.abfd = h->root.u.def.section->owner;
1714 *size_change_ok = TRUE;
1717 olddyncommon = FALSE;
1719 /* We again permit a type change when a common symbol may be
1720 overriding a function. */
1722 if (bfd_is_com_section (sec))
1726 /* If a common symbol overrides a function, make sure
1727 that it isn't defined dynamically nor has type
1730 h->type = STT_NOTYPE;
1732 *type_change_ok = TRUE;
1735 if (hi->root.type == bfd_link_hash_indirect)
1738 /* This union may have been set to be non-NULL when this symbol
1739 was seen in a dynamic object. We must force the union to be
1740 NULL, so that it is correct for a regular symbol. */
1741 h->verinfo.vertree = NULL;
1744 /* Handle the special case of a new common symbol merging with an
1745 old symbol that looks like it might be a common symbol defined in
1746 a shared object. Note that we have already handled the case in
1747 which a new common symbol should simply override the definition
1748 in the shared library. */
1751 && bfd_is_com_section (sec)
1754 /* It would be best if we could set the hash table entry to a
1755 common symbol, but we don't know what to use for the section
1756 or the alignment. */
1757 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1758 bfd_link_hash_common, sym->st_size);
1760 /* If the presumed common symbol in the dynamic object is
1761 larger, pretend that the new symbol has its size. */
1763 if (h->size > *pvalue)
1766 /* We need to remember the alignment required by the symbol
1767 in the dynamic object. */
1768 BFD_ASSERT (pold_alignment);
1769 *pold_alignment = h->root.u.def.section->alignment_power;
1772 olddyncommon = FALSE;
1774 h->root.type = bfd_link_hash_undefined;
1775 h->root.u.undef.abfd = h->root.u.def.section->owner;
1777 *size_change_ok = TRUE;
1778 *type_change_ok = TRUE;
1780 if (hi->root.type == bfd_link_hash_indirect)
1783 h->verinfo.vertree = NULL;
1788 /* Handle the case where we had a versioned symbol in a dynamic
1789 library and now find a definition in a normal object. In this
1790 case, we make the versioned symbol point to the normal one. */
1791 flip->root.type = h->root.type;
1792 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1793 h->root.type = bfd_link_hash_indirect;
1794 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1795 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1799 flip->ref_dynamic = 1;
1806 /* This function is called to create an indirect symbol from the
1807 default for the symbol with the default version if needed. The
1808 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1809 set DYNSYM if the new indirect symbol is dynamic. */
1812 _bfd_elf_add_default_symbol (bfd *abfd,
1813 struct bfd_link_info *info,
1814 struct elf_link_hash_entry *h,
1816 Elf_Internal_Sym *sym,
1820 bfd_boolean *dynsym)
1822 bfd_boolean type_change_ok;
1823 bfd_boolean size_change_ok;
1826 struct elf_link_hash_entry *hi;
1827 struct bfd_link_hash_entry *bh;
1828 const struct elf_backend_data *bed;
1829 bfd_boolean collect;
1830 bfd_boolean dynamic;
1831 bfd_boolean override;
1833 size_t len, shortlen;
1835 bfd_boolean matched;
1837 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1840 /* If this symbol has a version, and it is the default version, we
1841 create an indirect symbol from the default name to the fully
1842 decorated name. This will cause external references which do not
1843 specify a version to be bound to this version of the symbol. */
1844 p = strchr (name, ELF_VER_CHR);
1845 if (h->versioned == unknown)
1849 h->versioned = unversioned;
1854 if (p[1] != ELF_VER_CHR)
1856 h->versioned = versioned_hidden;
1860 h->versioned = versioned;
1865 /* PR ld/19073: We may see an unversioned definition after the
1871 bed = get_elf_backend_data (abfd);
1872 collect = bed->collect;
1873 dynamic = (abfd->flags & DYNAMIC) != 0;
1875 shortlen = p - name;
1876 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1877 if (shortname == NULL)
1879 memcpy (shortname, name, shortlen);
1880 shortname[shortlen] = '\0';
1882 /* We are going to create a new symbol. Merge it with any existing
1883 symbol with this name. For the purposes of the merge, act as
1884 though we were defining the symbol we just defined, although we
1885 actually going to define an indirect symbol. */
1886 type_change_ok = FALSE;
1887 size_change_ok = FALSE;
1890 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1891 &hi, poldbfd, NULL, NULL, &skip, &override,
1892 &type_change_ok, &size_change_ok, &matched))
1898 if (hi->def_regular)
1900 /* If the undecorated symbol will have a version added by a
1901 script different to H, then don't indirect to/from the
1902 undecorated symbol. This isn't ideal because we may not yet
1903 have seen symbol versions, if given by a script on the
1904 command line rather than via --version-script. */
1905 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1910 = bfd_find_version_for_sym (info->version_info,
1911 hi->root.root.string, &hide);
1912 if (hi->verinfo.vertree != NULL && hide)
1914 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1918 if (hi->verinfo.vertree != NULL
1919 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1925 /* Add the default symbol if not performing a relocatable link. */
1926 if (! bfd_link_relocatable (info))
1929 if (! (_bfd_generic_link_add_one_symbol
1930 (info, abfd, shortname, BSF_INDIRECT,
1931 bfd_ind_section_ptr,
1932 0, name, FALSE, collect, &bh)))
1934 hi = (struct elf_link_hash_entry *) bh;
1939 /* In this case the symbol named SHORTNAME is overriding the
1940 indirect symbol we want to add. We were planning on making
1941 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1942 is the name without a version. NAME is the fully versioned
1943 name, and it is the default version.
1945 Overriding means that we already saw a definition for the
1946 symbol SHORTNAME in a regular object, and it is overriding
1947 the symbol defined in the dynamic object.
1949 When this happens, we actually want to change NAME, the
1950 symbol we just added, to refer to SHORTNAME. This will cause
1951 references to NAME in the shared object to become references
1952 to SHORTNAME in the regular object. This is what we expect
1953 when we override a function in a shared object: that the
1954 references in the shared object will be mapped to the
1955 definition in the regular object. */
1957 while (hi->root.type == bfd_link_hash_indirect
1958 || hi->root.type == bfd_link_hash_warning)
1959 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1961 h->root.type = bfd_link_hash_indirect;
1962 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1966 hi->ref_dynamic = 1;
1970 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1975 /* Now set HI to H, so that the following code will set the
1976 other fields correctly. */
1980 /* Check if HI is a warning symbol. */
1981 if (hi->root.type == bfd_link_hash_warning)
1982 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1984 /* If there is a duplicate definition somewhere, then HI may not
1985 point to an indirect symbol. We will have reported an error to
1986 the user in that case. */
1988 if (hi->root.type == bfd_link_hash_indirect)
1990 struct elf_link_hash_entry *ht;
1992 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1993 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1995 /* A reference to the SHORTNAME symbol from a dynamic library
1996 will be satisfied by the versioned symbol at runtime. In
1997 effect, we have a reference to the versioned symbol. */
1998 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1999 hi->dynamic_def |= ht->dynamic_def;
2001 /* See if the new flags lead us to realize that the symbol must
2007 if (! bfd_link_executable (info)
2014 if (hi->ref_regular)
2020 /* We also need to define an indirection from the nondefault version
2024 len = strlen (name);
2025 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2026 if (shortname == NULL)
2028 memcpy (shortname, name, shortlen);
2029 memcpy (shortname + shortlen, p + 1, len - shortlen);
2031 /* Once again, merge with any existing symbol. */
2032 type_change_ok = FALSE;
2033 size_change_ok = FALSE;
2035 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2036 &hi, poldbfd, NULL, NULL, &skip, &override,
2037 &type_change_ok, &size_change_ok, &matched))
2045 /* Here SHORTNAME is a versioned name, so we don't expect to see
2046 the type of override we do in the case above unless it is
2047 overridden by a versioned definition. */
2048 if (hi->root.type != bfd_link_hash_defined
2049 && hi->root.type != bfd_link_hash_defweak)
2051 /* xgettext:c-format */
2052 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2058 if (! (_bfd_generic_link_add_one_symbol
2059 (info, abfd, shortname, BSF_INDIRECT,
2060 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2062 hi = (struct elf_link_hash_entry *) bh;
2064 /* If there is a duplicate definition somewhere, then HI may not
2065 point to an indirect symbol. We will have reported an error
2066 to the user in that case. */
2068 if (hi->root.type == bfd_link_hash_indirect)
2070 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2071 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2072 hi->dynamic_def |= h->dynamic_def;
2074 /* See if the new flags lead us to realize that the symbol
2080 if (! bfd_link_executable (info)
2086 if (hi->ref_regular)
2096 /* This routine is used to export all defined symbols into the dynamic
2097 symbol table. It is called via elf_link_hash_traverse. */
2100 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2102 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2104 /* Ignore indirect symbols. These are added by the versioning code. */
2105 if (h->root.type == bfd_link_hash_indirect)
2108 /* Ignore this if we won't export it. */
2109 if (!eif->info->export_dynamic && !h->dynamic)
2112 if (h->dynindx == -1
2113 && (h->def_regular || h->ref_regular)
2114 && ! bfd_hide_sym_by_version (eif->info->version_info,
2115 h->root.root.string))
2117 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2127 /* Look through the symbols which are defined in other shared
2128 libraries and referenced here. Update the list of version
2129 dependencies. This will be put into the .gnu.version_r section.
2130 This function is called via elf_link_hash_traverse. */
2133 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2136 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2137 Elf_Internal_Verneed *t;
2138 Elf_Internal_Vernaux *a;
2141 /* We only care about symbols defined in shared objects with version
2146 || h->verinfo.verdef == NULL
2147 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2148 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2151 /* See if we already know about this version. */
2152 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2156 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2159 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2160 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2166 /* This is a new version. Add it to tree we are building. */
2171 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2174 rinfo->failed = TRUE;
2178 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2179 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2180 elf_tdata (rinfo->info->output_bfd)->verref = t;
2184 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2187 rinfo->failed = TRUE;
2191 /* Note that we are copying a string pointer here, and testing it
2192 above. If bfd_elf_string_from_elf_section is ever changed to
2193 discard the string data when low in memory, this will have to be
2195 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2197 a->vna_flags = h->verinfo.verdef->vd_flags;
2198 a->vna_nextptr = t->vn_auxptr;
2200 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2203 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2210 /* Figure out appropriate versions for all the symbols. We may not
2211 have the version number script until we have read all of the input
2212 files, so until that point we don't know which symbols should be
2213 local. This function is called via elf_link_hash_traverse. */
2216 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2218 struct elf_info_failed *sinfo;
2219 struct bfd_link_info *info;
2220 const struct elf_backend_data *bed;
2221 struct elf_info_failed eif;
2224 sinfo = (struct elf_info_failed *) data;
2227 /* Fix the symbol flags. */
2230 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2233 sinfo->failed = TRUE;
2237 /* We only need version numbers for symbols defined in regular
2239 if (!h->def_regular)
2242 bed = get_elf_backend_data (info->output_bfd);
2243 p = strchr (h->root.root.string, ELF_VER_CHR);
2244 if (p != NULL && h->verinfo.vertree == NULL)
2246 struct bfd_elf_version_tree *t;
2249 if (*p == ELF_VER_CHR)
2252 /* If there is no version string, we can just return out. */
2256 /* Look for the version. If we find it, it is no longer weak. */
2257 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2259 if (strcmp (t->name, p) == 0)
2263 struct bfd_elf_version_expr *d;
2265 len = p - h->root.root.string;
2266 alc = (char *) bfd_malloc (len);
2269 sinfo->failed = TRUE;
2272 memcpy (alc, h->root.root.string, len - 1);
2273 alc[len - 1] = '\0';
2274 if (alc[len - 2] == ELF_VER_CHR)
2275 alc[len - 2] = '\0';
2277 h->verinfo.vertree = t;
2281 if (t->globals.list != NULL)
2282 d = (*t->match) (&t->globals, NULL, alc);
2284 /* See if there is anything to force this symbol to
2286 if (d == NULL && t->locals.list != NULL)
2288 d = (*t->match) (&t->locals, NULL, alc);
2291 && ! info->export_dynamic)
2292 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2300 /* If we are building an application, we need to create a
2301 version node for this version. */
2302 if (t == NULL && bfd_link_executable (info))
2304 struct bfd_elf_version_tree **pp;
2307 /* If we aren't going to export this symbol, we don't need
2308 to worry about it. */
2309 if (h->dynindx == -1)
2312 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2316 sinfo->failed = TRUE;
2321 t->name_indx = (unsigned int) -1;
2325 /* Don't count anonymous version tag. */
2326 if (sinfo->info->version_info != NULL
2327 && sinfo->info->version_info->vernum == 0)
2329 for (pp = &sinfo->info->version_info;
2333 t->vernum = version_index;
2337 h->verinfo.vertree = t;
2341 /* We could not find the version for a symbol when
2342 generating a shared archive. Return an error. */
2344 /* xgettext:c-format */
2345 (_("%B: version node not found for symbol %s"),
2346 info->output_bfd, h->root.root.string);
2347 bfd_set_error (bfd_error_bad_value);
2348 sinfo->failed = TRUE;
2353 /* If we don't have a version for this symbol, see if we can find
2355 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2360 = bfd_find_version_for_sym (sinfo->info->version_info,
2361 h->root.root.string, &hide);
2362 if (h->verinfo.vertree != NULL && hide)
2363 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2369 /* Read and swap the relocs from the section indicated by SHDR. This
2370 may be either a REL or a RELA section. The relocations are
2371 translated into RELA relocations and stored in INTERNAL_RELOCS,
2372 which should have already been allocated to contain enough space.
2373 The EXTERNAL_RELOCS are a buffer where the external form of the
2374 relocations should be stored.
2376 Returns FALSE if something goes wrong. */
2379 elf_link_read_relocs_from_section (bfd *abfd,
2381 Elf_Internal_Shdr *shdr,
2382 void *external_relocs,
2383 Elf_Internal_Rela *internal_relocs)
2385 const struct elf_backend_data *bed;
2386 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2387 const bfd_byte *erela;
2388 const bfd_byte *erelaend;
2389 Elf_Internal_Rela *irela;
2390 Elf_Internal_Shdr *symtab_hdr;
2393 /* Position ourselves at the start of the section. */
2394 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2397 /* Read the relocations. */
2398 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2401 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2402 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2404 bed = get_elf_backend_data (abfd);
2406 /* Convert the external relocations to the internal format. */
2407 if (shdr->sh_entsize == bed->s->sizeof_rel)
2408 swap_in = bed->s->swap_reloc_in;
2409 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2410 swap_in = bed->s->swap_reloca_in;
2413 bfd_set_error (bfd_error_wrong_format);
2417 erela = (const bfd_byte *) external_relocs;
2418 erelaend = erela + shdr->sh_size;
2419 irela = internal_relocs;
2420 while (erela < erelaend)
2424 (*swap_in) (abfd, erela, irela);
2425 r_symndx = ELF32_R_SYM (irela->r_info);
2426 if (bed->s->arch_size == 64)
2430 if ((size_t) r_symndx >= nsyms)
2433 /* xgettext:c-format */
2434 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2435 " for offset %#Lx in section `%A'"),
2436 abfd, r_symndx, (unsigned long) nsyms,
2437 irela->r_offset, sec);
2438 bfd_set_error (bfd_error_bad_value);
2442 else if (r_symndx != STN_UNDEF)
2445 /* xgettext:c-format */
2446 (_("%B: non-zero symbol index (%#Lx)"
2447 " for offset %#Lx in section `%A'"
2448 " when the object file has no symbol table"),
2450 irela->r_offset, sec);
2451 bfd_set_error (bfd_error_bad_value);
2454 irela += bed->s->int_rels_per_ext_rel;
2455 erela += shdr->sh_entsize;
2461 /* Read and swap the relocs for a section O. They may have been
2462 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2463 not NULL, they are used as buffers to read into. They are known to
2464 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2465 the return value is allocated using either malloc or bfd_alloc,
2466 according to the KEEP_MEMORY argument. If O has two relocation
2467 sections (both REL and RELA relocations), then the REL_HDR
2468 relocations will appear first in INTERNAL_RELOCS, followed by the
2469 RELA_HDR relocations. */
2472 _bfd_elf_link_read_relocs (bfd *abfd,
2474 void *external_relocs,
2475 Elf_Internal_Rela *internal_relocs,
2476 bfd_boolean keep_memory)
2478 void *alloc1 = NULL;
2479 Elf_Internal_Rela *alloc2 = NULL;
2480 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2481 struct bfd_elf_section_data *esdo = elf_section_data (o);
2482 Elf_Internal_Rela *internal_rela_relocs;
2484 if (esdo->relocs != NULL)
2485 return esdo->relocs;
2487 if (o->reloc_count == 0)
2490 if (internal_relocs == NULL)
2494 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2496 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2498 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2499 if (internal_relocs == NULL)
2503 if (external_relocs == NULL)
2505 bfd_size_type size = 0;
2508 size += esdo->rel.hdr->sh_size;
2510 size += esdo->rela.hdr->sh_size;
2512 alloc1 = bfd_malloc (size);
2515 external_relocs = alloc1;
2518 internal_rela_relocs = internal_relocs;
2521 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2525 external_relocs = (((bfd_byte *) external_relocs)
2526 + esdo->rel.hdr->sh_size);
2527 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2528 * bed->s->int_rels_per_ext_rel);
2532 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2534 internal_rela_relocs)))
2537 /* Cache the results for next time, if we can. */
2539 esdo->relocs = internal_relocs;
2544 /* Don't free alloc2, since if it was allocated we are passing it
2545 back (under the name of internal_relocs). */
2547 return internal_relocs;
2555 bfd_release (abfd, alloc2);
2562 /* Compute the size of, and allocate space for, REL_HDR which is the
2563 section header for a section containing relocations for O. */
2566 _bfd_elf_link_size_reloc_section (bfd *abfd,
2567 struct bfd_elf_section_reloc_data *reldata)
2569 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2571 /* That allows us to calculate the size of the section. */
2572 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2574 /* The contents field must last into write_object_contents, so we
2575 allocate it with bfd_alloc rather than malloc. Also since we
2576 cannot be sure that the contents will actually be filled in,
2577 we zero the allocated space. */
2578 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2579 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2582 if (reldata->hashes == NULL && reldata->count)
2584 struct elf_link_hash_entry **p;
2586 p = ((struct elf_link_hash_entry **)
2587 bfd_zmalloc (reldata->count * sizeof (*p)));
2591 reldata->hashes = p;
2597 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2598 originated from the section given by INPUT_REL_HDR) to the
2602 _bfd_elf_link_output_relocs (bfd *output_bfd,
2603 asection *input_section,
2604 Elf_Internal_Shdr *input_rel_hdr,
2605 Elf_Internal_Rela *internal_relocs,
2606 struct elf_link_hash_entry **rel_hash
2609 Elf_Internal_Rela *irela;
2610 Elf_Internal_Rela *irelaend;
2612 struct bfd_elf_section_reloc_data *output_reldata;
2613 asection *output_section;
2614 const struct elf_backend_data *bed;
2615 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2616 struct bfd_elf_section_data *esdo;
2618 output_section = input_section->output_section;
2620 bed = get_elf_backend_data (output_bfd);
2621 esdo = elf_section_data (output_section);
2622 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2624 output_reldata = &esdo->rel;
2625 swap_out = bed->s->swap_reloc_out;
2627 else if (esdo->rela.hdr
2628 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2630 output_reldata = &esdo->rela;
2631 swap_out = bed->s->swap_reloca_out;
2636 /* xgettext:c-format */
2637 (_("%B: relocation size mismatch in %B section %A"),
2638 output_bfd, input_section->owner, input_section);
2639 bfd_set_error (bfd_error_wrong_format);
2643 erel = output_reldata->hdr->contents;
2644 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2645 irela = internal_relocs;
2646 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2647 * bed->s->int_rels_per_ext_rel);
2648 while (irela < irelaend)
2650 (*swap_out) (output_bfd, irela, erel);
2651 irela += bed->s->int_rels_per_ext_rel;
2652 erel += input_rel_hdr->sh_entsize;
2655 /* Bump the counter, so that we know where to add the next set of
2657 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2662 /* Make weak undefined symbols in PIE dynamic. */
2665 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2666 struct elf_link_hash_entry *h)
2668 if (bfd_link_pie (info)
2670 && h->root.type == bfd_link_hash_undefweak)
2671 return bfd_elf_link_record_dynamic_symbol (info, h);
2676 /* Fix up the flags for a symbol. This handles various cases which
2677 can only be fixed after all the input files are seen. This is
2678 currently called by both adjust_dynamic_symbol and
2679 assign_sym_version, which is unnecessary but perhaps more robust in
2680 the face of future changes. */
2683 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2684 struct elf_info_failed *eif)
2686 const struct elf_backend_data *bed;
2688 /* If this symbol was mentioned in a non-ELF file, try to set
2689 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2690 permit a non-ELF file to correctly refer to a symbol defined in
2691 an ELF dynamic object. */
2694 while (h->root.type == bfd_link_hash_indirect)
2695 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2697 if (h->root.type != bfd_link_hash_defined
2698 && h->root.type != bfd_link_hash_defweak)
2701 h->ref_regular_nonweak = 1;
2705 if (h->root.u.def.section->owner != NULL
2706 && (bfd_get_flavour (h->root.u.def.section->owner)
2707 == bfd_target_elf_flavour))
2710 h->ref_regular_nonweak = 1;
2716 if (h->dynindx == -1
2720 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2729 /* Unfortunately, NON_ELF is only correct if the symbol
2730 was first seen in a non-ELF file. Fortunately, if the symbol
2731 was first seen in an ELF file, we're probably OK unless the
2732 symbol was defined in a non-ELF file. Catch that case here.
2733 FIXME: We're still in trouble if the symbol was first seen in
2734 a dynamic object, and then later in a non-ELF regular object. */
2735 if ((h->root.type == bfd_link_hash_defined
2736 || h->root.type == bfd_link_hash_defweak)
2738 && (h->root.u.def.section->owner != NULL
2739 ? (bfd_get_flavour (h->root.u.def.section->owner)
2740 != bfd_target_elf_flavour)
2741 : (bfd_is_abs_section (h->root.u.def.section)
2742 && !h->def_dynamic)))
2746 /* Backend specific symbol fixup. */
2747 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2748 if (bed->elf_backend_fixup_symbol
2749 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2752 /* If this is a final link, and the symbol was defined as a common
2753 symbol in a regular object file, and there was no definition in
2754 any dynamic object, then the linker will have allocated space for
2755 the symbol in a common section but the DEF_REGULAR
2756 flag will not have been set. */
2757 if (h->root.type == bfd_link_hash_defined
2761 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2764 /* If a weak undefined symbol has non-default visibility, we also
2765 hide it from the dynamic linker. */
2766 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2767 && h->root.type == bfd_link_hash_undefweak)
2768 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2770 /* A hidden versioned symbol in executable should be forced local if
2771 it is is locally defined, not referenced by shared library and not
2773 else if (bfd_link_executable (eif->info)
2774 && h->versioned == versioned_hidden
2775 && !eif->info->export_dynamic
2779 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2781 /* If -Bsymbolic was used (which means to bind references to global
2782 symbols to the definition within the shared object), and this
2783 symbol was defined in a regular object, then it actually doesn't
2784 need a PLT entry. Likewise, if the symbol has non-default
2785 visibility. If the symbol has hidden or internal visibility, we
2786 will force it local. */
2787 else if (h->needs_plt
2788 && bfd_link_pic (eif->info)
2789 && is_elf_hash_table (eif->info->hash)
2790 && (SYMBOLIC_BIND (eif->info, h)
2791 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2794 bfd_boolean force_local;
2796 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2797 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2798 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2801 /* If this is a weak defined symbol in a dynamic object, and we know
2802 the real definition in the dynamic object, copy interesting flags
2803 over to the real definition. */
2804 if (h->is_weakalias)
2806 struct elf_link_hash_entry *def = weakdef (h);
2808 /* If the real definition is defined by a regular object file,
2809 don't do anything special. See the longer description in
2810 _bfd_elf_adjust_dynamic_symbol, below. */
2811 if (def->def_regular)
2814 while ((h = h->u.alias) != def)
2815 h->is_weakalias = 0;
2819 while (h->root.type == bfd_link_hash_indirect)
2820 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2821 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2822 || h->root.type == bfd_link_hash_defweak);
2823 BFD_ASSERT (def->def_dynamic);
2824 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2825 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2832 /* Make the backend pick a good value for a dynamic symbol. This is
2833 called via elf_link_hash_traverse, and also calls itself
2837 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2839 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2840 struct elf_link_hash_table *htab;
2841 const struct elf_backend_data *bed;
2843 if (! is_elf_hash_table (eif->info->hash))
2846 /* Ignore indirect symbols. These are added by the versioning code. */
2847 if (h->root.type == bfd_link_hash_indirect)
2850 /* Fix the symbol flags. */
2851 if (! _bfd_elf_fix_symbol_flags (h, eif))
2854 htab = elf_hash_table (eif->info);
2855 bed = get_elf_backend_data (htab->dynobj);
2857 if (h->root.type == bfd_link_hash_undefweak)
2859 if (eif->info->dynamic_undefined_weak == 0)
2860 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2861 else if (eif->info->dynamic_undefined_weak > 0
2863 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2864 && !bfd_hide_sym_by_version (eif->info->version_info,
2865 h->root.root.string))
2867 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2875 /* If this symbol does not require a PLT entry, and it is not
2876 defined by a dynamic object, or is not referenced by a regular
2877 object, ignore it. We do have to handle a weak defined symbol,
2878 even if no regular object refers to it, if we decided to add it
2879 to the dynamic symbol table. FIXME: Do we normally need to worry
2880 about symbols which are defined by one dynamic object and
2881 referenced by another one? */
2883 && h->type != STT_GNU_IFUNC
2887 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2889 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2893 /* If we've already adjusted this symbol, don't do it again. This
2894 can happen via a recursive call. */
2895 if (h->dynamic_adjusted)
2898 /* Don't look at this symbol again. Note that we must set this
2899 after checking the above conditions, because we may look at a
2900 symbol once, decide not to do anything, and then get called
2901 recursively later after REF_REGULAR is set below. */
2902 h->dynamic_adjusted = 1;
2904 /* If this is a weak definition, and we know a real definition, and
2905 the real symbol is not itself defined by a regular object file,
2906 then get a good value for the real definition. We handle the
2907 real symbol first, for the convenience of the backend routine.
2909 Note that there is a confusing case here. If the real definition
2910 is defined by a regular object file, we don't get the real symbol
2911 from the dynamic object, but we do get the weak symbol. If the
2912 processor backend uses a COPY reloc, then if some routine in the
2913 dynamic object changes the real symbol, we will not see that
2914 change in the corresponding weak symbol. This is the way other
2915 ELF linkers work as well, and seems to be a result of the shared
2918 I will clarify this issue. Most SVR4 shared libraries define the
2919 variable _timezone and define timezone as a weak synonym. The
2920 tzset call changes _timezone. If you write
2921 extern int timezone;
2923 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2924 you might expect that, since timezone is a synonym for _timezone,
2925 the same number will print both times. However, if the processor
2926 backend uses a COPY reloc, then actually timezone will be copied
2927 into your process image, and, since you define _timezone
2928 yourself, _timezone will not. Thus timezone and _timezone will
2929 wind up at different memory locations. The tzset call will set
2930 _timezone, leaving timezone unchanged. */
2932 if (h->is_weakalias)
2934 struct elf_link_hash_entry *def = weakdef (h);
2936 /* If we get to this point, there is an implicit reference to
2937 the alias by a regular object file via the weak symbol H. */
2938 def->ref_regular = 1;
2940 /* Ensure that the backend adjust_dynamic_symbol function sees
2941 the strong alias before H by recursively calling ourselves. */
2942 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2946 /* If a symbol has no type and no size and does not require a PLT
2947 entry, then we are probably about to do the wrong thing here: we
2948 are probably going to create a COPY reloc for an empty object.
2949 This case can arise when a shared object is built with assembly
2950 code, and the assembly code fails to set the symbol type. */
2952 && h->type == STT_NOTYPE
2955 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2956 h->root.root.string);
2958 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2967 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2971 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2972 struct elf_link_hash_entry *h,
2975 unsigned int power_of_two;
2977 asection *sec = h->root.u.def.section;
2979 /* The section alignment of the definition is the maximum alignment
2980 requirement of symbols defined in the section. Since we don't
2981 know the symbol alignment requirement, we start with the
2982 maximum alignment and check low bits of the symbol address
2983 for the minimum alignment. */
2984 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2985 mask = ((bfd_vma) 1 << power_of_two) - 1;
2986 while ((h->root.u.def.value & mask) != 0)
2992 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2995 /* Adjust the section alignment if needed. */
2996 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3001 /* We make sure that the symbol will be aligned properly. */
3002 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3004 /* Define the symbol as being at this point in DYNBSS. */
3005 h->root.u.def.section = dynbss;
3006 h->root.u.def.value = dynbss->size;
3008 /* Increment the size of DYNBSS to make room for the symbol. */
3009 dynbss->size += h->size;
3011 /* No error if extern_protected_data is true. */
3012 if (h->protected_def
3013 && (!info->extern_protected_data
3014 || (info->extern_protected_data < 0
3015 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3016 info->callbacks->einfo
3017 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3018 h->root.root.string);
3023 /* Adjust all external symbols pointing into SEC_MERGE sections
3024 to reflect the object merging within the sections. */
3027 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3031 if ((h->root.type == bfd_link_hash_defined
3032 || h->root.type == bfd_link_hash_defweak)
3033 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3034 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3036 bfd *output_bfd = (bfd *) data;
3038 h->root.u.def.value =
3039 _bfd_merged_section_offset (output_bfd,
3040 &h->root.u.def.section,
3041 elf_section_data (sec)->sec_info,
3042 h->root.u.def.value);
3048 /* Returns false if the symbol referred to by H should be considered
3049 to resolve local to the current module, and true if it should be
3050 considered to bind dynamically. */
3053 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3054 struct bfd_link_info *info,
3055 bfd_boolean not_local_protected)
3057 bfd_boolean binding_stays_local_p;
3058 const struct elf_backend_data *bed;
3059 struct elf_link_hash_table *hash_table;
3064 while (h->root.type == bfd_link_hash_indirect
3065 || h->root.type == bfd_link_hash_warning)
3066 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3068 /* If it was forced local, then clearly it's not dynamic. */
3069 if (h->dynindx == -1)
3071 if (h->forced_local)
3074 /* Identify the cases where name binding rules say that a
3075 visible symbol resolves locally. */
3076 binding_stays_local_p = (bfd_link_executable (info)
3077 || SYMBOLIC_BIND (info, h));
3079 switch (ELF_ST_VISIBILITY (h->other))
3086 hash_table = elf_hash_table (info);
3087 if (!is_elf_hash_table (hash_table))
3090 bed = get_elf_backend_data (hash_table->dynobj);
3092 /* Proper resolution for function pointer equality may require
3093 that these symbols perhaps be resolved dynamically, even though
3094 we should be resolving them to the current module. */
3095 if (!not_local_protected || !bed->is_function_type (h->type))
3096 binding_stays_local_p = TRUE;
3103 /* If it isn't defined locally, then clearly it's dynamic. */
3104 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3107 /* Otherwise, the symbol is dynamic if binding rules don't tell
3108 us that it remains local. */
3109 return !binding_stays_local_p;
3112 /* Return true if the symbol referred to by H should be considered
3113 to resolve local to the current module, and false otherwise. Differs
3114 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3115 undefined symbols. The two functions are virtually identical except
3116 for the place where dynindx == -1 is tested. If that test is true,
3117 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3118 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3120 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3121 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3122 treatment of undefined weak symbols. For those that do not make
3123 undefined weak symbols dynamic, both functions may return false. */
3126 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3127 struct bfd_link_info *info,
3128 bfd_boolean local_protected)
3130 const struct elf_backend_data *bed;
3131 struct elf_link_hash_table *hash_table;
3133 /* If it's a local sym, of course we resolve locally. */
3137 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3138 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3139 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3142 /* Forced local symbols resolve locally. */
3143 if (h->forced_local)
3146 /* Common symbols that become definitions don't get the DEF_REGULAR
3147 flag set, so test it first, and don't bail out. */
3148 if (ELF_COMMON_DEF_P (h))
3150 /* If we don't have a definition in a regular file, then we can't
3151 resolve locally. The sym is either undefined or dynamic. */
3152 else if (!h->def_regular)
3155 /* Non-dynamic symbols resolve locally. */
3156 if (h->dynindx == -1)
3159 /* At this point, we know the symbol is defined and dynamic. In an
3160 executable it must resolve locally, likewise when building symbolic
3161 shared libraries. */
3162 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3165 /* Now deal with defined dynamic symbols in shared libraries. Ones
3166 with default visibility might not resolve locally. */
3167 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3170 hash_table = elf_hash_table (info);
3171 if (!is_elf_hash_table (hash_table))
3174 bed = get_elf_backend_data (hash_table->dynobj);
3176 /* If extern_protected_data is false, STV_PROTECTED non-function
3177 symbols are local. */
3178 if ((!info->extern_protected_data
3179 || (info->extern_protected_data < 0
3180 && !bed->extern_protected_data))
3181 && !bed->is_function_type (h->type))
3184 /* Function pointer equality tests may require that STV_PROTECTED
3185 symbols be treated as dynamic symbols. If the address of a
3186 function not defined in an executable is set to that function's
3187 plt entry in the executable, then the address of the function in
3188 a shared library must also be the plt entry in the executable. */
3189 return local_protected;
3192 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3193 aligned. Returns the first TLS output section. */
3195 struct bfd_section *
3196 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3198 struct bfd_section *sec, *tls;
3199 unsigned int align = 0;
3201 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3202 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3206 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3207 if (sec->alignment_power > align)
3208 align = sec->alignment_power;
3210 elf_hash_table (info)->tls_sec = tls;
3212 /* Ensure the alignment of the first section is the largest alignment,
3213 so that the tls segment starts aligned. */
3215 tls->alignment_power = align;
3220 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3222 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3223 Elf_Internal_Sym *sym)
3225 const struct elf_backend_data *bed;
3227 /* Local symbols do not count, but target specific ones might. */
3228 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3229 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3232 bed = get_elf_backend_data (abfd);
3233 /* Function symbols do not count. */
3234 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3237 /* If the section is undefined, then so is the symbol. */
3238 if (sym->st_shndx == SHN_UNDEF)
3241 /* If the symbol is defined in the common section, then
3242 it is a common definition and so does not count. */
3243 if (bed->common_definition (sym))
3246 /* If the symbol is in a target specific section then we
3247 must rely upon the backend to tell us what it is. */
3248 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3249 /* FIXME - this function is not coded yet:
3251 return _bfd_is_global_symbol_definition (abfd, sym);
3253 Instead for now assume that the definition is not global,
3254 Even if this is wrong, at least the linker will behave
3255 in the same way that it used to do. */
3261 /* Search the symbol table of the archive element of the archive ABFD
3262 whose archive map contains a mention of SYMDEF, and determine if
3263 the symbol is defined in this element. */
3265 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3267 Elf_Internal_Shdr * hdr;
3271 Elf_Internal_Sym *isymbuf;
3272 Elf_Internal_Sym *isym;
3273 Elf_Internal_Sym *isymend;
3276 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3280 if (! bfd_check_format (abfd, bfd_object))
3283 /* Select the appropriate symbol table. If we don't know if the
3284 object file is an IR object, give linker LTO plugin a chance to
3285 get the correct symbol table. */
3286 if (abfd->plugin_format == bfd_plugin_yes
3287 #if BFD_SUPPORTS_PLUGINS
3288 || (abfd->plugin_format == bfd_plugin_unknown
3289 && bfd_link_plugin_object_p (abfd))
3293 /* Use the IR symbol table if the object has been claimed by
3295 abfd = abfd->plugin_dummy_bfd;
3296 hdr = &elf_tdata (abfd)->symtab_hdr;
3298 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3299 hdr = &elf_tdata (abfd)->symtab_hdr;
3301 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3303 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3305 /* The sh_info field of the symtab header tells us where the
3306 external symbols start. We don't care about the local symbols. */
3307 if (elf_bad_symtab (abfd))
3309 extsymcount = symcount;
3314 extsymcount = symcount - hdr->sh_info;
3315 extsymoff = hdr->sh_info;
3318 if (extsymcount == 0)
3321 /* Read in the symbol table. */
3322 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3324 if (isymbuf == NULL)
3327 /* Scan the symbol table looking for SYMDEF. */
3329 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3333 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3338 if (strcmp (name, symdef->name) == 0)
3340 result = is_global_data_symbol_definition (abfd, isym);
3350 /* Add an entry to the .dynamic table. */
3353 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3357 struct elf_link_hash_table *hash_table;
3358 const struct elf_backend_data *bed;
3360 bfd_size_type newsize;
3361 bfd_byte *newcontents;
3362 Elf_Internal_Dyn dyn;
3364 hash_table = elf_hash_table (info);
3365 if (! is_elf_hash_table (hash_table))
3368 bed = get_elf_backend_data (hash_table->dynobj);
3369 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3370 BFD_ASSERT (s != NULL);
3372 newsize = s->size + bed->s->sizeof_dyn;
3373 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3374 if (newcontents == NULL)
3378 dyn.d_un.d_val = val;
3379 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3382 s->contents = newcontents;
3387 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3388 otherwise just check whether one already exists. Returns -1 on error,
3389 1 if a DT_NEEDED tag already exists, and 0 on success. */
3392 elf_add_dt_needed_tag (bfd *abfd,
3393 struct bfd_link_info *info,
3397 struct elf_link_hash_table *hash_table;
3400 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3403 hash_table = elf_hash_table (info);
3404 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3405 if (strindex == (size_t) -1)
3408 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3411 const struct elf_backend_data *bed;
3414 bed = get_elf_backend_data (hash_table->dynobj);
3415 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3417 for (extdyn = sdyn->contents;
3418 extdyn < sdyn->contents + sdyn->size;
3419 extdyn += bed->s->sizeof_dyn)
3421 Elf_Internal_Dyn dyn;
3423 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3424 if (dyn.d_tag == DT_NEEDED
3425 && dyn.d_un.d_val == strindex)
3427 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3435 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3438 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3442 /* We were just checking for existence of the tag. */
3443 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3448 /* Return true if SONAME is on the needed list between NEEDED and STOP
3449 (or the end of list if STOP is NULL), and needed by a library that
3453 on_needed_list (const char *soname,
3454 struct bfd_link_needed_list *needed,
3455 struct bfd_link_needed_list *stop)
3457 struct bfd_link_needed_list *look;
3458 for (look = needed; look != stop; look = look->next)
3459 if (strcmp (soname, look->name) == 0
3460 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3461 /* If needed by a library that itself is not directly
3462 needed, recursively check whether that library is
3463 indirectly needed. Since we add DT_NEEDED entries to
3464 the end of the list, library dependencies appear after
3465 the library. Therefore search prior to the current
3466 LOOK, preventing possible infinite recursion. */
3467 || on_needed_list (elf_dt_name (look->by), needed, look)))
3473 /* Sort symbol by value, section, and size. */
3475 elf_sort_symbol (const void *arg1, const void *arg2)
3477 const struct elf_link_hash_entry *h1;
3478 const struct elf_link_hash_entry *h2;
3479 bfd_signed_vma vdiff;
3481 h1 = *(const struct elf_link_hash_entry **) arg1;
3482 h2 = *(const struct elf_link_hash_entry **) arg2;
3483 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3485 return vdiff > 0 ? 1 : -1;
3488 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3490 return sdiff > 0 ? 1 : -1;
3492 vdiff = h1->size - h2->size;
3493 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3496 /* This function is used to adjust offsets into .dynstr for
3497 dynamic symbols. This is called via elf_link_hash_traverse. */
3500 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3502 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3504 if (h->dynindx != -1)
3505 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3509 /* Assign string offsets in .dynstr, update all structures referencing
3513 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3515 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3516 struct elf_link_local_dynamic_entry *entry;
3517 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3518 bfd *dynobj = hash_table->dynobj;
3521 const struct elf_backend_data *bed;
3524 _bfd_elf_strtab_finalize (dynstr);
3525 size = _bfd_elf_strtab_size (dynstr);
3527 bed = get_elf_backend_data (dynobj);
3528 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3529 BFD_ASSERT (sdyn != NULL);
3531 /* Update all .dynamic entries referencing .dynstr strings. */
3532 for (extdyn = sdyn->contents;
3533 extdyn < sdyn->contents + sdyn->size;
3534 extdyn += bed->s->sizeof_dyn)
3536 Elf_Internal_Dyn dyn;
3538 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3542 dyn.d_un.d_val = size;
3552 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3557 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3560 /* Now update local dynamic symbols. */
3561 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3562 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3563 entry->isym.st_name);
3565 /* And the rest of dynamic symbols. */
3566 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3568 /* Adjust version definitions. */
3569 if (elf_tdata (output_bfd)->cverdefs)
3574 Elf_Internal_Verdef def;
3575 Elf_Internal_Verdaux defaux;
3577 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3581 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3583 p += sizeof (Elf_External_Verdef);
3584 if (def.vd_aux != sizeof (Elf_External_Verdef))
3586 for (i = 0; i < def.vd_cnt; ++i)
3588 _bfd_elf_swap_verdaux_in (output_bfd,
3589 (Elf_External_Verdaux *) p, &defaux);
3590 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3592 _bfd_elf_swap_verdaux_out (output_bfd,
3593 &defaux, (Elf_External_Verdaux *) p);
3594 p += sizeof (Elf_External_Verdaux);
3597 while (def.vd_next);
3600 /* Adjust version references. */
3601 if (elf_tdata (output_bfd)->verref)
3606 Elf_Internal_Verneed need;
3607 Elf_Internal_Vernaux needaux;
3609 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3613 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3615 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3616 _bfd_elf_swap_verneed_out (output_bfd, &need,
3617 (Elf_External_Verneed *) p);
3618 p += sizeof (Elf_External_Verneed);
3619 for (i = 0; i < need.vn_cnt; ++i)
3621 _bfd_elf_swap_vernaux_in (output_bfd,
3622 (Elf_External_Vernaux *) p, &needaux);
3623 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3625 _bfd_elf_swap_vernaux_out (output_bfd,
3627 (Elf_External_Vernaux *) p);
3628 p += sizeof (Elf_External_Vernaux);
3631 while (need.vn_next);
3637 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3638 The default is to only match when the INPUT and OUTPUT are exactly
3642 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3643 const bfd_target *output)
3645 return input == output;
3648 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3649 This version is used when different targets for the same architecture
3650 are virtually identical. */
3653 _bfd_elf_relocs_compatible (const bfd_target *input,
3654 const bfd_target *output)
3656 const struct elf_backend_data *obed, *ibed;
3658 if (input == output)
3661 ibed = xvec_get_elf_backend_data (input);
3662 obed = xvec_get_elf_backend_data (output);
3664 if (ibed->arch != obed->arch)
3667 /* If both backends are using this function, deem them compatible. */
3668 return ibed->relocs_compatible == obed->relocs_compatible;
3671 /* Make a special call to the linker "notice" function to tell it that
3672 we are about to handle an as-needed lib, or have finished
3673 processing the lib. */
3676 _bfd_elf_notice_as_needed (bfd *ibfd,
3677 struct bfd_link_info *info,
3678 enum notice_asneeded_action act)
3680 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3683 /* Check relocations an ELF object file. */
3686 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3688 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3689 struct elf_link_hash_table *htab = elf_hash_table (info);
3691 /* If this object is the same format as the output object, and it is
3692 not a shared library, then let the backend look through the
3695 This is required to build global offset table entries and to
3696 arrange for dynamic relocs. It is not required for the
3697 particular common case of linking non PIC code, even when linking
3698 against shared libraries, but unfortunately there is no way of
3699 knowing whether an object file has been compiled PIC or not.
3700 Looking through the relocs is not particularly time consuming.
3701 The problem is that we must either (1) keep the relocs in memory,
3702 which causes the linker to require additional runtime memory or
3703 (2) read the relocs twice from the input file, which wastes time.
3704 This would be a good case for using mmap.
3706 I have no idea how to handle linking PIC code into a file of a
3707 different format. It probably can't be done. */
3708 if ((abfd->flags & DYNAMIC) == 0
3709 && is_elf_hash_table (htab)
3710 && bed->check_relocs != NULL
3711 && elf_object_id (abfd) == elf_hash_table_id (htab)
3712 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3716 for (o = abfd->sections; o != NULL; o = o->next)
3718 Elf_Internal_Rela *internal_relocs;
3721 /* Don't check relocations in excluded sections. */
3722 if ((o->flags & SEC_RELOC) == 0
3723 || (o->flags & SEC_EXCLUDE) != 0
3724 || o->reloc_count == 0
3725 || ((info->strip == strip_all || info->strip == strip_debugger)
3726 && (o->flags & SEC_DEBUGGING) != 0)
3727 || bfd_is_abs_section (o->output_section))
3730 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3732 if (internal_relocs == NULL)
3735 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3737 if (elf_section_data (o)->relocs != internal_relocs)
3738 free (internal_relocs);
3748 /* Add symbols from an ELF object file to the linker hash table. */
3751 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3753 Elf_Internal_Ehdr *ehdr;
3754 Elf_Internal_Shdr *hdr;
3758 struct elf_link_hash_entry **sym_hash;
3759 bfd_boolean dynamic;
3760 Elf_External_Versym *extversym = NULL;
3761 Elf_External_Versym *ever;
3762 struct elf_link_hash_entry *weaks;
3763 struct elf_link_hash_entry **nondeflt_vers = NULL;
3764 size_t nondeflt_vers_cnt = 0;
3765 Elf_Internal_Sym *isymbuf = NULL;
3766 Elf_Internal_Sym *isym;
3767 Elf_Internal_Sym *isymend;
3768 const struct elf_backend_data *bed;
3769 bfd_boolean add_needed;
3770 struct elf_link_hash_table *htab;
3772 void *alloc_mark = NULL;
3773 struct bfd_hash_entry **old_table = NULL;
3774 unsigned int old_size = 0;
3775 unsigned int old_count = 0;
3776 void *old_tab = NULL;
3778 struct bfd_link_hash_entry *old_undefs = NULL;
3779 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3780 void *old_strtab = NULL;
3783 bfd_boolean just_syms;
3785 htab = elf_hash_table (info);
3786 bed = get_elf_backend_data (abfd);
3788 if ((abfd->flags & DYNAMIC) == 0)
3794 /* You can't use -r against a dynamic object. Also, there's no
3795 hope of using a dynamic object which does not exactly match
3796 the format of the output file. */
3797 if (bfd_link_relocatable (info)
3798 || !is_elf_hash_table (htab)
3799 || info->output_bfd->xvec != abfd->xvec)
3801 if (bfd_link_relocatable (info))
3802 bfd_set_error (bfd_error_invalid_operation);
3804 bfd_set_error (bfd_error_wrong_format);
3809 ehdr = elf_elfheader (abfd);
3810 if (info->warn_alternate_em
3811 && bed->elf_machine_code != ehdr->e_machine
3812 && ((bed->elf_machine_alt1 != 0
3813 && ehdr->e_machine == bed->elf_machine_alt1)
3814 || (bed->elf_machine_alt2 != 0
3815 && ehdr->e_machine == bed->elf_machine_alt2)))
3816 info->callbacks->einfo
3817 /* xgettext:c-format */
3818 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3819 ehdr->e_machine, abfd, bed->elf_machine_code);
3821 /* As a GNU extension, any input sections which are named
3822 .gnu.warning.SYMBOL are treated as warning symbols for the given
3823 symbol. This differs from .gnu.warning sections, which generate
3824 warnings when they are included in an output file. */
3825 /* PR 12761: Also generate this warning when building shared libraries. */
3826 for (s = abfd->sections; s != NULL; s = s->next)
3830 name = bfd_get_section_name (abfd, s);
3831 if (CONST_STRNEQ (name, ".gnu.warning."))
3836 name += sizeof ".gnu.warning." - 1;
3838 /* If this is a shared object, then look up the symbol
3839 in the hash table. If it is there, and it is already
3840 been defined, then we will not be using the entry
3841 from this shared object, so we don't need to warn.
3842 FIXME: If we see the definition in a regular object
3843 later on, we will warn, but we shouldn't. The only
3844 fix is to keep track of what warnings we are supposed
3845 to emit, and then handle them all at the end of the
3849 struct elf_link_hash_entry *h;
3851 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3853 /* FIXME: What about bfd_link_hash_common? */
3855 && (h->root.type == bfd_link_hash_defined
3856 || h->root.type == bfd_link_hash_defweak))
3861 msg = (char *) bfd_alloc (abfd, sz + 1);
3865 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3870 if (! (_bfd_generic_link_add_one_symbol
3871 (info, abfd, name, BSF_WARNING, s, 0, msg,
3872 FALSE, bed->collect, NULL)))
3875 if (bfd_link_executable (info))
3877 /* Clobber the section size so that the warning does
3878 not get copied into the output file. */
3881 /* Also set SEC_EXCLUDE, so that symbols defined in
3882 the warning section don't get copied to the output. */
3883 s->flags |= SEC_EXCLUDE;
3888 just_syms = ((s = abfd->sections) != NULL
3889 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3894 /* If we are creating a shared library, create all the dynamic
3895 sections immediately. We need to attach them to something,
3896 so we attach them to this BFD, provided it is the right
3897 format and is not from ld --just-symbols. Always create the
3898 dynamic sections for -E/--dynamic-list. FIXME: If there
3899 are no input BFD's of the same format as the output, we can't
3900 make a shared library. */
3902 && (bfd_link_pic (info)
3903 || (!bfd_link_relocatable (info)
3905 && (info->export_dynamic || info->dynamic)))
3906 && is_elf_hash_table (htab)
3907 && info->output_bfd->xvec == abfd->xvec
3908 && !htab->dynamic_sections_created)
3910 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3914 else if (!is_elf_hash_table (htab))
3918 const char *soname = NULL;
3920 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3921 const Elf_Internal_Phdr *phdr;
3924 /* ld --just-symbols and dynamic objects don't mix very well.
3925 ld shouldn't allow it. */
3929 /* If this dynamic lib was specified on the command line with
3930 --as-needed in effect, then we don't want to add a DT_NEEDED
3931 tag unless the lib is actually used. Similary for libs brought
3932 in by another lib's DT_NEEDED. When --no-add-needed is used
3933 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3934 any dynamic library in DT_NEEDED tags in the dynamic lib at
3936 add_needed = (elf_dyn_lib_class (abfd)
3937 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3938 | DYN_NO_NEEDED)) == 0;
3940 s = bfd_get_section_by_name (abfd, ".dynamic");
3945 unsigned int elfsec;
3946 unsigned long shlink;
3948 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3955 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3956 if (elfsec == SHN_BAD)
3957 goto error_free_dyn;
3958 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3960 for (extdyn = dynbuf;
3961 extdyn < dynbuf + s->size;
3962 extdyn += bed->s->sizeof_dyn)
3964 Elf_Internal_Dyn dyn;
3966 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3967 if (dyn.d_tag == DT_SONAME)
3969 unsigned int tagv = dyn.d_un.d_val;
3970 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3972 goto error_free_dyn;
3974 if (dyn.d_tag == DT_NEEDED)
3976 struct bfd_link_needed_list *n, **pn;
3978 unsigned int tagv = dyn.d_un.d_val;
3980 amt = sizeof (struct bfd_link_needed_list);
3981 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3982 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3983 if (n == NULL || fnm == NULL)
3984 goto error_free_dyn;
3985 amt = strlen (fnm) + 1;
3986 anm = (char *) bfd_alloc (abfd, amt);
3988 goto error_free_dyn;
3989 memcpy (anm, fnm, amt);
3993 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3997 if (dyn.d_tag == DT_RUNPATH)
3999 struct bfd_link_needed_list *n, **pn;
4001 unsigned int tagv = dyn.d_un.d_val;
4003 amt = sizeof (struct bfd_link_needed_list);
4004 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4005 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4006 if (n == NULL || fnm == NULL)
4007 goto error_free_dyn;
4008 amt = strlen (fnm) + 1;
4009 anm = (char *) bfd_alloc (abfd, amt);
4011 goto error_free_dyn;
4012 memcpy (anm, fnm, amt);
4016 for (pn = & runpath;
4022 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4023 if (!runpath && dyn.d_tag == DT_RPATH)
4025 struct bfd_link_needed_list *n, **pn;
4027 unsigned int tagv = dyn.d_un.d_val;
4029 amt = sizeof (struct bfd_link_needed_list);
4030 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4031 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4032 if (n == NULL || fnm == NULL)
4033 goto error_free_dyn;
4034 amt = strlen (fnm) + 1;
4035 anm = (char *) bfd_alloc (abfd, amt);
4037 goto error_free_dyn;
4038 memcpy (anm, fnm, amt);
4048 if (dyn.d_tag == DT_AUDIT)
4050 unsigned int tagv = dyn.d_un.d_val;
4051 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4058 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4059 frees all more recently bfd_alloc'd blocks as well. */
4065 struct bfd_link_needed_list **pn;
4066 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4071 /* If we have a PT_GNU_RELRO program header, mark as read-only
4072 all sections contained fully therein. This makes relro
4073 shared library sections appear as they will at run-time. */
4074 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4075 while (--phdr >= elf_tdata (abfd)->phdr)
4076 if (phdr->p_type == PT_GNU_RELRO)
4078 for (s = abfd->sections; s != NULL; s = s->next)
4079 if ((s->flags & SEC_ALLOC) != 0
4080 && s->vma >= phdr->p_vaddr
4081 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4082 s->flags |= SEC_READONLY;
4086 /* We do not want to include any of the sections in a dynamic
4087 object in the output file. We hack by simply clobbering the
4088 list of sections in the BFD. This could be handled more
4089 cleanly by, say, a new section flag; the existing
4090 SEC_NEVER_LOAD flag is not the one we want, because that one
4091 still implies that the section takes up space in the output
4093 bfd_section_list_clear (abfd);
4095 /* Find the name to use in a DT_NEEDED entry that refers to this
4096 object. If the object has a DT_SONAME entry, we use it.
4097 Otherwise, if the generic linker stuck something in
4098 elf_dt_name, we use that. Otherwise, we just use the file
4100 if (soname == NULL || *soname == '\0')
4102 soname = elf_dt_name (abfd);
4103 if (soname == NULL || *soname == '\0')
4104 soname = bfd_get_filename (abfd);
4107 /* Save the SONAME because sometimes the linker emulation code
4108 will need to know it. */
4109 elf_dt_name (abfd) = soname;
4111 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4115 /* If we have already included this dynamic object in the
4116 link, just ignore it. There is no reason to include a
4117 particular dynamic object more than once. */
4121 /* Save the DT_AUDIT entry for the linker emulation code. */
4122 elf_dt_audit (abfd) = audit;
4125 /* If this is a dynamic object, we always link against the .dynsym
4126 symbol table, not the .symtab symbol table. The dynamic linker
4127 will only see the .dynsym symbol table, so there is no reason to
4128 look at .symtab for a dynamic object. */
4130 if (! dynamic || elf_dynsymtab (abfd) == 0)
4131 hdr = &elf_tdata (abfd)->symtab_hdr;
4133 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4135 symcount = hdr->sh_size / bed->s->sizeof_sym;
4137 /* The sh_info field of the symtab header tells us where the
4138 external symbols start. We don't care about the local symbols at
4140 if (elf_bad_symtab (abfd))
4142 extsymcount = symcount;
4147 extsymcount = symcount - hdr->sh_info;
4148 extsymoff = hdr->sh_info;
4151 sym_hash = elf_sym_hashes (abfd);
4152 if (extsymcount != 0)
4154 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4156 if (isymbuf == NULL)
4159 if (sym_hash == NULL)
4161 /* We store a pointer to the hash table entry for each
4164 amt *= sizeof (struct elf_link_hash_entry *);
4165 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4166 if (sym_hash == NULL)
4167 goto error_free_sym;
4168 elf_sym_hashes (abfd) = sym_hash;
4174 /* Read in any version definitions. */
4175 if (!_bfd_elf_slurp_version_tables (abfd,
4176 info->default_imported_symver))
4177 goto error_free_sym;
4179 /* Read in the symbol versions, but don't bother to convert them
4180 to internal format. */
4181 if (elf_dynversym (abfd) != 0)
4183 Elf_Internal_Shdr *versymhdr;
4185 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4186 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4187 if (extversym == NULL)
4188 goto error_free_sym;
4189 amt = versymhdr->sh_size;
4190 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4191 || bfd_bread (extversym, amt, abfd) != amt)
4192 goto error_free_vers;
4196 /* If we are loading an as-needed shared lib, save the symbol table
4197 state before we start adding symbols. If the lib turns out
4198 to be unneeded, restore the state. */
4199 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4204 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4206 struct bfd_hash_entry *p;
4207 struct elf_link_hash_entry *h;
4209 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4211 h = (struct elf_link_hash_entry *) p;
4212 entsize += htab->root.table.entsize;
4213 if (h->root.type == bfd_link_hash_warning)
4214 entsize += htab->root.table.entsize;
4218 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4219 old_tab = bfd_malloc (tabsize + entsize);
4220 if (old_tab == NULL)
4221 goto error_free_vers;
4223 /* Remember the current objalloc pointer, so that all mem for
4224 symbols added can later be reclaimed. */
4225 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4226 if (alloc_mark == NULL)
4227 goto error_free_vers;
4229 /* Make a special call to the linker "notice" function to
4230 tell it that we are about to handle an as-needed lib. */
4231 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4232 goto error_free_vers;
4234 /* Clone the symbol table. Remember some pointers into the
4235 symbol table, and dynamic symbol count. */
4236 old_ent = (char *) old_tab + tabsize;
4237 memcpy (old_tab, htab->root.table.table, tabsize);
4238 old_undefs = htab->root.undefs;
4239 old_undefs_tail = htab->root.undefs_tail;
4240 old_table = htab->root.table.table;
4241 old_size = htab->root.table.size;
4242 old_count = htab->root.table.count;
4243 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4244 if (old_strtab == NULL)
4245 goto error_free_vers;
4247 for (i = 0; i < htab->root.table.size; i++)
4249 struct bfd_hash_entry *p;
4250 struct elf_link_hash_entry *h;
4252 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4254 memcpy (old_ent, p, htab->root.table.entsize);
4255 old_ent = (char *) old_ent + htab->root.table.entsize;
4256 h = (struct elf_link_hash_entry *) p;
4257 if (h->root.type == bfd_link_hash_warning)
4259 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4260 old_ent = (char *) old_ent + htab->root.table.entsize;
4267 ever = extversym != NULL ? extversym + extsymoff : NULL;
4268 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4270 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4274 asection *sec, *new_sec;
4277 struct elf_link_hash_entry *h;
4278 struct elf_link_hash_entry *hi;
4279 bfd_boolean definition;
4280 bfd_boolean size_change_ok;
4281 bfd_boolean type_change_ok;
4282 bfd_boolean new_weak;
4283 bfd_boolean old_weak;
4284 bfd_boolean override;
4286 bfd_boolean discarded;
4287 unsigned int old_alignment;
4289 bfd_boolean matched;
4293 flags = BSF_NO_FLAGS;
4295 value = isym->st_value;
4296 common = bed->common_definition (isym);
4297 if (common && info->inhibit_common_definition)
4299 /* Treat common symbol as undefined for --no-define-common. */
4300 isym->st_shndx = SHN_UNDEF;
4305 bind = ELF_ST_BIND (isym->st_info);
4309 /* This should be impossible, since ELF requires that all
4310 global symbols follow all local symbols, and that sh_info
4311 point to the first global symbol. Unfortunately, Irix 5
4316 if (isym->st_shndx != SHN_UNDEF && !common)
4324 case STB_GNU_UNIQUE:
4325 flags = BSF_GNU_UNIQUE;
4329 /* Leave it up to the processor backend. */
4333 if (isym->st_shndx == SHN_UNDEF)
4334 sec = bfd_und_section_ptr;
4335 else if (isym->st_shndx == SHN_ABS)
4336 sec = bfd_abs_section_ptr;
4337 else if (isym->st_shndx == SHN_COMMON)
4339 sec = bfd_com_section_ptr;
4340 /* What ELF calls the size we call the value. What ELF
4341 calls the value we call the alignment. */
4342 value = isym->st_size;
4346 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4348 sec = bfd_abs_section_ptr;
4349 else if (discarded_section (sec))
4351 /* Symbols from discarded section are undefined. We keep
4353 sec = bfd_und_section_ptr;
4355 isym->st_shndx = SHN_UNDEF;
4357 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4361 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4364 goto error_free_vers;
4366 if (isym->st_shndx == SHN_COMMON
4367 && (abfd->flags & BFD_PLUGIN) != 0)
4369 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4373 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4375 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4377 goto error_free_vers;
4381 else if (isym->st_shndx == SHN_COMMON
4382 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4383 && !bfd_link_relocatable (info))
4385 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4389 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4390 | SEC_LINKER_CREATED);
4391 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4393 goto error_free_vers;
4397 else if (bed->elf_add_symbol_hook)
4399 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4401 goto error_free_vers;
4403 /* The hook function sets the name to NULL if this symbol
4404 should be skipped for some reason. */
4409 /* Sanity check that all possibilities were handled. */
4412 bfd_set_error (bfd_error_bad_value);
4413 goto error_free_vers;
4416 /* Silently discard TLS symbols from --just-syms. There's
4417 no way to combine a static TLS block with a new TLS block
4418 for this executable. */
4419 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4420 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4423 if (bfd_is_und_section (sec)
4424 || bfd_is_com_section (sec))
4429 size_change_ok = FALSE;
4430 type_change_ok = bed->type_change_ok;
4437 if (is_elf_hash_table (htab))
4439 Elf_Internal_Versym iver;
4440 unsigned int vernum = 0;
4445 if (info->default_imported_symver)
4446 /* Use the default symbol version created earlier. */
4447 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4452 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4454 vernum = iver.vs_vers & VERSYM_VERSION;
4456 /* If this is a hidden symbol, or if it is not version
4457 1, we append the version name to the symbol name.
4458 However, we do not modify a non-hidden absolute symbol
4459 if it is not a function, because it might be the version
4460 symbol itself. FIXME: What if it isn't? */
4461 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4463 && (!bfd_is_abs_section (sec)
4464 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4467 size_t namelen, verlen, newlen;
4470 if (isym->st_shndx != SHN_UNDEF)
4472 if (vernum > elf_tdata (abfd)->cverdefs)
4474 else if (vernum > 1)
4476 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4483 /* xgettext:c-format */
4484 (_("%B: %s: invalid version %u (max %d)"),
4486 elf_tdata (abfd)->cverdefs);
4487 bfd_set_error (bfd_error_bad_value);
4488 goto error_free_vers;
4493 /* We cannot simply test for the number of
4494 entries in the VERNEED section since the
4495 numbers for the needed versions do not start
4497 Elf_Internal_Verneed *t;
4500 for (t = elf_tdata (abfd)->verref;
4504 Elf_Internal_Vernaux *a;
4506 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4508 if (a->vna_other == vernum)
4510 verstr = a->vna_nodename;
4520 /* xgettext:c-format */
4521 (_("%B: %s: invalid needed version %d"),
4522 abfd, name, vernum);
4523 bfd_set_error (bfd_error_bad_value);
4524 goto error_free_vers;
4528 namelen = strlen (name);
4529 verlen = strlen (verstr);
4530 newlen = namelen + verlen + 2;
4531 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4532 && isym->st_shndx != SHN_UNDEF)
4535 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4536 if (newname == NULL)
4537 goto error_free_vers;
4538 memcpy (newname, name, namelen);
4539 p = newname + namelen;
4541 /* If this is a defined non-hidden version symbol,
4542 we add another @ to the name. This indicates the
4543 default version of the symbol. */
4544 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4545 && isym->st_shndx != SHN_UNDEF)
4547 memcpy (p, verstr, verlen + 1);
4552 /* If this symbol has default visibility and the user has
4553 requested we not re-export it, then mark it as hidden. */
4554 if (!bfd_is_und_section (sec)
4557 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4558 isym->st_other = (STV_HIDDEN
4559 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4561 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4562 sym_hash, &old_bfd, &old_weak,
4563 &old_alignment, &skip, &override,
4564 &type_change_ok, &size_change_ok,
4566 goto error_free_vers;
4571 /* Override a definition only if the new symbol matches the
4573 if (override && matched)
4577 while (h->root.type == bfd_link_hash_indirect
4578 || h->root.type == bfd_link_hash_warning)
4579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4581 if (elf_tdata (abfd)->verdef != NULL
4584 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4587 if (! (_bfd_generic_link_add_one_symbol
4588 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4589 (struct bfd_link_hash_entry **) sym_hash)))
4590 goto error_free_vers;
4592 if ((flags & BSF_GNU_UNIQUE)
4593 && (abfd->flags & DYNAMIC) == 0
4594 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4595 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4598 /* We need to make sure that indirect symbol dynamic flags are
4601 while (h->root.type == bfd_link_hash_indirect
4602 || h->root.type == bfd_link_hash_warning)
4603 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4605 /* Setting the index to -3 tells elf_link_output_extsym that
4606 this symbol is defined in a discarded section. */
4612 new_weak = (flags & BSF_WEAK) != 0;
4616 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4617 && is_elf_hash_table (htab)
4618 && h->u.alias == NULL)
4620 /* Keep a list of all weak defined non function symbols from
4621 a dynamic object, using the alias field. Later in this
4622 function we will set the alias field to the correct
4623 value. We only put non-function symbols from dynamic
4624 objects on this list, because that happens to be the only
4625 time we need to know the normal symbol corresponding to a
4626 weak symbol, and the information is time consuming to
4627 figure out. If the alias field is not already NULL,
4628 then this symbol was already defined by some previous
4629 dynamic object, and we will be using that previous
4630 definition anyhow. */
4636 /* Set the alignment of a common symbol. */
4637 if ((common || bfd_is_com_section (sec))
4638 && h->root.type == bfd_link_hash_common)
4643 align = bfd_log2 (isym->st_value);
4646 /* The new symbol is a common symbol in a shared object.
4647 We need to get the alignment from the section. */
4648 align = new_sec->alignment_power;
4650 if (align > old_alignment)
4651 h->root.u.c.p->alignment_power = align;
4653 h->root.u.c.p->alignment_power = old_alignment;
4656 if (is_elf_hash_table (htab))
4658 /* Set a flag in the hash table entry indicating the type of
4659 reference or definition we just found. A dynamic symbol
4660 is one which is referenced or defined by both a regular
4661 object and a shared object. */
4662 bfd_boolean dynsym = FALSE;
4664 /* Plugin symbols aren't normal. Don't set def_regular or
4665 ref_regular for them, or make them dynamic. */
4666 if ((abfd->flags & BFD_PLUGIN) != 0)
4673 if (bind != STB_WEAK)
4674 h->ref_regular_nonweak = 1;
4686 /* If the indirect symbol has been forced local, don't
4687 make the real symbol dynamic. */
4688 if ((h == hi || !hi->forced_local)
4689 && (bfd_link_dll (info)
4699 hi->ref_dynamic = 1;
4704 hi->def_dynamic = 1;
4707 /* If the indirect symbol has been forced local, don't
4708 make the real symbol dynamic. */
4709 if ((h == hi || !hi->forced_local)
4713 && weakdef (h)->dynindx != -1)))
4717 /* Check to see if we need to add an indirect symbol for
4718 the default name. */
4720 || (!override && h->root.type == bfd_link_hash_common))
4721 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4722 sec, value, &old_bfd, &dynsym))
4723 goto error_free_vers;
4725 /* Check the alignment when a common symbol is involved. This
4726 can change when a common symbol is overridden by a normal
4727 definition or a common symbol is ignored due to the old
4728 normal definition. We need to make sure the maximum
4729 alignment is maintained. */
4730 if ((old_alignment || common)
4731 && h->root.type != bfd_link_hash_common)
4733 unsigned int common_align;
4734 unsigned int normal_align;
4735 unsigned int symbol_align;
4739 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4740 || h->root.type == bfd_link_hash_defweak);
4742 symbol_align = ffs (h->root.u.def.value) - 1;
4743 if (h->root.u.def.section->owner != NULL
4744 && (h->root.u.def.section->owner->flags
4745 & (DYNAMIC | BFD_PLUGIN)) == 0)
4747 normal_align = h->root.u.def.section->alignment_power;
4748 if (normal_align > symbol_align)
4749 normal_align = symbol_align;
4752 normal_align = symbol_align;
4756 common_align = old_alignment;
4757 common_bfd = old_bfd;
4762 common_align = bfd_log2 (isym->st_value);
4764 normal_bfd = old_bfd;
4767 if (normal_align < common_align)
4769 /* PR binutils/2735 */
4770 if (normal_bfd == NULL)
4772 /* xgettext:c-format */
4773 (_("Warning: alignment %u of common symbol `%s' in %B is"
4774 " greater than the alignment (%u) of its section %A"),
4775 1 << common_align, name, common_bfd,
4776 1 << normal_align, h->root.u.def.section);
4779 /* xgettext:c-format */
4780 (_("Warning: alignment %u of symbol `%s' in %B"
4781 " is smaller than %u in %B"),
4782 1 << normal_align, name, normal_bfd,
4783 1 << common_align, common_bfd);
4787 /* Remember the symbol size if it isn't undefined. */
4788 if (isym->st_size != 0
4789 && isym->st_shndx != SHN_UNDEF
4790 && (definition || h->size == 0))
4793 && h->size != isym->st_size
4794 && ! size_change_ok)
4796 /* xgettext:c-format */
4797 (_("Warning: size of symbol `%s' changed"
4798 " from %Lu in %B to %Lu in %B"),
4799 name, h->size, old_bfd, isym->st_size, abfd);
4801 h->size = isym->st_size;
4804 /* If this is a common symbol, then we always want H->SIZE
4805 to be the size of the common symbol. The code just above
4806 won't fix the size if a common symbol becomes larger. We
4807 don't warn about a size change here, because that is
4808 covered by --warn-common. Allow changes between different
4810 if (h->root.type == bfd_link_hash_common)
4811 h->size = h->root.u.c.size;
4813 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4814 && ((definition && !new_weak)
4815 || (old_weak && h->root.type == bfd_link_hash_common)
4816 || h->type == STT_NOTYPE))
4818 unsigned int type = ELF_ST_TYPE (isym->st_info);
4820 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4822 if (type == STT_GNU_IFUNC
4823 && (abfd->flags & DYNAMIC) != 0)
4826 if (h->type != type)
4828 if (h->type != STT_NOTYPE && ! type_change_ok)
4829 /* xgettext:c-format */
4831 (_("Warning: type of symbol `%s' changed"
4832 " from %d to %d in %B"),
4833 name, h->type, type, abfd);
4839 /* Merge st_other field. */
4840 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4842 /* We don't want to make debug symbol dynamic. */
4844 && (sec->flags & SEC_DEBUGGING)
4845 && !bfd_link_relocatable (info))
4848 /* Nor should we make plugin symbols dynamic. */
4849 if ((abfd->flags & BFD_PLUGIN) != 0)
4854 h->target_internal = isym->st_target_internal;
4855 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4858 if (definition && !dynamic)
4860 char *p = strchr (name, ELF_VER_CHR);
4861 if (p != NULL && p[1] != ELF_VER_CHR)
4863 /* Queue non-default versions so that .symver x, x@FOO
4864 aliases can be checked. */
4867 amt = ((isymend - isym + 1)
4868 * sizeof (struct elf_link_hash_entry *));
4870 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4872 goto error_free_vers;
4874 nondeflt_vers[nondeflt_vers_cnt++] = h;
4878 if (dynsym && h->dynindx == -1)
4880 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4881 goto error_free_vers;
4883 && weakdef (h)->dynindx == -1)
4885 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4886 goto error_free_vers;
4889 else if (h->dynindx != -1)
4890 /* If the symbol already has a dynamic index, but
4891 visibility says it should not be visible, turn it into
4893 switch (ELF_ST_VISIBILITY (h->other))
4897 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4902 /* Don't add DT_NEEDED for references from the dummy bfd nor
4903 for unmatched symbol. */
4908 && h->ref_regular_nonweak
4910 || (old_bfd->flags & BFD_PLUGIN) == 0))
4911 || (h->ref_dynamic_nonweak
4912 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4913 && !on_needed_list (elf_dt_name (abfd),
4914 htab->needed, NULL))))
4917 const char *soname = elf_dt_name (abfd);
4919 info->callbacks->minfo ("%!", soname, old_bfd,
4920 h->root.root.string);
4922 /* A symbol from a library loaded via DT_NEEDED of some
4923 other library is referenced by a regular object.
4924 Add a DT_NEEDED entry for it. Issue an error if
4925 --no-add-needed is used and the reference was not
4928 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4931 /* xgettext:c-format */
4932 (_("%B: undefined reference to symbol '%s'"),
4934 bfd_set_error (bfd_error_missing_dso);
4935 goto error_free_vers;
4938 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4939 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4942 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4944 goto error_free_vers;
4946 BFD_ASSERT (ret == 0);
4951 if (info->lto_plugin_active
4952 && !bfd_link_relocatable (info)
4953 && (abfd->flags & BFD_PLUGIN) == 0
4959 if (bed->s->arch_size == 32)
4964 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4965 referenced in regular objects so that linker plugin will get
4966 the correct symbol resolution. */
4968 sym_hash = elf_sym_hashes (abfd);
4969 for (s = abfd->sections; s != NULL; s = s->next)
4971 Elf_Internal_Rela *internal_relocs;
4972 Elf_Internal_Rela *rel, *relend;
4974 /* Don't check relocations in excluded sections. */
4975 if ((s->flags & SEC_RELOC) == 0
4976 || s->reloc_count == 0
4977 || (s->flags & SEC_EXCLUDE) != 0
4978 || ((info->strip == strip_all
4979 || info->strip == strip_debugger)
4980 && (s->flags & SEC_DEBUGGING) != 0))
4983 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4986 if (internal_relocs == NULL)
4987 goto error_free_vers;
4989 rel = internal_relocs;
4990 relend = rel + s->reloc_count;
4991 for ( ; rel < relend; rel++)
4993 unsigned long r_symndx = rel->r_info >> r_sym_shift;
4994 struct elf_link_hash_entry *h;
4996 /* Skip local symbols. */
4997 if (r_symndx < extsymoff)
5000 h = sym_hash[r_symndx - extsymoff];
5002 h->root.non_ir_ref_regular = 1;
5005 if (elf_section_data (s)->relocs != internal_relocs)
5006 free (internal_relocs);
5010 if (extversym != NULL)
5016 if (isymbuf != NULL)
5022 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5026 /* Restore the symbol table. */
5027 old_ent = (char *) old_tab + tabsize;
5028 memset (elf_sym_hashes (abfd), 0,
5029 extsymcount * sizeof (struct elf_link_hash_entry *));
5030 htab->root.table.table = old_table;
5031 htab->root.table.size = old_size;
5032 htab->root.table.count = old_count;
5033 memcpy (htab->root.table.table, old_tab, tabsize);
5034 htab->root.undefs = old_undefs;
5035 htab->root.undefs_tail = old_undefs_tail;
5036 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5039 for (i = 0; i < htab->root.table.size; i++)
5041 struct bfd_hash_entry *p;
5042 struct elf_link_hash_entry *h;
5044 unsigned int alignment_power;
5045 unsigned int non_ir_ref_dynamic;
5047 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5049 h = (struct elf_link_hash_entry *) p;
5050 if (h->root.type == bfd_link_hash_warning)
5051 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5053 /* Preserve the maximum alignment and size for common
5054 symbols even if this dynamic lib isn't on DT_NEEDED
5055 since it can still be loaded at run time by another
5057 if (h->root.type == bfd_link_hash_common)
5059 size = h->root.u.c.size;
5060 alignment_power = h->root.u.c.p->alignment_power;
5065 alignment_power = 0;
5067 /* Preserve non_ir_ref_dynamic so that this symbol
5068 will be exported when the dynamic lib becomes needed
5069 in the second pass. */
5070 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5071 memcpy (p, old_ent, htab->root.table.entsize);
5072 old_ent = (char *) old_ent + htab->root.table.entsize;
5073 h = (struct elf_link_hash_entry *) p;
5074 if (h->root.type == bfd_link_hash_warning)
5076 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5077 old_ent = (char *) old_ent + htab->root.table.entsize;
5078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5080 if (h->root.type == bfd_link_hash_common)
5082 if (size > h->root.u.c.size)
5083 h->root.u.c.size = size;
5084 if (alignment_power > h->root.u.c.p->alignment_power)
5085 h->root.u.c.p->alignment_power = alignment_power;
5087 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5091 /* Make a special call to the linker "notice" function to
5092 tell it that symbols added for crefs may need to be removed. */
5093 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5094 goto error_free_vers;
5097 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5099 if (nondeflt_vers != NULL)
5100 free (nondeflt_vers);
5104 if (old_tab != NULL)
5106 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5107 goto error_free_vers;
5112 /* Now that all the symbols from this input file are created, if
5113 not performing a relocatable link, handle .symver foo, foo@BAR
5114 such that any relocs against foo become foo@BAR. */
5115 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5119 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5121 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5122 char *shortname, *p;
5124 p = strchr (h->root.root.string, ELF_VER_CHR);
5126 || (h->root.type != bfd_link_hash_defined
5127 && h->root.type != bfd_link_hash_defweak))
5130 amt = p - h->root.root.string;
5131 shortname = (char *) bfd_malloc (amt + 1);
5133 goto error_free_vers;
5134 memcpy (shortname, h->root.root.string, amt);
5135 shortname[amt] = '\0';
5137 hi = (struct elf_link_hash_entry *)
5138 bfd_link_hash_lookup (&htab->root, shortname,
5139 FALSE, FALSE, FALSE);
5141 && hi->root.type == h->root.type
5142 && hi->root.u.def.value == h->root.u.def.value
5143 && hi->root.u.def.section == h->root.u.def.section)
5145 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5146 hi->root.type = bfd_link_hash_indirect;
5147 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5148 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5149 sym_hash = elf_sym_hashes (abfd);
5151 for (symidx = 0; symidx < extsymcount; ++symidx)
5152 if (sym_hash[symidx] == hi)
5154 sym_hash[symidx] = h;
5160 free (nondeflt_vers);
5161 nondeflt_vers = NULL;
5164 /* Now set the alias field correctly for all the weak defined
5165 symbols we found. The only way to do this is to search all the
5166 symbols. Since we only need the information for non functions in
5167 dynamic objects, that's the only time we actually put anything on
5168 the list WEAKS. We need this information so that if a regular
5169 object refers to a symbol defined weakly in a dynamic object, the
5170 real symbol in the dynamic object is also put in the dynamic
5171 symbols; we also must arrange for both symbols to point to the
5172 same memory location. We could handle the general case of symbol
5173 aliasing, but a general symbol alias can only be generated in
5174 assembler code, handling it correctly would be very time
5175 consuming, and other ELF linkers don't handle general aliasing
5179 struct elf_link_hash_entry **hpp;
5180 struct elf_link_hash_entry **hppend;
5181 struct elf_link_hash_entry **sorted_sym_hash;
5182 struct elf_link_hash_entry *h;
5185 /* Since we have to search the whole symbol list for each weak
5186 defined symbol, search time for N weak defined symbols will be
5187 O(N^2). Binary search will cut it down to O(NlogN). */
5189 amt *= sizeof (struct elf_link_hash_entry *);
5190 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5191 if (sorted_sym_hash == NULL)
5193 sym_hash = sorted_sym_hash;
5194 hpp = elf_sym_hashes (abfd);
5195 hppend = hpp + extsymcount;
5197 for (; hpp < hppend; hpp++)
5201 && h->root.type == bfd_link_hash_defined
5202 && !bed->is_function_type (h->type))
5210 qsort (sorted_sym_hash, sym_count,
5211 sizeof (struct elf_link_hash_entry *),
5214 while (weaks != NULL)
5216 struct elf_link_hash_entry *hlook;
5219 size_t i, j, idx = 0;
5222 weaks = hlook->u.alias;
5223 hlook->u.alias = NULL;
5225 if (hlook->root.type != bfd_link_hash_defined
5226 && hlook->root.type != bfd_link_hash_defweak)
5229 slook = hlook->root.u.def.section;
5230 vlook = hlook->root.u.def.value;
5236 bfd_signed_vma vdiff;
5238 h = sorted_sym_hash[idx];
5239 vdiff = vlook - h->root.u.def.value;
5246 int sdiff = slook->id - h->root.u.def.section->id;
5256 /* We didn't find a value/section match. */
5260 /* With multiple aliases, or when the weak symbol is already
5261 strongly defined, we have multiple matching symbols and
5262 the binary search above may land on any of them. Step
5263 one past the matching symbol(s). */
5266 h = sorted_sym_hash[idx];
5267 if (h->root.u.def.section != slook
5268 || h->root.u.def.value != vlook)
5272 /* Now look back over the aliases. Since we sorted by size
5273 as well as value and section, we'll choose the one with
5274 the largest size. */
5277 h = sorted_sym_hash[idx];
5279 /* Stop if value or section doesn't match. */
5280 if (h->root.u.def.section != slook
5281 || h->root.u.def.value != vlook)
5283 else if (h != hlook)
5285 struct elf_link_hash_entry *t;
5288 hlook->is_weakalias = 1;
5290 if (t->u.alias != NULL)
5291 while (t->u.alias != h)
5295 /* If the weak definition is in the list of dynamic
5296 symbols, make sure the real definition is put
5298 if (hlook->dynindx != -1 && h->dynindx == -1)
5300 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5303 free (sorted_sym_hash);
5308 /* If the real definition is in the list of dynamic
5309 symbols, make sure the weak definition is put
5310 there as well. If we don't do this, then the
5311 dynamic loader might not merge the entries for the
5312 real definition and the weak definition. */
5313 if (h->dynindx != -1 && hlook->dynindx == -1)
5315 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5316 goto err_free_sym_hash;
5323 free (sorted_sym_hash);
5326 if (bed->check_directives
5327 && !(*bed->check_directives) (abfd, info))
5330 /* If this is a non-traditional link, try to optimize the handling
5331 of the .stab/.stabstr sections. */
5333 && ! info->traditional_format
5334 && is_elf_hash_table (htab)
5335 && (info->strip != strip_all && info->strip != strip_debugger))
5339 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5340 if (stabstr != NULL)
5342 bfd_size_type string_offset = 0;
5345 for (stab = abfd->sections; stab; stab = stab->next)
5346 if (CONST_STRNEQ (stab->name, ".stab")
5347 && (!stab->name[5] ||
5348 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5349 && (stab->flags & SEC_MERGE) == 0
5350 && !bfd_is_abs_section (stab->output_section))
5352 struct bfd_elf_section_data *secdata;
5354 secdata = elf_section_data (stab);
5355 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5356 stabstr, &secdata->sec_info,
5359 if (secdata->sec_info)
5360 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5365 if (is_elf_hash_table (htab) && add_needed)
5367 /* Add this bfd to the loaded list. */
5368 struct elf_link_loaded_list *n;
5370 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5374 n->next = htab->loaded;
5381 if (old_tab != NULL)
5383 if (old_strtab != NULL)
5385 if (nondeflt_vers != NULL)
5386 free (nondeflt_vers);
5387 if (extversym != NULL)
5390 if (isymbuf != NULL)
5396 /* Return the linker hash table entry of a symbol that might be
5397 satisfied by an archive symbol. Return -1 on error. */
5399 struct elf_link_hash_entry *
5400 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5401 struct bfd_link_info *info,
5404 struct elf_link_hash_entry *h;
5408 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5412 /* If this is a default version (the name contains @@), look up the
5413 symbol again with only one `@' as well as without the version.
5414 The effect is that references to the symbol with and without the
5415 version will be matched by the default symbol in the archive. */
5417 p = strchr (name, ELF_VER_CHR);
5418 if (p == NULL || p[1] != ELF_VER_CHR)
5421 /* First check with only one `@'. */
5422 len = strlen (name);
5423 copy = (char *) bfd_alloc (abfd, len);
5425 return (struct elf_link_hash_entry *) -1;
5427 first = p - name + 1;
5428 memcpy (copy, name, first);
5429 memcpy (copy + first, name + first + 1, len - first);
5431 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5434 /* We also need to check references to the symbol without the
5436 copy[first - 1] = '\0';
5437 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5438 FALSE, FALSE, TRUE);
5441 bfd_release (abfd, copy);
5445 /* Add symbols from an ELF archive file to the linker hash table. We
5446 don't use _bfd_generic_link_add_archive_symbols because we need to
5447 handle versioned symbols.
5449 Fortunately, ELF archive handling is simpler than that done by
5450 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5451 oddities. In ELF, if we find a symbol in the archive map, and the
5452 symbol is currently undefined, we know that we must pull in that
5455 Unfortunately, we do have to make multiple passes over the symbol
5456 table until nothing further is resolved. */
5459 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5462 unsigned char *included = NULL;
5466 const struct elf_backend_data *bed;
5467 struct elf_link_hash_entry * (*archive_symbol_lookup)
5468 (bfd *, struct bfd_link_info *, const char *);
5470 if (! bfd_has_map (abfd))
5472 /* An empty archive is a special case. */
5473 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5475 bfd_set_error (bfd_error_no_armap);
5479 /* Keep track of all symbols we know to be already defined, and all
5480 files we know to be already included. This is to speed up the
5481 second and subsequent passes. */
5482 c = bfd_ardata (abfd)->symdef_count;
5486 amt *= sizeof (*included);
5487 included = (unsigned char *) bfd_zmalloc (amt);
5488 if (included == NULL)
5491 symdefs = bfd_ardata (abfd)->symdefs;
5492 bed = get_elf_backend_data (abfd);
5493 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5506 symdefend = symdef + c;
5507 for (i = 0; symdef < symdefend; symdef++, i++)
5509 struct elf_link_hash_entry *h;
5511 struct bfd_link_hash_entry *undefs_tail;
5516 if (symdef->file_offset == last)
5522 h = archive_symbol_lookup (abfd, info, symdef->name);
5523 if (h == (struct elf_link_hash_entry *) -1)
5529 if (h->root.type == bfd_link_hash_common)
5531 /* We currently have a common symbol. The archive map contains
5532 a reference to this symbol, so we may want to include it. We
5533 only want to include it however, if this archive element
5534 contains a definition of the symbol, not just another common
5537 Unfortunately some archivers (including GNU ar) will put
5538 declarations of common symbols into their archive maps, as
5539 well as real definitions, so we cannot just go by the archive
5540 map alone. Instead we must read in the element's symbol
5541 table and check that to see what kind of symbol definition
5543 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5546 else if (h->root.type != bfd_link_hash_undefined)
5548 if (h->root.type != bfd_link_hash_undefweak)
5549 /* Symbol must be defined. Don't check it again. */
5554 /* We need to include this archive member. */
5555 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5556 if (element == NULL)
5559 if (! bfd_check_format (element, bfd_object))
5562 undefs_tail = info->hash->undefs_tail;
5564 if (!(*info->callbacks
5565 ->add_archive_element) (info, element, symdef->name, &element))
5567 if (!bfd_link_add_symbols (element, info))
5570 /* If there are any new undefined symbols, we need to make
5571 another pass through the archive in order to see whether
5572 they can be defined. FIXME: This isn't perfect, because
5573 common symbols wind up on undefs_tail and because an
5574 undefined symbol which is defined later on in this pass
5575 does not require another pass. This isn't a bug, but it
5576 does make the code less efficient than it could be. */
5577 if (undefs_tail != info->hash->undefs_tail)
5580 /* Look backward to mark all symbols from this object file
5581 which we have already seen in this pass. */
5585 included[mark] = TRUE;
5590 while (symdefs[mark].file_offset == symdef->file_offset);
5592 /* We mark subsequent symbols from this object file as we go
5593 on through the loop. */
5594 last = symdef->file_offset;
5604 if (included != NULL)
5609 /* Given an ELF BFD, add symbols to the global hash table as
5613 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5615 switch (bfd_get_format (abfd))
5618 return elf_link_add_object_symbols (abfd, info);
5620 return elf_link_add_archive_symbols (abfd, info);
5622 bfd_set_error (bfd_error_wrong_format);
5627 struct hash_codes_info
5629 unsigned long *hashcodes;
5633 /* This function will be called though elf_link_hash_traverse to store
5634 all hash value of the exported symbols in an array. */
5637 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5639 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5644 /* Ignore indirect symbols. These are added by the versioning code. */
5645 if (h->dynindx == -1)
5648 name = h->root.root.string;
5649 if (h->versioned >= versioned)
5651 char *p = strchr (name, ELF_VER_CHR);
5654 alc = (char *) bfd_malloc (p - name + 1);
5660 memcpy (alc, name, p - name);
5661 alc[p - name] = '\0';
5666 /* Compute the hash value. */
5667 ha = bfd_elf_hash (name);
5669 /* Store the found hash value in the array given as the argument. */
5670 *(inf->hashcodes)++ = ha;
5672 /* And store it in the struct so that we can put it in the hash table
5674 h->u.elf_hash_value = ha;
5682 struct collect_gnu_hash_codes
5685 const struct elf_backend_data *bed;
5686 unsigned long int nsyms;
5687 unsigned long int maskbits;
5688 unsigned long int *hashcodes;
5689 unsigned long int *hashval;
5690 unsigned long int *indx;
5691 unsigned long int *counts;
5694 long int min_dynindx;
5695 unsigned long int bucketcount;
5696 unsigned long int symindx;
5697 long int local_indx;
5698 long int shift1, shift2;
5699 unsigned long int mask;
5703 /* This function will be called though elf_link_hash_traverse to store
5704 all hash value of the exported symbols in an array. */
5707 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5709 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5714 /* Ignore indirect symbols. These are added by the versioning code. */
5715 if (h->dynindx == -1)
5718 /* Ignore also local symbols and undefined symbols. */
5719 if (! (*s->bed->elf_hash_symbol) (h))
5722 name = h->root.root.string;
5723 if (h->versioned >= versioned)
5725 char *p = strchr (name, ELF_VER_CHR);
5728 alc = (char *) bfd_malloc (p - name + 1);
5734 memcpy (alc, name, p - name);
5735 alc[p - name] = '\0';
5740 /* Compute the hash value. */
5741 ha = bfd_elf_gnu_hash (name);
5743 /* Store the found hash value in the array for compute_bucket_count,
5744 and also for .dynsym reordering purposes. */
5745 s->hashcodes[s->nsyms] = ha;
5746 s->hashval[h->dynindx] = ha;
5748 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5749 s->min_dynindx = h->dynindx;
5757 /* This function will be called though elf_link_hash_traverse to do
5758 final dynaminc symbol renumbering. */
5761 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5763 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5764 unsigned long int bucket;
5765 unsigned long int val;
5767 /* Ignore indirect symbols. */
5768 if (h->dynindx == -1)
5771 /* Ignore also local symbols and undefined symbols. */
5772 if (! (*s->bed->elf_hash_symbol) (h))
5774 if (h->dynindx >= s->min_dynindx)
5775 h->dynindx = s->local_indx++;
5779 bucket = s->hashval[h->dynindx] % s->bucketcount;
5780 val = (s->hashval[h->dynindx] >> s->shift1)
5781 & ((s->maskbits >> s->shift1) - 1);
5782 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5784 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5785 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5786 if (s->counts[bucket] == 1)
5787 /* Last element terminates the chain. */
5789 bfd_put_32 (s->output_bfd, val,
5790 s->contents + (s->indx[bucket] - s->symindx) * 4);
5791 --s->counts[bucket];
5792 h->dynindx = s->indx[bucket]++;
5796 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5799 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5801 return !(h->forced_local
5802 || h->root.type == bfd_link_hash_undefined
5803 || h->root.type == bfd_link_hash_undefweak
5804 || ((h->root.type == bfd_link_hash_defined
5805 || h->root.type == bfd_link_hash_defweak)
5806 && h->root.u.def.section->output_section == NULL));
5809 /* Array used to determine the number of hash table buckets to use
5810 based on the number of symbols there are. If there are fewer than
5811 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5812 fewer than 37 we use 17 buckets, and so forth. We never use more
5813 than 32771 buckets. */
5815 static const size_t elf_buckets[] =
5817 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5821 /* Compute bucket count for hashing table. We do not use a static set
5822 of possible tables sizes anymore. Instead we determine for all
5823 possible reasonable sizes of the table the outcome (i.e., the
5824 number of collisions etc) and choose the best solution. The
5825 weighting functions are not too simple to allow the table to grow
5826 without bounds. Instead one of the weighting factors is the size.
5827 Therefore the result is always a good payoff between few collisions
5828 (= short chain lengths) and table size. */
5830 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5831 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5832 unsigned long int nsyms,
5835 size_t best_size = 0;
5836 unsigned long int i;
5838 /* We have a problem here. The following code to optimize the table
5839 size requires an integer type with more the 32 bits. If
5840 BFD_HOST_U_64_BIT is set we know about such a type. */
5841 #ifdef BFD_HOST_U_64_BIT
5846 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5847 bfd *dynobj = elf_hash_table (info)->dynobj;
5848 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5849 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5850 unsigned long int *counts;
5852 unsigned int no_improvement_count = 0;
5854 /* Possible optimization parameters: if we have NSYMS symbols we say
5855 that the hashing table must at least have NSYMS/4 and at most
5857 minsize = nsyms / 4;
5860 best_size = maxsize = nsyms * 2;
5865 if ((best_size & 31) == 0)
5869 /* Create array where we count the collisions in. We must use bfd_malloc
5870 since the size could be large. */
5872 amt *= sizeof (unsigned long int);
5873 counts = (unsigned long int *) bfd_malloc (amt);
5877 /* Compute the "optimal" size for the hash table. The criteria is a
5878 minimal chain length. The minor criteria is (of course) the size
5880 for (i = minsize; i < maxsize; ++i)
5882 /* Walk through the array of hashcodes and count the collisions. */
5883 BFD_HOST_U_64_BIT max;
5884 unsigned long int j;
5885 unsigned long int fact;
5887 if (gnu_hash && (i & 31) == 0)
5890 memset (counts, '\0', i * sizeof (unsigned long int));
5892 /* Determine how often each hash bucket is used. */
5893 for (j = 0; j < nsyms; ++j)
5894 ++counts[hashcodes[j] % i];
5896 /* For the weight function we need some information about the
5897 pagesize on the target. This is information need not be 100%
5898 accurate. Since this information is not available (so far) we
5899 define it here to a reasonable default value. If it is crucial
5900 to have a better value some day simply define this value. */
5901 # ifndef BFD_TARGET_PAGESIZE
5902 # define BFD_TARGET_PAGESIZE (4096)
5905 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5907 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5910 /* Variant 1: optimize for short chains. We add the squares
5911 of all the chain lengths (which favors many small chain
5912 over a few long chains). */
5913 for (j = 0; j < i; ++j)
5914 max += counts[j] * counts[j];
5916 /* This adds penalties for the overall size of the table. */
5917 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5920 /* Variant 2: Optimize a lot more for small table. Here we
5921 also add squares of the size but we also add penalties for
5922 empty slots (the +1 term). */
5923 for (j = 0; j < i; ++j)
5924 max += (1 + counts[j]) * (1 + counts[j]);
5926 /* The overall size of the table is considered, but not as
5927 strong as in variant 1, where it is squared. */
5928 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5932 /* Compare with current best results. */
5933 if (max < best_chlen)
5937 no_improvement_count = 0;
5939 /* PR 11843: Avoid futile long searches for the best bucket size
5940 when there are a large number of symbols. */
5941 else if (++no_improvement_count == 100)
5948 #endif /* defined (BFD_HOST_U_64_BIT) */
5950 /* This is the fallback solution if no 64bit type is available or if we
5951 are not supposed to spend much time on optimizations. We select the
5952 bucket count using a fixed set of numbers. */
5953 for (i = 0; elf_buckets[i] != 0; i++)
5955 best_size = elf_buckets[i];
5956 if (nsyms < elf_buckets[i + 1])
5959 if (gnu_hash && best_size < 2)
5966 /* Size any SHT_GROUP section for ld -r. */
5969 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5974 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5975 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5976 && (s = ibfd->sections) != NULL
5977 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5978 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5983 /* Set a default stack segment size. The value in INFO wins. If it
5984 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5985 undefined it is initialized. */
5988 bfd_elf_stack_segment_size (bfd *output_bfd,
5989 struct bfd_link_info *info,
5990 const char *legacy_symbol,
5991 bfd_vma default_size)
5993 struct elf_link_hash_entry *h = NULL;
5995 /* Look for legacy symbol. */
5997 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5998 FALSE, FALSE, FALSE);
5999 if (h && (h->root.type == bfd_link_hash_defined
6000 || h->root.type == bfd_link_hash_defweak)
6002 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6004 /* The symbol has no type if specified on the command line. */
6005 h->type = STT_OBJECT;
6006 if (info->stacksize)
6007 /* xgettext:c-format */
6008 _bfd_error_handler (_("%B: stack size specified and %s set"),
6009 output_bfd, legacy_symbol);
6010 else if (h->root.u.def.section != bfd_abs_section_ptr)
6011 /* xgettext:c-format */
6012 _bfd_error_handler (_("%B: %s not absolute"),
6013 output_bfd, legacy_symbol);
6015 info->stacksize = h->root.u.def.value;
6018 if (!info->stacksize)
6019 /* If the user didn't set a size, or explicitly inhibit the
6020 size, set it now. */
6021 info->stacksize = default_size;
6023 /* Provide the legacy symbol, if it is referenced. */
6024 if (h && (h->root.type == bfd_link_hash_undefined
6025 || h->root.type == bfd_link_hash_undefweak))
6027 struct bfd_link_hash_entry *bh = NULL;
6029 if (!(_bfd_generic_link_add_one_symbol
6030 (info, output_bfd, legacy_symbol,
6031 BSF_GLOBAL, bfd_abs_section_ptr,
6032 info->stacksize >= 0 ? info->stacksize : 0,
6033 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6036 h = (struct elf_link_hash_entry *) bh;
6038 h->type = STT_OBJECT;
6044 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6046 struct elf_gc_sweep_symbol_info
6048 struct bfd_link_info *info;
6049 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6054 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6057 && (((h->root.type == bfd_link_hash_defined
6058 || h->root.type == bfd_link_hash_defweak)
6059 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6060 && h->root.u.def.section->gc_mark))
6061 || h->root.type == bfd_link_hash_undefined
6062 || h->root.type == bfd_link_hash_undefweak))
6064 struct elf_gc_sweep_symbol_info *inf;
6066 inf = (struct elf_gc_sweep_symbol_info *) data;
6067 (*inf->hide_symbol) (inf->info, h, TRUE);
6070 h->ref_regular_nonweak = 0;
6076 /* Set up the sizes and contents of the ELF dynamic sections. This is
6077 called by the ELF linker emulation before_allocation routine. We
6078 must set the sizes of the sections before the linker sets the
6079 addresses of the various sections. */
6082 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6085 const char *filter_shlib,
6087 const char *depaudit,
6088 const char * const *auxiliary_filters,
6089 struct bfd_link_info *info,
6090 asection **sinterpptr)
6093 const struct elf_backend_data *bed;
6097 if (!is_elf_hash_table (info->hash))
6100 dynobj = elf_hash_table (info)->dynobj;
6102 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6104 struct bfd_elf_version_tree *verdefs;
6105 struct elf_info_failed asvinfo;
6106 struct bfd_elf_version_tree *t;
6107 struct bfd_elf_version_expr *d;
6111 /* If we are supposed to export all symbols into the dynamic symbol
6112 table (this is not the normal case), then do so. */
6113 if (info->export_dynamic
6114 || (bfd_link_executable (info) && info->dynamic))
6116 struct elf_info_failed eif;
6120 elf_link_hash_traverse (elf_hash_table (info),
6121 _bfd_elf_export_symbol,
6129 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6131 if (soname_indx == (size_t) -1
6132 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6136 soname_indx = (size_t) -1;
6138 /* Make all global versions with definition. */
6139 for (t = info->version_info; t != NULL; t = t->next)
6140 for (d = t->globals.list; d != NULL; d = d->next)
6141 if (!d->symver && d->literal)
6143 const char *verstr, *name;
6144 size_t namelen, verlen, newlen;
6145 char *newname, *p, leading_char;
6146 struct elf_link_hash_entry *newh;
6148 leading_char = bfd_get_symbol_leading_char (output_bfd);
6150 namelen = strlen (name) + (leading_char != '\0');
6152 verlen = strlen (verstr);
6153 newlen = namelen + verlen + 3;
6155 newname = (char *) bfd_malloc (newlen);
6156 if (newname == NULL)
6158 newname[0] = leading_char;
6159 memcpy (newname + (leading_char != '\0'), name, namelen);
6161 /* Check the hidden versioned definition. */
6162 p = newname + namelen;
6164 memcpy (p, verstr, verlen + 1);
6165 newh = elf_link_hash_lookup (elf_hash_table (info),
6166 newname, FALSE, FALSE,
6169 || (newh->root.type != bfd_link_hash_defined
6170 && newh->root.type != bfd_link_hash_defweak))
6172 /* Check the default versioned definition. */
6174 memcpy (p, verstr, verlen + 1);
6175 newh = elf_link_hash_lookup (elf_hash_table (info),
6176 newname, FALSE, FALSE,
6181 /* Mark this version if there is a definition and it is
6182 not defined in a shared object. */
6184 && !newh->def_dynamic
6185 && (newh->root.type == bfd_link_hash_defined
6186 || newh->root.type == bfd_link_hash_defweak))
6190 /* Attach all the symbols to their version information. */
6191 asvinfo.info = info;
6192 asvinfo.failed = FALSE;
6194 elf_link_hash_traverse (elf_hash_table (info),
6195 _bfd_elf_link_assign_sym_version,
6200 if (!info->allow_undefined_version)
6202 /* Check if all global versions have a definition. */
6203 bfd_boolean all_defined = TRUE;
6204 for (t = info->version_info; t != NULL; t = t->next)
6205 for (d = t->globals.list; d != NULL; d = d->next)
6206 if (d->literal && !d->symver && !d->script)
6209 (_("%s: undefined version: %s"),
6210 d->pattern, t->name);
6211 all_defined = FALSE;
6216 bfd_set_error (bfd_error_bad_value);
6221 /* Set up the version definition section. */
6222 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6223 BFD_ASSERT (s != NULL);
6225 /* We may have created additional version definitions if we are
6226 just linking a regular application. */
6227 verdefs = info->version_info;
6229 /* Skip anonymous version tag. */
6230 if (verdefs != NULL && verdefs->vernum == 0)
6231 verdefs = verdefs->next;
6233 if (verdefs == NULL && !info->create_default_symver)
6234 s->flags |= SEC_EXCLUDE;
6240 Elf_Internal_Verdef def;
6241 Elf_Internal_Verdaux defaux;
6242 struct bfd_link_hash_entry *bh;
6243 struct elf_link_hash_entry *h;
6249 /* Make space for the base version. */
6250 size += sizeof (Elf_External_Verdef);
6251 size += sizeof (Elf_External_Verdaux);
6254 /* Make space for the default version. */
6255 if (info->create_default_symver)
6257 size += sizeof (Elf_External_Verdef);
6261 for (t = verdefs; t != NULL; t = t->next)
6263 struct bfd_elf_version_deps *n;
6265 /* Don't emit base version twice. */
6269 size += sizeof (Elf_External_Verdef);
6270 size += sizeof (Elf_External_Verdaux);
6273 for (n = t->deps; n != NULL; n = n->next)
6274 size += sizeof (Elf_External_Verdaux);
6278 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6279 if (s->contents == NULL && s->size != 0)
6282 /* Fill in the version definition section. */
6286 def.vd_version = VER_DEF_CURRENT;
6287 def.vd_flags = VER_FLG_BASE;
6290 if (info->create_default_symver)
6292 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6293 def.vd_next = sizeof (Elf_External_Verdef);
6297 def.vd_aux = sizeof (Elf_External_Verdef);
6298 def.vd_next = (sizeof (Elf_External_Verdef)
6299 + sizeof (Elf_External_Verdaux));
6302 if (soname_indx != (size_t) -1)
6304 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6306 def.vd_hash = bfd_elf_hash (soname);
6307 defaux.vda_name = soname_indx;
6314 name = lbasename (output_bfd->filename);
6315 def.vd_hash = bfd_elf_hash (name);
6316 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6318 if (indx == (size_t) -1)
6320 defaux.vda_name = indx;
6322 defaux.vda_next = 0;
6324 _bfd_elf_swap_verdef_out (output_bfd, &def,
6325 (Elf_External_Verdef *) p);
6326 p += sizeof (Elf_External_Verdef);
6327 if (info->create_default_symver)
6329 /* Add a symbol representing this version. */
6331 if (! (_bfd_generic_link_add_one_symbol
6332 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6334 get_elf_backend_data (dynobj)->collect, &bh)))
6336 h = (struct elf_link_hash_entry *) bh;
6339 h->type = STT_OBJECT;
6340 h->verinfo.vertree = NULL;
6342 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6345 /* Create a duplicate of the base version with the same
6346 aux block, but different flags. */
6349 def.vd_aux = sizeof (Elf_External_Verdef);
6351 def.vd_next = (sizeof (Elf_External_Verdef)
6352 + sizeof (Elf_External_Verdaux));
6355 _bfd_elf_swap_verdef_out (output_bfd, &def,
6356 (Elf_External_Verdef *) p);
6357 p += sizeof (Elf_External_Verdef);
6359 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6360 (Elf_External_Verdaux *) p);
6361 p += sizeof (Elf_External_Verdaux);
6363 for (t = verdefs; t != NULL; t = t->next)
6366 struct bfd_elf_version_deps *n;
6368 /* Don't emit the base version twice. */
6373 for (n = t->deps; n != NULL; n = n->next)
6376 /* Add a symbol representing this version. */
6378 if (! (_bfd_generic_link_add_one_symbol
6379 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6381 get_elf_backend_data (dynobj)->collect, &bh)))
6383 h = (struct elf_link_hash_entry *) bh;
6386 h->type = STT_OBJECT;
6387 h->verinfo.vertree = t;
6389 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6392 def.vd_version = VER_DEF_CURRENT;
6394 if (t->globals.list == NULL
6395 && t->locals.list == NULL
6397 def.vd_flags |= VER_FLG_WEAK;
6398 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6399 def.vd_cnt = cdeps + 1;
6400 def.vd_hash = bfd_elf_hash (t->name);
6401 def.vd_aux = sizeof (Elf_External_Verdef);
6404 /* If a basever node is next, it *must* be the last node in
6405 the chain, otherwise Verdef construction breaks. */
6406 if (t->next != NULL && t->next->vernum == 0)
6407 BFD_ASSERT (t->next->next == NULL);
6409 if (t->next != NULL && t->next->vernum != 0)
6410 def.vd_next = (sizeof (Elf_External_Verdef)
6411 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6413 _bfd_elf_swap_verdef_out (output_bfd, &def,
6414 (Elf_External_Verdef *) p);
6415 p += sizeof (Elf_External_Verdef);
6417 defaux.vda_name = h->dynstr_index;
6418 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6420 defaux.vda_next = 0;
6421 if (t->deps != NULL)
6422 defaux.vda_next = sizeof (Elf_External_Verdaux);
6423 t->name_indx = defaux.vda_name;
6425 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6426 (Elf_External_Verdaux *) p);
6427 p += sizeof (Elf_External_Verdaux);
6429 for (n = t->deps; n != NULL; n = n->next)
6431 if (n->version_needed == NULL)
6433 /* This can happen if there was an error in the
6435 defaux.vda_name = 0;
6439 defaux.vda_name = n->version_needed->name_indx;
6440 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6443 if (n->next == NULL)
6444 defaux.vda_next = 0;
6446 defaux.vda_next = sizeof (Elf_External_Verdaux);
6448 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6449 (Elf_External_Verdaux *) p);
6450 p += sizeof (Elf_External_Verdaux);
6454 elf_tdata (output_bfd)->cverdefs = cdefs;
6458 bed = get_elf_backend_data (output_bfd);
6460 if (info->gc_sections && bed->can_gc_sections)
6462 struct elf_gc_sweep_symbol_info sweep_info;
6464 /* Remove the symbols that were in the swept sections from the
6465 dynamic symbol table. */
6466 sweep_info.info = info;
6467 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6468 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6472 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6475 struct elf_find_verdep_info sinfo;
6477 /* Work out the size of the version reference section. */
6479 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6480 BFD_ASSERT (s != NULL);
6483 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6484 if (sinfo.vers == 0)
6486 sinfo.failed = FALSE;
6488 elf_link_hash_traverse (elf_hash_table (info),
6489 _bfd_elf_link_find_version_dependencies,
6494 if (elf_tdata (output_bfd)->verref == NULL)
6495 s->flags |= SEC_EXCLUDE;
6498 Elf_Internal_Verneed *vn;
6503 /* Build the version dependency section. */
6506 for (vn = elf_tdata (output_bfd)->verref;
6508 vn = vn->vn_nextref)
6510 Elf_Internal_Vernaux *a;
6512 size += sizeof (Elf_External_Verneed);
6514 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6515 size += sizeof (Elf_External_Vernaux);
6519 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6520 if (s->contents == NULL)
6524 for (vn = elf_tdata (output_bfd)->verref;
6526 vn = vn->vn_nextref)
6529 Elf_Internal_Vernaux *a;
6533 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6536 vn->vn_version = VER_NEED_CURRENT;
6538 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6539 elf_dt_name (vn->vn_bfd) != NULL
6540 ? elf_dt_name (vn->vn_bfd)
6541 : lbasename (vn->vn_bfd->filename),
6543 if (indx == (size_t) -1)
6546 vn->vn_aux = sizeof (Elf_External_Verneed);
6547 if (vn->vn_nextref == NULL)
6550 vn->vn_next = (sizeof (Elf_External_Verneed)
6551 + caux * sizeof (Elf_External_Vernaux));
6553 _bfd_elf_swap_verneed_out (output_bfd, vn,
6554 (Elf_External_Verneed *) p);
6555 p += sizeof (Elf_External_Verneed);
6557 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6559 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6560 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6561 a->vna_nodename, FALSE);
6562 if (indx == (size_t) -1)
6565 if (a->vna_nextptr == NULL)
6568 a->vna_next = sizeof (Elf_External_Vernaux);
6570 _bfd_elf_swap_vernaux_out (output_bfd, a,
6571 (Elf_External_Vernaux *) p);
6572 p += sizeof (Elf_External_Vernaux);
6576 elf_tdata (output_bfd)->cverrefs = crefs;
6580 /* Any syms created from now on start with -1 in
6581 got.refcount/offset and plt.refcount/offset. */
6582 elf_hash_table (info)->init_got_refcount
6583 = elf_hash_table (info)->init_got_offset;
6584 elf_hash_table (info)->init_plt_refcount
6585 = elf_hash_table (info)->init_plt_offset;
6587 if (bfd_link_relocatable (info)
6588 && !_bfd_elf_size_group_sections (info))
6591 /* The backend may have to create some sections regardless of whether
6592 we're dynamic or not. */
6593 if (bed->elf_backend_always_size_sections
6594 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6597 /* Determine any GNU_STACK segment requirements, after the backend
6598 has had a chance to set a default segment size. */
6599 if (info->execstack)
6600 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6601 else if (info->noexecstack)
6602 elf_stack_flags (output_bfd) = PF_R | PF_W;
6606 asection *notesec = NULL;
6609 for (inputobj = info->input_bfds;
6611 inputobj = inputobj->link.next)
6616 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6618 s = inputobj->sections;
6619 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6622 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6625 if (s->flags & SEC_CODE)
6629 else if (bed->default_execstack)
6632 if (notesec || info->stacksize > 0)
6633 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6634 if (notesec && exec && bfd_link_relocatable (info)
6635 && notesec->output_section != bfd_abs_section_ptr)
6636 notesec->output_section->flags |= SEC_CODE;
6639 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6641 struct elf_info_failed eif;
6642 struct elf_link_hash_entry *h;
6646 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6647 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6651 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6653 info->flags |= DF_SYMBOLIC;
6661 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6663 if (indx == (size_t) -1)
6666 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6667 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6671 if (filter_shlib != NULL)
6675 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6676 filter_shlib, TRUE);
6677 if (indx == (size_t) -1
6678 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6682 if (auxiliary_filters != NULL)
6684 const char * const *p;
6686 for (p = auxiliary_filters; *p != NULL; p++)
6690 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6692 if (indx == (size_t) -1
6693 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6702 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6704 if (indx == (size_t) -1
6705 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6709 if (depaudit != NULL)
6713 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6715 if (indx == (size_t) -1
6716 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6723 /* Find all symbols which were defined in a dynamic object and make
6724 the backend pick a reasonable value for them. */
6725 elf_link_hash_traverse (elf_hash_table (info),
6726 _bfd_elf_adjust_dynamic_symbol,
6731 /* Add some entries to the .dynamic section. We fill in some of the
6732 values later, in bfd_elf_final_link, but we must add the entries
6733 now so that we know the final size of the .dynamic section. */
6735 /* If there are initialization and/or finalization functions to
6736 call then add the corresponding DT_INIT/DT_FINI entries. */
6737 h = (info->init_function
6738 ? elf_link_hash_lookup (elf_hash_table (info),
6739 info->init_function, FALSE,
6746 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6749 h = (info->fini_function
6750 ? elf_link_hash_lookup (elf_hash_table (info),
6751 info->fini_function, FALSE,
6758 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6762 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6763 if (s != NULL && s->linker_has_input)
6765 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6766 if (! bfd_link_executable (info))
6771 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6772 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6773 && (o = sub->sections) != NULL
6774 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6775 for (o = sub->sections; o != NULL; o = o->next)
6776 if (elf_section_data (o)->this_hdr.sh_type
6777 == SHT_PREINIT_ARRAY)
6780 (_("%B: .preinit_array section is not allowed in DSO"),
6785 bfd_set_error (bfd_error_nonrepresentable_section);
6789 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6790 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6793 s = bfd_get_section_by_name (output_bfd, ".init_array");
6794 if (s != NULL && s->linker_has_input)
6796 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6797 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6800 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6801 if (s != NULL && s->linker_has_input)
6803 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6804 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6808 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6809 /* If .dynstr is excluded from the link, we don't want any of
6810 these tags. Strictly, we should be checking each section
6811 individually; This quick check covers for the case where
6812 someone does a /DISCARD/ : { *(*) }. */
6813 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6815 bfd_size_type strsize;
6817 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6818 if ((info->emit_hash
6819 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6820 || (info->emit_gnu_hash
6821 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6822 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6823 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6824 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6825 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6826 bed->s->sizeof_sym))
6831 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6834 /* The backend must work out the sizes of all the other dynamic
6837 && bed->elf_backend_size_dynamic_sections != NULL
6838 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6841 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6843 if (elf_tdata (output_bfd)->cverdefs)
6845 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6847 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6848 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6852 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6854 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6857 else if (info->flags & DF_BIND_NOW)
6859 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6865 if (bfd_link_executable (info))
6866 info->flags_1 &= ~ (DF_1_INITFIRST
6869 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6873 if (elf_tdata (output_bfd)->cverrefs)
6875 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6877 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6878 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6882 if ((elf_tdata (output_bfd)->cverrefs == 0
6883 && elf_tdata (output_bfd)->cverdefs == 0)
6884 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6888 s = bfd_get_linker_section (dynobj, ".gnu.version");
6889 s->flags |= SEC_EXCLUDE;
6895 /* Find the first non-excluded output section. We'll use its
6896 section symbol for some emitted relocs. */
6898 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6902 for (s = output_bfd->sections; s != NULL; s = s->next)
6903 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6904 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6906 elf_hash_table (info)->text_index_section = s;
6911 /* Find two non-excluded output sections, one for code, one for data.
6912 We'll use their section symbols for some emitted relocs. */
6914 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6918 /* Data first, since setting text_index_section changes
6919 _bfd_elf_link_omit_section_dynsym. */
6920 for (s = output_bfd->sections; s != NULL; s = s->next)
6921 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6922 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6924 elf_hash_table (info)->data_index_section = s;
6928 for (s = output_bfd->sections; s != NULL; s = s->next)
6929 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6930 == (SEC_ALLOC | SEC_READONLY))
6931 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6933 elf_hash_table (info)->text_index_section = s;
6937 if (elf_hash_table (info)->text_index_section == NULL)
6938 elf_hash_table (info)->text_index_section
6939 = elf_hash_table (info)->data_index_section;
6943 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6945 const struct elf_backend_data *bed;
6946 unsigned long section_sym_count;
6947 bfd_size_type dynsymcount = 0;
6949 if (!is_elf_hash_table (info->hash))
6952 bed = get_elf_backend_data (output_bfd);
6953 (*bed->elf_backend_init_index_section) (output_bfd, info);
6955 /* Assign dynsym indices. In a shared library we generate a section
6956 symbol for each output section, which come first. Next come all
6957 of the back-end allocated local dynamic syms, followed by the rest
6958 of the global symbols.
6960 This is usually not needed for static binaries, however backends
6961 can request to always do it, e.g. the MIPS backend uses dynamic
6962 symbol counts to lay out GOT, which will be produced in the
6963 presence of GOT relocations even in static binaries (holding fixed
6964 data in that case, to satisfy those relocations). */
6966 if (elf_hash_table (info)->dynamic_sections_created
6967 || bed->always_renumber_dynsyms)
6968 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6969 §ion_sym_count);
6971 if (elf_hash_table (info)->dynamic_sections_created)
6975 unsigned int dtagcount;
6977 dynobj = elf_hash_table (info)->dynobj;
6979 /* Work out the size of the symbol version section. */
6980 s = bfd_get_linker_section (dynobj, ".gnu.version");
6981 BFD_ASSERT (s != NULL);
6982 if ((s->flags & SEC_EXCLUDE) == 0)
6984 s->size = dynsymcount * sizeof (Elf_External_Versym);
6985 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6986 if (s->contents == NULL)
6989 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6993 /* Set the size of the .dynsym and .hash sections. We counted
6994 the number of dynamic symbols in elf_link_add_object_symbols.
6995 We will build the contents of .dynsym and .hash when we build
6996 the final symbol table, because until then we do not know the
6997 correct value to give the symbols. We built the .dynstr
6998 section as we went along in elf_link_add_object_symbols. */
6999 s = elf_hash_table (info)->dynsym;
7000 BFD_ASSERT (s != NULL);
7001 s->size = dynsymcount * bed->s->sizeof_sym;
7003 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7004 if (s->contents == NULL)
7007 /* The first entry in .dynsym is a dummy symbol. Clear all the
7008 section syms, in case we don't output them all. */
7009 ++section_sym_count;
7010 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7012 elf_hash_table (info)->bucketcount = 0;
7014 /* Compute the size of the hashing table. As a side effect this
7015 computes the hash values for all the names we export. */
7016 if (info->emit_hash)
7018 unsigned long int *hashcodes;
7019 struct hash_codes_info hashinf;
7021 unsigned long int nsyms;
7023 size_t hash_entry_size;
7025 /* Compute the hash values for all exported symbols. At the same
7026 time store the values in an array so that we could use them for
7028 amt = dynsymcount * sizeof (unsigned long int);
7029 hashcodes = (unsigned long int *) bfd_malloc (amt);
7030 if (hashcodes == NULL)
7032 hashinf.hashcodes = hashcodes;
7033 hashinf.error = FALSE;
7035 /* Put all hash values in HASHCODES. */
7036 elf_link_hash_traverse (elf_hash_table (info),
7037 elf_collect_hash_codes, &hashinf);
7044 nsyms = hashinf.hashcodes - hashcodes;
7046 = compute_bucket_count (info, hashcodes, nsyms, 0);
7049 if (bucketcount == 0 && nsyms > 0)
7052 elf_hash_table (info)->bucketcount = bucketcount;
7054 s = bfd_get_linker_section (dynobj, ".hash");
7055 BFD_ASSERT (s != NULL);
7056 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7057 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7058 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7059 if (s->contents == NULL)
7062 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7063 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7064 s->contents + hash_entry_size);
7067 if (info->emit_gnu_hash)
7070 unsigned char *contents;
7071 struct collect_gnu_hash_codes cinfo;
7075 memset (&cinfo, 0, sizeof (cinfo));
7077 /* Compute the hash values for all exported symbols. At the same
7078 time store the values in an array so that we could use them for
7080 amt = dynsymcount * 2 * sizeof (unsigned long int);
7081 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7082 if (cinfo.hashcodes == NULL)
7085 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7086 cinfo.min_dynindx = -1;
7087 cinfo.output_bfd = output_bfd;
7090 /* Put all hash values in HASHCODES. */
7091 elf_link_hash_traverse (elf_hash_table (info),
7092 elf_collect_gnu_hash_codes, &cinfo);
7095 free (cinfo.hashcodes);
7100 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7102 if (bucketcount == 0)
7104 free (cinfo.hashcodes);
7108 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7109 BFD_ASSERT (s != NULL);
7111 if (cinfo.nsyms == 0)
7113 /* Empty .gnu.hash section is special. */
7114 BFD_ASSERT (cinfo.min_dynindx == -1);
7115 free (cinfo.hashcodes);
7116 s->size = 5 * 4 + bed->s->arch_size / 8;
7117 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7118 if (contents == NULL)
7120 s->contents = contents;
7121 /* 1 empty bucket. */
7122 bfd_put_32 (output_bfd, 1, contents);
7123 /* SYMIDX above the special symbol 0. */
7124 bfd_put_32 (output_bfd, 1, contents + 4);
7125 /* Just one word for bitmask. */
7126 bfd_put_32 (output_bfd, 1, contents + 8);
7127 /* Only hash fn bloom filter. */
7128 bfd_put_32 (output_bfd, 0, contents + 12);
7129 /* No hashes are valid - empty bitmask. */
7130 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7131 /* No hashes in the only bucket. */
7132 bfd_put_32 (output_bfd, 0,
7133 contents + 16 + bed->s->arch_size / 8);
7137 unsigned long int maskwords, maskbitslog2, x;
7138 BFD_ASSERT (cinfo.min_dynindx != -1);
7142 while ((x >>= 1) != 0)
7144 if (maskbitslog2 < 3)
7146 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7147 maskbitslog2 = maskbitslog2 + 3;
7149 maskbitslog2 = maskbitslog2 + 2;
7150 if (bed->s->arch_size == 64)
7152 if (maskbitslog2 == 5)
7158 cinfo.mask = (1 << cinfo.shift1) - 1;
7159 cinfo.shift2 = maskbitslog2;
7160 cinfo.maskbits = 1 << maskbitslog2;
7161 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7162 amt = bucketcount * sizeof (unsigned long int) * 2;
7163 amt += maskwords * sizeof (bfd_vma);
7164 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7165 if (cinfo.bitmask == NULL)
7167 free (cinfo.hashcodes);
7171 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7172 cinfo.indx = cinfo.counts + bucketcount;
7173 cinfo.symindx = dynsymcount - cinfo.nsyms;
7174 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7176 /* Determine how often each hash bucket is used. */
7177 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7178 for (i = 0; i < cinfo.nsyms; ++i)
7179 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7181 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7182 if (cinfo.counts[i] != 0)
7184 cinfo.indx[i] = cnt;
7185 cnt += cinfo.counts[i];
7187 BFD_ASSERT (cnt == dynsymcount);
7188 cinfo.bucketcount = bucketcount;
7189 cinfo.local_indx = cinfo.min_dynindx;
7191 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7192 s->size += cinfo.maskbits / 8;
7193 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7194 if (contents == NULL)
7196 free (cinfo.bitmask);
7197 free (cinfo.hashcodes);
7201 s->contents = contents;
7202 bfd_put_32 (output_bfd, bucketcount, contents);
7203 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7204 bfd_put_32 (output_bfd, maskwords, contents + 8);
7205 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7206 contents += 16 + cinfo.maskbits / 8;
7208 for (i = 0; i < bucketcount; ++i)
7210 if (cinfo.counts[i] == 0)
7211 bfd_put_32 (output_bfd, 0, contents);
7213 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7217 cinfo.contents = contents;
7219 /* Renumber dynamic symbols, populate .gnu.hash section. */
7220 elf_link_hash_traverse (elf_hash_table (info),
7221 elf_renumber_gnu_hash_syms, &cinfo);
7223 contents = s->contents + 16;
7224 for (i = 0; i < maskwords; ++i)
7226 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7228 contents += bed->s->arch_size / 8;
7231 free (cinfo.bitmask);
7232 free (cinfo.hashcodes);
7236 s = bfd_get_linker_section (dynobj, ".dynstr");
7237 BFD_ASSERT (s != NULL);
7239 elf_finalize_dynstr (output_bfd, info);
7241 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7243 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7244 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7251 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7254 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7257 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7258 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7261 /* Finish SHF_MERGE section merging. */
7264 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7269 if (!is_elf_hash_table (info->hash))
7272 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7273 if ((ibfd->flags & DYNAMIC) == 0
7274 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7275 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7276 == get_elf_backend_data (obfd)->s->elfclass))
7277 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7278 if ((sec->flags & SEC_MERGE) != 0
7279 && !bfd_is_abs_section (sec->output_section))
7281 struct bfd_elf_section_data *secdata;
7283 secdata = elf_section_data (sec);
7284 if (! _bfd_add_merge_section (obfd,
7285 &elf_hash_table (info)->merge_info,
7286 sec, &secdata->sec_info))
7288 else if (secdata->sec_info)
7289 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7292 if (elf_hash_table (info)->merge_info != NULL)
7293 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7294 merge_sections_remove_hook);
7298 /* Create an entry in an ELF linker hash table. */
7300 struct bfd_hash_entry *
7301 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7302 struct bfd_hash_table *table,
7305 /* Allocate the structure if it has not already been allocated by a
7309 entry = (struct bfd_hash_entry *)
7310 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7315 /* Call the allocation method of the superclass. */
7316 entry = _bfd_link_hash_newfunc (entry, table, string);
7319 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7320 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7322 /* Set local fields. */
7325 ret->got = htab->init_got_refcount;
7326 ret->plt = htab->init_plt_refcount;
7327 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7328 - offsetof (struct elf_link_hash_entry, size)));
7329 /* Assume that we have been called by a non-ELF symbol reader.
7330 This flag is then reset by the code which reads an ELF input
7331 file. This ensures that a symbol created by a non-ELF symbol
7332 reader will have the flag set correctly. */
7339 /* Copy data from an indirect symbol to its direct symbol, hiding the
7340 old indirect symbol. Also used for copying flags to a weakdef. */
7343 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7344 struct elf_link_hash_entry *dir,
7345 struct elf_link_hash_entry *ind)
7347 struct elf_link_hash_table *htab;
7349 /* Copy down any references that we may have already seen to the
7350 symbol which just became indirect. */
7352 if (dir->versioned != versioned_hidden)
7353 dir->ref_dynamic |= ind->ref_dynamic;
7354 dir->ref_regular |= ind->ref_regular;
7355 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7356 dir->non_got_ref |= ind->non_got_ref;
7357 dir->needs_plt |= ind->needs_plt;
7358 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7360 if (ind->root.type != bfd_link_hash_indirect)
7363 /* Copy over the global and procedure linkage table refcount entries.
7364 These may have been already set up by a check_relocs routine. */
7365 htab = elf_hash_table (info);
7366 if (ind->got.refcount > htab->init_got_refcount.refcount)
7368 if (dir->got.refcount < 0)
7369 dir->got.refcount = 0;
7370 dir->got.refcount += ind->got.refcount;
7371 ind->got.refcount = htab->init_got_refcount.refcount;
7374 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7376 if (dir->plt.refcount < 0)
7377 dir->plt.refcount = 0;
7378 dir->plt.refcount += ind->plt.refcount;
7379 ind->plt.refcount = htab->init_plt_refcount.refcount;
7382 if (ind->dynindx != -1)
7384 if (dir->dynindx != -1)
7385 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7386 dir->dynindx = ind->dynindx;
7387 dir->dynstr_index = ind->dynstr_index;
7389 ind->dynstr_index = 0;
7394 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7395 struct elf_link_hash_entry *h,
7396 bfd_boolean force_local)
7398 /* STT_GNU_IFUNC symbol must go through PLT. */
7399 if (h->type != STT_GNU_IFUNC)
7401 h->plt = elf_hash_table (info)->init_plt_offset;
7406 h->forced_local = 1;
7407 if (h->dynindx != -1)
7409 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7412 h->dynstr_index = 0;
7417 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7421 _bfd_elf_link_hash_table_init
7422 (struct elf_link_hash_table *table,
7424 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7425 struct bfd_hash_table *,
7427 unsigned int entsize,
7428 enum elf_target_id target_id)
7431 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7433 table->init_got_refcount.refcount = can_refcount - 1;
7434 table->init_plt_refcount.refcount = can_refcount - 1;
7435 table->init_got_offset.offset = -(bfd_vma) 1;
7436 table->init_plt_offset.offset = -(bfd_vma) 1;
7437 /* The first dynamic symbol is a dummy. */
7438 table->dynsymcount = 1;
7440 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7442 table->root.type = bfd_link_elf_hash_table;
7443 table->hash_table_id = target_id;
7448 /* Create an ELF linker hash table. */
7450 struct bfd_link_hash_table *
7451 _bfd_elf_link_hash_table_create (bfd *abfd)
7453 struct elf_link_hash_table *ret;
7454 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7456 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7460 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7461 sizeof (struct elf_link_hash_entry),
7467 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7472 /* Destroy an ELF linker hash table. */
7475 _bfd_elf_link_hash_table_free (bfd *obfd)
7477 struct elf_link_hash_table *htab;
7479 htab = (struct elf_link_hash_table *) obfd->link.hash;
7480 if (htab->dynstr != NULL)
7481 _bfd_elf_strtab_free (htab->dynstr);
7482 _bfd_merge_sections_free (htab->merge_info);
7483 _bfd_generic_link_hash_table_free (obfd);
7486 /* This is a hook for the ELF emulation code in the generic linker to
7487 tell the backend linker what file name to use for the DT_NEEDED
7488 entry for a dynamic object. */
7491 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7493 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7494 && bfd_get_format (abfd) == bfd_object)
7495 elf_dt_name (abfd) = name;
7499 bfd_elf_get_dyn_lib_class (bfd *abfd)
7502 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7503 && bfd_get_format (abfd) == bfd_object)
7504 lib_class = elf_dyn_lib_class (abfd);
7511 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7513 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7514 && bfd_get_format (abfd) == bfd_object)
7515 elf_dyn_lib_class (abfd) = lib_class;
7518 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7519 the linker ELF emulation code. */
7521 struct bfd_link_needed_list *
7522 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7523 struct bfd_link_info *info)
7525 if (! is_elf_hash_table (info->hash))
7527 return elf_hash_table (info)->needed;
7530 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7531 hook for the linker ELF emulation code. */
7533 struct bfd_link_needed_list *
7534 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7535 struct bfd_link_info *info)
7537 if (! is_elf_hash_table (info->hash))
7539 return elf_hash_table (info)->runpath;
7542 /* Get the name actually used for a dynamic object for a link. This
7543 is the SONAME entry if there is one. Otherwise, it is the string
7544 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7547 bfd_elf_get_dt_soname (bfd *abfd)
7549 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7550 && bfd_get_format (abfd) == bfd_object)
7551 return elf_dt_name (abfd);
7555 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7556 the ELF linker emulation code. */
7559 bfd_elf_get_bfd_needed_list (bfd *abfd,
7560 struct bfd_link_needed_list **pneeded)
7563 bfd_byte *dynbuf = NULL;
7564 unsigned int elfsec;
7565 unsigned long shlink;
7566 bfd_byte *extdyn, *extdynend;
7568 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7572 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7573 || bfd_get_format (abfd) != bfd_object)
7576 s = bfd_get_section_by_name (abfd, ".dynamic");
7577 if (s == NULL || s->size == 0)
7580 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7583 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7584 if (elfsec == SHN_BAD)
7587 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7589 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7590 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7593 extdynend = extdyn + s->size;
7594 for (; extdyn < extdynend; extdyn += extdynsize)
7596 Elf_Internal_Dyn dyn;
7598 (*swap_dyn_in) (abfd, extdyn, &dyn);
7600 if (dyn.d_tag == DT_NULL)
7603 if (dyn.d_tag == DT_NEEDED)
7606 struct bfd_link_needed_list *l;
7607 unsigned int tagv = dyn.d_un.d_val;
7610 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7615 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7636 struct elf_symbuf_symbol
7638 unsigned long st_name; /* Symbol name, index in string tbl */
7639 unsigned char st_info; /* Type and binding attributes */
7640 unsigned char st_other; /* Visibilty, and target specific */
7643 struct elf_symbuf_head
7645 struct elf_symbuf_symbol *ssym;
7647 unsigned int st_shndx;
7654 Elf_Internal_Sym *isym;
7655 struct elf_symbuf_symbol *ssym;
7660 /* Sort references to symbols by ascending section number. */
7663 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7665 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7666 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7668 return s1->st_shndx - s2->st_shndx;
7672 elf_sym_name_compare (const void *arg1, const void *arg2)
7674 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7675 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7676 return strcmp (s1->name, s2->name);
7679 static struct elf_symbuf_head *
7680 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7682 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7683 struct elf_symbuf_symbol *ssym;
7684 struct elf_symbuf_head *ssymbuf, *ssymhead;
7685 size_t i, shndx_count, total_size;
7687 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7691 for (ind = indbuf, i = 0; i < symcount; i++)
7692 if (isymbuf[i].st_shndx != SHN_UNDEF)
7693 *ind++ = &isymbuf[i];
7696 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7697 elf_sort_elf_symbol);
7700 if (indbufend > indbuf)
7701 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7702 if (ind[0]->st_shndx != ind[1]->st_shndx)
7705 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7706 + (indbufend - indbuf) * sizeof (*ssym));
7707 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7708 if (ssymbuf == NULL)
7714 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7715 ssymbuf->ssym = NULL;
7716 ssymbuf->count = shndx_count;
7717 ssymbuf->st_shndx = 0;
7718 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7720 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7723 ssymhead->ssym = ssym;
7724 ssymhead->count = 0;
7725 ssymhead->st_shndx = (*ind)->st_shndx;
7727 ssym->st_name = (*ind)->st_name;
7728 ssym->st_info = (*ind)->st_info;
7729 ssym->st_other = (*ind)->st_other;
7732 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7733 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7740 /* Check if 2 sections define the same set of local and global
7744 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7745 struct bfd_link_info *info)
7748 const struct elf_backend_data *bed1, *bed2;
7749 Elf_Internal_Shdr *hdr1, *hdr2;
7750 size_t symcount1, symcount2;
7751 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7752 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7753 Elf_Internal_Sym *isym, *isymend;
7754 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7755 size_t count1, count2, i;
7756 unsigned int shndx1, shndx2;
7762 /* Both sections have to be in ELF. */
7763 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7764 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7767 if (elf_section_type (sec1) != elf_section_type (sec2))
7770 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7771 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7772 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7775 bed1 = get_elf_backend_data (bfd1);
7776 bed2 = get_elf_backend_data (bfd2);
7777 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7778 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7779 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7780 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7782 if (symcount1 == 0 || symcount2 == 0)
7788 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7789 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7791 if (ssymbuf1 == NULL)
7793 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7795 if (isymbuf1 == NULL)
7798 if (!info->reduce_memory_overheads)
7799 elf_tdata (bfd1)->symbuf = ssymbuf1
7800 = elf_create_symbuf (symcount1, isymbuf1);
7803 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7805 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7807 if (isymbuf2 == NULL)
7810 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7811 elf_tdata (bfd2)->symbuf = ssymbuf2
7812 = elf_create_symbuf (symcount2, isymbuf2);
7815 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7817 /* Optimized faster version. */
7819 struct elf_symbol *symp;
7820 struct elf_symbuf_symbol *ssym, *ssymend;
7823 hi = ssymbuf1->count;
7828 mid = (lo + hi) / 2;
7829 if (shndx1 < ssymbuf1[mid].st_shndx)
7831 else if (shndx1 > ssymbuf1[mid].st_shndx)
7835 count1 = ssymbuf1[mid].count;
7842 hi = ssymbuf2->count;
7847 mid = (lo + hi) / 2;
7848 if (shndx2 < ssymbuf2[mid].st_shndx)
7850 else if (shndx2 > ssymbuf2[mid].st_shndx)
7854 count2 = ssymbuf2[mid].count;
7860 if (count1 == 0 || count2 == 0 || count1 != count2)
7864 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7866 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7867 if (symtable1 == NULL || symtable2 == NULL)
7871 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7872 ssym < ssymend; ssym++, symp++)
7874 symp->u.ssym = ssym;
7875 symp->name = bfd_elf_string_from_elf_section (bfd1,
7881 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7882 ssym < ssymend; ssym++, symp++)
7884 symp->u.ssym = ssym;
7885 symp->name = bfd_elf_string_from_elf_section (bfd2,
7890 /* Sort symbol by name. */
7891 qsort (symtable1, count1, sizeof (struct elf_symbol),
7892 elf_sym_name_compare);
7893 qsort (symtable2, count1, sizeof (struct elf_symbol),
7894 elf_sym_name_compare);
7896 for (i = 0; i < count1; i++)
7897 /* Two symbols must have the same binding, type and name. */
7898 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7899 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7900 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7907 symtable1 = (struct elf_symbol *)
7908 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7909 symtable2 = (struct elf_symbol *)
7910 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7911 if (symtable1 == NULL || symtable2 == NULL)
7914 /* Count definitions in the section. */
7916 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7917 if (isym->st_shndx == shndx1)
7918 symtable1[count1++].u.isym = isym;
7921 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7922 if (isym->st_shndx == shndx2)
7923 symtable2[count2++].u.isym = isym;
7925 if (count1 == 0 || count2 == 0 || count1 != count2)
7928 for (i = 0; i < count1; i++)
7930 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7931 symtable1[i].u.isym->st_name);
7933 for (i = 0; i < count2; i++)
7935 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7936 symtable2[i].u.isym->st_name);
7938 /* Sort symbol by name. */
7939 qsort (symtable1, count1, sizeof (struct elf_symbol),
7940 elf_sym_name_compare);
7941 qsort (symtable2, count1, sizeof (struct elf_symbol),
7942 elf_sym_name_compare);
7944 for (i = 0; i < count1; i++)
7945 /* Two symbols must have the same binding, type and name. */
7946 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7947 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7948 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7966 /* Return TRUE if 2 section types are compatible. */
7969 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7970 bfd *bbfd, const asection *bsec)
7974 || abfd->xvec->flavour != bfd_target_elf_flavour
7975 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7978 return elf_section_type (asec) == elf_section_type (bsec);
7981 /* Final phase of ELF linker. */
7983 /* A structure we use to avoid passing large numbers of arguments. */
7985 struct elf_final_link_info
7987 /* General link information. */
7988 struct bfd_link_info *info;
7991 /* Symbol string table. */
7992 struct elf_strtab_hash *symstrtab;
7993 /* .hash section. */
7995 /* symbol version section (.gnu.version). */
7996 asection *symver_sec;
7997 /* Buffer large enough to hold contents of any section. */
7999 /* Buffer large enough to hold external relocs of any section. */
8000 void *external_relocs;
8001 /* Buffer large enough to hold internal relocs of any section. */
8002 Elf_Internal_Rela *internal_relocs;
8003 /* Buffer large enough to hold external local symbols of any input
8005 bfd_byte *external_syms;
8006 /* And a buffer for symbol section indices. */
8007 Elf_External_Sym_Shndx *locsym_shndx;
8008 /* Buffer large enough to hold internal local symbols of any input
8010 Elf_Internal_Sym *internal_syms;
8011 /* Array large enough to hold a symbol index for each local symbol
8012 of any input BFD. */
8014 /* Array large enough to hold a section pointer for each local
8015 symbol of any input BFD. */
8016 asection **sections;
8017 /* Buffer for SHT_SYMTAB_SHNDX section. */
8018 Elf_External_Sym_Shndx *symshndxbuf;
8019 /* Number of STT_FILE syms seen. */
8020 size_t filesym_count;
8023 /* This struct is used to pass information to elf_link_output_extsym. */
8025 struct elf_outext_info
8028 bfd_boolean localsyms;
8029 bfd_boolean file_sym_done;
8030 struct elf_final_link_info *flinfo;
8034 /* Support for evaluating a complex relocation.
8036 Complex relocations are generalized, self-describing relocations. The
8037 implementation of them consists of two parts: complex symbols, and the
8038 relocations themselves.
8040 The relocations are use a reserved elf-wide relocation type code (R_RELC
8041 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8042 information (start bit, end bit, word width, etc) into the addend. This
8043 information is extracted from CGEN-generated operand tables within gas.
8045 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8046 internal) representing prefix-notation expressions, including but not
8047 limited to those sorts of expressions normally encoded as addends in the
8048 addend field. The symbol mangling format is:
8051 | <unary-operator> ':' <node>
8052 | <binary-operator> ':' <node> ':' <node>
8055 <literal> := 's' <digits=N> ':' <N character symbol name>
8056 | 'S' <digits=N> ':' <N character section name>
8060 <binary-operator> := as in C
8061 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8064 set_symbol_value (bfd *bfd_with_globals,
8065 Elf_Internal_Sym *isymbuf,
8070 struct elf_link_hash_entry **sym_hashes;
8071 struct elf_link_hash_entry *h;
8072 size_t extsymoff = locsymcount;
8074 if (symidx < locsymcount)
8076 Elf_Internal_Sym *sym;
8078 sym = isymbuf + symidx;
8079 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8081 /* It is a local symbol: move it to the
8082 "absolute" section and give it a value. */
8083 sym->st_shndx = SHN_ABS;
8084 sym->st_value = val;
8087 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8091 /* It is a global symbol: set its link type
8092 to "defined" and give it a value. */
8094 sym_hashes = elf_sym_hashes (bfd_with_globals);
8095 h = sym_hashes [symidx - extsymoff];
8096 while (h->root.type == bfd_link_hash_indirect
8097 || h->root.type == bfd_link_hash_warning)
8098 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8099 h->root.type = bfd_link_hash_defined;
8100 h->root.u.def.value = val;
8101 h->root.u.def.section = bfd_abs_section_ptr;
8105 resolve_symbol (const char *name,
8107 struct elf_final_link_info *flinfo,
8109 Elf_Internal_Sym *isymbuf,
8112 Elf_Internal_Sym *sym;
8113 struct bfd_link_hash_entry *global_entry;
8114 const char *candidate = NULL;
8115 Elf_Internal_Shdr *symtab_hdr;
8118 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8120 for (i = 0; i < locsymcount; ++ i)
8124 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8127 candidate = bfd_elf_string_from_elf_section (input_bfd,
8128 symtab_hdr->sh_link,
8131 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8132 name, candidate, (unsigned long) sym->st_value);
8134 if (candidate && strcmp (candidate, name) == 0)
8136 asection *sec = flinfo->sections [i];
8138 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8139 *result += sec->output_offset + sec->output_section->vma;
8141 printf ("Found symbol with value %8.8lx\n",
8142 (unsigned long) *result);
8148 /* Hmm, haven't found it yet. perhaps it is a global. */
8149 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8150 FALSE, FALSE, TRUE);
8154 if (global_entry->type == bfd_link_hash_defined
8155 || global_entry->type == bfd_link_hash_defweak)
8157 *result = (global_entry->u.def.value
8158 + global_entry->u.def.section->output_section->vma
8159 + global_entry->u.def.section->output_offset);
8161 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8162 global_entry->root.string, (unsigned long) *result);
8170 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8171 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8172 names like "foo.end" which is the end address of section "foo". */
8175 resolve_section (const char *name,
8183 for (curr = sections; curr; curr = curr->next)
8184 if (strcmp (curr->name, name) == 0)
8186 *result = curr->vma;
8190 /* Hmm. still haven't found it. try pseudo-section names. */
8191 /* FIXME: This could be coded more efficiently... */
8192 for (curr = sections; curr; curr = curr->next)
8194 len = strlen (curr->name);
8195 if (len > strlen (name))
8198 if (strncmp (curr->name, name, len) == 0)
8200 if (strncmp (".end", name + len, 4) == 0)
8202 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8206 /* Insert more pseudo-section names here, if you like. */
8214 undefined_reference (const char *reftype, const char *name)
8216 /* xgettext:c-format */
8217 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8222 eval_symbol (bfd_vma *result,
8225 struct elf_final_link_info *flinfo,
8227 Elf_Internal_Sym *isymbuf,
8236 const char *sym = *symp;
8238 bfd_boolean symbol_is_section = FALSE;
8243 if (len < 1 || len > sizeof (symbuf))
8245 bfd_set_error (bfd_error_invalid_operation);
8258 *result = strtoul (sym, (char **) symp, 16);
8262 symbol_is_section = TRUE;
8266 symlen = strtol (sym, (char **) symp, 10);
8267 sym = *symp + 1; /* Skip the trailing ':'. */
8269 if (symend < sym || symlen + 1 > sizeof (symbuf))
8271 bfd_set_error (bfd_error_invalid_operation);
8275 memcpy (symbuf, sym, symlen);
8276 symbuf[symlen] = '\0';
8277 *symp = sym + symlen;
8279 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8280 the symbol as a section, or vice-versa. so we're pretty liberal in our
8281 interpretation here; section means "try section first", not "must be a
8282 section", and likewise with symbol. */
8284 if (symbol_is_section)
8286 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8287 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8288 isymbuf, locsymcount))
8290 undefined_reference ("section", symbuf);
8296 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8297 isymbuf, locsymcount)
8298 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8301 undefined_reference ("symbol", symbuf);
8308 /* All that remains are operators. */
8310 #define UNARY_OP(op) \
8311 if (strncmp (sym, #op, strlen (#op)) == 0) \
8313 sym += strlen (#op); \
8317 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8318 isymbuf, locsymcount, signed_p)) \
8321 *result = op ((bfd_signed_vma) a); \
8327 #define BINARY_OP(op) \
8328 if (strncmp (sym, #op, strlen (#op)) == 0) \
8330 sym += strlen (#op); \
8334 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8335 isymbuf, locsymcount, signed_p)) \
8338 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8339 isymbuf, locsymcount, signed_p)) \
8342 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8372 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8373 bfd_set_error (bfd_error_invalid_operation);
8379 put_value (bfd_vma size,
8380 unsigned long chunksz,
8385 location += (size - chunksz);
8387 for (; size; size -= chunksz, location -= chunksz)
8392 bfd_put_8 (input_bfd, x, location);
8396 bfd_put_16 (input_bfd, x, location);
8400 bfd_put_32 (input_bfd, x, location);
8401 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8407 bfd_put_64 (input_bfd, x, location);
8408 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8421 get_value (bfd_vma size,
8422 unsigned long chunksz,
8429 /* Sanity checks. */
8430 BFD_ASSERT (chunksz <= sizeof (x)
8433 && (size % chunksz) == 0
8434 && input_bfd != NULL
8435 && location != NULL);
8437 if (chunksz == sizeof (x))
8439 BFD_ASSERT (size == chunksz);
8441 /* Make sure that we do not perform an undefined shift operation.
8442 We know that size == chunksz so there will only be one iteration
8443 of the loop below. */
8447 shift = 8 * chunksz;
8449 for (; size; size -= chunksz, location += chunksz)
8454 x = (x << shift) | bfd_get_8 (input_bfd, location);
8457 x = (x << shift) | bfd_get_16 (input_bfd, location);
8460 x = (x << shift) | bfd_get_32 (input_bfd, location);
8464 x = (x << shift) | bfd_get_64 (input_bfd, location);
8475 decode_complex_addend (unsigned long *start, /* in bits */
8476 unsigned long *oplen, /* in bits */
8477 unsigned long *len, /* in bits */
8478 unsigned long *wordsz, /* in bytes */
8479 unsigned long *chunksz, /* in bytes */
8480 unsigned long *lsb0_p,
8481 unsigned long *signed_p,
8482 unsigned long *trunc_p,
8483 unsigned long encoded)
8485 * start = encoded & 0x3F;
8486 * len = (encoded >> 6) & 0x3F;
8487 * oplen = (encoded >> 12) & 0x3F;
8488 * wordsz = (encoded >> 18) & 0xF;
8489 * chunksz = (encoded >> 22) & 0xF;
8490 * lsb0_p = (encoded >> 27) & 1;
8491 * signed_p = (encoded >> 28) & 1;
8492 * trunc_p = (encoded >> 29) & 1;
8495 bfd_reloc_status_type
8496 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8497 asection *input_section ATTRIBUTE_UNUSED,
8499 Elf_Internal_Rela *rel,
8502 bfd_vma shift, x, mask;
8503 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8504 bfd_reloc_status_type r;
8506 /* Perform this reloc, since it is complex.
8507 (this is not to say that it necessarily refers to a complex
8508 symbol; merely that it is a self-describing CGEN based reloc.
8509 i.e. the addend has the complete reloc information (bit start, end,
8510 word size, etc) encoded within it.). */
8512 decode_complex_addend (&start, &oplen, &len, &wordsz,
8513 &chunksz, &lsb0_p, &signed_p,
8514 &trunc_p, rel->r_addend);
8516 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8519 shift = (start + 1) - len;
8521 shift = (8 * wordsz) - (start + len);
8523 x = get_value (wordsz, chunksz, input_bfd,
8524 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8527 printf ("Doing complex reloc: "
8528 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8529 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8530 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8531 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8532 oplen, (unsigned long) x, (unsigned long) mask,
8533 (unsigned long) relocation);
8538 /* Now do an overflow check. */
8539 r = bfd_check_overflow ((signed_p
8540 ? complain_overflow_signed
8541 : complain_overflow_unsigned),
8542 len, 0, (8 * wordsz),
8546 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8549 printf (" relocation: %8.8lx\n"
8550 " shifted mask: %8.8lx\n"
8551 " shifted/masked reloc: %8.8lx\n"
8552 " result: %8.8lx\n",
8553 (unsigned long) relocation, (unsigned long) (mask << shift),
8554 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8556 put_value (wordsz, chunksz, input_bfd, x,
8557 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8561 /* Functions to read r_offset from external (target order) reloc
8562 entry. Faster than bfd_getl32 et al, because we let the compiler
8563 know the value is aligned. */
8566 ext32l_r_offset (const void *p)
8573 const union aligned32 *a
8574 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8576 uint32_t aval = ( (uint32_t) a->c[0]
8577 | (uint32_t) a->c[1] << 8
8578 | (uint32_t) a->c[2] << 16
8579 | (uint32_t) a->c[3] << 24);
8584 ext32b_r_offset (const void *p)
8591 const union aligned32 *a
8592 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8594 uint32_t aval = ( (uint32_t) a->c[0] << 24
8595 | (uint32_t) a->c[1] << 16
8596 | (uint32_t) a->c[2] << 8
8597 | (uint32_t) a->c[3]);
8601 #ifdef BFD_HOST_64_BIT
8603 ext64l_r_offset (const void *p)
8610 const union aligned64 *a
8611 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8613 uint64_t aval = ( (uint64_t) a->c[0]
8614 | (uint64_t) a->c[1] << 8
8615 | (uint64_t) a->c[2] << 16
8616 | (uint64_t) a->c[3] << 24
8617 | (uint64_t) a->c[4] << 32
8618 | (uint64_t) a->c[5] << 40
8619 | (uint64_t) a->c[6] << 48
8620 | (uint64_t) a->c[7] << 56);
8625 ext64b_r_offset (const void *p)
8632 const union aligned64 *a
8633 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8635 uint64_t aval = ( (uint64_t) a->c[0] << 56
8636 | (uint64_t) a->c[1] << 48
8637 | (uint64_t) a->c[2] << 40
8638 | (uint64_t) a->c[3] << 32
8639 | (uint64_t) a->c[4] << 24
8640 | (uint64_t) a->c[5] << 16
8641 | (uint64_t) a->c[6] << 8
8642 | (uint64_t) a->c[7]);
8647 /* When performing a relocatable link, the input relocations are
8648 preserved. But, if they reference global symbols, the indices
8649 referenced must be updated. Update all the relocations found in
8653 elf_link_adjust_relocs (bfd *abfd,
8655 struct bfd_elf_section_reloc_data *reldata,
8657 struct bfd_link_info *info)
8660 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8662 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8663 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8664 bfd_vma r_type_mask;
8666 unsigned int count = reldata->count;
8667 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8669 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8671 swap_in = bed->s->swap_reloc_in;
8672 swap_out = bed->s->swap_reloc_out;
8674 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8676 swap_in = bed->s->swap_reloca_in;
8677 swap_out = bed->s->swap_reloca_out;
8682 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8685 if (bed->s->arch_size == 32)
8692 r_type_mask = 0xffffffff;
8696 erela = reldata->hdr->contents;
8697 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8699 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8702 if (*rel_hash == NULL)
8705 if ((*rel_hash)->indx == -2
8706 && info->gc_sections
8707 && ! info->gc_keep_exported)
8709 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8710 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8712 (*rel_hash)->root.root.string);
8713 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8715 bfd_set_error (bfd_error_invalid_operation);
8718 BFD_ASSERT ((*rel_hash)->indx >= 0);
8720 (*swap_in) (abfd, erela, irela);
8721 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8722 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8723 | (irela[j].r_info & r_type_mask));
8724 (*swap_out) (abfd, irela, erela);
8727 if (bed->elf_backend_update_relocs)
8728 (*bed->elf_backend_update_relocs) (sec, reldata);
8730 if (sort && count != 0)
8732 bfd_vma (*ext_r_off) (const void *);
8735 bfd_byte *base, *end, *p, *loc;
8736 bfd_byte *buf = NULL;
8738 if (bed->s->arch_size == 32)
8740 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8741 ext_r_off = ext32l_r_offset;
8742 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8743 ext_r_off = ext32b_r_offset;
8749 #ifdef BFD_HOST_64_BIT
8750 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8751 ext_r_off = ext64l_r_offset;
8752 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8753 ext_r_off = ext64b_r_offset;
8759 /* Must use a stable sort here. A modified insertion sort,
8760 since the relocs are mostly sorted already. */
8761 elt_size = reldata->hdr->sh_entsize;
8762 base = reldata->hdr->contents;
8763 end = base + count * elt_size;
8764 if (elt_size > sizeof (Elf64_External_Rela))
8767 /* Ensure the first element is lowest. This acts as a sentinel,
8768 speeding the main loop below. */
8769 r_off = (*ext_r_off) (base);
8770 for (p = loc = base; (p += elt_size) < end; )
8772 bfd_vma r_off2 = (*ext_r_off) (p);
8781 /* Don't just swap *base and *loc as that changes the order
8782 of the original base[0] and base[1] if they happen to
8783 have the same r_offset. */
8784 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8785 memcpy (onebuf, loc, elt_size);
8786 memmove (base + elt_size, base, loc - base);
8787 memcpy (base, onebuf, elt_size);
8790 for (p = base + elt_size; (p += elt_size) < end; )
8792 /* base to p is sorted, *p is next to insert. */
8793 r_off = (*ext_r_off) (p);
8794 /* Search the sorted region for location to insert. */
8796 while (r_off < (*ext_r_off) (loc))
8801 /* Chances are there is a run of relocs to insert here,
8802 from one of more input files. Files are not always
8803 linked in order due to the way elf_link_input_bfd is
8804 called. See pr17666. */
8805 size_t sortlen = p - loc;
8806 bfd_vma r_off2 = (*ext_r_off) (loc);
8807 size_t runlen = elt_size;
8808 size_t buf_size = 96 * 1024;
8809 while (p + runlen < end
8810 && (sortlen <= buf_size
8811 || runlen + elt_size <= buf_size)
8812 && r_off2 > (*ext_r_off) (p + runlen))
8816 buf = bfd_malloc (buf_size);
8820 if (runlen < sortlen)
8822 memcpy (buf, p, runlen);
8823 memmove (loc + runlen, loc, sortlen);
8824 memcpy (loc, buf, runlen);
8828 memcpy (buf, loc, sortlen);
8829 memmove (loc, p, runlen);
8830 memcpy (loc + runlen, buf, sortlen);
8832 p += runlen - elt_size;
8835 /* Hashes are no longer valid. */
8836 free (reldata->hashes);
8837 reldata->hashes = NULL;
8843 struct elf_link_sort_rela
8849 enum elf_reloc_type_class type;
8850 /* We use this as an array of size int_rels_per_ext_rel. */
8851 Elf_Internal_Rela rela[1];
8855 elf_link_sort_cmp1 (const void *A, const void *B)
8857 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8858 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8859 int relativea, relativeb;
8861 relativea = a->type == reloc_class_relative;
8862 relativeb = b->type == reloc_class_relative;
8864 if (relativea < relativeb)
8866 if (relativea > relativeb)
8868 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8870 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8872 if (a->rela->r_offset < b->rela->r_offset)
8874 if (a->rela->r_offset > b->rela->r_offset)
8880 elf_link_sort_cmp2 (const void *A, const void *B)
8882 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8883 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8885 if (a->type < b->type)
8887 if (a->type > b->type)
8889 if (a->u.offset < b->u.offset)
8891 if (a->u.offset > b->u.offset)
8893 if (a->rela->r_offset < b->rela->r_offset)
8895 if (a->rela->r_offset > b->rela->r_offset)
8901 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8903 asection *dynamic_relocs;
8906 bfd_size_type count, size;
8907 size_t i, ret, sort_elt, ext_size;
8908 bfd_byte *sort, *s_non_relative, *p;
8909 struct elf_link_sort_rela *sq;
8910 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8911 int i2e = bed->s->int_rels_per_ext_rel;
8912 unsigned int opb = bfd_octets_per_byte (abfd);
8913 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8914 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8915 struct bfd_link_order *lo;
8917 bfd_boolean use_rela;
8919 /* Find a dynamic reloc section. */
8920 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8921 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8922 if (rela_dyn != NULL && rela_dyn->size > 0
8923 && rel_dyn != NULL && rel_dyn->size > 0)
8925 bfd_boolean use_rela_initialised = FALSE;
8927 /* This is just here to stop gcc from complaining.
8928 Its initialization checking code is not perfect. */
8931 /* Both sections are present. Examine the sizes
8932 of the indirect sections to help us choose. */
8933 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8934 if (lo->type == bfd_indirect_link_order)
8936 asection *o = lo->u.indirect.section;
8938 if ((o->size % bed->s->sizeof_rela) == 0)
8940 if ((o->size % bed->s->sizeof_rel) == 0)
8941 /* Section size is divisible by both rel and rela sizes.
8942 It is of no help to us. */
8946 /* Section size is only divisible by rela. */
8947 if (use_rela_initialised && !use_rela)
8949 _bfd_error_handler (_("%B: Unable to sort relocs - "
8950 "they are in more than one size"),
8952 bfd_set_error (bfd_error_invalid_operation);
8958 use_rela_initialised = TRUE;
8962 else if ((o->size % bed->s->sizeof_rel) == 0)
8964 /* Section size is only divisible by rel. */
8965 if (use_rela_initialised && use_rela)
8967 _bfd_error_handler (_("%B: Unable to sort relocs - "
8968 "they are in more than one size"),
8970 bfd_set_error (bfd_error_invalid_operation);
8976 use_rela_initialised = TRUE;
8981 /* The section size is not divisible by either -
8982 something is wrong. */
8983 _bfd_error_handler (_("%B: Unable to sort relocs - "
8984 "they are of an unknown size"), abfd);
8985 bfd_set_error (bfd_error_invalid_operation);
8990 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8991 if (lo->type == bfd_indirect_link_order)
8993 asection *o = lo->u.indirect.section;
8995 if ((o->size % bed->s->sizeof_rela) == 0)
8997 if ((o->size % bed->s->sizeof_rel) == 0)
8998 /* Section size is divisible by both rel and rela sizes.
8999 It is of no help to us. */
9003 /* Section size is only divisible by rela. */
9004 if (use_rela_initialised && !use_rela)
9006 _bfd_error_handler (_("%B: Unable to sort relocs - "
9007 "they are in more than one size"),
9009 bfd_set_error (bfd_error_invalid_operation);
9015 use_rela_initialised = TRUE;
9019 else if ((o->size % bed->s->sizeof_rel) == 0)
9021 /* Section size is only divisible by rel. */
9022 if (use_rela_initialised && use_rela)
9024 _bfd_error_handler (_("%B: Unable to sort relocs - "
9025 "they are in more than one size"),
9027 bfd_set_error (bfd_error_invalid_operation);
9033 use_rela_initialised = TRUE;
9038 /* The section size is not divisible by either -
9039 something is wrong. */
9040 _bfd_error_handler (_("%B: Unable to sort relocs - "
9041 "they are of an unknown size"), abfd);
9042 bfd_set_error (bfd_error_invalid_operation);
9047 if (! use_rela_initialised)
9051 else if (rela_dyn != NULL && rela_dyn->size > 0)
9053 else if (rel_dyn != NULL && rel_dyn->size > 0)
9060 dynamic_relocs = rela_dyn;
9061 ext_size = bed->s->sizeof_rela;
9062 swap_in = bed->s->swap_reloca_in;
9063 swap_out = bed->s->swap_reloca_out;
9067 dynamic_relocs = rel_dyn;
9068 ext_size = bed->s->sizeof_rel;
9069 swap_in = bed->s->swap_reloc_in;
9070 swap_out = bed->s->swap_reloc_out;
9074 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9075 if (lo->type == bfd_indirect_link_order)
9076 size += lo->u.indirect.section->size;
9078 if (size != dynamic_relocs->size)
9081 sort_elt = (sizeof (struct elf_link_sort_rela)
9082 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9084 count = dynamic_relocs->size / ext_size;
9087 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9091 (*info->callbacks->warning)
9092 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9096 if (bed->s->arch_size == 32)
9097 r_sym_mask = ~(bfd_vma) 0xff;
9099 r_sym_mask = ~(bfd_vma) 0xffffffff;
9101 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9102 if (lo->type == bfd_indirect_link_order)
9104 bfd_byte *erel, *erelend;
9105 asection *o = lo->u.indirect.section;
9107 if (o->contents == NULL && o->size != 0)
9109 /* This is a reloc section that is being handled as a normal
9110 section. See bfd_section_from_shdr. We can't combine
9111 relocs in this case. */
9116 erelend = o->contents + o->size;
9117 p = sort + o->output_offset * opb / ext_size * sort_elt;
9119 while (erel < erelend)
9121 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9123 (*swap_in) (abfd, erel, s->rela);
9124 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9125 s->u.sym_mask = r_sym_mask;
9131 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9133 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9135 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9136 if (s->type != reloc_class_relative)
9142 sq = (struct elf_link_sort_rela *) s_non_relative;
9143 for (; i < count; i++, p += sort_elt)
9145 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9146 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9148 sp->u.offset = sq->rela->r_offset;
9151 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9153 struct elf_link_hash_table *htab = elf_hash_table (info);
9154 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9156 /* We have plt relocs in .rela.dyn. */
9157 sq = (struct elf_link_sort_rela *) sort;
9158 for (i = 0; i < count; i++)
9159 if (sq[count - i - 1].type != reloc_class_plt)
9161 if (i != 0 && htab->srelplt->size == i * ext_size)
9163 struct bfd_link_order **plo;
9164 /* Put srelplt link_order last. This is so the output_offset
9165 set in the next loop is correct for DT_JMPREL. */
9166 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9167 if ((*plo)->type == bfd_indirect_link_order
9168 && (*plo)->u.indirect.section == htab->srelplt)
9174 plo = &(*plo)->next;
9177 dynamic_relocs->map_tail.link_order = lo;
9182 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9183 if (lo->type == bfd_indirect_link_order)
9185 bfd_byte *erel, *erelend;
9186 asection *o = lo->u.indirect.section;
9189 erelend = o->contents + o->size;
9190 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9191 while (erel < erelend)
9193 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9194 (*swap_out) (abfd, s->rela, erel);
9201 *psec = dynamic_relocs;
9205 /* Add a symbol to the output symbol string table. */
9208 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9210 Elf_Internal_Sym *elfsym,
9211 asection *input_sec,
9212 struct elf_link_hash_entry *h)
9214 int (*output_symbol_hook)
9215 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9216 struct elf_link_hash_entry *);
9217 struct elf_link_hash_table *hash_table;
9218 const struct elf_backend_data *bed;
9219 bfd_size_type strtabsize;
9221 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9223 bed = get_elf_backend_data (flinfo->output_bfd);
9224 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9225 if (output_symbol_hook != NULL)
9227 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9234 || (input_sec->flags & SEC_EXCLUDE))
9235 elfsym->st_name = (unsigned long) -1;
9238 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9239 to get the final offset for st_name. */
9241 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9243 if (elfsym->st_name == (unsigned long) -1)
9247 hash_table = elf_hash_table (flinfo->info);
9248 strtabsize = hash_table->strtabsize;
9249 if (strtabsize <= hash_table->strtabcount)
9251 strtabsize += strtabsize;
9252 hash_table->strtabsize = strtabsize;
9253 strtabsize *= sizeof (*hash_table->strtab);
9255 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9257 if (hash_table->strtab == NULL)
9260 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9261 hash_table->strtab[hash_table->strtabcount].dest_index
9262 = hash_table->strtabcount;
9263 hash_table->strtab[hash_table->strtabcount].destshndx_index
9264 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9266 bfd_get_symcount (flinfo->output_bfd) += 1;
9267 hash_table->strtabcount += 1;
9272 /* Swap symbols out to the symbol table and flush the output symbols to
9276 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9278 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9281 const struct elf_backend_data *bed;
9283 Elf_Internal_Shdr *hdr;
9287 if (!hash_table->strtabcount)
9290 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9292 bed = get_elf_backend_data (flinfo->output_bfd);
9294 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9295 symbuf = (bfd_byte *) bfd_malloc (amt);
9299 if (flinfo->symshndxbuf)
9301 amt = sizeof (Elf_External_Sym_Shndx);
9302 amt *= bfd_get_symcount (flinfo->output_bfd);
9303 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9304 if (flinfo->symshndxbuf == NULL)
9311 for (i = 0; i < hash_table->strtabcount; i++)
9313 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9314 if (elfsym->sym.st_name == (unsigned long) -1)
9315 elfsym->sym.st_name = 0;
9318 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9319 elfsym->sym.st_name);
9320 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9321 ((bfd_byte *) symbuf
9322 + (elfsym->dest_index
9323 * bed->s->sizeof_sym)),
9324 (flinfo->symshndxbuf
9325 + elfsym->destshndx_index));
9328 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9329 pos = hdr->sh_offset + hdr->sh_size;
9330 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9331 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9332 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9334 hdr->sh_size += amt;
9342 free (hash_table->strtab);
9343 hash_table->strtab = NULL;
9348 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9351 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9353 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9354 && sym->st_shndx < SHN_LORESERVE)
9356 /* The gABI doesn't support dynamic symbols in output sections
9359 /* xgettext:c-format */
9360 (_("%B: Too many sections: %d (>= %d)"),
9361 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9362 bfd_set_error (bfd_error_nonrepresentable_section);
9368 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9369 allowing an unsatisfied unversioned symbol in the DSO to match a
9370 versioned symbol that would normally require an explicit version.
9371 We also handle the case that a DSO references a hidden symbol
9372 which may be satisfied by a versioned symbol in another DSO. */
9375 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9376 const struct elf_backend_data *bed,
9377 struct elf_link_hash_entry *h)
9380 struct elf_link_loaded_list *loaded;
9382 if (!is_elf_hash_table (info->hash))
9385 /* Check indirect symbol. */
9386 while (h->root.type == bfd_link_hash_indirect)
9387 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9389 switch (h->root.type)
9395 case bfd_link_hash_undefined:
9396 case bfd_link_hash_undefweak:
9397 abfd = h->root.u.undef.abfd;
9399 || (abfd->flags & DYNAMIC) == 0
9400 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9404 case bfd_link_hash_defined:
9405 case bfd_link_hash_defweak:
9406 abfd = h->root.u.def.section->owner;
9409 case bfd_link_hash_common:
9410 abfd = h->root.u.c.p->section->owner;
9413 BFD_ASSERT (abfd != NULL);
9415 for (loaded = elf_hash_table (info)->loaded;
9417 loaded = loaded->next)
9420 Elf_Internal_Shdr *hdr;
9424 Elf_Internal_Shdr *versymhdr;
9425 Elf_Internal_Sym *isym;
9426 Elf_Internal_Sym *isymend;
9427 Elf_Internal_Sym *isymbuf;
9428 Elf_External_Versym *ever;
9429 Elf_External_Versym *extversym;
9431 input = loaded->abfd;
9433 /* We check each DSO for a possible hidden versioned definition. */
9435 || (input->flags & DYNAMIC) == 0
9436 || elf_dynversym (input) == 0)
9439 hdr = &elf_tdata (input)->dynsymtab_hdr;
9441 symcount = hdr->sh_size / bed->s->sizeof_sym;
9442 if (elf_bad_symtab (input))
9444 extsymcount = symcount;
9449 extsymcount = symcount - hdr->sh_info;
9450 extsymoff = hdr->sh_info;
9453 if (extsymcount == 0)
9456 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9458 if (isymbuf == NULL)
9461 /* Read in any version definitions. */
9462 versymhdr = &elf_tdata (input)->dynversym_hdr;
9463 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9464 if (extversym == NULL)
9467 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9468 || (bfd_bread (extversym, versymhdr->sh_size, input)
9469 != versymhdr->sh_size))
9477 ever = extversym + extsymoff;
9478 isymend = isymbuf + extsymcount;
9479 for (isym = isymbuf; isym < isymend; isym++, ever++)
9482 Elf_Internal_Versym iver;
9483 unsigned short version_index;
9485 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9486 || isym->st_shndx == SHN_UNDEF)
9489 name = bfd_elf_string_from_elf_section (input,
9492 if (strcmp (name, h->root.root.string) != 0)
9495 _bfd_elf_swap_versym_in (input, ever, &iver);
9497 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9499 && h->forced_local))
9501 /* If we have a non-hidden versioned sym, then it should
9502 have provided a definition for the undefined sym unless
9503 it is defined in a non-shared object and forced local.
9508 version_index = iver.vs_vers & VERSYM_VERSION;
9509 if (version_index == 1 || version_index == 2)
9511 /* This is the base or first version. We can use it. */
9525 /* Convert ELF common symbol TYPE. */
9528 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9530 /* Commom symbol can only appear in relocatable link. */
9531 if (!bfd_link_relocatable (info))
9533 switch (info->elf_stt_common)
9537 case elf_stt_common:
9540 case no_elf_stt_common:
9547 /* Add an external symbol to the symbol table. This is called from
9548 the hash table traversal routine. When generating a shared object,
9549 we go through the symbol table twice. The first time we output
9550 anything that might have been forced to local scope in a version
9551 script. The second time we output the symbols that are still
9555 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9557 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9558 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9559 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9561 Elf_Internal_Sym sym;
9562 asection *input_sec;
9563 const struct elf_backend_data *bed;
9568 if (h->root.type == bfd_link_hash_warning)
9570 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9571 if (h->root.type == bfd_link_hash_new)
9575 /* Decide whether to output this symbol in this pass. */
9576 if (eoinfo->localsyms)
9578 if (!h->forced_local)
9583 if (h->forced_local)
9587 bed = get_elf_backend_data (flinfo->output_bfd);
9589 if (h->root.type == bfd_link_hash_undefined)
9591 /* If we have an undefined symbol reference here then it must have
9592 come from a shared library that is being linked in. (Undefined
9593 references in regular files have already been handled unless
9594 they are in unreferenced sections which are removed by garbage
9596 bfd_boolean ignore_undef = FALSE;
9598 /* Some symbols may be special in that the fact that they're
9599 undefined can be safely ignored - let backend determine that. */
9600 if (bed->elf_backend_ignore_undef_symbol)
9601 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9603 /* If we are reporting errors for this situation then do so now. */
9606 && (!h->ref_regular || flinfo->info->gc_sections)
9607 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9608 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9609 (*flinfo->info->callbacks->undefined_symbol)
9610 (flinfo->info, h->root.root.string,
9611 h->ref_regular ? NULL : h->root.u.undef.abfd,
9613 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9615 /* Strip a global symbol defined in a discarded section. */
9620 /* We should also warn if a forced local symbol is referenced from
9621 shared libraries. */
9622 if (bfd_link_executable (flinfo->info)
9627 && h->ref_dynamic_nonweak
9628 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9632 struct elf_link_hash_entry *hi = h;
9634 /* Check indirect symbol. */
9635 while (hi->root.type == bfd_link_hash_indirect)
9636 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9638 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9639 /* xgettext:c-format */
9640 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9641 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9642 /* xgettext:c-format */
9643 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9645 /* xgettext:c-format */
9646 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9647 def_bfd = flinfo->output_bfd;
9648 if (hi->root.u.def.section != bfd_abs_section_ptr)
9649 def_bfd = hi->root.u.def.section->owner;
9650 _bfd_error_handler (msg, flinfo->output_bfd,
9651 h->root.root.string, def_bfd);
9652 bfd_set_error (bfd_error_bad_value);
9653 eoinfo->failed = TRUE;
9657 /* We don't want to output symbols that have never been mentioned by
9658 a regular file, or that we have been told to strip. However, if
9659 h->indx is set to -2, the symbol is used by a reloc and we must
9664 else if ((h->def_dynamic
9666 || h->root.type == bfd_link_hash_new)
9670 else if (flinfo->info->strip == strip_all)
9672 else if (flinfo->info->strip == strip_some
9673 && bfd_hash_lookup (flinfo->info->keep_hash,
9674 h->root.root.string, FALSE, FALSE) == NULL)
9676 else if ((h->root.type == bfd_link_hash_defined
9677 || h->root.type == bfd_link_hash_defweak)
9678 && ((flinfo->info->strip_discarded
9679 && discarded_section (h->root.u.def.section))
9680 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9681 && h->root.u.def.section->owner != NULL
9682 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9684 else if ((h->root.type == bfd_link_hash_undefined
9685 || h->root.type == bfd_link_hash_undefweak)
9686 && h->root.u.undef.abfd != NULL
9687 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9692 /* If we're stripping it, and it's not a dynamic symbol, there's
9693 nothing else to do. However, if it is a forced local symbol or
9694 an ifunc symbol we need to give the backend finish_dynamic_symbol
9695 function a chance to make it dynamic. */
9698 && type != STT_GNU_IFUNC
9699 && !h->forced_local)
9703 sym.st_size = h->size;
9704 sym.st_other = h->other;
9705 switch (h->root.type)
9708 case bfd_link_hash_new:
9709 case bfd_link_hash_warning:
9713 case bfd_link_hash_undefined:
9714 case bfd_link_hash_undefweak:
9715 input_sec = bfd_und_section_ptr;
9716 sym.st_shndx = SHN_UNDEF;
9719 case bfd_link_hash_defined:
9720 case bfd_link_hash_defweak:
9722 input_sec = h->root.u.def.section;
9723 if (input_sec->output_section != NULL)
9726 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9727 input_sec->output_section);
9728 if (sym.st_shndx == SHN_BAD)
9731 /* xgettext:c-format */
9732 (_("%B: could not find output section %A for input section %A"),
9733 flinfo->output_bfd, input_sec->output_section, input_sec);
9734 bfd_set_error (bfd_error_nonrepresentable_section);
9735 eoinfo->failed = TRUE;
9739 /* ELF symbols in relocatable files are section relative,
9740 but in nonrelocatable files they are virtual
9742 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9743 if (!bfd_link_relocatable (flinfo->info))
9745 sym.st_value += input_sec->output_section->vma;
9746 if (h->type == STT_TLS)
9748 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9749 if (tls_sec != NULL)
9750 sym.st_value -= tls_sec->vma;
9756 BFD_ASSERT (input_sec->owner == NULL
9757 || (input_sec->owner->flags & DYNAMIC) != 0);
9758 sym.st_shndx = SHN_UNDEF;
9759 input_sec = bfd_und_section_ptr;
9764 case bfd_link_hash_common:
9765 input_sec = h->root.u.c.p->section;
9766 sym.st_shndx = bed->common_section_index (input_sec);
9767 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9770 case bfd_link_hash_indirect:
9771 /* These symbols are created by symbol versioning. They point
9772 to the decorated version of the name. For example, if the
9773 symbol foo@@GNU_1.2 is the default, which should be used when
9774 foo is used with no version, then we add an indirect symbol
9775 foo which points to foo@@GNU_1.2. We ignore these symbols,
9776 since the indirected symbol is already in the hash table. */
9780 if (type == STT_COMMON || type == STT_OBJECT)
9781 switch (h->root.type)
9783 case bfd_link_hash_common:
9784 type = elf_link_convert_common_type (flinfo->info, type);
9786 case bfd_link_hash_defined:
9787 case bfd_link_hash_defweak:
9788 if (bed->common_definition (&sym))
9789 type = elf_link_convert_common_type (flinfo->info, type);
9793 case bfd_link_hash_undefined:
9794 case bfd_link_hash_undefweak:
9800 if (h->forced_local)
9802 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9803 /* Turn off visibility on local symbol. */
9804 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9806 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9807 else if (h->unique_global && h->def_regular)
9808 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9809 else if (h->root.type == bfd_link_hash_undefweak
9810 || h->root.type == bfd_link_hash_defweak)
9811 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9813 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9814 sym.st_target_internal = h->target_internal;
9816 /* Give the processor backend a chance to tweak the symbol value,
9817 and also to finish up anything that needs to be done for this
9818 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9819 forced local syms when non-shared is due to a historical quirk.
9820 STT_GNU_IFUNC symbol must go through PLT. */
9821 if ((h->type == STT_GNU_IFUNC
9823 && !bfd_link_relocatable (flinfo->info))
9824 || ((h->dynindx != -1
9826 && ((bfd_link_pic (flinfo->info)
9827 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9828 || h->root.type != bfd_link_hash_undefweak))
9829 || !h->forced_local)
9830 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9832 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9833 (flinfo->output_bfd, flinfo->info, h, &sym)))
9835 eoinfo->failed = TRUE;
9840 /* If we are marking the symbol as undefined, and there are no
9841 non-weak references to this symbol from a regular object, then
9842 mark the symbol as weak undefined; if there are non-weak
9843 references, mark the symbol as strong. We can't do this earlier,
9844 because it might not be marked as undefined until the
9845 finish_dynamic_symbol routine gets through with it. */
9846 if (sym.st_shndx == SHN_UNDEF
9848 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9849 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9852 type = ELF_ST_TYPE (sym.st_info);
9854 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9855 if (type == STT_GNU_IFUNC)
9858 if (h->ref_regular_nonweak)
9859 bindtype = STB_GLOBAL;
9861 bindtype = STB_WEAK;
9862 sym.st_info = ELF_ST_INFO (bindtype, type);
9865 /* If this is a symbol defined in a dynamic library, don't use the
9866 symbol size from the dynamic library. Relinking an executable
9867 against a new library may introduce gratuitous changes in the
9868 executable's symbols if we keep the size. */
9869 if (sym.st_shndx == SHN_UNDEF
9874 /* If a non-weak symbol with non-default visibility is not defined
9875 locally, it is a fatal error. */
9876 if (!bfd_link_relocatable (flinfo->info)
9877 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9878 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9879 && h->root.type == bfd_link_hash_undefined
9884 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9885 /* xgettext:c-format */
9886 msg = _("%B: protected symbol `%s' isn't defined");
9887 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9888 /* xgettext:c-format */
9889 msg = _("%B: internal symbol `%s' isn't defined");
9891 /* xgettext:c-format */
9892 msg = _("%B: hidden symbol `%s' isn't defined");
9893 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9894 bfd_set_error (bfd_error_bad_value);
9895 eoinfo->failed = TRUE;
9899 /* If this symbol should be put in the .dynsym section, then put it
9900 there now. We already know the symbol index. We also fill in
9901 the entry in the .hash section. */
9902 if (elf_hash_table (flinfo->info)->dynsym != NULL
9904 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9908 /* Since there is no version information in the dynamic string,
9909 if there is no version info in symbol version section, we will
9910 have a run-time problem if not linking executable, referenced
9911 by shared library, or not bound locally. */
9912 if (h->verinfo.verdef == NULL
9913 && (!bfd_link_executable (flinfo->info)
9915 || !h->def_regular))
9917 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9919 if (p && p [1] != '\0')
9922 /* xgettext:c-format */
9923 (_("%B: No symbol version section for versioned symbol `%s'"),
9924 flinfo->output_bfd, h->root.root.string);
9925 eoinfo->failed = TRUE;
9930 sym.st_name = h->dynstr_index;
9931 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9932 + h->dynindx * bed->s->sizeof_sym);
9933 if (!check_dynsym (flinfo->output_bfd, &sym))
9935 eoinfo->failed = TRUE;
9938 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9940 if (flinfo->hash_sec != NULL)
9942 size_t hash_entry_size;
9943 bfd_byte *bucketpos;
9948 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9949 bucket = h->u.elf_hash_value % bucketcount;
9952 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9953 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9954 + (bucket + 2) * hash_entry_size);
9955 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9956 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9958 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9959 ((bfd_byte *) flinfo->hash_sec->contents
9960 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9963 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9965 Elf_Internal_Versym iversym;
9966 Elf_External_Versym *eversym;
9968 if (!h->def_regular)
9970 if (h->verinfo.verdef == NULL
9971 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9972 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9973 iversym.vs_vers = 0;
9975 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9979 if (h->verinfo.vertree == NULL)
9980 iversym.vs_vers = 1;
9982 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9983 if (flinfo->info->create_default_symver)
9987 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9989 if (h->versioned == versioned_hidden && h->def_regular)
9990 iversym.vs_vers |= VERSYM_HIDDEN;
9992 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9993 eversym += h->dynindx;
9994 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9998 /* If the symbol is undefined, and we didn't output it to .dynsym,
9999 strip it from .symtab too. Obviously we can't do this for
10000 relocatable output or when needed for --emit-relocs. */
10001 else if (input_sec == bfd_und_section_ptr
10003 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10004 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10005 && !bfd_link_relocatable (flinfo->info))
10008 /* Also strip others that we couldn't earlier due to dynamic symbol
10012 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10015 /* Output a FILE symbol so that following locals are not associated
10016 with the wrong input file. We need one for forced local symbols
10017 if we've seen more than one FILE symbol or when we have exactly
10018 one FILE symbol but global symbols are present in a file other
10019 than the one with the FILE symbol. We also need one if linker
10020 defined symbols are present. In practice these conditions are
10021 always met, so just emit the FILE symbol unconditionally. */
10022 if (eoinfo->localsyms
10023 && !eoinfo->file_sym_done
10024 && eoinfo->flinfo->filesym_count != 0)
10026 Elf_Internal_Sym fsym;
10028 memset (&fsym, 0, sizeof (fsym));
10029 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10030 fsym.st_shndx = SHN_ABS;
10031 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10032 bfd_und_section_ptr, NULL))
10035 eoinfo->file_sym_done = TRUE;
10038 indx = bfd_get_symcount (flinfo->output_bfd);
10039 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10043 eoinfo->failed = TRUE;
10048 else if (h->indx == -2)
10054 /* Return TRUE if special handling is done for relocs in SEC against
10055 symbols defined in discarded sections. */
10058 elf_section_ignore_discarded_relocs (asection *sec)
10060 const struct elf_backend_data *bed;
10062 switch (sec->sec_info_type)
10064 case SEC_INFO_TYPE_STABS:
10065 case SEC_INFO_TYPE_EH_FRAME:
10066 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10072 bed = get_elf_backend_data (sec->owner);
10073 if (bed->elf_backend_ignore_discarded_relocs != NULL
10074 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10080 /* Return a mask saying how ld should treat relocations in SEC against
10081 symbols defined in discarded sections. If this function returns
10082 COMPLAIN set, ld will issue a warning message. If this function
10083 returns PRETEND set, and the discarded section was link-once and the
10084 same size as the kept link-once section, ld will pretend that the
10085 symbol was actually defined in the kept section. Otherwise ld will
10086 zero the reloc (at least that is the intent, but some cooperation by
10087 the target dependent code is needed, particularly for REL targets). */
10090 _bfd_elf_default_action_discarded (asection *sec)
10092 if (sec->flags & SEC_DEBUGGING)
10095 if (strcmp (".eh_frame", sec->name) == 0)
10098 if (strcmp (".gcc_except_table", sec->name) == 0)
10101 return COMPLAIN | PRETEND;
10104 /* Find a match between a section and a member of a section group. */
10107 match_group_member (asection *sec, asection *group,
10108 struct bfd_link_info *info)
10110 asection *first = elf_next_in_group (group);
10111 asection *s = first;
10115 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10118 s = elf_next_in_group (s);
10126 /* Check if the kept section of a discarded section SEC can be used
10127 to replace it. Return the replacement if it is OK. Otherwise return
10131 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10135 kept = sec->kept_section;
10138 if ((kept->flags & SEC_GROUP) != 0)
10139 kept = match_group_member (sec, kept, info);
10141 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10142 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10144 sec->kept_section = kept;
10149 /* Link an input file into the linker output file. This function
10150 handles all the sections and relocations of the input file at once.
10151 This is so that we only have to read the local symbols once, and
10152 don't have to keep them in memory. */
10155 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10157 int (*relocate_section)
10158 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10159 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10161 Elf_Internal_Shdr *symtab_hdr;
10162 size_t locsymcount;
10164 Elf_Internal_Sym *isymbuf;
10165 Elf_Internal_Sym *isym;
10166 Elf_Internal_Sym *isymend;
10168 asection **ppsection;
10170 const struct elf_backend_data *bed;
10171 struct elf_link_hash_entry **sym_hashes;
10172 bfd_size_type address_size;
10173 bfd_vma r_type_mask;
10175 bfd_boolean have_file_sym = FALSE;
10177 output_bfd = flinfo->output_bfd;
10178 bed = get_elf_backend_data (output_bfd);
10179 relocate_section = bed->elf_backend_relocate_section;
10181 /* If this is a dynamic object, we don't want to do anything here:
10182 we don't want the local symbols, and we don't want the section
10184 if ((input_bfd->flags & DYNAMIC) != 0)
10187 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10188 if (elf_bad_symtab (input_bfd))
10190 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10195 locsymcount = symtab_hdr->sh_info;
10196 extsymoff = symtab_hdr->sh_info;
10199 /* Read the local symbols. */
10200 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10201 if (isymbuf == NULL && locsymcount != 0)
10203 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10204 flinfo->internal_syms,
10205 flinfo->external_syms,
10206 flinfo->locsym_shndx);
10207 if (isymbuf == NULL)
10211 /* Find local symbol sections and adjust values of symbols in
10212 SEC_MERGE sections. Write out those local symbols we know are
10213 going into the output file. */
10214 isymend = isymbuf + locsymcount;
10215 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10217 isym++, pindex++, ppsection++)
10221 Elf_Internal_Sym osym;
10227 if (elf_bad_symtab (input_bfd))
10229 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10236 if (isym->st_shndx == SHN_UNDEF)
10237 isec = bfd_und_section_ptr;
10238 else if (isym->st_shndx == SHN_ABS)
10239 isec = bfd_abs_section_ptr;
10240 else if (isym->st_shndx == SHN_COMMON)
10241 isec = bfd_com_section_ptr;
10244 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10247 /* Don't attempt to output symbols with st_shnx in the
10248 reserved range other than SHN_ABS and SHN_COMMON. */
10252 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10253 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10255 _bfd_merged_section_offset (output_bfd, &isec,
10256 elf_section_data (isec)->sec_info,
10262 /* Don't output the first, undefined, symbol. In fact, don't
10263 output any undefined local symbol. */
10264 if (isec == bfd_und_section_ptr)
10267 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10269 /* We never output section symbols. Instead, we use the
10270 section symbol of the corresponding section in the output
10275 /* If we are stripping all symbols, we don't want to output this
10277 if (flinfo->info->strip == strip_all)
10280 /* If we are discarding all local symbols, we don't want to
10281 output this one. If we are generating a relocatable output
10282 file, then some of the local symbols may be required by
10283 relocs; we output them below as we discover that they are
10285 if (flinfo->info->discard == discard_all)
10288 /* If this symbol is defined in a section which we are
10289 discarding, we don't need to keep it. */
10290 if (isym->st_shndx != SHN_UNDEF
10291 && isym->st_shndx < SHN_LORESERVE
10292 && bfd_section_removed_from_list (output_bfd,
10293 isec->output_section))
10296 /* Get the name of the symbol. */
10297 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10302 /* See if we are discarding symbols with this name. */
10303 if ((flinfo->info->strip == strip_some
10304 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10306 || (((flinfo->info->discard == discard_sec_merge
10307 && (isec->flags & SEC_MERGE)
10308 && !bfd_link_relocatable (flinfo->info))
10309 || flinfo->info->discard == discard_l)
10310 && bfd_is_local_label_name (input_bfd, name)))
10313 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10315 if (input_bfd->lto_output)
10316 /* -flto puts a temp file name here. This means builds
10317 are not reproducible. Discard the symbol. */
10319 have_file_sym = TRUE;
10320 flinfo->filesym_count += 1;
10322 if (!have_file_sym)
10324 /* In the absence of debug info, bfd_find_nearest_line uses
10325 FILE symbols to determine the source file for local
10326 function symbols. Provide a FILE symbol here if input
10327 files lack such, so that their symbols won't be
10328 associated with a previous input file. It's not the
10329 source file, but the best we can do. */
10330 have_file_sym = TRUE;
10331 flinfo->filesym_count += 1;
10332 memset (&osym, 0, sizeof (osym));
10333 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10334 osym.st_shndx = SHN_ABS;
10335 if (!elf_link_output_symstrtab (flinfo,
10336 (input_bfd->lto_output ? NULL
10337 : input_bfd->filename),
10338 &osym, bfd_abs_section_ptr,
10345 /* Adjust the section index for the output file. */
10346 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10347 isec->output_section);
10348 if (osym.st_shndx == SHN_BAD)
10351 /* ELF symbols in relocatable files are section relative, but
10352 in executable files they are virtual addresses. Note that
10353 this code assumes that all ELF sections have an associated
10354 BFD section with a reasonable value for output_offset; below
10355 we assume that they also have a reasonable value for
10356 output_section. Any special sections must be set up to meet
10357 these requirements. */
10358 osym.st_value += isec->output_offset;
10359 if (!bfd_link_relocatable (flinfo->info))
10361 osym.st_value += isec->output_section->vma;
10362 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10364 /* STT_TLS symbols are relative to PT_TLS segment base. */
10365 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10366 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10370 indx = bfd_get_symcount (output_bfd);
10371 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10378 if (bed->s->arch_size == 32)
10380 r_type_mask = 0xff;
10386 r_type_mask = 0xffffffff;
10391 /* Relocate the contents of each section. */
10392 sym_hashes = elf_sym_hashes (input_bfd);
10393 for (o = input_bfd->sections; o != NULL; o = o->next)
10395 bfd_byte *contents;
10397 if (! o->linker_mark)
10399 /* This section was omitted from the link. */
10403 if (!flinfo->info->resolve_section_groups
10404 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10406 /* Deal with the group signature symbol. */
10407 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10408 unsigned long symndx = sec_data->this_hdr.sh_info;
10409 asection *osec = o->output_section;
10411 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10412 if (symndx >= locsymcount
10413 || (elf_bad_symtab (input_bfd)
10414 && flinfo->sections[symndx] == NULL))
10416 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10417 while (h->root.type == bfd_link_hash_indirect
10418 || h->root.type == bfd_link_hash_warning)
10419 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10420 /* Arrange for symbol to be output. */
10422 elf_section_data (osec)->this_hdr.sh_info = -2;
10424 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10426 /* We'll use the output section target_index. */
10427 asection *sec = flinfo->sections[symndx]->output_section;
10428 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10432 if (flinfo->indices[symndx] == -1)
10434 /* Otherwise output the local symbol now. */
10435 Elf_Internal_Sym sym = isymbuf[symndx];
10436 asection *sec = flinfo->sections[symndx]->output_section;
10441 name = bfd_elf_string_from_elf_section (input_bfd,
10442 symtab_hdr->sh_link,
10447 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10449 if (sym.st_shndx == SHN_BAD)
10452 sym.st_value += o->output_offset;
10454 indx = bfd_get_symcount (output_bfd);
10455 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10460 flinfo->indices[symndx] = indx;
10464 elf_section_data (osec)->this_hdr.sh_info
10465 = flinfo->indices[symndx];
10469 if ((o->flags & SEC_HAS_CONTENTS) == 0
10470 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10473 if ((o->flags & SEC_LINKER_CREATED) != 0)
10475 /* Section was created by _bfd_elf_link_create_dynamic_sections
10480 /* Get the contents of the section. They have been cached by a
10481 relaxation routine. Note that o is a section in an input
10482 file, so the contents field will not have been set by any of
10483 the routines which work on output files. */
10484 if (elf_section_data (o)->this_hdr.contents != NULL)
10486 contents = elf_section_data (o)->this_hdr.contents;
10487 if (bed->caches_rawsize
10489 && o->rawsize < o->size)
10491 memcpy (flinfo->contents, contents, o->rawsize);
10492 contents = flinfo->contents;
10497 contents = flinfo->contents;
10498 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10502 if ((o->flags & SEC_RELOC) != 0)
10504 Elf_Internal_Rela *internal_relocs;
10505 Elf_Internal_Rela *rel, *relend;
10506 int action_discarded;
10509 /* Get the swapped relocs. */
10511 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10512 flinfo->internal_relocs, FALSE);
10513 if (internal_relocs == NULL
10514 && o->reloc_count > 0)
10517 /* We need to reverse-copy input .ctors/.dtors sections if
10518 they are placed in .init_array/.finit_array for output. */
10519 if (o->size > address_size
10520 && ((strncmp (o->name, ".ctors", 6) == 0
10521 && strcmp (o->output_section->name,
10522 ".init_array") == 0)
10523 || (strncmp (o->name, ".dtors", 6) == 0
10524 && strcmp (o->output_section->name,
10525 ".fini_array") == 0))
10526 && (o->name[6] == 0 || o->name[6] == '.'))
10528 if (o->size * bed->s->int_rels_per_ext_rel
10529 != o->reloc_count * address_size)
10532 /* xgettext:c-format */
10533 (_("error: %B: size of section %A is not "
10534 "multiple of address size"),
10536 bfd_set_error (bfd_error_bad_value);
10539 o->flags |= SEC_ELF_REVERSE_COPY;
10542 action_discarded = -1;
10543 if (!elf_section_ignore_discarded_relocs (o))
10544 action_discarded = (*bed->action_discarded) (o);
10546 /* Run through the relocs evaluating complex reloc symbols and
10547 looking for relocs against symbols from discarded sections
10548 or section symbols from removed link-once sections.
10549 Complain about relocs against discarded sections. Zero
10550 relocs against removed link-once sections. */
10552 rel = internal_relocs;
10553 relend = rel + o->reloc_count;
10554 for ( ; rel < relend; rel++)
10556 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10557 unsigned int s_type;
10558 asection **ps, *sec;
10559 struct elf_link_hash_entry *h = NULL;
10560 const char *sym_name;
10562 if (r_symndx == STN_UNDEF)
10565 if (r_symndx >= locsymcount
10566 || (elf_bad_symtab (input_bfd)
10567 && flinfo->sections[r_symndx] == NULL))
10569 h = sym_hashes[r_symndx - extsymoff];
10571 /* Badly formatted input files can contain relocs that
10572 reference non-existant symbols. Check here so that
10573 we do not seg fault. */
10577 /* xgettext:c-format */
10578 (_("error: %B contains a reloc (%#Lx) for section %A "
10579 "that references a non-existent global symbol"),
10580 input_bfd, rel->r_info, o);
10581 bfd_set_error (bfd_error_bad_value);
10585 while (h->root.type == bfd_link_hash_indirect
10586 || h->root.type == bfd_link_hash_warning)
10587 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10591 /* If a plugin symbol is referenced from a non-IR file,
10592 mark the symbol as undefined. Note that the
10593 linker may attach linker created dynamic sections
10594 to the plugin bfd. Symbols defined in linker
10595 created sections are not plugin symbols. */
10596 if ((h->root.non_ir_ref_regular
10597 || h->root.non_ir_ref_dynamic)
10598 && (h->root.type == bfd_link_hash_defined
10599 || h->root.type == bfd_link_hash_defweak)
10600 && (h->root.u.def.section->flags
10601 & SEC_LINKER_CREATED) == 0
10602 && h->root.u.def.section->owner != NULL
10603 && (h->root.u.def.section->owner->flags
10604 & BFD_PLUGIN) != 0)
10606 h->root.type = bfd_link_hash_undefined;
10607 h->root.u.undef.abfd = h->root.u.def.section->owner;
10611 if (h->root.type == bfd_link_hash_defined
10612 || h->root.type == bfd_link_hash_defweak)
10613 ps = &h->root.u.def.section;
10615 sym_name = h->root.root.string;
10619 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10621 s_type = ELF_ST_TYPE (sym->st_info);
10622 ps = &flinfo->sections[r_symndx];
10623 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10627 if ((s_type == STT_RELC || s_type == STT_SRELC)
10628 && !bfd_link_relocatable (flinfo->info))
10631 bfd_vma dot = (rel->r_offset
10632 + o->output_offset + o->output_section->vma);
10634 printf ("Encountered a complex symbol!");
10635 printf (" (input_bfd %s, section %s, reloc %ld\n",
10636 input_bfd->filename, o->name,
10637 (long) (rel - internal_relocs));
10638 printf (" symbol: idx %8.8lx, name %s\n",
10639 r_symndx, sym_name);
10640 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10641 (unsigned long) rel->r_info,
10642 (unsigned long) rel->r_offset);
10644 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10645 isymbuf, locsymcount, s_type == STT_SRELC))
10648 /* Symbol evaluated OK. Update to absolute value. */
10649 set_symbol_value (input_bfd, isymbuf, locsymcount,
10654 if (action_discarded != -1 && ps != NULL)
10656 /* Complain if the definition comes from a
10657 discarded section. */
10658 if ((sec = *ps) != NULL && discarded_section (sec))
10660 BFD_ASSERT (r_symndx != STN_UNDEF);
10661 if (action_discarded & COMPLAIN)
10662 (*flinfo->info->callbacks->einfo)
10663 /* xgettext:c-format */
10664 (_("%X`%s' referenced in section `%A' of %B: "
10665 "defined in discarded section `%A' of %B\n"),
10666 sym_name, o, input_bfd, sec, sec->owner);
10668 /* Try to do the best we can to support buggy old
10669 versions of gcc. Pretend that the symbol is
10670 really defined in the kept linkonce section.
10671 FIXME: This is quite broken. Modifying the
10672 symbol here means we will be changing all later
10673 uses of the symbol, not just in this section. */
10674 if (action_discarded & PRETEND)
10678 kept = _bfd_elf_check_kept_section (sec,
10690 /* Relocate the section by invoking a back end routine.
10692 The back end routine is responsible for adjusting the
10693 section contents as necessary, and (if using Rela relocs
10694 and generating a relocatable output file) adjusting the
10695 reloc addend as necessary.
10697 The back end routine does not have to worry about setting
10698 the reloc address or the reloc symbol index.
10700 The back end routine is given a pointer to the swapped in
10701 internal symbols, and can access the hash table entries
10702 for the external symbols via elf_sym_hashes (input_bfd).
10704 When generating relocatable output, the back end routine
10705 must handle STB_LOCAL/STT_SECTION symbols specially. The
10706 output symbol is going to be a section symbol
10707 corresponding to the output section, which will require
10708 the addend to be adjusted. */
10710 ret = (*relocate_section) (output_bfd, flinfo->info,
10711 input_bfd, o, contents,
10719 || bfd_link_relocatable (flinfo->info)
10720 || flinfo->info->emitrelocations)
10722 Elf_Internal_Rela *irela;
10723 Elf_Internal_Rela *irelaend, *irelamid;
10724 bfd_vma last_offset;
10725 struct elf_link_hash_entry **rel_hash;
10726 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10727 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10728 unsigned int next_erel;
10729 bfd_boolean rela_normal;
10730 struct bfd_elf_section_data *esdi, *esdo;
10732 esdi = elf_section_data (o);
10733 esdo = elf_section_data (o->output_section);
10734 rela_normal = FALSE;
10736 /* Adjust the reloc addresses and symbol indices. */
10738 irela = internal_relocs;
10739 irelaend = irela + o->reloc_count;
10740 rel_hash = esdo->rel.hashes + esdo->rel.count;
10741 /* We start processing the REL relocs, if any. When we reach
10742 IRELAMID in the loop, we switch to the RELA relocs. */
10744 if (esdi->rel.hdr != NULL)
10745 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10746 * bed->s->int_rels_per_ext_rel);
10747 rel_hash_list = rel_hash;
10748 rela_hash_list = NULL;
10749 last_offset = o->output_offset;
10750 if (!bfd_link_relocatable (flinfo->info))
10751 last_offset += o->output_section->vma;
10752 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10754 unsigned long r_symndx;
10756 Elf_Internal_Sym sym;
10758 if (next_erel == bed->s->int_rels_per_ext_rel)
10764 if (irela == irelamid)
10766 rel_hash = esdo->rela.hashes + esdo->rela.count;
10767 rela_hash_list = rel_hash;
10768 rela_normal = bed->rela_normal;
10771 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10774 if (irela->r_offset >= (bfd_vma) -2)
10776 /* This is a reloc for a deleted entry or somesuch.
10777 Turn it into an R_*_NONE reloc, at the same
10778 offset as the last reloc. elf_eh_frame.c and
10779 bfd_elf_discard_info rely on reloc offsets
10781 irela->r_offset = last_offset;
10783 irela->r_addend = 0;
10787 irela->r_offset += o->output_offset;
10789 /* Relocs in an executable have to be virtual addresses. */
10790 if (!bfd_link_relocatable (flinfo->info))
10791 irela->r_offset += o->output_section->vma;
10793 last_offset = irela->r_offset;
10795 r_symndx = irela->r_info >> r_sym_shift;
10796 if (r_symndx == STN_UNDEF)
10799 if (r_symndx >= locsymcount
10800 || (elf_bad_symtab (input_bfd)
10801 && flinfo->sections[r_symndx] == NULL))
10803 struct elf_link_hash_entry *rh;
10804 unsigned long indx;
10806 /* This is a reloc against a global symbol. We
10807 have not yet output all the local symbols, so
10808 we do not know the symbol index of any global
10809 symbol. We set the rel_hash entry for this
10810 reloc to point to the global hash table entry
10811 for this symbol. The symbol index is then
10812 set at the end of bfd_elf_final_link. */
10813 indx = r_symndx - extsymoff;
10814 rh = elf_sym_hashes (input_bfd)[indx];
10815 while (rh->root.type == bfd_link_hash_indirect
10816 || rh->root.type == bfd_link_hash_warning)
10817 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10819 /* Setting the index to -2 tells
10820 elf_link_output_extsym that this symbol is
10821 used by a reloc. */
10822 BFD_ASSERT (rh->indx < 0);
10829 /* This is a reloc against a local symbol. */
10832 sym = isymbuf[r_symndx];
10833 sec = flinfo->sections[r_symndx];
10834 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10836 /* I suppose the backend ought to fill in the
10837 section of any STT_SECTION symbol against a
10838 processor specific section. */
10839 r_symndx = STN_UNDEF;
10840 if (bfd_is_abs_section (sec))
10842 else if (sec == NULL || sec->owner == NULL)
10844 bfd_set_error (bfd_error_bad_value);
10849 asection *osec = sec->output_section;
10851 /* If we have discarded a section, the output
10852 section will be the absolute section. In
10853 case of discarded SEC_MERGE sections, use
10854 the kept section. relocate_section should
10855 have already handled discarded linkonce
10857 if (bfd_is_abs_section (osec)
10858 && sec->kept_section != NULL
10859 && sec->kept_section->output_section != NULL)
10861 osec = sec->kept_section->output_section;
10862 irela->r_addend -= osec->vma;
10865 if (!bfd_is_abs_section (osec))
10867 r_symndx = osec->target_index;
10868 if (r_symndx == STN_UNDEF)
10870 irela->r_addend += osec->vma;
10871 osec = _bfd_nearby_section (output_bfd, osec,
10873 irela->r_addend -= osec->vma;
10874 r_symndx = osec->target_index;
10879 /* Adjust the addend according to where the
10880 section winds up in the output section. */
10882 irela->r_addend += sec->output_offset;
10886 if (flinfo->indices[r_symndx] == -1)
10888 unsigned long shlink;
10893 if (flinfo->info->strip == strip_all)
10895 /* You can't do ld -r -s. */
10896 bfd_set_error (bfd_error_invalid_operation);
10900 /* This symbol was skipped earlier, but
10901 since it is needed by a reloc, we
10902 must output it now. */
10903 shlink = symtab_hdr->sh_link;
10904 name = (bfd_elf_string_from_elf_section
10905 (input_bfd, shlink, sym.st_name));
10909 osec = sec->output_section;
10911 _bfd_elf_section_from_bfd_section (output_bfd,
10913 if (sym.st_shndx == SHN_BAD)
10916 sym.st_value += sec->output_offset;
10917 if (!bfd_link_relocatable (flinfo->info))
10919 sym.st_value += osec->vma;
10920 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10922 /* STT_TLS symbols are relative to PT_TLS
10924 BFD_ASSERT (elf_hash_table (flinfo->info)
10925 ->tls_sec != NULL);
10926 sym.st_value -= (elf_hash_table (flinfo->info)
10931 indx = bfd_get_symcount (output_bfd);
10932 ret = elf_link_output_symstrtab (flinfo, name,
10938 flinfo->indices[r_symndx] = indx;
10943 r_symndx = flinfo->indices[r_symndx];
10946 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10947 | (irela->r_info & r_type_mask));
10950 /* Swap out the relocs. */
10951 input_rel_hdr = esdi->rel.hdr;
10952 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10954 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10959 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10960 * bed->s->int_rels_per_ext_rel);
10961 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10964 input_rela_hdr = esdi->rela.hdr;
10965 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10967 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10976 /* Write out the modified section contents. */
10977 if (bed->elf_backend_write_section
10978 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10981 /* Section written out. */
10983 else switch (o->sec_info_type)
10985 case SEC_INFO_TYPE_STABS:
10986 if (! (_bfd_write_section_stabs
10988 &elf_hash_table (flinfo->info)->stab_info,
10989 o, &elf_section_data (o)->sec_info, contents)))
10992 case SEC_INFO_TYPE_MERGE:
10993 if (! _bfd_write_merged_section (output_bfd, o,
10994 elf_section_data (o)->sec_info))
10997 case SEC_INFO_TYPE_EH_FRAME:
10999 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11004 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11006 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11014 if (! (o->flags & SEC_EXCLUDE))
11016 file_ptr offset = (file_ptr) o->output_offset;
11017 bfd_size_type todo = o->size;
11019 offset *= bfd_octets_per_byte (output_bfd);
11021 if ((o->flags & SEC_ELF_REVERSE_COPY))
11023 /* Reverse-copy input section to output. */
11026 todo -= address_size;
11027 if (! bfd_set_section_contents (output_bfd,
11035 offset += address_size;
11039 else if (! bfd_set_section_contents (output_bfd,
11053 /* Generate a reloc when linking an ELF file. This is a reloc
11054 requested by the linker, and does not come from any input file. This
11055 is used to build constructor and destructor tables when linking
11059 elf_reloc_link_order (bfd *output_bfd,
11060 struct bfd_link_info *info,
11061 asection *output_section,
11062 struct bfd_link_order *link_order)
11064 reloc_howto_type *howto;
11068 struct bfd_elf_section_reloc_data *reldata;
11069 struct elf_link_hash_entry **rel_hash_ptr;
11070 Elf_Internal_Shdr *rel_hdr;
11071 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11072 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11075 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11077 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11080 bfd_set_error (bfd_error_bad_value);
11084 addend = link_order->u.reloc.p->addend;
11087 reldata = &esdo->rel;
11088 else if (esdo->rela.hdr)
11089 reldata = &esdo->rela;
11096 /* Figure out the symbol index. */
11097 rel_hash_ptr = reldata->hashes + reldata->count;
11098 if (link_order->type == bfd_section_reloc_link_order)
11100 indx = link_order->u.reloc.p->u.section->target_index;
11101 BFD_ASSERT (indx != 0);
11102 *rel_hash_ptr = NULL;
11106 struct elf_link_hash_entry *h;
11108 /* Treat a reloc against a defined symbol as though it were
11109 actually against the section. */
11110 h = ((struct elf_link_hash_entry *)
11111 bfd_wrapped_link_hash_lookup (output_bfd, info,
11112 link_order->u.reloc.p->u.name,
11113 FALSE, FALSE, TRUE));
11115 && (h->root.type == bfd_link_hash_defined
11116 || h->root.type == bfd_link_hash_defweak))
11120 section = h->root.u.def.section;
11121 indx = section->output_section->target_index;
11122 *rel_hash_ptr = NULL;
11123 /* It seems that we ought to add the symbol value to the
11124 addend here, but in practice it has already been added
11125 because it was passed to constructor_callback. */
11126 addend += section->output_section->vma + section->output_offset;
11128 else if (h != NULL)
11130 /* Setting the index to -2 tells elf_link_output_extsym that
11131 this symbol is used by a reloc. */
11138 (*info->callbacks->unattached_reloc)
11139 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11144 /* If this is an inplace reloc, we must write the addend into the
11146 if (howto->partial_inplace && addend != 0)
11148 bfd_size_type size;
11149 bfd_reloc_status_type rstat;
11152 const char *sym_name;
11154 size = (bfd_size_type) bfd_get_reloc_size (howto);
11155 buf = (bfd_byte *) bfd_zmalloc (size);
11156 if (buf == NULL && size != 0)
11158 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11165 case bfd_reloc_outofrange:
11168 case bfd_reloc_overflow:
11169 if (link_order->type == bfd_section_reloc_link_order)
11170 sym_name = bfd_section_name (output_bfd,
11171 link_order->u.reloc.p->u.section);
11173 sym_name = link_order->u.reloc.p->u.name;
11174 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11175 howto->name, addend, NULL, NULL,
11180 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11182 * bfd_octets_per_byte (output_bfd),
11189 /* The address of a reloc is relative to the section in a
11190 relocatable file, and is a virtual address in an executable
11192 offset = link_order->offset;
11193 if (! bfd_link_relocatable (info))
11194 offset += output_section->vma;
11196 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11198 irel[i].r_offset = offset;
11199 irel[i].r_info = 0;
11200 irel[i].r_addend = 0;
11202 if (bed->s->arch_size == 32)
11203 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11205 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11207 rel_hdr = reldata->hdr;
11208 erel = rel_hdr->contents;
11209 if (rel_hdr->sh_type == SHT_REL)
11211 erel += reldata->count * bed->s->sizeof_rel;
11212 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11216 irel[0].r_addend = addend;
11217 erel += reldata->count * bed->s->sizeof_rela;
11218 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11227 /* Get the output vma of the section pointed to by the sh_link field. */
11230 elf_get_linked_section_vma (struct bfd_link_order *p)
11232 Elf_Internal_Shdr **elf_shdrp;
11236 s = p->u.indirect.section;
11237 elf_shdrp = elf_elfsections (s->owner);
11238 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11239 elfsec = elf_shdrp[elfsec]->sh_link;
11241 The Intel C compiler generates SHT_IA_64_UNWIND with
11242 SHF_LINK_ORDER. But it doesn't set the sh_link or
11243 sh_info fields. Hence we could get the situation
11244 where elfsec is 0. */
11247 const struct elf_backend_data *bed
11248 = get_elf_backend_data (s->owner);
11249 if (bed->link_order_error_handler)
11250 bed->link_order_error_handler
11251 /* xgettext:c-format */
11252 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11257 s = elf_shdrp[elfsec]->bfd_section;
11258 return s->output_section->vma + s->output_offset;
11263 /* Compare two sections based on the locations of the sections they are
11264 linked to. Used by elf_fixup_link_order. */
11267 compare_link_order (const void * a, const void * b)
11272 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11273 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11276 return apos > bpos;
11280 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11281 order as their linked sections. Returns false if this could not be done
11282 because an output section includes both ordered and unordered
11283 sections. Ideally we'd do this in the linker proper. */
11286 elf_fixup_link_order (bfd *abfd, asection *o)
11288 int seen_linkorder;
11291 struct bfd_link_order *p;
11293 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11295 struct bfd_link_order **sections;
11296 asection *s, *other_sec, *linkorder_sec;
11300 linkorder_sec = NULL;
11302 seen_linkorder = 0;
11303 for (p = o->map_head.link_order; p != NULL; p = p->next)
11305 if (p->type == bfd_indirect_link_order)
11307 s = p->u.indirect.section;
11309 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11310 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11311 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11312 && elfsec < elf_numsections (sub)
11313 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11314 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11328 if (seen_other && seen_linkorder)
11330 if (other_sec && linkorder_sec)
11332 /* xgettext:c-format */
11333 (_("%A has both ordered [`%A' in %B] "
11334 "and unordered [`%A' in %B] sections"),
11335 o, linkorder_sec, linkorder_sec->owner,
11336 other_sec, other_sec->owner);
11339 (_("%A has both ordered and unordered sections"), o);
11340 bfd_set_error (bfd_error_bad_value);
11345 if (!seen_linkorder)
11348 sections = (struct bfd_link_order **)
11349 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11350 if (sections == NULL)
11352 seen_linkorder = 0;
11354 for (p = o->map_head.link_order; p != NULL; p = p->next)
11356 sections[seen_linkorder++] = p;
11358 /* Sort the input sections in the order of their linked section. */
11359 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11360 compare_link_order);
11362 /* Change the offsets of the sections. */
11364 for (n = 0; n < seen_linkorder; n++)
11366 s = sections[n]->u.indirect.section;
11367 offset &= ~(bfd_vma) 0 << s->alignment_power;
11368 s->output_offset = offset / bfd_octets_per_byte (abfd);
11369 sections[n]->offset = offset;
11370 offset += sections[n]->size;
11377 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11378 Returns TRUE upon success, FALSE otherwise. */
11381 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11383 bfd_boolean ret = FALSE;
11385 const struct elf_backend_data *bed;
11387 enum bfd_architecture arch;
11389 asymbol **sympp = NULL;
11393 elf_symbol_type *osymbuf;
11395 implib_bfd = info->out_implib_bfd;
11396 bed = get_elf_backend_data (abfd);
11398 if (!bfd_set_format (implib_bfd, bfd_object))
11401 /* Use flag from executable but make it a relocatable object. */
11402 flags = bfd_get_file_flags (abfd);
11403 flags &= ~HAS_RELOC;
11404 if (!bfd_set_start_address (implib_bfd, 0)
11405 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11408 /* Copy architecture of output file to import library file. */
11409 arch = bfd_get_arch (abfd);
11410 mach = bfd_get_mach (abfd);
11411 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11412 && (abfd->target_defaulted
11413 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11416 /* Get symbol table size. */
11417 symsize = bfd_get_symtab_upper_bound (abfd);
11421 /* Read in the symbol table. */
11422 sympp = (asymbol **) xmalloc (symsize);
11423 symcount = bfd_canonicalize_symtab (abfd, sympp);
11427 /* Allow the BFD backend to copy any private header data it
11428 understands from the output BFD to the import library BFD. */
11429 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11432 /* Filter symbols to appear in the import library. */
11433 if (bed->elf_backend_filter_implib_symbols)
11434 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11437 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11440 bfd_set_error (bfd_error_no_symbols);
11441 _bfd_error_handler (_("%B: no symbol found for import library"),
11447 /* Make symbols absolute. */
11448 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11449 sizeof (*osymbuf));
11450 for (src_count = 0; src_count < symcount; src_count++)
11452 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11453 sizeof (*osymbuf));
11454 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11455 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11456 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11457 osymbuf[src_count].internal_elf_sym.st_value =
11458 osymbuf[src_count].symbol.value;
11459 sympp[src_count] = &osymbuf[src_count].symbol;
11462 bfd_set_symtab (implib_bfd, sympp, symcount);
11464 /* Allow the BFD backend to copy any private data it understands
11465 from the output BFD to the import library BFD. This is done last
11466 to permit the routine to look at the filtered symbol table. */
11467 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11470 if (!bfd_close (implib_bfd))
11481 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11485 if (flinfo->symstrtab != NULL)
11486 _bfd_elf_strtab_free (flinfo->symstrtab);
11487 if (flinfo->contents != NULL)
11488 free (flinfo->contents);
11489 if (flinfo->external_relocs != NULL)
11490 free (flinfo->external_relocs);
11491 if (flinfo->internal_relocs != NULL)
11492 free (flinfo->internal_relocs);
11493 if (flinfo->external_syms != NULL)
11494 free (flinfo->external_syms);
11495 if (flinfo->locsym_shndx != NULL)
11496 free (flinfo->locsym_shndx);
11497 if (flinfo->internal_syms != NULL)
11498 free (flinfo->internal_syms);
11499 if (flinfo->indices != NULL)
11500 free (flinfo->indices);
11501 if (flinfo->sections != NULL)
11502 free (flinfo->sections);
11503 if (flinfo->symshndxbuf != NULL)
11504 free (flinfo->symshndxbuf);
11505 for (o = obfd->sections; o != NULL; o = o->next)
11507 struct bfd_elf_section_data *esdo = elf_section_data (o);
11508 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11509 free (esdo->rel.hashes);
11510 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11511 free (esdo->rela.hashes);
11515 /* Do the final step of an ELF link. */
11518 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11520 bfd_boolean dynamic;
11521 bfd_boolean emit_relocs;
11523 struct elf_final_link_info flinfo;
11525 struct bfd_link_order *p;
11527 bfd_size_type max_contents_size;
11528 bfd_size_type max_external_reloc_size;
11529 bfd_size_type max_internal_reloc_count;
11530 bfd_size_type max_sym_count;
11531 bfd_size_type max_sym_shndx_count;
11532 Elf_Internal_Sym elfsym;
11534 Elf_Internal_Shdr *symtab_hdr;
11535 Elf_Internal_Shdr *symtab_shndx_hdr;
11536 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11537 struct elf_outext_info eoinfo;
11538 bfd_boolean merged;
11539 size_t relativecount = 0;
11540 asection *reldyn = 0;
11542 asection *attr_section = NULL;
11543 bfd_vma attr_size = 0;
11544 const char *std_attrs_section;
11545 struct elf_link_hash_table *htab = elf_hash_table (info);
11547 if (!is_elf_hash_table (htab))
11550 if (bfd_link_pic (info))
11551 abfd->flags |= DYNAMIC;
11553 dynamic = htab->dynamic_sections_created;
11554 dynobj = htab->dynobj;
11556 emit_relocs = (bfd_link_relocatable (info)
11557 || info->emitrelocations);
11559 flinfo.info = info;
11560 flinfo.output_bfd = abfd;
11561 flinfo.symstrtab = _bfd_elf_strtab_init ();
11562 if (flinfo.symstrtab == NULL)
11567 flinfo.hash_sec = NULL;
11568 flinfo.symver_sec = NULL;
11572 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11573 /* Note that dynsym_sec can be NULL (on VMS). */
11574 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11575 /* Note that it is OK if symver_sec is NULL. */
11578 flinfo.contents = NULL;
11579 flinfo.external_relocs = NULL;
11580 flinfo.internal_relocs = NULL;
11581 flinfo.external_syms = NULL;
11582 flinfo.locsym_shndx = NULL;
11583 flinfo.internal_syms = NULL;
11584 flinfo.indices = NULL;
11585 flinfo.sections = NULL;
11586 flinfo.symshndxbuf = NULL;
11587 flinfo.filesym_count = 0;
11589 /* The object attributes have been merged. Remove the input
11590 sections from the link, and set the contents of the output
11592 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11593 for (o = abfd->sections; o != NULL; o = o->next)
11595 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11596 || strcmp (o->name, ".gnu.attributes") == 0)
11598 for (p = o->map_head.link_order; p != NULL; p = p->next)
11600 asection *input_section;
11602 if (p->type != bfd_indirect_link_order)
11604 input_section = p->u.indirect.section;
11605 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11606 elf_link_input_bfd ignores this section. */
11607 input_section->flags &= ~SEC_HAS_CONTENTS;
11610 attr_size = bfd_elf_obj_attr_size (abfd);
11613 bfd_set_section_size (abfd, o, attr_size);
11615 /* Skip this section later on. */
11616 o->map_head.link_order = NULL;
11619 o->flags |= SEC_EXCLUDE;
11621 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11623 /* Remove empty group section from linker output. */
11624 o->flags |= SEC_EXCLUDE;
11625 bfd_section_list_remove (abfd, o);
11626 abfd->section_count--;
11630 /* Count up the number of relocations we will output for each output
11631 section, so that we know the sizes of the reloc sections. We
11632 also figure out some maximum sizes. */
11633 max_contents_size = 0;
11634 max_external_reloc_size = 0;
11635 max_internal_reloc_count = 0;
11637 max_sym_shndx_count = 0;
11639 for (o = abfd->sections; o != NULL; o = o->next)
11641 struct bfd_elf_section_data *esdo = elf_section_data (o);
11642 o->reloc_count = 0;
11644 for (p = o->map_head.link_order; p != NULL; p = p->next)
11646 unsigned int reloc_count = 0;
11647 unsigned int additional_reloc_count = 0;
11648 struct bfd_elf_section_data *esdi = NULL;
11650 if (p->type == bfd_section_reloc_link_order
11651 || p->type == bfd_symbol_reloc_link_order)
11653 else if (p->type == bfd_indirect_link_order)
11657 sec = p->u.indirect.section;
11659 /* Mark all sections which are to be included in the
11660 link. This will normally be every section. We need
11661 to do this so that we can identify any sections which
11662 the linker has decided to not include. */
11663 sec->linker_mark = TRUE;
11665 if (sec->flags & SEC_MERGE)
11668 if (sec->rawsize > max_contents_size)
11669 max_contents_size = sec->rawsize;
11670 if (sec->size > max_contents_size)
11671 max_contents_size = sec->size;
11673 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11674 && (sec->owner->flags & DYNAMIC) == 0)
11678 /* We are interested in just local symbols, not all
11680 if (elf_bad_symtab (sec->owner))
11681 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11682 / bed->s->sizeof_sym);
11684 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11686 if (sym_count > max_sym_count)
11687 max_sym_count = sym_count;
11689 if (sym_count > max_sym_shndx_count
11690 && elf_symtab_shndx_list (sec->owner) != NULL)
11691 max_sym_shndx_count = sym_count;
11693 if (esdo->this_hdr.sh_type == SHT_REL
11694 || esdo->this_hdr.sh_type == SHT_RELA)
11695 /* Some backends use reloc_count in relocation sections
11696 to count particular types of relocs. Of course,
11697 reloc sections themselves can't have relocations. */
11699 else if (emit_relocs)
11701 reloc_count = sec->reloc_count;
11702 if (bed->elf_backend_count_additional_relocs)
11705 c = (*bed->elf_backend_count_additional_relocs) (sec);
11706 additional_reloc_count += c;
11709 else if (bed->elf_backend_count_relocs)
11710 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11712 esdi = elf_section_data (sec);
11714 if ((sec->flags & SEC_RELOC) != 0)
11716 size_t ext_size = 0;
11718 if (esdi->rel.hdr != NULL)
11719 ext_size = esdi->rel.hdr->sh_size;
11720 if (esdi->rela.hdr != NULL)
11721 ext_size += esdi->rela.hdr->sh_size;
11723 if (ext_size > max_external_reloc_size)
11724 max_external_reloc_size = ext_size;
11725 if (sec->reloc_count > max_internal_reloc_count)
11726 max_internal_reloc_count = sec->reloc_count;
11731 if (reloc_count == 0)
11734 reloc_count += additional_reloc_count;
11735 o->reloc_count += reloc_count;
11737 if (p->type == bfd_indirect_link_order && emit_relocs)
11741 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11742 esdo->rel.count += additional_reloc_count;
11744 if (esdi->rela.hdr)
11746 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11747 esdo->rela.count += additional_reloc_count;
11753 esdo->rela.count += reloc_count;
11755 esdo->rel.count += reloc_count;
11759 if (o->reloc_count > 0)
11760 o->flags |= SEC_RELOC;
11763 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11764 set it (this is probably a bug) and if it is set
11765 assign_section_numbers will create a reloc section. */
11766 o->flags &=~ SEC_RELOC;
11769 /* If the SEC_ALLOC flag is not set, force the section VMA to
11770 zero. This is done in elf_fake_sections as well, but forcing
11771 the VMA to 0 here will ensure that relocs against these
11772 sections are handled correctly. */
11773 if ((o->flags & SEC_ALLOC) == 0
11774 && ! o->user_set_vma)
11778 if (! bfd_link_relocatable (info) && merged)
11779 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11781 /* Figure out the file positions for everything but the symbol table
11782 and the relocs. We set symcount to force assign_section_numbers
11783 to create a symbol table. */
11784 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11785 BFD_ASSERT (! abfd->output_has_begun);
11786 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11789 /* Set sizes, and assign file positions for reloc sections. */
11790 for (o = abfd->sections; o != NULL; o = o->next)
11792 struct bfd_elf_section_data *esdo = elf_section_data (o);
11793 if ((o->flags & SEC_RELOC) != 0)
11796 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11800 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11804 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11805 to count upwards while actually outputting the relocations. */
11806 esdo->rel.count = 0;
11807 esdo->rela.count = 0;
11809 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11811 /* Cache the section contents so that they can be compressed
11812 later. Use bfd_malloc since it will be freed by
11813 bfd_compress_section_contents. */
11814 unsigned char *contents = esdo->this_hdr.contents;
11815 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11818 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11819 if (contents == NULL)
11821 esdo->this_hdr.contents = contents;
11825 /* We have now assigned file positions for all the sections except
11826 .symtab, .strtab, and non-loaded reloc sections. We start the
11827 .symtab section at the current file position, and write directly
11828 to it. We build the .strtab section in memory. */
11829 bfd_get_symcount (abfd) = 0;
11830 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11831 /* sh_name is set in prep_headers. */
11832 symtab_hdr->sh_type = SHT_SYMTAB;
11833 /* sh_flags, sh_addr and sh_size all start off zero. */
11834 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11835 /* sh_link is set in assign_section_numbers. */
11836 /* sh_info is set below. */
11837 /* sh_offset is set just below. */
11838 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11840 if (max_sym_count < 20)
11841 max_sym_count = 20;
11842 htab->strtabsize = max_sym_count;
11843 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11844 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11845 if (htab->strtab == NULL)
11847 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11849 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11850 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11852 if (info->strip != strip_all || emit_relocs)
11854 file_ptr off = elf_next_file_pos (abfd);
11856 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11858 /* Note that at this point elf_next_file_pos (abfd) is
11859 incorrect. We do not yet know the size of the .symtab section.
11860 We correct next_file_pos below, after we do know the size. */
11862 /* Start writing out the symbol table. The first symbol is always a
11864 elfsym.st_value = 0;
11865 elfsym.st_size = 0;
11866 elfsym.st_info = 0;
11867 elfsym.st_other = 0;
11868 elfsym.st_shndx = SHN_UNDEF;
11869 elfsym.st_target_internal = 0;
11870 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11871 bfd_und_section_ptr, NULL) != 1)
11874 /* Output a symbol for each section. We output these even if we are
11875 discarding local symbols, since they are used for relocs. These
11876 symbols have no names. We store the index of each one in the
11877 index field of the section, so that we can find it again when
11878 outputting relocs. */
11880 elfsym.st_size = 0;
11881 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11882 elfsym.st_other = 0;
11883 elfsym.st_value = 0;
11884 elfsym.st_target_internal = 0;
11885 for (i = 1; i < elf_numsections (abfd); i++)
11887 o = bfd_section_from_elf_index (abfd, i);
11890 o->target_index = bfd_get_symcount (abfd);
11891 elfsym.st_shndx = i;
11892 if (!bfd_link_relocatable (info))
11893 elfsym.st_value = o->vma;
11894 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11901 /* Allocate some memory to hold information read in from the input
11903 if (max_contents_size != 0)
11905 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11906 if (flinfo.contents == NULL)
11910 if (max_external_reloc_size != 0)
11912 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11913 if (flinfo.external_relocs == NULL)
11917 if (max_internal_reloc_count != 0)
11919 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11920 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11921 if (flinfo.internal_relocs == NULL)
11925 if (max_sym_count != 0)
11927 amt = max_sym_count * bed->s->sizeof_sym;
11928 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11929 if (flinfo.external_syms == NULL)
11932 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11933 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11934 if (flinfo.internal_syms == NULL)
11937 amt = max_sym_count * sizeof (long);
11938 flinfo.indices = (long int *) bfd_malloc (amt);
11939 if (flinfo.indices == NULL)
11942 amt = max_sym_count * sizeof (asection *);
11943 flinfo.sections = (asection **) bfd_malloc (amt);
11944 if (flinfo.sections == NULL)
11948 if (max_sym_shndx_count != 0)
11950 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11951 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11952 if (flinfo.locsym_shndx == NULL)
11958 bfd_vma base, end = 0;
11961 for (sec = htab->tls_sec;
11962 sec && (sec->flags & SEC_THREAD_LOCAL);
11965 bfd_size_type size = sec->size;
11968 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11970 struct bfd_link_order *ord = sec->map_tail.link_order;
11973 size = ord->offset + ord->size;
11975 end = sec->vma + size;
11977 base = htab->tls_sec->vma;
11978 /* Only align end of TLS section if static TLS doesn't have special
11979 alignment requirements. */
11980 if (bed->static_tls_alignment == 1)
11981 end = align_power (end, htab->tls_sec->alignment_power);
11982 htab->tls_size = end - base;
11985 /* Reorder SHF_LINK_ORDER sections. */
11986 for (o = abfd->sections; o != NULL; o = o->next)
11988 if (!elf_fixup_link_order (abfd, o))
11992 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11995 /* Since ELF permits relocations to be against local symbols, we
11996 must have the local symbols available when we do the relocations.
11997 Since we would rather only read the local symbols once, and we
11998 would rather not keep them in memory, we handle all the
11999 relocations for a single input file at the same time.
12001 Unfortunately, there is no way to know the total number of local
12002 symbols until we have seen all of them, and the local symbol
12003 indices precede the global symbol indices. This means that when
12004 we are generating relocatable output, and we see a reloc against
12005 a global symbol, we can not know the symbol index until we have
12006 finished examining all the local symbols to see which ones we are
12007 going to output. To deal with this, we keep the relocations in
12008 memory, and don't output them until the end of the link. This is
12009 an unfortunate waste of memory, but I don't see a good way around
12010 it. Fortunately, it only happens when performing a relocatable
12011 link, which is not the common case. FIXME: If keep_memory is set
12012 we could write the relocs out and then read them again; I don't
12013 know how bad the memory loss will be. */
12015 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12016 sub->output_has_begun = FALSE;
12017 for (o = abfd->sections; o != NULL; o = o->next)
12019 for (p = o->map_head.link_order; p != NULL; p = p->next)
12021 if (p->type == bfd_indirect_link_order
12022 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12023 == bfd_target_elf_flavour)
12024 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12026 if (! sub->output_has_begun)
12028 if (! elf_link_input_bfd (&flinfo, sub))
12030 sub->output_has_begun = TRUE;
12033 else if (p->type == bfd_section_reloc_link_order
12034 || p->type == bfd_symbol_reloc_link_order)
12036 if (! elf_reloc_link_order (abfd, info, o, p))
12041 if (! _bfd_default_link_order (abfd, info, o, p))
12043 if (p->type == bfd_indirect_link_order
12044 && (bfd_get_flavour (sub)
12045 == bfd_target_elf_flavour)
12046 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12047 != bed->s->elfclass))
12049 const char *iclass, *oclass;
12051 switch (bed->s->elfclass)
12053 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12054 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12055 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12059 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12061 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12062 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12063 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12067 bfd_set_error (bfd_error_wrong_format);
12069 /* xgettext:c-format */
12070 (_("%B: file class %s incompatible with %s"),
12071 sub, iclass, oclass);
12080 /* Free symbol buffer if needed. */
12081 if (!info->reduce_memory_overheads)
12083 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12084 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12085 && elf_tdata (sub)->symbuf)
12087 free (elf_tdata (sub)->symbuf);
12088 elf_tdata (sub)->symbuf = NULL;
12092 /* Output any global symbols that got converted to local in a
12093 version script or due to symbol visibility. We do this in a
12094 separate step since ELF requires all local symbols to appear
12095 prior to any global symbols. FIXME: We should only do this if
12096 some global symbols were, in fact, converted to become local.
12097 FIXME: Will this work correctly with the Irix 5 linker? */
12098 eoinfo.failed = FALSE;
12099 eoinfo.flinfo = &flinfo;
12100 eoinfo.localsyms = TRUE;
12101 eoinfo.file_sym_done = FALSE;
12102 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12106 /* If backend needs to output some local symbols not present in the hash
12107 table, do it now. */
12108 if (bed->elf_backend_output_arch_local_syms
12109 && (info->strip != strip_all || emit_relocs))
12111 typedef int (*out_sym_func)
12112 (void *, const char *, Elf_Internal_Sym *, asection *,
12113 struct elf_link_hash_entry *);
12115 if (! ((*bed->elf_backend_output_arch_local_syms)
12116 (abfd, info, &flinfo,
12117 (out_sym_func) elf_link_output_symstrtab)))
12121 /* That wrote out all the local symbols. Finish up the symbol table
12122 with the global symbols. Even if we want to strip everything we
12123 can, we still need to deal with those global symbols that got
12124 converted to local in a version script. */
12126 /* The sh_info field records the index of the first non local symbol. */
12127 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12130 && htab->dynsym != NULL
12131 && htab->dynsym->output_section != bfd_abs_section_ptr)
12133 Elf_Internal_Sym sym;
12134 bfd_byte *dynsym = htab->dynsym->contents;
12136 o = htab->dynsym->output_section;
12137 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12139 /* Write out the section symbols for the output sections. */
12140 if (bfd_link_pic (info)
12141 || htab->is_relocatable_executable)
12147 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12149 sym.st_target_internal = 0;
12151 for (s = abfd->sections; s != NULL; s = s->next)
12157 dynindx = elf_section_data (s)->dynindx;
12160 indx = elf_section_data (s)->this_idx;
12161 BFD_ASSERT (indx > 0);
12162 sym.st_shndx = indx;
12163 if (! check_dynsym (abfd, &sym))
12165 sym.st_value = s->vma;
12166 dest = dynsym + dynindx * bed->s->sizeof_sym;
12167 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12171 /* Write out the local dynsyms. */
12172 if (htab->dynlocal)
12174 struct elf_link_local_dynamic_entry *e;
12175 for (e = htab->dynlocal; e ; e = e->next)
12180 /* Copy the internal symbol and turn off visibility.
12181 Note that we saved a word of storage and overwrote
12182 the original st_name with the dynstr_index. */
12184 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12186 s = bfd_section_from_elf_index (e->input_bfd,
12191 elf_section_data (s->output_section)->this_idx;
12192 if (! check_dynsym (abfd, &sym))
12194 sym.st_value = (s->output_section->vma
12196 + e->isym.st_value);
12199 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12200 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12205 /* We get the global symbols from the hash table. */
12206 eoinfo.failed = FALSE;
12207 eoinfo.localsyms = FALSE;
12208 eoinfo.flinfo = &flinfo;
12209 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12213 /* If backend needs to output some symbols not present in the hash
12214 table, do it now. */
12215 if (bed->elf_backend_output_arch_syms
12216 && (info->strip != strip_all || emit_relocs))
12218 typedef int (*out_sym_func)
12219 (void *, const char *, Elf_Internal_Sym *, asection *,
12220 struct elf_link_hash_entry *);
12222 if (! ((*bed->elf_backend_output_arch_syms)
12223 (abfd, info, &flinfo,
12224 (out_sym_func) elf_link_output_symstrtab)))
12228 /* Finalize the .strtab section. */
12229 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12231 /* Swap out the .strtab section. */
12232 if (!elf_link_swap_symbols_out (&flinfo))
12235 /* Now we know the size of the symtab section. */
12236 if (bfd_get_symcount (abfd) > 0)
12238 /* Finish up and write out the symbol string table (.strtab)
12240 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12241 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12243 if (elf_symtab_shndx_list (abfd))
12245 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12247 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12249 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12250 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12251 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12252 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12253 symtab_shndx_hdr->sh_size = amt;
12255 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12258 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12259 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12264 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12265 /* sh_name was set in prep_headers. */
12266 symstrtab_hdr->sh_type = SHT_STRTAB;
12267 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12268 symstrtab_hdr->sh_addr = 0;
12269 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12270 symstrtab_hdr->sh_entsize = 0;
12271 symstrtab_hdr->sh_link = 0;
12272 symstrtab_hdr->sh_info = 0;
12273 /* sh_offset is set just below. */
12274 symstrtab_hdr->sh_addralign = 1;
12276 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12278 elf_next_file_pos (abfd) = off;
12280 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12281 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12285 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12287 _bfd_error_handler (_("%B: failed to generate import library"),
12288 info->out_implib_bfd);
12292 /* Adjust the relocs to have the correct symbol indices. */
12293 for (o = abfd->sections; o != NULL; o = o->next)
12295 struct bfd_elf_section_data *esdo = elf_section_data (o);
12298 if ((o->flags & SEC_RELOC) == 0)
12301 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12302 if (esdo->rel.hdr != NULL
12303 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12305 if (esdo->rela.hdr != NULL
12306 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12309 /* Set the reloc_count field to 0 to prevent write_relocs from
12310 trying to swap the relocs out itself. */
12311 o->reloc_count = 0;
12314 if (dynamic && info->combreloc && dynobj != NULL)
12315 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12317 /* If we are linking against a dynamic object, or generating a
12318 shared library, finish up the dynamic linking information. */
12321 bfd_byte *dyncon, *dynconend;
12323 /* Fix up .dynamic entries. */
12324 o = bfd_get_linker_section (dynobj, ".dynamic");
12325 BFD_ASSERT (o != NULL);
12327 dyncon = o->contents;
12328 dynconend = o->contents + o->size;
12329 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12331 Elf_Internal_Dyn dyn;
12334 bfd_size_type sh_size;
12337 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12344 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12346 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12348 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12349 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12352 dyn.d_un.d_val = relativecount;
12359 name = info->init_function;
12362 name = info->fini_function;
12365 struct elf_link_hash_entry *h;
12367 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12369 && (h->root.type == bfd_link_hash_defined
12370 || h->root.type == bfd_link_hash_defweak))
12372 dyn.d_un.d_ptr = h->root.u.def.value;
12373 o = h->root.u.def.section;
12374 if (o->output_section != NULL)
12375 dyn.d_un.d_ptr += (o->output_section->vma
12376 + o->output_offset);
12379 /* The symbol is imported from another shared
12380 library and does not apply to this one. */
12381 dyn.d_un.d_ptr = 0;
12388 case DT_PREINIT_ARRAYSZ:
12389 name = ".preinit_array";
12391 case DT_INIT_ARRAYSZ:
12392 name = ".init_array";
12394 case DT_FINI_ARRAYSZ:
12395 name = ".fini_array";
12397 o = bfd_get_section_by_name (abfd, name);
12401 (_("could not find section %s"), name);
12406 (_("warning: %s section has zero size"), name);
12407 dyn.d_un.d_val = o->size;
12410 case DT_PREINIT_ARRAY:
12411 name = ".preinit_array";
12413 case DT_INIT_ARRAY:
12414 name = ".init_array";
12416 case DT_FINI_ARRAY:
12417 name = ".fini_array";
12419 o = bfd_get_section_by_name (abfd, name);
12426 name = ".gnu.hash";
12435 name = ".gnu.version_d";
12438 name = ".gnu.version_r";
12441 name = ".gnu.version";
12443 o = bfd_get_linker_section (dynobj, name);
12445 if (o == NULL || bfd_is_abs_section (o->output_section))
12448 (_("could not find section %s"), name);
12451 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12454 (_("warning: section '%s' is being made into a note"), name);
12455 bfd_set_error (bfd_error_nonrepresentable_section);
12458 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12465 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12471 for (i = 1; i < elf_numsections (abfd); i++)
12473 Elf_Internal_Shdr *hdr;
12475 hdr = elf_elfsections (abfd)[i];
12476 if (hdr->sh_type == type
12477 && (hdr->sh_flags & SHF_ALLOC) != 0)
12479 sh_size += hdr->sh_size;
12481 || sh_addr > hdr->sh_addr)
12482 sh_addr = hdr->sh_addr;
12486 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12488 /* Don't count procedure linkage table relocs in the
12489 overall reloc count. */
12490 sh_size -= htab->srelplt->size;
12492 /* If the size is zero, make the address zero too.
12493 This is to avoid a glibc bug. If the backend
12494 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12495 zero, then we'll put DT_RELA at the end of
12496 DT_JMPREL. glibc will interpret the end of
12497 DT_RELA matching the end of DT_JMPREL as the
12498 case where DT_RELA includes DT_JMPREL, and for
12499 LD_BIND_NOW will decide that processing DT_RELA
12500 will process the PLT relocs too. Net result:
12501 No PLT relocs applied. */
12504 /* If .rela.plt is the first .rela section, exclude
12505 it from DT_RELA. */
12506 else if (sh_addr == (htab->srelplt->output_section->vma
12507 + htab->srelplt->output_offset))
12508 sh_addr += htab->srelplt->size;
12511 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12512 dyn.d_un.d_val = sh_size;
12514 dyn.d_un.d_ptr = sh_addr;
12517 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12521 /* If we have created any dynamic sections, then output them. */
12522 if (dynobj != NULL)
12524 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12527 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12528 if (((info->warn_shared_textrel && bfd_link_pic (info))
12529 || info->error_textrel)
12530 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12532 bfd_byte *dyncon, *dynconend;
12534 dyncon = o->contents;
12535 dynconend = o->contents + o->size;
12536 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12538 Elf_Internal_Dyn dyn;
12540 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12542 if (dyn.d_tag == DT_TEXTREL)
12544 if (info->error_textrel)
12545 info->callbacks->einfo
12546 (_("%P%X: read-only segment has dynamic relocations.\n"));
12548 info->callbacks->einfo
12549 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12555 for (o = dynobj->sections; o != NULL; o = o->next)
12557 if ((o->flags & SEC_HAS_CONTENTS) == 0
12559 || o->output_section == bfd_abs_section_ptr)
12561 if ((o->flags & SEC_LINKER_CREATED) == 0)
12563 /* At this point, we are only interested in sections
12564 created by _bfd_elf_link_create_dynamic_sections. */
12567 if (htab->stab_info.stabstr == o)
12569 if (htab->eh_info.hdr_sec == o)
12571 if (strcmp (o->name, ".dynstr") != 0)
12573 if (! bfd_set_section_contents (abfd, o->output_section,
12575 (file_ptr) o->output_offset
12576 * bfd_octets_per_byte (abfd),
12582 /* The contents of the .dynstr section are actually in a
12586 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12587 if (bfd_seek (abfd, off, SEEK_SET) != 0
12588 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12594 if (!info->resolve_section_groups)
12596 bfd_boolean failed = FALSE;
12598 BFD_ASSERT (bfd_link_relocatable (info));
12599 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12604 /* If we have optimized stabs strings, output them. */
12605 if (htab->stab_info.stabstr != NULL)
12607 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12611 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12614 elf_final_link_free (abfd, &flinfo);
12616 elf_linker (abfd) = TRUE;
12620 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12621 if (contents == NULL)
12622 return FALSE; /* Bail out and fail. */
12623 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12624 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12631 elf_final_link_free (abfd, &flinfo);
12635 /* Initialize COOKIE for input bfd ABFD. */
12638 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12639 struct bfd_link_info *info, bfd *abfd)
12641 Elf_Internal_Shdr *symtab_hdr;
12642 const struct elf_backend_data *bed;
12644 bed = get_elf_backend_data (abfd);
12645 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12647 cookie->abfd = abfd;
12648 cookie->sym_hashes = elf_sym_hashes (abfd);
12649 cookie->bad_symtab = elf_bad_symtab (abfd);
12650 if (cookie->bad_symtab)
12652 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12653 cookie->extsymoff = 0;
12657 cookie->locsymcount = symtab_hdr->sh_info;
12658 cookie->extsymoff = symtab_hdr->sh_info;
12661 if (bed->s->arch_size == 32)
12662 cookie->r_sym_shift = 8;
12664 cookie->r_sym_shift = 32;
12666 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12667 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12669 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12670 cookie->locsymcount, 0,
12672 if (cookie->locsyms == NULL)
12674 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12677 if (info->keep_memory)
12678 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12683 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12686 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12688 Elf_Internal_Shdr *symtab_hdr;
12690 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12691 if (cookie->locsyms != NULL
12692 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12693 free (cookie->locsyms);
12696 /* Initialize the relocation information in COOKIE for input section SEC
12697 of input bfd ABFD. */
12700 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12701 struct bfd_link_info *info, bfd *abfd,
12704 if (sec->reloc_count == 0)
12706 cookie->rels = NULL;
12707 cookie->relend = NULL;
12711 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12712 info->keep_memory);
12713 if (cookie->rels == NULL)
12715 cookie->rel = cookie->rels;
12716 cookie->relend = cookie->rels + sec->reloc_count;
12718 cookie->rel = cookie->rels;
12722 /* Free the memory allocated by init_reloc_cookie_rels,
12726 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12729 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12730 free (cookie->rels);
12733 /* Initialize the whole of COOKIE for input section SEC. */
12736 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12737 struct bfd_link_info *info,
12740 if (!init_reloc_cookie (cookie, info, sec->owner))
12742 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12747 fini_reloc_cookie (cookie, sec->owner);
12752 /* Free the memory allocated by init_reloc_cookie_for_section,
12756 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12759 fini_reloc_cookie_rels (cookie, sec);
12760 fini_reloc_cookie (cookie, sec->owner);
12763 /* Garbage collect unused sections. */
12765 /* Default gc_mark_hook. */
12768 _bfd_elf_gc_mark_hook (asection *sec,
12769 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12770 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12771 struct elf_link_hash_entry *h,
12772 Elf_Internal_Sym *sym)
12776 switch (h->root.type)
12778 case bfd_link_hash_defined:
12779 case bfd_link_hash_defweak:
12780 return h->root.u.def.section;
12782 case bfd_link_hash_common:
12783 return h->root.u.c.p->section;
12790 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12795 /* Return the global debug definition section. */
12798 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12799 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12800 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12801 struct elf_link_hash_entry *h,
12802 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12805 && (h->root.type == bfd_link_hash_defined
12806 || h->root.type == bfd_link_hash_defweak)
12807 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12808 return h->root.u.def.section;
12813 /* COOKIE->rel describes a relocation against section SEC, which is
12814 a section we've decided to keep. Return the section that contains
12815 the relocation symbol, or NULL if no section contains it. */
12818 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12819 elf_gc_mark_hook_fn gc_mark_hook,
12820 struct elf_reloc_cookie *cookie,
12821 bfd_boolean *start_stop)
12823 unsigned long r_symndx;
12824 struct elf_link_hash_entry *h;
12826 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12827 if (r_symndx == STN_UNDEF)
12830 if (r_symndx >= cookie->locsymcount
12831 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12833 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12836 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12840 while (h->root.type == bfd_link_hash_indirect
12841 || h->root.type == bfd_link_hash_warning)
12842 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12844 /* If this symbol is weak and there is a non-weak definition, we
12845 keep the non-weak definition because many backends put
12846 dynamic reloc info on the non-weak definition for code
12847 handling copy relocs. */
12848 if (h->is_weakalias)
12849 weakdef (h)->mark = 1;
12851 if (start_stop != NULL)
12853 /* To work around a glibc bug, mark XXX input sections
12854 when there is a reference to __start_XXX or __stop_XXX
12858 asection *s = h->u2.start_stop_section;
12859 *start_stop = !s->gc_mark;
12864 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12867 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12868 &cookie->locsyms[r_symndx]);
12871 /* COOKIE->rel describes a relocation against section SEC, which is
12872 a section we've decided to keep. Mark the section that contains
12873 the relocation symbol. */
12876 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12878 elf_gc_mark_hook_fn gc_mark_hook,
12879 struct elf_reloc_cookie *cookie)
12882 bfd_boolean start_stop = FALSE;
12884 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12885 while (rsec != NULL)
12887 if (!rsec->gc_mark)
12889 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12890 || (rsec->owner->flags & DYNAMIC) != 0)
12892 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12897 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12902 /* The mark phase of garbage collection. For a given section, mark
12903 it and any sections in this section's group, and all the sections
12904 which define symbols to which it refers. */
12907 _bfd_elf_gc_mark (struct bfd_link_info *info,
12909 elf_gc_mark_hook_fn gc_mark_hook)
12912 asection *group_sec, *eh_frame;
12916 /* Mark all the sections in the group. */
12917 group_sec = elf_section_data (sec)->next_in_group;
12918 if (group_sec && !group_sec->gc_mark)
12919 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12922 /* Look through the section relocs. */
12924 eh_frame = elf_eh_frame_section (sec->owner);
12925 if ((sec->flags & SEC_RELOC) != 0
12926 && sec->reloc_count > 0
12927 && sec != eh_frame)
12929 struct elf_reloc_cookie cookie;
12931 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12935 for (; cookie.rel < cookie.relend; cookie.rel++)
12936 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12941 fini_reloc_cookie_for_section (&cookie, sec);
12945 if (ret && eh_frame && elf_fde_list (sec))
12947 struct elf_reloc_cookie cookie;
12949 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12953 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12954 gc_mark_hook, &cookie))
12956 fini_reloc_cookie_for_section (&cookie, eh_frame);
12960 eh_frame = elf_section_eh_frame_entry (sec);
12961 if (ret && eh_frame && !eh_frame->gc_mark)
12962 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12968 /* Scan and mark sections in a special or debug section group. */
12971 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12973 /* Point to first section of section group. */
12975 /* Used to iterate the section group. */
12978 bfd_boolean is_special_grp = TRUE;
12979 bfd_boolean is_debug_grp = TRUE;
12981 /* First scan to see if group contains any section other than debug
12982 and special section. */
12983 ssec = msec = elf_next_in_group (grp);
12986 if ((msec->flags & SEC_DEBUGGING) == 0)
12987 is_debug_grp = FALSE;
12989 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12990 is_special_grp = FALSE;
12992 msec = elf_next_in_group (msec);
12994 while (msec != ssec);
12996 /* If this is a pure debug section group or pure special section group,
12997 keep all sections in this group. */
12998 if (is_debug_grp || is_special_grp)
13003 msec = elf_next_in_group (msec);
13005 while (msec != ssec);
13009 /* Keep debug and special sections. */
13012 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13013 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13017 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13020 bfd_boolean some_kept;
13021 bfd_boolean debug_frag_seen;
13022 bfd_boolean has_kept_debug_info;
13024 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13026 isec = ibfd->sections;
13027 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13030 /* Ensure all linker created sections are kept,
13031 see if any other section is already marked,
13032 and note if we have any fragmented debug sections. */
13033 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13034 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13036 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13038 else if (isec->gc_mark
13039 && (isec->flags & SEC_ALLOC) != 0
13040 && elf_section_type (isec) != SHT_NOTE)
13043 if (!debug_frag_seen
13044 && (isec->flags & SEC_DEBUGGING)
13045 && CONST_STRNEQ (isec->name, ".debug_line."))
13046 debug_frag_seen = TRUE;
13049 /* If no non-note alloc section in this file will be kept, then
13050 we can toss out the debug and special sections. */
13054 /* Keep debug and special sections like .comment when they are
13055 not part of a group. Also keep section groups that contain
13056 just debug sections or special sections. */
13057 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13059 if ((isec->flags & SEC_GROUP) != 0)
13060 _bfd_elf_gc_mark_debug_special_section_group (isec);
13061 else if (((isec->flags & SEC_DEBUGGING) != 0
13062 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13063 && elf_next_in_group (isec) == NULL)
13065 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13066 has_kept_debug_info = TRUE;
13069 /* Look for CODE sections which are going to be discarded,
13070 and find and discard any fragmented debug sections which
13071 are associated with that code section. */
13072 if (debug_frag_seen)
13073 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13074 if ((isec->flags & SEC_CODE) != 0
13075 && isec->gc_mark == 0)
13080 ilen = strlen (isec->name);
13082 /* Association is determined by the name of the debug
13083 section containing the name of the code section as
13084 a suffix. For example .debug_line.text.foo is a
13085 debug section associated with .text.foo. */
13086 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13090 if (dsec->gc_mark == 0
13091 || (dsec->flags & SEC_DEBUGGING) == 0)
13094 dlen = strlen (dsec->name);
13097 && strncmp (dsec->name + (dlen - ilen),
13098 isec->name, ilen) == 0)
13103 /* Mark debug sections referenced by kept debug sections. */
13104 if (has_kept_debug_info)
13105 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13107 && (isec->flags & SEC_DEBUGGING) != 0)
13108 if (!_bfd_elf_gc_mark (info, isec,
13109 elf_gc_mark_debug_section))
13116 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13119 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13121 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13125 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13126 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13127 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13130 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13133 for (o = sub->sections; o != NULL; o = o->next)
13135 /* When any section in a section group is kept, we keep all
13136 sections in the section group. If the first member of
13137 the section group is excluded, we will also exclude the
13139 if (o->flags & SEC_GROUP)
13141 asection *first = elf_next_in_group (o);
13142 o->gc_mark = first->gc_mark;
13148 /* Skip sweeping sections already excluded. */
13149 if (o->flags & SEC_EXCLUDE)
13152 /* Since this is early in the link process, it is simple
13153 to remove a section from the output. */
13154 o->flags |= SEC_EXCLUDE;
13156 if (info->print_gc_sections && o->size != 0)
13157 /* xgettext:c-format */
13158 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13166 /* Propagate collected vtable information. This is called through
13167 elf_link_hash_traverse. */
13170 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13172 /* Those that are not vtables. */
13174 || h->u2.vtable == NULL
13175 || h->u2.vtable->parent == NULL)
13178 /* Those vtables that do not have parents, we cannot merge. */
13179 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13182 /* If we've already been done, exit. */
13183 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13186 /* Make sure the parent's table is up to date. */
13187 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13189 if (h->u2.vtable->used == NULL)
13191 /* None of this table's entries were referenced. Re-use the
13193 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13194 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13199 bfd_boolean *cu, *pu;
13201 /* Or the parent's entries into ours. */
13202 cu = h->u2.vtable->used;
13204 pu = h->u2.vtable->parent->u2.vtable->used;
13207 const struct elf_backend_data *bed;
13208 unsigned int log_file_align;
13210 bed = get_elf_backend_data (h->root.u.def.section->owner);
13211 log_file_align = bed->s->log_file_align;
13212 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13227 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13230 bfd_vma hstart, hend;
13231 Elf_Internal_Rela *relstart, *relend, *rel;
13232 const struct elf_backend_data *bed;
13233 unsigned int log_file_align;
13235 /* Take care of both those symbols that do not describe vtables as
13236 well as those that are not loaded. */
13238 || h->u2.vtable == NULL
13239 || h->u2.vtable->parent == NULL)
13242 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13243 || h->root.type == bfd_link_hash_defweak);
13245 sec = h->root.u.def.section;
13246 hstart = h->root.u.def.value;
13247 hend = hstart + h->size;
13249 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13251 return *(bfd_boolean *) okp = FALSE;
13252 bed = get_elf_backend_data (sec->owner);
13253 log_file_align = bed->s->log_file_align;
13255 relend = relstart + sec->reloc_count;
13257 for (rel = relstart; rel < relend; ++rel)
13258 if (rel->r_offset >= hstart && rel->r_offset < hend)
13260 /* If the entry is in use, do nothing. */
13261 if (h->u2.vtable->used
13262 && (rel->r_offset - hstart) < h->u2.vtable->size)
13264 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13265 if (h->u2.vtable->used[entry])
13268 /* Otherwise, kill it. */
13269 rel->r_offset = rel->r_info = rel->r_addend = 0;
13275 /* Mark sections containing dynamically referenced symbols. When
13276 building shared libraries, we must assume that any visible symbol is
13280 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13282 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13283 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13285 if ((h->root.type == bfd_link_hash_defined
13286 || h->root.type == bfd_link_hash_defweak)
13287 && ((h->ref_dynamic && !h->forced_local)
13288 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13289 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13290 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13291 && (!bfd_link_executable (info)
13292 || info->gc_keep_exported
13293 || info->export_dynamic
13296 && (*d->match) (&d->head, NULL, h->root.root.string)))
13297 && (h->versioned >= versioned
13298 || !bfd_hide_sym_by_version (info->version_info,
13299 h->root.root.string)))))
13300 h->root.u.def.section->flags |= SEC_KEEP;
13305 /* Keep all sections containing symbols undefined on the command-line,
13306 and the section containing the entry symbol. */
13309 _bfd_elf_gc_keep (struct bfd_link_info *info)
13311 struct bfd_sym_chain *sym;
13313 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13315 struct elf_link_hash_entry *h;
13317 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13318 FALSE, FALSE, FALSE);
13321 && (h->root.type == bfd_link_hash_defined
13322 || h->root.type == bfd_link_hash_defweak)
13323 && !bfd_is_abs_section (h->root.u.def.section)
13324 && !bfd_is_und_section (h->root.u.def.section))
13325 h->root.u.def.section->flags |= SEC_KEEP;
13330 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13331 struct bfd_link_info *info)
13333 bfd *ibfd = info->input_bfds;
13335 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13338 struct elf_reloc_cookie cookie;
13340 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13342 sec = ibfd->sections;
13343 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13346 if (!init_reloc_cookie (&cookie, info, ibfd))
13349 for (sec = ibfd->sections; sec; sec = sec->next)
13351 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13352 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13354 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13355 fini_reloc_cookie_rels (&cookie, sec);
13362 /* Do mark and sweep of unused sections. */
13365 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13367 bfd_boolean ok = TRUE;
13369 elf_gc_mark_hook_fn gc_mark_hook;
13370 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13371 struct elf_link_hash_table *htab;
13373 if (!bed->can_gc_sections
13374 || !is_elf_hash_table (info->hash))
13376 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13380 bed->gc_keep (info);
13381 htab = elf_hash_table (info);
13383 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13384 at the .eh_frame section if we can mark the FDEs individually. */
13385 for (sub = info->input_bfds;
13386 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13387 sub = sub->link.next)
13390 struct elf_reloc_cookie cookie;
13392 sec = sub->sections;
13393 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13395 sec = bfd_get_section_by_name (sub, ".eh_frame");
13396 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13398 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13399 if (elf_section_data (sec)->sec_info
13400 && (sec->flags & SEC_LINKER_CREATED) == 0)
13401 elf_eh_frame_section (sub) = sec;
13402 fini_reloc_cookie_for_section (&cookie, sec);
13403 sec = bfd_get_next_section_by_name (NULL, sec);
13407 /* Apply transitive closure to the vtable entry usage info. */
13408 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13412 /* Kill the vtable relocations that were not used. */
13413 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13417 /* Mark dynamically referenced symbols. */
13418 if (htab->dynamic_sections_created || info->gc_keep_exported)
13419 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13421 /* Grovel through relocs to find out who stays ... */
13422 gc_mark_hook = bed->gc_mark_hook;
13423 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13427 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13428 || elf_object_id (sub) != elf_hash_table_id (htab)
13429 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13433 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13436 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13437 Also treat note sections as a root, if the section is not part
13438 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13439 well as FINI_ARRAY sections for ld -r. */
13440 for (o = sub->sections; o != NULL; o = o->next)
13442 && (o->flags & SEC_EXCLUDE) == 0
13443 && ((o->flags & SEC_KEEP) != 0
13444 || (bfd_link_relocatable (info)
13445 && ((elf_section_data (o)->this_hdr.sh_type
13446 == SHT_PREINIT_ARRAY)
13447 || (elf_section_data (o)->this_hdr.sh_type
13449 || (elf_section_data (o)->this_hdr.sh_type
13450 == SHT_FINI_ARRAY)))
13451 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13452 && elf_next_in_group (o) == NULL )))
13454 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13459 /* Allow the backend to mark additional target specific sections. */
13460 bed->gc_mark_extra_sections (info, gc_mark_hook);
13462 /* ... and mark SEC_EXCLUDE for those that go. */
13463 return elf_gc_sweep (abfd, info);
13466 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13469 bfd_elf_gc_record_vtinherit (bfd *abfd,
13471 struct elf_link_hash_entry *h,
13474 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13475 struct elf_link_hash_entry **search, *child;
13476 size_t extsymcount;
13477 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13479 /* The sh_info field of the symtab header tells us where the
13480 external symbols start. We don't care about the local symbols at
13482 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13483 if (!elf_bad_symtab (abfd))
13484 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13486 sym_hashes = elf_sym_hashes (abfd);
13487 sym_hashes_end = sym_hashes + extsymcount;
13489 /* Hunt down the child symbol, which is in this section at the same
13490 offset as the relocation. */
13491 for (search = sym_hashes; search != sym_hashes_end; ++search)
13493 if ((child = *search) != NULL
13494 && (child->root.type == bfd_link_hash_defined
13495 || child->root.type == bfd_link_hash_defweak)
13496 && child->root.u.def.section == sec
13497 && child->root.u.def.value == offset)
13501 /* xgettext:c-format */
13502 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13503 abfd, sec, offset);
13504 bfd_set_error (bfd_error_invalid_operation);
13508 if (!child->u2.vtable)
13510 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13511 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13512 if (!child->u2.vtable)
13517 /* This *should* only be the absolute section. It could potentially
13518 be that someone has defined a non-global vtable though, which
13519 would be bad. It isn't worth paging in the local symbols to be
13520 sure though; that case should simply be handled by the assembler. */
13522 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13525 child->u2.vtable->parent = h;
13530 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13533 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13534 asection *sec ATTRIBUTE_UNUSED,
13535 struct elf_link_hash_entry *h,
13538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13539 unsigned int log_file_align = bed->s->log_file_align;
13543 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13544 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13549 if (addend >= h->u2.vtable->size)
13551 size_t size, bytes, file_align;
13552 bfd_boolean *ptr = h->u2.vtable->used;
13554 /* While the symbol is undefined, we have to be prepared to handle
13556 file_align = 1 << log_file_align;
13557 if (h->root.type == bfd_link_hash_undefined)
13558 size = addend + file_align;
13562 if (addend >= size)
13564 /* Oops! We've got a reference past the defined end of
13565 the table. This is probably a bug -- shall we warn? */
13566 size = addend + file_align;
13569 size = (size + file_align - 1) & -file_align;
13571 /* Allocate one extra entry for use as a "done" flag for the
13572 consolidation pass. */
13573 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13577 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13583 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13584 * sizeof (bfd_boolean));
13585 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13589 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13594 /* And arrange for that done flag to be at index -1. */
13595 h->u2.vtable->used = ptr + 1;
13596 h->u2.vtable->size = size;
13599 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13604 /* Map an ELF section header flag to its corresponding string. */
13608 flagword flag_value;
13609 } elf_flags_to_name_table;
13611 static elf_flags_to_name_table elf_flags_to_names [] =
13613 { "SHF_WRITE", SHF_WRITE },
13614 { "SHF_ALLOC", SHF_ALLOC },
13615 { "SHF_EXECINSTR", SHF_EXECINSTR },
13616 { "SHF_MERGE", SHF_MERGE },
13617 { "SHF_STRINGS", SHF_STRINGS },
13618 { "SHF_INFO_LINK", SHF_INFO_LINK},
13619 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13620 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13621 { "SHF_GROUP", SHF_GROUP },
13622 { "SHF_TLS", SHF_TLS },
13623 { "SHF_MASKOS", SHF_MASKOS },
13624 { "SHF_EXCLUDE", SHF_EXCLUDE },
13627 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13629 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13630 struct flag_info *flaginfo,
13633 const bfd_vma sh_flags = elf_section_flags (section);
13635 if (!flaginfo->flags_initialized)
13637 bfd *obfd = info->output_bfd;
13638 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13639 struct flag_info_list *tf = flaginfo->flag_list;
13641 int without_hex = 0;
13643 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13646 flagword (*lookup) (char *);
13648 lookup = bed->elf_backend_lookup_section_flags_hook;
13649 if (lookup != NULL)
13651 flagword hexval = (*lookup) ((char *) tf->name);
13655 if (tf->with == with_flags)
13656 with_hex |= hexval;
13657 else if (tf->with == without_flags)
13658 without_hex |= hexval;
13663 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13665 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13667 if (tf->with == with_flags)
13668 with_hex |= elf_flags_to_names[i].flag_value;
13669 else if (tf->with == without_flags)
13670 without_hex |= elf_flags_to_names[i].flag_value;
13677 info->callbacks->einfo
13678 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13682 flaginfo->flags_initialized = TRUE;
13683 flaginfo->only_with_flags |= with_hex;
13684 flaginfo->not_with_flags |= without_hex;
13687 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13690 if ((flaginfo->not_with_flags & sh_flags) != 0)
13696 struct alloc_got_off_arg {
13698 struct bfd_link_info *info;
13701 /* We need a special top-level link routine to convert got reference counts
13702 to real got offsets. */
13705 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13707 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13708 bfd *obfd = gofarg->info->output_bfd;
13709 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13711 if (h->got.refcount > 0)
13713 h->got.offset = gofarg->gotoff;
13714 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13717 h->got.offset = (bfd_vma) -1;
13722 /* And an accompanying bit to work out final got entry offsets once
13723 we're done. Should be called from final_link. */
13726 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13727 struct bfd_link_info *info)
13730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13732 struct alloc_got_off_arg gofarg;
13734 BFD_ASSERT (abfd == info->output_bfd);
13736 if (! is_elf_hash_table (info->hash))
13739 /* The GOT offset is relative to the .got section, but the GOT header is
13740 put into the .got.plt section, if the backend uses it. */
13741 if (bed->want_got_plt)
13744 gotoff = bed->got_header_size;
13746 /* Do the local .got entries first. */
13747 for (i = info->input_bfds; i; i = i->link.next)
13749 bfd_signed_vma *local_got;
13750 size_t j, locsymcount;
13751 Elf_Internal_Shdr *symtab_hdr;
13753 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13756 local_got = elf_local_got_refcounts (i);
13760 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13761 if (elf_bad_symtab (i))
13762 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13764 locsymcount = symtab_hdr->sh_info;
13766 for (j = 0; j < locsymcount; ++j)
13768 if (local_got[j] > 0)
13770 local_got[j] = gotoff;
13771 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13774 local_got[j] = (bfd_vma) -1;
13778 /* Then the global .got entries. .plt refcounts are handled by
13779 adjust_dynamic_symbol */
13780 gofarg.gotoff = gotoff;
13781 gofarg.info = info;
13782 elf_link_hash_traverse (elf_hash_table (info),
13783 elf_gc_allocate_got_offsets,
13788 /* Many folk need no more in the way of final link than this, once
13789 got entry reference counting is enabled. */
13792 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13794 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13797 /* Invoke the regular ELF backend linker to do all the work. */
13798 return bfd_elf_final_link (abfd, info);
13802 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13804 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13806 if (rcookie->bad_symtab)
13807 rcookie->rel = rcookie->rels;
13809 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13811 unsigned long r_symndx;
13813 if (! rcookie->bad_symtab)
13814 if (rcookie->rel->r_offset > offset)
13816 if (rcookie->rel->r_offset != offset)
13819 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13820 if (r_symndx == STN_UNDEF)
13823 if (r_symndx >= rcookie->locsymcount
13824 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13826 struct elf_link_hash_entry *h;
13828 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13830 while (h->root.type == bfd_link_hash_indirect
13831 || h->root.type == bfd_link_hash_warning)
13832 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13834 if ((h->root.type == bfd_link_hash_defined
13835 || h->root.type == bfd_link_hash_defweak)
13836 && (h->root.u.def.section->owner != rcookie->abfd
13837 || h->root.u.def.section->kept_section != NULL
13838 || discarded_section (h->root.u.def.section)))
13843 /* It's not a relocation against a global symbol,
13844 but it could be a relocation against a local
13845 symbol for a discarded section. */
13847 Elf_Internal_Sym *isym;
13849 /* Need to: get the symbol; get the section. */
13850 isym = &rcookie->locsyms[r_symndx];
13851 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13853 && (isec->kept_section != NULL
13854 || discarded_section (isec)))
13862 /* Discard unneeded references to discarded sections.
13863 Returns -1 on error, 1 if any section's size was changed, 0 if
13864 nothing changed. This function assumes that the relocations are in
13865 sorted order, which is true for all known assemblers. */
13868 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13870 struct elf_reloc_cookie cookie;
13875 if (info->traditional_format
13876 || !is_elf_hash_table (info->hash))
13879 o = bfd_get_section_by_name (output_bfd, ".stab");
13884 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13887 || i->reloc_count == 0
13888 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13892 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13895 if (!init_reloc_cookie_for_section (&cookie, info, i))
13898 if (_bfd_discard_section_stabs (abfd, i,
13899 elf_section_data (i)->sec_info,
13900 bfd_elf_reloc_symbol_deleted_p,
13904 fini_reloc_cookie_for_section (&cookie, i);
13909 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13910 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13914 int eh_changed = 0;
13915 unsigned int eh_alignment;
13917 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13923 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13926 if (!init_reloc_cookie_for_section (&cookie, info, i))
13929 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13930 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13931 bfd_elf_reloc_symbol_deleted_p,
13935 if (i->size != i->rawsize)
13939 fini_reloc_cookie_for_section (&cookie, i);
13942 eh_alignment = 1 << o->alignment_power;
13943 /* Skip over zero terminator, and prevent empty sections from
13944 adding alignment padding at the end. */
13945 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13947 i->flags |= SEC_EXCLUDE;
13948 else if (i->size > 4)
13950 /* The last non-empty eh_frame section doesn't need padding. */
13953 /* Any prior sections must pad the last FDE out to the output
13954 section alignment. Otherwise we might have zero padding
13955 between sections, which would be seen as a terminator. */
13956 for (; i != NULL; i = i->map_tail.s)
13958 /* All but the last zero terminator should have been removed. */
13963 = (i->size + eh_alignment - 1) & -eh_alignment;
13964 if (i->size != size)
13972 elf_link_hash_traverse (elf_hash_table (info),
13973 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13976 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13978 const struct elf_backend_data *bed;
13981 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13983 s = abfd->sections;
13984 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13987 bed = get_elf_backend_data (abfd);
13989 if (bed->elf_backend_discard_info != NULL)
13991 if (!init_reloc_cookie (&cookie, info, abfd))
13994 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13997 fini_reloc_cookie (&cookie, abfd);
14001 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14002 _bfd_elf_end_eh_frame_parsing (info);
14004 if (info->eh_frame_hdr_type
14005 && !bfd_link_relocatable (info)
14006 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14013 _bfd_elf_section_already_linked (bfd *abfd,
14015 struct bfd_link_info *info)
14018 const char *name, *key;
14019 struct bfd_section_already_linked *l;
14020 struct bfd_section_already_linked_hash_entry *already_linked_list;
14022 if (sec->output_section == bfd_abs_section_ptr)
14025 flags = sec->flags;
14027 /* Return if it isn't a linkonce section. A comdat group section
14028 also has SEC_LINK_ONCE set. */
14029 if ((flags & SEC_LINK_ONCE) == 0)
14032 /* Don't put group member sections on our list of already linked
14033 sections. They are handled as a group via their group section. */
14034 if (elf_sec_group (sec) != NULL)
14037 /* For a SHT_GROUP section, use the group signature as the key. */
14039 if ((flags & SEC_GROUP) != 0
14040 && elf_next_in_group (sec) != NULL
14041 && elf_group_name (elf_next_in_group (sec)) != NULL)
14042 key = elf_group_name (elf_next_in_group (sec));
14045 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14046 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14047 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14050 /* Must be a user linkonce section that doesn't follow gcc's
14051 naming convention. In this case we won't be matching
14052 single member groups. */
14056 already_linked_list = bfd_section_already_linked_table_lookup (key);
14058 for (l = already_linked_list->entry; l != NULL; l = l->next)
14060 /* We may have 2 different types of sections on the list: group
14061 sections with a signature of <key> (<key> is some string),
14062 and linkonce sections named .gnu.linkonce.<type>.<key>.
14063 Match like sections. LTO plugin sections are an exception.
14064 They are always named .gnu.linkonce.t.<key> and match either
14065 type of section. */
14066 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14067 && ((flags & SEC_GROUP) != 0
14068 || strcmp (name, l->sec->name) == 0))
14069 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14071 /* The section has already been linked. See if we should
14072 issue a warning. */
14073 if (!_bfd_handle_already_linked (sec, l, info))
14076 if (flags & SEC_GROUP)
14078 asection *first = elf_next_in_group (sec);
14079 asection *s = first;
14083 s->output_section = bfd_abs_section_ptr;
14084 /* Record which group discards it. */
14085 s->kept_section = l->sec;
14086 s = elf_next_in_group (s);
14087 /* These lists are circular. */
14097 /* A single member comdat group section may be discarded by a
14098 linkonce section and vice versa. */
14099 if ((flags & SEC_GROUP) != 0)
14101 asection *first = elf_next_in_group (sec);
14103 if (first != NULL && elf_next_in_group (first) == first)
14104 /* Check this single member group against linkonce sections. */
14105 for (l = already_linked_list->entry; l != NULL; l = l->next)
14106 if ((l->sec->flags & SEC_GROUP) == 0
14107 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14109 first->output_section = bfd_abs_section_ptr;
14110 first->kept_section = l->sec;
14111 sec->output_section = bfd_abs_section_ptr;
14116 /* Check this linkonce section against single member groups. */
14117 for (l = already_linked_list->entry; l != NULL; l = l->next)
14118 if (l->sec->flags & SEC_GROUP)
14120 asection *first = elf_next_in_group (l->sec);
14123 && elf_next_in_group (first) == first
14124 && bfd_elf_match_symbols_in_sections (first, sec, info))
14126 sec->output_section = bfd_abs_section_ptr;
14127 sec->kept_section = first;
14132 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14133 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14134 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14135 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14136 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14137 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14138 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14139 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14140 The reverse order cannot happen as there is never a bfd with only the
14141 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14142 matter as here were are looking only for cross-bfd sections. */
14144 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14145 for (l = already_linked_list->entry; l != NULL; l = l->next)
14146 if ((l->sec->flags & SEC_GROUP) == 0
14147 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14149 if (abfd != l->sec->owner)
14150 sec->output_section = bfd_abs_section_ptr;
14154 /* This is the first section with this name. Record it. */
14155 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14156 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14157 return sec->output_section == bfd_abs_section_ptr;
14161 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14163 return sym->st_shndx == SHN_COMMON;
14167 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14173 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14175 return bfd_com_section_ptr;
14179 _bfd_elf_default_got_elt_size (bfd *abfd,
14180 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14181 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14182 bfd *ibfd ATTRIBUTE_UNUSED,
14183 unsigned long symndx ATTRIBUTE_UNUSED)
14185 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14186 return bed->s->arch_size / 8;
14189 /* Routines to support the creation of dynamic relocs. */
14191 /* Returns the name of the dynamic reloc section associated with SEC. */
14193 static const char *
14194 get_dynamic_reloc_section_name (bfd * abfd,
14196 bfd_boolean is_rela)
14199 const char *old_name = bfd_get_section_name (NULL, sec);
14200 const char *prefix = is_rela ? ".rela" : ".rel";
14202 if (old_name == NULL)
14205 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14206 sprintf (name, "%s%s", prefix, old_name);
14211 /* Returns the dynamic reloc section associated with SEC.
14212 If necessary compute the name of the dynamic reloc section based
14213 on SEC's name (looked up in ABFD's string table) and the setting
14217 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14219 bfd_boolean is_rela)
14221 asection * reloc_sec = elf_section_data (sec)->sreloc;
14223 if (reloc_sec == NULL)
14225 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14229 reloc_sec = bfd_get_linker_section (abfd, name);
14231 if (reloc_sec != NULL)
14232 elf_section_data (sec)->sreloc = reloc_sec;
14239 /* Returns the dynamic reloc section associated with SEC. If the
14240 section does not exist it is created and attached to the DYNOBJ
14241 bfd and stored in the SRELOC field of SEC's elf_section_data
14244 ALIGNMENT is the alignment for the newly created section and
14245 IS_RELA defines whether the name should be .rela.<SEC's name>
14246 or .rel.<SEC's name>. The section name is looked up in the
14247 string table associated with ABFD. */
14250 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14252 unsigned int alignment,
14254 bfd_boolean is_rela)
14256 asection * reloc_sec = elf_section_data (sec)->sreloc;
14258 if (reloc_sec == NULL)
14260 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14265 reloc_sec = bfd_get_linker_section (dynobj, name);
14267 if (reloc_sec == NULL)
14269 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14270 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14271 if ((sec->flags & SEC_ALLOC) != 0)
14272 flags |= SEC_ALLOC | SEC_LOAD;
14274 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14275 if (reloc_sec != NULL)
14277 /* _bfd_elf_get_sec_type_attr chooses a section type by
14278 name. Override as it may be wrong, eg. for a user
14279 section named "auto" we'll get ".relauto" which is
14280 seen to be a .rela section. */
14281 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14282 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14287 elf_section_data (sec)->sreloc = reloc_sec;
14293 /* Copy the ELF symbol type and other attributes for a linker script
14294 assignment from HSRC to HDEST. Generally this should be treated as
14295 if we found a strong non-dynamic definition for HDEST (except that
14296 ld ignores multiple definition errors). */
14298 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14299 struct bfd_link_hash_entry *hdest,
14300 struct bfd_link_hash_entry *hsrc)
14302 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14303 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14304 Elf_Internal_Sym isym;
14306 ehdest->type = ehsrc->type;
14307 ehdest->target_internal = ehsrc->target_internal;
14309 isym.st_other = ehsrc->other;
14310 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14313 /* Append a RELA relocation REL to section S in BFD. */
14316 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14318 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14319 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14320 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14321 bed->s->swap_reloca_out (abfd, rel, loc);
14324 /* Append a REL relocation REL to section S in BFD. */
14327 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14329 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14330 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14331 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14332 bed->s->swap_reloc_out (abfd, rel, loc);
14335 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14337 struct bfd_link_hash_entry *
14338 bfd_elf_define_start_stop (struct bfd_link_info *info,
14339 const char *symbol, asection *sec)
14341 struct elf_link_hash_entry *h;
14343 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14344 FALSE, FALSE, TRUE);
14346 && (h->root.type == bfd_link_hash_undefined
14347 || h->root.type == bfd_link_hash_undefweak
14348 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14350 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14351 h->root.type = bfd_link_hash_defined;
14352 h->root.u.def.section = sec;
14353 h->root.u.def.value = 0;
14354 h->def_regular = 1;
14355 h->def_dynamic = 0;
14357 h->u2.start_stop_section = sec;
14358 if (symbol[0] == '.')
14360 /* .startof. and .sizeof. symbols are local. */
14361 const struct elf_backend_data *bed;
14362 bed = get_elf_backend_data (info->output_bfd);
14363 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14367 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14368 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14370 bfd_elf_link_record_dynamic_symbol (info, h);