1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
31 #if BFD_SUPPORTS_PLUGINS
32 #include "plugin-api.h"
36 /* This struct is used to pass information to routines called via
37 elf_link_hash_traverse which must return failure. */
39 struct elf_info_failed
41 struct bfd_link_info *info;
45 /* This structure is used to pass information to
46 _bfd_elf_link_find_version_dependencies. */
48 struct elf_find_verdep_info
50 /* General link information. */
51 struct bfd_link_info *info;
52 /* The number of dependencies. */
54 /* Whether we had a failure. */
58 static bfd_boolean _bfd_elf_fix_symbol_flags
59 (struct elf_link_hash_entry *, struct elf_info_failed *);
62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
63 unsigned long r_symndx,
66 if (r_symndx >= cookie->locsymcount
67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
69 struct elf_link_hash_entry *h;
71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
73 while (h->root.type == bfd_link_hash_indirect
74 || h->root.type == bfd_link_hash_warning)
75 h = (struct elf_link_hash_entry *) h->root.u.i.link;
77 if ((h->root.type == bfd_link_hash_defined
78 || h->root.type == bfd_link_hash_defweak)
79 && discarded_section (h->root.u.def.section))
80 return h->root.u.def.section;
86 /* It's not a relocation against a global symbol,
87 but it could be a relocation against a local
88 symbol for a discarded section. */
90 Elf_Internal_Sym *isym;
92 /* Need to: get the symbol; get the section. */
93 isym = &cookie->locsyms[r_symndx];
94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
96 && discard ? discarded_section (isec) : 1)
102 /* Define a symbol in a dynamic linkage section. */
104 struct elf_link_hash_entry *
105 _bfd_elf_define_linkage_sym (bfd *abfd,
106 struct bfd_link_info *info,
110 struct elf_link_hash_entry *h;
111 struct bfd_link_hash_entry *bh;
112 const struct elf_backend_data *bed;
114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
117 /* Zap symbol defined in an as-needed lib that wasn't linked.
118 This is a symptom of a larger problem: Absolute symbols
119 defined in shared libraries can't be overridden, because we
120 lose the link to the bfd which is via the symbol section. */
121 h->root.type = bfd_link_hash_new;
127 bed = get_elf_backend_data (abfd);
128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
129 sec, 0, NULL, FALSE, bed->collect,
132 h = (struct elf_link_hash_entry *) bh;
133 BFD_ASSERT (h != NULL);
136 h->root.linker_def = 1;
137 h->type = STT_OBJECT;
138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
141 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
150 struct elf_link_hash_entry *h;
151 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
152 struct elf_link_hash_table *htab = elf_hash_table (info);
154 /* This function may be called more than once. */
155 if (htab->sgot != NULL)
158 flags = bed->dynamic_sec_flags;
160 s = bfd_make_section_anyway_with_flags (abfd,
161 (bed->rela_plts_and_copies_p
162 ? ".rela.got" : ".rel.got"),
163 (bed->dynamic_sec_flags
166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
176 if (bed->want_got_plt)
178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
180 || !bfd_set_section_alignment (abfd, s,
181 bed->s->log_file_align))
186 /* The first bit of the global offset table is the header. */
187 s->size += bed->got_header_size;
189 if (bed->want_got_sym)
191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
192 (or .got.plt) section. We don't do this in the linker script
193 because we don't want to define the symbol if we are not creating
194 a global offset table. */
195 h = _bfd_elf_define_linkage_sym (abfd, info, s,
196 "_GLOBAL_OFFSET_TABLE_");
197 elf_hash_table (info)->hgot = h;
205 /* Create a strtab to hold the dynamic symbol names. */
207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
209 struct elf_link_hash_table *hash_table;
211 hash_table = elf_hash_table (info);
212 if (hash_table->dynobj == NULL)
214 /* We may not set dynobj, an input file holding linker created
215 dynamic sections to abfd, which may be a dynamic object with
216 its own dynamic sections. We need to find a normal input file
217 to hold linker created sections if possible. */
218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
226 && !((s = ibfd->sections) != NULL
227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
233 hash_table->dynobj = abfd;
236 if (hash_table->dynstr == NULL)
238 hash_table->dynstr = _bfd_elf_strtab_init ();
239 if (hash_table->dynstr == NULL)
245 /* Create some sections which will be filled in with dynamic linking
246 information. ABFD is an input file which requires dynamic sections
247 to be created. The dynamic sections take up virtual memory space
248 when the final executable is run, so we need to create them before
249 addresses are assigned to the output sections. We work out the
250 actual contents and size of these sections later. */
253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
257 const struct elf_backend_data *bed;
258 struct elf_link_hash_entry *h;
260 if (! is_elf_hash_table (info->hash))
263 if (elf_hash_table (info)->dynamic_sections_created)
266 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
269 abfd = elf_hash_table (info)->dynobj;
270 bed = get_elf_backend_data (abfd);
272 flags = bed->dynamic_sec_flags;
274 /* A dynamically linked executable has a .interp section, but a
275 shared library does not. */
276 if (bfd_link_executable (info) && !info->nointerp)
278 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
279 flags | SEC_READONLY);
284 /* Create sections to hold version informations. These are removed
285 if they are not needed. */
286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
287 flags | SEC_READONLY);
289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
293 flags | SEC_READONLY);
295 || ! bfd_set_section_alignment (abfd, s, 1))
298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
299 flags | SEC_READONLY);
301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
305 flags | SEC_READONLY);
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
309 elf_hash_table (info)->dynsym = s;
311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
312 flags | SEC_READONLY);
316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 /* The special symbol _DYNAMIC is always set to the start of the
322 .dynamic section. We could set _DYNAMIC in a linker script, but we
323 only want to define it if we are, in fact, creating a .dynamic
324 section. We don't want to define it if there is no .dynamic
325 section, since on some ELF platforms the start up code examines it
326 to decide how to initialize the process. */
327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
328 elf_hash_table (info)->hdynamic = h;
334 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
335 flags | SEC_READONLY);
337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
342 if (info->emit_gnu_hash)
344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
345 flags | SEC_READONLY);
347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
350 4 32-bit words followed by variable count of 64-bit words, then
351 variable count of 32-bit words. */
352 if (bed->s->arch_size == 64)
353 elf_section_data (s)->this_hdr.sh_entsize = 0;
355 elf_section_data (s)->this_hdr.sh_entsize = 4;
358 /* Let the backend create the rest of the sections. This lets the
359 backend set the right flags. The backend will normally create
360 the .got and .plt sections. */
361 if (bed->elf_backend_create_dynamic_sections == NULL
362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
365 elf_hash_table (info)->dynamic_sections_created = TRUE;
370 /* Create dynamic sections when linking against a dynamic object. */
373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
375 flagword flags, pltflags;
376 struct elf_link_hash_entry *h;
378 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
379 struct elf_link_hash_table *htab = elf_hash_table (info);
381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
382 .rel[a].bss sections. */
383 flags = bed->dynamic_sec_flags;
386 if (bed->plt_not_loaded)
387 /* We do not clear SEC_ALLOC here because we still want the OS to
388 allocate space for the section; it's just that there's nothing
389 to read in from the object file. */
390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
393 if (bed->plt_readonly)
394 pltflags |= SEC_READONLY;
396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
404 if (bed->want_plt_sym)
406 h = _bfd_elf_define_linkage_sym (abfd, info, s,
407 "_PROCEDURE_LINKAGE_TABLE_");
408 elf_hash_table (info)->hplt = h;
413 s = bfd_make_section_anyway_with_flags (abfd,
414 (bed->rela_plts_and_copies_p
415 ? ".rela.plt" : ".rel.plt"),
416 flags | SEC_READONLY);
418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
422 if (! _bfd_elf_create_got_section (abfd, info))
425 if (bed->want_dynbss)
427 /* The .dynbss section is a place to put symbols which are defined
428 by dynamic objects, are referenced by regular objects, and are
429 not functions. We must allocate space for them in the process
430 image and use a R_*_COPY reloc to tell the dynamic linker to
431 initialize them at run time. The linker script puts the .dynbss
432 section into the .bss section of the final image. */
433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
434 SEC_ALLOC | SEC_LINKER_CREATED);
439 if (bed->want_dynrelro)
441 /* Similarly, but for symbols that were originally in read-only
442 sections. This section doesn't really need to have contents,
443 but make it like other .data.rel.ro sections. */
444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
451 /* The .rel[a].bss section holds copy relocs. This section is not
452 normally needed. We need to create it here, though, so that the
453 linker will map it to an output section. We can't just create it
454 only if we need it, because we will not know whether we need it
455 until we have seen all the input files, and the first time the
456 main linker code calls BFD after examining all the input files
457 (size_dynamic_sections) the input sections have already been
458 mapped to the output sections. If the section turns out not to
459 be needed, we can discard it later. We will never need this
460 section when generating a shared object, since they do not use
462 if (bfd_link_executable (info))
464 s = bfd_make_section_anyway_with_flags (abfd,
465 (bed->rela_plts_and_copies_p
466 ? ".rela.bss" : ".rel.bss"),
467 flags | SEC_READONLY);
469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
473 if (bed->want_dynrelro)
475 s = (bfd_make_section_anyway_with_flags
476 (abfd, (bed->rela_plts_and_copies_p
477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
478 flags | SEC_READONLY));
480 || ! bfd_set_section_alignment (abfd, s,
481 bed->s->log_file_align))
483 htab->sreldynrelro = s;
491 /* Record a new dynamic symbol. We record the dynamic symbols as we
492 read the input files, since we need to have a list of all of them
493 before we can determine the final sizes of the output sections.
494 Note that we may actually call this function even though we are not
495 going to output any dynamic symbols; in some cases we know that a
496 symbol should be in the dynamic symbol table, but only if there is
500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
501 struct elf_link_hash_entry *h)
503 if (h->dynindx == -1)
505 struct elf_strtab_hash *dynstr;
510 /* XXX: The ABI draft says the linker must turn hidden and
511 internal symbols into STB_LOCAL symbols when producing the
512 DSO. However, if ld.so honors st_other in the dynamic table,
513 this would not be necessary. */
514 switch (ELF_ST_VISIBILITY (h->other))
518 if (h->root.type != bfd_link_hash_undefined
519 && h->root.type != bfd_link_hash_undefweak)
522 if (!elf_hash_table (info)->is_relocatable_executable)
530 h->dynindx = elf_hash_table (info)->dynsymcount;
531 ++elf_hash_table (info)->dynsymcount;
533 dynstr = elf_hash_table (info)->dynstr;
536 /* Create a strtab to hold the dynamic symbol names. */
537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
542 /* We don't put any version information in the dynamic string
544 name = h->root.root.string;
545 p = strchr (name, ELF_VER_CHR);
547 /* We know that the p points into writable memory. In fact,
548 there are only a few symbols that have read-only names, being
549 those like _GLOBAL_OFFSET_TABLE_ that are created specially
550 by the backends. Most symbols will have names pointing into
551 an ELF string table read from a file, or to objalloc memory. */
554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
559 if (indx == (size_t) -1)
561 h->dynstr_index = indx;
567 /* Mark a symbol dynamic. */
570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
571 struct elf_link_hash_entry *h,
572 Elf_Internal_Sym *sym)
574 struct bfd_elf_dynamic_list *d = info->dynamic_list;
576 /* It may be called more than once on the same H. */
577 if(h->dynamic || bfd_link_relocatable (info))
580 if ((info->dynamic_data
581 && (h->type == STT_OBJECT
582 || h->type == STT_COMMON
584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
588 && (*d->match) (&d->head, NULL, h->root.root.string)))
592 /* Record an assignment to a symbol made by a linker script. We need
593 this in case some dynamic object refers to this symbol. */
596 bfd_elf_record_link_assignment (bfd *output_bfd,
597 struct bfd_link_info *info,
602 struct elf_link_hash_entry *h, *hv;
603 struct elf_link_hash_table *htab;
604 const struct elf_backend_data *bed;
606 if (!is_elf_hash_table (info->hash))
609 htab = elf_hash_table (info);
610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
614 if (h->root.type == bfd_link_hash_warning)
615 h = (struct elf_link_hash_entry *) h->root.u.i.link;
617 if (h->versioned == unknown)
619 /* Set versioned if symbol version is unknown. */
620 char *version = strrchr (name, ELF_VER_CHR);
623 if (version > name && version[-1] != ELF_VER_CHR)
624 h->versioned = versioned_hidden;
626 h->versioned = versioned;
630 /* Symbols defined in a linker script but not referenced anywhere
631 else will have non_elf set. */
634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
638 switch (h->root.type)
640 case bfd_link_hash_defined:
641 case bfd_link_hash_defweak:
642 case bfd_link_hash_common:
644 case bfd_link_hash_undefweak:
645 case bfd_link_hash_undefined:
646 /* Since we're defining the symbol, don't let it seem to have not
647 been defined. record_dynamic_symbol and size_dynamic_sections
648 may depend on this. */
649 h->root.type = bfd_link_hash_new;
650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
651 bfd_link_repair_undef_list (&htab->root);
653 case bfd_link_hash_new:
655 case bfd_link_hash_indirect:
656 /* We had a versioned symbol in a dynamic library. We make the
657 the versioned symbol point to this one. */
658 bed = get_elf_backend_data (output_bfd);
660 while (hv->root.type == bfd_link_hash_indirect
661 || hv->root.type == bfd_link_hash_warning)
662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
663 /* We don't need to update h->root.u since linker will set them
665 h->root.type = bfd_link_hash_undefined;
666 hv->root.type = bfd_link_hash_indirect;
667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
675 /* If this symbol is being provided by the linker script, and it is
676 currently defined by a dynamic object, but not by a regular
677 object, then mark it as undefined so that the generic linker will
678 force the correct value. */
682 h->root.type = bfd_link_hash_undefined;
684 /* If this symbol is not being provided by the linker script, and it is
685 currently defined by a dynamic object, but not by a regular object,
686 then clear out any version information because the symbol will not be
687 associated with the dynamic object any more. */
691 h->verinfo.verdef = NULL;
693 /* Make sure this symbol is not garbage collected. */
700 bed = get_elf_backend_data (output_bfd);
701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
703 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
708 if (!bfd_link_relocatable (info)
710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
716 || bfd_link_dll (info)
717 || elf_hash_table (info)->is_relocatable_executable)
720 if (! bfd_elf_link_record_dynamic_symbol (info, h))
723 /* If this is a weak defined symbol, and we know a corresponding
724 real symbol from the same dynamic object, make sure the real
725 symbol is also made into a dynamic symbol. */
726 if (h->u.weakdef != NULL
727 && h->u.weakdef->dynindx == -1)
729 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
737 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
738 success, and 2 on a failure caused by attempting to record a symbol
739 in a discarded section, eg. a discarded link-once section symbol. */
742 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
747 struct elf_link_local_dynamic_entry *entry;
748 struct elf_link_hash_table *eht;
749 struct elf_strtab_hash *dynstr;
752 Elf_External_Sym_Shndx eshndx;
753 char esym[sizeof (Elf64_External_Sym)];
755 if (! is_elf_hash_table (info->hash))
758 /* See if the entry exists already. */
759 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
760 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
763 amt = sizeof (*entry);
764 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
768 /* Go find the symbol, so that we can find it's name. */
769 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
770 1, input_indx, &entry->isym, esym, &eshndx))
772 bfd_release (input_bfd, entry);
776 if (entry->isym.st_shndx != SHN_UNDEF
777 && entry->isym.st_shndx < SHN_LORESERVE)
781 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
782 if (s == NULL || bfd_is_abs_section (s->output_section))
784 /* We can still bfd_release here as nothing has done another
785 bfd_alloc. We can't do this later in this function. */
786 bfd_release (input_bfd, entry);
791 name = (bfd_elf_string_from_elf_section
792 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
793 entry->isym.st_name));
795 dynstr = elf_hash_table (info)->dynstr;
798 /* Create a strtab to hold the dynamic symbol names. */
799 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
804 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
805 if (dynstr_index == (size_t) -1)
807 entry->isym.st_name = dynstr_index;
809 eht = elf_hash_table (info);
811 entry->next = eht->dynlocal;
812 eht->dynlocal = entry;
813 entry->input_bfd = input_bfd;
814 entry->input_indx = input_indx;
817 /* Whatever binding the symbol had before, it's now local. */
819 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
821 /* The dynindx will be set at the end of size_dynamic_sections. */
826 /* Return the dynindex of a local dynamic symbol. */
829 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
833 struct elf_link_local_dynamic_entry *e;
835 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
836 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
841 /* This function is used to renumber the dynamic symbols, if some of
842 them are removed because they are marked as local. This is called
843 via elf_link_hash_traverse. */
846 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
849 size_t *count = (size_t *) data;
854 if (h->dynindx != -1)
855 h->dynindx = ++(*count);
861 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
862 STB_LOCAL binding. */
865 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
868 size_t *count = (size_t *) data;
870 if (!h->forced_local)
873 if (h->dynindx != -1)
874 h->dynindx = ++(*count);
879 /* Return true if the dynamic symbol for a given section should be
880 omitted when creating a shared library. */
882 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
883 struct bfd_link_info *info,
886 struct elf_link_hash_table *htab;
889 switch (elf_section_data (p)->this_hdr.sh_type)
893 /* If sh_type is yet undecided, assume it could be
894 SHT_PROGBITS/SHT_NOBITS. */
896 htab = elf_hash_table (info);
897 if (p == htab->tls_sec)
900 if (htab->text_index_section != NULL)
901 return p != htab->text_index_section && p != htab->data_index_section;
903 return (htab->dynobj != NULL
904 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
905 && ip->output_section == p);
907 /* There shouldn't be section relative relocations
908 against any other section. */
914 /* Assign dynsym indices. In a shared library we generate a section
915 symbol for each output section, which come first. Next come symbols
916 which have been forced to local binding. Then all of the back-end
917 allocated local dynamic syms, followed by the rest of the global
921 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
922 struct bfd_link_info *info,
923 unsigned long *section_sym_count)
925 unsigned long dynsymcount = 0;
927 if (bfd_link_pic (info)
928 || elf_hash_table (info)->is_relocatable_executable)
930 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
932 for (p = output_bfd->sections; p ; p = p->next)
933 if ((p->flags & SEC_EXCLUDE) == 0
934 && (p->flags & SEC_ALLOC) != 0
935 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
936 elf_section_data (p)->dynindx = ++dynsymcount;
938 elf_section_data (p)->dynindx = 0;
940 *section_sym_count = dynsymcount;
942 elf_link_hash_traverse (elf_hash_table (info),
943 elf_link_renumber_local_hash_table_dynsyms,
946 if (elf_hash_table (info)->dynlocal)
948 struct elf_link_local_dynamic_entry *p;
949 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
950 p->dynindx = ++dynsymcount;
952 elf_hash_table (info)->local_dynsymcount = dynsymcount;
954 elf_link_hash_traverse (elf_hash_table (info),
955 elf_link_renumber_hash_table_dynsyms,
958 /* There is an unused NULL entry at the head of the table which we
959 must account for in our count even if the table is empty since it
960 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
964 elf_hash_table (info)->dynsymcount = dynsymcount;
968 /* Merge st_other field. */
971 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
972 const Elf_Internal_Sym *isym, asection *sec,
973 bfd_boolean definition, bfd_boolean dynamic)
975 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
977 /* If st_other has a processor-specific meaning, specific
978 code might be needed here. */
979 if (bed->elf_backend_merge_symbol_attribute)
980 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
985 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
986 unsigned hvis = ELF_ST_VISIBILITY (h->other);
988 /* Keep the most constraining visibility. Leave the remainder
989 of the st_other field to elf_backend_merge_symbol_attribute. */
990 if (symvis - 1 < hvis - 1)
991 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
994 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
995 && (sec->flags & SEC_READONLY) == 0)
996 h->protected_def = 1;
999 /* This function is called when we want to merge a new symbol with an
1000 existing symbol. It handles the various cases which arise when we
1001 find a definition in a dynamic object, or when there is already a
1002 definition in a dynamic object. The new symbol is described by
1003 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1004 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1005 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1006 of an old common symbol. We set OVERRIDE if the old symbol is
1007 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1008 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1009 to change. By OK to change, we mean that we shouldn't warn if the
1010 type or size does change. */
1013 _bfd_elf_merge_symbol (bfd *abfd,
1014 struct bfd_link_info *info,
1016 Elf_Internal_Sym *sym,
1019 struct elf_link_hash_entry **sym_hash,
1021 bfd_boolean *pold_weak,
1022 unsigned int *pold_alignment,
1024 bfd_boolean *override,
1025 bfd_boolean *type_change_ok,
1026 bfd_boolean *size_change_ok,
1027 bfd_boolean *matched)
1029 asection *sec, *oldsec;
1030 struct elf_link_hash_entry *h;
1031 struct elf_link_hash_entry *hi;
1032 struct elf_link_hash_entry *flip;
1035 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1036 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1037 const struct elf_backend_data *bed;
1044 bind = ELF_ST_BIND (sym->st_info);
1046 if (! bfd_is_und_section (sec))
1047 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1049 h = ((struct elf_link_hash_entry *)
1050 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1055 bed = get_elf_backend_data (abfd);
1057 /* NEW_VERSION is the symbol version of the new symbol. */
1058 if (h->versioned != unversioned)
1060 /* Symbol version is unknown or versioned. */
1061 new_version = strrchr (name, ELF_VER_CHR);
1064 if (h->versioned == unknown)
1066 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1067 h->versioned = versioned_hidden;
1069 h->versioned = versioned;
1072 if (new_version[0] == '\0')
1076 h->versioned = unversioned;
1081 /* For merging, we only care about real symbols. But we need to make
1082 sure that indirect symbol dynamic flags are updated. */
1084 while (h->root.type == bfd_link_hash_indirect
1085 || h->root.type == bfd_link_hash_warning)
1086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1090 if (hi == h || h->root.type == bfd_link_hash_new)
1094 /* OLD_HIDDEN is true if the existing symbol is only visible
1095 to the symbol with the same symbol version. NEW_HIDDEN is
1096 true if the new symbol is only visible to the symbol with
1097 the same symbol version. */
1098 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1099 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1100 if (!old_hidden && !new_hidden)
1101 /* The new symbol matches the existing symbol if both
1106 /* OLD_VERSION is the symbol version of the existing
1110 if (h->versioned >= versioned)
1111 old_version = strrchr (h->root.root.string,
1116 /* The new symbol matches the existing symbol if they
1117 have the same symbol version. */
1118 *matched = (old_version == new_version
1119 || (old_version != NULL
1120 && new_version != NULL
1121 && strcmp (old_version, new_version) == 0));
1126 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1131 switch (h->root.type)
1136 case bfd_link_hash_undefined:
1137 case bfd_link_hash_undefweak:
1138 oldbfd = h->root.u.undef.abfd;
1141 case bfd_link_hash_defined:
1142 case bfd_link_hash_defweak:
1143 oldbfd = h->root.u.def.section->owner;
1144 oldsec = h->root.u.def.section;
1147 case bfd_link_hash_common:
1148 oldbfd = h->root.u.c.p->section->owner;
1149 oldsec = h->root.u.c.p->section;
1151 *pold_alignment = h->root.u.c.p->alignment_power;
1154 if (poldbfd && *poldbfd == NULL)
1157 /* Differentiate strong and weak symbols. */
1158 newweak = bind == STB_WEAK;
1159 oldweak = (h->root.type == bfd_link_hash_defweak
1160 || h->root.type == bfd_link_hash_undefweak);
1162 *pold_weak = oldweak;
1164 /* This code is for coping with dynamic objects, and is only useful
1165 if we are doing an ELF link. */
1166 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1169 /* We have to check it for every instance since the first few may be
1170 references and not all compilers emit symbol type for undefined
1172 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1174 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1175 respectively, is from a dynamic object. */
1177 newdyn = (abfd->flags & DYNAMIC) != 0;
1179 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1180 syms and defined syms in dynamic libraries respectively.
1181 ref_dynamic on the other hand can be set for a symbol defined in
1182 a dynamic library, and def_dynamic may not be set; When the
1183 definition in a dynamic lib is overridden by a definition in the
1184 executable use of the symbol in the dynamic lib becomes a
1185 reference to the executable symbol. */
1188 if (bfd_is_und_section (sec))
1190 if (bind != STB_WEAK)
1192 h->ref_dynamic_nonweak = 1;
1193 hi->ref_dynamic_nonweak = 1;
1198 /* Update the existing symbol only if they match. */
1201 hi->dynamic_def = 1;
1205 /* If we just created the symbol, mark it as being an ELF symbol.
1206 Other than that, there is nothing to do--there is no merge issue
1207 with a newly defined symbol--so we just return. */
1209 if (h->root.type == bfd_link_hash_new)
1215 /* In cases involving weak versioned symbols, we may wind up trying
1216 to merge a symbol with itself. Catch that here, to avoid the
1217 confusion that results if we try to override a symbol with
1218 itself. The additional tests catch cases like
1219 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1220 dynamic object, which we do want to handle here. */
1222 && (newweak || oldweak)
1223 && ((abfd->flags & DYNAMIC) == 0
1224 || !h->def_regular))
1229 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1230 else if (oldsec != NULL)
1232 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1233 indices used by MIPS ELF. */
1234 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1237 /* Handle a case where plugin_notice won't be called and thus won't
1238 set the non_ir_ref flags on the first pass over symbols. */
1240 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1241 && newdyn != olddyn)
1243 h->root.non_ir_ref_dynamic = TRUE;
1244 hi->root.non_ir_ref_dynamic = TRUE;
1247 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1248 respectively, appear to be a definition rather than reference. */
1250 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1252 olddef = (h->root.type != bfd_link_hash_undefined
1253 && h->root.type != bfd_link_hash_undefweak
1254 && h->root.type != bfd_link_hash_common);
1256 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1257 respectively, appear to be a function. */
1259 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1260 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1262 oldfunc = (h->type != STT_NOTYPE
1263 && bed->is_function_type (h->type));
1265 if (!(newfunc && oldfunc)
1266 && ELF_ST_TYPE (sym->st_info) != h->type
1267 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1268 && h->type != STT_NOTYPE
1269 && (newdef || bfd_is_com_section (sec))
1270 && (olddef || h->root.type == bfd_link_hash_common))
1272 /* If creating a default indirect symbol ("foo" or "foo@") from
1273 a dynamic versioned definition ("foo@@") skip doing so if
1274 there is an existing regular definition with a different
1275 type. We don't want, for example, a "time" variable in the
1276 executable overriding a "time" function in a shared library. */
1284 /* When adding a symbol from a regular object file after we have
1285 created indirect symbols, undo the indirection and any
1292 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1293 h->forced_local = 0;
1297 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1299 h->root.type = bfd_link_hash_undefined;
1300 h->root.u.undef.abfd = abfd;
1304 h->root.type = bfd_link_hash_new;
1305 h->root.u.undef.abfd = NULL;
1311 /* Check TLS symbols. We don't check undefined symbols introduced
1312 by "ld -u" which have no type (and oldbfd NULL), and we don't
1313 check symbols from plugins because they also have no type. */
1315 && (oldbfd->flags & BFD_PLUGIN) == 0
1316 && (abfd->flags & BFD_PLUGIN) == 0
1317 && ELF_ST_TYPE (sym->st_info) != h->type
1318 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1321 bfd_boolean ntdef, tdef;
1322 asection *ntsec, *tsec;
1324 if (h->type == STT_TLS)
1345 /* xgettext:c-format */
1346 (_("%s: TLS definition in %B section %A "
1347 "mismatches non-TLS definition in %B section %A"),
1348 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1349 else if (!tdef && !ntdef)
1351 /* xgettext:c-format */
1352 (_("%s: TLS reference in %B "
1353 "mismatches non-TLS reference in %B"),
1354 h->root.root.string, tbfd, ntbfd);
1357 /* xgettext:c-format */
1358 (_("%s: TLS definition in %B section %A "
1359 "mismatches non-TLS reference in %B"),
1360 h->root.root.string, tbfd, tsec, ntbfd);
1363 /* xgettext:c-format */
1364 (_("%s: TLS reference in %B "
1365 "mismatches non-TLS definition in %B section %A"),
1366 h->root.root.string, tbfd, ntbfd, ntsec);
1368 bfd_set_error (bfd_error_bad_value);
1372 /* If the old symbol has non-default visibility, we ignore the new
1373 definition from a dynamic object. */
1375 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1376 && !bfd_is_und_section (sec))
1379 /* Make sure this symbol is dynamic. */
1381 hi->ref_dynamic = 1;
1382 /* A protected symbol has external availability. Make sure it is
1383 recorded as dynamic.
1385 FIXME: Should we check type and size for protected symbol? */
1386 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1387 return bfd_elf_link_record_dynamic_symbol (info, h);
1392 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1395 /* If the new symbol with non-default visibility comes from a
1396 relocatable file and the old definition comes from a dynamic
1397 object, we remove the old definition. */
1398 if (hi->root.type == bfd_link_hash_indirect)
1400 /* Handle the case where the old dynamic definition is
1401 default versioned. We need to copy the symbol info from
1402 the symbol with default version to the normal one if it
1403 was referenced before. */
1406 hi->root.type = h->root.type;
1407 h->root.type = bfd_link_hash_indirect;
1408 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1410 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1411 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1413 /* If the new symbol is hidden or internal, completely undo
1414 any dynamic link state. */
1415 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1416 h->forced_local = 0;
1423 /* FIXME: Should we check type and size for protected symbol? */
1433 /* If the old symbol was undefined before, then it will still be
1434 on the undefs list. If the new symbol is undefined or
1435 common, we can't make it bfd_link_hash_new here, because new
1436 undefined or common symbols will be added to the undefs list
1437 by _bfd_generic_link_add_one_symbol. Symbols may not be
1438 added twice to the undefs list. Also, if the new symbol is
1439 undefweak then we don't want to lose the strong undef. */
1440 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1442 h->root.type = bfd_link_hash_undefined;
1443 h->root.u.undef.abfd = abfd;
1447 h->root.type = bfd_link_hash_new;
1448 h->root.u.undef.abfd = NULL;
1451 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1453 /* If the new symbol is hidden or internal, completely undo
1454 any dynamic link state. */
1455 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1456 h->forced_local = 0;
1462 /* FIXME: Should we check type and size for protected symbol? */
1468 /* If a new weak symbol definition comes from a regular file and the
1469 old symbol comes from a dynamic library, we treat the new one as
1470 strong. Similarly, an old weak symbol definition from a regular
1471 file is treated as strong when the new symbol comes from a dynamic
1472 library. Further, an old weak symbol from a dynamic library is
1473 treated as strong if the new symbol is from a dynamic library.
1474 This reflects the way glibc's ld.so works.
1476 Do this before setting *type_change_ok or *size_change_ok so that
1477 we warn properly when dynamic library symbols are overridden. */
1479 if (newdef && !newdyn && olddyn)
1481 if (olddef && newdyn)
1484 /* Allow changes between different types of function symbol. */
1485 if (newfunc && oldfunc)
1486 *type_change_ok = TRUE;
1488 /* It's OK to change the type if either the existing symbol or the
1489 new symbol is weak. A type change is also OK if the old symbol
1490 is undefined and the new symbol is defined. */
1495 && h->root.type == bfd_link_hash_undefined))
1496 *type_change_ok = TRUE;
1498 /* It's OK to change the size if either the existing symbol or the
1499 new symbol is weak, or if the old symbol is undefined. */
1502 || h->root.type == bfd_link_hash_undefined)
1503 *size_change_ok = TRUE;
1505 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1506 symbol, respectively, appears to be a common symbol in a dynamic
1507 object. If a symbol appears in an uninitialized section, and is
1508 not weak, and is not a function, then it may be a common symbol
1509 which was resolved when the dynamic object was created. We want
1510 to treat such symbols specially, because they raise special
1511 considerations when setting the symbol size: if the symbol
1512 appears as a common symbol in a regular object, and the size in
1513 the regular object is larger, we must make sure that we use the
1514 larger size. This problematic case can always be avoided in C,
1515 but it must be handled correctly when using Fortran shared
1518 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1519 likewise for OLDDYNCOMMON and OLDDEF.
1521 Note that this test is just a heuristic, and that it is quite
1522 possible to have an uninitialized symbol in a shared object which
1523 is really a definition, rather than a common symbol. This could
1524 lead to some minor confusion when the symbol really is a common
1525 symbol in some regular object. However, I think it will be
1531 && (sec->flags & SEC_ALLOC) != 0
1532 && (sec->flags & SEC_LOAD) == 0
1535 newdyncommon = TRUE;
1537 newdyncommon = FALSE;
1541 && h->root.type == bfd_link_hash_defined
1543 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1544 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1547 olddyncommon = TRUE;
1549 olddyncommon = FALSE;
1551 /* We now know everything about the old and new symbols. We ask the
1552 backend to check if we can merge them. */
1553 if (bed->merge_symbol != NULL)
1555 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1560 /* If both the old and the new symbols look like common symbols in a
1561 dynamic object, set the size of the symbol to the larger of the
1566 && sym->st_size != h->size)
1568 /* Since we think we have two common symbols, issue a multiple
1569 common warning if desired. Note that we only warn if the
1570 size is different. If the size is the same, we simply let
1571 the old symbol override the new one as normally happens with
1572 symbols defined in dynamic objects. */
1574 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1575 bfd_link_hash_common, sym->st_size);
1576 if (sym->st_size > h->size)
1577 h->size = sym->st_size;
1579 *size_change_ok = TRUE;
1582 /* If we are looking at a dynamic object, and we have found a
1583 definition, we need to see if the symbol was already defined by
1584 some other object. If so, we want to use the existing
1585 definition, and we do not want to report a multiple symbol
1586 definition error; we do this by clobbering *PSEC to be
1587 bfd_und_section_ptr.
1589 We treat a common symbol as a definition if the symbol in the
1590 shared library is a function, since common symbols always
1591 represent variables; this can cause confusion in principle, but
1592 any such confusion would seem to indicate an erroneous program or
1593 shared library. We also permit a common symbol in a regular
1594 object to override a weak symbol in a shared object. */
1599 || (h->root.type == bfd_link_hash_common
1600 && (newweak || newfunc))))
1604 newdyncommon = FALSE;
1606 *psec = sec = bfd_und_section_ptr;
1607 *size_change_ok = TRUE;
1609 /* If we get here when the old symbol is a common symbol, then
1610 we are explicitly letting it override a weak symbol or
1611 function in a dynamic object, and we don't want to warn about
1612 a type change. If the old symbol is a defined symbol, a type
1613 change warning may still be appropriate. */
1615 if (h->root.type == bfd_link_hash_common)
1616 *type_change_ok = TRUE;
1619 /* Handle the special case of an old common symbol merging with a
1620 new symbol which looks like a common symbol in a shared object.
1621 We change *PSEC and *PVALUE to make the new symbol look like a
1622 common symbol, and let _bfd_generic_link_add_one_symbol do the
1626 && h->root.type == bfd_link_hash_common)
1630 newdyncommon = FALSE;
1631 *pvalue = sym->st_size;
1632 *psec = sec = bed->common_section (oldsec);
1633 *size_change_ok = TRUE;
1636 /* Skip weak definitions of symbols that are already defined. */
1637 if (newdef && olddef && newweak)
1639 /* Don't skip new non-IR weak syms. */
1640 if (!(oldbfd != NULL
1641 && (oldbfd->flags & BFD_PLUGIN) != 0
1642 && (abfd->flags & BFD_PLUGIN) == 0))
1648 /* Merge st_other. If the symbol already has a dynamic index,
1649 but visibility says it should not be visible, turn it into a
1651 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1652 if (h->dynindx != -1)
1653 switch (ELF_ST_VISIBILITY (h->other))
1657 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1662 /* If the old symbol is from a dynamic object, and the new symbol is
1663 a definition which is not from a dynamic object, then the new
1664 symbol overrides the old symbol. Symbols from regular files
1665 always take precedence over symbols from dynamic objects, even if
1666 they are defined after the dynamic object in the link.
1668 As above, we again permit a common symbol in a regular object to
1669 override a definition in a shared object if the shared object
1670 symbol is a function or is weak. */
1675 || (bfd_is_com_section (sec)
1676 && (oldweak || oldfunc)))
1681 /* Change the hash table entry to undefined, and let
1682 _bfd_generic_link_add_one_symbol do the right thing with the
1685 h->root.type = bfd_link_hash_undefined;
1686 h->root.u.undef.abfd = h->root.u.def.section->owner;
1687 *size_change_ok = TRUE;
1690 olddyncommon = FALSE;
1692 /* We again permit a type change when a common symbol may be
1693 overriding a function. */
1695 if (bfd_is_com_section (sec))
1699 /* If a common symbol overrides a function, make sure
1700 that it isn't defined dynamically nor has type
1703 h->type = STT_NOTYPE;
1705 *type_change_ok = TRUE;
1708 if (hi->root.type == bfd_link_hash_indirect)
1711 /* This union may have been set to be non-NULL when this symbol
1712 was seen in a dynamic object. We must force the union to be
1713 NULL, so that it is correct for a regular symbol. */
1714 h->verinfo.vertree = NULL;
1717 /* Handle the special case of a new common symbol merging with an
1718 old symbol that looks like it might be a common symbol defined in
1719 a shared object. Note that we have already handled the case in
1720 which a new common symbol should simply override the definition
1721 in the shared library. */
1724 && bfd_is_com_section (sec)
1727 /* It would be best if we could set the hash table entry to a
1728 common symbol, but we don't know what to use for the section
1729 or the alignment. */
1730 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1731 bfd_link_hash_common, sym->st_size);
1733 /* If the presumed common symbol in the dynamic object is
1734 larger, pretend that the new symbol has its size. */
1736 if (h->size > *pvalue)
1739 /* We need to remember the alignment required by the symbol
1740 in the dynamic object. */
1741 BFD_ASSERT (pold_alignment);
1742 *pold_alignment = h->root.u.def.section->alignment_power;
1745 olddyncommon = FALSE;
1747 h->root.type = bfd_link_hash_undefined;
1748 h->root.u.undef.abfd = h->root.u.def.section->owner;
1750 *size_change_ok = TRUE;
1751 *type_change_ok = TRUE;
1753 if (hi->root.type == bfd_link_hash_indirect)
1756 h->verinfo.vertree = NULL;
1761 /* Handle the case where we had a versioned symbol in a dynamic
1762 library and now find a definition in a normal object. In this
1763 case, we make the versioned symbol point to the normal one. */
1764 flip->root.type = h->root.type;
1765 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1766 h->root.type = bfd_link_hash_indirect;
1767 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1768 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1772 flip->ref_dynamic = 1;
1779 /* This function is called to create an indirect symbol from the
1780 default for the symbol with the default version if needed. The
1781 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1782 set DYNSYM if the new indirect symbol is dynamic. */
1785 _bfd_elf_add_default_symbol (bfd *abfd,
1786 struct bfd_link_info *info,
1787 struct elf_link_hash_entry *h,
1789 Elf_Internal_Sym *sym,
1793 bfd_boolean *dynsym)
1795 bfd_boolean type_change_ok;
1796 bfd_boolean size_change_ok;
1799 struct elf_link_hash_entry *hi;
1800 struct bfd_link_hash_entry *bh;
1801 const struct elf_backend_data *bed;
1802 bfd_boolean collect;
1803 bfd_boolean dynamic;
1804 bfd_boolean override;
1806 size_t len, shortlen;
1808 bfd_boolean matched;
1810 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1813 /* If this symbol has a version, and it is the default version, we
1814 create an indirect symbol from the default name to the fully
1815 decorated name. This will cause external references which do not
1816 specify a version to be bound to this version of the symbol. */
1817 p = strchr (name, ELF_VER_CHR);
1818 if (h->versioned == unknown)
1822 h->versioned = unversioned;
1827 if (p[1] != ELF_VER_CHR)
1829 h->versioned = versioned_hidden;
1833 h->versioned = versioned;
1838 /* PR ld/19073: We may see an unversioned definition after the
1844 bed = get_elf_backend_data (abfd);
1845 collect = bed->collect;
1846 dynamic = (abfd->flags & DYNAMIC) != 0;
1848 shortlen = p - name;
1849 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1850 if (shortname == NULL)
1852 memcpy (shortname, name, shortlen);
1853 shortname[shortlen] = '\0';
1855 /* We are going to create a new symbol. Merge it with any existing
1856 symbol with this name. For the purposes of the merge, act as
1857 though we were defining the symbol we just defined, although we
1858 actually going to define an indirect symbol. */
1859 type_change_ok = FALSE;
1860 size_change_ok = FALSE;
1863 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1864 &hi, poldbfd, NULL, NULL, &skip, &override,
1865 &type_change_ok, &size_change_ok, &matched))
1871 if (hi->def_regular)
1873 /* If the undecorated symbol will have a version added by a
1874 script different to H, then don't indirect to/from the
1875 undecorated symbol. This isn't ideal because we may not yet
1876 have seen symbol versions, if given by a script on the
1877 command line rather than via --version-script. */
1878 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1883 = bfd_find_version_for_sym (info->version_info,
1884 hi->root.root.string, &hide);
1885 if (hi->verinfo.vertree != NULL && hide)
1887 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1891 if (hi->verinfo.vertree != NULL
1892 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1898 /* Add the default symbol if not performing a relocatable link. */
1899 if (! bfd_link_relocatable (info))
1902 if (! (_bfd_generic_link_add_one_symbol
1903 (info, abfd, shortname, BSF_INDIRECT,
1904 bfd_ind_section_ptr,
1905 0, name, FALSE, collect, &bh)))
1907 hi = (struct elf_link_hash_entry *) bh;
1912 /* In this case the symbol named SHORTNAME is overriding the
1913 indirect symbol we want to add. We were planning on making
1914 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1915 is the name without a version. NAME is the fully versioned
1916 name, and it is the default version.
1918 Overriding means that we already saw a definition for the
1919 symbol SHORTNAME in a regular object, and it is overriding
1920 the symbol defined in the dynamic object.
1922 When this happens, we actually want to change NAME, the
1923 symbol we just added, to refer to SHORTNAME. This will cause
1924 references to NAME in the shared object to become references
1925 to SHORTNAME in the regular object. This is what we expect
1926 when we override a function in a shared object: that the
1927 references in the shared object will be mapped to the
1928 definition in the regular object. */
1930 while (hi->root.type == bfd_link_hash_indirect
1931 || hi->root.type == bfd_link_hash_warning)
1932 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1934 h->root.type = bfd_link_hash_indirect;
1935 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1939 hi->ref_dynamic = 1;
1943 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1948 /* Now set HI to H, so that the following code will set the
1949 other fields correctly. */
1953 /* Check if HI is a warning symbol. */
1954 if (hi->root.type == bfd_link_hash_warning)
1955 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1957 /* If there is a duplicate definition somewhere, then HI may not
1958 point to an indirect symbol. We will have reported an error to
1959 the user in that case. */
1961 if (hi->root.type == bfd_link_hash_indirect)
1963 struct elf_link_hash_entry *ht;
1965 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1966 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1968 /* A reference to the SHORTNAME symbol from a dynamic library
1969 will be satisfied by the versioned symbol at runtime. In
1970 effect, we have a reference to the versioned symbol. */
1971 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1972 hi->dynamic_def |= ht->dynamic_def;
1974 /* See if the new flags lead us to realize that the symbol must
1980 if (! bfd_link_executable (info)
1987 if (hi->ref_regular)
1993 /* We also need to define an indirection from the nondefault version
1997 len = strlen (name);
1998 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1999 if (shortname == NULL)
2001 memcpy (shortname, name, shortlen);
2002 memcpy (shortname + shortlen, p + 1, len - shortlen);
2004 /* Once again, merge with any existing symbol. */
2005 type_change_ok = FALSE;
2006 size_change_ok = FALSE;
2008 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2009 &hi, poldbfd, NULL, NULL, &skip, &override,
2010 &type_change_ok, &size_change_ok, &matched))
2018 /* Here SHORTNAME is a versioned name, so we don't expect to see
2019 the type of override we do in the case above unless it is
2020 overridden by a versioned definition. */
2021 if (hi->root.type != bfd_link_hash_defined
2022 && hi->root.type != bfd_link_hash_defweak)
2024 /* xgettext:c-format */
2025 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
2031 if (! (_bfd_generic_link_add_one_symbol
2032 (info, abfd, shortname, BSF_INDIRECT,
2033 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2035 hi = (struct elf_link_hash_entry *) bh;
2037 /* If there is a duplicate definition somewhere, then HI may not
2038 point to an indirect symbol. We will have reported an error
2039 to the user in that case. */
2041 if (hi->root.type == bfd_link_hash_indirect)
2043 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2044 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2045 hi->dynamic_def |= h->dynamic_def;
2047 /* See if the new flags lead us to realize that the symbol
2053 if (! bfd_link_executable (info)
2059 if (hi->ref_regular)
2069 /* This routine is used to export all defined symbols into the dynamic
2070 symbol table. It is called via elf_link_hash_traverse. */
2073 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2075 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2077 /* Ignore indirect symbols. These are added by the versioning code. */
2078 if (h->root.type == bfd_link_hash_indirect)
2081 /* Ignore this if we won't export it. */
2082 if (!eif->info->export_dynamic && !h->dynamic)
2085 if (h->dynindx == -1
2086 && (h->def_regular || h->ref_regular)
2087 && ! bfd_hide_sym_by_version (eif->info->version_info,
2088 h->root.root.string))
2090 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2100 /* Look through the symbols which are defined in other shared
2101 libraries and referenced here. Update the list of version
2102 dependencies. This will be put into the .gnu.version_r section.
2103 This function is called via elf_link_hash_traverse. */
2106 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2109 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2110 Elf_Internal_Verneed *t;
2111 Elf_Internal_Vernaux *a;
2114 /* We only care about symbols defined in shared objects with version
2119 || h->verinfo.verdef == NULL
2120 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2121 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2124 /* See if we already know about this version. */
2125 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2129 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2132 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2133 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2139 /* This is a new version. Add it to tree we are building. */
2144 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2147 rinfo->failed = TRUE;
2151 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2152 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2153 elf_tdata (rinfo->info->output_bfd)->verref = t;
2157 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2160 rinfo->failed = TRUE;
2164 /* Note that we are copying a string pointer here, and testing it
2165 above. If bfd_elf_string_from_elf_section is ever changed to
2166 discard the string data when low in memory, this will have to be
2168 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2170 a->vna_flags = h->verinfo.verdef->vd_flags;
2171 a->vna_nextptr = t->vn_auxptr;
2173 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2176 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2183 /* Figure out appropriate versions for all the symbols. We may not
2184 have the version number script until we have read all of the input
2185 files, so until that point we don't know which symbols should be
2186 local. This function is called via elf_link_hash_traverse. */
2189 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2191 struct elf_info_failed *sinfo;
2192 struct bfd_link_info *info;
2193 const struct elf_backend_data *bed;
2194 struct elf_info_failed eif;
2197 sinfo = (struct elf_info_failed *) data;
2200 /* Fix the symbol flags. */
2203 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2206 sinfo->failed = TRUE;
2210 /* We only need version numbers for symbols defined in regular
2212 if (!h->def_regular)
2215 bed = get_elf_backend_data (info->output_bfd);
2216 p = strchr (h->root.root.string, ELF_VER_CHR);
2217 if (p != NULL && h->verinfo.vertree == NULL)
2219 struct bfd_elf_version_tree *t;
2222 if (*p == ELF_VER_CHR)
2225 /* If there is no version string, we can just return out. */
2229 /* Look for the version. If we find it, it is no longer weak. */
2230 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2232 if (strcmp (t->name, p) == 0)
2236 struct bfd_elf_version_expr *d;
2238 len = p - h->root.root.string;
2239 alc = (char *) bfd_malloc (len);
2242 sinfo->failed = TRUE;
2245 memcpy (alc, h->root.root.string, len - 1);
2246 alc[len - 1] = '\0';
2247 if (alc[len - 2] == ELF_VER_CHR)
2248 alc[len - 2] = '\0';
2250 h->verinfo.vertree = t;
2254 if (t->globals.list != NULL)
2255 d = (*t->match) (&t->globals, NULL, alc);
2257 /* See if there is anything to force this symbol to
2259 if (d == NULL && t->locals.list != NULL)
2261 d = (*t->match) (&t->locals, NULL, alc);
2264 && ! info->export_dynamic)
2265 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2273 /* If we are building an application, we need to create a
2274 version node for this version. */
2275 if (t == NULL && bfd_link_executable (info))
2277 struct bfd_elf_version_tree **pp;
2280 /* If we aren't going to export this symbol, we don't need
2281 to worry about it. */
2282 if (h->dynindx == -1)
2285 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2289 sinfo->failed = TRUE;
2294 t->name_indx = (unsigned int) -1;
2298 /* Don't count anonymous version tag. */
2299 if (sinfo->info->version_info != NULL
2300 && sinfo->info->version_info->vernum == 0)
2302 for (pp = &sinfo->info->version_info;
2306 t->vernum = version_index;
2310 h->verinfo.vertree = t;
2314 /* We could not find the version for a symbol when
2315 generating a shared archive. Return an error. */
2317 /* xgettext:c-format */
2318 (_("%B: version node not found for symbol %s"),
2319 info->output_bfd, h->root.root.string);
2320 bfd_set_error (bfd_error_bad_value);
2321 sinfo->failed = TRUE;
2326 /* If we don't have a version for this symbol, see if we can find
2328 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2333 = bfd_find_version_for_sym (sinfo->info->version_info,
2334 h->root.root.string, &hide);
2335 if (h->verinfo.vertree != NULL && hide)
2336 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2342 /* Read and swap the relocs from the section indicated by SHDR. This
2343 may be either a REL or a RELA section. The relocations are
2344 translated into RELA relocations and stored in INTERNAL_RELOCS,
2345 which should have already been allocated to contain enough space.
2346 The EXTERNAL_RELOCS are a buffer where the external form of the
2347 relocations should be stored.
2349 Returns FALSE if something goes wrong. */
2352 elf_link_read_relocs_from_section (bfd *abfd,
2354 Elf_Internal_Shdr *shdr,
2355 void *external_relocs,
2356 Elf_Internal_Rela *internal_relocs)
2358 const struct elf_backend_data *bed;
2359 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2360 const bfd_byte *erela;
2361 const bfd_byte *erelaend;
2362 Elf_Internal_Rela *irela;
2363 Elf_Internal_Shdr *symtab_hdr;
2366 /* Position ourselves at the start of the section. */
2367 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2370 /* Read the relocations. */
2371 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2374 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2375 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2377 bed = get_elf_backend_data (abfd);
2379 /* Convert the external relocations to the internal format. */
2380 if (shdr->sh_entsize == bed->s->sizeof_rel)
2381 swap_in = bed->s->swap_reloc_in;
2382 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2383 swap_in = bed->s->swap_reloca_in;
2386 bfd_set_error (bfd_error_wrong_format);
2390 erela = (const bfd_byte *) external_relocs;
2391 erelaend = erela + shdr->sh_size;
2392 irela = internal_relocs;
2393 while (erela < erelaend)
2397 (*swap_in) (abfd, erela, irela);
2398 r_symndx = ELF32_R_SYM (irela->r_info);
2399 if (bed->s->arch_size == 64)
2403 if ((size_t) r_symndx >= nsyms)
2406 /* xgettext:c-format */
2407 (_("%B: bad reloc symbol index (%#Lx >= %#lx)"
2408 " for offset %#Lx in section `%A'"),
2409 abfd, r_symndx, (unsigned long) nsyms,
2410 irela->r_offset, sec);
2411 bfd_set_error (bfd_error_bad_value);
2415 else if (r_symndx != STN_UNDEF)
2418 /* xgettext:c-format */
2419 (_("%B: non-zero symbol index (%#Lx)"
2420 " for offset %#Lx in section `%A'"
2421 " when the object file has no symbol table"),
2423 irela->r_offset, sec);
2424 bfd_set_error (bfd_error_bad_value);
2427 irela += bed->s->int_rels_per_ext_rel;
2428 erela += shdr->sh_entsize;
2434 /* Read and swap the relocs for a section O. They may have been
2435 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2436 not NULL, they are used as buffers to read into. They are known to
2437 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2438 the return value is allocated using either malloc or bfd_alloc,
2439 according to the KEEP_MEMORY argument. If O has two relocation
2440 sections (both REL and RELA relocations), then the REL_HDR
2441 relocations will appear first in INTERNAL_RELOCS, followed by the
2442 RELA_HDR relocations. */
2445 _bfd_elf_link_read_relocs (bfd *abfd,
2447 void *external_relocs,
2448 Elf_Internal_Rela *internal_relocs,
2449 bfd_boolean keep_memory)
2451 void *alloc1 = NULL;
2452 Elf_Internal_Rela *alloc2 = NULL;
2453 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2454 struct bfd_elf_section_data *esdo = elf_section_data (o);
2455 Elf_Internal_Rela *internal_rela_relocs;
2457 if (esdo->relocs != NULL)
2458 return esdo->relocs;
2460 if (o->reloc_count == 0)
2463 if (internal_relocs == NULL)
2467 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2469 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2471 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2472 if (internal_relocs == NULL)
2476 if (external_relocs == NULL)
2478 bfd_size_type size = 0;
2481 size += esdo->rel.hdr->sh_size;
2483 size += esdo->rela.hdr->sh_size;
2485 alloc1 = bfd_malloc (size);
2488 external_relocs = alloc1;
2491 internal_rela_relocs = internal_relocs;
2494 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2498 external_relocs = (((bfd_byte *) external_relocs)
2499 + esdo->rel.hdr->sh_size);
2500 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2501 * bed->s->int_rels_per_ext_rel);
2505 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2507 internal_rela_relocs)))
2510 /* Cache the results for next time, if we can. */
2512 esdo->relocs = internal_relocs;
2517 /* Don't free alloc2, since if it was allocated we are passing it
2518 back (under the name of internal_relocs). */
2520 return internal_relocs;
2528 bfd_release (abfd, alloc2);
2535 /* Compute the size of, and allocate space for, REL_HDR which is the
2536 section header for a section containing relocations for O. */
2539 _bfd_elf_link_size_reloc_section (bfd *abfd,
2540 struct bfd_elf_section_reloc_data *reldata)
2542 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2544 /* That allows us to calculate the size of the section. */
2545 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2547 /* The contents field must last into write_object_contents, so we
2548 allocate it with bfd_alloc rather than malloc. Also since we
2549 cannot be sure that the contents will actually be filled in,
2550 we zero the allocated space. */
2551 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2552 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2555 if (reldata->hashes == NULL && reldata->count)
2557 struct elf_link_hash_entry **p;
2559 p = ((struct elf_link_hash_entry **)
2560 bfd_zmalloc (reldata->count * sizeof (*p)));
2564 reldata->hashes = p;
2570 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2571 originated from the section given by INPUT_REL_HDR) to the
2575 _bfd_elf_link_output_relocs (bfd *output_bfd,
2576 asection *input_section,
2577 Elf_Internal_Shdr *input_rel_hdr,
2578 Elf_Internal_Rela *internal_relocs,
2579 struct elf_link_hash_entry **rel_hash
2582 Elf_Internal_Rela *irela;
2583 Elf_Internal_Rela *irelaend;
2585 struct bfd_elf_section_reloc_data *output_reldata;
2586 asection *output_section;
2587 const struct elf_backend_data *bed;
2588 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2589 struct bfd_elf_section_data *esdo;
2591 output_section = input_section->output_section;
2593 bed = get_elf_backend_data (output_bfd);
2594 esdo = elf_section_data (output_section);
2595 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2597 output_reldata = &esdo->rel;
2598 swap_out = bed->s->swap_reloc_out;
2600 else if (esdo->rela.hdr
2601 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2603 output_reldata = &esdo->rela;
2604 swap_out = bed->s->swap_reloca_out;
2609 /* xgettext:c-format */
2610 (_("%B: relocation size mismatch in %B section %A"),
2611 output_bfd, input_section->owner, input_section);
2612 bfd_set_error (bfd_error_wrong_format);
2616 erel = output_reldata->hdr->contents;
2617 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2618 irela = internal_relocs;
2619 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2620 * bed->s->int_rels_per_ext_rel);
2621 while (irela < irelaend)
2623 (*swap_out) (output_bfd, irela, erel);
2624 irela += bed->s->int_rels_per_ext_rel;
2625 erel += input_rel_hdr->sh_entsize;
2628 /* Bump the counter, so that we know where to add the next set of
2630 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2635 /* Make weak undefined symbols in PIE dynamic. */
2638 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2639 struct elf_link_hash_entry *h)
2641 if (bfd_link_pie (info)
2643 && h->root.type == bfd_link_hash_undefweak)
2644 return bfd_elf_link_record_dynamic_symbol (info, h);
2649 /* Fix up the flags for a symbol. This handles various cases which
2650 can only be fixed after all the input files are seen. This is
2651 currently called by both adjust_dynamic_symbol and
2652 assign_sym_version, which is unnecessary but perhaps more robust in
2653 the face of future changes. */
2656 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2657 struct elf_info_failed *eif)
2659 const struct elf_backend_data *bed;
2661 /* If this symbol was mentioned in a non-ELF file, try to set
2662 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2663 permit a non-ELF file to correctly refer to a symbol defined in
2664 an ELF dynamic object. */
2667 while (h->root.type == bfd_link_hash_indirect)
2668 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2670 if (h->root.type != bfd_link_hash_defined
2671 && h->root.type != bfd_link_hash_defweak)
2674 h->ref_regular_nonweak = 1;
2678 if (h->root.u.def.section->owner != NULL
2679 && (bfd_get_flavour (h->root.u.def.section->owner)
2680 == bfd_target_elf_flavour))
2683 h->ref_regular_nonweak = 1;
2689 if (h->dynindx == -1
2693 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2702 /* Unfortunately, NON_ELF is only correct if the symbol
2703 was first seen in a non-ELF file. Fortunately, if the symbol
2704 was first seen in an ELF file, we're probably OK unless the
2705 symbol was defined in a non-ELF file. Catch that case here.
2706 FIXME: We're still in trouble if the symbol was first seen in
2707 a dynamic object, and then later in a non-ELF regular object. */
2708 if ((h->root.type == bfd_link_hash_defined
2709 || h->root.type == bfd_link_hash_defweak)
2711 && (h->root.u.def.section->owner != NULL
2712 ? (bfd_get_flavour (h->root.u.def.section->owner)
2713 != bfd_target_elf_flavour)
2714 : (bfd_is_abs_section (h->root.u.def.section)
2715 && !h->def_dynamic)))
2719 /* Backend specific symbol fixup. */
2720 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2721 if (bed->elf_backend_fixup_symbol
2722 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2725 /* If this is a final link, and the symbol was defined as a common
2726 symbol in a regular object file, and there was no definition in
2727 any dynamic object, then the linker will have allocated space for
2728 the symbol in a common section but the DEF_REGULAR
2729 flag will not have been set. */
2730 if (h->root.type == bfd_link_hash_defined
2734 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2737 /* If a weak undefined symbol has non-default visibility, we also
2738 hide it from the dynamic linker. */
2739 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2740 && h->root.type == bfd_link_hash_undefweak)
2741 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2743 /* A hidden versioned symbol in executable should be forced local if
2744 it is is locally defined, not referenced by shared library and not
2746 else if (bfd_link_executable (eif->info)
2747 && h->versioned == versioned_hidden
2748 && !eif->info->export_dynamic
2752 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2754 /* If -Bsymbolic was used (which means to bind references to global
2755 symbols to the definition within the shared object), and this
2756 symbol was defined in a regular object, then it actually doesn't
2757 need a PLT entry. Likewise, if the symbol has non-default
2758 visibility. If the symbol has hidden or internal visibility, we
2759 will force it local. */
2760 else if (h->needs_plt
2761 && bfd_link_pic (eif->info)
2762 && is_elf_hash_table (eif->info->hash)
2763 && (SYMBOLIC_BIND (eif->info, h)
2764 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2767 bfd_boolean force_local;
2769 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2770 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2771 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2774 /* If this is a weak defined symbol in a dynamic object, and we know
2775 the real definition in the dynamic object, copy interesting flags
2776 over to the real definition. */
2777 if (h->u.weakdef != NULL)
2779 /* If the real definition is defined by a regular object file,
2780 don't do anything special. See the longer description in
2781 _bfd_elf_adjust_dynamic_symbol, below. */
2782 if (h->u.weakdef->def_regular)
2783 h->u.weakdef = NULL;
2786 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2788 while (h->root.type == bfd_link_hash_indirect)
2789 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2791 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2792 || h->root.type == bfd_link_hash_defweak);
2793 BFD_ASSERT (weakdef->def_dynamic);
2794 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2795 || weakdef->root.type == bfd_link_hash_defweak);
2796 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2803 /* Make the backend pick a good value for a dynamic symbol. This is
2804 called via elf_link_hash_traverse, and also calls itself
2808 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2810 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2811 struct elf_link_hash_table *htab;
2812 const struct elf_backend_data *bed;
2814 if (! is_elf_hash_table (eif->info->hash))
2817 /* Ignore indirect symbols. These are added by the versioning code. */
2818 if (h->root.type == bfd_link_hash_indirect)
2821 /* Fix the symbol flags. */
2822 if (! _bfd_elf_fix_symbol_flags (h, eif))
2825 htab = elf_hash_table (eif->info);
2826 bed = get_elf_backend_data (htab->dynobj);
2828 if (h->root.type == bfd_link_hash_undefweak)
2830 if (eif->info->dynamic_undefined_weak == 0)
2831 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2832 else if (eif->info->dynamic_undefined_weak > 0
2834 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2835 && !bfd_hide_sym_by_version (eif->info->version_info,
2836 h->root.root.string))
2838 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2846 /* If this symbol does not require a PLT entry, and it is not
2847 defined by a dynamic object, or is not referenced by a regular
2848 object, ignore it. We do have to handle a weak defined symbol,
2849 even if no regular object refers to it, if we decided to add it
2850 to the dynamic symbol table. FIXME: Do we normally need to worry
2851 about symbols which are defined by one dynamic object and
2852 referenced by another one? */
2854 && h->type != STT_GNU_IFUNC
2858 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2860 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2864 /* If we've already adjusted this symbol, don't do it again. This
2865 can happen via a recursive call. */
2866 if (h->dynamic_adjusted)
2869 /* Don't look at this symbol again. Note that we must set this
2870 after checking the above conditions, because we may look at a
2871 symbol once, decide not to do anything, and then get called
2872 recursively later after REF_REGULAR is set below. */
2873 h->dynamic_adjusted = 1;
2875 /* If this is a weak definition, and we know a real definition, and
2876 the real symbol is not itself defined by a regular object file,
2877 then get a good value for the real definition. We handle the
2878 real symbol first, for the convenience of the backend routine.
2880 Note that there is a confusing case here. If the real definition
2881 is defined by a regular object file, we don't get the real symbol
2882 from the dynamic object, but we do get the weak symbol. If the
2883 processor backend uses a COPY reloc, then if some routine in the
2884 dynamic object changes the real symbol, we will not see that
2885 change in the corresponding weak symbol. This is the way other
2886 ELF linkers work as well, and seems to be a result of the shared
2889 I will clarify this issue. Most SVR4 shared libraries define the
2890 variable _timezone and define timezone as a weak synonym. The
2891 tzset call changes _timezone. If you write
2892 extern int timezone;
2894 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2895 you might expect that, since timezone is a synonym for _timezone,
2896 the same number will print both times. However, if the processor
2897 backend uses a COPY reloc, then actually timezone will be copied
2898 into your process image, and, since you define _timezone
2899 yourself, _timezone will not. Thus timezone and _timezone will
2900 wind up at different memory locations. The tzset call will set
2901 _timezone, leaving timezone unchanged. */
2903 if (h->u.weakdef != NULL)
2905 /* If we get to this point, there is an implicit reference to
2906 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2907 h->u.weakdef->ref_regular = 1;
2909 /* Ensure that the backend adjust_dynamic_symbol function sees
2910 H->U.WEAKDEF before H by recursively calling ourselves. */
2911 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2915 /* If a symbol has no type and no size and does not require a PLT
2916 entry, then we are probably about to do the wrong thing here: we
2917 are probably going to create a COPY reloc for an empty object.
2918 This case can arise when a shared object is built with assembly
2919 code, and the assembly code fails to set the symbol type. */
2921 && h->type == STT_NOTYPE
2924 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2925 h->root.root.string);
2927 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2936 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2940 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2941 struct elf_link_hash_entry *h,
2944 unsigned int power_of_two;
2946 asection *sec = h->root.u.def.section;
2948 /* The section alignment of the definition is the maximum alignment
2949 requirement of symbols defined in the section. Since we don't
2950 know the symbol alignment requirement, we start with the
2951 maximum alignment and check low bits of the symbol address
2952 for the minimum alignment. */
2953 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2954 mask = ((bfd_vma) 1 << power_of_two) - 1;
2955 while ((h->root.u.def.value & mask) != 0)
2961 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2964 /* Adjust the section alignment if needed. */
2965 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2970 /* We make sure that the symbol will be aligned properly. */
2971 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2973 /* Define the symbol as being at this point in DYNBSS. */
2974 h->root.u.def.section = dynbss;
2975 h->root.u.def.value = dynbss->size;
2977 /* Increment the size of DYNBSS to make room for the symbol. */
2978 dynbss->size += h->size;
2980 /* No error if extern_protected_data is true. */
2981 if (h->protected_def
2982 && (!info->extern_protected_data
2983 || (info->extern_protected_data < 0
2984 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2985 info->callbacks->einfo
2986 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2987 h->root.root.string);
2992 /* Adjust all external symbols pointing into SEC_MERGE sections
2993 to reflect the object merging within the sections. */
2996 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3000 if ((h->root.type == bfd_link_hash_defined
3001 || h->root.type == bfd_link_hash_defweak)
3002 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3003 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3005 bfd *output_bfd = (bfd *) data;
3007 h->root.u.def.value =
3008 _bfd_merged_section_offset (output_bfd,
3009 &h->root.u.def.section,
3010 elf_section_data (sec)->sec_info,
3011 h->root.u.def.value);
3017 /* Returns false if the symbol referred to by H should be considered
3018 to resolve local to the current module, and true if it should be
3019 considered to bind dynamically. */
3022 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3023 struct bfd_link_info *info,
3024 bfd_boolean not_local_protected)
3026 bfd_boolean binding_stays_local_p;
3027 const struct elf_backend_data *bed;
3028 struct elf_link_hash_table *hash_table;
3033 while (h->root.type == bfd_link_hash_indirect
3034 || h->root.type == bfd_link_hash_warning)
3035 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3037 /* If it was forced local, then clearly it's not dynamic. */
3038 if (h->dynindx == -1)
3040 if (h->forced_local)
3043 /* Identify the cases where name binding rules say that a
3044 visible symbol resolves locally. */
3045 binding_stays_local_p = (bfd_link_executable (info)
3046 || SYMBOLIC_BIND (info, h));
3048 switch (ELF_ST_VISIBILITY (h->other))
3055 hash_table = elf_hash_table (info);
3056 if (!is_elf_hash_table (hash_table))
3059 bed = get_elf_backend_data (hash_table->dynobj);
3061 /* Proper resolution for function pointer equality may require
3062 that these symbols perhaps be resolved dynamically, even though
3063 we should be resolving them to the current module. */
3064 if (!not_local_protected || !bed->is_function_type (h->type))
3065 binding_stays_local_p = TRUE;
3072 /* If it isn't defined locally, then clearly it's dynamic. */
3073 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3076 /* Otherwise, the symbol is dynamic if binding rules don't tell
3077 us that it remains local. */
3078 return !binding_stays_local_p;
3081 /* Return true if the symbol referred to by H should be considered
3082 to resolve local to the current module, and false otherwise. Differs
3083 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3084 undefined symbols. The two functions are virtually identical except
3085 for the place where dynindx == -1 is tested. If that test is true,
3086 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3087 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3089 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3090 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3091 treatment of undefined weak symbols. For those that do not make
3092 undefined weak symbols dynamic, both functions may return false. */
3095 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3096 struct bfd_link_info *info,
3097 bfd_boolean local_protected)
3099 const struct elf_backend_data *bed;
3100 struct elf_link_hash_table *hash_table;
3102 /* If it's a local sym, of course we resolve locally. */
3106 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3107 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3108 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3111 /* Forced local symbols resolve locally. */
3112 if (h->forced_local)
3115 /* Common symbols that become definitions don't get the DEF_REGULAR
3116 flag set, so test it first, and don't bail out. */
3117 if (ELF_COMMON_DEF_P (h))
3119 /* If we don't have a definition in a regular file, then we can't
3120 resolve locally. The sym is either undefined or dynamic. */
3121 else if (!h->def_regular)
3124 /* Non-dynamic symbols resolve locally. */
3125 if (h->dynindx == -1)
3128 /* At this point, we know the symbol is defined and dynamic. In an
3129 executable it must resolve locally, likewise when building symbolic
3130 shared libraries. */
3131 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3134 /* Now deal with defined dynamic symbols in shared libraries. Ones
3135 with default visibility might not resolve locally. */
3136 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3139 hash_table = elf_hash_table (info);
3140 if (!is_elf_hash_table (hash_table))
3143 bed = get_elf_backend_data (hash_table->dynobj);
3145 /* If extern_protected_data is false, STV_PROTECTED non-function
3146 symbols are local. */
3147 if ((!info->extern_protected_data
3148 || (info->extern_protected_data < 0
3149 && !bed->extern_protected_data))
3150 && !bed->is_function_type (h->type))
3153 /* Function pointer equality tests may require that STV_PROTECTED
3154 symbols be treated as dynamic symbols. If the address of a
3155 function not defined in an executable is set to that function's
3156 plt entry in the executable, then the address of the function in
3157 a shared library must also be the plt entry in the executable. */
3158 return local_protected;
3161 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3162 aligned. Returns the first TLS output section. */
3164 struct bfd_section *
3165 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3167 struct bfd_section *sec, *tls;
3168 unsigned int align = 0;
3170 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3171 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3175 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3176 if (sec->alignment_power > align)
3177 align = sec->alignment_power;
3179 elf_hash_table (info)->tls_sec = tls;
3181 /* Ensure the alignment of the first section is the largest alignment,
3182 so that the tls segment starts aligned. */
3184 tls->alignment_power = align;
3189 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3191 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3192 Elf_Internal_Sym *sym)
3194 const struct elf_backend_data *bed;
3196 /* Local symbols do not count, but target specific ones might. */
3197 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3198 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3201 bed = get_elf_backend_data (abfd);
3202 /* Function symbols do not count. */
3203 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3206 /* If the section is undefined, then so is the symbol. */
3207 if (sym->st_shndx == SHN_UNDEF)
3210 /* If the symbol is defined in the common section, then
3211 it is a common definition and so does not count. */
3212 if (bed->common_definition (sym))
3215 /* If the symbol is in a target specific section then we
3216 must rely upon the backend to tell us what it is. */
3217 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3218 /* FIXME - this function is not coded yet:
3220 return _bfd_is_global_symbol_definition (abfd, sym);
3222 Instead for now assume that the definition is not global,
3223 Even if this is wrong, at least the linker will behave
3224 in the same way that it used to do. */
3230 /* Search the symbol table of the archive element of the archive ABFD
3231 whose archive map contains a mention of SYMDEF, and determine if
3232 the symbol is defined in this element. */
3234 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3236 Elf_Internal_Shdr * hdr;
3240 Elf_Internal_Sym *isymbuf;
3241 Elf_Internal_Sym *isym;
3242 Elf_Internal_Sym *isymend;
3245 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3249 if (! bfd_check_format (abfd, bfd_object))
3252 /* Select the appropriate symbol table. If we don't know if the
3253 object file is an IR object, give linker LTO plugin a chance to
3254 get the correct symbol table. */
3255 if (abfd->plugin_format == bfd_plugin_yes
3256 #if BFD_SUPPORTS_PLUGINS
3257 || (abfd->plugin_format == bfd_plugin_unknown
3258 && bfd_link_plugin_object_p (abfd))
3262 /* Use the IR symbol table if the object has been claimed by
3264 abfd = abfd->plugin_dummy_bfd;
3265 hdr = &elf_tdata (abfd)->symtab_hdr;
3267 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3268 hdr = &elf_tdata (abfd)->symtab_hdr;
3270 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3272 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3274 /* The sh_info field of the symtab header tells us where the
3275 external symbols start. We don't care about the local symbols. */
3276 if (elf_bad_symtab (abfd))
3278 extsymcount = symcount;
3283 extsymcount = symcount - hdr->sh_info;
3284 extsymoff = hdr->sh_info;
3287 if (extsymcount == 0)
3290 /* Read in the symbol table. */
3291 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3293 if (isymbuf == NULL)
3296 /* Scan the symbol table looking for SYMDEF. */
3298 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3302 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3307 if (strcmp (name, symdef->name) == 0)
3309 result = is_global_data_symbol_definition (abfd, isym);
3319 /* Add an entry to the .dynamic table. */
3322 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3326 struct elf_link_hash_table *hash_table;
3327 const struct elf_backend_data *bed;
3329 bfd_size_type newsize;
3330 bfd_byte *newcontents;
3331 Elf_Internal_Dyn dyn;
3333 hash_table = elf_hash_table (info);
3334 if (! is_elf_hash_table (hash_table))
3337 bed = get_elf_backend_data (hash_table->dynobj);
3338 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3339 BFD_ASSERT (s != NULL);
3341 newsize = s->size + bed->s->sizeof_dyn;
3342 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3343 if (newcontents == NULL)
3347 dyn.d_un.d_val = val;
3348 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3351 s->contents = newcontents;
3356 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3357 otherwise just check whether one already exists. Returns -1 on error,
3358 1 if a DT_NEEDED tag already exists, and 0 on success. */
3361 elf_add_dt_needed_tag (bfd *abfd,
3362 struct bfd_link_info *info,
3366 struct elf_link_hash_table *hash_table;
3369 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3372 hash_table = elf_hash_table (info);
3373 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3374 if (strindex == (size_t) -1)
3377 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3380 const struct elf_backend_data *bed;
3383 bed = get_elf_backend_data (hash_table->dynobj);
3384 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3386 for (extdyn = sdyn->contents;
3387 extdyn < sdyn->contents + sdyn->size;
3388 extdyn += bed->s->sizeof_dyn)
3390 Elf_Internal_Dyn dyn;
3392 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3393 if (dyn.d_tag == DT_NEEDED
3394 && dyn.d_un.d_val == strindex)
3396 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3404 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3407 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3411 /* We were just checking for existence of the tag. */
3412 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3417 /* Return true if SONAME is on the needed list between NEEDED and STOP
3418 (or the end of list if STOP is NULL), and needed by a library that
3422 on_needed_list (const char *soname,
3423 struct bfd_link_needed_list *needed,
3424 struct bfd_link_needed_list *stop)
3426 struct bfd_link_needed_list *look;
3427 for (look = needed; look != stop; look = look->next)
3428 if (strcmp (soname, look->name) == 0
3429 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3430 /* If needed by a library that itself is not directly
3431 needed, recursively check whether that library is
3432 indirectly needed. Since we add DT_NEEDED entries to
3433 the end of the list, library dependencies appear after
3434 the library. Therefore search prior to the current
3435 LOOK, preventing possible infinite recursion. */
3436 || on_needed_list (elf_dt_name (look->by), needed, look)))
3442 /* Sort symbol by value, section, and size. */
3444 elf_sort_symbol (const void *arg1, const void *arg2)
3446 const struct elf_link_hash_entry *h1;
3447 const struct elf_link_hash_entry *h2;
3448 bfd_signed_vma vdiff;
3450 h1 = *(const struct elf_link_hash_entry **) arg1;
3451 h2 = *(const struct elf_link_hash_entry **) arg2;
3452 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3454 return vdiff > 0 ? 1 : -1;
3457 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3459 return sdiff > 0 ? 1 : -1;
3461 vdiff = h1->size - h2->size;
3462 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3465 /* This function is used to adjust offsets into .dynstr for
3466 dynamic symbols. This is called via elf_link_hash_traverse. */
3469 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3471 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3473 if (h->dynindx != -1)
3474 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3478 /* Assign string offsets in .dynstr, update all structures referencing
3482 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3484 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3485 struct elf_link_local_dynamic_entry *entry;
3486 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3487 bfd *dynobj = hash_table->dynobj;
3490 const struct elf_backend_data *bed;
3493 _bfd_elf_strtab_finalize (dynstr);
3494 size = _bfd_elf_strtab_size (dynstr);
3496 bed = get_elf_backend_data (dynobj);
3497 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3498 BFD_ASSERT (sdyn != NULL);
3500 /* Update all .dynamic entries referencing .dynstr strings. */
3501 for (extdyn = sdyn->contents;
3502 extdyn < sdyn->contents + sdyn->size;
3503 extdyn += bed->s->sizeof_dyn)
3505 Elf_Internal_Dyn dyn;
3507 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3511 dyn.d_un.d_val = size;
3521 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3526 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3529 /* Now update local dynamic symbols. */
3530 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3531 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3532 entry->isym.st_name);
3534 /* And the rest of dynamic symbols. */
3535 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3537 /* Adjust version definitions. */
3538 if (elf_tdata (output_bfd)->cverdefs)
3543 Elf_Internal_Verdef def;
3544 Elf_Internal_Verdaux defaux;
3546 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3550 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3552 p += sizeof (Elf_External_Verdef);
3553 if (def.vd_aux != sizeof (Elf_External_Verdef))
3555 for (i = 0; i < def.vd_cnt; ++i)
3557 _bfd_elf_swap_verdaux_in (output_bfd,
3558 (Elf_External_Verdaux *) p, &defaux);
3559 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3561 _bfd_elf_swap_verdaux_out (output_bfd,
3562 &defaux, (Elf_External_Verdaux *) p);
3563 p += sizeof (Elf_External_Verdaux);
3566 while (def.vd_next);
3569 /* Adjust version references. */
3570 if (elf_tdata (output_bfd)->verref)
3575 Elf_Internal_Verneed need;
3576 Elf_Internal_Vernaux needaux;
3578 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3582 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3584 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3585 _bfd_elf_swap_verneed_out (output_bfd, &need,
3586 (Elf_External_Verneed *) p);
3587 p += sizeof (Elf_External_Verneed);
3588 for (i = 0; i < need.vn_cnt; ++i)
3590 _bfd_elf_swap_vernaux_in (output_bfd,
3591 (Elf_External_Vernaux *) p, &needaux);
3592 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3594 _bfd_elf_swap_vernaux_out (output_bfd,
3596 (Elf_External_Vernaux *) p);
3597 p += sizeof (Elf_External_Vernaux);
3600 while (need.vn_next);
3606 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3607 The default is to only match when the INPUT and OUTPUT are exactly
3611 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3612 const bfd_target *output)
3614 return input == output;
3617 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3618 This version is used when different targets for the same architecture
3619 are virtually identical. */
3622 _bfd_elf_relocs_compatible (const bfd_target *input,
3623 const bfd_target *output)
3625 const struct elf_backend_data *obed, *ibed;
3627 if (input == output)
3630 ibed = xvec_get_elf_backend_data (input);
3631 obed = xvec_get_elf_backend_data (output);
3633 if (ibed->arch != obed->arch)
3636 /* If both backends are using this function, deem them compatible. */
3637 return ibed->relocs_compatible == obed->relocs_compatible;
3640 /* Make a special call to the linker "notice" function to tell it that
3641 we are about to handle an as-needed lib, or have finished
3642 processing the lib. */
3645 _bfd_elf_notice_as_needed (bfd *ibfd,
3646 struct bfd_link_info *info,
3647 enum notice_asneeded_action act)
3649 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3652 /* Check relocations an ELF object file. */
3655 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3658 struct elf_link_hash_table *htab = elf_hash_table (info);
3660 /* If this object is the same format as the output object, and it is
3661 not a shared library, then let the backend look through the
3664 This is required to build global offset table entries and to
3665 arrange for dynamic relocs. It is not required for the
3666 particular common case of linking non PIC code, even when linking
3667 against shared libraries, but unfortunately there is no way of
3668 knowing whether an object file has been compiled PIC or not.
3669 Looking through the relocs is not particularly time consuming.
3670 The problem is that we must either (1) keep the relocs in memory,
3671 which causes the linker to require additional runtime memory or
3672 (2) read the relocs twice from the input file, which wastes time.
3673 This would be a good case for using mmap.
3675 I have no idea how to handle linking PIC code into a file of a
3676 different format. It probably can't be done. */
3677 if ((abfd->flags & DYNAMIC) == 0
3678 && is_elf_hash_table (htab)
3679 && bed->check_relocs != NULL
3680 && elf_object_id (abfd) == elf_hash_table_id (htab)
3681 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3685 for (o = abfd->sections; o != NULL; o = o->next)
3687 Elf_Internal_Rela *internal_relocs;
3690 /* Don't check relocations in excluded sections. */
3691 if ((o->flags & SEC_RELOC) == 0
3692 || (o->flags & SEC_EXCLUDE) != 0
3693 || o->reloc_count == 0
3694 || ((info->strip == strip_all || info->strip == strip_debugger)
3695 && (o->flags & SEC_DEBUGGING) != 0)
3696 || bfd_is_abs_section (o->output_section))
3699 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3701 if (internal_relocs == NULL)
3704 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3706 if (elf_section_data (o)->relocs != internal_relocs)
3707 free (internal_relocs);
3717 /* Add symbols from an ELF object file to the linker hash table. */
3720 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3722 Elf_Internal_Ehdr *ehdr;
3723 Elf_Internal_Shdr *hdr;
3727 struct elf_link_hash_entry **sym_hash;
3728 bfd_boolean dynamic;
3729 Elf_External_Versym *extversym = NULL;
3730 Elf_External_Versym *ever;
3731 struct elf_link_hash_entry *weaks;
3732 struct elf_link_hash_entry **nondeflt_vers = NULL;
3733 size_t nondeflt_vers_cnt = 0;
3734 Elf_Internal_Sym *isymbuf = NULL;
3735 Elf_Internal_Sym *isym;
3736 Elf_Internal_Sym *isymend;
3737 const struct elf_backend_data *bed;
3738 bfd_boolean add_needed;
3739 struct elf_link_hash_table *htab;
3741 void *alloc_mark = NULL;
3742 struct bfd_hash_entry **old_table = NULL;
3743 unsigned int old_size = 0;
3744 unsigned int old_count = 0;
3745 void *old_tab = NULL;
3747 struct bfd_link_hash_entry *old_undefs = NULL;
3748 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3749 void *old_strtab = NULL;
3752 bfd_boolean just_syms;
3754 htab = elf_hash_table (info);
3755 bed = get_elf_backend_data (abfd);
3757 if ((abfd->flags & DYNAMIC) == 0)
3763 /* You can't use -r against a dynamic object. Also, there's no
3764 hope of using a dynamic object which does not exactly match
3765 the format of the output file. */
3766 if (bfd_link_relocatable (info)
3767 || !is_elf_hash_table (htab)
3768 || info->output_bfd->xvec != abfd->xvec)
3770 if (bfd_link_relocatable (info))
3771 bfd_set_error (bfd_error_invalid_operation);
3773 bfd_set_error (bfd_error_wrong_format);
3778 ehdr = elf_elfheader (abfd);
3779 if (info->warn_alternate_em
3780 && bed->elf_machine_code != ehdr->e_machine
3781 && ((bed->elf_machine_alt1 != 0
3782 && ehdr->e_machine == bed->elf_machine_alt1)
3783 || (bed->elf_machine_alt2 != 0
3784 && ehdr->e_machine == bed->elf_machine_alt2)))
3785 info->callbacks->einfo
3786 /* xgettext:c-format */
3787 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3788 ehdr->e_machine, abfd, bed->elf_machine_code);
3790 /* As a GNU extension, any input sections which are named
3791 .gnu.warning.SYMBOL are treated as warning symbols for the given
3792 symbol. This differs from .gnu.warning sections, which generate
3793 warnings when they are included in an output file. */
3794 /* PR 12761: Also generate this warning when building shared libraries. */
3795 for (s = abfd->sections; s != NULL; s = s->next)
3799 name = bfd_get_section_name (abfd, s);
3800 if (CONST_STRNEQ (name, ".gnu.warning."))
3805 name += sizeof ".gnu.warning." - 1;
3807 /* If this is a shared object, then look up the symbol
3808 in the hash table. If it is there, and it is already
3809 been defined, then we will not be using the entry
3810 from this shared object, so we don't need to warn.
3811 FIXME: If we see the definition in a regular object
3812 later on, we will warn, but we shouldn't. The only
3813 fix is to keep track of what warnings we are supposed
3814 to emit, and then handle them all at the end of the
3818 struct elf_link_hash_entry *h;
3820 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3822 /* FIXME: What about bfd_link_hash_common? */
3824 && (h->root.type == bfd_link_hash_defined
3825 || h->root.type == bfd_link_hash_defweak))
3830 msg = (char *) bfd_alloc (abfd, sz + 1);
3834 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3839 if (! (_bfd_generic_link_add_one_symbol
3840 (info, abfd, name, BSF_WARNING, s, 0, msg,
3841 FALSE, bed->collect, NULL)))
3844 if (bfd_link_executable (info))
3846 /* Clobber the section size so that the warning does
3847 not get copied into the output file. */
3850 /* Also set SEC_EXCLUDE, so that symbols defined in
3851 the warning section don't get copied to the output. */
3852 s->flags |= SEC_EXCLUDE;
3857 just_syms = ((s = abfd->sections) != NULL
3858 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3863 /* If we are creating a shared library, create all the dynamic
3864 sections immediately. We need to attach them to something,
3865 so we attach them to this BFD, provided it is the right
3866 format and is not from ld --just-symbols. Always create the
3867 dynamic sections for -E/--dynamic-list. FIXME: If there
3868 are no input BFD's of the same format as the output, we can't
3869 make a shared library. */
3871 && (bfd_link_pic (info)
3872 || (!bfd_link_relocatable (info)
3874 && (info->export_dynamic || info->dynamic)))
3875 && is_elf_hash_table (htab)
3876 && info->output_bfd->xvec == abfd->xvec
3877 && !htab->dynamic_sections_created)
3879 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3883 else if (!is_elf_hash_table (htab))
3887 const char *soname = NULL;
3889 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3890 const Elf_Internal_Phdr *phdr;
3893 /* ld --just-symbols and dynamic objects don't mix very well.
3894 ld shouldn't allow it. */
3898 /* If this dynamic lib was specified on the command line with
3899 --as-needed in effect, then we don't want to add a DT_NEEDED
3900 tag unless the lib is actually used. Similary for libs brought
3901 in by another lib's DT_NEEDED. When --no-add-needed is used
3902 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3903 any dynamic library in DT_NEEDED tags in the dynamic lib at
3905 add_needed = (elf_dyn_lib_class (abfd)
3906 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3907 | DYN_NO_NEEDED)) == 0;
3909 s = bfd_get_section_by_name (abfd, ".dynamic");
3914 unsigned int elfsec;
3915 unsigned long shlink;
3917 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3924 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3925 if (elfsec == SHN_BAD)
3926 goto error_free_dyn;
3927 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3929 for (extdyn = dynbuf;
3930 extdyn < dynbuf + s->size;
3931 extdyn += bed->s->sizeof_dyn)
3933 Elf_Internal_Dyn dyn;
3935 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3936 if (dyn.d_tag == DT_SONAME)
3938 unsigned int tagv = dyn.d_un.d_val;
3939 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3941 goto error_free_dyn;
3943 if (dyn.d_tag == DT_NEEDED)
3945 struct bfd_link_needed_list *n, **pn;
3947 unsigned int tagv = dyn.d_un.d_val;
3949 amt = sizeof (struct bfd_link_needed_list);
3950 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3951 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3952 if (n == NULL || fnm == NULL)
3953 goto error_free_dyn;
3954 amt = strlen (fnm) + 1;
3955 anm = (char *) bfd_alloc (abfd, amt);
3957 goto error_free_dyn;
3958 memcpy (anm, fnm, amt);
3962 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3966 if (dyn.d_tag == DT_RUNPATH)
3968 struct bfd_link_needed_list *n, **pn;
3970 unsigned int tagv = dyn.d_un.d_val;
3972 amt = sizeof (struct bfd_link_needed_list);
3973 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3974 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3975 if (n == NULL || fnm == NULL)
3976 goto error_free_dyn;
3977 amt = strlen (fnm) + 1;
3978 anm = (char *) bfd_alloc (abfd, amt);
3980 goto error_free_dyn;
3981 memcpy (anm, fnm, amt);
3985 for (pn = & runpath;
3991 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3992 if (!runpath && dyn.d_tag == DT_RPATH)
3994 struct bfd_link_needed_list *n, **pn;
3996 unsigned int tagv = dyn.d_un.d_val;
3998 amt = sizeof (struct bfd_link_needed_list);
3999 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4000 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4001 if (n == NULL || fnm == NULL)
4002 goto error_free_dyn;
4003 amt = strlen (fnm) + 1;
4004 anm = (char *) bfd_alloc (abfd, amt);
4006 goto error_free_dyn;
4007 memcpy (anm, fnm, amt);
4017 if (dyn.d_tag == DT_AUDIT)
4019 unsigned int tagv = dyn.d_un.d_val;
4020 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4027 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4028 frees all more recently bfd_alloc'd blocks as well. */
4034 struct bfd_link_needed_list **pn;
4035 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4040 /* If we have a PT_GNU_RELRO program header, mark as read-only
4041 all sections contained fully therein. This makes relro
4042 shared library sections appear as they will at run-time. */
4043 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4044 while (--phdr >= elf_tdata (abfd)->phdr)
4045 if (phdr->p_type == PT_GNU_RELRO)
4047 for (s = abfd->sections; s != NULL; s = s->next)
4048 if ((s->flags & SEC_ALLOC) != 0
4049 && s->vma >= phdr->p_vaddr
4050 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4051 s->flags |= SEC_READONLY;
4055 /* We do not want to include any of the sections in a dynamic
4056 object in the output file. We hack by simply clobbering the
4057 list of sections in the BFD. This could be handled more
4058 cleanly by, say, a new section flag; the existing
4059 SEC_NEVER_LOAD flag is not the one we want, because that one
4060 still implies that the section takes up space in the output
4062 bfd_section_list_clear (abfd);
4064 /* Find the name to use in a DT_NEEDED entry that refers to this
4065 object. If the object has a DT_SONAME entry, we use it.
4066 Otherwise, if the generic linker stuck something in
4067 elf_dt_name, we use that. Otherwise, we just use the file
4069 if (soname == NULL || *soname == '\0')
4071 soname = elf_dt_name (abfd);
4072 if (soname == NULL || *soname == '\0')
4073 soname = bfd_get_filename (abfd);
4076 /* Save the SONAME because sometimes the linker emulation code
4077 will need to know it. */
4078 elf_dt_name (abfd) = soname;
4080 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4084 /* If we have already included this dynamic object in the
4085 link, just ignore it. There is no reason to include a
4086 particular dynamic object more than once. */
4090 /* Save the DT_AUDIT entry for the linker emulation code. */
4091 elf_dt_audit (abfd) = audit;
4094 /* If this is a dynamic object, we always link against the .dynsym
4095 symbol table, not the .symtab symbol table. The dynamic linker
4096 will only see the .dynsym symbol table, so there is no reason to
4097 look at .symtab for a dynamic object. */
4099 if (! dynamic || elf_dynsymtab (abfd) == 0)
4100 hdr = &elf_tdata (abfd)->symtab_hdr;
4102 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4104 symcount = hdr->sh_size / bed->s->sizeof_sym;
4106 /* The sh_info field of the symtab header tells us where the
4107 external symbols start. We don't care about the local symbols at
4109 if (elf_bad_symtab (abfd))
4111 extsymcount = symcount;
4116 extsymcount = symcount - hdr->sh_info;
4117 extsymoff = hdr->sh_info;
4120 sym_hash = elf_sym_hashes (abfd);
4121 if (extsymcount != 0)
4123 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4125 if (isymbuf == NULL)
4128 if (sym_hash == NULL)
4130 /* We store a pointer to the hash table entry for each
4133 amt *= sizeof (struct elf_link_hash_entry *);
4134 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4135 if (sym_hash == NULL)
4136 goto error_free_sym;
4137 elf_sym_hashes (abfd) = sym_hash;
4143 /* Read in any version definitions. */
4144 if (!_bfd_elf_slurp_version_tables (abfd,
4145 info->default_imported_symver))
4146 goto error_free_sym;
4148 /* Read in the symbol versions, but don't bother to convert them
4149 to internal format. */
4150 if (elf_dynversym (abfd) != 0)
4152 Elf_Internal_Shdr *versymhdr;
4154 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4155 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4156 if (extversym == NULL)
4157 goto error_free_sym;
4158 amt = versymhdr->sh_size;
4159 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4160 || bfd_bread (extversym, amt, abfd) != amt)
4161 goto error_free_vers;
4165 /* If we are loading an as-needed shared lib, save the symbol table
4166 state before we start adding symbols. If the lib turns out
4167 to be unneeded, restore the state. */
4168 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4173 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4175 struct bfd_hash_entry *p;
4176 struct elf_link_hash_entry *h;
4178 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4180 h = (struct elf_link_hash_entry *) p;
4181 entsize += htab->root.table.entsize;
4182 if (h->root.type == bfd_link_hash_warning)
4183 entsize += htab->root.table.entsize;
4187 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4188 old_tab = bfd_malloc (tabsize + entsize);
4189 if (old_tab == NULL)
4190 goto error_free_vers;
4192 /* Remember the current objalloc pointer, so that all mem for
4193 symbols added can later be reclaimed. */
4194 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4195 if (alloc_mark == NULL)
4196 goto error_free_vers;
4198 /* Make a special call to the linker "notice" function to
4199 tell it that we are about to handle an as-needed lib. */
4200 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4201 goto error_free_vers;
4203 /* Clone the symbol table. Remember some pointers into the
4204 symbol table, and dynamic symbol count. */
4205 old_ent = (char *) old_tab + tabsize;
4206 memcpy (old_tab, htab->root.table.table, tabsize);
4207 old_undefs = htab->root.undefs;
4208 old_undefs_tail = htab->root.undefs_tail;
4209 old_table = htab->root.table.table;
4210 old_size = htab->root.table.size;
4211 old_count = htab->root.table.count;
4212 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4213 if (old_strtab == NULL)
4214 goto error_free_vers;
4216 for (i = 0; i < htab->root.table.size; i++)
4218 struct bfd_hash_entry *p;
4219 struct elf_link_hash_entry *h;
4221 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4223 memcpy (old_ent, p, htab->root.table.entsize);
4224 old_ent = (char *) old_ent + htab->root.table.entsize;
4225 h = (struct elf_link_hash_entry *) p;
4226 if (h->root.type == bfd_link_hash_warning)
4228 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4229 old_ent = (char *) old_ent + htab->root.table.entsize;
4236 ever = extversym != NULL ? extversym + extsymoff : NULL;
4237 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4239 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4243 asection *sec, *new_sec;
4246 struct elf_link_hash_entry *h;
4247 struct elf_link_hash_entry *hi;
4248 bfd_boolean definition;
4249 bfd_boolean size_change_ok;
4250 bfd_boolean type_change_ok;
4251 bfd_boolean new_weakdef;
4252 bfd_boolean new_weak;
4253 bfd_boolean old_weak;
4254 bfd_boolean override;
4256 bfd_boolean discarded;
4257 unsigned int old_alignment;
4259 bfd_boolean matched;
4263 flags = BSF_NO_FLAGS;
4265 value = isym->st_value;
4266 common = bed->common_definition (isym);
4267 if (common && info->inhibit_common_definition)
4269 /* Treat common symbol as undefined for --no-define-common. */
4270 isym->st_shndx = SHN_UNDEF;
4275 bind = ELF_ST_BIND (isym->st_info);
4279 /* This should be impossible, since ELF requires that all
4280 global symbols follow all local symbols, and that sh_info
4281 point to the first global symbol. Unfortunately, Irix 5
4286 if (isym->st_shndx != SHN_UNDEF && !common)
4294 case STB_GNU_UNIQUE:
4295 flags = BSF_GNU_UNIQUE;
4299 /* Leave it up to the processor backend. */
4303 if (isym->st_shndx == SHN_UNDEF)
4304 sec = bfd_und_section_ptr;
4305 else if (isym->st_shndx == SHN_ABS)
4306 sec = bfd_abs_section_ptr;
4307 else if (isym->st_shndx == SHN_COMMON)
4309 sec = bfd_com_section_ptr;
4310 /* What ELF calls the size we call the value. What ELF
4311 calls the value we call the alignment. */
4312 value = isym->st_size;
4316 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4318 sec = bfd_abs_section_ptr;
4319 else if (discarded_section (sec))
4321 /* Symbols from discarded section are undefined. We keep
4323 sec = bfd_und_section_ptr;
4325 isym->st_shndx = SHN_UNDEF;
4327 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4331 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4334 goto error_free_vers;
4336 if (isym->st_shndx == SHN_COMMON
4337 && (abfd->flags & BFD_PLUGIN) != 0)
4339 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4343 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4345 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4347 goto error_free_vers;
4351 else if (isym->st_shndx == SHN_COMMON
4352 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4353 && !bfd_link_relocatable (info))
4355 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4359 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4360 | SEC_LINKER_CREATED);
4361 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4363 goto error_free_vers;
4367 else if (bed->elf_add_symbol_hook)
4369 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4371 goto error_free_vers;
4373 /* The hook function sets the name to NULL if this symbol
4374 should be skipped for some reason. */
4379 /* Sanity check that all possibilities were handled. */
4382 bfd_set_error (bfd_error_bad_value);
4383 goto error_free_vers;
4386 /* Silently discard TLS symbols from --just-syms. There's
4387 no way to combine a static TLS block with a new TLS block
4388 for this executable. */
4389 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4390 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4393 if (bfd_is_und_section (sec)
4394 || bfd_is_com_section (sec))
4399 size_change_ok = FALSE;
4400 type_change_ok = bed->type_change_ok;
4407 if (is_elf_hash_table (htab))
4409 Elf_Internal_Versym iver;
4410 unsigned int vernum = 0;
4415 if (info->default_imported_symver)
4416 /* Use the default symbol version created earlier. */
4417 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4422 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4424 vernum = iver.vs_vers & VERSYM_VERSION;
4426 /* If this is a hidden symbol, or if it is not version
4427 1, we append the version name to the symbol name.
4428 However, we do not modify a non-hidden absolute symbol
4429 if it is not a function, because it might be the version
4430 symbol itself. FIXME: What if it isn't? */
4431 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4433 && (!bfd_is_abs_section (sec)
4434 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4437 size_t namelen, verlen, newlen;
4440 if (isym->st_shndx != SHN_UNDEF)
4442 if (vernum > elf_tdata (abfd)->cverdefs)
4444 else if (vernum > 1)
4446 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4453 /* xgettext:c-format */
4454 (_("%B: %s: invalid version %u (max %d)"),
4456 elf_tdata (abfd)->cverdefs);
4457 bfd_set_error (bfd_error_bad_value);
4458 goto error_free_vers;
4463 /* We cannot simply test for the number of
4464 entries in the VERNEED section since the
4465 numbers for the needed versions do not start
4467 Elf_Internal_Verneed *t;
4470 for (t = elf_tdata (abfd)->verref;
4474 Elf_Internal_Vernaux *a;
4476 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4478 if (a->vna_other == vernum)
4480 verstr = a->vna_nodename;
4490 /* xgettext:c-format */
4491 (_("%B: %s: invalid needed version %d"),
4492 abfd, name, vernum);
4493 bfd_set_error (bfd_error_bad_value);
4494 goto error_free_vers;
4498 namelen = strlen (name);
4499 verlen = strlen (verstr);
4500 newlen = namelen + verlen + 2;
4501 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4502 && isym->st_shndx != SHN_UNDEF)
4505 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4506 if (newname == NULL)
4507 goto error_free_vers;
4508 memcpy (newname, name, namelen);
4509 p = newname + namelen;
4511 /* If this is a defined non-hidden version symbol,
4512 we add another @ to the name. This indicates the
4513 default version of the symbol. */
4514 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4515 && isym->st_shndx != SHN_UNDEF)
4517 memcpy (p, verstr, verlen + 1);
4522 /* If this symbol has default visibility and the user has
4523 requested we not re-export it, then mark it as hidden. */
4524 if (!bfd_is_und_section (sec)
4527 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4528 isym->st_other = (STV_HIDDEN
4529 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4531 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4532 sym_hash, &old_bfd, &old_weak,
4533 &old_alignment, &skip, &override,
4534 &type_change_ok, &size_change_ok,
4536 goto error_free_vers;
4541 /* Override a definition only if the new symbol matches the
4543 if (override && matched)
4547 while (h->root.type == bfd_link_hash_indirect
4548 || h->root.type == bfd_link_hash_warning)
4549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4551 if (elf_tdata (abfd)->verdef != NULL
4554 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4557 if (! (_bfd_generic_link_add_one_symbol
4558 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4559 (struct bfd_link_hash_entry **) sym_hash)))
4560 goto error_free_vers;
4562 if ((flags & BSF_GNU_UNIQUE)
4563 && (abfd->flags & DYNAMIC) == 0
4564 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4565 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4568 /* We need to make sure that indirect symbol dynamic flags are
4571 while (h->root.type == bfd_link_hash_indirect
4572 || h->root.type == bfd_link_hash_warning)
4573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4575 /* Setting the index to -3 tells elf_link_output_extsym that
4576 this symbol is defined in a discarded section. */
4582 new_weak = (flags & BSF_WEAK) != 0;
4583 new_weakdef = FALSE;
4587 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4588 && is_elf_hash_table (htab)
4589 && h->u.weakdef == NULL)
4591 /* Keep a list of all weak defined non function symbols from
4592 a dynamic object, using the weakdef field. Later in this
4593 function we will set the weakdef field to the correct
4594 value. We only put non-function symbols from dynamic
4595 objects on this list, because that happens to be the only
4596 time we need to know the normal symbol corresponding to a
4597 weak symbol, and the information is time consuming to
4598 figure out. If the weakdef field is not already NULL,
4599 then this symbol was already defined by some previous
4600 dynamic object, and we will be using that previous
4601 definition anyhow. */
4603 h->u.weakdef = weaks;
4608 /* Set the alignment of a common symbol. */
4609 if ((common || bfd_is_com_section (sec))
4610 && h->root.type == bfd_link_hash_common)
4615 align = bfd_log2 (isym->st_value);
4618 /* The new symbol is a common symbol in a shared object.
4619 We need to get the alignment from the section. */
4620 align = new_sec->alignment_power;
4622 if (align > old_alignment)
4623 h->root.u.c.p->alignment_power = align;
4625 h->root.u.c.p->alignment_power = old_alignment;
4628 if (is_elf_hash_table (htab))
4630 /* Set a flag in the hash table entry indicating the type of
4631 reference or definition we just found. A dynamic symbol
4632 is one which is referenced or defined by both a regular
4633 object and a shared object. */
4634 bfd_boolean dynsym = FALSE;
4636 /* Plugin symbols aren't normal. Don't set def_regular or
4637 ref_regular for them, or make them dynamic. */
4638 if ((abfd->flags & BFD_PLUGIN) != 0)
4645 if (bind != STB_WEAK)
4646 h->ref_regular_nonweak = 1;
4658 /* If the indirect symbol has been forced local, don't
4659 make the real symbol dynamic. */
4660 if ((h == hi || !hi->forced_local)
4661 && (bfd_link_dll (info)
4671 hi->ref_dynamic = 1;
4676 hi->def_dynamic = 1;
4679 /* If the indirect symbol has been forced local, don't
4680 make the real symbol dynamic. */
4681 if ((h == hi || !hi->forced_local)
4684 || (h->u.weakdef != NULL
4686 && h->u.weakdef->dynindx != -1)))
4690 /* Check to see if we need to add an indirect symbol for
4691 the default name. */
4693 || (!override && h->root.type == bfd_link_hash_common))
4694 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4695 sec, value, &old_bfd, &dynsym))
4696 goto error_free_vers;
4698 /* Check the alignment when a common symbol is involved. This
4699 can change when a common symbol is overridden by a normal
4700 definition or a common symbol is ignored due to the old
4701 normal definition. We need to make sure the maximum
4702 alignment is maintained. */
4703 if ((old_alignment || common)
4704 && h->root.type != bfd_link_hash_common)
4706 unsigned int common_align;
4707 unsigned int normal_align;
4708 unsigned int symbol_align;
4712 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4713 || h->root.type == bfd_link_hash_defweak);
4715 symbol_align = ffs (h->root.u.def.value) - 1;
4716 if (h->root.u.def.section->owner != NULL
4717 && (h->root.u.def.section->owner->flags
4718 & (DYNAMIC | BFD_PLUGIN)) == 0)
4720 normal_align = h->root.u.def.section->alignment_power;
4721 if (normal_align > symbol_align)
4722 normal_align = symbol_align;
4725 normal_align = symbol_align;
4729 common_align = old_alignment;
4730 common_bfd = old_bfd;
4735 common_align = bfd_log2 (isym->st_value);
4737 normal_bfd = old_bfd;
4740 if (normal_align < common_align)
4742 /* PR binutils/2735 */
4743 if (normal_bfd == NULL)
4745 /* xgettext:c-format */
4746 (_("Warning: alignment %u of common symbol `%s' in %B is"
4747 " greater than the alignment (%u) of its section %A"),
4748 1 << common_align, name, common_bfd,
4749 1 << normal_align, h->root.u.def.section);
4752 /* xgettext:c-format */
4753 (_("Warning: alignment %u of symbol `%s' in %B"
4754 " is smaller than %u in %B"),
4755 1 << normal_align, name, normal_bfd,
4756 1 << common_align, common_bfd);
4760 /* Remember the symbol size if it isn't undefined. */
4761 if (isym->st_size != 0
4762 && isym->st_shndx != SHN_UNDEF
4763 && (definition || h->size == 0))
4766 && h->size != isym->st_size
4767 && ! size_change_ok)
4769 /* xgettext:c-format */
4770 (_("Warning: size of symbol `%s' changed"
4771 " from %Lu in %B to %Lu in %B"),
4772 name, h->size, old_bfd, isym->st_size, abfd);
4774 h->size = isym->st_size;
4777 /* If this is a common symbol, then we always want H->SIZE
4778 to be the size of the common symbol. The code just above
4779 won't fix the size if a common symbol becomes larger. We
4780 don't warn about a size change here, because that is
4781 covered by --warn-common. Allow changes between different
4783 if (h->root.type == bfd_link_hash_common)
4784 h->size = h->root.u.c.size;
4786 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4787 && ((definition && !new_weak)
4788 || (old_weak && h->root.type == bfd_link_hash_common)
4789 || h->type == STT_NOTYPE))
4791 unsigned int type = ELF_ST_TYPE (isym->st_info);
4793 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4795 if (type == STT_GNU_IFUNC
4796 && (abfd->flags & DYNAMIC) != 0)
4799 if (h->type != type)
4801 if (h->type != STT_NOTYPE && ! type_change_ok)
4802 /* xgettext:c-format */
4804 (_("Warning: type of symbol `%s' changed"
4805 " from %d to %d in %B"),
4806 name, h->type, type, abfd);
4812 /* Merge st_other field. */
4813 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4815 /* We don't want to make debug symbol dynamic. */
4817 && (sec->flags & SEC_DEBUGGING)
4818 && !bfd_link_relocatable (info))
4821 /* Nor should we make plugin symbols dynamic. */
4822 if ((abfd->flags & BFD_PLUGIN) != 0)
4827 h->target_internal = isym->st_target_internal;
4828 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4831 if (definition && !dynamic)
4833 char *p = strchr (name, ELF_VER_CHR);
4834 if (p != NULL && p[1] != ELF_VER_CHR)
4836 /* Queue non-default versions so that .symver x, x@FOO
4837 aliases can be checked. */
4840 amt = ((isymend - isym + 1)
4841 * sizeof (struct elf_link_hash_entry *));
4843 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4845 goto error_free_vers;
4847 nondeflt_vers[nondeflt_vers_cnt++] = h;
4851 if (dynsym && h->dynindx == -1)
4853 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4854 goto error_free_vers;
4855 if (h->u.weakdef != NULL
4857 && h->u.weakdef->dynindx == -1)
4859 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4860 goto error_free_vers;
4863 else if (h->dynindx != -1)
4864 /* If the symbol already has a dynamic index, but
4865 visibility says it should not be visible, turn it into
4867 switch (ELF_ST_VISIBILITY (h->other))
4871 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4876 /* Don't add DT_NEEDED for references from the dummy bfd nor
4877 for unmatched symbol. */
4882 && h->ref_regular_nonweak
4884 || (old_bfd->flags & BFD_PLUGIN) == 0))
4885 || (h->ref_dynamic_nonweak
4886 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4887 && !on_needed_list (elf_dt_name (abfd),
4888 htab->needed, NULL))))
4891 const char *soname = elf_dt_name (abfd);
4893 info->callbacks->minfo ("%!", soname, old_bfd,
4894 h->root.root.string);
4896 /* A symbol from a library loaded via DT_NEEDED of some
4897 other library is referenced by a regular object.
4898 Add a DT_NEEDED entry for it. Issue an error if
4899 --no-add-needed is used and the reference was not
4902 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4905 /* xgettext:c-format */
4906 (_("%B: undefined reference to symbol '%s'"),
4908 bfd_set_error (bfd_error_missing_dso);
4909 goto error_free_vers;
4912 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4913 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4916 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4918 goto error_free_vers;
4920 BFD_ASSERT (ret == 0);
4925 if (extversym != NULL)
4931 if (isymbuf != NULL)
4937 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4941 /* Restore the symbol table. */
4942 old_ent = (char *) old_tab + tabsize;
4943 memset (elf_sym_hashes (abfd), 0,
4944 extsymcount * sizeof (struct elf_link_hash_entry *));
4945 htab->root.table.table = old_table;
4946 htab->root.table.size = old_size;
4947 htab->root.table.count = old_count;
4948 memcpy (htab->root.table.table, old_tab, tabsize);
4949 htab->root.undefs = old_undefs;
4950 htab->root.undefs_tail = old_undefs_tail;
4951 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4954 for (i = 0; i < htab->root.table.size; i++)
4956 struct bfd_hash_entry *p;
4957 struct elf_link_hash_entry *h;
4959 unsigned int alignment_power;
4960 unsigned int non_ir_ref_dynamic;
4962 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4964 h = (struct elf_link_hash_entry *) p;
4965 if (h->root.type == bfd_link_hash_warning)
4966 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4968 /* Preserve the maximum alignment and size for common
4969 symbols even if this dynamic lib isn't on DT_NEEDED
4970 since it can still be loaded at run time by another
4972 if (h->root.type == bfd_link_hash_common)
4974 size = h->root.u.c.size;
4975 alignment_power = h->root.u.c.p->alignment_power;
4980 alignment_power = 0;
4982 /* Preserve non_ir_ref_dynamic so that this symbol
4983 will be exported when the dynamic lib becomes needed
4984 in the second pass. */
4985 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
4986 memcpy (p, old_ent, htab->root.table.entsize);
4987 old_ent = (char *) old_ent + htab->root.table.entsize;
4988 h = (struct elf_link_hash_entry *) p;
4989 if (h->root.type == bfd_link_hash_warning)
4991 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4992 old_ent = (char *) old_ent + htab->root.table.entsize;
4993 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4995 if (h->root.type == bfd_link_hash_common)
4997 if (size > h->root.u.c.size)
4998 h->root.u.c.size = size;
4999 if (alignment_power > h->root.u.c.p->alignment_power)
5000 h->root.u.c.p->alignment_power = alignment_power;
5002 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5006 /* Make a special call to the linker "notice" function to
5007 tell it that symbols added for crefs may need to be removed. */
5008 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5009 goto error_free_vers;
5012 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5014 if (nondeflt_vers != NULL)
5015 free (nondeflt_vers);
5019 if (old_tab != NULL)
5021 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5022 goto error_free_vers;
5027 /* Now that all the symbols from this input file are created, if
5028 not performing a relocatable link, handle .symver foo, foo@BAR
5029 such that any relocs against foo become foo@BAR. */
5030 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5034 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5036 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5037 char *shortname, *p;
5039 p = strchr (h->root.root.string, ELF_VER_CHR);
5041 || (h->root.type != bfd_link_hash_defined
5042 && h->root.type != bfd_link_hash_defweak))
5045 amt = p - h->root.root.string;
5046 shortname = (char *) bfd_malloc (amt + 1);
5048 goto error_free_vers;
5049 memcpy (shortname, h->root.root.string, amt);
5050 shortname[amt] = '\0';
5052 hi = (struct elf_link_hash_entry *)
5053 bfd_link_hash_lookup (&htab->root, shortname,
5054 FALSE, FALSE, FALSE);
5056 && hi->root.type == h->root.type
5057 && hi->root.u.def.value == h->root.u.def.value
5058 && hi->root.u.def.section == h->root.u.def.section)
5060 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5061 hi->root.type = bfd_link_hash_indirect;
5062 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5063 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5064 sym_hash = elf_sym_hashes (abfd);
5066 for (symidx = 0; symidx < extsymcount; ++symidx)
5067 if (sym_hash[symidx] == hi)
5069 sym_hash[symidx] = h;
5075 free (nondeflt_vers);
5076 nondeflt_vers = NULL;
5079 /* Now set the weakdefs field correctly for all the weak defined
5080 symbols we found. The only way to do this is to search all the
5081 symbols. Since we only need the information for non functions in
5082 dynamic objects, that's the only time we actually put anything on
5083 the list WEAKS. We need this information so that if a regular
5084 object refers to a symbol defined weakly in a dynamic object, the
5085 real symbol in the dynamic object is also put in the dynamic
5086 symbols; we also must arrange for both symbols to point to the
5087 same memory location. We could handle the general case of symbol
5088 aliasing, but a general symbol alias can only be generated in
5089 assembler code, handling it correctly would be very time
5090 consuming, and other ELF linkers don't handle general aliasing
5094 struct elf_link_hash_entry **hpp;
5095 struct elf_link_hash_entry **hppend;
5096 struct elf_link_hash_entry **sorted_sym_hash;
5097 struct elf_link_hash_entry *h;
5100 /* Since we have to search the whole symbol list for each weak
5101 defined symbol, search time for N weak defined symbols will be
5102 O(N^2). Binary search will cut it down to O(NlogN). */
5104 amt *= sizeof (struct elf_link_hash_entry *);
5105 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5106 if (sorted_sym_hash == NULL)
5108 sym_hash = sorted_sym_hash;
5109 hpp = elf_sym_hashes (abfd);
5110 hppend = hpp + extsymcount;
5112 for (; hpp < hppend; hpp++)
5116 && h->root.type == bfd_link_hash_defined
5117 && !bed->is_function_type (h->type))
5125 qsort (sorted_sym_hash, sym_count,
5126 sizeof (struct elf_link_hash_entry *),
5129 while (weaks != NULL)
5131 struct elf_link_hash_entry *hlook;
5134 size_t i, j, idx = 0;
5137 weaks = hlook->u.weakdef;
5138 hlook->u.weakdef = NULL;
5140 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5141 || hlook->root.type == bfd_link_hash_defweak
5142 || hlook->root.type == bfd_link_hash_common
5143 || hlook->root.type == bfd_link_hash_indirect);
5144 slook = hlook->root.u.def.section;
5145 vlook = hlook->root.u.def.value;
5151 bfd_signed_vma vdiff;
5153 h = sorted_sym_hash[idx];
5154 vdiff = vlook - h->root.u.def.value;
5161 int sdiff = slook->id - h->root.u.def.section->id;
5171 /* We didn't find a value/section match. */
5175 /* With multiple aliases, or when the weak symbol is already
5176 strongly defined, we have multiple matching symbols and
5177 the binary search above may land on any of them. Step
5178 one past the matching symbol(s). */
5181 h = sorted_sym_hash[idx];
5182 if (h->root.u.def.section != slook
5183 || h->root.u.def.value != vlook)
5187 /* Now look back over the aliases. Since we sorted by size
5188 as well as value and section, we'll choose the one with
5189 the largest size. */
5192 h = sorted_sym_hash[idx];
5194 /* Stop if value or section doesn't match. */
5195 if (h->root.u.def.section != slook
5196 || h->root.u.def.value != vlook)
5198 else if (h != hlook)
5200 hlook->u.weakdef = h;
5202 /* If the weak definition is in the list of dynamic
5203 symbols, make sure the real definition is put
5205 if (hlook->dynindx != -1 && h->dynindx == -1)
5207 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5210 free (sorted_sym_hash);
5215 /* If the real definition is in the list of dynamic
5216 symbols, make sure the weak definition is put
5217 there as well. If we don't do this, then the
5218 dynamic loader might not merge the entries for the
5219 real definition and the weak definition. */
5220 if (h->dynindx != -1 && hlook->dynindx == -1)
5222 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5223 goto err_free_sym_hash;
5230 free (sorted_sym_hash);
5233 if (bed->check_directives
5234 && !(*bed->check_directives) (abfd, info))
5237 if (!info->check_relocs_after_open_input
5238 && !_bfd_elf_link_check_relocs (abfd, info))
5241 /* If this is a non-traditional link, try to optimize the handling
5242 of the .stab/.stabstr sections. */
5244 && ! info->traditional_format
5245 && is_elf_hash_table (htab)
5246 && (info->strip != strip_all && info->strip != strip_debugger))
5250 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5251 if (stabstr != NULL)
5253 bfd_size_type string_offset = 0;
5256 for (stab = abfd->sections; stab; stab = stab->next)
5257 if (CONST_STRNEQ (stab->name, ".stab")
5258 && (!stab->name[5] ||
5259 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5260 && (stab->flags & SEC_MERGE) == 0
5261 && !bfd_is_abs_section (stab->output_section))
5263 struct bfd_elf_section_data *secdata;
5265 secdata = elf_section_data (stab);
5266 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5267 stabstr, &secdata->sec_info,
5270 if (secdata->sec_info)
5271 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5276 if (is_elf_hash_table (htab) && add_needed)
5278 /* Add this bfd to the loaded list. */
5279 struct elf_link_loaded_list *n;
5281 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5285 n->next = htab->loaded;
5292 if (old_tab != NULL)
5294 if (old_strtab != NULL)
5296 if (nondeflt_vers != NULL)
5297 free (nondeflt_vers);
5298 if (extversym != NULL)
5301 if (isymbuf != NULL)
5307 /* Return the linker hash table entry of a symbol that might be
5308 satisfied by an archive symbol. Return -1 on error. */
5310 struct elf_link_hash_entry *
5311 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5312 struct bfd_link_info *info,
5315 struct elf_link_hash_entry *h;
5319 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5323 /* If this is a default version (the name contains @@), look up the
5324 symbol again with only one `@' as well as without the version.
5325 The effect is that references to the symbol with and without the
5326 version will be matched by the default symbol in the archive. */
5328 p = strchr (name, ELF_VER_CHR);
5329 if (p == NULL || p[1] != ELF_VER_CHR)
5332 /* First check with only one `@'. */
5333 len = strlen (name);
5334 copy = (char *) bfd_alloc (abfd, len);
5336 return (struct elf_link_hash_entry *) 0 - 1;
5338 first = p - name + 1;
5339 memcpy (copy, name, first);
5340 memcpy (copy + first, name + first + 1, len - first);
5342 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5345 /* We also need to check references to the symbol without the
5347 copy[first - 1] = '\0';
5348 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5349 FALSE, FALSE, TRUE);
5352 bfd_release (abfd, copy);
5356 /* Add symbols from an ELF archive file to the linker hash table. We
5357 don't use _bfd_generic_link_add_archive_symbols because we need to
5358 handle versioned symbols.
5360 Fortunately, ELF archive handling is simpler than that done by
5361 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5362 oddities. In ELF, if we find a symbol in the archive map, and the
5363 symbol is currently undefined, we know that we must pull in that
5366 Unfortunately, we do have to make multiple passes over the symbol
5367 table until nothing further is resolved. */
5370 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5373 unsigned char *included = NULL;
5377 const struct elf_backend_data *bed;
5378 struct elf_link_hash_entry * (*archive_symbol_lookup)
5379 (bfd *, struct bfd_link_info *, const char *);
5381 if (! bfd_has_map (abfd))
5383 /* An empty archive is a special case. */
5384 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5386 bfd_set_error (bfd_error_no_armap);
5390 /* Keep track of all symbols we know to be already defined, and all
5391 files we know to be already included. This is to speed up the
5392 second and subsequent passes. */
5393 c = bfd_ardata (abfd)->symdef_count;
5397 amt *= sizeof (*included);
5398 included = (unsigned char *) bfd_zmalloc (amt);
5399 if (included == NULL)
5402 symdefs = bfd_ardata (abfd)->symdefs;
5403 bed = get_elf_backend_data (abfd);
5404 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5417 symdefend = symdef + c;
5418 for (i = 0; symdef < symdefend; symdef++, i++)
5420 struct elf_link_hash_entry *h;
5422 struct bfd_link_hash_entry *undefs_tail;
5427 if (symdef->file_offset == last)
5433 h = archive_symbol_lookup (abfd, info, symdef->name);
5434 if (h == (struct elf_link_hash_entry *) 0 - 1)
5440 if (h->root.type == bfd_link_hash_common)
5442 /* We currently have a common symbol. The archive map contains
5443 a reference to this symbol, so we may want to include it. We
5444 only want to include it however, if this archive element
5445 contains a definition of the symbol, not just another common
5448 Unfortunately some archivers (including GNU ar) will put
5449 declarations of common symbols into their archive maps, as
5450 well as real definitions, so we cannot just go by the archive
5451 map alone. Instead we must read in the element's symbol
5452 table and check that to see what kind of symbol definition
5454 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5457 else if (h->root.type != bfd_link_hash_undefined)
5459 if (h->root.type != bfd_link_hash_undefweak)
5460 /* Symbol must be defined. Don't check it again. */
5465 /* We need to include this archive member. */
5466 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5467 if (element == NULL)
5470 if (! bfd_check_format (element, bfd_object))
5473 undefs_tail = info->hash->undefs_tail;
5475 if (!(*info->callbacks
5476 ->add_archive_element) (info, element, symdef->name, &element))
5478 if (!bfd_link_add_symbols (element, info))
5481 /* If there are any new undefined symbols, we need to make
5482 another pass through the archive in order to see whether
5483 they can be defined. FIXME: This isn't perfect, because
5484 common symbols wind up on undefs_tail and because an
5485 undefined symbol which is defined later on in this pass
5486 does not require another pass. This isn't a bug, but it
5487 does make the code less efficient than it could be. */
5488 if (undefs_tail != info->hash->undefs_tail)
5491 /* Look backward to mark all symbols from this object file
5492 which we have already seen in this pass. */
5496 included[mark] = TRUE;
5501 while (symdefs[mark].file_offset == symdef->file_offset);
5503 /* We mark subsequent symbols from this object file as we go
5504 on through the loop. */
5505 last = symdef->file_offset;
5515 if (included != NULL)
5520 /* Given an ELF BFD, add symbols to the global hash table as
5524 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5526 switch (bfd_get_format (abfd))
5529 return elf_link_add_object_symbols (abfd, info);
5531 return elf_link_add_archive_symbols (abfd, info);
5533 bfd_set_error (bfd_error_wrong_format);
5538 struct hash_codes_info
5540 unsigned long *hashcodes;
5544 /* This function will be called though elf_link_hash_traverse to store
5545 all hash value of the exported symbols in an array. */
5548 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5550 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5555 /* Ignore indirect symbols. These are added by the versioning code. */
5556 if (h->dynindx == -1)
5559 name = h->root.root.string;
5560 if (h->versioned >= versioned)
5562 char *p = strchr (name, ELF_VER_CHR);
5565 alc = (char *) bfd_malloc (p - name + 1);
5571 memcpy (alc, name, p - name);
5572 alc[p - name] = '\0';
5577 /* Compute the hash value. */
5578 ha = bfd_elf_hash (name);
5580 /* Store the found hash value in the array given as the argument. */
5581 *(inf->hashcodes)++ = ha;
5583 /* And store it in the struct so that we can put it in the hash table
5585 h->u.elf_hash_value = ha;
5593 struct collect_gnu_hash_codes
5596 const struct elf_backend_data *bed;
5597 unsigned long int nsyms;
5598 unsigned long int maskbits;
5599 unsigned long int *hashcodes;
5600 unsigned long int *hashval;
5601 unsigned long int *indx;
5602 unsigned long int *counts;
5605 long int min_dynindx;
5606 unsigned long int bucketcount;
5607 unsigned long int symindx;
5608 long int local_indx;
5609 long int shift1, shift2;
5610 unsigned long int mask;
5614 /* This function will be called though elf_link_hash_traverse to store
5615 all hash value of the exported symbols in an array. */
5618 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5620 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5625 /* Ignore indirect symbols. These are added by the versioning code. */
5626 if (h->dynindx == -1)
5629 /* Ignore also local symbols and undefined symbols. */
5630 if (! (*s->bed->elf_hash_symbol) (h))
5633 name = h->root.root.string;
5634 if (h->versioned >= versioned)
5636 char *p = strchr (name, ELF_VER_CHR);
5639 alc = (char *) bfd_malloc (p - name + 1);
5645 memcpy (alc, name, p - name);
5646 alc[p - name] = '\0';
5651 /* Compute the hash value. */
5652 ha = bfd_elf_gnu_hash (name);
5654 /* Store the found hash value in the array for compute_bucket_count,
5655 and also for .dynsym reordering purposes. */
5656 s->hashcodes[s->nsyms] = ha;
5657 s->hashval[h->dynindx] = ha;
5659 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5660 s->min_dynindx = h->dynindx;
5668 /* This function will be called though elf_link_hash_traverse to do
5669 final dynaminc symbol renumbering. */
5672 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5674 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5675 unsigned long int bucket;
5676 unsigned long int val;
5678 /* Ignore indirect symbols. */
5679 if (h->dynindx == -1)
5682 /* Ignore also local symbols and undefined symbols. */
5683 if (! (*s->bed->elf_hash_symbol) (h))
5685 if (h->dynindx >= s->min_dynindx)
5686 h->dynindx = s->local_indx++;
5690 bucket = s->hashval[h->dynindx] % s->bucketcount;
5691 val = (s->hashval[h->dynindx] >> s->shift1)
5692 & ((s->maskbits >> s->shift1) - 1);
5693 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5695 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5696 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5697 if (s->counts[bucket] == 1)
5698 /* Last element terminates the chain. */
5700 bfd_put_32 (s->output_bfd, val,
5701 s->contents + (s->indx[bucket] - s->symindx) * 4);
5702 --s->counts[bucket];
5703 h->dynindx = s->indx[bucket]++;
5707 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5710 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5712 return !(h->forced_local
5713 || h->root.type == bfd_link_hash_undefined
5714 || h->root.type == bfd_link_hash_undefweak
5715 || ((h->root.type == bfd_link_hash_defined
5716 || h->root.type == bfd_link_hash_defweak)
5717 && h->root.u.def.section->output_section == NULL));
5720 /* Array used to determine the number of hash table buckets to use
5721 based on the number of symbols there are. If there are fewer than
5722 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5723 fewer than 37 we use 17 buckets, and so forth. We never use more
5724 than 32771 buckets. */
5726 static const size_t elf_buckets[] =
5728 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5732 /* Compute bucket count for hashing table. We do not use a static set
5733 of possible tables sizes anymore. Instead we determine for all
5734 possible reasonable sizes of the table the outcome (i.e., the
5735 number of collisions etc) and choose the best solution. The
5736 weighting functions are not too simple to allow the table to grow
5737 without bounds. Instead one of the weighting factors is the size.
5738 Therefore the result is always a good payoff between few collisions
5739 (= short chain lengths) and table size. */
5741 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5742 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5743 unsigned long int nsyms,
5746 size_t best_size = 0;
5747 unsigned long int i;
5749 /* We have a problem here. The following code to optimize the table
5750 size requires an integer type with more the 32 bits. If
5751 BFD_HOST_U_64_BIT is set we know about such a type. */
5752 #ifdef BFD_HOST_U_64_BIT
5757 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5758 bfd *dynobj = elf_hash_table (info)->dynobj;
5759 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5760 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5761 unsigned long int *counts;
5763 unsigned int no_improvement_count = 0;
5765 /* Possible optimization parameters: if we have NSYMS symbols we say
5766 that the hashing table must at least have NSYMS/4 and at most
5768 minsize = nsyms / 4;
5771 best_size = maxsize = nsyms * 2;
5776 if ((best_size & 31) == 0)
5780 /* Create array where we count the collisions in. We must use bfd_malloc
5781 since the size could be large. */
5783 amt *= sizeof (unsigned long int);
5784 counts = (unsigned long int *) bfd_malloc (amt);
5788 /* Compute the "optimal" size for the hash table. The criteria is a
5789 minimal chain length. The minor criteria is (of course) the size
5791 for (i = minsize; i < maxsize; ++i)
5793 /* Walk through the array of hashcodes and count the collisions. */
5794 BFD_HOST_U_64_BIT max;
5795 unsigned long int j;
5796 unsigned long int fact;
5798 if (gnu_hash && (i & 31) == 0)
5801 memset (counts, '\0', i * sizeof (unsigned long int));
5803 /* Determine how often each hash bucket is used. */
5804 for (j = 0; j < nsyms; ++j)
5805 ++counts[hashcodes[j] % i];
5807 /* For the weight function we need some information about the
5808 pagesize on the target. This is information need not be 100%
5809 accurate. Since this information is not available (so far) we
5810 define it here to a reasonable default value. If it is crucial
5811 to have a better value some day simply define this value. */
5812 # ifndef BFD_TARGET_PAGESIZE
5813 # define BFD_TARGET_PAGESIZE (4096)
5816 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5818 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5821 /* Variant 1: optimize for short chains. We add the squares
5822 of all the chain lengths (which favors many small chain
5823 over a few long chains). */
5824 for (j = 0; j < i; ++j)
5825 max += counts[j] * counts[j];
5827 /* This adds penalties for the overall size of the table. */
5828 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5831 /* Variant 2: Optimize a lot more for small table. Here we
5832 also add squares of the size but we also add penalties for
5833 empty slots (the +1 term). */
5834 for (j = 0; j < i; ++j)
5835 max += (1 + counts[j]) * (1 + counts[j]);
5837 /* The overall size of the table is considered, but not as
5838 strong as in variant 1, where it is squared. */
5839 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5843 /* Compare with current best results. */
5844 if (max < best_chlen)
5848 no_improvement_count = 0;
5850 /* PR 11843: Avoid futile long searches for the best bucket size
5851 when there are a large number of symbols. */
5852 else if (++no_improvement_count == 100)
5859 #endif /* defined (BFD_HOST_U_64_BIT) */
5861 /* This is the fallback solution if no 64bit type is available or if we
5862 are not supposed to spend much time on optimizations. We select the
5863 bucket count using a fixed set of numbers. */
5864 for (i = 0; elf_buckets[i] != 0; i++)
5866 best_size = elf_buckets[i];
5867 if (nsyms < elf_buckets[i + 1])
5870 if (gnu_hash && best_size < 2)
5877 /* Size any SHT_GROUP section for ld -r. */
5880 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5885 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5886 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5887 && (s = ibfd->sections) != NULL
5888 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
5889 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5894 /* Set a default stack segment size. The value in INFO wins. If it
5895 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5896 undefined it is initialized. */
5899 bfd_elf_stack_segment_size (bfd *output_bfd,
5900 struct bfd_link_info *info,
5901 const char *legacy_symbol,
5902 bfd_vma default_size)
5904 struct elf_link_hash_entry *h = NULL;
5906 /* Look for legacy symbol. */
5908 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5909 FALSE, FALSE, FALSE);
5910 if (h && (h->root.type == bfd_link_hash_defined
5911 || h->root.type == bfd_link_hash_defweak)
5913 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5915 /* The symbol has no type if specified on the command line. */
5916 h->type = STT_OBJECT;
5917 if (info->stacksize)
5918 /* xgettext:c-format */
5919 _bfd_error_handler (_("%B: stack size specified and %s set"),
5920 output_bfd, legacy_symbol);
5921 else if (h->root.u.def.section != bfd_abs_section_ptr)
5922 /* xgettext:c-format */
5923 _bfd_error_handler (_("%B: %s not absolute"),
5924 output_bfd, legacy_symbol);
5926 info->stacksize = h->root.u.def.value;
5929 if (!info->stacksize)
5930 /* If the user didn't set a size, or explicitly inhibit the
5931 size, set it now. */
5932 info->stacksize = default_size;
5934 /* Provide the legacy symbol, if it is referenced. */
5935 if (h && (h->root.type == bfd_link_hash_undefined
5936 || h->root.type == bfd_link_hash_undefweak))
5938 struct bfd_link_hash_entry *bh = NULL;
5940 if (!(_bfd_generic_link_add_one_symbol
5941 (info, output_bfd, legacy_symbol,
5942 BSF_GLOBAL, bfd_abs_section_ptr,
5943 info->stacksize >= 0 ? info->stacksize : 0,
5944 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5947 h = (struct elf_link_hash_entry *) bh;
5949 h->type = STT_OBJECT;
5955 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5957 struct elf_gc_sweep_symbol_info
5959 struct bfd_link_info *info;
5960 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5965 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5968 && (((h->root.type == bfd_link_hash_defined
5969 || h->root.type == bfd_link_hash_defweak)
5970 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5971 && h->root.u.def.section->gc_mark))
5972 || h->root.type == bfd_link_hash_undefined
5973 || h->root.type == bfd_link_hash_undefweak))
5975 struct elf_gc_sweep_symbol_info *inf;
5977 inf = (struct elf_gc_sweep_symbol_info *) data;
5978 (*inf->hide_symbol) (inf->info, h, TRUE);
5981 h->ref_regular_nonweak = 0;
5987 /* Set up the sizes and contents of the ELF dynamic sections. This is
5988 called by the ELF linker emulation before_allocation routine. We
5989 must set the sizes of the sections before the linker sets the
5990 addresses of the various sections. */
5993 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5996 const char *filter_shlib,
5998 const char *depaudit,
5999 const char * const *auxiliary_filters,
6000 struct bfd_link_info *info,
6001 asection **sinterpptr)
6004 const struct elf_backend_data *bed;
6008 if (!is_elf_hash_table (info->hash))
6011 dynobj = elf_hash_table (info)->dynobj;
6013 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6015 struct bfd_elf_version_tree *verdefs;
6016 struct elf_info_failed asvinfo;
6017 struct bfd_elf_version_tree *t;
6018 struct bfd_elf_version_expr *d;
6022 /* If we are supposed to export all symbols into the dynamic symbol
6023 table (this is not the normal case), then do so. */
6024 if (info->export_dynamic
6025 || (bfd_link_executable (info) && info->dynamic))
6027 struct elf_info_failed eif;
6031 elf_link_hash_traverse (elf_hash_table (info),
6032 _bfd_elf_export_symbol,
6040 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6042 if (soname_indx == (size_t) -1
6043 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6047 soname_indx = (size_t) -1;
6049 /* Make all global versions with definition. */
6050 for (t = info->version_info; t != NULL; t = t->next)
6051 for (d = t->globals.list; d != NULL; d = d->next)
6052 if (!d->symver && d->literal)
6054 const char *verstr, *name;
6055 size_t namelen, verlen, newlen;
6056 char *newname, *p, leading_char;
6057 struct elf_link_hash_entry *newh;
6059 leading_char = bfd_get_symbol_leading_char (output_bfd);
6061 namelen = strlen (name) + (leading_char != '\0');
6063 verlen = strlen (verstr);
6064 newlen = namelen + verlen + 3;
6066 newname = (char *) bfd_malloc (newlen);
6067 if (newname == NULL)
6069 newname[0] = leading_char;
6070 memcpy (newname + (leading_char != '\0'), name, namelen);
6072 /* Check the hidden versioned definition. */
6073 p = newname + namelen;
6075 memcpy (p, verstr, verlen + 1);
6076 newh = elf_link_hash_lookup (elf_hash_table (info),
6077 newname, FALSE, FALSE,
6080 || (newh->root.type != bfd_link_hash_defined
6081 && newh->root.type != bfd_link_hash_defweak))
6083 /* Check the default versioned definition. */
6085 memcpy (p, verstr, verlen + 1);
6086 newh = elf_link_hash_lookup (elf_hash_table (info),
6087 newname, FALSE, FALSE,
6092 /* Mark this version if there is a definition and it is
6093 not defined in a shared object. */
6095 && !newh->def_dynamic
6096 && (newh->root.type == bfd_link_hash_defined
6097 || newh->root.type == bfd_link_hash_defweak))
6101 /* Attach all the symbols to their version information. */
6102 asvinfo.info = info;
6103 asvinfo.failed = FALSE;
6105 elf_link_hash_traverse (elf_hash_table (info),
6106 _bfd_elf_link_assign_sym_version,
6111 if (!info->allow_undefined_version)
6113 /* Check if all global versions have a definition. */
6114 bfd_boolean all_defined = TRUE;
6115 for (t = info->version_info; t != NULL; t = t->next)
6116 for (d = t->globals.list; d != NULL; d = d->next)
6117 if (d->literal && !d->symver && !d->script)
6120 (_("%s: undefined version: %s"),
6121 d->pattern, t->name);
6122 all_defined = FALSE;
6127 bfd_set_error (bfd_error_bad_value);
6132 /* Set up the version definition section. */
6133 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6134 BFD_ASSERT (s != NULL);
6136 /* We may have created additional version definitions if we are
6137 just linking a regular application. */
6138 verdefs = info->version_info;
6140 /* Skip anonymous version tag. */
6141 if (verdefs != NULL && verdefs->vernum == 0)
6142 verdefs = verdefs->next;
6144 if (verdefs == NULL && !info->create_default_symver)
6145 s->flags |= SEC_EXCLUDE;
6151 Elf_Internal_Verdef def;
6152 Elf_Internal_Verdaux defaux;
6153 struct bfd_link_hash_entry *bh;
6154 struct elf_link_hash_entry *h;
6160 /* Make space for the base version. */
6161 size += sizeof (Elf_External_Verdef);
6162 size += sizeof (Elf_External_Verdaux);
6165 /* Make space for the default version. */
6166 if (info->create_default_symver)
6168 size += sizeof (Elf_External_Verdef);
6172 for (t = verdefs; t != NULL; t = t->next)
6174 struct bfd_elf_version_deps *n;
6176 /* Don't emit base version twice. */
6180 size += sizeof (Elf_External_Verdef);
6181 size += sizeof (Elf_External_Verdaux);
6184 for (n = t->deps; n != NULL; n = n->next)
6185 size += sizeof (Elf_External_Verdaux);
6189 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6190 if (s->contents == NULL && s->size != 0)
6193 /* Fill in the version definition section. */
6197 def.vd_version = VER_DEF_CURRENT;
6198 def.vd_flags = VER_FLG_BASE;
6201 if (info->create_default_symver)
6203 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6204 def.vd_next = sizeof (Elf_External_Verdef);
6208 def.vd_aux = sizeof (Elf_External_Verdef);
6209 def.vd_next = (sizeof (Elf_External_Verdef)
6210 + sizeof (Elf_External_Verdaux));
6213 if (soname_indx != (size_t) -1)
6215 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6217 def.vd_hash = bfd_elf_hash (soname);
6218 defaux.vda_name = soname_indx;
6225 name = lbasename (output_bfd->filename);
6226 def.vd_hash = bfd_elf_hash (name);
6227 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6229 if (indx == (size_t) -1)
6231 defaux.vda_name = indx;
6233 defaux.vda_next = 0;
6235 _bfd_elf_swap_verdef_out (output_bfd, &def,
6236 (Elf_External_Verdef *) p);
6237 p += sizeof (Elf_External_Verdef);
6238 if (info->create_default_symver)
6240 /* Add a symbol representing this version. */
6242 if (! (_bfd_generic_link_add_one_symbol
6243 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6245 get_elf_backend_data (dynobj)->collect, &bh)))
6247 h = (struct elf_link_hash_entry *) bh;
6250 h->type = STT_OBJECT;
6251 h->verinfo.vertree = NULL;
6253 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6256 /* Create a duplicate of the base version with the same
6257 aux block, but different flags. */
6260 def.vd_aux = sizeof (Elf_External_Verdef);
6262 def.vd_next = (sizeof (Elf_External_Verdef)
6263 + sizeof (Elf_External_Verdaux));
6266 _bfd_elf_swap_verdef_out (output_bfd, &def,
6267 (Elf_External_Verdef *) p);
6268 p += sizeof (Elf_External_Verdef);
6270 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6271 (Elf_External_Verdaux *) p);
6272 p += sizeof (Elf_External_Verdaux);
6274 for (t = verdefs; t != NULL; t = t->next)
6277 struct bfd_elf_version_deps *n;
6279 /* Don't emit the base version twice. */
6284 for (n = t->deps; n != NULL; n = n->next)
6287 /* Add a symbol representing this version. */
6289 if (! (_bfd_generic_link_add_one_symbol
6290 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6292 get_elf_backend_data (dynobj)->collect, &bh)))
6294 h = (struct elf_link_hash_entry *) bh;
6297 h->type = STT_OBJECT;
6298 h->verinfo.vertree = t;
6300 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6303 def.vd_version = VER_DEF_CURRENT;
6305 if (t->globals.list == NULL
6306 && t->locals.list == NULL
6308 def.vd_flags |= VER_FLG_WEAK;
6309 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6310 def.vd_cnt = cdeps + 1;
6311 def.vd_hash = bfd_elf_hash (t->name);
6312 def.vd_aux = sizeof (Elf_External_Verdef);
6315 /* If a basever node is next, it *must* be the last node in
6316 the chain, otherwise Verdef construction breaks. */
6317 if (t->next != NULL && t->next->vernum == 0)
6318 BFD_ASSERT (t->next->next == NULL);
6320 if (t->next != NULL && t->next->vernum != 0)
6321 def.vd_next = (sizeof (Elf_External_Verdef)
6322 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6324 _bfd_elf_swap_verdef_out (output_bfd, &def,
6325 (Elf_External_Verdef *) p);
6326 p += sizeof (Elf_External_Verdef);
6328 defaux.vda_name = h->dynstr_index;
6329 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6331 defaux.vda_next = 0;
6332 if (t->deps != NULL)
6333 defaux.vda_next = sizeof (Elf_External_Verdaux);
6334 t->name_indx = defaux.vda_name;
6336 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6337 (Elf_External_Verdaux *) p);
6338 p += sizeof (Elf_External_Verdaux);
6340 for (n = t->deps; n != NULL; n = n->next)
6342 if (n->version_needed == NULL)
6344 /* This can happen if there was an error in the
6346 defaux.vda_name = 0;
6350 defaux.vda_name = n->version_needed->name_indx;
6351 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6354 if (n->next == NULL)
6355 defaux.vda_next = 0;
6357 defaux.vda_next = sizeof (Elf_External_Verdaux);
6359 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6360 (Elf_External_Verdaux *) p);
6361 p += sizeof (Elf_External_Verdaux);
6365 elf_tdata (output_bfd)->cverdefs = cdefs;
6369 bed = get_elf_backend_data (output_bfd);
6371 if (info->gc_sections && bed->can_gc_sections)
6373 struct elf_gc_sweep_symbol_info sweep_info;
6375 /* Remove the symbols that were in the swept sections from the
6376 dynamic symbol table. */
6377 sweep_info.info = info;
6378 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6379 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6383 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6386 struct elf_find_verdep_info sinfo;
6388 /* Work out the size of the version reference section. */
6390 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6391 BFD_ASSERT (s != NULL);
6394 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6395 if (sinfo.vers == 0)
6397 sinfo.failed = FALSE;
6399 elf_link_hash_traverse (elf_hash_table (info),
6400 _bfd_elf_link_find_version_dependencies,
6405 if (elf_tdata (output_bfd)->verref == NULL)
6406 s->flags |= SEC_EXCLUDE;
6409 Elf_Internal_Verneed *vn;
6414 /* Build the version dependency section. */
6417 for (vn = elf_tdata (output_bfd)->verref;
6419 vn = vn->vn_nextref)
6421 Elf_Internal_Vernaux *a;
6423 size += sizeof (Elf_External_Verneed);
6425 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6426 size += sizeof (Elf_External_Vernaux);
6430 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6431 if (s->contents == NULL)
6435 for (vn = elf_tdata (output_bfd)->verref;
6437 vn = vn->vn_nextref)
6440 Elf_Internal_Vernaux *a;
6444 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6447 vn->vn_version = VER_NEED_CURRENT;
6449 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6450 elf_dt_name (vn->vn_bfd) != NULL
6451 ? elf_dt_name (vn->vn_bfd)
6452 : lbasename (vn->vn_bfd->filename),
6454 if (indx == (size_t) -1)
6457 vn->vn_aux = sizeof (Elf_External_Verneed);
6458 if (vn->vn_nextref == NULL)
6461 vn->vn_next = (sizeof (Elf_External_Verneed)
6462 + caux * sizeof (Elf_External_Vernaux));
6464 _bfd_elf_swap_verneed_out (output_bfd, vn,
6465 (Elf_External_Verneed *) p);
6466 p += sizeof (Elf_External_Verneed);
6468 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6470 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6471 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6472 a->vna_nodename, FALSE);
6473 if (indx == (size_t) -1)
6476 if (a->vna_nextptr == NULL)
6479 a->vna_next = sizeof (Elf_External_Vernaux);
6481 _bfd_elf_swap_vernaux_out (output_bfd, a,
6482 (Elf_External_Vernaux *) p);
6483 p += sizeof (Elf_External_Vernaux);
6487 elf_tdata (output_bfd)->cverrefs = crefs;
6491 /* Any syms created from now on start with -1 in
6492 got.refcount/offset and plt.refcount/offset. */
6493 elf_hash_table (info)->init_got_refcount
6494 = elf_hash_table (info)->init_got_offset;
6495 elf_hash_table (info)->init_plt_refcount
6496 = elf_hash_table (info)->init_plt_offset;
6498 if (bfd_link_relocatable (info)
6499 && !_bfd_elf_size_group_sections (info))
6502 /* The backend may have to create some sections regardless of whether
6503 we're dynamic or not. */
6504 if (bed->elf_backend_always_size_sections
6505 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6508 /* Determine any GNU_STACK segment requirements, after the backend
6509 has had a chance to set a default segment size. */
6510 if (info->execstack)
6511 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6512 else if (info->noexecstack)
6513 elf_stack_flags (output_bfd) = PF_R | PF_W;
6517 asection *notesec = NULL;
6520 for (inputobj = info->input_bfds;
6522 inputobj = inputobj->link.next)
6527 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6529 s = inputobj->sections;
6530 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6533 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6536 if (s->flags & SEC_CODE)
6540 else if (bed->default_execstack)
6543 if (notesec || info->stacksize > 0)
6544 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6545 if (notesec && exec && bfd_link_relocatable (info)
6546 && notesec->output_section != bfd_abs_section_ptr)
6547 notesec->output_section->flags |= SEC_CODE;
6550 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6552 struct elf_info_failed eif;
6553 struct elf_link_hash_entry *h;
6557 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6558 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6562 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6564 info->flags |= DF_SYMBOLIC;
6572 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6574 if (indx == (size_t) -1)
6577 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6578 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6582 if (filter_shlib != NULL)
6586 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6587 filter_shlib, TRUE);
6588 if (indx == (size_t) -1
6589 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6593 if (auxiliary_filters != NULL)
6595 const char * const *p;
6597 for (p = auxiliary_filters; *p != NULL; p++)
6601 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6603 if (indx == (size_t) -1
6604 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6613 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6615 if (indx == (size_t) -1
6616 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6620 if (depaudit != NULL)
6624 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6626 if (indx == (size_t) -1
6627 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6634 /* Find all symbols which were defined in a dynamic object and make
6635 the backend pick a reasonable value for them. */
6636 elf_link_hash_traverse (elf_hash_table (info),
6637 _bfd_elf_adjust_dynamic_symbol,
6642 /* Add some entries to the .dynamic section. We fill in some of the
6643 values later, in bfd_elf_final_link, but we must add the entries
6644 now so that we know the final size of the .dynamic section. */
6646 /* If there are initialization and/or finalization functions to
6647 call then add the corresponding DT_INIT/DT_FINI entries. */
6648 h = (info->init_function
6649 ? elf_link_hash_lookup (elf_hash_table (info),
6650 info->init_function, FALSE,
6657 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6660 h = (info->fini_function
6661 ? elf_link_hash_lookup (elf_hash_table (info),
6662 info->fini_function, FALSE,
6669 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6673 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6674 if (s != NULL && s->linker_has_input)
6676 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6677 if (! bfd_link_executable (info))
6682 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6683 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6684 && (o = sub->sections) != NULL
6685 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6686 for (o = sub->sections; o != NULL; o = o->next)
6687 if (elf_section_data (o)->this_hdr.sh_type
6688 == SHT_PREINIT_ARRAY)
6691 (_("%B: .preinit_array section is not allowed in DSO"),
6696 bfd_set_error (bfd_error_nonrepresentable_section);
6700 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6701 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6704 s = bfd_get_section_by_name (output_bfd, ".init_array");
6705 if (s != NULL && s->linker_has_input)
6707 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6708 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6711 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6712 if (s != NULL && s->linker_has_input)
6714 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6715 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6719 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6720 /* If .dynstr is excluded from the link, we don't want any of
6721 these tags. Strictly, we should be checking each section
6722 individually; This quick check covers for the case where
6723 someone does a /DISCARD/ : { *(*) }. */
6724 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6726 bfd_size_type strsize;
6728 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6729 if ((info->emit_hash
6730 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6731 || (info->emit_gnu_hash
6732 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6733 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6734 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6735 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6736 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6737 bed->s->sizeof_sym))
6742 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6745 /* The backend must work out the sizes of all the other dynamic
6748 && bed->elf_backend_size_dynamic_sections != NULL
6749 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6752 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6754 unsigned long section_sym_count;
6756 if (elf_tdata (output_bfd)->cverdefs)
6758 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6760 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6761 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6765 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6767 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6770 else if (info->flags & DF_BIND_NOW)
6772 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6778 if (bfd_link_executable (info))
6779 info->flags_1 &= ~ (DF_1_INITFIRST
6782 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6786 if (elf_tdata (output_bfd)->cverrefs)
6788 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6790 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6791 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6795 if ((elf_tdata (output_bfd)->cverrefs == 0
6796 && elf_tdata (output_bfd)->cverdefs == 0)
6797 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6798 §ion_sym_count) <= 1)
6802 s = bfd_get_linker_section (dynobj, ".gnu.version");
6803 s->flags |= SEC_EXCLUDE;
6809 /* Find the first non-excluded output section. We'll use its
6810 section symbol for some emitted relocs. */
6812 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6816 for (s = output_bfd->sections; s != NULL; s = s->next)
6817 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6818 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6820 elf_hash_table (info)->text_index_section = s;
6825 /* Find two non-excluded output sections, one for code, one for data.
6826 We'll use their section symbols for some emitted relocs. */
6828 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6832 /* Data first, since setting text_index_section changes
6833 _bfd_elf_link_omit_section_dynsym. */
6834 for (s = output_bfd->sections; s != NULL; s = s->next)
6835 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6836 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6838 elf_hash_table (info)->data_index_section = s;
6842 for (s = output_bfd->sections; s != NULL; s = s->next)
6843 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6844 == (SEC_ALLOC | SEC_READONLY))
6845 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6847 elf_hash_table (info)->text_index_section = s;
6851 if (elf_hash_table (info)->text_index_section == NULL)
6852 elf_hash_table (info)->text_index_section
6853 = elf_hash_table (info)->data_index_section;
6857 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6859 const struct elf_backend_data *bed;
6860 unsigned long section_sym_count;
6861 bfd_size_type dynsymcount = 0;
6863 if (!is_elf_hash_table (info->hash))
6866 bed = get_elf_backend_data (output_bfd);
6867 (*bed->elf_backend_init_index_section) (output_bfd, info);
6869 /* Assign dynsym indices. In a shared library we generate a section
6870 symbol for each output section, which come first. Next come all
6871 of the back-end allocated local dynamic syms, followed by the rest
6872 of the global symbols.
6874 This is usually not needed for static binaries, however backends
6875 can request to always do it, e.g. the MIPS backend uses dynamic
6876 symbol counts to lay out GOT, which will be produced in the
6877 presence of GOT relocations even in static binaries (holding fixed
6878 data in that case, to satisfy those relocations). */
6880 if (elf_hash_table (info)->dynamic_sections_created
6881 || bed->always_renumber_dynsyms)
6882 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6883 §ion_sym_count);
6885 if (elf_hash_table (info)->dynamic_sections_created)
6889 unsigned int dtagcount;
6891 dynobj = elf_hash_table (info)->dynobj;
6893 /* Work out the size of the symbol version section. */
6894 s = bfd_get_linker_section (dynobj, ".gnu.version");
6895 BFD_ASSERT (s != NULL);
6896 if ((s->flags & SEC_EXCLUDE) == 0)
6898 s->size = dynsymcount * sizeof (Elf_External_Versym);
6899 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6900 if (s->contents == NULL)
6903 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6907 /* Set the size of the .dynsym and .hash sections. We counted
6908 the number of dynamic symbols in elf_link_add_object_symbols.
6909 We will build the contents of .dynsym and .hash when we build
6910 the final symbol table, because until then we do not know the
6911 correct value to give the symbols. We built the .dynstr
6912 section as we went along in elf_link_add_object_symbols. */
6913 s = elf_hash_table (info)->dynsym;
6914 BFD_ASSERT (s != NULL);
6915 s->size = dynsymcount * bed->s->sizeof_sym;
6917 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6918 if (s->contents == NULL)
6921 /* The first entry in .dynsym is a dummy symbol. Clear all the
6922 section syms, in case we don't output them all. */
6923 ++section_sym_count;
6924 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6926 elf_hash_table (info)->bucketcount = 0;
6928 /* Compute the size of the hashing table. As a side effect this
6929 computes the hash values for all the names we export. */
6930 if (info->emit_hash)
6932 unsigned long int *hashcodes;
6933 struct hash_codes_info hashinf;
6935 unsigned long int nsyms;
6937 size_t hash_entry_size;
6939 /* Compute the hash values for all exported symbols. At the same
6940 time store the values in an array so that we could use them for
6942 amt = dynsymcount * sizeof (unsigned long int);
6943 hashcodes = (unsigned long int *) bfd_malloc (amt);
6944 if (hashcodes == NULL)
6946 hashinf.hashcodes = hashcodes;
6947 hashinf.error = FALSE;
6949 /* Put all hash values in HASHCODES. */
6950 elf_link_hash_traverse (elf_hash_table (info),
6951 elf_collect_hash_codes, &hashinf);
6958 nsyms = hashinf.hashcodes - hashcodes;
6960 = compute_bucket_count (info, hashcodes, nsyms, 0);
6963 if (bucketcount == 0 && nsyms > 0)
6966 elf_hash_table (info)->bucketcount = bucketcount;
6968 s = bfd_get_linker_section (dynobj, ".hash");
6969 BFD_ASSERT (s != NULL);
6970 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6971 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6972 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6973 if (s->contents == NULL)
6976 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6977 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6978 s->contents + hash_entry_size);
6981 if (info->emit_gnu_hash)
6984 unsigned char *contents;
6985 struct collect_gnu_hash_codes cinfo;
6989 memset (&cinfo, 0, sizeof (cinfo));
6991 /* Compute the hash values for all exported symbols. At the same
6992 time store the values in an array so that we could use them for
6994 amt = dynsymcount * 2 * sizeof (unsigned long int);
6995 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6996 if (cinfo.hashcodes == NULL)
6999 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7000 cinfo.min_dynindx = -1;
7001 cinfo.output_bfd = output_bfd;
7004 /* Put all hash values in HASHCODES. */
7005 elf_link_hash_traverse (elf_hash_table (info),
7006 elf_collect_gnu_hash_codes, &cinfo);
7009 free (cinfo.hashcodes);
7014 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7016 if (bucketcount == 0)
7018 free (cinfo.hashcodes);
7022 s = bfd_get_linker_section (dynobj, ".gnu.hash");
7023 BFD_ASSERT (s != NULL);
7025 if (cinfo.nsyms == 0)
7027 /* Empty .gnu.hash section is special. */
7028 BFD_ASSERT (cinfo.min_dynindx == -1);
7029 free (cinfo.hashcodes);
7030 s->size = 5 * 4 + bed->s->arch_size / 8;
7031 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7032 if (contents == NULL)
7034 s->contents = contents;
7035 /* 1 empty bucket. */
7036 bfd_put_32 (output_bfd, 1, contents);
7037 /* SYMIDX above the special symbol 0. */
7038 bfd_put_32 (output_bfd, 1, contents + 4);
7039 /* Just one word for bitmask. */
7040 bfd_put_32 (output_bfd, 1, contents + 8);
7041 /* Only hash fn bloom filter. */
7042 bfd_put_32 (output_bfd, 0, contents + 12);
7043 /* No hashes are valid - empty bitmask. */
7044 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7045 /* No hashes in the only bucket. */
7046 bfd_put_32 (output_bfd, 0,
7047 contents + 16 + bed->s->arch_size / 8);
7051 unsigned long int maskwords, maskbitslog2, x;
7052 BFD_ASSERT (cinfo.min_dynindx != -1);
7056 while ((x >>= 1) != 0)
7058 if (maskbitslog2 < 3)
7060 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7061 maskbitslog2 = maskbitslog2 + 3;
7063 maskbitslog2 = maskbitslog2 + 2;
7064 if (bed->s->arch_size == 64)
7066 if (maskbitslog2 == 5)
7072 cinfo.mask = (1 << cinfo.shift1) - 1;
7073 cinfo.shift2 = maskbitslog2;
7074 cinfo.maskbits = 1 << maskbitslog2;
7075 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7076 amt = bucketcount * sizeof (unsigned long int) * 2;
7077 amt += maskwords * sizeof (bfd_vma);
7078 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7079 if (cinfo.bitmask == NULL)
7081 free (cinfo.hashcodes);
7085 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7086 cinfo.indx = cinfo.counts + bucketcount;
7087 cinfo.symindx = dynsymcount - cinfo.nsyms;
7088 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7090 /* Determine how often each hash bucket is used. */
7091 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7092 for (i = 0; i < cinfo.nsyms; ++i)
7093 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7095 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7096 if (cinfo.counts[i] != 0)
7098 cinfo.indx[i] = cnt;
7099 cnt += cinfo.counts[i];
7101 BFD_ASSERT (cnt == dynsymcount);
7102 cinfo.bucketcount = bucketcount;
7103 cinfo.local_indx = cinfo.min_dynindx;
7105 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7106 s->size += cinfo.maskbits / 8;
7107 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7108 if (contents == NULL)
7110 free (cinfo.bitmask);
7111 free (cinfo.hashcodes);
7115 s->contents = contents;
7116 bfd_put_32 (output_bfd, bucketcount, contents);
7117 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7118 bfd_put_32 (output_bfd, maskwords, contents + 8);
7119 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7120 contents += 16 + cinfo.maskbits / 8;
7122 for (i = 0; i < bucketcount; ++i)
7124 if (cinfo.counts[i] == 0)
7125 bfd_put_32 (output_bfd, 0, contents);
7127 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7131 cinfo.contents = contents;
7133 /* Renumber dynamic symbols, populate .gnu.hash section. */
7134 elf_link_hash_traverse (elf_hash_table (info),
7135 elf_renumber_gnu_hash_syms, &cinfo);
7137 contents = s->contents + 16;
7138 for (i = 0; i < maskwords; ++i)
7140 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7142 contents += bed->s->arch_size / 8;
7145 free (cinfo.bitmask);
7146 free (cinfo.hashcodes);
7150 s = bfd_get_linker_section (dynobj, ".dynstr");
7151 BFD_ASSERT (s != NULL);
7153 elf_finalize_dynstr (output_bfd, info);
7155 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7157 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7158 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7165 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7168 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7171 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7172 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7175 /* Finish SHF_MERGE section merging. */
7178 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7183 if (!is_elf_hash_table (info->hash))
7186 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7187 if ((ibfd->flags & DYNAMIC) == 0
7188 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7189 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7190 == get_elf_backend_data (obfd)->s->elfclass))
7191 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7192 if ((sec->flags & SEC_MERGE) != 0
7193 && !bfd_is_abs_section (sec->output_section))
7195 struct bfd_elf_section_data *secdata;
7197 secdata = elf_section_data (sec);
7198 if (! _bfd_add_merge_section (obfd,
7199 &elf_hash_table (info)->merge_info,
7200 sec, &secdata->sec_info))
7202 else if (secdata->sec_info)
7203 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7206 if (elf_hash_table (info)->merge_info != NULL)
7207 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7208 merge_sections_remove_hook);
7212 /* Create an entry in an ELF linker hash table. */
7214 struct bfd_hash_entry *
7215 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7216 struct bfd_hash_table *table,
7219 /* Allocate the structure if it has not already been allocated by a
7223 entry = (struct bfd_hash_entry *)
7224 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7229 /* Call the allocation method of the superclass. */
7230 entry = _bfd_link_hash_newfunc (entry, table, string);
7233 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7234 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7236 /* Set local fields. */
7239 ret->got = htab->init_got_refcount;
7240 ret->plt = htab->init_plt_refcount;
7241 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7242 - offsetof (struct elf_link_hash_entry, size)));
7243 /* Assume that we have been called by a non-ELF symbol reader.
7244 This flag is then reset by the code which reads an ELF input
7245 file. This ensures that a symbol created by a non-ELF symbol
7246 reader will have the flag set correctly. */
7253 /* Copy data from an indirect symbol to its direct symbol, hiding the
7254 old indirect symbol. Also used for copying flags to a weakdef. */
7257 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7258 struct elf_link_hash_entry *dir,
7259 struct elf_link_hash_entry *ind)
7261 struct elf_link_hash_table *htab;
7263 /* Copy down any references that we may have already seen to the
7264 symbol which just became indirect. */
7266 if (dir->versioned != versioned_hidden)
7267 dir->ref_dynamic |= ind->ref_dynamic;
7268 dir->ref_regular |= ind->ref_regular;
7269 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7270 dir->non_got_ref |= ind->non_got_ref;
7271 dir->needs_plt |= ind->needs_plt;
7272 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7274 if (ind->root.type != bfd_link_hash_indirect)
7277 /* Copy over the global and procedure linkage table refcount entries.
7278 These may have been already set up by a check_relocs routine. */
7279 htab = elf_hash_table (info);
7280 if (ind->got.refcount > htab->init_got_refcount.refcount)
7282 if (dir->got.refcount < 0)
7283 dir->got.refcount = 0;
7284 dir->got.refcount += ind->got.refcount;
7285 ind->got.refcount = htab->init_got_refcount.refcount;
7288 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7290 if (dir->plt.refcount < 0)
7291 dir->plt.refcount = 0;
7292 dir->plt.refcount += ind->plt.refcount;
7293 ind->plt.refcount = htab->init_plt_refcount.refcount;
7296 if (ind->dynindx != -1)
7298 if (dir->dynindx != -1)
7299 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7300 dir->dynindx = ind->dynindx;
7301 dir->dynstr_index = ind->dynstr_index;
7303 ind->dynstr_index = 0;
7308 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7309 struct elf_link_hash_entry *h,
7310 bfd_boolean force_local)
7312 /* STT_GNU_IFUNC symbol must go through PLT. */
7313 if (h->type != STT_GNU_IFUNC)
7315 h->plt = elf_hash_table (info)->init_plt_offset;
7320 h->forced_local = 1;
7321 if (h->dynindx != -1)
7323 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7326 h->dynstr_index = 0;
7331 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7335 _bfd_elf_link_hash_table_init
7336 (struct elf_link_hash_table *table,
7338 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7339 struct bfd_hash_table *,
7341 unsigned int entsize,
7342 enum elf_target_id target_id)
7345 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7347 table->init_got_refcount.refcount = can_refcount - 1;
7348 table->init_plt_refcount.refcount = can_refcount - 1;
7349 table->init_got_offset.offset = -(bfd_vma) 1;
7350 table->init_plt_offset.offset = -(bfd_vma) 1;
7351 /* The first dynamic symbol is a dummy. */
7352 table->dynsymcount = 1;
7354 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7356 table->root.type = bfd_link_elf_hash_table;
7357 table->hash_table_id = target_id;
7362 /* Create an ELF linker hash table. */
7364 struct bfd_link_hash_table *
7365 _bfd_elf_link_hash_table_create (bfd *abfd)
7367 struct elf_link_hash_table *ret;
7368 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7370 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7374 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7375 sizeof (struct elf_link_hash_entry),
7381 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7386 /* Destroy an ELF linker hash table. */
7389 _bfd_elf_link_hash_table_free (bfd *obfd)
7391 struct elf_link_hash_table *htab;
7393 htab = (struct elf_link_hash_table *) obfd->link.hash;
7394 if (htab->dynstr != NULL)
7395 _bfd_elf_strtab_free (htab->dynstr);
7396 _bfd_merge_sections_free (htab->merge_info);
7397 _bfd_generic_link_hash_table_free (obfd);
7400 /* This is a hook for the ELF emulation code in the generic linker to
7401 tell the backend linker what file name to use for the DT_NEEDED
7402 entry for a dynamic object. */
7405 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7407 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7408 && bfd_get_format (abfd) == bfd_object)
7409 elf_dt_name (abfd) = name;
7413 bfd_elf_get_dyn_lib_class (bfd *abfd)
7416 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7417 && bfd_get_format (abfd) == bfd_object)
7418 lib_class = elf_dyn_lib_class (abfd);
7425 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7427 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7428 && bfd_get_format (abfd) == bfd_object)
7429 elf_dyn_lib_class (abfd) = lib_class;
7432 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7433 the linker ELF emulation code. */
7435 struct bfd_link_needed_list *
7436 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7437 struct bfd_link_info *info)
7439 if (! is_elf_hash_table (info->hash))
7441 return elf_hash_table (info)->needed;
7444 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7445 hook for the linker ELF emulation code. */
7447 struct bfd_link_needed_list *
7448 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7449 struct bfd_link_info *info)
7451 if (! is_elf_hash_table (info->hash))
7453 return elf_hash_table (info)->runpath;
7456 /* Get the name actually used for a dynamic object for a link. This
7457 is the SONAME entry if there is one. Otherwise, it is the string
7458 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7461 bfd_elf_get_dt_soname (bfd *abfd)
7463 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7464 && bfd_get_format (abfd) == bfd_object)
7465 return elf_dt_name (abfd);
7469 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7470 the ELF linker emulation code. */
7473 bfd_elf_get_bfd_needed_list (bfd *abfd,
7474 struct bfd_link_needed_list **pneeded)
7477 bfd_byte *dynbuf = NULL;
7478 unsigned int elfsec;
7479 unsigned long shlink;
7480 bfd_byte *extdyn, *extdynend;
7482 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7486 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7487 || bfd_get_format (abfd) != bfd_object)
7490 s = bfd_get_section_by_name (abfd, ".dynamic");
7491 if (s == NULL || s->size == 0)
7494 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7497 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7498 if (elfsec == SHN_BAD)
7501 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7503 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7504 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7507 extdynend = extdyn + s->size;
7508 for (; extdyn < extdynend; extdyn += extdynsize)
7510 Elf_Internal_Dyn dyn;
7512 (*swap_dyn_in) (abfd, extdyn, &dyn);
7514 if (dyn.d_tag == DT_NULL)
7517 if (dyn.d_tag == DT_NEEDED)
7520 struct bfd_link_needed_list *l;
7521 unsigned int tagv = dyn.d_un.d_val;
7524 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7529 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7550 struct elf_symbuf_symbol
7552 unsigned long st_name; /* Symbol name, index in string tbl */
7553 unsigned char st_info; /* Type and binding attributes */
7554 unsigned char st_other; /* Visibilty, and target specific */
7557 struct elf_symbuf_head
7559 struct elf_symbuf_symbol *ssym;
7561 unsigned int st_shndx;
7568 Elf_Internal_Sym *isym;
7569 struct elf_symbuf_symbol *ssym;
7574 /* Sort references to symbols by ascending section number. */
7577 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7579 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7580 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7582 return s1->st_shndx - s2->st_shndx;
7586 elf_sym_name_compare (const void *arg1, const void *arg2)
7588 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7589 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7590 return strcmp (s1->name, s2->name);
7593 static struct elf_symbuf_head *
7594 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7596 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7597 struct elf_symbuf_symbol *ssym;
7598 struct elf_symbuf_head *ssymbuf, *ssymhead;
7599 size_t i, shndx_count, total_size;
7601 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7605 for (ind = indbuf, i = 0; i < symcount; i++)
7606 if (isymbuf[i].st_shndx != SHN_UNDEF)
7607 *ind++ = &isymbuf[i];
7610 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7611 elf_sort_elf_symbol);
7614 if (indbufend > indbuf)
7615 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7616 if (ind[0]->st_shndx != ind[1]->st_shndx)
7619 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7620 + (indbufend - indbuf) * sizeof (*ssym));
7621 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7622 if (ssymbuf == NULL)
7628 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7629 ssymbuf->ssym = NULL;
7630 ssymbuf->count = shndx_count;
7631 ssymbuf->st_shndx = 0;
7632 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7634 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7637 ssymhead->ssym = ssym;
7638 ssymhead->count = 0;
7639 ssymhead->st_shndx = (*ind)->st_shndx;
7641 ssym->st_name = (*ind)->st_name;
7642 ssym->st_info = (*ind)->st_info;
7643 ssym->st_other = (*ind)->st_other;
7646 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7647 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7654 /* Check if 2 sections define the same set of local and global
7658 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7659 struct bfd_link_info *info)
7662 const struct elf_backend_data *bed1, *bed2;
7663 Elf_Internal_Shdr *hdr1, *hdr2;
7664 size_t symcount1, symcount2;
7665 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7666 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7667 Elf_Internal_Sym *isym, *isymend;
7668 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7669 size_t count1, count2, i;
7670 unsigned int shndx1, shndx2;
7676 /* Both sections have to be in ELF. */
7677 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7678 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7681 if (elf_section_type (sec1) != elf_section_type (sec2))
7684 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7685 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7686 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7689 bed1 = get_elf_backend_data (bfd1);
7690 bed2 = get_elf_backend_data (bfd2);
7691 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7692 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7693 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7694 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7696 if (symcount1 == 0 || symcount2 == 0)
7702 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7703 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7705 if (ssymbuf1 == NULL)
7707 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7709 if (isymbuf1 == NULL)
7712 if (!info->reduce_memory_overheads)
7713 elf_tdata (bfd1)->symbuf = ssymbuf1
7714 = elf_create_symbuf (symcount1, isymbuf1);
7717 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7719 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7721 if (isymbuf2 == NULL)
7724 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7725 elf_tdata (bfd2)->symbuf = ssymbuf2
7726 = elf_create_symbuf (symcount2, isymbuf2);
7729 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7731 /* Optimized faster version. */
7733 struct elf_symbol *symp;
7734 struct elf_symbuf_symbol *ssym, *ssymend;
7737 hi = ssymbuf1->count;
7742 mid = (lo + hi) / 2;
7743 if (shndx1 < ssymbuf1[mid].st_shndx)
7745 else if (shndx1 > ssymbuf1[mid].st_shndx)
7749 count1 = ssymbuf1[mid].count;
7756 hi = ssymbuf2->count;
7761 mid = (lo + hi) / 2;
7762 if (shndx2 < ssymbuf2[mid].st_shndx)
7764 else if (shndx2 > ssymbuf2[mid].st_shndx)
7768 count2 = ssymbuf2[mid].count;
7774 if (count1 == 0 || count2 == 0 || count1 != count2)
7778 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7780 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7781 if (symtable1 == NULL || symtable2 == NULL)
7785 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7786 ssym < ssymend; ssym++, symp++)
7788 symp->u.ssym = ssym;
7789 symp->name = bfd_elf_string_from_elf_section (bfd1,
7795 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7796 ssym < ssymend; ssym++, symp++)
7798 symp->u.ssym = ssym;
7799 symp->name = bfd_elf_string_from_elf_section (bfd2,
7804 /* Sort symbol by name. */
7805 qsort (symtable1, count1, sizeof (struct elf_symbol),
7806 elf_sym_name_compare);
7807 qsort (symtable2, count1, sizeof (struct elf_symbol),
7808 elf_sym_name_compare);
7810 for (i = 0; i < count1; i++)
7811 /* Two symbols must have the same binding, type and name. */
7812 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7813 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7814 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7821 symtable1 = (struct elf_symbol *)
7822 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7823 symtable2 = (struct elf_symbol *)
7824 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7825 if (symtable1 == NULL || symtable2 == NULL)
7828 /* Count definitions in the section. */
7830 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7831 if (isym->st_shndx == shndx1)
7832 symtable1[count1++].u.isym = isym;
7835 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7836 if (isym->st_shndx == shndx2)
7837 symtable2[count2++].u.isym = isym;
7839 if (count1 == 0 || count2 == 0 || count1 != count2)
7842 for (i = 0; i < count1; i++)
7844 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7845 symtable1[i].u.isym->st_name);
7847 for (i = 0; i < count2; i++)
7849 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7850 symtable2[i].u.isym->st_name);
7852 /* Sort symbol by name. */
7853 qsort (symtable1, count1, sizeof (struct elf_symbol),
7854 elf_sym_name_compare);
7855 qsort (symtable2, count1, sizeof (struct elf_symbol),
7856 elf_sym_name_compare);
7858 for (i = 0; i < count1; i++)
7859 /* Two symbols must have the same binding, type and name. */
7860 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7861 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7862 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7880 /* Return TRUE if 2 section types are compatible. */
7883 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7884 bfd *bbfd, const asection *bsec)
7888 || abfd->xvec->flavour != bfd_target_elf_flavour
7889 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7892 return elf_section_type (asec) == elf_section_type (bsec);
7895 /* Final phase of ELF linker. */
7897 /* A structure we use to avoid passing large numbers of arguments. */
7899 struct elf_final_link_info
7901 /* General link information. */
7902 struct bfd_link_info *info;
7905 /* Symbol string table. */
7906 struct elf_strtab_hash *symstrtab;
7907 /* .hash section. */
7909 /* symbol version section (.gnu.version). */
7910 asection *symver_sec;
7911 /* Buffer large enough to hold contents of any section. */
7913 /* Buffer large enough to hold external relocs of any section. */
7914 void *external_relocs;
7915 /* Buffer large enough to hold internal relocs of any section. */
7916 Elf_Internal_Rela *internal_relocs;
7917 /* Buffer large enough to hold external local symbols of any input
7919 bfd_byte *external_syms;
7920 /* And a buffer for symbol section indices. */
7921 Elf_External_Sym_Shndx *locsym_shndx;
7922 /* Buffer large enough to hold internal local symbols of any input
7924 Elf_Internal_Sym *internal_syms;
7925 /* Array large enough to hold a symbol index for each local symbol
7926 of any input BFD. */
7928 /* Array large enough to hold a section pointer for each local
7929 symbol of any input BFD. */
7930 asection **sections;
7931 /* Buffer for SHT_SYMTAB_SHNDX section. */
7932 Elf_External_Sym_Shndx *symshndxbuf;
7933 /* Number of STT_FILE syms seen. */
7934 size_t filesym_count;
7937 /* This struct is used to pass information to elf_link_output_extsym. */
7939 struct elf_outext_info
7942 bfd_boolean localsyms;
7943 bfd_boolean file_sym_done;
7944 struct elf_final_link_info *flinfo;
7948 /* Support for evaluating a complex relocation.
7950 Complex relocations are generalized, self-describing relocations. The
7951 implementation of them consists of two parts: complex symbols, and the
7952 relocations themselves.
7954 The relocations are use a reserved elf-wide relocation type code (R_RELC
7955 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7956 information (start bit, end bit, word width, etc) into the addend. This
7957 information is extracted from CGEN-generated operand tables within gas.
7959 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7960 internal) representing prefix-notation expressions, including but not
7961 limited to those sorts of expressions normally encoded as addends in the
7962 addend field. The symbol mangling format is:
7965 | <unary-operator> ':' <node>
7966 | <binary-operator> ':' <node> ':' <node>
7969 <literal> := 's' <digits=N> ':' <N character symbol name>
7970 | 'S' <digits=N> ':' <N character section name>
7974 <binary-operator> := as in C
7975 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7978 set_symbol_value (bfd *bfd_with_globals,
7979 Elf_Internal_Sym *isymbuf,
7984 struct elf_link_hash_entry **sym_hashes;
7985 struct elf_link_hash_entry *h;
7986 size_t extsymoff = locsymcount;
7988 if (symidx < locsymcount)
7990 Elf_Internal_Sym *sym;
7992 sym = isymbuf + symidx;
7993 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7995 /* It is a local symbol: move it to the
7996 "absolute" section and give it a value. */
7997 sym->st_shndx = SHN_ABS;
7998 sym->st_value = val;
8001 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8005 /* It is a global symbol: set its link type
8006 to "defined" and give it a value. */
8008 sym_hashes = elf_sym_hashes (bfd_with_globals);
8009 h = sym_hashes [symidx - extsymoff];
8010 while (h->root.type == bfd_link_hash_indirect
8011 || h->root.type == bfd_link_hash_warning)
8012 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8013 h->root.type = bfd_link_hash_defined;
8014 h->root.u.def.value = val;
8015 h->root.u.def.section = bfd_abs_section_ptr;
8019 resolve_symbol (const char *name,
8021 struct elf_final_link_info *flinfo,
8023 Elf_Internal_Sym *isymbuf,
8026 Elf_Internal_Sym *sym;
8027 struct bfd_link_hash_entry *global_entry;
8028 const char *candidate = NULL;
8029 Elf_Internal_Shdr *symtab_hdr;
8032 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8034 for (i = 0; i < locsymcount; ++ i)
8038 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8041 candidate = bfd_elf_string_from_elf_section (input_bfd,
8042 symtab_hdr->sh_link,
8045 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8046 name, candidate, (unsigned long) sym->st_value);
8048 if (candidate && strcmp (candidate, name) == 0)
8050 asection *sec = flinfo->sections [i];
8052 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8053 *result += sec->output_offset + sec->output_section->vma;
8055 printf ("Found symbol with value %8.8lx\n",
8056 (unsigned long) *result);
8062 /* Hmm, haven't found it yet. perhaps it is a global. */
8063 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8064 FALSE, FALSE, TRUE);
8068 if (global_entry->type == bfd_link_hash_defined
8069 || global_entry->type == bfd_link_hash_defweak)
8071 *result = (global_entry->u.def.value
8072 + global_entry->u.def.section->output_section->vma
8073 + global_entry->u.def.section->output_offset);
8075 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8076 global_entry->root.string, (unsigned long) *result);
8084 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8085 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8086 names like "foo.end" which is the end address of section "foo". */
8089 resolve_section (const char *name,
8097 for (curr = sections; curr; curr = curr->next)
8098 if (strcmp (curr->name, name) == 0)
8100 *result = curr->vma;
8104 /* Hmm. still haven't found it. try pseudo-section names. */
8105 /* FIXME: This could be coded more efficiently... */
8106 for (curr = sections; curr; curr = curr->next)
8108 len = strlen (curr->name);
8109 if (len > strlen (name))
8112 if (strncmp (curr->name, name, len) == 0)
8114 if (strncmp (".end", name + len, 4) == 0)
8116 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8120 /* Insert more pseudo-section names here, if you like. */
8128 undefined_reference (const char *reftype, const char *name)
8130 /* xgettext:c-format */
8131 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8136 eval_symbol (bfd_vma *result,
8139 struct elf_final_link_info *flinfo,
8141 Elf_Internal_Sym *isymbuf,
8150 const char *sym = *symp;
8152 bfd_boolean symbol_is_section = FALSE;
8157 if (len < 1 || len > sizeof (symbuf))
8159 bfd_set_error (bfd_error_invalid_operation);
8172 *result = strtoul (sym, (char **) symp, 16);
8176 symbol_is_section = TRUE;
8180 symlen = strtol (sym, (char **) symp, 10);
8181 sym = *symp + 1; /* Skip the trailing ':'. */
8183 if (symend < sym || symlen + 1 > sizeof (symbuf))
8185 bfd_set_error (bfd_error_invalid_operation);
8189 memcpy (symbuf, sym, symlen);
8190 symbuf[symlen] = '\0';
8191 *symp = sym + symlen;
8193 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8194 the symbol as a section, or vice-versa. so we're pretty liberal in our
8195 interpretation here; section means "try section first", not "must be a
8196 section", and likewise with symbol. */
8198 if (symbol_is_section)
8200 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8201 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8202 isymbuf, locsymcount))
8204 undefined_reference ("section", symbuf);
8210 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8211 isymbuf, locsymcount)
8212 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8215 undefined_reference ("symbol", symbuf);
8222 /* All that remains are operators. */
8224 #define UNARY_OP(op) \
8225 if (strncmp (sym, #op, strlen (#op)) == 0) \
8227 sym += strlen (#op); \
8231 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8232 isymbuf, locsymcount, signed_p)) \
8235 *result = op ((bfd_signed_vma) a); \
8241 #define BINARY_OP(op) \
8242 if (strncmp (sym, #op, strlen (#op)) == 0) \
8244 sym += strlen (#op); \
8248 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8249 isymbuf, locsymcount, signed_p)) \
8252 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8253 isymbuf, locsymcount, signed_p)) \
8256 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8286 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8287 bfd_set_error (bfd_error_invalid_operation);
8293 put_value (bfd_vma size,
8294 unsigned long chunksz,
8299 location += (size - chunksz);
8301 for (; size; size -= chunksz, location -= chunksz)
8306 bfd_put_8 (input_bfd, x, location);
8310 bfd_put_16 (input_bfd, x, location);
8314 bfd_put_32 (input_bfd, x, location);
8315 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8321 bfd_put_64 (input_bfd, x, location);
8322 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8335 get_value (bfd_vma size,
8336 unsigned long chunksz,
8343 /* Sanity checks. */
8344 BFD_ASSERT (chunksz <= sizeof (x)
8347 && (size % chunksz) == 0
8348 && input_bfd != NULL
8349 && location != NULL);
8351 if (chunksz == sizeof (x))
8353 BFD_ASSERT (size == chunksz);
8355 /* Make sure that we do not perform an undefined shift operation.
8356 We know that size == chunksz so there will only be one iteration
8357 of the loop below. */
8361 shift = 8 * chunksz;
8363 for (; size; size -= chunksz, location += chunksz)
8368 x = (x << shift) | bfd_get_8 (input_bfd, location);
8371 x = (x << shift) | bfd_get_16 (input_bfd, location);
8374 x = (x << shift) | bfd_get_32 (input_bfd, location);
8378 x = (x << shift) | bfd_get_64 (input_bfd, location);
8389 decode_complex_addend (unsigned long *start, /* in bits */
8390 unsigned long *oplen, /* in bits */
8391 unsigned long *len, /* in bits */
8392 unsigned long *wordsz, /* in bytes */
8393 unsigned long *chunksz, /* in bytes */
8394 unsigned long *lsb0_p,
8395 unsigned long *signed_p,
8396 unsigned long *trunc_p,
8397 unsigned long encoded)
8399 * start = encoded & 0x3F;
8400 * len = (encoded >> 6) & 0x3F;
8401 * oplen = (encoded >> 12) & 0x3F;
8402 * wordsz = (encoded >> 18) & 0xF;
8403 * chunksz = (encoded >> 22) & 0xF;
8404 * lsb0_p = (encoded >> 27) & 1;
8405 * signed_p = (encoded >> 28) & 1;
8406 * trunc_p = (encoded >> 29) & 1;
8409 bfd_reloc_status_type
8410 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8411 asection *input_section ATTRIBUTE_UNUSED,
8413 Elf_Internal_Rela *rel,
8416 bfd_vma shift, x, mask;
8417 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8418 bfd_reloc_status_type r;
8420 /* Perform this reloc, since it is complex.
8421 (this is not to say that it necessarily refers to a complex
8422 symbol; merely that it is a self-describing CGEN based reloc.
8423 i.e. the addend has the complete reloc information (bit start, end,
8424 word size, etc) encoded within it.). */
8426 decode_complex_addend (&start, &oplen, &len, &wordsz,
8427 &chunksz, &lsb0_p, &signed_p,
8428 &trunc_p, rel->r_addend);
8430 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8433 shift = (start + 1) - len;
8435 shift = (8 * wordsz) - (start + len);
8437 x = get_value (wordsz, chunksz, input_bfd,
8438 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8441 printf ("Doing complex reloc: "
8442 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8443 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8444 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8445 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8446 oplen, (unsigned long) x, (unsigned long) mask,
8447 (unsigned long) relocation);
8452 /* Now do an overflow check. */
8453 r = bfd_check_overflow ((signed_p
8454 ? complain_overflow_signed
8455 : complain_overflow_unsigned),
8456 len, 0, (8 * wordsz),
8460 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8463 printf (" relocation: %8.8lx\n"
8464 " shifted mask: %8.8lx\n"
8465 " shifted/masked reloc: %8.8lx\n"
8466 " result: %8.8lx\n",
8467 (unsigned long) relocation, (unsigned long) (mask << shift),
8468 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8470 put_value (wordsz, chunksz, input_bfd, x,
8471 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8475 /* Functions to read r_offset from external (target order) reloc
8476 entry. Faster than bfd_getl32 et al, because we let the compiler
8477 know the value is aligned. */
8480 ext32l_r_offset (const void *p)
8487 const union aligned32 *a
8488 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8490 uint32_t aval = ( (uint32_t) a->c[0]
8491 | (uint32_t) a->c[1] << 8
8492 | (uint32_t) a->c[2] << 16
8493 | (uint32_t) a->c[3] << 24);
8498 ext32b_r_offset (const void *p)
8505 const union aligned32 *a
8506 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8508 uint32_t aval = ( (uint32_t) a->c[0] << 24
8509 | (uint32_t) a->c[1] << 16
8510 | (uint32_t) a->c[2] << 8
8511 | (uint32_t) a->c[3]);
8515 #ifdef BFD_HOST_64_BIT
8517 ext64l_r_offset (const void *p)
8524 const union aligned64 *a
8525 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8527 uint64_t aval = ( (uint64_t) a->c[0]
8528 | (uint64_t) a->c[1] << 8
8529 | (uint64_t) a->c[2] << 16
8530 | (uint64_t) a->c[3] << 24
8531 | (uint64_t) a->c[4] << 32
8532 | (uint64_t) a->c[5] << 40
8533 | (uint64_t) a->c[6] << 48
8534 | (uint64_t) a->c[7] << 56);
8539 ext64b_r_offset (const void *p)
8546 const union aligned64 *a
8547 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8549 uint64_t aval = ( (uint64_t) a->c[0] << 56
8550 | (uint64_t) a->c[1] << 48
8551 | (uint64_t) a->c[2] << 40
8552 | (uint64_t) a->c[3] << 32
8553 | (uint64_t) a->c[4] << 24
8554 | (uint64_t) a->c[5] << 16
8555 | (uint64_t) a->c[6] << 8
8556 | (uint64_t) a->c[7]);
8561 /* When performing a relocatable link, the input relocations are
8562 preserved. But, if they reference global symbols, the indices
8563 referenced must be updated. Update all the relocations found in
8567 elf_link_adjust_relocs (bfd *abfd,
8569 struct bfd_elf_section_reloc_data *reldata,
8571 struct bfd_link_info *info)
8574 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8576 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8577 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8578 bfd_vma r_type_mask;
8580 unsigned int count = reldata->count;
8581 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8583 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8585 swap_in = bed->s->swap_reloc_in;
8586 swap_out = bed->s->swap_reloc_out;
8588 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8590 swap_in = bed->s->swap_reloca_in;
8591 swap_out = bed->s->swap_reloca_out;
8596 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8599 if (bed->s->arch_size == 32)
8606 r_type_mask = 0xffffffff;
8610 erela = reldata->hdr->contents;
8611 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8613 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8616 if (*rel_hash == NULL)
8619 if ((*rel_hash)->indx == -2
8620 && info->gc_sections
8621 && ! info->gc_keep_exported)
8623 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8624 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."),
8626 (*rel_hash)->root.root.string);
8627 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."),
8629 bfd_set_error (bfd_error_invalid_operation);
8632 BFD_ASSERT ((*rel_hash)->indx >= 0);
8634 (*swap_in) (abfd, erela, irela);
8635 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8636 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8637 | (irela[j].r_info & r_type_mask));
8638 (*swap_out) (abfd, irela, erela);
8641 if (bed->elf_backend_update_relocs)
8642 (*bed->elf_backend_update_relocs) (sec, reldata);
8644 if (sort && count != 0)
8646 bfd_vma (*ext_r_off) (const void *);
8649 bfd_byte *base, *end, *p, *loc;
8650 bfd_byte *buf = NULL;
8652 if (bed->s->arch_size == 32)
8654 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8655 ext_r_off = ext32l_r_offset;
8656 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8657 ext_r_off = ext32b_r_offset;
8663 #ifdef BFD_HOST_64_BIT
8664 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8665 ext_r_off = ext64l_r_offset;
8666 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8667 ext_r_off = ext64b_r_offset;
8673 /* Must use a stable sort here. A modified insertion sort,
8674 since the relocs are mostly sorted already. */
8675 elt_size = reldata->hdr->sh_entsize;
8676 base = reldata->hdr->contents;
8677 end = base + count * elt_size;
8678 if (elt_size > sizeof (Elf64_External_Rela))
8681 /* Ensure the first element is lowest. This acts as a sentinel,
8682 speeding the main loop below. */
8683 r_off = (*ext_r_off) (base);
8684 for (p = loc = base; (p += elt_size) < end; )
8686 bfd_vma r_off2 = (*ext_r_off) (p);
8695 /* Don't just swap *base and *loc as that changes the order
8696 of the original base[0] and base[1] if they happen to
8697 have the same r_offset. */
8698 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8699 memcpy (onebuf, loc, elt_size);
8700 memmove (base + elt_size, base, loc - base);
8701 memcpy (base, onebuf, elt_size);
8704 for (p = base + elt_size; (p += elt_size) < end; )
8706 /* base to p is sorted, *p is next to insert. */
8707 r_off = (*ext_r_off) (p);
8708 /* Search the sorted region for location to insert. */
8710 while (r_off < (*ext_r_off) (loc))
8715 /* Chances are there is a run of relocs to insert here,
8716 from one of more input files. Files are not always
8717 linked in order due to the way elf_link_input_bfd is
8718 called. See pr17666. */
8719 size_t sortlen = p - loc;
8720 bfd_vma r_off2 = (*ext_r_off) (loc);
8721 size_t runlen = elt_size;
8722 size_t buf_size = 96 * 1024;
8723 while (p + runlen < end
8724 && (sortlen <= buf_size
8725 || runlen + elt_size <= buf_size)
8726 && r_off2 > (*ext_r_off) (p + runlen))
8730 buf = bfd_malloc (buf_size);
8734 if (runlen < sortlen)
8736 memcpy (buf, p, runlen);
8737 memmove (loc + runlen, loc, sortlen);
8738 memcpy (loc, buf, runlen);
8742 memcpy (buf, loc, sortlen);
8743 memmove (loc, p, runlen);
8744 memcpy (loc + runlen, buf, sortlen);
8746 p += runlen - elt_size;
8749 /* Hashes are no longer valid. */
8750 free (reldata->hashes);
8751 reldata->hashes = NULL;
8757 struct elf_link_sort_rela
8763 enum elf_reloc_type_class type;
8764 /* We use this as an array of size int_rels_per_ext_rel. */
8765 Elf_Internal_Rela rela[1];
8769 elf_link_sort_cmp1 (const void *A, const void *B)
8771 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8772 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8773 int relativea, relativeb;
8775 relativea = a->type == reloc_class_relative;
8776 relativeb = b->type == reloc_class_relative;
8778 if (relativea < relativeb)
8780 if (relativea > relativeb)
8782 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8784 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8786 if (a->rela->r_offset < b->rela->r_offset)
8788 if (a->rela->r_offset > b->rela->r_offset)
8794 elf_link_sort_cmp2 (const void *A, const void *B)
8796 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8797 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8799 if (a->type < b->type)
8801 if (a->type > b->type)
8803 if (a->u.offset < b->u.offset)
8805 if (a->u.offset > b->u.offset)
8807 if (a->rela->r_offset < b->rela->r_offset)
8809 if (a->rela->r_offset > b->rela->r_offset)
8815 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8817 asection *dynamic_relocs;
8820 bfd_size_type count, size;
8821 size_t i, ret, sort_elt, ext_size;
8822 bfd_byte *sort, *s_non_relative, *p;
8823 struct elf_link_sort_rela *sq;
8824 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8825 int i2e = bed->s->int_rels_per_ext_rel;
8826 unsigned int opb = bfd_octets_per_byte (abfd);
8827 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8828 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8829 struct bfd_link_order *lo;
8831 bfd_boolean use_rela;
8833 /* Find a dynamic reloc section. */
8834 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8835 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8836 if (rela_dyn != NULL && rela_dyn->size > 0
8837 && rel_dyn != NULL && rel_dyn->size > 0)
8839 bfd_boolean use_rela_initialised = FALSE;
8841 /* This is just here to stop gcc from complaining.
8842 Its initialization checking code is not perfect. */
8845 /* Both sections are present. Examine the sizes
8846 of the indirect sections to help us choose. */
8847 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8848 if (lo->type == bfd_indirect_link_order)
8850 asection *o = lo->u.indirect.section;
8852 if ((o->size % bed->s->sizeof_rela) == 0)
8854 if ((o->size % bed->s->sizeof_rel) == 0)
8855 /* Section size is divisible by both rel and rela sizes.
8856 It is of no help to us. */
8860 /* Section size is only divisible by rela. */
8861 if (use_rela_initialised && !use_rela)
8863 _bfd_error_handler (_("%B: Unable to sort relocs - "
8864 "they are in more than one size"),
8866 bfd_set_error (bfd_error_invalid_operation);
8872 use_rela_initialised = TRUE;
8876 else if ((o->size % bed->s->sizeof_rel) == 0)
8878 /* Section size is only divisible by rel. */
8879 if (use_rela_initialised && use_rela)
8881 _bfd_error_handler (_("%B: Unable to sort relocs - "
8882 "they are in more than one size"),
8884 bfd_set_error (bfd_error_invalid_operation);
8890 use_rela_initialised = TRUE;
8895 /* The section size is not divisible by either -
8896 something is wrong. */
8897 _bfd_error_handler (_("%B: Unable to sort relocs - "
8898 "they are of an unknown size"), abfd);
8899 bfd_set_error (bfd_error_invalid_operation);
8904 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8905 if (lo->type == bfd_indirect_link_order)
8907 asection *o = lo->u.indirect.section;
8909 if ((o->size % bed->s->sizeof_rela) == 0)
8911 if ((o->size % bed->s->sizeof_rel) == 0)
8912 /* Section size is divisible by both rel and rela sizes.
8913 It is of no help to us. */
8917 /* Section size is only divisible by rela. */
8918 if (use_rela_initialised && !use_rela)
8920 _bfd_error_handler (_("%B: Unable to sort relocs - "
8921 "they are in more than one size"),
8923 bfd_set_error (bfd_error_invalid_operation);
8929 use_rela_initialised = TRUE;
8933 else if ((o->size % bed->s->sizeof_rel) == 0)
8935 /* Section size is only divisible by rel. */
8936 if (use_rela_initialised && use_rela)
8938 _bfd_error_handler (_("%B: Unable to sort relocs - "
8939 "they are in more than one size"),
8941 bfd_set_error (bfd_error_invalid_operation);
8947 use_rela_initialised = TRUE;
8952 /* The section size is not divisible by either -
8953 something is wrong. */
8954 _bfd_error_handler (_("%B: Unable to sort relocs - "
8955 "they are of an unknown size"), abfd);
8956 bfd_set_error (bfd_error_invalid_operation);
8961 if (! use_rela_initialised)
8965 else if (rela_dyn != NULL && rela_dyn->size > 0)
8967 else if (rel_dyn != NULL && rel_dyn->size > 0)
8974 dynamic_relocs = rela_dyn;
8975 ext_size = bed->s->sizeof_rela;
8976 swap_in = bed->s->swap_reloca_in;
8977 swap_out = bed->s->swap_reloca_out;
8981 dynamic_relocs = rel_dyn;
8982 ext_size = bed->s->sizeof_rel;
8983 swap_in = bed->s->swap_reloc_in;
8984 swap_out = bed->s->swap_reloc_out;
8988 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8989 if (lo->type == bfd_indirect_link_order)
8990 size += lo->u.indirect.section->size;
8992 if (size != dynamic_relocs->size)
8995 sort_elt = (sizeof (struct elf_link_sort_rela)
8996 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8998 count = dynamic_relocs->size / ext_size;
9001 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9005 (*info->callbacks->warning)
9006 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
9010 if (bed->s->arch_size == 32)
9011 r_sym_mask = ~(bfd_vma) 0xff;
9013 r_sym_mask = ~(bfd_vma) 0xffffffff;
9015 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9016 if (lo->type == bfd_indirect_link_order)
9018 bfd_byte *erel, *erelend;
9019 asection *o = lo->u.indirect.section;
9021 if (o->contents == NULL && o->size != 0)
9023 /* This is a reloc section that is being handled as a normal
9024 section. See bfd_section_from_shdr. We can't combine
9025 relocs in this case. */
9030 erelend = o->contents + o->size;
9031 p = sort + o->output_offset * opb / ext_size * sort_elt;
9033 while (erel < erelend)
9035 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9037 (*swap_in) (abfd, erel, s->rela);
9038 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9039 s->u.sym_mask = r_sym_mask;
9045 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9047 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9049 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9050 if (s->type != reloc_class_relative)
9056 sq = (struct elf_link_sort_rela *) s_non_relative;
9057 for (; i < count; i++, p += sort_elt)
9059 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9060 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9062 sp->u.offset = sq->rela->r_offset;
9065 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9067 struct elf_link_hash_table *htab = elf_hash_table (info);
9068 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9070 /* We have plt relocs in .rela.dyn. */
9071 sq = (struct elf_link_sort_rela *) sort;
9072 for (i = 0; i < count; i++)
9073 if (sq[count - i - 1].type != reloc_class_plt)
9075 if (i != 0 && htab->srelplt->size == i * ext_size)
9077 struct bfd_link_order **plo;
9078 /* Put srelplt link_order last. This is so the output_offset
9079 set in the next loop is correct for DT_JMPREL. */
9080 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9081 if ((*plo)->type == bfd_indirect_link_order
9082 && (*plo)->u.indirect.section == htab->srelplt)
9088 plo = &(*plo)->next;
9091 dynamic_relocs->map_tail.link_order = lo;
9096 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9097 if (lo->type == bfd_indirect_link_order)
9099 bfd_byte *erel, *erelend;
9100 asection *o = lo->u.indirect.section;
9103 erelend = o->contents + o->size;
9104 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9105 while (erel < erelend)
9107 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9108 (*swap_out) (abfd, s->rela, erel);
9115 *psec = dynamic_relocs;
9119 /* Add a symbol to the output symbol string table. */
9122 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9124 Elf_Internal_Sym *elfsym,
9125 asection *input_sec,
9126 struct elf_link_hash_entry *h)
9128 int (*output_symbol_hook)
9129 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9130 struct elf_link_hash_entry *);
9131 struct elf_link_hash_table *hash_table;
9132 const struct elf_backend_data *bed;
9133 bfd_size_type strtabsize;
9135 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9137 bed = get_elf_backend_data (flinfo->output_bfd);
9138 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9139 if (output_symbol_hook != NULL)
9141 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9148 || (input_sec->flags & SEC_EXCLUDE))
9149 elfsym->st_name = (unsigned long) -1;
9152 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9153 to get the final offset for st_name. */
9155 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9157 if (elfsym->st_name == (unsigned long) -1)
9161 hash_table = elf_hash_table (flinfo->info);
9162 strtabsize = hash_table->strtabsize;
9163 if (strtabsize <= hash_table->strtabcount)
9165 strtabsize += strtabsize;
9166 hash_table->strtabsize = strtabsize;
9167 strtabsize *= sizeof (*hash_table->strtab);
9169 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9171 if (hash_table->strtab == NULL)
9174 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9175 hash_table->strtab[hash_table->strtabcount].dest_index
9176 = hash_table->strtabcount;
9177 hash_table->strtab[hash_table->strtabcount].destshndx_index
9178 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9180 bfd_get_symcount (flinfo->output_bfd) += 1;
9181 hash_table->strtabcount += 1;
9186 /* Swap symbols out to the symbol table and flush the output symbols to
9190 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9192 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9195 const struct elf_backend_data *bed;
9197 Elf_Internal_Shdr *hdr;
9201 if (!hash_table->strtabcount)
9204 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9206 bed = get_elf_backend_data (flinfo->output_bfd);
9208 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9209 symbuf = (bfd_byte *) bfd_malloc (amt);
9213 if (flinfo->symshndxbuf)
9215 amt = sizeof (Elf_External_Sym_Shndx);
9216 amt *= bfd_get_symcount (flinfo->output_bfd);
9217 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9218 if (flinfo->symshndxbuf == NULL)
9225 for (i = 0; i < hash_table->strtabcount; i++)
9227 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9228 if (elfsym->sym.st_name == (unsigned long) -1)
9229 elfsym->sym.st_name = 0;
9232 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9233 elfsym->sym.st_name);
9234 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9235 ((bfd_byte *) symbuf
9236 + (elfsym->dest_index
9237 * bed->s->sizeof_sym)),
9238 (flinfo->symshndxbuf
9239 + elfsym->destshndx_index));
9242 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9243 pos = hdr->sh_offset + hdr->sh_size;
9244 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9245 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9246 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9248 hdr->sh_size += amt;
9256 free (hash_table->strtab);
9257 hash_table->strtab = NULL;
9262 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9265 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9267 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9268 && sym->st_shndx < SHN_LORESERVE)
9270 /* The gABI doesn't support dynamic symbols in output sections
9273 /* xgettext:c-format */
9274 (_("%B: Too many sections: %d (>= %d)"),
9275 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9276 bfd_set_error (bfd_error_nonrepresentable_section);
9282 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9283 allowing an unsatisfied unversioned symbol in the DSO to match a
9284 versioned symbol that would normally require an explicit version.
9285 We also handle the case that a DSO references a hidden symbol
9286 which may be satisfied by a versioned symbol in another DSO. */
9289 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9290 const struct elf_backend_data *bed,
9291 struct elf_link_hash_entry *h)
9294 struct elf_link_loaded_list *loaded;
9296 if (!is_elf_hash_table (info->hash))
9299 /* Check indirect symbol. */
9300 while (h->root.type == bfd_link_hash_indirect)
9301 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9303 switch (h->root.type)
9309 case bfd_link_hash_undefined:
9310 case bfd_link_hash_undefweak:
9311 abfd = h->root.u.undef.abfd;
9313 || (abfd->flags & DYNAMIC) == 0
9314 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9318 case bfd_link_hash_defined:
9319 case bfd_link_hash_defweak:
9320 abfd = h->root.u.def.section->owner;
9323 case bfd_link_hash_common:
9324 abfd = h->root.u.c.p->section->owner;
9327 BFD_ASSERT (abfd != NULL);
9329 for (loaded = elf_hash_table (info)->loaded;
9331 loaded = loaded->next)
9334 Elf_Internal_Shdr *hdr;
9338 Elf_Internal_Shdr *versymhdr;
9339 Elf_Internal_Sym *isym;
9340 Elf_Internal_Sym *isymend;
9341 Elf_Internal_Sym *isymbuf;
9342 Elf_External_Versym *ever;
9343 Elf_External_Versym *extversym;
9345 input = loaded->abfd;
9347 /* We check each DSO for a possible hidden versioned definition. */
9349 || (input->flags & DYNAMIC) == 0
9350 || elf_dynversym (input) == 0)
9353 hdr = &elf_tdata (input)->dynsymtab_hdr;
9355 symcount = hdr->sh_size / bed->s->sizeof_sym;
9356 if (elf_bad_symtab (input))
9358 extsymcount = symcount;
9363 extsymcount = symcount - hdr->sh_info;
9364 extsymoff = hdr->sh_info;
9367 if (extsymcount == 0)
9370 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9372 if (isymbuf == NULL)
9375 /* Read in any version definitions. */
9376 versymhdr = &elf_tdata (input)->dynversym_hdr;
9377 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9378 if (extversym == NULL)
9381 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9382 || (bfd_bread (extversym, versymhdr->sh_size, input)
9383 != versymhdr->sh_size))
9391 ever = extversym + extsymoff;
9392 isymend = isymbuf + extsymcount;
9393 for (isym = isymbuf; isym < isymend; isym++, ever++)
9396 Elf_Internal_Versym iver;
9397 unsigned short version_index;
9399 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9400 || isym->st_shndx == SHN_UNDEF)
9403 name = bfd_elf_string_from_elf_section (input,
9406 if (strcmp (name, h->root.root.string) != 0)
9409 _bfd_elf_swap_versym_in (input, ever, &iver);
9411 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9413 && h->forced_local))
9415 /* If we have a non-hidden versioned sym, then it should
9416 have provided a definition for the undefined sym unless
9417 it is defined in a non-shared object and forced local.
9422 version_index = iver.vs_vers & VERSYM_VERSION;
9423 if (version_index == 1 || version_index == 2)
9425 /* This is the base or first version. We can use it. */
9439 /* Convert ELF common symbol TYPE. */
9442 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9444 /* Commom symbol can only appear in relocatable link. */
9445 if (!bfd_link_relocatable (info))
9447 switch (info->elf_stt_common)
9451 case elf_stt_common:
9454 case no_elf_stt_common:
9461 /* Add an external symbol to the symbol table. This is called from
9462 the hash table traversal routine. When generating a shared object,
9463 we go through the symbol table twice. The first time we output
9464 anything that might have been forced to local scope in a version
9465 script. The second time we output the symbols that are still
9469 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9471 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9472 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9473 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9475 Elf_Internal_Sym sym;
9476 asection *input_sec;
9477 const struct elf_backend_data *bed;
9482 if (h->root.type == bfd_link_hash_warning)
9484 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9485 if (h->root.type == bfd_link_hash_new)
9489 /* Decide whether to output this symbol in this pass. */
9490 if (eoinfo->localsyms)
9492 if (!h->forced_local)
9497 if (h->forced_local)
9501 bed = get_elf_backend_data (flinfo->output_bfd);
9503 if (h->root.type == bfd_link_hash_undefined)
9505 /* If we have an undefined symbol reference here then it must have
9506 come from a shared library that is being linked in. (Undefined
9507 references in regular files have already been handled unless
9508 they are in unreferenced sections which are removed by garbage
9510 bfd_boolean ignore_undef = FALSE;
9512 /* Some symbols may be special in that the fact that they're
9513 undefined can be safely ignored - let backend determine that. */
9514 if (bed->elf_backend_ignore_undef_symbol)
9515 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9517 /* If we are reporting errors for this situation then do so now. */
9520 && (!h->ref_regular || flinfo->info->gc_sections)
9521 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9522 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9523 (*flinfo->info->callbacks->undefined_symbol)
9524 (flinfo->info, h->root.root.string,
9525 h->ref_regular ? NULL : h->root.u.undef.abfd,
9527 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9529 /* Strip a global symbol defined in a discarded section. */
9534 /* We should also warn if a forced local symbol is referenced from
9535 shared libraries. */
9536 if (bfd_link_executable (flinfo->info)
9541 && h->ref_dynamic_nonweak
9542 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9546 struct elf_link_hash_entry *hi = h;
9548 /* Check indirect symbol. */
9549 while (hi->root.type == bfd_link_hash_indirect)
9550 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9552 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9553 /* xgettext:c-format */
9554 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9555 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9556 /* xgettext:c-format */
9557 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9559 /* xgettext:c-format */
9560 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9561 def_bfd = flinfo->output_bfd;
9562 if (hi->root.u.def.section != bfd_abs_section_ptr)
9563 def_bfd = hi->root.u.def.section->owner;
9564 _bfd_error_handler (msg, flinfo->output_bfd,
9565 h->root.root.string, def_bfd);
9566 bfd_set_error (bfd_error_bad_value);
9567 eoinfo->failed = TRUE;
9571 /* We don't want to output symbols that have never been mentioned by
9572 a regular file, or that we have been told to strip. However, if
9573 h->indx is set to -2, the symbol is used by a reloc and we must
9578 else if ((h->def_dynamic
9580 || h->root.type == bfd_link_hash_new)
9584 else if (flinfo->info->strip == strip_all)
9586 else if (flinfo->info->strip == strip_some
9587 && bfd_hash_lookup (flinfo->info->keep_hash,
9588 h->root.root.string, FALSE, FALSE) == NULL)
9590 else if ((h->root.type == bfd_link_hash_defined
9591 || h->root.type == bfd_link_hash_defweak)
9592 && ((flinfo->info->strip_discarded
9593 && discarded_section (h->root.u.def.section))
9594 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9595 && h->root.u.def.section->owner != NULL
9596 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9598 else if ((h->root.type == bfd_link_hash_undefined
9599 || h->root.type == bfd_link_hash_undefweak)
9600 && h->root.u.undef.abfd != NULL
9601 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9606 /* If we're stripping it, and it's not a dynamic symbol, there's
9607 nothing else to do. However, if it is a forced local symbol or
9608 an ifunc symbol we need to give the backend finish_dynamic_symbol
9609 function a chance to make it dynamic. */
9612 && type != STT_GNU_IFUNC
9613 && !h->forced_local)
9617 sym.st_size = h->size;
9618 sym.st_other = h->other;
9619 switch (h->root.type)
9622 case bfd_link_hash_new:
9623 case bfd_link_hash_warning:
9627 case bfd_link_hash_undefined:
9628 case bfd_link_hash_undefweak:
9629 input_sec = bfd_und_section_ptr;
9630 sym.st_shndx = SHN_UNDEF;
9633 case bfd_link_hash_defined:
9634 case bfd_link_hash_defweak:
9636 input_sec = h->root.u.def.section;
9637 if (input_sec->output_section != NULL)
9640 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9641 input_sec->output_section);
9642 if (sym.st_shndx == SHN_BAD)
9645 /* xgettext:c-format */
9646 (_("%B: could not find output section %A for input section %A"),
9647 flinfo->output_bfd, input_sec->output_section, input_sec);
9648 bfd_set_error (bfd_error_nonrepresentable_section);
9649 eoinfo->failed = TRUE;
9653 /* ELF symbols in relocatable files are section relative,
9654 but in nonrelocatable files they are virtual
9656 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9657 if (!bfd_link_relocatable (flinfo->info))
9659 sym.st_value += input_sec->output_section->vma;
9660 if (h->type == STT_TLS)
9662 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9663 if (tls_sec != NULL)
9664 sym.st_value -= tls_sec->vma;
9670 BFD_ASSERT (input_sec->owner == NULL
9671 || (input_sec->owner->flags & DYNAMIC) != 0);
9672 sym.st_shndx = SHN_UNDEF;
9673 input_sec = bfd_und_section_ptr;
9678 case bfd_link_hash_common:
9679 input_sec = h->root.u.c.p->section;
9680 sym.st_shndx = bed->common_section_index (input_sec);
9681 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9684 case bfd_link_hash_indirect:
9685 /* These symbols are created by symbol versioning. They point
9686 to the decorated version of the name. For example, if the
9687 symbol foo@@GNU_1.2 is the default, which should be used when
9688 foo is used with no version, then we add an indirect symbol
9689 foo which points to foo@@GNU_1.2. We ignore these symbols,
9690 since the indirected symbol is already in the hash table. */
9694 if (type == STT_COMMON || type == STT_OBJECT)
9695 switch (h->root.type)
9697 case bfd_link_hash_common:
9698 type = elf_link_convert_common_type (flinfo->info, type);
9700 case bfd_link_hash_defined:
9701 case bfd_link_hash_defweak:
9702 if (bed->common_definition (&sym))
9703 type = elf_link_convert_common_type (flinfo->info, type);
9707 case bfd_link_hash_undefined:
9708 case bfd_link_hash_undefweak:
9714 if (h->forced_local)
9716 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9717 /* Turn off visibility on local symbol. */
9718 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9720 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9721 else if (h->unique_global && h->def_regular)
9722 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9723 else if (h->root.type == bfd_link_hash_undefweak
9724 || h->root.type == bfd_link_hash_defweak)
9725 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9727 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9728 sym.st_target_internal = h->target_internal;
9730 /* Give the processor backend a chance to tweak the symbol value,
9731 and also to finish up anything that needs to be done for this
9732 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9733 forced local syms when non-shared is due to a historical quirk.
9734 STT_GNU_IFUNC symbol must go through PLT. */
9735 if ((h->type == STT_GNU_IFUNC
9737 && !bfd_link_relocatable (flinfo->info))
9738 || ((h->dynindx != -1
9740 && ((bfd_link_pic (flinfo->info)
9741 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9742 || h->root.type != bfd_link_hash_undefweak))
9743 || !h->forced_local)
9744 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9746 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9747 (flinfo->output_bfd, flinfo->info, h, &sym)))
9749 eoinfo->failed = TRUE;
9754 /* If we are marking the symbol as undefined, and there are no
9755 non-weak references to this symbol from a regular object, then
9756 mark the symbol as weak undefined; if there are non-weak
9757 references, mark the symbol as strong. We can't do this earlier,
9758 because it might not be marked as undefined until the
9759 finish_dynamic_symbol routine gets through with it. */
9760 if (sym.st_shndx == SHN_UNDEF
9762 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9763 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9766 type = ELF_ST_TYPE (sym.st_info);
9768 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9769 if (type == STT_GNU_IFUNC)
9772 if (h->ref_regular_nonweak)
9773 bindtype = STB_GLOBAL;
9775 bindtype = STB_WEAK;
9776 sym.st_info = ELF_ST_INFO (bindtype, type);
9779 /* If this is a symbol defined in a dynamic library, don't use the
9780 symbol size from the dynamic library. Relinking an executable
9781 against a new library may introduce gratuitous changes in the
9782 executable's symbols if we keep the size. */
9783 if (sym.st_shndx == SHN_UNDEF
9788 /* If a non-weak symbol with non-default visibility is not defined
9789 locally, it is a fatal error. */
9790 if (!bfd_link_relocatable (flinfo->info)
9791 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9792 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9793 && h->root.type == bfd_link_hash_undefined
9798 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9799 /* xgettext:c-format */
9800 msg = _("%B: protected symbol `%s' isn't defined");
9801 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9802 /* xgettext:c-format */
9803 msg = _("%B: internal symbol `%s' isn't defined");
9805 /* xgettext:c-format */
9806 msg = _("%B: hidden symbol `%s' isn't defined");
9807 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9808 bfd_set_error (bfd_error_bad_value);
9809 eoinfo->failed = TRUE;
9813 /* If this symbol should be put in the .dynsym section, then put it
9814 there now. We already know the symbol index. We also fill in
9815 the entry in the .hash section. */
9816 if (elf_hash_table (flinfo->info)->dynsym != NULL
9818 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9822 /* Since there is no version information in the dynamic string,
9823 if there is no version info in symbol version section, we will
9824 have a run-time problem if not linking executable, referenced
9825 by shared library, or not bound locally. */
9826 if (h->verinfo.verdef == NULL
9827 && (!bfd_link_executable (flinfo->info)
9829 || !h->def_regular))
9831 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9833 if (p && p [1] != '\0')
9836 /* xgettext:c-format */
9837 (_("%B: No symbol version section for versioned symbol `%s'"),
9838 flinfo->output_bfd, h->root.root.string);
9839 eoinfo->failed = TRUE;
9844 sym.st_name = h->dynstr_index;
9845 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9846 + h->dynindx * bed->s->sizeof_sym);
9847 if (!check_dynsym (flinfo->output_bfd, &sym))
9849 eoinfo->failed = TRUE;
9852 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9854 if (flinfo->hash_sec != NULL)
9856 size_t hash_entry_size;
9857 bfd_byte *bucketpos;
9862 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9863 bucket = h->u.elf_hash_value % bucketcount;
9866 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9867 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9868 + (bucket + 2) * hash_entry_size);
9869 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9870 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9872 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9873 ((bfd_byte *) flinfo->hash_sec->contents
9874 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9877 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9879 Elf_Internal_Versym iversym;
9880 Elf_External_Versym *eversym;
9882 if (!h->def_regular)
9884 if (h->verinfo.verdef == NULL
9885 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9886 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9887 iversym.vs_vers = 0;
9889 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9893 if (h->verinfo.vertree == NULL)
9894 iversym.vs_vers = 1;
9896 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9897 if (flinfo->info->create_default_symver)
9901 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9903 if (h->versioned == versioned_hidden && h->def_regular)
9904 iversym.vs_vers |= VERSYM_HIDDEN;
9906 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9907 eversym += h->dynindx;
9908 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9912 /* If the symbol is undefined, and we didn't output it to .dynsym,
9913 strip it from .symtab too. Obviously we can't do this for
9914 relocatable output or when needed for --emit-relocs. */
9915 else if (input_sec == bfd_und_section_ptr
9917 && !bfd_link_relocatable (flinfo->info))
9919 /* Also strip others that we couldn't earlier due to dynamic symbol
9923 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9926 /* Output a FILE symbol so that following locals are not associated
9927 with the wrong input file. We need one for forced local symbols
9928 if we've seen more than one FILE symbol or when we have exactly
9929 one FILE symbol but global symbols are present in a file other
9930 than the one with the FILE symbol. We also need one if linker
9931 defined symbols are present. In practice these conditions are
9932 always met, so just emit the FILE symbol unconditionally. */
9933 if (eoinfo->localsyms
9934 && !eoinfo->file_sym_done
9935 && eoinfo->flinfo->filesym_count != 0)
9937 Elf_Internal_Sym fsym;
9939 memset (&fsym, 0, sizeof (fsym));
9940 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9941 fsym.st_shndx = SHN_ABS;
9942 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9943 bfd_und_section_ptr, NULL))
9946 eoinfo->file_sym_done = TRUE;
9949 indx = bfd_get_symcount (flinfo->output_bfd);
9950 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9954 eoinfo->failed = TRUE;
9959 else if (h->indx == -2)
9965 /* Return TRUE if special handling is done for relocs in SEC against
9966 symbols defined in discarded sections. */
9969 elf_section_ignore_discarded_relocs (asection *sec)
9971 const struct elf_backend_data *bed;
9973 switch (sec->sec_info_type)
9975 case SEC_INFO_TYPE_STABS:
9976 case SEC_INFO_TYPE_EH_FRAME:
9977 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9983 bed = get_elf_backend_data (sec->owner);
9984 if (bed->elf_backend_ignore_discarded_relocs != NULL
9985 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9991 /* Return a mask saying how ld should treat relocations in SEC against
9992 symbols defined in discarded sections. If this function returns
9993 COMPLAIN set, ld will issue a warning message. If this function
9994 returns PRETEND set, and the discarded section was link-once and the
9995 same size as the kept link-once section, ld will pretend that the
9996 symbol was actually defined in the kept section. Otherwise ld will
9997 zero the reloc (at least that is the intent, but some cooperation by
9998 the target dependent code is needed, particularly for REL targets). */
10001 _bfd_elf_default_action_discarded (asection *sec)
10003 if (sec->flags & SEC_DEBUGGING)
10006 if (strcmp (".eh_frame", sec->name) == 0)
10009 if (strcmp (".gcc_except_table", sec->name) == 0)
10012 return COMPLAIN | PRETEND;
10015 /* Find a match between a section and a member of a section group. */
10018 match_group_member (asection *sec, asection *group,
10019 struct bfd_link_info *info)
10021 asection *first = elf_next_in_group (group);
10022 asection *s = first;
10026 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10029 s = elf_next_in_group (s);
10037 /* Check if the kept section of a discarded section SEC can be used
10038 to replace it. Return the replacement if it is OK. Otherwise return
10042 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10046 kept = sec->kept_section;
10049 if ((kept->flags & SEC_GROUP) != 0)
10050 kept = match_group_member (sec, kept, info);
10052 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10053 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10055 sec->kept_section = kept;
10060 /* Link an input file into the linker output file. This function
10061 handles all the sections and relocations of the input file at once.
10062 This is so that we only have to read the local symbols once, and
10063 don't have to keep them in memory. */
10066 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10068 int (*relocate_section)
10069 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10070 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10072 Elf_Internal_Shdr *symtab_hdr;
10073 size_t locsymcount;
10075 Elf_Internal_Sym *isymbuf;
10076 Elf_Internal_Sym *isym;
10077 Elf_Internal_Sym *isymend;
10079 asection **ppsection;
10081 const struct elf_backend_data *bed;
10082 struct elf_link_hash_entry **sym_hashes;
10083 bfd_size_type address_size;
10084 bfd_vma r_type_mask;
10086 bfd_boolean have_file_sym = FALSE;
10088 output_bfd = flinfo->output_bfd;
10089 bed = get_elf_backend_data (output_bfd);
10090 relocate_section = bed->elf_backend_relocate_section;
10092 /* If this is a dynamic object, we don't want to do anything here:
10093 we don't want the local symbols, and we don't want the section
10095 if ((input_bfd->flags & DYNAMIC) != 0)
10098 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10099 if (elf_bad_symtab (input_bfd))
10101 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10106 locsymcount = symtab_hdr->sh_info;
10107 extsymoff = symtab_hdr->sh_info;
10110 /* Read the local symbols. */
10111 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10112 if (isymbuf == NULL && locsymcount != 0)
10114 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10115 flinfo->internal_syms,
10116 flinfo->external_syms,
10117 flinfo->locsym_shndx);
10118 if (isymbuf == NULL)
10122 /* Find local symbol sections and adjust values of symbols in
10123 SEC_MERGE sections. Write out those local symbols we know are
10124 going into the output file. */
10125 isymend = isymbuf + locsymcount;
10126 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10128 isym++, pindex++, ppsection++)
10132 Elf_Internal_Sym osym;
10138 if (elf_bad_symtab (input_bfd))
10140 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10147 if (isym->st_shndx == SHN_UNDEF)
10148 isec = bfd_und_section_ptr;
10149 else if (isym->st_shndx == SHN_ABS)
10150 isec = bfd_abs_section_ptr;
10151 else if (isym->st_shndx == SHN_COMMON)
10152 isec = bfd_com_section_ptr;
10155 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10158 /* Don't attempt to output symbols with st_shnx in the
10159 reserved range other than SHN_ABS and SHN_COMMON. */
10163 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10164 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10166 _bfd_merged_section_offset (output_bfd, &isec,
10167 elf_section_data (isec)->sec_info,
10173 /* Don't output the first, undefined, symbol. In fact, don't
10174 output any undefined local symbol. */
10175 if (isec == bfd_und_section_ptr)
10178 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10180 /* We never output section symbols. Instead, we use the
10181 section symbol of the corresponding section in the output
10186 /* If we are stripping all symbols, we don't want to output this
10188 if (flinfo->info->strip == strip_all)
10191 /* If we are discarding all local symbols, we don't want to
10192 output this one. If we are generating a relocatable output
10193 file, then some of the local symbols may be required by
10194 relocs; we output them below as we discover that they are
10196 if (flinfo->info->discard == discard_all)
10199 /* If this symbol is defined in a section which we are
10200 discarding, we don't need to keep it. */
10201 if (isym->st_shndx != SHN_UNDEF
10202 && isym->st_shndx < SHN_LORESERVE
10203 && bfd_section_removed_from_list (output_bfd,
10204 isec->output_section))
10207 /* Get the name of the symbol. */
10208 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10213 /* See if we are discarding symbols with this name. */
10214 if ((flinfo->info->strip == strip_some
10215 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10217 || (((flinfo->info->discard == discard_sec_merge
10218 && (isec->flags & SEC_MERGE)
10219 && !bfd_link_relocatable (flinfo->info))
10220 || flinfo->info->discard == discard_l)
10221 && bfd_is_local_label_name (input_bfd, name)))
10224 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10226 if (input_bfd->lto_output)
10227 /* -flto puts a temp file name here. This means builds
10228 are not reproducible. Discard the symbol. */
10230 have_file_sym = TRUE;
10231 flinfo->filesym_count += 1;
10233 if (!have_file_sym)
10235 /* In the absence of debug info, bfd_find_nearest_line uses
10236 FILE symbols to determine the source file for local
10237 function symbols. Provide a FILE symbol here if input
10238 files lack such, so that their symbols won't be
10239 associated with a previous input file. It's not the
10240 source file, but the best we can do. */
10241 have_file_sym = TRUE;
10242 flinfo->filesym_count += 1;
10243 memset (&osym, 0, sizeof (osym));
10244 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10245 osym.st_shndx = SHN_ABS;
10246 if (!elf_link_output_symstrtab (flinfo,
10247 (input_bfd->lto_output ? NULL
10248 : input_bfd->filename),
10249 &osym, bfd_abs_section_ptr,
10256 /* Adjust the section index for the output file. */
10257 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10258 isec->output_section);
10259 if (osym.st_shndx == SHN_BAD)
10262 /* ELF symbols in relocatable files are section relative, but
10263 in executable files they are virtual addresses. Note that
10264 this code assumes that all ELF sections have an associated
10265 BFD section with a reasonable value for output_offset; below
10266 we assume that they also have a reasonable value for
10267 output_section. Any special sections must be set up to meet
10268 these requirements. */
10269 osym.st_value += isec->output_offset;
10270 if (!bfd_link_relocatable (flinfo->info))
10272 osym.st_value += isec->output_section->vma;
10273 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10275 /* STT_TLS symbols are relative to PT_TLS segment base. */
10276 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10277 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10281 indx = bfd_get_symcount (output_bfd);
10282 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10289 if (bed->s->arch_size == 32)
10291 r_type_mask = 0xff;
10297 r_type_mask = 0xffffffff;
10302 /* Relocate the contents of each section. */
10303 sym_hashes = elf_sym_hashes (input_bfd);
10304 for (o = input_bfd->sections; o != NULL; o = o->next)
10306 bfd_byte *contents;
10308 if (! o->linker_mark)
10310 /* This section was omitted from the link. */
10314 if (!flinfo->info->resolve_section_groups
10315 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10317 /* Deal with the group signature symbol. */
10318 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10319 unsigned long symndx = sec_data->this_hdr.sh_info;
10320 asection *osec = o->output_section;
10322 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10323 if (symndx >= locsymcount
10324 || (elf_bad_symtab (input_bfd)
10325 && flinfo->sections[symndx] == NULL))
10327 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10328 while (h->root.type == bfd_link_hash_indirect
10329 || h->root.type == bfd_link_hash_warning)
10330 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10331 /* Arrange for symbol to be output. */
10333 elf_section_data (osec)->this_hdr.sh_info = -2;
10335 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10337 /* We'll use the output section target_index. */
10338 asection *sec = flinfo->sections[symndx]->output_section;
10339 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10343 if (flinfo->indices[symndx] == -1)
10345 /* Otherwise output the local symbol now. */
10346 Elf_Internal_Sym sym = isymbuf[symndx];
10347 asection *sec = flinfo->sections[symndx]->output_section;
10352 name = bfd_elf_string_from_elf_section (input_bfd,
10353 symtab_hdr->sh_link,
10358 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10360 if (sym.st_shndx == SHN_BAD)
10363 sym.st_value += o->output_offset;
10365 indx = bfd_get_symcount (output_bfd);
10366 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10371 flinfo->indices[symndx] = indx;
10375 elf_section_data (osec)->this_hdr.sh_info
10376 = flinfo->indices[symndx];
10380 if ((o->flags & SEC_HAS_CONTENTS) == 0
10381 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10384 if ((o->flags & SEC_LINKER_CREATED) != 0)
10386 /* Section was created by _bfd_elf_link_create_dynamic_sections
10391 /* Get the contents of the section. They have been cached by a
10392 relaxation routine. Note that o is a section in an input
10393 file, so the contents field will not have been set by any of
10394 the routines which work on output files. */
10395 if (elf_section_data (o)->this_hdr.contents != NULL)
10397 contents = elf_section_data (o)->this_hdr.contents;
10398 if (bed->caches_rawsize
10400 && o->rawsize < o->size)
10402 memcpy (flinfo->contents, contents, o->rawsize);
10403 contents = flinfo->contents;
10408 contents = flinfo->contents;
10409 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10413 if ((o->flags & SEC_RELOC) != 0)
10415 Elf_Internal_Rela *internal_relocs;
10416 Elf_Internal_Rela *rel, *relend;
10417 int action_discarded;
10420 /* Get the swapped relocs. */
10422 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10423 flinfo->internal_relocs, FALSE);
10424 if (internal_relocs == NULL
10425 && o->reloc_count > 0)
10428 /* We need to reverse-copy input .ctors/.dtors sections if
10429 they are placed in .init_array/.finit_array for output. */
10430 if (o->size > address_size
10431 && ((strncmp (o->name, ".ctors", 6) == 0
10432 && strcmp (o->output_section->name,
10433 ".init_array") == 0)
10434 || (strncmp (o->name, ".dtors", 6) == 0
10435 && strcmp (o->output_section->name,
10436 ".fini_array") == 0))
10437 && (o->name[6] == 0 || o->name[6] == '.'))
10439 if (o->size * bed->s->int_rels_per_ext_rel
10440 != o->reloc_count * address_size)
10443 /* xgettext:c-format */
10444 (_("error: %B: size of section %A is not "
10445 "multiple of address size"),
10447 bfd_set_error (bfd_error_bad_value);
10450 o->flags |= SEC_ELF_REVERSE_COPY;
10453 action_discarded = -1;
10454 if (!elf_section_ignore_discarded_relocs (o))
10455 action_discarded = (*bed->action_discarded) (o);
10457 /* Run through the relocs evaluating complex reloc symbols and
10458 looking for relocs against symbols from discarded sections
10459 or section symbols from removed link-once sections.
10460 Complain about relocs against discarded sections. Zero
10461 relocs against removed link-once sections. */
10463 rel = internal_relocs;
10464 relend = rel + o->reloc_count;
10465 for ( ; rel < relend; rel++)
10467 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10468 unsigned int s_type;
10469 asection **ps, *sec;
10470 struct elf_link_hash_entry *h = NULL;
10471 const char *sym_name;
10473 if (r_symndx == STN_UNDEF)
10476 if (r_symndx >= locsymcount
10477 || (elf_bad_symtab (input_bfd)
10478 && flinfo->sections[r_symndx] == NULL))
10480 h = sym_hashes[r_symndx - extsymoff];
10482 /* Badly formatted input files can contain relocs that
10483 reference non-existant symbols. Check here so that
10484 we do not seg fault. */
10488 /* xgettext:c-format */
10489 (_("error: %B contains a reloc (%#Lx) for section %A "
10490 "that references a non-existent global symbol"),
10491 input_bfd, rel->r_info, o);
10492 bfd_set_error (bfd_error_bad_value);
10496 while (h->root.type == bfd_link_hash_indirect
10497 || h->root.type == bfd_link_hash_warning)
10498 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10502 /* If a plugin symbol is referenced from a non-IR file,
10503 mark the symbol as undefined. Note that the
10504 linker may attach linker created dynamic sections
10505 to the plugin bfd. Symbols defined in linker
10506 created sections are not plugin symbols. */
10507 if ((h->root.non_ir_ref_regular
10508 || h->root.non_ir_ref_dynamic)
10509 && (h->root.type == bfd_link_hash_defined
10510 || h->root.type == bfd_link_hash_defweak)
10511 && (h->root.u.def.section->flags
10512 & SEC_LINKER_CREATED) == 0
10513 && h->root.u.def.section->owner != NULL
10514 && (h->root.u.def.section->owner->flags
10515 & BFD_PLUGIN) != 0)
10517 h->root.type = bfd_link_hash_undefined;
10518 h->root.u.undef.abfd = h->root.u.def.section->owner;
10522 if (h->root.type == bfd_link_hash_defined
10523 || h->root.type == bfd_link_hash_defweak)
10524 ps = &h->root.u.def.section;
10526 sym_name = h->root.root.string;
10530 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10532 s_type = ELF_ST_TYPE (sym->st_info);
10533 ps = &flinfo->sections[r_symndx];
10534 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10538 if ((s_type == STT_RELC || s_type == STT_SRELC)
10539 && !bfd_link_relocatable (flinfo->info))
10542 bfd_vma dot = (rel->r_offset
10543 + o->output_offset + o->output_section->vma);
10545 printf ("Encountered a complex symbol!");
10546 printf (" (input_bfd %s, section %s, reloc %ld\n",
10547 input_bfd->filename, o->name,
10548 (long) (rel - internal_relocs));
10549 printf (" symbol: idx %8.8lx, name %s\n",
10550 r_symndx, sym_name);
10551 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10552 (unsigned long) rel->r_info,
10553 (unsigned long) rel->r_offset);
10555 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10556 isymbuf, locsymcount, s_type == STT_SRELC))
10559 /* Symbol evaluated OK. Update to absolute value. */
10560 set_symbol_value (input_bfd, isymbuf, locsymcount,
10565 if (action_discarded != -1 && ps != NULL)
10567 /* Complain if the definition comes from a
10568 discarded section. */
10569 if ((sec = *ps) != NULL && discarded_section (sec))
10571 BFD_ASSERT (r_symndx != STN_UNDEF);
10572 if (action_discarded & COMPLAIN)
10573 (*flinfo->info->callbacks->einfo)
10574 /* xgettext:c-format */
10575 (_("%X`%s' referenced in section `%A' of %B: "
10576 "defined in discarded section `%A' of %B\n"),
10577 sym_name, o, input_bfd, sec, sec->owner);
10579 /* Try to do the best we can to support buggy old
10580 versions of gcc. Pretend that the symbol is
10581 really defined in the kept linkonce section.
10582 FIXME: This is quite broken. Modifying the
10583 symbol here means we will be changing all later
10584 uses of the symbol, not just in this section. */
10585 if (action_discarded & PRETEND)
10589 kept = _bfd_elf_check_kept_section (sec,
10601 /* Relocate the section by invoking a back end routine.
10603 The back end routine is responsible for adjusting the
10604 section contents as necessary, and (if using Rela relocs
10605 and generating a relocatable output file) adjusting the
10606 reloc addend as necessary.
10608 The back end routine does not have to worry about setting
10609 the reloc address or the reloc symbol index.
10611 The back end routine is given a pointer to the swapped in
10612 internal symbols, and can access the hash table entries
10613 for the external symbols via elf_sym_hashes (input_bfd).
10615 When generating relocatable output, the back end routine
10616 must handle STB_LOCAL/STT_SECTION symbols specially. The
10617 output symbol is going to be a section symbol
10618 corresponding to the output section, which will require
10619 the addend to be adjusted. */
10621 ret = (*relocate_section) (output_bfd, flinfo->info,
10622 input_bfd, o, contents,
10630 || bfd_link_relocatable (flinfo->info)
10631 || flinfo->info->emitrelocations)
10633 Elf_Internal_Rela *irela;
10634 Elf_Internal_Rela *irelaend, *irelamid;
10635 bfd_vma last_offset;
10636 struct elf_link_hash_entry **rel_hash;
10637 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10638 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10639 unsigned int next_erel;
10640 bfd_boolean rela_normal;
10641 struct bfd_elf_section_data *esdi, *esdo;
10643 esdi = elf_section_data (o);
10644 esdo = elf_section_data (o->output_section);
10645 rela_normal = FALSE;
10647 /* Adjust the reloc addresses and symbol indices. */
10649 irela = internal_relocs;
10650 irelaend = irela + o->reloc_count;
10651 rel_hash = esdo->rel.hashes + esdo->rel.count;
10652 /* We start processing the REL relocs, if any. When we reach
10653 IRELAMID in the loop, we switch to the RELA relocs. */
10655 if (esdi->rel.hdr != NULL)
10656 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10657 * bed->s->int_rels_per_ext_rel);
10658 rel_hash_list = rel_hash;
10659 rela_hash_list = NULL;
10660 last_offset = o->output_offset;
10661 if (!bfd_link_relocatable (flinfo->info))
10662 last_offset += o->output_section->vma;
10663 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10665 unsigned long r_symndx;
10667 Elf_Internal_Sym sym;
10669 if (next_erel == bed->s->int_rels_per_ext_rel)
10675 if (irela == irelamid)
10677 rel_hash = esdo->rela.hashes + esdo->rela.count;
10678 rela_hash_list = rel_hash;
10679 rela_normal = bed->rela_normal;
10682 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10685 if (irela->r_offset >= (bfd_vma) -2)
10687 /* This is a reloc for a deleted entry or somesuch.
10688 Turn it into an R_*_NONE reloc, at the same
10689 offset as the last reloc. elf_eh_frame.c and
10690 bfd_elf_discard_info rely on reloc offsets
10692 irela->r_offset = last_offset;
10694 irela->r_addend = 0;
10698 irela->r_offset += o->output_offset;
10700 /* Relocs in an executable have to be virtual addresses. */
10701 if (!bfd_link_relocatable (flinfo->info))
10702 irela->r_offset += o->output_section->vma;
10704 last_offset = irela->r_offset;
10706 r_symndx = irela->r_info >> r_sym_shift;
10707 if (r_symndx == STN_UNDEF)
10710 if (r_symndx >= locsymcount
10711 || (elf_bad_symtab (input_bfd)
10712 && flinfo->sections[r_symndx] == NULL))
10714 struct elf_link_hash_entry *rh;
10715 unsigned long indx;
10717 /* This is a reloc against a global symbol. We
10718 have not yet output all the local symbols, so
10719 we do not know the symbol index of any global
10720 symbol. We set the rel_hash entry for this
10721 reloc to point to the global hash table entry
10722 for this symbol. The symbol index is then
10723 set at the end of bfd_elf_final_link. */
10724 indx = r_symndx - extsymoff;
10725 rh = elf_sym_hashes (input_bfd)[indx];
10726 while (rh->root.type == bfd_link_hash_indirect
10727 || rh->root.type == bfd_link_hash_warning)
10728 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10730 /* Setting the index to -2 tells
10731 elf_link_output_extsym that this symbol is
10732 used by a reloc. */
10733 BFD_ASSERT (rh->indx < 0);
10740 /* This is a reloc against a local symbol. */
10743 sym = isymbuf[r_symndx];
10744 sec = flinfo->sections[r_symndx];
10745 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10747 /* I suppose the backend ought to fill in the
10748 section of any STT_SECTION symbol against a
10749 processor specific section. */
10750 r_symndx = STN_UNDEF;
10751 if (bfd_is_abs_section (sec))
10753 else if (sec == NULL || sec->owner == NULL)
10755 bfd_set_error (bfd_error_bad_value);
10760 asection *osec = sec->output_section;
10762 /* If we have discarded a section, the output
10763 section will be the absolute section. In
10764 case of discarded SEC_MERGE sections, use
10765 the kept section. relocate_section should
10766 have already handled discarded linkonce
10768 if (bfd_is_abs_section (osec)
10769 && sec->kept_section != NULL
10770 && sec->kept_section->output_section != NULL)
10772 osec = sec->kept_section->output_section;
10773 irela->r_addend -= osec->vma;
10776 if (!bfd_is_abs_section (osec))
10778 r_symndx = osec->target_index;
10779 if (r_symndx == STN_UNDEF)
10781 irela->r_addend += osec->vma;
10782 osec = _bfd_nearby_section (output_bfd, osec,
10784 irela->r_addend -= osec->vma;
10785 r_symndx = osec->target_index;
10790 /* Adjust the addend according to where the
10791 section winds up in the output section. */
10793 irela->r_addend += sec->output_offset;
10797 if (flinfo->indices[r_symndx] == -1)
10799 unsigned long shlink;
10804 if (flinfo->info->strip == strip_all)
10806 /* You can't do ld -r -s. */
10807 bfd_set_error (bfd_error_invalid_operation);
10811 /* This symbol was skipped earlier, but
10812 since it is needed by a reloc, we
10813 must output it now. */
10814 shlink = symtab_hdr->sh_link;
10815 name = (bfd_elf_string_from_elf_section
10816 (input_bfd, shlink, sym.st_name));
10820 osec = sec->output_section;
10822 _bfd_elf_section_from_bfd_section (output_bfd,
10824 if (sym.st_shndx == SHN_BAD)
10827 sym.st_value += sec->output_offset;
10828 if (!bfd_link_relocatable (flinfo->info))
10830 sym.st_value += osec->vma;
10831 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10833 /* STT_TLS symbols are relative to PT_TLS
10835 BFD_ASSERT (elf_hash_table (flinfo->info)
10836 ->tls_sec != NULL);
10837 sym.st_value -= (elf_hash_table (flinfo->info)
10842 indx = bfd_get_symcount (output_bfd);
10843 ret = elf_link_output_symstrtab (flinfo, name,
10849 flinfo->indices[r_symndx] = indx;
10854 r_symndx = flinfo->indices[r_symndx];
10857 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10858 | (irela->r_info & r_type_mask));
10861 /* Swap out the relocs. */
10862 input_rel_hdr = esdi->rel.hdr;
10863 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10865 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10870 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10871 * bed->s->int_rels_per_ext_rel);
10872 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10875 input_rela_hdr = esdi->rela.hdr;
10876 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10878 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10887 /* Write out the modified section contents. */
10888 if (bed->elf_backend_write_section
10889 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10892 /* Section written out. */
10894 else switch (o->sec_info_type)
10896 case SEC_INFO_TYPE_STABS:
10897 if (! (_bfd_write_section_stabs
10899 &elf_hash_table (flinfo->info)->stab_info,
10900 o, &elf_section_data (o)->sec_info, contents)))
10903 case SEC_INFO_TYPE_MERGE:
10904 if (! _bfd_write_merged_section (output_bfd, o,
10905 elf_section_data (o)->sec_info))
10908 case SEC_INFO_TYPE_EH_FRAME:
10910 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10915 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10917 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10925 if (! (o->flags & SEC_EXCLUDE))
10927 file_ptr offset = (file_ptr) o->output_offset;
10928 bfd_size_type todo = o->size;
10930 offset *= bfd_octets_per_byte (output_bfd);
10932 if ((o->flags & SEC_ELF_REVERSE_COPY))
10934 /* Reverse-copy input section to output. */
10937 todo -= address_size;
10938 if (! bfd_set_section_contents (output_bfd,
10946 offset += address_size;
10950 else if (! bfd_set_section_contents (output_bfd,
10964 /* Generate a reloc when linking an ELF file. This is a reloc
10965 requested by the linker, and does not come from any input file. This
10966 is used to build constructor and destructor tables when linking
10970 elf_reloc_link_order (bfd *output_bfd,
10971 struct bfd_link_info *info,
10972 asection *output_section,
10973 struct bfd_link_order *link_order)
10975 reloc_howto_type *howto;
10979 struct bfd_elf_section_reloc_data *reldata;
10980 struct elf_link_hash_entry **rel_hash_ptr;
10981 Elf_Internal_Shdr *rel_hdr;
10982 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10983 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10986 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10988 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10991 bfd_set_error (bfd_error_bad_value);
10995 addend = link_order->u.reloc.p->addend;
10998 reldata = &esdo->rel;
10999 else if (esdo->rela.hdr)
11000 reldata = &esdo->rela;
11007 /* Figure out the symbol index. */
11008 rel_hash_ptr = reldata->hashes + reldata->count;
11009 if (link_order->type == bfd_section_reloc_link_order)
11011 indx = link_order->u.reloc.p->u.section->target_index;
11012 BFD_ASSERT (indx != 0);
11013 *rel_hash_ptr = NULL;
11017 struct elf_link_hash_entry *h;
11019 /* Treat a reloc against a defined symbol as though it were
11020 actually against the section. */
11021 h = ((struct elf_link_hash_entry *)
11022 bfd_wrapped_link_hash_lookup (output_bfd, info,
11023 link_order->u.reloc.p->u.name,
11024 FALSE, FALSE, TRUE));
11026 && (h->root.type == bfd_link_hash_defined
11027 || h->root.type == bfd_link_hash_defweak))
11031 section = h->root.u.def.section;
11032 indx = section->output_section->target_index;
11033 *rel_hash_ptr = NULL;
11034 /* It seems that we ought to add the symbol value to the
11035 addend here, but in practice it has already been added
11036 because it was passed to constructor_callback. */
11037 addend += section->output_section->vma + section->output_offset;
11039 else if (h != NULL)
11041 /* Setting the index to -2 tells elf_link_output_extsym that
11042 this symbol is used by a reloc. */
11049 (*info->callbacks->unattached_reloc)
11050 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11055 /* If this is an inplace reloc, we must write the addend into the
11057 if (howto->partial_inplace && addend != 0)
11059 bfd_size_type size;
11060 bfd_reloc_status_type rstat;
11063 const char *sym_name;
11065 size = (bfd_size_type) bfd_get_reloc_size (howto);
11066 buf = (bfd_byte *) bfd_zmalloc (size);
11067 if (buf == NULL && size != 0)
11069 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11076 case bfd_reloc_outofrange:
11079 case bfd_reloc_overflow:
11080 if (link_order->type == bfd_section_reloc_link_order)
11081 sym_name = bfd_section_name (output_bfd,
11082 link_order->u.reloc.p->u.section);
11084 sym_name = link_order->u.reloc.p->u.name;
11085 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11086 howto->name, addend, NULL, NULL,
11091 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11093 * bfd_octets_per_byte (output_bfd),
11100 /* The address of a reloc is relative to the section in a
11101 relocatable file, and is a virtual address in an executable
11103 offset = link_order->offset;
11104 if (! bfd_link_relocatable (info))
11105 offset += output_section->vma;
11107 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11109 irel[i].r_offset = offset;
11110 irel[i].r_info = 0;
11111 irel[i].r_addend = 0;
11113 if (bed->s->arch_size == 32)
11114 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11116 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11118 rel_hdr = reldata->hdr;
11119 erel = rel_hdr->contents;
11120 if (rel_hdr->sh_type == SHT_REL)
11122 erel += reldata->count * bed->s->sizeof_rel;
11123 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11127 irel[0].r_addend = addend;
11128 erel += reldata->count * bed->s->sizeof_rela;
11129 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11138 /* Get the output vma of the section pointed to by the sh_link field. */
11141 elf_get_linked_section_vma (struct bfd_link_order *p)
11143 Elf_Internal_Shdr **elf_shdrp;
11147 s = p->u.indirect.section;
11148 elf_shdrp = elf_elfsections (s->owner);
11149 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11150 elfsec = elf_shdrp[elfsec]->sh_link;
11152 The Intel C compiler generates SHT_IA_64_UNWIND with
11153 SHF_LINK_ORDER. But it doesn't set the sh_link or
11154 sh_info fields. Hence we could get the situation
11155 where elfsec is 0. */
11158 const struct elf_backend_data *bed
11159 = get_elf_backend_data (s->owner);
11160 if (bed->link_order_error_handler)
11161 bed->link_order_error_handler
11162 /* xgettext:c-format */
11163 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11168 s = elf_shdrp[elfsec]->bfd_section;
11169 return s->output_section->vma + s->output_offset;
11174 /* Compare two sections based on the locations of the sections they are
11175 linked to. Used by elf_fixup_link_order. */
11178 compare_link_order (const void * a, const void * b)
11183 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11184 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11187 return apos > bpos;
11191 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11192 order as their linked sections. Returns false if this could not be done
11193 because an output section includes both ordered and unordered
11194 sections. Ideally we'd do this in the linker proper. */
11197 elf_fixup_link_order (bfd *abfd, asection *o)
11199 int seen_linkorder;
11202 struct bfd_link_order *p;
11204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11206 struct bfd_link_order **sections;
11207 asection *s, *other_sec, *linkorder_sec;
11211 linkorder_sec = NULL;
11213 seen_linkorder = 0;
11214 for (p = o->map_head.link_order; p != NULL; p = p->next)
11216 if (p->type == bfd_indirect_link_order)
11218 s = p->u.indirect.section;
11220 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11221 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11222 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11223 && elfsec < elf_numsections (sub)
11224 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11225 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11239 if (seen_other && seen_linkorder)
11241 if (other_sec && linkorder_sec)
11243 /* xgettext:c-format */
11244 (_("%A has both ordered [`%A' in %B] "
11245 "and unordered [`%A' in %B] sections"),
11246 o, linkorder_sec, linkorder_sec->owner,
11247 other_sec, other_sec->owner);
11250 (_("%A has both ordered and unordered sections"), o);
11251 bfd_set_error (bfd_error_bad_value);
11256 if (!seen_linkorder)
11259 sections = (struct bfd_link_order **)
11260 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11261 if (sections == NULL)
11263 seen_linkorder = 0;
11265 for (p = o->map_head.link_order; p != NULL; p = p->next)
11267 sections[seen_linkorder++] = p;
11269 /* Sort the input sections in the order of their linked section. */
11270 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11271 compare_link_order);
11273 /* Change the offsets of the sections. */
11275 for (n = 0; n < seen_linkorder; n++)
11277 s = sections[n]->u.indirect.section;
11278 offset &= ~(bfd_vma) 0 << s->alignment_power;
11279 s->output_offset = offset / bfd_octets_per_byte (abfd);
11280 sections[n]->offset = offset;
11281 offset += sections[n]->size;
11288 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11289 Returns TRUE upon success, FALSE otherwise. */
11292 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11294 bfd_boolean ret = FALSE;
11296 const struct elf_backend_data *bed;
11298 enum bfd_architecture arch;
11300 asymbol **sympp = NULL;
11304 elf_symbol_type *osymbuf;
11306 implib_bfd = info->out_implib_bfd;
11307 bed = get_elf_backend_data (abfd);
11309 if (!bfd_set_format (implib_bfd, bfd_object))
11312 /* Use flag from executable but make it a relocatable object. */
11313 flags = bfd_get_file_flags (abfd);
11314 flags &= ~HAS_RELOC;
11315 if (!bfd_set_start_address (implib_bfd, 0)
11316 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11319 /* Copy architecture of output file to import library file. */
11320 arch = bfd_get_arch (abfd);
11321 mach = bfd_get_mach (abfd);
11322 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11323 && (abfd->target_defaulted
11324 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11327 /* Get symbol table size. */
11328 symsize = bfd_get_symtab_upper_bound (abfd);
11332 /* Read in the symbol table. */
11333 sympp = (asymbol **) xmalloc (symsize);
11334 symcount = bfd_canonicalize_symtab (abfd, sympp);
11338 /* Allow the BFD backend to copy any private header data it
11339 understands from the output BFD to the import library BFD. */
11340 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11343 /* Filter symbols to appear in the import library. */
11344 if (bed->elf_backend_filter_implib_symbols)
11345 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11348 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11351 bfd_set_error (bfd_error_no_symbols);
11352 _bfd_error_handler (_("%B: no symbol found for import library"),
11358 /* Make symbols absolute. */
11359 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11360 sizeof (*osymbuf));
11361 for (src_count = 0; src_count < symcount; src_count++)
11363 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11364 sizeof (*osymbuf));
11365 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11366 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11367 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11368 osymbuf[src_count].internal_elf_sym.st_value =
11369 osymbuf[src_count].symbol.value;
11370 sympp[src_count] = &osymbuf[src_count].symbol;
11373 bfd_set_symtab (implib_bfd, sympp, symcount);
11375 /* Allow the BFD backend to copy any private data it understands
11376 from the output BFD to the import library BFD. This is done last
11377 to permit the routine to look at the filtered symbol table. */
11378 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11381 if (!bfd_close (implib_bfd))
11392 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11396 if (flinfo->symstrtab != NULL)
11397 _bfd_elf_strtab_free (flinfo->symstrtab);
11398 if (flinfo->contents != NULL)
11399 free (flinfo->contents);
11400 if (flinfo->external_relocs != NULL)
11401 free (flinfo->external_relocs);
11402 if (flinfo->internal_relocs != NULL)
11403 free (flinfo->internal_relocs);
11404 if (flinfo->external_syms != NULL)
11405 free (flinfo->external_syms);
11406 if (flinfo->locsym_shndx != NULL)
11407 free (flinfo->locsym_shndx);
11408 if (flinfo->internal_syms != NULL)
11409 free (flinfo->internal_syms);
11410 if (flinfo->indices != NULL)
11411 free (flinfo->indices);
11412 if (flinfo->sections != NULL)
11413 free (flinfo->sections);
11414 if (flinfo->symshndxbuf != NULL)
11415 free (flinfo->symshndxbuf);
11416 for (o = obfd->sections; o != NULL; o = o->next)
11418 struct bfd_elf_section_data *esdo = elf_section_data (o);
11419 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11420 free (esdo->rel.hashes);
11421 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11422 free (esdo->rela.hashes);
11426 /* Do the final step of an ELF link. */
11429 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11431 bfd_boolean dynamic;
11432 bfd_boolean emit_relocs;
11434 struct elf_final_link_info flinfo;
11436 struct bfd_link_order *p;
11438 bfd_size_type max_contents_size;
11439 bfd_size_type max_external_reloc_size;
11440 bfd_size_type max_internal_reloc_count;
11441 bfd_size_type max_sym_count;
11442 bfd_size_type max_sym_shndx_count;
11443 Elf_Internal_Sym elfsym;
11445 Elf_Internal_Shdr *symtab_hdr;
11446 Elf_Internal_Shdr *symtab_shndx_hdr;
11447 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11448 struct elf_outext_info eoinfo;
11449 bfd_boolean merged;
11450 size_t relativecount = 0;
11451 asection *reldyn = 0;
11453 asection *attr_section = NULL;
11454 bfd_vma attr_size = 0;
11455 const char *std_attrs_section;
11456 struct elf_link_hash_table *htab = elf_hash_table (info);
11458 if (!is_elf_hash_table (htab))
11461 if (bfd_link_pic (info))
11462 abfd->flags |= DYNAMIC;
11464 dynamic = htab->dynamic_sections_created;
11465 dynobj = htab->dynobj;
11467 emit_relocs = (bfd_link_relocatable (info)
11468 || info->emitrelocations);
11470 flinfo.info = info;
11471 flinfo.output_bfd = abfd;
11472 flinfo.symstrtab = _bfd_elf_strtab_init ();
11473 if (flinfo.symstrtab == NULL)
11478 flinfo.hash_sec = NULL;
11479 flinfo.symver_sec = NULL;
11483 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11484 /* Note that dynsym_sec can be NULL (on VMS). */
11485 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11486 /* Note that it is OK if symver_sec is NULL. */
11489 flinfo.contents = NULL;
11490 flinfo.external_relocs = NULL;
11491 flinfo.internal_relocs = NULL;
11492 flinfo.external_syms = NULL;
11493 flinfo.locsym_shndx = NULL;
11494 flinfo.internal_syms = NULL;
11495 flinfo.indices = NULL;
11496 flinfo.sections = NULL;
11497 flinfo.symshndxbuf = NULL;
11498 flinfo.filesym_count = 0;
11500 /* The object attributes have been merged. Remove the input
11501 sections from the link, and set the contents of the output
11503 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11504 for (o = abfd->sections; o != NULL; o = o->next)
11506 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11507 || strcmp (o->name, ".gnu.attributes") == 0)
11509 for (p = o->map_head.link_order; p != NULL; p = p->next)
11511 asection *input_section;
11513 if (p->type != bfd_indirect_link_order)
11515 input_section = p->u.indirect.section;
11516 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11517 elf_link_input_bfd ignores this section. */
11518 input_section->flags &= ~SEC_HAS_CONTENTS;
11521 attr_size = bfd_elf_obj_attr_size (abfd);
11524 bfd_set_section_size (abfd, o, attr_size);
11526 /* Skip this section later on. */
11527 o->map_head.link_order = NULL;
11530 o->flags |= SEC_EXCLUDE;
11534 /* Count up the number of relocations we will output for each output
11535 section, so that we know the sizes of the reloc sections. We
11536 also figure out some maximum sizes. */
11537 max_contents_size = 0;
11538 max_external_reloc_size = 0;
11539 max_internal_reloc_count = 0;
11541 max_sym_shndx_count = 0;
11543 for (o = abfd->sections; o != NULL; o = o->next)
11545 struct bfd_elf_section_data *esdo = elf_section_data (o);
11546 o->reloc_count = 0;
11548 for (p = o->map_head.link_order; p != NULL; p = p->next)
11550 unsigned int reloc_count = 0;
11551 unsigned int additional_reloc_count = 0;
11552 struct bfd_elf_section_data *esdi = NULL;
11554 if (p->type == bfd_section_reloc_link_order
11555 || p->type == bfd_symbol_reloc_link_order)
11557 else if (p->type == bfd_indirect_link_order)
11561 sec = p->u.indirect.section;
11563 /* Mark all sections which are to be included in the
11564 link. This will normally be every section. We need
11565 to do this so that we can identify any sections which
11566 the linker has decided to not include. */
11567 sec->linker_mark = TRUE;
11569 if (sec->flags & SEC_MERGE)
11572 if (sec->rawsize > max_contents_size)
11573 max_contents_size = sec->rawsize;
11574 if (sec->size > max_contents_size)
11575 max_contents_size = sec->size;
11577 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11578 && (sec->owner->flags & DYNAMIC) == 0)
11582 /* We are interested in just local symbols, not all
11584 if (elf_bad_symtab (sec->owner))
11585 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11586 / bed->s->sizeof_sym);
11588 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11590 if (sym_count > max_sym_count)
11591 max_sym_count = sym_count;
11593 if (sym_count > max_sym_shndx_count
11594 && elf_symtab_shndx_list (sec->owner) != NULL)
11595 max_sym_shndx_count = sym_count;
11597 if (esdo->this_hdr.sh_type == SHT_REL
11598 || esdo->this_hdr.sh_type == SHT_RELA)
11599 /* Some backends use reloc_count in relocation sections
11600 to count particular types of relocs. Of course,
11601 reloc sections themselves can't have relocations. */
11603 else if (emit_relocs)
11605 reloc_count = sec->reloc_count;
11606 if (bed->elf_backend_count_additional_relocs)
11609 c = (*bed->elf_backend_count_additional_relocs) (sec);
11610 additional_reloc_count += c;
11613 else if (bed->elf_backend_count_relocs)
11614 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11616 esdi = elf_section_data (sec);
11618 if ((sec->flags & SEC_RELOC) != 0)
11620 size_t ext_size = 0;
11622 if (esdi->rel.hdr != NULL)
11623 ext_size = esdi->rel.hdr->sh_size;
11624 if (esdi->rela.hdr != NULL)
11625 ext_size += esdi->rela.hdr->sh_size;
11627 if (ext_size > max_external_reloc_size)
11628 max_external_reloc_size = ext_size;
11629 if (sec->reloc_count > max_internal_reloc_count)
11630 max_internal_reloc_count = sec->reloc_count;
11635 if (reloc_count == 0)
11638 reloc_count += additional_reloc_count;
11639 o->reloc_count += reloc_count;
11641 if (p->type == bfd_indirect_link_order && emit_relocs)
11645 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11646 esdo->rel.count += additional_reloc_count;
11648 if (esdi->rela.hdr)
11650 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11651 esdo->rela.count += additional_reloc_count;
11657 esdo->rela.count += reloc_count;
11659 esdo->rel.count += reloc_count;
11663 if (o->reloc_count > 0)
11664 o->flags |= SEC_RELOC;
11667 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11668 set it (this is probably a bug) and if it is set
11669 assign_section_numbers will create a reloc section. */
11670 o->flags &=~ SEC_RELOC;
11673 /* If the SEC_ALLOC flag is not set, force the section VMA to
11674 zero. This is done in elf_fake_sections as well, but forcing
11675 the VMA to 0 here will ensure that relocs against these
11676 sections are handled correctly. */
11677 if ((o->flags & SEC_ALLOC) == 0
11678 && ! o->user_set_vma)
11682 if (! bfd_link_relocatable (info) && merged)
11683 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11685 /* Figure out the file positions for everything but the symbol table
11686 and the relocs. We set symcount to force assign_section_numbers
11687 to create a symbol table. */
11688 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11689 BFD_ASSERT (! abfd->output_has_begun);
11690 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11693 /* Set sizes, and assign file positions for reloc sections. */
11694 for (o = abfd->sections; o != NULL; o = o->next)
11696 struct bfd_elf_section_data *esdo = elf_section_data (o);
11697 if ((o->flags & SEC_RELOC) != 0)
11700 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11704 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11708 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11709 to count upwards while actually outputting the relocations. */
11710 esdo->rel.count = 0;
11711 esdo->rela.count = 0;
11713 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11715 /* Cache the section contents so that they can be compressed
11716 later. Use bfd_malloc since it will be freed by
11717 bfd_compress_section_contents. */
11718 unsigned char *contents = esdo->this_hdr.contents;
11719 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11722 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11723 if (contents == NULL)
11725 esdo->this_hdr.contents = contents;
11729 /* We have now assigned file positions for all the sections except
11730 .symtab, .strtab, and non-loaded reloc sections. We start the
11731 .symtab section at the current file position, and write directly
11732 to it. We build the .strtab section in memory. */
11733 bfd_get_symcount (abfd) = 0;
11734 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11735 /* sh_name is set in prep_headers. */
11736 symtab_hdr->sh_type = SHT_SYMTAB;
11737 /* sh_flags, sh_addr and sh_size all start off zero. */
11738 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11739 /* sh_link is set in assign_section_numbers. */
11740 /* sh_info is set below. */
11741 /* sh_offset is set just below. */
11742 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11744 if (max_sym_count < 20)
11745 max_sym_count = 20;
11746 htab->strtabsize = max_sym_count;
11747 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11748 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11749 if (htab->strtab == NULL)
11751 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11753 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11754 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11756 if (info->strip != strip_all || emit_relocs)
11758 file_ptr off = elf_next_file_pos (abfd);
11760 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11762 /* Note that at this point elf_next_file_pos (abfd) is
11763 incorrect. We do not yet know the size of the .symtab section.
11764 We correct next_file_pos below, after we do know the size. */
11766 /* Start writing out the symbol table. The first symbol is always a
11768 elfsym.st_value = 0;
11769 elfsym.st_size = 0;
11770 elfsym.st_info = 0;
11771 elfsym.st_other = 0;
11772 elfsym.st_shndx = SHN_UNDEF;
11773 elfsym.st_target_internal = 0;
11774 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11775 bfd_und_section_ptr, NULL) != 1)
11778 /* Output a symbol for each section. We output these even if we are
11779 discarding local symbols, since they are used for relocs. These
11780 symbols have no names. We store the index of each one in the
11781 index field of the section, so that we can find it again when
11782 outputting relocs. */
11784 elfsym.st_size = 0;
11785 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11786 elfsym.st_other = 0;
11787 elfsym.st_value = 0;
11788 elfsym.st_target_internal = 0;
11789 for (i = 1; i < elf_numsections (abfd); i++)
11791 o = bfd_section_from_elf_index (abfd, i);
11794 o->target_index = bfd_get_symcount (abfd);
11795 elfsym.st_shndx = i;
11796 if (!bfd_link_relocatable (info))
11797 elfsym.st_value = o->vma;
11798 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11805 /* Allocate some memory to hold information read in from the input
11807 if (max_contents_size != 0)
11809 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11810 if (flinfo.contents == NULL)
11814 if (max_external_reloc_size != 0)
11816 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11817 if (flinfo.external_relocs == NULL)
11821 if (max_internal_reloc_count != 0)
11823 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
11824 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11825 if (flinfo.internal_relocs == NULL)
11829 if (max_sym_count != 0)
11831 amt = max_sym_count * bed->s->sizeof_sym;
11832 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11833 if (flinfo.external_syms == NULL)
11836 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11837 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11838 if (flinfo.internal_syms == NULL)
11841 amt = max_sym_count * sizeof (long);
11842 flinfo.indices = (long int *) bfd_malloc (amt);
11843 if (flinfo.indices == NULL)
11846 amt = max_sym_count * sizeof (asection *);
11847 flinfo.sections = (asection **) bfd_malloc (amt);
11848 if (flinfo.sections == NULL)
11852 if (max_sym_shndx_count != 0)
11854 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11855 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11856 if (flinfo.locsym_shndx == NULL)
11862 bfd_vma base, end = 0;
11865 for (sec = htab->tls_sec;
11866 sec && (sec->flags & SEC_THREAD_LOCAL);
11869 bfd_size_type size = sec->size;
11872 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11874 struct bfd_link_order *ord = sec->map_tail.link_order;
11877 size = ord->offset + ord->size;
11879 end = sec->vma + size;
11881 base = htab->tls_sec->vma;
11882 /* Only align end of TLS section if static TLS doesn't have special
11883 alignment requirements. */
11884 if (bed->static_tls_alignment == 1)
11885 end = align_power (end, htab->tls_sec->alignment_power);
11886 htab->tls_size = end - base;
11889 /* Reorder SHF_LINK_ORDER sections. */
11890 for (o = abfd->sections; o != NULL; o = o->next)
11892 if (!elf_fixup_link_order (abfd, o))
11896 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11899 /* Since ELF permits relocations to be against local symbols, we
11900 must have the local symbols available when we do the relocations.
11901 Since we would rather only read the local symbols once, and we
11902 would rather not keep them in memory, we handle all the
11903 relocations for a single input file at the same time.
11905 Unfortunately, there is no way to know the total number of local
11906 symbols until we have seen all of them, and the local symbol
11907 indices precede the global symbol indices. This means that when
11908 we are generating relocatable output, and we see a reloc against
11909 a global symbol, we can not know the symbol index until we have
11910 finished examining all the local symbols to see which ones we are
11911 going to output. To deal with this, we keep the relocations in
11912 memory, and don't output them until the end of the link. This is
11913 an unfortunate waste of memory, but I don't see a good way around
11914 it. Fortunately, it only happens when performing a relocatable
11915 link, which is not the common case. FIXME: If keep_memory is set
11916 we could write the relocs out and then read them again; I don't
11917 know how bad the memory loss will be. */
11919 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11920 sub->output_has_begun = FALSE;
11921 for (o = abfd->sections; o != NULL; o = o->next)
11923 for (p = o->map_head.link_order; p != NULL; p = p->next)
11925 if (p->type == bfd_indirect_link_order
11926 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11927 == bfd_target_elf_flavour)
11928 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11930 if (! sub->output_has_begun)
11932 if (! elf_link_input_bfd (&flinfo, sub))
11934 sub->output_has_begun = TRUE;
11937 else if (p->type == bfd_section_reloc_link_order
11938 || p->type == bfd_symbol_reloc_link_order)
11940 if (! elf_reloc_link_order (abfd, info, o, p))
11945 if (! _bfd_default_link_order (abfd, info, o, p))
11947 if (p->type == bfd_indirect_link_order
11948 && (bfd_get_flavour (sub)
11949 == bfd_target_elf_flavour)
11950 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11951 != bed->s->elfclass))
11953 const char *iclass, *oclass;
11955 switch (bed->s->elfclass)
11957 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11958 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11959 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11963 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11965 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11966 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11967 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11971 bfd_set_error (bfd_error_wrong_format);
11973 /* xgettext:c-format */
11974 (_("%B: file class %s incompatible with %s"),
11975 sub, iclass, oclass);
11984 /* Free symbol buffer if needed. */
11985 if (!info->reduce_memory_overheads)
11987 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11988 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11989 && elf_tdata (sub)->symbuf)
11991 free (elf_tdata (sub)->symbuf);
11992 elf_tdata (sub)->symbuf = NULL;
11996 /* Output any global symbols that got converted to local in a
11997 version script or due to symbol visibility. We do this in a
11998 separate step since ELF requires all local symbols to appear
11999 prior to any global symbols. FIXME: We should only do this if
12000 some global symbols were, in fact, converted to become local.
12001 FIXME: Will this work correctly with the Irix 5 linker? */
12002 eoinfo.failed = FALSE;
12003 eoinfo.flinfo = &flinfo;
12004 eoinfo.localsyms = TRUE;
12005 eoinfo.file_sym_done = FALSE;
12006 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12010 /* If backend needs to output some local symbols not present in the hash
12011 table, do it now. */
12012 if (bed->elf_backend_output_arch_local_syms
12013 && (info->strip != strip_all || emit_relocs))
12015 typedef int (*out_sym_func)
12016 (void *, const char *, Elf_Internal_Sym *, asection *,
12017 struct elf_link_hash_entry *);
12019 if (! ((*bed->elf_backend_output_arch_local_syms)
12020 (abfd, info, &flinfo,
12021 (out_sym_func) elf_link_output_symstrtab)))
12025 /* That wrote out all the local symbols. Finish up the symbol table
12026 with the global symbols. Even if we want to strip everything we
12027 can, we still need to deal with those global symbols that got
12028 converted to local in a version script. */
12030 /* The sh_info field records the index of the first non local symbol. */
12031 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12034 && htab->dynsym != NULL
12035 && htab->dynsym->output_section != bfd_abs_section_ptr)
12037 Elf_Internal_Sym sym;
12038 bfd_byte *dynsym = htab->dynsym->contents;
12040 o = htab->dynsym->output_section;
12041 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12043 /* Write out the section symbols for the output sections. */
12044 if (bfd_link_pic (info)
12045 || htab->is_relocatable_executable)
12051 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12053 sym.st_target_internal = 0;
12055 for (s = abfd->sections; s != NULL; s = s->next)
12061 dynindx = elf_section_data (s)->dynindx;
12064 indx = elf_section_data (s)->this_idx;
12065 BFD_ASSERT (indx > 0);
12066 sym.st_shndx = indx;
12067 if (! check_dynsym (abfd, &sym))
12069 sym.st_value = s->vma;
12070 dest = dynsym + dynindx * bed->s->sizeof_sym;
12071 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12075 /* Write out the local dynsyms. */
12076 if (htab->dynlocal)
12078 struct elf_link_local_dynamic_entry *e;
12079 for (e = htab->dynlocal; e ; e = e->next)
12084 /* Copy the internal symbol and turn off visibility.
12085 Note that we saved a word of storage and overwrote
12086 the original st_name with the dynstr_index. */
12088 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12090 s = bfd_section_from_elf_index (e->input_bfd,
12095 elf_section_data (s->output_section)->this_idx;
12096 if (! check_dynsym (abfd, &sym))
12098 sym.st_value = (s->output_section->vma
12100 + e->isym.st_value);
12103 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12104 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12109 /* We get the global symbols from the hash table. */
12110 eoinfo.failed = FALSE;
12111 eoinfo.localsyms = FALSE;
12112 eoinfo.flinfo = &flinfo;
12113 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12117 /* If backend needs to output some symbols not present in the hash
12118 table, do it now. */
12119 if (bed->elf_backend_output_arch_syms
12120 && (info->strip != strip_all || emit_relocs))
12122 typedef int (*out_sym_func)
12123 (void *, const char *, Elf_Internal_Sym *, asection *,
12124 struct elf_link_hash_entry *);
12126 if (! ((*bed->elf_backend_output_arch_syms)
12127 (abfd, info, &flinfo,
12128 (out_sym_func) elf_link_output_symstrtab)))
12132 /* Finalize the .strtab section. */
12133 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12135 /* Swap out the .strtab section. */
12136 if (!elf_link_swap_symbols_out (&flinfo))
12139 /* Now we know the size of the symtab section. */
12140 if (bfd_get_symcount (abfd) > 0)
12142 /* Finish up and write out the symbol string table (.strtab)
12144 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12145 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12147 if (elf_symtab_shndx_list (abfd))
12149 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12151 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12153 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12154 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12155 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12156 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12157 symtab_shndx_hdr->sh_size = amt;
12159 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12162 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12163 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12168 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12169 /* sh_name was set in prep_headers. */
12170 symstrtab_hdr->sh_type = SHT_STRTAB;
12171 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12172 symstrtab_hdr->sh_addr = 0;
12173 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12174 symstrtab_hdr->sh_entsize = 0;
12175 symstrtab_hdr->sh_link = 0;
12176 symstrtab_hdr->sh_info = 0;
12177 /* sh_offset is set just below. */
12178 symstrtab_hdr->sh_addralign = 1;
12180 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12182 elf_next_file_pos (abfd) = off;
12184 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12185 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12189 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12191 _bfd_error_handler (_("%B: failed to generate import library"),
12192 info->out_implib_bfd);
12196 /* Adjust the relocs to have the correct symbol indices. */
12197 for (o = abfd->sections; o != NULL; o = o->next)
12199 struct bfd_elf_section_data *esdo = elf_section_data (o);
12202 if ((o->flags & SEC_RELOC) == 0)
12205 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12206 if (esdo->rel.hdr != NULL
12207 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12209 if (esdo->rela.hdr != NULL
12210 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12213 /* Set the reloc_count field to 0 to prevent write_relocs from
12214 trying to swap the relocs out itself. */
12215 o->reloc_count = 0;
12218 if (dynamic && info->combreloc && dynobj != NULL)
12219 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12221 /* If we are linking against a dynamic object, or generating a
12222 shared library, finish up the dynamic linking information. */
12225 bfd_byte *dyncon, *dynconend;
12227 /* Fix up .dynamic entries. */
12228 o = bfd_get_linker_section (dynobj, ".dynamic");
12229 BFD_ASSERT (o != NULL);
12231 dyncon = o->contents;
12232 dynconend = o->contents + o->size;
12233 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12235 Elf_Internal_Dyn dyn;
12238 bfd_size_type sh_size;
12241 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12248 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12250 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12252 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12253 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12256 dyn.d_un.d_val = relativecount;
12263 name = info->init_function;
12266 name = info->fini_function;
12269 struct elf_link_hash_entry *h;
12271 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12273 && (h->root.type == bfd_link_hash_defined
12274 || h->root.type == bfd_link_hash_defweak))
12276 dyn.d_un.d_ptr = h->root.u.def.value;
12277 o = h->root.u.def.section;
12278 if (o->output_section != NULL)
12279 dyn.d_un.d_ptr += (o->output_section->vma
12280 + o->output_offset);
12283 /* The symbol is imported from another shared
12284 library and does not apply to this one. */
12285 dyn.d_un.d_ptr = 0;
12292 case DT_PREINIT_ARRAYSZ:
12293 name = ".preinit_array";
12295 case DT_INIT_ARRAYSZ:
12296 name = ".init_array";
12298 case DT_FINI_ARRAYSZ:
12299 name = ".fini_array";
12301 o = bfd_get_section_by_name (abfd, name);
12305 (_("could not find section %s"), name);
12310 (_("warning: %s section has zero size"), name);
12311 dyn.d_un.d_val = o->size;
12314 case DT_PREINIT_ARRAY:
12315 name = ".preinit_array";
12317 case DT_INIT_ARRAY:
12318 name = ".init_array";
12320 case DT_FINI_ARRAY:
12321 name = ".fini_array";
12323 o = bfd_get_section_by_name (abfd, name);
12330 name = ".gnu.hash";
12339 name = ".gnu.version_d";
12342 name = ".gnu.version_r";
12345 name = ".gnu.version";
12347 o = bfd_get_linker_section (dynobj, name);
12349 if (o == NULL || bfd_is_abs_section (o->output_section))
12352 (_("could not find section %s"), name);
12355 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12358 (_("warning: section '%s' is being made into a note"), name);
12359 bfd_set_error (bfd_error_nonrepresentable_section);
12362 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12369 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12375 for (i = 1; i < elf_numsections (abfd); i++)
12377 Elf_Internal_Shdr *hdr;
12379 hdr = elf_elfsections (abfd)[i];
12380 if (hdr->sh_type == type
12381 && (hdr->sh_flags & SHF_ALLOC) != 0)
12383 sh_size += hdr->sh_size;
12385 || sh_addr > hdr->sh_addr)
12386 sh_addr = hdr->sh_addr;
12390 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12392 /* Don't count procedure linkage table relocs in the
12393 overall reloc count. */
12394 sh_size -= htab->srelplt->size;
12396 /* If the size is zero, make the address zero too.
12397 This is to avoid a glibc bug. If the backend
12398 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12399 zero, then we'll put DT_RELA at the end of
12400 DT_JMPREL. glibc will interpret the end of
12401 DT_RELA matching the end of DT_JMPREL as the
12402 case where DT_RELA includes DT_JMPREL, and for
12403 LD_BIND_NOW will decide that processing DT_RELA
12404 will process the PLT relocs too. Net result:
12405 No PLT relocs applied. */
12408 /* If .rela.plt is the first .rela section, exclude
12409 it from DT_RELA. */
12410 else if (sh_addr == (htab->srelplt->output_section->vma
12411 + htab->srelplt->output_offset))
12412 sh_addr += htab->srelplt->size;
12415 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12416 dyn.d_un.d_val = sh_size;
12418 dyn.d_un.d_ptr = sh_addr;
12421 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12425 /* If we have created any dynamic sections, then output them. */
12426 if (dynobj != NULL)
12428 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12431 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12432 if (((info->warn_shared_textrel && bfd_link_pic (info))
12433 || info->error_textrel)
12434 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12436 bfd_byte *dyncon, *dynconend;
12438 dyncon = o->contents;
12439 dynconend = o->contents + o->size;
12440 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12442 Elf_Internal_Dyn dyn;
12444 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12446 if (dyn.d_tag == DT_TEXTREL)
12448 if (info->error_textrel)
12449 info->callbacks->einfo
12450 (_("%P%X: read-only segment has dynamic relocations.\n"));
12452 info->callbacks->einfo
12453 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12459 for (o = dynobj->sections; o != NULL; o = o->next)
12461 if ((o->flags & SEC_HAS_CONTENTS) == 0
12463 || o->output_section == bfd_abs_section_ptr)
12465 if ((o->flags & SEC_LINKER_CREATED) == 0)
12467 /* At this point, we are only interested in sections
12468 created by _bfd_elf_link_create_dynamic_sections. */
12471 if (htab->stab_info.stabstr == o)
12473 if (htab->eh_info.hdr_sec == o)
12475 if (strcmp (o->name, ".dynstr") != 0)
12477 if (! bfd_set_section_contents (abfd, o->output_section,
12479 (file_ptr) o->output_offset
12480 * bfd_octets_per_byte (abfd),
12486 /* The contents of the .dynstr section are actually in a
12490 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12491 if (bfd_seek (abfd, off, SEEK_SET) != 0
12492 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12498 if (!info->resolve_section_groups)
12500 bfd_boolean failed = FALSE;
12502 BFD_ASSERT (bfd_link_relocatable (info));
12503 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12508 /* If we have optimized stabs strings, output them. */
12509 if (htab->stab_info.stabstr != NULL)
12511 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12515 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12518 elf_final_link_free (abfd, &flinfo);
12520 elf_linker (abfd) = TRUE;
12524 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12525 if (contents == NULL)
12526 return FALSE; /* Bail out and fail. */
12527 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12528 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12535 elf_final_link_free (abfd, &flinfo);
12539 /* Initialize COOKIE for input bfd ABFD. */
12542 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12543 struct bfd_link_info *info, bfd *abfd)
12545 Elf_Internal_Shdr *symtab_hdr;
12546 const struct elf_backend_data *bed;
12548 bed = get_elf_backend_data (abfd);
12549 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12551 cookie->abfd = abfd;
12552 cookie->sym_hashes = elf_sym_hashes (abfd);
12553 cookie->bad_symtab = elf_bad_symtab (abfd);
12554 if (cookie->bad_symtab)
12556 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12557 cookie->extsymoff = 0;
12561 cookie->locsymcount = symtab_hdr->sh_info;
12562 cookie->extsymoff = symtab_hdr->sh_info;
12565 if (bed->s->arch_size == 32)
12566 cookie->r_sym_shift = 8;
12568 cookie->r_sym_shift = 32;
12570 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12571 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12573 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12574 cookie->locsymcount, 0,
12576 if (cookie->locsyms == NULL)
12578 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12581 if (info->keep_memory)
12582 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12587 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12590 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12592 Elf_Internal_Shdr *symtab_hdr;
12594 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12595 if (cookie->locsyms != NULL
12596 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12597 free (cookie->locsyms);
12600 /* Initialize the relocation information in COOKIE for input section SEC
12601 of input bfd ABFD. */
12604 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12605 struct bfd_link_info *info, bfd *abfd,
12608 if (sec->reloc_count == 0)
12610 cookie->rels = NULL;
12611 cookie->relend = NULL;
12615 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12616 info->keep_memory);
12617 if (cookie->rels == NULL)
12619 cookie->rel = cookie->rels;
12620 cookie->relend = cookie->rels + sec->reloc_count;
12622 cookie->rel = cookie->rels;
12626 /* Free the memory allocated by init_reloc_cookie_rels,
12630 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12633 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12634 free (cookie->rels);
12637 /* Initialize the whole of COOKIE for input section SEC. */
12640 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12641 struct bfd_link_info *info,
12644 if (!init_reloc_cookie (cookie, info, sec->owner))
12646 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12651 fini_reloc_cookie (cookie, sec->owner);
12656 /* Free the memory allocated by init_reloc_cookie_for_section,
12660 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12663 fini_reloc_cookie_rels (cookie, sec);
12664 fini_reloc_cookie (cookie, sec->owner);
12667 /* Garbage collect unused sections. */
12669 /* Default gc_mark_hook. */
12672 _bfd_elf_gc_mark_hook (asection *sec,
12673 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12674 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12675 struct elf_link_hash_entry *h,
12676 Elf_Internal_Sym *sym)
12680 switch (h->root.type)
12682 case bfd_link_hash_defined:
12683 case bfd_link_hash_defweak:
12684 return h->root.u.def.section;
12686 case bfd_link_hash_common:
12687 return h->root.u.c.p->section;
12694 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12699 /* Return the global debug definition section. */
12702 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
12703 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12704 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12705 struct elf_link_hash_entry *h,
12706 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
12709 && (h->root.type == bfd_link_hash_defined
12710 || h->root.type == bfd_link_hash_defweak)
12711 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
12712 return h->root.u.def.section;
12717 /* COOKIE->rel describes a relocation against section SEC, which is
12718 a section we've decided to keep. Return the section that contains
12719 the relocation symbol, or NULL if no section contains it. */
12722 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12723 elf_gc_mark_hook_fn gc_mark_hook,
12724 struct elf_reloc_cookie *cookie,
12725 bfd_boolean *start_stop)
12727 unsigned long r_symndx;
12728 struct elf_link_hash_entry *h;
12730 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12731 if (r_symndx == STN_UNDEF)
12734 if (r_symndx >= cookie->locsymcount
12735 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12737 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12740 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12744 while (h->root.type == bfd_link_hash_indirect
12745 || h->root.type == bfd_link_hash_warning)
12746 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12748 /* If this symbol is weak and there is a non-weak definition, we
12749 keep the non-weak definition because many backends put
12750 dynamic reloc info on the non-weak definition for code
12751 handling copy relocs. */
12752 if (h->u.weakdef != NULL)
12753 h->u.weakdef->mark = 1;
12755 if (start_stop != NULL)
12757 /* To work around a glibc bug, mark XXX input sections
12758 when there is a reference to __start_XXX or __stop_XXX
12762 asection *s = h->u2.start_stop_section;
12763 *start_stop = !s->gc_mark;
12768 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12771 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12772 &cookie->locsyms[r_symndx]);
12775 /* COOKIE->rel describes a relocation against section SEC, which is
12776 a section we've decided to keep. Mark the section that contains
12777 the relocation symbol. */
12780 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12782 elf_gc_mark_hook_fn gc_mark_hook,
12783 struct elf_reloc_cookie *cookie)
12786 bfd_boolean start_stop = FALSE;
12788 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12789 while (rsec != NULL)
12791 if (!rsec->gc_mark)
12793 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12794 || (rsec->owner->flags & DYNAMIC) != 0)
12796 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12801 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12806 /* The mark phase of garbage collection. For a given section, mark
12807 it and any sections in this section's group, and all the sections
12808 which define symbols to which it refers. */
12811 _bfd_elf_gc_mark (struct bfd_link_info *info,
12813 elf_gc_mark_hook_fn gc_mark_hook)
12816 asection *group_sec, *eh_frame;
12820 /* Mark all the sections in the group. */
12821 group_sec = elf_section_data (sec)->next_in_group;
12822 if (group_sec && !group_sec->gc_mark)
12823 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12826 /* Look through the section relocs. */
12828 eh_frame = elf_eh_frame_section (sec->owner);
12829 if ((sec->flags & SEC_RELOC) != 0
12830 && sec->reloc_count > 0
12831 && sec != eh_frame)
12833 struct elf_reloc_cookie cookie;
12835 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12839 for (; cookie.rel < cookie.relend; cookie.rel++)
12840 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12845 fini_reloc_cookie_for_section (&cookie, sec);
12849 if (ret && eh_frame && elf_fde_list (sec))
12851 struct elf_reloc_cookie cookie;
12853 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12857 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12858 gc_mark_hook, &cookie))
12860 fini_reloc_cookie_for_section (&cookie, eh_frame);
12864 eh_frame = elf_section_eh_frame_entry (sec);
12865 if (ret && eh_frame && !eh_frame->gc_mark)
12866 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12872 /* Scan and mark sections in a special or debug section group. */
12875 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12877 /* Point to first section of section group. */
12879 /* Used to iterate the section group. */
12882 bfd_boolean is_special_grp = TRUE;
12883 bfd_boolean is_debug_grp = TRUE;
12885 /* First scan to see if group contains any section other than debug
12886 and special section. */
12887 ssec = msec = elf_next_in_group (grp);
12890 if ((msec->flags & SEC_DEBUGGING) == 0)
12891 is_debug_grp = FALSE;
12893 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12894 is_special_grp = FALSE;
12896 msec = elf_next_in_group (msec);
12898 while (msec != ssec);
12900 /* If this is a pure debug section group or pure special section group,
12901 keep all sections in this group. */
12902 if (is_debug_grp || is_special_grp)
12907 msec = elf_next_in_group (msec);
12909 while (msec != ssec);
12913 /* Keep debug and special sections. */
12916 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12917 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12921 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12924 bfd_boolean some_kept;
12925 bfd_boolean debug_frag_seen;
12926 bfd_boolean has_kept_debug_info;
12928 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12930 isec = ibfd->sections;
12931 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
12934 /* Ensure all linker created sections are kept,
12935 see if any other section is already marked,
12936 and note if we have any fragmented debug sections. */
12937 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
12938 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12940 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12942 else if (isec->gc_mark
12943 && (isec->flags & SEC_ALLOC) != 0
12944 && elf_section_type (isec) != SHT_NOTE)
12947 if (!debug_frag_seen
12948 && (isec->flags & SEC_DEBUGGING)
12949 && CONST_STRNEQ (isec->name, ".debug_line."))
12950 debug_frag_seen = TRUE;
12953 /* If no non-note alloc section in this file will be kept, then
12954 we can toss out the debug and special sections. */
12958 /* Keep debug and special sections like .comment when they are
12959 not part of a group. Also keep section groups that contain
12960 just debug sections or special sections. */
12961 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12963 if ((isec->flags & SEC_GROUP) != 0)
12964 _bfd_elf_gc_mark_debug_special_section_group (isec);
12965 else if (((isec->flags & SEC_DEBUGGING) != 0
12966 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12967 && elf_next_in_group (isec) == NULL)
12969 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
12970 has_kept_debug_info = TRUE;
12973 /* Look for CODE sections which are going to be discarded,
12974 and find and discard any fragmented debug sections which
12975 are associated with that code section. */
12976 if (debug_frag_seen)
12977 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12978 if ((isec->flags & SEC_CODE) != 0
12979 && isec->gc_mark == 0)
12984 ilen = strlen (isec->name);
12986 /* Association is determined by the name of the debug
12987 section containing the name of the code section as
12988 a suffix. For example .debug_line.text.foo is a
12989 debug section associated with .text.foo. */
12990 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12994 if (dsec->gc_mark == 0
12995 || (dsec->flags & SEC_DEBUGGING) == 0)
12998 dlen = strlen (dsec->name);
13001 && strncmp (dsec->name + (dlen - ilen),
13002 isec->name, ilen) == 0)
13007 /* Mark debug sections referenced by kept debug sections. */
13008 if (has_kept_debug_info)
13009 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13011 && (isec->flags & SEC_DEBUGGING) != 0)
13012 if (!_bfd_elf_gc_mark (info, isec,
13013 elf_gc_mark_debug_section))
13019 /* The sweep phase of garbage collection. Remove all garbage sections. */
13021 typedef bfd_boolean (*gc_sweep_hook_fn)
13022 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
13025 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13028 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13029 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
13031 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13035 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13036 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13039 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13042 for (o = sub->sections; o != NULL; o = o->next)
13044 /* When any section in a section group is kept, we keep all
13045 sections in the section group. If the first member of
13046 the section group is excluded, we will also exclude the
13048 if (o->flags & SEC_GROUP)
13050 asection *first = elf_next_in_group (o);
13051 o->gc_mark = first->gc_mark;
13057 /* Skip sweeping sections already excluded. */
13058 if (o->flags & SEC_EXCLUDE)
13061 /* Since this is early in the link process, it is simple
13062 to remove a section from the output. */
13063 o->flags |= SEC_EXCLUDE;
13065 if (info->print_gc_sections && o->size != 0)
13066 /* xgettext:c-format */
13067 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"),
13070 /* But we also have to update some of the relocation
13071 info we collected before. */
13073 && (o->flags & SEC_RELOC) != 0
13074 && o->reloc_count != 0
13075 && !((info->strip == strip_all || info->strip == strip_debugger)
13076 && (o->flags & SEC_DEBUGGING) != 0)
13077 && !bfd_is_abs_section (o->output_section))
13079 Elf_Internal_Rela *internal_relocs;
13083 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
13084 info->keep_memory);
13085 if (internal_relocs == NULL)
13088 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13090 if (elf_section_data (o)->relocs != internal_relocs)
13091 free (internal_relocs);
13102 /* Propagate collected vtable information. This is called through
13103 elf_link_hash_traverse. */
13106 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13108 /* Those that are not vtables. */
13110 || h->u2.vtable == NULL
13111 || h->u2.vtable->parent == NULL)
13114 /* Those vtables that do not have parents, we cannot merge. */
13115 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13118 /* If we've already been done, exit. */
13119 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13122 /* Make sure the parent's table is up to date. */
13123 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13125 if (h->u2.vtable->used == NULL)
13127 /* None of this table's entries were referenced. Re-use the
13129 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13130 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13135 bfd_boolean *cu, *pu;
13137 /* Or the parent's entries into ours. */
13138 cu = h->u2.vtable->used;
13140 pu = h->u2.vtable->parent->u2.vtable->used;
13143 const struct elf_backend_data *bed;
13144 unsigned int log_file_align;
13146 bed = get_elf_backend_data (h->root.u.def.section->owner);
13147 log_file_align = bed->s->log_file_align;
13148 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13163 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13166 bfd_vma hstart, hend;
13167 Elf_Internal_Rela *relstart, *relend, *rel;
13168 const struct elf_backend_data *bed;
13169 unsigned int log_file_align;
13171 /* Take care of both those symbols that do not describe vtables as
13172 well as those that are not loaded. */
13174 || h->u2.vtable == NULL
13175 || h->u2.vtable->parent == NULL)
13178 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13179 || h->root.type == bfd_link_hash_defweak);
13181 sec = h->root.u.def.section;
13182 hstart = h->root.u.def.value;
13183 hend = hstart + h->size;
13185 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13187 return *(bfd_boolean *) okp = FALSE;
13188 bed = get_elf_backend_data (sec->owner);
13189 log_file_align = bed->s->log_file_align;
13191 relend = relstart + sec->reloc_count;
13193 for (rel = relstart; rel < relend; ++rel)
13194 if (rel->r_offset >= hstart && rel->r_offset < hend)
13196 /* If the entry is in use, do nothing. */
13197 if (h->u2.vtable->used
13198 && (rel->r_offset - hstart) < h->u2.vtable->size)
13200 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13201 if (h->u2.vtable->used[entry])
13204 /* Otherwise, kill it. */
13205 rel->r_offset = rel->r_info = rel->r_addend = 0;
13211 /* Mark sections containing dynamically referenced symbols. When
13212 building shared libraries, we must assume that any visible symbol is
13216 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13218 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13219 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13221 if ((h->root.type == bfd_link_hash_defined
13222 || h->root.type == bfd_link_hash_defweak)
13224 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13225 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13226 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13227 && (!bfd_link_executable (info)
13228 || info->gc_keep_exported
13229 || info->export_dynamic
13232 && (*d->match) (&d->head, NULL, h->root.root.string)))
13233 && (h->versioned >= versioned
13234 || !bfd_hide_sym_by_version (info->version_info,
13235 h->root.root.string)))))
13236 h->root.u.def.section->flags |= SEC_KEEP;
13241 /* Keep all sections containing symbols undefined on the command-line,
13242 and the section containing the entry symbol. */
13245 _bfd_elf_gc_keep (struct bfd_link_info *info)
13247 struct bfd_sym_chain *sym;
13249 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13251 struct elf_link_hash_entry *h;
13253 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13254 FALSE, FALSE, FALSE);
13257 && (h->root.type == bfd_link_hash_defined
13258 || h->root.type == bfd_link_hash_defweak)
13259 && !bfd_is_abs_section (h->root.u.def.section)
13260 && !bfd_is_und_section (h->root.u.def.section))
13261 h->root.u.def.section->flags |= SEC_KEEP;
13266 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13267 struct bfd_link_info *info)
13269 bfd *ibfd = info->input_bfds;
13271 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13274 struct elf_reloc_cookie cookie;
13276 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13278 sec = ibfd->sections;
13279 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13282 if (!init_reloc_cookie (&cookie, info, ibfd))
13285 for (sec = ibfd->sections; sec; sec = sec->next)
13287 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13288 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13290 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13291 fini_reloc_cookie_rels (&cookie, sec);
13298 /* Do mark and sweep of unused sections. */
13301 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13303 bfd_boolean ok = TRUE;
13305 elf_gc_mark_hook_fn gc_mark_hook;
13306 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13307 struct elf_link_hash_table *htab;
13309 if (!bed->can_gc_sections
13310 || !is_elf_hash_table (info->hash))
13312 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13316 bed->gc_keep (info);
13317 htab = elf_hash_table (info);
13319 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13320 at the .eh_frame section if we can mark the FDEs individually. */
13321 for (sub = info->input_bfds;
13322 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13323 sub = sub->link.next)
13326 struct elf_reloc_cookie cookie;
13328 sec = sub->sections;
13329 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13331 sec = bfd_get_section_by_name (sub, ".eh_frame");
13332 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13334 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13335 if (elf_section_data (sec)->sec_info
13336 && (sec->flags & SEC_LINKER_CREATED) == 0)
13337 elf_eh_frame_section (sub) = sec;
13338 fini_reloc_cookie_for_section (&cookie, sec);
13339 sec = bfd_get_next_section_by_name (NULL, sec);
13343 /* Apply transitive closure to the vtable entry usage info. */
13344 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13348 /* Kill the vtable relocations that were not used. */
13349 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13353 /* Mark dynamically referenced symbols. */
13354 if (htab->dynamic_sections_created || info->gc_keep_exported)
13355 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13357 /* Grovel through relocs to find out who stays ... */
13358 gc_mark_hook = bed->gc_mark_hook;
13359 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13363 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13364 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13368 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13371 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13372 Also treat note sections as a root, if the section is not part
13374 for (o = sub->sections; o != NULL; o = o->next)
13376 && (o->flags & SEC_EXCLUDE) == 0
13377 && ((o->flags & SEC_KEEP) != 0
13378 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13379 && elf_next_in_group (o) == NULL )))
13381 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13386 /* Allow the backend to mark additional target specific sections. */
13387 bed->gc_mark_extra_sections (info, gc_mark_hook);
13389 /* ... and mark SEC_EXCLUDE for those that go. */
13390 return elf_gc_sweep (abfd, info);
13393 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13396 bfd_elf_gc_record_vtinherit (bfd *abfd,
13398 struct elf_link_hash_entry *h,
13401 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13402 struct elf_link_hash_entry **search, *child;
13403 size_t extsymcount;
13404 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13406 /* The sh_info field of the symtab header tells us where the
13407 external symbols start. We don't care about the local symbols at
13409 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13410 if (!elf_bad_symtab (abfd))
13411 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13413 sym_hashes = elf_sym_hashes (abfd);
13414 sym_hashes_end = sym_hashes + extsymcount;
13416 /* Hunt down the child symbol, which is in this section at the same
13417 offset as the relocation. */
13418 for (search = sym_hashes; search != sym_hashes_end; ++search)
13420 if ((child = *search) != NULL
13421 && (child->root.type == bfd_link_hash_defined
13422 || child->root.type == bfd_link_hash_defweak)
13423 && child->root.u.def.section == sec
13424 && child->root.u.def.value == offset)
13428 /* xgettext:c-format */
13429 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"),
13430 abfd, sec, offset);
13431 bfd_set_error (bfd_error_invalid_operation);
13435 if (!child->u2.vtable)
13437 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13438 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13439 if (!child->u2.vtable)
13444 /* This *should* only be the absolute section. It could potentially
13445 be that someone has defined a non-global vtable though, which
13446 would be bad. It isn't worth paging in the local symbols to be
13447 sure though; that case should simply be handled by the assembler. */
13449 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13452 child->u2.vtable->parent = h;
13457 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13460 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13461 asection *sec ATTRIBUTE_UNUSED,
13462 struct elf_link_hash_entry *h,
13465 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13466 unsigned int log_file_align = bed->s->log_file_align;
13470 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13471 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13476 if (addend >= h->u2.vtable->size)
13478 size_t size, bytes, file_align;
13479 bfd_boolean *ptr = h->u2.vtable->used;
13481 /* While the symbol is undefined, we have to be prepared to handle
13483 file_align = 1 << log_file_align;
13484 if (h->root.type == bfd_link_hash_undefined)
13485 size = addend + file_align;
13489 if (addend >= size)
13491 /* Oops! We've got a reference past the defined end of
13492 the table. This is probably a bug -- shall we warn? */
13493 size = addend + file_align;
13496 size = (size + file_align - 1) & -file_align;
13498 /* Allocate one extra entry for use as a "done" flag for the
13499 consolidation pass. */
13500 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13504 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13510 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13511 * sizeof (bfd_boolean));
13512 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13516 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13521 /* And arrange for that done flag to be at index -1. */
13522 h->u2.vtable->used = ptr + 1;
13523 h->u2.vtable->size = size;
13526 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13531 /* Map an ELF section header flag to its corresponding string. */
13535 flagword flag_value;
13536 } elf_flags_to_name_table;
13538 static elf_flags_to_name_table elf_flags_to_names [] =
13540 { "SHF_WRITE", SHF_WRITE },
13541 { "SHF_ALLOC", SHF_ALLOC },
13542 { "SHF_EXECINSTR", SHF_EXECINSTR },
13543 { "SHF_MERGE", SHF_MERGE },
13544 { "SHF_STRINGS", SHF_STRINGS },
13545 { "SHF_INFO_LINK", SHF_INFO_LINK},
13546 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13547 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13548 { "SHF_GROUP", SHF_GROUP },
13549 { "SHF_TLS", SHF_TLS },
13550 { "SHF_MASKOS", SHF_MASKOS },
13551 { "SHF_EXCLUDE", SHF_EXCLUDE },
13554 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13556 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13557 struct flag_info *flaginfo,
13560 const bfd_vma sh_flags = elf_section_flags (section);
13562 if (!flaginfo->flags_initialized)
13564 bfd *obfd = info->output_bfd;
13565 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13566 struct flag_info_list *tf = flaginfo->flag_list;
13568 int without_hex = 0;
13570 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13573 flagword (*lookup) (char *);
13575 lookup = bed->elf_backend_lookup_section_flags_hook;
13576 if (lookup != NULL)
13578 flagword hexval = (*lookup) ((char *) tf->name);
13582 if (tf->with == with_flags)
13583 with_hex |= hexval;
13584 else if (tf->with == without_flags)
13585 without_hex |= hexval;
13590 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13592 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13594 if (tf->with == with_flags)
13595 with_hex |= elf_flags_to_names[i].flag_value;
13596 else if (tf->with == without_flags)
13597 without_hex |= elf_flags_to_names[i].flag_value;
13604 info->callbacks->einfo
13605 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13609 flaginfo->flags_initialized = TRUE;
13610 flaginfo->only_with_flags |= with_hex;
13611 flaginfo->not_with_flags |= without_hex;
13614 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13617 if ((flaginfo->not_with_flags & sh_flags) != 0)
13623 struct alloc_got_off_arg {
13625 struct bfd_link_info *info;
13628 /* We need a special top-level link routine to convert got reference counts
13629 to real got offsets. */
13632 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13634 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13635 bfd *obfd = gofarg->info->output_bfd;
13636 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13638 if (h->got.refcount > 0)
13640 h->got.offset = gofarg->gotoff;
13641 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13644 h->got.offset = (bfd_vma) -1;
13649 /* And an accompanying bit to work out final got entry offsets once
13650 we're done. Should be called from final_link. */
13653 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13654 struct bfd_link_info *info)
13657 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13659 struct alloc_got_off_arg gofarg;
13661 BFD_ASSERT (abfd == info->output_bfd);
13663 if (! is_elf_hash_table (info->hash))
13666 /* The GOT offset is relative to the .got section, but the GOT header is
13667 put into the .got.plt section, if the backend uses it. */
13668 if (bed->want_got_plt)
13671 gotoff = bed->got_header_size;
13673 /* Do the local .got entries first. */
13674 for (i = info->input_bfds; i; i = i->link.next)
13676 bfd_signed_vma *local_got;
13677 size_t j, locsymcount;
13678 Elf_Internal_Shdr *symtab_hdr;
13680 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13683 local_got = elf_local_got_refcounts (i);
13687 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13688 if (elf_bad_symtab (i))
13689 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13691 locsymcount = symtab_hdr->sh_info;
13693 for (j = 0; j < locsymcount; ++j)
13695 if (local_got[j] > 0)
13697 local_got[j] = gotoff;
13698 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13701 local_got[j] = (bfd_vma) -1;
13705 /* Then the global .got entries. .plt refcounts are handled by
13706 adjust_dynamic_symbol */
13707 gofarg.gotoff = gotoff;
13708 gofarg.info = info;
13709 elf_link_hash_traverse (elf_hash_table (info),
13710 elf_gc_allocate_got_offsets,
13715 /* Many folk need no more in the way of final link than this, once
13716 got entry reference counting is enabled. */
13719 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13721 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13724 /* Invoke the regular ELF backend linker to do all the work. */
13725 return bfd_elf_final_link (abfd, info);
13729 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13731 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13733 if (rcookie->bad_symtab)
13734 rcookie->rel = rcookie->rels;
13736 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13738 unsigned long r_symndx;
13740 if (! rcookie->bad_symtab)
13741 if (rcookie->rel->r_offset > offset)
13743 if (rcookie->rel->r_offset != offset)
13746 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13747 if (r_symndx == STN_UNDEF)
13750 if (r_symndx >= rcookie->locsymcount
13751 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13753 struct elf_link_hash_entry *h;
13755 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13757 while (h->root.type == bfd_link_hash_indirect
13758 || h->root.type == bfd_link_hash_warning)
13759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13761 if ((h->root.type == bfd_link_hash_defined
13762 || h->root.type == bfd_link_hash_defweak)
13763 && (h->root.u.def.section->owner != rcookie->abfd
13764 || h->root.u.def.section->kept_section != NULL
13765 || discarded_section (h->root.u.def.section)))
13770 /* It's not a relocation against a global symbol,
13771 but it could be a relocation against a local
13772 symbol for a discarded section. */
13774 Elf_Internal_Sym *isym;
13776 /* Need to: get the symbol; get the section. */
13777 isym = &rcookie->locsyms[r_symndx];
13778 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13780 && (isec->kept_section != NULL
13781 || discarded_section (isec)))
13789 /* Discard unneeded references to discarded sections.
13790 Returns -1 on error, 1 if any section's size was changed, 0 if
13791 nothing changed. This function assumes that the relocations are in
13792 sorted order, which is true for all known assemblers. */
13795 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13797 struct elf_reloc_cookie cookie;
13802 if (info->traditional_format
13803 || !is_elf_hash_table (info->hash))
13806 o = bfd_get_section_by_name (output_bfd, ".stab");
13811 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13814 || i->reloc_count == 0
13815 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13819 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13822 if (!init_reloc_cookie_for_section (&cookie, info, i))
13825 if (_bfd_discard_section_stabs (abfd, i,
13826 elf_section_data (i)->sec_info,
13827 bfd_elf_reloc_symbol_deleted_p,
13831 fini_reloc_cookie_for_section (&cookie, i);
13836 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13837 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13841 int eh_changed = 0;
13842 unsigned int eh_alignment;
13844 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13850 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13853 if (!init_reloc_cookie_for_section (&cookie, info, i))
13856 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13857 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13858 bfd_elf_reloc_symbol_deleted_p,
13862 if (i->size != i->rawsize)
13866 fini_reloc_cookie_for_section (&cookie, i);
13869 eh_alignment = 1 << o->alignment_power;
13870 /* Skip over zero terminator, and prevent empty sections from
13871 adding alignment padding at the end. */
13872 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
13874 i->flags |= SEC_EXCLUDE;
13875 else if (i->size > 4)
13877 /* The last non-empty eh_frame section doesn't need padding. */
13880 /* Any prior sections must pad the last FDE out to the output
13881 section alignment. Otherwise we might have zero padding
13882 between sections, which would be seen as a terminator. */
13883 for (; i != NULL; i = i->map_tail.s)
13885 /* All but the last zero terminator should have been removed. */
13890 = (i->size + eh_alignment - 1) & -eh_alignment;
13891 if (i->size != size)
13899 elf_link_hash_traverse (elf_hash_table (info),
13900 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
13903 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13905 const struct elf_backend_data *bed;
13908 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13910 s = abfd->sections;
13911 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13914 bed = get_elf_backend_data (abfd);
13916 if (bed->elf_backend_discard_info != NULL)
13918 if (!init_reloc_cookie (&cookie, info, abfd))
13921 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13924 fini_reloc_cookie (&cookie, abfd);
13928 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13929 _bfd_elf_end_eh_frame_parsing (info);
13931 if (info->eh_frame_hdr_type
13932 && !bfd_link_relocatable (info)
13933 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13940 _bfd_elf_section_already_linked (bfd *abfd,
13942 struct bfd_link_info *info)
13945 const char *name, *key;
13946 struct bfd_section_already_linked *l;
13947 struct bfd_section_already_linked_hash_entry *already_linked_list;
13949 if (sec->output_section == bfd_abs_section_ptr)
13952 flags = sec->flags;
13954 /* Return if it isn't a linkonce section. A comdat group section
13955 also has SEC_LINK_ONCE set. */
13956 if ((flags & SEC_LINK_ONCE) == 0)
13959 /* Don't put group member sections on our list of already linked
13960 sections. They are handled as a group via their group section. */
13961 if (elf_sec_group (sec) != NULL)
13964 /* For a SHT_GROUP section, use the group signature as the key. */
13966 if ((flags & SEC_GROUP) != 0
13967 && elf_next_in_group (sec) != NULL
13968 && elf_group_name (elf_next_in_group (sec)) != NULL)
13969 key = elf_group_name (elf_next_in_group (sec));
13972 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13973 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13974 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13977 /* Must be a user linkonce section that doesn't follow gcc's
13978 naming convention. In this case we won't be matching
13979 single member groups. */
13983 already_linked_list = bfd_section_already_linked_table_lookup (key);
13985 for (l = already_linked_list->entry; l != NULL; l = l->next)
13987 /* We may have 2 different types of sections on the list: group
13988 sections with a signature of <key> (<key> is some string),
13989 and linkonce sections named .gnu.linkonce.<type>.<key>.
13990 Match like sections. LTO plugin sections are an exception.
13991 They are always named .gnu.linkonce.t.<key> and match either
13992 type of section. */
13993 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13994 && ((flags & SEC_GROUP) != 0
13995 || strcmp (name, l->sec->name) == 0))
13996 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13998 /* The section has already been linked. See if we should
13999 issue a warning. */
14000 if (!_bfd_handle_already_linked (sec, l, info))
14003 if (flags & SEC_GROUP)
14005 asection *first = elf_next_in_group (sec);
14006 asection *s = first;
14010 s->output_section = bfd_abs_section_ptr;
14011 /* Record which group discards it. */
14012 s->kept_section = l->sec;
14013 s = elf_next_in_group (s);
14014 /* These lists are circular. */
14024 /* A single member comdat group section may be discarded by a
14025 linkonce section and vice versa. */
14026 if ((flags & SEC_GROUP) != 0)
14028 asection *first = elf_next_in_group (sec);
14030 if (first != NULL && elf_next_in_group (first) == first)
14031 /* Check this single member group against linkonce sections. */
14032 for (l = already_linked_list->entry; l != NULL; l = l->next)
14033 if ((l->sec->flags & SEC_GROUP) == 0
14034 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14036 first->output_section = bfd_abs_section_ptr;
14037 first->kept_section = l->sec;
14038 sec->output_section = bfd_abs_section_ptr;
14043 /* Check this linkonce section against single member groups. */
14044 for (l = already_linked_list->entry; l != NULL; l = l->next)
14045 if (l->sec->flags & SEC_GROUP)
14047 asection *first = elf_next_in_group (l->sec);
14050 && elf_next_in_group (first) == first
14051 && bfd_elf_match_symbols_in_sections (first, sec, info))
14053 sec->output_section = bfd_abs_section_ptr;
14054 sec->kept_section = first;
14059 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14060 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14061 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14062 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14063 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14064 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14065 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14066 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14067 The reverse order cannot happen as there is never a bfd with only the
14068 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14069 matter as here were are looking only for cross-bfd sections. */
14071 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14072 for (l = already_linked_list->entry; l != NULL; l = l->next)
14073 if ((l->sec->flags & SEC_GROUP) == 0
14074 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14076 if (abfd != l->sec->owner)
14077 sec->output_section = bfd_abs_section_ptr;
14081 /* This is the first section with this name. Record it. */
14082 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14083 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14084 return sec->output_section == bfd_abs_section_ptr;
14088 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14090 return sym->st_shndx == SHN_COMMON;
14094 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14100 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14102 return bfd_com_section_ptr;
14106 _bfd_elf_default_got_elt_size (bfd *abfd,
14107 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14108 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14109 bfd *ibfd ATTRIBUTE_UNUSED,
14110 unsigned long symndx ATTRIBUTE_UNUSED)
14112 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14113 return bed->s->arch_size / 8;
14116 /* Routines to support the creation of dynamic relocs. */
14118 /* Returns the name of the dynamic reloc section associated with SEC. */
14120 static const char *
14121 get_dynamic_reloc_section_name (bfd * abfd,
14123 bfd_boolean is_rela)
14126 const char *old_name = bfd_get_section_name (NULL, sec);
14127 const char *prefix = is_rela ? ".rela" : ".rel";
14129 if (old_name == NULL)
14132 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14133 sprintf (name, "%s%s", prefix, old_name);
14138 /* Returns the dynamic reloc section associated with SEC.
14139 If necessary compute the name of the dynamic reloc section based
14140 on SEC's name (looked up in ABFD's string table) and the setting
14144 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14146 bfd_boolean is_rela)
14148 asection * reloc_sec = elf_section_data (sec)->sreloc;
14150 if (reloc_sec == NULL)
14152 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14156 reloc_sec = bfd_get_linker_section (abfd, name);
14158 if (reloc_sec != NULL)
14159 elf_section_data (sec)->sreloc = reloc_sec;
14166 /* Returns the dynamic reloc section associated with SEC. If the
14167 section does not exist it is created and attached to the DYNOBJ
14168 bfd and stored in the SRELOC field of SEC's elf_section_data
14171 ALIGNMENT is the alignment for the newly created section and
14172 IS_RELA defines whether the name should be .rela.<SEC's name>
14173 or .rel.<SEC's name>. The section name is looked up in the
14174 string table associated with ABFD. */
14177 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14179 unsigned int alignment,
14181 bfd_boolean is_rela)
14183 asection * reloc_sec = elf_section_data (sec)->sreloc;
14185 if (reloc_sec == NULL)
14187 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14192 reloc_sec = bfd_get_linker_section (dynobj, name);
14194 if (reloc_sec == NULL)
14196 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14197 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14198 if ((sec->flags & SEC_ALLOC) != 0)
14199 flags |= SEC_ALLOC | SEC_LOAD;
14201 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14202 if (reloc_sec != NULL)
14204 /* _bfd_elf_get_sec_type_attr chooses a section type by
14205 name. Override as it may be wrong, eg. for a user
14206 section named "auto" we'll get ".relauto" which is
14207 seen to be a .rela section. */
14208 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14209 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14214 elf_section_data (sec)->sreloc = reloc_sec;
14220 /* Copy the ELF symbol type and other attributes for a linker script
14221 assignment from HSRC to HDEST. Generally this should be treated as
14222 if we found a strong non-dynamic definition for HDEST (except that
14223 ld ignores multiple definition errors). */
14225 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14226 struct bfd_link_hash_entry *hdest,
14227 struct bfd_link_hash_entry *hsrc)
14229 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14230 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14231 Elf_Internal_Sym isym;
14233 ehdest->type = ehsrc->type;
14234 ehdest->target_internal = ehsrc->target_internal;
14236 isym.st_other = ehsrc->other;
14237 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14240 /* Append a RELA relocation REL to section S in BFD. */
14243 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14245 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14246 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14247 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14248 bed->s->swap_reloca_out (abfd, rel, loc);
14251 /* Append a REL relocation REL to section S in BFD. */
14254 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14256 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14257 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14258 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14259 bed->s->swap_reloc_out (abfd, rel, loc);
14262 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14264 struct bfd_link_hash_entry *
14265 bfd_elf_define_start_stop (struct bfd_link_info *info,
14266 const char *symbol, asection *sec)
14268 struct elf_link_hash_entry *h;
14270 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14271 FALSE, FALSE, TRUE);
14273 && (h->root.type == bfd_link_hash_undefined
14274 || h->root.type == bfd_link_hash_undefweak
14275 || (h->ref_regular && !h->def_regular)))
14277 h->root.type = bfd_link_hash_defined;
14278 h->root.u.def.section = sec;
14279 h->root.u.def.value = 0;
14280 h->def_regular = 1;
14281 h->def_dynamic = 0;
14283 h->u2.start_stop_section = sec;
14284 if (symbol[0] == '.')
14286 /* .startof. and .sizeof. symbols are local. */
14287 const struct elf_backend_data *bed;
14288 bed = get_elf_backend_data (info->output_bfd);
14289 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14291 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14292 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;