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 (bh->type == bfd_link_hash_defined
1945 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1947 /* Mark the previous definition from IR object as
1948 undefined so that the generic linker will override
1950 bh->type = bfd_link_hash_undefined;
1951 bh->u.undef.abfd = bh->u.def.section->owner;
1953 if (! (_bfd_generic_link_add_one_symbol
1954 (info, abfd, shortname, BSF_INDIRECT,
1955 bfd_ind_section_ptr,
1956 0, name, FALSE, collect, &bh)))
1958 hi = (struct elf_link_hash_entry *) bh;
1963 /* In this case the symbol named SHORTNAME is overriding the
1964 indirect symbol we want to add. We were planning on making
1965 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1966 is the name without a version. NAME is the fully versioned
1967 name, and it is the default version.
1969 Overriding means that we already saw a definition for the
1970 symbol SHORTNAME in a regular object, and it is overriding
1971 the symbol defined in the dynamic object.
1973 When this happens, we actually want to change NAME, the
1974 symbol we just added, to refer to SHORTNAME. This will cause
1975 references to NAME in the shared object to become references
1976 to SHORTNAME in the regular object. This is what we expect
1977 when we override a function in a shared object: that the
1978 references in the shared object will be mapped to the
1979 definition in the regular object. */
1981 while (hi->root.type == bfd_link_hash_indirect
1982 || hi->root.type == bfd_link_hash_warning)
1983 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1985 h->root.type = bfd_link_hash_indirect;
1986 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1990 hi->ref_dynamic = 1;
1994 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1999 /* Now set HI to H, so that the following code will set the
2000 other fields correctly. */
2004 /* Check if HI is a warning symbol. */
2005 if (hi->root.type == bfd_link_hash_warning)
2006 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2008 /* If there is a duplicate definition somewhere, then HI may not
2009 point to an indirect symbol. We will have reported an error to
2010 the user in that case. */
2012 if (hi->root.type == bfd_link_hash_indirect)
2014 struct elf_link_hash_entry *ht;
2016 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2017 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2019 /* A reference to the SHORTNAME symbol from a dynamic library
2020 will be satisfied by the versioned symbol at runtime. In
2021 effect, we have a reference to the versioned symbol. */
2022 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2023 hi->dynamic_def |= ht->dynamic_def;
2025 /* See if the new flags lead us to realize that the symbol must
2031 if (! bfd_link_executable (info)
2038 if (hi->ref_regular)
2044 /* We also need to define an indirection from the nondefault version
2048 len = strlen (name);
2049 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2050 if (shortname == NULL)
2052 memcpy (shortname, name, shortlen);
2053 memcpy (shortname + shortlen, p + 1, len - shortlen);
2055 /* Once again, merge with any existing symbol. */
2056 type_change_ok = FALSE;
2057 size_change_ok = FALSE;
2059 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2060 &hi, poldbfd, NULL, NULL, &skip, &override,
2061 &type_change_ok, &size_change_ok, &matched))
2069 /* Here SHORTNAME is a versioned name, so we don't expect to see
2070 the type of override we do in the case above unless it is
2071 overridden by a versioned definition. */
2072 if (hi->root.type != bfd_link_hash_defined
2073 && hi->root.type != bfd_link_hash_defweak)
2075 /* xgettext:c-format */
2076 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2082 if (! (_bfd_generic_link_add_one_symbol
2083 (info, abfd, shortname, BSF_INDIRECT,
2084 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2086 hi = (struct elf_link_hash_entry *) bh;
2088 /* If there is a duplicate definition somewhere, then HI may not
2089 point to an indirect symbol. We will have reported an error
2090 to the user in that case. */
2092 if (hi->root.type == bfd_link_hash_indirect)
2094 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2095 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2096 hi->dynamic_def |= h->dynamic_def;
2098 /* See if the new flags lead us to realize that the symbol
2104 if (! bfd_link_executable (info)
2110 if (hi->ref_regular)
2120 /* This routine is used to export all defined symbols into the dynamic
2121 symbol table. It is called via elf_link_hash_traverse. */
2124 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2126 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2128 /* Ignore indirect symbols. These are added by the versioning code. */
2129 if (h->root.type == bfd_link_hash_indirect)
2132 /* Ignore this if we won't export it. */
2133 if (!eif->info->export_dynamic && !h->dynamic)
2136 if (h->dynindx == -1
2137 && (h->def_regular || h->ref_regular)
2138 && ! bfd_hide_sym_by_version (eif->info->version_info,
2139 h->root.root.string))
2141 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2151 /* Look through the symbols which are defined in other shared
2152 libraries and referenced here. Update the list of version
2153 dependencies. This will be put into the .gnu.version_r section.
2154 This function is called via elf_link_hash_traverse. */
2157 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2160 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2161 Elf_Internal_Verneed *t;
2162 Elf_Internal_Vernaux *a;
2165 /* We only care about symbols defined in shared objects with version
2170 || h->verinfo.verdef == NULL
2171 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2172 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2175 /* See if we already know about this version. */
2176 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2180 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2183 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2184 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2190 /* This is a new version. Add it to tree we are building. */
2195 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2198 rinfo->failed = TRUE;
2202 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2203 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2204 elf_tdata (rinfo->info->output_bfd)->verref = t;
2208 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2211 rinfo->failed = TRUE;
2215 /* Note that we are copying a string pointer here, and testing it
2216 above. If bfd_elf_string_from_elf_section is ever changed to
2217 discard the string data when low in memory, this will have to be
2219 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2221 a->vna_flags = h->verinfo.verdef->vd_flags;
2222 a->vna_nextptr = t->vn_auxptr;
2224 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2227 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2234 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2235 hidden. Set *T_P to NULL if there is no match. */
2238 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2239 struct elf_link_hash_entry *h,
2240 const char *version_p,
2241 struct bfd_elf_version_tree **t_p,
2244 struct bfd_elf_version_tree *t;
2246 /* Look for the version. If we find it, it is no longer weak. */
2247 for (t = info->version_info; t != NULL; t = t->next)
2249 if (strcmp (t->name, version_p) == 0)
2253 struct bfd_elf_version_expr *d;
2255 len = version_p - h->root.root.string;
2256 alc = (char *) bfd_malloc (len);
2259 memcpy (alc, h->root.root.string, len - 1);
2260 alc[len - 1] = '\0';
2261 if (alc[len - 2] == ELF_VER_CHR)
2262 alc[len - 2] = '\0';
2264 h->verinfo.vertree = t;
2268 if (t->globals.list != NULL)
2269 d = (*t->match) (&t->globals, NULL, alc);
2271 /* See if there is anything to force this symbol to
2273 if (d == NULL && t->locals.list != NULL)
2275 d = (*t->match) (&t->locals, NULL, alc);
2278 && ! info->export_dynamic)
2292 /* Return TRUE if the symbol H is hidden by version script. */
2295 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2296 struct elf_link_hash_entry *h)
2299 bfd_boolean hide = FALSE;
2300 const struct elf_backend_data *bed
2301 = get_elf_backend_data (info->output_bfd);
2303 /* Version script only hides symbols defined in regular objects. */
2304 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2307 p = strchr (h->root.root.string, ELF_VER_CHR);
2308 if (p != NULL && h->verinfo.vertree == NULL)
2310 struct bfd_elf_version_tree *t;
2313 if (*p == ELF_VER_CHR)
2317 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2321 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2326 /* If we don't have a version for this symbol, see if we can find
2328 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2331 = bfd_find_version_for_sym (info->version_info,
2332 h->root.root.string, &hide);
2333 if (h->verinfo.vertree != NULL && hide)
2335 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2343 /* Figure out appropriate versions for all the symbols. We may not
2344 have the version number script until we have read all of the input
2345 files, so until that point we don't know which symbols should be
2346 local. This function is called via elf_link_hash_traverse. */
2349 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2351 struct elf_info_failed *sinfo;
2352 struct bfd_link_info *info;
2353 const struct elf_backend_data *bed;
2354 struct elf_info_failed eif;
2358 sinfo = (struct elf_info_failed *) data;
2361 /* Fix the symbol flags. */
2364 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2367 sinfo->failed = TRUE;
2371 bed = get_elf_backend_data (info->output_bfd);
2373 /* We only need version numbers for symbols defined in regular
2375 if (!h->def_regular)
2377 /* Hide symbols defined in discarded input sections. */
2378 if ((h->root.type == bfd_link_hash_defined
2379 || h->root.type == bfd_link_hash_defweak)
2380 && discarded_section (h->root.u.def.section))
2381 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2386 p = strchr (h->root.root.string, ELF_VER_CHR);
2387 if (p != NULL && h->verinfo.vertree == NULL)
2389 struct bfd_elf_version_tree *t;
2392 if (*p == ELF_VER_CHR)
2395 /* If there is no version string, we can just return out. */
2399 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2401 sinfo->failed = TRUE;
2406 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2408 /* If we are building an application, we need to create a
2409 version node for this version. */
2410 if (t == NULL && bfd_link_executable (info))
2412 struct bfd_elf_version_tree **pp;
2415 /* If we aren't going to export this symbol, we don't need
2416 to worry about it. */
2417 if (h->dynindx == -1)
2420 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2424 sinfo->failed = TRUE;
2429 t->name_indx = (unsigned int) -1;
2433 /* Don't count anonymous version tag. */
2434 if (sinfo->info->version_info != NULL
2435 && sinfo->info->version_info->vernum == 0)
2437 for (pp = &sinfo->info->version_info;
2441 t->vernum = version_index;
2445 h->verinfo.vertree = t;
2449 /* We could not find the version for a symbol when
2450 generating a shared archive. Return an error. */
2452 /* xgettext:c-format */
2453 (_("%pB: version node not found for symbol %s"),
2454 info->output_bfd, h->root.root.string);
2455 bfd_set_error (bfd_error_bad_value);
2456 sinfo->failed = TRUE;
2461 /* If we don't have a version for this symbol, see if we can find
2464 && h->verinfo.vertree == NULL
2465 && sinfo->info->version_info != NULL)
2468 = bfd_find_version_for_sym (sinfo->info->version_info,
2469 h->root.root.string, &hide);
2470 if (h->verinfo.vertree != NULL && hide)
2471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2477 /* Read and swap the relocs from the section indicated by SHDR. This
2478 may be either a REL or a RELA section. The relocations are
2479 translated into RELA relocations and stored in INTERNAL_RELOCS,
2480 which should have already been allocated to contain enough space.
2481 The EXTERNAL_RELOCS are a buffer where the external form of the
2482 relocations should be stored.
2484 Returns FALSE if something goes wrong. */
2487 elf_link_read_relocs_from_section (bfd *abfd,
2489 Elf_Internal_Shdr *shdr,
2490 void *external_relocs,
2491 Elf_Internal_Rela *internal_relocs)
2493 const struct elf_backend_data *bed;
2494 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2495 const bfd_byte *erela;
2496 const bfd_byte *erelaend;
2497 Elf_Internal_Rela *irela;
2498 Elf_Internal_Shdr *symtab_hdr;
2501 /* Position ourselves at the start of the section. */
2502 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2505 /* Read the relocations. */
2506 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2509 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2510 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2512 bed = get_elf_backend_data (abfd);
2514 /* Convert the external relocations to the internal format. */
2515 if (shdr->sh_entsize == bed->s->sizeof_rel)
2516 swap_in = bed->s->swap_reloc_in;
2517 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2518 swap_in = bed->s->swap_reloca_in;
2521 bfd_set_error (bfd_error_wrong_format);
2525 erela = (const bfd_byte *) external_relocs;
2526 erelaend = erela + shdr->sh_size;
2527 irela = internal_relocs;
2528 while (erela < erelaend)
2532 (*swap_in) (abfd, erela, irela);
2533 r_symndx = ELF32_R_SYM (irela->r_info);
2534 if (bed->s->arch_size == 64)
2538 if ((size_t) r_symndx >= nsyms)
2541 /* xgettext:c-format */
2542 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2543 " for offset %#" PRIx64 " in section `%pA'"),
2544 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2545 (uint64_t) irela->r_offset, sec);
2546 bfd_set_error (bfd_error_bad_value);
2550 else if (r_symndx != STN_UNDEF)
2553 /* xgettext:c-format */
2554 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2555 " for offset %#" PRIx64 " in section `%pA'"
2556 " when the object file has no symbol table"),
2557 abfd, (uint64_t) r_symndx,
2558 (uint64_t) irela->r_offset, sec);
2559 bfd_set_error (bfd_error_bad_value);
2562 irela += bed->s->int_rels_per_ext_rel;
2563 erela += shdr->sh_entsize;
2569 /* Read and swap the relocs for a section O. They may have been
2570 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2571 not NULL, they are used as buffers to read into. They are known to
2572 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2573 the return value is allocated using either malloc or bfd_alloc,
2574 according to the KEEP_MEMORY argument. If O has two relocation
2575 sections (both REL and RELA relocations), then the REL_HDR
2576 relocations will appear first in INTERNAL_RELOCS, followed by the
2577 RELA_HDR relocations. */
2580 _bfd_elf_link_read_relocs (bfd *abfd,
2582 void *external_relocs,
2583 Elf_Internal_Rela *internal_relocs,
2584 bfd_boolean keep_memory)
2586 void *alloc1 = NULL;
2587 Elf_Internal_Rela *alloc2 = NULL;
2588 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2589 struct bfd_elf_section_data *esdo = elf_section_data (o);
2590 Elf_Internal_Rela *internal_rela_relocs;
2592 if (esdo->relocs != NULL)
2593 return esdo->relocs;
2595 if (o->reloc_count == 0)
2598 if (internal_relocs == NULL)
2602 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2604 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2606 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2607 if (internal_relocs == NULL)
2611 if (external_relocs == NULL)
2613 bfd_size_type size = 0;
2616 size += esdo->rel.hdr->sh_size;
2618 size += esdo->rela.hdr->sh_size;
2620 alloc1 = bfd_malloc (size);
2623 external_relocs = alloc1;
2626 internal_rela_relocs = internal_relocs;
2629 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2633 external_relocs = (((bfd_byte *) external_relocs)
2634 + esdo->rel.hdr->sh_size);
2635 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2636 * bed->s->int_rels_per_ext_rel);
2640 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2642 internal_rela_relocs)))
2645 /* Cache the results for next time, if we can. */
2647 esdo->relocs = internal_relocs;
2652 /* Don't free alloc2, since if it was allocated we are passing it
2653 back (under the name of internal_relocs). */
2655 return internal_relocs;
2663 bfd_release (abfd, alloc2);
2670 /* Compute the size of, and allocate space for, REL_HDR which is the
2671 section header for a section containing relocations for O. */
2674 _bfd_elf_link_size_reloc_section (bfd *abfd,
2675 struct bfd_elf_section_reloc_data *reldata)
2677 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2679 /* That allows us to calculate the size of the section. */
2680 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2682 /* The contents field must last into write_object_contents, so we
2683 allocate it with bfd_alloc rather than malloc. Also since we
2684 cannot be sure that the contents will actually be filled in,
2685 we zero the allocated space. */
2686 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2687 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2690 if (reldata->hashes == NULL && reldata->count)
2692 struct elf_link_hash_entry **p;
2694 p = ((struct elf_link_hash_entry **)
2695 bfd_zmalloc (reldata->count * sizeof (*p)));
2699 reldata->hashes = p;
2705 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2706 originated from the section given by INPUT_REL_HDR) to the
2710 _bfd_elf_link_output_relocs (bfd *output_bfd,
2711 asection *input_section,
2712 Elf_Internal_Shdr *input_rel_hdr,
2713 Elf_Internal_Rela *internal_relocs,
2714 struct elf_link_hash_entry **rel_hash
2717 Elf_Internal_Rela *irela;
2718 Elf_Internal_Rela *irelaend;
2720 struct bfd_elf_section_reloc_data *output_reldata;
2721 asection *output_section;
2722 const struct elf_backend_data *bed;
2723 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2724 struct bfd_elf_section_data *esdo;
2726 output_section = input_section->output_section;
2728 bed = get_elf_backend_data (output_bfd);
2729 esdo = elf_section_data (output_section);
2730 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2732 output_reldata = &esdo->rel;
2733 swap_out = bed->s->swap_reloc_out;
2735 else if (esdo->rela.hdr
2736 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2738 output_reldata = &esdo->rela;
2739 swap_out = bed->s->swap_reloca_out;
2744 /* xgettext:c-format */
2745 (_("%pB: relocation size mismatch in %pB section %pA"),
2746 output_bfd, input_section->owner, input_section);
2747 bfd_set_error (bfd_error_wrong_format);
2751 erel = output_reldata->hdr->contents;
2752 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2753 irela = internal_relocs;
2754 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2755 * bed->s->int_rels_per_ext_rel);
2756 while (irela < irelaend)
2758 (*swap_out) (output_bfd, irela, erel);
2759 irela += bed->s->int_rels_per_ext_rel;
2760 erel += input_rel_hdr->sh_entsize;
2763 /* Bump the counter, so that we know where to add the next set of
2765 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2770 /* Make weak undefined symbols in PIE dynamic. */
2773 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2774 struct elf_link_hash_entry *h)
2776 if (bfd_link_pie (info)
2778 && h->root.type == bfd_link_hash_undefweak)
2779 return bfd_elf_link_record_dynamic_symbol (info, h);
2784 /* Fix up the flags for a symbol. This handles various cases which
2785 can only be fixed after all the input files are seen. This is
2786 currently called by both adjust_dynamic_symbol and
2787 assign_sym_version, which is unnecessary but perhaps more robust in
2788 the face of future changes. */
2791 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2792 struct elf_info_failed *eif)
2794 const struct elf_backend_data *bed;
2796 /* If this symbol was mentioned in a non-ELF file, try to set
2797 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2798 permit a non-ELF file to correctly refer to a symbol defined in
2799 an ELF dynamic object. */
2802 while (h->root.type == bfd_link_hash_indirect)
2803 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2805 if (h->root.type != bfd_link_hash_defined
2806 && h->root.type != bfd_link_hash_defweak)
2809 h->ref_regular_nonweak = 1;
2813 if (h->root.u.def.section->owner != NULL
2814 && (bfd_get_flavour (h->root.u.def.section->owner)
2815 == bfd_target_elf_flavour))
2818 h->ref_regular_nonweak = 1;
2824 if (h->dynindx == -1
2828 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2837 /* Unfortunately, NON_ELF is only correct if the symbol
2838 was first seen in a non-ELF file. Fortunately, if the symbol
2839 was first seen in an ELF file, we're probably OK unless the
2840 symbol was defined in a non-ELF file. Catch that case here.
2841 FIXME: We're still in trouble if the symbol was first seen in
2842 a dynamic object, and then later in a non-ELF regular object. */
2843 if ((h->root.type == bfd_link_hash_defined
2844 || h->root.type == bfd_link_hash_defweak)
2846 && (h->root.u.def.section->owner != NULL
2847 ? (bfd_get_flavour (h->root.u.def.section->owner)
2848 != bfd_target_elf_flavour)
2849 : (bfd_is_abs_section (h->root.u.def.section)
2850 && !h->def_dynamic)))
2854 /* Backend specific symbol fixup. */
2855 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2856 if (bed->elf_backend_fixup_symbol
2857 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2860 /* If this is a final link, and the symbol was defined as a common
2861 symbol in a regular object file, and there was no definition in
2862 any dynamic object, then the linker will have allocated space for
2863 the symbol in a common section but the DEF_REGULAR
2864 flag will not have been set. */
2865 if (h->root.type == bfd_link_hash_defined
2869 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2872 /* Symbols defined in discarded sections shouldn't be dynamic. */
2873 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2874 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2876 /* If a weak undefined symbol has non-default visibility, we also
2877 hide it from the dynamic linker. */
2878 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2879 && h->root.type == bfd_link_hash_undefweak)
2880 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2882 /* A hidden versioned symbol in executable should be forced local if
2883 it is is locally defined, not referenced by shared library and not
2885 else if (bfd_link_executable (eif->info)
2886 && h->versioned == versioned_hidden
2887 && !eif->info->export_dynamic
2891 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2893 /* If -Bsymbolic was used (which means to bind references to global
2894 symbols to the definition within the shared object), and this
2895 symbol was defined in a regular object, then it actually doesn't
2896 need a PLT entry. Likewise, if the symbol has non-default
2897 visibility. If the symbol has hidden or internal visibility, we
2898 will force it local. */
2899 else if (h->needs_plt
2900 && bfd_link_pic (eif->info)
2901 && is_elf_hash_table (eif->info->hash)
2902 && (SYMBOLIC_BIND (eif->info, h)
2903 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2906 bfd_boolean force_local;
2908 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2909 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2910 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2913 /* If this is a weak defined symbol in a dynamic object, and we know
2914 the real definition in the dynamic object, copy interesting flags
2915 over to the real definition. */
2916 if (h->is_weakalias)
2918 struct elf_link_hash_entry *def = weakdef (h);
2920 /* If the real definition is defined by a regular object file,
2921 don't do anything special. See the longer description in
2922 _bfd_elf_adjust_dynamic_symbol, below. */
2923 if (def->def_regular)
2926 while ((h = h->u.alias) != def)
2927 h->is_weakalias = 0;
2931 while (h->root.type == bfd_link_hash_indirect)
2932 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2933 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2934 || h->root.type == bfd_link_hash_defweak);
2935 BFD_ASSERT (def->def_dynamic);
2936 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2937 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2944 /* Make the backend pick a good value for a dynamic symbol. This is
2945 called via elf_link_hash_traverse, and also calls itself
2949 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2951 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2952 struct elf_link_hash_table *htab;
2953 const struct elf_backend_data *bed;
2955 if (! is_elf_hash_table (eif->info->hash))
2958 /* Ignore indirect symbols. These are added by the versioning code. */
2959 if (h->root.type == bfd_link_hash_indirect)
2962 /* Fix the symbol flags. */
2963 if (! _bfd_elf_fix_symbol_flags (h, eif))
2966 htab = elf_hash_table (eif->info);
2967 bed = get_elf_backend_data (htab->dynobj);
2969 if (h->root.type == bfd_link_hash_undefweak)
2971 if (eif->info->dynamic_undefined_weak == 0)
2972 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2973 else if (eif->info->dynamic_undefined_weak > 0
2975 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2976 && !bfd_hide_sym_by_version (eif->info->version_info,
2977 h->root.root.string))
2979 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2987 /* If this symbol does not require a PLT entry, and it is not
2988 defined by a dynamic object, or is not referenced by a regular
2989 object, ignore it. We do have to handle a weak defined symbol,
2990 even if no regular object refers to it, if we decided to add it
2991 to the dynamic symbol table. FIXME: Do we normally need to worry
2992 about symbols which are defined by one dynamic object and
2993 referenced by another one? */
2995 && h->type != STT_GNU_IFUNC
2999 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3001 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3005 /* If we've already adjusted this symbol, don't do it again. This
3006 can happen via a recursive call. */
3007 if (h->dynamic_adjusted)
3010 /* Don't look at this symbol again. Note that we must set this
3011 after checking the above conditions, because we may look at a
3012 symbol once, decide not to do anything, and then get called
3013 recursively later after REF_REGULAR is set below. */
3014 h->dynamic_adjusted = 1;
3016 /* If this is a weak definition, and we know a real definition, and
3017 the real symbol is not itself defined by a regular object file,
3018 then get a good value for the real definition. We handle the
3019 real symbol first, for the convenience of the backend routine.
3021 Note that there is a confusing case here. If the real definition
3022 is defined by a regular object file, we don't get the real symbol
3023 from the dynamic object, but we do get the weak symbol. If the
3024 processor backend uses a COPY reloc, then if some routine in the
3025 dynamic object changes the real symbol, we will not see that
3026 change in the corresponding weak symbol. This is the way other
3027 ELF linkers work as well, and seems to be a result of the shared
3030 I will clarify this issue. Most SVR4 shared libraries define the
3031 variable _timezone and define timezone as a weak synonym. The
3032 tzset call changes _timezone. If you write
3033 extern int timezone;
3035 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3036 you might expect that, since timezone is a synonym for _timezone,
3037 the same number will print both times. However, if the processor
3038 backend uses a COPY reloc, then actually timezone will be copied
3039 into your process image, and, since you define _timezone
3040 yourself, _timezone will not. Thus timezone and _timezone will
3041 wind up at different memory locations. The tzset call will set
3042 _timezone, leaving timezone unchanged. */
3044 if (h->is_weakalias)
3046 struct elf_link_hash_entry *def = weakdef (h);
3048 /* If we get to this point, there is an implicit reference to
3049 the alias by a regular object file via the weak symbol H. */
3050 def->ref_regular = 1;
3052 /* Ensure that the backend adjust_dynamic_symbol function sees
3053 the strong alias before H by recursively calling ourselves. */
3054 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3058 /* If a symbol has no type and no size and does not require a PLT
3059 entry, then we are probably about to do the wrong thing here: we
3060 are probably going to create a COPY reloc for an empty object.
3061 This case can arise when a shared object is built with assembly
3062 code, and the assembly code fails to set the symbol type. */
3064 && h->type == STT_NOTYPE
3067 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3068 h->root.root.string);
3070 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3079 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3083 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3084 struct elf_link_hash_entry *h,
3087 unsigned int power_of_two;
3089 asection *sec = h->root.u.def.section;
3091 /* The section alignment of the definition is the maximum alignment
3092 requirement of symbols defined in the section. Since we don't
3093 know the symbol alignment requirement, we start with the
3094 maximum alignment and check low bits of the symbol address
3095 for the minimum alignment. */
3096 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3097 mask = ((bfd_vma) 1 << power_of_two) - 1;
3098 while ((h->root.u.def.value & mask) != 0)
3104 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3107 /* Adjust the section alignment if needed. */
3108 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3113 /* We make sure that the symbol will be aligned properly. */
3114 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3116 /* Define the symbol as being at this point in DYNBSS. */
3117 h->root.u.def.section = dynbss;
3118 h->root.u.def.value = dynbss->size;
3120 /* Increment the size of DYNBSS to make room for the symbol. */
3121 dynbss->size += h->size;
3123 /* No error if extern_protected_data is true. */
3124 if (h->protected_def
3125 && (!info->extern_protected_data
3126 || (info->extern_protected_data < 0
3127 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3128 info->callbacks->einfo
3129 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3130 h->root.root.string);
3135 /* Adjust all external symbols pointing into SEC_MERGE sections
3136 to reflect the object merging within the sections. */
3139 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3143 if ((h->root.type == bfd_link_hash_defined
3144 || h->root.type == bfd_link_hash_defweak)
3145 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3146 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3148 bfd *output_bfd = (bfd *) data;
3150 h->root.u.def.value =
3151 _bfd_merged_section_offset (output_bfd,
3152 &h->root.u.def.section,
3153 elf_section_data (sec)->sec_info,
3154 h->root.u.def.value);
3160 /* Returns false if the symbol referred to by H should be considered
3161 to resolve local to the current module, and true if it should be
3162 considered to bind dynamically. */
3165 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3166 struct bfd_link_info *info,
3167 bfd_boolean not_local_protected)
3169 bfd_boolean binding_stays_local_p;
3170 const struct elf_backend_data *bed;
3171 struct elf_link_hash_table *hash_table;
3176 while (h->root.type == bfd_link_hash_indirect
3177 || h->root.type == bfd_link_hash_warning)
3178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3180 /* If it was forced local, then clearly it's not dynamic. */
3181 if (h->dynindx == -1)
3183 if (h->forced_local)
3186 /* Identify the cases where name binding rules say that a
3187 visible symbol resolves locally. */
3188 binding_stays_local_p = (bfd_link_executable (info)
3189 || SYMBOLIC_BIND (info, h));
3191 switch (ELF_ST_VISIBILITY (h->other))
3198 hash_table = elf_hash_table (info);
3199 if (!is_elf_hash_table (hash_table))
3202 bed = get_elf_backend_data (hash_table->dynobj);
3204 /* Proper resolution for function pointer equality may require
3205 that these symbols perhaps be resolved dynamically, even though
3206 we should be resolving them to the current module. */
3207 if (!not_local_protected || !bed->is_function_type (h->type))
3208 binding_stays_local_p = TRUE;
3215 /* If it isn't defined locally, then clearly it's dynamic. */
3216 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3219 /* Otherwise, the symbol is dynamic if binding rules don't tell
3220 us that it remains local. */
3221 return !binding_stays_local_p;
3224 /* Return true if the symbol referred to by H should be considered
3225 to resolve local to the current module, and false otherwise. Differs
3226 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3227 undefined symbols. The two functions are virtually identical except
3228 for the place where dynindx == -1 is tested. If that test is true,
3229 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3230 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3232 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3233 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3234 treatment of undefined weak symbols. For those that do not make
3235 undefined weak symbols dynamic, both functions may return false. */
3238 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3239 struct bfd_link_info *info,
3240 bfd_boolean local_protected)
3242 const struct elf_backend_data *bed;
3243 struct elf_link_hash_table *hash_table;
3245 /* If it's a local sym, of course we resolve locally. */
3249 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3250 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3251 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3254 /* Forced local symbols resolve locally. */
3255 if (h->forced_local)
3258 /* Common symbols that become definitions don't get the DEF_REGULAR
3259 flag set, so test it first, and don't bail out. */
3260 if (ELF_COMMON_DEF_P (h))
3262 /* If we don't have a definition in a regular file, then we can't
3263 resolve locally. The sym is either undefined or dynamic. */
3264 else if (!h->def_regular)
3267 /* Non-dynamic symbols resolve locally. */
3268 if (h->dynindx == -1)
3271 /* At this point, we know the symbol is defined and dynamic. In an
3272 executable it must resolve locally, likewise when building symbolic
3273 shared libraries. */
3274 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3277 /* Now deal with defined dynamic symbols in shared libraries. Ones
3278 with default visibility might not resolve locally. */
3279 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3282 hash_table = elf_hash_table (info);
3283 if (!is_elf_hash_table (hash_table))
3286 bed = get_elf_backend_data (hash_table->dynobj);
3288 /* If extern_protected_data is false, STV_PROTECTED non-function
3289 symbols are local. */
3290 if ((!info->extern_protected_data
3291 || (info->extern_protected_data < 0
3292 && !bed->extern_protected_data))
3293 && !bed->is_function_type (h->type))
3296 /* Function pointer equality tests may require that STV_PROTECTED
3297 symbols be treated as dynamic symbols. If the address of a
3298 function not defined in an executable is set to that function's
3299 plt entry in the executable, then the address of the function in
3300 a shared library must also be the plt entry in the executable. */
3301 return local_protected;
3304 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3305 aligned. Returns the first TLS output section. */
3307 struct bfd_section *
3308 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3310 struct bfd_section *sec, *tls;
3311 unsigned int align = 0;
3313 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3314 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3318 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3319 if (sec->alignment_power > align)
3320 align = sec->alignment_power;
3322 elf_hash_table (info)->tls_sec = tls;
3324 /* Ensure the alignment of the first section is the largest alignment,
3325 so that the tls segment starts aligned. */
3327 tls->alignment_power = align;
3332 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3334 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3335 Elf_Internal_Sym *sym)
3337 const struct elf_backend_data *bed;
3339 /* Local symbols do not count, but target specific ones might. */
3340 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3341 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3344 bed = get_elf_backend_data (abfd);
3345 /* Function symbols do not count. */
3346 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3349 /* If the section is undefined, then so is the symbol. */
3350 if (sym->st_shndx == SHN_UNDEF)
3353 /* If the symbol is defined in the common section, then
3354 it is a common definition and so does not count. */
3355 if (bed->common_definition (sym))
3358 /* If the symbol is in a target specific section then we
3359 must rely upon the backend to tell us what it is. */
3360 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3361 /* FIXME - this function is not coded yet:
3363 return _bfd_is_global_symbol_definition (abfd, sym);
3365 Instead for now assume that the definition is not global,
3366 Even if this is wrong, at least the linker will behave
3367 in the same way that it used to do. */
3373 /* Search the symbol table of the archive element of the archive ABFD
3374 whose archive map contains a mention of SYMDEF, and determine if
3375 the symbol is defined in this element. */
3377 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3379 Elf_Internal_Shdr * hdr;
3383 Elf_Internal_Sym *isymbuf;
3384 Elf_Internal_Sym *isym;
3385 Elf_Internal_Sym *isymend;
3388 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3392 if (! bfd_check_format (abfd, bfd_object))
3395 /* Select the appropriate symbol table. If we don't know if the
3396 object file is an IR object, give linker LTO plugin a chance to
3397 get the correct symbol table. */
3398 if (abfd->plugin_format == bfd_plugin_yes
3399 #if BFD_SUPPORTS_PLUGINS
3400 || (abfd->plugin_format == bfd_plugin_unknown
3401 && bfd_link_plugin_object_p (abfd))
3405 /* Use the IR symbol table if the object has been claimed by
3407 abfd = abfd->plugin_dummy_bfd;
3408 hdr = &elf_tdata (abfd)->symtab_hdr;
3410 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3411 hdr = &elf_tdata (abfd)->symtab_hdr;
3413 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3415 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3417 /* The sh_info field of the symtab header tells us where the
3418 external symbols start. We don't care about the local symbols. */
3419 if (elf_bad_symtab (abfd))
3421 extsymcount = symcount;
3426 extsymcount = symcount - hdr->sh_info;
3427 extsymoff = hdr->sh_info;
3430 if (extsymcount == 0)
3433 /* Read in the symbol table. */
3434 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3436 if (isymbuf == NULL)
3439 /* Scan the symbol table looking for SYMDEF. */
3441 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3445 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3450 if (strcmp (name, symdef->name) == 0)
3452 result = is_global_data_symbol_definition (abfd, isym);
3462 /* Add an entry to the .dynamic table. */
3465 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3469 struct elf_link_hash_table *hash_table;
3470 const struct elf_backend_data *bed;
3472 bfd_size_type newsize;
3473 bfd_byte *newcontents;
3474 Elf_Internal_Dyn dyn;
3476 hash_table = elf_hash_table (info);
3477 if (! is_elf_hash_table (hash_table))
3480 if (tag == DT_RELA || tag == DT_REL)
3481 hash_table->dynamic_relocs = TRUE;
3483 bed = get_elf_backend_data (hash_table->dynobj);
3484 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3485 BFD_ASSERT (s != NULL);
3487 newsize = s->size + bed->s->sizeof_dyn;
3488 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3489 if (newcontents == NULL)
3493 dyn.d_un.d_val = val;
3494 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3497 s->contents = newcontents;
3502 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3503 otherwise just check whether one already exists. Returns -1 on error,
3504 1 if a DT_NEEDED tag already exists, and 0 on success. */
3507 elf_add_dt_needed_tag (bfd *abfd,
3508 struct bfd_link_info *info,
3512 struct elf_link_hash_table *hash_table;
3515 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3518 hash_table = elf_hash_table (info);
3519 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3520 if (strindex == (size_t) -1)
3523 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3526 const struct elf_backend_data *bed;
3529 bed = get_elf_backend_data (hash_table->dynobj);
3530 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3532 for (extdyn = sdyn->contents;
3533 extdyn < sdyn->contents + sdyn->size;
3534 extdyn += bed->s->sizeof_dyn)
3536 Elf_Internal_Dyn dyn;
3538 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3539 if (dyn.d_tag == DT_NEEDED
3540 && dyn.d_un.d_val == strindex)
3542 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3550 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3553 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3557 /* We were just checking for existence of the tag. */
3558 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3563 /* Return true if SONAME is on the needed list between NEEDED and STOP
3564 (or the end of list if STOP is NULL), and needed by a library that
3568 on_needed_list (const char *soname,
3569 struct bfd_link_needed_list *needed,
3570 struct bfd_link_needed_list *stop)
3572 struct bfd_link_needed_list *look;
3573 for (look = needed; look != stop; look = look->next)
3574 if (strcmp (soname, look->name) == 0
3575 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3576 /* If needed by a library that itself is not directly
3577 needed, recursively check whether that library is
3578 indirectly needed. Since we add DT_NEEDED entries to
3579 the end of the list, library dependencies appear after
3580 the library. Therefore search prior to the current
3581 LOOK, preventing possible infinite recursion. */
3582 || on_needed_list (elf_dt_name (look->by), needed, look)))
3588 /* Sort symbol by value, section, and size. */
3590 elf_sort_symbol (const void *arg1, const void *arg2)
3592 const struct elf_link_hash_entry *h1;
3593 const struct elf_link_hash_entry *h2;
3594 bfd_signed_vma vdiff;
3596 h1 = *(const struct elf_link_hash_entry **) arg1;
3597 h2 = *(const struct elf_link_hash_entry **) arg2;
3598 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3600 return vdiff > 0 ? 1 : -1;
3603 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3605 return sdiff > 0 ? 1 : -1;
3607 vdiff = h1->size - h2->size;
3608 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3611 /* This function is used to adjust offsets into .dynstr for
3612 dynamic symbols. This is called via elf_link_hash_traverse. */
3615 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3617 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3619 if (h->dynindx != -1)
3620 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3624 /* Assign string offsets in .dynstr, update all structures referencing
3628 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3630 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3631 struct elf_link_local_dynamic_entry *entry;
3632 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3633 bfd *dynobj = hash_table->dynobj;
3636 const struct elf_backend_data *bed;
3639 _bfd_elf_strtab_finalize (dynstr);
3640 size = _bfd_elf_strtab_size (dynstr);
3642 bed = get_elf_backend_data (dynobj);
3643 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3644 BFD_ASSERT (sdyn != NULL);
3646 /* Update all .dynamic entries referencing .dynstr strings. */
3647 for (extdyn = sdyn->contents;
3648 extdyn < sdyn->contents + sdyn->size;
3649 extdyn += bed->s->sizeof_dyn)
3651 Elf_Internal_Dyn dyn;
3653 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3657 dyn.d_un.d_val = size;
3667 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3672 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3675 /* Now update local dynamic symbols. */
3676 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3677 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3678 entry->isym.st_name);
3680 /* And the rest of dynamic symbols. */
3681 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3683 /* Adjust version definitions. */
3684 if (elf_tdata (output_bfd)->cverdefs)
3689 Elf_Internal_Verdef def;
3690 Elf_Internal_Verdaux defaux;
3692 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3696 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3698 p += sizeof (Elf_External_Verdef);
3699 if (def.vd_aux != sizeof (Elf_External_Verdef))
3701 for (i = 0; i < def.vd_cnt; ++i)
3703 _bfd_elf_swap_verdaux_in (output_bfd,
3704 (Elf_External_Verdaux *) p, &defaux);
3705 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3707 _bfd_elf_swap_verdaux_out (output_bfd,
3708 &defaux, (Elf_External_Verdaux *) p);
3709 p += sizeof (Elf_External_Verdaux);
3712 while (def.vd_next);
3715 /* Adjust version references. */
3716 if (elf_tdata (output_bfd)->verref)
3721 Elf_Internal_Verneed need;
3722 Elf_Internal_Vernaux needaux;
3724 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3728 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3730 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3731 _bfd_elf_swap_verneed_out (output_bfd, &need,
3732 (Elf_External_Verneed *) p);
3733 p += sizeof (Elf_External_Verneed);
3734 for (i = 0; i < need.vn_cnt; ++i)
3736 _bfd_elf_swap_vernaux_in (output_bfd,
3737 (Elf_External_Vernaux *) p, &needaux);
3738 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3740 _bfd_elf_swap_vernaux_out (output_bfd,
3742 (Elf_External_Vernaux *) p);
3743 p += sizeof (Elf_External_Vernaux);
3746 while (need.vn_next);
3752 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3753 The default is to only match when the INPUT and OUTPUT are exactly
3757 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3758 const bfd_target *output)
3760 return input == output;
3763 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3764 This version is used when different targets for the same architecture
3765 are virtually identical. */
3768 _bfd_elf_relocs_compatible (const bfd_target *input,
3769 const bfd_target *output)
3771 const struct elf_backend_data *obed, *ibed;
3773 if (input == output)
3776 ibed = xvec_get_elf_backend_data (input);
3777 obed = xvec_get_elf_backend_data (output);
3779 if (ibed->arch != obed->arch)
3782 /* If both backends are using this function, deem them compatible. */
3783 return ibed->relocs_compatible == obed->relocs_compatible;
3786 /* Make a special call to the linker "notice" function to tell it that
3787 we are about to handle an as-needed lib, or have finished
3788 processing the lib. */
3791 _bfd_elf_notice_as_needed (bfd *ibfd,
3792 struct bfd_link_info *info,
3793 enum notice_asneeded_action act)
3795 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3798 /* Check relocations an ELF object file. */
3801 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3803 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3804 struct elf_link_hash_table *htab = elf_hash_table (info);
3806 /* If this object is the same format as the output object, and it is
3807 not a shared library, then let the backend look through the
3810 This is required to build global offset table entries and to
3811 arrange for dynamic relocs. It is not required for the
3812 particular common case of linking non PIC code, even when linking
3813 against shared libraries, but unfortunately there is no way of
3814 knowing whether an object file has been compiled PIC or not.
3815 Looking through the relocs is not particularly time consuming.
3816 The problem is that we must either (1) keep the relocs in memory,
3817 which causes the linker to require additional runtime memory or
3818 (2) read the relocs twice from the input file, which wastes time.
3819 This would be a good case for using mmap.
3821 I have no idea how to handle linking PIC code into a file of a
3822 different format. It probably can't be done. */
3823 if ((abfd->flags & DYNAMIC) == 0
3824 && is_elf_hash_table (htab)
3825 && bed->check_relocs != NULL
3826 && elf_object_id (abfd) == elf_hash_table_id (htab)
3827 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3831 for (o = abfd->sections; o != NULL; o = o->next)
3833 Elf_Internal_Rela *internal_relocs;
3836 /* Don't check relocations in excluded sections. */
3837 if ((o->flags & SEC_RELOC) == 0
3838 || (o->flags & SEC_EXCLUDE) != 0
3839 || o->reloc_count == 0
3840 || ((info->strip == strip_all || info->strip == strip_debugger)
3841 && (o->flags & SEC_DEBUGGING) != 0)
3842 || bfd_is_abs_section (o->output_section))
3845 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3847 if (internal_relocs == NULL)
3850 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3852 if (elf_section_data (o)->relocs != internal_relocs)
3853 free (internal_relocs);
3863 /* Add symbols from an ELF object file to the linker hash table. */
3866 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3868 Elf_Internal_Ehdr *ehdr;
3869 Elf_Internal_Shdr *hdr;
3873 struct elf_link_hash_entry **sym_hash;
3874 bfd_boolean dynamic;
3875 Elf_External_Versym *extversym = NULL;
3876 Elf_External_Versym *ever;
3877 struct elf_link_hash_entry *weaks;
3878 struct elf_link_hash_entry **nondeflt_vers = NULL;
3879 size_t nondeflt_vers_cnt = 0;
3880 Elf_Internal_Sym *isymbuf = NULL;
3881 Elf_Internal_Sym *isym;
3882 Elf_Internal_Sym *isymend;
3883 const struct elf_backend_data *bed;
3884 bfd_boolean add_needed;
3885 struct elf_link_hash_table *htab;
3887 void *alloc_mark = NULL;
3888 struct bfd_hash_entry **old_table = NULL;
3889 unsigned int old_size = 0;
3890 unsigned int old_count = 0;
3891 void *old_tab = NULL;
3893 struct bfd_link_hash_entry *old_undefs = NULL;
3894 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3895 void *old_strtab = NULL;
3898 bfd_boolean just_syms;
3900 htab = elf_hash_table (info);
3901 bed = get_elf_backend_data (abfd);
3903 if ((abfd->flags & DYNAMIC) == 0)
3909 /* You can't use -r against a dynamic object. Also, there's no
3910 hope of using a dynamic object which does not exactly match
3911 the format of the output file. */
3912 if (bfd_link_relocatable (info)
3913 || !is_elf_hash_table (htab)
3914 || info->output_bfd->xvec != abfd->xvec)
3916 if (bfd_link_relocatable (info))
3917 bfd_set_error (bfd_error_invalid_operation);
3919 bfd_set_error (bfd_error_wrong_format);
3924 ehdr = elf_elfheader (abfd);
3925 if (info->warn_alternate_em
3926 && bed->elf_machine_code != ehdr->e_machine
3927 && ((bed->elf_machine_alt1 != 0
3928 && ehdr->e_machine == bed->elf_machine_alt1)
3929 || (bed->elf_machine_alt2 != 0
3930 && ehdr->e_machine == bed->elf_machine_alt2)))
3932 /* xgettext:c-format */
3933 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3934 ehdr->e_machine, abfd, bed->elf_machine_code);
3936 /* As a GNU extension, any input sections which are named
3937 .gnu.warning.SYMBOL are treated as warning symbols for the given
3938 symbol. This differs from .gnu.warning sections, which generate
3939 warnings when they are included in an output file. */
3940 /* PR 12761: Also generate this warning when building shared libraries. */
3941 for (s = abfd->sections; s != NULL; s = s->next)
3945 name = bfd_get_section_name (abfd, s);
3946 if (CONST_STRNEQ (name, ".gnu.warning."))
3951 name += sizeof ".gnu.warning." - 1;
3953 /* If this is a shared object, then look up the symbol
3954 in the hash table. If it is there, and it is already
3955 been defined, then we will not be using the entry
3956 from this shared object, so we don't need to warn.
3957 FIXME: If we see the definition in a regular object
3958 later on, we will warn, but we shouldn't. The only
3959 fix is to keep track of what warnings we are supposed
3960 to emit, and then handle them all at the end of the
3964 struct elf_link_hash_entry *h;
3966 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3968 /* FIXME: What about bfd_link_hash_common? */
3970 && (h->root.type == bfd_link_hash_defined
3971 || h->root.type == bfd_link_hash_defweak))
3976 msg = (char *) bfd_alloc (abfd, sz + 1);
3980 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3985 if (! (_bfd_generic_link_add_one_symbol
3986 (info, abfd, name, BSF_WARNING, s, 0, msg,
3987 FALSE, bed->collect, NULL)))
3990 if (bfd_link_executable (info))
3992 /* Clobber the section size so that the warning does
3993 not get copied into the output file. */
3996 /* Also set SEC_EXCLUDE, so that symbols defined in
3997 the warning section don't get copied to the output. */
3998 s->flags |= SEC_EXCLUDE;
4003 just_syms = ((s = abfd->sections) != NULL
4004 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4009 /* If we are creating a shared library, create all the dynamic
4010 sections immediately. We need to attach them to something,
4011 so we attach them to this BFD, provided it is the right
4012 format and is not from ld --just-symbols. Always create the
4013 dynamic sections for -E/--dynamic-list. FIXME: If there
4014 are no input BFD's of the same format as the output, we can't
4015 make a shared library. */
4017 && (bfd_link_pic (info)
4018 || (!bfd_link_relocatable (info)
4020 && (info->export_dynamic || info->dynamic)))
4021 && is_elf_hash_table (htab)
4022 && info->output_bfd->xvec == abfd->xvec
4023 && !htab->dynamic_sections_created)
4025 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4029 else if (!is_elf_hash_table (htab))
4033 const char *soname = NULL;
4035 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4036 const Elf_Internal_Phdr *phdr;
4039 /* ld --just-symbols and dynamic objects don't mix very well.
4040 ld shouldn't allow it. */
4044 /* If this dynamic lib was specified on the command line with
4045 --as-needed in effect, then we don't want to add a DT_NEEDED
4046 tag unless the lib is actually used. Similary for libs brought
4047 in by another lib's DT_NEEDED. When --no-add-needed is used
4048 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4049 any dynamic library in DT_NEEDED tags in the dynamic lib at
4051 add_needed = (elf_dyn_lib_class (abfd)
4052 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4053 | DYN_NO_NEEDED)) == 0;
4055 s = bfd_get_section_by_name (abfd, ".dynamic");
4060 unsigned int elfsec;
4061 unsigned long shlink;
4063 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4070 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4071 if (elfsec == SHN_BAD)
4072 goto error_free_dyn;
4073 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4075 for (extdyn = dynbuf;
4076 extdyn < dynbuf + s->size;
4077 extdyn += bed->s->sizeof_dyn)
4079 Elf_Internal_Dyn dyn;
4081 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4082 if (dyn.d_tag == DT_SONAME)
4084 unsigned int tagv = dyn.d_un.d_val;
4085 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4087 goto error_free_dyn;
4089 if (dyn.d_tag == DT_NEEDED)
4091 struct bfd_link_needed_list *n, **pn;
4093 unsigned int tagv = dyn.d_un.d_val;
4095 amt = sizeof (struct bfd_link_needed_list);
4096 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4097 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4098 if (n == NULL || fnm == NULL)
4099 goto error_free_dyn;
4100 amt = strlen (fnm) + 1;
4101 anm = (char *) bfd_alloc (abfd, amt);
4103 goto error_free_dyn;
4104 memcpy (anm, fnm, amt);
4108 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4112 if (dyn.d_tag == DT_RUNPATH)
4114 struct bfd_link_needed_list *n, **pn;
4116 unsigned int tagv = dyn.d_un.d_val;
4118 amt = sizeof (struct bfd_link_needed_list);
4119 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4120 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4121 if (n == NULL || fnm == NULL)
4122 goto error_free_dyn;
4123 amt = strlen (fnm) + 1;
4124 anm = (char *) bfd_alloc (abfd, amt);
4126 goto error_free_dyn;
4127 memcpy (anm, fnm, amt);
4131 for (pn = & runpath;
4137 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4138 if (!runpath && dyn.d_tag == DT_RPATH)
4140 struct bfd_link_needed_list *n, **pn;
4142 unsigned int tagv = dyn.d_un.d_val;
4144 amt = sizeof (struct bfd_link_needed_list);
4145 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4146 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4147 if (n == NULL || fnm == NULL)
4148 goto error_free_dyn;
4149 amt = strlen (fnm) + 1;
4150 anm = (char *) bfd_alloc (abfd, amt);
4152 goto error_free_dyn;
4153 memcpy (anm, fnm, amt);
4163 if (dyn.d_tag == DT_AUDIT)
4165 unsigned int tagv = dyn.d_un.d_val;
4166 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4173 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4174 frees all more recently bfd_alloc'd blocks as well. */
4180 struct bfd_link_needed_list **pn;
4181 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4186 /* If we have a PT_GNU_RELRO program header, mark as read-only
4187 all sections contained fully therein. This makes relro
4188 shared library sections appear as they will at run-time. */
4189 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4190 while (--phdr >= elf_tdata (abfd)->phdr)
4191 if (phdr->p_type == PT_GNU_RELRO)
4193 for (s = abfd->sections; s != NULL; s = s->next)
4194 if ((s->flags & SEC_ALLOC) != 0
4195 && s->vma >= phdr->p_vaddr
4196 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4197 s->flags |= SEC_READONLY;
4201 /* We do not want to include any of the sections in a dynamic
4202 object in the output file. We hack by simply clobbering the
4203 list of sections in the BFD. This could be handled more
4204 cleanly by, say, a new section flag; the existing
4205 SEC_NEVER_LOAD flag is not the one we want, because that one
4206 still implies that the section takes up space in the output
4208 bfd_section_list_clear (abfd);
4210 /* Find the name to use in a DT_NEEDED entry that refers to this
4211 object. If the object has a DT_SONAME entry, we use it.
4212 Otherwise, if the generic linker stuck something in
4213 elf_dt_name, we use that. Otherwise, we just use the file
4215 if (soname == NULL || *soname == '\0')
4217 soname = elf_dt_name (abfd);
4218 if (soname == NULL || *soname == '\0')
4219 soname = bfd_get_filename (abfd);
4222 /* Save the SONAME because sometimes the linker emulation code
4223 will need to know it. */
4224 elf_dt_name (abfd) = soname;
4226 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4230 /* If we have already included this dynamic object in the
4231 link, just ignore it. There is no reason to include a
4232 particular dynamic object more than once. */
4236 /* Save the DT_AUDIT entry for the linker emulation code. */
4237 elf_dt_audit (abfd) = audit;
4240 /* If this is a dynamic object, we always link against the .dynsym
4241 symbol table, not the .symtab symbol table. The dynamic linker
4242 will only see the .dynsym symbol table, so there is no reason to
4243 look at .symtab for a dynamic object. */
4245 if (! dynamic || elf_dynsymtab (abfd) == 0)
4246 hdr = &elf_tdata (abfd)->symtab_hdr;
4248 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4250 symcount = hdr->sh_size / bed->s->sizeof_sym;
4252 /* The sh_info field of the symtab header tells us where the
4253 external symbols start. We don't care about the local symbols at
4255 if (elf_bad_symtab (abfd))
4257 extsymcount = symcount;
4262 extsymcount = symcount - hdr->sh_info;
4263 extsymoff = hdr->sh_info;
4266 sym_hash = elf_sym_hashes (abfd);
4267 if (extsymcount != 0)
4269 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4271 if (isymbuf == NULL)
4274 if (sym_hash == NULL)
4276 /* We store a pointer to the hash table entry for each
4279 amt *= sizeof (struct elf_link_hash_entry *);
4280 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4281 if (sym_hash == NULL)
4282 goto error_free_sym;
4283 elf_sym_hashes (abfd) = sym_hash;
4289 /* Read in any version definitions. */
4290 if (!_bfd_elf_slurp_version_tables (abfd,
4291 info->default_imported_symver))
4292 goto error_free_sym;
4294 /* Read in the symbol versions, but don't bother to convert them
4295 to internal format. */
4296 if (elf_dynversym (abfd) != 0)
4298 Elf_Internal_Shdr *versymhdr;
4300 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4301 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4302 if (extversym == NULL)
4303 goto error_free_sym;
4304 amt = versymhdr->sh_size;
4305 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4306 || bfd_bread (extversym, amt, abfd) != amt)
4307 goto error_free_vers;
4311 /* If we are loading an as-needed shared lib, save the symbol table
4312 state before we start adding symbols. If the lib turns out
4313 to be unneeded, restore the state. */
4314 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4319 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4321 struct bfd_hash_entry *p;
4322 struct elf_link_hash_entry *h;
4324 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4326 h = (struct elf_link_hash_entry *) p;
4327 entsize += htab->root.table.entsize;
4328 if (h->root.type == bfd_link_hash_warning)
4329 entsize += htab->root.table.entsize;
4333 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4334 old_tab = bfd_malloc (tabsize + entsize);
4335 if (old_tab == NULL)
4336 goto error_free_vers;
4338 /* Remember the current objalloc pointer, so that all mem for
4339 symbols added can later be reclaimed. */
4340 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4341 if (alloc_mark == NULL)
4342 goto error_free_vers;
4344 /* Make a special call to the linker "notice" function to
4345 tell it that we are about to handle an as-needed lib. */
4346 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4347 goto error_free_vers;
4349 /* Clone the symbol table. Remember some pointers into the
4350 symbol table, and dynamic symbol count. */
4351 old_ent = (char *) old_tab + tabsize;
4352 memcpy (old_tab, htab->root.table.table, tabsize);
4353 old_undefs = htab->root.undefs;
4354 old_undefs_tail = htab->root.undefs_tail;
4355 old_table = htab->root.table.table;
4356 old_size = htab->root.table.size;
4357 old_count = htab->root.table.count;
4358 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4359 if (old_strtab == NULL)
4360 goto error_free_vers;
4362 for (i = 0; i < htab->root.table.size; i++)
4364 struct bfd_hash_entry *p;
4365 struct elf_link_hash_entry *h;
4367 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4369 memcpy (old_ent, p, htab->root.table.entsize);
4370 old_ent = (char *) old_ent + htab->root.table.entsize;
4371 h = (struct elf_link_hash_entry *) p;
4372 if (h->root.type == bfd_link_hash_warning)
4374 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4375 old_ent = (char *) old_ent + htab->root.table.entsize;
4382 ever = extversym != NULL ? extversym + extsymoff : NULL;
4383 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4385 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4389 asection *sec, *new_sec;
4392 struct elf_link_hash_entry *h;
4393 struct elf_link_hash_entry *hi;
4394 bfd_boolean definition;
4395 bfd_boolean size_change_ok;
4396 bfd_boolean type_change_ok;
4397 bfd_boolean new_weak;
4398 bfd_boolean old_weak;
4399 bfd_boolean override;
4401 bfd_boolean discarded;
4402 unsigned int old_alignment;
4404 bfd_boolean matched;
4408 flags = BSF_NO_FLAGS;
4410 value = isym->st_value;
4411 common = bed->common_definition (isym);
4412 if (common && info->inhibit_common_definition)
4414 /* Treat common symbol as undefined for --no-define-common. */
4415 isym->st_shndx = SHN_UNDEF;
4420 bind = ELF_ST_BIND (isym->st_info);
4424 /* This should be impossible, since ELF requires that all
4425 global symbols follow all local symbols, and that sh_info
4426 point to the first global symbol. Unfortunately, Irix 5
4431 if (isym->st_shndx != SHN_UNDEF && !common)
4439 case STB_GNU_UNIQUE:
4440 flags = BSF_GNU_UNIQUE;
4444 /* Leave it up to the processor backend. */
4448 if (isym->st_shndx == SHN_UNDEF)
4449 sec = bfd_und_section_ptr;
4450 else if (isym->st_shndx == SHN_ABS)
4451 sec = bfd_abs_section_ptr;
4452 else if (isym->st_shndx == SHN_COMMON)
4454 sec = bfd_com_section_ptr;
4455 /* What ELF calls the size we call the value. What ELF
4456 calls the value we call the alignment. */
4457 value = isym->st_size;
4461 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4463 sec = bfd_abs_section_ptr;
4464 else if (discarded_section (sec))
4466 /* Symbols from discarded section are undefined. We keep
4468 sec = bfd_und_section_ptr;
4470 isym->st_shndx = SHN_UNDEF;
4472 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4476 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4479 goto error_free_vers;
4481 if (isym->st_shndx == SHN_COMMON
4482 && (abfd->flags & BFD_PLUGIN) != 0)
4484 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4488 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4490 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4492 goto error_free_vers;
4496 else if (isym->st_shndx == SHN_COMMON
4497 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4498 && !bfd_link_relocatable (info))
4500 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4504 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4505 | SEC_LINKER_CREATED);
4506 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4508 goto error_free_vers;
4512 else if (bed->elf_add_symbol_hook)
4514 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4516 goto error_free_vers;
4518 /* The hook function sets the name to NULL if this symbol
4519 should be skipped for some reason. */
4524 /* Sanity check that all possibilities were handled. */
4527 bfd_set_error (bfd_error_bad_value);
4528 goto error_free_vers;
4531 /* Silently discard TLS symbols from --just-syms. There's
4532 no way to combine a static TLS block with a new TLS block
4533 for this executable. */
4534 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4535 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4538 if (bfd_is_und_section (sec)
4539 || bfd_is_com_section (sec))
4544 size_change_ok = FALSE;
4545 type_change_ok = bed->type_change_ok;
4552 if (is_elf_hash_table (htab))
4554 Elf_Internal_Versym iver;
4555 unsigned int vernum = 0;
4560 if (info->default_imported_symver)
4561 /* Use the default symbol version created earlier. */
4562 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4567 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4569 vernum = iver.vs_vers & VERSYM_VERSION;
4571 /* If this is a hidden symbol, or if it is not version
4572 1, we append the version name to the symbol name.
4573 However, we do not modify a non-hidden absolute symbol
4574 if it is not a function, because it might be the version
4575 symbol itself. FIXME: What if it isn't? */
4576 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4578 && (!bfd_is_abs_section (sec)
4579 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4582 size_t namelen, verlen, newlen;
4585 if (isym->st_shndx != SHN_UNDEF)
4587 if (vernum > elf_tdata (abfd)->cverdefs)
4589 else if (vernum > 1)
4591 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4598 /* xgettext:c-format */
4599 (_("%pB: %s: invalid version %u (max %d)"),
4601 elf_tdata (abfd)->cverdefs);
4602 bfd_set_error (bfd_error_bad_value);
4603 goto error_free_vers;
4608 /* We cannot simply test for the number of
4609 entries in the VERNEED section since the
4610 numbers for the needed versions do not start
4612 Elf_Internal_Verneed *t;
4615 for (t = elf_tdata (abfd)->verref;
4619 Elf_Internal_Vernaux *a;
4621 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4623 if (a->vna_other == vernum)
4625 verstr = a->vna_nodename;
4635 /* xgettext:c-format */
4636 (_("%pB: %s: invalid needed version %d"),
4637 abfd, name, vernum);
4638 bfd_set_error (bfd_error_bad_value);
4639 goto error_free_vers;
4643 namelen = strlen (name);
4644 verlen = strlen (verstr);
4645 newlen = namelen + verlen + 2;
4646 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4647 && isym->st_shndx != SHN_UNDEF)
4650 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4651 if (newname == NULL)
4652 goto error_free_vers;
4653 memcpy (newname, name, namelen);
4654 p = newname + namelen;
4656 /* If this is a defined non-hidden version symbol,
4657 we add another @ to the name. This indicates the
4658 default version of the symbol. */
4659 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4660 && isym->st_shndx != SHN_UNDEF)
4662 memcpy (p, verstr, verlen + 1);
4667 /* If this symbol has default visibility and the user has
4668 requested we not re-export it, then mark it as hidden. */
4669 if (!bfd_is_und_section (sec)
4672 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4673 isym->st_other = (STV_HIDDEN
4674 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4676 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4677 sym_hash, &old_bfd, &old_weak,
4678 &old_alignment, &skip, &override,
4679 &type_change_ok, &size_change_ok,
4681 goto error_free_vers;
4686 /* Override a definition only if the new symbol matches the
4688 if (override && matched)
4692 while (h->root.type == bfd_link_hash_indirect
4693 || h->root.type == bfd_link_hash_warning)
4694 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4696 if (elf_tdata (abfd)->verdef != NULL
4699 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4702 if (! (_bfd_generic_link_add_one_symbol
4703 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4704 (struct bfd_link_hash_entry **) sym_hash)))
4705 goto error_free_vers;
4707 if ((abfd->flags & DYNAMIC) == 0
4708 && (bfd_get_flavour (info->output_bfd)
4709 == bfd_target_elf_flavour))
4711 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4712 elf_tdata (info->output_bfd)->has_gnu_symbols
4713 |= elf_gnu_symbol_ifunc;
4714 if ((flags & BSF_GNU_UNIQUE))
4715 elf_tdata (info->output_bfd)->has_gnu_symbols
4716 |= elf_gnu_symbol_unique;
4720 /* We need to make sure that indirect symbol dynamic flags are
4723 while (h->root.type == bfd_link_hash_indirect
4724 || h->root.type == bfd_link_hash_warning)
4725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4727 /* Setting the index to -3 tells elf_link_output_extsym that
4728 this symbol is defined in a discarded section. */
4734 new_weak = (flags & BSF_WEAK) != 0;
4738 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4739 && is_elf_hash_table (htab)
4740 && h->u.alias == NULL)
4742 /* Keep a list of all weak defined non function symbols from
4743 a dynamic object, using the alias field. Later in this
4744 function we will set the alias field to the correct
4745 value. We only put non-function symbols from dynamic
4746 objects on this list, because that happens to be the only
4747 time we need to know the normal symbol corresponding to a
4748 weak symbol, and the information is time consuming to
4749 figure out. If the alias field is not already NULL,
4750 then this symbol was already defined by some previous
4751 dynamic object, and we will be using that previous
4752 definition anyhow. */
4758 /* Set the alignment of a common symbol. */
4759 if ((common || bfd_is_com_section (sec))
4760 && h->root.type == bfd_link_hash_common)
4765 align = bfd_log2 (isym->st_value);
4768 /* The new symbol is a common symbol in a shared object.
4769 We need to get the alignment from the section. */
4770 align = new_sec->alignment_power;
4772 if (align > old_alignment)
4773 h->root.u.c.p->alignment_power = align;
4775 h->root.u.c.p->alignment_power = old_alignment;
4778 if (is_elf_hash_table (htab))
4780 /* Set a flag in the hash table entry indicating the type of
4781 reference or definition we just found. A dynamic symbol
4782 is one which is referenced or defined by both a regular
4783 object and a shared object. */
4784 bfd_boolean dynsym = FALSE;
4786 /* Plugin symbols aren't normal. Don't set def_regular or
4787 ref_regular for them, or make them dynamic. */
4788 if ((abfd->flags & BFD_PLUGIN) != 0)
4795 if (bind != STB_WEAK)
4796 h->ref_regular_nonweak = 1;
4808 /* If the indirect symbol has been forced local, don't
4809 make the real symbol dynamic. */
4810 if ((h == hi || !hi->forced_local)
4811 && (bfd_link_dll (info)
4821 hi->ref_dynamic = 1;
4826 hi->def_dynamic = 1;
4829 /* If the indirect symbol has been forced local, don't
4830 make the real symbol dynamic. */
4831 if ((h == hi || !hi->forced_local)
4835 && weakdef (h)->dynindx != -1)))
4839 /* Check to see if we need to add an indirect symbol for
4840 the default name. */
4842 || (!override && h->root.type == bfd_link_hash_common))
4843 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4844 sec, value, &old_bfd, &dynsym))
4845 goto error_free_vers;
4847 /* Check the alignment when a common symbol is involved. This
4848 can change when a common symbol is overridden by a normal
4849 definition or a common symbol is ignored due to the old
4850 normal definition. We need to make sure the maximum
4851 alignment is maintained. */
4852 if ((old_alignment || common)
4853 && h->root.type != bfd_link_hash_common)
4855 unsigned int common_align;
4856 unsigned int normal_align;
4857 unsigned int symbol_align;
4861 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4862 || h->root.type == bfd_link_hash_defweak);
4864 symbol_align = ffs (h->root.u.def.value) - 1;
4865 if (h->root.u.def.section->owner != NULL
4866 && (h->root.u.def.section->owner->flags
4867 & (DYNAMIC | BFD_PLUGIN)) == 0)
4869 normal_align = h->root.u.def.section->alignment_power;
4870 if (normal_align > symbol_align)
4871 normal_align = symbol_align;
4874 normal_align = symbol_align;
4878 common_align = old_alignment;
4879 common_bfd = old_bfd;
4884 common_align = bfd_log2 (isym->st_value);
4886 normal_bfd = old_bfd;
4889 if (normal_align < common_align)
4891 /* PR binutils/2735 */
4892 if (normal_bfd == NULL)
4894 /* xgettext:c-format */
4895 (_("warning: alignment %u of common symbol `%s' in %pB is"
4896 " greater than the alignment (%u) of its section %pA"),
4897 1 << common_align, name, common_bfd,
4898 1 << normal_align, h->root.u.def.section);
4901 /* xgettext:c-format */
4902 (_("warning: alignment %u of symbol `%s' in %pB"
4903 " is smaller than %u in %pB"),
4904 1 << normal_align, name, normal_bfd,
4905 1 << common_align, common_bfd);
4909 /* Remember the symbol size if it isn't undefined. */
4910 if (isym->st_size != 0
4911 && isym->st_shndx != SHN_UNDEF
4912 && (definition || h->size == 0))
4915 && h->size != isym->st_size
4916 && ! size_change_ok)
4918 /* xgettext:c-format */
4919 (_("warning: size of symbol `%s' changed"
4920 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4921 name, (uint64_t) h->size, old_bfd,
4922 (uint64_t) isym->st_size, abfd);
4924 h->size = isym->st_size;
4927 /* If this is a common symbol, then we always want H->SIZE
4928 to be the size of the common symbol. The code just above
4929 won't fix the size if a common symbol becomes larger. We
4930 don't warn about a size change here, because that is
4931 covered by --warn-common. Allow changes between different
4933 if (h->root.type == bfd_link_hash_common)
4934 h->size = h->root.u.c.size;
4936 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4937 && ((definition && !new_weak)
4938 || (old_weak && h->root.type == bfd_link_hash_common)
4939 || h->type == STT_NOTYPE))
4941 unsigned int type = ELF_ST_TYPE (isym->st_info);
4943 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4945 if (type == STT_GNU_IFUNC
4946 && (abfd->flags & DYNAMIC) != 0)
4949 if (h->type != type)
4951 if (h->type != STT_NOTYPE && ! type_change_ok)
4952 /* xgettext:c-format */
4954 (_("warning: type of symbol `%s' changed"
4955 " from %d to %d in %pB"),
4956 name, h->type, type, abfd);
4962 /* Merge st_other field. */
4963 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4965 /* We don't want to make debug symbol dynamic. */
4967 && (sec->flags & SEC_DEBUGGING)
4968 && !bfd_link_relocatable (info))
4971 /* Nor should we make plugin symbols dynamic. */
4972 if ((abfd->flags & BFD_PLUGIN) != 0)
4977 h->target_internal = isym->st_target_internal;
4978 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4981 if (definition && !dynamic)
4983 char *p = strchr (name, ELF_VER_CHR);
4984 if (p != NULL && p[1] != ELF_VER_CHR)
4986 /* Queue non-default versions so that .symver x, x@FOO
4987 aliases can be checked. */
4990 amt = ((isymend - isym + 1)
4991 * sizeof (struct elf_link_hash_entry *));
4993 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4995 goto error_free_vers;
4997 nondeflt_vers[nondeflt_vers_cnt++] = h;
5001 if (dynsym && h->dynindx == -1)
5003 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5004 goto error_free_vers;
5006 && weakdef (h)->dynindx == -1)
5008 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5009 goto error_free_vers;
5012 else if (h->dynindx != -1)
5013 /* If the symbol already has a dynamic index, but
5014 visibility says it should not be visible, turn it into
5016 switch (ELF_ST_VISIBILITY (h->other))
5020 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5025 /* Don't add DT_NEEDED for references from the dummy bfd nor
5026 for unmatched symbol. */
5031 && h->ref_regular_nonweak
5033 || (old_bfd->flags & BFD_PLUGIN) == 0))
5034 || (h->ref_dynamic_nonweak
5035 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5036 && !on_needed_list (elf_dt_name (abfd),
5037 htab->needed, NULL))))
5040 const char *soname = elf_dt_name (abfd);
5042 info->callbacks->minfo ("%!", soname, old_bfd,
5043 h->root.root.string);
5045 /* A symbol from a library loaded via DT_NEEDED of some
5046 other library is referenced by a regular object.
5047 Add a DT_NEEDED entry for it. Issue an error if
5048 --no-add-needed is used and the reference was not
5051 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5054 /* xgettext:c-format */
5055 (_("%pB: undefined reference to symbol '%s'"),
5057 bfd_set_error (bfd_error_missing_dso);
5058 goto error_free_vers;
5061 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5062 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5065 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5067 goto error_free_vers;
5069 BFD_ASSERT (ret == 0);
5074 if (info->lto_plugin_active
5075 && !bfd_link_relocatable (info)
5076 && (abfd->flags & BFD_PLUGIN) == 0
5082 if (bed->s->arch_size == 32)
5087 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5088 referenced in regular objects so that linker plugin will get
5089 the correct symbol resolution. */
5091 sym_hash = elf_sym_hashes (abfd);
5092 for (s = abfd->sections; s != NULL; s = s->next)
5094 Elf_Internal_Rela *internal_relocs;
5095 Elf_Internal_Rela *rel, *relend;
5097 /* Don't check relocations in excluded sections. */
5098 if ((s->flags & SEC_RELOC) == 0
5099 || s->reloc_count == 0
5100 || (s->flags & SEC_EXCLUDE) != 0
5101 || ((info->strip == strip_all
5102 || info->strip == strip_debugger)
5103 && (s->flags & SEC_DEBUGGING) != 0))
5106 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5109 if (internal_relocs == NULL)
5110 goto error_free_vers;
5112 rel = internal_relocs;
5113 relend = rel + s->reloc_count;
5114 for ( ; rel < relend; rel++)
5116 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5117 struct elf_link_hash_entry *h;
5119 /* Skip local symbols. */
5120 if (r_symndx < extsymoff)
5123 h = sym_hash[r_symndx - extsymoff];
5125 h->root.non_ir_ref_regular = 1;
5128 if (elf_section_data (s)->relocs != internal_relocs)
5129 free (internal_relocs);
5133 if (extversym != NULL)
5139 if (isymbuf != NULL)
5145 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5149 /* Restore the symbol table. */
5150 old_ent = (char *) old_tab + tabsize;
5151 memset (elf_sym_hashes (abfd), 0,
5152 extsymcount * sizeof (struct elf_link_hash_entry *));
5153 htab->root.table.table = old_table;
5154 htab->root.table.size = old_size;
5155 htab->root.table.count = old_count;
5156 memcpy (htab->root.table.table, old_tab, tabsize);
5157 htab->root.undefs = old_undefs;
5158 htab->root.undefs_tail = old_undefs_tail;
5159 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5162 for (i = 0; i < htab->root.table.size; i++)
5164 struct bfd_hash_entry *p;
5165 struct elf_link_hash_entry *h;
5167 unsigned int alignment_power;
5168 unsigned int non_ir_ref_dynamic;
5170 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5172 h = (struct elf_link_hash_entry *) p;
5173 if (h->root.type == bfd_link_hash_warning)
5174 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5176 /* Preserve the maximum alignment and size for common
5177 symbols even if this dynamic lib isn't on DT_NEEDED
5178 since it can still be loaded at run time by another
5180 if (h->root.type == bfd_link_hash_common)
5182 size = h->root.u.c.size;
5183 alignment_power = h->root.u.c.p->alignment_power;
5188 alignment_power = 0;
5190 /* Preserve non_ir_ref_dynamic so that this symbol
5191 will be exported when the dynamic lib becomes needed
5192 in the second pass. */
5193 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5194 memcpy (p, old_ent, htab->root.table.entsize);
5195 old_ent = (char *) old_ent + htab->root.table.entsize;
5196 h = (struct elf_link_hash_entry *) p;
5197 if (h->root.type == bfd_link_hash_warning)
5199 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5200 old_ent = (char *) old_ent + htab->root.table.entsize;
5201 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5203 if (h->root.type == bfd_link_hash_common)
5205 if (size > h->root.u.c.size)
5206 h->root.u.c.size = size;
5207 if (alignment_power > h->root.u.c.p->alignment_power)
5208 h->root.u.c.p->alignment_power = alignment_power;
5210 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5214 /* Make a special call to the linker "notice" function to
5215 tell it that symbols added for crefs may need to be removed. */
5216 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5217 goto error_free_vers;
5220 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5222 if (nondeflt_vers != NULL)
5223 free (nondeflt_vers);
5227 if (old_tab != NULL)
5229 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5230 goto error_free_vers;
5235 /* Now that all the symbols from this input file are created, if
5236 not performing a relocatable link, handle .symver foo, foo@BAR
5237 such that any relocs against foo become foo@BAR. */
5238 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5242 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5244 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5245 char *shortname, *p;
5247 p = strchr (h->root.root.string, ELF_VER_CHR);
5249 || (h->root.type != bfd_link_hash_defined
5250 && h->root.type != bfd_link_hash_defweak))
5253 amt = p - h->root.root.string;
5254 shortname = (char *) bfd_malloc (amt + 1);
5256 goto error_free_vers;
5257 memcpy (shortname, h->root.root.string, amt);
5258 shortname[amt] = '\0';
5260 hi = (struct elf_link_hash_entry *)
5261 bfd_link_hash_lookup (&htab->root, shortname,
5262 FALSE, FALSE, FALSE);
5264 && hi->root.type == h->root.type
5265 && hi->root.u.def.value == h->root.u.def.value
5266 && hi->root.u.def.section == h->root.u.def.section)
5268 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5269 hi->root.type = bfd_link_hash_indirect;
5270 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5271 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5272 sym_hash = elf_sym_hashes (abfd);
5274 for (symidx = 0; symidx < extsymcount; ++symidx)
5275 if (sym_hash[symidx] == hi)
5277 sym_hash[symidx] = h;
5283 free (nondeflt_vers);
5284 nondeflt_vers = NULL;
5287 /* Now set the alias field correctly for all the weak defined
5288 symbols we found. The only way to do this is to search all the
5289 symbols. Since we only need the information for non functions in
5290 dynamic objects, that's the only time we actually put anything on
5291 the list WEAKS. We need this information so that if a regular
5292 object refers to a symbol defined weakly in a dynamic object, the
5293 real symbol in the dynamic object is also put in the dynamic
5294 symbols; we also must arrange for both symbols to point to the
5295 same memory location. We could handle the general case of symbol
5296 aliasing, but a general symbol alias can only be generated in
5297 assembler code, handling it correctly would be very time
5298 consuming, and other ELF linkers don't handle general aliasing
5302 struct elf_link_hash_entry **hpp;
5303 struct elf_link_hash_entry **hppend;
5304 struct elf_link_hash_entry **sorted_sym_hash;
5305 struct elf_link_hash_entry *h;
5308 /* Since we have to search the whole symbol list for each weak
5309 defined symbol, search time for N weak defined symbols will be
5310 O(N^2). Binary search will cut it down to O(NlogN). */
5312 amt *= sizeof (struct elf_link_hash_entry *);
5313 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5314 if (sorted_sym_hash == NULL)
5316 sym_hash = sorted_sym_hash;
5317 hpp = elf_sym_hashes (abfd);
5318 hppend = hpp + extsymcount;
5320 for (; hpp < hppend; hpp++)
5324 && h->root.type == bfd_link_hash_defined
5325 && !bed->is_function_type (h->type))
5333 qsort (sorted_sym_hash, sym_count,
5334 sizeof (struct elf_link_hash_entry *),
5337 while (weaks != NULL)
5339 struct elf_link_hash_entry *hlook;
5342 size_t i, j, idx = 0;
5345 weaks = hlook->u.alias;
5346 hlook->u.alias = NULL;
5348 if (hlook->root.type != bfd_link_hash_defined
5349 && hlook->root.type != bfd_link_hash_defweak)
5352 slook = hlook->root.u.def.section;
5353 vlook = hlook->root.u.def.value;
5359 bfd_signed_vma vdiff;
5361 h = sorted_sym_hash[idx];
5362 vdiff = vlook - h->root.u.def.value;
5369 int sdiff = slook->id - h->root.u.def.section->id;
5379 /* We didn't find a value/section match. */
5383 /* With multiple aliases, or when the weak symbol is already
5384 strongly defined, we have multiple matching symbols and
5385 the binary search above may land on any of them. Step
5386 one past the matching symbol(s). */
5389 h = sorted_sym_hash[idx];
5390 if (h->root.u.def.section != slook
5391 || h->root.u.def.value != vlook)
5395 /* Now look back over the aliases. Since we sorted by size
5396 as well as value and section, we'll choose the one with
5397 the largest size. */
5400 h = sorted_sym_hash[idx];
5402 /* Stop if value or section doesn't match. */
5403 if (h->root.u.def.section != slook
5404 || h->root.u.def.value != vlook)
5406 else if (h != hlook)
5408 struct elf_link_hash_entry *t;
5411 hlook->is_weakalias = 1;
5413 if (t->u.alias != NULL)
5414 while (t->u.alias != h)
5418 /* If the weak definition is in the list of dynamic
5419 symbols, make sure the real definition is put
5421 if (hlook->dynindx != -1 && h->dynindx == -1)
5423 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5426 free (sorted_sym_hash);
5431 /* If the real definition is in the list of dynamic
5432 symbols, make sure the weak definition is put
5433 there as well. If we don't do this, then the
5434 dynamic loader might not merge the entries for the
5435 real definition and the weak definition. */
5436 if (h->dynindx != -1 && hlook->dynindx == -1)
5438 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5439 goto err_free_sym_hash;
5446 free (sorted_sym_hash);
5449 if (bed->check_directives
5450 && !(*bed->check_directives) (abfd, info))
5453 /* If this is a non-traditional link, try to optimize the handling
5454 of the .stab/.stabstr sections. */
5456 && ! info->traditional_format
5457 && is_elf_hash_table (htab)
5458 && (info->strip != strip_all && info->strip != strip_debugger))
5462 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5463 if (stabstr != NULL)
5465 bfd_size_type string_offset = 0;
5468 for (stab = abfd->sections; stab; stab = stab->next)
5469 if (CONST_STRNEQ (stab->name, ".stab")
5470 && (!stab->name[5] ||
5471 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5472 && (stab->flags & SEC_MERGE) == 0
5473 && !bfd_is_abs_section (stab->output_section))
5475 struct bfd_elf_section_data *secdata;
5477 secdata = elf_section_data (stab);
5478 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5479 stabstr, &secdata->sec_info,
5482 if (secdata->sec_info)
5483 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5488 if (is_elf_hash_table (htab) && add_needed)
5490 /* Add this bfd to the loaded list. */
5491 struct elf_link_loaded_list *n;
5493 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5497 n->next = htab->loaded;
5504 if (old_tab != NULL)
5506 if (old_strtab != NULL)
5508 if (nondeflt_vers != NULL)
5509 free (nondeflt_vers);
5510 if (extversym != NULL)
5513 if (isymbuf != NULL)
5519 /* Return the linker hash table entry of a symbol that might be
5520 satisfied by an archive symbol. Return -1 on error. */
5522 struct elf_link_hash_entry *
5523 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5524 struct bfd_link_info *info,
5527 struct elf_link_hash_entry *h;
5531 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5535 /* If this is a default version (the name contains @@), look up the
5536 symbol again with only one `@' as well as without the version.
5537 The effect is that references to the symbol with and without the
5538 version will be matched by the default symbol in the archive. */
5540 p = strchr (name, ELF_VER_CHR);
5541 if (p == NULL || p[1] != ELF_VER_CHR)
5544 /* First check with only one `@'. */
5545 len = strlen (name);
5546 copy = (char *) bfd_alloc (abfd, len);
5548 return (struct elf_link_hash_entry *) -1;
5550 first = p - name + 1;
5551 memcpy (copy, name, first);
5552 memcpy (copy + first, name + first + 1, len - first);
5554 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5557 /* We also need to check references to the symbol without the
5559 copy[first - 1] = '\0';
5560 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5561 FALSE, FALSE, TRUE);
5564 bfd_release (abfd, copy);
5568 /* Add symbols from an ELF archive file to the linker hash table. We
5569 don't use _bfd_generic_link_add_archive_symbols because we need to
5570 handle versioned symbols.
5572 Fortunately, ELF archive handling is simpler than that done by
5573 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5574 oddities. In ELF, if we find a symbol in the archive map, and the
5575 symbol is currently undefined, we know that we must pull in that
5578 Unfortunately, we do have to make multiple passes over the symbol
5579 table until nothing further is resolved. */
5582 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5585 unsigned char *included = NULL;
5589 const struct elf_backend_data *bed;
5590 struct elf_link_hash_entry * (*archive_symbol_lookup)
5591 (bfd *, struct bfd_link_info *, const char *);
5593 if (! bfd_has_map (abfd))
5595 /* An empty archive is a special case. */
5596 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5598 bfd_set_error (bfd_error_no_armap);
5602 /* Keep track of all symbols we know to be already defined, and all
5603 files we know to be already included. This is to speed up the
5604 second and subsequent passes. */
5605 c = bfd_ardata (abfd)->symdef_count;
5609 amt *= sizeof (*included);
5610 included = (unsigned char *) bfd_zmalloc (amt);
5611 if (included == NULL)
5614 symdefs = bfd_ardata (abfd)->symdefs;
5615 bed = get_elf_backend_data (abfd);
5616 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5629 symdefend = symdef + c;
5630 for (i = 0; symdef < symdefend; symdef++, i++)
5632 struct elf_link_hash_entry *h;
5634 struct bfd_link_hash_entry *undefs_tail;
5639 if (symdef->file_offset == last)
5645 h = archive_symbol_lookup (abfd, info, symdef->name);
5646 if (h == (struct elf_link_hash_entry *) -1)
5652 if (h->root.type == bfd_link_hash_common)
5654 /* We currently have a common symbol. The archive map contains
5655 a reference to this symbol, so we may want to include it. We
5656 only want to include it however, if this archive element
5657 contains a definition of the symbol, not just another common
5660 Unfortunately some archivers (including GNU ar) will put
5661 declarations of common symbols into their archive maps, as
5662 well as real definitions, so we cannot just go by the archive
5663 map alone. Instead we must read in the element's symbol
5664 table and check that to see what kind of symbol definition
5666 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5669 else if (h->root.type != bfd_link_hash_undefined)
5671 if (h->root.type != bfd_link_hash_undefweak)
5672 /* Symbol must be defined. Don't check it again. */
5677 /* We need to include this archive member. */
5678 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5679 if (element == NULL)
5682 if (! bfd_check_format (element, bfd_object))
5685 undefs_tail = info->hash->undefs_tail;
5687 if (!(*info->callbacks
5688 ->add_archive_element) (info, element, symdef->name, &element))
5690 if (!bfd_link_add_symbols (element, info))
5693 /* If there are any new undefined symbols, we need to make
5694 another pass through the archive in order to see whether
5695 they can be defined. FIXME: This isn't perfect, because
5696 common symbols wind up on undefs_tail and because an
5697 undefined symbol which is defined later on in this pass
5698 does not require another pass. This isn't a bug, but it
5699 does make the code less efficient than it could be. */
5700 if (undefs_tail != info->hash->undefs_tail)
5703 /* Look backward to mark all symbols from this object file
5704 which we have already seen in this pass. */
5708 included[mark] = TRUE;
5713 while (symdefs[mark].file_offset == symdef->file_offset);
5715 /* We mark subsequent symbols from this object file as we go
5716 on through the loop. */
5717 last = symdef->file_offset;
5727 if (included != NULL)
5732 /* Given an ELF BFD, add symbols to the global hash table as
5736 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5738 switch (bfd_get_format (abfd))
5741 return elf_link_add_object_symbols (abfd, info);
5743 return elf_link_add_archive_symbols (abfd, info);
5745 bfd_set_error (bfd_error_wrong_format);
5750 struct hash_codes_info
5752 unsigned long *hashcodes;
5756 /* This function will be called though elf_link_hash_traverse to store
5757 all hash value of the exported symbols in an array. */
5760 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5762 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5767 /* Ignore indirect symbols. These are added by the versioning code. */
5768 if (h->dynindx == -1)
5771 name = h->root.root.string;
5772 if (h->versioned >= versioned)
5774 char *p = strchr (name, ELF_VER_CHR);
5777 alc = (char *) bfd_malloc (p - name + 1);
5783 memcpy (alc, name, p - name);
5784 alc[p - name] = '\0';
5789 /* Compute the hash value. */
5790 ha = bfd_elf_hash (name);
5792 /* Store the found hash value in the array given as the argument. */
5793 *(inf->hashcodes)++ = ha;
5795 /* And store it in the struct so that we can put it in the hash table
5797 h->u.elf_hash_value = ha;
5805 struct collect_gnu_hash_codes
5808 const struct elf_backend_data *bed;
5809 unsigned long int nsyms;
5810 unsigned long int maskbits;
5811 unsigned long int *hashcodes;
5812 unsigned long int *hashval;
5813 unsigned long int *indx;
5814 unsigned long int *counts;
5817 long int min_dynindx;
5818 unsigned long int bucketcount;
5819 unsigned long int symindx;
5820 long int local_indx;
5821 long int shift1, shift2;
5822 unsigned long int mask;
5826 /* This function will be called though elf_link_hash_traverse to store
5827 all hash value of the exported symbols in an array. */
5830 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5832 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5837 /* Ignore indirect symbols. These are added by the versioning code. */
5838 if (h->dynindx == -1)
5841 /* Ignore also local symbols and undefined symbols. */
5842 if (! (*s->bed->elf_hash_symbol) (h))
5845 name = h->root.root.string;
5846 if (h->versioned >= versioned)
5848 char *p = strchr (name, ELF_VER_CHR);
5851 alc = (char *) bfd_malloc (p - name + 1);
5857 memcpy (alc, name, p - name);
5858 alc[p - name] = '\0';
5863 /* Compute the hash value. */
5864 ha = bfd_elf_gnu_hash (name);
5866 /* Store the found hash value in the array for compute_bucket_count,
5867 and also for .dynsym reordering purposes. */
5868 s->hashcodes[s->nsyms] = ha;
5869 s->hashval[h->dynindx] = ha;
5871 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5872 s->min_dynindx = h->dynindx;
5880 /* This function will be called though elf_link_hash_traverse to do
5881 final dynaminc symbol renumbering. */
5884 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5886 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5887 unsigned long int bucket;
5888 unsigned long int val;
5890 /* Ignore indirect symbols. */
5891 if (h->dynindx == -1)
5894 /* Ignore also local symbols and undefined symbols. */
5895 if (! (*s->bed->elf_hash_symbol) (h))
5897 if (h->dynindx >= s->min_dynindx)
5898 h->dynindx = s->local_indx++;
5902 bucket = s->hashval[h->dynindx] % s->bucketcount;
5903 val = (s->hashval[h->dynindx] >> s->shift1)
5904 & ((s->maskbits >> s->shift1) - 1);
5905 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5907 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5908 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5909 if (s->counts[bucket] == 1)
5910 /* Last element terminates the chain. */
5912 bfd_put_32 (s->output_bfd, val,
5913 s->contents + (s->indx[bucket] - s->symindx) * 4);
5914 --s->counts[bucket];
5915 h->dynindx = s->indx[bucket]++;
5919 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5922 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5924 return !(h->forced_local
5925 || h->root.type == bfd_link_hash_undefined
5926 || h->root.type == bfd_link_hash_undefweak
5927 || ((h->root.type == bfd_link_hash_defined
5928 || h->root.type == bfd_link_hash_defweak)
5929 && h->root.u.def.section->output_section == NULL));
5932 /* Array used to determine the number of hash table buckets to use
5933 based on the number of symbols there are. If there are fewer than
5934 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5935 fewer than 37 we use 17 buckets, and so forth. We never use more
5936 than 32771 buckets. */
5938 static const size_t elf_buckets[] =
5940 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5944 /* Compute bucket count for hashing table. We do not use a static set
5945 of possible tables sizes anymore. Instead we determine for all
5946 possible reasonable sizes of the table the outcome (i.e., the
5947 number of collisions etc) and choose the best solution. The
5948 weighting functions are not too simple to allow the table to grow
5949 without bounds. Instead one of the weighting factors is the size.
5950 Therefore the result is always a good payoff between few collisions
5951 (= short chain lengths) and table size. */
5953 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5954 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5955 unsigned long int nsyms,
5958 size_t best_size = 0;
5959 unsigned long int i;
5961 /* We have a problem here. The following code to optimize the table
5962 size requires an integer type with more the 32 bits. If
5963 BFD_HOST_U_64_BIT is set we know about such a type. */
5964 #ifdef BFD_HOST_U_64_BIT
5969 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5970 bfd *dynobj = elf_hash_table (info)->dynobj;
5971 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5972 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5973 unsigned long int *counts;
5975 unsigned int no_improvement_count = 0;
5977 /* Possible optimization parameters: if we have NSYMS symbols we say
5978 that the hashing table must at least have NSYMS/4 and at most
5980 minsize = nsyms / 4;
5983 best_size = maxsize = nsyms * 2;
5988 if ((best_size & 31) == 0)
5992 /* Create array where we count the collisions in. We must use bfd_malloc
5993 since the size could be large. */
5995 amt *= sizeof (unsigned long int);
5996 counts = (unsigned long int *) bfd_malloc (amt);
6000 /* Compute the "optimal" size for the hash table. The criteria is a
6001 minimal chain length. The minor criteria is (of course) the size
6003 for (i = minsize; i < maxsize; ++i)
6005 /* Walk through the array of hashcodes and count the collisions. */
6006 BFD_HOST_U_64_BIT max;
6007 unsigned long int j;
6008 unsigned long int fact;
6010 if (gnu_hash && (i & 31) == 0)
6013 memset (counts, '\0', i * sizeof (unsigned long int));
6015 /* Determine how often each hash bucket is used. */
6016 for (j = 0; j < nsyms; ++j)
6017 ++counts[hashcodes[j] % i];
6019 /* For the weight function we need some information about the
6020 pagesize on the target. This is information need not be 100%
6021 accurate. Since this information is not available (so far) we
6022 define it here to a reasonable default value. If it is crucial
6023 to have a better value some day simply define this value. */
6024 # ifndef BFD_TARGET_PAGESIZE
6025 # define BFD_TARGET_PAGESIZE (4096)
6028 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6030 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6033 /* Variant 1: optimize for short chains. We add the squares
6034 of all the chain lengths (which favors many small chain
6035 over a few long chains). */
6036 for (j = 0; j < i; ++j)
6037 max += counts[j] * counts[j];
6039 /* This adds penalties for the overall size of the table. */
6040 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6043 /* Variant 2: Optimize a lot more for small table. Here we
6044 also add squares of the size but we also add penalties for
6045 empty slots (the +1 term). */
6046 for (j = 0; j < i; ++j)
6047 max += (1 + counts[j]) * (1 + counts[j]);
6049 /* The overall size of the table is considered, but not as
6050 strong as in variant 1, where it is squared. */
6051 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6055 /* Compare with current best results. */
6056 if (max < best_chlen)
6060 no_improvement_count = 0;
6062 /* PR 11843: Avoid futile long searches for the best bucket size
6063 when there are a large number of symbols. */
6064 else if (++no_improvement_count == 100)
6071 #endif /* defined (BFD_HOST_U_64_BIT) */
6073 /* This is the fallback solution if no 64bit type is available or if we
6074 are not supposed to spend much time on optimizations. We select the
6075 bucket count using a fixed set of numbers. */
6076 for (i = 0; elf_buckets[i] != 0; i++)
6078 best_size = elf_buckets[i];
6079 if (nsyms < elf_buckets[i + 1])
6082 if (gnu_hash && best_size < 2)
6089 /* Size any SHT_GROUP section for ld -r. */
6092 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6097 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6098 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6099 && (s = ibfd->sections) != NULL
6100 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6101 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6106 /* Set a default stack segment size. The value in INFO wins. If it
6107 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6108 undefined it is initialized. */
6111 bfd_elf_stack_segment_size (bfd *output_bfd,
6112 struct bfd_link_info *info,
6113 const char *legacy_symbol,
6114 bfd_vma default_size)
6116 struct elf_link_hash_entry *h = NULL;
6118 /* Look for legacy symbol. */
6120 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6121 FALSE, FALSE, FALSE);
6122 if (h && (h->root.type == bfd_link_hash_defined
6123 || h->root.type == bfd_link_hash_defweak)
6125 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6127 /* The symbol has no type if specified on the command line. */
6128 h->type = STT_OBJECT;
6129 if (info->stacksize)
6130 /* xgettext:c-format */
6131 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6132 output_bfd, legacy_symbol);
6133 else if (h->root.u.def.section != bfd_abs_section_ptr)
6134 /* xgettext:c-format */
6135 _bfd_error_handler (_("%pB: %s not absolute"),
6136 output_bfd, legacy_symbol);
6138 info->stacksize = h->root.u.def.value;
6141 if (!info->stacksize)
6142 /* If the user didn't set a size, or explicitly inhibit the
6143 size, set it now. */
6144 info->stacksize = default_size;
6146 /* Provide the legacy symbol, if it is referenced. */
6147 if (h && (h->root.type == bfd_link_hash_undefined
6148 || h->root.type == bfd_link_hash_undefweak))
6150 struct bfd_link_hash_entry *bh = NULL;
6152 if (!(_bfd_generic_link_add_one_symbol
6153 (info, output_bfd, legacy_symbol,
6154 BSF_GLOBAL, bfd_abs_section_ptr,
6155 info->stacksize >= 0 ? info->stacksize : 0,
6156 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6159 h = (struct elf_link_hash_entry *) bh;
6161 h->type = STT_OBJECT;
6167 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6169 struct elf_gc_sweep_symbol_info
6171 struct bfd_link_info *info;
6172 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6177 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6180 && (((h->root.type == bfd_link_hash_defined
6181 || h->root.type == bfd_link_hash_defweak)
6182 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6183 && h->root.u.def.section->gc_mark))
6184 || h->root.type == bfd_link_hash_undefined
6185 || h->root.type == bfd_link_hash_undefweak))
6187 struct elf_gc_sweep_symbol_info *inf;
6189 inf = (struct elf_gc_sweep_symbol_info *) data;
6190 (*inf->hide_symbol) (inf->info, h, TRUE);
6193 h->ref_regular_nonweak = 0;
6199 /* Set up the sizes and contents of the ELF dynamic sections. This is
6200 called by the ELF linker emulation before_allocation routine. We
6201 must set the sizes of the sections before the linker sets the
6202 addresses of the various sections. */
6205 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6208 const char *filter_shlib,
6210 const char *depaudit,
6211 const char * const *auxiliary_filters,
6212 struct bfd_link_info *info,
6213 asection **sinterpptr)
6216 const struct elf_backend_data *bed;
6220 if (!is_elf_hash_table (info->hash))
6223 dynobj = elf_hash_table (info)->dynobj;
6225 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6227 struct bfd_elf_version_tree *verdefs;
6228 struct elf_info_failed asvinfo;
6229 struct bfd_elf_version_tree *t;
6230 struct bfd_elf_version_expr *d;
6234 /* If we are supposed to export all symbols into the dynamic symbol
6235 table (this is not the normal case), then do so. */
6236 if (info->export_dynamic
6237 || (bfd_link_executable (info) && info->dynamic))
6239 struct elf_info_failed eif;
6243 elf_link_hash_traverse (elf_hash_table (info),
6244 _bfd_elf_export_symbol,
6252 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6254 if (soname_indx == (size_t) -1
6255 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6259 soname_indx = (size_t) -1;
6261 /* Make all global versions with definition. */
6262 for (t = info->version_info; t != NULL; t = t->next)
6263 for (d = t->globals.list; d != NULL; d = d->next)
6264 if (!d->symver && d->literal)
6266 const char *verstr, *name;
6267 size_t namelen, verlen, newlen;
6268 char *newname, *p, leading_char;
6269 struct elf_link_hash_entry *newh;
6271 leading_char = bfd_get_symbol_leading_char (output_bfd);
6273 namelen = strlen (name) + (leading_char != '\0');
6275 verlen = strlen (verstr);
6276 newlen = namelen + verlen + 3;
6278 newname = (char *) bfd_malloc (newlen);
6279 if (newname == NULL)
6281 newname[0] = leading_char;
6282 memcpy (newname + (leading_char != '\0'), name, namelen);
6284 /* Check the hidden versioned definition. */
6285 p = newname + namelen;
6287 memcpy (p, verstr, verlen + 1);
6288 newh = elf_link_hash_lookup (elf_hash_table (info),
6289 newname, FALSE, FALSE,
6292 || (newh->root.type != bfd_link_hash_defined
6293 && newh->root.type != bfd_link_hash_defweak))
6295 /* Check the default versioned definition. */
6297 memcpy (p, verstr, verlen + 1);
6298 newh = elf_link_hash_lookup (elf_hash_table (info),
6299 newname, FALSE, FALSE,
6304 /* Mark this version if there is a definition and it is
6305 not defined in a shared object. */
6307 && !newh->def_dynamic
6308 && (newh->root.type == bfd_link_hash_defined
6309 || newh->root.type == bfd_link_hash_defweak))
6313 /* Attach all the symbols to their version information. */
6314 asvinfo.info = info;
6315 asvinfo.failed = FALSE;
6317 elf_link_hash_traverse (elf_hash_table (info),
6318 _bfd_elf_link_assign_sym_version,
6323 if (!info->allow_undefined_version)
6325 /* Check if all global versions have a definition. */
6326 bfd_boolean all_defined = TRUE;
6327 for (t = info->version_info; t != NULL; t = t->next)
6328 for (d = t->globals.list; d != NULL; d = d->next)
6329 if (d->literal && !d->symver && !d->script)
6332 (_("%s: undefined version: %s"),
6333 d->pattern, t->name);
6334 all_defined = FALSE;
6339 bfd_set_error (bfd_error_bad_value);
6344 /* Set up the version definition section. */
6345 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6346 BFD_ASSERT (s != NULL);
6348 /* We may have created additional version definitions if we are
6349 just linking a regular application. */
6350 verdefs = info->version_info;
6352 /* Skip anonymous version tag. */
6353 if (verdefs != NULL && verdefs->vernum == 0)
6354 verdefs = verdefs->next;
6356 if (verdefs == NULL && !info->create_default_symver)
6357 s->flags |= SEC_EXCLUDE;
6363 Elf_Internal_Verdef def;
6364 Elf_Internal_Verdaux defaux;
6365 struct bfd_link_hash_entry *bh;
6366 struct elf_link_hash_entry *h;
6372 /* Make space for the base version. */
6373 size += sizeof (Elf_External_Verdef);
6374 size += sizeof (Elf_External_Verdaux);
6377 /* Make space for the default version. */
6378 if (info->create_default_symver)
6380 size += sizeof (Elf_External_Verdef);
6384 for (t = verdefs; t != NULL; t = t->next)
6386 struct bfd_elf_version_deps *n;
6388 /* Don't emit base version twice. */
6392 size += sizeof (Elf_External_Verdef);
6393 size += sizeof (Elf_External_Verdaux);
6396 for (n = t->deps; n != NULL; n = n->next)
6397 size += sizeof (Elf_External_Verdaux);
6401 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6402 if (s->contents == NULL && s->size != 0)
6405 /* Fill in the version definition section. */
6409 def.vd_version = VER_DEF_CURRENT;
6410 def.vd_flags = VER_FLG_BASE;
6413 if (info->create_default_symver)
6415 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6416 def.vd_next = sizeof (Elf_External_Verdef);
6420 def.vd_aux = sizeof (Elf_External_Verdef);
6421 def.vd_next = (sizeof (Elf_External_Verdef)
6422 + sizeof (Elf_External_Verdaux));
6425 if (soname_indx != (size_t) -1)
6427 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6429 def.vd_hash = bfd_elf_hash (soname);
6430 defaux.vda_name = soname_indx;
6437 name = lbasename (output_bfd->filename);
6438 def.vd_hash = bfd_elf_hash (name);
6439 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6441 if (indx == (size_t) -1)
6443 defaux.vda_name = indx;
6445 defaux.vda_next = 0;
6447 _bfd_elf_swap_verdef_out (output_bfd, &def,
6448 (Elf_External_Verdef *) p);
6449 p += sizeof (Elf_External_Verdef);
6450 if (info->create_default_symver)
6452 /* Add a symbol representing this version. */
6454 if (! (_bfd_generic_link_add_one_symbol
6455 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6457 get_elf_backend_data (dynobj)->collect, &bh)))
6459 h = (struct elf_link_hash_entry *) bh;
6462 h->type = STT_OBJECT;
6463 h->verinfo.vertree = NULL;
6465 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6468 /* Create a duplicate of the base version with the same
6469 aux block, but different flags. */
6472 def.vd_aux = sizeof (Elf_External_Verdef);
6474 def.vd_next = (sizeof (Elf_External_Verdef)
6475 + sizeof (Elf_External_Verdaux));
6478 _bfd_elf_swap_verdef_out (output_bfd, &def,
6479 (Elf_External_Verdef *) p);
6480 p += sizeof (Elf_External_Verdef);
6482 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6483 (Elf_External_Verdaux *) p);
6484 p += sizeof (Elf_External_Verdaux);
6486 for (t = verdefs; t != NULL; t = t->next)
6489 struct bfd_elf_version_deps *n;
6491 /* Don't emit the base version twice. */
6496 for (n = t->deps; n != NULL; n = n->next)
6499 /* Add a symbol representing this version. */
6501 if (! (_bfd_generic_link_add_one_symbol
6502 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6504 get_elf_backend_data (dynobj)->collect, &bh)))
6506 h = (struct elf_link_hash_entry *) bh;
6509 h->type = STT_OBJECT;
6510 h->verinfo.vertree = t;
6512 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6515 def.vd_version = VER_DEF_CURRENT;
6517 if (t->globals.list == NULL
6518 && t->locals.list == NULL
6520 def.vd_flags |= VER_FLG_WEAK;
6521 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6522 def.vd_cnt = cdeps + 1;
6523 def.vd_hash = bfd_elf_hash (t->name);
6524 def.vd_aux = sizeof (Elf_External_Verdef);
6527 /* If a basever node is next, it *must* be the last node in
6528 the chain, otherwise Verdef construction breaks. */
6529 if (t->next != NULL && t->next->vernum == 0)
6530 BFD_ASSERT (t->next->next == NULL);
6532 if (t->next != NULL && t->next->vernum != 0)
6533 def.vd_next = (sizeof (Elf_External_Verdef)
6534 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6536 _bfd_elf_swap_verdef_out (output_bfd, &def,
6537 (Elf_External_Verdef *) p);
6538 p += sizeof (Elf_External_Verdef);
6540 defaux.vda_name = h->dynstr_index;
6541 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6543 defaux.vda_next = 0;
6544 if (t->deps != NULL)
6545 defaux.vda_next = sizeof (Elf_External_Verdaux);
6546 t->name_indx = defaux.vda_name;
6548 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6549 (Elf_External_Verdaux *) p);
6550 p += sizeof (Elf_External_Verdaux);
6552 for (n = t->deps; n != NULL; n = n->next)
6554 if (n->version_needed == NULL)
6556 /* This can happen if there was an error in the
6558 defaux.vda_name = 0;
6562 defaux.vda_name = n->version_needed->name_indx;
6563 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6566 if (n->next == NULL)
6567 defaux.vda_next = 0;
6569 defaux.vda_next = sizeof (Elf_External_Verdaux);
6571 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6572 (Elf_External_Verdaux *) p);
6573 p += sizeof (Elf_External_Verdaux);
6577 elf_tdata (output_bfd)->cverdefs = cdefs;
6581 bed = get_elf_backend_data (output_bfd);
6583 if (info->gc_sections && bed->can_gc_sections)
6585 struct elf_gc_sweep_symbol_info sweep_info;
6587 /* Remove the symbols that were in the swept sections from the
6588 dynamic symbol table. */
6589 sweep_info.info = info;
6590 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6591 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6595 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6598 struct elf_find_verdep_info sinfo;
6600 /* Work out the size of the version reference section. */
6602 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6603 BFD_ASSERT (s != NULL);
6606 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6607 if (sinfo.vers == 0)
6609 sinfo.failed = FALSE;
6611 elf_link_hash_traverse (elf_hash_table (info),
6612 _bfd_elf_link_find_version_dependencies,
6617 if (elf_tdata (output_bfd)->verref == NULL)
6618 s->flags |= SEC_EXCLUDE;
6621 Elf_Internal_Verneed *vn;
6626 /* Build the version dependency section. */
6629 for (vn = elf_tdata (output_bfd)->verref;
6631 vn = vn->vn_nextref)
6633 Elf_Internal_Vernaux *a;
6635 size += sizeof (Elf_External_Verneed);
6637 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6638 size += sizeof (Elf_External_Vernaux);
6642 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6643 if (s->contents == NULL)
6647 for (vn = elf_tdata (output_bfd)->verref;
6649 vn = vn->vn_nextref)
6652 Elf_Internal_Vernaux *a;
6656 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6659 vn->vn_version = VER_NEED_CURRENT;
6661 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6662 elf_dt_name (vn->vn_bfd) != NULL
6663 ? elf_dt_name (vn->vn_bfd)
6664 : lbasename (vn->vn_bfd->filename),
6666 if (indx == (size_t) -1)
6669 vn->vn_aux = sizeof (Elf_External_Verneed);
6670 if (vn->vn_nextref == NULL)
6673 vn->vn_next = (sizeof (Elf_External_Verneed)
6674 + caux * sizeof (Elf_External_Vernaux));
6676 _bfd_elf_swap_verneed_out (output_bfd, vn,
6677 (Elf_External_Verneed *) p);
6678 p += sizeof (Elf_External_Verneed);
6680 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6682 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6683 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6684 a->vna_nodename, FALSE);
6685 if (indx == (size_t) -1)
6688 if (a->vna_nextptr == NULL)
6691 a->vna_next = sizeof (Elf_External_Vernaux);
6693 _bfd_elf_swap_vernaux_out (output_bfd, a,
6694 (Elf_External_Vernaux *) p);
6695 p += sizeof (Elf_External_Vernaux);
6699 elf_tdata (output_bfd)->cverrefs = crefs;
6703 /* Any syms created from now on start with -1 in
6704 got.refcount/offset and plt.refcount/offset. */
6705 elf_hash_table (info)->init_got_refcount
6706 = elf_hash_table (info)->init_got_offset;
6707 elf_hash_table (info)->init_plt_refcount
6708 = elf_hash_table (info)->init_plt_offset;
6710 if (bfd_link_relocatable (info)
6711 && !_bfd_elf_size_group_sections (info))
6714 /* The backend may have to create some sections regardless of whether
6715 we're dynamic or not. */
6716 if (bed->elf_backend_always_size_sections
6717 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6720 /* Determine any GNU_STACK segment requirements, after the backend
6721 has had a chance to set a default segment size. */
6722 if (info->execstack)
6723 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6724 else if (info->noexecstack)
6725 elf_stack_flags (output_bfd) = PF_R | PF_W;
6729 asection *notesec = NULL;
6732 for (inputobj = info->input_bfds;
6734 inputobj = inputobj->link.next)
6739 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6741 s = inputobj->sections;
6742 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6745 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6748 if (s->flags & SEC_CODE)
6752 else if (bed->default_execstack)
6755 if (notesec || info->stacksize > 0)
6756 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6757 if (notesec && exec && bfd_link_relocatable (info)
6758 && notesec->output_section != bfd_abs_section_ptr)
6759 notesec->output_section->flags |= SEC_CODE;
6762 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6764 struct elf_info_failed eif;
6765 struct elf_link_hash_entry *h;
6769 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6770 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6774 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6776 info->flags |= DF_SYMBOLIC;
6784 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6786 if (indx == (size_t) -1)
6789 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6790 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6794 if (filter_shlib != NULL)
6798 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6799 filter_shlib, TRUE);
6800 if (indx == (size_t) -1
6801 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6805 if (auxiliary_filters != NULL)
6807 const char * const *p;
6809 for (p = auxiliary_filters; *p != NULL; p++)
6813 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6815 if (indx == (size_t) -1
6816 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6825 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6827 if (indx == (size_t) -1
6828 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6832 if (depaudit != NULL)
6836 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6838 if (indx == (size_t) -1
6839 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6846 /* Find all symbols which were defined in a dynamic object and make
6847 the backend pick a reasonable value for them. */
6848 elf_link_hash_traverse (elf_hash_table (info),
6849 _bfd_elf_adjust_dynamic_symbol,
6854 /* Add some entries to the .dynamic section. We fill in some of the
6855 values later, in bfd_elf_final_link, but we must add the entries
6856 now so that we know the final size of the .dynamic section. */
6858 /* If there are initialization and/or finalization functions to
6859 call then add the corresponding DT_INIT/DT_FINI entries. */
6860 h = (info->init_function
6861 ? elf_link_hash_lookup (elf_hash_table (info),
6862 info->init_function, FALSE,
6869 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6872 h = (info->fini_function
6873 ? elf_link_hash_lookup (elf_hash_table (info),
6874 info->fini_function, FALSE,
6881 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6885 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6886 if (s != NULL && s->linker_has_input)
6888 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6889 if (! bfd_link_executable (info))
6894 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6895 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6896 && (o = sub->sections) != NULL
6897 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6898 for (o = sub->sections; o != NULL; o = o->next)
6899 if (elf_section_data (o)->this_hdr.sh_type
6900 == SHT_PREINIT_ARRAY)
6903 (_("%pB: .preinit_array section is not allowed in DSO"),
6908 bfd_set_error (bfd_error_nonrepresentable_section);
6912 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6913 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6916 s = bfd_get_section_by_name (output_bfd, ".init_array");
6917 if (s != NULL && s->linker_has_input)
6919 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6920 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6923 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6924 if (s != NULL && s->linker_has_input)
6926 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6927 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6931 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6932 /* If .dynstr is excluded from the link, we don't want any of
6933 these tags. Strictly, we should be checking each section
6934 individually; This quick check covers for the case where
6935 someone does a /DISCARD/ : { *(*) }. */
6936 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6938 bfd_size_type strsize;
6940 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6941 if ((info->emit_hash
6942 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6943 || (info->emit_gnu_hash
6944 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6945 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6946 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6947 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6948 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6949 bed->s->sizeof_sym))
6954 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6957 /* The backend must work out the sizes of all the other dynamic
6960 && bed->elf_backend_size_dynamic_sections != NULL
6961 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6964 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6966 if (elf_tdata (output_bfd)->cverdefs)
6968 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6970 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6971 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6975 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6977 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6980 else if (info->flags & DF_BIND_NOW)
6982 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6988 if (bfd_link_executable (info))
6989 info->flags_1 &= ~ (DF_1_INITFIRST
6992 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6996 if (elf_tdata (output_bfd)->cverrefs)
6998 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7000 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7001 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7005 if ((elf_tdata (output_bfd)->cverrefs == 0
7006 && elf_tdata (output_bfd)->cverdefs == 0)
7007 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7011 s = bfd_get_linker_section (dynobj, ".gnu.version");
7012 s->flags |= SEC_EXCLUDE;
7018 /* Find the first non-excluded output section. We'll use its
7019 section symbol for some emitted relocs. */
7021 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7025 for (s = output_bfd->sections; s != NULL; s = s->next)
7026 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7027 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7029 elf_hash_table (info)->text_index_section = s;
7034 /* Find two non-excluded output sections, one for code, one for data.
7035 We'll use their section symbols for some emitted relocs. */
7037 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7041 /* Data first, since setting text_index_section changes
7042 _bfd_elf_omit_section_dynsym_default. */
7043 for (s = output_bfd->sections; s != NULL; s = s->next)
7044 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7045 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7047 elf_hash_table (info)->data_index_section = s;
7051 for (s = output_bfd->sections; s != NULL; s = s->next)
7052 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7053 == (SEC_ALLOC | SEC_READONLY))
7054 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7056 elf_hash_table (info)->text_index_section = s;
7060 if (elf_hash_table (info)->text_index_section == NULL)
7061 elf_hash_table (info)->text_index_section
7062 = elf_hash_table (info)->data_index_section;
7066 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7068 const struct elf_backend_data *bed;
7069 unsigned long section_sym_count;
7070 bfd_size_type dynsymcount = 0;
7072 if (!is_elf_hash_table (info->hash))
7075 bed = get_elf_backend_data (output_bfd);
7076 (*bed->elf_backend_init_index_section) (output_bfd, info);
7078 /* Assign dynsym indices. In a shared library we generate a section
7079 symbol for each output section, which come first. Next come all
7080 of the back-end allocated local dynamic syms, followed by the rest
7081 of the global symbols.
7083 This is usually not needed for static binaries, however backends
7084 can request to always do it, e.g. the MIPS backend uses dynamic
7085 symbol counts to lay out GOT, which will be produced in the
7086 presence of GOT relocations even in static binaries (holding fixed
7087 data in that case, to satisfy those relocations). */
7089 if (elf_hash_table (info)->dynamic_sections_created
7090 || bed->always_renumber_dynsyms)
7091 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7092 §ion_sym_count);
7094 if (elf_hash_table (info)->dynamic_sections_created)
7098 unsigned int dtagcount;
7100 dynobj = elf_hash_table (info)->dynobj;
7102 /* Work out the size of the symbol version section. */
7103 s = bfd_get_linker_section (dynobj, ".gnu.version");
7104 BFD_ASSERT (s != NULL);
7105 if ((s->flags & SEC_EXCLUDE) == 0)
7107 s->size = dynsymcount * sizeof (Elf_External_Versym);
7108 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7109 if (s->contents == NULL)
7112 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7116 /* Set the size of the .dynsym and .hash sections. We counted
7117 the number of dynamic symbols in elf_link_add_object_symbols.
7118 We will build the contents of .dynsym and .hash when we build
7119 the final symbol table, because until then we do not know the
7120 correct value to give the symbols. We built the .dynstr
7121 section as we went along in elf_link_add_object_symbols. */
7122 s = elf_hash_table (info)->dynsym;
7123 BFD_ASSERT (s != NULL);
7124 s->size = dynsymcount * bed->s->sizeof_sym;
7126 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7127 if (s->contents == NULL)
7130 /* The first entry in .dynsym is a dummy symbol. Clear all the
7131 section syms, in case we don't output them all. */
7132 ++section_sym_count;
7133 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7135 elf_hash_table (info)->bucketcount = 0;
7137 /* Compute the size of the hashing table. As a side effect this
7138 computes the hash values for all the names we export. */
7139 if (info->emit_hash)
7141 unsigned long int *hashcodes;
7142 struct hash_codes_info hashinf;
7144 unsigned long int nsyms;
7146 size_t hash_entry_size;
7148 /* Compute the hash values for all exported symbols. At the same
7149 time store the values in an array so that we could use them for
7151 amt = dynsymcount * sizeof (unsigned long int);
7152 hashcodes = (unsigned long int *) bfd_malloc (amt);
7153 if (hashcodes == NULL)
7155 hashinf.hashcodes = hashcodes;
7156 hashinf.error = FALSE;
7158 /* Put all hash values in HASHCODES. */
7159 elf_link_hash_traverse (elf_hash_table (info),
7160 elf_collect_hash_codes, &hashinf);
7167 nsyms = hashinf.hashcodes - hashcodes;
7169 = compute_bucket_count (info, hashcodes, nsyms, 0);
7172 if (bucketcount == 0 && nsyms > 0)
7175 elf_hash_table (info)->bucketcount = bucketcount;
7177 s = bfd_get_linker_section (dynobj, ".hash");
7178 BFD_ASSERT (s != NULL);
7179 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7180 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7181 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7182 if (s->contents == NULL)
7185 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7186 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7187 s->contents + hash_entry_size);
7190 if (info->emit_gnu_hash)
7193 unsigned char *contents;
7194 struct collect_gnu_hash_codes cinfo;
7198 memset (&cinfo, 0, sizeof (cinfo));
7200 /* Compute the hash values for all exported symbols. At the same
7201 time store the values in an array so that we could use them for
7203 amt = dynsymcount * 2 * sizeof (unsigned long int);
7204 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7205 if (cinfo.hashcodes == NULL)
7208 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7209 cinfo.min_dynindx = -1;
7210 cinfo.output_bfd = output_bfd;
7213 /* Put all hash values in HASHCODES. */
7214 elf_link_hash_traverse (elf_hash_table (info),
7215 elf_collect_gnu_hash_codes, &cinfo);
7218 free (cinfo.hashcodes);
7223 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7225 if (bucketcount == 0)
7227 free (cinfo.hashcodes);
7231 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7232 BFD_ASSERT (s != NULL);
7234 if (cinfo.nsyms == 0)
7236 /* Empty .gnu.hash section is special. */
7237 BFD_ASSERT (cinfo.min_dynindx == -1);
7238 free (cinfo.hashcodes);
7239 s->size = 5 * 4 + bed->s->arch_size / 8;
7240 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7241 if (contents == NULL)
7243 s->contents = contents;
7244 /* 1 empty bucket. */
7245 bfd_put_32 (output_bfd, 1, contents);
7246 /* SYMIDX above the special symbol 0. */
7247 bfd_put_32 (output_bfd, 1, contents + 4);
7248 /* Just one word for bitmask. */
7249 bfd_put_32 (output_bfd, 1, contents + 8);
7250 /* Only hash fn bloom filter. */
7251 bfd_put_32 (output_bfd, 0, contents + 12);
7252 /* No hashes are valid - empty bitmask. */
7253 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7254 /* No hashes in the only bucket. */
7255 bfd_put_32 (output_bfd, 0,
7256 contents + 16 + bed->s->arch_size / 8);
7260 unsigned long int maskwords, maskbitslog2, x;
7261 BFD_ASSERT (cinfo.min_dynindx != -1);
7265 while ((x >>= 1) != 0)
7267 if (maskbitslog2 < 3)
7269 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7270 maskbitslog2 = maskbitslog2 + 3;
7272 maskbitslog2 = maskbitslog2 + 2;
7273 if (bed->s->arch_size == 64)
7275 if (maskbitslog2 == 5)
7281 cinfo.mask = (1 << cinfo.shift1) - 1;
7282 cinfo.shift2 = maskbitslog2;
7283 cinfo.maskbits = 1 << maskbitslog2;
7284 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7285 amt = bucketcount * sizeof (unsigned long int) * 2;
7286 amt += maskwords * sizeof (bfd_vma);
7287 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7288 if (cinfo.bitmask == NULL)
7290 free (cinfo.hashcodes);
7294 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7295 cinfo.indx = cinfo.counts + bucketcount;
7296 cinfo.symindx = dynsymcount - cinfo.nsyms;
7297 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7299 /* Determine how often each hash bucket is used. */
7300 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7301 for (i = 0; i < cinfo.nsyms; ++i)
7302 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7304 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7305 if (cinfo.counts[i] != 0)
7307 cinfo.indx[i] = cnt;
7308 cnt += cinfo.counts[i];
7310 BFD_ASSERT (cnt == dynsymcount);
7311 cinfo.bucketcount = bucketcount;
7312 cinfo.local_indx = cinfo.min_dynindx;
7314 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7315 s->size += cinfo.maskbits / 8;
7316 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7317 if (contents == NULL)
7319 free (cinfo.bitmask);
7320 free (cinfo.hashcodes);
7324 s->contents = contents;
7325 bfd_put_32 (output_bfd, bucketcount, contents);
7326 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7327 bfd_put_32 (output_bfd, maskwords, contents + 8);
7328 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7329 contents += 16 + cinfo.maskbits / 8;
7331 for (i = 0; i < bucketcount; ++i)
7333 if (cinfo.counts[i] == 0)
7334 bfd_put_32 (output_bfd, 0, contents);
7336 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7340 cinfo.contents = contents;
7342 /* Renumber dynamic symbols, populate .gnu.hash section. */
7343 elf_link_hash_traverse (elf_hash_table (info),
7344 elf_renumber_gnu_hash_syms, &cinfo);
7346 contents = s->contents + 16;
7347 for (i = 0; i < maskwords; ++i)
7349 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7351 contents += bed->s->arch_size / 8;
7354 free (cinfo.bitmask);
7355 free (cinfo.hashcodes);
7359 s = bfd_get_linker_section (dynobj, ".dynstr");
7360 BFD_ASSERT (s != NULL);
7362 elf_finalize_dynstr (output_bfd, info);
7364 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7366 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7367 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7374 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7377 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7380 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7381 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7384 /* Finish SHF_MERGE section merging. */
7387 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7392 if (!is_elf_hash_table (info->hash))
7395 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7396 if ((ibfd->flags & DYNAMIC) == 0
7397 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7398 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7399 == get_elf_backend_data (obfd)->s->elfclass))
7400 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7401 if ((sec->flags & SEC_MERGE) != 0
7402 && !bfd_is_abs_section (sec->output_section))
7404 struct bfd_elf_section_data *secdata;
7406 secdata = elf_section_data (sec);
7407 if (! _bfd_add_merge_section (obfd,
7408 &elf_hash_table (info)->merge_info,
7409 sec, &secdata->sec_info))
7411 else if (secdata->sec_info)
7412 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7415 if (elf_hash_table (info)->merge_info != NULL)
7416 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7417 merge_sections_remove_hook);
7421 /* Create an entry in an ELF linker hash table. */
7423 struct bfd_hash_entry *
7424 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7425 struct bfd_hash_table *table,
7428 /* Allocate the structure if it has not already been allocated by a
7432 entry = (struct bfd_hash_entry *)
7433 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7438 /* Call the allocation method of the superclass. */
7439 entry = _bfd_link_hash_newfunc (entry, table, string);
7442 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7443 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7445 /* Set local fields. */
7448 ret->got = htab->init_got_refcount;
7449 ret->plt = htab->init_plt_refcount;
7450 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7451 - offsetof (struct elf_link_hash_entry, size)));
7452 /* Assume that we have been called by a non-ELF symbol reader.
7453 This flag is then reset by the code which reads an ELF input
7454 file. This ensures that a symbol created by a non-ELF symbol
7455 reader will have the flag set correctly. */
7462 /* Copy data from an indirect symbol to its direct symbol, hiding the
7463 old indirect symbol. Also used for copying flags to a weakdef. */
7466 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7467 struct elf_link_hash_entry *dir,
7468 struct elf_link_hash_entry *ind)
7470 struct elf_link_hash_table *htab;
7472 /* Copy down any references that we may have already seen to the
7473 symbol which just became indirect. */
7475 if (dir->versioned != versioned_hidden)
7476 dir->ref_dynamic |= ind->ref_dynamic;
7477 dir->ref_regular |= ind->ref_regular;
7478 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7479 dir->non_got_ref |= ind->non_got_ref;
7480 dir->needs_plt |= ind->needs_plt;
7481 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7483 if (ind->root.type != bfd_link_hash_indirect)
7486 /* Copy over the global and procedure linkage table refcount entries.
7487 These may have been already set up by a check_relocs routine. */
7488 htab = elf_hash_table (info);
7489 if (ind->got.refcount > htab->init_got_refcount.refcount)
7491 if (dir->got.refcount < 0)
7492 dir->got.refcount = 0;
7493 dir->got.refcount += ind->got.refcount;
7494 ind->got.refcount = htab->init_got_refcount.refcount;
7497 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7499 if (dir->plt.refcount < 0)
7500 dir->plt.refcount = 0;
7501 dir->plt.refcount += ind->plt.refcount;
7502 ind->plt.refcount = htab->init_plt_refcount.refcount;
7505 if (ind->dynindx != -1)
7507 if (dir->dynindx != -1)
7508 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7509 dir->dynindx = ind->dynindx;
7510 dir->dynstr_index = ind->dynstr_index;
7512 ind->dynstr_index = 0;
7517 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7518 struct elf_link_hash_entry *h,
7519 bfd_boolean force_local)
7521 /* STT_GNU_IFUNC symbol must go through PLT. */
7522 if (h->type != STT_GNU_IFUNC)
7524 h->plt = elf_hash_table (info)->init_plt_offset;
7529 h->forced_local = 1;
7530 if (h->dynindx != -1)
7532 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7535 h->dynstr_index = 0;
7540 /* Hide a symbol. */
7543 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7544 struct bfd_link_info *info,
7545 struct bfd_link_hash_entry *h)
7547 if (is_elf_hash_table (info->hash))
7549 const struct elf_backend_data *bed
7550 = get_elf_backend_data (output_bfd);
7551 struct elf_link_hash_entry *eh
7552 = (struct elf_link_hash_entry *) h;
7553 bed->elf_backend_hide_symbol (info, eh, TRUE);
7554 eh->def_dynamic = 0;
7555 eh->ref_dynamic = 0;
7556 eh->dynamic_def = 0;
7560 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7564 _bfd_elf_link_hash_table_init
7565 (struct elf_link_hash_table *table,
7567 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7568 struct bfd_hash_table *,
7570 unsigned int entsize,
7571 enum elf_target_id target_id)
7574 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7576 table->init_got_refcount.refcount = can_refcount - 1;
7577 table->init_plt_refcount.refcount = can_refcount - 1;
7578 table->init_got_offset.offset = -(bfd_vma) 1;
7579 table->init_plt_offset.offset = -(bfd_vma) 1;
7580 /* The first dynamic symbol is a dummy. */
7581 table->dynsymcount = 1;
7583 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7585 table->root.type = bfd_link_elf_hash_table;
7586 table->hash_table_id = target_id;
7591 /* Create an ELF linker hash table. */
7593 struct bfd_link_hash_table *
7594 _bfd_elf_link_hash_table_create (bfd *abfd)
7596 struct elf_link_hash_table *ret;
7597 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7599 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7603 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7604 sizeof (struct elf_link_hash_entry),
7610 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7615 /* Destroy an ELF linker hash table. */
7618 _bfd_elf_link_hash_table_free (bfd *obfd)
7620 struct elf_link_hash_table *htab;
7622 htab = (struct elf_link_hash_table *) obfd->link.hash;
7623 if (htab->dynstr != NULL)
7624 _bfd_elf_strtab_free (htab->dynstr);
7625 _bfd_merge_sections_free (htab->merge_info);
7626 _bfd_generic_link_hash_table_free (obfd);
7629 /* This is a hook for the ELF emulation code in the generic linker to
7630 tell the backend linker what file name to use for the DT_NEEDED
7631 entry for a dynamic object. */
7634 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7636 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7637 && bfd_get_format (abfd) == bfd_object)
7638 elf_dt_name (abfd) = name;
7642 bfd_elf_get_dyn_lib_class (bfd *abfd)
7645 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7646 && bfd_get_format (abfd) == bfd_object)
7647 lib_class = elf_dyn_lib_class (abfd);
7654 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7656 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7657 && bfd_get_format (abfd) == bfd_object)
7658 elf_dyn_lib_class (abfd) = lib_class;
7661 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7662 the linker ELF emulation code. */
7664 struct bfd_link_needed_list *
7665 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7666 struct bfd_link_info *info)
7668 if (! is_elf_hash_table (info->hash))
7670 return elf_hash_table (info)->needed;
7673 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7674 hook for the linker ELF emulation code. */
7676 struct bfd_link_needed_list *
7677 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7678 struct bfd_link_info *info)
7680 if (! is_elf_hash_table (info->hash))
7682 return elf_hash_table (info)->runpath;
7685 /* Get the name actually used for a dynamic object for a link. This
7686 is the SONAME entry if there is one. Otherwise, it is the string
7687 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7690 bfd_elf_get_dt_soname (bfd *abfd)
7692 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7693 && bfd_get_format (abfd) == bfd_object)
7694 return elf_dt_name (abfd);
7698 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7699 the ELF linker emulation code. */
7702 bfd_elf_get_bfd_needed_list (bfd *abfd,
7703 struct bfd_link_needed_list **pneeded)
7706 bfd_byte *dynbuf = NULL;
7707 unsigned int elfsec;
7708 unsigned long shlink;
7709 bfd_byte *extdyn, *extdynend;
7711 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7715 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7716 || bfd_get_format (abfd) != bfd_object)
7719 s = bfd_get_section_by_name (abfd, ".dynamic");
7720 if (s == NULL || s->size == 0)
7723 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7726 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7727 if (elfsec == SHN_BAD)
7730 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7732 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7733 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7736 extdynend = extdyn + s->size;
7737 for (; extdyn < extdynend; extdyn += extdynsize)
7739 Elf_Internal_Dyn dyn;
7741 (*swap_dyn_in) (abfd, extdyn, &dyn);
7743 if (dyn.d_tag == DT_NULL)
7746 if (dyn.d_tag == DT_NEEDED)
7749 struct bfd_link_needed_list *l;
7750 unsigned int tagv = dyn.d_un.d_val;
7753 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7758 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7779 struct elf_symbuf_symbol
7781 unsigned long st_name; /* Symbol name, index in string tbl */
7782 unsigned char st_info; /* Type and binding attributes */
7783 unsigned char st_other; /* Visibilty, and target specific */
7786 struct elf_symbuf_head
7788 struct elf_symbuf_symbol *ssym;
7790 unsigned int st_shndx;
7797 Elf_Internal_Sym *isym;
7798 struct elf_symbuf_symbol *ssym;
7803 /* Sort references to symbols by ascending section number. */
7806 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7808 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7809 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7811 return s1->st_shndx - s2->st_shndx;
7815 elf_sym_name_compare (const void *arg1, const void *arg2)
7817 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7818 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7819 return strcmp (s1->name, s2->name);
7822 static struct elf_symbuf_head *
7823 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7825 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7826 struct elf_symbuf_symbol *ssym;
7827 struct elf_symbuf_head *ssymbuf, *ssymhead;
7828 size_t i, shndx_count, total_size;
7830 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7834 for (ind = indbuf, i = 0; i < symcount; i++)
7835 if (isymbuf[i].st_shndx != SHN_UNDEF)
7836 *ind++ = &isymbuf[i];
7839 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7840 elf_sort_elf_symbol);
7843 if (indbufend > indbuf)
7844 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7845 if (ind[0]->st_shndx != ind[1]->st_shndx)
7848 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7849 + (indbufend - indbuf) * sizeof (*ssym));
7850 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7851 if (ssymbuf == NULL)
7857 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7858 ssymbuf->ssym = NULL;
7859 ssymbuf->count = shndx_count;
7860 ssymbuf->st_shndx = 0;
7861 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7863 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7866 ssymhead->ssym = ssym;
7867 ssymhead->count = 0;
7868 ssymhead->st_shndx = (*ind)->st_shndx;
7870 ssym->st_name = (*ind)->st_name;
7871 ssym->st_info = (*ind)->st_info;
7872 ssym->st_other = (*ind)->st_other;
7875 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7876 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7883 /* Check if 2 sections define the same set of local and global
7887 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7888 struct bfd_link_info *info)
7891 const struct elf_backend_data *bed1, *bed2;
7892 Elf_Internal_Shdr *hdr1, *hdr2;
7893 size_t symcount1, symcount2;
7894 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7895 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7896 Elf_Internal_Sym *isym, *isymend;
7897 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7898 size_t count1, count2, i;
7899 unsigned int shndx1, shndx2;
7905 /* Both sections have to be in ELF. */
7906 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7907 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7910 if (elf_section_type (sec1) != elf_section_type (sec2))
7913 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7914 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7915 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7918 bed1 = get_elf_backend_data (bfd1);
7919 bed2 = get_elf_backend_data (bfd2);
7920 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7921 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7922 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7923 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7925 if (symcount1 == 0 || symcount2 == 0)
7931 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7932 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7934 if (ssymbuf1 == NULL)
7936 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7938 if (isymbuf1 == NULL)
7941 if (!info->reduce_memory_overheads)
7942 elf_tdata (bfd1)->symbuf = ssymbuf1
7943 = elf_create_symbuf (symcount1, isymbuf1);
7946 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7948 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7950 if (isymbuf2 == NULL)
7953 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7954 elf_tdata (bfd2)->symbuf = ssymbuf2
7955 = elf_create_symbuf (symcount2, isymbuf2);
7958 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7960 /* Optimized faster version. */
7962 struct elf_symbol *symp;
7963 struct elf_symbuf_symbol *ssym, *ssymend;
7966 hi = ssymbuf1->count;
7971 mid = (lo + hi) / 2;
7972 if (shndx1 < ssymbuf1[mid].st_shndx)
7974 else if (shndx1 > ssymbuf1[mid].st_shndx)
7978 count1 = ssymbuf1[mid].count;
7985 hi = ssymbuf2->count;
7990 mid = (lo + hi) / 2;
7991 if (shndx2 < ssymbuf2[mid].st_shndx)
7993 else if (shndx2 > ssymbuf2[mid].st_shndx)
7997 count2 = ssymbuf2[mid].count;
8003 if (count1 == 0 || count2 == 0 || count1 != count2)
8007 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8009 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8010 if (symtable1 == NULL || symtable2 == NULL)
8014 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8015 ssym < ssymend; ssym++, symp++)
8017 symp->u.ssym = ssym;
8018 symp->name = bfd_elf_string_from_elf_section (bfd1,
8024 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8025 ssym < ssymend; ssym++, symp++)
8027 symp->u.ssym = ssym;
8028 symp->name = bfd_elf_string_from_elf_section (bfd2,
8033 /* Sort symbol by name. */
8034 qsort (symtable1, count1, sizeof (struct elf_symbol),
8035 elf_sym_name_compare);
8036 qsort (symtable2, count1, sizeof (struct elf_symbol),
8037 elf_sym_name_compare);
8039 for (i = 0; i < count1; i++)
8040 /* Two symbols must have the same binding, type and name. */
8041 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8042 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8043 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8050 symtable1 = (struct elf_symbol *)
8051 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8052 symtable2 = (struct elf_symbol *)
8053 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8054 if (symtable1 == NULL || symtable2 == NULL)
8057 /* Count definitions in the section. */
8059 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8060 if (isym->st_shndx == shndx1)
8061 symtable1[count1++].u.isym = isym;
8064 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8065 if (isym->st_shndx == shndx2)
8066 symtable2[count2++].u.isym = isym;
8068 if (count1 == 0 || count2 == 0 || count1 != count2)
8071 for (i = 0; i < count1; i++)
8073 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8074 symtable1[i].u.isym->st_name);
8076 for (i = 0; i < count2; i++)
8078 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8079 symtable2[i].u.isym->st_name);
8081 /* Sort symbol by name. */
8082 qsort (symtable1, count1, sizeof (struct elf_symbol),
8083 elf_sym_name_compare);
8084 qsort (symtable2, count1, sizeof (struct elf_symbol),
8085 elf_sym_name_compare);
8087 for (i = 0; i < count1; i++)
8088 /* Two symbols must have the same binding, type and name. */
8089 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8090 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8091 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8109 /* Return TRUE if 2 section types are compatible. */
8112 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8113 bfd *bbfd, const asection *bsec)
8117 || abfd->xvec->flavour != bfd_target_elf_flavour
8118 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8121 return elf_section_type (asec) == elf_section_type (bsec);
8124 /* Final phase of ELF linker. */
8126 /* A structure we use to avoid passing large numbers of arguments. */
8128 struct elf_final_link_info
8130 /* General link information. */
8131 struct bfd_link_info *info;
8134 /* Symbol string table. */
8135 struct elf_strtab_hash *symstrtab;
8136 /* .hash section. */
8138 /* symbol version section (.gnu.version). */
8139 asection *symver_sec;
8140 /* Buffer large enough to hold contents of any section. */
8142 /* Buffer large enough to hold external relocs of any section. */
8143 void *external_relocs;
8144 /* Buffer large enough to hold internal relocs of any section. */
8145 Elf_Internal_Rela *internal_relocs;
8146 /* Buffer large enough to hold external local symbols of any input
8148 bfd_byte *external_syms;
8149 /* And a buffer for symbol section indices. */
8150 Elf_External_Sym_Shndx *locsym_shndx;
8151 /* Buffer large enough to hold internal local symbols of any input
8153 Elf_Internal_Sym *internal_syms;
8154 /* Array large enough to hold a symbol index for each local symbol
8155 of any input BFD. */
8157 /* Array large enough to hold a section pointer for each local
8158 symbol of any input BFD. */
8159 asection **sections;
8160 /* Buffer for SHT_SYMTAB_SHNDX section. */
8161 Elf_External_Sym_Shndx *symshndxbuf;
8162 /* Number of STT_FILE syms seen. */
8163 size_t filesym_count;
8166 /* This struct is used to pass information to elf_link_output_extsym. */
8168 struct elf_outext_info
8171 bfd_boolean localsyms;
8172 bfd_boolean file_sym_done;
8173 struct elf_final_link_info *flinfo;
8177 /* Support for evaluating a complex relocation.
8179 Complex relocations are generalized, self-describing relocations. The
8180 implementation of them consists of two parts: complex symbols, and the
8181 relocations themselves.
8183 The relocations are use a reserved elf-wide relocation type code (R_RELC
8184 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8185 information (start bit, end bit, word width, etc) into the addend. This
8186 information is extracted from CGEN-generated operand tables within gas.
8188 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8189 internal) representing prefix-notation expressions, including but not
8190 limited to those sorts of expressions normally encoded as addends in the
8191 addend field. The symbol mangling format is:
8194 | <unary-operator> ':' <node>
8195 | <binary-operator> ':' <node> ':' <node>
8198 <literal> := 's' <digits=N> ':' <N character symbol name>
8199 | 'S' <digits=N> ':' <N character section name>
8203 <binary-operator> := as in C
8204 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8207 set_symbol_value (bfd *bfd_with_globals,
8208 Elf_Internal_Sym *isymbuf,
8213 struct elf_link_hash_entry **sym_hashes;
8214 struct elf_link_hash_entry *h;
8215 size_t extsymoff = locsymcount;
8217 if (symidx < locsymcount)
8219 Elf_Internal_Sym *sym;
8221 sym = isymbuf + symidx;
8222 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8224 /* It is a local symbol: move it to the
8225 "absolute" section and give it a value. */
8226 sym->st_shndx = SHN_ABS;
8227 sym->st_value = val;
8230 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8234 /* It is a global symbol: set its link type
8235 to "defined" and give it a value. */
8237 sym_hashes = elf_sym_hashes (bfd_with_globals);
8238 h = sym_hashes [symidx - extsymoff];
8239 while (h->root.type == bfd_link_hash_indirect
8240 || h->root.type == bfd_link_hash_warning)
8241 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8242 h->root.type = bfd_link_hash_defined;
8243 h->root.u.def.value = val;
8244 h->root.u.def.section = bfd_abs_section_ptr;
8248 resolve_symbol (const char *name,
8250 struct elf_final_link_info *flinfo,
8252 Elf_Internal_Sym *isymbuf,
8255 Elf_Internal_Sym *sym;
8256 struct bfd_link_hash_entry *global_entry;
8257 const char *candidate = NULL;
8258 Elf_Internal_Shdr *symtab_hdr;
8261 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8263 for (i = 0; i < locsymcount; ++ i)
8267 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8270 candidate = bfd_elf_string_from_elf_section (input_bfd,
8271 symtab_hdr->sh_link,
8274 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8275 name, candidate, (unsigned long) sym->st_value);
8277 if (candidate && strcmp (candidate, name) == 0)
8279 asection *sec = flinfo->sections [i];
8281 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8282 *result += sec->output_offset + sec->output_section->vma;
8284 printf ("Found symbol with value %8.8lx\n",
8285 (unsigned long) *result);
8291 /* Hmm, haven't found it yet. perhaps it is a global. */
8292 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8293 FALSE, FALSE, TRUE);
8297 if (global_entry->type == bfd_link_hash_defined
8298 || global_entry->type == bfd_link_hash_defweak)
8300 *result = (global_entry->u.def.value
8301 + global_entry->u.def.section->output_section->vma
8302 + global_entry->u.def.section->output_offset);
8304 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8305 global_entry->root.string, (unsigned long) *result);
8313 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8314 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8315 names like "foo.end" which is the end address of section "foo". */
8318 resolve_section (const char *name,
8326 for (curr = sections; curr; curr = curr->next)
8327 if (strcmp (curr->name, name) == 0)
8329 *result = curr->vma;
8333 /* Hmm. still haven't found it. try pseudo-section names. */
8334 /* FIXME: This could be coded more efficiently... */
8335 for (curr = sections; curr; curr = curr->next)
8337 len = strlen (curr->name);
8338 if (len > strlen (name))
8341 if (strncmp (curr->name, name, len) == 0)
8343 if (strncmp (".end", name + len, 4) == 0)
8345 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8349 /* Insert more pseudo-section names here, if you like. */
8357 undefined_reference (const char *reftype, const char *name)
8359 /* xgettext:c-format */
8360 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8365 eval_symbol (bfd_vma *result,
8368 struct elf_final_link_info *flinfo,
8370 Elf_Internal_Sym *isymbuf,
8379 const char *sym = *symp;
8381 bfd_boolean symbol_is_section = FALSE;
8386 if (len < 1 || len > sizeof (symbuf))
8388 bfd_set_error (bfd_error_invalid_operation);
8401 *result = strtoul (sym, (char **) symp, 16);
8405 symbol_is_section = TRUE;
8409 symlen = strtol (sym, (char **) symp, 10);
8410 sym = *symp + 1; /* Skip the trailing ':'. */
8412 if (symend < sym || symlen + 1 > sizeof (symbuf))
8414 bfd_set_error (bfd_error_invalid_operation);
8418 memcpy (symbuf, sym, symlen);
8419 symbuf[symlen] = '\0';
8420 *symp = sym + symlen;
8422 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8423 the symbol as a section, or vice-versa. so we're pretty liberal in our
8424 interpretation here; section means "try section first", not "must be a
8425 section", and likewise with symbol. */
8427 if (symbol_is_section)
8429 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8430 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8431 isymbuf, locsymcount))
8433 undefined_reference ("section", symbuf);
8439 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8440 isymbuf, locsymcount)
8441 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8444 undefined_reference ("symbol", symbuf);
8451 /* All that remains are operators. */
8453 #define UNARY_OP(op) \
8454 if (strncmp (sym, #op, strlen (#op)) == 0) \
8456 sym += strlen (#op); \
8460 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8461 isymbuf, locsymcount, signed_p)) \
8464 *result = op ((bfd_signed_vma) a); \
8470 #define BINARY_OP(op) \
8471 if (strncmp (sym, #op, strlen (#op)) == 0) \
8473 sym += strlen (#op); \
8477 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8478 isymbuf, locsymcount, signed_p)) \
8481 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8482 isymbuf, locsymcount, signed_p)) \
8485 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8515 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8516 bfd_set_error (bfd_error_invalid_operation);
8522 put_value (bfd_vma size,
8523 unsigned long chunksz,
8528 location += (size - chunksz);
8530 for (; size; size -= chunksz, location -= chunksz)
8535 bfd_put_8 (input_bfd, x, location);
8539 bfd_put_16 (input_bfd, x, location);
8543 bfd_put_32 (input_bfd, x, location);
8544 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8550 bfd_put_64 (input_bfd, x, location);
8551 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8564 get_value (bfd_vma size,
8565 unsigned long chunksz,
8572 /* Sanity checks. */
8573 BFD_ASSERT (chunksz <= sizeof (x)
8576 && (size % chunksz) == 0
8577 && input_bfd != NULL
8578 && location != NULL);
8580 if (chunksz == sizeof (x))
8582 BFD_ASSERT (size == chunksz);
8584 /* Make sure that we do not perform an undefined shift operation.
8585 We know that size == chunksz so there will only be one iteration
8586 of the loop below. */
8590 shift = 8 * chunksz;
8592 for (; size; size -= chunksz, location += chunksz)
8597 x = (x << shift) | bfd_get_8 (input_bfd, location);
8600 x = (x << shift) | bfd_get_16 (input_bfd, location);
8603 x = (x << shift) | bfd_get_32 (input_bfd, location);
8607 x = (x << shift) | bfd_get_64 (input_bfd, location);
8618 decode_complex_addend (unsigned long *start, /* in bits */
8619 unsigned long *oplen, /* in bits */
8620 unsigned long *len, /* in bits */
8621 unsigned long *wordsz, /* in bytes */
8622 unsigned long *chunksz, /* in bytes */
8623 unsigned long *lsb0_p,
8624 unsigned long *signed_p,
8625 unsigned long *trunc_p,
8626 unsigned long encoded)
8628 * start = encoded & 0x3F;
8629 * len = (encoded >> 6) & 0x3F;
8630 * oplen = (encoded >> 12) & 0x3F;
8631 * wordsz = (encoded >> 18) & 0xF;
8632 * chunksz = (encoded >> 22) & 0xF;
8633 * lsb0_p = (encoded >> 27) & 1;
8634 * signed_p = (encoded >> 28) & 1;
8635 * trunc_p = (encoded >> 29) & 1;
8638 bfd_reloc_status_type
8639 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8640 asection *input_section ATTRIBUTE_UNUSED,
8642 Elf_Internal_Rela *rel,
8645 bfd_vma shift, x, mask;
8646 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8647 bfd_reloc_status_type r;
8649 /* Perform this reloc, since it is complex.
8650 (this is not to say that it necessarily refers to a complex
8651 symbol; merely that it is a self-describing CGEN based reloc.
8652 i.e. the addend has the complete reloc information (bit start, end,
8653 word size, etc) encoded within it.). */
8655 decode_complex_addend (&start, &oplen, &len, &wordsz,
8656 &chunksz, &lsb0_p, &signed_p,
8657 &trunc_p, rel->r_addend);
8659 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8662 shift = (start + 1) - len;
8664 shift = (8 * wordsz) - (start + len);
8666 x = get_value (wordsz, chunksz, input_bfd,
8667 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8670 printf ("Doing complex reloc: "
8671 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8672 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8673 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8674 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8675 oplen, (unsigned long) x, (unsigned long) mask,
8676 (unsigned long) relocation);
8681 /* Now do an overflow check. */
8682 r = bfd_check_overflow ((signed_p
8683 ? complain_overflow_signed
8684 : complain_overflow_unsigned),
8685 len, 0, (8 * wordsz),
8689 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8692 printf (" relocation: %8.8lx\n"
8693 " shifted mask: %8.8lx\n"
8694 " shifted/masked reloc: %8.8lx\n"
8695 " result: %8.8lx\n",
8696 (unsigned long) relocation, (unsigned long) (mask << shift),
8697 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8699 put_value (wordsz, chunksz, input_bfd, x,
8700 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8704 /* Functions to read r_offset from external (target order) reloc
8705 entry. Faster than bfd_getl32 et al, because we let the compiler
8706 know the value is aligned. */
8709 ext32l_r_offset (const void *p)
8716 const union aligned32 *a
8717 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8719 uint32_t aval = ( (uint32_t) a->c[0]
8720 | (uint32_t) a->c[1] << 8
8721 | (uint32_t) a->c[2] << 16
8722 | (uint32_t) a->c[3] << 24);
8727 ext32b_r_offset (const void *p)
8734 const union aligned32 *a
8735 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8737 uint32_t aval = ( (uint32_t) a->c[0] << 24
8738 | (uint32_t) a->c[1] << 16
8739 | (uint32_t) a->c[2] << 8
8740 | (uint32_t) a->c[3]);
8744 #ifdef BFD_HOST_64_BIT
8746 ext64l_r_offset (const void *p)
8753 const union aligned64 *a
8754 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8756 uint64_t aval = ( (uint64_t) a->c[0]
8757 | (uint64_t) a->c[1] << 8
8758 | (uint64_t) a->c[2] << 16
8759 | (uint64_t) a->c[3] << 24
8760 | (uint64_t) a->c[4] << 32
8761 | (uint64_t) a->c[5] << 40
8762 | (uint64_t) a->c[6] << 48
8763 | (uint64_t) a->c[7] << 56);
8768 ext64b_r_offset (const void *p)
8775 const union aligned64 *a
8776 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8778 uint64_t aval = ( (uint64_t) a->c[0] << 56
8779 | (uint64_t) a->c[1] << 48
8780 | (uint64_t) a->c[2] << 40
8781 | (uint64_t) a->c[3] << 32
8782 | (uint64_t) a->c[4] << 24
8783 | (uint64_t) a->c[5] << 16
8784 | (uint64_t) a->c[6] << 8
8785 | (uint64_t) a->c[7]);
8790 /* When performing a relocatable link, the input relocations are
8791 preserved. But, if they reference global symbols, the indices
8792 referenced must be updated. Update all the relocations found in
8796 elf_link_adjust_relocs (bfd *abfd,
8798 struct bfd_elf_section_reloc_data *reldata,
8800 struct bfd_link_info *info)
8803 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8805 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8806 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8807 bfd_vma r_type_mask;
8809 unsigned int count = reldata->count;
8810 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8812 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8814 swap_in = bed->s->swap_reloc_in;
8815 swap_out = bed->s->swap_reloc_out;
8817 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8819 swap_in = bed->s->swap_reloca_in;
8820 swap_out = bed->s->swap_reloca_out;
8825 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8828 if (bed->s->arch_size == 32)
8835 r_type_mask = 0xffffffff;
8839 erela = reldata->hdr->contents;
8840 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8842 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8845 if (*rel_hash == NULL)
8848 if ((*rel_hash)->indx == -2
8849 && info->gc_sections
8850 && ! info->gc_keep_exported)
8852 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8853 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8855 (*rel_hash)->root.root.string);
8856 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8858 bfd_set_error (bfd_error_invalid_operation);
8861 BFD_ASSERT ((*rel_hash)->indx >= 0);
8863 (*swap_in) (abfd, erela, irela);
8864 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8865 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8866 | (irela[j].r_info & r_type_mask));
8867 (*swap_out) (abfd, irela, erela);
8870 if (bed->elf_backend_update_relocs)
8871 (*bed->elf_backend_update_relocs) (sec, reldata);
8873 if (sort && count != 0)
8875 bfd_vma (*ext_r_off) (const void *);
8878 bfd_byte *base, *end, *p, *loc;
8879 bfd_byte *buf = NULL;
8881 if (bed->s->arch_size == 32)
8883 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8884 ext_r_off = ext32l_r_offset;
8885 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8886 ext_r_off = ext32b_r_offset;
8892 #ifdef BFD_HOST_64_BIT
8893 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8894 ext_r_off = ext64l_r_offset;
8895 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8896 ext_r_off = ext64b_r_offset;
8902 /* Must use a stable sort here. A modified insertion sort,
8903 since the relocs are mostly sorted already. */
8904 elt_size = reldata->hdr->sh_entsize;
8905 base = reldata->hdr->contents;
8906 end = base + count * elt_size;
8907 if (elt_size > sizeof (Elf64_External_Rela))
8910 /* Ensure the first element is lowest. This acts as a sentinel,
8911 speeding the main loop below. */
8912 r_off = (*ext_r_off) (base);
8913 for (p = loc = base; (p += elt_size) < end; )
8915 bfd_vma r_off2 = (*ext_r_off) (p);
8924 /* Don't just swap *base and *loc as that changes the order
8925 of the original base[0] and base[1] if they happen to
8926 have the same r_offset. */
8927 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8928 memcpy (onebuf, loc, elt_size);
8929 memmove (base + elt_size, base, loc - base);
8930 memcpy (base, onebuf, elt_size);
8933 for (p = base + elt_size; (p += elt_size) < end; )
8935 /* base to p is sorted, *p is next to insert. */
8936 r_off = (*ext_r_off) (p);
8937 /* Search the sorted region for location to insert. */
8939 while (r_off < (*ext_r_off) (loc))
8944 /* Chances are there is a run of relocs to insert here,
8945 from one of more input files. Files are not always
8946 linked in order due to the way elf_link_input_bfd is
8947 called. See pr17666. */
8948 size_t sortlen = p - loc;
8949 bfd_vma r_off2 = (*ext_r_off) (loc);
8950 size_t runlen = elt_size;
8951 size_t buf_size = 96 * 1024;
8952 while (p + runlen < end
8953 && (sortlen <= buf_size
8954 || runlen + elt_size <= buf_size)
8955 && r_off2 > (*ext_r_off) (p + runlen))
8959 buf = bfd_malloc (buf_size);
8963 if (runlen < sortlen)
8965 memcpy (buf, p, runlen);
8966 memmove (loc + runlen, loc, sortlen);
8967 memcpy (loc, buf, runlen);
8971 memcpy (buf, loc, sortlen);
8972 memmove (loc, p, runlen);
8973 memcpy (loc + runlen, buf, sortlen);
8975 p += runlen - elt_size;
8978 /* Hashes are no longer valid. */
8979 free (reldata->hashes);
8980 reldata->hashes = NULL;
8986 struct elf_link_sort_rela
8992 enum elf_reloc_type_class type;
8993 /* We use this as an array of size int_rels_per_ext_rel. */
8994 Elf_Internal_Rela rela[1];
8998 elf_link_sort_cmp1 (const void *A, const void *B)
9000 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9001 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9002 int relativea, relativeb;
9004 relativea = a->type == reloc_class_relative;
9005 relativeb = b->type == reloc_class_relative;
9007 if (relativea < relativeb)
9009 if (relativea > relativeb)
9011 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9013 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9015 if (a->rela->r_offset < b->rela->r_offset)
9017 if (a->rela->r_offset > b->rela->r_offset)
9023 elf_link_sort_cmp2 (const void *A, const void *B)
9025 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9026 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9028 if (a->type < b->type)
9030 if (a->type > b->type)
9032 if (a->u.offset < b->u.offset)
9034 if (a->u.offset > b->u.offset)
9036 if (a->rela->r_offset < b->rela->r_offset)
9038 if (a->rela->r_offset > b->rela->r_offset)
9044 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9046 asection *dynamic_relocs;
9049 bfd_size_type count, size;
9050 size_t i, ret, sort_elt, ext_size;
9051 bfd_byte *sort, *s_non_relative, *p;
9052 struct elf_link_sort_rela *sq;
9053 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9054 int i2e = bed->s->int_rels_per_ext_rel;
9055 unsigned int opb = bfd_octets_per_byte (abfd);
9056 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9057 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9058 struct bfd_link_order *lo;
9060 bfd_boolean use_rela;
9062 /* Find a dynamic reloc section. */
9063 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9064 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9065 if (rela_dyn != NULL && rela_dyn->size > 0
9066 && rel_dyn != NULL && rel_dyn->size > 0)
9068 bfd_boolean use_rela_initialised = FALSE;
9070 /* This is just here to stop gcc from complaining.
9071 Its initialization checking code is not perfect. */
9074 /* Both sections are present. Examine the sizes
9075 of the indirect sections to help us choose. */
9076 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9077 if (lo->type == bfd_indirect_link_order)
9079 asection *o = lo->u.indirect.section;
9081 if ((o->size % bed->s->sizeof_rela) == 0)
9083 if ((o->size % bed->s->sizeof_rel) == 0)
9084 /* Section size is divisible by both rel and rela sizes.
9085 It is of no help to us. */
9089 /* Section size is only divisible by rela. */
9090 if (use_rela_initialised && !use_rela)
9092 _bfd_error_handler (_("%pB: unable to sort relocs - "
9093 "they are in more than one size"),
9095 bfd_set_error (bfd_error_invalid_operation);
9101 use_rela_initialised = TRUE;
9105 else if ((o->size % bed->s->sizeof_rel) == 0)
9107 /* Section size is only divisible by rel. */
9108 if (use_rela_initialised && use_rela)
9110 _bfd_error_handler (_("%pB: unable to sort relocs - "
9111 "they are in more than one size"),
9113 bfd_set_error (bfd_error_invalid_operation);
9119 use_rela_initialised = TRUE;
9124 /* The section size is not divisible by either -
9125 something is wrong. */
9126 _bfd_error_handler (_("%pB: unable to sort relocs - "
9127 "they are of an unknown size"), abfd);
9128 bfd_set_error (bfd_error_invalid_operation);
9133 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9134 if (lo->type == bfd_indirect_link_order)
9136 asection *o = lo->u.indirect.section;
9138 if ((o->size % bed->s->sizeof_rela) == 0)
9140 if ((o->size % bed->s->sizeof_rel) == 0)
9141 /* Section size is divisible by both rel and rela sizes.
9142 It is of no help to us. */
9146 /* Section size is only divisible by rela. */
9147 if (use_rela_initialised && !use_rela)
9149 _bfd_error_handler (_("%pB: unable to sort relocs - "
9150 "they are in more than one size"),
9152 bfd_set_error (bfd_error_invalid_operation);
9158 use_rela_initialised = TRUE;
9162 else if ((o->size % bed->s->sizeof_rel) == 0)
9164 /* Section size is only divisible by rel. */
9165 if (use_rela_initialised && use_rela)
9167 _bfd_error_handler (_("%pB: unable to sort relocs - "
9168 "they are in more than one size"),
9170 bfd_set_error (bfd_error_invalid_operation);
9176 use_rela_initialised = TRUE;
9181 /* The section size is not divisible by either -
9182 something is wrong. */
9183 _bfd_error_handler (_("%pB: unable to sort relocs - "
9184 "they are of an unknown size"), abfd);
9185 bfd_set_error (bfd_error_invalid_operation);
9190 if (! use_rela_initialised)
9194 else if (rela_dyn != NULL && rela_dyn->size > 0)
9196 else if (rel_dyn != NULL && rel_dyn->size > 0)
9203 dynamic_relocs = rela_dyn;
9204 ext_size = bed->s->sizeof_rela;
9205 swap_in = bed->s->swap_reloca_in;
9206 swap_out = bed->s->swap_reloca_out;
9210 dynamic_relocs = rel_dyn;
9211 ext_size = bed->s->sizeof_rel;
9212 swap_in = bed->s->swap_reloc_in;
9213 swap_out = bed->s->swap_reloc_out;
9217 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9218 if (lo->type == bfd_indirect_link_order)
9219 size += lo->u.indirect.section->size;
9221 if (size != dynamic_relocs->size)
9224 sort_elt = (sizeof (struct elf_link_sort_rela)
9225 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9227 count = dynamic_relocs->size / ext_size;
9230 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9234 (*info->callbacks->warning)
9235 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9239 if (bed->s->arch_size == 32)
9240 r_sym_mask = ~(bfd_vma) 0xff;
9242 r_sym_mask = ~(bfd_vma) 0xffffffff;
9244 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9245 if (lo->type == bfd_indirect_link_order)
9247 bfd_byte *erel, *erelend;
9248 asection *o = lo->u.indirect.section;
9250 if (o->contents == NULL && o->size != 0)
9252 /* This is a reloc section that is being handled as a normal
9253 section. See bfd_section_from_shdr. We can't combine
9254 relocs in this case. */
9259 erelend = o->contents + o->size;
9260 p = sort + o->output_offset * opb / ext_size * sort_elt;
9262 while (erel < erelend)
9264 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9266 (*swap_in) (abfd, erel, s->rela);
9267 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9268 s->u.sym_mask = r_sym_mask;
9274 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9276 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9278 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9279 if (s->type != reloc_class_relative)
9285 sq = (struct elf_link_sort_rela *) s_non_relative;
9286 for (; i < count; i++, p += sort_elt)
9288 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9289 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9291 sp->u.offset = sq->rela->r_offset;
9294 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9296 struct elf_link_hash_table *htab = elf_hash_table (info);
9297 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9299 /* We have plt relocs in .rela.dyn. */
9300 sq = (struct elf_link_sort_rela *) sort;
9301 for (i = 0; i < count; i++)
9302 if (sq[count - i - 1].type != reloc_class_plt)
9304 if (i != 0 && htab->srelplt->size == i * ext_size)
9306 struct bfd_link_order **plo;
9307 /* Put srelplt link_order last. This is so the output_offset
9308 set in the next loop is correct for DT_JMPREL. */
9309 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9310 if ((*plo)->type == bfd_indirect_link_order
9311 && (*plo)->u.indirect.section == htab->srelplt)
9317 plo = &(*plo)->next;
9320 dynamic_relocs->map_tail.link_order = lo;
9325 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9326 if (lo->type == bfd_indirect_link_order)
9328 bfd_byte *erel, *erelend;
9329 asection *o = lo->u.indirect.section;
9332 erelend = o->contents + o->size;
9333 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9334 while (erel < erelend)
9336 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9337 (*swap_out) (abfd, s->rela, erel);
9344 *psec = dynamic_relocs;
9348 /* Add a symbol to the output symbol string table. */
9351 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9353 Elf_Internal_Sym *elfsym,
9354 asection *input_sec,
9355 struct elf_link_hash_entry *h)
9357 int (*output_symbol_hook)
9358 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9359 struct elf_link_hash_entry *);
9360 struct elf_link_hash_table *hash_table;
9361 const struct elf_backend_data *bed;
9362 bfd_size_type strtabsize;
9364 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9366 bed = get_elf_backend_data (flinfo->output_bfd);
9367 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9368 if (output_symbol_hook != NULL)
9370 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9377 || (input_sec->flags & SEC_EXCLUDE))
9378 elfsym->st_name = (unsigned long) -1;
9381 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9382 to get the final offset for st_name. */
9384 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9386 if (elfsym->st_name == (unsigned long) -1)
9390 hash_table = elf_hash_table (flinfo->info);
9391 strtabsize = hash_table->strtabsize;
9392 if (strtabsize <= hash_table->strtabcount)
9394 strtabsize += strtabsize;
9395 hash_table->strtabsize = strtabsize;
9396 strtabsize *= sizeof (*hash_table->strtab);
9398 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9400 if (hash_table->strtab == NULL)
9403 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9404 hash_table->strtab[hash_table->strtabcount].dest_index
9405 = hash_table->strtabcount;
9406 hash_table->strtab[hash_table->strtabcount].destshndx_index
9407 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9409 bfd_get_symcount (flinfo->output_bfd) += 1;
9410 hash_table->strtabcount += 1;
9415 /* Swap symbols out to the symbol table and flush the output symbols to
9419 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9421 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9424 const struct elf_backend_data *bed;
9426 Elf_Internal_Shdr *hdr;
9430 if (!hash_table->strtabcount)
9433 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9435 bed = get_elf_backend_data (flinfo->output_bfd);
9437 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9438 symbuf = (bfd_byte *) bfd_malloc (amt);
9442 if (flinfo->symshndxbuf)
9444 amt = sizeof (Elf_External_Sym_Shndx);
9445 amt *= bfd_get_symcount (flinfo->output_bfd);
9446 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9447 if (flinfo->symshndxbuf == NULL)
9454 for (i = 0; i < hash_table->strtabcount; i++)
9456 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9457 if (elfsym->sym.st_name == (unsigned long) -1)
9458 elfsym->sym.st_name = 0;
9461 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9462 elfsym->sym.st_name);
9463 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9464 ((bfd_byte *) symbuf
9465 + (elfsym->dest_index
9466 * bed->s->sizeof_sym)),
9467 (flinfo->symshndxbuf
9468 + elfsym->destshndx_index));
9471 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9472 pos = hdr->sh_offset + hdr->sh_size;
9473 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9474 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9475 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9477 hdr->sh_size += amt;
9485 free (hash_table->strtab);
9486 hash_table->strtab = NULL;
9491 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9494 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9496 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9497 && sym->st_shndx < SHN_LORESERVE)
9499 /* The gABI doesn't support dynamic symbols in output sections
9502 /* xgettext:c-format */
9503 (_("%pB: too many sections: %d (>= %d)"),
9504 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9505 bfd_set_error (bfd_error_nonrepresentable_section);
9511 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9512 allowing an unsatisfied unversioned symbol in the DSO to match a
9513 versioned symbol that would normally require an explicit version.
9514 We also handle the case that a DSO references a hidden symbol
9515 which may be satisfied by a versioned symbol in another DSO. */
9518 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9519 const struct elf_backend_data *bed,
9520 struct elf_link_hash_entry *h)
9523 struct elf_link_loaded_list *loaded;
9525 if (!is_elf_hash_table (info->hash))
9528 /* Check indirect symbol. */
9529 while (h->root.type == bfd_link_hash_indirect)
9530 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9532 switch (h->root.type)
9538 case bfd_link_hash_undefined:
9539 case bfd_link_hash_undefweak:
9540 abfd = h->root.u.undef.abfd;
9542 || (abfd->flags & DYNAMIC) == 0
9543 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9547 case bfd_link_hash_defined:
9548 case bfd_link_hash_defweak:
9549 abfd = h->root.u.def.section->owner;
9552 case bfd_link_hash_common:
9553 abfd = h->root.u.c.p->section->owner;
9556 BFD_ASSERT (abfd != NULL);
9558 for (loaded = elf_hash_table (info)->loaded;
9560 loaded = loaded->next)
9563 Elf_Internal_Shdr *hdr;
9567 Elf_Internal_Shdr *versymhdr;
9568 Elf_Internal_Sym *isym;
9569 Elf_Internal_Sym *isymend;
9570 Elf_Internal_Sym *isymbuf;
9571 Elf_External_Versym *ever;
9572 Elf_External_Versym *extversym;
9574 input = loaded->abfd;
9576 /* We check each DSO for a possible hidden versioned definition. */
9578 || (input->flags & DYNAMIC) == 0
9579 || elf_dynversym (input) == 0)
9582 hdr = &elf_tdata (input)->dynsymtab_hdr;
9584 symcount = hdr->sh_size / bed->s->sizeof_sym;
9585 if (elf_bad_symtab (input))
9587 extsymcount = symcount;
9592 extsymcount = symcount - hdr->sh_info;
9593 extsymoff = hdr->sh_info;
9596 if (extsymcount == 0)
9599 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9601 if (isymbuf == NULL)
9604 /* Read in any version definitions. */
9605 versymhdr = &elf_tdata (input)->dynversym_hdr;
9606 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9607 if (extversym == NULL)
9610 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9611 || (bfd_bread (extversym, versymhdr->sh_size, input)
9612 != versymhdr->sh_size))
9620 ever = extversym + extsymoff;
9621 isymend = isymbuf + extsymcount;
9622 for (isym = isymbuf; isym < isymend; isym++, ever++)
9625 Elf_Internal_Versym iver;
9626 unsigned short version_index;
9628 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9629 || isym->st_shndx == SHN_UNDEF)
9632 name = bfd_elf_string_from_elf_section (input,
9635 if (strcmp (name, h->root.root.string) != 0)
9638 _bfd_elf_swap_versym_in (input, ever, &iver);
9640 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9642 && h->forced_local))
9644 /* If we have a non-hidden versioned sym, then it should
9645 have provided a definition for the undefined sym unless
9646 it is defined in a non-shared object and forced local.
9651 version_index = iver.vs_vers & VERSYM_VERSION;
9652 if (version_index == 1 || version_index == 2)
9654 /* This is the base or first version. We can use it. */
9668 /* Convert ELF common symbol TYPE. */
9671 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9673 /* Commom symbol can only appear in relocatable link. */
9674 if (!bfd_link_relocatable (info))
9676 switch (info->elf_stt_common)
9680 case elf_stt_common:
9683 case no_elf_stt_common:
9690 /* Add an external symbol to the symbol table. This is called from
9691 the hash table traversal routine. When generating a shared object,
9692 we go through the symbol table twice. The first time we output
9693 anything that might have been forced to local scope in a version
9694 script. The second time we output the symbols that are still
9698 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9700 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9701 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9702 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9704 Elf_Internal_Sym sym;
9705 asection *input_sec;
9706 const struct elf_backend_data *bed;
9711 if (h->root.type == bfd_link_hash_warning)
9713 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9714 if (h->root.type == bfd_link_hash_new)
9718 /* Decide whether to output this symbol in this pass. */
9719 if (eoinfo->localsyms)
9721 if (!h->forced_local)
9726 if (h->forced_local)
9730 bed = get_elf_backend_data (flinfo->output_bfd);
9732 if (h->root.type == bfd_link_hash_undefined)
9734 /* If we have an undefined symbol reference here then it must have
9735 come from a shared library that is being linked in. (Undefined
9736 references in regular files have already been handled unless
9737 they are in unreferenced sections which are removed by garbage
9739 bfd_boolean ignore_undef = FALSE;
9741 /* Some symbols may be special in that the fact that they're
9742 undefined can be safely ignored - let backend determine that. */
9743 if (bed->elf_backend_ignore_undef_symbol)
9744 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9746 /* If we are reporting errors for this situation then do so now. */
9749 && (!h->ref_regular || flinfo->info->gc_sections)
9750 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9751 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9752 (*flinfo->info->callbacks->undefined_symbol)
9753 (flinfo->info, h->root.root.string,
9754 h->ref_regular ? NULL : h->root.u.undef.abfd,
9756 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9758 /* Strip a global symbol defined in a discarded section. */
9763 /* We should also warn if a forced local symbol is referenced from
9764 shared libraries. */
9765 if (bfd_link_executable (flinfo->info)
9770 && h->ref_dynamic_nonweak
9771 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9775 struct elf_link_hash_entry *hi = h;
9777 /* Check indirect symbol. */
9778 while (hi->root.type == bfd_link_hash_indirect)
9779 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9781 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9782 /* xgettext:c-format */
9783 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9784 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9785 /* xgettext:c-format */
9786 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9788 /* xgettext:c-format */
9789 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9790 def_bfd = flinfo->output_bfd;
9791 if (hi->root.u.def.section != bfd_abs_section_ptr)
9792 def_bfd = hi->root.u.def.section->owner;
9793 _bfd_error_handler (msg, flinfo->output_bfd,
9794 h->root.root.string, def_bfd);
9795 bfd_set_error (bfd_error_bad_value);
9796 eoinfo->failed = TRUE;
9800 /* We don't want to output symbols that have never been mentioned by
9801 a regular file, or that we have been told to strip. However, if
9802 h->indx is set to -2, the symbol is used by a reloc and we must
9807 else if ((h->def_dynamic
9809 || h->root.type == bfd_link_hash_new)
9813 else if (flinfo->info->strip == strip_all)
9815 else if (flinfo->info->strip == strip_some
9816 && bfd_hash_lookup (flinfo->info->keep_hash,
9817 h->root.root.string, FALSE, FALSE) == NULL)
9819 else if ((h->root.type == bfd_link_hash_defined
9820 || h->root.type == bfd_link_hash_defweak)
9821 && ((flinfo->info->strip_discarded
9822 && discarded_section (h->root.u.def.section))
9823 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9824 && h->root.u.def.section->owner != NULL
9825 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9827 else if ((h->root.type == bfd_link_hash_undefined
9828 || h->root.type == bfd_link_hash_undefweak)
9829 && h->root.u.undef.abfd != NULL
9830 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9835 /* If we're stripping it, and it's not a dynamic symbol, there's
9836 nothing else to do. However, if it is a forced local symbol or
9837 an ifunc symbol we need to give the backend finish_dynamic_symbol
9838 function a chance to make it dynamic. */
9841 && type != STT_GNU_IFUNC
9842 && !h->forced_local)
9846 sym.st_size = h->size;
9847 sym.st_other = h->other;
9848 switch (h->root.type)
9851 case bfd_link_hash_new:
9852 case bfd_link_hash_warning:
9856 case bfd_link_hash_undefined:
9857 case bfd_link_hash_undefweak:
9858 input_sec = bfd_und_section_ptr;
9859 sym.st_shndx = SHN_UNDEF;
9862 case bfd_link_hash_defined:
9863 case bfd_link_hash_defweak:
9865 input_sec = h->root.u.def.section;
9866 if (input_sec->output_section != NULL)
9869 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9870 input_sec->output_section);
9871 if (sym.st_shndx == SHN_BAD)
9874 /* xgettext:c-format */
9875 (_("%pB: could not find output section %pA for input section %pA"),
9876 flinfo->output_bfd, input_sec->output_section, input_sec);
9877 bfd_set_error (bfd_error_nonrepresentable_section);
9878 eoinfo->failed = TRUE;
9882 /* ELF symbols in relocatable files are section relative,
9883 but in nonrelocatable files they are virtual
9885 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9886 if (!bfd_link_relocatable (flinfo->info))
9888 sym.st_value += input_sec->output_section->vma;
9889 if (h->type == STT_TLS)
9891 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9892 if (tls_sec != NULL)
9893 sym.st_value -= tls_sec->vma;
9899 BFD_ASSERT (input_sec->owner == NULL
9900 || (input_sec->owner->flags & DYNAMIC) != 0);
9901 sym.st_shndx = SHN_UNDEF;
9902 input_sec = bfd_und_section_ptr;
9907 case bfd_link_hash_common:
9908 input_sec = h->root.u.c.p->section;
9909 sym.st_shndx = bed->common_section_index (input_sec);
9910 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9913 case bfd_link_hash_indirect:
9914 /* These symbols are created by symbol versioning. They point
9915 to the decorated version of the name. For example, if the
9916 symbol foo@@GNU_1.2 is the default, which should be used when
9917 foo is used with no version, then we add an indirect symbol
9918 foo which points to foo@@GNU_1.2. We ignore these symbols,
9919 since the indirected symbol is already in the hash table. */
9923 if (type == STT_COMMON || type == STT_OBJECT)
9924 switch (h->root.type)
9926 case bfd_link_hash_common:
9927 type = elf_link_convert_common_type (flinfo->info, type);
9929 case bfd_link_hash_defined:
9930 case bfd_link_hash_defweak:
9931 if (bed->common_definition (&sym))
9932 type = elf_link_convert_common_type (flinfo->info, type);
9936 case bfd_link_hash_undefined:
9937 case bfd_link_hash_undefweak:
9943 if (h->forced_local)
9945 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9946 /* Turn off visibility on local symbol. */
9947 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9949 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9950 else if (h->unique_global && h->def_regular)
9951 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9952 else if (h->root.type == bfd_link_hash_undefweak
9953 || h->root.type == bfd_link_hash_defweak)
9954 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9956 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9957 sym.st_target_internal = h->target_internal;
9959 /* Give the processor backend a chance to tweak the symbol value,
9960 and also to finish up anything that needs to be done for this
9961 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9962 forced local syms when non-shared is due to a historical quirk.
9963 STT_GNU_IFUNC symbol must go through PLT. */
9964 if ((h->type == STT_GNU_IFUNC
9966 && !bfd_link_relocatable (flinfo->info))
9967 || ((h->dynindx != -1
9969 && ((bfd_link_pic (flinfo->info)
9970 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9971 || h->root.type != bfd_link_hash_undefweak))
9972 || !h->forced_local)
9973 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9975 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9976 (flinfo->output_bfd, flinfo->info, h, &sym)))
9978 eoinfo->failed = TRUE;
9983 /* If we are marking the symbol as undefined, and there are no
9984 non-weak references to this symbol from a regular object, then
9985 mark the symbol as weak undefined; if there are non-weak
9986 references, mark the symbol as strong. We can't do this earlier,
9987 because it might not be marked as undefined until the
9988 finish_dynamic_symbol routine gets through with it. */
9989 if (sym.st_shndx == SHN_UNDEF
9991 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9992 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9995 type = ELF_ST_TYPE (sym.st_info);
9997 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9998 if (type == STT_GNU_IFUNC)
10001 if (h->ref_regular_nonweak)
10002 bindtype = STB_GLOBAL;
10004 bindtype = STB_WEAK;
10005 sym.st_info = ELF_ST_INFO (bindtype, type);
10008 /* If this is a symbol defined in a dynamic library, don't use the
10009 symbol size from the dynamic library. Relinking an executable
10010 against a new library may introduce gratuitous changes in the
10011 executable's symbols if we keep the size. */
10012 if (sym.st_shndx == SHN_UNDEF
10017 /* If a non-weak symbol with non-default visibility is not defined
10018 locally, it is a fatal error. */
10019 if (!bfd_link_relocatable (flinfo->info)
10020 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10021 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10022 && h->root.type == bfd_link_hash_undefined
10023 && !h->def_regular)
10027 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10028 /* xgettext:c-format */
10029 msg = _("%pB: protected symbol `%s' isn't defined");
10030 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10031 /* xgettext:c-format */
10032 msg = _("%pB: internal symbol `%s' isn't defined");
10034 /* xgettext:c-format */
10035 msg = _("%pB: hidden symbol `%s' isn't defined");
10036 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10037 bfd_set_error (bfd_error_bad_value);
10038 eoinfo->failed = TRUE;
10042 /* If this symbol should be put in the .dynsym section, then put it
10043 there now. We already know the symbol index. We also fill in
10044 the entry in the .hash section. */
10045 if (h->dynindx != -1
10046 && elf_hash_table (flinfo->info)->dynamic_sections_created
10047 && elf_hash_table (flinfo->info)->dynsym != NULL
10048 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10052 /* Since there is no version information in the dynamic string,
10053 if there is no version info in symbol version section, we will
10054 have a run-time problem if not linking executable, referenced
10055 by shared library, or not bound locally. */
10056 if (h->verinfo.verdef == NULL
10057 && (!bfd_link_executable (flinfo->info)
10059 || !h->def_regular))
10061 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10063 if (p && p [1] != '\0')
10066 /* xgettext:c-format */
10067 (_("%pB: no symbol version section for versioned symbol `%s'"),
10068 flinfo->output_bfd, h->root.root.string);
10069 eoinfo->failed = TRUE;
10074 sym.st_name = h->dynstr_index;
10075 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10076 + h->dynindx * bed->s->sizeof_sym);
10077 if (!check_dynsym (flinfo->output_bfd, &sym))
10079 eoinfo->failed = TRUE;
10082 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10084 if (flinfo->hash_sec != NULL)
10086 size_t hash_entry_size;
10087 bfd_byte *bucketpos;
10089 size_t bucketcount;
10092 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10093 bucket = h->u.elf_hash_value % bucketcount;
10096 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10097 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10098 + (bucket + 2) * hash_entry_size);
10099 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10100 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10102 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10103 ((bfd_byte *) flinfo->hash_sec->contents
10104 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10107 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10109 Elf_Internal_Versym iversym;
10110 Elf_External_Versym *eversym;
10112 if (!h->def_regular)
10114 if (h->verinfo.verdef == NULL
10115 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10116 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10117 iversym.vs_vers = 0;
10119 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10123 if (h->verinfo.vertree == NULL)
10124 iversym.vs_vers = 1;
10126 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10127 if (flinfo->info->create_default_symver)
10131 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10132 defined locally. */
10133 if (h->versioned == versioned_hidden && h->def_regular)
10134 iversym.vs_vers |= VERSYM_HIDDEN;
10136 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10137 eversym += h->dynindx;
10138 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10142 /* If the symbol is undefined, and we didn't output it to .dynsym,
10143 strip it from .symtab too. Obviously we can't do this for
10144 relocatable output or when needed for --emit-relocs. */
10145 else if (input_sec == bfd_und_section_ptr
10147 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10148 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10149 && !bfd_link_relocatable (flinfo->info))
10152 /* Also strip others that we couldn't earlier due to dynamic symbol
10156 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10159 /* Output a FILE symbol so that following locals are not associated
10160 with the wrong input file. We need one for forced local symbols
10161 if we've seen more than one FILE symbol or when we have exactly
10162 one FILE symbol but global symbols are present in a file other
10163 than the one with the FILE symbol. We also need one if linker
10164 defined symbols are present. In practice these conditions are
10165 always met, so just emit the FILE symbol unconditionally. */
10166 if (eoinfo->localsyms
10167 && !eoinfo->file_sym_done
10168 && eoinfo->flinfo->filesym_count != 0)
10170 Elf_Internal_Sym fsym;
10172 memset (&fsym, 0, sizeof (fsym));
10173 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10174 fsym.st_shndx = SHN_ABS;
10175 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10176 bfd_und_section_ptr, NULL))
10179 eoinfo->file_sym_done = TRUE;
10182 indx = bfd_get_symcount (flinfo->output_bfd);
10183 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10187 eoinfo->failed = TRUE;
10192 else if (h->indx == -2)
10198 /* Return TRUE if special handling is done for relocs in SEC against
10199 symbols defined in discarded sections. */
10202 elf_section_ignore_discarded_relocs (asection *sec)
10204 const struct elf_backend_data *bed;
10206 switch (sec->sec_info_type)
10208 case SEC_INFO_TYPE_STABS:
10209 case SEC_INFO_TYPE_EH_FRAME:
10210 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10216 bed = get_elf_backend_data (sec->owner);
10217 if (bed->elf_backend_ignore_discarded_relocs != NULL
10218 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10224 /* Return a mask saying how ld should treat relocations in SEC against
10225 symbols defined in discarded sections. If this function returns
10226 COMPLAIN set, ld will issue a warning message. If this function
10227 returns PRETEND set, and the discarded section was link-once and the
10228 same size as the kept link-once section, ld will pretend that the
10229 symbol was actually defined in the kept section. Otherwise ld will
10230 zero the reloc (at least that is the intent, but some cooperation by
10231 the target dependent code is needed, particularly for REL targets). */
10234 _bfd_elf_default_action_discarded (asection *sec)
10236 if (sec->flags & SEC_DEBUGGING)
10239 if (strcmp (".eh_frame", sec->name) == 0)
10242 if (strcmp (".gcc_except_table", sec->name) == 0)
10245 return COMPLAIN | PRETEND;
10248 /* Find a match between a section and a member of a section group. */
10251 match_group_member (asection *sec, asection *group,
10252 struct bfd_link_info *info)
10254 asection *first = elf_next_in_group (group);
10255 asection *s = first;
10259 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10262 s = elf_next_in_group (s);
10270 /* Check if the kept section of a discarded section SEC can be used
10271 to replace it. Return the replacement if it is OK. Otherwise return
10275 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10279 kept = sec->kept_section;
10282 if ((kept->flags & SEC_GROUP) != 0)
10283 kept = match_group_member (sec, kept, info);
10285 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10286 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10288 sec->kept_section = kept;
10293 /* Link an input file into the linker output file. This function
10294 handles all the sections and relocations of the input file at once.
10295 This is so that we only have to read the local symbols once, and
10296 don't have to keep them in memory. */
10299 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10301 int (*relocate_section)
10302 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10303 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10305 Elf_Internal_Shdr *symtab_hdr;
10306 size_t locsymcount;
10308 Elf_Internal_Sym *isymbuf;
10309 Elf_Internal_Sym *isym;
10310 Elf_Internal_Sym *isymend;
10312 asection **ppsection;
10314 const struct elf_backend_data *bed;
10315 struct elf_link_hash_entry **sym_hashes;
10316 bfd_size_type address_size;
10317 bfd_vma r_type_mask;
10319 bfd_boolean have_file_sym = FALSE;
10321 output_bfd = flinfo->output_bfd;
10322 bed = get_elf_backend_data (output_bfd);
10323 relocate_section = bed->elf_backend_relocate_section;
10325 /* If this is a dynamic object, we don't want to do anything here:
10326 we don't want the local symbols, and we don't want the section
10328 if ((input_bfd->flags & DYNAMIC) != 0)
10331 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10332 if (elf_bad_symtab (input_bfd))
10334 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10339 locsymcount = symtab_hdr->sh_info;
10340 extsymoff = symtab_hdr->sh_info;
10343 /* Read the local symbols. */
10344 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10345 if (isymbuf == NULL && locsymcount != 0)
10347 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10348 flinfo->internal_syms,
10349 flinfo->external_syms,
10350 flinfo->locsym_shndx);
10351 if (isymbuf == NULL)
10355 /* Find local symbol sections and adjust values of symbols in
10356 SEC_MERGE sections. Write out those local symbols we know are
10357 going into the output file. */
10358 isymend = isymbuf + locsymcount;
10359 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10361 isym++, pindex++, ppsection++)
10365 Elf_Internal_Sym osym;
10371 if (elf_bad_symtab (input_bfd))
10373 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10380 if (isym->st_shndx == SHN_UNDEF)
10381 isec = bfd_und_section_ptr;
10382 else if (isym->st_shndx == SHN_ABS)
10383 isec = bfd_abs_section_ptr;
10384 else if (isym->st_shndx == SHN_COMMON)
10385 isec = bfd_com_section_ptr;
10388 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10391 /* Don't attempt to output symbols with st_shnx in the
10392 reserved range other than SHN_ABS and SHN_COMMON. */
10396 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10397 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10399 _bfd_merged_section_offset (output_bfd, &isec,
10400 elf_section_data (isec)->sec_info,
10406 /* Don't output the first, undefined, symbol. In fact, don't
10407 output any undefined local symbol. */
10408 if (isec == bfd_und_section_ptr)
10411 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10413 /* We never output section symbols. Instead, we use the
10414 section symbol of the corresponding section in the output
10419 /* If we are stripping all symbols, we don't want to output this
10421 if (flinfo->info->strip == strip_all)
10424 /* If we are discarding all local symbols, we don't want to
10425 output this one. If we are generating a relocatable output
10426 file, then some of the local symbols may be required by
10427 relocs; we output them below as we discover that they are
10429 if (flinfo->info->discard == discard_all)
10432 /* If this symbol is defined in a section which we are
10433 discarding, we don't need to keep it. */
10434 if (isym->st_shndx != SHN_UNDEF
10435 && isym->st_shndx < SHN_LORESERVE
10436 && bfd_section_removed_from_list (output_bfd,
10437 isec->output_section))
10440 /* Get the name of the symbol. */
10441 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10446 /* See if we are discarding symbols with this name. */
10447 if ((flinfo->info->strip == strip_some
10448 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10450 || (((flinfo->info->discard == discard_sec_merge
10451 && (isec->flags & SEC_MERGE)
10452 && !bfd_link_relocatable (flinfo->info))
10453 || flinfo->info->discard == discard_l)
10454 && bfd_is_local_label_name (input_bfd, name)))
10457 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10459 if (input_bfd->lto_output)
10460 /* -flto puts a temp file name here. This means builds
10461 are not reproducible. Discard the symbol. */
10463 have_file_sym = TRUE;
10464 flinfo->filesym_count += 1;
10466 if (!have_file_sym)
10468 /* In the absence of debug info, bfd_find_nearest_line uses
10469 FILE symbols to determine the source file for local
10470 function symbols. Provide a FILE symbol here if input
10471 files lack such, so that their symbols won't be
10472 associated with a previous input file. It's not the
10473 source file, but the best we can do. */
10474 have_file_sym = TRUE;
10475 flinfo->filesym_count += 1;
10476 memset (&osym, 0, sizeof (osym));
10477 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10478 osym.st_shndx = SHN_ABS;
10479 if (!elf_link_output_symstrtab (flinfo,
10480 (input_bfd->lto_output ? NULL
10481 : input_bfd->filename),
10482 &osym, bfd_abs_section_ptr,
10489 /* Adjust the section index for the output file. */
10490 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10491 isec->output_section);
10492 if (osym.st_shndx == SHN_BAD)
10495 /* ELF symbols in relocatable files are section relative, but
10496 in executable files they are virtual addresses. Note that
10497 this code assumes that all ELF sections have an associated
10498 BFD section with a reasonable value for output_offset; below
10499 we assume that they also have a reasonable value for
10500 output_section. Any special sections must be set up to meet
10501 these requirements. */
10502 osym.st_value += isec->output_offset;
10503 if (!bfd_link_relocatable (flinfo->info))
10505 osym.st_value += isec->output_section->vma;
10506 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10508 /* STT_TLS symbols are relative to PT_TLS segment base. */
10509 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10510 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10512 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10517 indx = bfd_get_symcount (output_bfd);
10518 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10525 if (bed->s->arch_size == 32)
10527 r_type_mask = 0xff;
10533 r_type_mask = 0xffffffff;
10538 /* Relocate the contents of each section. */
10539 sym_hashes = elf_sym_hashes (input_bfd);
10540 for (o = input_bfd->sections; o != NULL; o = o->next)
10542 bfd_byte *contents;
10544 if (! o->linker_mark)
10546 /* This section was omitted from the link. */
10550 if (!flinfo->info->resolve_section_groups
10551 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10553 /* Deal with the group signature symbol. */
10554 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10555 unsigned long symndx = sec_data->this_hdr.sh_info;
10556 asection *osec = o->output_section;
10558 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10559 if (symndx >= locsymcount
10560 || (elf_bad_symtab (input_bfd)
10561 && flinfo->sections[symndx] == NULL))
10563 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10564 while (h->root.type == bfd_link_hash_indirect
10565 || h->root.type == bfd_link_hash_warning)
10566 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10567 /* Arrange for symbol to be output. */
10569 elf_section_data (osec)->this_hdr.sh_info = -2;
10571 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10573 /* We'll use the output section target_index. */
10574 asection *sec = flinfo->sections[symndx]->output_section;
10575 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10579 if (flinfo->indices[symndx] == -1)
10581 /* Otherwise output the local symbol now. */
10582 Elf_Internal_Sym sym = isymbuf[symndx];
10583 asection *sec = flinfo->sections[symndx]->output_section;
10588 name = bfd_elf_string_from_elf_section (input_bfd,
10589 symtab_hdr->sh_link,
10594 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10596 if (sym.st_shndx == SHN_BAD)
10599 sym.st_value += o->output_offset;
10601 indx = bfd_get_symcount (output_bfd);
10602 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10607 flinfo->indices[symndx] = indx;
10611 elf_section_data (osec)->this_hdr.sh_info
10612 = flinfo->indices[symndx];
10616 if ((o->flags & SEC_HAS_CONTENTS) == 0
10617 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10620 if ((o->flags & SEC_LINKER_CREATED) != 0)
10622 /* Section was created by _bfd_elf_link_create_dynamic_sections
10627 /* Get the contents of the section. They have been cached by a
10628 relaxation routine. Note that o is a section in an input
10629 file, so the contents field will not have been set by any of
10630 the routines which work on output files. */
10631 if (elf_section_data (o)->this_hdr.contents != NULL)
10633 contents = elf_section_data (o)->this_hdr.contents;
10634 if (bed->caches_rawsize
10636 && o->rawsize < o->size)
10638 memcpy (flinfo->contents, contents, o->rawsize);
10639 contents = flinfo->contents;
10644 contents = flinfo->contents;
10645 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10649 if ((o->flags & SEC_RELOC) != 0)
10651 Elf_Internal_Rela *internal_relocs;
10652 Elf_Internal_Rela *rel, *relend;
10653 int action_discarded;
10656 /* Get the swapped relocs. */
10658 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10659 flinfo->internal_relocs, FALSE);
10660 if (internal_relocs == NULL
10661 && o->reloc_count > 0)
10664 /* We need to reverse-copy input .ctors/.dtors sections if
10665 they are placed in .init_array/.finit_array for output. */
10666 if (o->size > address_size
10667 && ((strncmp (o->name, ".ctors", 6) == 0
10668 && strcmp (o->output_section->name,
10669 ".init_array") == 0)
10670 || (strncmp (o->name, ".dtors", 6) == 0
10671 && strcmp (o->output_section->name,
10672 ".fini_array") == 0))
10673 && (o->name[6] == 0 || o->name[6] == '.'))
10675 if (o->size * bed->s->int_rels_per_ext_rel
10676 != o->reloc_count * address_size)
10679 /* xgettext:c-format */
10680 (_("error: %pB: size of section %pA is not "
10681 "multiple of address size"),
10683 bfd_set_error (bfd_error_bad_value);
10686 o->flags |= SEC_ELF_REVERSE_COPY;
10689 action_discarded = -1;
10690 if (!elf_section_ignore_discarded_relocs (o))
10691 action_discarded = (*bed->action_discarded) (o);
10693 /* Run through the relocs evaluating complex reloc symbols and
10694 looking for relocs against symbols from discarded sections
10695 or section symbols from removed link-once sections.
10696 Complain about relocs against discarded sections. Zero
10697 relocs against removed link-once sections. */
10699 rel = internal_relocs;
10700 relend = rel + o->reloc_count;
10701 for ( ; rel < relend; rel++)
10703 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10704 unsigned int s_type;
10705 asection **ps, *sec;
10706 struct elf_link_hash_entry *h = NULL;
10707 const char *sym_name;
10709 if (r_symndx == STN_UNDEF)
10712 if (r_symndx >= locsymcount
10713 || (elf_bad_symtab (input_bfd)
10714 && flinfo->sections[r_symndx] == NULL))
10716 h = sym_hashes[r_symndx - extsymoff];
10718 /* Badly formatted input files can contain relocs that
10719 reference non-existant symbols. Check here so that
10720 we do not seg fault. */
10724 /* xgettext:c-format */
10725 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10726 "that references a non-existent global symbol"),
10727 input_bfd, (uint64_t) rel->r_info, o);
10728 bfd_set_error (bfd_error_bad_value);
10732 while (h->root.type == bfd_link_hash_indirect
10733 || h->root.type == bfd_link_hash_warning)
10734 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10738 /* If a plugin symbol is referenced from a non-IR file,
10739 mark the symbol as undefined. Note that the
10740 linker may attach linker created dynamic sections
10741 to the plugin bfd. Symbols defined in linker
10742 created sections are not plugin symbols. */
10743 if ((h->root.non_ir_ref_regular
10744 || h->root.non_ir_ref_dynamic)
10745 && (h->root.type == bfd_link_hash_defined
10746 || h->root.type == bfd_link_hash_defweak)
10747 && (h->root.u.def.section->flags
10748 & SEC_LINKER_CREATED) == 0
10749 && h->root.u.def.section->owner != NULL
10750 && (h->root.u.def.section->owner->flags
10751 & BFD_PLUGIN) != 0)
10753 h->root.type = bfd_link_hash_undefined;
10754 h->root.u.undef.abfd = h->root.u.def.section->owner;
10758 if (h->root.type == bfd_link_hash_defined
10759 || h->root.type == bfd_link_hash_defweak)
10760 ps = &h->root.u.def.section;
10762 sym_name = h->root.root.string;
10766 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10768 s_type = ELF_ST_TYPE (sym->st_info);
10769 ps = &flinfo->sections[r_symndx];
10770 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10774 if ((s_type == STT_RELC || s_type == STT_SRELC)
10775 && !bfd_link_relocatable (flinfo->info))
10778 bfd_vma dot = (rel->r_offset
10779 + o->output_offset + o->output_section->vma);
10781 printf ("Encountered a complex symbol!");
10782 printf (" (input_bfd %s, section %s, reloc %ld\n",
10783 input_bfd->filename, o->name,
10784 (long) (rel - internal_relocs));
10785 printf (" symbol: idx %8.8lx, name %s\n",
10786 r_symndx, sym_name);
10787 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10788 (unsigned long) rel->r_info,
10789 (unsigned long) rel->r_offset);
10791 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10792 isymbuf, locsymcount, s_type == STT_SRELC))
10795 /* Symbol evaluated OK. Update to absolute value. */
10796 set_symbol_value (input_bfd, isymbuf, locsymcount,
10801 if (action_discarded != -1 && ps != NULL)
10803 /* Complain if the definition comes from a
10804 discarded section. */
10805 if ((sec = *ps) != NULL && discarded_section (sec))
10807 BFD_ASSERT (r_symndx != STN_UNDEF);
10808 if (action_discarded & COMPLAIN)
10809 (*flinfo->info->callbacks->einfo)
10810 /* xgettext:c-format */
10811 (_("%X`%s' referenced in section `%pA' of %pB: "
10812 "defined in discarded section `%pA' of %pB\n"),
10813 sym_name, o, input_bfd, sec, sec->owner);
10815 /* Try to do the best we can to support buggy old
10816 versions of gcc. Pretend that the symbol is
10817 really defined in the kept linkonce section.
10818 FIXME: This is quite broken. Modifying the
10819 symbol here means we will be changing all later
10820 uses of the symbol, not just in this section. */
10821 if (action_discarded & PRETEND)
10825 kept = _bfd_elf_check_kept_section (sec,
10837 /* Relocate the section by invoking a back end routine.
10839 The back end routine is responsible for adjusting the
10840 section contents as necessary, and (if using Rela relocs
10841 and generating a relocatable output file) adjusting the
10842 reloc addend as necessary.
10844 The back end routine does not have to worry about setting
10845 the reloc address or the reloc symbol index.
10847 The back end routine is given a pointer to the swapped in
10848 internal symbols, and can access the hash table entries
10849 for the external symbols via elf_sym_hashes (input_bfd).
10851 When generating relocatable output, the back end routine
10852 must handle STB_LOCAL/STT_SECTION symbols specially. The
10853 output symbol is going to be a section symbol
10854 corresponding to the output section, which will require
10855 the addend to be adjusted. */
10857 ret = (*relocate_section) (output_bfd, flinfo->info,
10858 input_bfd, o, contents,
10866 || bfd_link_relocatable (flinfo->info)
10867 || flinfo->info->emitrelocations)
10869 Elf_Internal_Rela *irela;
10870 Elf_Internal_Rela *irelaend, *irelamid;
10871 bfd_vma last_offset;
10872 struct elf_link_hash_entry **rel_hash;
10873 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10874 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10875 unsigned int next_erel;
10876 bfd_boolean rela_normal;
10877 struct bfd_elf_section_data *esdi, *esdo;
10879 esdi = elf_section_data (o);
10880 esdo = elf_section_data (o->output_section);
10881 rela_normal = FALSE;
10883 /* Adjust the reloc addresses and symbol indices. */
10885 irela = internal_relocs;
10886 irelaend = irela + o->reloc_count;
10887 rel_hash = esdo->rel.hashes + esdo->rel.count;
10888 /* We start processing the REL relocs, if any. When we reach
10889 IRELAMID in the loop, we switch to the RELA relocs. */
10891 if (esdi->rel.hdr != NULL)
10892 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10893 * bed->s->int_rels_per_ext_rel);
10894 rel_hash_list = rel_hash;
10895 rela_hash_list = NULL;
10896 last_offset = o->output_offset;
10897 if (!bfd_link_relocatable (flinfo->info))
10898 last_offset += o->output_section->vma;
10899 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10901 unsigned long r_symndx;
10903 Elf_Internal_Sym sym;
10905 if (next_erel == bed->s->int_rels_per_ext_rel)
10911 if (irela == irelamid)
10913 rel_hash = esdo->rela.hashes + esdo->rela.count;
10914 rela_hash_list = rel_hash;
10915 rela_normal = bed->rela_normal;
10918 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10921 if (irela->r_offset >= (bfd_vma) -2)
10923 /* This is a reloc for a deleted entry or somesuch.
10924 Turn it into an R_*_NONE reloc, at the same
10925 offset as the last reloc. elf_eh_frame.c and
10926 bfd_elf_discard_info rely on reloc offsets
10928 irela->r_offset = last_offset;
10930 irela->r_addend = 0;
10934 irela->r_offset += o->output_offset;
10936 /* Relocs in an executable have to be virtual addresses. */
10937 if (!bfd_link_relocatable (flinfo->info))
10938 irela->r_offset += o->output_section->vma;
10940 last_offset = irela->r_offset;
10942 r_symndx = irela->r_info >> r_sym_shift;
10943 if (r_symndx == STN_UNDEF)
10946 if (r_symndx >= locsymcount
10947 || (elf_bad_symtab (input_bfd)
10948 && flinfo->sections[r_symndx] == NULL))
10950 struct elf_link_hash_entry *rh;
10951 unsigned long indx;
10953 /* This is a reloc against a global symbol. We
10954 have not yet output all the local symbols, so
10955 we do not know the symbol index of any global
10956 symbol. We set the rel_hash entry for this
10957 reloc to point to the global hash table entry
10958 for this symbol. The symbol index is then
10959 set at the end of bfd_elf_final_link. */
10960 indx = r_symndx - extsymoff;
10961 rh = elf_sym_hashes (input_bfd)[indx];
10962 while (rh->root.type == bfd_link_hash_indirect
10963 || rh->root.type == bfd_link_hash_warning)
10964 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10966 /* Setting the index to -2 tells
10967 elf_link_output_extsym that this symbol is
10968 used by a reloc. */
10969 BFD_ASSERT (rh->indx < 0);
10976 /* This is a reloc against a local symbol. */
10979 sym = isymbuf[r_symndx];
10980 sec = flinfo->sections[r_symndx];
10981 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10983 /* I suppose the backend ought to fill in the
10984 section of any STT_SECTION symbol against a
10985 processor specific section. */
10986 r_symndx = STN_UNDEF;
10987 if (bfd_is_abs_section (sec))
10989 else if (sec == NULL || sec->owner == NULL)
10991 bfd_set_error (bfd_error_bad_value);
10996 asection *osec = sec->output_section;
10998 /* If we have discarded a section, the output
10999 section will be the absolute section. In
11000 case of discarded SEC_MERGE sections, use
11001 the kept section. relocate_section should
11002 have already handled discarded linkonce
11004 if (bfd_is_abs_section (osec)
11005 && sec->kept_section != NULL
11006 && sec->kept_section->output_section != NULL)
11008 osec = sec->kept_section->output_section;
11009 irela->r_addend -= osec->vma;
11012 if (!bfd_is_abs_section (osec))
11014 r_symndx = osec->target_index;
11015 if (r_symndx == STN_UNDEF)
11017 irela->r_addend += osec->vma;
11018 osec = _bfd_nearby_section (output_bfd, osec,
11020 irela->r_addend -= osec->vma;
11021 r_symndx = osec->target_index;
11026 /* Adjust the addend according to where the
11027 section winds up in the output section. */
11029 irela->r_addend += sec->output_offset;
11033 if (flinfo->indices[r_symndx] == -1)
11035 unsigned long shlink;
11040 if (flinfo->info->strip == strip_all)
11042 /* You can't do ld -r -s. */
11043 bfd_set_error (bfd_error_invalid_operation);
11047 /* This symbol was skipped earlier, but
11048 since it is needed by a reloc, we
11049 must output it now. */
11050 shlink = symtab_hdr->sh_link;
11051 name = (bfd_elf_string_from_elf_section
11052 (input_bfd, shlink, sym.st_name));
11056 osec = sec->output_section;
11058 _bfd_elf_section_from_bfd_section (output_bfd,
11060 if (sym.st_shndx == SHN_BAD)
11063 sym.st_value += sec->output_offset;
11064 if (!bfd_link_relocatable (flinfo->info))
11066 sym.st_value += osec->vma;
11067 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11069 struct elf_link_hash_table *htab
11070 = elf_hash_table (flinfo->info);
11072 /* STT_TLS symbols are relative to PT_TLS
11074 if (htab->tls_sec != NULL)
11075 sym.st_value -= htab->tls_sec->vma;
11078 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11083 indx = bfd_get_symcount (output_bfd);
11084 ret = elf_link_output_symstrtab (flinfo, name,
11090 flinfo->indices[r_symndx] = indx;
11095 r_symndx = flinfo->indices[r_symndx];
11098 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11099 | (irela->r_info & r_type_mask));
11102 /* Swap out the relocs. */
11103 input_rel_hdr = esdi->rel.hdr;
11104 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11106 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11111 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11112 * bed->s->int_rels_per_ext_rel);
11113 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11116 input_rela_hdr = esdi->rela.hdr;
11117 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11119 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11128 /* Write out the modified section contents. */
11129 if (bed->elf_backend_write_section
11130 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11133 /* Section written out. */
11135 else switch (o->sec_info_type)
11137 case SEC_INFO_TYPE_STABS:
11138 if (! (_bfd_write_section_stabs
11140 &elf_hash_table (flinfo->info)->stab_info,
11141 o, &elf_section_data (o)->sec_info, contents)))
11144 case SEC_INFO_TYPE_MERGE:
11145 if (! _bfd_write_merged_section (output_bfd, o,
11146 elf_section_data (o)->sec_info))
11149 case SEC_INFO_TYPE_EH_FRAME:
11151 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11156 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11158 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11166 if (! (o->flags & SEC_EXCLUDE))
11168 file_ptr offset = (file_ptr) o->output_offset;
11169 bfd_size_type todo = o->size;
11171 offset *= bfd_octets_per_byte (output_bfd);
11173 if ((o->flags & SEC_ELF_REVERSE_COPY))
11175 /* Reverse-copy input section to output. */
11178 todo -= address_size;
11179 if (! bfd_set_section_contents (output_bfd,
11187 offset += address_size;
11191 else if (! bfd_set_section_contents (output_bfd,
11205 /* Generate a reloc when linking an ELF file. This is a reloc
11206 requested by the linker, and does not come from any input file. This
11207 is used to build constructor and destructor tables when linking
11211 elf_reloc_link_order (bfd *output_bfd,
11212 struct bfd_link_info *info,
11213 asection *output_section,
11214 struct bfd_link_order *link_order)
11216 reloc_howto_type *howto;
11220 struct bfd_elf_section_reloc_data *reldata;
11221 struct elf_link_hash_entry **rel_hash_ptr;
11222 Elf_Internal_Shdr *rel_hdr;
11223 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11224 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11227 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11229 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11232 bfd_set_error (bfd_error_bad_value);
11236 addend = link_order->u.reloc.p->addend;
11239 reldata = &esdo->rel;
11240 else if (esdo->rela.hdr)
11241 reldata = &esdo->rela;
11248 /* Figure out the symbol index. */
11249 rel_hash_ptr = reldata->hashes + reldata->count;
11250 if (link_order->type == bfd_section_reloc_link_order)
11252 indx = link_order->u.reloc.p->u.section->target_index;
11253 BFD_ASSERT (indx != 0);
11254 *rel_hash_ptr = NULL;
11258 struct elf_link_hash_entry *h;
11260 /* Treat a reloc against a defined symbol as though it were
11261 actually against the section. */
11262 h = ((struct elf_link_hash_entry *)
11263 bfd_wrapped_link_hash_lookup (output_bfd, info,
11264 link_order->u.reloc.p->u.name,
11265 FALSE, FALSE, TRUE));
11267 && (h->root.type == bfd_link_hash_defined
11268 || h->root.type == bfd_link_hash_defweak))
11272 section = h->root.u.def.section;
11273 indx = section->output_section->target_index;
11274 *rel_hash_ptr = NULL;
11275 /* It seems that we ought to add the symbol value to the
11276 addend here, but in practice it has already been added
11277 because it was passed to constructor_callback. */
11278 addend += section->output_section->vma + section->output_offset;
11280 else if (h != NULL)
11282 /* Setting the index to -2 tells elf_link_output_extsym that
11283 this symbol is used by a reloc. */
11290 (*info->callbacks->unattached_reloc)
11291 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11296 /* If this is an inplace reloc, we must write the addend into the
11298 if (howto->partial_inplace && addend != 0)
11300 bfd_size_type size;
11301 bfd_reloc_status_type rstat;
11304 const char *sym_name;
11306 size = (bfd_size_type) bfd_get_reloc_size (howto);
11307 buf = (bfd_byte *) bfd_zmalloc (size);
11308 if (buf == NULL && size != 0)
11310 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11317 case bfd_reloc_outofrange:
11320 case bfd_reloc_overflow:
11321 if (link_order->type == bfd_section_reloc_link_order)
11322 sym_name = bfd_section_name (output_bfd,
11323 link_order->u.reloc.p->u.section);
11325 sym_name = link_order->u.reloc.p->u.name;
11326 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11327 howto->name, addend, NULL, NULL,
11332 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11334 * bfd_octets_per_byte (output_bfd),
11341 /* The address of a reloc is relative to the section in a
11342 relocatable file, and is a virtual address in an executable
11344 offset = link_order->offset;
11345 if (! bfd_link_relocatable (info))
11346 offset += output_section->vma;
11348 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11350 irel[i].r_offset = offset;
11351 irel[i].r_info = 0;
11352 irel[i].r_addend = 0;
11354 if (bed->s->arch_size == 32)
11355 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11357 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11359 rel_hdr = reldata->hdr;
11360 erel = rel_hdr->contents;
11361 if (rel_hdr->sh_type == SHT_REL)
11363 erel += reldata->count * bed->s->sizeof_rel;
11364 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11368 irel[0].r_addend = addend;
11369 erel += reldata->count * bed->s->sizeof_rela;
11370 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11379 /* Get the output vma of the section pointed to by the sh_link field. */
11382 elf_get_linked_section_vma (struct bfd_link_order *p)
11384 Elf_Internal_Shdr **elf_shdrp;
11388 s = p->u.indirect.section;
11389 elf_shdrp = elf_elfsections (s->owner);
11390 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11391 elfsec = elf_shdrp[elfsec]->sh_link;
11393 The Intel C compiler generates SHT_IA_64_UNWIND with
11394 SHF_LINK_ORDER. But it doesn't set the sh_link or
11395 sh_info fields. Hence we could get the situation
11396 where elfsec is 0. */
11399 const struct elf_backend_data *bed
11400 = get_elf_backend_data (s->owner);
11401 if (bed->link_order_error_handler)
11402 bed->link_order_error_handler
11403 /* xgettext:c-format */
11404 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11409 s = elf_shdrp[elfsec]->bfd_section;
11410 return s->output_section->vma + s->output_offset;
11415 /* Compare two sections based on the locations of the sections they are
11416 linked to. Used by elf_fixup_link_order. */
11419 compare_link_order (const void * a, const void * b)
11424 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11425 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11428 return apos > bpos;
11432 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11433 order as their linked sections. Returns false if this could not be done
11434 because an output section includes both ordered and unordered
11435 sections. Ideally we'd do this in the linker proper. */
11438 elf_fixup_link_order (bfd *abfd, asection *o)
11440 int seen_linkorder;
11443 struct bfd_link_order *p;
11445 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11447 struct bfd_link_order **sections;
11448 asection *s, *other_sec, *linkorder_sec;
11452 linkorder_sec = NULL;
11454 seen_linkorder = 0;
11455 for (p = o->map_head.link_order; p != NULL; p = p->next)
11457 if (p->type == bfd_indirect_link_order)
11459 s = p->u.indirect.section;
11461 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11462 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11463 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11464 && elfsec < elf_numsections (sub)
11465 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11466 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11480 if (seen_other && seen_linkorder)
11482 if (other_sec && linkorder_sec)
11484 /* xgettext:c-format */
11485 (_("%pA has both ordered [`%pA' in %pB] "
11486 "and unordered [`%pA' in %pB] sections"),
11487 o, linkorder_sec, linkorder_sec->owner,
11488 other_sec, other_sec->owner);
11491 (_("%pA has both ordered and unordered sections"), o);
11492 bfd_set_error (bfd_error_bad_value);
11497 if (!seen_linkorder)
11500 sections = (struct bfd_link_order **)
11501 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11502 if (sections == NULL)
11504 seen_linkorder = 0;
11506 for (p = o->map_head.link_order; p != NULL; p = p->next)
11508 sections[seen_linkorder++] = p;
11510 /* Sort the input sections in the order of their linked section. */
11511 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11512 compare_link_order);
11514 /* Change the offsets of the sections. */
11516 for (n = 0; n < seen_linkorder; n++)
11518 s = sections[n]->u.indirect.section;
11519 offset &= ~(bfd_vma) 0 << s->alignment_power;
11520 s->output_offset = offset / bfd_octets_per_byte (abfd);
11521 sections[n]->offset = offset;
11522 offset += sections[n]->size;
11529 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11530 Returns TRUE upon success, FALSE otherwise. */
11533 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11535 bfd_boolean ret = FALSE;
11537 const struct elf_backend_data *bed;
11539 enum bfd_architecture arch;
11541 asymbol **sympp = NULL;
11545 elf_symbol_type *osymbuf;
11547 implib_bfd = info->out_implib_bfd;
11548 bed = get_elf_backend_data (abfd);
11550 if (!bfd_set_format (implib_bfd, bfd_object))
11553 /* Use flag from executable but make it a relocatable object. */
11554 flags = bfd_get_file_flags (abfd);
11555 flags &= ~HAS_RELOC;
11556 if (!bfd_set_start_address (implib_bfd, 0)
11557 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11560 /* Copy architecture of output file to import library file. */
11561 arch = bfd_get_arch (abfd);
11562 mach = bfd_get_mach (abfd);
11563 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11564 && (abfd->target_defaulted
11565 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11568 /* Get symbol table size. */
11569 symsize = bfd_get_symtab_upper_bound (abfd);
11573 /* Read in the symbol table. */
11574 sympp = (asymbol **) xmalloc (symsize);
11575 symcount = bfd_canonicalize_symtab (abfd, sympp);
11579 /* Allow the BFD backend to copy any private header data it
11580 understands from the output BFD to the import library BFD. */
11581 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11584 /* Filter symbols to appear in the import library. */
11585 if (bed->elf_backend_filter_implib_symbols)
11586 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11589 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11592 bfd_set_error (bfd_error_no_symbols);
11593 _bfd_error_handler (_("%pB: no symbol found for import library"),
11599 /* Make symbols absolute. */
11600 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11601 sizeof (*osymbuf));
11602 for (src_count = 0; src_count < symcount; src_count++)
11604 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11605 sizeof (*osymbuf));
11606 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11607 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11608 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11609 osymbuf[src_count].internal_elf_sym.st_value =
11610 osymbuf[src_count].symbol.value;
11611 sympp[src_count] = &osymbuf[src_count].symbol;
11614 bfd_set_symtab (implib_bfd, sympp, symcount);
11616 /* Allow the BFD backend to copy any private data it understands
11617 from the output BFD to the import library BFD. This is done last
11618 to permit the routine to look at the filtered symbol table. */
11619 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11622 if (!bfd_close (implib_bfd))
11633 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11637 if (flinfo->symstrtab != NULL)
11638 _bfd_elf_strtab_free (flinfo->symstrtab);
11639 if (flinfo->contents != NULL)
11640 free (flinfo->contents);
11641 if (flinfo->external_relocs != NULL)
11642 free (flinfo->external_relocs);
11643 if (flinfo->internal_relocs != NULL)
11644 free (flinfo->internal_relocs);
11645 if (flinfo->external_syms != NULL)
11646 free (flinfo->external_syms);
11647 if (flinfo->locsym_shndx != NULL)
11648 free (flinfo->locsym_shndx);
11649 if (flinfo->internal_syms != NULL)
11650 free (flinfo->internal_syms);
11651 if (flinfo->indices != NULL)
11652 free (flinfo->indices);
11653 if (flinfo->sections != NULL)
11654 free (flinfo->sections);
11655 if (flinfo->symshndxbuf != NULL)
11656 free (flinfo->symshndxbuf);
11657 for (o = obfd->sections; o != NULL; o = o->next)
11659 struct bfd_elf_section_data *esdo = elf_section_data (o);
11660 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11661 free (esdo->rel.hashes);
11662 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11663 free (esdo->rela.hashes);
11667 /* Do the final step of an ELF link. */
11670 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11672 bfd_boolean dynamic;
11673 bfd_boolean emit_relocs;
11675 struct elf_final_link_info flinfo;
11677 struct bfd_link_order *p;
11679 bfd_size_type max_contents_size;
11680 bfd_size_type max_external_reloc_size;
11681 bfd_size_type max_internal_reloc_count;
11682 bfd_size_type max_sym_count;
11683 bfd_size_type max_sym_shndx_count;
11684 Elf_Internal_Sym elfsym;
11686 Elf_Internal_Shdr *symtab_hdr;
11687 Elf_Internal_Shdr *symtab_shndx_hdr;
11688 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11689 struct elf_outext_info eoinfo;
11690 bfd_boolean merged;
11691 size_t relativecount = 0;
11692 asection *reldyn = 0;
11694 asection *attr_section = NULL;
11695 bfd_vma attr_size = 0;
11696 const char *std_attrs_section;
11697 struct elf_link_hash_table *htab = elf_hash_table (info);
11699 if (!is_elf_hash_table (htab))
11702 if (bfd_link_pic (info))
11703 abfd->flags |= DYNAMIC;
11705 dynamic = htab->dynamic_sections_created;
11706 dynobj = htab->dynobj;
11708 emit_relocs = (bfd_link_relocatable (info)
11709 || info->emitrelocations);
11711 flinfo.info = info;
11712 flinfo.output_bfd = abfd;
11713 flinfo.symstrtab = _bfd_elf_strtab_init ();
11714 if (flinfo.symstrtab == NULL)
11719 flinfo.hash_sec = NULL;
11720 flinfo.symver_sec = NULL;
11724 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11725 /* Note that dynsym_sec can be NULL (on VMS). */
11726 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11727 /* Note that it is OK if symver_sec is NULL. */
11730 flinfo.contents = NULL;
11731 flinfo.external_relocs = NULL;
11732 flinfo.internal_relocs = NULL;
11733 flinfo.external_syms = NULL;
11734 flinfo.locsym_shndx = NULL;
11735 flinfo.internal_syms = NULL;
11736 flinfo.indices = NULL;
11737 flinfo.sections = NULL;
11738 flinfo.symshndxbuf = NULL;
11739 flinfo.filesym_count = 0;
11741 /* The object attributes have been merged. Remove the input
11742 sections from the link, and set the contents of the output
11744 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11745 for (o = abfd->sections; o != NULL; o = o->next)
11747 bfd_boolean remove_section = FALSE;
11749 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11750 || strcmp (o->name, ".gnu.attributes") == 0)
11752 for (p = o->map_head.link_order; p != NULL; p = p->next)
11754 asection *input_section;
11756 if (p->type != bfd_indirect_link_order)
11758 input_section = p->u.indirect.section;
11759 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11760 elf_link_input_bfd ignores this section. */
11761 input_section->flags &= ~SEC_HAS_CONTENTS;
11764 attr_size = bfd_elf_obj_attr_size (abfd);
11765 bfd_set_section_size (abfd, o, attr_size);
11766 /* Skip this section later on. */
11767 o->map_head.link_order = NULL;
11771 remove_section = TRUE;
11773 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11775 /* Remove empty group section from linker output. */
11776 remove_section = TRUE;
11778 if (remove_section)
11780 o->flags |= SEC_EXCLUDE;
11781 bfd_section_list_remove (abfd, o);
11782 abfd->section_count--;
11786 /* Count up the number of relocations we will output for each output
11787 section, so that we know the sizes of the reloc sections. We
11788 also figure out some maximum sizes. */
11789 max_contents_size = 0;
11790 max_external_reloc_size = 0;
11791 max_internal_reloc_count = 0;
11793 max_sym_shndx_count = 0;
11795 for (o = abfd->sections; o != NULL; o = o->next)
11797 struct bfd_elf_section_data *esdo = elf_section_data (o);
11798 o->reloc_count = 0;
11800 for (p = o->map_head.link_order; p != NULL; p = p->next)
11802 unsigned int reloc_count = 0;
11803 unsigned int additional_reloc_count = 0;
11804 struct bfd_elf_section_data *esdi = NULL;
11806 if (p->type == bfd_section_reloc_link_order
11807 || p->type == bfd_symbol_reloc_link_order)
11809 else if (p->type == bfd_indirect_link_order)
11813 sec = p->u.indirect.section;
11815 /* Mark all sections which are to be included in the
11816 link. This will normally be every section. We need
11817 to do this so that we can identify any sections which
11818 the linker has decided to not include. */
11819 sec->linker_mark = TRUE;
11821 if (sec->flags & SEC_MERGE)
11824 if (sec->rawsize > max_contents_size)
11825 max_contents_size = sec->rawsize;
11826 if (sec->size > max_contents_size)
11827 max_contents_size = sec->size;
11829 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11830 && (sec->owner->flags & DYNAMIC) == 0)
11834 /* We are interested in just local symbols, not all
11836 if (elf_bad_symtab (sec->owner))
11837 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11838 / bed->s->sizeof_sym);
11840 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11842 if (sym_count > max_sym_count)
11843 max_sym_count = sym_count;
11845 if (sym_count > max_sym_shndx_count
11846 && elf_symtab_shndx_list (sec->owner) != NULL)
11847 max_sym_shndx_count = sym_count;
11849 if (esdo->this_hdr.sh_type == SHT_REL
11850 || esdo->this_hdr.sh_type == SHT_RELA)
11851 /* Some backends use reloc_count in relocation sections
11852 to count particular types of relocs. Of course,
11853 reloc sections themselves can't have relocations. */
11855 else if (emit_relocs)
11857 reloc_count = sec->reloc_count;
11858 if (bed->elf_backend_count_additional_relocs)
11861 c = (*bed->elf_backend_count_additional_relocs) (sec);
11862 additional_reloc_count += c;
11865 else if (bed->elf_backend_count_relocs)
11866 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11868 esdi = elf_section_data (sec);
11870 if ((sec->flags & SEC_RELOC) != 0)
11872 size_t ext_size = 0;
11874 if (esdi->rel.hdr != NULL)
11875 ext_size = esdi->rel.hdr->sh_size;
11876 if (esdi->rela.hdr != NULL)
11877 ext_size += esdi->rela.hdr->sh_size;
11879 if (ext_size > max_external_reloc_size)
11880 max_external_reloc_size = ext_size;
11881 if (sec->reloc_count > max_internal_reloc_count)
11882 max_internal_reloc_count = sec->reloc_count;
11887 if (reloc_count == 0)
11890 reloc_count += additional_reloc_count;
11891 o->reloc_count += reloc_count;
11893 if (p->type == bfd_indirect_link_order && emit_relocs)
11897 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11898 esdo->rel.count += additional_reloc_count;
11900 if (esdi->rela.hdr)
11902 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11903 esdo->rela.count += additional_reloc_count;
11909 esdo->rela.count += reloc_count;
11911 esdo->rel.count += reloc_count;
11915 if (o->reloc_count > 0)
11916 o->flags |= SEC_RELOC;
11919 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11920 set it (this is probably a bug) and if it is set
11921 assign_section_numbers will create a reloc section. */
11922 o->flags &=~ SEC_RELOC;
11925 /* If the SEC_ALLOC flag is not set, force the section VMA to
11926 zero. This is done in elf_fake_sections as well, but forcing
11927 the VMA to 0 here will ensure that relocs against these
11928 sections are handled correctly. */
11929 if ((o->flags & SEC_ALLOC) == 0
11930 && ! o->user_set_vma)
11934 if (! bfd_link_relocatable (info) && merged)
11935 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11937 /* Figure out the file positions for everything but the symbol table
11938 and the relocs. We set symcount to force assign_section_numbers
11939 to create a symbol table. */
11940 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11941 BFD_ASSERT (! abfd->output_has_begun);
11942 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11945 /* Set sizes, and assign file positions for reloc sections. */
11946 for (o = abfd->sections; o != NULL; o = o->next)
11948 struct bfd_elf_section_data *esdo = elf_section_data (o);
11949 if ((o->flags & SEC_RELOC) != 0)
11952 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11956 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11960 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11961 to count upwards while actually outputting the relocations. */
11962 esdo->rel.count = 0;
11963 esdo->rela.count = 0;
11965 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11967 /* Cache the section contents so that they can be compressed
11968 later. Use bfd_malloc since it will be freed by
11969 bfd_compress_section_contents. */
11970 unsigned char *contents = esdo->this_hdr.contents;
11971 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11974 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11975 if (contents == NULL)
11977 esdo->this_hdr.contents = contents;
11981 /* We have now assigned file positions for all the sections except
11982 .symtab, .strtab, and non-loaded reloc sections. We start the
11983 .symtab section at the current file position, and write directly
11984 to it. We build the .strtab section in memory. */
11985 bfd_get_symcount (abfd) = 0;
11986 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11987 /* sh_name is set in prep_headers. */
11988 symtab_hdr->sh_type = SHT_SYMTAB;
11989 /* sh_flags, sh_addr and sh_size all start off zero. */
11990 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11991 /* sh_link is set in assign_section_numbers. */
11992 /* sh_info is set below. */
11993 /* sh_offset is set just below. */
11994 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11996 if (max_sym_count < 20)
11997 max_sym_count = 20;
11998 htab->strtabsize = max_sym_count;
11999 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12000 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12001 if (htab->strtab == NULL)
12003 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12005 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12006 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12008 if (info->strip != strip_all || emit_relocs)
12010 file_ptr off = elf_next_file_pos (abfd);
12012 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12014 /* Note that at this point elf_next_file_pos (abfd) is
12015 incorrect. We do not yet know the size of the .symtab section.
12016 We correct next_file_pos below, after we do know the size. */
12018 /* Start writing out the symbol table. The first symbol is always a
12020 elfsym.st_value = 0;
12021 elfsym.st_size = 0;
12022 elfsym.st_info = 0;
12023 elfsym.st_other = 0;
12024 elfsym.st_shndx = SHN_UNDEF;
12025 elfsym.st_target_internal = 0;
12026 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12027 bfd_und_section_ptr, NULL) != 1)
12030 /* Output a symbol for each section. We output these even if we are
12031 discarding local symbols, since they are used for relocs. These
12032 symbols have no names. We store the index of each one in the
12033 index field of the section, so that we can find it again when
12034 outputting relocs. */
12036 elfsym.st_size = 0;
12037 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12038 elfsym.st_other = 0;
12039 elfsym.st_value = 0;
12040 elfsym.st_target_internal = 0;
12041 for (i = 1; i < elf_numsections (abfd); i++)
12043 o = bfd_section_from_elf_index (abfd, i);
12046 o->target_index = bfd_get_symcount (abfd);
12047 elfsym.st_shndx = i;
12048 if (!bfd_link_relocatable (info))
12049 elfsym.st_value = o->vma;
12050 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12057 /* Allocate some memory to hold information read in from the input
12059 if (max_contents_size != 0)
12061 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12062 if (flinfo.contents == NULL)
12066 if (max_external_reloc_size != 0)
12068 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12069 if (flinfo.external_relocs == NULL)
12073 if (max_internal_reloc_count != 0)
12075 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12076 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12077 if (flinfo.internal_relocs == NULL)
12081 if (max_sym_count != 0)
12083 amt = max_sym_count * bed->s->sizeof_sym;
12084 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12085 if (flinfo.external_syms == NULL)
12088 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12089 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12090 if (flinfo.internal_syms == NULL)
12093 amt = max_sym_count * sizeof (long);
12094 flinfo.indices = (long int *) bfd_malloc (amt);
12095 if (flinfo.indices == NULL)
12098 amt = max_sym_count * sizeof (asection *);
12099 flinfo.sections = (asection **) bfd_malloc (amt);
12100 if (flinfo.sections == NULL)
12104 if (max_sym_shndx_count != 0)
12106 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12107 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12108 if (flinfo.locsym_shndx == NULL)
12114 bfd_vma base, end = 0;
12117 for (sec = htab->tls_sec;
12118 sec && (sec->flags & SEC_THREAD_LOCAL);
12121 bfd_size_type size = sec->size;
12124 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12126 struct bfd_link_order *ord = sec->map_tail.link_order;
12129 size = ord->offset + ord->size;
12131 end = sec->vma + size;
12133 base = htab->tls_sec->vma;
12134 /* Only align end of TLS section if static TLS doesn't have special
12135 alignment requirements. */
12136 if (bed->static_tls_alignment == 1)
12137 end = align_power (end, htab->tls_sec->alignment_power);
12138 htab->tls_size = end - base;
12141 /* Reorder SHF_LINK_ORDER sections. */
12142 for (o = abfd->sections; o != NULL; o = o->next)
12144 if (!elf_fixup_link_order (abfd, o))
12148 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12151 /* Since ELF permits relocations to be against local symbols, we
12152 must have the local symbols available when we do the relocations.
12153 Since we would rather only read the local symbols once, and we
12154 would rather not keep them in memory, we handle all the
12155 relocations for a single input file at the same time.
12157 Unfortunately, there is no way to know the total number of local
12158 symbols until we have seen all of them, and the local symbol
12159 indices precede the global symbol indices. This means that when
12160 we are generating relocatable output, and we see a reloc against
12161 a global symbol, we can not know the symbol index until we have
12162 finished examining all the local symbols to see which ones we are
12163 going to output. To deal with this, we keep the relocations in
12164 memory, and don't output them until the end of the link. This is
12165 an unfortunate waste of memory, but I don't see a good way around
12166 it. Fortunately, it only happens when performing a relocatable
12167 link, which is not the common case. FIXME: If keep_memory is set
12168 we could write the relocs out and then read them again; I don't
12169 know how bad the memory loss will be. */
12171 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12172 sub->output_has_begun = FALSE;
12173 for (o = abfd->sections; o != NULL; o = o->next)
12175 for (p = o->map_head.link_order; p != NULL; p = p->next)
12177 if (p->type == bfd_indirect_link_order
12178 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12179 == bfd_target_elf_flavour)
12180 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12182 if (! sub->output_has_begun)
12184 if (! elf_link_input_bfd (&flinfo, sub))
12186 sub->output_has_begun = TRUE;
12189 else if (p->type == bfd_section_reloc_link_order
12190 || p->type == bfd_symbol_reloc_link_order)
12192 if (! elf_reloc_link_order (abfd, info, o, p))
12197 if (! _bfd_default_link_order (abfd, info, o, p))
12199 if (p->type == bfd_indirect_link_order
12200 && (bfd_get_flavour (sub)
12201 == bfd_target_elf_flavour)
12202 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12203 != bed->s->elfclass))
12205 const char *iclass, *oclass;
12207 switch (bed->s->elfclass)
12209 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12210 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12211 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12215 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12217 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12218 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12219 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12223 bfd_set_error (bfd_error_wrong_format);
12225 /* xgettext:c-format */
12226 (_("%pB: file class %s incompatible with %s"),
12227 sub, iclass, oclass);
12236 /* Free symbol buffer if needed. */
12237 if (!info->reduce_memory_overheads)
12239 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12240 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12241 && elf_tdata (sub)->symbuf)
12243 free (elf_tdata (sub)->symbuf);
12244 elf_tdata (sub)->symbuf = NULL;
12248 /* Output any global symbols that got converted to local in a
12249 version script or due to symbol visibility. We do this in a
12250 separate step since ELF requires all local symbols to appear
12251 prior to any global symbols. FIXME: We should only do this if
12252 some global symbols were, in fact, converted to become local.
12253 FIXME: Will this work correctly with the Irix 5 linker? */
12254 eoinfo.failed = FALSE;
12255 eoinfo.flinfo = &flinfo;
12256 eoinfo.localsyms = TRUE;
12257 eoinfo.file_sym_done = FALSE;
12258 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12262 /* If backend needs to output some local symbols not present in the hash
12263 table, do it now. */
12264 if (bed->elf_backend_output_arch_local_syms
12265 && (info->strip != strip_all || emit_relocs))
12267 typedef int (*out_sym_func)
12268 (void *, const char *, Elf_Internal_Sym *, asection *,
12269 struct elf_link_hash_entry *);
12271 if (! ((*bed->elf_backend_output_arch_local_syms)
12272 (abfd, info, &flinfo,
12273 (out_sym_func) elf_link_output_symstrtab)))
12277 /* That wrote out all the local symbols. Finish up the symbol table
12278 with the global symbols. Even if we want to strip everything we
12279 can, we still need to deal with those global symbols that got
12280 converted to local in a version script. */
12282 /* The sh_info field records the index of the first non local symbol. */
12283 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12286 && htab->dynsym != NULL
12287 && htab->dynsym->output_section != bfd_abs_section_ptr)
12289 Elf_Internal_Sym sym;
12290 bfd_byte *dynsym = htab->dynsym->contents;
12292 o = htab->dynsym->output_section;
12293 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12295 /* Write out the section symbols for the output sections. */
12296 if (bfd_link_pic (info)
12297 || htab->is_relocatable_executable)
12303 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12305 sym.st_target_internal = 0;
12307 for (s = abfd->sections; s != NULL; s = s->next)
12313 dynindx = elf_section_data (s)->dynindx;
12316 indx = elf_section_data (s)->this_idx;
12317 BFD_ASSERT (indx > 0);
12318 sym.st_shndx = indx;
12319 if (! check_dynsym (abfd, &sym))
12321 sym.st_value = s->vma;
12322 dest = dynsym + dynindx * bed->s->sizeof_sym;
12323 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12327 /* Write out the local dynsyms. */
12328 if (htab->dynlocal)
12330 struct elf_link_local_dynamic_entry *e;
12331 for (e = htab->dynlocal; e ; e = e->next)
12336 /* Copy the internal symbol and turn off visibility.
12337 Note that we saved a word of storage and overwrote
12338 the original st_name with the dynstr_index. */
12340 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12342 s = bfd_section_from_elf_index (e->input_bfd,
12347 elf_section_data (s->output_section)->this_idx;
12348 if (! check_dynsym (abfd, &sym))
12350 sym.st_value = (s->output_section->vma
12352 + e->isym.st_value);
12355 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12356 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12361 /* We get the global symbols from the hash table. */
12362 eoinfo.failed = FALSE;
12363 eoinfo.localsyms = FALSE;
12364 eoinfo.flinfo = &flinfo;
12365 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12369 /* If backend needs to output some symbols not present in the hash
12370 table, do it now. */
12371 if (bed->elf_backend_output_arch_syms
12372 && (info->strip != strip_all || emit_relocs))
12374 typedef int (*out_sym_func)
12375 (void *, const char *, Elf_Internal_Sym *, asection *,
12376 struct elf_link_hash_entry *);
12378 if (! ((*bed->elf_backend_output_arch_syms)
12379 (abfd, info, &flinfo,
12380 (out_sym_func) elf_link_output_symstrtab)))
12384 /* Finalize the .strtab section. */
12385 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12387 /* Swap out the .strtab section. */
12388 if (!elf_link_swap_symbols_out (&flinfo))
12391 /* Now we know the size of the symtab section. */
12392 if (bfd_get_symcount (abfd) > 0)
12394 /* Finish up and write out the symbol string table (.strtab)
12396 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12397 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12399 if (elf_symtab_shndx_list (abfd))
12401 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12403 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12405 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12406 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12407 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12408 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12409 symtab_shndx_hdr->sh_size = amt;
12411 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12414 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12415 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12420 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12421 /* sh_name was set in prep_headers. */
12422 symstrtab_hdr->sh_type = SHT_STRTAB;
12423 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12424 symstrtab_hdr->sh_addr = 0;
12425 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12426 symstrtab_hdr->sh_entsize = 0;
12427 symstrtab_hdr->sh_link = 0;
12428 symstrtab_hdr->sh_info = 0;
12429 /* sh_offset is set just below. */
12430 symstrtab_hdr->sh_addralign = 1;
12432 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12434 elf_next_file_pos (abfd) = off;
12436 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12437 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12441 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12443 _bfd_error_handler (_("%pB: failed to generate import library"),
12444 info->out_implib_bfd);
12448 /* Adjust the relocs to have the correct symbol indices. */
12449 for (o = abfd->sections; o != NULL; o = o->next)
12451 struct bfd_elf_section_data *esdo = elf_section_data (o);
12454 if ((o->flags & SEC_RELOC) == 0)
12457 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12458 if (esdo->rel.hdr != NULL
12459 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12461 if (esdo->rela.hdr != NULL
12462 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12465 /* Set the reloc_count field to 0 to prevent write_relocs from
12466 trying to swap the relocs out itself. */
12467 o->reloc_count = 0;
12470 if (dynamic && info->combreloc && dynobj != NULL)
12471 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12473 /* If we are linking against a dynamic object, or generating a
12474 shared library, finish up the dynamic linking information. */
12477 bfd_byte *dyncon, *dynconend;
12479 /* Fix up .dynamic entries. */
12480 o = bfd_get_linker_section (dynobj, ".dynamic");
12481 BFD_ASSERT (o != NULL);
12483 dyncon = o->contents;
12484 dynconend = o->contents + o->size;
12485 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12487 Elf_Internal_Dyn dyn;
12490 bfd_size_type sh_size;
12493 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12500 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12502 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12504 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12505 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12508 dyn.d_un.d_val = relativecount;
12515 name = info->init_function;
12518 name = info->fini_function;
12521 struct elf_link_hash_entry *h;
12523 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12525 && (h->root.type == bfd_link_hash_defined
12526 || h->root.type == bfd_link_hash_defweak))
12528 dyn.d_un.d_ptr = h->root.u.def.value;
12529 o = h->root.u.def.section;
12530 if (o->output_section != NULL)
12531 dyn.d_un.d_ptr += (o->output_section->vma
12532 + o->output_offset);
12535 /* The symbol is imported from another shared
12536 library and does not apply to this one. */
12537 dyn.d_un.d_ptr = 0;
12544 case DT_PREINIT_ARRAYSZ:
12545 name = ".preinit_array";
12547 case DT_INIT_ARRAYSZ:
12548 name = ".init_array";
12550 case DT_FINI_ARRAYSZ:
12551 name = ".fini_array";
12553 o = bfd_get_section_by_name (abfd, name);
12557 (_("could not find section %s"), name);
12562 (_("warning: %s section has zero size"), name);
12563 dyn.d_un.d_val = o->size;
12566 case DT_PREINIT_ARRAY:
12567 name = ".preinit_array";
12569 case DT_INIT_ARRAY:
12570 name = ".init_array";
12572 case DT_FINI_ARRAY:
12573 name = ".fini_array";
12575 o = bfd_get_section_by_name (abfd, name);
12582 name = ".gnu.hash";
12591 name = ".gnu.version_d";
12594 name = ".gnu.version_r";
12597 name = ".gnu.version";
12599 o = bfd_get_linker_section (dynobj, name);
12601 if (o == NULL || bfd_is_abs_section (o->output_section))
12604 (_("could not find section %s"), name);
12607 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12610 (_("warning: section '%s' is being made into a note"), name);
12611 bfd_set_error (bfd_error_nonrepresentable_section);
12614 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12621 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12627 for (i = 1; i < elf_numsections (abfd); i++)
12629 Elf_Internal_Shdr *hdr;
12631 hdr = elf_elfsections (abfd)[i];
12632 if (hdr->sh_type == type
12633 && (hdr->sh_flags & SHF_ALLOC) != 0)
12635 sh_size += hdr->sh_size;
12637 || sh_addr > hdr->sh_addr)
12638 sh_addr = hdr->sh_addr;
12642 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12644 /* Don't count procedure linkage table relocs in the
12645 overall reloc count. */
12646 sh_size -= htab->srelplt->size;
12648 /* If the size is zero, make the address zero too.
12649 This is to avoid a glibc bug. If the backend
12650 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12651 zero, then we'll put DT_RELA at the end of
12652 DT_JMPREL. glibc will interpret the end of
12653 DT_RELA matching the end of DT_JMPREL as the
12654 case where DT_RELA includes DT_JMPREL, and for
12655 LD_BIND_NOW will decide that processing DT_RELA
12656 will process the PLT relocs too. Net result:
12657 No PLT relocs applied. */
12660 /* If .rela.plt is the first .rela section, exclude
12661 it from DT_RELA. */
12662 else if (sh_addr == (htab->srelplt->output_section->vma
12663 + htab->srelplt->output_offset))
12664 sh_addr += htab->srelplt->size;
12667 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12668 dyn.d_un.d_val = sh_size;
12670 dyn.d_un.d_ptr = sh_addr;
12673 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12677 /* If we have created any dynamic sections, then output them. */
12678 if (dynobj != NULL)
12680 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12683 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12684 if (((info->warn_shared_textrel && bfd_link_pic (info))
12685 || info->error_textrel)
12686 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12688 bfd_byte *dyncon, *dynconend;
12690 dyncon = o->contents;
12691 dynconend = o->contents + o->size;
12692 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12694 Elf_Internal_Dyn dyn;
12696 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12698 if (dyn.d_tag == DT_TEXTREL)
12700 if (info->error_textrel)
12701 info->callbacks->einfo
12702 (_("%P%X: read-only segment has dynamic relocations\n"));
12704 info->callbacks->einfo
12705 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12711 for (o = dynobj->sections; o != NULL; o = o->next)
12713 if ((o->flags & SEC_HAS_CONTENTS) == 0
12715 || o->output_section == bfd_abs_section_ptr)
12717 if ((o->flags & SEC_LINKER_CREATED) == 0)
12719 /* At this point, we are only interested in sections
12720 created by _bfd_elf_link_create_dynamic_sections. */
12723 if (htab->stab_info.stabstr == o)
12725 if (htab->eh_info.hdr_sec == o)
12727 if (strcmp (o->name, ".dynstr") != 0)
12729 if (! bfd_set_section_contents (abfd, o->output_section,
12731 (file_ptr) o->output_offset
12732 * bfd_octets_per_byte (abfd),
12738 /* The contents of the .dynstr section are actually in a
12742 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12743 if (bfd_seek (abfd, off, SEEK_SET) != 0
12744 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12750 if (!info->resolve_section_groups)
12752 bfd_boolean failed = FALSE;
12754 BFD_ASSERT (bfd_link_relocatable (info));
12755 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12760 /* If we have optimized stabs strings, output them. */
12761 if (htab->stab_info.stabstr != NULL)
12763 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12767 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12770 elf_final_link_free (abfd, &flinfo);
12772 elf_linker (abfd) = TRUE;
12776 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12777 if (contents == NULL)
12778 return FALSE; /* Bail out and fail. */
12779 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12780 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12787 elf_final_link_free (abfd, &flinfo);
12791 /* Initialize COOKIE for input bfd ABFD. */
12794 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12795 struct bfd_link_info *info, bfd *abfd)
12797 Elf_Internal_Shdr *symtab_hdr;
12798 const struct elf_backend_data *bed;
12800 bed = get_elf_backend_data (abfd);
12801 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12803 cookie->abfd = abfd;
12804 cookie->sym_hashes = elf_sym_hashes (abfd);
12805 cookie->bad_symtab = elf_bad_symtab (abfd);
12806 if (cookie->bad_symtab)
12808 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12809 cookie->extsymoff = 0;
12813 cookie->locsymcount = symtab_hdr->sh_info;
12814 cookie->extsymoff = symtab_hdr->sh_info;
12817 if (bed->s->arch_size == 32)
12818 cookie->r_sym_shift = 8;
12820 cookie->r_sym_shift = 32;
12822 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12823 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12825 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12826 cookie->locsymcount, 0,
12828 if (cookie->locsyms == NULL)
12830 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12833 if (info->keep_memory)
12834 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12839 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12842 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12844 Elf_Internal_Shdr *symtab_hdr;
12846 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12847 if (cookie->locsyms != NULL
12848 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12849 free (cookie->locsyms);
12852 /* Initialize the relocation information in COOKIE for input section SEC
12853 of input bfd ABFD. */
12856 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12857 struct bfd_link_info *info, bfd *abfd,
12860 if (sec->reloc_count == 0)
12862 cookie->rels = NULL;
12863 cookie->relend = NULL;
12867 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12868 info->keep_memory);
12869 if (cookie->rels == NULL)
12871 cookie->rel = cookie->rels;
12872 cookie->relend = cookie->rels + sec->reloc_count;
12874 cookie->rel = cookie->rels;
12878 /* Free the memory allocated by init_reloc_cookie_rels,
12882 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12885 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12886 free (cookie->rels);
12889 /* Initialize the whole of COOKIE for input section SEC. */
12892 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12893 struct bfd_link_info *info,
12896 if (!init_reloc_cookie (cookie, info, sec->owner))
12898 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12903 fini_reloc_cookie (cookie, sec->owner);
12908 /* Free the memory allocated by init_reloc_cookie_for_section,
12912 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12915 fini_reloc_cookie_rels (cookie, sec);
12916 fini_reloc_cookie (cookie, sec->owner);
12919 /* Garbage collect unused sections. */
12921 /* Default gc_mark_hook. */
12924 _bfd_elf_gc_mark_hook (asection *sec,
12925 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12926 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12927 struct elf_link_hash_entry *h,
12928 Elf_Internal_Sym *sym)
12932 switch (h->root.type)
12934 case bfd_link_hash_defined:
12935 case bfd_link_hash_defweak:
12936 return h->root.u.def.section;
12938 case bfd_link_hash_common:
12939 return h->root.u.c.p->section;
12946 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12951 /* Return the debug definition section. */
12954 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12955 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12956 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12957 struct elf_link_hash_entry *h,
12958 Elf_Internal_Sym *sym)
12962 /* Return the global debug definition section. */
12963 if ((h->root.type == bfd_link_hash_defined
12964 || h->root.type == bfd_link_hash_defweak)
12965 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12966 return h->root.u.def.section;
12970 /* Return the local debug definition section. */
12971 asection *isec = bfd_section_from_elf_index (sec->owner,
12973 if ((isec->flags & SEC_DEBUGGING) != 0)
12980 /* COOKIE->rel describes a relocation against section SEC, which is
12981 a section we've decided to keep. Return the section that contains
12982 the relocation symbol, or NULL if no section contains it. */
12985 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12986 elf_gc_mark_hook_fn gc_mark_hook,
12987 struct elf_reloc_cookie *cookie,
12988 bfd_boolean *start_stop)
12990 unsigned long r_symndx;
12991 struct elf_link_hash_entry *h;
12993 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12994 if (r_symndx == STN_UNDEF)
12997 if (r_symndx >= cookie->locsymcount
12998 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13000 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13003 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13007 while (h->root.type == bfd_link_hash_indirect
13008 || h->root.type == bfd_link_hash_warning)
13009 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13011 /* If this symbol is weak and there is a non-weak definition, we
13012 keep the non-weak definition because many backends put
13013 dynamic reloc info on the non-weak definition for code
13014 handling copy relocs. */
13015 if (h->is_weakalias)
13016 weakdef (h)->mark = 1;
13018 if (start_stop != NULL)
13020 /* To work around a glibc bug, mark XXX input sections
13021 when there is a reference to __start_XXX or __stop_XXX
13025 asection *s = h->u2.start_stop_section;
13026 *start_stop = !s->gc_mark;
13031 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13034 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13035 &cookie->locsyms[r_symndx]);
13038 /* COOKIE->rel describes a relocation against section SEC, which is
13039 a section we've decided to keep. Mark the section that contains
13040 the relocation symbol. */
13043 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13045 elf_gc_mark_hook_fn gc_mark_hook,
13046 struct elf_reloc_cookie *cookie)
13049 bfd_boolean start_stop = FALSE;
13051 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13052 while (rsec != NULL)
13054 if (!rsec->gc_mark)
13056 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13057 || (rsec->owner->flags & DYNAMIC) != 0)
13059 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13064 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13069 /* The mark phase of garbage collection. For a given section, mark
13070 it and any sections in this section's group, and all the sections
13071 which define symbols to which it refers. */
13074 _bfd_elf_gc_mark (struct bfd_link_info *info,
13076 elf_gc_mark_hook_fn gc_mark_hook)
13079 asection *group_sec, *eh_frame;
13083 /* Mark all the sections in the group. */
13084 group_sec = elf_section_data (sec)->next_in_group;
13085 if (group_sec && !group_sec->gc_mark)
13086 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13089 /* Look through the section relocs. */
13091 eh_frame = elf_eh_frame_section (sec->owner);
13092 if ((sec->flags & SEC_RELOC) != 0
13093 && sec->reloc_count > 0
13094 && sec != eh_frame)
13096 struct elf_reloc_cookie cookie;
13098 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13102 for (; cookie.rel < cookie.relend; cookie.rel++)
13103 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13108 fini_reloc_cookie_for_section (&cookie, sec);
13112 if (ret && eh_frame && elf_fde_list (sec))
13114 struct elf_reloc_cookie cookie;
13116 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13120 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13121 gc_mark_hook, &cookie))
13123 fini_reloc_cookie_for_section (&cookie, eh_frame);
13127 eh_frame = elf_section_eh_frame_entry (sec);
13128 if (ret && eh_frame && !eh_frame->gc_mark)
13129 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13135 /* Scan and mark sections in a special or debug section group. */
13138 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13140 /* Point to first section of section group. */
13142 /* Used to iterate the section group. */
13145 bfd_boolean is_special_grp = TRUE;
13146 bfd_boolean is_debug_grp = TRUE;
13148 /* First scan to see if group contains any section other than debug
13149 and special section. */
13150 ssec = msec = elf_next_in_group (grp);
13153 if ((msec->flags & SEC_DEBUGGING) == 0)
13154 is_debug_grp = FALSE;
13156 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13157 is_special_grp = FALSE;
13159 msec = elf_next_in_group (msec);
13161 while (msec != ssec);
13163 /* If this is a pure debug section group or pure special section group,
13164 keep all sections in this group. */
13165 if (is_debug_grp || is_special_grp)
13170 msec = elf_next_in_group (msec);
13172 while (msec != ssec);
13176 /* Keep debug and special sections. */
13179 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13180 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13184 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13187 bfd_boolean some_kept;
13188 bfd_boolean debug_frag_seen;
13189 bfd_boolean has_kept_debug_info;
13191 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13193 isec = ibfd->sections;
13194 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13197 /* Ensure all linker created sections are kept,
13198 see if any other section is already marked,
13199 and note if we have any fragmented debug sections. */
13200 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13201 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13203 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13205 else if (isec->gc_mark
13206 && (isec->flags & SEC_ALLOC) != 0
13207 && elf_section_type (isec) != SHT_NOTE)
13210 if (!debug_frag_seen
13211 && (isec->flags & SEC_DEBUGGING)
13212 && CONST_STRNEQ (isec->name, ".debug_line."))
13213 debug_frag_seen = TRUE;
13216 /* If no non-note alloc section in this file will be kept, then
13217 we can toss out the debug and special sections. */
13221 /* Keep debug and special sections like .comment when they are
13222 not part of a group. Also keep section groups that contain
13223 just debug sections or special sections. */
13224 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13226 if ((isec->flags & SEC_GROUP) != 0)
13227 _bfd_elf_gc_mark_debug_special_section_group (isec);
13228 else if (((isec->flags & SEC_DEBUGGING) != 0
13229 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13230 && elf_next_in_group (isec) == NULL)
13232 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13233 has_kept_debug_info = TRUE;
13236 /* Look for CODE sections which are going to be discarded,
13237 and find and discard any fragmented debug sections which
13238 are associated with that code section. */
13239 if (debug_frag_seen)
13240 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13241 if ((isec->flags & SEC_CODE) != 0
13242 && isec->gc_mark == 0)
13247 ilen = strlen (isec->name);
13249 /* Association is determined by the name of the debug
13250 section containing the name of the code section as
13251 a suffix. For example .debug_line.text.foo is a
13252 debug section associated with .text.foo. */
13253 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13257 if (dsec->gc_mark == 0
13258 || (dsec->flags & SEC_DEBUGGING) == 0)
13261 dlen = strlen (dsec->name);
13264 && strncmp (dsec->name + (dlen - ilen),
13265 isec->name, ilen) == 0)
13270 /* Mark debug sections referenced by kept debug sections. */
13271 if (has_kept_debug_info)
13272 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13274 && (isec->flags & SEC_DEBUGGING) != 0)
13275 if (!_bfd_elf_gc_mark (info, isec,
13276 elf_gc_mark_debug_section))
13283 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13286 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13288 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13292 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13293 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13294 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13297 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13300 for (o = sub->sections; o != NULL; o = o->next)
13302 /* When any section in a section group is kept, we keep all
13303 sections in the section group. If the first member of
13304 the section group is excluded, we will also exclude the
13306 if (o->flags & SEC_GROUP)
13308 asection *first = elf_next_in_group (o);
13309 o->gc_mark = first->gc_mark;
13315 /* Skip sweeping sections already excluded. */
13316 if (o->flags & SEC_EXCLUDE)
13319 /* Since this is early in the link process, it is simple
13320 to remove a section from the output. */
13321 o->flags |= SEC_EXCLUDE;
13323 if (info->print_gc_sections && o->size != 0)
13324 /* xgettext:c-format */
13325 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13333 /* Propagate collected vtable information. This is called through
13334 elf_link_hash_traverse. */
13337 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13339 /* Those that are not vtables. */
13341 || h->u2.vtable == NULL
13342 || h->u2.vtable->parent == NULL)
13345 /* Those vtables that do not have parents, we cannot merge. */
13346 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13349 /* If we've already been done, exit. */
13350 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13353 /* Make sure the parent's table is up to date. */
13354 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13356 if (h->u2.vtable->used == NULL)
13358 /* None of this table's entries were referenced. Re-use the
13360 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13361 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13366 bfd_boolean *cu, *pu;
13368 /* Or the parent's entries into ours. */
13369 cu = h->u2.vtable->used;
13371 pu = h->u2.vtable->parent->u2.vtable->used;
13374 const struct elf_backend_data *bed;
13375 unsigned int log_file_align;
13377 bed = get_elf_backend_data (h->root.u.def.section->owner);
13378 log_file_align = bed->s->log_file_align;
13379 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13394 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13397 bfd_vma hstart, hend;
13398 Elf_Internal_Rela *relstart, *relend, *rel;
13399 const struct elf_backend_data *bed;
13400 unsigned int log_file_align;
13402 /* Take care of both those symbols that do not describe vtables as
13403 well as those that are not loaded. */
13405 || h->u2.vtable == NULL
13406 || h->u2.vtable->parent == NULL)
13409 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13410 || h->root.type == bfd_link_hash_defweak);
13412 sec = h->root.u.def.section;
13413 hstart = h->root.u.def.value;
13414 hend = hstart + h->size;
13416 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13418 return *(bfd_boolean *) okp = FALSE;
13419 bed = get_elf_backend_data (sec->owner);
13420 log_file_align = bed->s->log_file_align;
13422 relend = relstart + sec->reloc_count;
13424 for (rel = relstart; rel < relend; ++rel)
13425 if (rel->r_offset >= hstart && rel->r_offset < hend)
13427 /* If the entry is in use, do nothing. */
13428 if (h->u2.vtable->used
13429 && (rel->r_offset - hstart) < h->u2.vtable->size)
13431 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13432 if (h->u2.vtable->used[entry])
13435 /* Otherwise, kill it. */
13436 rel->r_offset = rel->r_info = rel->r_addend = 0;
13442 /* Mark sections containing dynamically referenced symbols. When
13443 building shared libraries, we must assume that any visible symbol is
13447 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13449 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13450 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13452 if ((h->root.type == bfd_link_hash_defined
13453 || h->root.type == bfd_link_hash_defweak)
13454 && ((h->ref_dynamic && !h->forced_local)
13455 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13456 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13457 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13458 && (!bfd_link_executable (info)
13459 || info->gc_keep_exported
13460 || info->export_dynamic
13463 && (*d->match) (&d->head, NULL, h->root.root.string)))
13464 && (h->versioned >= versioned
13465 || !bfd_hide_sym_by_version (info->version_info,
13466 h->root.root.string)))))
13467 h->root.u.def.section->flags |= SEC_KEEP;
13472 /* Keep all sections containing symbols undefined on the command-line,
13473 and the section containing the entry symbol. */
13476 _bfd_elf_gc_keep (struct bfd_link_info *info)
13478 struct bfd_sym_chain *sym;
13480 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13482 struct elf_link_hash_entry *h;
13484 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13485 FALSE, FALSE, FALSE);
13488 && (h->root.type == bfd_link_hash_defined
13489 || h->root.type == bfd_link_hash_defweak)
13490 && !bfd_is_abs_section (h->root.u.def.section)
13491 && !bfd_is_und_section (h->root.u.def.section))
13492 h->root.u.def.section->flags |= SEC_KEEP;
13497 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13498 struct bfd_link_info *info)
13500 bfd *ibfd = info->input_bfds;
13502 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13505 struct elf_reloc_cookie cookie;
13507 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13509 sec = ibfd->sections;
13510 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13513 if (!init_reloc_cookie (&cookie, info, ibfd))
13516 for (sec = ibfd->sections; sec; sec = sec->next)
13518 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13519 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13521 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13522 fini_reloc_cookie_rels (&cookie, sec);
13529 /* Do mark and sweep of unused sections. */
13532 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13534 bfd_boolean ok = TRUE;
13536 elf_gc_mark_hook_fn gc_mark_hook;
13537 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13538 struct elf_link_hash_table *htab;
13540 if (!bed->can_gc_sections
13541 || !is_elf_hash_table (info->hash))
13543 _bfd_error_handler(_("warning: gc-sections option ignored"));
13547 bed->gc_keep (info);
13548 htab = elf_hash_table (info);
13550 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13551 at the .eh_frame section if we can mark the FDEs individually. */
13552 for (sub = info->input_bfds;
13553 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13554 sub = sub->link.next)
13557 struct elf_reloc_cookie cookie;
13559 sec = sub->sections;
13560 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13562 sec = bfd_get_section_by_name (sub, ".eh_frame");
13563 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13565 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13566 if (elf_section_data (sec)->sec_info
13567 && (sec->flags & SEC_LINKER_CREATED) == 0)
13568 elf_eh_frame_section (sub) = sec;
13569 fini_reloc_cookie_for_section (&cookie, sec);
13570 sec = bfd_get_next_section_by_name (NULL, sec);
13574 /* Apply transitive closure to the vtable entry usage info. */
13575 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13579 /* Kill the vtable relocations that were not used. */
13580 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13584 /* Mark dynamically referenced symbols. */
13585 if (htab->dynamic_sections_created || info->gc_keep_exported)
13586 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13588 /* Grovel through relocs to find out who stays ... */
13589 gc_mark_hook = bed->gc_mark_hook;
13590 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13594 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13595 || elf_object_id (sub) != elf_hash_table_id (htab)
13596 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13600 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13603 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13604 Also treat note sections as a root, if the section is not part
13605 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13606 well as FINI_ARRAY sections for ld -r. */
13607 for (o = sub->sections; o != NULL; o = o->next)
13609 && (o->flags & SEC_EXCLUDE) == 0
13610 && ((o->flags & SEC_KEEP) != 0
13611 || (bfd_link_relocatable (info)
13612 && ((elf_section_data (o)->this_hdr.sh_type
13613 == SHT_PREINIT_ARRAY)
13614 || (elf_section_data (o)->this_hdr.sh_type
13616 || (elf_section_data (o)->this_hdr.sh_type
13617 == SHT_FINI_ARRAY)))
13618 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13619 && elf_next_in_group (o) == NULL )))
13621 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13626 /* Allow the backend to mark additional target specific sections. */
13627 bed->gc_mark_extra_sections (info, gc_mark_hook);
13629 /* ... and mark SEC_EXCLUDE for those that go. */
13630 return elf_gc_sweep (abfd, info);
13633 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13636 bfd_elf_gc_record_vtinherit (bfd *abfd,
13638 struct elf_link_hash_entry *h,
13641 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13642 struct elf_link_hash_entry **search, *child;
13643 size_t extsymcount;
13644 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13646 /* The sh_info field of the symtab header tells us where the
13647 external symbols start. We don't care about the local symbols at
13649 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13650 if (!elf_bad_symtab (abfd))
13651 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13653 sym_hashes = elf_sym_hashes (abfd);
13654 sym_hashes_end = sym_hashes + extsymcount;
13656 /* Hunt down the child symbol, which is in this section at the same
13657 offset as the relocation. */
13658 for (search = sym_hashes; search != sym_hashes_end; ++search)
13660 if ((child = *search) != NULL
13661 && (child->root.type == bfd_link_hash_defined
13662 || child->root.type == bfd_link_hash_defweak)
13663 && child->root.u.def.section == sec
13664 && child->root.u.def.value == offset)
13668 /* xgettext:c-format */
13669 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13670 abfd, sec, (uint64_t) offset);
13671 bfd_set_error (bfd_error_invalid_operation);
13675 if (!child->u2.vtable)
13677 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13678 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13679 if (!child->u2.vtable)
13684 /* This *should* only be the absolute section. It could potentially
13685 be that someone has defined a non-global vtable though, which
13686 would be bad. It isn't worth paging in the local symbols to be
13687 sure though; that case should simply be handled by the assembler. */
13689 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13692 child->u2.vtable->parent = h;
13697 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13700 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13701 asection *sec ATTRIBUTE_UNUSED,
13702 struct elf_link_hash_entry *h,
13705 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13706 unsigned int log_file_align = bed->s->log_file_align;
13710 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13711 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13716 if (addend >= h->u2.vtable->size)
13718 size_t size, bytes, file_align;
13719 bfd_boolean *ptr = h->u2.vtable->used;
13721 /* While the symbol is undefined, we have to be prepared to handle
13723 file_align = 1 << log_file_align;
13724 if (h->root.type == bfd_link_hash_undefined)
13725 size = addend + file_align;
13729 if (addend >= size)
13731 /* Oops! We've got a reference past the defined end of
13732 the table. This is probably a bug -- shall we warn? */
13733 size = addend + file_align;
13736 size = (size + file_align - 1) & -file_align;
13738 /* Allocate one extra entry for use as a "done" flag for the
13739 consolidation pass. */
13740 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13744 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13750 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13751 * sizeof (bfd_boolean));
13752 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13756 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13761 /* And arrange for that done flag to be at index -1. */
13762 h->u2.vtable->used = ptr + 1;
13763 h->u2.vtable->size = size;
13766 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13771 /* Map an ELF section header flag to its corresponding string. */
13775 flagword flag_value;
13776 } elf_flags_to_name_table;
13778 static elf_flags_to_name_table elf_flags_to_names [] =
13780 { "SHF_WRITE", SHF_WRITE },
13781 { "SHF_ALLOC", SHF_ALLOC },
13782 { "SHF_EXECINSTR", SHF_EXECINSTR },
13783 { "SHF_MERGE", SHF_MERGE },
13784 { "SHF_STRINGS", SHF_STRINGS },
13785 { "SHF_INFO_LINK", SHF_INFO_LINK},
13786 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13787 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13788 { "SHF_GROUP", SHF_GROUP },
13789 { "SHF_TLS", SHF_TLS },
13790 { "SHF_MASKOS", SHF_MASKOS },
13791 { "SHF_EXCLUDE", SHF_EXCLUDE },
13794 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13796 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13797 struct flag_info *flaginfo,
13800 const bfd_vma sh_flags = elf_section_flags (section);
13802 if (!flaginfo->flags_initialized)
13804 bfd *obfd = info->output_bfd;
13805 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13806 struct flag_info_list *tf = flaginfo->flag_list;
13808 int without_hex = 0;
13810 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13813 flagword (*lookup) (char *);
13815 lookup = bed->elf_backend_lookup_section_flags_hook;
13816 if (lookup != NULL)
13818 flagword hexval = (*lookup) ((char *) tf->name);
13822 if (tf->with == with_flags)
13823 with_hex |= hexval;
13824 else if (tf->with == without_flags)
13825 without_hex |= hexval;
13830 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13832 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13834 if (tf->with == with_flags)
13835 with_hex |= elf_flags_to_names[i].flag_value;
13836 else if (tf->with == without_flags)
13837 without_hex |= elf_flags_to_names[i].flag_value;
13844 info->callbacks->einfo
13845 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13849 flaginfo->flags_initialized = TRUE;
13850 flaginfo->only_with_flags |= with_hex;
13851 flaginfo->not_with_flags |= without_hex;
13854 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13857 if ((flaginfo->not_with_flags & sh_flags) != 0)
13863 struct alloc_got_off_arg {
13865 struct bfd_link_info *info;
13868 /* We need a special top-level link routine to convert got reference counts
13869 to real got offsets. */
13872 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13874 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13875 bfd *obfd = gofarg->info->output_bfd;
13876 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13878 if (h->got.refcount > 0)
13880 h->got.offset = gofarg->gotoff;
13881 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13884 h->got.offset = (bfd_vma) -1;
13889 /* And an accompanying bit to work out final got entry offsets once
13890 we're done. Should be called from final_link. */
13893 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13894 struct bfd_link_info *info)
13897 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13899 struct alloc_got_off_arg gofarg;
13901 BFD_ASSERT (abfd == info->output_bfd);
13903 if (! is_elf_hash_table (info->hash))
13906 /* The GOT offset is relative to the .got section, but the GOT header is
13907 put into the .got.plt section, if the backend uses it. */
13908 if (bed->want_got_plt)
13911 gotoff = bed->got_header_size;
13913 /* Do the local .got entries first. */
13914 for (i = info->input_bfds; i; i = i->link.next)
13916 bfd_signed_vma *local_got;
13917 size_t j, locsymcount;
13918 Elf_Internal_Shdr *symtab_hdr;
13920 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13923 local_got = elf_local_got_refcounts (i);
13927 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13928 if (elf_bad_symtab (i))
13929 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13931 locsymcount = symtab_hdr->sh_info;
13933 for (j = 0; j < locsymcount; ++j)
13935 if (local_got[j] > 0)
13937 local_got[j] = gotoff;
13938 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13941 local_got[j] = (bfd_vma) -1;
13945 /* Then the global .got entries. .plt refcounts are handled by
13946 adjust_dynamic_symbol */
13947 gofarg.gotoff = gotoff;
13948 gofarg.info = info;
13949 elf_link_hash_traverse (elf_hash_table (info),
13950 elf_gc_allocate_got_offsets,
13955 /* Many folk need no more in the way of final link than this, once
13956 got entry reference counting is enabled. */
13959 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13961 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13964 /* Invoke the regular ELF backend linker to do all the work. */
13965 return bfd_elf_final_link (abfd, info);
13969 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13971 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13973 if (rcookie->bad_symtab)
13974 rcookie->rel = rcookie->rels;
13976 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13978 unsigned long r_symndx;
13980 if (! rcookie->bad_symtab)
13981 if (rcookie->rel->r_offset > offset)
13983 if (rcookie->rel->r_offset != offset)
13986 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13987 if (r_symndx == STN_UNDEF)
13990 if (r_symndx >= rcookie->locsymcount
13991 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13993 struct elf_link_hash_entry *h;
13995 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13997 while (h->root.type == bfd_link_hash_indirect
13998 || h->root.type == bfd_link_hash_warning)
13999 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14001 if ((h->root.type == bfd_link_hash_defined
14002 || h->root.type == bfd_link_hash_defweak)
14003 && (h->root.u.def.section->owner != rcookie->abfd
14004 || h->root.u.def.section->kept_section != NULL
14005 || discarded_section (h->root.u.def.section)))
14010 /* It's not a relocation against a global symbol,
14011 but it could be a relocation against a local
14012 symbol for a discarded section. */
14014 Elf_Internal_Sym *isym;
14016 /* Need to: get the symbol; get the section. */
14017 isym = &rcookie->locsyms[r_symndx];
14018 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14020 && (isec->kept_section != NULL
14021 || discarded_section (isec)))
14029 /* Discard unneeded references to discarded sections.
14030 Returns -1 on error, 1 if any section's size was changed, 0 if
14031 nothing changed. This function assumes that the relocations are in
14032 sorted order, which is true for all known assemblers. */
14035 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14037 struct elf_reloc_cookie cookie;
14042 if (info->traditional_format
14043 || !is_elf_hash_table (info->hash))
14046 o = bfd_get_section_by_name (output_bfd, ".stab");
14051 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14054 || i->reloc_count == 0
14055 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14059 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14062 if (!init_reloc_cookie_for_section (&cookie, info, i))
14065 if (_bfd_discard_section_stabs (abfd, i,
14066 elf_section_data (i)->sec_info,
14067 bfd_elf_reloc_symbol_deleted_p,
14071 fini_reloc_cookie_for_section (&cookie, i);
14076 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14077 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14081 int eh_changed = 0;
14082 unsigned int eh_alignment;
14084 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14090 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14093 if (!init_reloc_cookie_for_section (&cookie, info, i))
14096 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14097 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14098 bfd_elf_reloc_symbol_deleted_p,
14102 if (i->size != i->rawsize)
14106 fini_reloc_cookie_for_section (&cookie, i);
14109 eh_alignment = 1 << o->alignment_power;
14110 /* Skip over zero terminator, and prevent empty sections from
14111 adding alignment padding at the end. */
14112 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14114 i->flags |= SEC_EXCLUDE;
14115 else if (i->size > 4)
14117 /* The last non-empty eh_frame section doesn't need padding. */
14120 /* Any prior sections must pad the last FDE out to the output
14121 section alignment. Otherwise we might have zero padding
14122 between sections, which would be seen as a terminator. */
14123 for (; i != NULL; i = i->map_tail.s)
14125 /* All but the last zero terminator should have been removed. */
14130 = (i->size + eh_alignment - 1) & -eh_alignment;
14131 if (i->size != size)
14139 elf_link_hash_traverse (elf_hash_table (info),
14140 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14143 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14145 const struct elf_backend_data *bed;
14148 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14150 s = abfd->sections;
14151 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14154 bed = get_elf_backend_data (abfd);
14156 if (bed->elf_backend_discard_info != NULL)
14158 if (!init_reloc_cookie (&cookie, info, abfd))
14161 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14164 fini_reloc_cookie (&cookie, abfd);
14168 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14169 _bfd_elf_end_eh_frame_parsing (info);
14171 if (info->eh_frame_hdr_type
14172 && !bfd_link_relocatable (info)
14173 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14180 _bfd_elf_section_already_linked (bfd *abfd,
14182 struct bfd_link_info *info)
14185 const char *name, *key;
14186 struct bfd_section_already_linked *l;
14187 struct bfd_section_already_linked_hash_entry *already_linked_list;
14189 if (sec->output_section == bfd_abs_section_ptr)
14192 flags = sec->flags;
14194 /* Return if it isn't a linkonce section. A comdat group section
14195 also has SEC_LINK_ONCE set. */
14196 if ((flags & SEC_LINK_ONCE) == 0)
14199 /* Don't put group member sections on our list of already linked
14200 sections. They are handled as a group via their group section. */
14201 if (elf_sec_group (sec) != NULL)
14204 /* For a SHT_GROUP section, use the group signature as the key. */
14206 if ((flags & SEC_GROUP) != 0
14207 && elf_next_in_group (sec) != NULL
14208 && elf_group_name (elf_next_in_group (sec)) != NULL)
14209 key = elf_group_name (elf_next_in_group (sec));
14212 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14213 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14214 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14217 /* Must be a user linkonce section that doesn't follow gcc's
14218 naming convention. In this case we won't be matching
14219 single member groups. */
14223 already_linked_list = bfd_section_already_linked_table_lookup (key);
14225 for (l = already_linked_list->entry; l != NULL; l = l->next)
14227 /* We may have 2 different types of sections on the list: group
14228 sections with a signature of <key> (<key> is some string),
14229 and linkonce sections named .gnu.linkonce.<type>.<key>.
14230 Match like sections. LTO plugin sections are an exception.
14231 They are always named .gnu.linkonce.t.<key> and match either
14232 type of section. */
14233 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14234 && ((flags & SEC_GROUP) != 0
14235 || strcmp (name, l->sec->name) == 0))
14236 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14238 /* The section has already been linked. See if we should
14239 issue a warning. */
14240 if (!_bfd_handle_already_linked (sec, l, info))
14243 if (flags & SEC_GROUP)
14245 asection *first = elf_next_in_group (sec);
14246 asection *s = first;
14250 s->output_section = bfd_abs_section_ptr;
14251 /* Record which group discards it. */
14252 s->kept_section = l->sec;
14253 s = elf_next_in_group (s);
14254 /* These lists are circular. */
14264 /* A single member comdat group section may be discarded by a
14265 linkonce section and vice versa. */
14266 if ((flags & SEC_GROUP) != 0)
14268 asection *first = elf_next_in_group (sec);
14270 if (first != NULL && elf_next_in_group (first) == first)
14271 /* Check this single member group against linkonce sections. */
14272 for (l = already_linked_list->entry; l != NULL; l = l->next)
14273 if ((l->sec->flags & SEC_GROUP) == 0
14274 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14276 first->output_section = bfd_abs_section_ptr;
14277 first->kept_section = l->sec;
14278 sec->output_section = bfd_abs_section_ptr;
14283 /* Check this linkonce section against single member groups. */
14284 for (l = already_linked_list->entry; l != NULL; l = l->next)
14285 if (l->sec->flags & SEC_GROUP)
14287 asection *first = elf_next_in_group (l->sec);
14290 && elf_next_in_group (first) == first
14291 && bfd_elf_match_symbols_in_sections (first, sec, info))
14293 sec->output_section = bfd_abs_section_ptr;
14294 sec->kept_section = first;
14299 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14300 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14301 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14302 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14303 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14304 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14305 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14306 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14307 The reverse order cannot happen as there is never a bfd with only the
14308 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14309 matter as here were are looking only for cross-bfd sections. */
14311 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14312 for (l = already_linked_list->entry; l != NULL; l = l->next)
14313 if ((l->sec->flags & SEC_GROUP) == 0
14314 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14316 if (abfd != l->sec->owner)
14317 sec->output_section = bfd_abs_section_ptr;
14321 /* This is the first section with this name. Record it. */
14322 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14323 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14324 return sec->output_section == bfd_abs_section_ptr;
14328 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14330 return sym->st_shndx == SHN_COMMON;
14334 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14340 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14342 return bfd_com_section_ptr;
14346 _bfd_elf_default_got_elt_size (bfd *abfd,
14347 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14348 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14349 bfd *ibfd ATTRIBUTE_UNUSED,
14350 unsigned long symndx ATTRIBUTE_UNUSED)
14352 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14353 return bed->s->arch_size / 8;
14356 /* Routines to support the creation of dynamic relocs. */
14358 /* Returns the name of the dynamic reloc section associated with SEC. */
14360 static const char *
14361 get_dynamic_reloc_section_name (bfd * abfd,
14363 bfd_boolean is_rela)
14366 const char *old_name = bfd_get_section_name (NULL, sec);
14367 const char *prefix = is_rela ? ".rela" : ".rel";
14369 if (old_name == NULL)
14372 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14373 sprintf (name, "%s%s", prefix, old_name);
14378 /* Returns the dynamic reloc section associated with SEC.
14379 If necessary compute the name of the dynamic reloc section based
14380 on SEC's name (looked up in ABFD's string table) and the setting
14384 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14386 bfd_boolean is_rela)
14388 asection * reloc_sec = elf_section_data (sec)->sreloc;
14390 if (reloc_sec == NULL)
14392 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14396 reloc_sec = bfd_get_linker_section (abfd, name);
14398 if (reloc_sec != NULL)
14399 elf_section_data (sec)->sreloc = reloc_sec;
14406 /* Returns the dynamic reloc section associated with SEC. If the
14407 section does not exist it is created and attached to the DYNOBJ
14408 bfd and stored in the SRELOC field of SEC's elf_section_data
14411 ALIGNMENT is the alignment for the newly created section and
14412 IS_RELA defines whether the name should be .rela.<SEC's name>
14413 or .rel.<SEC's name>. The section name is looked up in the
14414 string table associated with ABFD. */
14417 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14419 unsigned int alignment,
14421 bfd_boolean is_rela)
14423 asection * reloc_sec = elf_section_data (sec)->sreloc;
14425 if (reloc_sec == NULL)
14427 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14432 reloc_sec = bfd_get_linker_section (dynobj, name);
14434 if (reloc_sec == NULL)
14436 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14437 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14438 if ((sec->flags & SEC_ALLOC) != 0)
14439 flags |= SEC_ALLOC | SEC_LOAD;
14441 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14442 if (reloc_sec != NULL)
14444 /* _bfd_elf_get_sec_type_attr chooses a section type by
14445 name. Override as it may be wrong, eg. for a user
14446 section named "auto" we'll get ".relauto" which is
14447 seen to be a .rela section. */
14448 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14449 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14454 elf_section_data (sec)->sreloc = reloc_sec;
14460 /* Copy the ELF symbol type and other attributes for a linker script
14461 assignment from HSRC to HDEST. Generally this should be treated as
14462 if we found a strong non-dynamic definition for HDEST (except that
14463 ld ignores multiple definition errors). */
14465 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14466 struct bfd_link_hash_entry *hdest,
14467 struct bfd_link_hash_entry *hsrc)
14469 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14470 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14471 Elf_Internal_Sym isym;
14473 ehdest->type = ehsrc->type;
14474 ehdest->target_internal = ehsrc->target_internal;
14476 isym.st_other = ehsrc->other;
14477 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14480 /* Append a RELA relocation REL to section S in BFD. */
14483 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14485 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14486 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14487 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14488 bed->s->swap_reloca_out (abfd, rel, loc);
14491 /* Append a REL relocation REL to section S in BFD. */
14494 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14496 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14497 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14498 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14499 bed->s->swap_reloc_out (abfd, rel, loc);
14502 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14504 struct bfd_link_hash_entry *
14505 bfd_elf_define_start_stop (struct bfd_link_info *info,
14506 const char *symbol, asection *sec)
14508 struct elf_link_hash_entry *h;
14510 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14511 FALSE, FALSE, TRUE);
14513 && (h->root.type == bfd_link_hash_undefined
14514 || h->root.type == bfd_link_hash_undefweak
14515 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14517 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14518 h->root.type = bfd_link_hash_defined;
14519 h->root.u.def.section = sec;
14520 h->root.u.def.value = 0;
14521 h->def_regular = 1;
14522 h->def_dynamic = 0;
14524 h->u2.start_stop_section = sec;
14525 if (symbol[0] == '.')
14527 /* .startof. and .sizeof. symbols are local. */
14528 const struct elf_backend_data *bed;
14529 bed = get_elf_backend_data (info->output_bfd);
14530 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14534 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14535 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14537 bfd_elf_link_record_dynamic_symbol (info, h);