1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2018 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
96 && discard ? discarded_section (isec) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
233 hash_table->dynobj = abfd;
236 if (hash_table->dynstr == NULL)
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
260 if (! is_elf_hash_table (info->hash))
263 if (elf_hash_table (info)->dynamic_sections_created)
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
272 flags = bed->dynamic_sec_flags;
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
295 || ! bfd_set_section_alignment (abfd, s, 1))
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 elf_hash_table (info)->dynsym = s;
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
342 if (info->emit_gnu_hash)
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
370 /* Create dynamic sections when linking against a dynamic object. */
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 if (bed->want_plt_sym)
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
422 if (! _bfd_elf_create_got_section (abfd, info))
425 if (bed->want_dynbss)
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (bed->want_dynrelro)
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
462 if (bfd_link_executable (info))
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
473 if (bed->want_dynrelro)
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
483 htab->sreldynrelro = s;
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
503 if (h->dynindx == -1)
505 struct elf_strtab_hash *dynstr;
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
522 if (!elf_hash_table (info)->is_relocatable_executable)
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
533 dynstr = elf_hash_table (info)->dynstr;
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
542 /* We don't put any version information in the dynamic string
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
559 if (indx == (size_t) -1)
561 h->dynstr_index = indx;
567 /* Mark a symbol dynamic. */
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
591 /* NB: If a symbol is made dynamic by --dynamic-list, it has
593 h->root.non_ir_ref_dynamic = 1;
597 /* Record an assignment to a symbol made by a linker script. We need
598 this in case some dynamic object refers to this symbol. */
601 bfd_elf_record_link_assignment (bfd *output_bfd,
602 struct bfd_link_info *info,
607 struct elf_link_hash_entry *h, *hv;
608 struct elf_link_hash_table *htab;
609 const struct elf_backend_data *bed;
611 if (!is_elf_hash_table (info->hash))
614 htab = elf_hash_table (info);
615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
619 if (h->root.type == bfd_link_hash_warning)
620 h = (struct elf_link_hash_entry *) h->root.u.i.link;
622 if (h->versioned == unknown)
624 /* Set versioned if symbol version is unknown. */
625 char *version = strrchr (name, ELF_VER_CHR);
628 if (version > name && version[-1] != ELF_VER_CHR)
629 h->versioned = versioned_hidden;
631 h->versioned = versioned;
635 /* Symbols defined in a linker script but not referenced anywhere
636 else will have non_elf set. */
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
643 switch (h->root.type)
645 case bfd_link_hash_defined:
646 case bfd_link_hash_defweak:
647 case bfd_link_hash_common:
649 case bfd_link_hash_undefweak:
650 case bfd_link_hash_undefined:
651 /* Since we're defining the symbol, don't let it seem to have not
652 been defined. record_dynamic_symbol and size_dynamic_sections
653 may depend on this. */
654 h->root.type = bfd_link_hash_new;
655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
656 bfd_link_repair_undef_list (&htab->root);
658 case bfd_link_hash_new:
660 case bfd_link_hash_indirect:
661 /* We had a versioned symbol in a dynamic library. We make the
662 the versioned symbol point to this one. */
663 bed = get_elf_backend_data (output_bfd);
665 while (hv->root.type == bfd_link_hash_indirect
666 || hv->root.type == bfd_link_hash_warning)
667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
668 /* We don't need to update h->root.u since linker will set them
670 h->root.type = bfd_link_hash_undefined;
671 hv->root.type = bfd_link_hash_indirect;
672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
680 /* If this symbol is being provided by the linker script, and it is
681 currently defined by a dynamic object, but not by a regular
682 object, then mark it as undefined so that the generic linker will
683 force the correct value. */
687 h->root.type = bfd_link_hash_undefined;
689 /* If this symbol is not being provided by the linker script, and it is
690 currently defined by a dynamic object, but not by a regular object,
691 then clear out any version information because the symbol will not be
692 associated with the dynamic object any more. */
696 h->verinfo.verdef = NULL;
698 /* Make sure this symbol is not garbage collected. */
705 bed = get_elf_backend_data (output_bfd);
706 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
707 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
708 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
711 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
713 if (!bfd_link_relocatable (info)
715 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
716 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
721 || bfd_link_dll (info)
722 || elf_hash_table (info)->is_relocatable_executable)
726 if (! bfd_elf_link_record_dynamic_symbol (info, h))
729 /* If this is a weak defined symbol, and we know a corresponding
730 real symbol from the same dynamic object, make sure the real
731 symbol is also made into a dynamic symbol. */
734 struct elf_link_hash_entry *def = weakdef (h);
736 if (def->dynindx == -1
737 && !bfd_elf_link_record_dynamic_symbol (info, def))
745 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
746 success, and 2 on a failure caused by attempting to record a symbol
747 in a discarded section, eg. a discarded link-once section symbol. */
750 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
755 struct elf_link_local_dynamic_entry *entry;
756 struct elf_link_hash_table *eht;
757 struct elf_strtab_hash *dynstr;
760 Elf_External_Sym_Shndx eshndx;
761 char esym[sizeof (Elf64_External_Sym)];
763 if (! is_elf_hash_table (info->hash))
766 /* See if the entry exists already. */
767 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
768 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
771 amt = sizeof (*entry);
772 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
776 /* Go find the symbol, so that we can find it's name. */
777 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
778 1, input_indx, &entry->isym, esym, &eshndx))
780 bfd_release (input_bfd, entry);
784 if (entry->isym.st_shndx != SHN_UNDEF
785 && entry->isym.st_shndx < SHN_LORESERVE)
789 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
790 if (s == NULL || bfd_is_abs_section (s->output_section))
792 /* We can still bfd_release here as nothing has done another
793 bfd_alloc. We can't do this later in this function. */
794 bfd_release (input_bfd, entry);
799 name = (bfd_elf_string_from_elf_section
800 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
801 entry->isym.st_name));
803 dynstr = elf_hash_table (info)->dynstr;
806 /* Create a strtab to hold the dynamic symbol names. */
807 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
812 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
813 if (dynstr_index == (size_t) -1)
815 entry->isym.st_name = dynstr_index;
817 eht = elf_hash_table (info);
819 entry->next = eht->dynlocal;
820 eht->dynlocal = entry;
821 entry->input_bfd = input_bfd;
822 entry->input_indx = input_indx;
825 /* Whatever binding the symbol had before, it's now local. */
827 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
829 /* The dynindx will be set at the end of size_dynamic_sections. */
834 /* Return the dynindex of a local dynamic symbol. */
837 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
841 struct elf_link_local_dynamic_entry *e;
843 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
844 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
849 /* This function is used to renumber the dynamic symbols, if some of
850 them are removed because they are marked as local. This is called
851 via elf_link_hash_traverse. */
854 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
857 size_t *count = (size_t *) data;
862 if (h->dynindx != -1)
863 h->dynindx = ++(*count);
869 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
870 STB_LOCAL binding. */
873 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
876 size_t *count = (size_t *) data;
878 if (!h->forced_local)
881 if (h->dynindx != -1)
882 h->dynindx = ++(*count);
887 /* Return true if the dynamic symbol for a given section should be
888 omitted when creating a shared library. */
890 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
891 struct bfd_link_info *info,
894 struct elf_link_hash_table *htab;
897 switch (elf_section_data (p)->this_hdr.sh_type)
901 /* If sh_type is yet undecided, assume it could be
902 SHT_PROGBITS/SHT_NOBITS. */
904 htab = elf_hash_table (info);
905 if (p == htab->tls_sec)
908 if (htab->text_index_section != NULL)
909 return p != htab->text_index_section && p != htab->data_index_section;
911 return (htab->dynobj != NULL
912 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
913 && ip->output_section == p);
915 /* There shouldn't be section relative relocations
916 against any other section. */
923 _bfd_elf_omit_section_dynsym_all
924 (bfd *output_bfd ATTRIBUTE_UNUSED,
925 struct bfd_link_info *info ATTRIBUTE_UNUSED,
926 asection *p ATTRIBUTE_UNUSED)
931 /* Assign dynsym indices. In a shared library we generate a section
932 symbol for each output section, which come first. Next come symbols
933 which have been forced to local binding. Then all of the back-end
934 allocated local dynamic syms, followed by the rest of the global
935 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
936 (This prevents the early call before elf_backend_init_index_section
937 and strip_excluded_output_sections setting dynindx for sections
938 that are stripped.) */
941 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
942 struct bfd_link_info *info,
943 unsigned long *section_sym_count)
945 unsigned long dynsymcount = 0;
946 bfd_boolean do_sec = section_sym_count != NULL;
948 if (bfd_link_pic (info)
949 || elf_hash_table (info)->is_relocatable_executable)
951 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
953 for (p = output_bfd->sections; p ; p = p->next)
954 if ((p->flags & SEC_EXCLUDE) == 0
955 && (p->flags & SEC_ALLOC) != 0
956 && elf_hash_table (info)->dynamic_relocs
957 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
961 elf_section_data (p)->dynindx = dynsymcount;
964 elf_section_data (p)->dynindx = 0;
967 *section_sym_count = dynsymcount;
969 elf_link_hash_traverse (elf_hash_table (info),
970 elf_link_renumber_local_hash_table_dynsyms,
973 if (elf_hash_table (info)->dynlocal)
975 struct elf_link_local_dynamic_entry *p;
976 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
977 p->dynindx = ++dynsymcount;
979 elf_hash_table (info)->local_dynsymcount = dynsymcount;
981 elf_link_hash_traverse (elf_hash_table (info),
982 elf_link_renumber_hash_table_dynsyms,
985 /* There is an unused NULL entry at the head of the table which we
986 must account for in our count even if the table is empty since it
987 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
991 elf_hash_table (info)->dynsymcount = dynsymcount;
995 /* Merge st_other field. */
998 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
999 const Elf_Internal_Sym *isym, asection *sec,
1000 bfd_boolean definition, bfd_boolean dynamic)
1002 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1004 /* If st_other has a processor-specific meaning, specific
1005 code might be needed here. */
1006 if (bed->elf_backend_merge_symbol_attribute)
1007 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1012 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1013 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1015 /* Keep the most constraining visibility. Leave the remainder
1016 of the st_other field to elf_backend_merge_symbol_attribute. */
1017 if (symvis - 1 < hvis - 1)
1018 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1021 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1022 && (sec->flags & SEC_READONLY) == 0)
1023 h->protected_def = 1;
1026 /* This function is called when we want to merge a new symbol with an
1027 existing symbol. It handles the various cases which arise when we
1028 find a definition in a dynamic object, or when there is already a
1029 definition in a dynamic object. The new symbol is described by
1030 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1031 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1032 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1033 of an old common symbol. We set OVERRIDE if the old symbol is
1034 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1035 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1036 to change. By OK to change, we mean that we shouldn't warn if the
1037 type or size does change. */
1040 _bfd_elf_merge_symbol (bfd *abfd,
1041 struct bfd_link_info *info,
1043 Elf_Internal_Sym *sym,
1046 struct elf_link_hash_entry **sym_hash,
1048 bfd_boolean *pold_weak,
1049 unsigned int *pold_alignment,
1051 bfd_boolean *override,
1052 bfd_boolean *type_change_ok,
1053 bfd_boolean *size_change_ok,
1054 bfd_boolean *matched)
1056 asection *sec, *oldsec;
1057 struct elf_link_hash_entry *h;
1058 struct elf_link_hash_entry *hi;
1059 struct elf_link_hash_entry *flip;
1062 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1063 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1064 const struct elf_backend_data *bed;
1066 bfd_boolean default_sym = *matched;
1072 bind = ELF_ST_BIND (sym->st_info);
1074 if (! bfd_is_und_section (sec))
1075 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1077 h = ((struct elf_link_hash_entry *)
1078 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1083 bed = get_elf_backend_data (abfd);
1085 /* NEW_VERSION is the symbol version of the new symbol. */
1086 if (h->versioned != unversioned)
1088 /* Symbol version is unknown or versioned. */
1089 new_version = strrchr (name, ELF_VER_CHR);
1092 if (h->versioned == unknown)
1094 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1095 h->versioned = versioned_hidden;
1097 h->versioned = versioned;
1100 if (new_version[0] == '\0')
1104 h->versioned = unversioned;
1109 /* For merging, we only care about real symbols. But we need to make
1110 sure that indirect symbol dynamic flags are updated. */
1112 while (h->root.type == bfd_link_hash_indirect
1113 || h->root.type == bfd_link_hash_warning)
1114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1118 if (hi == h || h->root.type == bfd_link_hash_new)
1122 /* OLD_HIDDEN is true if the existing symbol is only visible
1123 to the symbol with the same symbol version. NEW_HIDDEN is
1124 true if the new symbol is only visible to the symbol with
1125 the same symbol version. */
1126 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1127 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1128 if (!old_hidden && !new_hidden)
1129 /* The new symbol matches the existing symbol if both
1134 /* OLD_VERSION is the symbol version of the existing
1138 if (h->versioned >= versioned)
1139 old_version = strrchr (h->root.root.string,
1144 /* The new symbol matches the existing symbol if they
1145 have the same symbol version. */
1146 *matched = (old_version == new_version
1147 || (old_version != NULL
1148 && new_version != NULL
1149 && strcmp (old_version, new_version) == 0));
1154 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1159 switch (h->root.type)
1164 case bfd_link_hash_undefined:
1165 case bfd_link_hash_undefweak:
1166 oldbfd = h->root.u.undef.abfd;
1169 case bfd_link_hash_defined:
1170 case bfd_link_hash_defweak:
1171 oldbfd = h->root.u.def.section->owner;
1172 oldsec = h->root.u.def.section;
1175 case bfd_link_hash_common:
1176 oldbfd = h->root.u.c.p->section->owner;
1177 oldsec = h->root.u.c.p->section;
1179 *pold_alignment = h->root.u.c.p->alignment_power;
1182 if (poldbfd && *poldbfd == NULL)
1185 /* Differentiate strong and weak symbols. */
1186 newweak = bind == STB_WEAK;
1187 oldweak = (h->root.type == bfd_link_hash_defweak
1188 || h->root.type == bfd_link_hash_undefweak);
1190 *pold_weak = oldweak;
1192 /* We have to check it for every instance since the first few may be
1193 references and not all compilers emit symbol type for undefined
1195 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1197 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1198 respectively, is from a dynamic object. */
1200 newdyn = (abfd->flags & DYNAMIC) != 0;
1202 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1203 syms and defined syms in dynamic libraries respectively.
1204 ref_dynamic on the other hand can be set for a symbol defined in
1205 a dynamic library, and def_dynamic may not be set; When the
1206 definition in a dynamic lib is overridden by a definition in the
1207 executable use of the symbol in the dynamic lib becomes a
1208 reference to the executable symbol. */
1211 if (bfd_is_und_section (sec))
1213 if (bind != STB_WEAK)
1215 h->ref_dynamic_nonweak = 1;
1216 hi->ref_dynamic_nonweak = 1;
1221 /* Update the existing symbol only if they match. */
1224 hi->dynamic_def = 1;
1228 /* If we just created the symbol, mark it as being an ELF symbol.
1229 Other than that, there is nothing to do--there is no merge issue
1230 with a newly defined symbol--so we just return. */
1232 if (h->root.type == bfd_link_hash_new)
1238 /* In cases involving weak versioned symbols, we may wind up trying
1239 to merge a symbol with itself. Catch that here, to avoid the
1240 confusion that results if we try to override a symbol with
1241 itself. The additional tests catch cases like
1242 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1243 dynamic object, which we do want to handle here. */
1245 && (newweak || oldweak)
1246 && ((abfd->flags & DYNAMIC) == 0
1247 || !h->def_regular))
1252 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1253 else if (oldsec != NULL)
1255 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1256 indices used by MIPS ELF. */
1257 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1260 /* Handle a case where plugin_notice won't be called and thus won't
1261 set the non_ir_ref flags on the first pass over symbols. */
1263 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1264 && newdyn != olddyn)
1266 h->root.non_ir_ref_dynamic = TRUE;
1267 hi->root.non_ir_ref_dynamic = TRUE;
1270 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1271 respectively, appear to be a definition rather than reference. */
1273 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1275 olddef = (h->root.type != bfd_link_hash_undefined
1276 && h->root.type != bfd_link_hash_undefweak
1277 && h->root.type != bfd_link_hash_common);
1279 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1280 respectively, appear to be a function. */
1282 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1283 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1285 oldfunc = (h->type != STT_NOTYPE
1286 && bed->is_function_type (h->type));
1288 if (!(newfunc && oldfunc)
1289 && ELF_ST_TYPE (sym->st_info) != h->type
1290 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1291 && h->type != STT_NOTYPE
1292 && (newdef || bfd_is_com_section (sec))
1293 && (olddef || h->root.type == bfd_link_hash_common))
1295 /* If creating a default indirect symbol ("foo" or "foo@") from
1296 a dynamic versioned definition ("foo@@") skip doing so if
1297 there is an existing regular definition with a different
1298 type. We don't want, for example, a "time" variable in the
1299 executable overriding a "time" function in a shared library. */
1307 /* When adding a symbol from a regular object file after we have
1308 created indirect symbols, undo the indirection and any
1315 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1316 h->forced_local = 0;
1320 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1322 h->root.type = bfd_link_hash_undefined;
1323 h->root.u.undef.abfd = abfd;
1327 h->root.type = bfd_link_hash_new;
1328 h->root.u.undef.abfd = NULL;
1334 /* Check TLS symbols. We don't check undefined symbols introduced
1335 by "ld -u" which have no type (and oldbfd NULL), and we don't
1336 check symbols from plugins because they also have no type. */
1338 && (oldbfd->flags & BFD_PLUGIN) == 0
1339 && (abfd->flags & BFD_PLUGIN) == 0
1340 && ELF_ST_TYPE (sym->st_info) != h->type
1341 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1344 bfd_boolean ntdef, tdef;
1345 asection *ntsec, *tsec;
1347 if (h->type == STT_TLS)
1368 /* xgettext:c-format */
1369 (_("%s: TLS definition in %pB section %pA "
1370 "mismatches non-TLS definition in %pB section %pA"),
1371 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1372 else if (!tdef && !ntdef)
1374 /* xgettext:c-format */
1375 (_("%s: TLS reference in %pB "
1376 "mismatches non-TLS reference in %pB"),
1377 h->root.root.string, tbfd, ntbfd);
1380 /* xgettext:c-format */
1381 (_("%s: TLS definition in %pB section %pA "
1382 "mismatches non-TLS reference in %pB"),
1383 h->root.root.string, tbfd, tsec, ntbfd);
1386 /* xgettext:c-format */
1387 (_("%s: TLS reference in %pB "
1388 "mismatches non-TLS definition in %pB section %pA"),
1389 h->root.root.string, tbfd, ntbfd, ntsec);
1391 bfd_set_error (bfd_error_bad_value);
1395 /* If the old symbol has non-default visibility, we ignore the new
1396 definition from a dynamic object. */
1398 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1399 && !bfd_is_und_section (sec))
1402 /* Make sure this symbol is dynamic. */
1404 hi->ref_dynamic = 1;
1405 /* A protected symbol has external availability. Make sure it is
1406 recorded as dynamic.
1408 FIXME: Should we check type and size for protected symbol? */
1409 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1410 return bfd_elf_link_record_dynamic_symbol (info, h);
1415 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1418 /* If the new symbol with non-default visibility comes from a
1419 relocatable file and the old definition comes from a dynamic
1420 object, we remove the old definition. */
1421 if (hi->root.type == bfd_link_hash_indirect)
1423 /* Handle the case where the old dynamic definition is
1424 default versioned. We need to copy the symbol info from
1425 the symbol with default version to the normal one if it
1426 was referenced before. */
1429 hi->root.type = h->root.type;
1430 h->root.type = bfd_link_hash_indirect;
1431 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1433 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1434 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1436 /* If the new symbol is hidden or internal, completely undo
1437 any dynamic link state. */
1438 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1439 h->forced_local = 0;
1446 /* FIXME: Should we check type and size for protected symbol? */
1456 /* If the old symbol was undefined before, then it will still be
1457 on the undefs list. If the new symbol is undefined or
1458 common, we can't make it bfd_link_hash_new here, because new
1459 undefined or common symbols will be added to the undefs list
1460 by _bfd_generic_link_add_one_symbol. Symbols may not be
1461 added twice to the undefs list. Also, if the new symbol is
1462 undefweak then we don't want to lose the strong undef. */
1463 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1465 h->root.type = bfd_link_hash_undefined;
1466 h->root.u.undef.abfd = abfd;
1470 h->root.type = bfd_link_hash_new;
1471 h->root.u.undef.abfd = NULL;
1474 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1476 /* If the new symbol is hidden or internal, completely undo
1477 any dynamic link state. */
1478 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1479 h->forced_local = 0;
1485 /* FIXME: Should we check type and size for protected symbol? */
1491 /* If a new weak symbol definition comes from a regular file and the
1492 old symbol comes from a dynamic library, we treat the new one as
1493 strong. Similarly, an old weak symbol definition from a regular
1494 file is treated as strong when the new symbol comes from a dynamic
1495 library. Further, an old weak symbol from a dynamic library is
1496 treated as strong if the new symbol is from a dynamic library.
1497 This reflects the way glibc's ld.so works.
1499 Also allow a weak symbol to override a linker script symbol
1500 defined by an early pass over the script. This is done so the
1501 linker knows the symbol is defined in an object file, for the
1502 DEFINED script function.
1504 Do this before setting *type_change_ok or *size_change_ok so that
1505 we warn properly when dynamic library symbols are overridden. */
1507 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1509 if (olddef && newdyn)
1512 /* Allow changes between different types of function symbol. */
1513 if (newfunc && oldfunc)
1514 *type_change_ok = TRUE;
1516 /* It's OK to change the type if either the existing symbol or the
1517 new symbol is weak. A type change is also OK if the old symbol
1518 is undefined and the new symbol is defined. */
1523 && h->root.type == bfd_link_hash_undefined))
1524 *type_change_ok = TRUE;
1526 /* It's OK to change the size if either the existing symbol or the
1527 new symbol is weak, or if the old symbol is undefined. */
1530 || h->root.type == bfd_link_hash_undefined)
1531 *size_change_ok = TRUE;
1533 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1534 symbol, respectively, appears to be a common symbol in a dynamic
1535 object. If a symbol appears in an uninitialized section, and is
1536 not weak, and is not a function, then it may be a common symbol
1537 which was resolved when the dynamic object was created. We want
1538 to treat such symbols specially, because they raise special
1539 considerations when setting the symbol size: if the symbol
1540 appears as a common symbol in a regular object, and the size in
1541 the regular object is larger, we must make sure that we use the
1542 larger size. This problematic case can always be avoided in C,
1543 but it must be handled correctly when using Fortran shared
1546 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1547 likewise for OLDDYNCOMMON and OLDDEF.
1549 Note that this test is just a heuristic, and that it is quite
1550 possible to have an uninitialized symbol in a shared object which
1551 is really a definition, rather than a common symbol. This could
1552 lead to some minor confusion when the symbol really is a common
1553 symbol in some regular object. However, I think it will be
1559 && (sec->flags & SEC_ALLOC) != 0
1560 && (sec->flags & SEC_LOAD) == 0
1563 newdyncommon = TRUE;
1565 newdyncommon = FALSE;
1569 && h->root.type == bfd_link_hash_defined
1571 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1572 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1575 olddyncommon = TRUE;
1577 olddyncommon = FALSE;
1579 /* We now know everything about the old and new symbols. We ask the
1580 backend to check if we can merge them. */
1581 if (bed->merge_symbol != NULL)
1583 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1588 /* There are multiple definitions of a normal symbol. Skip the
1589 default symbol as well as definition from an IR object. */
1590 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1591 && !default_sym && h->def_regular
1593 && (oldbfd->flags & BFD_PLUGIN) != 0
1594 && (abfd->flags & BFD_PLUGIN) == 0))
1596 /* Handle a multiple definition. */
1597 (*info->callbacks->multiple_definition) (info, &h->root,
1598 abfd, sec, *pvalue);
1603 /* If both the old and the new symbols look like common symbols in a
1604 dynamic object, set the size of the symbol to the larger of the
1609 && sym->st_size != h->size)
1611 /* Since we think we have two common symbols, issue a multiple
1612 common warning if desired. Note that we only warn if the
1613 size is different. If the size is the same, we simply let
1614 the old symbol override the new one as normally happens with
1615 symbols defined in dynamic objects. */
1617 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1618 bfd_link_hash_common, sym->st_size);
1619 if (sym->st_size > h->size)
1620 h->size = sym->st_size;
1622 *size_change_ok = TRUE;
1625 /* If we are looking at a dynamic object, and we have found a
1626 definition, we need to see if the symbol was already defined by
1627 some other object. If so, we want to use the existing
1628 definition, and we do not want to report a multiple symbol
1629 definition error; we do this by clobbering *PSEC to be
1630 bfd_und_section_ptr.
1632 We treat a common symbol as a definition if the symbol in the
1633 shared library is a function, since common symbols always
1634 represent variables; this can cause confusion in principle, but
1635 any such confusion would seem to indicate an erroneous program or
1636 shared library. We also permit a common symbol in a regular
1637 object to override a weak symbol in a shared object. */
1642 || (h->root.type == bfd_link_hash_common
1643 && (newweak || newfunc))))
1647 newdyncommon = FALSE;
1649 *psec = sec = bfd_und_section_ptr;
1650 *size_change_ok = TRUE;
1652 /* If we get here when the old symbol is a common symbol, then
1653 we are explicitly letting it override a weak symbol or
1654 function in a dynamic object, and we don't want to warn about
1655 a type change. If the old symbol is a defined symbol, a type
1656 change warning may still be appropriate. */
1658 if (h->root.type == bfd_link_hash_common)
1659 *type_change_ok = TRUE;
1662 /* Handle the special case of an old common symbol merging with a
1663 new symbol which looks like a common symbol in a shared object.
1664 We change *PSEC and *PVALUE to make the new symbol look like a
1665 common symbol, and let _bfd_generic_link_add_one_symbol do the
1669 && h->root.type == bfd_link_hash_common)
1673 newdyncommon = FALSE;
1674 *pvalue = sym->st_size;
1675 *psec = sec = bed->common_section (oldsec);
1676 *size_change_ok = TRUE;
1679 /* Skip weak definitions of symbols that are already defined. */
1680 if (newdef && olddef && newweak)
1682 /* Don't skip new non-IR weak syms. */
1683 if (!(oldbfd != NULL
1684 && (oldbfd->flags & BFD_PLUGIN) != 0
1685 && (abfd->flags & BFD_PLUGIN) == 0))
1691 /* Merge st_other. If the symbol already has a dynamic index,
1692 but visibility says it should not be visible, turn it into a
1694 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1695 if (h->dynindx != -1)
1696 switch (ELF_ST_VISIBILITY (h->other))
1700 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1705 /* If the old symbol is from a dynamic object, and the new symbol is
1706 a definition which is not from a dynamic object, then the new
1707 symbol overrides the old symbol. Symbols from regular files
1708 always take precedence over symbols from dynamic objects, even if
1709 they are defined after the dynamic object in the link.
1711 As above, we again permit a common symbol in a regular object to
1712 override a definition in a shared object if the shared object
1713 symbol is a function or is weak. */
1718 || (bfd_is_com_section (sec)
1719 && (oldweak || oldfunc)))
1724 /* Change the hash table entry to undefined, and let
1725 _bfd_generic_link_add_one_symbol do the right thing with the
1728 h->root.type = bfd_link_hash_undefined;
1729 h->root.u.undef.abfd = h->root.u.def.section->owner;
1730 *size_change_ok = TRUE;
1733 olddyncommon = FALSE;
1735 /* We again permit a type change when a common symbol may be
1736 overriding a function. */
1738 if (bfd_is_com_section (sec))
1742 /* If a common symbol overrides a function, make sure
1743 that it isn't defined dynamically nor has type
1746 h->type = STT_NOTYPE;
1748 *type_change_ok = TRUE;
1751 if (hi->root.type == bfd_link_hash_indirect)
1754 /* This union may have been set to be non-NULL when this symbol
1755 was seen in a dynamic object. We must force the union to be
1756 NULL, so that it is correct for a regular symbol. */
1757 h->verinfo.vertree = NULL;
1760 /* Handle the special case of a new common symbol merging with an
1761 old symbol that looks like it might be a common symbol defined in
1762 a shared object. Note that we have already handled the case in
1763 which a new common symbol should simply override the definition
1764 in the shared library. */
1767 && bfd_is_com_section (sec)
1770 /* It would be best if we could set the hash table entry to a
1771 common symbol, but we don't know what to use for the section
1772 or the alignment. */
1773 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1774 bfd_link_hash_common, sym->st_size);
1776 /* If the presumed common symbol in the dynamic object is
1777 larger, pretend that the new symbol has its size. */
1779 if (h->size > *pvalue)
1782 /* We need to remember the alignment required by the symbol
1783 in the dynamic object. */
1784 BFD_ASSERT (pold_alignment);
1785 *pold_alignment = h->root.u.def.section->alignment_power;
1788 olddyncommon = FALSE;
1790 h->root.type = bfd_link_hash_undefined;
1791 h->root.u.undef.abfd = h->root.u.def.section->owner;
1793 *size_change_ok = TRUE;
1794 *type_change_ok = TRUE;
1796 if (hi->root.type == bfd_link_hash_indirect)
1799 h->verinfo.vertree = NULL;
1804 /* Handle the case where we had a versioned symbol in a dynamic
1805 library and now find a definition in a normal object. In this
1806 case, we make the versioned symbol point to the normal one. */
1807 flip->root.type = h->root.type;
1808 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1809 h->root.type = bfd_link_hash_indirect;
1810 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1811 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1815 flip->ref_dynamic = 1;
1822 /* This function is called to create an indirect symbol from the
1823 default for the symbol with the default version if needed. The
1824 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1825 set DYNSYM if the new indirect symbol is dynamic. */
1828 _bfd_elf_add_default_symbol (bfd *abfd,
1829 struct bfd_link_info *info,
1830 struct elf_link_hash_entry *h,
1832 Elf_Internal_Sym *sym,
1836 bfd_boolean *dynsym)
1838 bfd_boolean type_change_ok;
1839 bfd_boolean size_change_ok;
1842 struct elf_link_hash_entry *hi;
1843 struct bfd_link_hash_entry *bh;
1844 const struct elf_backend_data *bed;
1845 bfd_boolean collect;
1846 bfd_boolean dynamic;
1847 bfd_boolean override;
1849 size_t len, shortlen;
1851 bfd_boolean matched;
1853 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1856 /* If this symbol has a version, and it is the default version, we
1857 create an indirect symbol from the default name to the fully
1858 decorated name. This will cause external references which do not
1859 specify a version to be bound to this version of the symbol. */
1860 p = strchr (name, ELF_VER_CHR);
1861 if (h->versioned == unknown)
1865 h->versioned = unversioned;
1870 if (p[1] != ELF_VER_CHR)
1872 h->versioned = versioned_hidden;
1876 h->versioned = versioned;
1881 /* PR ld/19073: We may see an unversioned definition after the
1887 bed = get_elf_backend_data (abfd);
1888 collect = bed->collect;
1889 dynamic = (abfd->flags & DYNAMIC) != 0;
1891 shortlen = p - name;
1892 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1893 if (shortname == NULL)
1895 memcpy (shortname, name, shortlen);
1896 shortname[shortlen] = '\0';
1898 /* We are going to create a new symbol. Merge it with any existing
1899 symbol with this name. For the purposes of the merge, act as
1900 though we were defining the symbol we just defined, although we
1901 actually going to define an indirect symbol. */
1902 type_change_ok = FALSE;
1903 size_change_ok = FALSE;
1906 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1907 &hi, poldbfd, NULL, NULL, &skip, &override,
1908 &type_change_ok, &size_change_ok, &matched))
1914 if (hi->def_regular)
1916 /* If the undecorated symbol will have a version added by a
1917 script different to H, then don't indirect to/from the
1918 undecorated symbol. This isn't ideal because we may not yet
1919 have seen symbol versions, if given by a script on the
1920 command line rather than via --version-script. */
1921 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1926 = bfd_find_version_for_sym (info->version_info,
1927 hi->root.root.string, &hide);
1928 if (hi->verinfo.vertree != NULL && hide)
1930 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1934 if (hi->verinfo.vertree != NULL
1935 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1941 /* Add the default symbol if not performing a relocatable link. */
1942 if (! bfd_link_relocatable (info))
1945 if (! (_bfd_generic_link_add_one_symbol
1946 (info, abfd, shortname, BSF_INDIRECT,
1947 bfd_ind_section_ptr,
1948 0, name, FALSE, collect, &bh)))
1950 hi = (struct elf_link_hash_entry *) bh;
1955 /* In this case the symbol named SHORTNAME is overriding the
1956 indirect symbol we want to add. We were planning on making
1957 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1958 is the name without a version. NAME is the fully versioned
1959 name, and it is the default version.
1961 Overriding means that we already saw a definition for the
1962 symbol SHORTNAME in a regular object, and it is overriding
1963 the symbol defined in the dynamic object.
1965 When this happens, we actually want to change NAME, the
1966 symbol we just added, to refer to SHORTNAME. This will cause
1967 references to NAME in the shared object to become references
1968 to SHORTNAME in the regular object. This is what we expect
1969 when we override a function in a shared object: that the
1970 references in the shared object will be mapped to the
1971 definition in the regular object. */
1973 while (hi->root.type == bfd_link_hash_indirect
1974 || hi->root.type == bfd_link_hash_warning)
1975 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1977 h->root.type = bfd_link_hash_indirect;
1978 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1982 hi->ref_dynamic = 1;
1986 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1991 /* Now set HI to H, so that the following code will set the
1992 other fields correctly. */
1996 /* Check if HI is a warning symbol. */
1997 if (hi->root.type == bfd_link_hash_warning)
1998 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2000 /* If there is a duplicate definition somewhere, then HI may not
2001 point to an indirect symbol. We will have reported an error to
2002 the user in that case. */
2004 if (hi->root.type == bfd_link_hash_indirect)
2006 struct elf_link_hash_entry *ht;
2008 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2009 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2011 /* A reference to the SHORTNAME symbol from a dynamic library
2012 will be satisfied by the versioned symbol at runtime. In
2013 effect, we have a reference to the versioned symbol. */
2014 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2015 hi->dynamic_def |= ht->dynamic_def;
2017 /* See if the new flags lead us to realize that the symbol must
2023 if (! bfd_link_executable (info)
2030 if (hi->ref_regular)
2036 /* We also need to define an indirection from the nondefault version
2040 len = strlen (name);
2041 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2042 if (shortname == NULL)
2044 memcpy (shortname, name, shortlen);
2045 memcpy (shortname + shortlen, p + 1, len - shortlen);
2047 /* Once again, merge with any existing symbol. */
2048 type_change_ok = FALSE;
2049 size_change_ok = FALSE;
2051 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2052 &hi, poldbfd, NULL, NULL, &skip, &override,
2053 &type_change_ok, &size_change_ok, &matched))
2061 /* Here SHORTNAME is a versioned name, so we don't expect to see
2062 the type of override we do in the case above unless it is
2063 overridden by a versioned definition. */
2064 if (hi->root.type != bfd_link_hash_defined
2065 && hi->root.type != bfd_link_hash_defweak)
2067 /* xgettext:c-format */
2068 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2074 if (! (_bfd_generic_link_add_one_symbol
2075 (info, abfd, shortname, BSF_INDIRECT,
2076 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2078 hi = (struct elf_link_hash_entry *) bh;
2080 /* If there is a duplicate definition somewhere, then HI may not
2081 point to an indirect symbol. We will have reported an error
2082 to the user in that case. */
2084 if (hi->root.type == bfd_link_hash_indirect)
2086 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2087 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2088 hi->dynamic_def |= h->dynamic_def;
2090 /* See if the new flags lead us to realize that the symbol
2096 if (! bfd_link_executable (info)
2102 if (hi->ref_regular)
2112 /* This routine is used to export all defined symbols into the dynamic
2113 symbol table. It is called via elf_link_hash_traverse. */
2116 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2118 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2120 /* Ignore indirect symbols. These are added by the versioning code. */
2121 if (h->root.type == bfd_link_hash_indirect)
2124 /* Ignore this if we won't export it. */
2125 if (!eif->info->export_dynamic && !h->dynamic)
2128 if (h->dynindx == -1
2129 && (h->def_regular || h->ref_regular)
2130 && ! bfd_hide_sym_by_version (eif->info->version_info,
2131 h->root.root.string))
2133 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2143 /* Look through the symbols which are defined in other shared
2144 libraries and referenced here. Update the list of version
2145 dependencies. This will be put into the .gnu.version_r section.
2146 This function is called via elf_link_hash_traverse. */
2149 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2152 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2153 Elf_Internal_Verneed *t;
2154 Elf_Internal_Vernaux *a;
2157 /* We only care about symbols defined in shared objects with version
2162 || h->verinfo.verdef == NULL
2163 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2164 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2167 /* See if we already know about this version. */
2168 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2172 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2175 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2176 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2182 /* This is a new version. Add it to tree we are building. */
2187 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2190 rinfo->failed = TRUE;
2194 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2195 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2196 elf_tdata (rinfo->info->output_bfd)->verref = t;
2200 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2203 rinfo->failed = TRUE;
2207 /* Note that we are copying a string pointer here, and testing it
2208 above. If bfd_elf_string_from_elf_section is ever changed to
2209 discard the string data when low in memory, this will have to be
2211 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2213 a->vna_flags = h->verinfo.verdef->vd_flags;
2214 a->vna_nextptr = t->vn_auxptr;
2216 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2219 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2226 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2227 hidden. Set *T_P to NULL if there is no match. */
2230 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2231 struct elf_link_hash_entry *h,
2232 const char *version_p,
2233 struct bfd_elf_version_tree **t_p,
2236 struct bfd_elf_version_tree *t;
2238 /* Look for the version. If we find it, it is no longer weak. */
2239 for (t = info->version_info; t != NULL; t = t->next)
2241 if (strcmp (t->name, version_p) == 0)
2245 struct bfd_elf_version_expr *d;
2247 len = version_p - h->root.root.string;
2248 alc = (char *) bfd_malloc (len);
2251 memcpy (alc, h->root.root.string, len - 1);
2252 alc[len - 1] = '\0';
2253 if (alc[len - 2] == ELF_VER_CHR)
2254 alc[len - 2] = '\0';
2256 h->verinfo.vertree = t;
2260 if (t->globals.list != NULL)
2261 d = (*t->match) (&t->globals, NULL, alc);
2263 /* See if there is anything to force this symbol to
2265 if (d == NULL && t->locals.list != NULL)
2267 d = (*t->match) (&t->locals, NULL, alc);
2270 && ! info->export_dynamic)
2284 /* Return TRUE if the symbol H is hidden by version script. */
2287 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2288 struct elf_link_hash_entry *h)
2291 bfd_boolean hide = FALSE;
2292 const struct elf_backend_data *bed
2293 = get_elf_backend_data (info->output_bfd);
2295 /* Version script only hides symbols defined in regular objects. */
2296 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2299 p = strchr (h->root.root.string, ELF_VER_CHR);
2300 if (p != NULL && h->verinfo.vertree == NULL)
2302 struct bfd_elf_version_tree *t;
2305 if (*p == ELF_VER_CHR)
2309 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2313 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2318 /* If we don't have a version for this symbol, see if we can find
2320 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2323 = bfd_find_version_for_sym (info->version_info,
2324 h->root.root.string, &hide);
2325 if (h->verinfo.vertree != NULL && hide)
2327 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2335 /* Figure out appropriate versions for all the symbols. We may not
2336 have the version number script until we have read all of the input
2337 files, so until that point we don't know which symbols should be
2338 local. This function is called via elf_link_hash_traverse. */
2341 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2343 struct elf_info_failed *sinfo;
2344 struct bfd_link_info *info;
2345 const struct elf_backend_data *bed;
2346 struct elf_info_failed eif;
2350 sinfo = (struct elf_info_failed *) data;
2353 /* Fix the symbol flags. */
2356 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2359 sinfo->failed = TRUE;
2363 /* We only need version numbers for symbols defined in regular
2365 if (!h->def_regular)
2369 bed = get_elf_backend_data (info->output_bfd);
2370 p = strchr (h->root.root.string, ELF_VER_CHR);
2371 if (p != NULL && h->verinfo.vertree == NULL)
2373 struct bfd_elf_version_tree *t;
2376 if (*p == ELF_VER_CHR)
2379 /* If there is no version string, we can just return out. */
2383 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2385 sinfo->failed = TRUE;
2390 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2392 /* If we are building an application, we need to create a
2393 version node for this version. */
2394 if (t == NULL && bfd_link_executable (info))
2396 struct bfd_elf_version_tree **pp;
2399 /* If we aren't going to export this symbol, we don't need
2400 to worry about it. */
2401 if (h->dynindx == -1)
2404 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2408 sinfo->failed = TRUE;
2413 t->name_indx = (unsigned int) -1;
2417 /* Don't count anonymous version tag. */
2418 if (sinfo->info->version_info != NULL
2419 && sinfo->info->version_info->vernum == 0)
2421 for (pp = &sinfo->info->version_info;
2425 t->vernum = version_index;
2429 h->verinfo.vertree = t;
2433 /* We could not find the version for a symbol when
2434 generating a shared archive. Return an error. */
2436 /* xgettext:c-format */
2437 (_("%pB: version node not found for symbol %s"),
2438 info->output_bfd, h->root.root.string);
2439 bfd_set_error (bfd_error_bad_value);
2440 sinfo->failed = TRUE;
2445 /* If we don't have a version for this symbol, see if we can find
2448 && h->verinfo.vertree == NULL
2449 && sinfo->info->version_info != NULL)
2452 = bfd_find_version_for_sym (sinfo->info->version_info,
2453 h->root.root.string, &hide);
2454 if (h->verinfo.vertree != NULL && hide)
2455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2461 /* Read and swap the relocs from the section indicated by SHDR. This
2462 may be either a REL or a RELA section. The relocations are
2463 translated into RELA relocations and stored in INTERNAL_RELOCS,
2464 which should have already been allocated to contain enough space.
2465 The EXTERNAL_RELOCS are a buffer where the external form of the
2466 relocations should be stored.
2468 Returns FALSE if something goes wrong. */
2471 elf_link_read_relocs_from_section (bfd *abfd,
2473 Elf_Internal_Shdr *shdr,
2474 void *external_relocs,
2475 Elf_Internal_Rela *internal_relocs)
2477 const struct elf_backend_data *bed;
2478 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2479 const bfd_byte *erela;
2480 const bfd_byte *erelaend;
2481 Elf_Internal_Rela *irela;
2482 Elf_Internal_Shdr *symtab_hdr;
2485 /* Position ourselves at the start of the section. */
2486 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2489 /* Read the relocations. */
2490 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2493 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2494 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2496 bed = get_elf_backend_data (abfd);
2498 /* Convert the external relocations to the internal format. */
2499 if (shdr->sh_entsize == bed->s->sizeof_rel)
2500 swap_in = bed->s->swap_reloc_in;
2501 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2502 swap_in = bed->s->swap_reloca_in;
2505 bfd_set_error (bfd_error_wrong_format);
2509 erela = (const bfd_byte *) external_relocs;
2510 erelaend = erela + shdr->sh_size;
2511 irela = internal_relocs;
2512 while (erela < erelaend)
2516 (*swap_in) (abfd, erela, irela);
2517 r_symndx = ELF32_R_SYM (irela->r_info);
2518 if (bed->s->arch_size == 64)
2522 if ((size_t) r_symndx >= nsyms)
2525 /* xgettext:c-format */
2526 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2527 " for offset %#" PRIx64 " in section `%pA'"),
2528 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2529 (uint64_t) irela->r_offset, sec);
2530 bfd_set_error (bfd_error_bad_value);
2534 else if (r_symndx != STN_UNDEF)
2537 /* xgettext:c-format */
2538 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2539 " for offset %#" PRIx64 " in section `%pA'"
2540 " when the object file has no symbol table"),
2541 abfd, (uint64_t) r_symndx,
2542 (uint64_t) irela->r_offset, sec);
2543 bfd_set_error (bfd_error_bad_value);
2546 irela += bed->s->int_rels_per_ext_rel;
2547 erela += shdr->sh_entsize;
2553 /* Read and swap the relocs for a section O. They may have been
2554 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2555 not NULL, they are used as buffers to read into. They are known to
2556 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2557 the return value is allocated using either malloc or bfd_alloc,
2558 according to the KEEP_MEMORY argument. If O has two relocation
2559 sections (both REL and RELA relocations), then the REL_HDR
2560 relocations will appear first in INTERNAL_RELOCS, followed by the
2561 RELA_HDR relocations. */
2564 _bfd_elf_link_read_relocs (bfd *abfd,
2566 void *external_relocs,
2567 Elf_Internal_Rela *internal_relocs,
2568 bfd_boolean keep_memory)
2570 void *alloc1 = NULL;
2571 Elf_Internal_Rela *alloc2 = NULL;
2572 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2573 struct bfd_elf_section_data *esdo = elf_section_data (o);
2574 Elf_Internal_Rela *internal_rela_relocs;
2576 if (esdo->relocs != NULL)
2577 return esdo->relocs;
2579 if (o->reloc_count == 0)
2582 if (internal_relocs == NULL)
2586 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2588 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2590 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2591 if (internal_relocs == NULL)
2595 if (external_relocs == NULL)
2597 bfd_size_type size = 0;
2600 size += esdo->rel.hdr->sh_size;
2602 size += esdo->rela.hdr->sh_size;
2604 alloc1 = bfd_malloc (size);
2607 external_relocs = alloc1;
2610 internal_rela_relocs = internal_relocs;
2613 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2617 external_relocs = (((bfd_byte *) external_relocs)
2618 + esdo->rel.hdr->sh_size);
2619 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2620 * bed->s->int_rels_per_ext_rel);
2624 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2626 internal_rela_relocs)))
2629 /* Cache the results for next time, if we can. */
2631 esdo->relocs = internal_relocs;
2636 /* Don't free alloc2, since if it was allocated we are passing it
2637 back (under the name of internal_relocs). */
2639 return internal_relocs;
2647 bfd_release (abfd, alloc2);
2654 /* Compute the size of, and allocate space for, REL_HDR which is the
2655 section header for a section containing relocations for O. */
2658 _bfd_elf_link_size_reloc_section (bfd *abfd,
2659 struct bfd_elf_section_reloc_data *reldata)
2661 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2663 /* That allows us to calculate the size of the section. */
2664 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2666 /* The contents field must last into write_object_contents, so we
2667 allocate it with bfd_alloc rather than malloc. Also since we
2668 cannot be sure that the contents will actually be filled in,
2669 we zero the allocated space. */
2670 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2671 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2674 if (reldata->hashes == NULL && reldata->count)
2676 struct elf_link_hash_entry **p;
2678 p = ((struct elf_link_hash_entry **)
2679 bfd_zmalloc (reldata->count * sizeof (*p)));
2683 reldata->hashes = p;
2689 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2690 originated from the section given by INPUT_REL_HDR) to the
2694 _bfd_elf_link_output_relocs (bfd *output_bfd,
2695 asection *input_section,
2696 Elf_Internal_Shdr *input_rel_hdr,
2697 Elf_Internal_Rela *internal_relocs,
2698 struct elf_link_hash_entry **rel_hash
2701 Elf_Internal_Rela *irela;
2702 Elf_Internal_Rela *irelaend;
2704 struct bfd_elf_section_reloc_data *output_reldata;
2705 asection *output_section;
2706 const struct elf_backend_data *bed;
2707 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2708 struct bfd_elf_section_data *esdo;
2710 output_section = input_section->output_section;
2712 bed = get_elf_backend_data (output_bfd);
2713 esdo = elf_section_data (output_section);
2714 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2716 output_reldata = &esdo->rel;
2717 swap_out = bed->s->swap_reloc_out;
2719 else if (esdo->rela.hdr
2720 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2722 output_reldata = &esdo->rela;
2723 swap_out = bed->s->swap_reloca_out;
2728 /* xgettext:c-format */
2729 (_("%pB: relocation size mismatch in %pB section %pA"),
2730 output_bfd, input_section->owner, input_section);
2731 bfd_set_error (bfd_error_wrong_format);
2735 erel = output_reldata->hdr->contents;
2736 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2737 irela = internal_relocs;
2738 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2739 * bed->s->int_rels_per_ext_rel);
2740 while (irela < irelaend)
2742 (*swap_out) (output_bfd, irela, erel);
2743 irela += bed->s->int_rels_per_ext_rel;
2744 erel += input_rel_hdr->sh_entsize;
2747 /* Bump the counter, so that we know where to add the next set of
2749 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2754 /* Make weak undefined symbols in PIE dynamic. */
2757 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2758 struct elf_link_hash_entry *h)
2760 if (bfd_link_pie (info)
2762 && h->root.type == bfd_link_hash_undefweak)
2763 return bfd_elf_link_record_dynamic_symbol (info, h);
2768 /* Fix up the flags for a symbol. This handles various cases which
2769 can only be fixed after all the input files are seen. This is
2770 currently called by both adjust_dynamic_symbol and
2771 assign_sym_version, which is unnecessary but perhaps more robust in
2772 the face of future changes. */
2775 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2776 struct elf_info_failed *eif)
2778 const struct elf_backend_data *bed;
2780 /* If this symbol was mentioned in a non-ELF file, try to set
2781 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2782 permit a non-ELF file to correctly refer to a symbol defined in
2783 an ELF dynamic object. */
2786 while (h->root.type == bfd_link_hash_indirect)
2787 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2789 if (h->root.type != bfd_link_hash_defined
2790 && h->root.type != bfd_link_hash_defweak)
2793 h->ref_regular_nonweak = 1;
2797 if (h->root.u.def.section->owner != NULL
2798 && (bfd_get_flavour (h->root.u.def.section->owner)
2799 == bfd_target_elf_flavour))
2802 h->ref_regular_nonweak = 1;
2808 if (h->dynindx == -1
2812 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2821 /* Unfortunately, NON_ELF is only correct if the symbol
2822 was first seen in a non-ELF file. Fortunately, if the symbol
2823 was first seen in an ELF file, we're probably OK unless the
2824 symbol was defined in a non-ELF file. Catch that case here.
2825 FIXME: We're still in trouble if the symbol was first seen in
2826 a dynamic object, and then later in a non-ELF regular object. */
2827 if ((h->root.type == bfd_link_hash_defined
2828 || h->root.type == bfd_link_hash_defweak)
2830 && (h->root.u.def.section->owner != NULL
2831 ? (bfd_get_flavour (h->root.u.def.section->owner)
2832 != bfd_target_elf_flavour)
2833 : (bfd_is_abs_section (h->root.u.def.section)
2834 && !h->def_dynamic)))
2838 /* Backend specific symbol fixup. */
2839 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2840 if (bed->elf_backend_fixup_symbol
2841 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2844 /* If this is a final link, and the symbol was defined as a common
2845 symbol in a regular object file, and there was no definition in
2846 any dynamic object, then the linker will have allocated space for
2847 the symbol in a common section but the DEF_REGULAR
2848 flag will not have been set. */
2849 if (h->root.type == bfd_link_hash_defined
2853 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2856 /* Symbols defined in discarded sections shouldn't be dynamic. */
2857 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2858 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2860 /* If a weak undefined symbol has non-default visibility, we also
2861 hide it from the dynamic linker. */
2862 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2863 && h->root.type == bfd_link_hash_undefweak)
2864 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2866 /* A hidden versioned symbol in executable should be forced local if
2867 it is is locally defined, not referenced by shared library and not
2869 else if (bfd_link_executable (eif->info)
2870 && h->versioned == versioned_hidden
2871 && !eif->info->export_dynamic
2875 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2877 /* If -Bsymbolic was used (which means to bind references to global
2878 symbols to the definition within the shared object), and this
2879 symbol was defined in a regular object, then it actually doesn't
2880 need a PLT entry. Likewise, if the symbol has non-default
2881 visibility. If the symbol has hidden or internal visibility, we
2882 will force it local. */
2883 else if (h->needs_plt
2884 && bfd_link_pic (eif->info)
2885 && is_elf_hash_table (eif->info->hash)
2886 && (SYMBOLIC_BIND (eif->info, h)
2887 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2890 bfd_boolean force_local;
2892 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2893 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2894 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2897 /* If this is a weak defined symbol in a dynamic object, and we know
2898 the real definition in the dynamic object, copy interesting flags
2899 over to the real definition. */
2900 if (h->is_weakalias)
2902 struct elf_link_hash_entry *def = weakdef (h);
2904 /* If the real definition is defined by a regular object file,
2905 don't do anything special. See the longer description in
2906 _bfd_elf_adjust_dynamic_symbol, below. */
2907 if (def->def_regular)
2910 while ((h = h->u.alias) != def)
2911 h->is_weakalias = 0;
2915 while (h->root.type == bfd_link_hash_indirect)
2916 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2917 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2918 || h->root.type == bfd_link_hash_defweak);
2919 BFD_ASSERT (def->def_dynamic);
2920 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2921 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2928 /* Make the backend pick a good value for a dynamic symbol. This is
2929 called via elf_link_hash_traverse, and also calls itself
2933 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2935 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2936 struct elf_link_hash_table *htab;
2937 const struct elf_backend_data *bed;
2939 if (! is_elf_hash_table (eif->info->hash))
2942 /* Ignore indirect symbols. These are added by the versioning code. */
2943 if (h->root.type == bfd_link_hash_indirect)
2946 /* Fix the symbol flags. */
2947 if (! _bfd_elf_fix_symbol_flags (h, eif))
2950 htab = elf_hash_table (eif->info);
2951 bed = get_elf_backend_data (htab->dynobj);
2953 if (h->root.type == bfd_link_hash_undefweak)
2955 if (eif->info->dynamic_undefined_weak == 0)
2956 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2957 else if (eif->info->dynamic_undefined_weak > 0
2959 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2960 && !bfd_hide_sym_by_version (eif->info->version_info,
2961 h->root.root.string))
2963 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2971 /* If this symbol does not require a PLT entry, and it is not
2972 defined by a dynamic object, or is not referenced by a regular
2973 object, ignore it. We do have to handle a weak defined symbol,
2974 even if no regular object refers to it, if we decided to add it
2975 to the dynamic symbol table. FIXME: Do we normally need to worry
2976 about symbols which are defined by one dynamic object and
2977 referenced by another one? */
2979 && h->type != STT_GNU_IFUNC
2983 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
2985 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2989 /* If we've already adjusted this symbol, don't do it again. This
2990 can happen via a recursive call. */
2991 if (h->dynamic_adjusted)
2994 /* Don't look at this symbol again. Note that we must set this
2995 after checking the above conditions, because we may look at a
2996 symbol once, decide not to do anything, and then get called
2997 recursively later after REF_REGULAR is set below. */
2998 h->dynamic_adjusted = 1;
3000 /* If this is a weak definition, and we know a real definition, and
3001 the real symbol is not itself defined by a regular object file,
3002 then get a good value for the real definition. We handle the
3003 real symbol first, for the convenience of the backend routine.
3005 Note that there is a confusing case here. If the real definition
3006 is defined by a regular object file, we don't get the real symbol
3007 from the dynamic object, but we do get the weak symbol. If the
3008 processor backend uses a COPY reloc, then if some routine in the
3009 dynamic object changes the real symbol, we will not see that
3010 change in the corresponding weak symbol. This is the way other
3011 ELF linkers work as well, and seems to be a result of the shared
3014 I will clarify this issue. Most SVR4 shared libraries define the
3015 variable _timezone and define timezone as a weak synonym. The
3016 tzset call changes _timezone. If you write
3017 extern int timezone;
3019 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3020 you might expect that, since timezone is a synonym for _timezone,
3021 the same number will print both times. However, if the processor
3022 backend uses a COPY reloc, then actually timezone will be copied
3023 into your process image, and, since you define _timezone
3024 yourself, _timezone will not. Thus timezone and _timezone will
3025 wind up at different memory locations. The tzset call will set
3026 _timezone, leaving timezone unchanged. */
3028 if (h->is_weakalias)
3030 struct elf_link_hash_entry *def = weakdef (h);
3032 /* If we get to this point, there is an implicit reference to
3033 the alias by a regular object file via the weak symbol H. */
3034 def->ref_regular = 1;
3036 /* Ensure that the backend adjust_dynamic_symbol function sees
3037 the strong alias before H by recursively calling ourselves. */
3038 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3042 /* If a symbol has no type and no size and does not require a PLT
3043 entry, then we are probably about to do the wrong thing here: we
3044 are probably going to create a COPY reloc for an empty object.
3045 This case can arise when a shared object is built with assembly
3046 code, and the assembly code fails to set the symbol type. */
3048 && h->type == STT_NOTYPE
3051 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3052 h->root.root.string);
3054 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3063 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3067 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3068 struct elf_link_hash_entry *h,
3071 unsigned int power_of_two;
3073 asection *sec = h->root.u.def.section;
3075 /* The section alignment of the definition is the maximum alignment
3076 requirement of symbols defined in the section. Since we don't
3077 know the symbol alignment requirement, we start with the
3078 maximum alignment and check low bits of the symbol address
3079 for the minimum alignment. */
3080 power_of_two = bfd_get_section_alignment (sec->owner, sec);
3081 mask = ((bfd_vma) 1 << power_of_two) - 1;
3082 while ((h->root.u.def.value & mask) != 0)
3088 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
3091 /* Adjust the section alignment if needed. */
3092 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
3097 /* We make sure that the symbol will be aligned properly. */
3098 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3100 /* Define the symbol as being at this point in DYNBSS. */
3101 h->root.u.def.section = dynbss;
3102 h->root.u.def.value = dynbss->size;
3104 /* Increment the size of DYNBSS to make room for the symbol. */
3105 dynbss->size += h->size;
3107 /* No error if extern_protected_data is true. */
3108 if (h->protected_def
3109 && (!info->extern_protected_data
3110 || (info->extern_protected_data < 0
3111 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3112 info->callbacks->einfo
3113 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3114 h->root.root.string);
3119 /* Adjust all external symbols pointing into SEC_MERGE sections
3120 to reflect the object merging within the sections. */
3123 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3127 if ((h->root.type == bfd_link_hash_defined
3128 || h->root.type == bfd_link_hash_defweak)
3129 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3130 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3132 bfd *output_bfd = (bfd *) data;
3134 h->root.u.def.value =
3135 _bfd_merged_section_offset (output_bfd,
3136 &h->root.u.def.section,
3137 elf_section_data (sec)->sec_info,
3138 h->root.u.def.value);
3144 /* Returns false if the symbol referred to by H should be considered
3145 to resolve local to the current module, and true if it should be
3146 considered to bind dynamically. */
3149 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3150 struct bfd_link_info *info,
3151 bfd_boolean not_local_protected)
3153 bfd_boolean binding_stays_local_p;
3154 const struct elf_backend_data *bed;
3155 struct elf_link_hash_table *hash_table;
3160 while (h->root.type == bfd_link_hash_indirect
3161 || h->root.type == bfd_link_hash_warning)
3162 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3164 /* If it was forced local, then clearly it's not dynamic. */
3165 if (h->dynindx == -1)
3167 if (h->forced_local)
3170 /* Identify the cases where name binding rules say that a
3171 visible symbol resolves locally. */
3172 binding_stays_local_p = (bfd_link_executable (info)
3173 || SYMBOLIC_BIND (info, h));
3175 switch (ELF_ST_VISIBILITY (h->other))
3182 hash_table = elf_hash_table (info);
3183 if (!is_elf_hash_table (hash_table))
3186 bed = get_elf_backend_data (hash_table->dynobj);
3188 /* Proper resolution for function pointer equality may require
3189 that these symbols perhaps be resolved dynamically, even though
3190 we should be resolving them to the current module. */
3191 if (!not_local_protected || !bed->is_function_type (h->type))
3192 binding_stays_local_p = TRUE;
3199 /* If it isn't defined locally, then clearly it's dynamic. */
3200 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3203 /* Otherwise, the symbol is dynamic if binding rules don't tell
3204 us that it remains local. */
3205 return !binding_stays_local_p;
3208 /* Return true if the symbol referred to by H should be considered
3209 to resolve local to the current module, and false otherwise. Differs
3210 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3211 undefined symbols. The two functions are virtually identical except
3212 for the place where dynindx == -1 is tested. If that test is true,
3213 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3214 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3216 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3217 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3218 treatment of undefined weak symbols. For those that do not make
3219 undefined weak symbols dynamic, both functions may return false. */
3222 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3223 struct bfd_link_info *info,
3224 bfd_boolean local_protected)
3226 const struct elf_backend_data *bed;
3227 struct elf_link_hash_table *hash_table;
3229 /* If it's a local sym, of course we resolve locally. */
3233 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3234 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3235 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3238 /* Forced local symbols resolve locally. */
3239 if (h->forced_local)
3242 /* Common symbols that become definitions don't get the DEF_REGULAR
3243 flag set, so test it first, and don't bail out. */
3244 if (ELF_COMMON_DEF_P (h))
3246 /* If we don't have a definition in a regular file, then we can't
3247 resolve locally. The sym is either undefined or dynamic. */
3248 else if (!h->def_regular)
3251 /* Non-dynamic symbols resolve locally. */
3252 if (h->dynindx == -1)
3255 /* At this point, we know the symbol is defined and dynamic. In an
3256 executable it must resolve locally, likewise when building symbolic
3257 shared libraries. */
3258 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3261 /* Now deal with defined dynamic symbols in shared libraries. Ones
3262 with default visibility might not resolve locally. */
3263 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3266 hash_table = elf_hash_table (info);
3267 if (!is_elf_hash_table (hash_table))
3270 bed = get_elf_backend_data (hash_table->dynobj);
3272 /* If extern_protected_data is false, STV_PROTECTED non-function
3273 symbols are local. */
3274 if ((!info->extern_protected_data
3275 || (info->extern_protected_data < 0
3276 && !bed->extern_protected_data))
3277 && !bed->is_function_type (h->type))
3280 /* Function pointer equality tests may require that STV_PROTECTED
3281 symbols be treated as dynamic symbols. If the address of a
3282 function not defined in an executable is set to that function's
3283 plt entry in the executable, then the address of the function in
3284 a shared library must also be the plt entry in the executable. */
3285 return local_protected;
3288 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3289 aligned. Returns the first TLS output section. */
3291 struct bfd_section *
3292 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3294 struct bfd_section *sec, *tls;
3295 unsigned int align = 0;
3297 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3298 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3302 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3303 if (sec->alignment_power > align)
3304 align = sec->alignment_power;
3306 elf_hash_table (info)->tls_sec = tls;
3308 /* Ensure the alignment of the first section is the largest alignment,
3309 so that the tls segment starts aligned. */
3311 tls->alignment_power = align;
3316 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3318 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3319 Elf_Internal_Sym *sym)
3321 const struct elf_backend_data *bed;
3323 /* Local symbols do not count, but target specific ones might. */
3324 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3325 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3328 bed = get_elf_backend_data (abfd);
3329 /* Function symbols do not count. */
3330 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3333 /* If the section is undefined, then so is the symbol. */
3334 if (sym->st_shndx == SHN_UNDEF)
3337 /* If the symbol is defined in the common section, then
3338 it is a common definition and so does not count. */
3339 if (bed->common_definition (sym))
3342 /* If the symbol is in a target specific section then we
3343 must rely upon the backend to tell us what it is. */
3344 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3345 /* FIXME - this function is not coded yet:
3347 return _bfd_is_global_symbol_definition (abfd, sym);
3349 Instead for now assume that the definition is not global,
3350 Even if this is wrong, at least the linker will behave
3351 in the same way that it used to do. */
3357 /* Search the symbol table of the archive element of the archive ABFD
3358 whose archive map contains a mention of SYMDEF, and determine if
3359 the symbol is defined in this element. */
3361 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3363 Elf_Internal_Shdr * hdr;
3367 Elf_Internal_Sym *isymbuf;
3368 Elf_Internal_Sym *isym;
3369 Elf_Internal_Sym *isymend;
3372 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3376 if (! bfd_check_format (abfd, bfd_object))
3379 /* Select the appropriate symbol table. If we don't know if the
3380 object file is an IR object, give linker LTO plugin a chance to
3381 get the correct symbol table. */
3382 if (abfd->plugin_format == bfd_plugin_yes
3383 #if BFD_SUPPORTS_PLUGINS
3384 || (abfd->plugin_format == bfd_plugin_unknown
3385 && bfd_link_plugin_object_p (abfd))
3389 /* Use the IR symbol table if the object has been claimed by
3391 abfd = abfd->plugin_dummy_bfd;
3392 hdr = &elf_tdata (abfd)->symtab_hdr;
3394 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3395 hdr = &elf_tdata (abfd)->symtab_hdr;
3397 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3399 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3401 /* The sh_info field of the symtab header tells us where the
3402 external symbols start. We don't care about the local symbols. */
3403 if (elf_bad_symtab (abfd))
3405 extsymcount = symcount;
3410 extsymcount = symcount - hdr->sh_info;
3411 extsymoff = hdr->sh_info;
3414 if (extsymcount == 0)
3417 /* Read in the symbol table. */
3418 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3420 if (isymbuf == NULL)
3423 /* Scan the symbol table looking for SYMDEF. */
3425 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3429 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3434 if (strcmp (name, symdef->name) == 0)
3436 result = is_global_data_symbol_definition (abfd, isym);
3446 /* Add an entry to the .dynamic table. */
3449 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3453 struct elf_link_hash_table *hash_table;
3454 const struct elf_backend_data *bed;
3456 bfd_size_type newsize;
3457 bfd_byte *newcontents;
3458 Elf_Internal_Dyn dyn;
3460 hash_table = elf_hash_table (info);
3461 if (! is_elf_hash_table (hash_table))
3464 if (tag == DT_RELA || tag == DT_REL)
3465 hash_table->dynamic_relocs = TRUE;
3467 bed = get_elf_backend_data (hash_table->dynobj);
3468 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3469 BFD_ASSERT (s != NULL);
3471 newsize = s->size + bed->s->sizeof_dyn;
3472 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3473 if (newcontents == NULL)
3477 dyn.d_un.d_val = val;
3478 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3481 s->contents = newcontents;
3486 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3487 otherwise just check whether one already exists. Returns -1 on error,
3488 1 if a DT_NEEDED tag already exists, and 0 on success. */
3491 elf_add_dt_needed_tag (bfd *abfd,
3492 struct bfd_link_info *info,
3496 struct elf_link_hash_table *hash_table;
3499 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3502 hash_table = elf_hash_table (info);
3503 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3504 if (strindex == (size_t) -1)
3507 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3510 const struct elf_backend_data *bed;
3513 bed = get_elf_backend_data (hash_table->dynobj);
3514 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3516 for (extdyn = sdyn->contents;
3517 extdyn < sdyn->contents + sdyn->size;
3518 extdyn += bed->s->sizeof_dyn)
3520 Elf_Internal_Dyn dyn;
3522 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3523 if (dyn.d_tag == DT_NEEDED
3524 && dyn.d_un.d_val == strindex)
3526 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3534 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3537 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3541 /* We were just checking for existence of the tag. */
3542 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3547 /* Return true if SONAME is on the needed list between NEEDED and STOP
3548 (or the end of list if STOP is NULL), and needed by a library that
3552 on_needed_list (const char *soname,
3553 struct bfd_link_needed_list *needed,
3554 struct bfd_link_needed_list *stop)
3556 struct bfd_link_needed_list *look;
3557 for (look = needed; look != stop; look = look->next)
3558 if (strcmp (soname, look->name) == 0
3559 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3560 /* If needed by a library that itself is not directly
3561 needed, recursively check whether that library is
3562 indirectly needed. Since we add DT_NEEDED entries to
3563 the end of the list, library dependencies appear after
3564 the library. Therefore search prior to the current
3565 LOOK, preventing possible infinite recursion. */
3566 || on_needed_list (elf_dt_name (look->by), needed, look)))
3572 /* Sort symbol by value, section, and size. */
3574 elf_sort_symbol (const void *arg1, const void *arg2)
3576 const struct elf_link_hash_entry *h1;
3577 const struct elf_link_hash_entry *h2;
3578 bfd_signed_vma vdiff;
3580 h1 = *(const struct elf_link_hash_entry **) arg1;
3581 h2 = *(const struct elf_link_hash_entry **) arg2;
3582 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3584 return vdiff > 0 ? 1 : -1;
3587 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3589 return sdiff > 0 ? 1 : -1;
3591 vdiff = h1->size - h2->size;
3592 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3595 /* This function is used to adjust offsets into .dynstr for
3596 dynamic symbols. This is called via elf_link_hash_traverse. */
3599 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3601 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3603 if (h->dynindx != -1)
3604 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3608 /* Assign string offsets in .dynstr, update all structures referencing
3612 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3614 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3615 struct elf_link_local_dynamic_entry *entry;
3616 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3617 bfd *dynobj = hash_table->dynobj;
3620 const struct elf_backend_data *bed;
3623 _bfd_elf_strtab_finalize (dynstr);
3624 size = _bfd_elf_strtab_size (dynstr);
3626 bed = get_elf_backend_data (dynobj);
3627 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3628 BFD_ASSERT (sdyn != NULL);
3630 /* Update all .dynamic entries referencing .dynstr strings. */
3631 for (extdyn = sdyn->contents;
3632 extdyn < sdyn->contents + sdyn->size;
3633 extdyn += bed->s->sizeof_dyn)
3635 Elf_Internal_Dyn dyn;
3637 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3641 dyn.d_un.d_val = size;
3651 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3656 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3659 /* Now update local dynamic symbols. */
3660 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3661 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3662 entry->isym.st_name);
3664 /* And the rest of dynamic symbols. */
3665 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3667 /* Adjust version definitions. */
3668 if (elf_tdata (output_bfd)->cverdefs)
3673 Elf_Internal_Verdef def;
3674 Elf_Internal_Verdaux defaux;
3676 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3680 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3682 p += sizeof (Elf_External_Verdef);
3683 if (def.vd_aux != sizeof (Elf_External_Verdef))
3685 for (i = 0; i < def.vd_cnt; ++i)
3687 _bfd_elf_swap_verdaux_in (output_bfd,
3688 (Elf_External_Verdaux *) p, &defaux);
3689 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3691 _bfd_elf_swap_verdaux_out (output_bfd,
3692 &defaux, (Elf_External_Verdaux *) p);
3693 p += sizeof (Elf_External_Verdaux);
3696 while (def.vd_next);
3699 /* Adjust version references. */
3700 if (elf_tdata (output_bfd)->verref)
3705 Elf_Internal_Verneed need;
3706 Elf_Internal_Vernaux needaux;
3708 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3712 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3714 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3715 _bfd_elf_swap_verneed_out (output_bfd, &need,
3716 (Elf_External_Verneed *) p);
3717 p += sizeof (Elf_External_Verneed);
3718 for (i = 0; i < need.vn_cnt; ++i)
3720 _bfd_elf_swap_vernaux_in (output_bfd,
3721 (Elf_External_Vernaux *) p, &needaux);
3722 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3724 _bfd_elf_swap_vernaux_out (output_bfd,
3726 (Elf_External_Vernaux *) p);
3727 p += sizeof (Elf_External_Vernaux);
3730 while (need.vn_next);
3736 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3737 The default is to only match when the INPUT and OUTPUT are exactly
3741 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3742 const bfd_target *output)
3744 return input == output;
3747 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3748 This version is used when different targets for the same architecture
3749 are virtually identical. */
3752 _bfd_elf_relocs_compatible (const bfd_target *input,
3753 const bfd_target *output)
3755 const struct elf_backend_data *obed, *ibed;
3757 if (input == output)
3760 ibed = xvec_get_elf_backend_data (input);
3761 obed = xvec_get_elf_backend_data (output);
3763 if (ibed->arch != obed->arch)
3766 /* If both backends are using this function, deem them compatible. */
3767 return ibed->relocs_compatible == obed->relocs_compatible;
3770 /* Make a special call to the linker "notice" function to tell it that
3771 we are about to handle an as-needed lib, or have finished
3772 processing the lib. */
3775 _bfd_elf_notice_as_needed (bfd *ibfd,
3776 struct bfd_link_info *info,
3777 enum notice_asneeded_action act)
3779 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3782 /* Check relocations an ELF object file. */
3785 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3787 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3788 struct elf_link_hash_table *htab = elf_hash_table (info);
3790 /* If this object is the same format as the output object, and it is
3791 not a shared library, then let the backend look through the
3794 This is required to build global offset table entries and to
3795 arrange for dynamic relocs. It is not required for the
3796 particular common case of linking non PIC code, even when linking
3797 against shared libraries, but unfortunately there is no way of
3798 knowing whether an object file has been compiled PIC or not.
3799 Looking through the relocs is not particularly time consuming.
3800 The problem is that we must either (1) keep the relocs in memory,
3801 which causes the linker to require additional runtime memory or
3802 (2) read the relocs twice from the input file, which wastes time.
3803 This would be a good case for using mmap.
3805 I have no idea how to handle linking PIC code into a file of a
3806 different format. It probably can't be done. */
3807 if ((abfd->flags & DYNAMIC) == 0
3808 && is_elf_hash_table (htab)
3809 && bed->check_relocs != NULL
3810 && elf_object_id (abfd) == elf_hash_table_id (htab)
3811 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3815 for (o = abfd->sections; o != NULL; o = o->next)
3817 Elf_Internal_Rela *internal_relocs;
3820 /* Don't check relocations in excluded sections. */
3821 if ((o->flags & SEC_RELOC) == 0
3822 || (o->flags & SEC_EXCLUDE) != 0
3823 || o->reloc_count == 0
3824 || ((info->strip == strip_all || info->strip == strip_debugger)
3825 && (o->flags & SEC_DEBUGGING) != 0)
3826 || bfd_is_abs_section (o->output_section))
3829 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3831 if (internal_relocs == NULL)
3834 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3836 if (elf_section_data (o)->relocs != internal_relocs)
3837 free (internal_relocs);
3847 /* Add symbols from an ELF object file to the linker hash table. */
3850 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3852 Elf_Internal_Ehdr *ehdr;
3853 Elf_Internal_Shdr *hdr;
3857 struct elf_link_hash_entry **sym_hash;
3858 bfd_boolean dynamic;
3859 Elf_External_Versym *extversym = NULL;
3860 Elf_External_Versym *ever;
3861 struct elf_link_hash_entry *weaks;
3862 struct elf_link_hash_entry **nondeflt_vers = NULL;
3863 size_t nondeflt_vers_cnt = 0;
3864 Elf_Internal_Sym *isymbuf = NULL;
3865 Elf_Internal_Sym *isym;
3866 Elf_Internal_Sym *isymend;
3867 const struct elf_backend_data *bed;
3868 bfd_boolean add_needed;
3869 struct elf_link_hash_table *htab;
3871 void *alloc_mark = NULL;
3872 struct bfd_hash_entry **old_table = NULL;
3873 unsigned int old_size = 0;
3874 unsigned int old_count = 0;
3875 void *old_tab = NULL;
3877 struct bfd_link_hash_entry *old_undefs = NULL;
3878 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3879 void *old_strtab = NULL;
3882 bfd_boolean just_syms;
3884 htab = elf_hash_table (info);
3885 bed = get_elf_backend_data (abfd);
3887 if ((abfd->flags & DYNAMIC) == 0)
3893 /* You can't use -r against a dynamic object. Also, there's no
3894 hope of using a dynamic object which does not exactly match
3895 the format of the output file. */
3896 if (bfd_link_relocatable (info)
3897 || !is_elf_hash_table (htab)
3898 || info->output_bfd->xvec != abfd->xvec)
3900 if (bfd_link_relocatable (info))
3901 bfd_set_error (bfd_error_invalid_operation);
3903 bfd_set_error (bfd_error_wrong_format);
3908 ehdr = elf_elfheader (abfd);
3909 if (info->warn_alternate_em
3910 && bed->elf_machine_code != ehdr->e_machine
3911 && ((bed->elf_machine_alt1 != 0
3912 && ehdr->e_machine == bed->elf_machine_alt1)
3913 || (bed->elf_machine_alt2 != 0
3914 && ehdr->e_machine == bed->elf_machine_alt2)))
3916 /* xgettext:c-format */
3917 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3918 ehdr->e_machine, abfd, bed->elf_machine_code);
3920 /* As a GNU extension, any input sections which are named
3921 .gnu.warning.SYMBOL are treated as warning symbols for the given
3922 symbol. This differs from .gnu.warning sections, which generate
3923 warnings when they are included in an output file. */
3924 /* PR 12761: Also generate this warning when building shared libraries. */
3925 for (s = abfd->sections; s != NULL; s = s->next)
3929 name = bfd_get_section_name (abfd, s);
3930 if (CONST_STRNEQ (name, ".gnu.warning."))
3935 name += sizeof ".gnu.warning." - 1;
3937 /* If this is a shared object, then look up the symbol
3938 in the hash table. If it is there, and it is already
3939 been defined, then we will not be using the entry
3940 from this shared object, so we don't need to warn.
3941 FIXME: If we see the definition in a regular object
3942 later on, we will warn, but we shouldn't. The only
3943 fix is to keep track of what warnings we are supposed
3944 to emit, and then handle them all at the end of the
3948 struct elf_link_hash_entry *h;
3950 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3952 /* FIXME: What about bfd_link_hash_common? */
3954 && (h->root.type == bfd_link_hash_defined
3955 || h->root.type == bfd_link_hash_defweak))
3960 msg = (char *) bfd_alloc (abfd, sz + 1);
3964 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3969 if (! (_bfd_generic_link_add_one_symbol
3970 (info, abfd, name, BSF_WARNING, s, 0, msg,
3971 FALSE, bed->collect, NULL)))
3974 if (bfd_link_executable (info))
3976 /* Clobber the section size so that the warning does
3977 not get copied into the output file. */
3980 /* Also set SEC_EXCLUDE, so that symbols defined in
3981 the warning section don't get copied to the output. */
3982 s->flags |= SEC_EXCLUDE;
3987 just_syms = ((s = abfd->sections) != NULL
3988 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3993 /* If we are creating a shared library, create all the dynamic
3994 sections immediately. We need to attach them to something,
3995 so we attach them to this BFD, provided it is the right
3996 format and is not from ld --just-symbols. Always create the
3997 dynamic sections for -E/--dynamic-list. FIXME: If there
3998 are no input BFD's of the same format as the output, we can't
3999 make a shared library. */
4001 && (bfd_link_pic (info)
4002 || (!bfd_link_relocatable (info)
4004 && (info->export_dynamic || info->dynamic)))
4005 && is_elf_hash_table (htab)
4006 && info->output_bfd->xvec == abfd->xvec
4007 && !htab->dynamic_sections_created)
4009 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4013 else if (!is_elf_hash_table (htab))
4017 const char *soname = NULL;
4019 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4020 const Elf_Internal_Phdr *phdr;
4023 /* ld --just-symbols and dynamic objects don't mix very well.
4024 ld shouldn't allow it. */
4028 /* If this dynamic lib was specified on the command line with
4029 --as-needed in effect, then we don't want to add a DT_NEEDED
4030 tag unless the lib is actually used. Similary for libs brought
4031 in by another lib's DT_NEEDED. When --no-add-needed is used
4032 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4033 any dynamic library in DT_NEEDED tags in the dynamic lib at
4035 add_needed = (elf_dyn_lib_class (abfd)
4036 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4037 | DYN_NO_NEEDED)) == 0;
4039 s = bfd_get_section_by_name (abfd, ".dynamic");
4044 unsigned int elfsec;
4045 unsigned long shlink;
4047 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4054 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4055 if (elfsec == SHN_BAD)
4056 goto error_free_dyn;
4057 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4059 for (extdyn = dynbuf;
4060 extdyn < dynbuf + s->size;
4061 extdyn += bed->s->sizeof_dyn)
4063 Elf_Internal_Dyn dyn;
4065 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4066 if (dyn.d_tag == DT_SONAME)
4068 unsigned int tagv = dyn.d_un.d_val;
4069 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4071 goto error_free_dyn;
4073 if (dyn.d_tag == DT_NEEDED)
4075 struct bfd_link_needed_list *n, **pn;
4077 unsigned int tagv = dyn.d_un.d_val;
4079 amt = sizeof (struct bfd_link_needed_list);
4080 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4081 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4082 if (n == NULL || fnm == NULL)
4083 goto error_free_dyn;
4084 amt = strlen (fnm) + 1;
4085 anm = (char *) bfd_alloc (abfd, amt);
4087 goto error_free_dyn;
4088 memcpy (anm, fnm, amt);
4092 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4096 if (dyn.d_tag == DT_RUNPATH)
4098 struct bfd_link_needed_list *n, **pn;
4100 unsigned int tagv = dyn.d_un.d_val;
4102 amt = sizeof (struct bfd_link_needed_list);
4103 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4104 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4105 if (n == NULL || fnm == NULL)
4106 goto error_free_dyn;
4107 amt = strlen (fnm) + 1;
4108 anm = (char *) bfd_alloc (abfd, amt);
4110 goto error_free_dyn;
4111 memcpy (anm, fnm, amt);
4115 for (pn = & runpath;
4121 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4122 if (!runpath && dyn.d_tag == DT_RPATH)
4124 struct bfd_link_needed_list *n, **pn;
4126 unsigned int tagv = dyn.d_un.d_val;
4128 amt = sizeof (struct bfd_link_needed_list);
4129 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4130 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4131 if (n == NULL || fnm == NULL)
4132 goto error_free_dyn;
4133 amt = strlen (fnm) + 1;
4134 anm = (char *) bfd_alloc (abfd, amt);
4136 goto error_free_dyn;
4137 memcpy (anm, fnm, amt);
4147 if (dyn.d_tag == DT_AUDIT)
4149 unsigned int tagv = dyn.d_un.d_val;
4150 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4157 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4158 frees all more recently bfd_alloc'd blocks as well. */
4164 struct bfd_link_needed_list **pn;
4165 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4170 /* If we have a PT_GNU_RELRO program header, mark as read-only
4171 all sections contained fully therein. This makes relro
4172 shared library sections appear as they will at run-time. */
4173 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4174 while (--phdr >= elf_tdata (abfd)->phdr)
4175 if (phdr->p_type == PT_GNU_RELRO)
4177 for (s = abfd->sections; s != NULL; s = s->next)
4178 if ((s->flags & SEC_ALLOC) != 0
4179 && s->vma >= phdr->p_vaddr
4180 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4181 s->flags |= SEC_READONLY;
4185 /* We do not want to include any of the sections in a dynamic
4186 object in the output file. We hack by simply clobbering the
4187 list of sections in the BFD. This could be handled more
4188 cleanly by, say, a new section flag; the existing
4189 SEC_NEVER_LOAD flag is not the one we want, because that one
4190 still implies that the section takes up space in the output
4192 bfd_section_list_clear (abfd);
4194 /* Find the name to use in a DT_NEEDED entry that refers to this
4195 object. If the object has a DT_SONAME entry, we use it.
4196 Otherwise, if the generic linker stuck something in
4197 elf_dt_name, we use that. Otherwise, we just use the file
4199 if (soname == NULL || *soname == '\0')
4201 soname = elf_dt_name (abfd);
4202 if (soname == NULL || *soname == '\0')
4203 soname = bfd_get_filename (abfd);
4206 /* Save the SONAME because sometimes the linker emulation code
4207 will need to know it. */
4208 elf_dt_name (abfd) = soname;
4210 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4214 /* If we have already included this dynamic object in the
4215 link, just ignore it. There is no reason to include a
4216 particular dynamic object more than once. */
4220 /* Save the DT_AUDIT entry for the linker emulation code. */
4221 elf_dt_audit (abfd) = audit;
4224 /* If this is a dynamic object, we always link against the .dynsym
4225 symbol table, not the .symtab symbol table. The dynamic linker
4226 will only see the .dynsym symbol table, so there is no reason to
4227 look at .symtab for a dynamic object. */
4229 if (! dynamic || elf_dynsymtab (abfd) == 0)
4230 hdr = &elf_tdata (abfd)->symtab_hdr;
4232 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4234 symcount = hdr->sh_size / bed->s->sizeof_sym;
4236 /* The sh_info field of the symtab header tells us where the
4237 external symbols start. We don't care about the local symbols at
4239 if (elf_bad_symtab (abfd))
4241 extsymcount = symcount;
4246 extsymcount = symcount - hdr->sh_info;
4247 extsymoff = hdr->sh_info;
4250 sym_hash = elf_sym_hashes (abfd);
4251 if (extsymcount != 0)
4253 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4255 if (isymbuf == NULL)
4258 if (sym_hash == NULL)
4260 /* We store a pointer to the hash table entry for each
4263 amt *= sizeof (struct elf_link_hash_entry *);
4264 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4265 if (sym_hash == NULL)
4266 goto error_free_sym;
4267 elf_sym_hashes (abfd) = sym_hash;
4273 /* Read in any version definitions. */
4274 if (!_bfd_elf_slurp_version_tables (abfd,
4275 info->default_imported_symver))
4276 goto error_free_sym;
4278 /* Read in the symbol versions, but don't bother to convert them
4279 to internal format. */
4280 if (elf_dynversym (abfd) != 0)
4282 Elf_Internal_Shdr *versymhdr;
4284 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4285 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4286 if (extversym == NULL)
4287 goto error_free_sym;
4288 amt = versymhdr->sh_size;
4289 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4290 || bfd_bread (extversym, amt, abfd) != amt)
4291 goto error_free_vers;
4295 /* If we are loading an as-needed shared lib, save the symbol table
4296 state before we start adding symbols. If the lib turns out
4297 to be unneeded, restore the state. */
4298 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4303 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4305 struct bfd_hash_entry *p;
4306 struct elf_link_hash_entry *h;
4308 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4310 h = (struct elf_link_hash_entry *) p;
4311 entsize += htab->root.table.entsize;
4312 if (h->root.type == bfd_link_hash_warning)
4313 entsize += htab->root.table.entsize;
4317 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4318 old_tab = bfd_malloc (tabsize + entsize);
4319 if (old_tab == NULL)
4320 goto error_free_vers;
4322 /* Remember the current objalloc pointer, so that all mem for
4323 symbols added can later be reclaimed. */
4324 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4325 if (alloc_mark == NULL)
4326 goto error_free_vers;
4328 /* Make a special call to the linker "notice" function to
4329 tell it that we are about to handle an as-needed lib. */
4330 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4331 goto error_free_vers;
4333 /* Clone the symbol table. Remember some pointers into the
4334 symbol table, and dynamic symbol count. */
4335 old_ent = (char *) old_tab + tabsize;
4336 memcpy (old_tab, htab->root.table.table, tabsize);
4337 old_undefs = htab->root.undefs;
4338 old_undefs_tail = htab->root.undefs_tail;
4339 old_table = htab->root.table.table;
4340 old_size = htab->root.table.size;
4341 old_count = htab->root.table.count;
4342 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4343 if (old_strtab == NULL)
4344 goto error_free_vers;
4346 for (i = 0; i < htab->root.table.size; i++)
4348 struct bfd_hash_entry *p;
4349 struct elf_link_hash_entry *h;
4351 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4353 memcpy (old_ent, p, htab->root.table.entsize);
4354 old_ent = (char *) old_ent + htab->root.table.entsize;
4355 h = (struct elf_link_hash_entry *) p;
4356 if (h->root.type == bfd_link_hash_warning)
4358 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4359 old_ent = (char *) old_ent + htab->root.table.entsize;
4366 ever = extversym != NULL ? extversym + extsymoff : NULL;
4367 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4369 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4373 asection *sec, *new_sec;
4376 struct elf_link_hash_entry *h;
4377 struct elf_link_hash_entry *hi;
4378 bfd_boolean definition;
4379 bfd_boolean size_change_ok;
4380 bfd_boolean type_change_ok;
4381 bfd_boolean new_weak;
4382 bfd_boolean old_weak;
4383 bfd_boolean override;
4385 bfd_boolean discarded;
4386 unsigned int old_alignment;
4388 bfd_boolean matched;
4392 flags = BSF_NO_FLAGS;
4394 value = isym->st_value;
4395 common = bed->common_definition (isym);
4396 if (common && info->inhibit_common_definition)
4398 /* Treat common symbol as undefined for --no-define-common. */
4399 isym->st_shndx = SHN_UNDEF;
4404 bind = ELF_ST_BIND (isym->st_info);
4408 /* This should be impossible, since ELF requires that all
4409 global symbols follow all local symbols, and that sh_info
4410 point to the first global symbol. Unfortunately, Irix 5
4415 if (isym->st_shndx != SHN_UNDEF && !common)
4423 case STB_GNU_UNIQUE:
4424 flags = BSF_GNU_UNIQUE;
4428 /* Leave it up to the processor backend. */
4432 if (isym->st_shndx == SHN_UNDEF)
4433 sec = bfd_und_section_ptr;
4434 else if (isym->st_shndx == SHN_ABS)
4435 sec = bfd_abs_section_ptr;
4436 else if (isym->st_shndx == SHN_COMMON)
4438 sec = bfd_com_section_ptr;
4439 /* What ELF calls the size we call the value. What ELF
4440 calls the value we call the alignment. */
4441 value = isym->st_size;
4445 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4447 sec = bfd_abs_section_ptr;
4448 else if (discarded_section (sec))
4450 /* Symbols from discarded section are undefined. We keep
4452 sec = bfd_und_section_ptr;
4454 isym->st_shndx = SHN_UNDEF;
4456 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4460 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4463 goto error_free_vers;
4465 if (isym->st_shndx == SHN_COMMON
4466 && (abfd->flags & BFD_PLUGIN) != 0)
4468 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4472 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4474 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4476 goto error_free_vers;
4480 else if (isym->st_shndx == SHN_COMMON
4481 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4482 && !bfd_link_relocatable (info))
4484 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4488 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4489 | SEC_LINKER_CREATED);
4490 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4492 goto error_free_vers;
4496 else if (bed->elf_add_symbol_hook)
4498 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4500 goto error_free_vers;
4502 /* The hook function sets the name to NULL if this symbol
4503 should be skipped for some reason. */
4508 /* Sanity check that all possibilities were handled. */
4511 bfd_set_error (bfd_error_bad_value);
4512 goto error_free_vers;
4515 /* Silently discard TLS symbols from --just-syms. There's
4516 no way to combine a static TLS block with a new TLS block
4517 for this executable. */
4518 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4519 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4522 if (bfd_is_und_section (sec)
4523 || bfd_is_com_section (sec))
4528 size_change_ok = FALSE;
4529 type_change_ok = bed->type_change_ok;
4536 if (is_elf_hash_table (htab))
4538 Elf_Internal_Versym iver;
4539 unsigned int vernum = 0;
4544 if (info->default_imported_symver)
4545 /* Use the default symbol version created earlier. */
4546 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4551 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4553 vernum = iver.vs_vers & VERSYM_VERSION;
4555 /* If this is a hidden symbol, or if it is not version
4556 1, we append the version name to the symbol name.
4557 However, we do not modify a non-hidden absolute symbol
4558 if it is not a function, because it might be the version
4559 symbol itself. FIXME: What if it isn't? */
4560 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4562 && (!bfd_is_abs_section (sec)
4563 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4566 size_t namelen, verlen, newlen;
4569 if (isym->st_shndx != SHN_UNDEF)
4571 if (vernum > elf_tdata (abfd)->cverdefs)
4573 else if (vernum > 1)
4575 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4582 /* xgettext:c-format */
4583 (_("%pB: %s: invalid version %u (max %d)"),
4585 elf_tdata (abfd)->cverdefs);
4586 bfd_set_error (bfd_error_bad_value);
4587 goto error_free_vers;
4592 /* We cannot simply test for the number of
4593 entries in the VERNEED section since the
4594 numbers for the needed versions do not start
4596 Elf_Internal_Verneed *t;
4599 for (t = elf_tdata (abfd)->verref;
4603 Elf_Internal_Vernaux *a;
4605 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4607 if (a->vna_other == vernum)
4609 verstr = a->vna_nodename;
4619 /* xgettext:c-format */
4620 (_("%pB: %s: invalid needed version %d"),
4621 abfd, name, vernum);
4622 bfd_set_error (bfd_error_bad_value);
4623 goto error_free_vers;
4627 namelen = strlen (name);
4628 verlen = strlen (verstr);
4629 newlen = namelen + verlen + 2;
4630 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4631 && isym->st_shndx != SHN_UNDEF)
4634 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4635 if (newname == NULL)
4636 goto error_free_vers;
4637 memcpy (newname, name, namelen);
4638 p = newname + namelen;
4640 /* If this is a defined non-hidden version symbol,
4641 we add another @ to the name. This indicates the
4642 default version of the symbol. */
4643 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4644 && isym->st_shndx != SHN_UNDEF)
4646 memcpy (p, verstr, verlen + 1);
4651 /* If this symbol has default visibility and the user has
4652 requested we not re-export it, then mark it as hidden. */
4653 if (!bfd_is_und_section (sec)
4656 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4657 isym->st_other = (STV_HIDDEN
4658 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4660 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4661 sym_hash, &old_bfd, &old_weak,
4662 &old_alignment, &skip, &override,
4663 &type_change_ok, &size_change_ok,
4665 goto error_free_vers;
4670 /* Override a definition only if the new symbol matches the
4672 if (override && matched)
4676 while (h->root.type == bfd_link_hash_indirect
4677 || h->root.type == bfd_link_hash_warning)
4678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4680 if (elf_tdata (abfd)->verdef != NULL
4683 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4686 if (! (_bfd_generic_link_add_one_symbol
4687 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4688 (struct bfd_link_hash_entry **) sym_hash)))
4689 goto error_free_vers;
4691 if ((abfd->flags & DYNAMIC) == 0
4692 && (bfd_get_flavour (info->output_bfd)
4693 == bfd_target_elf_flavour))
4695 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4696 elf_tdata (info->output_bfd)->has_gnu_symbols
4697 |= elf_gnu_symbol_ifunc;
4698 if ((flags & BSF_GNU_UNIQUE))
4699 elf_tdata (info->output_bfd)->has_gnu_symbols
4700 |= elf_gnu_symbol_unique;
4704 /* We need to make sure that indirect symbol dynamic flags are
4707 while (h->root.type == bfd_link_hash_indirect
4708 || h->root.type == bfd_link_hash_warning)
4709 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4711 /* Setting the index to -3 tells elf_link_output_extsym that
4712 this symbol is defined in a discarded section. */
4718 new_weak = (flags & BSF_WEAK) != 0;
4722 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4723 && is_elf_hash_table (htab)
4724 && h->u.alias == NULL)
4726 /* Keep a list of all weak defined non function symbols from
4727 a dynamic object, using the alias field. Later in this
4728 function we will set the alias field to the correct
4729 value. We only put non-function symbols from dynamic
4730 objects on this list, because that happens to be the only
4731 time we need to know the normal symbol corresponding to a
4732 weak symbol, and the information is time consuming to
4733 figure out. If the alias field is not already NULL,
4734 then this symbol was already defined by some previous
4735 dynamic object, and we will be using that previous
4736 definition anyhow. */
4742 /* Set the alignment of a common symbol. */
4743 if ((common || bfd_is_com_section (sec))
4744 && h->root.type == bfd_link_hash_common)
4749 align = bfd_log2 (isym->st_value);
4752 /* The new symbol is a common symbol in a shared object.
4753 We need to get the alignment from the section. */
4754 align = new_sec->alignment_power;
4756 if (align > old_alignment)
4757 h->root.u.c.p->alignment_power = align;
4759 h->root.u.c.p->alignment_power = old_alignment;
4762 if (is_elf_hash_table (htab))
4764 /* Set a flag in the hash table entry indicating the type of
4765 reference or definition we just found. A dynamic symbol
4766 is one which is referenced or defined by both a regular
4767 object and a shared object. */
4768 bfd_boolean dynsym = FALSE;
4770 /* Plugin symbols aren't normal. Don't set def_regular or
4771 ref_regular for them, or make them dynamic. */
4772 if ((abfd->flags & BFD_PLUGIN) != 0)
4779 if (bind != STB_WEAK)
4780 h->ref_regular_nonweak = 1;
4792 /* If the indirect symbol has been forced local, don't
4793 make the real symbol dynamic. */
4794 if ((h == hi || !hi->forced_local)
4795 && (bfd_link_dll (info)
4805 hi->ref_dynamic = 1;
4810 hi->def_dynamic = 1;
4813 /* If the indirect symbol has been forced local, don't
4814 make the real symbol dynamic. */
4815 if ((h == hi || !hi->forced_local)
4819 && weakdef (h)->dynindx != -1)))
4823 /* Check to see if we need to add an indirect symbol for
4824 the default name. */
4826 || (!override && h->root.type == bfd_link_hash_common))
4827 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4828 sec, value, &old_bfd, &dynsym))
4829 goto error_free_vers;
4831 /* Check the alignment when a common symbol is involved. This
4832 can change when a common symbol is overridden by a normal
4833 definition or a common symbol is ignored due to the old
4834 normal definition. We need to make sure the maximum
4835 alignment is maintained. */
4836 if ((old_alignment || common)
4837 && h->root.type != bfd_link_hash_common)
4839 unsigned int common_align;
4840 unsigned int normal_align;
4841 unsigned int symbol_align;
4845 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4846 || h->root.type == bfd_link_hash_defweak);
4848 symbol_align = ffs (h->root.u.def.value) - 1;
4849 if (h->root.u.def.section->owner != NULL
4850 && (h->root.u.def.section->owner->flags
4851 & (DYNAMIC | BFD_PLUGIN)) == 0)
4853 normal_align = h->root.u.def.section->alignment_power;
4854 if (normal_align > symbol_align)
4855 normal_align = symbol_align;
4858 normal_align = symbol_align;
4862 common_align = old_alignment;
4863 common_bfd = old_bfd;
4868 common_align = bfd_log2 (isym->st_value);
4870 normal_bfd = old_bfd;
4873 if (normal_align < common_align)
4875 /* PR binutils/2735 */
4876 if (normal_bfd == NULL)
4878 /* xgettext:c-format */
4879 (_("warning: alignment %u of common symbol `%s' in %pB is"
4880 " greater than the alignment (%u) of its section %pA"),
4881 1 << common_align, name, common_bfd,
4882 1 << normal_align, h->root.u.def.section);
4885 /* xgettext:c-format */
4886 (_("warning: alignment %u of symbol `%s' in %pB"
4887 " is smaller than %u in %pB"),
4888 1 << normal_align, name, normal_bfd,
4889 1 << common_align, common_bfd);
4893 /* Remember the symbol size if it isn't undefined. */
4894 if (isym->st_size != 0
4895 && isym->st_shndx != SHN_UNDEF
4896 && (definition || h->size == 0))
4899 && h->size != isym->st_size
4900 && ! size_change_ok)
4902 /* xgettext:c-format */
4903 (_("warning: size of symbol `%s' changed"
4904 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4905 name, (uint64_t) h->size, old_bfd,
4906 (uint64_t) isym->st_size, abfd);
4908 h->size = isym->st_size;
4911 /* If this is a common symbol, then we always want H->SIZE
4912 to be the size of the common symbol. The code just above
4913 won't fix the size if a common symbol becomes larger. We
4914 don't warn about a size change here, because that is
4915 covered by --warn-common. Allow changes between different
4917 if (h->root.type == bfd_link_hash_common)
4918 h->size = h->root.u.c.size;
4920 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4921 && ((definition && !new_weak)
4922 || (old_weak && h->root.type == bfd_link_hash_common)
4923 || h->type == STT_NOTYPE))
4925 unsigned int type = ELF_ST_TYPE (isym->st_info);
4927 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4929 if (type == STT_GNU_IFUNC
4930 && (abfd->flags & DYNAMIC) != 0)
4933 if (h->type != type)
4935 if (h->type != STT_NOTYPE && ! type_change_ok)
4936 /* xgettext:c-format */
4938 (_("warning: type of symbol `%s' changed"
4939 " from %d to %d in %pB"),
4940 name, h->type, type, abfd);
4946 /* Merge st_other field. */
4947 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4949 /* We don't want to make debug symbol dynamic. */
4951 && (sec->flags & SEC_DEBUGGING)
4952 && !bfd_link_relocatable (info))
4955 /* Nor should we make plugin symbols dynamic. */
4956 if ((abfd->flags & BFD_PLUGIN) != 0)
4961 h->target_internal = isym->st_target_internal;
4962 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4965 if (definition && !dynamic)
4967 char *p = strchr (name, ELF_VER_CHR);
4968 if (p != NULL && p[1] != ELF_VER_CHR)
4970 /* Queue non-default versions so that .symver x, x@FOO
4971 aliases can be checked. */
4974 amt = ((isymend - isym + 1)
4975 * sizeof (struct elf_link_hash_entry *));
4977 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4979 goto error_free_vers;
4981 nondeflt_vers[nondeflt_vers_cnt++] = h;
4985 if (dynsym && h->dynindx == -1)
4987 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4988 goto error_free_vers;
4990 && weakdef (h)->dynindx == -1)
4992 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
4993 goto error_free_vers;
4996 else if (h->dynindx != -1)
4997 /* If the symbol already has a dynamic index, but
4998 visibility says it should not be visible, turn it into
5000 switch (ELF_ST_VISIBILITY (h->other))
5004 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5009 /* Don't add DT_NEEDED for references from the dummy bfd nor
5010 for unmatched symbol. */
5015 && h->ref_regular_nonweak
5017 || (old_bfd->flags & BFD_PLUGIN) == 0))
5018 || (h->ref_dynamic_nonweak
5019 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5020 && !on_needed_list (elf_dt_name (abfd),
5021 htab->needed, NULL))))
5024 const char *soname = elf_dt_name (abfd);
5026 info->callbacks->minfo ("%!", soname, old_bfd,
5027 h->root.root.string);
5029 /* A symbol from a library loaded via DT_NEEDED of some
5030 other library is referenced by a regular object.
5031 Add a DT_NEEDED entry for it. Issue an error if
5032 --no-add-needed is used and the reference was not
5035 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5038 /* xgettext:c-format */
5039 (_("%pB: undefined reference to symbol '%s'"),
5041 bfd_set_error (bfd_error_missing_dso);
5042 goto error_free_vers;
5045 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5046 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5049 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5051 goto error_free_vers;
5053 BFD_ASSERT (ret == 0);
5058 if (info->lto_plugin_active
5059 && !bfd_link_relocatable (info)
5060 && (abfd->flags & BFD_PLUGIN) == 0
5066 if (bed->s->arch_size == 32)
5071 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5072 referenced in regular objects so that linker plugin will get
5073 the correct symbol resolution. */
5075 sym_hash = elf_sym_hashes (abfd);
5076 for (s = abfd->sections; s != NULL; s = s->next)
5078 Elf_Internal_Rela *internal_relocs;
5079 Elf_Internal_Rela *rel, *relend;
5081 /* Don't check relocations in excluded sections. */
5082 if ((s->flags & SEC_RELOC) == 0
5083 || s->reloc_count == 0
5084 || (s->flags & SEC_EXCLUDE) != 0
5085 || ((info->strip == strip_all
5086 || info->strip == strip_debugger)
5087 && (s->flags & SEC_DEBUGGING) != 0))
5090 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5093 if (internal_relocs == NULL)
5094 goto error_free_vers;
5096 rel = internal_relocs;
5097 relend = rel + s->reloc_count;
5098 for ( ; rel < relend; rel++)
5100 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5101 struct elf_link_hash_entry *h;
5103 /* Skip local symbols. */
5104 if (r_symndx < extsymoff)
5107 h = sym_hash[r_symndx - extsymoff];
5109 h->root.non_ir_ref_regular = 1;
5112 if (elf_section_data (s)->relocs != internal_relocs)
5113 free (internal_relocs);
5117 if (extversym != NULL)
5123 if (isymbuf != NULL)
5129 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5133 /* Restore the symbol table. */
5134 old_ent = (char *) old_tab + tabsize;
5135 memset (elf_sym_hashes (abfd), 0,
5136 extsymcount * sizeof (struct elf_link_hash_entry *));
5137 htab->root.table.table = old_table;
5138 htab->root.table.size = old_size;
5139 htab->root.table.count = old_count;
5140 memcpy (htab->root.table.table, old_tab, tabsize);
5141 htab->root.undefs = old_undefs;
5142 htab->root.undefs_tail = old_undefs_tail;
5143 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5146 for (i = 0; i < htab->root.table.size; i++)
5148 struct bfd_hash_entry *p;
5149 struct elf_link_hash_entry *h;
5151 unsigned int alignment_power;
5152 unsigned int non_ir_ref_dynamic;
5154 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5156 h = (struct elf_link_hash_entry *) p;
5157 if (h->root.type == bfd_link_hash_warning)
5158 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5160 /* Preserve the maximum alignment and size for common
5161 symbols even if this dynamic lib isn't on DT_NEEDED
5162 since it can still be loaded at run time by another
5164 if (h->root.type == bfd_link_hash_common)
5166 size = h->root.u.c.size;
5167 alignment_power = h->root.u.c.p->alignment_power;
5172 alignment_power = 0;
5174 /* Preserve non_ir_ref_dynamic so that this symbol
5175 will be exported when the dynamic lib becomes needed
5176 in the second pass. */
5177 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5178 memcpy (p, old_ent, htab->root.table.entsize);
5179 old_ent = (char *) old_ent + htab->root.table.entsize;
5180 h = (struct elf_link_hash_entry *) p;
5181 if (h->root.type == bfd_link_hash_warning)
5183 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5184 old_ent = (char *) old_ent + htab->root.table.entsize;
5185 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5187 if (h->root.type == bfd_link_hash_common)
5189 if (size > h->root.u.c.size)
5190 h->root.u.c.size = size;
5191 if (alignment_power > h->root.u.c.p->alignment_power)
5192 h->root.u.c.p->alignment_power = alignment_power;
5194 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5198 /* Make a special call to the linker "notice" function to
5199 tell it that symbols added for crefs may need to be removed. */
5200 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5201 goto error_free_vers;
5204 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5206 if (nondeflt_vers != NULL)
5207 free (nondeflt_vers);
5211 if (old_tab != NULL)
5213 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5214 goto error_free_vers;
5219 /* Now that all the symbols from this input file are created, if
5220 not performing a relocatable link, handle .symver foo, foo@BAR
5221 such that any relocs against foo become foo@BAR. */
5222 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5226 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5228 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5229 char *shortname, *p;
5231 p = strchr (h->root.root.string, ELF_VER_CHR);
5233 || (h->root.type != bfd_link_hash_defined
5234 && h->root.type != bfd_link_hash_defweak))
5237 amt = p - h->root.root.string;
5238 shortname = (char *) bfd_malloc (amt + 1);
5240 goto error_free_vers;
5241 memcpy (shortname, h->root.root.string, amt);
5242 shortname[amt] = '\0';
5244 hi = (struct elf_link_hash_entry *)
5245 bfd_link_hash_lookup (&htab->root, shortname,
5246 FALSE, FALSE, FALSE);
5248 && hi->root.type == h->root.type
5249 && hi->root.u.def.value == h->root.u.def.value
5250 && hi->root.u.def.section == h->root.u.def.section)
5252 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5253 hi->root.type = bfd_link_hash_indirect;
5254 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5255 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5256 sym_hash = elf_sym_hashes (abfd);
5258 for (symidx = 0; symidx < extsymcount; ++symidx)
5259 if (sym_hash[symidx] == hi)
5261 sym_hash[symidx] = h;
5267 free (nondeflt_vers);
5268 nondeflt_vers = NULL;
5271 /* Now set the alias field correctly for all the weak defined
5272 symbols we found. The only way to do this is to search all the
5273 symbols. Since we only need the information for non functions in
5274 dynamic objects, that's the only time we actually put anything on
5275 the list WEAKS. We need this information so that if a regular
5276 object refers to a symbol defined weakly in a dynamic object, the
5277 real symbol in the dynamic object is also put in the dynamic
5278 symbols; we also must arrange for both symbols to point to the
5279 same memory location. We could handle the general case of symbol
5280 aliasing, but a general symbol alias can only be generated in
5281 assembler code, handling it correctly would be very time
5282 consuming, and other ELF linkers don't handle general aliasing
5286 struct elf_link_hash_entry **hpp;
5287 struct elf_link_hash_entry **hppend;
5288 struct elf_link_hash_entry **sorted_sym_hash;
5289 struct elf_link_hash_entry *h;
5292 /* Since we have to search the whole symbol list for each weak
5293 defined symbol, search time for N weak defined symbols will be
5294 O(N^2). Binary search will cut it down to O(NlogN). */
5296 amt *= sizeof (struct elf_link_hash_entry *);
5297 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5298 if (sorted_sym_hash == NULL)
5300 sym_hash = sorted_sym_hash;
5301 hpp = elf_sym_hashes (abfd);
5302 hppend = hpp + extsymcount;
5304 for (; hpp < hppend; hpp++)
5308 && h->root.type == bfd_link_hash_defined
5309 && !bed->is_function_type (h->type))
5317 qsort (sorted_sym_hash, sym_count,
5318 sizeof (struct elf_link_hash_entry *),
5321 while (weaks != NULL)
5323 struct elf_link_hash_entry *hlook;
5326 size_t i, j, idx = 0;
5329 weaks = hlook->u.alias;
5330 hlook->u.alias = NULL;
5332 if (hlook->root.type != bfd_link_hash_defined
5333 && hlook->root.type != bfd_link_hash_defweak)
5336 slook = hlook->root.u.def.section;
5337 vlook = hlook->root.u.def.value;
5343 bfd_signed_vma vdiff;
5345 h = sorted_sym_hash[idx];
5346 vdiff = vlook - h->root.u.def.value;
5353 int sdiff = slook->id - h->root.u.def.section->id;
5363 /* We didn't find a value/section match. */
5367 /* With multiple aliases, or when the weak symbol is already
5368 strongly defined, we have multiple matching symbols and
5369 the binary search above may land on any of them. Step
5370 one past the matching symbol(s). */
5373 h = sorted_sym_hash[idx];
5374 if (h->root.u.def.section != slook
5375 || h->root.u.def.value != vlook)
5379 /* Now look back over the aliases. Since we sorted by size
5380 as well as value and section, we'll choose the one with
5381 the largest size. */
5384 h = sorted_sym_hash[idx];
5386 /* Stop if value or section doesn't match. */
5387 if (h->root.u.def.section != slook
5388 || h->root.u.def.value != vlook)
5390 else if (h != hlook)
5392 struct elf_link_hash_entry *t;
5395 hlook->is_weakalias = 1;
5397 if (t->u.alias != NULL)
5398 while (t->u.alias != h)
5402 /* If the weak definition is in the list of dynamic
5403 symbols, make sure the real definition is put
5405 if (hlook->dynindx != -1 && h->dynindx == -1)
5407 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5410 free (sorted_sym_hash);
5415 /* If the real definition is in the list of dynamic
5416 symbols, make sure the weak definition is put
5417 there as well. If we don't do this, then the
5418 dynamic loader might not merge the entries for the
5419 real definition and the weak definition. */
5420 if (h->dynindx != -1 && hlook->dynindx == -1)
5422 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5423 goto err_free_sym_hash;
5430 free (sorted_sym_hash);
5433 if (bed->check_directives
5434 && !(*bed->check_directives) (abfd, info))
5437 /* If this is a non-traditional link, try to optimize the handling
5438 of the .stab/.stabstr sections. */
5440 && ! info->traditional_format
5441 && is_elf_hash_table (htab)
5442 && (info->strip != strip_all && info->strip != strip_debugger))
5446 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5447 if (stabstr != NULL)
5449 bfd_size_type string_offset = 0;
5452 for (stab = abfd->sections; stab; stab = stab->next)
5453 if (CONST_STRNEQ (stab->name, ".stab")
5454 && (!stab->name[5] ||
5455 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5456 && (stab->flags & SEC_MERGE) == 0
5457 && !bfd_is_abs_section (stab->output_section))
5459 struct bfd_elf_section_data *secdata;
5461 secdata = elf_section_data (stab);
5462 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5463 stabstr, &secdata->sec_info,
5466 if (secdata->sec_info)
5467 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5472 if (is_elf_hash_table (htab) && add_needed)
5474 /* Add this bfd to the loaded list. */
5475 struct elf_link_loaded_list *n;
5477 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5481 n->next = htab->loaded;
5488 if (old_tab != NULL)
5490 if (old_strtab != NULL)
5492 if (nondeflt_vers != NULL)
5493 free (nondeflt_vers);
5494 if (extversym != NULL)
5497 if (isymbuf != NULL)
5503 /* Return the linker hash table entry of a symbol that might be
5504 satisfied by an archive symbol. Return -1 on error. */
5506 struct elf_link_hash_entry *
5507 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5508 struct bfd_link_info *info,
5511 struct elf_link_hash_entry *h;
5515 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5519 /* If this is a default version (the name contains @@), look up the
5520 symbol again with only one `@' as well as without the version.
5521 The effect is that references to the symbol with and without the
5522 version will be matched by the default symbol in the archive. */
5524 p = strchr (name, ELF_VER_CHR);
5525 if (p == NULL || p[1] != ELF_VER_CHR)
5528 /* First check with only one `@'. */
5529 len = strlen (name);
5530 copy = (char *) bfd_alloc (abfd, len);
5532 return (struct elf_link_hash_entry *) -1;
5534 first = p - name + 1;
5535 memcpy (copy, name, first);
5536 memcpy (copy + first, name + first + 1, len - first);
5538 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5541 /* We also need to check references to the symbol without the
5543 copy[first - 1] = '\0';
5544 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5545 FALSE, FALSE, TRUE);
5548 bfd_release (abfd, copy);
5552 /* Add symbols from an ELF archive file to the linker hash table. We
5553 don't use _bfd_generic_link_add_archive_symbols because we need to
5554 handle versioned symbols.
5556 Fortunately, ELF archive handling is simpler than that done by
5557 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5558 oddities. In ELF, if we find a symbol in the archive map, and the
5559 symbol is currently undefined, we know that we must pull in that
5562 Unfortunately, we do have to make multiple passes over the symbol
5563 table until nothing further is resolved. */
5566 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5569 unsigned char *included = NULL;
5573 const struct elf_backend_data *bed;
5574 struct elf_link_hash_entry * (*archive_symbol_lookup)
5575 (bfd *, struct bfd_link_info *, const char *);
5577 if (! bfd_has_map (abfd))
5579 /* An empty archive is a special case. */
5580 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5582 bfd_set_error (bfd_error_no_armap);
5586 /* Keep track of all symbols we know to be already defined, and all
5587 files we know to be already included. This is to speed up the
5588 second and subsequent passes. */
5589 c = bfd_ardata (abfd)->symdef_count;
5593 amt *= sizeof (*included);
5594 included = (unsigned char *) bfd_zmalloc (amt);
5595 if (included == NULL)
5598 symdefs = bfd_ardata (abfd)->symdefs;
5599 bed = get_elf_backend_data (abfd);
5600 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5613 symdefend = symdef + c;
5614 for (i = 0; symdef < symdefend; symdef++, i++)
5616 struct elf_link_hash_entry *h;
5618 struct bfd_link_hash_entry *undefs_tail;
5623 if (symdef->file_offset == last)
5629 h = archive_symbol_lookup (abfd, info, symdef->name);
5630 if (h == (struct elf_link_hash_entry *) -1)
5636 if (h->root.type == bfd_link_hash_common)
5638 /* We currently have a common symbol. The archive map contains
5639 a reference to this symbol, so we may want to include it. We
5640 only want to include it however, if this archive element
5641 contains a definition of the symbol, not just another common
5644 Unfortunately some archivers (including GNU ar) will put
5645 declarations of common symbols into their archive maps, as
5646 well as real definitions, so we cannot just go by the archive
5647 map alone. Instead we must read in the element's symbol
5648 table and check that to see what kind of symbol definition
5650 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5653 else if (h->root.type != bfd_link_hash_undefined)
5655 if (h->root.type != bfd_link_hash_undefweak)
5656 /* Symbol must be defined. Don't check it again. */
5661 /* We need to include this archive member. */
5662 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5663 if (element == NULL)
5666 if (! bfd_check_format (element, bfd_object))
5669 undefs_tail = info->hash->undefs_tail;
5671 if (!(*info->callbacks
5672 ->add_archive_element) (info, element, symdef->name, &element))
5674 if (!bfd_link_add_symbols (element, info))
5677 /* If there are any new undefined symbols, we need to make
5678 another pass through the archive in order to see whether
5679 they can be defined. FIXME: This isn't perfect, because
5680 common symbols wind up on undefs_tail and because an
5681 undefined symbol which is defined later on in this pass
5682 does not require another pass. This isn't a bug, but it
5683 does make the code less efficient than it could be. */
5684 if (undefs_tail != info->hash->undefs_tail)
5687 /* Look backward to mark all symbols from this object file
5688 which we have already seen in this pass. */
5692 included[mark] = TRUE;
5697 while (symdefs[mark].file_offset == symdef->file_offset);
5699 /* We mark subsequent symbols from this object file as we go
5700 on through the loop. */
5701 last = symdef->file_offset;
5711 if (included != NULL)
5716 /* Given an ELF BFD, add symbols to the global hash table as
5720 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5722 switch (bfd_get_format (abfd))
5725 return elf_link_add_object_symbols (abfd, info);
5727 return elf_link_add_archive_symbols (abfd, info);
5729 bfd_set_error (bfd_error_wrong_format);
5734 struct hash_codes_info
5736 unsigned long *hashcodes;
5740 /* This function will be called though elf_link_hash_traverse to store
5741 all hash value of the exported symbols in an array. */
5744 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5746 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5751 /* Ignore indirect symbols. These are added by the versioning code. */
5752 if (h->dynindx == -1)
5755 name = h->root.root.string;
5756 if (h->versioned >= versioned)
5758 char *p = strchr (name, ELF_VER_CHR);
5761 alc = (char *) bfd_malloc (p - name + 1);
5767 memcpy (alc, name, p - name);
5768 alc[p - name] = '\0';
5773 /* Compute the hash value. */
5774 ha = bfd_elf_hash (name);
5776 /* Store the found hash value in the array given as the argument. */
5777 *(inf->hashcodes)++ = ha;
5779 /* And store it in the struct so that we can put it in the hash table
5781 h->u.elf_hash_value = ha;
5789 struct collect_gnu_hash_codes
5792 const struct elf_backend_data *bed;
5793 unsigned long int nsyms;
5794 unsigned long int maskbits;
5795 unsigned long int *hashcodes;
5796 unsigned long int *hashval;
5797 unsigned long int *indx;
5798 unsigned long int *counts;
5801 long int min_dynindx;
5802 unsigned long int bucketcount;
5803 unsigned long int symindx;
5804 long int local_indx;
5805 long int shift1, shift2;
5806 unsigned long int mask;
5810 /* This function will be called though elf_link_hash_traverse to store
5811 all hash value of the exported symbols in an array. */
5814 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5816 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5821 /* Ignore indirect symbols. These are added by the versioning code. */
5822 if (h->dynindx == -1)
5825 /* Ignore also local symbols and undefined symbols. */
5826 if (! (*s->bed->elf_hash_symbol) (h))
5829 name = h->root.root.string;
5830 if (h->versioned >= versioned)
5832 char *p = strchr (name, ELF_VER_CHR);
5835 alc = (char *) bfd_malloc (p - name + 1);
5841 memcpy (alc, name, p - name);
5842 alc[p - name] = '\0';
5847 /* Compute the hash value. */
5848 ha = bfd_elf_gnu_hash (name);
5850 /* Store the found hash value in the array for compute_bucket_count,
5851 and also for .dynsym reordering purposes. */
5852 s->hashcodes[s->nsyms] = ha;
5853 s->hashval[h->dynindx] = ha;
5855 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5856 s->min_dynindx = h->dynindx;
5864 /* This function will be called though elf_link_hash_traverse to do
5865 final dynaminc symbol renumbering. */
5868 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5870 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5871 unsigned long int bucket;
5872 unsigned long int val;
5874 /* Ignore indirect symbols. */
5875 if (h->dynindx == -1)
5878 /* Ignore also local symbols and undefined symbols. */
5879 if (! (*s->bed->elf_hash_symbol) (h))
5881 if (h->dynindx >= s->min_dynindx)
5882 h->dynindx = s->local_indx++;
5886 bucket = s->hashval[h->dynindx] % s->bucketcount;
5887 val = (s->hashval[h->dynindx] >> s->shift1)
5888 & ((s->maskbits >> s->shift1) - 1);
5889 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5891 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5892 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5893 if (s->counts[bucket] == 1)
5894 /* Last element terminates the chain. */
5896 bfd_put_32 (s->output_bfd, val,
5897 s->contents + (s->indx[bucket] - s->symindx) * 4);
5898 --s->counts[bucket];
5899 h->dynindx = s->indx[bucket]++;
5903 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5906 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5908 return !(h->forced_local
5909 || h->root.type == bfd_link_hash_undefined
5910 || h->root.type == bfd_link_hash_undefweak
5911 || ((h->root.type == bfd_link_hash_defined
5912 || h->root.type == bfd_link_hash_defweak)
5913 && h->root.u.def.section->output_section == NULL));
5916 /* Array used to determine the number of hash table buckets to use
5917 based on the number of symbols there are. If there are fewer than
5918 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5919 fewer than 37 we use 17 buckets, and so forth. We never use more
5920 than 32771 buckets. */
5922 static const size_t elf_buckets[] =
5924 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5928 /* Compute bucket count for hashing table. We do not use a static set
5929 of possible tables sizes anymore. Instead we determine for all
5930 possible reasonable sizes of the table the outcome (i.e., the
5931 number of collisions etc) and choose the best solution. The
5932 weighting functions are not too simple to allow the table to grow
5933 without bounds. Instead one of the weighting factors is the size.
5934 Therefore the result is always a good payoff between few collisions
5935 (= short chain lengths) and table size. */
5937 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5938 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5939 unsigned long int nsyms,
5942 size_t best_size = 0;
5943 unsigned long int i;
5945 /* We have a problem here. The following code to optimize the table
5946 size requires an integer type with more the 32 bits. If
5947 BFD_HOST_U_64_BIT is set we know about such a type. */
5948 #ifdef BFD_HOST_U_64_BIT
5953 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5954 bfd *dynobj = elf_hash_table (info)->dynobj;
5955 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5956 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5957 unsigned long int *counts;
5959 unsigned int no_improvement_count = 0;
5961 /* Possible optimization parameters: if we have NSYMS symbols we say
5962 that the hashing table must at least have NSYMS/4 and at most
5964 minsize = nsyms / 4;
5967 best_size = maxsize = nsyms * 2;
5972 if ((best_size & 31) == 0)
5976 /* Create array where we count the collisions in. We must use bfd_malloc
5977 since the size could be large. */
5979 amt *= sizeof (unsigned long int);
5980 counts = (unsigned long int *) bfd_malloc (amt);
5984 /* Compute the "optimal" size for the hash table. The criteria is a
5985 minimal chain length. The minor criteria is (of course) the size
5987 for (i = minsize; i < maxsize; ++i)
5989 /* Walk through the array of hashcodes and count the collisions. */
5990 BFD_HOST_U_64_BIT max;
5991 unsigned long int j;
5992 unsigned long int fact;
5994 if (gnu_hash && (i & 31) == 0)
5997 memset (counts, '\0', i * sizeof (unsigned long int));
5999 /* Determine how often each hash bucket is used. */
6000 for (j = 0; j < nsyms; ++j)
6001 ++counts[hashcodes[j] % i];
6003 /* For the weight function we need some information about the
6004 pagesize on the target. This is information need not be 100%
6005 accurate. Since this information is not available (so far) we
6006 define it here to a reasonable default value. If it is crucial
6007 to have a better value some day simply define this value. */
6008 # ifndef BFD_TARGET_PAGESIZE
6009 # define BFD_TARGET_PAGESIZE (4096)
6012 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6014 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6017 /* Variant 1: optimize for short chains. We add the squares
6018 of all the chain lengths (which favors many small chain
6019 over a few long chains). */
6020 for (j = 0; j < i; ++j)
6021 max += counts[j] * counts[j];
6023 /* This adds penalties for the overall size of the table. */
6024 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6027 /* Variant 2: Optimize a lot more for small table. Here we
6028 also add squares of the size but we also add penalties for
6029 empty slots (the +1 term). */
6030 for (j = 0; j < i; ++j)
6031 max += (1 + counts[j]) * (1 + counts[j]);
6033 /* The overall size of the table is considered, but not as
6034 strong as in variant 1, where it is squared. */
6035 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6039 /* Compare with current best results. */
6040 if (max < best_chlen)
6044 no_improvement_count = 0;
6046 /* PR 11843: Avoid futile long searches for the best bucket size
6047 when there are a large number of symbols. */
6048 else if (++no_improvement_count == 100)
6055 #endif /* defined (BFD_HOST_U_64_BIT) */
6057 /* This is the fallback solution if no 64bit type is available or if we
6058 are not supposed to spend much time on optimizations. We select the
6059 bucket count using a fixed set of numbers. */
6060 for (i = 0; elf_buckets[i] != 0; i++)
6062 best_size = elf_buckets[i];
6063 if (nsyms < elf_buckets[i + 1])
6066 if (gnu_hash && best_size < 2)
6073 /* Size any SHT_GROUP section for ld -r. */
6076 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6081 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6082 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6083 && (s = ibfd->sections) != NULL
6084 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6085 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6090 /* Set a default stack segment size. The value in INFO wins. If it
6091 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6092 undefined it is initialized. */
6095 bfd_elf_stack_segment_size (bfd *output_bfd,
6096 struct bfd_link_info *info,
6097 const char *legacy_symbol,
6098 bfd_vma default_size)
6100 struct elf_link_hash_entry *h = NULL;
6102 /* Look for legacy symbol. */
6104 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6105 FALSE, FALSE, FALSE);
6106 if (h && (h->root.type == bfd_link_hash_defined
6107 || h->root.type == bfd_link_hash_defweak)
6109 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6111 /* The symbol has no type if specified on the command line. */
6112 h->type = STT_OBJECT;
6113 if (info->stacksize)
6114 /* xgettext:c-format */
6115 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6116 output_bfd, legacy_symbol);
6117 else if (h->root.u.def.section != bfd_abs_section_ptr)
6118 /* xgettext:c-format */
6119 _bfd_error_handler (_("%pB: %s not absolute"),
6120 output_bfd, legacy_symbol);
6122 info->stacksize = h->root.u.def.value;
6125 if (!info->stacksize)
6126 /* If the user didn't set a size, or explicitly inhibit the
6127 size, set it now. */
6128 info->stacksize = default_size;
6130 /* Provide the legacy symbol, if it is referenced. */
6131 if (h && (h->root.type == bfd_link_hash_undefined
6132 || h->root.type == bfd_link_hash_undefweak))
6134 struct bfd_link_hash_entry *bh = NULL;
6136 if (!(_bfd_generic_link_add_one_symbol
6137 (info, output_bfd, legacy_symbol,
6138 BSF_GLOBAL, bfd_abs_section_ptr,
6139 info->stacksize >= 0 ? info->stacksize : 0,
6140 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6143 h = (struct elf_link_hash_entry *) bh;
6145 h->type = STT_OBJECT;
6151 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6153 struct elf_gc_sweep_symbol_info
6155 struct bfd_link_info *info;
6156 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6161 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6164 && (((h->root.type == bfd_link_hash_defined
6165 || h->root.type == bfd_link_hash_defweak)
6166 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6167 && h->root.u.def.section->gc_mark))
6168 || h->root.type == bfd_link_hash_undefined
6169 || h->root.type == bfd_link_hash_undefweak))
6171 struct elf_gc_sweep_symbol_info *inf;
6173 inf = (struct elf_gc_sweep_symbol_info *) data;
6174 (*inf->hide_symbol) (inf->info, h, TRUE);
6177 h->ref_regular_nonweak = 0;
6183 /* Set up the sizes and contents of the ELF dynamic sections. This is
6184 called by the ELF linker emulation before_allocation routine. We
6185 must set the sizes of the sections before the linker sets the
6186 addresses of the various sections. */
6189 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6192 const char *filter_shlib,
6194 const char *depaudit,
6195 const char * const *auxiliary_filters,
6196 struct bfd_link_info *info,
6197 asection **sinterpptr)
6200 const struct elf_backend_data *bed;
6204 if (!is_elf_hash_table (info->hash))
6207 dynobj = elf_hash_table (info)->dynobj;
6209 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6211 struct bfd_elf_version_tree *verdefs;
6212 struct elf_info_failed asvinfo;
6213 struct bfd_elf_version_tree *t;
6214 struct bfd_elf_version_expr *d;
6218 /* If we are supposed to export all symbols into the dynamic symbol
6219 table (this is not the normal case), then do so. */
6220 if (info->export_dynamic
6221 || (bfd_link_executable (info) && info->dynamic))
6223 struct elf_info_failed eif;
6227 elf_link_hash_traverse (elf_hash_table (info),
6228 _bfd_elf_export_symbol,
6236 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6238 if (soname_indx == (size_t) -1
6239 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6243 soname_indx = (size_t) -1;
6245 /* Make all global versions with definition. */
6246 for (t = info->version_info; t != NULL; t = t->next)
6247 for (d = t->globals.list; d != NULL; d = d->next)
6248 if (!d->symver && d->literal)
6250 const char *verstr, *name;
6251 size_t namelen, verlen, newlen;
6252 char *newname, *p, leading_char;
6253 struct elf_link_hash_entry *newh;
6255 leading_char = bfd_get_symbol_leading_char (output_bfd);
6257 namelen = strlen (name) + (leading_char != '\0');
6259 verlen = strlen (verstr);
6260 newlen = namelen + verlen + 3;
6262 newname = (char *) bfd_malloc (newlen);
6263 if (newname == NULL)
6265 newname[0] = leading_char;
6266 memcpy (newname + (leading_char != '\0'), name, namelen);
6268 /* Check the hidden versioned definition. */
6269 p = newname + namelen;
6271 memcpy (p, verstr, verlen + 1);
6272 newh = elf_link_hash_lookup (elf_hash_table (info),
6273 newname, FALSE, FALSE,
6276 || (newh->root.type != bfd_link_hash_defined
6277 && newh->root.type != bfd_link_hash_defweak))
6279 /* Check the default versioned definition. */
6281 memcpy (p, verstr, verlen + 1);
6282 newh = elf_link_hash_lookup (elf_hash_table (info),
6283 newname, FALSE, FALSE,
6288 /* Mark this version if there is a definition and it is
6289 not defined in a shared object. */
6291 && !newh->def_dynamic
6292 && (newh->root.type == bfd_link_hash_defined
6293 || newh->root.type == bfd_link_hash_defweak))
6297 /* Attach all the symbols to their version information. */
6298 asvinfo.info = info;
6299 asvinfo.failed = FALSE;
6301 elf_link_hash_traverse (elf_hash_table (info),
6302 _bfd_elf_link_assign_sym_version,
6307 if (!info->allow_undefined_version)
6309 /* Check if all global versions have a definition. */
6310 bfd_boolean all_defined = TRUE;
6311 for (t = info->version_info; t != NULL; t = t->next)
6312 for (d = t->globals.list; d != NULL; d = d->next)
6313 if (d->literal && !d->symver && !d->script)
6316 (_("%s: undefined version: %s"),
6317 d->pattern, t->name);
6318 all_defined = FALSE;
6323 bfd_set_error (bfd_error_bad_value);
6328 /* Set up the version definition section. */
6329 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6330 BFD_ASSERT (s != NULL);
6332 /* We may have created additional version definitions if we are
6333 just linking a regular application. */
6334 verdefs = info->version_info;
6336 /* Skip anonymous version tag. */
6337 if (verdefs != NULL && verdefs->vernum == 0)
6338 verdefs = verdefs->next;
6340 if (verdefs == NULL && !info->create_default_symver)
6341 s->flags |= SEC_EXCLUDE;
6347 Elf_Internal_Verdef def;
6348 Elf_Internal_Verdaux defaux;
6349 struct bfd_link_hash_entry *bh;
6350 struct elf_link_hash_entry *h;
6356 /* Make space for the base version. */
6357 size += sizeof (Elf_External_Verdef);
6358 size += sizeof (Elf_External_Verdaux);
6361 /* Make space for the default version. */
6362 if (info->create_default_symver)
6364 size += sizeof (Elf_External_Verdef);
6368 for (t = verdefs; t != NULL; t = t->next)
6370 struct bfd_elf_version_deps *n;
6372 /* Don't emit base version twice. */
6376 size += sizeof (Elf_External_Verdef);
6377 size += sizeof (Elf_External_Verdaux);
6380 for (n = t->deps; n != NULL; n = n->next)
6381 size += sizeof (Elf_External_Verdaux);
6385 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6386 if (s->contents == NULL && s->size != 0)
6389 /* Fill in the version definition section. */
6393 def.vd_version = VER_DEF_CURRENT;
6394 def.vd_flags = VER_FLG_BASE;
6397 if (info->create_default_symver)
6399 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6400 def.vd_next = sizeof (Elf_External_Verdef);
6404 def.vd_aux = sizeof (Elf_External_Verdef);
6405 def.vd_next = (sizeof (Elf_External_Verdef)
6406 + sizeof (Elf_External_Verdaux));
6409 if (soname_indx != (size_t) -1)
6411 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6413 def.vd_hash = bfd_elf_hash (soname);
6414 defaux.vda_name = soname_indx;
6421 name = lbasename (output_bfd->filename);
6422 def.vd_hash = bfd_elf_hash (name);
6423 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6425 if (indx == (size_t) -1)
6427 defaux.vda_name = indx;
6429 defaux.vda_next = 0;
6431 _bfd_elf_swap_verdef_out (output_bfd, &def,
6432 (Elf_External_Verdef *) p);
6433 p += sizeof (Elf_External_Verdef);
6434 if (info->create_default_symver)
6436 /* Add a symbol representing this version. */
6438 if (! (_bfd_generic_link_add_one_symbol
6439 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6441 get_elf_backend_data (dynobj)->collect, &bh)))
6443 h = (struct elf_link_hash_entry *) bh;
6446 h->type = STT_OBJECT;
6447 h->verinfo.vertree = NULL;
6449 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6452 /* Create a duplicate of the base version with the same
6453 aux block, but different flags. */
6456 def.vd_aux = sizeof (Elf_External_Verdef);
6458 def.vd_next = (sizeof (Elf_External_Verdef)
6459 + sizeof (Elf_External_Verdaux));
6462 _bfd_elf_swap_verdef_out (output_bfd, &def,
6463 (Elf_External_Verdef *) p);
6464 p += sizeof (Elf_External_Verdef);
6466 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6467 (Elf_External_Verdaux *) p);
6468 p += sizeof (Elf_External_Verdaux);
6470 for (t = verdefs; t != NULL; t = t->next)
6473 struct bfd_elf_version_deps *n;
6475 /* Don't emit the base version twice. */
6480 for (n = t->deps; n != NULL; n = n->next)
6483 /* Add a symbol representing this version. */
6485 if (! (_bfd_generic_link_add_one_symbol
6486 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6488 get_elf_backend_data (dynobj)->collect, &bh)))
6490 h = (struct elf_link_hash_entry *) bh;
6493 h->type = STT_OBJECT;
6494 h->verinfo.vertree = t;
6496 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6499 def.vd_version = VER_DEF_CURRENT;
6501 if (t->globals.list == NULL
6502 && t->locals.list == NULL
6504 def.vd_flags |= VER_FLG_WEAK;
6505 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6506 def.vd_cnt = cdeps + 1;
6507 def.vd_hash = bfd_elf_hash (t->name);
6508 def.vd_aux = sizeof (Elf_External_Verdef);
6511 /* If a basever node is next, it *must* be the last node in
6512 the chain, otherwise Verdef construction breaks. */
6513 if (t->next != NULL && t->next->vernum == 0)
6514 BFD_ASSERT (t->next->next == NULL);
6516 if (t->next != NULL && t->next->vernum != 0)
6517 def.vd_next = (sizeof (Elf_External_Verdef)
6518 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6520 _bfd_elf_swap_verdef_out (output_bfd, &def,
6521 (Elf_External_Verdef *) p);
6522 p += sizeof (Elf_External_Verdef);
6524 defaux.vda_name = h->dynstr_index;
6525 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6527 defaux.vda_next = 0;
6528 if (t->deps != NULL)
6529 defaux.vda_next = sizeof (Elf_External_Verdaux);
6530 t->name_indx = defaux.vda_name;
6532 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6533 (Elf_External_Verdaux *) p);
6534 p += sizeof (Elf_External_Verdaux);
6536 for (n = t->deps; n != NULL; n = n->next)
6538 if (n->version_needed == NULL)
6540 /* This can happen if there was an error in the
6542 defaux.vda_name = 0;
6546 defaux.vda_name = n->version_needed->name_indx;
6547 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6550 if (n->next == NULL)
6551 defaux.vda_next = 0;
6553 defaux.vda_next = sizeof (Elf_External_Verdaux);
6555 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6556 (Elf_External_Verdaux *) p);
6557 p += sizeof (Elf_External_Verdaux);
6561 elf_tdata (output_bfd)->cverdefs = cdefs;
6565 bed = get_elf_backend_data (output_bfd);
6567 if (info->gc_sections && bed->can_gc_sections)
6569 struct elf_gc_sweep_symbol_info sweep_info;
6571 /* Remove the symbols that were in the swept sections from the
6572 dynamic symbol table. */
6573 sweep_info.info = info;
6574 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6575 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6579 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6582 struct elf_find_verdep_info sinfo;
6584 /* Work out the size of the version reference section. */
6586 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6587 BFD_ASSERT (s != NULL);
6590 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6591 if (sinfo.vers == 0)
6593 sinfo.failed = FALSE;
6595 elf_link_hash_traverse (elf_hash_table (info),
6596 _bfd_elf_link_find_version_dependencies,
6601 if (elf_tdata (output_bfd)->verref == NULL)
6602 s->flags |= SEC_EXCLUDE;
6605 Elf_Internal_Verneed *vn;
6610 /* Build the version dependency section. */
6613 for (vn = elf_tdata (output_bfd)->verref;
6615 vn = vn->vn_nextref)
6617 Elf_Internal_Vernaux *a;
6619 size += sizeof (Elf_External_Verneed);
6621 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6622 size += sizeof (Elf_External_Vernaux);
6626 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6627 if (s->contents == NULL)
6631 for (vn = elf_tdata (output_bfd)->verref;
6633 vn = vn->vn_nextref)
6636 Elf_Internal_Vernaux *a;
6640 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6643 vn->vn_version = VER_NEED_CURRENT;
6645 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6646 elf_dt_name (vn->vn_bfd) != NULL
6647 ? elf_dt_name (vn->vn_bfd)
6648 : lbasename (vn->vn_bfd->filename),
6650 if (indx == (size_t) -1)
6653 vn->vn_aux = sizeof (Elf_External_Verneed);
6654 if (vn->vn_nextref == NULL)
6657 vn->vn_next = (sizeof (Elf_External_Verneed)
6658 + caux * sizeof (Elf_External_Vernaux));
6660 _bfd_elf_swap_verneed_out (output_bfd, vn,
6661 (Elf_External_Verneed *) p);
6662 p += sizeof (Elf_External_Verneed);
6664 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6666 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6667 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6668 a->vna_nodename, FALSE);
6669 if (indx == (size_t) -1)
6672 if (a->vna_nextptr == NULL)
6675 a->vna_next = sizeof (Elf_External_Vernaux);
6677 _bfd_elf_swap_vernaux_out (output_bfd, a,
6678 (Elf_External_Vernaux *) p);
6679 p += sizeof (Elf_External_Vernaux);
6683 elf_tdata (output_bfd)->cverrefs = crefs;
6687 /* Any syms created from now on start with -1 in
6688 got.refcount/offset and plt.refcount/offset. */
6689 elf_hash_table (info)->init_got_refcount
6690 = elf_hash_table (info)->init_got_offset;
6691 elf_hash_table (info)->init_plt_refcount
6692 = elf_hash_table (info)->init_plt_offset;
6694 if (bfd_link_relocatable (info)
6695 && !_bfd_elf_size_group_sections (info))
6698 /* The backend may have to create some sections regardless of whether
6699 we're dynamic or not. */
6700 if (bed->elf_backend_always_size_sections
6701 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6704 /* Determine any GNU_STACK segment requirements, after the backend
6705 has had a chance to set a default segment size. */
6706 if (info->execstack)
6707 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6708 else if (info->noexecstack)
6709 elf_stack_flags (output_bfd) = PF_R | PF_W;
6713 asection *notesec = NULL;
6716 for (inputobj = info->input_bfds;
6718 inputobj = inputobj->link.next)
6723 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6725 s = inputobj->sections;
6726 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6729 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6732 if (s->flags & SEC_CODE)
6736 else if (bed->default_execstack)
6739 if (notesec || info->stacksize > 0)
6740 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6741 if (notesec && exec && bfd_link_relocatable (info)
6742 && notesec->output_section != bfd_abs_section_ptr)
6743 notesec->output_section->flags |= SEC_CODE;
6746 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6748 struct elf_info_failed eif;
6749 struct elf_link_hash_entry *h;
6753 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6754 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6758 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6760 info->flags |= DF_SYMBOLIC;
6768 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6770 if (indx == (size_t) -1)
6773 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6774 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6778 if (filter_shlib != NULL)
6782 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6783 filter_shlib, TRUE);
6784 if (indx == (size_t) -1
6785 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6789 if (auxiliary_filters != NULL)
6791 const char * const *p;
6793 for (p = auxiliary_filters; *p != NULL; p++)
6797 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6799 if (indx == (size_t) -1
6800 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6809 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6811 if (indx == (size_t) -1
6812 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6816 if (depaudit != NULL)
6820 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6822 if (indx == (size_t) -1
6823 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6830 /* Find all symbols which were defined in a dynamic object and make
6831 the backend pick a reasonable value for them. */
6832 elf_link_hash_traverse (elf_hash_table (info),
6833 _bfd_elf_adjust_dynamic_symbol,
6838 /* Add some entries to the .dynamic section. We fill in some of the
6839 values later, in bfd_elf_final_link, but we must add the entries
6840 now so that we know the final size of the .dynamic section. */
6842 /* If there are initialization and/or finalization functions to
6843 call then add the corresponding DT_INIT/DT_FINI entries. */
6844 h = (info->init_function
6845 ? elf_link_hash_lookup (elf_hash_table (info),
6846 info->init_function, FALSE,
6853 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6856 h = (info->fini_function
6857 ? elf_link_hash_lookup (elf_hash_table (info),
6858 info->fini_function, FALSE,
6865 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6869 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6870 if (s != NULL && s->linker_has_input)
6872 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6873 if (! bfd_link_executable (info))
6878 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6879 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6880 && (o = sub->sections) != NULL
6881 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6882 for (o = sub->sections; o != NULL; o = o->next)
6883 if (elf_section_data (o)->this_hdr.sh_type
6884 == SHT_PREINIT_ARRAY)
6887 (_("%pB: .preinit_array section is not allowed in DSO"),
6892 bfd_set_error (bfd_error_nonrepresentable_section);
6896 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6897 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6900 s = bfd_get_section_by_name (output_bfd, ".init_array");
6901 if (s != NULL && s->linker_has_input)
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6904 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6907 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6908 if (s != NULL && s->linker_has_input)
6910 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6911 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6915 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6916 /* If .dynstr is excluded from the link, we don't want any of
6917 these tags. Strictly, we should be checking each section
6918 individually; This quick check covers for the case where
6919 someone does a /DISCARD/ : { *(*) }. */
6920 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6922 bfd_size_type strsize;
6924 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6925 if ((info->emit_hash
6926 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6927 || (info->emit_gnu_hash
6928 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6929 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6930 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6931 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6932 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6933 bed->s->sizeof_sym))
6938 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6941 /* The backend must work out the sizes of all the other dynamic
6944 && bed->elf_backend_size_dynamic_sections != NULL
6945 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6948 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6950 if (elf_tdata (output_bfd)->cverdefs)
6952 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6954 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6955 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6959 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6961 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6964 else if (info->flags & DF_BIND_NOW)
6966 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6972 if (bfd_link_executable (info))
6973 info->flags_1 &= ~ (DF_1_INITFIRST
6976 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6980 if (elf_tdata (output_bfd)->cverrefs)
6982 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6984 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6985 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6989 if ((elf_tdata (output_bfd)->cverrefs == 0
6990 && elf_tdata (output_bfd)->cverdefs == 0)
6991 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
6995 s = bfd_get_linker_section (dynobj, ".gnu.version");
6996 s->flags |= SEC_EXCLUDE;
7002 /* Find the first non-excluded output section. We'll use its
7003 section symbol for some emitted relocs. */
7005 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7009 for (s = output_bfd->sections; s != NULL; s = s->next)
7010 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7011 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7013 elf_hash_table (info)->text_index_section = s;
7018 /* Find two non-excluded output sections, one for code, one for data.
7019 We'll use their section symbols for some emitted relocs. */
7021 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7025 /* Data first, since setting text_index_section changes
7026 _bfd_elf_omit_section_dynsym_default. */
7027 for (s = output_bfd->sections; s != NULL; s = s->next)
7028 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7029 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7031 elf_hash_table (info)->data_index_section = s;
7035 for (s = output_bfd->sections; s != NULL; s = s->next)
7036 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
7037 == (SEC_ALLOC | SEC_READONLY))
7038 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7040 elf_hash_table (info)->text_index_section = s;
7044 if (elf_hash_table (info)->text_index_section == NULL)
7045 elf_hash_table (info)->text_index_section
7046 = elf_hash_table (info)->data_index_section;
7050 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7052 const struct elf_backend_data *bed;
7053 unsigned long section_sym_count;
7054 bfd_size_type dynsymcount = 0;
7056 if (!is_elf_hash_table (info->hash))
7059 bed = get_elf_backend_data (output_bfd);
7060 (*bed->elf_backend_init_index_section) (output_bfd, info);
7062 /* Assign dynsym indices. In a shared library we generate a section
7063 symbol for each output section, which come first. Next come all
7064 of the back-end allocated local dynamic syms, followed by the rest
7065 of the global symbols.
7067 This is usually not needed for static binaries, however backends
7068 can request to always do it, e.g. the MIPS backend uses dynamic
7069 symbol counts to lay out GOT, which will be produced in the
7070 presence of GOT relocations even in static binaries (holding fixed
7071 data in that case, to satisfy those relocations). */
7073 if (elf_hash_table (info)->dynamic_sections_created
7074 || bed->always_renumber_dynsyms)
7075 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7076 §ion_sym_count);
7078 if (elf_hash_table (info)->dynamic_sections_created)
7082 unsigned int dtagcount;
7084 dynobj = elf_hash_table (info)->dynobj;
7086 /* Work out the size of the symbol version section. */
7087 s = bfd_get_linker_section (dynobj, ".gnu.version");
7088 BFD_ASSERT (s != NULL);
7089 if ((s->flags & SEC_EXCLUDE) == 0)
7091 s->size = dynsymcount * sizeof (Elf_External_Versym);
7092 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7093 if (s->contents == NULL)
7096 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7100 /* Set the size of the .dynsym and .hash sections. We counted
7101 the number of dynamic symbols in elf_link_add_object_symbols.
7102 We will build the contents of .dynsym and .hash when we build
7103 the final symbol table, because until then we do not know the
7104 correct value to give the symbols. We built the .dynstr
7105 section as we went along in elf_link_add_object_symbols. */
7106 s = elf_hash_table (info)->dynsym;
7107 BFD_ASSERT (s != NULL);
7108 s->size = dynsymcount * bed->s->sizeof_sym;
7110 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7111 if (s->contents == NULL)
7114 /* The first entry in .dynsym is a dummy symbol. Clear all the
7115 section syms, in case we don't output them all. */
7116 ++section_sym_count;
7117 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7119 elf_hash_table (info)->bucketcount = 0;
7121 /* Compute the size of the hashing table. As a side effect this
7122 computes the hash values for all the names we export. */
7123 if (info->emit_hash)
7125 unsigned long int *hashcodes;
7126 struct hash_codes_info hashinf;
7128 unsigned long int nsyms;
7130 size_t hash_entry_size;
7132 /* Compute the hash values for all exported symbols. At the same
7133 time store the values in an array so that we could use them for
7135 amt = dynsymcount * sizeof (unsigned long int);
7136 hashcodes = (unsigned long int *) bfd_malloc (amt);
7137 if (hashcodes == NULL)
7139 hashinf.hashcodes = hashcodes;
7140 hashinf.error = FALSE;
7142 /* Put all hash values in HASHCODES. */
7143 elf_link_hash_traverse (elf_hash_table (info),
7144 elf_collect_hash_codes, &hashinf);
7151 nsyms = hashinf.hashcodes - hashcodes;
7153 = compute_bucket_count (info, hashcodes, nsyms, 0);
7156 if (bucketcount == 0 && nsyms > 0)
7159 elf_hash_table (info)->bucketcount = bucketcount;
7161 s = bfd_get_linker_section (dynobj, ".hash");
7162 BFD_ASSERT (s != NULL);
7163 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7164 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7165 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7166 if (s->contents == NULL)
7169 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7170 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7171 s->contents + hash_entry_size);
7174 if (info->emit_gnu_hash)
7177 unsigned char *contents;
7178 struct collect_gnu_hash_codes cinfo;
7182 memset (&cinfo, 0, sizeof (cinfo));
7184 /* Compute the hash values for all exported symbols. At the same
7185 time store the values in an array so that we could use them for
7187 amt = dynsymcount * 2 * sizeof (unsigned long int);
7188 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7189 if (cinfo.hashcodes == NULL)
7192 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7193 cinfo.min_dynindx = -1;
7194 cinfo.output_bfd = output_bfd;
7197 /* Put all hash values in HASHCODES. */
7198 elf_link_hash_traverse (elf_hash_table (info),
7199 elf_collect_gnu_hash_codes, &cinfo);
7202 free (cinfo.hashcodes);
7207 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7209 if (bucketcount == 0)
7211 free (cinfo.hashcodes);
7215 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7216 BFD_ASSERT (s != NULL);
7218 if (cinfo.nsyms == 0)
7220 /* Empty .gnu.hash section is special. */
7221 BFD_ASSERT (cinfo.min_dynindx == -1);
7222 free (cinfo.hashcodes);
7223 s->size = 5 * 4 + bed->s->arch_size / 8;
7224 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7225 if (contents == NULL)
7227 s->contents = contents;
7228 /* 1 empty bucket. */
7229 bfd_put_32 (output_bfd, 1, contents);
7230 /* SYMIDX above the special symbol 0. */
7231 bfd_put_32 (output_bfd, 1, contents + 4);
7232 /* Just one word for bitmask. */
7233 bfd_put_32 (output_bfd, 1, contents + 8);
7234 /* Only hash fn bloom filter. */
7235 bfd_put_32 (output_bfd, 0, contents + 12);
7236 /* No hashes are valid - empty bitmask. */
7237 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7238 /* No hashes in the only bucket. */
7239 bfd_put_32 (output_bfd, 0,
7240 contents + 16 + bed->s->arch_size / 8);
7244 unsigned long int maskwords, maskbitslog2, x;
7245 BFD_ASSERT (cinfo.min_dynindx != -1);
7249 while ((x >>= 1) != 0)
7251 if (maskbitslog2 < 3)
7253 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7254 maskbitslog2 = maskbitslog2 + 3;
7256 maskbitslog2 = maskbitslog2 + 2;
7257 if (bed->s->arch_size == 64)
7259 if (maskbitslog2 == 5)
7265 cinfo.mask = (1 << cinfo.shift1) - 1;
7266 cinfo.shift2 = maskbitslog2;
7267 cinfo.maskbits = 1 << maskbitslog2;
7268 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7269 amt = bucketcount * sizeof (unsigned long int) * 2;
7270 amt += maskwords * sizeof (bfd_vma);
7271 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7272 if (cinfo.bitmask == NULL)
7274 free (cinfo.hashcodes);
7278 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7279 cinfo.indx = cinfo.counts + bucketcount;
7280 cinfo.symindx = dynsymcount - cinfo.nsyms;
7281 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7283 /* Determine how often each hash bucket is used. */
7284 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7285 for (i = 0; i < cinfo.nsyms; ++i)
7286 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7288 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7289 if (cinfo.counts[i] != 0)
7291 cinfo.indx[i] = cnt;
7292 cnt += cinfo.counts[i];
7294 BFD_ASSERT (cnt == dynsymcount);
7295 cinfo.bucketcount = bucketcount;
7296 cinfo.local_indx = cinfo.min_dynindx;
7298 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7299 s->size += cinfo.maskbits / 8;
7300 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7301 if (contents == NULL)
7303 free (cinfo.bitmask);
7304 free (cinfo.hashcodes);
7308 s->contents = contents;
7309 bfd_put_32 (output_bfd, bucketcount, contents);
7310 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7311 bfd_put_32 (output_bfd, maskwords, contents + 8);
7312 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7313 contents += 16 + cinfo.maskbits / 8;
7315 for (i = 0; i < bucketcount; ++i)
7317 if (cinfo.counts[i] == 0)
7318 bfd_put_32 (output_bfd, 0, contents);
7320 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7324 cinfo.contents = contents;
7326 /* Renumber dynamic symbols, populate .gnu.hash section. */
7327 elf_link_hash_traverse (elf_hash_table (info),
7328 elf_renumber_gnu_hash_syms, &cinfo);
7330 contents = s->contents + 16;
7331 for (i = 0; i < maskwords; ++i)
7333 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7335 contents += bed->s->arch_size / 8;
7338 free (cinfo.bitmask);
7339 free (cinfo.hashcodes);
7343 s = bfd_get_linker_section (dynobj, ".dynstr");
7344 BFD_ASSERT (s != NULL);
7346 elf_finalize_dynstr (output_bfd, info);
7348 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7350 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7351 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7358 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7361 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7364 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7365 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7368 /* Finish SHF_MERGE section merging. */
7371 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7376 if (!is_elf_hash_table (info->hash))
7379 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7380 if ((ibfd->flags & DYNAMIC) == 0
7381 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7382 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7383 == get_elf_backend_data (obfd)->s->elfclass))
7384 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7385 if ((sec->flags & SEC_MERGE) != 0
7386 && !bfd_is_abs_section (sec->output_section))
7388 struct bfd_elf_section_data *secdata;
7390 secdata = elf_section_data (sec);
7391 if (! _bfd_add_merge_section (obfd,
7392 &elf_hash_table (info)->merge_info,
7393 sec, &secdata->sec_info))
7395 else if (secdata->sec_info)
7396 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7399 if (elf_hash_table (info)->merge_info != NULL)
7400 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7401 merge_sections_remove_hook);
7405 /* Create an entry in an ELF linker hash table. */
7407 struct bfd_hash_entry *
7408 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7409 struct bfd_hash_table *table,
7412 /* Allocate the structure if it has not already been allocated by a
7416 entry = (struct bfd_hash_entry *)
7417 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7422 /* Call the allocation method of the superclass. */
7423 entry = _bfd_link_hash_newfunc (entry, table, string);
7426 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7427 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7429 /* Set local fields. */
7432 ret->got = htab->init_got_refcount;
7433 ret->plt = htab->init_plt_refcount;
7434 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7435 - offsetof (struct elf_link_hash_entry, size)));
7436 /* Assume that we have been called by a non-ELF symbol reader.
7437 This flag is then reset by the code which reads an ELF input
7438 file. This ensures that a symbol created by a non-ELF symbol
7439 reader will have the flag set correctly. */
7446 /* Copy data from an indirect symbol to its direct symbol, hiding the
7447 old indirect symbol. Also used for copying flags to a weakdef. */
7450 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7451 struct elf_link_hash_entry *dir,
7452 struct elf_link_hash_entry *ind)
7454 struct elf_link_hash_table *htab;
7456 /* Copy down any references that we may have already seen to the
7457 symbol which just became indirect. */
7459 if (dir->versioned != versioned_hidden)
7460 dir->ref_dynamic |= ind->ref_dynamic;
7461 dir->ref_regular |= ind->ref_regular;
7462 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7463 dir->non_got_ref |= ind->non_got_ref;
7464 dir->needs_plt |= ind->needs_plt;
7465 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7467 if (ind->root.type != bfd_link_hash_indirect)
7470 /* Copy over the global and procedure linkage table refcount entries.
7471 These may have been already set up by a check_relocs routine. */
7472 htab = elf_hash_table (info);
7473 if (ind->got.refcount > htab->init_got_refcount.refcount)
7475 if (dir->got.refcount < 0)
7476 dir->got.refcount = 0;
7477 dir->got.refcount += ind->got.refcount;
7478 ind->got.refcount = htab->init_got_refcount.refcount;
7481 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7483 if (dir->plt.refcount < 0)
7484 dir->plt.refcount = 0;
7485 dir->plt.refcount += ind->plt.refcount;
7486 ind->plt.refcount = htab->init_plt_refcount.refcount;
7489 if (ind->dynindx != -1)
7491 if (dir->dynindx != -1)
7492 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7493 dir->dynindx = ind->dynindx;
7494 dir->dynstr_index = ind->dynstr_index;
7496 ind->dynstr_index = 0;
7501 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7502 struct elf_link_hash_entry *h,
7503 bfd_boolean force_local)
7505 /* STT_GNU_IFUNC symbol must go through PLT. */
7506 if (h->type != STT_GNU_IFUNC)
7508 h->plt = elf_hash_table (info)->init_plt_offset;
7513 h->forced_local = 1;
7514 if (h->dynindx != -1)
7516 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7519 h->dynstr_index = 0;
7524 /* Hide a symbol. */
7527 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7528 struct bfd_link_info *info,
7529 struct bfd_link_hash_entry *h)
7531 if (is_elf_hash_table (info->hash))
7533 const struct elf_backend_data *bed
7534 = get_elf_backend_data (output_bfd);
7535 struct elf_link_hash_entry *eh
7536 = (struct elf_link_hash_entry *) h;
7537 bed->elf_backend_hide_symbol (info, eh, TRUE);
7538 eh->def_dynamic = 0;
7539 eh->ref_dynamic = 0;
7540 eh->dynamic_def = 0;
7544 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7548 _bfd_elf_link_hash_table_init
7549 (struct elf_link_hash_table *table,
7551 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7552 struct bfd_hash_table *,
7554 unsigned int entsize,
7555 enum elf_target_id target_id)
7558 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7560 table->init_got_refcount.refcount = can_refcount - 1;
7561 table->init_plt_refcount.refcount = can_refcount - 1;
7562 table->init_got_offset.offset = -(bfd_vma) 1;
7563 table->init_plt_offset.offset = -(bfd_vma) 1;
7564 /* The first dynamic symbol is a dummy. */
7565 table->dynsymcount = 1;
7567 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7569 table->root.type = bfd_link_elf_hash_table;
7570 table->hash_table_id = target_id;
7575 /* Create an ELF linker hash table. */
7577 struct bfd_link_hash_table *
7578 _bfd_elf_link_hash_table_create (bfd *abfd)
7580 struct elf_link_hash_table *ret;
7581 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7583 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7587 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7588 sizeof (struct elf_link_hash_entry),
7594 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7599 /* Destroy an ELF linker hash table. */
7602 _bfd_elf_link_hash_table_free (bfd *obfd)
7604 struct elf_link_hash_table *htab;
7606 htab = (struct elf_link_hash_table *) obfd->link.hash;
7607 if (htab->dynstr != NULL)
7608 _bfd_elf_strtab_free (htab->dynstr);
7609 _bfd_merge_sections_free (htab->merge_info);
7610 _bfd_generic_link_hash_table_free (obfd);
7613 /* This is a hook for the ELF emulation code in the generic linker to
7614 tell the backend linker what file name to use for the DT_NEEDED
7615 entry for a dynamic object. */
7618 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7620 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7621 && bfd_get_format (abfd) == bfd_object)
7622 elf_dt_name (abfd) = name;
7626 bfd_elf_get_dyn_lib_class (bfd *abfd)
7629 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7630 && bfd_get_format (abfd) == bfd_object)
7631 lib_class = elf_dyn_lib_class (abfd);
7638 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7640 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7641 && bfd_get_format (abfd) == bfd_object)
7642 elf_dyn_lib_class (abfd) = lib_class;
7645 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7646 the linker ELF emulation code. */
7648 struct bfd_link_needed_list *
7649 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7650 struct bfd_link_info *info)
7652 if (! is_elf_hash_table (info->hash))
7654 return elf_hash_table (info)->needed;
7657 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7658 hook for the linker ELF emulation code. */
7660 struct bfd_link_needed_list *
7661 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7662 struct bfd_link_info *info)
7664 if (! is_elf_hash_table (info->hash))
7666 return elf_hash_table (info)->runpath;
7669 /* Get the name actually used for a dynamic object for a link. This
7670 is the SONAME entry if there is one. Otherwise, it is the string
7671 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7674 bfd_elf_get_dt_soname (bfd *abfd)
7676 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7677 && bfd_get_format (abfd) == bfd_object)
7678 return elf_dt_name (abfd);
7682 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7683 the ELF linker emulation code. */
7686 bfd_elf_get_bfd_needed_list (bfd *abfd,
7687 struct bfd_link_needed_list **pneeded)
7690 bfd_byte *dynbuf = NULL;
7691 unsigned int elfsec;
7692 unsigned long shlink;
7693 bfd_byte *extdyn, *extdynend;
7695 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7699 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7700 || bfd_get_format (abfd) != bfd_object)
7703 s = bfd_get_section_by_name (abfd, ".dynamic");
7704 if (s == NULL || s->size == 0)
7707 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7710 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7711 if (elfsec == SHN_BAD)
7714 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7716 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7717 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7720 extdynend = extdyn + s->size;
7721 for (; extdyn < extdynend; extdyn += extdynsize)
7723 Elf_Internal_Dyn dyn;
7725 (*swap_dyn_in) (abfd, extdyn, &dyn);
7727 if (dyn.d_tag == DT_NULL)
7730 if (dyn.d_tag == DT_NEEDED)
7733 struct bfd_link_needed_list *l;
7734 unsigned int tagv = dyn.d_un.d_val;
7737 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7742 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7763 struct elf_symbuf_symbol
7765 unsigned long st_name; /* Symbol name, index in string tbl */
7766 unsigned char st_info; /* Type and binding attributes */
7767 unsigned char st_other; /* Visibilty, and target specific */
7770 struct elf_symbuf_head
7772 struct elf_symbuf_symbol *ssym;
7774 unsigned int st_shndx;
7781 Elf_Internal_Sym *isym;
7782 struct elf_symbuf_symbol *ssym;
7787 /* Sort references to symbols by ascending section number. */
7790 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7792 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7793 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7795 return s1->st_shndx - s2->st_shndx;
7799 elf_sym_name_compare (const void *arg1, const void *arg2)
7801 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7802 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7803 return strcmp (s1->name, s2->name);
7806 static struct elf_symbuf_head *
7807 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7809 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7810 struct elf_symbuf_symbol *ssym;
7811 struct elf_symbuf_head *ssymbuf, *ssymhead;
7812 size_t i, shndx_count, total_size;
7814 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7818 for (ind = indbuf, i = 0; i < symcount; i++)
7819 if (isymbuf[i].st_shndx != SHN_UNDEF)
7820 *ind++ = &isymbuf[i];
7823 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7824 elf_sort_elf_symbol);
7827 if (indbufend > indbuf)
7828 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7829 if (ind[0]->st_shndx != ind[1]->st_shndx)
7832 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7833 + (indbufend - indbuf) * sizeof (*ssym));
7834 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7835 if (ssymbuf == NULL)
7841 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7842 ssymbuf->ssym = NULL;
7843 ssymbuf->count = shndx_count;
7844 ssymbuf->st_shndx = 0;
7845 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7847 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7850 ssymhead->ssym = ssym;
7851 ssymhead->count = 0;
7852 ssymhead->st_shndx = (*ind)->st_shndx;
7854 ssym->st_name = (*ind)->st_name;
7855 ssym->st_info = (*ind)->st_info;
7856 ssym->st_other = (*ind)->st_other;
7859 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7860 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7867 /* Check if 2 sections define the same set of local and global
7871 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7872 struct bfd_link_info *info)
7875 const struct elf_backend_data *bed1, *bed2;
7876 Elf_Internal_Shdr *hdr1, *hdr2;
7877 size_t symcount1, symcount2;
7878 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7879 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7880 Elf_Internal_Sym *isym, *isymend;
7881 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7882 size_t count1, count2, i;
7883 unsigned int shndx1, shndx2;
7889 /* Both sections have to be in ELF. */
7890 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7891 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7894 if (elf_section_type (sec1) != elf_section_type (sec2))
7897 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7898 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7899 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7902 bed1 = get_elf_backend_data (bfd1);
7903 bed2 = get_elf_backend_data (bfd2);
7904 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7905 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7906 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7907 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7909 if (symcount1 == 0 || symcount2 == 0)
7915 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7916 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7918 if (ssymbuf1 == NULL)
7920 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7922 if (isymbuf1 == NULL)
7925 if (!info->reduce_memory_overheads)
7926 elf_tdata (bfd1)->symbuf = ssymbuf1
7927 = elf_create_symbuf (symcount1, isymbuf1);
7930 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7932 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7934 if (isymbuf2 == NULL)
7937 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7938 elf_tdata (bfd2)->symbuf = ssymbuf2
7939 = elf_create_symbuf (symcount2, isymbuf2);
7942 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7944 /* Optimized faster version. */
7946 struct elf_symbol *symp;
7947 struct elf_symbuf_symbol *ssym, *ssymend;
7950 hi = ssymbuf1->count;
7955 mid = (lo + hi) / 2;
7956 if (shndx1 < ssymbuf1[mid].st_shndx)
7958 else if (shndx1 > ssymbuf1[mid].st_shndx)
7962 count1 = ssymbuf1[mid].count;
7969 hi = ssymbuf2->count;
7974 mid = (lo + hi) / 2;
7975 if (shndx2 < ssymbuf2[mid].st_shndx)
7977 else if (shndx2 > ssymbuf2[mid].st_shndx)
7981 count2 = ssymbuf2[mid].count;
7987 if (count1 == 0 || count2 == 0 || count1 != count2)
7991 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7993 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7994 if (symtable1 == NULL || symtable2 == NULL)
7998 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7999 ssym < ssymend; ssym++, symp++)
8001 symp->u.ssym = ssym;
8002 symp->name = bfd_elf_string_from_elf_section (bfd1,
8008 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8009 ssym < ssymend; ssym++, symp++)
8011 symp->u.ssym = ssym;
8012 symp->name = bfd_elf_string_from_elf_section (bfd2,
8017 /* Sort symbol by name. */
8018 qsort (symtable1, count1, sizeof (struct elf_symbol),
8019 elf_sym_name_compare);
8020 qsort (symtable2, count1, sizeof (struct elf_symbol),
8021 elf_sym_name_compare);
8023 for (i = 0; i < count1; i++)
8024 /* Two symbols must have the same binding, type and name. */
8025 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8026 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8027 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8034 symtable1 = (struct elf_symbol *)
8035 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8036 symtable2 = (struct elf_symbol *)
8037 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8038 if (symtable1 == NULL || symtable2 == NULL)
8041 /* Count definitions in the section. */
8043 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8044 if (isym->st_shndx == shndx1)
8045 symtable1[count1++].u.isym = isym;
8048 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8049 if (isym->st_shndx == shndx2)
8050 symtable2[count2++].u.isym = isym;
8052 if (count1 == 0 || count2 == 0 || count1 != count2)
8055 for (i = 0; i < count1; i++)
8057 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8058 symtable1[i].u.isym->st_name);
8060 for (i = 0; i < count2; i++)
8062 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8063 symtable2[i].u.isym->st_name);
8065 /* Sort symbol by name. */
8066 qsort (symtable1, count1, sizeof (struct elf_symbol),
8067 elf_sym_name_compare);
8068 qsort (symtable2, count1, sizeof (struct elf_symbol),
8069 elf_sym_name_compare);
8071 for (i = 0; i < count1; i++)
8072 /* Two symbols must have the same binding, type and name. */
8073 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8074 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8075 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8093 /* Return TRUE if 2 section types are compatible. */
8096 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8097 bfd *bbfd, const asection *bsec)
8101 || abfd->xvec->flavour != bfd_target_elf_flavour
8102 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8105 return elf_section_type (asec) == elf_section_type (bsec);
8108 /* Final phase of ELF linker. */
8110 /* A structure we use to avoid passing large numbers of arguments. */
8112 struct elf_final_link_info
8114 /* General link information. */
8115 struct bfd_link_info *info;
8118 /* Symbol string table. */
8119 struct elf_strtab_hash *symstrtab;
8120 /* .hash section. */
8122 /* symbol version section (.gnu.version). */
8123 asection *symver_sec;
8124 /* Buffer large enough to hold contents of any section. */
8126 /* Buffer large enough to hold external relocs of any section. */
8127 void *external_relocs;
8128 /* Buffer large enough to hold internal relocs of any section. */
8129 Elf_Internal_Rela *internal_relocs;
8130 /* Buffer large enough to hold external local symbols of any input
8132 bfd_byte *external_syms;
8133 /* And a buffer for symbol section indices. */
8134 Elf_External_Sym_Shndx *locsym_shndx;
8135 /* Buffer large enough to hold internal local symbols of any input
8137 Elf_Internal_Sym *internal_syms;
8138 /* Array large enough to hold a symbol index for each local symbol
8139 of any input BFD. */
8141 /* Array large enough to hold a section pointer for each local
8142 symbol of any input BFD. */
8143 asection **sections;
8144 /* Buffer for SHT_SYMTAB_SHNDX section. */
8145 Elf_External_Sym_Shndx *symshndxbuf;
8146 /* Number of STT_FILE syms seen. */
8147 size_t filesym_count;
8150 /* This struct is used to pass information to elf_link_output_extsym. */
8152 struct elf_outext_info
8155 bfd_boolean localsyms;
8156 bfd_boolean file_sym_done;
8157 struct elf_final_link_info *flinfo;
8161 /* Support for evaluating a complex relocation.
8163 Complex relocations are generalized, self-describing relocations. The
8164 implementation of them consists of two parts: complex symbols, and the
8165 relocations themselves.
8167 The relocations are use a reserved elf-wide relocation type code (R_RELC
8168 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8169 information (start bit, end bit, word width, etc) into the addend. This
8170 information is extracted from CGEN-generated operand tables within gas.
8172 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8173 internal) representing prefix-notation expressions, including but not
8174 limited to those sorts of expressions normally encoded as addends in the
8175 addend field. The symbol mangling format is:
8178 | <unary-operator> ':' <node>
8179 | <binary-operator> ':' <node> ':' <node>
8182 <literal> := 's' <digits=N> ':' <N character symbol name>
8183 | 'S' <digits=N> ':' <N character section name>
8187 <binary-operator> := as in C
8188 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8191 set_symbol_value (bfd *bfd_with_globals,
8192 Elf_Internal_Sym *isymbuf,
8197 struct elf_link_hash_entry **sym_hashes;
8198 struct elf_link_hash_entry *h;
8199 size_t extsymoff = locsymcount;
8201 if (symidx < locsymcount)
8203 Elf_Internal_Sym *sym;
8205 sym = isymbuf + symidx;
8206 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8208 /* It is a local symbol: move it to the
8209 "absolute" section and give it a value. */
8210 sym->st_shndx = SHN_ABS;
8211 sym->st_value = val;
8214 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8218 /* It is a global symbol: set its link type
8219 to "defined" and give it a value. */
8221 sym_hashes = elf_sym_hashes (bfd_with_globals);
8222 h = sym_hashes [symidx - extsymoff];
8223 while (h->root.type == bfd_link_hash_indirect
8224 || h->root.type == bfd_link_hash_warning)
8225 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8226 h->root.type = bfd_link_hash_defined;
8227 h->root.u.def.value = val;
8228 h->root.u.def.section = bfd_abs_section_ptr;
8232 resolve_symbol (const char *name,
8234 struct elf_final_link_info *flinfo,
8236 Elf_Internal_Sym *isymbuf,
8239 Elf_Internal_Sym *sym;
8240 struct bfd_link_hash_entry *global_entry;
8241 const char *candidate = NULL;
8242 Elf_Internal_Shdr *symtab_hdr;
8245 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8247 for (i = 0; i < locsymcount; ++ i)
8251 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8254 candidate = bfd_elf_string_from_elf_section (input_bfd,
8255 symtab_hdr->sh_link,
8258 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8259 name, candidate, (unsigned long) sym->st_value);
8261 if (candidate && strcmp (candidate, name) == 0)
8263 asection *sec = flinfo->sections [i];
8265 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8266 *result += sec->output_offset + sec->output_section->vma;
8268 printf ("Found symbol with value %8.8lx\n",
8269 (unsigned long) *result);
8275 /* Hmm, haven't found it yet. perhaps it is a global. */
8276 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8277 FALSE, FALSE, TRUE);
8281 if (global_entry->type == bfd_link_hash_defined
8282 || global_entry->type == bfd_link_hash_defweak)
8284 *result = (global_entry->u.def.value
8285 + global_entry->u.def.section->output_section->vma
8286 + global_entry->u.def.section->output_offset);
8288 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8289 global_entry->root.string, (unsigned long) *result);
8297 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8298 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8299 names like "foo.end" which is the end address of section "foo". */
8302 resolve_section (const char *name,
8310 for (curr = sections; curr; curr = curr->next)
8311 if (strcmp (curr->name, name) == 0)
8313 *result = curr->vma;
8317 /* Hmm. still haven't found it. try pseudo-section names. */
8318 /* FIXME: This could be coded more efficiently... */
8319 for (curr = sections; curr; curr = curr->next)
8321 len = strlen (curr->name);
8322 if (len > strlen (name))
8325 if (strncmp (curr->name, name, len) == 0)
8327 if (strncmp (".end", name + len, 4) == 0)
8329 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8333 /* Insert more pseudo-section names here, if you like. */
8341 undefined_reference (const char *reftype, const char *name)
8343 /* xgettext:c-format */
8344 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8349 eval_symbol (bfd_vma *result,
8352 struct elf_final_link_info *flinfo,
8354 Elf_Internal_Sym *isymbuf,
8363 const char *sym = *symp;
8365 bfd_boolean symbol_is_section = FALSE;
8370 if (len < 1 || len > sizeof (symbuf))
8372 bfd_set_error (bfd_error_invalid_operation);
8385 *result = strtoul (sym, (char **) symp, 16);
8389 symbol_is_section = TRUE;
8393 symlen = strtol (sym, (char **) symp, 10);
8394 sym = *symp + 1; /* Skip the trailing ':'. */
8396 if (symend < sym || symlen + 1 > sizeof (symbuf))
8398 bfd_set_error (bfd_error_invalid_operation);
8402 memcpy (symbuf, sym, symlen);
8403 symbuf[symlen] = '\0';
8404 *symp = sym + symlen;
8406 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8407 the symbol as a section, or vice-versa. so we're pretty liberal in our
8408 interpretation here; section means "try section first", not "must be a
8409 section", and likewise with symbol. */
8411 if (symbol_is_section)
8413 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8414 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8415 isymbuf, locsymcount))
8417 undefined_reference ("section", symbuf);
8423 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8424 isymbuf, locsymcount)
8425 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8428 undefined_reference ("symbol", symbuf);
8435 /* All that remains are operators. */
8437 #define UNARY_OP(op) \
8438 if (strncmp (sym, #op, strlen (#op)) == 0) \
8440 sym += strlen (#op); \
8444 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8445 isymbuf, locsymcount, signed_p)) \
8448 *result = op ((bfd_signed_vma) a); \
8454 #define BINARY_OP(op) \
8455 if (strncmp (sym, #op, strlen (#op)) == 0) \
8457 sym += strlen (#op); \
8461 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8462 isymbuf, locsymcount, signed_p)) \
8465 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8466 isymbuf, locsymcount, signed_p)) \
8469 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8499 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8500 bfd_set_error (bfd_error_invalid_operation);
8506 put_value (bfd_vma size,
8507 unsigned long chunksz,
8512 location += (size - chunksz);
8514 for (; size; size -= chunksz, location -= chunksz)
8519 bfd_put_8 (input_bfd, x, location);
8523 bfd_put_16 (input_bfd, x, location);
8527 bfd_put_32 (input_bfd, x, location);
8528 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8534 bfd_put_64 (input_bfd, x, location);
8535 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8548 get_value (bfd_vma size,
8549 unsigned long chunksz,
8556 /* Sanity checks. */
8557 BFD_ASSERT (chunksz <= sizeof (x)
8560 && (size % chunksz) == 0
8561 && input_bfd != NULL
8562 && location != NULL);
8564 if (chunksz == sizeof (x))
8566 BFD_ASSERT (size == chunksz);
8568 /* Make sure that we do not perform an undefined shift operation.
8569 We know that size == chunksz so there will only be one iteration
8570 of the loop below. */
8574 shift = 8 * chunksz;
8576 for (; size; size -= chunksz, location += chunksz)
8581 x = (x << shift) | bfd_get_8 (input_bfd, location);
8584 x = (x << shift) | bfd_get_16 (input_bfd, location);
8587 x = (x << shift) | bfd_get_32 (input_bfd, location);
8591 x = (x << shift) | bfd_get_64 (input_bfd, location);
8602 decode_complex_addend (unsigned long *start, /* in bits */
8603 unsigned long *oplen, /* in bits */
8604 unsigned long *len, /* in bits */
8605 unsigned long *wordsz, /* in bytes */
8606 unsigned long *chunksz, /* in bytes */
8607 unsigned long *lsb0_p,
8608 unsigned long *signed_p,
8609 unsigned long *trunc_p,
8610 unsigned long encoded)
8612 * start = encoded & 0x3F;
8613 * len = (encoded >> 6) & 0x3F;
8614 * oplen = (encoded >> 12) & 0x3F;
8615 * wordsz = (encoded >> 18) & 0xF;
8616 * chunksz = (encoded >> 22) & 0xF;
8617 * lsb0_p = (encoded >> 27) & 1;
8618 * signed_p = (encoded >> 28) & 1;
8619 * trunc_p = (encoded >> 29) & 1;
8622 bfd_reloc_status_type
8623 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8624 asection *input_section ATTRIBUTE_UNUSED,
8626 Elf_Internal_Rela *rel,
8629 bfd_vma shift, x, mask;
8630 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8631 bfd_reloc_status_type r;
8633 /* Perform this reloc, since it is complex.
8634 (this is not to say that it necessarily refers to a complex
8635 symbol; merely that it is a self-describing CGEN based reloc.
8636 i.e. the addend has the complete reloc information (bit start, end,
8637 word size, etc) encoded within it.). */
8639 decode_complex_addend (&start, &oplen, &len, &wordsz,
8640 &chunksz, &lsb0_p, &signed_p,
8641 &trunc_p, rel->r_addend);
8643 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8646 shift = (start + 1) - len;
8648 shift = (8 * wordsz) - (start + len);
8650 x = get_value (wordsz, chunksz, input_bfd,
8651 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8654 printf ("Doing complex reloc: "
8655 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8656 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8657 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8658 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8659 oplen, (unsigned long) x, (unsigned long) mask,
8660 (unsigned long) relocation);
8665 /* Now do an overflow check. */
8666 r = bfd_check_overflow ((signed_p
8667 ? complain_overflow_signed
8668 : complain_overflow_unsigned),
8669 len, 0, (8 * wordsz),
8673 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8676 printf (" relocation: %8.8lx\n"
8677 " shifted mask: %8.8lx\n"
8678 " shifted/masked reloc: %8.8lx\n"
8679 " result: %8.8lx\n",
8680 (unsigned long) relocation, (unsigned long) (mask << shift),
8681 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8683 put_value (wordsz, chunksz, input_bfd, x,
8684 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8688 /* Functions to read r_offset from external (target order) reloc
8689 entry. Faster than bfd_getl32 et al, because we let the compiler
8690 know the value is aligned. */
8693 ext32l_r_offset (const void *p)
8700 const union aligned32 *a
8701 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8703 uint32_t aval = ( (uint32_t) a->c[0]
8704 | (uint32_t) a->c[1] << 8
8705 | (uint32_t) a->c[2] << 16
8706 | (uint32_t) a->c[3] << 24);
8711 ext32b_r_offset (const void *p)
8718 const union aligned32 *a
8719 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8721 uint32_t aval = ( (uint32_t) a->c[0] << 24
8722 | (uint32_t) a->c[1] << 16
8723 | (uint32_t) a->c[2] << 8
8724 | (uint32_t) a->c[3]);
8728 #ifdef BFD_HOST_64_BIT
8730 ext64l_r_offset (const void *p)
8737 const union aligned64 *a
8738 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8740 uint64_t aval = ( (uint64_t) a->c[0]
8741 | (uint64_t) a->c[1] << 8
8742 | (uint64_t) a->c[2] << 16
8743 | (uint64_t) a->c[3] << 24
8744 | (uint64_t) a->c[4] << 32
8745 | (uint64_t) a->c[5] << 40
8746 | (uint64_t) a->c[6] << 48
8747 | (uint64_t) a->c[7] << 56);
8752 ext64b_r_offset (const void *p)
8759 const union aligned64 *a
8760 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8762 uint64_t aval = ( (uint64_t) a->c[0] << 56
8763 | (uint64_t) a->c[1] << 48
8764 | (uint64_t) a->c[2] << 40
8765 | (uint64_t) a->c[3] << 32
8766 | (uint64_t) a->c[4] << 24
8767 | (uint64_t) a->c[5] << 16
8768 | (uint64_t) a->c[6] << 8
8769 | (uint64_t) a->c[7]);
8774 /* When performing a relocatable link, the input relocations are
8775 preserved. But, if they reference global symbols, the indices
8776 referenced must be updated. Update all the relocations found in
8780 elf_link_adjust_relocs (bfd *abfd,
8782 struct bfd_elf_section_reloc_data *reldata,
8784 struct bfd_link_info *info)
8787 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8789 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8790 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8791 bfd_vma r_type_mask;
8793 unsigned int count = reldata->count;
8794 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8796 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8798 swap_in = bed->s->swap_reloc_in;
8799 swap_out = bed->s->swap_reloc_out;
8801 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8803 swap_in = bed->s->swap_reloca_in;
8804 swap_out = bed->s->swap_reloca_out;
8809 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8812 if (bed->s->arch_size == 32)
8819 r_type_mask = 0xffffffff;
8823 erela = reldata->hdr->contents;
8824 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8826 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8829 if (*rel_hash == NULL)
8832 if ((*rel_hash)->indx == -2
8833 && info->gc_sections
8834 && ! info->gc_keep_exported)
8836 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8837 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8839 (*rel_hash)->root.root.string);
8840 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8842 bfd_set_error (bfd_error_invalid_operation);
8845 BFD_ASSERT ((*rel_hash)->indx >= 0);
8847 (*swap_in) (abfd, erela, irela);
8848 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8849 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8850 | (irela[j].r_info & r_type_mask));
8851 (*swap_out) (abfd, irela, erela);
8854 if (bed->elf_backend_update_relocs)
8855 (*bed->elf_backend_update_relocs) (sec, reldata);
8857 if (sort && count != 0)
8859 bfd_vma (*ext_r_off) (const void *);
8862 bfd_byte *base, *end, *p, *loc;
8863 bfd_byte *buf = NULL;
8865 if (bed->s->arch_size == 32)
8867 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8868 ext_r_off = ext32l_r_offset;
8869 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8870 ext_r_off = ext32b_r_offset;
8876 #ifdef BFD_HOST_64_BIT
8877 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8878 ext_r_off = ext64l_r_offset;
8879 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8880 ext_r_off = ext64b_r_offset;
8886 /* Must use a stable sort here. A modified insertion sort,
8887 since the relocs are mostly sorted already. */
8888 elt_size = reldata->hdr->sh_entsize;
8889 base = reldata->hdr->contents;
8890 end = base + count * elt_size;
8891 if (elt_size > sizeof (Elf64_External_Rela))
8894 /* Ensure the first element is lowest. This acts as a sentinel,
8895 speeding the main loop below. */
8896 r_off = (*ext_r_off) (base);
8897 for (p = loc = base; (p += elt_size) < end; )
8899 bfd_vma r_off2 = (*ext_r_off) (p);
8908 /* Don't just swap *base and *loc as that changes the order
8909 of the original base[0] and base[1] if they happen to
8910 have the same r_offset. */
8911 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8912 memcpy (onebuf, loc, elt_size);
8913 memmove (base + elt_size, base, loc - base);
8914 memcpy (base, onebuf, elt_size);
8917 for (p = base + elt_size; (p += elt_size) < end; )
8919 /* base to p is sorted, *p is next to insert. */
8920 r_off = (*ext_r_off) (p);
8921 /* Search the sorted region for location to insert. */
8923 while (r_off < (*ext_r_off) (loc))
8928 /* Chances are there is a run of relocs to insert here,
8929 from one of more input files. Files are not always
8930 linked in order due to the way elf_link_input_bfd is
8931 called. See pr17666. */
8932 size_t sortlen = p - loc;
8933 bfd_vma r_off2 = (*ext_r_off) (loc);
8934 size_t runlen = elt_size;
8935 size_t buf_size = 96 * 1024;
8936 while (p + runlen < end
8937 && (sortlen <= buf_size
8938 || runlen + elt_size <= buf_size)
8939 && r_off2 > (*ext_r_off) (p + runlen))
8943 buf = bfd_malloc (buf_size);
8947 if (runlen < sortlen)
8949 memcpy (buf, p, runlen);
8950 memmove (loc + runlen, loc, sortlen);
8951 memcpy (loc, buf, runlen);
8955 memcpy (buf, loc, sortlen);
8956 memmove (loc, p, runlen);
8957 memcpy (loc + runlen, buf, sortlen);
8959 p += runlen - elt_size;
8962 /* Hashes are no longer valid. */
8963 free (reldata->hashes);
8964 reldata->hashes = NULL;
8970 struct elf_link_sort_rela
8976 enum elf_reloc_type_class type;
8977 /* We use this as an array of size int_rels_per_ext_rel. */
8978 Elf_Internal_Rela rela[1];
8982 elf_link_sort_cmp1 (const void *A, const void *B)
8984 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8985 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8986 int relativea, relativeb;
8988 relativea = a->type == reloc_class_relative;
8989 relativeb = b->type == reloc_class_relative;
8991 if (relativea < relativeb)
8993 if (relativea > relativeb)
8995 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8997 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8999 if (a->rela->r_offset < b->rela->r_offset)
9001 if (a->rela->r_offset > b->rela->r_offset)
9007 elf_link_sort_cmp2 (const void *A, const void *B)
9009 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9010 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9012 if (a->type < b->type)
9014 if (a->type > b->type)
9016 if (a->u.offset < b->u.offset)
9018 if (a->u.offset > b->u.offset)
9020 if (a->rela->r_offset < b->rela->r_offset)
9022 if (a->rela->r_offset > b->rela->r_offset)
9028 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9030 asection *dynamic_relocs;
9033 bfd_size_type count, size;
9034 size_t i, ret, sort_elt, ext_size;
9035 bfd_byte *sort, *s_non_relative, *p;
9036 struct elf_link_sort_rela *sq;
9037 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9038 int i2e = bed->s->int_rels_per_ext_rel;
9039 unsigned int opb = bfd_octets_per_byte (abfd);
9040 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9041 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9042 struct bfd_link_order *lo;
9044 bfd_boolean use_rela;
9046 /* Find a dynamic reloc section. */
9047 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9048 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9049 if (rela_dyn != NULL && rela_dyn->size > 0
9050 && rel_dyn != NULL && rel_dyn->size > 0)
9052 bfd_boolean use_rela_initialised = FALSE;
9054 /* This is just here to stop gcc from complaining.
9055 Its initialization checking code is not perfect. */
9058 /* Both sections are present. Examine the sizes
9059 of the indirect sections to help us choose. */
9060 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9061 if (lo->type == bfd_indirect_link_order)
9063 asection *o = lo->u.indirect.section;
9065 if ((o->size % bed->s->sizeof_rela) == 0)
9067 if ((o->size % bed->s->sizeof_rel) == 0)
9068 /* Section size is divisible by both rel and rela sizes.
9069 It is of no help to us. */
9073 /* Section size is only divisible by rela. */
9074 if (use_rela_initialised && !use_rela)
9076 _bfd_error_handler (_("%pB: unable to sort relocs - "
9077 "they are in more than one size"),
9079 bfd_set_error (bfd_error_invalid_operation);
9085 use_rela_initialised = TRUE;
9089 else if ((o->size % bed->s->sizeof_rel) == 0)
9091 /* Section size is only divisible by rel. */
9092 if (use_rela_initialised && use_rela)
9094 _bfd_error_handler (_("%pB: unable to sort relocs - "
9095 "they are in more than one size"),
9097 bfd_set_error (bfd_error_invalid_operation);
9103 use_rela_initialised = TRUE;
9108 /* The section size is not divisible by either -
9109 something is wrong. */
9110 _bfd_error_handler (_("%pB: unable to sort relocs - "
9111 "they are of an unknown size"), abfd);
9112 bfd_set_error (bfd_error_invalid_operation);
9117 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9118 if (lo->type == bfd_indirect_link_order)
9120 asection *o = lo->u.indirect.section;
9122 if ((o->size % bed->s->sizeof_rela) == 0)
9124 if ((o->size % bed->s->sizeof_rel) == 0)
9125 /* Section size is divisible by both rel and rela sizes.
9126 It is of no help to us. */
9130 /* Section size is only divisible by rela. */
9131 if (use_rela_initialised && !use_rela)
9133 _bfd_error_handler (_("%pB: unable to sort relocs - "
9134 "they are in more than one size"),
9136 bfd_set_error (bfd_error_invalid_operation);
9142 use_rela_initialised = TRUE;
9146 else if ((o->size % bed->s->sizeof_rel) == 0)
9148 /* Section size is only divisible by rel. */
9149 if (use_rela_initialised && use_rela)
9151 _bfd_error_handler (_("%pB: unable to sort relocs - "
9152 "they are in more than one size"),
9154 bfd_set_error (bfd_error_invalid_operation);
9160 use_rela_initialised = TRUE;
9165 /* The section size is not divisible by either -
9166 something is wrong. */
9167 _bfd_error_handler (_("%pB: unable to sort relocs - "
9168 "they are of an unknown size"), abfd);
9169 bfd_set_error (bfd_error_invalid_operation);
9174 if (! use_rela_initialised)
9178 else if (rela_dyn != NULL && rela_dyn->size > 0)
9180 else if (rel_dyn != NULL && rel_dyn->size > 0)
9187 dynamic_relocs = rela_dyn;
9188 ext_size = bed->s->sizeof_rela;
9189 swap_in = bed->s->swap_reloca_in;
9190 swap_out = bed->s->swap_reloca_out;
9194 dynamic_relocs = rel_dyn;
9195 ext_size = bed->s->sizeof_rel;
9196 swap_in = bed->s->swap_reloc_in;
9197 swap_out = bed->s->swap_reloc_out;
9201 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9202 if (lo->type == bfd_indirect_link_order)
9203 size += lo->u.indirect.section->size;
9205 if (size != dynamic_relocs->size)
9208 sort_elt = (sizeof (struct elf_link_sort_rela)
9209 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9211 count = dynamic_relocs->size / ext_size;
9214 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9218 (*info->callbacks->warning)
9219 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9223 if (bed->s->arch_size == 32)
9224 r_sym_mask = ~(bfd_vma) 0xff;
9226 r_sym_mask = ~(bfd_vma) 0xffffffff;
9228 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9229 if (lo->type == bfd_indirect_link_order)
9231 bfd_byte *erel, *erelend;
9232 asection *o = lo->u.indirect.section;
9234 if (o->contents == NULL && o->size != 0)
9236 /* This is a reloc section that is being handled as a normal
9237 section. See bfd_section_from_shdr. We can't combine
9238 relocs in this case. */
9243 erelend = o->contents + o->size;
9244 p = sort + o->output_offset * opb / ext_size * sort_elt;
9246 while (erel < erelend)
9248 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9250 (*swap_in) (abfd, erel, s->rela);
9251 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9252 s->u.sym_mask = r_sym_mask;
9258 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9260 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9262 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9263 if (s->type != reloc_class_relative)
9269 sq = (struct elf_link_sort_rela *) s_non_relative;
9270 for (; i < count; i++, p += sort_elt)
9272 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9273 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9275 sp->u.offset = sq->rela->r_offset;
9278 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9280 struct elf_link_hash_table *htab = elf_hash_table (info);
9281 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9283 /* We have plt relocs in .rela.dyn. */
9284 sq = (struct elf_link_sort_rela *) sort;
9285 for (i = 0; i < count; i++)
9286 if (sq[count - i - 1].type != reloc_class_plt)
9288 if (i != 0 && htab->srelplt->size == i * ext_size)
9290 struct bfd_link_order **plo;
9291 /* Put srelplt link_order last. This is so the output_offset
9292 set in the next loop is correct for DT_JMPREL. */
9293 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9294 if ((*plo)->type == bfd_indirect_link_order
9295 && (*plo)->u.indirect.section == htab->srelplt)
9301 plo = &(*plo)->next;
9304 dynamic_relocs->map_tail.link_order = lo;
9309 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9310 if (lo->type == bfd_indirect_link_order)
9312 bfd_byte *erel, *erelend;
9313 asection *o = lo->u.indirect.section;
9316 erelend = o->contents + o->size;
9317 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9318 while (erel < erelend)
9320 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9321 (*swap_out) (abfd, s->rela, erel);
9328 *psec = dynamic_relocs;
9332 /* Add a symbol to the output symbol string table. */
9335 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9337 Elf_Internal_Sym *elfsym,
9338 asection *input_sec,
9339 struct elf_link_hash_entry *h)
9341 int (*output_symbol_hook)
9342 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9343 struct elf_link_hash_entry *);
9344 struct elf_link_hash_table *hash_table;
9345 const struct elf_backend_data *bed;
9346 bfd_size_type strtabsize;
9348 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9350 bed = get_elf_backend_data (flinfo->output_bfd);
9351 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9352 if (output_symbol_hook != NULL)
9354 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9361 || (input_sec->flags & SEC_EXCLUDE))
9362 elfsym->st_name = (unsigned long) -1;
9365 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9366 to get the final offset for st_name. */
9368 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9370 if (elfsym->st_name == (unsigned long) -1)
9374 hash_table = elf_hash_table (flinfo->info);
9375 strtabsize = hash_table->strtabsize;
9376 if (strtabsize <= hash_table->strtabcount)
9378 strtabsize += strtabsize;
9379 hash_table->strtabsize = strtabsize;
9380 strtabsize *= sizeof (*hash_table->strtab);
9382 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9384 if (hash_table->strtab == NULL)
9387 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9388 hash_table->strtab[hash_table->strtabcount].dest_index
9389 = hash_table->strtabcount;
9390 hash_table->strtab[hash_table->strtabcount].destshndx_index
9391 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9393 bfd_get_symcount (flinfo->output_bfd) += 1;
9394 hash_table->strtabcount += 1;
9399 /* Swap symbols out to the symbol table and flush the output symbols to
9403 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9405 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9408 const struct elf_backend_data *bed;
9410 Elf_Internal_Shdr *hdr;
9414 if (!hash_table->strtabcount)
9417 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9419 bed = get_elf_backend_data (flinfo->output_bfd);
9421 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9422 symbuf = (bfd_byte *) bfd_malloc (amt);
9426 if (flinfo->symshndxbuf)
9428 amt = sizeof (Elf_External_Sym_Shndx);
9429 amt *= bfd_get_symcount (flinfo->output_bfd);
9430 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9431 if (flinfo->symshndxbuf == NULL)
9438 for (i = 0; i < hash_table->strtabcount; i++)
9440 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9441 if (elfsym->sym.st_name == (unsigned long) -1)
9442 elfsym->sym.st_name = 0;
9445 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9446 elfsym->sym.st_name);
9447 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9448 ((bfd_byte *) symbuf
9449 + (elfsym->dest_index
9450 * bed->s->sizeof_sym)),
9451 (flinfo->symshndxbuf
9452 + elfsym->destshndx_index));
9455 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9456 pos = hdr->sh_offset + hdr->sh_size;
9457 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9458 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9459 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9461 hdr->sh_size += amt;
9469 free (hash_table->strtab);
9470 hash_table->strtab = NULL;
9475 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9478 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9480 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9481 && sym->st_shndx < SHN_LORESERVE)
9483 /* The gABI doesn't support dynamic symbols in output sections
9486 /* xgettext:c-format */
9487 (_("%pB: too many sections: %d (>= %d)"),
9488 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9489 bfd_set_error (bfd_error_nonrepresentable_section);
9495 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9496 allowing an unsatisfied unversioned symbol in the DSO to match a
9497 versioned symbol that would normally require an explicit version.
9498 We also handle the case that a DSO references a hidden symbol
9499 which may be satisfied by a versioned symbol in another DSO. */
9502 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9503 const struct elf_backend_data *bed,
9504 struct elf_link_hash_entry *h)
9507 struct elf_link_loaded_list *loaded;
9509 if (!is_elf_hash_table (info->hash))
9512 /* Check indirect symbol. */
9513 while (h->root.type == bfd_link_hash_indirect)
9514 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9516 switch (h->root.type)
9522 case bfd_link_hash_undefined:
9523 case bfd_link_hash_undefweak:
9524 abfd = h->root.u.undef.abfd;
9526 || (abfd->flags & DYNAMIC) == 0
9527 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9531 case bfd_link_hash_defined:
9532 case bfd_link_hash_defweak:
9533 abfd = h->root.u.def.section->owner;
9536 case bfd_link_hash_common:
9537 abfd = h->root.u.c.p->section->owner;
9540 BFD_ASSERT (abfd != NULL);
9542 for (loaded = elf_hash_table (info)->loaded;
9544 loaded = loaded->next)
9547 Elf_Internal_Shdr *hdr;
9551 Elf_Internal_Shdr *versymhdr;
9552 Elf_Internal_Sym *isym;
9553 Elf_Internal_Sym *isymend;
9554 Elf_Internal_Sym *isymbuf;
9555 Elf_External_Versym *ever;
9556 Elf_External_Versym *extversym;
9558 input = loaded->abfd;
9560 /* We check each DSO for a possible hidden versioned definition. */
9562 || (input->flags & DYNAMIC) == 0
9563 || elf_dynversym (input) == 0)
9566 hdr = &elf_tdata (input)->dynsymtab_hdr;
9568 symcount = hdr->sh_size / bed->s->sizeof_sym;
9569 if (elf_bad_symtab (input))
9571 extsymcount = symcount;
9576 extsymcount = symcount - hdr->sh_info;
9577 extsymoff = hdr->sh_info;
9580 if (extsymcount == 0)
9583 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9585 if (isymbuf == NULL)
9588 /* Read in any version definitions. */
9589 versymhdr = &elf_tdata (input)->dynversym_hdr;
9590 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9591 if (extversym == NULL)
9594 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9595 || (bfd_bread (extversym, versymhdr->sh_size, input)
9596 != versymhdr->sh_size))
9604 ever = extversym + extsymoff;
9605 isymend = isymbuf + extsymcount;
9606 for (isym = isymbuf; isym < isymend; isym++, ever++)
9609 Elf_Internal_Versym iver;
9610 unsigned short version_index;
9612 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9613 || isym->st_shndx == SHN_UNDEF)
9616 name = bfd_elf_string_from_elf_section (input,
9619 if (strcmp (name, h->root.root.string) != 0)
9622 _bfd_elf_swap_versym_in (input, ever, &iver);
9624 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9626 && h->forced_local))
9628 /* If we have a non-hidden versioned sym, then it should
9629 have provided a definition for the undefined sym unless
9630 it is defined in a non-shared object and forced local.
9635 version_index = iver.vs_vers & VERSYM_VERSION;
9636 if (version_index == 1 || version_index == 2)
9638 /* This is the base or first version. We can use it. */
9652 /* Convert ELF common symbol TYPE. */
9655 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9657 /* Commom symbol can only appear in relocatable link. */
9658 if (!bfd_link_relocatable (info))
9660 switch (info->elf_stt_common)
9664 case elf_stt_common:
9667 case no_elf_stt_common:
9674 /* Add an external symbol to the symbol table. This is called from
9675 the hash table traversal routine. When generating a shared object,
9676 we go through the symbol table twice. The first time we output
9677 anything that might have been forced to local scope in a version
9678 script. The second time we output the symbols that are still
9682 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9684 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9685 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9686 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9688 Elf_Internal_Sym sym;
9689 asection *input_sec;
9690 const struct elf_backend_data *bed;
9695 if (h->root.type == bfd_link_hash_warning)
9697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9698 if (h->root.type == bfd_link_hash_new)
9702 /* Decide whether to output this symbol in this pass. */
9703 if (eoinfo->localsyms)
9705 if (!h->forced_local)
9710 if (h->forced_local)
9714 bed = get_elf_backend_data (flinfo->output_bfd);
9716 if (h->root.type == bfd_link_hash_undefined)
9718 /* If we have an undefined symbol reference here then it must have
9719 come from a shared library that is being linked in. (Undefined
9720 references in regular files have already been handled unless
9721 they are in unreferenced sections which are removed by garbage
9723 bfd_boolean ignore_undef = FALSE;
9725 /* Some symbols may be special in that the fact that they're
9726 undefined can be safely ignored - let backend determine that. */
9727 if (bed->elf_backend_ignore_undef_symbol)
9728 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9730 /* If we are reporting errors for this situation then do so now. */
9733 && (!h->ref_regular || flinfo->info->gc_sections)
9734 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9735 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9736 (*flinfo->info->callbacks->undefined_symbol)
9737 (flinfo->info, h->root.root.string,
9738 h->ref_regular ? NULL : h->root.u.undef.abfd,
9740 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9742 /* Strip a global symbol defined in a discarded section. */
9747 /* We should also warn if a forced local symbol is referenced from
9748 shared libraries. */
9749 if (bfd_link_executable (flinfo->info)
9754 && h->ref_dynamic_nonweak
9755 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9759 struct elf_link_hash_entry *hi = h;
9761 /* Check indirect symbol. */
9762 while (hi->root.type == bfd_link_hash_indirect)
9763 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9765 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9766 /* xgettext:c-format */
9767 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9768 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9769 /* xgettext:c-format */
9770 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9772 /* xgettext:c-format */
9773 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9774 def_bfd = flinfo->output_bfd;
9775 if (hi->root.u.def.section != bfd_abs_section_ptr)
9776 def_bfd = hi->root.u.def.section->owner;
9777 _bfd_error_handler (msg, flinfo->output_bfd,
9778 h->root.root.string, def_bfd);
9779 bfd_set_error (bfd_error_bad_value);
9780 eoinfo->failed = TRUE;
9784 /* We don't want to output symbols that have never been mentioned by
9785 a regular file, or that we have been told to strip. However, if
9786 h->indx is set to -2, the symbol is used by a reloc and we must
9791 else if ((h->def_dynamic
9793 || h->root.type == bfd_link_hash_new)
9797 else if (flinfo->info->strip == strip_all)
9799 else if (flinfo->info->strip == strip_some
9800 && bfd_hash_lookup (flinfo->info->keep_hash,
9801 h->root.root.string, FALSE, FALSE) == NULL)
9803 else if ((h->root.type == bfd_link_hash_defined
9804 || h->root.type == bfd_link_hash_defweak)
9805 && ((flinfo->info->strip_discarded
9806 && discarded_section (h->root.u.def.section))
9807 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9808 && h->root.u.def.section->owner != NULL
9809 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9811 else if ((h->root.type == bfd_link_hash_undefined
9812 || h->root.type == bfd_link_hash_undefweak)
9813 && h->root.u.undef.abfd != NULL
9814 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9819 /* If we're stripping it, and it's not a dynamic symbol, there's
9820 nothing else to do. However, if it is a forced local symbol or
9821 an ifunc symbol we need to give the backend finish_dynamic_symbol
9822 function a chance to make it dynamic. */
9825 && type != STT_GNU_IFUNC
9826 && !h->forced_local)
9830 sym.st_size = h->size;
9831 sym.st_other = h->other;
9832 switch (h->root.type)
9835 case bfd_link_hash_new:
9836 case bfd_link_hash_warning:
9840 case bfd_link_hash_undefined:
9841 case bfd_link_hash_undefweak:
9842 input_sec = bfd_und_section_ptr;
9843 sym.st_shndx = SHN_UNDEF;
9846 case bfd_link_hash_defined:
9847 case bfd_link_hash_defweak:
9849 input_sec = h->root.u.def.section;
9850 if (input_sec->output_section != NULL)
9853 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9854 input_sec->output_section);
9855 if (sym.st_shndx == SHN_BAD)
9858 /* xgettext:c-format */
9859 (_("%pB: could not find output section %pA for input section %pA"),
9860 flinfo->output_bfd, input_sec->output_section, input_sec);
9861 bfd_set_error (bfd_error_nonrepresentable_section);
9862 eoinfo->failed = TRUE;
9866 /* ELF symbols in relocatable files are section relative,
9867 but in nonrelocatable files they are virtual
9869 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9870 if (!bfd_link_relocatable (flinfo->info))
9872 sym.st_value += input_sec->output_section->vma;
9873 if (h->type == STT_TLS)
9875 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9876 if (tls_sec != NULL)
9877 sym.st_value -= tls_sec->vma;
9883 BFD_ASSERT (input_sec->owner == NULL
9884 || (input_sec->owner->flags & DYNAMIC) != 0);
9885 sym.st_shndx = SHN_UNDEF;
9886 input_sec = bfd_und_section_ptr;
9891 case bfd_link_hash_common:
9892 input_sec = h->root.u.c.p->section;
9893 sym.st_shndx = bed->common_section_index (input_sec);
9894 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9897 case bfd_link_hash_indirect:
9898 /* These symbols are created by symbol versioning. They point
9899 to the decorated version of the name. For example, if the
9900 symbol foo@@GNU_1.2 is the default, which should be used when
9901 foo is used with no version, then we add an indirect symbol
9902 foo which points to foo@@GNU_1.2. We ignore these symbols,
9903 since the indirected symbol is already in the hash table. */
9907 if (type == STT_COMMON || type == STT_OBJECT)
9908 switch (h->root.type)
9910 case bfd_link_hash_common:
9911 type = elf_link_convert_common_type (flinfo->info, type);
9913 case bfd_link_hash_defined:
9914 case bfd_link_hash_defweak:
9915 if (bed->common_definition (&sym))
9916 type = elf_link_convert_common_type (flinfo->info, type);
9920 case bfd_link_hash_undefined:
9921 case bfd_link_hash_undefweak:
9927 if (h->forced_local)
9929 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9930 /* Turn off visibility on local symbol. */
9931 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9933 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9934 else if (h->unique_global && h->def_regular)
9935 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9936 else if (h->root.type == bfd_link_hash_undefweak
9937 || h->root.type == bfd_link_hash_defweak)
9938 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9940 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9941 sym.st_target_internal = h->target_internal;
9943 /* Give the processor backend a chance to tweak the symbol value,
9944 and also to finish up anything that needs to be done for this
9945 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9946 forced local syms when non-shared is due to a historical quirk.
9947 STT_GNU_IFUNC symbol must go through PLT. */
9948 if ((h->type == STT_GNU_IFUNC
9950 && !bfd_link_relocatable (flinfo->info))
9951 || ((h->dynindx != -1
9953 && ((bfd_link_pic (flinfo->info)
9954 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9955 || h->root.type != bfd_link_hash_undefweak))
9956 || !h->forced_local)
9957 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9959 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9960 (flinfo->output_bfd, flinfo->info, h, &sym)))
9962 eoinfo->failed = TRUE;
9967 /* If we are marking the symbol as undefined, and there are no
9968 non-weak references to this symbol from a regular object, then
9969 mark the symbol as weak undefined; if there are non-weak
9970 references, mark the symbol as strong. We can't do this earlier,
9971 because it might not be marked as undefined until the
9972 finish_dynamic_symbol routine gets through with it. */
9973 if (sym.st_shndx == SHN_UNDEF
9975 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9976 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9979 type = ELF_ST_TYPE (sym.st_info);
9981 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9982 if (type == STT_GNU_IFUNC)
9985 if (h->ref_regular_nonweak)
9986 bindtype = STB_GLOBAL;
9988 bindtype = STB_WEAK;
9989 sym.st_info = ELF_ST_INFO (bindtype, type);
9992 /* If this is a symbol defined in a dynamic library, don't use the
9993 symbol size from the dynamic library. Relinking an executable
9994 against a new library may introduce gratuitous changes in the
9995 executable's symbols if we keep the size. */
9996 if (sym.st_shndx == SHN_UNDEF
10001 /* If a non-weak symbol with non-default visibility is not defined
10002 locally, it is a fatal error. */
10003 if (!bfd_link_relocatable (flinfo->info)
10004 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10005 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10006 && h->root.type == bfd_link_hash_undefined
10007 && !h->def_regular)
10011 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10012 /* xgettext:c-format */
10013 msg = _("%pB: protected symbol `%s' isn't defined");
10014 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10015 /* xgettext:c-format */
10016 msg = _("%pB: internal symbol `%s' isn't defined");
10018 /* xgettext:c-format */
10019 msg = _("%pB: hidden symbol `%s' isn't defined");
10020 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10021 bfd_set_error (bfd_error_bad_value);
10022 eoinfo->failed = TRUE;
10026 /* If this symbol should be put in the .dynsym section, then put it
10027 there now. We already know the symbol index. We also fill in
10028 the entry in the .hash section. */
10029 if (elf_hash_table (flinfo->info)->dynsym != NULL
10030 && h->dynindx != -1
10031 && elf_hash_table (flinfo->info)->dynamic_sections_created)
10035 /* Since there is no version information in the dynamic string,
10036 if there is no version info in symbol version section, we will
10037 have a run-time problem if not linking executable, referenced
10038 by shared library, or not bound locally. */
10039 if (h->verinfo.verdef == NULL
10040 && (!bfd_link_executable (flinfo->info)
10042 || !h->def_regular))
10044 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10046 if (p && p [1] != '\0')
10049 /* xgettext:c-format */
10050 (_("%pB: no symbol version section for versioned symbol `%s'"),
10051 flinfo->output_bfd, h->root.root.string);
10052 eoinfo->failed = TRUE;
10057 sym.st_name = h->dynstr_index;
10058 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10059 + h->dynindx * bed->s->sizeof_sym);
10060 if (!check_dynsym (flinfo->output_bfd, &sym))
10062 eoinfo->failed = TRUE;
10065 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10067 if (flinfo->hash_sec != NULL)
10069 size_t hash_entry_size;
10070 bfd_byte *bucketpos;
10072 size_t bucketcount;
10075 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10076 bucket = h->u.elf_hash_value % bucketcount;
10079 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10080 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10081 + (bucket + 2) * hash_entry_size);
10082 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10083 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10085 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10086 ((bfd_byte *) flinfo->hash_sec->contents
10087 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10090 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10092 Elf_Internal_Versym iversym;
10093 Elf_External_Versym *eversym;
10095 if (!h->def_regular)
10097 if (h->verinfo.verdef == NULL
10098 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10099 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10100 iversym.vs_vers = 0;
10102 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10106 if (h->verinfo.vertree == NULL)
10107 iversym.vs_vers = 1;
10109 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10110 if (flinfo->info->create_default_symver)
10114 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10115 defined locally. */
10116 if (h->versioned == versioned_hidden && h->def_regular)
10117 iversym.vs_vers |= VERSYM_HIDDEN;
10119 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10120 eversym += h->dynindx;
10121 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10125 /* If the symbol is undefined, and we didn't output it to .dynsym,
10126 strip it from .symtab too. Obviously we can't do this for
10127 relocatable output or when needed for --emit-relocs. */
10128 else if (input_sec == bfd_und_section_ptr
10130 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10131 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10132 && !bfd_link_relocatable (flinfo->info))
10135 /* Also strip others that we couldn't earlier due to dynamic symbol
10139 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10142 /* Output a FILE symbol so that following locals are not associated
10143 with the wrong input file. We need one for forced local symbols
10144 if we've seen more than one FILE symbol or when we have exactly
10145 one FILE symbol but global symbols are present in a file other
10146 than the one with the FILE symbol. We also need one if linker
10147 defined symbols are present. In practice these conditions are
10148 always met, so just emit the FILE symbol unconditionally. */
10149 if (eoinfo->localsyms
10150 && !eoinfo->file_sym_done
10151 && eoinfo->flinfo->filesym_count != 0)
10153 Elf_Internal_Sym fsym;
10155 memset (&fsym, 0, sizeof (fsym));
10156 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10157 fsym.st_shndx = SHN_ABS;
10158 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10159 bfd_und_section_ptr, NULL))
10162 eoinfo->file_sym_done = TRUE;
10165 indx = bfd_get_symcount (flinfo->output_bfd);
10166 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10170 eoinfo->failed = TRUE;
10175 else if (h->indx == -2)
10181 /* Return TRUE if special handling is done for relocs in SEC against
10182 symbols defined in discarded sections. */
10185 elf_section_ignore_discarded_relocs (asection *sec)
10187 const struct elf_backend_data *bed;
10189 switch (sec->sec_info_type)
10191 case SEC_INFO_TYPE_STABS:
10192 case SEC_INFO_TYPE_EH_FRAME:
10193 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10199 bed = get_elf_backend_data (sec->owner);
10200 if (bed->elf_backend_ignore_discarded_relocs != NULL
10201 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10207 /* Return a mask saying how ld should treat relocations in SEC against
10208 symbols defined in discarded sections. If this function returns
10209 COMPLAIN set, ld will issue a warning message. If this function
10210 returns PRETEND set, and the discarded section was link-once and the
10211 same size as the kept link-once section, ld will pretend that the
10212 symbol was actually defined in the kept section. Otherwise ld will
10213 zero the reloc (at least that is the intent, but some cooperation by
10214 the target dependent code is needed, particularly for REL targets). */
10217 _bfd_elf_default_action_discarded (asection *sec)
10219 if (sec->flags & SEC_DEBUGGING)
10222 if (strcmp (".eh_frame", sec->name) == 0)
10225 if (strcmp (".gcc_except_table", sec->name) == 0)
10228 return COMPLAIN | PRETEND;
10231 /* Find a match between a section and a member of a section group. */
10234 match_group_member (asection *sec, asection *group,
10235 struct bfd_link_info *info)
10237 asection *first = elf_next_in_group (group);
10238 asection *s = first;
10242 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10245 s = elf_next_in_group (s);
10253 /* Check if the kept section of a discarded section SEC can be used
10254 to replace it. Return the replacement if it is OK. Otherwise return
10258 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10262 kept = sec->kept_section;
10265 if ((kept->flags & SEC_GROUP) != 0)
10266 kept = match_group_member (sec, kept, info);
10268 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10269 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10271 sec->kept_section = kept;
10276 /* Link an input file into the linker output file. This function
10277 handles all the sections and relocations of the input file at once.
10278 This is so that we only have to read the local symbols once, and
10279 don't have to keep them in memory. */
10282 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10284 int (*relocate_section)
10285 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10286 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10288 Elf_Internal_Shdr *symtab_hdr;
10289 size_t locsymcount;
10291 Elf_Internal_Sym *isymbuf;
10292 Elf_Internal_Sym *isym;
10293 Elf_Internal_Sym *isymend;
10295 asection **ppsection;
10297 const struct elf_backend_data *bed;
10298 struct elf_link_hash_entry **sym_hashes;
10299 bfd_size_type address_size;
10300 bfd_vma r_type_mask;
10302 bfd_boolean have_file_sym = FALSE;
10304 output_bfd = flinfo->output_bfd;
10305 bed = get_elf_backend_data (output_bfd);
10306 relocate_section = bed->elf_backend_relocate_section;
10308 /* If this is a dynamic object, we don't want to do anything here:
10309 we don't want the local symbols, and we don't want the section
10311 if ((input_bfd->flags & DYNAMIC) != 0)
10314 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10315 if (elf_bad_symtab (input_bfd))
10317 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10322 locsymcount = symtab_hdr->sh_info;
10323 extsymoff = symtab_hdr->sh_info;
10326 /* Read the local symbols. */
10327 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10328 if (isymbuf == NULL && locsymcount != 0)
10330 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10331 flinfo->internal_syms,
10332 flinfo->external_syms,
10333 flinfo->locsym_shndx);
10334 if (isymbuf == NULL)
10338 /* Find local symbol sections and adjust values of symbols in
10339 SEC_MERGE sections. Write out those local symbols we know are
10340 going into the output file. */
10341 isymend = isymbuf + locsymcount;
10342 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10344 isym++, pindex++, ppsection++)
10348 Elf_Internal_Sym osym;
10354 if (elf_bad_symtab (input_bfd))
10356 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10363 if (isym->st_shndx == SHN_UNDEF)
10364 isec = bfd_und_section_ptr;
10365 else if (isym->st_shndx == SHN_ABS)
10366 isec = bfd_abs_section_ptr;
10367 else if (isym->st_shndx == SHN_COMMON)
10368 isec = bfd_com_section_ptr;
10371 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10374 /* Don't attempt to output symbols with st_shnx in the
10375 reserved range other than SHN_ABS and SHN_COMMON. */
10379 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10380 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10382 _bfd_merged_section_offset (output_bfd, &isec,
10383 elf_section_data (isec)->sec_info,
10389 /* Don't output the first, undefined, symbol. In fact, don't
10390 output any undefined local symbol. */
10391 if (isec == bfd_und_section_ptr)
10394 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10396 /* We never output section symbols. Instead, we use the
10397 section symbol of the corresponding section in the output
10402 /* If we are stripping all symbols, we don't want to output this
10404 if (flinfo->info->strip == strip_all)
10407 /* If we are discarding all local symbols, we don't want to
10408 output this one. If we are generating a relocatable output
10409 file, then some of the local symbols may be required by
10410 relocs; we output them below as we discover that they are
10412 if (flinfo->info->discard == discard_all)
10415 /* If this symbol is defined in a section which we are
10416 discarding, we don't need to keep it. */
10417 if (isym->st_shndx != SHN_UNDEF
10418 && isym->st_shndx < SHN_LORESERVE
10419 && bfd_section_removed_from_list (output_bfd,
10420 isec->output_section))
10423 /* Get the name of the symbol. */
10424 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10429 /* See if we are discarding symbols with this name. */
10430 if ((flinfo->info->strip == strip_some
10431 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10433 || (((flinfo->info->discard == discard_sec_merge
10434 && (isec->flags & SEC_MERGE)
10435 && !bfd_link_relocatable (flinfo->info))
10436 || flinfo->info->discard == discard_l)
10437 && bfd_is_local_label_name (input_bfd, name)))
10440 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10442 if (input_bfd->lto_output)
10443 /* -flto puts a temp file name here. This means builds
10444 are not reproducible. Discard the symbol. */
10446 have_file_sym = TRUE;
10447 flinfo->filesym_count += 1;
10449 if (!have_file_sym)
10451 /* In the absence of debug info, bfd_find_nearest_line uses
10452 FILE symbols to determine the source file for local
10453 function symbols. Provide a FILE symbol here if input
10454 files lack such, so that their symbols won't be
10455 associated with a previous input file. It's not the
10456 source file, but the best we can do. */
10457 have_file_sym = TRUE;
10458 flinfo->filesym_count += 1;
10459 memset (&osym, 0, sizeof (osym));
10460 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10461 osym.st_shndx = SHN_ABS;
10462 if (!elf_link_output_symstrtab (flinfo,
10463 (input_bfd->lto_output ? NULL
10464 : input_bfd->filename),
10465 &osym, bfd_abs_section_ptr,
10472 /* Adjust the section index for the output file. */
10473 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10474 isec->output_section);
10475 if (osym.st_shndx == SHN_BAD)
10478 /* ELF symbols in relocatable files are section relative, but
10479 in executable files they are virtual addresses. Note that
10480 this code assumes that all ELF sections have an associated
10481 BFD section with a reasonable value for output_offset; below
10482 we assume that they also have a reasonable value for
10483 output_section. Any special sections must be set up to meet
10484 these requirements. */
10485 osym.st_value += isec->output_offset;
10486 if (!bfd_link_relocatable (flinfo->info))
10488 osym.st_value += isec->output_section->vma;
10489 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10491 /* STT_TLS symbols are relative to PT_TLS segment base. */
10492 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10493 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10497 indx = bfd_get_symcount (output_bfd);
10498 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10505 if (bed->s->arch_size == 32)
10507 r_type_mask = 0xff;
10513 r_type_mask = 0xffffffff;
10518 /* Relocate the contents of each section. */
10519 sym_hashes = elf_sym_hashes (input_bfd);
10520 for (o = input_bfd->sections; o != NULL; o = o->next)
10522 bfd_byte *contents;
10524 if (! o->linker_mark)
10526 /* This section was omitted from the link. */
10530 if (!flinfo->info->resolve_section_groups
10531 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10533 /* Deal with the group signature symbol. */
10534 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10535 unsigned long symndx = sec_data->this_hdr.sh_info;
10536 asection *osec = o->output_section;
10538 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10539 if (symndx >= locsymcount
10540 || (elf_bad_symtab (input_bfd)
10541 && flinfo->sections[symndx] == NULL))
10543 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10544 while (h->root.type == bfd_link_hash_indirect
10545 || h->root.type == bfd_link_hash_warning)
10546 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10547 /* Arrange for symbol to be output. */
10549 elf_section_data (osec)->this_hdr.sh_info = -2;
10551 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10553 /* We'll use the output section target_index. */
10554 asection *sec = flinfo->sections[symndx]->output_section;
10555 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10559 if (flinfo->indices[symndx] == -1)
10561 /* Otherwise output the local symbol now. */
10562 Elf_Internal_Sym sym = isymbuf[symndx];
10563 asection *sec = flinfo->sections[symndx]->output_section;
10568 name = bfd_elf_string_from_elf_section (input_bfd,
10569 symtab_hdr->sh_link,
10574 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10576 if (sym.st_shndx == SHN_BAD)
10579 sym.st_value += o->output_offset;
10581 indx = bfd_get_symcount (output_bfd);
10582 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10587 flinfo->indices[symndx] = indx;
10591 elf_section_data (osec)->this_hdr.sh_info
10592 = flinfo->indices[symndx];
10596 if ((o->flags & SEC_HAS_CONTENTS) == 0
10597 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10600 if ((o->flags & SEC_LINKER_CREATED) != 0)
10602 /* Section was created by _bfd_elf_link_create_dynamic_sections
10607 /* Get the contents of the section. They have been cached by a
10608 relaxation routine. Note that o is a section in an input
10609 file, so the contents field will not have been set by any of
10610 the routines which work on output files. */
10611 if (elf_section_data (o)->this_hdr.contents != NULL)
10613 contents = elf_section_data (o)->this_hdr.contents;
10614 if (bed->caches_rawsize
10616 && o->rawsize < o->size)
10618 memcpy (flinfo->contents, contents, o->rawsize);
10619 contents = flinfo->contents;
10624 contents = flinfo->contents;
10625 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10629 if ((o->flags & SEC_RELOC) != 0)
10631 Elf_Internal_Rela *internal_relocs;
10632 Elf_Internal_Rela *rel, *relend;
10633 int action_discarded;
10636 /* Get the swapped relocs. */
10638 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10639 flinfo->internal_relocs, FALSE);
10640 if (internal_relocs == NULL
10641 && o->reloc_count > 0)
10644 /* We need to reverse-copy input .ctors/.dtors sections if
10645 they are placed in .init_array/.finit_array for output. */
10646 if (o->size > address_size
10647 && ((strncmp (o->name, ".ctors", 6) == 0
10648 && strcmp (o->output_section->name,
10649 ".init_array") == 0)
10650 || (strncmp (o->name, ".dtors", 6) == 0
10651 && strcmp (o->output_section->name,
10652 ".fini_array") == 0))
10653 && (o->name[6] == 0 || o->name[6] == '.'))
10655 if (o->size * bed->s->int_rels_per_ext_rel
10656 != o->reloc_count * address_size)
10659 /* xgettext:c-format */
10660 (_("error: %pB: size of section %pA is not "
10661 "multiple of address size"),
10663 bfd_set_error (bfd_error_bad_value);
10666 o->flags |= SEC_ELF_REVERSE_COPY;
10669 action_discarded = -1;
10670 if (!elf_section_ignore_discarded_relocs (o))
10671 action_discarded = (*bed->action_discarded) (o);
10673 /* Run through the relocs evaluating complex reloc symbols and
10674 looking for relocs against symbols from discarded sections
10675 or section symbols from removed link-once sections.
10676 Complain about relocs against discarded sections. Zero
10677 relocs against removed link-once sections. */
10679 rel = internal_relocs;
10680 relend = rel + o->reloc_count;
10681 for ( ; rel < relend; rel++)
10683 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10684 unsigned int s_type;
10685 asection **ps, *sec;
10686 struct elf_link_hash_entry *h = NULL;
10687 const char *sym_name;
10689 if (r_symndx == STN_UNDEF)
10692 if (r_symndx >= locsymcount
10693 || (elf_bad_symtab (input_bfd)
10694 && flinfo->sections[r_symndx] == NULL))
10696 h = sym_hashes[r_symndx - extsymoff];
10698 /* Badly formatted input files can contain relocs that
10699 reference non-existant symbols. Check here so that
10700 we do not seg fault. */
10704 /* xgettext:c-format */
10705 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10706 "that references a non-existent global symbol"),
10707 input_bfd, (uint64_t) rel->r_info, o);
10708 bfd_set_error (bfd_error_bad_value);
10712 while (h->root.type == bfd_link_hash_indirect
10713 || h->root.type == bfd_link_hash_warning)
10714 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10718 /* If a plugin symbol is referenced from a non-IR file,
10719 mark the symbol as undefined. Note that the
10720 linker may attach linker created dynamic sections
10721 to the plugin bfd. Symbols defined in linker
10722 created sections are not plugin symbols. */
10723 if ((h->root.non_ir_ref_regular
10724 || h->root.non_ir_ref_dynamic)
10725 && (h->root.type == bfd_link_hash_defined
10726 || h->root.type == bfd_link_hash_defweak)
10727 && (h->root.u.def.section->flags
10728 & SEC_LINKER_CREATED) == 0
10729 && h->root.u.def.section->owner != NULL
10730 && (h->root.u.def.section->owner->flags
10731 & BFD_PLUGIN) != 0)
10733 h->root.type = bfd_link_hash_undefined;
10734 h->root.u.undef.abfd = h->root.u.def.section->owner;
10738 if (h->root.type == bfd_link_hash_defined
10739 || h->root.type == bfd_link_hash_defweak)
10740 ps = &h->root.u.def.section;
10742 sym_name = h->root.root.string;
10746 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10748 s_type = ELF_ST_TYPE (sym->st_info);
10749 ps = &flinfo->sections[r_symndx];
10750 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10754 if ((s_type == STT_RELC || s_type == STT_SRELC)
10755 && !bfd_link_relocatable (flinfo->info))
10758 bfd_vma dot = (rel->r_offset
10759 + o->output_offset + o->output_section->vma);
10761 printf ("Encountered a complex symbol!");
10762 printf (" (input_bfd %s, section %s, reloc %ld\n",
10763 input_bfd->filename, o->name,
10764 (long) (rel - internal_relocs));
10765 printf (" symbol: idx %8.8lx, name %s\n",
10766 r_symndx, sym_name);
10767 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10768 (unsigned long) rel->r_info,
10769 (unsigned long) rel->r_offset);
10771 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10772 isymbuf, locsymcount, s_type == STT_SRELC))
10775 /* Symbol evaluated OK. Update to absolute value. */
10776 set_symbol_value (input_bfd, isymbuf, locsymcount,
10781 if (action_discarded != -1 && ps != NULL)
10783 /* Complain if the definition comes from a
10784 discarded section. */
10785 if ((sec = *ps) != NULL && discarded_section (sec))
10787 BFD_ASSERT (r_symndx != STN_UNDEF);
10788 if (action_discarded & COMPLAIN)
10789 (*flinfo->info->callbacks->einfo)
10790 /* xgettext:c-format */
10791 (_("%X`%s' referenced in section `%pA' of %pB: "
10792 "defined in discarded section `%pA' of %pB\n"),
10793 sym_name, o, input_bfd, sec, sec->owner);
10795 /* Try to do the best we can to support buggy old
10796 versions of gcc. Pretend that the symbol is
10797 really defined in the kept linkonce section.
10798 FIXME: This is quite broken. Modifying the
10799 symbol here means we will be changing all later
10800 uses of the symbol, not just in this section. */
10801 if (action_discarded & PRETEND)
10805 kept = _bfd_elf_check_kept_section (sec,
10817 /* Relocate the section by invoking a back end routine.
10819 The back end routine is responsible for adjusting the
10820 section contents as necessary, and (if using Rela relocs
10821 and generating a relocatable output file) adjusting the
10822 reloc addend as necessary.
10824 The back end routine does not have to worry about setting
10825 the reloc address or the reloc symbol index.
10827 The back end routine is given a pointer to the swapped in
10828 internal symbols, and can access the hash table entries
10829 for the external symbols via elf_sym_hashes (input_bfd).
10831 When generating relocatable output, the back end routine
10832 must handle STB_LOCAL/STT_SECTION symbols specially. The
10833 output symbol is going to be a section symbol
10834 corresponding to the output section, which will require
10835 the addend to be adjusted. */
10837 ret = (*relocate_section) (output_bfd, flinfo->info,
10838 input_bfd, o, contents,
10846 || bfd_link_relocatable (flinfo->info)
10847 || flinfo->info->emitrelocations)
10849 Elf_Internal_Rela *irela;
10850 Elf_Internal_Rela *irelaend, *irelamid;
10851 bfd_vma last_offset;
10852 struct elf_link_hash_entry **rel_hash;
10853 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10854 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10855 unsigned int next_erel;
10856 bfd_boolean rela_normal;
10857 struct bfd_elf_section_data *esdi, *esdo;
10859 esdi = elf_section_data (o);
10860 esdo = elf_section_data (o->output_section);
10861 rela_normal = FALSE;
10863 /* Adjust the reloc addresses and symbol indices. */
10865 irela = internal_relocs;
10866 irelaend = irela + o->reloc_count;
10867 rel_hash = esdo->rel.hashes + esdo->rel.count;
10868 /* We start processing the REL relocs, if any. When we reach
10869 IRELAMID in the loop, we switch to the RELA relocs. */
10871 if (esdi->rel.hdr != NULL)
10872 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10873 * bed->s->int_rels_per_ext_rel);
10874 rel_hash_list = rel_hash;
10875 rela_hash_list = NULL;
10876 last_offset = o->output_offset;
10877 if (!bfd_link_relocatable (flinfo->info))
10878 last_offset += o->output_section->vma;
10879 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10881 unsigned long r_symndx;
10883 Elf_Internal_Sym sym;
10885 if (next_erel == bed->s->int_rels_per_ext_rel)
10891 if (irela == irelamid)
10893 rel_hash = esdo->rela.hashes + esdo->rela.count;
10894 rela_hash_list = rel_hash;
10895 rela_normal = bed->rela_normal;
10898 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10901 if (irela->r_offset >= (bfd_vma) -2)
10903 /* This is a reloc for a deleted entry or somesuch.
10904 Turn it into an R_*_NONE reloc, at the same
10905 offset as the last reloc. elf_eh_frame.c and
10906 bfd_elf_discard_info rely on reloc offsets
10908 irela->r_offset = last_offset;
10910 irela->r_addend = 0;
10914 irela->r_offset += o->output_offset;
10916 /* Relocs in an executable have to be virtual addresses. */
10917 if (!bfd_link_relocatable (flinfo->info))
10918 irela->r_offset += o->output_section->vma;
10920 last_offset = irela->r_offset;
10922 r_symndx = irela->r_info >> r_sym_shift;
10923 if (r_symndx == STN_UNDEF)
10926 if (r_symndx >= locsymcount
10927 || (elf_bad_symtab (input_bfd)
10928 && flinfo->sections[r_symndx] == NULL))
10930 struct elf_link_hash_entry *rh;
10931 unsigned long indx;
10933 /* This is a reloc against a global symbol. We
10934 have not yet output all the local symbols, so
10935 we do not know the symbol index of any global
10936 symbol. We set the rel_hash entry for this
10937 reloc to point to the global hash table entry
10938 for this symbol. The symbol index is then
10939 set at the end of bfd_elf_final_link. */
10940 indx = r_symndx - extsymoff;
10941 rh = elf_sym_hashes (input_bfd)[indx];
10942 while (rh->root.type == bfd_link_hash_indirect
10943 || rh->root.type == bfd_link_hash_warning)
10944 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10946 /* Setting the index to -2 tells
10947 elf_link_output_extsym that this symbol is
10948 used by a reloc. */
10949 BFD_ASSERT (rh->indx < 0);
10956 /* This is a reloc against a local symbol. */
10959 sym = isymbuf[r_symndx];
10960 sec = flinfo->sections[r_symndx];
10961 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10963 /* I suppose the backend ought to fill in the
10964 section of any STT_SECTION symbol against a
10965 processor specific section. */
10966 r_symndx = STN_UNDEF;
10967 if (bfd_is_abs_section (sec))
10969 else if (sec == NULL || sec->owner == NULL)
10971 bfd_set_error (bfd_error_bad_value);
10976 asection *osec = sec->output_section;
10978 /* If we have discarded a section, the output
10979 section will be the absolute section. In
10980 case of discarded SEC_MERGE sections, use
10981 the kept section. relocate_section should
10982 have already handled discarded linkonce
10984 if (bfd_is_abs_section (osec)
10985 && sec->kept_section != NULL
10986 && sec->kept_section->output_section != NULL)
10988 osec = sec->kept_section->output_section;
10989 irela->r_addend -= osec->vma;
10992 if (!bfd_is_abs_section (osec))
10994 r_symndx = osec->target_index;
10995 if (r_symndx == STN_UNDEF)
10997 irela->r_addend += osec->vma;
10998 osec = _bfd_nearby_section (output_bfd, osec,
11000 irela->r_addend -= osec->vma;
11001 r_symndx = osec->target_index;
11006 /* Adjust the addend according to where the
11007 section winds up in the output section. */
11009 irela->r_addend += sec->output_offset;
11013 if (flinfo->indices[r_symndx] == -1)
11015 unsigned long shlink;
11020 if (flinfo->info->strip == strip_all)
11022 /* You can't do ld -r -s. */
11023 bfd_set_error (bfd_error_invalid_operation);
11027 /* This symbol was skipped earlier, but
11028 since it is needed by a reloc, we
11029 must output it now. */
11030 shlink = symtab_hdr->sh_link;
11031 name = (bfd_elf_string_from_elf_section
11032 (input_bfd, shlink, sym.st_name));
11036 osec = sec->output_section;
11038 _bfd_elf_section_from_bfd_section (output_bfd,
11040 if (sym.st_shndx == SHN_BAD)
11043 sym.st_value += sec->output_offset;
11044 if (!bfd_link_relocatable (flinfo->info))
11046 sym.st_value += osec->vma;
11047 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11049 /* STT_TLS symbols are relative to PT_TLS
11051 BFD_ASSERT (elf_hash_table (flinfo->info)
11052 ->tls_sec != NULL);
11053 sym.st_value -= (elf_hash_table (flinfo->info)
11058 indx = bfd_get_symcount (output_bfd);
11059 ret = elf_link_output_symstrtab (flinfo, name,
11065 flinfo->indices[r_symndx] = indx;
11070 r_symndx = flinfo->indices[r_symndx];
11073 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11074 | (irela->r_info & r_type_mask));
11077 /* Swap out the relocs. */
11078 input_rel_hdr = esdi->rel.hdr;
11079 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11081 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11086 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11087 * bed->s->int_rels_per_ext_rel);
11088 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11091 input_rela_hdr = esdi->rela.hdr;
11092 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11094 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11103 /* Write out the modified section contents. */
11104 if (bed->elf_backend_write_section
11105 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11108 /* Section written out. */
11110 else switch (o->sec_info_type)
11112 case SEC_INFO_TYPE_STABS:
11113 if (! (_bfd_write_section_stabs
11115 &elf_hash_table (flinfo->info)->stab_info,
11116 o, &elf_section_data (o)->sec_info, contents)))
11119 case SEC_INFO_TYPE_MERGE:
11120 if (! _bfd_write_merged_section (output_bfd, o,
11121 elf_section_data (o)->sec_info))
11124 case SEC_INFO_TYPE_EH_FRAME:
11126 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11131 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11133 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11141 if (! (o->flags & SEC_EXCLUDE))
11143 file_ptr offset = (file_ptr) o->output_offset;
11144 bfd_size_type todo = o->size;
11146 offset *= bfd_octets_per_byte (output_bfd);
11148 if ((o->flags & SEC_ELF_REVERSE_COPY))
11150 /* Reverse-copy input section to output. */
11153 todo -= address_size;
11154 if (! bfd_set_section_contents (output_bfd,
11162 offset += address_size;
11166 else if (! bfd_set_section_contents (output_bfd,
11180 /* Generate a reloc when linking an ELF file. This is a reloc
11181 requested by the linker, and does not come from any input file. This
11182 is used to build constructor and destructor tables when linking
11186 elf_reloc_link_order (bfd *output_bfd,
11187 struct bfd_link_info *info,
11188 asection *output_section,
11189 struct bfd_link_order *link_order)
11191 reloc_howto_type *howto;
11195 struct bfd_elf_section_reloc_data *reldata;
11196 struct elf_link_hash_entry **rel_hash_ptr;
11197 Elf_Internal_Shdr *rel_hdr;
11198 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11199 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11202 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11204 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11207 bfd_set_error (bfd_error_bad_value);
11211 addend = link_order->u.reloc.p->addend;
11214 reldata = &esdo->rel;
11215 else if (esdo->rela.hdr)
11216 reldata = &esdo->rela;
11223 /* Figure out the symbol index. */
11224 rel_hash_ptr = reldata->hashes + reldata->count;
11225 if (link_order->type == bfd_section_reloc_link_order)
11227 indx = link_order->u.reloc.p->u.section->target_index;
11228 BFD_ASSERT (indx != 0);
11229 *rel_hash_ptr = NULL;
11233 struct elf_link_hash_entry *h;
11235 /* Treat a reloc against a defined symbol as though it were
11236 actually against the section. */
11237 h = ((struct elf_link_hash_entry *)
11238 bfd_wrapped_link_hash_lookup (output_bfd, info,
11239 link_order->u.reloc.p->u.name,
11240 FALSE, FALSE, TRUE));
11242 && (h->root.type == bfd_link_hash_defined
11243 || h->root.type == bfd_link_hash_defweak))
11247 section = h->root.u.def.section;
11248 indx = section->output_section->target_index;
11249 *rel_hash_ptr = NULL;
11250 /* It seems that we ought to add the symbol value to the
11251 addend here, but in practice it has already been added
11252 because it was passed to constructor_callback. */
11253 addend += section->output_section->vma + section->output_offset;
11255 else if (h != NULL)
11257 /* Setting the index to -2 tells elf_link_output_extsym that
11258 this symbol is used by a reloc. */
11265 (*info->callbacks->unattached_reloc)
11266 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11271 /* If this is an inplace reloc, we must write the addend into the
11273 if (howto->partial_inplace && addend != 0)
11275 bfd_size_type size;
11276 bfd_reloc_status_type rstat;
11279 const char *sym_name;
11281 size = (bfd_size_type) bfd_get_reloc_size (howto);
11282 buf = (bfd_byte *) bfd_zmalloc (size);
11283 if (buf == NULL && size != 0)
11285 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11292 case bfd_reloc_outofrange:
11295 case bfd_reloc_overflow:
11296 if (link_order->type == bfd_section_reloc_link_order)
11297 sym_name = bfd_section_name (output_bfd,
11298 link_order->u.reloc.p->u.section);
11300 sym_name = link_order->u.reloc.p->u.name;
11301 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11302 howto->name, addend, NULL, NULL,
11307 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11309 * bfd_octets_per_byte (output_bfd),
11316 /* The address of a reloc is relative to the section in a
11317 relocatable file, and is a virtual address in an executable
11319 offset = link_order->offset;
11320 if (! bfd_link_relocatable (info))
11321 offset += output_section->vma;
11323 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11325 irel[i].r_offset = offset;
11326 irel[i].r_info = 0;
11327 irel[i].r_addend = 0;
11329 if (bed->s->arch_size == 32)
11330 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11332 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11334 rel_hdr = reldata->hdr;
11335 erel = rel_hdr->contents;
11336 if (rel_hdr->sh_type == SHT_REL)
11338 erel += reldata->count * bed->s->sizeof_rel;
11339 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11343 irel[0].r_addend = addend;
11344 erel += reldata->count * bed->s->sizeof_rela;
11345 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11354 /* Get the output vma of the section pointed to by the sh_link field. */
11357 elf_get_linked_section_vma (struct bfd_link_order *p)
11359 Elf_Internal_Shdr **elf_shdrp;
11363 s = p->u.indirect.section;
11364 elf_shdrp = elf_elfsections (s->owner);
11365 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11366 elfsec = elf_shdrp[elfsec]->sh_link;
11368 The Intel C compiler generates SHT_IA_64_UNWIND with
11369 SHF_LINK_ORDER. But it doesn't set the sh_link or
11370 sh_info fields. Hence we could get the situation
11371 where elfsec is 0. */
11374 const struct elf_backend_data *bed
11375 = get_elf_backend_data (s->owner);
11376 if (bed->link_order_error_handler)
11377 bed->link_order_error_handler
11378 /* xgettext:c-format */
11379 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s);
11384 s = elf_shdrp[elfsec]->bfd_section;
11385 return s->output_section->vma + s->output_offset;
11390 /* Compare two sections based on the locations of the sections they are
11391 linked to. Used by elf_fixup_link_order. */
11394 compare_link_order (const void * a, const void * b)
11399 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11400 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11403 return apos > bpos;
11407 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11408 order as their linked sections. Returns false if this could not be done
11409 because an output section includes both ordered and unordered
11410 sections. Ideally we'd do this in the linker proper. */
11413 elf_fixup_link_order (bfd *abfd, asection *o)
11415 int seen_linkorder;
11418 struct bfd_link_order *p;
11420 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11422 struct bfd_link_order **sections;
11423 asection *s, *other_sec, *linkorder_sec;
11427 linkorder_sec = NULL;
11429 seen_linkorder = 0;
11430 for (p = o->map_head.link_order; p != NULL; p = p->next)
11432 if (p->type == bfd_indirect_link_order)
11434 s = p->u.indirect.section;
11436 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11437 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11438 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11439 && elfsec < elf_numsections (sub)
11440 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11441 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11455 if (seen_other && seen_linkorder)
11457 if (other_sec && linkorder_sec)
11459 /* xgettext:c-format */
11460 (_("%pA has both ordered [`%pA' in %pB] "
11461 "and unordered [`%pA' in %pB] sections"),
11462 o, linkorder_sec, linkorder_sec->owner,
11463 other_sec, other_sec->owner);
11466 (_("%pA has both ordered and unordered sections"), o);
11467 bfd_set_error (bfd_error_bad_value);
11472 if (!seen_linkorder)
11475 sections = (struct bfd_link_order **)
11476 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11477 if (sections == NULL)
11479 seen_linkorder = 0;
11481 for (p = o->map_head.link_order; p != NULL; p = p->next)
11483 sections[seen_linkorder++] = p;
11485 /* Sort the input sections in the order of their linked section. */
11486 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11487 compare_link_order);
11489 /* Change the offsets of the sections. */
11491 for (n = 0; n < seen_linkorder; n++)
11493 s = sections[n]->u.indirect.section;
11494 offset &= ~(bfd_vma) 0 << s->alignment_power;
11495 s->output_offset = offset / bfd_octets_per_byte (abfd);
11496 sections[n]->offset = offset;
11497 offset += sections[n]->size;
11504 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11505 Returns TRUE upon success, FALSE otherwise. */
11508 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11510 bfd_boolean ret = FALSE;
11512 const struct elf_backend_data *bed;
11514 enum bfd_architecture arch;
11516 asymbol **sympp = NULL;
11520 elf_symbol_type *osymbuf;
11522 implib_bfd = info->out_implib_bfd;
11523 bed = get_elf_backend_data (abfd);
11525 if (!bfd_set_format (implib_bfd, bfd_object))
11528 /* Use flag from executable but make it a relocatable object. */
11529 flags = bfd_get_file_flags (abfd);
11530 flags &= ~HAS_RELOC;
11531 if (!bfd_set_start_address (implib_bfd, 0)
11532 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11535 /* Copy architecture of output file to import library file. */
11536 arch = bfd_get_arch (abfd);
11537 mach = bfd_get_mach (abfd);
11538 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11539 && (abfd->target_defaulted
11540 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11543 /* Get symbol table size. */
11544 symsize = bfd_get_symtab_upper_bound (abfd);
11548 /* Read in the symbol table. */
11549 sympp = (asymbol **) xmalloc (symsize);
11550 symcount = bfd_canonicalize_symtab (abfd, sympp);
11554 /* Allow the BFD backend to copy any private header data it
11555 understands from the output BFD to the import library BFD. */
11556 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11559 /* Filter symbols to appear in the import library. */
11560 if (bed->elf_backend_filter_implib_symbols)
11561 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11564 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11567 bfd_set_error (bfd_error_no_symbols);
11568 _bfd_error_handler (_("%pB: no symbol found for import library"),
11574 /* Make symbols absolute. */
11575 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11576 sizeof (*osymbuf));
11577 for (src_count = 0; src_count < symcount; src_count++)
11579 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11580 sizeof (*osymbuf));
11581 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11582 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11583 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11584 osymbuf[src_count].internal_elf_sym.st_value =
11585 osymbuf[src_count].symbol.value;
11586 sympp[src_count] = &osymbuf[src_count].symbol;
11589 bfd_set_symtab (implib_bfd, sympp, symcount);
11591 /* Allow the BFD backend to copy any private data it understands
11592 from the output BFD to the import library BFD. This is done last
11593 to permit the routine to look at the filtered symbol table. */
11594 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11597 if (!bfd_close (implib_bfd))
11608 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11612 if (flinfo->symstrtab != NULL)
11613 _bfd_elf_strtab_free (flinfo->symstrtab);
11614 if (flinfo->contents != NULL)
11615 free (flinfo->contents);
11616 if (flinfo->external_relocs != NULL)
11617 free (flinfo->external_relocs);
11618 if (flinfo->internal_relocs != NULL)
11619 free (flinfo->internal_relocs);
11620 if (flinfo->external_syms != NULL)
11621 free (flinfo->external_syms);
11622 if (flinfo->locsym_shndx != NULL)
11623 free (flinfo->locsym_shndx);
11624 if (flinfo->internal_syms != NULL)
11625 free (flinfo->internal_syms);
11626 if (flinfo->indices != NULL)
11627 free (flinfo->indices);
11628 if (flinfo->sections != NULL)
11629 free (flinfo->sections);
11630 if (flinfo->symshndxbuf != NULL)
11631 free (flinfo->symshndxbuf);
11632 for (o = obfd->sections; o != NULL; o = o->next)
11634 struct bfd_elf_section_data *esdo = elf_section_data (o);
11635 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11636 free (esdo->rel.hashes);
11637 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11638 free (esdo->rela.hashes);
11642 /* Do the final step of an ELF link. */
11645 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11647 bfd_boolean dynamic;
11648 bfd_boolean emit_relocs;
11650 struct elf_final_link_info flinfo;
11652 struct bfd_link_order *p;
11654 bfd_size_type max_contents_size;
11655 bfd_size_type max_external_reloc_size;
11656 bfd_size_type max_internal_reloc_count;
11657 bfd_size_type max_sym_count;
11658 bfd_size_type max_sym_shndx_count;
11659 Elf_Internal_Sym elfsym;
11661 Elf_Internal_Shdr *symtab_hdr;
11662 Elf_Internal_Shdr *symtab_shndx_hdr;
11663 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11664 struct elf_outext_info eoinfo;
11665 bfd_boolean merged;
11666 size_t relativecount = 0;
11667 asection *reldyn = 0;
11669 asection *attr_section = NULL;
11670 bfd_vma attr_size = 0;
11671 const char *std_attrs_section;
11672 struct elf_link_hash_table *htab = elf_hash_table (info);
11674 if (!is_elf_hash_table (htab))
11677 if (bfd_link_pic (info))
11678 abfd->flags |= DYNAMIC;
11680 dynamic = htab->dynamic_sections_created;
11681 dynobj = htab->dynobj;
11683 emit_relocs = (bfd_link_relocatable (info)
11684 || info->emitrelocations);
11686 flinfo.info = info;
11687 flinfo.output_bfd = abfd;
11688 flinfo.symstrtab = _bfd_elf_strtab_init ();
11689 if (flinfo.symstrtab == NULL)
11694 flinfo.hash_sec = NULL;
11695 flinfo.symver_sec = NULL;
11699 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11700 /* Note that dynsym_sec can be NULL (on VMS). */
11701 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11702 /* Note that it is OK if symver_sec is NULL. */
11705 flinfo.contents = NULL;
11706 flinfo.external_relocs = NULL;
11707 flinfo.internal_relocs = NULL;
11708 flinfo.external_syms = NULL;
11709 flinfo.locsym_shndx = NULL;
11710 flinfo.internal_syms = NULL;
11711 flinfo.indices = NULL;
11712 flinfo.sections = NULL;
11713 flinfo.symshndxbuf = NULL;
11714 flinfo.filesym_count = 0;
11716 /* The object attributes have been merged. Remove the input
11717 sections from the link, and set the contents of the output
11719 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11720 for (o = abfd->sections; o != NULL; o = o->next)
11722 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11723 || strcmp (o->name, ".gnu.attributes") == 0)
11725 for (p = o->map_head.link_order; p != NULL; p = p->next)
11727 asection *input_section;
11729 if (p->type != bfd_indirect_link_order)
11731 input_section = p->u.indirect.section;
11732 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11733 elf_link_input_bfd ignores this section. */
11734 input_section->flags &= ~SEC_HAS_CONTENTS;
11737 attr_size = bfd_elf_obj_attr_size (abfd);
11740 bfd_set_section_size (abfd, o, attr_size);
11742 /* Skip this section later on. */
11743 o->map_head.link_order = NULL;
11746 o->flags |= SEC_EXCLUDE;
11748 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11750 /* Remove empty group section from linker output. */
11751 o->flags |= SEC_EXCLUDE;
11752 bfd_section_list_remove (abfd, o);
11753 abfd->section_count--;
11757 /* Count up the number of relocations we will output for each output
11758 section, so that we know the sizes of the reloc sections. We
11759 also figure out some maximum sizes. */
11760 max_contents_size = 0;
11761 max_external_reloc_size = 0;
11762 max_internal_reloc_count = 0;
11764 max_sym_shndx_count = 0;
11766 for (o = abfd->sections; o != NULL; o = o->next)
11768 struct bfd_elf_section_data *esdo = elf_section_data (o);
11769 o->reloc_count = 0;
11771 for (p = o->map_head.link_order; p != NULL; p = p->next)
11773 unsigned int reloc_count = 0;
11774 unsigned int additional_reloc_count = 0;
11775 struct bfd_elf_section_data *esdi = NULL;
11777 if (p->type == bfd_section_reloc_link_order
11778 || p->type == bfd_symbol_reloc_link_order)
11780 else if (p->type == bfd_indirect_link_order)
11784 sec = p->u.indirect.section;
11786 /* Mark all sections which are to be included in the
11787 link. This will normally be every section. We need
11788 to do this so that we can identify any sections which
11789 the linker has decided to not include. */
11790 sec->linker_mark = TRUE;
11792 if (sec->flags & SEC_MERGE)
11795 if (sec->rawsize > max_contents_size)
11796 max_contents_size = sec->rawsize;
11797 if (sec->size > max_contents_size)
11798 max_contents_size = sec->size;
11800 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11801 && (sec->owner->flags & DYNAMIC) == 0)
11805 /* We are interested in just local symbols, not all
11807 if (elf_bad_symtab (sec->owner))
11808 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11809 / bed->s->sizeof_sym);
11811 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11813 if (sym_count > max_sym_count)
11814 max_sym_count = sym_count;
11816 if (sym_count > max_sym_shndx_count
11817 && elf_symtab_shndx_list (sec->owner) != NULL)
11818 max_sym_shndx_count = sym_count;
11820 if (esdo->this_hdr.sh_type == SHT_REL
11821 || esdo->this_hdr.sh_type == SHT_RELA)
11822 /* Some backends use reloc_count in relocation sections
11823 to count particular types of relocs. Of course,
11824 reloc sections themselves can't have relocations. */
11826 else if (emit_relocs)
11828 reloc_count = sec->reloc_count;
11829 if (bed->elf_backend_count_additional_relocs)
11832 c = (*bed->elf_backend_count_additional_relocs) (sec);
11833 additional_reloc_count += c;
11836 else if (bed->elf_backend_count_relocs)
11837 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11839 esdi = elf_section_data (sec);
11841 if ((sec->flags & SEC_RELOC) != 0)
11843 size_t ext_size = 0;
11845 if (esdi->rel.hdr != NULL)
11846 ext_size = esdi->rel.hdr->sh_size;
11847 if (esdi->rela.hdr != NULL)
11848 ext_size += esdi->rela.hdr->sh_size;
11850 if (ext_size > max_external_reloc_size)
11851 max_external_reloc_size = ext_size;
11852 if (sec->reloc_count > max_internal_reloc_count)
11853 max_internal_reloc_count = sec->reloc_count;
11858 if (reloc_count == 0)
11861 reloc_count += additional_reloc_count;
11862 o->reloc_count += reloc_count;
11864 if (p->type == bfd_indirect_link_order && emit_relocs)
11868 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11869 esdo->rel.count += additional_reloc_count;
11871 if (esdi->rela.hdr)
11873 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11874 esdo->rela.count += additional_reloc_count;
11880 esdo->rela.count += reloc_count;
11882 esdo->rel.count += reloc_count;
11886 if (o->reloc_count > 0)
11887 o->flags |= SEC_RELOC;
11890 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11891 set it (this is probably a bug) and if it is set
11892 assign_section_numbers will create a reloc section. */
11893 o->flags &=~ SEC_RELOC;
11896 /* If the SEC_ALLOC flag is not set, force the section VMA to
11897 zero. This is done in elf_fake_sections as well, but forcing
11898 the VMA to 0 here will ensure that relocs against these
11899 sections are handled correctly. */
11900 if ((o->flags & SEC_ALLOC) == 0
11901 && ! o->user_set_vma)
11905 if (! bfd_link_relocatable (info) && merged)
11906 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11908 /* Figure out the file positions for everything but the symbol table
11909 and the relocs. We set symcount to force assign_section_numbers
11910 to create a symbol table. */
11911 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11912 BFD_ASSERT (! abfd->output_has_begun);
11913 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11916 /* Set sizes, and assign file positions for reloc sections. */
11917 for (o = abfd->sections; o != NULL; o = o->next)
11919 struct bfd_elf_section_data *esdo = elf_section_data (o);
11920 if ((o->flags & SEC_RELOC) != 0)
11923 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11927 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11931 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11932 to count upwards while actually outputting the relocations. */
11933 esdo->rel.count = 0;
11934 esdo->rela.count = 0;
11936 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11938 /* Cache the section contents so that they can be compressed
11939 later. Use bfd_malloc since it will be freed by
11940 bfd_compress_section_contents. */
11941 unsigned char *contents = esdo->this_hdr.contents;
11942 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11945 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11946 if (contents == NULL)
11948 esdo->this_hdr.contents = contents;
11952 /* We have now assigned file positions for all the sections except
11953 .symtab, .strtab, and non-loaded reloc sections. We start the
11954 .symtab section at the current file position, and write directly
11955 to it. We build the .strtab section in memory. */
11956 bfd_get_symcount (abfd) = 0;
11957 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11958 /* sh_name is set in prep_headers. */
11959 symtab_hdr->sh_type = SHT_SYMTAB;
11960 /* sh_flags, sh_addr and sh_size all start off zero. */
11961 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11962 /* sh_link is set in assign_section_numbers. */
11963 /* sh_info is set below. */
11964 /* sh_offset is set just below. */
11965 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11967 if (max_sym_count < 20)
11968 max_sym_count = 20;
11969 htab->strtabsize = max_sym_count;
11970 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11971 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11972 if (htab->strtab == NULL)
11974 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11976 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11977 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11979 if (info->strip != strip_all || emit_relocs)
11981 file_ptr off = elf_next_file_pos (abfd);
11983 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11985 /* Note that at this point elf_next_file_pos (abfd) is
11986 incorrect. We do not yet know the size of the .symtab section.
11987 We correct next_file_pos below, after we do know the size. */
11989 /* Start writing out the symbol table. The first symbol is always a
11991 elfsym.st_value = 0;
11992 elfsym.st_size = 0;
11993 elfsym.st_info = 0;
11994 elfsym.st_other = 0;
11995 elfsym.st_shndx = SHN_UNDEF;
11996 elfsym.st_target_internal = 0;
11997 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11998 bfd_und_section_ptr, NULL) != 1)
12001 /* Output a symbol for each section. We output these even if we are
12002 discarding local symbols, since they are used for relocs. These
12003 symbols have no names. We store the index of each one in the
12004 index field of the section, so that we can find it again when
12005 outputting relocs. */
12007 elfsym.st_size = 0;
12008 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12009 elfsym.st_other = 0;
12010 elfsym.st_value = 0;
12011 elfsym.st_target_internal = 0;
12012 for (i = 1; i < elf_numsections (abfd); i++)
12014 o = bfd_section_from_elf_index (abfd, i);
12017 o->target_index = bfd_get_symcount (abfd);
12018 elfsym.st_shndx = i;
12019 if (!bfd_link_relocatable (info))
12020 elfsym.st_value = o->vma;
12021 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12028 /* Allocate some memory to hold information read in from the input
12030 if (max_contents_size != 0)
12032 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12033 if (flinfo.contents == NULL)
12037 if (max_external_reloc_size != 0)
12039 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12040 if (flinfo.external_relocs == NULL)
12044 if (max_internal_reloc_count != 0)
12046 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12047 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12048 if (flinfo.internal_relocs == NULL)
12052 if (max_sym_count != 0)
12054 amt = max_sym_count * bed->s->sizeof_sym;
12055 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12056 if (flinfo.external_syms == NULL)
12059 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12060 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12061 if (flinfo.internal_syms == NULL)
12064 amt = max_sym_count * sizeof (long);
12065 flinfo.indices = (long int *) bfd_malloc (amt);
12066 if (flinfo.indices == NULL)
12069 amt = max_sym_count * sizeof (asection *);
12070 flinfo.sections = (asection **) bfd_malloc (amt);
12071 if (flinfo.sections == NULL)
12075 if (max_sym_shndx_count != 0)
12077 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12078 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12079 if (flinfo.locsym_shndx == NULL)
12085 bfd_vma base, end = 0;
12088 for (sec = htab->tls_sec;
12089 sec && (sec->flags & SEC_THREAD_LOCAL);
12092 bfd_size_type size = sec->size;
12095 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12097 struct bfd_link_order *ord = sec->map_tail.link_order;
12100 size = ord->offset + ord->size;
12102 end = sec->vma + size;
12104 base = htab->tls_sec->vma;
12105 /* Only align end of TLS section if static TLS doesn't have special
12106 alignment requirements. */
12107 if (bed->static_tls_alignment == 1)
12108 end = align_power (end, htab->tls_sec->alignment_power);
12109 htab->tls_size = end - base;
12112 /* Reorder SHF_LINK_ORDER sections. */
12113 for (o = abfd->sections; o != NULL; o = o->next)
12115 if (!elf_fixup_link_order (abfd, o))
12119 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12122 /* Since ELF permits relocations to be against local symbols, we
12123 must have the local symbols available when we do the relocations.
12124 Since we would rather only read the local symbols once, and we
12125 would rather not keep them in memory, we handle all the
12126 relocations for a single input file at the same time.
12128 Unfortunately, there is no way to know the total number of local
12129 symbols until we have seen all of them, and the local symbol
12130 indices precede the global symbol indices. This means that when
12131 we are generating relocatable output, and we see a reloc against
12132 a global symbol, we can not know the symbol index until we have
12133 finished examining all the local symbols to see which ones we are
12134 going to output. To deal with this, we keep the relocations in
12135 memory, and don't output them until the end of the link. This is
12136 an unfortunate waste of memory, but I don't see a good way around
12137 it. Fortunately, it only happens when performing a relocatable
12138 link, which is not the common case. FIXME: If keep_memory is set
12139 we could write the relocs out and then read them again; I don't
12140 know how bad the memory loss will be. */
12142 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12143 sub->output_has_begun = FALSE;
12144 for (o = abfd->sections; o != NULL; o = o->next)
12146 for (p = o->map_head.link_order; p != NULL; p = p->next)
12148 if (p->type == bfd_indirect_link_order
12149 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12150 == bfd_target_elf_flavour)
12151 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12153 if (! sub->output_has_begun)
12155 if (! elf_link_input_bfd (&flinfo, sub))
12157 sub->output_has_begun = TRUE;
12160 else if (p->type == bfd_section_reloc_link_order
12161 || p->type == bfd_symbol_reloc_link_order)
12163 if (! elf_reloc_link_order (abfd, info, o, p))
12168 if (! _bfd_default_link_order (abfd, info, o, p))
12170 if (p->type == bfd_indirect_link_order
12171 && (bfd_get_flavour (sub)
12172 == bfd_target_elf_flavour)
12173 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12174 != bed->s->elfclass))
12176 const char *iclass, *oclass;
12178 switch (bed->s->elfclass)
12180 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12181 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12182 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12186 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12188 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12189 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12190 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12194 bfd_set_error (bfd_error_wrong_format);
12196 /* xgettext:c-format */
12197 (_("%pB: file class %s incompatible with %s"),
12198 sub, iclass, oclass);
12207 /* Free symbol buffer if needed. */
12208 if (!info->reduce_memory_overheads)
12210 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12211 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12212 && elf_tdata (sub)->symbuf)
12214 free (elf_tdata (sub)->symbuf);
12215 elf_tdata (sub)->symbuf = NULL;
12219 /* Output any global symbols that got converted to local in a
12220 version script or due to symbol visibility. We do this in a
12221 separate step since ELF requires all local symbols to appear
12222 prior to any global symbols. FIXME: We should only do this if
12223 some global symbols were, in fact, converted to become local.
12224 FIXME: Will this work correctly with the Irix 5 linker? */
12225 eoinfo.failed = FALSE;
12226 eoinfo.flinfo = &flinfo;
12227 eoinfo.localsyms = TRUE;
12228 eoinfo.file_sym_done = FALSE;
12229 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12233 /* If backend needs to output some local symbols not present in the hash
12234 table, do it now. */
12235 if (bed->elf_backend_output_arch_local_syms
12236 && (info->strip != strip_all || emit_relocs))
12238 typedef int (*out_sym_func)
12239 (void *, const char *, Elf_Internal_Sym *, asection *,
12240 struct elf_link_hash_entry *);
12242 if (! ((*bed->elf_backend_output_arch_local_syms)
12243 (abfd, info, &flinfo,
12244 (out_sym_func) elf_link_output_symstrtab)))
12248 /* That wrote out all the local symbols. Finish up the symbol table
12249 with the global symbols. Even if we want to strip everything we
12250 can, we still need to deal with those global symbols that got
12251 converted to local in a version script. */
12253 /* The sh_info field records the index of the first non local symbol. */
12254 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12257 && htab->dynsym != NULL
12258 && htab->dynsym->output_section != bfd_abs_section_ptr)
12260 Elf_Internal_Sym sym;
12261 bfd_byte *dynsym = htab->dynsym->contents;
12263 o = htab->dynsym->output_section;
12264 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12266 /* Write out the section symbols for the output sections. */
12267 if (bfd_link_pic (info)
12268 || htab->is_relocatable_executable)
12274 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12276 sym.st_target_internal = 0;
12278 for (s = abfd->sections; s != NULL; s = s->next)
12284 dynindx = elf_section_data (s)->dynindx;
12287 indx = elf_section_data (s)->this_idx;
12288 BFD_ASSERT (indx > 0);
12289 sym.st_shndx = indx;
12290 if (! check_dynsym (abfd, &sym))
12292 sym.st_value = s->vma;
12293 dest = dynsym + dynindx * bed->s->sizeof_sym;
12294 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12298 /* Write out the local dynsyms. */
12299 if (htab->dynlocal)
12301 struct elf_link_local_dynamic_entry *e;
12302 for (e = htab->dynlocal; e ; e = e->next)
12307 /* Copy the internal symbol and turn off visibility.
12308 Note that we saved a word of storage and overwrote
12309 the original st_name with the dynstr_index. */
12311 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12313 s = bfd_section_from_elf_index (e->input_bfd,
12318 elf_section_data (s->output_section)->this_idx;
12319 if (! check_dynsym (abfd, &sym))
12321 sym.st_value = (s->output_section->vma
12323 + e->isym.st_value);
12326 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12327 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12332 /* We get the global symbols from the hash table. */
12333 eoinfo.failed = FALSE;
12334 eoinfo.localsyms = FALSE;
12335 eoinfo.flinfo = &flinfo;
12336 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12340 /* If backend needs to output some symbols not present in the hash
12341 table, do it now. */
12342 if (bed->elf_backend_output_arch_syms
12343 && (info->strip != strip_all || emit_relocs))
12345 typedef int (*out_sym_func)
12346 (void *, const char *, Elf_Internal_Sym *, asection *,
12347 struct elf_link_hash_entry *);
12349 if (! ((*bed->elf_backend_output_arch_syms)
12350 (abfd, info, &flinfo,
12351 (out_sym_func) elf_link_output_symstrtab)))
12355 /* Finalize the .strtab section. */
12356 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12358 /* Swap out the .strtab section. */
12359 if (!elf_link_swap_symbols_out (&flinfo))
12362 /* Now we know the size of the symtab section. */
12363 if (bfd_get_symcount (abfd) > 0)
12365 /* Finish up and write out the symbol string table (.strtab)
12367 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12368 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12370 if (elf_symtab_shndx_list (abfd))
12372 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12374 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12376 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12377 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12378 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12379 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12380 symtab_shndx_hdr->sh_size = amt;
12382 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12385 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12386 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12391 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12392 /* sh_name was set in prep_headers. */
12393 symstrtab_hdr->sh_type = SHT_STRTAB;
12394 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12395 symstrtab_hdr->sh_addr = 0;
12396 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12397 symstrtab_hdr->sh_entsize = 0;
12398 symstrtab_hdr->sh_link = 0;
12399 symstrtab_hdr->sh_info = 0;
12400 /* sh_offset is set just below. */
12401 symstrtab_hdr->sh_addralign = 1;
12403 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12405 elf_next_file_pos (abfd) = off;
12407 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12408 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12412 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12414 _bfd_error_handler (_("%pB: failed to generate import library"),
12415 info->out_implib_bfd);
12419 /* Adjust the relocs to have the correct symbol indices. */
12420 for (o = abfd->sections; o != NULL; o = o->next)
12422 struct bfd_elf_section_data *esdo = elf_section_data (o);
12425 if ((o->flags & SEC_RELOC) == 0)
12428 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12429 if (esdo->rel.hdr != NULL
12430 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12432 if (esdo->rela.hdr != NULL
12433 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12436 /* Set the reloc_count field to 0 to prevent write_relocs from
12437 trying to swap the relocs out itself. */
12438 o->reloc_count = 0;
12441 if (dynamic && info->combreloc && dynobj != NULL)
12442 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12444 /* If we are linking against a dynamic object, or generating a
12445 shared library, finish up the dynamic linking information. */
12448 bfd_byte *dyncon, *dynconend;
12450 /* Fix up .dynamic entries. */
12451 o = bfd_get_linker_section (dynobj, ".dynamic");
12452 BFD_ASSERT (o != NULL);
12454 dyncon = o->contents;
12455 dynconend = o->contents + o->size;
12456 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12458 Elf_Internal_Dyn dyn;
12461 bfd_size_type sh_size;
12464 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12471 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12473 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12475 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12476 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12479 dyn.d_un.d_val = relativecount;
12486 name = info->init_function;
12489 name = info->fini_function;
12492 struct elf_link_hash_entry *h;
12494 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12496 && (h->root.type == bfd_link_hash_defined
12497 || h->root.type == bfd_link_hash_defweak))
12499 dyn.d_un.d_ptr = h->root.u.def.value;
12500 o = h->root.u.def.section;
12501 if (o->output_section != NULL)
12502 dyn.d_un.d_ptr += (o->output_section->vma
12503 + o->output_offset);
12506 /* The symbol is imported from another shared
12507 library and does not apply to this one. */
12508 dyn.d_un.d_ptr = 0;
12515 case DT_PREINIT_ARRAYSZ:
12516 name = ".preinit_array";
12518 case DT_INIT_ARRAYSZ:
12519 name = ".init_array";
12521 case DT_FINI_ARRAYSZ:
12522 name = ".fini_array";
12524 o = bfd_get_section_by_name (abfd, name);
12528 (_("could not find section %s"), name);
12533 (_("warning: %s section has zero size"), name);
12534 dyn.d_un.d_val = o->size;
12537 case DT_PREINIT_ARRAY:
12538 name = ".preinit_array";
12540 case DT_INIT_ARRAY:
12541 name = ".init_array";
12543 case DT_FINI_ARRAY:
12544 name = ".fini_array";
12546 o = bfd_get_section_by_name (abfd, name);
12553 name = ".gnu.hash";
12562 name = ".gnu.version_d";
12565 name = ".gnu.version_r";
12568 name = ".gnu.version";
12570 o = bfd_get_linker_section (dynobj, name);
12572 if (o == NULL || bfd_is_abs_section (o->output_section))
12575 (_("could not find section %s"), name);
12578 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12581 (_("warning: section '%s' is being made into a note"), name);
12582 bfd_set_error (bfd_error_nonrepresentable_section);
12585 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12592 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12598 for (i = 1; i < elf_numsections (abfd); i++)
12600 Elf_Internal_Shdr *hdr;
12602 hdr = elf_elfsections (abfd)[i];
12603 if (hdr->sh_type == type
12604 && (hdr->sh_flags & SHF_ALLOC) != 0)
12606 sh_size += hdr->sh_size;
12608 || sh_addr > hdr->sh_addr)
12609 sh_addr = hdr->sh_addr;
12613 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12615 /* Don't count procedure linkage table relocs in the
12616 overall reloc count. */
12617 sh_size -= htab->srelplt->size;
12619 /* If the size is zero, make the address zero too.
12620 This is to avoid a glibc bug. If the backend
12621 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12622 zero, then we'll put DT_RELA at the end of
12623 DT_JMPREL. glibc will interpret the end of
12624 DT_RELA matching the end of DT_JMPREL as the
12625 case where DT_RELA includes DT_JMPREL, and for
12626 LD_BIND_NOW will decide that processing DT_RELA
12627 will process the PLT relocs too. Net result:
12628 No PLT relocs applied. */
12631 /* If .rela.plt is the first .rela section, exclude
12632 it from DT_RELA. */
12633 else if (sh_addr == (htab->srelplt->output_section->vma
12634 + htab->srelplt->output_offset))
12635 sh_addr += htab->srelplt->size;
12638 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12639 dyn.d_un.d_val = sh_size;
12641 dyn.d_un.d_ptr = sh_addr;
12644 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12648 /* If we have created any dynamic sections, then output them. */
12649 if (dynobj != NULL)
12651 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12654 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12655 if (((info->warn_shared_textrel && bfd_link_pic (info))
12656 || info->error_textrel)
12657 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12659 bfd_byte *dyncon, *dynconend;
12661 dyncon = o->contents;
12662 dynconend = o->contents + o->size;
12663 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12665 Elf_Internal_Dyn dyn;
12667 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12669 if (dyn.d_tag == DT_TEXTREL)
12671 if (info->error_textrel)
12672 info->callbacks->einfo
12673 (_("%P%X: read-only segment has dynamic relocations\n"));
12675 info->callbacks->einfo
12676 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12682 for (o = dynobj->sections; o != NULL; o = o->next)
12684 if ((o->flags & SEC_HAS_CONTENTS) == 0
12686 || o->output_section == bfd_abs_section_ptr)
12688 if ((o->flags & SEC_LINKER_CREATED) == 0)
12690 /* At this point, we are only interested in sections
12691 created by _bfd_elf_link_create_dynamic_sections. */
12694 if (htab->stab_info.stabstr == o)
12696 if (htab->eh_info.hdr_sec == o)
12698 if (strcmp (o->name, ".dynstr") != 0)
12700 if (! bfd_set_section_contents (abfd, o->output_section,
12702 (file_ptr) o->output_offset
12703 * bfd_octets_per_byte (abfd),
12709 /* The contents of the .dynstr section are actually in a
12713 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12714 if (bfd_seek (abfd, off, SEEK_SET) != 0
12715 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12721 if (!info->resolve_section_groups)
12723 bfd_boolean failed = FALSE;
12725 BFD_ASSERT (bfd_link_relocatable (info));
12726 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12731 /* If we have optimized stabs strings, output them. */
12732 if (htab->stab_info.stabstr != NULL)
12734 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12738 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12741 elf_final_link_free (abfd, &flinfo);
12743 elf_linker (abfd) = TRUE;
12747 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12748 if (contents == NULL)
12749 return FALSE; /* Bail out and fail. */
12750 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12751 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12758 elf_final_link_free (abfd, &flinfo);
12762 /* Initialize COOKIE for input bfd ABFD. */
12765 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12766 struct bfd_link_info *info, bfd *abfd)
12768 Elf_Internal_Shdr *symtab_hdr;
12769 const struct elf_backend_data *bed;
12771 bed = get_elf_backend_data (abfd);
12772 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12774 cookie->abfd = abfd;
12775 cookie->sym_hashes = elf_sym_hashes (abfd);
12776 cookie->bad_symtab = elf_bad_symtab (abfd);
12777 if (cookie->bad_symtab)
12779 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12780 cookie->extsymoff = 0;
12784 cookie->locsymcount = symtab_hdr->sh_info;
12785 cookie->extsymoff = symtab_hdr->sh_info;
12788 if (bed->s->arch_size == 32)
12789 cookie->r_sym_shift = 8;
12791 cookie->r_sym_shift = 32;
12793 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12794 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12796 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12797 cookie->locsymcount, 0,
12799 if (cookie->locsyms == NULL)
12801 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12804 if (info->keep_memory)
12805 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12810 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12813 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12815 Elf_Internal_Shdr *symtab_hdr;
12817 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12818 if (cookie->locsyms != NULL
12819 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12820 free (cookie->locsyms);
12823 /* Initialize the relocation information in COOKIE for input section SEC
12824 of input bfd ABFD. */
12827 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12828 struct bfd_link_info *info, bfd *abfd,
12831 if (sec->reloc_count == 0)
12833 cookie->rels = NULL;
12834 cookie->relend = NULL;
12838 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12839 info->keep_memory);
12840 if (cookie->rels == NULL)
12842 cookie->rel = cookie->rels;
12843 cookie->relend = cookie->rels + sec->reloc_count;
12845 cookie->rel = cookie->rels;
12849 /* Free the memory allocated by init_reloc_cookie_rels,
12853 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12856 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12857 free (cookie->rels);
12860 /* Initialize the whole of COOKIE for input section SEC. */
12863 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12864 struct bfd_link_info *info,
12867 if (!init_reloc_cookie (cookie, info, sec->owner))
12869 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12874 fini_reloc_cookie (cookie, sec->owner);
12879 /* Free the memory allocated by init_reloc_cookie_for_section,
12883 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12886 fini_reloc_cookie_rels (cookie, sec);
12887 fini_reloc_cookie (cookie, sec->owner);
12890 /* Garbage collect unused sections. */
12892 /* Default gc_mark_hook. */
12895 _bfd_elf_gc_mark_hook (asection *sec,
12896 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12897 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12898 struct elf_link_hash_entry *h,
12899 Elf_Internal_Sym *sym)
12903 switch (h->root.type)
12905 case bfd_link_hash_defined:
12906 case bfd_link_hash_defweak:
12907 return h->root.u.def.section;
12909 case bfd_link_hash_common:
12910 return h->root.u.c.p->section;
12917 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12922 /* Return the debug definition section. */
12925 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12926 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12927 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12928 struct elf_link_hash_entry *h,
12929 Elf_Internal_Sym *sym)
12933 /* Return the global debug definition section. */
12934 if ((h->root.type == bfd_link_hash_defined
12935 || h->root.type == bfd_link_hash_defweak)
12936 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12937 return h->root.u.def.section;
12941 /* Return the local debug definition section. */
12942 asection *isec = bfd_section_from_elf_index (sec->owner,
12944 if ((isec->flags & SEC_DEBUGGING) != 0)
12951 /* COOKIE->rel describes a relocation against section SEC, which is
12952 a section we've decided to keep. Return the section that contains
12953 the relocation symbol, or NULL if no section contains it. */
12956 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12957 elf_gc_mark_hook_fn gc_mark_hook,
12958 struct elf_reloc_cookie *cookie,
12959 bfd_boolean *start_stop)
12961 unsigned long r_symndx;
12962 struct elf_link_hash_entry *h;
12964 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12965 if (r_symndx == STN_UNDEF)
12968 if (r_symndx >= cookie->locsymcount
12969 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12971 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12974 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
12978 while (h->root.type == bfd_link_hash_indirect
12979 || h->root.type == bfd_link_hash_warning)
12980 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12982 /* If this symbol is weak and there is a non-weak definition, we
12983 keep the non-weak definition because many backends put
12984 dynamic reloc info on the non-weak definition for code
12985 handling copy relocs. */
12986 if (h->is_weakalias)
12987 weakdef (h)->mark = 1;
12989 if (start_stop != NULL)
12991 /* To work around a glibc bug, mark XXX input sections
12992 when there is a reference to __start_XXX or __stop_XXX
12996 asection *s = h->u2.start_stop_section;
12997 *start_stop = !s->gc_mark;
13002 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13005 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13006 &cookie->locsyms[r_symndx]);
13009 /* COOKIE->rel describes a relocation against section SEC, which is
13010 a section we've decided to keep. Mark the section that contains
13011 the relocation symbol. */
13014 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13016 elf_gc_mark_hook_fn gc_mark_hook,
13017 struct elf_reloc_cookie *cookie)
13020 bfd_boolean start_stop = FALSE;
13022 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13023 while (rsec != NULL)
13025 if (!rsec->gc_mark)
13027 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13028 || (rsec->owner->flags & DYNAMIC) != 0)
13030 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13035 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13040 /* The mark phase of garbage collection. For a given section, mark
13041 it and any sections in this section's group, and all the sections
13042 which define symbols to which it refers. */
13045 _bfd_elf_gc_mark (struct bfd_link_info *info,
13047 elf_gc_mark_hook_fn gc_mark_hook)
13050 asection *group_sec, *eh_frame;
13054 /* Mark all the sections in the group. */
13055 group_sec = elf_section_data (sec)->next_in_group;
13056 if (group_sec && !group_sec->gc_mark)
13057 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13060 /* Look through the section relocs. */
13062 eh_frame = elf_eh_frame_section (sec->owner);
13063 if ((sec->flags & SEC_RELOC) != 0
13064 && sec->reloc_count > 0
13065 && sec != eh_frame)
13067 struct elf_reloc_cookie cookie;
13069 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13073 for (; cookie.rel < cookie.relend; cookie.rel++)
13074 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13079 fini_reloc_cookie_for_section (&cookie, sec);
13083 if (ret && eh_frame && elf_fde_list (sec))
13085 struct elf_reloc_cookie cookie;
13087 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13091 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13092 gc_mark_hook, &cookie))
13094 fini_reloc_cookie_for_section (&cookie, eh_frame);
13098 eh_frame = elf_section_eh_frame_entry (sec);
13099 if (ret && eh_frame && !eh_frame->gc_mark)
13100 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13106 /* Scan and mark sections in a special or debug section group. */
13109 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13111 /* Point to first section of section group. */
13113 /* Used to iterate the section group. */
13116 bfd_boolean is_special_grp = TRUE;
13117 bfd_boolean is_debug_grp = TRUE;
13119 /* First scan to see if group contains any section other than debug
13120 and special section. */
13121 ssec = msec = elf_next_in_group (grp);
13124 if ((msec->flags & SEC_DEBUGGING) == 0)
13125 is_debug_grp = FALSE;
13127 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13128 is_special_grp = FALSE;
13130 msec = elf_next_in_group (msec);
13132 while (msec != ssec);
13134 /* If this is a pure debug section group or pure special section group,
13135 keep all sections in this group. */
13136 if (is_debug_grp || is_special_grp)
13141 msec = elf_next_in_group (msec);
13143 while (msec != ssec);
13147 /* Keep debug and special sections. */
13150 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13151 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13155 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13158 bfd_boolean some_kept;
13159 bfd_boolean debug_frag_seen;
13160 bfd_boolean has_kept_debug_info;
13162 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13164 isec = ibfd->sections;
13165 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13168 /* Ensure all linker created sections are kept,
13169 see if any other section is already marked,
13170 and note if we have any fragmented debug sections. */
13171 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13172 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13174 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13176 else if (isec->gc_mark
13177 && (isec->flags & SEC_ALLOC) != 0
13178 && elf_section_type (isec) != SHT_NOTE)
13181 if (!debug_frag_seen
13182 && (isec->flags & SEC_DEBUGGING)
13183 && CONST_STRNEQ (isec->name, ".debug_line."))
13184 debug_frag_seen = TRUE;
13187 /* If no non-note alloc section in this file will be kept, then
13188 we can toss out the debug and special sections. */
13192 /* Keep debug and special sections like .comment when they are
13193 not part of a group. Also keep section groups that contain
13194 just debug sections or special sections. */
13195 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13197 if ((isec->flags & SEC_GROUP) != 0)
13198 _bfd_elf_gc_mark_debug_special_section_group (isec);
13199 else if (((isec->flags & SEC_DEBUGGING) != 0
13200 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13201 && elf_next_in_group (isec) == NULL)
13203 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13204 has_kept_debug_info = TRUE;
13207 /* Look for CODE sections which are going to be discarded,
13208 and find and discard any fragmented debug sections which
13209 are associated with that code section. */
13210 if (debug_frag_seen)
13211 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13212 if ((isec->flags & SEC_CODE) != 0
13213 && isec->gc_mark == 0)
13218 ilen = strlen (isec->name);
13220 /* Association is determined by the name of the debug
13221 section containing the name of the code section as
13222 a suffix. For example .debug_line.text.foo is a
13223 debug section associated with .text.foo. */
13224 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13228 if (dsec->gc_mark == 0
13229 || (dsec->flags & SEC_DEBUGGING) == 0)
13232 dlen = strlen (dsec->name);
13235 && strncmp (dsec->name + (dlen - ilen),
13236 isec->name, ilen) == 0)
13241 /* Mark debug sections referenced by kept debug sections. */
13242 if (has_kept_debug_info)
13243 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13245 && (isec->flags & SEC_DEBUGGING) != 0)
13246 if (!_bfd_elf_gc_mark (info, isec,
13247 elf_gc_mark_debug_section))
13254 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13259 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13263 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13264 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13265 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13268 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13271 for (o = sub->sections; o != NULL; o = o->next)
13273 /* When any section in a section group is kept, we keep all
13274 sections in the section group. If the first member of
13275 the section group is excluded, we will also exclude the
13277 if (o->flags & SEC_GROUP)
13279 asection *first = elf_next_in_group (o);
13280 o->gc_mark = first->gc_mark;
13286 /* Skip sweeping sections already excluded. */
13287 if (o->flags & SEC_EXCLUDE)
13290 /* Since this is early in the link process, it is simple
13291 to remove a section from the output. */
13292 o->flags |= SEC_EXCLUDE;
13294 if (info->print_gc_sections && o->size != 0)
13295 /* xgettext:c-format */
13296 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13304 /* Propagate collected vtable information. This is called through
13305 elf_link_hash_traverse. */
13308 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13310 /* Those that are not vtables. */
13312 || h->u2.vtable == NULL
13313 || h->u2.vtable->parent == NULL)
13316 /* Those vtables that do not have parents, we cannot merge. */
13317 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13320 /* If we've already been done, exit. */
13321 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13324 /* Make sure the parent's table is up to date. */
13325 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13327 if (h->u2.vtable->used == NULL)
13329 /* None of this table's entries were referenced. Re-use the
13331 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13332 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13337 bfd_boolean *cu, *pu;
13339 /* Or the parent's entries into ours. */
13340 cu = h->u2.vtable->used;
13342 pu = h->u2.vtable->parent->u2.vtable->used;
13345 const struct elf_backend_data *bed;
13346 unsigned int log_file_align;
13348 bed = get_elf_backend_data (h->root.u.def.section->owner);
13349 log_file_align = bed->s->log_file_align;
13350 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13365 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13368 bfd_vma hstart, hend;
13369 Elf_Internal_Rela *relstart, *relend, *rel;
13370 const struct elf_backend_data *bed;
13371 unsigned int log_file_align;
13373 /* Take care of both those symbols that do not describe vtables as
13374 well as those that are not loaded. */
13376 || h->u2.vtable == NULL
13377 || h->u2.vtable->parent == NULL)
13380 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13381 || h->root.type == bfd_link_hash_defweak);
13383 sec = h->root.u.def.section;
13384 hstart = h->root.u.def.value;
13385 hend = hstart + h->size;
13387 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13389 return *(bfd_boolean *) okp = FALSE;
13390 bed = get_elf_backend_data (sec->owner);
13391 log_file_align = bed->s->log_file_align;
13393 relend = relstart + sec->reloc_count;
13395 for (rel = relstart; rel < relend; ++rel)
13396 if (rel->r_offset >= hstart && rel->r_offset < hend)
13398 /* If the entry is in use, do nothing. */
13399 if (h->u2.vtable->used
13400 && (rel->r_offset - hstart) < h->u2.vtable->size)
13402 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13403 if (h->u2.vtable->used[entry])
13406 /* Otherwise, kill it. */
13407 rel->r_offset = rel->r_info = rel->r_addend = 0;
13413 /* Mark sections containing dynamically referenced symbols. When
13414 building shared libraries, we must assume that any visible symbol is
13418 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13420 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13421 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13423 if ((h->root.type == bfd_link_hash_defined
13424 || h->root.type == bfd_link_hash_defweak)
13425 && ((h->ref_dynamic && !h->forced_local)
13426 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13427 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13428 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13429 && (!bfd_link_executable (info)
13430 || info->gc_keep_exported
13431 || info->export_dynamic
13434 && (*d->match) (&d->head, NULL, h->root.root.string)))
13435 && (h->versioned >= versioned
13436 || !bfd_hide_sym_by_version (info->version_info,
13437 h->root.root.string)))))
13438 h->root.u.def.section->flags |= SEC_KEEP;
13443 /* Keep all sections containing symbols undefined on the command-line,
13444 and the section containing the entry symbol. */
13447 _bfd_elf_gc_keep (struct bfd_link_info *info)
13449 struct bfd_sym_chain *sym;
13451 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13453 struct elf_link_hash_entry *h;
13455 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13456 FALSE, FALSE, FALSE);
13459 && (h->root.type == bfd_link_hash_defined
13460 || h->root.type == bfd_link_hash_defweak)
13461 && !bfd_is_abs_section (h->root.u.def.section)
13462 && !bfd_is_und_section (h->root.u.def.section))
13463 h->root.u.def.section->flags |= SEC_KEEP;
13468 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13469 struct bfd_link_info *info)
13471 bfd *ibfd = info->input_bfds;
13473 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13476 struct elf_reloc_cookie cookie;
13478 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13480 sec = ibfd->sections;
13481 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13484 if (!init_reloc_cookie (&cookie, info, ibfd))
13487 for (sec = ibfd->sections; sec; sec = sec->next)
13489 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13490 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13492 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13493 fini_reloc_cookie_rels (&cookie, sec);
13500 /* Do mark and sweep of unused sections. */
13503 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13505 bfd_boolean ok = TRUE;
13507 elf_gc_mark_hook_fn gc_mark_hook;
13508 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13509 struct elf_link_hash_table *htab;
13511 if (!bed->can_gc_sections
13512 || !is_elf_hash_table (info->hash))
13514 _bfd_error_handler(_("warning: gc-sections option ignored"));
13518 bed->gc_keep (info);
13519 htab = elf_hash_table (info);
13521 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13522 at the .eh_frame section if we can mark the FDEs individually. */
13523 for (sub = info->input_bfds;
13524 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13525 sub = sub->link.next)
13528 struct elf_reloc_cookie cookie;
13530 sec = sub->sections;
13531 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13533 sec = bfd_get_section_by_name (sub, ".eh_frame");
13534 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13536 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13537 if (elf_section_data (sec)->sec_info
13538 && (sec->flags & SEC_LINKER_CREATED) == 0)
13539 elf_eh_frame_section (sub) = sec;
13540 fini_reloc_cookie_for_section (&cookie, sec);
13541 sec = bfd_get_next_section_by_name (NULL, sec);
13545 /* Apply transitive closure to the vtable entry usage info. */
13546 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13550 /* Kill the vtable relocations that were not used. */
13551 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13555 /* Mark dynamically referenced symbols. */
13556 if (htab->dynamic_sections_created || info->gc_keep_exported)
13557 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13559 /* Grovel through relocs to find out who stays ... */
13560 gc_mark_hook = bed->gc_mark_hook;
13561 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13565 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13566 || elf_object_id (sub) != elf_hash_table_id (htab)
13567 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13571 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13574 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13575 Also treat note sections as a root, if the section is not part
13576 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13577 well as FINI_ARRAY sections for ld -r. */
13578 for (o = sub->sections; o != NULL; o = o->next)
13580 && (o->flags & SEC_EXCLUDE) == 0
13581 && ((o->flags & SEC_KEEP) != 0
13582 || (bfd_link_relocatable (info)
13583 && ((elf_section_data (o)->this_hdr.sh_type
13584 == SHT_PREINIT_ARRAY)
13585 || (elf_section_data (o)->this_hdr.sh_type
13587 || (elf_section_data (o)->this_hdr.sh_type
13588 == SHT_FINI_ARRAY)))
13589 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13590 && elf_next_in_group (o) == NULL )))
13592 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13597 /* Allow the backend to mark additional target specific sections. */
13598 bed->gc_mark_extra_sections (info, gc_mark_hook);
13600 /* ... and mark SEC_EXCLUDE for those that go. */
13601 return elf_gc_sweep (abfd, info);
13604 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13607 bfd_elf_gc_record_vtinherit (bfd *abfd,
13609 struct elf_link_hash_entry *h,
13612 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13613 struct elf_link_hash_entry **search, *child;
13614 size_t extsymcount;
13615 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13617 /* The sh_info field of the symtab header tells us where the
13618 external symbols start. We don't care about the local symbols at
13620 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13621 if (!elf_bad_symtab (abfd))
13622 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13624 sym_hashes = elf_sym_hashes (abfd);
13625 sym_hashes_end = sym_hashes + extsymcount;
13627 /* Hunt down the child symbol, which is in this section at the same
13628 offset as the relocation. */
13629 for (search = sym_hashes; search != sym_hashes_end; ++search)
13631 if ((child = *search) != NULL
13632 && (child->root.type == bfd_link_hash_defined
13633 || child->root.type == bfd_link_hash_defweak)
13634 && child->root.u.def.section == sec
13635 && child->root.u.def.value == offset)
13639 /* xgettext:c-format */
13640 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13641 abfd, sec, (uint64_t) offset);
13642 bfd_set_error (bfd_error_invalid_operation);
13646 if (!child->u2.vtable)
13648 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13649 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13650 if (!child->u2.vtable)
13655 /* This *should* only be the absolute section. It could potentially
13656 be that someone has defined a non-global vtable though, which
13657 would be bad. It isn't worth paging in the local symbols to be
13658 sure though; that case should simply be handled by the assembler. */
13660 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13663 child->u2.vtable->parent = h;
13668 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13671 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13672 asection *sec ATTRIBUTE_UNUSED,
13673 struct elf_link_hash_entry *h,
13676 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13677 unsigned int log_file_align = bed->s->log_file_align;
13681 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13682 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13687 if (addend >= h->u2.vtable->size)
13689 size_t size, bytes, file_align;
13690 bfd_boolean *ptr = h->u2.vtable->used;
13692 /* While the symbol is undefined, we have to be prepared to handle
13694 file_align = 1 << log_file_align;
13695 if (h->root.type == bfd_link_hash_undefined)
13696 size = addend + file_align;
13700 if (addend >= size)
13702 /* Oops! We've got a reference past the defined end of
13703 the table. This is probably a bug -- shall we warn? */
13704 size = addend + file_align;
13707 size = (size + file_align - 1) & -file_align;
13709 /* Allocate one extra entry for use as a "done" flag for the
13710 consolidation pass. */
13711 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13715 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13721 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13722 * sizeof (bfd_boolean));
13723 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13727 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13732 /* And arrange for that done flag to be at index -1. */
13733 h->u2.vtable->used = ptr + 1;
13734 h->u2.vtable->size = size;
13737 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13742 /* Map an ELF section header flag to its corresponding string. */
13746 flagword flag_value;
13747 } elf_flags_to_name_table;
13749 static elf_flags_to_name_table elf_flags_to_names [] =
13751 { "SHF_WRITE", SHF_WRITE },
13752 { "SHF_ALLOC", SHF_ALLOC },
13753 { "SHF_EXECINSTR", SHF_EXECINSTR },
13754 { "SHF_MERGE", SHF_MERGE },
13755 { "SHF_STRINGS", SHF_STRINGS },
13756 { "SHF_INFO_LINK", SHF_INFO_LINK},
13757 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13758 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13759 { "SHF_GROUP", SHF_GROUP },
13760 { "SHF_TLS", SHF_TLS },
13761 { "SHF_MASKOS", SHF_MASKOS },
13762 { "SHF_EXCLUDE", SHF_EXCLUDE },
13765 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13767 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13768 struct flag_info *flaginfo,
13771 const bfd_vma sh_flags = elf_section_flags (section);
13773 if (!flaginfo->flags_initialized)
13775 bfd *obfd = info->output_bfd;
13776 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13777 struct flag_info_list *tf = flaginfo->flag_list;
13779 int without_hex = 0;
13781 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13784 flagword (*lookup) (char *);
13786 lookup = bed->elf_backend_lookup_section_flags_hook;
13787 if (lookup != NULL)
13789 flagword hexval = (*lookup) ((char *) tf->name);
13793 if (tf->with == with_flags)
13794 with_hex |= hexval;
13795 else if (tf->with == without_flags)
13796 without_hex |= hexval;
13801 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13803 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13805 if (tf->with == with_flags)
13806 with_hex |= elf_flags_to_names[i].flag_value;
13807 else if (tf->with == without_flags)
13808 without_hex |= elf_flags_to_names[i].flag_value;
13815 info->callbacks->einfo
13816 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13820 flaginfo->flags_initialized = TRUE;
13821 flaginfo->only_with_flags |= with_hex;
13822 flaginfo->not_with_flags |= without_hex;
13825 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13828 if ((flaginfo->not_with_flags & sh_flags) != 0)
13834 struct alloc_got_off_arg {
13836 struct bfd_link_info *info;
13839 /* We need a special top-level link routine to convert got reference counts
13840 to real got offsets. */
13843 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13845 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13846 bfd *obfd = gofarg->info->output_bfd;
13847 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13849 if (h->got.refcount > 0)
13851 h->got.offset = gofarg->gotoff;
13852 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13855 h->got.offset = (bfd_vma) -1;
13860 /* And an accompanying bit to work out final got entry offsets once
13861 we're done. Should be called from final_link. */
13864 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13865 struct bfd_link_info *info)
13868 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13870 struct alloc_got_off_arg gofarg;
13872 BFD_ASSERT (abfd == info->output_bfd);
13874 if (! is_elf_hash_table (info->hash))
13877 /* The GOT offset is relative to the .got section, but the GOT header is
13878 put into the .got.plt section, if the backend uses it. */
13879 if (bed->want_got_plt)
13882 gotoff = bed->got_header_size;
13884 /* Do the local .got entries first. */
13885 for (i = info->input_bfds; i; i = i->link.next)
13887 bfd_signed_vma *local_got;
13888 size_t j, locsymcount;
13889 Elf_Internal_Shdr *symtab_hdr;
13891 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13894 local_got = elf_local_got_refcounts (i);
13898 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13899 if (elf_bad_symtab (i))
13900 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13902 locsymcount = symtab_hdr->sh_info;
13904 for (j = 0; j < locsymcount; ++j)
13906 if (local_got[j] > 0)
13908 local_got[j] = gotoff;
13909 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13912 local_got[j] = (bfd_vma) -1;
13916 /* Then the global .got entries. .plt refcounts are handled by
13917 adjust_dynamic_symbol */
13918 gofarg.gotoff = gotoff;
13919 gofarg.info = info;
13920 elf_link_hash_traverse (elf_hash_table (info),
13921 elf_gc_allocate_got_offsets,
13926 /* Many folk need no more in the way of final link than this, once
13927 got entry reference counting is enabled. */
13930 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13932 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13935 /* Invoke the regular ELF backend linker to do all the work. */
13936 return bfd_elf_final_link (abfd, info);
13940 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13942 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13944 if (rcookie->bad_symtab)
13945 rcookie->rel = rcookie->rels;
13947 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13949 unsigned long r_symndx;
13951 if (! rcookie->bad_symtab)
13952 if (rcookie->rel->r_offset > offset)
13954 if (rcookie->rel->r_offset != offset)
13957 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13958 if (r_symndx == STN_UNDEF)
13961 if (r_symndx >= rcookie->locsymcount
13962 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13964 struct elf_link_hash_entry *h;
13966 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13968 while (h->root.type == bfd_link_hash_indirect
13969 || h->root.type == bfd_link_hash_warning)
13970 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13972 if ((h->root.type == bfd_link_hash_defined
13973 || h->root.type == bfd_link_hash_defweak)
13974 && (h->root.u.def.section->owner != rcookie->abfd
13975 || h->root.u.def.section->kept_section != NULL
13976 || discarded_section (h->root.u.def.section)))
13981 /* It's not a relocation against a global symbol,
13982 but it could be a relocation against a local
13983 symbol for a discarded section. */
13985 Elf_Internal_Sym *isym;
13987 /* Need to: get the symbol; get the section. */
13988 isym = &rcookie->locsyms[r_symndx];
13989 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13991 && (isec->kept_section != NULL
13992 || discarded_section (isec)))
14000 /* Discard unneeded references to discarded sections.
14001 Returns -1 on error, 1 if any section's size was changed, 0 if
14002 nothing changed. This function assumes that the relocations are in
14003 sorted order, which is true for all known assemblers. */
14006 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14008 struct elf_reloc_cookie cookie;
14013 if (info->traditional_format
14014 || !is_elf_hash_table (info->hash))
14017 o = bfd_get_section_by_name (output_bfd, ".stab");
14022 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14025 || i->reloc_count == 0
14026 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14030 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14033 if (!init_reloc_cookie_for_section (&cookie, info, i))
14036 if (_bfd_discard_section_stabs (abfd, i,
14037 elf_section_data (i)->sec_info,
14038 bfd_elf_reloc_symbol_deleted_p,
14042 fini_reloc_cookie_for_section (&cookie, i);
14047 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14048 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14052 int eh_changed = 0;
14053 unsigned int eh_alignment;
14055 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14061 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14064 if (!init_reloc_cookie_for_section (&cookie, info, i))
14067 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14068 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14069 bfd_elf_reloc_symbol_deleted_p,
14073 if (i->size != i->rawsize)
14077 fini_reloc_cookie_for_section (&cookie, i);
14080 eh_alignment = 1 << o->alignment_power;
14081 /* Skip over zero terminator, and prevent empty sections from
14082 adding alignment padding at the end. */
14083 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14085 i->flags |= SEC_EXCLUDE;
14086 else if (i->size > 4)
14088 /* The last non-empty eh_frame section doesn't need padding. */
14091 /* Any prior sections must pad the last FDE out to the output
14092 section alignment. Otherwise we might have zero padding
14093 between sections, which would be seen as a terminator. */
14094 for (; i != NULL; i = i->map_tail.s)
14096 /* All but the last zero terminator should have been removed. */
14101 = (i->size + eh_alignment - 1) & -eh_alignment;
14102 if (i->size != size)
14110 elf_link_hash_traverse (elf_hash_table (info),
14111 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14114 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14116 const struct elf_backend_data *bed;
14119 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14121 s = abfd->sections;
14122 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14125 bed = get_elf_backend_data (abfd);
14127 if (bed->elf_backend_discard_info != NULL)
14129 if (!init_reloc_cookie (&cookie, info, abfd))
14132 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14135 fini_reloc_cookie (&cookie, abfd);
14139 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14140 _bfd_elf_end_eh_frame_parsing (info);
14142 if (info->eh_frame_hdr_type
14143 && !bfd_link_relocatable (info)
14144 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14151 _bfd_elf_section_already_linked (bfd *abfd,
14153 struct bfd_link_info *info)
14156 const char *name, *key;
14157 struct bfd_section_already_linked *l;
14158 struct bfd_section_already_linked_hash_entry *already_linked_list;
14160 if (sec->output_section == bfd_abs_section_ptr)
14163 flags = sec->flags;
14165 /* Return if it isn't a linkonce section. A comdat group section
14166 also has SEC_LINK_ONCE set. */
14167 if ((flags & SEC_LINK_ONCE) == 0)
14170 /* Don't put group member sections on our list of already linked
14171 sections. They are handled as a group via their group section. */
14172 if (elf_sec_group (sec) != NULL)
14175 /* For a SHT_GROUP section, use the group signature as the key. */
14177 if ((flags & SEC_GROUP) != 0
14178 && elf_next_in_group (sec) != NULL
14179 && elf_group_name (elf_next_in_group (sec)) != NULL)
14180 key = elf_group_name (elf_next_in_group (sec));
14183 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14184 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14185 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14188 /* Must be a user linkonce section that doesn't follow gcc's
14189 naming convention. In this case we won't be matching
14190 single member groups. */
14194 already_linked_list = bfd_section_already_linked_table_lookup (key);
14196 for (l = already_linked_list->entry; l != NULL; l = l->next)
14198 /* We may have 2 different types of sections on the list: group
14199 sections with a signature of <key> (<key> is some string),
14200 and linkonce sections named .gnu.linkonce.<type>.<key>.
14201 Match like sections. LTO plugin sections are an exception.
14202 They are always named .gnu.linkonce.t.<key> and match either
14203 type of section. */
14204 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14205 && ((flags & SEC_GROUP) != 0
14206 || strcmp (name, l->sec->name) == 0))
14207 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14209 /* The section has already been linked. See if we should
14210 issue a warning. */
14211 if (!_bfd_handle_already_linked (sec, l, info))
14214 if (flags & SEC_GROUP)
14216 asection *first = elf_next_in_group (sec);
14217 asection *s = first;
14221 s->output_section = bfd_abs_section_ptr;
14222 /* Record which group discards it. */
14223 s->kept_section = l->sec;
14224 s = elf_next_in_group (s);
14225 /* These lists are circular. */
14235 /* A single member comdat group section may be discarded by a
14236 linkonce section and vice versa. */
14237 if ((flags & SEC_GROUP) != 0)
14239 asection *first = elf_next_in_group (sec);
14241 if (first != NULL && elf_next_in_group (first) == first)
14242 /* Check this single member group against linkonce sections. */
14243 for (l = already_linked_list->entry; l != NULL; l = l->next)
14244 if ((l->sec->flags & SEC_GROUP) == 0
14245 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14247 first->output_section = bfd_abs_section_ptr;
14248 first->kept_section = l->sec;
14249 sec->output_section = bfd_abs_section_ptr;
14254 /* Check this linkonce section against single member groups. */
14255 for (l = already_linked_list->entry; l != NULL; l = l->next)
14256 if (l->sec->flags & SEC_GROUP)
14258 asection *first = elf_next_in_group (l->sec);
14261 && elf_next_in_group (first) == first
14262 && bfd_elf_match_symbols_in_sections (first, sec, info))
14264 sec->output_section = bfd_abs_section_ptr;
14265 sec->kept_section = first;
14270 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14271 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14272 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14273 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14274 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14275 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14276 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14277 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14278 The reverse order cannot happen as there is never a bfd with only the
14279 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14280 matter as here were are looking only for cross-bfd sections. */
14282 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14283 for (l = already_linked_list->entry; l != NULL; l = l->next)
14284 if ((l->sec->flags & SEC_GROUP) == 0
14285 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14287 if (abfd != l->sec->owner)
14288 sec->output_section = bfd_abs_section_ptr;
14292 /* This is the first section with this name. Record it. */
14293 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14294 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14295 return sec->output_section == bfd_abs_section_ptr;
14299 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14301 return sym->st_shndx == SHN_COMMON;
14305 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14311 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14313 return bfd_com_section_ptr;
14317 _bfd_elf_default_got_elt_size (bfd *abfd,
14318 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14319 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14320 bfd *ibfd ATTRIBUTE_UNUSED,
14321 unsigned long symndx ATTRIBUTE_UNUSED)
14323 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14324 return bed->s->arch_size / 8;
14327 /* Routines to support the creation of dynamic relocs. */
14329 /* Returns the name of the dynamic reloc section associated with SEC. */
14331 static const char *
14332 get_dynamic_reloc_section_name (bfd * abfd,
14334 bfd_boolean is_rela)
14337 const char *old_name = bfd_get_section_name (NULL, sec);
14338 const char *prefix = is_rela ? ".rela" : ".rel";
14340 if (old_name == NULL)
14343 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14344 sprintf (name, "%s%s", prefix, old_name);
14349 /* Returns the dynamic reloc section associated with SEC.
14350 If necessary compute the name of the dynamic reloc section based
14351 on SEC's name (looked up in ABFD's string table) and the setting
14355 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14357 bfd_boolean is_rela)
14359 asection * reloc_sec = elf_section_data (sec)->sreloc;
14361 if (reloc_sec == NULL)
14363 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14367 reloc_sec = bfd_get_linker_section (abfd, name);
14369 if (reloc_sec != NULL)
14370 elf_section_data (sec)->sreloc = reloc_sec;
14377 /* Returns the dynamic reloc section associated with SEC. If the
14378 section does not exist it is created and attached to the DYNOBJ
14379 bfd and stored in the SRELOC field of SEC's elf_section_data
14382 ALIGNMENT is the alignment for the newly created section and
14383 IS_RELA defines whether the name should be .rela.<SEC's name>
14384 or .rel.<SEC's name>. The section name is looked up in the
14385 string table associated with ABFD. */
14388 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14390 unsigned int alignment,
14392 bfd_boolean is_rela)
14394 asection * reloc_sec = elf_section_data (sec)->sreloc;
14396 if (reloc_sec == NULL)
14398 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14403 reloc_sec = bfd_get_linker_section (dynobj, name);
14405 if (reloc_sec == NULL)
14407 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14408 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14409 if ((sec->flags & SEC_ALLOC) != 0)
14410 flags |= SEC_ALLOC | SEC_LOAD;
14412 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14413 if (reloc_sec != NULL)
14415 /* _bfd_elf_get_sec_type_attr chooses a section type by
14416 name. Override as it may be wrong, eg. for a user
14417 section named "auto" we'll get ".relauto" which is
14418 seen to be a .rela section. */
14419 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14420 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14425 elf_section_data (sec)->sreloc = reloc_sec;
14431 /* Copy the ELF symbol type and other attributes for a linker script
14432 assignment from HSRC to HDEST. Generally this should be treated as
14433 if we found a strong non-dynamic definition for HDEST (except that
14434 ld ignores multiple definition errors). */
14436 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14437 struct bfd_link_hash_entry *hdest,
14438 struct bfd_link_hash_entry *hsrc)
14440 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14441 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14442 Elf_Internal_Sym isym;
14444 ehdest->type = ehsrc->type;
14445 ehdest->target_internal = ehsrc->target_internal;
14447 isym.st_other = ehsrc->other;
14448 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14451 /* Append a RELA relocation REL to section S in BFD. */
14454 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14456 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14457 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14458 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14459 bed->s->swap_reloca_out (abfd, rel, loc);
14462 /* Append a REL relocation REL to section S in BFD. */
14465 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14467 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14468 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14469 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14470 bed->s->swap_reloc_out (abfd, rel, loc);
14473 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14475 struct bfd_link_hash_entry *
14476 bfd_elf_define_start_stop (struct bfd_link_info *info,
14477 const char *symbol, asection *sec)
14479 struct elf_link_hash_entry *h;
14481 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14482 FALSE, FALSE, TRUE);
14484 && (h->root.type == bfd_link_hash_undefined
14485 || h->root.type == bfd_link_hash_undefweak
14486 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14488 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14489 h->root.type = bfd_link_hash_defined;
14490 h->root.u.def.section = sec;
14491 h->root.u.def.value = 0;
14492 h->def_regular = 1;
14493 h->def_dynamic = 0;
14495 h->u2.start_stop_section = sec;
14496 if (symbol[0] == '.')
14498 /* .startof. and .sizeof. symbols are local. */
14499 const struct elf_backend_data *bed;
14500 bed = get_elf_backend_data (info->output_bfd);
14501 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14505 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14506 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14508 bfd_elf_link_record_dynamic_symbol (info, h);