1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
96 && discard ? discarded_section (isec) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
227 && !((s = ibfd->sections) != NULL
228 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
234 hash_table->dynobj = abfd;
237 if (hash_table->dynstr == NULL)
239 hash_table->dynstr = _bfd_elf_strtab_init ();
240 if (hash_table->dynstr == NULL)
246 /* Create some sections which will be filled in with dynamic linking
247 information. ABFD is an input file which requires dynamic sections
248 to be created. The dynamic sections take up virtual memory space
249 when the final executable is run, so we need to create them before
250 addresses are assigned to the output sections. We work out the
251 actual contents and size of these sections later. */
254 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
258 const struct elf_backend_data *bed;
259 struct elf_link_hash_entry *h;
261 if (! is_elf_hash_table (info->hash))
264 if (elf_hash_table (info)->dynamic_sections_created)
267 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
270 abfd = elf_hash_table (info)->dynobj;
271 bed = get_elf_backend_data (abfd);
273 flags = bed->dynamic_sec_flags;
275 /* A dynamically linked executable has a .interp section, but a
276 shared library does not. */
277 if (bfd_link_executable (info) && !info->nointerp)
279 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
280 flags | SEC_READONLY);
285 /* Create sections to hold version informations. These are removed
286 if they are not needed. */
287 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
288 flags | SEC_READONLY);
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
293 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
294 flags | SEC_READONLY);
296 || ! bfd_set_section_alignment (abfd, s, 1))
299 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
300 flags | SEC_READONLY);
302 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
305 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
306 flags | SEC_READONLY);
308 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
310 elf_hash_table (info)->dynsym = s;
312 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
313 flags | SEC_READONLY);
317 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
319 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
322 /* The special symbol _DYNAMIC is always set to the start of the
323 .dynamic section. We could set _DYNAMIC in a linker script, but we
324 only want to define it if we are, in fact, creating a .dynamic
325 section. We don't want to define it if there is no .dynamic
326 section, since on some ELF platforms the start up code examines it
327 to decide how to initialize the process. */
328 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
329 elf_hash_table (info)->hdynamic = h;
335 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
336 flags | SEC_READONLY);
338 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
340 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
343 if (info->emit_gnu_hash)
345 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
346 flags | SEC_READONLY);
348 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
350 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
351 4 32-bit words followed by variable count of 64-bit words, then
352 variable count of 32-bit words. */
353 if (bed->s->arch_size == 64)
354 elf_section_data (s)->this_hdr.sh_entsize = 0;
356 elf_section_data (s)->this_hdr.sh_entsize = 4;
359 /* Let the backend create the rest of the sections. This lets the
360 backend set the right flags. The backend will normally create
361 the .got and .plt sections. */
362 if (bed->elf_backend_create_dynamic_sections == NULL
363 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
366 elf_hash_table (info)->dynamic_sections_created = TRUE;
371 /* Create dynamic sections when linking against a dynamic object. */
374 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
376 flagword flags, pltflags;
377 struct elf_link_hash_entry *h;
379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
380 struct elf_link_hash_table *htab = elf_hash_table (info);
382 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
383 .rel[a].bss sections. */
384 flags = bed->dynamic_sec_flags;
387 if (bed->plt_not_loaded)
388 /* We do not clear SEC_ALLOC here because we still want the OS to
389 allocate space for the section; it's just that there's nothing
390 to read in from the object file. */
391 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
393 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
394 if (bed->plt_readonly)
395 pltflags |= SEC_READONLY;
397 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
399 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
403 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
405 if (bed->want_plt_sym)
407 h = _bfd_elf_define_linkage_sym (abfd, info, s,
408 "_PROCEDURE_LINKAGE_TABLE_");
409 elf_hash_table (info)->hplt = h;
414 s = bfd_make_section_anyway_with_flags (abfd,
415 (bed->rela_plts_and_copies_p
416 ? ".rela.plt" : ".rel.plt"),
417 flags | SEC_READONLY);
419 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
423 if (! _bfd_elf_create_got_section (abfd, info))
426 if (bed->want_dynbss)
428 /* The .dynbss section is a place to put symbols which are defined
429 by dynamic objects, are referenced by regular objects, and are
430 not functions. We must allocate space for them in the process
431 image and use a R_*_COPY reloc to tell the dynamic linker to
432 initialize them at run time. The linker script puts the .dynbss
433 section into the .bss section of the final image. */
434 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
435 SEC_ALLOC | SEC_LINKER_CREATED);
440 if (bed->want_dynrelro)
442 /* Similarly, but for symbols that were originally in read-only
443 sections. This section doesn't really need to have contents,
444 but make it like other .data.rel.ro sections. */
445 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
452 /* The .rel[a].bss section holds copy relocs. This section is not
453 normally needed. We need to create it here, though, so that the
454 linker will map it to an output section. We can't just create it
455 only if we need it, because we will not know whether we need it
456 until we have seen all the input files, and the first time the
457 main linker code calls BFD after examining all the input files
458 (size_dynamic_sections) the input sections have already been
459 mapped to the output sections. If the section turns out not to
460 be needed, we can discard it later. We will never need this
461 section when generating a shared object, since they do not use
463 if (bfd_link_executable (info))
465 s = bfd_make_section_anyway_with_flags (abfd,
466 (bed->rela_plts_and_copies_p
467 ? ".rela.bss" : ".rel.bss"),
468 flags | SEC_READONLY);
470 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
474 if (bed->want_dynrelro)
476 s = (bfd_make_section_anyway_with_flags
477 (abfd, (bed->rela_plts_and_copies_p
478 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
479 flags | SEC_READONLY));
481 || ! bfd_set_section_alignment (abfd, s,
482 bed->s->log_file_align))
484 htab->sreldynrelro = s;
492 /* Record a new dynamic symbol. We record the dynamic symbols as we
493 read the input files, since we need to have a list of all of them
494 before we can determine the final sizes of the output sections.
495 Note that we may actually call this function even though we are not
496 going to output any dynamic symbols; in some cases we know that a
497 symbol should be in the dynamic symbol table, but only if there is
501 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
502 struct elf_link_hash_entry *h)
504 if (h->dynindx == -1)
506 struct elf_strtab_hash *dynstr;
511 /* XXX: The ABI draft says the linker must turn hidden and
512 internal symbols into STB_LOCAL symbols when producing the
513 DSO. However, if ld.so honors st_other in the dynamic table,
514 this would not be necessary. */
515 switch (ELF_ST_VISIBILITY (h->other))
519 if (h->root.type != bfd_link_hash_undefined
520 && h->root.type != bfd_link_hash_undefweak)
523 if (!elf_hash_table (info)->is_relocatable_executable)
531 h->dynindx = elf_hash_table (info)->dynsymcount;
532 ++elf_hash_table (info)->dynsymcount;
534 dynstr = elf_hash_table (info)->dynstr;
537 /* Create a strtab to hold the dynamic symbol names. */
538 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
543 /* We don't put any version information in the dynamic string
545 name = h->root.root.string;
546 p = strchr (name, ELF_VER_CHR);
548 /* We know that the p points into writable memory. In fact,
549 there are only a few symbols that have read-only names, being
550 those like _GLOBAL_OFFSET_TABLE_ that are created specially
551 by the backends. Most symbols will have names pointing into
552 an ELF string table read from a file, or to objalloc memory. */
555 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
560 if (indx == (size_t) -1)
562 h->dynstr_index = indx;
568 /* Mark a symbol dynamic. */
571 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
572 struct elf_link_hash_entry *h,
573 Elf_Internal_Sym *sym)
575 struct bfd_elf_dynamic_list *d = info->dynamic_list;
577 /* It may be called more than once on the same H. */
578 if(h->dynamic || bfd_link_relocatable (info))
581 if ((info->dynamic_data
582 && (h->type == STT_OBJECT
583 || h->type == STT_COMMON
585 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
586 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
589 && (*d->match) (&d->head, NULL, h->root.root.string)))
592 /* NB: If a symbol is made dynamic by --dynamic-list, it has
594 h->root.non_ir_ref_dynamic = 1;
598 /* Record an assignment to a symbol made by a linker script. We need
599 this in case some dynamic object refers to this symbol. */
602 bfd_elf_record_link_assignment (bfd *output_bfd,
603 struct bfd_link_info *info,
608 struct elf_link_hash_entry *h, *hv;
609 struct elf_link_hash_table *htab;
610 const struct elf_backend_data *bed;
612 if (!is_elf_hash_table (info->hash))
615 htab = elf_hash_table (info);
616 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
620 if (h->root.type == bfd_link_hash_warning)
621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
623 if (h->versioned == unknown)
625 /* Set versioned if symbol version is unknown. */
626 char *version = strrchr (name, ELF_VER_CHR);
629 if (version > name && version[-1] != ELF_VER_CHR)
630 h->versioned = versioned_hidden;
632 h->versioned = versioned;
636 /* Symbols defined in a linker script but not referenced anywhere
637 else will have non_elf set. */
640 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
644 switch (h->root.type)
646 case bfd_link_hash_defined:
647 case bfd_link_hash_defweak:
648 case bfd_link_hash_common:
650 case bfd_link_hash_undefweak:
651 case bfd_link_hash_undefined:
652 /* Since we're defining the symbol, don't let it seem to have not
653 been defined. record_dynamic_symbol and size_dynamic_sections
654 may depend on this. */
655 h->root.type = bfd_link_hash_new;
656 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
657 bfd_link_repair_undef_list (&htab->root);
659 case bfd_link_hash_new:
661 case bfd_link_hash_indirect:
662 /* We had a versioned symbol in a dynamic library. We make the
663 the versioned symbol point to this one. */
664 bed = get_elf_backend_data (output_bfd);
666 while (hv->root.type == bfd_link_hash_indirect
667 || hv->root.type == bfd_link_hash_warning)
668 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
669 /* We don't need to update h->root.u since linker will set them
671 h->root.type = bfd_link_hash_undefined;
672 hv->root.type = bfd_link_hash_indirect;
673 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
674 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
681 /* If this symbol is being provided by the linker script, and it is
682 currently defined by a dynamic object, but not by a regular
683 object, then mark it as undefined so that the generic linker will
684 force the correct value. */
688 h->root.type = bfd_link_hash_undefined;
690 /* If this symbol is currently defined by a dynamic object, but not
691 by a regular object, then clear out any version information because
692 the symbol will not be associated with the dynamic object any
694 if (h->def_dynamic && !h->def_regular)
695 h->verinfo.verdef = NULL;
697 /* Make sure this symbol is not garbage collected. */
704 bed = get_elf_backend_data (output_bfd);
705 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
706 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
707 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
710 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
712 if (!bfd_link_relocatable (info)
714 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
715 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
720 || bfd_link_dll (info)
721 || elf_hash_table (info)->is_relocatable_executable)
725 if (! bfd_elf_link_record_dynamic_symbol (info, h))
728 /* If this is a weak defined symbol, and we know a corresponding
729 real symbol from the same dynamic object, make sure the real
730 symbol is also made into a dynamic symbol. */
733 struct elf_link_hash_entry *def = weakdef (h);
735 if (def->dynindx == -1
736 && !bfd_elf_link_record_dynamic_symbol (info, def))
744 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
745 success, and 2 on a failure caused by attempting to record a symbol
746 in a discarded section, eg. a discarded link-once section symbol. */
749 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
754 struct elf_link_local_dynamic_entry *entry;
755 struct elf_link_hash_table *eht;
756 struct elf_strtab_hash *dynstr;
759 Elf_External_Sym_Shndx eshndx;
760 char esym[sizeof (Elf64_External_Sym)];
762 if (! is_elf_hash_table (info->hash))
765 /* See if the entry exists already. */
766 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
767 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
770 amt = sizeof (*entry);
771 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
775 /* Go find the symbol, so that we can find it's name. */
776 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
777 1, input_indx, &entry->isym, esym, &eshndx))
779 bfd_release (input_bfd, entry);
783 if (entry->isym.st_shndx != SHN_UNDEF
784 && entry->isym.st_shndx < SHN_LORESERVE)
788 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
789 if (s == NULL || bfd_is_abs_section (s->output_section))
791 /* We can still bfd_release here as nothing has done another
792 bfd_alloc. We can't do this later in this function. */
793 bfd_release (input_bfd, entry);
798 name = (bfd_elf_string_from_elf_section
799 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
800 entry->isym.st_name));
802 dynstr = elf_hash_table (info)->dynstr;
805 /* Create a strtab to hold the dynamic symbol names. */
806 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
811 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
812 if (dynstr_index == (size_t) -1)
814 entry->isym.st_name = dynstr_index;
816 eht = elf_hash_table (info);
818 entry->next = eht->dynlocal;
819 eht->dynlocal = entry;
820 entry->input_bfd = input_bfd;
821 entry->input_indx = input_indx;
824 /* Whatever binding the symbol had before, it's now local. */
826 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
828 /* The dynindx will be set at the end of size_dynamic_sections. */
833 /* Return the dynindex of a local dynamic symbol. */
836 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
840 struct elf_link_local_dynamic_entry *e;
842 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
843 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
848 /* This function is used to renumber the dynamic symbols, if some of
849 them are removed because they are marked as local. This is called
850 via elf_link_hash_traverse. */
853 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
856 size_t *count = (size_t *) data;
861 if (h->dynindx != -1)
862 h->dynindx = ++(*count);
868 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
869 STB_LOCAL binding. */
872 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
875 size_t *count = (size_t *) data;
877 if (!h->forced_local)
880 if (h->dynindx != -1)
881 h->dynindx = ++(*count);
886 /* Return true if the dynamic symbol for a given section should be
887 omitted when creating a shared library. */
889 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
890 struct bfd_link_info *info,
893 struct elf_link_hash_table *htab;
896 switch (elf_section_data (p)->this_hdr.sh_type)
900 /* If sh_type is yet undecided, assume it could be
901 SHT_PROGBITS/SHT_NOBITS. */
903 htab = elf_hash_table (info);
904 if (p == htab->tls_sec)
907 if (htab->text_index_section != NULL)
908 return p != htab->text_index_section && p != htab->data_index_section;
910 return (htab->dynobj != NULL
911 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
912 && ip->output_section == p);
914 /* There shouldn't be section relative relocations
915 against any other section. */
922 _bfd_elf_omit_section_dynsym_all
923 (bfd *output_bfd ATTRIBUTE_UNUSED,
924 struct bfd_link_info *info ATTRIBUTE_UNUSED,
925 asection *p ATTRIBUTE_UNUSED)
930 /* Assign dynsym indices. In a shared library we generate a section
931 symbol for each output section, which come first. Next come symbols
932 which have been forced to local binding. Then all of the back-end
933 allocated local dynamic syms, followed by the rest of the global
934 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
935 (This prevents the early call before elf_backend_init_index_section
936 and strip_excluded_output_sections setting dynindx for sections
937 that are stripped.) */
940 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
941 struct bfd_link_info *info,
942 unsigned long *section_sym_count)
944 unsigned long dynsymcount = 0;
945 bfd_boolean do_sec = section_sym_count != NULL;
947 if (bfd_link_pic (info)
948 || elf_hash_table (info)->is_relocatable_executable)
950 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
952 for (p = output_bfd->sections; p ; p = p->next)
953 if ((p->flags & SEC_EXCLUDE) == 0
954 && (p->flags & SEC_ALLOC) != 0
955 && elf_hash_table (info)->dynamic_relocs
956 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
960 elf_section_data (p)->dynindx = dynsymcount;
963 elf_section_data (p)->dynindx = 0;
966 *section_sym_count = dynsymcount;
968 elf_link_hash_traverse (elf_hash_table (info),
969 elf_link_renumber_local_hash_table_dynsyms,
972 if (elf_hash_table (info)->dynlocal)
974 struct elf_link_local_dynamic_entry *p;
975 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
976 p->dynindx = ++dynsymcount;
978 elf_hash_table (info)->local_dynsymcount = dynsymcount;
980 elf_link_hash_traverse (elf_hash_table (info),
981 elf_link_renumber_hash_table_dynsyms,
984 /* There is an unused NULL entry at the head of the table which we
985 must account for in our count even if the table is empty since it
986 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
990 elf_hash_table (info)->dynsymcount = dynsymcount;
994 /* Merge st_other field. */
997 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
998 const Elf_Internal_Sym *isym, asection *sec,
999 bfd_boolean definition, bfd_boolean dynamic)
1001 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1003 /* If st_other has a processor-specific meaning, specific
1004 code might be needed here. */
1005 if (bed->elf_backend_merge_symbol_attribute)
1006 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1011 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1012 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1014 /* Keep the most constraining visibility. Leave the remainder
1015 of the st_other field to elf_backend_merge_symbol_attribute. */
1016 if (symvis - 1 < hvis - 1)
1017 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1020 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1021 && (sec->flags & SEC_READONLY) == 0)
1022 h->protected_def = 1;
1025 /* This function is called when we want to merge a new symbol with an
1026 existing symbol. It handles the various cases which arise when we
1027 find a definition in a dynamic object, or when there is already a
1028 definition in a dynamic object. The new symbol is described by
1029 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1030 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1031 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1032 of an old common symbol. We set OVERRIDE if the old symbol is
1033 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1034 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1035 to change. By OK to change, we mean that we shouldn't warn if the
1036 type or size does change. */
1039 _bfd_elf_merge_symbol (bfd *abfd,
1040 struct bfd_link_info *info,
1042 Elf_Internal_Sym *sym,
1045 struct elf_link_hash_entry **sym_hash,
1047 bfd_boolean *pold_weak,
1048 unsigned int *pold_alignment,
1050 bfd_boolean *override,
1051 bfd_boolean *type_change_ok,
1052 bfd_boolean *size_change_ok,
1053 bfd_boolean *matched)
1055 asection *sec, *oldsec;
1056 struct elf_link_hash_entry *h;
1057 struct elf_link_hash_entry *hi;
1058 struct elf_link_hash_entry *flip;
1061 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1062 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1063 const struct elf_backend_data *bed;
1065 bfd_boolean default_sym = *matched;
1071 bind = ELF_ST_BIND (sym->st_info);
1073 if (! bfd_is_und_section (sec))
1074 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1076 h = ((struct elf_link_hash_entry *)
1077 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1082 bed = get_elf_backend_data (abfd);
1084 /* NEW_VERSION is the symbol version of the new symbol. */
1085 if (h->versioned != unversioned)
1087 /* Symbol version is unknown or versioned. */
1088 new_version = strrchr (name, ELF_VER_CHR);
1091 if (h->versioned == unknown)
1093 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1094 h->versioned = versioned_hidden;
1096 h->versioned = versioned;
1099 if (new_version[0] == '\0')
1103 h->versioned = unversioned;
1108 /* For merging, we only care about real symbols. But we need to make
1109 sure that indirect symbol dynamic flags are updated. */
1111 while (h->root.type == bfd_link_hash_indirect
1112 || h->root.type == bfd_link_hash_warning)
1113 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1117 if (hi == h || h->root.type == bfd_link_hash_new)
1121 /* OLD_HIDDEN is true if the existing symbol is only visible
1122 to the symbol with the same symbol version. NEW_HIDDEN is
1123 true if the new symbol is only visible to the symbol with
1124 the same symbol version. */
1125 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1126 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1127 if (!old_hidden && !new_hidden)
1128 /* The new symbol matches the existing symbol if both
1133 /* OLD_VERSION is the symbol version of the existing
1137 if (h->versioned >= versioned)
1138 old_version = strrchr (h->root.root.string,
1143 /* The new symbol matches the existing symbol if they
1144 have the same symbol version. */
1145 *matched = (old_version == new_version
1146 || (old_version != NULL
1147 && new_version != NULL
1148 && strcmp (old_version, new_version) == 0));
1153 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1158 switch (h->root.type)
1163 case bfd_link_hash_undefined:
1164 case bfd_link_hash_undefweak:
1165 oldbfd = h->root.u.undef.abfd;
1168 case bfd_link_hash_defined:
1169 case bfd_link_hash_defweak:
1170 oldbfd = h->root.u.def.section->owner;
1171 oldsec = h->root.u.def.section;
1174 case bfd_link_hash_common:
1175 oldbfd = h->root.u.c.p->section->owner;
1176 oldsec = h->root.u.c.p->section;
1178 *pold_alignment = h->root.u.c.p->alignment_power;
1181 if (poldbfd && *poldbfd == NULL)
1184 /* Differentiate strong and weak symbols. */
1185 newweak = bind == STB_WEAK;
1186 oldweak = (h->root.type == bfd_link_hash_defweak
1187 || h->root.type == bfd_link_hash_undefweak);
1189 *pold_weak = oldweak;
1191 /* We have to check it for every instance since the first few may be
1192 references and not all compilers emit symbol type for undefined
1194 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1196 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1197 respectively, is from a dynamic object. */
1199 newdyn = (abfd->flags & DYNAMIC) != 0;
1201 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1202 syms and defined syms in dynamic libraries respectively.
1203 ref_dynamic on the other hand can be set for a symbol defined in
1204 a dynamic library, and def_dynamic may not be set; When the
1205 definition in a dynamic lib is overridden by a definition in the
1206 executable use of the symbol in the dynamic lib becomes a
1207 reference to the executable symbol. */
1210 if (bfd_is_und_section (sec))
1212 if (bind != STB_WEAK)
1214 h->ref_dynamic_nonweak = 1;
1215 hi->ref_dynamic_nonweak = 1;
1220 /* Update the existing symbol only if they match. */
1223 hi->dynamic_def = 1;
1227 /* If we just created the symbol, mark it as being an ELF symbol.
1228 Other than that, there is nothing to do--there is no merge issue
1229 with a newly defined symbol--so we just return. */
1231 if (h->root.type == bfd_link_hash_new)
1237 /* In cases involving weak versioned symbols, we may wind up trying
1238 to merge a symbol with itself. Catch that here, to avoid the
1239 confusion that results if we try to override a symbol with
1240 itself. The additional tests catch cases like
1241 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1242 dynamic object, which we do want to handle here. */
1244 && (newweak || oldweak)
1245 && ((abfd->flags & DYNAMIC) == 0
1246 || !h->def_regular))
1251 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1252 else if (oldsec != NULL)
1254 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1255 indices used by MIPS ELF. */
1256 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1259 /* Handle a case where plugin_notice won't be called and thus won't
1260 set the non_ir_ref flags on the first pass over symbols. */
1262 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1263 && newdyn != olddyn)
1265 h->root.non_ir_ref_dynamic = TRUE;
1266 hi->root.non_ir_ref_dynamic = TRUE;
1269 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1270 respectively, appear to be a definition rather than reference. */
1272 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1274 olddef = (h->root.type != bfd_link_hash_undefined
1275 && h->root.type != bfd_link_hash_undefweak
1276 && h->root.type != bfd_link_hash_common);
1278 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1279 respectively, appear to be a function. */
1281 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1282 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1284 oldfunc = (h->type != STT_NOTYPE
1285 && bed->is_function_type (h->type));
1287 if (!(newfunc && oldfunc)
1288 && ELF_ST_TYPE (sym->st_info) != h->type
1289 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1290 && h->type != STT_NOTYPE
1291 && (newdef || bfd_is_com_section (sec))
1292 && (olddef || h->root.type == bfd_link_hash_common))
1294 /* If creating a default indirect symbol ("foo" or "foo@") from
1295 a dynamic versioned definition ("foo@@") skip doing so if
1296 there is an existing regular definition with a different
1297 type. We don't want, for example, a "time" variable in the
1298 executable overriding a "time" function in a shared library. */
1306 /* When adding a symbol from a regular object file after we have
1307 created indirect symbols, undo the indirection and any
1314 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1315 h->forced_local = 0;
1319 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1321 h->root.type = bfd_link_hash_undefined;
1322 h->root.u.undef.abfd = abfd;
1326 h->root.type = bfd_link_hash_new;
1327 h->root.u.undef.abfd = NULL;
1333 /* Check TLS symbols. We don't check undefined symbols introduced
1334 by "ld -u" which have no type (and oldbfd NULL), and we don't
1335 check symbols from plugins because they also have no type. */
1337 && (oldbfd->flags & BFD_PLUGIN) == 0
1338 && (abfd->flags & BFD_PLUGIN) == 0
1339 && ELF_ST_TYPE (sym->st_info) != h->type
1340 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1343 bfd_boolean ntdef, tdef;
1344 asection *ntsec, *tsec;
1346 if (h->type == STT_TLS)
1367 /* xgettext:c-format */
1368 (_("%s: TLS definition in %pB section %pA "
1369 "mismatches non-TLS definition in %pB section %pA"),
1370 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1371 else if (!tdef && !ntdef)
1373 /* xgettext:c-format */
1374 (_("%s: TLS reference in %pB "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, ntbfd);
1379 /* xgettext:c-format */
1380 (_("%s: TLS definition in %pB section %pA "
1381 "mismatches non-TLS reference in %pB"),
1382 h->root.root.string, tbfd, tsec, ntbfd);
1385 /* xgettext:c-format */
1386 (_("%s: TLS reference in %pB "
1387 "mismatches non-TLS definition in %pB section %pA"),
1388 h->root.root.string, tbfd, ntbfd, ntsec);
1390 bfd_set_error (bfd_error_bad_value);
1394 /* If the old symbol has non-default visibility, we ignore the new
1395 definition from a dynamic object. */
1397 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1398 && !bfd_is_und_section (sec))
1401 /* Make sure this symbol is dynamic. */
1403 hi->ref_dynamic = 1;
1404 /* A protected symbol has external availability. Make sure it is
1405 recorded as dynamic.
1407 FIXME: Should we check type and size for protected symbol? */
1408 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1409 return bfd_elf_link_record_dynamic_symbol (info, h);
1414 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1417 /* If the new symbol with non-default visibility comes from a
1418 relocatable file and the old definition comes from a dynamic
1419 object, we remove the old definition. */
1420 if (hi->root.type == bfd_link_hash_indirect)
1422 /* Handle the case where the old dynamic definition is
1423 default versioned. We need to copy the symbol info from
1424 the symbol with default version to the normal one if it
1425 was referenced before. */
1428 hi->root.type = h->root.type;
1429 h->root.type = bfd_link_hash_indirect;
1430 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1432 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1433 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1435 /* If the new symbol is hidden or internal, completely undo
1436 any dynamic link state. */
1437 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1438 h->forced_local = 0;
1445 /* FIXME: Should we check type and size for protected symbol? */
1455 /* If the old symbol was undefined before, then it will still be
1456 on the undefs list. If the new symbol is undefined or
1457 common, we can't make it bfd_link_hash_new here, because new
1458 undefined or common symbols will be added to the undefs list
1459 by _bfd_generic_link_add_one_symbol. Symbols may not be
1460 added twice to the undefs list. Also, if the new symbol is
1461 undefweak then we don't want to lose the strong undef. */
1462 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1464 h->root.type = bfd_link_hash_undefined;
1465 h->root.u.undef.abfd = abfd;
1469 h->root.type = bfd_link_hash_new;
1470 h->root.u.undef.abfd = NULL;
1473 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1475 /* If the new symbol is hidden or internal, completely undo
1476 any dynamic link state. */
1477 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1478 h->forced_local = 0;
1484 /* FIXME: Should we check type and size for protected symbol? */
1490 /* If a new weak symbol definition comes from a regular file and the
1491 old symbol comes from a dynamic library, we treat the new one as
1492 strong. Similarly, an old weak symbol definition from a regular
1493 file is treated as strong when the new symbol comes from a dynamic
1494 library. Further, an old weak symbol from a dynamic library is
1495 treated as strong if the new symbol is from a dynamic library.
1496 This reflects the way glibc's ld.so works.
1498 Also allow a weak symbol to override a linker script symbol
1499 defined by an early pass over the script. This is done so the
1500 linker knows the symbol is defined in an object file, for the
1501 DEFINED script function.
1503 Do this before setting *type_change_ok or *size_change_ok so that
1504 we warn properly when dynamic library symbols are overridden. */
1506 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1508 if (olddef && newdyn)
1511 /* Allow changes between different types of function symbol. */
1512 if (newfunc && oldfunc)
1513 *type_change_ok = TRUE;
1515 /* It's OK to change the type if either the existing symbol or the
1516 new symbol is weak. A type change is also OK if the old symbol
1517 is undefined and the new symbol is defined. */
1522 && h->root.type == bfd_link_hash_undefined))
1523 *type_change_ok = TRUE;
1525 /* It's OK to change the size if either the existing symbol or the
1526 new symbol is weak, or if the old symbol is undefined. */
1529 || h->root.type == bfd_link_hash_undefined)
1530 *size_change_ok = TRUE;
1532 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1533 symbol, respectively, appears to be a common symbol in a dynamic
1534 object. If a symbol appears in an uninitialized section, and is
1535 not weak, and is not a function, then it may be a common symbol
1536 which was resolved when the dynamic object was created. We want
1537 to treat such symbols specially, because they raise special
1538 considerations when setting the symbol size: if the symbol
1539 appears as a common symbol in a regular object, and the size in
1540 the regular object is larger, we must make sure that we use the
1541 larger size. This problematic case can always be avoided in C,
1542 but it must be handled correctly when using Fortran shared
1545 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1546 likewise for OLDDYNCOMMON and OLDDEF.
1548 Note that this test is just a heuristic, and that it is quite
1549 possible to have an uninitialized symbol in a shared object which
1550 is really a definition, rather than a common symbol. This could
1551 lead to some minor confusion when the symbol really is a common
1552 symbol in some regular object. However, I think it will be
1558 && (sec->flags & SEC_ALLOC) != 0
1559 && (sec->flags & SEC_LOAD) == 0
1562 newdyncommon = TRUE;
1564 newdyncommon = FALSE;
1568 && h->root.type == bfd_link_hash_defined
1570 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1571 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1574 olddyncommon = TRUE;
1576 olddyncommon = FALSE;
1578 /* We now know everything about the old and new symbols. We ask the
1579 backend to check if we can merge them. */
1580 if (bed->merge_symbol != NULL)
1582 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1587 /* There are multiple definitions of a normal symbol. Skip the
1588 default symbol as well as definition from an IR object. */
1589 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1590 && !default_sym && h->def_regular
1592 && (oldbfd->flags & BFD_PLUGIN) != 0
1593 && (abfd->flags & BFD_PLUGIN) == 0))
1595 /* Handle a multiple definition. */
1596 (*info->callbacks->multiple_definition) (info, &h->root,
1597 abfd, sec, *pvalue);
1602 /* If both the old and the new symbols look like common symbols in a
1603 dynamic object, set the size of the symbol to the larger of the
1608 && sym->st_size != h->size)
1610 /* Since we think we have two common symbols, issue a multiple
1611 common warning if desired. Note that we only warn if the
1612 size is different. If the size is the same, we simply let
1613 the old symbol override the new one as normally happens with
1614 symbols defined in dynamic objects. */
1616 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1617 bfd_link_hash_common, sym->st_size);
1618 if (sym->st_size > h->size)
1619 h->size = sym->st_size;
1621 *size_change_ok = TRUE;
1624 /* If we are looking at a dynamic object, and we have found a
1625 definition, we need to see if the symbol was already defined by
1626 some other object. If so, we want to use the existing
1627 definition, and we do not want to report a multiple symbol
1628 definition error; we do this by clobbering *PSEC to be
1629 bfd_und_section_ptr.
1631 We treat a common symbol as a definition if the symbol in the
1632 shared library is a function, since common symbols always
1633 represent variables; this can cause confusion in principle, but
1634 any such confusion would seem to indicate an erroneous program or
1635 shared library. We also permit a common symbol in a regular
1636 object to override a weak symbol in a shared object. */
1641 || (h->root.type == bfd_link_hash_common
1642 && (newweak || newfunc))))
1646 newdyncommon = FALSE;
1648 *psec = sec = bfd_und_section_ptr;
1649 *size_change_ok = TRUE;
1651 /* If we get here when the old symbol is a common symbol, then
1652 we are explicitly letting it override a weak symbol or
1653 function in a dynamic object, and we don't want to warn about
1654 a type change. If the old symbol is a defined symbol, a type
1655 change warning may still be appropriate. */
1657 if (h->root.type == bfd_link_hash_common)
1658 *type_change_ok = TRUE;
1661 /* Handle the special case of an old common symbol merging with a
1662 new symbol which looks like a common symbol in a shared object.
1663 We change *PSEC and *PVALUE to make the new symbol look like a
1664 common symbol, and let _bfd_generic_link_add_one_symbol do the
1668 && h->root.type == bfd_link_hash_common)
1672 newdyncommon = FALSE;
1673 *pvalue = sym->st_size;
1674 *psec = sec = bed->common_section (oldsec);
1675 *size_change_ok = TRUE;
1678 /* Skip weak definitions of symbols that are already defined. */
1679 if (newdef && olddef && newweak)
1681 /* Don't skip new non-IR weak syms. */
1682 if (!(oldbfd != NULL
1683 && (oldbfd->flags & BFD_PLUGIN) != 0
1684 && (abfd->flags & BFD_PLUGIN) == 0))
1690 /* Merge st_other. If the symbol already has a dynamic index,
1691 but visibility says it should not be visible, turn it into a
1693 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1694 if (h->dynindx != -1)
1695 switch (ELF_ST_VISIBILITY (h->other))
1699 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1704 /* If the old symbol is from a dynamic object, and the new symbol is
1705 a definition which is not from a dynamic object, then the new
1706 symbol overrides the old symbol. Symbols from regular files
1707 always take precedence over symbols from dynamic objects, even if
1708 they are defined after the dynamic object in the link.
1710 As above, we again permit a common symbol in a regular object to
1711 override a definition in a shared object if the shared object
1712 symbol is a function or is weak. */
1717 || (bfd_is_com_section (sec)
1718 && (oldweak || oldfunc)))
1723 /* Change the hash table entry to undefined, and let
1724 _bfd_generic_link_add_one_symbol do the right thing with the
1727 h->root.type = bfd_link_hash_undefined;
1728 h->root.u.undef.abfd = h->root.u.def.section->owner;
1729 *size_change_ok = TRUE;
1732 olddyncommon = FALSE;
1734 /* We again permit a type change when a common symbol may be
1735 overriding a function. */
1737 if (bfd_is_com_section (sec))
1741 /* If a common symbol overrides a function, make sure
1742 that it isn't defined dynamically nor has type
1745 h->type = STT_NOTYPE;
1747 *type_change_ok = TRUE;
1750 if (hi->root.type == bfd_link_hash_indirect)
1753 /* This union may have been set to be non-NULL when this symbol
1754 was seen in a dynamic object. We must force the union to be
1755 NULL, so that it is correct for a regular symbol. */
1756 h->verinfo.vertree = NULL;
1759 /* Handle the special case of a new common symbol merging with an
1760 old symbol that looks like it might be a common symbol defined in
1761 a shared object. Note that we have already handled the case in
1762 which a new common symbol should simply override the definition
1763 in the shared library. */
1766 && bfd_is_com_section (sec)
1769 /* It would be best if we could set the hash table entry to a
1770 common symbol, but we don't know what to use for the section
1771 or the alignment. */
1772 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1773 bfd_link_hash_common, sym->st_size);
1775 /* If the presumed common symbol in the dynamic object is
1776 larger, pretend that the new symbol has its size. */
1778 if (h->size > *pvalue)
1781 /* We need to remember the alignment required by the symbol
1782 in the dynamic object. */
1783 BFD_ASSERT (pold_alignment);
1784 *pold_alignment = h->root.u.def.section->alignment_power;
1787 olddyncommon = FALSE;
1789 h->root.type = bfd_link_hash_undefined;
1790 h->root.u.undef.abfd = h->root.u.def.section->owner;
1792 *size_change_ok = TRUE;
1793 *type_change_ok = TRUE;
1795 if (hi->root.type == bfd_link_hash_indirect)
1798 h->verinfo.vertree = NULL;
1803 /* Handle the case where we had a versioned symbol in a dynamic
1804 library and now find a definition in a normal object. In this
1805 case, we make the versioned symbol point to the normal one. */
1806 flip->root.type = h->root.type;
1807 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1808 h->root.type = bfd_link_hash_indirect;
1809 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1810 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1814 flip->ref_dynamic = 1;
1821 /* This function is called to create an indirect symbol from the
1822 default for the symbol with the default version if needed. The
1823 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1824 set DYNSYM if the new indirect symbol is dynamic. */
1827 _bfd_elf_add_default_symbol (bfd *abfd,
1828 struct bfd_link_info *info,
1829 struct elf_link_hash_entry *h,
1831 Elf_Internal_Sym *sym,
1835 bfd_boolean *dynsym)
1837 bfd_boolean type_change_ok;
1838 bfd_boolean size_change_ok;
1841 struct elf_link_hash_entry *hi;
1842 struct bfd_link_hash_entry *bh;
1843 const struct elf_backend_data *bed;
1844 bfd_boolean collect;
1845 bfd_boolean dynamic;
1846 bfd_boolean override;
1848 size_t len, shortlen;
1850 bfd_boolean matched;
1852 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1855 /* If this symbol has a version, and it is the default version, we
1856 create an indirect symbol from the default name to the fully
1857 decorated name. This will cause external references which do not
1858 specify a version to be bound to this version of the symbol. */
1859 p = strchr (name, ELF_VER_CHR);
1860 if (h->versioned == unknown)
1864 h->versioned = unversioned;
1869 if (p[1] != ELF_VER_CHR)
1871 h->versioned = versioned_hidden;
1875 h->versioned = versioned;
1880 /* PR ld/19073: We may see an unversioned definition after the
1886 bed = get_elf_backend_data (abfd);
1887 collect = bed->collect;
1888 dynamic = (abfd->flags & DYNAMIC) != 0;
1890 shortlen = p - name;
1891 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1892 if (shortname == NULL)
1894 memcpy (shortname, name, shortlen);
1895 shortname[shortlen] = '\0';
1897 /* We are going to create a new symbol. Merge it with any existing
1898 symbol with this name. For the purposes of the merge, act as
1899 though we were defining the symbol we just defined, although we
1900 actually going to define an indirect symbol. */
1901 type_change_ok = FALSE;
1902 size_change_ok = FALSE;
1905 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1906 &hi, poldbfd, NULL, NULL, &skip, &override,
1907 &type_change_ok, &size_change_ok, &matched))
1913 if (hi->def_regular)
1915 /* If the undecorated symbol will have a version added by a
1916 script different to H, then don't indirect to/from the
1917 undecorated symbol. This isn't ideal because we may not yet
1918 have seen symbol versions, if given by a script on the
1919 command line rather than via --version-script. */
1920 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1925 = bfd_find_version_for_sym (info->version_info,
1926 hi->root.root.string, &hide);
1927 if (hi->verinfo.vertree != NULL && hide)
1929 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1933 if (hi->verinfo.vertree != NULL
1934 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1940 /* Add the default symbol if not performing a relocatable link. */
1941 if (! bfd_link_relocatable (info))
1944 if (! (_bfd_generic_link_add_one_symbol
1945 (info, abfd, shortname, BSF_INDIRECT,
1946 bfd_ind_section_ptr,
1947 0, name, FALSE, collect, &bh)))
1949 hi = (struct elf_link_hash_entry *) bh;
1954 /* In this case the symbol named SHORTNAME is overriding the
1955 indirect symbol we want to add. We were planning on making
1956 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1957 is the name without a version. NAME is the fully versioned
1958 name, and it is the default version.
1960 Overriding means that we already saw a definition for the
1961 symbol SHORTNAME in a regular object, and it is overriding
1962 the symbol defined in the dynamic object.
1964 When this happens, we actually want to change NAME, the
1965 symbol we just added, to refer to SHORTNAME. This will cause
1966 references to NAME in the shared object to become references
1967 to SHORTNAME in the regular object. This is what we expect
1968 when we override a function in a shared object: that the
1969 references in the shared object will be mapped to the
1970 definition in the regular object. */
1972 while (hi->root.type == bfd_link_hash_indirect
1973 || hi->root.type == bfd_link_hash_warning)
1974 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1976 h->root.type = bfd_link_hash_indirect;
1977 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1981 hi->ref_dynamic = 1;
1985 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1990 /* Now set HI to H, so that the following code will set the
1991 other fields correctly. */
1995 /* Check if HI is a warning symbol. */
1996 if (hi->root.type == bfd_link_hash_warning)
1997 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1999 /* If there is a duplicate definition somewhere, then HI may not
2000 point to an indirect symbol. We will have reported an error to
2001 the user in that case. */
2003 if (hi->root.type == bfd_link_hash_indirect)
2005 struct elf_link_hash_entry *ht;
2007 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2008 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2010 /* A reference to the SHORTNAME symbol from a dynamic library
2011 will be satisfied by the versioned symbol at runtime. In
2012 effect, we have a reference to the versioned symbol. */
2013 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2014 hi->dynamic_def |= ht->dynamic_def;
2016 /* See if the new flags lead us to realize that the symbol must
2022 if (! bfd_link_executable (info)
2029 if (hi->ref_regular)
2035 /* We also need to define an indirection from the nondefault version
2039 len = strlen (name);
2040 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2041 if (shortname == NULL)
2043 memcpy (shortname, name, shortlen);
2044 memcpy (shortname + shortlen, p + 1, len - shortlen);
2046 /* Once again, merge with any existing symbol. */
2047 type_change_ok = FALSE;
2048 size_change_ok = FALSE;
2050 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2051 &hi, poldbfd, NULL, NULL, &skip, &override,
2052 &type_change_ok, &size_change_ok, &matched))
2060 /* Here SHORTNAME is a versioned name, so we don't expect to see
2061 the type of override we do in the case above unless it is
2062 overridden by a versioned definition. */
2063 if (hi->root.type != bfd_link_hash_defined
2064 && hi->root.type != bfd_link_hash_defweak)
2066 /* xgettext:c-format */
2067 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2073 if (! (_bfd_generic_link_add_one_symbol
2074 (info, abfd, shortname, BSF_INDIRECT,
2075 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2077 hi = (struct elf_link_hash_entry *) bh;
2079 /* If there is a duplicate definition somewhere, then HI may not
2080 point to an indirect symbol. We will have reported an error
2081 to the user in that case. */
2083 if (hi->root.type == bfd_link_hash_indirect)
2085 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2086 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2087 hi->dynamic_def |= h->dynamic_def;
2089 /* See if the new flags lead us to realize that the symbol
2095 if (! bfd_link_executable (info)
2101 if (hi->ref_regular)
2111 /* This routine is used to export all defined symbols into the dynamic
2112 symbol table. It is called via elf_link_hash_traverse. */
2115 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2117 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2119 /* Ignore indirect symbols. These are added by the versioning code. */
2120 if (h->root.type == bfd_link_hash_indirect)
2123 /* Ignore this if we won't export it. */
2124 if (!eif->info->export_dynamic && !h->dynamic)
2127 if (h->dynindx == -1
2128 && (h->def_regular || h->ref_regular)
2129 && ! bfd_hide_sym_by_version (eif->info->version_info,
2130 h->root.root.string))
2132 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2142 /* Look through the symbols which are defined in other shared
2143 libraries and referenced here. Update the list of version
2144 dependencies. This will be put into the .gnu.version_r section.
2145 This function is called via elf_link_hash_traverse. */
2148 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2151 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2152 Elf_Internal_Verneed *t;
2153 Elf_Internal_Vernaux *a;
2156 /* We only care about symbols defined in shared objects with version
2161 || h->verinfo.verdef == NULL
2162 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2163 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2166 /* See if we already know about this version. */
2167 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2171 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2174 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2175 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2181 /* This is a new version. Add it to tree we are building. */
2186 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2189 rinfo->failed = TRUE;
2193 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2194 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2195 elf_tdata (rinfo->info->output_bfd)->verref = t;
2199 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2202 rinfo->failed = TRUE;
2206 /* Note that we are copying a string pointer here, and testing it
2207 above. If bfd_elf_string_from_elf_section is ever changed to
2208 discard the string data when low in memory, this will have to be
2210 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2212 a->vna_flags = h->verinfo.verdef->vd_flags;
2213 a->vna_nextptr = t->vn_auxptr;
2215 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2218 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2225 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2226 hidden. Set *T_P to NULL if there is no match. */
2229 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2230 struct elf_link_hash_entry *h,
2231 const char *version_p,
2232 struct bfd_elf_version_tree **t_p,
2235 struct bfd_elf_version_tree *t;
2237 /* Look for the version. If we find it, it is no longer weak. */
2238 for (t = info->version_info; t != NULL; t = t->next)
2240 if (strcmp (t->name, version_p) == 0)
2244 struct bfd_elf_version_expr *d;
2246 len = version_p - h->root.root.string;
2247 alc = (char *) bfd_malloc (len);
2250 memcpy (alc, h->root.root.string, len - 1);
2251 alc[len - 1] = '\0';
2252 if (alc[len - 2] == ELF_VER_CHR)
2253 alc[len - 2] = '\0';
2255 h->verinfo.vertree = t;
2259 if (t->globals.list != NULL)
2260 d = (*t->match) (&t->globals, NULL, alc);
2262 /* See if there is anything to force this symbol to
2264 if (d == NULL && t->locals.list != NULL)
2266 d = (*t->match) (&t->locals, NULL, alc);
2269 && ! info->export_dynamic)
2283 /* Return TRUE if the symbol H is hidden by version script. */
2286 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2287 struct elf_link_hash_entry *h)
2290 bfd_boolean hide = FALSE;
2291 const struct elf_backend_data *bed
2292 = get_elf_backend_data (info->output_bfd);
2294 /* Version script only hides symbols defined in regular objects. */
2295 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2298 p = strchr (h->root.root.string, ELF_VER_CHR);
2299 if (p != NULL && h->verinfo.vertree == NULL)
2301 struct bfd_elf_version_tree *t;
2304 if (*p == ELF_VER_CHR)
2308 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2312 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2317 /* If we don't have a version for this symbol, see if we can find
2319 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2322 = bfd_find_version_for_sym (info->version_info,
2323 h->root.root.string, &hide);
2324 if (h->verinfo.vertree != NULL && hide)
2326 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2334 /* Figure out appropriate versions for all the symbols. We may not
2335 have the version number script until we have read all of the input
2336 files, so until that point we don't know which symbols should be
2337 local. This function is called via elf_link_hash_traverse. */
2340 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2342 struct elf_info_failed *sinfo;
2343 struct bfd_link_info *info;
2344 const struct elf_backend_data *bed;
2345 struct elf_info_failed eif;
2349 sinfo = (struct elf_info_failed *) data;
2352 /* Fix the symbol flags. */
2355 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2358 sinfo->failed = TRUE;
2362 bed = get_elf_backend_data (info->output_bfd);
2364 /* We only need version numbers for symbols defined in regular
2366 if (!h->def_regular)
2368 /* Hide symbols defined in discarded input sections. */
2369 if ((h->root.type == bfd_link_hash_defined
2370 || h->root.type == bfd_link_hash_defweak)
2371 && discarded_section (h->root.u.def.section))
2372 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2377 p = strchr (h->root.root.string, ELF_VER_CHR);
2378 if (p != NULL && h->verinfo.vertree == NULL)
2380 struct bfd_elf_version_tree *t;
2383 if (*p == ELF_VER_CHR)
2386 /* If there is no version string, we can just return out. */
2390 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2392 sinfo->failed = TRUE;
2397 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2399 /* If we are building an application, we need to create a
2400 version node for this version. */
2401 if (t == NULL && bfd_link_executable (info))
2403 struct bfd_elf_version_tree **pp;
2406 /* If we aren't going to export this symbol, we don't need
2407 to worry about it. */
2408 if (h->dynindx == -1)
2411 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2415 sinfo->failed = TRUE;
2420 t->name_indx = (unsigned int) -1;
2424 /* Don't count anonymous version tag. */
2425 if (sinfo->info->version_info != NULL
2426 && sinfo->info->version_info->vernum == 0)
2428 for (pp = &sinfo->info->version_info;
2432 t->vernum = version_index;
2436 h->verinfo.vertree = t;
2440 /* We could not find the version for a symbol when
2441 generating a shared archive. Return an error. */
2443 /* xgettext:c-format */
2444 (_("%pB: version node not found for symbol %s"),
2445 info->output_bfd, h->root.root.string);
2446 bfd_set_error (bfd_error_bad_value);
2447 sinfo->failed = TRUE;
2452 /* If we don't have a version for this symbol, see if we can find
2455 && h->verinfo.vertree == NULL
2456 && sinfo->info->version_info != NULL)
2459 = bfd_find_version_for_sym (sinfo->info->version_info,
2460 h->root.root.string, &hide);
2461 if (h->verinfo.vertree != NULL && hide)
2462 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2468 /* Read and swap the relocs from the section indicated by SHDR. This
2469 may be either a REL or a RELA section. The relocations are
2470 translated into RELA relocations and stored in INTERNAL_RELOCS,
2471 which should have already been allocated to contain enough space.
2472 The EXTERNAL_RELOCS are a buffer where the external form of the
2473 relocations should be stored.
2475 Returns FALSE if something goes wrong. */
2478 elf_link_read_relocs_from_section (bfd *abfd,
2480 Elf_Internal_Shdr *shdr,
2481 void *external_relocs,
2482 Elf_Internal_Rela *internal_relocs)
2484 const struct elf_backend_data *bed;
2485 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2486 const bfd_byte *erela;
2487 const bfd_byte *erelaend;
2488 Elf_Internal_Rela *irela;
2489 Elf_Internal_Shdr *symtab_hdr;
2492 /* Position ourselves at the start of the section. */
2493 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2496 /* Read the relocations. */
2497 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2500 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2501 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2503 bed = get_elf_backend_data (abfd);
2505 /* Convert the external relocations to the internal format. */
2506 if (shdr->sh_entsize == bed->s->sizeof_rel)
2507 swap_in = bed->s->swap_reloc_in;
2508 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2509 swap_in = bed->s->swap_reloca_in;
2512 bfd_set_error (bfd_error_wrong_format);
2516 erela = (const bfd_byte *) external_relocs;
2517 erelaend = erela + shdr->sh_size;
2518 irela = internal_relocs;
2519 while (erela < erelaend)
2523 (*swap_in) (abfd, erela, irela);
2524 r_symndx = ELF32_R_SYM (irela->r_info);
2525 if (bed->s->arch_size == 64)
2529 if ((size_t) r_symndx >= nsyms)
2532 /* xgettext:c-format */
2533 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2534 " for offset %#" PRIx64 " in section `%pA'"),
2535 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2536 (uint64_t) irela->r_offset, sec);
2537 bfd_set_error (bfd_error_bad_value);
2541 else if (r_symndx != STN_UNDEF)
2544 /* xgettext:c-format */
2545 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2546 " for offset %#" PRIx64 " in section `%pA'"
2547 " when the object file has no symbol table"),
2548 abfd, (uint64_t) r_symndx,
2549 (uint64_t) irela->r_offset, sec);
2550 bfd_set_error (bfd_error_bad_value);
2553 irela += bed->s->int_rels_per_ext_rel;
2554 erela += shdr->sh_entsize;
2560 /* Read and swap the relocs for a section O. They may have been
2561 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2562 not NULL, they are used as buffers to read into. They are known to
2563 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2564 the return value is allocated using either malloc or bfd_alloc,
2565 according to the KEEP_MEMORY argument. If O has two relocation
2566 sections (both REL and RELA relocations), then the REL_HDR
2567 relocations will appear first in INTERNAL_RELOCS, followed by the
2568 RELA_HDR relocations. */
2571 _bfd_elf_link_read_relocs (bfd *abfd,
2573 void *external_relocs,
2574 Elf_Internal_Rela *internal_relocs,
2575 bfd_boolean keep_memory)
2577 void *alloc1 = NULL;
2578 Elf_Internal_Rela *alloc2 = NULL;
2579 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2580 struct bfd_elf_section_data *esdo = elf_section_data (o);
2581 Elf_Internal_Rela *internal_rela_relocs;
2583 if (esdo->relocs != NULL)
2584 return esdo->relocs;
2586 if (o->reloc_count == 0)
2589 if (internal_relocs == NULL)
2593 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2595 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2597 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2598 if (internal_relocs == NULL)
2602 if (external_relocs == NULL)
2604 bfd_size_type size = 0;
2607 size += esdo->rel.hdr->sh_size;
2609 size += esdo->rela.hdr->sh_size;
2611 alloc1 = bfd_malloc (size);
2614 external_relocs = alloc1;
2617 internal_rela_relocs = internal_relocs;
2620 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2624 external_relocs = (((bfd_byte *) external_relocs)
2625 + esdo->rel.hdr->sh_size);
2626 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2627 * bed->s->int_rels_per_ext_rel);
2631 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2633 internal_rela_relocs)))
2636 /* Cache the results for next time, if we can. */
2638 esdo->relocs = internal_relocs;
2643 /* Don't free alloc2, since if it was allocated we are passing it
2644 back (under the name of internal_relocs). */
2646 return internal_relocs;
2654 bfd_release (abfd, alloc2);
2661 /* Compute the size of, and allocate space for, REL_HDR which is the
2662 section header for a section containing relocations for O. */
2665 _bfd_elf_link_size_reloc_section (bfd *abfd,
2666 struct bfd_elf_section_reloc_data *reldata)
2668 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2670 /* That allows us to calculate the size of the section. */
2671 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2673 /* The contents field must last into write_object_contents, so we
2674 allocate it with bfd_alloc rather than malloc. Also since we
2675 cannot be sure that the contents will actually be filled in,
2676 we zero the allocated space. */
2677 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2678 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2681 if (reldata->hashes == NULL && reldata->count)
2683 struct elf_link_hash_entry **p;
2685 p = ((struct elf_link_hash_entry **)
2686 bfd_zmalloc (reldata->count * sizeof (*p)));
2690 reldata->hashes = p;
2696 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2697 originated from the section given by INPUT_REL_HDR) to the
2701 _bfd_elf_link_output_relocs (bfd *output_bfd,
2702 asection *input_section,
2703 Elf_Internal_Shdr *input_rel_hdr,
2704 Elf_Internal_Rela *internal_relocs,
2705 struct elf_link_hash_entry **rel_hash
2708 Elf_Internal_Rela *irela;
2709 Elf_Internal_Rela *irelaend;
2711 struct bfd_elf_section_reloc_data *output_reldata;
2712 asection *output_section;
2713 const struct elf_backend_data *bed;
2714 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2715 struct bfd_elf_section_data *esdo;
2717 output_section = input_section->output_section;
2719 bed = get_elf_backend_data (output_bfd);
2720 esdo = elf_section_data (output_section);
2721 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2723 output_reldata = &esdo->rel;
2724 swap_out = bed->s->swap_reloc_out;
2726 else if (esdo->rela.hdr
2727 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2729 output_reldata = &esdo->rela;
2730 swap_out = bed->s->swap_reloca_out;
2735 /* xgettext:c-format */
2736 (_("%pB: relocation size mismatch in %pB section %pA"),
2737 output_bfd, input_section->owner, input_section);
2738 bfd_set_error (bfd_error_wrong_format);
2742 erel = output_reldata->hdr->contents;
2743 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2744 irela = internal_relocs;
2745 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2746 * bed->s->int_rels_per_ext_rel);
2747 while (irela < irelaend)
2749 (*swap_out) (output_bfd, irela, erel);
2750 irela += bed->s->int_rels_per_ext_rel;
2751 erel += input_rel_hdr->sh_entsize;
2754 /* Bump the counter, so that we know where to add the next set of
2756 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2761 /* Make weak undefined symbols in PIE dynamic. */
2764 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2765 struct elf_link_hash_entry *h)
2767 if (bfd_link_pie (info)
2769 && h->root.type == bfd_link_hash_undefweak)
2770 return bfd_elf_link_record_dynamic_symbol (info, h);
2775 /* Fix up the flags for a symbol. This handles various cases which
2776 can only be fixed after all the input files are seen. This is
2777 currently called by both adjust_dynamic_symbol and
2778 assign_sym_version, which is unnecessary but perhaps more robust in
2779 the face of future changes. */
2782 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2783 struct elf_info_failed *eif)
2785 const struct elf_backend_data *bed;
2787 /* If this symbol was mentioned in a non-ELF file, try to set
2788 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2789 permit a non-ELF file to correctly refer to a symbol defined in
2790 an ELF dynamic object. */
2793 while (h->root.type == bfd_link_hash_indirect)
2794 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2796 if (h->root.type != bfd_link_hash_defined
2797 && h->root.type != bfd_link_hash_defweak)
2800 h->ref_regular_nonweak = 1;
2804 if (h->root.u.def.section->owner != NULL
2805 && (bfd_get_flavour (h->root.u.def.section->owner)
2806 == bfd_target_elf_flavour))
2809 h->ref_regular_nonweak = 1;
2815 if (h->dynindx == -1
2819 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2828 /* Unfortunately, NON_ELF is only correct if the symbol
2829 was first seen in a non-ELF file. Fortunately, if the symbol
2830 was first seen in an ELF file, we're probably OK unless the
2831 symbol was defined in a non-ELF file. Catch that case here.
2832 FIXME: We're still in trouble if the symbol was first seen in
2833 a dynamic object, and then later in a non-ELF regular object. */
2834 if ((h->root.type == bfd_link_hash_defined
2835 || h->root.type == bfd_link_hash_defweak)
2837 && (h->root.u.def.section->owner != NULL
2838 ? (bfd_get_flavour (h->root.u.def.section->owner)
2839 != bfd_target_elf_flavour)
2840 : (bfd_is_abs_section (h->root.u.def.section)
2841 && !h->def_dynamic)))
2845 /* Backend specific symbol fixup. */
2846 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2847 if (bed->elf_backend_fixup_symbol
2848 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2851 /* If this is a final link, and the symbol was defined as a common
2852 symbol in a regular object file, and there was no definition in
2853 any dynamic object, then the linker will have allocated space for
2854 the symbol in a common section but the DEF_REGULAR
2855 flag will not have been set. */
2856 if (h->root.type == bfd_link_hash_defined
2860 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2863 /* Symbols defined in discarded sections shouldn't be dynamic. */
2864 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2865 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2867 /* If a weak undefined symbol has non-default visibility, we also
2868 hide it from the dynamic linker. */
2869 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2870 && h->root.type == bfd_link_hash_undefweak)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2873 /* A hidden versioned symbol in executable should be forced local if
2874 it is is locally defined, not referenced by shared library and not
2876 else if (bfd_link_executable (eif->info)
2877 && h->versioned == versioned_hidden
2878 && !eif->info->export_dynamic
2882 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2884 /* If -Bsymbolic was used (which means to bind references to global
2885 symbols to the definition within the shared object), and this
2886 symbol was defined in a regular object, then it actually doesn't
2887 need a PLT entry. Likewise, if the symbol has non-default
2888 visibility. If the symbol has hidden or internal visibility, we
2889 will force it local. */
2890 else if (h->needs_plt
2891 && bfd_link_pic (eif->info)
2892 && is_elf_hash_table (eif->info->hash)
2893 && (SYMBOLIC_BIND (eif->info, h)
2894 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2897 bfd_boolean force_local;
2899 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2900 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2901 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2904 /* If this is a weak defined symbol in a dynamic object, and we know
2905 the real definition in the dynamic object, copy interesting flags
2906 over to the real definition. */
2907 if (h->is_weakalias)
2909 struct elf_link_hash_entry *def = weakdef (h);
2911 /* If the real definition is defined by a regular object file,
2912 don't do anything special. See the longer description in
2913 _bfd_elf_adjust_dynamic_symbol, below. */
2914 if (def->def_regular)
2917 while ((h = h->u.alias) != def)
2918 h->is_weakalias = 0;
2922 while (h->root.type == bfd_link_hash_indirect)
2923 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2924 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2925 || h->root.type == bfd_link_hash_defweak);
2926 BFD_ASSERT (def->def_dynamic);
2927 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2928 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2935 /* Make the backend pick a good value for a dynamic symbol. This is
2936 called via elf_link_hash_traverse, and also calls itself
2940 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2942 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2943 struct elf_link_hash_table *htab;
2944 const struct elf_backend_data *bed;
2946 if (! is_elf_hash_table (eif->info->hash))
2949 /* Ignore indirect symbols. These are added by the versioning code. */
2950 if (h->root.type == bfd_link_hash_indirect)
2953 /* Fix the symbol flags. */
2954 if (! _bfd_elf_fix_symbol_flags (h, eif))
2957 htab = elf_hash_table (eif->info);
2958 bed = get_elf_backend_data (htab->dynobj);
2960 if (h->root.type == bfd_link_hash_undefweak)
2962 if (eif->info->dynamic_undefined_weak == 0)
2963 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2964 else if (eif->info->dynamic_undefined_weak > 0
2966 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2967 && !bfd_hide_sym_by_version (eif->info->version_info,
2968 h->root.root.string))
2970 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2978 /* If this symbol does not require a PLT entry, and it is not
2979 defined by a dynamic object, or is not referenced by a regular
2980 object, ignore it. We do have to handle a weak defined symbol,
2981 even if no regular object refers to it, if we decided to add it
2982 to the dynamic symbol table. FIXME: Do we normally need to worry
2983 about symbols which are defined by one dynamic object and
2984 referenced by another one? */
2986 && h->type != STT_GNU_IFUNC
2990 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2992 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2996 /* If we've already adjusted this symbol, don't do it again. This
2997 can happen via a recursive call. */
2998 if (h->dynamic_adjusted)
3001 /* Don't look at this symbol again. Note that we must set this
3002 after checking the above conditions, because we may look at a
3003 symbol once, decide not to do anything, and then get called
3004 recursively later after REF_REGULAR is set below. */
3005 h->dynamic_adjusted = 1;
3007 /* If this is a weak definition, and we know a real definition, and
3008 the real symbol is not itself defined by a regular object file,
3009 then get a good value for the real definition. We handle the
3010 real symbol first, for the convenience of the backend routine.
3012 Note that there is a confusing case here. If the real definition
3013 is defined by a regular object file, we don't get the real symbol
3014 from the dynamic object, but we do get the weak symbol. If the
3015 processor backend uses a COPY reloc, then if some routine in the
3016 dynamic object changes the real symbol, we will not see that
3017 change in the corresponding weak symbol. This is the way other
3018 ELF linkers work as well, and seems to be a result of the shared
3021 I will clarify this issue. Most SVR4 shared libraries define the
3022 variable _timezone and define timezone as a weak synonym. The
3023 tzset call changes _timezone. If you write
3024 extern int timezone;
3026 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3027 you might expect that, since timezone is a synonym for _timezone,
3028 the same number will print both times. However, if the processor
3029 backend uses a COPY reloc, then actually timezone will be copied
3030 into your process image, and, since you define _timezone
3031 yourself, _timezone will not. Thus timezone and _timezone will
3032 wind up at different memory locations. The tzset call will set
3033 _timezone, leaving timezone unchanged. */
3035 if (h->is_weakalias)
3037 struct elf_link_hash_entry *def = weakdef (h);
3039 /* If we get to this point, there is an implicit reference to
3040 the alias by a regular object file via the weak symbol H. */
3041 def->ref_regular = 1;
3043 /* Ensure that the backend adjust_dynamic_symbol function sees
3044 the strong alias before H by recursively calling ourselves. */
3045 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3049 /* If a symbol has no type and no size and does not require a PLT
3050 entry, then we are probably about to do the wrong thing here: we
3051 are probably going to create a COPY reloc for an empty object.
3052 This case can arise when a shared object is built with assembly
3053 code, and the assembly code fails to set the symbol type. */
3055 && h->type == STT_NOTYPE
3058 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3059 h->root.root.string);
3061 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3070 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3074 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3075 struct elf_link_hash_entry *h,
3078 unsigned int power_of_two;
3080 asection *sec = h->root.u.def.section;
3082 /* The section alignment of the definition is the maximum alignment
3083 requirement of symbols defined in the section. Since we don't
3084 know the symbol alignment requirement, we start with the
3085 maximum alignment and check low bits of the symbol address
3086 for the minimum alignment. */
3087 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3088 mask = ((bfd_vma) 1 << power_of_two) - 1;
3089 while ((h->root.u.def.value & mask) != 0)
3095 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3098 /* Adjust the section alignment if needed. */
3099 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3104 /* We make sure that the symbol will be aligned properly. */
3105 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3107 /* Define the symbol as being at this point in DYNBSS. */
3108 h->root.u.def.section = dynbss;
3109 h->root.u.def.value = dynbss->size;
3111 /* Increment the size of DYNBSS to make room for the symbol. */
3112 dynbss->size += h->size;
3114 /* No error if extern_protected_data is true. */
3115 if (h->protected_def
3116 && (!info->extern_protected_data
3117 || (info->extern_protected_data < 0
3118 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3119 info->callbacks->einfo
3120 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3121 h->root.root.string);
3126 /* Adjust all external symbols pointing into SEC_MERGE sections
3127 to reflect the object merging within the sections. */
3130 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3134 if ((h->root.type == bfd_link_hash_defined
3135 || h->root.type == bfd_link_hash_defweak)
3136 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3137 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3139 bfd *output_bfd = (bfd *) data;
3141 h->root.u.def.value =
3142 _bfd_merged_section_offset (output_bfd,
3143 &h->root.u.def.section,
3144 elf_section_data (sec)->sec_info,
3145 h->root.u.def.value);
3151 /* Returns false if the symbol referred to by H should be considered
3152 to resolve local to the current module, and true if it should be
3153 considered to bind dynamically. */
3156 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3157 struct bfd_link_info *info,
3158 bfd_boolean not_local_protected)
3160 bfd_boolean binding_stays_local_p;
3161 const struct elf_backend_data *bed;
3162 struct elf_link_hash_table *hash_table;
3167 while (h->root.type == bfd_link_hash_indirect
3168 || h->root.type == bfd_link_hash_warning)
3169 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3171 /* If it was forced local, then clearly it's not dynamic. */
3172 if (h->dynindx == -1)
3174 if (h->forced_local)
3177 /* Identify the cases where name binding rules say that a
3178 visible symbol resolves locally. */
3179 binding_stays_local_p = (bfd_link_executable (info)
3180 || SYMBOLIC_BIND (info, h));
3182 switch (ELF_ST_VISIBILITY (h->other))
3189 hash_table = elf_hash_table (info);
3190 if (!is_elf_hash_table (hash_table))
3193 bed = get_elf_backend_data (hash_table->dynobj);
3195 /* Proper resolution for function pointer equality may require
3196 that these symbols perhaps be resolved dynamically, even though
3197 we should be resolving them to the current module. */
3198 if (!not_local_protected || !bed->is_function_type (h->type))
3199 binding_stays_local_p = TRUE;
3206 /* If it isn't defined locally, then clearly it's dynamic. */
3207 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3210 /* Otherwise, the symbol is dynamic if binding rules don't tell
3211 us that it remains local. */
3212 return !binding_stays_local_p;
3215 /* Return true if the symbol referred to by H should be considered
3216 to resolve local to the current module, and false otherwise. Differs
3217 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3218 undefined symbols. The two functions are virtually identical except
3219 for the place where dynindx == -1 is tested. If that test is true,
3220 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3221 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3223 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3224 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3225 treatment of undefined weak symbols. For those that do not make
3226 undefined weak symbols dynamic, both functions may return false. */
3229 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3230 struct bfd_link_info *info,
3231 bfd_boolean local_protected)
3233 const struct elf_backend_data *bed;
3234 struct elf_link_hash_table *hash_table;
3236 /* If it's a local sym, of course we resolve locally. */
3240 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3241 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3242 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3245 /* Forced local symbols resolve locally. */
3246 if (h->forced_local)
3249 /* Common symbols that become definitions don't get the DEF_REGULAR
3250 flag set, so test it first, and don't bail out. */
3251 if (ELF_COMMON_DEF_P (h))
3253 /* If we don't have a definition in a regular file, then we can't
3254 resolve locally. The sym is either undefined or dynamic. */
3255 else if (!h->def_regular)
3258 /* Non-dynamic symbols resolve locally. */
3259 if (h->dynindx == -1)
3262 /* At this point, we know the symbol is defined and dynamic. In an
3263 executable it must resolve locally, likewise when building symbolic
3264 shared libraries. */
3265 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3268 /* Now deal with defined dynamic symbols in shared libraries. Ones
3269 with default visibility might not resolve locally. */
3270 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3273 hash_table = elf_hash_table (info);
3274 if (!is_elf_hash_table (hash_table))
3277 bed = get_elf_backend_data (hash_table->dynobj);
3279 /* If extern_protected_data is false, STV_PROTECTED non-function
3280 symbols are local. */
3281 if ((!info->extern_protected_data
3282 || (info->extern_protected_data < 0
3283 && !bed->extern_protected_data))
3284 && !bed->is_function_type (h->type))
3287 /* Function pointer equality tests may require that STV_PROTECTED
3288 symbols be treated as dynamic symbols. If the address of a
3289 function not defined in an executable is set to that function's
3290 plt entry in the executable, then the address of the function in
3291 a shared library must also be the plt entry in the executable. */
3292 return local_protected;
3295 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3296 aligned. Returns the first TLS output section. */
3298 struct bfd_section *
3299 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3301 struct bfd_section *sec, *tls;
3302 unsigned int align = 0;
3304 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3305 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3309 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3310 if (sec->alignment_power > align)
3311 align = sec->alignment_power;
3313 elf_hash_table (info)->tls_sec = tls;
3315 /* Ensure the alignment of the first section is the largest alignment,
3316 so that the tls segment starts aligned. */
3318 tls->alignment_power = align;
3323 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3325 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3326 Elf_Internal_Sym *sym)
3328 const struct elf_backend_data *bed;
3330 /* Local symbols do not count, but target specific ones might. */
3331 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3332 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3335 bed = get_elf_backend_data (abfd);
3336 /* Function symbols do not count. */
3337 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3340 /* If the section is undefined, then so is the symbol. */
3341 if (sym->st_shndx == SHN_UNDEF)
3344 /* If the symbol is defined in the common section, then
3345 it is a common definition and so does not count. */
3346 if (bed->common_definition (sym))
3349 /* If the symbol is in a target specific section then we
3350 must rely upon the backend to tell us what it is. */
3351 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3352 /* FIXME - this function is not coded yet:
3354 return _bfd_is_global_symbol_definition (abfd, sym);
3356 Instead for now assume that the definition is not global,
3357 Even if this is wrong, at least the linker will behave
3358 in the same way that it used to do. */
3364 /* Search the symbol table of the archive element of the archive ABFD
3365 whose archive map contains a mention of SYMDEF, and determine if
3366 the symbol is defined in this element. */
3368 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3370 Elf_Internal_Shdr * hdr;
3374 Elf_Internal_Sym *isymbuf;
3375 Elf_Internal_Sym *isym;
3376 Elf_Internal_Sym *isymend;
3379 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3383 if (! bfd_check_format (abfd, bfd_object))
3386 /* Select the appropriate symbol table. If we don't know if the
3387 object file is an IR object, give linker LTO plugin a chance to
3388 get the correct symbol table. */
3389 if (abfd->plugin_format == bfd_plugin_yes
3390 #if BFD_SUPPORTS_PLUGINS
3391 || (abfd->plugin_format == bfd_plugin_unknown
3392 && bfd_link_plugin_object_p (abfd))
3396 /* Use the IR symbol table if the object has been claimed by
3398 abfd = abfd->plugin_dummy_bfd;
3399 hdr = &elf_tdata (abfd)->symtab_hdr;
3401 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3402 hdr = &elf_tdata (abfd)->symtab_hdr;
3404 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3406 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3408 /* The sh_info field of the symtab header tells us where the
3409 external symbols start. We don't care about the local symbols. */
3410 if (elf_bad_symtab (abfd))
3412 extsymcount = symcount;
3417 extsymcount = symcount - hdr->sh_info;
3418 extsymoff = hdr->sh_info;
3421 if (extsymcount == 0)
3424 /* Read in the symbol table. */
3425 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3427 if (isymbuf == NULL)
3430 /* Scan the symbol table looking for SYMDEF. */
3432 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3436 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3441 if (strcmp (name, symdef->name) == 0)
3443 result = is_global_data_symbol_definition (abfd, isym);
3453 /* Add an entry to the .dynamic table. */
3456 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3460 struct elf_link_hash_table *hash_table;
3461 const struct elf_backend_data *bed;
3463 bfd_size_type newsize;
3464 bfd_byte *newcontents;
3465 Elf_Internal_Dyn dyn;
3467 hash_table = elf_hash_table (info);
3468 if (! is_elf_hash_table (hash_table))
3471 if (tag == DT_RELA || tag == DT_REL)
3472 hash_table->dynamic_relocs = TRUE;
3474 bed = get_elf_backend_data (hash_table->dynobj);
3475 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3476 BFD_ASSERT (s != NULL);
3478 newsize = s->size + bed->s->sizeof_dyn;
3479 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3480 if (newcontents == NULL)
3484 dyn.d_un.d_val = val;
3485 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3488 s->contents = newcontents;
3493 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3494 otherwise just check whether one already exists. Returns -1 on error,
3495 1 if a DT_NEEDED tag already exists, and 0 on success. */
3498 elf_add_dt_needed_tag (bfd *abfd,
3499 struct bfd_link_info *info,
3503 struct elf_link_hash_table *hash_table;
3506 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3509 hash_table = elf_hash_table (info);
3510 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3511 if (strindex == (size_t) -1)
3514 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3517 const struct elf_backend_data *bed;
3520 bed = get_elf_backend_data (hash_table->dynobj);
3521 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3523 for (extdyn = sdyn->contents;
3524 extdyn < sdyn->contents + sdyn->size;
3525 extdyn += bed->s->sizeof_dyn)
3527 Elf_Internal_Dyn dyn;
3529 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3530 if (dyn.d_tag == DT_NEEDED
3531 && dyn.d_un.d_val == strindex)
3533 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3541 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3544 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3548 /* We were just checking for existence of the tag. */
3549 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3554 /* Return true if SONAME is on the needed list between NEEDED and STOP
3555 (or the end of list if STOP is NULL), and needed by a library that
3559 on_needed_list (const char *soname,
3560 struct bfd_link_needed_list *needed,
3561 struct bfd_link_needed_list *stop)
3563 struct bfd_link_needed_list *look;
3564 for (look = needed; look != stop; look = look->next)
3565 if (strcmp (soname, look->name) == 0
3566 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3567 /* If needed by a library that itself is not directly
3568 needed, recursively check whether that library is
3569 indirectly needed. Since we add DT_NEEDED entries to
3570 the end of the list, library dependencies appear after
3571 the library. Therefore search prior to the current
3572 LOOK, preventing possible infinite recursion. */
3573 || on_needed_list (elf_dt_name (look->by), needed, look)))
3579 /* Sort symbol by value, section, and size. */
3581 elf_sort_symbol (const void *arg1, const void *arg2)
3583 const struct elf_link_hash_entry *h1;
3584 const struct elf_link_hash_entry *h2;
3585 bfd_signed_vma vdiff;
3587 h1 = *(const struct elf_link_hash_entry **) arg1;
3588 h2 = *(const struct elf_link_hash_entry **) arg2;
3589 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3591 return vdiff > 0 ? 1 : -1;
3594 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3596 return sdiff > 0 ? 1 : -1;
3598 vdiff = h1->size - h2->size;
3599 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3602 /* This function is used to adjust offsets into .dynstr for
3603 dynamic symbols. This is called via elf_link_hash_traverse. */
3606 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3608 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3610 if (h->dynindx != -1)
3611 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3615 /* Assign string offsets in .dynstr, update all structures referencing
3619 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3621 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3622 struct elf_link_local_dynamic_entry *entry;
3623 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3624 bfd *dynobj = hash_table->dynobj;
3627 const struct elf_backend_data *bed;
3630 _bfd_elf_strtab_finalize (dynstr);
3631 size = _bfd_elf_strtab_size (dynstr);
3633 bed = get_elf_backend_data (dynobj);
3634 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3635 BFD_ASSERT (sdyn != NULL);
3637 /* Update all .dynamic entries referencing .dynstr strings. */
3638 for (extdyn = sdyn->contents;
3639 extdyn < sdyn->contents + sdyn->size;
3640 extdyn += bed->s->sizeof_dyn)
3642 Elf_Internal_Dyn dyn;
3644 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3648 dyn.d_un.d_val = size;
3658 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3663 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3666 /* Now update local dynamic symbols. */
3667 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3668 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3669 entry->isym.st_name);
3671 /* And the rest of dynamic symbols. */
3672 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3674 /* Adjust version definitions. */
3675 if (elf_tdata (output_bfd)->cverdefs)
3680 Elf_Internal_Verdef def;
3681 Elf_Internal_Verdaux defaux;
3683 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3687 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3689 p += sizeof (Elf_External_Verdef);
3690 if (def.vd_aux != sizeof (Elf_External_Verdef))
3692 for (i = 0; i < def.vd_cnt; ++i)
3694 _bfd_elf_swap_verdaux_in (output_bfd,
3695 (Elf_External_Verdaux *) p, &defaux);
3696 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3698 _bfd_elf_swap_verdaux_out (output_bfd,
3699 &defaux, (Elf_External_Verdaux *) p);
3700 p += sizeof (Elf_External_Verdaux);
3703 while (def.vd_next);
3706 /* Adjust version references. */
3707 if (elf_tdata (output_bfd)->verref)
3712 Elf_Internal_Verneed need;
3713 Elf_Internal_Vernaux needaux;
3715 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3719 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3721 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3722 _bfd_elf_swap_verneed_out (output_bfd, &need,
3723 (Elf_External_Verneed *) p);
3724 p += sizeof (Elf_External_Verneed);
3725 for (i = 0; i < need.vn_cnt; ++i)
3727 _bfd_elf_swap_vernaux_in (output_bfd,
3728 (Elf_External_Vernaux *) p, &needaux);
3729 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3731 _bfd_elf_swap_vernaux_out (output_bfd,
3733 (Elf_External_Vernaux *) p);
3734 p += sizeof (Elf_External_Vernaux);
3737 while (need.vn_next);
3743 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3744 The default is to only match when the INPUT and OUTPUT are exactly
3748 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3749 const bfd_target *output)
3751 return input == output;
3754 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3755 This version is used when different targets for the same architecture
3756 are virtually identical. */
3759 _bfd_elf_relocs_compatible (const bfd_target *input,
3760 const bfd_target *output)
3762 const struct elf_backend_data *obed, *ibed;
3764 if (input == output)
3767 ibed = xvec_get_elf_backend_data (input);
3768 obed = xvec_get_elf_backend_data (output);
3770 if (ibed->arch != obed->arch)
3773 /* If both backends are using this function, deem them compatible. */
3774 return ibed->relocs_compatible == obed->relocs_compatible;
3777 /* Make a special call to the linker "notice" function to tell it that
3778 we are about to handle an as-needed lib, or have finished
3779 processing the lib. */
3782 _bfd_elf_notice_as_needed (bfd *ibfd,
3783 struct bfd_link_info *info,
3784 enum notice_asneeded_action act)
3786 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3789 /* Check relocations an ELF object file. */
3792 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3795 struct elf_link_hash_table *htab = elf_hash_table (info);
3797 /* If this object is the same format as the output object, and it is
3798 not a shared library, then let the backend look through the
3801 This is required to build global offset table entries and to
3802 arrange for dynamic relocs. It is not required for the
3803 particular common case of linking non PIC code, even when linking
3804 against shared libraries, but unfortunately there is no way of
3805 knowing whether an object file has been compiled PIC or not.
3806 Looking through the relocs is not particularly time consuming.
3807 The problem is that we must either (1) keep the relocs in memory,
3808 which causes the linker to require additional runtime memory or
3809 (2) read the relocs twice from the input file, which wastes time.
3810 This would be a good case for using mmap.
3812 I have no idea how to handle linking PIC code into a file of a
3813 different format. It probably can't be done. */
3814 if ((abfd->flags & DYNAMIC) == 0
3815 && is_elf_hash_table (htab)
3816 && bed->check_relocs != NULL
3817 && elf_object_id (abfd) == elf_hash_table_id (htab)
3818 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3822 for (o = abfd->sections; o != NULL; o = o->next)
3824 Elf_Internal_Rela *internal_relocs;
3827 /* Don't check relocations in excluded sections. */
3828 if ((o->flags & SEC_RELOC) == 0
3829 || (o->flags & SEC_EXCLUDE) != 0
3830 || o->reloc_count == 0
3831 || ((info->strip == strip_all || info->strip == strip_debugger)
3832 && (o->flags & SEC_DEBUGGING) != 0)
3833 || bfd_is_abs_section (o->output_section))
3836 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3838 if (internal_relocs == NULL)
3841 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3843 if (elf_section_data (o)->relocs != internal_relocs)
3844 free (internal_relocs);
3854 /* Add symbols from an ELF object file to the linker hash table. */
3857 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3859 Elf_Internal_Ehdr *ehdr;
3860 Elf_Internal_Shdr *hdr;
3864 struct elf_link_hash_entry **sym_hash;
3865 bfd_boolean dynamic;
3866 Elf_External_Versym *extversym = NULL;
3867 Elf_External_Versym *ever;
3868 struct elf_link_hash_entry *weaks;
3869 struct elf_link_hash_entry **nondeflt_vers = NULL;
3870 size_t nondeflt_vers_cnt = 0;
3871 Elf_Internal_Sym *isymbuf = NULL;
3872 Elf_Internal_Sym *isym;
3873 Elf_Internal_Sym *isymend;
3874 const struct elf_backend_data *bed;
3875 bfd_boolean add_needed;
3876 struct elf_link_hash_table *htab;
3878 void *alloc_mark = NULL;
3879 struct bfd_hash_entry **old_table = NULL;
3880 unsigned int old_size = 0;
3881 unsigned int old_count = 0;
3882 void *old_tab = NULL;
3884 struct bfd_link_hash_entry *old_undefs = NULL;
3885 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3886 void *old_strtab = NULL;
3889 bfd_boolean just_syms;
3891 htab = elf_hash_table (info);
3892 bed = get_elf_backend_data (abfd);
3894 if ((abfd->flags & DYNAMIC) == 0)
3900 /* You can't use -r against a dynamic object. Also, there's no
3901 hope of using a dynamic object which does not exactly match
3902 the format of the output file. */
3903 if (bfd_link_relocatable (info)
3904 || !is_elf_hash_table (htab)
3905 || info->output_bfd->xvec != abfd->xvec)
3907 if (bfd_link_relocatable (info))
3908 bfd_set_error (bfd_error_invalid_operation);
3910 bfd_set_error (bfd_error_wrong_format);
3915 ehdr = elf_elfheader (abfd);
3916 if (info->warn_alternate_em
3917 && bed->elf_machine_code != ehdr->e_machine
3918 && ((bed->elf_machine_alt1 != 0
3919 && ehdr->e_machine == bed->elf_machine_alt1)
3920 || (bed->elf_machine_alt2 != 0
3921 && ehdr->e_machine == bed->elf_machine_alt2)))
3923 /* xgettext:c-format */
3924 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3925 ehdr->e_machine, abfd, bed->elf_machine_code);
3927 /* As a GNU extension, any input sections which are named
3928 .gnu.warning.SYMBOL are treated as warning symbols for the given
3929 symbol. This differs from .gnu.warning sections, which generate
3930 warnings when they are included in an output file. */
3931 /* PR 12761: Also generate this warning when building shared libraries. */
3932 for (s = abfd->sections; s != NULL; s = s->next)
3936 name = bfd_get_section_name (abfd, s);
3937 if (CONST_STRNEQ (name, ".gnu.warning."))
3942 name += sizeof ".gnu.warning." - 1;
3944 /* If this is a shared object, then look up the symbol
3945 in the hash table. If it is there, and it is already
3946 been defined, then we will not be using the entry
3947 from this shared object, so we don't need to warn.
3948 FIXME: If we see the definition in a regular object
3949 later on, we will warn, but we shouldn't. The only
3950 fix is to keep track of what warnings we are supposed
3951 to emit, and then handle them all at the end of the
3955 struct elf_link_hash_entry *h;
3957 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3959 /* FIXME: What about bfd_link_hash_common? */
3961 && (h->root.type == bfd_link_hash_defined
3962 || h->root.type == bfd_link_hash_defweak))
3967 msg = (char *) bfd_alloc (abfd, sz + 1);
3971 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3976 if (! (_bfd_generic_link_add_one_symbol
3977 (info, abfd, name, BSF_WARNING, s, 0, msg,
3978 FALSE, bed->collect, NULL)))
3981 if (bfd_link_executable (info))
3983 /* Clobber the section size so that the warning does
3984 not get copied into the output file. */
3987 /* Also set SEC_EXCLUDE, so that symbols defined in
3988 the warning section don't get copied to the output. */
3989 s->flags |= SEC_EXCLUDE;
3994 just_syms = ((s = abfd->sections) != NULL
3995 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4000 /* If we are creating a shared library, create all the dynamic
4001 sections immediately. We need to attach them to something,
4002 so we attach them to this BFD, provided it is the right
4003 format and is not from ld --just-symbols. Always create the
4004 dynamic sections for -E/--dynamic-list. FIXME: If there
4005 are no input BFD's of the same format as the output, we can't
4006 make a shared library. */
4008 && (bfd_link_pic (info)
4009 || (!bfd_link_relocatable (info)
4011 && (info->export_dynamic || info->dynamic)))
4012 && is_elf_hash_table (htab)
4013 && info->output_bfd->xvec == abfd->xvec
4014 && !htab->dynamic_sections_created)
4016 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4020 else if (!is_elf_hash_table (htab))
4024 const char *soname = NULL;
4026 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4027 const Elf_Internal_Phdr *phdr;
4030 /* ld --just-symbols and dynamic objects don't mix very well.
4031 ld shouldn't allow it. */
4035 /* If this dynamic lib was specified on the command line with
4036 --as-needed in effect, then we don't want to add a DT_NEEDED
4037 tag unless the lib is actually used. Similary for libs brought
4038 in by another lib's DT_NEEDED. When --no-add-needed is used
4039 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4040 any dynamic library in DT_NEEDED tags in the dynamic lib at
4042 add_needed = (elf_dyn_lib_class (abfd)
4043 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4044 | DYN_NO_NEEDED)) == 0;
4046 s = bfd_get_section_by_name (abfd, ".dynamic");
4051 unsigned int elfsec;
4052 unsigned long shlink;
4054 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4061 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4062 if (elfsec == SHN_BAD)
4063 goto error_free_dyn;
4064 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4066 for (extdyn = dynbuf;
4067 extdyn < dynbuf + s->size;
4068 extdyn += bed->s->sizeof_dyn)
4070 Elf_Internal_Dyn dyn;
4072 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4073 if (dyn.d_tag == DT_SONAME)
4075 unsigned int tagv = dyn.d_un.d_val;
4076 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4078 goto error_free_dyn;
4080 if (dyn.d_tag == DT_NEEDED)
4082 struct bfd_link_needed_list *n, **pn;
4084 unsigned int tagv = dyn.d_un.d_val;
4086 amt = sizeof (struct bfd_link_needed_list);
4087 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4088 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4089 if (n == NULL || fnm == NULL)
4090 goto error_free_dyn;
4091 amt = strlen (fnm) + 1;
4092 anm = (char *) bfd_alloc (abfd, amt);
4094 goto error_free_dyn;
4095 memcpy (anm, fnm, amt);
4099 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4103 if (dyn.d_tag == DT_RUNPATH)
4105 struct bfd_link_needed_list *n, **pn;
4107 unsigned int tagv = dyn.d_un.d_val;
4109 amt = sizeof (struct bfd_link_needed_list);
4110 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4111 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4112 if (n == NULL || fnm == NULL)
4113 goto error_free_dyn;
4114 amt = strlen (fnm) + 1;
4115 anm = (char *) bfd_alloc (abfd, amt);
4117 goto error_free_dyn;
4118 memcpy (anm, fnm, amt);
4122 for (pn = & runpath;
4128 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4129 if (!runpath && dyn.d_tag == DT_RPATH)
4131 struct bfd_link_needed_list *n, **pn;
4133 unsigned int tagv = dyn.d_un.d_val;
4135 amt = sizeof (struct bfd_link_needed_list);
4136 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4137 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4138 if (n == NULL || fnm == NULL)
4139 goto error_free_dyn;
4140 amt = strlen (fnm) + 1;
4141 anm = (char *) bfd_alloc (abfd, amt);
4143 goto error_free_dyn;
4144 memcpy (anm, fnm, amt);
4154 if (dyn.d_tag == DT_AUDIT)
4156 unsigned int tagv = dyn.d_un.d_val;
4157 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4164 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4165 frees all more recently bfd_alloc'd blocks as well. */
4171 struct bfd_link_needed_list **pn;
4172 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4177 /* If we have a PT_GNU_RELRO program header, mark as read-only
4178 all sections contained fully therein. This makes relro
4179 shared library sections appear as they will at run-time. */
4180 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4181 while (--phdr >= elf_tdata (abfd)->phdr)
4182 if (phdr->p_type == PT_GNU_RELRO)
4184 for (s = abfd->sections; s != NULL; s = s->next)
4185 if ((s->flags & SEC_ALLOC) != 0
4186 && s->vma >= phdr->p_vaddr
4187 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4188 s->flags |= SEC_READONLY;
4192 /* We do not want to include any of the sections in a dynamic
4193 object in the output file. We hack by simply clobbering the
4194 list of sections in the BFD. This could be handled more
4195 cleanly by, say, a new section flag; the existing
4196 SEC_NEVER_LOAD flag is not the one we want, because that one
4197 still implies that the section takes up space in the output
4199 bfd_section_list_clear (abfd);
4201 /* Find the name to use in a DT_NEEDED entry that refers to this
4202 object. If the object has a DT_SONAME entry, we use it.
4203 Otherwise, if the generic linker stuck something in
4204 elf_dt_name, we use that. Otherwise, we just use the file
4206 if (soname == NULL || *soname == '\0')
4208 soname = elf_dt_name (abfd);
4209 if (soname == NULL || *soname == '\0')
4210 soname = bfd_get_filename (abfd);
4213 /* Save the SONAME because sometimes the linker emulation code
4214 will need to know it. */
4215 elf_dt_name (abfd) = soname;
4217 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4221 /* If we have already included this dynamic object in the
4222 link, just ignore it. There is no reason to include a
4223 particular dynamic object more than once. */
4227 /* Save the DT_AUDIT entry for the linker emulation code. */
4228 elf_dt_audit (abfd) = audit;
4231 /* If this is a dynamic object, we always link against the .dynsym
4232 symbol table, not the .symtab symbol table. The dynamic linker
4233 will only see the .dynsym symbol table, so there is no reason to
4234 look at .symtab for a dynamic object. */
4236 if (! dynamic || elf_dynsymtab (abfd) == 0)
4237 hdr = &elf_tdata (abfd)->symtab_hdr;
4239 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4241 symcount = hdr->sh_size / bed->s->sizeof_sym;
4243 /* The sh_info field of the symtab header tells us where the
4244 external symbols start. We don't care about the local symbols at
4246 if (elf_bad_symtab (abfd))
4248 extsymcount = symcount;
4253 extsymcount = symcount - hdr->sh_info;
4254 extsymoff = hdr->sh_info;
4257 sym_hash = elf_sym_hashes (abfd);
4258 if (extsymcount != 0)
4260 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4262 if (isymbuf == NULL)
4265 if (sym_hash == NULL)
4267 /* We store a pointer to the hash table entry for each
4270 amt *= sizeof (struct elf_link_hash_entry *);
4271 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4272 if (sym_hash == NULL)
4273 goto error_free_sym;
4274 elf_sym_hashes (abfd) = sym_hash;
4280 /* Read in any version definitions. */
4281 if (!_bfd_elf_slurp_version_tables (abfd,
4282 info->default_imported_symver))
4283 goto error_free_sym;
4285 /* Read in the symbol versions, but don't bother to convert them
4286 to internal format. */
4287 if (elf_dynversym (abfd) != 0)
4289 Elf_Internal_Shdr *versymhdr;
4291 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4292 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4293 if (extversym == NULL)
4294 goto error_free_sym;
4295 amt = versymhdr->sh_size;
4296 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4297 || bfd_bread (extversym, amt, abfd) != amt)
4298 goto error_free_vers;
4302 /* If we are loading an as-needed shared lib, save the symbol table
4303 state before we start adding symbols. If the lib turns out
4304 to be unneeded, restore the state. */
4305 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4310 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4312 struct bfd_hash_entry *p;
4313 struct elf_link_hash_entry *h;
4315 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4317 h = (struct elf_link_hash_entry *) p;
4318 entsize += htab->root.table.entsize;
4319 if (h->root.type == bfd_link_hash_warning)
4320 entsize += htab->root.table.entsize;
4324 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4325 old_tab = bfd_malloc (tabsize + entsize);
4326 if (old_tab == NULL)
4327 goto error_free_vers;
4329 /* Remember the current objalloc pointer, so that all mem for
4330 symbols added can later be reclaimed. */
4331 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4332 if (alloc_mark == NULL)
4333 goto error_free_vers;
4335 /* Make a special call to the linker "notice" function to
4336 tell it that we are about to handle an as-needed lib. */
4337 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4338 goto error_free_vers;
4340 /* Clone the symbol table. Remember some pointers into the
4341 symbol table, and dynamic symbol count. */
4342 old_ent = (char *) old_tab + tabsize;
4343 memcpy (old_tab, htab->root.table.table, tabsize);
4344 old_undefs = htab->root.undefs;
4345 old_undefs_tail = htab->root.undefs_tail;
4346 old_table = htab->root.table.table;
4347 old_size = htab->root.table.size;
4348 old_count = htab->root.table.count;
4349 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4350 if (old_strtab == NULL)
4351 goto error_free_vers;
4353 for (i = 0; i < htab->root.table.size; i++)
4355 struct bfd_hash_entry *p;
4356 struct elf_link_hash_entry *h;
4358 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4360 memcpy (old_ent, p, htab->root.table.entsize);
4361 old_ent = (char *) old_ent + htab->root.table.entsize;
4362 h = (struct elf_link_hash_entry *) p;
4363 if (h->root.type == bfd_link_hash_warning)
4365 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4366 old_ent = (char *) old_ent + htab->root.table.entsize;
4373 ever = extversym != NULL ? extversym + extsymoff : NULL;
4374 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4376 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4380 asection *sec, *new_sec;
4383 struct elf_link_hash_entry *h;
4384 struct elf_link_hash_entry *hi;
4385 bfd_boolean definition;
4386 bfd_boolean size_change_ok;
4387 bfd_boolean type_change_ok;
4388 bfd_boolean new_weak;
4389 bfd_boolean old_weak;
4390 bfd_boolean override;
4392 bfd_boolean discarded;
4393 unsigned int old_alignment;
4395 bfd_boolean matched;
4399 flags = BSF_NO_FLAGS;
4401 value = isym->st_value;
4402 common = bed->common_definition (isym);
4403 if (common && info->inhibit_common_definition)
4405 /* Treat common symbol as undefined for --no-define-common. */
4406 isym->st_shndx = SHN_UNDEF;
4411 bind = ELF_ST_BIND (isym->st_info);
4415 /* This should be impossible, since ELF requires that all
4416 global symbols follow all local symbols, and that sh_info
4417 point to the first global symbol. Unfortunately, Irix 5
4422 if (isym->st_shndx != SHN_UNDEF && !common)
4430 case STB_GNU_UNIQUE:
4431 flags = BSF_GNU_UNIQUE;
4435 /* Leave it up to the processor backend. */
4439 if (isym->st_shndx == SHN_UNDEF)
4440 sec = bfd_und_section_ptr;
4441 else if (isym->st_shndx == SHN_ABS)
4442 sec = bfd_abs_section_ptr;
4443 else if (isym->st_shndx == SHN_COMMON)
4445 sec = bfd_com_section_ptr;
4446 /* What ELF calls the size we call the value. What ELF
4447 calls the value we call the alignment. */
4448 value = isym->st_size;
4452 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4454 sec = bfd_abs_section_ptr;
4455 else if (discarded_section (sec))
4457 /* Symbols from discarded section are undefined. We keep
4459 sec = bfd_und_section_ptr;
4461 isym->st_shndx = SHN_UNDEF;
4463 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4467 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4470 goto error_free_vers;
4472 if (isym->st_shndx == SHN_COMMON
4473 && (abfd->flags & BFD_PLUGIN) != 0)
4475 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4479 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4481 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4483 goto error_free_vers;
4487 else if (isym->st_shndx == SHN_COMMON
4488 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4489 && !bfd_link_relocatable (info))
4491 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4495 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4496 | SEC_LINKER_CREATED);
4497 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4499 goto error_free_vers;
4503 else if (bed->elf_add_symbol_hook)
4505 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4507 goto error_free_vers;
4509 /* The hook function sets the name to NULL if this symbol
4510 should be skipped for some reason. */
4515 /* Sanity check that all possibilities were handled. */
4518 bfd_set_error (bfd_error_bad_value);
4519 goto error_free_vers;
4522 /* Silently discard TLS symbols from --just-syms. There's
4523 no way to combine a static TLS block with a new TLS block
4524 for this executable. */
4525 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4526 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4529 if (bfd_is_und_section (sec)
4530 || bfd_is_com_section (sec))
4535 size_change_ok = FALSE;
4536 type_change_ok = bed->type_change_ok;
4543 if (is_elf_hash_table (htab))
4545 Elf_Internal_Versym iver;
4546 unsigned int vernum = 0;
4551 if (info->default_imported_symver)
4552 /* Use the default symbol version created earlier. */
4553 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4558 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4560 vernum = iver.vs_vers & VERSYM_VERSION;
4562 /* If this is a hidden symbol, or if it is not version
4563 1, we append the version name to the symbol name.
4564 However, we do not modify a non-hidden absolute symbol
4565 if it is not a function, because it might be the version
4566 symbol itself. FIXME: What if it isn't? */
4567 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4569 && (!bfd_is_abs_section (sec)
4570 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4573 size_t namelen, verlen, newlen;
4576 if (isym->st_shndx != SHN_UNDEF)
4578 if (vernum > elf_tdata (abfd)->cverdefs)
4580 else if (vernum > 1)
4582 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4589 /* xgettext:c-format */
4590 (_("%pB: %s: invalid version %u (max %d)"),
4592 elf_tdata (abfd)->cverdefs);
4593 bfd_set_error (bfd_error_bad_value);
4594 goto error_free_vers;
4599 /* We cannot simply test for the number of
4600 entries in the VERNEED section since the
4601 numbers for the needed versions do not start
4603 Elf_Internal_Verneed *t;
4606 for (t = elf_tdata (abfd)->verref;
4610 Elf_Internal_Vernaux *a;
4612 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4614 if (a->vna_other == vernum)
4616 verstr = a->vna_nodename;
4626 /* xgettext:c-format */
4627 (_("%pB: %s: invalid needed version %d"),
4628 abfd, name, vernum);
4629 bfd_set_error (bfd_error_bad_value);
4630 goto error_free_vers;
4634 namelen = strlen (name);
4635 verlen = strlen (verstr);
4636 newlen = namelen + verlen + 2;
4637 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4638 && isym->st_shndx != SHN_UNDEF)
4641 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4642 if (newname == NULL)
4643 goto error_free_vers;
4644 memcpy (newname, name, namelen);
4645 p = newname + namelen;
4647 /* If this is a defined non-hidden version symbol,
4648 we add another @ to the name. This indicates the
4649 default version of the symbol. */
4650 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4651 && isym->st_shndx != SHN_UNDEF)
4653 memcpy (p, verstr, verlen + 1);
4658 /* If this symbol has default visibility and the user has
4659 requested we not re-export it, then mark it as hidden. */
4660 if (!bfd_is_und_section (sec)
4663 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4664 isym->st_other = (STV_HIDDEN
4665 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4667 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4668 sym_hash, &old_bfd, &old_weak,
4669 &old_alignment, &skip, &override,
4670 &type_change_ok, &size_change_ok,
4672 goto error_free_vers;
4677 /* Override a definition only if the new symbol matches the
4679 if (override && matched)
4683 while (h->root.type == bfd_link_hash_indirect
4684 || h->root.type == bfd_link_hash_warning)
4685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4687 if (elf_tdata (abfd)->verdef != NULL
4690 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4693 if (! (_bfd_generic_link_add_one_symbol
4694 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4695 (struct bfd_link_hash_entry **) sym_hash)))
4696 goto error_free_vers;
4698 if ((abfd->flags & DYNAMIC) == 0
4699 && (bfd_get_flavour (info->output_bfd)
4700 == bfd_target_elf_flavour))
4702 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4703 elf_tdata (info->output_bfd)->has_gnu_symbols
4704 |= elf_gnu_symbol_ifunc;
4705 if ((flags & BSF_GNU_UNIQUE))
4706 elf_tdata (info->output_bfd)->has_gnu_symbols
4707 |= elf_gnu_symbol_unique;
4711 /* We need to make sure that indirect symbol dynamic flags are
4714 while (h->root.type == bfd_link_hash_indirect
4715 || h->root.type == bfd_link_hash_warning)
4716 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4718 /* Setting the index to -3 tells elf_link_output_extsym that
4719 this symbol is defined in a discarded section. */
4725 new_weak = (flags & BSF_WEAK) != 0;
4729 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4730 && is_elf_hash_table (htab)
4731 && h->u.alias == NULL)
4733 /* Keep a list of all weak defined non function symbols from
4734 a dynamic object, using the alias field. Later in this
4735 function we will set the alias field to the correct
4736 value. We only put non-function symbols from dynamic
4737 objects on this list, because that happens to be the only
4738 time we need to know the normal symbol corresponding to a
4739 weak symbol, and the information is time consuming to
4740 figure out. If the alias field is not already NULL,
4741 then this symbol was already defined by some previous
4742 dynamic object, and we will be using that previous
4743 definition anyhow. */
4749 /* Set the alignment of a common symbol. */
4750 if ((common || bfd_is_com_section (sec))
4751 && h->root.type == bfd_link_hash_common)
4756 align = bfd_log2 (isym->st_value);
4759 /* The new symbol is a common symbol in a shared object.
4760 We need to get the alignment from the section. */
4761 align = new_sec->alignment_power;
4763 if (align > old_alignment)
4764 h->root.u.c.p->alignment_power = align;
4766 h->root.u.c.p->alignment_power = old_alignment;
4769 if (is_elf_hash_table (htab))
4771 /* Set a flag in the hash table entry indicating the type of
4772 reference or definition we just found. A dynamic symbol
4773 is one which is referenced or defined by both a regular
4774 object and a shared object. */
4775 bfd_boolean dynsym = FALSE;
4777 /* Plugin symbols aren't normal. Don't set def_regular or
4778 ref_regular for them, or make them dynamic. */
4779 if ((abfd->flags & BFD_PLUGIN) != 0)
4786 if (bind != STB_WEAK)
4787 h->ref_regular_nonweak = 1;
4799 /* If the indirect symbol has been forced local, don't
4800 make the real symbol dynamic. */
4801 if ((h == hi || !hi->forced_local)
4802 && (bfd_link_dll (info)
4812 hi->ref_dynamic = 1;
4817 hi->def_dynamic = 1;
4820 /* If the indirect symbol has been forced local, don't
4821 make the real symbol dynamic. */
4822 if ((h == hi || !hi->forced_local)
4826 && weakdef (h)->dynindx != -1)))
4830 /* Check to see if we need to add an indirect symbol for
4831 the default name. */
4833 || (!override && h->root.type == bfd_link_hash_common))
4834 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4835 sec, value, &old_bfd, &dynsym))
4836 goto error_free_vers;
4838 /* Check the alignment when a common symbol is involved. This
4839 can change when a common symbol is overridden by a normal
4840 definition or a common symbol is ignored due to the old
4841 normal definition. We need to make sure the maximum
4842 alignment is maintained. */
4843 if ((old_alignment || common)
4844 && h->root.type != bfd_link_hash_common)
4846 unsigned int common_align;
4847 unsigned int normal_align;
4848 unsigned int symbol_align;
4852 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4853 || h->root.type == bfd_link_hash_defweak);
4855 symbol_align = ffs (h->root.u.def.value) - 1;
4856 if (h->root.u.def.section->owner != NULL
4857 && (h->root.u.def.section->owner->flags
4858 & (DYNAMIC | BFD_PLUGIN)) == 0)
4860 normal_align = h->root.u.def.section->alignment_power;
4861 if (normal_align > symbol_align)
4862 normal_align = symbol_align;
4865 normal_align = symbol_align;
4869 common_align = old_alignment;
4870 common_bfd = old_bfd;
4875 common_align = bfd_log2 (isym->st_value);
4877 normal_bfd = old_bfd;
4880 if (normal_align < common_align)
4882 /* PR binutils/2735 */
4883 if (normal_bfd == NULL)
4885 /* xgettext:c-format */
4886 (_("warning: alignment %u of common symbol `%s' in %pB is"
4887 " greater than the alignment (%u) of its section %pA"),
4888 1 << common_align, name, common_bfd,
4889 1 << normal_align, h->root.u.def.section);
4892 /* xgettext:c-format */
4893 (_("warning: alignment %u of symbol `%s' in %pB"
4894 " is smaller than %u in %pB"),
4895 1 << normal_align, name, normal_bfd,
4896 1 << common_align, common_bfd);
4900 /* Remember the symbol size if it isn't undefined. */
4901 if (isym->st_size != 0
4902 && isym->st_shndx != SHN_UNDEF
4903 && (definition || h->size == 0))
4906 && h->size != isym->st_size
4907 && ! size_change_ok)
4909 /* xgettext:c-format */
4910 (_("warning: size of symbol `%s' changed"
4911 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4912 name, (uint64_t) h->size, old_bfd,
4913 (uint64_t) isym->st_size, abfd);
4915 h->size = isym->st_size;
4918 /* If this is a common symbol, then we always want H->SIZE
4919 to be the size of the common symbol. The code just above
4920 won't fix the size if a common symbol becomes larger. We
4921 don't warn about a size change here, because that is
4922 covered by --warn-common. Allow changes between different
4924 if (h->root.type == bfd_link_hash_common)
4925 h->size = h->root.u.c.size;
4927 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4928 && ((definition && !new_weak)
4929 || (old_weak && h->root.type == bfd_link_hash_common)
4930 || h->type == STT_NOTYPE))
4932 unsigned int type = ELF_ST_TYPE (isym->st_info);
4934 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4936 if (type == STT_GNU_IFUNC
4937 && (abfd->flags & DYNAMIC) != 0)
4940 if (h->type != type)
4942 if (h->type != STT_NOTYPE && ! type_change_ok)
4943 /* xgettext:c-format */
4945 (_("warning: type of symbol `%s' changed"
4946 " from %d to %d in %pB"),
4947 name, h->type, type, abfd);
4953 /* Merge st_other field. */
4954 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4956 /* We don't want to make debug symbol dynamic. */
4958 && (sec->flags & SEC_DEBUGGING)
4959 && !bfd_link_relocatable (info))
4962 /* Nor should we make plugin symbols dynamic. */
4963 if ((abfd->flags & BFD_PLUGIN) != 0)
4968 h->target_internal = isym->st_target_internal;
4969 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4972 if (definition && !dynamic)
4974 char *p = strchr (name, ELF_VER_CHR);
4975 if (p != NULL && p[1] != ELF_VER_CHR)
4977 /* Queue non-default versions so that .symver x, x@FOO
4978 aliases can be checked. */
4981 amt = ((isymend - isym + 1)
4982 * sizeof (struct elf_link_hash_entry *));
4984 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4986 goto error_free_vers;
4988 nondeflt_vers[nondeflt_vers_cnt++] = h;
4992 if (dynsym && h->dynindx == -1)
4994 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4995 goto error_free_vers;
4997 && weakdef (h)->dynindx == -1)
4999 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5000 goto error_free_vers;
5003 else if (h->dynindx != -1)
5004 /* If the symbol already has a dynamic index, but
5005 visibility says it should not be visible, turn it into
5007 switch (ELF_ST_VISIBILITY (h->other))
5011 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5016 /* Don't add DT_NEEDED for references from the dummy bfd nor
5017 for unmatched symbol. */
5022 && h->ref_regular_nonweak
5024 || (old_bfd->flags & BFD_PLUGIN) == 0))
5025 || (h->ref_dynamic_nonweak
5026 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5027 && !on_needed_list (elf_dt_name (abfd),
5028 htab->needed, NULL))))
5031 const char *soname = elf_dt_name (abfd);
5033 info->callbacks->minfo ("%!", soname, old_bfd,
5034 h->root.root.string);
5036 /* A symbol from a library loaded via DT_NEEDED of some
5037 other library is referenced by a regular object.
5038 Add a DT_NEEDED entry for it. Issue an error if
5039 --no-add-needed is used and the reference was not
5042 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5045 /* xgettext:c-format */
5046 (_("%pB: undefined reference to symbol '%s'"),
5048 bfd_set_error (bfd_error_missing_dso);
5049 goto error_free_vers;
5052 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5053 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5056 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5058 goto error_free_vers;
5060 BFD_ASSERT (ret == 0);
5065 if (info->lto_plugin_active
5066 && !bfd_link_relocatable (info)
5067 && (abfd->flags & BFD_PLUGIN) == 0
5073 if (bed->s->arch_size == 32)
5078 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5079 referenced in regular objects so that linker plugin will get
5080 the correct symbol resolution. */
5082 sym_hash = elf_sym_hashes (abfd);
5083 for (s = abfd->sections; s != NULL; s = s->next)
5085 Elf_Internal_Rela *internal_relocs;
5086 Elf_Internal_Rela *rel, *relend;
5088 /* Don't check relocations in excluded sections. */
5089 if ((s->flags & SEC_RELOC) == 0
5090 || s->reloc_count == 0
5091 || (s->flags & SEC_EXCLUDE) != 0
5092 || ((info->strip == strip_all
5093 || info->strip == strip_debugger)
5094 && (s->flags & SEC_DEBUGGING) != 0))
5097 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5100 if (internal_relocs == NULL)
5101 goto error_free_vers;
5103 rel = internal_relocs;
5104 relend = rel + s->reloc_count;
5105 for ( ; rel < relend; rel++)
5107 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5108 struct elf_link_hash_entry *h;
5110 /* Skip local symbols. */
5111 if (r_symndx < extsymoff)
5114 h = sym_hash[r_symndx - extsymoff];
5116 h->root.non_ir_ref_regular = 1;
5119 if (elf_section_data (s)->relocs != internal_relocs)
5120 free (internal_relocs);
5124 if (extversym != NULL)
5130 if (isymbuf != NULL)
5136 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5140 /* Restore the symbol table. */
5141 old_ent = (char *) old_tab + tabsize;
5142 memset (elf_sym_hashes (abfd), 0,
5143 extsymcount * sizeof (struct elf_link_hash_entry *));
5144 htab->root.table.table = old_table;
5145 htab->root.table.size = old_size;
5146 htab->root.table.count = old_count;
5147 memcpy (htab->root.table.table, old_tab, tabsize);
5148 htab->root.undefs = old_undefs;
5149 htab->root.undefs_tail = old_undefs_tail;
5150 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5153 for (i = 0; i < htab->root.table.size; i++)
5155 struct bfd_hash_entry *p;
5156 struct elf_link_hash_entry *h;
5158 unsigned int alignment_power;
5159 unsigned int non_ir_ref_dynamic;
5161 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5163 h = (struct elf_link_hash_entry *) p;
5164 if (h->root.type == bfd_link_hash_warning)
5165 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5167 /* Preserve the maximum alignment and size for common
5168 symbols even if this dynamic lib isn't on DT_NEEDED
5169 since it can still be loaded at run time by another
5171 if (h->root.type == bfd_link_hash_common)
5173 size = h->root.u.c.size;
5174 alignment_power = h->root.u.c.p->alignment_power;
5179 alignment_power = 0;
5181 /* Preserve non_ir_ref_dynamic so that this symbol
5182 will be exported when the dynamic lib becomes needed
5183 in the second pass. */
5184 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5185 memcpy (p, old_ent, htab->root.table.entsize);
5186 old_ent = (char *) old_ent + htab->root.table.entsize;
5187 h = (struct elf_link_hash_entry *) p;
5188 if (h->root.type == bfd_link_hash_warning)
5190 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5191 old_ent = (char *) old_ent + htab->root.table.entsize;
5192 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5194 if (h->root.type == bfd_link_hash_common)
5196 if (size > h->root.u.c.size)
5197 h->root.u.c.size = size;
5198 if (alignment_power > h->root.u.c.p->alignment_power)
5199 h->root.u.c.p->alignment_power = alignment_power;
5201 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5205 /* Make a special call to the linker "notice" function to
5206 tell it that symbols added for crefs may need to be removed. */
5207 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5208 goto error_free_vers;
5211 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5213 if (nondeflt_vers != NULL)
5214 free (nondeflt_vers);
5218 if (old_tab != NULL)
5220 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5221 goto error_free_vers;
5226 /* Now that all the symbols from this input file are created, if
5227 not performing a relocatable link, handle .symver foo, foo@BAR
5228 such that any relocs against foo become foo@BAR. */
5229 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5233 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5235 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5236 char *shortname, *p;
5238 p = strchr (h->root.root.string, ELF_VER_CHR);
5240 || (h->root.type != bfd_link_hash_defined
5241 && h->root.type != bfd_link_hash_defweak))
5244 amt = p - h->root.root.string;
5245 shortname = (char *) bfd_malloc (amt + 1);
5247 goto error_free_vers;
5248 memcpy (shortname, h->root.root.string, amt);
5249 shortname[amt] = '\0';
5251 hi = (struct elf_link_hash_entry *)
5252 bfd_link_hash_lookup (&htab->root, shortname,
5253 FALSE, FALSE, FALSE);
5255 && hi->root.type == h->root.type
5256 && hi->root.u.def.value == h->root.u.def.value
5257 && hi->root.u.def.section == h->root.u.def.section)
5259 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5260 hi->root.type = bfd_link_hash_indirect;
5261 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5262 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5263 sym_hash = elf_sym_hashes (abfd);
5265 for (symidx = 0; symidx < extsymcount; ++symidx)
5266 if (sym_hash[symidx] == hi)
5268 sym_hash[symidx] = h;
5274 free (nondeflt_vers);
5275 nondeflt_vers = NULL;
5278 /* Now set the alias field correctly for all the weak defined
5279 symbols we found. The only way to do this is to search all the
5280 symbols. Since we only need the information for non functions in
5281 dynamic objects, that's the only time we actually put anything on
5282 the list WEAKS. We need this information so that if a regular
5283 object refers to a symbol defined weakly in a dynamic object, the
5284 real symbol in the dynamic object is also put in the dynamic
5285 symbols; we also must arrange for both symbols to point to the
5286 same memory location. We could handle the general case of symbol
5287 aliasing, but a general symbol alias can only be generated in
5288 assembler code, handling it correctly would be very time
5289 consuming, and other ELF linkers don't handle general aliasing
5293 struct elf_link_hash_entry **hpp;
5294 struct elf_link_hash_entry **hppend;
5295 struct elf_link_hash_entry **sorted_sym_hash;
5296 struct elf_link_hash_entry *h;
5299 /* Since we have to search the whole symbol list for each weak
5300 defined symbol, search time for N weak defined symbols will be
5301 O(N^2). Binary search will cut it down to O(NlogN). */
5303 amt *= sizeof (struct elf_link_hash_entry *);
5304 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5305 if (sorted_sym_hash == NULL)
5307 sym_hash = sorted_sym_hash;
5308 hpp = elf_sym_hashes (abfd);
5309 hppend = hpp + extsymcount;
5311 for (; hpp < hppend; hpp++)
5315 && h->root.type == bfd_link_hash_defined
5316 && !bed->is_function_type (h->type))
5324 qsort (sorted_sym_hash, sym_count,
5325 sizeof (struct elf_link_hash_entry *),
5328 while (weaks != NULL)
5330 struct elf_link_hash_entry *hlook;
5333 size_t i, j, idx = 0;
5336 weaks = hlook->u.alias;
5337 hlook->u.alias = NULL;
5339 if (hlook->root.type != bfd_link_hash_defined
5340 && hlook->root.type != bfd_link_hash_defweak)
5343 slook = hlook->root.u.def.section;
5344 vlook = hlook->root.u.def.value;
5350 bfd_signed_vma vdiff;
5352 h = sorted_sym_hash[idx];
5353 vdiff = vlook - h->root.u.def.value;
5360 int sdiff = slook->id - h->root.u.def.section->id;
5370 /* We didn't find a value/section match. */
5374 /* With multiple aliases, or when the weak symbol is already
5375 strongly defined, we have multiple matching symbols and
5376 the binary search above may land on any of them. Step
5377 one past the matching symbol(s). */
5380 h = sorted_sym_hash[idx];
5381 if (h->root.u.def.section != slook
5382 || h->root.u.def.value != vlook)
5386 /* Now look back over the aliases. Since we sorted by size
5387 as well as value and section, we'll choose the one with
5388 the largest size. */
5391 h = sorted_sym_hash[idx];
5393 /* Stop if value or section doesn't match. */
5394 if (h->root.u.def.section != slook
5395 || h->root.u.def.value != vlook)
5397 else if (h != hlook)
5399 struct elf_link_hash_entry *t;
5402 hlook->is_weakalias = 1;
5404 if (t->u.alias != NULL)
5405 while (t->u.alias != h)
5409 /* If the weak definition is in the list of dynamic
5410 symbols, make sure the real definition is put
5412 if (hlook->dynindx != -1 && h->dynindx == -1)
5414 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5417 free (sorted_sym_hash);
5422 /* If the real definition is in the list of dynamic
5423 symbols, make sure the weak definition is put
5424 there as well. If we don't do this, then the
5425 dynamic loader might not merge the entries for the
5426 real definition and the weak definition. */
5427 if (h->dynindx != -1 && hlook->dynindx == -1)
5429 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5430 goto err_free_sym_hash;
5437 free (sorted_sym_hash);
5440 if (bed->check_directives
5441 && !(*bed->check_directives) (abfd, info))
5444 /* If this is a non-traditional link, try to optimize the handling
5445 of the .stab/.stabstr sections. */
5447 && ! info->traditional_format
5448 && is_elf_hash_table (htab)
5449 && (info->strip != strip_all && info->strip != strip_debugger))
5453 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5454 if (stabstr != NULL)
5456 bfd_size_type string_offset = 0;
5459 for (stab = abfd->sections; stab; stab = stab->next)
5460 if (CONST_STRNEQ (stab->name, ".stab")
5461 && (!stab->name[5] ||
5462 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5463 && (stab->flags & SEC_MERGE) == 0
5464 && !bfd_is_abs_section (stab->output_section))
5466 struct bfd_elf_section_data *secdata;
5468 secdata = elf_section_data (stab);
5469 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5470 stabstr, &secdata->sec_info,
5473 if (secdata->sec_info)
5474 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5479 if (is_elf_hash_table (htab) && add_needed)
5481 /* Add this bfd to the loaded list. */
5482 struct elf_link_loaded_list *n;
5484 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5488 n->next = htab->loaded;
5495 if (old_tab != NULL)
5497 if (old_strtab != NULL)
5499 if (nondeflt_vers != NULL)
5500 free (nondeflt_vers);
5501 if (extversym != NULL)
5504 if (isymbuf != NULL)
5510 /* Return the linker hash table entry of a symbol that might be
5511 satisfied by an archive symbol. Return -1 on error. */
5513 struct elf_link_hash_entry *
5514 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5515 struct bfd_link_info *info,
5518 struct elf_link_hash_entry *h;
5522 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5526 /* If this is a default version (the name contains @@), look up the
5527 symbol again with only one `@' as well as without the version.
5528 The effect is that references to the symbol with and without the
5529 version will be matched by the default symbol in the archive. */
5531 p = strchr (name, ELF_VER_CHR);
5532 if (p == NULL || p[1] != ELF_VER_CHR)
5535 /* First check with only one `@'. */
5536 len = strlen (name);
5537 copy = (char *) bfd_alloc (abfd, len);
5539 return (struct elf_link_hash_entry *) -1;
5541 first = p - name + 1;
5542 memcpy (copy, name, first);
5543 memcpy (copy + first, name + first + 1, len - first);
5545 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5548 /* We also need to check references to the symbol without the
5550 copy[first - 1] = '\0';
5551 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5552 FALSE, FALSE, TRUE);
5555 bfd_release (abfd, copy);
5559 /* Add symbols from an ELF archive file to the linker hash table. We
5560 don't use _bfd_generic_link_add_archive_symbols because we need to
5561 handle versioned symbols.
5563 Fortunately, ELF archive handling is simpler than that done by
5564 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5565 oddities. In ELF, if we find a symbol in the archive map, and the
5566 symbol is currently undefined, we know that we must pull in that
5569 Unfortunately, we do have to make multiple passes over the symbol
5570 table until nothing further is resolved. */
5573 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5576 unsigned char *included = NULL;
5580 const struct elf_backend_data *bed;
5581 struct elf_link_hash_entry * (*archive_symbol_lookup)
5582 (bfd *, struct bfd_link_info *, const char *);
5584 if (! bfd_has_map (abfd))
5586 /* An empty archive is a special case. */
5587 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5589 bfd_set_error (bfd_error_no_armap);
5593 /* Keep track of all symbols we know to be already defined, and all
5594 files we know to be already included. This is to speed up the
5595 second and subsequent passes. */
5596 c = bfd_ardata (abfd)->symdef_count;
5600 amt *= sizeof (*included);
5601 included = (unsigned char *) bfd_zmalloc (amt);
5602 if (included == NULL)
5605 symdefs = bfd_ardata (abfd)->symdefs;
5606 bed = get_elf_backend_data (abfd);
5607 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5620 symdefend = symdef + c;
5621 for (i = 0; symdef < symdefend; symdef++, i++)
5623 struct elf_link_hash_entry *h;
5625 struct bfd_link_hash_entry *undefs_tail;
5630 if (symdef->file_offset == last)
5636 h = archive_symbol_lookup (abfd, info, symdef->name);
5637 if (h == (struct elf_link_hash_entry *) -1)
5643 if (h->root.type == bfd_link_hash_common)
5645 /* We currently have a common symbol. The archive map contains
5646 a reference to this symbol, so we may want to include it. We
5647 only want to include it however, if this archive element
5648 contains a definition of the symbol, not just another common
5651 Unfortunately some archivers (including GNU ar) will put
5652 declarations of common symbols into their archive maps, as
5653 well as real definitions, so we cannot just go by the archive
5654 map alone. Instead we must read in the element's symbol
5655 table and check that to see what kind of symbol definition
5657 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5660 else if (h->root.type != bfd_link_hash_undefined)
5662 if (h->root.type != bfd_link_hash_undefweak)
5663 /* Symbol must be defined. Don't check it again. */
5668 /* We need to include this archive member. */
5669 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5670 if (element == NULL)
5673 if (! bfd_check_format (element, bfd_object))
5676 undefs_tail = info->hash->undefs_tail;
5678 if (!(*info->callbacks
5679 ->add_archive_element) (info, element, symdef->name, &element))
5681 if (!bfd_link_add_symbols (element, info))
5684 /* If there are any new undefined symbols, we need to make
5685 another pass through the archive in order to see whether
5686 they can be defined. FIXME: This isn't perfect, because
5687 common symbols wind up on undefs_tail and because an
5688 undefined symbol which is defined later on in this pass
5689 does not require another pass. This isn't a bug, but it
5690 does make the code less efficient than it could be. */
5691 if (undefs_tail != info->hash->undefs_tail)
5694 /* Look backward to mark all symbols from this object file
5695 which we have already seen in this pass. */
5699 included[mark] = TRUE;
5704 while (symdefs[mark].file_offset == symdef->file_offset);
5706 /* We mark subsequent symbols from this object file as we go
5707 on through the loop. */
5708 last = symdef->file_offset;
5718 if (included != NULL)
5723 /* Given an ELF BFD, add symbols to the global hash table as
5727 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5729 switch (bfd_get_format (abfd))
5732 return elf_link_add_object_symbols (abfd, info);
5734 return elf_link_add_archive_symbols (abfd, info);
5736 bfd_set_error (bfd_error_wrong_format);
5741 struct hash_codes_info
5743 unsigned long *hashcodes;
5747 /* This function will be called though elf_link_hash_traverse to store
5748 all hash value of the exported symbols in an array. */
5751 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5753 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5758 /* Ignore indirect symbols. These are added by the versioning code. */
5759 if (h->dynindx == -1)
5762 name = h->root.root.string;
5763 if (h->versioned >= versioned)
5765 char *p = strchr (name, ELF_VER_CHR);
5768 alc = (char *) bfd_malloc (p - name + 1);
5774 memcpy (alc, name, p - name);
5775 alc[p - name] = '\0';
5780 /* Compute the hash value. */
5781 ha = bfd_elf_hash (name);
5783 /* Store the found hash value in the array given as the argument. */
5784 *(inf->hashcodes)++ = ha;
5786 /* And store it in the struct so that we can put it in the hash table
5788 h->u.elf_hash_value = ha;
5796 struct collect_gnu_hash_codes
5799 const struct elf_backend_data *bed;
5800 unsigned long int nsyms;
5801 unsigned long int maskbits;
5802 unsigned long int *hashcodes;
5803 unsigned long int *hashval;
5804 unsigned long int *indx;
5805 unsigned long int *counts;
5808 long int min_dynindx;
5809 unsigned long int bucketcount;
5810 unsigned long int symindx;
5811 long int local_indx;
5812 long int shift1, shift2;
5813 unsigned long int mask;
5817 /* This function will be called though elf_link_hash_traverse to store
5818 all hash value of the exported symbols in an array. */
5821 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5823 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5828 /* Ignore indirect symbols. These are added by the versioning code. */
5829 if (h->dynindx == -1)
5832 /* Ignore also local symbols and undefined symbols. */
5833 if (! (*s->bed->elf_hash_symbol) (h))
5836 name = h->root.root.string;
5837 if (h->versioned >= versioned)
5839 char *p = strchr (name, ELF_VER_CHR);
5842 alc = (char *) bfd_malloc (p - name + 1);
5848 memcpy (alc, name, p - name);
5849 alc[p - name] = '\0';
5854 /* Compute the hash value. */
5855 ha = bfd_elf_gnu_hash (name);
5857 /* Store the found hash value in the array for compute_bucket_count,
5858 and also for .dynsym reordering purposes. */
5859 s->hashcodes[s->nsyms] = ha;
5860 s->hashval[h->dynindx] = ha;
5862 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5863 s->min_dynindx = h->dynindx;
5871 /* This function will be called though elf_link_hash_traverse to do
5872 final dynaminc symbol renumbering. */
5875 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5877 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5878 unsigned long int bucket;
5879 unsigned long int val;
5881 /* Ignore indirect symbols. */
5882 if (h->dynindx == -1)
5885 /* Ignore also local symbols and undefined symbols. */
5886 if (! (*s->bed->elf_hash_symbol) (h))
5888 if (h->dynindx >= s->min_dynindx)
5889 h->dynindx = s->local_indx++;
5893 bucket = s->hashval[h->dynindx] % s->bucketcount;
5894 val = (s->hashval[h->dynindx] >> s->shift1)
5895 & ((s->maskbits >> s->shift1) - 1);
5896 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5898 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5899 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5900 if (s->counts[bucket] == 1)
5901 /* Last element terminates the chain. */
5903 bfd_put_32 (s->output_bfd, val,
5904 s->contents + (s->indx[bucket] - s->symindx) * 4);
5905 --s->counts[bucket];
5906 h->dynindx = s->indx[bucket]++;
5910 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5913 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5915 return !(h->forced_local
5916 || h->root.type == bfd_link_hash_undefined
5917 || h->root.type == bfd_link_hash_undefweak
5918 || ((h->root.type == bfd_link_hash_defined
5919 || h->root.type == bfd_link_hash_defweak)
5920 && h->root.u.def.section->output_section == NULL));
5923 /* Array used to determine the number of hash table buckets to use
5924 based on the number of symbols there are. If there are fewer than
5925 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5926 fewer than 37 we use 17 buckets, and so forth. We never use more
5927 than 32771 buckets. */
5929 static const size_t elf_buckets[] =
5931 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5935 /* Compute bucket count for hashing table. We do not use a static set
5936 of possible tables sizes anymore. Instead we determine for all
5937 possible reasonable sizes of the table the outcome (i.e., the
5938 number of collisions etc) and choose the best solution. The
5939 weighting functions are not too simple to allow the table to grow
5940 without bounds. Instead one of the weighting factors is the size.
5941 Therefore the result is always a good payoff between few collisions
5942 (= short chain lengths) and table size. */
5944 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5945 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5946 unsigned long int nsyms,
5949 size_t best_size = 0;
5950 unsigned long int i;
5952 /* We have a problem here. The following code to optimize the table
5953 size requires an integer type with more the 32 bits. If
5954 BFD_HOST_U_64_BIT is set we know about such a type. */
5955 #ifdef BFD_HOST_U_64_BIT
5960 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5961 bfd *dynobj = elf_hash_table (info)->dynobj;
5962 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5963 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5964 unsigned long int *counts;
5966 unsigned int no_improvement_count = 0;
5968 /* Possible optimization parameters: if we have NSYMS symbols we say
5969 that the hashing table must at least have NSYMS/4 and at most
5971 minsize = nsyms / 4;
5974 best_size = maxsize = nsyms * 2;
5979 if ((best_size & 31) == 0)
5983 /* Create array where we count the collisions in. We must use bfd_malloc
5984 since the size could be large. */
5986 amt *= sizeof (unsigned long int);
5987 counts = (unsigned long int *) bfd_malloc (amt);
5991 /* Compute the "optimal" size for the hash table. The criteria is a
5992 minimal chain length. The minor criteria is (of course) the size
5994 for (i = minsize; i < maxsize; ++i)
5996 /* Walk through the array of hashcodes and count the collisions. */
5997 BFD_HOST_U_64_BIT max;
5998 unsigned long int j;
5999 unsigned long int fact;
6001 if (gnu_hash && (i & 31) == 0)
6004 memset (counts, '\0', i * sizeof (unsigned long int));
6006 /* Determine how often each hash bucket is used. */
6007 for (j = 0; j < nsyms; ++j)
6008 ++counts[hashcodes[j] % i];
6010 /* For the weight function we need some information about the
6011 pagesize on the target. This is information need not be 100%
6012 accurate. Since this information is not available (so far) we
6013 define it here to a reasonable default value. If it is crucial
6014 to have a better value some day simply define this value. */
6015 # ifndef BFD_TARGET_PAGESIZE
6016 # define BFD_TARGET_PAGESIZE (4096)
6019 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6021 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6024 /* Variant 1: optimize for short chains. We add the squares
6025 of all the chain lengths (which favors many small chain
6026 over a few long chains). */
6027 for (j = 0; j < i; ++j)
6028 max += counts[j] * counts[j];
6030 /* This adds penalties for the overall size of the table. */
6031 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6034 /* Variant 2: Optimize a lot more for small table. Here we
6035 also add squares of the size but we also add penalties for
6036 empty slots (the +1 term). */
6037 for (j = 0; j < i; ++j)
6038 max += (1 + counts[j]) * (1 + counts[j]);
6040 /* The overall size of the table is considered, but not as
6041 strong as in variant 1, where it is squared. */
6042 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6046 /* Compare with current best results. */
6047 if (max < best_chlen)
6051 no_improvement_count = 0;
6053 /* PR 11843: Avoid futile long searches for the best bucket size
6054 when there are a large number of symbols. */
6055 else if (++no_improvement_count == 100)
6062 #endif /* defined (BFD_HOST_U_64_BIT) */
6064 /* This is the fallback solution if no 64bit type is available or if we
6065 are not supposed to spend much time on optimizations. We select the
6066 bucket count using a fixed set of numbers. */
6067 for (i = 0; elf_buckets[i] != 0; i++)
6069 best_size = elf_buckets[i];
6070 if (nsyms < elf_buckets[i + 1])
6073 if (gnu_hash && best_size < 2)
6080 /* Size any SHT_GROUP section for ld -r. */
6083 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6088 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6089 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6090 && (s = ibfd->sections) != NULL
6091 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6092 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6097 /* Set a default stack segment size. The value in INFO wins. If it
6098 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6099 undefined it is initialized. */
6102 bfd_elf_stack_segment_size (bfd *output_bfd,
6103 struct bfd_link_info *info,
6104 const char *legacy_symbol,
6105 bfd_vma default_size)
6107 struct elf_link_hash_entry *h = NULL;
6109 /* Look for legacy symbol. */
6111 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6112 FALSE, FALSE, FALSE);
6113 if (h && (h->root.type == bfd_link_hash_defined
6114 || h->root.type == bfd_link_hash_defweak)
6116 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6118 /* The symbol has no type if specified on the command line. */
6119 h->type = STT_OBJECT;
6120 if (info->stacksize)
6121 /* xgettext:c-format */
6122 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6123 output_bfd, legacy_symbol);
6124 else if (h->root.u.def.section != bfd_abs_section_ptr)
6125 /* xgettext:c-format */
6126 _bfd_error_handler (_("%pB: %s not absolute"),
6127 output_bfd, legacy_symbol);
6129 info->stacksize = h->root.u.def.value;
6132 if (!info->stacksize)
6133 /* If the user didn't set a size, or explicitly inhibit the
6134 size, set it now. */
6135 info->stacksize = default_size;
6137 /* Provide the legacy symbol, if it is referenced. */
6138 if (h && (h->root.type == bfd_link_hash_undefined
6139 || h->root.type == bfd_link_hash_undefweak))
6141 struct bfd_link_hash_entry *bh = NULL;
6143 if (!(_bfd_generic_link_add_one_symbol
6144 (info, output_bfd, legacy_symbol,
6145 BSF_GLOBAL, bfd_abs_section_ptr,
6146 info->stacksize >= 0 ? info->stacksize : 0,
6147 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6150 h = (struct elf_link_hash_entry *) bh;
6152 h->type = STT_OBJECT;
6158 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6160 struct elf_gc_sweep_symbol_info
6162 struct bfd_link_info *info;
6163 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6168 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6171 && (((h->root.type == bfd_link_hash_defined
6172 || h->root.type == bfd_link_hash_defweak)
6173 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6174 && h->root.u.def.section->gc_mark))
6175 || h->root.type == bfd_link_hash_undefined
6176 || h->root.type == bfd_link_hash_undefweak))
6178 struct elf_gc_sweep_symbol_info *inf;
6180 inf = (struct elf_gc_sweep_symbol_info *) data;
6181 (*inf->hide_symbol) (inf->info, h, TRUE);
6184 h->ref_regular_nonweak = 0;
6190 /* Set up the sizes and contents of the ELF dynamic sections. This is
6191 called by the ELF linker emulation before_allocation routine. We
6192 must set the sizes of the sections before the linker sets the
6193 addresses of the various sections. */
6196 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6199 const char *filter_shlib,
6201 const char *depaudit,
6202 const char * const *auxiliary_filters,
6203 struct bfd_link_info *info,
6204 asection **sinterpptr)
6207 const struct elf_backend_data *bed;
6211 if (!is_elf_hash_table (info->hash))
6214 dynobj = elf_hash_table (info)->dynobj;
6216 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6218 struct bfd_elf_version_tree *verdefs;
6219 struct elf_info_failed asvinfo;
6220 struct bfd_elf_version_tree *t;
6221 struct bfd_elf_version_expr *d;
6225 /* If we are supposed to export all symbols into the dynamic symbol
6226 table (this is not the normal case), then do so. */
6227 if (info->export_dynamic
6228 || (bfd_link_executable (info) && info->dynamic))
6230 struct elf_info_failed eif;
6234 elf_link_hash_traverse (elf_hash_table (info),
6235 _bfd_elf_export_symbol,
6243 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6245 if (soname_indx == (size_t) -1
6246 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6250 soname_indx = (size_t) -1;
6252 /* Make all global versions with definition. */
6253 for (t = info->version_info; t != NULL; t = t->next)
6254 for (d = t->globals.list; d != NULL; d = d->next)
6255 if (!d->symver && d->literal)
6257 const char *verstr, *name;
6258 size_t namelen, verlen, newlen;
6259 char *newname, *p, leading_char;
6260 struct elf_link_hash_entry *newh;
6262 leading_char = bfd_get_symbol_leading_char (output_bfd);
6264 namelen = strlen (name) + (leading_char != '\0');
6266 verlen = strlen (verstr);
6267 newlen = namelen + verlen + 3;
6269 newname = (char *) bfd_malloc (newlen);
6270 if (newname == NULL)
6272 newname[0] = leading_char;
6273 memcpy (newname + (leading_char != '\0'), name, namelen);
6275 /* Check the hidden versioned definition. */
6276 p = newname + namelen;
6278 memcpy (p, verstr, verlen + 1);
6279 newh = elf_link_hash_lookup (elf_hash_table (info),
6280 newname, FALSE, FALSE,
6283 || (newh->root.type != bfd_link_hash_defined
6284 && newh->root.type != bfd_link_hash_defweak))
6286 /* Check the default versioned definition. */
6288 memcpy (p, verstr, verlen + 1);
6289 newh = elf_link_hash_lookup (elf_hash_table (info),
6290 newname, FALSE, FALSE,
6295 /* Mark this version if there is a definition and it is
6296 not defined in a shared object. */
6298 && !newh->def_dynamic
6299 && (newh->root.type == bfd_link_hash_defined
6300 || newh->root.type == bfd_link_hash_defweak))
6304 /* Attach all the symbols to their version information. */
6305 asvinfo.info = info;
6306 asvinfo.failed = FALSE;
6308 elf_link_hash_traverse (elf_hash_table (info),
6309 _bfd_elf_link_assign_sym_version,
6314 if (!info->allow_undefined_version)
6316 /* Check if all global versions have a definition. */
6317 bfd_boolean all_defined = TRUE;
6318 for (t = info->version_info; t != NULL; t = t->next)
6319 for (d = t->globals.list; d != NULL; d = d->next)
6320 if (d->literal && !d->symver && !d->script)
6323 (_("%s: undefined version: %s"),
6324 d->pattern, t->name);
6325 all_defined = FALSE;
6330 bfd_set_error (bfd_error_bad_value);
6335 /* Set up the version definition section. */
6336 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6337 BFD_ASSERT (s != NULL);
6339 /* We may have created additional version definitions if we are
6340 just linking a regular application. */
6341 verdefs = info->version_info;
6343 /* Skip anonymous version tag. */
6344 if (verdefs != NULL && verdefs->vernum == 0)
6345 verdefs = verdefs->next;
6347 if (verdefs == NULL && !info->create_default_symver)
6348 s->flags |= SEC_EXCLUDE;
6354 Elf_Internal_Verdef def;
6355 Elf_Internal_Verdaux defaux;
6356 struct bfd_link_hash_entry *bh;
6357 struct elf_link_hash_entry *h;
6363 /* Make space for the base version. */
6364 size += sizeof (Elf_External_Verdef);
6365 size += sizeof (Elf_External_Verdaux);
6368 /* Make space for the default version. */
6369 if (info->create_default_symver)
6371 size += sizeof (Elf_External_Verdef);
6375 for (t = verdefs; t != NULL; t = t->next)
6377 struct bfd_elf_version_deps *n;
6379 /* Don't emit base version twice. */
6383 size += sizeof (Elf_External_Verdef);
6384 size += sizeof (Elf_External_Verdaux);
6387 for (n = t->deps; n != NULL; n = n->next)
6388 size += sizeof (Elf_External_Verdaux);
6392 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6393 if (s->contents == NULL && s->size != 0)
6396 /* Fill in the version definition section. */
6400 def.vd_version = VER_DEF_CURRENT;
6401 def.vd_flags = VER_FLG_BASE;
6404 if (info->create_default_symver)
6406 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6407 def.vd_next = sizeof (Elf_External_Verdef);
6411 def.vd_aux = sizeof (Elf_External_Verdef);
6412 def.vd_next = (sizeof (Elf_External_Verdef)
6413 + sizeof (Elf_External_Verdaux));
6416 if (soname_indx != (size_t) -1)
6418 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6420 def.vd_hash = bfd_elf_hash (soname);
6421 defaux.vda_name = soname_indx;
6428 name = lbasename (output_bfd->filename);
6429 def.vd_hash = bfd_elf_hash (name);
6430 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6432 if (indx == (size_t) -1)
6434 defaux.vda_name = indx;
6436 defaux.vda_next = 0;
6438 _bfd_elf_swap_verdef_out (output_bfd, &def,
6439 (Elf_External_Verdef *) p);
6440 p += sizeof (Elf_External_Verdef);
6441 if (info->create_default_symver)
6443 /* Add a symbol representing this version. */
6445 if (! (_bfd_generic_link_add_one_symbol
6446 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6448 get_elf_backend_data (dynobj)->collect, &bh)))
6450 h = (struct elf_link_hash_entry *) bh;
6453 h->type = STT_OBJECT;
6454 h->verinfo.vertree = NULL;
6456 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6459 /* Create a duplicate of the base version with the same
6460 aux block, but different flags. */
6463 def.vd_aux = sizeof (Elf_External_Verdef);
6465 def.vd_next = (sizeof (Elf_External_Verdef)
6466 + sizeof (Elf_External_Verdaux));
6469 _bfd_elf_swap_verdef_out (output_bfd, &def,
6470 (Elf_External_Verdef *) p);
6471 p += sizeof (Elf_External_Verdef);
6473 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6474 (Elf_External_Verdaux *) p);
6475 p += sizeof (Elf_External_Verdaux);
6477 for (t = verdefs; t != NULL; t = t->next)
6480 struct bfd_elf_version_deps *n;
6482 /* Don't emit the base version twice. */
6487 for (n = t->deps; n != NULL; n = n->next)
6490 /* Add a symbol representing this version. */
6492 if (! (_bfd_generic_link_add_one_symbol
6493 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6495 get_elf_backend_data (dynobj)->collect, &bh)))
6497 h = (struct elf_link_hash_entry *) bh;
6500 h->type = STT_OBJECT;
6501 h->verinfo.vertree = t;
6503 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6506 def.vd_version = VER_DEF_CURRENT;
6508 if (t->globals.list == NULL
6509 && t->locals.list == NULL
6511 def.vd_flags |= VER_FLG_WEAK;
6512 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6513 def.vd_cnt = cdeps + 1;
6514 def.vd_hash = bfd_elf_hash (t->name);
6515 def.vd_aux = sizeof (Elf_External_Verdef);
6518 /* If a basever node is next, it *must* be the last node in
6519 the chain, otherwise Verdef construction breaks. */
6520 if (t->next != NULL && t->next->vernum == 0)
6521 BFD_ASSERT (t->next->next == NULL);
6523 if (t->next != NULL && t->next->vernum != 0)
6524 def.vd_next = (sizeof (Elf_External_Verdef)
6525 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6527 _bfd_elf_swap_verdef_out (output_bfd, &def,
6528 (Elf_External_Verdef *) p);
6529 p += sizeof (Elf_External_Verdef);
6531 defaux.vda_name = h->dynstr_index;
6532 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6534 defaux.vda_next = 0;
6535 if (t->deps != NULL)
6536 defaux.vda_next = sizeof (Elf_External_Verdaux);
6537 t->name_indx = defaux.vda_name;
6539 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6540 (Elf_External_Verdaux *) p);
6541 p += sizeof (Elf_External_Verdaux);
6543 for (n = t->deps; n != NULL; n = n->next)
6545 if (n->version_needed == NULL)
6547 /* This can happen if there was an error in the
6549 defaux.vda_name = 0;
6553 defaux.vda_name = n->version_needed->name_indx;
6554 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6557 if (n->next == NULL)
6558 defaux.vda_next = 0;
6560 defaux.vda_next = sizeof (Elf_External_Verdaux);
6562 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6563 (Elf_External_Verdaux *) p);
6564 p += sizeof (Elf_External_Verdaux);
6568 elf_tdata (output_bfd)->cverdefs = cdefs;
6572 bed = get_elf_backend_data (output_bfd);
6574 if (info->gc_sections && bed->can_gc_sections)
6576 struct elf_gc_sweep_symbol_info sweep_info;
6578 /* Remove the symbols that were in the swept sections from the
6579 dynamic symbol table. */
6580 sweep_info.info = info;
6581 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6582 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6586 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6589 struct elf_find_verdep_info sinfo;
6591 /* Work out the size of the version reference section. */
6593 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6594 BFD_ASSERT (s != NULL);
6597 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6598 if (sinfo.vers == 0)
6600 sinfo.failed = FALSE;
6602 elf_link_hash_traverse (elf_hash_table (info),
6603 _bfd_elf_link_find_version_dependencies,
6608 if (elf_tdata (output_bfd)->verref == NULL)
6609 s->flags |= SEC_EXCLUDE;
6612 Elf_Internal_Verneed *vn;
6617 /* Build the version dependency section. */
6620 for (vn = elf_tdata (output_bfd)->verref;
6622 vn = vn->vn_nextref)
6624 Elf_Internal_Vernaux *a;
6626 size += sizeof (Elf_External_Verneed);
6628 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6629 size += sizeof (Elf_External_Vernaux);
6633 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6634 if (s->contents == NULL)
6638 for (vn = elf_tdata (output_bfd)->verref;
6640 vn = vn->vn_nextref)
6643 Elf_Internal_Vernaux *a;
6647 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6650 vn->vn_version = VER_NEED_CURRENT;
6652 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6653 elf_dt_name (vn->vn_bfd) != NULL
6654 ? elf_dt_name (vn->vn_bfd)
6655 : lbasename (vn->vn_bfd->filename),
6657 if (indx == (size_t) -1)
6660 vn->vn_aux = sizeof (Elf_External_Verneed);
6661 if (vn->vn_nextref == NULL)
6664 vn->vn_next = (sizeof (Elf_External_Verneed)
6665 + caux * sizeof (Elf_External_Vernaux));
6667 _bfd_elf_swap_verneed_out (output_bfd, vn,
6668 (Elf_External_Verneed *) p);
6669 p += sizeof (Elf_External_Verneed);
6671 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6673 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6674 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6675 a->vna_nodename, FALSE);
6676 if (indx == (size_t) -1)
6679 if (a->vna_nextptr == NULL)
6682 a->vna_next = sizeof (Elf_External_Vernaux);
6684 _bfd_elf_swap_vernaux_out (output_bfd, a,
6685 (Elf_External_Vernaux *) p);
6686 p += sizeof (Elf_External_Vernaux);
6690 elf_tdata (output_bfd)->cverrefs = crefs;
6694 /* Any syms created from now on start with -1 in
6695 got.refcount/offset and plt.refcount/offset. */
6696 elf_hash_table (info)->init_got_refcount
6697 = elf_hash_table (info)->init_got_offset;
6698 elf_hash_table (info)->init_plt_refcount
6699 = elf_hash_table (info)->init_plt_offset;
6701 if (bfd_link_relocatable (info)
6702 && !_bfd_elf_size_group_sections (info))
6705 /* The backend may have to create some sections regardless of whether
6706 we're dynamic or not. */
6707 if (bed->elf_backend_always_size_sections
6708 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6711 /* Determine any GNU_STACK segment requirements, after the backend
6712 has had a chance to set a default segment size. */
6713 if (info->execstack)
6714 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6715 else if (info->noexecstack)
6716 elf_stack_flags (output_bfd) = PF_R | PF_W;
6720 asection *notesec = NULL;
6723 for (inputobj = info->input_bfds;
6725 inputobj = inputobj->link.next)
6730 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6732 s = inputobj->sections;
6733 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6736 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6739 if (s->flags & SEC_CODE)
6743 else if (bed->default_execstack)
6746 if (notesec || info->stacksize > 0)
6747 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6748 if (notesec && exec && bfd_link_relocatable (info)
6749 && notesec->output_section != bfd_abs_section_ptr)
6750 notesec->output_section->flags |= SEC_CODE;
6753 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6755 struct elf_info_failed eif;
6756 struct elf_link_hash_entry *h;
6760 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6761 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6765 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6767 info->flags |= DF_SYMBOLIC;
6775 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6777 if (indx == (size_t) -1)
6780 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6781 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6785 if (filter_shlib != NULL)
6789 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6790 filter_shlib, TRUE);
6791 if (indx == (size_t) -1
6792 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6796 if (auxiliary_filters != NULL)
6798 const char * const *p;
6800 for (p = auxiliary_filters; *p != NULL; p++)
6804 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6806 if (indx == (size_t) -1
6807 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6816 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6818 if (indx == (size_t) -1
6819 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6823 if (depaudit != NULL)
6827 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6829 if (indx == (size_t) -1
6830 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6837 /* Find all symbols which were defined in a dynamic object and make
6838 the backend pick a reasonable value for them. */
6839 elf_link_hash_traverse (elf_hash_table (info),
6840 _bfd_elf_adjust_dynamic_symbol,
6845 /* Add some entries to the .dynamic section. We fill in some of the
6846 values later, in bfd_elf_final_link, but we must add the entries
6847 now so that we know the final size of the .dynamic section. */
6849 /* If there are initialization and/or finalization functions to
6850 call then add the corresponding DT_INIT/DT_FINI entries. */
6851 h = (info->init_function
6852 ? elf_link_hash_lookup (elf_hash_table (info),
6853 info->init_function, FALSE,
6860 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6863 h = (info->fini_function
6864 ? elf_link_hash_lookup (elf_hash_table (info),
6865 info->fini_function, FALSE,
6872 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6876 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6877 if (s != NULL && s->linker_has_input)
6879 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6880 if (! bfd_link_executable (info))
6885 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6886 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6887 && (o = sub->sections) != NULL
6888 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6889 for (o = sub->sections; o != NULL; o = o->next)
6890 if (elf_section_data (o)->this_hdr.sh_type
6891 == SHT_PREINIT_ARRAY)
6894 (_("%pB: .preinit_array section is not allowed in DSO"),
6899 bfd_set_error (bfd_error_nonrepresentable_section);
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6904 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6907 s = bfd_get_section_by_name (output_bfd, ".init_array");
6908 if (s != NULL && s->linker_has_input)
6910 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6911 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6914 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6915 if (s != NULL && s->linker_has_input)
6917 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6918 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6922 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6923 /* If .dynstr is excluded from the link, we don't want any of
6924 these tags. Strictly, we should be checking each section
6925 individually; This quick check covers for the case where
6926 someone does a /DISCARD/ : { *(*) }. */
6927 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6929 bfd_size_type strsize;
6931 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6932 if ((info->emit_hash
6933 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6934 || (info->emit_gnu_hash
6935 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6936 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6937 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6938 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6939 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6940 bed->s->sizeof_sym))
6945 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6948 /* The backend must work out the sizes of all the other dynamic
6951 && bed->elf_backend_size_dynamic_sections != NULL
6952 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6955 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6957 if (elf_tdata (output_bfd)->cverdefs)
6959 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6961 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6962 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6966 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6968 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6971 else if (info->flags & DF_BIND_NOW)
6973 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6979 if (bfd_link_executable (info))
6980 info->flags_1 &= ~ (DF_1_INITFIRST
6983 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6987 if (elf_tdata (output_bfd)->cverrefs)
6989 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6991 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6992 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6996 if ((elf_tdata (output_bfd)->cverrefs == 0
6997 && elf_tdata (output_bfd)->cverdefs == 0)
6998 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7002 s = bfd_get_linker_section (dynobj, ".gnu.version");
7003 s->flags |= SEC_EXCLUDE;
7009 /* Find the first non-excluded output section. We'll use its
7010 section symbol for some emitted relocs. */
7012 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7016 for (s = output_bfd->sections; s != NULL; s = s->next)
7017 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7018 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7020 elf_hash_table (info)->text_index_section = s;
7025 /* Find two non-excluded output sections, one for code, one for data.
7026 We'll use their section symbols for some emitted relocs. */
7028 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7032 /* Data first, since setting text_index_section changes
7033 _bfd_elf_omit_section_dynsym_default. */
7034 for (s = output_bfd->sections; s != NULL; s = s->next)
7035 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7036 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7038 elf_hash_table (info)->data_index_section = s;
7042 for (s = output_bfd->sections; s != NULL; s = s->next)
7043 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7044 == (SEC_ALLOC | SEC_READONLY))
7045 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7047 elf_hash_table (info)->text_index_section = s;
7051 if (elf_hash_table (info)->text_index_section == NULL)
7052 elf_hash_table (info)->text_index_section
7053 = elf_hash_table (info)->data_index_section;
7057 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7059 const struct elf_backend_data *bed;
7060 unsigned long section_sym_count;
7061 bfd_size_type dynsymcount = 0;
7063 if (!is_elf_hash_table (info->hash))
7066 bed = get_elf_backend_data (output_bfd);
7067 (*bed->elf_backend_init_index_section) (output_bfd, info);
7069 /* Assign dynsym indices. In a shared library we generate a section
7070 symbol for each output section, which come first. Next come all
7071 of the back-end allocated local dynamic syms, followed by the rest
7072 of the global symbols.
7074 This is usually not needed for static binaries, however backends
7075 can request to always do it, e.g. the MIPS backend uses dynamic
7076 symbol counts to lay out GOT, which will be produced in the
7077 presence of GOT relocations even in static binaries (holding fixed
7078 data in that case, to satisfy those relocations). */
7080 if (elf_hash_table (info)->dynamic_sections_created
7081 || bed->always_renumber_dynsyms)
7082 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7083 §ion_sym_count);
7085 if (elf_hash_table (info)->dynamic_sections_created)
7089 unsigned int dtagcount;
7091 dynobj = elf_hash_table (info)->dynobj;
7093 /* Work out the size of the symbol version section. */
7094 s = bfd_get_linker_section (dynobj, ".gnu.version");
7095 BFD_ASSERT (s != NULL);
7096 if ((s->flags & SEC_EXCLUDE) == 0)
7098 s->size = dynsymcount * sizeof (Elf_External_Versym);
7099 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7100 if (s->contents == NULL)
7103 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7107 /* Set the size of the .dynsym and .hash sections. We counted
7108 the number of dynamic symbols in elf_link_add_object_symbols.
7109 We will build the contents of .dynsym and .hash when we build
7110 the final symbol table, because until then we do not know the
7111 correct value to give the symbols. We built the .dynstr
7112 section as we went along in elf_link_add_object_symbols. */
7113 s = elf_hash_table (info)->dynsym;
7114 BFD_ASSERT (s != NULL);
7115 s->size = dynsymcount * bed->s->sizeof_sym;
7117 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7118 if (s->contents == NULL)
7121 /* The first entry in .dynsym is a dummy symbol. Clear all the
7122 section syms, in case we don't output them all. */
7123 ++section_sym_count;
7124 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7126 elf_hash_table (info)->bucketcount = 0;
7128 /* Compute the size of the hashing table. As a side effect this
7129 computes the hash values for all the names we export. */
7130 if (info->emit_hash)
7132 unsigned long int *hashcodes;
7133 struct hash_codes_info hashinf;
7135 unsigned long int nsyms;
7137 size_t hash_entry_size;
7139 /* Compute the hash values for all exported symbols. At the same
7140 time store the values in an array so that we could use them for
7142 amt = dynsymcount * sizeof (unsigned long int);
7143 hashcodes = (unsigned long int *) bfd_malloc (amt);
7144 if (hashcodes == NULL)
7146 hashinf.hashcodes = hashcodes;
7147 hashinf.error = FALSE;
7149 /* Put all hash values in HASHCODES. */
7150 elf_link_hash_traverse (elf_hash_table (info),
7151 elf_collect_hash_codes, &hashinf);
7158 nsyms = hashinf.hashcodes - hashcodes;
7160 = compute_bucket_count (info, hashcodes, nsyms, 0);
7163 if (bucketcount == 0 && nsyms > 0)
7166 elf_hash_table (info)->bucketcount = bucketcount;
7168 s = bfd_get_linker_section (dynobj, ".hash");
7169 BFD_ASSERT (s != NULL);
7170 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7171 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7172 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7173 if (s->contents == NULL)
7176 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7177 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7178 s->contents + hash_entry_size);
7181 if (info->emit_gnu_hash)
7184 unsigned char *contents;
7185 struct collect_gnu_hash_codes cinfo;
7189 memset (&cinfo, 0, sizeof (cinfo));
7191 /* Compute the hash values for all exported symbols. At the same
7192 time store the values in an array so that we could use them for
7194 amt = dynsymcount * 2 * sizeof (unsigned long int);
7195 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7196 if (cinfo.hashcodes == NULL)
7199 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7200 cinfo.min_dynindx = -1;
7201 cinfo.output_bfd = output_bfd;
7204 /* Put all hash values in HASHCODES. */
7205 elf_link_hash_traverse (elf_hash_table (info),
7206 elf_collect_gnu_hash_codes, &cinfo);
7209 free (cinfo.hashcodes);
7214 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7216 if (bucketcount == 0)
7218 free (cinfo.hashcodes);
7222 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7223 BFD_ASSERT (s != NULL);
7225 if (cinfo.nsyms == 0)
7227 /* Empty .gnu.hash section is special. */
7228 BFD_ASSERT (cinfo.min_dynindx == -1);
7229 free (cinfo.hashcodes);
7230 s->size = 5 * 4 + bed->s->arch_size / 8;
7231 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7232 if (contents == NULL)
7234 s->contents = contents;
7235 /* 1 empty bucket. */
7236 bfd_put_32 (output_bfd, 1, contents);
7237 /* SYMIDX above the special symbol 0. */
7238 bfd_put_32 (output_bfd, 1, contents + 4);
7239 /* Just one word for bitmask. */
7240 bfd_put_32 (output_bfd, 1, contents + 8);
7241 /* Only hash fn bloom filter. */
7242 bfd_put_32 (output_bfd, 0, contents + 12);
7243 /* No hashes are valid - empty bitmask. */
7244 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7245 /* No hashes in the only bucket. */
7246 bfd_put_32 (output_bfd, 0,
7247 contents + 16 + bed->s->arch_size / 8);
7251 unsigned long int maskwords, maskbitslog2, x;
7252 BFD_ASSERT (cinfo.min_dynindx != -1);
7256 while ((x >>= 1) != 0)
7258 if (maskbitslog2 < 3)
7260 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7261 maskbitslog2 = maskbitslog2 + 3;
7263 maskbitslog2 = maskbitslog2 + 2;
7264 if (bed->s->arch_size == 64)
7266 if (maskbitslog2 == 5)
7272 cinfo.mask = (1 << cinfo.shift1) - 1;
7273 cinfo.shift2 = maskbitslog2;
7274 cinfo.maskbits = 1 << maskbitslog2;
7275 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7276 amt = bucketcount * sizeof (unsigned long int) * 2;
7277 amt += maskwords * sizeof (bfd_vma);
7278 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7279 if (cinfo.bitmask == NULL)
7281 free (cinfo.hashcodes);
7285 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7286 cinfo.indx = cinfo.counts + bucketcount;
7287 cinfo.symindx = dynsymcount - cinfo.nsyms;
7288 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7290 /* Determine how often each hash bucket is used. */
7291 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7292 for (i = 0; i < cinfo.nsyms; ++i)
7293 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7295 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7296 if (cinfo.counts[i] != 0)
7298 cinfo.indx[i] = cnt;
7299 cnt += cinfo.counts[i];
7301 BFD_ASSERT (cnt == dynsymcount);
7302 cinfo.bucketcount = bucketcount;
7303 cinfo.local_indx = cinfo.min_dynindx;
7305 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7306 s->size += cinfo.maskbits / 8;
7307 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7308 if (contents == NULL)
7310 free (cinfo.bitmask);
7311 free (cinfo.hashcodes);
7315 s->contents = contents;
7316 bfd_put_32 (output_bfd, bucketcount, contents);
7317 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7318 bfd_put_32 (output_bfd, maskwords, contents + 8);
7319 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7320 contents += 16 + cinfo.maskbits / 8;
7322 for (i = 0; i < bucketcount; ++i)
7324 if (cinfo.counts[i] == 0)
7325 bfd_put_32 (output_bfd, 0, contents);
7327 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7331 cinfo.contents = contents;
7333 /* Renumber dynamic symbols, populate .gnu.hash section. */
7334 elf_link_hash_traverse (elf_hash_table (info),
7335 elf_renumber_gnu_hash_syms, &cinfo);
7337 contents = s->contents + 16;
7338 for (i = 0; i < maskwords; ++i)
7340 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7342 contents += bed->s->arch_size / 8;
7345 free (cinfo.bitmask);
7346 free (cinfo.hashcodes);
7350 s = bfd_get_linker_section (dynobj, ".dynstr");
7351 BFD_ASSERT (s != NULL);
7353 elf_finalize_dynstr (output_bfd, info);
7355 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7357 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7358 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7365 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7368 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7371 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7372 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7375 /* Finish SHF_MERGE section merging. */
7378 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7383 if (!is_elf_hash_table (info->hash))
7386 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7387 if ((ibfd->flags & DYNAMIC) == 0
7388 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7389 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7390 == get_elf_backend_data (obfd)->s->elfclass))
7391 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7392 if ((sec->flags & SEC_MERGE) != 0
7393 && !bfd_is_abs_section (sec->output_section))
7395 struct bfd_elf_section_data *secdata;
7397 secdata = elf_section_data (sec);
7398 if (! _bfd_add_merge_section (obfd,
7399 &elf_hash_table (info)->merge_info,
7400 sec, &secdata->sec_info))
7402 else if (secdata->sec_info)
7403 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7406 if (elf_hash_table (info)->merge_info != NULL)
7407 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7408 merge_sections_remove_hook);
7412 /* Create an entry in an ELF linker hash table. */
7414 struct bfd_hash_entry *
7415 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7416 struct bfd_hash_table *table,
7419 /* Allocate the structure if it has not already been allocated by a
7423 entry = (struct bfd_hash_entry *)
7424 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7429 /* Call the allocation method of the superclass. */
7430 entry = _bfd_link_hash_newfunc (entry, table, string);
7433 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7434 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7436 /* Set local fields. */
7439 ret->got = htab->init_got_refcount;
7440 ret->plt = htab->init_plt_refcount;
7441 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7442 - offsetof (struct elf_link_hash_entry, size)));
7443 /* Assume that we have been called by a non-ELF symbol reader.
7444 This flag is then reset by the code which reads an ELF input
7445 file. This ensures that a symbol created by a non-ELF symbol
7446 reader will have the flag set correctly. */
7453 /* Copy data from an indirect symbol to its direct symbol, hiding the
7454 old indirect symbol. Also used for copying flags to a weakdef. */
7457 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7458 struct elf_link_hash_entry *dir,
7459 struct elf_link_hash_entry *ind)
7461 struct elf_link_hash_table *htab;
7463 /* Copy down any references that we may have already seen to the
7464 symbol which just became indirect. */
7466 if (dir->versioned != versioned_hidden)
7467 dir->ref_dynamic |= ind->ref_dynamic;
7468 dir->ref_regular |= ind->ref_regular;
7469 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7470 dir->non_got_ref |= ind->non_got_ref;
7471 dir->needs_plt |= ind->needs_plt;
7472 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7474 if (ind->root.type != bfd_link_hash_indirect)
7477 /* Copy over the global and procedure linkage table refcount entries.
7478 These may have been already set up by a check_relocs routine. */
7479 htab = elf_hash_table (info);
7480 if (ind->got.refcount > htab->init_got_refcount.refcount)
7482 if (dir->got.refcount < 0)
7483 dir->got.refcount = 0;
7484 dir->got.refcount += ind->got.refcount;
7485 ind->got.refcount = htab->init_got_refcount.refcount;
7488 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7490 if (dir->plt.refcount < 0)
7491 dir->plt.refcount = 0;
7492 dir->plt.refcount += ind->plt.refcount;
7493 ind->plt.refcount = htab->init_plt_refcount.refcount;
7496 if (ind->dynindx != -1)
7498 if (dir->dynindx != -1)
7499 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7500 dir->dynindx = ind->dynindx;
7501 dir->dynstr_index = ind->dynstr_index;
7503 ind->dynstr_index = 0;
7508 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7509 struct elf_link_hash_entry *h,
7510 bfd_boolean force_local)
7512 /* STT_GNU_IFUNC symbol must go through PLT. */
7513 if (h->type != STT_GNU_IFUNC)
7515 h->plt = elf_hash_table (info)->init_plt_offset;
7520 h->forced_local = 1;
7521 if (h->dynindx != -1)
7523 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7526 h->dynstr_index = 0;
7531 /* Hide a symbol. */
7534 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7535 struct bfd_link_info *info,
7536 struct bfd_link_hash_entry *h)
7538 if (is_elf_hash_table (info->hash))
7540 const struct elf_backend_data *bed
7541 = get_elf_backend_data (output_bfd);
7542 struct elf_link_hash_entry *eh
7543 = (struct elf_link_hash_entry *) h;
7544 bed->elf_backend_hide_symbol (info, eh, TRUE);
7545 eh->def_dynamic = 0;
7546 eh->ref_dynamic = 0;
7547 eh->dynamic_def = 0;
7551 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7555 _bfd_elf_link_hash_table_init
7556 (struct elf_link_hash_table *table,
7558 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7559 struct bfd_hash_table *,
7561 unsigned int entsize,
7562 enum elf_target_id target_id)
7565 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7567 table->init_got_refcount.refcount = can_refcount - 1;
7568 table->init_plt_refcount.refcount = can_refcount - 1;
7569 table->init_got_offset.offset = -(bfd_vma) 1;
7570 table->init_plt_offset.offset = -(bfd_vma) 1;
7571 /* The first dynamic symbol is a dummy. */
7572 table->dynsymcount = 1;
7574 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7576 table->root.type = bfd_link_elf_hash_table;
7577 table->hash_table_id = target_id;
7582 /* Create an ELF linker hash table. */
7584 struct bfd_link_hash_table *
7585 _bfd_elf_link_hash_table_create (bfd *abfd)
7587 struct elf_link_hash_table *ret;
7588 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7590 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7594 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7595 sizeof (struct elf_link_hash_entry),
7601 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7606 /* Destroy an ELF linker hash table. */
7609 _bfd_elf_link_hash_table_free (bfd *obfd)
7611 struct elf_link_hash_table *htab;
7613 htab = (struct elf_link_hash_table *) obfd->link.hash;
7614 if (htab->dynstr != NULL)
7615 _bfd_elf_strtab_free (htab->dynstr);
7616 _bfd_merge_sections_free (htab->merge_info);
7617 _bfd_generic_link_hash_table_free (obfd);
7620 /* This is a hook for the ELF emulation code in the generic linker to
7621 tell the backend linker what file name to use for the DT_NEEDED
7622 entry for a dynamic object. */
7625 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7627 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7628 && bfd_get_format (abfd) == bfd_object)
7629 elf_dt_name (abfd) = name;
7633 bfd_elf_get_dyn_lib_class (bfd *abfd)
7636 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7637 && bfd_get_format (abfd) == bfd_object)
7638 lib_class = elf_dyn_lib_class (abfd);
7645 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7647 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7648 && bfd_get_format (abfd) == bfd_object)
7649 elf_dyn_lib_class (abfd) = lib_class;
7652 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7653 the linker ELF emulation code. */
7655 struct bfd_link_needed_list *
7656 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7657 struct bfd_link_info *info)
7659 if (! is_elf_hash_table (info->hash))
7661 return elf_hash_table (info)->needed;
7664 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7665 hook for the linker ELF emulation code. */
7667 struct bfd_link_needed_list *
7668 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7669 struct bfd_link_info *info)
7671 if (! is_elf_hash_table (info->hash))
7673 return elf_hash_table (info)->runpath;
7676 /* Get the name actually used for a dynamic object for a link. This
7677 is the SONAME entry if there is one. Otherwise, it is the string
7678 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7681 bfd_elf_get_dt_soname (bfd *abfd)
7683 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7684 && bfd_get_format (abfd) == bfd_object)
7685 return elf_dt_name (abfd);
7689 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7690 the ELF linker emulation code. */
7693 bfd_elf_get_bfd_needed_list (bfd *abfd,
7694 struct bfd_link_needed_list **pneeded)
7697 bfd_byte *dynbuf = NULL;
7698 unsigned int elfsec;
7699 unsigned long shlink;
7700 bfd_byte *extdyn, *extdynend;
7702 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7706 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7707 || bfd_get_format (abfd) != bfd_object)
7710 s = bfd_get_section_by_name (abfd, ".dynamic");
7711 if (s == NULL || s->size == 0)
7714 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7717 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7718 if (elfsec == SHN_BAD)
7721 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7723 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7724 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7727 extdynend = extdyn + s->size;
7728 for (; extdyn < extdynend; extdyn += extdynsize)
7730 Elf_Internal_Dyn dyn;
7732 (*swap_dyn_in) (abfd, extdyn, &dyn);
7734 if (dyn.d_tag == DT_NULL)
7737 if (dyn.d_tag == DT_NEEDED)
7740 struct bfd_link_needed_list *l;
7741 unsigned int tagv = dyn.d_un.d_val;
7744 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7749 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7770 struct elf_symbuf_symbol
7772 unsigned long st_name; /* Symbol name, index in string tbl */
7773 unsigned char st_info; /* Type and binding attributes */
7774 unsigned char st_other; /* Visibilty, and target specific */
7777 struct elf_symbuf_head
7779 struct elf_symbuf_symbol *ssym;
7781 unsigned int st_shndx;
7788 Elf_Internal_Sym *isym;
7789 struct elf_symbuf_symbol *ssym;
7794 /* Sort references to symbols by ascending section number. */
7797 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7799 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7800 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7802 return s1->st_shndx - s2->st_shndx;
7806 elf_sym_name_compare (const void *arg1, const void *arg2)
7808 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7809 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7810 return strcmp (s1->name, s2->name);
7813 static struct elf_symbuf_head *
7814 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7816 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7817 struct elf_symbuf_symbol *ssym;
7818 struct elf_symbuf_head *ssymbuf, *ssymhead;
7819 size_t i, shndx_count, total_size;
7821 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7825 for (ind = indbuf, i = 0; i < symcount; i++)
7826 if (isymbuf[i].st_shndx != SHN_UNDEF)
7827 *ind++ = &isymbuf[i];
7830 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7831 elf_sort_elf_symbol);
7834 if (indbufend > indbuf)
7835 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7836 if (ind[0]->st_shndx != ind[1]->st_shndx)
7839 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7840 + (indbufend - indbuf) * sizeof (*ssym));
7841 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7842 if (ssymbuf == NULL)
7848 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7849 ssymbuf->ssym = NULL;
7850 ssymbuf->count = shndx_count;
7851 ssymbuf->st_shndx = 0;
7852 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7854 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7857 ssymhead->ssym = ssym;
7858 ssymhead->count = 0;
7859 ssymhead->st_shndx = (*ind)->st_shndx;
7861 ssym->st_name = (*ind)->st_name;
7862 ssym->st_info = (*ind)->st_info;
7863 ssym->st_other = (*ind)->st_other;
7866 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7867 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7874 /* Check if 2 sections define the same set of local and global
7878 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7879 struct bfd_link_info *info)
7882 const struct elf_backend_data *bed1, *bed2;
7883 Elf_Internal_Shdr *hdr1, *hdr2;
7884 size_t symcount1, symcount2;
7885 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7886 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7887 Elf_Internal_Sym *isym, *isymend;
7888 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7889 size_t count1, count2, i;
7890 unsigned int shndx1, shndx2;
7896 /* Both sections have to be in ELF. */
7897 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7898 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7901 if (elf_section_type (sec1) != elf_section_type (sec2))
7904 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7905 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7906 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7909 bed1 = get_elf_backend_data (bfd1);
7910 bed2 = get_elf_backend_data (bfd2);
7911 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7912 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7913 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7914 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7916 if (symcount1 == 0 || symcount2 == 0)
7922 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7923 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7925 if (ssymbuf1 == NULL)
7927 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7929 if (isymbuf1 == NULL)
7932 if (!info->reduce_memory_overheads)
7933 elf_tdata (bfd1)->symbuf = ssymbuf1
7934 = elf_create_symbuf (symcount1, isymbuf1);
7937 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7939 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7941 if (isymbuf2 == NULL)
7944 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7945 elf_tdata (bfd2)->symbuf = ssymbuf2
7946 = elf_create_symbuf (symcount2, isymbuf2);
7949 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7951 /* Optimized faster version. */
7953 struct elf_symbol *symp;
7954 struct elf_symbuf_symbol *ssym, *ssymend;
7957 hi = ssymbuf1->count;
7962 mid = (lo + hi) / 2;
7963 if (shndx1 < ssymbuf1[mid].st_shndx)
7965 else if (shndx1 > ssymbuf1[mid].st_shndx)
7969 count1 = ssymbuf1[mid].count;
7976 hi = ssymbuf2->count;
7981 mid = (lo + hi) / 2;
7982 if (shndx2 < ssymbuf2[mid].st_shndx)
7984 else if (shndx2 > ssymbuf2[mid].st_shndx)
7988 count2 = ssymbuf2[mid].count;
7994 if (count1 == 0 || count2 == 0 || count1 != count2)
7998 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8000 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8001 if (symtable1 == NULL || symtable2 == NULL)
8005 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8006 ssym < ssymend; ssym++, symp++)
8008 symp->u.ssym = ssym;
8009 symp->name = bfd_elf_string_from_elf_section (bfd1,
8015 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8016 ssym < ssymend; ssym++, symp++)
8018 symp->u.ssym = ssym;
8019 symp->name = bfd_elf_string_from_elf_section (bfd2,
8024 /* Sort symbol by name. */
8025 qsort (symtable1, count1, sizeof (struct elf_symbol),
8026 elf_sym_name_compare);
8027 qsort (symtable2, count1, sizeof (struct elf_symbol),
8028 elf_sym_name_compare);
8030 for (i = 0; i < count1; i++)
8031 /* Two symbols must have the same binding, type and name. */
8032 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8033 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8034 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8041 symtable1 = (struct elf_symbol *)
8042 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8043 symtable2 = (struct elf_symbol *)
8044 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8045 if (symtable1 == NULL || symtable2 == NULL)
8048 /* Count definitions in the section. */
8050 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8051 if (isym->st_shndx == shndx1)
8052 symtable1[count1++].u.isym = isym;
8055 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8056 if (isym->st_shndx == shndx2)
8057 symtable2[count2++].u.isym = isym;
8059 if (count1 == 0 || count2 == 0 || count1 != count2)
8062 for (i = 0; i < count1; i++)
8064 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8065 symtable1[i].u.isym->st_name);
8067 for (i = 0; i < count2; i++)
8069 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8070 symtable2[i].u.isym->st_name);
8072 /* Sort symbol by name. */
8073 qsort (symtable1, count1, sizeof (struct elf_symbol),
8074 elf_sym_name_compare);
8075 qsort (symtable2, count1, sizeof (struct elf_symbol),
8076 elf_sym_name_compare);
8078 for (i = 0; i < count1; i++)
8079 /* Two symbols must have the same binding, type and name. */
8080 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8081 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8082 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8100 /* Return TRUE if 2 section types are compatible. */
8103 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8104 bfd *bbfd, const asection *bsec)
8108 || abfd->xvec->flavour != bfd_target_elf_flavour
8109 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8112 return elf_section_type (asec) == elf_section_type (bsec);
8115 /* Final phase of ELF linker. */
8117 /* A structure we use to avoid passing large numbers of arguments. */
8119 struct elf_final_link_info
8121 /* General link information. */
8122 struct bfd_link_info *info;
8125 /* Symbol string table. */
8126 struct elf_strtab_hash *symstrtab;
8127 /* .hash section. */
8129 /* symbol version section (.gnu.version). */
8130 asection *symver_sec;
8131 /* Buffer large enough to hold contents of any section. */
8133 /* Buffer large enough to hold external relocs of any section. */
8134 void *external_relocs;
8135 /* Buffer large enough to hold internal relocs of any section. */
8136 Elf_Internal_Rela *internal_relocs;
8137 /* Buffer large enough to hold external local symbols of any input
8139 bfd_byte *external_syms;
8140 /* And a buffer for symbol section indices. */
8141 Elf_External_Sym_Shndx *locsym_shndx;
8142 /* Buffer large enough to hold internal local symbols of any input
8144 Elf_Internal_Sym *internal_syms;
8145 /* Array large enough to hold a symbol index for each local symbol
8146 of any input BFD. */
8148 /* Array large enough to hold a section pointer for each local
8149 symbol of any input BFD. */
8150 asection **sections;
8151 /* Buffer for SHT_SYMTAB_SHNDX section. */
8152 Elf_External_Sym_Shndx *symshndxbuf;
8153 /* Number of STT_FILE syms seen. */
8154 size_t filesym_count;
8157 /* This struct is used to pass information to elf_link_output_extsym. */
8159 struct elf_outext_info
8162 bfd_boolean localsyms;
8163 bfd_boolean file_sym_done;
8164 struct elf_final_link_info *flinfo;
8168 /* Support for evaluating a complex relocation.
8170 Complex relocations are generalized, self-describing relocations. The
8171 implementation of them consists of two parts: complex symbols, and the
8172 relocations themselves.
8174 The relocations are use a reserved elf-wide relocation type code (R_RELC
8175 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8176 information (start bit, end bit, word width, etc) into the addend. This
8177 information is extracted from CGEN-generated operand tables within gas.
8179 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8180 internal) representing prefix-notation expressions, including but not
8181 limited to those sorts of expressions normally encoded as addends in the
8182 addend field. The symbol mangling format is:
8185 | <unary-operator> ':' <node>
8186 | <binary-operator> ':' <node> ':' <node>
8189 <literal> := 's' <digits=N> ':' <N character symbol name>
8190 | 'S' <digits=N> ':' <N character section name>
8194 <binary-operator> := as in C
8195 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8198 set_symbol_value (bfd *bfd_with_globals,
8199 Elf_Internal_Sym *isymbuf,
8204 struct elf_link_hash_entry **sym_hashes;
8205 struct elf_link_hash_entry *h;
8206 size_t extsymoff = locsymcount;
8208 if (symidx < locsymcount)
8210 Elf_Internal_Sym *sym;
8212 sym = isymbuf + symidx;
8213 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8215 /* It is a local symbol: move it to the
8216 "absolute" section and give it a value. */
8217 sym->st_shndx = SHN_ABS;
8218 sym->st_value = val;
8221 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8225 /* It is a global symbol: set its link type
8226 to "defined" and give it a value. */
8228 sym_hashes = elf_sym_hashes (bfd_with_globals);
8229 h = sym_hashes [symidx - extsymoff];
8230 while (h->root.type == bfd_link_hash_indirect
8231 || h->root.type == bfd_link_hash_warning)
8232 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8233 h->root.type = bfd_link_hash_defined;
8234 h->root.u.def.value = val;
8235 h->root.u.def.section = bfd_abs_section_ptr;
8239 resolve_symbol (const char *name,
8241 struct elf_final_link_info *flinfo,
8243 Elf_Internal_Sym *isymbuf,
8246 Elf_Internal_Sym *sym;
8247 struct bfd_link_hash_entry *global_entry;
8248 const char *candidate = NULL;
8249 Elf_Internal_Shdr *symtab_hdr;
8252 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8254 for (i = 0; i < locsymcount; ++ i)
8258 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8261 candidate = bfd_elf_string_from_elf_section (input_bfd,
8262 symtab_hdr->sh_link,
8265 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8266 name, candidate, (unsigned long) sym->st_value);
8268 if (candidate && strcmp (candidate, name) == 0)
8270 asection *sec = flinfo->sections [i];
8272 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8273 *result += sec->output_offset + sec->output_section->vma;
8275 printf ("Found symbol with value %8.8lx\n",
8276 (unsigned long) *result);
8282 /* Hmm, haven't found it yet. perhaps it is a global. */
8283 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8284 FALSE, FALSE, TRUE);
8288 if (global_entry->type == bfd_link_hash_defined
8289 || global_entry->type == bfd_link_hash_defweak)
8291 *result = (global_entry->u.def.value
8292 + global_entry->u.def.section->output_section->vma
8293 + global_entry->u.def.section->output_offset);
8295 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8296 global_entry->root.string, (unsigned long) *result);
8304 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8305 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8306 names like "foo.end" which is the end address of section "foo". */
8309 resolve_section (const char *name,
8317 for (curr = sections; curr; curr = curr->next)
8318 if (strcmp (curr->name, name) == 0)
8320 *result = curr->vma;
8324 /* Hmm. still haven't found it. try pseudo-section names. */
8325 /* FIXME: This could be coded more efficiently... */
8326 for (curr = sections; curr; curr = curr->next)
8328 len = strlen (curr->name);
8329 if (len > strlen (name))
8332 if (strncmp (curr->name, name, len) == 0)
8334 if (strncmp (".end", name + len, 4) == 0)
8336 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8340 /* Insert more pseudo-section names here, if you like. */
8348 undefined_reference (const char *reftype, const char *name)
8350 /* xgettext:c-format */
8351 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8356 eval_symbol (bfd_vma *result,
8359 struct elf_final_link_info *flinfo,
8361 Elf_Internal_Sym *isymbuf,
8370 const char *sym = *symp;
8372 bfd_boolean symbol_is_section = FALSE;
8377 if (len < 1 || len > sizeof (symbuf))
8379 bfd_set_error (bfd_error_invalid_operation);
8392 *result = strtoul (sym, (char **) symp, 16);
8396 symbol_is_section = TRUE;
8400 symlen = strtol (sym, (char **) symp, 10);
8401 sym = *symp + 1; /* Skip the trailing ':'. */
8403 if (symend < sym || symlen + 1 > sizeof (symbuf))
8405 bfd_set_error (bfd_error_invalid_operation);
8409 memcpy (symbuf, sym, symlen);
8410 symbuf[symlen] = '\0';
8411 *symp = sym + symlen;
8413 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8414 the symbol as a section, or vice-versa. so we're pretty liberal in our
8415 interpretation here; section means "try section first", not "must be a
8416 section", and likewise with symbol. */
8418 if (symbol_is_section)
8420 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8421 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8422 isymbuf, locsymcount))
8424 undefined_reference ("section", symbuf);
8430 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8431 isymbuf, locsymcount)
8432 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8435 undefined_reference ("symbol", symbuf);
8442 /* All that remains are operators. */
8444 #define UNARY_OP(op) \
8445 if (strncmp (sym, #op, strlen (#op)) == 0) \
8447 sym += strlen (#op); \
8451 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8452 isymbuf, locsymcount, signed_p)) \
8455 *result = op ((bfd_signed_vma) a); \
8461 #define BINARY_OP(op) \
8462 if (strncmp (sym, #op, strlen (#op)) == 0) \
8464 sym += strlen (#op); \
8468 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8469 isymbuf, locsymcount, signed_p)) \
8472 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8473 isymbuf, locsymcount, signed_p)) \
8476 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8506 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8507 bfd_set_error (bfd_error_invalid_operation);
8513 put_value (bfd_vma size,
8514 unsigned long chunksz,
8519 location += (size - chunksz);
8521 for (; size; size -= chunksz, location -= chunksz)
8526 bfd_put_8 (input_bfd, x, location);
8530 bfd_put_16 (input_bfd, x, location);
8534 bfd_put_32 (input_bfd, x, location);
8535 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8541 bfd_put_64 (input_bfd, x, location);
8542 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8555 get_value (bfd_vma size,
8556 unsigned long chunksz,
8563 /* Sanity checks. */
8564 BFD_ASSERT (chunksz <= sizeof (x)
8567 && (size % chunksz) == 0
8568 && input_bfd != NULL
8569 && location != NULL);
8571 if (chunksz == sizeof (x))
8573 BFD_ASSERT (size == chunksz);
8575 /* Make sure that we do not perform an undefined shift operation.
8576 We know that size == chunksz so there will only be one iteration
8577 of the loop below. */
8581 shift = 8 * chunksz;
8583 for (; size; size -= chunksz, location += chunksz)
8588 x = (x << shift) | bfd_get_8 (input_bfd, location);
8591 x = (x << shift) | bfd_get_16 (input_bfd, location);
8594 x = (x << shift) | bfd_get_32 (input_bfd, location);
8598 x = (x << shift) | bfd_get_64 (input_bfd, location);
8609 decode_complex_addend (unsigned long *start, /* in bits */
8610 unsigned long *oplen, /* in bits */
8611 unsigned long *len, /* in bits */
8612 unsigned long *wordsz, /* in bytes */
8613 unsigned long *chunksz, /* in bytes */
8614 unsigned long *lsb0_p,
8615 unsigned long *signed_p,
8616 unsigned long *trunc_p,
8617 unsigned long encoded)
8619 * start = encoded & 0x3F;
8620 * len = (encoded >> 6) & 0x3F;
8621 * oplen = (encoded >> 12) & 0x3F;
8622 * wordsz = (encoded >> 18) & 0xF;
8623 * chunksz = (encoded >> 22) & 0xF;
8624 * lsb0_p = (encoded >> 27) & 1;
8625 * signed_p = (encoded >> 28) & 1;
8626 * trunc_p = (encoded >> 29) & 1;
8629 bfd_reloc_status_type
8630 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8631 asection *input_section ATTRIBUTE_UNUSED,
8633 Elf_Internal_Rela *rel,
8636 bfd_vma shift, x, mask;
8637 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8638 bfd_reloc_status_type r;
8640 /* Perform this reloc, since it is complex.
8641 (this is not to say that it necessarily refers to a complex
8642 symbol; merely that it is a self-describing CGEN based reloc.
8643 i.e. the addend has the complete reloc information (bit start, end,
8644 word size, etc) encoded within it.). */
8646 decode_complex_addend (&start, &oplen, &len, &wordsz,
8647 &chunksz, &lsb0_p, &signed_p,
8648 &trunc_p, rel->r_addend);
8650 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8653 shift = (start + 1) - len;
8655 shift = (8 * wordsz) - (start + len);
8657 x = get_value (wordsz, chunksz, input_bfd,
8658 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8661 printf ("Doing complex reloc: "
8662 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8663 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8664 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8665 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8666 oplen, (unsigned long) x, (unsigned long) mask,
8667 (unsigned long) relocation);
8672 /* Now do an overflow check. */
8673 r = bfd_check_overflow ((signed_p
8674 ? complain_overflow_signed
8675 : complain_overflow_unsigned),
8676 len, 0, (8 * wordsz),
8680 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8683 printf (" relocation: %8.8lx\n"
8684 " shifted mask: %8.8lx\n"
8685 " shifted/masked reloc: %8.8lx\n"
8686 " result: %8.8lx\n",
8687 (unsigned long) relocation, (unsigned long) (mask << shift),
8688 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8690 put_value (wordsz, chunksz, input_bfd, x,
8691 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8695 /* Functions to read r_offset from external (target order) reloc
8696 entry. Faster than bfd_getl32 et al, because we let the compiler
8697 know the value is aligned. */
8700 ext32l_r_offset (const void *p)
8707 const union aligned32 *a
8708 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8710 uint32_t aval = ( (uint32_t) a->c[0]
8711 | (uint32_t) a->c[1] << 8
8712 | (uint32_t) a->c[2] << 16
8713 | (uint32_t) a->c[3] << 24);
8718 ext32b_r_offset (const void *p)
8725 const union aligned32 *a
8726 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8728 uint32_t aval = ( (uint32_t) a->c[0] << 24
8729 | (uint32_t) a->c[1] << 16
8730 | (uint32_t) a->c[2] << 8
8731 | (uint32_t) a->c[3]);
8735 #ifdef BFD_HOST_64_BIT
8737 ext64l_r_offset (const void *p)
8744 const union aligned64 *a
8745 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8747 uint64_t aval = ( (uint64_t) a->c[0]
8748 | (uint64_t) a->c[1] << 8
8749 | (uint64_t) a->c[2] << 16
8750 | (uint64_t) a->c[3] << 24
8751 | (uint64_t) a->c[4] << 32
8752 | (uint64_t) a->c[5] << 40
8753 | (uint64_t) a->c[6] << 48
8754 | (uint64_t) a->c[7] << 56);
8759 ext64b_r_offset (const void *p)
8766 const union aligned64 *a
8767 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8769 uint64_t aval = ( (uint64_t) a->c[0] << 56
8770 | (uint64_t) a->c[1] << 48
8771 | (uint64_t) a->c[2] << 40
8772 | (uint64_t) a->c[3] << 32
8773 | (uint64_t) a->c[4] << 24
8774 | (uint64_t) a->c[5] << 16
8775 | (uint64_t) a->c[6] << 8
8776 | (uint64_t) a->c[7]);
8781 /* When performing a relocatable link, the input relocations are
8782 preserved. But, if they reference global symbols, the indices
8783 referenced must be updated. Update all the relocations found in
8787 elf_link_adjust_relocs (bfd *abfd,
8789 struct bfd_elf_section_reloc_data *reldata,
8791 struct bfd_link_info *info)
8794 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8796 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8797 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8798 bfd_vma r_type_mask;
8800 unsigned int count = reldata->count;
8801 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8803 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8805 swap_in = bed->s->swap_reloc_in;
8806 swap_out = bed->s->swap_reloc_out;
8808 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8810 swap_in = bed->s->swap_reloca_in;
8811 swap_out = bed->s->swap_reloca_out;
8816 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8819 if (bed->s->arch_size == 32)
8826 r_type_mask = 0xffffffff;
8830 erela = reldata->hdr->contents;
8831 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8833 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8836 if (*rel_hash == NULL)
8839 if ((*rel_hash)->indx == -2
8840 && info->gc_sections
8841 && ! info->gc_keep_exported)
8843 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8844 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8846 (*rel_hash)->root.root.string);
8847 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8849 bfd_set_error (bfd_error_invalid_operation);
8852 BFD_ASSERT ((*rel_hash)->indx >= 0);
8854 (*swap_in) (abfd, erela, irela);
8855 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8856 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8857 | (irela[j].r_info & r_type_mask));
8858 (*swap_out) (abfd, irela, erela);
8861 if (bed->elf_backend_update_relocs)
8862 (*bed->elf_backend_update_relocs) (sec, reldata);
8864 if (sort && count != 0)
8866 bfd_vma (*ext_r_off) (const void *);
8869 bfd_byte *base, *end, *p, *loc;
8870 bfd_byte *buf = NULL;
8872 if (bed->s->arch_size == 32)
8874 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8875 ext_r_off = ext32l_r_offset;
8876 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8877 ext_r_off = ext32b_r_offset;
8883 #ifdef BFD_HOST_64_BIT
8884 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8885 ext_r_off = ext64l_r_offset;
8886 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8887 ext_r_off = ext64b_r_offset;
8893 /* Must use a stable sort here. A modified insertion sort,
8894 since the relocs are mostly sorted already. */
8895 elt_size = reldata->hdr->sh_entsize;
8896 base = reldata->hdr->contents;
8897 end = base + count * elt_size;
8898 if (elt_size > sizeof (Elf64_External_Rela))
8901 /* Ensure the first element is lowest. This acts as a sentinel,
8902 speeding the main loop below. */
8903 r_off = (*ext_r_off) (base);
8904 for (p = loc = base; (p += elt_size) < end; )
8906 bfd_vma r_off2 = (*ext_r_off) (p);
8915 /* Don't just swap *base and *loc as that changes the order
8916 of the original base[0] and base[1] if they happen to
8917 have the same r_offset. */
8918 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8919 memcpy (onebuf, loc, elt_size);
8920 memmove (base + elt_size, base, loc - base);
8921 memcpy (base, onebuf, elt_size);
8924 for (p = base + elt_size; (p += elt_size) < end; )
8926 /* base to p is sorted, *p is next to insert. */
8927 r_off = (*ext_r_off) (p);
8928 /* Search the sorted region for location to insert. */
8930 while (r_off < (*ext_r_off) (loc))
8935 /* Chances are there is a run of relocs to insert here,
8936 from one of more input files. Files are not always
8937 linked in order due to the way elf_link_input_bfd is
8938 called. See pr17666. */
8939 size_t sortlen = p - loc;
8940 bfd_vma r_off2 = (*ext_r_off) (loc);
8941 size_t runlen = elt_size;
8942 size_t buf_size = 96 * 1024;
8943 while (p + runlen < end
8944 && (sortlen <= buf_size
8945 || runlen + elt_size <= buf_size)
8946 && r_off2 > (*ext_r_off) (p + runlen))
8950 buf = bfd_malloc (buf_size);
8954 if (runlen < sortlen)
8956 memcpy (buf, p, runlen);
8957 memmove (loc + runlen, loc, sortlen);
8958 memcpy (loc, buf, runlen);
8962 memcpy (buf, loc, sortlen);
8963 memmove (loc, p, runlen);
8964 memcpy (loc + runlen, buf, sortlen);
8966 p += runlen - elt_size;
8969 /* Hashes are no longer valid. */
8970 free (reldata->hashes);
8971 reldata->hashes = NULL;
8977 struct elf_link_sort_rela
8983 enum elf_reloc_type_class type;
8984 /* We use this as an array of size int_rels_per_ext_rel. */
8985 Elf_Internal_Rela rela[1];
8989 elf_link_sort_cmp1 (const void *A, const void *B)
8991 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8992 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8993 int relativea, relativeb;
8995 relativea = a->type == reloc_class_relative;
8996 relativeb = b->type == reloc_class_relative;
8998 if (relativea < relativeb)
9000 if (relativea > relativeb)
9002 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9004 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9006 if (a->rela->r_offset < b->rela->r_offset)
9008 if (a->rela->r_offset > b->rela->r_offset)
9014 elf_link_sort_cmp2 (const void *A, const void *B)
9016 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9017 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9019 if (a->type < b->type)
9021 if (a->type > b->type)
9023 if (a->u.offset < b->u.offset)
9025 if (a->u.offset > b->u.offset)
9027 if (a->rela->r_offset < b->rela->r_offset)
9029 if (a->rela->r_offset > b->rela->r_offset)
9035 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9037 asection *dynamic_relocs;
9040 bfd_size_type count, size;
9041 size_t i, ret, sort_elt, ext_size;
9042 bfd_byte *sort, *s_non_relative, *p;
9043 struct elf_link_sort_rela *sq;
9044 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9045 int i2e = bed->s->int_rels_per_ext_rel;
9046 unsigned int opb = bfd_octets_per_byte (abfd);
9047 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9048 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9049 struct bfd_link_order *lo;
9051 bfd_boolean use_rela;
9053 /* Find a dynamic reloc section. */
9054 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9055 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9056 if (rela_dyn != NULL && rela_dyn->size > 0
9057 && rel_dyn != NULL && rel_dyn->size > 0)
9059 bfd_boolean use_rela_initialised = FALSE;
9061 /* This is just here to stop gcc from complaining.
9062 Its initialization checking code is not perfect. */
9065 /* Both sections are present. Examine the sizes
9066 of the indirect sections to help us choose. */
9067 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9068 if (lo->type == bfd_indirect_link_order)
9070 asection *o = lo->u.indirect.section;
9072 if ((o->size % bed->s->sizeof_rela) == 0)
9074 if ((o->size % bed->s->sizeof_rel) == 0)
9075 /* Section size is divisible by both rel and rela sizes.
9076 It is of no help to us. */
9080 /* Section size is only divisible by rela. */
9081 if (use_rela_initialised && !use_rela)
9083 _bfd_error_handler (_("%pB: unable to sort relocs - "
9084 "they are in more than one size"),
9086 bfd_set_error (bfd_error_invalid_operation);
9092 use_rela_initialised = TRUE;
9096 else if ((o->size % bed->s->sizeof_rel) == 0)
9098 /* Section size is only divisible by rel. */
9099 if (use_rela_initialised && use_rela)
9101 _bfd_error_handler (_("%pB: unable to sort relocs - "
9102 "they are in more than one size"),
9104 bfd_set_error (bfd_error_invalid_operation);
9110 use_rela_initialised = TRUE;
9115 /* The section size is not divisible by either -
9116 something is wrong. */
9117 _bfd_error_handler (_("%pB: unable to sort relocs - "
9118 "they are of an unknown size"), abfd);
9119 bfd_set_error (bfd_error_invalid_operation);
9124 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9125 if (lo->type == bfd_indirect_link_order)
9127 asection *o = lo->u.indirect.section;
9129 if ((o->size % bed->s->sizeof_rela) == 0)
9131 if ((o->size % bed->s->sizeof_rel) == 0)
9132 /* Section size is divisible by both rel and rela sizes.
9133 It is of no help to us. */
9137 /* Section size is only divisible by rela. */
9138 if (use_rela_initialised && !use_rela)
9140 _bfd_error_handler (_("%pB: unable to sort relocs - "
9141 "they are in more than one size"),
9143 bfd_set_error (bfd_error_invalid_operation);
9149 use_rela_initialised = TRUE;
9153 else if ((o->size % bed->s->sizeof_rel) == 0)
9155 /* Section size is only divisible by rel. */
9156 if (use_rela_initialised && use_rela)
9158 _bfd_error_handler (_("%pB: unable to sort relocs - "
9159 "they are in more than one size"),
9161 bfd_set_error (bfd_error_invalid_operation);
9167 use_rela_initialised = TRUE;
9172 /* The section size is not divisible by either -
9173 something is wrong. */
9174 _bfd_error_handler (_("%pB: unable to sort relocs - "
9175 "they are of an unknown size"), abfd);
9176 bfd_set_error (bfd_error_invalid_operation);
9181 if (! use_rela_initialised)
9185 else if (rela_dyn != NULL && rela_dyn->size > 0)
9187 else if (rel_dyn != NULL && rel_dyn->size > 0)
9194 dynamic_relocs = rela_dyn;
9195 ext_size = bed->s->sizeof_rela;
9196 swap_in = bed->s->swap_reloca_in;
9197 swap_out = bed->s->swap_reloca_out;
9201 dynamic_relocs = rel_dyn;
9202 ext_size = bed->s->sizeof_rel;
9203 swap_in = bed->s->swap_reloc_in;
9204 swap_out = bed->s->swap_reloc_out;
9208 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9209 if (lo->type == bfd_indirect_link_order)
9210 size += lo->u.indirect.section->size;
9212 if (size != dynamic_relocs->size)
9215 sort_elt = (sizeof (struct elf_link_sort_rela)
9216 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9218 count = dynamic_relocs->size / ext_size;
9221 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9225 (*info->callbacks->warning)
9226 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9230 if (bed->s->arch_size == 32)
9231 r_sym_mask = ~(bfd_vma) 0xff;
9233 r_sym_mask = ~(bfd_vma) 0xffffffff;
9235 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9236 if (lo->type == bfd_indirect_link_order)
9238 bfd_byte *erel, *erelend;
9239 asection *o = lo->u.indirect.section;
9241 if (o->contents == NULL && o->size != 0)
9243 /* This is a reloc section that is being handled as a normal
9244 section. See bfd_section_from_shdr. We can't combine
9245 relocs in this case. */
9250 erelend = o->contents + o->size;
9251 p = sort + o->output_offset * opb / ext_size * sort_elt;
9253 while (erel < erelend)
9255 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9257 (*swap_in) (abfd, erel, s->rela);
9258 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9259 s->u.sym_mask = r_sym_mask;
9265 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9267 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9269 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9270 if (s->type != reloc_class_relative)
9276 sq = (struct elf_link_sort_rela *) s_non_relative;
9277 for (; i < count; i++, p += sort_elt)
9279 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9280 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9282 sp->u.offset = sq->rela->r_offset;
9285 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9287 struct elf_link_hash_table *htab = elf_hash_table (info);
9288 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9290 /* We have plt relocs in .rela.dyn. */
9291 sq = (struct elf_link_sort_rela *) sort;
9292 for (i = 0; i < count; i++)
9293 if (sq[count - i - 1].type != reloc_class_plt)
9295 if (i != 0 && htab->srelplt->size == i * ext_size)
9297 struct bfd_link_order **plo;
9298 /* Put srelplt link_order last. This is so the output_offset
9299 set in the next loop is correct for DT_JMPREL. */
9300 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9301 if ((*plo)->type == bfd_indirect_link_order
9302 && (*plo)->u.indirect.section == htab->srelplt)
9308 plo = &(*plo)->next;
9311 dynamic_relocs->map_tail.link_order = lo;
9316 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9317 if (lo->type == bfd_indirect_link_order)
9319 bfd_byte *erel, *erelend;
9320 asection *o = lo->u.indirect.section;
9323 erelend = o->contents + o->size;
9324 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9325 while (erel < erelend)
9327 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9328 (*swap_out) (abfd, s->rela, erel);
9335 *psec = dynamic_relocs;
9339 /* Add a symbol to the output symbol string table. */
9342 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9344 Elf_Internal_Sym *elfsym,
9345 asection *input_sec,
9346 struct elf_link_hash_entry *h)
9348 int (*output_symbol_hook)
9349 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9350 struct elf_link_hash_entry *);
9351 struct elf_link_hash_table *hash_table;
9352 const struct elf_backend_data *bed;
9353 bfd_size_type strtabsize;
9355 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9357 bed = get_elf_backend_data (flinfo->output_bfd);
9358 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9359 if (output_symbol_hook != NULL)
9361 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9368 || (input_sec->flags & SEC_EXCLUDE))
9369 elfsym->st_name = (unsigned long) -1;
9372 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9373 to get the final offset for st_name. */
9375 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9377 if (elfsym->st_name == (unsigned long) -1)
9381 hash_table = elf_hash_table (flinfo->info);
9382 strtabsize = hash_table->strtabsize;
9383 if (strtabsize <= hash_table->strtabcount)
9385 strtabsize += strtabsize;
9386 hash_table->strtabsize = strtabsize;
9387 strtabsize *= sizeof (*hash_table->strtab);
9389 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9391 if (hash_table->strtab == NULL)
9394 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9395 hash_table->strtab[hash_table->strtabcount].dest_index
9396 = hash_table->strtabcount;
9397 hash_table->strtab[hash_table->strtabcount].destshndx_index
9398 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9400 bfd_get_symcount (flinfo->output_bfd) += 1;
9401 hash_table->strtabcount += 1;
9406 /* Swap symbols out to the symbol table and flush the output symbols to
9410 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9412 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9415 const struct elf_backend_data *bed;
9417 Elf_Internal_Shdr *hdr;
9421 if (!hash_table->strtabcount)
9424 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9426 bed = get_elf_backend_data (flinfo->output_bfd);
9428 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9429 symbuf = (bfd_byte *) bfd_malloc (amt);
9433 if (flinfo->symshndxbuf)
9435 amt = sizeof (Elf_External_Sym_Shndx);
9436 amt *= bfd_get_symcount (flinfo->output_bfd);
9437 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9438 if (flinfo->symshndxbuf == NULL)
9445 for (i = 0; i < hash_table->strtabcount; i++)
9447 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9448 if (elfsym->sym.st_name == (unsigned long) -1)
9449 elfsym->sym.st_name = 0;
9452 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9453 elfsym->sym.st_name);
9454 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9455 ((bfd_byte *) symbuf
9456 + (elfsym->dest_index
9457 * bed->s->sizeof_sym)),
9458 (flinfo->symshndxbuf
9459 + elfsym->destshndx_index));
9462 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9463 pos = hdr->sh_offset + hdr->sh_size;
9464 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9465 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9466 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9468 hdr->sh_size += amt;
9476 free (hash_table->strtab);
9477 hash_table->strtab = NULL;
9482 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9485 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9487 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9488 && sym->st_shndx < SHN_LORESERVE)
9490 /* The gABI doesn't support dynamic symbols in output sections
9493 /* xgettext:c-format */
9494 (_("%pB: too many sections: %d (>= %d)"),
9495 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9496 bfd_set_error (bfd_error_nonrepresentable_section);
9502 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9503 allowing an unsatisfied unversioned symbol in the DSO to match a
9504 versioned symbol that would normally require an explicit version.
9505 We also handle the case that a DSO references a hidden symbol
9506 which may be satisfied by a versioned symbol in another DSO. */
9509 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9510 const struct elf_backend_data *bed,
9511 struct elf_link_hash_entry *h)
9514 struct elf_link_loaded_list *loaded;
9516 if (!is_elf_hash_table (info->hash))
9519 /* Check indirect symbol. */
9520 while (h->root.type == bfd_link_hash_indirect)
9521 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9523 switch (h->root.type)
9529 case bfd_link_hash_undefined:
9530 case bfd_link_hash_undefweak:
9531 abfd = h->root.u.undef.abfd;
9533 || (abfd->flags & DYNAMIC) == 0
9534 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9538 case bfd_link_hash_defined:
9539 case bfd_link_hash_defweak:
9540 abfd = h->root.u.def.section->owner;
9543 case bfd_link_hash_common:
9544 abfd = h->root.u.c.p->section->owner;
9547 BFD_ASSERT (abfd != NULL);
9549 for (loaded = elf_hash_table (info)->loaded;
9551 loaded = loaded->next)
9554 Elf_Internal_Shdr *hdr;
9558 Elf_Internal_Shdr *versymhdr;
9559 Elf_Internal_Sym *isym;
9560 Elf_Internal_Sym *isymend;
9561 Elf_Internal_Sym *isymbuf;
9562 Elf_External_Versym *ever;
9563 Elf_External_Versym *extversym;
9565 input = loaded->abfd;
9567 /* We check each DSO for a possible hidden versioned definition. */
9569 || (input->flags & DYNAMIC) == 0
9570 || elf_dynversym (input) == 0)
9573 hdr = &elf_tdata (input)->dynsymtab_hdr;
9575 symcount = hdr->sh_size / bed->s->sizeof_sym;
9576 if (elf_bad_symtab (input))
9578 extsymcount = symcount;
9583 extsymcount = symcount - hdr->sh_info;
9584 extsymoff = hdr->sh_info;
9587 if (extsymcount == 0)
9590 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9592 if (isymbuf == NULL)
9595 /* Read in any version definitions. */
9596 versymhdr = &elf_tdata (input)->dynversym_hdr;
9597 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9598 if (extversym == NULL)
9601 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9602 || (bfd_bread (extversym, versymhdr->sh_size, input)
9603 != versymhdr->sh_size))
9611 ever = extversym + extsymoff;
9612 isymend = isymbuf + extsymcount;
9613 for (isym = isymbuf; isym < isymend; isym++, ever++)
9616 Elf_Internal_Versym iver;
9617 unsigned short version_index;
9619 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9620 || isym->st_shndx == SHN_UNDEF)
9623 name = bfd_elf_string_from_elf_section (input,
9626 if (strcmp (name, h->root.root.string) != 0)
9629 _bfd_elf_swap_versym_in (input, ever, &iver);
9631 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9633 && h->forced_local))
9635 /* If we have a non-hidden versioned sym, then it should
9636 have provided a definition for the undefined sym unless
9637 it is defined in a non-shared object and forced local.
9642 version_index = iver.vs_vers & VERSYM_VERSION;
9643 if (version_index == 1 || version_index == 2)
9645 /* This is the base or first version. We can use it. */
9659 /* Convert ELF common symbol TYPE. */
9662 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9664 /* Commom symbol can only appear in relocatable link. */
9665 if (!bfd_link_relocatable (info))
9667 switch (info->elf_stt_common)
9671 case elf_stt_common:
9674 case no_elf_stt_common:
9681 /* Add an external symbol to the symbol table. This is called from
9682 the hash table traversal routine. When generating a shared object,
9683 we go through the symbol table twice. The first time we output
9684 anything that might have been forced to local scope in a version
9685 script. The second time we output the symbols that are still
9689 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9691 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9692 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9693 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9695 Elf_Internal_Sym sym;
9696 asection *input_sec;
9697 const struct elf_backend_data *bed;
9702 if (h->root.type == bfd_link_hash_warning)
9704 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9705 if (h->root.type == bfd_link_hash_new)
9709 /* Decide whether to output this symbol in this pass. */
9710 if (eoinfo->localsyms)
9712 if (!h->forced_local)
9717 if (h->forced_local)
9721 bed = get_elf_backend_data (flinfo->output_bfd);
9723 if (h->root.type == bfd_link_hash_undefined)
9725 /* If we have an undefined symbol reference here then it must have
9726 come from a shared library that is being linked in. (Undefined
9727 references in regular files have already been handled unless
9728 they are in unreferenced sections which are removed by garbage
9730 bfd_boolean ignore_undef = FALSE;
9732 /* Some symbols may be special in that the fact that they're
9733 undefined can be safely ignored - let backend determine that. */
9734 if (bed->elf_backend_ignore_undef_symbol)
9735 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9737 /* If we are reporting errors for this situation then do so now. */
9740 && (!h->ref_regular || flinfo->info->gc_sections)
9741 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9742 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9743 (*flinfo->info->callbacks->undefined_symbol)
9744 (flinfo->info, h->root.root.string,
9745 h->ref_regular ? NULL : h->root.u.undef.abfd,
9747 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9749 /* Strip a global symbol defined in a discarded section. */
9754 /* We should also warn if a forced local symbol is referenced from
9755 shared libraries. */
9756 if (bfd_link_executable (flinfo->info)
9761 && h->ref_dynamic_nonweak
9762 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9766 struct elf_link_hash_entry *hi = h;
9768 /* Check indirect symbol. */
9769 while (hi->root.type == bfd_link_hash_indirect)
9770 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9772 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9773 /* xgettext:c-format */
9774 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9775 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9776 /* xgettext:c-format */
9777 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9779 /* xgettext:c-format */
9780 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9781 def_bfd = flinfo->output_bfd;
9782 if (hi->root.u.def.section != bfd_abs_section_ptr)
9783 def_bfd = hi->root.u.def.section->owner;
9784 _bfd_error_handler (msg, flinfo->output_bfd,
9785 h->root.root.string, def_bfd);
9786 bfd_set_error (bfd_error_bad_value);
9787 eoinfo->failed = TRUE;
9791 /* We don't want to output symbols that have never been mentioned by
9792 a regular file, or that we have been told to strip. However, if
9793 h->indx is set to -2, the symbol is used by a reloc and we must
9798 else if ((h->def_dynamic
9800 || h->root.type == bfd_link_hash_new)
9804 else if (flinfo->info->strip == strip_all)
9806 else if (flinfo->info->strip == strip_some
9807 && bfd_hash_lookup (flinfo->info->keep_hash,
9808 h->root.root.string, FALSE, FALSE) == NULL)
9810 else if ((h->root.type == bfd_link_hash_defined
9811 || h->root.type == bfd_link_hash_defweak)
9812 && ((flinfo->info->strip_discarded
9813 && discarded_section (h->root.u.def.section))
9814 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9815 && h->root.u.def.section->owner != NULL
9816 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9818 else if ((h->root.type == bfd_link_hash_undefined
9819 || h->root.type == bfd_link_hash_undefweak)
9820 && h->root.u.undef.abfd != NULL
9821 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9826 /* If we're stripping it, and it's not a dynamic symbol, there's
9827 nothing else to do. However, if it is a forced local symbol or
9828 an ifunc symbol we need to give the backend finish_dynamic_symbol
9829 function a chance to make it dynamic. */
9832 && type != STT_GNU_IFUNC
9833 && !h->forced_local)
9837 sym.st_size = h->size;
9838 sym.st_other = h->other;
9839 switch (h->root.type)
9842 case bfd_link_hash_new:
9843 case bfd_link_hash_warning:
9847 case bfd_link_hash_undefined:
9848 case bfd_link_hash_undefweak:
9849 input_sec = bfd_und_section_ptr;
9850 sym.st_shndx = SHN_UNDEF;
9853 case bfd_link_hash_defined:
9854 case bfd_link_hash_defweak:
9856 input_sec = h->root.u.def.section;
9857 if (input_sec->output_section != NULL)
9860 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9861 input_sec->output_section);
9862 if (sym.st_shndx == SHN_BAD)
9865 /* xgettext:c-format */
9866 (_("%pB: could not find output section %pA for input section %pA"),
9867 flinfo->output_bfd, input_sec->output_section, input_sec);
9868 bfd_set_error (bfd_error_nonrepresentable_section);
9869 eoinfo->failed = TRUE;
9873 /* ELF symbols in relocatable files are section relative,
9874 but in nonrelocatable files they are virtual
9876 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9877 if (!bfd_link_relocatable (flinfo->info))
9879 sym.st_value += input_sec->output_section->vma;
9880 if (h->type == STT_TLS)
9882 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9883 if (tls_sec != NULL)
9884 sym.st_value -= tls_sec->vma;
9890 BFD_ASSERT (input_sec->owner == NULL
9891 || (input_sec->owner->flags & DYNAMIC) != 0);
9892 sym.st_shndx = SHN_UNDEF;
9893 input_sec = bfd_und_section_ptr;
9898 case bfd_link_hash_common:
9899 input_sec = h->root.u.c.p->section;
9900 sym.st_shndx = bed->common_section_index (input_sec);
9901 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9904 case bfd_link_hash_indirect:
9905 /* These symbols are created by symbol versioning. They point
9906 to the decorated version of the name. For example, if the
9907 symbol foo@@GNU_1.2 is the default, which should be used when
9908 foo is used with no version, then we add an indirect symbol
9909 foo which points to foo@@GNU_1.2. We ignore these symbols,
9910 since the indirected symbol is already in the hash table. */
9914 if (type == STT_COMMON || type == STT_OBJECT)
9915 switch (h->root.type)
9917 case bfd_link_hash_common:
9918 type = elf_link_convert_common_type (flinfo->info, type);
9920 case bfd_link_hash_defined:
9921 case bfd_link_hash_defweak:
9922 if (bed->common_definition (&sym))
9923 type = elf_link_convert_common_type (flinfo->info, type);
9927 case bfd_link_hash_undefined:
9928 case bfd_link_hash_undefweak:
9934 if (h->forced_local)
9936 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9937 /* Turn off visibility on local symbol. */
9938 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9940 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9941 else if (h->unique_global && h->def_regular)
9942 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9943 else if (h->root.type == bfd_link_hash_undefweak
9944 || h->root.type == bfd_link_hash_defweak)
9945 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9947 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9948 sym.st_target_internal = h->target_internal;
9950 /* Give the processor backend a chance to tweak the symbol value,
9951 and also to finish up anything that needs to be done for this
9952 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9953 forced local syms when non-shared is due to a historical quirk.
9954 STT_GNU_IFUNC symbol must go through PLT. */
9955 if ((h->type == STT_GNU_IFUNC
9957 && !bfd_link_relocatable (flinfo->info))
9958 || ((h->dynindx != -1
9960 && ((bfd_link_pic (flinfo->info)
9961 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9962 || h->root.type != bfd_link_hash_undefweak))
9963 || !h->forced_local)
9964 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9966 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9967 (flinfo->output_bfd, flinfo->info, h, &sym)))
9969 eoinfo->failed = TRUE;
9974 /* If we are marking the symbol as undefined, and there are no
9975 non-weak references to this symbol from a regular object, then
9976 mark the symbol as weak undefined; if there are non-weak
9977 references, mark the symbol as strong. We can't do this earlier,
9978 because it might not be marked as undefined until the
9979 finish_dynamic_symbol routine gets through with it. */
9980 if (sym.st_shndx == SHN_UNDEF
9982 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9983 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9986 type = ELF_ST_TYPE (sym.st_info);
9988 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9989 if (type == STT_GNU_IFUNC)
9992 if (h->ref_regular_nonweak)
9993 bindtype = STB_GLOBAL;
9995 bindtype = STB_WEAK;
9996 sym.st_info = ELF_ST_INFO (bindtype, type);
9999 /* If this is a symbol defined in a dynamic library, don't use the
10000 symbol size from the dynamic library. Relinking an executable
10001 against a new library may introduce gratuitous changes in the
10002 executable's symbols if we keep the size. */
10003 if (sym.st_shndx == SHN_UNDEF
10008 /* If a non-weak symbol with non-default visibility is not defined
10009 locally, it is a fatal error. */
10010 if (!bfd_link_relocatable (flinfo->info)
10011 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10012 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10013 && h->root.type == bfd_link_hash_undefined
10014 && !h->def_regular)
10018 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10019 /* xgettext:c-format */
10020 msg = _("%pB: protected symbol `%s' isn't defined");
10021 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10022 /* xgettext:c-format */
10023 msg = _("%pB: internal symbol `%s' isn't defined");
10025 /* xgettext:c-format */
10026 msg = _("%pB: hidden symbol `%s' isn't defined");
10027 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10028 bfd_set_error (bfd_error_bad_value);
10029 eoinfo->failed = TRUE;
10033 /* If this symbol should be put in the .dynsym section, then put it
10034 there now. We already know the symbol index. We also fill in
10035 the entry in the .hash section. */
10036 if (h->dynindx != -1
10037 && elf_hash_table (flinfo->info)->dynamic_sections_created
10038 && elf_hash_table (flinfo->info)->dynsym != NULL
10039 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10043 /* Since there is no version information in the dynamic string,
10044 if there is no version info in symbol version section, we will
10045 have a run-time problem if not linking executable, referenced
10046 by shared library, or not bound locally. */
10047 if (h->verinfo.verdef == NULL
10048 && (!bfd_link_executable (flinfo->info)
10050 || !h->def_regular))
10052 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10054 if (p && p [1] != '\0')
10057 /* xgettext:c-format */
10058 (_("%pB: no symbol version section for versioned symbol `%s'"),
10059 flinfo->output_bfd, h->root.root.string);
10060 eoinfo->failed = TRUE;
10065 sym.st_name = h->dynstr_index;
10066 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10067 + h->dynindx * bed->s->sizeof_sym);
10068 if (!check_dynsym (flinfo->output_bfd, &sym))
10070 eoinfo->failed = TRUE;
10073 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10075 if (flinfo->hash_sec != NULL)
10077 size_t hash_entry_size;
10078 bfd_byte *bucketpos;
10080 size_t bucketcount;
10083 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10084 bucket = h->u.elf_hash_value % bucketcount;
10087 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10088 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10089 + (bucket + 2) * hash_entry_size);
10090 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10091 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10093 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10094 ((bfd_byte *) flinfo->hash_sec->contents
10095 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10098 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10100 Elf_Internal_Versym iversym;
10101 Elf_External_Versym *eversym;
10103 if (!h->def_regular)
10105 if (h->verinfo.verdef == NULL
10106 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10107 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10108 iversym.vs_vers = 0;
10110 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10114 if (h->verinfo.vertree == NULL)
10115 iversym.vs_vers = 1;
10117 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10118 if (flinfo->info->create_default_symver)
10122 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10123 defined locally. */
10124 if (h->versioned == versioned_hidden && h->def_regular)
10125 iversym.vs_vers |= VERSYM_HIDDEN;
10127 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10128 eversym += h->dynindx;
10129 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10133 /* If the symbol is undefined, and we didn't output it to .dynsym,
10134 strip it from .symtab too. Obviously we can't do this for
10135 relocatable output or when needed for --emit-relocs. */
10136 else if (input_sec == bfd_und_section_ptr
10138 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10139 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10140 && !bfd_link_relocatable (flinfo->info))
10143 /* Also strip others that we couldn't earlier due to dynamic symbol
10147 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10150 /* Output a FILE symbol so that following locals are not associated
10151 with the wrong input file. We need one for forced local symbols
10152 if we've seen more than one FILE symbol or when we have exactly
10153 one FILE symbol but global symbols are present in a file other
10154 than the one with the FILE symbol. We also need one if linker
10155 defined symbols are present. In practice these conditions are
10156 always met, so just emit the FILE symbol unconditionally. */
10157 if (eoinfo->localsyms
10158 && !eoinfo->file_sym_done
10159 && eoinfo->flinfo->filesym_count != 0)
10161 Elf_Internal_Sym fsym;
10163 memset (&fsym, 0, sizeof (fsym));
10164 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10165 fsym.st_shndx = SHN_ABS;
10166 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10167 bfd_und_section_ptr, NULL))
10170 eoinfo->file_sym_done = TRUE;
10173 indx = bfd_get_symcount (flinfo->output_bfd);
10174 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10178 eoinfo->failed = TRUE;
10183 else if (h->indx == -2)
10189 /* Return TRUE if special handling is done for relocs in SEC against
10190 symbols defined in discarded sections. */
10193 elf_section_ignore_discarded_relocs (asection *sec)
10195 const struct elf_backend_data *bed;
10197 switch (sec->sec_info_type)
10199 case SEC_INFO_TYPE_STABS:
10200 case SEC_INFO_TYPE_EH_FRAME:
10201 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10207 bed = get_elf_backend_data (sec->owner);
10208 if (bed->elf_backend_ignore_discarded_relocs != NULL
10209 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10215 /* Return a mask saying how ld should treat relocations in SEC against
10216 symbols defined in discarded sections. If this function returns
10217 COMPLAIN set, ld will issue a warning message. If this function
10218 returns PRETEND set, and the discarded section was link-once and the
10219 same size as the kept link-once section, ld will pretend that the
10220 symbol was actually defined in the kept section. Otherwise ld will
10221 zero the reloc (at least that is the intent, but some cooperation by
10222 the target dependent code is needed, particularly for REL targets). */
10225 _bfd_elf_default_action_discarded (asection *sec)
10227 if (sec->flags & SEC_DEBUGGING)
10230 if (strcmp (".eh_frame", sec->name) == 0)
10233 if (strcmp (".gcc_except_table", sec->name) == 0)
10236 return COMPLAIN | PRETEND;
10239 /* Find a match between a section and a member of a section group. */
10242 match_group_member (asection *sec, asection *group,
10243 struct bfd_link_info *info)
10245 asection *first = elf_next_in_group (group);
10246 asection *s = first;
10250 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10253 s = elf_next_in_group (s);
10261 /* Check if the kept section of a discarded section SEC can be used
10262 to replace it. Return the replacement if it is OK. Otherwise return
10266 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10270 kept = sec->kept_section;
10273 if ((kept->flags & SEC_GROUP) != 0)
10274 kept = match_group_member (sec, kept, info);
10276 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10277 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10279 sec->kept_section = kept;
10284 /* Link an input file into the linker output file. This function
10285 handles all the sections and relocations of the input file at once.
10286 This is so that we only have to read the local symbols once, and
10287 don't have to keep them in memory. */
10290 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10292 int (*relocate_section)
10293 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10294 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10296 Elf_Internal_Shdr *symtab_hdr;
10297 size_t locsymcount;
10299 Elf_Internal_Sym *isymbuf;
10300 Elf_Internal_Sym *isym;
10301 Elf_Internal_Sym *isymend;
10303 asection **ppsection;
10305 const struct elf_backend_data *bed;
10306 struct elf_link_hash_entry **sym_hashes;
10307 bfd_size_type address_size;
10308 bfd_vma r_type_mask;
10310 bfd_boolean have_file_sym = FALSE;
10312 output_bfd = flinfo->output_bfd;
10313 bed = get_elf_backend_data (output_bfd);
10314 relocate_section = bed->elf_backend_relocate_section;
10316 /* If this is a dynamic object, we don't want to do anything here:
10317 we don't want the local symbols, and we don't want the section
10319 if ((input_bfd->flags & DYNAMIC) != 0)
10322 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10323 if (elf_bad_symtab (input_bfd))
10325 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10330 locsymcount = symtab_hdr->sh_info;
10331 extsymoff = symtab_hdr->sh_info;
10334 /* Read the local symbols. */
10335 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10336 if (isymbuf == NULL && locsymcount != 0)
10338 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10339 flinfo->internal_syms,
10340 flinfo->external_syms,
10341 flinfo->locsym_shndx);
10342 if (isymbuf == NULL)
10346 /* Find local symbol sections and adjust values of symbols in
10347 SEC_MERGE sections. Write out those local symbols we know are
10348 going into the output file. */
10349 isymend = isymbuf + locsymcount;
10350 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10352 isym++, pindex++, ppsection++)
10356 Elf_Internal_Sym osym;
10362 if (elf_bad_symtab (input_bfd))
10364 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10371 if (isym->st_shndx == SHN_UNDEF)
10372 isec = bfd_und_section_ptr;
10373 else if (isym->st_shndx == SHN_ABS)
10374 isec = bfd_abs_section_ptr;
10375 else if (isym->st_shndx == SHN_COMMON)
10376 isec = bfd_com_section_ptr;
10379 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10382 /* Don't attempt to output symbols with st_shnx in the
10383 reserved range other than SHN_ABS and SHN_COMMON. */
10387 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10388 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10390 _bfd_merged_section_offset (output_bfd, &isec,
10391 elf_section_data (isec)->sec_info,
10397 /* Don't output the first, undefined, symbol. In fact, don't
10398 output any undefined local symbol. */
10399 if (isec == bfd_und_section_ptr)
10402 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10404 /* We never output section symbols. Instead, we use the
10405 section symbol of the corresponding section in the output
10410 /* If we are stripping all symbols, we don't want to output this
10412 if (flinfo->info->strip == strip_all)
10415 /* If we are discarding all local symbols, we don't want to
10416 output this one. If we are generating a relocatable output
10417 file, then some of the local symbols may be required by
10418 relocs; we output them below as we discover that they are
10420 if (flinfo->info->discard == discard_all)
10423 /* If this symbol is defined in a section which we are
10424 discarding, we don't need to keep it. */
10425 if (isym->st_shndx != SHN_UNDEF
10426 && isym->st_shndx < SHN_LORESERVE
10427 && bfd_section_removed_from_list (output_bfd,
10428 isec->output_section))
10431 /* Get the name of the symbol. */
10432 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10437 /* See if we are discarding symbols with this name. */
10438 if ((flinfo->info->strip == strip_some
10439 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10441 || (((flinfo->info->discard == discard_sec_merge
10442 && (isec->flags & SEC_MERGE)
10443 && !bfd_link_relocatable (flinfo->info))
10444 || flinfo->info->discard == discard_l)
10445 && bfd_is_local_label_name (input_bfd, name)))
10448 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10450 if (input_bfd->lto_output)
10451 /* -flto puts a temp file name here. This means builds
10452 are not reproducible. Discard the symbol. */
10454 have_file_sym = TRUE;
10455 flinfo->filesym_count += 1;
10457 if (!have_file_sym)
10459 /* In the absence of debug info, bfd_find_nearest_line uses
10460 FILE symbols to determine the source file for local
10461 function symbols. Provide a FILE symbol here if input
10462 files lack such, so that their symbols won't be
10463 associated with a previous input file. It's not the
10464 source file, but the best we can do. */
10465 have_file_sym = TRUE;
10466 flinfo->filesym_count += 1;
10467 memset (&osym, 0, sizeof (osym));
10468 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10469 osym.st_shndx = SHN_ABS;
10470 if (!elf_link_output_symstrtab (flinfo,
10471 (input_bfd->lto_output ? NULL
10472 : input_bfd->filename),
10473 &osym, bfd_abs_section_ptr,
10480 /* Adjust the section index for the output file. */
10481 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10482 isec->output_section);
10483 if (osym.st_shndx == SHN_BAD)
10486 /* ELF symbols in relocatable files are section relative, but
10487 in executable files they are virtual addresses. Note that
10488 this code assumes that all ELF sections have an associated
10489 BFD section with a reasonable value for output_offset; below
10490 we assume that they also have a reasonable value for
10491 output_section. Any special sections must be set up to meet
10492 these requirements. */
10493 osym.st_value += isec->output_offset;
10494 if (!bfd_link_relocatable (flinfo->info))
10496 osym.st_value += isec->output_section->vma;
10497 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10499 /* STT_TLS symbols are relative to PT_TLS segment base. */
10500 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10501 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10503 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10508 indx = bfd_get_symcount (output_bfd);
10509 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10516 if (bed->s->arch_size == 32)
10518 r_type_mask = 0xff;
10524 r_type_mask = 0xffffffff;
10529 /* Relocate the contents of each section. */
10530 sym_hashes = elf_sym_hashes (input_bfd);
10531 for (o = input_bfd->sections; o != NULL; o = o->next)
10533 bfd_byte *contents;
10535 if (! o->linker_mark)
10537 /* This section was omitted from the link. */
10541 if (!flinfo->info->resolve_section_groups
10542 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10544 /* Deal with the group signature symbol. */
10545 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10546 unsigned long symndx = sec_data->this_hdr.sh_info;
10547 asection *osec = o->output_section;
10549 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10550 if (symndx >= locsymcount
10551 || (elf_bad_symtab (input_bfd)
10552 && flinfo->sections[symndx] == NULL))
10554 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10555 while (h->root.type == bfd_link_hash_indirect
10556 || h->root.type == bfd_link_hash_warning)
10557 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10558 /* Arrange for symbol to be output. */
10560 elf_section_data (osec)->this_hdr.sh_info = -2;
10562 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10564 /* We'll use the output section target_index. */
10565 asection *sec = flinfo->sections[symndx]->output_section;
10566 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10570 if (flinfo->indices[symndx] == -1)
10572 /* Otherwise output the local symbol now. */
10573 Elf_Internal_Sym sym = isymbuf[symndx];
10574 asection *sec = flinfo->sections[symndx]->output_section;
10579 name = bfd_elf_string_from_elf_section (input_bfd,
10580 symtab_hdr->sh_link,
10585 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10587 if (sym.st_shndx == SHN_BAD)
10590 sym.st_value += o->output_offset;
10592 indx = bfd_get_symcount (output_bfd);
10593 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10598 flinfo->indices[symndx] = indx;
10602 elf_section_data (osec)->this_hdr.sh_info
10603 = flinfo->indices[symndx];
10607 if ((o->flags & SEC_HAS_CONTENTS) == 0
10608 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10611 if ((o->flags & SEC_LINKER_CREATED) != 0)
10613 /* Section was created by _bfd_elf_link_create_dynamic_sections
10618 /* Get the contents of the section. They have been cached by a
10619 relaxation routine. Note that o is a section in an input
10620 file, so the contents field will not have been set by any of
10621 the routines which work on output files. */
10622 if (elf_section_data (o)->this_hdr.contents != NULL)
10624 contents = elf_section_data (o)->this_hdr.contents;
10625 if (bed->caches_rawsize
10627 && o->rawsize < o->size)
10629 memcpy (flinfo->contents, contents, o->rawsize);
10630 contents = flinfo->contents;
10635 contents = flinfo->contents;
10636 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10640 if ((o->flags & SEC_RELOC) != 0)
10642 Elf_Internal_Rela *internal_relocs;
10643 Elf_Internal_Rela *rel, *relend;
10644 int action_discarded;
10647 /* Get the swapped relocs. */
10649 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10650 flinfo->internal_relocs, FALSE);
10651 if (internal_relocs == NULL
10652 && o->reloc_count > 0)
10655 /* We need to reverse-copy input .ctors/.dtors sections if
10656 they are placed in .init_array/.finit_array for output. */
10657 if (o->size > address_size
10658 && ((strncmp (o->name, ".ctors", 6) == 0
10659 && strcmp (o->output_section->name,
10660 ".init_array") == 0)
10661 || (strncmp (o->name, ".dtors", 6) == 0
10662 && strcmp (o->output_section->name,
10663 ".fini_array") == 0))
10664 && (o->name[6] == 0 || o->name[6] == '.'))
10666 if (o->size * bed->s->int_rels_per_ext_rel
10667 != o->reloc_count * address_size)
10670 /* xgettext:c-format */
10671 (_("error: %pB: size of section %pA is not "
10672 "multiple of address size"),
10674 bfd_set_error (bfd_error_bad_value);
10677 o->flags |= SEC_ELF_REVERSE_COPY;
10680 action_discarded = -1;
10681 if (!elf_section_ignore_discarded_relocs (o))
10682 action_discarded = (*bed->action_discarded) (o);
10684 /* Run through the relocs evaluating complex reloc symbols and
10685 looking for relocs against symbols from discarded sections
10686 or section symbols from removed link-once sections.
10687 Complain about relocs against discarded sections. Zero
10688 relocs against removed link-once sections. */
10690 rel = internal_relocs;
10691 relend = rel + o->reloc_count;
10692 for ( ; rel < relend; rel++)
10694 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10695 unsigned int s_type;
10696 asection **ps, *sec;
10697 struct elf_link_hash_entry *h = NULL;
10698 const char *sym_name;
10700 if (r_symndx == STN_UNDEF)
10703 if (r_symndx >= locsymcount
10704 || (elf_bad_symtab (input_bfd)
10705 && flinfo->sections[r_symndx] == NULL))
10707 h = sym_hashes[r_symndx - extsymoff];
10709 /* Badly formatted input files can contain relocs that
10710 reference non-existant symbols. Check here so that
10711 we do not seg fault. */
10715 /* xgettext:c-format */
10716 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10717 "that references a non-existent global symbol"),
10718 input_bfd, (uint64_t) rel->r_info, o);
10719 bfd_set_error (bfd_error_bad_value);
10723 while (h->root.type == bfd_link_hash_indirect
10724 || h->root.type == bfd_link_hash_warning)
10725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10729 /* If a plugin symbol is referenced from a non-IR file,
10730 mark the symbol as undefined. Note that the
10731 linker may attach linker created dynamic sections
10732 to the plugin bfd. Symbols defined in linker
10733 created sections are not plugin symbols. */
10734 if ((h->root.non_ir_ref_regular
10735 || h->root.non_ir_ref_dynamic)
10736 && (h->root.type == bfd_link_hash_defined
10737 || h->root.type == bfd_link_hash_defweak)
10738 && (h->root.u.def.section->flags
10739 & SEC_LINKER_CREATED) == 0
10740 && h->root.u.def.section->owner != NULL
10741 && (h->root.u.def.section->owner->flags
10742 & BFD_PLUGIN) != 0)
10744 h->root.type = bfd_link_hash_undefined;
10745 h->root.u.undef.abfd = h->root.u.def.section->owner;
10749 if (h->root.type == bfd_link_hash_defined
10750 || h->root.type == bfd_link_hash_defweak)
10751 ps = &h->root.u.def.section;
10753 sym_name = h->root.root.string;
10757 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10759 s_type = ELF_ST_TYPE (sym->st_info);
10760 ps = &flinfo->sections[r_symndx];
10761 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10765 if ((s_type == STT_RELC || s_type == STT_SRELC)
10766 && !bfd_link_relocatable (flinfo->info))
10769 bfd_vma dot = (rel->r_offset
10770 + o->output_offset + o->output_section->vma);
10772 printf ("Encountered a complex symbol!");
10773 printf (" (input_bfd %s, section %s, reloc %ld\n",
10774 input_bfd->filename, o->name,
10775 (long) (rel - internal_relocs));
10776 printf (" symbol: idx %8.8lx, name %s\n",
10777 r_symndx, sym_name);
10778 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10779 (unsigned long) rel->r_info,
10780 (unsigned long) rel->r_offset);
10782 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10783 isymbuf, locsymcount, s_type == STT_SRELC))
10786 /* Symbol evaluated OK. Update to absolute value. */
10787 set_symbol_value (input_bfd, isymbuf, locsymcount,
10792 if (action_discarded != -1 && ps != NULL)
10794 /* Complain if the definition comes from a
10795 discarded section. */
10796 if ((sec = *ps) != NULL && discarded_section (sec))
10798 BFD_ASSERT (r_symndx != STN_UNDEF);
10799 if (action_discarded & COMPLAIN)
10800 (*flinfo->info->callbacks->einfo)
10801 /* xgettext:c-format */
10802 (_("%X`%s' referenced in section `%pA' of %pB: "
10803 "defined in discarded section `%pA' of %pB\n"),
10804 sym_name, o, input_bfd, sec, sec->owner);
10806 /* Try to do the best we can to support buggy old
10807 versions of gcc. Pretend that the symbol is
10808 really defined in the kept linkonce section.
10809 FIXME: This is quite broken. Modifying the
10810 symbol here means we will be changing all later
10811 uses of the symbol, not just in this section. */
10812 if (action_discarded & PRETEND)
10816 kept = _bfd_elf_check_kept_section (sec,
10828 /* Relocate the section by invoking a back end routine.
10830 The back end routine is responsible for adjusting the
10831 section contents as necessary, and (if using Rela relocs
10832 and generating a relocatable output file) adjusting the
10833 reloc addend as necessary.
10835 The back end routine does not have to worry about setting
10836 the reloc address or the reloc symbol index.
10838 The back end routine is given a pointer to the swapped in
10839 internal symbols, and can access the hash table entries
10840 for the external symbols via elf_sym_hashes (input_bfd).
10842 When generating relocatable output, the back end routine
10843 must handle STB_LOCAL/STT_SECTION symbols specially. The
10844 output symbol is going to be a section symbol
10845 corresponding to the output section, which will require
10846 the addend to be adjusted. */
10848 ret = (*relocate_section) (output_bfd, flinfo->info,
10849 input_bfd, o, contents,
10857 || bfd_link_relocatable (flinfo->info)
10858 || flinfo->info->emitrelocations)
10860 Elf_Internal_Rela *irela;
10861 Elf_Internal_Rela *irelaend, *irelamid;
10862 bfd_vma last_offset;
10863 struct elf_link_hash_entry **rel_hash;
10864 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10865 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10866 unsigned int next_erel;
10867 bfd_boolean rela_normal;
10868 struct bfd_elf_section_data *esdi, *esdo;
10870 esdi = elf_section_data (o);
10871 esdo = elf_section_data (o->output_section);
10872 rela_normal = FALSE;
10874 /* Adjust the reloc addresses and symbol indices. */
10876 irela = internal_relocs;
10877 irelaend = irela + o->reloc_count;
10878 rel_hash = esdo->rel.hashes + esdo->rel.count;
10879 /* We start processing the REL relocs, if any. When we reach
10880 IRELAMID in the loop, we switch to the RELA relocs. */
10882 if (esdi->rel.hdr != NULL)
10883 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10884 * bed->s->int_rels_per_ext_rel);
10885 rel_hash_list = rel_hash;
10886 rela_hash_list = NULL;
10887 last_offset = o->output_offset;
10888 if (!bfd_link_relocatable (flinfo->info))
10889 last_offset += o->output_section->vma;
10890 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10892 unsigned long r_symndx;
10894 Elf_Internal_Sym sym;
10896 if (next_erel == bed->s->int_rels_per_ext_rel)
10902 if (irela == irelamid)
10904 rel_hash = esdo->rela.hashes + esdo->rela.count;
10905 rela_hash_list = rel_hash;
10906 rela_normal = bed->rela_normal;
10909 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10912 if (irela->r_offset >= (bfd_vma) -2)
10914 /* This is a reloc for a deleted entry or somesuch.
10915 Turn it into an R_*_NONE reloc, at the same
10916 offset as the last reloc. elf_eh_frame.c and
10917 bfd_elf_discard_info rely on reloc offsets
10919 irela->r_offset = last_offset;
10921 irela->r_addend = 0;
10925 irela->r_offset += o->output_offset;
10927 /* Relocs in an executable have to be virtual addresses. */
10928 if (!bfd_link_relocatable (flinfo->info))
10929 irela->r_offset += o->output_section->vma;
10931 last_offset = irela->r_offset;
10933 r_symndx = irela->r_info >> r_sym_shift;
10934 if (r_symndx == STN_UNDEF)
10937 if (r_symndx >= locsymcount
10938 || (elf_bad_symtab (input_bfd)
10939 && flinfo->sections[r_symndx] == NULL))
10941 struct elf_link_hash_entry *rh;
10942 unsigned long indx;
10944 /* This is a reloc against a global symbol. We
10945 have not yet output all the local symbols, so
10946 we do not know the symbol index of any global
10947 symbol. We set the rel_hash entry for this
10948 reloc to point to the global hash table entry
10949 for this symbol. The symbol index is then
10950 set at the end of bfd_elf_final_link. */
10951 indx = r_symndx - extsymoff;
10952 rh = elf_sym_hashes (input_bfd)[indx];
10953 while (rh->root.type == bfd_link_hash_indirect
10954 || rh->root.type == bfd_link_hash_warning)
10955 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10957 /* Setting the index to -2 tells
10958 elf_link_output_extsym that this symbol is
10959 used by a reloc. */
10960 BFD_ASSERT (rh->indx < 0);
10967 /* This is a reloc against a local symbol. */
10970 sym = isymbuf[r_symndx];
10971 sec = flinfo->sections[r_symndx];
10972 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10974 /* I suppose the backend ought to fill in the
10975 section of any STT_SECTION symbol against a
10976 processor specific section. */
10977 r_symndx = STN_UNDEF;
10978 if (bfd_is_abs_section (sec))
10980 else if (sec == NULL || sec->owner == NULL)
10982 bfd_set_error (bfd_error_bad_value);
10987 asection *osec = sec->output_section;
10989 /* If we have discarded a section, the output
10990 section will be the absolute section. In
10991 case of discarded SEC_MERGE sections, use
10992 the kept section. relocate_section should
10993 have already handled discarded linkonce
10995 if (bfd_is_abs_section (osec)
10996 && sec->kept_section != NULL
10997 && sec->kept_section->output_section != NULL)
10999 osec = sec->kept_section->output_section;
11000 irela->r_addend -= osec->vma;
11003 if (!bfd_is_abs_section (osec))
11005 r_symndx = osec->target_index;
11006 if (r_symndx == STN_UNDEF)
11008 irela->r_addend += osec->vma;
11009 osec = _bfd_nearby_section (output_bfd, osec,
11011 irela->r_addend -= osec->vma;
11012 r_symndx = osec->target_index;
11017 /* Adjust the addend according to where the
11018 section winds up in the output section. */
11020 irela->r_addend += sec->output_offset;
11024 if (flinfo->indices[r_symndx] == -1)
11026 unsigned long shlink;
11031 if (flinfo->info->strip == strip_all)
11033 /* You can't do ld -r -s. */
11034 bfd_set_error (bfd_error_invalid_operation);
11038 /* This symbol was skipped earlier, but
11039 since it is needed by a reloc, we
11040 must output it now. */
11041 shlink = symtab_hdr->sh_link;
11042 name = (bfd_elf_string_from_elf_section
11043 (input_bfd, shlink, sym.st_name));
11047 osec = sec->output_section;
11049 _bfd_elf_section_from_bfd_section (output_bfd,
11051 if (sym.st_shndx == SHN_BAD)
11054 sym.st_value += sec->output_offset;
11055 if (!bfd_link_relocatable (flinfo->info))
11057 sym.st_value += osec->vma;
11058 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11060 struct elf_link_hash_table *htab
11061 = elf_hash_table (flinfo->info);
11063 /* STT_TLS symbols are relative to PT_TLS
11065 if (htab->tls_sec != NULL)
11066 sym.st_value -= htab->tls_sec->vma;
11069 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11074 indx = bfd_get_symcount (output_bfd);
11075 ret = elf_link_output_symstrtab (flinfo, name,
11081 flinfo->indices[r_symndx] = indx;
11086 r_symndx = flinfo->indices[r_symndx];
11089 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11090 | (irela->r_info & r_type_mask));
11093 /* Swap out the relocs. */
11094 input_rel_hdr = esdi->rel.hdr;
11095 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11097 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11102 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11103 * bed->s->int_rels_per_ext_rel);
11104 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11107 input_rela_hdr = esdi->rela.hdr;
11108 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11110 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11119 /* Write out the modified section contents. */
11120 if (bed->elf_backend_write_section
11121 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11124 /* Section written out. */
11126 else switch (o->sec_info_type)
11128 case SEC_INFO_TYPE_STABS:
11129 if (! (_bfd_write_section_stabs
11131 &elf_hash_table (flinfo->info)->stab_info,
11132 o, &elf_section_data (o)->sec_info, contents)))
11135 case SEC_INFO_TYPE_MERGE:
11136 if (! _bfd_write_merged_section (output_bfd, o,
11137 elf_section_data (o)->sec_info))
11140 case SEC_INFO_TYPE_EH_FRAME:
11142 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11147 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11149 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11157 if (! (o->flags & SEC_EXCLUDE))
11159 file_ptr offset = (file_ptr) o->output_offset;
11160 bfd_size_type todo = o->size;
11162 offset *= bfd_octets_per_byte (output_bfd);
11164 if ((o->flags & SEC_ELF_REVERSE_COPY))
11166 /* Reverse-copy input section to output. */
11169 todo -= address_size;
11170 if (! bfd_set_section_contents (output_bfd,
11178 offset += address_size;
11182 else if (! bfd_set_section_contents (output_bfd,
11196 /* Generate a reloc when linking an ELF file. This is a reloc
11197 requested by the linker, and does not come from any input file. This
11198 is used to build constructor and destructor tables when linking
11202 elf_reloc_link_order (bfd *output_bfd,
11203 struct bfd_link_info *info,
11204 asection *output_section,
11205 struct bfd_link_order *link_order)
11207 reloc_howto_type *howto;
11211 struct bfd_elf_section_reloc_data *reldata;
11212 struct elf_link_hash_entry **rel_hash_ptr;
11213 Elf_Internal_Shdr *rel_hdr;
11214 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11215 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11218 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11220 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11223 bfd_set_error (bfd_error_bad_value);
11227 addend = link_order->u.reloc.p->addend;
11230 reldata = &esdo->rel;
11231 else if (esdo->rela.hdr)
11232 reldata = &esdo->rela;
11239 /* Figure out the symbol index. */
11240 rel_hash_ptr = reldata->hashes + reldata->count;
11241 if (link_order->type == bfd_section_reloc_link_order)
11243 indx = link_order->u.reloc.p->u.section->target_index;
11244 BFD_ASSERT (indx != 0);
11245 *rel_hash_ptr = NULL;
11249 struct elf_link_hash_entry *h;
11251 /* Treat a reloc against a defined symbol as though it were
11252 actually against the section. */
11253 h = ((struct elf_link_hash_entry *)
11254 bfd_wrapped_link_hash_lookup (output_bfd, info,
11255 link_order->u.reloc.p->u.name,
11256 FALSE, FALSE, TRUE));
11258 && (h->root.type == bfd_link_hash_defined
11259 || h->root.type == bfd_link_hash_defweak))
11263 section = h->root.u.def.section;
11264 indx = section->output_section->target_index;
11265 *rel_hash_ptr = NULL;
11266 /* It seems that we ought to add the symbol value to the
11267 addend here, but in practice it has already been added
11268 because it was passed to constructor_callback. */
11269 addend += section->output_section->vma + section->output_offset;
11271 else if (h != NULL)
11273 /* Setting the index to -2 tells elf_link_output_extsym that
11274 this symbol is used by a reloc. */
11281 (*info->callbacks->unattached_reloc)
11282 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11287 /* If this is an inplace reloc, we must write the addend into the
11289 if (howto->partial_inplace && addend != 0)
11291 bfd_size_type size;
11292 bfd_reloc_status_type rstat;
11295 const char *sym_name;
11297 size = (bfd_size_type) bfd_get_reloc_size (howto);
11298 buf = (bfd_byte *) bfd_zmalloc (size);
11299 if (buf == NULL && size != 0)
11301 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11308 case bfd_reloc_outofrange:
11311 case bfd_reloc_overflow:
11312 if (link_order->type == bfd_section_reloc_link_order)
11313 sym_name = bfd_section_name (output_bfd,
11314 link_order->u.reloc.p->u.section);
11316 sym_name = link_order->u.reloc.p->u.name;
11317 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11318 howto->name, addend, NULL, NULL,
11323 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11325 * bfd_octets_per_byte (output_bfd),
11332 /* The address of a reloc is relative to the section in a
11333 relocatable file, and is a virtual address in an executable
11335 offset = link_order->offset;
11336 if (! bfd_link_relocatable (info))
11337 offset += output_section->vma;
11339 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11341 irel[i].r_offset = offset;
11342 irel[i].r_info = 0;
11343 irel[i].r_addend = 0;
11345 if (bed->s->arch_size == 32)
11346 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11348 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11350 rel_hdr = reldata->hdr;
11351 erel = rel_hdr->contents;
11352 if (rel_hdr->sh_type == SHT_REL)
11354 erel += reldata->count * bed->s->sizeof_rel;
11355 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11359 irel[0].r_addend = addend;
11360 erel += reldata->count * bed->s->sizeof_rela;
11361 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11370 /* Get the output vma of the section pointed to by the sh_link field. */
11373 elf_get_linked_section_vma (struct bfd_link_order *p)
11375 Elf_Internal_Shdr **elf_shdrp;
11379 s = p->u.indirect.section;
11380 elf_shdrp = elf_elfsections (s->owner);
11381 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11382 elfsec = elf_shdrp[elfsec]->sh_link;
11384 The Intel C compiler generates SHT_IA_64_UNWIND with
11385 SHF_LINK_ORDER. But it doesn't set the sh_link or
11386 sh_info fields. Hence we could get the situation
11387 where elfsec is 0. */
11390 const struct elf_backend_data *bed
11391 = get_elf_backend_data (s->owner);
11392 if (bed->link_order_error_handler)
11393 bed->link_order_error_handler
11394 /* xgettext:c-format */
11395 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11400 s = elf_shdrp[elfsec]->bfd_section;
11401 return s->output_section->vma + s->output_offset;
11406 /* Compare two sections based on the locations of the sections they are
11407 linked to. Used by elf_fixup_link_order. */
11410 compare_link_order (const void * a, const void * b)
11415 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11416 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11419 return apos > bpos;
11423 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11424 order as their linked sections. Returns false if this could not be done
11425 because an output section includes both ordered and unordered
11426 sections. Ideally we'd do this in the linker proper. */
11429 elf_fixup_link_order (bfd *abfd, asection *o)
11431 int seen_linkorder;
11434 struct bfd_link_order *p;
11436 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11438 struct bfd_link_order **sections;
11439 asection *s, *other_sec, *linkorder_sec;
11443 linkorder_sec = NULL;
11445 seen_linkorder = 0;
11446 for (p = o->map_head.link_order; p != NULL; p = p->next)
11448 if (p->type == bfd_indirect_link_order)
11450 s = p->u.indirect.section;
11452 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11453 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11454 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11455 && elfsec < elf_numsections (sub)
11456 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11457 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11471 if (seen_other && seen_linkorder)
11473 if (other_sec && linkorder_sec)
11475 /* xgettext:c-format */
11476 (_("%pA has both ordered [`%pA' in %pB] "
11477 "and unordered [`%pA' in %pB] sections"),
11478 o, linkorder_sec, linkorder_sec->owner,
11479 other_sec, other_sec->owner);
11482 (_("%pA has both ordered and unordered sections"), o);
11483 bfd_set_error (bfd_error_bad_value);
11488 if (!seen_linkorder)
11491 sections = (struct bfd_link_order **)
11492 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11493 if (sections == NULL)
11495 seen_linkorder = 0;
11497 for (p = o->map_head.link_order; p != NULL; p = p->next)
11499 sections[seen_linkorder++] = p;
11501 /* Sort the input sections in the order of their linked section. */
11502 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11503 compare_link_order);
11505 /* Change the offsets of the sections. */
11507 for (n = 0; n < seen_linkorder; n++)
11509 s = sections[n]->u.indirect.section;
11510 offset &= ~(bfd_vma) 0 << s->alignment_power;
11511 s->output_offset = offset / bfd_octets_per_byte (abfd);
11512 sections[n]->offset = offset;
11513 offset += sections[n]->size;
11520 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11521 Returns TRUE upon success, FALSE otherwise. */
11524 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11526 bfd_boolean ret = FALSE;
11528 const struct elf_backend_data *bed;
11530 enum bfd_architecture arch;
11532 asymbol **sympp = NULL;
11536 elf_symbol_type *osymbuf;
11538 implib_bfd = info->out_implib_bfd;
11539 bed = get_elf_backend_data (abfd);
11541 if (!bfd_set_format (implib_bfd, bfd_object))
11544 /* Use flag from executable but make it a relocatable object. */
11545 flags = bfd_get_file_flags (abfd);
11546 flags &= ~HAS_RELOC;
11547 if (!bfd_set_start_address (implib_bfd, 0)
11548 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11551 /* Copy architecture of output file to import library file. */
11552 arch = bfd_get_arch (abfd);
11553 mach = bfd_get_mach (abfd);
11554 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11555 && (abfd->target_defaulted
11556 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11559 /* Get symbol table size. */
11560 symsize = bfd_get_symtab_upper_bound (abfd);
11564 /* Read in the symbol table. */
11565 sympp = (asymbol **) xmalloc (symsize);
11566 symcount = bfd_canonicalize_symtab (abfd, sympp);
11570 /* Allow the BFD backend to copy any private header data it
11571 understands from the output BFD to the import library BFD. */
11572 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11575 /* Filter symbols to appear in the import library. */
11576 if (bed->elf_backend_filter_implib_symbols)
11577 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11580 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11583 bfd_set_error (bfd_error_no_symbols);
11584 _bfd_error_handler (_("%pB: no symbol found for import library"),
11590 /* Make symbols absolute. */
11591 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11592 sizeof (*osymbuf));
11593 for (src_count = 0; src_count < symcount; src_count++)
11595 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11596 sizeof (*osymbuf));
11597 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11598 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11599 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11600 osymbuf[src_count].internal_elf_sym.st_value =
11601 osymbuf[src_count].symbol.value;
11602 sympp[src_count] = &osymbuf[src_count].symbol;
11605 bfd_set_symtab (implib_bfd, sympp, symcount);
11607 /* Allow the BFD backend to copy any private data it understands
11608 from the output BFD to the import library BFD. This is done last
11609 to permit the routine to look at the filtered symbol table. */
11610 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11613 if (!bfd_close (implib_bfd))
11624 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11628 if (flinfo->symstrtab != NULL)
11629 _bfd_elf_strtab_free (flinfo->symstrtab);
11630 if (flinfo->contents != NULL)
11631 free (flinfo->contents);
11632 if (flinfo->external_relocs != NULL)
11633 free (flinfo->external_relocs);
11634 if (flinfo->internal_relocs != NULL)
11635 free (flinfo->internal_relocs);
11636 if (flinfo->external_syms != NULL)
11637 free (flinfo->external_syms);
11638 if (flinfo->locsym_shndx != NULL)
11639 free (flinfo->locsym_shndx);
11640 if (flinfo->internal_syms != NULL)
11641 free (flinfo->internal_syms);
11642 if (flinfo->indices != NULL)
11643 free (flinfo->indices);
11644 if (flinfo->sections != NULL)
11645 free (flinfo->sections);
11646 if (flinfo->symshndxbuf != NULL)
11647 free (flinfo->symshndxbuf);
11648 for (o = obfd->sections; o != NULL; o = o->next)
11650 struct bfd_elf_section_data *esdo = elf_section_data (o);
11651 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11652 free (esdo->rel.hashes);
11653 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11654 free (esdo->rela.hashes);
11658 /* Do the final step of an ELF link. */
11661 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11663 bfd_boolean dynamic;
11664 bfd_boolean emit_relocs;
11666 struct elf_final_link_info flinfo;
11668 struct bfd_link_order *p;
11670 bfd_size_type max_contents_size;
11671 bfd_size_type max_external_reloc_size;
11672 bfd_size_type max_internal_reloc_count;
11673 bfd_size_type max_sym_count;
11674 bfd_size_type max_sym_shndx_count;
11675 Elf_Internal_Sym elfsym;
11677 Elf_Internal_Shdr *symtab_hdr;
11678 Elf_Internal_Shdr *symtab_shndx_hdr;
11679 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11680 struct elf_outext_info eoinfo;
11681 bfd_boolean merged;
11682 size_t relativecount = 0;
11683 asection *reldyn = 0;
11685 asection *attr_section = NULL;
11686 bfd_vma attr_size = 0;
11687 const char *std_attrs_section;
11688 struct elf_link_hash_table *htab = elf_hash_table (info);
11690 if (!is_elf_hash_table (htab))
11693 if (bfd_link_pic (info))
11694 abfd->flags |= DYNAMIC;
11696 dynamic = htab->dynamic_sections_created;
11697 dynobj = htab->dynobj;
11699 emit_relocs = (bfd_link_relocatable (info)
11700 || info->emitrelocations);
11702 flinfo.info = info;
11703 flinfo.output_bfd = abfd;
11704 flinfo.symstrtab = _bfd_elf_strtab_init ();
11705 if (flinfo.symstrtab == NULL)
11710 flinfo.hash_sec = NULL;
11711 flinfo.symver_sec = NULL;
11715 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11716 /* Note that dynsym_sec can be NULL (on VMS). */
11717 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11718 /* Note that it is OK if symver_sec is NULL. */
11721 flinfo.contents = NULL;
11722 flinfo.external_relocs = NULL;
11723 flinfo.internal_relocs = NULL;
11724 flinfo.external_syms = NULL;
11725 flinfo.locsym_shndx = NULL;
11726 flinfo.internal_syms = NULL;
11727 flinfo.indices = NULL;
11728 flinfo.sections = NULL;
11729 flinfo.symshndxbuf = NULL;
11730 flinfo.filesym_count = 0;
11732 /* The object attributes have been merged. Remove the input
11733 sections from the link, and set the contents of the output
11735 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11736 for (o = abfd->sections; o != NULL; o = o->next)
11738 bfd_boolean remove_section = FALSE;
11740 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11741 || strcmp (o->name, ".gnu.attributes") == 0)
11743 for (p = o->map_head.link_order; p != NULL; p = p->next)
11745 asection *input_section;
11747 if (p->type != bfd_indirect_link_order)
11749 input_section = p->u.indirect.section;
11750 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11751 elf_link_input_bfd ignores this section. */
11752 input_section->flags &= ~SEC_HAS_CONTENTS;
11755 attr_size = bfd_elf_obj_attr_size (abfd);
11756 bfd_set_section_size (abfd, o, attr_size);
11757 /* Skip this section later on. */
11758 o->map_head.link_order = NULL;
11762 remove_section = TRUE;
11764 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11766 /* Remove empty group section from linker output. */
11767 remove_section = TRUE;
11769 if (remove_section)
11771 o->flags |= SEC_EXCLUDE;
11772 bfd_section_list_remove (abfd, o);
11773 abfd->section_count--;
11777 /* Count up the number of relocations we will output for each output
11778 section, so that we know the sizes of the reloc sections. We
11779 also figure out some maximum sizes. */
11780 max_contents_size = 0;
11781 max_external_reloc_size = 0;
11782 max_internal_reloc_count = 0;
11784 max_sym_shndx_count = 0;
11786 for (o = abfd->sections; o != NULL; o = o->next)
11788 struct bfd_elf_section_data *esdo = elf_section_data (o);
11789 o->reloc_count = 0;
11791 for (p = o->map_head.link_order; p != NULL; p = p->next)
11793 unsigned int reloc_count = 0;
11794 unsigned int additional_reloc_count = 0;
11795 struct bfd_elf_section_data *esdi = NULL;
11797 if (p->type == bfd_section_reloc_link_order
11798 || p->type == bfd_symbol_reloc_link_order)
11800 else if (p->type == bfd_indirect_link_order)
11804 sec = p->u.indirect.section;
11806 /* Mark all sections which are to be included in the
11807 link. This will normally be every section. We need
11808 to do this so that we can identify any sections which
11809 the linker has decided to not include. */
11810 sec->linker_mark = TRUE;
11812 if (sec->flags & SEC_MERGE)
11815 if (sec->rawsize > max_contents_size)
11816 max_contents_size = sec->rawsize;
11817 if (sec->size > max_contents_size)
11818 max_contents_size = sec->size;
11820 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11821 && (sec->owner->flags & DYNAMIC) == 0)
11825 /* We are interested in just local symbols, not all
11827 if (elf_bad_symtab (sec->owner))
11828 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11829 / bed->s->sizeof_sym);
11831 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11833 if (sym_count > max_sym_count)
11834 max_sym_count = sym_count;
11836 if (sym_count > max_sym_shndx_count
11837 && elf_symtab_shndx_list (sec->owner) != NULL)
11838 max_sym_shndx_count = sym_count;
11840 if (esdo->this_hdr.sh_type == SHT_REL
11841 || esdo->this_hdr.sh_type == SHT_RELA)
11842 /* Some backends use reloc_count in relocation sections
11843 to count particular types of relocs. Of course,
11844 reloc sections themselves can't have relocations. */
11846 else if (emit_relocs)
11848 reloc_count = sec->reloc_count;
11849 if (bed->elf_backend_count_additional_relocs)
11852 c = (*bed->elf_backend_count_additional_relocs) (sec);
11853 additional_reloc_count += c;
11856 else if (bed->elf_backend_count_relocs)
11857 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11859 esdi = elf_section_data (sec);
11861 if ((sec->flags & SEC_RELOC) != 0)
11863 size_t ext_size = 0;
11865 if (esdi->rel.hdr != NULL)
11866 ext_size = esdi->rel.hdr->sh_size;
11867 if (esdi->rela.hdr != NULL)
11868 ext_size += esdi->rela.hdr->sh_size;
11870 if (ext_size > max_external_reloc_size)
11871 max_external_reloc_size = ext_size;
11872 if (sec->reloc_count > max_internal_reloc_count)
11873 max_internal_reloc_count = sec->reloc_count;
11878 if (reloc_count == 0)
11881 reloc_count += additional_reloc_count;
11882 o->reloc_count += reloc_count;
11884 if (p->type == bfd_indirect_link_order && emit_relocs)
11888 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11889 esdo->rel.count += additional_reloc_count;
11891 if (esdi->rela.hdr)
11893 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11894 esdo->rela.count += additional_reloc_count;
11900 esdo->rela.count += reloc_count;
11902 esdo->rel.count += reloc_count;
11906 if (o->reloc_count > 0)
11907 o->flags |= SEC_RELOC;
11910 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11911 set it (this is probably a bug) and if it is set
11912 assign_section_numbers will create a reloc section. */
11913 o->flags &=~ SEC_RELOC;
11916 /* If the SEC_ALLOC flag is not set, force the section VMA to
11917 zero. This is done in elf_fake_sections as well, but forcing
11918 the VMA to 0 here will ensure that relocs against these
11919 sections are handled correctly. */
11920 if ((o->flags & SEC_ALLOC) == 0
11921 && ! o->user_set_vma)
11925 if (! bfd_link_relocatable (info) && merged)
11926 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11928 /* Figure out the file positions for everything but the symbol table
11929 and the relocs. We set symcount to force assign_section_numbers
11930 to create a symbol table. */
11931 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11932 BFD_ASSERT (! abfd->output_has_begun);
11933 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11936 /* Set sizes, and assign file positions for reloc sections. */
11937 for (o = abfd->sections; o != NULL; o = o->next)
11939 struct bfd_elf_section_data *esdo = elf_section_data (o);
11940 if ((o->flags & SEC_RELOC) != 0)
11943 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11947 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11951 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11952 to count upwards while actually outputting the relocations. */
11953 esdo->rel.count = 0;
11954 esdo->rela.count = 0;
11956 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11958 /* Cache the section contents so that they can be compressed
11959 later. Use bfd_malloc since it will be freed by
11960 bfd_compress_section_contents. */
11961 unsigned char *contents = esdo->this_hdr.contents;
11962 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11965 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11966 if (contents == NULL)
11968 esdo->this_hdr.contents = contents;
11972 /* We have now assigned file positions for all the sections except
11973 .symtab, .strtab, and non-loaded reloc sections. We start the
11974 .symtab section at the current file position, and write directly
11975 to it. We build the .strtab section in memory. */
11976 bfd_get_symcount (abfd) = 0;
11977 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11978 /* sh_name is set in prep_headers. */
11979 symtab_hdr->sh_type = SHT_SYMTAB;
11980 /* sh_flags, sh_addr and sh_size all start off zero. */
11981 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11982 /* sh_link is set in assign_section_numbers. */
11983 /* sh_info is set below. */
11984 /* sh_offset is set just below. */
11985 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11987 if (max_sym_count < 20)
11988 max_sym_count = 20;
11989 htab->strtabsize = max_sym_count;
11990 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11991 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11992 if (htab->strtab == NULL)
11994 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11996 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11997 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11999 if (info->strip != strip_all || emit_relocs)
12001 file_ptr off = elf_next_file_pos (abfd);
12003 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12005 /* Note that at this point elf_next_file_pos (abfd) is
12006 incorrect. We do not yet know the size of the .symtab section.
12007 We correct next_file_pos below, after we do know the size. */
12009 /* Start writing out the symbol table. The first symbol is always a
12011 elfsym.st_value = 0;
12012 elfsym.st_size = 0;
12013 elfsym.st_info = 0;
12014 elfsym.st_other = 0;
12015 elfsym.st_shndx = SHN_UNDEF;
12016 elfsym.st_target_internal = 0;
12017 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12018 bfd_und_section_ptr, NULL) != 1)
12021 /* Output a symbol for each section. We output these even if we are
12022 discarding local symbols, since they are used for relocs. These
12023 symbols have no names. We store the index of each one in the
12024 index field of the section, so that we can find it again when
12025 outputting relocs. */
12027 elfsym.st_size = 0;
12028 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12029 elfsym.st_other = 0;
12030 elfsym.st_value = 0;
12031 elfsym.st_target_internal = 0;
12032 for (i = 1; i < elf_numsections (abfd); i++)
12034 o = bfd_section_from_elf_index (abfd, i);
12037 o->target_index = bfd_get_symcount (abfd);
12038 elfsym.st_shndx = i;
12039 if (!bfd_link_relocatable (info))
12040 elfsym.st_value = o->vma;
12041 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12048 /* Allocate some memory to hold information read in from the input
12050 if (max_contents_size != 0)
12052 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12053 if (flinfo.contents == NULL)
12057 if (max_external_reloc_size != 0)
12059 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12060 if (flinfo.external_relocs == NULL)
12064 if (max_internal_reloc_count != 0)
12066 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12067 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12068 if (flinfo.internal_relocs == NULL)
12072 if (max_sym_count != 0)
12074 amt = max_sym_count * bed->s->sizeof_sym;
12075 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12076 if (flinfo.external_syms == NULL)
12079 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12080 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12081 if (flinfo.internal_syms == NULL)
12084 amt = max_sym_count * sizeof (long);
12085 flinfo.indices = (long int *) bfd_malloc (amt);
12086 if (flinfo.indices == NULL)
12089 amt = max_sym_count * sizeof (asection *);
12090 flinfo.sections = (asection **) bfd_malloc (amt);
12091 if (flinfo.sections == NULL)
12095 if (max_sym_shndx_count != 0)
12097 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12098 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12099 if (flinfo.locsym_shndx == NULL)
12105 bfd_vma base, end = 0;
12108 for (sec = htab->tls_sec;
12109 sec && (sec->flags & SEC_THREAD_LOCAL);
12112 bfd_size_type size = sec->size;
12115 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12117 struct bfd_link_order *ord = sec->map_tail.link_order;
12120 size = ord->offset + ord->size;
12122 end = sec->vma + size;
12124 base = htab->tls_sec->vma;
12125 /* Only align end of TLS section if static TLS doesn't have special
12126 alignment requirements. */
12127 if (bed->static_tls_alignment == 1)
12128 end = align_power (end, htab->tls_sec->alignment_power);
12129 htab->tls_size = end - base;
12132 /* Reorder SHF_LINK_ORDER sections. */
12133 for (o = abfd->sections; o != NULL; o = o->next)
12135 if (!elf_fixup_link_order (abfd, o))
12139 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12142 /* Since ELF permits relocations to be against local symbols, we
12143 must have the local symbols available when we do the relocations.
12144 Since we would rather only read the local symbols once, and we
12145 would rather not keep them in memory, we handle all the
12146 relocations for a single input file at the same time.
12148 Unfortunately, there is no way to know the total number of local
12149 symbols until we have seen all of them, and the local symbol
12150 indices precede the global symbol indices. This means that when
12151 we are generating relocatable output, and we see a reloc against
12152 a global symbol, we can not know the symbol index until we have
12153 finished examining all the local symbols to see which ones we are
12154 going to output. To deal with this, we keep the relocations in
12155 memory, and don't output them until the end of the link. This is
12156 an unfortunate waste of memory, but I don't see a good way around
12157 it. Fortunately, it only happens when performing a relocatable
12158 link, which is not the common case. FIXME: If keep_memory is set
12159 we could write the relocs out and then read them again; I don't
12160 know how bad the memory loss will be. */
12162 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12163 sub->output_has_begun = FALSE;
12164 for (o = abfd->sections; o != NULL; o = o->next)
12166 for (p = o->map_head.link_order; p != NULL; p = p->next)
12168 if (p->type == bfd_indirect_link_order
12169 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12170 == bfd_target_elf_flavour)
12171 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12173 if (! sub->output_has_begun)
12175 if (! elf_link_input_bfd (&flinfo, sub))
12177 sub->output_has_begun = TRUE;
12180 else if (p->type == bfd_section_reloc_link_order
12181 || p->type == bfd_symbol_reloc_link_order)
12183 if (! elf_reloc_link_order (abfd, info, o, p))
12188 if (! _bfd_default_link_order (abfd, info, o, p))
12190 if (p->type == bfd_indirect_link_order
12191 && (bfd_get_flavour (sub)
12192 == bfd_target_elf_flavour)
12193 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12194 != bed->s->elfclass))
12196 const char *iclass, *oclass;
12198 switch (bed->s->elfclass)
12200 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12201 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12202 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12206 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12208 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12209 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12210 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12214 bfd_set_error (bfd_error_wrong_format);
12216 /* xgettext:c-format */
12217 (_("%pB: file class %s incompatible with %s"),
12218 sub, iclass, oclass);
12227 /* Free symbol buffer if needed. */
12228 if (!info->reduce_memory_overheads)
12230 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12231 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12232 && elf_tdata (sub)->symbuf)
12234 free (elf_tdata (sub)->symbuf);
12235 elf_tdata (sub)->symbuf = NULL;
12239 /* Output any global symbols that got converted to local in a
12240 version script or due to symbol visibility. We do this in a
12241 separate step since ELF requires all local symbols to appear
12242 prior to any global symbols. FIXME: We should only do this if
12243 some global symbols were, in fact, converted to become local.
12244 FIXME: Will this work correctly with the Irix 5 linker? */
12245 eoinfo.failed = FALSE;
12246 eoinfo.flinfo = &flinfo;
12247 eoinfo.localsyms = TRUE;
12248 eoinfo.file_sym_done = FALSE;
12249 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12253 /* If backend needs to output some local symbols not present in the hash
12254 table, do it now. */
12255 if (bed->elf_backend_output_arch_local_syms
12256 && (info->strip != strip_all || emit_relocs))
12258 typedef int (*out_sym_func)
12259 (void *, const char *, Elf_Internal_Sym *, asection *,
12260 struct elf_link_hash_entry *);
12262 if (! ((*bed->elf_backend_output_arch_local_syms)
12263 (abfd, info, &flinfo,
12264 (out_sym_func) elf_link_output_symstrtab)))
12268 /* That wrote out all the local symbols. Finish up the symbol table
12269 with the global symbols. Even if we want to strip everything we
12270 can, we still need to deal with those global symbols that got
12271 converted to local in a version script. */
12273 /* The sh_info field records the index of the first non local symbol. */
12274 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12277 && htab->dynsym != NULL
12278 && htab->dynsym->output_section != bfd_abs_section_ptr)
12280 Elf_Internal_Sym sym;
12281 bfd_byte *dynsym = htab->dynsym->contents;
12283 o = htab->dynsym->output_section;
12284 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12286 /* Write out the section symbols for the output sections. */
12287 if (bfd_link_pic (info)
12288 || htab->is_relocatable_executable)
12294 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12296 sym.st_target_internal = 0;
12298 for (s = abfd->sections; s != NULL; s = s->next)
12304 dynindx = elf_section_data (s)->dynindx;
12307 indx = elf_section_data (s)->this_idx;
12308 BFD_ASSERT (indx > 0);
12309 sym.st_shndx = indx;
12310 if (! check_dynsym (abfd, &sym))
12312 sym.st_value = s->vma;
12313 dest = dynsym + dynindx * bed->s->sizeof_sym;
12314 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12318 /* Write out the local dynsyms. */
12319 if (htab->dynlocal)
12321 struct elf_link_local_dynamic_entry *e;
12322 for (e = htab->dynlocal; e ; e = e->next)
12327 /* Copy the internal symbol and turn off visibility.
12328 Note that we saved a word of storage and overwrote
12329 the original st_name with the dynstr_index. */
12331 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12333 s = bfd_section_from_elf_index (e->input_bfd,
12338 elf_section_data (s->output_section)->this_idx;
12339 if (! check_dynsym (abfd, &sym))
12341 sym.st_value = (s->output_section->vma
12343 + e->isym.st_value);
12346 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12347 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12352 /* We get the global symbols from the hash table. */
12353 eoinfo.failed = FALSE;
12354 eoinfo.localsyms = FALSE;
12355 eoinfo.flinfo = &flinfo;
12356 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12360 /* If backend needs to output some symbols not present in the hash
12361 table, do it now. */
12362 if (bed->elf_backend_output_arch_syms
12363 && (info->strip != strip_all || emit_relocs))
12365 typedef int (*out_sym_func)
12366 (void *, const char *, Elf_Internal_Sym *, asection *,
12367 struct elf_link_hash_entry *);
12369 if (! ((*bed->elf_backend_output_arch_syms)
12370 (abfd, info, &flinfo,
12371 (out_sym_func) elf_link_output_symstrtab)))
12375 /* Finalize the .strtab section. */
12376 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12378 /* Swap out the .strtab section. */
12379 if (!elf_link_swap_symbols_out (&flinfo))
12382 /* Now we know the size of the symtab section. */
12383 if (bfd_get_symcount (abfd) > 0)
12385 /* Finish up and write out the symbol string table (.strtab)
12387 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12388 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12390 if (elf_symtab_shndx_list (abfd))
12392 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12394 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12396 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12397 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12398 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12399 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12400 symtab_shndx_hdr->sh_size = amt;
12402 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12405 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12406 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12411 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12412 /* sh_name was set in prep_headers. */
12413 symstrtab_hdr->sh_type = SHT_STRTAB;
12414 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12415 symstrtab_hdr->sh_addr = 0;
12416 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12417 symstrtab_hdr->sh_entsize = 0;
12418 symstrtab_hdr->sh_link = 0;
12419 symstrtab_hdr->sh_info = 0;
12420 /* sh_offset is set just below. */
12421 symstrtab_hdr->sh_addralign = 1;
12423 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12425 elf_next_file_pos (abfd) = off;
12427 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12428 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12432 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12434 _bfd_error_handler (_("%pB: failed to generate import library"),
12435 info->out_implib_bfd);
12439 /* Adjust the relocs to have the correct symbol indices. */
12440 for (o = abfd->sections; o != NULL; o = o->next)
12442 struct bfd_elf_section_data *esdo = elf_section_data (o);
12445 if ((o->flags & SEC_RELOC) == 0)
12448 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12449 if (esdo->rel.hdr != NULL
12450 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12452 if (esdo->rela.hdr != NULL
12453 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12456 /* Set the reloc_count field to 0 to prevent write_relocs from
12457 trying to swap the relocs out itself. */
12458 o->reloc_count = 0;
12461 if (dynamic && info->combreloc && dynobj != NULL)
12462 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12464 /* If we are linking against a dynamic object, or generating a
12465 shared library, finish up the dynamic linking information. */
12468 bfd_byte *dyncon, *dynconend;
12470 /* Fix up .dynamic entries. */
12471 o = bfd_get_linker_section (dynobj, ".dynamic");
12472 BFD_ASSERT (o != NULL);
12474 dyncon = o->contents;
12475 dynconend = o->contents + o->size;
12476 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12478 Elf_Internal_Dyn dyn;
12481 bfd_size_type sh_size;
12484 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12491 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12493 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12495 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12496 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12499 dyn.d_un.d_val = relativecount;
12506 name = info->init_function;
12509 name = info->fini_function;
12512 struct elf_link_hash_entry *h;
12514 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12516 && (h->root.type == bfd_link_hash_defined
12517 || h->root.type == bfd_link_hash_defweak))
12519 dyn.d_un.d_ptr = h->root.u.def.value;
12520 o = h->root.u.def.section;
12521 if (o->output_section != NULL)
12522 dyn.d_un.d_ptr += (o->output_section->vma
12523 + o->output_offset);
12526 /* The symbol is imported from another shared
12527 library and does not apply to this one. */
12528 dyn.d_un.d_ptr = 0;
12535 case DT_PREINIT_ARRAYSZ:
12536 name = ".preinit_array";
12538 case DT_INIT_ARRAYSZ:
12539 name = ".init_array";
12541 case DT_FINI_ARRAYSZ:
12542 name = ".fini_array";
12544 o = bfd_get_section_by_name (abfd, name);
12548 (_("could not find section %s"), name);
12553 (_("warning: %s section has zero size"), name);
12554 dyn.d_un.d_val = o->size;
12557 case DT_PREINIT_ARRAY:
12558 name = ".preinit_array";
12560 case DT_INIT_ARRAY:
12561 name = ".init_array";
12563 case DT_FINI_ARRAY:
12564 name = ".fini_array";
12566 o = bfd_get_section_by_name (abfd, name);
12573 name = ".gnu.hash";
12582 name = ".gnu.version_d";
12585 name = ".gnu.version_r";
12588 name = ".gnu.version";
12590 o = bfd_get_linker_section (dynobj, name);
12592 if (o == NULL || bfd_is_abs_section (o->output_section))
12595 (_("could not find section %s"), name);
12598 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12601 (_("warning: section '%s' is being made into a note"), name);
12602 bfd_set_error (bfd_error_nonrepresentable_section);
12605 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12612 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12618 for (i = 1; i < elf_numsections (abfd); i++)
12620 Elf_Internal_Shdr *hdr;
12622 hdr = elf_elfsections (abfd)[i];
12623 if (hdr->sh_type == type
12624 && (hdr->sh_flags & SHF_ALLOC) != 0)
12626 sh_size += hdr->sh_size;
12628 || sh_addr > hdr->sh_addr)
12629 sh_addr = hdr->sh_addr;
12633 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12635 /* Don't count procedure linkage table relocs in the
12636 overall reloc count. */
12637 sh_size -= htab->srelplt->size;
12639 /* If the size is zero, make the address zero too.
12640 This is to avoid a glibc bug. If the backend
12641 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12642 zero, then we'll put DT_RELA at the end of
12643 DT_JMPREL. glibc will interpret the end of
12644 DT_RELA matching the end of DT_JMPREL as the
12645 case where DT_RELA includes DT_JMPREL, and for
12646 LD_BIND_NOW will decide that processing DT_RELA
12647 will process the PLT relocs too. Net result:
12648 No PLT relocs applied. */
12651 /* If .rela.plt is the first .rela section, exclude
12652 it from DT_RELA. */
12653 else if (sh_addr == (htab->srelplt->output_section->vma
12654 + htab->srelplt->output_offset))
12655 sh_addr += htab->srelplt->size;
12658 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12659 dyn.d_un.d_val = sh_size;
12661 dyn.d_un.d_ptr = sh_addr;
12664 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12668 /* If we have created any dynamic sections, then output them. */
12669 if (dynobj != NULL)
12671 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12674 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12675 if (((info->warn_shared_textrel && bfd_link_pic (info))
12676 || info->error_textrel)
12677 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12679 bfd_byte *dyncon, *dynconend;
12681 dyncon = o->contents;
12682 dynconend = o->contents + o->size;
12683 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12685 Elf_Internal_Dyn dyn;
12687 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12689 if (dyn.d_tag == DT_TEXTREL)
12691 if (info->error_textrel)
12692 info->callbacks->einfo
12693 (_("%P%X: read-only segment has dynamic relocations\n"));
12695 info->callbacks->einfo
12696 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12702 for (o = dynobj->sections; o != NULL; o = o->next)
12704 if ((o->flags & SEC_HAS_CONTENTS) == 0
12706 || o->output_section == bfd_abs_section_ptr)
12708 if ((o->flags & SEC_LINKER_CREATED) == 0)
12710 /* At this point, we are only interested in sections
12711 created by _bfd_elf_link_create_dynamic_sections. */
12714 if (htab->stab_info.stabstr == o)
12716 if (htab->eh_info.hdr_sec == o)
12718 if (strcmp (o->name, ".dynstr") != 0)
12720 if (! bfd_set_section_contents (abfd, o->output_section,
12722 (file_ptr) o->output_offset
12723 * bfd_octets_per_byte (abfd),
12729 /* The contents of the .dynstr section are actually in a
12733 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12734 if (bfd_seek (abfd, off, SEEK_SET) != 0
12735 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12741 if (!info->resolve_section_groups)
12743 bfd_boolean failed = FALSE;
12745 BFD_ASSERT (bfd_link_relocatable (info));
12746 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12751 /* If we have optimized stabs strings, output them. */
12752 if (htab->stab_info.stabstr != NULL)
12754 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12758 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12761 elf_final_link_free (abfd, &flinfo);
12763 elf_linker (abfd) = TRUE;
12767 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12768 if (contents == NULL)
12769 return FALSE; /* Bail out and fail. */
12770 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12771 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12778 elf_final_link_free (abfd, &flinfo);
12782 /* Initialize COOKIE for input bfd ABFD. */
12785 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12786 struct bfd_link_info *info, bfd *abfd)
12788 Elf_Internal_Shdr *symtab_hdr;
12789 const struct elf_backend_data *bed;
12791 bed = get_elf_backend_data (abfd);
12792 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12794 cookie->abfd = abfd;
12795 cookie->sym_hashes = elf_sym_hashes (abfd);
12796 cookie->bad_symtab = elf_bad_symtab (abfd);
12797 if (cookie->bad_symtab)
12799 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12800 cookie->extsymoff = 0;
12804 cookie->locsymcount = symtab_hdr->sh_info;
12805 cookie->extsymoff = symtab_hdr->sh_info;
12808 if (bed->s->arch_size == 32)
12809 cookie->r_sym_shift = 8;
12811 cookie->r_sym_shift = 32;
12813 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12814 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12816 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12817 cookie->locsymcount, 0,
12819 if (cookie->locsyms == NULL)
12821 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12824 if (info->keep_memory)
12825 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12830 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12833 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12835 Elf_Internal_Shdr *symtab_hdr;
12837 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12838 if (cookie->locsyms != NULL
12839 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12840 free (cookie->locsyms);
12843 /* Initialize the relocation information in COOKIE for input section SEC
12844 of input bfd ABFD. */
12847 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12848 struct bfd_link_info *info, bfd *abfd,
12851 if (sec->reloc_count == 0)
12853 cookie->rels = NULL;
12854 cookie->relend = NULL;
12858 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12859 info->keep_memory);
12860 if (cookie->rels == NULL)
12862 cookie->rel = cookie->rels;
12863 cookie->relend = cookie->rels + sec->reloc_count;
12865 cookie->rel = cookie->rels;
12869 /* Free the memory allocated by init_reloc_cookie_rels,
12873 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12876 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12877 free (cookie->rels);
12880 /* Initialize the whole of COOKIE for input section SEC. */
12883 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12884 struct bfd_link_info *info,
12887 if (!init_reloc_cookie (cookie, info, sec->owner))
12889 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12894 fini_reloc_cookie (cookie, sec->owner);
12899 /* Free the memory allocated by init_reloc_cookie_for_section,
12903 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12906 fini_reloc_cookie_rels (cookie, sec);
12907 fini_reloc_cookie (cookie, sec->owner);
12910 /* Garbage collect unused sections. */
12912 /* Default gc_mark_hook. */
12915 _bfd_elf_gc_mark_hook (asection *sec,
12916 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12917 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12918 struct elf_link_hash_entry *h,
12919 Elf_Internal_Sym *sym)
12923 switch (h->root.type)
12925 case bfd_link_hash_defined:
12926 case bfd_link_hash_defweak:
12927 return h->root.u.def.section;
12929 case bfd_link_hash_common:
12930 return h->root.u.c.p->section;
12937 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12942 /* Return the debug definition section. */
12945 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12946 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12947 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12948 struct elf_link_hash_entry *h,
12949 Elf_Internal_Sym *sym)
12953 /* Return the global debug definition section. */
12954 if ((h->root.type == bfd_link_hash_defined
12955 || h->root.type == bfd_link_hash_defweak)
12956 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12957 return h->root.u.def.section;
12961 /* Return the local debug definition section. */
12962 asection *isec = bfd_section_from_elf_index (sec->owner,
12964 if ((isec->flags & SEC_DEBUGGING) != 0)
12971 /* COOKIE->rel describes a relocation against section SEC, which is
12972 a section we've decided to keep. Return the section that contains
12973 the relocation symbol, or NULL if no section contains it. */
12976 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12977 elf_gc_mark_hook_fn gc_mark_hook,
12978 struct elf_reloc_cookie *cookie,
12979 bfd_boolean *start_stop)
12981 unsigned long r_symndx;
12982 struct elf_link_hash_entry *h;
12984 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12985 if (r_symndx == STN_UNDEF)
12988 if (r_symndx >= cookie->locsymcount
12989 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12991 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12994 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
12998 while (h->root.type == bfd_link_hash_indirect
12999 || h->root.type == bfd_link_hash_warning)
13000 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13002 /* If this symbol is weak and there is a non-weak definition, we
13003 keep the non-weak definition because many backends put
13004 dynamic reloc info on the non-weak definition for code
13005 handling copy relocs. */
13006 if (h->is_weakalias)
13007 weakdef (h)->mark = 1;
13009 if (start_stop != NULL)
13011 /* To work around a glibc bug, mark XXX input sections
13012 when there is a reference to __start_XXX or __stop_XXX
13016 asection *s = h->u2.start_stop_section;
13017 *start_stop = !s->gc_mark;
13022 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13025 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13026 &cookie->locsyms[r_symndx]);
13029 /* COOKIE->rel describes a relocation against section SEC, which is
13030 a section we've decided to keep. Mark the section that contains
13031 the relocation symbol. */
13034 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13036 elf_gc_mark_hook_fn gc_mark_hook,
13037 struct elf_reloc_cookie *cookie)
13040 bfd_boolean start_stop = FALSE;
13042 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13043 while (rsec != NULL)
13045 if (!rsec->gc_mark)
13047 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13048 || (rsec->owner->flags & DYNAMIC) != 0)
13050 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13055 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13060 /* The mark phase of garbage collection. For a given section, mark
13061 it and any sections in this section's group, and all the sections
13062 which define symbols to which it refers. */
13065 _bfd_elf_gc_mark (struct bfd_link_info *info,
13067 elf_gc_mark_hook_fn gc_mark_hook)
13070 asection *group_sec, *eh_frame;
13074 /* Mark all the sections in the group. */
13075 group_sec = elf_section_data (sec)->next_in_group;
13076 if (group_sec && !group_sec->gc_mark)
13077 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13080 /* Look through the section relocs. */
13082 eh_frame = elf_eh_frame_section (sec->owner);
13083 if ((sec->flags & SEC_RELOC) != 0
13084 && sec->reloc_count > 0
13085 && sec != eh_frame)
13087 struct elf_reloc_cookie cookie;
13089 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13093 for (; cookie.rel < cookie.relend; cookie.rel++)
13094 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13099 fini_reloc_cookie_for_section (&cookie, sec);
13103 if (ret && eh_frame && elf_fde_list (sec))
13105 struct elf_reloc_cookie cookie;
13107 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13111 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13112 gc_mark_hook, &cookie))
13114 fini_reloc_cookie_for_section (&cookie, eh_frame);
13118 eh_frame = elf_section_eh_frame_entry (sec);
13119 if (ret && eh_frame && !eh_frame->gc_mark)
13120 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13126 /* Scan and mark sections in a special or debug section group. */
13129 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13131 /* Point to first section of section group. */
13133 /* Used to iterate the section group. */
13136 bfd_boolean is_special_grp = TRUE;
13137 bfd_boolean is_debug_grp = TRUE;
13139 /* First scan to see if group contains any section other than debug
13140 and special section. */
13141 ssec = msec = elf_next_in_group (grp);
13144 if ((msec->flags & SEC_DEBUGGING) == 0)
13145 is_debug_grp = FALSE;
13147 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13148 is_special_grp = FALSE;
13150 msec = elf_next_in_group (msec);
13152 while (msec != ssec);
13154 /* If this is a pure debug section group or pure special section group,
13155 keep all sections in this group. */
13156 if (is_debug_grp || is_special_grp)
13161 msec = elf_next_in_group (msec);
13163 while (msec != ssec);
13167 /* Keep debug and special sections. */
13170 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13171 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13175 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13178 bfd_boolean some_kept;
13179 bfd_boolean debug_frag_seen;
13180 bfd_boolean has_kept_debug_info;
13182 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13184 isec = ibfd->sections;
13185 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13188 /* Ensure all linker created sections are kept,
13189 see if any other section is already marked,
13190 and note if we have any fragmented debug sections. */
13191 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13192 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13194 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13196 else if (isec->gc_mark
13197 && (isec->flags & SEC_ALLOC) != 0
13198 && elf_section_type (isec) != SHT_NOTE)
13201 if (!debug_frag_seen
13202 && (isec->flags & SEC_DEBUGGING)
13203 && CONST_STRNEQ (isec->name, ".debug_line."))
13204 debug_frag_seen = TRUE;
13207 /* If no non-note alloc section in this file will be kept, then
13208 we can toss out the debug and special sections. */
13212 /* Keep debug and special sections like .comment when they are
13213 not part of a group. Also keep section groups that contain
13214 just debug sections or special sections. */
13215 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13217 if ((isec->flags & SEC_GROUP) != 0)
13218 _bfd_elf_gc_mark_debug_special_section_group (isec);
13219 else if (((isec->flags & SEC_DEBUGGING) != 0
13220 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13221 && elf_next_in_group (isec) == NULL)
13223 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13224 has_kept_debug_info = TRUE;
13227 /* Look for CODE sections which are going to be discarded,
13228 and find and discard any fragmented debug sections which
13229 are associated with that code section. */
13230 if (debug_frag_seen)
13231 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13232 if ((isec->flags & SEC_CODE) != 0
13233 && isec->gc_mark == 0)
13238 ilen = strlen (isec->name);
13240 /* Association is determined by the name of the debug
13241 section containing the name of the code section as
13242 a suffix. For example .debug_line.text.foo is a
13243 debug section associated with .text.foo. */
13244 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13248 if (dsec->gc_mark == 0
13249 || (dsec->flags & SEC_DEBUGGING) == 0)
13252 dlen = strlen (dsec->name);
13255 && strncmp (dsec->name + (dlen - ilen),
13256 isec->name, ilen) == 0)
13261 /* Mark debug sections referenced by kept debug sections. */
13262 if (has_kept_debug_info)
13263 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13265 && (isec->flags & SEC_DEBUGGING) != 0)
13266 if (!_bfd_elf_gc_mark (info, isec,
13267 elf_gc_mark_debug_section))
13274 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13277 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13279 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13283 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13284 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13285 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13288 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13291 for (o = sub->sections; o != NULL; o = o->next)
13293 /* When any section in a section group is kept, we keep all
13294 sections in the section group. If the first member of
13295 the section group is excluded, we will also exclude the
13297 if (o->flags & SEC_GROUP)
13299 asection *first = elf_next_in_group (o);
13300 o->gc_mark = first->gc_mark;
13306 /* Skip sweeping sections already excluded. */
13307 if (o->flags & SEC_EXCLUDE)
13310 /* Since this is early in the link process, it is simple
13311 to remove a section from the output. */
13312 o->flags |= SEC_EXCLUDE;
13314 if (info->print_gc_sections && o->size != 0)
13315 /* xgettext:c-format */
13316 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13324 /* Propagate collected vtable information. This is called through
13325 elf_link_hash_traverse. */
13328 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13330 /* Those that are not vtables. */
13332 || h->u2.vtable == NULL
13333 || h->u2.vtable->parent == NULL)
13336 /* Those vtables that do not have parents, we cannot merge. */
13337 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13340 /* If we've already been done, exit. */
13341 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13344 /* Make sure the parent's table is up to date. */
13345 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13347 if (h->u2.vtable->used == NULL)
13349 /* None of this table's entries were referenced. Re-use the
13351 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13352 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13357 bfd_boolean *cu, *pu;
13359 /* Or the parent's entries into ours. */
13360 cu = h->u2.vtable->used;
13362 pu = h->u2.vtable->parent->u2.vtable->used;
13365 const struct elf_backend_data *bed;
13366 unsigned int log_file_align;
13368 bed = get_elf_backend_data (h->root.u.def.section->owner);
13369 log_file_align = bed->s->log_file_align;
13370 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13385 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13388 bfd_vma hstart, hend;
13389 Elf_Internal_Rela *relstart, *relend, *rel;
13390 const struct elf_backend_data *bed;
13391 unsigned int log_file_align;
13393 /* Take care of both those symbols that do not describe vtables as
13394 well as those that are not loaded. */
13396 || h->u2.vtable == NULL
13397 || h->u2.vtable->parent == NULL)
13400 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13401 || h->root.type == bfd_link_hash_defweak);
13403 sec = h->root.u.def.section;
13404 hstart = h->root.u.def.value;
13405 hend = hstart + h->size;
13407 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13409 return *(bfd_boolean *) okp = FALSE;
13410 bed = get_elf_backend_data (sec->owner);
13411 log_file_align = bed->s->log_file_align;
13413 relend = relstart + sec->reloc_count;
13415 for (rel = relstart; rel < relend; ++rel)
13416 if (rel->r_offset >= hstart && rel->r_offset < hend)
13418 /* If the entry is in use, do nothing. */
13419 if (h->u2.vtable->used
13420 && (rel->r_offset - hstart) < h->u2.vtable->size)
13422 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13423 if (h->u2.vtable->used[entry])
13426 /* Otherwise, kill it. */
13427 rel->r_offset = rel->r_info = rel->r_addend = 0;
13433 /* Mark sections containing dynamically referenced symbols. When
13434 building shared libraries, we must assume that any visible symbol is
13438 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13440 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13441 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13443 if ((h->root.type == bfd_link_hash_defined
13444 || h->root.type == bfd_link_hash_defweak)
13445 && ((h->ref_dynamic && !h->forced_local)
13446 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13447 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13448 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13449 && (!bfd_link_executable (info)
13450 || info->gc_keep_exported
13451 || info->export_dynamic
13454 && (*d->match) (&d->head, NULL, h->root.root.string)))
13455 && (h->versioned >= versioned
13456 || !bfd_hide_sym_by_version (info->version_info,
13457 h->root.root.string)))))
13458 h->root.u.def.section->flags |= SEC_KEEP;
13463 /* Keep all sections containing symbols undefined on the command-line,
13464 and the section containing the entry symbol. */
13467 _bfd_elf_gc_keep (struct bfd_link_info *info)
13469 struct bfd_sym_chain *sym;
13471 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13473 struct elf_link_hash_entry *h;
13475 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13476 FALSE, FALSE, FALSE);
13479 && (h->root.type == bfd_link_hash_defined
13480 || h->root.type == bfd_link_hash_defweak)
13481 && !bfd_is_abs_section (h->root.u.def.section)
13482 && !bfd_is_und_section (h->root.u.def.section))
13483 h->root.u.def.section->flags |= SEC_KEEP;
13488 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13489 struct bfd_link_info *info)
13491 bfd *ibfd = info->input_bfds;
13493 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13496 struct elf_reloc_cookie cookie;
13498 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13500 sec = ibfd->sections;
13501 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13504 if (!init_reloc_cookie (&cookie, info, ibfd))
13507 for (sec = ibfd->sections; sec; sec = sec->next)
13509 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13510 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13512 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13513 fini_reloc_cookie_rels (&cookie, sec);
13520 /* Do mark and sweep of unused sections. */
13523 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13525 bfd_boolean ok = TRUE;
13527 elf_gc_mark_hook_fn gc_mark_hook;
13528 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13529 struct elf_link_hash_table *htab;
13531 if (!bed->can_gc_sections
13532 || !is_elf_hash_table (info->hash))
13534 _bfd_error_handler(_("warning: gc-sections option ignored"));
13538 bed->gc_keep (info);
13539 htab = elf_hash_table (info);
13541 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13542 at the .eh_frame section if we can mark the FDEs individually. */
13543 for (sub = info->input_bfds;
13544 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13545 sub = sub->link.next)
13548 struct elf_reloc_cookie cookie;
13550 sec = sub->sections;
13551 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13553 sec = bfd_get_section_by_name (sub, ".eh_frame");
13554 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13556 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13557 if (elf_section_data (sec)->sec_info
13558 && (sec->flags & SEC_LINKER_CREATED) == 0)
13559 elf_eh_frame_section (sub) = sec;
13560 fini_reloc_cookie_for_section (&cookie, sec);
13561 sec = bfd_get_next_section_by_name (NULL, sec);
13565 /* Apply transitive closure to the vtable entry usage info. */
13566 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13570 /* Kill the vtable relocations that were not used. */
13571 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13575 /* Mark dynamically referenced symbols. */
13576 if (htab->dynamic_sections_created || info->gc_keep_exported)
13577 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13579 /* Grovel through relocs to find out who stays ... */
13580 gc_mark_hook = bed->gc_mark_hook;
13581 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13585 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13586 || elf_object_id (sub) != elf_hash_table_id (htab)
13587 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13591 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13594 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13595 Also treat note sections as a root, if the section is not part
13596 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13597 well as FINI_ARRAY sections for ld -r. */
13598 for (o = sub->sections; o != NULL; o = o->next)
13600 && (o->flags & SEC_EXCLUDE) == 0
13601 && ((o->flags & SEC_KEEP) != 0
13602 || (bfd_link_relocatable (info)
13603 && ((elf_section_data (o)->this_hdr.sh_type
13604 == SHT_PREINIT_ARRAY)
13605 || (elf_section_data (o)->this_hdr.sh_type
13607 || (elf_section_data (o)->this_hdr.sh_type
13608 == SHT_FINI_ARRAY)))
13609 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13610 && elf_next_in_group (o) == NULL )))
13612 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13617 /* Allow the backend to mark additional target specific sections. */
13618 bed->gc_mark_extra_sections (info, gc_mark_hook);
13620 /* ... and mark SEC_EXCLUDE for those that go. */
13621 return elf_gc_sweep (abfd, info);
13624 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13627 bfd_elf_gc_record_vtinherit (bfd *abfd,
13629 struct elf_link_hash_entry *h,
13632 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13633 struct elf_link_hash_entry **search, *child;
13634 size_t extsymcount;
13635 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13637 /* The sh_info field of the symtab header tells us where the
13638 external symbols start. We don't care about the local symbols at
13640 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13641 if (!elf_bad_symtab (abfd))
13642 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13644 sym_hashes = elf_sym_hashes (abfd);
13645 sym_hashes_end = sym_hashes + extsymcount;
13647 /* Hunt down the child symbol, which is in this section at the same
13648 offset as the relocation. */
13649 for (search = sym_hashes; search != sym_hashes_end; ++search)
13651 if ((child = *search) != NULL
13652 && (child->root.type == bfd_link_hash_defined
13653 || child->root.type == bfd_link_hash_defweak)
13654 && child->root.u.def.section == sec
13655 && child->root.u.def.value == offset)
13659 /* xgettext:c-format */
13660 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13661 abfd, sec, (uint64_t) offset);
13662 bfd_set_error (bfd_error_invalid_operation);
13666 if (!child->u2.vtable)
13668 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13669 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13670 if (!child->u2.vtable)
13675 /* This *should* only be the absolute section. It could potentially
13676 be that someone has defined a non-global vtable though, which
13677 would be bad. It isn't worth paging in the local symbols to be
13678 sure though; that case should simply be handled by the assembler. */
13680 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13683 child->u2.vtable->parent = h;
13688 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13691 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13692 asection *sec ATTRIBUTE_UNUSED,
13693 struct elf_link_hash_entry *h,
13696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13697 unsigned int log_file_align = bed->s->log_file_align;
13701 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13702 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13707 if (addend >= h->u2.vtable->size)
13709 size_t size, bytes, file_align;
13710 bfd_boolean *ptr = h->u2.vtable->used;
13712 /* While the symbol is undefined, we have to be prepared to handle
13714 file_align = 1 << log_file_align;
13715 if (h->root.type == bfd_link_hash_undefined)
13716 size = addend + file_align;
13720 if (addend >= size)
13722 /* Oops! We've got a reference past the defined end of
13723 the table. This is probably a bug -- shall we warn? */
13724 size = addend + file_align;
13727 size = (size + file_align - 1) & -file_align;
13729 /* Allocate one extra entry for use as a "done" flag for the
13730 consolidation pass. */
13731 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13735 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13741 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13742 * sizeof (bfd_boolean));
13743 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13747 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13752 /* And arrange for that done flag to be at index -1. */
13753 h->u2.vtable->used = ptr + 1;
13754 h->u2.vtable->size = size;
13757 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13762 /* Map an ELF section header flag to its corresponding string. */
13766 flagword flag_value;
13767 } elf_flags_to_name_table;
13769 static elf_flags_to_name_table elf_flags_to_names [] =
13771 { "SHF_WRITE", SHF_WRITE },
13772 { "SHF_ALLOC", SHF_ALLOC },
13773 { "SHF_EXECINSTR", SHF_EXECINSTR },
13774 { "SHF_MERGE", SHF_MERGE },
13775 { "SHF_STRINGS", SHF_STRINGS },
13776 { "SHF_INFO_LINK", SHF_INFO_LINK},
13777 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13778 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13779 { "SHF_GROUP", SHF_GROUP },
13780 { "SHF_TLS", SHF_TLS },
13781 { "SHF_MASKOS", SHF_MASKOS },
13782 { "SHF_EXCLUDE", SHF_EXCLUDE },
13785 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13787 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13788 struct flag_info *flaginfo,
13791 const bfd_vma sh_flags = elf_section_flags (section);
13793 if (!flaginfo->flags_initialized)
13795 bfd *obfd = info->output_bfd;
13796 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13797 struct flag_info_list *tf = flaginfo->flag_list;
13799 int without_hex = 0;
13801 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13804 flagword (*lookup) (char *);
13806 lookup = bed->elf_backend_lookup_section_flags_hook;
13807 if (lookup != NULL)
13809 flagword hexval = (*lookup) ((char *) tf->name);
13813 if (tf->with == with_flags)
13814 with_hex |= hexval;
13815 else if (tf->with == without_flags)
13816 without_hex |= hexval;
13821 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13823 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13825 if (tf->with == with_flags)
13826 with_hex |= elf_flags_to_names[i].flag_value;
13827 else if (tf->with == without_flags)
13828 without_hex |= elf_flags_to_names[i].flag_value;
13835 info->callbacks->einfo
13836 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13840 flaginfo->flags_initialized = TRUE;
13841 flaginfo->only_with_flags |= with_hex;
13842 flaginfo->not_with_flags |= without_hex;
13845 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13848 if ((flaginfo->not_with_flags & sh_flags) != 0)
13854 struct alloc_got_off_arg {
13856 struct bfd_link_info *info;
13859 /* We need a special top-level link routine to convert got reference counts
13860 to real got offsets. */
13863 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13865 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13866 bfd *obfd = gofarg->info->output_bfd;
13867 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13869 if (h->got.refcount > 0)
13871 h->got.offset = gofarg->gotoff;
13872 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13875 h->got.offset = (bfd_vma) -1;
13880 /* And an accompanying bit to work out final got entry offsets once
13881 we're done. Should be called from final_link. */
13884 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13885 struct bfd_link_info *info)
13888 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13890 struct alloc_got_off_arg gofarg;
13892 BFD_ASSERT (abfd == info->output_bfd);
13894 if (! is_elf_hash_table (info->hash))
13897 /* The GOT offset is relative to the .got section, but the GOT header is
13898 put into the .got.plt section, if the backend uses it. */
13899 if (bed->want_got_plt)
13902 gotoff = bed->got_header_size;
13904 /* Do the local .got entries first. */
13905 for (i = info->input_bfds; i; i = i->link.next)
13907 bfd_signed_vma *local_got;
13908 size_t j, locsymcount;
13909 Elf_Internal_Shdr *symtab_hdr;
13911 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13914 local_got = elf_local_got_refcounts (i);
13918 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13919 if (elf_bad_symtab (i))
13920 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13922 locsymcount = symtab_hdr->sh_info;
13924 for (j = 0; j < locsymcount; ++j)
13926 if (local_got[j] > 0)
13928 local_got[j] = gotoff;
13929 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13932 local_got[j] = (bfd_vma) -1;
13936 /* Then the global .got entries. .plt refcounts are handled by
13937 adjust_dynamic_symbol */
13938 gofarg.gotoff = gotoff;
13939 gofarg.info = info;
13940 elf_link_hash_traverse (elf_hash_table (info),
13941 elf_gc_allocate_got_offsets,
13946 /* Many folk need no more in the way of final link than this, once
13947 got entry reference counting is enabled. */
13950 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13952 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13955 /* Invoke the regular ELF backend linker to do all the work. */
13956 return bfd_elf_final_link (abfd, info);
13960 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13962 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13964 if (rcookie->bad_symtab)
13965 rcookie->rel = rcookie->rels;
13967 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13969 unsigned long r_symndx;
13971 if (! rcookie->bad_symtab)
13972 if (rcookie->rel->r_offset > offset)
13974 if (rcookie->rel->r_offset != offset)
13977 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13978 if (r_symndx == STN_UNDEF)
13981 if (r_symndx >= rcookie->locsymcount
13982 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13984 struct elf_link_hash_entry *h;
13986 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13988 while (h->root.type == bfd_link_hash_indirect
13989 || h->root.type == bfd_link_hash_warning)
13990 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13992 if ((h->root.type == bfd_link_hash_defined
13993 || h->root.type == bfd_link_hash_defweak)
13994 && (h->root.u.def.section->owner != rcookie->abfd
13995 || h->root.u.def.section->kept_section != NULL
13996 || discarded_section (h->root.u.def.section)))
14001 /* It's not a relocation against a global symbol,
14002 but it could be a relocation against a local
14003 symbol for a discarded section. */
14005 Elf_Internal_Sym *isym;
14007 /* Need to: get the symbol; get the section. */
14008 isym = &rcookie->locsyms[r_symndx];
14009 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14011 && (isec->kept_section != NULL
14012 || discarded_section (isec)))
14020 /* Discard unneeded references to discarded sections.
14021 Returns -1 on error, 1 if any section's size was changed, 0 if
14022 nothing changed. This function assumes that the relocations are in
14023 sorted order, which is true for all known assemblers. */
14026 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14028 struct elf_reloc_cookie cookie;
14033 if (info->traditional_format
14034 || !is_elf_hash_table (info->hash))
14037 o = bfd_get_section_by_name (output_bfd, ".stab");
14042 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14045 || i->reloc_count == 0
14046 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14050 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14053 if (!init_reloc_cookie_for_section (&cookie, info, i))
14056 if (_bfd_discard_section_stabs (abfd, i,
14057 elf_section_data (i)->sec_info,
14058 bfd_elf_reloc_symbol_deleted_p,
14062 fini_reloc_cookie_for_section (&cookie, i);
14067 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14068 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14072 int eh_changed = 0;
14073 unsigned int eh_alignment;
14075 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14081 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14084 if (!init_reloc_cookie_for_section (&cookie, info, i))
14087 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14088 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14089 bfd_elf_reloc_symbol_deleted_p,
14093 if (i->size != i->rawsize)
14097 fini_reloc_cookie_for_section (&cookie, i);
14100 eh_alignment = 1 << o->alignment_power;
14101 /* Skip over zero terminator, and prevent empty sections from
14102 adding alignment padding at the end. */
14103 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14105 i->flags |= SEC_EXCLUDE;
14106 else if (i->size > 4)
14108 /* The last non-empty eh_frame section doesn't need padding. */
14111 /* Any prior sections must pad the last FDE out to the output
14112 section alignment. Otherwise we might have zero padding
14113 between sections, which would be seen as a terminator. */
14114 for (; i != NULL; i = i->map_tail.s)
14116 /* All but the last zero terminator should have been removed. */
14121 = (i->size + eh_alignment - 1) & -eh_alignment;
14122 if (i->size != size)
14130 elf_link_hash_traverse (elf_hash_table (info),
14131 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14134 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14136 const struct elf_backend_data *bed;
14139 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14141 s = abfd->sections;
14142 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14145 bed = get_elf_backend_data (abfd);
14147 if (bed->elf_backend_discard_info != NULL)
14149 if (!init_reloc_cookie (&cookie, info, abfd))
14152 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14155 fini_reloc_cookie (&cookie, abfd);
14159 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14160 _bfd_elf_end_eh_frame_parsing (info);
14162 if (info->eh_frame_hdr_type
14163 && !bfd_link_relocatable (info)
14164 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14171 _bfd_elf_section_already_linked (bfd *abfd,
14173 struct bfd_link_info *info)
14176 const char *name, *key;
14177 struct bfd_section_already_linked *l;
14178 struct bfd_section_already_linked_hash_entry *already_linked_list;
14180 if (sec->output_section == bfd_abs_section_ptr)
14183 flags = sec->flags;
14185 /* Return if it isn't a linkonce section. A comdat group section
14186 also has SEC_LINK_ONCE set. */
14187 if ((flags & SEC_LINK_ONCE) == 0)
14190 /* Don't put group member sections on our list of already linked
14191 sections. They are handled as a group via their group section. */
14192 if (elf_sec_group (sec) != NULL)
14195 /* For a SHT_GROUP section, use the group signature as the key. */
14197 if ((flags & SEC_GROUP) != 0
14198 && elf_next_in_group (sec) != NULL
14199 && elf_group_name (elf_next_in_group (sec)) != NULL)
14200 key = elf_group_name (elf_next_in_group (sec));
14203 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14204 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14205 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14208 /* Must be a user linkonce section that doesn't follow gcc's
14209 naming convention. In this case we won't be matching
14210 single member groups. */
14214 already_linked_list = bfd_section_already_linked_table_lookup (key);
14216 for (l = already_linked_list->entry; l != NULL; l = l->next)
14218 /* We may have 2 different types of sections on the list: group
14219 sections with a signature of <key> (<key> is some string),
14220 and linkonce sections named .gnu.linkonce.<type>.<key>.
14221 Match like sections. LTO plugin sections are an exception.
14222 They are always named .gnu.linkonce.t.<key> and match either
14223 type of section. */
14224 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14225 && ((flags & SEC_GROUP) != 0
14226 || strcmp (name, l->sec->name) == 0))
14227 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14229 /* The section has already been linked. See if we should
14230 issue a warning. */
14231 if (!_bfd_handle_already_linked (sec, l, info))
14234 if (flags & SEC_GROUP)
14236 asection *first = elf_next_in_group (sec);
14237 asection *s = first;
14241 s->output_section = bfd_abs_section_ptr;
14242 /* Record which group discards it. */
14243 s->kept_section = l->sec;
14244 s = elf_next_in_group (s);
14245 /* These lists are circular. */
14255 /* A single member comdat group section may be discarded by a
14256 linkonce section and vice versa. */
14257 if ((flags & SEC_GROUP) != 0)
14259 asection *first = elf_next_in_group (sec);
14261 if (first != NULL && elf_next_in_group (first) == first)
14262 /* Check this single member group against linkonce sections. */
14263 for (l = already_linked_list->entry; l != NULL; l = l->next)
14264 if ((l->sec->flags & SEC_GROUP) == 0
14265 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14267 first->output_section = bfd_abs_section_ptr;
14268 first->kept_section = l->sec;
14269 sec->output_section = bfd_abs_section_ptr;
14274 /* Check this linkonce section against single member groups. */
14275 for (l = already_linked_list->entry; l != NULL; l = l->next)
14276 if (l->sec->flags & SEC_GROUP)
14278 asection *first = elf_next_in_group (l->sec);
14281 && elf_next_in_group (first) == first
14282 && bfd_elf_match_symbols_in_sections (first, sec, info))
14284 sec->output_section = bfd_abs_section_ptr;
14285 sec->kept_section = first;
14290 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14291 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14292 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14293 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14294 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14295 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14296 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14297 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14298 The reverse order cannot happen as there is never a bfd with only the
14299 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14300 matter as here were are looking only for cross-bfd sections. */
14302 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14303 for (l = already_linked_list->entry; l != NULL; l = l->next)
14304 if ((l->sec->flags & SEC_GROUP) == 0
14305 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14307 if (abfd != l->sec->owner)
14308 sec->output_section = bfd_abs_section_ptr;
14312 /* This is the first section with this name. Record it. */
14313 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14314 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14315 return sec->output_section == bfd_abs_section_ptr;
14319 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14321 return sym->st_shndx == SHN_COMMON;
14325 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14331 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14333 return bfd_com_section_ptr;
14337 _bfd_elf_default_got_elt_size (bfd *abfd,
14338 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14339 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14340 bfd *ibfd ATTRIBUTE_UNUSED,
14341 unsigned long symndx ATTRIBUTE_UNUSED)
14343 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14344 return bed->s->arch_size / 8;
14347 /* Routines to support the creation of dynamic relocs. */
14349 /* Returns the name of the dynamic reloc section associated with SEC. */
14351 static const char *
14352 get_dynamic_reloc_section_name (bfd * abfd,
14354 bfd_boolean is_rela)
14357 const char *old_name = bfd_get_section_name (NULL, sec);
14358 const char *prefix = is_rela ? ".rela" : ".rel";
14360 if (old_name == NULL)
14363 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14364 sprintf (name, "%s%s", prefix, old_name);
14369 /* Returns the dynamic reloc section associated with SEC.
14370 If necessary compute the name of the dynamic reloc section based
14371 on SEC's name (looked up in ABFD's string table) and the setting
14375 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14377 bfd_boolean is_rela)
14379 asection * reloc_sec = elf_section_data (sec)->sreloc;
14381 if (reloc_sec == NULL)
14383 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14387 reloc_sec = bfd_get_linker_section (abfd, name);
14389 if (reloc_sec != NULL)
14390 elf_section_data (sec)->sreloc = reloc_sec;
14397 /* Returns the dynamic reloc section associated with SEC. If the
14398 section does not exist it is created and attached to the DYNOBJ
14399 bfd and stored in the SRELOC field of SEC's elf_section_data
14402 ALIGNMENT is the alignment for the newly created section and
14403 IS_RELA defines whether the name should be .rela.<SEC's name>
14404 or .rel.<SEC's name>. The section name is looked up in the
14405 string table associated with ABFD. */
14408 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14410 unsigned int alignment,
14412 bfd_boolean is_rela)
14414 asection * reloc_sec = elf_section_data (sec)->sreloc;
14416 if (reloc_sec == NULL)
14418 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14423 reloc_sec = bfd_get_linker_section (dynobj, name);
14425 if (reloc_sec == NULL)
14427 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14428 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14429 if ((sec->flags & SEC_ALLOC) != 0)
14430 flags |= SEC_ALLOC | SEC_LOAD;
14432 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14433 if (reloc_sec != NULL)
14435 /* _bfd_elf_get_sec_type_attr chooses a section type by
14436 name. Override as it may be wrong, eg. for a user
14437 section named "auto" we'll get ".relauto" which is
14438 seen to be a .rela section. */
14439 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14440 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14445 elf_section_data (sec)->sreloc = reloc_sec;
14451 /* Copy the ELF symbol type and other attributes for a linker script
14452 assignment from HSRC to HDEST. Generally this should be treated as
14453 if we found a strong non-dynamic definition for HDEST (except that
14454 ld ignores multiple definition errors). */
14456 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14457 struct bfd_link_hash_entry *hdest,
14458 struct bfd_link_hash_entry *hsrc)
14460 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14461 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14462 Elf_Internal_Sym isym;
14464 ehdest->type = ehsrc->type;
14465 ehdest->target_internal = ehsrc->target_internal;
14467 isym.st_other = ehsrc->other;
14468 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14471 /* Append a RELA relocation REL to section S in BFD. */
14474 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14476 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14477 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14478 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14479 bed->s->swap_reloca_out (abfd, rel, loc);
14482 /* Append a REL relocation REL to section S in BFD. */
14485 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14487 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14488 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14489 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14490 bed->s->swap_reloc_out (abfd, rel, loc);
14493 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14495 struct bfd_link_hash_entry *
14496 bfd_elf_define_start_stop (struct bfd_link_info *info,
14497 const char *symbol, asection *sec)
14499 struct elf_link_hash_entry *h;
14501 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14502 FALSE, FALSE, TRUE);
14504 && (h->root.type == bfd_link_hash_undefined
14505 || h->root.type == bfd_link_hash_undefweak
14506 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14508 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14509 h->root.type = bfd_link_hash_defined;
14510 h->root.u.def.section = sec;
14511 h->root.u.def.value = 0;
14512 h->def_regular = 1;
14513 h->def_dynamic = 0;
14515 h->u2.start_stop_section = sec;
14516 if (symbol[0] == '.')
14518 /* .startof. and .sizeof. symbols are local. */
14519 const struct elf_backend_data *bed;
14520 bed = get_elf_backend_data (info->output_bfd);
14521 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14525 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14526 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14528 bfd_elf_link_record_dynamic_symbol (info, h);