1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2019 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. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
38 struct elf_info_failed
40 struct bfd_link_info *info;
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
47 struct elf_find_verdep_info
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
53 /* Whether we had a failure. */
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 struct elf_link_hash_entry *h;
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
89 Elf_Internal_Sym *isym;
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 && discard ? discarded_section (isec) : 1)
101 /* Define a symbol in a dynamic linkage section. */
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
157 flags = bed->dynamic_sec_flags;
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
165 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
175 if (bed->want_got_plt)
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 || !bfd_set_section_alignment (abfd, s,
180 bed->s->log_file_align))
185 /* The first bit of the global offset table is the header. */
186 s->size += bed->got_header_size;
188 if (bed->want_got_sym)
190 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
191 (or .got.plt) section. We don't do this in the linker script
192 because we don't want to define the symbol if we are not creating
193 a global offset table. */
194 h = _bfd_elf_define_linkage_sym (abfd, info, s,
195 "_GLOBAL_OFFSET_TABLE_");
196 elf_hash_table (info)->hgot = h;
204 /* Create a strtab to hold the dynamic symbol names. */
206 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
208 struct elf_link_hash_table *hash_table;
210 hash_table = elf_hash_table (info);
211 if (hash_table->dynobj == NULL)
213 /* We may not set dynobj, an input file holding linker created
214 dynamic sections to abfd, which may be a dynamic object with
215 its own dynamic sections. We need to find a normal input file
216 to hold linker created sections if possible. */
217 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
221 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
223 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
224 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
225 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
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)))
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
593 h->root.non_ir_ref_dynamic = 1;
597 /* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
601 bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
607 struct elf_link_hash_entry *h, *hv;
608 struct elf_link_hash_table *htab;
609 const struct elf_backend_data *bed;
611 if (!is_elf_hash_table (info->hash))
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
622 if (h->versioned == unknown)
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
631 h->versioned = versioned;
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
643 switch (h->root.type)
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
654 h->root.type = bfd_link_hash_new;
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
658 case bfd_link_hash_new:
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
662 the versioned symbol point to this one. */
663 bed = get_elf_backend_data (output_bfd);
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
687 h->root.type = bfd_link_hash_undefined;
689 /* If this symbol is currently defined by a dynamic object, but not
690 by a regular object, then clear out any version information because
691 the symbol will not be associated with the dynamic object any
693 if (h->def_dynamic && !h->def_regular)
694 h->verinfo.verdef = NULL;
696 /* Make sure this symbol is not garbage collected. */
703 bed = get_elf_backend_data (output_bfd);
704 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
705 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
706 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
709 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
711 if (!bfd_link_relocatable (info)
713 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
714 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
719 || bfd_link_dll (info)
720 || elf_hash_table (info)->is_relocatable_executable)
724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
727 /* If this is a weak defined symbol, and we know a corresponding
728 real symbol from the same dynamic object, make sure the real
729 symbol is also made into a dynamic symbol. */
732 struct elf_link_hash_entry *def = weakdef (h);
734 if (def->dynindx == -1
735 && !bfd_elf_link_record_dynamic_symbol (info, def))
743 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
744 success, and 2 on a failure caused by attempting to record a symbol
745 in a discarded section, eg. a discarded link-once section symbol. */
748 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
753 struct elf_link_local_dynamic_entry *entry;
754 struct elf_link_hash_table *eht;
755 struct elf_strtab_hash *dynstr;
758 Elf_External_Sym_Shndx eshndx;
759 char esym[sizeof (Elf64_External_Sym)];
761 if (! is_elf_hash_table (info->hash))
764 /* See if the entry exists already. */
765 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
766 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
769 amt = sizeof (*entry);
770 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
774 /* Go find the symbol, so that we can find it's name. */
775 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
776 1, input_indx, &entry->isym, esym, &eshndx))
778 bfd_release (input_bfd, entry);
782 if (entry->isym.st_shndx != SHN_UNDEF
783 && entry->isym.st_shndx < SHN_LORESERVE)
787 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
788 if (s == NULL || bfd_is_abs_section (s->output_section))
790 /* We can still bfd_release here as nothing has done another
791 bfd_alloc. We can't do this later in this function. */
792 bfd_release (input_bfd, entry);
797 name = (bfd_elf_string_from_elf_section
798 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
799 entry->isym.st_name));
801 dynstr = elf_hash_table (info)->dynstr;
804 /* Create a strtab to hold the dynamic symbol names. */
805 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
810 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
811 if (dynstr_index == (size_t) -1)
813 entry->isym.st_name = dynstr_index;
815 eht = elf_hash_table (info);
817 entry->next = eht->dynlocal;
818 eht->dynlocal = entry;
819 entry->input_bfd = input_bfd;
820 entry->input_indx = input_indx;
823 /* Whatever binding the symbol had before, it's now local. */
825 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
827 /* The dynindx will be set at the end of size_dynamic_sections. */
832 /* Return the dynindex of a local dynamic symbol. */
835 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
839 struct elf_link_local_dynamic_entry *e;
841 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
842 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
847 /* This function is used to renumber the dynamic symbols, if some of
848 them are removed because they are marked as local. This is called
849 via elf_link_hash_traverse. */
852 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
855 size_t *count = (size_t *) data;
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
867 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
868 STB_LOCAL binding. */
871 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
874 size_t *count = (size_t *) data;
876 if (!h->forced_local)
879 if (h->dynindx != -1)
880 h->dynindx = ++(*count);
885 /* Return true if the dynamic symbol for a given section should be
886 omitted when creating a shared library. */
888 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
889 struct bfd_link_info *info,
892 struct elf_link_hash_table *htab;
895 switch (elf_section_data (p)->this_hdr.sh_type)
899 /* If sh_type is yet undecided, assume it could be
900 SHT_PROGBITS/SHT_NOBITS. */
902 htab = elf_hash_table (info);
903 if (p == htab->tls_sec)
906 if (htab->text_index_section != NULL)
907 return p != htab->text_index_section && p != htab->data_index_section;
909 return (htab->dynobj != NULL
910 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
911 && ip->output_section == p);
913 /* There shouldn't be section relative relocations
914 against any other section. */
921 _bfd_elf_omit_section_dynsym_all
922 (bfd *output_bfd ATTRIBUTE_UNUSED,
923 struct bfd_link_info *info ATTRIBUTE_UNUSED,
924 asection *p ATTRIBUTE_UNUSED)
929 /* Assign dynsym indices. In a shared library we generate a section
930 symbol for each output section, which come first. Next come symbols
931 which have been forced to local binding. Then all of the back-end
932 allocated local dynamic syms, followed by the rest of the global
933 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
934 (This prevents the early call before elf_backend_init_index_section
935 and strip_excluded_output_sections setting dynindx for sections
936 that are stripped.) */
939 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
940 struct bfd_link_info *info,
941 unsigned long *section_sym_count)
943 unsigned long dynsymcount = 0;
944 bfd_boolean do_sec = section_sym_count != NULL;
946 if (bfd_link_pic (info)
947 || elf_hash_table (info)->is_relocatable_executable)
949 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
951 for (p = output_bfd->sections; p ; p = p->next)
952 if ((p->flags & SEC_EXCLUDE) == 0
953 && (p->flags & SEC_ALLOC) != 0
954 && elf_hash_table (info)->dynamic_relocs
955 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
959 elf_section_data (p)->dynindx = dynsymcount;
962 elf_section_data (p)->dynindx = 0;
965 *section_sym_count = dynsymcount;
967 elf_link_hash_traverse (elf_hash_table (info),
968 elf_link_renumber_local_hash_table_dynsyms,
971 if (elf_hash_table (info)->dynlocal)
973 struct elf_link_local_dynamic_entry *p;
974 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
975 p->dynindx = ++dynsymcount;
977 elf_hash_table (info)->local_dynsymcount = dynsymcount;
979 elf_link_hash_traverse (elf_hash_table (info),
980 elf_link_renumber_hash_table_dynsyms,
983 /* There is an unused NULL entry at the head of the table which we
984 must account for in our count even if the table is empty since it
985 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
989 elf_hash_table (info)->dynsymcount = dynsymcount;
993 /* Merge st_other field. */
996 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
997 const Elf_Internal_Sym *isym, asection *sec,
998 bfd_boolean definition, bfd_boolean dynamic)
1000 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1002 /* If st_other has a processor-specific meaning, specific
1003 code might be needed here. */
1004 if (bed->elf_backend_merge_symbol_attribute)
1005 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1010 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1011 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1013 /* Keep the most constraining visibility. Leave the remainder
1014 of the st_other field to elf_backend_merge_symbol_attribute. */
1015 if (symvis - 1 < hvis - 1)
1016 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1019 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1020 && (sec->flags & SEC_READONLY) == 0)
1021 h->protected_def = 1;
1024 /* This function is called when we want to merge a new symbol with an
1025 existing symbol. It handles the various cases which arise when we
1026 find a definition in a dynamic object, or when there is already a
1027 definition in a dynamic object. The new symbol is described by
1028 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1029 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1030 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1031 of an old common symbol. We set OVERRIDE if the old symbol is
1032 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1033 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1034 to change. By OK to change, we mean that we shouldn't warn if the
1035 type or size does change. */
1038 _bfd_elf_merge_symbol (bfd *abfd,
1039 struct bfd_link_info *info,
1041 Elf_Internal_Sym *sym,
1044 struct elf_link_hash_entry **sym_hash,
1046 bfd_boolean *pold_weak,
1047 unsigned int *pold_alignment,
1049 bfd_boolean *override,
1050 bfd_boolean *type_change_ok,
1051 bfd_boolean *size_change_ok,
1052 bfd_boolean *matched)
1054 asection *sec, *oldsec;
1055 struct elf_link_hash_entry *h;
1056 struct elf_link_hash_entry *hi;
1057 struct elf_link_hash_entry *flip;
1060 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1061 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1062 const struct elf_backend_data *bed;
1064 bfd_boolean default_sym = *matched;
1070 bind = ELF_ST_BIND (sym->st_info);
1072 if (! bfd_is_und_section (sec))
1073 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1075 h = ((struct elf_link_hash_entry *)
1076 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1081 bed = get_elf_backend_data (abfd);
1083 /* NEW_VERSION is the symbol version of the new symbol. */
1084 if (h->versioned != unversioned)
1086 /* Symbol version is unknown or versioned. */
1087 new_version = strrchr (name, ELF_VER_CHR);
1090 if (h->versioned == unknown)
1092 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1093 h->versioned = versioned_hidden;
1095 h->versioned = versioned;
1098 if (new_version[0] == '\0')
1102 h->versioned = unversioned;
1107 /* For merging, we only care about real symbols. But we need to make
1108 sure that indirect symbol dynamic flags are updated. */
1110 while (h->root.type == bfd_link_hash_indirect
1111 || h->root.type == bfd_link_hash_warning)
1112 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1116 if (hi == h || h->root.type == bfd_link_hash_new)
1120 /* OLD_HIDDEN is true if the existing symbol is only visible
1121 to the symbol with the same symbol version. NEW_HIDDEN is
1122 true if the new symbol is only visible to the symbol with
1123 the same symbol version. */
1124 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1125 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1126 if (!old_hidden && !new_hidden)
1127 /* The new symbol matches the existing symbol if both
1132 /* OLD_VERSION is the symbol version of the existing
1136 if (h->versioned >= versioned)
1137 old_version = strrchr (h->root.root.string,
1142 /* The new symbol matches the existing symbol if they
1143 have the same symbol version. */
1144 *matched = (old_version == new_version
1145 || (old_version != NULL
1146 && new_version != NULL
1147 && strcmp (old_version, new_version) == 0));
1152 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1157 switch (h->root.type)
1162 case bfd_link_hash_undefined:
1163 case bfd_link_hash_undefweak:
1164 oldbfd = h->root.u.undef.abfd;
1167 case bfd_link_hash_defined:
1168 case bfd_link_hash_defweak:
1169 oldbfd = h->root.u.def.section->owner;
1170 oldsec = h->root.u.def.section;
1173 case bfd_link_hash_common:
1174 oldbfd = h->root.u.c.p->section->owner;
1175 oldsec = h->root.u.c.p->section;
1177 *pold_alignment = h->root.u.c.p->alignment_power;
1180 if (poldbfd && *poldbfd == NULL)
1183 /* Differentiate strong and weak symbols. */
1184 newweak = bind == STB_WEAK;
1185 oldweak = (h->root.type == bfd_link_hash_defweak
1186 || h->root.type == bfd_link_hash_undefweak);
1188 *pold_weak = oldweak;
1190 /* We have to check it for every instance since the first few may be
1191 references and not all compilers emit symbol type for undefined
1193 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1195 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1196 respectively, is from a dynamic object. */
1198 newdyn = (abfd->flags & DYNAMIC) != 0;
1200 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1201 syms and defined syms in dynamic libraries respectively.
1202 ref_dynamic on the other hand can be set for a symbol defined in
1203 a dynamic library, and def_dynamic may not be set; When the
1204 definition in a dynamic lib is overridden by a definition in the
1205 executable use of the symbol in the dynamic lib becomes a
1206 reference to the executable symbol. */
1209 if (bfd_is_und_section (sec))
1211 if (bind != STB_WEAK)
1213 h->ref_dynamic_nonweak = 1;
1214 hi->ref_dynamic_nonweak = 1;
1219 /* Update the existing symbol only if they match. */
1222 hi->dynamic_def = 1;
1226 /* If we just created the symbol, mark it as being an ELF symbol.
1227 Other than that, there is nothing to do--there is no merge issue
1228 with a newly defined symbol--so we just return. */
1230 if (h->root.type == bfd_link_hash_new)
1236 /* In cases involving weak versioned symbols, we may wind up trying
1237 to merge a symbol with itself. Catch that here, to avoid the
1238 confusion that results if we try to override a symbol with
1239 itself. The additional tests catch cases like
1240 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1241 dynamic object, which we do want to handle here. */
1243 && (newweak || oldweak)
1244 && ((abfd->flags & DYNAMIC) == 0
1245 || !h->def_regular))
1250 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1251 else if (oldsec != NULL)
1253 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1254 indices used by MIPS ELF. */
1255 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1258 /* Handle a case where plugin_notice won't be called and thus won't
1259 set the non_ir_ref flags on the first pass over symbols. */
1261 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1262 && newdyn != olddyn)
1264 h->root.non_ir_ref_dynamic = TRUE;
1265 hi->root.non_ir_ref_dynamic = TRUE;
1268 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1269 respectively, appear to be a definition rather than reference. */
1271 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1273 olddef = (h->root.type != bfd_link_hash_undefined
1274 && h->root.type != bfd_link_hash_undefweak
1275 && h->root.type != bfd_link_hash_common);
1277 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1278 respectively, appear to be a function. */
1280 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1281 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1283 oldfunc = (h->type != STT_NOTYPE
1284 && bed->is_function_type (h->type));
1286 if (!(newfunc && oldfunc)
1287 && ELF_ST_TYPE (sym->st_info) != h->type
1288 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1289 && h->type != STT_NOTYPE
1290 && (newdef || bfd_is_com_section (sec))
1291 && (olddef || h->root.type == bfd_link_hash_common))
1293 /* If creating a default indirect symbol ("foo" or "foo@") from
1294 a dynamic versioned definition ("foo@@") skip doing so if
1295 there is an existing regular definition with a different
1296 type. We don't want, for example, a "time" variable in the
1297 executable overriding a "time" function in a shared library. */
1305 /* When adding a symbol from a regular object file after we have
1306 created indirect symbols, undo the indirection and any
1313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1314 h->forced_local = 0;
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1320 h->root.type = bfd_link_hash_undefined;
1321 h->root.u.undef.abfd = abfd;
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1332 /* Check TLS symbols. We don't check undefined symbols introduced
1333 by "ld -u" which have no type (and oldbfd NULL), and we don't
1334 check symbols from plugins because they also have no type. */
1336 && (oldbfd->flags & BFD_PLUGIN) == 0
1337 && (abfd->flags & BFD_PLUGIN) == 0
1338 && ELF_ST_TYPE (sym->st_info) != h->type
1339 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1342 bfd_boolean ntdef, tdef;
1343 asection *ntsec, *tsec;
1345 if (h->type == STT_TLS)
1366 /* xgettext:c-format */
1367 (_("%s: TLS definition in %pB section %pA "
1368 "mismatches non-TLS definition in %pB section %pA"),
1369 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1370 else if (!tdef && !ntdef)
1372 /* xgettext:c-format */
1373 (_("%s: TLS reference in %pB "
1374 "mismatches non-TLS reference in %pB"),
1375 h->root.root.string, tbfd, ntbfd);
1378 /* xgettext:c-format */
1379 (_("%s: TLS definition in %pB section %pA "
1380 "mismatches non-TLS reference in %pB"),
1381 h->root.root.string, tbfd, tsec, ntbfd);
1384 /* xgettext:c-format */
1385 (_("%s: TLS reference in %pB "
1386 "mismatches non-TLS definition in %pB section %pA"),
1387 h->root.root.string, tbfd, ntbfd, ntsec);
1389 bfd_set_error (bfd_error_bad_value);
1393 /* If the old symbol has non-default visibility, we ignore the new
1394 definition from a dynamic object. */
1396 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1397 && !bfd_is_und_section (sec))
1400 /* Make sure this symbol is dynamic. */
1402 hi->ref_dynamic = 1;
1403 /* A protected symbol has external availability. Make sure it is
1404 recorded as dynamic.
1406 FIXME: Should we check type and size for protected symbol? */
1407 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1408 return bfd_elf_link_record_dynamic_symbol (info, h);
1413 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1416 /* If the new symbol with non-default visibility comes from a
1417 relocatable file and the old definition comes from a dynamic
1418 object, we remove the old definition. */
1419 if (hi->root.type == bfd_link_hash_indirect)
1421 /* Handle the case where the old dynamic definition is
1422 default versioned. We need to copy the symbol info from
1423 the symbol with default version to the normal one if it
1424 was referenced before. */
1427 hi->root.type = h->root.type;
1428 h->root.type = bfd_link_hash_indirect;
1429 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1431 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1432 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1434 /* If the new symbol is hidden or internal, completely undo
1435 any dynamic link state. */
1436 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1437 h->forced_local = 0;
1444 /* FIXME: Should we check type and size for protected symbol? */
1454 /* If the old symbol was undefined before, then it will still be
1455 on the undefs list. If the new symbol is undefined or
1456 common, we can't make it bfd_link_hash_new here, because new
1457 undefined or common symbols will be added to the undefs list
1458 by _bfd_generic_link_add_one_symbol. Symbols may not be
1459 added twice to the undefs list. Also, if the new symbol is
1460 undefweak then we don't want to lose the strong undef. */
1461 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1463 h->root.type = bfd_link_hash_undefined;
1464 h->root.u.undef.abfd = abfd;
1468 h->root.type = bfd_link_hash_new;
1469 h->root.u.undef.abfd = NULL;
1472 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1474 /* If the new symbol is hidden or internal, completely undo
1475 any dynamic link state. */
1476 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1477 h->forced_local = 0;
1483 /* FIXME: Should we check type and size for protected symbol? */
1489 /* If a new weak symbol definition comes from a regular file and the
1490 old symbol comes from a dynamic library, we treat the new one as
1491 strong. Similarly, an old weak symbol definition from a regular
1492 file is treated as strong when the new symbol comes from a dynamic
1493 library. Further, an old weak symbol from a dynamic library is
1494 treated as strong if the new symbol is from a dynamic library.
1495 This reflects the way glibc's ld.so works.
1497 Also allow a weak symbol to override a linker script symbol
1498 defined by an early pass over the script. This is done so the
1499 linker knows the symbol is defined in an object file, for the
1500 DEFINED script function.
1502 Do this before setting *type_change_ok or *size_change_ok so that
1503 we warn properly when dynamic library symbols are overridden. */
1505 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1507 if (olddef && newdyn)
1510 /* Allow changes between different types of function symbol. */
1511 if (newfunc && oldfunc)
1512 *type_change_ok = TRUE;
1514 /* It's OK to change the type if either the existing symbol or the
1515 new symbol is weak. A type change is also OK if the old symbol
1516 is undefined and the new symbol is defined. */
1521 && h->root.type == bfd_link_hash_undefined))
1522 *type_change_ok = TRUE;
1524 /* It's OK to change the size if either the existing symbol or the
1525 new symbol is weak, or if the old symbol is undefined. */
1528 || h->root.type == bfd_link_hash_undefined)
1529 *size_change_ok = TRUE;
1531 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1532 symbol, respectively, appears to be a common symbol in a dynamic
1533 object. If a symbol appears in an uninitialized section, and is
1534 not weak, and is not a function, then it may be a common symbol
1535 which was resolved when the dynamic object was created. We want
1536 to treat such symbols specially, because they raise special
1537 considerations when setting the symbol size: if the symbol
1538 appears as a common symbol in a regular object, and the size in
1539 the regular object is larger, we must make sure that we use the
1540 larger size. This problematic case can always be avoided in C,
1541 but it must be handled correctly when using Fortran shared
1544 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1545 likewise for OLDDYNCOMMON and OLDDEF.
1547 Note that this test is just a heuristic, and that it is quite
1548 possible to have an uninitialized symbol in a shared object which
1549 is really a definition, rather than a common symbol. This could
1550 lead to some minor confusion when the symbol really is a common
1551 symbol in some regular object. However, I think it will be
1557 && (sec->flags & SEC_ALLOC) != 0
1558 && (sec->flags & SEC_LOAD) == 0
1561 newdyncommon = TRUE;
1563 newdyncommon = FALSE;
1567 && h->root.type == bfd_link_hash_defined
1569 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1570 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1573 olddyncommon = TRUE;
1575 olddyncommon = FALSE;
1577 /* We now know everything about the old and new symbols. We ask the
1578 backend to check if we can merge them. */
1579 if (bed->merge_symbol != NULL)
1581 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1586 /* There are multiple definitions of a normal symbol. Skip the
1587 default symbol as well as definition from an IR object. */
1588 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1589 && !default_sym && h->def_regular
1591 && (oldbfd->flags & BFD_PLUGIN) != 0
1592 && (abfd->flags & BFD_PLUGIN) == 0))
1594 /* Handle a multiple definition. */
1595 (*info->callbacks->multiple_definition) (info, &h->root,
1596 abfd, sec, *pvalue);
1601 /* If both the old and the new symbols look like common symbols in a
1602 dynamic object, set the size of the symbol to the larger of the
1607 && sym->st_size != h->size)
1609 /* Since we think we have two common symbols, issue a multiple
1610 common warning if desired. Note that we only warn if the
1611 size is different. If the size is the same, we simply let
1612 the old symbol override the new one as normally happens with
1613 symbols defined in dynamic objects. */
1615 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1616 bfd_link_hash_common, sym->st_size);
1617 if (sym->st_size > h->size)
1618 h->size = sym->st_size;
1620 *size_change_ok = TRUE;
1623 /* If we are looking at a dynamic object, and we have found a
1624 definition, we need to see if the symbol was already defined by
1625 some other object. If so, we want to use the existing
1626 definition, and we do not want to report a multiple symbol
1627 definition error; we do this by clobbering *PSEC to be
1628 bfd_und_section_ptr.
1630 We treat a common symbol as a definition if the symbol in the
1631 shared library is a function, since common symbols always
1632 represent variables; this can cause confusion in principle, but
1633 any such confusion would seem to indicate an erroneous program or
1634 shared library. We also permit a common symbol in a regular
1635 object to override a weak symbol in a shared object. */
1640 || (h->root.type == bfd_link_hash_common
1641 && (newweak || newfunc))))
1645 newdyncommon = FALSE;
1647 *psec = sec = bfd_und_section_ptr;
1648 *size_change_ok = TRUE;
1650 /* If we get here when the old symbol is a common symbol, then
1651 we are explicitly letting it override a weak symbol or
1652 function in a dynamic object, and we don't want to warn about
1653 a type change. If the old symbol is a defined symbol, a type
1654 change warning may still be appropriate. */
1656 if (h->root.type == bfd_link_hash_common)
1657 *type_change_ok = TRUE;
1660 /* Handle the special case of an old common symbol merging with a
1661 new symbol which looks like a common symbol in a shared object.
1662 We change *PSEC and *PVALUE to make the new symbol look like a
1663 common symbol, and let _bfd_generic_link_add_one_symbol do the
1667 && h->root.type == bfd_link_hash_common)
1671 newdyncommon = FALSE;
1672 *pvalue = sym->st_size;
1673 *psec = sec = bed->common_section (oldsec);
1674 *size_change_ok = TRUE;
1677 /* Skip weak definitions of symbols that are already defined. */
1678 if (newdef && olddef && newweak)
1680 /* Don't skip new non-IR weak syms. */
1681 if (!(oldbfd != NULL
1682 && (oldbfd->flags & BFD_PLUGIN) != 0
1683 && (abfd->flags & BFD_PLUGIN) == 0))
1689 /* Merge st_other. If the symbol already has a dynamic index,
1690 but visibility says it should not be visible, turn it into a
1692 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1693 if (h->dynindx != -1)
1694 switch (ELF_ST_VISIBILITY (h->other))
1698 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1703 /* If the old symbol is from a dynamic object, and the new symbol is
1704 a definition which is not from a dynamic object, then the new
1705 symbol overrides the old symbol. Symbols from regular files
1706 always take precedence over symbols from dynamic objects, even if
1707 they are defined after the dynamic object in the link.
1709 As above, we again permit a common symbol in a regular object to
1710 override a definition in a shared object if the shared object
1711 symbol is a function or is weak. */
1716 || (bfd_is_com_section (sec)
1717 && (oldweak || oldfunc)))
1722 /* Change the hash table entry to undefined, and let
1723 _bfd_generic_link_add_one_symbol do the right thing with the
1726 h->root.type = bfd_link_hash_undefined;
1727 h->root.u.undef.abfd = h->root.u.def.section->owner;
1728 *size_change_ok = TRUE;
1731 olddyncommon = FALSE;
1733 /* We again permit a type change when a common symbol may be
1734 overriding a function. */
1736 if (bfd_is_com_section (sec))
1740 /* If a common symbol overrides a function, make sure
1741 that it isn't defined dynamically nor has type
1744 h->type = STT_NOTYPE;
1746 *type_change_ok = TRUE;
1749 if (hi->root.type == bfd_link_hash_indirect)
1752 /* This union may have been set to be non-NULL when this symbol
1753 was seen in a dynamic object. We must force the union to be
1754 NULL, so that it is correct for a regular symbol. */
1755 h->verinfo.vertree = NULL;
1758 /* Handle the special case of a new common symbol merging with an
1759 old symbol that looks like it might be a common symbol defined in
1760 a shared object. Note that we have already handled the case in
1761 which a new common symbol should simply override the definition
1762 in the shared library. */
1765 && bfd_is_com_section (sec)
1768 /* It would be best if we could set the hash table entry to a
1769 common symbol, but we don't know what to use for the section
1770 or the alignment. */
1771 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1772 bfd_link_hash_common, sym->st_size);
1774 /* If the presumed common symbol in the dynamic object is
1775 larger, pretend that the new symbol has its size. */
1777 if (h->size > *pvalue)
1780 /* We need to remember the alignment required by the symbol
1781 in the dynamic object. */
1782 BFD_ASSERT (pold_alignment);
1783 *pold_alignment = h->root.u.def.section->alignment_power;
1786 olddyncommon = FALSE;
1788 h->root.type = bfd_link_hash_undefined;
1789 h->root.u.undef.abfd = h->root.u.def.section->owner;
1791 *size_change_ok = TRUE;
1792 *type_change_ok = TRUE;
1794 if (hi->root.type == bfd_link_hash_indirect)
1797 h->verinfo.vertree = NULL;
1802 /* Handle the case where we had a versioned symbol in a dynamic
1803 library and now find a definition in a normal object. In this
1804 case, we make the versioned symbol point to the normal one. */
1805 flip->root.type = h->root.type;
1806 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1807 h->root.type = bfd_link_hash_indirect;
1808 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1809 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1813 flip->ref_dynamic = 1;
1820 /* This function is called to create an indirect symbol from the
1821 default for the symbol with the default version if needed. The
1822 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1823 set DYNSYM if the new indirect symbol is dynamic. */
1826 _bfd_elf_add_default_symbol (bfd *abfd,
1827 struct bfd_link_info *info,
1828 struct elf_link_hash_entry *h,
1830 Elf_Internal_Sym *sym,
1834 bfd_boolean *dynsym)
1836 bfd_boolean type_change_ok;
1837 bfd_boolean size_change_ok;
1840 struct elf_link_hash_entry *hi;
1841 struct bfd_link_hash_entry *bh;
1842 const struct elf_backend_data *bed;
1843 bfd_boolean collect;
1844 bfd_boolean dynamic;
1845 bfd_boolean override;
1847 size_t len, shortlen;
1849 bfd_boolean matched;
1851 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1854 /* If this symbol has a version, and it is the default version, we
1855 create an indirect symbol from the default name to the fully
1856 decorated name. This will cause external references which do not
1857 specify a version to be bound to this version of the symbol. */
1858 p = strchr (name, ELF_VER_CHR);
1859 if (h->versioned == unknown)
1863 h->versioned = unversioned;
1868 if (p[1] != ELF_VER_CHR)
1870 h->versioned = versioned_hidden;
1874 h->versioned = versioned;
1879 /* PR ld/19073: We may see an unversioned definition after the
1885 bed = get_elf_backend_data (abfd);
1886 collect = bed->collect;
1887 dynamic = (abfd->flags & DYNAMIC) != 0;
1889 shortlen = p - name;
1890 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1891 if (shortname == NULL)
1893 memcpy (shortname, name, shortlen);
1894 shortname[shortlen] = '\0';
1896 /* We are going to create a new symbol. Merge it with any existing
1897 symbol with this name. For the purposes of the merge, act as
1898 though we were defining the symbol we just defined, although we
1899 actually going to define an indirect symbol. */
1900 type_change_ok = FALSE;
1901 size_change_ok = FALSE;
1904 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1905 &hi, poldbfd, NULL, NULL, &skip, &override,
1906 &type_change_ok, &size_change_ok, &matched))
1912 if (hi->def_regular)
1914 /* If the undecorated symbol will have a version added by a
1915 script different to H, then don't indirect to/from the
1916 undecorated symbol. This isn't ideal because we may not yet
1917 have seen symbol versions, if given by a script on the
1918 command line rather than via --version-script. */
1919 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1924 = bfd_find_version_for_sym (info->version_info,
1925 hi->root.root.string, &hide);
1926 if (hi->verinfo.vertree != NULL && hide)
1928 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1932 if (hi->verinfo.vertree != NULL
1933 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1939 /* Add the default symbol if not performing a relocatable link. */
1940 if (! bfd_link_relocatable (info))
1943 if (bh->type == bfd_link_hash_defined
1944 && bh->u.def.section->owner != NULL
1945 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1947 /* Mark the previous definition from IR object as
1948 undefined so that the generic linker will override
1950 bh->type = bfd_link_hash_undefined;
1951 bh->u.undef.abfd = bh->u.def.section->owner;
1953 if (! (_bfd_generic_link_add_one_symbol
1954 (info, abfd, shortname, BSF_INDIRECT,
1955 bfd_ind_section_ptr,
1956 0, name, FALSE, collect, &bh)))
1958 hi = (struct elf_link_hash_entry *) bh;
1963 /* In this case the symbol named SHORTNAME is overriding the
1964 indirect symbol we want to add. We were planning on making
1965 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1966 is the name without a version. NAME is the fully versioned
1967 name, and it is the default version.
1969 Overriding means that we already saw a definition for the
1970 symbol SHORTNAME in a regular object, and it is overriding
1971 the symbol defined in the dynamic object.
1973 When this happens, we actually want to change NAME, the
1974 symbol we just added, to refer to SHORTNAME. This will cause
1975 references to NAME in the shared object to become references
1976 to SHORTNAME in the regular object. This is what we expect
1977 when we override a function in a shared object: that the
1978 references in the shared object will be mapped to the
1979 definition in the regular object. */
1981 while (hi->root.type == bfd_link_hash_indirect
1982 || hi->root.type == bfd_link_hash_warning)
1983 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1985 h->root.type = bfd_link_hash_indirect;
1986 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1990 hi->ref_dynamic = 1;
1994 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1999 /* Now set HI to H, so that the following code will set the
2000 other fields correctly. */
2004 /* Check if HI is a warning symbol. */
2005 if (hi->root.type == bfd_link_hash_warning)
2006 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2008 /* If there is a duplicate definition somewhere, then HI may not
2009 point to an indirect symbol. We will have reported an error to
2010 the user in that case. */
2012 if (hi->root.type == bfd_link_hash_indirect)
2014 struct elf_link_hash_entry *ht;
2016 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2017 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2019 /* A reference to the SHORTNAME symbol from a dynamic library
2020 will be satisfied by the versioned symbol at runtime. In
2021 effect, we have a reference to the versioned symbol. */
2022 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2023 hi->dynamic_def |= ht->dynamic_def;
2025 /* See if the new flags lead us to realize that the symbol must
2031 if (! bfd_link_executable (info)
2038 if (hi->ref_regular)
2044 /* We also need to define an indirection from the nondefault version
2048 len = strlen (name);
2049 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2050 if (shortname == NULL)
2052 memcpy (shortname, name, shortlen);
2053 memcpy (shortname + shortlen, p + 1, len - shortlen);
2055 /* Once again, merge with any existing symbol. */
2056 type_change_ok = FALSE;
2057 size_change_ok = FALSE;
2059 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2060 &hi, poldbfd, NULL, NULL, &skip, &override,
2061 &type_change_ok, &size_change_ok, &matched))
2069 /* Here SHORTNAME is a versioned name, so we don't expect to see
2070 the type of override we do in the case above unless it is
2071 overridden by a versioned definition. */
2072 if (hi->root.type != bfd_link_hash_defined
2073 && hi->root.type != bfd_link_hash_defweak)
2075 /* xgettext:c-format */
2076 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2082 if (! (_bfd_generic_link_add_one_symbol
2083 (info, abfd, shortname, BSF_INDIRECT,
2084 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2086 hi = (struct elf_link_hash_entry *) bh;
2088 /* If there is a duplicate definition somewhere, then HI may not
2089 point to an indirect symbol. We will have reported an error
2090 to the user in that case. */
2092 if (hi->root.type == bfd_link_hash_indirect)
2094 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2095 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2096 hi->dynamic_def |= h->dynamic_def;
2098 /* See if the new flags lead us to realize that the symbol
2104 if (! bfd_link_executable (info)
2110 if (hi->ref_regular)
2120 /* This routine is used to export all defined symbols into the dynamic
2121 symbol table. It is called via elf_link_hash_traverse. */
2124 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2126 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2128 /* Ignore indirect symbols. These are added by the versioning code. */
2129 if (h->root.type == bfd_link_hash_indirect)
2132 /* Ignore this if we won't export it. */
2133 if (!eif->info->export_dynamic && !h->dynamic)
2136 if (h->dynindx == -1
2137 && (h->def_regular || h->ref_regular)
2138 && ! bfd_hide_sym_by_version (eif->info->version_info,
2139 h->root.root.string))
2141 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2151 /* Look through the symbols which are defined in other shared
2152 libraries and referenced here. Update the list of version
2153 dependencies. This will be put into the .gnu.version_r section.
2154 This function is called via elf_link_hash_traverse. */
2157 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2160 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2161 Elf_Internal_Verneed *t;
2162 Elf_Internal_Vernaux *a;
2165 /* We only care about symbols defined in shared objects with version
2170 || h->verinfo.verdef == NULL
2171 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2172 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2175 /* See if we already know about this version. */
2176 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2180 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2183 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2184 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2190 /* This is a new version. Add it to tree we are building. */
2195 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2198 rinfo->failed = TRUE;
2202 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2203 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2204 elf_tdata (rinfo->info->output_bfd)->verref = t;
2208 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2211 rinfo->failed = TRUE;
2215 /* Note that we are copying a string pointer here, and testing it
2216 above. If bfd_elf_string_from_elf_section is ever changed to
2217 discard the string data when low in memory, this will have to be
2219 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2221 a->vna_flags = h->verinfo.verdef->vd_flags;
2222 a->vna_nextptr = t->vn_auxptr;
2224 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2227 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2234 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2235 hidden. Set *T_P to NULL if there is no match. */
2238 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2239 struct elf_link_hash_entry *h,
2240 const char *version_p,
2241 struct bfd_elf_version_tree **t_p,
2244 struct bfd_elf_version_tree *t;
2246 /* Look for the version. If we find it, it is no longer weak. */
2247 for (t = info->version_info; t != NULL; t = t->next)
2249 if (strcmp (t->name, version_p) == 0)
2253 struct bfd_elf_version_expr *d;
2255 len = version_p - h->root.root.string;
2256 alc = (char *) bfd_malloc (len);
2259 memcpy (alc, h->root.root.string, len - 1);
2260 alc[len - 1] = '\0';
2261 if (alc[len - 2] == ELF_VER_CHR)
2262 alc[len - 2] = '\0';
2264 h->verinfo.vertree = t;
2268 if (t->globals.list != NULL)
2269 d = (*t->match) (&t->globals, NULL, alc);
2271 /* See if there is anything to force this symbol to
2273 if (d == NULL && t->locals.list != NULL)
2275 d = (*t->match) (&t->locals, NULL, alc);
2278 && ! info->export_dynamic)
2292 /* Return TRUE if the symbol H is hidden by version script. */
2295 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2296 struct elf_link_hash_entry *h)
2299 bfd_boolean hide = FALSE;
2300 const struct elf_backend_data *bed
2301 = get_elf_backend_data (info->output_bfd);
2303 /* Version script only hides symbols defined in regular objects. */
2304 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2307 p = strchr (h->root.root.string, ELF_VER_CHR);
2308 if (p != NULL && h->verinfo.vertree == NULL)
2310 struct bfd_elf_version_tree *t;
2313 if (*p == ELF_VER_CHR)
2317 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2321 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2326 /* If we don't have a version for this symbol, see if we can find
2328 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2331 = bfd_find_version_for_sym (info->version_info,
2332 h->root.root.string, &hide);
2333 if (h->verinfo.vertree != NULL && hide)
2335 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2343 /* Figure out appropriate versions for all the symbols. We may not
2344 have the version number script until we have read all of the input
2345 files, so until that point we don't know which symbols should be
2346 local. This function is called via elf_link_hash_traverse. */
2349 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2351 struct elf_info_failed *sinfo;
2352 struct bfd_link_info *info;
2353 const struct elf_backend_data *bed;
2354 struct elf_info_failed eif;
2358 sinfo = (struct elf_info_failed *) data;
2361 /* Fix the symbol flags. */
2364 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2367 sinfo->failed = TRUE;
2371 bed = get_elf_backend_data (info->output_bfd);
2373 /* We only need version numbers for symbols defined in regular
2375 if (!h->def_regular)
2377 /* Hide symbols defined in discarded input sections. */
2378 if ((h->root.type == bfd_link_hash_defined
2379 || h->root.type == bfd_link_hash_defweak)
2380 && discarded_section (h->root.u.def.section))
2381 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2386 p = strchr (h->root.root.string, ELF_VER_CHR);
2387 if (p != NULL && h->verinfo.vertree == NULL)
2389 struct bfd_elf_version_tree *t;
2392 if (*p == ELF_VER_CHR)
2395 /* If there is no version string, we can just return out. */
2399 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2401 sinfo->failed = TRUE;
2406 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2408 /* If we are building an application, we need to create a
2409 version node for this version. */
2410 if (t == NULL && bfd_link_executable (info))
2412 struct bfd_elf_version_tree **pp;
2415 /* If we aren't going to export this symbol, we don't need
2416 to worry about it. */
2417 if (h->dynindx == -1)
2420 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2424 sinfo->failed = TRUE;
2429 t->name_indx = (unsigned int) -1;
2433 /* Don't count anonymous version tag. */
2434 if (sinfo->info->version_info != NULL
2435 && sinfo->info->version_info->vernum == 0)
2437 for (pp = &sinfo->info->version_info;
2441 t->vernum = version_index;
2445 h->verinfo.vertree = t;
2449 /* We could not find the version for a symbol when
2450 generating a shared archive. Return an error. */
2452 /* xgettext:c-format */
2453 (_("%pB: version node not found for symbol %s"),
2454 info->output_bfd, h->root.root.string);
2455 bfd_set_error (bfd_error_bad_value);
2456 sinfo->failed = TRUE;
2461 /* If we don't have a version for this symbol, see if we can find
2464 && h->verinfo.vertree == NULL
2465 && sinfo->info->version_info != NULL)
2468 = bfd_find_version_for_sym (sinfo->info->version_info,
2469 h->root.root.string, &hide);
2470 if (h->verinfo.vertree != NULL && hide)
2471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2477 /* Read and swap the relocs from the section indicated by SHDR. This
2478 may be either a REL or a RELA section. The relocations are
2479 translated into RELA relocations and stored in INTERNAL_RELOCS,
2480 which should have already been allocated to contain enough space.
2481 The EXTERNAL_RELOCS are a buffer where the external form of the
2482 relocations should be stored.
2484 Returns FALSE if something goes wrong. */
2487 elf_link_read_relocs_from_section (bfd *abfd,
2489 Elf_Internal_Shdr *shdr,
2490 void *external_relocs,
2491 Elf_Internal_Rela *internal_relocs)
2493 const struct elf_backend_data *bed;
2494 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2495 const bfd_byte *erela;
2496 const bfd_byte *erelaend;
2497 Elf_Internal_Rela *irela;
2498 Elf_Internal_Shdr *symtab_hdr;
2501 /* Position ourselves at the start of the section. */
2502 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2505 /* Read the relocations. */
2506 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2509 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2510 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2512 bed = get_elf_backend_data (abfd);
2514 /* Convert the external relocations to the internal format. */
2515 if (shdr->sh_entsize == bed->s->sizeof_rel)
2516 swap_in = bed->s->swap_reloc_in;
2517 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2518 swap_in = bed->s->swap_reloca_in;
2521 bfd_set_error (bfd_error_wrong_format);
2525 erela = (const bfd_byte *) external_relocs;
2526 /* Setting erelaend like this and comparing with <= handles case of
2527 a fuzzed object with sh_size not a multiple of sh_entsize. */
2528 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2529 irela = internal_relocs;
2530 while (erela <= erelaend)
2534 (*swap_in) (abfd, erela, irela);
2535 r_symndx = ELF32_R_SYM (irela->r_info);
2536 if (bed->s->arch_size == 64)
2540 if ((size_t) r_symndx >= nsyms)
2543 /* xgettext:c-format */
2544 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2545 " for offset %#" PRIx64 " in section `%pA'"),
2546 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2547 (uint64_t) irela->r_offset, sec);
2548 bfd_set_error (bfd_error_bad_value);
2552 else if (r_symndx != STN_UNDEF)
2555 /* xgettext:c-format */
2556 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2557 " for offset %#" PRIx64 " in section `%pA'"
2558 " when the object file has no symbol table"),
2559 abfd, (uint64_t) r_symndx,
2560 (uint64_t) irela->r_offset, sec);
2561 bfd_set_error (bfd_error_bad_value);
2564 irela += bed->s->int_rels_per_ext_rel;
2565 erela += shdr->sh_entsize;
2571 /* Read and swap the relocs for a section O. They may have been
2572 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2573 not NULL, they are used as buffers to read into. They are known to
2574 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2575 the return value is allocated using either malloc or bfd_alloc,
2576 according to the KEEP_MEMORY argument. If O has two relocation
2577 sections (both REL and RELA relocations), then the REL_HDR
2578 relocations will appear first in INTERNAL_RELOCS, followed by the
2579 RELA_HDR relocations. */
2582 _bfd_elf_link_read_relocs (bfd *abfd,
2584 void *external_relocs,
2585 Elf_Internal_Rela *internal_relocs,
2586 bfd_boolean keep_memory)
2588 void *alloc1 = NULL;
2589 Elf_Internal_Rela *alloc2 = NULL;
2590 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2591 struct bfd_elf_section_data *esdo = elf_section_data (o);
2592 Elf_Internal_Rela *internal_rela_relocs;
2594 if (esdo->relocs != NULL)
2595 return esdo->relocs;
2597 if (o->reloc_count == 0)
2600 if (internal_relocs == NULL)
2604 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2606 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2608 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2609 if (internal_relocs == NULL)
2613 if (external_relocs == NULL)
2615 bfd_size_type size = 0;
2618 size += esdo->rel.hdr->sh_size;
2620 size += esdo->rela.hdr->sh_size;
2622 alloc1 = bfd_malloc (size);
2625 external_relocs = alloc1;
2628 internal_rela_relocs = internal_relocs;
2631 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2635 external_relocs = (((bfd_byte *) external_relocs)
2636 + esdo->rel.hdr->sh_size);
2637 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2638 * bed->s->int_rels_per_ext_rel);
2642 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2644 internal_rela_relocs)))
2647 /* Cache the results for next time, if we can. */
2649 esdo->relocs = internal_relocs;
2654 /* Don't free alloc2, since if it was allocated we are passing it
2655 back (under the name of internal_relocs). */
2657 return internal_relocs;
2665 bfd_release (abfd, alloc2);
2672 /* Compute the size of, and allocate space for, REL_HDR which is the
2673 section header for a section containing relocations for O. */
2676 _bfd_elf_link_size_reloc_section (bfd *abfd,
2677 struct bfd_elf_section_reloc_data *reldata)
2679 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2681 /* That allows us to calculate the size of the section. */
2682 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2684 /* The contents field must last into write_object_contents, so we
2685 allocate it with bfd_alloc rather than malloc. Also since we
2686 cannot be sure that the contents will actually be filled in,
2687 we zero the allocated space. */
2688 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2689 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2692 if (reldata->hashes == NULL && reldata->count)
2694 struct elf_link_hash_entry **p;
2696 p = ((struct elf_link_hash_entry **)
2697 bfd_zmalloc (reldata->count * sizeof (*p)));
2701 reldata->hashes = p;
2707 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2708 originated from the section given by INPUT_REL_HDR) to the
2712 _bfd_elf_link_output_relocs (bfd *output_bfd,
2713 asection *input_section,
2714 Elf_Internal_Shdr *input_rel_hdr,
2715 Elf_Internal_Rela *internal_relocs,
2716 struct elf_link_hash_entry **rel_hash
2719 Elf_Internal_Rela *irela;
2720 Elf_Internal_Rela *irelaend;
2722 struct bfd_elf_section_reloc_data *output_reldata;
2723 asection *output_section;
2724 const struct elf_backend_data *bed;
2725 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2726 struct bfd_elf_section_data *esdo;
2728 output_section = input_section->output_section;
2730 bed = get_elf_backend_data (output_bfd);
2731 esdo = elf_section_data (output_section);
2732 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2734 output_reldata = &esdo->rel;
2735 swap_out = bed->s->swap_reloc_out;
2737 else if (esdo->rela.hdr
2738 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2740 output_reldata = &esdo->rela;
2741 swap_out = bed->s->swap_reloca_out;
2746 /* xgettext:c-format */
2747 (_("%pB: relocation size mismatch in %pB section %pA"),
2748 output_bfd, input_section->owner, input_section);
2749 bfd_set_error (bfd_error_wrong_format);
2753 erel = output_reldata->hdr->contents;
2754 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2755 irela = internal_relocs;
2756 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2757 * bed->s->int_rels_per_ext_rel);
2758 while (irela < irelaend)
2760 (*swap_out) (output_bfd, irela, erel);
2761 irela += bed->s->int_rels_per_ext_rel;
2762 erel += input_rel_hdr->sh_entsize;
2765 /* Bump the counter, so that we know where to add the next set of
2767 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2772 /* Make weak undefined symbols in PIE dynamic. */
2775 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2776 struct elf_link_hash_entry *h)
2778 if (bfd_link_pie (info)
2780 && h->root.type == bfd_link_hash_undefweak)
2781 return bfd_elf_link_record_dynamic_symbol (info, h);
2786 /* Fix up the flags for a symbol. This handles various cases which
2787 can only be fixed after all the input files are seen. This is
2788 currently called by both adjust_dynamic_symbol and
2789 assign_sym_version, which is unnecessary but perhaps more robust in
2790 the face of future changes. */
2793 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2794 struct elf_info_failed *eif)
2796 const struct elf_backend_data *bed;
2798 /* If this symbol was mentioned in a non-ELF file, try to set
2799 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2800 permit a non-ELF file to correctly refer to a symbol defined in
2801 an ELF dynamic object. */
2804 while (h->root.type == bfd_link_hash_indirect)
2805 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2807 if (h->root.type != bfd_link_hash_defined
2808 && h->root.type != bfd_link_hash_defweak)
2811 h->ref_regular_nonweak = 1;
2815 if (h->root.u.def.section->owner != NULL
2816 && (bfd_get_flavour (h->root.u.def.section->owner)
2817 == bfd_target_elf_flavour))
2820 h->ref_regular_nonweak = 1;
2826 if (h->dynindx == -1
2830 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2839 /* Unfortunately, NON_ELF is only correct if the symbol
2840 was first seen in a non-ELF file. Fortunately, if the symbol
2841 was first seen in an ELF file, we're probably OK unless the
2842 symbol was defined in a non-ELF file. Catch that case here.
2843 FIXME: We're still in trouble if the symbol was first seen in
2844 a dynamic object, and then later in a non-ELF regular object. */
2845 if ((h->root.type == bfd_link_hash_defined
2846 || h->root.type == bfd_link_hash_defweak)
2848 && (h->root.u.def.section->owner != NULL
2849 ? (bfd_get_flavour (h->root.u.def.section->owner)
2850 != bfd_target_elf_flavour)
2851 : (bfd_is_abs_section (h->root.u.def.section)
2852 && !h->def_dynamic)))
2856 /* Backend specific symbol fixup. */
2857 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2858 if (bed->elf_backend_fixup_symbol
2859 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2862 /* If this is a final link, and the symbol was defined as a common
2863 symbol in a regular object file, and there was no definition in
2864 any dynamic object, then the linker will have allocated space for
2865 the symbol in a common section but the DEF_REGULAR
2866 flag will not have been set. */
2867 if (h->root.type == bfd_link_hash_defined
2871 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2874 /* Symbols defined in discarded sections shouldn't be dynamic. */
2875 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2876 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2878 /* If a weak undefined symbol has non-default visibility, we also
2879 hide it from the dynamic linker. */
2880 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2881 && h->root.type == bfd_link_hash_undefweak)
2882 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2884 /* A hidden versioned symbol in executable should be forced local if
2885 it is is locally defined, not referenced by shared library and not
2887 else if (bfd_link_executable (eif->info)
2888 && h->versioned == versioned_hidden
2889 && !eif->info->export_dynamic
2893 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2895 /* If -Bsymbolic was used (which means to bind references to global
2896 symbols to the definition within the shared object), and this
2897 symbol was defined in a regular object, then it actually doesn't
2898 need a PLT entry. Likewise, if the symbol has non-default
2899 visibility. If the symbol has hidden or internal visibility, we
2900 will force it local. */
2901 else if (h->needs_plt
2902 && bfd_link_pic (eif->info)
2903 && is_elf_hash_table (eif->info->hash)
2904 && (SYMBOLIC_BIND (eif->info, h)
2905 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2908 bfd_boolean force_local;
2910 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2911 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2912 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2915 /* If this is a weak defined symbol in a dynamic object, and we know
2916 the real definition in the dynamic object, copy interesting flags
2917 over to the real definition. */
2918 if (h->is_weakalias)
2920 struct elf_link_hash_entry *def = weakdef (h);
2922 /* If the real definition is defined by a regular object file,
2923 don't do anything special. See the longer description in
2924 _bfd_elf_adjust_dynamic_symbol, below. */
2925 if (def->def_regular)
2928 while ((h = h->u.alias) != def)
2929 h->is_weakalias = 0;
2933 while (h->root.type == bfd_link_hash_indirect)
2934 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2935 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2936 || h->root.type == bfd_link_hash_defweak);
2937 BFD_ASSERT (def->def_dynamic);
2938 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2939 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2946 /* Make the backend pick a good value for a dynamic symbol. This is
2947 called via elf_link_hash_traverse, and also calls itself
2951 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2953 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2954 struct elf_link_hash_table *htab;
2955 const struct elf_backend_data *bed;
2957 if (! is_elf_hash_table (eif->info->hash))
2960 /* Ignore indirect symbols. These are added by the versioning code. */
2961 if (h->root.type == bfd_link_hash_indirect)
2964 /* Fix the symbol flags. */
2965 if (! _bfd_elf_fix_symbol_flags (h, eif))
2968 htab = elf_hash_table (eif->info);
2969 bed = get_elf_backend_data (htab->dynobj);
2971 if (h->root.type == bfd_link_hash_undefweak)
2973 if (eif->info->dynamic_undefined_weak == 0)
2974 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2975 else if (eif->info->dynamic_undefined_weak > 0
2977 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2978 && !bfd_hide_sym_by_version (eif->info->version_info,
2979 h->root.root.string))
2981 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2989 /* If this symbol does not require a PLT entry, and it is not
2990 defined by a dynamic object, or is not referenced by a regular
2991 object, ignore it. We do have to handle a weak defined symbol,
2992 even if no regular object refers to it, if we decided to add it
2993 to the dynamic symbol table. FIXME: Do we normally need to worry
2994 about symbols which are defined by one dynamic object and
2995 referenced by another one? */
2997 && h->type != STT_GNU_IFUNC
3001 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3003 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3007 /* If we've already adjusted this symbol, don't do it again. This
3008 can happen via a recursive call. */
3009 if (h->dynamic_adjusted)
3012 /* Don't look at this symbol again. Note that we must set this
3013 after checking the above conditions, because we may look at a
3014 symbol once, decide not to do anything, and then get called
3015 recursively later after REF_REGULAR is set below. */
3016 h->dynamic_adjusted = 1;
3018 /* If this is a weak definition, and we know a real definition, and
3019 the real symbol is not itself defined by a regular object file,
3020 then get a good value for the real definition. We handle the
3021 real symbol first, for the convenience of the backend routine.
3023 Note that there is a confusing case here. If the real definition
3024 is defined by a regular object file, we don't get the real symbol
3025 from the dynamic object, but we do get the weak symbol. If the
3026 processor backend uses a COPY reloc, then if some routine in the
3027 dynamic object changes the real symbol, we will not see that
3028 change in the corresponding weak symbol. This is the way other
3029 ELF linkers work as well, and seems to be a result of the shared
3032 I will clarify this issue. Most SVR4 shared libraries define the
3033 variable _timezone and define timezone as a weak synonym. The
3034 tzset call changes _timezone. If you write
3035 extern int timezone;
3037 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3038 you might expect that, since timezone is a synonym for _timezone,
3039 the same number will print both times. However, if the processor
3040 backend uses a COPY reloc, then actually timezone will be copied
3041 into your process image, and, since you define _timezone
3042 yourself, _timezone will not. Thus timezone and _timezone will
3043 wind up at different memory locations. The tzset call will set
3044 _timezone, leaving timezone unchanged. */
3046 if (h->is_weakalias)
3048 struct elf_link_hash_entry *def = weakdef (h);
3050 /* If we get to this point, there is an implicit reference to
3051 the alias by a regular object file via the weak symbol H. */
3052 def->ref_regular = 1;
3054 /* Ensure that the backend adjust_dynamic_symbol function sees
3055 the strong alias before H by recursively calling ourselves. */
3056 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3060 /* If a symbol has no type and no size and does not require a PLT
3061 entry, then we are probably about to do the wrong thing here: we
3062 are probably going to create a COPY reloc for an empty object.
3063 This case can arise when a shared object is built with assembly
3064 code, and the assembly code fails to set the symbol type. */
3066 && h->type == STT_NOTYPE
3069 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3070 h->root.root.string);
3072 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3081 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3085 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3086 struct elf_link_hash_entry *h,
3089 unsigned int power_of_two;
3091 asection *sec = h->root.u.def.section;
3093 /* The section alignment of the definition is the maximum alignment
3094 requirement of symbols defined in the section. Since we don't
3095 know the symbol alignment requirement, we start with the
3096 maximum alignment and check low bits of the symbol address
3097 for the minimum alignment. */
3098 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3099 mask = ((bfd_vma) 1 << power_of_two) - 1;
3100 while ((h->root.u.def.value & mask) != 0)
3106 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3109 /* Adjust the section alignment if needed. */
3110 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3115 /* We make sure that the symbol will be aligned properly. */
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3130 info->callbacks->einfo
3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3132 h->root.root.string);
3137 /* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3141 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3150 bfd *output_bfd = (bfd *) data;
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
3156 h->root.u.def.value);
3162 /* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3167 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
3169 bfd_boolean not_local_protected)
3171 bfd_boolean binding_stays_local_p;
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3185 if (h->forced_local)
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
3193 switch (ELF_ST_VISIBILITY (h->other))
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3204 bed = get_elf_backend_data (hash_table->dynobj);
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
3209 if (!not_local_protected || !bed->is_function_type (h->type))
3210 binding_stays_local_p = TRUE;
3217 /* If it isn't defined locally, then clearly it's dynamic. */
3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3226 /* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3229 undefined symbols. The two functions are virtually identical except
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
3240 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3247 /* If it's a local sym, of course we resolve locally. */
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3264 /* If we don't have a definition in a regular file, then we can't
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
3269 /* Non-dynamic symbols resolve locally. */
3270 if (h->dynindx == -1)
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3288 bed = get_elf_backend_data (hash_table->dynobj);
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
3298 /* Function pointer equality tests may require that STV_PROTECTED
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
3303 return local_protected;
3306 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3309 struct bfd_section *
3310 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3324 elf_hash_table (info)->tls_sec = tls;
3326 /* Ensure the alignment of the first section is the largest alignment,
3327 so that the tls segment starts aligned. */
3329 tls->alignment_power = align;
3334 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3336 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3339 const struct elf_backend_data *bed;
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3346 bed = get_elf_backend_data (abfd);
3347 /* Function symbols do not count. */
3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
3357 if (bed->common_definition (sym))
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3375 /* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3379 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3381 Elf_Internal_Shdr * hdr;
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3394 if (! bfd_check_format (abfd, bfd_object))
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
3401 #if BFD_SUPPORTS_PLUGINS
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3407 /* Use the IR symbol table if the object has been claimed by
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3423 extsymcount = symcount;
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3432 if (extsymcount == 0)
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3438 if (isymbuf == NULL)
3441 /* Scan the symbol table looking for SYMDEF. */
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3452 if (strcmp (name, symdef->name) == 0)
3454 result = is_global_data_symbol_definition (abfd, isym);
3464 /* Add an entry to the .dynamic table. */
3467 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3485 bed = get_elf_backend_data (hash_table->dynobj);
3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3487 BFD_ASSERT (s != NULL);
3489 newsize = s->size + bed->s->sizeof_dyn;
3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3491 if (newcontents == NULL)
3495 dyn.d_un.d_val = val;
3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3499 s->contents = newcontents;
3504 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3505 otherwise just check whether one already exists. Returns -1 on error,
3506 1 if a DT_NEEDED tag already exists, and 0 on success. */
3509 elf_add_dt_needed_tag (bfd *abfd,
3510 struct bfd_link_info *info,
3514 struct elf_link_hash_table *hash_table;
3517 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3520 hash_table = elf_hash_table (info);
3521 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3522 if (strindex == (size_t) -1)
3525 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3528 const struct elf_backend_data *bed;
3531 bed = get_elf_backend_data (hash_table->dynobj);
3532 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3534 for (extdyn = sdyn->contents;
3535 extdyn < sdyn->contents + sdyn->size;
3536 extdyn += bed->s->sizeof_dyn)
3538 Elf_Internal_Dyn dyn;
3540 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3541 if (dyn.d_tag == DT_NEEDED
3542 && dyn.d_un.d_val == strindex)
3544 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3552 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3555 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3559 /* We were just checking for existence of the tag. */
3560 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3565 /* Return true if SONAME is on the needed list between NEEDED and STOP
3566 (or the end of list if STOP is NULL), and needed by a library that
3570 on_needed_list (const char *soname,
3571 struct bfd_link_needed_list *needed,
3572 struct bfd_link_needed_list *stop)
3574 struct bfd_link_needed_list *look;
3575 for (look = needed; look != stop; look = look->next)
3576 if (strcmp (soname, look->name) == 0
3577 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3578 /* If needed by a library that itself is not directly
3579 needed, recursively check whether that library is
3580 indirectly needed. Since we add DT_NEEDED entries to
3581 the end of the list, library dependencies appear after
3582 the library. Therefore search prior to the current
3583 LOOK, preventing possible infinite recursion. */
3584 || on_needed_list (elf_dt_name (look->by), needed, look)))
3590 /* Sort symbol by value, section, and size. */
3592 elf_sort_symbol (const void *arg1, const void *arg2)
3594 const struct elf_link_hash_entry *h1;
3595 const struct elf_link_hash_entry *h2;
3596 bfd_signed_vma vdiff;
3598 h1 = *(const struct elf_link_hash_entry **) arg1;
3599 h2 = *(const struct elf_link_hash_entry **) arg2;
3600 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3602 return vdiff > 0 ? 1 : -1;
3605 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3607 return sdiff > 0 ? 1 : -1;
3609 vdiff = h1->size - h2->size;
3610 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3613 /* This function is used to adjust offsets into .dynstr for
3614 dynamic symbols. This is called via elf_link_hash_traverse. */
3617 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3619 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3621 if (h->dynindx != -1)
3622 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3626 /* Assign string offsets in .dynstr, update all structures referencing
3630 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3632 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3633 struct elf_link_local_dynamic_entry *entry;
3634 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3635 bfd *dynobj = hash_table->dynobj;
3638 const struct elf_backend_data *bed;
3641 _bfd_elf_strtab_finalize (dynstr);
3642 size = _bfd_elf_strtab_size (dynstr);
3644 bed = get_elf_backend_data (dynobj);
3645 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3646 BFD_ASSERT (sdyn != NULL);
3648 /* Update all .dynamic entries referencing .dynstr strings. */
3649 for (extdyn = sdyn->contents;
3650 extdyn < sdyn->contents + sdyn->size;
3651 extdyn += bed->s->sizeof_dyn)
3653 Elf_Internal_Dyn dyn;
3655 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3659 dyn.d_un.d_val = size;
3669 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3674 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3677 /* Now update local dynamic symbols. */
3678 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3679 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3680 entry->isym.st_name);
3682 /* And the rest of dynamic symbols. */
3683 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3685 /* Adjust version definitions. */
3686 if (elf_tdata (output_bfd)->cverdefs)
3691 Elf_Internal_Verdef def;
3692 Elf_Internal_Verdaux defaux;
3694 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3698 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3700 p += sizeof (Elf_External_Verdef);
3701 if (def.vd_aux != sizeof (Elf_External_Verdef))
3703 for (i = 0; i < def.vd_cnt; ++i)
3705 _bfd_elf_swap_verdaux_in (output_bfd,
3706 (Elf_External_Verdaux *) p, &defaux);
3707 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3709 _bfd_elf_swap_verdaux_out (output_bfd,
3710 &defaux, (Elf_External_Verdaux *) p);
3711 p += sizeof (Elf_External_Verdaux);
3714 while (def.vd_next);
3717 /* Adjust version references. */
3718 if (elf_tdata (output_bfd)->verref)
3723 Elf_Internal_Verneed need;
3724 Elf_Internal_Vernaux needaux;
3726 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3730 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3732 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3733 _bfd_elf_swap_verneed_out (output_bfd, &need,
3734 (Elf_External_Verneed *) p);
3735 p += sizeof (Elf_External_Verneed);
3736 for (i = 0; i < need.vn_cnt; ++i)
3738 _bfd_elf_swap_vernaux_in (output_bfd,
3739 (Elf_External_Vernaux *) p, &needaux);
3740 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3742 _bfd_elf_swap_vernaux_out (output_bfd,
3744 (Elf_External_Vernaux *) p);
3745 p += sizeof (Elf_External_Vernaux);
3748 while (need.vn_next);
3754 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3755 The default is to only match when the INPUT and OUTPUT are exactly
3759 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3760 const bfd_target *output)
3762 return input == output;
3765 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3766 This version is used when different targets for the same architecture
3767 are virtually identical. */
3770 _bfd_elf_relocs_compatible (const bfd_target *input,
3771 const bfd_target *output)
3773 const struct elf_backend_data *obed, *ibed;
3775 if (input == output)
3778 ibed = xvec_get_elf_backend_data (input);
3779 obed = xvec_get_elf_backend_data (output);
3781 if (ibed->arch != obed->arch)
3784 /* If both backends are using this function, deem them compatible. */
3785 return ibed->relocs_compatible == obed->relocs_compatible;
3788 /* Make a special call to the linker "notice" function to tell it that
3789 we are about to handle an as-needed lib, or have finished
3790 processing the lib. */
3793 _bfd_elf_notice_as_needed (bfd *ibfd,
3794 struct bfd_link_info *info,
3795 enum notice_asneeded_action act)
3797 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3800 /* Check relocations an ELF object file. */
3803 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3806 struct elf_link_hash_table *htab = elf_hash_table (info);
3808 /* If this object is the same format as the output object, and it is
3809 not a shared library, then let the backend look through the
3812 This is required to build global offset table entries and to
3813 arrange for dynamic relocs. It is not required for the
3814 particular common case of linking non PIC code, even when linking
3815 against shared libraries, but unfortunately there is no way of
3816 knowing whether an object file has been compiled PIC or not.
3817 Looking through the relocs is not particularly time consuming.
3818 The problem is that we must either (1) keep the relocs in memory,
3819 which causes the linker to require additional runtime memory or
3820 (2) read the relocs twice from the input file, which wastes time.
3821 This would be a good case for using mmap.
3823 I have no idea how to handle linking PIC code into a file of a
3824 different format. It probably can't be done. */
3825 if ((abfd->flags & DYNAMIC) == 0
3826 && is_elf_hash_table (htab)
3827 && bed->check_relocs != NULL
3828 && elf_object_id (abfd) == elf_hash_table_id (htab)
3829 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3833 for (o = abfd->sections; o != NULL; o = o->next)
3835 Elf_Internal_Rela *internal_relocs;
3838 /* Don't check relocations in excluded sections. */
3839 if ((o->flags & SEC_RELOC) == 0
3840 || (o->flags & SEC_EXCLUDE) != 0
3841 || o->reloc_count == 0
3842 || ((info->strip == strip_all || info->strip == strip_debugger)
3843 && (o->flags & SEC_DEBUGGING) != 0)
3844 || bfd_is_abs_section (o->output_section))
3847 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3849 if (internal_relocs == NULL)
3852 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3854 if (elf_section_data (o)->relocs != internal_relocs)
3855 free (internal_relocs);
3865 /* Add symbols from an ELF object file to the linker hash table. */
3868 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3870 Elf_Internal_Ehdr *ehdr;
3871 Elf_Internal_Shdr *hdr;
3875 struct elf_link_hash_entry **sym_hash;
3876 bfd_boolean dynamic;
3877 Elf_External_Versym *extversym = NULL;
3878 Elf_External_Versym *extversym_end = NULL;
3879 Elf_External_Versym *ever;
3880 struct elf_link_hash_entry *weaks;
3881 struct elf_link_hash_entry **nondeflt_vers = NULL;
3882 size_t nondeflt_vers_cnt = 0;
3883 Elf_Internal_Sym *isymbuf = NULL;
3884 Elf_Internal_Sym *isym;
3885 Elf_Internal_Sym *isymend;
3886 const struct elf_backend_data *bed;
3887 bfd_boolean add_needed;
3888 struct elf_link_hash_table *htab;
3890 void *alloc_mark = NULL;
3891 struct bfd_hash_entry **old_table = NULL;
3892 unsigned int old_size = 0;
3893 unsigned int old_count = 0;
3894 void *old_tab = NULL;
3896 struct bfd_link_hash_entry *old_undefs = NULL;
3897 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3898 void *old_strtab = NULL;
3901 bfd_boolean just_syms;
3903 htab = elf_hash_table (info);
3904 bed = get_elf_backend_data (abfd);
3906 if ((abfd->flags & DYNAMIC) == 0)
3912 /* You can't use -r against a dynamic object. Also, there's no
3913 hope of using a dynamic object which does not exactly match
3914 the format of the output file. */
3915 if (bfd_link_relocatable (info)
3916 || !is_elf_hash_table (htab)
3917 || info->output_bfd->xvec != abfd->xvec)
3919 if (bfd_link_relocatable (info))
3920 bfd_set_error (bfd_error_invalid_operation);
3922 bfd_set_error (bfd_error_wrong_format);
3927 ehdr = elf_elfheader (abfd);
3928 if (info->warn_alternate_em
3929 && bed->elf_machine_code != ehdr->e_machine
3930 && ((bed->elf_machine_alt1 != 0
3931 && ehdr->e_machine == bed->elf_machine_alt1)
3932 || (bed->elf_machine_alt2 != 0
3933 && ehdr->e_machine == bed->elf_machine_alt2)))
3935 /* xgettext:c-format */
3936 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3937 ehdr->e_machine, abfd, bed->elf_machine_code);
3939 /* As a GNU extension, any input sections which are named
3940 .gnu.warning.SYMBOL are treated as warning symbols for the given
3941 symbol. This differs from .gnu.warning sections, which generate
3942 warnings when they are included in an output file. */
3943 /* PR 12761: Also generate this warning when building shared libraries. */
3944 for (s = abfd->sections; s != NULL; s = s->next)
3948 name = bfd_get_section_name (abfd, s);
3949 if (CONST_STRNEQ (name, ".gnu.warning."))
3954 name += sizeof ".gnu.warning." - 1;
3956 /* If this is a shared object, then look up the symbol
3957 in the hash table. If it is there, and it is already
3958 been defined, then we will not be using the entry
3959 from this shared object, so we don't need to warn.
3960 FIXME: If we see the definition in a regular object
3961 later on, we will warn, but we shouldn't. The only
3962 fix is to keep track of what warnings we are supposed
3963 to emit, and then handle them all at the end of the
3967 struct elf_link_hash_entry *h;
3969 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3971 /* FIXME: What about bfd_link_hash_common? */
3973 && (h->root.type == bfd_link_hash_defined
3974 || h->root.type == bfd_link_hash_defweak))
3979 msg = (char *) bfd_alloc (abfd, sz + 1);
3983 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3988 if (! (_bfd_generic_link_add_one_symbol
3989 (info, abfd, name, BSF_WARNING, s, 0, msg,
3990 FALSE, bed->collect, NULL)))
3993 if (bfd_link_executable (info))
3995 /* Clobber the section size so that the warning does
3996 not get copied into the output file. */
3999 /* Also set SEC_EXCLUDE, so that symbols defined in
4000 the warning section don't get copied to the output. */
4001 s->flags |= SEC_EXCLUDE;
4006 just_syms = ((s = abfd->sections) != NULL
4007 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4012 /* If we are creating a shared library, create all the dynamic
4013 sections immediately. We need to attach them to something,
4014 so we attach them to this BFD, provided it is the right
4015 format and is not from ld --just-symbols. Always create the
4016 dynamic sections for -E/--dynamic-list. FIXME: If there
4017 are no input BFD's of the same format as the output, we can't
4018 make a shared library. */
4020 && (bfd_link_pic (info)
4021 || (!bfd_link_relocatable (info)
4023 && (info->export_dynamic || info->dynamic)))
4024 && is_elf_hash_table (htab)
4025 && info->output_bfd->xvec == abfd->xvec
4026 && !htab->dynamic_sections_created)
4028 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4032 else if (!is_elf_hash_table (htab))
4036 const char *soname = NULL;
4038 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4039 const Elf_Internal_Phdr *phdr;
4042 /* ld --just-symbols and dynamic objects don't mix very well.
4043 ld shouldn't allow it. */
4047 /* If this dynamic lib was specified on the command line with
4048 --as-needed in effect, then we don't want to add a DT_NEEDED
4049 tag unless the lib is actually used. Similary for libs brought
4050 in by another lib's DT_NEEDED. When --no-add-needed is used
4051 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4052 any dynamic library in DT_NEEDED tags in the dynamic lib at
4054 add_needed = (elf_dyn_lib_class (abfd)
4055 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4056 | DYN_NO_NEEDED)) == 0;
4058 s = bfd_get_section_by_name (abfd, ".dynamic");
4063 unsigned int elfsec;
4064 unsigned long shlink;
4066 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4073 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4074 if (elfsec == SHN_BAD)
4075 goto error_free_dyn;
4076 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4078 for (extdyn = dynbuf;
4079 extdyn < dynbuf + s->size;
4080 extdyn += bed->s->sizeof_dyn)
4082 Elf_Internal_Dyn dyn;
4084 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4085 if (dyn.d_tag == DT_SONAME)
4087 unsigned int tagv = dyn.d_un.d_val;
4088 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4090 goto error_free_dyn;
4092 if (dyn.d_tag == DT_NEEDED)
4094 struct bfd_link_needed_list *n, **pn;
4096 unsigned int tagv = dyn.d_un.d_val;
4098 amt = sizeof (struct bfd_link_needed_list);
4099 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4100 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4101 if (n == NULL || fnm == NULL)
4102 goto error_free_dyn;
4103 amt = strlen (fnm) + 1;
4104 anm = (char *) bfd_alloc (abfd, amt);
4106 goto error_free_dyn;
4107 memcpy (anm, fnm, amt);
4111 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4115 if (dyn.d_tag == DT_RUNPATH)
4117 struct bfd_link_needed_list *n, **pn;
4119 unsigned int tagv = dyn.d_un.d_val;
4121 amt = sizeof (struct bfd_link_needed_list);
4122 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4123 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4124 if (n == NULL || fnm == NULL)
4125 goto error_free_dyn;
4126 amt = strlen (fnm) + 1;
4127 anm = (char *) bfd_alloc (abfd, amt);
4129 goto error_free_dyn;
4130 memcpy (anm, fnm, amt);
4134 for (pn = & runpath;
4140 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4141 if (!runpath && dyn.d_tag == DT_RPATH)
4143 struct bfd_link_needed_list *n, **pn;
4145 unsigned int tagv = dyn.d_un.d_val;
4147 amt = sizeof (struct bfd_link_needed_list);
4148 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4149 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4150 if (n == NULL || fnm == NULL)
4151 goto error_free_dyn;
4152 amt = strlen (fnm) + 1;
4153 anm = (char *) bfd_alloc (abfd, amt);
4155 goto error_free_dyn;
4156 memcpy (anm, fnm, amt);
4166 if (dyn.d_tag == DT_AUDIT)
4168 unsigned int tagv = dyn.d_un.d_val;
4169 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4176 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4177 frees all more recently bfd_alloc'd blocks as well. */
4183 struct bfd_link_needed_list **pn;
4184 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4189 /* If we have a PT_GNU_RELRO program header, mark as read-only
4190 all sections contained fully therein. This makes relro
4191 shared library sections appear as they will at run-time. */
4192 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4193 while (phdr-- > elf_tdata (abfd)->phdr)
4194 if (phdr->p_type == PT_GNU_RELRO)
4196 for (s = abfd->sections; s != NULL; s = s->next)
4197 if ((s->flags & SEC_ALLOC) != 0
4198 && s->vma >= phdr->p_vaddr
4199 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4200 s->flags |= SEC_READONLY;
4204 /* We do not want to include any of the sections in a dynamic
4205 object in the output file. We hack by simply clobbering the
4206 list of sections in the BFD. This could be handled more
4207 cleanly by, say, a new section flag; the existing
4208 SEC_NEVER_LOAD flag is not the one we want, because that one
4209 still implies that the section takes up space in the output
4211 bfd_section_list_clear (abfd);
4213 /* Find the name to use in a DT_NEEDED entry that refers to this
4214 object. If the object has a DT_SONAME entry, we use it.
4215 Otherwise, if the generic linker stuck something in
4216 elf_dt_name, we use that. Otherwise, we just use the file
4218 if (soname == NULL || *soname == '\0')
4220 soname = elf_dt_name (abfd);
4221 if (soname == NULL || *soname == '\0')
4222 soname = bfd_get_filename (abfd);
4225 /* Save the SONAME because sometimes the linker emulation code
4226 will need to know it. */
4227 elf_dt_name (abfd) = soname;
4229 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4233 /* If we have already included this dynamic object in the
4234 link, just ignore it. There is no reason to include a
4235 particular dynamic object more than once. */
4239 /* Save the DT_AUDIT entry for the linker emulation code. */
4240 elf_dt_audit (abfd) = audit;
4243 /* If this is a dynamic object, we always link against the .dynsym
4244 symbol table, not the .symtab symbol table. The dynamic linker
4245 will only see the .dynsym symbol table, so there is no reason to
4246 look at .symtab for a dynamic object. */
4248 if (! dynamic || elf_dynsymtab (abfd) == 0)
4249 hdr = &elf_tdata (abfd)->symtab_hdr;
4251 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4253 symcount = hdr->sh_size / bed->s->sizeof_sym;
4255 /* The sh_info field of the symtab header tells us where the
4256 external symbols start. We don't care about the local symbols at
4258 if (elf_bad_symtab (abfd))
4260 extsymcount = symcount;
4265 extsymcount = symcount - hdr->sh_info;
4266 extsymoff = hdr->sh_info;
4269 sym_hash = elf_sym_hashes (abfd);
4270 if (extsymcount != 0)
4272 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4274 if (isymbuf == NULL)
4277 if (sym_hash == NULL)
4279 /* We store a pointer to the hash table entry for each
4282 amt *= sizeof (struct elf_link_hash_entry *);
4283 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4284 if (sym_hash == NULL)
4285 goto error_free_sym;
4286 elf_sym_hashes (abfd) = sym_hash;
4292 /* Read in any version definitions. */
4293 if (!_bfd_elf_slurp_version_tables (abfd,
4294 info->default_imported_symver))
4295 goto error_free_sym;
4297 /* Read in the symbol versions, but don't bother to convert them
4298 to internal format. */
4299 if (elf_dynversym (abfd) != 0)
4301 Elf_Internal_Shdr *versymhdr;
4303 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4304 amt = versymhdr->sh_size;
4305 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4306 if (extversym == NULL)
4307 goto error_free_sym;
4308 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4309 || bfd_bread (extversym, amt, abfd) != amt)
4310 goto error_free_vers;
4311 extversym_end = extversym + (amt / sizeof (* extversym));
4315 /* If we are loading an as-needed shared lib, save the symbol table
4316 state before we start adding symbols. If the lib turns out
4317 to be unneeded, restore the state. */
4318 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4323 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4325 struct bfd_hash_entry *p;
4326 struct elf_link_hash_entry *h;
4328 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4330 h = (struct elf_link_hash_entry *) p;
4331 entsize += htab->root.table.entsize;
4332 if (h->root.type == bfd_link_hash_warning)
4333 entsize += htab->root.table.entsize;
4337 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4338 old_tab = bfd_malloc (tabsize + entsize);
4339 if (old_tab == NULL)
4340 goto error_free_vers;
4342 /* Remember the current objalloc pointer, so that all mem for
4343 symbols added can later be reclaimed. */
4344 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4345 if (alloc_mark == NULL)
4346 goto error_free_vers;
4348 /* Make a special call to the linker "notice" function to
4349 tell it that we are about to handle an as-needed lib. */
4350 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4351 goto error_free_vers;
4353 /* Clone the symbol table. Remember some pointers into the
4354 symbol table, and dynamic symbol count. */
4355 old_ent = (char *) old_tab + tabsize;
4356 memcpy (old_tab, htab->root.table.table, tabsize);
4357 old_undefs = htab->root.undefs;
4358 old_undefs_tail = htab->root.undefs_tail;
4359 old_table = htab->root.table.table;
4360 old_size = htab->root.table.size;
4361 old_count = htab->root.table.count;
4362 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4363 if (old_strtab == NULL)
4364 goto error_free_vers;
4366 for (i = 0; i < htab->root.table.size; i++)
4368 struct bfd_hash_entry *p;
4369 struct elf_link_hash_entry *h;
4371 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4373 memcpy (old_ent, p, htab->root.table.entsize);
4374 old_ent = (char *) old_ent + htab->root.table.entsize;
4375 h = (struct elf_link_hash_entry *) p;
4376 if (h->root.type == bfd_link_hash_warning)
4378 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4379 old_ent = (char *) old_ent + htab->root.table.entsize;
4386 if (extversym == NULL)
4388 else if (extversym + extsymoff < extversym_end)
4389 ever = extversym + extsymoff;
4392 /* xgettext:c-format */
4393 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4394 abfd, (long) extsymoff,
4395 (long) (extversym_end - extversym) / sizeof (* extversym));
4396 bfd_set_error (bfd_error_bad_value);
4397 goto error_free_vers;
4400 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4402 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4406 asection *sec, *new_sec;
4409 struct elf_link_hash_entry *h;
4410 struct elf_link_hash_entry *hi;
4411 bfd_boolean definition;
4412 bfd_boolean size_change_ok;
4413 bfd_boolean type_change_ok;
4414 bfd_boolean new_weak;
4415 bfd_boolean old_weak;
4416 bfd_boolean override;
4418 bfd_boolean discarded;
4419 unsigned int old_alignment;
4421 bfd_boolean matched;
4425 flags = BSF_NO_FLAGS;
4427 value = isym->st_value;
4428 common = bed->common_definition (isym);
4429 if (common && info->inhibit_common_definition)
4431 /* Treat common symbol as undefined for --no-define-common. */
4432 isym->st_shndx = SHN_UNDEF;
4437 bind = ELF_ST_BIND (isym->st_info);
4441 /* This should be impossible, since ELF requires that all
4442 global symbols follow all local symbols, and that sh_info
4443 point to the first global symbol. Unfortunately, Irix 5
4448 if (isym->st_shndx != SHN_UNDEF && !common)
4456 case STB_GNU_UNIQUE:
4457 flags = BSF_GNU_UNIQUE;
4461 /* Leave it up to the processor backend. */
4465 if (isym->st_shndx == SHN_UNDEF)
4466 sec = bfd_und_section_ptr;
4467 else if (isym->st_shndx == SHN_ABS)
4468 sec = bfd_abs_section_ptr;
4469 else if (isym->st_shndx == SHN_COMMON)
4471 sec = bfd_com_section_ptr;
4472 /* What ELF calls the size we call the value. What ELF
4473 calls the value we call the alignment. */
4474 value = isym->st_size;
4478 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4480 sec = bfd_abs_section_ptr;
4481 else if (discarded_section (sec))
4483 /* Symbols from discarded section are undefined. We keep
4485 sec = bfd_und_section_ptr;
4487 isym->st_shndx = SHN_UNDEF;
4489 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4493 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4496 goto error_free_vers;
4498 if (isym->st_shndx == SHN_COMMON
4499 && (abfd->flags & BFD_PLUGIN) != 0)
4501 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4505 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4507 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4509 goto error_free_vers;
4513 else if (isym->st_shndx == SHN_COMMON
4514 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4515 && !bfd_link_relocatable (info))
4517 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4521 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4522 | SEC_LINKER_CREATED);
4523 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4525 goto error_free_vers;
4529 else if (bed->elf_add_symbol_hook)
4531 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4533 goto error_free_vers;
4535 /* The hook function sets the name to NULL if this symbol
4536 should be skipped for some reason. */
4541 /* Sanity check that all possibilities were handled. */
4544 bfd_set_error (bfd_error_bad_value);
4545 goto error_free_vers;
4548 /* Silently discard TLS symbols from --just-syms. There's
4549 no way to combine a static TLS block with a new TLS block
4550 for this executable. */
4551 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4552 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4555 if (bfd_is_und_section (sec)
4556 || bfd_is_com_section (sec))
4561 size_change_ok = FALSE;
4562 type_change_ok = bed->type_change_ok;
4569 if (is_elf_hash_table (htab))
4571 Elf_Internal_Versym iver;
4572 unsigned int vernum = 0;
4577 if (info->default_imported_symver)
4578 /* Use the default symbol version created earlier. */
4579 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4583 else if (ever >= extversym_end)
4585 /* xgettext:c-format */
4586 _bfd_error_handler (_("%pB: not enough version information"),
4588 bfd_set_error (bfd_error_bad_value);
4589 goto error_free_vers;
4592 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4594 vernum = iver.vs_vers & VERSYM_VERSION;
4596 /* If this is a hidden symbol, or if it is not version
4597 1, we append the version name to the symbol name.
4598 However, we do not modify a non-hidden absolute symbol
4599 if it is not a function, because it might be the version
4600 symbol itself. FIXME: What if it isn't? */
4601 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4603 && (!bfd_is_abs_section (sec)
4604 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4607 size_t namelen, verlen, newlen;
4610 if (isym->st_shndx != SHN_UNDEF)
4612 if (vernum > elf_tdata (abfd)->cverdefs)
4614 else if (vernum > 1)
4616 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4623 /* xgettext:c-format */
4624 (_("%pB: %s: invalid version %u (max %d)"),
4626 elf_tdata (abfd)->cverdefs);
4627 bfd_set_error (bfd_error_bad_value);
4628 goto error_free_vers;
4633 /* We cannot simply test for the number of
4634 entries in the VERNEED section since the
4635 numbers for the needed versions do not start
4637 Elf_Internal_Verneed *t;
4640 for (t = elf_tdata (abfd)->verref;
4644 Elf_Internal_Vernaux *a;
4646 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4648 if (a->vna_other == vernum)
4650 verstr = a->vna_nodename;
4660 /* xgettext:c-format */
4661 (_("%pB: %s: invalid needed version %d"),
4662 abfd, name, vernum);
4663 bfd_set_error (bfd_error_bad_value);
4664 goto error_free_vers;
4668 namelen = strlen (name);
4669 verlen = strlen (verstr);
4670 newlen = namelen + verlen + 2;
4671 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4672 && isym->st_shndx != SHN_UNDEF)
4675 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4676 if (newname == NULL)
4677 goto error_free_vers;
4678 memcpy (newname, name, namelen);
4679 p = newname + namelen;
4681 /* If this is a defined non-hidden version symbol,
4682 we add another @ to the name. This indicates the
4683 default version of the symbol. */
4684 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4685 && isym->st_shndx != SHN_UNDEF)
4687 memcpy (p, verstr, verlen + 1);
4692 /* If this symbol has default visibility and the user has
4693 requested we not re-export it, then mark it as hidden. */
4694 if (!bfd_is_und_section (sec)
4697 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4698 isym->st_other = (STV_HIDDEN
4699 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4701 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4702 sym_hash, &old_bfd, &old_weak,
4703 &old_alignment, &skip, &override,
4704 &type_change_ok, &size_change_ok,
4706 goto error_free_vers;
4711 /* Override a definition only if the new symbol matches the
4713 if (override && matched)
4717 while (h->root.type == bfd_link_hash_indirect
4718 || h->root.type == bfd_link_hash_warning)
4719 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4721 if (elf_tdata (abfd)->verdef != NULL
4724 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4727 if (! (_bfd_generic_link_add_one_symbol
4728 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4729 (struct bfd_link_hash_entry **) sym_hash)))
4730 goto error_free_vers;
4732 if ((abfd->flags & DYNAMIC) == 0
4733 && (bfd_get_flavour (info->output_bfd)
4734 == bfd_target_elf_flavour))
4736 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4737 elf_tdata (info->output_bfd)->has_gnu_symbols
4738 |= elf_gnu_symbol_ifunc;
4739 if ((flags & BSF_GNU_UNIQUE))
4740 elf_tdata (info->output_bfd)->has_gnu_symbols
4741 |= elf_gnu_symbol_unique;
4745 /* We need to make sure that indirect symbol dynamic flags are
4748 while (h->root.type == bfd_link_hash_indirect
4749 || h->root.type == bfd_link_hash_warning)
4750 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4752 /* Setting the index to -3 tells elf_link_output_extsym that
4753 this symbol is defined in a discarded section. */
4759 new_weak = (flags & BSF_WEAK) != 0;
4763 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4764 && is_elf_hash_table (htab)
4765 && h->u.alias == NULL)
4767 /* Keep a list of all weak defined non function symbols from
4768 a dynamic object, using the alias field. Later in this
4769 function we will set the alias field to the correct
4770 value. We only put non-function symbols from dynamic
4771 objects on this list, because that happens to be the only
4772 time we need to know the normal symbol corresponding to a
4773 weak symbol, and the information is time consuming to
4774 figure out. If the alias field is not already NULL,
4775 then this symbol was already defined by some previous
4776 dynamic object, and we will be using that previous
4777 definition anyhow. */
4783 /* Set the alignment of a common symbol. */
4784 if ((common || bfd_is_com_section (sec))
4785 && h->root.type == bfd_link_hash_common)
4790 align = bfd_log2 (isym->st_value);
4793 /* The new symbol is a common symbol in a shared object.
4794 We need to get the alignment from the section. */
4795 align = new_sec->alignment_power;
4797 if (align > old_alignment)
4798 h->root.u.c.p->alignment_power = align;
4800 h->root.u.c.p->alignment_power = old_alignment;
4803 if (is_elf_hash_table (htab))
4805 /* Set a flag in the hash table entry indicating the type of
4806 reference or definition we just found. A dynamic symbol
4807 is one which is referenced or defined by both a regular
4808 object and a shared object. */
4809 bfd_boolean dynsym = FALSE;
4811 /* Plugin symbols aren't normal. Don't set def_regular or
4812 ref_regular for them, or make them dynamic. */
4813 if ((abfd->flags & BFD_PLUGIN) != 0)
4820 if (bind != STB_WEAK)
4821 h->ref_regular_nonweak = 1;
4833 /* If the indirect symbol has been forced local, don't
4834 make the real symbol dynamic. */
4835 if ((h == hi || !hi->forced_local)
4836 && (bfd_link_dll (info)
4846 hi->ref_dynamic = 1;
4851 hi->def_dynamic = 1;
4854 /* If the indirect symbol has been forced local, don't
4855 make the real symbol dynamic. */
4856 if ((h == hi || !hi->forced_local)
4860 && weakdef (h)->dynindx != -1)))
4864 /* Check to see if we need to add an indirect symbol for
4865 the default name. */
4867 || (!override && h->root.type == bfd_link_hash_common))
4868 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4869 sec, value, &old_bfd, &dynsym))
4870 goto error_free_vers;
4872 /* Check the alignment when a common symbol is involved. This
4873 can change when a common symbol is overridden by a normal
4874 definition or a common symbol is ignored due to the old
4875 normal definition. We need to make sure the maximum
4876 alignment is maintained. */
4877 if ((old_alignment || common)
4878 && h->root.type != bfd_link_hash_common)
4880 unsigned int common_align;
4881 unsigned int normal_align;
4882 unsigned int symbol_align;
4886 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4887 || h->root.type == bfd_link_hash_defweak);
4889 symbol_align = ffs (h->root.u.def.value) - 1;
4890 if (h->root.u.def.section->owner != NULL
4891 && (h->root.u.def.section->owner->flags
4892 & (DYNAMIC | BFD_PLUGIN)) == 0)
4894 normal_align = h->root.u.def.section->alignment_power;
4895 if (normal_align > symbol_align)
4896 normal_align = symbol_align;
4899 normal_align = symbol_align;
4903 common_align = old_alignment;
4904 common_bfd = old_bfd;
4909 common_align = bfd_log2 (isym->st_value);
4911 normal_bfd = old_bfd;
4914 if (normal_align < common_align)
4916 /* PR binutils/2735 */
4917 if (normal_bfd == NULL)
4919 /* xgettext:c-format */
4920 (_("warning: alignment %u of common symbol `%s' in %pB is"
4921 " greater than the alignment (%u) of its section %pA"),
4922 1 << common_align, name, common_bfd,
4923 1 << normal_align, h->root.u.def.section);
4926 /* xgettext:c-format */
4927 (_("warning: alignment %u of symbol `%s' in %pB"
4928 " is smaller than %u in %pB"),
4929 1 << normal_align, name, normal_bfd,
4930 1 << common_align, common_bfd);
4934 /* Remember the symbol size if it isn't undefined. */
4935 if (isym->st_size != 0
4936 && isym->st_shndx != SHN_UNDEF
4937 && (definition || h->size == 0))
4940 && h->size != isym->st_size
4941 && ! size_change_ok)
4943 /* xgettext:c-format */
4944 (_("warning: size of symbol `%s' changed"
4945 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4946 name, (uint64_t) h->size, old_bfd,
4947 (uint64_t) isym->st_size, abfd);
4949 h->size = isym->st_size;
4952 /* If this is a common symbol, then we always want H->SIZE
4953 to be the size of the common symbol. The code just above
4954 won't fix the size if a common symbol becomes larger. We
4955 don't warn about a size change here, because that is
4956 covered by --warn-common. Allow changes between different
4958 if (h->root.type == bfd_link_hash_common)
4959 h->size = h->root.u.c.size;
4961 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4962 && ((definition && !new_weak)
4963 || (old_weak && h->root.type == bfd_link_hash_common)
4964 || h->type == STT_NOTYPE))
4966 unsigned int type = ELF_ST_TYPE (isym->st_info);
4968 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4970 if (type == STT_GNU_IFUNC
4971 && (abfd->flags & DYNAMIC) != 0)
4974 if (h->type != type)
4976 if (h->type != STT_NOTYPE && ! type_change_ok)
4977 /* xgettext:c-format */
4979 (_("warning: type of symbol `%s' changed"
4980 " from %d to %d in %pB"),
4981 name, h->type, type, abfd);
4987 /* Merge st_other field. */
4988 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4990 /* We don't want to make debug symbol dynamic. */
4992 && (sec->flags & SEC_DEBUGGING)
4993 && !bfd_link_relocatable (info))
4996 /* Nor should we make plugin symbols dynamic. */
4997 if ((abfd->flags & BFD_PLUGIN) != 0)
5002 h->target_internal = isym->st_target_internal;
5003 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5006 if (definition && !dynamic)
5008 char *p = strchr (name, ELF_VER_CHR);
5009 if (p != NULL && p[1] != ELF_VER_CHR)
5011 /* Queue non-default versions so that .symver x, x@FOO
5012 aliases can be checked. */
5015 amt = ((isymend - isym + 1)
5016 * sizeof (struct elf_link_hash_entry *));
5018 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5020 goto error_free_vers;
5022 nondeflt_vers[nondeflt_vers_cnt++] = h;
5026 if (dynsym && h->dynindx == -1)
5028 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5029 goto error_free_vers;
5031 && weakdef (h)->dynindx == -1)
5033 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5034 goto error_free_vers;
5037 else if (h->dynindx != -1)
5038 /* If the symbol already has a dynamic index, but
5039 visibility says it should not be visible, turn it into
5041 switch (ELF_ST_VISIBILITY (h->other))
5045 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5050 /* Don't add DT_NEEDED for references from the dummy bfd nor
5051 for unmatched symbol. */
5056 && h->ref_regular_nonweak
5058 || (old_bfd->flags & BFD_PLUGIN) == 0))
5059 || (h->ref_dynamic_nonweak
5060 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5061 && !on_needed_list (elf_dt_name (abfd),
5062 htab->needed, NULL))))
5065 const char *soname = elf_dt_name (abfd);
5067 info->callbacks->minfo ("%!", soname, old_bfd,
5068 h->root.root.string);
5070 /* A symbol from a library loaded via DT_NEEDED of some
5071 other library is referenced by a regular object.
5072 Add a DT_NEEDED entry for it. Issue an error if
5073 --no-add-needed is used and the reference was not
5076 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5079 /* xgettext:c-format */
5080 (_("%pB: undefined reference to symbol '%s'"),
5082 bfd_set_error (bfd_error_missing_dso);
5083 goto error_free_vers;
5086 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5087 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5090 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5092 goto error_free_vers;
5094 BFD_ASSERT (ret == 0);
5099 if (info->lto_plugin_active
5100 && !bfd_link_relocatable (info)
5101 && (abfd->flags & BFD_PLUGIN) == 0
5107 if (bed->s->arch_size == 32)
5112 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5113 referenced in regular objects so that linker plugin will get
5114 the correct symbol resolution. */
5116 sym_hash = elf_sym_hashes (abfd);
5117 for (s = abfd->sections; s != NULL; s = s->next)
5119 Elf_Internal_Rela *internal_relocs;
5120 Elf_Internal_Rela *rel, *relend;
5122 /* Don't check relocations in excluded sections. */
5123 if ((s->flags & SEC_RELOC) == 0
5124 || s->reloc_count == 0
5125 || (s->flags & SEC_EXCLUDE) != 0
5126 || ((info->strip == strip_all
5127 || info->strip == strip_debugger)
5128 && (s->flags & SEC_DEBUGGING) != 0))
5131 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5134 if (internal_relocs == NULL)
5135 goto error_free_vers;
5137 rel = internal_relocs;
5138 relend = rel + s->reloc_count;
5139 for ( ; rel < relend; rel++)
5141 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5142 struct elf_link_hash_entry *h;
5144 /* Skip local symbols. */
5145 if (r_symndx < extsymoff)
5148 h = sym_hash[r_symndx - extsymoff];
5150 h->root.non_ir_ref_regular = 1;
5153 if (elf_section_data (s)->relocs != internal_relocs)
5154 free (internal_relocs);
5158 if (extversym != NULL)
5164 if (isymbuf != NULL)
5170 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5174 /* Restore the symbol table. */
5175 old_ent = (char *) old_tab + tabsize;
5176 memset (elf_sym_hashes (abfd), 0,
5177 extsymcount * sizeof (struct elf_link_hash_entry *));
5178 htab->root.table.table = old_table;
5179 htab->root.table.size = old_size;
5180 htab->root.table.count = old_count;
5181 memcpy (htab->root.table.table, old_tab, tabsize);
5182 htab->root.undefs = old_undefs;
5183 htab->root.undefs_tail = old_undefs_tail;
5184 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5187 for (i = 0; i < htab->root.table.size; i++)
5189 struct bfd_hash_entry *p;
5190 struct elf_link_hash_entry *h;
5192 unsigned int alignment_power;
5193 unsigned int non_ir_ref_dynamic;
5195 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5197 h = (struct elf_link_hash_entry *) p;
5198 if (h->root.type == bfd_link_hash_warning)
5199 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5201 /* Preserve the maximum alignment and size for common
5202 symbols even if this dynamic lib isn't on DT_NEEDED
5203 since it can still be loaded at run time by another
5205 if (h->root.type == bfd_link_hash_common)
5207 size = h->root.u.c.size;
5208 alignment_power = h->root.u.c.p->alignment_power;
5213 alignment_power = 0;
5215 /* Preserve non_ir_ref_dynamic so that this symbol
5216 will be exported when the dynamic lib becomes needed
5217 in the second pass. */
5218 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5219 memcpy (p, old_ent, htab->root.table.entsize);
5220 old_ent = (char *) old_ent + htab->root.table.entsize;
5221 h = (struct elf_link_hash_entry *) p;
5222 if (h->root.type == bfd_link_hash_warning)
5224 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5225 old_ent = (char *) old_ent + htab->root.table.entsize;
5226 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5228 if (h->root.type == bfd_link_hash_common)
5230 if (size > h->root.u.c.size)
5231 h->root.u.c.size = size;
5232 if (alignment_power > h->root.u.c.p->alignment_power)
5233 h->root.u.c.p->alignment_power = alignment_power;
5235 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5239 /* Make a special call to the linker "notice" function to
5240 tell it that symbols added for crefs may need to be removed. */
5241 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5242 goto error_free_vers;
5245 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5247 if (nondeflt_vers != NULL)
5248 free (nondeflt_vers);
5252 if (old_tab != NULL)
5254 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5255 goto error_free_vers;
5260 /* Now that all the symbols from this input file are created, if
5261 not performing a relocatable link, handle .symver foo, foo@BAR
5262 such that any relocs against foo become foo@BAR. */
5263 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5267 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5269 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5270 char *shortname, *p;
5272 p = strchr (h->root.root.string, ELF_VER_CHR);
5274 || (h->root.type != bfd_link_hash_defined
5275 && h->root.type != bfd_link_hash_defweak))
5278 amt = p - h->root.root.string;
5279 shortname = (char *) bfd_malloc (amt + 1);
5281 goto error_free_vers;
5282 memcpy (shortname, h->root.root.string, amt);
5283 shortname[amt] = '\0';
5285 hi = (struct elf_link_hash_entry *)
5286 bfd_link_hash_lookup (&htab->root, shortname,
5287 FALSE, FALSE, FALSE);
5289 && hi->root.type == h->root.type
5290 && hi->root.u.def.value == h->root.u.def.value
5291 && hi->root.u.def.section == h->root.u.def.section)
5293 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5294 hi->root.type = bfd_link_hash_indirect;
5295 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5296 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5297 sym_hash = elf_sym_hashes (abfd);
5299 for (symidx = 0; symidx < extsymcount; ++symidx)
5300 if (sym_hash[symidx] == hi)
5302 sym_hash[symidx] = h;
5308 free (nondeflt_vers);
5309 nondeflt_vers = NULL;
5312 /* Now set the alias field correctly for all the weak defined
5313 symbols we found. The only way to do this is to search all the
5314 symbols. Since we only need the information for non functions in
5315 dynamic objects, that's the only time we actually put anything on
5316 the list WEAKS. We need this information so that if a regular
5317 object refers to a symbol defined weakly in a dynamic object, the
5318 real symbol in the dynamic object is also put in the dynamic
5319 symbols; we also must arrange for both symbols to point to the
5320 same memory location. We could handle the general case of symbol
5321 aliasing, but a general symbol alias can only be generated in
5322 assembler code, handling it correctly would be very time
5323 consuming, and other ELF linkers don't handle general aliasing
5327 struct elf_link_hash_entry **hpp;
5328 struct elf_link_hash_entry **hppend;
5329 struct elf_link_hash_entry **sorted_sym_hash;
5330 struct elf_link_hash_entry *h;
5333 /* Since we have to search the whole symbol list for each weak
5334 defined symbol, search time for N weak defined symbols will be
5335 O(N^2). Binary search will cut it down to O(NlogN). */
5337 amt *= sizeof (struct elf_link_hash_entry *);
5338 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5339 if (sorted_sym_hash == NULL)
5341 sym_hash = sorted_sym_hash;
5342 hpp = elf_sym_hashes (abfd);
5343 hppend = hpp + extsymcount;
5345 for (; hpp < hppend; hpp++)
5349 && h->root.type == bfd_link_hash_defined
5350 && !bed->is_function_type (h->type))
5358 qsort (sorted_sym_hash, sym_count,
5359 sizeof (struct elf_link_hash_entry *),
5362 while (weaks != NULL)
5364 struct elf_link_hash_entry *hlook;
5367 size_t i, j, idx = 0;
5370 weaks = hlook->u.alias;
5371 hlook->u.alias = NULL;
5373 if (hlook->root.type != bfd_link_hash_defined
5374 && hlook->root.type != bfd_link_hash_defweak)
5377 slook = hlook->root.u.def.section;
5378 vlook = hlook->root.u.def.value;
5384 bfd_signed_vma vdiff;
5386 h = sorted_sym_hash[idx];
5387 vdiff = vlook - h->root.u.def.value;
5394 int sdiff = slook->id - h->root.u.def.section->id;
5404 /* We didn't find a value/section match. */
5408 /* With multiple aliases, or when the weak symbol is already
5409 strongly defined, we have multiple matching symbols and
5410 the binary search above may land on any of them. Step
5411 one past the matching symbol(s). */
5414 h = sorted_sym_hash[idx];
5415 if (h->root.u.def.section != slook
5416 || h->root.u.def.value != vlook)
5420 /* Now look back over the aliases. Since we sorted by size
5421 as well as value and section, we'll choose the one with
5422 the largest size. */
5425 h = sorted_sym_hash[idx];
5427 /* Stop if value or section doesn't match. */
5428 if (h->root.u.def.section != slook
5429 || h->root.u.def.value != vlook)
5431 else if (h != hlook)
5433 struct elf_link_hash_entry *t;
5436 hlook->is_weakalias = 1;
5438 if (t->u.alias != NULL)
5439 while (t->u.alias != h)
5443 /* If the weak definition is in the list of dynamic
5444 symbols, make sure the real definition is put
5446 if (hlook->dynindx != -1 && h->dynindx == -1)
5448 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5451 free (sorted_sym_hash);
5456 /* If the real definition is in the list of dynamic
5457 symbols, make sure the weak definition is put
5458 there as well. If we don't do this, then the
5459 dynamic loader might not merge the entries for the
5460 real definition and the weak definition. */
5461 if (h->dynindx != -1 && hlook->dynindx == -1)
5463 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5464 goto err_free_sym_hash;
5471 free (sorted_sym_hash);
5474 if (bed->check_directives
5475 && !(*bed->check_directives) (abfd, info))
5478 /* If this is a non-traditional link, try to optimize the handling
5479 of the .stab/.stabstr sections. */
5481 && ! info->traditional_format
5482 && is_elf_hash_table (htab)
5483 && (info->strip != strip_all && info->strip != strip_debugger))
5487 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5488 if (stabstr != NULL)
5490 bfd_size_type string_offset = 0;
5493 for (stab = abfd->sections; stab; stab = stab->next)
5494 if (CONST_STRNEQ (stab->name, ".stab")
5495 && (!stab->name[5] ||
5496 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5497 && (stab->flags & SEC_MERGE) == 0
5498 && !bfd_is_abs_section (stab->output_section))
5500 struct bfd_elf_section_data *secdata;
5502 secdata = elf_section_data (stab);
5503 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5504 stabstr, &secdata->sec_info,
5507 if (secdata->sec_info)
5508 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5513 if (is_elf_hash_table (htab) && add_needed)
5515 /* Add this bfd to the loaded list. */
5516 struct elf_link_loaded_list *n;
5518 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5522 n->next = htab->loaded;
5529 if (old_tab != NULL)
5531 if (old_strtab != NULL)
5533 if (nondeflt_vers != NULL)
5534 free (nondeflt_vers);
5535 if (extversym != NULL)
5538 if (isymbuf != NULL)
5544 /* Return the linker hash table entry of a symbol that might be
5545 satisfied by an archive symbol. Return -1 on error. */
5547 struct elf_link_hash_entry *
5548 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5549 struct bfd_link_info *info,
5552 struct elf_link_hash_entry *h;
5556 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5560 /* If this is a default version (the name contains @@), look up the
5561 symbol again with only one `@' as well as without the version.
5562 The effect is that references to the symbol with and without the
5563 version will be matched by the default symbol in the archive. */
5565 p = strchr (name, ELF_VER_CHR);
5566 if (p == NULL || p[1] != ELF_VER_CHR)
5569 /* First check with only one `@'. */
5570 len = strlen (name);
5571 copy = (char *) bfd_alloc (abfd, len);
5573 return (struct elf_link_hash_entry *) -1;
5575 first = p - name + 1;
5576 memcpy (copy, name, first);
5577 memcpy (copy + first, name + first + 1, len - first);
5579 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5582 /* We also need to check references to the symbol without the
5584 copy[first - 1] = '\0';
5585 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5586 FALSE, FALSE, TRUE);
5589 bfd_release (abfd, copy);
5593 /* Add symbols from an ELF archive file to the linker hash table. We
5594 don't use _bfd_generic_link_add_archive_symbols because we need to
5595 handle versioned symbols.
5597 Fortunately, ELF archive handling is simpler than that done by
5598 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5599 oddities. In ELF, if we find a symbol in the archive map, and the
5600 symbol is currently undefined, we know that we must pull in that
5603 Unfortunately, we do have to make multiple passes over the symbol
5604 table until nothing further is resolved. */
5607 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5610 unsigned char *included = NULL;
5614 const struct elf_backend_data *bed;
5615 struct elf_link_hash_entry * (*archive_symbol_lookup)
5616 (bfd *, struct bfd_link_info *, const char *);
5618 if (! bfd_has_map (abfd))
5620 /* An empty archive is a special case. */
5621 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5623 bfd_set_error (bfd_error_no_armap);
5627 /* Keep track of all symbols we know to be already defined, and all
5628 files we know to be already included. This is to speed up the
5629 second and subsequent passes. */
5630 c = bfd_ardata (abfd)->symdef_count;
5634 amt *= sizeof (*included);
5635 included = (unsigned char *) bfd_zmalloc (amt);
5636 if (included == NULL)
5639 symdefs = bfd_ardata (abfd)->symdefs;
5640 bed = get_elf_backend_data (abfd);
5641 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5654 symdefend = symdef + c;
5655 for (i = 0; symdef < symdefend; symdef++, i++)
5657 struct elf_link_hash_entry *h;
5659 struct bfd_link_hash_entry *undefs_tail;
5664 if (symdef->file_offset == last)
5670 h = archive_symbol_lookup (abfd, info, symdef->name);
5671 if (h == (struct elf_link_hash_entry *) -1)
5677 if (h->root.type == bfd_link_hash_common)
5679 /* We currently have a common symbol. The archive map contains
5680 a reference to this symbol, so we may want to include it. We
5681 only want to include it however, if this archive element
5682 contains a definition of the symbol, not just another common
5685 Unfortunately some archivers (including GNU ar) will put
5686 declarations of common symbols into their archive maps, as
5687 well as real definitions, so we cannot just go by the archive
5688 map alone. Instead we must read in the element's symbol
5689 table and check that to see what kind of symbol definition
5691 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5694 else if (h->root.type != bfd_link_hash_undefined)
5696 if (h->root.type != bfd_link_hash_undefweak)
5697 /* Symbol must be defined. Don't check it again. */
5702 /* We need to include this archive member. */
5703 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5704 if (element == NULL)
5707 if (! bfd_check_format (element, bfd_object))
5710 undefs_tail = info->hash->undefs_tail;
5712 if (!(*info->callbacks
5713 ->add_archive_element) (info, element, symdef->name, &element))
5715 if (!bfd_link_add_symbols (element, info))
5718 /* If there are any new undefined symbols, we need to make
5719 another pass through the archive in order to see whether
5720 they can be defined. FIXME: This isn't perfect, because
5721 common symbols wind up on undefs_tail and because an
5722 undefined symbol which is defined later on in this pass
5723 does not require another pass. This isn't a bug, but it
5724 does make the code less efficient than it could be. */
5725 if (undefs_tail != info->hash->undefs_tail)
5728 /* Look backward to mark all symbols from this object file
5729 which we have already seen in this pass. */
5733 included[mark] = TRUE;
5738 while (symdefs[mark].file_offset == symdef->file_offset);
5740 /* We mark subsequent symbols from this object file as we go
5741 on through the loop. */
5742 last = symdef->file_offset;
5752 if (included != NULL)
5757 /* Given an ELF BFD, add symbols to the global hash table as
5761 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5763 switch (bfd_get_format (abfd))
5766 return elf_link_add_object_symbols (abfd, info);
5768 return elf_link_add_archive_symbols (abfd, info);
5770 bfd_set_error (bfd_error_wrong_format);
5775 struct hash_codes_info
5777 unsigned long *hashcodes;
5781 /* This function will be called though elf_link_hash_traverse to store
5782 all hash value of the exported symbols in an array. */
5785 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5787 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5792 /* Ignore indirect symbols. These are added by the versioning code. */
5793 if (h->dynindx == -1)
5796 name = h->root.root.string;
5797 if (h->versioned >= versioned)
5799 char *p = strchr (name, ELF_VER_CHR);
5802 alc = (char *) bfd_malloc (p - name + 1);
5808 memcpy (alc, name, p - name);
5809 alc[p - name] = '\0';
5814 /* Compute the hash value. */
5815 ha = bfd_elf_hash (name);
5817 /* Store the found hash value in the array given as the argument. */
5818 *(inf->hashcodes)++ = ha;
5820 /* And store it in the struct so that we can put it in the hash table
5822 h->u.elf_hash_value = ha;
5830 struct collect_gnu_hash_codes
5833 const struct elf_backend_data *bed;
5834 unsigned long int nsyms;
5835 unsigned long int maskbits;
5836 unsigned long int *hashcodes;
5837 unsigned long int *hashval;
5838 unsigned long int *indx;
5839 unsigned long int *counts;
5842 long int min_dynindx;
5843 unsigned long int bucketcount;
5844 unsigned long int symindx;
5845 long int local_indx;
5846 long int shift1, shift2;
5847 unsigned long int mask;
5851 /* This function will be called though elf_link_hash_traverse to store
5852 all hash value of the exported symbols in an array. */
5855 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5857 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5862 /* Ignore indirect symbols. These are added by the versioning code. */
5863 if (h->dynindx == -1)
5866 /* Ignore also local symbols and undefined symbols. */
5867 if (! (*s->bed->elf_hash_symbol) (h))
5870 name = h->root.root.string;
5871 if (h->versioned >= versioned)
5873 char *p = strchr (name, ELF_VER_CHR);
5876 alc = (char *) bfd_malloc (p - name + 1);
5882 memcpy (alc, name, p - name);
5883 alc[p - name] = '\0';
5888 /* Compute the hash value. */
5889 ha = bfd_elf_gnu_hash (name);
5891 /* Store the found hash value in the array for compute_bucket_count,
5892 and also for .dynsym reordering purposes. */
5893 s->hashcodes[s->nsyms] = ha;
5894 s->hashval[h->dynindx] = ha;
5896 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5897 s->min_dynindx = h->dynindx;
5905 /* This function will be called though elf_link_hash_traverse to do
5906 final dynaminc symbol renumbering. */
5909 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5911 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5912 unsigned long int bucket;
5913 unsigned long int val;
5915 /* Ignore indirect symbols. */
5916 if (h->dynindx == -1)
5919 /* Ignore also local symbols and undefined symbols. */
5920 if (! (*s->bed->elf_hash_symbol) (h))
5922 if (h->dynindx >= s->min_dynindx)
5923 h->dynindx = s->local_indx++;
5927 bucket = s->hashval[h->dynindx] % s->bucketcount;
5928 val = (s->hashval[h->dynindx] >> s->shift1)
5929 & ((s->maskbits >> s->shift1) - 1);
5930 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5932 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5933 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5934 if (s->counts[bucket] == 1)
5935 /* Last element terminates the chain. */
5937 bfd_put_32 (s->output_bfd, val,
5938 s->contents + (s->indx[bucket] - s->symindx) * 4);
5939 --s->counts[bucket];
5940 h->dynindx = s->indx[bucket]++;
5944 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5947 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5949 return !(h->forced_local
5950 || h->root.type == bfd_link_hash_undefined
5951 || h->root.type == bfd_link_hash_undefweak
5952 || ((h->root.type == bfd_link_hash_defined
5953 || h->root.type == bfd_link_hash_defweak)
5954 && h->root.u.def.section->output_section == NULL));
5957 /* Array used to determine the number of hash table buckets to use
5958 based on the number of symbols there are. If there are fewer than
5959 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5960 fewer than 37 we use 17 buckets, and so forth. We never use more
5961 than 32771 buckets. */
5963 static const size_t elf_buckets[] =
5965 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5969 /* Compute bucket count for hashing table. We do not use a static set
5970 of possible tables sizes anymore. Instead we determine for all
5971 possible reasonable sizes of the table the outcome (i.e., the
5972 number of collisions etc) and choose the best solution. The
5973 weighting functions are not too simple to allow the table to grow
5974 without bounds. Instead one of the weighting factors is the size.
5975 Therefore the result is always a good payoff between few collisions
5976 (= short chain lengths) and table size. */
5978 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5979 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5980 unsigned long int nsyms,
5983 size_t best_size = 0;
5984 unsigned long int i;
5986 /* We have a problem here. The following code to optimize the table
5987 size requires an integer type with more the 32 bits. If
5988 BFD_HOST_U_64_BIT is set we know about such a type. */
5989 #ifdef BFD_HOST_U_64_BIT
5994 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5995 bfd *dynobj = elf_hash_table (info)->dynobj;
5996 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5997 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5998 unsigned long int *counts;
6000 unsigned int no_improvement_count = 0;
6002 /* Possible optimization parameters: if we have NSYMS symbols we say
6003 that the hashing table must at least have NSYMS/4 and at most
6005 minsize = nsyms / 4;
6008 best_size = maxsize = nsyms * 2;
6013 if ((best_size & 31) == 0)
6017 /* Create array where we count the collisions in. We must use bfd_malloc
6018 since the size could be large. */
6020 amt *= sizeof (unsigned long int);
6021 counts = (unsigned long int *) bfd_malloc (amt);
6025 /* Compute the "optimal" size for the hash table. The criteria is a
6026 minimal chain length. The minor criteria is (of course) the size
6028 for (i = minsize; i < maxsize; ++i)
6030 /* Walk through the array of hashcodes and count the collisions. */
6031 BFD_HOST_U_64_BIT max;
6032 unsigned long int j;
6033 unsigned long int fact;
6035 if (gnu_hash && (i & 31) == 0)
6038 memset (counts, '\0', i * sizeof (unsigned long int));
6040 /* Determine how often each hash bucket is used. */
6041 for (j = 0; j < nsyms; ++j)
6042 ++counts[hashcodes[j] % i];
6044 /* For the weight function we need some information about the
6045 pagesize on the target. This is information need not be 100%
6046 accurate. Since this information is not available (so far) we
6047 define it here to a reasonable default value. If it is crucial
6048 to have a better value some day simply define this value. */
6049 # ifndef BFD_TARGET_PAGESIZE
6050 # define BFD_TARGET_PAGESIZE (4096)
6053 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6055 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6058 /* Variant 1: optimize for short chains. We add the squares
6059 of all the chain lengths (which favors many small chain
6060 over a few long chains). */
6061 for (j = 0; j < i; ++j)
6062 max += counts[j] * counts[j];
6064 /* This adds penalties for the overall size of the table. */
6065 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6068 /* Variant 2: Optimize a lot more for small table. Here we
6069 also add squares of the size but we also add penalties for
6070 empty slots (the +1 term). */
6071 for (j = 0; j < i; ++j)
6072 max += (1 + counts[j]) * (1 + counts[j]);
6074 /* The overall size of the table is considered, but not as
6075 strong as in variant 1, where it is squared. */
6076 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6080 /* Compare with current best results. */
6081 if (max < best_chlen)
6085 no_improvement_count = 0;
6087 /* PR 11843: Avoid futile long searches for the best bucket size
6088 when there are a large number of symbols. */
6089 else if (++no_improvement_count == 100)
6096 #endif /* defined (BFD_HOST_U_64_BIT) */
6098 /* This is the fallback solution if no 64bit type is available or if we
6099 are not supposed to spend much time on optimizations. We select the
6100 bucket count using a fixed set of numbers. */
6101 for (i = 0; elf_buckets[i] != 0; i++)
6103 best_size = elf_buckets[i];
6104 if (nsyms < elf_buckets[i + 1])
6107 if (gnu_hash && best_size < 2)
6114 /* Size any SHT_GROUP section for ld -r. */
6117 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6122 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6123 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6124 && (s = ibfd->sections) != NULL
6125 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6126 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6131 /* Set a default stack segment size. The value in INFO wins. If it
6132 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6133 undefined it is initialized. */
6136 bfd_elf_stack_segment_size (bfd *output_bfd,
6137 struct bfd_link_info *info,
6138 const char *legacy_symbol,
6139 bfd_vma default_size)
6141 struct elf_link_hash_entry *h = NULL;
6143 /* Look for legacy symbol. */
6145 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6146 FALSE, FALSE, FALSE);
6147 if (h && (h->root.type == bfd_link_hash_defined
6148 || h->root.type == bfd_link_hash_defweak)
6150 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6152 /* The symbol has no type if specified on the command line. */
6153 h->type = STT_OBJECT;
6154 if (info->stacksize)
6155 /* xgettext:c-format */
6156 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6157 output_bfd, legacy_symbol);
6158 else if (h->root.u.def.section != bfd_abs_section_ptr)
6159 /* xgettext:c-format */
6160 _bfd_error_handler (_("%pB: %s not absolute"),
6161 output_bfd, legacy_symbol);
6163 info->stacksize = h->root.u.def.value;
6166 if (!info->stacksize)
6167 /* If the user didn't set a size, or explicitly inhibit the
6168 size, set it now. */
6169 info->stacksize = default_size;
6171 /* Provide the legacy symbol, if it is referenced. */
6172 if (h && (h->root.type == bfd_link_hash_undefined
6173 || h->root.type == bfd_link_hash_undefweak))
6175 struct bfd_link_hash_entry *bh = NULL;
6177 if (!(_bfd_generic_link_add_one_symbol
6178 (info, output_bfd, legacy_symbol,
6179 BSF_GLOBAL, bfd_abs_section_ptr,
6180 info->stacksize >= 0 ? info->stacksize : 0,
6181 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6184 h = (struct elf_link_hash_entry *) bh;
6186 h->type = STT_OBJECT;
6192 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6194 struct elf_gc_sweep_symbol_info
6196 struct bfd_link_info *info;
6197 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6202 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6205 && (((h->root.type == bfd_link_hash_defined
6206 || h->root.type == bfd_link_hash_defweak)
6207 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6208 && h->root.u.def.section->gc_mark))
6209 || h->root.type == bfd_link_hash_undefined
6210 || h->root.type == bfd_link_hash_undefweak))
6212 struct elf_gc_sweep_symbol_info *inf;
6214 inf = (struct elf_gc_sweep_symbol_info *) data;
6215 (*inf->hide_symbol) (inf->info, h, TRUE);
6218 h->ref_regular_nonweak = 0;
6224 /* Set up the sizes and contents of the ELF dynamic sections. This is
6225 called by the ELF linker emulation before_allocation routine. We
6226 must set the sizes of the sections before the linker sets the
6227 addresses of the various sections. */
6230 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6233 const char *filter_shlib,
6235 const char *depaudit,
6236 const char * const *auxiliary_filters,
6237 struct bfd_link_info *info,
6238 asection **sinterpptr)
6241 const struct elf_backend_data *bed;
6245 if (!is_elf_hash_table (info->hash))
6248 dynobj = elf_hash_table (info)->dynobj;
6250 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6252 struct bfd_elf_version_tree *verdefs;
6253 struct elf_info_failed asvinfo;
6254 struct bfd_elf_version_tree *t;
6255 struct bfd_elf_version_expr *d;
6259 /* If we are supposed to export all symbols into the dynamic symbol
6260 table (this is not the normal case), then do so. */
6261 if (info->export_dynamic
6262 || (bfd_link_executable (info) && info->dynamic))
6264 struct elf_info_failed eif;
6268 elf_link_hash_traverse (elf_hash_table (info),
6269 _bfd_elf_export_symbol,
6277 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6279 if (soname_indx == (size_t) -1
6280 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6284 soname_indx = (size_t) -1;
6286 /* Make all global versions with definition. */
6287 for (t = info->version_info; t != NULL; t = t->next)
6288 for (d = t->globals.list; d != NULL; d = d->next)
6289 if (!d->symver && d->literal)
6291 const char *verstr, *name;
6292 size_t namelen, verlen, newlen;
6293 char *newname, *p, leading_char;
6294 struct elf_link_hash_entry *newh;
6296 leading_char = bfd_get_symbol_leading_char (output_bfd);
6298 namelen = strlen (name) + (leading_char != '\0');
6300 verlen = strlen (verstr);
6301 newlen = namelen + verlen + 3;
6303 newname = (char *) bfd_malloc (newlen);
6304 if (newname == NULL)
6306 newname[0] = leading_char;
6307 memcpy (newname + (leading_char != '\0'), name, namelen);
6309 /* Check the hidden versioned definition. */
6310 p = newname + namelen;
6312 memcpy (p, verstr, verlen + 1);
6313 newh = elf_link_hash_lookup (elf_hash_table (info),
6314 newname, FALSE, FALSE,
6317 || (newh->root.type != bfd_link_hash_defined
6318 && newh->root.type != bfd_link_hash_defweak))
6320 /* Check the default versioned definition. */
6322 memcpy (p, verstr, verlen + 1);
6323 newh = elf_link_hash_lookup (elf_hash_table (info),
6324 newname, FALSE, FALSE,
6329 /* Mark this version if there is a definition and it is
6330 not defined in a shared object. */
6332 && !newh->def_dynamic
6333 && (newh->root.type == bfd_link_hash_defined
6334 || newh->root.type == bfd_link_hash_defweak))
6338 /* Attach all the symbols to their version information. */
6339 asvinfo.info = info;
6340 asvinfo.failed = FALSE;
6342 elf_link_hash_traverse (elf_hash_table (info),
6343 _bfd_elf_link_assign_sym_version,
6348 if (!info->allow_undefined_version)
6350 /* Check if all global versions have a definition. */
6351 bfd_boolean all_defined = TRUE;
6352 for (t = info->version_info; t != NULL; t = t->next)
6353 for (d = t->globals.list; d != NULL; d = d->next)
6354 if (d->literal && !d->symver && !d->script)
6357 (_("%s: undefined version: %s"),
6358 d->pattern, t->name);
6359 all_defined = FALSE;
6364 bfd_set_error (bfd_error_bad_value);
6369 /* Set up the version definition section. */
6370 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6371 BFD_ASSERT (s != NULL);
6373 /* We may have created additional version definitions if we are
6374 just linking a regular application. */
6375 verdefs = info->version_info;
6377 /* Skip anonymous version tag. */
6378 if (verdefs != NULL && verdefs->vernum == 0)
6379 verdefs = verdefs->next;
6381 if (verdefs == NULL && !info->create_default_symver)
6382 s->flags |= SEC_EXCLUDE;
6388 Elf_Internal_Verdef def;
6389 Elf_Internal_Verdaux defaux;
6390 struct bfd_link_hash_entry *bh;
6391 struct elf_link_hash_entry *h;
6397 /* Make space for the base version. */
6398 size += sizeof (Elf_External_Verdef);
6399 size += sizeof (Elf_External_Verdaux);
6402 /* Make space for the default version. */
6403 if (info->create_default_symver)
6405 size += sizeof (Elf_External_Verdef);
6409 for (t = verdefs; t != NULL; t = t->next)
6411 struct bfd_elf_version_deps *n;
6413 /* Don't emit base version twice. */
6417 size += sizeof (Elf_External_Verdef);
6418 size += sizeof (Elf_External_Verdaux);
6421 for (n = t->deps; n != NULL; n = n->next)
6422 size += sizeof (Elf_External_Verdaux);
6426 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6427 if (s->contents == NULL && s->size != 0)
6430 /* Fill in the version definition section. */
6434 def.vd_version = VER_DEF_CURRENT;
6435 def.vd_flags = VER_FLG_BASE;
6438 if (info->create_default_symver)
6440 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6441 def.vd_next = sizeof (Elf_External_Verdef);
6445 def.vd_aux = sizeof (Elf_External_Verdef);
6446 def.vd_next = (sizeof (Elf_External_Verdef)
6447 + sizeof (Elf_External_Verdaux));
6450 if (soname_indx != (size_t) -1)
6452 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6454 def.vd_hash = bfd_elf_hash (soname);
6455 defaux.vda_name = soname_indx;
6462 name = lbasename (output_bfd->filename);
6463 def.vd_hash = bfd_elf_hash (name);
6464 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6466 if (indx == (size_t) -1)
6468 defaux.vda_name = indx;
6470 defaux.vda_next = 0;
6472 _bfd_elf_swap_verdef_out (output_bfd, &def,
6473 (Elf_External_Verdef *) p);
6474 p += sizeof (Elf_External_Verdef);
6475 if (info->create_default_symver)
6477 /* Add a symbol representing this version. */
6479 if (! (_bfd_generic_link_add_one_symbol
6480 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6482 get_elf_backend_data (dynobj)->collect, &bh)))
6484 h = (struct elf_link_hash_entry *) bh;
6487 h->type = STT_OBJECT;
6488 h->verinfo.vertree = NULL;
6490 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6493 /* Create a duplicate of the base version with the same
6494 aux block, but different flags. */
6497 def.vd_aux = sizeof (Elf_External_Verdef);
6499 def.vd_next = (sizeof (Elf_External_Verdef)
6500 + sizeof (Elf_External_Verdaux));
6503 _bfd_elf_swap_verdef_out (output_bfd, &def,
6504 (Elf_External_Verdef *) p);
6505 p += sizeof (Elf_External_Verdef);
6507 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6508 (Elf_External_Verdaux *) p);
6509 p += sizeof (Elf_External_Verdaux);
6511 for (t = verdefs; t != NULL; t = t->next)
6514 struct bfd_elf_version_deps *n;
6516 /* Don't emit the base version twice. */
6521 for (n = t->deps; n != NULL; n = n->next)
6524 /* Add a symbol representing this version. */
6526 if (! (_bfd_generic_link_add_one_symbol
6527 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6529 get_elf_backend_data (dynobj)->collect, &bh)))
6531 h = (struct elf_link_hash_entry *) bh;
6534 h->type = STT_OBJECT;
6535 h->verinfo.vertree = t;
6537 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6540 def.vd_version = VER_DEF_CURRENT;
6542 if (t->globals.list == NULL
6543 && t->locals.list == NULL
6545 def.vd_flags |= VER_FLG_WEAK;
6546 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6547 def.vd_cnt = cdeps + 1;
6548 def.vd_hash = bfd_elf_hash (t->name);
6549 def.vd_aux = sizeof (Elf_External_Verdef);
6552 /* If a basever node is next, it *must* be the last node in
6553 the chain, otherwise Verdef construction breaks. */
6554 if (t->next != NULL && t->next->vernum == 0)
6555 BFD_ASSERT (t->next->next == NULL);
6557 if (t->next != NULL && t->next->vernum != 0)
6558 def.vd_next = (sizeof (Elf_External_Verdef)
6559 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6561 _bfd_elf_swap_verdef_out (output_bfd, &def,
6562 (Elf_External_Verdef *) p);
6563 p += sizeof (Elf_External_Verdef);
6565 defaux.vda_name = h->dynstr_index;
6566 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6568 defaux.vda_next = 0;
6569 if (t->deps != NULL)
6570 defaux.vda_next = sizeof (Elf_External_Verdaux);
6571 t->name_indx = defaux.vda_name;
6573 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6574 (Elf_External_Verdaux *) p);
6575 p += sizeof (Elf_External_Verdaux);
6577 for (n = t->deps; n != NULL; n = n->next)
6579 if (n->version_needed == NULL)
6581 /* This can happen if there was an error in the
6583 defaux.vda_name = 0;
6587 defaux.vda_name = n->version_needed->name_indx;
6588 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6591 if (n->next == NULL)
6592 defaux.vda_next = 0;
6594 defaux.vda_next = sizeof (Elf_External_Verdaux);
6596 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6597 (Elf_External_Verdaux *) p);
6598 p += sizeof (Elf_External_Verdaux);
6602 elf_tdata (output_bfd)->cverdefs = cdefs;
6606 bed = get_elf_backend_data (output_bfd);
6608 if (info->gc_sections && bed->can_gc_sections)
6610 struct elf_gc_sweep_symbol_info sweep_info;
6612 /* Remove the symbols that were in the swept sections from the
6613 dynamic symbol table. */
6614 sweep_info.info = info;
6615 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6616 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6620 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6623 struct elf_find_verdep_info sinfo;
6625 /* Work out the size of the version reference section. */
6627 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6628 BFD_ASSERT (s != NULL);
6631 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6632 if (sinfo.vers == 0)
6634 sinfo.failed = FALSE;
6636 elf_link_hash_traverse (elf_hash_table (info),
6637 _bfd_elf_link_find_version_dependencies,
6642 if (elf_tdata (output_bfd)->verref == NULL)
6643 s->flags |= SEC_EXCLUDE;
6646 Elf_Internal_Verneed *vn;
6651 /* Build the version dependency section. */
6654 for (vn = elf_tdata (output_bfd)->verref;
6656 vn = vn->vn_nextref)
6658 Elf_Internal_Vernaux *a;
6660 size += sizeof (Elf_External_Verneed);
6662 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6663 size += sizeof (Elf_External_Vernaux);
6667 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6668 if (s->contents == NULL)
6672 for (vn = elf_tdata (output_bfd)->verref;
6674 vn = vn->vn_nextref)
6677 Elf_Internal_Vernaux *a;
6681 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6684 vn->vn_version = VER_NEED_CURRENT;
6686 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6687 elf_dt_name (vn->vn_bfd) != NULL
6688 ? elf_dt_name (vn->vn_bfd)
6689 : lbasename (vn->vn_bfd->filename),
6691 if (indx == (size_t) -1)
6694 vn->vn_aux = sizeof (Elf_External_Verneed);
6695 if (vn->vn_nextref == NULL)
6698 vn->vn_next = (sizeof (Elf_External_Verneed)
6699 + caux * sizeof (Elf_External_Vernaux));
6701 _bfd_elf_swap_verneed_out (output_bfd, vn,
6702 (Elf_External_Verneed *) p);
6703 p += sizeof (Elf_External_Verneed);
6705 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6707 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6708 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6709 a->vna_nodename, FALSE);
6710 if (indx == (size_t) -1)
6713 if (a->vna_nextptr == NULL)
6716 a->vna_next = sizeof (Elf_External_Vernaux);
6718 _bfd_elf_swap_vernaux_out (output_bfd, a,
6719 (Elf_External_Vernaux *) p);
6720 p += sizeof (Elf_External_Vernaux);
6724 elf_tdata (output_bfd)->cverrefs = crefs;
6728 /* Any syms created from now on start with -1 in
6729 got.refcount/offset and plt.refcount/offset. */
6730 elf_hash_table (info)->init_got_refcount
6731 = elf_hash_table (info)->init_got_offset;
6732 elf_hash_table (info)->init_plt_refcount
6733 = elf_hash_table (info)->init_plt_offset;
6735 if (bfd_link_relocatable (info)
6736 && !_bfd_elf_size_group_sections (info))
6739 /* The backend may have to create some sections regardless of whether
6740 we're dynamic or not. */
6741 if (bed->elf_backend_always_size_sections
6742 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6745 /* Determine any GNU_STACK segment requirements, after the backend
6746 has had a chance to set a default segment size. */
6747 if (info->execstack)
6748 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6749 else if (info->noexecstack)
6750 elf_stack_flags (output_bfd) = PF_R | PF_W;
6754 asection *notesec = NULL;
6757 for (inputobj = info->input_bfds;
6759 inputobj = inputobj->link.next)
6764 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6766 s = inputobj->sections;
6767 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6770 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6773 if (s->flags & SEC_CODE)
6777 else if (bed->default_execstack)
6780 if (notesec || info->stacksize > 0)
6781 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6782 if (notesec && exec && bfd_link_relocatable (info)
6783 && notesec->output_section != bfd_abs_section_ptr)
6784 notesec->output_section->flags |= SEC_CODE;
6787 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6789 struct elf_info_failed eif;
6790 struct elf_link_hash_entry *h;
6794 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6795 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6799 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6801 info->flags |= DF_SYMBOLIC;
6809 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6811 if (indx == (size_t) -1)
6814 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6815 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6819 if (filter_shlib != NULL)
6823 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6824 filter_shlib, TRUE);
6825 if (indx == (size_t) -1
6826 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6830 if (auxiliary_filters != NULL)
6832 const char * const *p;
6834 for (p = auxiliary_filters; *p != NULL; p++)
6838 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6840 if (indx == (size_t) -1
6841 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6850 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6852 if (indx == (size_t) -1
6853 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6857 if (depaudit != NULL)
6861 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6863 if (indx == (size_t) -1
6864 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6871 /* Find all symbols which were defined in a dynamic object and make
6872 the backend pick a reasonable value for them. */
6873 elf_link_hash_traverse (elf_hash_table (info),
6874 _bfd_elf_adjust_dynamic_symbol,
6879 /* Add some entries to the .dynamic section. We fill in some of the
6880 values later, in bfd_elf_final_link, but we must add the entries
6881 now so that we know the final size of the .dynamic section. */
6883 /* If there are initialization and/or finalization functions to
6884 call then add the corresponding DT_INIT/DT_FINI entries. */
6885 h = (info->init_function
6886 ? elf_link_hash_lookup (elf_hash_table (info),
6887 info->init_function, FALSE,
6894 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6897 h = (info->fini_function
6898 ? elf_link_hash_lookup (elf_hash_table (info),
6899 info->fini_function, FALSE,
6906 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6910 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6911 if (s != NULL && s->linker_has_input)
6913 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6914 if (! bfd_link_executable (info))
6919 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6920 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6921 && (o = sub->sections) != NULL
6922 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6923 for (o = sub->sections; o != NULL; o = o->next)
6924 if (elf_section_data (o)->this_hdr.sh_type
6925 == SHT_PREINIT_ARRAY)
6928 (_("%pB: .preinit_array section is not allowed in DSO"),
6933 bfd_set_error (bfd_error_nonrepresentable_section);
6937 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6938 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6941 s = bfd_get_section_by_name (output_bfd, ".init_array");
6942 if (s != NULL && s->linker_has_input)
6944 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6945 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6948 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6949 if (s != NULL && s->linker_has_input)
6951 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6952 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6956 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6957 /* If .dynstr is excluded from the link, we don't want any of
6958 these tags. Strictly, we should be checking each section
6959 individually; This quick check covers for the case where
6960 someone does a /DISCARD/ : { *(*) }. */
6961 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6963 bfd_size_type strsize;
6965 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6966 if ((info->emit_hash
6967 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6968 || (info->emit_gnu_hash
6969 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6970 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6971 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6972 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6973 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6974 bed->s->sizeof_sym))
6979 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6982 /* The backend must work out the sizes of all the other dynamic
6985 && bed->elf_backend_size_dynamic_sections != NULL
6986 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6989 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6991 if (elf_tdata (output_bfd)->cverdefs)
6993 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6995 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6996 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7000 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7002 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7005 else if (info->flags & DF_BIND_NOW)
7007 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7013 if (bfd_link_executable (info))
7014 info->flags_1 &= ~ (DF_1_INITFIRST
7017 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7021 if (elf_tdata (output_bfd)->cverrefs)
7023 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7025 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7026 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7030 if ((elf_tdata (output_bfd)->cverrefs == 0
7031 && elf_tdata (output_bfd)->cverdefs == 0)
7032 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7036 s = bfd_get_linker_section (dynobj, ".gnu.version");
7037 s->flags |= SEC_EXCLUDE;
7043 /* Find the first non-excluded output section. We'll use its
7044 section symbol for some emitted relocs. */
7046 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7050 for (s = output_bfd->sections; s != NULL; s = s->next)
7051 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7052 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7054 elf_hash_table (info)->text_index_section = s;
7059 /* Find two non-excluded output sections, one for code, one for data.
7060 We'll use their section symbols for some emitted relocs. */
7062 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7066 /* Data first, since setting text_index_section changes
7067 _bfd_elf_omit_section_dynsym_default. */
7068 for (s = output_bfd->sections; s != NULL; s = s->next)
7069 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7070 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7072 elf_hash_table (info)->data_index_section = s;
7076 for (s = output_bfd->sections; s != NULL; s = s->next)
7077 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7078 == (SEC_ALLOC | SEC_READONLY))
7079 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7081 elf_hash_table (info)->text_index_section = s;
7085 if (elf_hash_table (info)->text_index_section == NULL)
7086 elf_hash_table (info)->text_index_section
7087 = elf_hash_table (info)->data_index_section;
7091 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7093 const struct elf_backend_data *bed;
7094 unsigned long section_sym_count;
7095 bfd_size_type dynsymcount = 0;
7097 if (!is_elf_hash_table (info->hash))
7100 bed = get_elf_backend_data (output_bfd);
7101 (*bed->elf_backend_init_index_section) (output_bfd, info);
7103 /* Assign dynsym indices. In a shared library we generate a section
7104 symbol for each output section, which come first. Next come all
7105 of the back-end allocated local dynamic syms, followed by the rest
7106 of the global symbols.
7108 This is usually not needed for static binaries, however backends
7109 can request to always do it, e.g. the MIPS backend uses dynamic
7110 symbol counts to lay out GOT, which will be produced in the
7111 presence of GOT relocations even in static binaries (holding fixed
7112 data in that case, to satisfy those relocations). */
7114 if (elf_hash_table (info)->dynamic_sections_created
7115 || bed->always_renumber_dynsyms)
7116 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7117 §ion_sym_count);
7119 if (elf_hash_table (info)->dynamic_sections_created)
7123 unsigned int dtagcount;
7125 dynobj = elf_hash_table (info)->dynobj;
7127 /* Work out the size of the symbol version section. */
7128 s = bfd_get_linker_section (dynobj, ".gnu.version");
7129 BFD_ASSERT (s != NULL);
7130 if ((s->flags & SEC_EXCLUDE) == 0)
7132 s->size = dynsymcount * sizeof (Elf_External_Versym);
7133 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7134 if (s->contents == NULL)
7137 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7141 /* Set the size of the .dynsym and .hash sections. We counted
7142 the number of dynamic symbols in elf_link_add_object_symbols.
7143 We will build the contents of .dynsym and .hash when we build
7144 the final symbol table, because until then we do not know the
7145 correct value to give the symbols. We built the .dynstr
7146 section as we went along in elf_link_add_object_symbols. */
7147 s = elf_hash_table (info)->dynsym;
7148 BFD_ASSERT (s != NULL);
7149 s->size = dynsymcount * bed->s->sizeof_sym;
7151 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7152 if (s->contents == NULL)
7155 /* The first entry in .dynsym is a dummy symbol. Clear all the
7156 section syms, in case we don't output them all. */
7157 ++section_sym_count;
7158 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7160 elf_hash_table (info)->bucketcount = 0;
7162 /* Compute the size of the hashing table. As a side effect this
7163 computes the hash values for all the names we export. */
7164 if (info->emit_hash)
7166 unsigned long int *hashcodes;
7167 struct hash_codes_info hashinf;
7169 unsigned long int nsyms;
7171 size_t hash_entry_size;
7173 /* Compute the hash values for all exported symbols. At the same
7174 time store the values in an array so that we could use them for
7176 amt = dynsymcount * sizeof (unsigned long int);
7177 hashcodes = (unsigned long int *) bfd_malloc (amt);
7178 if (hashcodes == NULL)
7180 hashinf.hashcodes = hashcodes;
7181 hashinf.error = FALSE;
7183 /* Put all hash values in HASHCODES. */
7184 elf_link_hash_traverse (elf_hash_table (info),
7185 elf_collect_hash_codes, &hashinf);
7192 nsyms = hashinf.hashcodes - hashcodes;
7194 = compute_bucket_count (info, hashcodes, nsyms, 0);
7197 if (bucketcount == 0 && nsyms > 0)
7200 elf_hash_table (info)->bucketcount = bucketcount;
7202 s = bfd_get_linker_section (dynobj, ".hash");
7203 BFD_ASSERT (s != NULL);
7204 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7205 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7206 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7207 if (s->contents == NULL)
7210 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7211 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7212 s->contents + hash_entry_size);
7215 if (info->emit_gnu_hash)
7218 unsigned char *contents;
7219 struct collect_gnu_hash_codes cinfo;
7223 memset (&cinfo, 0, sizeof (cinfo));
7225 /* Compute the hash values for all exported symbols. At the same
7226 time store the values in an array so that we could use them for
7228 amt = dynsymcount * 2 * sizeof (unsigned long int);
7229 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7230 if (cinfo.hashcodes == NULL)
7233 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7234 cinfo.min_dynindx = -1;
7235 cinfo.output_bfd = output_bfd;
7238 /* Put all hash values in HASHCODES. */
7239 elf_link_hash_traverse (elf_hash_table (info),
7240 elf_collect_gnu_hash_codes, &cinfo);
7243 free (cinfo.hashcodes);
7248 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7250 if (bucketcount == 0)
7252 free (cinfo.hashcodes);
7256 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7257 BFD_ASSERT (s != NULL);
7259 if (cinfo.nsyms == 0)
7261 /* Empty .gnu.hash section is special. */
7262 BFD_ASSERT (cinfo.min_dynindx == -1);
7263 free (cinfo.hashcodes);
7264 s->size = 5 * 4 + bed->s->arch_size / 8;
7265 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7266 if (contents == NULL)
7268 s->contents = contents;
7269 /* 1 empty bucket. */
7270 bfd_put_32 (output_bfd, 1, contents);
7271 /* SYMIDX above the special symbol 0. */
7272 bfd_put_32 (output_bfd, 1, contents + 4);
7273 /* Just one word for bitmask. */
7274 bfd_put_32 (output_bfd, 1, contents + 8);
7275 /* Only hash fn bloom filter. */
7276 bfd_put_32 (output_bfd, 0, contents + 12);
7277 /* No hashes are valid - empty bitmask. */
7278 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7279 /* No hashes in the only bucket. */
7280 bfd_put_32 (output_bfd, 0,
7281 contents + 16 + bed->s->arch_size / 8);
7285 unsigned long int maskwords, maskbitslog2, x;
7286 BFD_ASSERT (cinfo.min_dynindx != -1);
7290 while ((x >>= 1) != 0)
7292 if (maskbitslog2 < 3)
7294 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7295 maskbitslog2 = maskbitslog2 + 3;
7297 maskbitslog2 = maskbitslog2 + 2;
7298 if (bed->s->arch_size == 64)
7300 if (maskbitslog2 == 5)
7306 cinfo.mask = (1 << cinfo.shift1) - 1;
7307 cinfo.shift2 = maskbitslog2;
7308 cinfo.maskbits = 1 << maskbitslog2;
7309 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7310 amt = bucketcount * sizeof (unsigned long int) * 2;
7311 amt += maskwords * sizeof (bfd_vma);
7312 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7313 if (cinfo.bitmask == NULL)
7315 free (cinfo.hashcodes);
7319 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7320 cinfo.indx = cinfo.counts + bucketcount;
7321 cinfo.symindx = dynsymcount - cinfo.nsyms;
7322 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7324 /* Determine how often each hash bucket is used. */
7325 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7326 for (i = 0; i < cinfo.nsyms; ++i)
7327 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7329 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7330 if (cinfo.counts[i] != 0)
7332 cinfo.indx[i] = cnt;
7333 cnt += cinfo.counts[i];
7335 BFD_ASSERT (cnt == dynsymcount);
7336 cinfo.bucketcount = bucketcount;
7337 cinfo.local_indx = cinfo.min_dynindx;
7339 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7340 s->size += cinfo.maskbits / 8;
7341 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7342 if (contents == NULL)
7344 free (cinfo.bitmask);
7345 free (cinfo.hashcodes);
7349 s->contents = contents;
7350 bfd_put_32 (output_bfd, bucketcount, contents);
7351 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7352 bfd_put_32 (output_bfd, maskwords, contents + 8);
7353 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7354 contents += 16 + cinfo.maskbits / 8;
7356 for (i = 0; i < bucketcount; ++i)
7358 if (cinfo.counts[i] == 0)
7359 bfd_put_32 (output_bfd, 0, contents);
7361 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7365 cinfo.contents = contents;
7367 /* Renumber dynamic symbols, populate .gnu.hash section. */
7368 elf_link_hash_traverse (elf_hash_table (info),
7369 elf_renumber_gnu_hash_syms, &cinfo);
7371 contents = s->contents + 16;
7372 for (i = 0; i < maskwords; ++i)
7374 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7376 contents += bed->s->arch_size / 8;
7379 free (cinfo.bitmask);
7380 free (cinfo.hashcodes);
7384 s = bfd_get_linker_section (dynobj, ".dynstr");
7385 BFD_ASSERT (s != NULL);
7387 elf_finalize_dynstr (output_bfd, info);
7389 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7391 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7392 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7399 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7402 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7405 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7406 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7409 /* Finish SHF_MERGE section merging. */
7412 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7417 if (!is_elf_hash_table (info->hash))
7420 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7421 if ((ibfd->flags & DYNAMIC) == 0
7422 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7423 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7424 == get_elf_backend_data (obfd)->s->elfclass))
7425 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7426 if ((sec->flags & SEC_MERGE) != 0
7427 && !bfd_is_abs_section (sec->output_section))
7429 struct bfd_elf_section_data *secdata;
7431 secdata = elf_section_data (sec);
7432 if (! _bfd_add_merge_section (obfd,
7433 &elf_hash_table (info)->merge_info,
7434 sec, &secdata->sec_info))
7436 else if (secdata->sec_info)
7437 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7440 if (elf_hash_table (info)->merge_info != NULL)
7441 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7442 merge_sections_remove_hook);
7446 /* Create an entry in an ELF linker hash table. */
7448 struct bfd_hash_entry *
7449 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7450 struct bfd_hash_table *table,
7453 /* Allocate the structure if it has not already been allocated by a
7457 entry = (struct bfd_hash_entry *)
7458 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7463 /* Call the allocation method of the superclass. */
7464 entry = _bfd_link_hash_newfunc (entry, table, string);
7467 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7468 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7470 /* Set local fields. */
7473 ret->got = htab->init_got_refcount;
7474 ret->plt = htab->init_plt_refcount;
7475 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7476 - offsetof (struct elf_link_hash_entry, size)));
7477 /* Assume that we have been called by a non-ELF symbol reader.
7478 This flag is then reset by the code which reads an ELF input
7479 file. This ensures that a symbol created by a non-ELF symbol
7480 reader will have the flag set correctly. */
7487 /* Copy data from an indirect symbol to its direct symbol, hiding the
7488 old indirect symbol. Also used for copying flags to a weakdef. */
7491 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7492 struct elf_link_hash_entry *dir,
7493 struct elf_link_hash_entry *ind)
7495 struct elf_link_hash_table *htab;
7497 /* Copy down any references that we may have already seen to the
7498 symbol which just became indirect. */
7500 if (dir->versioned != versioned_hidden)
7501 dir->ref_dynamic |= ind->ref_dynamic;
7502 dir->ref_regular |= ind->ref_regular;
7503 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7504 dir->non_got_ref |= ind->non_got_ref;
7505 dir->needs_plt |= ind->needs_plt;
7506 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7508 if (ind->root.type != bfd_link_hash_indirect)
7511 /* Copy over the global and procedure linkage table refcount entries.
7512 These may have been already set up by a check_relocs routine. */
7513 htab = elf_hash_table (info);
7514 if (ind->got.refcount > htab->init_got_refcount.refcount)
7516 if (dir->got.refcount < 0)
7517 dir->got.refcount = 0;
7518 dir->got.refcount += ind->got.refcount;
7519 ind->got.refcount = htab->init_got_refcount.refcount;
7522 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7524 if (dir->plt.refcount < 0)
7525 dir->plt.refcount = 0;
7526 dir->plt.refcount += ind->plt.refcount;
7527 ind->plt.refcount = htab->init_plt_refcount.refcount;
7530 if (ind->dynindx != -1)
7532 if (dir->dynindx != -1)
7533 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7534 dir->dynindx = ind->dynindx;
7535 dir->dynstr_index = ind->dynstr_index;
7537 ind->dynstr_index = 0;
7542 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7543 struct elf_link_hash_entry *h,
7544 bfd_boolean force_local)
7546 /* STT_GNU_IFUNC symbol must go through PLT. */
7547 if (h->type != STT_GNU_IFUNC)
7549 h->plt = elf_hash_table (info)->init_plt_offset;
7554 h->forced_local = 1;
7555 if (h->dynindx != -1)
7557 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7560 h->dynstr_index = 0;
7565 /* Hide a symbol. */
7568 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7569 struct bfd_link_info *info,
7570 struct bfd_link_hash_entry *h)
7572 if (is_elf_hash_table (info->hash))
7574 const struct elf_backend_data *bed
7575 = get_elf_backend_data (output_bfd);
7576 struct elf_link_hash_entry *eh
7577 = (struct elf_link_hash_entry *) h;
7578 bed->elf_backend_hide_symbol (info, eh, TRUE);
7579 eh->def_dynamic = 0;
7580 eh->ref_dynamic = 0;
7581 eh->dynamic_def = 0;
7585 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7589 _bfd_elf_link_hash_table_init
7590 (struct elf_link_hash_table *table,
7592 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7593 struct bfd_hash_table *,
7595 unsigned int entsize,
7596 enum elf_target_id target_id)
7599 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7601 table->init_got_refcount.refcount = can_refcount - 1;
7602 table->init_plt_refcount.refcount = can_refcount - 1;
7603 table->init_got_offset.offset = -(bfd_vma) 1;
7604 table->init_plt_offset.offset = -(bfd_vma) 1;
7605 /* The first dynamic symbol is a dummy. */
7606 table->dynsymcount = 1;
7608 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7610 table->root.type = bfd_link_elf_hash_table;
7611 table->hash_table_id = target_id;
7616 /* Create an ELF linker hash table. */
7618 struct bfd_link_hash_table *
7619 _bfd_elf_link_hash_table_create (bfd *abfd)
7621 struct elf_link_hash_table *ret;
7622 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7624 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7628 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7629 sizeof (struct elf_link_hash_entry),
7635 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7640 /* Destroy an ELF linker hash table. */
7643 _bfd_elf_link_hash_table_free (bfd *obfd)
7645 struct elf_link_hash_table *htab;
7647 htab = (struct elf_link_hash_table *) obfd->link.hash;
7648 if (htab->dynstr != NULL)
7649 _bfd_elf_strtab_free (htab->dynstr);
7650 _bfd_merge_sections_free (htab->merge_info);
7651 _bfd_generic_link_hash_table_free (obfd);
7654 /* This is a hook for the ELF emulation code in the generic linker to
7655 tell the backend linker what file name to use for the DT_NEEDED
7656 entry for a dynamic object. */
7659 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7661 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7662 && bfd_get_format (abfd) == bfd_object)
7663 elf_dt_name (abfd) = name;
7667 bfd_elf_get_dyn_lib_class (bfd *abfd)
7670 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7671 && bfd_get_format (abfd) == bfd_object)
7672 lib_class = elf_dyn_lib_class (abfd);
7679 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7681 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7682 && bfd_get_format (abfd) == bfd_object)
7683 elf_dyn_lib_class (abfd) = lib_class;
7686 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7687 the linker ELF emulation code. */
7689 struct bfd_link_needed_list *
7690 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7691 struct bfd_link_info *info)
7693 if (! is_elf_hash_table (info->hash))
7695 return elf_hash_table (info)->needed;
7698 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7699 hook for the linker ELF emulation code. */
7701 struct bfd_link_needed_list *
7702 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7703 struct bfd_link_info *info)
7705 if (! is_elf_hash_table (info->hash))
7707 return elf_hash_table (info)->runpath;
7710 /* Get the name actually used for a dynamic object for a link. This
7711 is the SONAME entry if there is one. Otherwise, it is the string
7712 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7715 bfd_elf_get_dt_soname (bfd *abfd)
7717 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7718 && bfd_get_format (abfd) == bfd_object)
7719 return elf_dt_name (abfd);
7723 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7724 the ELF linker emulation code. */
7727 bfd_elf_get_bfd_needed_list (bfd *abfd,
7728 struct bfd_link_needed_list **pneeded)
7731 bfd_byte *dynbuf = NULL;
7732 unsigned int elfsec;
7733 unsigned long shlink;
7734 bfd_byte *extdyn, *extdynend;
7736 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7740 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7741 || bfd_get_format (abfd) != bfd_object)
7744 s = bfd_get_section_by_name (abfd, ".dynamic");
7745 if (s == NULL || s->size == 0)
7748 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7751 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7752 if (elfsec == SHN_BAD)
7755 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7757 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7758 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7761 extdynend = extdyn + s->size;
7762 for (; extdyn < extdynend; extdyn += extdynsize)
7764 Elf_Internal_Dyn dyn;
7766 (*swap_dyn_in) (abfd, extdyn, &dyn);
7768 if (dyn.d_tag == DT_NULL)
7771 if (dyn.d_tag == DT_NEEDED)
7774 struct bfd_link_needed_list *l;
7775 unsigned int tagv = dyn.d_un.d_val;
7778 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7783 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7804 struct elf_symbuf_symbol
7806 unsigned long st_name; /* Symbol name, index in string tbl */
7807 unsigned char st_info; /* Type and binding attributes */
7808 unsigned char st_other; /* Visibilty, and target specific */
7811 struct elf_symbuf_head
7813 struct elf_symbuf_symbol *ssym;
7815 unsigned int st_shndx;
7822 Elf_Internal_Sym *isym;
7823 struct elf_symbuf_symbol *ssym;
7828 /* Sort references to symbols by ascending section number. */
7831 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7833 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7834 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7836 return s1->st_shndx - s2->st_shndx;
7840 elf_sym_name_compare (const void *arg1, const void *arg2)
7842 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7843 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7844 return strcmp (s1->name, s2->name);
7847 static struct elf_symbuf_head *
7848 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7850 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7851 struct elf_symbuf_symbol *ssym;
7852 struct elf_symbuf_head *ssymbuf, *ssymhead;
7853 size_t i, shndx_count, total_size;
7855 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7859 for (ind = indbuf, i = 0; i < symcount; i++)
7860 if (isymbuf[i].st_shndx != SHN_UNDEF)
7861 *ind++ = &isymbuf[i];
7864 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7865 elf_sort_elf_symbol);
7868 if (indbufend > indbuf)
7869 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7870 if (ind[0]->st_shndx != ind[1]->st_shndx)
7873 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7874 + (indbufend - indbuf) * sizeof (*ssym));
7875 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7876 if (ssymbuf == NULL)
7882 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7883 ssymbuf->ssym = NULL;
7884 ssymbuf->count = shndx_count;
7885 ssymbuf->st_shndx = 0;
7886 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7888 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7891 ssymhead->ssym = ssym;
7892 ssymhead->count = 0;
7893 ssymhead->st_shndx = (*ind)->st_shndx;
7895 ssym->st_name = (*ind)->st_name;
7896 ssym->st_info = (*ind)->st_info;
7897 ssym->st_other = (*ind)->st_other;
7900 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7901 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7908 /* Check if 2 sections define the same set of local and global
7912 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7913 struct bfd_link_info *info)
7916 const struct elf_backend_data *bed1, *bed2;
7917 Elf_Internal_Shdr *hdr1, *hdr2;
7918 size_t symcount1, symcount2;
7919 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7920 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7921 Elf_Internal_Sym *isym, *isymend;
7922 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7923 size_t count1, count2, i;
7924 unsigned int shndx1, shndx2;
7930 /* Both sections have to be in ELF. */
7931 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7932 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7935 if (elf_section_type (sec1) != elf_section_type (sec2))
7938 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7939 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7940 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7943 bed1 = get_elf_backend_data (bfd1);
7944 bed2 = get_elf_backend_data (bfd2);
7945 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7946 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7947 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7948 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7950 if (symcount1 == 0 || symcount2 == 0)
7956 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7957 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7959 if (ssymbuf1 == NULL)
7961 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7963 if (isymbuf1 == NULL)
7966 if (!info->reduce_memory_overheads)
7967 elf_tdata (bfd1)->symbuf = ssymbuf1
7968 = elf_create_symbuf (symcount1, isymbuf1);
7971 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7973 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7975 if (isymbuf2 == NULL)
7978 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7979 elf_tdata (bfd2)->symbuf = ssymbuf2
7980 = elf_create_symbuf (symcount2, isymbuf2);
7983 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7985 /* Optimized faster version. */
7987 struct elf_symbol *symp;
7988 struct elf_symbuf_symbol *ssym, *ssymend;
7991 hi = ssymbuf1->count;
7996 mid = (lo + hi) / 2;
7997 if (shndx1 < ssymbuf1[mid].st_shndx)
7999 else if (shndx1 > ssymbuf1[mid].st_shndx)
8003 count1 = ssymbuf1[mid].count;
8010 hi = ssymbuf2->count;
8015 mid = (lo + hi) / 2;
8016 if (shndx2 < ssymbuf2[mid].st_shndx)
8018 else if (shndx2 > ssymbuf2[mid].st_shndx)
8022 count2 = ssymbuf2[mid].count;
8028 if (count1 == 0 || count2 == 0 || count1 != count2)
8032 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8034 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8035 if (symtable1 == NULL || symtable2 == NULL)
8039 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8040 ssym < ssymend; ssym++, symp++)
8042 symp->u.ssym = ssym;
8043 symp->name = bfd_elf_string_from_elf_section (bfd1,
8049 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8050 ssym < ssymend; ssym++, symp++)
8052 symp->u.ssym = ssym;
8053 symp->name = bfd_elf_string_from_elf_section (bfd2,
8058 /* Sort symbol by name. */
8059 qsort (symtable1, count1, sizeof (struct elf_symbol),
8060 elf_sym_name_compare);
8061 qsort (symtable2, count1, sizeof (struct elf_symbol),
8062 elf_sym_name_compare);
8064 for (i = 0; i < count1; i++)
8065 /* Two symbols must have the same binding, type and name. */
8066 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8067 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8068 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8075 symtable1 = (struct elf_symbol *)
8076 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8077 symtable2 = (struct elf_symbol *)
8078 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8079 if (symtable1 == NULL || symtable2 == NULL)
8082 /* Count definitions in the section. */
8084 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8085 if (isym->st_shndx == shndx1)
8086 symtable1[count1++].u.isym = isym;
8089 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8090 if (isym->st_shndx == shndx2)
8091 symtable2[count2++].u.isym = isym;
8093 if (count1 == 0 || count2 == 0 || count1 != count2)
8096 for (i = 0; i < count1; i++)
8098 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8099 symtable1[i].u.isym->st_name);
8101 for (i = 0; i < count2; i++)
8103 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8104 symtable2[i].u.isym->st_name);
8106 /* Sort symbol by name. */
8107 qsort (symtable1, count1, sizeof (struct elf_symbol),
8108 elf_sym_name_compare);
8109 qsort (symtable2, count1, sizeof (struct elf_symbol),
8110 elf_sym_name_compare);
8112 for (i = 0; i < count1; i++)
8113 /* Two symbols must have the same binding, type and name. */
8114 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8115 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8116 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8134 /* Return TRUE if 2 section types are compatible. */
8137 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8138 bfd *bbfd, const asection *bsec)
8142 || abfd->xvec->flavour != bfd_target_elf_flavour
8143 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8146 return elf_section_type (asec) == elf_section_type (bsec);
8149 /* Final phase of ELF linker. */
8151 /* A structure we use to avoid passing large numbers of arguments. */
8153 struct elf_final_link_info
8155 /* General link information. */
8156 struct bfd_link_info *info;
8159 /* Symbol string table. */
8160 struct elf_strtab_hash *symstrtab;
8161 /* .hash section. */
8163 /* symbol version section (.gnu.version). */
8164 asection *symver_sec;
8165 /* Buffer large enough to hold contents of any section. */
8167 /* Buffer large enough to hold external relocs of any section. */
8168 void *external_relocs;
8169 /* Buffer large enough to hold internal relocs of any section. */
8170 Elf_Internal_Rela *internal_relocs;
8171 /* Buffer large enough to hold external local symbols of any input
8173 bfd_byte *external_syms;
8174 /* And a buffer for symbol section indices. */
8175 Elf_External_Sym_Shndx *locsym_shndx;
8176 /* Buffer large enough to hold internal local symbols of any input
8178 Elf_Internal_Sym *internal_syms;
8179 /* Array large enough to hold a symbol index for each local symbol
8180 of any input BFD. */
8182 /* Array large enough to hold a section pointer for each local
8183 symbol of any input BFD. */
8184 asection **sections;
8185 /* Buffer for SHT_SYMTAB_SHNDX section. */
8186 Elf_External_Sym_Shndx *symshndxbuf;
8187 /* Number of STT_FILE syms seen. */
8188 size_t filesym_count;
8191 /* This struct is used to pass information to elf_link_output_extsym. */
8193 struct elf_outext_info
8196 bfd_boolean localsyms;
8197 bfd_boolean file_sym_done;
8198 struct elf_final_link_info *flinfo;
8202 /* Support for evaluating a complex relocation.
8204 Complex relocations are generalized, self-describing relocations. The
8205 implementation of them consists of two parts: complex symbols, and the
8206 relocations themselves.
8208 The relocations are use a reserved elf-wide relocation type code (R_RELC
8209 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8210 information (start bit, end bit, word width, etc) into the addend. This
8211 information is extracted from CGEN-generated operand tables within gas.
8213 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8214 internal) representing prefix-notation expressions, including but not
8215 limited to those sorts of expressions normally encoded as addends in the
8216 addend field. The symbol mangling format is:
8219 | <unary-operator> ':' <node>
8220 | <binary-operator> ':' <node> ':' <node>
8223 <literal> := 's' <digits=N> ':' <N character symbol name>
8224 | 'S' <digits=N> ':' <N character section name>
8228 <binary-operator> := as in C
8229 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8232 set_symbol_value (bfd *bfd_with_globals,
8233 Elf_Internal_Sym *isymbuf,
8238 struct elf_link_hash_entry **sym_hashes;
8239 struct elf_link_hash_entry *h;
8240 size_t extsymoff = locsymcount;
8242 if (symidx < locsymcount)
8244 Elf_Internal_Sym *sym;
8246 sym = isymbuf + symidx;
8247 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8249 /* It is a local symbol: move it to the
8250 "absolute" section and give it a value. */
8251 sym->st_shndx = SHN_ABS;
8252 sym->st_value = val;
8255 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8259 /* It is a global symbol: set its link type
8260 to "defined" and give it a value. */
8262 sym_hashes = elf_sym_hashes (bfd_with_globals);
8263 h = sym_hashes [symidx - extsymoff];
8264 while (h->root.type == bfd_link_hash_indirect
8265 || h->root.type == bfd_link_hash_warning)
8266 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8267 h->root.type = bfd_link_hash_defined;
8268 h->root.u.def.value = val;
8269 h->root.u.def.section = bfd_abs_section_ptr;
8273 resolve_symbol (const char *name,
8275 struct elf_final_link_info *flinfo,
8277 Elf_Internal_Sym *isymbuf,
8280 Elf_Internal_Sym *sym;
8281 struct bfd_link_hash_entry *global_entry;
8282 const char *candidate = NULL;
8283 Elf_Internal_Shdr *symtab_hdr;
8286 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8288 for (i = 0; i < locsymcount; ++ i)
8292 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8295 candidate = bfd_elf_string_from_elf_section (input_bfd,
8296 symtab_hdr->sh_link,
8299 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8300 name, candidate, (unsigned long) sym->st_value);
8302 if (candidate && strcmp (candidate, name) == 0)
8304 asection *sec = flinfo->sections [i];
8306 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8307 *result += sec->output_offset + sec->output_section->vma;
8309 printf ("Found symbol with value %8.8lx\n",
8310 (unsigned long) *result);
8316 /* Hmm, haven't found it yet. perhaps it is a global. */
8317 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8318 FALSE, FALSE, TRUE);
8322 if (global_entry->type == bfd_link_hash_defined
8323 || global_entry->type == bfd_link_hash_defweak)
8325 *result = (global_entry->u.def.value
8326 + global_entry->u.def.section->output_section->vma
8327 + global_entry->u.def.section->output_offset);
8329 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8330 global_entry->root.string, (unsigned long) *result);
8338 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8339 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8340 names like "foo.end" which is the end address of section "foo". */
8343 resolve_section (const char *name,
8351 for (curr = sections; curr; curr = curr->next)
8352 if (strcmp (curr->name, name) == 0)
8354 *result = curr->vma;
8358 /* Hmm. still haven't found it. try pseudo-section names. */
8359 /* FIXME: This could be coded more efficiently... */
8360 for (curr = sections; curr; curr = curr->next)
8362 len = strlen (curr->name);
8363 if (len > strlen (name))
8366 if (strncmp (curr->name, name, len) == 0)
8368 if (strncmp (".end", name + len, 4) == 0)
8370 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8374 /* Insert more pseudo-section names here, if you like. */
8382 undefined_reference (const char *reftype, const char *name)
8384 /* xgettext:c-format */
8385 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8390 eval_symbol (bfd_vma *result,
8393 struct elf_final_link_info *flinfo,
8395 Elf_Internal_Sym *isymbuf,
8404 const char *sym = *symp;
8406 bfd_boolean symbol_is_section = FALSE;
8411 if (len < 1 || len > sizeof (symbuf))
8413 bfd_set_error (bfd_error_invalid_operation);
8426 *result = strtoul (sym, (char **) symp, 16);
8430 symbol_is_section = TRUE;
8434 symlen = strtol (sym, (char **) symp, 10);
8435 sym = *symp + 1; /* Skip the trailing ':'. */
8437 if (symend < sym || symlen + 1 > sizeof (symbuf))
8439 bfd_set_error (bfd_error_invalid_operation);
8443 memcpy (symbuf, sym, symlen);
8444 symbuf[symlen] = '\0';
8445 *symp = sym + symlen;
8447 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8448 the symbol as a section, or vice-versa. so we're pretty liberal in our
8449 interpretation here; section means "try section first", not "must be a
8450 section", and likewise with symbol. */
8452 if (symbol_is_section)
8454 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8455 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8456 isymbuf, locsymcount))
8458 undefined_reference ("section", symbuf);
8464 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8465 isymbuf, locsymcount)
8466 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8469 undefined_reference ("symbol", symbuf);
8476 /* All that remains are operators. */
8478 #define UNARY_OP(op) \
8479 if (strncmp (sym, #op, strlen (#op)) == 0) \
8481 sym += strlen (#op); \
8485 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8486 isymbuf, locsymcount, signed_p)) \
8489 *result = op ((bfd_signed_vma) a); \
8495 #define BINARY_OP(op) \
8496 if (strncmp (sym, #op, strlen (#op)) == 0) \
8498 sym += strlen (#op); \
8502 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8503 isymbuf, locsymcount, signed_p)) \
8506 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8507 isymbuf, locsymcount, signed_p)) \
8510 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8540 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8541 bfd_set_error (bfd_error_invalid_operation);
8547 put_value (bfd_vma size,
8548 unsigned long chunksz,
8553 location += (size - chunksz);
8555 for (; size; size -= chunksz, location -= chunksz)
8560 bfd_put_8 (input_bfd, x, location);
8564 bfd_put_16 (input_bfd, x, location);
8568 bfd_put_32 (input_bfd, x, location);
8569 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8575 bfd_put_64 (input_bfd, x, location);
8576 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8589 get_value (bfd_vma size,
8590 unsigned long chunksz,
8597 /* Sanity checks. */
8598 BFD_ASSERT (chunksz <= sizeof (x)
8601 && (size % chunksz) == 0
8602 && input_bfd != NULL
8603 && location != NULL);
8605 if (chunksz == sizeof (x))
8607 BFD_ASSERT (size == chunksz);
8609 /* Make sure that we do not perform an undefined shift operation.
8610 We know that size == chunksz so there will only be one iteration
8611 of the loop below. */
8615 shift = 8 * chunksz;
8617 for (; size; size -= chunksz, location += chunksz)
8622 x = (x << shift) | bfd_get_8 (input_bfd, location);
8625 x = (x << shift) | bfd_get_16 (input_bfd, location);
8628 x = (x << shift) | bfd_get_32 (input_bfd, location);
8632 x = (x << shift) | bfd_get_64 (input_bfd, location);
8643 decode_complex_addend (unsigned long *start, /* in bits */
8644 unsigned long *oplen, /* in bits */
8645 unsigned long *len, /* in bits */
8646 unsigned long *wordsz, /* in bytes */
8647 unsigned long *chunksz, /* in bytes */
8648 unsigned long *lsb0_p,
8649 unsigned long *signed_p,
8650 unsigned long *trunc_p,
8651 unsigned long encoded)
8653 * start = encoded & 0x3F;
8654 * len = (encoded >> 6) & 0x3F;
8655 * oplen = (encoded >> 12) & 0x3F;
8656 * wordsz = (encoded >> 18) & 0xF;
8657 * chunksz = (encoded >> 22) & 0xF;
8658 * lsb0_p = (encoded >> 27) & 1;
8659 * signed_p = (encoded >> 28) & 1;
8660 * trunc_p = (encoded >> 29) & 1;
8663 bfd_reloc_status_type
8664 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8665 asection *input_section ATTRIBUTE_UNUSED,
8667 Elf_Internal_Rela *rel,
8670 bfd_vma shift, x, mask;
8671 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8672 bfd_reloc_status_type r;
8674 /* Perform this reloc, since it is complex.
8675 (this is not to say that it necessarily refers to a complex
8676 symbol; merely that it is a self-describing CGEN based reloc.
8677 i.e. the addend has the complete reloc information (bit start, end,
8678 word size, etc) encoded within it.). */
8680 decode_complex_addend (&start, &oplen, &len, &wordsz,
8681 &chunksz, &lsb0_p, &signed_p,
8682 &trunc_p, rel->r_addend);
8684 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8687 shift = (start + 1) - len;
8689 shift = (8 * wordsz) - (start + len);
8691 x = get_value (wordsz, chunksz, input_bfd,
8692 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8695 printf ("Doing complex reloc: "
8696 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8697 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8698 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8699 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8700 oplen, (unsigned long) x, (unsigned long) mask,
8701 (unsigned long) relocation);
8706 /* Now do an overflow check. */
8707 r = bfd_check_overflow ((signed_p
8708 ? complain_overflow_signed
8709 : complain_overflow_unsigned),
8710 len, 0, (8 * wordsz),
8714 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8717 printf (" relocation: %8.8lx\n"
8718 " shifted mask: %8.8lx\n"
8719 " shifted/masked reloc: %8.8lx\n"
8720 " result: %8.8lx\n",
8721 (unsigned long) relocation, (unsigned long) (mask << shift),
8722 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8724 put_value (wordsz, chunksz, input_bfd, x,
8725 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8729 /* Functions to read r_offset from external (target order) reloc
8730 entry. Faster than bfd_getl32 et al, because we let the compiler
8731 know the value is aligned. */
8734 ext32l_r_offset (const void *p)
8741 const union aligned32 *a
8742 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8744 uint32_t aval = ( (uint32_t) a->c[0]
8745 | (uint32_t) a->c[1] << 8
8746 | (uint32_t) a->c[2] << 16
8747 | (uint32_t) a->c[3] << 24);
8752 ext32b_r_offset (const void *p)
8759 const union aligned32 *a
8760 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8762 uint32_t aval = ( (uint32_t) a->c[0] << 24
8763 | (uint32_t) a->c[1] << 16
8764 | (uint32_t) a->c[2] << 8
8765 | (uint32_t) a->c[3]);
8769 #ifdef BFD_HOST_64_BIT
8771 ext64l_r_offset (const void *p)
8778 const union aligned64 *a
8779 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8781 uint64_t aval = ( (uint64_t) a->c[0]
8782 | (uint64_t) a->c[1] << 8
8783 | (uint64_t) a->c[2] << 16
8784 | (uint64_t) a->c[3] << 24
8785 | (uint64_t) a->c[4] << 32
8786 | (uint64_t) a->c[5] << 40
8787 | (uint64_t) a->c[6] << 48
8788 | (uint64_t) a->c[7] << 56);
8793 ext64b_r_offset (const void *p)
8800 const union aligned64 *a
8801 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8803 uint64_t aval = ( (uint64_t) a->c[0] << 56
8804 | (uint64_t) a->c[1] << 48
8805 | (uint64_t) a->c[2] << 40
8806 | (uint64_t) a->c[3] << 32
8807 | (uint64_t) a->c[4] << 24
8808 | (uint64_t) a->c[5] << 16
8809 | (uint64_t) a->c[6] << 8
8810 | (uint64_t) a->c[7]);
8815 /* When performing a relocatable link, the input relocations are
8816 preserved. But, if they reference global symbols, the indices
8817 referenced must be updated. Update all the relocations found in
8821 elf_link_adjust_relocs (bfd *abfd,
8823 struct bfd_elf_section_reloc_data *reldata,
8825 struct bfd_link_info *info)
8828 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8830 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8831 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8832 bfd_vma r_type_mask;
8834 unsigned int count = reldata->count;
8835 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8837 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8839 swap_in = bed->s->swap_reloc_in;
8840 swap_out = bed->s->swap_reloc_out;
8842 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8844 swap_in = bed->s->swap_reloca_in;
8845 swap_out = bed->s->swap_reloca_out;
8850 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8853 if (bed->s->arch_size == 32)
8860 r_type_mask = 0xffffffff;
8864 erela = reldata->hdr->contents;
8865 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8867 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8870 if (*rel_hash == NULL)
8873 if ((*rel_hash)->indx == -2
8874 && info->gc_sections
8875 && ! info->gc_keep_exported)
8877 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8878 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8880 (*rel_hash)->root.root.string);
8881 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8883 bfd_set_error (bfd_error_invalid_operation);
8886 BFD_ASSERT ((*rel_hash)->indx >= 0);
8888 (*swap_in) (abfd, erela, irela);
8889 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8890 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8891 | (irela[j].r_info & r_type_mask));
8892 (*swap_out) (abfd, irela, erela);
8895 if (bed->elf_backend_update_relocs)
8896 (*bed->elf_backend_update_relocs) (sec, reldata);
8898 if (sort && count != 0)
8900 bfd_vma (*ext_r_off) (const void *);
8903 bfd_byte *base, *end, *p, *loc;
8904 bfd_byte *buf = NULL;
8906 if (bed->s->arch_size == 32)
8908 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8909 ext_r_off = ext32l_r_offset;
8910 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8911 ext_r_off = ext32b_r_offset;
8917 #ifdef BFD_HOST_64_BIT
8918 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8919 ext_r_off = ext64l_r_offset;
8920 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8921 ext_r_off = ext64b_r_offset;
8927 /* Must use a stable sort here. A modified insertion sort,
8928 since the relocs are mostly sorted already. */
8929 elt_size = reldata->hdr->sh_entsize;
8930 base = reldata->hdr->contents;
8931 end = base + count * elt_size;
8932 if (elt_size > sizeof (Elf64_External_Rela))
8935 /* Ensure the first element is lowest. This acts as a sentinel,
8936 speeding the main loop below. */
8937 r_off = (*ext_r_off) (base);
8938 for (p = loc = base; (p += elt_size) < end; )
8940 bfd_vma r_off2 = (*ext_r_off) (p);
8949 /* Don't just swap *base and *loc as that changes the order
8950 of the original base[0] and base[1] if they happen to
8951 have the same r_offset. */
8952 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8953 memcpy (onebuf, loc, elt_size);
8954 memmove (base + elt_size, base, loc - base);
8955 memcpy (base, onebuf, elt_size);
8958 for (p = base + elt_size; (p += elt_size) < end; )
8960 /* base to p is sorted, *p is next to insert. */
8961 r_off = (*ext_r_off) (p);
8962 /* Search the sorted region for location to insert. */
8964 while (r_off < (*ext_r_off) (loc))
8969 /* Chances are there is a run of relocs to insert here,
8970 from one of more input files. Files are not always
8971 linked in order due to the way elf_link_input_bfd is
8972 called. See pr17666. */
8973 size_t sortlen = p - loc;
8974 bfd_vma r_off2 = (*ext_r_off) (loc);
8975 size_t runlen = elt_size;
8976 size_t buf_size = 96 * 1024;
8977 while (p + runlen < end
8978 && (sortlen <= buf_size
8979 || runlen + elt_size <= buf_size)
8980 && r_off2 > (*ext_r_off) (p + runlen))
8984 buf = bfd_malloc (buf_size);
8988 if (runlen < sortlen)
8990 memcpy (buf, p, runlen);
8991 memmove (loc + runlen, loc, sortlen);
8992 memcpy (loc, buf, runlen);
8996 memcpy (buf, loc, sortlen);
8997 memmove (loc, p, runlen);
8998 memcpy (loc + runlen, buf, sortlen);
9000 p += runlen - elt_size;
9003 /* Hashes are no longer valid. */
9004 free (reldata->hashes);
9005 reldata->hashes = NULL;
9011 struct elf_link_sort_rela
9017 enum elf_reloc_type_class type;
9018 /* We use this as an array of size int_rels_per_ext_rel. */
9019 Elf_Internal_Rela rela[1];
9023 elf_link_sort_cmp1 (const void *A, const void *B)
9025 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9026 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9027 int relativea, relativeb;
9029 relativea = a->type == reloc_class_relative;
9030 relativeb = b->type == reloc_class_relative;
9032 if (relativea < relativeb)
9034 if (relativea > relativeb)
9036 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9038 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9040 if (a->rela->r_offset < b->rela->r_offset)
9042 if (a->rela->r_offset > b->rela->r_offset)
9048 elf_link_sort_cmp2 (const void *A, const void *B)
9050 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9051 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9053 if (a->type < b->type)
9055 if (a->type > b->type)
9057 if (a->u.offset < b->u.offset)
9059 if (a->u.offset > b->u.offset)
9061 if (a->rela->r_offset < b->rela->r_offset)
9063 if (a->rela->r_offset > b->rela->r_offset)
9069 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9071 asection *dynamic_relocs;
9074 bfd_size_type count, size;
9075 size_t i, ret, sort_elt, ext_size;
9076 bfd_byte *sort, *s_non_relative, *p;
9077 struct elf_link_sort_rela *sq;
9078 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9079 int i2e = bed->s->int_rels_per_ext_rel;
9080 unsigned int opb = bfd_octets_per_byte (abfd);
9081 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9082 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9083 struct bfd_link_order *lo;
9085 bfd_boolean use_rela;
9087 /* Find a dynamic reloc section. */
9088 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9089 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9090 if (rela_dyn != NULL && rela_dyn->size > 0
9091 && rel_dyn != NULL && rel_dyn->size > 0)
9093 bfd_boolean use_rela_initialised = FALSE;
9095 /* This is just here to stop gcc from complaining.
9096 Its initialization checking code is not perfect. */
9099 /* Both sections are present. Examine the sizes
9100 of the indirect sections to help us choose. */
9101 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9102 if (lo->type == bfd_indirect_link_order)
9104 asection *o = lo->u.indirect.section;
9106 if ((o->size % bed->s->sizeof_rela) == 0)
9108 if ((o->size % bed->s->sizeof_rel) == 0)
9109 /* Section size is divisible by both rel and rela sizes.
9110 It is of no help to us. */
9114 /* Section size is only divisible by rela. */
9115 if (use_rela_initialised && !use_rela)
9117 _bfd_error_handler (_("%pB: unable to sort relocs - "
9118 "they are in more than one size"),
9120 bfd_set_error (bfd_error_invalid_operation);
9126 use_rela_initialised = TRUE;
9130 else if ((o->size % bed->s->sizeof_rel) == 0)
9132 /* Section size is only divisible by rel. */
9133 if (use_rela_initialised && use_rela)
9135 _bfd_error_handler (_("%pB: unable to sort relocs - "
9136 "they are in more than one size"),
9138 bfd_set_error (bfd_error_invalid_operation);
9144 use_rela_initialised = TRUE;
9149 /* The section size is not divisible by either -
9150 something is wrong. */
9151 _bfd_error_handler (_("%pB: unable to sort relocs - "
9152 "they are of an unknown size"), abfd);
9153 bfd_set_error (bfd_error_invalid_operation);
9158 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9159 if (lo->type == bfd_indirect_link_order)
9161 asection *o = lo->u.indirect.section;
9163 if ((o->size % bed->s->sizeof_rela) == 0)
9165 if ((o->size % bed->s->sizeof_rel) == 0)
9166 /* Section size is divisible by both rel and rela sizes.
9167 It is of no help to us. */
9171 /* Section size is only divisible by rela. */
9172 if (use_rela_initialised && !use_rela)
9174 _bfd_error_handler (_("%pB: unable to sort relocs - "
9175 "they are in more than one size"),
9177 bfd_set_error (bfd_error_invalid_operation);
9183 use_rela_initialised = TRUE;
9187 else if ((o->size % bed->s->sizeof_rel) == 0)
9189 /* Section size is only divisible by rel. */
9190 if (use_rela_initialised && use_rela)
9192 _bfd_error_handler (_("%pB: unable to sort relocs - "
9193 "they are in more than one size"),
9195 bfd_set_error (bfd_error_invalid_operation);
9201 use_rela_initialised = TRUE;
9206 /* The section size is not divisible by either -
9207 something is wrong. */
9208 _bfd_error_handler (_("%pB: unable to sort relocs - "
9209 "they are of an unknown size"), abfd);
9210 bfd_set_error (bfd_error_invalid_operation);
9215 if (! use_rela_initialised)
9219 else if (rela_dyn != NULL && rela_dyn->size > 0)
9221 else if (rel_dyn != NULL && rel_dyn->size > 0)
9228 dynamic_relocs = rela_dyn;
9229 ext_size = bed->s->sizeof_rela;
9230 swap_in = bed->s->swap_reloca_in;
9231 swap_out = bed->s->swap_reloca_out;
9235 dynamic_relocs = rel_dyn;
9236 ext_size = bed->s->sizeof_rel;
9237 swap_in = bed->s->swap_reloc_in;
9238 swap_out = bed->s->swap_reloc_out;
9242 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9243 if (lo->type == bfd_indirect_link_order)
9244 size += lo->u.indirect.section->size;
9246 if (size != dynamic_relocs->size)
9249 sort_elt = (sizeof (struct elf_link_sort_rela)
9250 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9252 count = dynamic_relocs->size / ext_size;
9255 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9259 (*info->callbacks->warning)
9260 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9264 if (bed->s->arch_size == 32)
9265 r_sym_mask = ~(bfd_vma) 0xff;
9267 r_sym_mask = ~(bfd_vma) 0xffffffff;
9269 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9270 if (lo->type == bfd_indirect_link_order)
9272 bfd_byte *erel, *erelend;
9273 asection *o = lo->u.indirect.section;
9275 if (o->contents == NULL && o->size != 0)
9277 /* This is a reloc section that is being handled as a normal
9278 section. See bfd_section_from_shdr. We can't combine
9279 relocs in this case. */
9284 erelend = o->contents + o->size;
9285 p = sort + o->output_offset * opb / ext_size * sort_elt;
9287 while (erel < erelend)
9289 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9291 (*swap_in) (abfd, erel, s->rela);
9292 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9293 s->u.sym_mask = r_sym_mask;
9299 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9301 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9303 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9304 if (s->type != reloc_class_relative)
9310 sq = (struct elf_link_sort_rela *) s_non_relative;
9311 for (; i < count; i++, p += sort_elt)
9313 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9314 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9316 sp->u.offset = sq->rela->r_offset;
9319 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9321 struct elf_link_hash_table *htab = elf_hash_table (info);
9322 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9324 /* We have plt relocs in .rela.dyn. */
9325 sq = (struct elf_link_sort_rela *) sort;
9326 for (i = 0; i < count; i++)
9327 if (sq[count - i - 1].type != reloc_class_plt)
9329 if (i != 0 && htab->srelplt->size == i * ext_size)
9331 struct bfd_link_order **plo;
9332 /* Put srelplt link_order last. This is so the output_offset
9333 set in the next loop is correct for DT_JMPREL. */
9334 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9335 if ((*plo)->type == bfd_indirect_link_order
9336 && (*plo)->u.indirect.section == htab->srelplt)
9342 plo = &(*plo)->next;
9345 dynamic_relocs->map_tail.link_order = lo;
9350 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9351 if (lo->type == bfd_indirect_link_order)
9353 bfd_byte *erel, *erelend;
9354 asection *o = lo->u.indirect.section;
9357 erelend = o->contents + o->size;
9358 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9359 while (erel < erelend)
9361 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9362 (*swap_out) (abfd, s->rela, erel);
9369 *psec = dynamic_relocs;
9373 /* Add a symbol to the output symbol string table. */
9376 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9378 Elf_Internal_Sym *elfsym,
9379 asection *input_sec,
9380 struct elf_link_hash_entry *h)
9382 int (*output_symbol_hook)
9383 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9384 struct elf_link_hash_entry *);
9385 struct elf_link_hash_table *hash_table;
9386 const struct elf_backend_data *bed;
9387 bfd_size_type strtabsize;
9389 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9391 bed = get_elf_backend_data (flinfo->output_bfd);
9392 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9393 if (output_symbol_hook != NULL)
9395 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9402 || (input_sec->flags & SEC_EXCLUDE))
9403 elfsym->st_name = (unsigned long) -1;
9406 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9407 to get the final offset for st_name. */
9409 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9411 if (elfsym->st_name == (unsigned long) -1)
9415 hash_table = elf_hash_table (flinfo->info);
9416 strtabsize = hash_table->strtabsize;
9417 if (strtabsize <= hash_table->strtabcount)
9419 strtabsize += strtabsize;
9420 hash_table->strtabsize = strtabsize;
9421 strtabsize *= sizeof (*hash_table->strtab);
9423 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9425 if (hash_table->strtab == NULL)
9428 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9429 hash_table->strtab[hash_table->strtabcount].dest_index
9430 = hash_table->strtabcount;
9431 hash_table->strtab[hash_table->strtabcount].destshndx_index
9432 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9434 bfd_get_symcount (flinfo->output_bfd) += 1;
9435 hash_table->strtabcount += 1;
9440 /* Swap symbols out to the symbol table and flush the output symbols to
9444 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9446 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9449 const struct elf_backend_data *bed;
9451 Elf_Internal_Shdr *hdr;
9455 if (!hash_table->strtabcount)
9458 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9460 bed = get_elf_backend_data (flinfo->output_bfd);
9462 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9463 symbuf = (bfd_byte *) bfd_malloc (amt);
9467 if (flinfo->symshndxbuf)
9469 amt = sizeof (Elf_External_Sym_Shndx);
9470 amt *= bfd_get_symcount (flinfo->output_bfd);
9471 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9472 if (flinfo->symshndxbuf == NULL)
9479 for (i = 0; i < hash_table->strtabcount; i++)
9481 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9482 if (elfsym->sym.st_name == (unsigned long) -1)
9483 elfsym->sym.st_name = 0;
9486 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9487 elfsym->sym.st_name);
9488 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9489 ((bfd_byte *) symbuf
9490 + (elfsym->dest_index
9491 * bed->s->sizeof_sym)),
9492 (flinfo->symshndxbuf
9493 + elfsym->destshndx_index));
9496 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9497 pos = hdr->sh_offset + hdr->sh_size;
9498 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9499 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9500 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9502 hdr->sh_size += amt;
9510 free (hash_table->strtab);
9511 hash_table->strtab = NULL;
9516 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9519 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9521 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9522 && sym->st_shndx < SHN_LORESERVE)
9524 /* The gABI doesn't support dynamic symbols in output sections
9527 /* xgettext:c-format */
9528 (_("%pB: too many sections: %d (>= %d)"),
9529 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9530 bfd_set_error (bfd_error_nonrepresentable_section);
9536 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9537 allowing an unsatisfied unversioned symbol in the DSO to match a
9538 versioned symbol that would normally require an explicit version.
9539 We also handle the case that a DSO references a hidden symbol
9540 which may be satisfied by a versioned symbol in another DSO. */
9543 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9544 const struct elf_backend_data *bed,
9545 struct elf_link_hash_entry *h)
9548 struct elf_link_loaded_list *loaded;
9550 if (!is_elf_hash_table (info->hash))
9553 /* Check indirect symbol. */
9554 while (h->root.type == bfd_link_hash_indirect)
9555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9557 switch (h->root.type)
9563 case bfd_link_hash_undefined:
9564 case bfd_link_hash_undefweak:
9565 abfd = h->root.u.undef.abfd;
9567 || (abfd->flags & DYNAMIC) == 0
9568 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9572 case bfd_link_hash_defined:
9573 case bfd_link_hash_defweak:
9574 abfd = h->root.u.def.section->owner;
9577 case bfd_link_hash_common:
9578 abfd = h->root.u.c.p->section->owner;
9581 BFD_ASSERT (abfd != NULL);
9583 for (loaded = elf_hash_table (info)->loaded;
9585 loaded = loaded->next)
9588 Elf_Internal_Shdr *hdr;
9592 Elf_Internal_Shdr *versymhdr;
9593 Elf_Internal_Sym *isym;
9594 Elf_Internal_Sym *isymend;
9595 Elf_Internal_Sym *isymbuf;
9596 Elf_External_Versym *ever;
9597 Elf_External_Versym *extversym;
9599 input = loaded->abfd;
9601 /* We check each DSO for a possible hidden versioned definition. */
9603 || (input->flags & DYNAMIC) == 0
9604 || elf_dynversym (input) == 0)
9607 hdr = &elf_tdata (input)->dynsymtab_hdr;
9609 symcount = hdr->sh_size / bed->s->sizeof_sym;
9610 if (elf_bad_symtab (input))
9612 extsymcount = symcount;
9617 extsymcount = symcount - hdr->sh_info;
9618 extsymoff = hdr->sh_info;
9621 if (extsymcount == 0)
9624 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9626 if (isymbuf == NULL)
9629 /* Read in any version definitions. */
9630 versymhdr = &elf_tdata (input)->dynversym_hdr;
9631 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9632 if (extversym == NULL)
9635 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9636 || (bfd_bread (extversym, versymhdr->sh_size, input)
9637 != versymhdr->sh_size))
9645 ever = extversym + extsymoff;
9646 isymend = isymbuf + extsymcount;
9647 for (isym = isymbuf; isym < isymend; isym++, ever++)
9650 Elf_Internal_Versym iver;
9651 unsigned short version_index;
9653 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9654 || isym->st_shndx == SHN_UNDEF)
9657 name = bfd_elf_string_from_elf_section (input,
9660 if (strcmp (name, h->root.root.string) != 0)
9663 _bfd_elf_swap_versym_in (input, ever, &iver);
9665 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9667 && h->forced_local))
9669 /* If we have a non-hidden versioned sym, then it should
9670 have provided a definition for the undefined sym unless
9671 it is defined in a non-shared object and forced local.
9676 version_index = iver.vs_vers & VERSYM_VERSION;
9677 if (version_index == 1 || version_index == 2)
9679 /* This is the base or first version. We can use it. */
9693 /* Convert ELF common symbol TYPE. */
9696 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9698 /* Commom symbol can only appear in relocatable link. */
9699 if (!bfd_link_relocatable (info))
9701 switch (info->elf_stt_common)
9705 case elf_stt_common:
9708 case no_elf_stt_common:
9715 /* Add an external symbol to the symbol table. This is called from
9716 the hash table traversal routine. When generating a shared object,
9717 we go through the symbol table twice. The first time we output
9718 anything that might have been forced to local scope in a version
9719 script. The second time we output the symbols that are still
9723 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9725 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9726 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9727 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9729 Elf_Internal_Sym sym;
9730 asection *input_sec;
9731 const struct elf_backend_data *bed;
9736 if (h->root.type == bfd_link_hash_warning)
9738 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9739 if (h->root.type == bfd_link_hash_new)
9743 /* Decide whether to output this symbol in this pass. */
9744 if (eoinfo->localsyms)
9746 if (!h->forced_local)
9751 if (h->forced_local)
9755 bed = get_elf_backend_data (flinfo->output_bfd);
9757 if (h->root.type == bfd_link_hash_undefined)
9759 /* If we have an undefined symbol reference here then it must have
9760 come from a shared library that is being linked in. (Undefined
9761 references in regular files have already been handled unless
9762 they are in unreferenced sections which are removed by garbage
9764 bfd_boolean ignore_undef = FALSE;
9766 /* Some symbols may be special in that the fact that they're
9767 undefined can be safely ignored - let backend determine that. */
9768 if (bed->elf_backend_ignore_undef_symbol)
9769 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9771 /* If we are reporting errors for this situation then do so now. */
9774 && (!h->ref_regular || flinfo->info->gc_sections)
9775 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9776 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9777 (*flinfo->info->callbacks->undefined_symbol)
9778 (flinfo->info, h->root.root.string,
9779 h->ref_regular ? NULL : h->root.u.undef.abfd,
9781 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9783 /* Strip a global symbol defined in a discarded section. */
9788 /* We should also warn if a forced local symbol is referenced from
9789 shared libraries. */
9790 if (bfd_link_executable (flinfo->info)
9795 && h->ref_dynamic_nonweak
9796 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9800 struct elf_link_hash_entry *hi = h;
9802 /* Check indirect symbol. */
9803 while (hi->root.type == bfd_link_hash_indirect)
9804 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9806 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9807 /* xgettext:c-format */
9808 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9809 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9810 /* xgettext:c-format */
9811 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9813 /* xgettext:c-format */
9814 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9815 def_bfd = flinfo->output_bfd;
9816 if (hi->root.u.def.section != bfd_abs_section_ptr)
9817 def_bfd = hi->root.u.def.section->owner;
9818 _bfd_error_handler (msg, flinfo->output_bfd,
9819 h->root.root.string, def_bfd);
9820 bfd_set_error (bfd_error_bad_value);
9821 eoinfo->failed = TRUE;
9825 /* We don't want to output symbols that have never been mentioned by
9826 a regular file, or that we have been told to strip. However, if
9827 h->indx is set to -2, the symbol is used by a reloc and we must
9832 else if ((h->def_dynamic
9834 || h->root.type == bfd_link_hash_new)
9838 else if (flinfo->info->strip == strip_all)
9840 else if (flinfo->info->strip == strip_some
9841 && bfd_hash_lookup (flinfo->info->keep_hash,
9842 h->root.root.string, FALSE, FALSE) == NULL)
9844 else if ((h->root.type == bfd_link_hash_defined
9845 || h->root.type == bfd_link_hash_defweak)
9846 && ((flinfo->info->strip_discarded
9847 && discarded_section (h->root.u.def.section))
9848 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9849 && h->root.u.def.section->owner != NULL
9850 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9852 else if ((h->root.type == bfd_link_hash_undefined
9853 || h->root.type == bfd_link_hash_undefweak)
9854 && h->root.u.undef.abfd != NULL
9855 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9860 /* If we're stripping it, and it's not a dynamic symbol, there's
9861 nothing else to do. However, if it is a forced local symbol or
9862 an ifunc symbol we need to give the backend finish_dynamic_symbol
9863 function a chance to make it dynamic. */
9866 && type != STT_GNU_IFUNC
9867 && !h->forced_local)
9871 sym.st_size = h->size;
9872 sym.st_other = h->other;
9873 switch (h->root.type)
9876 case bfd_link_hash_new:
9877 case bfd_link_hash_warning:
9881 case bfd_link_hash_undefined:
9882 case bfd_link_hash_undefweak:
9883 input_sec = bfd_und_section_ptr;
9884 sym.st_shndx = SHN_UNDEF;
9887 case bfd_link_hash_defined:
9888 case bfd_link_hash_defweak:
9890 input_sec = h->root.u.def.section;
9891 if (input_sec->output_section != NULL)
9894 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9895 input_sec->output_section);
9896 if (sym.st_shndx == SHN_BAD)
9899 /* xgettext:c-format */
9900 (_("%pB: could not find output section %pA for input section %pA"),
9901 flinfo->output_bfd, input_sec->output_section, input_sec);
9902 bfd_set_error (bfd_error_nonrepresentable_section);
9903 eoinfo->failed = TRUE;
9907 /* ELF symbols in relocatable files are section relative,
9908 but in nonrelocatable files they are virtual
9910 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9911 if (!bfd_link_relocatable (flinfo->info))
9913 sym.st_value += input_sec->output_section->vma;
9914 if (h->type == STT_TLS)
9916 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9917 if (tls_sec != NULL)
9918 sym.st_value -= tls_sec->vma;
9924 BFD_ASSERT (input_sec->owner == NULL
9925 || (input_sec->owner->flags & DYNAMIC) != 0);
9926 sym.st_shndx = SHN_UNDEF;
9927 input_sec = bfd_und_section_ptr;
9932 case bfd_link_hash_common:
9933 input_sec = h->root.u.c.p->section;
9934 sym.st_shndx = bed->common_section_index (input_sec);
9935 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9938 case bfd_link_hash_indirect:
9939 /* These symbols are created by symbol versioning. They point
9940 to the decorated version of the name. For example, if the
9941 symbol foo@@GNU_1.2 is the default, which should be used when
9942 foo is used with no version, then we add an indirect symbol
9943 foo which points to foo@@GNU_1.2. We ignore these symbols,
9944 since the indirected symbol is already in the hash table. */
9948 if (type == STT_COMMON || type == STT_OBJECT)
9949 switch (h->root.type)
9951 case bfd_link_hash_common:
9952 type = elf_link_convert_common_type (flinfo->info, type);
9954 case bfd_link_hash_defined:
9955 case bfd_link_hash_defweak:
9956 if (bed->common_definition (&sym))
9957 type = elf_link_convert_common_type (flinfo->info, type);
9961 case bfd_link_hash_undefined:
9962 case bfd_link_hash_undefweak:
9968 if (h->forced_local)
9970 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9971 /* Turn off visibility on local symbol. */
9972 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9974 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9975 else if (h->unique_global && h->def_regular)
9976 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9977 else if (h->root.type == bfd_link_hash_undefweak
9978 || h->root.type == bfd_link_hash_defweak)
9979 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9981 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9982 sym.st_target_internal = h->target_internal;
9984 /* Give the processor backend a chance to tweak the symbol value,
9985 and also to finish up anything that needs to be done for this
9986 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9987 forced local syms when non-shared is due to a historical quirk.
9988 STT_GNU_IFUNC symbol must go through PLT. */
9989 if ((h->type == STT_GNU_IFUNC
9991 && !bfd_link_relocatable (flinfo->info))
9992 || ((h->dynindx != -1
9994 && ((bfd_link_pic (flinfo->info)
9995 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9996 || h->root.type != bfd_link_hash_undefweak))
9997 || !h->forced_local)
9998 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10000 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10001 (flinfo->output_bfd, flinfo->info, h, &sym)))
10003 eoinfo->failed = TRUE;
10008 /* If we are marking the symbol as undefined, and there are no
10009 non-weak references to this symbol from a regular object, then
10010 mark the symbol as weak undefined; if there are non-weak
10011 references, mark the symbol as strong. We can't do this earlier,
10012 because it might not be marked as undefined until the
10013 finish_dynamic_symbol routine gets through with it. */
10014 if (sym.st_shndx == SHN_UNDEF
10016 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10017 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10020 type = ELF_ST_TYPE (sym.st_info);
10022 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10023 if (type == STT_GNU_IFUNC)
10026 if (h->ref_regular_nonweak)
10027 bindtype = STB_GLOBAL;
10029 bindtype = STB_WEAK;
10030 sym.st_info = ELF_ST_INFO (bindtype, type);
10033 /* If this is a symbol defined in a dynamic library, don't use the
10034 symbol size from the dynamic library. Relinking an executable
10035 against a new library may introduce gratuitous changes in the
10036 executable's symbols if we keep the size. */
10037 if (sym.st_shndx == SHN_UNDEF
10042 /* If a non-weak symbol with non-default visibility is not defined
10043 locally, it is a fatal error. */
10044 if (!bfd_link_relocatable (flinfo->info)
10045 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10046 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10047 && h->root.type == bfd_link_hash_undefined
10048 && !h->def_regular)
10052 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10053 /* xgettext:c-format */
10054 msg = _("%pB: protected symbol `%s' isn't defined");
10055 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10056 /* xgettext:c-format */
10057 msg = _("%pB: internal symbol `%s' isn't defined");
10059 /* xgettext:c-format */
10060 msg = _("%pB: hidden symbol `%s' isn't defined");
10061 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10062 bfd_set_error (bfd_error_bad_value);
10063 eoinfo->failed = TRUE;
10067 /* If this symbol should be put in the .dynsym section, then put it
10068 there now. We already know the symbol index. We also fill in
10069 the entry in the .hash section. */
10070 if (h->dynindx != -1
10071 && elf_hash_table (flinfo->info)->dynamic_sections_created
10072 && elf_hash_table (flinfo->info)->dynsym != NULL
10073 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10077 /* Since there is no version information in the dynamic string,
10078 if there is no version info in symbol version section, we will
10079 have a run-time problem if not linking executable, referenced
10080 by shared library, or not bound locally. */
10081 if (h->verinfo.verdef == NULL
10082 && (!bfd_link_executable (flinfo->info)
10084 || !h->def_regular))
10086 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10088 if (p && p [1] != '\0')
10091 /* xgettext:c-format */
10092 (_("%pB: no symbol version section for versioned symbol `%s'"),
10093 flinfo->output_bfd, h->root.root.string);
10094 eoinfo->failed = TRUE;
10099 sym.st_name = h->dynstr_index;
10100 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10101 + h->dynindx * bed->s->sizeof_sym);
10102 if (!check_dynsym (flinfo->output_bfd, &sym))
10104 eoinfo->failed = TRUE;
10107 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10109 if (flinfo->hash_sec != NULL)
10111 size_t hash_entry_size;
10112 bfd_byte *bucketpos;
10114 size_t bucketcount;
10117 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10118 bucket = h->u.elf_hash_value % bucketcount;
10121 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10122 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10123 + (bucket + 2) * hash_entry_size);
10124 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10125 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10127 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10128 ((bfd_byte *) flinfo->hash_sec->contents
10129 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10132 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10134 Elf_Internal_Versym iversym;
10135 Elf_External_Versym *eversym;
10137 if (!h->def_regular)
10139 if (h->verinfo.verdef == NULL
10140 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10141 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10142 iversym.vs_vers = 0;
10144 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10148 if (h->verinfo.vertree == NULL)
10149 iversym.vs_vers = 1;
10151 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10152 if (flinfo->info->create_default_symver)
10156 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10157 defined locally. */
10158 if (h->versioned == versioned_hidden && h->def_regular)
10159 iversym.vs_vers |= VERSYM_HIDDEN;
10161 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10162 eversym += h->dynindx;
10163 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10167 /* If the symbol is undefined, and we didn't output it to .dynsym,
10168 strip it from .symtab too. Obviously we can't do this for
10169 relocatable output or when needed for --emit-relocs. */
10170 else if (input_sec == bfd_und_section_ptr
10172 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10173 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10174 && !bfd_link_relocatable (flinfo->info))
10177 /* Also strip others that we couldn't earlier due to dynamic symbol
10181 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10184 /* Output a FILE symbol so that following locals are not associated
10185 with the wrong input file. We need one for forced local symbols
10186 if we've seen more than one FILE symbol or when we have exactly
10187 one FILE symbol but global symbols are present in a file other
10188 than the one with the FILE symbol. We also need one if linker
10189 defined symbols are present. In practice these conditions are
10190 always met, so just emit the FILE symbol unconditionally. */
10191 if (eoinfo->localsyms
10192 && !eoinfo->file_sym_done
10193 && eoinfo->flinfo->filesym_count != 0)
10195 Elf_Internal_Sym fsym;
10197 memset (&fsym, 0, sizeof (fsym));
10198 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10199 fsym.st_shndx = SHN_ABS;
10200 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10201 bfd_und_section_ptr, NULL))
10204 eoinfo->file_sym_done = TRUE;
10207 indx = bfd_get_symcount (flinfo->output_bfd);
10208 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10212 eoinfo->failed = TRUE;
10217 else if (h->indx == -2)
10223 /* Return TRUE if special handling is done for relocs in SEC against
10224 symbols defined in discarded sections. */
10227 elf_section_ignore_discarded_relocs (asection *sec)
10229 const struct elf_backend_data *bed;
10231 switch (sec->sec_info_type)
10233 case SEC_INFO_TYPE_STABS:
10234 case SEC_INFO_TYPE_EH_FRAME:
10235 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10241 bed = get_elf_backend_data (sec->owner);
10242 if (bed->elf_backend_ignore_discarded_relocs != NULL
10243 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10249 /* Return a mask saying how ld should treat relocations in SEC against
10250 symbols defined in discarded sections. If this function returns
10251 COMPLAIN set, ld will issue a warning message. If this function
10252 returns PRETEND set, and the discarded section was link-once and the
10253 same size as the kept link-once section, ld will pretend that the
10254 symbol was actually defined in the kept section. Otherwise ld will
10255 zero the reloc (at least that is the intent, but some cooperation by
10256 the target dependent code is needed, particularly for REL targets). */
10259 _bfd_elf_default_action_discarded (asection *sec)
10261 if (sec->flags & SEC_DEBUGGING)
10264 if (strcmp (".eh_frame", sec->name) == 0)
10267 if (strcmp (".gcc_except_table", sec->name) == 0)
10270 return COMPLAIN | PRETEND;
10273 /* Find a match between a section and a member of a section group. */
10276 match_group_member (asection *sec, asection *group,
10277 struct bfd_link_info *info)
10279 asection *first = elf_next_in_group (group);
10280 asection *s = first;
10284 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10287 s = elf_next_in_group (s);
10295 /* Check if the kept section of a discarded section SEC can be used
10296 to replace it. Return the replacement if it is OK. Otherwise return
10300 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10304 kept = sec->kept_section;
10307 if ((kept->flags & SEC_GROUP) != 0)
10308 kept = match_group_member (sec, kept, info);
10310 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10311 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10313 sec->kept_section = kept;
10318 /* Link an input file into the linker output file. This function
10319 handles all the sections and relocations of the input file at once.
10320 This is so that we only have to read the local symbols once, and
10321 don't have to keep them in memory. */
10324 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10326 int (*relocate_section)
10327 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10328 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10330 Elf_Internal_Shdr *symtab_hdr;
10331 size_t locsymcount;
10333 Elf_Internal_Sym *isymbuf;
10334 Elf_Internal_Sym *isym;
10335 Elf_Internal_Sym *isymend;
10337 asection **ppsection;
10339 const struct elf_backend_data *bed;
10340 struct elf_link_hash_entry **sym_hashes;
10341 bfd_size_type address_size;
10342 bfd_vma r_type_mask;
10344 bfd_boolean have_file_sym = FALSE;
10346 output_bfd = flinfo->output_bfd;
10347 bed = get_elf_backend_data (output_bfd);
10348 relocate_section = bed->elf_backend_relocate_section;
10350 /* If this is a dynamic object, we don't want to do anything here:
10351 we don't want the local symbols, and we don't want the section
10353 if ((input_bfd->flags & DYNAMIC) != 0)
10356 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10357 if (elf_bad_symtab (input_bfd))
10359 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10364 locsymcount = symtab_hdr->sh_info;
10365 extsymoff = symtab_hdr->sh_info;
10368 /* Read the local symbols. */
10369 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10370 if (isymbuf == NULL && locsymcount != 0)
10372 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10373 flinfo->internal_syms,
10374 flinfo->external_syms,
10375 flinfo->locsym_shndx);
10376 if (isymbuf == NULL)
10380 /* Find local symbol sections and adjust values of symbols in
10381 SEC_MERGE sections. Write out those local symbols we know are
10382 going into the output file. */
10383 isymend = isymbuf + locsymcount;
10384 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10386 isym++, pindex++, ppsection++)
10390 Elf_Internal_Sym osym;
10396 if (elf_bad_symtab (input_bfd))
10398 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10405 if (isym->st_shndx == SHN_UNDEF)
10406 isec = bfd_und_section_ptr;
10407 else if (isym->st_shndx == SHN_ABS)
10408 isec = bfd_abs_section_ptr;
10409 else if (isym->st_shndx == SHN_COMMON)
10410 isec = bfd_com_section_ptr;
10413 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10416 /* Don't attempt to output symbols with st_shnx in the
10417 reserved range other than SHN_ABS and SHN_COMMON. */
10421 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10422 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10424 _bfd_merged_section_offset (output_bfd, &isec,
10425 elf_section_data (isec)->sec_info,
10431 /* Don't output the first, undefined, symbol. In fact, don't
10432 output any undefined local symbol. */
10433 if (isec == bfd_und_section_ptr)
10436 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10438 /* We never output section symbols. Instead, we use the
10439 section symbol of the corresponding section in the output
10444 /* If we are stripping all symbols, we don't want to output this
10446 if (flinfo->info->strip == strip_all)
10449 /* If we are discarding all local symbols, we don't want to
10450 output this one. If we are generating a relocatable output
10451 file, then some of the local symbols may be required by
10452 relocs; we output them below as we discover that they are
10454 if (flinfo->info->discard == discard_all)
10457 /* If this symbol is defined in a section which we are
10458 discarding, we don't need to keep it. */
10459 if (isym->st_shndx != SHN_UNDEF
10460 && isym->st_shndx < SHN_LORESERVE
10461 && bfd_section_removed_from_list (output_bfd,
10462 isec->output_section))
10465 /* Get the name of the symbol. */
10466 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10471 /* See if we are discarding symbols with this name. */
10472 if ((flinfo->info->strip == strip_some
10473 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10475 || (((flinfo->info->discard == discard_sec_merge
10476 && (isec->flags & SEC_MERGE)
10477 && !bfd_link_relocatable (flinfo->info))
10478 || flinfo->info->discard == discard_l)
10479 && bfd_is_local_label_name (input_bfd, name)))
10482 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10484 if (input_bfd->lto_output)
10485 /* -flto puts a temp file name here. This means builds
10486 are not reproducible. Discard the symbol. */
10488 have_file_sym = TRUE;
10489 flinfo->filesym_count += 1;
10491 if (!have_file_sym)
10493 /* In the absence of debug info, bfd_find_nearest_line uses
10494 FILE symbols to determine the source file for local
10495 function symbols. Provide a FILE symbol here if input
10496 files lack such, so that their symbols won't be
10497 associated with a previous input file. It's not the
10498 source file, but the best we can do. */
10499 have_file_sym = TRUE;
10500 flinfo->filesym_count += 1;
10501 memset (&osym, 0, sizeof (osym));
10502 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10503 osym.st_shndx = SHN_ABS;
10504 if (!elf_link_output_symstrtab (flinfo,
10505 (input_bfd->lto_output ? NULL
10506 : input_bfd->filename),
10507 &osym, bfd_abs_section_ptr,
10514 /* Adjust the section index for the output file. */
10515 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10516 isec->output_section);
10517 if (osym.st_shndx == SHN_BAD)
10520 /* ELF symbols in relocatable files are section relative, but
10521 in executable files they are virtual addresses. Note that
10522 this code assumes that all ELF sections have an associated
10523 BFD section with a reasonable value for output_offset; below
10524 we assume that they also have a reasonable value for
10525 output_section. Any special sections must be set up to meet
10526 these requirements. */
10527 osym.st_value += isec->output_offset;
10528 if (!bfd_link_relocatable (flinfo->info))
10530 osym.st_value += isec->output_section->vma;
10531 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10533 /* STT_TLS symbols are relative to PT_TLS segment base. */
10534 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10535 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10537 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10542 indx = bfd_get_symcount (output_bfd);
10543 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10550 if (bed->s->arch_size == 32)
10552 r_type_mask = 0xff;
10558 r_type_mask = 0xffffffff;
10563 /* Relocate the contents of each section. */
10564 sym_hashes = elf_sym_hashes (input_bfd);
10565 for (o = input_bfd->sections; o != NULL; o = o->next)
10567 bfd_byte *contents;
10569 if (! o->linker_mark)
10571 /* This section was omitted from the link. */
10575 if (!flinfo->info->resolve_section_groups
10576 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10578 /* Deal with the group signature symbol. */
10579 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10580 unsigned long symndx = sec_data->this_hdr.sh_info;
10581 asection *osec = o->output_section;
10583 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10584 if (symndx >= locsymcount
10585 || (elf_bad_symtab (input_bfd)
10586 && flinfo->sections[symndx] == NULL))
10588 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10589 while (h->root.type == bfd_link_hash_indirect
10590 || h->root.type == bfd_link_hash_warning)
10591 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10592 /* Arrange for symbol to be output. */
10594 elf_section_data (osec)->this_hdr.sh_info = -2;
10596 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10598 /* We'll use the output section target_index. */
10599 asection *sec = flinfo->sections[symndx]->output_section;
10600 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10604 if (flinfo->indices[symndx] == -1)
10606 /* Otherwise output the local symbol now. */
10607 Elf_Internal_Sym sym = isymbuf[symndx];
10608 asection *sec = flinfo->sections[symndx]->output_section;
10613 name = bfd_elf_string_from_elf_section (input_bfd,
10614 symtab_hdr->sh_link,
10619 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10621 if (sym.st_shndx == SHN_BAD)
10624 sym.st_value += o->output_offset;
10626 indx = bfd_get_symcount (output_bfd);
10627 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10632 flinfo->indices[symndx] = indx;
10636 elf_section_data (osec)->this_hdr.sh_info
10637 = flinfo->indices[symndx];
10641 if ((o->flags & SEC_HAS_CONTENTS) == 0
10642 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10645 if ((o->flags & SEC_LINKER_CREATED) != 0)
10647 /* Section was created by _bfd_elf_link_create_dynamic_sections
10652 /* Get the contents of the section. They have been cached by a
10653 relaxation routine. Note that o is a section in an input
10654 file, so the contents field will not have been set by any of
10655 the routines which work on output files. */
10656 if (elf_section_data (o)->this_hdr.contents != NULL)
10658 contents = elf_section_data (o)->this_hdr.contents;
10659 if (bed->caches_rawsize
10661 && o->rawsize < o->size)
10663 memcpy (flinfo->contents, contents, o->rawsize);
10664 contents = flinfo->contents;
10669 contents = flinfo->contents;
10670 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10674 if ((o->flags & SEC_RELOC) != 0)
10676 Elf_Internal_Rela *internal_relocs;
10677 Elf_Internal_Rela *rel, *relend;
10678 int action_discarded;
10681 /* Get the swapped relocs. */
10683 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10684 flinfo->internal_relocs, FALSE);
10685 if (internal_relocs == NULL
10686 && o->reloc_count > 0)
10689 /* We need to reverse-copy input .ctors/.dtors sections if
10690 they are placed in .init_array/.finit_array for output. */
10691 if (o->size > address_size
10692 && ((strncmp (o->name, ".ctors", 6) == 0
10693 && strcmp (o->output_section->name,
10694 ".init_array") == 0)
10695 || (strncmp (o->name, ".dtors", 6) == 0
10696 && strcmp (o->output_section->name,
10697 ".fini_array") == 0))
10698 && (o->name[6] == 0 || o->name[6] == '.'))
10700 if (o->size * bed->s->int_rels_per_ext_rel
10701 != o->reloc_count * address_size)
10704 /* xgettext:c-format */
10705 (_("error: %pB: size of section %pA is not "
10706 "multiple of address size"),
10708 bfd_set_error (bfd_error_bad_value);
10711 o->flags |= SEC_ELF_REVERSE_COPY;
10714 action_discarded = -1;
10715 if (!elf_section_ignore_discarded_relocs (o))
10716 action_discarded = (*bed->action_discarded) (o);
10718 /* Run through the relocs evaluating complex reloc symbols and
10719 looking for relocs against symbols from discarded sections
10720 or section symbols from removed link-once sections.
10721 Complain about relocs against discarded sections. Zero
10722 relocs against removed link-once sections. */
10724 rel = internal_relocs;
10725 relend = rel + o->reloc_count;
10726 for ( ; rel < relend; rel++)
10728 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10729 unsigned int s_type;
10730 asection **ps, *sec;
10731 struct elf_link_hash_entry *h = NULL;
10732 const char *sym_name;
10734 if (r_symndx == STN_UNDEF)
10737 if (r_symndx >= locsymcount
10738 || (elf_bad_symtab (input_bfd)
10739 && flinfo->sections[r_symndx] == NULL))
10741 h = sym_hashes[r_symndx - extsymoff];
10743 /* Badly formatted input files can contain relocs that
10744 reference non-existant symbols. Check here so that
10745 we do not seg fault. */
10749 /* xgettext:c-format */
10750 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10751 "that references a non-existent global symbol"),
10752 input_bfd, (uint64_t) rel->r_info, o);
10753 bfd_set_error (bfd_error_bad_value);
10757 while (h->root.type == bfd_link_hash_indirect
10758 || h->root.type == bfd_link_hash_warning)
10759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10763 /* If a plugin symbol is referenced from a non-IR file,
10764 mark the symbol as undefined. Note that the
10765 linker may attach linker created dynamic sections
10766 to the plugin bfd. Symbols defined in linker
10767 created sections are not plugin symbols. */
10768 if ((h->root.non_ir_ref_regular
10769 || h->root.non_ir_ref_dynamic)
10770 && (h->root.type == bfd_link_hash_defined
10771 || h->root.type == bfd_link_hash_defweak)
10772 && (h->root.u.def.section->flags
10773 & SEC_LINKER_CREATED) == 0
10774 && h->root.u.def.section->owner != NULL
10775 && (h->root.u.def.section->owner->flags
10776 & BFD_PLUGIN) != 0)
10778 h->root.type = bfd_link_hash_undefined;
10779 h->root.u.undef.abfd = h->root.u.def.section->owner;
10783 if (h->root.type == bfd_link_hash_defined
10784 || h->root.type == bfd_link_hash_defweak)
10785 ps = &h->root.u.def.section;
10787 sym_name = h->root.root.string;
10791 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10793 s_type = ELF_ST_TYPE (sym->st_info);
10794 ps = &flinfo->sections[r_symndx];
10795 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10799 if ((s_type == STT_RELC || s_type == STT_SRELC)
10800 && !bfd_link_relocatable (flinfo->info))
10803 bfd_vma dot = (rel->r_offset
10804 + o->output_offset + o->output_section->vma);
10806 printf ("Encountered a complex symbol!");
10807 printf (" (input_bfd %s, section %s, reloc %ld\n",
10808 input_bfd->filename, o->name,
10809 (long) (rel - internal_relocs));
10810 printf (" symbol: idx %8.8lx, name %s\n",
10811 r_symndx, sym_name);
10812 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10813 (unsigned long) rel->r_info,
10814 (unsigned long) rel->r_offset);
10816 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10817 isymbuf, locsymcount, s_type == STT_SRELC))
10820 /* Symbol evaluated OK. Update to absolute value. */
10821 set_symbol_value (input_bfd, isymbuf, locsymcount,
10826 if (action_discarded != -1 && ps != NULL)
10828 /* Complain if the definition comes from a
10829 discarded section. */
10830 if ((sec = *ps) != NULL && discarded_section (sec))
10832 BFD_ASSERT (r_symndx != STN_UNDEF);
10833 if (action_discarded & COMPLAIN)
10834 (*flinfo->info->callbacks->einfo)
10835 /* xgettext:c-format */
10836 (_("%X`%s' referenced in section `%pA' of %pB: "
10837 "defined in discarded section `%pA' of %pB\n"),
10838 sym_name, o, input_bfd, sec, sec->owner);
10840 /* Try to do the best we can to support buggy old
10841 versions of gcc. Pretend that the symbol is
10842 really defined in the kept linkonce section.
10843 FIXME: This is quite broken. Modifying the
10844 symbol here means we will be changing all later
10845 uses of the symbol, not just in this section. */
10846 if (action_discarded & PRETEND)
10850 kept = _bfd_elf_check_kept_section (sec,
10862 /* Relocate the section by invoking a back end routine.
10864 The back end routine is responsible for adjusting the
10865 section contents as necessary, and (if using Rela relocs
10866 and generating a relocatable output file) adjusting the
10867 reloc addend as necessary.
10869 The back end routine does not have to worry about setting
10870 the reloc address or the reloc symbol index.
10872 The back end routine is given a pointer to the swapped in
10873 internal symbols, and can access the hash table entries
10874 for the external symbols via elf_sym_hashes (input_bfd).
10876 When generating relocatable output, the back end routine
10877 must handle STB_LOCAL/STT_SECTION symbols specially. The
10878 output symbol is going to be a section symbol
10879 corresponding to the output section, which will require
10880 the addend to be adjusted. */
10882 ret = (*relocate_section) (output_bfd, flinfo->info,
10883 input_bfd, o, contents,
10891 || bfd_link_relocatable (flinfo->info)
10892 || flinfo->info->emitrelocations)
10894 Elf_Internal_Rela *irela;
10895 Elf_Internal_Rela *irelaend, *irelamid;
10896 bfd_vma last_offset;
10897 struct elf_link_hash_entry **rel_hash;
10898 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10899 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10900 unsigned int next_erel;
10901 bfd_boolean rela_normal;
10902 struct bfd_elf_section_data *esdi, *esdo;
10904 esdi = elf_section_data (o);
10905 esdo = elf_section_data (o->output_section);
10906 rela_normal = FALSE;
10908 /* Adjust the reloc addresses and symbol indices. */
10910 irela = internal_relocs;
10911 irelaend = irela + o->reloc_count;
10912 rel_hash = esdo->rel.hashes + esdo->rel.count;
10913 /* We start processing the REL relocs, if any. When we reach
10914 IRELAMID in the loop, we switch to the RELA relocs. */
10916 if (esdi->rel.hdr != NULL)
10917 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10918 * bed->s->int_rels_per_ext_rel);
10919 rel_hash_list = rel_hash;
10920 rela_hash_list = NULL;
10921 last_offset = o->output_offset;
10922 if (!bfd_link_relocatable (flinfo->info))
10923 last_offset += o->output_section->vma;
10924 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10926 unsigned long r_symndx;
10928 Elf_Internal_Sym sym;
10930 if (next_erel == bed->s->int_rels_per_ext_rel)
10936 if (irela == irelamid)
10938 rel_hash = esdo->rela.hashes + esdo->rela.count;
10939 rela_hash_list = rel_hash;
10940 rela_normal = bed->rela_normal;
10943 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10946 if (irela->r_offset >= (bfd_vma) -2)
10948 /* This is a reloc for a deleted entry or somesuch.
10949 Turn it into an R_*_NONE reloc, at the same
10950 offset as the last reloc. elf_eh_frame.c and
10951 bfd_elf_discard_info rely on reloc offsets
10953 irela->r_offset = last_offset;
10955 irela->r_addend = 0;
10959 irela->r_offset += o->output_offset;
10961 /* Relocs in an executable have to be virtual addresses. */
10962 if (!bfd_link_relocatable (flinfo->info))
10963 irela->r_offset += o->output_section->vma;
10965 last_offset = irela->r_offset;
10967 r_symndx = irela->r_info >> r_sym_shift;
10968 if (r_symndx == STN_UNDEF)
10971 if (r_symndx >= locsymcount
10972 || (elf_bad_symtab (input_bfd)
10973 && flinfo->sections[r_symndx] == NULL))
10975 struct elf_link_hash_entry *rh;
10976 unsigned long indx;
10978 /* This is a reloc against a global symbol. We
10979 have not yet output all the local symbols, so
10980 we do not know the symbol index of any global
10981 symbol. We set the rel_hash entry for this
10982 reloc to point to the global hash table entry
10983 for this symbol. The symbol index is then
10984 set at the end of bfd_elf_final_link. */
10985 indx = r_symndx - extsymoff;
10986 rh = elf_sym_hashes (input_bfd)[indx];
10987 while (rh->root.type == bfd_link_hash_indirect
10988 || rh->root.type == bfd_link_hash_warning)
10989 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10991 /* Setting the index to -2 tells
10992 elf_link_output_extsym that this symbol is
10993 used by a reloc. */
10994 BFD_ASSERT (rh->indx < 0);
11001 /* This is a reloc against a local symbol. */
11004 sym = isymbuf[r_symndx];
11005 sec = flinfo->sections[r_symndx];
11006 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11008 /* I suppose the backend ought to fill in the
11009 section of any STT_SECTION symbol against a
11010 processor specific section. */
11011 r_symndx = STN_UNDEF;
11012 if (bfd_is_abs_section (sec))
11014 else if (sec == NULL || sec->owner == NULL)
11016 bfd_set_error (bfd_error_bad_value);
11021 asection *osec = sec->output_section;
11023 /* If we have discarded a section, the output
11024 section will be the absolute section. In
11025 case of discarded SEC_MERGE sections, use
11026 the kept section. relocate_section should
11027 have already handled discarded linkonce
11029 if (bfd_is_abs_section (osec)
11030 && sec->kept_section != NULL
11031 && sec->kept_section->output_section != NULL)
11033 osec = sec->kept_section->output_section;
11034 irela->r_addend -= osec->vma;
11037 if (!bfd_is_abs_section (osec))
11039 r_symndx = osec->target_index;
11040 if (r_symndx == STN_UNDEF)
11042 irela->r_addend += osec->vma;
11043 osec = _bfd_nearby_section (output_bfd, osec,
11045 irela->r_addend -= osec->vma;
11046 r_symndx = osec->target_index;
11051 /* Adjust the addend according to where the
11052 section winds up in the output section. */
11054 irela->r_addend += sec->output_offset;
11058 if (flinfo->indices[r_symndx] == -1)
11060 unsigned long shlink;
11065 if (flinfo->info->strip == strip_all)
11067 /* You can't do ld -r -s. */
11068 bfd_set_error (bfd_error_invalid_operation);
11072 /* This symbol was skipped earlier, but
11073 since it is needed by a reloc, we
11074 must output it now. */
11075 shlink = symtab_hdr->sh_link;
11076 name = (bfd_elf_string_from_elf_section
11077 (input_bfd, shlink, sym.st_name));
11081 osec = sec->output_section;
11083 _bfd_elf_section_from_bfd_section (output_bfd,
11085 if (sym.st_shndx == SHN_BAD)
11088 sym.st_value += sec->output_offset;
11089 if (!bfd_link_relocatable (flinfo->info))
11091 sym.st_value += osec->vma;
11092 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11094 struct elf_link_hash_table *htab
11095 = elf_hash_table (flinfo->info);
11097 /* STT_TLS symbols are relative to PT_TLS
11099 if (htab->tls_sec != NULL)
11100 sym.st_value -= htab->tls_sec->vma;
11103 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11108 indx = bfd_get_symcount (output_bfd);
11109 ret = elf_link_output_symstrtab (flinfo, name,
11115 flinfo->indices[r_symndx] = indx;
11120 r_symndx = flinfo->indices[r_symndx];
11123 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11124 | (irela->r_info & r_type_mask));
11127 /* Swap out the relocs. */
11128 input_rel_hdr = esdi->rel.hdr;
11129 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11131 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11136 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11137 * bed->s->int_rels_per_ext_rel);
11138 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11141 input_rela_hdr = esdi->rela.hdr;
11142 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11144 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11153 /* Write out the modified section contents. */
11154 if (bed->elf_backend_write_section
11155 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11158 /* Section written out. */
11160 else switch (o->sec_info_type)
11162 case SEC_INFO_TYPE_STABS:
11163 if (! (_bfd_write_section_stabs
11165 &elf_hash_table (flinfo->info)->stab_info,
11166 o, &elf_section_data (o)->sec_info, contents)))
11169 case SEC_INFO_TYPE_MERGE:
11170 if (! _bfd_write_merged_section (output_bfd, o,
11171 elf_section_data (o)->sec_info))
11174 case SEC_INFO_TYPE_EH_FRAME:
11176 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11181 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11183 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11191 if (! (o->flags & SEC_EXCLUDE))
11193 file_ptr offset = (file_ptr) o->output_offset;
11194 bfd_size_type todo = o->size;
11196 offset *= bfd_octets_per_byte (output_bfd);
11198 if ((o->flags & SEC_ELF_REVERSE_COPY))
11200 /* Reverse-copy input section to output. */
11203 todo -= address_size;
11204 if (! bfd_set_section_contents (output_bfd,
11212 offset += address_size;
11216 else if (! bfd_set_section_contents (output_bfd,
11230 /* Generate a reloc when linking an ELF file. This is a reloc
11231 requested by the linker, and does not come from any input file. This
11232 is used to build constructor and destructor tables when linking
11236 elf_reloc_link_order (bfd *output_bfd,
11237 struct bfd_link_info *info,
11238 asection *output_section,
11239 struct bfd_link_order *link_order)
11241 reloc_howto_type *howto;
11245 struct bfd_elf_section_reloc_data *reldata;
11246 struct elf_link_hash_entry **rel_hash_ptr;
11247 Elf_Internal_Shdr *rel_hdr;
11248 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11249 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11252 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11254 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11257 bfd_set_error (bfd_error_bad_value);
11261 addend = link_order->u.reloc.p->addend;
11264 reldata = &esdo->rel;
11265 else if (esdo->rela.hdr)
11266 reldata = &esdo->rela;
11273 /* Figure out the symbol index. */
11274 rel_hash_ptr = reldata->hashes + reldata->count;
11275 if (link_order->type == bfd_section_reloc_link_order)
11277 indx = link_order->u.reloc.p->u.section->target_index;
11278 BFD_ASSERT (indx != 0);
11279 *rel_hash_ptr = NULL;
11283 struct elf_link_hash_entry *h;
11285 /* Treat a reloc against a defined symbol as though it were
11286 actually against the section. */
11287 h = ((struct elf_link_hash_entry *)
11288 bfd_wrapped_link_hash_lookup (output_bfd, info,
11289 link_order->u.reloc.p->u.name,
11290 FALSE, FALSE, TRUE));
11292 && (h->root.type == bfd_link_hash_defined
11293 || h->root.type == bfd_link_hash_defweak))
11297 section = h->root.u.def.section;
11298 indx = section->output_section->target_index;
11299 *rel_hash_ptr = NULL;
11300 /* It seems that we ought to add the symbol value to the
11301 addend here, but in practice it has already been added
11302 because it was passed to constructor_callback. */
11303 addend += section->output_section->vma + section->output_offset;
11305 else if (h != NULL)
11307 /* Setting the index to -2 tells elf_link_output_extsym that
11308 this symbol is used by a reloc. */
11315 (*info->callbacks->unattached_reloc)
11316 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11321 /* If this is an inplace reloc, we must write the addend into the
11323 if (howto->partial_inplace && addend != 0)
11325 bfd_size_type size;
11326 bfd_reloc_status_type rstat;
11329 const char *sym_name;
11331 size = (bfd_size_type) bfd_get_reloc_size (howto);
11332 buf = (bfd_byte *) bfd_zmalloc (size);
11333 if (buf == NULL && size != 0)
11335 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11342 case bfd_reloc_outofrange:
11345 case bfd_reloc_overflow:
11346 if (link_order->type == bfd_section_reloc_link_order)
11347 sym_name = bfd_section_name (output_bfd,
11348 link_order->u.reloc.p->u.section);
11350 sym_name = link_order->u.reloc.p->u.name;
11351 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11352 howto->name, addend, NULL, NULL,
11357 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11359 * bfd_octets_per_byte (output_bfd),
11366 /* The address of a reloc is relative to the section in a
11367 relocatable file, and is a virtual address in an executable
11369 offset = link_order->offset;
11370 if (! bfd_link_relocatable (info))
11371 offset += output_section->vma;
11373 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11375 irel[i].r_offset = offset;
11376 irel[i].r_info = 0;
11377 irel[i].r_addend = 0;
11379 if (bed->s->arch_size == 32)
11380 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11382 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11384 rel_hdr = reldata->hdr;
11385 erel = rel_hdr->contents;
11386 if (rel_hdr->sh_type == SHT_REL)
11388 erel += reldata->count * bed->s->sizeof_rel;
11389 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11393 irel[0].r_addend = addend;
11394 erel += reldata->count * bed->s->sizeof_rela;
11395 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11404 /* Get the output vma of the section pointed to by the sh_link field. */
11407 elf_get_linked_section_vma (struct bfd_link_order *p)
11409 Elf_Internal_Shdr **elf_shdrp;
11413 s = p->u.indirect.section;
11414 elf_shdrp = elf_elfsections (s->owner);
11415 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11416 elfsec = elf_shdrp[elfsec]->sh_link;
11418 The Intel C compiler generates SHT_IA_64_UNWIND with
11419 SHF_LINK_ORDER. But it doesn't set the sh_link or
11420 sh_info fields. Hence we could get the situation
11421 where elfsec is 0. */
11424 const struct elf_backend_data *bed
11425 = get_elf_backend_data (s->owner);
11426 if (bed->link_order_error_handler)
11427 bed->link_order_error_handler
11428 /* xgettext:c-format */
11429 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11434 s = elf_shdrp[elfsec]->bfd_section;
11435 return s->output_section->vma + s->output_offset;
11440 /* Compare two sections based on the locations of the sections they are
11441 linked to. Used by elf_fixup_link_order. */
11444 compare_link_order (const void * a, const void * b)
11449 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11450 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11453 return apos > bpos;
11457 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11458 order as their linked sections. Returns false if this could not be done
11459 because an output section includes both ordered and unordered
11460 sections. Ideally we'd do this in the linker proper. */
11463 elf_fixup_link_order (bfd *abfd, asection *o)
11465 int seen_linkorder;
11468 struct bfd_link_order *p;
11470 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11472 struct bfd_link_order **sections;
11473 asection *s, *other_sec, *linkorder_sec;
11477 linkorder_sec = NULL;
11479 seen_linkorder = 0;
11480 for (p = o->map_head.link_order; p != NULL; p = p->next)
11482 if (p->type == bfd_indirect_link_order)
11484 s = p->u.indirect.section;
11486 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11487 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11488 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11489 && elfsec < elf_numsections (sub)
11490 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11491 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11505 if (seen_other && seen_linkorder)
11507 if (other_sec && linkorder_sec)
11509 /* xgettext:c-format */
11510 (_("%pA has both ordered [`%pA' in %pB] "
11511 "and unordered [`%pA' in %pB] sections"),
11512 o, linkorder_sec, linkorder_sec->owner,
11513 other_sec, other_sec->owner);
11516 (_("%pA has both ordered and unordered sections"), o);
11517 bfd_set_error (bfd_error_bad_value);
11522 if (!seen_linkorder)
11525 sections = (struct bfd_link_order **)
11526 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11527 if (sections == NULL)
11529 seen_linkorder = 0;
11531 for (p = o->map_head.link_order; p != NULL; p = p->next)
11533 sections[seen_linkorder++] = p;
11535 /* Sort the input sections in the order of their linked section. */
11536 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11537 compare_link_order);
11539 /* Change the offsets of the sections. */
11541 for (n = 0; n < seen_linkorder; n++)
11543 s = sections[n]->u.indirect.section;
11544 offset &= ~(bfd_vma) 0 << s->alignment_power;
11545 s->output_offset = offset / bfd_octets_per_byte (abfd);
11546 sections[n]->offset = offset;
11547 offset += sections[n]->size;
11554 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11555 Returns TRUE upon success, FALSE otherwise. */
11558 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11560 bfd_boolean ret = FALSE;
11562 const struct elf_backend_data *bed;
11564 enum bfd_architecture arch;
11566 asymbol **sympp = NULL;
11570 elf_symbol_type *osymbuf;
11572 implib_bfd = info->out_implib_bfd;
11573 bed = get_elf_backend_data (abfd);
11575 if (!bfd_set_format (implib_bfd, bfd_object))
11578 /* Use flag from executable but make it a relocatable object. */
11579 flags = bfd_get_file_flags (abfd);
11580 flags &= ~HAS_RELOC;
11581 if (!bfd_set_start_address (implib_bfd, 0)
11582 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11585 /* Copy architecture of output file to import library file. */
11586 arch = bfd_get_arch (abfd);
11587 mach = bfd_get_mach (abfd);
11588 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11589 && (abfd->target_defaulted
11590 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11593 /* Get symbol table size. */
11594 symsize = bfd_get_symtab_upper_bound (abfd);
11598 /* Read in the symbol table. */
11599 sympp = (asymbol **) xmalloc (symsize);
11600 symcount = bfd_canonicalize_symtab (abfd, sympp);
11604 /* Allow the BFD backend to copy any private header data it
11605 understands from the output BFD to the import library BFD. */
11606 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11609 /* Filter symbols to appear in the import library. */
11610 if (bed->elf_backend_filter_implib_symbols)
11611 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11614 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11617 bfd_set_error (bfd_error_no_symbols);
11618 _bfd_error_handler (_("%pB: no symbol found for import library"),
11624 /* Make symbols absolute. */
11625 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11626 sizeof (*osymbuf));
11627 for (src_count = 0; src_count < symcount; src_count++)
11629 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11630 sizeof (*osymbuf));
11631 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11632 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11633 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11634 osymbuf[src_count].internal_elf_sym.st_value =
11635 osymbuf[src_count].symbol.value;
11636 sympp[src_count] = &osymbuf[src_count].symbol;
11639 bfd_set_symtab (implib_bfd, sympp, symcount);
11641 /* Allow the BFD backend to copy any private data it understands
11642 from the output BFD to the import library BFD. This is done last
11643 to permit the routine to look at the filtered symbol table. */
11644 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11647 if (!bfd_close (implib_bfd))
11658 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11662 if (flinfo->symstrtab != NULL)
11663 _bfd_elf_strtab_free (flinfo->symstrtab);
11664 if (flinfo->contents != NULL)
11665 free (flinfo->contents);
11666 if (flinfo->external_relocs != NULL)
11667 free (flinfo->external_relocs);
11668 if (flinfo->internal_relocs != NULL)
11669 free (flinfo->internal_relocs);
11670 if (flinfo->external_syms != NULL)
11671 free (flinfo->external_syms);
11672 if (flinfo->locsym_shndx != NULL)
11673 free (flinfo->locsym_shndx);
11674 if (flinfo->internal_syms != NULL)
11675 free (flinfo->internal_syms);
11676 if (flinfo->indices != NULL)
11677 free (flinfo->indices);
11678 if (flinfo->sections != NULL)
11679 free (flinfo->sections);
11680 if (flinfo->symshndxbuf != NULL)
11681 free (flinfo->symshndxbuf);
11682 for (o = obfd->sections; o != NULL; o = o->next)
11684 struct bfd_elf_section_data *esdo = elf_section_data (o);
11685 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11686 free (esdo->rel.hashes);
11687 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11688 free (esdo->rela.hashes);
11692 /* Do the final step of an ELF link. */
11695 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11697 bfd_boolean dynamic;
11698 bfd_boolean emit_relocs;
11700 struct elf_final_link_info flinfo;
11702 struct bfd_link_order *p;
11704 bfd_size_type max_contents_size;
11705 bfd_size_type max_external_reloc_size;
11706 bfd_size_type max_internal_reloc_count;
11707 bfd_size_type max_sym_count;
11708 bfd_size_type max_sym_shndx_count;
11709 Elf_Internal_Sym elfsym;
11711 Elf_Internal_Shdr *symtab_hdr;
11712 Elf_Internal_Shdr *symtab_shndx_hdr;
11713 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11714 struct elf_outext_info eoinfo;
11715 bfd_boolean merged;
11716 size_t relativecount = 0;
11717 asection *reldyn = 0;
11719 asection *attr_section = NULL;
11720 bfd_vma attr_size = 0;
11721 const char *std_attrs_section;
11722 struct elf_link_hash_table *htab = elf_hash_table (info);
11724 if (!is_elf_hash_table (htab))
11727 if (bfd_link_pic (info))
11728 abfd->flags |= DYNAMIC;
11730 dynamic = htab->dynamic_sections_created;
11731 dynobj = htab->dynobj;
11733 emit_relocs = (bfd_link_relocatable (info)
11734 || info->emitrelocations);
11736 flinfo.info = info;
11737 flinfo.output_bfd = abfd;
11738 flinfo.symstrtab = _bfd_elf_strtab_init ();
11739 if (flinfo.symstrtab == NULL)
11744 flinfo.hash_sec = NULL;
11745 flinfo.symver_sec = NULL;
11749 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11750 /* Note that dynsym_sec can be NULL (on VMS). */
11751 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11752 /* Note that it is OK if symver_sec is NULL. */
11755 flinfo.contents = NULL;
11756 flinfo.external_relocs = NULL;
11757 flinfo.internal_relocs = NULL;
11758 flinfo.external_syms = NULL;
11759 flinfo.locsym_shndx = NULL;
11760 flinfo.internal_syms = NULL;
11761 flinfo.indices = NULL;
11762 flinfo.sections = NULL;
11763 flinfo.symshndxbuf = NULL;
11764 flinfo.filesym_count = 0;
11766 /* The object attributes have been merged. Remove the input
11767 sections from the link, and set the contents of the output
11769 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11770 for (o = abfd->sections; o != NULL; o = o->next)
11772 bfd_boolean remove_section = FALSE;
11774 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11775 || strcmp (o->name, ".gnu.attributes") == 0)
11777 for (p = o->map_head.link_order; p != NULL; p = p->next)
11779 asection *input_section;
11781 if (p->type != bfd_indirect_link_order)
11783 input_section = p->u.indirect.section;
11784 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11785 elf_link_input_bfd ignores this section. */
11786 input_section->flags &= ~SEC_HAS_CONTENTS;
11789 attr_size = bfd_elf_obj_attr_size (abfd);
11790 bfd_set_section_size (abfd, o, attr_size);
11791 /* Skip this section later on. */
11792 o->map_head.link_order = NULL;
11796 remove_section = TRUE;
11798 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11800 /* Remove empty group section from linker output. */
11801 remove_section = TRUE;
11803 if (remove_section)
11805 o->flags |= SEC_EXCLUDE;
11806 bfd_section_list_remove (abfd, o);
11807 abfd->section_count--;
11811 /* Count up the number of relocations we will output for each output
11812 section, so that we know the sizes of the reloc sections. We
11813 also figure out some maximum sizes. */
11814 max_contents_size = 0;
11815 max_external_reloc_size = 0;
11816 max_internal_reloc_count = 0;
11818 max_sym_shndx_count = 0;
11820 for (o = abfd->sections; o != NULL; o = o->next)
11822 struct bfd_elf_section_data *esdo = elf_section_data (o);
11823 o->reloc_count = 0;
11825 for (p = o->map_head.link_order; p != NULL; p = p->next)
11827 unsigned int reloc_count = 0;
11828 unsigned int additional_reloc_count = 0;
11829 struct bfd_elf_section_data *esdi = NULL;
11831 if (p->type == bfd_section_reloc_link_order
11832 || p->type == bfd_symbol_reloc_link_order)
11834 else if (p->type == bfd_indirect_link_order)
11838 sec = p->u.indirect.section;
11840 /* Mark all sections which are to be included in the
11841 link. This will normally be every section. We need
11842 to do this so that we can identify any sections which
11843 the linker has decided to not include. */
11844 sec->linker_mark = TRUE;
11846 if (sec->flags & SEC_MERGE)
11849 if (sec->rawsize > max_contents_size)
11850 max_contents_size = sec->rawsize;
11851 if (sec->size > max_contents_size)
11852 max_contents_size = sec->size;
11854 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11855 && (sec->owner->flags & DYNAMIC) == 0)
11859 /* We are interested in just local symbols, not all
11861 if (elf_bad_symtab (sec->owner))
11862 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11863 / bed->s->sizeof_sym);
11865 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11867 if (sym_count > max_sym_count)
11868 max_sym_count = sym_count;
11870 if (sym_count > max_sym_shndx_count
11871 && elf_symtab_shndx_list (sec->owner) != NULL)
11872 max_sym_shndx_count = sym_count;
11874 if (esdo->this_hdr.sh_type == SHT_REL
11875 || esdo->this_hdr.sh_type == SHT_RELA)
11876 /* Some backends use reloc_count in relocation sections
11877 to count particular types of relocs. Of course,
11878 reloc sections themselves can't have relocations. */
11880 else if (emit_relocs)
11882 reloc_count = sec->reloc_count;
11883 if (bed->elf_backend_count_additional_relocs)
11886 c = (*bed->elf_backend_count_additional_relocs) (sec);
11887 additional_reloc_count += c;
11890 else if (bed->elf_backend_count_relocs)
11891 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11893 esdi = elf_section_data (sec);
11895 if ((sec->flags & SEC_RELOC) != 0)
11897 size_t ext_size = 0;
11899 if (esdi->rel.hdr != NULL)
11900 ext_size = esdi->rel.hdr->sh_size;
11901 if (esdi->rela.hdr != NULL)
11902 ext_size += esdi->rela.hdr->sh_size;
11904 if (ext_size > max_external_reloc_size)
11905 max_external_reloc_size = ext_size;
11906 if (sec->reloc_count > max_internal_reloc_count)
11907 max_internal_reloc_count = sec->reloc_count;
11912 if (reloc_count == 0)
11915 reloc_count += additional_reloc_count;
11916 o->reloc_count += reloc_count;
11918 if (p->type == bfd_indirect_link_order && emit_relocs)
11922 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11923 esdo->rel.count += additional_reloc_count;
11925 if (esdi->rela.hdr)
11927 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11928 esdo->rela.count += additional_reloc_count;
11934 esdo->rela.count += reloc_count;
11936 esdo->rel.count += reloc_count;
11940 if (o->reloc_count > 0)
11941 o->flags |= SEC_RELOC;
11944 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11945 set it (this is probably a bug) and if it is set
11946 assign_section_numbers will create a reloc section. */
11947 o->flags &=~ SEC_RELOC;
11950 /* If the SEC_ALLOC flag is not set, force the section VMA to
11951 zero. This is done in elf_fake_sections as well, but forcing
11952 the VMA to 0 here will ensure that relocs against these
11953 sections are handled correctly. */
11954 if ((o->flags & SEC_ALLOC) == 0
11955 && ! o->user_set_vma)
11959 if (! bfd_link_relocatable (info) && merged)
11960 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11962 /* Figure out the file positions for everything but the symbol table
11963 and the relocs. We set symcount to force assign_section_numbers
11964 to create a symbol table. */
11965 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11966 BFD_ASSERT (! abfd->output_has_begun);
11967 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11970 /* Set sizes, and assign file positions for reloc sections. */
11971 for (o = abfd->sections; o != NULL; o = o->next)
11973 struct bfd_elf_section_data *esdo = elf_section_data (o);
11974 if ((o->flags & SEC_RELOC) != 0)
11977 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11981 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11985 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11986 to count upwards while actually outputting the relocations. */
11987 esdo->rel.count = 0;
11988 esdo->rela.count = 0;
11990 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11992 /* Cache the section contents so that they can be compressed
11993 later. Use bfd_malloc since it will be freed by
11994 bfd_compress_section_contents. */
11995 unsigned char *contents = esdo->this_hdr.contents;
11996 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11999 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12000 if (contents == NULL)
12002 esdo->this_hdr.contents = contents;
12006 /* We have now assigned file positions for all the sections except
12007 .symtab, .strtab, and non-loaded reloc sections. We start the
12008 .symtab section at the current file position, and write directly
12009 to it. We build the .strtab section in memory. */
12010 bfd_get_symcount (abfd) = 0;
12011 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12012 /* sh_name is set in prep_headers. */
12013 symtab_hdr->sh_type = SHT_SYMTAB;
12014 /* sh_flags, sh_addr and sh_size all start off zero. */
12015 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12016 /* sh_link is set in assign_section_numbers. */
12017 /* sh_info is set below. */
12018 /* sh_offset is set just below. */
12019 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12021 if (max_sym_count < 20)
12022 max_sym_count = 20;
12023 htab->strtabsize = max_sym_count;
12024 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12025 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12026 if (htab->strtab == NULL)
12028 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12030 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12031 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12033 if (info->strip != strip_all || emit_relocs)
12035 file_ptr off = elf_next_file_pos (abfd);
12037 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12039 /* Note that at this point elf_next_file_pos (abfd) is
12040 incorrect. We do not yet know the size of the .symtab section.
12041 We correct next_file_pos below, after we do know the size. */
12043 /* Start writing out the symbol table. The first symbol is always a
12045 elfsym.st_value = 0;
12046 elfsym.st_size = 0;
12047 elfsym.st_info = 0;
12048 elfsym.st_other = 0;
12049 elfsym.st_shndx = SHN_UNDEF;
12050 elfsym.st_target_internal = 0;
12051 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12052 bfd_und_section_ptr, NULL) != 1)
12055 /* Output a symbol for each section. We output these even if we are
12056 discarding local symbols, since they are used for relocs. These
12057 symbols have no names. We store the index of each one in the
12058 index field of the section, so that we can find it again when
12059 outputting relocs. */
12061 elfsym.st_size = 0;
12062 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12063 elfsym.st_other = 0;
12064 elfsym.st_value = 0;
12065 elfsym.st_target_internal = 0;
12066 for (i = 1; i < elf_numsections (abfd); i++)
12068 o = bfd_section_from_elf_index (abfd, i);
12071 o->target_index = bfd_get_symcount (abfd);
12072 elfsym.st_shndx = i;
12073 if (!bfd_link_relocatable (info))
12074 elfsym.st_value = o->vma;
12075 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12082 /* Allocate some memory to hold information read in from the input
12084 if (max_contents_size != 0)
12086 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12087 if (flinfo.contents == NULL)
12091 if (max_external_reloc_size != 0)
12093 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12094 if (flinfo.external_relocs == NULL)
12098 if (max_internal_reloc_count != 0)
12100 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12101 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12102 if (flinfo.internal_relocs == NULL)
12106 if (max_sym_count != 0)
12108 amt = max_sym_count * bed->s->sizeof_sym;
12109 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12110 if (flinfo.external_syms == NULL)
12113 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12114 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12115 if (flinfo.internal_syms == NULL)
12118 amt = max_sym_count * sizeof (long);
12119 flinfo.indices = (long int *) bfd_malloc (amt);
12120 if (flinfo.indices == NULL)
12123 amt = max_sym_count * sizeof (asection *);
12124 flinfo.sections = (asection **) bfd_malloc (amt);
12125 if (flinfo.sections == NULL)
12129 if (max_sym_shndx_count != 0)
12131 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12132 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12133 if (flinfo.locsym_shndx == NULL)
12139 bfd_vma base, end = 0;
12142 for (sec = htab->tls_sec;
12143 sec && (sec->flags & SEC_THREAD_LOCAL);
12146 bfd_size_type size = sec->size;
12149 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12151 struct bfd_link_order *ord = sec->map_tail.link_order;
12154 size = ord->offset + ord->size;
12156 end = sec->vma + size;
12158 base = htab->tls_sec->vma;
12159 /* Only align end of TLS section if static TLS doesn't have special
12160 alignment requirements. */
12161 if (bed->static_tls_alignment == 1)
12162 end = align_power (end, htab->tls_sec->alignment_power);
12163 htab->tls_size = end - base;
12166 /* Reorder SHF_LINK_ORDER sections. */
12167 for (o = abfd->sections; o != NULL; o = o->next)
12169 if (!elf_fixup_link_order (abfd, o))
12173 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12176 /* Since ELF permits relocations to be against local symbols, we
12177 must have the local symbols available when we do the relocations.
12178 Since we would rather only read the local symbols once, and we
12179 would rather not keep them in memory, we handle all the
12180 relocations for a single input file at the same time.
12182 Unfortunately, there is no way to know the total number of local
12183 symbols until we have seen all of them, and the local symbol
12184 indices precede the global symbol indices. This means that when
12185 we are generating relocatable output, and we see a reloc against
12186 a global symbol, we can not know the symbol index until we have
12187 finished examining all the local symbols to see which ones we are
12188 going to output. To deal with this, we keep the relocations in
12189 memory, and don't output them until the end of the link. This is
12190 an unfortunate waste of memory, but I don't see a good way around
12191 it. Fortunately, it only happens when performing a relocatable
12192 link, which is not the common case. FIXME: If keep_memory is set
12193 we could write the relocs out and then read them again; I don't
12194 know how bad the memory loss will be. */
12196 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12197 sub->output_has_begun = FALSE;
12198 for (o = abfd->sections; o != NULL; o = o->next)
12200 for (p = o->map_head.link_order; p != NULL; p = p->next)
12202 if (p->type == bfd_indirect_link_order
12203 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12204 == bfd_target_elf_flavour)
12205 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12207 if (! sub->output_has_begun)
12209 if (! elf_link_input_bfd (&flinfo, sub))
12211 sub->output_has_begun = TRUE;
12214 else if (p->type == bfd_section_reloc_link_order
12215 || p->type == bfd_symbol_reloc_link_order)
12217 if (! elf_reloc_link_order (abfd, info, o, p))
12222 if (! _bfd_default_link_order (abfd, info, o, p))
12224 if (p->type == bfd_indirect_link_order
12225 && (bfd_get_flavour (sub)
12226 == bfd_target_elf_flavour)
12227 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12228 != bed->s->elfclass))
12230 const char *iclass, *oclass;
12232 switch (bed->s->elfclass)
12234 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12235 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12236 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12240 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12242 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12243 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12244 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12248 bfd_set_error (bfd_error_wrong_format);
12250 /* xgettext:c-format */
12251 (_("%pB: file class %s incompatible with %s"),
12252 sub, iclass, oclass);
12261 /* Free symbol buffer if needed. */
12262 if (!info->reduce_memory_overheads)
12264 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12265 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12266 && elf_tdata (sub)->symbuf)
12268 free (elf_tdata (sub)->symbuf);
12269 elf_tdata (sub)->symbuf = NULL;
12273 /* Output any global symbols that got converted to local in a
12274 version script or due to symbol visibility. We do this in a
12275 separate step since ELF requires all local symbols to appear
12276 prior to any global symbols. FIXME: We should only do this if
12277 some global symbols were, in fact, converted to become local.
12278 FIXME: Will this work correctly with the Irix 5 linker? */
12279 eoinfo.failed = FALSE;
12280 eoinfo.flinfo = &flinfo;
12281 eoinfo.localsyms = TRUE;
12282 eoinfo.file_sym_done = FALSE;
12283 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12287 /* If backend needs to output some local symbols not present in the hash
12288 table, do it now. */
12289 if (bed->elf_backend_output_arch_local_syms
12290 && (info->strip != strip_all || emit_relocs))
12292 typedef int (*out_sym_func)
12293 (void *, const char *, Elf_Internal_Sym *, asection *,
12294 struct elf_link_hash_entry *);
12296 if (! ((*bed->elf_backend_output_arch_local_syms)
12297 (abfd, info, &flinfo,
12298 (out_sym_func) elf_link_output_symstrtab)))
12302 /* That wrote out all the local symbols. Finish up the symbol table
12303 with the global symbols. Even if we want to strip everything we
12304 can, we still need to deal with those global symbols that got
12305 converted to local in a version script. */
12307 /* The sh_info field records the index of the first non local symbol. */
12308 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12311 && htab->dynsym != NULL
12312 && htab->dynsym->output_section != bfd_abs_section_ptr)
12314 Elf_Internal_Sym sym;
12315 bfd_byte *dynsym = htab->dynsym->contents;
12317 o = htab->dynsym->output_section;
12318 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12320 /* Write out the section symbols for the output sections. */
12321 if (bfd_link_pic (info)
12322 || htab->is_relocatable_executable)
12328 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12330 sym.st_target_internal = 0;
12332 for (s = abfd->sections; s != NULL; s = s->next)
12338 dynindx = elf_section_data (s)->dynindx;
12341 indx = elf_section_data (s)->this_idx;
12342 BFD_ASSERT (indx > 0);
12343 sym.st_shndx = indx;
12344 if (! check_dynsym (abfd, &sym))
12346 sym.st_value = s->vma;
12347 dest = dynsym + dynindx * bed->s->sizeof_sym;
12348 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12352 /* Write out the local dynsyms. */
12353 if (htab->dynlocal)
12355 struct elf_link_local_dynamic_entry *e;
12356 for (e = htab->dynlocal; e ; e = e->next)
12361 /* Copy the internal symbol and turn off visibility.
12362 Note that we saved a word of storage and overwrote
12363 the original st_name with the dynstr_index. */
12365 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12367 s = bfd_section_from_elf_index (e->input_bfd,
12372 elf_section_data (s->output_section)->this_idx;
12373 if (! check_dynsym (abfd, &sym))
12375 sym.st_value = (s->output_section->vma
12377 + e->isym.st_value);
12380 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12381 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12386 /* We get the global symbols from the hash table. */
12387 eoinfo.failed = FALSE;
12388 eoinfo.localsyms = FALSE;
12389 eoinfo.flinfo = &flinfo;
12390 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12394 /* If backend needs to output some symbols not present in the hash
12395 table, do it now. */
12396 if (bed->elf_backend_output_arch_syms
12397 && (info->strip != strip_all || emit_relocs))
12399 typedef int (*out_sym_func)
12400 (void *, const char *, Elf_Internal_Sym *, asection *,
12401 struct elf_link_hash_entry *);
12403 if (! ((*bed->elf_backend_output_arch_syms)
12404 (abfd, info, &flinfo,
12405 (out_sym_func) elf_link_output_symstrtab)))
12409 /* Finalize the .strtab section. */
12410 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12412 /* Swap out the .strtab section. */
12413 if (!elf_link_swap_symbols_out (&flinfo))
12416 /* Now we know the size of the symtab section. */
12417 if (bfd_get_symcount (abfd) > 0)
12419 /* Finish up and write out the symbol string table (.strtab)
12421 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12422 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12424 if (elf_symtab_shndx_list (abfd))
12426 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12428 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12430 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12431 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12432 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12433 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12434 symtab_shndx_hdr->sh_size = amt;
12436 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12439 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12440 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12445 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12446 /* sh_name was set in prep_headers. */
12447 symstrtab_hdr->sh_type = SHT_STRTAB;
12448 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12449 symstrtab_hdr->sh_addr = 0;
12450 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12451 symstrtab_hdr->sh_entsize = 0;
12452 symstrtab_hdr->sh_link = 0;
12453 symstrtab_hdr->sh_info = 0;
12454 /* sh_offset is set just below. */
12455 symstrtab_hdr->sh_addralign = 1;
12457 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12459 elf_next_file_pos (abfd) = off;
12461 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12462 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12466 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12468 _bfd_error_handler (_("%pB: failed to generate import library"),
12469 info->out_implib_bfd);
12473 /* Adjust the relocs to have the correct symbol indices. */
12474 for (o = abfd->sections; o != NULL; o = o->next)
12476 struct bfd_elf_section_data *esdo = elf_section_data (o);
12479 if ((o->flags & SEC_RELOC) == 0)
12482 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12483 if (esdo->rel.hdr != NULL
12484 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12486 if (esdo->rela.hdr != NULL
12487 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12490 /* Set the reloc_count field to 0 to prevent write_relocs from
12491 trying to swap the relocs out itself. */
12492 o->reloc_count = 0;
12495 if (dynamic && info->combreloc && dynobj != NULL)
12496 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12498 /* If we are linking against a dynamic object, or generating a
12499 shared library, finish up the dynamic linking information. */
12502 bfd_byte *dyncon, *dynconend;
12504 /* Fix up .dynamic entries. */
12505 o = bfd_get_linker_section (dynobj, ".dynamic");
12506 BFD_ASSERT (o != NULL);
12508 dyncon = o->contents;
12509 dynconend = o->contents + o->size;
12510 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12512 Elf_Internal_Dyn dyn;
12515 bfd_size_type sh_size;
12518 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12525 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12527 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12529 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12530 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12533 dyn.d_un.d_val = relativecount;
12540 name = info->init_function;
12543 name = info->fini_function;
12546 struct elf_link_hash_entry *h;
12548 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12550 && (h->root.type == bfd_link_hash_defined
12551 || h->root.type == bfd_link_hash_defweak))
12553 dyn.d_un.d_ptr = h->root.u.def.value;
12554 o = h->root.u.def.section;
12555 if (o->output_section != NULL)
12556 dyn.d_un.d_ptr += (o->output_section->vma
12557 + o->output_offset);
12560 /* The symbol is imported from another shared
12561 library and does not apply to this one. */
12562 dyn.d_un.d_ptr = 0;
12569 case DT_PREINIT_ARRAYSZ:
12570 name = ".preinit_array";
12572 case DT_INIT_ARRAYSZ:
12573 name = ".init_array";
12575 case DT_FINI_ARRAYSZ:
12576 name = ".fini_array";
12578 o = bfd_get_section_by_name (abfd, name);
12582 (_("could not find section %s"), name);
12587 (_("warning: %s section has zero size"), name);
12588 dyn.d_un.d_val = o->size;
12591 case DT_PREINIT_ARRAY:
12592 name = ".preinit_array";
12594 case DT_INIT_ARRAY:
12595 name = ".init_array";
12597 case DT_FINI_ARRAY:
12598 name = ".fini_array";
12600 o = bfd_get_section_by_name (abfd, name);
12607 name = ".gnu.hash";
12616 name = ".gnu.version_d";
12619 name = ".gnu.version_r";
12622 name = ".gnu.version";
12624 o = bfd_get_linker_section (dynobj, name);
12626 if (o == NULL || bfd_is_abs_section (o->output_section))
12629 (_("could not find section %s"), name);
12632 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12635 (_("warning: section '%s' is being made into a note"), name);
12636 bfd_set_error (bfd_error_nonrepresentable_section);
12639 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12646 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12652 for (i = 1; i < elf_numsections (abfd); i++)
12654 Elf_Internal_Shdr *hdr;
12656 hdr = elf_elfsections (abfd)[i];
12657 if (hdr->sh_type == type
12658 && (hdr->sh_flags & SHF_ALLOC) != 0)
12660 sh_size += hdr->sh_size;
12662 || sh_addr > hdr->sh_addr)
12663 sh_addr = hdr->sh_addr;
12667 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12669 /* Don't count procedure linkage table relocs in the
12670 overall reloc count. */
12671 sh_size -= htab->srelplt->size;
12673 /* If the size is zero, make the address zero too.
12674 This is to avoid a glibc bug. If the backend
12675 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12676 zero, then we'll put DT_RELA at the end of
12677 DT_JMPREL. glibc will interpret the end of
12678 DT_RELA matching the end of DT_JMPREL as the
12679 case where DT_RELA includes DT_JMPREL, and for
12680 LD_BIND_NOW will decide that processing DT_RELA
12681 will process the PLT relocs too. Net result:
12682 No PLT relocs applied. */
12685 /* If .rela.plt is the first .rela section, exclude
12686 it from DT_RELA. */
12687 else if (sh_addr == (htab->srelplt->output_section->vma
12688 + htab->srelplt->output_offset))
12689 sh_addr += htab->srelplt->size;
12692 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12693 dyn.d_un.d_val = sh_size;
12695 dyn.d_un.d_ptr = sh_addr;
12698 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12702 /* If we have created any dynamic sections, then output them. */
12703 if (dynobj != NULL)
12705 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12708 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12709 if (((info->warn_shared_textrel && bfd_link_pic (info))
12710 || info->error_textrel)
12711 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12713 bfd_byte *dyncon, *dynconend;
12715 dyncon = o->contents;
12716 dynconend = o->contents + o->size;
12717 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12719 Elf_Internal_Dyn dyn;
12721 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12723 if (dyn.d_tag == DT_TEXTREL)
12725 if (info->error_textrel)
12726 info->callbacks->einfo
12727 (_("%P%X: read-only segment has dynamic relocations\n"));
12729 info->callbacks->einfo
12730 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12736 for (o = dynobj->sections; o != NULL; o = o->next)
12738 if ((o->flags & SEC_HAS_CONTENTS) == 0
12740 || o->output_section == bfd_abs_section_ptr)
12742 if ((o->flags & SEC_LINKER_CREATED) == 0)
12744 /* At this point, we are only interested in sections
12745 created by _bfd_elf_link_create_dynamic_sections. */
12748 if (htab->stab_info.stabstr == o)
12750 if (htab->eh_info.hdr_sec == o)
12752 if (strcmp (o->name, ".dynstr") != 0)
12754 if (! bfd_set_section_contents (abfd, o->output_section,
12756 (file_ptr) o->output_offset
12757 * bfd_octets_per_byte (abfd),
12763 /* The contents of the .dynstr section are actually in a
12767 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12768 if (bfd_seek (abfd, off, SEEK_SET) != 0
12769 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12775 if (!info->resolve_section_groups)
12777 bfd_boolean failed = FALSE;
12779 BFD_ASSERT (bfd_link_relocatable (info));
12780 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12785 /* If we have optimized stabs strings, output them. */
12786 if (htab->stab_info.stabstr != NULL)
12788 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12792 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12795 elf_final_link_free (abfd, &flinfo);
12797 elf_linker (abfd) = TRUE;
12801 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12802 if (contents == NULL)
12803 return FALSE; /* Bail out and fail. */
12804 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12805 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12812 elf_final_link_free (abfd, &flinfo);
12816 /* Initialize COOKIE for input bfd ABFD. */
12819 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12820 struct bfd_link_info *info, bfd *abfd)
12822 Elf_Internal_Shdr *symtab_hdr;
12823 const struct elf_backend_data *bed;
12825 bed = get_elf_backend_data (abfd);
12826 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12828 cookie->abfd = abfd;
12829 cookie->sym_hashes = elf_sym_hashes (abfd);
12830 cookie->bad_symtab = elf_bad_symtab (abfd);
12831 if (cookie->bad_symtab)
12833 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12834 cookie->extsymoff = 0;
12838 cookie->locsymcount = symtab_hdr->sh_info;
12839 cookie->extsymoff = symtab_hdr->sh_info;
12842 if (bed->s->arch_size == 32)
12843 cookie->r_sym_shift = 8;
12845 cookie->r_sym_shift = 32;
12847 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12848 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12850 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12851 cookie->locsymcount, 0,
12853 if (cookie->locsyms == NULL)
12855 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12858 if (info->keep_memory)
12859 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12864 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12867 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12869 Elf_Internal_Shdr *symtab_hdr;
12871 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12872 if (cookie->locsyms != NULL
12873 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12874 free (cookie->locsyms);
12877 /* Initialize the relocation information in COOKIE for input section SEC
12878 of input bfd ABFD. */
12881 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12882 struct bfd_link_info *info, bfd *abfd,
12885 if (sec->reloc_count == 0)
12887 cookie->rels = NULL;
12888 cookie->relend = NULL;
12892 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12893 info->keep_memory);
12894 if (cookie->rels == NULL)
12896 cookie->rel = cookie->rels;
12897 cookie->relend = cookie->rels + sec->reloc_count;
12899 cookie->rel = cookie->rels;
12903 /* Free the memory allocated by init_reloc_cookie_rels,
12907 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12910 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12911 free (cookie->rels);
12914 /* Initialize the whole of COOKIE for input section SEC. */
12917 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12918 struct bfd_link_info *info,
12921 if (!init_reloc_cookie (cookie, info, sec->owner))
12923 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12928 fini_reloc_cookie (cookie, sec->owner);
12933 /* Free the memory allocated by init_reloc_cookie_for_section,
12937 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12940 fini_reloc_cookie_rels (cookie, sec);
12941 fini_reloc_cookie (cookie, sec->owner);
12944 /* Garbage collect unused sections. */
12946 /* Default gc_mark_hook. */
12949 _bfd_elf_gc_mark_hook (asection *sec,
12950 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12951 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12952 struct elf_link_hash_entry *h,
12953 Elf_Internal_Sym *sym)
12957 switch (h->root.type)
12959 case bfd_link_hash_defined:
12960 case bfd_link_hash_defweak:
12961 return h->root.u.def.section;
12963 case bfd_link_hash_common:
12964 return h->root.u.c.p->section;
12971 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12976 /* Return the debug definition section. */
12979 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12980 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12981 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12982 struct elf_link_hash_entry *h,
12983 Elf_Internal_Sym *sym)
12987 /* Return the global debug definition section. */
12988 if ((h->root.type == bfd_link_hash_defined
12989 || h->root.type == bfd_link_hash_defweak)
12990 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12991 return h->root.u.def.section;
12995 /* Return the local debug definition section. */
12996 asection *isec = bfd_section_from_elf_index (sec->owner,
12998 if ((isec->flags & SEC_DEBUGGING) != 0)
13005 /* COOKIE->rel describes a relocation against section SEC, which is
13006 a section we've decided to keep. Return the section that contains
13007 the relocation symbol, or NULL if no section contains it. */
13010 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13011 elf_gc_mark_hook_fn gc_mark_hook,
13012 struct elf_reloc_cookie *cookie,
13013 bfd_boolean *start_stop)
13015 unsigned long r_symndx;
13016 struct elf_link_hash_entry *h;
13018 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13019 if (r_symndx == STN_UNDEF)
13022 if (r_symndx >= cookie->locsymcount
13023 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13025 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13028 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13032 while (h->root.type == bfd_link_hash_indirect
13033 || h->root.type == bfd_link_hash_warning)
13034 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13036 /* If this symbol is weak and there is a non-weak definition, we
13037 keep the non-weak definition because many backends put
13038 dynamic reloc info on the non-weak definition for code
13039 handling copy relocs. */
13040 if (h->is_weakalias)
13041 weakdef (h)->mark = 1;
13043 if (start_stop != NULL)
13045 /* To work around a glibc bug, mark XXX input sections
13046 when there is a reference to __start_XXX or __stop_XXX
13050 asection *s = h->u2.start_stop_section;
13051 *start_stop = !s->gc_mark;
13056 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13059 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13060 &cookie->locsyms[r_symndx]);
13063 /* COOKIE->rel describes a relocation against section SEC, which is
13064 a section we've decided to keep. Mark the section that contains
13065 the relocation symbol. */
13068 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13070 elf_gc_mark_hook_fn gc_mark_hook,
13071 struct elf_reloc_cookie *cookie)
13074 bfd_boolean start_stop = FALSE;
13076 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13077 while (rsec != NULL)
13079 if (!rsec->gc_mark)
13081 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13082 || (rsec->owner->flags & DYNAMIC) != 0)
13084 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13089 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13094 /* The mark phase of garbage collection. For a given section, mark
13095 it and any sections in this section's group, and all the sections
13096 which define symbols to which it refers. */
13099 _bfd_elf_gc_mark (struct bfd_link_info *info,
13101 elf_gc_mark_hook_fn gc_mark_hook)
13104 asection *group_sec, *eh_frame;
13108 /* Mark all the sections in the group. */
13109 group_sec = elf_section_data (sec)->next_in_group;
13110 if (group_sec && !group_sec->gc_mark)
13111 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13114 /* Look through the section relocs. */
13116 eh_frame = elf_eh_frame_section (sec->owner);
13117 if ((sec->flags & SEC_RELOC) != 0
13118 && sec->reloc_count > 0
13119 && sec != eh_frame)
13121 struct elf_reloc_cookie cookie;
13123 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13127 for (; cookie.rel < cookie.relend; cookie.rel++)
13128 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13133 fini_reloc_cookie_for_section (&cookie, sec);
13137 if (ret && eh_frame && elf_fde_list (sec))
13139 struct elf_reloc_cookie cookie;
13141 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13145 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13146 gc_mark_hook, &cookie))
13148 fini_reloc_cookie_for_section (&cookie, eh_frame);
13152 eh_frame = elf_section_eh_frame_entry (sec);
13153 if (ret && eh_frame && !eh_frame->gc_mark)
13154 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13160 /* Scan and mark sections in a special or debug section group. */
13163 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13165 /* Point to first section of section group. */
13167 /* Used to iterate the section group. */
13170 bfd_boolean is_special_grp = TRUE;
13171 bfd_boolean is_debug_grp = TRUE;
13173 /* First scan to see if group contains any section other than debug
13174 and special section. */
13175 ssec = msec = elf_next_in_group (grp);
13178 if ((msec->flags & SEC_DEBUGGING) == 0)
13179 is_debug_grp = FALSE;
13181 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13182 is_special_grp = FALSE;
13184 msec = elf_next_in_group (msec);
13186 while (msec != ssec);
13188 /* If this is a pure debug section group or pure special section group,
13189 keep all sections in this group. */
13190 if (is_debug_grp || is_special_grp)
13195 msec = elf_next_in_group (msec);
13197 while (msec != ssec);
13201 /* Keep debug and special sections. */
13204 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13205 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13209 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13212 bfd_boolean some_kept;
13213 bfd_boolean debug_frag_seen;
13214 bfd_boolean has_kept_debug_info;
13216 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13218 isec = ibfd->sections;
13219 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13222 /* Ensure all linker created sections are kept,
13223 see if any other section is already marked,
13224 and note if we have any fragmented debug sections. */
13225 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13226 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13228 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13230 else if (isec->gc_mark
13231 && (isec->flags & SEC_ALLOC) != 0
13232 && elf_section_type (isec) != SHT_NOTE)
13235 if (!debug_frag_seen
13236 && (isec->flags & SEC_DEBUGGING)
13237 && CONST_STRNEQ (isec->name, ".debug_line."))
13238 debug_frag_seen = TRUE;
13241 /* If no non-note alloc section in this file will be kept, then
13242 we can toss out the debug and special sections. */
13246 /* Keep debug and special sections like .comment when they are
13247 not part of a group. Also keep section groups that contain
13248 just debug sections or special sections. */
13249 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13251 if ((isec->flags & SEC_GROUP) != 0)
13252 _bfd_elf_gc_mark_debug_special_section_group (isec);
13253 else if (((isec->flags & SEC_DEBUGGING) != 0
13254 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13255 && elf_next_in_group (isec) == NULL)
13257 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13258 has_kept_debug_info = TRUE;
13261 /* Look for CODE sections which are going to be discarded,
13262 and find and discard any fragmented debug sections which
13263 are associated with that code section. */
13264 if (debug_frag_seen)
13265 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13266 if ((isec->flags & SEC_CODE) != 0
13267 && isec->gc_mark == 0)
13272 ilen = strlen (isec->name);
13274 /* Association is determined by the name of the debug
13275 section containing the name of the code section as
13276 a suffix. For example .debug_line.text.foo is a
13277 debug section associated with .text.foo. */
13278 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13282 if (dsec->gc_mark == 0
13283 || (dsec->flags & SEC_DEBUGGING) == 0)
13286 dlen = strlen (dsec->name);
13289 && strncmp (dsec->name + (dlen - ilen),
13290 isec->name, ilen) == 0)
13295 /* Mark debug sections referenced by kept debug sections. */
13296 if (has_kept_debug_info)
13297 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13299 && (isec->flags & SEC_DEBUGGING) != 0)
13300 if (!_bfd_elf_gc_mark (info, isec,
13301 elf_gc_mark_debug_section))
13308 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13311 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13313 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13317 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13318 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13319 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13322 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13325 for (o = sub->sections; o != NULL; o = o->next)
13327 /* When any section in a section group is kept, we keep all
13328 sections in the section group. If the first member of
13329 the section group is excluded, we will also exclude the
13331 if (o->flags & SEC_GROUP)
13333 asection *first = elf_next_in_group (o);
13334 o->gc_mark = first->gc_mark;
13340 /* Skip sweeping sections already excluded. */
13341 if (o->flags & SEC_EXCLUDE)
13344 /* Since this is early in the link process, it is simple
13345 to remove a section from the output. */
13346 o->flags |= SEC_EXCLUDE;
13348 if (info->print_gc_sections && o->size != 0)
13349 /* xgettext:c-format */
13350 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13358 /* Propagate collected vtable information. This is called through
13359 elf_link_hash_traverse. */
13362 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13364 /* Those that are not vtables. */
13366 || h->u2.vtable == NULL
13367 || h->u2.vtable->parent == NULL)
13370 /* Those vtables that do not have parents, we cannot merge. */
13371 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13374 /* If we've already been done, exit. */
13375 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13378 /* Make sure the parent's table is up to date. */
13379 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13381 if (h->u2.vtable->used == NULL)
13383 /* None of this table's entries were referenced. Re-use the
13385 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13386 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13391 bfd_boolean *cu, *pu;
13393 /* Or the parent's entries into ours. */
13394 cu = h->u2.vtable->used;
13396 pu = h->u2.vtable->parent->u2.vtable->used;
13399 const struct elf_backend_data *bed;
13400 unsigned int log_file_align;
13402 bed = get_elf_backend_data (h->root.u.def.section->owner);
13403 log_file_align = bed->s->log_file_align;
13404 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13419 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13422 bfd_vma hstart, hend;
13423 Elf_Internal_Rela *relstart, *relend, *rel;
13424 const struct elf_backend_data *bed;
13425 unsigned int log_file_align;
13427 /* Take care of both those symbols that do not describe vtables as
13428 well as those that are not loaded. */
13430 || h->u2.vtable == NULL
13431 || h->u2.vtable->parent == NULL)
13434 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13435 || h->root.type == bfd_link_hash_defweak);
13437 sec = h->root.u.def.section;
13438 hstart = h->root.u.def.value;
13439 hend = hstart + h->size;
13441 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13443 return *(bfd_boolean *) okp = FALSE;
13444 bed = get_elf_backend_data (sec->owner);
13445 log_file_align = bed->s->log_file_align;
13447 relend = relstart + sec->reloc_count;
13449 for (rel = relstart; rel < relend; ++rel)
13450 if (rel->r_offset >= hstart && rel->r_offset < hend)
13452 /* If the entry is in use, do nothing. */
13453 if (h->u2.vtable->used
13454 && (rel->r_offset - hstart) < h->u2.vtable->size)
13456 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13457 if (h->u2.vtable->used[entry])
13460 /* Otherwise, kill it. */
13461 rel->r_offset = rel->r_info = rel->r_addend = 0;
13467 /* Mark sections containing dynamically referenced symbols. When
13468 building shared libraries, we must assume that any visible symbol is
13472 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13474 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13475 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13477 if ((h->root.type == bfd_link_hash_defined
13478 || h->root.type == bfd_link_hash_defweak)
13479 && ((h->ref_dynamic && !h->forced_local)
13480 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13481 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13482 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13483 && (!bfd_link_executable (info)
13484 || info->gc_keep_exported
13485 || info->export_dynamic
13488 && (*d->match) (&d->head, NULL, h->root.root.string)))
13489 && (h->versioned >= versioned
13490 || !bfd_hide_sym_by_version (info->version_info,
13491 h->root.root.string)))))
13492 h->root.u.def.section->flags |= SEC_KEEP;
13497 /* Keep all sections containing symbols undefined on the command-line,
13498 and the section containing the entry symbol. */
13501 _bfd_elf_gc_keep (struct bfd_link_info *info)
13503 struct bfd_sym_chain *sym;
13505 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13507 struct elf_link_hash_entry *h;
13509 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13510 FALSE, FALSE, FALSE);
13513 && (h->root.type == bfd_link_hash_defined
13514 || h->root.type == bfd_link_hash_defweak)
13515 && !bfd_is_abs_section (h->root.u.def.section)
13516 && !bfd_is_und_section (h->root.u.def.section))
13517 h->root.u.def.section->flags |= SEC_KEEP;
13522 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13523 struct bfd_link_info *info)
13525 bfd *ibfd = info->input_bfds;
13527 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13530 struct elf_reloc_cookie cookie;
13532 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13534 sec = ibfd->sections;
13535 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13538 if (!init_reloc_cookie (&cookie, info, ibfd))
13541 for (sec = ibfd->sections; sec; sec = sec->next)
13543 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13544 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13546 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13547 fini_reloc_cookie_rels (&cookie, sec);
13554 /* Do mark and sweep of unused sections. */
13557 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13559 bfd_boolean ok = TRUE;
13561 elf_gc_mark_hook_fn gc_mark_hook;
13562 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13563 struct elf_link_hash_table *htab;
13565 if (!bed->can_gc_sections
13566 || !is_elf_hash_table (info->hash))
13568 _bfd_error_handler(_("warning: gc-sections option ignored"));
13572 bed->gc_keep (info);
13573 htab = elf_hash_table (info);
13575 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13576 at the .eh_frame section if we can mark the FDEs individually. */
13577 for (sub = info->input_bfds;
13578 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13579 sub = sub->link.next)
13582 struct elf_reloc_cookie cookie;
13584 sec = sub->sections;
13585 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13587 sec = bfd_get_section_by_name (sub, ".eh_frame");
13588 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13590 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13591 if (elf_section_data (sec)->sec_info
13592 && (sec->flags & SEC_LINKER_CREATED) == 0)
13593 elf_eh_frame_section (sub) = sec;
13594 fini_reloc_cookie_for_section (&cookie, sec);
13595 sec = bfd_get_next_section_by_name (NULL, sec);
13599 /* Apply transitive closure to the vtable entry usage info. */
13600 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13604 /* Kill the vtable relocations that were not used. */
13605 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13609 /* Mark dynamically referenced symbols. */
13610 if (htab->dynamic_sections_created || info->gc_keep_exported)
13611 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13613 /* Grovel through relocs to find out who stays ... */
13614 gc_mark_hook = bed->gc_mark_hook;
13615 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13619 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13620 || elf_object_id (sub) != elf_hash_table_id (htab)
13621 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13625 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13628 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13629 Also treat note sections as a root, if the section is not part
13630 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13631 well as FINI_ARRAY sections for ld -r. */
13632 for (o = sub->sections; o != NULL; o = o->next)
13634 && (o->flags & SEC_EXCLUDE) == 0
13635 && ((o->flags & SEC_KEEP) != 0
13636 || (bfd_link_relocatable (info)
13637 && ((elf_section_data (o)->this_hdr.sh_type
13638 == SHT_PREINIT_ARRAY)
13639 || (elf_section_data (o)->this_hdr.sh_type
13641 || (elf_section_data (o)->this_hdr.sh_type
13642 == SHT_FINI_ARRAY)))
13643 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13644 && elf_next_in_group (o) == NULL )))
13646 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13651 /* Allow the backend to mark additional target specific sections. */
13652 bed->gc_mark_extra_sections (info, gc_mark_hook);
13654 /* ... and mark SEC_EXCLUDE for those that go. */
13655 return elf_gc_sweep (abfd, info);
13658 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13661 bfd_elf_gc_record_vtinherit (bfd *abfd,
13663 struct elf_link_hash_entry *h,
13666 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13667 struct elf_link_hash_entry **search, *child;
13668 size_t extsymcount;
13669 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13671 /* The sh_info field of the symtab header tells us where the
13672 external symbols start. We don't care about the local symbols at
13674 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13675 if (!elf_bad_symtab (abfd))
13676 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13678 sym_hashes = elf_sym_hashes (abfd);
13679 sym_hashes_end = sym_hashes + extsymcount;
13681 /* Hunt down the child symbol, which is in this section at the same
13682 offset as the relocation. */
13683 for (search = sym_hashes; search != sym_hashes_end; ++search)
13685 if ((child = *search) != NULL
13686 && (child->root.type == bfd_link_hash_defined
13687 || child->root.type == bfd_link_hash_defweak)
13688 && child->root.u.def.section == sec
13689 && child->root.u.def.value == offset)
13693 /* xgettext:c-format */
13694 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13695 abfd, sec, (uint64_t) offset);
13696 bfd_set_error (bfd_error_invalid_operation);
13700 if (!child->u2.vtable)
13702 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13703 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13704 if (!child->u2.vtable)
13709 /* This *should* only be the absolute section. It could potentially
13710 be that someone has defined a non-global vtable though, which
13711 would be bad. It isn't worth paging in the local symbols to be
13712 sure though; that case should simply be handled by the assembler. */
13714 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13717 child->u2.vtable->parent = h;
13722 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13725 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13726 asection *sec ATTRIBUTE_UNUSED,
13727 struct elf_link_hash_entry *h,
13730 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13731 unsigned int log_file_align = bed->s->log_file_align;
13735 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13736 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13741 if (addend >= h->u2.vtable->size)
13743 size_t size, bytes, file_align;
13744 bfd_boolean *ptr = h->u2.vtable->used;
13746 /* While the symbol is undefined, we have to be prepared to handle
13748 file_align = 1 << log_file_align;
13749 if (h->root.type == bfd_link_hash_undefined)
13750 size = addend + file_align;
13754 if (addend >= size)
13756 /* Oops! We've got a reference past the defined end of
13757 the table. This is probably a bug -- shall we warn? */
13758 size = addend + file_align;
13761 size = (size + file_align - 1) & -file_align;
13763 /* Allocate one extra entry for use as a "done" flag for the
13764 consolidation pass. */
13765 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13769 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13775 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13776 * sizeof (bfd_boolean));
13777 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13781 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13786 /* And arrange for that done flag to be at index -1. */
13787 h->u2.vtable->used = ptr + 1;
13788 h->u2.vtable->size = size;
13791 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13796 /* Map an ELF section header flag to its corresponding string. */
13800 flagword flag_value;
13801 } elf_flags_to_name_table;
13803 static elf_flags_to_name_table elf_flags_to_names [] =
13805 { "SHF_WRITE", SHF_WRITE },
13806 { "SHF_ALLOC", SHF_ALLOC },
13807 { "SHF_EXECINSTR", SHF_EXECINSTR },
13808 { "SHF_MERGE", SHF_MERGE },
13809 { "SHF_STRINGS", SHF_STRINGS },
13810 { "SHF_INFO_LINK", SHF_INFO_LINK},
13811 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13812 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13813 { "SHF_GROUP", SHF_GROUP },
13814 { "SHF_TLS", SHF_TLS },
13815 { "SHF_MASKOS", SHF_MASKOS },
13816 { "SHF_EXCLUDE", SHF_EXCLUDE },
13819 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13821 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13822 struct flag_info *flaginfo,
13825 const bfd_vma sh_flags = elf_section_flags (section);
13827 if (!flaginfo->flags_initialized)
13829 bfd *obfd = info->output_bfd;
13830 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13831 struct flag_info_list *tf = flaginfo->flag_list;
13833 int without_hex = 0;
13835 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13838 flagword (*lookup) (char *);
13840 lookup = bed->elf_backend_lookup_section_flags_hook;
13841 if (lookup != NULL)
13843 flagword hexval = (*lookup) ((char *) tf->name);
13847 if (tf->with == with_flags)
13848 with_hex |= hexval;
13849 else if (tf->with == without_flags)
13850 without_hex |= hexval;
13855 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13857 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13859 if (tf->with == with_flags)
13860 with_hex |= elf_flags_to_names[i].flag_value;
13861 else if (tf->with == without_flags)
13862 without_hex |= elf_flags_to_names[i].flag_value;
13869 info->callbacks->einfo
13870 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13874 flaginfo->flags_initialized = TRUE;
13875 flaginfo->only_with_flags |= with_hex;
13876 flaginfo->not_with_flags |= without_hex;
13879 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13882 if ((flaginfo->not_with_flags & sh_flags) != 0)
13888 struct alloc_got_off_arg {
13890 struct bfd_link_info *info;
13893 /* We need a special top-level link routine to convert got reference counts
13894 to real got offsets. */
13897 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13899 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13900 bfd *obfd = gofarg->info->output_bfd;
13901 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13903 if (h->got.refcount > 0)
13905 h->got.offset = gofarg->gotoff;
13906 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13909 h->got.offset = (bfd_vma) -1;
13914 /* And an accompanying bit to work out final got entry offsets once
13915 we're done. Should be called from final_link. */
13918 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13919 struct bfd_link_info *info)
13922 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13924 struct alloc_got_off_arg gofarg;
13926 BFD_ASSERT (abfd == info->output_bfd);
13928 if (! is_elf_hash_table (info->hash))
13931 /* The GOT offset is relative to the .got section, but the GOT header is
13932 put into the .got.plt section, if the backend uses it. */
13933 if (bed->want_got_plt)
13936 gotoff = bed->got_header_size;
13938 /* Do the local .got entries first. */
13939 for (i = info->input_bfds; i; i = i->link.next)
13941 bfd_signed_vma *local_got;
13942 size_t j, locsymcount;
13943 Elf_Internal_Shdr *symtab_hdr;
13945 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13948 local_got = elf_local_got_refcounts (i);
13952 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13953 if (elf_bad_symtab (i))
13954 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13956 locsymcount = symtab_hdr->sh_info;
13958 for (j = 0; j < locsymcount; ++j)
13960 if (local_got[j] > 0)
13962 local_got[j] = gotoff;
13963 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13966 local_got[j] = (bfd_vma) -1;
13970 /* Then the global .got entries. .plt refcounts are handled by
13971 adjust_dynamic_symbol */
13972 gofarg.gotoff = gotoff;
13973 gofarg.info = info;
13974 elf_link_hash_traverse (elf_hash_table (info),
13975 elf_gc_allocate_got_offsets,
13980 /* Many folk need no more in the way of final link than this, once
13981 got entry reference counting is enabled. */
13984 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13986 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13989 /* Invoke the regular ELF backend linker to do all the work. */
13990 return bfd_elf_final_link (abfd, info);
13994 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13996 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13998 if (rcookie->bad_symtab)
13999 rcookie->rel = rcookie->rels;
14001 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14003 unsigned long r_symndx;
14005 if (! rcookie->bad_symtab)
14006 if (rcookie->rel->r_offset > offset)
14008 if (rcookie->rel->r_offset != offset)
14011 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14012 if (r_symndx == STN_UNDEF)
14015 if (r_symndx >= rcookie->locsymcount
14016 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14018 struct elf_link_hash_entry *h;
14020 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14022 while (h->root.type == bfd_link_hash_indirect
14023 || h->root.type == bfd_link_hash_warning)
14024 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14026 if ((h->root.type == bfd_link_hash_defined
14027 || h->root.type == bfd_link_hash_defweak)
14028 && (h->root.u.def.section->owner != rcookie->abfd
14029 || h->root.u.def.section->kept_section != NULL
14030 || discarded_section (h->root.u.def.section)))
14035 /* It's not a relocation against a global symbol,
14036 but it could be a relocation against a local
14037 symbol for a discarded section. */
14039 Elf_Internal_Sym *isym;
14041 /* Need to: get the symbol; get the section. */
14042 isym = &rcookie->locsyms[r_symndx];
14043 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14045 && (isec->kept_section != NULL
14046 || discarded_section (isec)))
14054 /* Discard unneeded references to discarded sections.
14055 Returns -1 on error, 1 if any section's size was changed, 0 if
14056 nothing changed. This function assumes that the relocations are in
14057 sorted order, which is true for all known assemblers. */
14060 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14062 struct elf_reloc_cookie cookie;
14067 if (info->traditional_format
14068 || !is_elf_hash_table (info->hash))
14071 o = bfd_get_section_by_name (output_bfd, ".stab");
14076 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14079 || i->reloc_count == 0
14080 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14084 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14087 if (!init_reloc_cookie_for_section (&cookie, info, i))
14090 if (_bfd_discard_section_stabs (abfd, i,
14091 elf_section_data (i)->sec_info,
14092 bfd_elf_reloc_symbol_deleted_p,
14096 fini_reloc_cookie_for_section (&cookie, i);
14101 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14102 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14106 int eh_changed = 0;
14107 unsigned int eh_alignment;
14109 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14115 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14118 if (!init_reloc_cookie_for_section (&cookie, info, i))
14121 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14122 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14123 bfd_elf_reloc_symbol_deleted_p,
14127 if (i->size != i->rawsize)
14131 fini_reloc_cookie_for_section (&cookie, i);
14134 eh_alignment = 1 << o->alignment_power;
14135 /* Skip over zero terminator, and prevent empty sections from
14136 adding alignment padding at the end. */
14137 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14139 i->flags |= SEC_EXCLUDE;
14140 else if (i->size > 4)
14142 /* The last non-empty eh_frame section doesn't need padding. */
14145 /* Any prior sections must pad the last FDE out to the output
14146 section alignment. Otherwise we might have zero padding
14147 between sections, which would be seen as a terminator. */
14148 for (; i != NULL; i = i->map_tail.s)
14150 /* All but the last zero terminator should have been removed. */
14155 = (i->size + eh_alignment - 1) & -eh_alignment;
14156 if (i->size != size)
14164 elf_link_hash_traverse (elf_hash_table (info),
14165 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14168 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14170 const struct elf_backend_data *bed;
14173 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14175 s = abfd->sections;
14176 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14179 bed = get_elf_backend_data (abfd);
14181 if (bed->elf_backend_discard_info != NULL)
14183 if (!init_reloc_cookie (&cookie, info, abfd))
14186 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14189 fini_reloc_cookie (&cookie, abfd);
14193 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14194 _bfd_elf_end_eh_frame_parsing (info);
14196 if (info->eh_frame_hdr_type
14197 && !bfd_link_relocatable (info)
14198 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14205 _bfd_elf_section_already_linked (bfd *abfd,
14207 struct bfd_link_info *info)
14210 const char *name, *key;
14211 struct bfd_section_already_linked *l;
14212 struct bfd_section_already_linked_hash_entry *already_linked_list;
14214 if (sec->output_section == bfd_abs_section_ptr)
14217 flags = sec->flags;
14219 /* Return if it isn't a linkonce section. A comdat group section
14220 also has SEC_LINK_ONCE set. */
14221 if ((flags & SEC_LINK_ONCE) == 0)
14224 /* Don't put group member sections on our list of already linked
14225 sections. They are handled as a group via their group section. */
14226 if (elf_sec_group (sec) != NULL)
14229 /* For a SHT_GROUP section, use the group signature as the key. */
14231 if ((flags & SEC_GROUP) != 0
14232 && elf_next_in_group (sec) != NULL
14233 && elf_group_name (elf_next_in_group (sec)) != NULL)
14234 key = elf_group_name (elf_next_in_group (sec));
14237 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14238 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14239 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14242 /* Must be a user linkonce section that doesn't follow gcc's
14243 naming convention. In this case we won't be matching
14244 single member groups. */
14248 already_linked_list = bfd_section_already_linked_table_lookup (key);
14250 for (l = already_linked_list->entry; l != NULL; l = l->next)
14252 /* We may have 2 different types of sections on the list: group
14253 sections with a signature of <key> (<key> is some string),
14254 and linkonce sections named .gnu.linkonce.<type>.<key>.
14255 Match like sections. LTO plugin sections are an exception.
14256 They are always named .gnu.linkonce.t.<key> and match either
14257 type of section. */
14258 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14259 && ((flags & SEC_GROUP) != 0
14260 || strcmp (name, l->sec->name) == 0))
14261 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14263 /* The section has already been linked. See if we should
14264 issue a warning. */
14265 if (!_bfd_handle_already_linked (sec, l, info))
14268 if (flags & SEC_GROUP)
14270 asection *first = elf_next_in_group (sec);
14271 asection *s = first;
14275 s->output_section = bfd_abs_section_ptr;
14276 /* Record which group discards it. */
14277 s->kept_section = l->sec;
14278 s = elf_next_in_group (s);
14279 /* These lists are circular. */
14289 /* A single member comdat group section may be discarded by a
14290 linkonce section and vice versa. */
14291 if ((flags & SEC_GROUP) != 0)
14293 asection *first = elf_next_in_group (sec);
14295 if (first != NULL && elf_next_in_group (first) == first)
14296 /* Check this single member group against linkonce sections. */
14297 for (l = already_linked_list->entry; l != NULL; l = l->next)
14298 if ((l->sec->flags & SEC_GROUP) == 0
14299 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14301 first->output_section = bfd_abs_section_ptr;
14302 first->kept_section = l->sec;
14303 sec->output_section = bfd_abs_section_ptr;
14308 /* Check this linkonce section against single member groups. */
14309 for (l = already_linked_list->entry; l != NULL; l = l->next)
14310 if (l->sec->flags & SEC_GROUP)
14312 asection *first = elf_next_in_group (l->sec);
14315 && elf_next_in_group (first) == first
14316 && bfd_elf_match_symbols_in_sections (first, sec, info))
14318 sec->output_section = bfd_abs_section_ptr;
14319 sec->kept_section = first;
14324 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14325 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14326 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14327 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14328 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14329 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14330 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14331 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14332 The reverse order cannot happen as there is never a bfd with only the
14333 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14334 matter as here were are looking only for cross-bfd sections. */
14336 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14337 for (l = already_linked_list->entry; l != NULL; l = l->next)
14338 if ((l->sec->flags & SEC_GROUP) == 0
14339 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14341 if (abfd != l->sec->owner)
14342 sec->output_section = bfd_abs_section_ptr;
14346 /* This is the first section with this name. Record it. */
14347 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14348 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14349 return sec->output_section == bfd_abs_section_ptr;
14353 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14355 return sym->st_shndx == SHN_COMMON;
14359 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14365 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14367 return bfd_com_section_ptr;
14371 _bfd_elf_default_got_elt_size (bfd *abfd,
14372 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14373 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14374 bfd *ibfd ATTRIBUTE_UNUSED,
14375 unsigned long symndx ATTRIBUTE_UNUSED)
14377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14378 return bed->s->arch_size / 8;
14381 /* Routines to support the creation of dynamic relocs. */
14383 /* Returns the name of the dynamic reloc section associated with SEC. */
14385 static const char *
14386 get_dynamic_reloc_section_name (bfd * abfd,
14388 bfd_boolean is_rela)
14391 const char *old_name = bfd_get_section_name (NULL, sec);
14392 const char *prefix = is_rela ? ".rela" : ".rel";
14394 if (old_name == NULL)
14397 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14398 sprintf (name, "%s%s", prefix, old_name);
14403 /* Returns the dynamic reloc section associated with SEC.
14404 If necessary compute the name of the dynamic reloc section based
14405 on SEC's name (looked up in ABFD's string table) and the setting
14409 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14411 bfd_boolean is_rela)
14413 asection * reloc_sec = elf_section_data (sec)->sreloc;
14415 if (reloc_sec == NULL)
14417 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14421 reloc_sec = bfd_get_linker_section (abfd, name);
14423 if (reloc_sec != NULL)
14424 elf_section_data (sec)->sreloc = reloc_sec;
14431 /* Returns the dynamic reloc section associated with SEC. If the
14432 section does not exist it is created and attached to the DYNOBJ
14433 bfd and stored in the SRELOC field of SEC's elf_section_data
14436 ALIGNMENT is the alignment for the newly created section and
14437 IS_RELA defines whether the name should be .rela.<SEC's name>
14438 or .rel.<SEC's name>. The section name is looked up in the
14439 string table associated with ABFD. */
14442 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14444 unsigned int alignment,
14446 bfd_boolean is_rela)
14448 asection * reloc_sec = elf_section_data (sec)->sreloc;
14450 if (reloc_sec == NULL)
14452 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14457 reloc_sec = bfd_get_linker_section (dynobj, name);
14459 if (reloc_sec == NULL)
14461 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14462 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14463 if ((sec->flags & SEC_ALLOC) != 0)
14464 flags |= SEC_ALLOC | SEC_LOAD;
14466 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14467 if (reloc_sec != NULL)
14469 /* _bfd_elf_get_sec_type_attr chooses a section type by
14470 name. Override as it may be wrong, eg. for a user
14471 section named "auto" we'll get ".relauto" which is
14472 seen to be a .rela section. */
14473 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14474 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14479 elf_section_data (sec)->sreloc = reloc_sec;
14485 /* Copy the ELF symbol type and other attributes for a linker script
14486 assignment from HSRC to HDEST. Generally this should be treated as
14487 if we found a strong non-dynamic definition for HDEST (except that
14488 ld ignores multiple definition errors). */
14490 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14491 struct bfd_link_hash_entry *hdest,
14492 struct bfd_link_hash_entry *hsrc)
14494 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14495 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14496 Elf_Internal_Sym isym;
14498 ehdest->type = ehsrc->type;
14499 ehdest->target_internal = ehsrc->target_internal;
14501 isym.st_other = ehsrc->other;
14502 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14505 /* Append a RELA relocation REL to section S in BFD. */
14508 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14510 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14511 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14512 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14513 bed->s->swap_reloca_out (abfd, rel, loc);
14516 /* Append a REL relocation REL to section S in BFD. */
14519 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14521 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14522 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14523 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14524 bed->s->swap_reloc_out (abfd, rel, loc);
14527 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14529 struct bfd_link_hash_entry *
14530 bfd_elf_define_start_stop (struct bfd_link_info *info,
14531 const char *symbol, asection *sec)
14533 struct elf_link_hash_entry *h;
14535 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14536 FALSE, FALSE, TRUE);
14538 && (h->root.type == bfd_link_hash_undefined
14539 || h->root.type == bfd_link_hash_undefweak
14540 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14542 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14543 h->root.type = bfd_link_hash_defined;
14544 h->root.u.def.section = sec;
14545 h->root.u.def.value = 0;
14546 h->def_regular = 1;
14547 h->def_dynamic = 0;
14549 h->u2.start_stop_section = sec;
14550 if (symbol[0] == '.')
14552 /* .startof. and .sizeof. symbols are local. */
14553 const struct elf_backend_data *bed;
14554 bed = get_elf_backend_data (info->output_bfd);
14555 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14559 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14560 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14562 bfd_elf_link_record_dynamic_symbol (info, h);