1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 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
923 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
924 struct bfd_link_info *info,
925 unsigned long *section_sym_count)
927 unsigned long dynsymcount = 0;
929 if (bfd_link_pic (info)
930 || elf_hash_table (info)->is_relocatable_executable)
932 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
934 for (p = output_bfd->sections; p ; p = p->next)
935 if ((p->flags & SEC_EXCLUDE) == 0
936 && (p->flags & SEC_ALLOC) != 0
937 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
938 elf_section_data (p)->dynindx = ++dynsymcount;
940 elf_section_data (p)->dynindx = 0;
942 *section_sym_count = dynsymcount;
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_local_hash_table_dynsyms,
948 if (elf_hash_table (info)->dynlocal)
950 struct elf_link_local_dynamic_entry *p;
951 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
952 p->dynindx = ++dynsymcount;
954 elf_hash_table (info)->local_dynsymcount = dynsymcount;
956 elf_link_hash_traverse (elf_hash_table (info),
957 elf_link_renumber_hash_table_dynsyms,
960 /* There is an unused NULL entry at the head of the table which we
961 must account for in our count even if the table is empty since it
962 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
966 elf_hash_table (info)->dynsymcount = dynsymcount;
970 /* Merge st_other field. */
973 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
974 const Elf_Internal_Sym *isym, asection *sec,
975 bfd_boolean definition, bfd_boolean dynamic)
977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
979 /* If st_other has a processor-specific meaning, specific
980 code might be needed here. */
981 if (bed->elf_backend_merge_symbol_attribute)
982 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
987 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
988 unsigned hvis = ELF_ST_VISIBILITY (h->other);
990 /* Keep the most constraining visibility. Leave the remainder
991 of the st_other field to elf_backend_merge_symbol_attribute. */
992 if (symvis - 1 < hvis - 1)
993 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
996 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
997 && (sec->flags & SEC_READONLY) == 0)
998 h->protected_def = 1;
1001 /* This function is called when we want to merge a new symbol with an
1002 existing symbol. It handles the various cases which arise when we
1003 find a definition in a dynamic object, or when there is already a
1004 definition in a dynamic object. The new symbol is described by
1005 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1006 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1007 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1008 of an old common symbol. We set OVERRIDE if the old symbol is
1009 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1010 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1011 to change. By OK to change, we mean that we shouldn't warn if the
1012 type or size does change. */
1015 _bfd_elf_merge_symbol (bfd *abfd,
1016 struct bfd_link_info *info,
1018 Elf_Internal_Sym *sym,
1021 struct elf_link_hash_entry **sym_hash,
1023 bfd_boolean *pold_weak,
1024 unsigned int *pold_alignment,
1026 bfd_boolean *override,
1027 bfd_boolean *type_change_ok,
1028 bfd_boolean *size_change_ok,
1029 bfd_boolean *matched)
1031 asection *sec, *oldsec;
1032 struct elf_link_hash_entry *h;
1033 struct elf_link_hash_entry *hi;
1034 struct elf_link_hash_entry *flip;
1037 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1038 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1039 const struct elf_backend_data *bed;
1041 bfd_boolean default_sym = *matched;
1047 bind = ELF_ST_BIND (sym->st_info);
1049 if (! bfd_is_und_section (sec))
1050 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1052 h = ((struct elf_link_hash_entry *)
1053 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1058 bed = get_elf_backend_data (abfd);
1060 /* NEW_VERSION is the symbol version of the new symbol. */
1061 if (h->versioned != unversioned)
1063 /* Symbol version is unknown or versioned. */
1064 new_version = strrchr (name, ELF_VER_CHR);
1067 if (h->versioned == unknown)
1069 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1070 h->versioned = versioned_hidden;
1072 h->versioned = versioned;
1075 if (new_version[0] == '\0')
1079 h->versioned = unversioned;
1084 /* For merging, we only care about real symbols. But we need to make
1085 sure that indirect symbol dynamic flags are updated. */
1087 while (h->root.type == bfd_link_hash_indirect
1088 || h->root.type == bfd_link_hash_warning)
1089 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1093 if (hi == h || h->root.type == bfd_link_hash_new)
1097 /* OLD_HIDDEN is true if the existing symbol is only visible
1098 to the symbol with the same symbol version. NEW_HIDDEN is
1099 true if the new symbol is only visible to the symbol with
1100 the same symbol version. */
1101 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1102 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1103 if (!old_hidden && !new_hidden)
1104 /* The new symbol matches the existing symbol if both
1109 /* OLD_VERSION is the symbol version of the existing
1113 if (h->versioned >= versioned)
1114 old_version = strrchr (h->root.root.string,
1119 /* The new symbol matches the existing symbol if they
1120 have the same symbol version. */
1121 *matched = (old_version == new_version
1122 || (old_version != NULL
1123 && new_version != NULL
1124 && strcmp (old_version, new_version) == 0));
1129 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1134 switch (h->root.type)
1139 case bfd_link_hash_undefined:
1140 case bfd_link_hash_undefweak:
1141 oldbfd = h->root.u.undef.abfd;
1144 case bfd_link_hash_defined:
1145 case bfd_link_hash_defweak:
1146 oldbfd = h->root.u.def.section->owner;
1147 oldsec = h->root.u.def.section;
1150 case bfd_link_hash_common:
1151 oldbfd = h->root.u.c.p->section->owner;
1152 oldsec = h->root.u.c.p->section;
1154 *pold_alignment = h->root.u.c.p->alignment_power;
1157 if (poldbfd && *poldbfd == NULL)
1160 /* Differentiate strong and weak symbols. */
1161 newweak = bind == STB_WEAK;
1162 oldweak = (h->root.type == bfd_link_hash_defweak
1163 || h->root.type == bfd_link_hash_undefweak);
1165 *pold_weak = oldweak;
1167 /* We have to check it for every instance since the first few may be
1168 references and not all compilers emit symbol type for undefined
1170 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1172 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1173 respectively, is from a dynamic object. */
1175 newdyn = (abfd->flags & DYNAMIC) != 0;
1177 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1178 syms and defined syms in dynamic libraries respectively.
1179 ref_dynamic on the other hand can be set for a symbol defined in
1180 a dynamic library, and def_dynamic may not be set; When the
1181 definition in a dynamic lib is overridden by a definition in the
1182 executable use of the symbol in the dynamic lib becomes a
1183 reference to the executable symbol. */
1186 if (bfd_is_und_section (sec))
1188 if (bind != STB_WEAK)
1190 h->ref_dynamic_nonweak = 1;
1191 hi->ref_dynamic_nonweak = 1;
1196 /* Update the existing symbol only if they match. */
1199 hi->dynamic_def = 1;
1203 /* If we just created the symbol, mark it as being an ELF symbol.
1204 Other than that, there is nothing to do--there is no merge issue
1205 with a newly defined symbol--so we just return. */
1207 if (h->root.type == bfd_link_hash_new)
1213 /* In cases involving weak versioned symbols, we may wind up trying
1214 to merge a symbol with itself. Catch that here, to avoid the
1215 confusion that results if we try to override a symbol with
1216 itself. The additional tests catch cases like
1217 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1218 dynamic object, which we do want to handle here. */
1220 && (newweak || oldweak)
1221 && ((abfd->flags & DYNAMIC) == 0
1222 || !h->def_regular))
1227 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1228 else if (oldsec != NULL)
1230 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1231 indices used by MIPS ELF. */
1232 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1235 /* Handle a case where plugin_notice won't be called and thus won't
1236 set the non_ir_ref flags on the first pass over symbols. */
1238 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1239 && newdyn != olddyn)
1241 h->root.non_ir_ref_dynamic = TRUE;
1242 hi->root.non_ir_ref_dynamic = TRUE;
1245 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1246 respectively, appear to be a definition rather than reference. */
1248 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1250 olddef = (h->root.type != bfd_link_hash_undefined
1251 && h->root.type != bfd_link_hash_undefweak
1252 && h->root.type != bfd_link_hash_common);
1254 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1255 respectively, appear to be a function. */
1257 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1258 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1260 oldfunc = (h->type != STT_NOTYPE
1261 && bed->is_function_type (h->type));
1263 if (!(newfunc && oldfunc)
1264 && ELF_ST_TYPE (sym->st_info) != h->type
1265 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1266 && h->type != STT_NOTYPE
1267 && (newdef || bfd_is_com_section (sec))
1268 && (olddef || h->root.type == bfd_link_hash_common))
1270 /* If creating a default indirect symbol ("foo" or "foo@") from
1271 a dynamic versioned definition ("foo@@") skip doing so if
1272 there is an existing regular definition with a different
1273 type. We don't want, for example, a "time" variable in the
1274 executable overriding a "time" function in a shared library. */
1282 /* When adding a symbol from a regular object file after we have
1283 created indirect symbols, undo the indirection and any
1290 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1291 h->forced_local = 0;
1295 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1297 h->root.type = bfd_link_hash_undefined;
1298 h->root.u.undef.abfd = abfd;
1302 h->root.type = bfd_link_hash_new;
1303 h->root.u.undef.abfd = NULL;
1309 /* Check TLS symbols. We don't check undefined symbols introduced
1310 by "ld -u" which have no type (and oldbfd NULL), and we don't
1311 check symbols from plugins because they also have no type. */
1313 && (oldbfd->flags & BFD_PLUGIN) == 0
1314 && (abfd->flags & BFD_PLUGIN) == 0
1315 && ELF_ST_TYPE (sym->st_info) != h->type
1316 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1319 bfd_boolean ntdef, tdef;
1320 asection *ntsec, *tsec;
1322 if (h->type == STT_TLS)
1343 /* xgettext:c-format */
1344 (_("%s: TLS definition in %B section %A "
1345 "mismatches non-TLS definition in %B section %A"),
1346 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1347 else if (!tdef && !ntdef)
1349 /* xgettext:c-format */
1350 (_("%s: TLS reference in %B "
1351 "mismatches non-TLS reference in %B"),
1352 h->root.root.string, tbfd, ntbfd);
1355 /* xgettext:c-format */
1356 (_("%s: TLS definition in %B section %A "
1357 "mismatches non-TLS reference in %B"),
1358 h->root.root.string, tbfd, tsec, ntbfd);
1361 /* xgettext:c-format */
1362 (_("%s: TLS reference in %B "
1363 "mismatches non-TLS definition in %B section %A"),
1364 h->root.root.string, tbfd, ntbfd, ntsec);
1366 bfd_set_error (bfd_error_bad_value);
1370 /* If the old symbol has non-default visibility, we ignore the new
1371 definition from a dynamic object. */
1373 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1374 && !bfd_is_und_section (sec))
1377 /* Make sure this symbol is dynamic. */
1379 hi->ref_dynamic = 1;
1380 /* A protected symbol has external availability. Make sure it is
1381 recorded as dynamic.
1383 FIXME: Should we check type and size for protected symbol? */
1384 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1385 return bfd_elf_link_record_dynamic_symbol (info, h);
1390 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1393 /* If the new symbol with non-default visibility comes from a
1394 relocatable file and the old definition comes from a dynamic
1395 object, we remove the old definition. */
1396 if (hi->root.type == bfd_link_hash_indirect)
1398 /* Handle the case where the old dynamic definition is
1399 default versioned. We need to copy the symbol info from
1400 the symbol with default version to the normal one if it
1401 was referenced before. */
1404 hi->root.type = h->root.type;
1405 h->root.type = bfd_link_hash_indirect;
1406 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1408 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1409 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1411 /* If the new symbol is hidden or internal, completely undo
1412 any dynamic link state. */
1413 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1414 h->forced_local = 0;
1421 /* FIXME: Should we check type and size for protected symbol? */
1431 /* If the old symbol was undefined before, then it will still be
1432 on the undefs list. If the new symbol is undefined or
1433 common, we can't make it bfd_link_hash_new here, because new
1434 undefined or common symbols will be added to the undefs list
1435 by _bfd_generic_link_add_one_symbol. Symbols may not be
1436 added twice to the undefs list. Also, if the new symbol is
1437 undefweak then we don't want to lose the strong undef. */
1438 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1440 h->root.type = bfd_link_hash_undefined;
1441 h->root.u.undef.abfd = abfd;
1445 h->root.type = bfd_link_hash_new;
1446 h->root.u.undef.abfd = NULL;
1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1454 h->forced_local = 0;
1460 /* FIXME: Should we check type and size for protected symbol? */
1466 /* If a new weak symbol definition comes from a regular file and the
1467 old symbol comes from a dynamic library, we treat the new one as
1468 strong. Similarly, an old weak symbol definition from a regular
1469 file is treated as strong when the new symbol comes from a dynamic
1470 library. Further, an old weak symbol from a dynamic library is
1471 treated as strong if the new symbol is from a dynamic library.
1472 This reflects the way glibc's ld.so works.
1474 Do this before setting *type_change_ok or *size_change_ok so that
1475 we warn properly when dynamic library symbols are overridden. */
1477 if (newdef && !newdyn && olddyn)
1479 if (olddef && newdyn)
1482 /* Allow changes between different types of function symbol. */
1483 if (newfunc && oldfunc)
1484 *type_change_ok = TRUE;
1486 /* It's OK to change the type if either the existing symbol or the
1487 new symbol is weak. A type change is also OK if the old symbol
1488 is undefined and the new symbol is defined. */
1493 && h->root.type == bfd_link_hash_undefined))
1494 *type_change_ok = TRUE;
1496 /* It's OK to change the size if either the existing symbol or the
1497 new symbol is weak, or if the old symbol is undefined. */
1500 || h->root.type == bfd_link_hash_undefined)
1501 *size_change_ok = TRUE;
1503 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1504 symbol, respectively, appears to be a common symbol in a dynamic
1505 object. If a symbol appears in an uninitialized section, and is
1506 not weak, and is not a function, then it may be a common symbol
1507 which was resolved when the dynamic object was created. We want
1508 to treat such symbols specially, because they raise special
1509 considerations when setting the symbol size: if the symbol
1510 appears as a common symbol in a regular object, and the size in
1511 the regular object is larger, we must make sure that we use the
1512 larger size. This problematic case can always be avoided in C,
1513 but it must be handled correctly when using Fortran shared
1516 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1517 likewise for OLDDYNCOMMON and OLDDEF.
1519 Note that this test is just a heuristic, and that it is quite
1520 possible to have an uninitialized symbol in a shared object which
1521 is really a definition, rather than a common symbol. This could
1522 lead to some minor confusion when the symbol really is a common
1523 symbol in some regular object. However, I think it will be
1529 && (sec->flags & SEC_ALLOC) != 0
1530 && (sec->flags & SEC_LOAD) == 0
1533 newdyncommon = TRUE;
1535 newdyncommon = FALSE;
1539 && h->root.type == bfd_link_hash_defined
1541 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1542 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1545 olddyncommon = TRUE;
1547 olddyncommon = FALSE;
1549 /* We now know everything about the old and new symbols. We ask the
1550 backend to check if we can merge them. */
1551 if (bed->merge_symbol != NULL)
1553 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1558 /* There are multiple definitions of a normal symbol.
1559 Skip the default symbol as well. */
1560 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1561 && !default_sym && h->def_regular)
1563 /* Handle a multiple definition. */
1564 (*info->callbacks->multiple_definition) (info, &h->root,
1565 abfd, sec, *pvalue);
1570 /* If both the old and the new symbols look like common symbols in a
1571 dynamic object, set the size of the symbol to the larger of the
1576 && sym->st_size != h->size)
1578 /* Since we think we have two common symbols, issue a multiple
1579 common warning if desired. Note that we only warn if the
1580 size is different. If the size is the same, we simply let
1581 the old symbol override the new one as normally happens with
1582 symbols defined in dynamic objects. */
1584 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1585 bfd_link_hash_common, sym->st_size);
1586 if (sym->st_size > h->size)
1587 h->size = sym->st_size;
1589 *size_change_ok = TRUE;
1592 /* If we are looking at a dynamic object, and we have found a
1593 definition, we need to see if the symbol was already defined by
1594 some other object. If so, we want to use the existing
1595 definition, and we do not want to report a multiple symbol
1596 definition error; we do this by clobbering *PSEC to be
1597 bfd_und_section_ptr.
1599 We treat a common symbol as a definition if the symbol in the
1600 shared library is a function, since common symbols always
1601 represent variables; this can cause confusion in principle, but
1602 any such confusion would seem to indicate an erroneous program or
1603 shared library. We also permit a common symbol in a regular
1604 object to override a weak symbol in a shared object. */
1609 || (h->root.type == bfd_link_hash_common
1610 && (newweak || newfunc))))
1614 newdyncommon = FALSE;
1616 *psec = sec = bfd_und_section_ptr;
1617 *size_change_ok = TRUE;
1619 /* If we get here when the old symbol is a common symbol, then
1620 we are explicitly letting it override a weak symbol or
1621 function in a dynamic object, and we don't want to warn about
1622 a type change. If the old symbol is a defined symbol, a type
1623 change warning may still be appropriate. */
1625 if (h->root.type == bfd_link_hash_common)
1626 *type_change_ok = TRUE;
1629 /* Handle the special case of an old common symbol merging with a
1630 new symbol which looks like a common symbol in a shared object.
1631 We change *PSEC and *PVALUE to make the new symbol look like a
1632 common symbol, and let _bfd_generic_link_add_one_symbol do the
1636 && h->root.type == bfd_link_hash_common)
1640 newdyncommon = FALSE;
1641 *pvalue = sym->st_size;
1642 *psec = sec = bed->common_section (oldsec);
1643 *size_change_ok = TRUE;
1646 /* Skip weak definitions of symbols that are already defined. */
1647 if (newdef && olddef && newweak)
1649 /* Don't skip new non-IR weak syms. */
1650 if (!(oldbfd != NULL
1651 && (oldbfd->flags & BFD_PLUGIN) != 0
1652 && (abfd->flags & BFD_PLUGIN) == 0))
1658 /* Merge st_other. If the symbol already has a dynamic index,
1659 but visibility says it should not be visible, turn it into a
1661 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1662 if (h->dynindx != -1)
1663 switch (ELF_ST_VISIBILITY (h->other))
1667 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1672 /* If the old symbol is from a dynamic object, and the new symbol is
1673 a definition which is not from a dynamic object, then the new
1674 symbol overrides the old symbol. Symbols from regular files
1675 always take precedence over symbols from dynamic objects, even if
1676 they are defined after the dynamic object in the link.
1678 As above, we again permit a common symbol in a regular object to
1679 override a definition in a shared object if the shared object
1680 symbol is a function or is weak. */
1685 || (bfd_is_com_section (sec)
1686 && (oldweak || oldfunc)))
1691 /* Change the hash table entry to undefined, and let
1692 _bfd_generic_link_add_one_symbol do the right thing with the
1695 h->root.type = bfd_link_hash_undefined;
1696 h->root.u.undef.abfd = h->root.u.def.section->owner;
1697 *size_change_ok = TRUE;
1700 olddyncommon = FALSE;
1702 /* We again permit a type change when a common symbol may be
1703 overriding a function. */
1705 if (bfd_is_com_section (sec))
1709 /* If a common symbol overrides a function, make sure
1710 that it isn't defined dynamically nor has type
1713 h->type = STT_NOTYPE;
1715 *type_change_ok = TRUE;
1718 if (hi->root.type == bfd_link_hash_indirect)
1721 /* This union may have been set to be non-NULL when this symbol
1722 was seen in a dynamic object. We must force the union to be
1723 NULL, so that it is correct for a regular symbol. */
1724 h->verinfo.vertree = NULL;
1727 /* Handle the special case of a new common symbol merging with an
1728 old symbol that looks like it might be a common symbol defined in
1729 a shared object. Note that we have already handled the case in
1730 which a new common symbol should simply override the definition
1731 in the shared library. */
1734 && bfd_is_com_section (sec)
1737 /* It would be best if we could set the hash table entry to a
1738 common symbol, but we don't know what to use for the section
1739 or the alignment. */
1740 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1741 bfd_link_hash_common, sym->st_size);
1743 /* If the presumed common symbol in the dynamic object is
1744 larger, pretend that the new symbol has its size. */
1746 if (h->size > *pvalue)
1749 /* We need to remember the alignment required by the symbol
1750 in the dynamic object. */
1751 BFD_ASSERT (pold_alignment);
1752 *pold_alignment = h->root.u.def.section->alignment_power;
1755 olddyncommon = FALSE;
1757 h->root.type = bfd_link_hash_undefined;
1758 h->root.u.undef.abfd = h->root.u.def.section->owner;
1760 *size_change_ok = TRUE;
1761 *type_change_ok = TRUE;
1763 if (hi->root.type == bfd_link_hash_indirect)
1766 h->verinfo.vertree = NULL;
1771 /* Handle the case where we had a versioned symbol in a dynamic
1772 library and now find a definition in a normal object. In this
1773 case, we make the versioned symbol point to the normal one. */
1774 flip->root.type = h->root.type;
1775 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1776 h->root.type = bfd_link_hash_indirect;
1777 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1778 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1782 flip->ref_dynamic = 1;
1789 /* This function is called to create an indirect symbol from the
1790 default for the symbol with the default version if needed. The
1791 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1792 set DYNSYM if the new indirect symbol is dynamic. */
1795 _bfd_elf_add_default_symbol (bfd *abfd,
1796 struct bfd_link_info *info,
1797 struct elf_link_hash_entry *h,
1799 Elf_Internal_Sym *sym,
1803 bfd_boolean *dynsym)
1805 bfd_boolean type_change_ok;
1806 bfd_boolean size_change_ok;
1809 struct elf_link_hash_entry *hi;
1810 struct bfd_link_hash_entry *bh;
1811 const struct elf_backend_data *bed;
1812 bfd_boolean collect;
1813 bfd_boolean dynamic;
1814 bfd_boolean override;
1816 size_t len, shortlen;
1818 bfd_boolean matched;
1820 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1823 /* If this symbol has a version, and it is the default version, we
1824 create an indirect symbol from the default name to the fully
1825 decorated name. This will cause external references which do not
1826 specify a version to be bound to this version of the symbol. */
1827 p = strchr (name, ELF_VER_CHR);
1828 if (h->versioned == unknown)
1832 h->versioned = unversioned;
1837 if (p[1] != ELF_VER_CHR)
1839 h->versioned = versioned_hidden;
1843 h->versioned = versioned;
1848 /* PR ld/19073: We may see an unversioned definition after the
1854 bed = get_elf_backend_data (abfd);
1855 collect = bed->collect;
1856 dynamic = (abfd->flags & DYNAMIC) != 0;
1858 shortlen = p - name;
1859 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1860 if (shortname == NULL)
1862 memcpy (shortname, name, shortlen);
1863 shortname[shortlen] = '\0';
1865 /* We are going to create a new symbol. Merge it with any existing
1866 symbol with this name. For the purposes of the merge, act as
1867 though we were defining the symbol we just defined, although we
1868 actually going to define an indirect symbol. */
1869 type_change_ok = FALSE;
1870 size_change_ok = FALSE;
1873 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1874 &hi, poldbfd, NULL, NULL, &skip, &override,
1875 &type_change_ok, &size_change_ok, &matched))
1881 if (hi->def_regular)
1883 /* If the undecorated symbol will have a version added by a
1884 script different to H, then don't indirect to/from the
1885 undecorated symbol. This isn't ideal because we may not yet
1886 have seen symbol versions, if given by a script on the
1887 command line rather than via --version-script. */
1888 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1893 = bfd_find_version_for_sym (info->version_info,
1894 hi->root.root.string, &hide);
1895 if (hi->verinfo.vertree != NULL && hide)
1897 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1901 if (hi->verinfo.vertree != NULL
1902 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1908 /* Add the default symbol if not performing a relocatable link. */
1909 if (! bfd_link_relocatable (info))
1912 if (! (_bfd_generic_link_add_one_symbol
1913 (info, abfd, shortname, BSF_INDIRECT,
1914 bfd_ind_section_ptr,
1915 0, name, FALSE, collect, &bh)))
1917 hi = (struct elf_link_hash_entry *) bh;
1922 /* In this case the symbol named SHORTNAME is overriding the
1923 indirect symbol we want to add. We were planning on making
1924 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1925 is the name without a version. NAME is the fully versioned
1926 name, and it is the default version.
1928 Overriding means that we already saw a definition for the
1929 symbol SHORTNAME in a regular object, and it is overriding
1930 the symbol defined in the dynamic object.
1932 When this happens, we actually want to change NAME, the
1933 symbol we just added, to refer to SHORTNAME. This will cause
1934 references to NAME in the shared object to become references
1935 to SHORTNAME in the regular object. This is what we expect
1936 when we override a function in a shared object: that the
1937 references in the shared object will be mapped to the
1938 definition in the regular object. */
1940 while (hi->root.type == bfd_link_hash_indirect
1941 || hi->root.type == bfd_link_hash_warning)
1942 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1944 h->root.type = bfd_link_hash_indirect;
1945 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1949 hi->ref_dynamic = 1;
1953 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1958 /* Now set HI to H, so that the following code will set the
1959 other fields correctly. */
1963 /* Check if HI is a warning symbol. */
1964 if (hi->root.type == bfd_link_hash_warning)
1965 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1967 /* If there is a duplicate definition somewhere, then HI may not
1968 point to an indirect symbol. We will have reported an error to
1969 the user in that case. */
1971 if (hi->root.type == bfd_link_hash_indirect)
1973 struct elf_link_hash_entry *ht;
1975 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1976 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1978 /* A reference to the SHORTNAME symbol from a dynamic library
1979 will be satisfied by the versioned symbol at runtime. In
1980 effect, we have a reference to the versioned symbol. */
1981 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1982 hi->dynamic_def |= ht->dynamic_def;
1984 /* See if the new flags lead us to realize that the symbol must
1990 if (! bfd_link_executable (info)
1997 if (hi->ref_regular)
2003 /* We also need to define an indirection from the nondefault version
2007 len = strlen (name);
2008 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2009 if (shortname == NULL)
2011 memcpy (shortname, name, shortlen);
2012 memcpy (shortname + shortlen, p + 1, len - shortlen);
2014 /* Once again, merge with any existing symbol. */
2015 type_change_ok = FALSE;
2016 size_change_ok = FALSE;
2018 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2019 &hi, poldbfd, NULL, NULL, &skip, &override,
2020 &type_change_ok, &size_change_ok, &matched))
2028 /* Here SHORTNAME is a versioned name, so we don't expect to see
2029 the type of override we do in the case above unless it is
2030 overridden by a versioned definition. */
2031 if (hi->root.type != bfd_link_hash_defined
2032 && hi->root.type != bfd_link_hash_defweak)
2034 /* xgettext:c-format */
2035 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2041 if (! (_bfd_generic_link_add_one_symbol
2042 (info, abfd, shortname, BSF_INDIRECT,
2043 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2045 hi = (struct elf_link_hash_entry *) bh;
2047 /* If there is a duplicate definition somewhere, then HI may not
2048 point to an indirect symbol. We will have reported an error
2049 to the user in that case. */
2051 if (hi->root.type == bfd_link_hash_indirect)
2053 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2054 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2055 hi->dynamic_def |= h->dynamic_def;
2057 /* See if the new flags lead us to realize that the symbol
2063 if (! bfd_link_executable (info)
2069 if (hi->ref_regular)
2079 /* This routine is used to export all defined symbols into the dynamic
2080 symbol table. It is called via elf_link_hash_traverse. */
2083 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2085 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2087 /* Ignore indirect symbols. These are added by the versioning code. */
2088 if (h->root.type == bfd_link_hash_indirect)
2091 /* Ignore this if we won't export it. */
2092 if (!eif->info->export_dynamic && !h->dynamic)
2095 if (h->dynindx == -1
2096 && (h->def_regular || h->ref_regular)
2097 && ! bfd_hide_sym_by_version (eif->info->version_info,
2098 h->root.root.string))
2100 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2110 /* Look through the symbols which are defined in other shared
2111 libraries and referenced here. Update the list of version
2112 dependencies. This will be put into the .gnu.version_r section.
2113 This function is called via elf_link_hash_traverse. */
2116 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2119 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2120 Elf_Internal_Verneed *t;
2121 Elf_Internal_Vernaux *a;
2124 /* We only care about symbols defined in shared objects with version
2129 || h->verinfo.verdef == NULL
2130 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2131 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2134 /* See if we already know about this version. */
2135 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2139 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2142 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2143 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2149 /* This is a new version. Add it to tree we are building. */
2154 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2157 rinfo->failed = TRUE;
2161 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2162 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2163 elf_tdata (rinfo->info->output_bfd)->verref = t;
2167 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2170 rinfo->failed = TRUE;
2174 /* Note that we are copying a string pointer here, and testing it
2175 above. If bfd_elf_string_from_elf_section is ever changed to
2176 discard the string data when low in memory, this will have to be
2178 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2180 a->vna_flags = h->verinfo.verdef->vd_flags;
2181 a->vna_nextptr = t->vn_auxptr;
2183 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2186 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2193 /* Figure out appropriate versions for all the symbols. We may not
2194 have the version number script until we have read all of the input
2195 files, so until that point we don't know which symbols should be
2196 local. This function is called via elf_link_hash_traverse. */
2199 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2201 struct elf_info_failed *sinfo;
2202 struct bfd_link_info *info;
2203 const struct elf_backend_data *bed;
2204 struct elf_info_failed eif;
2207 sinfo = (struct elf_info_failed *) data;
2210 /* Fix the symbol flags. */
2213 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2216 sinfo->failed = TRUE;
2220 /* We only need version numbers for symbols defined in regular
2222 if (!h->def_regular)
2225 bed = get_elf_backend_data (info->output_bfd);
2226 p = strchr (h->root.root.string, ELF_VER_CHR);
2227 if (p != NULL && h->verinfo.vertree == NULL)
2229 struct bfd_elf_version_tree *t;
2232 if (*p == ELF_VER_CHR)
2235 /* If there is no version string, we can just return out. */
2239 /* Look for the version. If we find it, it is no longer weak. */
2240 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2242 if (strcmp (t->name, p) == 0)
2246 struct bfd_elf_version_expr *d;
2248 len = p - h->root.root.string;
2249 alc = (char *) bfd_malloc (len);
2252 sinfo->failed = TRUE;
2255 memcpy (alc, h->root.root.string, len - 1);
2256 alc[len - 1] = '\0';
2257 if (alc[len - 2] == ELF_VER_CHR)
2258 alc[len - 2] = '\0';
2260 h->verinfo.vertree = t;
2264 if (t->globals.list != NULL)
2265 d = (*t->match) (&t->globals, NULL, alc);
2267 /* See if there is anything to force this symbol to
2269 if (d == NULL && t->locals.list != NULL)
2271 d = (*t->match) (&t->locals, NULL, alc);
2274 && ! info->export_dynamic)
2275 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2283 /* If we are building an application, we need to create a
2284 version node for this version. */
2285 if (t == NULL && bfd_link_executable (info))
2287 struct bfd_elf_version_tree **pp;
2290 /* If we aren't going to export this symbol, we don't need
2291 to worry about it. */
2292 if (h->dynindx == -1)
2295 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2299 sinfo->failed = TRUE;
2304 t->name_indx = (unsigned int) -1;
2308 /* Don't count anonymous version tag. */
2309 if (sinfo->info->version_info != NULL
2310 && sinfo->info->version_info->vernum == 0)
2312 for (pp = &sinfo->info->version_info;
2316 t->vernum = version_index;
2320 h->verinfo.vertree = t;
2324 /* We could not find the version for a symbol when
2325 generating a shared archive. Return an error. */
2327 /* xgettext:c-format */
2328 (_("%B: version node not found for symbol %s"),
2329 info->output_bfd, h->root.root.string);
2330 bfd_set_error (bfd_error_bad_value);
2331 sinfo->failed = TRUE;
2336 /* If we don't have a version for this symbol, see if we can find
2338 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2343 = bfd_find_version_for_sym (sinfo->info->version_info,
2344 h->root.root.string, &hide);
2345 if (h->verinfo.vertree != NULL && hide)
2346 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2352 /* Read and swap the relocs from the section indicated by SHDR. This
2353 may be either a REL or a RELA section. The relocations are
2354 translated into RELA relocations and stored in INTERNAL_RELOCS,
2355 which should have already been allocated to contain enough space.
2356 The EXTERNAL_RELOCS are a buffer where the external form of the
2357 relocations should be stored.
2359 Returns FALSE if something goes wrong. */
2362 elf_link_read_relocs_from_section (bfd *abfd,
2364 Elf_Internal_Shdr *shdr,
2365 void *external_relocs,
2366 Elf_Internal_Rela *internal_relocs)
2368 const struct elf_backend_data *bed;
2369 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2370 const bfd_byte *erela;
2371 const bfd_byte *erelaend;
2372 Elf_Internal_Rela *irela;
2373 Elf_Internal_Shdr *symtab_hdr;
2376 /* Position ourselves at the start of the section. */
2377 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2380 /* Read the relocations. */
2381 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2384 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2385 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2387 bed = get_elf_backend_data (abfd);
2389 /* Convert the external relocations to the internal format. */
2390 if (shdr->sh_entsize == bed->s->sizeof_rel)
2391 swap_in = bed->s->swap_reloc_in;
2392 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2393 swap_in = bed->s->swap_reloca_in;
2396 bfd_set_error (bfd_error_wrong_format);
2400 erela = (const bfd_byte *) external_relocs;
2401 erelaend = erela + shdr->sh_size;
2402 irela = internal_relocs;
2403 while (erela < erelaend)
2407 (*swap_in) (abfd, erela, irela);
2408 r_symndx = ELF32_R_SYM (irela->r_info);
2409 if (bed->s->arch_size == 64)
2413 if ((size_t) r_symndx >= nsyms)
2416 /* xgettext:c-format */
2417 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2418 " for offset %#Lx in section `%A'"),
2419 abfd, r_symndx, (unsigned long) nsyms,
2420 irela->r_offset, sec);
2421 bfd_set_error (bfd_error_bad_value);
2425 else if (r_symndx != STN_UNDEF)
2428 /* xgettext:c-format */
2429 (_("%B: non-zero symbol index (%#Lx)"
2430 " for offset %#Lx in section `%A'"
2431 " when the object file has no symbol table"),
2433 irela->r_offset, sec);
2434 bfd_set_error (bfd_error_bad_value);
2437 irela += bed->s->int_rels_per_ext_rel;
2438 erela += shdr->sh_entsize;
2444 /* Read and swap the relocs for a section O. They may have been
2445 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2446 not NULL, they are used as buffers to read into. They are known to
2447 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2448 the return value is allocated using either malloc or bfd_alloc,
2449 according to the KEEP_MEMORY argument. If O has two relocation
2450 sections (both REL and RELA relocations), then the REL_HDR
2451 relocations will appear first in INTERNAL_RELOCS, followed by the
2452 RELA_HDR relocations. */
2455 _bfd_elf_link_read_relocs (bfd *abfd,
2457 void *external_relocs,
2458 Elf_Internal_Rela *internal_relocs,
2459 bfd_boolean keep_memory)
2461 void *alloc1 = NULL;
2462 Elf_Internal_Rela *alloc2 = NULL;
2463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2464 struct bfd_elf_section_data *esdo = elf_section_data (o);
2465 Elf_Internal_Rela *internal_rela_relocs;
2467 if (esdo->relocs != NULL)
2468 return esdo->relocs;
2470 if (o->reloc_count == 0)
2473 if (internal_relocs == NULL)
2477 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2479 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2481 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2482 if (internal_relocs == NULL)
2486 if (external_relocs == NULL)
2488 bfd_size_type size = 0;
2491 size += esdo->rel.hdr->sh_size;
2493 size += esdo->rela.hdr->sh_size;
2495 alloc1 = bfd_malloc (size);
2498 external_relocs = alloc1;
2501 internal_rela_relocs = internal_relocs;
2504 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2508 external_relocs = (((bfd_byte *) external_relocs)
2509 + esdo->rel.hdr->sh_size);
2510 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2511 * bed->s->int_rels_per_ext_rel);
2515 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2517 internal_rela_relocs)))
2520 /* Cache the results for next time, if we can. */
2522 esdo->relocs = internal_relocs;
2527 /* Don't free alloc2, since if it was allocated we are passing it
2528 back (under the name of internal_relocs). */
2530 return internal_relocs;
2538 bfd_release (abfd, alloc2);
2545 /* Compute the size of, and allocate space for, REL_HDR which is the
2546 section header for a section containing relocations for O. */
2549 _bfd_elf_link_size_reloc_section (bfd *abfd,
2550 struct bfd_elf_section_reloc_data *reldata)
2552 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2554 /* That allows us to calculate the size of the section. */
2555 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2557 /* The contents field must last into write_object_contents, so we
2558 allocate it with bfd_alloc rather than malloc. Also since we
2559 cannot be sure that the contents will actually be filled in,
2560 we zero the allocated space. */
2561 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2562 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2565 if (reldata->hashes == NULL && reldata->count)
2567 struct elf_link_hash_entry **p;
2569 p = ((struct elf_link_hash_entry **)
2570 bfd_zmalloc (reldata->count * sizeof (*p)));
2574 reldata->hashes = p;
2580 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2581 originated from the section given by INPUT_REL_HDR) to the
2585 _bfd_elf_link_output_relocs (bfd *output_bfd,
2586 asection *input_section,
2587 Elf_Internal_Shdr *input_rel_hdr,
2588 Elf_Internal_Rela *internal_relocs,
2589 struct elf_link_hash_entry **rel_hash
2592 Elf_Internal_Rela *irela;
2593 Elf_Internal_Rela *irelaend;
2595 struct bfd_elf_section_reloc_data *output_reldata;
2596 asection *output_section;
2597 const struct elf_backend_data *bed;
2598 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2599 struct bfd_elf_section_data *esdo;
2601 output_section = input_section->output_section;
2603 bed = get_elf_backend_data (output_bfd);
2604 esdo = elf_section_data (output_section);
2605 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2607 output_reldata = &esdo->rel;
2608 swap_out = bed->s->swap_reloc_out;
2610 else if (esdo->rela.hdr
2611 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2613 output_reldata = &esdo->rela;
2614 swap_out = bed->s->swap_reloca_out;
2619 /* xgettext:c-format */
2620 (_("%B: relocation size mismatch in %B section %A"),
2621 output_bfd, input_section->owner, input_section);
2622 bfd_set_error (bfd_error_wrong_format);
2626 erel = output_reldata->hdr->contents;
2627 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2628 irela = internal_relocs;
2629 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2630 * bed->s->int_rels_per_ext_rel);
2631 while (irela < irelaend)
2633 (*swap_out) (output_bfd, irela, erel);
2634 irela += bed->s->int_rels_per_ext_rel;
2635 erel += input_rel_hdr->sh_entsize;
2638 /* Bump the counter, so that we know where to add the next set of
2640 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2645 /* Make weak undefined symbols in PIE dynamic. */
2648 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2649 struct elf_link_hash_entry *h)
2651 if (bfd_link_pie (info)
2653 && h->root.type == bfd_link_hash_undefweak)
2654 return bfd_elf_link_record_dynamic_symbol (info, h);
2659 /* Fix up the flags for a symbol. This handles various cases which
2660 can only be fixed after all the input files are seen. This is
2661 currently called by both adjust_dynamic_symbol and
2662 assign_sym_version, which is unnecessary but perhaps more robust in
2663 the face of future changes. */
2666 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2667 struct elf_info_failed *eif)
2669 const struct elf_backend_data *bed;
2671 /* If this symbol was mentioned in a non-ELF file, try to set
2672 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2673 permit a non-ELF file to correctly refer to a symbol defined in
2674 an ELF dynamic object. */
2677 while (h->root.type == bfd_link_hash_indirect)
2678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2680 if (h->root.type != bfd_link_hash_defined
2681 && h->root.type != bfd_link_hash_defweak)
2684 h->ref_regular_nonweak = 1;
2688 if (h->root.u.def.section->owner != NULL
2689 && (bfd_get_flavour (h->root.u.def.section->owner)
2690 == bfd_target_elf_flavour))
2693 h->ref_regular_nonweak = 1;
2699 if (h->dynindx == -1
2703 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2712 /* Unfortunately, NON_ELF is only correct if the symbol
2713 was first seen in a non-ELF file. Fortunately, if the symbol
2714 was first seen in an ELF file, we're probably OK unless the
2715 symbol was defined in a non-ELF file. Catch that case here.
2716 FIXME: We're still in trouble if the symbol was first seen in
2717 a dynamic object, and then later in a non-ELF regular object. */
2718 if ((h->root.type == bfd_link_hash_defined
2719 || h->root.type == bfd_link_hash_defweak)
2721 && (h->root.u.def.section->owner != NULL
2722 ? (bfd_get_flavour (h->root.u.def.section->owner)
2723 != bfd_target_elf_flavour)
2724 : (bfd_is_abs_section (h->root.u.def.section)
2725 && !h->def_dynamic)))
2729 /* Backend specific symbol fixup. */
2730 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2731 if (bed->elf_backend_fixup_symbol
2732 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2735 /* If this is a final link, and the symbol was defined as a common
2736 symbol in a regular object file, and there was no definition in
2737 any dynamic object, then the linker will have allocated space for
2738 the symbol in a common section but the DEF_REGULAR
2739 flag will not have been set. */
2740 if (h->root.type == bfd_link_hash_defined
2744 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2747 /* If a weak undefined symbol has non-default visibility, we also
2748 hide it from the dynamic linker. */
2749 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2750 && h->root.type == bfd_link_hash_undefweak)
2751 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2753 /* A hidden versioned symbol in executable should be forced local if
2754 it is is locally defined, not referenced by shared library and not
2756 else if (bfd_link_executable (eif->info)
2757 && h->versioned == versioned_hidden
2758 && !eif->info->export_dynamic
2762 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2764 /* If -Bsymbolic was used (which means to bind references to global
2765 symbols to the definition within the shared object), and this
2766 symbol was defined in a regular object, then it actually doesn't
2767 need a PLT entry. Likewise, if the symbol has non-default
2768 visibility. If the symbol has hidden or internal visibility, we
2769 will force it local. */
2770 else if (h->needs_plt
2771 && bfd_link_pic (eif->info)
2772 && is_elf_hash_table (eif->info->hash)
2773 && (SYMBOLIC_BIND (eif->info, h)
2774 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2777 bfd_boolean force_local;
2779 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2780 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2781 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2784 /* If this is a weak defined symbol in a dynamic object, and we know
2785 the real definition in the dynamic object, copy interesting flags
2786 over to the real definition. */
2787 if (h->is_weakalias)
2789 struct elf_link_hash_entry *def = weakdef (h);
2791 /* If the real definition is defined by a regular object file,
2792 don't do anything special. See the longer description in
2793 _bfd_elf_adjust_dynamic_symbol, below. */
2794 if (def->def_regular)
2797 while ((h = h->u.alias) != def)
2798 h->is_weakalias = 0;
2802 while (h->root.type == bfd_link_hash_indirect)
2803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2804 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2805 || h->root.type == bfd_link_hash_defweak);
2806 BFD_ASSERT (def->def_dynamic);
2807 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2808 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2815 /* Make the backend pick a good value for a dynamic symbol. This is
2816 called via elf_link_hash_traverse, and also calls itself
2820 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2822 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2823 struct elf_link_hash_table *htab;
2824 const struct elf_backend_data *bed;
2826 if (! is_elf_hash_table (eif->info->hash))
2829 /* Ignore indirect symbols. These are added by the versioning code. */
2830 if (h->root.type == bfd_link_hash_indirect)
2833 /* Fix the symbol flags. */
2834 if (! _bfd_elf_fix_symbol_flags (h, eif))
2837 htab = elf_hash_table (eif->info);
2838 bed = get_elf_backend_data (htab->dynobj);
2840 if (h->root.type == bfd_link_hash_undefweak)
2842 if (eif->info->dynamic_undefined_weak == 0)
2843 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2844 else if (eif->info->dynamic_undefined_weak > 0
2846 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2847 && !bfd_hide_sym_by_version (eif->info->version_info,
2848 h->root.root.string))
2850 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2858 /* If this symbol does not require a PLT entry, and it is not
2859 defined by a dynamic object, or is not referenced by a regular
2860 object, ignore it. We do have to handle a weak defined symbol,
2861 even if no regular object refers to it, if we decided to add it
2862 to the dynamic symbol table. FIXME: Do we normally need to worry
2863 about symbols which are defined by one dynamic object and
2864 referenced by another one? */
2866 && h->type != STT_GNU_IFUNC
2870 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2872 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2876 /* If we've already adjusted this symbol, don't do it again. This
2877 can happen via a recursive call. */
2878 if (h->dynamic_adjusted)
2881 /* Don't look at this symbol again. Note that we must set this
2882 after checking the above conditions, because we may look at a
2883 symbol once, decide not to do anything, and then get called
2884 recursively later after REF_REGULAR is set below. */
2885 h->dynamic_adjusted = 1;
2887 /* If this is a weak definition, and we know a real definition, and
2888 the real symbol is not itself defined by a regular object file,
2889 then get a good value for the real definition. We handle the
2890 real symbol first, for the convenience of the backend routine.
2892 Note that there is a confusing case here. If the real definition
2893 is defined by a regular object file, we don't get the real symbol
2894 from the dynamic object, but we do get the weak symbol. If the
2895 processor backend uses a COPY reloc, then if some routine in the
2896 dynamic object changes the real symbol, we will not see that
2897 change in the corresponding weak symbol. This is the way other
2898 ELF linkers work as well, and seems to be a result of the shared
2901 I will clarify this issue. Most SVR4 shared libraries define the
2902 variable _timezone and define timezone as a weak synonym. The
2903 tzset call changes _timezone. If you write
2904 extern int timezone;
2906 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2907 you might expect that, since timezone is a synonym for _timezone,
2908 the same number will print both times. However, if the processor
2909 backend uses a COPY reloc, then actually timezone will be copied
2910 into your process image, and, since you define _timezone
2911 yourself, _timezone will not. Thus timezone and _timezone will
2912 wind up at different memory locations. The tzset call will set
2913 _timezone, leaving timezone unchanged. */
2915 if (h->is_weakalias)
2917 struct elf_link_hash_entry *def = weakdef (h);
2919 /* If we get to this point, there is an implicit reference to
2920 the alias by a regular object file via the weak symbol H. */
2921 def->ref_regular = 1;
2923 /* Ensure that the backend adjust_dynamic_symbol function sees
2924 the strong alias before H by recursively calling ourselves. */
2925 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2929 /* If a symbol has no type and no size and does not require a PLT
2930 entry, then we are probably about to do the wrong thing here: we
2931 are probably going to create a COPY reloc for an empty object.
2932 This case can arise when a shared object is built with assembly
2933 code, and the assembly code fails to set the symbol type. */
2935 && h->type == STT_NOTYPE
2938 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2939 h->root.root.string);
2941 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2950 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2954 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2955 struct elf_link_hash_entry *h,
2958 unsigned int power_of_two;
2960 asection *sec = h->root.u.def.section;
2962 /* The section alignment of the definition is the maximum alignment
2963 requirement of symbols defined in the section. Since we don't
2964 know the symbol alignment requirement, we start with the
2965 maximum alignment and check low bits of the symbol address
2966 for the minimum alignment. */
2967 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2968 mask = ((bfd_vma) 1 << power_of_two) - 1;
2969 while ((h->root.u.def.value & mask) != 0)
2975 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2978 /* Adjust the section alignment if needed. */
2979 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2984 /* We make sure that the symbol will be aligned properly. */
2985 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2987 /* Define the symbol as being at this point in DYNBSS. */
2988 h->root.u.def.section = dynbss;
2989 h->root.u.def.value = dynbss->size;
2991 /* Increment the size of DYNBSS to make room for the symbol. */
2992 dynbss->size += h->size;
2994 /* No error if extern_protected_data is true. */
2995 if (h->protected_def
2996 && (!info->extern_protected_data
2997 || (info->extern_protected_data < 0
2998 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2999 info->callbacks->einfo
3000 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3001 h->root.root.string);
3006 /* Adjust all external symbols pointing into SEC_MERGE sections
3007 to reflect the object merging within the sections. */
3010 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3014 if ((h->root.type == bfd_link_hash_defined
3015 || h->root.type == bfd_link_hash_defweak)
3016 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3017 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3019 bfd *output_bfd = (bfd *) data;
3021 h->root.u.def.value =
3022 _bfd_merged_section_offset (output_bfd,
3023 &h->root.u.def.section,
3024 elf_section_data (sec)->sec_info,
3025 h->root.u.def.value);
3031 /* Returns false if the symbol referred to by H should be considered
3032 to resolve local to the current module, and true if it should be
3033 considered to bind dynamically. */
3036 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3037 struct bfd_link_info *info,
3038 bfd_boolean not_local_protected)
3040 bfd_boolean binding_stays_local_p;
3041 const struct elf_backend_data *bed;
3042 struct elf_link_hash_table *hash_table;
3047 while (h->root.type == bfd_link_hash_indirect
3048 || h->root.type == bfd_link_hash_warning)
3049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3051 /* If it was forced local, then clearly it's not dynamic. */
3052 if (h->dynindx == -1)
3054 if (h->forced_local)
3057 /* Identify the cases where name binding rules say that a
3058 visible symbol resolves locally. */
3059 binding_stays_local_p = (bfd_link_executable (info)
3060 || SYMBOLIC_BIND (info, h));
3062 switch (ELF_ST_VISIBILITY (h->other))
3069 hash_table = elf_hash_table (info);
3070 if (!is_elf_hash_table (hash_table))
3073 bed = get_elf_backend_data (hash_table->dynobj);
3075 /* Proper resolution for function pointer equality may require
3076 that these symbols perhaps be resolved dynamically, even though
3077 we should be resolving them to the current module. */
3078 if (!not_local_protected || !bed->is_function_type (h->type))
3079 binding_stays_local_p = TRUE;
3086 /* If it isn't defined locally, then clearly it's dynamic. */
3087 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3090 /* Otherwise, the symbol is dynamic if binding rules don't tell
3091 us that it remains local. */
3092 return !binding_stays_local_p;
3095 /* Return true if the symbol referred to by H should be considered
3096 to resolve local to the current module, and false otherwise. Differs
3097 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3098 undefined symbols. The two functions are virtually identical except
3099 for the place where dynindx == -1 is tested. If that test is true,
3100 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3101 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3103 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3104 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3105 treatment of undefined weak symbols. For those that do not make
3106 undefined weak symbols dynamic, both functions may return false. */
3109 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3110 struct bfd_link_info *info,
3111 bfd_boolean local_protected)
3113 const struct elf_backend_data *bed;
3114 struct elf_link_hash_table *hash_table;
3116 /* If it's a local sym, of course we resolve locally. */
3120 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3121 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3122 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3125 /* Forced local symbols resolve locally. */
3126 if (h->forced_local)
3129 /* Common symbols that become definitions don't get the DEF_REGULAR
3130 flag set, so test it first, and don't bail out. */
3131 if (ELF_COMMON_DEF_P (h))
3133 /* If we don't have a definition in a regular file, then we can't
3134 resolve locally. The sym is either undefined or dynamic. */
3135 else if (!h->def_regular)
3138 /* Non-dynamic symbols resolve locally. */
3139 if (h->dynindx == -1)
3142 /* At this point, we know the symbol is defined and dynamic. In an
3143 executable it must resolve locally, likewise when building symbolic
3144 shared libraries. */
3145 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3148 /* Now deal with defined dynamic symbols in shared libraries. Ones
3149 with default visibility might not resolve locally. */
3150 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3153 hash_table = elf_hash_table (info);
3154 if (!is_elf_hash_table (hash_table))
3157 bed = get_elf_backend_data (hash_table->dynobj);
3159 /* If extern_protected_data is false, STV_PROTECTED non-function
3160 symbols are local. */
3161 if ((!info->extern_protected_data
3162 || (info->extern_protected_data < 0
3163 && !bed->extern_protected_data))
3164 && !bed->is_function_type (h->type))
3167 /* Function pointer equality tests may require that STV_PROTECTED
3168 symbols be treated as dynamic symbols. If the address of a
3169 function not defined in an executable is set to that function's
3170 plt entry in the executable, then the address of the function in
3171 a shared library must also be the plt entry in the executable. */
3172 return local_protected;
3175 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3176 aligned. Returns the first TLS output section. */
3178 struct bfd_section *
3179 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3181 struct bfd_section *sec, *tls;
3182 unsigned int align = 0;
3184 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3185 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3189 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3190 if (sec->alignment_power > align)
3191 align = sec->alignment_power;
3193 elf_hash_table (info)->tls_sec = tls;
3195 /* Ensure the alignment of the first section is the largest alignment,
3196 so that the tls segment starts aligned. */
3198 tls->alignment_power = align;
3203 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3205 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3206 Elf_Internal_Sym *sym)
3208 const struct elf_backend_data *bed;
3210 /* Local symbols do not count, but target specific ones might. */
3211 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3212 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3215 bed = get_elf_backend_data (abfd);
3216 /* Function symbols do not count. */
3217 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3220 /* If the section is undefined, then so is the symbol. */
3221 if (sym->st_shndx == SHN_UNDEF)
3224 /* If the symbol is defined in the common section, then
3225 it is a common definition and so does not count. */
3226 if (bed->common_definition (sym))
3229 /* If the symbol is in a target specific section then we
3230 must rely upon the backend to tell us what it is. */
3231 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3232 /* FIXME - this function is not coded yet:
3234 return _bfd_is_global_symbol_definition (abfd, sym);
3236 Instead for now assume that the definition is not global,
3237 Even if this is wrong, at least the linker will behave
3238 in the same way that it used to do. */
3244 /* Search the symbol table of the archive element of the archive ABFD
3245 whose archive map contains a mention of SYMDEF, and determine if
3246 the symbol is defined in this element. */
3248 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3250 Elf_Internal_Shdr * hdr;
3254 Elf_Internal_Sym *isymbuf;
3255 Elf_Internal_Sym *isym;
3256 Elf_Internal_Sym *isymend;
3259 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3263 if (! bfd_check_format (abfd, bfd_object))
3266 /* Select the appropriate symbol table. If we don't know if the
3267 object file is an IR object, give linker LTO plugin a chance to
3268 get the correct symbol table. */
3269 if (abfd->plugin_format == bfd_plugin_yes
3270 #if BFD_SUPPORTS_PLUGINS
3271 || (abfd->plugin_format == bfd_plugin_unknown
3272 && bfd_link_plugin_object_p (abfd))
3276 /* Use the IR symbol table if the object has been claimed by
3278 abfd = abfd->plugin_dummy_bfd;
3279 hdr = &elf_tdata (abfd)->symtab_hdr;
3281 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3282 hdr = &elf_tdata (abfd)->symtab_hdr;
3284 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3286 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3288 /* The sh_info field of the symtab header tells us where the
3289 external symbols start. We don't care about the local symbols. */
3290 if (elf_bad_symtab (abfd))
3292 extsymcount = symcount;
3297 extsymcount = symcount - hdr->sh_info;
3298 extsymoff = hdr->sh_info;
3301 if (extsymcount == 0)
3304 /* Read in the symbol table. */
3305 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3307 if (isymbuf == NULL)
3310 /* Scan the symbol table looking for SYMDEF. */
3312 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3316 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3321 if (strcmp (name, symdef->name) == 0)
3323 result = is_global_data_symbol_definition (abfd, isym);
3333 /* Add an entry to the .dynamic table. */
3336 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3340 struct elf_link_hash_table *hash_table;
3341 const struct elf_backend_data *bed;
3343 bfd_size_type newsize;
3344 bfd_byte *newcontents;
3345 Elf_Internal_Dyn dyn;
3347 hash_table = elf_hash_table (info);
3348 if (! is_elf_hash_table (hash_table))
3351 bed = get_elf_backend_data (hash_table->dynobj);
3352 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3353 BFD_ASSERT (s != NULL);
3355 newsize = s->size + bed->s->sizeof_dyn;
3356 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3357 if (newcontents == NULL)
3361 dyn.d_un.d_val = val;
3362 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3365 s->contents = newcontents;
3370 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3371 otherwise just check whether one already exists. Returns -1 on error,
3372 1 if a DT_NEEDED tag already exists, and 0 on success. */
3375 elf_add_dt_needed_tag (bfd *abfd,
3376 struct bfd_link_info *info,
3380 struct elf_link_hash_table *hash_table;
3383 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3386 hash_table = elf_hash_table (info);
3387 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3388 if (strindex == (size_t) -1)
3391 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3394 const struct elf_backend_data *bed;
3397 bed = get_elf_backend_data (hash_table->dynobj);
3398 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3400 for (extdyn = sdyn->contents;
3401 extdyn < sdyn->contents + sdyn->size;
3402 extdyn += bed->s->sizeof_dyn)
3404 Elf_Internal_Dyn dyn;
3406 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3407 if (dyn.d_tag == DT_NEEDED
3408 && dyn.d_un.d_val == strindex)
3410 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3418 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3421 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3425 /* We were just checking for existence of the tag. */
3426 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3431 /* Return true if SONAME is on the needed list between NEEDED and STOP
3432 (or the end of list if STOP is NULL), and needed by a library that
3436 on_needed_list (const char *soname,
3437 struct bfd_link_needed_list *needed,
3438 struct bfd_link_needed_list *stop)
3440 struct bfd_link_needed_list *look;
3441 for (look = needed; look != stop; look = look->next)
3442 if (strcmp (soname, look->name) == 0
3443 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3444 /* If needed by a library that itself is not directly
3445 needed, recursively check whether that library is
3446 indirectly needed. Since we add DT_NEEDED entries to
3447 the end of the list, library dependencies appear after
3448 the library. Therefore search prior to the current
3449 LOOK, preventing possible infinite recursion. */
3450 || on_needed_list (elf_dt_name (look->by), needed, look)))
3456 /* Sort symbol by value, section, and size. */
3458 elf_sort_symbol (const void *arg1, const void *arg2)
3460 const struct elf_link_hash_entry *h1;
3461 const struct elf_link_hash_entry *h2;
3462 bfd_signed_vma vdiff;
3464 h1 = *(const struct elf_link_hash_entry **) arg1;
3465 h2 = *(const struct elf_link_hash_entry **) arg2;
3466 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3468 return vdiff > 0 ? 1 : -1;
3471 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3473 return sdiff > 0 ? 1 : -1;
3475 vdiff = h1->size - h2->size;
3476 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3479 /* This function is used to adjust offsets into .dynstr for
3480 dynamic symbols. This is called via elf_link_hash_traverse. */
3483 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3485 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3487 if (h->dynindx != -1)
3488 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3492 /* Assign string offsets in .dynstr, update all structures referencing
3496 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3498 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3499 struct elf_link_local_dynamic_entry *entry;
3500 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3501 bfd *dynobj = hash_table->dynobj;
3504 const struct elf_backend_data *bed;
3507 _bfd_elf_strtab_finalize (dynstr);
3508 size = _bfd_elf_strtab_size (dynstr);
3510 bed = get_elf_backend_data (dynobj);
3511 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3512 BFD_ASSERT (sdyn != NULL);
3514 /* Update all .dynamic entries referencing .dynstr strings. */
3515 for (extdyn = sdyn->contents;
3516 extdyn < sdyn->contents + sdyn->size;
3517 extdyn += bed->s->sizeof_dyn)
3519 Elf_Internal_Dyn dyn;
3521 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3525 dyn.d_un.d_val = size;
3535 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3540 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3543 /* Now update local dynamic symbols. */
3544 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3545 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3546 entry->isym.st_name);
3548 /* And the rest of dynamic symbols. */
3549 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3551 /* Adjust version definitions. */
3552 if (elf_tdata (output_bfd)->cverdefs)
3557 Elf_Internal_Verdef def;
3558 Elf_Internal_Verdaux defaux;
3560 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3564 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3566 p += sizeof (Elf_External_Verdef);
3567 if (def.vd_aux != sizeof (Elf_External_Verdef))
3569 for (i = 0; i < def.vd_cnt; ++i)
3571 _bfd_elf_swap_verdaux_in (output_bfd,
3572 (Elf_External_Verdaux *) p, &defaux);
3573 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3575 _bfd_elf_swap_verdaux_out (output_bfd,
3576 &defaux, (Elf_External_Verdaux *) p);
3577 p += sizeof (Elf_External_Verdaux);
3580 while (def.vd_next);
3583 /* Adjust version references. */
3584 if (elf_tdata (output_bfd)->verref)
3589 Elf_Internal_Verneed need;
3590 Elf_Internal_Vernaux needaux;
3592 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3596 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3598 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3599 _bfd_elf_swap_verneed_out (output_bfd, &need,
3600 (Elf_External_Verneed *) p);
3601 p += sizeof (Elf_External_Verneed);
3602 for (i = 0; i < need.vn_cnt; ++i)
3604 _bfd_elf_swap_vernaux_in (output_bfd,
3605 (Elf_External_Vernaux *) p, &needaux);
3606 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3608 _bfd_elf_swap_vernaux_out (output_bfd,
3610 (Elf_External_Vernaux *) p);
3611 p += sizeof (Elf_External_Vernaux);
3614 while (need.vn_next);
3620 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3621 The default is to only match when the INPUT and OUTPUT are exactly
3625 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3626 const bfd_target *output)
3628 return input == output;
3631 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3632 This version is used when different targets for the same architecture
3633 are virtually identical. */
3636 _bfd_elf_relocs_compatible (const bfd_target *input,
3637 const bfd_target *output)
3639 const struct elf_backend_data *obed, *ibed;
3641 if (input == output)
3644 ibed = xvec_get_elf_backend_data (input);
3645 obed = xvec_get_elf_backend_data (output);
3647 if (ibed->arch != obed->arch)
3650 /* If both backends are using this function, deem them compatible. */
3651 return ibed->relocs_compatible == obed->relocs_compatible;
3654 /* Make a special call to the linker "notice" function to tell it that
3655 we are about to handle an as-needed lib, or have finished
3656 processing the lib. */
3659 _bfd_elf_notice_as_needed (bfd *ibfd,
3660 struct bfd_link_info *info,
3661 enum notice_asneeded_action act)
3663 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3666 /* Check relocations an ELF object file. */
3669 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3671 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3672 struct elf_link_hash_table *htab = elf_hash_table (info);
3674 /* If this object is the same format as the output object, and it is
3675 not a shared library, then let the backend look through the
3678 This is required to build global offset table entries and to
3679 arrange for dynamic relocs. It is not required for the
3680 particular common case of linking non PIC code, even when linking
3681 against shared libraries, but unfortunately there is no way of
3682 knowing whether an object file has been compiled PIC or not.
3683 Looking through the relocs is not particularly time consuming.
3684 The problem is that we must either (1) keep the relocs in memory,
3685 which causes the linker to require additional runtime memory or
3686 (2) read the relocs twice from the input file, which wastes time.
3687 This would be a good case for using mmap.
3689 I have no idea how to handle linking PIC code into a file of a
3690 different format. It probably can't be done. */
3691 if ((abfd->flags & DYNAMIC) == 0
3692 && is_elf_hash_table (htab)
3693 && bed->check_relocs != NULL
3694 && elf_object_id (abfd) == elf_hash_table_id (htab)
3695 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3699 for (o = abfd->sections; o != NULL; o = o->next)
3701 Elf_Internal_Rela *internal_relocs;
3704 /* Don't check relocations in excluded sections. */
3705 if ((o->flags & SEC_RELOC) == 0
3706 || (o->flags & SEC_EXCLUDE) != 0
3707 || o->reloc_count == 0
3708 || ((info->strip == strip_all || info->strip == strip_debugger)
3709 && (o->flags & SEC_DEBUGGING) != 0)
3710 || bfd_is_abs_section (o->output_section))
3713 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3715 if (internal_relocs == NULL)
3718 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3720 if (elf_section_data (o)->relocs != internal_relocs)
3721 free (internal_relocs);
3731 /* Add symbols from an ELF object file to the linker hash table. */
3734 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3736 Elf_Internal_Ehdr *ehdr;
3737 Elf_Internal_Shdr *hdr;
3741 struct elf_link_hash_entry **sym_hash;
3742 bfd_boolean dynamic;
3743 Elf_External_Versym *extversym = NULL;
3744 Elf_External_Versym *ever;
3745 struct elf_link_hash_entry *weaks;
3746 struct elf_link_hash_entry **nondeflt_vers = NULL;
3747 size_t nondeflt_vers_cnt = 0;
3748 Elf_Internal_Sym *isymbuf = NULL;
3749 Elf_Internal_Sym *isym;
3750 Elf_Internal_Sym *isymend;
3751 const struct elf_backend_data *bed;
3752 bfd_boolean add_needed;
3753 struct elf_link_hash_table *htab;
3755 void *alloc_mark = NULL;
3756 struct bfd_hash_entry **old_table = NULL;
3757 unsigned int old_size = 0;
3758 unsigned int old_count = 0;
3759 void *old_tab = NULL;
3761 struct bfd_link_hash_entry *old_undefs = NULL;
3762 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3763 void *old_strtab = NULL;
3766 bfd_boolean just_syms;
3768 htab = elf_hash_table (info);
3769 bed = get_elf_backend_data (abfd);
3771 if ((abfd->flags & DYNAMIC) == 0)
3777 /* You can't use -r against a dynamic object. Also, there's no
3778 hope of using a dynamic object which does not exactly match
3779 the format of the output file. */
3780 if (bfd_link_relocatable (info)
3781 || !is_elf_hash_table (htab)
3782 || info->output_bfd->xvec != abfd->xvec)
3784 if (bfd_link_relocatable (info))
3785 bfd_set_error (bfd_error_invalid_operation);
3787 bfd_set_error (bfd_error_wrong_format);
3792 ehdr = elf_elfheader (abfd);
3793 if (info->warn_alternate_em
3794 && bed->elf_machine_code != ehdr->e_machine
3795 && ((bed->elf_machine_alt1 != 0
3796 && ehdr->e_machine == bed->elf_machine_alt1)
3797 || (bed->elf_machine_alt2 != 0
3798 && ehdr->e_machine == bed->elf_machine_alt2)))
3799 info->callbacks->einfo
3800 /* xgettext:c-format */
3801 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3802 ehdr->e_machine, abfd, bed->elf_machine_code);
3804 /* As a GNU extension, any input sections which are named
3805 .gnu.warning.SYMBOL are treated as warning symbols for the given
3806 symbol. This differs from .gnu.warning sections, which generate
3807 warnings when they are included in an output file. */
3808 /* PR 12761: Also generate this warning when building shared libraries. */
3809 for (s = abfd->sections; s != NULL; s = s->next)
3813 name = bfd_get_section_name (abfd, s);
3814 if (CONST_STRNEQ (name, ".gnu.warning."))
3819 name += sizeof ".gnu.warning." - 1;
3821 /* If this is a shared object, then look up the symbol
3822 in the hash table. If it is there, and it is already
3823 been defined, then we will not be using the entry
3824 from this shared object, so we don't need to warn.
3825 FIXME: If we see the definition in a regular object
3826 later on, we will warn, but we shouldn't. The only
3827 fix is to keep track of what warnings we are supposed
3828 to emit, and then handle them all at the end of the
3832 struct elf_link_hash_entry *h;
3834 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3836 /* FIXME: What about bfd_link_hash_common? */
3838 && (h->root.type == bfd_link_hash_defined
3839 || h->root.type == bfd_link_hash_defweak))
3844 msg = (char *) bfd_alloc (abfd, sz + 1);
3848 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3853 if (! (_bfd_generic_link_add_one_symbol
3854 (info, abfd, name, BSF_WARNING, s, 0, msg,
3855 FALSE, bed->collect, NULL)))
3858 if (bfd_link_executable (info))
3860 /* Clobber the section size so that the warning does
3861 not get copied into the output file. */
3864 /* Also set SEC_EXCLUDE, so that symbols defined in
3865 the warning section don't get copied to the output. */
3866 s->flags |= SEC_EXCLUDE;
3871 just_syms = ((s = abfd->sections) != NULL
3872 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3877 /* If we are creating a shared library, create all the dynamic
3878 sections immediately. We need to attach them to something,
3879 so we attach them to this BFD, provided it is the right
3880 format and is not from ld --just-symbols. Always create the
3881 dynamic sections for -E/--dynamic-list. FIXME: If there
3882 are no input BFD's of the same format as the output, we can't
3883 make a shared library. */
3885 && (bfd_link_pic (info)
3886 || (!bfd_link_relocatable (info)
3888 && (info->export_dynamic || info->dynamic)))
3889 && is_elf_hash_table (htab)
3890 && info->output_bfd->xvec == abfd->xvec
3891 && !htab->dynamic_sections_created)
3893 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3897 else if (!is_elf_hash_table (htab))
3901 const char *soname = NULL;
3903 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3904 const Elf_Internal_Phdr *phdr;
3907 /* ld --just-symbols and dynamic objects don't mix very well.
3908 ld shouldn't allow it. */
3912 /* If this dynamic lib was specified on the command line with
3913 --as-needed in effect, then we don't want to add a DT_NEEDED
3914 tag unless the lib is actually used. Similary for libs brought
3915 in by another lib's DT_NEEDED. When --no-add-needed is used
3916 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3917 any dynamic library in DT_NEEDED tags in the dynamic lib at
3919 add_needed = (elf_dyn_lib_class (abfd)
3920 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3921 | DYN_NO_NEEDED)) == 0;
3923 s = bfd_get_section_by_name (abfd, ".dynamic");
3928 unsigned int elfsec;
3929 unsigned long shlink;
3931 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3938 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3939 if (elfsec == SHN_BAD)
3940 goto error_free_dyn;
3941 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3943 for (extdyn = dynbuf;
3944 extdyn < dynbuf + s->size;
3945 extdyn += bed->s->sizeof_dyn)
3947 Elf_Internal_Dyn dyn;
3949 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3950 if (dyn.d_tag == DT_SONAME)
3952 unsigned int tagv = dyn.d_un.d_val;
3953 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3955 goto error_free_dyn;
3957 if (dyn.d_tag == DT_NEEDED)
3959 struct bfd_link_needed_list *n, **pn;
3961 unsigned int tagv = dyn.d_un.d_val;
3963 amt = sizeof (struct bfd_link_needed_list);
3964 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3965 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3966 if (n == NULL || fnm == NULL)
3967 goto error_free_dyn;
3968 amt = strlen (fnm) + 1;
3969 anm = (char *) bfd_alloc (abfd, amt);
3971 goto error_free_dyn;
3972 memcpy (anm, fnm, amt);
3976 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3980 if (dyn.d_tag == DT_RUNPATH)
3982 struct bfd_link_needed_list *n, **pn;
3984 unsigned int tagv = dyn.d_un.d_val;
3986 amt = sizeof (struct bfd_link_needed_list);
3987 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3988 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3989 if (n == NULL || fnm == NULL)
3990 goto error_free_dyn;
3991 amt = strlen (fnm) + 1;
3992 anm = (char *) bfd_alloc (abfd, amt);
3994 goto error_free_dyn;
3995 memcpy (anm, fnm, amt);
3999 for (pn = & runpath;
4005 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4006 if (!runpath && dyn.d_tag == DT_RPATH)
4008 struct bfd_link_needed_list *n, **pn;
4010 unsigned int tagv = dyn.d_un.d_val;
4012 amt = sizeof (struct bfd_link_needed_list);
4013 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4014 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4015 if (n == NULL || fnm == NULL)
4016 goto error_free_dyn;
4017 amt = strlen (fnm) + 1;
4018 anm = (char *) bfd_alloc (abfd, amt);
4020 goto error_free_dyn;
4021 memcpy (anm, fnm, amt);
4031 if (dyn.d_tag == DT_AUDIT)
4033 unsigned int tagv = dyn.d_un.d_val;
4034 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4041 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4042 frees all more recently bfd_alloc'd blocks as well. */
4048 struct bfd_link_needed_list **pn;
4049 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4054 /* If we have a PT_GNU_RELRO program header, mark as read-only
4055 all sections contained fully therein. This makes relro
4056 shared library sections appear as they will at run-time. */
4057 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4058 while (--phdr >= elf_tdata (abfd)->phdr)
4059 if (phdr->p_type == PT_GNU_RELRO)
4061 for (s = abfd->sections; s != NULL; s = s->next)
4062 if ((s->flags & SEC_ALLOC) != 0
4063 && s->vma >= phdr->p_vaddr
4064 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4065 s->flags |= SEC_READONLY;
4069 /* We do not want to include any of the sections in a dynamic
4070 object in the output file. We hack by simply clobbering the
4071 list of sections in the BFD. This could be handled more
4072 cleanly by, say, a new section flag; the existing
4073 SEC_NEVER_LOAD flag is not the one we want, because that one
4074 still implies that the section takes up space in the output
4076 bfd_section_list_clear (abfd);
4078 /* Find the name to use in a DT_NEEDED entry that refers to this
4079 object. If the object has a DT_SONAME entry, we use it.
4080 Otherwise, if the generic linker stuck something in
4081 elf_dt_name, we use that. Otherwise, we just use the file
4083 if (soname == NULL || *soname == '\0')
4085 soname = elf_dt_name (abfd);
4086 if (soname == NULL || *soname == '\0')
4087 soname = bfd_get_filename (abfd);
4090 /* Save the SONAME because sometimes the linker emulation code
4091 will need to know it. */
4092 elf_dt_name (abfd) = soname;
4094 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4098 /* If we have already included this dynamic object in the
4099 link, just ignore it. There is no reason to include a
4100 particular dynamic object more than once. */
4104 /* Save the DT_AUDIT entry for the linker emulation code. */
4105 elf_dt_audit (abfd) = audit;
4108 /* If this is a dynamic object, we always link against the .dynsym
4109 symbol table, not the .symtab symbol table. The dynamic linker
4110 will only see the .dynsym symbol table, so there is no reason to
4111 look at .symtab for a dynamic object. */
4113 if (! dynamic || elf_dynsymtab (abfd) == 0)
4114 hdr = &elf_tdata (abfd)->symtab_hdr;
4116 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4118 symcount = hdr->sh_size / bed->s->sizeof_sym;
4120 /* The sh_info field of the symtab header tells us where the
4121 external symbols start. We don't care about the local symbols at
4123 if (elf_bad_symtab (abfd))
4125 extsymcount = symcount;
4130 extsymcount = symcount - hdr->sh_info;
4131 extsymoff = hdr->sh_info;
4134 sym_hash = elf_sym_hashes (abfd);
4135 if (extsymcount != 0)
4137 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4139 if (isymbuf == NULL)
4142 if (sym_hash == NULL)
4144 /* We store a pointer to the hash table entry for each
4147 amt *= sizeof (struct elf_link_hash_entry *);
4148 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4149 if (sym_hash == NULL)
4150 goto error_free_sym;
4151 elf_sym_hashes (abfd) = sym_hash;
4157 /* Read in any version definitions. */
4158 if (!_bfd_elf_slurp_version_tables (abfd,
4159 info->default_imported_symver))
4160 goto error_free_sym;
4162 /* Read in the symbol versions, but don't bother to convert them
4163 to internal format. */
4164 if (elf_dynversym (abfd) != 0)
4166 Elf_Internal_Shdr *versymhdr;
4168 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4169 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4170 if (extversym == NULL)
4171 goto error_free_sym;
4172 amt = versymhdr->sh_size;
4173 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4174 || bfd_bread (extversym, amt, abfd) != amt)
4175 goto error_free_vers;
4179 /* If we are loading an as-needed shared lib, save the symbol table
4180 state before we start adding symbols. If the lib turns out
4181 to be unneeded, restore the state. */
4182 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4187 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4189 struct bfd_hash_entry *p;
4190 struct elf_link_hash_entry *h;
4192 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4194 h = (struct elf_link_hash_entry *) p;
4195 entsize += htab->root.table.entsize;
4196 if (h->root.type == bfd_link_hash_warning)
4197 entsize += htab->root.table.entsize;
4201 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4202 old_tab = bfd_malloc (tabsize + entsize);
4203 if (old_tab == NULL)
4204 goto error_free_vers;
4206 /* Remember the current objalloc pointer, so that all mem for
4207 symbols added can later be reclaimed. */
4208 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4209 if (alloc_mark == NULL)
4210 goto error_free_vers;
4212 /* Make a special call to the linker "notice" function to
4213 tell it that we are about to handle an as-needed lib. */
4214 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4215 goto error_free_vers;
4217 /* Clone the symbol table. Remember some pointers into the
4218 symbol table, and dynamic symbol count. */
4219 old_ent = (char *) old_tab + tabsize;
4220 memcpy (old_tab, htab->root.table.table, tabsize);
4221 old_undefs = htab->root.undefs;
4222 old_undefs_tail = htab->root.undefs_tail;
4223 old_table = htab->root.table.table;
4224 old_size = htab->root.table.size;
4225 old_count = htab->root.table.count;
4226 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4227 if (old_strtab == NULL)
4228 goto error_free_vers;
4230 for (i = 0; i < htab->root.table.size; i++)
4232 struct bfd_hash_entry *p;
4233 struct elf_link_hash_entry *h;
4235 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4237 memcpy (old_ent, p, htab->root.table.entsize);
4238 old_ent = (char *) old_ent + htab->root.table.entsize;
4239 h = (struct elf_link_hash_entry *) p;
4240 if (h->root.type == bfd_link_hash_warning)
4242 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4243 old_ent = (char *) old_ent + htab->root.table.entsize;
4250 ever = extversym != NULL ? extversym + extsymoff : NULL;
4251 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4253 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4257 asection *sec, *new_sec;
4260 struct elf_link_hash_entry *h;
4261 struct elf_link_hash_entry *hi;
4262 bfd_boolean definition;
4263 bfd_boolean size_change_ok;
4264 bfd_boolean type_change_ok;
4265 bfd_boolean new_weak;
4266 bfd_boolean old_weak;
4267 bfd_boolean override;
4269 bfd_boolean discarded;
4270 unsigned int old_alignment;
4272 bfd_boolean matched;
4276 flags = BSF_NO_FLAGS;
4278 value = isym->st_value;
4279 common = bed->common_definition (isym);
4280 if (common && info->inhibit_common_definition)
4282 /* Treat common symbol as undefined for --no-define-common. */
4283 isym->st_shndx = SHN_UNDEF;
4288 bind = ELF_ST_BIND (isym->st_info);
4292 /* This should be impossible, since ELF requires that all
4293 global symbols follow all local symbols, and that sh_info
4294 point to the first global symbol. Unfortunately, Irix 5
4299 if (isym->st_shndx != SHN_UNDEF && !common)
4307 case STB_GNU_UNIQUE:
4308 flags = BSF_GNU_UNIQUE;
4312 /* Leave it up to the processor backend. */
4316 if (isym->st_shndx == SHN_UNDEF)
4317 sec = bfd_und_section_ptr;
4318 else if (isym->st_shndx == SHN_ABS)
4319 sec = bfd_abs_section_ptr;
4320 else if (isym->st_shndx == SHN_COMMON)
4322 sec = bfd_com_section_ptr;
4323 /* What ELF calls the size we call the value. What ELF
4324 calls the value we call the alignment. */
4325 value = isym->st_size;
4329 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4331 sec = bfd_abs_section_ptr;
4332 else if (discarded_section (sec))
4334 /* Symbols from discarded section are undefined. We keep
4336 sec = bfd_und_section_ptr;
4338 isym->st_shndx = SHN_UNDEF;
4340 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4344 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4347 goto error_free_vers;
4349 if (isym->st_shndx == SHN_COMMON
4350 && (abfd->flags & BFD_PLUGIN) != 0)
4352 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4356 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4358 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4360 goto error_free_vers;
4364 else if (isym->st_shndx == SHN_COMMON
4365 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4366 && !bfd_link_relocatable (info))
4368 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4372 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4373 | SEC_LINKER_CREATED);
4374 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4376 goto error_free_vers;
4380 else if (bed->elf_add_symbol_hook)
4382 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4384 goto error_free_vers;
4386 /* The hook function sets the name to NULL if this symbol
4387 should be skipped for some reason. */
4392 /* Sanity check that all possibilities were handled. */
4395 bfd_set_error (bfd_error_bad_value);
4396 goto error_free_vers;
4399 /* Silently discard TLS symbols from --just-syms. There's
4400 no way to combine a static TLS block with a new TLS block
4401 for this executable. */
4402 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4403 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4406 if (bfd_is_und_section (sec)
4407 || bfd_is_com_section (sec))
4412 size_change_ok = FALSE;
4413 type_change_ok = bed->type_change_ok;
4420 if (is_elf_hash_table (htab))
4422 Elf_Internal_Versym iver;
4423 unsigned int vernum = 0;
4428 if (info->default_imported_symver)
4429 /* Use the default symbol version created earlier. */
4430 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4435 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4437 vernum = iver.vs_vers & VERSYM_VERSION;
4439 /* If this is a hidden symbol, or if it is not version
4440 1, we append the version name to the symbol name.
4441 However, we do not modify a non-hidden absolute symbol
4442 if it is not a function, because it might be the version
4443 symbol itself. FIXME: What if it isn't? */
4444 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4446 && (!bfd_is_abs_section (sec)
4447 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4450 size_t namelen, verlen, newlen;
4453 if (isym->st_shndx != SHN_UNDEF)
4455 if (vernum > elf_tdata (abfd)->cverdefs)
4457 else if (vernum > 1)
4459 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4466 /* xgettext:c-format */
4467 (_("%B: %s: invalid version %u (max %d)"),
4469 elf_tdata (abfd)->cverdefs);
4470 bfd_set_error (bfd_error_bad_value);
4471 goto error_free_vers;
4476 /* We cannot simply test for the number of
4477 entries in the VERNEED section since the
4478 numbers for the needed versions do not start
4480 Elf_Internal_Verneed *t;
4483 for (t = elf_tdata (abfd)->verref;
4487 Elf_Internal_Vernaux *a;
4489 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4491 if (a->vna_other == vernum)
4493 verstr = a->vna_nodename;
4503 /* xgettext:c-format */
4504 (_("%B: %s: invalid needed version %d"),
4505 abfd, name, vernum);
4506 bfd_set_error (bfd_error_bad_value);
4507 goto error_free_vers;
4511 namelen = strlen (name);
4512 verlen = strlen (verstr);
4513 newlen = namelen + verlen + 2;
4514 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4515 && isym->st_shndx != SHN_UNDEF)
4518 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4519 if (newname == NULL)
4520 goto error_free_vers;
4521 memcpy (newname, name, namelen);
4522 p = newname + namelen;
4524 /* If this is a defined non-hidden version symbol,
4525 we add another @ to the name. This indicates the
4526 default version of the symbol. */
4527 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4528 && isym->st_shndx != SHN_UNDEF)
4530 memcpy (p, verstr, verlen + 1);
4535 /* If this symbol has default visibility and the user has
4536 requested we not re-export it, then mark it as hidden. */
4537 if (!bfd_is_und_section (sec)
4540 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4541 isym->st_other = (STV_HIDDEN
4542 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4544 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4545 sym_hash, &old_bfd, &old_weak,
4546 &old_alignment, &skip, &override,
4547 &type_change_ok, &size_change_ok,
4549 goto error_free_vers;
4554 /* Override a definition only if the new symbol matches the
4556 if (override && matched)
4560 while (h->root.type == bfd_link_hash_indirect
4561 || h->root.type == bfd_link_hash_warning)
4562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4564 if (elf_tdata (abfd)->verdef != NULL
4567 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4570 if (! (_bfd_generic_link_add_one_symbol
4571 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4572 (struct bfd_link_hash_entry **) sym_hash)))
4573 goto error_free_vers;
4575 if ((flags & BSF_GNU_UNIQUE)
4576 && (abfd->flags & DYNAMIC) == 0
4577 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4578 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4581 /* We need to make sure that indirect symbol dynamic flags are
4584 while (h->root.type == bfd_link_hash_indirect
4585 || h->root.type == bfd_link_hash_warning)
4586 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4588 /* Setting the index to -3 tells elf_link_output_extsym that
4589 this symbol is defined in a discarded section. */
4595 new_weak = (flags & BSF_WEAK) != 0;
4599 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4600 && is_elf_hash_table (htab)
4601 && h->u.alias == NULL)
4603 /* Keep a list of all weak defined non function symbols from
4604 a dynamic object, using the alias field. Later in this
4605 function we will set the alias field to the correct
4606 value. We only put non-function symbols from dynamic
4607 objects on this list, because that happens to be the only
4608 time we need to know the normal symbol corresponding to a
4609 weak symbol, and the information is time consuming to
4610 figure out. If the alias field is not already NULL,
4611 then this symbol was already defined by some previous
4612 dynamic object, and we will be using that previous
4613 definition anyhow. */
4619 /* Set the alignment of a common symbol. */
4620 if ((common || bfd_is_com_section (sec))
4621 && h->root.type == bfd_link_hash_common)
4626 align = bfd_log2 (isym->st_value);
4629 /* The new symbol is a common symbol in a shared object.
4630 We need to get the alignment from the section. */
4631 align = new_sec->alignment_power;
4633 if (align > old_alignment)
4634 h->root.u.c.p->alignment_power = align;
4636 h->root.u.c.p->alignment_power = old_alignment;
4639 if (is_elf_hash_table (htab))
4641 /* Set a flag in the hash table entry indicating the type of
4642 reference or definition we just found. A dynamic symbol
4643 is one which is referenced or defined by both a regular
4644 object and a shared object. */
4645 bfd_boolean dynsym = FALSE;
4647 /* Plugin symbols aren't normal. Don't set def_regular or
4648 ref_regular for them, or make them dynamic. */
4649 if ((abfd->flags & BFD_PLUGIN) != 0)
4656 if (bind != STB_WEAK)
4657 h->ref_regular_nonweak = 1;
4669 /* If the indirect symbol has been forced local, don't
4670 make the real symbol dynamic. */
4671 if ((h == hi || !hi->forced_local)
4672 && (bfd_link_dll (info)
4682 hi->ref_dynamic = 1;
4687 hi->def_dynamic = 1;
4690 /* If the indirect symbol has been forced local, don't
4691 make the real symbol dynamic. */
4692 if ((h == hi || !hi->forced_local)
4696 && weakdef (h)->dynindx != -1)))
4700 /* Check to see if we need to add an indirect symbol for
4701 the default name. */
4703 || (!override && h->root.type == bfd_link_hash_common))
4704 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4705 sec, value, &old_bfd, &dynsym))
4706 goto error_free_vers;
4708 /* Check the alignment when a common symbol is involved. This
4709 can change when a common symbol is overridden by a normal
4710 definition or a common symbol is ignored due to the old
4711 normal definition. We need to make sure the maximum
4712 alignment is maintained. */
4713 if ((old_alignment || common)
4714 && h->root.type != bfd_link_hash_common)
4716 unsigned int common_align;
4717 unsigned int normal_align;
4718 unsigned int symbol_align;
4722 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4723 || h->root.type == bfd_link_hash_defweak);
4725 symbol_align = ffs (h->root.u.def.value) - 1;
4726 if (h->root.u.def.section->owner != NULL
4727 && (h->root.u.def.section->owner->flags
4728 & (DYNAMIC | BFD_PLUGIN)) == 0)
4730 normal_align = h->root.u.def.section->alignment_power;
4731 if (normal_align > symbol_align)
4732 normal_align = symbol_align;
4735 normal_align = symbol_align;
4739 common_align = old_alignment;
4740 common_bfd = old_bfd;
4745 common_align = bfd_log2 (isym->st_value);
4747 normal_bfd = old_bfd;
4750 if (normal_align < common_align)
4752 /* PR binutils/2735 */
4753 if (normal_bfd == NULL)
4755 /* xgettext:c-format */
4756 (_("Warning: alignment %u of common symbol `%s' in %B is"
4757 " greater than the alignment (%u) of its section %A"),
4758 1 << common_align, name, common_bfd,
4759 1 << normal_align, h->root.u.def.section);
4762 /* xgettext:c-format */
4763 (_("Warning: alignment %u of symbol `%s' in %B"
4764 " is smaller than %u in %B"),
4765 1 << normal_align, name, normal_bfd,
4766 1 << common_align, common_bfd);
4770 /* Remember the symbol size if it isn't undefined. */
4771 if (isym->st_size != 0
4772 && isym->st_shndx != SHN_UNDEF
4773 && (definition || h->size == 0))
4776 && h->size != isym->st_size
4777 && ! size_change_ok)
4779 /* xgettext:c-format */
4780 (_("Warning: size of symbol `%s' changed"
4781 " from %Lu in %B to %Lu in %B"),
4782 name, h->size, old_bfd, isym->st_size, abfd);
4784 h->size = isym->st_size;
4787 /* If this is a common symbol, then we always want H->SIZE
4788 to be the size of the common symbol. The code just above
4789 won't fix the size if a common symbol becomes larger. We
4790 don't warn about a size change here, because that is
4791 covered by --warn-common. Allow changes between different
4793 if (h->root.type == bfd_link_hash_common)
4794 h->size = h->root.u.c.size;
4796 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4797 && ((definition && !new_weak)
4798 || (old_weak && h->root.type == bfd_link_hash_common)
4799 || h->type == STT_NOTYPE))
4801 unsigned int type = ELF_ST_TYPE (isym->st_info);
4803 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4805 if (type == STT_GNU_IFUNC
4806 && (abfd->flags & DYNAMIC) != 0)
4809 if (h->type != type)
4811 if (h->type != STT_NOTYPE && ! type_change_ok)
4812 /* xgettext:c-format */
4814 (_("Warning: type of symbol `%s' changed"
4815 " from %d to %d in %B"),
4816 name, h->type, type, abfd);
4822 /* Merge st_other field. */
4823 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4825 /* We don't want to make debug symbol dynamic. */
4827 && (sec->flags & SEC_DEBUGGING)
4828 && !bfd_link_relocatable (info))
4831 /* Nor should we make plugin symbols dynamic. */
4832 if ((abfd->flags & BFD_PLUGIN) != 0)
4837 h->target_internal = isym->st_target_internal;
4838 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4841 if (definition && !dynamic)
4843 char *p = strchr (name, ELF_VER_CHR);
4844 if (p != NULL && p[1] != ELF_VER_CHR)
4846 /* Queue non-default versions so that .symver x, x@FOO
4847 aliases can be checked. */
4850 amt = ((isymend - isym + 1)
4851 * sizeof (struct elf_link_hash_entry *));
4853 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4855 goto error_free_vers;
4857 nondeflt_vers[nondeflt_vers_cnt++] = h;
4861 if (dynsym && h->dynindx == -1)
4863 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4864 goto error_free_vers;
4866 && weakdef (h)->dynindx == -1)
4868 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4869 goto error_free_vers;
4872 else if (h->dynindx != -1)
4873 /* If the symbol already has a dynamic index, but
4874 visibility says it should not be visible, turn it into
4876 switch (ELF_ST_VISIBILITY (h->other))
4880 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4885 /* Don't add DT_NEEDED for references from the dummy bfd nor
4886 for unmatched symbol. */
4891 && h->ref_regular_nonweak
4893 || (old_bfd->flags & BFD_PLUGIN) == 0))
4894 || (h->ref_dynamic_nonweak
4895 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4896 && !on_needed_list (elf_dt_name (abfd),
4897 htab->needed, NULL))))
4900 const char *soname = elf_dt_name (abfd);
4902 info->callbacks->minfo ("%!", soname, old_bfd,
4903 h->root.root.string);
4905 /* A symbol from a library loaded via DT_NEEDED of some
4906 other library is referenced by a regular object.
4907 Add a DT_NEEDED entry for it. Issue an error if
4908 --no-add-needed is used and the reference was not
4911 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4914 /* xgettext:c-format */
4915 (_("%B: undefined reference to symbol '%s'"),
4917 bfd_set_error (bfd_error_missing_dso);
4918 goto error_free_vers;
4921 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4922 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4925 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4927 goto error_free_vers;
4929 BFD_ASSERT (ret == 0);
4934 if (extversym != NULL)
4940 if (isymbuf != NULL)
4946 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4950 /* Restore the symbol table. */
4951 old_ent = (char *) old_tab + tabsize;
4952 memset (elf_sym_hashes (abfd), 0,
4953 extsymcount * sizeof (struct elf_link_hash_entry *));
4954 htab->root.table.table = old_table;
4955 htab->root.table.size = old_size;
4956 htab->root.table.count = old_count;
4957 memcpy (htab->root.table.table, old_tab, tabsize);
4958 htab->root.undefs = old_undefs;
4959 htab->root.undefs_tail = old_undefs_tail;
4960 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4963 for (i = 0; i < htab->root.table.size; i++)
4965 struct bfd_hash_entry *p;
4966 struct elf_link_hash_entry *h;
4968 unsigned int alignment_power;
4969 unsigned int non_ir_ref_dynamic;
4971 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4973 h = (struct elf_link_hash_entry *) p;
4974 if (h->root.type == bfd_link_hash_warning)
4975 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4977 /* Preserve the maximum alignment and size for common
4978 symbols even if this dynamic lib isn't on DT_NEEDED
4979 since it can still be loaded at run time by another
4981 if (h->root.type == bfd_link_hash_common)
4983 size = h->root.u.c.size;
4984 alignment_power = h->root.u.c.p->alignment_power;
4989 alignment_power = 0;
4991 /* Preserve non_ir_ref_dynamic so that this symbol
4992 will be exported when the dynamic lib becomes needed
4993 in the second pass. */
4994 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
4995 memcpy (p, old_ent, htab->root.table.entsize);
4996 old_ent = (char *) old_ent + htab->root.table.entsize;
4997 h = (struct elf_link_hash_entry *) p;
4998 if (h->root.type == bfd_link_hash_warning)
5000 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5001 old_ent = (char *) old_ent + htab->root.table.entsize;
5002 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5004 if (h->root.type == bfd_link_hash_common)
5006 if (size > h->root.u.c.size)
5007 h->root.u.c.size = size;
5008 if (alignment_power > h->root.u.c.p->alignment_power)
5009 h->root.u.c.p->alignment_power = alignment_power;
5011 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5015 /* Make a special call to the linker "notice" function to
5016 tell it that symbols added for crefs may need to be removed. */
5017 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5018 goto error_free_vers;
5021 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5023 if (nondeflt_vers != NULL)
5024 free (nondeflt_vers);
5028 if (old_tab != NULL)
5030 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5031 goto error_free_vers;
5036 /* Now that all the symbols from this input file are created, if
5037 not performing a relocatable link, handle .symver foo, foo@BAR
5038 such that any relocs against foo become foo@BAR. */
5039 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5043 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5045 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5046 char *shortname, *p;
5048 p = strchr (h->root.root.string, ELF_VER_CHR);
5050 || (h->root.type != bfd_link_hash_defined
5051 && h->root.type != bfd_link_hash_defweak))
5054 amt = p - h->root.root.string;
5055 shortname = (char *) bfd_malloc (amt + 1);
5057 goto error_free_vers;
5058 memcpy (shortname, h->root.root.string, amt);
5059 shortname[amt] = '\0';
5061 hi = (struct elf_link_hash_entry *)
5062 bfd_link_hash_lookup (&htab->root, shortname,
5063 FALSE, FALSE, FALSE);
5065 && hi->root.type == h->root.type
5066 && hi->root.u.def.value == h->root.u.def.value
5067 && hi->root.u.def.section == h->root.u.def.section)
5069 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5070 hi->root.type = bfd_link_hash_indirect;
5071 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5072 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5073 sym_hash = elf_sym_hashes (abfd);
5075 for (symidx = 0; symidx < extsymcount; ++symidx)
5076 if (sym_hash[symidx] == hi)
5078 sym_hash[symidx] = h;
5084 free (nondeflt_vers);
5085 nondeflt_vers = NULL;
5088 /* Now set the alias field correctly for all the weak defined
5089 symbols we found. The only way to do this is to search all the
5090 symbols. Since we only need the information for non functions in
5091 dynamic objects, that's the only time we actually put anything on
5092 the list WEAKS. We need this information so that if a regular
5093 object refers to a symbol defined weakly in a dynamic object, the
5094 real symbol in the dynamic object is also put in the dynamic
5095 symbols; we also must arrange for both symbols to point to the
5096 same memory location. We could handle the general case of symbol
5097 aliasing, but a general symbol alias can only be generated in
5098 assembler code, handling it correctly would be very time
5099 consuming, and other ELF linkers don't handle general aliasing
5103 struct elf_link_hash_entry **hpp;
5104 struct elf_link_hash_entry **hppend;
5105 struct elf_link_hash_entry **sorted_sym_hash;
5106 struct elf_link_hash_entry *h;
5109 /* Since we have to search the whole symbol list for each weak
5110 defined symbol, search time for N weak defined symbols will be
5111 O(N^2). Binary search will cut it down to O(NlogN). */
5113 amt *= sizeof (struct elf_link_hash_entry *);
5114 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5115 if (sorted_sym_hash == NULL)
5117 sym_hash = sorted_sym_hash;
5118 hpp = elf_sym_hashes (abfd);
5119 hppend = hpp + extsymcount;
5121 for (; hpp < hppend; hpp++)
5125 && h->root.type == bfd_link_hash_defined
5126 && !bed->is_function_type (h->type))
5134 qsort (sorted_sym_hash, sym_count,
5135 sizeof (struct elf_link_hash_entry *),
5138 while (weaks != NULL)
5140 struct elf_link_hash_entry *hlook;
5143 size_t i, j, idx = 0;
5146 weaks = hlook->u.alias;
5147 hlook->u.alias = NULL;
5149 if (hlook->root.type != bfd_link_hash_defined
5150 && hlook->root.type != bfd_link_hash_defweak)
5153 slook = hlook->root.u.def.section;
5154 vlook = hlook->root.u.def.value;
5160 bfd_signed_vma vdiff;
5162 h = sorted_sym_hash[idx];
5163 vdiff = vlook - h->root.u.def.value;
5170 int sdiff = slook->id - h->root.u.def.section->id;
5180 /* We didn't find a value/section match. */
5184 /* With multiple aliases, or when the weak symbol is already
5185 strongly defined, we have multiple matching symbols and
5186 the binary search above may land on any of them. Step
5187 one past the matching symbol(s). */
5190 h = sorted_sym_hash[idx];
5191 if (h->root.u.def.section != slook
5192 || h->root.u.def.value != vlook)
5196 /* Now look back over the aliases. Since we sorted by size
5197 as well as value and section, we'll choose the one with
5198 the largest size. */
5201 h = sorted_sym_hash[idx];
5203 /* Stop if value or section doesn't match. */
5204 if (h->root.u.def.section != slook
5205 || h->root.u.def.value != vlook)
5207 else if (h != hlook)
5209 struct elf_link_hash_entry *t;
5212 hlook->is_weakalias = 1;
5214 if (t->u.alias != NULL)
5215 while (t->u.alias != h)
5219 /* If the weak definition is in the list of dynamic
5220 symbols, make sure the real definition is put
5222 if (hlook->dynindx != -1 && h->dynindx == -1)
5224 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5227 free (sorted_sym_hash);
5232 /* If the real definition is in the list of dynamic
5233 symbols, make sure the weak definition is put
5234 there as well. If we don't do this, then the
5235 dynamic loader might not merge the entries for the
5236 real definition and the weak definition. */
5237 if (h->dynindx != -1 && hlook->dynindx == -1)
5239 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5240 goto err_free_sym_hash;
5247 free (sorted_sym_hash);
5250 if (bed->check_directives
5251 && !(*bed->check_directives) (abfd, info))
5254 /* If this is a non-traditional link, try to optimize the handling
5255 of the .stab/.stabstr sections. */
5257 && ! info->traditional_format
5258 && is_elf_hash_table (htab)
5259 && (info->strip != strip_all && info->strip != strip_debugger))
5263 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5264 if (stabstr != NULL)
5266 bfd_size_type string_offset = 0;
5269 for (stab = abfd->sections; stab; stab = stab->next)
5270 if (CONST_STRNEQ (stab->name, ".stab")
5271 && (!stab->name[5] ||
5272 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5273 && (stab->flags & SEC_MERGE) == 0
5274 && !bfd_is_abs_section (stab->output_section))
5276 struct bfd_elf_section_data *secdata;
5278 secdata = elf_section_data (stab);
5279 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5280 stabstr, &secdata->sec_info,
5283 if (secdata->sec_info)
5284 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5289 if (is_elf_hash_table (htab) && add_needed)
5291 /* Add this bfd to the loaded list. */
5292 struct elf_link_loaded_list *n;
5294 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5298 n->next = htab->loaded;
5305 if (old_tab != NULL)
5307 if (old_strtab != NULL)
5309 if (nondeflt_vers != NULL)
5310 free (nondeflt_vers);
5311 if (extversym != NULL)
5314 if (isymbuf != NULL)
5320 /* Return the linker hash table entry of a symbol that might be
5321 satisfied by an archive symbol. Return -1 on error. */
5323 struct elf_link_hash_entry *
5324 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5325 struct bfd_link_info *info,
5328 struct elf_link_hash_entry *h;
5332 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5336 /* If this is a default version (the name contains @@), look up the
5337 symbol again with only one `@' as well as without the version.
5338 The effect is that references to the symbol with and without the
5339 version will be matched by the default symbol in the archive. */
5341 p = strchr (name, ELF_VER_CHR);
5342 if (p == NULL || p[1] != ELF_VER_CHR)
5345 /* First check with only one `@'. */
5346 len = strlen (name);
5347 copy = (char *) bfd_alloc (abfd, len);
5349 return (struct elf_link_hash_entry *) 0 - 1;
5351 first = p - name + 1;
5352 memcpy (copy, name, first);
5353 memcpy (copy + first, name + first + 1, len - first);
5355 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5358 /* We also need to check references to the symbol without the
5360 copy[first - 1] = '\0';
5361 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5362 FALSE, FALSE, TRUE);
5365 bfd_release (abfd, copy);
5369 /* Add symbols from an ELF archive file to the linker hash table. We
5370 don't use _bfd_generic_link_add_archive_symbols because we need to
5371 handle versioned symbols.
5373 Fortunately, ELF archive handling is simpler than that done by
5374 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5375 oddities. In ELF, if we find a symbol in the archive map, and the
5376 symbol is currently undefined, we know that we must pull in that
5379 Unfortunately, we do have to make multiple passes over the symbol
5380 table until nothing further is resolved. */
5383 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5386 unsigned char *included = NULL;
5390 const struct elf_backend_data *bed;
5391 struct elf_link_hash_entry * (*archive_symbol_lookup)
5392 (bfd *, struct bfd_link_info *, const char *);
5394 if (! bfd_has_map (abfd))
5396 /* An empty archive is a special case. */
5397 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5399 bfd_set_error (bfd_error_no_armap);
5403 /* Keep track of all symbols we know to be already defined, and all
5404 files we know to be already included. This is to speed up the
5405 second and subsequent passes. */
5406 c = bfd_ardata (abfd)->symdef_count;
5410 amt *= sizeof (*included);
5411 included = (unsigned char *) bfd_zmalloc (amt);
5412 if (included == NULL)
5415 symdefs = bfd_ardata (abfd)->symdefs;
5416 bed = get_elf_backend_data (abfd);
5417 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5430 symdefend = symdef + c;
5431 for (i = 0; symdef < symdefend; symdef++, i++)
5433 struct elf_link_hash_entry *h;
5435 struct bfd_link_hash_entry *undefs_tail;
5440 if (symdef->file_offset == last)
5446 h = archive_symbol_lookup (abfd, info, symdef->name);
5447 if (h == (struct elf_link_hash_entry *) 0 - 1)
5453 if (h->root.type == bfd_link_hash_common)
5455 /* We currently have a common symbol. The archive map contains
5456 a reference to this symbol, so we may want to include it. We
5457 only want to include it however, if this archive element
5458 contains a definition of the symbol, not just another common
5461 Unfortunately some archivers (including GNU ar) will put
5462 declarations of common symbols into their archive maps, as
5463 well as real definitions, so we cannot just go by the archive
5464 map alone. Instead we must read in the element's symbol
5465 table and check that to see what kind of symbol definition
5467 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5470 else if (h->root.type != bfd_link_hash_undefined)
5472 if (h->root.type != bfd_link_hash_undefweak)
5473 /* Symbol must be defined. Don't check it again. */
5478 /* We need to include this archive member. */
5479 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5480 if (element == NULL)
5483 if (! bfd_check_format (element, bfd_object))
5486 undefs_tail = info->hash->undefs_tail;
5488 if (!(*info->callbacks
5489 ->add_archive_element) (info, element, symdef->name, &element))
5491 if (!bfd_link_add_symbols (element, info))
5494 /* If there are any new undefined symbols, we need to make
5495 another pass through the archive in order to see whether
5496 they can be defined. FIXME: This isn't perfect, because
5497 common symbols wind up on undefs_tail and because an
5498 undefined symbol which is defined later on in this pass
5499 does not require another pass. This isn't a bug, but it
5500 does make the code less efficient than it could be. */
5501 if (undefs_tail != info->hash->undefs_tail)
5504 /* Look backward to mark all symbols from this object file
5505 which we have already seen in this pass. */
5509 included[mark] = TRUE;
5514 while (symdefs[mark].file_offset == symdef->file_offset);
5516 /* We mark subsequent symbols from this object file as we go
5517 on through the loop. */
5518 last = symdef->file_offset;
5528 if (included != NULL)
5533 /* Given an ELF BFD, add symbols to the global hash table as
5537 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5539 switch (bfd_get_format (abfd))
5542 return elf_link_add_object_symbols (abfd, info);
5544 return elf_link_add_archive_symbols (abfd, info);
5546 bfd_set_error (bfd_error_wrong_format);
5551 struct hash_codes_info
5553 unsigned long *hashcodes;
5557 /* This function will be called though elf_link_hash_traverse to store
5558 all hash value of the exported symbols in an array. */
5561 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5563 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5568 /* Ignore indirect symbols. These are added by the versioning code. */
5569 if (h->dynindx == -1)
5572 name = h->root.root.string;
5573 if (h->versioned >= versioned)
5575 char *p = strchr (name, ELF_VER_CHR);
5578 alc = (char *) bfd_malloc (p - name + 1);
5584 memcpy (alc, name, p - name);
5585 alc[p - name] = '\0';
5590 /* Compute the hash value. */
5591 ha = bfd_elf_hash (name);
5593 /* Store the found hash value in the array given as the argument. */
5594 *(inf->hashcodes)++ = ha;
5596 /* And store it in the struct so that we can put it in the hash table
5598 h->u.elf_hash_value = ha;
5606 struct collect_gnu_hash_codes
5609 const struct elf_backend_data *bed;
5610 unsigned long int nsyms;
5611 unsigned long int maskbits;
5612 unsigned long int *hashcodes;
5613 unsigned long int *hashval;
5614 unsigned long int *indx;
5615 unsigned long int *counts;
5618 long int min_dynindx;
5619 unsigned long int bucketcount;
5620 unsigned long int symindx;
5621 long int local_indx;
5622 long int shift1, shift2;
5623 unsigned long int mask;
5627 /* This function will be called though elf_link_hash_traverse to store
5628 all hash value of the exported symbols in an array. */
5631 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5633 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5638 /* Ignore indirect symbols. These are added by the versioning code. */
5639 if (h->dynindx == -1)
5642 /* Ignore also local symbols and undefined symbols. */
5643 if (! (*s->bed->elf_hash_symbol) (h))
5646 name = h->root.root.string;
5647 if (h->versioned >= versioned)
5649 char *p = strchr (name, ELF_VER_CHR);
5652 alc = (char *) bfd_malloc (p - name + 1);
5658 memcpy (alc, name, p - name);
5659 alc[p - name] = '\0';
5664 /* Compute the hash value. */
5665 ha = bfd_elf_gnu_hash (name);
5667 /* Store the found hash value in the array for compute_bucket_count,
5668 and also for .dynsym reordering purposes. */
5669 s->hashcodes[s->nsyms] = ha;
5670 s->hashval[h->dynindx] = ha;
5672 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5673 s->min_dynindx = h->dynindx;
5681 /* This function will be called though elf_link_hash_traverse to do
5682 final dynaminc symbol renumbering. */
5685 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5687 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5688 unsigned long int bucket;
5689 unsigned long int val;
5691 /* Ignore indirect symbols. */
5692 if (h->dynindx == -1)
5695 /* Ignore also local symbols and undefined symbols. */
5696 if (! (*s->bed->elf_hash_symbol) (h))
5698 if (h->dynindx >= s->min_dynindx)
5699 h->dynindx = s->local_indx++;
5703 bucket = s->hashval[h->dynindx] % s->bucketcount;
5704 val = (s->hashval[h->dynindx] >> s->shift1)
5705 & ((s->maskbits >> s->shift1) - 1);
5706 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5708 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5709 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5710 if (s->counts[bucket] == 1)
5711 /* Last element terminates the chain. */
5713 bfd_put_32 (s->output_bfd, val,
5714 s->contents + (s->indx[bucket] - s->symindx) * 4);
5715 --s->counts[bucket];
5716 h->dynindx = s->indx[bucket]++;
5720 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5723 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5725 return !(h->forced_local
5726 || h->root.type == bfd_link_hash_undefined
5727 || h->root.type == bfd_link_hash_undefweak
5728 || ((h->root.type == bfd_link_hash_defined
5729 || h->root.type == bfd_link_hash_defweak)
5730 && h->root.u.def.section->output_section == NULL));
5733 /* Array used to determine the number of hash table buckets to use
5734 based on the number of symbols there are. If there are fewer than
5735 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5736 fewer than 37 we use 17 buckets, and so forth. We never use more
5737 than 32771 buckets. */
5739 static const size_t elf_buckets[] =
5741 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5745 /* Compute bucket count for hashing table. We do not use a static set
5746 of possible tables sizes anymore. Instead we determine for all
5747 possible reasonable sizes of the table the outcome (i.e., the
5748 number of collisions etc) and choose the best solution. The
5749 weighting functions are not too simple to allow the table to grow
5750 without bounds. Instead one of the weighting factors is the size.
5751 Therefore the result is always a good payoff between few collisions
5752 (= short chain lengths) and table size. */
5754 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5755 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5756 unsigned long int nsyms,
5759 size_t best_size = 0;
5760 unsigned long int i;
5762 /* We have a problem here. The following code to optimize the table
5763 size requires an integer type with more the 32 bits. If
5764 BFD_HOST_U_64_BIT is set we know about such a type. */
5765 #ifdef BFD_HOST_U_64_BIT
5770 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5771 bfd *dynobj = elf_hash_table (info)->dynobj;
5772 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5773 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5774 unsigned long int *counts;
5776 unsigned int no_improvement_count = 0;
5778 /* Possible optimization parameters: if we have NSYMS symbols we say
5779 that the hashing table must at least have NSYMS/4 and at most
5781 minsize = nsyms / 4;
5784 best_size = maxsize = nsyms * 2;
5789 if ((best_size & 31) == 0)
5793 /* Create array where we count the collisions in. We must use bfd_malloc
5794 since the size could be large. */
5796 amt *= sizeof (unsigned long int);
5797 counts = (unsigned long int *) bfd_malloc (amt);
5801 /* Compute the "optimal" size for the hash table. The criteria is a
5802 minimal chain length. The minor criteria is (of course) the size
5804 for (i = minsize; i < maxsize; ++i)
5806 /* Walk through the array of hashcodes and count the collisions. */
5807 BFD_HOST_U_64_BIT max;
5808 unsigned long int j;
5809 unsigned long int fact;
5811 if (gnu_hash && (i & 31) == 0)
5814 memset (counts, '\0', i * sizeof (unsigned long int));
5816 /* Determine how often each hash bucket is used. */
5817 for (j = 0; j < nsyms; ++j)
5818 ++counts[hashcodes[j] % i];
5820 /* For the weight function we need some information about the
5821 pagesize on the target. This is information need not be 100%
5822 accurate. Since this information is not available (so far) we
5823 define it here to a reasonable default value. If it is crucial
5824 to have a better value some day simply define this value. */
5825 # ifndef BFD_TARGET_PAGESIZE
5826 # define BFD_TARGET_PAGESIZE (4096)
5829 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5831 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5834 /* Variant 1: optimize for short chains. We add the squares
5835 of all the chain lengths (which favors many small chain
5836 over a few long chains). */
5837 for (j = 0; j < i; ++j)
5838 max += counts[j] * counts[j];
5840 /* This adds penalties for the overall size of the table. */
5841 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5844 /* Variant 2: Optimize a lot more for small table. Here we
5845 also add squares of the size but we also add penalties for
5846 empty slots (the +1 term). */
5847 for (j = 0; j < i; ++j)
5848 max += (1 + counts[j]) * (1 + counts[j]);
5850 /* The overall size of the table is considered, but not as
5851 strong as in variant 1, where it is squared. */
5852 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5856 /* Compare with current best results. */
5857 if (max < best_chlen)
5861 no_improvement_count = 0;
5863 /* PR 11843: Avoid futile long searches for the best bucket size
5864 when there are a large number of symbols. */
5865 else if (++no_improvement_count == 100)
5872 #endif /* defined (BFD_HOST_U_64_BIT) */
5874 /* This is the fallback solution if no 64bit type is available or if we
5875 are not supposed to spend much time on optimizations. We select the
5876 bucket count using a fixed set of numbers. */
5877 for (i = 0; elf_buckets[i] != 0; i++)
5879 best_size = elf_buckets[i];
5880 if (nsyms < elf_buckets[i + 1])
5883 if (gnu_hash && best_size < 2)
5890 /* Size any SHT_GROUP section for ld -r. */
5893 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5898 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5899 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5900 && (s = ibfd->sections) != NULL
5901 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5902 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5907 /* Set a default stack segment size. The value in INFO wins. If it
5908 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5909 undefined it is initialized. */
5912 bfd_elf_stack_segment_size (bfd *output_bfd,
5913 struct bfd_link_info *info,
5914 const char *legacy_symbol,
5915 bfd_vma default_size)
5917 struct elf_link_hash_entry *h = NULL;
5919 /* Look for legacy symbol. */
5921 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5922 FALSE, FALSE, FALSE);
5923 if (h && (h->root.type == bfd_link_hash_defined
5924 || h->root.type == bfd_link_hash_defweak)
5926 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5928 /* The symbol has no type if specified on the command line. */
5929 h->type = STT_OBJECT;
5930 if (info->stacksize)
5931 /* xgettext:c-format */
5932 _bfd_error_handler (_("%B: stack size specified and %s set"),
5933 output_bfd, legacy_symbol);
5934 else if (h->root.u.def.section != bfd_abs_section_ptr)
5935 /* xgettext:c-format */
5936 _bfd_error_handler (_("%B: %s not absolute"),
5937 output_bfd, legacy_symbol);
5939 info->stacksize = h->root.u.def.value;
5942 if (!info->stacksize)
5943 /* If the user didn't set a size, or explicitly inhibit the
5944 size, set it now. */
5945 info->stacksize = default_size;
5947 /* Provide the legacy symbol, if it is referenced. */
5948 if (h && (h->root.type == bfd_link_hash_undefined
5949 || h->root.type == bfd_link_hash_undefweak))
5951 struct bfd_link_hash_entry *bh = NULL;
5953 if (!(_bfd_generic_link_add_one_symbol
5954 (info, output_bfd, legacy_symbol,
5955 BSF_GLOBAL, bfd_abs_section_ptr,
5956 info->stacksize >= 0 ? info->stacksize : 0,
5957 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5960 h = (struct elf_link_hash_entry *) bh;
5962 h->type = STT_OBJECT;
5968 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5970 struct elf_gc_sweep_symbol_info
5972 struct bfd_link_info *info;
5973 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5978 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5981 && (((h->root.type == bfd_link_hash_defined
5982 || h->root.type == bfd_link_hash_defweak)
5983 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5984 && h->root.u.def.section->gc_mark))
5985 || h->root.type == bfd_link_hash_undefined
5986 || h->root.type == bfd_link_hash_undefweak))
5988 struct elf_gc_sweep_symbol_info *inf;
5990 inf = (struct elf_gc_sweep_symbol_info *) data;
5991 (*inf->hide_symbol) (inf->info, h, TRUE);
5994 h->ref_regular_nonweak = 0;
6000 /* Set up the sizes and contents of the ELF dynamic sections. This is
6001 called by the ELF linker emulation before_allocation routine. We
6002 must set the sizes of the sections before the linker sets the
6003 addresses of the various sections. */
6006 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6009 const char *filter_shlib,
6011 const char *depaudit,
6012 const char * const *auxiliary_filters,
6013 struct bfd_link_info *info,
6014 asection **sinterpptr)
6017 const struct elf_backend_data *bed;
6021 if (!is_elf_hash_table (info->hash))
6024 dynobj = elf_hash_table (info)->dynobj;
6026 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6028 struct bfd_elf_version_tree *verdefs;
6029 struct elf_info_failed asvinfo;
6030 struct bfd_elf_version_tree *t;
6031 struct bfd_elf_version_expr *d;
6035 /* If we are supposed to export all symbols into the dynamic symbol
6036 table (this is not the normal case), then do so. */
6037 if (info->export_dynamic
6038 || (bfd_link_executable (info) && info->dynamic))
6040 struct elf_info_failed eif;
6044 elf_link_hash_traverse (elf_hash_table (info),
6045 _bfd_elf_export_symbol,
6053 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6055 if (soname_indx == (size_t) -1
6056 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6060 soname_indx = (size_t) -1;
6062 /* Make all global versions with definition. */
6063 for (t = info->version_info; t != NULL; t = t->next)
6064 for (d = t->globals.list; d != NULL; d = d->next)
6065 if (!d->symver && d->literal)
6067 const char *verstr, *name;
6068 size_t namelen, verlen, newlen;
6069 char *newname, *p, leading_char;
6070 struct elf_link_hash_entry *newh;
6072 leading_char = bfd_get_symbol_leading_char (output_bfd);
6074 namelen = strlen (name) + (leading_char != '\0');
6076 verlen = strlen (verstr);
6077 newlen = namelen + verlen + 3;
6079 newname = (char *) bfd_malloc (newlen);
6080 if (newname == NULL)
6082 newname[0] = leading_char;
6083 memcpy (newname + (leading_char != '\0'), name, namelen);
6085 /* Check the hidden versioned definition. */
6086 p = newname + namelen;
6088 memcpy (p, verstr, verlen + 1);
6089 newh = elf_link_hash_lookup (elf_hash_table (info),
6090 newname, FALSE, FALSE,
6093 || (newh->root.type != bfd_link_hash_defined
6094 && newh->root.type != bfd_link_hash_defweak))
6096 /* Check the default versioned definition. */
6098 memcpy (p, verstr, verlen + 1);
6099 newh = elf_link_hash_lookup (elf_hash_table (info),
6100 newname, FALSE, FALSE,
6105 /* Mark this version if there is a definition and it is
6106 not defined in a shared object. */
6108 && !newh->def_dynamic
6109 && (newh->root.type == bfd_link_hash_defined
6110 || newh->root.type == bfd_link_hash_defweak))
6114 /* Attach all the symbols to their version information. */
6115 asvinfo.info = info;
6116 asvinfo.failed = FALSE;
6118 elf_link_hash_traverse (elf_hash_table (info),
6119 _bfd_elf_link_assign_sym_version,
6124 if (!info->allow_undefined_version)
6126 /* Check if all global versions have a definition. */
6127 bfd_boolean all_defined = TRUE;
6128 for (t = info->version_info; t != NULL; t = t->next)
6129 for (d = t->globals.list; d != NULL; d = d->next)
6130 if (d->literal && !d->symver && !d->script)
6133 (_("%s: undefined version: %s"),
6134 d->pattern, t->name);
6135 all_defined = FALSE;
6140 bfd_set_error (bfd_error_bad_value);
6145 /* Set up the version definition section. */
6146 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6147 BFD_ASSERT (s != NULL);
6149 /* We may have created additional version definitions if we are
6150 just linking a regular application. */
6151 verdefs = info->version_info;
6153 /* Skip anonymous version tag. */
6154 if (verdefs != NULL && verdefs->vernum == 0)
6155 verdefs = verdefs->next;
6157 if (verdefs == NULL && !info->create_default_symver)
6158 s->flags |= SEC_EXCLUDE;
6164 Elf_Internal_Verdef def;
6165 Elf_Internal_Verdaux defaux;
6166 struct bfd_link_hash_entry *bh;
6167 struct elf_link_hash_entry *h;
6173 /* Make space for the base version. */
6174 size += sizeof (Elf_External_Verdef);
6175 size += sizeof (Elf_External_Verdaux);
6178 /* Make space for the default version. */
6179 if (info->create_default_symver)
6181 size += sizeof (Elf_External_Verdef);
6185 for (t = verdefs; t != NULL; t = t->next)
6187 struct bfd_elf_version_deps *n;
6189 /* Don't emit base version twice. */
6193 size += sizeof (Elf_External_Verdef);
6194 size += sizeof (Elf_External_Verdaux);
6197 for (n = t->deps; n != NULL; n = n->next)
6198 size += sizeof (Elf_External_Verdaux);
6202 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6203 if (s->contents == NULL && s->size != 0)
6206 /* Fill in the version definition section. */
6210 def.vd_version = VER_DEF_CURRENT;
6211 def.vd_flags = VER_FLG_BASE;
6214 if (info->create_default_symver)
6216 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6217 def.vd_next = sizeof (Elf_External_Verdef);
6221 def.vd_aux = sizeof (Elf_External_Verdef);
6222 def.vd_next = (sizeof (Elf_External_Verdef)
6223 + sizeof (Elf_External_Verdaux));
6226 if (soname_indx != (size_t) -1)
6228 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6230 def.vd_hash = bfd_elf_hash (soname);
6231 defaux.vda_name = soname_indx;
6238 name = lbasename (output_bfd->filename);
6239 def.vd_hash = bfd_elf_hash (name);
6240 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6242 if (indx == (size_t) -1)
6244 defaux.vda_name = indx;
6246 defaux.vda_next = 0;
6248 _bfd_elf_swap_verdef_out (output_bfd, &def,
6249 (Elf_External_Verdef *) p);
6250 p += sizeof (Elf_External_Verdef);
6251 if (info->create_default_symver)
6253 /* Add a symbol representing this version. */
6255 if (! (_bfd_generic_link_add_one_symbol
6256 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6258 get_elf_backend_data (dynobj)->collect, &bh)))
6260 h = (struct elf_link_hash_entry *) bh;
6263 h->type = STT_OBJECT;
6264 h->verinfo.vertree = NULL;
6266 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6269 /* Create a duplicate of the base version with the same
6270 aux block, but different flags. */
6273 def.vd_aux = sizeof (Elf_External_Verdef);
6275 def.vd_next = (sizeof (Elf_External_Verdef)
6276 + sizeof (Elf_External_Verdaux));
6279 _bfd_elf_swap_verdef_out (output_bfd, &def,
6280 (Elf_External_Verdef *) p);
6281 p += sizeof (Elf_External_Verdef);
6283 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6284 (Elf_External_Verdaux *) p);
6285 p += sizeof (Elf_External_Verdaux);
6287 for (t = verdefs; t != NULL; t = t->next)
6290 struct bfd_elf_version_deps *n;
6292 /* Don't emit the base version twice. */
6297 for (n = t->deps; n != NULL; n = n->next)
6300 /* Add a symbol representing this version. */
6302 if (! (_bfd_generic_link_add_one_symbol
6303 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6305 get_elf_backend_data (dynobj)->collect, &bh)))
6307 h = (struct elf_link_hash_entry *) bh;
6310 h->type = STT_OBJECT;
6311 h->verinfo.vertree = t;
6313 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6316 def.vd_version = VER_DEF_CURRENT;
6318 if (t->globals.list == NULL
6319 && t->locals.list == NULL
6321 def.vd_flags |= VER_FLG_WEAK;
6322 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6323 def.vd_cnt = cdeps + 1;
6324 def.vd_hash = bfd_elf_hash (t->name);
6325 def.vd_aux = sizeof (Elf_External_Verdef);
6328 /* If a basever node is next, it *must* be the last node in
6329 the chain, otherwise Verdef construction breaks. */
6330 if (t->next != NULL && t->next->vernum == 0)
6331 BFD_ASSERT (t->next->next == NULL);
6333 if (t->next != NULL && t->next->vernum != 0)
6334 def.vd_next = (sizeof (Elf_External_Verdef)
6335 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6337 _bfd_elf_swap_verdef_out (output_bfd, &def,
6338 (Elf_External_Verdef *) p);
6339 p += sizeof (Elf_External_Verdef);
6341 defaux.vda_name = h->dynstr_index;
6342 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6344 defaux.vda_next = 0;
6345 if (t->deps != NULL)
6346 defaux.vda_next = sizeof (Elf_External_Verdaux);
6347 t->name_indx = defaux.vda_name;
6349 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6350 (Elf_External_Verdaux *) p);
6351 p += sizeof (Elf_External_Verdaux);
6353 for (n = t->deps; n != NULL; n = n->next)
6355 if (n->version_needed == NULL)
6357 /* This can happen if there was an error in the
6359 defaux.vda_name = 0;
6363 defaux.vda_name = n->version_needed->name_indx;
6364 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6367 if (n->next == NULL)
6368 defaux.vda_next = 0;
6370 defaux.vda_next = sizeof (Elf_External_Verdaux);
6372 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6373 (Elf_External_Verdaux *) p);
6374 p += sizeof (Elf_External_Verdaux);
6378 elf_tdata (output_bfd)->cverdefs = cdefs;
6382 bed = get_elf_backend_data (output_bfd);
6384 if (info->gc_sections && bed->can_gc_sections)
6386 struct elf_gc_sweep_symbol_info sweep_info;
6388 /* Remove the symbols that were in the swept sections from the
6389 dynamic symbol table. */
6390 sweep_info.info = info;
6391 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6392 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6396 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6399 struct elf_find_verdep_info sinfo;
6401 /* Work out the size of the version reference section. */
6403 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6404 BFD_ASSERT (s != NULL);
6407 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6408 if (sinfo.vers == 0)
6410 sinfo.failed = FALSE;
6412 elf_link_hash_traverse (elf_hash_table (info),
6413 _bfd_elf_link_find_version_dependencies,
6418 if (elf_tdata (output_bfd)->verref == NULL)
6419 s->flags |= SEC_EXCLUDE;
6422 Elf_Internal_Verneed *vn;
6427 /* Build the version dependency section. */
6430 for (vn = elf_tdata (output_bfd)->verref;
6432 vn = vn->vn_nextref)
6434 Elf_Internal_Vernaux *a;
6436 size += sizeof (Elf_External_Verneed);
6438 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6439 size += sizeof (Elf_External_Vernaux);
6443 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6444 if (s->contents == NULL)
6448 for (vn = elf_tdata (output_bfd)->verref;
6450 vn = vn->vn_nextref)
6453 Elf_Internal_Vernaux *a;
6457 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6460 vn->vn_version = VER_NEED_CURRENT;
6462 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6463 elf_dt_name (vn->vn_bfd) != NULL
6464 ? elf_dt_name (vn->vn_bfd)
6465 : lbasename (vn->vn_bfd->filename),
6467 if (indx == (size_t) -1)
6470 vn->vn_aux = sizeof (Elf_External_Verneed);
6471 if (vn->vn_nextref == NULL)
6474 vn->vn_next = (sizeof (Elf_External_Verneed)
6475 + caux * sizeof (Elf_External_Vernaux));
6477 _bfd_elf_swap_verneed_out (output_bfd, vn,
6478 (Elf_External_Verneed *) p);
6479 p += sizeof (Elf_External_Verneed);
6481 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6483 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6484 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6485 a->vna_nodename, FALSE);
6486 if (indx == (size_t) -1)
6489 if (a->vna_nextptr == NULL)
6492 a->vna_next = sizeof (Elf_External_Vernaux);
6494 _bfd_elf_swap_vernaux_out (output_bfd, a,
6495 (Elf_External_Vernaux *) p);
6496 p += sizeof (Elf_External_Vernaux);
6500 elf_tdata (output_bfd)->cverrefs = crefs;
6504 /* Any syms created from now on start with -1 in
6505 got.refcount/offset and plt.refcount/offset. */
6506 elf_hash_table (info)->init_got_refcount
6507 = elf_hash_table (info)->init_got_offset;
6508 elf_hash_table (info)->init_plt_refcount
6509 = elf_hash_table (info)->init_plt_offset;
6511 if (bfd_link_relocatable (info)
6512 && !_bfd_elf_size_group_sections (info))
6515 /* The backend may have to create some sections regardless of whether
6516 we're dynamic or not. */
6517 if (bed->elf_backend_always_size_sections
6518 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6521 /* Determine any GNU_STACK segment requirements, after the backend
6522 has had a chance to set a default segment size. */
6523 if (info->execstack)
6524 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6525 else if (info->noexecstack)
6526 elf_stack_flags (output_bfd) = PF_R | PF_W;
6530 asection *notesec = NULL;
6533 for (inputobj = info->input_bfds;
6535 inputobj = inputobj->link.next)
6540 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6542 s = inputobj->sections;
6543 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6546 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6549 if (s->flags & SEC_CODE)
6553 else if (bed->default_execstack)
6556 if (notesec || info->stacksize > 0)
6557 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6558 if (notesec && exec && bfd_link_relocatable (info)
6559 && notesec->output_section != bfd_abs_section_ptr)
6560 notesec->output_section->flags |= SEC_CODE;
6563 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6565 struct elf_info_failed eif;
6566 struct elf_link_hash_entry *h;
6570 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6571 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6575 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6577 info->flags |= DF_SYMBOLIC;
6585 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6587 if (indx == (size_t) -1)
6590 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6591 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6595 if (filter_shlib != NULL)
6599 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6600 filter_shlib, TRUE);
6601 if (indx == (size_t) -1
6602 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6606 if (auxiliary_filters != NULL)
6608 const char * const *p;
6610 for (p = auxiliary_filters; *p != NULL; p++)
6614 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6616 if (indx == (size_t) -1
6617 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6626 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6628 if (indx == (size_t) -1
6629 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6633 if (depaudit != NULL)
6637 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6639 if (indx == (size_t) -1
6640 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6647 /* Find all symbols which were defined in a dynamic object and make
6648 the backend pick a reasonable value for them. */
6649 elf_link_hash_traverse (elf_hash_table (info),
6650 _bfd_elf_adjust_dynamic_symbol,
6655 /* Add some entries to the .dynamic section. We fill in some of the
6656 values later, in bfd_elf_final_link, but we must add the entries
6657 now so that we know the final size of the .dynamic section. */
6659 /* If there are initialization and/or finalization functions to
6660 call then add the corresponding DT_INIT/DT_FINI entries. */
6661 h = (info->init_function
6662 ? elf_link_hash_lookup (elf_hash_table (info),
6663 info->init_function, FALSE,
6670 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6673 h = (info->fini_function
6674 ? elf_link_hash_lookup (elf_hash_table (info),
6675 info->fini_function, FALSE,
6682 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6686 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6687 if (s != NULL && s->linker_has_input)
6689 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6690 if (! bfd_link_executable (info))
6695 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6696 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6697 && (o = sub->sections) != NULL
6698 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6699 for (o = sub->sections; o != NULL; o = o->next)
6700 if (elf_section_data (o)->this_hdr.sh_type
6701 == SHT_PREINIT_ARRAY)
6704 (_("%B: .preinit_array section is not allowed in DSO"),
6709 bfd_set_error (bfd_error_nonrepresentable_section);
6713 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6714 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6717 s = bfd_get_section_by_name (output_bfd, ".init_array");
6718 if (s != NULL && s->linker_has_input)
6720 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6721 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6724 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6725 if (s != NULL && s->linker_has_input)
6727 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6728 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6732 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6733 /* If .dynstr is excluded from the link, we don't want any of
6734 these tags. Strictly, we should be checking each section
6735 individually; This quick check covers for the case where
6736 someone does a /DISCARD/ : { *(*) }. */
6737 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6739 bfd_size_type strsize;
6741 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6742 if ((info->emit_hash
6743 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6744 || (info->emit_gnu_hash
6745 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6746 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6747 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6748 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6749 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6750 bed->s->sizeof_sym))
6755 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6758 /* The backend must work out the sizes of all the other dynamic
6761 && bed->elf_backend_size_dynamic_sections != NULL
6762 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6765 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6767 unsigned long section_sym_count;
6769 if (elf_tdata (output_bfd)->cverdefs)
6771 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6773 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6774 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6778 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6780 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6783 else if (info->flags & DF_BIND_NOW)
6785 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6791 if (bfd_link_executable (info))
6792 info->flags_1 &= ~ (DF_1_INITFIRST
6795 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6799 if (elf_tdata (output_bfd)->cverrefs)
6801 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6803 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6804 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6808 if ((elf_tdata (output_bfd)->cverrefs == 0
6809 && elf_tdata (output_bfd)->cverdefs == 0)
6810 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6811 §ion_sym_count) <= 1)
6815 s = bfd_get_linker_section (dynobj, ".gnu.version");
6816 s->flags |= SEC_EXCLUDE;
6822 /* Find the first non-excluded output section. We'll use its
6823 section symbol for some emitted relocs. */
6825 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6829 for (s = output_bfd->sections; s != NULL; s = s->next)
6830 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6831 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6833 elf_hash_table (info)->text_index_section = s;
6838 /* Find two non-excluded output sections, one for code, one for data.
6839 We'll use their section symbols for some emitted relocs. */
6841 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6845 /* Data first, since setting text_index_section changes
6846 _bfd_elf_link_omit_section_dynsym. */
6847 for (s = output_bfd->sections; s != NULL; s = s->next)
6848 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6849 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6851 elf_hash_table (info)->data_index_section = s;
6855 for (s = output_bfd->sections; s != NULL; s = s->next)
6856 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6857 == (SEC_ALLOC | SEC_READONLY))
6858 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6860 elf_hash_table (info)->text_index_section = s;
6864 if (elf_hash_table (info)->text_index_section == NULL)
6865 elf_hash_table (info)->text_index_section
6866 = elf_hash_table (info)->data_index_section;
6870 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6872 const struct elf_backend_data *bed;
6873 unsigned long section_sym_count;
6874 bfd_size_type dynsymcount = 0;
6876 if (!is_elf_hash_table (info->hash))
6879 bed = get_elf_backend_data (output_bfd);
6880 (*bed->elf_backend_init_index_section) (output_bfd, info);
6882 /* Assign dynsym indices. In a shared library we generate a section
6883 symbol for each output section, which come first. Next come all
6884 of the back-end allocated local dynamic syms, followed by the rest
6885 of the global symbols.
6887 This is usually not needed for static binaries, however backends
6888 can request to always do it, e.g. the MIPS backend uses dynamic
6889 symbol counts to lay out GOT, which will be produced in the
6890 presence of GOT relocations even in static binaries (holding fixed
6891 data in that case, to satisfy those relocations). */
6893 if (elf_hash_table (info)->dynamic_sections_created
6894 || bed->always_renumber_dynsyms)
6895 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6896 §ion_sym_count);
6898 if (elf_hash_table (info)->dynamic_sections_created)
6902 unsigned int dtagcount;
6904 dynobj = elf_hash_table (info)->dynobj;
6906 /* Work out the size of the symbol version section. */
6907 s = bfd_get_linker_section (dynobj, ".gnu.version");
6908 BFD_ASSERT (s != NULL);
6909 if ((s->flags & SEC_EXCLUDE) == 0)
6911 s->size = dynsymcount * sizeof (Elf_External_Versym);
6912 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6913 if (s->contents == NULL)
6916 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6920 /* Set the size of the .dynsym and .hash sections. We counted
6921 the number of dynamic symbols in elf_link_add_object_symbols.
6922 We will build the contents of .dynsym and .hash when we build
6923 the final symbol table, because until then we do not know the
6924 correct value to give the symbols. We built the .dynstr
6925 section as we went along in elf_link_add_object_symbols. */
6926 s = elf_hash_table (info)->dynsym;
6927 BFD_ASSERT (s != NULL);
6928 s->size = dynsymcount * bed->s->sizeof_sym;
6930 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6931 if (s->contents == NULL)
6934 /* The first entry in .dynsym is a dummy symbol. Clear all the
6935 section syms, in case we don't output them all. */
6936 ++section_sym_count;
6937 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6939 elf_hash_table (info)->bucketcount = 0;
6941 /* Compute the size of the hashing table. As a side effect this
6942 computes the hash values for all the names we export. */
6943 if (info->emit_hash)
6945 unsigned long int *hashcodes;
6946 struct hash_codes_info hashinf;
6948 unsigned long int nsyms;
6950 size_t hash_entry_size;
6952 /* Compute the hash values for all exported symbols. At the same
6953 time store the values in an array so that we could use them for
6955 amt = dynsymcount * sizeof (unsigned long int);
6956 hashcodes = (unsigned long int *) bfd_malloc (amt);
6957 if (hashcodes == NULL)
6959 hashinf.hashcodes = hashcodes;
6960 hashinf.error = FALSE;
6962 /* Put all hash values in HASHCODES. */
6963 elf_link_hash_traverse (elf_hash_table (info),
6964 elf_collect_hash_codes, &hashinf);
6971 nsyms = hashinf.hashcodes - hashcodes;
6973 = compute_bucket_count (info, hashcodes, nsyms, 0);
6976 if (bucketcount == 0 && nsyms > 0)
6979 elf_hash_table (info)->bucketcount = bucketcount;
6981 s = bfd_get_linker_section (dynobj, ".hash");
6982 BFD_ASSERT (s != NULL);
6983 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6984 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6985 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6986 if (s->contents == NULL)
6989 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6990 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6991 s->contents + hash_entry_size);
6994 if (info->emit_gnu_hash)
6997 unsigned char *contents;
6998 struct collect_gnu_hash_codes cinfo;
7002 memset (&cinfo, 0, sizeof (cinfo));
7004 /* Compute the hash values for all exported symbols. At the same
7005 time store the values in an array so that we could use them for
7007 amt = dynsymcount * 2 * sizeof (unsigned long int);
7008 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7009 if (cinfo.hashcodes == NULL)
7012 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7013 cinfo.min_dynindx = -1;
7014 cinfo.output_bfd = output_bfd;
7017 /* Put all hash values in HASHCODES. */
7018 elf_link_hash_traverse (elf_hash_table (info),
7019 elf_collect_gnu_hash_codes, &cinfo);
7022 free (cinfo.hashcodes);
7027 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7029 if (bucketcount == 0)
7031 free (cinfo.hashcodes);
7035 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7036 BFD_ASSERT (s != NULL);
7038 if (cinfo.nsyms == 0)
7040 /* Empty .gnu.hash section is special. */
7041 BFD_ASSERT (cinfo.min_dynindx == -1);
7042 free (cinfo.hashcodes);
7043 s->size = 5 * 4 + bed->s->arch_size / 8;
7044 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7045 if (contents == NULL)
7047 s->contents = contents;
7048 /* 1 empty bucket. */
7049 bfd_put_32 (output_bfd, 1, contents);
7050 /* SYMIDX above the special symbol 0. */
7051 bfd_put_32 (output_bfd, 1, contents + 4);
7052 /* Just one word for bitmask. */
7053 bfd_put_32 (output_bfd, 1, contents + 8);
7054 /* Only hash fn bloom filter. */
7055 bfd_put_32 (output_bfd, 0, contents + 12);
7056 /* No hashes are valid - empty bitmask. */
7057 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7058 /* No hashes in the only bucket. */
7059 bfd_put_32 (output_bfd, 0,
7060 contents + 16 + bed->s->arch_size / 8);
7064 unsigned long int maskwords, maskbitslog2, x;
7065 BFD_ASSERT (cinfo.min_dynindx != -1);
7069 while ((x >>= 1) != 0)
7071 if (maskbitslog2 < 3)
7073 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7074 maskbitslog2 = maskbitslog2 + 3;
7076 maskbitslog2 = maskbitslog2 + 2;
7077 if (bed->s->arch_size == 64)
7079 if (maskbitslog2 == 5)
7085 cinfo.mask = (1 << cinfo.shift1) - 1;
7086 cinfo.shift2 = maskbitslog2;
7087 cinfo.maskbits = 1 << maskbitslog2;
7088 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7089 amt = bucketcount * sizeof (unsigned long int) * 2;
7090 amt += maskwords * sizeof (bfd_vma);
7091 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7092 if (cinfo.bitmask == NULL)
7094 free (cinfo.hashcodes);
7098 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7099 cinfo.indx = cinfo.counts + bucketcount;
7100 cinfo.symindx = dynsymcount - cinfo.nsyms;
7101 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7103 /* Determine how often each hash bucket is used. */
7104 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7105 for (i = 0; i < cinfo.nsyms; ++i)
7106 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7108 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7109 if (cinfo.counts[i] != 0)
7111 cinfo.indx[i] = cnt;
7112 cnt += cinfo.counts[i];
7114 BFD_ASSERT (cnt == dynsymcount);
7115 cinfo.bucketcount = bucketcount;
7116 cinfo.local_indx = cinfo.min_dynindx;
7118 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7119 s->size += cinfo.maskbits / 8;
7120 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7121 if (contents == NULL)
7123 free (cinfo.bitmask);
7124 free (cinfo.hashcodes);
7128 s->contents = contents;
7129 bfd_put_32 (output_bfd, bucketcount, contents);
7130 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7131 bfd_put_32 (output_bfd, maskwords, contents + 8);
7132 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7133 contents += 16 + cinfo.maskbits / 8;
7135 for (i = 0; i < bucketcount; ++i)
7137 if (cinfo.counts[i] == 0)
7138 bfd_put_32 (output_bfd, 0, contents);
7140 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7144 cinfo.contents = contents;
7146 /* Renumber dynamic symbols, populate .gnu.hash section. */
7147 elf_link_hash_traverse (elf_hash_table (info),
7148 elf_renumber_gnu_hash_syms, &cinfo);
7150 contents = s->contents + 16;
7151 for (i = 0; i < maskwords; ++i)
7153 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7155 contents += bed->s->arch_size / 8;
7158 free (cinfo.bitmask);
7159 free (cinfo.hashcodes);
7163 s = bfd_get_linker_section (dynobj, ".dynstr");
7164 BFD_ASSERT (s != NULL);
7166 elf_finalize_dynstr (output_bfd, info);
7168 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7170 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7171 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7178 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7181 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7184 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7185 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7188 /* Finish SHF_MERGE section merging. */
7191 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7196 if (!is_elf_hash_table (info->hash))
7199 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7200 if ((ibfd->flags & DYNAMIC) == 0
7201 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7202 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7203 == get_elf_backend_data (obfd)->s->elfclass))
7204 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7205 if ((sec->flags & SEC_MERGE) != 0
7206 && !bfd_is_abs_section (sec->output_section))
7208 struct bfd_elf_section_data *secdata;
7210 secdata = elf_section_data (sec);
7211 if (! _bfd_add_merge_section (obfd,
7212 &elf_hash_table (info)->merge_info,
7213 sec, &secdata->sec_info))
7215 else if (secdata->sec_info)
7216 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7219 if (elf_hash_table (info)->merge_info != NULL)
7220 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7221 merge_sections_remove_hook);
7225 /* Create an entry in an ELF linker hash table. */
7227 struct bfd_hash_entry *
7228 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7229 struct bfd_hash_table *table,
7232 /* Allocate the structure if it has not already been allocated by a
7236 entry = (struct bfd_hash_entry *)
7237 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7242 /* Call the allocation method of the superclass. */
7243 entry = _bfd_link_hash_newfunc (entry, table, string);
7246 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7247 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7249 /* Set local fields. */
7252 ret->got = htab->init_got_refcount;
7253 ret->plt = htab->init_plt_refcount;
7254 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7255 - offsetof (struct elf_link_hash_entry, size)));
7256 /* Assume that we have been called by a non-ELF symbol reader.
7257 This flag is then reset by the code which reads an ELF input
7258 file. This ensures that a symbol created by a non-ELF symbol
7259 reader will have the flag set correctly. */
7266 /* Copy data from an indirect symbol to its direct symbol, hiding the
7267 old indirect symbol. Also used for copying flags to a weakdef. */
7270 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7271 struct elf_link_hash_entry *dir,
7272 struct elf_link_hash_entry *ind)
7274 struct elf_link_hash_table *htab;
7276 /* Copy down any references that we may have already seen to the
7277 symbol which just became indirect. */
7279 if (dir->versioned != versioned_hidden)
7280 dir->ref_dynamic |= ind->ref_dynamic;
7281 dir->ref_regular |= ind->ref_regular;
7282 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7283 dir->non_got_ref |= ind->non_got_ref;
7284 dir->needs_plt |= ind->needs_plt;
7285 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7287 if (ind->root.type != bfd_link_hash_indirect)
7290 /* Copy over the global and procedure linkage table refcount entries.
7291 These may have been already set up by a check_relocs routine. */
7292 htab = elf_hash_table (info);
7293 if (ind->got.refcount > htab->init_got_refcount.refcount)
7295 if (dir->got.refcount < 0)
7296 dir->got.refcount = 0;
7297 dir->got.refcount += ind->got.refcount;
7298 ind->got.refcount = htab->init_got_refcount.refcount;
7301 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7303 if (dir->plt.refcount < 0)
7304 dir->plt.refcount = 0;
7305 dir->plt.refcount += ind->plt.refcount;
7306 ind->plt.refcount = htab->init_plt_refcount.refcount;
7309 if (ind->dynindx != -1)
7311 if (dir->dynindx != -1)
7312 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7313 dir->dynindx = ind->dynindx;
7314 dir->dynstr_index = ind->dynstr_index;
7316 ind->dynstr_index = 0;
7321 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7322 struct elf_link_hash_entry *h,
7323 bfd_boolean force_local)
7325 /* STT_GNU_IFUNC symbol must go through PLT. */
7326 if (h->type != STT_GNU_IFUNC)
7328 h->plt = elf_hash_table (info)->init_plt_offset;
7333 h->forced_local = 1;
7334 if (h->dynindx != -1)
7336 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7339 h->dynstr_index = 0;
7344 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7348 _bfd_elf_link_hash_table_init
7349 (struct elf_link_hash_table *table,
7351 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7352 struct bfd_hash_table *,
7354 unsigned int entsize,
7355 enum elf_target_id target_id)
7358 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7360 table->init_got_refcount.refcount = can_refcount - 1;
7361 table->init_plt_refcount.refcount = can_refcount - 1;
7362 table->init_got_offset.offset = -(bfd_vma) 1;
7363 table->init_plt_offset.offset = -(bfd_vma) 1;
7364 /* The first dynamic symbol is a dummy. */
7365 table->dynsymcount = 1;
7367 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7369 table->root.type = bfd_link_elf_hash_table;
7370 table->hash_table_id = target_id;
7375 /* Create an ELF linker hash table. */
7377 struct bfd_link_hash_table *
7378 _bfd_elf_link_hash_table_create (bfd *abfd)
7380 struct elf_link_hash_table *ret;
7381 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7383 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7387 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7388 sizeof (struct elf_link_hash_entry),
7394 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7399 /* Destroy an ELF linker hash table. */
7402 _bfd_elf_link_hash_table_free (bfd *obfd)
7404 struct elf_link_hash_table *htab;
7406 htab = (struct elf_link_hash_table *) obfd->link.hash;
7407 if (htab->dynstr != NULL)
7408 _bfd_elf_strtab_free (htab->dynstr);
7409 _bfd_merge_sections_free (htab->merge_info);
7410 _bfd_generic_link_hash_table_free (obfd);
7413 /* This is a hook for the ELF emulation code in the generic linker to
7414 tell the backend linker what file name to use for the DT_NEEDED
7415 entry for a dynamic object. */
7418 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7420 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7421 && bfd_get_format (abfd) == bfd_object)
7422 elf_dt_name (abfd) = name;
7426 bfd_elf_get_dyn_lib_class (bfd *abfd)
7429 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7430 && bfd_get_format (abfd) == bfd_object)
7431 lib_class = elf_dyn_lib_class (abfd);
7438 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7440 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7441 && bfd_get_format (abfd) == bfd_object)
7442 elf_dyn_lib_class (abfd) = lib_class;
7445 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7446 the linker ELF emulation code. */
7448 struct bfd_link_needed_list *
7449 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7450 struct bfd_link_info *info)
7452 if (! is_elf_hash_table (info->hash))
7454 return elf_hash_table (info)->needed;
7457 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7458 hook for the linker ELF emulation code. */
7460 struct bfd_link_needed_list *
7461 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7462 struct bfd_link_info *info)
7464 if (! is_elf_hash_table (info->hash))
7466 return elf_hash_table (info)->runpath;
7469 /* Get the name actually used for a dynamic object for a link. This
7470 is the SONAME entry if there is one. Otherwise, it is the string
7471 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7474 bfd_elf_get_dt_soname (bfd *abfd)
7476 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7477 && bfd_get_format (abfd) == bfd_object)
7478 return elf_dt_name (abfd);
7482 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7483 the ELF linker emulation code. */
7486 bfd_elf_get_bfd_needed_list (bfd *abfd,
7487 struct bfd_link_needed_list **pneeded)
7490 bfd_byte *dynbuf = NULL;
7491 unsigned int elfsec;
7492 unsigned long shlink;
7493 bfd_byte *extdyn, *extdynend;
7495 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7499 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7500 || bfd_get_format (abfd) != bfd_object)
7503 s = bfd_get_section_by_name (abfd, ".dynamic");
7504 if (s == NULL || s->size == 0)
7507 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7510 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7511 if (elfsec == SHN_BAD)
7514 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7516 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7517 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7520 extdynend = extdyn + s->size;
7521 for (; extdyn < extdynend; extdyn += extdynsize)
7523 Elf_Internal_Dyn dyn;
7525 (*swap_dyn_in) (abfd, extdyn, &dyn);
7527 if (dyn.d_tag == DT_NULL)
7530 if (dyn.d_tag == DT_NEEDED)
7533 struct bfd_link_needed_list *l;
7534 unsigned int tagv = dyn.d_un.d_val;
7537 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7542 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7563 struct elf_symbuf_symbol
7565 unsigned long st_name; /* Symbol name, index in string tbl */
7566 unsigned char st_info; /* Type and binding attributes */
7567 unsigned char st_other; /* Visibilty, and target specific */
7570 struct elf_symbuf_head
7572 struct elf_symbuf_symbol *ssym;
7574 unsigned int st_shndx;
7581 Elf_Internal_Sym *isym;
7582 struct elf_symbuf_symbol *ssym;
7587 /* Sort references to symbols by ascending section number. */
7590 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7592 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7593 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7595 return s1->st_shndx - s2->st_shndx;
7599 elf_sym_name_compare (const void *arg1, const void *arg2)
7601 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7602 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7603 return strcmp (s1->name, s2->name);
7606 static struct elf_symbuf_head *
7607 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7609 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7610 struct elf_symbuf_symbol *ssym;
7611 struct elf_symbuf_head *ssymbuf, *ssymhead;
7612 size_t i, shndx_count, total_size;
7614 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7618 for (ind = indbuf, i = 0; i < symcount; i++)
7619 if (isymbuf[i].st_shndx != SHN_UNDEF)
7620 *ind++ = &isymbuf[i];
7623 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7624 elf_sort_elf_symbol);
7627 if (indbufend > indbuf)
7628 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7629 if (ind[0]->st_shndx != ind[1]->st_shndx)
7632 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7633 + (indbufend - indbuf) * sizeof (*ssym));
7634 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7635 if (ssymbuf == NULL)
7641 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7642 ssymbuf->ssym = NULL;
7643 ssymbuf->count = shndx_count;
7644 ssymbuf->st_shndx = 0;
7645 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7647 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7650 ssymhead->ssym = ssym;
7651 ssymhead->count = 0;
7652 ssymhead->st_shndx = (*ind)->st_shndx;
7654 ssym->st_name = (*ind)->st_name;
7655 ssym->st_info = (*ind)->st_info;
7656 ssym->st_other = (*ind)->st_other;
7659 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7660 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7667 /* Check if 2 sections define the same set of local and global
7671 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7672 struct bfd_link_info *info)
7675 const struct elf_backend_data *bed1, *bed2;
7676 Elf_Internal_Shdr *hdr1, *hdr2;
7677 size_t symcount1, symcount2;
7678 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7679 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7680 Elf_Internal_Sym *isym, *isymend;
7681 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7682 size_t count1, count2, i;
7683 unsigned int shndx1, shndx2;
7689 /* Both sections have to be in ELF. */
7690 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7691 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7694 if (elf_section_type (sec1) != elf_section_type (sec2))
7697 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7698 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7699 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7702 bed1 = get_elf_backend_data (bfd1);
7703 bed2 = get_elf_backend_data (bfd2);
7704 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7705 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7706 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7707 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7709 if (symcount1 == 0 || symcount2 == 0)
7715 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7716 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7718 if (ssymbuf1 == NULL)
7720 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7722 if (isymbuf1 == NULL)
7725 if (!info->reduce_memory_overheads)
7726 elf_tdata (bfd1)->symbuf = ssymbuf1
7727 = elf_create_symbuf (symcount1, isymbuf1);
7730 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7732 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7734 if (isymbuf2 == NULL)
7737 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7738 elf_tdata (bfd2)->symbuf = ssymbuf2
7739 = elf_create_symbuf (symcount2, isymbuf2);
7742 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7744 /* Optimized faster version. */
7746 struct elf_symbol *symp;
7747 struct elf_symbuf_symbol *ssym, *ssymend;
7750 hi = ssymbuf1->count;
7755 mid = (lo + hi) / 2;
7756 if (shndx1 < ssymbuf1[mid].st_shndx)
7758 else if (shndx1 > ssymbuf1[mid].st_shndx)
7762 count1 = ssymbuf1[mid].count;
7769 hi = ssymbuf2->count;
7774 mid = (lo + hi) / 2;
7775 if (shndx2 < ssymbuf2[mid].st_shndx)
7777 else if (shndx2 > ssymbuf2[mid].st_shndx)
7781 count2 = ssymbuf2[mid].count;
7787 if (count1 == 0 || count2 == 0 || count1 != count2)
7791 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7793 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7794 if (symtable1 == NULL || symtable2 == NULL)
7798 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7799 ssym < ssymend; ssym++, symp++)
7801 symp->u.ssym = ssym;
7802 symp->name = bfd_elf_string_from_elf_section (bfd1,
7808 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7809 ssym < ssymend; ssym++, symp++)
7811 symp->u.ssym = ssym;
7812 symp->name = bfd_elf_string_from_elf_section (bfd2,
7817 /* Sort symbol by name. */
7818 qsort (symtable1, count1, sizeof (struct elf_symbol),
7819 elf_sym_name_compare);
7820 qsort (symtable2, count1, sizeof (struct elf_symbol),
7821 elf_sym_name_compare);
7823 for (i = 0; i < count1; i++)
7824 /* Two symbols must have the same binding, type and name. */
7825 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7826 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7827 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7834 symtable1 = (struct elf_symbol *)
7835 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7836 symtable2 = (struct elf_symbol *)
7837 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7838 if (symtable1 == NULL || symtable2 == NULL)
7841 /* Count definitions in the section. */
7843 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7844 if (isym->st_shndx == shndx1)
7845 symtable1[count1++].u.isym = isym;
7848 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7849 if (isym->st_shndx == shndx2)
7850 symtable2[count2++].u.isym = isym;
7852 if (count1 == 0 || count2 == 0 || count1 != count2)
7855 for (i = 0; i < count1; i++)
7857 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7858 symtable1[i].u.isym->st_name);
7860 for (i = 0; i < count2; i++)
7862 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7863 symtable2[i].u.isym->st_name);
7865 /* Sort symbol by name. */
7866 qsort (symtable1, count1, sizeof (struct elf_symbol),
7867 elf_sym_name_compare);
7868 qsort (symtable2, count1, sizeof (struct elf_symbol),
7869 elf_sym_name_compare);
7871 for (i = 0; i < count1; i++)
7872 /* Two symbols must have the same binding, type and name. */
7873 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7874 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7875 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7893 /* Return TRUE if 2 section types are compatible. */
7896 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7897 bfd *bbfd, const asection *bsec)
7901 || abfd->xvec->flavour != bfd_target_elf_flavour
7902 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7905 return elf_section_type (asec) == elf_section_type (bsec);
7908 /* Final phase of ELF linker. */
7910 /* A structure we use to avoid passing large numbers of arguments. */
7912 struct elf_final_link_info
7914 /* General link information. */
7915 struct bfd_link_info *info;
7918 /* Symbol string table. */
7919 struct elf_strtab_hash *symstrtab;
7920 /* .hash section. */
7922 /* symbol version section (.gnu.version). */
7923 asection *symver_sec;
7924 /* Buffer large enough to hold contents of any section. */
7926 /* Buffer large enough to hold external relocs of any section. */
7927 void *external_relocs;
7928 /* Buffer large enough to hold internal relocs of any section. */
7929 Elf_Internal_Rela *internal_relocs;
7930 /* Buffer large enough to hold external local symbols of any input
7932 bfd_byte *external_syms;
7933 /* And a buffer for symbol section indices. */
7934 Elf_External_Sym_Shndx *locsym_shndx;
7935 /* Buffer large enough to hold internal local symbols of any input
7937 Elf_Internal_Sym *internal_syms;
7938 /* Array large enough to hold a symbol index for each local symbol
7939 of any input BFD. */
7941 /* Array large enough to hold a section pointer for each local
7942 symbol of any input BFD. */
7943 asection **sections;
7944 /* Buffer for SHT_SYMTAB_SHNDX section. */
7945 Elf_External_Sym_Shndx *symshndxbuf;
7946 /* Number of STT_FILE syms seen. */
7947 size_t filesym_count;
7950 /* This struct is used to pass information to elf_link_output_extsym. */
7952 struct elf_outext_info
7955 bfd_boolean localsyms;
7956 bfd_boolean file_sym_done;
7957 struct elf_final_link_info *flinfo;
7961 /* Support for evaluating a complex relocation.
7963 Complex relocations are generalized, self-describing relocations. The
7964 implementation of them consists of two parts: complex symbols, and the
7965 relocations themselves.
7967 The relocations are use a reserved elf-wide relocation type code (R_RELC
7968 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7969 information (start bit, end bit, word width, etc) into the addend. This
7970 information is extracted from CGEN-generated operand tables within gas.
7972 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7973 internal) representing prefix-notation expressions, including but not
7974 limited to those sorts of expressions normally encoded as addends in the
7975 addend field. The symbol mangling format is:
7978 | <unary-operator> ':' <node>
7979 | <binary-operator> ':' <node> ':' <node>
7982 <literal> := 's' <digits=N> ':' <N character symbol name>
7983 | 'S' <digits=N> ':' <N character section name>
7987 <binary-operator> := as in C
7988 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7991 set_symbol_value (bfd *bfd_with_globals,
7992 Elf_Internal_Sym *isymbuf,
7997 struct elf_link_hash_entry **sym_hashes;
7998 struct elf_link_hash_entry *h;
7999 size_t extsymoff = locsymcount;
8001 if (symidx < locsymcount)
8003 Elf_Internal_Sym *sym;
8005 sym = isymbuf + symidx;
8006 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8008 /* It is a local symbol: move it to the
8009 "absolute" section and give it a value. */
8010 sym->st_shndx = SHN_ABS;
8011 sym->st_value = val;
8014 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8018 /* It is a global symbol: set its link type
8019 to "defined" and give it a value. */
8021 sym_hashes = elf_sym_hashes (bfd_with_globals);
8022 h = sym_hashes [symidx - extsymoff];
8023 while (h->root.type == bfd_link_hash_indirect
8024 || h->root.type == bfd_link_hash_warning)
8025 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8026 h->root.type = bfd_link_hash_defined;
8027 h->root.u.def.value = val;
8028 h->root.u.def.section = bfd_abs_section_ptr;
8032 resolve_symbol (const char *name,
8034 struct elf_final_link_info *flinfo,
8036 Elf_Internal_Sym *isymbuf,
8039 Elf_Internal_Sym *sym;
8040 struct bfd_link_hash_entry *global_entry;
8041 const char *candidate = NULL;
8042 Elf_Internal_Shdr *symtab_hdr;
8045 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8047 for (i = 0; i < locsymcount; ++ i)
8051 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8054 candidate = bfd_elf_string_from_elf_section (input_bfd,
8055 symtab_hdr->sh_link,
8058 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8059 name, candidate, (unsigned long) sym->st_value);
8061 if (candidate && strcmp (candidate, name) == 0)
8063 asection *sec = flinfo->sections [i];
8065 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8066 *result += sec->output_offset + sec->output_section->vma;
8068 printf ("Found symbol with value %8.8lx\n",
8069 (unsigned long) *result);
8075 /* Hmm, haven't found it yet. perhaps it is a global. */
8076 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8077 FALSE, FALSE, TRUE);
8081 if (global_entry->type == bfd_link_hash_defined
8082 || global_entry->type == bfd_link_hash_defweak)
8084 *result = (global_entry->u.def.value
8085 + global_entry->u.def.section->output_section->vma
8086 + global_entry->u.def.section->output_offset);
8088 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8089 global_entry->root.string, (unsigned long) *result);
8097 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8098 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8099 names like "foo.end" which is the end address of section "foo". */
8102 resolve_section (const char *name,
8110 for (curr = sections; curr; curr = curr->next)
8111 if (strcmp (curr->name, name) == 0)
8113 *result = curr->vma;
8117 /* Hmm. still haven't found it. try pseudo-section names. */
8118 /* FIXME: This could be coded more efficiently... */
8119 for (curr = sections; curr; curr = curr->next)
8121 len = strlen (curr->name);
8122 if (len > strlen (name))
8125 if (strncmp (curr->name, name, len) == 0)
8127 if (strncmp (".end", name + len, 4) == 0)
8129 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8133 /* Insert more pseudo-section names here, if you like. */
8141 undefined_reference (const char *reftype, const char *name)
8143 /* xgettext:c-format */
8144 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8149 eval_symbol (bfd_vma *result,
8152 struct elf_final_link_info *flinfo,
8154 Elf_Internal_Sym *isymbuf,
8163 const char *sym = *symp;
8165 bfd_boolean symbol_is_section = FALSE;
8170 if (len < 1 || len > sizeof (symbuf))
8172 bfd_set_error (bfd_error_invalid_operation);
8185 *result = strtoul (sym, (char **) symp, 16);
8189 symbol_is_section = TRUE;
8193 symlen = strtol (sym, (char **) symp, 10);
8194 sym = *symp + 1; /* Skip the trailing ':'. */
8196 if (symend < sym || symlen + 1 > sizeof (symbuf))
8198 bfd_set_error (bfd_error_invalid_operation);
8202 memcpy (symbuf, sym, symlen);
8203 symbuf[symlen] = '\0';
8204 *symp = sym + symlen;
8206 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8207 the symbol as a section, or vice-versa. so we're pretty liberal in our
8208 interpretation here; section means "try section first", not "must be a
8209 section", and likewise with symbol. */
8211 if (symbol_is_section)
8213 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8214 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8215 isymbuf, locsymcount))
8217 undefined_reference ("section", symbuf);
8223 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8224 isymbuf, locsymcount)
8225 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8228 undefined_reference ("symbol", symbuf);
8235 /* All that remains are operators. */
8237 #define UNARY_OP(op) \
8238 if (strncmp (sym, #op, strlen (#op)) == 0) \
8240 sym += strlen (#op); \
8244 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8245 isymbuf, locsymcount, signed_p)) \
8248 *result = op ((bfd_signed_vma) a); \
8254 #define BINARY_OP(op) \
8255 if (strncmp (sym, #op, strlen (#op)) == 0) \
8257 sym += strlen (#op); \
8261 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8262 isymbuf, locsymcount, signed_p)) \
8265 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8266 isymbuf, locsymcount, signed_p)) \
8269 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8299 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8300 bfd_set_error (bfd_error_invalid_operation);
8306 put_value (bfd_vma size,
8307 unsigned long chunksz,
8312 location += (size - chunksz);
8314 for (; size; size -= chunksz, location -= chunksz)
8319 bfd_put_8 (input_bfd, x, location);
8323 bfd_put_16 (input_bfd, x, location);
8327 bfd_put_32 (input_bfd, x, location);
8328 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8334 bfd_put_64 (input_bfd, x, location);
8335 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8348 get_value (bfd_vma size,
8349 unsigned long chunksz,
8356 /* Sanity checks. */
8357 BFD_ASSERT (chunksz <= sizeof (x)
8360 && (size % chunksz) == 0
8361 && input_bfd != NULL
8362 && location != NULL);
8364 if (chunksz == sizeof (x))
8366 BFD_ASSERT (size == chunksz);
8368 /* Make sure that we do not perform an undefined shift operation.
8369 We know that size == chunksz so there will only be one iteration
8370 of the loop below. */
8374 shift = 8 * chunksz;
8376 for (; size; size -= chunksz, location += chunksz)
8381 x = (x << shift) | bfd_get_8 (input_bfd, location);
8384 x = (x << shift) | bfd_get_16 (input_bfd, location);
8387 x = (x << shift) | bfd_get_32 (input_bfd, location);
8391 x = (x << shift) | bfd_get_64 (input_bfd, location);
8402 decode_complex_addend (unsigned long *start, /* in bits */
8403 unsigned long *oplen, /* in bits */
8404 unsigned long *len, /* in bits */
8405 unsigned long *wordsz, /* in bytes */
8406 unsigned long *chunksz, /* in bytes */
8407 unsigned long *lsb0_p,
8408 unsigned long *signed_p,
8409 unsigned long *trunc_p,
8410 unsigned long encoded)
8412 * start = encoded & 0x3F;
8413 * len = (encoded >> 6) & 0x3F;
8414 * oplen = (encoded >> 12) & 0x3F;
8415 * wordsz = (encoded >> 18) & 0xF;
8416 * chunksz = (encoded >> 22) & 0xF;
8417 * lsb0_p = (encoded >> 27) & 1;
8418 * signed_p = (encoded >> 28) & 1;
8419 * trunc_p = (encoded >> 29) & 1;
8422 bfd_reloc_status_type
8423 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8424 asection *input_section ATTRIBUTE_UNUSED,
8426 Elf_Internal_Rela *rel,
8429 bfd_vma shift, x, mask;
8430 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8431 bfd_reloc_status_type r;
8433 /* Perform this reloc, since it is complex.
8434 (this is not to say that it necessarily refers to a complex
8435 symbol; merely that it is a self-describing CGEN based reloc.
8436 i.e. the addend has the complete reloc information (bit start, end,
8437 word size, etc) encoded within it.). */
8439 decode_complex_addend (&start, &oplen, &len, &wordsz,
8440 &chunksz, &lsb0_p, &signed_p,
8441 &trunc_p, rel->r_addend);
8443 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8446 shift = (start + 1) - len;
8448 shift = (8 * wordsz) - (start + len);
8450 x = get_value (wordsz, chunksz, input_bfd,
8451 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8454 printf ("Doing complex reloc: "
8455 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8456 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8457 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8458 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8459 oplen, (unsigned long) x, (unsigned long) mask,
8460 (unsigned long) relocation);
8465 /* Now do an overflow check. */
8466 r = bfd_check_overflow ((signed_p
8467 ? complain_overflow_signed
8468 : complain_overflow_unsigned),
8469 len, 0, (8 * wordsz),
8473 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8476 printf (" relocation: %8.8lx\n"
8477 " shifted mask: %8.8lx\n"
8478 " shifted/masked reloc: %8.8lx\n"
8479 " result: %8.8lx\n",
8480 (unsigned long) relocation, (unsigned long) (mask << shift),
8481 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8483 put_value (wordsz, chunksz, input_bfd, x,
8484 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8488 /* Functions to read r_offset from external (target order) reloc
8489 entry. Faster than bfd_getl32 et al, because we let the compiler
8490 know the value is aligned. */
8493 ext32l_r_offset (const void *p)
8500 const union aligned32 *a
8501 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8503 uint32_t aval = ( (uint32_t) a->c[0]
8504 | (uint32_t) a->c[1] << 8
8505 | (uint32_t) a->c[2] << 16
8506 | (uint32_t) a->c[3] << 24);
8511 ext32b_r_offset (const void *p)
8518 const union aligned32 *a
8519 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8521 uint32_t aval = ( (uint32_t) a->c[0] << 24
8522 | (uint32_t) a->c[1] << 16
8523 | (uint32_t) a->c[2] << 8
8524 | (uint32_t) a->c[3]);
8528 #ifdef BFD_HOST_64_BIT
8530 ext64l_r_offset (const void *p)
8537 const union aligned64 *a
8538 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8540 uint64_t aval = ( (uint64_t) a->c[0]
8541 | (uint64_t) a->c[1] << 8
8542 | (uint64_t) a->c[2] << 16
8543 | (uint64_t) a->c[3] << 24
8544 | (uint64_t) a->c[4] << 32
8545 | (uint64_t) a->c[5] << 40
8546 | (uint64_t) a->c[6] << 48
8547 | (uint64_t) a->c[7] << 56);
8552 ext64b_r_offset (const void *p)
8559 const union aligned64 *a
8560 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8562 uint64_t aval = ( (uint64_t) a->c[0] << 56
8563 | (uint64_t) a->c[1] << 48
8564 | (uint64_t) a->c[2] << 40
8565 | (uint64_t) a->c[3] << 32
8566 | (uint64_t) a->c[4] << 24
8567 | (uint64_t) a->c[5] << 16
8568 | (uint64_t) a->c[6] << 8
8569 | (uint64_t) a->c[7]);
8574 /* When performing a relocatable link, the input relocations are
8575 preserved. But, if they reference global symbols, the indices
8576 referenced must be updated. Update all the relocations found in
8580 elf_link_adjust_relocs (bfd *abfd,
8582 struct bfd_elf_section_reloc_data *reldata,
8584 struct bfd_link_info *info)
8587 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8589 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8590 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8591 bfd_vma r_type_mask;
8593 unsigned int count = reldata->count;
8594 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8596 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8598 swap_in = bed->s->swap_reloc_in;
8599 swap_out = bed->s->swap_reloc_out;
8601 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8603 swap_in = bed->s->swap_reloca_in;
8604 swap_out = bed->s->swap_reloca_out;
8609 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8612 if (bed->s->arch_size == 32)
8619 r_type_mask = 0xffffffff;
8623 erela = reldata->hdr->contents;
8624 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8626 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8629 if (*rel_hash == NULL)
8632 if ((*rel_hash)->indx == -2
8633 && info->gc_sections
8634 && ! info->gc_keep_exported)
8636 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8637 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8639 (*rel_hash)->root.root.string);
8640 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8642 bfd_set_error (bfd_error_invalid_operation);
8645 BFD_ASSERT ((*rel_hash)->indx >= 0);
8647 (*swap_in) (abfd, erela, irela);
8648 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8649 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8650 | (irela[j].r_info & r_type_mask));
8651 (*swap_out) (abfd, irela, erela);
8654 if (bed->elf_backend_update_relocs)
8655 (*bed->elf_backend_update_relocs) (sec, reldata);
8657 if (sort && count != 0)
8659 bfd_vma (*ext_r_off) (const void *);
8662 bfd_byte *base, *end, *p, *loc;
8663 bfd_byte *buf = NULL;
8665 if (bed->s->arch_size == 32)
8667 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8668 ext_r_off = ext32l_r_offset;
8669 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8670 ext_r_off = ext32b_r_offset;
8676 #ifdef BFD_HOST_64_BIT
8677 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8678 ext_r_off = ext64l_r_offset;
8679 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8680 ext_r_off = ext64b_r_offset;
8686 /* Must use a stable sort here. A modified insertion sort,
8687 since the relocs are mostly sorted already. */
8688 elt_size = reldata->hdr->sh_entsize;
8689 base = reldata->hdr->contents;
8690 end = base + count * elt_size;
8691 if (elt_size > sizeof (Elf64_External_Rela))
8694 /* Ensure the first element is lowest. This acts as a sentinel,
8695 speeding the main loop below. */
8696 r_off = (*ext_r_off) (base);
8697 for (p = loc = base; (p += elt_size) < end; )
8699 bfd_vma r_off2 = (*ext_r_off) (p);
8708 /* Don't just swap *base and *loc as that changes the order
8709 of the original base[0] and base[1] if they happen to
8710 have the same r_offset. */
8711 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8712 memcpy (onebuf, loc, elt_size);
8713 memmove (base + elt_size, base, loc - base);
8714 memcpy (base, onebuf, elt_size);
8717 for (p = base + elt_size; (p += elt_size) < end; )
8719 /* base to p is sorted, *p is next to insert. */
8720 r_off = (*ext_r_off) (p);
8721 /* Search the sorted region for location to insert. */
8723 while (r_off < (*ext_r_off) (loc))
8728 /* Chances are there is a run of relocs to insert here,
8729 from one of more input files. Files are not always
8730 linked in order due to the way elf_link_input_bfd is
8731 called. See pr17666. */
8732 size_t sortlen = p - loc;
8733 bfd_vma r_off2 = (*ext_r_off) (loc);
8734 size_t runlen = elt_size;
8735 size_t buf_size = 96 * 1024;
8736 while (p + runlen < end
8737 && (sortlen <= buf_size
8738 || runlen + elt_size <= buf_size)
8739 && r_off2 > (*ext_r_off) (p + runlen))
8743 buf = bfd_malloc (buf_size);
8747 if (runlen < sortlen)
8749 memcpy (buf, p, runlen);
8750 memmove (loc + runlen, loc, sortlen);
8751 memcpy (loc, buf, runlen);
8755 memcpy (buf, loc, sortlen);
8756 memmove (loc, p, runlen);
8757 memcpy (loc + runlen, buf, sortlen);
8759 p += runlen - elt_size;
8762 /* Hashes are no longer valid. */
8763 free (reldata->hashes);
8764 reldata->hashes = NULL;
8770 struct elf_link_sort_rela
8776 enum elf_reloc_type_class type;
8777 /* We use this as an array of size int_rels_per_ext_rel. */
8778 Elf_Internal_Rela rela[1];
8782 elf_link_sort_cmp1 (const void *A, const void *B)
8784 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8785 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8786 int relativea, relativeb;
8788 relativea = a->type == reloc_class_relative;
8789 relativeb = b->type == reloc_class_relative;
8791 if (relativea < relativeb)
8793 if (relativea > relativeb)
8795 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8797 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8799 if (a->rela->r_offset < b->rela->r_offset)
8801 if (a->rela->r_offset > b->rela->r_offset)
8807 elf_link_sort_cmp2 (const void *A, const void *B)
8809 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8810 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8812 if (a->type < b->type)
8814 if (a->type > b->type)
8816 if (a->u.offset < b->u.offset)
8818 if (a->u.offset > b->u.offset)
8820 if (a->rela->r_offset < b->rela->r_offset)
8822 if (a->rela->r_offset > b->rela->r_offset)
8828 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8830 asection *dynamic_relocs;
8833 bfd_size_type count, size;
8834 size_t i, ret, sort_elt, ext_size;
8835 bfd_byte *sort, *s_non_relative, *p;
8836 struct elf_link_sort_rela *sq;
8837 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8838 int i2e = bed->s->int_rels_per_ext_rel;
8839 unsigned int opb = bfd_octets_per_byte (abfd);
8840 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8841 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8842 struct bfd_link_order *lo;
8844 bfd_boolean use_rela;
8846 /* Find a dynamic reloc section. */
8847 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8848 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8849 if (rela_dyn != NULL && rela_dyn->size > 0
8850 && rel_dyn != NULL && rel_dyn->size > 0)
8852 bfd_boolean use_rela_initialised = FALSE;
8854 /* This is just here to stop gcc from complaining.
8855 Its initialization checking code is not perfect. */
8858 /* Both sections are present. Examine the sizes
8859 of the indirect sections to help us choose. */
8860 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8861 if (lo->type == bfd_indirect_link_order)
8863 asection *o = lo->u.indirect.section;
8865 if ((o->size % bed->s->sizeof_rela) == 0)
8867 if ((o->size % bed->s->sizeof_rel) == 0)
8868 /* Section size is divisible by both rel and rela sizes.
8869 It is of no help to us. */
8873 /* Section size is only divisible by rela. */
8874 if (use_rela_initialised && !use_rela)
8876 _bfd_error_handler (_("%B: Unable to sort relocs - "
8877 "they are in more than one size"),
8879 bfd_set_error (bfd_error_invalid_operation);
8885 use_rela_initialised = TRUE;
8889 else if ((o->size % bed->s->sizeof_rel) == 0)
8891 /* Section size is only divisible by rel. */
8892 if (use_rela_initialised && use_rela)
8894 _bfd_error_handler (_("%B: Unable to sort relocs - "
8895 "they are in more than one size"),
8897 bfd_set_error (bfd_error_invalid_operation);
8903 use_rela_initialised = TRUE;
8908 /* The section size is not divisible by either -
8909 something is wrong. */
8910 _bfd_error_handler (_("%B: Unable to sort relocs - "
8911 "they are of an unknown size"), abfd);
8912 bfd_set_error (bfd_error_invalid_operation);
8917 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8918 if (lo->type == bfd_indirect_link_order)
8920 asection *o = lo->u.indirect.section;
8922 if ((o->size % bed->s->sizeof_rela) == 0)
8924 if ((o->size % bed->s->sizeof_rel) == 0)
8925 /* Section size is divisible by both rel and rela sizes.
8926 It is of no help to us. */
8930 /* Section size is only divisible by rela. */
8931 if (use_rela_initialised && !use_rela)
8933 _bfd_error_handler (_("%B: Unable to sort relocs - "
8934 "they are in more than one size"),
8936 bfd_set_error (bfd_error_invalid_operation);
8942 use_rela_initialised = TRUE;
8946 else if ((o->size % bed->s->sizeof_rel) == 0)
8948 /* Section size is only divisible by rel. */
8949 if (use_rela_initialised && use_rela)
8951 _bfd_error_handler (_("%B: Unable to sort relocs - "
8952 "they are in more than one size"),
8954 bfd_set_error (bfd_error_invalid_operation);
8960 use_rela_initialised = TRUE;
8965 /* The section size is not divisible by either -
8966 something is wrong. */
8967 _bfd_error_handler (_("%B: Unable to sort relocs - "
8968 "they are of an unknown size"), abfd);
8969 bfd_set_error (bfd_error_invalid_operation);
8974 if (! use_rela_initialised)
8978 else if (rela_dyn != NULL && rela_dyn->size > 0)
8980 else if (rel_dyn != NULL && rel_dyn->size > 0)
8987 dynamic_relocs = rela_dyn;
8988 ext_size = bed->s->sizeof_rela;
8989 swap_in = bed->s->swap_reloca_in;
8990 swap_out = bed->s->swap_reloca_out;
8994 dynamic_relocs = rel_dyn;
8995 ext_size = bed->s->sizeof_rel;
8996 swap_in = bed->s->swap_reloc_in;
8997 swap_out = bed->s->swap_reloc_out;
9001 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9002 if (lo->type == bfd_indirect_link_order)
9003 size += lo->u.indirect.section->size;
9005 if (size != dynamic_relocs->size)
9008 sort_elt = (sizeof (struct elf_link_sort_rela)
9009 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9011 count = dynamic_relocs->size / ext_size;
9014 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9018 (*info->callbacks->warning)
9019 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9023 if (bed->s->arch_size == 32)
9024 r_sym_mask = ~(bfd_vma) 0xff;
9026 r_sym_mask = ~(bfd_vma) 0xffffffff;
9028 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9029 if (lo->type == bfd_indirect_link_order)
9031 bfd_byte *erel, *erelend;
9032 asection *o = lo->u.indirect.section;
9034 if (o->contents == NULL && o->size != 0)
9036 /* This is a reloc section that is being handled as a normal
9037 section. See bfd_section_from_shdr. We can't combine
9038 relocs in this case. */
9043 erelend = o->contents + o->size;
9044 p = sort + o->output_offset * opb / ext_size * sort_elt;
9046 while (erel < erelend)
9048 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9050 (*swap_in) (abfd, erel, s->rela);
9051 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9052 s->u.sym_mask = r_sym_mask;
9058 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9060 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9062 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9063 if (s->type != reloc_class_relative)
9069 sq = (struct elf_link_sort_rela *) s_non_relative;
9070 for (; i < count; i++, p += sort_elt)
9072 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9073 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9075 sp->u.offset = sq->rela->r_offset;
9078 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9080 struct elf_link_hash_table *htab = elf_hash_table (info);
9081 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9083 /* We have plt relocs in .rela.dyn. */
9084 sq = (struct elf_link_sort_rela *) sort;
9085 for (i = 0; i < count; i++)
9086 if (sq[count - i - 1].type != reloc_class_plt)
9088 if (i != 0 && htab->srelplt->size == i * ext_size)
9090 struct bfd_link_order **plo;
9091 /* Put srelplt link_order last. This is so the output_offset
9092 set in the next loop is correct for DT_JMPREL. */
9093 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9094 if ((*plo)->type == bfd_indirect_link_order
9095 && (*plo)->u.indirect.section == htab->srelplt)
9101 plo = &(*plo)->next;
9104 dynamic_relocs->map_tail.link_order = lo;
9109 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9110 if (lo->type == bfd_indirect_link_order)
9112 bfd_byte *erel, *erelend;
9113 asection *o = lo->u.indirect.section;
9116 erelend = o->contents + o->size;
9117 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9118 while (erel < erelend)
9120 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9121 (*swap_out) (abfd, s->rela, erel);
9128 *psec = dynamic_relocs;
9132 /* Add a symbol to the output symbol string table. */
9135 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9137 Elf_Internal_Sym *elfsym,
9138 asection *input_sec,
9139 struct elf_link_hash_entry *h)
9141 int (*output_symbol_hook)
9142 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9143 struct elf_link_hash_entry *);
9144 struct elf_link_hash_table *hash_table;
9145 const struct elf_backend_data *bed;
9146 bfd_size_type strtabsize;
9148 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9150 bed = get_elf_backend_data (flinfo->output_bfd);
9151 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9152 if (output_symbol_hook != NULL)
9154 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9161 || (input_sec->flags & SEC_EXCLUDE))
9162 elfsym->st_name = (unsigned long) -1;
9165 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9166 to get the final offset for st_name. */
9168 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9170 if (elfsym->st_name == (unsigned long) -1)
9174 hash_table = elf_hash_table (flinfo->info);
9175 strtabsize = hash_table->strtabsize;
9176 if (strtabsize <= hash_table->strtabcount)
9178 strtabsize += strtabsize;
9179 hash_table->strtabsize = strtabsize;
9180 strtabsize *= sizeof (*hash_table->strtab);
9182 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9184 if (hash_table->strtab == NULL)
9187 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9188 hash_table->strtab[hash_table->strtabcount].dest_index
9189 = hash_table->strtabcount;
9190 hash_table->strtab[hash_table->strtabcount].destshndx_index
9191 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9193 bfd_get_symcount (flinfo->output_bfd) += 1;
9194 hash_table->strtabcount += 1;
9199 /* Swap symbols out to the symbol table and flush the output symbols to
9203 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9205 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9208 const struct elf_backend_data *bed;
9210 Elf_Internal_Shdr *hdr;
9214 if (!hash_table->strtabcount)
9217 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9219 bed = get_elf_backend_data (flinfo->output_bfd);
9221 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9222 symbuf = (bfd_byte *) bfd_malloc (amt);
9226 if (flinfo->symshndxbuf)
9228 amt = sizeof (Elf_External_Sym_Shndx);
9229 amt *= bfd_get_symcount (flinfo->output_bfd);
9230 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9231 if (flinfo->symshndxbuf == NULL)
9238 for (i = 0; i < hash_table->strtabcount; i++)
9240 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9241 if (elfsym->sym.st_name == (unsigned long) -1)
9242 elfsym->sym.st_name = 0;
9245 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9246 elfsym->sym.st_name);
9247 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9248 ((bfd_byte *) symbuf
9249 + (elfsym->dest_index
9250 * bed->s->sizeof_sym)),
9251 (flinfo->symshndxbuf
9252 + elfsym->destshndx_index));
9255 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9256 pos = hdr->sh_offset + hdr->sh_size;
9257 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9258 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9259 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9261 hdr->sh_size += amt;
9269 free (hash_table->strtab);
9270 hash_table->strtab = NULL;
9275 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9278 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9280 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9281 && sym->st_shndx < SHN_LORESERVE)
9283 /* The gABI doesn't support dynamic symbols in output sections
9286 /* xgettext:c-format */
9287 (_("%B: Too many sections: %d (>= %d)"),
9288 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9289 bfd_set_error (bfd_error_nonrepresentable_section);
9295 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9296 allowing an unsatisfied unversioned symbol in the DSO to match a
9297 versioned symbol that would normally require an explicit version.
9298 We also handle the case that a DSO references a hidden symbol
9299 which may be satisfied by a versioned symbol in another DSO. */
9302 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9303 const struct elf_backend_data *bed,
9304 struct elf_link_hash_entry *h)
9307 struct elf_link_loaded_list *loaded;
9309 if (!is_elf_hash_table (info->hash))
9312 /* Check indirect symbol. */
9313 while (h->root.type == bfd_link_hash_indirect)
9314 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9316 switch (h->root.type)
9322 case bfd_link_hash_undefined:
9323 case bfd_link_hash_undefweak:
9324 abfd = h->root.u.undef.abfd;
9326 || (abfd->flags & DYNAMIC) == 0
9327 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9331 case bfd_link_hash_defined:
9332 case bfd_link_hash_defweak:
9333 abfd = h->root.u.def.section->owner;
9336 case bfd_link_hash_common:
9337 abfd = h->root.u.c.p->section->owner;
9340 BFD_ASSERT (abfd != NULL);
9342 for (loaded = elf_hash_table (info)->loaded;
9344 loaded = loaded->next)
9347 Elf_Internal_Shdr *hdr;
9351 Elf_Internal_Shdr *versymhdr;
9352 Elf_Internal_Sym *isym;
9353 Elf_Internal_Sym *isymend;
9354 Elf_Internal_Sym *isymbuf;
9355 Elf_External_Versym *ever;
9356 Elf_External_Versym *extversym;
9358 input = loaded->abfd;
9360 /* We check each DSO for a possible hidden versioned definition. */
9362 || (input->flags & DYNAMIC) == 0
9363 || elf_dynversym (input) == 0)
9366 hdr = &elf_tdata (input)->dynsymtab_hdr;
9368 symcount = hdr->sh_size / bed->s->sizeof_sym;
9369 if (elf_bad_symtab (input))
9371 extsymcount = symcount;
9376 extsymcount = symcount - hdr->sh_info;
9377 extsymoff = hdr->sh_info;
9380 if (extsymcount == 0)
9383 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9385 if (isymbuf == NULL)
9388 /* Read in any version definitions. */
9389 versymhdr = &elf_tdata (input)->dynversym_hdr;
9390 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9391 if (extversym == NULL)
9394 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9395 || (bfd_bread (extversym, versymhdr->sh_size, input)
9396 != versymhdr->sh_size))
9404 ever = extversym + extsymoff;
9405 isymend = isymbuf + extsymcount;
9406 for (isym = isymbuf; isym < isymend; isym++, ever++)
9409 Elf_Internal_Versym iver;
9410 unsigned short version_index;
9412 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9413 || isym->st_shndx == SHN_UNDEF)
9416 name = bfd_elf_string_from_elf_section (input,
9419 if (strcmp (name, h->root.root.string) != 0)
9422 _bfd_elf_swap_versym_in (input, ever, &iver);
9424 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9426 && h->forced_local))
9428 /* If we have a non-hidden versioned sym, then it should
9429 have provided a definition for the undefined sym unless
9430 it is defined in a non-shared object and forced local.
9435 version_index = iver.vs_vers & VERSYM_VERSION;
9436 if (version_index == 1 || version_index == 2)
9438 /* This is the base or first version. We can use it. */
9452 /* Convert ELF common symbol TYPE. */
9455 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9457 /* Commom symbol can only appear in relocatable link. */
9458 if (!bfd_link_relocatable (info))
9460 switch (info->elf_stt_common)
9464 case elf_stt_common:
9467 case no_elf_stt_common:
9474 /* Add an external symbol to the symbol table. This is called from
9475 the hash table traversal routine. When generating a shared object,
9476 we go through the symbol table twice. The first time we output
9477 anything that might have been forced to local scope in a version
9478 script. The second time we output the symbols that are still
9482 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9484 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9485 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9486 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9488 Elf_Internal_Sym sym;
9489 asection *input_sec;
9490 const struct elf_backend_data *bed;
9495 if (h->root.type == bfd_link_hash_warning)
9497 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9498 if (h->root.type == bfd_link_hash_new)
9502 /* Decide whether to output this symbol in this pass. */
9503 if (eoinfo->localsyms)
9505 if (!h->forced_local)
9510 if (h->forced_local)
9514 bed = get_elf_backend_data (flinfo->output_bfd);
9516 if (h->root.type == bfd_link_hash_undefined)
9518 /* If we have an undefined symbol reference here then it must have
9519 come from a shared library that is being linked in. (Undefined
9520 references in regular files have already been handled unless
9521 they are in unreferenced sections which are removed by garbage
9523 bfd_boolean ignore_undef = FALSE;
9525 /* Some symbols may be special in that the fact that they're
9526 undefined can be safely ignored - let backend determine that. */
9527 if (bed->elf_backend_ignore_undef_symbol)
9528 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9530 /* If we are reporting errors for this situation then do so now. */
9533 && (!h->ref_regular || flinfo->info->gc_sections)
9534 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9535 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9536 (*flinfo->info->callbacks->undefined_symbol)
9537 (flinfo->info, h->root.root.string,
9538 h->ref_regular ? NULL : h->root.u.undef.abfd,
9540 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9542 /* Strip a global symbol defined in a discarded section. */
9547 /* We should also warn if a forced local symbol is referenced from
9548 shared libraries. */
9549 if (bfd_link_executable (flinfo->info)
9554 && h->ref_dynamic_nonweak
9555 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9559 struct elf_link_hash_entry *hi = h;
9561 /* Check indirect symbol. */
9562 while (hi->root.type == bfd_link_hash_indirect)
9563 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9565 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9566 /* xgettext:c-format */
9567 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9568 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9569 /* xgettext:c-format */
9570 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9572 /* xgettext:c-format */
9573 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9574 def_bfd = flinfo->output_bfd;
9575 if (hi->root.u.def.section != bfd_abs_section_ptr)
9576 def_bfd = hi->root.u.def.section->owner;
9577 _bfd_error_handler (msg, flinfo->output_bfd,
9578 h->root.root.string, def_bfd);
9579 bfd_set_error (bfd_error_bad_value);
9580 eoinfo->failed = TRUE;
9584 /* We don't want to output symbols that have never been mentioned by
9585 a regular file, or that we have been told to strip. However, if
9586 h->indx is set to -2, the symbol is used by a reloc and we must
9591 else if ((h->def_dynamic
9593 || h->root.type == bfd_link_hash_new)
9597 else if (flinfo->info->strip == strip_all)
9599 else if (flinfo->info->strip == strip_some
9600 && bfd_hash_lookup (flinfo->info->keep_hash,
9601 h->root.root.string, FALSE, FALSE) == NULL)
9603 else if ((h->root.type == bfd_link_hash_defined
9604 || h->root.type == bfd_link_hash_defweak)
9605 && ((flinfo->info->strip_discarded
9606 && discarded_section (h->root.u.def.section))
9607 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9608 && h->root.u.def.section->owner != NULL
9609 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9611 else if ((h->root.type == bfd_link_hash_undefined
9612 || h->root.type == bfd_link_hash_undefweak)
9613 && h->root.u.undef.abfd != NULL
9614 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9619 /* If we're stripping it, and it's not a dynamic symbol, there's
9620 nothing else to do. However, if it is a forced local symbol or
9621 an ifunc symbol we need to give the backend finish_dynamic_symbol
9622 function a chance to make it dynamic. */
9625 && type != STT_GNU_IFUNC
9626 && !h->forced_local)
9630 sym.st_size = h->size;
9631 sym.st_other = h->other;
9632 switch (h->root.type)
9635 case bfd_link_hash_new:
9636 case bfd_link_hash_warning:
9640 case bfd_link_hash_undefined:
9641 case bfd_link_hash_undefweak:
9642 input_sec = bfd_und_section_ptr;
9643 sym.st_shndx = SHN_UNDEF;
9646 case bfd_link_hash_defined:
9647 case bfd_link_hash_defweak:
9649 input_sec = h->root.u.def.section;
9650 if (input_sec->output_section != NULL)
9653 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9654 input_sec->output_section);
9655 if (sym.st_shndx == SHN_BAD)
9658 /* xgettext:c-format */
9659 (_("%B: could not find output section %A for input section %A"),
9660 flinfo->output_bfd, input_sec->output_section, input_sec);
9661 bfd_set_error (bfd_error_nonrepresentable_section);
9662 eoinfo->failed = TRUE;
9666 /* ELF symbols in relocatable files are section relative,
9667 but in nonrelocatable files they are virtual
9669 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9670 if (!bfd_link_relocatable (flinfo->info))
9672 sym.st_value += input_sec->output_section->vma;
9673 if (h->type == STT_TLS)
9675 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9676 if (tls_sec != NULL)
9677 sym.st_value -= tls_sec->vma;
9683 BFD_ASSERT (input_sec->owner == NULL
9684 || (input_sec->owner->flags & DYNAMIC) != 0);
9685 sym.st_shndx = SHN_UNDEF;
9686 input_sec = bfd_und_section_ptr;
9691 case bfd_link_hash_common:
9692 input_sec = h->root.u.c.p->section;
9693 sym.st_shndx = bed->common_section_index (input_sec);
9694 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9697 case bfd_link_hash_indirect:
9698 /* These symbols are created by symbol versioning. They point
9699 to the decorated version of the name. For example, if the
9700 symbol foo@@GNU_1.2 is the default, which should be used when
9701 foo is used with no version, then we add an indirect symbol
9702 foo which points to foo@@GNU_1.2. We ignore these symbols,
9703 since the indirected symbol is already in the hash table. */
9707 if (type == STT_COMMON || type == STT_OBJECT)
9708 switch (h->root.type)
9710 case bfd_link_hash_common:
9711 type = elf_link_convert_common_type (flinfo->info, type);
9713 case bfd_link_hash_defined:
9714 case bfd_link_hash_defweak:
9715 if (bed->common_definition (&sym))
9716 type = elf_link_convert_common_type (flinfo->info, type);
9720 case bfd_link_hash_undefined:
9721 case bfd_link_hash_undefweak:
9727 if (h->forced_local)
9729 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9730 /* Turn off visibility on local symbol. */
9731 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9733 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9734 else if (h->unique_global && h->def_regular)
9735 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9736 else if (h->root.type == bfd_link_hash_undefweak
9737 || h->root.type == bfd_link_hash_defweak)
9738 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9740 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9741 sym.st_target_internal = h->target_internal;
9743 /* Give the processor backend a chance to tweak the symbol value,
9744 and also to finish up anything that needs to be done for this
9745 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9746 forced local syms when non-shared is due to a historical quirk.
9747 STT_GNU_IFUNC symbol must go through PLT. */
9748 if ((h->type == STT_GNU_IFUNC
9750 && !bfd_link_relocatable (flinfo->info))
9751 || ((h->dynindx != -1
9753 && ((bfd_link_pic (flinfo->info)
9754 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9755 || h->root.type != bfd_link_hash_undefweak))
9756 || !h->forced_local)
9757 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9759 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9760 (flinfo->output_bfd, flinfo->info, h, &sym)))
9762 eoinfo->failed = TRUE;
9767 /* If we are marking the symbol as undefined, and there are no
9768 non-weak references to this symbol from a regular object, then
9769 mark the symbol as weak undefined; if there are non-weak
9770 references, mark the symbol as strong. We can't do this earlier,
9771 because it might not be marked as undefined until the
9772 finish_dynamic_symbol routine gets through with it. */
9773 if (sym.st_shndx == SHN_UNDEF
9775 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9776 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9779 type = ELF_ST_TYPE (sym.st_info);
9781 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9782 if (type == STT_GNU_IFUNC)
9785 if (h->ref_regular_nonweak)
9786 bindtype = STB_GLOBAL;
9788 bindtype = STB_WEAK;
9789 sym.st_info = ELF_ST_INFO (bindtype, type);
9792 /* If this is a symbol defined in a dynamic library, don't use the
9793 symbol size from the dynamic library. Relinking an executable
9794 against a new library may introduce gratuitous changes in the
9795 executable's symbols if we keep the size. */
9796 if (sym.st_shndx == SHN_UNDEF
9801 /* If a non-weak symbol with non-default visibility is not defined
9802 locally, it is a fatal error. */
9803 if (!bfd_link_relocatable (flinfo->info)
9804 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9805 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9806 && h->root.type == bfd_link_hash_undefined
9811 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9812 /* xgettext:c-format */
9813 msg = _("%B: protected symbol `%s' isn't defined");
9814 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9815 /* xgettext:c-format */
9816 msg = _("%B: internal symbol `%s' isn't defined");
9818 /* xgettext:c-format */
9819 msg = _("%B: hidden symbol `%s' isn't defined");
9820 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9821 bfd_set_error (bfd_error_bad_value);
9822 eoinfo->failed = TRUE;
9826 /* If this symbol should be put in the .dynsym section, then put it
9827 there now. We already know the symbol index. We also fill in
9828 the entry in the .hash section. */
9829 if (elf_hash_table (flinfo->info)->dynsym != NULL
9831 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9835 /* Since there is no version information in the dynamic string,
9836 if there is no version info in symbol version section, we will
9837 have a run-time problem if not linking executable, referenced
9838 by shared library, or not bound locally. */
9839 if (h->verinfo.verdef == NULL
9840 && (!bfd_link_executable (flinfo->info)
9842 || !h->def_regular))
9844 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9846 if (p && p [1] != '\0')
9849 /* xgettext:c-format */
9850 (_("%B: No symbol version section for versioned symbol `%s'"),
9851 flinfo->output_bfd, h->root.root.string);
9852 eoinfo->failed = TRUE;
9857 sym.st_name = h->dynstr_index;
9858 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9859 + h->dynindx * bed->s->sizeof_sym);
9860 if (!check_dynsym (flinfo->output_bfd, &sym))
9862 eoinfo->failed = TRUE;
9865 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9867 if (flinfo->hash_sec != NULL)
9869 size_t hash_entry_size;
9870 bfd_byte *bucketpos;
9875 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9876 bucket = h->u.elf_hash_value % bucketcount;
9879 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9880 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9881 + (bucket + 2) * hash_entry_size);
9882 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9883 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9885 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9886 ((bfd_byte *) flinfo->hash_sec->contents
9887 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9890 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9892 Elf_Internal_Versym iversym;
9893 Elf_External_Versym *eversym;
9895 if (!h->def_regular)
9897 if (h->verinfo.verdef == NULL
9898 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9899 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9900 iversym.vs_vers = 0;
9902 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9906 if (h->verinfo.vertree == NULL)
9907 iversym.vs_vers = 1;
9909 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9910 if (flinfo->info->create_default_symver)
9914 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9916 if (h->versioned == versioned_hidden && h->def_regular)
9917 iversym.vs_vers |= VERSYM_HIDDEN;
9919 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9920 eversym += h->dynindx;
9921 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9925 /* If the symbol is undefined, and we didn't output it to .dynsym,
9926 strip it from .symtab too. Obviously we can't do this for
9927 relocatable output or when needed for --emit-relocs. */
9928 else if (input_sec == bfd_und_section_ptr
9930 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
9931 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
9932 && !bfd_link_relocatable (flinfo->info))
9935 /* Also strip others that we couldn't earlier due to dynamic symbol
9939 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9942 /* Output a FILE symbol so that following locals are not associated
9943 with the wrong input file. We need one for forced local symbols
9944 if we've seen more than one FILE symbol or when we have exactly
9945 one FILE symbol but global symbols are present in a file other
9946 than the one with the FILE symbol. We also need one if linker
9947 defined symbols are present. In practice these conditions are
9948 always met, so just emit the FILE symbol unconditionally. */
9949 if (eoinfo->localsyms
9950 && !eoinfo->file_sym_done
9951 && eoinfo->flinfo->filesym_count != 0)
9953 Elf_Internal_Sym fsym;
9955 memset (&fsym, 0, sizeof (fsym));
9956 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9957 fsym.st_shndx = SHN_ABS;
9958 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9959 bfd_und_section_ptr, NULL))
9962 eoinfo->file_sym_done = TRUE;
9965 indx = bfd_get_symcount (flinfo->output_bfd);
9966 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9970 eoinfo->failed = TRUE;
9975 else if (h->indx == -2)
9981 /* Return TRUE if special handling is done for relocs in SEC against
9982 symbols defined in discarded sections. */
9985 elf_section_ignore_discarded_relocs (asection *sec)
9987 const struct elf_backend_data *bed;
9989 switch (sec->sec_info_type)
9991 case SEC_INFO_TYPE_STABS:
9992 case SEC_INFO_TYPE_EH_FRAME:
9993 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9999 bed = get_elf_backend_data (sec->owner);
10000 if (bed->elf_backend_ignore_discarded_relocs != NULL
10001 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10007 /* Return a mask saying how ld should treat relocations in SEC against
10008 symbols defined in discarded sections. If this function returns
10009 COMPLAIN set, ld will issue a warning message. If this function
10010 returns PRETEND set, and the discarded section was link-once and the
10011 same size as the kept link-once section, ld will pretend that the
10012 symbol was actually defined in the kept section. Otherwise ld will
10013 zero the reloc (at least that is the intent, but some cooperation by
10014 the target dependent code is needed, particularly for REL targets). */
10017 _bfd_elf_default_action_discarded (asection *sec)
10019 if (sec->flags & SEC_DEBUGGING)
10022 if (strcmp (".eh_frame", sec->name) == 0)
10025 if (strcmp (".gcc_except_table", sec->name) == 0)
10028 return COMPLAIN | PRETEND;
10031 /* Find a match between a section and a member of a section group. */
10034 match_group_member (asection *sec, asection *group,
10035 struct bfd_link_info *info)
10037 asection *first = elf_next_in_group (group);
10038 asection *s = first;
10042 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10045 s = elf_next_in_group (s);
10053 /* Check if the kept section of a discarded section SEC can be used
10054 to replace it. Return the replacement if it is OK. Otherwise return
10058 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10062 kept = sec->kept_section;
10065 if ((kept->flags & SEC_GROUP) != 0)
10066 kept = match_group_member (sec, kept, info);
10068 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10069 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10071 sec->kept_section = kept;
10076 /* Link an input file into the linker output file. This function
10077 handles all the sections and relocations of the input file at once.
10078 This is so that we only have to read the local symbols once, and
10079 don't have to keep them in memory. */
10082 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10084 int (*relocate_section)
10085 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10086 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10088 Elf_Internal_Shdr *symtab_hdr;
10089 size_t locsymcount;
10091 Elf_Internal_Sym *isymbuf;
10092 Elf_Internal_Sym *isym;
10093 Elf_Internal_Sym *isymend;
10095 asection **ppsection;
10097 const struct elf_backend_data *bed;
10098 struct elf_link_hash_entry **sym_hashes;
10099 bfd_size_type address_size;
10100 bfd_vma r_type_mask;
10102 bfd_boolean have_file_sym = FALSE;
10104 output_bfd = flinfo->output_bfd;
10105 bed = get_elf_backend_data (output_bfd);
10106 relocate_section = bed->elf_backend_relocate_section;
10108 /* If this is a dynamic object, we don't want to do anything here:
10109 we don't want the local symbols, and we don't want the section
10111 if ((input_bfd->flags & DYNAMIC) != 0)
10114 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10115 if (elf_bad_symtab (input_bfd))
10117 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10122 locsymcount = symtab_hdr->sh_info;
10123 extsymoff = symtab_hdr->sh_info;
10126 /* Read the local symbols. */
10127 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10128 if (isymbuf == NULL && locsymcount != 0)
10130 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10131 flinfo->internal_syms,
10132 flinfo->external_syms,
10133 flinfo->locsym_shndx);
10134 if (isymbuf == NULL)
10138 /* Find local symbol sections and adjust values of symbols in
10139 SEC_MERGE sections. Write out those local symbols we know are
10140 going into the output file. */
10141 isymend = isymbuf + locsymcount;
10142 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10144 isym++, pindex++, ppsection++)
10148 Elf_Internal_Sym osym;
10154 if (elf_bad_symtab (input_bfd))
10156 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10163 if (isym->st_shndx == SHN_UNDEF)
10164 isec = bfd_und_section_ptr;
10165 else if (isym->st_shndx == SHN_ABS)
10166 isec = bfd_abs_section_ptr;
10167 else if (isym->st_shndx == SHN_COMMON)
10168 isec = bfd_com_section_ptr;
10171 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10174 /* Don't attempt to output symbols with st_shnx in the
10175 reserved range other than SHN_ABS and SHN_COMMON. */
10179 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10180 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10182 _bfd_merged_section_offset (output_bfd, &isec,
10183 elf_section_data (isec)->sec_info,
10189 /* Don't output the first, undefined, symbol. In fact, don't
10190 output any undefined local symbol. */
10191 if (isec == bfd_und_section_ptr)
10194 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10196 /* We never output section symbols. Instead, we use the
10197 section symbol of the corresponding section in the output
10202 /* If we are stripping all symbols, we don't want to output this
10204 if (flinfo->info->strip == strip_all)
10207 /* If we are discarding all local symbols, we don't want to
10208 output this one. If we are generating a relocatable output
10209 file, then some of the local symbols may be required by
10210 relocs; we output them below as we discover that they are
10212 if (flinfo->info->discard == discard_all)
10215 /* If this symbol is defined in a section which we are
10216 discarding, we don't need to keep it. */
10217 if (isym->st_shndx != SHN_UNDEF
10218 && isym->st_shndx < SHN_LORESERVE
10219 && bfd_section_removed_from_list (output_bfd,
10220 isec->output_section))
10223 /* Get the name of the symbol. */
10224 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10229 /* See if we are discarding symbols with this name. */
10230 if ((flinfo->info->strip == strip_some
10231 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10233 || (((flinfo->info->discard == discard_sec_merge
10234 && (isec->flags & SEC_MERGE)
10235 && !bfd_link_relocatable (flinfo->info))
10236 || flinfo->info->discard == discard_l)
10237 && bfd_is_local_label_name (input_bfd, name)))
10240 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10242 if (input_bfd->lto_output)
10243 /* -flto puts a temp file name here. This means builds
10244 are not reproducible. Discard the symbol. */
10246 have_file_sym = TRUE;
10247 flinfo->filesym_count += 1;
10249 if (!have_file_sym)
10251 /* In the absence of debug info, bfd_find_nearest_line uses
10252 FILE symbols to determine the source file for local
10253 function symbols. Provide a FILE symbol here if input
10254 files lack such, so that their symbols won't be
10255 associated with a previous input file. It's not the
10256 source file, but the best we can do. */
10257 have_file_sym = TRUE;
10258 flinfo->filesym_count += 1;
10259 memset (&osym, 0, sizeof (osym));
10260 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10261 osym.st_shndx = SHN_ABS;
10262 if (!elf_link_output_symstrtab (flinfo,
10263 (input_bfd->lto_output ? NULL
10264 : input_bfd->filename),
10265 &osym, bfd_abs_section_ptr,
10272 /* Adjust the section index for the output file. */
10273 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10274 isec->output_section);
10275 if (osym.st_shndx == SHN_BAD)
10278 /* ELF symbols in relocatable files are section relative, but
10279 in executable files they are virtual addresses. Note that
10280 this code assumes that all ELF sections have an associated
10281 BFD section with a reasonable value for output_offset; below
10282 we assume that they also have a reasonable value for
10283 output_section. Any special sections must be set up to meet
10284 these requirements. */
10285 osym.st_value += isec->output_offset;
10286 if (!bfd_link_relocatable (flinfo->info))
10288 osym.st_value += isec->output_section->vma;
10289 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10291 /* STT_TLS symbols are relative to PT_TLS segment base. */
10292 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10293 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10297 indx = bfd_get_symcount (output_bfd);
10298 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10305 if (bed->s->arch_size == 32)
10307 r_type_mask = 0xff;
10313 r_type_mask = 0xffffffff;
10318 /* Relocate the contents of each section. */
10319 sym_hashes = elf_sym_hashes (input_bfd);
10320 for (o = input_bfd->sections; o != NULL; o = o->next)
10322 bfd_byte *contents;
10324 if (! o->linker_mark)
10326 /* This section was omitted from the link. */
10330 if (!flinfo->info->resolve_section_groups
10331 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10333 /* Deal with the group signature symbol. */
10334 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10335 unsigned long symndx = sec_data->this_hdr.sh_info;
10336 asection *osec = o->output_section;
10338 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10339 if (symndx >= locsymcount
10340 || (elf_bad_symtab (input_bfd)
10341 && flinfo->sections[symndx] == NULL))
10343 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10344 while (h->root.type == bfd_link_hash_indirect
10345 || h->root.type == bfd_link_hash_warning)
10346 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10347 /* Arrange for symbol to be output. */
10349 elf_section_data (osec)->this_hdr.sh_info = -2;
10351 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10353 /* We'll use the output section target_index. */
10354 asection *sec = flinfo->sections[symndx]->output_section;
10355 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10359 if (flinfo->indices[symndx] == -1)
10361 /* Otherwise output the local symbol now. */
10362 Elf_Internal_Sym sym = isymbuf[symndx];
10363 asection *sec = flinfo->sections[symndx]->output_section;
10368 name = bfd_elf_string_from_elf_section (input_bfd,
10369 symtab_hdr->sh_link,
10374 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10376 if (sym.st_shndx == SHN_BAD)
10379 sym.st_value += o->output_offset;
10381 indx = bfd_get_symcount (output_bfd);
10382 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10387 flinfo->indices[symndx] = indx;
10391 elf_section_data (osec)->this_hdr.sh_info
10392 = flinfo->indices[symndx];
10396 if ((o->flags & SEC_HAS_CONTENTS) == 0
10397 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10400 if ((o->flags & SEC_LINKER_CREATED) != 0)
10402 /* Section was created by _bfd_elf_link_create_dynamic_sections
10407 /* Get the contents of the section. They have been cached by a
10408 relaxation routine. Note that o is a section in an input
10409 file, so the contents field will not have been set by any of
10410 the routines which work on output files. */
10411 if (elf_section_data (o)->this_hdr.contents != NULL)
10413 contents = elf_section_data (o)->this_hdr.contents;
10414 if (bed->caches_rawsize
10416 && o->rawsize < o->size)
10418 memcpy (flinfo->contents, contents, o->rawsize);
10419 contents = flinfo->contents;
10424 contents = flinfo->contents;
10425 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10429 if ((o->flags & SEC_RELOC) != 0)
10431 Elf_Internal_Rela *internal_relocs;
10432 Elf_Internal_Rela *rel, *relend;
10433 int action_discarded;
10436 /* Get the swapped relocs. */
10438 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10439 flinfo->internal_relocs, FALSE);
10440 if (internal_relocs == NULL
10441 && o->reloc_count > 0)
10444 /* We need to reverse-copy input .ctors/.dtors sections if
10445 they are placed in .init_array/.finit_array for output. */
10446 if (o->size > address_size
10447 && ((strncmp (o->name, ".ctors", 6) == 0
10448 && strcmp (o->output_section->name,
10449 ".init_array") == 0)
10450 || (strncmp (o->name, ".dtors", 6) == 0
10451 && strcmp (o->output_section->name,
10452 ".fini_array") == 0))
10453 && (o->name[6] == 0 || o->name[6] == '.'))
10455 if (o->size * bed->s->int_rels_per_ext_rel
10456 != o->reloc_count * address_size)
10459 /* xgettext:c-format */
10460 (_("error: %B: size of section %A is not "
10461 "multiple of address size"),
10463 bfd_set_error (bfd_error_bad_value);
10466 o->flags |= SEC_ELF_REVERSE_COPY;
10469 action_discarded = -1;
10470 if (!elf_section_ignore_discarded_relocs (o))
10471 action_discarded = (*bed->action_discarded) (o);
10473 /* Run through the relocs evaluating complex reloc symbols and
10474 looking for relocs against symbols from discarded sections
10475 or section symbols from removed link-once sections.
10476 Complain about relocs against discarded sections. Zero
10477 relocs against removed link-once sections. */
10479 rel = internal_relocs;
10480 relend = rel + o->reloc_count;
10481 for ( ; rel < relend; rel++)
10483 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10484 unsigned int s_type;
10485 asection **ps, *sec;
10486 struct elf_link_hash_entry *h = NULL;
10487 const char *sym_name;
10489 if (r_symndx == STN_UNDEF)
10492 if (r_symndx >= locsymcount
10493 || (elf_bad_symtab (input_bfd)
10494 && flinfo->sections[r_symndx] == NULL))
10496 h = sym_hashes[r_symndx - extsymoff];
10498 /* Badly formatted input files can contain relocs that
10499 reference non-existant symbols. Check here so that
10500 we do not seg fault. */
10504 /* xgettext:c-format */
10505 (_("error: %B contains a reloc (%#Lx) for section %A "
10506 "that references a non-existent global symbol"),
10507 input_bfd, rel->r_info, o);
10508 bfd_set_error (bfd_error_bad_value);
10512 while (h->root.type == bfd_link_hash_indirect
10513 || h->root.type == bfd_link_hash_warning)
10514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10518 /* If a plugin symbol is referenced from a non-IR file,
10519 mark the symbol as undefined. Note that the
10520 linker may attach linker created dynamic sections
10521 to the plugin bfd. Symbols defined in linker
10522 created sections are not plugin symbols. */
10523 if ((h->root.non_ir_ref_regular
10524 || h->root.non_ir_ref_dynamic)
10525 && (h->root.type == bfd_link_hash_defined
10526 || h->root.type == bfd_link_hash_defweak)
10527 && (h->root.u.def.section->flags
10528 & SEC_LINKER_CREATED) == 0
10529 && h->root.u.def.section->owner != NULL
10530 && (h->root.u.def.section->owner->flags
10531 & BFD_PLUGIN) != 0)
10533 h->root.type = bfd_link_hash_undefined;
10534 h->root.u.undef.abfd = h->root.u.def.section->owner;
10538 if (h->root.type == bfd_link_hash_defined
10539 || h->root.type == bfd_link_hash_defweak)
10540 ps = &h->root.u.def.section;
10542 sym_name = h->root.root.string;
10546 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10548 s_type = ELF_ST_TYPE (sym->st_info);
10549 ps = &flinfo->sections[r_symndx];
10550 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10554 if ((s_type == STT_RELC || s_type == STT_SRELC)
10555 && !bfd_link_relocatable (flinfo->info))
10558 bfd_vma dot = (rel->r_offset
10559 + o->output_offset + o->output_section->vma);
10561 printf ("Encountered a complex symbol!");
10562 printf (" (input_bfd %s, section %s, reloc %ld\n",
10563 input_bfd->filename, o->name,
10564 (long) (rel - internal_relocs));
10565 printf (" symbol: idx %8.8lx, name %s\n",
10566 r_symndx, sym_name);
10567 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10568 (unsigned long) rel->r_info,
10569 (unsigned long) rel->r_offset);
10571 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10572 isymbuf, locsymcount, s_type == STT_SRELC))
10575 /* Symbol evaluated OK. Update to absolute value. */
10576 set_symbol_value (input_bfd, isymbuf, locsymcount,
10581 if (action_discarded != -1 && ps != NULL)
10583 /* Complain if the definition comes from a
10584 discarded section. */
10585 if ((sec = *ps) != NULL && discarded_section (sec))
10587 BFD_ASSERT (r_symndx != STN_UNDEF);
10588 if (action_discarded & COMPLAIN)
10589 (*flinfo->info->callbacks->einfo)
10590 /* xgettext:c-format */
10591 (_("%X`%s' referenced in section `%A' of %B: "
10592 "defined in discarded section `%A' of %B\n"),
10593 sym_name, o, input_bfd, sec, sec->owner);
10595 /* Try to do the best we can to support buggy old
10596 versions of gcc. Pretend that the symbol is
10597 really defined in the kept linkonce section.
10598 FIXME: This is quite broken. Modifying the
10599 symbol here means we will be changing all later
10600 uses of the symbol, not just in this section. */
10601 if (action_discarded & PRETEND)
10605 kept = _bfd_elf_check_kept_section (sec,
10617 /* Relocate the section by invoking a back end routine.
10619 The back end routine is responsible for adjusting the
10620 section contents as necessary, and (if using Rela relocs
10621 and generating a relocatable output file) adjusting the
10622 reloc addend as necessary.
10624 The back end routine does not have to worry about setting
10625 the reloc address or the reloc symbol index.
10627 The back end routine is given a pointer to the swapped in
10628 internal symbols, and can access the hash table entries
10629 for the external symbols via elf_sym_hashes (input_bfd).
10631 When generating relocatable output, the back end routine
10632 must handle STB_LOCAL/STT_SECTION symbols specially. The
10633 output symbol is going to be a section symbol
10634 corresponding to the output section, which will require
10635 the addend to be adjusted. */
10637 ret = (*relocate_section) (output_bfd, flinfo->info,
10638 input_bfd, o, contents,
10646 || bfd_link_relocatable (flinfo->info)
10647 || flinfo->info->emitrelocations)
10649 Elf_Internal_Rela *irela;
10650 Elf_Internal_Rela *irelaend, *irelamid;
10651 bfd_vma last_offset;
10652 struct elf_link_hash_entry **rel_hash;
10653 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10654 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10655 unsigned int next_erel;
10656 bfd_boolean rela_normal;
10657 struct bfd_elf_section_data *esdi, *esdo;
10659 esdi = elf_section_data (o);
10660 esdo = elf_section_data (o->output_section);
10661 rela_normal = FALSE;
10663 /* Adjust the reloc addresses and symbol indices. */
10665 irela = internal_relocs;
10666 irelaend = irela + o->reloc_count;
10667 rel_hash = esdo->rel.hashes + esdo->rel.count;
10668 /* We start processing the REL relocs, if any. When we reach
10669 IRELAMID in the loop, we switch to the RELA relocs. */
10671 if (esdi->rel.hdr != NULL)
10672 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10673 * bed->s->int_rels_per_ext_rel);
10674 rel_hash_list = rel_hash;
10675 rela_hash_list = NULL;
10676 last_offset = o->output_offset;
10677 if (!bfd_link_relocatable (flinfo->info))
10678 last_offset += o->output_section->vma;
10679 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10681 unsigned long r_symndx;
10683 Elf_Internal_Sym sym;
10685 if (next_erel == bed->s->int_rels_per_ext_rel)
10691 if (irela == irelamid)
10693 rel_hash = esdo->rela.hashes + esdo->rela.count;
10694 rela_hash_list = rel_hash;
10695 rela_normal = bed->rela_normal;
10698 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10701 if (irela->r_offset >= (bfd_vma) -2)
10703 /* This is a reloc for a deleted entry or somesuch.
10704 Turn it into an R_*_NONE reloc, at the same
10705 offset as the last reloc. elf_eh_frame.c and
10706 bfd_elf_discard_info rely on reloc offsets
10708 irela->r_offset = last_offset;
10710 irela->r_addend = 0;
10714 irela->r_offset += o->output_offset;
10716 /* Relocs in an executable have to be virtual addresses. */
10717 if (!bfd_link_relocatable (flinfo->info))
10718 irela->r_offset += o->output_section->vma;
10720 last_offset = irela->r_offset;
10722 r_symndx = irela->r_info >> r_sym_shift;
10723 if (r_symndx == STN_UNDEF)
10726 if (r_symndx >= locsymcount
10727 || (elf_bad_symtab (input_bfd)
10728 && flinfo->sections[r_symndx] == NULL))
10730 struct elf_link_hash_entry *rh;
10731 unsigned long indx;
10733 /* This is a reloc against a global symbol. We
10734 have not yet output all the local symbols, so
10735 we do not know the symbol index of any global
10736 symbol. We set the rel_hash entry for this
10737 reloc to point to the global hash table entry
10738 for this symbol. The symbol index is then
10739 set at the end of bfd_elf_final_link. */
10740 indx = r_symndx - extsymoff;
10741 rh = elf_sym_hashes (input_bfd)[indx];
10742 while (rh->root.type == bfd_link_hash_indirect
10743 || rh->root.type == bfd_link_hash_warning)
10744 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10746 /* Setting the index to -2 tells
10747 elf_link_output_extsym that this symbol is
10748 used by a reloc. */
10749 BFD_ASSERT (rh->indx < 0);
10756 /* This is a reloc against a local symbol. */
10759 sym = isymbuf[r_symndx];
10760 sec = flinfo->sections[r_symndx];
10761 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10763 /* I suppose the backend ought to fill in the
10764 section of any STT_SECTION symbol against a
10765 processor specific section. */
10766 r_symndx = STN_UNDEF;
10767 if (bfd_is_abs_section (sec))
10769 else if (sec == NULL || sec->owner == NULL)
10771 bfd_set_error (bfd_error_bad_value);
10776 asection *osec = sec->output_section;
10778 /* If we have discarded a section, the output
10779 section will be the absolute section. In
10780 case of discarded SEC_MERGE sections, use
10781 the kept section. relocate_section should
10782 have already handled discarded linkonce
10784 if (bfd_is_abs_section (osec)
10785 && sec->kept_section != NULL
10786 && sec->kept_section->output_section != NULL)
10788 osec = sec->kept_section->output_section;
10789 irela->r_addend -= osec->vma;
10792 if (!bfd_is_abs_section (osec))
10794 r_symndx = osec->target_index;
10795 if (r_symndx == STN_UNDEF)
10797 irela->r_addend += osec->vma;
10798 osec = _bfd_nearby_section (output_bfd, osec,
10800 irela->r_addend -= osec->vma;
10801 r_symndx = osec->target_index;
10806 /* Adjust the addend according to where the
10807 section winds up in the output section. */
10809 irela->r_addend += sec->output_offset;
10813 if (flinfo->indices[r_symndx] == -1)
10815 unsigned long shlink;
10820 if (flinfo->info->strip == strip_all)
10822 /* You can't do ld -r -s. */
10823 bfd_set_error (bfd_error_invalid_operation);
10827 /* This symbol was skipped earlier, but
10828 since it is needed by a reloc, we
10829 must output it now. */
10830 shlink = symtab_hdr->sh_link;
10831 name = (bfd_elf_string_from_elf_section
10832 (input_bfd, shlink, sym.st_name));
10836 osec = sec->output_section;
10838 _bfd_elf_section_from_bfd_section (output_bfd,
10840 if (sym.st_shndx == SHN_BAD)
10843 sym.st_value += sec->output_offset;
10844 if (!bfd_link_relocatable (flinfo->info))
10846 sym.st_value += osec->vma;
10847 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10849 /* STT_TLS symbols are relative to PT_TLS
10851 BFD_ASSERT (elf_hash_table (flinfo->info)
10852 ->tls_sec != NULL);
10853 sym.st_value -= (elf_hash_table (flinfo->info)
10858 indx = bfd_get_symcount (output_bfd);
10859 ret = elf_link_output_symstrtab (flinfo, name,
10865 flinfo->indices[r_symndx] = indx;
10870 r_symndx = flinfo->indices[r_symndx];
10873 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10874 | (irela->r_info & r_type_mask));
10877 /* Swap out the relocs. */
10878 input_rel_hdr = esdi->rel.hdr;
10879 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10881 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10886 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10887 * bed->s->int_rels_per_ext_rel);
10888 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10891 input_rela_hdr = esdi->rela.hdr;
10892 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10894 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10903 /* Write out the modified section contents. */
10904 if (bed->elf_backend_write_section
10905 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10908 /* Section written out. */
10910 else switch (o->sec_info_type)
10912 case SEC_INFO_TYPE_STABS:
10913 if (! (_bfd_write_section_stabs
10915 &elf_hash_table (flinfo->info)->stab_info,
10916 o, &elf_section_data (o)->sec_info, contents)))
10919 case SEC_INFO_TYPE_MERGE:
10920 if (! _bfd_write_merged_section (output_bfd, o,
10921 elf_section_data (o)->sec_info))
10924 case SEC_INFO_TYPE_EH_FRAME:
10926 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10931 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10933 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10941 if (! (o->flags & SEC_EXCLUDE))
10943 file_ptr offset = (file_ptr) o->output_offset;
10944 bfd_size_type todo = o->size;
10946 offset *= bfd_octets_per_byte (output_bfd);
10948 if ((o->flags & SEC_ELF_REVERSE_COPY))
10950 /* Reverse-copy input section to output. */
10953 todo -= address_size;
10954 if (! bfd_set_section_contents (output_bfd,
10962 offset += address_size;
10966 else if (! bfd_set_section_contents (output_bfd,
10980 /* Generate a reloc when linking an ELF file. This is a reloc
10981 requested by the linker, and does not come from any input file. This
10982 is used to build constructor and destructor tables when linking
10986 elf_reloc_link_order (bfd *output_bfd,
10987 struct bfd_link_info *info,
10988 asection *output_section,
10989 struct bfd_link_order *link_order)
10991 reloc_howto_type *howto;
10995 struct bfd_elf_section_reloc_data *reldata;
10996 struct elf_link_hash_entry **rel_hash_ptr;
10997 Elf_Internal_Shdr *rel_hdr;
10998 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10999 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11002 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11004 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11007 bfd_set_error (bfd_error_bad_value);
11011 addend = link_order->u.reloc.p->addend;
11014 reldata = &esdo->rel;
11015 else if (esdo->rela.hdr)
11016 reldata = &esdo->rela;
11023 /* Figure out the symbol index. */
11024 rel_hash_ptr = reldata->hashes + reldata->count;
11025 if (link_order->type == bfd_section_reloc_link_order)
11027 indx = link_order->u.reloc.p->u.section->target_index;
11028 BFD_ASSERT (indx != 0);
11029 *rel_hash_ptr = NULL;
11033 struct elf_link_hash_entry *h;
11035 /* Treat a reloc against a defined symbol as though it were
11036 actually against the section. */
11037 h = ((struct elf_link_hash_entry *)
11038 bfd_wrapped_link_hash_lookup (output_bfd, info,
11039 link_order->u.reloc.p->u.name,
11040 FALSE, FALSE, TRUE));
11042 && (h->root.type == bfd_link_hash_defined
11043 || h->root.type == bfd_link_hash_defweak))
11047 section = h->root.u.def.section;
11048 indx = section->output_section->target_index;
11049 *rel_hash_ptr = NULL;
11050 /* It seems that we ought to add the symbol value to the
11051 addend here, but in practice it has already been added
11052 because it was passed to constructor_callback. */
11053 addend += section->output_section->vma + section->output_offset;
11055 else if (h != NULL)
11057 /* Setting the index to -2 tells elf_link_output_extsym that
11058 this symbol is used by a reloc. */
11065 (*info->callbacks->unattached_reloc)
11066 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11071 /* If this is an inplace reloc, we must write the addend into the
11073 if (howto->partial_inplace && addend != 0)
11075 bfd_size_type size;
11076 bfd_reloc_status_type rstat;
11079 const char *sym_name;
11081 size = (bfd_size_type) bfd_get_reloc_size (howto);
11082 buf = (bfd_byte *) bfd_zmalloc (size);
11083 if (buf == NULL && size != 0)
11085 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11092 case bfd_reloc_outofrange:
11095 case bfd_reloc_overflow:
11096 if (link_order->type == bfd_section_reloc_link_order)
11097 sym_name = bfd_section_name (output_bfd,
11098 link_order->u.reloc.p->u.section);
11100 sym_name = link_order->u.reloc.p->u.name;
11101 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11102 howto->name, addend, NULL, NULL,
11107 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11109 * bfd_octets_per_byte (output_bfd),
11116 /* The address of a reloc is relative to the section in a
11117 relocatable file, and is a virtual address in an executable
11119 offset = link_order->offset;
11120 if (! bfd_link_relocatable (info))
11121 offset += output_section->vma;
11123 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11125 irel[i].r_offset = offset;
11126 irel[i].r_info = 0;
11127 irel[i].r_addend = 0;
11129 if (bed->s->arch_size == 32)
11130 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11132 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11134 rel_hdr = reldata->hdr;
11135 erel = rel_hdr->contents;
11136 if (rel_hdr->sh_type == SHT_REL)
11138 erel += reldata->count * bed->s->sizeof_rel;
11139 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11143 irel[0].r_addend = addend;
11144 erel += reldata->count * bed->s->sizeof_rela;
11145 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11154 /* Get the output vma of the section pointed to by the sh_link field. */
11157 elf_get_linked_section_vma (struct bfd_link_order *p)
11159 Elf_Internal_Shdr **elf_shdrp;
11163 s = p->u.indirect.section;
11164 elf_shdrp = elf_elfsections (s->owner);
11165 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11166 elfsec = elf_shdrp[elfsec]->sh_link;
11168 The Intel C compiler generates SHT_IA_64_UNWIND with
11169 SHF_LINK_ORDER. But it doesn't set the sh_link or
11170 sh_info fields. Hence we could get the situation
11171 where elfsec is 0. */
11174 const struct elf_backend_data *bed
11175 = get_elf_backend_data (s->owner);
11176 if (bed->link_order_error_handler)
11177 bed->link_order_error_handler
11178 /* xgettext:c-format */
11179 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11184 s = elf_shdrp[elfsec]->bfd_section;
11185 return s->output_section->vma + s->output_offset;
11190 /* Compare two sections based on the locations of the sections they are
11191 linked to. Used by elf_fixup_link_order. */
11194 compare_link_order (const void * a, const void * b)
11199 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11200 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11203 return apos > bpos;
11207 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11208 order as their linked sections. Returns false if this could not be done
11209 because an output section includes both ordered and unordered
11210 sections. Ideally we'd do this in the linker proper. */
11213 elf_fixup_link_order (bfd *abfd, asection *o)
11215 int seen_linkorder;
11218 struct bfd_link_order *p;
11220 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11222 struct bfd_link_order **sections;
11223 asection *s, *other_sec, *linkorder_sec;
11227 linkorder_sec = NULL;
11229 seen_linkorder = 0;
11230 for (p = o->map_head.link_order; p != NULL; p = p->next)
11232 if (p->type == bfd_indirect_link_order)
11234 s = p->u.indirect.section;
11236 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11237 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11238 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11239 && elfsec < elf_numsections (sub)
11240 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11241 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11255 if (seen_other && seen_linkorder)
11257 if (other_sec && linkorder_sec)
11259 /* xgettext:c-format */
11260 (_("%A has both ordered [`%A' in %B] "
11261 "and unordered [`%A' in %B] sections"),
11262 o, linkorder_sec, linkorder_sec->owner,
11263 other_sec, other_sec->owner);
11266 (_("%A has both ordered and unordered sections"), o);
11267 bfd_set_error (bfd_error_bad_value);
11272 if (!seen_linkorder)
11275 sections = (struct bfd_link_order **)
11276 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11277 if (sections == NULL)
11279 seen_linkorder = 0;
11281 for (p = o->map_head.link_order; p != NULL; p = p->next)
11283 sections[seen_linkorder++] = p;
11285 /* Sort the input sections in the order of their linked section. */
11286 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11287 compare_link_order);
11289 /* Change the offsets of the sections. */
11291 for (n = 0; n < seen_linkorder; n++)
11293 s = sections[n]->u.indirect.section;
11294 offset &= ~(bfd_vma) 0 << s->alignment_power;
11295 s->output_offset = offset / bfd_octets_per_byte (abfd);
11296 sections[n]->offset = offset;
11297 offset += sections[n]->size;
11304 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11305 Returns TRUE upon success, FALSE otherwise. */
11308 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11310 bfd_boolean ret = FALSE;
11312 const struct elf_backend_data *bed;
11314 enum bfd_architecture arch;
11316 asymbol **sympp = NULL;
11320 elf_symbol_type *osymbuf;
11322 implib_bfd = info->out_implib_bfd;
11323 bed = get_elf_backend_data (abfd);
11325 if (!bfd_set_format (implib_bfd, bfd_object))
11328 /* Use flag from executable but make it a relocatable object. */
11329 flags = bfd_get_file_flags (abfd);
11330 flags &= ~HAS_RELOC;
11331 if (!bfd_set_start_address (implib_bfd, 0)
11332 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11335 /* Copy architecture of output file to import library file. */
11336 arch = bfd_get_arch (abfd);
11337 mach = bfd_get_mach (abfd);
11338 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11339 && (abfd->target_defaulted
11340 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11343 /* Get symbol table size. */
11344 symsize = bfd_get_symtab_upper_bound (abfd);
11348 /* Read in the symbol table. */
11349 sympp = (asymbol **) xmalloc (symsize);
11350 symcount = bfd_canonicalize_symtab (abfd, sympp);
11354 /* Allow the BFD backend to copy any private header data it
11355 understands from the output BFD to the import library BFD. */
11356 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11359 /* Filter symbols to appear in the import library. */
11360 if (bed->elf_backend_filter_implib_symbols)
11361 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11364 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11367 bfd_set_error (bfd_error_no_symbols);
11368 _bfd_error_handler (_("%B: no symbol found for import library"),
11374 /* Make symbols absolute. */
11375 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11376 sizeof (*osymbuf));
11377 for (src_count = 0; src_count < symcount; src_count++)
11379 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11380 sizeof (*osymbuf));
11381 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11382 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11383 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11384 osymbuf[src_count].internal_elf_sym.st_value =
11385 osymbuf[src_count].symbol.value;
11386 sympp[src_count] = &osymbuf[src_count].symbol;
11389 bfd_set_symtab (implib_bfd, sympp, symcount);
11391 /* Allow the BFD backend to copy any private data it understands
11392 from the output BFD to the import library BFD. This is done last
11393 to permit the routine to look at the filtered symbol table. */
11394 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11397 if (!bfd_close (implib_bfd))
11408 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11412 if (flinfo->symstrtab != NULL)
11413 _bfd_elf_strtab_free (flinfo->symstrtab);
11414 if (flinfo->contents != NULL)
11415 free (flinfo->contents);
11416 if (flinfo->external_relocs != NULL)
11417 free (flinfo->external_relocs);
11418 if (flinfo->internal_relocs != NULL)
11419 free (flinfo->internal_relocs);
11420 if (flinfo->external_syms != NULL)
11421 free (flinfo->external_syms);
11422 if (flinfo->locsym_shndx != NULL)
11423 free (flinfo->locsym_shndx);
11424 if (flinfo->internal_syms != NULL)
11425 free (flinfo->internal_syms);
11426 if (flinfo->indices != NULL)
11427 free (flinfo->indices);
11428 if (flinfo->sections != NULL)
11429 free (flinfo->sections);
11430 if (flinfo->symshndxbuf != NULL)
11431 free (flinfo->symshndxbuf);
11432 for (o = obfd->sections; o != NULL; o = o->next)
11434 struct bfd_elf_section_data *esdo = elf_section_data (o);
11435 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11436 free (esdo->rel.hashes);
11437 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11438 free (esdo->rela.hashes);
11442 /* Do the final step of an ELF link. */
11445 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11447 bfd_boolean dynamic;
11448 bfd_boolean emit_relocs;
11450 struct elf_final_link_info flinfo;
11452 struct bfd_link_order *p;
11454 bfd_size_type max_contents_size;
11455 bfd_size_type max_external_reloc_size;
11456 bfd_size_type max_internal_reloc_count;
11457 bfd_size_type max_sym_count;
11458 bfd_size_type max_sym_shndx_count;
11459 Elf_Internal_Sym elfsym;
11461 Elf_Internal_Shdr *symtab_hdr;
11462 Elf_Internal_Shdr *symtab_shndx_hdr;
11463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11464 struct elf_outext_info eoinfo;
11465 bfd_boolean merged;
11466 size_t relativecount = 0;
11467 asection *reldyn = 0;
11469 asection *attr_section = NULL;
11470 bfd_vma attr_size = 0;
11471 const char *std_attrs_section;
11472 struct elf_link_hash_table *htab = elf_hash_table (info);
11474 if (!is_elf_hash_table (htab))
11477 if (bfd_link_pic (info))
11478 abfd->flags |= DYNAMIC;
11480 dynamic = htab->dynamic_sections_created;
11481 dynobj = htab->dynobj;
11483 emit_relocs = (bfd_link_relocatable (info)
11484 || info->emitrelocations);
11486 flinfo.info = info;
11487 flinfo.output_bfd = abfd;
11488 flinfo.symstrtab = _bfd_elf_strtab_init ();
11489 if (flinfo.symstrtab == NULL)
11494 flinfo.hash_sec = NULL;
11495 flinfo.symver_sec = NULL;
11499 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11500 /* Note that dynsym_sec can be NULL (on VMS). */
11501 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11502 /* Note that it is OK if symver_sec is NULL. */
11505 flinfo.contents = NULL;
11506 flinfo.external_relocs = NULL;
11507 flinfo.internal_relocs = NULL;
11508 flinfo.external_syms = NULL;
11509 flinfo.locsym_shndx = NULL;
11510 flinfo.internal_syms = NULL;
11511 flinfo.indices = NULL;
11512 flinfo.sections = NULL;
11513 flinfo.symshndxbuf = NULL;
11514 flinfo.filesym_count = 0;
11516 /* The object attributes have been merged. Remove the input
11517 sections from the link, and set the contents of the output
11519 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11520 for (o = abfd->sections; o != NULL; o = o->next)
11522 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11523 || strcmp (o->name, ".gnu.attributes") == 0)
11525 for (p = o->map_head.link_order; p != NULL; p = p->next)
11527 asection *input_section;
11529 if (p->type != bfd_indirect_link_order)
11531 input_section = p->u.indirect.section;
11532 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11533 elf_link_input_bfd ignores this section. */
11534 input_section->flags &= ~SEC_HAS_CONTENTS;
11537 attr_size = bfd_elf_obj_attr_size (abfd);
11540 bfd_set_section_size (abfd, o, attr_size);
11542 /* Skip this section later on. */
11543 o->map_head.link_order = NULL;
11546 o->flags |= SEC_EXCLUDE;
11550 /* Count up the number of relocations we will output for each output
11551 section, so that we know the sizes of the reloc sections. We
11552 also figure out some maximum sizes. */
11553 max_contents_size = 0;
11554 max_external_reloc_size = 0;
11555 max_internal_reloc_count = 0;
11557 max_sym_shndx_count = 0;
11559 for (o = abfd->sections; o != NULL; o = o->next)
11561 struct bfd_elf_section_data *esdo = elf_section_data (o);
11562 o->reloc_count = 0;
11564 for (p = o->map_head.link_order; p != NULL; p = p->next)
11566 unsigned int reloc_count = 0;
11567 unsigned int additional_reloc_count = 0;
11568 struct bfd_elf_section_data *esdi = NULL;
11570 if (p->type == bfd_section_reloc_link_order
11571 || p->type == bfd_symbol_reloc_link_order)
11573 else if (p->type == bfd_indirect_link_order)
11577 sec = p->u.indirect.section;
11579 /* Mark all sections which are to be included in the
11580 link. This will normally be every section. We need
11581 to do this so that we can identify any sections which
11582 the linker has decided to not include. */
11583 sec->linker_mark = TRUE;
11585 if (sec->flags & SEC_MERGE)
11588 if (sec->rawsize > max_contents_size)
11589 max_contents_size = sec->rawsize;
11590 if (sec->size > max_contents_size)
11591 max_contents_size = sec->size;
11593 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11594 && (sec->owner->flags & DYNAMIC) == 0)
11598 /* We are interested in just local symbols, not all
11600 if (elf_bad_symtab (sec->owner))
11601 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11602 / bed->s->sizeof_sym);
11604 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11606 if (sym_count > max_sym_count)
11607 max_sym_count = sym_count;
11609 if (sym_count > max_sym_shndx_count
11610 && elf_symtab_shndx_list (sec->owner) != NULL)
11611 max_sym_shndx_count = sym_count;
11613 if (esdo->this_hdr.sh_type == SHT_REL
11614 || esdo->this_hdr.sh_type == SHT_RELA)
11615 /* Some backends use reloc_count in relocation sections
11616 to count particular types of relocs. Of course,
11617 reloc sections themselves can't have relocations. */
11619 else if (emit_relocs)
11621 reloc_count = sec->reloc_count;
11622 if (bed->elf_backend_count_additional_relocs)
11625 c = (*bed->elf_backend_count_additional_relocs) (sec);
11626 additional_reloc_count += c;
11629 else if (bed->elf_backend_count_relocs)
11630 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11632 esdi = elf_section_data (sec);
11634 if ((sec->flags & SEC_RELOC) != 0)
11636 size_t ext_size = 0;
11638 if (esdi->rel.hdr != NULL)
11639 ext_size = esdi->rel.hdr->sh_size;
11640 if (esdi->rela.hdr != NULL)
11641 ext_size += esdi->rela.hdr->sh_size;
11643 if (ext_size > max_external_reloc_size)
11644 max_external_reloc_size = ext_size;
11645 if (sec->reloc_count > max_internal_reloc_count)
11646 max_internal_reloc_count = sec->reloc_count;
11651 if (reloc_count == 0)
11654 reloc_count += additional_reloc_count;
11655 o->reloc_count += reloc_count;
11657 if (p->type == bfd_indirect_link_order && emit_relocs)
11661 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11662 esdo->rel.count += additional_reloc_count;
11664 if (esdi->rela.hdr)
11666 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11667 esdo->rela.count += additional_reloc_count;
11673 esdo->rela.count += reloc_count;
11675 esdo->rel.count += reloc_count;
11679 if (o->reloc_count > 0)
11680 o->flags |= SEC_RELOC;
11683 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11684 set it (this is probably a bug) and if it is set
11685 assign_section_numbers will create a reloc section. */
11686 o->flags &=~ SEC_RELOC;
11689 /* If the SEC_ALLOC flag is not set, force the section VMA to
11690 zero. This is done in elf_fake_sections as well, but forcing
11691 the VMA to 0 here will ensure that relocs against these
11692 sections are handled correctly. */
11693 if ((o->flags & SEC_ALLOC) == 0
11694 && ! o->user_set_vma)
11698 if (! bfd_link_relocatable (info) && merged)
11699 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11701 /* Figure out the file positions for everything but the symbol table
11702 and the relocs. We set symcount to force assign_section_numbers
11703 to create a symbol table. */
11704 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11705 BFD_ASSERT (! abfd->output_has_begun);
11706 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11709 /* Set sizes, and assign file positions for reloc sections. */
11710 for (o = abfd->sections; o != NULL; o = o->next)
11712 struct bfd_elf_section_data *esdo = elf_section_data (o);
11713 if ((o->flags & SEC_RELOC) != 0)
11716 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11720 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11724 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11725 to count upwards while actually outputting the relocations. */
11726 esdo->rel.count = 0;
11727 esdo->rela.count = 0;
11729 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11731 /* Cache the section contents so that they can be compressed
11732 later. Use bfd_malloc since it will be freed by
11733 bfd_compress_section_contents. */
11734 unsigned char *contents = esdo->this_hdr.contents;
11735 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11738 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11739 if (contents == NULL)
11741 esdo->this_hdr.contents = contents;
11745 /* We have now assigned file positions for all the sections except
11746 .symtab, .strtab, and non-loaded reloc sections. We start the
11747 .symtab section at the current file position, and write directly
11748 to it. We build the .strtab section in memory. */
11749 bfd_get_symcount (abfd) = 0;
11750 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11751 /* sh_name is set in prep_headers. */
11752 symtab_hdr->sh_type = SHT_SYMTAB;
11753 /* sh_flags, sh_addr and sh_size all start off zero. */
11754 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11755 /* sh_link is set in assign_section_numbers. */
11756 /* sh_info is set below. */
11757 /* sh_offset is set just below. */
11758 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11760 if (max_sym_count < 20)
11761 max_sym_count = 20;
11762 htab->strtabsize = max_sym_count;
11763 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11764 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11765 if (htab->strtab == NULL)
11767 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11769 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11770 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11772 if (info->strip != strip_all || emit_relocs)
11774 file_ptr off = elf_next_file_pos (abfd);
11776 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11778 /* Note that at this point elf_next_file_pos (abfd) is
11779 incorrect. We do not yet know the size of the .symtab section.
11780 We correct next_file_pos below, after we do know the size. */
11782 /* Start writing out the symbol table. The first symbol is always a
11784 elfsym.st_value = 0;
11785 elfsym.st_size = 0;
11786 elfsym.st_info = 0;
11787 elfsym.st_other = 0;
11788 elfsym.st_shndx = SHN_UNDEF;
11789 elfsym.st_target_internal = 0;
11790 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11791 bfd_und_section_ptr, NULL) != 1)
11794 /* Output a symbol for each section. We output these even if we are
11795 discarding local symbols, since they are used for relocs. These
11796 symbols have no names. We store the index of each one in the
11797 index field of the section, so that we can find it again when
11798 outputting relocs. */
11800 elfsym.st_size = 0;
11801 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11802 elfsym.st_other = 0;
11803 elfsym.st_value = 0;
11804 elfsym.st_target_internal = 0;
11805 for (i = 1; i < elf_numsections (abfd); i++)
11807 o = bfd_section_from_elf_index (abfd, i);
11810 o->target_index = bfd_get_symcount (abfd);
11811 elfsym.st_shndx = i;
11812 if (!bfd_link_relocatable (info))
11813 elfsym.st_value = o->vma;
11814 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11821 /* Allocate some memory to hold information read in from the input
11823 if (max_contents_size != 0)
11825 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11826 if (flinfo.contents == NULL)
11830 if (max_external_reloc_size != 0)
11832 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11833 if (flinfo.external_relocs == NULL)
11837 if (max_internal_reloc_count != 0)
11839 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11840 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11841 if (flinfo.internal_relocs == NULL)
11845 if (max_sym_count != 0)
11847 amt = max_sym_count * bed->s->sizeof_sym;
11848 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11849 if (flinfo.external_syms == NULL)
11852 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11853 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11854 if (flinfo.internal_syms == NULL)
11857 amt = max_sym_count * sizeof (long);
11858 flinfo.indices = (long int *) bfd_malloc (amt);
11859 if (flinfo.indices == NULL)
11862 amt = max_sym_count * sizeof (asection *);
11863 flinfo.sections = (asection **) bfd_malloc (amt);
11864 if (flinfo.sections == NULL)
11868 if (max_sym_shndx_count != 0)
11870 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11871 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11872 if (flinfo.locsym_shndx == NULL)
11878 bfd_vma base, end = 0;
11881 for (sec = htab->tls_sec;
11882 sec && (sec->flags & SEC_THREAD_LOCAL);
11885 bfd_size_type size = sec->size;
11888 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11890 struct bfd_link_order *ord = sec->map_tail.link_order;
11893 size = ord->offset + ord->size;
11895 end = sec->vma + size;
11897 base = htab->tls_sec->vma;
11898 /* Only align end of TLS section if static TLS doesn't have special
11899 alignment requirements. */
11900 if (bed->static_tls_alignment == 1)
11901 end = align_power (end, htab->tls_sec->alignment_power);
11902 htab->tls_size = end - base;
11905 /* Reorder SHF_LINK_ORDER sections. */
11906 for (o = abfd->sections; o != NULL; o = o->next)
11908 if (!elf_fixup_link_order (abfd, o))
11912 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11915 /* Since ELF permits relocations to be against local symbols, we
11916 must have the local symbols available when we do the relocations.
11917 Since we would rather only read the local symbols once, and we
11918 would rather not keep them in memory, we handle all the
11919 relocations for a single input file at the same time.
11921 Unfortunately, there is no way to know the total number of local
11922 symbols until we have seen all of them, and the local symbol
11923 indices precede the global symbol indices. This means that when
11924 we are generating relocatable output, and we see a reloc against
11925 a global symbol, we can not know the symbol index until we have
11926 finished examining all the local symbols to see which ones we are
11927 going to output. To deal with this, we keep the relocations in
11928 memory, and don't output them until the end of the link. This is
11929 an unfortunate waste of memory, but I don't see a good way around
11930 it. Fortunately, it only happens when performing a relocatable
11931 link, which is not the common case. FIXME: If keep_memory is set
11932 we could write the relocs out and then read them again; I don't
11933 know how bad the memory loss will be. */
11935 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11936 sub->output_has_begun = FALSE;
11937 for (o = abfd->sections; o != NULL; o = o->next)
11939 for (p = o->map_head.link_order; p != NULL; p = p->next)
11941 if (p->type == bfd_indirect_link_order
11942 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11943 == bfd_target_elf_flavour)
11944 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11946 if (! sub->output_has_begun)
11948 if (! elf_link_input_bfd (&flinfo, sub))
11950 sub->output_has_begun = TRUE;
11953 else if (p->type == bfd_section_reloc_link_order
11954 || p->type == bfd_symbol_reloc_link_order)
11956 if (! elf_reloc_link_order (abfd, info, o, p))
11961 if (! _bfd_default_link_order (abfd, info, o, p))
11963 if (p->type == bfd_indirect_link_order
11964 && (bfd_get_flavour (sub)
11965 == bfd_target_elf_flavour)
11966 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11967 != bed->s->elfclass))
11969 const char *iclass, *oclass;
11971 switch (bed->s->elfclass)
11973 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11974 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11975 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11979 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11981 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11982 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11983 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11987 bfd_set_error (bfd_error_wrong_format);
11989 /* xgettext:c-format */
11990 (_("%B: file class %s incompatible with %s"),
11991 sub, iclass, oclass);
12000 /* Free symbol buffer if needed. */
12001 if (!info->reduce_memory_overheads)
12003 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12004 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12005 && elf_tdata (sub)->symbuf)
12007 free (elf_tdata (sub)->symbuf);
12008 elf_tdata (sub)->symbuf = NULL;
12012 /* Output any global symbols that got converted to local in a
12013 version script or due to symbol visibility. We do this in a
12014 separate step since ELF requires all local symbols to appear
12015 prior to any global symbols. FIXME: We should only do this if
12016 some global symbols were, in fact, converted to become local.
12017 FIXME: Will this work correctly with the Irix 5 linker? */
12018 eoinfo.failed = FALSE;
12019 eoinfo.flinfo = &flinfo;
12020 eoinfo.localsyms = TRUE;
12021 eoinfo.file_sym_done = FALSE;
12022 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12026 /* If backend needs to output some local symbols not present in the hash
12027 table, do it now. */
12028 if (bed->elf_backend_output_arch_local_syms
12029 && (info->strip != strip_all || emit_relocs))
12031 typedef int (*out_sym_func)
12032 (void *, const char *, Elf_Internal_Sym *, asection *,
12033 struct elf_link_hash_entry *);
12035 if (! ((*bed->elf_backend_output_arch_local_syms)
12036 (abfd, info, &flinfo,
12037 (out_sym_func) elf_link_output_symstrtab)))
12041 /* That wrote out all the local symbols. Finish up the symbol table
12042 with the global symbols. Even if we want to strip everything we
12043 can, we still need to deal with those global symbols that got
12044 converted to local in a version script. */
12046 /* The sh_info field records the index of the first non local symbol. */
12047 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12050 && htab->dynsym != NULL
12051 && htab->dynsym->output_section != bfd_abs_section_ptr)
12053 Elf_Internal_Sym sym;
12054 bfd_byte *dynsym = htab->dynsym->contents;
12056 o = htab->dynsym->output_section;
12057 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12059 /* Write out the section symbols for the output sections. */
12060 if (bfd_link_pic (info)
12061 || htab->is_relocatable_executable)
12067 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12069 sym.st_target_internal = 0;
12071 for (s = abfd->sections; s != NULL; s = s->next)
12077 dynindx = elf_section_data (s)->dynindx;
12080 indx = elf_section_data (s)->this_idx;
12081 BFD_ASSERT (indx > 0);
12082 sym.st_shndx = indx;
12083 if (! check_dynsym (abfd, &sym))
12085 sym.st_value = s->vma;
12086 dest = dynsym + dynindx * bed->s->sizeof_sym;
12087 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12091 /* Write out the local dynsyms. */
12092 if (htab->dynlocal)
12094 struct elf_link_local_dynamic_entry *e;
12095 for (e = htab->dynlocal; e ; e = e->next)
12100 /* Copy the internal symbol and turn off visibility.
12101 Note that we saved a word of storage and overwrote
12102 the original st_name with the dynstr_index. */
12104 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12106 s = bfd_section_from_elf_index (e->input_bfd,
12111 elf_section_data (s->output_section)->this_idx;
12112 if (! check_dynsym (abfd, &sym))
12114 sym.st_value = (s->output_section->vma
12116 + e->isym.st_value);
12119 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12120 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12125 /* We get the global symbols from the hash table. */
12126 eoinfo.failed = FALSE;
12127 eoinfo.localsyms = FALSE;
12128 eoinfo.flinfo = &flinfo;
12129 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12133 /* If backend needs to output some symbols not present in the hash
12134 table, do it now. */
12135 if (bed->elf_backend_output_arch_syms
12136 && (info->strip != strip_all || emit_relocs))
12138 typedef int (*out_sym_func)
12139 (void *, const char *, Elf_Internal_Sym *, asection *,
12140 struct elf_link_hash_entry *);
12142 if (! ((*bed->elf_backend_output_arch_syms)
12143 (abfd, info, &flinfo,
12144 (out_sym_func) elf_link_output_symstrtab)))
12148 /* Finalize the .strtab section. */
12149 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12151 /* Swap out the .strtab section. */
12152 if (!elf_link_swap_symbols_out (&flinfo))
12155 /* Now we know the size of the symtab section. */
12156 if (bfd_get_symcount (abfd) > 0)
12158 /* Finish up and write out the symbol string table (.strtab)
12160 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12161 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12163 if (elf_symtab_shndx_list (abfd))
12165 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12167 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12169 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12170 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12171 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12172 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12173 symtab_shndx_hdr->sh_size = amt;
12175 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12178 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12179 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12184 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12185 /* sh_name was set in prep_headers. */
12186 symstrtab_hdr->sh_type = SHT_STRTAB;
12187 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12188 symstrtab_hdr->sh_addr = 0;
12189 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12190 symstrtab_hdr->sh_entsize = 0;
12191 symstrtab_hdr->sh_link = 0;
12192 symstrtab_hdr->sh_info = 0;
12193 /* sh_offset is set just below. */
12194 symstrtab_hdr->sh_addralign = 1;
12196 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12198 elf_next_file_pos (abfd) = off;
12200 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12201 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12205 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12207 _bfd_error_handler (_("%B: failed to generate import library"),
12208 info->out_implib_bfd);
12212 /* Adjust the relocs to have the correct symbol indices. */
12213 for (o = abfd->sections; o != NULL; o = o->next)
12215 struct bfd_elf_section_data *esdo = elf_section_data (o);
12218 if ((o->flags & SEC_RELOC) == 0)
12221 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12222 if (esdo->rel.hdr != NULL
12223 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12225 if (esdo->rela.hdr != NULL
12226 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12229 /* Set the reloc_count field to 0 to prevent write_relocs from
12230 trying to swap the relocs out itself. */
12231 o->reloc_count = 0;
12234 if (dynamic && info->combreloc && dynobj != NULL)
12235 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12237 /* If we are linking against a dynamic object, or generating a
12238 shared library, finish up the dynamic linking information. */
12241 bfd_byte *dyncon, *dynconend;
12243 /* Fix up .dynamic entries. */
12244 o = bfd_get_linker_section (dynobj, ".dynamic");
12245 BFD_ASSERT (o != NULL);
12247 dyncon = o->contents;
12248 dynconend = o->contents + o->size;
12249 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12251 Elf_Internal_Dyn dyn;
12254 bfd_size_type sh_size;
12257 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12264 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12266 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12268 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12269 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12272 dyn.d_un.d_val = relativecount;
12279 name = info->init_function;
12282 name = info->fini_function;
12285 struct elf_link_hash_entry *h;
12287 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12289 && (h->root.type == bfd_link_hash_defined
12290 || h->root.type == bfd_link_hash_defweak))
12292 dyn.d_un.d_ptr = h->root.u.def.value;
12293 o = h->root.u.def.section;
12294 if (o->output_section != NULL)
12295 dyn.d_un.d_ptr += (o->output_section->vma
12296 + o->output_offset);
12299 /* The symbol is imported from another shared
12300 library and does not apply to this one. */
12301 dyn.d_un.d_ptr = 0;
12308 case DT_PREINIT_ARRAYSZ:
12309 name = ".preinit_array";
12311 case DT_INIT_ARRAYSZ:
12312 name = ".init_array";
12314 case DT_FINI_ARRAYSZ:
12315 name = ".fini_array";
12317 o = bfd_get_section_by_name (abfd, name);
12321 (_("could not find section %s"), name);
12326 (_("warning: %s section has zero size"), name);
12327 dyn.d_un.d_val = o->size;
12330 case DT_PREINIT_ARRAY:
12331 name = ".preinit_array";
12333 case DT_INIT_ARRAY:
12334 name = ".init_array";
12336 case DT_FINI_ARRAY:
12337 name = ".fini_array";
12339 o = bfd_get_section_by_name (abfd, name);
12346 name = ".gnu.hash";
12355 name = ".gnu.version_d";
12358 name = ".gnu.version_r";
12361 name = ".gnu.version";
12363 o = bfd_get_linker_section (dynobj, name);
12365 if (o == NULL || bfd_is_abs_section (o->output_section))
12368 (_("could not find section %s"), name);
12371 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12374 (_("warning: section '%s' is being made into a note"), name);
12375 bfd_set_error (bfd_error_nonrepresentable_section);
12378 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12385 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12391 for (i = 1; i < elf_numsections (abfd); i++)
12393 Elf_Internal_Shdr *hdr;
12395 hdr = elf_elfsections (abfd)[i];
12396 if (hdr->sh_type == type
12397 && (hdr->sh_flags & SHF_ALLOC) != 0)
12399 sh_size += hdr->sh_size;
12401 || sh_addr > hdr->sh_addr)
12402 sh_addr = hdr->sh_addr;
12406 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12408 /* Don't count procedure linkage table relocs in the
12409 overall reloc count. */
12410 sh_size -= htab->srelplt->size;
12412 /* If the size is zero, make the address zero too.
12413 This is to avoid a glibc bug. If the backend
12414 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12415 zero, then we'll put DT_RELA at the end of
12416 DT_JMPREL. glibc will interpret the end of
12417 DT_RELA matching the end of DT_JMPREL as the
12418 case where DT_RELA includes DT_JMPREL, and for
12419 LD_BIND_NOW will decide that processing DT_RELA
12420 will process the PLT relocs too. Net result:
12421 No PLT relocs applied. */
12424 /* If .rela.plt is the first .rela section, exclude
12425 it from DT_RELA. */
12426 else if (sh_addr == (htab->srelplt->output_section->vma
12427 + htab->srelplt->output_offset))
12428 sh_addr += htab->srelplt->size;
12431 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12432 dyn.d_un.d_val = sh_size;
12434 dyn.d_un.d_ptr = sh_addr;
12437 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12441 /* If we have created any dynamic sections, then output them. */
12442 if (dynobj != NULL)
12444 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12447 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12448 if (((info->warn_shared_textrel && bfd_link_pic (info))
12449 || info->error_textrel)
12450 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12452 bfd_byte *dyncon, *dynconend;
12454 dyncon = o->contents;
12455 dynconend = o->contents + o->size;
12456 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12458 Elf_Internal_Dyn dyn;
12460 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12462 if (dyn.d_tag == DT_TEXTREL)
12464 if (info->error_textrel)
12465 info->callbacks->einfo
12466 (_("%P%X: read-only segment has dynamic relocations.\n"));
12468 info->callbacks->einfo
12469 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12475 for (o = dynobj->sections; o != NULL; o = o->next)
12477 if ((o->flags & SEC_HAS_CONTENTS) == 0
12479 || o->output_section == bfd_abs_section_ptr)
12481 if ((o->flags & SEC_LINKER_CREATED) == 0)
12483 /* At this point, we are only interested in sections
12484 created by _bfd_elf_link_create_dynamic_sections. */
12487 if (htab->stab_info.stabstr == o)
12489 if (htab->eh_info.hdr_sec == o)
12491 if (strcmp (o->name, ".dynstr") != 0)
12493 if (! bfd_set_section_contents (abfd, o->output_section,
12495 (file_ptr) o->output_offset
12496 * bfd_octets_per_byte (abfd),
12502 /* The contents of the .dynstr section are actually in a
12506 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12507 if (bfd_seek (abfd, off, SEEK_SET) != 0
12508 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12514 if (!info->resolve_section_groups)
12516 bfd_boolean failed = FALSE;
12518 BFD_ASSERT (bfd_link_relocatable (info));
12519 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12524 /* If we have optimized stabs strings, output them. */
12525 if (htab->stab_info.stabstr != NULL)
12527 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12531 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12534 elf_final_link_free (abfd, &flinfo);
12536 elf_linker (abfd) = TRUE;
12540 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12541 if (contents == NULL)
12542 return FALSE; /* Bail out and fail. */
12543 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12544 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12551 elf_final_link_free (abfd, &flinfo);
12555 /* Initialize COOKIE for input bfd ABFD. */
12558 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12559 struct bfd_link_info *info, bfd *abfd)
12561 Elf_Internal_Shdr *symtab_hdr;
12562 const struct elf_backend_data *bed;
12564 bed = get_elf_backend_data (abfd);
12565 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12567 cookie->abfd = abfd;
12568 cookie->sym_hashes = elf_sym_hashes (abfd);
12569 cookie->bad_symtab = elf_bad_symtab (abfd);
12570 if (cookie->bad_symtab)
12572 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12573 cookie->extsymoff = 0;
12577 cookie->locsymcount = symtab_hdr->sh_info;
12578 cookie->extsymoff = symtab_hdr->sh_info;
12581 if (bed->s->arch_size == 32)
12582 cookie->r_sym_shift = 8;
12584 cookie->r_sym_shift = 32;
12586 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12587 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12589 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12590 cookie->locsymcount, 0,
12592 if (cookie->locsyms == NULL)
12594 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12597 if (info->keep_memory)
12598 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12603 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12606 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12608 Elf_Internal_Shdr *symtab_hdr;
12610 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12611 if (cookie->locsyms != NULL
12612 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12613 free (cookie->locsyms);
12616 /* Initialize the relocation information in COOKIE for input section SEC
12617 of input bfd ABFD. */
12620 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12621 struct bfd_link_info *info, bfd *abfd,
12624 if (sec->reloc_count == 0)
12626 cookie->rels = NULL;
12627 cookie->relend = NULL;
12631 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12632 info->keep_memory);
12633 if (cookie->rels == NULL)
12635 cookie->rel = cookie->rels;
12636 cookie->relend = cookie->rels + sec->reloc_count;
12638 cookie->rel = cookie->rels;
12642 /* Free the memory allocated by init_reloc_cookie_rels,
12646 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12649 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12650 free (cookie->rels);
12653 /* Initialize the whole of COOKIE for input section SEC. */
12656 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12657 struct bfd_link_info *info,
12660 if (!init_reloc_cookie (cookie, info, sec->owner))
12662 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12667 fini_reloc_cookie (cookie, sec->owner);
12672 /* Free the memory allocated by init_reloc_cookie_for_section,
12676 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12679 fini_reloc_cookie_rels (cookie, sec);
12680 fini_reloc_cookie (cookie, sec->owner);
12683 /* Garbage collect unused sections. */
12685 /* Default gc_mark_hook. */
12688 _bfd_elf_gc_mark_hook (asection *sec,
12689 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12690 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12691 struct elf_link_hash_entry *h,
12692 Elf_Internal_Sym *sym)
12696 switch (h->root.type)
12698 case bfd_link_hash_defined:
12699 case bfd_link_hash_defweak:
12700 return h->root.u.def.section;
12702 case bfd_link_hash_common:
12703 return h->root.u.c.p->section;
12710 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12715 /* Return the global debug definition section. */
12718 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12719 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12720 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12721 struct elf_link_hash_entry *h,
12722 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12725 && (h->root.type == bfd_link_hash_defined
12726 || h->root.type == bfd_link_hash_defweak)
12727 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12728 return h->root.u.def.section;
12733 /* COOKIE->rel describes a relocation against section SEC, which is
12734 a section we've decided to keep. Return the section that contains
12735 the relocation symbol, or NULL if no section contains it. */
12738 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12739 elf_gc_mark_hook_fn gc_mark_hook,
12740 struct elf_reloc_cookie *cookie,
12741 bfd_boolean *start_stop)
12743 unsigned long r_symndx;
12744 struct elf_link_hash_entry *h;
12746 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12747 if (r_symndx == STN_UNDEF)
12750 if (r_symndx >= cookie->locsymcount
12751 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12753 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12756 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12760 while (h->root.type == bfd_link_hash_indirect
12761 || h->root.type == bfd_link_hash_warning)
12762 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12764 /* If this symbol is weak and there is a non-weak definition, we
12765 keep the non-weak definition because many backends put
12766 dynamic reloc info on the non-weak definition for code
12767 handling copy relocs. */
12768 if (h->is_weakalias)
12769 weakdef (h)->mark = 1;
12771 if (start_stop != NULL)
12773 /* To work around a glibc bug, mark XXX input sections
12774 when there is a reference to __start_XXX or __stop_XXX
12778 asection *s = h->u2.start_stop_section;
12779 *start_stop = !s->gc_mark;
12784 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12787 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12788 &cookie->locsyms[r_symndx]);
12791 /* COOKIE->rel describes a relocation against section SEC, which is
12792 a section we've decided to keep. Mark the section that contains
12793 the relocation symbol. */
12796 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12798 elf_gc_mark_hook_fn gc_mark_hook,
12799 struct elf_reloc_cookie *cookie)
12802 bfd_boolean start_stop = FALSE;
12804 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12805 while (rsec != NULL)
12807 if (!rsec->gc_mark)
12809 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12810 || (rsec->owner->flags & DYNAMIC) != 0)
12812 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12817 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12822 /* The mark phase of garbage collection. For a given section, mark
12823 it and any sections in this section's group, and all the sections
12824 which define symbols to which it refers. */
12827 _bfd_elf_gc_mark (struct bfd_link_info *info,
12829 elf_gc_mark_hook_fn gc_mark_hook)
12832 asection *group_sec, *eh_frame;
12836 /* Mark all the sections in the group. */
12837 group_sec = elf_section_data (sec)->next_in_group;
12838 if (group_sec && !group_sec->gc_mark)
12839 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12842 /* Look through the section relocs. */
12844 eh_frame = elf_eh_frame_section (sec->owner);
12845 if ((sec->flags & SEC_RELOC) != 0
12846 && sec->reloc_count > 0
12847 && sec != eh_frame)
12849 struct elf_reloc_cookie cookie;
12851 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12855 for (; cookie.rel < cookie.relend; cookie.rel++)
12856 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12861 fini_reloc_cookie_for_section (&cookie, sec);
12865 if (ret && eh_frame && elf_fde_list (sec))
12867 struct elf_reloc_cookie cookie;
12869 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12873 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12874 gc_mark_hook, &cookie))
12876 fini_reloc_cookie_for_section (&cookie, eh_frame);
12880 eh_frame = elf_section_eh_frame_entry (sec);
12881 if (ret && eh_frame && !eh_frame->gc_mark)
12882 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12888 /* Scan and mark sections in a special or debug section group. */
12891 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12893 /* Point to first section of section group. */
12895 /* Used to iterate the section group. */
12898 bfd_boolean is_special_grp = TRUE;
12899 bfd_boolean is_debug_grp = TRUE;
12901 /* First scan to see if group contains any section other than debug
12902 and special section. */
12903 ssec = msec = elf_next_in_group (grp);
12906 if ((msec->flags & SEC_DEBUGGING) == 0)
12907 is_debug_grp = FALSE;
12909 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12910 is_special_grp = FALSE;
12912 msec = elf_next_in_group (msec);
12914 while (msec != ssec);
12916 /* If this is a pure debug section group or pure special section group,
12917 keep all sections in this group. */
12918 if (is_debug_grp || is_special_grp)
12923 msec = elf_next_in_group (msec);
12925 while (msec != ssec);
12929 /* Keep debug and special sections. */
12932 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12933 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12937 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12940 bfd_boolean some_kept;
12941 bfd_boolean debug_frag_seen;
12942 bfd_boolean has_kept_debug_info;
12944 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12946 isec = ibfd->sections;
12947 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
12950 /* Ensure all linker created sections are kept,
12951 see if any other section is already marked,
12952 and note if we have any fragmented debug sections. */
12953 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
12954 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12956 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12958 else if (isec->gc_mark
12959 && (isec->flags & SEC_ALLOC) != 0
12960 && elf_section_type (isec) != SHT_NOTE)
12963 if (!debug_frag_seen
12964 && (isec->flags & SEC_DEBUGGING)
12965 && CONST_STRNEQ (isec->name, ".debug_line."))
12966 debug_frag_seen = TRUE;
12969 /* If no non-note alloc section in this file will be kept, then
12970 we can toss out the debug and special sections. */
12974 /* Keep debug and special sections like .comment when they are
12975 not part of a group. Also keep section groups that contain
12976 just debug sections or special sections. */
12977 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12979 if ((isec->flags & SEC_GROUP) != 0)
12980 _bfd_elf_gc_mark_debug_special_section_group (isec);
12981 else if (((isec->flags & SEC_DEBUGGING) != 0
12982 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12983 && elf_next_in_group (isec) == NULL)
12985 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12986 has_kept_debug_info = TRUE;
12989 /* Look for CODE sections which are going to be discarded,
12990 and find and discard any fragmented debug sections which
12991 are associated with that code section. */
12992 if (debug_frag_seen)
12993 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12994 if ((isec->flags & SEC_CODE) != 0
12995 && isec->gc_mark == 0)
13000 ilen = strlen (isec->name);
13002 /* Association is determined by the name of the debug
13003 section containing the name of the code section as
13004 a suffix. For example .debug_line.text.foo is a
13005 debug section associated with .text.foo. */
13006 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13010 if (dsec->gc_mark == 0
13011 || (dsec->flags & SEC_DEBUGGING) == 0)
13014 dlen = strlen (dsec->name);
13017 && strncmp (dsec->name + (dlen - ilen),
13018 isec->name, ilen) == 0)
13023 /* Mark debug sections referenced by kept debug sections. */
13024 if (has_kept_debug_info)
13025 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13027 && (isec->flags & SEC_DEBUGGING) != 0)
13028 if (!_bfd_elf_gc_mark (info, isec,
13029 elf_gc_mark_debug_section))
13036 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13041 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13045 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13046 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13047 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13050 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13053 for (o = sub->sections; o != NULL; o = o->next)
13055 /* When any section in a section group is kept, we keep all
13056 sections in the section group. If the first member of
13057 the section group is excluded, we will also exclude the
13059 if (o->flags & SEC_GROUP)
13061 asection *first = elf_next_in_group (o);
13062 o->gc_mark = first->gc_mark;
13068 /* Skip sweeping sections already excluded. */
13069 if (o->flags & SEC_EXCLUDE)
13072 /* Since this is early in the link process, it is simple
13073 to remove a section from the output. */
13074 o->flags |= SEC_EXCLUDE;
13076 if (info->print_gc_sections && o->size != 0)
13077 /* xgettext:c-format */
13078 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13086 /* Propagate collected vtable information. This is called through
13087 elf_link_hash_traverse. */
13090 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13092 /* Those that are not vtables. */
13094 || h->u2.vtable == NULL
13095 || h->u2.vtable->parent == NULL)
13098 /* Those vtables that do not have parents, we cannot merge. */
13099 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13102 /* If we've already been done, exit. */
13103 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13106 /* Make sure the parent's table is up to date. */
13107 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13109 if (h->u2.vtable->used == NULL)
13111 /* None of this table's entries were referenced. Re-use the
13113 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13114 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13119 bfd_boolean *cu, *pu;
13121 /* Or the parent's entries into ours. */
13122 cu = h->u2.vtable->used;
13124 pu = h->u2.vtable->parent->u2.vtable->used;
13127 const struct elf_backend_data *bed;
13128 unsigned int log_file_align;
13130 bed = get_elf_backend_data (h->root.u.def.section->owner);
13131 log_file_align = bed->s->log_file_align;
13132 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13147 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13150 bfd_vma hstart, hend;
13151 Elf_Internal_Rela *relstart, *relend, *rel;
13152 const struct elf_backend_data *bed;
13153 unsigned int log_file_align;
13155 /* Take care of both those symbols that do not describe vtables as
13156 well as those that are not loaded. */
13158 || h->u2.vtable == NULL
13159 || h->u2.vtable->parent == NULL)
13162 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13163 || h->root.type == bfd_link_hash_defweak);
13165 sec = h->root.u.def.section;
13166 hstart = h->root.u.def.value;
13167 hend = hstart + h->size;
13169 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13171 return *(bfd_boolean *) okp = FALSE;
13172 bed = get_elf_backend_data (sec->owner);
13173 log_file_align = bed->s->log_file_align;
13175 relend = relstart + sec->reloc_count;
13177 for (rel = relstart; rel < relend; ++rel)
13178 if (rel->r_offset >= hstart && rel->r_offset < hend)
13180 /* If the entry is in use, do nothing. */
13181 if (h->u2.vtable->used
13182 && (rel->r_offset - hstart) < h->u2.vtable->size)
13184 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13185 if (h->u2.vtable->used[entry])
13188 /* Otherwise, kill it. */
13189 rel->r_offset = rel->r_info = rel->r_addend = 0;
13195 /* Mark sections containing dynamically referenced symbols. When
13196 building shared libraries, we must assume that any visible symbol is
13200 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13202 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13203 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13205 if ((h->root.type == bfd_link_hash_defined
13206 || h->root.type == bfd_link_hash_defweak)
13208 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13209 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13210 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13211 && (!bfd_link_executable (info)
13212 || info->gc_keep_exported
13213 || info->export_dynamic
13216 && (*d->match) (&d->head, NULL, h->root.root.string)))
13217 && (h->versioned >= versioned
13218 || !bfd_hide_sym_by_version (info->version_info,
13219 h->root.root.string)))))
13220 h->root.u.def.section->flags |= SEC_KEEP;
13225 /* Keep all sections containing symbols undefined on the command-line,
13226 and the section containing the entry symbol. */
13229 _bfd_elf_gc_keep (struct bfd_link_info *info)
13231 struct bfd_sym_chain *sym;
13233 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13235 struct elf_link_hash_entry *h;
13237 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13238 FALSE, FALSE, FALSE);
13241 && (h->root.type == bfd_link_hash_defined
13242 || h->root.type == bfd_link_hash_defweak)
13243 && !bfd_is_abs_section (h->root.u.def.section)
13244 && !bfd_is_und_section (h->root.u.def.section))
13245 h->root.u.def.section->flags |= SEC_KEEP;
13250 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13251 struct bfd_link_info *info)
13253 bfd *ibfd = info->input_bfds;
13255 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13258 struct elf_reloc_cookie cookie;
13260 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13262 sec = ibfd->sections;
13263 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13266 if (!init_reloc_cookie (&cookie, info, ibfd))
13269 for (sec = ibfd->sections; sec; sec = sec->next)
13271 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13272 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13274 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13275 fini_reloc_cookie_rels (&cookie, sec);
13282 /* Do mark and sweep of unused sections. */
13285 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13287 bfd_boolean ok = TRUE;
13289 elf_gc_mark_hook_fn gc_mark_hook;
13290 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13291 struct elf_link_hash_table *htab;
13293 if (!bed->can_gc_sections
13294 || !is_elf_hash_table (info->hash))
13296 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13300 bed->gc_keep (info);
13301 htab = elf_hash_table (info);
13303 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13304 at the .eh_frame section if we can mark the FDEs individually. */
13305 for (sub = info->input_bfds;
13306 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13307 sub = sub->link.next)
13310 struct elf_reloc_cookie cookie;
13312 sec = sub->sections;
13313 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13315 sec = bfd_get_section_by_name (sub, ".eh_frame");
13316 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13318 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13319 if (elf_section_data (sec)->sec_info
13320 && (sec->flags & SEC_LINKER_CREATED) == 0)
13321 elf_eh_frame_section (sub) = sec;
13322 fini_reloc_cookie_for_section (&cookie, sec);
13323 sec = bfd_get_next_section_by_name (NULL, sec);
13327 /* Apply transitive closure to the vtable entry usage info. */
13328 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13332 /* Kill the vtable relocations that were not used. */
13333 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13337 /* Mark dynamically referenced symbols. */
13338 if (htab->dynamic_sections_created || info->gc_keep_exported)
13339 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13341 /* Grovel through relocs to find out who stays ... */
13342 gc_mark_hook = bed->gc_mark_hook;
13343 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13347 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13348 || elf_object_id (sub) != elf_hash_table_id (htab)
13349 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13353 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13356 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13357 Also treat note sections as a root, if the section is not part
13359 for (o = sub->sections; o != NULL; o = o->next)
13361 && (o->flags & SEC_EXCLUDE) == 0
13362 && ((o->flags & SEC_KEEP) != 0
13363 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13364 && elf_next_in_group (o) == NULL )))
13366 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13371 /* Allow the backend to mark additional target specific sections. */
13372 bed->gc_mark_extra_sections (info, gc_mark_hook);
13374 /* ... and mark SEC_EXCLUDE for those that go. */
13375 return elf_gc_sweep (abfd, info);
13378 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13381 bfd_elf_gc_record_vtinherit (bfd *abfd,
13383 struct elf_link_hash_entry *h,
13386 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13387 struct elf_link_hash_entry **search, *child;
13388 size_t extsymcount;
13389 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13391 /* The sh_info field of the symtab header tells us where the
13392 external symbols start. We don't care about the local symbols at
13394 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13395 if (!elf_bad_symtab (abfd))
13396 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13398 sym_hashes = elf_sym_hashes (abfd);
13399 sym_hashes_end = sym_hashes + extsymcount;
13401 /* Hunt down the child symbol, which is in this section at the same
13402 offset as the relocation. */
13403 for (search = sym_hashes; search != sym_hashes_end; ++search)
13405 if ((child = *search) != NULL
13406 && (child->root.type == bfd_link_hash_defined
13407 || child->root.type == bfd_link_hash_defweak)
13408 && child->root.u.def.section == sec
13409 && child->root.u.def.value == offset)
13413 /* xgettext:c-format */
13414 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13415 abfd, sec, offset);
13416 bfd_set_error (bfd_error_invalid_operation);
13420 if (!child->u2.vtable)
13422 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13423 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13424 if (!child->u2.vtable)
13429 /* This *should* only be the absolute section. It could potentially
13430 be that someone has defined a non-global vtable though, which
13431 would be bad. It isn't worth paging in the local symbols to be
13432 sure though; that case should simply be handled by the assembler. */
13434 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13437 child->u2.vtable->parent = h;
13442 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13445 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13446 asection *sec ATTRIBUTE_UNUSED,
13447 struct elf_link_hash_entry *h,
13450 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13451 unsigned int log_file_align = bed->s->log_file_align;
13455 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13456 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13461 if (addend >= h->u2.vtable->size)
13463 size_t size, bytes, file_align;
13464 bfd_boolean *ptr = h->u2.vtable->used;
13466 /* While the symbol is undefined, we have to be prepared to handle
13468 file_align = 1 << log_file_align;
13469 if (h->root.type == bfd_link_hash_undefined)
13470 size = addend + file_align;
13474 if (addend >= size)
13476 /* Oops! We've got a reference past the defined end of
13477 the table. This is probably a bug -- shall we warn? */
13478 size = addend + file_align;
13481 size = (size + file_align - 1) & -file_align;
13483 /* Allocate one extra entry for use as a "done" flag for the
13484 consolidation pass. */
13485 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13489 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13495 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13496 * sizeof (bfd_boolean));
13497 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13501 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13506 /* And arrange for that done flag to be at index -1. */
13507 h->u2.vtable->used = ptr + 1;
13508 h->u2.vtable->size = size;
13511 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13516 /* Map an ELF section header flag to its corresponding string. */
13520 flagword flag_value;
13521 } elf_flags_to_name_table;
13523 static elf_flags_to_name_table elf_flags_to_names [] =
13525 { "SHF_WRITE", SHF_WRITE },
13526 { "SHF_ALLOC", SHF_ALLOC },
13527 { "SHF_EXECINSTR", SHF_EXECINSTR },
13528 { "SHF_MERGE", SHF_MERGE },
13529 { "SHF_STRINGS", SHF_STRINGS },
13530 { "SHF_INFO_LINK", SHF_INFO_LINK},
13531 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13532 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13533 { "SHF_GROUP", SHF_GROUP },
13534 { "SHF_TLS", SHF_TLS },
13535 { "SHF_MASKOS", SHF_MASKOS },
13536 { "SHF_EXCLUDE", SHF_EXCLUDE },
13539 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13541 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13542 struct flag_info *flaginfo,
13545 const bfd_vma sh_flags = elf_section_flags (section);
13547 if (!flaginfo->flags_initialized)
13549 bfd *obfd = info->output_bfd;
13550 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13551 struct flag_info_list *tf = flaginfo->flag_list;
13553 int without_hex = 0;
13555 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13558 flagword (*lookup) (char *);
13560 lookup = bed->elf_backend_lookup_section_flags_hook;
13561 if (lookup != NULL)
13563 flagword hexval = (*lookup) ((char *) tf->name);
13567 if (tf->with == with_flags)
13568 with_hex |= hexval;
13569 else if (tf->with == without_flags)
13570 without_hex |= hexval;
13575 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13577 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13579 if (tf->with == with_flags)
13580 with_hex |= elf_flags_to_names[i].flag_value;
13581 else if (tf->with == without_flags)
13582 without_hex |= elf_flags_to_names[i].flag_value;
13589 info->callbacks->einfo
13590 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13594 flaginfo->flags_initialized = TRUE;
13595 flaginfo->only_with_flags |= with_hex;
13596 flaginfo->not_with_flags |= without_hex;
13599 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13602 if ((flaginfo->not_with_flags & sh_flags) != 0)
13608 struct alloc_got_off_arg {
13610 struct bfd_link_info *info;
13613 /* We need a special top-level link routine to convert got reference counts
13614 to real got offsets. */
13617 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13619 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13620 bfd *obfd = gofarg->info->output_bfd;
13621 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13623 if (h->got.refcount > 0)
13625 h->got.offset = gofarg->gotoff;
13626 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13629 h->got.offset = (bfd_vma) -1;
13634 /* And an accompanying bit to work out final got entry offsets once
13635 we're done. Should be called from final_link. */
13638 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13639 struct bfd_link_info *info)
13642 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13644 struct alloc_got_off_arg gofarg;
13646 BFD_ASSERT (abfd == info->output_bfd);
13648 if (! is_elf_hash_table (info->hash))
13651 /* The GOT offset is relative to the .got section, but the GOT header is
13652 put into the .got.plt section, if the backend uses it. */
13653 if (bed->want_got_plt)
13656 gotoff = bed->got_header_size;
13658 /* Do the local .got entries first. */
13659 for (i = info->input_bfds; i; i = i->link.next)
13661 bfd_signed_vma *local_got;
13662 size_t j, locsymcount;
13663 Elf_Internal_Shdr *symtab_hdr;
13665 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13668 local_got = elf_local_got_refcounts (i);
13672 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13673 if (elf_bad_symtab (i))
13674 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13676 locsymcount = symtab_hdr->sh_info;
13678 for (j = 0; j < locsymcount; ++j)
13680 if (local_got[j] > 0)
13682 local_got[j] = gotoff;
13683 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13686 local_got[j] = (bfd_vma) -1;
13690 /* Then the global .got entries. .plt refcounts are handled by
13691 adjust_dynamic_symbol */
13692 gofarg.gotoff = gotoff;
13693 gofarg.info = info;
13694 elf_link_hash_traverse (elf_hash_table (info),
13695 elf_gc_allocate_got_offsets,
13700 /* Many folk need no more in the way of final link than this, once
13701 got entry reference counting is enabled. */
13704 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13706 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13709 /* Invoke the regular ELF backend linker to do all the work. */
13710 return bfd_elf_final_link (abfd, info);
13714 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13716 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13718 if (rcookie->bad_symtab)
13719 rcookie->rel = rcookie->rels;
13721 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13723 unsigned long r_symndx;
13725 if (! rcookie->bad_symtab)
13726 if (rcookie->rel->r_offset > offset)
13728 if (rcookie->rel->r_offset != offset)
13731 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13732 if (r_symndx == STN_UNDEF)
13735 if (r_symndx >= rcookie->locsymcount
13736 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13738 struct elf_link_hash_entry *h;
13740 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13742 while (h->root.type == bfd_link_hash_indirect
13743 || h->root.type == bfd_link_hash_warning)
13744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13746 if ((h->root.type == bfd_link_hash_defined
13747 || h->root.type == bfd_link_hash_defweak)
13748 && (h->root.u.def.section->owner != rcookie->abfd
13749 || h->root.u.def.section->kept_section != NULL
13750 || discarded_section (h->root.u.def.section)))
13755 /* It's not a relocation against a global symbol,
13756 but it could be a relocation against a local
13757 symbol for a discarded section. */
13759 Elf_Internal_Sym *isym;
13761 /* Need to: get the symbol; get the section. */
13762 isym = &rcookie->locsyms[r_symndx];
13763 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13765 && (isec->kept_section != NULL
13766 || discarded_section (isec)))
13774 /* Discard unneeded references to discarded sections.
13775 Returns -1 on error, 1 if any section's size was changed, 0 if
13776 nothing changed. This function assumes that the relocations are in
13777 sorted order, which is true for all known assemblers. */
13780 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13782 struct elf_reloc_cookie cookie;
13787 if (info->traditional_format
13788 || !is_elf_hash_table (info->hash))
13791 o = bfd_get_section_by_name (output_bfd, ".stab");
13796 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13799 || i->reloc_count == 0
13800 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13804 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13807 if (!init_reloc_cookie_for_section (&cookie, info, i))
13810 if (_bfd_discard_section_stabs (abfd, i,
13811 elf_section_data (i)->sec_info,
13812 bfd_elf_reloc_symbol_deleted_p,
13816 fini_reloc_cookie_for_section (&cookie, i);
13821 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13822 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13826 int eh_changed = 0;
13827 unsigned int eh_alignment;
13829 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13835 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13838 if (!init_reloc_cookie_for_section (&cookie, info, i))
13841 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13842 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13843 bfd_elf_reloc_symbol_deleted_p,
13847 if (i->size != i->rawsize)
13851 fini_reloc_cookie_for_section (&cookie, i);
13854 eh_alignment = 1 << o->alignment_power;
13855 /* Skip over zero terminator, and prevent empty sections from
13856 adding alignment padding at the end. */
13857 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13859 i->flags |= SEC_EXCLUDE;
13860 else if (i->size > 4)
13862 /* The last non-empty eh_frame section doesn't need padding. */
13865 /* Any prior sections must pad the last FDE out to the output
13866 section alignment. Otherwise we might have zero padding
13867 between sections, which would be seen as a terminator. */
13868 for (; i != NULL; i = i->map_tail.s)
13870 /* All but the last zero terminator should have been removed. */
13875 = (i->size + eh_alignment - 1) & -eh_alignment;
13876 if (i->size != size)
13884 elf_link_hash_traverse (elf_hash_table (info),
13885 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13888 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13890 const struct elf_backend_data *bed;
13893 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13895 s = abfd->sections;
13896 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13899 bed = get_elf_backend_data (abfd);
13901 if (bed->elf_backend_discard_info != NULL)
13903 if (!init_reloc_cookie (&cookie, info, abfd))
13906 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13909 fini_reloc_cookie (&cookie, abfd);
13913 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13914 _bfd_elf_end_eh_frame_parsing (info);
13916 if (info->eh_frame_hdr_type
13917 && !bfd_link_relocatable (info)
13918 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13925 _bfd_elf_section_already_linked (bfd *abfd,
13927 struct bfd_link_info *info)
13930 const char *name, *key;
13931 struct bfd_section_already_linked *l;
13932 struct bfd_section_already_linked_hash_entry *already_linked_list;
13934 if (sec->output_section == bfd_abs_section_ptr)
13937 flags = sec->flags;
13939 /* Return if it isn't a linkonce section. A comdat group section
13940 also has SEC_LINK_ONCE set. */
13941 if ((flags & SEC_LINK_ONCE) == 0)
13944 /* Don't put group member sections on our list of already linked
13945 sections. They are handled as a group via their group section. */
13946 if (elf_sec_group (sec) != NULL)
13949 /* For a SHT_GROUP section, use the group signature as the key. */
13951 if ((flags & SEC_GROUP) != 0
13952 && elf_next_in_group (sec) != NULL
13953 && elf_group_name (elf_next_in_group (sec)) != NULL)
13954 key = elf_group_name (elf_next_in_group (sec));
13957 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13958 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13959 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13962 /* Must be a user linkonce section that doesn't follow gcc's
13963 naming convention. In this case we won't be matching
13964 single member groups. */
13968 already_linked_list = bfd_section_already_linked_table_lookup (key);
13970 for (l = already_linked_list->entry; l != NULL; l = l->next)
13972 /* We may have 2 different types of sections on the list: group
13973 sections with a signature of <key> (<key> is some string),
13974 and linkonce sections named .gnu.linkonce.<type>.<key>.
13975 Match like sections. LTO plugin sections are an exception.
13976 They are always named .gnu.linkonce.t.<key> and match either
13977 type of section. */
13978 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13979 && ((flags & SEC_GROUP) != 0
13980 || strcmp (name, l->sec->name) == 0))
13981 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13983 /* The section has already been linked. See if we should
13984 issue a warning. */
13985 if (!_bfd_handle_already_linked (sec, l, info))
13988 if (flags & SEC_GROUP)
13990 asection *first = elf_next_in_group (sec);
13991 asection *s = first;
13995 s->output_section = bfd_abs_section_ptr;
13996 /* Record which group discards it. */
13997 s->kept_section = l->sec;
13998 s = elf_next_in_group (s);
13999 /* These lists are circular. */
14009 /* A single member comdat group section may be discarded by a
14010 linkonce section and vice versa. */
14011 if ((flags & SEC_GROUP) != 0)
14013 asection *first = elf_next_in_group (sec);
14015 if (first != NULL && elf_next_in_group (first) == first)
14016 /* Check this single member group against linkonce sections. */
14017 for (l = already_linked_list->entry; l != NULL; l = l->next)
14018 if ((l->sec->flags & SEC_GROUP) == 0
14019 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14021 first->output_section = bfd_abs_section_ptr;
14022 first->kept_section = l->sec;
14023 sec->output_section = bfd_abs_section_ptr;
14028 /* Check this linkonce section against single member groups. */
14029 for (l = already_linked_list->entry; l != NULL; l = l->next)
14030 if (l->sec->flags & SEC_GROUP)
14032 asection *first = elf_next_in_group (l->sec);
14035 && elf_next_in_group (first) == first
14036 && bfd_elf_match_symbols_in_sections (first, sec, info))
14038 sec->output_section = bfd_abs_section_ptr;
14039 sec->kept_section = first;
14044 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14045 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14046 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14047 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14048 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14049 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14050 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14051 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14052 The reverse order cannot happen as there is never a bfd with only the
14053 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14054 matter as here were are looking only for cross-bfd sections. */
14056 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14057 for (l = already_linked_list->entry; l != NULL; l = l->next)
14058 if ((l->sec->flags & SEC_GROUP) == 0
14059 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14061 if (abfd != l->sec->owner)
14062 sec->output_section = bfd_abs_section_ptr;
14066 /* This is the first section with this name. Record it. */
14067 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14068 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14069 return sec->output_section == bfd_abs_section_ptr;
14073 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14075 return sym->st_shndx == SHN_COMMON;
14079 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14085 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14087 return bfd_com_section_ptr;
14091 _bfd_elf_default_got_elt_size (bfd *abfd,
14092 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14093 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14094 bfd *ibfd ATTRIBUTE_UNUSED,
14095 unsigned long symndx ATTRIBUTE_UNUSED)
14097 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14098 return bed->s->arch_size / 8;
14101 /* Routines to support the creation of dynamic relocs. */
14103 /* Returns the name of the dynamic reloc section associated with SEC. */
14105 static const char *
14106 get_dynamic_reloc_section_name (bfd * abfd,
14108 bfd_boolean is_rela)
14111 const char *old_name = bfd_get_section_name (NULL, sec);
14112 const char *prefix = is_rela ? ".rela" : ".rel";
14114 if (old_name == NULL)
14117 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14118 sprintf (name, "%s%s", prefix, old_name);
14123 /* Returns the dynamic reloc section associated with SEC.
14124 If necessary compute the name of the dynamic reloc section based
14125 on SEC's name (looked up in ABFD's string table) and the setting
14129 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14131 bfd_boolean is_rela)
14133 asection * reloc_sec = elf_section_data (sec)->sreloc;
14135 if (reloc_sec == NULL)
14137 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14141 reloc_sec = bfd_get_linker_section (abfd, name);
14143 if (reloc_sec != NULL)
14144 elf_section_data (sec)->sreloc = reloc_sec;
14151 /* Returns the dynamic reloc section associated with SEC. If the
14152 section does not exist it is created and attached to the DYNOBJ
14153 bfd and stored in the SRELOC field of SEC's elf_section_data
14156 ALIGNMENT is the alignment for the newly created section and
14157 IS_RELA defines whether the name should be .rela.<SEC's name>
14158 or .rel.<SEC's name>. The section name is looked up in the
14159 string table associated with ABFD. */
14162 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14164 unsigned int alignment,
14166 bfd_boolean is_rela)
14168 asection * reloc_sec = elf_section_data (sec)->sreloc;
14170 if (reloc_sec == NULL)
14172 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14177 reloc_sec = bfd_get_linker_section (dynobj, name);
14179 if (reloc_sec == NULL)
14181 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14182 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14183 if ((sec->flags & SEC_ALLOC) != 0)
14184 flags |= SEC_ALLOC | SEC_LOAD;
14186 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14187 if (reloc_sec != NULL)
14189 /* _bfd_elf_get_sec_type_attr chooses a section type by
14190 name. Override as it may be wrong, eg. for a user
14191 section named "auto" we'll get ".relauto" which is
14192 seen to be a .rela section. */
14193 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14194 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14199 elf_section_data (sec)->sreloc = reloc_sec;
14205 /* Copy the ELF symbol type and other attributes for a linker script
14206 assignment from HSRC to HDEST. Generally this should be treated as
14207 if we found a strong non-dynamic definition for HDEST (except that
14208 ld ignores multiple definition errors). */
14210 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14211 struct bfd_link_hash_entry *hdest,
14212 struct bfd_link_hash_entry *hsrc)
14214 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14215 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14216 Elf_Internal_Sym isym;
14218 ehdest->type = ehsrc->type;
14219 ehdest->target_internal = ehsrc->target_internal;
14221 isym.st_other = ehsrc->other;
14222 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14225 /* Append a RELA relocation REL to section S in BFD. */
14228 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14230 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14231 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14232 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14233 bed->s->swap_reloca_out (abfd, rel, loc);
14236 /* Append a REL relocation REL to section S in BFD. */
14239 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14241 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14242 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14243 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14244 bed->s->swap_reloc_out (abfd, rel, loc);
14247 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14249 struct bfd_link_hash_entry *
14250 bfd_elf_define_start_stop (struct bfd_link_info *info,
14251 const char *symbol, asection *sec)
14253 struct elf_link_hash_entry *h;
14255 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14256 FALSE, FALSE, TRUE);
14258 && (h->root.type == bfd_link_hash_undefined
14259 || h->root.type == bfd_link_hash_undefweak
14260 || (h->ref_regular && !h->def_regular)))
14262 h->root.type = bfd_link_hash_defined;
14263 h->root.u.def.section = sec;
14264 h->root.u.def.value = 0;
14265 h->def_regular = 1;
14266 h->def_dynamic = 0;
14268 h->u2.start_stop_section = sec;
14269 if (symbol[0] == '.')
14271 /* .startof. and .sizeof. symbols are local. */
14272 const struct elf_backend_data *bed;
14273 bed = get_elf_backend_data (info->output_bfd);
14274 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14276 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14277 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;