1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
96 && discard ? discarded_section (isec) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
233 hash_table->dynobj = abfd;
236 if (hash_table->dynstr == NULL)
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
260 if (! is_elf_hash_table (info->hash))
263 if (elf_hash_table (info)->dynamic_sections_created)
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
272 flags = bed->dynamic_sec_flags;
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
295 || ! bfd_set_section_alignment (abfd, s, 1))
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 elf_hash_table (info)->dynsym = s;
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
342 if (info->emit_gnu_hash)
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
370 /* Create dynamic sections when linking against a dynamic object. */
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 if (bed->want_plt_sym)
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
422 if (! _bfd_elf_create_got_section (abfd, info))
425 if (bed->want_dynbss)
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (bed->want_dynrelro)
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
462 if (bfd_link_executable (info))
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
473 if (bed->want_dynrelro)
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
483 htab->sreldynrelro = s;
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
503 if (h->dynindx == -1)
505 struct elf_strtab_hash *dynstr;
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
522 if (!elf_hash_table (info)->is_relocatable_executable)
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
533 dynstr = elf_hash_table (info)->dynstr;
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
542 /* We don't put any version information in the dynamic string
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
559 if (indx == (size_t) -1)
561 h->dynstr_index = indx;
567 /* Mark a symbol dynamic. */
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
596 bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
602 struct elf_link_hash_entry *h, *hv;
603 struct elf_link_hash_table *htab;
604 const struct elf_backend_data *bed;
606 if (!is_elf_hash_table (info->hash))
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
617 if (h->versioned == unknown)
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
626 h->versioned = versioned;
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 switch (h->root.type)
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h->root.type = bfd_link_hash_new;
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
653 case bfd_link_hash_new:
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed = get_elf_backend_data (output_bfd);
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
682 h->root.type = bfd_link_hash_undefined;
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
691 h->verinfo.verdef = NULL;
693 /* Make sure this symbol is not garbage collected. */
700 bed = get_elf_backend_data (output_bfd);
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 if (!bfd_link_relocatable (info)
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
728 struct elf_link_hash_entry *def = weakdef (h);
730 if (def->dynindx == -1
731 && !bfd_elf_link_record_dynamic_symbol (info, def))
739 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
740 success, and 2 on a failure caused by attempting to record a symbol
741 in a discarded section, eg. a discarded link-once section symbol. */
744 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
749 struct elf_link_local_dynamic_entry *entry;
750 struct elf_link_hash_table *eht;
751 struct elf_strtab_hash *dynstr;
754 Elf_External_Sym_Shndx eshndx;
755 char esym[sizeof (Elf64_External_Sym)];
757 if (! is_elf_hash_table (info->hash))
760 /* See if the entry exists already. */
761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
762 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
765 amt = sizeof (*entry);
766 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
770 /* Go find the symbol, so that we can find it's name. */
771 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
772 1, input_indx, &entry->isym, esym, &eshndx))
774 bfd_release (input_bfd, entry);
778 if (entry->isym.st_shndx != SHN_UNDEF
779 && entry->isym.st_shndx < SHN_LORESERVE)
783 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
784 if (s == NULL || bfd_is_abs_section (s->output_section))
786 /* We can still bfd_release here as nothing has done another
787 bfd_alloc. We can't do this later in this function. */
788 bfd_release (input_bfd, entry);
793 name = (bfd_elf_string_from_elf_section
794 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
795 entry->isym.st_name));
797 dynstr = elf_hash_table (info)->dynstr;
800 /* Create a strtab to hold the dynamic symbol names. */
801 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
806 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
807 if (dynstr_index == (size_t) -1)
809 entry->isym.st_name = dynstr_index;
811 eht = elf_hash_table (info);
813 entry->next = eht->dynlocal;
814 eht->dynlocal = entry;
815 entry->input_bfd = input_bfd;
816 entry->input_indx = input_indx;
819 /* Whatever binding the symbol had before, it's now local. */
821 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
823 /* The dynindx will be set at the end of size_dynamic_sections. */
828 /* Return the dynindex of a local dynamic symbol. */
831 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
835 struct elf_link_local_dynamic_entry *e;
837 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
838 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
843 /* This function is used to renumber the dynamic symbols, if some of
844 them are removed because they are marked as local. This is called
845 via elf_link_hash_traverse. */
848 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
851 size_t *count = (size_t *) data;
856 if (h->dynindx != -1)
857 h->dynindx = ++(*count);
863 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
864 STB_LOCAL binding. */
867 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
870 size_t *count = (size_t *) data;
872 if (!h->forced_local)
875 if (h->dynindx != -1)
876 h->dynindx = ++(*count);
881 /* Return true if the dynamic symbol for a given section should be
882 omitted when creating a shared library. */
884 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
885 struct bfd_link_info *info,
888 struct elf_link_hash_table *htab;
891 switch (elf_section_data (p)->this_hdr.sh_type)
895 /* If sh_type is yet undecided, assume it could be
896 SHT_PROGBITS/SHT_NOBITS. */
898 htab = elf_hash_table (info);
899 if (p == htab->tls_sec)
902 if (htab->text_index_section != NULL)
903 return p != htab->text_index_section && p != htab->data_index_section;
905 return (htab->dynobj != NULL
906 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
907 && ip->output_section == p);
909 /* There shouldn't be section relative relocations
910 against any other section. */
916 /* Assign dynsym indices. In a shared library we generate a section
917 symbol for each output section, which come first. Next come symbols
918 which have been forced to local binding. Then all of the back-end
919 allocated local dynamic syms, followed by the rest of the global
923 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
924 struct bfd_link_info *info,
925 unsigned long *section_sym_count)
927 unsigned long dynsymcount = 0;
929 if (bfd_link_pic (info)
930 || elf_hash_table (info)->is_relocatable_executable)
932 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
934 for (p = output_bfd->sections; p ; p = p->next)
935 if ((p->flags & SEC_EXCLUDE) == 0
936 && (p->flags & SEC_ALLOC) != 0
937 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
938 elf_section_data (p)->dynindx = ++dynsymcount;
940 elf_section_data (p)->dynindx = 0;
942 *section_sym_count = dynsymcount;
944 elf_link_hash_traverse (elf_hash_table (info),
945 elf_link_renumber_local_hash_table_dynsyms,
948 if (elf_hash_table (info)->dynlocal)
950 struct elf_link_local_dynamic_entry *p;
951 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
952 p->dynindx = ++dynsymcount;
954 elf_hash_table (info)->local_dynsymcount = dynsymcount;
956 elf_link_hash_traverse (elf_hash_table (info),
957 elf_link_renumber_hash_table_dynsyms,
960 /* There is an unused NULL entry at the head of the table which we
961 must account for in our count even if the table is empty since it
962 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
966 elf_hash_table (info)->dynsymcount = dynsymcount;
970 /* Merge st_other field. */
973 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
974 const Elf_Internal_Sym *isym, asection *sec,
975 bfd_boolean definition, bfd_boolean dynamic)
977 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
979 /* If st_other has a processor-specific meaning, specific
980 code might be needed here. */
981 if (bed->elf_backend_merge_symbol_attribute)
982 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
987 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
988 unsigned hvis = ELF_ST_VISIBILITY (h->other);
990 /* Keep the most constraining visibility. Leave the remainder
991 of the st_other field to elf_backend_merge_symbol_attribute. */
992 if (symvis - 1 < hvis - 1)
993 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
996 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
997 && (sec->flags & SEC_READONLY) == 0)
998 h->protected_def = 1;
1001 /* This function is called when we want to merge a new symbol with an
1002 existing symbol. It handles the various cases which arise when we
1003 find a definition in a dynamic object, or when there is already a
1004 definition in a dynamic object. The new symbol is described by
1005 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1006 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1007 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1008 of an old common symbol. We set OVERRIDE if the old symbol is
1009 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1010 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1011 to change. By OK to change, we mean that we shouldn't warn if the
1012 type or size does change. */
1015 _bfd_elf_merge_symbol (bfd *abfd,
1016 struct bfd_link_info *info,
1018 Elf_Internal_Sym *sym,
1021 struct elf_link_hash_entry **sym_hash,
1023 bfd_boolean *pold_weak,
1024 unsigned int *pold_alignment,
1026 bfd_boolean *override,
1027 bfd_boolean *type_change_ok,
1028 bfd_boolean *size_change_ok,
1029 bfd_boolean *matched)
1031 asection *sec, *oldsec;
1032 struct elf_link_hash_entry *h;
1033 struct elf_link_hash_entry *hi;
1034 struct elf_link_hash_entry *flip;
1037 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1038 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1039 const struct elf_backend_data *bed;
1041 bfd_boolean default_sym = *matched;
1047 bind = ELF_ST_BIND (sym->st_info);
1049 if (! bfd_is_und_section (sec))
1050 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1052 h = ((struct elf_link_hash_entry *)
1053 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1058 bed = get_elf_backend_data (abfd);
1060 /* NEW_VERSION is the symbol version of the new symbol. */
1061 if (h->versioned != unversioned)
1063 /* Symbol version is unknown or versioned. */
1064 new_version = strrchr (name, ELF_VER_CHR);
1067 if (h->versioned == unknown)
1069 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1070 h->versioned = versioned_hidden;
1072 h->versioned = versioned;
1075 if (new_version[0] == '\0')
1079 h->versioned = unversioned;
1084 /* For merging, we only care about real symbols. But we need to make
1085 sure that indirect symbol dynamic flags are updated. */
1087 while (h->root.type == bfd_link_hash_indirect
1088 || h->root.type == bfd_link_hash_warning)
1089 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1093 if (hi == h || h->root.type == bfd_link_hash_new)
1097 /* OLD_HIDDEN is true if the existing symbol is only visible
1098 to the symbol with the same symbol version. NEW_HIDDEN is
1099 true if the new symbol is only visible to the symbol with
1100 the same symbol version. */
1101 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1102 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1103 if (!old_hidden && !new_hidden)
1104 /* The new symbol matches the existing symbol if both
1109 /* OLD_VERSION is the symbol version of the existing
1113 if (h->versioned >= versioned)
1114 old_version = strrchr (h->root.root.string,
1119 /* The new symbol matches the existing symbol if they
1120 have the same symbol version. */
1121 *matched = (old_version == new_version
1122 || (old_version != NULL
1123 && new_version != NULL
1124 && strcmp (old_version, new_version) == 0));
1129 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1134 switch (h->root.type)
1139 case bfd_link_hash_undefined:
1140 case bfd_link_hash_undefweak:
1141 oldbfd = h->root.u.undef.abfd;
1144 case bfd_link_hash_defined:
1145 case bfd_link_hash_defweak:
1146 oldbfd = h->root.u.def.section->owner;
1147 oldsec = h->root.u.def.section;
1150 case bfd_link_hash_common:
1151 oldbfd = h->root.u.c.p->section->owner;
1152 oldsec = h->root.u.c.p->section;
1154 *pold_alignment = h->root.u.c.p->alignment_power;
1157 if (poldbfd && *poldbfd == NULL)
1160 /* Differentiate strong and weak symbols. */
1161 newweak = bind == STB_WEAK;
1162 oldweak = (h->root.type == bfd_link_hash_defweak
1163 || h->root.type == bfd_link_hash_undefweak);
1165 *pold_weak = oldweak;
1167 /* We have to check it for every instance since the first few may be
1168 references and not all compilers emit symbol type for undefined
1170 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1172 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1173 respectively, is from a dynamic object. */
1175 newdyn = (abfd->flags & DYNAMIC) != 0;
1177 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1178 syms and defined syms in dynamic libraries respectively.
1179 ref_dynamic on the other hand can be set for a symbol defined in
1180 a dynamic library, and def_dynamic may not be set; When the
1181 definition in a dynamic lib is overridden by a definition in the
1182 executable use of the symbol in the dynamic lib becomes a
1183 reference to the executable symbol. */
1186 if (bfd_is_und_section (sec))
1188 if (bind != STB_WEAK)
1190 h->ref_dynamic_nonweak = 1;
1191 hi->ref_dynamic_nonweak = 1;
1196 /* Update the existing symbol only if they match. */
1199 hi->dynamic_def = 1;
1203 /* If we just created the symbol, mark it as being an ELF symbol.
1204 Other than that, there is nothing to do--there is no merge issue
1205 with a newly defined symbol--so we just return. */
1207 if (h->root.type == bfd_link_hash_new)
1213 /* In cases involving weak versioned symbols, we may wind up trying
1214 to merge a symbol with itself. Catch that here, to avoid the
1215 confusion that results if we try to override a symbol with
1216 itself. The additional tests catch cases like
1217 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1218 dynamic object, which we do want to handle here. */
1220 && (newweak || oldweak)
1221 && ((abfd->flags & DYNAMIC) == 0
1222 || !h->def_regular))
1227 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1228 else if (oldsec != NULL)
1230 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1231 indices used by MIPS ELF. */
1232 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1235 /* Handle a case where plugin_notice won't be called and thus won't
1236 set the non_ir_ref flags on the first pass over symbols. */
1238 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1239 && newdyn != olddyn)
1241 h->root.non_ir_ref_dynamic = TRUE;
1242 hi->root.non_ir_ref_dynamic = TRUE;
1245 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1246 respectively, appear to be a definition rather than reference. */
1248 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1250 olddef = (h->root.type != bfd_link_hash_undefined
1251 && h->root.type != bfd_link_hash_undefweak
1252 && h->root.type != bfd_link_hash_common);
1254 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1255 respectively, appear to be a function. */
1257 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1258 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1260 oldfunc = (h->type != STT_NOTYPE
1261 && bed->is_function_type (h->type));
1263 if (!(newfunc && oldfunc)
1264 && ELF_ST_TYPE (sym->st_info) != h->type
1265 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1266 && h->type != STT_NOTYPE
1267 && (newdef || bfd_is_com_section (sec))
1268 && (olddef || h->root.type == bfd_link_hash_common))
1270 /* If creating a default indirect symbol ("foo" or "foo@") from
1271 a dynamic versioned definition ("foo@@") skip doing so if
1272 there is an existing regular definition with a different
1273 type. We don't want, for example, a "time" variable in the
1274 executable overriding a "time" function in a shared library. */
1282 /* When adding a symbol from a regular object file after we have
1283 created indirect symbols, undo the indirection and any
1290 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1291 h->forced_local = 0;
1295 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1297 h->root.type = bfd_link_hash_undefined;
1298 h->root.u.undef.abfd = abfd;
1302 h->root.type = bfd_link_hash_new;
1303 h->root.u.undef.abfd = NULL;
1309 /* Check TLS symbols. We don't check undefined symbols introduced
1310 by "ld -u" which have no type (and oldbfd NULL), and we don't
1311 check symbols from plugins because they also have no type. */
1313 && (oldbfd->flags & BFD_PLUGIN) == 0
1314 && (abfd->flags & BFD_PLUGIN) == 0
1315 && ELF_ST_TYPE (sym->st_info) != h->type
1316 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1319 bfd_boolean ntdef, tdef;
1320 asection *ntsec, *tsec;
1322 if (h->type == STT_TLS)
1343 /* xgettext:c-format */
1344 (_("%s: TLS definition in %B section %A "
1345 "mismatches non-TLS definition in %B section %A"),
1346 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1347 else if (!tdef && !ntdef)
1349 /* xgettext:c-format */
1350 (_("%s: TLS reference in %B "
1351 "mismatches non-TLS reference in %B"),
1352 h->root.root.string, tbfd, ntbfd);
1355 /* xgettext:c-format */
1356 (_("%s: TLS definition in %B section %A "
1357 "mismatches non-TLS reference in %B"),
1358 h->root.root.string, tbfd, tsec, ntbfd);
1361 /* xgettext:c-format */
1362 (_("%s: TLS reference in %B "
1363 "mismatches non-TLS definition in %B section %A"),
1364 h->root.root.string, tbfd, ntbfd, ntsec);
1366 bfd_set_error (bfd_error_bad_value);
1370 /* If the old symbol has non-default visibility, we ignore the new
1371 definition from a dynamic object. */
1373 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1374 && !bfd_is_und_section (sec))
1377 /* Make sure this symbol is dynamic. */
1379 hi->ref_dynamic = 1;
1380 /* A protected symbol has external availability. Make sure it is
1381 recorded as dynamic.
1383 FIXME: Should we check type and size for protected symbol? */
1384 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1385 return bfd_elf_link_record_dynamic_symbol (info, h);
1390 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1393 /* If the new symbol with non-default visibility comes from a
1394 relocatable file and the old definition comes from a dynamic
1395 object, we remove the old definition. */
1396 if (hi->root.type == bfd_link_hash_indirect)
1398 /* Handle the case where the old dynamic definition is
1399 default versioned. We need to copy the symbol info from
1400 the symbol with default version to the normal one if it
1401 was referenced before. */
1404 hi->root.type = h->root.type;
1405 h->root.type = bfd_link_hash_indirect;
1406 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1408 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1409 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1411 /* If the new symbol is hidden or internal, completely undo
1412 any dynamic link state. */
1413 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1414 h->forced_local = 0;
1421 /* FIXME: Should we check type and size for protected symbol? */
1431 /* If the old symbol was undefined before, then it will still be
1432 on the undefs list. If the new symbol is undefined or
1433 common, we can't make it bfd_link_hash_new here, because new
1434 undefined or common symbols will be added to the undefs list
1435 by _bfd_generic_link_add_one_symbol. Symbols may not be
1436 added twice to the undefs list. Also, if the new symbol is
1437 undefweak then we don't want to lose the strong undef. */
1438 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1440 h->root.type = bfd_link_hash_undefined;
1441 h->root.u.undef.abfd = abfd;
1445 h->root.type = bfd_link_hash_new;
1446 h->root.u.undef.abfd = NULL;
1449 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1451 /* If the new symbol is hidden or internal, completely undo
1452 any dynamic link state. */
1453 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1454 h->forced_local = 0;
1460 /* FIXME: Should we check type and size for protected symbol? */
1466 /* If a new weak symbol definition comes from a regular file and the
1467 old symbol comes from a dynamic library, we treat the new one as
1468 strong. Similarly, an old weak symbol definition from a regular
1469 file is treated as strong when the new symbol comes from a dynamic
1470 library. Further, an old weak symbol from a dynamic library is
1471 treated as strong if the new symbol is from a dynamic library.
1472 This reflects the way glibc's ld.so works.
1474 Also allow a weak symbol to override a linker script symbol
1475 defined by an early pass over the script. This is done so the
1476 linker knows the symbol is defined in an object file, for the
1477 DEFINED script function.
1479 Do this before setting *type_change_ok or *size_change_ok so that
1480 we warn properly when dynamic library symbols are overridden. */
1482 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1484 if (olddef && newdyn)
1487 /* Allow changes between different types of function symbol. */
1488 if (newfunc && oldfunc)
1489 *type_change_ok = TRUE;
1491 /* It's OK to change the type if either the existing symbol or the
1492 new symbol is weak. A type change is also OK if the old symbol
1493 is undefined and the new symbol is defined. */
1498 && h->root.type == bfd_link_hash_undefined))
1499 *type_change_ok = TRUE;
1501 /* It's OK to change the size if either the existing symbol or the
1502 new symbol is weak, or if the old symbol is undefined. */
1505 || h->root.type == bfd_link_hash_undefined)
1506 *size_change_ok = TRUE;
1508 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1509 symbol, respectively, appears to be a common symbol in a dynamic
1510 object. If a symbol appears in an uninitialized section, and is
1511 not weak, and is not a function, then it may be a common symbol
1512 which was resolved when the dynamic object was created. We want
1513 to treat such symbols specially, because they raise special
1514 considerations when setting the symbol size: if the symbol
1515 appears as a common symbol in a regular object, and the size in
1516 the regular object is larger, we must make sure that we use the
1517 larger size. This problematic case can always be avoided in C,
1518 but it must be handled correctly when using Fortran shared
1521 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1522 likewise for OLDDYNCOMMON and OLDDEF.
1524 Note that this test is just a heuristic, and that it is quite
1525 possible to have an uninitialized symbol in a shared object which
1526 is really a definition, rather than a common symbol. This could
1527 lead to some minor confusion when the symbol really is a common
1528 symbol in some regular object. However, I think it will be
1534 && (sec->flags & SEC_ALLOC) != 0
1535 && (sec->flags & SEC_LOAD) == 0
1538 newdyncommon = TRUE;
1540 newdyncommon = FALSE;
1544 && h->root.type == bfd_link_hash_defined
1546 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1547 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1550 olddyncommon = TRUE;
1552 olddyncommon = FALSE;
1554 /* We now know everything about the old and new symbols. We ask the
1555 backend to check if we can merge them. */
1556 if (bed->merge_symbol != NULL)
1558 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1563 /* There are multiple definitions of a normal symbol. Skip the
1564 default symbol as well as definition from an IR object. */
1565 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1566 && !default_sym && h->def_regular
1568 && (oldbfd->flags & BFD_PLUGIN) != 0
1569 && (abfd->flags & BFD_PLUGIN) == 0))
1571 /* Handle a multiple definition. */
1572 (*info->callbacks->multiple_definition) (info, &h->root,
1573 abfd, sec, *pvalue);
1578 /* If both the old and the new symbols look like common symbols in a
1579 dynamic object, set the size of the symbol to the larger of the
1584 && sym->st_size != h->size)
1586 /* Since we think we have two common symbols, issue a multiple
1587 common warning if desired. Note that we only warn if the
1588 size is different. If the size is the same, we simply let
1589 the old symbol override the new one as normally happens with
1590 symbols defined in dynamic objects. */
1592 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1593 bfd_link_hash_common, sym->st_size);
1594 if (sym->st_size > h->size)
1595 h->size = sym->st_size;
1597 *size_change_ok = TRUE;
1600 /* If we are looking at a dynamic object, and we have found a
1601 definition, we need to see if the symbol was already defined by
1602 some other object. If so, we want to use the existing
1603 definition, and we do not want to report a multiple symbol
1604 definition error; we do this by clobbering *PSEC to be
1605 bfd_und_section_ptr.
1607 We treat a common symbol as a definition if the symbol in the
1608 shared library is a function, since common symbols always
1609 represent variables; this can cause confusion in principle, but
1610 any such confusion would seem to indicate an erroneous program or
1611 shared library. We also permit a common symbol in a regular
1612 object to override a weak symbol in a shared object. */
1617 || (h->root.type == bfd_link_hash_common
1618 && (newweak || newfunc))))
1622 newdyncommon = FALSE;
1624 *psec = sec = bfd_und_section_ptr;
1625 *size_change_ok = TRUE;
1627 /* If we get here when the old symbol is a common symbol, then
1628 we are explicitly letting it override a weak symbol or
1629 function in a dynamic object, and we don't want to warn about
1630 a type change. If the old symbol is a defined symbol, a type
1631 change warning may still be appropriate. */
1633 if (h->root.type == bfd_link_hash_common)
1634 *type_change_ok = TRUE;
1637 /* Handle the special case of an old common symbol merging with a
1638 new symbol which looks like a common symbol in a shared object.
1639 We change *PSEC and *PVALUE to make the new symbol look like a
1640 common symbol, and let _bfd_generic_link_add_one_symbol do the
1644 && h->root.type == bfd_link_hash_common)
1648 newdyncommon = FALSE;
1649 *pvalue = sym->st_size;
1650 *psec = sec = bed->common_section (oldsec);
1651 *size_change_ok = TRUE;
1654 /* Skip weak definitions of symbols that are already defined. */
1655 if (newdef && olddef && newweak)
1657 /* Don't skip new non-IR weak syms. */
1658 if (!(oldbfd != NULL
1659 && (oldbfd->flags & BFD_PLUGIN) != 0
1660 && (abfd->flags & BFD_PLUGIN) == 0))
1666 /* Merge st_other. If the symbol already has a dynamic index,
1667 but visibility says it should not be visible, turn it into a
1669 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1670 if (h->dynindx != -1)
1671 switch (ELF_ST_VISIBILITY (h->other))
1675 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1680 /* If the old symbol is from a dynamic object, and the new symbol is
1681 a definition which is not from a dynamic object, then the new
1682 symbol overrides the old symbol. Symbols from regular files
1683 always take precedence over symbols from dynamic objects, even if
1684 they are defined after the dynamic object in the link.
1686 As above, we again permit a common symbol in a regular object to
1687 override a definition in a shared object if the shared object
1688 symbol is a function or is weak. */
1693 || (bfd_is_com_section (sec)
1694 && (oldweak || oldfunc)))
1699 /* Change the hash table entry to undefined, and let
1700 _bfd_generic_link_add_one_symbol do the right thing with the
1703 h->root.type = bfd_link_hash_undefined;
1704 h->root.u.undef.abfd = h->root.u.def.section->owner;
1705 *size_change_ok = TRUE;
1708 olddyncommon = FALSE;
1710 /* We again permit a type change when a common symbol may be
1711 overriding a function. */
1713 if (bfd_is_com_section (sec))
1717 /* If a common symbol overrides a function, make sure
1718 that it isn't defined dynamically nor has type
1721 h->type = STT_NOTYPE;
1723 *type_change_ok = TRUE;
1726 if (hi->root.type == bfd_link_hash_indirect)
1729 /* This union may have been set to be non-NULL when this symbol
1730 was seen in a dynamic object. We must force the union to be
1731 NULL, so that it is correct for a regular symbol. */
1732 h->verinfo.vertree = NULL;
1735 /* Handle the special case of a new common symbol merging with an
1736 old symbol that looks like it might be a common symbol defined in
1737 a shared object. Note that we have already handled the case in
1738 which a new common symbol should simply override the definition
1739 in the shared library. */
1742 && bfd_is_com_section (sec)
1745 /* It would be best if we could set the hash table entry to a
1746 common symbol, but we don't know what to use for the section
1747 or the alignment. */
1748 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1749 bfd_link_hash_common, sym->st_size);
1751 /* If the presumed common symbol in the dynamic object is
1752 larger, pretend that the new symbol has its size. */
1754 if (h->size > *pvalue)
1757 /* We need to remember the alignment required by the symbol
1758 in the dynamic object. */
1759 BFD_ASSERT (pold_alignment);
1760 *pold_alignment = h->root.u.def.section->alignment_power;
1763 olddyncommon = FALSE;
1765 h->root.type = bfd_link_hash_undefined;
1766 h->root.u.undef.abfd = h->root.u.def.section->owner;
1768 *size_change_ok = TRUE;
1769 *type_change_ok = TRUE;
1771 if (hi->root.type == bfd_link_hash_indirect)
1774 h->verinfo.vertree = NULL;
1779 /* Handle the case where we had a versioned symbol in a dynamic
1780 library and now find a definition in a normal object. In this
1781 case, we make the versioned symbol point to the normal one. */
1782 flip->root.type = h->root.type;
1783 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1784 h->root.type = bfd_link_hash_indirect;
1785 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1786 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1790 flip->ref_dynamic = 1;
1797 /* This function is called to create an indirect symbol from the
1798 default for the symbol with the default version if needed. The
1799 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1800 set DYNSYM if the new indirect symbol is dynamic. */
1803 _bfd_elf_add_default_symbol (bfd *abfd,
1804 struct bfd_link_info *info,
1805 struct elf_link_hash_entry *h,
1807 Elf_Internal_Sym *sym,
1811 bfd_boolean *dynsym)
1813 bfd_boolean type_change_ok;
1814 bfd_boolean size_change_ok;
1817 struct elf_link_hash_entry *hi;
1818 struct bfd_link_hash_entry *bh;
1819 const struct elf_backend_data *bed;
1820 bfd_boolean collect;
1821 bfd_boolean dynamic;
1822 bfd_boolean override;
1824 size_t len, shortlen;
1826 bfd_boolean matched;
1828 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1831 /* If this symbol has a version, and it is the default version, we
1832 create an indirect symbol from the default name to the fully
1833 decorated name. This will cause external references which do not
1834 specify a version to be bound to this version of the symbol. */
1835 p = strchr (name, ELF_VER_CHR);
1836 if (h->versioned == unknown)
1840 h->versioned = unversioned;
1845 if (p[1] != ELF_VER_CHR)
1847 h->versioned = versioned_hidden;
1851 h->versioned = versioned;
1856 /* PR ld/19073: We may see an unversioned definition after the
1862 bed = get_elf_backend_data (abfd);
1863 collect = bed->collect;
1864 dynamic = (abfd->flags & DYNAMIC) != 0;
1866 shortlen = p - name;
1867 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1868 if (shortname == NULL)
1870 memcpy (shortname, name, shortlen);
1871 shortname[shortlen] = '\0';
1873 /* We are going to create a new symbol. Merge it with any existing
1874 symbol with this name. For the purposes of the merge, act as
1875 though we were defining the symbol we just defined, although we
1876 actually going to define an indirect symbol. */
1877 type_change_ok = FALSE;
1878 size_change_ok = FALSE;
1881 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1882 &hi, poldbfd, NULL, NULL, &skip, &override,
1883 &type_change_ok, &size_change_ok, &matched))
1889 if (hi->def_regular)
1891 /* If the undecorated symbol will have a version added by a
1892 script different to H, then don't indirect to/from the
1893 undecorated symbol. This isn't ideal because we may not yet
1894 have seen symbol versions, if given by a script on the
1895 command line rather than via --version-script. */
1896 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1901 = bfd_find_version_for_sym (info->version_info,
1902 hi->root.root.string, &hide);
1903 if (hi->verinfo.vertree != NULL && hide)
1905 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1909 if (hi->verinfo.vertree != NULL
1910 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1916 /* Add the default symbol if not performing a relocatable link. */
1917 if (! bfd_link_relocatable (info))
1920 if (! (_bfd_generic_link_add_one_symbol
1921 (info, abfd, shortname, BSF_INDIRECT,
1922 bfd_ind_section_ptr,
1923 0, name, FALSE, collect, &bh)))
1925 hi = (struct elf_link_hash_entry *) bh;
1930 /* In this case the symbol named SHORTNAME is overriding the
1931 indirect symbol we want to add. We were planning on making
1932 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1933 is the name without a version. NAME is the fully versioned
1934 name, and it is the default version.
1936 Overriding means that we already saw a definition for the
1937 symbol SHORTNAME in a regular object, and it is overriding
1938 the symbol defined in the dynamic object.
1940 When this happens, we actually want to change NAME, the
1941 symbol we just added, to refer to SHORTNAME. This will cause
1942 references to NAME in the shared object to become references
1943 to SHORTNAME in the regular object. This is what we expect
1944 when we override a function in a shared object: that the
1945 references in the shared object will be mapped to the
1946 definition in the regular object. */
1948 while (hi->root.type == bfd_link_hash_indirect
1949 || hi->root.type == bfd_link_hash_warning)
1950 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1952 h->root.type = bfd_link_hash_indirect;
1953 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1957 hi->ref_dynamic = 1;
1961 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1966 /* Now set HI to H, so that the following code will set the
1967 other fields correctly. */
1971 /* Check if HI is a warning symbol. */
1972 if (hi->root.type == bfd_link_hash_warning)
1973 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1975 /* If there is a duplicate definition somewhere, then HI may not
1976 point to an indirect symbol. We will have reported an error to
1977 the user in that case. */
1979 if (hi->root.type == bfd_link_hash_indirect)
1981 struct elf_link_hash_entry *ht;
1983 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1984 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1986 /* A reference to the SHORTNAME symbol from a dynamic library
1987 will be satisfied by the versioned symbol at runtime. In
1988 effect, we have a reference to the versioned symbol. */
1989 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1990 hi->dynamic_def |= ht->dynamic_def;
1992 /* See if the new flags lead us to realize that the symbol must
1998 if (! bfd_link_executable (info)
2005 if (hi->ref_regular)
2011 /* We also need to define an indirection from the nondefault version
2015 len = strlen (name);
2016 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2017 if (shortname == NULL)
2019 memcpy (shortname, name, shortlen);
2020 memcpy (shortname + shortlen, p + 1, len - shortlen);
2022 /* Once again, merge with any existing symbol. */
2023 type_change_ok = FALSE;
2024 size_change_ok = FALSE;
2026 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2027 &hi, poldbfd, NULL, NULL, &skip, &override,
2028 &type_change_ok, &size_change_ok, &matched))
2036 /* Here SHORTNAME is a versioned name, so we don't expect to see
2037 the type of override we do in the case above unless it is
2038 overridden by a versioned definition. */
2039 if (hi->root.type != bfd_link_hash_defined
2040 && hi->root.type != bfd_link_hash_defweak)
2042 /* xgettext:c-format */
2043 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2049 if (! (_bfd_generic_link_add_one_symbol
2050 (info, abfd, shortname, BSF_INDIRECT,
2051 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2053 hi = (struct elf_link_hash_entry *) bh;
2055 /* If there is a duplicate definition somewhere, then HI may not
2056 point to an indirect symbol. We will have reported an error
2057 to the user in that case. */
2059 if (hi->root.type == bfd_link_hash_indirect)
2061 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2062 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2063 hi->dynamic_def |= h->dynamic_def;
2065 /* See if the new flags lead us to realize that the symbol
2071 if (! bfd_link_executable (info)
2077 if (hi->ref_regular)
2087 /* This routine is used to export all defined symbols into the dynamic
2088 symbol table. It is called via elf_link_hash_traverse. */
2091 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2093 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2095 /* Ignore indirect symbols. These are added by the versioning code. */
2096 if (h->root.type == bfd_link_hash_indirect)
2099 /* Ignore this if we won't export it. */
2100 if (!eif->info->export_dynamic && !h->dynamic)
2103 if (h->dynindx == -1
2104 && (h->def_regular || h->ref_regular)
2105 && ! bfd_hide_sym_by_version (eif->info->version_info,
2106 h->root.root.string))
2108 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2118 /* Look through the symbols which are defined in other shared
2119 libraries and referenced here. Update the list of version
2120 dependencies. This will be put into the .gnu.version_r section.
2121 This function is called via elf_link_hash_traverse. */
2124 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2127 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2128 Elf_Internal_Verneed *t;
2129 Elf_Internal_Vernaux *a;
2132 /* We only care about symbols defined in shared objects with version
2137 || h->verinfo.verdef == NULL
2138 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2139 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2142 /* See if we already know about this version. */
2143 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2147 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2150 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2151 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2157 /* This is a new version. Add it to tree we are building. */
2162 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2165 rinfo->failed = TRUE;
2169 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2170 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2171 elf_tdata (rinfo->info->output_bfd)->verref = t;
2175 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2178 rinfo->failed = TRUE;
2182 /* Note that we are copying a string pointer here, and testing it
2183 above. If bfd_elf_string_from_elf_section is ever changed to
2184 discard the string data when low in memory, this will have to be
2186 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2188 a->vna_flags = h->verinfo.verdef->vd_flags;
2189 a->vna_nextptr = t->vn_auxptr;
2191 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2194 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2201 /* Figure out appropriate versions for all the symbols. We may not
2202 have the version number script until we have read all of the input
2203 files, so until that point we don't know which symbols should be
2204 local. This function is called via elf_link_hash_traverse. */
2207 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2209 struct elf_info_failed *sinfo;
2210 struct bfd_link_info *info;
2211 const struct elf_backend_data *bed;
2212 struct elf_info_failed eif;
2215 sinfo = (struct elf_info_failed *) data;
2218 /* Fix the symbol flags. */
2221 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2224 sinfo->failed = TRUE;
2228 /* We only need version numbers for symbols defined in regular
2230 if (!h->def_regular)
2233 bed = get_elf_backend_data (info->output_bfd);
2234 p = strchr (h->root.root.string, ELF_VER_CHR);
2235 if (p != NULL && h->verinfo.vertree == NULL)
2237 struct bfd_elf_version_tree *t;
2240 if (*p == ELF_VER_CHR)
2243 /* If there is no version string, we can just return out. */
2247 /* Look for the version. If we find it, it is no longer weak. */
2248 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2250 if (strcmp (t->name, p) == 0)
2254 struct bfd_elf_version_expr *d;
2256 len = p - h->root.root.string;
2257 alc = (char *) bfd_malloc (len);
2260 sinfo->failed = TRUE;
2263 memcpy (alc, h->root.root.string, len - 1);
2264 alc[len - 1] = '\0';
2265 if (alc[len - 2] == ELF_VER_CHR)
2266 alc[len - 2] = '\0';
2268 h->verinfo.vertree = t;
2272 if (t->globals.list != NULL)
2273 d = (*t->match) (&t->globals, NULL, alc);
2275 /* See if there is anything to force this symbol to
2277 if (d == NULL && t->locals.list != NULL)
2279 d = (*t->match) (&t->locals, NULL, alc);
2282 && ! info->export_dynamic)
2283 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2291 /* If we are building an application, we need to create a
2292 version node for this version. */
2293 if (t == NULL && bfd_link_executable (info))
2295 struct bfd_elf_version_tree **pp;
2298 /* If we aren't going to export this symbol, we don't need
2299 to worry about it. */
2300 if (h->dynindx == -1)
2303 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2307 sinfo->failed = TRUE;
2312 t->name_indx = (unsigned int) -1;
2316 /* Don't count anonymous version tag. */
2317 if (sinfo->info->version_info != NULL
2318 && sinfo->info->version_info->vernum == 0)
2320 for (pp = &sinfo->info->version_info;
2324 t->vernum = version_index;
2328 h->verinfo.vertree = t;
2332 /* We could not find the version for a symbol when
2333 generating a shared archive. Return an error. */
2335 /* xgettext:c-format */
2336 (_("%B: version node not found for symbol %s"),
2337 info->output_bfd, h->root.root.string);
2338 bfd_set_error (bfd_error_bad_value);
2339 sinfo->failed = TRUE;
2344 /* If we don't have a version for this symbol, see if we can find
2346 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2351 = bfd_find_version_for_sym (sinfo->info->version_info,
2352 h->root.root.string, &hide);
2353 if (h->verinfo.vertree != NULL && hide)
2354 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2360 /* Read and swap the relocs from the section indicated by SHDR. This
2361 may be either a REL or a RELA section. The relocations are
2362 translated into RELA relocations and stored in INTERNAL_RELOCS,
2363 which should have already been allocated to contain enough space.
2364 The EXTERNAL_RELOCS are a buffer where the external form of the
2365 relocations should be stored.
2367 Returns FALSE if something goes wrong. */
2370 elf_link_read_relocs_from_section (bfd *abfd,
2372 Elf_Internal_Shdr *shdr,
2373 void *external_relocs,
2374 Elf_Internal_Rela *internal_relocs)
2376 const struct elf_backend_data *bed;
2377 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2378 const bfd_byte *erela;
2379 const bfd_byte *erelaend;
2380 Elf_Internal_Rela *irela;
2381 Elf_Internal_Shdr *symtab_hdr;
2384 /* Position ourselves at the start of the section. */
2385 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2388 /* Read the relocations. */
2389 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2392 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2393 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2395 bed = get_elf_backend_data (abfd);
2397 /* Convert the external relocations to the internal format. */
2398 if (shdr->sh_entsize == bed->s->sizeof_rel)
2399 swap_in = bed->s->swap_reloc_in;
2400 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2401 swap_in = bed->s->swap_reloca_in;
2404 bfd_set_error (bfd_error_wrong_format);
2408 erela = (const bfd_byte *) external_relocs;
2409 erelaend = erela + shdr->sh_size;
2410 irela = internal_relocs;
2411 while (erela < erelaend)
2415 (*swap_in) (abfd, erela, irela);
2416 r_symndx = ELF32_R_SYM (irela->r_info);
2417 if (bed->s->arch_size == 64)
2421 if ((size_t) r_symndx >= nsyms)
2424 /* xgettext:c-format */
2425 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2426 " for offset %#Lx in section `%A'"),
2427 abfd, r_symndx, (unsigned long) nsyms,
2428 irela->r_offset, sec);
2429 bfd_set_error (bfd_error_bad_value);
2433 else if (r_symndx != STN_UNDEF)
2436 /* xgettext:c-format */
2437 (_("%B: non-zero symbol index (%#Lx)"
2438 " for offset %#Lx in section `%A'"
2439 " when the object file has no symbol table"),
2441 irela->r_offset, sec);
2442 bfd_set_error (bfd_error_bad_value);
2445 irela += bed->s->int_rels_per_ext_rel;
2446 erela += shdr->sh_entsize;
2452 /* Read and swap the relocs for a section O. They may have been
2453 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2454 not NULL, they are used as buffers to read into. They are known to
2455 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2456 the return value is allocated using either malloc or bfd_alloc,
2457 according to the KEEP_MEMORY argument. If O has two relocation
2458 sections (both REL and RELA relocations), then the REL_HDR
2459 relocations will appear first in INTERNAL_RELOCS, followed by the
2460 RELA_HDR relocations. */
2463 _bfd_elf_link_read_relocs (bfd *abfd,
2465 void *external_relocs,
2466 Elf_Internal_Rela *internal_relocs,
2467 bfd_boolean keep_memory)
2469 void *alloc1 = NULL;
2470 Elf_Internal_Rela *alloc2 = NULL;
2471 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2472 struct bfd_elf_section_data *esdo = elf_section_data (o);
2473 Elf_Internal_Rela *internal_rela_relocs;
2475 if (esdo->relocs != NULL)
2476 return esdo->relocs;
2478 if (o->reloc_count == 0)
2481 if (internal_relocs == NULL)
2485 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2487 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2489 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2490 if (internal_relocs == NULL)
2494 if (external_relocs == NULL)
2496 bfd_size_type size = 0;
2499 size += esdo->rel.hdr->sh_size;
2501 size += esdo->rela.hdr->sh_size;
2503 alloc1 = bfd_malloc (size);
2506 external_relocs = alloc1;
2509 internal_rela_relocs = internal_relocs;
2512 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2516 external_relocs = (((bfd_byte *) external_relocs)
2517 + esdo->rel.hdr->sh_size);
2518 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2519 * bed->s->int_rels_per_ext_rel);
2523 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2525 internal_rela_relocs)))
2528 /* Cache the results for next time, if we can. */
2530 esdo->relocs = internal_relocs;
2535 /* Don't free alloc2, since if it was allocated we are passing it
2536 back (under the name of internal_relocs). */
2538 return internal_relocs;
2546 bfd_release (abfd, alloc2);
2553 /* Compute the size of, and allocate space for, REL_HDR which is the
2554 section header for a section containing relocations for O. */
2557 _bfd_elf_link_size_reloc_section (bfd *abfd,
2558 struct bfd_elf_section_reloc_data *reldata)
2560 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2562 /* That allows us to calculate the size of the section. */
2563 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2565 /* The contents field must last into write_object_contents, so we
2566 allocate it with bfd_alloc rather than malloc. Also since we
2567 cannot be sure that the contents will actually be filled in,
2568 we zero the allocated space. */
2569 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2570 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2573 if (reldata->hashes == NULL && reldata->count)
2575 struct elf_link_hash_entry **p;
2577 p = ((struct elf_link_hash_entry **)
2578 bfd_zmalloc (reldata->count * sizeof (*p)));
2582 reldata->hashes = p;
2588 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2589 originated from the section given by INPUT_REL_HDR) to the
2593 _bfd_elf_link_output_relocs (bfd *output_bfd,
2594 asection *input_section,
2595 Elf_Internal_Shdr *input_rel_hdr,
2596 Elf_Internal_Rela *internal_relocs,
2597 struct elf_link_hash_entry **rel_hash
2600 Elf_Internal_Rela *irela;
2601 Elf_Internal_Rela *irelaend;
2603 struct bfd_elf_section_reloc_data *output_reldata;
2604 asection *output_section;
2605 const struct elf_backend_data *bed;
2606 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2607 struct bfd_elf_section_data *esdo;
2609 output_section = input_section->output_section;
2611 bed = get_elf_backend_data (output_bfd);
2612 esdo = elf_section_data (output_section);
2613 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2615 output_reldata = &esdo->rel;
2616 swap_out = bed->s->swap_reloc_out;
2618 else if (esdo->rela.hdr
2619 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2621 output_reldata = &esdo->rela;
2622 swap_out = bed->s->swap_reloca_out;
2627 /* xgettext:c-format */
2628 (_("%B: relocation size mismatch in %B section %A"),
2629 output_bfd, input_section->owner, input_section);
2630 bfd_set_error (bfd_error_wrong_format);
2634 erel = output_reldata->hdr->contents;
2635 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2636 irela = internal_relocs;
2637 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2638 * bed->s->int_rels_per_ext_rel);
2639 while (irela < irelaend)
2641 (*swap_out) (output_bfd, irela, erel);
2642 irela += bed->s->int_rels_per_ext_rel;
2643 erel += input_rel_hdr->sh_entsize;
2646 /* Bump the counter, so that we know where to add the next set of
2648 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2653 /* Make weak undefined symbols in PIE dynamic. */
2656 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2657 struct elf_link_hash_entry *h)
2659 if (bfd_link_pie (info)
2661 && h->root.type == bfd_link_hash_undefweak)
2662 return bfd_elf_link_record_dynamic_symbol (info, h);
2667 /* Fix up the flags for a symbol. This handles various cases which
2668 can only be fixed after all the input files are seen. This is
2669 currently called by both adjust_dynamic_symbol and
2670 assign_sym_version, which is unnecessary but perhaps more robust in
2671 the face of future changes. */
2674 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2675 struct elf_info_failed *eif)
2677 const struct elf_backend_data *bed;
2679 /* If this symbol was mentioned in a non-ELF file, try to set
2680 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2681 permit a non-ELF file to correctly refer to a symbol defined in
2682 an ELF dynamic object. */
2685 while (h->root.type == bfd_link_hash_indirect)
2686 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2688 if (h->root.type != bfd_link_hash_defined
2689 && h->root.type != bfd_link_hash_defweak)
2692 h->ref_regular_nonweak = 1;
2696 if (h->root.u.def.section->owner != NULL
2697 && (bfd_get_flavour (h->root.u.def.section->owner)
2698 == bfd_target_elf_flavour))
2701 h->ref_regular_nonweak = 1;
2707 if (h->dynindx == -1
2711 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2720 /* Unfortunately, NON_ELF is only correct if the symbol
2721 was first seen in a non-ELF file. Fortunately, if the symbol
2722 was first seen in an ELF file, we're probably OK unless the
2723 symbol was defined in a non-ELF file. Catch that case here.
2724 FIXME: We're still in trouble if the symbol was first seen in
2725 a dynamic object, and then later in a non-ELF regular object. */
2726 if ((h->root.type == bfd_link_hash_defined
2727 || h->root.type == bfd_link_hash_defweak)
2729 && (h->root.u.def.section->owner != NULL
2730 ? (bfd_get_flavour (h->root.u.def.section->owner)
2731 != bfd_target_elf_flavour)
2732 : (bfd_is_abs_section (h->root.u.def.section)
2733 && !h->def_dynamic)))
2737 /* Backend specific symbol fixup. */
2738 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2739 if (bed->elf_backend_fixup_symbol
2740 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2743 /* If this is a final link, and the symbol was defined as a common
2744 symbol in a regular object file, and there was no definition in
2745 any dynamic object, then the linker will have allocated space for
2746 the symbol in a common section but the DEF_REGULAR
2747 flag will not have been set. */
2748 if (h->root.type == bfd_link_hash_defined
2752 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2755 /* If a weak undefined symbol has non-default visibility, we also
2756 hide it from the dynamic linker. */
2757 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2758 && h->root.type == bfd_link_hash_undefweak)
2759 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2761 /* A hidden versioned symbol in executable should be forced local if
2762 it is is locally defined, not referenced by shared library and not
2764 else if (bfd_link_executable (eif->info)
2765 && h->versioned == versioned_hidden
2766 && !eif->info->export_dynamic
2770 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2772 /* If -Bsymbolic was used (which means to bind references to global
2773 symbols to the definition within the shared object), and this
2774 symbol was defined in a regular object, then it actually doesn't
2775 need a PLT entry. Likewise, if the symbol has non-default
2776 visibility. If the symbol has hidden or internal visibility, we
2777 will force it local. */
2778 else if (h->needs_plt
2779 && bfd_link_pic (eif->info)
2780 && is_elf_hash_table (eif->info->hash)
2781 && (SYMBOLIC_BIND (eif->info, h)
2782 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2785 bfd_boolean force_local;
2787 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2788 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2789 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2792 /* If this is a weak defined symbol in a dynamic object, and we know
2793 the real definition in the dynamic object, copy interesting flags
2794 over to the real definition. */
2795 if (h->is_weakalias)
2797 struct elf_link_hash_entry *def = weakdef (h);
2799 /* If the real definition is defined by a regular object file,
2800 don't do anything special. See the longer description in
2801 _bfd_elf_adjust_dynamic_symbol, below. */
2802 if (def->def_regular)
2805 while ((h = h->u.alias) != def)
2806 h->is_weakalias = 0;
2810 while (h->root.type == bfd_link_hash_indirect)
2811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2812 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2813 || h->root.type == bfd_link_hash_defweak);
2814 BFD_ASSERT (def->def_dynamic);
2815 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2816 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2823 /* Make the backend pick a good value for a dynamic symbol. This is
2824 called via elf_link_hash_traverse, and also calls itself
2828 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2830 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2831 struct elf_link_hash_table *htab;
2832 const struct elf_backend_data *bed;
2834 if (! is_elf_hash_table (eif->info->hash))
2837 /* Ignore indirect symbols. These are added by the versioning code. */
2838 if (h->root.type == bfd_link_hash_indirect)
2841 /* Fix the symbol flags. */
2842 if (! _bfd_elf_fix_symbol_flags (h, eif))
2845 htab = elf_hash_table (eif->info);
2846 bed = get_elf_backend_data (htab->dynobj);
2848 if (h->root.type == bfd_link_hash_undefweak)
2850 if (eif->info->dynamic_undefined_weak == 0)
2851 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2852 else if (eif->info->dynamic_undefined_weak > 0
2854 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2855 && !bfd_hide_sym_by_version (eif->info->version_info,
2856 h->root.root.string))
2858 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2866 /* If this symbol does not require a PLT entry, and it is not
2867 defined by a dynamic object, or is not referenced by a regular
2868 object, ignore it. We do have to handle a weak defined symbol,
2869 even if no regular object refers to it, if we decided to add it
2870 to the dynamic symbol table. FIXME: Do we normally need to worry
2871 about symbols which are defined by one dynamic object and
2872 referenced by another one? */
2874 && h->type != STT_GNU_IFUNC
2878 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2880 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2884 /* If we've already adjusted this symbol, don't do it again. This
2885 can happen via a recursive call. */
2886 if (h->dynamic_adjusted)
2889 /* Don't look at this symbol again. Note that we must set this
2890 after checking the above conditions, because we may look at a
2891 symbol once, decide not to do anything, and then get called
2892 recursively later after REF_REGULAR is set below. */
2893 h->dynamic_adjusted = 1;
2895 /* If this is a weak definition, and we know a real definition, and
2896 the real symbol is not itself defined by a regular object file,
2897 then get a good value for the real definition. We handle the
2898 real symbol first, for the convenience of the backend routine.
2900 Note that there is a confusing case here. If the real definition
2901 is defined by a regular object file, we don't get the real symbol
2902 from the dynamic object, but we do get the weak symbol. If the
2903 processor backend uses a COPY reloc, then if some routine in the
2904 dynamic object changes the real symbol, we will not see that
2905 change in the corresponding weak symbol. This is the way other
2906 ELF linkers work as well, and seems to be a result of the shared
2909 I will clarify this issue. Most SVR4 shared libraries define the
2910 variable _timezone and define timezone as a weak synonym. The
2911 tzset call changes _timezone. If you write
2912 extern int timezone;
2914 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2915 you might expect that, since timezone is a synonym for _timezone,
2916 the same number will print both times. However, if the processor
2917 backend uses a COPY reloc, then actually timezone will be copied
2918 into your process image, and, since you define _timezone
2919 yourself, _timezone will not. Thus timezone and _timezone will
2920 wind up at different memory locations. The tzset call will set
2921 _timezone, leaving timezone unchanged. */
2923 if (h->is_weakalias)
2925 struct elf_link_hash_entry *def = weakdef (h);
2927 /* If we get to this point, there is an implicit reference to
2928 the alias by a regular object file via the weak symbol H. */
2929 def->ref_regular = 1;
2931 /* Ensure that the backend adjust_dynamic_symbol function sees
2932 the strong alias before H by recursively calling ourselves. */
2933 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
2937 /* If a symbol has no type and no size and does not require a PLT
2938 entry, then we are probably about to do the wrong thing here: we
2939 are probably going to create a COPY reloc for an empty object.
2940 This case can arise when a shared object is built with assembly
2941 code, and the assembly code fails to set the symbol type. */
2943 && h->type == STT_NOTYPE
2946 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2947 h->root.root.string);
2949 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2958 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2962 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2963 struct elf_link_hash_entry *h,
2966 unsigned int power_of_two;
2968 asection *sec = h->root.u.def.section;
2970 /* The section alignment of the definition is the maximum alignment
2971 requirement of symbols defined in the section. Since we don't
2972 know the symbol alignment requirement, we start with the
2973 maximum alignment and check low bits of the symbol address
2974 for the minimum alignment. */
2975 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2976 mask = ((bfd_vma) 1 << power_of_two) - 1;
2977 while ((h->root.u.def.value & mask) != 0)
2983 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2986 /* Adjust the section alignment if needed. */
2987 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2992 /* We make sure that the symbol will be aligned properly. */
2993 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2995 /* Define the symbol as being at this point in DYNBSS. */
2996 h->root.u.def.section = dynbss;
2997 h->root.u.def.value = dynbss->size;
2999 /* Increment the size of DYNBSS to make room for the symbol. */
3000 dynbss->size += h->size;
3002 /* No error if extern_protected_data is true. */
3003 if (h->protected_def
3004 && (!info->extern_protected_data
3005 || (info->extern_protected_data < 0
3006 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3007 info->callbacks->einfo
3008 (_("%P: copy reloc against protected `%T' is dangerous\n"),
3009 h->root.root.string);
3014 /* Adjust all external symbols pointing into SEC_MERGE sections
3015 to reflect the object merging within the sections. */
3018 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3022 if ((h->root.type == bfd_link_hash_defined
3023 || h->root.type == bfd_link_hash_defweak)
3024 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3025 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3027 bfd *output_bfd = (bfd *) data;
3029 h->root.u.def.value =
3030 _bfd_merged_section_offset (output_bfd,
3031 &h->root.u.def.section,
3032 elf_section_data (sec)->sec_info,
3033 h->root.u.def.value);
3039 /* Returns false if the symbol referred to by H should be considered
3040 to resolve local to the current module, and true if it should be
3041 considered to bind dynamically. */
3044 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3045 struct bfd_link_info *info,
3046 bfd_boolean not_local_protected)
3048 bfd_boolean binding_stays_local_p;
3049 const struct elf_backend_data *bed;
3050 struct elf_link_hash_table *hash_table;
3055 while (h->root.type == bfd_link_hash_indirect
3056 || h->root.type == bfd_link_hash_warning)
3057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3059 /* If it was forced local, then clearly it's not dynamic. */
3060 if (h->dynindx == -1)
3062 if (h->forced_local)
3065 /* Identify the cases where name binding rules say that a
3066 visible symbol resolves locally. */
3067 binding_stays_local_p = (bfd_link_executable (info)
3068 || SYMBOLIC_BIND (info, h));
3070 switch (ELF_ST_VISIBILITY (h->other))
3077 hash_table = elf_hash_table (info);
3078 if (!is_elf_hash_table (hash_table))
3081 bed = get_elf_backend_data (hash_table->dynobj);
3083 /* Proper resolution for function pointer equality may require
3084 that these symbols perhaps be resolved dynamically, even though
3085 we should be resolving them to the current module. */
3086 if (!not_local_protected || !bed->is_function_type (h->type))
3087 binding_stays_local_p = TRUE;
3094 /* If it isn't defined locally, then clearly it's dynamic. */
3095 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3098 /* Otherwise, the symbol is dynamic if binding rules don't tell
3099 us that it remains local. */
3100 return !binding_stays_local_p;
3103 /* Return true if the symbol referred to by H should be considered
3104 to resolve local to the current module, and false otherwise. Differs
3105 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3106 undefined symbols. The two functions are virtually identical except
3107 for the place where dynindx == -1 is tested. If that test is true,
3108 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3109 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3111 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3112 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3113 treatment of undefined weak symbols. For those that do not make
3114 undefined weak symbols dynamic, both functions may return false. */
3117 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3118 struct bfd_link_info *info,
3119 bfd_boolean local_protected)
3121 const struct elf_backend_data *bed;
3122 struct elf_link_hash_table *hash_table;
3124 /* If it's a local sym, of course we resolve locally. */
3128 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3129 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3130 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3133 /* Forced local symbols resolve locally. */
3134 if (h->forced_local)
3137 /* Common symbols that become definitions don't get the DEF_REGULAR
3138 flag set, so test it first, and don't bail out. */
3139 if (ELF_COMMON_DEF_P (h))
3141 /* If we don't have a definition in a regular file, then we can't
3142 resolve locally. The sym is either undefined or dynamic. */
3143 else if (!h->def_regular)
3146 /* Non-dynamic symbols resolve locally. */
3147 if (h->dynindx == -1)
3150 /* At this point, we know the symbol is defined and dynamic. In an
3151 executable it must resolve locally, likewise when building symbolic
3152 shared libraries. */
3153 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3156 /* Now deal with defined dynamic symbols in shared libraries. Ones
3157 with default visibility might not resolve locally. */
3158 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3161 hash_table = elf_hash_table (info);
3162 if (!is_elf_hash_table (hash_table))
3165 bed = get_elf_backend_data (hash_table->dynobj);
3167 /* If extern_protected_data is false, STV_PROTECTED non-function
3168 symbols are local. */
3169 if ((!info->extern_protected_data
3170 || (info->extern_protected_data < 0
3171 && !bed->extern_protected_data))
3172 && !bed->is_function_type (h->type))
3175 /* Function pointer equality tests may require that STV_PROTECTED
3176 symbols be treated as dynamic symbols. If the address of a
3177 function not defined in an executable is set to that function's
3178 plt entry in the executable, then the address of the function in
3179 a shared library must also be the plt entry in the executable. */
3180 return local_protected;
3183 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3184 aligned. Returns the first TLS output section. */
3186 struct bfd_section *
3187 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3189 struct bfd_section *sec, *tls;
3190 unsigned int align = 0;
3192 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3193 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3197 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3198 if (sec->alignment_power > align)
3199 align = sec->alignment_power;
3201 elf_hash_table (info)->tls_sec = tls;
3203 /* Ensure the alignment of the first section is the largest alignment,
3204 so that the tls segment starts aligned. */
3206 tls->alignment_power = align;
3211 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3213 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3214 Elf_Internal_Sym *sym)
3216 const struct elf_backend_data *bed;
3218 /* Local symbols do not count, but target specific ones might. */
3219 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3220 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3223 bed = get_elf_backend_data (abfd);
3224 /* Function symbols do not count. */
3225 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3228 /* If the section is undefined, then so is the symbol. */
3229 if (sym->st_shndx == SHN_UNDEF)
3232 /* If the symbol is defined in the common section, then
3233 it is a common definition and so does not count. */
3234 if (bed->common_definition (sym))
3237 /* If the symbol is in a target specific section then we
3238 must rely upon the backend to tell us what it is. */
3239 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3240 /* FIXME - this function is not coded yet:
3242 return _bfd_is_global_symbol_definition (abfd, sym);
3244 Instead for now assume that the definition is not global,
3245 Even if this is wrong, at least the linker will behave
3246 in the same way that it used to do. */
3252 /* Search the symbol table of the archive element of the archive ABFD
3253 whose archive map contains a mention of SYMDEF, and determine if
3254 the symbol is defined in this element. */
3256 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3258 Elf_Internal_Shdr * hdr;
3262 Elf_Internal_Sym *isymbuf;
3263 Elf_Internal_Sym *isym;
3264 Elf_Internal_Sym *isymend;
3267 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3271 if (! bfd_check_format (abfd, bfd_object))
3274 /* Select the appropriate symbol table. If we don't know if the
3275 object file is an IR object, give linker LTO plugin a chance to
3276 get the correct symbol table. */
3277 if (abfd->plugin_format == bfd_plugin_yes
3278 #if BFD_SUPPORTS_PLUGINS
3279 || (abfd->plugin_format == bfd_plugin_unknown
3280 && bfd_link_plugin_object_p (abfd))
3284 /* Use the IR symbol table if the object has been claimed by
3286 abfd = abfd->plugin_dummy_bfd;
3287 hdr = &elf_tdata (abfd)->symtab_hdr;
3289 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3290 hdr = &elf_tdata (abfd)->symtab_hdr;
3292 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3294 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3296 /* The sh_info field of the symtab header tells us where the
3297 external symbols start. We don't care about the local symbols. */
3298 if (elf_bad_symtab (abfd))
3300 extsymcount = symcount;
3305 extsymcount = symcount - hdr->sh_info;
3306 extsymoff = hdr->sh_info;
3309 if (extsymcount == 0)
3312 /* Read in the symbol table. */
3313 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3315 if (isymbuf == NULL)
3318 /* Scan the symbol table looking for SYMDEF. */
3320 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3324 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3329 if (strcmp (name, symdef->name) == 0)
3331 result = is_global_data_symbol_definition (abfd, isym);
3341 /* Add an entry to the .dynamic table. */
3344 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3348 struct elf_link_hash_table *hash_table;
3349 const struct elf_backend_data *bed;
3351 bfd_size_type newsize;
3352 bfd_byte *newcontents;
3353 Elf_Internal_Dyn dyn;
3355 hash_table = elf_hash_table (info);
3356 if (! is_elf_hash_table (hash_table))
3359 bed = get_elf_backend_data (hash_table->dynobj);
3360 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3361 BFD_ASSERT (s != NULL);
3363 newsize = s->size + bed->s->sizeof_dyn;
3364 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3365 if (newcontents == NULL)
3369 dyn.d_un.d_val = val;
3370 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3373 s->contents = newcontents;
3378 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3379 otherwise just check whether one already exists. Returns -1 on error,
3380 1 if a DT_NEEDED tag already exists, and 0 on success. */
3383 elf_add_dt_needed_tag (bfd *abfd,
3384 struct bfd_link_info *info,
3388 struct elf_link_hash_table *hash_table;
3391 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3394 hash_table = elf_hash_table (info);
3395 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3396 if (strindex == (size_t) -1)
3399 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3402 const struct elf_backend_data *bed;
3405 bed = get_elf_backend_data (hash_table->dynobj);
3406 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3408 for (extdyn = sdyn->contents;
3409 extdyn < sdyn->contents + sdyn->size;
3410 extdyn += bed->s->sizeof_dyn)
3412 Elf_Internal_Dyn dyn;
3414 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3415 if (dyn.d_tag == DT_NEEDED
3416 && dyn.d_un.d_val == strindex)
3418 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3426 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3429 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3433 /* We were just checking for existence of the tag. */
3434 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3439 /* Return true if SONAME is on the needed list between NEEDED and STOP
3440 (or the end of list if STOP is NULL), and needed by a library that
3444 on_needed_list (const char *soname,
3445 struct bfd_link_needed_list *needed,
3446 struct bfd_link_needed_list *stop)
3448 struct bfd_link_needed_list *look;
3449 for (look = needed; look != stop; look = look->next)
3450 if (strcmp (soname, look->name) == 0
3451 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3452 /* If needed by a library that itself is not directly
3453 needed, recursively check whether that library is
3454 indirectly needed. Since we add DT_NEEDED entries to
3455 the end of the list, library dependencies appear after
3456 the library. Therefore search prior to the current
3457 LOOK, preventing possible infinite recursion. */
3458 || on_needed_list (elf_dt_name (look->by), needed, look)))
3464 /* Sort symbol by value, section, and size. */
3466 elf_sort_symbol (const void *arg1, const void *arg2)
3468 const struct elf_link_hash_entry *h1;
3469 const struct elf_link_hash_entry *h2;
3470 bfd_signed_vma vdiff;
3472 h1 = *(const struct elf_link_hash_entry **) arg1;
3473 h2 = *(const struct elf_link_hash_entry **) arg2;
3474 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3476 return vdiff > 0 ? 1 : -1;
3479 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3481 return sdiff > 0 ? 1 : -1;
3483 vdiff = h1->size - h2->size;
3484 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3487 /* This function is used to adjust offsets into .dynstr for
3488 dynamic symbols. This is called via elf_link_hash_traverse. */
3491 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3493 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3495 if (h->dynindx != -1)
3496 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3500 /* Assign string offsets in .dynstr, update all structures referencing
3504 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3506 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3507 struct elf_link_local_dynamic_entry *entry;
3508 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3509 bfd *dynobj = hash_table->dynobj;
3512 const struct elf_backend_data *bed;
3515 _bfd_elf_strtab_finalize (dynstr);
3516 size = _bfd_elf_strtab_size (dynstr);
3518 bed = get_elf_backend_data (dynobj);
3519 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3520 BFD_ASSERT (sdyn != NULL);
3522 /* Update all .dynamic entries referencing .dynstr strings. */
3523 for (extdyn = sdyn->contents;
3524 extdyn < sdyn->contents + sdyn->size;
3525 extdyn += bed->s->sizeof_dyn)
3527 Elf_Internal_Dyn dyn;
3529 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3533 dyn.d_un.d_val = size;
3543 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3548 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3551 /* Now update local dynamic symbols. */
3552 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3553 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3554 entry->isym.st_name);
3556 /* And the rest of dynamic symbols. */
3557 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3559 /* Adjust version definitions. */
3560 if (elf_tdata (output_bfd)->cverdefs)
3565 Elf_Internal_Verdef def;
3566 Elf_Internal_Verdaux defaux;
3568 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3572 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3574 p += sizeof (Elf_External_Verdef);
3575 if (def.vd_aux != sizeof (Elf_External_Verdef))
3577 for (i = 0; i < def.vd_cnt; ++i)
3579 _bfd_elf_swap_verdaux_in (output_bfd,
3580 (Elf_External_Verdaux *) p, &defaux);
3581 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3583 _bfd_elf_swap_verdaux_out (output_bfd,
3584 &defaux, (Elf_External_Verdaux *) p);
3585 p += sizeof (Elf_External_Verdaux);
3588 while (def.vd_next);
3591 /* Adjust version references. */
3592 if (elf_tdata (output_bfd)->verref)
3597 Elf_Internal_Verneed need;
3598 Elf_Internal_Vernaux needaux;
3600 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3604 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3606 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3607 _bfd_elf_swap_verneed_out (output_bfd, &need,
3608 (Elf_External_Verneed *) p);
3609 p += sizeof (Elf_External_Verneed);
3610 for (i = 0; i < need.vn_cnt; ++i)
3612 _bfd_elf_swap_vernaux_in (output_bfd,
3613 (Elf_External_Vernaux *) p, &needaux);
3614 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3616 _bfd_elf_swap_vernaux_out (output_bfd,
3618 (Elf_External_Vernaux *) p);
3619 p += sizeof (Elf_External_Vernaux);
3622 while (need.vn_next);
3628 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3629 The default is to only match when the INPUT and OUTPUT are exactly
3633 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3634 const bfd_target *output)
3636 return input == output;
3639 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3640 This version is used when different targets for the same architecture
3641 are virtually identical. */
3644 _bfd_elf_relocs_compatible (const bfd_target *input,
3645 const bfd_target *output)
3647 const struct elf_backend_data *obed, *ibed;
3649 if (input == output)
3652 ibed = xvec_get_elf_backend_data (input);
3653 obed = xvec_get_elf_backend_data (output);
3655 if (ibed->arch != obed->arch)
3658 /* If both backends are using this function, deem them compatible. */
3659 return ibed->relocs_compatible == obed->relocs_compatible;
3662 /* Make a special call to the linker "notice" function to tell it that
3663 we are about to handle an as-needed lib, or have finished
3664 processing the lib. */
3667 _bfd_elf_notice_as_needed (bfd *ibfd,
3668 struct bfd_link_info *info,
3669 enum notice_asneeded_action act)
3671 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3674 /* Check relocations an ELF object file. */
3677 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3679 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3680 struct elf_link_hash_table *htab = elf_hash_table (info);
3682 /* If this object is the same format as the output object, and it is
3683 not a shared library, then let the backend look through the
3686 This is required to build global offset table entries and to
3687 arrange for dynamic relocs. It is not required for the
3688 particular common case of linking non PIC code, even when linking
3689 against shared libraries, but unfortunately there is no way of
3690 knowing whether an object file has been compiled PIC or not.
3691 Looking through the relocs is not particularly time consuming.
3692 The problem is that we must either (1) keep the relocs in memory,
3693 which causes the linker to require additional runtime memory or
3694 (2) read the relocs twice from the input file, which wastes time.
3695 This would be a good case for using mmap.
3697 I have no idea how to handle linking PIC code into a file of a
3698 different format. It probably can't be done. */
3699 if ((abfd->flags & DYNAMIC) == 0
3700 && is_elf_hash_table (htab)
3701 && bed->check_relocs != NULL
3702 && elf_object_id (abfd) == elf_hash_table_id (htab)
3703 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3707 for (o = abfd->sections; o != NULL; o = o->next)
3709 Elf_Internal_Rela *internal_relocs;
3712 /* Don't check relocations in excluded sections. */
3713 if ((o->flags & SEC_RELOC) == 0
3714 || (o->flags & SEC_EXCLUDE) != 0
3715 || o->reloc_count == 0
3716 || ((info->strip == strip_all || info->strip == strip_debugger)
3717 && (o->flags & SEC_DEBUGGING) != 0)
3718 || bfd_is_abs_section (o->output_section))
3721 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3723 if (internal_relocs == NULL)
3726 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3728 if (elf_section_data (o)->relocs != internal_relocs)
3729 free (internal_relocs);
3739 /* Add symbols from an ELF object file to the linker hash table. */
3742 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3744 Elf_Internal_Ehdr *ehdr;
3745 Elf_Internal_Shdr *hdr;
3749 struct elf_link_hash_entry **sym_hash;
3750 bfd_boolean dynamic;
3751 Elf_External_Versym *extversym = NULL;
3752 Elf_External_Versym *ever;
3753 struct elf_link_hash_entry *weaks;
3754 struct elf_link_hash_entry **nondeflt_vers = NULL;
3755 size_t nondeflt_vers_cnt = 0;
3756 Elf_Internal_Sym *isymbuf = NULL;
3757 Elf_Internal_Sym *isym;
3758 Elf_Internal_Sym *isymend;
3759 const struct elf_backend_data *bed;
3760 bfd_boolean add_needed;
3761 struct elf_link_hash_table *htab;
3763 void *alloc_mark = NULL;
3764 struct bfd_hash_entry **old_table = NULL;
3765 unsigned int old_size = 0;
3766 unsigned int old_count = 0;
3767 void *old_tab = NULL;
3769 struct bfd_link_hash_entry *old_undefs = NULL;
3770 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3771 void *old_strtab = NULL;
3774 bfd_boolean just_syms;
3776 htab = elf_hash_table (info);
3777 bed = get_elf_backend_data (abfd);
3779 if ((abfd->flags & DYNAMIC) == 0)
3785 /* You can't use -r against a dynamic object. Also, there's no
3786 hope of using a dynamic object which does not exactly match
3787 the format of the output file. */
3788 if (bfd_link_relocatable (info)
3789 || !is_elf_hash_table (htab)
3790 || info->output_bfd->xvec != abfd->xvec)
3792 if (bfd_link_relocatable (info))
3793 bfd_set_error (bfd_error_invalid_operation);
3795 bfd_set_error (bfd_error_wrong_format);
3800 ehdr = elf_elfheader (abfd);
3801 if (info->warn_alternate_em
3802 && bed->elf_machine_code != ehdr->e_machine
3803 && ((bed->elf_machine_alt1 != 0
3804 && ehdr->e_machine == bed->elf_machine_alt1)
3805 || (bed->elf_machine_alt2 != 0
3806 && ehdr->e_machine == bed->elf_machine_alt2)))
3807 info->callbacks->einfo
3808 /* xgettext:c-format */
3809 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3810 ehdr->e_machine, abfd, bed->elf_machine_code);
3812 /* As a GNU extension, any input sections which are named
3813 .gnu.warning.SYMBOL are treated as warning symbols for the given
3814 symbol. This differs from .gnu.warning sections, which generate
3815 warnings when they are included in an output file. */
3816 /* PR 12761: Also generate this warning when building shared libraries. */
3817 for (s = abfd->sections; s != NULL; s = s->next)
3821 name = bfd_get_section_name (abfd, s);
3822 if (CONST_STRNEQ (name, ".gnu.warning."))
3827 name += sizeof ".gnu.warning." - 1;
3829 /* If this is a shared object, then look up the symbol
3830 in the hash table. If it is there, and it is already
3831 been defined, then we will not be using the entry
3832 from this shared object, so we don't need to warn.
3833 FIXME: If we see the definition in a regular object
3834 later on, we will warn, but we shouldn't. The only
3835 fix is to keep track of what warnings we are supposed
3836 to emit, and then handle them all at the end of the
3840 struct elf_link_hash_entry *h;
3842 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3844 /* FIXME: What about bfd_link_hash_common? */
3846 && (h->root.type == bfd_link_hash_defined
3847 || h->root.type == bfd_link_hash_defweak))
3852 msg = (char *) bfd_alloc (abfd, sz + 1);
3856 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3861 if (! (_bfd_generic_link_add_one_symbol
3862 (info, abfd, name, BSF_WARNING, s, 0, msg,
3863 FALSE, bed->collect, NULL)))
3866 if (bfd_link_executable (info))
3868 /* Clobber the section size so that the warning does
3869 not get copied into the output file. */
3872 /* Also set SEC_EXCLUDE, so that symbols defined in
3873 the warning section don't get copied to the output. */
3874 s->flags |= SEC_EXCLUDE;
3879 just_syms = ((s = abfd->sections) != NULL
3880 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3885 /* If we are creating a shared library, create all the dynamic
3886 sections immediately. We need to attach them to something,
3887 so we attach them to this BFD, provided it is the right
3888 format and is not from ld --just-symbols. Always create the
3889 dynamic sections for -E/--dynamic-list. FIXME: If there
3890 are no input BFD's of the same format as the output, we can't
3891 make a shared library. */
3893 && (bfd_link_pic (info)
3894 || (!bfd_link_relocatable (info)
3896 && (info->export_dynamic || info->dynamic)))
3897 && is_elf_hash_table (htab)
3898 && info->output_bfd->xvec == abfd->xvec
3899 && !htab->dynamic_sections_created)
3901 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3905 else if (!is_elf_hash_table (htab))
3909 const char *soname = NULL;
3911 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3912 const Elf_Internal_Phdr *phdr;
3915 /* ld --just-symbols and dynamic objects don't mix very well.
3916 ld shouldn't allow it. */
3920 /* If this dynamic lib was specified on the command line with
3921 --as-needed in effect, then we don't want to add a DT_NEEDED
3922 tag unless the lib is actually used. Similary for libs brought
3923 in by another lib's DT_NEEDED. When --no-add-needed is used
3924 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3925 any dynamic library in DT_NEEDED tags in the dynamic lib at
3927 add_needed = (elf_dyn_lib_class (abfd)
3928 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3929 | DYN_NO_NEEDED)) == 0;
3931 s = bfd_get_section_by_name (abfd, ".dynamic");
3936 unsigned int elfsec;
3937 unsigned long shlink;
3939 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3946 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3947 if (elfsec == SHN_BAD)
3948 goto error_free_dyn;
3949 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3951 for (extdyn = dynbuf;
3952 extdyn < dynbuf + s->size;
3953 extdyn += bed->s->sizeof_dyn)
3955 Elf_Internal_Dyn dyn;
3957 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3958 if (dyn.d_tag == DT_SONAME)
3960 unsigned int tagv = dyn.d_un.d_val;
3961 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3963 goto error_free_dyn;
3965 if (dyn.d_tag == DT_NEEDED)
3967 struct bfd_link_needed_list *n, **pn;
3969 unsigned int tagv = dyn.d_un.d_val;
3971 amt = sizeof (struct bfd_link_needed_list);
3972 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3973 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3974 if (n == NULL || fnm == NULL)
3975 goto error_free_dyn;
3976 amt = strlen (fnm) + 1;
3977 anm = (char *) bfd_alloc (abfd, amt);
3979 goto error_free_dyn;
3980 memcpy (anm, fnm, amt);
3984 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3988 if (dyn.d_tag == DT_RUNPATH)
3990 struct bfd_link_needed_list *n, **pn;
3992 unsigned int tagv = dyn.d_un.d_val;
3994 amt = sizeof (struct bfd_link_needed_list);
3995 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3996 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3997 if (n == NULL || fnm == NULL)
3998 goto error_free_dyn;
3999 amt = strlen (fnm) + 1;
4000 anm = (char *) bfd_alloc (abfd, amt);
4002 goto error_free_dyn;
4003 memcpy (anm, fnm, amt);
4007 for (pn = & runpath;
4013 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4014 if (!runpath && dyn.d_tag == DT_RPATH)
4016 struct bfd_link_needed_list *n, **pn;
4018 unsigned int tagv = dyn.d_un.d_val;
4020 amt = sizeof (struct bfd_link_needed_list);
4021 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4022 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4023 if (n == NULL || fnm == NULL)
4024 goto error_free_dyn;
4025 amt = strlen (fnm) + 1;
4026 anm = (char *) bfd_alloc (abfd, amt);
4028 goto error_free_dyn;
4029 memcpy (anm, fnm, amt);
4039 if (dyn.d_tag == DT_AUDIT)
4041 unsigned int tagv = dyn.d_un.d_val;
4042 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4049 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4050 frees all more recently bfd_alloc'd blocks as well. */
4056 struct bfd_link_needed_list **pn;
4057 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4062 /* If we have a PT_GNU_RELRO program header, mark as read-only
4063 all sections contained fully therein. This makes relro
4064 shared library sections appear as they will at run-time. */
4065 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4066 while (--phdr >= elf_tdata (abfd)->phdr)
4067 if (phdr->p_type == PT_GNU_RELRO)
4069 for (s = abfd->sections; s != NULL; s = s->next)
4070 if ((s->flags & SEC_ALLOC) != 0
4071 && s->vma >= phdr->p_vaddr
4072 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4073 s->flags |= SEC_READONLY;
4077 /* We do not want to include any of the sections in a dynamic
4078 object in the output file. We hack by simply clobbering the
4079 list of sections in the BFD. This could be handled more
4080 cleanly by, say, a new section flag; the existing
4081 SEC_NEVER_LOAD flag is not the one we want, because that one
4082 still implies that the section takes up space in the output
4084 bfd_section_list_clear (abfd);
4086 /* Find the name to use in a DT_NEEDED entry that refers to this
4087 object. If the object has a DT_SONAME entry, we use it.
4088 Otherwise, if the generic linker stuck something in
4089 elf_dt_name, we use that. Otherwise, we just use the file
4091 if (soname == NULL || *soname == '\0')
4093 soname = elf_dt_name (abfd);
4094 if (soname == NULL || *soname == '\0')
4095 soname = bfd_get_filename (abfd);
4098 /* Save the SONAME because sometimes the linker emulation code
4099 will need to know it. */
4100 elf_dt_name (abfd) = soname;
4102 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4106 /* If we have already included this dynamic object in the
4107 link, just ignore it. There is no reason to include a
4108 particular dynamic object more than once. */
4112 /* Save the DT_AUDIT entry for the linker emulation code. */
4113 elf_dt_audit (abfd) = audit;
4116 /* If this is a dynamic object, we always link against the .dynsym
4117 symbol table, not the .symtab symbol table. The dynamic linker
4118 will only see the .dynsym symbol table, so there is no reason to
4119 look at .symtab for a dynamic object. */
4121 if (! dynamic || elf_dynsymtab (abfd) == 0)
4122 hdr = &elf_tdata (abfd)->symtab_hdr;
4124 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4126 symcount = hdr->sh_size / bed->s->sizeof_sym;
4128 /* The sh_info field of the symtab header tells us where the
4129 external symbols start. We don't care about the local symbols at
4131 if (elf_bad_symtab (abfd))
4133 extsymcount = symcount;
4138 extsymcount = symcount - hdr->sh_info;
4139 extsymoff = hdr->sh_info;
4142 sym_hash = elf_sym_hashes (abfd);
4143 if (extsymcount != 0)
4145 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4147 if (isymbuf == NULL)
4150 if (sym_hash == NULL)
4152 /* We store a pointer to the hash table entry for each
4155 amt *= sizeof (struct elf_link_hash_entry *);
4156 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4157 if (sym_hash == NULL)
4158 goto error_free_sym;
4159 elf_sym_hashes (abfd) = sym_hash;
4165 /* Read in any version definitions. */
4166 if (!_bfd_elf_slurp_version_tables (abfd,
4167 info->default_imported_symver))
4168 goto error_free_sym;
4170 /* Read in the symbol versions, but don't bother to convert them
4171 to internal format. */
4172 if (elf_dynversym (abfd) != 0)
4174 Elf_Internal_Shdr *versymhdr;
4176 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4177 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4178 if (extversym == NULL)
4179 goto error_free_sym;
4180 amt = versymhdr->sh_size;
4181 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4182 || bfd_bread (extversym, amt, abfd) != amt)
4183 goto error_free_vers;
4187 /* If we are loading an as-needed shared lib, save the symbol table
4188 state before we start adding symbols. If the lib turns out
4189 to be unneeded, restore the state. */
4190 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4195 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4197 struct bfd_hash_entry *p;
4198 struct elf_link_hash_entry *h;
4200 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4202 h = (struct elf_link_hash_entry *) p;
4203 entsize += htab->root.table.entsize;
4204 if (h->root.type == bfd_link_hash_warning)
4205 entsize += htab->root.table.entsize;
4209 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4210 old_tab = bfd_malloc (tabsize + entsize);
4211 if (old_tab == NULL)
4212 goto error_free_vers;
4214 /* Remember the current objalloc pointer, so that all mem for
4215 symbols added can later be reclaimed. */
4216 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4217 if (alloc_mark == NULL)
4218 goto error_free_vers;
4220 /* Make a special call to the linker "notice" function to
4221 tell it that we are about to handle an as-needed lib. */
4222 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4223 goto error_free_vers;
4225 /* Clone the symbol table. Remember some pointers into the
4226 symbol table, and dynamic symbol count. */
4227 old_ent = (char *) old_tab + tabsize;
4228 memcpy (old_tab, htab->root.table.table, tabsize);
4229 old_undefs = htab->root.undefs;
4230 old_undefs_tail = htab->root.undefs_tail;
4231 old_table = htab->root.table.table;
4232 old_size = htab->root.table.size;
4233 old_count = htab->root.table.count;
4234 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4235 if (old_strtab == NULL)
4236 goto error_free_vers;
4238 for (i = 0; i < htab->root.table.size; i++)
4240 struct bfd_hash_entry *p;
4241 struct elf_link_hash_entry *h;
4243 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4245 memcpy (old_ent, p, htab->root.table.entsize);
4246 old_ent = (char *) old_ent + htab->root.table.entsize;
4247 h = (struct elf_link_hash_entry *) p;
4248 if (h->root.type == bfd_link_hash_warning)
4250 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4251 old_ent = (char *) old_ent + htab->root.table.entsize;
4258 ever = extversym != NULL ? extversym + extsymoff : NULL;
4259 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4261 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4265 asection *sec, *new_sec;
4268 struct elf_link_hash_entry *h;
4269 struct elf_link_hash_entry *hi;
4270 bfd_boolean definition;
4271 bfd_boolean size_change_ok;
4272 bfd_boolean type_change_ok;
4273 bfd_boolean new_weak;
4274 bfd_boolean old_weak;
4275 bfd_boolean override;
4277 bfd_boolean discarded;
4278 unsigned int old_alignment;
4280 bfd_boolean matched;
4284 flags = BSF_NO_FLAGS;
4286 value = isym->st_value;
4287 common = bed->common_definition (isym);
4288 if (common && info->inhibit_common_definition)
4290 /* Treat common symbol as undefined for --no-define-common. */
4291 isym->st_shndx = SHN_UNDEF;
4296 bind = ELF_ST_BIND (isym->st_info);
4300 /* This should be impossible, since ELF requires that all
4301 global symbols follow all local symbols, and that sh_info
4302 point to the first global symbol. Unfortunately, Irix 5
4307 if (isym->st_shndx != SHN_UNDEF && !common)
4315 case STB_GNU_UNIQUE:
4316 flags = BSF_GNU_UNIQUE;
4320 /* Leave it up to the processor backend. */
4324 if (isym->st_shndx == SHN_UNDEF)
4325 sec = bfd_und_section_ptr;
4326 else if (isym->st_shndx == SHN_ABS)
4327 sec = bfd_abs_section_ptr;
4328 else if (isym->st_shndx == SHN_COMMON)
4330 sec = bfd_com_section_ptr;
4331 /* What ELF calls the size we call the value. What ELF
4332 calls the value we call the alignment. */
4333 value = isym->st_size;
4337 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4339 sec = bfd_abs_section_ptr;
4340 else if (discarded_section (sec))
4342 /* Symbols from discarded section are undefined. We keep
4344 sec = bfd_und_section_ptr;
4346 isym->st_shndx = SHN_UNDEF;
4348 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4352 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4355 goto error_free_vers;
4357 if (isym->st_shndx == SHN_COMMON
4358 && (abfd->flags & BFD_PLUGIN) != 0)
4360 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4364 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4366 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4368 goto error_free_vers;
4372 else if (isym->st_shndx == SHN_COMMON
4373 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4374 && !bfd_link_relocatable (info))
4376 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4380 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4381 | SEC_LINKER_CREATED);
4382 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4384 goto error_free_vers;
4388 else if (bed->elf_add_symbol_hook)
4390 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4392 goto error_free_vers;
4394 /* The hook function sets the name to NULL if this symbol
4395 should be skipped for some reason. */
4400 /* Sanity check that all possibilities were handled. */
4403 bfd_set_error (bfd_error_bad_value);
4404 goto error_free_vers;
4407 /* Silently discard TLS symbols from --just-syms. There's
4408 no way to combine a static TLS block with a new TLS block
4409 for this executable. */
4410 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4411 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4414 if (bfd_is_und_section (sec)
4415 || bfd_is_com_section (sec))
4420 size_change_ok = FALSE;
4421 type_change_ok = bed->type_change_ok;
4428 if (is_elf_hash_table (htab))
4430 Elf_Internal_Versym iver;
4431 unsigned int vernum = 0;
4436 if (info->default_imported_symver)
4437 /* Use the default symbol version created earlier. */
4438 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4443 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4445 vernum = iver.vs_vers & VERSYM_VERSION;
4447 /* If this is a hidden symbol, or if it is not version
4448 1, we append the version name to the symbol name.
4449 However, we do not modify a non-hidden absolute symbol
4450 if it is not a function, because it might be the version
4451 symbol itself. FIXME: What if it isn't? */
4452 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4454 && (!bfd_is_abs_section (sec)
4455 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4458 size_t namelen, verlen, newlen;
4461 if (isym->st_shndx != SHN_UNDEF)
4463 if (vernum > elf_tdata (abfd)->cverdefs)
4465 else if (vernum > 1)
4467 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4474 /* xgettext:c-format */
4475 (_("%B: %s: invalid version %u (max %d)"),
4477 elf_tdata (abfd)->cverdefs);
4478 bfd_set_error (bfd_error_bad_value);
4479 goto error_free_vers;
4484 /* We cannot simply test for the number of
4485 entries in the VERNEED section since the
4486 numbers for the needed versions do not start
4488 Elf_Internal_Verneed *t;
4491 for (t = elf_tdata (abfd)->verref;
4495 Elf_Internal_Vernaux *a;
4497 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4499 if (a->vna_other == vernum)
4501 verstr = a->vna_nodename;
4511 /* xgettext:c-format */
4512 (_("%B: %s: invalid needed version %d"),
4513 abfd, name, vernum);
4514 bfd_set_error (bfd_error_bad_value);
4515 goto error_free_vers;
4519 namelen = strlen (name);
4520 verlen = strlen (verstr);
4521 newlen = namelen + verlen + 2;
4522 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4523 && isym->st_shndx != SHN_UNDEF)
4526 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4527 if (newname == NULL)
4528 goto error_free_vers;
4529 memcpy (newname, name, namelen);
4530 p = newname + namelen;
4532 /* If this is a defined non-hidden version symbol,
4533 we add another @ to the name. This indicates the
4534 default version of the symbol. */
4535 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4536 && isym->st_shndx != SHN_UNDEF)
4538 memcpy (p, verstr, verlen + 1);
4543 /* If this symbol has default visibility and the user has
4544 requested we not re-export it, then mark it as hidden. */
4545 if (!bfd_is_und_section (sec)
4548 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4549 isym->st_other = (STV_HIDDEN
4550 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4552 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4553 sym_hash, &old_bfd, &old_weak,
4554 &old_alignment, &skip, &override,
4555 &type_change_ok, &size_change_ok,
4557 goto error_free_vers;
4562 /* Override a definition only if the new symbol matches the
4564 if (override && matched)
4568 while (h->root.type == bfd_link_hash_indirect
4569 || h->root.type == bfd_link_hash_warning)
4570 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4572 if (elf_tdata (abfd)->verdef != NULL
4575 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4578 if (! (_bfd_generic_link_add_one_symbol
4579 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4580 (struct bfd_link_hash_entry **) sym_hash)))
4581 goto error_free_vers;
4583 if ((flags & BSF_GNU_UNIQUE)
4584 && (abfd->flags & DYNAMIC) == 0
4585 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4586 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4589 /* We need to make sure that indirect symbol dynamic flags are
4592 while (h->root.type == bfd_link_hash_indirect
4593 || h->root.type == bfd_link_hash_warning)
4594 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4596 /* Setting the index to -3 tells elf_link_output_extsym that
4597 this symbol is defined in a discarded section. */
4603 new_weak = (flags & BSF_WEAK) != 0;
4607 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4608 && is_elf_hash_table (htab)
4609 && h->u.alias == NULL)
4611 /* Keep a list of all weak defined non function symbols from
4612 a dynamic object, using the alias field. Later in this
4613 function we will set the alias field to the correct
4614 value. We only put non-function symbols from dynamic
4615 objects on this list, because that happens to be the only
4616 time we need to know the normal symbol corresponding to a
4617 weak symbol, and the information is time consuming to
4618 figure out. If the alias field is not already NULL,
4619 then this symbol was already defined by some previous
4620 dynamic object, and we will be using that previous
4621 definition anyhow. */
4627 /* Set the alignment of a common symbol. */
4628 if ((common || bfd_is_com_section (sec))
4629 && h->root.type == bfd_link_hash_common)
4634 align = bfd_log2 (isym->st_value);
4637 /* The new symbol is a common symbol in a shared object.
4638 We need to get the alignment from the section. */
4639 align = new_sec->alignment_power;
4641 if (align > old_alignment)
4642 h->root.u.c.p->alignment_power = align;
4644 h->root.u.c.p->alignment_power = old_alignment;
4647 if (is_elf_hash_table (htab))
4649 /* Set a flag in the hash table entry indicating the type of
4650 reference or definition we just found. A dynamic symbol
4651 is one which is referenced or defined by both a regular
4652 object and a shared object. */
4653 bfd_boolean dynsym = FALSE;
4655 /* Plugin symbols aren't normal. Don't set def_regular or
4656 ref_regular for them, or make them dynamic. */
4657 if ((abfd->flags & BFD_PLUGIN) != 0)
4664 if (bind != STB_WEAK)
4665 h->ref_regular_nonweak = 1;
4677 /* If the indirect symbol has been forced local, don't
4678 make the real symbol dynamic. */
4679 if ((h == hi || !hi->forced_local)
4680 && (bfd_link_dll (info)
4690 hi->ref_dynamic = 1;
4695 hi->def_dynamic = 1;
4698 /* If the indirect symbol has been forced local, don't
4699 make the real symbol dynamic. */
4700 if ((h == hi || !hi->forced_local)
4704 && weakdef (h)->dynindx != -1)))
4708 /* Check to see if we need to add an indirect symbol for
4709 the default name. */
4711 || (!override && h->root.type == bfd_link_hash_common))
4712 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4713 sec, value, &old_bfd, &dynsym))
4714 goto error_free_vers;
4716 /* Check the alignment when a common symbol is involved. This
4717 can change when a common symbol is overridden by a normal
4718 definition or a common symbol is ignored due to the old
4719 normal definition. We need to make sure the maximum
4720 alignment is maintained. */
4721 if ((old_alignment || common)
4722 && h->root.type != bfd_link_hash_common)
4724 unsigned int common_align;
4725 unsigned int normal_align;
4726 unsigned int symbol_align;
4730 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4731 || h->root.type == bfd_link_hash_defweak);
4733 symbol_align = ffs (h->root.u.def.value) - 1;
4734 if (h->root.u.def.section->owner != NULL
4735 && (h->root.u.def.section->owner->flags
4736 & (DYNAMIC | BFD_PLUGIN)) == 0)
4738 normal_align = h->root.u.def.section->alignment_power;
4739 if (normal_align > symbol_align)
4740 normal_align = symbol_align;
4743 normal_align = symbol_align;
4747 common_align = old_alignment;
4748 common_bfd = old_bfd;
4753 common_align = bfd_log2 (isym->st_value);
4755 normal_bfd = old_bfd;
4758 if (normal_align < common_align)
4760 /* PR binutils/2735 */
4761 if (normal_bfd == NULL)
4763 /* xgettext:c-format */
4764 (_("Warning: alignment %u of common symbol `%s' in %B is"
4765 " greater than the alignment (%u) of its section %A"),
4766 1 << common_align, name, common_bfd,
4767 1 << normal_align, h->root.u.def.section);
4770 /* xgettext:c-format */
4771 (_("Warning: alignment %u of symbol `%s' in %B"
4772 " is smaller than %u in %B"),
4773 1 << normal_align, name, normal_bfd,
4774 1 << common_align, common_bfd);
4778 /* Remember the symbol size if it isn't undefined. */
4779 if (isym->st_size != 0
4780 && isym->st_shndx != SHN_UNDEF
4781 && (definition || h->size == 0))
4784 && h->size != isym->st_size
4785 && ! size_change_ok)
4787 /* xgettext:c-format */
4788 (_("Warning: size of symbol `%s' changed"
4789 " from %Lu in %B to %Lu in %B"),
4790 name, h->size, old_bfd, isym->st_size, abfd);
4792 h->size = isym->st_size;
4795 /* If this is a common symbol, then we always want H->SIZE
4796 to be the size of the common symbol. The code just above
4797 won't fix the size if a common symbol becomes larger. We
4798 don't warn about a size change here, because that is
4799 covered by --warn-common. Allow changes between different
4801 if (h->root.type == bfd_link_hash_common)
4802 h->size = h->root.u.c.size;
4804 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4805 && ((definition && !new_weak)
4806 || (old_weak && h->root.type == bfd_link_hash_common)
4807 || h->type == STT_NOTYPE))
4809 unsigned int type = ELF_ST_TYPE (isym->st_info);
4811 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4813 if (type == STT_GNU_IFUNC
4814 && (abfd->flags & DYNAMIC) != 0)
4817 if (h->type != type)
4819 if (h->type != STT_NOTYPE && ! type_change_ok)
4820 /* xgettext:c-format */
4822 (_("Warning: type of symbol `%s' changed"
4823 " from %d to %d in %B"),
4824 name, h->type, type, abfd);
4830 /* Merge st_other field. */
4831 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4833 /* We don't want to make debug symbol dynamic. */
4835 && (sec->flags & SEC_DEBUGGING)
4836 && !bfd_link_relocatable (info))
4839 /* Nor should we make plugin symbols dynamic. */
4840 if ((abfd->flags & BFD_PLUGIN) != 0)
4845 h->target_internal = isym->st_target_internal;
4846 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4849 if (definition && !dynamic)
4851 char *p = strchr (name, ELF_VER_CHR);
4852 if (p != NULL && p[1] != ELF_VER_CHR)
4854 /* Queue non-default versions so that .symver x, x@FOO
4855 aliases can be checked. */
4858 amt = ((isymend - isym + 1)
4859 * sizeof (struct elf_link_hash_entry *));
4861 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4863 goto error_free_vers;
4865 nondeflt_vers[nondeflt_vers_cnt++] = h;
4869 if (dynsym && h->dynindx == -1)
4871 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4872 goto error_free_vers;
4874 && weakdef (h)->dynindx == -1)
4876 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4877 goto error_free_vers;
4880 else if (h->dynindx != -1)
4881 /* If the symbol already has a dynamic index, but
4882 visibility says it should not be visible, turn it into
4884 switch (ELF_ST_VISIBILITY (h->other))
4888 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4893 /* Don't add DT_NEEDED for references from the dummy bfd nor
4894 for unmatched symbol. */
4899 && h->ref_regular_nonweak
4901 || (old_bfd->flags & BFD_PLUGIN) == 0))
4902 || (h->ref_dynamic_nonweak
4903 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4904 && !on_needed_list (elf_dt_name (abfd),
4905 htab->needed, NULL))))
4908 const char *soname = elf_dt_name (abfd);
4910 info->callbacks->minfo ("%!", soname, old_bfd,
4911 h->root.root.string);
4913 /* A symbol from a library loaded via DT_NEEDED of some
4914 other library is referenced by a regular object.
4915 Add a DT_NEEDED entry for it. Issue an error if
4916 --no-add-needed is used and the reference was not
4919 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4922 /* xgettext:c-format */
4923 (_("%B: undefined reference to symbol '%s'"),
4925 bfd_set_error (bfd_error_missing_dso);
4926 goto error_free_vers;
4929 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4930 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4933 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4935 goto error_free_vers;
4937 BFD_ASSERT (ret == 0);
4942 if (info->lto_plugin_active
4943 && !bfd_link_relocatable (info)
4944 && (abfd->flags & BFD_PLUGIN) == 0
4950 if (bed->s->arch_size == 32)
4955 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
4956 referenced in regular objects so that linker plugin will get
4957 the correct symbol resolution. */
4959 sym_hash = elf_sym_hashes (abfd);
4960 for (s = abfd->sections; s != NULL; s = s->next)
4962 Elf_Internal_Rela *internal_relocs;
4963 Elf_Internal_Rela *rel, *relend;
4965 /* Don't check relocations in excluded sections. */
4966 if ((s->flags & SEC_RELOC) == 0
4967 || s->reloc_count == 0
4968 || (s->flags & SEC_EXCLUDE) != 0
4969 || ((info->strip == strip_all
4970 || info->strip == strip_debugger)
4971 && (s->flags & SEC_DEBUGGING) != 0))
4974 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
4977 if (internal_relocs == NULL)
4978 goto error_free_vers;
4980 rel = internal_relocs;
4981 relend = rel + s->reloc_count;
4982 for ( ; rel < relend; rel++)
4984 unsigned long r_symndx = rel->r_info >> r_sym_shift;
4985 struct elf_link_hash_entry *h;
4987 /* Skip local symbols. */
4988 if (r_symndx < extsymoff)
4991 h = sym_hash[r_symndx - extsymoff];
4993 h->root.non_ir_ref_regular = 1;
4996 if (elf_section_data (s)->relocs != internal_relocs)
4997 free (internal_relocs);
5001 if (extversym != NULL)
5007 if (isymbuf != NULL)
5013 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5017 /* Restore the symbol table. */
5018 old_ent = (char *) old_tab + tabsize;
5019 memset (elf_sym_hashes (abfd), 0,
5020 extsymcount * sizeof (struct elf_link_hash_entry *));
5021 htab->root.table.table = old_table;
5022 htab->root.table.size = old_size;
5023 htab->root.table.count = old_count;
5024 memcpy (htab->root.table.table, old_tab, tabsize);
5025 htab->root.undefs = old_undefs;
5026 htab->root.undefs_tail = old_undefs_tail;
5027 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5030 for (i = 0; i < htab->root.table.size; i++)
5032 struct bfd_hash_entry *p;
5033 struct elf_link_hash_entry *h;
5035 unsigned int alignment_power;
5036 unsigned int non_ir_ref_dynamic;
5038 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5040 h = (struct elf_link_hash_entry *) p;
5041 if (h->root.type == bfd_link_hash_warning)
5042 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5044 /* Preserve the maximum alignment and size for common
5045 symbols even if this dynamic lib isn't on DT_NEEDED
5046 since it can still be loaded at run time by another
5048 if (h->root.type == bfd_link_hash_common)
5050 size = h->root.u.c.size;
5051 alignment_power = h->root.u.c.p->alignment_power;
5056 alignment_power = 0;
5058 /* Preserve non_ir_ref_dynamic so that this symbol
5059 will be exported when the dynamic lib becomes needed
5060 in the second pass. */
5061 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5062 memcpy (p, old_ent, htab->root.table.entsize);
5063 old_ent = (char *) old_ent + htab->root.table.entsize;
5064 h = (struct elf_link_hash_entry *) p;
5065 if (h->root.type == bfd_link_hash_warning)
5067 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5068 old_ent = (char *) old_ent + htab->root.table.entsize;
5069 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5071 if (h->root.type == bfd_link_hash_common)
5073 if (size > h->root.u.c.size)
5074 h->root.u.c.size = size;
5075 if (alignment_power > h->root.u.c.p->alignment_power)
5076 h->root.u.c.p->alignment_power = alignment_power;
5078 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5082 /* Make a special call to the linker "notice" function to
5083 tell it that symbols added for crefs may need to be removed. */
5084 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5085 goto error_free_vers;
5088 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5090 if (nondeflt_vers != NULL)
5091 free (nondeflt_vers);
5095 if (old_tab != NULL)
5097 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5098 goto error_free_vers;
5103 /* Now that all the symbols from this input file are created, if
5104 not performing a relocatable link, handle .symver foo, foo@BAR
5105 such that any relocs against foo become foo@BAR. */
5106 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5110 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5112 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5113 char *shortname, *p;
5115 p = strchr (h->root.root.string, ELF_VER_CHR);
5117 || (h->root.type != bfd_link_hash_defined
5118 && h->root.type != bfd_link_hash_defweak))
5121 amt = p - h->root.root.string;
5122 shortname = (char *) bfd_malloc (amt + 1);
5124 goto error_free_vers;
5125 memcpy (shortname, h->root.root.string, amt);
5126 shortname[amt] = '\0';
5128 hi = (struct elf_link_hash_entry *)
5129 bfd_link_hash_lookup (&htab->root, shortname,
5130 FALSE, FALSE, FALSE);
5132 && hi->root.type == h->root.type
5133 && hi->root.u.def.value == h->root.u.def.value
5134 && hi->root.u.def.section == h->root.u.def.section)
5136 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5137 hi->root.type = bfd_link_hash_indirect;
5138 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5139 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5140 sym_hash = elf_sym_hashes (abfd);
5142 for (symidx = 0; symidx < extsymcount; ++symidx)
5143 if (sym_hash[symidx] == hi)
5145 sym_hash[symidx] = h;
5151 free (nondeflt_vers);
5152 nondeflt_vers = NULL;
5155 /* Now set the alias field correctly for all the weak defined
5156 symbols we found. The only way to do this is to search all the
5157 symbols. Since we only need the information for non functions in
5158 dynamic objects, that's the only time we actually put anything on
5159 the list WEAKS. We need this information so that if a regular
5160 object refers to a symbol defined weakly in a dynamic object, the
5161 real symbol in the dynamic object is also put in the dynamic
5162 symbols; we also must arrange for both symbols to point to the
5163 same memory location. We could handle the general case of symbol
5164 aliasing, but a general symbol alias can only be generated in
5165 assembler code, handling it correctly would be very time
5166 consuming, and other ELF linkers don't handle general aliasing
5170 struct elf_link_hash_entry **hpp;
5171 struct elf_link_hash_entry **hppend;
5172 struct elf_link_hash_entry **sorted_sym_hash;
5173 struct elf_link_hash_entry *h;
5176 /* Since we have to search the whole symbol list for each weak
5177 defined symbol, search time for N weak defined symbols will be
5178 O(N^2). Binary search will cut it down to O(NlogN). */
5180 amt *= sizeof (struct elf_link_hash_entry *);
5181 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5182 if (sorted_sym_hash == NULL)
5184 sym_hash = sorted_sym_hash;
5185 hpp = elf_sym_hashes (abfd);
5186 hppend = hpp + extsymcount;
5188 for (; hpp < hppend; hpp++)
5192 && h->root.type == bfd_link_hash_defined
5193 && !bed->is_function_type (h->type))
5201 qsort (sorted_sym_hash, sym_count,
5202 sizeof (struct elf_link_hash_entry *),
5205 while (weaks != NULL)
5207 struct elf_link_hash_entry *hlook;
5210 size_t i, j, idx = 0;
5213 weaks = hlook->u.alias;
5214 hlook->u.alias = NULL;
5216 if (hlook->root.type != bfd_link_hash_defined
5217 && hlook->root.type != bfd_link_hash_defweak)
5220 slook = hlook->root.u.def.section;
5221 vlook = hlook->root.u.def.value;
5227 bfd_signed_vma vdiff;
5229 h = sorted_sym_hash[idx];
5230 vdiff = vlook - h->root.u.def.value;
5237 int sdiff = slook->id - h->root.u.def.section->id;
5247 /* We didn't find a value/section match. */
5251 /* With multiple aliases, or when the weak symbol is already
5252 strongly defined, we have multiple matching symbols and
5253 the binary search above may land on any of them. Step
5254 one past the matching symbol(s). */
5257 h = sorted_sym_hash[idx];
5258 if (h->root.u.def.section != slook
5259 || h->root.u.def.value != vlook)
5263 /* Now look back over the aliases. Since we sorted by size
5264 as well as value and section, we'll choose the one with
5265 the largest size. */
5268 h = sorted_sym_hash[idx];
5270 /* Stop if value or section doesn't match. */
5271 if (h->root.u.def.section != slook
5272 || h->root.u.def.value != vlook)
5274 else if (h != hlook)
5276 struct elf_link_hash_entry *t;
5279 hlook->is_weakalias = 1;
5281 if (t->u.alias != NULL)
5282 while (t->u.alias != h)
5286 /* If the weak definition is in the list of dynamic
5287 symbols, make sure the real definition is put
5289 if (hlook->dynindx != -1 && h->dynindx == -1)
5291 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5294 free (sorted_sym_hash);
5299 /* If the real definition is in the list of dynamic
5300 symbols, make sure the weak definition is put
5301 there as well. If we don't do this, then the
5302 dynamic loader might not merge the entries for the
5303 real definition and the weak definition. */
5304 if (h->dynindx != -1 && hlook->dynindx == -1)
5306 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5307 goto err_free_sym_hash;
5314 free (sorted_sym_hash);
5317 if (bed->check_directives
5318 && !(*bed->check_directives) (abfd, info))
5321 /* If this is a non-traditional link, try to optimize the handling
5322 of the .stab/.stabstr sections. */
5324 && ! info->traditional_format
5325 && is_elf_hash_table (htab)
5326 && (info->strip != strip_all && info->strip != strip_debugger))
5330 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5331 if (stabstr != NULL)
5333 bfd_size_type string_offset = 0;
5336 for (stab = abfd->sections; stab; stab = stab->next)
5337 if (CONST_STRNEQ (stab->name, ".stab")
5338 && (!stab->name[5] ||
5339 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5340 && (stab->flags & SEC_MERGE) == 0
5341 && !bfd_is_abs_section (stab->output_section))
5343 struct bfd_elf_section_data *secdata;
5345 secdata = elf_section_data (stab);
5346 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5347 stabstr, &secdata->sec_info,
5350 if (secdata->sec_info)
5351 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5356 if (is_elf_hash_table (htab) && add_needed)
5358 /* Add this bfd to the loaded list. */
5359 struct elf_link_loaded_list *n;
5361 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5365 n->next = htab->loaded;
5372 if (old_tab != NULL)
5374 if (old_strtab != NULL)
5376 if (nondeflt_vers != NULL)
5377 free (nondeflt_vers);
5378 if (extversym != NULL)
5381 if (isymbuf != NULL)
5387 /* Return the linker hash table entry of a symbol that might be
5388 satisfied by an archive symbol. Return -1 on error. */
5390 struct elf_link_hash_entry *
5391 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5392 struct bfd_link_info *info,
5395 struct elf_link_hash_entry *h;
5399 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5403 /* If this is a default version (the name contains @@), look up the
5404 symbol again with only one `@' as well as without the version.
5405 The effect is that references to the symbol with and without the
5406 version will be matched by the default symbol in the archive. */
5408 p = strchr (name, ELF_VER_CHR);
5409 if (p == NULL || p[1] != ELF_VER_CHR)
5412 /* First check with only one `@'. */
5413 len = strlen (name);
5414 copy = (char *) bfd_alloc (abfd, len);
5416 return (struct elf_link_hash_entry *) 0 - 1;
5418 first = p - name + 1;
5419 memcpy (copy, name, first);
5420 memcpy (copy + first, name + first + 1, len - first);
5422 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5425 /* We also need to check references to the symbol without the
5427 copy[first - 1] = '\0';
5428 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5429 FALSE, FALSE, TRUE);
5432 bfd_release (abfd, copy);
5436 /* Add symbols from an ELF archive file to the linker hash table. We
5437 don't use _bfd_generic_link_add_archive_symbols because we need to
5438 handle versioned symbols.
5440 Fortunately, ELF archive handling is simpler than that done by
5441 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5442 oddities. In ELF, if we find a symbol in the archive map, and the
5443 symbol is currently undefined, we know that we must pull in that
5446 Unfortunately, we do have to make multiple passes over the symbol
5447 table until nothing further is resolved. */
5450 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5453 unsigned char *included = NULL;
5457 const struct elf_backend_data *bed;
5458 struct elf_link_hash_entry * (*archive_symbol_lookup)
5459 (bfd *, struct bfd_link_info *, const char *);
5461 if (! bfd_has_map (abfd))
5463 /* An empty archive is a special case. */
5464 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5466 bfd_set_error (bfd_error_no_armap);
5470 /* Keep track of all symbols we know to be already defined, and all
5471 files we know to be already included. This is to speed up the
5472 second and subsequent passes. */
5473 c = bfd_ardata (abfd)->symdef_count;
5477 amt *= sizeof (*included);
5478 included = (unsigned char *) bfd_zmalloc (amt);
5479 if (included == NULL)
5482 symdefs = bfd_ardata (abfd)->symdefs;
5483 bed = get_elf_backend_data (abfd);
5484 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5497 symdefend = symdef + c;
5498 for (i = 0; symdef < symdefend; symdef++, i++)
5500 struct elf_link_hash_entry *h;
5502 struct bfd_link_hash_entry *undefs_tail;
5507 if (symdef->file_offset == last)
5513 h = archive_symbol_lookup (abfd, info, symdef->name);
5514 if (h == (struct elf_link_hash_entry *) 0 - 1)
5520 if (h->root.type == bfd_link_hash_common)
5522 /* We currently have a common symbol. The archive map contains
5523 a reference to this symbol, so we may want to include it. We
5524 only want to include it however, if this archive element
5525 contains a definition of the symbol, not just another common
5528 Unfortunately some archivers (including GNU ar) will put
5529 declarations of common symbols into their archive maps, as
5530 well as real definitions, so we cannot just go by the archive
5531 map alone. Instead we must read in the element's symbol
5532 table and check that to see what kind of symbol definition
5534 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5537 else if (h->root.type != bfd_link_hash_undefined)
5539 if (h->root.type != bfd_link_hash_undefweak)
5540 /* Symbol must be defined. Don't check it again. */
5545 /* We need to include this archive member. */
5546 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5547 if (element == NULL)
5550 if (! bfd_check_format (element, bfd_object))
5553 undefs_tail = info->hash->undefs_tail;
5555 if (!(*info->callbacks
5556 ->add_archive_element) (info, element, symdef->name, &element))
5558 if (!bfd_link_add_symbols (element, info))
5561 /* If there are any new undefined symbols, we need to make
5562 another pass through the archive in order to see whether
5563 they can be defined. FIXME: This isn't perfect, because
5564 common symbols wind up on undefs_tail and because an
5565 undefined symbol which is defined later on in this pass
5566 does not require another pass. This isn't a bug, but it
5567 does make the code less efficient than it could be. */
5568 if (undefs_tail != info->hash->undefs_tail)
5571 /* Look backward to mark all symbols from this object file
5572 which we have already seen in this pass. */
5576 included[mark] = TRUE;
5581 while (symdefs[mark].file_offset == symdef->file_offset);
5583 /* We mark subsequent symbols from this object file as we go
5584 on through the loop. */
5585 last = symdef->file_offset;
5595 if (included != NULL)
5600 /* Given an ELF BFD, add symbols to the global hash table as
5604 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5606 switch (bfd_get_format (abfd))
5609 return elf_link_add_object_symbols (abfd, info);
5611 return elf_link_add_archive_symbols (abfd, info);
5613 bfd_set_error (bfd_error_wrong_format);
5618 struct hash_codes_info
5620 unsigned long *hashcodes;
5624 /* This function will be called though elf_link_hash_traverse to store
5625 all hash value of the exported symbols in an array. */
5628 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5630 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5635 /* Ignore indirect symbols. These are added by the versioning code. */
5636 if (h->dynindx == -1)
5639 name = h->root.root.string;
5640 if (h->versioned >= versioned)
5642 char *p = strchr (name, ELF_VER_CHR);
5645 alc = (char *) bfd_malloc (p - name + 1);
5651 memcpy (alc, name, p - name);
5652 alc[p - name] = '\0';
5657 /* Compute the hash value. */
5658 ha = bfd_elf_hash (name);
5660 /* Store the found hash value in the array given as the argument. */
5661 *(inf->hashcodes)++ = ha;
5663 /* And store it in the struct so that we can put it in the hash table
5665 h->u.elf_hash_value = ha;
5673 struct collect_gnu_hash_codes
5676 const struct elf_backend_data *bed;
5677 unsigned long int nsyms;
5678 unsigned long int maskbits;
5679 unsigned long int *hashcodes;
5680 unsigned long int *hashval;
5681 unsigned long int *indx;
5682 unsigned long int *counts;
5685 long int min_dynindx;
5686 unsigned long int bucketcount;
5687 unsigned long int symindx;
5688 long int local_indx;
5689 long int shift1, shift2;
5690 unsigned long int mask;
5694 /* This function will be called though elf_link_hash_traverse to store
5695 all hash value of the exported symbols in an array. */
5698 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5700 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5705 /* Ignore indirect symbols. These are added by the versioning code. */
5706 if (h->dynindx == -1)
5709 /* Ignore also local symbols and undefined symbols. */
5710 if (! (*s->bed->elf_hash_symbol) (h))
5713 name = h->root.root.string;
5714 if (h->versioned >= versioned)
5716 char *p = strchr (name, ELF_VER_CHR);
5719 alc = (char *) bfd_malloc (p - name + 1);
5725 memcpy (alc, name, p - name);
5726 alc[p - name] = '\0';
5731 /* Compute the hash value. */
5732 ha = bfd_elf_gnu_hash (name);
5734 /* Store the found hash value in the array for compute_bucket_count,
5735 and also for .dynsym reordering purposes. */
5736 s->hashcodes[s->nsyms] = ha;
5737 s->hashval[h->dynindx] = ha;
5739 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5740 s->min_dynindx = h->dynindx;
5748 /* This function will be called though elf_link_hash_traverse to do
5749 final dynaminc symbol renumbering. */
5752 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5754 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5755 unsigned long int bucket;
5756 unsigned long int val;
5758 /* Ignore indirect symbols. */
5759 if (h->dynindx == -1)
5762 /* Ignore also local symbols and undefined symbols. */
5763 if (! (*s->bed->elf_hash_symbol) (h))
5765 if (h->dynindx >= s->min_dynindx)
5766 h->dynindx = s->local_indx++;
5770 bucket = s->hashval[h->dynindx] % s->bucketcount;
5771 val = (s->hashval[h->dynindx] >> s->shift1)
5772 & ((s->maskbits >> s->shift1) - 1);
5773 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5775 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5776 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5777 if (s->counts[bucket] == 1)
5778 /* Last element terminates the chain. */
5780 bfd_put_32 (s->output_bfd, val,
5781 s->contents + (s->indx[bucket] - s->symindx) * 4);
5782 --s->counts[bucket];
5783 h->dynindx = s->indx[bucket]++;
5787 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5790 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5792 return !(h->forced_local
5793 || h->root.type == bfd_link_hash_undefined
5794 || h->root.type == bfd_link_hash_undefweak
5795 || ((h->root.type == bfd_link_hash_defined
5796 || h->root.type == bfd_link_hash_defweak)
5797 && h->root.u.def.section->output_section == NULL));
5800 /* Array used to determine the number of hash table buckets to use
5801 based on the number of symbols there are. If there are fewer than
5802 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5803 fewer than 37 we use 17 buckets, and so forth. We never use more
5804 than 32771 buckets. */
5806 static const size_t elf_buckets[] =
5808 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5812 /* Compute bucket count for hashing table. We do not use a static set
5813 of possible tables sizes anymore. Instead we determine for all
5814 possible reasonable sizes of the table the outcome (i.e., the
5815 number of collisions etc) and choose the best solution. The
5816 weighting functions are not too simple to allow the table to grow
5817 without bounds. Instead one of the weighting factors is the size.
5818 Therefore the result is always a good payoff between few collisions
5819 (= short chain lengths) and table size. */
5821 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5822 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5823 unsigned long int nsyms,
5826 size_t best_size = 0;
5827 unsigned long int i;
5829 /* We have a problem here. The following code to optimize the table
5830 size requires an integer type with more the 32 bits. If
5831 BFD_HOST_U_64_BIT is set we know about such a type. */
5832 #ifdef BFD_HOST_U_64_BIT
5837 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5838 bfd *dynobj = elf_hash_table (info)->dynobj;
5839 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5840 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5841 unsigned long int *counts;
5843 unsigned int no_improvement_count = 0;
5845 /* Possible optimization parameters: if we have NSYMS symbols we say
5846 that the hashing table must at least have NSYMS/4 and at most
5848 minsize = nsyms / 4;
5851 best_size = maxsize = nsyms * 2;
5856 if ((best_size & 31) == 0)
5860 /* Create array where we count the collisions in. We must use bfd_malloc
5861 since the size could be large. */
5863 amt *= sizeof (unsigned long int);
5864 counts = (unsigned long int *) bfd_malloc (amt);
5868 /* Compute the "optimal" size for the hash table. The criteria is a
5869 minimal chain length. The minor criteria is (of course) the size
5871 for (i = minsize; i < maxsize; ++i)
5873 /* Walk through the array of hashcodes and count the collisions. */
5874 BFD_HOST_U_64_BIT max;
5875 unsigned long int j;
5876 unsigned long int fact;
5878 if (gnu_hash && (i & 31) == 0)
5881 memset (counts, '\0', i * sizeof (unsigned long int));
5883 /* Determine how often each hash bucket is used. */
5884 for (j = 0; j < nsyms; ++j)
5885 ++counts[hashcodes[j] % i];
5887 /* For the weight function we need some information about the
5888 pagesize on the target. This is information need not be 100%
5889 accurate. Since this information is not available (so far) we
5890 define it here to a reasonable default value. If it is crucial
5891 to have a better value some day simply define this value. */
5892 # ifndef BFD_TARGET_PAGESIZE
5893 # define BFD_TARGET_PAGESIZE (4096)
5896 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5898 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5901 /* Variant 1: optimize for short chains. We add the squares
5902 of all the chain lengths (which favors many small chain
5903 over a few long chains). */
5904 for (j = 0; j < i; ++j)
5905 max += counts[j] * counts[j];
5907 /* This adds penalties for the overall size of the table. */
5908 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5911 /* Variant 2: Optimize a lot more for small table. Here we
5912 also add squares of the size but we also add penalties for
5913 empty slots (the +1 term). */
5914 for (j = 0; j < i; ++j)
5915 max += (1 + counts[j]) * (1 + counts[j]);
5917 /* The overall size of the table is considered, but not as
5918 strong as in variant 1, where it is squared. */
5919 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5923 /* Compare with current best results. */
5924 if (max < best_chlen)
5928 no_improvement_count = 0;
5930 /* PR 11843: Avoid futile long searches for the best bucket size
5931 when there are a large number of symbols. */
5932 else if (++no_improvement_count == 100)
5939 #endif /* defined (BFD_HOST_U_64_BIT) */
5941 /* This is the fallback solution if no 64bit type is available or if we
5942 are not supposed to spend much time on optimizations. We select the
5943 bucket count using a fixed set of numbers. */
5944 for (i = 0; elf_buckets[i] != 0; i++)
5946 best_size = elf_buckets[i];
5947 if (nsyms < elf_buckets[i + 1])
5950 if (gnu_hash && best_size < 2)
5957 /* Size any SHT_GROUP section for ld -r. */
5960 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5965 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5966 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5967 && (s = ibfd->sections) != NULL
5968 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5969 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5974 /* Set a default stack segment size. The value in INFO wins. If it
5975 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5976 undefined it is initialized. */
5979 bfd_elf_stack_segment_size (bfd *output_bfd,
5980 struct bfd_link_info *info,
5981 const char *legacy_symbol,
5982 bfd_vma default_size)
5984 struct elf_link_hash_entry *h = NULL;
5986 /* Look for legacy symbol. */
5988 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5989 FALSE, FALSE, FALSE);
5990 if (h && (h->root.type == bfd_link_hash_defined
5991 || h->root.type == bfd_link_hash_defweak)
5993 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5995 /* The symbol has no type if specified on the command line. */
5996 h->type = STT_OBJECT;
5997 if (info->stacksize)
5998 /* xgettext:c-format */
5999 _bfd_error_handler (_("%B: stack size specified and %s set"),
6000 output_bfd, legacy_symbol);
6001 else if (h->root.u.def.section != bfd_abs_section_ptr)
6002 /* xgettext:c-format */
6003 _bfd_error_handler (_("%B: %s not absolute"),
6004 output_bfd, legacy_symbol);
6006 info->stacksize = h->root.u.def.value;
6009 if (!info->stacksize)
6010 /* If the user didn't set a size, or explicitly inhibit the
6011 size, set it now. */
6012 info->stacksize = default_size;
6014 /* Provide the legacy symbol, if it is referenced. */
6015 if (h && (h->root.type == bfd_link_hash_undefined
6016 || h->root.type == bfd_link_hash_undefweak))
6018 struct bfd_link_hash_entry *bh = NULL;
6020 if (!(_bfd_generic_link_add_one_symbol
6021 (info, output_bfd, legacy_symbol,
6022 BSF_GLOBAL, bfd_abs_section_ptr,
6023 info->stacksize >= 0 ? info->stacksize : 0,
6024 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6027 h = (struct elf_link_hash_entry *) bh;
6029 h->type = STT_OBJECT;
6035 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6037 struct elf_gc_sweep_symbol_info
6039 struct bfd_link_info *info;
6040 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6045 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6048 && (((h->root.type == bfd_link_hash_defined
6049 || h->root.type == bfd_link_hash_defweak)
6050 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6051 && h->root.u.def.section->gc_mark))
6052 || h->root.type == bfd_link_hash_undefined
6053 || h->root.type == bfd_link_hash_undefweak))
6055 struct elf_gc_sweep_symbol_info *inf;
6057 inf = (struct elf_gc_sweep_symbol_info *) data;
6058 (*inf->hide_symbol) (inf->info, h, TRUE);
6061 h->ref_regular_nonweak = 0;
6067 /* Set up the sizes and contents of the ELF dynamic sections. This is
6068 called by the ELF linker emulation before_allocation routine. We
6069 must set the sizes of the sections before the linker sets the
6070 addresses of the various sections. */
6073 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6076 const char *filter_shlib,
6078 const char *depaudit,
6079 const char * const *auxiliary_filters,
6080 struct bfd_link_info *info,
6081 asection **sinterpptr)
6084 const struct elf_backend_data *bed;
6088 if (!is_elf_hash_table (info->hash))
6091 dynobj = elf_hash_table (info)->dynobj;
6093 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6095 struct bfd_elf_version_tree *verdefs;
6096 struct elf_info_failed asvinfo;
6097 struct bfd_elf_version_tree *t;
6098 struct bfd_elf_version_expr *d;
6102 /* If we are supposed to export all symbols into the dynamic symbol
6103 table (this is not the normal case), then do so. */
6104 if (info->export_dynamic
6105 || (bfd_link_executable (info) && info->dynamic))
6107 struct elf_info_failed eif;
6111 elf_link_hash_traverse (elf_hash_table (info),
6112 _bfd_elf_export_symbol,
6120 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6122 if (soname_indx == (size_t) -1
6123 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6127 soname_indx = (size_t) -1;
6129 /* Make all global versions with definition. */
6130 for (t = info->version_info; t != NULL; t = t->next)
6131 for (d = t->globals.list; d != NULL; d = d->next)
6132 if (!d->symver && d->literal)
6134 const char *verstr, *name;
6135 size_t namelen, verlen, newlen;
6136 char *newname, *p, leading_char;
6137 struct elf_link_hash_entry *newh;
6139 leading_char = bfd_get_symbol_leading_char (output_bfd);
6141 namelen = strlen (name) + (leading_char != '\0');
6143 verlen = strlen (verstr);
6144 newlen = namelen + verlen + 3;
6146 newname = (char *) bfd_malloc (newlen);
6147 if (newname == NULL)
6149 newname[0] = leading_char;
6150 memcpy (newname + (leading_char != '\0'), name, namelen);
6152 /* Check the hidden versioned definition. */
6153 p = newname + namelen;
6155 memcpy (p, verstr, verlen + 1);
6156 newh = elf_link_hash_lookup (elf_hash_table (info),
6157 newname, FALSE, FALSE,
6160 || (newh->root.type != bfd_link_hash_defined
6161 && newh->root.type != bfd_link_hash_defweak))
6163 /* Check the default versioned definition. */
6165 memcpy (p, verstr, verlen + 1);
6166 newh = elf_link_hash_lookup (elf_hash_table (info),
6167 newname, FALSE, FALSE,
6172 /* Mark this version if there is a definition and it is
6173 not defined in a shared object. */
6175 && !newh->def_dynamic
6176 && (newh->root.type == bfd_link_hash_defined
6177 || newh->root.type == bfd_link_hash_defweak))
6181 /* Attach all the symbols to their version information. */
6182 asvinfo.info = info;
6183 asvinfo.failed = FALSE;
6185 elf_link_hash_traverse (elf_hash_table (info),
6186 _bfd_elf_link_assign_sym_version,
6191 if (!info->allow_undefined_version)
6193 /* Check if all global versions have a definition. */
6194 bfd_boolean all_defined = TRUE;
6195 for (t = info->version_info; t != NULL; t = t->next)
6196 for (d = t->globals.list; d != NULL; d = d->next)
6197 if (d->literal && !d->symver && !d->script)
6200 (_("%s: undefined version: %s"),
6201 d->pattern, t->name);
6202 all_defined = FALSE;
6207 bfd_set_error (bfd_error_bad_value);
6212 /* Set up the version definition section. */
6213 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6214 BFD_ASSERT (s != NULL);
6216 /* We may have created additional version definitions if we are
6217 just linking a regular application. */
6218 verdefs = info->version_info;
6220 /* Skip anonymous version tag. */
6221 if (verdefs != NULL && verdefs->vernum == 0)
6222 verdefs = verdefs->next;
6224 if (verdefs == NULL && !info->create_default_symver)
6225 s->flags |= SEC_EXCLUDE;
6231 Elf_Internal_Verdef def;
6232 Elf_Internal_Verdaux defaux;
6233 struct bfd_link_hash_entry *bh;
6234 struct elf_link_hash_entry *h;
6240 /* Make space for the base version. */
6241 size += sizeof (Elf_External_Verdef);
6242 size += sizeof (Elf_External_Verdaux);
6245 /* Make space for the default version. */
6246 if (info->create_default_symver)
6248 size += sizeof (Elf_External_Verdef);
6252 for (t = verdefs; t != NULL; t = t->next)
6254 struct bfd_elf_version_deps *n;
6256 /* Don't emit base version twice. */
6260 size += sizeof (Elf_External_Verdef);
6261 size += sizeof (Elf_External_Verdaux);
6264 for (n = t->deps; n != NULL; n = n->next)
6265 size += sizeof (Elf_External_Verdaux);
6269 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6270 if (s->contents == NULL && s->size != 0)
6273 /* Fill in the version definition section. */
6277 def.vd_version = VER_DEF_CURRENT;
6278 def.vd_flags = VER_FLG_BASE;
6281 if (info->create_default_symver)
6283 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6284 def.vd_next = sizeof (Elf_External_Verdef);
6288 def.vd_aux = sizeof (Elf_External_Verdef);
6289 def.vd_next = (sizeof (Elf_External_Verdef)
6290 + sizeof (Elf_External_Verdaux));
6293 if (soname_indx != (size_t) -1)
6295 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6297 def.vd_hash = bfd_elf_hash (soname);
6298 defaux.vda_name = soname_indx;
6305 name = lbasename (output_bfd->filename);
6306 def.vd_hash = bfd_elf_hash (name);
6307 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6309 if (indx == (size_t) -1)
6311 defaux.vda_name = indx;
6313 defaux.vda_next = 0;
6315 _bfd_elf_swap_verdef_out (output_bfd, &def,
6316 (Elf_External_Verdef *) p);
6317 p += sizeof (Elf_External_Verdef);
6318 if (info->create_default_symver)
6320 /* Add a symbol representing this version. */
6322 if (! (_bfd_generic_link_add_one_symbol
6323 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6325 get_elf_backend_data (dynobj)->collect, &bh)))
6327 h = (struct elf_link_hash_entry *) bh;
6330 h->type = STT_OBJECT;
6331 h->verinfo.vertree = NULL;
6333 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6336 /* Create a duplicate of the base version with the same
6337 aux block, but different flags. */
6340 def.vd_aux = sizeof (Elf_External_Verdef);
6342 def.vd_next = (sizeof (Elf_External_Verdef)
6343 + sizeof (Elf_External_Verdaux));
6346 _bfd_elf_swap_verdef_out (output_bfd, &def,
6347 (Elf_External_Verdef *) p);
6348 p += sizeof (Elf_External_Verdef);
6350 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6351 (Elf_External_Verdaux *) p);
6352 p += sizeof (Elf_External_Verdaux);
6354 for (t = verdefs; t != NULL; t = t->next)
6357 struct bfd_elf_version_deps *n;
6359 /* Don't emit the base version twice. */
6364 for (n = t->deps; n != NULL; n = n->next)
6367 /* Add a symbol representing this version. */
6369 if (! (_bfd_generic_link_add_one_symbol
6370 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6372 get_elf_backend_data (dynobj)->collect, &bh)))
6374 h = (struct elf_link_hash_entry *) bh;
6377 h->type = STT_OBJECT;
6378 h->verinfo.vertree = t;
6380 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6383 def.vd_version = VER_DEF_CURRENT;
6385 if (t->globals.list == NULL
6386 && t->locals.list == NULL
6388 def.vd_flags |= VER_FLG_WEAK;
6389 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6390 def.vd_cnt = cdeps + 1;
6391 def.vd_hash = bfd_elf_hash (t->name);
6392 def.vd_aux = sizeof (Elf_External_Verdef);
6395 /* If a basever node is next, it *must* be the last node in
6396 the chain, otherwise Verdef construction breaks. */
6397 if (t->next != NULL && t->next->vernum == 0)
6398 BFD_ASSERT (t->next->next == NULL);
6400 if (t->next != NULL && t->next->vernum != 0)
6401 def.vd_next = (sizeof (Elf_External_Verdef)
6402 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6404 _bfd_elf_swap_verdef_out (output_bfd, &def,
6405 (Elf_External_Verdef *) p);
6406 p += sizeof (Elf_External_Verdef);
6408 defaux.vda_name = h->dynstr_index;
6409 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6411 defaux.vda_next = 0;
6412 if (t->deps != NULL)
6413 defaux.vda_next = sizeof (Elf_External_Verdaux);
6414 t->name_indx = defaux.vda_name;
6416 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6417 (Elf_External_Verdaux *) p);
6418 p += sizeof (Elf_External_Verdaux);
6420 for (n = t->deps; n != NULL; n = n->next)
6422 if (n->version_needed == NULL)
6424 /* This can happen if there was an error in the
6426 defaux.vda_name = 0;
6430 defaux.vda_name = n->version_needed->name_indx;
6431 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6434 if (n->next == NULL)
6435 defaux.vda_next = 0;
6437 defaux.vda_next = sizeof (Elf_External_Verdaux);
6439 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6440 (Elf_External_Verdaux *) p);
6441 p += sizeof (Elf_External_Verdaux);
6445 elf_tdata (output_bfd)->cverdefs = cdefs;
6449 bed = get_elf_backend_data (output_bfd);
6451 if (info->gc_sections && bed->can_gc_sections)
6453 struct elf_gc_sweep_symbol_info sweep_info;
6455 /* Remove the symbols that were in the swept sections from the
6456 dynamic symbol table. */
6457 sweep_info.info = info;
6458 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6459 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6463 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6466 struct elf_find_verdep_info sinfo;
6468 /* Work out the size of the version reference section. */
6470 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6471 BFD_ASSERT (s != NULL);
6474 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6475 if (sinfo.vers == 0)
6477 sinfo.failed = FALSE;
6479 elf_link_hash_traverse (elf_hash_table (info),
6480 _bfd_elf_link_find_version_dependencies,
6485 if (elf_tdata (output_bfd)->verref == NULL)
6486 s->flags |= SEC_EXCLUDE;
6489 Elf_Internal_Verneed *vn;
6494 /* Build the version dependency section. */
6497 for (vn = elf_tdata (output_bfd)->verref;
6499 vn = vn->vn_nextref)
6501 Elf_Internal_Vernaux *a;
6503 size += sizeof (Elf_External_Verneed);
6505 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6506 size += sizeof (Elf_External_Vernaux);
6510 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6511 if (s->contents == NULL)
6515 for (vn = elf_tdata (output_bfd)->verref;
6517 vn = vn->vn_nextref)
6520 Elf_Internal_Vernaux *a;
6524 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6527 vn->vn_version = VER_NEED_CURRENT;
6529 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6530 elf_dt_name (vn->vn_bfd) != NULL
6531 ? elf_dt_name (vn->vn_bfd)
6532 : lbasename (vn->vn_bfd->filename),
6534 if (indx == (size_t) -1)
6537 vn->vn_aux = sizeof (Elf_External_Verneed);
6538 if (vn->vn_nextref == NULL)
6541 vn->vn_next = (sizeof (Elf_External_Verneed)
6542 + caux * sizeof (Elf_External_Vernaux));
6544 _bfd_elf_swap_verneed_out (output_bfd, vn,
6545 (Elf_External_Verneed *) p);
6546 p += sizeof (Elf_External_Verneed);
6548 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6550 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6551 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6552 a->vna_nodename, FALSE);
6553 if (indx == (size_t) -1)
6556 if (a->vna_nextptr == NULL)
6559 a->vna_next = sizeof (Elf_External_Vernaux);
6561 _bfd_elf_swap_vernaux_out (output_bfd, a,
6562 (Elf_External_Vernaux *) p);
6563 p += sizeof (Elf_External_Vernaux);
6567 elf_tdata (output_bfd)->cverrefs = crefs;
6571 /* Any syms created from now on start with -1 in
6572 got.refcount/offset and plt.refcount/offset. */
6573 elf_hash_table (info)->init_got_refcount
6574 = elf_hash_table (info)->init_got_offset;
6575 elf_hash_table (info)->init_plt_refcount
6576 = elf_hash_table (info)->init_plt_offset;
6578 if (bfd_link_relocatable (info)
6579 && !_bfd_elf_size_group_sections (info))
6582 /* The backend may have to create some sections regardless of whether
6583 we're dynamic or not. */
6584 if (bed->elf_backend_always_size_sections
6585 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6588 /* Determine any GNU_STACK segment requirements, after the backend
6589 has had a chance to set a default segment size. */
6590 if (info->execstack)
6591 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6592 else if (info->noexecstack)
6593 elf_stack_flags (output_bfd) = PF_R | PF_W;
6597 asection *notesec = NULL;
6600 for (inputobj = info->input_bfds;
6602 inputobj = inputobj->link.next)
6607 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6609 s = inputobj->sections;
6610 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6613 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6616 if (s->flags & SEC_CODE)
6620 else if (bed->default_execstack)
6623 if (notesec || info->stacksize > 0)
6624 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6625 if (notesec && exec && bfd_link_relocatable (info)
6626 && notesec->output_section != bfd_abs_section_ptr)
6627 notesec->output_section->flags |= SEC_CODE;
6630 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6632 struct elf_info_failed eif;
6633 struct elf_link_hash_entry *h;
6637 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6638 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6642 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6644 info->flags |= DF_SYMBOLIC;
6652 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6654 if (indx == (size_t) -1)
6657 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6658 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6662 if (filter_shlib != NULL)
6666 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6667 filter_shlib, TRUE);
6668 if (indx == (size_t) -1
6669 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6673 if (auxiliary_filters != NULL)
6675 const char * const *p;
6677 for (p = auxiliary_filters; *p != NULL; p++)
6681 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6683 if (indx == (size_t) -1
6684 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6693 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6695 if (indx == (size_t) -1
6696 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6700 if (depaudit != NULL)
6704 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6706 if (indx == (size_t) -1
6707 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6714 /* Find all symbols which were defined in a dynamic object and make
6715 the backend pick a reasonable value for them. */
6716 elf_link_hash_traverse (elf_hash_table (info),
6717 _bfd_elf_adjust_dynamic_symbol,
6722 /* Add some entries to the .dynamic section. We fill in some of the
6723 values later, in bfd_elf_final_link, but we must add the entries
6724 now so that we know the final size of the .dynamic section. */
6726 /* If there are initialization and/or finalization functions to
6727 call then add the corresponding DT_INIT/DT_FINI entries. */
6728 h = (info->init_function
6729 ? elf_link_hash_lookup (elf_hash_table (info),
6730 info->init_function, FALSE,
6737 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6740 h = (info->fini_function
6741 ? elf_link_hash_lookup (elf_hash_table (info),
6742 info->fini_function, FALSE,
6749 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6753 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6754 if (s != NULL && s->linker_has_input)
6756 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6757 if (! bfd_link_executable (info))
6762 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6763 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6764 && (o = sub->sections) != NULL
6765 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6766 for (o = sub->sections; o != NULL; o = o->next)
6767 if (elf_section_data (o)->this_hdr.sh_type
6768 == SHT_PREINIT_ARRAY)
6771 (_("%B: .preinit_array section is not allowed in DSO"),
6776 bfd_set_error (bfd_error_nonrepresentable_section);
6780 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6781 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6784 s = bfd_get_section_by_name (output_bfd, ".init_array");
6785 if (s != NULL && s->linker_has_input)
6787 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6788 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6791 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6792 if (s != NULL && s->linker_has_input)
6794 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6795 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6799 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6800 /* If .dynstr is excluded from the link, we don't want any of
6801 these tags. Strictly, we should be checking each section
6802 individually; This quick check covers for the case where
6803 someone does a /DISCARD/ : { *(*) }. */
6804 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6806 bfd_size_type strsize;
6808 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6809 if ((info->emit_hash
6810 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6811 || (info->emit_gnu_hash
6812 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6813 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6814 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6815 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6816 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6817 bed->s->sizeof_sym))
6822 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6825 /* The backend must work out the sizes of all the other dynamic
6828 && bed->elf_backend_size_dynamic_sections != NULL
6829 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6832 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6834 unsigned long section_sym_count;
6836 if (elf_tdata (output_bfd)->cverdefs)
6838 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6840 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6841 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6845 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6847 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6850 else if (info->flags & DF_BIND_NOW)
6852 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6858 if (bfd_link_executable (info))
6859 info->flags_1 &= ~ (DF_1_INITFIRST
6862 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6866 if (elf_tdata (output_bfd)->cverrefs)
6868 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6870 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6871 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6875 if ((elf_tdata (output_bfd)->cverrefs == 0
6876 && elf_tdata (output_bfd)->cverdefs == 0)
6877 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6878 §ion_sym_count) <= 1)
6882 s = bfd_get_linker_section (dynobj, ".gnu.version");
6883 s->flags |= SEC_EXCLUDE;
6889 /* Find the first non-excluded output section. We'll use its
6890 section symbol for some emitted relocs. */
6892 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6896 for (s = output_bfd->sections; s != NULL; s = s->next)
6897 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6898 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6900 elf_hash_table (info)->text_index_section = s;
6905 /* Find two non-excluded output sections, one for code, one for data.
6906 We'll use their section symbols for some emitted relocs. */
6908 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6912 /* Data first, since setting text_index_section changes
6913 _bfd_elf_link_omit_section_dynsym. */
6914 for (s = output_bfd->sections; s != NULL; s = s->next)
6915 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6916 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6918 elf_hash_table (info)->data_index_section = s;
6922 for (s = output_bfd->sections; s != NULL; s = s->next)
6923 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6924 == (SEC_ALLOC | SEC_READONLY))
6925 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6927 elf_hash_table (info)->text_index_section = s;
6931 if (elf_hash_table (info)->text_index_section == NULL)
6932 elf_hash_table (info)->text_index_section
6933 = elf_hash_table (info)->data_index_section;
6937 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6939 const struct elf_backend_data *bed;
6940 unsigned long section_sym_count;
6941 bfd_size_type dynsymcount = 0;
6943 if (!is_elf_hash_table (info->hash))
6946 bed = get_elf_backend_data (output_bfd);
6947 (*bed->elf_backend_init_index_section) (output_bfd, info);
6949 /* Assign dynsym indices. In a shared library we generate a section
6950 symbol for each output section, which come first. Next come all
6951 of the back-end allocated local dynamic syms, followed by the rest
6952 of the global symbols.
6954 This is usually not needed for static binaries, however backends
6955 can request to always do it, e.g. the MIPS backend uses dynamic
6956 symbol counts to lay out GOT, which will be produced in the
6957 presence of GOT relocations even in static binaries (holding fixed
6958 data in that case, to satisfy those relocations). */
6960 if (elf_hash_table (info)->dynamic_sections_created
6961 || bed->always_renumber_dynsyms)
6962 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6963 §ion_sym_count);
6965 if (elf_hash_table (info)->dynamic_sections_created)
6969 unsigned int dtagcount;
6971 dynobj = elf_hash_table (info)->dynobj;
6973 /* Work out the size of the symbol version section. */
6974 s = bfd_get_linker_section (dynobj, ".gnu.version");
6975 BFD_ASSERT (s != NULL);
6976 if ((s->flags & SEC_EXCLUDE) == 0)
6978 s->size = dynsymcount * sizeof (Elf_External_Versym);
6979 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6980 if (s->contents == NULL)
6983 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6987 /* Set the size of the .dynsym and .hash sections. We counted
6988 the number of dynamic symbols in elf_link_add_object_symbols.
6989 We will build the contents of .dynsym and .hash when we build
6990 the final symbol table, because until then we do not know the
6991 correct value to give the symbols. We built the .dynstr
6992 section as we went along in elf_link_add_object_symbols. */
6993 s = elf_hash_table (info)->dynsym;
6994 BFD_ASSERT (s != NULL);
6995 s->size = dynsymcount * bed->s->sizeof_sym;
6997 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6998 if (s->contents == NULL)
7001 /* The first entry in .dynsym is a dummy symbol. Clear all the
7002 section syms, in case we don't output them all. */
7003 ++section_sym_count;
7004 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7006 elf_hash_table (info)->bucketcount = 0;
7008 /* Compute the size of the hashing table. As a side effect this
7009 computes the hash values for all the names we export. */
7010 if (info->emit_hash)
7012 unsigned long int *hashcodes;
7013 struct hash_codes_info hashinf;
7015 unsigned long int nsyms;
7017 size_t hash_entry_size;
7019 /* Compute the hash values for all exported symbols. At the same
7020 time store the values in an array so that we could use them for
7022 amt = dynsymcount * sizeof (unsigned long int);
7023 hashcodes = (unsigned long int *) bfd_malloc (amt);
7024 if (hashcodes == NULL)
7026 hashinf.hashcodes = hashcodes;
7027 hashinf.error = FALSE;
7029 /* Put all hash values in HASHCODES. */
7030 elf_link_hash_traverse (elf_hash_table (info),
7031 elf_collect_hash_codes, &hashinf);
7038 nsyms = hashinf.hashcodes - hashcodes;
7040 = compute_bucket_count (info, hashcodes, nsyms, 0);
7043 if (bucketcount == 0 && nsyms > 0)
7046 elf_hash_table (info)->bucketcount = bucketcount;
7048 s = bfd_get_linker_section (dynobj, ".hash");
7049 BFD_ASSERT (s != NULL);
7050 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7051 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7052 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7053 if (s->contents == NULL)
7056 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7057 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7058 s->contents + hash_entry_size);
7061 if (info->emit_gnu_hash)
7064 unsigned char *contents;
7065 struct collect_gnu_hash_codes cinfo;
7069 memset (&cinfo, 0, sizeof (cinfo));
7071 /* Compute the hash values for all exported symbols. At the same
7072 time store the values in an array so that we could use them for
7074 amt = dynsymcount * 2 * sizeof (unsigned long int);
7075 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7076 if (cinfo.hashcodes == NULL)
7079 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7080 cinfo.min_dynindx = -1;
7081 cinfo.output_bfd = output_bfd;
7084 /* Put all hash values in HASHCODES. */
7085 elf_link_hash_traverse (elf_hash_table (info),
7086 elf_collect_gnu_hash_codes, &cinfo);
7089 free (cinfo.hashcodes);
7094 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7096 if (bucketcount == 0)
7098 free (cinfo.hashcodes);
7102 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7103 BFD_ASSERT (s != NULL);
7105 if (cinfo.nsyms == 0)
7107 /* Empty .gnu.hash section is special. */
7108 BFD_ASSERT (cinfo.min_dynindx == -1);
7109 free (cinfo.hashcodes);
7110 s->size = 5 * 4 + bed->s->arch_size / 8;
7111 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7112 if (contents == NULL)
7114 s->contents = contents;
7115 /* 1 empty bucket. */
7116 bfd_put_32 (output_bfd, 1, contents);
7117 /* SYMIDX above the special symbol 0. */
7118 bfd_put_32 (output_bfd, 1, contents + 4);
7119 /* Just one word for bitmask. */
7120 bfd_put_32 (output_bfd, 1, contents + 8);
7121 /* Only hash fn bloom filter. */
7122 bfd_put_32 (output_bfd, 0, contents + 12);
7123 /* No hashes are valid - empty bitmask. */
7124 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7125 /* No hashes in the only bucket. */
7126 bfd_put_32 (output_bfd, 0,
7127 contents + 16 + bed->s->arch_size / 8);
7131 unsigned long int maskwords, maskbitslog2, x;
7132 BFD_ASSERT (cinfo.min_dynindx != -1);
7136 while ((x >>= 1) != 0)
7138 if (maskbitslog2 < 3)
7140 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7141 maskbitslog2 = maskbitslog2 + 3;
7143 maskbitslog2 = maskbitslog2 + 2;
7144 if (bed->s->arch_size == 64)
7146 if (maskbitslog2 == 5)
7152 cinfo.mask = (1 << cinfo.shift1) - 1;
7153 cinfo.shift2 = maskbitslog2;
7154 cinfo.maskbits = 1 << maskbitslog2;
7155 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7156 amt = bucketcount * sizeof (unsigned long int) * 2;
7157 amt += maskwords * sizeof (bfd_vma);
7158 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7159 if (cinfo.bitmask == NULL)
7161 free (cinfo.hashcodes);
7165 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7166 cinfo.indx = cinfo.counts + bucketcount;
7167 cinfo.symindx = dynsymcount - cinfo.nsyms;
7168 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7170 /* Determine how often each hash bucket is used. */
7171 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7172 for (i = 0; i < cinfo.nsyms; ++i)
7173 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7175 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7176 if (cinfo.counts[i] != 0)
7178 cinfo.indx[i] = cnt;
7179 cnt += cinfo.counts[i];
7181 BFD_ASSERT (cnt == dynsymcount);
7182 cinfo.bucketcount = bucketcount;
7183 cinfo.local_indx = cinfo.min_dynindx;
7185 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7186 s->size += cinfo.maskbits / 8;
7187 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7188 if (contents == NULL)
7190 free (cinfo.bitmask);
7191 free (cinfo.hashcodes);
7195 s->contents = contents;
7196 bfd_put_32 (output_bfd, bucketcount, contents);
7197 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7198 bfd_put_32 (output_bfd, maskwords, contents + 8);
7199 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7200 contents += 16 + cinfo.maskbits / 8;
7202 for (i = 0; i < bucketcount; ++i)
7204 if (cinfo.counts[i] == 0)
7205 bfd_put_32 (output_bfd, 0, contents);
7207 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7211 cinfo.contents = contents;
7213 /* Renumber dynamic symbols, populate .gnu.hash section. */
7214 elf_link_hash_traverse (elf_hash_table (info),
7215 elf_renumber_gnu_hash_syms, &cinfo);
7217 contents = s->contents + 16;
7218 for (i = 0; i < maskwords; ++i)
7220 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7222 contents += bed->s->arch_size / 8;
7225 free (cinfo.bitmask);
7226 free (cinfo.hashcodes);
7230 s = bfd_get_linker_section (dynobj, ".dynstr");
7231 BFD_ASSERT (s != NULL);
7233 elf_finalize_dynstr (output_bfd, info);
7235 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7237 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7238 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7245 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7248 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7251 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7252 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7255 /* Finish SHF_MERGE section merging. */
7258 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7263 if (!is_elf_hash_table (info->hash))
7266 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7267 if ((ibfd->flags & DYNAMIC) == 0
7268 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7269 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7270 == get_elf_backend_data (obfd)->s->elfclass))
7271 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7272 if ((sec->flags & SEC_MERGE) != 0
7273 && !bfd_is_abs_section (sec->output_section))
7275 struct bfd_elf_section_data *secdata;
7277 secdata = elf_section_data (sec);
7278 if (! _bfd_add_merge_section (obfd,
7279 &elf_hash_table (info)->merge_info,
7280 sec, &secdata->sec_info))
7282 else if (secdata->sec_info)
7283 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7286 if (elf_hash_table (info)->merge_info != NULL)
7287 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7288 merge_sections_remove_hook);
7292 /* Create an entry in an ELF linker hash table. */
7294 struct bfd_hash_entry *
7295 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7296 struct bfd_hash_table *table,
7299 /* Allocate the structure if it has not already been allocated by a
7303 entry = (struct bfd_hash_entry *)
7304 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7309 /* Call the allocation method of the superclass. */
7310 entry = _bfd_link_hash_newfunc (entry, table, string);
7313 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7314 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7316 /* Set local fields. */
7319 ret->got = htab->init_got_refcount;
7320 ret->plt = htab->init_plt_refcount;
7321 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7322 - offsetof (struct elf_link_hash_entry, size)));
7323 /* Assume that we have been called by a non-ELF symbol reader.
7324 This flag is then reset by the code which reads an ELF input
7325 file. This ensures that a symbol created by a non-ELF symbol
7326 reader will have the flag set correctly. */
7333 /* Copy data from an indirect symbol to its direct symbol, hiding the
7334 old indirect symbol. Also used for copying flags to a weakdef. */
7337 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7338 struct elf_link_hash_entry *dir,
7339 struct elf_link_hash_entry *ind)
7341 struct elf_link_hash_table *htab;
7343 /* Copy down any references that we may have already seen to the
7344 symbol which just became indirect. */
7346 if (dir->versioned != versioned_hidden)
7347 dir->ref_dynamic |= ind->ref_dynamic;
7348 dir->ref_regular |= ind->ref_regular;
7349 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7350 dir->non_got_ref |= ind->non_got_ref;
7351 dir->needs_plt |= ind->needs_plt;
7352 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7354 if (ind->root.type != bfd_link_hash_indirect)
7357 /* Copy over the global and procedure linkage table refcount entries.
7358 These may have been already set up by a check_relocs routine. */
7359 htab = elf_hash_table (info);
7360 if (ind->got.refcount > htab->init_got_refcount.refcount)
7362 if (dir->got.refcount < 0)
7363 dir->got.refcount = 0;
7364 dir->got.refcount += ind->got.refcount;
7365 ind->got.refcount = htab->init_got_refcount.refcount;
7368 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7370 if (dir->plt.refcount < 0)
7371 dir->plt.refcount = 0;
7372 dir->plt.refcount += ind->plt.refcount;
7373 ind->plt.refcount = htab->init_plt_refcount.refcount;
7376 if (ind->dynindx != -1)
7378 if (dir->dynindx != -1)
7379 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7380 dir->dynindx = ind->dynindx;
7381 dir->dynstr_index = ind->dynstr_index;
7383 ind->dynstr_index = 0;
7388 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7389 struct elf_link_hash_entry *h,
7390 bfd_boolean force_local)
7392 /* STT_GNU_IFUNC symbol must go through PLT. */
7393 if (h->type != STT_GNU_IFUNC)
7395 h->plt = elf_hash_table (info)->init_plt_offset;
7400 h->forced_local = 1;
7401 if (h->dynindx != -1)
7403 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7406 h->dynstr_index = 0;
7411 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7415 _bfd_elf_link_hash_table_init
7416 (struct elf_link_hash_table *table,
7418 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7419 struct bfd_hash_table *,
7421 unsigned int entsize,
7422 enum elf_target_id target_id)
7425 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7427 table->init_got_refcount.refcount = can_refcount - 1;
7428 table->init_plt_refcount.refcount = can_refcount - 1;
7429 table->init_got_offset.offset = -(bfd_vma) 1;
7430 table->init_plt_offset.offset = -(bfd_vma) 1;
7431 /* The first dynamic symbol is a dummy. */
7432 table->dynsymcount = 1;
7434 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7436 table->root.type = bfd_link_elf_hash_table;
7437 table->hash_table_id = target_id;
7442 /* Create an ELF linker hash table. */
7444 struct bfd_link_hash_table *
7445 _bfd_elf_link_hash_table_create (bfd *abfd)
7447 struct elf_link_hash_table *ret;
7448 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7450 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7454 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7455 sizeof (struct elf_link_hash_entry),
7461 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7466 /* Destroy an ELF linker hash table. */
7469 _bfd_elf_link_hash_table_free (bfd *obfd)
7471 struct elf_link_hash_table *htab;
7473 htab = (struct elf_link_hash_table *) obfd->link.hash;
7474 if (htab->dynstr != NULL)
7475 _bfd_elf_strtab_free (htab->dynstr);
7476 _bfd_merge_sections_free (htab->merge_info);
7477 _bfd_generic_link_hash_table_free (obfd);
7480 /* This is a hook for the ELF emulation code in the generic linker to
7481 tell the backend linker what file name to use for the DT_NEEDED
7482 entry for a dynamic object. */
7485 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7487 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7488 && bfd_get_format (abfd) == bfd_object)
7489 elf_dt_name (abfd) = name;
7493 bfd_elf_get_dyn_lib_class (bfd *abfd)
7496 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7497 && bfd_get_format (abfd) == bfd_object)
7498 lib_class = elf_dyn_lib_class (abfd);
7505 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7507 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7508 && bfd_get_format (abfd) == bfd_object)
7509 elf_dyn_lib_class (abfd) = lib_class;
7512 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7513 the linker ELF emulation code. */
7515 struct bfd_link_needed_list *
7516 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7517 struct bfd_link_info *info)
7519 if (! is_elf_hash_table (info->hash))
7521 return elf_hash_table (info)->needed;
7524 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7525 hook for the linker ELF emulation code. */
7527 struct bfd_link_needed_list *
7528 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7529 struct bfd_link_info *info)
7531 if (! is_elf_hash_table (info->hash))
7533 return elf_hash_table (info)->runpath;
7536 /* Get the name actually used for a dynamic object for a link. This
7537 is the SONAME entry if there is one. Otherwise, it is the string
7538 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7541 bfd_elf_get_dt_soname (bfd *abfd)
7543 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7544 && bfd_get_format (abfd) == bfd_object)
7545 return elf_dt_name (abfd);
7549 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7550 the ELF linker emulation code. */
7553 bfd_elf_get_bfd_needed_list (bfd *abfd,
7554 struct bfd_link_needed_list **pneeded)
7557 bfd_byte *dynbuf = NULL;
7558 unsigned int elfsec;
7559 unsigned long shlink;
7560 bfd_byte *extdyn, *extdynend;
7562 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7566 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7567 || bfd_get_format (abfd) != bfd_object)
7570 s = bfd_get_section_by_name (abfd, ".dynamic");
7571 if (s == NULL || s->size == 0)
7574 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7577 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7578 if (elfsec == SHN_BAD)
7581 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7583 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7584 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7587 extdynend = extdyn + s->size;
7588 for (; extdyn < extdynend; extdyn += extdynsize)
7590 Elf_Internal_Dyn dyn;
7592 (*swap_dyn_in) (abfd, extdyn, &dyn);
7594 if (dyn.d_tag == DT_NULL)
7597 if (dyn.d_tag == DT_NEEDED)
7600 struct bfd_link_needed_list *l;
7601 unsigned int tagv = dyn.d_un.d_val;
7604 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7609 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7630 struct elf_symbuf_symbol
7632 unsigned long st_name; /* Symbol name, index in string tbl */
7633 unsigned char st_info; /* Type and binding attributes */
7634 unsigned char st_other; /* Visibilty, and target specific */
7637 struct elf_symbuf_head
7639 struct elf_symbuf_symbol *ssym;
7641 unsigned int st_shndx;
7648 Elf_Internal_Sym *isym;
7649 struct elf_symbuf_symbol *ssym;
7654 /* Sort references to symbols by ascending section number. */
7657 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7659 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7660 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7662 return s1->st_shndx - s2->st_shndx;
7666 elf_sym_name_compare (const void *arg1, const void *arg2)
7668 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7669 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7670 return strcmp (s1->name, s2->name);
7673 static struct elf_symbuf_head *
7674 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7676 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7677 struct elf_symbuf_symbol *ssym;
7678 struct elf_symbuf_head *ssymbuf, *ssymhead;
7679 size_t i, shndx_count, total_size;
7681 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7685 for (ind = indbuf, i = 0; i < symcount; i++)
7686 if (isymbuf[i].st_shndx != SHN_UNDEF)
7687 *ind++ = &isymbuf[i];
7690 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7691 elf_sort_elf_symbol);
7694 if (indbufend > indbuf)
7695 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7696 if (ind[0]->st_shndx != ind[1]->st_shndx)
7699 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7700 + (indbufend - indbuf) * sizeof (*ssym));
7701 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7702 if (ssymbuf == NULL)
7708 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7709 ssymbuf->ssym = NULL;
7710 ssymbuf->count = shndx_count;
7711 ssymbuf->st_shndx = 0;
7712 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7714 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7717 ssymhead->ssym = ssym;
7718 ssymhead->count = 0;
7719 ssymhead->st_shndx = (*ind)->st_shndx;
7721 ssym->st_name = (*ind)->st_name;
7722 ssym->st_info = (*ind)->st_info;
7723 ssym->st_other = (*ind)->st_other;
7726 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7727 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7734 /* Check if 2 sections define the same set of local and global
7738 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7739 struct bfd_link_info *info)
7742 const struct elf_backend_data *bed1, *bed2;
7743 Elf_Internal_Shdr *hdr1, *hdr2;
7744 size_t symcount1, symcount2;
7745 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7746 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7747 Elf_Internal_Sym *isym, *isymend;
7748 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7749 size_t count1, count2, i;
7750 unsigned int shndx1, shndx2;
7756 /* Both sections have to be in ELF. */
7757 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7758 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7761 if (elf_section_type (sec1) != elf_section_type (sec2))
7764 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7765 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7766 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7769 bed1 = get_elf_backend_data (bfd1);
7770 bed2 = get_elf_backend_data (bfd2);
7771 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7772 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7773 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7774 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7776 if (symcount1 == 0 || symcount2 == 0)
7782 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7783 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7785 if (ssymbuf1 == NULL)
7787 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7789 if (isymbuf1 == NULL)
7792 if (!info->reduce_memory_overheads)
7793 elf_tdata (bfd1)->symbuf = ssymbuf1
7794 = elf_create_symbuf (symcount1, isymbuf1);
7797 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7799 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7801 if (isymbuf2 == NULL)
7804 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7805 elf_tdata (bfd2)->symbuf = ssymbuf2
7806 = elf_create_symbuf (symcount2, isymbuf2);
7809 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7811 /* Optimized faster version. */
7813 struct elf_symbol *symp;
7814 struct elf_symbuf_symbol *ssym, *ssymend;
7817 hi = ssymbuf1->count;
7822 mid = (lo + hi) / 2;
7823 if (shndx1 < ssymbuf1[mid].st_shndx)
7825 else if (shndx1 > ssymbuf1[mid].st_shndx)
7829 count1 = ssymbuf1[mid].count;
7836 hi = ssymbuf2->count;
7841 mid = (lo + hi) / 2;
7842 if (shndx2 < ssymbuf2[mid].st_shndx)
7844 else if (shndx2 > ssymbuf2[mid].st_shndx)
7848 count2 = ssymbuf2[mid].count;
7854 if (count1 == 0 || count2 == 0 || count1 != count2)
7858 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7860 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7861 if (symtable1 == NULL || symtable2 == NULL)
7865 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7866 ssym < ssymend; ssym++, symp++)
7868 symp->u.ssym = ssym;
7869 symp->name = bfd_elf_string_from_elf_section (bfd1,
7875 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7876 ssym < ssymend; ssym++, symp++)
7878 symp->u.ssym = ssym;
7879 symp->name = bfd_elf_string_from_elf_section (bfd2,
7884 /* Sort symbol by name. */
7885 qsort (symtable1, count1, sizeof (struct elf_symbol),
7886 elf_sym_name_compare);
7887 qsort (symtable2, count1, sizeof (struct elf_symbol),
7888 elf_sym_name_compare);
7890 for (i = 0; i < count1; i++)
7891 /* Two symbols must have the same binding, type and name. */
7892 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7893 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7894 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7901 symtable1 = (struct elf_symbol *)
7902 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7903 symtable2 = (struct elf_symbol *)
7904 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7905 if (symtable1 == NULL || symtable2 == NULL)
7908 /* Count definitions in the section. */
7910 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7911 if (isym->st_shndx == shndx1)
7912 symtable1[count1++].u.isym = isym;
7915 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7916 if (isym->st_shndx == shndx2)
7917 symtable2[count2++].u.isym = isym;
7919 if (count1 == 0 || count2 == 0 || count1 != count2)
7922 for (i = 0; i < count1; i++)
7924 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7925 symtable1[i].u.isym->st_name);
7927 for (i = 0; i < count2; i++)
7929 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7930 symtable2[i].u.isym->st_name);
7932 /* Sort symbol by name. */
7933 qsort (symtable1, count1, sizeof (struct elf_symbol),
7934 elf_sym_name_compare);
7935 qsort (symtable2, count1, sizeof (struct elf_symbol),
7936 elf_sym_name_compare);
7938 for (i = 0; i < count1; i++)
7939 /* Two symbols must have the same binding, type and name. */
7940 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7941 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7942 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7960 /* Return TRUE if 2 section types are compatible. */
7963 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7964 bfd *bbfd, const asection *bsec)
7968 || abfd->xvec->flavour != bfd_target_elf_flavour
7969 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7972 return elf_section_type (asec) == elf_section_type (bsec);
7975 /* Final phase of ELF linker. */
7977 /* A structure we use to avoid passing large numbers of arguments. */
7979 struct elf_final_link_info
7981 /* General link information. */
7982 struct bfd_link_info *info;
7985 /* Symbol string table. */
7986 struct elf_strtab_hash *symstrtab;
7987 /* .hash section. */
7989 /* symbol version section (.gnu.version). */
7990 asection *symver_sec;
7991 /* Buffer large enough to hold contents of any section. */
7993 /* Buffer large enough to hold external relocs of any section. */
7994 void *external_relocs;
7995 /* Buffer large enough to hold internal relocs of any section. */
7996 Elf_Internal_Rela *internal_relocs;
7997 /* Buffer large enough to hold external local symbols of any input
7999 bfd_byte *external_syms;
8000 /* And a buffer for symbol section indices. */
8001 Elf_External_Sym_Shndx *locsym_shndx;
8002 /* Buffer large enough to hold internal local symbols of any input
8004 Elf_Internal_Sym *internal_syms;
8005 /* Array large enough to hold a symbol index for each local symbol
8006 of any input BFD. */
8008 /* Array large enough to hold a section pointer for each local
8009 symbol of any input BFD. */
8010 asection **sections;
8011 /* Buffer for SHT_SYMTAB_SHNDX section. */
8012 Elf_External_Sym_Shndx *symshndxbuf;
8013 /* Number of STT_FILE syms seen. */
8014 size_t filesym_count;
8017 /* This struct is used to pass information to elf_link_output_extsym. */
8019 struct elf_outext_info
8022 bfd_boolean localsyms;
8023 bfd_boolean file_sym_done;
8024 struct elf_final_link_info *flinfo;
8028 /* Support for evaluating a complex relocation.
8030 Complex relocations are generalized, self-describing relocations. The
8031 implementation of them consists of two parts: complex symbols, and the
8032 relocations themselves.
8034 The relocations are use a reserved elf-wide relocation type code (R_RELC
8035 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8036 information (start bit, end bit, word width, etc) into the addend. This
8037 information is extracted from CGEN-generated operand tables within gas.
8039 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8040 internal) representing prefix-notation expressions, including but not
8041 limited to those sorts of expressions normally encoded as addends in the
8042 addend field. The symbol mangling format is:
8045 | <unary-operator> ':' <node>
8046 | <binary-operator> ':' <node> ':' <node>
8049 <literal> := 's' <digits=N> ':' <N character symbol name>
8050 | 'S' <digits=N> ':' <N character section name>
8054 <binary-operator> := as in C
8055 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8058 set_symbol_value (bfd *bfd_with_globals,
8059 Elf_Internal_Sym *isymbuf,
8064 struct elf_link_hash_entry **sym_hashes;
8065 struct elf_link_hash_entry *h;
8066 size_t extsymoff = locsymcount;
8068 if (symidx < locsymcount)
8070 Elf_Internal_Sym *sym;
8072 sym = isymbuf + symidx;
8073 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8075 /* It is a local symbol: move it to the
8076 "absolute" section and give it a value. */
8077 sym->st_shndx = SHN_ABS;
8078 sym->st_value = val;
8081 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8085 /* It is a global symbol: set its link type
8086 to "defined" and give it a value. */
8088 sym_hashes = elf_sym_hashes (bfd_with_globals);
8089 h = sym_hashes [symidx - extsymoff];
8090 while (h->root.type == bfd_link_hash_indirect
8091 || h->root.type == bfd_link_hash_warning)
8092 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8093 h->root.type = bfd_link_hash_defined;
8094 h->root.u.def.value = val;
8095 h->root.u.def.section = bfd_abs_section_ptr;
8099 resolve_symbol (const char *name,
8101 struct elf_final_link_info *flinfo,
8103 Elf_Internal_Sym *isymbuf,
8106 Elf_Internal_Sym *sym;
8107 struct bfd_link_hash_entry *global_entry;
8108 const char *candidate = NULL;
8109 Elf_Internal_Shdr *symtab_hdr;
8112 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8114 for (i = 0; i < locsymcount; ++ i)
8118 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8121 candidate = bfd_elf_string_from_elf_section (input_bfd,
8122 symtab_hdr->sh_link,
8125 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8126 name, candidate, (unsigned long) sym->st_value);
8128 if (candidate && strcmp (candidate, name) == 0)
8130 asection *sec = flinfo->sections [i];
8132 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8133 *result += sec->output_offset + sec->output_section->vma;
8135 printf ("Found symbol with value %8.8lx\n",
8136 (unsigned long) *result);
8142 /* Hmm, haven't found it yet. perhaps it is a global. */
8143 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8144 FALSE, FALSE, TRUE);
8148 if (global_entry->type == bfd_link_hash_defined
8149 || global_entry->type == bfd_link_hash_defweak)
8151 *result = (global_entry->u.def.value
8152 + global_entry->u.def.section->output_section->vma
8153 + global_entry->u.def.section->output_offset);
8155 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8156 global_entry->root.string, (unsigned long) *result);
8164 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8165 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8166 names like "foo.end" which is the end address of section "foo". */
8169 resolve_section (const char *name,
8177 for (curr = sections; curr; curr = curr->next)
8178 if (strcmp (curr->name, name) == 0)
8180 *result = curr->vma;
8184 /* Hmm. still haven't found it. try pseudo-section names. */
8185 /* FIXME: This could be coded more efficiently... */
8186 for (curr = sections; curr; curr = curr->next)
8188 len = strlen (curr->name);
8189 if (len > strlen (name))
8192 if (strncmp (curr->name, name, len) == 0)
8194 if (strncmp (".end", name + len, 4) == 0)
8196 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8200 /* Insert more pseudo-section names here, if you like. */
8208 undefined_reference (const char *reftype, const char *name)
8210 /* xgettext:c-format */
8211 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8216 eval_symbol (bfd_vma *result,
8219 struct elf_final_link_info *flinfo,
8221 Elf_Internal_Sym *isymbuf,
8230 const char *sym = *symp;
8232 bfd_boolean symbol_is_section = FALSE;
8237 if (len < 1 || len > sizeof (symbuf))
8239 bfd_set_error (bfd_error_invalid_operation);
8252 *result = strtoul (sym, (char **) symp, 16);
8256 symbol_is_section = TRUE;
8260 symlen = strtol (sym, (char **) symp, 10);
8261 sym = *symp + 1; /* Skip the trailing ':'. */
8263 if (symend < sym || symlen + 1 > sizeof (symbuf))
8265 bfd_set_error (bfd_error_invalid_operation);
8269 memcpy (symbuf, sym, symlen);
8270 symbuf[symlen] = '\0';
8271 *symp = sym + symlen;
8273 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8274 the symbol as a section, or vice-versa. so we're pretty liberal in our
8275 interpretation here; section means "try section first", not "must be a
8276 section", and likewise with symbol. */
8278 if (symbol_is_section)
8280 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8281 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8282 isymbuf, locsymcount))
8284 undefined_reference ("section", symbuf);
8290 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8291 isymbuf, locsymcount)
8292 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8295 undefined_reference ("symbol", symbuf);
8302 /* All that remains are operators. */
8304 #define UNARY_OP(op) \
8305 if (strncmp (sym, #op, strlen (#op)) == 0) \
8307 sym += strlen (#op); \
8311 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8312 isymbuf, locsymcount, signed_p)) \
8315 *result = op ((bfd_signed_vma) a); \
8321 #define BINARY_OP(op) \
8322 if (strncmp (sym, #op, strlen (#op)) == 0) \
8324 sym += strlen (#op); \
8328 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8329 isymbuf, locsymcount, signed_p)) \
8332 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8333 isymbuf, locsymcount, signed_p)) \
8336 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8366 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8367 bfd_set_error (bfd_error_invalid_operation);
8373 put_value (bfd_vma size,
8374 unsigned long chunksz,
8379 location += (size - chunksz);
8381 for (; size; size -= chunksz, location -= chunksz)
8386 bfd_put_8 (input_bfd, x, location);
8390 bfd_put_16 (input_bfd, x, location);
8394 bfd_put_32 (input_bfd, x, location);
8395 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8401 bfd_put_64 (input_bfd, x, location);
8402 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8415 get_value (bfd_vma size,
8416 unsigned long chunksz,
8423 /* Sanity checks. */
8424 BFD_ASSERT (chunksz <= sizeof (x)
8427 && (size % chunksz) == 0
8428 && input_bfd != NULL
8429 && location != NULL);
8431 if (chunksz == sizeof (x))
8433 BFD_ASSERT (size == chunksz);
8435 /* Make sure that we do not perform an undefined shift operation.
8436 We know that size == chunksz so there will only be one iteration
8437 of the loop below. */
8441 shift = 8 * chunksz;
8443 for (; size; size -= chunksz, location += chunksz)
8448 x = (x << shift) | bfd_get_8 (input_bfd, location);
8451 x = (x << shift) | bfd_get_16 (input_bfd, location);
8454 x = (x << shift) | bfd_get_32 (input_bfd, location);
8458 x = (x << shift) | bfd_get_64 (input_bfd, location);
8469 decode_complex_addend (unsigned long *start, /* in bits */
8470 unsigned long *oplen, /* in bits */
8471 unsigned long *len, /* in bits */
8472 unsigned long *wordsz, /* in bytes */
8473 unsigned long *chunksz, /* in bytes */
8474 unsigned long *lsb0_p,
8475 unsigned long *signed_p,
8476 unsigned long *trunc_p,
8477 unsigned long encoded)
8479 * start = encoded & 0x3F;
8480 * len = (encoded >> 6) & 0x3F;
8481 * oplen = (encoded >> 12) & 0x3F;
8482 * wordsz = (encoded >> 18) & 0xF;
8483 * chunksz = (encoded >> 22) & 0xF;
8484 * lsb0_p = (encoded >> 27) & 1;
8485 * signed_p = (encoded >> 28) & 1;
8486 * trunc_p = (encoded >> 29) & 1;
8489 bfd_reloc_status_type
8490 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8491 asection *input_section ATTRIBUTE_UNUSED,
8493 Elf_Internal_Rela *rel,
8496 bfd_vma shift, x, mask;
8497 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8498 bfd_reloc_status_type r;
8500 /* Perform this reloc, since it is complex.
8501 (this is not to say that it necessarily refers to a complex
8502 symbol; merely that it is a self-describing CGEN based reloc.
8503 i.e. the addend has the complete reloc information (bit start, end,
8504 word size, etc) encoded within it.). */
8506 decode_complex_addend (&start, &oplen, &len, &wordsz,
8507 &chunksz, &lsb0_p, &signed_p,
8508 &trunc_p, rel->r_addend);
8510 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8513 shift = (start + 1) - len;
8515 shift = (8 * wordsz) - (start + len);
8517 x = get_value (wordsz, chunksz, input_bfd,
8518 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8521 printf ("Doing complex reloc: "
8522 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8523 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8524 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8525 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8526 oplen, (unsigned long) x, (unsigned long) mask,
8527 (unsigned long) relocation);
8532 /* Now do an overflow check. */
8533 r = bfd_check_overflow ((signed_p
8534 ? complain_overflow_signed
8535 : complain_overflow_unsigned),
8536 len, 0, (8 * wordsz),
8540 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8543 printf (" relocation: %8.8lx\n"
8544 " shifted mask: %8.8lx\n"
8545 " shifted/masked reloc: %8.8lx\n"
8546 " result: %8.8lx\n",
8547 (unsigned long) relocation, (unsigned long) (mask << shift),
8548 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8550 put_value (wordsz, chunksz, input_bfd, x,
8551 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8555 /* Functions to read r_offset from external (target order) reloc
8556 entry. Faster than bfd_getl32 et al, because we let the compiler
8557 know the value is aligned. */
8560 ext32l_r_offset (const void *p)
8567 const union aligned32 *a
8568 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8570 uint32_t aval = ( (uint32_t) a->c[0]
8571 | (uint32_t) a->c[1] << 8
8572 | (uint32_t) a->c[2] << 16
8573 | (uint32_t) a->c[3] << 24);
8578 ext32b_r_offset (const void *p)
8585 const union aligned32 *a
8586 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8588 uint32_t aval = ( (uint32_t) a->c[0] << 24
8589 | (uint32_t) a->c[1] << 16
8590 | (uint32_t) a->c[2] << 8
8591 | (uint32_t) a->c[3]);
8595 #ifdef BFD_HOST_64_BIT
8597 ext64l_r_offset (const void *p)
8604 const union aligned64 *a
8605 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8607 uint64_t aval = ( (uint64_t) a->c[0]
8608 | (uint64_t) a->c[1] << 8
8609 | (uint64_t) a->c[2] << 16
8610 | (uint64_t) a->c[3] << 24
8611 | (uint64_t) a->c[4] << 32
8612 | (uint64_t) a->c[5] << 40
8613 | (uint64_t) a->c[6] << 48
8614 | (uint64_t) a->c[7] << 56);
8619 ext64b_r_offset (const void *p)
8626 const union aligned64 *a
8627 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8629 uint64_t aval = ( (uint64_t) a->c[0] << 56
8630 | (uint64_t) a->c[1] << 48
8631 | (uint64_t) a->c[2] << 40
8632 | (uint64_t) a->c[3] << 32
8633 | (uint64_t) a->c[4] << 24
8634 | (uint64_t) a->c[5] << 16
8635 | (uint64_t) a->c[6] << 8
8636 | (uint64_t) a->c[7]);
8641 /* When performing a relocatable link, the input relocations are
8642 preserved. But, if they reference global symbols, the indices
8643 referenced must be updated. Update all the relocations found in
8647 elf_link_adjust_relocs (bfd *abfd,
8649 struct bfd_elf_section_reloc_data *reldata,
8651 struct bfd_link_info *info)
8654 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8656 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8657 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8658 bfd_vma r_type_mask;
8660 unsigned int count = reldata->count;
8661 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8663 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8665 swap_in = bed->s->swap_reloc_in;
8666 swap_out = bed->s->swap_reloc_out;
8668 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8670 swap_in = bed->s->swap_reloca_in;
8671 swap_out = bed->s->swap_reloca_out;
8676 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8679 if (bed->s->arch_size == 32)
8686 r_type_mask = 0xffffffff;
8690 erela = reldata->hdr->contents;
8691 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8693 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8696 if (*rel_hash == NULL)
8699 if ((*rel_hash)->indx == -2
8700 && info->gc_sections
8701 && ! info->gc_keep_exported)
8703 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8704 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8706 (*rel_hash)->root.root.string);
8707 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8709 bfd_set_error (bfd_error_invalid_operation);
8712 BFD_ASSERT ((*rel_hash)->indx >= 0);
8714 (*swap_in) (abfd, erela, irela);
8715 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8716 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8717 | (irela[j].r_info & r_type_mask));
8718 (*swap_out) (abfd, irela, erela);
8721 if (bed->elf_backend_update_relocs)
8722 (*bed->elf_backend_update_relocs) (sec, reldata);
8724 if (sort && count != 0)
8726 bfd_vma (*ext_r_off) (const void *);
8729 bfd_byte *base, *end, *p, *loc;
8730 bfd_byte *buf = NULL;
8732 if (bed->s->arch_size == 32)
8734 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8735 ext_r_off = ext32l_r_offset;
8736 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8737 ext_r_off = ext32b_r_offset;
8743 #ifdef BFD_HOST_64_BIT
8744 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8745 ext_r_off = ext64l_r_offset;
8746 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8747 ext_r_off = ext64b_r_offset;
8753 /* Must use a stable sort here. A modified insertion sort,
8754 since the relocs are mostly sorted already. */
8755 elt_size = reldata->hdr->sh_entsize;
8756 base = reldata->hdr->contents;
8757 end = base + count * elt_size;
8758 if (elt_size > sizeof (Elf64_External_Rela))
8761 /* Ensure the first element is lowest. This acts as a sentinel,
8762 speeding the main loop below. */
8763 r_off = (*ext_r_off) (base);
8764 for (p = loc = base; (p += elt_size) < end; )
8766 bfd_vma r_off2 = (*ext_r_off) (p);
8775 /* Don't just swap *base and *loc as that changes the order
8776 of the original base[0] and base[1] if they happen to
8777 have the same r_offset. */
8778 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8779 memcpy (onebuf, loc, elt_size);
8780 memmove (base + elt_size, base, loc - base);
8781 memcpy (base, onebuf, elt_size);
8784 for (p = base + elt_size; (p += elt_size) < end; )
8786 /* base to p is sorted, *p is next to insert. */
8787 r_off = (*ext_r_off) (p);
8788 /* Search the sorted region for location to insert. */
8790 while (r_off < (*ext_r_off) (loc))
8795 /* Chances are there is a run of relocs to insert here,
8796 from one of more input files. Files are not always
8797 linked in order due to the way elf_link_input_bfd is
8798 called. See pr17666. */
8799 size_t sortlen = p - loc;
8800 bfd_vma r_off2 = (*ext_r_off) (loc);
8801 size_t runlen = elt_size;
8802 size_t buf_size = 96 * 1024;
8803 while (p + runlen < end
8804 && (sortlen <= buf_size
8805 || runlen + elt_size <= buf_size)
8806 && r_off2 > (*ext_r_off) (p + runlen))
8810 buf = bfd_malloc (buf_size);
8814 if (runlen < sortlen)
8816 memcpy (buf, p, runlen);
8817 memmove (loc + runlen, loc, sortlen);
8818 memcpy (loc, buf, runlen);
8822 memcpy (buf, loc, sortlen);
8823 memmove (loc, p, runlen);
8824 memcpy (loc + runlen, buf, sortlen);
8826 p += runlen - elt_size;
8829 /* Hashes are no longer valid. */
8830 free (reldata->hashes);
8831 reldata->hashes = NULL;
8837 struct elf_link_sort_rela
8843 enum elf_reloc_type_class type;
8844 /* We use this as an array of size int_rels_per_ext_rel. */
8845 Elf_Internal_Rela rela[1];
8849 elf_link_sort_cmp1 (const void *A, const void *B)
8851 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8852 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8853 int relativea, relativeb;
8855 relativea = a->type == reloc_class_relative;
8856 relativeb = b->type == reloc_class_relative;
8858 if (relativea < relativeb)
8860 if (relativea > relativeb)
8862 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8864 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8866 if (a->rela->r_offset < b->rela->r_offset)
8868 if (a->rela->r_offset > b->rela->r_offset)
8874 elf_link_sort_cmp2 (const void *A, const void *B)
8876 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8877 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8879 if (a->type < b->type)
8881 if (a->type > b->type)
8883 if (a->u.offset < b->u.offset)
8885 if (a->u.offset > b->u.offset)
8887 if (a->rela->r_offset < b->rela->r_offset)
8889 if (a->rela->r_offset > b->rela->r_offset)
8895 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8897 asection *dynamic_relocs;
8900 bfd_size_type count, size;
8901 size_t i, ret, sort_elt, ext_size;
8902 bfd_byte *sort, *s_non_relative, *p;
8903 struct elf_link_sort_rela *sq;
8904 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8905 int i2e = bed->s->int_rels_per_ext_rel;
8906 unsigned int opb = bfd_octets_per_byte (abfd);
8907 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8908 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8909 struct bfd_link_order *lo;
8911 bfd_boolean use_rela;
8913 /* Find a dynamic reloc section. */
8914 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8915 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8916 if (rela_dyn != NULL && rela_dyn->size > 0
8917 && rel_dyn != NULL && rel_dyn->size > 0)
8919 bfd_boolean use_rela_initialised = FALSE;
8921 /* This is just here to stop gcc from complaining.
8922 Its initialization checking code is not perfect. */
8925 /* Both sections are present. Examine the sizes
8926 of the indirect sections to help us choose. */
8927 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8928 if (lo->type == bfd_indirect_link_order)
8930 asection *o = lo->u.indirect.section;
8932 if ((o->size % bed->s->sizeof_rela) == 0)
8934 if ((o->size % bed->s->sizeof_rel) == 0)
8935 /* Section size is divisible by both rel and rela sizes.
8936 It is of no help to us. */
8940 /* Section size is only divisible by rela. */
8941 if (use_rela_initialised && !use_rela)
8943 _bfd_error_handler (_("%B: Unable to sort relocs - "
8944 "they are in more than one size"),
8946 bfd_set_error (bfd_error_invalid_operation);
8952 use_rela_initialised = TRUE;
8956 else if ((o->size % bed->s->sizeof_rel) == 0)
8958 /* Section size is only divisible by rel. */
8959 if (use_rela_initialised && use_rela)
8961 _bfd_error_handler (_("%B: Unable to sort relocs - "
8962 "they are in more than one size"),
8964 bfd_set_error (bfd_error_invalid_operation);
8970 use_rela_initialised = TRUE;
8975 /* The section size is not divisible by either -
8976 something is wrong. */
8977 _bfd_error_handler (_("%B: Unable to sort relocs - "
8978 "they are of an unknown size"), abfd);
8979 bfd_set_error (bfd_error_invalid_operation);
8984 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8985 if (lo->type == bfd_indirect_link_order)
8987 asection *o = lo->u.indirect.section;
8989 if ((o->size % bed->s->sizeof_rela) == 0)
8991 if ((o->size % bed->s->sizeof_rel) == 0)
8992 /* Section size is divisible by both rel and rela sizes.
8993 It is of no help to us. */
8997 /* Section size is only divisible by rela. */
8998 if (use_rela_initialised && !use_rela)
9000 _bfd_error_handler (_("%B: Unable to sort relocs - "
9001 "they are in more than one size"),
9003 bfd_set_error (bfd_error_invalid_operation);
9009 use_rela_initialised = TRUE;
9013 else if ((o->size % bed->s->sizeof_rel) == 0)
9015 /* Section size is only divisible by rel. */
9016 if (use_rela_initialised && use_rela)
9018 _bfd_error_handler (_("%B: Unable to sort relocs - "
9019 "they are in more than one size"),
9021 bfd_set_error (bfd_error_invalid_operation);
9027 use_rela_initialised = TRUE;
9032 /* The section size is not divisible by either -
9033 something is wrong. */
9034 _bfd_error_handler (_("%B: Unable to sort relocs - "
9035 "they are of an unknown size"), abfd);
9036 bfd_set_error (bfd_error_invalid_operation);
9041 if (! use_rela_initialised)
9045 else if (rela_dyn != NULL && rela_dyn->size > 0)
9047 else if (rel_dyn != NULL && rel_dyn->size > 0)
9054 dynamic_relocs = rela_dyn;
9055 ext_size = bed->s->sizeof_rela;
9056 swap_in = bed->s->swap_reloca_in;
9057 swap_out = bed->s->swap_reloca_out;
9061 dynamic_relocs = rel_dyn;
9062 ext_size = bed->s->sizeof_rel;
9063 swap_in = bed->s->swap_reloc_in;
9064 swap_out = bed->s->swap_reloc_out;
9068 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9069 if (lo->type == bfd_indirect_link_order)
9070 size += lo->u.indirect.section->size;
9072 if (size != dynamic_relocs->size)
9075 sort_elt = (sizeof (struct elf_link_sort_rela)
9076 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9078 count = dynamic_relocs->size / ext_size;
9081 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9085 (*info->callbacks->warning)
9086 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9090 if (bed->s->arch_size == 32)
9091 r_sym_mask = ~(bfd_vma) 0xff;
9093 r_sym_mask = ~(bfd_vma) 0xffffffff;
9095 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9096 if (lo->type == bfd_indirect_link_order)
9098 bfd_byte *erel, *erelend;
9099 asection *o = lo->u.indirect.section;
9101 if (o->contents == NULL && o->size != 0)
9103 /* This is a reloc section that is being handled as a normal
9104 section. See bfd_section_from_shdr. We can't combine
9105 relocs in this case. */
9110 erelend = o->contents + o->size;
9111 p = sort + o->output_offset * opb / ext_size * sort_elt;
9113 while (erel < erelend)
9115 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9117 (*swap_in) (abfd, erel, s->rela);
9118 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9119 s->u.sym_mask = r_sym_mask;
9125 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9127 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9129 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9130 if (s->type != reloc_class_relative)
9136 sq = (struct elf_link_sort_rela *) s_non_relative;
9137 for (; i < count; i++, p += sort_elt)
9139 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9140 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9142 sp->u.offset = sq->rela->r_offset;
9145 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9147 struct elf_link_hash_table *htab = elf_hash_table (info);
9148 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9150 /* We have plt relocs in .rela.dyn. */
9151 sq = (struct elf_link_sort_rela *) sort;
9152 for (i = 0; i < count; i++)
9153 if (sq[count - i - 1].type != reloc_class_plt)
9155 if (i != 0 && htab->srelplt->size == i * ext_size)
9157 struct bfd_link_order **plo;
9158 /* Put srelplt link_order last. This is so the output_offset
9159 set in the next loop is correct for DT_JMPREL. */
9160 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9161 if ((*plo)->type == bfd_indirect_link_order
9162 && (*plo)->u.indirect.section == htab->srelplt)
9168 plo = &(*plo)->next;
9171 dynamic_relocs->map_tail.link_order = lo;
9176 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9177 if (lo->type == bfd_indirect_link_order)
9179 bfd_byte *erel, *erelend;
9180 asection *o = lo->u.indirect.section;
9183 erelend = o->contents + o->size;
9184 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9185 while (erel < erelend)
9187 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9188 (*swap_out) (abfd, s->rela, erel);
9195 *psec = dynamic_relocs;
9199 /* Add a symbol to the output symbol string table. */
9202 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9204 Elf_Internal_Sym *elfsym,
9205 asection *input_sec,
9206 struct elf_link_hash_entry *h)
9208 int (*output_symbol_hook)
9209 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9210 struct elf_link_hash_entry *);
9211 struct elf_link_hash_table *hash_table;
9212 const struct elf_backend_data *bed;
9213 bfd_size_type strtabsize;
9215 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9217 bed = get_elf_backend_data (flinfo->output_bfd);
9218 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9219 if (output_symbol_hook != NULL)
9221 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9228 || (input_sec->flags & SEC_EXCLUDE))
9229 elfsym->st_name = (unsigned long) -1;
9232 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9233 to get the final offset for st_name. */
9235 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9237 if (elfsym->st_name == (unsigned long) -1)
9241 hash_table = elf_hash_table (flinfo->info);
9242 strtabsize = hash_table->strtabsize;
9243 if (strtabsize <= hash_table->strtabcount)
9245 strtabsize += strtabsize;
9246 hash_table->strtabsize = strtabsize;
9247 strtabsize *= sizeof (*hash_table->strtab);
9249 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9251 if (hash_table->strtab == NULL)
9254 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9255 hash_table->strtab[hash_table->strtabcount].dest_index
9256 = hash_table->strtabcount;
9257 hash_table->strtab[hash_table->strtabcount].destshndx_index
9258 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9260 bfd_get_symcount (flinfo->output_bfd) += 1;
9261 hash_table->strtabcount += 1;
9266 /* Swap symbols out to the symbol table and flush the output symbols to
9270 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9272 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9275 const struct elf_backend_data *bed;
9277 Elf_Internal_Shdr *hdr;
9281 if (!hash_table->strtabcount)
9284 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9286 bed = get_elf_backend_data (flinfo->output_bfd);
9288 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9289 symbuf = (bfd_byte *) bfd_malloc (amt);
9293 if (flinfo->symshndxbuf)
9295 amt = sizeof (Elf_External_Sym_Shndx);
9296 amt *= bfd_get_symcount (flinfo->output_bfd);
9297 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9298 if (flinfo->symshndxbuf == NULL)
9305 for (i = 0; i < hash_table->strtabcount; i++)
9307 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9308 if (elfsym->sym.st_name == (unsigned long) -1)
9309 elfsym->sym.st_name = 0;
9312 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9313 elfsym->sym.st_name);
9314 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9315 ((bfd_byte *) symbuf
9316 + (elfsym->dest_index
9317 * bed->s->sizeof_sym)),
9318 (flinfo->symshndxbuf
9319 + elfsym->destshndx_index));
9322 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9323 pos = hdr->sh_offset + hdr->sh_size;
9324 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9325 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9326 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9328 hdr->sh_size += amt;
9336 free (hash_table->strtab);
9337 hash_table->strtab = NULL;
9342 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9345 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9347 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9348 && sym->st_shndx < SHN_LORESERVE)
9350 /* The gABI doesn't support dynamic symbols in output sections
9353 /* xgettext:c-format */
9354 (_("%B: Too many sections: %d (>= %d)"),
9355 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9356 bfd_set_error (bfd_error_nonrepresentable_section);
9362 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9363 allowing an unsatisfied unversioned symbol in the DSO to match a
9364 versioned symbol that would normally require an explicit version.
9365 We also handle the case that a DSO references a hidden symbol
9366 which may be satisfied by a versioned symbol in another DSO. */
9369 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9370 const struct elf_backend_data *bed,
9371 struct elf_link_hash_entry *h)
9374 struct elf_link_loaded_list *loaded;
9376 if (!is_elf_hash_table (info->hash))
9379 /* Check indirect symbol. */
9380 while (h->root.type == bfd_link_hash_indirect)
9381 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9383 switch (h->root.type)
9389 case bfd_link_hash_undefined:
9390 case bfd_link_hash_undefweak:
9391 abfd = h->root.u.undef.abfd;
9393 || (abfd->flags & DYNAMIC) == 0
9394 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9398 case bfd_link_hash_defined:
9399 case bfd_link_hash_defweak:
9400 abfd = h->root.u.def.section->owner;
9403 case bfd_link_hash_common:
9404 abfd = h->root.u.c.p->section->owner;
9407 BFD_ASSERT (abfd != NULL);
9409 for (loaded = elf_hash_table (info)->loaded;
9411 loaded = loaded->next)
9414 Elf_Internal_Shdr *hdr;
9418 Elf_Internal_Shdr *versymhdr;
9419 Elf_Internal_Sym *isym;
9420 Elf_Internal_Sym *isymend;
9421 Elf_Internal_Sym *isymbuf;
9422 Elf_External_Versym *ever;
9423 Elf_External_Versym *extversym;
9425 input = loaded->abfd;
9427 /* We check each DSO for a possible hidden versioned definition. */
9429 || (input->flags & DYNAMIC) == 0
9430 || elf_dynversym (input) == 0)
9433 hdr = &elf_tdata (input)->dynsymtab_hdr;
9435 symcount = hdr->sh_size / bed->s->sizeof_sym;
9436 if (elf_bad_symtab (input))
9438 extsymcount = symcount;
9443 extsymcount = symcount - hdr->sh_info;
9444 extsymoff = hdr->sh_info;
9447 if (extsymcount == 0)
9450 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9452 if (isymbuf == NULL)
9455 /* Read in any version definitions. */
9456 versymhdr = &elf_tdata (input)->dynversym_hdr;
9457 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9458 if (extversym == NULL)
9461 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9462 || (bfd_bread (extversym, versymhdr->sh_size, input)
9463 != versymhdr->sh_size))
9471 ever = extversym + extsymoff;
9472 isymend = isymbuf + extsymcount;
9473 for (isym = isymbuf; isym < isymend; isym++, ever++)
9476 Elf_Internal_Versym iver;
9477 unsigned short version_index;
9479 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9480 || isym->st_shndx == SHN_UNDEF)
9483 name = bfd_elf_string_from_elf_section (input,
9486 if (strcmp (name, h->root.root.string) != 0)
9489 _bfd_elf_swap_versym_in (input, ever, &iver);
9491 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9493 && h->forced_local))
9495 /* If we have a non-hidden versioned sym, then it should
9496 have provided a definition for the undefined sym unless
9497 it is defined in a non-shared object and forced local.
9502 version_index = iver.vs_vers & VERSYM_VERSION;
9503 if (version_index == 1 || version_index == 2)
9505 /* This is the base or first version. We can use it. */
9519 /* Convert ELF common symbol TYPE. */
9522 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9524 /* Commom symbol can only appear in relocatable link. */
9525 if (!bfd_link_relocatable (info))
9527 switch (info->elf_stt_common)
9531 case elf_stt_common:
9534 case no_elf_stt_common:
9541 /* Add an external symbol to the symbol table. This is called from
9542 the hash table traversal routine. When generating a shared object,
9543 we go through the symbol table twice. The first time we output
9544 anything that might have been forced to local scope in a version
9545 script. The second time we output the symbols that are still
9549 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9551 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9552 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9553 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9555 Elf_Internal_Sym sym;
9556 asection *input_sec;
9557 const struct elf_backend_data *bed;
9562 if (h->root.type == bfd_link_hash_warning)
9564 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9565 if (h->root.type == bfd_link_hash_new)
9569 /* Decide whether to output this symbol in this pass. */
9570 if (eoinfo->localsyms)
9572 if (!h->forced_local)
9577 if (h->forced_local)
9581 bed = get_elf_backend_data (flinfo->output_bfd);
9583 if (h->root.type == bfd_link_hash_undefined)
9585 /* If we have an undefined symbol reference here then it must have
9586 come from a shared library that is being linked in. (Undefined
9587 references in regular files have already been handled unless
9588 they are in unreferenced sections which are removed by garbage
9590 bfd_boolean ignore_undef = FALSE;
9592 /* Some symbols may be special in that the fact that they're
9593 undefined can be safely ignored - let backend determine that. */
9594 if (bed->elf_backend_ignore_undef_symbol)
9595 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9597 /* If we are reporting errors for this situation then do so now. */
9600 && (!h->ref_regular || flinfo->info->gc_sections)
9601 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9602 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9603 (*flinfo->info->callbacks->undefined_symbol)
9604 (flinfo->info, h->root.root.string,
9605 h->ref_regular ? NULL : h->root.u.undef.abfd,
9607 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9609 /* Strip a global symbol defined in a discarded section. */
9614 /* We should also warn if a forced local symbol is referenced from
9615 shared libraries. */
9616 if (bfd_link_executable (flinfo->info)
9621 && h->ref_dynamic_nonweak
9622 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9626 struct elf_link_hash_entry *hi = h;
9628 /* Check indirect symbol. */
9629 while (hi->root.type == bfd_link_hash_indirect)
9630 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9632 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9633 /* xgettext:c-format */
9634 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9635 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9636 /* xgettext:c-format */
9637 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9639 /* xgettext:c-format */
9640 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9641 def_bfd = flinfo->output_bfd;
9642 if (hi->root.u.def.section != bfd_abs_section_ptr)
9643 def_bfd = hi->root.u.def.section->owner;
9644 _bfd_error_handler (msg, flinfo->output_bfd,
9645 h->root.root.string, def_bfd);
9646 bfd_set_error (bfd_error_bad_value);
9647 eoinfo->failed = TRUE;
9651 /* We don't want to output symbols that have never been mentioned by
9652 a regular file, or that we have been told to strip. However, if
9653 h->indx is set to -2, the symbol is used by a reloc and we must
9658 else if ((h->def_dynamic
9660 || h->root.type == bfd_link_hash_new)
9664 else if (flinfo->info->strip == strip_all)
9666 else if (flinfo->info->strip == strip_some
9667 && bfd_hash_lookup (flinfo->info->keep_hash,
9668 h->root.root.string, FALSE, FALSE) == NULL)
9670 else if ((h->root.type == bfd_link_hash_defined
9671 || h->root.type == bfd_link_hash_defweak)
9672 && ((flinfo->info->strip_discarded
9673 && discarded_section (h->root.u.def.section))
9674 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9675 && h->root.u.def.section->owner != NULL
9676 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9678 else if ((h->root.type == bfd_link_hash_undefined
9679 || h->root.type == bfd_link_hash_undefweak)
9680 && h->root.u.undef.abfd != NULL
9681 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9686 /* If we're stripping it, and it's not a dynamic symbol, there's
9687 nothing else to do. However, if it is a forced local symbol or
9688 an ifunc symbol we need to give the backend finish_dynamic_symbol
9689 function a chance to make it dynamic. */
9692 && type != STT_GNU_IFUNC
9693 && !h->forced_local)
9697 sym.st_size = h->size;
9698 sym.st_other = h->other;
9699 switch (h->root.type)
9702 case bfd_link_hash_new:
9703 case bfd_link_hash_warning:
9707 case bfd_link_hash_undefined:
9708 case bfd_link_hash_undefweak:
9709 input_sec = bfd_und_section_ptr;
9710 sym.st_shndx = SHN_UNDEF;
9713 case bfd_link_hash_defined:
9714 case bfd_link_hash_defweak:
9716 input_sec = h->root.u.def.section;
9717 if (input_sec->output_section != NULL)
9720 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9721 input_sec->output_section);
9722 if (sym.st_shndx == SHN_BAD)
9725 /* xgettext:c-format */
9726 (_("%B: could not find output section %A for input section %A"),
9727 flinfo->output_bfd, input_sec->output_section, input_sec);
9728 bfd_set_error (bfd_error_nonrepresentable_section);
9729 eoinfo->failed = TRUE;
9733 /* ELF symbols in relocatable files are section relative,
9734 but in nonrelocatable files they are virtual
9736 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9737 if (!bfd_link_relocatable (flinfo->info))
9739 sym.st_value += input_sec->output_section->vma;
9740 if (h->type == STT_TLS)
9742 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9743 if (tls_sec != NULL)
9744 sym.st_value -= tls_sec->vma;
9750 BFD_ASSERT (input_sec->owner == NULL
9751 || (input_sec->owner->flags & DYNAMIC) != 0);
9752 sym.st_shndx = SHN_UNDEF;
9753 input_sec = bfd_und_section_ptr;
9758 case bfd_link_hash_common:
9759 input_sec = h->root.u.c.p->section;
9760 sym.st_shndx = bed->common_section_index (input_sec);
9761 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9764 case bfd_link_hash_indirect:
9765 /* These symbols are created by symbol versioning. They point
9766 to the decorated version of the name. For example, if the
9767 symbol foo@@GNU_1.2 is the default, which should be used when
9768 foo is used with no version, then we add an indirect symbol
9769 foo which points to foo@@GNU_1.2. We ignore these symbols,
9770 since the indirected symbol is already in the hash table. */
9774 if (type == STT_COMMON || type == STT_OBJECT)
9775 switch (h->root.type)
9777 case bfd_link_hash_common:
9778 type = elf_link_convert_common_type (flinfo->info, type);
9780 case bfd_link_hash_defined:
9781 case bfd_link_hash_defweak:
9782 if (bed->common_definition (&sym))
9783 type = elf_link_convert_common_type (flinfo->info, type);
9787 case bfd_link_hash_undefined:
9788 case bfd_link_hash_undefweak:
9794 if (h->forced_local)
9796 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9797 /* Turn off visibility on local symbol. */
9798 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9800 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9801 else if (h->unique_global && h->def_regular)
9802 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9803 else if (h->root.type == bfd_link_hash_undefweak
9804 || h->root.type == bfd_link_hash_defweak)
9805 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9807 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9808 sym.st_target_internal = h->target_internal;
9810 /* Give the processor backend a chance to tweak the symbol value,
9811 and also to finish up anything that needs to be done for this
9812 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9813 forced local syms when non-shared is due to a historical quirk.
9814 STT_GNU_IFUNC symbol must go through PLT. */
9815 if ((h->type == STT_GNU_IFUNC
9817 && !bfd_link_relocatable (flinfo->info))
9818 || ((h->dynindx != -1
9820 && ((bfd_link_pic (flinfo->info)
9821 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9822 || h->root.type != bfd_link_hash_undefweak))
9823 || !h->forced_local)
9824 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9826 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9827 (flinfo->output_bfd, flinfo->info, h, &sym)))
9829 eoinfo->failed = TRUE;
9834 /* If we are marking the symbol as undefined, and there are no
9835 non-weak references to this symbol from a regular object, then
9836 mark the symbol as weak undefined; if there are non-weak
9837 references, mark the symbol as strong. We can't do this earlier,
9838 because it might not be marked as undefined until the
9839 finish_dynamic_symbol routine gets through with it. */
9840 if (sym.st_shndx == SHN_UNDEF
9842 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9843 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9846 type = ELF_ST_TYPE (sym.st_info);
9848 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9849 if (type == STT_GNU_IFUNC)
9852 if (h->ref_regular_nonweak)
9853 bindtype = STB_GLOBAL;
9855 bindtype = STB_WEAK;
9856 sym.st_info = ELF_ST_INFO (bindtype, type);
9859 /* If this is a symbol defined in a dynamic library, don't use the
9860 symbol size from the dynamic library. Relinking an executable
9861 against a new library may introduce gratuitous changes in the
9862 executable's symbols if we keep the size. */
9863 if (sym.st_shndx == SHN_UNDEF
9868 /* If a non-weak symbol with non-default visibility is not defined
9869 locally, it is a fatal error. */
9870 if (!bfd_link_relocatable (flinfo->info)
9871 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9872 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9873 && h->root.type == bfd_link_hash_undefined
9878 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9879 /* xgettext:c-format */
9880 msg = _("%B: protected symbol `%s' isn't defined");
9881 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9882 /* xgettext:c-format */
9883 msg = _("%B: internal symbol `%s' isn't defined");
9885 /* xgettext:c-format */
9886 msg = _("%B: hidden symbol `%s' isn't defined");
9887 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9888 bfd_set_error (bfd_error_bad_value);
9889 eoinfo->failed = TRUE;
9893 /* If this symbol should be put in the .dynsym section, then put it
9894 there now. We already know the symbol index. We also fill in
9895 the entry in the .hash section. */
9896 if (elf_hash_table (flinfo->info)->dynsym != NULL
9898 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9902 /* Since there is no version information in the dynamic string,
9903 if there is no version info in symbol version section, we will
9904 have a run-time problem if not linking executable, referenced
9905 by shared library, or not bound locally. */
9906 if (h->verinfo.verdef == NULL
9907 && (!bfd_link_executable (flinfo->info)
9909 || !h->def_regular))
9911 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9913 if (p && p [1] != '\0')
9916 /* xgettext:c-format */
9917 (_("%B: No symbol version section for versioned symbol `%s'"),
9918 flinfo->output_bfd, h->root.root.string);
9919 eoinfo->failed = TRUE;
9924 sym.st_name = h->dynstr_index;
9925 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9926 + h->dynindx * bed->s->sizeof_sym);
9927 if (!check_dynsym (flinfo->output_bfd, &sym))
9929 eoinfo->failed = TRUE;
9932 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9934 if (flinfo->hash_sec != NULL)
9936 size_t hash_entry_size;
9937 bfd_byte *bucketpos;
9942 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9943 bucket = h->u.elf_hash_value % bucketcount;
9946 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9947 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9948 + (bucket + 2) * hash_entry_size);
9949 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9950 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9952 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9953 ((bfd_byte *) flinfo->hash_sec->contents
9954 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9957 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9959 Elf_Internal_Versym iversym;
9960 Elf_External_Versym *eversym;
9962 if (!h->def_regular)
9964 if (h->verinfo.verdef == NULL
9965 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9966 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9967 iversym.vs_vers = 0;
9969 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9973 if (h->verinfo.vertree == NULL)
9974 iversym.vs_vers = 1;
9976 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9977 if (flinfo->info->create_default_symver)
9981 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9983 if (h->versioned == versioned_hidden && h->def_regular)
9984 iversym.vs_vers |= VERSYM_HIDDEN;
9986 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9987 eversym += h->dynindx;
9988 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9992 /* If the symbol is undefined, and we didn't output it to .dynsym,
9993 strip it from .symtab too. Obviously we can't do this for
9994 relocatable output or when needed for --emit-relocs. */
9995 else if (input_sec == bfd_und_section_ptr
9997 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
9998 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
9999 && !bfd_link_relocatable (flinfo->info))
10002 /* Also strip others that we couldn't earlier due to dynamic symbol
10006 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10009 /* Output a FILE symbol so that following locals are not associated
10010 with the wrong input file. We need one for forced local symbols
10011 if we've seen more than one FILE symbol or when we have exactly
10012 one FILE symbol but global symbols are present in a file other
10013 than the one with the FILE symbol. We also need one if linker
10014 defined symbols are present. In practice these conditions are
10015 always met, so just emit the FILE symbol unconditionally. */
10016 if (eoinfo->localsyms
10017 && !eoinfo->file_sym_done
10018 && eoinfo->flinfo->filesym_count != 0)
10020 Elf_Internal_Sym fsym;
10022 memset (&fsym, 0, sizeof (fsym));
10023 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10024 fsym.st_shndx = SHN_ABS;
10025 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10026 bfd_und_section_ptr, NULL))
10029 eoinfo->file_sym_done = TRUE;
10032 indx = bfd_get_symcount (flinfo->output_bfd);
10033 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10037 eoinfo->failed = TRUE;
10042 else if (h->indx == -2)
10048 /* Return TRUE if special handling is done for relocs in SEC against
10049 symbols defined in discarded sections. */
10052 elf_section_ignore_discarded_relocs (asection *sec)
10054 const struct elf_backend_data *bed;
10056 switch (sec->sec_info_type)
10058 case SEC_INFO_TYPE_STABS:
10059 case SEC_INFO_TYPE_EH_FRAME:
10060 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10066 bed = get_elf_backend_data (sec->owner);
10067 if (bed->elf_backend_ignore_discarded_relocs != NULL
10068 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10074 /* Return a mask saying how ld should treat relocations in SEC against
10075 symbols defined in discarded sections. If this function returns
10076 COMPLAIN set, ld will issue a warning message. If this function
10077 returns PRETEND set, and the discarded section was link-once and the
10078 same size as the kept link-once section, ld will pretend that the
10079 symbol was actually defined in the kept section. Otherwise ld will
10080 zero the reloc (at least that is the intent, but some cooperation by
10081 the target dependent code is needed, particularly for REL targets). */
10084 _bfd_elf_default_action_discarded (asection *sec)
10086 if (sec->flags & SEC_DEBUGGING)
10089 if (strcmp (".eh_frame", sec->name) == 0)
10092 if (strcmp (".gcc_except_table", sec->name) == 0)
10095 return COMPLAIN | PRETEND;
10098 /* Find a match between a section and a member of a section group. */
10101 match_group_member (asection *sec, asection *group,
10102 struct bfd_link_info *info)
10104 asection *first = elf_next_in_group (group);
10105 asection *s = first;
10109 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10112 s = elf_next_in_group (s);
10120 /* Check if the kept section of a discarded section SEC can be used
10121 to replace it. Return the replacement if it is OK. Otherwise return
10125 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10129 kept = sec->kept_section;
10132 if ((kept->flags & SEC_GROUP) != 0)
10133 kept = match_group_member (sec, kept, info);
10135 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10136 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10138 sec->kept_section = kept;
10143 /* Link an input file into the linker output file. This function
10144 handles all the sections and relocations of the input file at once.
10145 This is so that we only have to read the local symbols once, and
10146 don't have to keep them in memory. */
10149 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10151 int (*relocate_section)
10152 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10153 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10155 Elf_Internal_Shdr *symtab_hdr;
10156 size_t locsymcount;
10158 Elf_Internal_Sym *isymbuf;
10159 Elf_Internal_Sym *isym;
10160 Elf_Internal_Sym *isymend;
10162 asection **ppsection;
10164 const struct elf_backend_data *bed;
10165 struct elf_link_hash_entry **sym_hashes;
10166 bfd_size_type address_size;
10167 bfd_vma r_type_mask;
10169 bfd_boolean have_file_sym = FALSE;
10171 output_bfd = flinfo->output_bfd;
10172 bed = get_elf_backend_data (output_bfd);
10173 relocate_section = bed->elf_backend_relocate_section;
10175 /* If this is a dynamic object, we don't want to do anything here:
10176 we don't want the local symbols, and we don't want the section
10178 if ((input_bfd->flags & DYNAMIC) != 0)
10181 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10182 if (elf_bad_symtab (input_bfd))
10184 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10189 locsymcount = symtab_hdr->sh_info;
10190 extsymoff = symtab_hdr->sh_info;
10193 /* Read the local symbols. */
10194 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10195 if (isymbuf == NULL && locsymcount != 0)
10197 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10198 flinfo->internal_syms,
10199 flinfo->external_syms,
10200 flinfo->locsym_shndx);
10201 if (isymbuf == NULL)
10205 /* Find local symbol sections and adjust values of symbols in
10206 SEC_MERGE sections. Write out those local symbols we know are
10207 going into the output file. */
10208 isymend = isymbuf + locsymcount;
10209 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10211 isym++, pindex++, ppsection++)
10215 Elf_Internal_Sym osym;
10221 if (elf_bad_symtab (input_bfd))
10223 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10230 if (isym->st_shndx == SHN_UNDEF)
10231 isec = bfd_und_section_ptr;
10232 else if (isym->st_shndx == SHN_ABS)
10233 isec = bfd_abs_section_ptr;
10234 else if (isym->st_shndx == SHN_COMMON)
10235 isec = bfd_com_section_ptr;
10238 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10241 /* Don't attempt to output symbols with st_shnx in the
10242 reserved range other than SHN_ABS and SHN_COMMON. */
10246 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10247 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10249 _bfd_merged_section_offset (output_bfd, &isec,
10250 elf_section_data (isec)->sec_info,
10256 /* Don't output the first, undefined, symbol. In fact, don't
10257 output any undefined local symbol. */
10258 if (isec == bfd_und_section_ptr)
10261 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10263 /* We never output section symbols. Instead, we use the
10264 section symbol of the corresponding section in the output
10269 /* If we are stripping all symbols, we don't want to output this
10271 if (flinfo->info->strip == strip_all)
10274 /* If we are discarding all local symbols, we don't want to
10275 output this one. If we are generating a relocatable output
10276 file, then some of the local symbols may be required by
10277 relocs; we output them below as we discover that they are
10279 if (flinfo->info->discard == discard_all)
10282 /* If this symbol is defined in a section which we are
10283 discarding, we don't need to keep it. */
10284 if (isym->st_shndx != SHN_UNDEF
10285 && isym->st_shndx < SHN_LORESERVE
10286 && bfd_section_removed_from_list (output_bfd,
10287 isec->output_section))
10290 /* Get the name of the symbol. */
10291 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10296 /* See if we are discarding symbols with this name. */
10297 if ((flinfo->info->strip == strip_some
10298 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10300 || (((flinfo->info->discard == discard_sec_merge
10301 && (isec->flags & SEC_MERGE)
10302 && !bfd_link_relocatable (flinfo->info))
10303 || flinfo->info->discard == discard_l)
10304 && bfd_is_local_label_name (input_bfd, name)))
10307 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10309 if (input_bfd->lto_output)
10310 /* -flto puts a temp file name here. This means builds
10311 are not reproducible. Discard the symbol. */
10313 have_file_sym = TRUE;
10314 flinfo->filesym_count += 1;
10316 if (!have_file_sym)
10318 /* In the absence of debug info, bfd_find_nearest_line uses
10319 FILE symbols to determine the source file for local
10320 function symbols. Provide a FILE symbol here if input
10321 files lack such, so that their symbols won't be
10322 associated with a previous input file. It's not the
10323 source file, but the best we can do. */
10324 have_file_sym = TRUE;
10325 flinfo->filesym_count += 1;
10326 memset (&osym, 0, sizeof (osym));
10327 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10328 osym.st_shndx = SHN_ABS;
10329 if (!elf_link_output_symstrtab (flinfo,
10330 (input_bfd->lto_output ? NULL
10331 : input_bfd->filename),
10332 &osym, bfd_abs_section_ptr,
10339 /* Adjust the section index for the output file. */
10340 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10341 isec->output_section);
10342 if (osym.st_shndx == SHN_BAD)
10345 /* ELF symbols in relocatable files are section relative, but
10346 in executable files they are virtual addresses. Note that
10347 this code assumes that all ELF sections have an associated
10348 BFD section with a reasonable value for output_offset; below
10349 we assume that they also have a reasonable value for
10350 output_section. Any special sections must be set up to meet
10351 these requirements. */
10352 osym.st_value += isec->output_offset;
10353 if (!bfd_link_relocatable (flinfo->info))
10355 osym.st_value += isec->output_section->vma;
10356 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10358 /* STT_TLS symbols are relative to PT_TLS segment base. */
10359 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10360 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10364 indx = bfd_get_symcount (output_bfd);
10365 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10372 if (bed->s->arch_size == 32)
10374 r_type_mask = 0xff;
10380 r_type_mask = 0xffffffff;
10385 /* Relocate the contents of each section. */
10386 sym_hashes = elf_sym_hashes (input_bfd);
10387 for (o = input_bfd->sections; o != NULL; o = o->next)
10389 bfd_byte *contents;
10391 if (! o->linker_mark)
10393 /* This section was omitted from the link. */
10397 if (!flinfo->info->resolve_section_groups
10398 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10400 /* Deal with the group signature symbol. */
10401 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10402 unsigned long symndx = sec_data->this_hdr.sh_info;
10403 asection *osec = o->output_section;
10405 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10406 if (symndx >= locsymcount
10407 || (elf_bad_symtab (input_bfd)
10408 && flinfo->sections[symndx] == NULL))
10410 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10411 while (h->root.type == bfd_link_hash_indirect
10412 || h->root.type == bfd_link_hash_warning)
10413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10414 /* Arrange for symbol to be output. */
10416 elf_section_data (osec)->this_hdr.sh_info = -2;
10418 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10420 /* We'll use the output section target_index. */
10421 asection *sec = flinfo->sections[symndx]->output_section;
10422 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10426 if (flinfo->indices[symndx] == -1)
10428 /* Otherwise output the local symbol now. */
10429 Elf_Internal_Sym sym = isymbuf[symndx];
10430 asection *sec = flinfo->sections[symndx]->output_section;
10435 name = bfd_elf_string_from_elf_section (input_bfd,
10436 symtab_hdr->sh_link,
10441 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10443 if (sym.st_shndx == SHN_BAD)
10446 sym.st_value += o->output_offset;
10448 indx = bfd_get_symcount (output_bfd);
10449 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10454 flinfo->indices[symndx] = indx;
10458 elf_section_data (osec)->this_hdr.sh_info
10459 = flinfo->indices[symndx];
10463 if ((o->flags & SEC_HAS_CONTENTS) == 0
10464 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10467 if ((o->flags & SEC_LINKER_CREATED) != 0)
10469 /* Section was created by _bfd_elf_link_create_dynamic_sections
10474 /* Get the contents of the section. They have been cached by a
10475 relaxation routine. Note that o is a section in an input
10476 file, so the contents field will not have been set by any of
10477 the routines which work on output files. */
10478 if (elf_section_data (o)->this_hdr.contents != NULL)
10480 contents = elf_section_data (o)->this_hdr.contents;
10481 if (bed->caches_rawsize
10483 && o->rawsize < o->size)
10485 memcpy (flinfo->contents, contents, o->rawsize);
10486 contents = flinfo->contents;
10491 contents = flinfo->contents;
10492 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10496 if ((o->flags & SEC_RELOC) != 0)
10498 Elf_Internal_Rela *internal_relocs;
10499 Elf_Internal_Rela *rel, *relend;
10500 int action_discarded;
10503 /* Get the swapped relocs. */
10505 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10506 flinfo->internal_relocs, FALSE);
10507 if (internal_relocs == NULL
10508 && o->reloc_count > 0)
10511 /* We need to reverse-copy input .ctors/.dtors sections if
10512 they are placed in .init_array/.finit_array for output. */
10513 if (o->size > address_size
10514 && ((strncmp (o->name, ".ctors", 6) == 0
10515 && strcmp (o->output_section->name,
10516 ".init_array") == 0)
10517 || (strncmp (o->name, ".dtors", 6) == 0
10518 && strcmp (o->output_section->name,
10519 ".fini_array") == 0))
10520 && (o->name[6] == 0 || o->name[6] == '.'))
10522 if (o->size * bed->s->int_rels_per_ext_rel
10523 != o->reloc_count * address_size)
10526 /* xgettext:c-format */
10527 (_("error: %B: size of section %A is not "
10528 "multiple of address size"),
10530 bfd_set_error (bfd_error_bad_value);
10533 o->flags |= SEC_ELF_REVERSE_COPY;
10536 action_discarded = -1;
10537 if (!elf_section_ignore_discarded_relocs (o))
10538 action_discarded = (*bed->action_discarded) (o);
10540 /* Run through the relocs evaluating complex reloc symbols and
10541 looking for relocs against symbols from discarded sections
10542 or section symbols from removed link-once sections.
10543 Complain about relocs against discarded sections. Zero
10544 relocs against removed link-once sections. */
10546 rel = internal_relocs;
10547 relend = rel + o->reloc_count;
10548 for ( ; rel < relend; rel++)
10550 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10551 unsigned int s_type;
10552 asection **ps, *sec;
10553 struct elf_link_hash_entry *h = NULL;
10554 const char *sym_name;
10556 if (r_symndx == STN_UNDEF)
10559 if (r_symndx >= locsymcount
10560 || (elf_bad_symtab (input_bfd)
10561 && flinfo->sections[r_symndx] == NULL))
10563 h = sym_hashes[r_symndx - extsymoff];
10565 /* Badly formatted input files can contain relocs that
10566 reference non-existant symbols. Check here so that
10567 we do not seg fault. */
10571 /* xgettext:c-format */
10572 (_("error: %B contains a reloc (%#Lx) for section %A "
10573 "that references a non-existent global symbol"),
10574 input_bfd, rel->r_info, o);
10575 bfd_set_error (bfd_error_bad_value);
10579 while (h->root.type == bfd_link_hash_indirect
10580 || h->root.type == bfd_link_hash_warning)
10581 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10585 /* If a plugin symbol is referenced from a non-IR file,
10586 mark the symbol as undefined. Note that the
10587 linker may attach linker created dynamic sections
10588 to the plugin bfd. Symbols defined in linker
10589 created sections are not plugin symbols. */
10590 if ((h->root.non_ir_ref_regular
10591 || h->root.non_ir_ref_dynamic)
10592 && (h->root.type == bfd_link_hash_defined
10593 || h->root.type == bfd_link_hash_defweak)
10594 && (h->root.u.def.section->flags
10595 & SEC_LINKER_CREATED) == 0
10596 && h->root.u.def.section->owner != NULL
10597 && (h->root.u.def.section->owner->flags
10598 & BFD_PLUGIN) != 0)
10600 h->root.type = bfd_link_hash_undefined;
10601 h->root.u.undef.abfd = h->root.u.def.section->owner;
10605 if (h->root.type == bfd_link_hash_defined
10606 || h->root.type == bfd_link_hash_defweak)
10607 ps = &h->root.u.def.section;
10609 sym_name = h->root.root.string;
10613 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10615 s_type = ELF_ST_TYPE (sym->st_info);
10616 ps = &flinfo->sections[r_symndx];
10617 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10621 if ((s_type == STT_RELC || s_type == STT_SRELC)
10622 && !bfd_link_relocatable (flinfo->info))
10625 bfd_vma dot = (rel->r_offset
10626 + o->output_offset + o->output_section->vma);
10628 printf ("Encountered a complex symbol!");
10629 printf (" (input_bfd %s, section %s, reloc %ld\n",
10630 input_bfd->filename, o->name,
10631 (long) (rel - internal_relocs));
10632 printf (" symbol: idx %8.8lx, name %s\n",
10633 r_symndx, sym_name);
10634 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10635 (unsigned long) rel->r_info,
10636 (unsigned long) rel->r_offset);
10638 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10639 isymbuf, locsymcount, s_type == STT_SRELC))
10642 /* Symbol evaluated OK. Update to absolute value. */
10643 set_symbol_value (input_bfd, isymbuf, locsymcount,
10648 if (action_discarded != -1 && ps != NULL)
10650 /* Complain if the definition comes from a
10651 discarded section. */
10652 if ((sec = *ps) != NULL && discarded_section (sec))
10654 BFD_ASSERT (r_symndx != STN_UNDEF);
10655 if (action_discarded & COMPLAIN)
10656 (*flinfo->info->callbacks->einfo)
10657 /* xgettext:c-format */
10658 (_("%X`%s' referenced in section `%A' of %B: "
10659 "defined in discarded section `%A' of %B\n"),
10660 sym_name, o, input_bfd, sec, sec->owner);
10662 /* Try to do the best we can to support buggy old
10663 versions of gcc. Pretend that the symbol is
10664 really defined in the kept linkonce section.
10665 FIXME: This is quite broken. Modifying the
10666 symbol here means we will be changing all later
10667 uses of the symbol, not just in this section. */
10668 if (action_discarded & PRETEND)
10672 kept = _bfd_elf_check_kept_section (sec,
10684 /* Relocate the section by invoking a back end routine.
10686 The back end routine is responsible for adjusting the
10687 section contents as necessary, and (if using Rela relocs
10688 and generating a relocatable output file) adjusting the
10689 reloc addend as necessary.
10691 The back end routine does not have to worry about setting
10692 the reloc address or the reloc symbol index.
10694 The back end routine is given a pointer to the swapped in
10695 internal symbols, and can access the hash table entries
10696 for the external symbols via elf_sym_hashes (input_bfd).
10698 When generating relocatable output, the back end routine
10699 must handle STB_LOCAL/STT_SECTION symbols specially. The
10700 output symbol is going to be a section symbol
10701 corresponding to the output section, which will require
10702 the addend to be adjusted. */
10704 ret = (*relocate_section) (output_bfd, flinfo->info,
10705 input_bfd, o, contents,
10713 || bfd_link_relocatable (flinfo->info)
10714 || flinfo->info->emitrelocations)
10716 Elf_Internal_Rela *irela;
10717 Elf_Internal_Rela *irelaend, *irelamid;
10718 bfd_vma last_offset;
10719 struct elf_link_hash_entry **rel_hash;
10720 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10721 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10722 unsigned int next_erel;
10723 bfd_boolean rela_normal;
10724 struct bfd_elf_section_data *esdi, *esdo;
10726 esdi = elf_section_data (o);
10727 esdo = elf_section_data (o->output_section);
10728 rela_normal = FALSE;
10730 /* Adjust the reloc addresses and symbol indices. */
10732 irela = internal_relocs;
10733 irelaend = irela + o->reloc_count;
10734 rel_hash = esdo->rel.hashes + esdo->rel.count;
10735 /* We start processing the REL relocs, if any. When we reach
10736 IRELAMID in the loop, we switch to the RELA relocs. */
10738 if (esdi->rel.hdr != NULL)
10739 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10740 * bed->s->int_rels_per_ext_rel);
10741 rel_hash_list = rel_hash;
10742 rela_hash_list = NULL;
10743 last_offset = o->output_offset;
10744 if (!bfd_link_relocatable (flinfo->info))
10745 last_offset += o->output_section->vma;
10746 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10748 unsigned long r_symndx;
10750 Elf_Internal_Sym sym;
10752 if (next_erel == bed->s->int_rels_per_ext_rel)
10758 if (irela == irelamid)
10760 rel_hash = esdo->rela.hashes + esdo->rela.count;
10761 rela_hash_list = rel_hash;
10762 rela_normal = bed->rela_normal;
10765 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10768 if (irela->r_offset >= (bfd_vma) -2)
10770 /* This is a reloc for a deleted entry or somesuch.
10771 Turn it into an R_*_NONE reloc, at the same
10772 offset as the last reloc. elf_eh_frame.c and
10773 bfd_elf_discard_info rely on reloc offsets
10775 irela->r_offset = last_offset;
10777 irela->r_addend = 0;
10781 irela->r_offset += o->output_offset;
10783 /* Relocs in an executable have to be virtual addresses. */
10784 if (!bfd_link_relocatable (flinfo->info))
10785 irela->r_offset += o->output_section->vma;
10787 last_offset = irela->r_offset;
10789 r_symndx = irela->r_info >> r_sym_shift;
10790 if (r_symndx == STN_UNDEF)
10793 if (r_symndx >= locsymcount
10794 || (elf_bad_symtab (input_bfd)
10795 && flinfo->sections[r_symndx] == NULL))
10797 struct elf_link_hash_entry *rh;
10798 unsigned long indx;
10800 /* This is a reloc against a global symbol. We
10801 have not yet output all the local symbols, so
10802 we do not know the symbol index of any global
10803 symbol. We set the rel_hash entry for this
10804 reloc to point to the global hash table entry
10805 for this symbol. The symbol index is then
10806 set at the end of bfd_elf_final_link. */
10807 indx = r_symndx - extsymoff;
10808 rh = elf_sym_hashes (input_bfd)[indx];
10809 while (rh->root.type == bfd_link_hash_indirect
10810 || rh->root.type == bfd_link_hash_warning)
10811 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10813 /* Setting the index to -2 tells
10814 elf_link_output_extsym that this symbol is
10815 used by a reloc. */
10816 BFD_ASSERT (rh->indx < 0);
10823 /* This is a reloc against a local symbol. */
10826 sym = isymbuf[r_symndx];
10827 sec = flinfo->sections[r_symndx];
10828 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10830 /* I suppose the backend ought to fill in the
10831 section of any STT_SECTION symbol against a
10832 processor specific section. */
10833 r_symndx = STN_UNDEF;
10834 if (bfd_is_abs_section (sec))
10836 else if (sec == NULL || sec->owner == NULL)
10838 bfd_set_error (bfd_error_bad_value);
10843 asection *osec = sec->output_section;
10845 /* If we have discarded a section, the output
10846 section will be the absolute section. In
10847 case of discarded SEC_MERGE sections, use
10848 the kept section. relocate_section should
10849 have already handled discarded linkonce
10851 if (bfd_is_abs_section (osec)
10852 && sec->kept_section != NULL
10853 && sec->kept_section->output_section != NULL)
10855 osec = sec->kept_section->output_section;
10856 irela->r_addend -= osec->vma;
10859 if (!bfd_is_abs_section (osec))
10861 r_symndx = osec->target_index;
10862 if (r_symndx == STN_UNDEF)
10864 irela->r_addend += osec->vma;
10865 osec = _bfd_nearby_section (output_bfd, osec,
10867 irela->r_addend -= osec->vma;
10868 r_symndx = osec->target_index;
10873 /* Adjust the addend according to where the
10874 section winds up in the output section. */
10876 irela->r_addend += sec->output_offset;
10880 if (flinfo->indices[r_symndx] == -1)
10882 unsigned long shlink;
10887 if (flinfo->info->strip == strip_all)
10889 /* You can't do ld -r -s. */
10890 bfd_set_error (bfd_error_invalid_operation);
10894 /* This symbol was skipped earlier, but
10895 since it is needed by a reloc, we
10896 must output it now. */
10897 shlink = symtab_hdr->sh_link;
10898 name = (bfd_elf_string_from_elf_section
10899 (input_bfd, shlink, sym.st_name));
10903 osec = sec->output_section;
10905 _bfd_elf_section_from_bfd_section (output_bfd,
10907 if (sym.st_shndx == SHN_BAD)
10910 sym.st_value += sec->output_offset;
10911 if (!bfd_link_relocatable (flinfo->info))
10913 sym.st_value += osec->vma;
10914 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10916 /* STT_TLS symbols are relative to PT_TLS
10918 BFD_ASSERT (elf_hash_table (flinfo->info)
10919 ->tls_sec != NULL);
10920 sym.st_value -= (elf_hash_table (flinfo->info)
10925 indx = bfd_get_symcount (output_bfd);
10926 ret = elf_link_output_symstrtab (flinfo, name,
10932 flinfo->indices[r_symndx] = indx;
10937 r_symndx = flinfo->indices[r_symndx];
10940 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10941 | (irela->r_info & r_type_mask));
10944 /* Swap out the relocs. */
10945 input_rel_hdr = esdi->rel.hdr;
10946 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10948 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10953 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10954 * bed->s->int_rels_per_ext_rel);
10955 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10958 input_rela_hdr = esdi->rela.hdr;
10959 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10961 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10970 /* Write out the modified section contents. */
10971 if (bed->elf_backend_write_section
10972 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10975 /* Section written out. */
10977 else switch (o->sec_info_type)
10979 case SEC_INFO_TYPE_STABS:
10980 if (! (_bfd_write_section_stabs
10982 &elf_hash_table (flinfo->info)->stab_info,
10983 o, &elf_section_data (o)->sec_info, contents)))
10986 case SEC_INFO_TYPE_MERGE:
10987 if (! _bfd_write_merged_section (output_bfd, o,
10988 elf_section_data (o)->sec_info))
10991 case SEC_INFO_TYPE_EH_FRAME:
10993 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10998 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11000 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11008 if (! (o->flags & SEC_EXCLUDE))
11010 file_ptr offset = (file_ptr) o->output_offset;
11011 bfd_size_type todo = o->size;
11013 offset *= bfd_octets_per_byte (output_bfd);
11015 if ((o->flags & SEC_ELF_REVERSE_COPY))
11017 /* Reverse-copy input section to output. */
11020 todo -= address_size;
11021 if (! bfd_set_section_contents (output_bfd,
11029 offset += address_size;
11033 else if (! bfd_set_section_contents (output_bfd,
11047 /* Generate a reloc when linking an ELF file. This is a reloc
11048 requested by the linker, and does not come from any input file. This
11049 is used to build constructor and destructor tables when linking
11053 elf_reloc_link_order (bfd *output_bfd,
11054 struct bfd_link_info *info,
11055 asection *output_section,
11056 struct bfd_link_order *link_order)
11058 reloc_howto_type *howto;
11062 struct bfd_elf_section_reloc_data *reldata;
11063 struct elf_link_hash_entry **rel_hash_ptr;
11064 Elf_Internal_Shdr *rel_hdr;
11065 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11066 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11069 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11071 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11074 bfd_set_error (bfd_error_bad_value);
11078 addend = link_order->u.reloc.p->addend;
11081 reldata = &esdo->rel;
11082 else if (esdo->rela.hdr)
11083 reldata = &esdo->rela;
11090 /* Figure out the symbol index. */
11091 rel_hash_ptr = reldata->hashes + reldata->count;
11092 if (link_order->type == bfd_section_reloc_link_order)
11094 indx = link_order->u.reloc.p->u.section->target_index;
11095 BFD_ASSERT (indx != 0);
11096 *rel_hash_ptr = NULL;
11100 struct elf_link_hash_entry *h;
11102 /* Treat a reloc against a defined symbol as though it were
11103 actually against the section. */
11104 h = ((struct elf_link_hash_entry *)
11105 bfd_wrapped_link_hash_lookup (output_bfd, info,
11106 link_order->u.reloc.p->u.name,
11107 FALSE, FALSE, TRUE));
11109 && (h->root.type == bfd_link_hash_defined
11110 || h->root.type == bfd_link_hash_defweak))
11114 section = h->root.u.def.section;
11115 indx = section->output_section->target_index;
11116 *rel_hash_ptr = NULL;
11117 /* It seems that we ought to add the symbol value to the
11118 addend here, but in practice it has already been added
11119 because it was passed to constructor_callback. */
11120 addend += section->output_section->vma + section->output_offset;
11122 else if (h != NULL)
11124 /* Setting the index to -2 tells elf_link_output_extsym that
11125 this symbol is used by a reloc. */
11132 (*info->callbacks->unattached_reloc)
11133 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11138 /* If this is an inplace reloc, we must write the addend into the
11140 if (howto->partial_inplace && addend != 0)
11142 bfd_size_type size;
11143 bfd_reloc_status_type rstat;
11146 const char *sym_name;
11148 size = (bfd_size_type) bfd_get_reloc_size (howto);
11149 buf = (bfd_byte *) bfd_zmalloc (size);
11150 if (buf == NULL && size != 0)
11152 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11159 case bfd_reloc_outofrange:
11162 case bfd_reloc_overflow:
11163 if (link_order->type == bfd_section_reloc_link_order)
11164 sym_name = bfd_section_name (output_bfd,
11165 link_order->u.reloc.p->u.section);
11167 sym_name = link_order->u.reloc.p->u.name;
11168 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11169 howto->name, addend, NULL, NULL,
11174 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11176 * bfd_octets_per_byte (output_bfd),
11183 /* The address of a reloc is relative to the section in a
11184 relocatable file, and is a virtual address in an executable
11186 offset = link_order->offset;
11187 if (! bfd_link_relocatable (info))
11188 offset += output_section->vma;
11190 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11192 irel[i].r_offset = offset;
11193 irel[i].r_info = 0;
11194 irel[i].r_addend = 0;
11196 if (bed->s->arch_size == 32)
11197 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11199 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11201 rel_hdr = reldata->hdr;
11202 erel = rel_hdr->contents;
11203 if (rel_hdr->sh_type == SHT_REL)
11205 erel += reldata->count * bed->s->sizeof_rel;
11206 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11210 irel[0].r_addend = addend;
11211 erel += reldata->count * bed->s->sizeof_rela;
11212 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11221 /* Get the output vma of the section pointed to by the sh_link field. */
11224 elf_get_linked_section_vma (struct bfd_link_order *p)
11226 Elf_Internal_Shdr **elf_shdrp;
11230 s = p->u.indirect.section;
11231 elf_shdrp = elf_elfsections (s->owner);
11232 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11233 elfsec = elf_shdrp[elfsec]->sh_link;
11235 The Intel C compiler generates SHT_IA_64_UNWIND with
11236 SHF_LINK_ORDER. But it doesn't set the sh_link or
11237 sh_info fields. Hence we could get the situation
11238 where elfsec is 0. */
11241 const struct elf_backend_data *bed
11242 = get_elf_backend_data (s->owner);
11243 if (bed->link_order_error_handler)
11244 bed->link_order_error_handler
11245 /* xgettext:c-format */
11246 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11251 s = elf_shdrp[elfsec]->bfd_section;
11252 return s->output_section->vma + s->output_offset;
11257 /* Compare two sections based on the locations of the sections they are
11258 linked to. Used by elf_fixup_link_order. */
11261 compare_link_order (const void * a, const void * b)
11266 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11267 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11270 return apos > bpos;
11274 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11275 order as their linked sections. Returns false if this could not be done
11276 because an output section includes both ordered and unordered
11277 sections. Ideally we'd do this in the linker proper. */
11280 elf_fixup_link_order (bfd *abfd, asection *o)
11282 int seen_linkorder;
11285 struct bfd_link_order *p;
11287 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11289 struct bfd_link_order **sections;
11290 asection *s, *other_sec, *linkorder_sec;
11294 linkorder_sec = NULL;
11296 seen_linkorder = 0;
11297 for (p = o->map_head.link_order; p != NULL; p = p->next)
11299 if (p->type == bfd_indirect_link_order)
11301 s = p->u.indirect.section;
11303 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11304 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11305 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11306 && elfsec < elf_numsections (sub)
11307 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11308 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11322 if (seen_other && seen_linkorder)
11324 if (other_sec && linkorder_sec)
11326 /* xgettext:c-format */
11327 (_("%A has both ordered [`%A' in %B] "
11328 "and unordered [`%A' in %B] sections"),
11329 o, linkorder_sec, linkorder_sec->owner,
11330 other_sec, other_sec->owner);
11333 (_("%A has both ordered and unordered sections"), o);
11334 bfd_set_error (bfd_error_bad_value);
11339 if (!seen_linkorder)
11342 sections = (struct bfd_link_order **)
11343 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11344 if (sections == NULL)
11346 seen_linkorder = 0;
11348 for (p = o->map_head.link_order; p != NULL; p = p->next)
11350 sections[seen_linkorder++] = p;
11352 /* Sort the input sections in the order of their linked section. */
11353 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11354 compare_link_order);
11356 /* Change the offsets of the sections. */
11358 for (n = 0; n < seen_linkorder; n++)
11360 s = sections[n]->u.indirect.section;
11361 offset &= ~(bfd_vma) 0 << s->alignment_power;
11362 s->output_offset = offset / bfd_octets_per_byte (abfd);
11363 sections[n]->offset = offset;
11364 offset += sections[n]->size;
11371 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11372 Returns TRUE upon success, FALSE otherwise. */
11375 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11377 bfd_boolean ret = FALSE;
11379 const struct elf_backend_data *bed;
11381 enum bfd_architecture arch;
11383 asymbol **sympp = NULL;
11387 elf_symbol_type *osymbuf;
11389 implib_bfd = info->out_implib_bfd;
11390 bed = get_elf_backend_data (abfd);
11392 if (!bfd_set_format (implib_bfd, bfd_object))
11395 /* Use flag from executable but make it a relocatable object. */
11396 flags = bfd_get_file_flags (abfd);
11397 flags &= ~HAS_RELOC;
11398 if (!bfd_set_start_address (implib_bfd, 0)
11399 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11402 /* Copy architecture of output file to import library file. */
11403 arch = bfd_get_arch (abfd);
11404 mach = bfd_get_mach (abfd);
11405 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11406 && (abfd->target_defaulted
11407 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11410 /* Get symbol table size. */
11411 symsize = bfd_get_symtab_upper_bound (abfd);
11415 /* Read in the symbol table. */
11416 sympp = (asymbol **) xmalloc (symsize);
11417 symcount = bfd_canonicalize_symtab (abfd, sympp);
11421 /* Allow the BFD backend to copy any private header data it
11422 understands from the output BFD to the import library BFD. */
11423 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11426 /* Filter symbols to appear in the import library. */
11427 if (bed->elf_backend_filter_implib_symbols)
11428 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11431 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11434 bfd_set_error (bfd_error_no_symbols);
11435 _bfd_error_handler (_("%B: no symbol found for import library"),
11441 /* Make symbols absolute. */
11442 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11443 sizeof (*osymbuf));
11444 for (src_count = 0; src_count < symcount; src_count++)
11446 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11447 sizeof (*osymbuf));
11448 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11449 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11450 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11451 osymbuf[src_count].internal_elf_sym.st_value =
11452 osymbuf[src_count].symbol.value;
11453 sympp[src_count] = &osymbuf[src_count].symbol;
11456 bfd_set_symtab (implib_bfd, sympp, symcount);
11458 /* Allow the BFD backend to copy any private data it understands
11459 from the output BFD to the import library BFD. This is done last
11460 to permit the routine to look at the filtered symbol table. */
11461 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11464 if (!bfd_close (implib_bfd))
11475 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11479 if (flinfo->symstrtab != NULL)
11480 _bfd_elf_strtab_free (flinfo->symstrtab);
11481 if (flinfo->contents != NULL)
11482 free (flinfo->contents);
11483 if (flinfo->external_relocs != NULL)
11484 free (flinfo->external_relocs);
11485 if (flinfo->internal_relocs != NULL)
11486 free (flinfo->internal_relocs);
11487 if (flinfo->external_syms != NULL)
11488 free (flinfo->external_syms);
11489 if (flinfo->locsym_shndx != NULL)
11490 free (flinfo->locsym_shndx);
11491 if (flinfo->internal_syms != NULL)
11492 free (flinfo->internal_syms);
11493 if (flinfo->indices != NULL)
11494 free (flinfo->indices);
11495 if (flinfo->sections != NULL)
11496 free (flinfo->sections);
11497 if (flinfo->symshndxbuf != NULL)
11498 free (flinfo->symshndxbuf);
11499 for (o = obfd->sections; o != NULL; o = o->next)
11501 struct bfd_elf_section_data *esdo = elf_section_data (o);
11502 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11503 free (esdo->rel.hashes);
11504 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11505 free (esdo->rela.hashes);
11509 /* Do the final step of an ELF link. */
11512 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11514 bfd_boolean dynamic;
11515 bfd_boolean emit_relocs;
11517 struct elf_final_link_info flinfo;
11519 struct bfd_link_order *p;
11521 bfd_size_type max_contents_size;
11522 bfd_size_type max_external_reloc_size;
11523 bfd_size_type max_internal_reloc_count;
11524 bfd_size_type max_sym_count;
11525 bfd_size_type max_sym_shndx_count;
11526 Elf_Internal_Sym elfsym;
11528 Elf_Internal_Shdr *symtab_hdr;
11529 Elf_Internal_Shdr *symtab_shndx_hdr;
11530 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11531 struct elf_outext_info eoinfo;
11532 bfd_boolean merged;
11533 size_t relativecount = 0;
11534 asection *reldyn = 0;
11536 asection *attr_section = NULL;
11537 bfd_vma attr_size = 0;
11538 const char *std_attrs_section;
11539 struct elf_link_hash_table *htab = elf_hash_table (info);
11541 if (!is_elf_hash_table (htab))
11544 if (bfd_link_pic (info))
11545 abfd->flags |= DYNAMIC;
11547 dynamic = htab->dynamic_sections_created;
11548 dynobj = htab->dynobj;
11550 emit_relocs = (bfd_link_relocatable (info)
11551 || info->emitrelocations);
11553 flinfo.info = info;
11554 flinfo.output_bfd = abfd;
11555 flinfo.symstrtab = _bfd_elf_strtab_init ();
11556 if (flinfo.symstrtab == NULL)
11561 flinfo.hash_sec = NULL;
11562 flinfo.symver_sec = NULL;
11566 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11567 /* Note that dynsym_sec can be NULL (on VMS). */
11568 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11569 /* Note that it is OK if symver_sec is NULL. */
11572 flinfo.contents = NULL;
11573 flinfo.external_relocs = NULL;
11574 flinfo.internal_relocs = NULL;
11575 flinfo.external_syms = NULL;
11576 flinfo.locsym_shndx = NULL;
11577 flinfo.internal_syms = NULL;
11578 flinfo.indices = NULL;
11579 flinfo.sections = NULL;
11580 flinfo.symshndxbuf = NULL;
11581 flinfo.filesym_count = 0;
11583 /* The object attributes have been merged. Remove the input
11584 sections from the link, and set the contents of the output
11586 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11587 for (o = abfd->sections; o != NULL; o = o->next)
11589 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11590 || strcmp (o->name, ".gnu.attributes") == 0)
11592 for (p = o->map_head.link_order; p != NULL; p = p->next)
11594 asection *input_section;
11596 if (p->type != bfd_indirect_link_order)
11598 input_section = p->u.indirect.section;
11599 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11600 elf_link_input_bfd ignores this section. */
11601 input_section->flags &= ~SEC_HAS_CONTENTS;
11604 attr_size = bfd_elf_obj_attr_size (abfd);
11607 bfd_set_section_size (abfd, o, attr_size);
11609 /* Skip this section later on. */
11610 o->map_head.link_order = NULL;
11613 o->flags |= SEC_EXCLUDE;
11617 /* Count up the number of relocations we will output for each output
11618 section, so that we know the sizes of the reloc sections. We
11619 also figure out some maximum sizes. */
11620 max_contents_size = 0;
11621 max_external_reloc_size = 0;
11622 max_internal_reloc_count = 0;
11624 max_sym_shndx_count = 0;
11626 for (o = abfd->sections; o != NULL; o = o->next)
11628 struct bfd_elf_section_data *esdo = elf_section_data (o);
11629 o->reloc_count = 0;
11631 for (p = o->map_head.link_order; p != NULL; p = p->next)
11633 unsigned int reloc_count = 0;
11634 unsigned int additional_reloc_count = 0;
11635 struct bfd_elf_section_data *esdi = NULL;
11637 if (p->type == bfd_section_reloc_link_order
11638 || p->type == bfd_symbol_reloc_link_order)
11640 else if (p->type == bfd_indirect_link_order)
11644 sec = p->u.indirect.section;
11646 /* Mark all sections which are to be included in the
11647 link. This will normally be every section. We need
11648 to do this so that we can identify any sections which
11649 the linker has decided to not include. */
11650 sec->linker_mark = TRUE;
11652 if (sec->flags & SEC_MERGE)
11655 if (sec->rawsize > max_contents_size)
11656 max_contents_size = sec->rawsize;
11657 if (sec->size > max_contents_size)
11658 max_contents_size = sec->size;
11660 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11661 && (sec->owner->flags & DYNAMIC) == 0)
11665 /* We are interested in just local symbols, not all
11667 if (elf_bad_symtab (sec->owner))
11668 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11669 / bed->s->sizeof_sym);
11671 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11673 if (sym_count > max_sym_count)
11674 max_sym_count = sym_count;
11676 if (sym_count > max_sym_shndx_count
11677 && elf_symtab_shndx_list (sec->owner) != NULL)
11678 max_sym_shndx_count = sym_count;
11680 if (esdo->this_hdr.sh_type == SHT_REL
11681 || esdo->this_hdr.sh_type == SHT_RELA)
11682 /* Some backends use reloc_count in relocation sections
11683 to count particular types of relocs. Of course,
11684 reloc sections themselves can't have relocations. */
11686 else if (emit_relocs)
11688 reloc_count = sec->reloc_count;
11689 if (bed->elf_backend_count_additional_relocs)
11692 c = (*bed->elf_backend_count_additional_relocs) (sec);
11693 additional_reloc_count += c;
11696 else if (bed->elf_backend_count_relocs)
11697 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11699 esdi = elf_section_data (sec);
11701 if ((sec->flags & SEC_RELOC) != 0)
11703 size_t ext_size = 0;
11705 if (esdi->rel.hdr != NULL)
11706 ext_size = esdi->rel.hdr->sh_size;
11707 if (esdi->rela.hdr != NULL)
11708 ext_size += esdi->rela.hdr->sh_size;
11710 if (ext_size > max_external_reloc_size)
11711 max_external_reloc_size = ext_size;
11712 if (sec->reloc_count > max_internal_reloc_count)
11713 max_internal_reloc_count = sec->reloc_count;
11718 if (reloc_count == 0)
11721 reloc_count += additional_reloc_count;
11722 o->reloc_count += reloc_count;
11724 if (p->type == bfd_indirect_link_order && emit_relocs)
11728 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11729 esdo->rel.count += additional_reloc_count;
11731 if (esdi->rela.hdr)
11733 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11734 esdo->rela.count += additional_reloc_count;
11740 esdo->rela.count += reloc_count;
11742 esdo->rel.count += reloc_count;
11746 if (o->reloc_count > 0)
11747 o->flags |= SEC_RELOC;
11750 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11751 set it (this is probably a bug) and if it is set
11752 assign_section_numbers will create a reloc section. */
11753 o->flags &=~ SEC_RELOC;
11756 /* If the SEC_ALLOC flag is not set, force the section VMA to
11757 zero. This is done in elf_fake_sections as well, but forcing
11758 the VMA to 0 here will ensure that relocs against these
11759 sections are handled correctly. */
11760 if ((o->flags & SEC_ALLOC) == 0
11761 && ! o->user_set_vma)
11765 if (! bfd_link_relocatable (info) && merged)
11766 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11768 /* Figure out the file positions for everything but the symbol table
11769 and the relocs. We set symcount to force assign_section_numbers
11770 to create a symbol table. */
11771 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11772 BFD_ASSERT (! abfd->output_has_begun);
11773 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11776 /* Set sizes, and assign file positions for reloc sections. */
11777 for (o = abfd->sections; o != NULL; o = o->next)
11779 struct bfd_elf_section_data *esdo = elf_section_data (o);
11780 if ((o->flags & SEC_RELOC) != 0)
11783 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11787 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11791 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11792 to count upwards while actually outputting the relocations. */
11793 esdo->rel.count = 0;
11794 esdo->rela.count = 0;
11796 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11798 /* Cache the section contents so that they can be compressed
11799 later. Use bfd_malloc since it will be freed by
11800 bfd_compress_section_contents. */
11801 unsigned char *contents = esdo->this_hdr.contents;
11802 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11805 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11806 if (contents == NULL)
11808 esdo->this_hdr.contents = contents;
11812 /* We have now assigned file positions for all the sections except
11813 .symtab, .strtab, and non-loaded reloc sections. We start the
11814 .symtab section at the current file position, and write directly
11815 to it. We build the .strtab section in memory. */
11816 bfd_get_symcount (abfd) = 0;
11817 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11818 /* sh_name is set in prep_headers. */
11819 symtab_hdr->sh_type = SHT_SYMTAB;
11820 /* sh_flags, sh_addr and sh_size all start off zero. */
11821 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11822 /* sh_link is set in assign_section_numbers. */
11823 /* sh_info is set below. */
11824 /* sh_offset is set just below. */
11825 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11827 if (max_sym_count < 20)
11828 max_sym_count = 20;
11829 htab->strtabsize = max_sym_count;
11830 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11831 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11832 if (htab->strtab == NULL)
11834 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11836 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11837 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11839 if (info->strip != strip_all || emit_relocs)
11841 file_ptr off = elf_next_file_pos (abfd);
11843 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11845 /* Note that at this point elf_next_file_pos (abfd) is
11846 incorrect. We do not yet know the size of the .symtab section.
11847 We correct next_file_pos below, after we do know the size. */
11849 /* Start writing out the symbol table. The first symbol is always a
11851 elfsym.st_value = 0;
11852 elfsym.st_size = 0;
11853 elfsym.st_info = 0;
11854 elfsym.st_other = 0;
11855 elfsym.st_shndx = SHN_UNDEF;
11856 elfsym.st_target_internal = 0;
11857 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11858 bfd_und_section_ptr, NULL) != 1)
11861 /* Output a symbol for each section. We output these even if we are
11862 discarding local symbols, since they are used for relocs. These
11863 symbols have no names. We store the index of each one in the
11864 index field of the section, so that we can find it again when
11865 outputting relocs. */
11867 elfsym.st_size = 0;
11868 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11869 elfsym.st_other = 0;
11870 elfsym.st_value = 0;
11871 elfsym.st_target_internal = 0;
11872 for (i = 1; i < elf_numsections (abfd); i++)
11874 o = bfd_section_from_elf_index (abfd, i);
11877 o->target_index = bfd_get_symcount (abfd);
11878 elfsym.st_shndx = i;
11879 if (!bfd_link_relocatable (info))
11880 elfsym.st_value = o->vma;
11881 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11888 /* Allocate some memory to hold information read in from the input
11890 if (max_contents_size != 0)
11892 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11893 if (flinfo.contents == NULL)
11897 if (max_external_reloc_size != 0)
11899 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11900 if (flinfo.external_relocs == NULL)
11904 if (max_internal_reloc_count != 0)
11906 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11907 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11908 if (flinfo.internal_relocs == NULL)
11912 if (max_sym_count != 0)
11914 amt = max_sym_count * bed->s->sizeof_sym;
11915 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11916 if (flinfo.external_syms == NULL)
11919 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11920 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11921 if (flinfo.internal_syms == NULL)
11924 amt = max_sym_count * sizeof (long);
11925 flinfo.indices = (long int *) bfd_malloc (amt);
11926 if (flinfo.indices == NULL)
11929 amt = max_sym_count * sizeof (asection *);
11930 flinfo.sections = (asection **) bfd_malloc (amt);
11931 if (flinfo.sections == NULL)
11935 if (max_sym_shndx_count != 0)
11937 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11938 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11939 if (flinfo.locsym_shndx == NULL)
11945 bfd_vma base, end = 0;
11948 for (sec = htab->tls_sec;
11949 sec && (sec->flags & SEC_THREAD_LOCAL);
11952 bfd_size_type size = sec->size;
11955 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11957 struct bfd_link_order *ord = sec->map_tail.link_order;
11960 size = ord->offset + ord->size;
11962 end = sec->vma + size;
11964 base = htab->tls_sec->vma;
11965 /* Only align end of TLS section if static TLS doesn't have special
11966 alignment requirements. */
11967 if (bed->static_tls_alignment == 1)
11968 end = align_power (end, htab->tls_sec->alignment_power);
11969 htab->tls_size = end - base;
11972 /* Reorder SHF_LINK_ORDER sections. */
11973 for (o = abfd->sections; o != NULL; o = o->next)
11975 if (!elf_fixup_link_order (abfd, o))
11979 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11982 /* Since ELF permits relocations to be against local symbols, we
11983 must have the local symbols available when we do the relocations.
11984 Since we would rather only read the local symbols once, and we
11985 would rather not keep them in memory, we handle all the
11986 relocations for a single input file at the same time.
11988 Unfortunately, there is no way to know the total number of local
11989 symbols until we have seen all of them, and the local symbol
11990 indices precede the global symbol indices. This means that when
11991 we are generating relocatable output, and we see a reloc against
11992 a global symbol, we can not know the symbol index until we have
11993 finished examining all the local symbols to see which ones we are
11994 going to output. To deal with this, we keep the relocations in
11995 memory, and don't output them until the end of the link. This is
11996 an unfortunate waste of memory, but I don't see a good way around
11997 it. Fortunately, it only happens when performing a relocatable
11998 link, which is not the common case. FIXME: If keep_memory is set
11999 we could write the relocs out and then read them again; I don't
12000 know how bad the memory loss will be. */
12002 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12003 sub->output_has_begun = FALSE;
12004 for (o = abfd->sections; o != NULL; o = o->next)
12006 for (p = o->map_head.link_order; p != NULL; p = p->next)
12008 if (p->type == bfd_indirect_link_order
12009 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12010 == bfd_target_elf_flavour)
12011 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12013 if (! sub->output_has_begun)
12015 if (! elf_link_input_bfd (&flinfo, sub))
12017 sub->output_has_begun = TRUE;
12020 else if (p->type == bfd_section_reloc_link_order
12021 || p->type == bfd_symbol_reloc_link_order)
12023 if (! elf_reloc_link_order (abfd, info, o, p))
12028 if (! _bfd_default_link_order (abfd, info, o, p))
12030 if (p->type == bfd_indirect_link_order
12031 && (bfd_get_flavour (sub)
12032 == bfd_target_elf_flavour)
12033 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12034 != bed->s->elfclass))
12036 const char *iclass, *oclass;
12038 switch (bed->s->elfclass)
12040 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12041 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12042 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12046 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12048 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12049 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12050 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12054 bfd_set_error (bfd_error_wrong_format);
12056 /* xgettext:c-format */
12057 (_("%B: file class %s incompatible with %s"),
12058 sub, iclass, oclass);
12067 /* Free symbol buffer if needed. */
12068 if (!info->reduce_memory_overheads)
12070 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12071 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12072 && elf_tdata (sub)->symbuf)
12074 free (elf_tdata (sub)->symbuf);
12075 elf_tdata (sub)->symbuf = NULL;
12079 /* Output any global symbols that got converted to local in a
12080 version script or due to symbol visibility. We do this in a
12081 separate step since ELF requires all local symbols to appear
12082 prior to any global symbols. FIXME: We should only do this if
12083 some global symbols were, in fact, converted to become local.
12084 FIXME: Will this work correctly with the Irix 5 linker? */
12085 eoinfo.failed = FALSE;
12086 eoinfo.flinfo = &flinfo;
12087 eoinfo.localsyms = TRUE;
12088 eoinfo.file_sym_done = FALSE;
12089 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12093 /* If backend needs to output some local symbols not present in the hash
12094 table, do it now. */
12095 if (bed->elf_backend_output_arch_local_syms
12096 && (info->strip != strip_all || emit_relocs))
12098 typedef int (*out_sym_func)
12099 (void *, const char *, Elf_Internal_Sym *, asection *,
12100 struct elf_link_hash_entry *);
12102 if (! ((*bed->elf_backend_output_arch_local_syms)
12103 (abfd, info, &flinfo,
12104 (out_sym_func) elf_link_output_symstrtab)))
12108 /* That wrote out all the local symbols. Finish up the symbol table
12109 with the global symbols. Even if we want to strip everything we
12110 can, we still need to deal with those global symbols that got
12111 converted to local in a version script. */
12113 /* The sh_info field records the index of the first non local symbol. */
12114 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12117 && htab->dynsym != NULL
12118 && htab->dynsym->output_section != bfd_abs_section_ptr)
12120 Elf_Internal_Sym sym;
12121 bfd_byte *dynsym = htab->dynsym->contents;
12123 o = htab->dynsym->output_section;
12124 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12126 /* Write out the section symbols for the output sections. */
12127 if (bfd_link_pic (info)
12128 || htab->is_relocatable_executable)
12134 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12136 sym.st_target_internal = 0;
12138 for (s = abfd->sections; s != NULL; s = s->next)
12144 dynindx = elf_section_data (s)->dynindx;
12147 indx = elf_section_data (s)->this_idx;
12148 BFD_ASSERT (indx > 0);
12149 sym.st_shndx = indx;
12150 if (! check_dynsym (abfd, &sym))
12152 sym.st_value = s->vma;
12153 dest = dynsym + dynindx * bed->s->sizeof_sym;
12154 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12158 /* Write out the local dynsyms. */
12159 if (htab->dynlocal)
12161 struct elf_link_local_dynamic_entry *e;
12162 for (e = htab->dynlocal; e ; e = e->next)
12167 /* Copy the internal symbol and turn off visibility.
12168 Note that we saved a word of storage and overwrote
12169 the original st_name with the dynstr_index. */
12171 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12173 s = bfd_section_from_elf_index (e->input_bfd,
12178 elf_section_data (s->output_section)->this_idx;
12179 if (! check_dynsym (abfd, &sym))
12181 sym.st_value = (s->output_section->vma
12183 + e->isym.st_value);
12186 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12187 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12192 /* We get the global symbols from the hash table. */
12193 eoinfo.failed = FALSE;
12194 eoinfo.localsyms = FALSE;
12195 eoinfo.flinfo = &flinfo;
12196 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12200 /* If backend needs to output some symbols not present in the hash
12201 table, do it now. */
12202 if (bed->elf_backend_output_arch_syms
12203 && (info->strip != strip_all || emit_relocs))
12205 typedef int (*out_sym_func)
12206 (void *, const char *, Elf_Internal_Sym *, asection *,
12207 struct elf_link_hash_entry *);
12209 if (! ((*bed->elf_backend_output_arch_syms)
12210 (abfd, info, &flinfo,
12211 (out_sym_func) elf_link_output_symstrtab)))
12215 /* Finalize the .strtab section. */
12216 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12218 /* Swap out the .strtab section. */
12219 if (!elf_link_swap_symbols_out (&flinfo))
12222 /* Now we know the size of the symtab section. */
12223 if (bfd_get_symcount (abfd) > 0)
12225 /* Finish up and write out the symbol string table (.strtab)
12227 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12228 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12230 if (elf_symtab_shndx_list (abfd))
12232 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12234 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12236 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12237 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12238 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12239 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12240 symtab_shndx_hdr->sh_size = amt;
12242 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12245 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12246 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12251 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12252 /* sh_name was set in prep_headers. */
12253 symstrtab_hdr->sh_type = SHT_STRTAB;
12254 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12255 symstrtab_hdr->sh_addr = 0;
12256 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12257 symstrtab_hdr->sh_entsize = 0;
12258 symstrtab_hdr->sh_link = 0;
12259 symstrtab_hdr->sh_info = 0;
12260 /* sh_offset is set just below. */
12261 symstrtab_hdr->sh_addralign = 1;
12263 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12265 elf_next_file_pos (abfd) = off;
12267 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12268 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12272 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12274 _bfd_error_handler (_("%B: failed to generate import library"),
12275 info->out_implib_bfd);
12279 /* Adjust the relocs to have the correct symbol indices. */
12280 for (o = abfd->sections; o != NULL; o = o->next)
12282 struct bfd_elf_section_data *esdo = elf_section_data (o);
12285 if ((o->flags & SEC_RELOC) == 0)
12288 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12289 if (esdo->rel.hdr != NULL
12290 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12292 if (esdo->rela.hdr != NULL
12293 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12296 /* Set the reloc_count field to 0 to prevent write_relocs from
12297 trying to swap the relocs out itself. */
12298 o->reloc_count = 0;
12301 if (dynamic && info->combreloc && dynobj != NULL)
12302 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12304 /* If we are linking against a dynamic object, or generating a
12305 shared library, finish up the dynamic linking information. */
12308 bfd_byte *dyncon, *dynconend;
12310 /* Fix up .dynamic entries. */
12311 o = bfd_get_linker_section (dynobj, ".dynamic");
12312 BFD_ASSERT (o != NULL);
12314 dyncon = o->contents;
12315 dynconend = o->contents + o->size;
12316 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12318 Elf_Internal_Dyn dyn;
12321 bfd_size_type sh_size;
12324 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12331 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12333 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12335 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12336 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12339 dyn.d_un.d_val = relativecount;
12346 name = info->init_function;
12349 name = info->fini_function;
12352 struct elf_link_hash_entry *h;
12354 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12356 && (h->root.type == bfd_link_hash_defined
12357 || h->root.type == bfd_link_hash_defweak))
12359 dyn.d_un.d_ptr = h->root.u.def.value;
12360 o = h->root.u.def.section;
12361 if (o->output_section != NULL)
12362 dyn.d_un.d_ptr += (o->output_section->vma
12363 + o->output_offset);
12366 /* The symbol is imported from another shared
12367 library and does not apply to this one. */
12368 dyn.d_un.d_ptr = 0;
12375 case DT_PREINIT_ARRAYSZ:
12376 name = ".preinit_array";
12378 case DT_INIT_ARRAYSZ:
12379 name = ".init_array";
12381 case DT_FINI_ARRAYSZ:
12382 name = ".fini_array";
12384 o = bfd_get_section_by_name (abfd, name);
12388 (_("could not find section %s"), name);
12393 (_("warning: %s section has zero size"), name);
12394 dyn.d_un.d_val = o->size;
12397 case DT_PREINIT_ARRAY:
12398 name = ".preinit_array";
12400 case DT_INIT_ARRAY:
12401 name = ".init_array";
12403 case DT_FINI_ARRAY:
12404 name = ".fini_array";
12406 o = bfd_get_section_by_name (abfd, name);
12413 name = ".gnu.hash";
12422 name = ".gnu.version_d";
12425 name = ".gnu.version_r";
12428 name = ".gnu.version";
12430 o = bfd_get_linker_section (dynobj, name);
12432 if (o == NULL || bfd_is_abs_section (o->output_section))
12435 (_("could not find section %s"), name);
12438 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12441 (_("warning: section '%s' is being made into a note"), name);
12442 bfd_set_error (bfd_error_nonrepresentable_section);
12445 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12452 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12458 for (i = 1; i < elf_numsections (abfd); i++)
12460 Elf_Internal_Shdr *hdr;
12462 hdr = elf_elfsections (abfd)[i];
12463 if (hdr->sh_type == type
12464 && (hdr->sh_flags & SHF_ALLOC) != 0)
12466 sh_size += hdr->sh_size;
12468 || sh_addr > hdr->sh_addr)
12469 sh_addr = hdr->sh_addr;
12473 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12475 /* Don't count procedure linkage table relocs in the
12476 overall reloc count. */
12477 sh_size -= htab->srelplt->size;
12479 /* If the size is zero, make the address zero too.
12480 This is to avoid a glibc bug. If the backend
12481 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12482 zero, then we'll put DT_RELA at the end of
12483 DT_JMPREL. glibc will interpret the end of
12484 DT_RELA matching the end of DT_JMPREL as the
12485 case where DT_RELA includes DT_JMPREL, and for
12486 LD_BIND_NOW will decide that processing DT_RELA
12487 will process the PLT relocs too. Net result:
12488 No PLT relocs applied. */
12491 /* If .rela.plt is the first .rela section, exclude
12492 it from DT_RELA. */
12493 else if (sh_addr == (htab->srelplt->output_section->vma
12494 + htab->srelplt->output_offset))
12495 sh_addr += htab->srelplt->size;
12498 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12499 dyn.d_un.d_val = sh_size;
12501 dyn.d_un.d_ptr = sh_addr;
12504 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12508 /* If we have created any dynamic sections, then output them. */
12509 if (dynobj != NULL)
12511 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12514 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12515 if (((info->warn_shared_textrel && bfd_link_pic (info))
12516 || info->error_textrel)
12517 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12519 bfd_byte *dyncon, *dynconend;
12521 dyncon = o->contents;
12522 dynconend = o->contents + o->size;
12523 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12525 Elf_Internal_Dyn dyn;
12527 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12529 if (dyn.d_tag == DT_TEXTREL)
12531 if (info->error_textrel)
12532 info->callbacks->einfo
12533 (_("%P%X: read-only segment has dynamic relocations.\n"));
12535 info->callbacks->einfo
12536 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12542 for (o = dynobj->sections; o != NULL; o = o->next)
12544 if ((o->flags & SEC_HAS_CONTENTS) == 0
12546 || o->output_section == bfd_abs_section_ptr)
12548 if ((o->flags & SEC_LINKER_CREATED) == 0)
12550 /* At this point, we are only interested in sections
12551 created by _bfd_elf_link_create_dynamic_sections. */
12554 if (htab->stab_info.stabstr == o)
12556 if (htab->eh_info.hdr_sec == o)
12558 if (strcmp (o->name, ".dynstr") != 0)
12560 if (! bfd_set_section_contents (abfd, o->output_section,
12562 (file_ptr) o->output_offset
12563 * bfd_octets_per_byte (abfd),
12569 /* The contents of the .dynstr section are actually in a
12573 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12574 if (bfd_seek (abfd, off, SEEK_SET) != 0
12575 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12581 if (!info->resolve_section_groups)
12583 bfd_boolean failed = FALSE;
12585 BFD_ASSERT (bfd_link_relocatable (info));
12586 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12591 /* If we have optimized stabs strings, output them. */
12592 if (htab->stab_info.stabstr != NULL)
12594 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12598 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12601 elf_final_link_free (abfd, &flinfo);
12603 elf_linker (abfd) = TRUE;
12607 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12608 if (contents == NULL)
12609 return FALSE; /* Bail out and fail. */
12610 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12611 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12618 elf_final_link_free (abfd, &flinfo);
12622 /* Initialize COOKIE for input bfd ABFD. */
12625 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12626 struct bfd_link_info *info, bfd *abfd)
12628 Elf_Internal_Shdr *symtab_hdr;
12629 const struct elf_backend_data *bed;
12631 bed = get_elf_backend_data (abfd);
12632 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12634 cookie->abfd = abfd;
12635 cookie->sym_hashes = elf_sym_hashes (abfd);
12636 cookie->bad_symtab = elf_bad_symtab (abfd);
12637 if (cookie->bad_symtab)
12639 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12640 cookie->extsymoff = 0;
12644 cookie->locsymcount = symtab_hdr->sh_info;
12645 cookie->extsymoff = symtab_hdr->sh_info;
12648 if (bed->s->arch_size == 32)
12649 cookie->r_sym_shift = 8;
12651 cookie->r_sym_shift = 32;
12653 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12654 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12656 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12657 cookie->locsymcount, 0,
12659 if (cookie->locsyms == NULL)
12661 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12664 if (info->keep_memory)
12665 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12670 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12673 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12675 Elf_Internal_Shdr *symtab_hdr;
12677 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12678 if (cookie->locsyms != NULL
12679 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12680 free (cookie->locsyms);
12683 /* Initialize the relocation information in COOKIE for input section SEC
12684 of input bfd ABFD. */
12687 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12688 struct bfd_link_info *info, bfd *abfd,
12691 if (sec->reloc_count == 0)
12693 cookie->rels = NULL;
12694 cookie->relend = NULL;
12698 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12699 info->keep_memory);
12700 if (cookie->rels == NULL)
12702 cookie->rel = cookie->rels;
12703 cookie->relend = cookie->rels + sec->reloc_count;
12705 cookie->rel = cookie->rels;
12709 /* Free the memory allocated by init_reloc_cookie_rels,
12713 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12716 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12717 free (cookie->rels);
12720 /* Initialize the whole of COOKIE for input section SEC. */
12723 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12724 struct bfd_link_info *info,
12727 if (!init_reloc_cookie (cookie, info, sec->owner))
12729 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12734 fini_reloc_cookie (cookie, sec->owner);
12739 /* Free the memory allocated by init_reloc_cookie_for_section,
12743 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12746 fini_reloc_cookie_rels (cookie, sec);
12747 fini_reloc_cookie (cookie, sec->owner);
12750 /* Garbage collect unused sections. */
12752 /* Default gc_mark_hook. */
12755 _bfd_elf_gc_mark_hook (asection *sec,
12756 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12757 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12758 struct elf_link_hash_entry *h,
12759 Elf_Internal_Sym *sym)
12763 switch (h->root.type)
12765 case bfd_link_hash_defined:
12766 case bfd_link_hash_defweak:
12767 return h->root.u.def.section;
12769 case bfd_link_hash_common:
12770 return h->root.u.c.p->section;
12777 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12782 /* Return the global debug definition section. */
12785 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12786 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12787 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12788 struct elf_link_hash_entry *h,
12789 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12792 && (h->root.type == bfd_link_hash_defined
12793 || h->root.type == bfd_link_hash_defweak)
12794 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12795 return h->root.u.def.section;
12800 /* COOKIE->rel describes a relocation against section SEC, which is
12801 a section we've decided to keep. Return the section that contains
12802 the relocation symbol, or NULL if no section contains it. */
12805 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12806 elf_gc_mark_hook_fn gc_mark_hook,
12807 struct elf_reloc_cookie *cookie,
12808 bfd_boolean *start_stop)
12810 unsigned long r_symndx;
12811 struct elf_link_hash_entry *h;
12813 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12814 if (r_symndx == STN_UNDEF)
12817 if (r_symndx >= cookie->locsymcount
12818 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12820 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12823 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12827 while (h->root.type == bfd_link_hash_indirect
12828 || h->root.type == bfd_link_hash_warning)
12829 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12831 /* If this symbol is weak and there is a non-weak definition, we
12832 keep the non-weak definition because many backends put
12833 dynamic reloc info on the non-weak definition for code
12834 handling copy relocs. */
12835 if (h->is_weakalias)
12836 weakdef (h)->mark = 1;
12838 if (start_stop != NULL)
12840 /* To work around a glibc bug, mark XXX input sections
12841 when there is a reference to __start_XXX or __stop_XXX
12845 asection *s = h->u2.start_stop_section;
12846 *start_stop = !s->gc_mark;
12851 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12854 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12855 &cookie->locsyms[r_symndx]);
12858 /* COOKIE->rel describes a relocation against section SEC, which is
12859 a section we've decided to keep. Mark the section that contains
12860 the relocation symbol. */
12863 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12865 elf_gc_mark_hook_fn gc_mark_hook,
12866 struct elf_reloc_cookie *cookie)
12869 bfd_boolean start_stop = FALSE;
12871 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12872 while (rsec != NULL)
12874 if (!rsec->gc_mark)
12876 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12877 || (rsec->owner->flags & DYNAMIC) != 0)
12879 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12884 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12889 /* The mark phase of garbage collection. For a given section, mark
12890 it and any sections in this section's group, and all the sections
12891 which define symbols to which it refers. */
12894 _bfd_elf_gc_mark (struct bfd_link_info *info,
12896 elf_gc_mark_hook_fn gc_mark_hook)
12899 asection *group_sec, *eh_frame;
12903 /* Mark all the sections in the group. */
12904 group_sec = elf_section_data (sec)->next_in_group;
12905 if (group_sec && !group_sec->gc_mark)
12906 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12909 /* Look through the section relocs. */
12911 eh_frame = elf_eh_frame_section (sec->owner);
12912 if ((sec->flags & SEC_RELOC) != 0
12913 && sec->reloc_count > 0
12914 && sec != eh_frame)
12916 struct elf_reloc_cookie cookie;
12918 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12922 for (; cookie.rel < cookie.relend; cookie.rel++)
12923 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12928 fini_reloc_cookie_for_section (&cookie, sec);
12932 if (ret && eh_frame && elf_fde_list (sec))
12934 struct elf_reloc_cookie cookie;
12936 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12940 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12941 gc_mark_hook, &cookie))
12943 fini_reloc_cookie_for_section (&cookie, eh_frame);
12947 eh_frame = elf_section_eh_frame_entry (sec);
12948 if (ret && eh_frame && !eh_frame->gc_mark)
12949 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12955 /* Scan and mark sections in a special or debug section group. */
12958 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12960 /* Point to first section of section group. */
12962 /* Used to iterate the section group. */
12965 bfd_boolean is_special_grp = TRUE;
12966 bfd_boolean is_debug_grp = TRUE;
12968 /* First scan to see if group contains any section other than debug
12969 and special section. */
12970 ssec = msec = elf_next_in_group (grp);
12973 if ((msec->flags & SEC_DEBUGGING) == 0)
12974 is_debug_grp = FALSE;
12976 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12977 is_special_grp = FALSE;
12979 msec = elf_next_in_group (msec);
12981 while (msec != ssec);
12983 /* If this is a pure debug section group or pure special section group,
12984 keep all sections in this group. */
12985 if (is_debug_grp || is_special_grp)
12990 msec = elf_next_in_group (msec);
12992 while (msec != ssec);
12996 /* Keep debug and special sections. */
12999 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13000 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13004 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13007 bfd_boolean some_kept;
13008 bfd_boolean debug_frag_seen;
13009 bfd_boolean has_kept_debug_info;
13011 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13013 isec = ibfd->sections;
13014 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13017 /* Ensure all linker created sections are kept,
13018 see if any other section is already marked,
13019 and note if we have any fragmented debug sections. */
13020 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13021 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13023 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13025 else if (isec->gc_mark
13026 && (isec->flags & SEC_ALLOC) != 0
13027 && elf_section_type (isec) != SHT_NOTE)
13030 if (!debug_frag_seen
13031 && (isec->flags & SEC_DEBUGGING)
13032 && CONST_STRNEQ (isec->name, ".debug_line."))
13033 debug_frag_seen = TRUE;
13036 /* If no non-note alloc section in this file will be kept, then
13037 we can toss out the debug and special sections. */
13041 /* Keep debug and special sections like .comment when they are
13042 not part of a group. Also keep section groups that contain
13043 just debug sections or special sections. */
13044 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13046 if ((isec->flags & SEC_GROUP) != 0)
13047 _bfd_elf_gc_mark_debug_special_section_group (isec);
13048 else if (((isec->flags & SEC_DEBUGGING) != 0
13049 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13050 && elf_next_in_group (isec) == NULL)
13052 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13053 has_kept_debug_info = TRUE;
13056 /* Look for CODE sections which are going to be discarded,
13057 and find and discard any fragmented debug sections which
13058 are associated with that code section. */
13059 if (debug_frag_seen)
13060 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13061 if ((isec->flags & SEC_CODE) != 0
13062 && isec->gc_mark == 0)
13067 ilen = strlen (isec->name);
13069 /* Association is determined by the name of the debug
13070 section containing the name of the code section as
13071 a suffix. For example .debug_line.text.foo is a
13072 debug section associated with .text.foo. */
13073 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13077 if (dsec->gc_mark == 0
13078 || (dsec->flags & SEC_DEBUGGING) == 0)
13081 dlen = strlen (dsec->name);
13084 && strncmp (dsec->name + (dlen - ilen),
13085 isec->name, ilen) == 0)
13090 /* Mark debug sections referenced by kept debug sections. */
13091 if (has_kept_debug_info)
13092 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13094 && (isec->flags & SEC_DEBUGGING) != 0)
13095 if (!_bfd_elf_gc_mark (info, isec,
13096 elf_gc_mark_debug_section))
13103 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13106 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13108 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13112 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13113 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13114 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13117 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13120 for (o = sub->sections; o != NULL; o = o->next)
13122 /* When any section in a section group is kept, we keep all
13123 sections in the section group. If the first member of
13124 the section group is excluded, we will also exclude the
13126 if (o->flags & SEC_GROUP)
13128 asection *first = elf_next_in_group (o);
13129 o->gc_mark = first->gc_mark;
13135 /* Skip sweeping sections already excluded. */
13136 if (o->flags & SEC_EXCLUDE)
13139 /* Since this is early in the link process, it is simple
13140 to remove a section from the output. */
13141 o->flags |= SEC_EXCLUDE;
13143 if (info->print_gc_sections && o->size != 0)
13144 /* xgettext:c-format */
13145 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13153 /* Propagate collected vtable information. This is called through
13154 elf_link_hash_traverse. */
13157 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13159 /* Those that are not vtables. */
13161 || h->u2.vtable == NULL
13162 || h->u2.vtable->parent == NULL)
13165 /* Those vtables that do not have parents, we cannot merge. */
13166 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13169 /* If we've already been done, exit. */
13170 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13173 /* Make sure the parent's table is up to date. */
13174 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13176 if (h->u2.vtable->used == NULL)
13178 /* None of this table's entries were referenced. Re-use the
13180 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13181 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13186 bfd_boolean *cu, *pu;
13188 /* Or the parent's entries into ours. */
13189 cu = h->u2.vtable->used;
13191 pu = h->u2.vtable->parent->u2.vtable->used;
13194 const struct elf_backend_data *bed;
13195 unsigned int log_file_align;
13197 bed = get_elf_backend_data (h->root.u.def.section->owner);
13198 log_file_align = bed->s->log_file_align;
13199 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13214 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13217 bfd_vma hstart, hend;
13218 Elf_Internal_Rela *relstart, *relend, *rel;
13219 const struct elf_backend_data *bed;
13220 unsigned int log_file_align;
13222 /* Take care of both those symbols that do not describe vtables as
13223 well as those that are not loaded. */
13225 || h->u2.vtable == NULL
13226 || h->u2.vtable->parent == NULL)
13229 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13230 || h->root.type == bfd_link_hash_defweak);
13232 sec = h->root.u.def.section;
13233 hstart = h->root.u.def.value;
13234 hend = hstart + h->size;
13236 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13238 return *(bfd_boolean *) okp = FALSE;
13239 bed = get_elf_backend_data (sec->owner);
13240 log_file_align = bed->s->log_file_align;
13242 relend = relstart + sec->reloc_count;
13244 for (rel = relstart; rel < relend; ++rel)
13245 if (rel->r_offset >= hstart && rel->r_offset < hend)
13247 /* If the entry is in use, do nothing. */
13248 if (h->u2.vtable->used
13249 && (rel->r_offset - hstart) < h->u2.vtable->size)
13251 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13252 if (h->u2.vtable->used[entry])
13255 /* Otherwise, kill it. */
13256 rel->r_offset = rel->r_info = rel->r_addend = 0;
13262 /* Mark sections containing dynamically referenced symbols. When
13263 building shared libraries, we must assume that any visible symbol is
13267 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13269 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13270 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13272 if ((h->root.type == bfd_link_hash_defined
13273 || h->root.type == bfd_link_hash_defweak)
13275 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13276 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13277 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13278 && (!bfd_link_executable (info)
13279 || info->gc_keep_exported
13280 || info->export_dynamic
13283 && (*d->match) (&d->head, NULL, h->root.root.string)))
13284 && (h->versioned >= versioned
13285 || !bfd_hide_sym_by_version (info->version_info,
13286 h->root.root.string)))))
13287 h->root.u.def.section->flags |= SEC_KEEP;
13292 /* Keep all sections containing symbols undefined on the command-line,
13293 and the section containing the entry symbol. */
13296 _bfd_elf_gc_keep (struct bfd_link_info *info)
13298 struct bfd_sym_chain *sym;
13300 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13302 struct elf_link_hash_entry *h;
13304 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13305 FALSE, FALSE, FALSE);
13308 && (h->root.type == bfd_link_hash_defined
13309 || h->root.type == bfd_link_hash_defweak)
13310 && !bfd_is_abs_section (h->root.u.def.section)
13311 && !bfd_is_und_section (h->root.u.def.section))
13312 h->root.u.def.section->flags |= SEC_KEEP;
13317 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13318 struct bfd_link_info *info)
13320 bfd *ibfd = info->input_bfds;
13322 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13325 struct elf_reloc_cookie cookie;
13327 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13329 sec = ibfd->sections;
13330 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13333 if (!init_reloc_cookie (&cookie, info, ibfd))
13336 for (sec = ibfd->sections; sec; sec = sec->next)
13338 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13339 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13341 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13342 fini_reloc_cookie_rels (&cookie, sec);
13349 /* Do mark and sweep of unused sections. */
13352 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13354 bfd_boolean ok = TRUE;
13356 elf_gc_mark_hook_fn gc_mark_hook;
13357 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13358 struct elf_link_hash_table *htab;
13360 if (!bed->can_gc_sections
13361 || !is_elf_hash_table (info->hash))
13363 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13367 bed->gc_keep (info);
13368 htab = elf_hash_table (info);
13370 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13371 at the .eh_frame section if we can mark the FDEs individually. */
13372 for (sub = info->input_bfds;
13373 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13374 sub = sub->link.next)
13377 struct elf_reloc_cookie cookie;
13379 sec = sub->sections;
13380 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13382 sec = bfd_get_section_by_name (sub, ".eh_frame");
13383 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13385 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13386 if (elf_section_data (sec)->sec_info
13387 && (sec->flags & SEC_LINKER_CREATED) == 0)
13388 elf_eh_frame_section (sub) = sec;
13389 fini_reloc_cookie_for_section (&cookie, sec);
13390 sec = bfd_get_next_section_by_name (NULL, sec);
13394 /* Apply transitive closure to the vtable entry usage info. */
13395 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13399 /* Kill the vtable relocations that were not used. */
13400 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13404 /* Mark dynamically referenced symbols. */
13405 if (htab->dynamic_sections_created || info->gc_keep_exported)
13406 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13408 /* Grovel through relocs to find out who stays ... */
13409 gc_mark_hook = bed->gc_mark_hook;
13410 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13414 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13415 || elf_object_id (sub) != elf_hash_table_id (htab)
13416 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13420 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13423 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13424 Also treat note sections as a root, if the section is not part
13426 for (o = sub->sections; o != NULL; o = o->next)
13428 && (o->flags & SEC_EXCLUDE) == 0
13429 && ((o->flags & SEC_KEEP) != 0
13430 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13431 && elf_next_in_group (o) == NULL )))
13433 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13438 /* Allow the backend to mark additional target specific sections. */
13439 bed->gc_mark_extra_sections (info, gc_mark_hook);
13441 /* ... and mark SEC_EXCLUDE for those that go. */
13442 return elf_gc_sweep (abfd, info);
13445 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13448 bfd_elf_gc_record_vtinherit (bfd *abfd,
13450 struct elf_link_hash_entry *h,
13453 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13454 struct elf_link_hash_entry **search, *child;
13455 size_t extsymcount;
13456 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13458 /* The sh_info field of the symtab header tells us where the
13459 external symbols start. We don't care about the local symbols at
13461 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13462 if (!elf_bad_symtab (abfd))
13463 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13465 sym_hashes = elf_sym_hashes (abfd);
13466 sym_hashes_end = sym_hashes + extsymcount;
13468 /* Hunt down the child symbol, which is in this section at the same
13469 offset as the relocation. */
13470 for (search = sym_hashes; search != sym_hashes_end; ++search)
13472 if ((child = *search) != NULL
13473 && (child->root.type == bfd_link_hash_defined
13474 || child->root.type == bfd_link_hash_defweak)
13475 && child->root.u.def.section == sec
13476 && child->root.u.def.value == offset)
13480 /* xgettext:c-format */
13481 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13482 abfd, sec, offset);
13483 bfd_set_error (bfd_error_invalid_operation);
13487 if (!child->u2.vtable)
13489 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13490 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13491 if (!child->u2.vtable)
13496 /* This *should* only be the absolute section. It could potentially
13497 be that someone has defined a non-global vtable though, which
13498 would be bad. It isn't worth paging in the local symbols to be
13499 sure though; that case should simply be handled by the assembler. */
13501 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13504 child->u2.vtable->parent = h;
13509 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13512 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13513 asection *sec ATTRIBUTE_UNUSED,
13514 struct elf_link_hash_entry *h,
13517 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13518 unsigned int log_file_align = bed->s->log_file_align;
13522 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13523 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13528 if (addend >= h->u2.vtable->size)
13530 size_t size, bytes, file_align;
13531 bfd_boolean *ptr = h->u2.vtable->used;
13533 /* While the symbol is undefined, we have to be prepared to handle
13535 file_align = 1 << log_file_align;
13536 if (h->root.type == bfd_link_hash_undefined)
13537 size = addend + file_align;
13541 if (addend >= size)
13543 /* Oops! We've got a reference past the defined end of
13544 the table. This is probably a bug -- shall we warn? */
13545 size = addend + file_align;
13548 size = (size + file_align - 1) & -file_align;
13550 /* Allocate one extra entry for use as a "done" flag for the
13551 consolidation pass. */
13552 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13556 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13562 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13563 * sizeof (bfd_boolean));
13564 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13568 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13573 /* And arrange for that done flag to be at index -1. */
13574 h->u2.vtable->used = ptr + 1;
13575 h->u2.vtable->size = size;
13578 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13583 /* Map an ELF section header flag to its corresponding string. */
13587 flagword flag_value;
13588 } elf_flags_to_name_table;
13590 static elf_flags_to_name_table elf_flags_to_names [] =
13592 { "SHF_WRITE", SHF_WRITE },
13593 { "SHF_ALLOC", SHF_ALLOC },
13594 { "SHF_EXECINSTR", SHF_EXECINSTR },
13595 { "SHF_MERGE", SHF_MERGE },
13596 { "SHF_STRINGS", SHF_STRINGS },
13597 { "SHF_INFO_LINK", SHF_INFO_LINK},
13598 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13599 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13600 { "SHF_GROUP", SHF_GROUP },
13601 { "SHF_TLS", SHF_TLS },
13602 { "SHF_MASKOS", SHF_MASKOS },
13603 { "SHF_EXCLUDE", SHF_EXCLUDE },
13606 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13608 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13609 struct flag_info *flaginfo,
13612 const bfd_vma sh_flags = elf_section_flags (section);
13614 if (!flaginfo->flags_initialized)
13616 bfd *obfd = info->output_bfd;
13617 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13618 struct flag_info_list *tf = flaginfo->flag_list;
13620 int without_hex = 0;
13622 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13625 flagword (*lookup) (char *);
13627 lookup = bed->elf_backend_lookup_section_flags_hook;
13628 if (lookup != NULL)
13630 flagword hexval = (*lookup) ((char *) tf->name);
13634 if (tf->with == with_flags)
13635 with_hex |= hexval;
13636 else if (tf->with == without_flags)
13637 without_hex |= hexval;
13642 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13644 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13646 if (tf->with == with_flags)
13647 with_hex |= elf_flags_to_names[i].flag_value;
13648 else if (tf->with == without_flags)
13649 without_hex |= elf_flags_to_names[i].flag_value;
13656 info->callbacks->einfo
13657 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13661 flaginfo->flags_initialized = TRUE;
13662 flaginfo->only_with_flags |= with_hex;
13663 flaginfo->not_with_flags |= without_hex;
13666 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13669 if ((flaginfo->not_with_flags & sh_flags) != 0)
13675 struct alloc_got_off_arg {
13677 struct bfd_link_info *info;
13680 /* We need a special top-level link routine to convert got reference counts
13681 to real got offsets. */
13684 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13686 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13687 bfd *obfd = gofarg->info->output_bfd;
13688 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13690 if (h->got.refcount > 0)
13692 h->got.offset = gofarg->gotoff;
13693 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13696 h->got.offset = (bfd_vma) -1;
13701 /* And an accompanying bit to work out final got entry offsets once
13702 we're done. Should be called from final_link. */
13705 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13706 struct bfd_link_info *info)
13709 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13711 struct alloc_got_off_arg gofarg;
13713 BFD_ASSERT (abfd == info->output_bfd);
13715 if (! is_elf_hash_table (info->hash))
13718 /* The GOT offset is relative to the .got section, but the GOT header is
13719 put into the .got.plt section, if the backend uses it. */
13720 if (bed->want_got_plt)
13723 gotoff = bed->got_header_size;
13725 /* Do the local .got entries first. */
13726 for (i = info->input_bfds; i; i = i->link.next)
13728 bfd_signed_vma *local_got;
13729 size_t j, locsymcount;
13730 Elf_Internal_Shdr *symtab_hdr;
13732 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13735 local_got = elf_local_got_refcounts (i);
13739 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13740 if (elf_bad_symtab (i))
13741 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13743 locsymcount = symtab_hdr->sh_info;
13745 for (j = 0; j < locsymcount; ++j)
13747 if (local_got[j] > 0)
13749 local_got[j] = gotoff;
13750 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13753 local_got[j] = (bfd_vma) -1;
13757 /* Then the global .got entries. .plt refcounts are handled by
13758 adjust_dynamic_symbol */
13759 gofarg.gotoff = gotoff;
13760 gofarg.info = info;
13761 elf_link_hash_traverse (elf_hash_table (info),
13762 elf_gc_allocate_got_offsets,
13767 /* Many folk need no more in the way of final link than this, once
13768 got entry reference counting is enabled. */
13771 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13773 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13776 /* Invoke the regular ELF backend linker to do all the work. */
13777 return bfd_elf_final_link (abfd, info);
13781 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13783 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13785 if (rcookie->bad_symtab)
13786 rcookie->rel = rcookie->rels;
13788 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13790 unsigned long r_symndx;
13792 if (! rcookie->bad_symtab)
13793 if (rcookie->rel->r_offset > offset)
13795 if (rcookie->rel->r_offset != offset)
13798 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13799 if (r_symndx == STN_UNDEF)
13802 if (r_symndx >= rcookie->locsymcount
13803 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13805 struct elf_link_hash_entry *h;
13807 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13809 while (h->root.type == bfd_link_hash_indirect
13810 || h->root.type == bfd_link_hash_warning)
13811 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13813 if ((h->root.type == bfd_link_hash_defined
13814 || h->root.type == bfd_link_hash_defweak)
13815 && (h->root.u.def.section->owner != rcookie->abfd
13816 || h->root.u.def.section->kept_section != NULL
13817 || discarded_section (h->root.u.def.section)))
13822 /* It's not a relocation against a global symbol,
13823 but it could be a relocation against a local
13824 symbol for a discarded section. */
13826 Elf_Internal_Sym *isym;
13828 /* Need to: get the symbol; get the section. */
13829 isym = &rcookie->locsyms[r_symndx];
13830 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13832 && (isec->kept_section != NULL
13833 || discarded_section (isec)))
13841 /* Discard unneeded references to discarded sections.
13842 Returns -1 on error, 1 if any section's size was changed, 0 if
13843 nothing changed. This function assumes that the relocations are in
13844 sorted order, which is true for all known assemblers. */
13847 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13849 struct elf_reloc_cookie cookie;
13854 if (info->traditional_format
13855 || !is_elf_hash_table (info->hash))
13858 o = bfd_get_section_by_name (output_bfd, ".stab");
13863 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13866 || i->reloc_count == 0
13867 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13871 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13874 if (!init_reloc_cookie_for_section (&cookie, info, i))
13877 if (_bfd_discard_section_stabs (abfd, i,
13878 elf_section_data (i)->sec_info,
13879 bfd_elf_reloc_symbol_deleted_p,
13883 fini_reloc_cookie_for_section (&cookie, i);
13888 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13889 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13893 int eh_changed = 0;
13894 unsigned int eh_alignment;
13896 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13902 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13905 if (!init_reloc_cookie_for_section (&cookie, info, i))
13908 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13909 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13910 bfd_elf_reloc_symbol_deleted_p,
13914 if (i->size != i->rawsize)
13918 fini_reloc_cookie_for_section (&cookie, i);
13921 eh_alignment = 1 << o->alignment_power;
13922 /* Skip over zero terminator, and prevent empty sections from
13923 adding alignment padding at the end. */
13924 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13926 i->flags |= SEC_EXCLUDE;
13927 else if (i->size > 4)
13929 /* The last non-empty eh_frame section doesn't need padding. */
13932 /* Any prior sections must pad the last FDE out to the output
13933 section alignment. Otherwise we might have zero padding
13934 between sections, which would be seen as a terminator. */
13935 for (; i != NULL; i = i->map_tail.s)
13937 /* All but the last zero terminator should have been removed. */
13942 = (i->size + eh_alignment - 1) & -eh_alignment;
13943 if (i->size != size)
13951 elf_link_hash_traverse (elf_hash_table (info),
13952 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13955 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13957 const struct elf_backend_data *bed;
13960 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13962 s = abfd->sections;
13963 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13966 bed = get_elf_backend_data (abfd);
13968 if (bed->elf_backend_discard_info != NULL)
13970 if (!init_reloc_cookie (&cookie, info, abfd))
13973 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13976 fini_reloc_cookie (&cookie, abfd);
13980 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13981 _bfd_elf_end_eh_frame_parsing (info);
13983 if (info->eh_frame_hdr_type
13984 && !bfd_link_relocatable (info)
13985 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13992 _bfd_elf_section_already_linked (bfd *abfd,
13994 struct bfd_link_info *info)
13997 const char *name, *key;
13998 struct bfd_section_already_linked *l;
13999 struct bfd_section_already_linked_hash_entry *already_linked_list;
14001 if (sec->output_section == bfd_abs_section_ptr)
14004 flags = sec->flags;
14006 /* Return if it isn't a linkonce section. A comdat group section
14007 also has SEC_LINK_ONCE set. */
14008 if ((flags & SEC_LINK_ONCE) == 0)
14011 /* Don't put group member sections on our list of already linked
14012 sections. They are handled as a group via their group section. */
14013 if (elf_sec_group (sec) != NULL)
14016 /* For a SHT_GROUP section, use the group signature as the key. */
14018 if ((flags & SEC_GROUP) != 0
14019 && elf_next_in_group (sec) != NULL
14020 && elf_group_name (elf_next_in_group (sec)) != NULL)
14021 key = elf_group_name (elf_next_in_group (sec));
14024 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14025 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14026 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14029 /* Must be a user linkonce section that doesn't follow gcc's
14030 naming convention. In this case we won't be matching
14031 single member groups. */
14035 already_linked_list = bfd_section_already_linked_table_lookup (key);
14037 for (l = already_linked_list->entry; l != NULL; l = l->next)
14039 /* We may have 2 different types of sections on the list: group
14040 sections with a signature of <key> (<key> is some string),
14041 and linkonce sections named .gnu.linkonce.<type>.<key>.
14042 Match like sections. LTO plugin sections are an exception.
14043 They are always named .gnu.linkonce.t.<key> and match either
14044 type of section. */
14045 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14046 && ((flags & SEC_GROUP) != 0
14047 || strcmp (name, l->sec->name) == 0))
14048 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14050 /* The section has already been linked. See if we should
14051 issue a warning. */
14052 if (!_bfd_handle_already_linked (sec, l, info))
14055 if (flags & SEC_GROUP)
14057 asection *first = elf_next_in_group (sec);
14058 asection *s = first;
14062 s->output_section = bfd_abs_section_ptr;
14063 /* Record which group discards it. */
14064 s->kept_section = l->sec;
14065 s = elf_next_in_group (s);
14066 /* These lists are circular. */
14076 /* A single member comdat group section may be discarded by a
14077 linkonce section and vice versa. */
14078 if ((flags & SEC_GROUP) != 0)
14080 asection *first = elf_next_in_group (sec);
14082 if (first != NULL && elf_next_in_group (first) == first)
14083 /* Check this single member group against linkonce sections. */
14084 for (l = already_linked_list->entry; l != NULL; l = l->next)
14085 if ((l->sec->flags & SEC_GROUP) == 0
14086 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14088 first->output_section = bfd_abs_section_ptr;
14089 first->kept_section = l->sec;
14090 sec->output_section = bfd_abs_section_ptr;
14095 /* Check this linkonce section against single member groups. */
14096 for (l = already_linked_list->entry; l != NULL; l = l->next)
14097 if (l->sec->flags & SEC_GROUP)
14099 asection *first = elf_next_in_group (l->sec);
14102 && elf_next_in_group (first) == first
14103 && bfd_elf_match_symbols_in_sections (first, sec, info))
14105 sec->output_section = bfd_abs_section_ptr;
14106 sec->kept_section = first;
14111 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14112 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14113 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14114 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14115 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14116 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14117 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14118 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14119 The reverse order cannot happen as there is never a bfd with only the
14120 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14121 matter as here were are looking only for cross-bfd sections. */
14123 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14124 for (l = already_linked_list->entry; l != NULL; l = l->next)
14125 if ((l->sec->flags & SEC_GROUP) == 0
14126 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14128 if (abfd != l->sec->owner)
14129 sec->output_section = bfd_abs_section_ptr;
14133 /* This is the first section with this name. Record it. */
14134 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14135 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14136 return sec->output_section == bfd_abs_section_ptr;
14140 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14142 return sym->st_shndx == SHN_COMMON;
14146 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14152 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14154 return bfd_com_section_ptr;
14158 _bfd_elf_default_got_elt_size (bfd *abfd,
14159 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14160 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14161 bfd *ibfd ATTRIBUTE_UNUSED,
14162 unsigned long symndx ATTRIBUTE_UNUSED)
14164 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14165 return bed->s->arch_size / 8;
14168 /* Routines to support the creation of dynamic relocs. */
14170 /* Returns the name of the dynamic reloc section associated with SEC. */
14172 static const char *
14173 get_dynamic_reloc_section_name (bfd * abfd,
14175 bfd_boolean is_rela)
14178 const char *old_name = bfd_get_section_name (NULL, sec);
14179 const char *prefix = is_rela ? ".rela" : ".rel";
14181 if (old_name == NULL)
14184 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14185 sprintf (name, "%s%s", prefix, old_name);
14190 /* Returns the dynamic reloc section associated with SEC.
14191 If necessary compute the name of the dynamic reloc section based
14192 on SEC's name (looked up in ABFD's string table) and the setting
14196 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14198 bfd_boolean is_rela)
14200 asection * reloc_sec = elf_section_data (sec)->sreloc;
14202 if (reloc_sec == NULL)
14204 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14208 reloc_sec = bfd_get_linker_section (abfd, name);
14210 if (reloc_sec != NULL)
14211 elf_section_data (sec)->sreloc = reloc_sec;
14218 /* Returns the dynamic reloc section associated with SEC. If the
14219 section does not exist it is created and attached to the DYNOBJ
14220 bfd and stored in the SRELOC field of SEC's elf_section_data
14223 ALIGNMENT is the alignment for the newly created section and
14224 IS_RELA defines whether the name should be .rela.<SEC's name>
14225 or .rel.<SEC's name>. The section name is looked up in the
14226 string table associated with ABFD. */
14229 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14231 unsigned int alignment,
14233 bfd_boolean is_rela)
14235 asection * reloc_sec = elf_section_data (sec)->sreloc;
14237 if (reloc_sec == NULL)
14239 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14244 reloc_sec = bfd_get_linker_section (dynobj, name);
14246 if (reloc_sec == NULL)
14248 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14249 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14250 if ((sec->flags & SEC_ALLOC) != 0)
14251 flags |= SEC_ALLOC | SEC_LOAD;
14253 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14254 if (reloc_sec != NULL)
14256 /* _bfd_elf_get_sec_type_attr chooses a section type by
14257 name. Override as it may be wrong, eg. for a user
14258 section named "auto" we'll get ".relauto" which is
14259 seen to be a .rela section. */
14260 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14261 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14266 elf_section_data (sec)->sreloc = reloc_sec;
14272 /* Copy the ELF symbol type and other attributes for a linker script
14273 assignment from HSRC to HDEST. Generally this should be treated as
14274 if we found a strong non-dynamic definition for HDEST (except that
14275 ld ignores multiple definition errors). */
14277 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14278 struct bfd_link_hash_entry *hdest,
14279 struct bfd_link_hash_entry *hsrc)
14281 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14282 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14283 Elf_Internal_Sym isym;
14285 ehdest->type = ehsrc->type;
14286 ehdest->target_internal = ehsrc->target_internal;
14288 isym.st_other = ehsrc->other;
14289 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14292 /* Append a RELA relocation REL to section S in BFD. */
14295 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14297 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14298 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14299 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14300 bed->s->swap_reloca_out (abfd, rel, loc);
14303 /* Append a REL relocation REL to section S in BFD. */
14306 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14308 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14309 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14310 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14311 bed->s->swap_reloc_out (abfd, rel, loc);
14314 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14316 struct bfd_link_hash_entry *
14317 bfd_elf_define_start_stop (struct bfd_link_info *info,
14318 const char *symbol, asection *sec)
14320 struct elf_link_hash_entry *h;
14322 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14323 FALSE, FALSE, TRUE);
14325 && (h->root.type == bfd_link_hash_undefined
14326 || h->root.type == bfd_link_hash_undefweak
14327 || (h->ref_regular && !h->def_regular)))
14329 h->root.type = bfd_link_hash_defined;
14330 h->root.u.def.section = sec;
14331 h->root.u.def.value = 0;
14332 h->def_regular = 1;
14333 h->def_dynamic = 0;
14335 h->u2.start_stop_section = sec;
14336 if (symbol[0] == '.')
14338 /* .startof. and .sizeof. symbols are local. */
14339 const struct elf_backend_data *bed;
14340 bed = get_elf_backend_data (info->output_bfd);
14341 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14343 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14344 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;