1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
96 && discard ? discarded_section (isec) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
233 hash_table->dynobj = abfd;
236 if (hash_table->dynstr == NULL)
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
260 if (! is_elf_hash_table (info->hash))
263 if (elf_hash_table (info)->dynamic_sections_created)
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
272 flags = bed->dynamic_sec_flags;
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
295 || ! bfd_set_section_alignment (abfd, s, 1))
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 elf_hash_table (info)->dynsym = s;
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
342 if (info->emit_gnu_hash)
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
370 /* Create dynamic sections when linking against a dynamic object. */
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 if (bed->want_plt_sym)
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
422 if (! _bfd_elf_create_got_section (abfd, info))
425 if (bed->want_dynbss)
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (bed->want_dynrelro)
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
462 if (bfd_link_executable (info))
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
473 if (bed->want_dynrelro)
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
483 htab->sreldynrelro = s;
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
503 if (h->dynindx == -1)
505 struct elf_strtab_hash *dynstr;
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
522 if (!elf_hash_table (info)->is_relocatable_executable)
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
533 dynstr = elf_hash_table (info)->dynstr;
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
542 /* We don't put any version information in the dynamic string
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
559 if (indx == (size_t) -1)
561 h->dynstr_index = indx;
567 /* Mark a symbol dynamic. */
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
596 bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
602 struct elf_link_hash_entry *h, *hv;
603 struct elf_link_hash_table *htab;
604 const struct elf_backend_data *bed;
606 if (!is_elf_hash_table (info->hash))
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
617 if (h->versioned == unknown)
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
626 h->versioned = versioned;
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 switch (h->root.type)
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h->root.type = bfd_link_hash_new;
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
653 case bfd_link_hash_new:
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed = get_elf_backend_data (output_bfd);
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
682 h->root.type = bfd_link_hash_undefined;
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
691 h->verinfo.verdef = NULL;
693 /* Make sure this symbol is not garbage collected. */
700 bed = get_elf_backend_data (output_bfd);
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 if (!bfd_link_relocatable (info)
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
728 struct elf_link_hash_entry *def = weakdef (h);
730 if (def->dynindx == -1
731 && !bfd_elf_link_record_dynamic_symbol (info, def))
739 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
740 success, and 2 on a failure caused by attempting to record a symbol
741 in a discarded section, eg. a discarded link-once section symbol. */
744 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 struct elf_link_local_dynamic_entry *entry;
750 struct elf_link_hash_table *eht;
751 struct elf_strtab_hash *dynstr;
754 Elf_External_Sym_Shndx eshndx;
755 char esym[sizeof (Elf64_External_Sym)];
757 if (! is_elf_hash_table (info->hash))
760 /* See if the entry exists already. */
761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
762 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 amt = sizeof (*entry);
766 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
770 /* Go find the symbol, so that we can find it's name. */
771 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
772 1, input_indx, &entry->isym, esym, &eshndx))
774 bfd_release (input_bfd, entry);
778 if (entry->isym.st_shndx != SHN_UNDEF
779 && entry->isym.st_shndx < SHN_LORESERVE)
783 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
784 if (s == NULL || bfd_is_abs_section (s->output_section))
786 /* We can still bfd_release here as nothing has done another
787 bfd_alloc. We can't do this later in this function. */
788 bfd_release (input_bfd, entry);
793 name = (bfd_elf_string_from_elf_section
794 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
795 entry->isym.st_name));
797 dynstr = elf_hash_table (info)->dynstr;
800 /* Create a strtab to hold the dynamic symbol names. */
801 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
807 if (dynstr_index == (size_t) -1)
809 entry->isym.st_name = dynstr_index;
811 eht = elf_hash_table (info);
813 entry->next = eht->dynlocal;
814 eht->dynlocal = entry;
815 entry->input_bfd = input_bfd;
816 entry->input_indx = input_indx;
819 /* Whatever binding the symbol had before, it's now local. */
821 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
823 /* The dynindx will be set at the end of size_dynamic_sections. */
828 /* Return the dynindex of a local dynamic symbol. */
831 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
835 struct elf_link_local_dynamic_entry *e;
837 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
838 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 /* This function is used to renumber the dynamic symbols, if some of
844 them are removed because they are marked as local. This is called
845 via elf_link_hash_traverse. */
848 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 size_t *count = (size_t *) data;
856 if (h->dynindx != -1)
857 h->dynindx = ++(*count);
863 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
864 STB_LOCAL binding. */
867 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 size_t *count = (size_t *) data;
872 if (!h->forced_local)
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
881 /* Return true if the dynamic symbol for a given section should be
882 omitted when creating a shared library. */
884 _bfd_elf_omit_section_dynsym_default (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. */
917 _bfd_elf_omit_section_dynsym_all
918 (bfd *output_bfd ATTRIBUTE_UNUSED,
919 struct bfd_link_info *info ATTRIBUTE_UNUSED,
920 asection *p ATTRIBUTE_UNUSED)
925 /* Assign dynsym indices. In a shared library we generate a section
926 symbol for each output section, which come first. Next come symbols
927 which have been forced to local binding. Then all of the back-end
928 allocated local dynamic syms, followed by the rest of the global
929 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
930 (This prevents the early call before elf_backend_init_index_section
931 and strip_excluded_output_sections setting dynindx for sections
932 that are stripped.) */
935 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
936 struct bfd_link_info *info,
937 unsigned long *section_sym_count)
939 unsigned long dynsymcount = 0;
940 bfd_boolean do_sec = section_sym_count != NULL;
942 if (bfd_link_pic (info)
943 || elf_hash_table (info)->is_relocatable_executable)
945 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
947 for (p = output_bfd->sections; p ; p = p->next)
948 if ((p->flags & SEC_EXCLUDE) == 0
949 && (p->flags & SEC_ALLOC) != 0
950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
954 elf_section_data (p)->dynindx = dynsymcount;
957 elf_section_data (p)->dynindx = 0;
960 *section_sym_count = dynsymcount;
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
966 if (elf_hash_table (info)->dynlocal)
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
984 elf_hash_table (info)->dynsymcount = dynsymcount;
988 /* Merge st_other field. */
991 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
992 const Elf_Internal_Sym *isym, asection *sec,
993 bfd_boolean definition, bfd_boolean dynamic)
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
1016 h->protected_def = 1;
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1033 _bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1036 Elf_Internal_Sym *sym,
1039 struct elf_link_hash_entry **sym_hash,
1041 bfd_boolean *pold_weak,
1042 unsigned int *pold_alignment,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
1049 asection *sec, *oldsec;
1050 struct elf_link_hash_entry *h;
1051 struct elf_link_hash_entry *hi;
1052 struct elf_link_hash_entry *flip;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1057 const struct elf_backend_data *bed;
1059 bfd_boolean default_sym = *matched;
1065 bind = ELF_ST_BIND (sym->st_info);
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1076 bed = get_elf_backend_data (abfd);
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h->versioned != unversioned)
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1085 if (h->versioned == unknown)
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1090 h->versioned = versioned;
1093 if (new_version[0] == '\0')
1097 h->versioned = unversioned;
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1127 /* OLD_VERSION is the symbol version of the existing
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1152 switch (h->root.type)
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1175 if (poldbfd && *poldbfd == NULL)
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1183 *pold_weak = oldweak;
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1204 if (bfd_is_und_section (sec))
1206 if (bind != STB_WEAK)
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1214 /* Update the existing symbol only if they match. */
1217 hi->dynamic_def = 1;
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1225 if (h->root.type == bfd_link_hash_new)
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1238 && (newweak || oldweak)
1239 && ((abfd->flags & DYNAMIC) == 0
1240 || !h->def_regular))
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1246 else if (oldsec != NULL)
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
1270 && h->root.type != bfd_link_hash_common);
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1281 if (!(newfunc && oldfunc)
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1340 if (h->type == STT_TLS)
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1365 else if (!tdef && !ntdef)
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
1370 h->root.root.string, tbfd, ntbfd);
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, tsec, ntbfd);
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
1382 h->root.root.string, tbfd, ntbfd, ntsec);
1384 bfd_set_error (bfd_error_bad_value);
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1392 && !bfd_is_und_section (sec))
1395 /* Make sure this symbol is dynamic. */
1397 hi->ref_dynamic = 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1403 return bfd_elf_link_record_dynamic_symbol (info, h);
1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi->root.type == bfd_link_hash_indirect)
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1422 hi->root.type = h->root.type;
1423 h->root.type = bfd_link_hash_indirect;
1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1458 h->root.type = bfd_link_hash_undefined;
1459 h->root.u.undef.abfd = abfd;
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1502 if (olddef && newdyn)
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc && oldfunc)
1507 *type_change_ok = TRUE;
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1556 newdyncommon = TRUE;
1558 newdyncommon = FALSE;
1562 && h->root.type == bfd_link_hash_defined
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1568 olddyncommon = TRUE;
1570 olddyncommon = FALSE;
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed->merge_symbol != NULL)
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1584 && !default_sym && h->def_regular
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1602 && sym->st_size != h->size)
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
1615 *size_change_ok = TRUE;
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1635 || (h->root.type == bfd_link_hash_common
1636 && (newweak || newfunc))))
1640 newdyncommon = FALSE;
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1662 && h->root.type == bfd_link_hash_common)
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
1668 *psec = sec = bed->common_section (oldsec);
1669 *size_change_ok = TRUE;
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef && olddef && newweak)
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
1678 && (abfd->flags & BFD_PLUGIN) == 0))
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1711 || (bfd_is_com_section (sec)
1712 && (oldweak || oldfunc)))
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1726 olddyncommon = FALSE;
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1731 if (bfd_is_com_section (sec))
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1739 h->type = STT_NOTYPE;
1741 *type_change_ok = TRUE;
1744 if (hi->root.type == bfd_link_hash_indirect)
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1760 && bfd_is_com_section (sec)
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1772 if (h->size > *pvalue)
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
1781 olddyncommon = FALSE;
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1789 if (hi->root.type == bfd_link_hash_indirect)
1792 h->verinfo.vertree = NULL;
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip->root.type = h->root.type;
1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1808 flip->ref_dynamic = 1;
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1821 _bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1825 Elf_Internal_Sym *sym,
1829 bfd_boolean *dynsym)
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
1837 const struct elf_backend_data *bed;
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
1840 bfd_boolean override;
1842 size_t len, shortlen;
1844 bfd_boolean matched;
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
1854 if (h->versioned == unknown)
1858 h->versioned = unversioned;
1863 if (p[1] != ELF_VER_CHR)
1865 h->versioned = versioned_hidden;
1869 h->versioned = versioned;
1874 /* PR ld/19073: We may see an unversioned definition after the
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1884 shortlen = p - name;
1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1886 if (shortname == NULL)
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 &hi, poldbfd, NULL, NULL, &skip, &override,
1901 &type_change_ok, &size_change_ok, &matched))
1907 if (hi->def_regular)
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info))
1938 if (! (_bfd_generic_link_add_one_symbol
1939 (info, abfd, shortname, BSF_INDIRECT,
1940 bfd_ind_section_ptr,
1941 0, name, FALSE, collect, &bh)))
1943 hi = (struct elf_link_hash_entry *) bh;
1948 /* In this case the symbol named SHORTNAME is overriding the
1949 indirect symbol we want to add. We were planning on making
1950 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1951 is the name without a version. NAME is the fully versioned
1952 name, and it is the default version.
1954 Overriding means that we already saw a definition for the
1955 symbol SHORTNAME in a regular object, and it is overriding
1956 the symbol defined in the dynamic object.
1958 When this happens, we actually want to change NAME, the
1959 symbol we just added, to refer to SHORTNAME. This will cause
1960 references to NAME in the shared object to become references
1961 to SHORTNAME in the regular object. This is what we expect
1962 when we override a function in a shared object: that the
1963 references in the shared object will be mapped to the
1964 definition in the regular object. */
1966 while (hi->root.type == bfd_link_hash_indirect
1967 || hi->root.type == bfd_link_hash_warning)
1968 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1970 h->root.type = bfd_link_hash_indirect;
1971 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1975 hi->ref_dynamic = 1;
1979 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1984 /* Now set HI to H, so that the following code will set the
1985 other fields correctly. */
1989 /* Check if HI is a warning symbol. */
1990 if (hi->root.type == bfd_link_hash_warning)
1991 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1993 /* If there is a duplicate definition somewhere, then HI may not
1994 point to an indirect symbol. We will have reported an error to
1995 the user in that case. */
1997 if (hi->root.type == bfd_link_hash_indirect)
1999 struct elf_link_hash_entry *ht;
2001 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2002 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2004 /* A reference to the SHORTNAME symbol from a dynamic library
2005 will be satisfied by the versioned symbol at runtime. In
2006 effect, we have a reference to the versioned symbol. */
2007 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2008 hi->dynamic_def |= ht->dynamic_def;
2010 /* See if the new flags lead us to realize that the symbol must
2016 if (! bfd_link_executable (info)
2023 if (hi->ref_regular)
2029 /* We also need to define an indirection from the nondefault version
2033 len = strlen (name);
2034 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2035 if (shortname == NULL)
2037 memcpy (shortname, name, shortlen);
2038 memcpy (shortname + shortlen, p + 1, len - shortlen);
2040 /* Once again, merge with any existing symbol. */
2041 type_change_ok = FALSE;
2042 size_change_ok = FALSE;
2044 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2045 &hi, poldbfd, NULL, NULL, &skip, &override,
2046 &type_change_ok, &size_change_ok, &matched))
2054 /* Here SHORTNAME is a versioned name, so we don't expect to see
2055 the type of override we do in the case above unless it is
2056 overridden by a versioned definition. */
2057 if (hi->root.type != bfd_link_hash_defined
2058 && hi->root.type != bfd_link_hash_defweak)
2060 /* xgettext:c-format */
2061 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2067 if (! (_bfd_generic_link_add_one_symbol
2068 (info, abfd, shortname, BSF_INDIRECT,
2069 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2071 hi = (struct elf_link_hash_entry *) bh;
2073 /* If there is a duplicate definition somewhere, then HI may not
2074 point to an indirect symbol. We will have reported an error
2075 to the user in that case. */
2077 if (hi->root.type == bfd_link_hash_indirect)
2079 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2080 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2081 hi->dynamic_def |= h->dynamic_def;
2083 /* See if the new flags lead us to realize that the symbol
2089 if (! bfd_link_executable (info)
2095 if (hi->ref_regular)
2105 /* This routine is used to export all defined symbols into the dynamic
2106 symbol table. It is called via elf_link_hash_traverse. */
2109 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2111 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2113 /* Ignore indirect symbols. These are added by the versioning code. */
2114 if (h->root.type == bfd_link_hash_indirect)
2117 /* Ignore this if we won't export it. */
2118 if (!eif->info->export_dynamic && !h->dynamic)
2121 if (h->dynindx == -1
2122 && (h->def_regular || h->ref_regular)
2123 && ! bfd_hide_sym_by_version (eif->info->version_info,
2124 h->root.root.string))
2126 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2136 /* Look through the symbols which are defined in other shared
2137 libraries and referenced here. Update the list of version
2138 dependencies. This will be put into the .gnu.version_r section.
2139 This function is called via elf_link_hash_traverse. */
2142 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2145 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2146 Elf_Internal_Verneed *t;
2147 Elf_Internal_Vernaux *a;
2150 /* We only care about symbols defined in shared objects with version
2155 || h->verinfo.verdef == NULL
2156 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2157 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2160 /* See if we already know about this version. */
2161 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2165 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2168 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2169 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2175 /* This is a new version. Add it to tree we are building. */
2180 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2183 rinfo->failed = TRUE;
2187 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2188 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2189 elf_tdata (rinfo->info->output_bfd)->verref = t;
2193 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2196 rinfo->failed = TRUE;
2200 /* Note that we are copying a string pointer here, and testing it
2201 above. If bfd_elf_string_from_elf_section is ever changed to
2202 discard the string data when low in memory, this will have to be
2204 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2206 a->vna_flags = h->verinfo.verdef->vd_flags;
2207 a->vna_nextptr = t->vn_auxptr;
2209 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2212 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2219 /* Figure out appropriate versions for all the symbols. We may not
2220 have the version number script until we have read all of the input
2221 files, so until that point we don't know which symbols should be
2222 local. This function is called via elf_link_hash_traverse. */
2225 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2227 struct elf_info_failed *sinfo;
2228 struct bfd_link_info *info;
2229 const struct elf_backend_data *bed;
2230 struct elf_info_failed eif;
2233 sinfo = (struct elf_info_failed *) data;
2236 /* Fix the symbol flags. */
2239 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2242 sinfo->failed = TRUE;
2246 /* We only need version numbers for symbols defined in regular
2248 if (!h->def_regular)
2251 bed = get_elf_backend_data (info->output_bfd);
2252 p = strchr (h->root.root.string, ELF_VER_CHR);
2253 if (p != NULL && h->verinfo.vertree == NULL)
2255 struct bfd_elf_version_tree *t;
2258 if (*p == ELF_VER_CHR)
2261 /* If there is no version string, we can just return out. */
2265 /* Look for the version. If we find it, it is no longer weak. */
2266 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2268 if (strcmp (t->name, p) == 0)
2272 struct bfd_elf_version_expr *d;
2274 len = p - h->root.root.string;
2275 alc = (char *) bfd_malloc (len);
2278 sinfo->failed = TRUE;
2281 memcpy (alc, h->root.root.string, len - 1);
2282 alc[len - 1] = '\0';
2283 if (alc[len - 2] == ELF_VER_CHR)
2284 alc[len - 2] = '\0';
2286 h->verinfo.vertree = t;
2290 if (t->globals.list != NULL)
2291 d = (*t->match) (&t->globals, NULL, alc);
2293 /* See if there is anything to force this symbol to
2295 if (d == NULL && t->locals.list != NULL)
2297 d = (*t->match) (&t->locals, NULL, alc);
2300 && ! info->export_dynamic)
2301 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2309 /* If we are building an application, we need to create a
2310 version node for this version. */
2311 if (t == NULL && bfd_link_executable (info))
2313 struct bfd_elf_version_tree **pp;
2316 /* If we aren't going to export this symbol, we don't need
2317 to worry about it. */
2318 if (h->dynindx == -1)
2321 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2325 sinfo->failed = TRUE;
2330 t->name_indx = (unsigned int) -1;
2334 /* Don't count anonymous version tag. */
2335 if (sinfo->info->version_info != NULL
2336 && sinfo->info->version_info->vernum == 0)
2338 for (pp = &sinfo->info->version_info;
2342 t->vernum = version_index;
2346 h->verinfo.vertree = t;
2350 /* We could not find the version for a symbol when
2351 generating a shared archive. Return an error. */
2353 /* xgettext:c-format */
2354 (_("%pB: version node not found for symbol %s"),
2355 info->output_bfd, h->root.root.string);
2356 bfd_set_error (bfd_error_bad_value);
2357 sinfo->failed = TRUE;
2362 /* If we don't have a version for this symbol, see if we can find
2364 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2369 = bfd_find_version_for_sym (sinfo->info->version_info,
2370 h->root.root.string, &hide);
2371 if (h->verinfo.vertree != NULL && hide)
2372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2378 /* Read and swap the relocs from the section indicated by SHDR. This
2379 may be either a REL or a RELA section. The relocations are
2380 translated into RELA relocations and stored in INTERNAL_RELOCS,
2381 which should have already been allocated to contain enough space.
2382 The EXTERNAL_RELOCS are a buffer where the external form of the
2383 relocations should be stored.
2385 Returns FALSE if something goes wrong. */
2388 elf_link_read_relocs_from_section (bfd *abfd,
2390 Elf_Internal_Shdr *shdr,
2391 void *external_relocs,
2392 Elf_Internal_Rela *internal_relocs)
2394 const struct elf_backend_data *bed;
2395 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2396 const bfd_byte *erela;
2397 const bfd_byte *erelaend;
2398 Elf_Internal_Rela *irela;
2399 Elf_Internal_Shdr *symtab_hdr;
2402 /* Position ourselves at the start of the section. */
2403 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2406 /* Read the relocations. */
2407 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2410 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2411 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2413 bed = get_elf_backend_data (abfd);
2415 /* Convert the external relocations to the internal format. */
2416 if (shdr->sh_entsize == bed->s->sizeof_rel)
2417 swap_in = bed->s->swap_reloc_in;
2418 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2419 swap_in = bed->s->swap_reloca_in;
2422 bfd_set_error (bfd_error_wrong_format);
2426 erela = (const bfd_byte *) external_relocs;
2427 erelaend = erela + shdr->sh_size;
2428 irela = internal_relocs;
2429 while (erela < erelaend)
2433 (*swap_in) (abfd, erela, irela);
2434 r_symndx = ELF32_R_SYM (irela->r_info);
2435 if (bed->s->arch_size == 64)
2439 if ((size_t) r_symndx >= nsyms)
2442 /* xgettext:c-format */
2443 (_("%pB: bad reloc symbol index (%#Lx >= %#lx)"
2444 " for offset %#Lx in section `%pA'"),
2445 abfd, r_symndx, (unsigned long) nsyms,
2446 irela->r_offset, sec);
2447 bfd_set_error (bfd_error_bad_value);
2451 else if (r_symndx != STN_UNDEF)
2454 /* xgettext:c-format */
2455 (_("%pB: non-zero symbol index (%#Lx)"
2456 " for offset %#Lx in section `%pA'"
2457 " when the object file has no symbol table"),
2459 irela->r_offset, sec);
2460 bfd_set_error (bfd_error_bad_value);
2463 irela += bed->s->int_rels_per_ext_rel;
2464 erela += shdr->sh_entsize;
2470 /* Read and swap the relocs for a section O. They may have been
2471 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2472 not NULL, they are used as buffers to read into. They are known to
2473 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2474 the return value is allocated using either malloc or bfd_alloc,
2475 according to the KEEP_MEMORY argument. If O has two relocation
2476 sections (both REL and RELA relocations), then the REL_HDR
2477 relocations will appear first in INTERNAL_RELOCS, followed by the
2478 RELA_HDR relocations. */
2481 _bfd_elf_link_read_relocs (bfd *abfd,
2483 void *external_relocs,
2484 Elf_Internal_Rela *internal_relocs,
2485 bfd_boolean keep_memory)
2487 void *alloc1 = NULL;
2488 Elf_Internal_Rela *alloc2 = NULL;
2489 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2490 struct bfd_elf_section_data *esdo = elf_section_data (o);
2491 Elf_Internal_Rela *internal_rela_relocs;
2493 if (esdo->relocs != NULL)
2494 return esdo->relocs;
2496 if (o->reloc_count == 0)
2499 if (internal_relocs == NULL)
2503 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2505 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2507 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2508 if (internal_relocs == NULL)
2512 if (external_relocs == NULL)
2514 bfd_size_type size = 0;
2517 size += esdo->rel.hdr->sh_size;
2519 size += esdo->rela.hdr->sh_size;
2521 alloc1 = bfd_malloc (size);
2524 external_relocs = alloc1;
2527 internal_rela_relocs = internal_relocs;
2530 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2534 external_relocs = (((bfd_byte *) external_relocs)
2535 + esdo->rel.hdr->sh_size);
2536 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2537 * bed->s->int_rels_per_ext_rel);
2541 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2543 internal_rela_relocs)))
2546 /* Cache the results for next time, if we can. */
2548 esdo->relocs = internal_relocs;
2553 /* Don't free alloc2, since if it was allocated we are passing it
2554 back (under the name of internal_relocs). */
2556 return internal_relocs;
2564 bfd_release (abfd, alloc2);
2571 /* Compute the size of, and allocate space for, REL_HDR which is the
2572 section header for a section containing relocations for O. */
2575 _bfd_elf_link_size_reloc_section (bfd *abfd,
2576 struct bfd_elf_section_reloc_data *reldata)
2578 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2580 /* That allows us to calculate the size of the section. */
2581 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2583 /* The contents field must last into write_object_contents, so we
2584 allocate it with bfd_alloc rather than malloc. Also since we
2585 cannot be sure that the contents will actually be filled in,
2586 we zero the allocated space. */
2587 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2588 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2591 if (reldata->hashes == NULL && reldata->count)
2593 struct elf_link_hash_entry **p;
2595 p = ((struct elf_link_hash_entry **)
2596 bfd_zmalloc (reldata->count * sizeof (*p)));
2600 reldata->hashes = p;
2606 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2607 originated from the section given by INPUT_REL_HDR) to the
2611 _bfd_elf_link_output_relocs (bfd *output_bfd,
2612 asection *input_section,
2613 Elf_Internal_Shdr *input_rel_hdr,
2614 Elf_Internal_Rela *internal_relocs,
2615 struct elf_link_hash_entry **rel_hash
2618 Elf_Internal_Rela *irela;
2619 Elf_Internal_Rela *irelaend;
2621 struct bfd_elf_section_reloc_data *output_reldata;
2622 asection *output_section;
2623 const struct elf_backend_data *bed;
2624 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2625 struct bfd_elf_section_data *esdo;
2627 output_section = input_section->output_section;
2629 bed = get_elf_backend_data (output_bfd);
2630 esdo = elf_section_data (output_section);
2631 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2633 output_reldata = &esdo->rel;
2634 swap_out = bed->s->swap_reloc_out;
2636 else if (esdo->rela.hdr
2637 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2639 output_reldata = &esdo->rela;
2640 swap_out = bed->s->swap_reloca_out;
2645 /* xgettext:c-format */
2646 (_("%pB: relocation size mismatch in %pB section %pA"),
2647 output_bfd, input_section->owner, input_section);
2648 bfd_set_error (bfd_error_wrong_format);
2652 erel = output_reldata->hdr->contents;
2653 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2654 irela = internal_relocs;
2655 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2656 * bed->s->int_rels_per_ext_rel);
2657 while (irela < irelaend)
2659 (*swap_out) (output_bfd, irela, erel);
2660 irela += bed->s->int_rels_per_ext_rel;
2661 erel += input_rel_hdr->sh_entsize;
2664 /* Bump the counter, so that we know where to add the next set of
2666 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2671 /* Make weak undefined symbols in PIE dynamic. */
2674 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2675 struct elf_link_hash_entry *h)
2677 if (bfd_link_pie (info)
2679 && h->root.type == bfd_link_hash_undefweak)
2680 return bfd_elf_link_record_dynamic_symbol (info, h);
2685 /* Fix up the flags for a symbol. This handles various cases which
2686 can only be fixed after all the input files are seen. This is
2687 currently called by both adjust_dynamic_symbol and
2688 assign_sym_version, which is unnecessary but perhaps more robust in
2689 the face of future changes. */
2692 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2693 struct elf_info_failed *eif)
2695 const struct elf_backend_data *bed;
2697 /* If this symbol was mentioned in a non-ELF file, try to set
2698 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2699 permit a non-ELF file to correctly refer to a symbol defined in
2700 an ELF dynamic object. */
2703 while (h->root.type == bfd_link_hash_indirect)
2704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2706 if (h->root.type != bfd_link_hash_defined
2707 && h->root.type != bfd_link_hash_defweak)
2710 h->ref_regular_nonweak = 1;
2714 if (h->root.u.def.section->owner != NULL
2715 && (bfd_get_flavour (h->root.u.def.section->owner)
2716 == bfd_target_elf_flavour))
2719 h->ref_regular_nonweak = 1;
2725 if (h->dynindx == -1
2729 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2738 /* Unfortunately, NON_ELF is only correct if the symbol
2739 was first seen in a non-ELF file. Fortunately, if the symbol
2740 was first seen in an ELF file, we're probably OK unless the
2741 symbol was defined in a non-ELF file. Catch that case here.
2742 FIXME: We're still in trouble if the symbol was first seen in
2743 a dynamic object, and then later in a non-ELF regular object. */
2744 if ((h->root.type == bfd_link_hash_defined
2745 || h->root.type == bfd_link_hash_defweak)
2747 && (h->root.u.def.section->owner != NULL
2748 ? (bfd_get_flavour (h->root.u.def.section->owner)
2749 != bfd_target_elf_flavour)
2750 : (bfd_is_abs_section (h->root.u.def.section)
2751 && !h->def_dynamic)))
2755 /* Backend specific symbol fixup. */
2756 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2757 if (bed->elf_backend_fixup_symbol
2758 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2761 /* If this is a final link, and the symbol was defined as a common
2762 symbol in a regular object file, and there was no definition in
2763 any dynamic object, then the linker will have allocated space for
2764 the symbol in a common section but the DEF_REGULAR
2765 flag will not have been set. */
2766 if (h->root.type == bfd_link_hash_defined
2770 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2773 /* If a weak undefined symbol has non-default visibility, we also
2774 hide it from the dynamic linker. */
2775 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2776 && h->root.type == bfd_link_hash_undefweak)
2777 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2779 /* A hidden versioned symbol in executable should be forced local if
2780 it is is locally defined, not referenced by shared library and not
2782 else if (bfd_link_executable (eif->info)
2783 && h->versioned == versioned_hidden
2784 && !eif->info->export_dynamic
2788 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2790 /* If -Bsymbolic was used (which means to bind references to global
2791 symbols to the definition within the shared object), and this
2792 symbol was defined in a regular object, then it actually doesn't
2793 need a PLT entry. Likewise, if the symbol has non-default
2794 visibility. If the symbol has hidden or internal visibility, we
2795 will force it local. */
2796 else if (h->needs_plt
2797 && bfd_link_pic (eif->info)
2798 && is_elf_hash_table (eif->info->hash)
2799 && (SYMBOLIC_BIND (eif->info, h)
2800 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2803 bfd_boolean force_local;
2805 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2806 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2807 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2810 /* If this is a weak defined symbol in a dynamic object, and we know
2811 the real definition in the dynamic object, copy interesting flags
2812 over to the real definition. */
2813 if (h->is_weakalias)
2815 struct elf_link_hash_entry *def = weakdef (h);
2817 /* If the real definition is defined by a regular object file,
2818 don't do anything special. See the longer description in
2819 _bfd_elf_adjust_dynamic_symbol, below. */
2820 if (def->def_regular)
2823 while ((h = h->u.alias) != def)
2824 h->is_weakalias = 0;
2828 while (h->root.type == bfd_link_hash_indirect)
2829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2830 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2831 || h->root.type == bfd_link_hash_defweak);
2832 BFD_ASSERT (def->def_dynamic);
2833 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2834 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2841 /* Make the backend pick a good value for a dynamic symbol. This is
2842 called via elf_link_hash_traverse, and also calls itself
2846 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2848 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2849 struct elf_link_hash_table *htab;
2850 const struct elf_backend_data *bed;
2852 if (! is_elf_hash_table (eif->info->hash))
2855 /* Ignore indirect symbols. These are added by the versioning code. */
2856 if (h->root.type == bfd_link_hash_indirect)
2859 /* Fix the symbol flags. */
2860 if (! _bfd_elf_fix_symbol_flags (h, eif))
2863 htab = elf_hash_table (eif->info);
2864 bed = get_elf_backend_data (htab->dynobj);
2866 if (h->root.type == bfd_link_hash_undefweak)
2868 if (eif->info->dynamic_undefined_weak == 0)
2869 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2870 else if (eif->info->dynamic_undefined_weak > 0
2872 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2873 && !bfd_hide_sym_by_version (eif->info->version_info,
2874 h->root.root.string))
2876 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2884 /* If this symbol does not require a PLT entry, and it is not
2885 defined by a dynamic object, or is not referenced by a regular
2886 object, ignore it. We do have to handle a weak defined symbol,
2887 even if no regular object refers to it, if we decided to add it
2888 to the dynamic symbol table. FIXME: Do we normally need to worry
2889 about symbols which are defined by one dynamic object and
2890 referenced by another one? */
2892 && h->type != STT_GNU_IFUNC
2896 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2898 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2902 /* If we've already adjusted this symbol, don't do it again. This
2903 can happen via a recursive call. */
2904 if (h->dynamic_adjusted)
2907 /* Don't look at this symbol again. Note that we must set this
2908 after checking the above conditions, because we may look at a
2909 symbol once, decide not to do anything, and then get called
2910 recursively later after REF_REGULAR is set below. */
2911 h->dynamic_adjusted = 1;
2913 /* If this is a weak definition, and we know a real definition, and
2914 the real symbol is not itself defined by a regular object file,
2915 then get a good value for the real definition. We handle the
2916 real symbol first, for the convenience of the backend routine.
2918 Note that there is a confusing case here. If the real definition
2919 is defined by a regular object file, we don't get the real symbol
2920 from the dynamic object, but we do get the weak symbol. If the
2921 processor backend uses a COPY reloc, then if some routine in the
2922 dynamic object changes the real symbol, we will not see that
2923 change in the corresponding weak symbol. This is the way other
2924 ELF linkers work as well, and seems to be a result of the shared
2927 I will clarify this issue. Most SVR4 shared libraries define the
2928 variable _timezone and define timezone as a weak synonym. The
2929 tzset call changes _timezone. If you write
2930 extern int timezone;
2932 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2933 you might expect that, since timezone is a synonym for _timezone,
2934 the same number will print both times. However, if the processor
2935 backend uses a COPY reloc, then actually timezone will be copied
2936 into your process image, and, since you define _timezone
2937 yourself, _timezone will not. Thus timezone and _timezone will
2938 wind up at different memory locations. The tzset call will set
2939 _timezone, leaving timezone unchanged. */
2941 if (h->is_weakalias)
2943 struct elf_link_hash_entry *def = weakdef (h);
2945 /* If we get to this point, there is an implicit reference to
2946 the alias by a regular object file via the weak symbol H. */
2947 def->ref_regular = 1;
2949 /* Ensure that the backend adjust_dynamic_symbol function sees
2950 the strong alias before H by recursively calling ourselves. */
2951 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2955 /* If a symbol has no type and no size and does not require a PLT
2956 entry, then we are probably about to do the wrong thing here: we
2957 are probably going to create a COPY reloc for an empty object.
2958 This case can arise when a shared object is built with assembly
2959 code, and the assembly code fails to set the symbol type. */
2961 && h->type == STT_NOTYPE
2964 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2965 h->root.root.string);
2967 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2976 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2980 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2981 struct elf_link_hash_entry *h,
2984 unsigned int power_of_two;
2986 asection *sec = h->root.u.def.section;
2988 /* The section alignment of the definition is the maximum alignment
2989 requirement of symbols defined in the section. Since we don't
2990 know the symbol alignment requirement, we start with the
2991 maximum alignment and check low bits of the symbol address
2992 for the minimum alignment. */
2993 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2994 mask = ((bfd_vma) 1 << power_of_two) - 1;
2995 while ((h->root.u.def.value & mask) != 0)
3001 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3004 /* Adjust the section alignment if needed. */
3005 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3010 /* We make sure that the symbol will be aligned properly. */
3011 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3013 /* Define the symbol as being at this point in DYNBSS. */
3014 h->root.u.def.section = dynbss;
3015 h->root.u.def.value = dynbss->size;
3017 /* Increment the size of DYNBSS to make room for the symbol. */
3018 dynbss->size += h->size;
3020 /* No error if extern_protected_data is true. */
3021 if (h->protected_def
3022 && (!info->extern_protected_data
3023 || (info->extern_protected_data < 0
3024 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3025 info->callbacks->einfo
3026 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3027 h->root.root.string);
3032 /* Adjust all external symbols pointing into SEC_MERGE sections
3033 to reflect the object merging within the sections. */
3036 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3040 if ((h->root.type == bfd_link_hash_defined
3041 || h->root.type == bfd_link_hash_defweak)
3042 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3043 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3045 bfd *output_bfd = (bfd *) data;
3047 h->root.u.def.value =
3048 _bfd_merged_section_offset (output_bfd,
3049 &h->root.u.def.section,
3050 elf_section_data (sec)->sec_info,
3051 h->root.u.def.value);
3057 /* Returns false if the symbol referred to by H should be considered
3058 to resolve local to the current module, and true if it should be
3059 considered to bind dynamically. */
3062 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3063 struct bfd_link_info *info,
3064 bfd_boolean not_local_protected)
3066 bfd_boolean binding_stays_local_p;
3067 const struct elf_backend_data *bed;
3068 struct elf_link_hash_table *hash_table;
3073 while (h->root.type == bfd_link_hash_indirect
3074 || h->root.type == bfd_link_hash_warning)
3075 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3077 /* If it was forced local, then clearly it's not dynamic. */
3078 if (h->dynindx == -1)
3080 if (h->forced_local)
3083 /* Identify the cases where name binding rules say that a
3084 visible symbol resolves locally. */
3085 binding_stays_local_p = (bfd_link_executable (info)
3086 || SYMBOLIC_BIND (info, h));
3088 switch (ELF_ST_VISIBILITY (h->other))
3095 hash_table = elf_hash_table (info);
3096 if (!is_elf_hash_table (hash_table))
3099 bed = get_elf_backend_data (hash_table->dynobj);
3101 /* Proper resolution for function pointer equality may require
3102 that these symbols perhaps be resolved dynamically, even though
3103 we should be resolving them to the current module. */
3104 if (!not_local_protected || !bed->is_function_type (h->type))
3105 binding_stays_local_p = TRUE;
3112 /* If it isn't defined locally, then clearly it's dynamic. */
3113 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3116 /* Otherwise, the symbol is dynamic if binding rules don't tell
3117 us that it remains local. */
3118 return !binding_stays_local_p;
3121 /* Return true if the symbol referred to by H should be considered
3122 to resolve local to the current module, and false otherwise. Differs
3123 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3124 undefined symbols. The two functions are virtually identical except
3125 for the place where dynindx == -1 is tested. If that test is true,
3126 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3127 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3129 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3130 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3131 treatment of undefined weak symbols. For those that do not make
3132 undefined weak symbols dynamic, both functions may return false. */
3135 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3136 struct bfd_link_info *info,
3137 bfd_boolean local_protected)
3139 const struct elf_backend_data *bed;
3140 struct elf_link_hash_table *hash_table;
3142 /* If it's a local sym, of course we resolve locally. */
3146 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3147 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3148 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3151 /* Forced local symbols resolve locally. */
3152 if (h->forced_local)
3155 /* Common symbols that become definitions don't get the DEF_REGULAR
3156 flag set, so test it first, and don't bail out. */
3157 if (ELF_COMMON_DEF_P (h))
3159 /* If we don't have a definition in a regular file, then we can't
3160 resolve locally. The sym is either undefined or dynamic. */
3161 else if (!h->def_regular)
3164 /* Non-dynamic symbols resolve locally. */
3165 if (h->dynindx == -1)
3168 /* At this point, we know the symbol is defined and dynamic. In an
3169 executable it must resolve locally, likewise when building symbolic
3170 shared libraries. */
3171 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3174 /* Now deal with defined dynamic symbols in shared libraries. Ones
3175 with default visibility might not resolve locally. */
3176 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3179 hash_table = elf_hash_table (info);
3180 if (!is_elf_hash_table (hash_table))
3183 bed = get_elf_backend_data (hash_table->dynobj);
3185 /* If extern_protected_data is false, STV_PROTECTED non-function
3186 symbols are local. */
3187 if ((!info->extern_protected_data
3188 || (info->extern_protected_data < 0
3189 && !bed->extern_protected_data))
3190 && !bed->is_function_type (h->type))
3193 /* Function pointer equality tests may require that STV_PROTECTED
3194 symbols be treated as dynamic symbols. If the address of a
3195 function not defined in an executable is set to that function's
3196 plt entry in the executable, then the address of the function in
3197 a shared library must also be the plt entry in the executable. */
3198 return local_protected;
3201 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3202 aligned. Returns the first TLS output section. */
3204 struct bfd_section *
3205 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3207 struct bfd_section *sec, *tls;
3208 unsigned int align = 0;
3210 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3211 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3215 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3216 if (sec->alignment_power > align)
3217 align = sec->alignment_power;
3219 elf_hash_table (info)->tls_sec = tls;
3221 /* Ensure the alignment of the first section is the largest alignment,
3222 so that the tls segment starts aligned. */
3224 tls->alignment_power = align;
3229 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3231 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3232 Elf_Internal_Sym *sym)
3234 const struct elf_backend_data *bed;
3236 /* Local symbols do not count, but target specific ones might. */
3237 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3238 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3241 bed = get_elf_backend_data (abfd);
3242 /* Function symbols do not count. */
3243 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3246 /* If the section is undefined, then so is the symbol. */
3247 if (sym->st_shndx == SHN_UNDEF)
3250 /* If the symbol is defined in the common section, then
3251 it is a common definition and so does not count. */
3252 if (bed->common_definition (sym))
3255 /* If the symbol is in a target specific section then we
3256 must rely upon the backend to tell us what it is. */
3257 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3258 /* FIXME - this function is not coded yet:
3260 return _bfd_is_global_symbol_definition (abfd, sym);
3262 Instead for now assume that the definition is not global,
3263 Even if this is wrong, at least the linker will behave
3264 in the same way that it used to do. */
3270 /* Search the symbol table of the archive element of the archive ABFD
3271 whose archive map contains a mention of SYMDEF, and determine if
3272 the symbol is defined in this element. */
3274 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3276 Elf_Internal_Shdr * hdr;
3280 Elf_Internal_Sym *isymbuf;
3281 Elf_Internal_Sym *isym;
3282 Elf_Internal_Sym *isymend;
3285 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3289 if (! bfd_check_format (abfd, bfd_object))
3292 /* Select the appropriate symbol table. If we don't know if the
3293 object file is an IR object, give linker LTO plugin a chance to
3294 get the correct symbol table. */
3295 if (abfd->plugin_format == bfd_plugin_yes
3296 #if BFD_SUPPORTS_PLUGINS
3297 || (abfd->plugin_format == bfd_plugin_unknown
3298 && bfd_link_plugin_object_p (abfd))
3302 /* Use the IR symbol table if the object has been claimed by
3304 abfd = abfd->plugin_dummy_bfd;
3305 hdr = &elf_tdata (abfd)->symtab_hdr;
3307 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3308 hdr = &elf_tdata (abfd)->symtab_hdr;
3310 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3312 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3314 /* The sh_info field of the symtab header tells us where the
3315 external symbols start. We don't care about the local symbols. */
3316 if (elf_bad_symtab (abfd))
3318 extsymcount = symcount;
3323 extsymcount = symcount - hdr->sh_info;
3324 extsymoff = hdr->sh_info;
3327 if (extsymcount == 0)
3330 /* Read in the symbol table. */
3331 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3333 if (isymbuf == NULL)
3336 /* Scan the symbol table looking for SYMDEF. */
3338 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3342 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3347 if (strcmp (name, symdef->name) == 0)
3349 result = is_global_data_symbol_definition (abfd, isym);
3359 /* Add an entry to the .dynamic table. */
3362 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3366 struct elf_link_hash_table *hash_table;
3367 const struct elf_backend_data *bed;
3369 bfd_size_type newsize;
3370 bfd_byte *newcontents;
3371 Elf_Internal_Dyn dyn;
3373 hash_table = elf_hash_table (info);
3374 if (! is_elf_hash_table (hash_table))
3377 bed = get_elf_backend_data (hash_table->dynobj);
3378 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3379 BFD_ASSERT (s != NULL);
3381 newsize = s->size + bed->s->sizeof_dyn;
3382 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3383 if (newcontents == NULL)
3387 dyn.d_un.d_val = val;
3388 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3391 s->contents = newcontents;
3396 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3397 otherwise just check whether one already exists. Returns -1 on error,
3398 1 if a DT_NEEDED tag already exists, and 0 on success. */
3401 elf_add_dt_needed_tag (bfd *abfd,
3402 struct bfd_link_info *info,
3406 struct elf_link_hash_table *hash_table;
3409 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3412 hash_table = elf_hash_table (info);
3413 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3414 if (strindex == (size_t) -1)
3417 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3420 const struct elf_backend_data *bed;
3423 bed = get_elf_backend_data (hash_table->dynobj);
3424 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3426 for (extdyn = sdyn->contents;
3427 extdyn < sdyn->contents + sdyn->size;
3428 extdyn += bed->s->sizeof_dyn)
3430 Elf_Internal_Dyn dyn;
3432 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3433 if (dyn.d_tag == DT_NEEDED
3434 && dyn.d_un.d_val == strindex)
3436 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3444 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3447 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3451 /* We were just checking for existence of the tag. */
3452 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3457 /* Return true if SONAME is on the needed list between NEEDED and STOP
3458 (or the end of list if STOP is NULL), and needed by a library that
3462 on_needed_list (const char *soname,
3463 struct bfd_link_needed_list *needed,
3464 struct bfd_link_needed_list *stop)
3466 struct bfd_link_needed_list *look;
3467 for (look = needed; look != stop; look = look->next)
3468 if (strcmp (soname, look->name) == 0
3469 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3470 /* If needed by a library that itself is not directly
3471 needed, recursively check whether that library is
3472 indirectly needed. Since we add DT_NEEDED entries to
3473 the end of the list, library dependencies appear after
3474 the library. Therefore search prior to the current
3475 LOOK, preventing possible infinite recursion. */
3476 || on_needed_list (elf_dt_name (look->by), needed, look)))
3482 /* Sort symbol by value, section, and size. */
3484 elf_sort_symbol (const void *arg1, const void *arg2)
3486 const struct elf_link_hash_entry *h1;
3487 const struct elf_link_hash_entry *h2;
3488 bfd_signed_vma vdiff;
3490 h1 = *(const struct elf_link_hash_entry **) arg1;
3491 h2 = *(const struct elf_link_hash_entry **) arg2;
3492 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3494 return vdiff > 0 ? 1 : -1;
3497 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3499 return sdiff > 0 ? 1 : -1;
3501 vdiff = h1->size - h2->size;
3502 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3505 /* This function is used to adjust offsets into .dynstr for
3506 dynamic symbols. This is called via elf_link_hash_traverse. */
3509 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3511 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3513 if (h->dynindx != -1)
3514 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3518 /* Assign string offsets in .dynstr, update all structures referencing
3522 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3524 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3525 struct elf_link_local_dynamic_entry *entry;
3526 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3527 bfd *dynobj = hash_table->dynobj;
3530 const struct elf_backend_data *bed;
3533 _bfd_elf_strtab_finalize (dynstr);
3534 size = _bfd_elf_strtab_size (dynstr);
3536 bed = get_elf_backend_data (dynobj);
3537 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3538 BFD_ASSERT (sdyn != NULL);
3540 /* Update all .dynamic entries referencing .dynstr strings. */
3541 for (extdyn = sdyn->contents;
3542 extdyn < sdyn->contents + sdyn->size;
3543 extdyn += bed->s->sizeof_dyn)
3545 Elf_Internal_Dyn dyn;
3547 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3551 dyn.d_un.d_val = size;
3561 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3566 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3569 /* Now update local dynamic symbols. */
3570 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3571 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3572 entry->isym.st_name);
3574 /* And the rest of dynamic symbols. */
3575 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3577 /* Adjust version definitions. */
3578 if (elf_tdata (output_bfd)->cverdefs)
3583 Elf_Internal_Verdef def;
3584 Elf_Internal_Verdaux defaux;
3586 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3590 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3592 p += sizeof (Elf_External_Verdef);
3593 if (def.vd_aux != sizeof (Elf_External_Verdef))
3595 for (i = 0; i < def.vd_cnt; ++i)
3597 _bfd_elf_swap_verdaux_in (output_bfd,
3598 (Elf_External_Verdaux *) p, &defaux);
3599 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3601 _bfd_elf_swap_verdaux_out (output_bfd,
3602 &defaux, (Elf_External_Verdaux *) p);
3603 p += sizeof (Elf_External_Verdaux);
3606 while (def.vd_next);
3609 /* Adjust version references. */
3610 if (elf_tdata (output_bfd)->verref)
3615 Elf_Internal_Verneed need;
3616 Elf_Internal_Vernaux needaux;
3618 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3622 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3624 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3625 _bfd_elf_swap_verneed_out (output_bfd, &need,
3626 (Elf_External_Verneed *) p);
3627 p += sizeof (Elf_External_Verneed);
3628 for (i = 0; i < need.vn_cnt; ++i)
3630 _bfd_elf_swap_vernaux_in (output_bfd,
3631 (Elf_External_Vernaux *) p, &needaux);
3632 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3634 _bfd_elf_swap_vernaux_out (output_bfd,
3636 (Elf_External_Vernaux *) p);
3637 p += sizeof (Elf_External_Vernaux);
3640 while (need.vn_next);
3646 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3647 The default is to only match when the INPUT and OUTPUT are exactly
3651 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3652 const bfd_target *output)
3654 return input == output;
3657 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3658 This version is used when different targets for the same architecture
3659 are virtually identical. */
3662 _bfd_elf_relocs_compatible (const bfd_target *input,
3663 const bfd_target *output)
3665 const struct elf_backend_data *obed, *ibed;
3667 if (input == output)
3670 ibed = xvec_get_elf_backend_data (input);
3671 obed = xvec_get_elf_backend_data (output);
3673 if (ibed->arch != obed->arch)
3676 /* If both backends are using this function, deem them compatible. */
3677 return ibed->relocs_compatible == obed->relocs_compatible;
3680 /* Make a special call to the linker "notice" function to tell it that
3681 we are about to handle an as-needed lib, or have finished
3682 processing the lib. */
3685 _bfd_elf_notice_as_needed (bfd *ibfd,
3686 struct bfd_link_info *info,
3687 enum notice_asneeded_action act)
3689 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3692 /* Check relocations an ELF object file. */
3695 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3697 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3698 struct elf_link_hash_table *htab = elf_hash_table (info);
3700 /* If this object is the same format as the output object, and it is
3701 not a shared library, then let the backend look through the
3704 This is required to build global offset table entries and to
3705 arrange for dynamic relocs. It is not required for the
3706 particular common case of linking non PIC code, even when linking
3707 against shared libraries, but unfortunately there is no way of
3708 knowing whether an object file has been compiled PIC or not.
3709 Looking through the relocs is not particularly time consuming.
3710 The problem is that we must either (1) keep the relocs in memory,
3711 which causes the linker to require additional runtime memory or
3712 (2) read the relocs twice from the input file, which wastes time.
3713 This would be a good case for using mmap.
3715 I have no idea how to handle linking PIC code into a file of a
3716 different format. It probably can't be done. */
3717 if ((abfd->flags & DYNAMIC) == 0
3718 && is_elf_hash_table (htab)
3719 && bed->check_relocs != NULL
3720 && elf_object_id (abfd) == elf_hash_table_id (htab)
3721 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3725 for (o = abfd->sections; o != NULL; o = o->next)
3727 Elf_Internal_Rela *internal_relocs;
3730 /* Don't check relocations in excluded sections. */
3731 if ((o->flags & SEC_RELOC) == 0
3732 || (o->flags & SEC_EXCLUDE) != 0
3733 || o->reloc_count == 0
3734 || ((info->strip == strip_all || info->strip == strip_debugger)
3735 && (o->flags & SEC_DEBUGGING) != 0)
3736 || bfd_is_abs_section (o->output_section))
3739 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3741 if (internal_relocs == NULL)
3744 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3746 if (elf_section_data (o)->relocs != internal_relocs)
3747 free (internal_relocs);
3757 /* Add symbols from an ELF object file to the linker hash table. */
3760 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3762 Elf_Internal_Ehdr *ehdr;
3763 Elf_Internal_Shdr *hdr;
3767 struct elf_link_hash_entry **sym_hash;
3768 bfd_boolean dynamic;
3769 Elf_External_Versym *extversym = NULL;
3770 Elf_External_Versym *ever;
3771 struct elf_link_hash_entry *weaks;
3772 struct elf_link_hash_entry **nondeflt_vers = NULL;
3773 size_t nondeflt_vers_cnt = 0;
3774 Elf_Internal_Sym *isymbuf = NULL;
3775 Elf_Internal_Sym *isym;
3776 Elf_Internal_Sym *isymend;
3777 const struct elf_backend_data *bed;
3778 bfd_boolean add_needed;
3779 struct elf_link_hash_table *htab;
3781 void *alloc_mark = NULL;
3782 struct bfd_hash_entry **old_table = NULL;
3783 unsigned int old_size = 0;
3784 unsigned int old_count = 0;
3785 void *old_tab = NULL;
3787 struct bfd_link_hash_entry *old_undefs = NULL;
3788 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3789 void *old_strtab = NULL;
3792 bfd_boolean just_syms;
3794 htab = elf_hash_table (info);
3795 bed = get_elf_backend_data (abfd);
3797 if ((abfd->flags & DYNAMIC) == 0)
3803 /* You can't use -r against a dynamic object. Also, there's no
3804 hope of using a dynamic object which does not exactly match
3805 the format of the output file. */
3806 if (bfd_link_relocatable (info)
3807 || !is_elf_hash_table (htab)
3808 || info->output_bfd->xvec != abfd->xvec)
3810 if (bfd_link_relocatable (info))
3811 bfd_set_error (bfd_error_invalid_operation);
3813 bfd_set_error (bfd_error_wrong_format);
3818 ehdr = elf_elfheader (abfd);
3819 if (info->warn_alternate_em
3820 && bed->elf_machine_code != ehdr->e_machine
3821 && ((bed->elf_machine_alt1 != 0
3822 && ehdr->e_machine == bed->elf_machine_alt1)
3823 || (bed->elf_machine_alt2 != 0
3824 && ehdr->e_machine == bed->elf_machine_alt2)))
3825 info->callbacks->einfo
3826 /* xgettext:c-format */
3827 (_("%P: alternate ELF machine code found (%d) in %pB, expecting %d\n"),
3828 ehdr->e_machine, abfd, bed->elf_machine_code);
3830 /* As a GNU extension, any input sections which are named
3831 .gnu.warning.SYMBOL are treated as warning symbols for the given
3832 symbol. This differs from .gnu.warning sections, which generate
3833 warnings when they are included in an output file. */
3834 /* PR 12761: Also generate this warning when building shared libraries. */
3835 for (s = abfd->sections; s != NULL; s = s->next)
3839 name = bfd_get_section_name (abfd, s);
3840 if (CONST_STRNEQ (name, ".gnu.warning."))
3845 name += sizeof ".gnu.warning." - 1;
3847 /* If this is a shared object, then look up the symbol
3848 in the hash table. If it is there, and it is already
3849 been defined, then we will not be using the entry
3850 from this shared object, so we don't need to warn.
3851 FIXME: If we see the definition in a regular object
3852 later on, we will warn, but we shouldn't. The only
3853 fix is to keep track of what warnings we are supposed
3854 to emit, and then handle them all at the end of the
3858 struct elf_link_hash_entry *h;
3860 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3862 /* FIXME: What about bfd_link_hash_common? */
3864 && (h->root.type == bfd_link_hash_defined
3865 || h->root.type == bfd_link_hash_defweak))
3870 msg = (char *) bfd_alloc (abfd, sz + 1);
3874 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3879 if (! (_bfd_generic_link_add_one_symbol
3880 (info, abfd, name, BSF_WARNING, s, 0, msg,
3881 FALSE, bed->collect, NULL)))
3884 if (bfd_link_executable (info))
3886 /* Clobber the section size so that the warning does
3887 not get copied into the output file. */
3890 /* Also set SEC_EXCLUDE, so that symbols defined in
3891 the warning section don't get copied to the output. */
3892 s->flags |= SEC_EXCLUDE;
3897 just_syms = ((s = abfd->sections) != NULL
3898 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3903 /* If we are creating a shared library, create all the dynamic
3904 sections immediately. We need to attach them to something,
3905 so we attach them to this BFD, provided it is the right
3906 format and is not from ld --just-symbols. Always create the
3907 dynamic sections for -E/--dynamic-list. FIXME: If there
3908 are no input BFD's of the same format as the output, we can't
3909 make a shared library. */
3911 && (bfd_link_pic (info)
3912 || (!bfd_link_relocatable (info)
3914 && (info->export_dynamic || info->dynamic)))
3915 && is_elf_hash_table (htab)
3916 && info->output_bfd->xvec == abfd->xvec
3917 && !htab->dynamic_sections_created)
3919 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3923 else if (!is_elf_hash_table (htab))
3927 const char *soname = NULL;
3929 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3930 const Elf_Internal_Phdr *phdr;
3933 /* ld --just-symbols and dynamic objects don't mix very well.
3934 ld shouldn't allow it. */
3938 /* If this dynamic lib was specified on the command line with
3939 --as-needed in effect, then we don't want to add a DT_NEEDED
3940 tag unless the lib is actually used. Similary for libs brought
3941 in by another lib's DT_NEEDED. When --no-add-needed is used
3942 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3943 any dynamic library in DT_NEEDED tags in the dynamic lib at
3945 add_needed = (elf_dyn_lib_class (abfd)
3946 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3947 | DYN_NO_NEEDED)) == 0;
3949 s = bfd_get_section_by_name (abfd, ".dynamic");
3954 unsigned int elfsec;
3955 unsigned long shlink;
3957 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3964 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3965 if (elfsec == SHN_BAD)
3966 goto error_free_dyn;
3967 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3969 for (extdyn = dynbuf;
3970 extdyn < dynbuf + s->size;
3971 extdyn += bed->s->sizeof_dyn)
3973 Elf_Internal_Dyn dyn;
3975 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3976 if (dyn.d_tag == DT_SONAME)
3978 unsigned int tagv = dyn.d_un.d_val;
3979 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3981 goto error_free_dyn;
3983 if (dyn.d_tag == DT_NEEDED)
3985 struct bfd_link_needed_list *n, **pn;
3987 unsigned int tagv = dyn.d_un.d_val;
3989 amt = sizeof (struct bfd_link_needed_list);
3990 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3991 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3992 if (n == NULL || fnm == NULL)
3993 goto error_free_dyn;
3994 amt = strlen (fnm) + 1;
3995 anm = (char *) bfd_alloc (abfd, amt);
3997 goto error_free_dyn;
3998 memcpy (anm, fnm, amt);
4002 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4006 if (dyn.d_tag == DT_RUNPATH)
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);
4025 for (pn = & runpath;
4031 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4032 if (!runpath && dyn.d_tag == DT_RPATH)
4034 struct bfd_link_needed_list *n, **pn;
4036 unsigned int tagv = dyn.d_un.d_val;
4038 amt = sizeof (struct bfd_link_needed_list);
4039 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4040 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4041 if (n == NULL || fnm == NULL)
4042 goto error_free_dyn;
4043 amt = strlen (fnm) + 1;
4044 anm = (char *) bfd_alloc (abfd, amt);
4046 goto error_free_dyn;
4047 memcpy (anm, fnm, amt);
4057 if (dyn.d_tag == DT_AUDIT)
4059 unsigned int tagv = dyn.d_un.d_val;
4060 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4067 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4068 frees all more recently bfd_alloc'd blocks as well. */
4074 struct bfd_link_needed_list **pn;
4075 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4080 /* If we have a PT_GNU_RELRO program header, mark as read-only
4081 all sections contained fully therein. This makes relro
4082 shared library sections appear as they will at run-time. */
4083 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4084 while (--phdr >= elf_tdata (abfd)->phdr)
4085 if (phdr->p_type == PT_GNU_RELRO)
4087 for (s = abfd->sections; s != NULL; s = s->next)
4088 if ((s->flags & SEC_ALLOC) != 0
4089 && s->vma >= phdr->p_vaddr
4090 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4091 s->flags |= SEC_READONLY;
4095 /* We do not want to include any of the sections in a dynamic
4096 object in the output file. We hack by simply clobbering the
4097 list of sections in the BFD. This could be handled more
4098 cleanly by, say, a new section flag; the existing
4099 SEC_NEVER_LOAD flag is not the one we want, because that one
4100 still implies that the section takes up space in the output
4102 bfd_section_list_clear (abfd);
4104 /* Find the name to use in a DT_NEEDED entry that refers to this
4105 object. If the object has a DT_SONAME entry, we use it.
4106 Otherwise, if the generic linker stuck something in
4107 elf_dt_name, we use that. Otherwise, we just use the file
4109 if (soname == NULL || *soname == '\0')
4111 soname = elf_dt_name (abfd);
4112 if (soname == NULL || *soname == '\0')
4113 soname = bfd_get_filename (abfd);
4116 /* Save the SONAME because sometimes the linker emulation code
4117 will need to know it. */
4118 elf_dt_name (abfd) = soname;
4120 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4124 /* If we have already included this dynamic object in the
4125 link, just ignore it. There is no reason to include a
4126 particular dynamic object more than once. */
4130 /* Save the DT_AUDIT entry for the linker emulation code. */
4131 elf_dt_audit (abfd) = audit;
4134 /* If this is a dynamic object, we always link against the .dynsym
4135 symbol table, not the .symtab symbol table. The dynamic linker
4136 will only see the .dynsym symbol table, so there is no reason to
4137 look at .symtab for a dynamic object. */
4139 if (! dynamic || elf_dynsymtab (abfd) == 0)
4140 hdr = &elf_tdata (abfd)->symtab_hdr;
4142 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4144 symcount = hdr->sh_size / bed->s->sizeof_sym;
4146 /* The sh_info field of the symtab header tells us where the
4147 external symbols start. We don't care about the local symbols at
4149 if (elf_bad_symtab (abfd))
4151 extsymcount = symcount;
4156 extsymcount = symcount - hdr->sh_info;
4157 extsymoff = hdr->sh_info;
4160 sym_hash = elf_sym_hashes (abfd);
4161 if (extsymcount != 0)
4163 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4165 if (isymbuf == NULL)
4168 if (sym_hash == NULL)
4170 /* We store a pointer to the hash table entry for each
4173 amt *= sizeof (struct elf_link_hash_entry *);
4174 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4175 if (sym_hash == NULL)
4176 goto error_free_sym;
4177 elf_sym_hashes (abfd) = sym_hash;
4183 /* Read in any version definitions. */
4184 if (!_bfd_elf_slurp_version_tables (abfd,
4185 info->default_imported_symver))
4186 goto error_free_sym;
4188 /* Read in the symbol versions, but don't bother to convert them
4189 to internal format. */
4190 if (elf_dynversym (abfd) != 0)
4192 Elf_Internal_Shdr *versymhdr;
4194 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4195 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4196 if (extversym == NULL)
4197 goto error_free_sym;
4198 amt = versymhdr->sh_size;
4199 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4200 || bfd_bread (extversym, amt, abfd) != amt)
4201 goto error_free_vers;
4205 /* If we are loading an as-needed shared lib, save the symbol table
4206 state before we start adding symbols. If the lib turns out
4207 to be unneeded, restore the state. */
4208 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4213 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4215 struct bfd_hash_entry *p;
4216 struct elf_link_hash_entry *h;
4218 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4220 h = (struct elf_link_hash_entry *) p;
4221 entsize += htab->root.table.entsize;
4222 if (h->root.type == bfd_link_hash_warning)
4223 entsize += htab->root.table.entsize;
4227 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4228 old_tab = bfd_malloc (tabsize + entsize);
4229 if (old_tab == NULL)
4230 goto error_free_vers;
4232 /* Remember the current objalloc pointer, so that all mem for
4233 symbols added can later be reclaimed. */
4234 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4235 if (alloc_mark == NULL)
4236 goto error_free_vers;
4238 /* Make a special call to the linker "notice" function to
4239 tell it that we are about to handle an as-needed lib. */
4240 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4241 goto error_free_vers;
4243 /* Clone the symbol table. Remember some pointers into the
4244 symbol table, and dynamic symbol count. */
4245 old_ent = (char *) old_tab + tabsize;
4246 memcpy (old_tab, htab->root.table.table, tabsize);
4247 old_undefs = htab->root.undefs;
4248 old_undefs_tail = htab->root.undefs_tail;
4249 old_table = htab->root.table.table;
4250 old_size = htab->root.table.size;
4251 old_count = htab->root.table.count;
4252 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4253 if (old_strtab == NULL)
4254 goto error_free_vers;
4256 for (i = 0; i < htab->root.table.size; i++)
4258 struct bfd_hash_entry *p;
4259 struct elf_link_hash_entry *h;
4261 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4263 memcpy (old_ent, p, htab->root.table.entsize);
4264 old_ent = (char *) old_ent + htab->root.table.entsize;
4265 h = (struct elf_link_hash_entry *) p;
4266 if (h->root.type == bfd_link_hash_warning)
4268 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4269 old_ent = (char *) old_ent + htab->root.table.entsize;
4276 ever = extversym != NULL ? extversym + extsymoff : NULL;
4277 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4279 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4283 asection *sec, *new_sec;
4286 struct elf_link_hash_entry *h;
4287 struct elf_link_hash_entry *hi;
4288 bfd_boolean definition;
4289 bfd_boolean size_change_ok;
4290 bfd_boolean type_change_ok;
4291 bfd_boolean new_weak;
4292 bfd_boolean old_weak;
4293 bfd_boolean override;
4295 bfd_boolean discarded;
4296 unsigned int old_alignment;
4298 bfd_boolean matched;
4302 flags = BSF_NO_FLAGS;
4304 value = isym->st_value;
4305 common = bed->common_definition (isym);
4306 if (common && info->inhibit_common_definition)
4308 /* Treat common symbol as undefined for --no-define-common. */
4309 isym->st_shndx = SHN_UNDEF;
4314 bind = ELF_ST_BIND (isym->st_info);
4318 /* This should be impossible, since ELF requires that all
4319 global symbols follow all local symbols, and that sh_info
4320 point to the first global symbol. Unfortunately, Irix 5
4325 if (isym->st_shndx != SHN_UNDEF && !common)
4333 case STB_GNU_UNIQUE:
4334 flags = BSF_GNU_UNIQUE;
4338 /* Leave it up to the processor backend. */
4342 if (isym->st_shndx == SHN_UNDEF)
4343 sec = bfd_und_section_ptr;
4344 else if (isym->st_shndx == SHN_ABS)
4345 sec = bfd_abs_section_ptr;
4346 else if (isym->st_shndx == SHN_COMMON)
4348 sec = bfd_com_section_ptr;
4349 /* What ELF calls the size we call the value. What ELF
4350 calls the value we call the alignment. */
4351 value = isym->st_size;
4355 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4357 sec = bfd_abs_section_ptr;
4358 else if (discarded_section (sec))
4360 /* Symbols from discarded section are undefined. We keep
4362 sec = bfd_und_section_ptr;
4364 isym->st_shndx = SHN_UNDEF;
4366 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4370 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4373 goto error_free_vers;
4375 if (isym->st_shndx == SHN_COMMON
4376 && (abfd->flags & BFD_PLUGIN) != 0)
4378 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4382 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4384 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4386 goto error_free_vers;
4390 else if (isym->st_shndx == SHN_COMMON
4391 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4392 && !bfd_link_relocatable (info))
4394 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4398 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4399 | SEC_LINKER_CREATED);
4400 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4402 goto error_free_vers;
4406 else if (bed->elf_add_symbol_hook)
4408 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4410 goto error_free_vers;
4412 /* The hook function sets the name to NULL if this symbol
4413 should be skipped for some reason. */
4418 /* Sanity check that all possibilities were handled. */
4421 bfd_set_error (bfd_error_bad_value);
4422 goto error_free_vers;
4425 /* Silently discard TLS symbols from --just-syms. There's
4426 no way to combine a static TLS block with a new TLS block
4427 for this executable. */
4428 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4429 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4432 if (bfd_is_und_section (sec)
4433 || bfd_is_com_section (sec))
4438 size_change_ok = FALSE;
4439 type_change_ok = bed->type_change_ok;
4446 if (is_elf_hash_table (htab))
4448 Elf_Internal_Versym iver;
4449 unsigned int vernum = 0;
4454 if (info->default_imported_symver)
4455 /* Use the default symbol version created earlier. */
4456 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4461 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4463 vernum = iver.vs_vers & VERSYM_VERSION;
4465 /* If this is a hidden symbol, or if it is not version
4466 1, we append the version name to the symbol name.
4467 However, we do not modify a non-hidden absolute symbol
4468 if it is not a function, because it might be the version
4469 symbol itself. FIXME: What if it isn't? */
4470 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4472 && (!bfd_is_abs_section (sec)
4473 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4476 size_t namelen, verlen, newlen;
4479 if (isym->st_shndx != SHN_UNDEF)
4481 if (vernum > elf_tdata (abfd)->cverdefs)
4483 else if (vernum > 1)
4485 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4492 /* xgettext:c-format */
4493 (_("%pB: %s: invalid version %u (max %d)"),
4495 elf_tdata (abfd)->cverdefs);
4496 bfd_set_error (bfd_error_bad_value);
4497 goto error_free_vers;
4502 /* We cannot simply test for the number of
4503 entries in the VERNEED section since the
4504 numbers for the needed versions do not start
4506 Elf_Internal_Verneed *t;
4509 for (t = elf_tdata (abfd)->verref;
4513 Elf_Internal_Vernaux *a;
4515 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4517 if (a->vna_other == vernum)
4519 verstr = a->vna_nodename;
4529 /* xgettext:c-format */
4530 (_("%pB: %s: invalid needed version %d"),
4531 abfd, name, vernum);
4532 bfd_set_error (bfd_error_bad_value);
4533 goto error_free_vers;
4537 namelen = strlen (name);
4538 verlen = strlen (verstr);
4539 newlen = namelen + verlen + 2;
4540 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4541 && isym->st_shndx != SHN_UNDEF)
4544 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4545 if (newname == NULL)
4546 goto error_free_vers;
4547 memcpy (newname, name, namelen);
4548 p = newname + namelen;
4550 /* If this is a defined non-hidden version symbol,
4551 we add another @ to the name. This indicates the
4552 default version of the symbol. */
4553 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4554 && isym->st_shndx != SHN_UNDEF)
4556 memcpy (p, verstr, verlen + 1);
4561 /* If this symbol has default visibility and the user has
4562 requested we not re-export it, then mark it as hidden. */
4563 if (!bfd_is_und_section (sec)
4566 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4567 isym->st_other = (STV_HIDDEN
4568 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4570 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4571 sym_hash, &old_bfd, &old_weak,
4572 &old_alignment, &skip, &override,
4573 &type_change_ok, &size_change_ok,
4575 goto error_free_vers;
4580 /* Override a definition only if the new symbol matches the
4582 if (override && matched)
4586 while (h->root.type == bfd_link_hash_indirect
4587 || h->root.type == bfd_link_hash_warning)
4588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4590 if (elf_tdata (abfd)->verdef != NULL
4593 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4596 if (! (_bfd_generic_link_add_one_symbol
4597 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4598 (struct bfd_link_hash_entry **) sym_hash)))
4599 goto error_free_vers;
4601 if ((flags & BSF_GNU_UNIQUE)
4602 && (abfd->flags & DYNAMIC) == 0
4603 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4604 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4607 /* We need to make sure that indirect symbol dynamic flags are
4610 while (h->root.type == bfd_link_hash_indirect
4611 || h->root.type == bfd_link_hash_warning)
4612 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4614 /* Setting the index to -3 tells elf_link_output_extsym that
4615 this symbol is defined in a discarded section. */
4621 new_weak = (flags & BSF_WEAK) != 0;
4625 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4626 && is_elf_hash_table (htab)
4627 && h->u.alias == NULL)
4629 /* Keep a list of all weak defined non function symbols from
4630 a dynamic object, using the alias field. Later in this
4631 function we will set the alias field to the correct
4632 value. We only put non-function symbols from dynamic
4633 objects on this list, because that happens to be the only
4634 time we need to know the normal symbol corresponding to a
4635 weak symbol, and the information is time consuming to
4636 figure out. If the alias field is not already NULL,
4637 then this symbol was already defined by some previous
4638 dynamic object, and we will be using that previous
4639 definition anyhow. */
4645 /* Set the alignment of a common symbol. */
4646 if ((common || bfd_is_com_section (sec))
4647 && h->root.type == bfd_link_hash_common)
4652 align = bfd_log2 (isym->st_value);
4655 /* The new symbol is a common symbol in a shared object.
4656 We need to get the alignment from the section. */
4657 align = new_sec->alignment_power;
4659 if (align > old_alignment)
4660 h->root.u.c.p->alignment_power = align;
4662 h->root.u.c.p->alignment_power = old_alignment;
4665 if (is_elf_hash_table (htab))
4667 /* Set a flag in the hash table entry indicating the type of
4668 reference or definition we just found. A dynamic symbol
4669 is one which is referenced or defined by both a regular
4670 object and a shared object. */
4671 bfd_boolean dynsym = FALSE;
4673 /* Plugin symbols aren't normal. Don't set def_regular or
4674 ref_regular for them, or make them dynamic. */
4675 if ((abfd->flags & BFD_PLUGIN) != 0)
4682 if (bind != STB_WEAK)
4683 h->ref_regular_nonweak = 1;
4695 /* If the indirect symbol has been forced local, don't
4696 make the real symbol dynamic. */
4697 if ((h == hi || !hi->forced_local)
4698 && (bfd_link_dll (info)
4708 hi->ref_dynamic = 1;
4713 hi->def_dynamic = 1;
4716 /* If the indirect symbol has been forced local, don't
4717 make the real symbol dynamic. */
4718 if ((h == hi || !hi->forced_local)
4722 && weakdef (h)->dynindx != -1)))
4726 /* Check to see if we need to add an indirect symbol for
4727 the default name. */
4729 || (!override && h->root.type == bfd_link_hash_common))
4730 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4731 sec, value, &old_bfd, &dynsym))
4732 goto error_free_vers;
4734 /* Check the alignment when a common symbol is involved. This
4735 can change when a common symbol is overridden by a normal
4736 definition or a common symbol is ignored due to the old
4737 normal definition. We need to make sure the maximum
4738 alignment is maintained. */
4739 if ((old_alignment || common)
4740 && h->root.type != bfd_link_hash_common)
4742 unsigned int common_align;
4743 unsigned int normal_align;
4744 unsigned int symbol_align;
4748 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4749 || h->root.type == bfd_link_hash_defweak);
4751 symbol_align = ffs (h->root.u.def.value) - 1;
4752 if (h->root.u.def.section->owner != NULL
4753 && (h->root.u.def.section->owner->flags
4754 & (DYNAMIC | BFD_PLUGIN)) == 0)
4756 normal_align = h->root.u.def.section->alignment_power;
4757 if (normal_align > symbol_align)
4758 normal_align = symbol_align;
4761 normal_align = symbol_align;
4765 common_align = old_alignment;
4766 common_bfd = old_bfd;
4771 common_align = bfd_log2 (isym->st_value);
4773 normal_bfd = old_bfd;
4776 if (normal_align < common_align)
4778 /* PR binutils/2735 */
4779 if (normal_bfd == NULL)
4781 /* xgettext:c-format */
4782 (_("Warning: alignment %u of common symbol `%s' in %pB is"
4783 " greater than the alignment (%u) of its section %pA"),
4784 1 << common_align, name, common_bfd,
4785 1 << normal_align, h->root.u.def.section);
4788 /* xgettext:c-format */
4789 (_("Warning: alignment %u of symbol `%s' in %pB"
4790 " is smaller than %u in %pB"),
4791 1 << normal_align, name, normal_bfd,
4792 1 << common_align, common_bfd);
4796 /* Remember the symbol size if it isn't undefined. */
4797 if (isym->st_size != 0
4798 && isym->st_shndx != SHN_UNDEF
4799 && (definition || h->size == 0))
4802 && h->size != isym->st_size
4803 && ! size_change_ok)
4805 /* xgettext:c-format */
4806 (_("Warning: size of symbol `%s' changed"
4807 " from %Lu in %pB to %Lu in %pB"),
4808 name, h->size, old_bfd, isym->st_size, abfd);
4810 h->size = isym->st_size;
4813 /* If this is a common symbol, then we always want H->SIZE
4814 to be the size of the common symbol. The code just above
4815 won't fix the size if a common symbol becomes larger. We
4816 don't warn about a size change here, because that is
4817 covered by --warn-common. Allow changes between different
4819 if (h->root.type == bfd_link_hash_common)
4820 h->size = h->root.u.c.size;
4822 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4823 && ((definition && !new_weak)
4824 || (old_weak && h->root.type == bfd_link_hash_common)
4825 || h->type == STT_NOTYPE))
4827 unsigned int type = ELF_ST_TYPE (isym->st_info);
4829 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4831 if (type == STT_GNU_IFUNC
4832 && (abfd->flags & DYNAMIC) != 0)
4835 if (h->type != type)
4837 if (h->type != STT_NOTYPE && ! type_change_ok)
4838 /* xgettext:c-format */
4840 (_("Warning: type of symbol `%s' changed"
4841 " from %d to %d in %pB"),
4842 name, h->type, type, abfd);
4848 /* Merge st_other field. */
4849 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4851 /* We don't want to make debug symbol dynamic. */
4853 && (sec->flags & SEC_DEBUGGING)
4854 && !bfd_link_relocatable (info))
4857 /* Nor should we make plugin symbols dynamic. */
4858 if ((abfd->flags & BFD_PLUGIN) != 0)
4863 h->target_internal = isym->st_target_internal;
4864 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4867 if (definition && !dynamic)
4869 char *p = strchr (name, ELF_VER_CHR);
4870 if (p != NULL && p[1] != ELF_VER_CHR)
4872 /* Queue non-default versions so that .symver x, x@FOO
4873 aliases can be checked. */
4876 amt = ((isymend - isym + 1)
4877 * sizeof (struct elf_link_hash_entry *));
4879 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4881 goto error_free_vers;
4883 nondeflt_vers[nondeflt_vers_cnt++] = h;
4887 if (dynsym && h->dynindx == -1)
4889 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4890 goto error_free_vers;
4892 && weakdef (h)->dynindx == -1)
4894 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4895 goto error_free_vers;
4898 else if (h->dynindx != -1)
4899 /* If the symbol already has a dynamic index, but
4900 visibility says it should not be visible, turn it into
4902 switch (ELF_ST_VISIBILITY (h->other))
4906 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4911 /* Don't add DT_NEEDED for references from the dummy bfd nor
4912 for unmatched symbol. */
4917 && h->ref_regular_nonweak
4919 || (old_bfd->flags & BFD_PLUGIN) == 0))
4920 || (h->ref_dynamic_nonweak
4921 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4922 && !on_needed_list (elf_dt_name (abfd),
4923 htab->needed, NULL))))
4926 const char *soname = elf_dt_name (abfd);
4928 info->callbacks->minfo ("%!", soname, old_bfd,
4929 h->root.root.string);
4931 /* A symbol from a library loaded via DT_NEEDED of some
4932 other library is referenced by a regular object.
4933 Add a DT_NEEDED entry for it. Issue an error if
4934 --no-add-needed is used and the reference was not
4937 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4940 /* xgettext:c-format */
4941 (_("%pB: undefined reference to symbol '%s'"),
4943 bfd_set_error (bfd_error_missing_dso);
4944 goto error_free_vers;
4947 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4948 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4951 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4953 goto error_free_vers;
4955 BFD_ASSERT (ret == 0);
4960 if (info->lto_plugin_active
4961 && !bfd_link_relocatable (info)
4962 && (abfd->flags & BFD_PLUGIN) == 0
4968 if (bed->s->arch_size == 32)
4973 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4974 referenced in regular objects so that linker plugin will get
4975 the correct symbol resolution. */
4977 sym_hash = elf_sym_hashes (abfd);
4978 for (s = abfd->sections; s != NULL; s = s->next)
4980 Elf_Internal_Rela *internal_relocs;
4981 Elf_Internal_Rela *rel, *relend;
4983 /* Don't check relocations in excluded sections. */
4984 if ((s->flags & SEC_RELOC) == 0
4985 || s->reloc_count == 0
4986 || (s->flags & SEC_EXCLUDE) != 0
4987 || ((info->strip == strip_all
4988 || info->strip == strip_debugger)
4989 && (s->flags & SEC_DEBUGGING) != 0))
4992 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4995 if (internal_relocs == NULL)
4996 goto error_free_vers;
4998 rel = internal_relocs;
4999 relend = rel + s->reloc_count;
5000 for ( ; rel < relend; rel++)
5002 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5003 struct elf_link_hash_entry *h;
5005 /* Skip local symbols. */
5006 if (r_symndx < extsymoff)
5009 h = sym_hash[r_symndx - extsymoff];
5011 h->root.non_ir_ref_regular = 1;
5014 if (elf_section_data (s)->relocs != internal_relocs)
5015 free (internal_relocs);
5019 if (extversym != NULL)
5025 if (isymbuf != NULL)
5031 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5035 /* Restore the symbol table. */
5036 old_ent = (char *) old_tab + tabsize;
5037 memset (elf_sym_hashes (abfd), 0,
5038 extsymcount * sizeof (struct elf_link_hash_entry *));
5039 htab->root.table.table = old_table;
5040 htab->root.table.size = old_size;
5041 htab->root.table.count = old_count;
5042 memcpy (htab->root.table.table, old_tab, tabsize);
5043 htab->root.undefs = old_undefs;
5044 htab->root.undefs_tail = old_undefs_tail;
5045 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5048 for (i = 0; i < htab->root.table.size; i++)
5050 struct bfd_hash_entry *p;
5051 struct elf_link_hash_entry *h;
5053 unsigned int alignment_power;
5054 unsigned int non_ir_ref_dynamic;
5056 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5058 h = (struct elf_link_hash_entry *) p;
5059 if (h->root.type == bfd_link_hash_warning)
5060 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5062 /* Preserve the maximum alignment and size for common
5063 symbols even if this dynamic lib isn't on DT_NEEDED
5064 since it can still be loaded at run time by another
5066 if (h->root.type == bfd_link_hash_common)
5068 size = h->root.u.c.size;
5069 alignment_power = h->root.u.c.p->alignment_power;
5074 alignment_power = 0;
5076 /* Preserve non_ir_ref_dynamic so that this symbol
5077 will be exported when the dynamic lib becomes needed
5078 in the second pass. */
5079 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5080 memcpy (p, old_ent, htab->root.table.entsize);
5081 old_ent = (char *) old_ent + htab->root.table.entsize;
5082 h = (struct elf_link_hash_entry *) p;
5083 if (h->root.type == bfd_link_hash_warning)
5085 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5086 old_ent = (char *) old_ent + htab->root.table.entsize;
5087 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5089 if (h->root.type == bfd_link_hash_common)
5091 if (size > h->root.u.c.size)
5092 h->root.u.c.size = size;
5093 if (alignment_power > h->root.u.c.p->alignment_power)
5094 h->root.u.c.p->alignment_power = alignment_power;
5096 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5100 /* Make a special call to the linker "notice" function to
5101 tell it that symbols added for crefs may need to be removed. */
5102 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5103 goto error_free_vers;
5106 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5108 if (nondeflt_vers != NULL)
5109 free (nondeflt_vers);
5113 if (old_tab != NULL)
5115 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5116 goto error_free_vers;
5121 /* Now that all the symbols from this input file are created, if
5122 not performing a relocatable link, handle .symver foo, foo@BAR
5123 such that any relocs against foo become foo@BAR. */
5124 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5128 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5130 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5131 char *shortname, *p;
5133 p = strchr (h->root.root.string, ELF_VER_CHR);
5135 || (h->root.type != bfd_link_hash_defined
5136 && h->root.type != bfd_link_hash_defweak))
5139 amt = p - h->root.root.string;
5140 shortname = (char *) bfd_malloc (amt + 1);
5142 goto error_free_vers;
5143 memcpy (shortname, h->root.root.string, amt);
5144 shortname[amt] = '\0';
5146 hi = (struct elf_link_hash_entry *)
5147 bfd_link_hash_lookup (&htab->root, shortname,
5148 FALSE, FALSE, FALSE);
5150 && hi->root.type == h->root.type
5151 && hi->root.u.def.value == h->root.u.def.value
5152 && hi->root.u.def.section == h->root.u.def.section)
5154 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5155 hi->root.type = bfd_link_hash_indirect;
5156 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5157 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5158 sym_hash = elf_sym_hashes (abfd);
5160 for (symidx = 0; symidx < extsymcount; ++symidx)
5161 if (sym_hash[symidx] == hi)
5163 sym_hash[symidx] = h;
5169 free (nondeflt_vers);
5170 nondeflt_vers = NULL;
5173 /* Now set the alias field correctly for all the weak defined
5174 symbols we found. The only way to do this is to search all the
5175 symbols. Since we only need the information for non functions in
5176 dynamic objects, that's the only time we actually put anything on
5177 the list WEAKS. We need this information so that if a regular
5178 object refers to a symbol defined weakly in a dynamic object, the
5179 real symbol in the dynamic object is also put in the dynamic
5180 symbols; we also must arrange for both symbols to point to the
5181 same memory location. We could handle the general case of symbol
5182 aliasing, but a general symbol alias can only be generated in
5183 assembler code, handling it correctly would be very time
5184 consuming, and other ELF linkers don't handle general aliasing
5188 struct elf_link_hash_entry **hpp;
5189 struct elf_link_hash_entry **hppend;
5190 struct elf_link_hash_entry **sorted_sym_hash;
5191 struct elf_link_hash_entry *h;
5194 /* Since we have to search the whole symbol list for each weak
5195 defined symbol, search time for N weak defined symbols will be
5196 O(N^2). Binary search will cut it down to O(NlogN). */
5198 amt *= sizeof (struct elf_link_hash_entry *);
5199 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5200 if (sorted_sym_hash == NULL)
5202 sym_hash = sorted_sym_hash;
5203 hpp = elf_sym_hashes (abfd);
5204 hppend = hpp + extsymcount;
5206 for (; hpp < hppend; hpp++)
5210 && h->root.type == bfd_link_hash_defined
5211 && !bed->is_function_type (h->type))
5219 qsort (sorted_sym_hash, sym_count,
5220 sizeof (struct elf_link_hash_entry *),
5223 while (weaks != NULL)
5225 struct elf_link_hash_entry *hlook;
5228 size_t i, j, idx = 0;
5231 weaks = hlook->u.alias;
5232 hlook->u.alias = NULL;
5234 if (hlook->root.type != bfd_link_hash_defined
5235 && hlook->root.type != bfd_link_hash_defweak)
5238 slook = hlook->root.u.def.section;
5239 vlook = hlook->root.u.def.value;
5245 bfd_signed_vma vdiff;
5247 h = sorted_sym_hash[idx];
5248 vdiff = vlook - h->root.u.def.value;
5255 int sdiff = slook->id - h->root.u.def.section->id;
5265 /* We didn't find a value/section match. */
5269 /* With multiple aliases, or when the weak symbol is already
5270 strongly defined, we have multiple matching symbols and
5271 the binary search above may land on any of them. Step
5272 one past the matching symbol(s). */
5275 h = sorted_sym_hash[idx];
5276 if (h->root.u.def.section != slook
5277 || h->root.u.def.value != vlook)
5281 /* Now look back over the aliases. Since we sorted by size
5282 as well as value and section, we'll choose the one with
5283 the largest size. */
5286 h = sorted_sym_hash[idx];
5288 /* Stop if value or section doesn't match. */
5289 if (h->root.u.def.section != slook
5290 || h->root.u.def.value != vlook)
5292 else if (h != hlook)
5294 struct elf_link_hash_entry *t;
5297 hlook->is_weakalias = 1;
5299 if (t->u.alias != NULL)
5300 while (t->u.alias != h)
5304 /* If the weak definition is in the list of dynamic
5305 symbols, make sure the real definition is put
5307 if (hlook->dynindx != -1 && h->dynindx == -1)
5309 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5312 free (sorted_sym_hash);
5317 /* If the real definition is in the list of dynamic
5318 symbols, make sure the weak definition is put
5319 there as well. If we don't do this, then the
5320 dynamic loader might not merge the entries for the
5321 real definition and the weak definition. */
5322 if (h->dynindx != -1 && hlook->dynindx == -1)
5324 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5325 goto err_free_sym_hash;
5332 free (sorted_sym_hash);
5335 if (bed->check_directives
5336 && !(*bed->check_directives) (abfd, info))
5339 /* If this is a non-traditional link, try to optimize the handling
5340 of the .stab/.stabstr sections. */
5342 && ! info->traditional_format
5343 && is_elf_hash_table (htab)
5344 && (info->strip != strip_all && info->strip != strip_debugger))
5348 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5349 if (stabstr != NULL)
5351 bfd_size_type string_offset = 0;
5354 for (stab = abfd->sections; stab; stab = stab->next)
5355 if (CONST_STRNEQ (stab->name, ".stab")
5356 && (!stab->name[5] ||
5357 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5358 && (stab->flags & SEC_MERGE) == 0
5359 && !bfd_is_abs_section (stab->output_section))
5361 struct bfd_elf_section_data *secdata;
5363 secdata = elf_section_data (stab);
5364 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5365 stabstr, &secdata->sec_info,
5368 if (secdata->sec_info)
5369 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5374 if (is_elf_hash_table (htab) && add_needed)
5376 /* Add this bfd to the loaded list. */
5377 struct elf_link_loaded_list *n;
5379 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5383 n->next = htab->loaded;
5390 if (old_tab != NULL)
5392 if (old_strtab != NULL)
5394 if (nondeflt_vers != NULL)
5395 free (nondeflt_vers);
5396 if (extversym != NULL)
5399 if (isymbuf != NULL)
5405 /* Return the linker hash table entry of a symbol that might be
5406 satisfied by an archive symbol. Return -1 on error. */
5408 struct elf_link_hash_entry *
5409 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5410 struct bfd_link_info *info,
5413 struct elf_link_hash_entry *h;
5417 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5421 /* If this is a default version (the name contains @@), look up the
5422 symbol again with only one `@' as well as without the version.
5423 The effect is that references to the symbol with and without the
5424 version will be matched by the default symbol in the archive. */
5426 p = strchr (name, ELF_VER_CHR);
5427 if (p == NULL || p[1] != ELF_VER_CHR)
5430 /* First check with only one `@'. */
5431 len = strlen (name);
5432 copy = (char *) bfd_alloc (abfd, len);
5434 return (struct elf_link_hash_entry *) -1;
5436 first = p - name + 1;
5437 memcpy (copy, name, first);
5438 memcpy (copy + first, name + first + 1, len - first);
5440 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5443 /* We also need to check references to the symbol without the
5445 copy[first - 1] = '\0';
5446 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5447 FALSE, FALSE, TRUE);
5450 bfd_release (abfd, copy);
5454 /* Add symbols from an ELF archive file to the linker hash table. We
5455 don't use _bfd_generic_link_add_archive_symbols because we need to
5456 handle versioned symbols.
5458 Fortunately, ELF archive handling is simpler than that done by
5459 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5460 oddities. In ELF, if we find a symbol in the archive map, and the
5461 symbol is currently undefined, we know that we must pull in that
5464 Unfortunately, we do have to make multiple passes over the symbol
5465 table until nothing further is resolved. */
5468 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5471 unsigned char *included = NULL;
5475 const struct elf_backend_data *bed;
5476 struct elf_link_hash_entry * (*archive_symbol_lookup)
5477 (bfd *, struct bfd_link_info *, const char *);
5479 if (! bfd_has_map (abfd))
5481 /* An empty archive is a special case. */
5482 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5484 bfd_set_error (bfd_error_no_armap);
5488 /* Keep track of all symbols we know to be already defined, and all
5489 files we know to be already included. This is to speed up the
5490 second and subsequent passes. */
5491 c = bfd_ardata (abfd)->symdef_count;
5495 amt *= sizeof (*included);
5496 included = (unsigned char *) bfd_zmalloc (amt);
5497 if (included == NULL)
5500 symdefs = bfd_ardata (abfd)->symdefs;
5501 bed = get_elf_backend_data (abfd);
5502 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5515 symdefend = symdef + c;
5516 for (i = 0; symdef < symdefend; symdef++, i++)
5518 struct elf_link_hash_entry *h;
5520 struct bfd_link_hash_entry *undefs_tail;
5525 if (symdef->file_offset == last)
5531 h = archive_symbol_lookup (abfd, info, symdef->name);
5532 if (h == (struct elf_link_hash_entry *) -1)
5538 if (h->root.type == bfd_link_hash_common)
5540 /* We currently have a common symbol. The archive map contains
5541 a reference to this symbol, so we may want to include it. We
5542 only want to include it however, if this archive element
5543 contains a definition of the symbol, not just another common
5546 Unfortunately some archivers (including GNU ar) will put
5547 declarations of common symbols into their archive maps, as
5548 well as real definitions, so we cannot just go by the archive
5549 map alone. Instead we must read in the element's symbol
5550 table and check that to see what kind of symbol definition
5552 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5555 else if (h->root.type != bfd_link_hash_undefined)
5557 if (h->root.type != bfd_link_hash_undefweak)
5558 /* Symbol must be defined. Don't check it again. */
5563 /* We need to include this archive member. */
5564 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5565 if (element == NULL)
5568 if (! bfd_check_format (element, bfd_object))
5571 undefs_tail = info->hash->undefs_tail;
5573 if (!(*info->callbacks
5574 ->add_archive_element) (info, element, symdef->name, &element))
5576 if (!bfd_link_add_symbols (element, info))
5579 /* If there are any new undefined symbols, we need to make
5580 another pass through the archive in order to see whether
5581 they can be defined. FIXME: This isn't perfect, because
5582 common symbols wind up on undefs_tail and because an
5583 undefined symbol which is defined later on in this pass
5584 does not require another pass. This isn't a bug, but it
5585 does make the code less efficient than it could be. */
5586 if (undefs_tail != info->hash->undefs_tail)
5589 /* Look backward to mark all symbols from this object file
5590 which we have already seen in this pass. */
5594 included[mark] = TRUE;
5599 while (symdefs[mark].file_offset == symdef->file_offset);
5601 /* We mark subsequent symbols from this object file as we go
5602 on through the loop. */
5603 last = symdef->file_offset;
5613 if (included != NULL)
5618 /* Given an ELF BFD, add symbols to the global hash table as
5622 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5624 switch (bfd_get_format (abfd))
5627 return elf_link_add_object_symbols (abfd, info);
5629 return elf_link_add_archive_symbols (abfd, info);
5631 bfd_set_error (bfd_error_wrong_format);
5636 struct hash_codes_info
5638 unsigned long *hashcodes;
5642 /* This function will be called though elf_link_hash_traverse to store
5643 all hash value of the exported symbols in an array. */
5646 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5648 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5653 /* Ignore indirect symbols. These are added by the versioning code. */
5654 if (h->dynindx == -1)
5657 name = h->root.root.string;
5658 if (h->versioned >= versioned)
5660 char *p = strchr (name, ELF_VER_CHR);
5663 alc = (char *) bfd_malloc (p - name + 1);
5669 memcpy (alc, name, p - name);
5670 alc[p - name] = '\0';
5675 /* Compute the hash value. */
5676 ha = bfd_elf_hash (name);
5678 /* Store the found hash value in the array given as the argument. */
5679 *(inf->hashcodes)++ = ha;
5681 /* And store it in the struct so that we can put it in the hash table
5683 h->u.elf_hash_value = ha;
5691 struct collect_gnu_hash_codes
5694 const struct elf_backend_data *bed;
5695 unsigned long int nsyms;
5696 unsigned long int maskbits;
5697 unsigned long int *hashcodes;
5698 unsigned long int *hashval;
5699 unsigned long int *indx;
5700 unsigned long int *counts;
5703 long int min_dynindx;
5704 unsigned long int bucketcount;
5705 unsigned long int symindx;
5706 long int local_indx;
5707 long int shift1, shift2;
5708 unsigned long int mask;
5712 /* This function will be called though elf_link_hash_traverse to store
5713 all hash value of the exported symbols in an array. */
5716 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5718 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5723 /* Ignore indirect symbols. These are added by the versioning code. */
5724 if (h->dynindx == -1)
5727 /* Ignore also local symbols and undefined symbols. */
5728 if (! (*s->bed->elf_hash_symbol) (h))
5731 name = h->root.root.string;
5732 if (h->versioned >= versioned)
5734 char *p = strchr (name, ELF_VER_CHR);
5737 alc = (char *) bfd_malloc (p - name + 1);
5743 memcpy (alc, name, p - name);
5744 alc[p - name] = '\0';
5749 /* Compute the hash value. */
5750 ha = bfd_elf_gnu_hash (name);
5752 /* Store the found hash value in the array for compute_bucket_count,
5753 and also for .dynsym reordering purposes. */
5754 s->hashcodes[s->nsyms] = ha;
5755 s->hashval[h->dynindx] = ha;
5757 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5758 s->min_dynindx = h->dynindx;
5766 /* This function will be called though elf_link_hash_traverse to do
5767 final dynaminc symbol renumbering. */
5770 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5772 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5773 unsigned long int bucket;
5774 unsigned long int val;
5776 /* Ignore indirect symbols. */
5777 if (h->dynindx == -1)
5780 /* Ignore also local symbols and undefined symbols. */
5781 if (! (*s->bed->elf_hash_symbol) (h))
5783 if (h->dynindx >= s->min_dynindx)
5784 h->dynindx = s->local_indx++;
5788 bucket = s->hashval[h->dynindx] % s->bucketcount;
5789 val = (s->hashval[h->dynindx] >> s->shift1)
5790 & ((s->maskbits >> s->shift1) - 1);
5791 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5793 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5794 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5795 if (s->counts[bucket] == 1)
5796 /* Last element terminates the chain. */
5798 bfd_put_32 (s->output_bfd, val,
5799 s->contents + (s->indx[bucket] - s->symindx) * 4);
5800 --s->counts[bucket];
5801 h->dynindx = s->indx[bucket]++;
5805 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5808 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5810 return !(h->forced_local
5811 || h->root.type == bfd_link_hash_undefined
5812 || h->root.type == bfd_link_hash_undefweak
5813 || ((h->root.type == bfd_link_hash_defined
5814 || h->root.type == bfd_link_hash_defweak)
5815 && h->root.u.def.section->output_section == NULL));
5818 /* Array used to determine the number of hash table buckets to use
5819 based on the number of symbols there are. If there are fewer than
5820 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5821 fewer than 37 we use 17 buckets, and so forth. We never use more
5822 than 32771 buckets. */
5824 static const size_t elf_buckets[] =
5826 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5830 /* Compute bucket count for hashing table. We do not use a static set
5831 of possible tables sizes anymore. Instead we determine for all
5832 possible reasonable sizes of the table the outcome (i.e., the
5833 number of collisions etc) and choose the best solution. The
5834 weighting functions are not too simple to allow the table to grow
5835 without bounds. Instead one of the weighting factors is the size.
5836 Therefore the result is always a good payoff between few collisions
5837 (= short chain lengths) and table size. */
5839 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5840 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5841 unsigned long int nsyms,
5844 size_t best_size = 0;
5845 unsigned long int i;
5847 /* We have a problem here. The following code to optimize the table
5848 size requires an integer type with more the 32 bits. If
5849 BFD_HOST_U_64_BIT is set we know about such a type. */
5850 #ifdef BFD_HOST_U_64_BIT
5855 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5856 bfd *dynobj = elf_hash_table (info)->dynobj;
5857 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5858 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5859 unsigned long int *counts;
5861 unsigned int no_improvement_count = 0;
5863 /* Possible optimization parameters: if we have NSYMS symbols we say
5864 that the hashing table must at least have NSYMS/4 and at most
5866 minsize = nsyms / 4;
5869 best_size = maxsize = nsyms * 2;
5874 if ((best_size & 31) == 0)
5878 /* Create array where we count the collisions in. We must use bfd_malloc
5879 since the size could be large. */
5881 amt *= sizeof (unsigned long int);
5882 counts = (unsigned long int *) bfd_malloc (amt);
5886 /* Compute the "optimal" size for the hash table. The criteria is a
5887 minimal chain length. The minor criteria is (of course) the size
5889 for (i = minsize; i < maxsize; ++i)
5891 /* Walk through the array of hashcodes and count the collisions. */
5892 BFD_HOST_U_64_BIT max;
5893 unsigned long int j;
5894 unsigned long int fact;
5896 if (gnu_hash && (i & 31) == 0)
5899 memset (counts, '\0', i * sizeof (unsigned long int));
5901 /* Determine how often each hash bucket is used. */
5902 for (j = 0; j < nsyms; ++j)
5903 ++counts[hashcodes[j] % i];
5905 /* For the weight function we need some information about the
5906 pagesize on the target. This is information need not be 100%
5907 accurate. Since this information is not available (so far) we
5908 define it here to a reasonable default value. If it is crucial
5909 to have a better value some day simply define this value. */
5910 # ifndef BFD_TARGET_PAGESIZE
5911 # define BFD_TARGET_PAGESIZE (4096)
5914 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5916 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5919 /* Variant 1: optimize for short chains. We add the squares
5920 of all the chain lengths (which favors many small chain
5921 over a few long chains). */
5922 for (j = 0; j < i; ++j)
5923 max += counts[j] * counts[j];
5925 /* This adds penalties for the overall size of the table. */
5926 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5929 /* Variant 2: Optimize a lot more for small table. Here we
5930 also add squares of the size but we also add penalties for
5931 empty slots (the +1 term). */
5932 for (j = 0; j < i; ++j)
5933 max += (1 + counts[j]) * (1 + counts[j]);
5935 /* The overall size of the table is considered, but not as
5936 strong as in variant 1, where it is squared. */
5937 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5941 /* Compare with current best results. */
5942 if (max < best_chlen)
5946 no_improvement_count = 0;
5948 /* PR 11843: Avoid futile long searches for the best bucket size
5949 when there are a large number of symbols. */
5950 else if (++no_improvement_count == 100)
5957 #endif /* defined (BFD_HOST_U_64_BIT) */
5959 /* This is the fallback solution if no 64bit type is available or if we
5960 are not supposed to spend much time on optimizations. We select the
5961 bucket count using a fixed set of numbers. */
5962 for (i = 0; elf_buckets[i] != 0; i++)
5964 best_size = elf_buckets[i];
5965 if (nsyms < elf_buckets[i + 1])
5968 if (gnu_hash && best_size < 2)
5975 /* Size any SHT_GROUP section for ld -r. */
5978 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5983 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5984 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5985 && (s = ibfd->sections) != NULL
5986 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5987 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5992 /* Set a default stack segment size. The value in INFO wins. If it
5993 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5994 undefined it is initialized. */
5997 bfd_elf_stack_segment_size (bfd *output_bfd,
5998 struct bfd_link_info *info,
5999 const char *legacy_symbol,
6000 bfd_vma default_size)
6002 struct elf_link_hash_entry *h = NULL;
6004 /* Look for legacy symbol. */
6006 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6007 FALSE, FALSE, FALSE);
6008 if (h && (h->root.type == bfd_link_hash_defined
6009 || h->root.type == bfd_link_hash_defweak)
6011 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6013 /* The symbol has no type if specified on the command line. */
6014 h->type = STT_OBJECT;
6015 if (info->stacksize)
6016 /* xgettext:c-format */
6017 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6018 output_bfd, legacy_symbol);
6019 else if (h->root.u.def.section != bfd_abs_section_ptr)
6020 /* xgettext:c-format */
6021 _bfd_error_handler (_("%pB: %s not absolute"),
6022 output_bfd, legacy_symbol);
6024 info->stacksize = h->root.u.def.value;
6027 if (!info->stacksize)
6028 /* If the user didn't set a size, or explicitly inhibit the
6029 size, set it now. */
6030 info->stacksize = default_size;
6032 /* Provide the legacy symbol, if it is referenced. */
6033 if (h && (h->root.type == bfd_link_hash_undefined
6034 || h->root.type == bfd_link_hash_undefweak))
6036 struct bfd_link_hash_entry *bh = NULL;
6038 if (!(_bfd_generic_link_add_one_symbol
6039 (info, output_bfd, legacy_symbol,
6040 BSF_GLOBAL, bfd_abs_section_ptr,
6041 info->stacksize >= 0 ? info->stacksize : 0,
6042 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6045 h = (struct elf_link_hash_entry *) bh;
6047 h->type = STT_OBJECT;
6053 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6055 struct elf_gc_sweep_symbol_info
6057 struct bfd_link_info *info;
6058 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6063 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6066 && (((h->root.type == bfd_link_hash_defined
6067 || h->root.type == bfd_link_hash_defweak)
6068 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6069 && h->root.u.def.section->gc_mark))
6070 || h->root.type == bfd_link_hash_undefined
6071 || h->root.type == bfd_link_hash_undefweak))
6073 struct elf_gc_sweep_symbol_info *inf;
6075 inf = (struct elf_gc_sweep_symbol_info *) data;
6076 (*inf->hide_symbol) (inf->info, h, TRUE);
6079 h->ref_regular_nonweak = 0;
6085 /* Set up the sizes and contents of the ELF dynamic sections. This is
6086 called by the ELF linker emulation before_allocation routine. We
6087 must set the sizes of the sections before the linker sets the
6088 addresses of the various sections. */
6091 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6094 const char *filter_shlib,
6096 const char *depaudit,
6097 const char * const *auxiliary_filters,
6098 struct bfd_link_info *info,
6099 asection **sinterpptr)
6102 const struct elf_backend_data *bed;
6106 if (!is_elf_hash_table (info->hash))
6109 dynobj = elf_hash_table (info)->dynobj;
6111 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6113 struct bfd_elf_version_tree *verdefs;
6114 struct elf_info_failed asvinfo;
6115 struct bfd_elf_version_tree *t;
6116 struct bfd_elf_version_expr *d;
6120 /* If we are supposed to export all symbols into the dynamic symbol
6121 table (this is not the normal case), then do so. */
6122 if (info->export_dynamic
6123 || (bfd_link_executable (info) && info->dynamic))
6125 struct elf_info_failed eif;
6129 elf_link_hash_traverse (elf_hash_table (info),
6130 _bfd_elf_export_symbol,
6138 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6140 if (soname_indx == (size_t) -1
6141 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6145 soname_indx = (size_t) -1;
6147 /* Make all global versions with definition. */
6148 for (t = info->version_info; t != NULL; t = t->next)
6149 for (d = t->globals.list; d != NULL; d = d->next)
6150 if (!d->symver && d->literal)
6152 const char *verstr, *name;
6153 size_t namelen, verlen, newlen;
6154 char *newname, *p, leading_char;
6155 struct elf_link_hash_entry *newh;
6157 leading_char = bfd_get_symbol_leading_char (output_bfd);
6159 namelen = strlen (name) + (leading_char != '\0');
6161 verlen = strlen (verstr);
6162 newlen = namelen + verlen + 3;
6164 newname = (char *) bfd_malloc (newlen);
6165 if (newname == NULL)
6167 newname[0] = leading_char;
6168 memcpy (newname + (leading_char != '\0'), name, namelen);
6170 /* Check the hidden versioned definition. */
6171 p = newname + namelen;
6173 memcpy (p, verstr, verlen + 1);
6174 newh = elf_link_hash_lookup (elf_hash_table (info),
6175 newname, FALSE, FALSE,
6178 || (newh->root.type != bfd_link_hash_defined
6179 && newh->root.type != bfd_link_hash_defweak))
6181 /* Check the default versioned definition. */
6183 memcpy (p, verstr, verlen + 1);
6184 newh = elf_link_hash_lookup (elf_hash_table (info),
6185 newname, FALSE, FALSE,
6190 /* Mark this version if there is a definition and it is
6191 not defined in a shared object. */
6193 && !newh->def_dynamic
6194 && (newh->root.type == bfd_link_hash_defined
6195 || newh->root.type == bfd_link_hash_defweak))
6199 /* Attach all the symbols to their version information. */
6200 asvinfo.info = info;
6201 asvinfo.failed = FALSE;
6203 elf_link_hash_traverse (elf_hash_table (info),
6204 _bfd_elf_link_assign_sym_version,
6209 if (!info->allow_undefined_version)
6211 /* Check if all global versions have a definition. */
6212 bfd_boolean all_defined = TRUE;
6213 for (t = info->version_info; t != NULL; t = t->next)
6214 for (d = t->globals.list; d != NULL; d = d->next)
6215 if (d->literal && !d->symver && !d->script)
6218 (_("%s: undefined version: %s"),
6219 d->pattern, t->name);
6220 all_defined = FALSE;
6225 bfd_set_error (bfd_error_bad_value);
6230 /* Set up the version definition section. */
6231 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6232 BFD_ASSERT (s != NULL);
6234 /* We may have created additional version definitions if we are
6235 just linking a regular application. */
6236 verdefs = info->version_info;
6238 /* Skip anonymous version tag. */
6239 if (verdefs != NULL && verdefs->vernum == 0)
6240 verdefs = verdefs->next;
6242 if (verdefs == NULL && !info->create_default_symver)
6243 s->flags |= SEC_EXCLUDE;
6249 Elf_Internal_Verdef def;
6250 Elf_Internal_Verdaux defaux;
6251 struct bfd_link_hash_entry *bh;
6252 struct elf_link_hash_entry *h;
6258 /* Make space for the base version. */
6259 size += sizeof (Elf_External_Verdef);
6260 size += sizeof (Elf_External_Verdaux);
6263 /* Make space for the default version. */
6264 if (info->create_default_symver)
6266 size += sizeof (Elf_External_Verdef);
6270 for (t = verdefs; t != NULL; t = t->next)
6272 struct bfd_elf_version_deps *n;
6274 /* Don't emit base version twice. */
6278 size += sizeof (Elf_External_Verdef);
6279 size += sizeof (Elf_External_Verdaux);
6282 for (n = t->deps; n != NULL; n = n->next)
6283 size += sizeof (Elf_External_Verdaux);
6287 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6288 if (s->contents == NULL && s->size != 0)
6291 /* Fill in the version definition section. */
6295 def.vd_version = VER_DEF_CURRENT;
6296 def.vd_flags = VER_FLG_BASE;
6299 if (info->create_default_symver)
6301 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6302 def.vd_next = sizeof (Elf_External_Verdef);
6306 def.vd_aux = sizeof (Elf_External_Verdef);
6307 def.vd_next = (sizeof (Elf_External_Verdef)
6308 + sizeof (Elf_External_Verdaux));
6311 if (soname_indx != (size_t) -1)
6313 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6315 def.vd_hash = bfd_elf_hash (soname);
6316 defaux.vda_name = soname_indx;
6323 name = lbasename (output_bfd->filename);
6324 def.vd_hash = bfd_elf_hash (name);
6325 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6327 if (indx == (size_t) -1)
6329 defaux.vda_name = indx;
6331 defaux.vda_next = 0;
6333 _bfd_elf_swap_verdef_out (output_bfd, &def,
6334 (Elf_External_Verdef *) p);
6335 p += sizeof (Elf_External_Verdef);
6336 if (info->create_default_symver)
6338 /* Add a symbol representing this version. */
6340 if (! (_bfd_generic_link_add_one_symbol
6341 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6343 get_elf_backend_data (dynobj)->collect, &bh)))
6345 h = (struct elf_link_hash_entry *) bh;
6348 h->type = STT_OBJECT;
6349 h->verinfo.vertree = NULL;
6351 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6354 /* Create a duplicate of the base version with the same
6355 aux block, but different flags. */
6358 def.vd_aux = sizeof (Elf_External_Verdef);
6360 def.vd_next = (sizeof (Elf_External_Verdef)
6361 + sizeof (Elf_External_Verdaux));
6364 _bfd_elf_swap_verdef_out (output_bfd, &def,
6365 (Elf_External_Verdef *) p);
6366 p += sizeof (Elf_External_Verdef);
6368 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6369 (Elf_External_Verdaux *) p);
6370 p += sizeof (Elf_External_Verdaux);
6372 for (t = verdefs; t != NULL; t = t->next)
6375 struct bfd_elf_version_deps *n;
6377 /* Don't emit the base version twice. */
6382 for (n = t->deps; n != NULL; n = n->next)
6385 /* Add a symbol representing this version. */
6387 if (! (_bfd_generic_link_add_one_symbol
6388 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6390 get_elf_backend_data (dynobj)->collect, &bh)))
6392 h = (struct elf_link_hash_entry *) bh;
6395 h->type = STT_OBJECT;
6396 h->verinfo.vertree = t;
6398 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6401 def.vd_version = VER_DEF_CURRENT;
6403 if (t->globals.list == NULL
6404 && t->locals.list == NULL
6406 def.vd_flags |= VER_FLG_WEAK;
6407 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6408 def.vd_cnt = cdeps + 1;
6409 def.vd_hash = bfd_elf_hash (t->name);
6410 def.vd_aux = sizeof (Elf_External_Verdef);
6413 /* If a basever node is next, it *must* be the last node in
6414 the chain, otherwise Verdef construction breaks. */
6415 if (t->next != NULL && t->next->vernum == 0)
6416 BFD_ASSERT (t->next->next == NULL);
6418 if (t->next != NULL && t->next->vernum != 0)
6419 def.vd_next = (sizeof (Elf_External_Verdef)
6420 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6422 _bfd_elf_swap_verdef_out (output_bfd, &def,
6423 (Elf_External_Verdef *) p);
6424 p += sizeof (Elf_External_Verdef);
6426 defaux.vda_name = h->dynstr_index;
6427 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6429 defaux.vda_next = 0;
6430 if (t->deps != NULL)
6431 defaux.vda_next = sizeof (Elf_External_Verdaux);
6432 t->name_indx = defaux.vda_name;
6434 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6435 (Elf_External_Verdaux *) p);
6436 p += sizeof (Elf_External_Verdaux);
6438 for (n = t->deps; n != NULL; n = n->next)
6440 if (n->version_needed == NULL)
6442 /* This can happen if there was an error in the
6444 defaux.vda_name = 0;
6448 defaux.vda_name = n->version_needed->name_indx;
6449 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6452 if (n->next == NULL)
6453 defaux.vda_next = 0;
6455 defaux.vda_next = sizeof (Elf_External_Verdaux);
6457 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6458 (Elf_External_Verdaux *) p);
6459 p += sizeof (Elf_External_Verdaux);
6463 elf_tdata (output_bfd)->cverdefs = cdefs;
6467 bed = get_elf_backend_data (output_bfd);
6469 if (info->gc_sections && bed->can_gc_sections)
6471 struct elf_gc_sweep_symbol_info sweep_info;
6473 /* Remove the symbols that were in the swept sections from the
6474 dynamic symbol table. */
6475 sweep_info.info = info;
6476 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6477 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6481 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6484 struct elf_find_verdep_info sinfo;
6486 /* Work out the size of the version reference section. */
6488 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6489 BFD_ASSERT (s != NULL);
6492 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6493 if (sinfo.vers == 0)
6495 sinfo.failed = FALSE;
6497 elf_link_hash_traverse (elf_hash_table (info),
6498 _bfd_elf_link_find_version_dependencies,
6503 if (elf_tdata (output_bfd)->verref == NULL)
6504 s->flags |= SEC_EXCLUDE;
6507 Elf_Internal_Verneed *vn;
6512 /* Build the version dependency section. */
6515 for (vn = elf_tdata (output_bfd)->verref;
6517 vn = vn->vn_nextref)
6519 Elf_Internal_Vernaux *a;
6521 size += sizeof (Elf_External_Verneed);
6523 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6524 size += sizeof (Elf_External_Vernaux);
6528 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6529 if (s->contents == NULL)
6533 for (vn = elf_tdata (output_bfd)->verref;
6535 vn = vn->vn_nextref)
6538 Elf_Internal_Vernaux *a;
6542 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6545 vn->vn_version = VER_NEED_CURRENT;
6547 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6548 elf_dt_name (vn->vn_bfd) != NULL
6549 ? elf_dt_name (vn->vn_bfd)
6550 : lbasename (vn->vn_bfd->filename),
6552 if (indx == (size_t) -1)
6555 vn->vn_aux = sizeof (Elf_External_Verneed);
6556 if (vn->vn_nextref == NULL)
6559 vn->vn_next = (sizeof (Elf_External_Verneed)
6560 + caux * sizeof (Elf_External_Vernaux));
6562 _bfd_elf_swap_verneed_out (output_bfd, vn,
6563 (Elf_External_Verneed *) p);
6564 p += sizeof (Elf_External_Verneed);
6566 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6568 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6569 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6570 a->vna_nodename, FALSE);
6571 if (indx == (size_t) -1)
6574 if (a->vna_nextptr == NULL)
6577 a->vna_next = sizeof (Elf_External_Vernaux);
6579 _bfd_elf_swap_vernaux_out (output_bfd, a,
6580 (Elf_External_Vernaux *) p);
6581 p += sizeof (Elf_External_Vernaux);
6585 elf_tdata (output_bfd)->cverrefs = crefs;
6589 /* Any syms created from now on start with -1 in
6590 got.refcount/offset and plt.refcount/offset. */
6591 elf_hash_table (info)->init_got_refcount
6592 = elf_hash_table (info)->init_got_offset;
6593 elf_hash_table (info)->init_plt_refcount
6594 = elf_hash_table (info)->init_plt_offset;
6596 if (bfd_link_relocatable (info)
6597 && !_bfd_elf_size_group_sections (info))
6600 /* The backend may have to create some sections regardless of whether
6601 we're dynamic or not. */
6602 if (bed->elf_backend_always_size_sections
6603 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6606 /* Determine any GNU_STACK segment requirements, after the backend
6607 has had a chance to set a default segment size. */
6608 if (info->execstack)
6609 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6610 else if (info->noexecstack)
6611 elf_stack_flags (output_bfd) = PF_R | PF_W;
6615 asection *notesec = NULL;
6618 for (inputobj = info->input_bfds;
6620 inputobj = inputobj->link.next)
6625 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6627 s = inputobj->sections;
6628 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6631 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6634 if (s->flags & SEC_CODE)
6638 else if (bed->default_execstack)
6641 if (notesec || info->stacksize > 0)
6642 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6643 if (notesec && exec && bfd_link_relocatable (info)
6644 && notesec->output_section != bfd_abs_section_ptr)
6645 notesec->output_section->flags |= SEC_CODE;
6648 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6650 struct elf_info_failed eif;
6651 struct elf_link_hash_entry *h;
6655 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6656 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6660 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6662 info->flags |= DF_SYMBOLIC;
6670 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6672 if (indx == (size_t) -1)
6675 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6676 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6680 if (filter_shlib != NULL)
6684 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6685 filter_shlib, TRUE);
6686 if (indx == (size_t) -1
6687 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6691 if (auxiliary_filters != NULL)
6693 const char * const *p;
6695 for (p = auxiliary_filters; *p != NULL; p++)
6699 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6701 if (indx == (size_t) -1
6702 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6711 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6713 if (indx == (size_t) -1
6714 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6718 if (depaudit != NULL)
6722 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6724 if (indx == (size_t) -1
6725 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6732 /* Find all symbols which were defined in a dynamic object and make
6733 the backend pick a reasonable value for them. */
6734 elf_link_hash_traverse (elf_hash_table (info),
6735 _bfd_elf_adjust_dynamic_symbol,
6740 /* Add some entries to the .dynamic section. We fill in some of the
6741 values later, in bfd_elf_final_link, but we must add the entries
6742 now so that we know the final size of the .dynamic section. */
6744 /* If there are initialization and/or finalization functions to
6745 call then add the corresponding DT_INIT/DT_FINI entries. */
6746 h = (info->init_function
6747 ? elf_link_hash_lookup (elf_hash_table (info),
6748 info->init_function, FALSE,
6755 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6758 h = (info->fini_function
6759 ? elf_link_hash_lookup (elf_hash_table (info),
6760 info->fini_function, FALSE,
6767 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6771 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6772 if (s != NULL && s->linker_has_input)
6774 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6775 if (! bfd_link_executable (info))
6780 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6781 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6782 && (o = sub->sections) != NULL
6783 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6784 for (o = sub->sections; o != NULL; o = o->next)
6785 if (elf_section_data (o)->this_hdr.sh_type
6786 == SHT_PREINIT_ARRAY)
6789 (_("%pB: .preinit_array section is not allowed in DSO"),
6794 bfd_set_error (bfd_error_nonrepresentable_section);
6798 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6799 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6802 s = bfd_get_section_by_name (output_bfd, ".init_array");
6803 if (s != NULL && s->linker_has_input)
6805 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6806 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6809 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6810 if (s != NULL && s->linker_has_input)
6812 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6813 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6817 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6818 /* If .dynstr is excluded from the link, we don't want any of
6819 these tags. Strictly, we should be checking each section
6820 individually; This quick check covers for the case where
6821 someone does a /DISCARD/ : { *(*) }. */
6822 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6824 bfd_size_type strsize;
6826 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6827 if ((info->emit_hash
6828 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6829 || (info->emit_gnu_hash
6830 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6831 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6832 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6833 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6834 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6835 bed->s->sizeof_sym))
6840 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6843 /* The backend must work out the sizes of all the other dynamic
6846 && bed->elf_backend_size_dynamic_sections != NULL
6847 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6850 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6852 if (elf_tdata (output_bfd)->cverdefs)
6854 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6856 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6857 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6861 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6863 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6866 else if (info->flags & DF_BIND_NOW)
6868 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6874 if (bfd_link_executable (info))
6875 info->flags_1 &= ~ (DF_1_INITFIRST
6878 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6882 if (elf_tdata (output_bfd)->cverrefs)
6884 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6886 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6887 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6891 if ((elf_tdata (output_bfd)->cverrefs == 0
6892 && elf_tdata (output_bfd)->cverdefs == 0)
6893 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6897 s = bfd_get_linker_section (dynobj, ".gnu.version");
6898 s->flags |= SEC_EXCLUDE;
6904 /* Find the first non-excluded output section. We'll use its
6905 section symbol for some emitted relocs. */
6907 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6911 for (s = output_bfd->sections; s != NULL; s = s->next)
6912 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6913 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
6915 elf_hash_table (info)->text_index_section = s;
6920 /* Find two non-excluded output sections, one for code, one for data.
6921 We'll use their section symbols for some emitted relocs. */
6923 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6927 /* Data first, since setting text_index_section changes
6928 _bfd_elf_link_omit_section_dynsym. */
6929 for (s = output_bfd->sections; s != NULL; s = s->next)
6930 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6931 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
6933 elf_hash_table (info)->data_index_section = s;
6937 for (s = output_bfd->sections; s != NULL; s = s->next)
6938 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6939 == (SEC_ALLOC | SEC_READONLY))
6940 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
6942 elf_hash_table (info)->text_index_section = s;
6946 if (elf_hash_table (info)->text_index_section == NULL)
6947 elf_hash_table (info)->text_index_section
6948 = elf_hash_table (info)->data_index_section;
6952 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6954 const struct elf_backend_data *bed;
6955 unsigned long section_sym_count;
6956 bfd_size_type dynsymcount = 0;
6958 if (!is_elf_hash_table (info->hash))
6961 bed = get_elf_backend_data (output_bfd);
6962 (*bed->elf_backend_init_index_section) (output_bfd, info);
6964 /* Assign dynsym indices. In a shared library we generate a section
6965 symbol for each output section, which come first. Next come all
6966 of the back-end allocated local dynamic syms, followed by the rest
6967 of the global symbols.
6969 This is usually not needed for static binaries, however backends
6970 can request to always do it, e.g. the MIPS backend uses dynamic
6971 symbol counts to lay out GOT, which will be produced in the
6972 presence of GOT relocations even in static binaries (holding fixed
6973 data in that case, to satisfy those relocations). */
6975 if (elf_hash_table (info)->dynamic_sections_created
6976 || bed->always_renumber_dynsyms)
6977 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6978 §ion_sym_count);
6980 if (elf_hash_table (info)->dynamic_sections_created)
6984 unsigned int dtagcount;
6986 dynobj = elf_hash_table (info)->dynobj;
6988 /* Work out the size of the symbol version section. */
6989 s = bfd_get_linker_section (dynobj, ".gnu.version");
6990 BFD_ASSERT (s != NULL);
6991 if ((s->flags & SEC_EXCLUDE) == 0)
6993 s->size = dynsymcount * sizeof (Elf_External_Versym);
6994 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6995 if (s->contents == NULL)
6998 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7002 /* Set the size of the .dynsym and .hash sections. We counted
7003 the number of dynamic symbols in elf_link_add_object_symbols.
7004 We will build the contents of .dynsym and .hash when we build
7005 the final symbol table, because until then we do not know the
7006 correct value to give the symbols. We built the .dynstr
7007 section as we went along in elf_link_add_object_symbols. */
7008 s = elf_hash_table (info)->dynsym;
7009 BFD_ASSERT (s != NULL);
7010 s->size = dynsymcount * bed->s->sizeof_sym;
7012 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7013 if (s->contents == NULL)
7016 /* The first entry in .dynsym is a dummy symbol. Clear all the
7017 section syms, in case we don't output them all. */
7018 ++section_sym_count;
7019 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7021 elf_hash_table (info)->bucketcount = 0;
7023 /* Compute the size of the hashing table. As a side effect this
7024 computes the hash values for all the names we export. */
7025 if (info->emit_hash)
7027 unsigned long int *hashcodes;
7028 struct hash_codes_info hashinf;
7030 unsigned long int nsyms;
7032 size_t hash_entry_size;
7034 /* Compute the hash values for all exported symbols. At the same
7035 time store the values in an array so that we could use them for
7037 amt = dynsymcount * sizeof (unsigned long int);
7038 hashcodes = (unsigned long int *) bfd_malloc (amt);
7039 if (hashcodes == NULL)
7041 hashinf.hashcodes = hashcodes;
7042 hashinf.error = FALSE;
7044 /* Put all hash values in HASHCODES. */
7045 elf_link_hash_traverse (elf_hash_table (info),
7046 elf_collect_hash_codes, &hashinf);
7053 nsyms = hashinf.hashcodes - hashcodes;
7055 = compute_bucket_count (info, hashcodes, nsyms, 0);
7058 if (bucketcount == 0 && nsyms > 0)
7061 elf_hash_table (info)->bucketcount = bucketcount;
7063 s = bfd_get_linker_section (dynobj, ".hash");
7064 BFD_ASSERT (s != NULL);
7065 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7066 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7067 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7068 if (s->contents == NULL)
7071 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7072 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7073 s->contents + hash_entry_size);
7076 if (info->emit_gnu_hash)
7079 unsigned char *contents;
7080 struct collect_gnu_hash_codes cinfo;
7084 memset (&cinfo, 0, sizeof (cinfo));
7086 /* Compute the hash values for all exported symbols. At the same
7087 time store the values in an array so that we could use them for
7089 amt = dynsymcount * 2 * sizeof (unsigned long int);
7090 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7091 if (cinfo.hashcodes == NULL)
7094 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7095 cinfo.min_dynindx = -1;
7096 cinfo.output_bfd = output_bfd;
7099 /* Put all hash values in HASHCODES. */
7100 elf_link_hash_traverse (elf_hash_table (info),
7101 elf_collect_gnu_hash_codes, &cinfo);
7104 free (cinfo.hashcodes);
7109 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7111 if (bucketcount == 0)
7113 free (cinfo.hashcodes);
7117 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7118 BFD_ASSERT (s != NULL);
7120 if (cinfo.nsyms == 0)
7122 /* Empty .gnu.hash section is special. */
7123 BFD_ASSERT (cinfo.min_dynindx == -1);
7124 free (cinfo.hashcodes);
7125 s->size = 5 * 4 + bed->s->arch_size / 8;
7126 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7127 if (contents == NULL)
7129 s->contents = contents;
7130 /* 1 empty bucket. */
7131 bfd_put_32 (output_bfd, 1, contents);
7132 /* SYMIDX above the special symbol 0. */
7133 bfd_put_32 (output_bfd, 1, contents + 4);
7134 /* Just one word for bitmask. */
7135 bfd_put_32 (output_bfd, 1, contents + 8);
7136 /* Only hash fn bloom filter. */
7137 bfd_put_32 (output_bfd, 0, contents + 12);
7138 /* No hashes are valid - empty bitmask. */
7139 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7140 /* No hashes in the only bucket. */
7141 bfd_put_32 (output_bfd, 0,
7142 contents + 16 + bed->s->arch_size / 8);
7146 unsigned long int maskwords, maskbitslog2, x;
7147 BFD_ASSERT (cinfo.min_dynindx != -1);
7151 while ((x >>= 1) != 0)
7153 if (maskbitslog2 < 3)
7155 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7156 maskbitslog2 = maskbitslog2 + 3;
7158 maskbitslog2 = maskbitslog2 + 2;
7159 if (bed->s->arch_size == 64)
7161 if (maskbitslog2 == 5)
7167 cinfo.mask = (1 << cinfo.shift1) - 1;
7168 cinfo.shift2 = maskbitslog2;
7169 cinfo.maskbits = 1 << maskbitslog2;
7170 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7171 amt = bucketcount * sizeof (unsigned long int) * 2;
7172 amt += maskwords * sizeof (bfd_vma);
7173 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7174 if (cinfo.bitmask == NULL)
7176 free (cinfo.hashcodes);
7180 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7181 cinfo.indx = cinfo.counts + bucketcount;
7182 cinfo.symindx = dynsymcount - cinfo.nsyms;
7183 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7185 /* Determine how often each hash bucket is used. */
7186 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7187 for (i = 0; i < cinfo.nsyms; ++i)
7188 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7190 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7191 if (cinfo.counts[i] != 0)
7193 cinfo.indx[i] = cnt;
7194 cnt += cinfo.counts[i];
7196 BFD_ASSERT (cnt == dynsymcount);
7197 cinfo.bucketcount = bucketcount;
7198 cinfo.local_indx = cinfo.min_dynindx;
7200 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7201 s->size += cinfo.maskbits / 8;
7202 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7203 if (contents == NULL)
7205 free (cinfo.bitmask);
7206 free (cinfo.hashcodes);
7210 s->contents = contents;
7211 bfd_put_32 (output_bfd, bucketcount, contents);
7212 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7213 bfd_put_32 (output_bfd, maskwords, contents + 8);
7214 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7215 contents += 16 + cinfo.maskbits / 8;
7217 for (i = 0; i < bucketcount; ++i)
7219 if (cinfo.counts[i] == 0)
7220 bfd_put_32 (output_bfd, 0, contents);
7222 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7226 cinfo.contents = contents;
7228 /* Renumber dynamic symbols, populate .gnu.hash section. */
7229 elf_link_hash_traverse (elf_hash_table (info),
7230 elf_renumber_gnu_hash_syms, &cinfo);
7232 contents = s->contents + 16;
7233 for (i = 0; i < maskwords; ++i)
7235 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7237 contents += bed->s->arch_size / 8;
7240 free (cinfo.bitmask);
7241 free (cinfo.hashcodes);
7245 s = bfd_get_linker_section (dynobj, ".dynstr");
7246 BFD_ASSERT (s != NULL);
7248 elf_finalize_dynstr (output_bfd, info);
7250 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7252 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7253 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7260 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7263 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7266 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7267 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7270 /* Finish SHF_MERGE section merging. */
7273 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7278 if (!is_elf_hash_table (info->hash))
7281 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7282 if ((ibfd->flags & DYNAMIC) == 0
7283 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7284 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7285 == get_elf_backend_data (obfd)->s->elfclass))
7286 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7287 if ((sec->flags & SEC_MERGE) != 0
7288 && !bfd_is_abs_section (sec->output_section))
7290 struct bfd_elf_section_data *secdata;
7292 secdata = elf_section_data (sec);
7293 if (! _bfd_add_merge_section (obfd,
7294 &elf_hash_table (info)->merge_info,
7295 sec, &secdata->sec_info))
7297 else if (secdata->sec_info)
7298 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7301 if (elf_hash_table (info)->merge_info != NULL)
7302 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7303 merge_sections_remove_hook);
7307 /* Create an entry in an ELF linker hash table. */
7309 struct bfd_hash_entry *
7310 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7311 struct bfd_hash_table *table,
7314 /* Allocate the structure if it has not already been allocated by a
7318 entry = (struct bfd_hash_entry *)
7319 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7324 /* Call the allocation method of the superclass. */
7325 entry = _bfd_link_hash_newfunc (entry, table, string);
7328 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7329 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7331 /* Set local fields. */
7334 ret->got = htab->init_got_refcount;
7335 ret->plt = htab->init_plt_refcount;
7336 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7337 - offsetof (struct elf_link_hash_entry, size)));
7338 /* Assume that we have been called by a non-ELF symbol reader.
7339 This flag is then reset by the code which reads an ELF input
7340 file. This ensures that a symbol created by a non-ELF symbol
7341 reader will have the flag set correctly. */
7348 /* Copy data from an indirect symbol to its direct symbol, hiding the
7349 old indirect symbol. Also used for copying flags to a weakdef. */
7352 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7353 struct elf_link_hash_entry *dir,
7354 struct elf_link_hash_entry *ind)
7356 struct elf_link_hash_table *htab;
7358 /* Copy down any references that we may have already seen to the
7359 symbol which just became indirect. */
7361 if (dir->versioned != versioned_hidden)
7362 dir->ref_dynamic |= ind->ref_dynamic;
7363 dir->ref_regular |= ind->ref_regular;
7364 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7365 dir->non_got_ref |= ind->non_got_ref;
7366 dir->needs_plt |= ind->needs_plt;
7367 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7369 if (ind->root.type != bfd_link_hash_indirect)
7372 /* Copy over the global and procedure linkage table refcount entries.
7373 These may have been already set up by a check_relocs routine. */
7374 htab = elf_hash_table (info);
7375 if (ind->got.refcount > htab->init_got_refcount.refcount)
7377 if (dir->got.refcount < 0)
7378 dir->got.refcount = 0;
7379 dir->got.refcount += ind->got.refcount;
7380 ind->got.refcount = htab->init_got_refcount.refcount;
7383 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7385 if (dir->plt.refcount < 0)
7386 dir->plt.refcount = 0;
7387 dir->plt.refcount += ind->plt.refcount;
7388 ind->plt.refcount = htab->init_plt_refcount.refcount;
7391 if (ind->dynindx != -1)
7393 if (dir->dynindx != -1)
7394 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7395 dir->dynindx = ind->dynindx;
7396 dir->dynstr_index = ind->dynstr_index;
7398 ind->dynstr_index = 0;
7403 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7404 struct elf_link_hash_entry *h,
7405 bfd_boolean force_local)
7407 /* STT_GNU_IFUNC symbol must go through PLT. */
7408 if (h->type != STT_GNU_IFUNC)
7410 h->plt = elf_hash_table (info)->init_plt_offset;
7415 h->forced_local = 1;
7416 if (h->dynindx != -1)
7418 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7421 h->dynstr_index = 0;
7426 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7430 _bfd_elf_link_hash_table_init
7431 (struct elf_link_hash_table *table,
7433 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7434 struct bfd_hash_table *,
7436 unsigned int entsize,
7437 enum elf_target_id target_id)
7440 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7442 table->init_got_refcount.refcount = can_refcount - 1;
7443 table->init_plt_refcount.refcount = can_refcount - 1;
7444 table->init_got_offset.offset = -(bfd_vma) 1;
7445 table->init_plt_offset.offset = -(bfd_vma) 1;
7446 /* The first dynamic symbol is a dummy. */
7447 table->dynsymcount = 1;
7449 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7451 table->root.type = bfd_link_elf_hash_table;
7452 table->hash_table_id = target_id;
7457 /* Create an ELF linker hash table. */
7459 struct bfd_link_hash_table *
7460 _bfd_elf_link_hash_table_create (bfd *abfd)
7462 struct elf_link_hash_table *ret;
7463 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7465 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7469 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7470 sizeof (struct elf_link_hash_entry),
7476 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7481 /* Destroy an ELF linker hash table. */
7484 _bfd_elf_link_hash_table_free (bfd *obfd)
7486 struct elf_link_hash_table *htab;
7488 htab = (struct elf_link_hash_table *) obfd->link.hash;
7489 if (htab->dynstr != NULL)
7490 _bfd_elf_strtab_free (htab->dynstr);
7491 _bfd_merge_sections_free (htab->merge_info);
7492 _bfd_generic_link_hash_table_free (obfd);
7495 /* This is a hook for the ELF emulation code in the generic linker to
7496 tell the backend linker what file name to use for the DT_NEEDED
7497 entry for a dynamic object. */
7500 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7502 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7503 && bfd_get_format (abfd) == bfd_object)
7504 elf_dt_name (abfd) = name;
7508 bfd_elf_get_dyn_lib_class (bfd *abfd)
7511 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7512 && bfd_get_format (abfd) == bfd_object)
7513 lib_class = elf_dyn_lib_class (abfd);
7520 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7522 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7523 && bfd_get_format (abfd) == bfd_object)
7524 elf_dyn_lib_class (abfd) = lib_class;
7527 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7528 the linker ELF emulation code. */
7530 struct bfd_link_needed_list *
7531 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7532 struct bfd_link_info *info)
7534 if (! is_elf_hash_table (info->hash))
7536 return elf_hash_table (info)->needed;
7539 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7540 hook for the linker ELF emulation code. */
7542 struct bfd_link_needed_list *
7543 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7544 struct bfd_link_info *info)
7546 if (! is_elf_hash_table (info->hash))
7548 return elf_hash_table (info)->runpath;
7551 /* Get the name actually used for a dynamic object for a link. This
7552 is the SONAME entry if there is one. Otherwise, it is the string
7553 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7556 bfd_elf_get_dt_soname (bfd *abfd)
7558 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7559 && bfd_get_format (abfd) == bfd_object)
7560 return elf_dt_name (abfd);
7564 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7565 the ELF linker emulation code. */
7568 bfd_elf_get_bfd_needed_list (bfd *abfd,
7569 struct bfd_link_needed_list **pneeded)
7572 bfd_byte *dynbuf = NULL;
7573 unsigned int elfsec;
7574 unsigned long shlink;
7575 bfd_byte *extdyn, *extdynend;
7577 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7581 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7582 || bfd_get_format (abfd) != bfd_object)
7585 s = bfd_get_section_by_name (abfd, ".dynamic");
7586 if (s == NULL || s->size == 0)
7589 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7592 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7593 if (elfsec == SHN_BAD)
7596 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7598 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7599 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7602 extdynend = extdyn + s->size;
7603 for (; extdyn < extdynend; extdyn += extdynsize)
7605 Elf_Internal_Dyn dyn;
7607 (*swap_dyn_in) (abfd, extdyn, &dyn);
7609 if (dyn.d_tag == DT_NULL)
7612 if (dyn.d_tag == DT_NEEDED)
7615 struct bfd_link_needed_list *l;
7616 unsigned int tagv = dyn.d_un.d_val;
7619 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7624 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7645 struct elf_symbuf_symbol
7647 unsigned long st_name; /* Symbol name, index in string tbl */
7648 unsigned char st_info; /* Type and binding attributes */
7649 unsigned char st_other; /* Visibilty, and target specific */
7652 struct elf_symbuf_head
7654 struct elf_symbuf_symbol *ssym;
7656 unsigned int st_shndx;
7663 Elf_Internal_Sym *isym;
7664 struct elf_symbuf_symbol *ssym;
7669 /* Sort references to symbols by ascending section number. */
7672 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7674 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7675 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7677 return s1->st_shndx - s2->st_shndx;
7681 elf_sym_name_compare (const void *arg1, const void *arg2)
7683 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7684 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7685 return strcmp (s1->name, s2->name);
7688 static struct elf_symbuf_head *
7689 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7691 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7692 struct elf_symbuf_symbol *ssym;
7693 struct elf_symbuf_head *ssymbuf, *ssymhead;
7694 size_t i, shndx_count, total_size;
7696 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7700 for (ind = indbuf, i = 0; i < symcount; i++)
7701 if (isymbuf[i].st_shndx != SHN_UNDEF)
7702 *ind++ = &isymbuf[i];
7705 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7706 elf_sort_elf_symbol);
7709 if (indbufend > indbuf)
7710 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7711 if (ind[0]->st_shndx != ind[1]->st_shndx)
7714 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7715 + (indbufend - indbuf) * sizeof (*ssym));
7716 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7717 if (ssymbuf == NULL)
7723 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7724 ssymbuf->ssym = NULL;
7725 ssymbuf->count = shndx_count;
7726 ssymbuf->st_shndx = 0;
7727 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7729 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7732 ssymhead->ssym = ssym;
7733 ssymhead->count = 0;
7734 ssymhead->st_shndx = (*ind)->st_shndx;
7736 ssym->st_name = (*ind)->st_name;
7737 ssym->st_info = (*ind)->st_info;
7738 ssym->st_other = (*ind)->st_other;
7741 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7742 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7749 /* Check if 2 sections define the same set of local and global
7753 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7754 struct bfd_link_info *info)
7757 const struct elf_backend_data *bed1, *bed2;
7758 Elf_Internal_Shdr *hdr1, *hdr2;
7759 size_t symcount1, symcount2;
7760 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7761 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7762 Elf_Internal_Sym *isym, *isymend;
7763 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7764 size_t count1, count2, i;
7765 unsigned int shndx1, shndx2;
7771 /* Both sections have to be in ELF. */
7772 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7773 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7776 if (elf_section_type (sec1) != elf_section_type (sec2))
7779 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7780 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7781 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7784 bed1 = get_elf_backend_data (bfd1);
7785 bed2 = get_elf_backend_data (bfd2);
7786 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7787 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7788 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7789 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7791 if (symcount1 == 0 || symcount2 == 0)
7797 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7798 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7800 if (ssymbuf1 == NULL)
7802 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7804 if (isymbuf1 == NULL)
7807 if (!info->reduce_memory_overheads)
7808 elf_tdata (bfd1)->symbuf = ssymbuf1
7809 = elf_create_symbuf (symcount1, isymbuf1);
7812 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7814 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7816 if (isymbuf2 == NULL)
7819 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7820 elf_tdata (bfd2)->symbuf = ssymbuf2
7821 = elf_create_symbuf (symcount2, isymbuf2);
7824 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7826 /* Optimized faster version. */
7828 struct elf_symbol *symp;
7829 struct elf_symbuf_symbol *ssym, *ssymend;
7832 hi = ssymbuf1->count;
7837 mid = (lo + hi) / 2;
7838 if (shndx1 < ssymbuf1[mid].st_shndx)
7840 else if (shndx1 > ssymbuf1[mid].st_shndx)
7844 count1 = ssymbuf1[mid].count;
7851 hi = ssymbuf2->count;
7856 mid = (lo + hi) / 2;
7857 if (shndx2 < ssymbuf2[mid].st_shndx)
7859 else if (shndx2 > ssymbuf2[mid].st_shndx)
7863 count2 = ssymbuf2[mid].count;
7869 if (count1 == 0 || count2 == 0 || count1 != count2)
7873 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7875 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7876 if (symtable1 == NULL || symtable2 == NULL)
7880 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7881 ssym < ssymend; ssym++, symp++)
7883 symp->u.ssym = ssym;
7884 symp->name = bfd_elf_string_from_elf_section (bfd1,
7890 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7891 ssym < ssymend; ssym++, symp++)
7893 symp->u.ssym = ssym;
7894 symp->name = bfd_elf_string_from_elf_section (bfd2,
7899 /* Sort symbol by name. */
7900 qsort (symtable1, count1, sizeof (struct elf_symbol),
7901 elf_sym_name_compare);
7902 qsort (symtable2, count1, sizeof (struct elf_symbol),
7903 elf_sym_name_compare);
7905 for (i = 0; i < count1; i++)
7906 /* Two symbols must have the same binding, type and name. */
7907 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7908 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7909 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7916 symtable1 = (struct elf_symbol *)
7917 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7918 symtable2 = (struct elf_symbol *)
7919 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7920 if (symtable1 == NULL || symtable2 == NULL)
7923 /* Count definitions in the section. */
7925 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7926 if (isym->st_shndx == shndx1)
7927 symtable1[count1++].u.isym = isym;
7930 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7931 if (isym->st_shndx == shndx2)
7932 symtable2[count2++].u.isym = isym;
7934 if (count1 == 0 || count2 == 0 || count1 != count2)
7937 for (i = 0; i < count1; i++)
7939 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7940 symtable1[i].u.isym->st_name);
7942 for (i = 0; i < count2; i++)
7944 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7945 symtable2[i].u.isym->st_name);
7947 /* Sort symbol by name. */
7948 qsort (symtable1, count1, sizeof (struct elf_symbol),
7949 elf_sym_name_compare);
7950 qsort (symtable2, count1, sizeof (struct elf_symbol),
7951 elf_sym_name_compare);
7953 for (i = 0; i < count1; i++)
7954 /* Two symbols must have the same binding, type and name. */
7955 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7956 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7957 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7975 /* Return TRUE if 2 section types are compatible. */
7978 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7979 bfd *bbfd, const asection *bsec)
7983 || abfd->xvec->flavour != bfd_target_elf_flavour
7984 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7987 return elf_section_type (asec) == elf_section_type (bsec);
7990 /* Final phase of ELF linker. */
7992 /* A structure we use to avoid passing large numbers of arguments. */
7994 struct elf_final_link_info
7996 /* General link information. */
7997 struct bfd_link_info *info;
8000 /* Symbol string table. */
8001 struct elf_strtab_hash *symstrtab;
8002 /* .hash section. */
8004 /* symbol version section (.gnu.version). */
8005 asection *symver_sec;
8006 /* Buffer large enough to hold contents of any section. */
8008 /* Buffer large enough to hold external relocs of any section. */
8009 void *external_relocs;
8010 /* Buffer large enough to hold internal relocs of any section. */
8011 Elf_Internal_Rela *internal_relocs;
8012 /* Buffer large enough to hold external local symbols of any input
8014 bfd_byte *external_syms;
8015 /* And a buffer for symbol section indices. */
8016 Elf_External_Sym_Shndx *locsym_shndx;
8017 /* Buffer large enough to hold internal local symbols of any input
8019 Elf_Internal_Sym *internal_syms;
8020 /* Array large enough to hold a symbol index for each local symbol
8021 of any input BFD. */
8023 /* Array large enough to hold a section pointer for each local
8024 symbol of any input BFD. */
8025 asection **sections;
8026 /* Buffer for SHT_SYMTAB_SHNDX section. */
8027 Elf_External_Sym_Shndx *symshndxbuf;
8028 /* Number of STT_FILE syms seen. */
8029 size_t filesym_count;
8032 /* This struct is used to pass information to elf_link_output_extsym. */
8034 struct elf_outext_info
8037 bfd_boolean localsyms;
8038 bfd_boolean file_sym_done;
8039 struct elf_final_link_info *flinfo;
8043 /* Support for evaluating a complex relocation.
8045 Complex relocations are generalized, self-describing relocations. The
8046 implementation of them consists of two parts: complex symbols, and the
8047 relocations themselves.
8049 The relocations are use a reserved elf-wide relocation type code (R_RELC
8050 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8051 information (start bit, end bit, word width, etc) into the addend. This
8052 information is extracted from CGEN-generated operand tables within gas.
8054 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8055 internal) representing prefix-notation expressions, including but not
8056 limited to those sorts of expressions normally encoded as addends in the
8057 addend field. The symbol mangling format is:
8060 | <unary-operator> ':' <node>
8061 | <binary-operator> ':' <node> ':' <node>
8064 <literal> := 's' <digits=N> ':' <N character symbol name>
8065 | 'S' <digits=N> ':' <N character section name>
8069 <binary-operator> := as in C
8070 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8073 set_symbol_value (bfd *bfd_with_globals,
8074 Elf_Internal_Sym *isymbuf,
8079 struct elf_link_hash_entry **sym_hashes;
8080 struct elf_link_hash_entry *h;
8081 size_t extsymoff = locsymcount;
8083 if (symidx < locsymcount)
8085 Elf_Internal_Sym *sym;
8087 sym = isymbuf + symidx;
8088 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8090 /* It is a local symbol: move it to the
8091 "absolute" section and give it a value. */
8092 sym->st_shndx = SHN_ABS;
8093 sym->st_value = val;
8096 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8100 /* It is a global symbol: set its link type
8101 to "defined" and give it a value. */
8103 sym_hashes = elf_sym_hashes (bfd_with_globals);
8104 h = sym_hashes [symidx - extsymoff];
8105 while (h->root.type == bfd_link_hash_indirect
8106 || h->root.type == bfd_link_hash_warning)
8107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8108 h->root.type = bfd_link_hash_defined;
8109 h->root.u.def.value = val;
8110 h->root.u.def.section = bfd_abs_section_ptr;
8114 resolve_symbol (const char *name,
8116 struct elf_final_link_info *flinfo,
8118 Elf_Internal_Sym *isymbuf,
8121 Elf_Internal_Sym *sym;
8122 struct bfd_link_hash_entry *global_entry;
8123 const char *candidate = NULL;
8124 Elf_Internal_Shdr *symtab_hdr;
8127 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8129 for (i = 0; i < locsymcount; ++ i)
8133 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8136 candidate = bfd_elf_string_from_elf_section (input_bfd,
8137 symtab_hdr->sh_link,
8140 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8141 name, candidate, (unsigned long) sym->st_value);
8143 if (candidate && strcmp (candidate, name) == 0)
8145 asection *sec = flinfo->sections [i];
8147 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8148 *result += sec->output_offset + sec->output_section->vma;
8150 printf ("Found symbol with value %8.8lx\n",
8151 (unsigned long) *result);
8157 /* Hmm, haven't found it yet. perhaps it is a global. */
8158 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8159 FALSE, FALSE, TRUE);
8163 if (global_entry->type == bfd_link_hash_defined
8164 || global_entry->type == bfd_link_hash_defweak)
8166 *result = (global_entry->u.def.value
8167 + global_entry->u.def.section->output_section->vma
8168 + global_entry->u.def.section->output_offset);
8170 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8171 global_entry->root.string, (unsigned long) *result);
8179 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8180 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8181 names like "foo.end" which is the end address of section "foo". */
8184 resolve_section (const char *name,
8192 for (curr = sections; curr; curr = curr->next)
8193 if (strcmp (curr->name, name) == 0)
8195 *result = curr->vma;
8199 /* Hmm. still haven't found it. try pseudo-section names. */
8200 /* FIXME: This could be coded more efficiently... */
8201 for (curr = sections; curr; curr = curr->next)
8203 len = strlen (curr->name);
8204 if (len > strlen (name))
8207 if (strncmp (curr->name, name, len) == 0)
8209 if (strncmp (".end", name + len, 4) == 0)
8211 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8215 /* Insert more pseudo-section names here, if you like. */
8223 undefined_reference (const char *reftype, const char *name)
8225 /* xgettext:c-format */
8226 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8231 eval_symbol (bfd_vma *result,
8234 struct elf_final_link_info *flinfo,
8236 Elf_Internal_Sym *isymbuf,
8245 const char *sym = *symp;
8247 bfd_boolean symbol_is_section = FALSE;
8252 if (len < 1 || len > sizeof (symbuf))
8254 bfd_set_error (bfd_error_invalid_operation);
8267 *result = strtoul (sym, (char **) symp, 16);
8271 symbol_is_section = TRUE;
8275 symlen = strtol (sym, (char **) symp, 10);
8276 sym = *symp + 1; /* Skip the trailing ':'. */
8278 if (symend < sym || symlen + 1 > sizeof (symbuf))
8280 bfd_set_error (bfd_error_invalid_operation);
8284 memcpy (symbuf, sym, symlen);
8285 symbuf[symlen] = '\0';
8286 *symp = sym + symlen;
8288 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8289 the symbol as a section, or vice-versa. so we're pretty liberal in our
8290 interpretation here; section means "try section first", not "must be a
8291 section", and likewise with symbol. */
8293 if (symbol_is_section)
8295 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8296 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8297 isymbuf, locsymcount))
8299 undefined_reference ("section", symbuf);
8305 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8306 isymbuf, locsymcount)
8307 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8310 undefined_reference ("symbol", symbuf);
8317 /* All that remains are operators. */
8319 #define UNARY_OP(op) \
8320 if (strncmp (sym, #op, strlen (#op)) == 0) \
8322 sym += strlen (#op); \
8326 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8327 isymbuf, locsymcount, signed_p)) \
8330 *result = op ((bfd_signed_vma) a); \
8336 #define BINARY_OP(op) \
8337 if (strncmp (sym, #op, strlen (#op)) == 0) \
8339 sym += strlen (#op); \
8343 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8344 isymbuf, locsymcount, signed_p)) \
8347 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8348 isymbuf, locsymcount, signed_p)) \
8351 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8381 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8382 bfd_set_error (bfd_error_invalid_operation);
8388 put_value (bfd_vma size,
8389 unsigned long chunksz,
8394 location += (size - chunksz);
8396 for (; size; size -= chunksz, location -= chunksz)
8401 bfd_put_8 (input_bfd, x, location);
8405 bfd_put_16 (input_bfd, x, location);
8409 bfd_put_32 (input_bfd, x, location);
8410 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8416 bfd_put_64 (input_bfd, x, location);
8417 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8430 get_value (bfd_vma size,
8431 unsigned long chunksz,
8438 /* Sanity checks. */
8439 BFD_ASSERT (chunksz <= sizeof (x)
8442 && (size % chunksz) == 0
8443 && input_bfd != NULL
8444 && location != NULL);
8446 if (chunksz == sizeof (x))
8448 BFD_ASSERT (size == chunksz);
8450 /* Make sure that we do not perform an undefined shift operation.
8451 We know that size == chunksz so there will only be one iteration
8452 of the loop below. */
8456 shift = 8 * chunksz;
8458 for (; size; size -= chunksz, location += chunksz)
8463 x = (x << shift) | bfd_get_8 (input_bfd, location);
8466 x = (x << shift) | bfd_get_16 (input_bfd, location);
8469 x = (x << shift) | bfd_get_32 (input_bfd, location);
8473 x = (x << shift) | bfd_get_64 (input_bfd, location);
8484 decode_complex_addend (unsigned long *start, /* in bits */
8485 unsigned long *oplen, /* in bits */
8486 unsigned long *len, /* in bits */
8487 unsigned long *wordsz, /* in bytes */
8488 unsigned long *chunksz, /* in bytes */
8489 unsigned long *lsb0_p,
8490 unsigned long *signed_p,
8491 unsigned long *trunc_p,
8492 unsigned long encoded)
8494 * start = encoded & 0x3F;
8495 * len = (encoded >> 6) & 0x3F;
8496 * oplen = (encoded >> 12) & 0x3F;
8497 * wordsz = (encoded >> 18) & 0xF;
8498 * chunksz = (encoded >> 22) & 0xF;
8499 * lsb0_p = (encoded >> 27) & 1;
8500 * signed_p = (encoded >> 28) & 1;
8501 * trunc_p = (encoded >> 29) & 1;
8504 bfd_reloc_status_type
8505 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8506 asection *input_section ATTRIBUTE_UNUSED,
8508 Elf_Internal_Rela *rel,
8511 bfd_vma shift, x, mask;
8512 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8513 bfd_reloc_status_type r;
8515 /* Perform this reloc, since it is complex.
8516 (this is not to say that it necessarily refers to a complex
8517 symbol; merely that it is a self-describing CGEN based reloc.
8518 i.e. the addend has the complete reloc information (bit start, end,
8519 word size, etc) encoded within it.). */
8521 decode_complex_addend (&start, &oplen, &len, &wordsz,
8522 &chunksz, &lsb0_p, &signed_p,
8523 &trunc_p, rel->r_addend);
8525 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8528 shift = (start + 1) - len;
8530 shift = (8 * wordsz) - (start + len);
8532 x = get_value (wordsz, chunksz, input_bfd,
8533 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8536 printf ("Doing complex reloc: "
8537 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8538 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8539 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8540 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8541 oplen, (unsigned long) x, (unsigned long) mask,
8542 (unsigned long) relocation);
8547 /* Now do an overflow check. */
8548 r = bfd_check_overflow ((signed_p
8549 ? complain_overflow_signed
8550 : complain_overflow_unsigned),
8551 len, 0, (8 * wordsz),
8555 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8558 printf (" relocation: %8.8lx\n"
8559 " shifted mask: %8.8lx\n"
8560 " shifted/masked reloc: %8.8lx\n"
8561 " result: %8.8lx\n",
8562 (unsigned long) relocation, (unsigned long) (mask << shift),
8563 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8565 put_value (wordsz, chunksz, input_bfd, x,
8566 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8570 /* Functions to read r_offset from external (target order) reloc
8571 entry. Faster than bfd_getl32 et al, because we let the compiler
8572 know the value is aligned. */
8575 ext32l_r_offset (const void *p)
8582 const union aligned32 *a
8583 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8585 uint32_t aval = ( (uint32_t) a->c[0]
8586 | (uint32_t) a->c[1] << 8
8587 | (uint32_t) a->c[2] << 16
8588 | (uint32_t) a->c[3] << 24);
8593 ext32b_r_offset (const void *p)
8600 const union aligned32 *a
8601 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8603 uint32_t aval = ( (uint32_t) a->c[0] << 24
8604 | (uint32_t) a->c[1] << 16
8605 | (uint32_t) a->c[2] << 8
8606 | (uint32_t) a->c[3]);
8610 #ifdef BFD_HOST_64_BIT
8612 ext64l_r_offset (const void *p)
8619 const union aligned64 *a
8620 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8622 uint64_t aval = ( (uint64_t) a->c[0]
8623 | (uint64_t) a->c[1] << 8
8624 | (uint64_t) a->c[2] << 16
8625 | (uint64_t) a->c[3] << 24
8626 | (uint64_t) a->c[4] << 32
8627 | (uint64_t) a->c[5] << 40
8628 | (uint64_t) a->c[6] << 48
8629 | (uint64_t) a->c[7] << 56);
8634 ext64b_r_offset (const void *p)
8641 const union aligned64 *a
8642 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8644 uint64_t aval = ( (uint64_t) a->c[0] << 56
8645 | (uint64_t) a->c[1] << 48
8646 | (uint64_t) a->c[2] << 40
8647 | (uint64_t) a->c[3] << 32
8648 | (uint64_t) a->c[4] << 24
8649 | (uint64_t) a->c[5] << 16
8650 | (uint64_t) a->c[6] << 8
8651 | (uint64_t) a->c[7]);
8656 /* When performing a relocatable link, the input relocations are
8657 preserved. But, if they reference global symbols, the indices
8658 referenced must be updated. Update all the relocations found in
8662 elf_link_adjust_relocs (bfd *abfd,
8664 struct bfd_elf_section_reloc_data *reldata,
8666 struct bfd_link_info *info)
8669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8671 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8672 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8673 bfd_vma r_type_mask;
8675 unsigned int count = reldata->count;
8676 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8678 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8680 swap_in = bed->s->swap_reloc_in;
8681 swap_out = bed->s->swap_reloc_out;
8683 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8685 swap_in = bed->s->swap_reloca_in;
8686 swap_out = bed->s->swap_reloca_out;
8691 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8694 if (bed->s->arch_size == 32)
8701 r_type_mask = 0xffffffff;
8705 erela = reldata->hdr->contents;
8706 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8708 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8711 if (*rel_hash == NULL)
8714 if ((*rel_hash)->indx == -2
8715 && info->gc_sections
8716 && ! info->gc_keep_exported)
8718 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8719 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection."),
8721 (*rel_hash)->root.root.string);
8722 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled."),
8724 bfd_set_error (bfd_error_invalid_operation);
8727 BFD_ASSERT ((*rel_hash)->indx >= 0);
8729 (*swap_in) (abfd, erela, irela);
8730 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8731 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8732 | (irela[j].r_info & r_type_mask));
8733 (*swap_out) (abfd, irela, erela);
8736 if (bed->elf_backend_update_relocs)
8737 (*bed->elf_backend_update_relocs) (sec, reldata);
8739 if (sort && count != 0)
8741 bfd_vma (*ext_r_off) (const void *);
8744 bfd_byte *base, *end, *p, *loc;
8745 bfd_byte *buf = NULL;
8747 if (bed->s->arch_size == 32)
8749 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8750 ext_r_off = ext32l_r_offset;
8751 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8752 ext_r_off = ext32b_r_offset;
8758 #ifdef BFD_HOST_64_BIT
8759 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8760 ext_r_off = ext64l_r_offset;
8761 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8762 ext_r_off = ext64b_r_offset;
8768 /* Must use a stable sort here. A modified insertion sort,
8769 since the relocs are mostly sorted already. */
8770 elt_size = reldata->hdr->sh_entsize;
8771 base = reldata->hdr->contents;
8772 end = base + count * elt_size;
8773 if (elt_size > sizeof (Elf64_External_Rela))
8776 /* Ensure the first element is lowest. This acts as a sentinel,
8777 speeding the main loop below. */
8778 r_off = (*ext_r_off) (base);
8779 for (p = loc = base; (p += elt_size) < end; )
8781 bfd_vma r_off2 = (*ext_r_off) (p);
8790 /* Don't just swap *base and *loc as that changes the order
8791 of the original base[0] and base[1] if they happen to
8792 have the same r_offset. */
8793 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8794 memcpy (onebuf, loc, elt_size);
8795 memmove (base + elt_size, base, loc - base);
8796 memcpy (base, onebuf, elt_size);
8799 for (p = base + elt_size; (p += elt_size) < end; )
8801 /* base to p is sorted, *p is next to insert. */
8802 r_off = (*ext_r_off) (p);
8803 /* Search the sorted region for location to insert. */
8805 while (r_off < (*ext_r_off) (loc))
8810 /* Chances are there is a run of relocs to insert here,
8811 from one of more input files. Files are not always
8812 linked in order due to the way elf_link_input_bfd is
8813 called. See pr17666. */
8814 size_t sortlen = p - loc;
8815 bfd_vma r_off2 = (*ext_r_off) (loc);
8816 size_t runlen = elt_size;
8817 size_t buf_size = 96 * 1024;
8818 while (p + runlen < end
8819 && (sortlen <= buf_size
8820 || runlen + elt_size <= buf_size)
8821 && r_off2 > (*ext_r_off) (p + runlen))
8825 buf = bfd_malloc (buf_size);
8829 if (runlen < sortlen)
8831 memcpy (buf, p, runlen);
8832 memmove (loc + runlen, loc, sortlen);
8833 memcpy (loc, buf, runlen);
8837 memcpy (buf, loc, sortlen);
8838 memmove (loc, p, runlen);
8839 memcpy (loc + runlen, buf, sortlen);
8841 p += runlen - elt_size;
8844 /* Hashes are no longer valid. */
8845 free (reldata->hashes);
8846 reldata->hashes = NULL;
8852 struct elf_link_sort_rela
8858 enum elf_reloc_type_class type;
8859 /* We use this as an array of size int_rels_per_ext_rel. */
8860 Elf_Internal_Rela rela[1];
8864 elf_link_sort_cmp1 (const void *A, const void *B)
8866 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8867 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8868 int relativea, relativeb;
8870 relativea = a->type == reloc_class_relative;
8871 relativeb = b->type == reloc_class_relative;
8873 if (relativea < relativeb)
8875 if (relativea > relativeb)
8877 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8879 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8881 if (a->rela->r_offset < b->rela->r_offset)
8883 if (a->rela->r_offset > b->rela->r_offset)
8889 elf_link_sort_cmp2 (const void *A, const void *B)
8891 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8892 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8894 if (a->type < b->type)
8896 if (a->type > b->type)
8898 if (a->u.offset < b->u.offset)
8900 if (a->u.offset > b->u.offset)
8902 if (a->rela->r_offset < b->rela->r_offset)
8904 if (a->rela->r_offset > b->rela->r_offset)
8910 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8912 asection *dynamic_relocs;
8915 bfd_size_type count, size;
8916 size_t i, ret, sort_elt, ext_size;
8917 bfd_byte *sort, *s_non_relative, *p;
8918 struct elf_link_sort_rela *sq;
8919 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8920 int i2e = bed->s->int_rels_per_ext_rel;
8921 unsigned int opb = bfd_octets_per_byte (abfd);
8922 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8923 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8924 struct bfd_link_order *lo;
8926 bfd_boolean use_rela;
8928 /* Find a dynamic reloc section. */
8929 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8930 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8931 if (rela_dyn != NULL && rela_dyn->size > 0
8932 && rel_dyn != NULL && rel_dyn->size > 0)
8934 bfd_boolean use_rela_initialised = FALSE;
8936 /* This is just here to stop gcc from complaining.
8937 Its initialization checking code is not perfect. */
8940 /* Both sections are present. Examine the sizes
8941 of the indirect sections to help us choose. */
8942 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8943 if (lo->type == bfd_indirect_link_order)
8945 asection *o = lo->u.indirect.section;
8947 if ((o->size % bed->s->sizeof_rela) == 0)
8949 if ((o->size % bed->s->sizeof_rel) == 0)
8950 /* Section size is divisible by both rel and rela sizes.
8951 It is of no help to us. */
8955 /* Section size is only divisible by rela. */
8956 if (use_rela_initialised && !use_rela)
8958 _bfd_error_handler (_("%pB: Unable to sort relocs - "
8959 "they are in more than one size"),
8961 bfd_set_error (bfd_error_invalid_operation);
8967 use_rela_initialised = TRUE;
8971 else if ((o->size % bed->s->sizeof_rel) == 0)
8973 /* Section size is only divisible by rel. */
8974 if (use_rela_initialised && use_rela)
8976 _bfd_error_handler (_("%pB: Unable to sort relocs - "
8977 "they are in more than one size"),
8979 bfd_set_error (bfd_error_invalid_operation);
8985 use_rela_initialised = TRUE;
8990 /* The section size is not divisible by either -
8991 something is wrong. */
8992 _bfd_error_handler (_("%pB: Unable to sort relocs - "
8993 "they are of an unknown size"), abfd);
8994 bfd_set_error (bfd_error_invalid_operation);
8999 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9000 if (lo->type == bfd_indirect_link_order)
9002 asection *o = lo->u.indirect.section;
9004 if ((o->size % bed->s->sizeof_rela) == 0)
9006 if ((o->size % bed->s->sizeof_rel) == 0)
9007 /* Section size is divisible by both rel and rela sizes.
9008 It is of no help to us. */
9012 /* Section size is only divisible by rela. */
9013 if (use_rela_initialised && !use_rela)
9015 _bfd_error_handler (_("%pB: Unable to sort relocs - "
9016 "they are in more than one size"),
9018 bfd_set_error (bfd_error_invalid_operation);
9024 use_rela_initialised = TRUE;
9028 else if ((o->size % bed->s->sizeof_rel) == 0)
9030 /* Section size is only divisible by rel. */
9031 if (use_rela_initialised && use_rela)
9033 _bfd_error_handler (_("%pB: Unable to sort relocs - "
9034 "they are in more than one size"),
9036 bfd_set_error (bfd_error_invalid_operation);
9042 use_rela_initialised = TRUE;
9047 /* The section size is not divisible by either -
9048 something is wrong. */
9049 _bfd_error_handler (_("%pB: Unable to sort relocs - "
9050 "they are of an unknown size"), abfd);
9051 bfd_set_error (bfd_error_invalid_operation);
9056 if (! use_rela_initialised)
9060 else if (rela_dyn != NULL && rela_dyn->size > 0)
9062 else if (rel_dyn != NULL && rel_dyn->size > 0)
9069 dynamic_relocs = rela_dyn;
9070 ext_size = bed->s->sizeof_rela;
9071 swap_in = bed->s->swap_reloca_in;
9072 swap_out = bed->s->swap_reloca_out;
9076 dynamic_relocs = rel_dyn;
9077 ext_size = bed->s->sizeof_rel;
9078 swap_in = bed->s->swap_reloc_in;
9079 swap_out = bed->s->swap_reloc_out;
9083 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9084 if (lo->type == bfd_indirect_link_order)
9085 size += lo->u.indirect.section->size;
9087 if (size != dynamic_relocs->size)
9090 sort_elt = (sizeof (struct elf_link_sort_rela)
9091 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9093 count = dynamic_relocs->size / ext_size;
9096 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9100 (*info->callbacks->warning)
9101 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9105 if (bed->s->arch_size == 32)
9106 r_sym_mask = ~(bfd_vma) 0xff;
9108 r_sym_mask = ~(bfd_vma) 0xffffffff;
9110 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9111 if (lo->type == bfd_indirect_link_order)
9113 bfd_byte *erel, *erelend;
9114 asection *o = lo->u.indirect.section;
9116 if (o->contents == NULL && o->size != 0)
9118 /* This is a reloc section that is being handled as a normal
9119 section. See bfd_section_from_shdr. We can't combine
9120 relocs in this case. */
9125 erelend = o->contents + o->size;
9126 p = sort + o->output_offset * opb / ext_size * sort_elt;
9128 while (erel < erelend)
9130 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9132 (*swap_in) (abfd, erel, s->rela);
9133 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9134 s->u.sym_mask = r_sym_mask;
9140 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9142 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9144 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9145 if (s->type != reloc_class_relative)
9151 sq = (struct elf_link_sort_rela *) s_non_relative;
9152 for (; i < count; i++, p += sort_elt)
9154 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9155 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9157 sp->u.offset = sq->rela->r_offset;
9160 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9162 struct elf_link_hash_table *htab = elf_hash_table (info);
9163 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9165 /* We have plt relocs in .rela.dyn. */
9166 sq = (struct elf_link_sort_rela *) sort;
9167 for (i = 0; i < count; i++)
9168 if (sq[count - i - 1].type != reloc_class_plt)
9170 if (i != 0 && htab->srelplt->size == i * ext_size)
9172 struct bfd_link_order **plo;
9173 /* Put srelplt link_order last. This is so the output_offset
9174 set in the next loop is correct for DT_JMPREL. */
9175 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9176 if ((*plo)->type == bfd_indirect_link_order
9177 && (*plo)->u.indirect.section == htab->srelplt)
9183 plo = &(*plo)->next;
9186 dynamic_relocs->map_tail.link_order = lo;
9191 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9192 if (lo->type == bfd_indirect_link_order)
9194 bfd_byte *erel, *erelend;
9195 asection *o = lo->u.indirect.section;
9198 erelend = o->contents + o->size;
9199 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9200 while (erel < erelend)
9202 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9203 (*swap_out) (abfd, s->rela, erel);
9210 *psec = dynamic_relocs;
9214 /* Add a symbol to the output symbol string table. */
9217 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9219 Elf_Internal_Sym *elfsym,
9220 asection *input_sec,
9221 struct elf_link_hash_entry *h)
9223 int (*output_symbol_hook)
9224 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9225 struct elf_link_hash_entry *);
9226 struct elf_link_hash_table *hash_table;
9227 const struct elf_backend_data *bed;
9228 bfd_size_type strtabsize;
9230 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9232 bed = get_elf_backend_data (flinfo->output_bfd);
9233 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9234 if (output_symbol_hook != NULL)
9236 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9243 || (input_sec->flags & SEC_EXCLUDE))
9244 elfsym->st_name = (unsigned long) -1;
9247 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9248 to get the final offset for st_name. */
9250 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9252 if (elfsym->st_name == (unsigned long) -1)
9256 hash_table = elf_hash_table (flinfo->info);
9257 strtabsize = hash_table->strtabsize;
9258 if (strtabsize <= hash_table->strtabcount)
9260 strtabsize += strtabsize;
9261 hash_table->strtabsize = strtabsize;
9262 strtabsize *= sizeof (*hash_table->strtab);
9264 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9266 if (hash_table->strtab == NULL)
9269 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9270 hash_table->strtab[hash_table->strtabcount].dest_index
9271 = hash_table->strtabcount;
9272 hash_table->strtab[hash_table->strtabcount].destshndx_index
9273 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9275 bfd_get_symcount (flinfo->output_bfd) += 1;
9276 hash_table->strtabcount += 1;
9281 /* Swap symbols out to the symbol table and flush the output symbols to
9285 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9287 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9290 const struct elf_backend_data *bed;
9292 Elf_Internal_Shdr *hdr;
9296 if (!hash_table->strtabcount)
9299 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9301 bed = get_elf_backend_data (flinfo->output_bfd);
9303 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9304 symbuf = (bfd_byte *) bfd_malloc (amt);
9308 if (flinfo->symshndxbuf)
9310 amt = sizeof (Elf_External_Sym_Shndx);
9311 amt *= bfd_get_symcount (flinfo->output_bfd);
9312 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9313 if (flinfo->symshndxbuf == NULL)
9320 for (i = 0; i < hash_table->strtabcount; i++)
9322 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9323 if (elfsym->sym.st_name == (unsigned long) -1)
9324 elfsym->sym.st_name = 0;
9327 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9328 elfsym->sym.st_name);
9329 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9330 ((bfd_byte *) symbuf
9331 + (elfsym->dest_index
9332 * bed->s->sizeof_sym)),
9333 (flinfo->symshndxbuf
9334 + elfsym->destshndx_index));
9337 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9338 pos = hdr->sh_offset + hdr->sh_size;
9339 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9340 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9341 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9343 hdr->sh_size += amt;
9351 free (hash_table->strtab);
9352 hash_table->strtab = NULL;
9357 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9360 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9362 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9363 && sym->st_shndx < SHN_LORESERVE)
9365 /* The gABI doesn't support dynamic symbols in output sections
9368 /* xgettext:c-format */
9369 (_("%pB: Too many sections: %d (>= %d)"),
9370 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9371 bfd_set_error (bfd_error_nonrepresentable_section);
9377 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9378 allowing an unsatisfied unversioned symbol in the DSO to match a
9379 versioned symbol that would normally require an explicit version.
9380 We also handle the case that a DSO references a hidden symbol
9381 which may be satisfied by a versioned symbol in another DSO. */
9384 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9385 const struct elf_backend_data *bed,
9386 struct elf_link_hash_entry *h)
9389 struct elf_link_loaded_list *loaded;
9391 if (!is_elf_hash_table (info->hash))
9394 /* Check indirect symbol. */
9395 while (h->root.type == bfd_link_hash_indirect)
9396 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9398 switch (h->root.type)
9404 case bfd_link_hash_undefined:
9405 case bfd_link_hash_undefweak:
9406 abfd = h->root.u.undef.abfd;
9408 || (abfd->flags & DYNAMIC) == 0
9409 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9413 case bfd_link_hash_defined:
9414 case bfd_link_hash_defweak:
9415 abfd = h->root.u.def.section->owner;
9418 case bfd_link_hash_common:
9419 abfd = h->root.u.c.p->section->owner;
9422 BFD_ASSERT (abfd != NULL);
9424 for (loaded = elf_hash_table (info)->loaded;
9426 loaded = loaded->next)
9429 Elf_Internal_Shdr *hdr;
9433 Elf_Internal_Shdr *versymhdr;
9434 Elf_Internal_Sym *isym;
9435 Elf_Internal_Sym *isymend;
9436 Elf_Internal_Sym *isymbuf;
9437 Elf_External_Versym *ever;
9438 Elf_External_Versym *extversym;
9440 input = loaded->abfd;
9442 /* We check each DSO for a possible hidden versioned definition. */
9444 || (input->flags & DYNAMIC) == 0
9445 || elf_dynversym (input) == 0)
9448 hdr = &elf_tdata (input)->dynsymtab_hdr;
9450 symcount = hdr->sh_size / bed->s->sizeof_sym;
9451 if (elf_bad_symtab (input))
9453 extsymcount = symcount;
9458 extsymcount = symcount - hdr->sh_info;
9459 extsymoff = hdr->sh_info;
9462 if (extsymcount == 0)
9465 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9467 if (isymbuf == NULL)
9470 /* Read in any version definitions. */
9471 versymhdr = &elf_tdata (input)->dynversym_hdr;
9472 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9473 if (extversym == NULL)
9476 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9477 || (bfd_bread (extversym, versymhdr->sh_size, input)
9478 != versymhdr->sh_size))
9486 ever = extversym + extsymoff;
9487 isymend = isymbuf + extsymcount;
9488 for (isym = isymbuf; isym < isymend; isym++, ever++)
9491 Elf_Internal_Versym iver;
9492 unsigned short version_index;
9494 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9495 || isym->st_shndx == SHN_UNDEF)
9498 name = bfd_elf_string_from_elf_section (input,
9501 if (strcmp (name, h->root.root.string) != 0)
9504 _bfd_elf_swap_versym_in (input, ever, &iver);
9506 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9508 && h->forced_local))
9510 /* If we have a non-hidden versioned sym, then it should
9511 have provided a definition for the undefined sym unless
9512 it is defined in a non-shared object and forced local.
9517 version_index = iver.vs_vers & VERSYM_VERSION;
9518 if (version_index == 1 || version_index == 2)
9520 /* This is the base or first version. We can use it. */
9534 /* Convert ELF common symbol TYPE. */
9537 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9539 /* Commom symbol can only appear in relocatable link. */
9540 if (!bfd_link_relocatable (info))
9542 switch (info->elf_stt_common)
9546 case elf_stt_common:
9549 case no_elf_stt_common:
9556 /* Add an external symbol to the symbol table. This is called from
9557 the hash table traversal routine. When generating a shared object,
9558 we go through the symbol table twice. The first time we output
9559 anything that might have been forced to local scope in a version
9560 script. The second time we output the symbols that are still
9564 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9566 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9567 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9568 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9570 Elf_Internal_Sym sym;
9571 asection *input_sec;
9572 const struct elf_backend_data *bed;
9577 if (h->root.type == bfd_link_hash_warning)
9579 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9580 if (h->root.type == bfd_link_hash_new)
9584 /* Decide whether to output this symbol in this pass. */
9585 if (eoinfo->localsyms)
9587 if (!h->forced_local)
9592 if (h->forced_local)
9596 bed = get_elf_backend_data (flinfo->output_bfd);
9598 if (h->root.type == bfd_link_hash_undefined)
9600 /* If we have an undefined symbol reference here then it must have
9601 come from a shared library that is being linked in. (Undefined
9602 references in regular files have already been handled unless
9603 they are in unreferenced sections which are removed by garbage
9605 bfd_boolean ignore_undef = FALSE;
9607 /* Some symbols may be special in that the fact that they're
9608 undefined can be safely ignored - let backend determine that. */
9609 if (bed->elf_backend_ignore_undef_symbol)
9610 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9612 /* If we are reporting errors for this situation then do so now. */
9615 && (!h->ref_regular || flinfo->info->gc_sections)
9616 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9617 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9618 (*flinfo->info->callbacks->undefined_symbol)
9619 (flinfo->info, h->root.root.string,
9620 h->ref_regular ? NULL : h->root.u.undef.abfd,
9622 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9624 /* Strip a global symbol defined in a discarded section. */
9629 /* We should also warn if a forced local symbol is referenced from
9630 shared libraries. */
9631 if (bfd_link_executable (flinfo->info)
9636 && h->ref_dynamic_nonweak
9637 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9641 struct elf_link_hash_entry *hi = h;
9643 /* Check indirect symbol. */
9644 while (hi->root.type == bfd_link_hash_indirect)
9645 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9647 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9648 /* xgettext:c-format */
9649 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9650 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9651 /* xgettext:c-format */
9652 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9654 /* xgettext:c-format */
9655 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9656 def_bfd = flinfo->output_bfd;
9657 if (hi->root.u.def.section != bfd_abs_section_ptr)
9658 def_bfd = hi->root.u.def.section->owner;
9659 _bfd_error_handler (msg, flinfo->output_bfd,
9660 h->root.root.string, def_bfd);
9661 bfd_set_error (bfd_error_bad_value);
9662 eoinfo->failed = TRUE;
9666 /* We don't want to output symbols that have never been mentioned by
9667 a regular file, or that we have been told to strip. However, if
9668 h->indx is set to -2, the symbol is used by a reloc and we must
9673 else if ((h->def_dynamic
9675 || h->root.type == bfd_link_hash_new)
9679 else if (flinfo->info->strip == strip_all)
9681 else if (flinfo->info->strip == strip_some
9682 && bfd_hash_lookup (flinfo->info->keep_hash,
9683 h->root.root.string, FALSE, FALSE) == NULL)
9685 else if ((h->root.type == bfd_link_hash_defined
9686 || h->root.type == bfd_link_hash_defweak)
9687 && ((flinfo->info->strip_discarded
9688 && discarded_section (h->root.u.def.section))
9689 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9690 && h->root.u.def.section->owner != NULL
9691 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9693 else if ((h->root.type == bfd_link_hash_undefined
9694 || h->root.type == bfd_link_hash_undefweak)
9695 && h->root.u.undef.abfd != NULL
9696 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9701 /* If we're stripping it, and it's not a dynamic symbol, there's
9702 nothing else to do. However, if it is a forced local symbol or
9703 an ifunc symbol we need to give the backend finish_dynamic_symbol
9704 function a chance to make it dynamic. */
9707 && type != STT_GNU_IFUNC
9708 && !h->forced_local)
9712 sym.st_size = h->size;
9713 sym.st_other = h->other;
9714 switch (h->root.type)
9717 case bfd_link_hash_new:
9718 case bfd_link_hash_warning:
9722 case bfd_link_hash_undefined:
9723 case bfd_link_hash_undefweak:
9724 input_sec = bfd_und_section_ptr;
9725 sym.st_shndx = SHN_UNDEF;
9728 case bfd_link_hash_defined:
9729 case bfd_link_hash_defweak:
9731 input_sec = h->root.u.def.section;
9732 if (input_sec->output_section != NULL)
9735 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9736 input_sec->output_section);
9737 if (sym.st_shndx == SHN_BAD)
9740 /* xgettext:c-format */
9741 (_("%pB: could not find output section %pA for input section %pA"),
9742 flinfo->output_bfd, input_sec->output_section, input_sec);
9743 bfd_set_error (bfd_error_nonrepresentable_section);
9744 eoinfo->failed = TRUE;
9748 /* ELF symbols in relocatable files are section relative,
9749 but in nonrelocatable files they are virtual
9751 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9752 if (!bfd_link_relocatable (flinfo->info))
9754 sym.st_value += input_sec->output_section->vma;
9755 if (h->type == STT_TLS)
9757 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9758 if (tls_sec != NULL)
9759 sym.st_value -= tls_sec->vma;
9765 BFD_ASSERT (input_sec->owner == NULL
9766 || (input_sec->owner->flags & DYNAMIC) != 0);
9767 sym.st_shndx = SHN_UNDEF;
9768 input_sec = bfd_und_section_ptr;
9773 case bfd_link_hash_common:
9774 input_sec = h->root.u.c.p->section;
9775 sym.st_shndx = bed->common_section_index (input_sec);
9776 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9779 case bfd_link_hash_indirect:
9780 /* These symbols are created by symbol versioning. They point
9781 to the decorated version of the name. For example, if the
9782 symbol foo@@GNU_1.2 is the default, which should be used when
9783 foo is used with no version, then we add an indirect symbol
9784 foo which points to foo@@GNU_1.2. We ignore these symbols,
9785 since the indirected symbol is already in the hash table. */
9789 if (type == STT_COMMON || type == STT_OBJECT)
9790 switch (h->root.type)
9792 case bfd_link_hash_common:
9793 type = elf_link_convert_common_type (flinfo->info, type);
9795 case bfd_link_hash_defined:
9796 case bfd_link_hash_defweak:
9797 if (bed->common_definition (&sym))
9798 type = elf_link_convert_common_type (flinfo->info, type);
9802 case bfd_link_hash_undefined:
9803 case bfd_link_hash_undefweak:
9809 if (h->forced_local)
9811 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9812 /* Turn off visibility on local symbol. */
9813 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9815 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9816 else if (h->unique_global && h->def_regular)
9817 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9818 else if (h->root.type == bfd_link_hash_undefweak
9819 || h->root.type == bfd_link_hash_defweak)
9820 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9822 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9823 sym.st_target_internal = h->target_internal;
9825 /* Give the processor backend a chance to tweak the symbol value,
9826 and also to finish up anything that needs to be done for this
9827 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9828 forced local syms when non-shared is due to a historical quirk.
9829 STT_GNU_IFUNC symbol must go through PLT. */
9830 if ((h->type == STT_GNU_IFUNC
9832 && !bfd_link_relocatable (flinfo->info))
9833 || ((h->dynindx != -1
9835 && ((bfd_link_pic (flinfo->info)
9836 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9837 || h->root.type != bfd_link_hash_undefweak))
9838 || !h->forced_local)
9839 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9841 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9842 (flinfo->output_bfd, flinfo->info, h, &sym)))
9844 eoinfo->failed = TRUE;
9849 /* If we are marking the symbol as undefined, and there are no
9850 non-weak references to this symbol from a regular object, then
9851 mark the symbol as weak undefined; if there are non-weak
9852 references, mark the symbol as strong. We can't do this earlier,
9853 because it might not be marked as undefined until the
9854 finish_dynamic_symbol routine gets through with it. */
9855 if (sym.st_shndx == SHN_UNDEF
9857 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9858 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9861 type = ELF_ST_TYPE (sym.st_info);
9863 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9864 if (type == STT_GNU_IFUNC)
9867 if (h->ref_regular_nonweak)
9868 bindtype = STB_GLOBAL;
9870 bindtype = STB_WEAK;
9871 sym.st_info = ELF_ST_INFO (bindtype, type);
9874 /* If this is a symbol defined in a dynamic library, don't use the
9875 symbol size from the dynamic library. Relinking an executable
9876 against a new library may introduce gratuitous changes in the
9877 executable's symbols if we keep the size. */
9878 if (sym.st_shndx == SHN_UNDEF
9883 /* If a non-weak symbol with non-default visibility is not defined
9884 locally, it is a fatal error. */
9885 if (!bfd_link_relocatable (flinfo->info)
9886 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9887 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9888 && h->root.type == bfd_link_hash_undefined
9893 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9894 /* xgettext:c-format */
9895 msg = _("%pB: protected symbol `%s' isn't defined");
9896 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9897 /* xgettext:c-format */
9898 msg = _("%pB: internal symbol `%s' isn't defined");
9900 /* xgettext:c-format */
9901 msg = _("%pB: hidden symbol `%s' isn't defined");
9902 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9903 bfd_set_error (bfd_error_bad_value);
9904 eoinfo->failed = TRUE;
9908 /* If this symbol should be put in the .dynsym section, then put it
9909 there now. We already know the symbol index. We also fill in
9910 the entry in the .hash section. */
9911 if (elf_hash_table (flinfo->info)->dynsym != NULL
9913 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9917 /* Since there is no version information in the dynamic string,
9918 if there is no version info in symbol version section, we will
9919 have a run-time problem if not linking executable, referenced
9920 by shared library, or not bound locally. */
9921 if (h->verinfo.verdef == NULL
9922 && (!bfd_link_executable (flinfo->info)
9924 || !h->def_regular))
9926 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9928 if (p && p [1] != '\0')
9931 /* xgettext:c-format */
9932 (_("%pB: No symbol version section for versioned symbol `%s'"),
9933 flinfo->output_bfd, h->root.root.string);
9934 eoinfo->failed = TRUE;
9939 sym.st_name = h->dynstr_index;
9940 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9941 + h->dynindx * bed->s->sizeof_sym);
9942 if (!check_dynsym (flinfo->output_bfd, &sym))
9944 eoinfo->failed = TRUE;
9947 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9949 if (flinfo->hash_sec != NULL)
9951 size_t hash_entry_size;
9952 bfd_byte *bucketpos;
9957 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9958 bucket = h->u.elf_hash_value % bucketcount;
9961 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9962 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9963 + (bucket + 2) * hash_entry_size);
9964 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9965 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9967 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9968 ((bfd_byte *) flinfo->hash_sec->contents
9969 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9972 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9974 Elf_Internal_Versym iversym;
9975 Elf_External_Versym *eversym;
9977 if (!h->def_regular)
9979 if (h->verinfo.verdef == NULL
9980 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9981 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9982 iversym.vs_vers = 0;
9984 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9988 if (h->verinfo.vertree == NULL)
9989 iversym.vs_vers = 1;
9991 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9992 if (flinfo->info->create_default_symver)
9996 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9998 if (h->versioned == versioned_hidden && h->def_regular)
9999 iversym.vs_vers |= VERSYM_HIDDEN;
10001 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10002 eversym += h->dynindx;
10003 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10007 /* If the symbol is undefined, and we didn't output it to .dynsym,
10008 strip it from .symtab too. Obviously we can't do this for
10009 relocatable output or when needed for --emit-relocs. */
10010 else if (input_sec == bfd_und_section_ptr
10012 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10013 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10014 && !bfd_link_relocatable (flinfo->info))
10017 /* Also strip others that we couldn't earlier due to dynamic symbol
10021 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10024 /* Output a FILE symbol so that following locals are not associated
10025 with the wrong input file. We need one for forced local symbols
10026 if we've seen more than one FILE symbol or when we have exactly
10027 one FILE symbol but global symbols are present in a file other
10028 than the one with the FILE symbol. We also need one if linker
10029 defined symbols are present. In practice these conditions are
10030 always met, so just emit the FILE symbol unconditionally. */
10031 if (eoinfo->localsyms
10032 && !eoinfo->file_sym_done
10033 && eoinfo->flinfo->filesym_count != 0)
10035 Elf_Internal_Sym fsym;
10037 memset (&fsym, 0, sizeof (fsym));
10038 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10039 fsym.st_shndx = SHN_ABS;
10040 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10041 bfd_und_section_ptr, NULL))
10044 eoinfo->file_sym_done = TRUE;
10047 indx = bfd_get_symcount (flinfo->output_bfd);
10048 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10052 eoinfo->failed = TRUE;
10057 else if (h->indx == -2)
10063 /* Return TRUE if special handling is done for relocs in SEC against
10064 symbols defined in discarded sections. */
10067 elf_section_ignore_discarded_relocs (asection *sec)
10069 const struct elf_backend_data *bed;
10071 switch (sec->sec_info_type)
10073 case SEC_INFO_TYPE_STABS:
10074 case SEC_INFO_TYPE_EH_FRAME:
10075 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10081 bed = get_elf_backend_data (sec->owner);
10082 if (bed->elf_backend_ignore_discarded_relocs != NULL
10083 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10089 /* Return a mask saying how ld should treat relocations in SEC against
10090 symbols defined in discarded sections. If this function returns
10091 COMPLAIN set, ld will issue a warning message. If this function
10092 returns PRETEND set, and the discarded section was link-once and the
10093 same size as the kept link-once section, ld will pretend that the
10094 symbol was actually defined in the kept section. Otherwise ld will
10095 zero the reloc (at least that is the intent, but some cooperation by
10096 the target dependent code is needed, particularly for REL targets). */
10099 _bfd_elf_default_action_discarded (asection *sec)
10101 if (sec->flags & SEC_DEBUGGING)
10104 if (strcmp (".eh_frame", sec->name) == 0)
10107 if (strcmp (".gcc_except_table", sec->name) == 0)
10110 return COMPLAIN | PRETEND;
10113 /* Find a match between a section and a member of a section group. */
10116 match_group_member (asection *sec, asection *group,
10117 struct bfd_link_info *info)
10119 asection *first = elf_next_in_group (group);
10120 asection *s = first;
10124 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10127 s = elf_next_in_group (s);
10135 /* Check if the kept section of a discarded section SEC can be used
10136 to replace it. Return the replacement if it is OK. Otherwise return
10140 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10144 kept = sec->kept_section;
10147 if ((kept->flags & SEC_GROUP) != 0)
10148 kept = match_group_member (sec, kept, info);
10150 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10151 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10153 sec->kept_section = kept;
10158 /* Link an input file into the linker output file. This function
10159 handles all the sections and relocations of the input file at once.
10160 This is so that we only have to read the local symbols once, and
10161 don't have to keep them in memory. */
10164 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10166 int (*relocate_section)
10167 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10168 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10170 Elf_Internal_Shdr *symtab_hdr;
10171 size_t locsymcount;
10173 Elf_Internal_Sym *isymbuf;
10174 Elf_Internal_Sym *isym;
10175 Elf_Internal_Sym *isymend;
10177 asection **ppsection;
10179 const struct elf_backend_data *bed;
10180 struct elf_link_hash_entry **sym_hashes;
10181 bfd_size_type address_size;
10182 bfd_vma r_type_mask;
10184 bfd_boolean have_file_sym = FALSE;
10186 output_bfd = flinfo->output_bfd;
10187 bed = get_elf_backend_data (output_bfd);
10188 relocate_section = bed->elf_backend_relocate_section;
10190 /* If this is a dynamic object, we don't want to do anything here:
10191 we don't want the local symbols, and we don't want the section
10193 if ((input_bfd->flags & DYNAMIC) != 0)
10196 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10197 if (elf_bad_symtab (input_bfd))
10199 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10204 locsymcount = symtab_hdr->sh_info;
10205 extsymoff = symtab_hdr->sh_info;
10208 /* Read the local symbols. */
10209 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10210 if (isymbuf == NULL && locsymcount != 0)
10212 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10213 flinfo->internal_syms,
10214 flinfo->external_syms,
10215 flinfo->locsym_shndx);
10216 if (isymbuf == NULL)
10220 /* Find local symbol sections and adjust values of symbols in
10221 SEC_MERGE sections. Write out those local symbols we know are
10222 going into the output file. */
10223 isymend = isymbuf + locsymcount;
10224 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10226 isym++, pindex++, ppsection++)
10230 Elf_Internal_Sym osym;
10236 if (elf_bad_symtab (input_bfd))
10238 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10245 if (isym->st_shndx == SHN_UNDEF)
10246 isec = bfd_und_section_ptr;
10247 else if (isym->st_shndx == SHN_ABS)
10248 isec = bfd_abs_section_ptr;
10249 else if (isym->st_shndx == SHN_COMMON)
10250 isec = bfd_com_section_ptr;
10253 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10256 /* Don't attempt to output symbols with st_shnx in the
10257 reserved range other than SHN_ABS and SHN_COMMON. */
10261 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10262 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10264 _bfd_merged_section_offset (output_bfd, &isec,
10265 elf_section_data (isec)->sec_info,
10271 /* Don't output the first, undefined, symbol. In fact, don't
10272 output any undefined local symbol. */
10273 if (isec == bfd_und_section_ptr)
10276 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10278 /* We never output section symbols. Instead, we use the
10279 section symbol of the corresponding section in the output
10284 /* If we are stripping all symbols, we don't want to output this
10286 if (flinfo->info->strip == strip_all)
10289 /* If we are discarding all local symbols, we don't want to
10290 output this one. If we are generating a relocatable output
10291 file, then some of the local symbols may be required by
10292 relocs; we output them below as we discover that they are
10294 if (flinfo->info->discard == discard_all)
10297 /* If this symbol is defined in a section which we are
10298 discarding, we don't need to keep it. */
10299 if (isym->st_shndx != SHN_UNDEF
10300 && isym->st_shndx < SHN_LORESERVE
10301 && bfd_section_removed_from_list (output_bfd,
10302 isec->output_section))
10305 /* Get the name of the symbol. */
10306 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10311 /* See if we are discarding symbols with this name. */
10312 if ((flinfo->info->strip == strip_some
10313 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10315 || (((flinfo->info->discard == discard_sec_merge
10316 && (isec->flags & SEC_MERGE)
10317 && !bfd_link_relocatable (flinfo->info))
10318 || flinfo->info->discard == discard_l)
10319 && bfd_is_local_label_name (input_bfd, name)))
10322 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10324 if (input_bfd->lto_output)
10325 /* -flto puts a temp file name here. This means builds
10326 are not reproducible. Discard the symbol. */
10328 have_file_sym = TRUE;
10329 flinfo->filesym_count += 1;
10331 if (!have_file_sym)
10333 /* In the absence of debug info, bfd_find_nearest_line uses
10334 FILE symbols to determine the source file for local
10335 function symbols. Provide a FILE symbol here if input
10336 files lack such, so that their symbols won't be
10337 associated with a previous input file. It's not the
10338 source file, but the best we can do. */
10339 have_file_sym = TRUE;
10340 flinfo->filesym_count += 1;
10341 memset (&osym, 0, sizeof (osym));
10342 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10343 osym.st_shndx = SHN_ABS;
10344 if (!elf_link_output_symstrtab (flinfo,
10345 (input_bfd->lto_output ? NULL
10346 : input_bfd->filename),
10347 &osym, bfd_abs_section_ptr,
10354 /* Adjust the section index for the output file. */
10355 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10356 isec->output_section);
10357 if (osym.st_shndx == SHN_BAD)
10360 /* ELF symbols in relocatable files are section relative, but
10361 in executable files they are virtual addresses. Note that
10362 this code assumes that all ELF sections have an associated
10363 BFD section with a reasonable value for output_offset; below
10364 we assume that they also have a reasonable value for
10365 output_section. Any special sections must be set up to meet
10366 these requirements. */
10367 osym.st_value += isec->output_offset;
10368 if (!bfd_link_relocatable (flinfo->info))
10370 osym.st_value += isec->output_section->vma;
10371 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10373 /* STT_TLS symbols are relative to PT_TLS segment base. */
10374 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10375 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10379 indx = bfd_get_symcount (output_bfd);
10380 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10387 if (bed->s->arch_size == 32)
10389 r_type_mask = 0xff;
10395 r_type_mask = 0xffffffff;
10400 /* Relocate the contents of each section. */
10401 sym_hashes = elf_sym_hashes (input_bfd);
10402 for (o = input_bfd->sections; o != NULL; o = o->next)
10404 bfd_byte *contents;
10406 if (! o->linker_mark)
10408 /* This section was omitted from the link. */
10412 if (!flinfo->info->resolve_section_groups
10413 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10415 /* Deal with the group signature symbol. */
10416 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10417 unsigned long symndx = sec_data->this_hdr.sh_info;
10418 asection *osec = o->output_section;
10420 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10421 if (symndx >= locsymcount
10422 || (elf_bad_symtab (input_bfd)
10423 && flinfo->sections[symndx] == NULL))
10425 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10426 while (h->root.type == bfd_link_hash_indirect
10427 || h->root.type == bfd_link_hash_warning)
10428 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10429 /* Arrange for symbol to be output. */
10431 elf_section_data (osec)->this_hdr.sh_info = -2;
10433 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10435 /* We'll use the output section target_index. */
10436 asection *sec = flinfo->sections[symndx]->output_section;
10437 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10441 if (flinfo->indices[symndx] == -1)
10443 /* Otherwise output the local symbol now. */
10444 Elf_Internal_Sym sym = isymbuf[symndx];
10445 asection *sec = flinfo->sections[symndx]->output_section;
10450 name = bfd_elf_string_from_elf_section (input_bfd,
10451 symtab_hdr->sh_link,
10456 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10458 if (sym.st_shndx == SHN_BAD)
10461 sym.st_value += o->output_offset;
10463 indx = bfd_get_symcount (output_bfd);
10464 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10469 flinfo->indices[symndx] = indx;
10473 elf_section_data (osec)->this_hdr.sh_info
10474 = flinfo->indices[symndx];
10478 if ((o->flags & SEC_HAS_CONTENTS) == 0
10479 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10482 if ((o->flags & SEC_LINKER_CREATED) != 0)
10484 /* Section was created by _bfd_elf_link_create_dynamic_sections
10489 /* Get the contents of the section. They have been cached by a
10490 relaxation routine. Note that o is a section in an input
10491 file, so the contents field will not have been set by any of
10492 the routines which work on output files. */
10493 if (elf_section_data (o)->this_hdr.contents != NULL)
10495 contents = elf_section_data (o)->this_hdr.contents;
10496 if (bed->caches_rawsize
10498 && o->rawsize < o->size)
10500 memcpy (flinfo->contents, contents, o->rawsize);
10501 contents = flinfo->contents;
10506 contents = flinfo->contents;
10507 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10511 if ((o->flags & SEC_RELOC) != 0)
10513 Elf_Internal_Rela *internal_relocs;
10514 Elf_Internal_Rela *rel, *relend;
10515 int action_discarded;
10518 /* Get the swapped relocs. */
10520 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10521 flinfo->internal_relocs, FALSE);
10522 if (internal_relocs == NULL
10523 && o->reloc_count > 0)
10526 /* We need to reverse-copy input .ctors/.dtors sections if
10527 they are placed in .init_array/.finit_array for output. */
10528 if (o->size > address_size
10529 && ((strncmp (o->name, ".ctors", 6) == 0
10530 && strcmp (o->output_section->name,
10531 ".init_array") == 0)
10532 || (strncmp (o->name, ".dtors", 6) == 0
10533 && strcmp (o->output_section->name,
10534 ".fini_array") == 0))
10535 && (o->name[6] == 0 || o->name[6] == '.'))
10537 if (o->size * bed->s->int_rels_per_ext_rel
10538 != o->reloc_count * address_size)
10541 /* xgettext:c-format */
10542 (_("error: %pB: size of section %pA is not "
10543 "multiple of address size"),
10545 bfd_set_error (bfd_error_bad_value);
10548 o->flags |= SEC_ELF_REVERSE_COPY;
10551 action_discarded = -1;
10552 if (!elf_section_ignore_discarded_relocs (o))
10553 action_discarded = (*bed->action_discarded) (o);
10555 /* Run through the relocs evaluating complex reloc symbols and
10556 looking for relocs against symbols from discarded sections
10557 or section symbols from removed link-once sections.
10558 Complain about relocs against discarded sections. Zero
10559 relocs against removed link-once sections. */
10561 rel = internal_relocs;
10562 relend = rel + o->reloc_count;
10563 for ( ; rel < relend; rel++)
10565 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10566 unsigned int s_type;
10567 asection **ps, *sec;
10568 struct elf_link_hash_entry *h = NULL;
10569 const char *sym_name;
10571 if (r_symndx == STN_UNDEF)
10574 if (r_symndx >= locsymcount
10575 || (elf_bad_symtab (input_bfd)
10576 && flinfo->sections[r_symndx] == NULL))
10578 h = sym_hashes[r_symndx - extsymoff];
10580 /* Badly formatted input files can contain relocs that
10581 reference non-existant symbols. Check here so that
10582 we do not seg fault. */
10586 /* xgettext:c-format */
10587 (_("error: %pB contains a reloc (%#Lx) for section %pA "
10588 "that references a non-existent global symbol"),
10589 input_bfd, rel->r_info, o);
10590 bfd_set_error (bfd_error_bad_value);
10594 while (h->root.type == bfd_link_hash_indirect
10595 || h->root.type == bfd_link_hash_warning)
10596 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10600 /* If a plugin symbol is referenced from a non-IR file,
10601 mark the symbol as undefined. Note that the
10602 linker may attach linker created dynamic sections
10603 to the plugin bfd. Symbols defined in linker
10604 created sections are not plugin symbols. */
10605 if ((h->root.non_ir_ref_regular
10606 || h->root.non_ir_ref_dynamic)
10607 && (h->root.type == bfd_link_hash_defined
10608 || h->root.type == bfd_link_hash_defweak)
10609 && (h->root.u.def.section->flags
10610 & SEC_LINKER_CREATED) == 0
10611 && h->root.u.def.section->owner != NULL
10612 && (h->root.u.def.section->owner->flags
10613 & BFD_PLUGIN) != 0)
10615 h->root.type = bfd_link_hash_undefined;
10616 h->root.u.undef.abfd = h->root.u.def.section->owner;
10620 if (h->root.type == bfd_link_hash_defined
10621 || h->root.type == bfd_link_hash_defweak)
10622 ps = &h->root.u.def.section;
10624 sym_name = h->root.root.string;
10628 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10630 s_type = ELF_ST_TYPE (sym->st_info);
10631 ps = &flinfo->sections[r_symndx];
10632 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10636 if ((s_type == STT_RELC || s_type == STT_SRELC)
10637 && !bfd_link_relocatable (flinfo->info))
10640 bfd_vma dot = (rel->r_offset
10641 + o->output_offset + o->output_section->vma);
10643 printf ("Encountered a complex symbol!");
10644 printf (" (input_bfd %s, section %s, reloc %ld\n",
10645 input_bfd->filename, o->name,
10646 (long) (rel - internal_relocs));
10647 printf (" symbol: idx %8.8lx, name %s\n",
10648 r_symndx, sym_name);
10649 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10650 (unsigned long) rel->r_info,
10651 (unsigned long) rel->r_offset);
10653 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10654 isymbuf, locsymcount, s_type == STT_SRELC))
10657 /* Symbol evaluated OK. Update to absolute value. */
10658 set_symbol_value (input_bfd, isymbuf, locsymcount,
10663 if (action_discarded != -1 && ps != NULL)
10665 /* Complain if the definition comes from a
10666 discarded section. */
10667 if ((sec = *ps) != NULL && discarded_section (sec))
10669 BFD_ASSERT (r_symndx != STN_UNDEF);
10670 if (action_discarded & COMPLAIN)
10671 (*flinfo->info->callbacks->einfo)
10672 /* xgettext:c-format */
10673 (_("%X`%s' referenced in section `%pA' of %pB: "
10674 "defined in discarded section `%pA' of %pB\n"),
10675 sym_name, o, input_bfd, sec, sec->owner);
10677 /* Try to do the best we can to support buggy old
10678 versions of gcc. Pretend that the symbol is
10679 really defined in the kept linkonce section.
10680 FIXME: This is quite broken. Modifying the
10681 symbol here means we will be changing all later
10682 uses of the symbol, not just in this section. */
10683 if (action_discarded & PRETEND)
10687 kept = _bfd_elf_check_kept_section (sec,
10699 /* Relocate the section by invoking a back end routine.
10701 The back end routine is responsible for adjusting the
10702 section contents as necessary, and (if using Rela relocs
10703 and generating a relocatable output file) adjusting the
10704 reloc addend as necessary.
10706 The back end routine does not have to worry about setting
10707 the reloc address or the reloc symbol index.
10709 The back end routine is given a pointer to the swapped in
10710 internal symbols, and can access the hash table entries
10711 for the external symbols via elf_sym_hashes (input_bfd).
10713 When generating relocatable output, the back end routine
10714 must handle STB_LOCAL/STT_SECTION symbols specially. The
10715 output symbol is going to be a section symbol
10716 corresponding to the output section, which will require
10717 the addend to be adjusted. */
10719 ret = (*relocate_section) (output_bfd, flinfo->info,
10720 input_bfd, o, contents,
10728 || bfd_link_relocatable (flinfo->info)
10729 || flinfo->info->emitrelocations)
10731 Elf_Internal_Rela *irela;
10732 Elf_Internal_Rela *irelaend, *irelamid;
10733 bfd_vma last_offset;
10734 struct elf_link_hash_entry **rel_hash;
10735 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10736 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10737 unsigned int next_erel;
10738 bfd_boolean rela_normal;
10739 struct bfd_elf_section_data *esdi, *esdo;
10741 esdi = elf_section_data (o);
10742 esdo = elf_section_data (o->output_section);
10743 rela_normal = FALSE;
10745 /* Adjust the reloc addresses and symbol indices. */
10747 irela = internal_relocs;
10748 irelaend = irela + o->reloc_count;
10749 rel_hash = esdo->rel.hashes + esdo->rel.count;
10750 /* We start processing the REL relocs, if any. When we reach
10751 IRELAMID in the loop, we switch to the RELA relocs. */
10753 if (esdi->rel.hdr != NULL)
10754 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10755 * bed->s->int_rels_per_ext_rel);
10756 rel_hash_list = rel_hash;
10757 rela_hash_list = NULL;
10758 last_offset = o->output_offset;
10759 if (!bfd_link_relocatable (flinfo->info))
10760 last_offset += o->output_section->vma;
10761 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10763 unsigned long r_symndx;
10765 Elf_Internal_Sym sym;
10767 if (next_erel == bed->s->int_rels_per_ext_rel)
10773 if (irela == irelamid)
10775 rel_hash = esdo->rela.hashes + esdo->rela.count;
10776 rela_hash_list = rel_hash;
10777 rela_normal = bed->rela_normal;
10780 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10783 if (irela->r_offset >= (bfd_vma) -2)
10785 /* This is a reloc for a deleted entry or somesuch.
10786 Turn it into an R_*_NONE reloc, at the same
10787 offset as the last reloc. elf_eh_frame.c and
10788 bfd_elf_discard_info rely on reloc offsets
10790 irela->r_offset = last_offset;
10792 irela->r_addend = 0;
10796 irela->r_offset += o->output_offset;
10798 /* Relocs in an executable have to be virtual addresses. */
10799 if (!bfd_link_relocatable (flinfo->info))
10800 irela->r_offset += o->output_section->vma;
10802 last_offset = irela->r_offset;
10804 r_symndx = irela->r_info >> r_sym_shift;
10805 if (r_symndx == STN_UNDEF)
10808 if (r_symndx >= locsymcount
10809 || (elf_bad_symtab (input_bfd)
10810 && flinfo->sections[r_symndx] == NULL))
10812 struct elf_link_hash_entry *rh;
10813 unsigned long indx;
10815 /* This is a reloc against a global symbol. We
10816 have not yet output all the local symbols, so
10817 we do not know the symbol index of any global
10818 symbol. We set the rel_hash entry for this
10819 reloc to point to the global hash table entry
10820 for this symbol. The symbol index is then
10821 set at the end of bfd_elf_final_link. */
10822 indx = r_symndx - extsymoff;
10823 rh = elf_sym_hashes (input_bfd)[indx];
10824 while (rh->root.type == bfd_link_hash_indirect
10825 || rh->root.type == bfd_link_hash_warning)
10826 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10828 /* Setting the index to -2 tells
10829 elf_link_output_extsym that this symbol is
10830 used by a reloc. */
10831 BFD_ASSERT (rh->indx < 0);
10838 /* This is a reloc against a local symbol. */
10841 sym = isymbuf[r_symndx];
10842 sec = flinfo->sections[r_symndx];
10843 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10845 /* I suppose the backend ought to fill in the
10846 section of any STT_SECTION symbol against a
10847 processor specific section. */
10848 r_symndx = STN_UNDEF;
10849 if (bfd_is_abs_section (sec))
10851 else if (sec == NULL || sec->owner == NULL)
10853 bfd_set_error (bfd_error_bad_value);
10858 asection *osec = sec->output_section;
10860 /* If we have discarded a section, the output
10861 section will be the absolute section. In
10862 case of discarded SEC_MERGE sections, use
10863 the kept section. relocate_section should
10864 have already handled discarded linkonce
10866 if (bfd_is_abs_section (osec)
10867 && sec->kept_section != NULL
10868 && sec->kept_section->output_section != NULL)
10870 osec = sec->kept_section->output_section;
10871 irela->r_addend -= osec->vma;
10874 if (!bfd_is_abs_section (osec))
10876 r_symndx = osec->target_index;
10877 if (r_symndx == STN_UNDEF)
10879 irela->r_addend += osec->vma;
10880 osec = _bfd_nearby_section (output_bfd, osec,
10882 irela->r_addend -= osec->vma;
10883 r_symndx = osec->target_index;
10888 /* Adjust the addend according to where the
10889 section winds up in the output section. */
10891 irela->r_addend += sec->output_offset;
10895 if (flinfo->indices[r_symndx] == -1)
10897 unsigned long shlink;
10902 if (flinfo->info->strip == strip_all)
10904 /* You can't do ld -r -s. */
10905 bfd_set_error (bfd_error_invalid_operation);
10909 /* This symbol was skipped earlier, but
10910 since it is needed by a reloc, we
10911 must output it now. */
10912 shlink = symtab_hdr->sh_link;
10913 name = (bfd_elf_string_from_elf_section
10914 (input_bfd, shlink, sym.st_name));
10918 osec = sec->output_section;
10920 _bfd_elf_section_from_bfd_section (output_bfd,
10922 if (sym.st_shndx == SHN_BAD)
10925 sym.st_value += sec->output_offset;
10926 if (!bfd_link_relocatable (flinfo->info))
10928 sym.st_value += osec->vma;
10929 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10931 /* STT_TLS symbols are relative to PT_TLS
10933 BFD_ASSERT (elf_hash_table (flinfo->info)
10934 ->tls_sec != NULL);
10935 sym.st_value -= (elf_hash_table (flinfo->info)
10940 indx = bfd_get_symcount (output_bfd);
10941 ret = elf_link_output_symstrtab (flinfo, name,
10947 flinfo->indices[r_symndx] = indx;
10952 r_symndx = flinfo->indices[r_symndx];
10955 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10956 | (irela->r_info & r_type_mask));
10959 /* Swap out the relocs. */
10960 input_rel_hdr = esdi->rel.hdr;
10961 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10963 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10968 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10969 * bed->s->int_rels_per_ext_rel);
10970 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10973 input_rela_hdr = esdi->rela.hdr;
10974 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10976 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10985 /* Write out the modified section contents. */
10986 if (bed->elf_backend_write_section
10987 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10990 /* Section written out. */
10992 else switch (o->sec_info_type)
10994 case SEC_INFO_TYPE_STABS:
10995 if (! (_bfd_write_section_stabs
10997 &elf_hash_table (flinfo->info)->stab_info,
10998 o, &elf_section_data (o)->sec_info, contents)))
11001 case SEC_INFO_TYPE_MERGE:
11002 if (! _bfd_write_merged_section (output_bfd, o,
11003 elf_section_data (o)->sec_info))
11006 case SEC_INFO_TYPE_EH_FRAME:
11008 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11013 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11015 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11023 if (! (o->flags & SEC_EXCLUDE))
11025 file_ptr offset = (file_ptr) o->output_offset;
11026 bfd_size_type todo = o->size;
11028 offset *= bfd_octets_per_byte (output_bfd);
11030 if ((o->flags & SEC_ELF_REVERSE_COPY))
11032 /* Reverse-copy input section to output. */
11035 todo -= address_size;
11036 if (! bfd_set_section_contents (output_bfd,
11044 offset += address_size;
11048 else if (! bfd_set_section_contents (output_bfd,
11062 /* Generate a reloc when linking an ELF file. This is a reloc
11063 requested by the linker, and does not come from any input file. This
11064 is used to build constructor and destructor tables when linking
11068 elf_reloc_link_order (bfd *output_bfd,
11069 struct bfd_link_info *info,
11070 asection *output_section,
11071 struct bfd_link_order *link_order)
11073 reloc_howto_type *howto;
11077 struct bfd_elf_section_reloc_data *reldata;
11078 struct elf_link_hash_entry **rel_hash_ptr;
11079 Elf_Internal_Shdr *rel_hdr;
11080 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11081 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11084 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11086 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11089 bfd_set_error (bfd_error_bad_value);
11093 addend = link_order->u.reloc.p->addend;
11096 reldata = &esdo->rel;
11097 else if (esdo->rela.hdr)
11098 reldata = &esdo->rela;
11105 /* Figure out the symbol index. */
11106 rel_hash_ptr = reldata->hashes + reldata->count;
11107 if (link_order->type == bfd_section_reloc_link_order)
11109 indx = link_order->u.reloc.p->u.section->target_index;
11110 BFD_ASSERT (indx != 0);
11111 *rel_hash_ptr = NULL;
11115 struct elf_link_hash_entry *h;
11117 /* Treat a reloc against a defined symbol as though it were
11118 actually against the section. */
11119 h = ((struct elf_link_hash_entry *)
11120 bfd_wrapped_link_hash_lookup (output_bfd, info,
11121 link_order->u.reloc.p->u.name,
11122 FALSE, FALSE, TRUE));
11124 && (h->root.type == bfd_link_hash_defined
11125 || h->root.type == bfd_link_hash_defweak))
11129 section = h->root.u.def.section;
11130 indx = section->output_section->target_index;
11131 *rel_hash_ptr = NULL;
11132 /* It seems that we ought to add the symbol value to the
11133 addend here, but in practice it has already been added
11134 because it was passed to constructor_callback. */
11135 addend += section->output_section->vma + section->output_offset;
11137 else if (h != NULL)
11139 /* Setting the index to -2 tells elf_link_output_extsym that
11140 this symbol is used by a reloc. */
11147 (*info->callbacks->unattached_reloc)
11148 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11153 /* If this is an inplace reloc, we must write the addend into the
11155 if (howto->partial_inplace && addend != 0)
11157 bfd_size_type size;
11158 bfd_reloc_status_type rstat;
11161 const char *sym_name;
11163 size = (bfd_size_type) bfd_get_reloc_size (howto);
11164 buf = (bfd_byte *) bfd_zmalloc (size);
11165 if (buf == NULL && size != 0)
11167 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11174 case bfd_reloc_outofrange:
11177 case bfd_reloc_overflow:
11178 if (link_order->type == bfd_section_reloc_link_order)
11179 sym_name = bfd_section_name (output_bfd,
11180 link_order->u.reloc.p->u.section);
11182 sym_name = link_order->u.reloc.p->u.name;
11183 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11184 howto->name, addend, NULL, NULL,
11189 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11191 * bfd_octets_per_byte (output_bfd),
11198 /* The address of a reloc is relative to the section in a
11199 relocatable file, and is a virtual address in an executable
11201 offset = link_order->offset;
11202 if (! bfd_link_relocatable (info))
11203 offset += output_section->vma;
11205 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11207 irel[i].r_offset = offset;
11208 irel[i].r_info = 0;
11209 irel[i].r_addend = 0;
11211 if (bed->s->arch_size == 32)
11212 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11214 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11216 rel_hdr = reldata->hdr;
11217 erel = rel_hdr->contents;
11218 if (rel_hdr->sh_type == SHT_REL)
11220 erel += reldata->count * bed->s->sizeof_rel;
11221 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11225 irel[0].r_addend = addend;
11226 erel += reldata->count * bed->s->sizeof_rela;
11227 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11236 /* Get the output vma of the section pointed to by the sh_link field. */
11239 elf_get_linked_section_vma (struct bfd_link_order *p)
11241 Elf_Internal_Shdr **elf_shdrp;
11245 s = p->u.indirect.section;
11246 elf_shdrp = elf_elfsections (s->owner);
11247 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11248 elfsec = elf_shdrp[elfsec]->sh_link;
11250 The Intel C compiler generates SHT_IA_64_UNWIND with
11251 SHF_LINK_ORDER. But it doesn't set the sh_link or
11252 sh_info fields. Hence we could get the situation
11253 where elfsec is 0. */
11256 const struct elf_backend_data *bed
11257 = get_elf_backend_data (s->owner);
11258 if (bed->link_order_error_handler)
11259 bed->link_order_error_handler
11260 /* xgettext:c-format */
11261 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11266 s = elf_shdrp[elfsec]->bfd_section;
11267 return s->output_section->vma + s->output_offset;
11272 /* Compare two sections based on the locations of the sections they are
11273 linked to. Used by elf_fixup_link_order. */
11276 compare_link_order (const void * a, const void * b)
11281 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11282 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11285 return apos > bpos;
11289 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11290 order as their linked sections. Returns false if this could not be done
11291 because an output section includes both ordered and unordered
11292 sections. Ideally we'd do this in the linker proper. */
11295 elf_fixup_link_order (bfd *abfd, asection *o)
11297 int seen_linkorder;
11300 struct bfd_link_order *p;
11302 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11304 struct bfd_link_order **sections;
11305 asection *s, *other_sec, *linkorder_sec;
11309 linkorder_sec = NULL;
11311 seen_linkorder = 0;
11312 for (p = o->map_head.link_order; p != NULL; p = p->next)
11314 if (p->type == bfd_indirect_link_order)
11316 s = p->u.indirect.section;
11318 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11319 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11320 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11321 && elfsec < elf_numsections (sub)
11322 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11323 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11337 if (seen_other && seen_linkorder)
11339 if (other_sec && linkorder_sec)
11341 /* xgettext:c-format */
11342 (_("%pA has both ordered [`%pA' in %pB] "
11343 "and unordered [`%pA' in %pB] sections"),
11344 o, linkorder_sec, linkorder_sec->owner,
11345 other_sec, other_sec->owner);
11348 (_("%pA has both ordered and unordered sections"), o);
11349 bfd_set_error (bfd_error_bad_value);
11354 if (!seen_linkorder)
11357 sections = (struct bfd_link_order **)
11358 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11359 if (sections == NULL)
11361 seen_linkorder = 0;
11363 for (p = o->map_head.link_order; p != NULL; p = p->next)
11365 sections[seen_linkorder++] = p;
11367 /* Sort the input sections in the order of their linked section. */
11368 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11369 compare_link_order);
11371 /* Change the offsets of the sections. */
11373 for (n = 0; n < seen_linkorder; n++)
11375 s = sections[n]->u.indirect.section;
11376 offset &= ~(bfd_vma) 0 << s->alignment_power;
11377 s->output_offset = offset / bfd_octets_per_byte (abfd);
11378 sections[n]->offset = offset;
11379 offset += sections[n]->size;
11386 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11387 Returns TRUE upon success, FALSE otherwise. */
11390 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11392 bfd_boolean ret = FALSE;
11394 const struct elf_backend_data *bed;
11396 enum bfd_architecture arch;
11398 asymbol **sympp = NULL;
11402 elf_symbol_type *osymbuf;
11404 implib_bfd = info->out_implib_bfd;
11405 bed = get_elf_backend_data (abfd);
11407 if (!bfd_set_format (implib_bfd, bfd_object))
11410 /* Use flag from executable but make it a relocatable object. */
11411 flags = bfd_get_file_flags (abfd);
11412 flags &= ~HAS_RELOC;
11413 if (!bfd_set_start_address (implib_bfd, 0)
11414 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11417 /* Copy architecture of output file to import library file. */
11418 arch = bfd_get_arch (abfd);
11419 mach = bfd_get_mach (abfd);
11420 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11421 && (abfd->target_defaulted
11422 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11425 /* Get symbol table size. */
11426 symsize = bfd_get_symtab_upper_bound (abfd);
11430 /* Read in the symbol table. */
11431 sympp = (asymbol **) xmalloc (symsize);
11432 symcount = bfd_canonicalize_symtab (abfd, sympp);
11436 /* Allow the BFD backend to copy any private header data it
11437 understands from the output BFD to the import library BFD. */
11438 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11441 /* Filter symbols to appear in the import library. */
11442 if (bed->elf_backend_filter_implib_symbols)
11443 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11446 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11449 bfd_set_error (bfd_error_no_symbols);
11450 _bfd_error_handler (_("%pB: no symbol found for import library"),
11456 /* Make symbols absolute. */
11457 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11458 sizeof (*osymbuf));
11459 for (src_count = 0; src_count < symcount; src_count++)
11461 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11462 sizeof (*osymbuf));
11463 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11464 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11465 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11466 osymbuf[src_count].internal_elf_sym.st_value =
11467 osymbuf[src_count].symbol.value;
11468 sympp[src_count] = &osymbuf[src_count].symbol;
11471 bfd_set_symtab (implib_bfd, sympp, symcount);
11473 /* Allow the BFD backend to copy any private data it understands
11474 from the output BFD to the import library BFD. This is done last
11475 to permit the routine to look at the filtered symbol table. */
11476 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11479 if (!bfd_close (implib_bfd))
11490 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11494 if (flinfo->symstrtab != NULL)
11495 _bfd_elf_strtab_free (flinfo->symstrtab);
11496 if (flinfo->contents != NULL)
11497 free (flinfo->contents);
11498 if (flinfo->external_relocs != NULL)
11499 free (flinfo->external_relocs);
11500 if (flinfo->internal_relocs != NULL)
11501 free (flinfo->internal_relocs);
11502 if (flinfo->external_syms != NULL)
11503 free (flinfo->external_syms);
11504 if (flinfo->locsym_shndx != NULL)
11505 free (flinfo->locsym_shndx);
11506 if (flinfo->internal_syms != NULL)
11507 free (flinfo->internal_syms);
11508 if (flinfo->indices != NULL)
11509 free (flinfo->indices);
11510 if (flinfo->sections != NULL)
11511 free (flinfo->sections);
11512 if (flinfo->symshndxbuf != NULL)
11513 free (flinfo->symshndxbuf);
11514 for (o = obfd->sections; o != NULL; o = o->next)
11516 struct bfd_elf_section_data *esdo = elf_section_data (o);
11517 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11518 free (esdo->rel.hashes);
11519 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11520 free (esdo->rela.hashes);
11524 /* Do the final step of an ELF link. */
11527 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11529 bfd_boolean dynamic;
11530 bfd_boolean emit_relocs;
11532 struct elf_final_link_info flinfo;
11534 struct bfd_link_order *p;
11536 bfd_size_type max_contents_size;
11537 bfd_size_type max_external_reloc_size;
11538 bfd_size_type max_internal_reloc_count;
11539 bfd_size_type max_sym_count;
11540 bfd_size_type max_sym_shndx_count;
11541 Elf_Internal_Sym elfsym;
11543 Elf_Internal_Shdr *symtab_hdr;
11544 Elf_Internal_Shdr *symtab_shndx_hdr;
11545 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11546 struct elf_outext_info eoinfo;
11547 bfd_boolean merged;
11548 size_t relativecount = 0;
11549 asection *reldyn = 0;
11551 asection *attr_section = NULL;
11552 bfd_vma attr_size = 0;
11553 const char *std_attrs_section;
11554 struct elf_link_hash_table *htab = elf_hash_table (info);
11556 if (!is_elf_hash_table (htab))
11559 if (bfd_link_pic (info))
11560 abfd->flags |= DYNAMIC;
11562 dynamic = htab->dynamic_sections_created;
11563 dynobj = htab->dynobj;
11565 emit_relocs = (bfd_link_relocatable (info)
11566 || info->emitrelocations);
11568 flinfo.info = info;
11569 flinfo.output_bfd = abfd;
11570 flinfo.symstrtab = _bfd_elf_strtab_init ();
11571 if (flinfo.symstrtab == NULL)
11576 flinfo.hash_sec = NULL;
11577 flinfo.symver_sec = NULL;
11581 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11582 /* Note that dynsym_sec can be NULL (on VMS). */
11583 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11584 /* Note that it is OK if symver_sec is NULL. */
11587 flinfo.contents = NULL;
11588 flinfo.external_relocs = NULL;
11589 flinfo.internal_relocs = NULL;
11590 flinfo.external_syms = NULL;
11591 flinfo.locsym_shndx = NULL;
11592 flinfo.internal_syms = NULL;
11593 flinfo.indices = NULL;
11594 flinfo.sections = NULL;
11595 flinfo.symshndxbuf = NULL;
11596 flinfo.filesym_count = 0;
11598 /* The object attributes have been merged. Remove the input
11599 sections from the link, and set the contents of the output
11601 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11602 for (o = abfd->sections; o != NULL; o = o->next)
11604 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11605 || strcmp (o->name, ".gnu.attributes") == 0)
11607 for (p = o->map_head.link_order; p != NULL; p = p->next)
11609 asection *input_section;
11611 if (p->type != bfd_indirect_link_order)
11613 input_section = p->u.indirect.section;
11614 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11615 elf_link_input_bfd ignores this section. */
11616 input_section->flags &= ~SEC_HAS_CONTENTS;
11619 attr_size = bfd_elf_obj_attr_size (abfd);
11622 bfd_set_section_size (abfd, o, attr_size);
11624 /* Skip this section later on. */
11625 o->map_head.link_order = NULL;
11628 o->flags |= SEC_EXCLUDE;
11630 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11632 /* Remove empty group section from linker output. */
11633 o->flags |= SEC_EXCLUDE;
11634 bfd_section_list_remove (abfd, o);
11635 abfd->section_count--;
11639 /* Count up the number of relocations we will output for each output
11640 section, so that we know the sizes of the reloc sections. We
11641 also figure out some maximum sizes. */
11642 max_contents_size = 0;
11643 max_external_reloc_size = 0;
11644 max_internal_reloc_count = 0;
11646 max_sym_shndx_count = 0;
11648 for (o = abfd->sections; o != NULL; o = o->next)
11650 struct bfd_elf_section_data *esdo = elf_section_data (o);
11651 o->reloc_count = 0;
11653 for (p = o->map_head.link_order; p != NULL; p = p->next)
11655 unsigned int reloc_count = 0;
11656 unsigned int additional_reloc_count = 0;
11657 struct bfd_elf_section_data *esdi = NULL;
11659 if (p->type == bfd_section_reloc_link_order
11660 || p->type == bfd_symbol_reloc_link_order)
11662 else if (p->type == bfd_indirect_link_order)
11666 sec = p->u.indirect.section;
11668 /* Mark all sections which are to be included in the
11669 link. This will normally be every section. We need
11670 to do this so that we can identify any sections which
11671 the linker has decided to not include. */
11672 sec->linker_mark = TRUE;
11674 if (sec->flags & SEC_MERGE)
11677 if (sec->rawsize > max_contents_size)
11678 max_contents_size = sec->rawsize;
11679 if (sec->size > max_contents_size)
11680 max_contents_size = sec->size;
11682 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11683 && (sec->owner->flags & DYNAMIC) == 0)
11687 /* We are interested in just local symbols, not all
11689 if (elf_bad_symtab (sec->owner))
11690 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11691 / bed->s->sizeof_sym);
11693 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11695 if (sym_count > max_sym_count)
11696 max_sym_count = sym_count;
11698 if (sym_count > max_sym_shndx_count
11699 && elf_symtab_shndx_list (sec->owner) != NULL)
11700 max_sym_shndx_count = sym_count;
11702 if (esdo->this_hdr.sh_type == SHT_REL
11703 || esdo->this_hdr.sh_type == SHT_RELA)
11704 /* Some backends use reloc_count in relocation sections
11705 to count particular types of relocs. Of course,
11706 reloc sections themselves can't have relocations. */
11708 else if (emit_relocs)
11710 reloc_count = sec->reloc_count;
11711 if (bed->elf_backend_count_additional_relocs)
11714 c = (*bed->elf_backend_count_additional_relocs) (sec);
11715 additional_reloc_count += c;
11718 else if (bed->elf_backend_count_relocs)
11719 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11721 esdi = elf_section_data (sec);
11723 if ((sec->flags & SEC_RELOC) != 0)
11725 size_t ext_size = 0;
11727 if (esdi->rel.hdr != NULL)
11728 ext_size = esdi->rel.hdr->sh_size;
11729 if (esdi->rela.hdr != NULL)
11730 ext_size += esdi->rela.hdr->sh_size;
11732 if (ext_size > max_external_reloc_size)
11733 max_external_reloc_size = ext_size;
11734 if (sec->reloc_count > max_internal_reloc_count)
11735 max_internal_reloc_count = sec->reloc_count;
11740 if (reloc_count == 0)
11743 reloc_count += additional_reloc_count;
11744 o->reloc_count += reloc_count;
11746 if (p->type == bfd_indirect_link_order && emit_relocs)
11750 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11751 esdo->rel.count += additional_reloc_count;
11753 if (esdi->rela.hdr)
11755 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11756 esdo->rela.count += additional_reloc_count;
11762 esdo->rela.count += reloc_count;
11764 esdo->rel.count += reloc_count;
11768 if (o->reloc_count > 0)
11769 o->flags |= SEC_RELOC;
11772 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11773 set it (this is probably a bug) and if it is set
11774 assign_section_numbers will create a reloc section. */
11775 o->flags &=~ SEC_RELOC;
11778 /* If the SEC_ALLOC flag is not set, force the section VMA to
11779 zero. This is done in elf_fake_sections as well, but forcing
11780 the VMA to 0 here will ensure that relocs against these
11781 sections are handled correctly. */
11782 if ((o->flags & SEC_ALLOC) == 0
11783 && ! o->user_set_vma)
11787 if (! bfd_link_relocatable (info) && merged)
11788 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11790 /* Figure out the file positions for everything but the symbol table
11791 and the relocs. We set symcount to force assign_section_numbers
11792 to create a symbol table. */
11793 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11794 BFD_ASSERT (! abfd->output_has_begun);
11795 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11798 /* Set sizes, and assign file positions for reloc sections. */
11799 for (o = abfd->sections; o != NULL; o = o->next)
11801 struct bfd_elf_section_data *esdo = elf_section_data (o);
11802 if ((o->flags & SEC_RELOC) != 0)
11805 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11809 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11813 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11814 to count upwards while actually outputting the relocations. */
11815 esdo->rel.count = 0;
11816 esdo->rela.count = 0;
11818 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11820 /* Cache the section contents so that they can be compressed
11821 later. Use bfd_malloc since it will be freed by
11822 bfd_compress_section_contents. */
11823 unsigned char *contents = esdo->this_hdr.contents;
11824 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11827 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11828 if (contents == NULL)
11830 esdo->this_hdr.contents = contents;
11834 /* We have now assigned file positions for all the sections except
11835 .symtab, .strtab, and non-loaded reloc sections. We start the
11836 .symtab section at the current file position, and write directly
11837 to it. We build the .strtab section in memory. */
11838 bfd_get_symcount (abfd) = 0;
11839 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11840 /* sh_name is set in prep_headers. */
11841 symtab_hdr->sh_type = SHT_SYMTAB;
11842 /* sh_flags, sh_addr and sh_size all start off zero. */
11843 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11844 /* sh_link is set in assign_section_numbers. */
11845 /* sh_info is set below. */
11846 /* sh_offset is set just below. */
11847 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11849 if (max_sym_count < 20)
11850 max_sym_count = 20;
11851 htab->strtabsize = max_sym_count;
11852 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11853 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11854 if (htab->strtab == NULL)
11856 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11858 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11859 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11861 if (info->strip != strip_all || emit_relocs)
11863 file_ptr off = elf_next_file_pos (abfd);
11865 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11867 /* Note that at this point elf_next_file_pos (abfd) is
11868 incorrect. We do not yet know the size of the .symtab section.
11869 We correct next_file_pos below, after we do know the size. */
11871 /* Start writing out the symbol table. The first symbol is always a
11873 elfsym.st_value = 0;
11874 elfsym.st_size = 0;
11875 elfsym.st_info = 0;
11876 elfsym.st_other = 0;
11877 elfsym.st_shndx = SHN_UNDEF;
11878 elfsym.st_target_internal = 0;
11879 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11880 bfd_und_section_ptr, NULL) != 1)
11883 /* Output a symbol for each section. We output these even if we are
11884 discarding local symbols, since they are used for relocs. These
11885 symbols have no names. We store the index of each one in the
11886 index field of the section, so that we can find it again when
11887 outputting relocs. */
11889 elfsym.st_size = 0;
11890 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11891 elfsym.st_other = 0;
11892 elfsym.st_value = 0;
11893 elfsym.st_target_internal = 0;
11894 for (i = 1; i < elf_numsections (abfd); i++)
11896 o = bfd_section_from_elf_index (abfd, i);
11899 o->target_index = bfd_get_symcount (abfd);
11900 elfsym.st_shndx = i;
11901 if (!bfd_link_relocatable (info))
11902 elfsym.st_value = o->vma;
11903 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11910 /* Allocate some memory to hold information read in from the input
11912 if (max_contents_size != 0)
11914 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11915 if (flinfo.contents == NULL)
11919 if (max_external_reloc_size != 0)
11921 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11922 if (flinfo.external_relocs == NULL)
11926 if (max_internal_reloc_count != 0)
11928 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11929 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11930 if (flinfo.internal_relocs == NULL)
11934 if (max_sym_count != 0)
11936 amt = max_sym_count * bed->s->sizeof_sym;
11937 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11938 if (flinfo.external_syms == NULL)
11941 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11942 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11943 if (flinfo.internal_syms == NULL)
11946 amt = max_sym_count * sizeof (long);
11947 flinfo.indices = (long int *) bfd_malloc (amt);
11948 if (flinfo.indices == NULL)
11951 amt = max_sym_count * sizeof (asection *);
11952 flinfo.sections = (asection **) bfd_malloc (amt);
11953 if (flinfo.sections == NULL)
11957 if (max_sym_shndx_count != 0)
11959 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11960 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11961 if (flinfo.locsym_shndx == NULL)
11967 bfd_vma base, end = 0;
11970 for (sec = htab->tls_sec;
11971 sec && (sec->flags & SEC_THREAD_LOCAL);
11974 bfd_size_type size = sec->size;
11977 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11979 struct bfd_link_order *ord = sec->map_tail.link_order;
11982 size = ord->offset + ord->size;
11984 end = sec->vma + size;
11986 base = htab->tls_sec->vma;
11987 /* Only align end of TLS section if static TLS doesn't have special
11988 alignment requirements. */
11989 if (bed->static_tls_alignment == 1)
11990 end = align_power (end, htab->tls_sec->alignment_power);
11991 htab->tls_size = end - base;
11994 /* Reorder SHF_LINK_ORDER sections. */
11995 for (o = abfd->sections; o != NULL; o = o->next)
11997 if (!elf_fixup_link_order (abfd, o))
12001 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12004 /* Since ELF permits relocations to be against local symbols, we
12005 must have the local symbols available when we do the relocations.
12006 Since we would rather only read the local symbols once, and we
12007 would rather not keep them in memory, we handle all the
12008 relocations for a single input file at the same time.
12010 Unfortunately, there is no way to know the total number of local
12011 symbols until we have seen all of them, and the local symbol
12012 indices precede the global symbol indices. This means that when
12013 we are generating relocatable output, and we see a reloc against
12014 a global symbol, we can not know the symbol index until we have
12015 finished examining all the local symbols to see which ones we are
12016 going to output. To deal with this, we keep the relocations in
12017 memory, and don't output them until the end of the link. This is
12018 an unfortunate waste of memory, but I don't see a good way around
12019 it. Fortunately, it only happens when performing a relocatable
12020 link, which is not the common case. FIXME: If keep_memory is set
12021 we could write the relocs out and then read them again; I don't
12022 know how bad the memory loss will be. */
12024 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12025 sub->output_has_begun = FALSE;
12026 for (o = abfd->sections; o != NULL; o = o->next)
12028 for (p = o->map_head.link_order; p != NULL; p = p->next)
12030 if (p->type == bfd_indirect_link_order
12031 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12032 == bfd_target_elf_flavour)
12033 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12035 if (! sub->output_has_begun)
12037 if (! elf_link_input_bfd (&flinfo, sub))
12039 sub->output_has_begun = TRUE;
12042 else if (p->type == bfd_section_reloc_link_order
12043 || p->type == bfd_symbol_reloc_link_order)
12045 if (! elf_reloc_link_order (abfd, info, o, p))
12050 if (! _bfd_default_link_order (abfd, info, o, p))
12052 if (p->type == bfd_indirect_link_order
12053 && (bfd_get_flavour (sub)
12054 == bfd_target_elf_flavour)
12055 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12056 != bed->s->elfclass))
12058 const char *iclass, *oclass;
12060 switch (bed->s->elfclass)
12062 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12063 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12064 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12068 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12070 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12071 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12072 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12076 bfd_set_error (bfd_error_wrong_format);
12078 /* xgettext:c-format */
12079 (_("%pB: file class %s incompatible with %s"),
12080 sub, iclass, oclass);
12089 /* Free symbol buffer if needed. */
12090 if (!info->reduce_memory_overheads)
12092 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12093 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12094 && elf_tdata (sub)->symbuf)
12096 free (elf_tdata (sub)->symbuf);
12097 elf_tdata (sub)->symbuf = NULL;
12101 /* Output any global symbols that got converted to local in a
12102 version script or due to symbol visibility. We do this in a
12103 separate step since ELF requires all local symbols to appear
12104 prior to any global symbols. FIXME: We should only do this if
12105 some global symbols were, in fact, converted to become local.
12106 FIXME: Will this work correctly with the Irix 5 linker? */
12107 eoinfo.failed = FALSE;
12108 eoinfo.flinfo = &flinfo;
12109 eoinfo.localsyms = TRUE;
12110 eoinfo.file_sym_done = FALSE;
12111 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12115 /* If backend needs to output some local symbols not present in the hash
12116 table, do it now. */
12117 if (bed->elf_backend_output_arch_local_syms
12118 && (info->strip != strip_all || emit_relocs))
12120 typedef int (*out_sym_func)
12121 (void *, const char *, Elf_Internal_Sym *, asection *,
12122 struct elf_link_hash_entry *);
12124 if (! ((*bed->elf_backend_output_arch_local_syms)
12125 (abfd, info, &flinfo,
12126 (out_sym_func) elf_link_output_symstrtab)))
12130 /* That wrote out all the local symbols. Finish up the symbol table
12131 with the global symbols. Even if we want to strip everything we
12132 can, we still need to deal with those global symbols that got
12133 converted to local in a version script. */
12135 /* The sh_info field records the index of the first non local symbol. */
12136 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12139 && htab->dynsym != NULL
12140 && htab->dynsym->output_section != bfd_abs_section_ptr)
12142 Elf_Internal_Sym sym;
12143 bfd_byte *dynsym = htab->dynsym->contents;
12145 o = htab->dynsym->output_section;
12146 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12148 /* Write out the section symbols for the output sections. */
12149 if (bfd_link_pic (info)
12150 || htab->is_relocatable_executable)
12156 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12158 sym.st_target_internal = 0;
12160 for (s = abfd->sections; s != NULL; s = s->next)
12166 dynindx = elf_section_data (s)->dynindx;
12169 indx = elf_section_data (s)->this_idx;
12170 BFD_ASSERT (indx > 0);
12171 sym.st_shndx = indx;
12172 if (! check_dynsym (abfd, &sym))
12174 sym.st_value = s->vma;
12175 dest = dynsym + dynindx * bed->s->sizeof_sym;
12176 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12180 /* Write out the local dynsyms. */
12181 if (htab->dynlocal)
12183 struct elf_link_local_dynamic_entry *e;
12184 for (e = htab->dynlocal; e ; e = e->next)
12189 /* Copy the internal symbol and turn off visibility.
12190 Note that we saved a word of storage and overwrote
12191 the original st_name with the dynstr_index. */
12193 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12195 s = bfd_section_from_elf_index (e->input_bfd,
12200 elf_section_data (s->output_section)->this_idx;
12201 if (! check_dynsym (abfd, &sym))
12203 sym.st_value = (s->output_section->vma
12205 + e->isym.st_value);
12208 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12209 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12214 /* We get the global symbols from the hash table. */
12215 eoinfo.failed = FALSE;
12216 eoinfo.localsyms = FALSE;
12217 eoinfo.flinfo = &flinfo;
12218 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12222 /* If backend needs to output some symbols not present in the hash
12223 table, do it now. */
12224 if (bed->elf_backend_output_arch_syms
12225 && (info->strip != strip_all || emit_relocs))
12227 typedef int (*out_sym_func)
12228 (void *, const char *, Elf_Internal_Sym *, asection *,
12229 struct elf_link_hash_entry *);
12231 if (! ((*bed->elf_backend_output_arch_syms)
12232 (abfd, info, &flinfo,
12233 (out_sym_func) elf_link_output_symstrtab)))
12237 /* Finalize the .strtab section. */
12238 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12240 /* Swap out the .strtab section. */
12241 if (!elf_link_swap_symbols_out (&flinfo))
12244 /* Now we know the size of the symtab section. */
12245 if (bfd_get_symcount (abfd) > 0)
12247 /* Finish up and write out the symbol string table (.strtab)
12249 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12250 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12252 if (elf_symtab_shndx_list (abfd))
12254 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12256 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12258 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12259 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12260 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12261 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12262 symtab_shndx_hdr->sh_size = amt;
12264 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12267 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12268 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12273 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12274 /* sh_name was set in prep_headers. */
12275 symstrtab_hdr->sh_type = SHT_STRTAB;
12276 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12277 symstrtab_hdr->sh_addr = 0;
12278 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12279 symstrtab_hdr->sh_entsize = 0;
12280 symstrtab_hdr->sh_link = 0;
12281 symstrtab_hdr->sh_info = 0;
12282 /* sh_offset is set just below. */
12283 symstrtab_hdr->sh_addralign = 1;
12285 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12287 elf_next_file_pos (abfd) = off;
12289 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12290 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12294 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12296 _bfd_error_handler (_("%pB: failed to generate import library"),
12297 info->out_implib_bfd);
12301 /* Adjust the relocs to have the correct symbol indices. */
12302 for (o = abfd->sections; o != NULL; o = o->next)
12304 struct bfd_elf_section_data *esdo = elf_section_data (o);
12307 if ((o->flags & SEC_RELOC) == 0)
12310 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12311 if (esdo->rel.hdr != NULL
12312 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12314 if (esdo->rela.hdr != NULL
12315 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12318 /* Set the reloc_count field to 0 to prevent write_relocs from
12319 trying to swap the relocs out itself. */
12320 o->reloc_count = 0;
12323 if (dynamic && info->combreloc && dynobj != NULL)
12324 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12326 /* If we are linking against a dynamic object, or generating a
12327 shared library, finish up the dynamic linking information. */
12330 bfd_byte *dyncon, *dynconend;
12332 /* Fix up .dynamic entries. */
12333 o = bfd_get_linker_section (dynobj, ".dynamic");
12334 BFD_ASSERT (o != NULL);
12336 dyncon = o->contents;
12337 dynconend = o->contents + o->size;
12338 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12340 Elf_Internal_Dyn dyn;
12343 bfd_size_type sh_size;
12346 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12353 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12355 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12357 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12358 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12361 dyn.d_un.d_val = relativecount;
12368 name = info->init_function;
12371 name = info->fini_function;
12374 struct elf_link_hash_entry *h;
12376 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12378 && (h->root.type == bfd_link_hash_defined
12379 || h->root.type == bfd_link_hash_defweak))
12381 dyn.d_un.d_ptr = h->root.u.def.value;
12382 o = h->root.u.def.section;
12383 if (o->output_section != NULL)
12384 dyn.d_un.d_ptr += (o->output_section->vma
12385 + o->output_offset);
12388 /* The symbol is imported from another shared
12389 library and does not apply to this one. */
12390 dyn.d_un.d_ptr = 0;
12397 case DT_PREINIT_ARRAYSZ:
12398 name = ".preinit_array";
12400 case DT_INIT_ARRAYSZ:
12401 name = ".init_array";
12403 case DT_FINI_ARRAYSZ:
12404 name = ".fini_array";
12406 o = bfd_get_section_by_name (abfd, name);
12410 (_("could not find section %s"), name);
12415 (_("warning: %s section has zero size"), name);
12416 dyn.d_un.d_val = o->size;
12419 case DT_PREINIT_ARRAY:
12420 name = ".preinit_array";
12422 case DT_INIT_ARRAY:
12423 name = ".init_array";
12425 case DT_FINI_ARRAY:
12426 name = ".fini_array";
12428 o = bfd_get_section_by_name (abfd, name);
12435 name = ".gnu.hash";
12444 name = ".gnu.version_d";
12447 name = ".gnu.version_r";
12450 name = ".gnu.version";
12452 o = bfd_get_linker_section (dynobj, name);
12454 if (o == NULL || bfd_is_abs_section (o->output_section))
12457 (_("could not find section %s"), name);
12460 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12463 (_("warning: section '%s' is being made into a note"), name);
12464 bfd_set_error (bfd_error_nonrepresentable_section);
12467 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12474 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12480 for (i = 1; i < elf_numsections (abfd); i++)
12482 Elf_Internal_Shdr *hdr;
12484 hdr = elf_elfsections (abfd)[i];
12485 if (hdr->sh_type == type
12486 && (hdr->sh_flags & SHF_ALLOC) != 0)
12488 sh_size += hdr->sh_size;
12490 || sh_addr > hdr->sh_addr)
12491 sh_addr = hdr->sh_addr;
12495 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12497 /* Don't count procedure linkage table relocs in the
12498 overall reloc count. */
12499 sh_size -= htab->srelplt->size;
12501 /* If the size is zero, make the address zero too.
12502 This is to avoid a glibc bug. If the backend
12503 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12504 zero, then we'll put DT_RELA at the end of
12505 DT_JMPREL. glibc will interpret the end of
12506 DT_RELA matching the end of DT_JMPREL as the
12507 case where DT_RELA includes DT_JMPREL, and for
12508 LD_BIND_NOW will decide that processing DT_RELA
12509 will process the PLT relocs too. Net result:
12510 No PLT relocs applied. */
12513 /* If .rela.plt is the first .rela section, exclude
12514 it from DT_RELA. */
12515 else if (sh_addr == (htab->srelplt->output_section->vma
12516 + htab->srelplt->output_offset))
12517 sh_addr += htab->srelplt->size;
12520 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12521 dyn.d_un.d_val = sh_size;
12523 dyn.d_un.d_ptr = sh_addr;
12526 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12530 /* If we have created any dynamic sections, then output them. */
12531 if (dynobj != NULL)
12533 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12536 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12537 if (((info->warn_shared_textrel && bfd_link_pic (info))
12538 || info->error_textrel)
12539 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12541 bfd_byte *dyncon, *dynconend;
12543 dyncon = o->contents;
12544 dynconend = o->contents + o->size;
12545 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12547 Elf_Internal_Dyn dyn;
12549 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12551 if (dyn.d_tag == DT_TEXTREL)
12553 if (info->error_textrel)
12554 info->callbacks->einfo
12555 (_("%P%X: read-only segment has dynamic relocations.\n"));
12557 info->callbacks->einfo
12558 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12564 for (o = dynobj->sections; o != NULL; o = o->next)
12566 if ((o->flags & SEC_HAS_CONTENTS) == 0
12568 || o->output_section == bfd_abs_section_ptr)
12570 if ((o->flags & SEC_LINKER_CREATED) == 0)
12572 /* At this point, we are only interested in sections
12573 created by _bfd_elf_link_create_dynamic_sections. */
12576 if (htab->stab_info.stabstr == o)
12578 if (htab->eh_info.hdr_sec == o)
12580 if (strcmp (o->name, ".dynstr") != 0)
12582 if (! bfd_set_section_contents (abfd, o->output_section,
12584 (file_ptr) o->output_offset
12585 * bfd_octets_per_byte (abfd),
12591 /* The contents of the .dynstr section are actually in a
12595 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12596 if (bfd_seek (abfd, off, SEEK_SET) != 0
12597 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12603 if (!info->resolve_section_groups)
12605 bfd_boolean failed = FALSE;
12607 BFD_ASSERT (bfd_link_relocatable (info));
12608 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12613 /* If we have optimized stabs strings, output them. */
12614 if (htab->stab_info.stabstr != NULL)
12616 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12620 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12623 elf_final_link_free (abfd, &flinfo);
12625 elf_linker (abfd) = TRUE;
12629 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12630 if (contents == NULL)
12631 return FALSE; /* Bail out and fail. */
12632 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12633 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12640 elf_final_link_free (abfd, &flinfo);
12644 /* Initialize COOKIE for input bfd ABFD. */
12647 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12648 struct bfd_link_info *info, bfd *abfd)
12650 Elf_Internal_Shdr *symtab_hdr;
12651 const struct elf_backend_data *bed;
12653 bed = get_elf_backend_data (abfd);
12654 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12656 cookie->abfd = abfd;
12657 cookie->sym_hashes = elf_sym_hashes (abfd);
12658 cookie->bad_symtab = elf_bad_symtab (abfd);
12659 if (cookie->bad_symtab)
12661 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12662 cookie->extsymoff = 0;
12666 cookie->locsymcount = symtab_hdr->sh_info;
12667 cookie->extsymoff = symtab_hdr->sh_info;
12670 if (bed->s->arch_size == 32)
12671 cookie->r_sym_shift = 8;
12673 cookie->r_sym_shift = 32;
12675 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12676 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12678 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12679 cookie->locsymcount, 0,
12681 if (cookie->locsyms == NULL)
12683 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12686 if (info->keep_memory)
12687 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12692 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12695 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12697 Elf_Internal_Shdr *symtab_hdr;
12699 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12700 if (cookie->locsyms != NULL
12701 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12702 free (cookie->locsyms);
12705 /* Initialize the relocation information in COOKIE for input section SEC
12706 of input bfd ABFD. */
12709 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12710 struct bfd_link_info *info, bfd *abfd,
12713 if (sec->reloc_count == 0)
12715 cookie->rels = NULL;
12716 cookie->relend = NULL;
12720 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12721 info->keep_memory);
12722 if (cookie->rels == NULL)
12724 cookie->rel = cookie->rels;
12725 cookie->relend = cookie->rels + sec->reloc_count;
12727 cookie->rel = cookie->rels;
12731 /* Free the memory allocated by init_reloc_cookie_rels,
12735 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12738 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12739 free (cookie->rels);
12742 /* Initialize the whole of COOKIE for input section SEC. */
12745 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12746 struct bfd_link_info *info,
12749 if (!init_reloc_cookie (cookie, info, sec->owner))
12751 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12756 fini_reloc_cookie (cookie, sec->owner);
12761 /* Free the memory allocated by init_reloc_cookie_for_section,
12765 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12768 fini_reloc_cookie_rels (cookie, sec);
12769 fini_reloc_cookie (cookie, sec->owner);
12772 /* Garbage collect unused sections. */
12774 /* Default gc_mark_hook. */
12777 _bfd_elf_gc_mark_hook (asection *sec,
12778 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12779 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12780 struct elf_link_hash_entry *h,
12781 Elf_Internal_Sym *sym)
12785 switch (h->root.type)
12787 case bfd_link_hash_defined:
12788 case bfd_link_hash_defweak:
12789 return h->root.u.def.section;
12791 case bfd_link_hash_common:
12792 return h->root.u.c.p->section;
12799 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12804 /* Return the global debug definition section. */
12807 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12808 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12809 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12810 struct elf_link_hash_entry *h,
12811 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12814 && (h->root.type == bfd_link_hash_defined
12815 || h->root.type == bfd_link_hash_defweak)
12816 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12817 return h->root.u.def.section;
12822 /* COOKIE->rel describes a relocation against section SEC, which is
12823 a section we've decided to keep. Return the section that contains
12824 the relocation symbol, or NULL if no section contains it. */
12827 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12828 elf_gc_mark_hook_fn gc_mark_hook,
12829 struct elf_reloc_cookie *cookie,
12830 bfd_boolean *start_stop)
12832 unsigned long r_symndx;
12833 struct elf_link_hash_entry *h;
12835 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12836 if (r_symndx == STN_UNDEF)
12839 if (r_symndx >= cookie->locsymcount
12840 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12842 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12845 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
12849 while (h->root.type == bfd_link_hash_indirect
12850 || h->root.type == bfd_link_hash_warning)
12851 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12853 /* If this symbol is weak and there is a non-weak definition, we
12854 keep the non-weak definition because many backends put
12855 dynamic reloc info on the non-weak definition for code
12856 handling copy relocs. */
12857 if (h->is_weakalias)
12858 weakdef (h)->mark = 1;
12860 if (start_stop != NULL)
12862 /* To work around a glibc bug, mark XXX input sections
12863 when there is a reference to __start_XXX or __stop_XXX
12867 asection *s = h->u2.start_stop_section;
12868 *start_stop = !s->gc_mark;
12873 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12876 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12877 &cookie->locsyms[r_symndx]);
12880 /* COOKIE->rel describes a relocation against section SEC, which is
12881 a section we've decided to keep. Mark the section that contains
12882 the relocation symbol. */
12885 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12887 elf_gc_mark_hook_fn gc_mark_hook,
12888 struct elf_reloc_cookie *cookie)
12891 bfd_boolean start_stop = FALSE;
12893 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12894 while (rsec != NULL)
12896 if (!rsec->gc_mark)
12898 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12899 || (rsec->owner->flags & DYNAMIC) != 0)
12901 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12906 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12911 /* The mark phase of garbage collection. For a given section, mark
12912 it and any sections in this section's group, and all the sections
12913 which define symbols to which it refers. */
12916 _bfd_elf_gc_mark (struct bfd_link_info *info,
12918 elf_gc_mark_hook_fn gc_mark_hook)
12921 asection *group_sec, *eh_frame;
12925 /* Mark all the sections in the group. */
12926 group_sec = elf_section_data (sec)->next_in_group;
12927 if (group_sec && !group_sec->gc_mark)
12928 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12931 /* Look through the section relocs. */
12933 eh_frame = elf_eh_frame_section (sec->owner);
12934 if ((sec->flags & SEC_RELOC) != 0
12935 && sec->reloc_count > 0
12936 && sec != eh_frame)
12938 struct elf_reloc_cookie cookie;
12940 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12944 for (; cookie.rel < cookie.relend; cookie.rel++)
12945 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12950 fini_reloc_cookie_for_section (&cookie, sec);
12954 if (ret && eh_frame && elf_fde_list (sec))
12956 struct elf_reloc_cookie cookie;
12958 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12962 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12963 gc_mark_hook, &cookie))
12965 fini_reloc_cookie_for_section (&cookie, eh_frame);
12969 eh_frame = elf_section_eh_frame_entry (sec);
12970 if (ret && eh_frame && !eh_frame->gc_mark)
12971 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12977 /* Scan and mark sections in a special or debug section group. */
12980 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12982 /* Point to first section of section group. */
12984 /* Used to iterate the section group. */
12987 bfd_boolean is_special_grp = TRUE;
12988 bfd_boolean is_debug_grp = TRUE;
12990 /* First scan to see if group contains any section other than debug
12991 and special section. */
12992 ssec = msec = elf_next_in_group (grp);
12995 if ((msec->flags & SEC_DEBUGGING) == 0)
12996 is_debug_grp = FALSE;
12998 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12999 is_special_grp = FALSE;
13001 msec = elf_next_in_group (msec);
13003 while (msec != ssec);
13005 /* If this is a pure debug section group or pure special section group,
13006 keep all sections in this group. */
13007 if (is_debug_grp || is_special_grp)
13012 msec = elf_next_in_group (msec);
13014 while (msec != ssec);
13018 /* Keep debug and special sections. */
13021 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13022 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13026 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13029 bfd_boolean some_kept;
13030 bfd_boolean debug_frag_seen;
13031 bfd_boolean has_kept_debug_info;
13033 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13035 isec = ibfd->sections;
13036 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13039 /* Ensure all linker created sections are kept,
13040 see if any other section is already marked,
13041 and note if we have any fragmented debug sections. */
13042 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13043 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13045 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13047 else if (isec->gc_mark
13048 && (isec->flags & SEC_ALLOC) != 0
13049 && elf_section_type (isec) != SHT_NOTE)
13052 if (!debug_frag_seen
13053 && (isec->flags & SEC_DEBUGGING)
13054 && CONST_STRNEQ (isec->name, ".debug_line."))
13055 debug_frag_seen = TRUE;
13058 /* If no non-note alloc section in this file will be kept, then
13059 we can toss out the debug and special sections. */
13063 /* Keep debug and special sections like .comment when they are
13064 not part of a group. Also keep section groups that contain
13065 just debug sections or special sections. */
13066 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13068 if ((isec->flags & SEC_GROUP) != 0)
13069 _bfd_elf_gc_mark_debug_special_section_group (isec);
13070 else if (((isec->flags & SEC_DEBUGGING) != 0
13071 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13072 && elf_next_in_group (isec) == NULL)
13074 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13075 has_kept_debug_info = TRUE;
13078 /* Look for CODE sections which are going to be discarded,
13079 and find and discard any fragmented debug sections which
13080 are associated with that code section. */
13081 if (debug_frag_seen)
13082 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13083 if ((isec->flags & SEC_CODE) != 0
13084 && isec->gc_mark == 0)
13089 ilen = strlen (isec->name);
13091 /* Association is determined by the name of the debug
13092 section containing the name of the code section as
13093 a suffix. For example .debug_line.text.foo is a
13094 debug section associated with .text.foo. */
13095 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13099 if (dsec->gc_mark == 0
13100 || (dsec->flags & SEC_DEBUGGING) == 0)
13103 dlen = strlen (dsec->name);
13106 && strncmp (dsec->name + (dlen - ilen),
13107 isec->name, ilen) == 0)
13112 /* Mark debug sections referenced by kept debug sections. */
13113 if (has_kept_debug_info)
13114 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13116 && (isec->flags & SEC_DEBUGGING) != 0)
13117 if (!_bfd_elf_gc_mark (info, isec,
13118 elf_gc_mark_debug_section))
13125 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13128 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13130 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13134 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13135 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13136 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13139 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13142 for (o = sub->sections; o != NULL; o = o->next)
13144 /* When any section in a section group is kept, we keep all
13145 sections in the section group. If the first member of
13146 the section group is excluded, we will also exclude the
13148 if (o->flags & SEC_GROUP)
13150 asection *first = elf_next_in_group (o);
13151 o->gc_mark = first->gc_mark;
13157 /* Skip sweeping sections already excluded. */
13158 if (o->flags & SEC_EXCLUDE)
13161 /* Since this is early in the link process, it is simple
13162 to remove a section from the output. */
13163 o->flags |= SEC_EXCLUDE;
13165 if (info->print_gc_sections && o->size != 0)
13166 /* xgettext:c-format */
13167 _bfd_error_handler (_("Removing unused section '%pA' in file '%pB'"),
13175 /* Propagate collected vtable information. This is called through
13176 elf_link_hash_traverse. */
13179 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13181 /* Those that are not vtables. */
13183 || h->u2.vtable == NULL
13184 || h->u2.vtable->parent == NULL)
13187 /* Those vtables that do not have parents, we cannot merge. */
13188 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13191 /* If we've already been done, exit. */
13192 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13195 /* Make sure the parent's table is up to date. */
13196 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13198 if (h->u2.vtable->used == NULL)
13200 /* None of this table's entries were referenced. Re-use the
13202 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13203 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13208 bfd_boolean *cu, *pu;
13210 /* Or the parent's entries into ours. */
13211 cu = h->u2.vtable->used;
13213 pu = h->u2.vtable->parent->u2.vtable->used;
13216 const struct elf_backend_data *bed;
13217 unsigned int log_file_align;
13219 bed = get_elf_backend_data (h->root.u.def.section->owner);
13220 log_file_align = bed->s->log_file_align;
13221 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13236 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13239 bfd_vma hstart, hend;
13240 Elf_Internal_Rela *relstart, *relend, *rel;
13241 const struct elf_backend_data *bed;
13242 unsigned int log_file_align;
13244 /* Take care of both those symbols that do not describe vtables as
13245 well as those that are not loaded. */
13247 || h->u2.vtable == NULL
13248 || h->u2.vtable->parent == NULL)
13251 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13252 || h->root.type == bfd_link_hash_defweak);
13254 sec = h->root.u.def.section;
13255 hstart = h->root.u.def.value;
13256 hend = hstart + h->size;
13258 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13260 return *(bfd_boolean *) okp = FALSE;
13261 bed = get_elf_backend_data (sec->owner);
13262 log_file_align = bed->s->log_file_align;
13264 relend = relstart + sec->reloc_count;
13266 for (rel = relstart; rel < relend; ++rel)
13267 if (rel->r_offset >= hstart && rel->r_offset < hend)
13269 /* If the entry is in use, do nothing. */
13270 if (h->u2.vtable->used
13271 && (rel->r_offset - hstart) < h->u2.vtable->size)
13273 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13274 if (h->u2.vtable->used[entry])
13277 /* Otherwise, kill it. */
13278 rel->r_offset = rel->r_info = rel->r_addend = 0;
13284 /* Mark sections containing dynamically referenced symbols. When
13285 building shared libraries, we must assume that any visible symbol is
13289 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13291 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13292 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13294 if ((h->root.type == bfd_link_hash_defined
13295 || h->root.type == bfd_link_hash_defweak)
13296 && ((h->ref_dynamic && !h->forced_local)
13297 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13298 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13299 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13300 && (!bfd_link_executable (info)
13301 || info->gc_keep_exported
13302 || info->export_dynamic
13305 && (*d->match) (&d->head, NULL, h->root.root.string)))
13306 && (h->versioned >= versioned
13307 || !bfd_hide_sym_by_version (info->version_info,
13308 h->root.root.string)))))
13309 h->root.u.def.section->flags |= SEC_KEEP;
13314 /* Keep all sections containing symbols undefined on the command-line,
13315 and the section containing the entry symbol. */
13318 _bfd_elf_gc_keep (struct bfd_link_info *info)
13320 struct bfd_sym_chain *sym;
13322 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13324 struct elf_link_hash_entry *h;
13326 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13327 FALSE, FALSE, FALSE);
13330 && (h->root.type == bfd_link_hash_defined
13331 || h->root.type == bfd_link_hash_defweak)
13332 && !bfd_is_abs_section (h->root.u.def.section)
13333 && !bfd_is_und_section (h->root.u.def.section))
13334 h->root.u.def.section->flags |= SEC_KEEP;
13339 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13340 struct bfd_link_info *info)
13342 bfd *ibfd = info->input_bfds;
13344 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13347 struct elf_reloc_cookie cookie;
13349 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13351 sec = ibfd->sections;
13352 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13355 if (!init_reloc_cookie (&cookie, info, ibfd))
13358 for (sec = ibfd->sections; sec; sec = sec->next)
13360 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13361 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13363 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13364 fini_reloc_cookie_rels (&cookie, sec);
13371 /* Do mark and sweep of unused sections. */
13374 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13376 bfd_boolean ok = TRUE;
13378 elf_gc_mark_hook_fn gc_mark_hook;
13379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13380 struct elf_link_hash_table *htab;
13382 if (!bed->can_gc_sections
13383 || !is_elf_hash_table (info->hash))
13385 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13389 bed->gc_keep (info);
13390 htab = elf_hash_table (info);
13392 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13393 at the .eh_frame section if we can mark the FDEs individually. */
13394 for (sub = info->input_bfds;
13395 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13396 sub = sub->link.next)
13399 struct elf_reloc_cookie cookie;
13401 sec = sub->sections;
13402 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13404 sec = bfd_get_section_by_name (sub, ".eh_frame");
13405 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13407 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13408 if (elf_section_data (sec)->sec_info
13409 && (sec->flags & SEC_LINKER_CREATED) == 0)
13410 elf_eh_frame_section (sub) = sec;
13411 fini_reloc_cookie_for_section (&cookie, sec);
13412 sec = bfd_get_next_section_by_name (NULL, sec);
13416 /* Apply transitive closure to the vtable entry usage info. */
13417 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13421 /* Kill the vtable relocations that were not used. */
13422 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13426 /* Mark dynamically referenced symbols. */
13427 if (htab->dynamic_sections_created || info->gc_keep_exported)
13428 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13430 /* Grovel through relocs to find out who stays ... */
13431 gc_mark_hook = bed->gc_mark_hook;
13432 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13436 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13437 || elf_object_id (sub) != elf_hash_table_id (htab)
13438 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13442 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13445 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13446 Also treat note sections as a root, if the section is not part
13447 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13448 well as FINI_ARRAY sections for ld -r. */
13449 for (o = sub->sections; o != NULL; o = o->next)
13451 && (o->flags & SEC_EXCLUDE) == 0
13452 && ((o->flags & SEC_KEEP) != 0
13453 || (bfd_link_relocatable (info)
13454 && ((elf_section_data (o)->this_hdr.sh_type
13455 == SHT_PREINIT_ARRAY)
13456 || (elf_section_data (o)->this_hdr.sh_type
13458 || (elf_section_data (o)->this_hdr.sh_type
13459 == SHT_FINI_ARRAY)))
13460 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13461 && elf_next_in_group (o) == NULL )))
13463 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13468 /* Allow the backend to mark additional target specific sections. */
13469 bed->gc_mark_extra_sections (info, gc_mark_hook);
13471 /* ... and mark SEC_EXCLUDE for those that go. */
13472 return elf_gc_sweep (abfd, info);
13475 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13478 bfd_elf_gc_record_vtinherit (bfd *abfd,
13480 struct elf_link_hash_entry *h,
13483 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13484 struct elf_link_hash_entry **search, *child;
13485 size_t extsymcount;
13486 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13488 /* The sh_info field of the symtab header tells us where the
13489 external symbols start. We don't care about the local symbols at
13491 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13492 if (!elf_bad_symtab (abfd))
13493 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13495 sym_hashes = elf_sym_hashes (abfd);
13496 sym_hashes_end = sym_hashes + extsymcount;
13498 /* Hunt down the child symbol, which is in this section at the same
13499 offset as the relocation. */
13500 for (search = sym_hashes; search != sym_hashes_end; ++search)
13502 if ((child = *search) != NULL
13503 && (child->root.type == bfd_link_hash_defined
13504 || child->root.type == bfd_link_hash_defweak)
13505 && child->root.u.def.section == sec
13506 && child->root.u.def.value == offset)
13510 /* xgettext:c-format */
13511 _bfd_error_handler (_("%pB: %pA+%#Lx: No symbol found for INHERIT"),
13512 abfd, sec, offset);
13513 bfd_set_error (bfd_error_invalid_operation);
13517 if (!child->u2.vtable)
13519 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13520 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13521 if (!child->u2.vtable)
13526 /* This *should* only be the absolute section. It could potentially
13527 be that someone has defined a non-global vtable though, which
13528 would be bad. It isn't worth paging in the local symbols to be
13529 sure though; that case should simply be handled by the assembler. */
13531 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13534 child->u2.vtable->parent = h;
13539 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13542 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13543 asection *sec ATTRIBUTE_UNUSED,
13544 struct elf_link_hash_entry *h,
13547 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13548 unsigned int log_file_align = bed->s->log_file_align;
13552 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13553 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13558 if (addend >= h->u2.vtable->size)
13560 size_t size, bytes, file_align;
13561 bfd_boolean *ptr = h->u2.vtable->used;
13563 /* While the symbol is undefined, we have to be prepared to handle
13565 file_align = 1 << log_file_align;
13566 if (h->root.type == bfd_link_hash_undefined)
13567 size = addend + file_align;
13571 if (addend >= size)
13573 /* Oops! We've got a reference past the defined end of
13574 the table. This is probably a bug -- shall we warn? */
13575 size = addend + file_align;
13578 size = (size + file_align - 1) & -file_align;
13580 /* Allocate one extra entry for use as a "done" flag for the
13581 consolidation pass. */
13582 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13586 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13592 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13593 * sizeof (bfd_boolean));
13594 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13598 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13603 /* And arrange for that done flag to be at index -1. */
13604 h->u2.vtable->used = ptr + 1;
13605 h->u2.vtable->size = size;
13608 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13613 /* Map an ELF section header flag to its corresponding string. */
13617 flagword flag_value;
13618 } elf_flags_to_name_table;
13620 static elf_flags_to_name_table elf_flags_to_names [] =
13622 { "SHF_WRITE", SHF_WRITE },
13623 { "SHF_ALLOC", SHF_ALLOC },
13624 { "SHF_EXECINSTR", SHF_EXECINSTR },
13625 { "SHF_MERGE", SHF_MERGE },
13626 { "SHF_STRINGS", SHF_STRINGS },
13627 { "SHF_INFO_LINK", SHF_INFO_LINK},
13628 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13629 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13630 { "SHF_GROUP", SHF_GROUP },
13631 { "SHF_TLS", SHF_TLS },
13632 { "SHF_MASKOS", SHF_MASKOS },
13633 { "SHF_EXCLUDE", SHF_EXCLUDE },
13636 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13638 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13639 struct flag_info *flaginfo,
13642 const bfd_vma sh_flags = elf_section_flags (section);
13644 if (!flaginfo->flags_initialized)
13646 bfd *obfd = info->output_bfd;
13647 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13648 struct flag_info_list *tf = flaginfo->flag_list;
13650 int without_hex = 0;
13652 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13655 flagword (*lookup) (char *);
13657 lookup = bed->elf_backend_lookup_section_flags_hook;
13658 if (lookup != NULL)
13660 flagword hexval = (*lookup) ((char *) tf->name);
13664 if (tf->with == with_flags)
13665 with_hex |= hexval;
13666 else if (tf->with == without_flags)
13667 without_hex |= hexval;
13672 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13674 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13676 if (tf->with == with_flags)
13677 with_hex |= elf_flags_to_names[i].flag_value;
13678 else if (tf->with == without_flags)
13679 without_hex |= elf_flags_to_names[i].flag_value;
13686 info->callbacks->einfo
13687 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13691 flaginfo->flags_initialized = TRUE;
13692 flaginfo->only_with_flags |= with_hex;
13693 flaginfo->not_with_flags |= without_hex;
13696 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13699 if ((flaginfo->not_with_flags & sh_flags) != 0)
13705 struct alloc_got_off_arg {
13707 struct bfd_link_info *info;
13710 /* We need a special top-level link routine to convert got reference counts
13711 to real got offsets. */
13714 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13716 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13717 bfd *obfd = gofarg->info->output_bfd;
13718 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13720 if (h->got.refcount > 0)
13722 h->got.offset = gofarg->gotoff;
13723 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13726 h->got.offset = (bfd_vma) -1;
13731 /* And an accompanying bit to work out final got entry offsets once
13732 we're done. Should be called from final_link. */
13735 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13736 struct bfd_link_info *info)
13739 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13741 struct alloc_got_off_arg gofarg;
13743 BFD_ASSERT (abfd == info->output_bfd);
13745 if (! is_elf_hash_table (info->hash))
13748 /* The GOT offset is relative to the .got section, but the GOT header is
13749 put into the .got.plt section, if the backend uses it. */
13750 if (bed->want_got_plt)
13753 gotoff = bed->got_header_size;
13755 /* Do the local .got entries first. */
13756 for (i = info->input_bfds; i; i = i->link.next)
13758 bfd_signed_vma *local_got;
13759 size_t j, locsymcount;
13760 Elf_Internal_Shdr *symtab_hdr;
13762 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13765 local_got = elf_local_got_refcounts (i);
13769 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13770 if (elf_bad_symtab (i))
13771 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13773 locsymcount = symtab_hdr->sh_info;
13775 for (j = 0; j < locsymcount; ++j)
13777 if (local_got[j] > 0)
13779 local_got[j] = gotoff;
13780 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13783 local_got[j] = (bfd_vma) -1;
13787 /* Then the global .got entries. .plt refcounts are handled by
13788 adjust_dynamic_symbol */
13789 gofarg.gotoff = gotoff;
13790 gofarg.info = info;
13791 elf_link_hash_traverse (elf_hash_table (info),
13792 elf_gc_allocate_got_offsets,
13797 /* Many folk need no more in the way of final link than this, once
13798 got entry reference counting is enabled. */
13801 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13803 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13806 /* Invoke the regular ELF backend linker to do all the work. */
13807 return bfd_elf_final_link (abfd, info);
13811 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13813 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13815 if (rcookie->bad_symtab)
13816 rcookie->rel = rcookie->rels;
13818 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13820 unsigned long r_symndx;
13822 if (! rcookie->bad_symtab)
13823 if (rcookie->rel->r_offset > offset)
13825 if (rcookie->rel->r_offset != offset)
13828 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13829 if (r_symndx == STN_UNDEF)
13832 if (r_symndx >= rcookie->locsymcount
13833 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13835 struct elf_link_hash_entry *h;
13837 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13839 while (h->root.type == bfd_link_hash_indirect
13840 || h->root.type == bfd_link_hash_warning)
13841 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13843 if ((h->root.type == bfd_link_hash_defined
13844 || h->root.type == bfd_link_hash_defweak)
13845 && (h->root.u.def.section->owner != rcookie->abfd
13846 || h->root.u.def.section->kept_section != NULL
13847 || discarded_section (h->root.u.def.section)))
13852 /* It's not a relocation against a global symbol,
13853 but it could be a relocation against a local
13854 symbol for a discarded section. */
13856 Elf_Internal_Sym *isym;
13858 /* Need to: get the symbol; get the section. */
13859 isym = &rcookie->locsyms[r_symndx];
13860 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13862 && (isec->kept_section != NULL
13863 || discarded_section (isec)))
13871 /* Discard unneeded references to discarded sections.
13872 Returns -1 on error, 1 if any section's size was changed, 0 if
13873 nothing changed. This function assumes that the relocations are in
13874 sorted order, which is true for all known assemblers. */
13877 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13879 struct elf_reloc_cookie cookie;
13884 if (info->traditional_format
13885 || !is_elf_hash_table (info->hash))
13888 o = bfd_get_section_by_name (output_bfd, ".stab");
13893 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13896 || i->reloc_count == 0
13897 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13901 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13904 if (!init_reloc_cookie_for_section (&cookie, info, i))
13907 if (_bfd_discard_section_stabs (abfd, i,
13908 elf_section_data (i)->sec_info,
13909 bfd_elf_reloc_symbol_deleted_p,
13913 fini_reloc_cookie_for_section (&cookie, i);
13918 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13919 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13923 int eh_changed = 0;
13924 unsigned int eh_alignment;
13926 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13932 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13935 if (!init_reloc_cookie_for_section (&cookie, info, i))
13938 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13939 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13940 bfd_elf_reloc_symbol_deleted_p,
13944 if (i->size != i->rawsize)
13948 fini_reloc_cookie_for_section (&cookie, i);
13951 eh_alignment = 1 << o->alignment_power;
13952 /* Skip over zero terminator, and prevent empty sections from
13953 adding alignment padding at the end. */
13954 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13956 i->flags |= SEC_EXCLUDE;
13957 else if (i->size > 4)
13959 /* The last non-empty eh_frame section doesn't need padding. */
13962 /* Any prior sections must pad the last FDE out to the output
13963 section alignment. Otherwise we might have zero padding
13964 between sections, which would be seen as a terminator. */
13965 for (; i != NULL; i = i->map_tail.s)
13967 /* All but the last zero terminator should have been removed. */
13972 = (i->size + eh_alignment - 1) & -eh_alignment;
13973 if (i->size != size)
13981 elf_link_hash_traverse (elf_hash_table (info),
13982 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13985 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13987 const struct elf_backend_data *bed;
13990 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13992 s = abfd->sections;
13993 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13996 bed = get_elf_backend_data (abfd);
13998 if (bed->elf_backend_discard_info != NULL)
14000 if (!init_reloc_cookie (&cookie, info, abfd))
14003 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14006 fini_reloc_cookie (&cookie, abfd);
14010 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14011 _bfd_elf_end_eh_frame_parsing (info);
14013 if (info->eh_frame_hdr_type
14014 && !bfd_link_relocatable (info)
14015 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14022 _bfd_elf_section_already_linked (bfd *abfd,
14024 struct bfd_link_info *info)
14027 const char *name, *key;
14028 struct bfd_section_already_linked *l;
14029 struct bfd_section_already_linked_hash_entry *already_linked_list;
14031 if (sec->output_section == bfd_abs_section_ptr)
14034 flags = sec->flags;
14036 /* Return if it isn't a linkonce section. A comdat group section
14037 also has SEC_LINK_ONCE set. */
14038 if ((flags & SEC_LINK_ONCE) == 0)
14041 /* Don't put group member sections on our list of already linked
14042 sections. They are handled as a group via their group section. */
14043 if (elf_sec_group (sec) != NULL)
14046 /* For a SHT_GROUP section, use the group signature as the key. */
14048 if ((flags & SEC_GROUP) != 0
14049 && elf_next_in_group (sec) != NULL
14050 && elf_group_name (elf_next_in_group (sec)) != NULL)
14051 key = elf_group_name (elf_next_in_group (sec));
14054 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14055 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14056 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14059 /* Must be a user linkonce section that doesn't follow gcc's
14060 naming convention. In this case we won't be matching
14061 single member groups. */
14065 already_linked_list = bfd_section_already_linked_table_lookup (key);
14067 for (l = already_linked_list->entry; l != NULL; l = l->next)
14069 /* We may have 2 different types of sections on the list: group
14070 sections with a signature of <key> (<key> is some string),
14071 and linkonce sections named .gnu.linkonce.<type>.<key>.
14072 Match like sections. LTO plugin sections are an exception.
14073 They are always named .gnu.linkonce.t.<key> and match either
14074 type of section. */
14075 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14076 && ((flags & SEC_GROUP) != 0
14077 || strcmp (name, l->sec->name) == 0))
14078 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14080 /* The section has already been linked. See if we should
14081 issue a warning. */
14082 if (!_bfd_handle_already_linked (sec, l, info))
14085 if (flags & SEC_GROUP)
14087 asection *first = elf_next_in_group (sec);
14088 asection *s = first;
14092 s->output_section = bfd_abs_section_ptr;
14093 /* Record which group discards it. */
14094 s->kept_section = l->sec;
14095 s = elf_next_in_group (s);
14096 /* These lists are circular. */
14106 /* A single member comdat group section may be discarded by a
14107 linkonce section and vice versa. */
14108 if ((flags & SEC_GROUP) != 0)
14110 asection *first = elf_next_in_group (sec);
14112 if (first != NULL && elf_next_in_group (first) == first)
14113 /* Check this single member group against linkonce sections. */
14114 for (l = already_linked_list->entry; l != NULL; l = l->next)
14115 if ((l->sec->flags & SEC_GROUP) == 0
14116 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14118 first->output_section = bfd_abs_section_ptr;
14119 first->kept_section = l->sec;
14120 sec->output_section = bfd_abs_section_ptr;
14125 /* Check this linkonce section against single member groups. */
14126 for (l = already_linked_list->entry; l != NULL; l = l->next)
14127 if (l->sec->flags & SEC_GROUP)
14129 asection *first = elf_next_in_group (l->sec);
14132 && elf_next_in_group (first) == first
14133 && bfd_elf_match_symbols_in_sections (first, sec, info))
14135 sec->output_section = bfd_abs_section_ptr;
14136 sec->kept_section = first;
14141 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14142 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14143 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14144 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14145 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14146 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14147 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14148 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14149 The reverse order cannot happen as there is never a bfd with only the
14150 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14151 matter as here were are looking only for cross-bfd sections. */
14153 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14154 for (l = already_linked_list->entry; l != NULL; l = l->next)
14155 if ((l->sec->flags & SEC_GROUP) == 0
14156 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14158 if (abfd != l->sec->owner)
14159 sec->output_section = bfd_abs_section_ptr;
14163 /* This is the first section with this name. Record it. */
14164 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14165 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14166 return sec->output_section == bfd_abs_section_ptr;
14170 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14172 return sym->st_shndx == SHN_COMMON;
14176 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14182 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14184 return bfd_com_section_ptr;
14188 _bfd_elf_default_got_elt_size (bfd *abfd,
14189 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14190 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14191 bfd *ibfd ATTRIBUTE_UNUSED,
14192 unsigned long symndx ATTRIBUTE_UNUSED)
14194 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14195 return bed->s->arch_size / 8;
14198 /* Routines to support the creation of dynamic relocs. */
14200 /* Returns the name of the dynamic reloc section associated with SEC. */
14202 static const char *
14203 get_dynamic_reloc_section_name (bfd * abfd,
14205 bfd_boolean is_rela)
14208 const char *old_name = bfd_get_section_name (NULL, sec);
14209 const char *prefix = is_rela ? ".rela" : ".rel";
14211 if (old_name == NULL)
14214 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14215 sprintf (name, "%s%s", prefix, old_name);
14220 /* Returns the dynamic reloc section associated with SEC.
14221 If necessary compute the name of the dynamic reloc section based
14222 on SEC's name (looked up in ABFD's string table) and the setting
14226 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14228 bfd_boolean is_rela)
14230 asection * reloc_sec = elf_section_data (sec)->sreloc;
14232 if (reloc_sec == NULL)
14234 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14238 reloc_sec = bfd_get_linker_section (abfd, name);
14240 if (reloc_sec != NULL)
14241 elf_section_data (sec)->sreloc = reloc_sec;
14248 /* Returns the dynamic reloc section associated with SEC. If the
14249 section does not exist it is created and attached to the DYNOBJ
14250 bfd and stored in the SRELOC field of SEC's elf_section_data
14253 ALIGNMENT is the alignment for the newly created section and
14254 IS_RELA defines whether the name should be .rela.<SEC's name>
14255 or .rel.<SEC's name>. The section name is looked up in the
14256 string table associated with ABFD. */
14259 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14261 unsigned int alignment,
14263 bfd_boolean is_rela)
14265 asection * reloc_sec = elf_section_data (sec)->sreloc;
14267 if (reloc_sec == NULL)
14269 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14274 reloc_sec = bfd_get_linker_section (dynobj, name);
14276 if (reloc_sec == NULL)
14278 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14279 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14280 if ((sec->flags & SEC_ALLOC) != 0)
14281 flags |= SEC_ALLOC | SEC_LOAD;
14283 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14284 if (reloc_sec != NULL)
14286 /* _bfd_elf_get_sec_type_attr chooses a section type by
14287 name. Override as it may be wrong, eg. for a user
14288 section named "auto" we'll get ".relauto" which is
14289 seen to be a .rela section. */
14290 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14291 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14296 elf_section_data (sec)->sreloc = reloc_sec;
14302 /* Copy the ELF symbol type and other attributes for a linker script
14303 assignment from HSRC to HDEST. Generally this should be treated as
14304 if we found a strong non-dynamic definition for HDEST (except that
14305 ld ignores multiple definition errors). */
14307 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14308 struct bfd_link_hash_entry *hdest,
14309 struct bfd_link_hash_entry *hsrc)
14311 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14312 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14313 Elf_Internal_Sym isym;
14315 ehdest->type = ehsrc->type;
14316 ehdest->target_internal = ehsrc->target_internal;
14318 isym.st_other = ehsrc->other;
14319 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14322 /* Append a RELA relocation REL to section S in BFD. */
14325 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14327 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14328 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14329 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14330 bed->s->swap_reloca_out (abfd, rel, loc);
14333 /* Append a REL relocation REL to section S in BFD. */
14336 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14338 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14339 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14340 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14341 bed->s->swap_reloc_out (abfd, rel, loc);
14344 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14346 struct bfd_link_hash_entry *
14347 bfd_elf_define_start_stop (struct bfd_link_info *info,
14348 const char *symbol, asection *sec)
14350 struct elf_link_hash_entry *h;
14352 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14353 FALSE, FALSE, TRUE);
14355 && (h->root.type == bfd_link_hash_undefined
14356 || h->root.type == bfd_link_hash_undefweak
14357 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14359 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14360 h->root.type = bfd_link_hash_defined;
14361 h->root.u.def.section = sec;
14362 h->root.u.def.value = 0;
14363 h->def_regular = 1;
14364 h->def_dynamic = 0;
14366 h->u2.start_stop_section = sec;
14367 if (symbol[0] == '.')
14369 /* .startof. and .sizeof. symbols are local. */
14370 const struct elf_backend_data *bed;
14371 bed = get_elf_backend_data (info->output_bfd);
14372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14376 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14377 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14379 bfd_elf_link_record_dynamic_symbol (info, h);