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;
125 bed = get_elf_backend_data (abfd);
126 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
127 sec, 0, NULL, FALSE, bed->collect,
130 h = (struct elf_link_hash_entry *) bh;
133 h->root.linker_def = 1;
134 h->type = STT_OBJECT;
135 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
136 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
138 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
143 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
147 struct elf_link_hash_entry *h;
148 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
149 struct elf_link_hash_table *htab = elf_hash_table (info);
151 /* This function may be called more than once. */
152 if (htab->sgot != NULL)
155 flags = bed->dynamic_sec_flags;
157 s = bfd_make_section_anyway_with_flags (abfd,
158 (bed->rela_plts_and_copies_p
159 ? ".rela.got" : ".rel.got"),
160 (bed->dynamic_sec_flags
163 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
167 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
169 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
173 if (bed->want_got_plt)
175 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
177 || !bfd_set_section_alignment (abfd, s,
178 bed->s->log_file_align))
183 /* The first bit of the global offset table is the header. */
184 s->size += bed->got_header_size;
186 if (bed->want_got_sym)
188 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
189 (or .got.plt) section. We don't do this in the linker script
190 because we don't want to define the symbol if we are not creating
191 a global offset table. */
192 h = _bfd_elf_define_linkage_sym (abfd, info, s,
193 "_GLOBAL_OFFSET_TABLE_");
194 elf_hash_table (info)->hgot = h;
202 /* Create a strtab to hold the dynamic symbol names. */
204 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
206 struct elf_link_hash_table *hash_table;
208 hash_table = elf_hash_table (info);
209 if (hash_table->dynobj == NULL)
211 /* We may not set dynobj, an input file holding linker created
212 dynamic sections to abfd, which may be a dynamic object with
213 its own dynamic sections. We need to find a normal input file
214 to hold linker created sections if possible. */
215 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
218 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
220 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
226 hash_table->dynobj = abfd;
229 if (hash_table->dynstr == NULL)
231 hash_table->dynstr = _bfd_elf_strtab_init ();
232 if (hash_table->dynstr == NULL)
238 /* Create some sections which will be filled in with dynamic linking
239 information. ABFD is an input file which requires dynamic sections
240 to be created. The dynamic sections take up virtual memory space
241 when the final executable is run, so we need to create them before
242 addresses are assigned to the output sections. We work out the
243 actual contents and size of these sections later. */
246 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
250 const struct elf_backend_data *bed;
251 struct elf_link_hash_entry *h;
253 if (! is_elf_hash_table (info->hash))
256 if (elf_hash_table (info)->dynamic_sections_created)
259 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
262 abfd = elf_hash_table (info)->dynobj;
263 bed = get_elf_backend_data (abfd);
265 flags = bed->dynamic_sec_flags;
267 /* A dynamically linked executable has a .interp section, but a
268 shared library does not. */
269 if (bfd_link_executable (info) && !info->nointerp)
271 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
272 flags | SEC_READONLY);
277 /* Create sections to hold version informations. These are removed
278 if they are not needed. */
279 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
280 flags | SEC_READONLY);
282 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
286 flags | SEC_READONLY);
288 || ! bfd_set_section_alignment (abfd, s, 1))
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
292 flags | SEC_READONLY);
294 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
297 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
298 flags | SEC_READONLY);
300 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
302 elf_hash_table (info)->dynsym = s;
304 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
305 flags | SEC_READONLY);
309 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
311 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
314 /* The special symbol _DYNAMIC is always set to the start of the
315 .dynamic section. We could set _DYNAMIC in a linker script, but we
316 only want to define it if we are, in fact, creating a .dynamic
317 section. We don't want to define it if there is no .dynamic
318 section, since on some ELF platforms the start up code examines it
319 to decide how to initialize the process. */
320 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
321 elf_hash_table (info)->hdynamic = h;
327 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
328 flags | SEC_READONLY);
330 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
332 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
335 if (info->emit_gnu_hash)
337 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
338 flags | SEC_READONLY);
340 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
342 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
343 4 32-bit words followed by variable count of 64-bit words, then
344 variable count of 32-bit words. */
345 if (bed->s->arch_size == 64)
346 elf_section_data (s)->this_hdr.sh_entsize = 0;
348 elf_section_data (s)->this_hdr.sh_entsize = 4;
351 /* Let the backend create the rest of the sections. This lets the
352 backend set the right flags. The backend will normally create
353 the .got and .plt sections. */
354 if (bed->elf_backend_create_dynamic_sections == NULL
355 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
358 elf_hash_table (info)->dynamic_sections_created = TRUE;
363 /* Create dynamic sections when linking against a dynamic object. */
366 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
368 flagword flags, pltflags;
369 struct elf_link_hash_entry *h;
371 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
372 struct elf_link_hash_table *htab = elf_hash_table (info);
374 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
375 .rel[a].bss sections. */
376 flags = bed->dynamic_sec_flags;
379 if (bed->plt_not_loaded)
380 /* We do not clear SEC_ALLOC here because we still want the OS to
381 allocate space for the section; it's just that there's nothing
382 to read in from the object file. */
383 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
385 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
386 if (bed->plt_readonly)
387 pltflags |= SEC_READONLY;
389 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
391 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
395 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
397 if (bed->want_plt_sym)
399 h = _bfd_elf_define_linkage_sym (abfd, info, s,
400 "_PROCEDURE_LINKAGE_TABLE_");
401 elf_hash_table (info)->hplt = h;
406 s = bfd_make_section_anyway_with_flags (abfd,
407 (bed->rela_plts_and_copies_p
408 ? ".rela.plt" : ".rel.plt"),
409 flags | SEC_READONLY);
411 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
415 if (! _bfd_elf_create_got_section (abfd, info))
418 if (bed->want_dynbss)
420 /* The .dynbss section is a place to put symbols which are defined
421 by dynamic objects, are referenced by regular objects, and are
422 not functions. We must allocate space for them in the process
423 image and use a R_*_COPY reloc to tell the dynamic linker to
424 initialize them at run time. The linker script puts the .dynbss
425 section into the .bss section of the final image. */
426 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
427 SEC_ALLOC | SEC_LINKER_CREATED);
432 if (bed->want_dynrelro)
434 /* Similarly, but for symbols that were originally in read-only
435 sections. This section doesn't really need to have contents,
436 but make it like other .data.rel.ro sections. */
437 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
444 /* The .rel[a].bss section holds copy relocs. This section is not
445 normally needed. We need to create it here, though, so that the
446 linker will map it to an output section. We can't just create it
447 only if we need it, because we will not know whether we need it
448 until we have seen all the input files, and the first time the
449 main linker code calls BFD after examining all the input files
450 (size_dynamic_sections) the input sections have already been
451 mapped to the output sections. If the section turns out not to
452 be needed, we can discard it later. We will never need this
453 section when generating a shared object, since they do not use
455 if (bfd_link_executable (info))
457 s = bfd_make_section_anyway_with_flags (abfd,
458 (bed->rela_plts_and_copies_p
459 ? ".rela.bss" : ".rel.bss"),
460 flags | SEC_READONLY);
462 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
466 if (bed->want_dynrelro)
468 s = (bfd_make_section_anyway_with_flags
469 (abfd, (bed->rela_plts_and_copies_p
470 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
471 flags | SEC_READONLY));
473 || ! bfd_set_section_alignment (abfd, s,
474 bed->s->log_file_align))
476 htab->sreldynrelro = s;
484 /* Record a new dynamic symbol. We record the dynamic symbols as we
485 read the input files, since we need to have a list of all of them
486 before we can determine the final sizes of the output sections.
487 Note that we may actually call this function even though we are not
488 going to output any dynamic symbols; in some cases we know that a
489 symbol should be in the dynamic symbol table, but only if there is
493 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
494 struct elf_link_hash_entry *h)
496 if (h->dynindx == -1)
498 struct elf_strtab_hash *dynstr;
503 /* XXX: The ABI draft says the linker must turn hidden and
504 internal symbols into STB_LOCAL symbols when producing the
505 DSO. However, if ld.so honors st_other in the dynamic table,
506 this would not be necessary. */
507 switch (ELF_ST_VISIBILITY (h->other))
511 if (h->root.type != bfd_link_hash_undefined
512 && h->root.type != bfd_link_hash_undefweak)
515 if (!elf_hash_table (info)->is_relocatable_executable)
523 h->dynindx = elf_hash_table (info)->dynsymcount;
524 ++elf_hash_table (info)->dynsymcount;
526 dynstr = elf_hash_table (info)->dynstr;
529 /* Create a strtab to hold the dynamic symbol names. */
530 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
535 /* We don't put any version information in the dynamic string
537 name = h->root.root.string;
538 p = strchr (name, ELF_VER_CHR);
540 /* We know that the p points into writable memory. In fact,
541 there are only a few symbols that have read-only names, being
542 those like _GLOBAL_OFFSET_TABLE_ that are created specially
543 by the backends. Most symbols will have names pointing into
544 an ELF string table read from a file, or to objalloc memory. */
547 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
552 if (indx == (size_t) -1)
554 h->dynstr_index = indx;
560 /* Mark a symbol dynamic. */
563 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
564 struct elf_link_hash_entry *h,
565 Elf_Internal_Sym *sym)
567 struct bfd_elf_dynamic_list *d = info->dynamic_list;
569 /* It may be called more than once on the same H. */
570 if(h->dynamic || bfd_link_relocatable (info))
573 if ((info->dynamic_data
574 && (h->type == STT_OBJECT
575 || h->type == STT_COMMON
577 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
578 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
580 && h->root.type == bfd_link_hash_new
581 && (*d->match) (&d->head, NULL, h->root.root.string)))
585 /* Record an assignment to a symbol made by a linker script. We need
586 this in case some dynamic object refers to this symbol. */
589 bfd_elf_record_link_assignment (bfd *output_bfd,
590 struct bfd_link_info *info,
595 struct elf_link_hash_entry *h, *hv;
596 struct elf_link_hash_table *htab;
597 const struct elf_backend_data *bed;
599 if (!is_elf_hash_table (info->hash))
602 htab = elf_hash_table (info);
603 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
607 if (h->root.type == bfd_link_hash_warning)
608 h = (struct elf_link_hash_entry *) h->root.u.i.link;
610 if (h->versioned == unknown)
612 /* Set versioned if symbol version is unknown. */
613 char *version = strrchr (name, ELF_VER_CHR);
616 if (version > name && version[-1] != ELF_VER_CHR)
617 h->versioned = versioned_hidden;
619 h->versioned = versioned;
623 switch (h->root.type)
625 case bfd_link_hash_defined:
626 case bfd_link_hash_defweak:
627 case bfd_link_hash_common:
629 case bfd_link_hash_undefweak:
630 case bfd_link_hash_undefined:
631 /* Since we're defining the symbol, don't let it seem to have not
632 been defined. record_dynamic_symbol and size_dynamic_sections
633 may depend on this. */
634 h->root.type = bfd_link_hash_new;
635 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
636 bfd_link_repair_undef_list (&htab->root);
638 case bfd_link_hash_new:
639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
642 case bfd_link_hash_indirect:
643 /* We had a versioned symbol in a dynamic library. We make the
644 the versioned symbol point to this one. */
645 bed = get_elf_backend_data (output_bfd);
647 while (hv->root.type == bfd_link_hash_indirect
648 || hv->root.type == bfd_link_hash_warning)
649 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
650 /* We don't need to update h->root.u since linker will set them
652 h->root.type = bfd_link_hash_undefined;
653 hv->root.type = bfd_link_hash_indirect;
654 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
655 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
662 /* If this symbol is being provided by the linker script, and it is
663 currently defined by a dynamic object, but not by a regular
664 object, then mark it as undefined so that the generic linker will
665 force the correct value. */
669 h->root.type = bfd_link_hash_undefined;
671 /* If this symbol is not being provided by the linker script, and it is
672 currently defined by a dynamic object, but not by a regular object,
673 then clear out any version information because the symbol will not be
674 associated with the dynamic object any more. */
678 h->verinfo.verdef = NULL;
680 /* Make sure this symbol is not garbage collected. */
687 bed = get_elf_backend_data (output_bfd);
688 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
689 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
690 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
693 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
695 if (!bfd_link_relocatable (info)
697 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
698 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
703 || bfd_link_dll (info)
704 || elf_hash_table (info)->is_relocatable_executable)
707 if (! bfd_elf_link_record_dynamic_symbol (info, h))
710 /* If this is a weak defined symbol, and we know a corresponding
711 real symbol from the same dynamic object, make sure the real
712 symbol is also made into a dynamic symbol. */
713 if (h->u.weakdef != NULL
714 && h->u.weakdef->dynindx == -1)
716 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
724 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
725 success, and 2 on a failure caused by attempting to record a symbol
726 in a discarded section, eg. a discarded link-once section symbol. */
729 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
734 struct elf_link_local_dynamic_entry *entry;
735 struct elf_link_hash_table *eht;
736 struct elf_strtab_hash *dynstr;
739 Elf_External_Sym_Shndx eshndx;
740 char esym[sizeof (Elf64_External_Sym)];
742 if (! is_elf_hash_table (info->hash))
745 /* See if the entry exists already. */
746 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
747 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
750 amt = sizeof (*entry);
751 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
755 /* Go find the symbol, so that we can find it's name. */
756 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
757 1, input_indx, &entry->isym, esym, &eshndx))
759 bfd_release (input_bfd, entry);
763 if (entry->isym.st_shndx != SHN_UNDEF
764 && entry->isym.st_shndx < SHN_LORESERVE)
768 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
769 if (s == NULL || bfd_is_abs_section (s->output_section))
771 /* We can still bfd_release here as nothing has done another
772 bfd_alloc. We can't do this later in this function. */
773 bfd_release (input_bfd, entry);
778 name = (bfd_elf_string_from_elf_section
779 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
780 entry->isym.st_name));
782 dynstr = elf_hash_table (info)->dynstr;
785 /* Create a strtab to hold the dynamic symbol names. */
786 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
791 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
792 if (dynstr_index == (size_t) -1)
794 entry->isym.st_name = dynstr_index;
796 eht = elf_hash_table (info);
798 entry->next = eht->dynlocal;
799 eht->dynlocal = entry;
800 entry->input_bfd = input_bfd;
801 entry->input_indx = input_indx;
804 /* Whatever binding the symbol had before, it's now local. */
806 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
808 /* The dynindx will be set at the end of size_dynamic_sections. */
813 /* Return the dynindex of a local dynamic symbol. */
816 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
820 struct elf_link_local_dynamic_entry *e;
822 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
823 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
828 /* This function is used to renumber the dynamic symbols, if some of
829 them are removed because they are marked as local. This is called
830 via elf_link_hash_traverse. */
833 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
836 size_t *count = (size_t *) data;
841 if (h->dynindx != -1)
842 h->dynindx = ++(*count);
848 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
849 STB_LOCAL binding. */
852 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
855 size_t *count = (size_t *) data;
857 if (!h->forced_local)
860 if (h->dynindx != -1)
861 h->dynindx = ++(*count);
866 /* Return true if the dynamic symbol for a given section should be
867 omitted when creating a shared library. */
869 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
870 struct bfd_link_info *info,
873 struct elf_link_hash_table *htab;
876 switch (elf_section_data (p)->this_hdr.sh_type)
880 /* If sh_type is yet undecided, assume it could be
881 SHT_PROGBITS/SHT_NOBITS. */
883 htab = elf_hash_table (info);
884 if (p == htab->tls_sec)
887 if (htab->text_index_section != NULL)
888 return p != htab->text_index_section && p != htab->data_index_section;
890 return (htab->dynobj != NULL
891 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
892 && ip->output_section == p);
894 /* There shouldn't be section relative relocations
895 against any other section. */
901 /* Assign dynsym indices. In a shared library we generate a section
902 symbol for each output section, which come first. Next come symbols
903 which have been forced to local binding. Then all of the back-end
904 allocated local dynamic syms, followed by the rest of the global
908 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
909 struct bfd_link_info *info,
910 unsigned long *section_sym_count)
912 unsigned long dynsymcount = 0;
914 if (bfd_link_pic (info)
915 || elf_hash_table (info)->is_relocatable_executable)
917 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
919 for (p = output_bfd->sections; p ; p = p->next)
920 if ((p->flags & SEC_EXCLUDE) == 0
921 && (p->flags & SEC_ALLOC) != 0
922 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
923 elf_section_data (p)->dynindx = ++dynsymcount;
925 elf_section_data (p)->dynindx = 0;
927 *section_sym_count = dynsymcount;
929 elf_link_hash_traverse (elf_hash_table (info),
930 elf_link_renumber_local_hash_table_dynsyms,
933 if (elf_hash_table (info)->dynlocal)
935 struct elf_link_local_dynamic_entry *p;
936 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
937 p->dynindx = ++dynsymcount;
939 elf_hash_table (info)->local_dynsymcount = dynsymcount;
941 elf_link_hash_traverse (elf_hash_table (info),
942 elf_link_renumber_hash_table_dynsyms,
945 /* There is an unused NULL entry at the head of the table which we
946 must account for in our count even if the table is empty since it
947 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
951 elf_hash_table (info)->dynsymcount = dynsymcount;
955 /* Merge st_other field. */
958 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
959 const Elf_Internal_Sym *isym, asection *sec,
960 bfd_boolean definition, bfd_boolean dynamic)
962 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
964 /* If st_other has a processor-specific meaning, specific
965 code might be needed here. */
966 if (bed->elf_backend_merge_symbol_attribute)
967 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
972 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
973 unsigned hvis = ELF_ST_VISIBILITY (h->other);
975 /* Keep the most constraining visibility. Leave the remainder
976 of the st_other field to elf_backend_merge_symbol_attribute. */
977 if (symvis - 1 < hvis - 1)
978 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
981 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
982 && (sec->flags & SEC_READONLY) == 0)
983 h->protected_def = 1;
986 /* This function is called when we want to merge a new symbol with an
987 existing symbol. It handles the various cases which arise when we
988 find a definition in a dynamic object, or when there is already a
989 definition in a dynamic object. The new symbol is described by
990 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
991 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
992 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
993 of an old common symbol. We set OVERRIDE if the old symbol is
994 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
995 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
996 to change. By OK to change, we mean that we shouldn't warn if the
997 type or size does change. */
1000 _bfd_elf_merge_symbol (bfd *abfd,
1001 struct bfd_link_info *info,
1003 Elf_Internal_Sym *sym,
1006 struct elf_link_hash_entry **sym_hash,
1008 bfd_boolean *pold_weak,
1009 unsigned int *pold_alignment,
1011 bfd_boolean *override,
1012 bfd_boolean *type_change_ok,
1013 bfd_boolean *size_change_ok,
1014 bfd_boolean *matched)
1016 asection *sec, *oldsec;
1017 struct elf_link_hash_entry *h;
1018 struct elf_link_hash_entry *hi;
1019 struct elf_link_hash_entry *flip;
1022 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1023 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1024 const struct elf_backend_data *bed;
1031 bind = ELF_ST_BIND (sym->st_info);
1033 if (! bfd_is_und_section (sec))
1034 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1036 h = ((struct elf_link_hash_entry *)
1037 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1042 bed = get_elf_backend_data (abfd);
1044 /* NEW_VERSION is the symbol version of the new symbol. */
1045 if (h->versioned != unversioned)
1047 /* Symbol version is unknown or versioned. */
1048 new_version = strrchr (name, ELF_VER_CHR);
1051 if (h->versioned == unknown)
1053 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1054 h->versioned = versioned_hidden;
1056 h->versioned = versioned;
1059 if (new_version[0] == '\0')
1063 h->versioned = unversioned;
1068 /* For merging, we only care about real symbols. But we need to make
1069 sure that indirect symbol dynamic flags are updated. */
1071 while (h->root.type == bfd_link_hash_indirect
1072 || h->root.type == bfd_link_hash_warning)
1073 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1077 if (hi == h || h->root.type == bfd_link_hash_new)
1081 /* OLD_HIDDEN is true if the existing symbol is only visible
1082 to the symbol with the same symbol version. NEW_HIDDEN is
1083 true if the new symbol is only visible to the symbol with
1084 the same symbol version. */
1085 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1086 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1087 if (!old_hidden && !new_hidden)
1088 /* The new symbol matches the existing symbol if both
1093 /* OLD_VERSION is the symbol version of the existing
1097 if (h->versioned >= versioned)
1098 old_version = strrchr (h->root.root.string,
1103 /* The new symbol matches the existing symbol if they
1104 have the same symbol version. */
1105 *matched = (old_version == new_version
1106 || (old_version != NULL
1107 && new_version != NULL
1108 && strcmp (old_version, new_version) == 0));
1113 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1118 switch (h->root.type)
1123 case bfd_link_hash_undefined:
1124 case bfd_link_hash_undefweak:
1125 oldbfd = h->root.u.undef.abfd;
1128 case bfd_link_hash_defined:
1129 case bfd_link_hash_defweak:
1130 oldbfd = h->root.u.def.section->owner;
1131 oldsec = h->root.u.def.section;
1134 case bfd_link_hash_common:
1135 oldbfd = h->root.u.c.p->section->owner;
1136 oldsec = h->root.u.c.p->section;
1138 *pold_alignment = h->root.u.c.p->alignment_power;
1141 if (poldbfd && *poldbfd == NULL)
1144 /* Differentiate strong and weak symbols. */
1145 newweak = bind == STB_WEAK;
1146 oldweak = (h->root.type == bfd_link_hash_defweak
1147 || h->root.type == bfd_link_hash_undefweak);
1149 *pold_weak = oldweak;
1151 /* This code is for coping with dynamic objects, and is only useful
1152 if we are doing an ELF link. */
1153 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1156 /* We have to check it for every instance since the first few may be
1157 references and not all compilers emit symbol type for undefined
1159 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1161 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1162 respectively, is from a dynamic object. */
1164 newdyn = (abfd->flags & DYNAMIC) != 0;
1166 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1167 syms and defined syms in dynamic libraries respectively.
1168 ref_dynamic on the other hand can be set for a symbol defined in
1169 a dynamic library, and def_dynamic may not be set; When the
1170 definition in a dynamic lib is overridden by a definition in the
1171 executable use of the symbol in the dynamic lib becomes a
1172 reference to the executable symbol. */
1175 if (bfd_is_und_section (sec))
1177 if (bind != STB_WEAK)
1179 h->ref_dynamic_nonweak = 1;
1180 hi->ref_dynamic_nonweak = 1;
1185 /* Update the existing symbol only if they match. */
1188 hi->dynamic_def = 1;
1192 /* If we just created the symbol, mark it as being an ELF symbol.
1193 Other than that, there is nothing to do--there is no merge issue
1194 with a newly defined symbol--so we just return. */
1196 if (h->root.type == bfd_link_hash_new)
1202 /* In cases involving weak versioned symbols, we may wind up trying
1203 to merge a symbol with itself. Catch that here, to avoid the
1204 confusion that results if we try to override a symbol with
1205 itself. The additional tests catch cases like
1206 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1207 dynamic object, which we do want to handle here. */
1209 && (newweak || oldweak)
1210 && ((abfd->flags & DYNAMIC) == 0
1211 || !h->def_regular))
1216 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1217 else if (oldsec != NULL)
1219 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1220 indices used by MIPS ELF. */
1221 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1224 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1225 respectively, appear to be a definition rather than reference. */
1227 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1229 olddef = (h->root.type != bfd_link_hash_undefined
1230 && h->root.type != bfd_link_hash_undefweak
1231 && h->root.type != bfd_link_hash_common);
1233 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1234 respectively, appear to be a function. */
1236 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1237 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1239 oldfunc = (h->type != STT_NOTYPE
1240 && bed->is_function_type (h->type));
1242 /* If creating a default indirect symbol ("foo" or "foo@") from a
1243 dynamic versioned definition ("foo@@") skip doing so if there is
1244 an existing regular definition with a different type. We don't
1245 want, for example, a "time" variable in the executable overriding
1246 a "time" function in a shared library. */
1247 if (pold_alignment == NULL
1251 && (olddef || h->root.type == bfd_link_hash_common)
1252 && ELF_ST_TYPE (sym->st_info) != h->type
1253 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1254 && h->type != STT_NOTYPE
1255 && !(newfunc && oldfunc))
1261 /* Check TLS symbols. We don't check undefined symbols introduced
1262 by "ld -u" which have no type (and oldbfd NULL), and we don't
1263 check symbols from plugins because they also have no type. */
1265 && (oldbfd->flags & BFD_PLUGIN) == 0
1266 && (abfd->flags & BFD_PLUGIN) == 0
1267 && ELF_ST_TYPE (sym->st_info) != h->type
1268 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1271 bfd_boolean ntdef, tdef;
1272 asection *ntsec, *tsec;
1274 if (h->type == STT_TLS)
1295 /* xgettext:c-format */
1296 (_("%s: TLS definition in %B section %A "
1297 "mismatches non-TLS definition in %B section %A"),
1298 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1299 else if (!tdef && !ntdef)
1301 /* xgettext:c-format */
1302 (_("%s: TLS reference in %B "
1303 "mismatches non-TLS reference in %B"),
1304 tbfd, ntbfd, h->root.root.string);
1307 /* xgettext:c-format */
1308 (_("%s: TLS definition in %B section %A "
1309 "mismatches non-TLS reference in %B"),
1310 tbfd, tsec, ntbfd, h->root.root.string);
1313 /* xgettext:c-format */
1314 (_("%s: TLS reference in %B "
1315 "mismatches non-TLS definition in %B section %A"),
1316 tbfd, ntbfd, ntsec, h->root.root.string);
1318 bfd_set_error (bfd_error_bad_value);
1322 /* If the old symbol has non-default visibility, we ignore the new
1323 definition from a dynamic object. */
1325 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1326 && !bfd_is_und_section (sec))
1329 /* Make sure this symbol is dynamic. */
1331 hi->ref_dynamic = 1;
1332 /* A protected symbol has external availability. Make sure it is
1333 recorded as dynamic.
1335 FIXME: Should we check type and size for protected symbol? */
1336 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1337 return bfd_elf_link_record_dynamic_symbol (info, h);
1342 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1345 /* If the new symbol with non-default visibility comes from a
1346 relocatable file and the old definition comes from a dynamic
1347 object, we remove the old definition. */
1348 if (hi->root.type == bfd_link_hash_indirect)
1350 /* Handle the case where the old dynamic definition is
1351 default versioned. We need to copy the symbol info from
1352 the symbol with default version to the normal one if it
1353 was referenced before. */
1356 hi->root.type = h->root.type;
1357 h->root.type = bfd_link_hash_indirect;
1358 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1360 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1361 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1363 /* If the new symbol is hidden or internal, completely undo
1364 any dynamic link state. */
1365 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1366 h->forced_local = 0;
1373 /* FIXME: Should we check type and size for protected symbol? */
1383 /* If the old symbol was undefined before, then it will still be
1384 on the undefs list. If the new symbol is undefined or
1385 common, we can't make it bfd_link_hash_new here, because new
1386 undefined or common symbols will be added to the undefs list
1387 by _bfd_generic_link_add_one_symbol. Symbols may not be
1388 added twice to the undefs list. Also, if the new symbol is
1389 undefweak then we don't want to lose the strong undef. */
1390 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1392 h->root.type = bfd_link_hash_undefined;
1393 h->root.u.undef.abfd = abfd;
1397 h->root.type = bfd_link_hash_new;
1398 h->root.u.undef.abfd = NULL;
1401 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1403 /* If the new symbol is hidden or internal, completely undo
1404 any dynamic link state. */
1405 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1406 h->forced_local = 0;
1412 /* FIXME: Should we check type and size for protected symbol? */
1418 /* If a new weak symbol definition comes from a regular file and the
1419 old symbol comes from a dynamic library, we treat the new one as
1420 strong. Similarly, an old weak symbol definition from a regular
1421 file is treated as strong when the new symbol comes from a dynamic
1422 library. Further, an old weak symbol from a dynamic library is
1423 treated as strong if the new symbol is from a dynamic library.
1424 This reflects the way glibc's ld.so works.
1426 Do this before setting *type_change_ok or *size_change_ok so that
1427 we warn properly when dynamic library symbols are overridden. */
1429 if (newdef && !newdyn && olddyn)
1431 if (olddef && newdyn)
1434 /* Allow changes between different types of function symbol. */
1435 if (newfunc && oldfunc)
1436 *type_change_ok = TRUE;
1438 /* It's OK to change the type if either the existing symbol or the
1439 new symbol is weak. A type change is also OK if the old symbol
1440 is undefined and the new symbol is defined. */
1445 && h->root.type == bfd_link_hash_undefined))
1446 *type_change_ok = TRUE;
1448 /* It's OK to change the size if either the existing symbol or the
1449 new symbol is weak, or if the old symbol is undefined. */
1452 || h->root.type == bfd_link_hash_undefined)
1453 *size_change_ok = TRUE;
1455 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1456 symbol, respectively, appears to be a common symbol in a dynamic
1457 object. If a symbol appears in an uninitialized section, and is
1458 not weak, and is not a function, then it may be a common symbol
1459 which was resolved when the dynamic object was created. We want
1460 to treat such symbols specially, because they raise special
1461 considerations when setting the symbol size: if the symbol
1462 appears as a common symbol in a regular object, and the size in
1463 the regular object is larger, we must make sure that we use the
1464 larger size. This problematic case can always be avoided in C,
1465 but it must be handled correctly when using Fortran shared
1468 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1469 likewise for OLDDYNCOMMON and OLDDEF.
1471 Note that this test is just a heuristic, and that it is quite
1472 possible to have an uninitialized symbol in a shared object which
1473 is really a definition, rather than a common symbol. This could
1474 lead to some minor confusion when the symbol really is a common
1475 symbol in some regular object. However, I think it will be
1481 && (sec->flags & SEC_ALLOC) != 0
1482 && (sec->flags & SEC_LOAD) == 0
1485 newdyncommon = TRUE;
1487 newdyncommon = FALSE;
1491 && h->root.type == bfd_link_hash_defined
1493 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1494 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1497 olddyncommon = TRUE;
1499 olddyncommon = FALSE;
1501 /* We now know everything about the old and new symbols. We ask the
1502 backend to check if we can merge them. */
1503 if (bed->merge_symbol != NULL)
1505 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1510 /* If both the old and the new symbols look like common symbols in a
1511 dynamic object, set the size of the symbol to the larger of the
1516 && sym->st_size != h->size)
1518 /* Since we think we have two common symbols, issue a multiple
1519 common warning if desired. Note that we only warn if the
1520 size is different. If the size is the same, we simply let
1521 the old symbol override the new one as normally happens with
1522 symbols defined in dynamic objects. */
1524 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1525 bfd_link_hash_common, sym->st_size);
1526 if (sym->st_size > h->size)
1527 h->size = sym->st_size;
1529 *size_change_ok = TRUE;
1532 /* If we are looking at a dynamic object, and we have found a
1533 definition, we need to see if the symbol was already defined by
1534 some other object. If so, we want to use the existing
1535 definition, and we do not want to report a multiple symbol
1536 definition error; we do this by clobbering *PSEC to be
1537 bfd_und_section_ptr.
1539 We treat a common symbol as a definition if the symbol in the
1540 shared library is a function, since common symbols always
1541 represent variables; this can cause confusion in principle, but
1542 any such confusion would seem to indicate an erroneous program or
1543 shared library. We also permit a common symbol in a regular
1544 object to override a weak symbol in a shared object. A common
1545 symbol in executable also overrides a symbol in a shared object. */
1550 || (h->root.type == bfd_link_hash_common
1553 || (!olddyn && bfd_link_executable (info))))))
1557 newdyncommon = FALSE;
1559 *psec = sec = bfd_und_section_ptr;
1560 *size_change_ok = TRUE;
1562 /* If we get here when the old symbol is a common symbol, then
1563 we are explicitly letting it override a weak symbol or
1564 function in a dynamic object, and we don't want to warn about
1565 a type change. If the old symbol is a defined symbol, a type
1566 change warning may still be appropriate. */
1568 if (h->root.type == bfd_link_hash_common)
1569 *type_change_ok = TRUE;
1572 /* Handle the special case of an old common symbol merging with a
1573 new symbol which looks like a common symbol in a shared object.
1574 We change *PSEC and *PVALUE to make the new symbol look like a
1575 common symbol, and let _bfd_generic_link_add_one_symbol do the
1579 && h->root.type == bfd_link_hash_common)
1583 newdyncommon = FALSE;
1584 *pvalue = sym->st_size;
1585 *psec = sec = bed->common_section (oldsec);
1586 *size_change_ok = TRUE;
1589 /* Skip weak definitions of symbols that are already defined. */
1590 if (newdef && olddef && newweak)
1592 /* Don't skip new non-IR weak syms. */
1593 if (!(oldbfd != NULL
1594 && (oldbfd->flags & BFD_PLUGIN) != 0
1595 && (abfd->flags & BFD_PLUGIN) == 0))
1601 /* Merge st_other. If the symbol already has a dynamic index,
1602 but visibility says it should not be visible, turn it into a
1604 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1605 if (h->dynindx != -1)
1606 switch (ELF_ST_VISIBILITY (h->other))
1610 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1615 /* If the old symbol is from a dynamic object, and the new symbol is
1616 a definition which is not from a dynamic object, then the new
1617 symbol overrides the old symbol. Symbols from regular files
1618 always take precedence over symbols from dynamic objects, even if
1619 they are defined after the dynamic object in the link.
1621 As above, we again permit a common symbol in a regular object to
1622 override a definition in a shared object if the shared object
1623 symbol is a function or is weak. */
1628 || (bfd_is_com_section (sec)
1629 && (oldweak || oldfunc)))
1634 /* Change the hash table entry to undefined, and let
1635 _bfd_generic_link_add_one_symbol do the right thing with the
1638 h->root.type = bfd_link_hash_undefined;
1639 h->root.u.undef.abfd = h->root.u.def.section->owner;
1640 *size_change_ok = TRUE;
1643 olddyncommon = FALSE;
1645 /* We again permit a type change when a common symbol may be
1646 overriding a function. */
1648 if (bfd_is_com_section (sec))
1652 /* If a common symbol overrides a function, make sure
1653 that it isn't defined dynamically nor has type
1656 h->type = STT_NOTYPE;
1658 *type_change_ok = TRUE;
1661 if (hi->root.type == bfd_link_hash_indirect)
1664 /* This union may have been set to be non-NULL when this symbol
1665 was seen in a dynamic object. We must force the union to be
1666 NULL, so that it is correct for a regular symbol. */
1667 h->verinfo.vertree = NULL;
1670 /* Handle the special case of a new common symbol merging with an
1671 old symbol that looks like it might be a common symbol defined in
1672 a shared object. Note that we have already handled the case in
1673 which a new common symbol should simply override the definition
1674 in the shared library. */
1677 && bfd_is_com_section (sec)
1680 /* It would be best if we could set the hash table entry to a
1681 common symbol, but we don't know what to use for the section
1682 or the alignment. */
1683 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1684 bfd_link_hash_common, sym->st_size);
1686 /* If the presumed common symbol in the dynamic object is
1687 larger, pretend that the new symbol has its size. */
1689 if (h->size > *pvalue)
1692 /* We need to remember the alignment required by the symbol
1693 in the dynamic object. */
1694 BFD_ASSERT (pold_alignment);
1695 *pold_alignment = h->root.u.def.section->alignment_power;
1698 olddyncommon = FALSE;
1700 h->root.type = bfd_link_hash_undefined;
1701 h->root.u.undef.abfd = h->root.u.def.section->owner;
1703 *size_change_ok = TRUE;
1704 *type_change_ok = TRUE;
1706 if (hi->root.type == bfd_link_hash_indirect)
1709 h->verinfo.vertree = NULL;
1714 /* Handle the case where we had a versioned symbol in a dynamic
1715 library and now find a definition in a normal object. In this
1716 case, we make the versioned symbol point to the normal one. */
1717 flip->root.type = h->root.type;
1718 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1719 h->root.type = bfd_link_hash_indirect;
1720 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1721 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1725 flip->ref_dynamic = 1;
1732 /* This function is called to create an indirect symbol from the
1733 default for the symbol with the default version if needed. The
1734 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1735 set DYNSYM if the new indirect symbol is dynamic. */
1738 _bfd_elf_add_default_symbol (bfd *abfd,
1739 struct bfd_link_info *info,
1740 struct elf_link_hash_entry *h,
1742 Elf_Internal_Sym *sym,
1746 bfd_boolean *dynsym)
1748 bfd_boolean type_change_ok;
1749 bfd_boolean size_change_ok;
1752 struct elf_link_hash_entry *hi;
1753 struct bfd_link_hash_entry *bh;
1754 const struct elf_backend_data *bed;
1755 bfd_boolean collect;
1756 bfd_boolean dynamic;
1757 bfd_boolean override;
1759 size_t len, shortlen;
1761 bfd_boolean matched;
1763 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1766 /* If this symbol has a version, and it is the default version, we
1767 create an indirect symbol from the default name to the fully
1768 decorated name. This will cause external references which do not
1769 specify a version to be bound to this version of the symbol. */
1770 p = strchr (name, ELF_VER_CHR);
1771 if (h->versioned == unknown)
1775 h->versioned = unversioned;
1780 if (p[1] != ELF_VER_CHR)
1782 h->versioned = versioned_hidden;
1786 h->versioned = versioned;
1791 /* PR ld/19073: We may see an unversioned definition after the
1797 bed = get_elf_backend_data (abfd);
1798 collect = bed->collect;
1799 dynamic = (abfd->flags & DYNAMIC) != 0;
1801 shortlen = p - name;
1802 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1803 if (shortname == NULL)
1805 memcpy (shortname, name, shortlen);
1806 shortname[shortlen] = '\0';
1808 /* We are going to create a new symbol. Merge it with any existing
1809 symbol with this name. For the purposes of the merge, act as
1810 though we were defining the symbol we just defined, although we
1811 actually going to define an indirect symbol. */
1812 type_change_ok = FALSE;
1813 size_change_ok = FALSE;
1816 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1817 &hi, poldbfd, NULL, NULL, &skip, &override,
1818 &type_change_ok, &size_change_ok, &matched))
1824 if (hi->def_regular)
1826 /* If the undecorated symbol will have a version added by a
1827 script different to H, then don't indirect to/from the
1828 undecorated symbol. This isn't ideal because we may not yet
1829 have seen symbol versions, if given by a script on the
1830 command line rather than via --version-script. */
1831 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1836 = bfd_find_version_for_sym (info->version_info,
1837 hi->root.root.string, &hide);
1838 if (hi->verinfo.vertree != NULL && hide)
1840 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1844 if (hi->verinfo.vertree != NULL
1845 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1851 /* Add the default symbol if not performing a relocatable link. */
1852 if (! bfd_link_relocatable (info))
1855 if (! (_bfd_generic_link_add_one_symbol
1856 (info, abfd, shortname, BSF_INDIRECT,
1857 bfd_ind_section_ptr,
1858 0, name, FALSE, collect, &bh)))
1860 hi = (struct elf_link_hash_entry *) bh;
1865 /* In this case the symbol named SHORTNAME is overriding the
1866 indirect symbol we want to add. We were planning on making
1867 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1868 is the name without a version. NAME is the fully versioned
1869 name, and it is the default version.
1871 Overriding means that we already saw a definition for the
1872 symbol SHORTNAME in a regular object, and it is overriding
1873 the symbol defined in the dynamic object.
1875 When this happens, we actually want to change NAME, the
1876 symbol we just added, to refer to SHORTNAME. This will cause
1877 references to NAME in the shared object to become references
1878 to SHORTNAME in the regular object. This is what we expect
1879 when we override a function in a shared object: that the
1880 references in the shared object will be mapped to the
1881 definition in the regular object. */
1883 while (hi->root.type == bfd_link_hash_indirect
1884 || hi->root.type == bfd_link_hash_warning)
1885 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1887 h->root.type = bfd_link_hash_indirect;
1888 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1892 hi->ref_dynamic = 1;
1896 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1901 /* Now set HI to H, so that the following code will set the
1902 other fields correctly. */
1906 /* Check if HI is a warning symbol. */
1907 if (hi->root.type == bfd_link_hash_warning)
1908 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1910 /* If there is a duplicate definition somewhere, then HI may not
1911 point to an indirect symbol. We will have reported an error to
1912 the user in that case. */
1914 if (hi->root.type == bfd_link_hash_indirect)
1916 struct elf_link_hash_entry *ht;
1918 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1919 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1921 /* A reference to the SHORTNAME symbol from a dynamic library
1922 will be satisfied by the versioned symbol at runtime. In
1923 effect, we have a reference to the versioned symbol. */
1924 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1925 hi->dynamic_def |= ht->dynamic_def;
1927 /* See if the new flags lead us to realize that the symbol must
1933 if (! bfd_link_executable (info)
1940 if (hi->ref_regular)
1946 /* We also need to define an indirection from the nondefault version
1950 len = strlen (name);
1951 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1952 if (shortname == NULL)
1954 memcpy (shortname, name, shortlen);
1955 memcpy (shortname + shortlen, p + 1, len - shortlen);
1957 /* Once again, merge with any existing symbol. */
1958 type_change_ok = FALSE;
1959 size_change_ok = FALSE;
1961 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1962 &hi, poldbfd, NULL, NULL, &skip, &override,
1963 &type_change_ok, &size_change_ok, &matched))
1971 /* Here SHORTNAME is a versioned name, so we don't expect to see
1972 the type of override we do in the case above unless it is
1973 overridden by a versioned definition. */
1974 if (hi->root.type != bfd_link_hash_defined
1975 && hi->root.type != bfd_link_hash_defweak)
1977 /* xgettext:c-format */
1978 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1984 if (! (_bfd_generic_link_add_one_symbol
1985 (info, abfd, shortname, BSF_INDIRECT,
1986 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1988 hi = (struct elf_link_hash_entry *) bh;
1990 /* If there is a duplicate definition somewhere, then HI may not
1991 point to an indirect symbol. We will have reported an error
1992 to the user in that case. */
1994 if (hi->root.type == bfd_link_hash_indirect)
1996 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1997 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1998 hi->dynamic_def |= h->dynamic_def;
2000 /* See if the new flags lead us to realize that the symbol
2006 if (! bfd_link_executable (info)
2012 if (hi->ref_regular)
2022 /* This routine is used to export all defined symbols into the dynamic
2023 symbol table. It is called via elf_link_hash_traverse. */
2026 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2028 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2030 /* Ignore indirect symbols. These are added by the versioning code. */
2031 if (h->root.type == bfd_link_hash_indirect)
2034 /* Ignore this if we won't export it. */
2035 if (!eif->info->export_dynamic && !h->dynamic)
2038 if (h->dynindx == -1
2039 && (h->def_regular || h->ref_regular)
2040 && ! bfd_hide_sym_by_version (eif->info->version_info,
2041 h->root.root.string))
2043 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2053 /* Look through the symbols which are defined in other shared
2054 libraries and referenced here. Update the list of version
2055 dependencies. This will be put into the .gnu.version_r section.
2056 This function is called via elf_link_hash_traverse. */
2059 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2062 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2063 Elf_Internal_Verneed *t;
2064 Elf_Internal_Vernaux *a;
2067 /* We only care about symbols defined in shared objects with version
2072 || h->verinfo.verdef == NULL
2073 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2074 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2077 /* See if we already know about this version. */
2078 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2082 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2085 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2086 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2092 /* This is a new version. Add it to tree we are building. */
2097 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2100 rinfo->failed = TRUE;
2104 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2105 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2106 elf_tdata (rinfo->info->output_bfd)->verref = t;
2110 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2113 rinfo->failed = TRUE;
2117 /* Note that we are copying a string pointer here, and testing it
2118 above. If bfd_elf_string_from_elf_section is ever changed to
2119 discard the string data when low in memory, this will have to be
2121 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2123 a->vna_flags = h->verinfo.verdef->vd_flags;
2124 a->vna_nextptr = t->vn_auxptr;
2126 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2129 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2136 /* Figure out appropriate versions for all the symbols. We may not
2137 have the version number script until we have read all of the input
2138 files, so until that point we don't know which symbols should be
2139 local. This function is called via elf_link_hash_traverse. */
2142 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2144 struct elf_info_failed *sinfo;
2145 struct bfd_link_info *info;
2146 const struct elf_backend_data *bed;
2147 struct elf_info_failed eif;
2150 sinfo = (struct elf_info_failed *) data;
2153 /* Fix the symbol flags. */
2156 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2159 sinfo->failed = TRUE;
2163 /* We only need version numbers for symbols defined in regular
2165 if (!h->def_regular)
2168 bed = get_elf_backend_data (info->output_bfd);
2169 p = strchr (h->root.root.string, ELF_VER_CHR);
2170 if (p != NULL && h->verinfo.vertree == NULL)
2172 struct bfd_elf_version_tree *t;
2175 if (*p == ELF_VER_CHR)
2178 /* If there is no version string, we can just return out. */
2182 /* Look for the version. If we find it, it is no longer weak. */
2183 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2185 if (strcmp (t->name, p) == 0)
2189 struct bfd_elf_version_expr *d;
2191 len = p - h->root.root.string;
2192 alc = (char *) bfd_malloc (len);
2195 sinfo->failed = TRUE;
2198 memcpy (alc, h->root.root.string, len - 1);
2199 alc[len - 1] = '\0';
2200 if (alc[len - 2] == ELF_VER_CHR)
2201 alc[len - 2] = '\0';
2203 h->verinfo.vertree = t;
2207 if (t->globals.list != NULL)
2208 d = (*t->match) (&t->globals, NULL, alc);
2210 /* See if there is anything to force this symbol to
2212 if (d == NULL && t->locals.list != NULL)
2214 d = (*t->match) (&t->locals, NULL, alc);
2217 && ! info->export_dynamic)
2218 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2226 /* If we are building an application, we need to create a
2227 version node for this version. */
2228 if (t == NULL && bfd_link_executable (info))
2230 struct bfd_elf_version_tree **pp;
2233 /* If we aren't going to export this symbol, we don't need
2234 to worry about it. */
2235 if (h->dynindx == -1)
2238 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2242 sinfo->failed = TRUE;
2247 t->name_indx = (unsigned int) -1;
2251 /* Don't count anonymous version tag. */
2252 if (sinfo->info->version_info != NULL
2253 && sinfo->info->version_info->vernum == 0)
2255 for (pp = &sinfo->info->version_info;
2259 t->vernum = version_index;
2263 h->verinfo.vertree = t;
2267 /* We could not find the version for a symbol when
2268 generating a shared archive. Return an error. */
2270 /* xgettext:c-format */
2271 (_("%B: version node not found for symbol %s"),
2272 info->output_bfd, h->root.root.string);
2273 bfd_set_error (bfd_error_bad_value);
2274 sinfo->failed = TRUE;
2279 /* If we don't have a version for this symbol, see if we can find
2281 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2286 = bfd_find_version_for_sym (sinfo->info->version_info,
2287 h->root.root.string, &hide);
2288 if (h->verinfo.vertree != NULL && hide)
2289 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2295 /* Read and swap the relocs from the section indicated by SHDR. This
2296 may be either a REL or a RELA section. The relocations are
2297 translated into RELA relocations and stored in INTERNAL_RELOCS,
2298 which should have already been allocated to contain enough space.
2299 The EXTERNAL_RELOCS are a buffer where the external form of the
2300 relocations should be stored.
2302 Returns FALSE if something goes wrong. */
2305 elf_link_read_relocs_from_section (bfd *abfd,
2307 Elf_Internal_Shdr *shdr,
2308 void *external_relocs,
2309 Elf_Internal_Rela *internal_relocs)
2311 const struct elf_backend_data *bed;
2312 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2313 const bfd_byte *erela;
2314 const bfd_byte *erelaend;
2315 Elf_Internal_Rela *irela;
2316 Elf_Internal_Shdr *symtab_hdr;
2319 /* Position ourselves at the start of the section. */
2320 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2323 /* Read the relocations. */
2324 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2327 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2328 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2330 bed = get_elf_backend_data (abfd);
2332 /* Convert the external relocations to the internal format. */
2333 if (shdr->sh_entsize == bed->s->sizeof_rel)
2334 swap_in = bed->s->swap_reloc_in;
2335 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2336 swap_in = bed->s->swap_reloca_in;
2339 bfd_set_error (bfd_error_wrong_format);
2343 erela = (const bfd_byte *) external_relocs;
2344 erelaend = erela + shdr->sh_size;
2345 irela = internal_relocs;
2346 while (erela < erelaend)
2350 (*swap_in) (abfd, erela, irela);
2351 r_symndx = ELF32_R_SYM (irela->r_info);
2352 if (bed->s->arch_size == 64)
2356 if ((size_t) r_symndx >= nsyms)
2359 /* xgettext:c-format */
2360 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2361 " for offset 0x%lx in section `%A'"),
2363 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2364 bfd_set_error (bfd_error_bad_value);
2368 else if (r_symndx != STN_UNDEF)
2371 /* xgettext:c-format */
2372 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2373 " when the object file has no symbol table"),
2375 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2376 bfd_set_error (bfd_error_bad_value);
2379 irela += bed->s->int_rels_per_ext_rel;
2380 erela += shdr->sh_entsize;
2386 /* Read and swap the relocs for a section O. They may have been
2387 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2388 not NULL, they are used as buffers to read into. They are known to
2389 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2390 the return value is allocated using either malloc or bfd_alloc,
2391 according to the KEEP_MEMORY argument. If O has two relocation
2392 sections (both REL and RELA relocations), then the REL_HDR
2393 relocations will appear first in INTERNAL_RELOCS, followed by the
2394 RELA_HDR relocations. */
2397 _bfd_elf_link_read_relocs (bfd *abfd,
2399 void *external_relocs,
2400 Elf_Internal_Rela *internal_relocs,
2401 bfd_boolean keep_memory)
2403 void *alloc1 = NULL;
2404 Elf_Internal_Rela *alloc2 = NULL;
2405 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2406 struct bfd_elf_section_data *esdo = elf_section_data (o);
2407 Elf_Internal_Rela *internal_rela_relocs;
2409 if (esdo->relocs != NULL)
2410 return esdo->relocs;
2412 if (o->reloc_count == 0)
2415 if (internal_relocs == NULL)
2419 size = o->reloc_count;
2420 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2422 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2424 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2425 if (internal_relocs == NULL)
2429 if (external_relocs == NULL)
2431 bfd_size_type size = 0;
2434 size += esdo->rel.hdr->sh_size;
2436 size += esdo->rela.hdr->sh_size;
2438 alloc1 = bfd_malloc (size);
2441 external_relocs = alloc1;
2444 internal_rela_relocs = internal_relocs;
2447 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2451 external_relocs = (((bfd_byte *) external_relocs)
2452 + esdo->rel.hdr->sh_size);
2453 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2454 * bed->s->int_rels_per_ext_rel);
2458 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2460 internal_rela_relocs)))
2463 /* Cache the results for next time, if we can. */
2465 esdo->relocs = internal_relocs;
2470 /* Don't free alloc2, since if it was allocated we are passing it
2471 back (under the name of internal_relocs). */
2473 return internal_relocs;
2481 bfd_release (abfd, alloc2);
2488 /* Compute the size of, and allocate space for, REL_HDR which is the
2489 section header for a section containing relocations for O. */
2492 _bfd_elf_link_size_reloc_section (bfd *abfd,
2493 struct bfd_elf_section_reloc_data *reldata)
2495 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2497 /* That allows us to calculate the size of the section. */
2498 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2500 /* The contents field must last into write_object_contents, so we
2501 allocate it with bfd_alloc rather than malloc. Also since we
2502 cannot be sure that the contents will actually be filled in,
2503 we zero the allocated space. */
2504 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2505 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2508 if (reldata->hashes == NULL && reldata->count)
2510 struct elf_link_hash_entry **p;
2512 p = ((struct elf_link_hash_entry **)
2513 bfd_zmalloc (reldata->count * sizeof (*p)));
2517 reldata->hashes = p;
2523 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2524 originated from the section given by INPUT_REL_HDR) to the
2528 _bfd_elf_link_output_relocs (bfd *output_bfd,
2529 asection *input_section,
2530 Elf_Internal_Shdr *input_rel_hdr,
2531 Elf_Internal_Rela *internal_relocs,
2532 struct elf_link_hash_entry **rel_hash
2535 Elf_Internal_Rela *irela;
2536 Elf_Internal_Rela *irelaend;
2538 struct bfd_elf_section_reloc_data *output_reldata;
2539 asection *output_section;
2540 const struct elf_backend_data *bed;
2541 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2542 struct bfd_elf_section_data *esdo;
2544 output_section = input_section->output_section;
2546 bed = get_elf_backend_data (output_bfd);
2547 esdo = elf_section_data (output_section);
2548 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2550 output_reldata = &esdo->rel;
2551 swap_out = bed->s->swap_reloc_out;
2553 else if (esdo->rela.hdr
2554 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2556 output_reldata = &esdo->rela;
2557 swap_out = bed->s->swap_reloca_out;
2562 /* xgettext:c-format */
2563 (_("%B: relocation size mismatch in %B section %A"),
2564 output_bfd, input_section->owner, input_section);
2565 bfd_set_error (bfd_error_wrong_format);
2569 erel = output_reldata->hdr->contents;
2570 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2571 irela = internal_relocs;
2572 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2573 * bed->s->int_rels_per_ext_rel);
2574 while (irela < irelaend)
2576 (*swap_out) (output_bfd, irela, erel);
2577 irela += bed->s->int_rels_per_ext_rel;
2578 erel += input_rel_hdr->sh_entsize;
2581 /* Bump the counter, so that we know where to add the next set of
2583 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2588 /* Make weak undefined symbols in PIE dynamic. */
2591 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2592 struct elf_link_hash_entry *h)
2594 if (bfd_link_pie (info)
2596 && h->root.type == bfd_link_hash_undefweak)
2597 return bfd_elf_link_record_dynamic_symbol (info, h);
2602 /* Fix up the flags for a symbol. This handles various cases which
2603 can only be fixed after all the input files are seen. This is
2604 currently called by both adjust_dynamic_symbol and
2605 assign_sym_version, which is unnecessary but perhaps more robust in
2606 the face of future changes. */
2609 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2610 struct elf_info_failed *eif)
2612 const struct elf_backend_data *bed;
2614 /* If this symbol was mentioned in a non-ELF file, try to set
2615 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2616 permit a non-ELF file to correctly refer to a symbol defined in
2617 an ELF dynamic object. */
2620 while (h->root.type == bfd_link_hash_indirect)
2621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2623 if (h->root.type != bfd_link_hash_defined
2624 && h->root.type != bfd_link_hash_defweak)
2627 h->ref_regular_nonweak = 1;
2631 if (h->root.u.def.section->owner != NULL
2632 && (bfd_get_flavour (h->root.u.def.section->owner)
2633 == bfd_target_elf_flavour))
2636 h->ref_regular_nonweak = 1;
2642 if (h->dynindx == -1
2646 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2655 /* Unfortunately, NON_ELF is only correct if the symbol
2656 was first seen in a non-ELF file. Fortunately, if the symbol
2657 was first seen in an ELF file, we're probably OK unless the
2658 symbol was defined in a non-ELF file. Catch that case here.
2659 FIXME: We're still in trouble if the symbol was first seen in
2660 a dynamic object, and then later in a non-ELF regular object. */
2661 if ((h->root.type == bfd_link_hash_defined
2662 || h->root.type == bfd_link_hash_defweak)
2664 && (h->root.u.def.section->owner != NULL
2665 ? (bfd_get_flavour (h->root.u.def.section->owner)
2666 != bfd_target_elf_flavour)
2667 : (bfd_is_abs_section (h->root.u.def.section)
2668 && !h->def_dynamic)))
2672 /* Backend specific symbol fixup. */
2673 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2674 if (bed->elf_backend_fixup_symbol
2675 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2678 /* If this is a final link, and the symbol was defined as a common
2679 symbol in a regular object file, and there was no definition in
2680 any dynamic object, then the linker will have allocated space for
2681 the symbol in a common section but the DEF_REGULAR
2682 flag will not have been set. */
2683 if (h->root.type == bfd_link_hash_defined
2687 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2690 /* If a weak undefined symbol has non-default visibility, we also
2691 hide it from the dynamic linker. */
2692 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2693 && h->root.type == bfd_link_hash_undefweak)
2694 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2696 /* A hidden versioned symbol in executable should be forced local if
2697 it is is locally defined, not referenced by shared library and not
2699 else if (bfd_link_executable (eif->info)
2700 && h->versioned == versioned_hidden
2701 && !eif->info->export_dynamic
2705 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2707 /* If -Bsymbolic was used (which means to bind references to global
2708 symbols to the definition within the shared object), and this
2709 symbol was defined in a regular object, then it actually doesn't
2710 need a PLT entry. Likewise, if the symbol has non-default
2711 visibility. If the symbol has hidden or internal visibility, we
2712 will force it local. */
2713 else if (h->needs_plt
2714 && bfd_link_pic (eif->info)
2715 && is_elf_hash_table (eif->info->hash)
2716 && (SYMBOLIC_BIND (eif->info, h)
2717 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2720 bfd_boolean force_local;
2722 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2723 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2724 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2727 /* If this is a weak defined symbol in a dynamic object, and we know
2728 the real definition in the dynamic object, copy interesting flags
2729 over to the real definition. */
2730 if (h->u.weakdef != NULL)
2732 /* If the real definition is defined by a regular object file,
2733 don't do anything special. See the longer description in
2734 _bfd_elf_adjust_dynamic_symbol, below. */
2735 if (h->u.weakdef->def_regular)
2736 h->u.weakdef = NULL;
2739 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2741 while (h->root.type == bfd_link_hash_indirect)
2742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2744 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2745 || h->root.type == bfd_link_hash_defweak);
2746 BFD_ASSERT (weakdef->def_dynamic);
2747 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2748 || weakdef->root.type == bfd_link_hash_defweak);
2749 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2756 /* Make the backend pick a good value for a dynamic symbol. This is
2757 called via elf_link_hash_traverse, and also calls itself
2761 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2763 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2765 const struct elf_backend_data *bed;
2767 if (! is_elf_hash_table (eif->info->hash))
2770 /* Ignore indirect symbols. These are added by the versioning code. */
2771 if (h->root.type == bfd_link_hash_indirect)
2774 /* Fix the symbol flags. */
2775 if (! _bfd_elf_fix_symbol_flags (h, eif))
2778 /* If this symbol does not require a PLT entry, and it is not
2779 defined by a dynamic object, or is not referenced by a regular
2780 object, ignore it. We do have to handle a weak defined symbol,
2781 even if no regular object refers to it, if we decided to add it
2782 to the dynamic symbol table. FIXME: Do we normally need to worry
2783 about symbols which are defined by one dynamic object and
2784 referenced by another one? */
2786 && h->type != STT_GNU_IFUNC
2790 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2792 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2796 /* If we've already adjusted this symbol, don't do it again. This
2797 can happen via a recursive call. */
2798 if (h->dynamic_adjusted)
2801 /* Don't look at this symbol again. Note that we must set this
2802 after checking the above conditions, because we may look at a
2803 symbol once, decide not to do anything, and then get called
2804 recursively later after REF_REGULAR is set below. */
2805 h->dynamic_adjusted = 1;
2807 /* If this is a weak definition, and we know a real definition, and
2808 the real symbol is not itself defined by a regular object file,
2809 then get a good value for the real definition. We handle the
2810 real symbol first, for the convenience of the backend routine.
2812 Note that there is a confusing case here. If the real definition
2813 is defined by a regular object file, we don't get the real symbol
2814 from the dynamic object, but we do get the weak symbol. If the
2815 processor backend uses a COPY reloc, then if some routine in the
2816 dynamic object changes the real symbol, we will not see that
2817 change in the corresponding weak symbol. This is the way other
2818 ELF linkers work as well, and seems to be a result of the shared
2821 I will clarify this issue. Most SVR4 shared libraries define the
2822 variable _timezone and define timezone as a weak synonym. The
2823 tzset call changes _timezone. If you write
2824 extern int timezone;
2826 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2827 you might expect that, since timezone is a synonym for _timezone,
2828 the same number will print both times. However, if the processor
2829 backend uses a COPY reloc, then actually timezone will be copied
2830 into your process image, and, since you define _timezone
2831 yourself, _timezone will not. Thus timezone and _timezone will
2832 wind up at different memory locations. The tzset call will set
2833 _timezone, leaving timezone unchanged. */
2835 if (h->u.weakdef != NULL)
2837 /* If we get to this point, there is an implicit reference to
2838 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2839 h->u.weakdef->ref_regular = 1;
2841 /* Ensure that the backend adjust_dynamic_symbol function sees
2842 H->U.WEAKDEF before H by recursively calling ourselves. */
2843 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2847 /* If a symbol has no type and no size and does not require a PLT
2848 entry, then we are probably about to do the wrong thing here: we
2849 are probably going to create a COPY reloc for an empty object.
2850 This case can arise when a shared object is built with assembly
2851 code, and the assembly code fails to set the symbol type. */
2853 && h->type == STT_NOTYPE
2856 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2857 h->root.root.string);
2859 dynobj = elf_hash_table (eif->info)->dynobj;
2860 bed = get_elf_backend_data (dynobj);
2862 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2871 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2875 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2876 struct elf_link_hash_entry *h,
2879 unsigned int power_of_two;
2881 asection *sec = h->root.u.def.section;
2883 /* The section aligment of definition is the maximum alignment
2884 requirement of symbols defined in the section. Since we don't
2885 know the symbol alignment requirement, we start with the
2886 maximum alignment and check low bits of the symbol address
2887 for the minimum alignment. */
2888 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2889 mask = ((bfd_vma) 1 << power_of_two) - 1;
2890 while ((h->root.u.def.value & mask) != 0)
2896 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2899 /* Adjust the section alignment if needed. */
2900 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2905 /* We make sure that the symbol will be aligned properly. */
2906 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2908 /* Define the symbol as being at this point in DYNBSS. */
2909 h->root.u.def.section = dynbss;
2910 h->root.u.def.value = dynbss->size;
2912 /* Increment the size of DYNBSS to make room for the symbol. */
2913 dynbss->size += h->size;
2915 /* No error if extern_protected_data is true. */
2916 if (h->protected_def
2917 && (!info->extern_protected_data
2918 || (info->extern_protected_data < 0
2919 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2920 info->callbacks->einfo
2921 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2922 h->root.root.string);
2927 /* Adjust all external symbols pointing into SEC_MERGE sections
2928 to reflect the object merging within the sections. */
2931 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2935 if ((h->root.type == bfd_link_hash_defined
2936 || h->root.type == bfd_link_hash_defweak)
2937 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2938 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2940 bfd *output_bfd = (bfd *) data;
2942 h->root.u.def.value =
2943 _bfd_merged_section_offset (output_bfd,
2944 &h->root.u.def.section,
2945 elf_section_data (sec)->sec_info,
2946 h->root.u.def.value);
2952 /* Returns false if the symbol referred to by H should be considered
2953 to resolve local to the current module, and true if it should be
2954 considered to bind dynamically. */
2957 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2958 struct bfd_link_info *info,
2959 bfd_boolean not_local_protected)
2961 bfd_boolean binding_stays_local_p;
2962 const struct elf_backend_data *bed;
2963 struct elf_link_hash_table *hash_table;
2968 while (h->root.type == bfd_link_hash_indirect
2969 || h->root.type == bfd_link_hash_warning)
2970 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2972 /* If it was forced local, then clearly it's not dynamic. */
2973 if (h->dynindx == -1)
2975 if (h->forced_local)
2978 /* Identify the cases where name binding rules say that a
2979 visible symbol resolves locally. */
2980 binding_stays_local_p = (bfd_link_executable (info)
2981 || SYMBOLIC_BIND (info, h));
2983 switch (ELF_ST_VISIBILITY (h->other))
2990 hash_table = elf_hash_table (info);
2991 if (!is_elf_hash_table (hash_table))
2994 bed = get_elf_backend_data (hash_table->dynobj);
2996 /* Proper resolution for function pointer equality may require
2997 that these symbols perhaps be resolved dynamically, even though
2998 we should be resolving them to the current module. */
2999 if (!not_local_protected || !bed->is_function_type (h->type))
3000 binding_stays_local_p = TRUE;
3007 /* If it isn't defined locally, then clearly it's dynamic. */
3008 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3011 /* Otherwise, the symbol is dynamic if binding rules don't tell
3012 us that it remains local. */
3013 return !binding_stays_local_p;
3016 /* Return true if the symbol referred to by H should be considered
3017 to resolve local to the current module, and false otherwise. Differs
3018 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3019 undefined symbols. The two functions are virtually identical except
3020 for the place where forced_local and dynindx == -1 are tested. If
3021 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
3022 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
3023 the symbol is local only for defined symbols.
3024 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3025 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3026 treatment of undefined weak symbols. For those that do not make
3027 undefined weak symbols dynamic, both functions may return false. */
3030 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3031 struct bfd_link_info *info,
3032 bfd_boolean local_protected)
3034 const struct elf_backend_data *bed;
3035 struct elf_link_hash_table *hash_table;
3037 /* If it's a local sym, of course we resolve locally. */
3041 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3042 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3043 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3046 /* Common symbols that become definitions don't get the DEF_REGULAR
3047 flag set, so test it first, and don't bail out. */
3048 if (ELF_COMMON_DEF_P (h))
3050 /* If we don't have a definition in a regular file, then we can't
3051 resolve locally. The sym is either undefined or dynamic. */
3052 else if (!h->def_regular)
3055 /* Forced local symbols resolve locally. */
3056 if (h->forced_local)
3059 /* As do non-dynamic symbols. */
3060 if (h->dynindx == -1)
3063 /* At this point, we know the symbol is defined and dynamic. In an
3064 executable it must resolve locally, likewise when building symbolic
3065 shared libraries. */
3066 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3069 /* Now deal with defined dynamic symbols in shared libraries. Ones
3070 with default visibility might not resolve locally. */
3071 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3074 hash_table = elf_hash_table (info);
3075 if (!is_elf_hash_table (hash_table))
3078 bed = get_elf_backend_data (hash_table->dynobj);
3080 /* If extern_protected_data is false, STV_PROTECTED non-function
3081 symbols are local. */
3082 if ((!info->extern_protected_data
3083 || (info->extern_protected_data < 0
3084 && !bed->extern_protected_data))
3085 && !bed->is_function_type (h->type))
3088 /* Function pointer equality tests may require that STV_PROTECTED
3089 symbols be treated as dynamic symbols. If the address of a
3090 function not defined in an executable is set to that function's
3091 plt entry in the executable, then the address of the function in
3092 a shared library must also be the plt entry in the executable. */
3093 return local_protected;
3096 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3097 aligned. Returns the first TLS output section. */
3099 struct bfd_section *
3100 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3102 struct bfd_section *sec, *tls;
3103 unsigned int align = 0;
3105 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3106 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3110 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3111 if (sec->alignment_power > align)
3112 align = sec->alignment_power;
3114 elf_hash_table (info)->tls_sec = tls;
3116 /* Ensure the alignment of the first section is the largest alignment,
3117 so that the tls segment starts aligned. */
3119 tls->alignment_power = align;
3124 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3126 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3127 Elf_Internal_Sym *sym)
3129 const struct elf_backend_data *bed;
3131 /* Local symbols do not count, but target specific ones might. */
3132 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3133 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3136 bed = get_elf_backend_data (abfd);
3137 /* Function symbols do not count. */
3138 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3141 /* If the section is undefined, then so is the symbol. */
3142 if (sym->st_shndx == SHN_UNDEF)
3145 /* If the symbol is defined in the common section, then
3146 it is a common definition and so does not count. */
3147 if (bed->common_definition (sym))
3150 /* If the symbol is in a target specific section then we
3151 must rely upon the backend to tell us what it is. */
3152 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3153 /* FIXME - this function is not coded yet:
3155 return _bfd_is_global_symbol_definition (abfd, sym);
3157 Instead for now assume that the definition is not global,
3158 Even if this is wrong, at least the linker will behave
3159 in the same way that it used to do. */
3165 /* Search the symbol table of the archive element of the archive ABFD
3166 whose archive map contains a mention of SYMDEF, and determine if
3167 the symbol is defined in this element. */
3169 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3171 Elf_Internal_Shdr * hdr;
3175 Elf_Internal_Sym *isymbuf;
3176 Elf_Internal_Sym *isym;
3177 Elf_Internal_Sym *isymend;
3180 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3184 if (! bfd_check_format (abfd, bfd_object))
3187 /* Select the appropriate symbol table. If we don't know if the
3188 object file is an IR object, give linker LTO plugin a chance to
3189 get the correct symbol table. */
3190 if (abfd->plugin_format == bfd_plugin_yes
3191 #if BFD_SUPPORTS_PLUGINS
3192 || (abfd->plugin_format == bfd_plugin_unknown
3193 && bfd_link_plugin_object_p (abfd))
3197 /* Use the IR symbol table if the object has been claimed by
3199 abfd = abfd->plugin_dummy_bfd;
3200 hdr = &elf_tdata (abfd)->symtab_hdr;
3202 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3203 hdr = &elf_tdata (abfd)->symtab_hdr;
3205 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3207 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3209 /* The sh_info field of the symtab header tells us where the
3210 external symbols start. We don't care about the local symbols. */
3211 if (elf_bad_symtab (abfd))
3213 extsymcount = symcount;
3218 extsymcount = symcount - hdr->sh_info;
3219 extsymoff = hdr->sh_info;
3222 if (extsymcount == 0)
3225 /* Read in the symbol table. */
3226 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3228 if (isymbuf == NULL)
3231 /* Scan the symbol table looking for SYMDEF. */
3233 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3237 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3242 if (strcmp (name, symdef->name) == 0)
3244 result = is_global_data_symbol_definition (abfd, isym);
3254 /* Add an entry to the .dynamic table. */
3257 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3261 struct elf_link_hash_table *hash_table;
3262 const struct elf_backend_data *bed;
3264 bfd_size_type newsize;
3265 bfd_byte *newcontents;
3266 Elf_Internal_Dyn dyn;
3268 hash_table = elf_hash_table (info);
3269 if (! is_elf_hash_table (hash_table))
3272 bed = get_elf_backend_data (hash_table->dynobj);
3273 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3274 BFD_ASSERT (s != NULL);
3276 newsize = s->size + bed->s->sizeof_dyn;
3277 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3278 if (newcontents == NULL)
3282 dyn.d_un.d_val = val;
3283 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3286 s->contents = newcontents;
3291 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3292 otherwise just check whether one already exists. Returns -1 on error,
3293 1 if a DT_NEEDED tag already exists, and 0 on success. */
3296 elf_add_dt_needed_tag (bfd *abfd,
3297 struct bfd_link_info *info,
3301 struct elf_link_hash_table *hash_table;
3304 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3307 hash_table = elf_hash_table (info);
3308 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3309 if (strindex == (size_t) -1)
3312 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3315 const struct elf_backend_data *bed;
3318 bed = get_elf_backend_data (hash_table->dynobj);
3319 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3321 for (extdyn = sdyn->contents;
3322 extdyn < sdyn->contents + sdyn->size;
3323 extdyn += bed->s->sizeof_dyn)
3325 Elf_Internal_Dyn dyn;
3327 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3328 if (dyn.d_tag == DT_NEEDED
3329 && dyn.d_un.d_val == strindex)
3331 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3339 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3342 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3346 /* We were just checking for existence of the tag. */
3347 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3352 /* Return true if SONAME is on the needed list between NEEDED and STOP
3353 (or the end of list if STOP is NULL), and needed by a library that
3357 on_needed_list (const char *soname,
3358 struct bfd_link_needed_list *needed,
3359 struct bfd_link_needed_list *stop)
3361 struct bfd_link_needed_list *look;
3362 for (look = needed; look != stop; look = look->next)
3363 if (strcmp (soname, look->name) == 0
3364 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3365 /* If needed by a library that itself is not directly
3366 needed, recursively check whether that library is
3367 indirectly needed. Since we add DT_NEEDED entries to
3368 the end of the list, library dependencies appear after
3369 the library. Therefore search prior to the current
3370 LOOK, preventing possible infinite recursion. */
3371 || on_needed_list (elf_dt_name (look->by), needed, look)))
3377 /* Sort symbol by value, section, and size. */
3379 elf_sort_symbol (const void *arg1, const void *arg2)
3381 const struct elf_link_hash_entry *h1;
3382 const struct elf_link_hash_entry *h2;
3383 bfd_signed_vma vdiff;
3385 h1 = *(const struct elf_link_hash_entry **) arg1;
3386 h2 = *(const struct elf_link_hash_entry **) arg2;
3387 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3389 return vdiff > 0 ? 1 : -1;
3392 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3394 return sdiff > 0 ? 1 : -1;
3396 vdiff = h1->size - h2->size;
3397 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3400 /* This function is used to adjust offsets into .dynstr for
3401 dynamic symbols. This is called via elf_link_hash_traverse. */
3404 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3406 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3408 if (h->dynindx != -1)
3409 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3413 /* Assign string offsets in .dynstr, update all structures referencing
3417 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3419 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3420 struct elf_link_local_dynamic_entry *entry;
3421 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3422 bfd *dynobj = hash_table->dynobj;
3425 const struct elf_backend_data *bed;
3428 _bfd_elf_strtab_finalize (dynstr);
3429 size = _bfd_elf_strtab_size (dynstr);
3431 bed = get_elf_backend_data (dynobj);
3432 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3433 BFD_ASSERT (sdyn != NULL);
3435 /* Update all .dynamic entries referencing .dynstr strings. */
3436 for (extdyn = sdyn->contents;
3437 extdyn < sdyn->contents + sdyn->size;
3438 extdyn += bed->s->sizeof_dyn)
3440 Elf_Internal_Dyn dyn;
3442 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3446 dyn.d_un.d_val = size;
3456 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3461 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3464 /* Now update local dynamic symbols. */
3465 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3466 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3467 entry->isym.st_name);
3469 /* And the rest of dynamic symbols. */
3470 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3472 /* Adjust version definitions. */
3473 if (elf_tdata (output_bfd)->cverdefs)
3478 Elf_Internal_Verdef def;
3479 Elf_Internal_Verdaux defaux;
3481 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3485 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3487 p += sizeof (Elf_External_Verdef);
3488 if (def.vd_aux != sizeof (Elf_External_Verdef))
3490 for (i = 0; i < def.vd_cnt; ++i)
3492 _bfd_elf_swap_verdaux_in (output_bfd,
3493 (Elf_External_Verdaux *) p, &defaux);
3494 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3496 _bfd_elf_swap_verdaux_out (output_bfd,
3497 &defaux, (Elf_External_Verdaux *) p);
3498 p += sizeof (Elf_External_Verdaux);
3501 while (def.vd_next);
3504 /* Adjust version references. */
3505 if (elf_tdata (output_bfd)->verref)
3510 Elf_Internal_Verneed need;
3511 Elf_Internal_Vernaux needaux;
3513 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3517 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3519 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3520 _bfd_elf_swap_verneed_out (output_bfd, &need,
3521 (Elf_External_Verneed *) p);
3522 p += sizeof (Elf_External_Verneed);
3523 for (i = 0; i < need.vn_cnt; ++i)
3525 _bfd_elf_swap_vernaux_in (output_bfd,
3526 (Elf_External_Vernaux *) p, &needaux);
3527 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3529 _bfd_elf_swap_vernaux_out (output_bfd,
3531 (Elf_External_Vernaux *) p);
3532 p += sizeof (Elf_External_Vernaux);
3535 while (need.vn_next);
3541 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3542 The default is to only match when the INPUT and OUTPUT are exactly
3546 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3547 const bfd_target *output)
3549 return input == output;
3552 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3553 This version is used when different targets for the same architecture
3554 are virtually identical. */
3557 _bfd_elf_relocs_compatible (const bfd_target *input,
3558 const bfd_target *output)
3560 const struct elf_backend_data *obed, *ibed;
3562 if (input == output)
3565 ibed = xvec_get_elf_backend_data (input);
3566 obed = xvec_get_elf_backend_data (output);
3568 if (ibed->arch != obed->arch)
3571 /* If both backends are using this function, deem them compatible. */
3572 return ibed->relocs_compatible == obed->relocs_compatible;
3575 /* Make a special call to the linker "notice" function to tell it that
3576 we are about to handle an as-needed lib, or have finished
3577 processing the lib. */
3580 _bfd_elf_notice_as_needed (bfd *ibfd,
3581 struct bfd_link_info *info,
3582 enum notice_asneeded_action act)
3584 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3587 /* Check relocations an ELF object file. */
3590 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3592 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3593 struct elf_link_hash_table *htab = elf_hash_table (info);
3595 /* If this object is the same format as the output object, and it is
3596 not a shared library, then let the backend look through the
3599 This is required to build global offset table entries and to
3600 arrange for dynamic relocs. It is not required for the
3601 particular common case of linking non PIC code, even when linking
3602 against shared libraries, but unfortunately there is no way of
3603 knowing whether an object file has been compiled PIC or not.
3604 Looking through the relocs is not particularly time consuming.
3605 The problem is that we must either (1) keep the relocs in memory,
3606 which causes the linker to require additional runtime memory or
3607 (2) read the relocs twice from the input file, which wastes time.
3608 This would be a good case for using mmap.
3610 I have no idea how to handle linking PIC code into a file of a
3611 different format. It probably can't be done. */
3612 if ((abfd->flags & DYNAMIC) == 0
3613 && is_elf_hash_table (htab)
3614 && bed->check_relocs != NULL
3615 && elf_object_id (abfd) == elf_hash_table_id (htab)
3616 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3620 for (o = abfd->sections; o != NULL; o = o->next)
3622 Elf_Internal_Rela *internal_relocs;
3625 /* Don't check relocations in excluded sections. */
3626 if ((o->flags & SEC_RELOC) == 0
3627 || (o->flags & SEC_EXCLUDE) != 0
3628 || o->reloc_count == 0
3629 || ((info->strip == strip_all || info->strip == strip_debugger)
3630 && (o->flags & SEC_DEBUGGING) != 0)
3631 || bfd_is_abs_section (o->output_section))
3634 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3636 if (internal_relocs == NULL)
3639 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3641 if (elf_section_data (o)->relocs != internal_relocs)
3642 free (internal_relocs);
3652 /* Add symbols from an ELF object file to the linker hash table. */
3655 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3657 Elf_Internal_Ehdr *ehdr;
3658 Elf_Internal_Shdr *hdr;
3662 struct elf_link_hash_entry **sym_hash;
3663 bfd_boolean dynamic;
3664 Elf_External_Versym *extversym = NULL;
3665 Elf_External_Versym *ever;
3666 struct elf_link_hash_entry *weaks;
3667 struct elf_link_hash_entry **nondeflt_vers = NULL;
3668 size_t nondeflt_vers_cnt = 0;
3669 Elf_Internal_Sym *isymbuf = NULL;
3670 Elf_Internal_Sym *isym;
3671 Elf_Internal_Sym *isymend;
3672 const struct elf_backend_data *bed;
3673 bfd_boolean add_needed;
3674 struct elf_link_hash_table *htab;
3676 void *alloc_mark = NULL;
3677 struct bfd_hash_entry **old_table = NULL;
3678 unsigned int old_size = 0;
3679 unsigned int old_count = 0;
3680 void *old_tab = NULL;
3682 struct bfd_link_hash_entry *old_undefs = NULL;
3683 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3684 void *old_strtab = NULL;
3687 bfd_boolean just_syms;
3689 htab = elf_hash_table (info);
3690 bed = get_elf_backend_data (abfd);
3692 if ((abfd->flags & DYNAMIC) == 0)
3698 /* You can't use -r against a dynamic object. Also, there's no
3699 hope of using a dynamic object which does not exactly match
3700 the format of the output file. */
3701 if (bfd_link_relocatable (info)
3702 || !is_elf_hash_table (htab)
3703 || info->output_bfd->xvec != abfd->xvec)
3705 if (bfd_link_relocatable (info))
3706 bfd_set_error (bfd_error_invalid_operation);
3708 bfd_set_error (bfd_error_wrong_format);
3713 ehdr = elf_elfheader (abfd);
3714 if (info->warn_alternate_em
3715 && bed->elf_machine_code != ehdr->e_machine
3716 && ((bed->elf_machine_alt1 != 0
3717 && ehdr->e_machine == bed->elf_machine_alt1)
3718 || (bed->elf_machine_alt2 != 0
3719 && ehdr->e_machine == bed->elf_machine_alt2)))
3720 info->callbacks->einfo
3721 /* xgettext:c-format */
3722 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3723 ehdr->e_machine, abfd, bed->elf_machine_code);
3725 /* As a GNU extension, any input sections which are named
3726 .gnu.warning.SYMBOL are treated as warning symbols for the given
3727 symbol. This differs from .gnu.warning sections, which generate
3728 warnings when they are included in an output file. */
3729 /* PR 12761: Also generate this warning when building shared libraries. */
3730 for (s = abfd->sections; s != NULL; s = s->next)
3734 name = bfd_get_section_name (abfd, s);
3735 if (CONST_STRNEQ (name, ".gnu.warning."))
3740 name += sizeof ".gnu.warning." - 1;
3742 /* If this is a shared object, then look up the symbol
3743 in the hash table. If it is there, and it is already
3744 been defined, then we will not be using the entry
3745 from this shared object, so we don't need to warn.
3746 FIXME: If we see the definition in a regular object
3747 later on, we will warn, but we shouldn't. The only
3748 fix is to keep track of what warnings we are supposed
3749 to emit, and then handle them all at the end of the
3753 struct elf_link_hash_entry *h;
3755 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3757 /* FIXME: What about bfd_link_hash_common? */
3759 && (h->root.type == bfd_link_hash_defined
3760 || h->root.type == bfd_link_hash_defweak))
3765 msg = (char *) bfd_alloc (abfd, sz + 1);
3769 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3774 if (! (_bfd_generic_link_add_one_symbol
3775 (info, abfd, name, BSF_WARNING, s, 0, msg,
3776 FALSE, bed->collect, NULL)))
3779 if (bfd_link_executable (info))
3781 /* Clobber the section size so that the warning does
3782 not get copied into the output file. */
3785 /* Also set SEC_EXCLUDE, so that symbols defined in
3786 the warning section don't get copied to the output. */
3787 s->flags |= SEC_EXCLUDE;
3792 just_syms = ((s = abfd->sections) != NULL
3793 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3798 /* If we are creating a shared library, create all the dynamic
3799 sections immediately. We need to attach them to something,
3800 so we attach them to this BFD, provided it is the right
3801 format and is not from ld --just-symbols. Always create the
3802 dynamic sections for -E/--dynamic-list. FIXME: If there
3803 are no input BFD's of the same format as the output, we can't
3804 make a shared library. */
3806 && (bfd_link_pic (info)
3807 || (!bfd_link_relocatable (info)
3808 && (info->export_dynamic || info->dynamic)))
3809 && is_elf_hash_table (htab)
3810 && info->output_bfd->xvec == abfd->xvec
3811 && !htab->dynamic_sections_created)
3813 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3817 else if (!is_elf_hash_table (htab))
3821 const char *soname = NULL;
3823 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3824 const Elf_Internal_Phdr *phdr;
3827 /* ld --just-symbols and dynamic objects don't mix very well.
3828 ld shouldn't allow it. */
3832 /* If this dynamic lib was specified on the command line with
3833 --as-needed in effect, then we don't want to add a DT_NEEDED
3834 tag unless the lib is actually used. Similary for libs brought
3835 in by another lib's DT_NEEDED. When --no-add-needed is used
3836 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3837 any dynamic library in DT_NEEDED tags in the dynamic lib at
3839 add_needed = (elf_dyn_lib_class (abfd)
3840 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3841 | DYN_NO_NEEDED)) == 0;
3843 s = bfd_get_section_by_name (abfd, ".dynamic");
3848 unsigned int elfsec;
3849 unsigned long shlink;
3851 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3858 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3859 if (elfsec == SHN_BAD)
3860 goto error_free_dyn;
3861 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3863 for (extdyn = dynbuf;
3864 extdyn < dynbuf + s->size;
3865 extdyn += bed->s->sizeof_dyn)
3867 Elf_Internal_Dyn dyn;
3869 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3870 if (dyn.d_tag == DT_SONAME)
3872 unsigned int tagv = dyn.d_un.d_val;
3873 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3875 goto error_free_dyn;
3877 if (dyn.d_tag == DT_NEEDED)
3879 struct bfd_link_needed_list *n, **pn;
3881 unsigned int tagv = dyn.d_un.d_val;
3883 amt = sizeof (struct bfd_link_needed_list);
3884 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3885 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3886 if (n == NULL || fnm == NULL)
3887 goto error_free_dyn;
3888 amt = strlen (fnm) + 1;
3889 anm = (char *) bfd_alloc (abfd, amt);
3891 goto error_free_dyn;
3892 memcpy (anm, fnm, amt);
3896 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3900 if (dyn.d_tag == DT_RUNPATH)
3902 struct bfd_link_needed_list *n, **pn;
3904 unsigned int tagv = dyn.d_un.d_val;
3906 amt = sizeof (struct bfd_link_needed_list);
3907 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3908 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3909 if (n == NULL || fnm == NULL)
3910 goto error_free_dyn;
3911 amt = strlen (fnm) + 1;
3912 anm = (char *) bfd_alloc (abfd, amt);
3914 goto error_free_dyn;
3915 memcpy (anm, fnm, amt);
3919 for (pn = & runpath;
3925 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3926 if (!runpath && dyn.d_tag == DT_RPATH)
3928 struct bfd_link_needed_list *n, **pn;
3930 unsigned int tagv = dyn.d_un.d_val;
3932 amt = sizeof (struct bfd_link_needed_list);
3933 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3934 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3935 if (n == NULL || fnm == NULL)
3936 goto error_free_dyn;
3937 amt = strlen (fnm) + 1;
3938 anm = (char *) bfd_alloc (abfd, amt);
3940 goto error_free_dyn;
3941 memcpy (anm, fnm, amt);
3951 if (dyn.d_tag == DT_AUDIT)
3953 unsigned int tagv = dyn.d_un.d_val;
3954 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3961 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3962 frees all more recently bfd_alloc'd blocks as well. */
3968 struct bfd_link_needed_list **pn;
3969 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3974 /* If we have a PT_GNU_RELRO program header, mark as read-only
3975 all sections contained fully therein. This makes relro
3976 shared library sections appear as they will at run-time. */
3977 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
3978 while (--phdr >= elf_tdata (abfd)->phdr)
3979 if (phdr->p_type == PT_GNU_RELRO)
3981 for (s = abfd->sections; s != NULL; s = s->next)
3982 if ((s->flags & SEC_ALLOC) != 0
3983 && s->vma >= phdr->p_vaddr
3984 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
3985 s->flags |= SEC_READONLY;
3989 /* We do not want to include any of the sections in a dynamic
3990 object in the output file. We hack by simply clobbering the
3991 list of sections in the BFD. This could be handled more
3992 cleanly by, say, a new section flag; the existing
3993 SEC_NEVER_LOAD flag is not the one we want, because that one
3994 still implies that the section takes up space in the output
3996 bfd_section_list_clear (abfd);
3998 /* Find the name to use in a DT_NEEDED entry that refers to this
3999 object. If the object has a DT_SONAME entry, we use it.
4000 Otherwise, if the generic linker stuck something in
4001 elf_dt_name, we use that. Otherwise, we just use the file
4003 if (soname == NULL || *soname == '\0')
4005 soname = elf_dt_name (abfd);
4006 if (soname == NULL || *soname == '\0')
4007 soname = bfd_get_filename (abfd);
4010 /* Save the SONAME because sometimes the linker emulation code
4011 will need to know it. */
4012 elf_dt_name (abfd) = soname;
4014 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4018 /* If we have already included this dynamic object in the
4019 link, just ignore it. There is no reason to include a
4020 particular dynamic object more than once. */
4024 /* Save the DT_AUDIT entry for the linker emulation code. */
4025 elf_dt_audit (abfd) = audit;
4028 /* If this is a dynamic object, we always link against the .dynsym
4029 symbol table, not the .symtab symbol table. The dynamic linker
4030 will only see the .dynsym symbol table, so there is no reason to
4031 look at .symtab for a dynamic object. */
4033 if (! dynamic || elf_dynsymtab (abfd) == 0)
4034 hdr = &elf_tdata (abfd)->symtab_hdr;
4036 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4038 symcount = hdr->sh_size / bed->s->sizeof_sym;
4040 /* The sh_info field of the symtab header tells us where the
4041 external symbols start. We don't care about the local symbols at
4043 if (elf_bad_symtab (abfd))
4045 extsymcount = symcount;
4050 extsymcount = symcount - hdr->sh_info;
4051 extsymoff = hdr->sh_info;
4054 sym_hash = elf_sym_hashes (abfd);
4055 if (extsymcount != 0)
4057 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4059 if (isymbuf == NULL)
4062 if (sym_hash == NULL)
4064 /* We store a pointer to the hash table entry for each
4067 amt *= sizeof (struct elf_link_hash_entry *);
4068 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4069 if (sym_hash == NULL)
4070 goto error_free_sym;
4071 elf_sym_hashes (abfd) = sym_hash;
4077 /* Read in any version definitions. */
4078 if (!_bfd_elf_slurp_version_tables (abfd,
4079 info->default_imported_symver))
4080 goto error_free_sym;
4082 /* Read in the symbol versions, but don't bother to convert them
4083 to internal format. */
4084 if (elf_dynversym (abfd) != 0)
4086 Elf_Internal_Shdr *versymhdr;
4088 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4089 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
4090 if (extversym == NULL)
4091 goto error_free_sym;
4092 amt = versymhdr->sh_size;
4093 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4094 || bfd_bread (extversym, amt, abfd) != amt)
4095 goto error_free_vers;
4099 /* If we are loading an as-needed shared lib, save the symbol table
4100 state before we start adding symbols. If the lib turns out
4101 to be unneeded, restore the state. */
4102 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4107 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4109 struct bfd_hash_entry *p;
4110 struct elf_link_hash_entry *h;
4112 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4114 h = (struct elf_link_hash_entry *) p;
4115 entsize += htab->root.table.entsize;
4116 if (h->root.type == bfd_link_hash_warning)
4117 entsize += htab->root.table.entsize;
4121 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4122 old_tab = bfd_malloc (tabsize + entsize);
4123 if (old_tab == NULL)
4124 goto error_free_vers;
4126 /* Remember the current objalloc pointer, so that all mem for
4127 symbols added can later be reclaimed. */
4128 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4129 if (alloc_mark == NULL)
4130 goto error_free_vers;
4132 /* Make a special call to the linker "notice" function to
4133 tell it that we are about to handle an as-needed lib. */
4134 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4135 goto error_free_vers;
4137 /* Clone the symbol table. Remember some pointers into the
4138 symbol table, and dynamic symbol count. */
4139 old_ent = (char *) old_tab + tabsize;
4140 memcpy (old_tab, htab->root.table.table, tabsize);
4141 old_undefs = htab->root.undefs;
4142 old_undefs_tail = htab->root.undefs_tail;
4143 old_table = htab->root.table.table;
4144 old_size = htab->root.table.size;
4145 old_count = htab->root.table.count;
4146 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4147 if (old_strtab == NULL)
4148 goto error_free_vers;
4150 for (i = 0; i < htab->root.table.size; i++)
4152 struct bfd_hash_entry *p;
4153 struct elf_link_hash_entry *h;
4155 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4157 memcpy (old_ent, p, htab->root.table.entsize);
4158 old_ent = (char *) old_ent + htab->root.table.entsize;
4159 h = (struct elf_link_hash_entry *) p;
4160 if (h->root.type == bfd_link_hash_warning)
4162 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4163 old_ent = (char *) old_ent + htab->root.table.entsize;
4170 ever = extversym != NULL ? extversym + extsymoff : NULL;
4171 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4173 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4177 asection *sec, *new_sec;
4180 struct elf_link_hash_entry *h;
4181 struct elf_link_hash_entry *hi;
4182 bfd_boolean definition;
4183 bfd_boolean size_change_ok;
4184 bfd_boolean type_change_ok;
4185 bfd_boolean new_weakdef;
4186 bfd_boolean new_weak;
4187 bfd_boolean old_weak;
4188 bfd_boolean override;
4190 bfd_boolean discarded;
4191 unsigned int old_alignment;
4193 bfd_boolean matched;
4197 flags = BSF_NO_FLAGS;
4199 value = isym->st_value;
4200 common = bed->common_definition (isym);
4203 bind = ELF_ST_BIND (isym->st_info);
4207 /* This should be impossible, since ELF requires that all
4208 global symbols follow all local symbols, and that sh_info
4209 point to the first global symbol. Unfortunately, Irix 5
4214 if (isym->st_shndx != SHN_UNDEF && !common)
4222 case STB_GNU_UNIQUE:
4223 flags = BSF_GNU_UNIQUE;
4227 /* Leave it up to the processor backend. */
4231 if (isym->st_shndx == SHN_UNDEF)
4232 sec = bfd_und_section_ptr;
4233 else if (isym->st_shndx == SHN_ABS)
4234 sec = bfd_abs_section_ptr;
4235 else if (isym->st_shndx == SHN_COMMON)
4237 sec = bfd_com_section_ptr;
4238 /* What ELF calls the size we call the value. What ELF
4239 calls the value we call the alignment. */
4240 value = isym->st_size;
4244 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4246 sec = bfd_abs_section_ptr;
4247 else if (discarded_section (sec))
4249 /* Symbols from discarded section are undefined. We keep
4251 sec = bfd_und_section_ptr;
4253 isym->st_shndx = SHN_UNDEF;
4255 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4259 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4262 goto error_free_vers;
4264 if (isym->st_shndx == SHN_COMMON
4265 && (abfd->flags & BFD_PLUGIN) != 0)
4267 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4271 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4273 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4275 goto error_free_vers;
4279 else if (isym->st_shndx == SHN_COMMON
4280 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4281 && !bfd_link_relocatable (info))
4283 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4287 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4288 | SEC_LINKER_CREATED);
4289 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4291 goto error_free_vers;
4295 else if (bed->elf_add_symbol_hook)
4297 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4299 goto error_free_vers;
4301 /* The hook function sets the name to NULL if this symbol
4302 should be skipped for some reason. */
4307 /* Sanity check that all possibilities were handled. */
4310 bfd_set_error (bfd_error_bad_value);
4311 goto error_free_vers;
4314 /* Silently discard TLS symbols from --just-syms. There's
4315 no way to combine a static TLS block with a new TLS block
4316 for this executable. */
4317 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4318 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4321 if (bfd_is_und_section (sec)
4322 || bfd_is_com_section (sec))
4327 size_change_ok = FALSE;
4328 type_change_ok = bed->type_change_ok;
4335 if (is_elf_hash_table (htab))
4337 Elf_Internal_Versym iver;
4338 unsigned int vernum = 0;
4343 if (info->default_imported_symver)
4344 /* Use the default symbol version created earlier. */
4345 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4350 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4352 vernum = iver.vs_vers & VERSYM_VERSION;
4354 /* If this is a hidden symbol, or if it is not version
4355 1, we append the version name to the symbol name.
4356 However, we do not modify a non-hidden absolute symbol
4357 if it is not a function, because it might be the version
4358 symbol itself. FIXME: What if it isn't? */
4359 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4361 && (!bfd_is_abs_section (sec)
4362 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4365 size_t namelen, verlen, newlen;
4368 if (isym->st_shndx != SHN_UNDEF)
4370 if (vernum > elf_tdata (abfd)->cverdefs)
4372 else if (vernum > 1)
4374 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4381 /* xgettext:c-format */
4382 (_("%B: %s: invalid version %u (max %d)"),
4384 elf_tdata (abfd)->cverdefs);
4385 bfd_set_error (bfd_error_bad_value);
4386 goto error_free_vers;
4391 /* We cannot simply test for the number of
4392 entries in the VERNEED section since the
4393 numbers for the needed versions do not start
4395 Elf_Internal_Verneed *t;
4398 for (t = elf_tdata (abfd)->verref;
4402 Elf_Internal_Vernaux *a;
4404 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4406 if (a->vna_other == vernum)
4408 verstr = a->vna_nodename;
4418 /* xgettext:c-format */
4419 (_("%B: %s: invalid needed version %d"),
4420 abfd, name, vernum);
4421 bfd_set_error (bfd_error_bad_value);
4422 goto error_free_vers;
4426 namelen = strlen (name);
4427 verlen = strlen (verstr);
4428 newlen = namelen + verlen + 2;
4429 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4430 && isym->st_shndx != SHN_UNDEF)
4433 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4434 if (newname == NULL)
4435 goto error_free_vers;
4436 memcpy (newname, name, namelen);
4437 p = newname + namelen;
4439 /* If this is a defined non-hidden version symbol,
4440 we add another @ to the name. This indicates the
4441 default version of the symbol. */
4442 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4443 && isym->st_shndx != SHN_UNDEF)
4445 memcpy (p, verstr, verlen + 1);
4450 /* If this symbol has default visibility and the user has
4451 requested we not re-export it, then mark it as hidden. */
4452 if (!bfd_is_und_section (sec)
4455 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4456 isym->st_other = (STV_HIDDEN
4457 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4459 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4460 sym_hash, &old_bfd, &old_weak,
4461 &old_alignment, &skip, &override,
4462 &type_change_ok, &size_change_ok,
4464 goto error_free_vers;
4469 /* Override a definition only if the new symbol matches the
4471 if (override && matched)
4475 while (h->root.type == bfd_link_hash_indirect
4476 || h->root.type == bfd_link_hash_warning)
4477 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4479 if (elf_tdata (abfd)->verdef != NULL
4482 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4485 if (! (_bfd_generic_link_add_one_symbol
4486 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4487 (struct bfd_link_hash_entry **) sym_hash)))
4488 goto error_free_vers;
4490 if ((flags & BSF_GNU_UNIQUE)
4491 && (abfd->flags & DYNAMIC) == 0
4492 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4493 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique;
4496 /* We need to make sure that indirect symbol dynamic flags are
4499 while (h->root.type == bfd_link_hash_indirect
4500 || h->root.type == bfd_link_hash_warning)
4501 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4503 /* Setting the index to -3 tells elf_link_output_extsym that
4504 this symbol is defined in a discarded section. */
4510 new_weak = (flags & BSF_WEAK) != 0;
4511 new_weakdef = FALSE;
4515 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4516 && is_elf_hash_table (htab)
4517 && h->u.weakdef == NULL)
4519 /* Keep a list of all weak defined non function symbols from
4520 a dynamic object, using the weakdef field. Later in this
4521 function we will set the weakdef field to the correct
4522 value. We only put non-function symbols from dynamic
4523 objects on this list, because that happens to be the only
4524 time we need to know the normal symbol corresponding to a
4525 weak symbol, and the information is time consuming to
4526 figure out. If the weakdef field is not already NULL,
4527 then this symbol was already defined by some previous
4528 dynamic object, and we will be using that previous
4529 definition anyhow. */
4531 h->u.weakdef = weaks;
4536 /* Set the alignment of a common symbol. */
4537 if ((common || bfd_is_com_section (sec))
4538 && h->root.type == bfd_link_hash_common)
4543 align = bfd_log2 (isym->st_value);
4546 /* The new symbol is a common symbol in a shared object.
4547 We need to get the alignment from the section. */
4548 align = new_sec->alignment_power;
4550 if (align > old_alignment)
4551 h->root.u.c.p->alignment_power = align;
4553 h->root.u.c.p->alignment_power = old_alignment;
4556 if (is_elf_hash_table (htab))
4558 /* Set a flag in the hash table entry indicating the type of
4559 reference or definition we just found. A dynamic symbol
4560 is one which is referenced or defined by both a regular
4561 object and a shared object. */
4562 bfd_boolean dynsym = FALSE;
4564 /* Plugin symbols aren't normal. Don't set def_regular or
4565 ref_regular for them, or make them dynamic. */
4566 if ((abfd->flags & BFD_PLUGIN) != 0)
4573 if (bind != STB_WEAK)
4574 h->ref_regular_nonweak = 1;
4586 /* If the indirect symbol has been forced local, don't
4587 make the real symbol dynamic. */
4588 if ((h == hi || !hi->forced_local)
4589 && (bfd_link_dll (info)
4599 hi->ref_dynamic = 1;
4604 hi->def_dynamic = 1;
4607 /* If the indirect symbol has been forced local, don't
4608 make the real symbol dynamic. */
4609 if ((h == hi || !hi->forced_local)
4612 || (h->u.weakdef != NULL
4614 && h->u.weakdef->dynindx != -1)))
4618 /* Check to see if we need to add an indirect symbol for
4619 the default name. */
4621 || (!override && h->root.type == bfd_link_hash_common))
4622 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4623 sec, value, &old_bfd, &dynsym))
4624 goto error_free_vers;
4626 /* Check the alignment when a common symbol is involved. This
4627 can change when a common symbol is overridden by a normal
4628 definition or a common symbol is ignored due to the old
4629 normal definition. We need to make sure the maximum
4630 alignment is maintained. */
4631 if ((old_alignment || common)
4632 && h->root.type != bfd_link_hash_common)
4634 unsigned int common_align;
4635 unsigned int normal_align;
4636 unsigned int symbol_align;
4640 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4641 || h->root.type == bfd_link_hash_defweak);
4643 symbol_align = ffs (h->root.u.def.value) - 1;
4644 if (h->root.u.def.section->owner != NULL
4645 && (h->root.u.def.section->owner->flags
4646 & (DYNAMIC | BFD_PLUGIN)) == 0)
4648 normal_align = h->root.u.def.section->alignment_power;
4649 if (normal_align > symbol_align)
4650 normal_align = symbol_align;
4653 normal_align = symbol_align;
4657 common_align = old_alignment;
4658 common_bfd = old_bfd;
4663 common_align = bfd_log2 (isym->st_value);
4665 normal_bfd = old_bfd;
4668 if (normal_align < common_align)
4670 /* PR binutils/2735 */
4671 if (normal_bfd == NULL)
4673 /* xgettext:c-format */
4674 (_("Warning: alignment %u of common symbol `%s' in %B is"
4675 " greater than the alignment (%u) of its section %A"),
4676 common_bfd, h->root.u.def.section,
4677 1 << common_align, name, 1 << normal_align);
4680 /* xgettext:c-format */
4681 (_("Warning: alignment %u of symbol `%s' in %B"
4682 " is smaller than %u in %B"),
4683 normal_bfd, common_bfd,
4684 1 << normal_align, name, 1 << common_align);
4688 /* Remember the symbol size if it isn't undefined. */
4689 if (isym->st_size != 0
4690 && isym->st_shndx != SHN_UNDEF
4691 && (definition || h->size == 0))
4694 && h->size != isym->st_size
4695 && ! size_change_ok)
4697 /* xgettext:c-format */
4698 (_("Warning: size of symbol `%s' changed"
4699 " from %lu in %B to %lu in %B"),
4701 name, (unsigned long) h->size,
4702 (unsigned long) isym->st_size);
4704 h->size = isym->st_size;
4707 /* If this is a common symbol, then we always want H->SIZE
4708 to be the size of the common symbol. The code just above
4709 won't fix the size if a common symbol becomes larger. We
4710 don't warn about a size change here, because that is
4711 covered by --warn-common. Allow changes between different
4713 if (h->root.type == bfd_link_hash_common)
4714 h->size = h->root.u.c.size;
4716 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4717 && ((definition && !new_weak)
4718 || (old_weak && h->root.type == bfd_link_hash_common)
4719 || h->type == STT_NOTYPE))
4721 unsigned int type = ELF_ST_TYPE (isym->st_info);
4723 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4725 if (type == STT_GNU_IFUNC
4726 && (abfd->flags & DYNAMIC) != 0)
4729 if (h->type != type)
4731 if (h->type != STT_NOTYPE && ! type_change_ok)
4732 /* xgettext:c-format */
4734 (_("Warning: type of symbol `%s' changed"
4735 " from %d to %d in %B"),
4736 abfd, name, h->type, type);
4742 /* Merge st_other field. */
4743 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4745 /* We don't want to make debug symbol dynamic. */
4747 && (sec->flags & SEC_DEBUGGING)
4748 && !bfd_link_relocatable (info))
4751 /* Nor should we make plugin symbols dynamic. */
4752 if ((abfd->flags & BFD_PLUGIN) != 0)
4757 h->target_internal = isym->st_target_internal;
4758 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4761 if (definition && !dynamic)
4763 char *p = strchr (name, ELF_VER_CHR);
4764 if (p != NULL && p[1] != ELF_VER_CHR)
4766 /* Queue non-default versions so that .symver x, x@FOO
4767 aliases can be checked. */
4770 amt = ((isymend - isym + 1)
4771 * sizeof (struct elf_link_hash_entry *));
4773 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4775 goto error_free_vers;
4777 nondeflt_vers[nondeflt_vers_cnt++] = h;
4781 if (dynsym && h->dynindx == -1)
4783 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4784 goto error_free_vers;
4785 if (h->u.weakdef != NULL
4787 && h->u.weakdef->dynindx == -1)
4789 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4790 goto error_free_vers;
4793 else if (h->dynindx != -1)
4794 /* If the symbol already has a dynamic index, but
4795 visibility says it should not be visible, turn it into
4797 switch (ELF_ST_VISIBILITY (h->other))
4801 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4806 /* Don't add DT_NEEDED for references from the dummy bfd nor
4807 for unmatched symbol. */
4812 && h->ref_regular_nonweak
4814 || (old_bfd->flags & BFD_PLUGIN) == 0))
4815 || (h->ref_dynamic_nonweak
4816 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4817 && !on_needed_list (elf_dt_name (abfd),
4818 htab->needed, NULL))))
4821 const char *soname = elf_dt_name (abfd);
4823 info->callbacks->minfo ("%!", soname, old_bfd,
4824 h->root.root.string);
4826 /* A symbol from a library loaded via DT_NEEDED of some
4827 other library is referenced by a regular object.
4828 Add a DT_NEEDED entry for it. Issue an error if
4829 --no-add-needed is used and the reference was not
4832 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4835 /* xgettext:c-format */
4836 (_("%B: undefined reference to symbol '%s'"),
4838 bfd_set_error (bfd_error_missing_dso);
4839 goto error_free_vers;
4842 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4843 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4846 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4848 goto error_free_vers;
4850 BFD_ASSERT (ret == 0);
4855 if (extversym != NULL)
4861 if (isymbuf != NULL)
4867 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4871 /* Restore the symbol table. */
4872 old_ent = (char *) old_tab + tabsize;
4873 memset (elf_sym_hashes (abfd), 0,
4874 extsymcount * sizeof (struct elf_link_hash_entry *));
4875 htab->root.table.table = old_table;
4876 htab->root.table.size = old_size;
4877 htab->root.table.count = old_count;
4878 memcpy (htab->root.table.table, old_tab, tabsize);
4879 htab->root.undefs = old_undefs;
4880 htab->root.undefs_tail = old_undefs_tail;
4881 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
4884 for (i = 0; i < htab->root.table.size; i++)
4886 struct bfd_hash_entry *p;
4887 struct elf_link_hash_entry *h;
4889 unsigned int alignment_power;
4891 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4893 h = (struct elf_link_hash_entry *) p;
4894 if (h->root.type == bfd_link_hash_warning)
4895 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4897 /* Preserve the maximum alignment and size for common
4898 symbols even if this dynamic lib isn't on DT_NEEDED
4899 since it can still be loaded at run time by another
4901 if (h->root.type == bfd_link_hash_common)
4903 size = h->root.u.c.size;
4904 alignment_power = h->root.u.c.p->alignment_power;
4909 alignment_power = 0;
4911 memcpy (p, old_ent, htab->root.table.entsize);
4912 old_ent = (char *) old_ent + htab->root.table.entsize;
4913 h = (struct elf_link_hash_entry *) p;
4914 if (h->root.type == bfd_link_hash_warning)
4916 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4917 old_ent = (char *) old_ent + htab->root.table.entsize;
4918 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4920 if (h->root.type == bfd_link_hash_common)
4922 if (size > h->root.u.c.size)
4923 h->root.u.c.size = size;
4924 if (alignment_power > h->root.u.c.p->alignment_power)
4925 h->root.u.c.p->alignment_power = alignment_power;
4930 /* Make a special call to the linker "notice" function to
4931 tell it that symbols added for crefs may need to be removed. */
4932 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4933 goto error_free_vers;
4936 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4938 if (nondeflt_vers != NULL)
4939 free (nondeflt_vers);
4943 if (old_tab != NULL)
4945 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4946 goto error_free_vers;
4951 /* Now that all the symbols from this input file are created, if
4952 not performing a relocatable link, handle .symver foo, foo@BAR
4953 such that any relocs against foo become foo@BAR. */
4954 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4958 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4960 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4961 char *shortname, *p;
4963 p = strchr (h->root.root.string, ELF_VER_CHR);
4965 || (h->root.type != bfd_link_hash_defined
4966 && h->root.type != bfd_link_hash_defweak))
4969 amt = p - h->root.root.string;
4970 shortname = (char *) bfd_malloc (amt + 1);
4972 goto error_free_vers;
4973 memcpy (shortname, h->root.root.string, amt);
4974 shortname[amt] = '\0';
4976 hi = (struct elf_link_hash_entry *)
4977 bfd_link_hash_lookup (&htab->root, shortname,
4978 FALSE, FALSE, FALSE);
4980 && hi->root.type == h->root.type
4981 && hi->root.u.def.value == h->root.u.def.value
4982 && hi->root.u.def.section == h->root.u.def.section)
4984 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4985 hi->root.type = bfd_link_hash_indirect;
4986 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4987 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4988 sym_hash = elf_sym_hashes (abfd);
4990 for (symidx = 0; symidx < extsymcount; ++symidx)
4991 if (sym_hash[symidx] == hi)
4993 sym_hash[symidx] = h;
4999 free (nondeflt_vers);
5000 nondeflt_vers = NULL;
5003 /* Now set the weakdefs field correctly for all the weak defined
5004 symbols we found. The only way to do this is to search all the
5005 symbols. Since we only need the information for non functions in
5006 dynamic objects, that's the only time we actually put anything on
5007 the list WEAKS. We need this information so that if a regular
5008 object refers to a symbol defined weakly in a dynamic object, the
5009 real symbol in the dynamic object is also put in the dynamic
5010 symbols; we also must arrange for both symbols to point to the
5011 same memory location. We could handle the general case of symbol
5012 aliasing, but a general symbol alias can only be generated in
5013 assembler code, handling it correctly would be very time
5014 consuming, and other ELF linkers don't handle general aliasing
5018 struct elf_link_hash_entry **hpp;
5019 struct elf_link_hash_entry **hppend;
5020 struct elf_link_hash_entry **sorted_sym_hash;
5021 struct elf_link_hash_entry *h;
5024 /* Since we have to search the whole symbol list for each weak
5025 defined symbol, search time for N weak defined symbols will be
5026 O(N^2). Binary search will cut it down to O(NlogN). */
5028 amt *= sizeof (struct elf_link_hash_entry *);
5029 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5030 if (sorted_sym_hash == NULL)
5032 sym_hash = sorted_sym_hash;
5033 hpp = elf_sym_hashes (abfd);
5034 hppend = hpp + extsymcount;
5036 for (; hpp < hppend; hpp++)
5040 && h->root.type == bfd_link_hash_defined
5041 && !bed->is_function_type (h->type))
5049 qsort (sorted_sym_hash, sym_count,
5050 sizeof (struct elf_link_hash_entry *),
5053 while (weaks != NULL)
5055 struct elf_link_hash_entry *hlook;
5058 size_t i, j, idx = 0;
5061 weaks = hlook->u.weakdef;
5062 hlook->u.weakdef = NULL;
5064 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
5065 || hlook->root.type == bfd_link_hash_defweak
5066 || hlook->root.type == bfd_link_hash_common
5067 || hlook->root.type == bfd_link_hash_indirect);
5068 slook = hlook->root.u.def.section;
5069 vlook = hlook->root.u.def.value;
5075 bfd_signed_vma vdiff;
5077 h = sorted_sym_hash[idx];
5078 vdiff = vlook - h->root.u.def.value;
5085 int sdiff = slook->id - h->root.u.def.section->id;
5095 /* We didn't find a value/section match. */
5099 /* With multiple aliases, or when the weak symbol is already
5100 strongly defined, we have multiple matching symbols and
5101 the binary search above may land on any of them. Step
5102 one past the matching symbol(s). */
5105 h = sorted_sym_hash[idx];
5106 if (h->root.u.def.section != slook
5107 || h->root.u.def.value != vlook)
5111 /* Now look back over the aliases. Since we sorted by size
5112 as well as value and section, we'll choose the one with
5113 the largest size. */
5116 h = sorted_sym_hash[idx];
5118 /* Stop if value or section doesn't match. */
5119 if (h->root.u.def.section != slook
5120 || h->root.u.def.value != vlook)
5122 else if (h != hlook)
5124 hlook->u.weakdef = h;
5126 /* If the weak definition is in the list of dynamic
5127 symbols, make sure the real definition is put
5129 if (hlook->dynindx != -1 && h->dynindx == -1)
5131 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5134 free (sorted_sym_hash);
5139 /* If the real definition is in the list of dynamic
5140 symbols, make sure the weak definition is put
5141 there as well. If we don't do this, then the
5142 dynamic loader might not merge the entries for the
5143 real definition and the weak definition. */
5144 if (h->dynindx != -1 && hlook->dynindx == -1)
5146 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5147 goto err_free_sym_hash;
5154 free (sorted_sym_hash);
5157 if (bed->check_directives
5158 && !(*bed->check_directives) (abfd, info))
5161 if (!info->check_relocs_after_open_input
5162 && !_bfd_elf_link_check_relocs (abfd, info))
5165 /* If this is a non-traditional link, try to optimize the handling
5166 of the .stab/.stabstr sections. */
5168 && ! info->traditional_format
5169 && is_elf_hash_table (htab)
5170 && (info->strip != strip_all && info->strip != strip_debugger))
5174 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5175 if (stabstr != NULL)
5177 bfd_size_type string_offset = 0;
5180 for (stab = abfd->sections; stab; stab = stab->next)
5181 if (CONST_STRNEQ (stab->name, ".stab")
5182 && (!stab->name[5] ||
5183 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5184 && (stab->flags & SEC_MERGE) == 0
5185 && !bfd_is_abs_section (stab->output_section))
5187 struct bfd_elf_section_data *secdata;
5189 secdata = elf_section_data (stab);
5190 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5191 stabstr, &secdata->sec_info,
5194 if (secdata->sec_info)
5195 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5200 if (is_elf_hash_table (htab) && add_needed)
5202 /* Add this bfd to the loaded list. */
5203 struct elf_link_loaded_list *n;
5205 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5209 n->next = htab->loaded;
5216 if (old_tab != NULL)
5218 if (old_strtab != NULL)
5220 if (nondeflt_vers != NULL)
5221 free (nondeflt_vers);
5222 if (extversym != NULL)
5225 if (isymbuf != NULL)
5231 /* Return the linker hash table entry of a symbol that might be
5232 satisfied by an archive symbol. Return -1 on error. */
5234 struct elf_link_hash_entry *
5235 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5236 struct bfd_link_info *info,
5239 struct elf_link_hash_entry *h;
5243 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5247 /* If this is a default version (the name contains @@), look up the
5248 symbol again with only one `@' as well as without the version.
5249 The effect is that references to the symbol with and without the
5250 version will be matched by the default symbol in the archive. */
5252 p = strchr (name, ELF_VER_CHR);
5253 if (p == NULL || p[1] != ELF_VER_CHR)
5256 /* First check with only one `@'. */
5257 len = strlen (name);
5258 copy = (char *) bfd_alloc (abfd, len);
5260 return (struct elf_link_hash_entry *) 0 - 1;
5262 first = p - name + 1;
5263 memcpy (copy, name, first);
5264 memcpy (copy + first, name + first + 1, len - first);
5266 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5269 /* We also need to check references to the symbol without the
5271 copy[first - 1] = '\0';
5272 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5273 FALSE, FALSE, TRUE);
5276 bfd_release (abfd, copy);
5280 /* Add symbols from an ELF archive file to the linker hash table. We
5281 don't use _bfd_generic_link_add_archive_symbols because we need to
5282 handle versioned symbols.
5284 Fortunately, ELF archive handling is simpler than that done by
5285 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5286 oddities. In ELF, if we find a symbol in the archive map, and the
5287 symbol is currently undefined, we know that we must pull in that
5290 Unfortunately, we do have to make multiple passes over the symbol
5291 table until nothing further is resolved. */
5294 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5297 unsigned char *included = NULL;
5301 const struct elf_backend_data *bed;
5302 struct elf_link_hash_entry * (*archive_symbol_lookup)
5303 (bfd *, struct bfd_link_info *, const char *);
5305 if (! bfd_has_map (abfd))
5307 /* An empty archive is a special case. */
5308 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5310 bfd_set_error (bfd_error_no_armap);
5314 /* Keep track of all symbols we know to be already defined, and all
5315 files we know to be already included. This is to speed up the
5316 second and subsequent passes. */
5317 c = bfd_ardata (abfd)->symdef_count;
5321 amt *= sizeof (*included);
5322 included = (unsigned char *) bfd_zmalloc (amt);
5323 if (included == NULL)
5326 symdefs = bfd_ardata (abfd)->symdefs;
5327 bed = get_elf_backend_data (abfd);
5328 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5341 symdefend = symdef + c;
5342 for (i = 0; symdef < symdefend; symdef++, i++)
5344 struct elf_link_hash_entry *h;
5346 struct bfd_link_hash_entry *undefs_tail;
5351 if (symdef->file_offset == last)
5357 h = archive_symbol_lookup (abfd, info, symdef->name);
5358 if (h == (struct elf_link_hash_entry *) 0 - 1)
5364 if (h->root.type == bfd_link_hash_common)
5366 /* We currently have a common symbol. The archive map contains
5367 a reference to this symbol, so we may want to include it. We
5368 only want to include it however, if this archive element
5369 contains a definition of the symbol, not just another common
5372 Unfortunately some archivers (including GNU ar) will put
5373 declarations of common symbols into their archive maps, as
5374 well as real definitions, so we cannot just go by the archive
5375 map alone. Instead we must read in the element's symbol
5376 table and check that to see what kind of symbol definition
5378 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5381 else if (h->root.type != bfd_link_hash_undefined)
5383 if (h->root.type != bfd_link_hash_undefweak)
5384 /* Symbol must be defined. Don't check it again. */
5389 /* We need to include this archive member. */
5390 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5391 if (element == NULL)
5394 if (! bfd_check_format (element, bfd_object))
5397 undefs_tail = info->hash->undefs_tail;
5399 if (!(*info->callbacks
5400 ->add_archive_element) (info, element, symdef->name, &element))
5402 if (!bfd_link_add_symbols (element, info))
5405 /* If there are any new undefined symbols, we need to make
5406 another pass through the archive in order to see whether
5407 they can be defined. FIXME: This isn't perfect, because
5408 common symbols wind up on undefs_tail and because an
5409 undefined symbol which is defined later on in this pass
5410 does not require another pass. This isn't a bug, but it
5411 does make the code less efficient than it could be. */
5412 if (undefs_tail != info->hash->undefs_tail)
5415 /* Look backward to mark all symbols from this object file
5416 which we have already seen in this pass. */
5420 included[mark] = TRUE;
5425 while (symdefs[mark].file_offset == symdef->file_offset);
5427 /* We mark subsequent symbols from this object file as we go
5428 on through the loop. */
5429 last = symdef->file_offset;
5439 if (included != NULL)
5444 /* Given an ELF BFD, add symbols to the global hash table as
5448 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5450 switch (bfd_get_format (abfd))
5453 return elf_link_add_object_symbols (abfd, info);
5455 return elf_link_add_archive_symbols (abfd, info);
5457 bfd_set_error (bfd_error_wrong_format);
5462 struct hash_codes_info
5464 unsigned long *hashcodes;
5468 /* This function will be called though elf_link_hash_traverse to store
5469 all hash value of the exported symbols in an array. */
5472 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5474 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5479 /* Ignore indirect symbols. These are added by the versioning code. */
5480 if (h->dynindx == -1)
5483 name = h->root.root.string;
5484 if (h->versioned >= versioned)
5486 char *p = strchr (name, ELF_VER_CHR);
5489 alc = (char *) bfd_malloc (p - name + 1);
5495 memcpy (alc, name, p - name);
5496 alc[p - name] = '\0';
5501 /* Compute the hash value. */
5502 ha = bfd_elf_hash (name);
5504 /* Store the found hash value in the array given as the argument. */
5505 *(inf->hashcodes)++ = ha;
5507 /* And store it in the struct so that we can put it in the hash table
5509 h->u.elf_hash_value = ha;
5517 struct collect_gnu_hash_codes
5520 const struct elf_backend_data *bed;
5521 unsigned long int nsyms;
5522 unsigned long int maskbits;
5523 unsigned long int *hashcodes;
5524 unsigned long int *hashval;
5525 unsigned long int *indx;
5526 unsigned long int *counts;
5529 long int min_dynindx;
5530 unsigned long int bucketcount;
5531 unsigned long int symindx;
5532 long int local_indx;
5533 long int shift1, shift2;
5534 unsigned long int mask;
5538 /* This function will be called though elf_link_hash_traverse to store
5539 all hash value of the exported symbols in an array. */
5542 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5544 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5549 /* Ignore indirect symbols. These are added by the versioning code. */
5550 if (h->dynindx == -1)
5553 /* Ignore also local symbols and undefined symbols. */
5554 if (! (*s->bed->elf_hash_symbol) (h))
5557 name = h->root.root.string;
5558 if (h->versioned >= versioned)
5560 char *p = strchr (name, ELF_VER_CHR);
5563 alc = (char *) bfd_malloc (p - name + 1);
5569 memcpy (alc, name, p - name);
5570 alc[p - name] = '\0';
5575 /* Compute the hash value. */
5576 ha = bfd_elf_gnu_hash (name);
5578 /* Store the found hash value in the array for compute_bucket_count,
5579 and also for .dynsym reordering purposes. */
5580 s->hashcodes[s->nsyms] = ha;
5581 s->hashval[h->dynindx] = ha;
5583 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5584 s->min_dynindx = h->dynindx;
5592 /* This function will be called though elf_link_hash_traverse to do
5593 final dynaminc symbol renumbering. */
5596 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5598 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5599 unsigned long int bucket;
5600 unsigned long int val;
5602 /* Ignore indirect symbols. */
5603 if (h->dynindx == -1)
5606 /* Ignore also local symbols and undefined symbols. */
5607 if (! (*s->bed->elf_hash_symbol) (h))
5609 if (h->dynindx >= s->min_dynindx)
5610 h->dynindx = s->local_indx++;
5614 bucket = s->hashval[h->dynindx] % s->bucketcount;
5615 val = (s->hashval[h->dynindx] >> s->shift1)
5616 & ((s->maskbits >> s->shift1) - 1);
5617 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5619 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5620 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5621 if (s->counts[bucket] == 1)
5622 /* Last element terminates the chain. */
5624 bfd_put_32 (s->output_bfd, val,
5625 s->contents + (s->indx[bucket] - s->symindx) * 4);
5626 --s->counts[bucket];
5627 h->dynindx = s->indx[bucket]++;
5631 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5634 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5636 return !(h->forced_local
5637 || h->root.type == bfd_link_hash_undefined
5638 || h->root.type == bfd_link_hash_undefweak
5639 || ((h->root.type == bfd_link_hash_defined
5640 || h->root.type == bfd_link_hash_defweak)
5641 && h->root.u.def.section->output_section == NULL));
5644 /* Array used to determine the number of hash table buckets to use
5645 based on the number of symbols there are. If there are fewer than
5646 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5647 fewer than 37 we use 17 buckets, and so forth. We never use more
5648 than 32771 buckets. */
5650 static const size_t elf_buckets[] =
5652 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5656 /* Compute bucket count for hashing table. We do not use a static set
5657 of possible tables sizes anymore. Instead we determine for all
5658 possible reasonable sizes of the table the outcome (i.e., the
5659 number of collisions etc) and choose the best solution. The
5660 weighting functions are not too simple to allow the table to grow
5661 without bounds. Instead one of the weighting factors is the size.
5662 Therefore the result is always a good payoff between few collisions
5663 (= short chain lengths) and table size. */
5665 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5666 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5667 unsigned long int nsyms,
5670 size_t best_size = 0;
5671 unsigned long int i;
5673 /* We have a problem here. The following code to optimize the table
5674 size requires an integer type with more the 32 bits. If
5675 BFD_HOST_U_64_BIT is set we know about such a type. */
5676 #ifdef BFD_HOST_U_64_BIT
5681 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5682 bfd *dynobj = elf_hash_table (info)->dynobj;
5683 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5684 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5685 unsigned long int *counts;
5687 unsigned int no_improvement_count = 0;
5689 /* Possible optimization parameters: if we have NSYMS symbols we say
5690 that the hashing table must at least have NSYMS/4 and at most
5692 minsize = nsyms / 4;
5695 best_size = maxsize = nsyms * 2;
5700 if ((best_size & 31) == 0)
5704 /* Create array where we count the collisions in. We must use bfd_malloc
5705 since the size could be large. */
5707 amt *= sizeof (unsigned long int);
5708 counts = (unsigned long int *) bfd_malloc (amt);
5712 /* Compute the "optimal" size for the hash table. The criteria is a
5713 minimal chain length. The minor criteria is (of course) the size
5715 for (i = minsize; i < maxsize; ++i)
5717 /* Walk through the array of hashcodes and count the collisions. */
5718 BFD_HOST_U_64_BIT max;
5719 unsigned long int j;
5720 unsigned long int fact;
5722 if (gnu_hash && (i & 31) == 0)
5725 memset (counts, '\0', i * sizeof (unsigned long int));
5727 /* Determine how often each hash bucket is used. */
5728 for (j = 0; j < nsyms; ++j)
5729 ++counts[hashcodes[j] % i];
5731 /* For the weight function we need some information about the
5732 pagesize on the target. This is information need not be 100%
5733 accurate. Since this information is not available (so far) we
5734 define it here to a reasonable default value. If it is crucial
5735 to have a better value some day simply define this value. */
5736 # ifndef BFD_TARGET_PAGESIZE
5737 # define BFD_TARGET_PAGESIZE (4096)
5740 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5742 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5745 /* Variant 1: optimize for short chains. We add the squares
5746 of all the chain lengths (which favors many small chain
5747 over a few long chains). */
5748 for (j = 0; j < i; ++j)
5749 max += counts[j] * counts[j];
5751 /* This adds penalties for the overall size of the table. */
5752 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5755 /* Variant 2: Optimize a lot more for small table. Here we
5756 also add squares of the size but we also add penalties for
5757 empty slots (the +1 term). */
5758 for (j = 0; j < i; ++j)
5759 max += (1 + counts[j]) * (1 + counts[j]);
5761 /* The overall size of the table is considered, but not as
5762 strong as in variant 1, where it is squared. */
5763 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5767 /* Compare with current best results. */
5768 if (max < best_chlen)
5772 no_improvement_count = 0;
5774 /* PR 11843: Avoid futile long searches for the best bucket size
5775 when there are a large number of symbols. */
5776 else if (++no_improvement_count == 100)
5783 #endif /* defined (BFD_HOST_U_64_BIT) */
5785 /* This is the fallback solution if no 64bit type is available or if we
5786 are not supposed to spend much time on optimizations. We select the
5787 bucket count using a fixed set of numbers. */
5788 for (i = 0; elf_buckets[i] != 0; i++)
5790 best_size = elf_buckets[i];
5791 if (nsyms < elf_buckets[i + 1])
5794 if (gnu_hash && best_size < 2)
5801 /* Size any SHT_GROUP section for ld -r. */
5804 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5808 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5809 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5810 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5815 /* Set a default stack segment size. The value in INFO wins. If it
5816 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5817 undefined it is initialized. */
5820 bfd_elf_stack_segment_size (bfd *output_bfd,
5821 struct bfd_link_info *info,
5822 const char *legacy_symbol,
5823 bfd_vma default_size)
5825 struct elf_link_hash_entry *h = NULL;
5827 /* Look for legacy symbol. */
5829 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5830 FALSE, FALSE, FALSE);
5831 if (h && (h->root.type == bfd_link_hash_defined
5832 || h->root.type == bfd_link_hash_defweak)
5834 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5836 /* The symbol has no type if specified on the command line. */
5837 h->type = STT_OBJECT;
5838 if (info->stacksize)
5839 /* xgettext:c-format */
5840 _bfd_error_handler (_("%B: stack size specified and %s set"),
5841 output_bfd, legacy_symbol);
5842 else if (h->root.u.def.section != bfd_abs_section_ptr)
5843 /* xgettext:c-format */
5844 _bfd_error_handler (_("%B: %s not absolute"),
5845 output_bfd, legacy_symbol);
5847 info->stacksize = h->root.u.def.value;
5850 if (!info->stacksize)
5851 /* If the user didn't set a size, or explicitly inhibit the
5852 size, set it now. */
5853 info->stacksize = default_size;
5855 /* Provide the legacy symbol, if it is referenced. */
5856 if (h && (h->root.type == bfd_link_hash_undefined
5857 || h->root.type == bfd_link_hash_undefweak))
5859 struct bfd_link_hash_entry *bh = NULL;
5861 if (!(_bfd_generic_link_add_one_symbol
5862 (info, output_bfd, legacy_symbol,
5863 BSF_GLOBAL, bfd_abs_section_ptr,
5864 info->stacksize >= 0 ? info->stacksize : 0,
5865 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5868 h = (struct elf_link_hash_entry *) bh;
5870 h->type = STT_OBJECT;
5876 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
5878 struct elf_gc_sweep_symbol_info
5880 struct bfd_link_info *info;
5881 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
5886 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
5889 && (((h->root.type == bfd_link_hash_defined
5890 || h->root.type == bfd_link_hash_defweak)
5891 && !((h->def_regular || ELF_COMMON_DEF_P (h))
5892 && h->root.u.def.section->gc_mark))
5893 || h->root.type == bfd_link_hash_undefined
5894 || h->root.type == bfd_link_hash_undefweak))
5896 struct elf_gc_sweep_symbol_info *inf;
5898 inf = (struct elf_gc_sweep_symbol_info *) data;
5899 (*inf->hide_symbol) (inf->info, h, TRUE);
5902 h->ref_regular_nonweak = 0;
5908 /* Set up the sizes and contents of the ELF dynamic sections. This is
5909 called by the ELF linker emulation before_allocation routine. We
5910 must set the sizes of the sections before the linker sets the
5911 addresses of the various sections. */
5914 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5917 const char *filter_shlib,
5919 const char *depaudit,
5920 const char * const *auxiliary_filters,
5921 struct bfd_link_info *info,
5922 asection **sinterpptr)
5926 const struct elf_backend_data *bed;
5930 soname_indx = (size_t) -1;
5932 if (!is_elf_hash_table (info->hash))
5935 dynobj = elf_hash_table (info)->dynobj;
5937 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5939 struct bfd_elf_version_tree *verdefs;
5940 struct elf_info_failed asvinfo;
5941 struct bfd_elf_version_tree *t;
5942 struct bfd_elf_version_expr *d;
5943 struct elf_info_failed eif;
5944 bfd_boolean all_defined;
5950 /* If we are supposed to export all symbols into the dynamic symbol
5951 table (this is not the normal case), then do so. */
5952 if (info->export_dynamic
5953 || (bfd_link_executable (info) && info->dynamic))
5955 elf_link_hash_traverse (elf_hash_table (info),
5956 _bfd_elf_export_symbol,
5962 /* Make all global versions with definition. */
5963 for (t = info->version_info; t != NULL; t = t->next)
5964 for (d = t->globals.list; d != NULL; d = d->next)
5965 if (!d->symver && d->literal)
5967 const char *verstr, *name;
5968 size_t namelen, verlen, newlen;
5969 char *newname, *p, leading_char;
5970 struct elf_link_hash_entry *newh;
5972 leading_char = bfd_get_symbol_leading_char (output_bfd);
5974 namelen = strlen (name) + (leading_char != '\0');
5976 verlen = strlen (verstr);
5977 newlen = namelen + verlen + 3;
5979 newname = (char *) bfd_malloc (newlen);
5980 if (newname == NULL)
5982 newname[0] = leading_char;
5983 memcpy (newname + (leading_char != '\0'), name, namelen);
5985 /* Check the hidden versioned definition. */
5986 p = newname + namelen;
5988 memcpy (p, verstr, verlen + 1);
5989 newh = elf_link_hash_lookup (elf_hash_table (info),
5990 newname, FALSE, FALSE,
5993 || (newh->root.type != bfd_link_hash_defined
5994 && newh->root.type != bfd_link_hash_defweak))
5996 /* Check the default versioned definition. */
5998 memcpy (p, verstr, verlen + 1);
5999 newh = elf_link_hash_lookup (elf_hash_table (info),
6000 newname, FALSE, FALSE,
6005 /* Mark this version if there is a definition and it is
6006 not defined in a shared object. */
6008 && !newh->def_dynamic
6009 && (newh->root.type == bfd_link_hash_defined
6010 || newh->root.type == bfd_link_hash_defweak))
6014 /* Attach all the symbols to their version information. */
6015 asvinfo.info = info;
6016 asvinfo.failed = FALSE;
6018 elf_link_hash_traverse (elf_hash_table (info),
6019 _bfd_elf_link_assign_sym_version,
6024 if (!info->allow_undefined_version)
6026 /* Check if all global versions have a definition. */
6028 for (t = info->version_info; t != NULL; t = t->next)
6029 for (d = t->globals.list; d != NULL; d = d->next)
6030 if (d->literal && !d->symver && !d->script)
6033 (_("%s: undefined version: %s"),
6034 d->pattern, t->name);
6035 all_defined = FALSE;
6040 bfd_set_error (bfd_error_bad_value);
6045 /* Set up the version definition section. */
6046 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6047 BFD_ASSERT (s != NULL);
6049 /* We may have created additional version definitions if we are
6050 just linking a regular application. */
6051 verdefs = info->version_info;
6053 /* Skip anonymous version tag. */
6054 if (verdefs != NULL && verdefs->vernum == 0)
6055 verdefs = verdefs->next;
6057 if (verdefs == NULL && !info->create_default_symver)
6058 s->flags |= SEC_EXCLUDE;
6064 Elf_Internal_Verdef def;
6065 Elf_Internal_Verdaux defaux;
6066 struct bfd_link_hash_entry *bh;
6067 struct elf_link_hash_entry *h;
6073 /* Make space for the base version. */
6074 size += sizeof (Elf_External_Verdef);
6075 size += sizeof (Elf_External_Verdaux);
6078 /* Make space for the default version. */
6079 if (info->create_default_symver)
6081 size += sizeof (Elf_External_Verdef);
6085 for (t = verdefs; t != NULL; t = t->next)
6087 struct bfd_elf_version_deps *n;
6089 /* Don't emit base version twice. */
6093 size += sizeof (Elf_External_Verdef);
6094 size += sizeof (Elf_External_Verdaux);
6097 for (n = t->deps; n != NULL; n = n->next)
6098 size += sizeof (Elf_External_Verdaux);
6102 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6103 if (s->contents == NULL && s->size != 0)
6106 /* Fill in the version definition section. */
6110 def.vd_version = VER_DEF_CURRENT;
6111 def.vd_flags = VER_FLG_BASE;
6114 if (info->create_default_symver)
6116 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6117 def.vd_next = sizeof (Elf_External_Verdef);
6121 def.vd_aux = sizeof (Elf_External_Verdef);
6122 def.vd_next = (sizeof (Elf_External_Verdef)
6123 + sizeof (Elf_External_Verdaux));
6126 if (soname_indx != (size_t) -1)
6128 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6130 def.vd_hash = bfd_elf_hash (soname);
6131 defaux.vda_name = soname_indx;
6138 name = lbasename (output_bfd->filename);
6139 def.vd_hash = bfd_elf_hash (name);
6140 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6142 if (indx == (size_t) -1)
6144 defaux.vda_name = indx;
6146 defaux.vda_next = 0;
6148 _bfd_elf_swap_verdef_out (output_bfd, &def,
6149 (Elf_External_Verdef *) p);
6150 p += sizeof (Elf_External_Verdef);
6151 if (info->create_default_symver)
6153 /* Add a symbol representing this version. */
6155 if (! (_bfd_generic_link_add_one_symbol
6156 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6158 get_elf_backend_data (dynobj)->collect, &bh)))
6160 h = (struct elf_link_hash_entry *) bh;
6163 h->type = STT_OBJECT;
6164 h->verinfo.vertree = NULL;
6166 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6169 /* Create a duplicate of the base version with the same
6170 aux block, but different flags. */
6173 def.vd_aux = sizeof (Elf_External_Verdef);
6175 def.vd_next = (sizeof (Elf_External_Verdef)
6176 + sizeof (Elf_External_Verdaux));
6179 _bfd_elf_swap_verdef_out (output_bfd, &def,
6180 (Elf_External_Verdef *) p);
6181 p += sizeof (Elf_External_Verdef);
6183 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6184 (Elf_External_Verdaux *) p);
6185 p += sizeof (Elf_External_Verdaux);
6187 for (t = verdefs; t != NULL; t = t->next)
6190 struct bfd_elf_version_deps *n;
6192 /* Don't emit the base version twice. */
6197 for (n = t->deps; n != NULL; n = n->next)
6200 /* Add a symbol representing this version. */
6202 if (! (_bfd_generic_link_add_one_symbol
6203 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6205 get_elf_backend_data (dynobj)->collect, &bh)))
6207 h = (struct elf_link_hash_entry *) bh;
6210 h->type = STT_OBJECT;
6211 h->verinfo.vertree = t;
6213 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6216 def.vd_version = VER_DEF_CURRENT;
6218 if (t->globals.list == NULL
6219 && t->locals.list == NULL
6221 def.vd_flags |= VER_FLG_WEAK;
6222 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6223 def.vd_cnt = cdeps + 1;
6224 def.vd_hash = bfd_elf_hash (t->name);
6225 def.vd_aux = sizeof (Elf_External_Verdef);
6228 /* If a basever node is next, it *must* be the last node in
6229 the chain, otherwise Verdef construction breaks. */
6230 if (t->next != NULL && t->next->vernum == 0)
6231 BFD_ASSERT (t->next->next == NULL);
6233 if (t->next != NULL && t->next->vernum != 0)
6234 def.vd_next = (sizeof (Elf_External_Verdef)
6235 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6237 _bfd_elf_swap_verdef_out (output_bfd, &def,
6238 (Elf_External_Verdef *) p);
6239 p += sizeof (Elf_External_Verdef);
6241 defaux.vda_name = h->dynstr_index;
6242 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6244 defaux.vda_next = 0;
6245 if (t->deps != NULL)
6246 defaux.vda_next = sizeof (Elf_External_Verdaux);
6247 t->name_indx = defaux.vda_name;
6249 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6250 (Elf_External_Verdaux *) p);
6251 p += sizeof (Elf_External_Verdaux);
6253 for (n = t->deps; n != NULL; n = n->next)
6255 if (n->version_needed == NULL)
6257 /* This can happen if there was an error in the
6259 defaux.vda_name = 0;
6263 defaux.vda_name = n->version_needed->name_indx;
6264 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6267 if (n->next == NULL)
6268 defaux.vda_next = 0;
6270 defaux.vda_next = sizeof (Elf_External_Verdaux);
6272 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6273 (Elf_External_Verdaux *) p);
6274 p += sizeof (Elf_External_Verdaux);
6278 elf_tdata (output_bfd)->cverdefs = cdefs;
6281 /* Work out the size of the version reference section. */
6283 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6284 BFD_ASSERT (s != NULL);
6286 struct elf_find_verdep_info sinfo;
6289 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6290 if (sinfo.vers == 0)
6292 sinfo.failed = FALSE;
6294 elf_link_hash_traverse (elf_hash_table (info),
6295 _bfd_elf_link_find_version_dependencies,
6300 if (elf_tdata (output_bfd)->verref == NULL)
6301 s->flags |= SEC_EXCLUDE;
6304 Elf_Internal_Verneed *vn;
6309 /* Build the version dependency section. */
6312 for (vn = elf_tdata (output_bfd)->verref;
6314 vn = vn->vn_nextref)
6316 Elf_Internal_Vernaux *a;
6318 size += sizeof (Elf_External_Verneed);
6320 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6321 size += sizeof (Elf_External_Vernaux);
6325 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6326 if (s->contents == NULL)
6330 for (vn = elf_tdata (output_bfd)->verref;
6332 vn = vn->vn_nextref)
6335 Elf_Internal_Vernaux *a;
6339 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6342 vn->vn_version = VER_NEED_CURRENT;
6344 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6345 elf_dt_name (vn->vn_bfd) != NULL
6346 ? elf_dt_name (vn->vn_bfd)
6347 : lbasename (vn->vn_bfd->filename),
6349 if (indx == (size_t) -1)
6352 vn->vn_aux = sizeof (Elf_External_Verneed);
6353 if (vn->vn_nextref == NULL)
6356 vn->vn_next = (sizeof (Elf_External_Verneed)
6357 + caux * sizeof (Elf_External_Vernaux));
6359 _bfd_elf_swap_verneed_out (output_bfd, vn,
6360 (Elf_External_Verneed *) p);
6361 p += sizeof (Elf_External_Verneed);
6363 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6365 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6366 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6367 a->vna_nodename, FALSE);
6368 if (indx == (size_t) -1)
6371 if (a->vna_nextptr == NULL)
6374 a->vna_next = sizeof (Elf_External_Vernaux);
6376 _bfd_elf_swap_vernaux_out (output_bfd, a,
6377 (Elf_External_Vernaux *) p);
6378 p += sizeof (Elf_External_Vernaux);
6382 elf_tdata (output_bfd)->cverrefs = crefs;
6387 bed = get_elf_backend_data (output_bfd);
6389 if (info->gc_sections && bed->can_gc_sections)
6391 struct elf_gc_sweep_symbol_info sweep_info;
6392 unsigned long section_sym_count;
6394 /* Remove the symbols that were in the swept sections from the
6395 dynamic symbol table. GCFIXME: Anyone know how to get them
6396 out of the static symbol table as well? */
6397 sweep_info.info = info;
6398 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6399 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6402 _bfd_elf_link_renumber_dynsyms (output_bfd, info, §ion_sym_count);
6405 /* Any syms created from now on start with -1 in
6406 got.refcount/offset and plt.refcount/offset. */
6407 elf_hash_table (info)->init_got_refcount
6408 = elf_hash_table (info)->init_got_offset;
6409 elf_hash_table (info)->init_plt_refcount
6410 = elf_hash_table (info)->init_plt_offset;
6412 if (bfd_link_relocatable (info)
6413 && !_bfd_elf_size_group_sections (info))
6416 /* The backend may have to create some sections regardless of whether
6417 we're dynamic or not. */
6418 if (bed->elf_backend_always_size_sections
6419 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6422 /* Determine any GNU_STACK segment requirements, after the backend
6423 has had a chance to set a default segment size. */
6424 if (info->execstack)
6425 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6426 else if (info->noexecstack)
6427 elf_stack_flags (output_bfd) = PF_R | PF_W;
6431 asection *notesec = NULL;
6434 for (inputobj = info->input_bfds;
6436 inputobj = inputobj->link.next)
6441 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6443 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6446 if (s->flags & SEC_CODE)
6450 else if (bed->default_execstack)
6453 if (notesec || info->stacksize > 0)
6454 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6455 if (notesec && exec && bfd_link_relocatable (info)
6456 && notesec->output_section != bfd_abs_section_ptr)
6457 notesec->output_section->flags |= SEC_CODE;
6460 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6462 struct elf_info_failed eif;
6463 struct elf_link_hash_entry *h;
6467 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6468 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6472 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6474 if (soname_indx == (size_t) -1
6475 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6481 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6483 info->flags |= DF_SYMBOLIC;
6491 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6493 if (indx == (size_t) -1)
6496 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6497 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6501 if (filter_shlib != NULL)
6505 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6506 filter_shlib, TRUE);
6507 if (indx == (size_t) -1
6508 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6512 if (auxiliary_filters != NULL)
6514 const char * const *p;
6516 for (p = auxiliary_filters; *p != NULL; p++)
6520 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6522 if (indx == (size_t) -1
6523 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6532 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6534 if (indx == (size_t) -1
6535 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6539 if (depaudit != NULL)
6543 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6545 if (indx == (size_t) -1
6546 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6553 /* Find all symbols which were defined in a dynamic object and make
6554 the backend pick a reasonable value for them. */
6555 elf_link_hash_traverse (elf_hash_table (info),
6556 _bfd_elf_adjust_dynamic_symbol,
6561 /* Add some entries to the .dynamic section. We fill in some of the
6562 values later, in bfd_elf_final_link, but we must add the entries
6563 now so that we know the final size of the .dynamic section. */
6565 /* If there are initialization and/or finalization functions to
6566 call then add the corresponding DT_INIT/DT_FINI entries. */
6567 h = (info->init_function
6568 ? elf_link_hash_lookup (elf_hash_table (info),
6569 info->init_function, FALSE,
6576 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6579 h = (info->fini_function
6580 ? elf_link_hash_lookup (elf_hash_table (info),
6581 info->fini_function, FALSE,
6588 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6592 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6593 if (s != NULL && s->linker_has_input)
6595 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6596 if (! bfd_link_executable (info))
6601 for (sub = info->input_bfds; sub != NULL;
6602 sub = sub->link.next)
6603 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6604 for (o = sub->sections; o != NULL; o = o->next)
6605 if (elf_section_data (o)->this_hdr.sh_type
6606 == SHT_PREINIT_ARRAY)
6609 (_("%B: .preinit_array section is not allowed in DSO"),
6614 bfd_set_error (bfd_error_nonrepresentable_section);
6618 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6619 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6622 s = bfd_get_section_by_name (output_bfd, ".init_array");
6623 if (s != NULL && s->linker_has_input)
6625 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6626 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6629 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6630 if (s != NULL && s->linker_has_input)
6632 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6633 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6637 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6638 /* If .dynstr is excluded from the link, we don't want any of
6639 these tags. Strictly, we should be checking each section
6640 individually; This quick check covers for the case where
6641 someone does a /DISCARD/ : { *(*) }. */
6642 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6644 bfd_size_type strsize;
6646 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6647 if ((info->emit_hash
6648 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6649 || (info->emit_gnu_hash
6650 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6651 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6652 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6653 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6654 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6655 bed->s->sizeof_sym))
6660 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6663 /* The backend must work out the sizes of all the other dynamic
6666 && bed->elf_backend_size_dynamic_sections != NULL
6667 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6670 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6672 unsigned long section_sym_count;
6674 if (elf_tdata (output_bfd)->cverdefs)
6676 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
6678 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6679 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
6683 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6685 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6688 else if (info->flags & DF_BIND_NOW)
6690 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6696 if (bfd_link_executable (info))
6697 info->flags_1 &= ~ (DF_1_INITFIRST
6700 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6704 if (elf_tdata (output_bfd)->cverrefs)
6706 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
6708 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6709 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6713 if ((elf_tdata (output_bfd)->cverrefs == 0
6714 && elf_tdata (output_bfd)->cverdefs == 0)
6715 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6716 §ion_sym_count) == 0)
6720 s = bfd_get_linker_section (dynobj, ".gnu.version");
6721 s->flags |= SEC_EXCLUDE;
6727 /* Find the first non-excluded output section. We'll use its
6728 section symbol for some emitted relocs. */
6730 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6734 for (s = output_bfd->sections; s != NULL; s = s->next)
6735 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6736 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6738 elf_hash_table (info)->text_index_section = s;
6743 /* Find two non-excluded output sections, one for code, one for data.
6744 We'll use their section symbols for some emitted relocs. */
6746 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6750 /* Data first, since setting text_index_section changes
6751 _bfd_elf_link_omit_section_dynsym. */
6752 for (s = output_bfd->sections; s != NULL; s = s->next)
6753 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6754 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6756 elf_hash_table (info)->data_index_section = s;
6760 for (s = output_bfd->sections; s != NULL; s = s->next)
6761 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6762 == (SEC_ALLOC | SEC_READONLY))
6763 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6765 elf_hash_table (info)->text_index_section = s;
6769 if (elf_hash_table (info)->text_index_section == NULL)
6770 elf_hash_table (info)->text_index_section
6771 = elf_hash_table (info)->data_index_section;
6775 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6777 const struct elf_backend_data *bed;
6779 if (!is_elf_hash_table (info->hash))
6782 bed = get_elf_backend_data (output_bfd);
6783 (*bed->elf_backend_init_index_section) (output_bfd, info);
6785 if (elf_hash_table (info)->dynamic_sections_created)
6789 bfd_size_type dynsymcount;
6790 unsigned long section_sym_count;
6791 unsigned int dtagcount;
6793 dynobj = elf_hash_table (info)->dynobj;
6795 /* Assign dynsym indicies. In a shared library we generate a
6796 section symbol for each output section, which come first.
6797 Next come all of the back-end allocated local dynamic syms,
6798 followed by the rest of the global symbols. */
6800 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6801 §ion_sym_count);
6803 /* Work out the size of the symbol version section. */
6804 s = bfd_get_linker_section (dynobj, ".gnu.version");
6805 BFD_ASSERT (s != NULL);
6806 if ((s->flags & SEC_EXCLUDE) == 0)
6808 s->size = dynsymcount * sizeof (Elf_External_Versym);
6809 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6810 if (s->contents == NULL)
6813 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6817 /* Set the size of the .dynsym and .hash sections. We counted
6818 the number of dynamic symbols in elf_link_add_object_symbols.
6819 We will build the contents of .dynsym and .hash when we build
6820 the final symbol table, because until then we do not know the
6821 correct value to give the symbols. We built the .dynstr
6822 section as we went along in elf_link_add_object_symbols. */
6823 s = elf_hash_table (info)->dynsym;
6824 BFD_ASSERT (s != NULL);
6825 s->size = dynsymcount * bed->s->sizeof_sym;
6827 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6828 if (s->contents == NULL)
6831 /* The first entry in .dynsym is a dummy symbol. Clear all the
6832 section syms, in case we don't output them all. */
6833 ++section_sym_count;
6834 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6836 elf_hash_table (info)->bucketcount = 0;
6838 /* Compute the size of the hashing table. As a side effect this
6839 computes the hash values for all the names we export. */
6840 if (info->emit_hash)
6842 unsigned long int *hashcodes;
6843 struct hash_codes_info hashinf;
6845 unsigned long int nsyms;
6847 size_t hash_entry_size;
6849 /* Compute the hash values for all exported symbols. At the same
6850 time store the values in an array so that we could use them for
6852 amt = dynsymcount * sizeof (unsigned long int);
6853 hashcodes = (unsigned long int *) bfd_malloc (amt);
6854 if (hashcodes == NULL)
6856 hashinf.hashcodes = hashcodes;
6857 hashinf.error = FALSE;
6859 /* Put all hash values in HASHCODES. */
6860 elf_link_hash_traverse (elf_hash_table (info),
6861 elf_collect_hash_codes, &hashinf);
6868 nsyms = hashinf.hashcodes - hashcodes;
6870 = compute_bucket_count (info, hashcodes, nsyms, 0);
6873 if (bucketcount == 0)
6876 elf_hash_table (info)->bucketcount = bucketcount;
6878 s = bfd_get_linker_section (dynobj, ".hash");
6879 BFD_ASSERT (s != NULL);
6880 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6881 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6882 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6883 if (s->contents == NULL)
6886 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6887 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6888 s->contents + hash_entry_size);
6891 if (info->emit_gnu_hash)
6894 unsigned char *contents;
6895 struct collect_gnu_hash_codes cinfo;
6899 memset (&cinfo, 0, sizeof (cinfo));
6901 /* Compute the hash values for all exported symbols. At the same
6902 time store the values in an array so that we could use them for
6904 amt = dynsymcount * 2 * sizeof (unsigned long int);
6905 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6906 if (cinfo.hashcodes == NULL)
6909 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6910 cinfo.min_dynindx = -1;
6911 cinfo.output_bfd = output_bfd;
6914 /* Put all hash values in HASHCODES. */
6915 elf_link_hash_traverse (elf_hash_table (info),
6916 elf_collect_gnu_hash_codes, &cinfo);
6919 free (cinfo.hashcodes);
6924 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6926 if (bucketcount == 0)
6928 free (cinfo.hashcodes);
6932 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6933 BFD_ASSERT (s != NULL);
6935 if (cinfo.nsyms == 0)
6937 /* Empty .gnu.hash section is special. */
6938 BFD_ASSERT (cinfo.min_dynindx == -1);
6939 free (cinfo.hashcodes);
6940 s->size = 5 * 4 + bed->s->arch_size / 8;
6941 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6942 if (contents == NULL)
6944 s->contents = contents;
6945 /* 1 empty bucket. */
6946 bfd_put_32 (output_bfd, 1, contents);
6947 /* SYMIDX above the special symbol 0. */
6948 bfd_put_32 (output_bfd, 1, contents + 4);
6949 /* Just one word for bitmask. */
6950 bfd_put_32 (output_bfd, 1, contents + 8);
6951 /* Only hash fn bloom filter. */
6952 bfd_put_32 (output_bfd, 0, contents + 12);
6953 /* No hashes are valid - empty bitmask. */
6954 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6955 /* No hashes in the only bucket. */
6956 bfd_put_32 (output_bfd, 0,
6957 contents + 16 + bed->s->arch_size / 8);
6961 unsigned long int maskwords, maskbitslog2, x;
6962 BFD_ASSERT (cinfo.min_dynindx != -1);
6966 while ((x >>= 1) != 0)
6968 if (maskbitslog2 < 3)
6970 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6971 maskbitslog2 = maskbitslog2 + 3;
6973 maskbitslog2 = maskbitslog2 + 2;
6974 if (bed->s->arch_size == 64)
6976 if (maskbitslog2 == 5)
6982 cinfo.mask = (1 << cinfo.shift1) - 1;
6983 cinfo.shift2 = maskbitslog2;
6984 cinfo.maskbits = 1 << maskbitslog2;
6985 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6986 amt = bucketcount * sizeof (unsigned long int) * 2;
6987 amt += maskwords * sizeof (bfd_vma);
6988 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6989 if (cinfo.bitmask == NULL)
6991 free (cinfo.hashcodes);
6995 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6996 cinfo.indx = cinfo.counts + bucketcount;
6997 cinfo.symindx = dynsymcount - cinfo.nsyms;
6998 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7000 /* Determine how often each hash bucket is used. */
7001 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7002 for (i = 0; i < cinfo.nsyms; ++i)
7003 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7005 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7006 if (cinfo.counts[i] != 0)
7008 cinfo.indx[i] = cnt;
7009 cnt += cinfo.counts[i];
7011 BFD_ASSERT (cnt == dynsymcount);
7012 cinfo.bucketcount = bucketcount;
7013 cinfo.local_indx = cinfo.min_dynindx;
7015 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7016 s->size += cinfo.maskbits / 8;
7017 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7018 if (contents == NULL)
7020 free (cinfo.bitmask);
7021 free (cinfo.hashcodes);
7025 s->contents = contents;
7026 bfd_put_32 (output_bfd, bucketcount, contents);
7027 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7028 bfd_put_32 (output_bfd, maskwords, contents + 8);
7029 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7030 contents += 16 + cinfo.maskbits / 8;
7032 for (i = 0; i < bucketcount; ++i)
7034 if (cinfo.counts[i] == 0)
7035 bfd_put_32 (output_bfd, 0, contents);
7037 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7041 cinfo.contents = contents;
7043 /* Renumber dynamic symbols, populate .gnu.hash section. */
7044 elf_link_hash_traverse (elf_hash_table (info),
7045 elf_renumber_gnu_hash_syms, &cinfo);
7047 contents = s->contents + 16;
7048 for (i = 0; i < maskwords; ++i)
7050 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7052 contents += bed->s->arch_size / 8;
7055 free (cinfo.bitmask);
7056 free (cinfo.hashcodes);
7060 s = bfd_get_linker_section (dynobj, ".dynstr");
7061 BFD_ASSERT (s != NULL);
7063 elf_finalize_dynstr (output_bfd, info);
7065 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7067 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7068 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7075 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7078 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7081 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7082 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7085 /* Finish SHF_MERGE section merging. */
7088 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7093 if (!is_elf_hash_table (info->hash))
7096 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7097 if ((ibfd->flags & DYNAMIC) == 0
7098 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7099 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7100 == get_elf_backend_data (obfd)->s->elfclass))
7101 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7102 if ((sec->flags & SEC_MERGE) != 0
7103 && !bfd_is_abs_section (sec->output_section))
7105 struct bfd_elf_section_data *secdata;
7107 secdata = elf_section_data (sec);
7108 if (! _bfd_add_merge_section (obfd,
7109 &elf_hash_table (info)->merge_info,
7110 sec, &secdata->sec_info))
7112 else if (secdata->sec_info)
7113 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7116 if (elf_hash_table (info)->merge_info != NULL)
7117 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7118 merge_sections_remove_hook);
7122 /* Create an entry in an ELF linker hash table. */
7124 struct bfd_hash_entry *
7125 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7126 struct bfd_hash_table *table,
7129 /* Allocate the structure if it has not already been allocated by a
7133 entry = (struct bfd_hash_entry *)
7134 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7139 /* Call the allocation method of the superclass. */
7140 entry = _bfd_link_hash_newfunc (entry, table, string);
7143 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7144 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7146 /* Set local fields. */
7149 ret->got = htab->init_got_refcount;
7150 ret->plt = htab->init_plt_refcount;
7151 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7152 - offsetof (struct elf_link_hash_entry, size)));
7153 /* Assume that we have been called by a non-ELF symbol reader.
7154 This flag is then reset by the code which reads an ELF input
7155 file. This ensures that a symbol created by a non-ELF symbol
7156 reader will have the flag set correctly. */
7163 /* Copy data from an indirect symbol to its direct symbol, hiding the
7164 old indirect symbol. Also used for copying flags to a weakdef. */
7167 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7168 struct elf_link_hash_entry *dir,
7169 struct elf_link_hash_entry *ind)
7171 struct elf_link_hash_table *htab;
7173 /* Copy down any references that we may have already seen to the
7174 symbol which just became indirect. */
7176 if (dir->versioned != versioned_hidden)
7177 dir->ref_dynamic |= ind->ref_dynamic;
7178 dir->ref_regular |= ind->ref_regular;
7179 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7180 dir->non_got_ref |= ind->non_got_ref;
7181 dir->needs_plt |= ind->needs_plt;
7182 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7184 if (ind->root.type != bfd_link_hash_indirect)
7187 /* Copy over the global and procedure linkage table refcount entries.
7188 These may have been already set up by a check_relocs routine. */
7189 htab = elf_hash_table (info);
7190 if (ind->got.refcount > htab->init_got_refcount.refcount)
7192 if (dir->got.refcount < 0)
7193 dir->got.refcount = 0;
7194 dir->got.refcount += ind->got.refcount;
7195 ind->got.refcount = htab->init_got_refcount.refcount;
7198 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7200 if (dir->plt.refcount < 0)
7201 dir->plt.refcount = 0;
7202 dir->plt.refcount += ind->plt.refcount;
7203 ind->plt.refcount = htab->init_plt_refcount.refcount;
7206 if (ind->dynindx != -1)
7208 if (dir->dynindx != -1)
7209 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7210 dir->dynindx = ind->dynindx;
7211 dir->dynstr_index = ind->dynstr_index;
7213 ind->dynstr_index = 0;
7218 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7219 struct elf_link_hash_entry *h,
7220 bfd_boolean force_local)
7222 /* STT_GNU_IFUNC symbol must go through PLT. */
7223 if (h->type != STT_GNU_IFUNC)
7225 h->plt = elf_hash_table (info)->init_plt_offset;
7230 h->forced_local = 1;
7231 if (h->dynindx != -1)
7234 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7240 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7244 _bfd_elf_link_hash_table_init
7245 (struct elf_link_hash_table *table,
7247 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7248 struct bfd_hash_table *,
7250 unsigned int entsize,
7251 enum elf_target_id target_id)
7254 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7256 table->init_got_refcount.refcount = can_refcount - 1;
7257 table->init_plt_refcount.refcount = can_refcount - 1;
7258 table->init_got_offset.offset = -(bfd_vma) 1;
7259 table->init_plt_offset.offset = -(bfd_vma) 1;
7260 /* The first dynamic symbol is a dummy. */
7261 table->dynsymcount = 1;
7263 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7265 table->root.type = bfd_link_elf_hash_table;
7266 table->hash_table_id = target_id;
7271 /* Create an ELF linker hash table. */
7273 struct bfd_link_hash_table *
7274 _bfd_elf_link_hash_table_create (bfd *abfd)
7276 struct elf_link_hash_table *ret;
7277 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7279 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7283 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7284 sizeof (struct elf_link_hash_entry),
7290 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7295 /* Destroy an ELF linker hash table. */
7298 _bfd_elf_link_hash_table_free (bfd *obfd)
7300 struct elf_link_hash_table *htab;
7302 htab = (struct elf_link_hash_table *) obfd->link.hash;
7303 if (htab->dynstr != NULL)
7304 _bfd_elf_strtab_free (htab->dynstr);
7305 _bfd_merge_sections_free (htab->merge_info);
7306 _bfd_generic_link_hash_table_free (obfd);
7309 /* This is a hook for the ELF emulation code in the generic linker to
7310 tell the backend linker what file name to use for the DT_NEEDED
7311 entry for a dynamic object. */
7314 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7316 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7317 && bfd_get_format (abfd) == bfd_object)
7318 elf_dt_name (abfd) = name;
7322 bfd_elf_get_dyn_lib_class (bfd *abfd)
7325 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7326 && bfd_get_format (abfd) == bfd_object)
7327 lib_class = elf_dyn_lib_class (abfd);
7334 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7336 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7337 && bfd_get_format (abfd) == bfd_object)
7338 elf_dyn_lib_class (abfd) = lib_class;
7341 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7342 the linker ELF emulation code. */
7344 struct bfd_link_needed_list *
7345 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7346 struct bfd_link_info *info)
7348 if (! is_elf_hash_table (info->hash))
7350 return elf_hash_table (info)->needed;
7353 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7354 hook for the linker ELF emulation code. */
7356 struct bfd_link_needed_list *
7357 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7358 struct bfd_link_info *info)
7360 if (! is_elf_hash_table (info->hash))
7362 return elf_hash_table (info)->runpath;
7365 /* Get the name actually used for a dynamic object for a link. This
7366 is the SONAME entry if there is one. Otherwise, it is the string
7367 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7370 bfd_elf_get_dt_soname (bfd *abfd)
7372 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7373 && bfd_get_format (abfd) == bfd_object)
7374 return elf_dt_name (abfd);
7378 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7379 the ELF linker emulation code. */
7382 bfd_elf_get_bfd_needed_list (bfd *abfd,
7383 struct bfd_link_needed_list **pneeded)
7386 bfd_byte *dynbuf = NULL;
7387 unsigned int elfsec;
7388 unsigned long shlink;
7389 bfd_byte *extdyn, *extdynend;
7391 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7395 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7396 || bfd_get_format (abfd) != bfd_object)
7399 s = bfd_get_section_by_name (abfd, ".dynamic");
7400 if (s == NULL || s->size == 0)
7403 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7406 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7407 if (elfsec == SHN_BAD)
7410 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7412 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7413 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7416 extdynend = extdyn + s->size;
7417 for (; extdyn < extdynend; extdyn += extdynsize)
7419 Elf_Internal_Dyn dyn;
7421 (*swap_dyn_in) (abfd, extdyn, &dyn);
7423 if (dyn.d_tag == DT_NULL)
7426 if (dyn.d_tag == DT_NEEDED)
7429 struct bfd_link_needed_list *l;
7430 unsigned int tagv = dyn.d_un.d_val;
7433 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7438 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7459 struct elf_symbuf_symbol
7461 unsigned long st_name; /* Symbol name, index in string tbl */
7462 unsigned char st_info; /* Type and binding attributes */
7463 unsigned char st_other; /* Visibilty, and target specific */
7466 struct elf_symbuf_head
7468 struct elf_symbuf_symbol *ssym;
7470 unsigned int st_shndx;
7477 Elf_Internal_Sym *isym;
7478 struct elf_symbuf_symbol *ssym;
7483 /* Sort references to symbols by ascending section number. */
7486 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7488 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7489 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7491 return s1->st_shndx - s2->st_shndx;
7495 elf_sym_name_compare (const void *arg1, const void *arg2)
7497 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7498 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7499 return strcmp (s1->name, s2->name);
7502 static struct elf_symbuf_head *
7503 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7505 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7506 struct elf_symbuf_symbol *ssym;
7507 struct elf_symbuf_head *ssymbuf, *ssymhead;
7508 size_t i, shndx_count, total_size;
7510 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7514 for (ind = indbuf, i = 0; i < symcount; i++)
7515 if (isymbuf[i].st_shndx != SHN_UNDEF)
7516 *ind++ = &isymbuf[i];
7519 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7520 elf_sort_elf_symbol);
7523 if (indbufend > indbuf)
7524 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7525 if (ind[0]->st_shndx != ind[1]->st_shndx)
7528 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7529 + (indbufend - indbuf) * sizeof (*ssym));
7530 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7531 if (ssymbuf == NULL)
7537 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7538 ssymbuf->ssym = NULL;
7539 ssymbuf->count = shndx_count;
7540 ssymbuf->st_shndx = 0;
7541 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7543 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7546 ssymhead->ssym = ssym;
7547 ssymhead->count = 0;
7548 ssymhead->st_shndx = (*ind)->st_shndx;
7550 ssym->st_name = (*ind)->st_name;
7551 ssym->st_info = (*ind)->st_info;
7552 ssym->st_other = (*ind)->st_other;
7555 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7556 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7563 /* Check if 2 sections define the same set of local and global
7567 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7568 struct bfd_link_info *info)
7571 const struct elf_backend_data *bed1, *bed2;
7572 Elf_Internal_Shdr *hdr1, *hdr2;
7573 size_t symcount1, symcount2;
7574 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7575 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7576 Elf_Internal_Sym *isym, *isymend;
7577 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7578 size_t count1, count2, i;
7579 unsigned int shndx1, shndx2;
7585 /* Both sections have to be in ELF. */
7586 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7587 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7590 if (elf_section_type (sec1) != elf_section_type (sec2))
7593 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7594 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7595 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7598 bed1 = get_elf_backend_data (bfd1);
7599 bed2 = get_elf_backend_data (bfd2);
7600 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7601 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7602 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7603 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7605 if (symcount1 == 0 || symcount2 == 0)
7611 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7612 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7614 if (ssymbuf1 == NULL)
7616 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7618 if (isymbuf1 == NULL)
7621 if (!info->reduce_memory_overheads)
7622 elf_tdata (bfd1)->symbuf = ssymbuf1
7623 = elf_create_symbuf (symcount1, isymbuf1);
7626 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7628 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7630 if (isymbuf2 == NULL)
7633 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7634 elf_tdata (bfd2)->symbuf = ssymbuf2
7635 = elf_create_symbuf (symcount2, isymbuf2);
7638 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7640 /* Optimized faster version. */
7642 struct elf_symbol *symp;
7643 struct elf_symbuf_symbol *ssym, *ssymend;
7646 hi = ssymbuf1->count;
7651 mid = (lo + hi) / 2;
7652 if (shndx1 < ssymbuf1[mid].st_shndx)
7654 else if (shndx1 > ssymbuf1[mid].st_shndx)
7658 count1 = ssymbuf1[mid].count;
7665 hi = ssymbuf2->count;
7670 mid = (lo + hi) / 2;
7671 if (shndx2 < ssymbuf2[mid].st_shndx)
7673 else if (shndx2 > ssymbuf2[mid].st_shndx)
7677 count2 = ssymbuf2[mid].count;
7683 if (count1 == 0 || count2 == 0 || count1 != count2)
7687 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7689 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7690 if (symtable1 == NULL || symtable2 == NULL)
7694 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7695 ssym < ssymend; ssym++, symp++)
7697 symp->u.ssym = ssym;
7698 symp->name = bfd_elf_string_from_elf_section (bfd1,
7704 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7705 ssym < ssymend; ssym++, symp++)
7707 symp->u.ssym = ssym;
7708 symp->name = bfd_elf_string_from_elf_section (bfd2,
7713 /* Sort symbol by name. */
7714 qsort (symtable1, count1, sizeof (struct elf_symbol),
7715 elf_sym_name_compare);
7716 qsort (symtable2, count1, sizeof (struct elf_symbol),
7717 elf_sym_name_compare);
7719 for (i = 0; i < count1; i++)
7720 /* Two symbols must have the same binding, type and name. */
7721 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7722 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7723 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7730 symtable1 = (struct elf_symbol *)
7731 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7732 symtable2 = (struct elf_symbol *)
7733 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7734 if (symtable1 == NULL || symtable2 == NULL)
7737 /* Count definitions in the section. */
7739 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7740 if (isym->st_shndx == shndx1)
7741 symtable1[count1++].u.isym = isym;
7744 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7745 if (isym->st_shndx == shndx2)
7746 symtable2[count2++].u.isym = isym;
7748 if (count1 == 0 || count2 == 0 || count1 != count2)
7751 for (i = 0; i < count1; i++)
7753 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7754 symtable1[i].u.isym->st_name);
7756 for (i = 0; i < count2; i++)
7758 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7759 symtable2[i].u.isym->st_name);
7761 /* Sort symbol by name. */
7762 qsort (symtable1, count1, sizeof (struct elf_symbol),
7763 elf_sym_name_compare);
7764 qsort (symtable2, count1, sizeof (struct elf_symbol),
7765 elf_sym_name_compare);
7767 for (i = 0; i < count1; i++)
7768 /* Two symbols must have the same binding, type and name. */
7769 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7770 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7771 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7789 /* Return TRUE if 2 section types are compatible. */
7792 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7793 bfd *bbfd, const asection *bsec)
7797 || abfd->xvec->flavour != bfd_target_elf_flavour
7798 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7801 return elf_section_type (asec) == elf_section_type (bsec);
7804 /* Final phase of ELF linker. */
7806 /* A structure we use to avoid passing large numbers of arguments. */
7808 struct elf_final_link_info
7810 /* General link information. */
7811 struct bfd_link_info *info;
7814 /* Symbol string table. */
7815 struct elf_strtab_hash *symstrtab;
7816 /* .hash section. */
7818 /* symbol version section (.gnu.version). */
7819 asection *symver_sec;
7820 /* Buffer large enough to hold contents of any section. */
7822 /* Buffer large enough to hold external relocs of any section. */
7823 void *external_relocs;
7824 /* Buffer large enough to hold internal relocs of any section. */
7825 Elf_Internal_Rela *internal_relocs;
7826 /* Buffer large enough to hold external local symbols of any input
7828 bfd_byte *external_syms;
7829 /* And a buffer for symbol section indices. */
7830 Elf_External_Sym_Shndx *locsym_shndx;
7831 /* Buffer large enough to hold internal local symbols of any input
7833 Elf_Internal_Sym *internal_syms;
7834 /* Array large enough to hold a symbol index for each local symbol
7835 of any input BFD. */
7837 /* Array large enough to hold a section pointer for each local
7838 symbol of any input BFD. */
7839 asection **sections;
7840 /* Buffer for SHT_SYMTAB_SHNDX section. */
7841 Elf_External_Sym_Shndx *symshndxbuf;
7842 /* Number of STT_FILE syms seen. */
7843 size_t filesym_count;
7846 /* This struct is used to pass information to elf_link_output_extsym. */
7848 struct elf_outext_info
7851 bfd_boolean localsyms;
7852 bfd_boolean file_sym_done;
7853 struct elf_final_link_info *flinfo;
7857 /* Support for evaluating a complex relocation.
7859 Complex relocations are generalized, self-describing relocations. The
7860 implementation of them consists of two parts: complex symbols, and the
7861 relocations themselves.
7863 The relocations are use a reserved elf-wide relocation type code (R_RELC
7864 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7865 information (start bit, end bit, word width, etc) into the addend. This
7866 information is extracted from CGEN-generated operand tables within gas.
7868 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7869 internal) representing prefix-notation expressions, including but not
7870 limited to those sorts of expressions normally encoded as addends in the
7871 addend field. The symbol mangling format is:
7874 | <unary-operator> ':' <node>
7875 | <binary-operator> ':' <node> ':' <node>
7878 <literal> := 's' <digits=N> ':' <N character symbol name>
7879 | 'S' <digits=N> ':' <N character section name>
7883 <binary-operator> := as in C
7884 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7887 set_symbol_value (bfd *bfd_with_globals,
7888 Elf_Internal_Sym *isymbuf,
7893 struct elf_link_hash_entry **sym_hashes;
7894 struct elf_link_hash_entry *h;
7895 size_t extsymoff = locsymcount;
7897 if (symidx < locsymcount)
7899 Elf_Internal_Sym *sym;
7901 sym = isymbuf + symidx;
7902 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7904 /* It is a local symbol: move it to the
7905 "absolute" section and give it a value. */
7906 sym->st_shndx = SHN_ABS;
7907 sym->st_value = val;
7910 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7914 /* It is a global symbol: set its link type
7915 to "defined" and give it a value. */
7917 sym_hashes = elf_sym_hashes (bfd_with_globals);
7918 h = sym_hashes [symidx - extsymoff];
7919 while (h->root.type == bfd_link_hash_indirect
7920 || h->root.type == bfd_link_hash_warning)
7921 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7922 h->root.type = bfd_link_hash_defined;
7923 h->root.u.def.value = val;
7924 h->root.u.def.section = bfd_abs_section_ptr;
7928 resolve_symbol (const char *name,
7930 struct elf_final_link_info *flinfo,
7932 Elf_Internal_Sym *isymbuf,
7935 Elf_Internal_Sym *sym;
7936 struct bfd_link_hash_entry *global_entry;
7937 const char *candidate = NULL;
7938 Elf_Internal_Shdr *symtab_hdr;
7941 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7943 for (i = 0; i < locsymcount; ++ i)
7947 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7950 candidate = bfd_elf_string_from_elf_section (input_bfd,
7951 symtab_hdr->sh_link,
7954 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7955 name, candidate, (unsigned long) sym->st_value);
7957 if (candidate && strcmp (candidate, name) == 0)
7959 asection *sec = flinfo->sections [i];
7961 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7962 *result += sec->output_offset + sec->output_section->vma;
7964 printf ("Found symbol with value %8.8lx\n",
7965 (unsigned long) *result);
7971 /* Hmm, haven't found it yet. perhaps it is a global. */
7972 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7973 FALSE, FALSE, TRUE);
7977 if (global_entry->type == bfd_link_hash_defined
7978 || global_entry->type == bfd_link_hash_defweak)
7980 *result = (global_entry->u.def.value
7981 + global_entry->u.def.section->output_section->vma
7982 + global_entry->u.def.section->output_offset);
7984 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7985 global_entry->root.string, (unsigned long) *result);
7993 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
7994 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
7995 names like "foo.end" which is the end address of section "foo". */
7998 resolve_section (const char *name,
8006 for (curr = sections; curr; curr = curr->next)
8007 if (strcmp (curr->name, name) == 0)
8009 *result = curr->vma;
8013 /* Hmm. still haven't found it. try pseudo-section names. */
8014 /* FIXME: This could be coded more efficiently... */
8015 for (curr = sections; curr; curr = curr->next)
8017 len = strlen (curr->name);
8018 if (len > strlen (name))
8021 if (strncmp (curr->name, name, len) == 0)
8023 if (strncmp (".end", name + len, 4) == 0)
8025 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8029 /* Insert more pseudo-section names here, if you like. */
8037 undefined_reference (const char *reftype, const char *name)
8039 /* xgettext:c-format */
8040 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8045 eval_symbol (bfd_vma *result,
8048 struct elf_final_link_info *flinfo,
8050 Elf_Internal_Sym *isymbuf,
8059 const char *sym = *symp;
8061 bfd_boolean symbol_is_section = FALSE;
8066 if (len < 1 || len > sizeof (symbuf))
8068 bfd_set_error (bfd_error_invalid_operation);
8081 *result = strtoul (sym, (char **) symp, 16);
8085 symbol_is_section = TRUE;
8089 symlen = strtol (sym, (char **) symp, 10);
8090 sym = *symp + 1; /* Skip the trailing ':'. */
8092 if (symend < sym || symlen + 1 > sizeof (symbuf))
8094 bfd_set_error (bfd_error_invalid_operation);
8098 memcpy (symbuf, sym, symlen);
8099 symbuf[symlen] = '\0';
8100 *symp = sym + symlen;
8102 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8103 the symbol as a section, or vice-versa. so we're pretty liberal in our
8104 interpretation here; section means "try section first", not "must be a
8105 section", and likewise with symbol. */
8107 if (symbol_is_section)
8109 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8110 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8111 isymbuf, locsymcount))
8113 undefined_reference ("section", symbuf);
8119 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8120 isymbuf, locsymcount)
8121 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8124 undefined_reference ("symbol", symbuf);
8131 /* All that remains are operators. */
8133 #define UNARY_OP(op) \
8134 if (strncmp (sym, #op, strlen (#op)) == 0) \
8136 sym += strlen (#op); \
8140 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8141 isymbuf, locsymcount, signed_p)) \
8144 *result = op ((bfd_signed_vma) a); \
8150 #define BINARY_OP(op) \
8151 if (strncmp (sym, #op, strlen (#op)) == 0) \
8153 sym += strlen (#op); \
8157 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8158 isymbuf, locsymcount, signed_p)) \
8161 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8162 isymbuf, locsymcount, signed_p)) \
8165 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8195 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8196 bfd_set_error (bfd_error_invalid_operation);
8202 put_value (bfd_vma size,
8203 unsigned long chunksz,
8208 location += (size - chunksz);
8210 for (; size; size -= chunksz, location -= chunksz)
8215 bfd_put_8 (input_bfd, x, location);
8219 bfd_put_16 (input_bfd, x, location);
8223 bfd_put_32 (input_bfd, x, location);
8224 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8230 bfd_put_64 (input_bfd, x, location);
8231 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8244 get_value (bfd_vma size,
8245 unsigned long chunksz,
8252 /* Sanity checks. */
8253 BFD_ASSERT (chunksz <= sizeof (x)
8256 && (size % chunksz) == 0
8257 && input_bfd != NULL
8258 && location != NULL);
8260 if (chunksz == sizeof (x))
8262 BFD_ASSERT (size == chunksz);
8264 /* Make sure that we do not perform an undefined shift operation.
8265 We know that size == chunksz so there will only be one iteration
8266 of the loop below. */
8270 shift = 8 * chunksz;
8272 for (; size; size -= chunksz, location += chunksz)
8277 x = (x << shift) | bfd_get_8 (input_bfd, location);
8280 x = (x << shift) | bfd_get_16 (input_bfd, location);
8283 x = (x << shift) | bfd_get_32 (input_bfd, location);
8287 x = (x << shift) | bfd_get_64 (input_bfd, location);
8298 decode_complex_addend (unsigned long *start, /* in bits */
8299 unsigned long *oplen, /* in bits */
8300 unsigned long *len, /* in bits */
8301 unsigned long *wordsz, /* in bytes */
8302 unsigned long *chunksz, /* in bytes */
8303 unsigned long *lsb0_p,
8304 unsigned long *signed_p,
8305 unsigned long *trunc_p,
8306 unsigned long encoded)
8308 * start = encoded & 0x3F;
8309 * len = (encoded >> 6) & 0x3F;
8310 * oplen = (encoded >> 12) & 0x3F;
8311 * wordsz = (encoded >> 18) & 0xF;
8312 * chunksz = (encoded >> 22) & 0xF;
8313 * lsb0_p = (encoded >> 27) & 1;
8314 * signed_p = (encoded >> 28) & 1;
8315 * trunc_p = (encoded >> 29) & 1;
8318 bfd_reloc_status_type
8319 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8320 asection *input_section ATTRIBUTE_UNUSED,
8322 Elf_Internal_Rela *rel,
8325 bfd_vma shift, x, mask;
8326 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8327 bfd_reloc_status_type r;
8329 /* Perform this reloc, since it is complex.
8330 (this is not to say that it necessarily refers to a complex
8331 symbol; merely that it is a self-describing CGEN based reloc.
8332 i.e. the addend has the complete reloc information (bit start, end,
8333 word size, etc) encoded within it.). */
8335 decode_complex_addend (&start, &oplen, &len, &wordsz,
8336 &chunksz, &lsb0_p, &signed_p,
8337 &trunc_p, rel->r_addend);
8339 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8342 shift = (start + 1) - len;
8344 shift = (8 * wordsz) - (start + len);
8346 x = get_value (wordsz, chunksz, input_bfd,
8347 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8350 printf ("Doing complex reloc: "
8351 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8352 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8353 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8354 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8355 oplen, (unsigned long) x, (unsigned long) mask,
8356 (unsigned long) relocation);
8361 /* Now do an overflow check. */
8362 r = bfd_check_overflow ((signed_p
8363 ? complain_overflow_signed
8364 : complain_overflow_unsigned),
8365 len, 0, (8 * wordsz),
8369 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8372 printf (" relocation: %8.8lx\n"
8373 " shifted mask: %8.8lx\n"
8374 " shifted/masked reloc: %8.8lx\n"
8375 " result: %8.8lx\n",
8376 (unsigned long) relocation, (unsigned long) (mask << shift),
8377 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8379 put_value (wordsz, chunksz, input_bfd, x,
8380 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8384 /* Functions to read r_offset from external (target order) reloc
8385 entry. Faster than bfd_getl32 et al, because we let the compiler
8386 know the value is aligned. */
8389 ext32l_r_offset (const void *p)
8396 const union aligned32 *a
8397 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8399 uint32_t aval = ( (uint32_t) a->c[0]
8400 | (uint32_t) a->c[1] << 8
8401 | (uint32_t) a->c[2] << 16
8402 | (uint32_t) a->c[3] << 24);
8407 ext32b_r_offset (const void *p)
8414 const union aligned32 *a
8415 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8417 uint32_t aval = ( (uint32_t) a->c[0] << 24
8418 | (uint32_t) a->c[1] << 16
8419 | (uint32_t) a->c[2] << 8
8420 | (uint32_t) a->c[3]);
8424 #ifdef BFD_HOST_64_BIT
8426 ext64l_r_offset (const void *p)
8433 const union aligned64 *a
8434 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8436 uint64_t aval = ( (uint64_t) a->c[0]
8437 | (uint64_t) a->c[1] << 8
8438 | (uint64_t) a->c[2] << 16
8439 | (uint64_t) a->c[3] << 24
8440 | (uint64_t) a->c[4] << 32
8441 | (uint64_t) a->c[5] << 40
8442 | (uint64_t) a->c[6] << 48
8443 | (uint64_t) a->c[7] << 56);
8448 ext64b_r_offset (const void *p)
8455 const union aligned64 *a
8456 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8458 uint64_t aval = ( (uint64_t) a->c[0] << 56
8459 | (uint64_t) a->c[1] << 48
8460 | (uint64_t) a->c[2] << 40
8461 | (uint64_t) a->c[3] << 32
8462 | (uint64_t) a->c[4] << 24
8463 | (uint64_t) a->c[5] << 16
8464 | (uint64_t) a->c[6] << 8
8465 | (uint64_t) a->c[7]);
8470 /* When performing a relocatable link, the input relocations are
8471 preserved. But, if they reference global symbols, the indices
8472 referenced must be updated. Update all the relocations found in
8476 elf_link_adjust_relocs (bfd *abfd,
8478 struct bfd_elf_section_reloc_data *reldata,
8482 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8484 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8485 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8486 bfd_vma r_type_mask;
8488 unsigned int count = reldata->count;
8489 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8491 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8493 swap_in = bed->s->swap_reloc_in;
8494 swap_out = bed->s->swap_reloc_out;
8496 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8498 swap_in = bed->s->swap_reloca_in;
8499 swap_out = bed->s->swap_reloca_out;
8504 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8507 if (bed->s->arch_size == 32)
8514 r_type_mask = 0xffffffff;
8518 erela = reldata->hdr->contents;
8519 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8521 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8524 if (*rel_hash == NULL)
8527 BFD_ASSERT ((*rel_hash)->indx >= 0);
8529 (*swap_in) (abfd, erela, irela);
8530 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8531 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8532 | (irela[j].r_info & r_type_mask));
8533 (*swap_out) (abfd, irela, erela);
8536 if (bed->elf_backend_update_relocs)
8537 (*bed->elf_backend_update_relocs) (sec, reldata);
8539 if (sort && count != 0)
8541 bfd_vma (*ext_r_off) (const void *);
8544 bfd_byte *base, *end, *p, *loc;
8545 bfd_byte *buf = NULL;
8547 if (bed->s->arch_size == 32)
8549 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8550 ext_r_off = ext32l_r_offset;
8551 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8552 ext_r_off = ext32b_r_offset;
8558 #ifdef BFD_HOST_64_BIT
8559 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8560 ext_r_off = ext64l_r_offset;
8561 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8562 ext_r_off = ext64b_r_offset;
8568 /* Must use a stable sort here. A modified insertion sort,
8569 since the relocs are mostly sorted already. */
8570 elt_size = reldata->hdr->sh_entsize;
8571 base = reldata->hdr->contents;
8572 end = base + count * elt_size;
8573 if (elt_size > sizeof (Elf64_External_Rela))
8576 /* Ensure the first element is lowest. This acts as a sentinel,
8577 speeding the main loop below. */
8578 r_off = (*ext_r_off) (base);
8579 for (p = loc = base; (p += elt_size) < end; )
8581 bfd_vma r_off2 = (*ext_r_off) (p);
8590 /* Don't just swap *base and *loc as that changes the order
8591 of the original base[0] and base[1] if they happen to
8592 have the same r_offset. */
8593 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8594 memcpy (onebuf, loc, elt_size);
8595 memmove (base + elt_size, base, loc - base);
8596 memcpy (base, onebuf, elt_size);
8599 for (p = base + elt_size; (p += elt_size) < end; )
8601 /* base to p is sorted, *p is next to insert. */
8602 r_off = (*ext_r_off) (p);
8603 /* Search the sorted region for location to insert. */
8605 while (r_off < (*ext_r_off) (loc))
8610 /* Chances are there is a run of relocs to insert here,
8611 from one of more input files. Files are not always
8612 linked in order due to the way elf_link_input_bfd is
8613 called. See pr17666. */
8614 size_t sortlen = p - loc;
8615 bfd_vma r_off2 = (*ext_r_off) (loc);
8616 size_t runlen = elt_size;
8617 size_t buf_size = 96 * 1024;
8618 while (p + runlen < end
8619 && (sortlen <= buf_size
8620 || runlen + elt_size <= buf_size)
8621 && r_off2 > (*ext_r_off) (p + runlen))
8625 buf = bfd_malloc (buf_size);
8629 if (runlen < sortlen)
8631 memcpy (buf, p, runlen);
8632 memmove (loc + runlen, loc, sortlen);
8633 memcpy (loc, buf, runlen);
8637 memcpy (buf, loc, sortlen);
8638 memmove (loc, p, runlen);
8639 memcpy (loc + runlen, buf, sortlen);
8641 p += runlen - elt_size;
8644 /* Hashes are no longer valid. */
8645 free (reldata->hashes);
8646 reldata->hashes = NULL;
8652 struct elf_link_sort_rela
8658 enum elf_reloc_type_class type;
8659 /* We use this as an array of size int_rels_per_ext_rel. */
8660 Elf_Internal_Rela rela[1];
8664 elf_link_sort_cmp1 (const void *A, const void *B)
8666 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8667 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8668 int relativea, relativeb;
8670 relativea = a->type == reloc_class_relative;
8671 relativeb = b->type == reloc_class_relative;
8673 if (relativea < relativeb)
8675 if (relativea > relativeb)
8677 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8679 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8681 if (a->rela->r_offset < b->rela->r_offset)
8683 if (a->rela->r_offset > b->rela->r_offset)
8689 elf_link_sort_cmp2 (const void *A, const void *B)
8691 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8692 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8694 if (a->type < b->type)
8696 if (a->type > b->type)
8698 if (a->u.offset < b->u.offset)
8700 if (a->u.offset > b->u.offset)
8702 if (a->rela->r_offset < b->rela->r_offset)
8704 if (a->rela->r_offset > b->rela->r_offset)
8710 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8712 asection *dynamic_relocs;
8715 bfd_size_type count, size;
8716 size_t i, ret, sort_elt, ext_size;
8717 bfd_byte *sort, *s_non_relative, *p;
8718 struct elf_link_sort_rela *sq;
8719 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8720 int i2e = bed->s->int_rels_per_ext_rel;
8721 unsigned int opb = bfd_octets_per_byte (abfd);
8722 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8723 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8724 struct bfd_link_order *lo;
8726 bfd_boolean use_rela;
8728 /* Find a dynamic reloc section. */
8729 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8730 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8731 if (rela_dyn != NULL && rela_dyn->size > 0
8732 && rel_dyn != NULL && rel_dyn->size > 0)
8734 bfd_boolean use_rela_initialised = FALSE;
8736 /* This is just here to stop gcc from complaining.
8737 Its initialization checking code is not perfect. */
8740 /* Both sections are present. Examine the sizes
8741 of the indirect sections to help us choose. */
8742 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8743 if (lo->type == bfd_indirect_link_order)
8745 asection *o = lo->u.indirect.section;
8747 if ((o->size % bed->s->sizeof_rela) == 0)
8749 if ((o->size % bed->s->sizeof_rel) == 0)
8750 /* Section size is divisible by both rel and rela sizes.
8751 It is of no help to us. */
8755 /* Section size is only divisible by rela. */
8756 if (use_rela_initialised && (use_rela == FALSE))
8758 _bfd_error_handler (_("%B: Unable to sort relocs - "
8759 "they are in more than one size"),
8761 bfd_set_error (bfd_error_invalid_operation);
8767 use_rela_initialised = TRUE;
8771 else if ((o->size % bed->s->sizeof_rel) == 0)
8773 /* Section size is only divisible by rel. */
8774 if (use_rela_initialised && (use_rela == TRUE))
8776 _bfd_error_handler (_("%B: Unable to sort relocs - "
8777 "they are in more than one size"),
8779 bfd_set_error (bfd_error_invalid_operation);
8785 use_rela_initialised = TRUE;
8790 /* The section size is not divisible by either -
8791 something is wrong. */
8792 _bfd_error_handler (_("%B: Unable to sort relocs - "
8793 "they are of an unknown size"), abfd);
8794 bfd_set_error (bfd_error_invalid_operation);
8799 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8800 if (lo->type == bfd_indirect_link_order)
8802 asection *o = lo->u.indirect.section;
8804 if ((o->size % bed->s->sizeof_rela) == 0)
8806 if ((o->size % bed->s->sizeof_rel) == 0)
8807 /* Section size is divisible by both rel and rela sizes.
8808 It is of no help to us. */
8812 /* Section size is only divisible by rela. */
8813 if (use_rela_initialised && (use_rela == FALSE))
8815 _bfd_error_handler (_("%B: Unable to sort relocs - "
8816 "they are in more than one size"),
8818 bfd_set_error (bfd_error_invalid_operation);
8824 use_rela_initialised = TRUE;
8828 else if ((o->size % bed->s->sizeof_rel) == 0)
8830 /* Section size is only divisible by rel. */
8831 if (use_rela_initialised && (use_rela == TRUE))
8833 _bfd_error_handler (_("%B: Unable to sort relocs - "
8834 "they are in more than one size"),
8836 bfd_set_error (bfd_error_invalid_operation);
8842 use_rela_initialised = TRUE;
8847 /* The section size is not divisible by either -
8848 something is wrong. */
8849 _bfd_error_handler (_("%B: Unable to sort relocs - "
8850 "they are of an unknown size"), abfd);
8851 bfd_set_error (bfd_error_invalid_operation);
8856 if (! use_rela_initialised)
8860 else if (rela_dyn != NULL && rela_dyn->size > 0)
8862 else if (rel_dyn != NULL && rel_dyn->size > 0)
8869 dynamic_relocs = rela_dyn;
8870 ext_size = bed->s->sizeof_rela;
8871 swap_in = bed->s->swap_reloca_in;
8872 swap_out = bed->s->swap_reloca_out;
8876 dynamic_relocs = rel_dyn;
8877 ext_size = bed->s->sizeof_rel;
8878 swap_in = bed->s->swap_reloc_in;
8879 swap_out = bed->s->swap_reloc_out;
8883 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8884 if (lo->type == bfd_indirect_link_order)
8885 size += lo->u.indirect.section->size;
8887 if (size != dynamic_relocs->size)
8890 sort_elt = (sizeof (struct elf_link_sort_rela)
8891 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8893 count = dynamic_relocs->size / ext_size;
8896 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8900 (*info->callbacks->warning)
8901 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8905 if (bed->s->arch_size == 32)
8906 r_sym_mask = ~(bfd_vma) 0xff;
8908 r_sym_mask = ~(bfd_vma) 0xffffffff;
8910 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8911 if (lo->type == bfd_indirect_link_order)
8913 bfd_byte *erel, *erelend;
8914 asection *o = lo->u.indirect.section;
8916 if (o->contents == NULL && o->size != 0)
8918 /* This is a reloc section that is being handled as a normal
8919 section. See bfd_section_from_shdr. We can't combine
8920 relocs in this case. */
8925 erelend = o->contents + o->size;
8926 p = sort + o->output_offset * opb / ext_size * sort_elt;
8928 while (erel < erelend)
8930 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8932 (*swap_in) (abfd, erel, s->rela);
8933 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8934 s->u.sym_mask = r_sym_mask;
8940 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8942 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8944 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8945 if (s->type != reloc_class_relative)
8951 sq = (struct elf_link_sort_rela *) s_non_relative;
8952 for (; i < count; i++, p += sort_elt)
8954 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8955 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8957 sp->u.offset = sq->rela->r_offset;
8960 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8962 struct elf_link_hash_table *htab = elf_hash_table (info);
8963 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
8965 /* We have plt relocs in .rela.dyn. */
8966 sq = (struct elf_link_sort_rela *) sort;
8967 for (i = 0; i < count; i++)
8968 if (sq[count - i - 1].type != reloc_class_plt)
8970 if (i != 0 && htab->srelplt->size == i * ext_size)
8972 struct bfd_link_order **plo;
8973 /* Put srelplt link_order last. This is so the output_offset
8974 set in the next loop is correct for DT_JMPREL. */
8975 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
8976 if ((*plo)->type == bfd_indirect_link_order
8977 && (*plo)->u.indirect.section == htab->srelplt)
8983 plo = &(*plo)->next;
8986 dynamic_relocs->map_tail.link_order = lo;
8991 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8992 if (lo->type == bfd_indirect_link_order)
8994 bfd_byte *erel, *erelend;
8995 asection *o = lo->u.indirect.section;
8998 erelend = o->contents + o->size;
8999 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9000 while (erel < erelend)
9002 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9003 (*swap_out) (abfd, s->rela, erel);
9010 *psec = dynamic_relocs;
9014 /* Add a symbol to the output symbol string table. */
9017 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9019 Elf_Internal_Sym *elfsym,
9020 asection *input_sec,
9021 struct elf_link_hash_entry *h)
9023 int (*output_symbol_hook)
9024 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9025 struct elf_link_hash_entry *);
9026 struct elf_link_hash_table *hash_table;
9027 const struct elf_backend_data *bed;
9028 bfd_size_type strtabsize;
9030 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9032 bed = get_elf_backend_data (flinfo->output_bfd);
9033 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9034 if (output_symbol_hook != NULL)
9036 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9043 || (input_sec->flags & SEC_EXCLUDE))
9044 elfsym->st_name = (unsigned long) -1;
9047 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9048 to get the final offset for st_name. */
9050 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9052 if (elfsym->st_name == (unsigned long) -1)
9056 hash_table = elf_hash_table (flinfo->info);
9057 strtabsize = hash_table->strtabsize;
9058 if (strtabsize <= hash_table->strtabcount)
9060 strtabsize += strtabsize;
9061 hash_table->strtabsize = strtabsize;
9062 strtabsize *= sizeof (*hash_table->strtab);
9064 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9066 if (hash_table->strtab == NULL)
9069 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9070 hash_table->strtab[hash_table->strtabcount].dest_index
9071 = hash_table->strtabcount;
9072 hash_table->strtab[hash_table->strtabcount].destshndx_index
9073 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9075 bfd_get_symcount (flinfo->output_bfd) += 1;
9076 hash_table->strtabcount += 1;
9081 /* Swap symbols out to the symbol table and flush the output symbols to
9085 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9087 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9090 const struct elf_backend_data *bed;
9092 Elf_Internal_Shdr *hdr;
9096 if (!hash_table->strtabcount)
9099 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9101 bed = get_elf_backend_data (flinfo->output_bfd);
9103 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9104 symbuf = (bfd_byte *) bfd_malloc (amt);
9108 if (flinfo->symshndxbuf)
9110 amt = sizeof (Elf_External_Sym_Shndx);
9111 amt *= bfd_get_symcount (flinfo->output_bfd);
9112 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9113 if (flinfo->symshndxbuf == NULL)
9120 for (i = 0; i < hash_table->strtabcount; i++)
9122 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9123 if (elfsym->sym.st_name == (unsigned long) -1)
9124 elfsym->sym.st_name = 0;
9127 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9128 elfsym->sym.st_name);
9129 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9130 ((bfd_byte *) symbuf
9131 + (elfsym->dest_index
9132 * bed->s->sizeof_sym)),
9133 (flinfo->symshndxbuf
9134 + elfsym->destshndx_index));
9137 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9138 pos = hdr->sh_offset + hdr->sh_size;
9139 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9140 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9141 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9143 hdr->sh_size += amt;
9151 free (hash_table->strtab);
9152 hash_table->strtab = NULL;
9157 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9160 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9162 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9163 && sym->st_shndx < SHN_LORESERVE)
9165 /* The gABI doesn't support dynamic symbols in output sections
9168 /* xgettext:c-format */
9169 (_("%B: Too many sections: %d (>= %d)"),
9170 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9171 bfd_set_error (bfd_error_nonrepresentable_section);
9177 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9178 allowing an unsatisfied unversioned symbol in the DSO to match a
9179 versioned symbol that would normally require an explicit version.
9180 We also handle the case that a DSO references a hidden symbol
9181 which may be satisfied by a versioned symbol in another DSO. */
9184 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9185 const struct elf_backend_data *bed,
9186 struct elf_link_hash_entry *h)
9189 struct elf_link_loaded_list *loaded;
9191 if (!is_elf_hash_table (info->hash))
9194 /* Check indirect symbol. */
9195 while (h->root.type == bfd_link_hash_indirect)
9196 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9198 switch (h->root.type)
9204 case bfd_link_hash_undefined:
9205 case bfd_link_hash_undefweak:
9206 abfd = h->root.u.undef.abfd;
9208 || (abfd->flags & DYNAMIC) == 0
9209 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9213 case bfd_link_hash_defined:
9214 case bfd_link_hash_defweak:
9215 abfd = h->root.u.def.section->owner;
9218 case bfd_link_hash_common:
9219 abfd = h->root.u.c.p->section->owner;
9222 BFD_ASSERT (abfd != NULL);
9224 for (loaded = elf_hash_table (info)->loaded;
9226 loaded = loaded->next)
9229 Elf_Internal_Shdr *hdr;
9233 Elf_Internal_Shdr *versymhdr;
9234 Elf_Internal_Sym *isym;
9235 Elf_Internal_Sym *isymend;
9236 Elf_Internal_Sym *isymbuf;
9237 Elf_External_Versym *ever;
9238 Elf_External_Versym *extversym;
9240 input = loaded->abfd;
9242 /* We check each DSO for a possible hidden versioned definition. */
9244 || (input->flags & DYNAMIC) == 0
9245 || elf_dynversym (input) == 0)
9248 hdr = &elf_tdata (input)->dynsymtab_hdr;
9250 symcount = hdr->sh_size / bed->s->sizeof_sym;
9251 if (elf_bad_symtab (input))
9253 extsymcount = symcount;
9258 extsymcount = symcount - hdr->sh_info;
9259 extsymoff = hdr->sh_info;
9262 if (extsymcount == 0)
9265 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9267 if (isymbuf == NULL)
9270 /* Read in any version definitions. */
9271 versymhdr = &elf_tdata (input)->dynversym_hdr;
9272 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9273 if (extversym == NULL)
9276 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9277 || (bfd_bread (extversym, versymhdr->sh_size, input)
9278 != versymhdr->sh_size))
9286 ever = extversym + extsymoff;
9287 isymend = isymbuf + extsymcount;
9288 for (isym = isymbuf; isym < isymend; isym++, ever++)
9291 Elf_Internal_Versym iver;
9292 unsigned short version_index;
9294 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9295 || isym->st_shndx == SHN_UNDEF)
9298 name = bfd_elf_string_from_elf_section (input,
9301 if (strcmp (name, h->root.root.string) != 0)
9304 _bfd_elf_swap_versym_in (input, ever, &iver);
9306 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9308 && h->forced_local))
9310 /* If we have a non-hidden versioned sym, then it should
9311 have provided a definition for the undefined sym unless
9312 it is defined in a non-shared object and forced local.
9317 version_index = iver.vs_vers & VERSYM_VERSION;
9318 if (version_index == 1 || version_index == 2)
9320 /* This is the base or first version. We can use it. */
9334 /* Convert ELF common symbol TYPE. */
9337 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9339 /* Commom symbol can only appear in relocatable link. */
9340 if (!bfd_link_relocatable (info))
9342 switch (info->elf_stt_common)
9346 case elf_stt_common:
9349 case no_elf_stt_common:
9356 /* Add an external symbol to the symbol table. This is called from
9357 the hash table traversal routine. When generating a shared object,
9358 we go through the symbol table twice. The first time we output
9359 anything that might have been forced to local scope in a version
9360 script. The second time we output the symbols that are still
9364 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9366 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9367 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9368 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9370 Elf_Internal_Sym sym;
9371 asection *input_sec;
9372 const struct elf_backend_data *bed;
9377 if (h->root.type == bfd_link_hash_warning)
9379 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9380 if (h->root.type == bfd_link_hash_new)
9384 /* Decide whether to output this symbol in this pass. */
9385 if (eoinfo->localsyms)
9387 if (!h->forced_local)
9392 if (h->forced_local)
9396 bed = get_elf_backend_data (flinfo->output_bfd);
9398 if (h->root.type == bfd_link_hash_undefined)
9400 /* If we have an undefined symbol reference here then it must have
9401 come from a shared library that is being linked in. (Undefined
9402 references in regular files have already been handled unless
9403 they are in unreferenced sections which are removed by garbage
9405 bfd_boolean ignore_undef = FALSE;
9407 /* Some symbols may be special in that the fact that they're
9408 undefined can be safely ignored - let backend determine that. */
9409 if (bed->elf_backend_ignore_undef_symbol)
9410 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9412 /* If we are reporting errors for this situation then do so now. */
9415 && (!h->ref_regular || flinfo->info->gc_sections)
9416 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9417 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9418 (*flinfo->info->callbacks->undefined_symbol)
9419 (flinfo->info, h->root.root.string,
9420 h->ref_regular ? NULL : h->root.u.undef.abfd,
9422 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9424 /* Strip a global symbol defined in a discarded section. */
9429 /* We should also warn if a forced local symbol is referenced from
9430 shared libraries. */
9431 if (bfd_link_executable (flinfo->info)
9436 && h->ref_dynamic_nonweak
9437 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9441 struct elf_link_hash_entry *hi = h;
9443 /* Check indirect symbol. */
9444 while (hi->root.type == bfd_link_hash_indirect)
9445 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9447 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9448 /* xgettext:c-format */
9449 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9450 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9451 /* xgettext:c-format */
9452 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9454 /* xgettext:c-format */
9455 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9456 def_bfd = flinfo->output_bfd;
9457 if (hi->root.u.def.section != bfd_abs_section_ptr)
9458 def_bfd = hi->root.u.def.section->owner;
9459 _bfd_error_handler (msg, flinfo->output_bfd, def_bfd,
9460 h->root.root.string);
9461 bfd_set_error (bfd_error_bad_value);
9462 eoinfo->failed = TRUE;
9466 /* We don't want to output symbols that have never been mentioned by
9467 a regular file, or that we have been told to strip. However, if
9468 h->indx is set to -2, the symbol is used by a reloc and we must
9473 else if ((h->def_dynamic
9475 || h->root.type == bfd_link_hash_new)
9479 else if (flinfo->info->strip == strip_all)
9481 else if (flinfo->info->strip == strip_some
9482 && bfd_hash_lookup (flinfo->info->keep_hash,
9483 h->root.root.string, FALSE, FALSE) == NULL)
9485 else if ((h->root.type == bfd_link_hash_defined
9486 || h->root.type == bfd_link_hash_defweak)
9487 && ((flinfo->info->strip_discarded
9488 && discarded_section (h->root.u.def.section))
9489 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9490 && h->root.u.def.section->owner != NULL
9491 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9493 else if ((h->root.type == bfd_link_hash_undefined
9494 || h->root.type == bfd_link_hash_undefweak)
9495 && h->root.u.undef.abfd != NULL
9496 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9501 /* If we're stripping it, and it's not a dynamic symbol, there's
9502 nothing else to do. However, if it is a forced local symbol or
9503 an ifunc symbol we need to give the backend finish_dynamic_symbol
9504 function a chance to make it dynamic. */
9507 && type != STT_GNU_IFUNC
9508 && !h->forced_local)
9512 sym.st_size = h->size;
9513 sym.st_other = h->other;
9514 switch (h->root.type)
9517 case bfd_link_hash_new:
9518 case bfd_link_hash_warning:
9522 case bfd_link_hash_undefined:
9523 case bfd_link_hash_undefweak:
9524 input_sec = bfd_und_section_ptr;
9525 sym.st_shndx = SHN_UNDEF;
9528 case bfd_link_hash_defined:
9529 case bfd_link_hash_defweak:
9531 input_sec = h->root.u.def.section;
9532 if (input_sec->output_section != NULL)
9535 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9536 input_sec->output_section);
9537 if (sym.st_shndx == SHN_BAD)
9540 /* xgettext:c-format */
9541 (_("%B: could not find output section %A for input section %A"),
9542 flinfo->output_bfd, input_sec->output_section, input_sec);
9543 bfd_set_error (bfd_error_nonrepresentable_section);
9544 eoinfo->failed = TRUE;
9548 /* ELF symbols in relocatable files are section relative,
9549 but in nonrelocatable files they are virtual
9551 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9552 if (!bfd_link_relocatable (flinfo->info))
9554 sym.st_value += input_sec->output_section->vma;
9555 if (h->type == STT_TLS)
9557 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9558 if (tls_sec != NULL)
9559 sym.st_value -= tls_sec->vma;
9565 BFD_ASSERT (input_sec->owner == NULL
9566 || (input_sec->owner->flags & DYNAMIC) != 0);
9567 sym.st_shndx = SHN_UNDEF;
9568 input_sec = bfd_und_section_ptr;
9573 case bfd_link_hash_common:
9574 input_sec = h->root.u.c.p->section;
9575 sym.st_shndx = bed->common_section_index (input_sec);
9576 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9579 case bfd_link_hash_indirect:
9580 /* These symbols are created by symbol versioning. They point
9581 to the decorated version of the name. For example, if the
9582 symbol foo@@GNU_1.2 is the default, which should be used when
9583 foo is used with no version, then we add an indirect symbol
9584 foo which points to foo@@GNU_1.2. We ignore these symbols,
9585 since the indirected symbol is already in the hash table. */
9589 if (type == STT_COMMON || type == STT_OBJECT)
9590 switch (h->root.type)
9592 case bfd_link_hash_common:
9593 type = elf_link_convert_common_type (flinfo->info, type);
9595 case bfd_link_hash_defined:
9596 case bfd_link_hash_defweak:
9597 if (bed->common_definition (&sym))
9598 type = elf_link_convert_common_type (flinfo->info, type);
9602 case bfd_link_hash_undefined:
9603 case bfd_link_hash_undefweak:
9609 if (h->forced_local)
9611 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
9612 /* Turn off visibility on local symbol. */
9613 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9615 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9616 else if (h->unique_global && h->def_regular)
9617 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
9618 else if (h->root.type == bfd_link_hash_undefweak
9619 || h->root.type == bfd_link_hash_defweak)
9620 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
9622 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9623 sym.st_target_internal = h->target_internal;
9625 /* Give the processor backend a chance to tweak the symbol value,
9626 and also to finish up anything that needs to be done for this
9627 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9628 forced local syms when non-shared is due to a historical quirk.
9629 STT_GNU_IFUNC symbol must go through PLT. */
9630 if ((h->type == STT_GNU_IFUNC
9632 && !bfd_link_relocatable (flinfo->info))
9633 || ((h->dynindx != -1
9635 && ((bfd_link_pic (flinfo->info)
9636 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9637 || h->root.type != bfd_link_hash_undefweak))
9638 || !h->forced_local)
9639 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9641 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9642 (flinfo->output_bfd, flinfo->info, h, &sym)))
9644 eoinfo->failed = TRUE;
9649 /* If we are marking the symbol as undefined, and there are no
9650 non-weak references to this symbol from a regular object, then
9651 mark the symbol as weak undefined; if there are non-weak
9652 references, mark the symbol as strong. We can't do this earlier,
9653 because it might not be marked as undefined until the
9654 finish_dynamic_symbol routine gets through with it. */
9655 if (sym.st_shndx == SHN_UNDEF
9657 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9658 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9661 type = ELF_ST_TYPE (sym.st_info);
9663 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9664 if (type == STT_GNU_IFUNC)
9667 if (h->ref_regular_nonweak)
9668 bindtype = STB_GLOBAL;
9670 bindtype = STB_WEAK;
9671 sym.st_info = ELF_ST_INFO (bindtype, type);
9674 /* If this is a symbol defined in a dynamic library, don't use the
9675 symbol size from the dynamic library. Relinking an executable
9676 against a new library may introduce gratuitous changes in the
9677 executable's symbols if we keep the size. */
9678 if (sym.st_shndx == SHN_UNDEF
9683 /* If a non-weak symbol with non-default visibility is not defined
9684 locally, it is a fatal error. */
9685 if (!bfd_link_relocatable (flinfo->info)
9686 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9687 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9688 && h->root.type == bfd_link_hash_undefined
9693 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9694 /* xgettext:c-format */
9695 msg = _("%B: protected symbol `%s' isn't defined");
9696 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9697 /* xgettext:c-format */
9698 msg = _("%B: internal symbol `%s' isn't defined");
9700 /* xgettext:c-format */
9701 msg = _("%B: hidden symbol `%s' isn't defined");
9702 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
9703 bfd_set_error (bfd_error_bad_value);
9704 eoinfo->failed = TRUE;
9708 /* If this symbol should be put in the .dynsym section, then put it
9709 there now. We already know the symbol index. We also fill in
9710 the entry in the .hash section. */
9711 if (elf_hash_table (flinfo->info)->dynsym != NULL
9713 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9717 /* Since there is no version information in the dynamic string,
9718 if there is no version info in symbol version section, we will
9719 have a run-time problem if not linking executable, referenced
9720 by shared library, or not bound locally. */
9721 if (h->verinfo.verdef == NULL
9722 && (!bfd_link_executable (flinfo->info)
9724 || !h->def_regular))
9726 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9728 if (p && p [1] != '\0')
9731 /* xgettext:c-format */
9732 (_("%B: No symbol version section for versioned symbol `%s'"),
9733 flinfo->output_bfd, h->root.root.string);
9734 eoinfo->failed = TRUE;
9739 sym.st_name = h->dynstr_index;
9740 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9741 + h->dynindx * bed->s->sizeof_sym);
9742 if (!check_dynsym (flinfo->output_bfd, &sym))
9744 eoinfo->failed = TRUE;
9747 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9749 if (flinfo->hash_sec != NULL)
9751 size_t hash_entry_size;
9752 bfd_byte *bucketpos;
9757 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9758 bucket = h->u.elf_hash_value % bucketcount;
9761 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9762 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9763 + (bucket + 2) * hash_entry_size);
9764 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9765 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9767 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9768 ((bfd_byte *) flinfo->hash_sec->contents
9769 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9772 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9774 Elf_Internal_Versym iversym;
9775 Elf_External_Versym *eversym;
9777 if (!h->def_regular)
9779 if (h->verinfo.verdef == NULL
9780 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9781 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9782 iversym.vs_vers = 0;
9784 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9788 if (h->verinfo.vertree == NULL)
9789 iversym.vs_vers = 1;
9791 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9792 if (flinfo->info->create_default_symver)
9796 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9798 if (h->versioned == versioned_hidden && h->def_regular)
9799 iversym.vs_vers |= VERSYM_HIDDEN;
9801 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9802 eversym += h->dynindx;
9803 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9807 /* If the symbol is undefined, and we didn't output it to .dynsym,
9808 strip it from .symtab too. Obviously we can't do this for
9809 relocatable output or when needed for --emit-relocs. */
9810 else if (input_sec == bfd_und_section_ptr
9812 && !bfd_link_relocatable (flinfo->info))
9814 /* Also strip others that we couldn't earlier due to dynamic symbol
9818 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9821 /* Output a FILE symbol so that following locals are not associated
9822 with the wrong input file. We need one for forced local symbols
9823 if we've seen more than one FILE symbol or when we have exactly
9824 one FILE symbol but global symbols are present in a file other
9825 than the one with the FILE symbol. We also need one if linker
9826 defined symbols are present. In practice these conditions are
9827 always met, so just emit the FILE symbol unconditionally. */
9828 if (eoinfo->localsyms
9829 && !eoinfo->file_sym_done
9830 && eoinfo->flinfo->filesym_count != 0)
9832 Elf_Internal_Sym fsym;
9834 memset (&fsym, 0, sizeof (fsym));
9835 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9836 fsym.st_shndx = SHN_ABS;
9837 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9838 bfd_und_section_ptr, NULL))
9841 eoinfo->file_sym_done = TRUE;
9844 indx = bfd_get_symcount (flinfo->output_bfd);
9845 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9849 eoinfo->failed = TRUE;
9854 else if (h->indx == -2)
9860 /* Return TRUE if special handling is done for relocs in SEC against
9861 symbols defined in discarded sections. */
9864 elf_section_ignore_discarded_relocs (asection *sec)
9866 const struct elf_backend_data *bed;
9868 switch (sec->sec_info_type)
9870 case SEC_INFO_TYPE_STABS:
9871 case SEC_INFO_TYPE_EH_FRAME:
9872 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9878 bed = get_elf_backend_data (sec->owner);
9879 if (bed->elf_backend_ignore_discarded_relocs != NULL
9880 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9886 /* Return a mask saying how ld should treat relocations in SEC against
9887 symbols defined in discarded sections. If this function returns
9888 COMPLAIN set, ld will issue a warning message. If this function
9889 returns PRETEND set, and the discarded section was link-once and the
9890 same size as the kept link-once section, ld will pretend that the
9891 symbol was actually defined in the kept section. Otherwise ld will
9892 zero the reloc (at least that is the intent, but some cooperation by
9893 the target dependent code is needed, particularly for REL targets). */
9896 _bfd_elf_default_action_discarded (asection *sec)
9898 if (sec->flags & SEC_DEBUGGING)
9901 if (strcmp (".eh_frame", sec->name) == 0)
9904 if (strcmp (".gcc_except_table", sec->name) == 0)
9907 return COMPLAIN | PRETEND;
9910 /* Find a match between a section and a member of a section group. */
9913 match_group_member (asection *sec, asection *group,
9914 struct bfd_link_info *info)
9916 asection *first = elf_next_in_group (group);
9917 asection *s = first;
9921 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9924 s = elf_next_in_group (s);
9932 /* Check if the kept section of a discarded section SEC can be used
9933 to replace it. Return the replacement if it is OK. Otherwise return
9937 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9941 kept = sec->kept_section;
9944 if ((kept->flags & SEC_GROUP) != 0)
9945 kept = match_group_member (sec, kept, info);
9947 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9948 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9950 sec->kept_section = kept;
9955 /* Link an input file into the linker output file. This function
9956 handles all the sections and relocations of the input file at once.
9957 This is so that we only have to read the local symbols once, and
9958 don't have to keep them in memory. */
9961 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9963 int (*relocate_section)
9964 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9965 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9967 Elf_Internal_Shdr *symtab_hdr;
9970 Elf_Internal_Sym *isymbuf;
9971 Elf_Internal_Sym *isym;
9972 Elf_Internal_Sym *isymend;
9974 asection **ppsection;
9976 const struct elf_backend_data *bed;
9977 struct elf_link_hash_entry **sym_hashes;
9978 bfd_size_type address_size;
9979 bfd_vma r_type_mask;
9981 bfd_boolean have_file_sym = FALSE;
9983 output_bfd = flinfo->output_bfd;
9984 bed = get_elf_backend_data (output_bfd);
9985 relocate_section = bed->elf_backend_relocate_section;
9987 /* If this is a dynamic object, we don't want to do anything here:
9988 we don't want the local symbols, and we don't want the section
9990 if ((input_bfd->flags & DYNAMIC) != 0)
9993 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9994 if (elf_bad_symtab (input_bfd))
9996 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10001 locsymcount = symtab_hdr->sh_info;
10002 extsymoff = symtab_hdr->sh_info;
10005 /* Read the local symbols. */
10006 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10007 if (isymbuf == NULL && locsymcount != 0)
10009 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10010 flinfo->internal_syms,
10011 flinfo->external_syms,
10012 flinfo->locsym_shndx);
10013 if (isymbuf == NULL)
10017 /* Find local symbol sections and adjust values of symbols in
10018 SEC_MERGE sections. Write out those local symbols we know are
10019 going into the output file. */
10020 isymend = isymbuf + locsymcount;
10021 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10023 isym++, pindex++, ppsection++)
10027 Elf_Internal_Sym osym;
10033 if (elf_bad_symtab (input_bfd))
10035 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10042 if (isym->st_shndx == SHN_UNDEF)
10043 isec = bfd_und_section_ptr;
10044 else if (isym->st_shndx == SHN_ABS)
10045 isec = bfd_abs_section_ptr;
10046 else if (isym->st_shndx == SHN_COMMON)
10047 isec = bfd_com_section_ptr;
10050 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10053 /* Don't attempt to output symbols with st_shnx in the
10054 reserved range other than SHN_ABS and SHN_COMMON. */
10058 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10059 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10061 _bfd_merged_section_offset (output_bfd, &isec,
10062 elf_section_data (isec)->sec_info,
10068 /* Don't output the first, undefined, symbol. In fact, don't
10069 output any undefined local symbol. */
10070 if (isec == bfd_und_section_ptr)
10073 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10075 /* We never output section symbols. Instead, we use the
10076 section symbol of the corresponding section in the output
10081 /* If we are stripping all symbols, we don't want to output this
10083 if (flinfo->info->strip == strip_all)
10086 /* If we are discarding all local symbols, we don't want to
10087 output this one. If we are generating a relocatable output
10088 file, then some of the local symbols may be required by
10089 relocs; we output them below as we discover that they are
10091 if (flinfo->info->discard == discard_all)
10094 /* If this symbol is defined in a section which we are
10095 discarding, we don't need to keep it. */
10096 if (isym->st_shndx != SHN_UNDEF
10097 && isym->st_shndx < SHN_LORESERVE
10098 && bfd_section_removed_from_list (output_bfd,
10099 isec->output_section))
10102 /* Get the name of the symbol. */
10103 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10108 /* See if we are discarding symbols with this name. */
10109 if ((flinfo->info->strip == strip_some
10110 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10112 || (((flinfo->info->discard == discard_sec_merge
10113 && (isec->flags & SEC_MERGE)
10114 && !bfd_link_relocatable (flinfo->info))
10115 || flinfo->info->discard == discard_l)
10116 && bfd_is_local_label_name (input_bfd, name)))
10119 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10121 if (input_bfd->lto_output)
10122 /* -flto puts a temp file name here. This means builds
10123 are not reproducible. Discard the symbol. */
10125 have_file_sym = TRUE;
10126 flinfo->filesym_count += 1;
10128 if (!have_file_sym)
10130 /* In the absence of debug info, bfd_find_nearest_line uses
10131 FILE symbols to determine the source file for local
10132 function symbols. Provide a FILE symbol here if input
10133 files lack such, so that their symbols won't be
10134 associated with a previous input file. It's not the
10135 source file, but the best we can do. */
10136 have_file_sym = TRUE;
10137 flinfo->filesym_count += 1;
10138 memset (&osym, 0, sizeof (osym));
10139 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10140 osym.st_shndx = SHN_ABS;
10141 if (!elf_link_output_symstrtab (flinfo,
10142 (input_bfd->lto_output ? NULL
10143 : input_bfd->filename),
10144 &osym, bfd_abs_section_ptr,
10151 /* Adjust the section index for the output file. */
10152 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10153 isec->output_section);
10154 if (osym.st_shndx == SHN_BAD)
10157 /* ELF symbols in relocatable files are section relative, but
10158 in executable files they are virtual addresses. Note that
10159 this code assumes that all ELF sections have an associated
10160 BFD section with a reasonable value for output_offset; below
10161 we assume that they also have a reasonable value for
10162 output_section. Any special sections must be set up to meet
10163 these requirements. */
10164 osym.st_value += isec->output_offset;
10165 if (!bfd_link_relocatable (flinfo->info))
10167 osym.st_value += isec->output_section->vma;
10168 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10170 /* STT_TLS symbols are relative to PT_TLS segment base. */
10171 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
10172 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10176 indx = bfd_get_symcount (output_bfd);
10177 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10184 if (bed->s->arch_size == 32)
10186 r_type_mask = 0xff;
10192 r_type_mask = 0xffffffff;
10197 /* Relocate the contents of each section. */
10198 sym_hashes = elf_sym_hashes (input_bfd);
10199 for (o = input_bfd->sections; o != NULL; o = o->next)
10201 bfd_byte *contents;
10203 if (! o->linker_mark)
10205 /* This section was omitted from the link. */
10209 if (bfd_link_relocatable (flinfo->info)
10210 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10212 /* Deal with the group signature symbol. */
10213 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10214 unsigned long symndx = sec_data->this_hdr.sh_info;
10215 asection *osec = o->output_section;
10217 if (symndx >= locsymcount
10218 || (elf_bad_symtab (input_bfd)
10219 && flinfo->sections[symndx] == NULL))
10221 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10222 while (h->root.type == bfd_link_hash_indirect
10223 || h->root.type == bfd_link_hash_warning)
10224 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10225 /* Arrange for symbol to be output. */
10227 elf_section_data (osec)->this_hdr.sh_info = -2;
10229 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10231 /* We'll use the output section target_index. */
10232 asection *sec = flinfo->sections[symndx]->output_section;
10233 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10237 if (flinfo->indices[symndx] == -1)
10239 /* Otherwise output the local symbol now. */
10240 Elf_Internal_Sym sym = isymbuf[symndx];
10241 asection *sec = flinfo->sections[symndx]->output_section;
10246 name = bfd_elf_string_from_elf_section (input_bfd,
10247 symtab_hdr->sh_link,
10252 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10254 if (sym.st_shndx == SHN_BAD)
10257 sym.st_value += o->output_offset;
10259 indx = bfd_get_symcount (output_bfd);
10260 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10265 flinfo->indices[symndx] = indx;
10269 elf_section_data (osec)->this_hdr.sh_info
10270 = flinfo->indices[symndx];
10274 if ((o->flags & SEC_HAS_CONTENTS) == 0
10275 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10278 if ((o->flags & SEC_LINKER_CREATED) != 0)
10280 /* Section was created by _bfd_elf_link_create_dynamic_sections
10285 /* Get the contents of the section. They have been cached by a
10286 relaxation routine. Note that o is a section in an input
10287 file, so the contents field will not have been set by any of
10288 the routines which work on output files. */
10289 if (elf_section_data (o)->this_hdr.contents != NULL)
10291 contents = elf_section_data (o)->this_hdr.contents;
10292 if (bed->caches_rawsize
10294 && o->rawsize < o->size)
10296 memcpy (flinfo->contents, contents, o->rawsize);
10297 contents = flinfo->contents;
10302 contents = flinfo->contents;
10303 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10307 if ((o->flags & SEC_RELOC) != 0)
10309 Elf_Internal_Rela *internal_relocs;
10310 Elf_Internal_Rela *rel, *relend;
10311 int action_discarded;
10314 /* Get the swapped relocs. */
10316 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10317 flinfo->internal_relocs, FALSE);
10318 if (internal_relocs == NULL
10319 && o->reloc_count > 0)
10322 /* We need to reverse-copy input .ctors/.dtors sections if
10323 they are placed in .init_array/.finit_array for output. */
10324 if (o->size > address_size
10325 && ((strncmp (o->name, ".ctors", 6) == 0
10326 && strcmp (o->output_section->name,
10327 ".init_array") == 0)
10328 || (strncmp (o->name, ".dtors", 6) == 0
10329 && strcmp (o->output_section->name,
10330 ".fini_array") == 0))
10331 && (o->name[6] == 0 || o->name[6] == '.'))
10333 if (o->size != o->reloc_count * address_size)
10336 /* xgettext:c-format */
10337 (_("error: %B: size of section %A is not "
10338 "multiple of address size"),
10340 bfd_set_error (bfd_error_on_input);
10343 o->flags |= SEC_ELF_REVERSE_COPY;
10346 action_discarded = -1;
10347 if (!elf_section_ignore_discarded_relocs (o))
10348 action_discarded = (*bed->action_discarded) (o);
10350 /* Run through the relocs evaluating complex reloc symbols and
10351 looking for relocs against symbols from discarded sections
10352 or section symbols from removed link-once sections.
10353 Complain about relocs against discarded sections. Zero
10354 relocs against removed link-once sections. */
10356 rel = internal_relocs;
10357 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10358 for ( ; rel < relend; rel++)
10360 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10361 unsigned int s_type;
10362 asection **ps, *sec;
10363 struct elf_link_hash_entry *h = NULL;
10364 const char *sym_name;
10366 if (r_symndx == STN_UNDEF)
10369 if (r_symndx >= locsymcount
10370 || (elf_bad_symtab (input_bfd)
10371 && flinfo->sections[r_symndx] == NULL))
10373 h = sym_hashes[r_symndx - extsymoff];
10375 /* Badly formatted input files can contain relocs that
10376 reference non-existant symbols. Check here so that
10377 we do not seg fault. */
10382 sprintf_vma (buffer, rel->r_info);
10384 /* xgettext:c-format */
10385 (_("error: %B contains a reloc (0x%s) for section %A "
10386 "that references a non-existent global symbol"),
10387 input_bfd, o, buffer);
10388 bfd_set_error (bfd_error_bad_value);
10392 while (h->root.type == bfd_link_hash_indirect
10393 || h->root.type == bfd_link_hash_warning)
10394 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10398 /* If a plugin symbol is referenced from a non-IR file,
10399 mark the symbol as undefined. Note that the
10400 linker may attach linker created dynamic sections
10401 to the plugin bfd. Symbols defined in linker
10402 created sections are not plugin symbols. */
10403 if (h->root.non_ir_ref
10404 && (h->root.type == bfd_link_hash_defined
10405 || h->root.type == bfd_link_hash_defweak)
10406 && (h->root.u.def.section->flags
10407 & SEC_LINKER_CREATED) == 0
10408 && h->root.u.def.section->owner != NULL
10409 && (h->root.u.def.section->owner->flags
10410 & BFD_PLUGIN) != 0)
10412 h->root.type = bfd_link_hash_undefined;
10413 h->root.u.undef.abfd = h->root.u.def.section->owner;
10417 if (h->root.type == bfd_link_hash_defined
10418 || h->root.type == bfd_link_hash_defweak)
10419 ps = &h->root.u.def.section;
10421 sym_name = h->root.root.string;
10425 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10427 s_type = ELF_ST_TYPE (sym->st_info);
10428 ps = &flinfo->sections[r_symndx];
10429 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10433 if ((s_type == STT_RELC || s_type == STT_SRELC)
10434 && !bfd_link_relocatable (flinfo->info))
10437 bfd_vma dot = (rel->r_offset
10438 + o->output_offset + o->output_section->vma);
10440 printf ("Encountered a complex symbol!");
10441 printf (" (input_bfd %s, section %s, reloc %ld\n",
10442 input_bfd->filename, o->name,
10443 (long) (rel - internal_relocs));
10444 printf (" symbol: idx %8.8lx, name %s\n",
10445 r_symndx, sym_name);
10446 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10447 (unsigned long) rel->r_info,
10448 (unsigned long) rel->r_offset);
10450 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10451 isymbuf, locsymcount, s_type == STT_SRELC))
10454 /* Symbol evaluated OK. Update to absolute value. */
10455 set_symbol_value (input_bfd, isymbuf, locsymcount,
10460 if (action_discarded != -1 && ps != NULL)
10462 /* Complain if the definition comes from a
10463 discarded section. */
10464 if ((sec = *ps) != NULL && discarded_section (sec))
10466 BFD_ASSERT (r_symndx != STN_UNDEF);
10467 if (action_discarded & COMPLAIN)
10468 (*flinfo->info->callbacks->einfo)
10469 /* xgettext:c-format */
10470 (_("%X`%s' referenced in section `%A' of %B: "
10471 "defined in discarded section `%A' of %B\n"),
10472 sym_name, o, input_bfd, sec, sec->owner);
10474 /* Try to do the best we can to support buggy old
10475 versions of gcc. Pretend that the symbol is
10476 really defined in the kept linkonce section.
10477 FIXME: This is quite broken. Modifying the
10478 symbol here means we will be changing all later
10479 uses of the symbol, not just in this section. */
10480 if (action_discarded & PRETEND)
10484 kept = _bfd_elf_check_kept_section (sec,
10496 /* Relocate the section by invoking a back end routine.
10498 The back end routine is responsible for adjusting the
10499 section contents as necessary, and (if using Rela relocs
10500 and generating a relocatable output file) adjusting the
10501 reloc addend as necessary.
10503 The back end routine does not have to worry about setting
10504 the reloc address or the reloc symbol index.
10506 The back end routine is given a pointer to the swapped in
10507 internal symbols, and can access the hash table entries
10508 for the external symbols via elf_sym_hashes (input_bfd).
10510 When generating relocatable output, the back end routine
10511 must handle STB_LOCAL/STT_SECTION symbols specially. The
10512 output symbol is going to be a section symbol
10513 corresponding to the output section, which will require
10514 the addend to be adjusted. */
10516 ret = (*relocate_section) (output_bfd, flinfo->info,
10517 input_bfd, o, contents,
10525 || bfd_link_relocatable (flinfo->info)
10526 || flinfo->info->emitrelocations)
10528 Elf_Internal_Rela *irela;
10529 Elf_Internal_Rela *irelaend, *irelamid;
10530 bfd_vma last_offset;
10531 struct elf_link_hash_entry **rel_hash;
10532 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10533 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10534 unsigned int next_erel;
10535 bfd_boolean rela_normal;
10536 struct bfd_elf_section_data *esdi, *esdo;
10538 esdi = elf_section_data (o);
10539 esdo = elf_section_data (o->output_section);
10540 rela_normal = FALSE;
10542 /* Adjust the reloc addresses and symbol indices. */
10544 irela = internal_relocs;
10545 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10546 rel_hash = esdo->rel.hashes + esdo->rel.count;
10547 /* We start processing the REL relocs, if any. When we reach
10548 IRELAMID in the loop, we switch to the RELA relocs. */
10550 if (esdi->rel.hdr != NULL)
10551 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10552 * bed->s->int_rels_per_ext_rel);
10553 rel_hash_list = rel_hash;
10554 rela_hash_list = NULL;
10555 last_offset = o->output_offset;
10556 if (!bfd_link_relocatable (flinfo->info))
10557 last_offset += o->output_section->vma;
10558 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10560 unsigned long r_symndx;
10562 Elf_Internal_Sym sym;
10564 if (next_erel == bed->s->int_rels_per_ext_rel)
10570 if (irela == irelamid)
10572 rel_hash = esdo->rela.hashes + esdo->rela.count;
10573 rela_hash_list = rel_hash;
10574 rela_normal = bed->rela_normal;
10577 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10580 if (irela->r_offset >= (bfd_vma) -2)
10582 /* This is a reloc for a deleted entry or somesuch.
10583 Turn it into an R_*_NONE reloc, at the same
10584 offset as the last reloc. elf_eh_frame.c and
10585 bfd_elf_discard_info rely on reloc offsets
10587 irela->r_offset = last_offset;
10589 irela->r_addend = 0;
10593 irela->r_offset += o->output_offset;
10595 /* Relocs in an executable have to be virtual addresses. */
10596 if (!bfd_link_relocatable (flinfo->info))
10597 irela->r_offset += o->output_section->vma;
10599 last_offset = irela->r_offset;
10601 r_symndx = irela->r_info >> r_sym_shift;
10602 if (r_symndx == STN_UNDEF)
10605 if (r_symndx >= locsymcount
10606 || (elf_bad_symtab (input_bfd)
10607 && flinfo->sections[r_symndx] == NULL))
10609 struct elf_link_hash_entry *rh;
10610 unsigned long indx;
10612 /* This is a reloc against a global symbol. We
10613 have not yet output all the local symbols, so
10614 we do not know the symbol index of any global
10615 symbol. We set the rel_hash entry for this
10616 reloc to point to the global hash table entry
10617 for this symbol. The symbol index is then
10618 set at the end of bfd_elf_final_link. */
10619 indx = r_symndx - extsymoff;
10620 rh = elf_sym_hashes (input_bfd)[indx];
10621 while (rh->root.type == bfd_link_hash_indirect
10622 || rh->root.type == bfd_link_hash_warning)
10623 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10625 /* Setting the index to -2 tells
10626 elf_link_output_extsym that this symbol is
10627 used by a reloc. */
10628 BFD_ASSERT (rh->indx < 0);
10636 /* This is a reloc against a local symbol. */
10639 sym = isymbuf[r_symndx];
10640 sec = flinfo->sections[r_symndx];
10641 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10643 /* I suppose the backend ought to fill in the
10644 section of any STT_SECTION symbol against a
10645 processor specific section. */
10646 r_symndx = STN_UNDEF;
10647 if (bfd_is_abs_section (sec))
10649 else if (sec == NULL || sec->owner == NULL)
10651 bfd_set_error (bfd_error_bad_value);
10656 asection *osec = sec->output_section;
10658 /* If we have discarded a section, the output
10659 section will be the absolute section. In
10660 case of discarded SEC_MERGE sections, use
10661 the kept section. relocate_section should
10662 have already handled discarded linkonce
10664 if (bfd_is_abs_section (osec)
10665 && sec->kept_section != NULL
10666 && sec->kept_section->output_section != NULL)
10668 osec = sec->kept_section->output_section;
10669 irela->r_addend -= osec->vma;
10672 if (!bfd_is_abs_section (osec))
10674 r_symndx = osec->target_index;
10675 if (r_symndx == STN_UNDEF)
10677 irela->r_addend += osec->vma;
10678 osec = _bfd_nearby_section (output_bfd, osec,
10680 irela->r_addend -= osec->vma;
10681 r_symndx = osec->target_index;
10686 /* Adjust the addend according to where the
10687 section winds up in the output section. */
10689 irela->r_addend += sec->output_offset;
10693 if (flinfo->indices[r_symndx] == -1)
10695 unsigned long shlink;
10700 if (flinfo->info->strip == strip_all)
10702 /* You can't do ld -r -s. */
10703 bfd_set_error (bfd_error_invalid_operation);
10707 /* This symbol was skipped earlier, but
10708 since it is needed by a reloc, we
10709 must output it now. */
10710 shlink = symtab_hdr->sh_link;
10711 name = (bfd_elf_string_from_elf_section
10712 (input_bfd, shlink, sym.st_name));
10716 osec = sec->output_section;
10718 _bfd_elf_section_from_bfd_section (output_bfd,
10720 if (sym.st_shndx == SHN_BAD)
10723 sym.st_value += sec->output_offset;
10724 if (!bfd_link_relocatable (flinfo->info))
10726 sym.st_value += osec->vma;
10727 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10729 /* STT_TLS symbols are relative to PT_TLS
10731 BFD_ASSERT (elf_hash_table (flinfo->info)
10732 ->tls_sec != NULL);
10733 sym.st_value -= (elf_hash_table (flinfo->info)
10738 indx = bfd_get_symcount (output_bfd);
10739 ret = elf_link_output_symstrtab (flinfo, name,
10745 flinfo->indices[r_symndx] = indx;
10750 r_symndx = flinfo->indices[r_symndx];
10753 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10754 | (irela->r_info & r_type_mask));
10757 /* Swap out the relocs. */
10758 input_rel_hdr = esdi->rel.hdr;
10759 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10761 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10766 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10767 * bed->s->int_rels_per_ext_rel);
10768 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10771 input_rela_hdr = esdi->rela.hdr;
10772 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10774 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10783 /* Write out the modified section contents. */
10784 if (bed->elf_backend_write_section
10785 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10788 /* Section written out. */
10790 else switch (o->sec_info_type)
10792 case SEC_INFO_TYPE_STABS:
10793 if (! (_bfd_write_section_stabs
10795 &elf_hash_table (flinfo->info)->stab_info,
10796 o, &elf_section_data (o)->sec_info, contents)))
10799 case SEC_INFO_TYPE_MERGE:
10800 if (! _bfd_write_merged_section (output_bfd, o,
10801 elf_section_data (o)->sec_info))
10804 case SEC_INFO_TYPE_EH_FRAME:
10806 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10811 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10813 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10821 if (! (o->flags & SEC_EXCLUDE))
10823 file_ptr offset = (file_ptr) o->output_offset;
10824 bfd_size_type todo = o->size;
10826 offset *= bfd_octets_per_byte (output_bfd);
10828 if ((o->flags & SEC_ELF_REVERSE_COPY))
10830 /* Reverse-copy input section to output. */
10833 todo -= address_size;
10834 if (! bfd_set_section_contents (output_bfd,
10842 offset += address_size;
10846 else if (! bfd_set_section_contents (output_bfd,
10860 /* Generate a reloc when linking an ELF file. This is a reloc
10861 requested by the linker, and does not come from any input file. This
10862 is used to build constructor and destructor tables when linking
10866 elf_reloc_link_order (bfd *output_bfd,
10867 struct bfd_link_info *info,
10868 asection *output_section,
10869 struct bfd_link_order *link_order)
10871 reloc_howto_type *howto;
10875 struct bfd_elf_section_reloc_data *reldata;
10876 struct elf_link_hash_entry **rel_hash_ptr;
10877 Elf_Internal_Shdr *rel_hdr;
10878 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10879 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10882 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10884 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10887 bfd_set_error (bfd_error_bad_value);
10891 addend = link_order->u.reloc.p->addend;
10894 reldata = &esdo->rel;
10895 else if (esdo->rela.hdr)
10896 reldata = &esdo->rela;
10903 /* Figure out the symbol index. */
10904 rel_hash_ptr = reldata->hashes + reldata->count;
10905 if (link_order->type == bfd_section_reloc_link_order)
10907 indx = link_order->u.reloc.p->u.section->target_index;
10908 BFD_ASSERT (indx != 0);
10909 *rel_hash_ptr = NULL;
10913 struct elf_link_hash_entry *h;
10915 /* Treat a reloc against a defined symbol as though it were
10916 actually against the section. */
10917 h = ((struct elf_link_hash_entry *)
10918 bfd_wrapped_link_hash_lookup (output_bfd, info,
10919 link_order->u.reloc.p->u.name,
10920 FALSE, FALSE, TRUE));
10922 && (h->root.type == bfd_link_hash_defined
10923 || h->root.type == bfd_link_hash_defweak))
10927 section = h->root.u.def.section;
10928 indx = section->output_section->target_index;
10929 *rel_hash_ptr = NULL;
10930 /* It seems that we ought to add the symbol value to the
10931 addend here, but in practice it has already been added
10932 because it was passed to constructor_callback. */
10933 addend += section->output_section->vma + section->output_offset;
10935 else if (h != NULL)
10937 /* Setting the index to -2 tells elf_link_output_extsym that
10938 this symbol is used by a reloc. */
10945 (*info->callbacks->unattached_reloc)
10946 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
10951 /* If this is an inplace reloc, we must write the addend into the
10953 if (howto->partial_inplace && addend != 0)
10955 bfd_size_type size;
10956 bfd_reloc_status_type rstat;
10959 const char *sym_name;
10961 size = (bfd_size_type) bfd_get_reloc_size (howto);
10962 buf = (bfd_byte *) bfd_zmalloc (size);
10963 if (buf == NULL && size != 0)
10965 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10972 case bfd_reloc_outofrange:
10975 case bfd_reloc_overflow:
10976 if (link_order->type == bfd_section_reloc_link_order)
10977 sym_name = bfd_section_name (output_bfd,
10978 link_order->u.reloc.p->u.section);
10980 sym_name = link_order->u.reloc.p->u.name;
10981 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
10982 howto->name, addend, NULL, NULL,
10987 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10989 * bfd_octets_per_byte (output_bfd),
10996 /* The address of a reloc is relative to the section in a
10997 relocatable file, and is a virtual address in an executable
10999 offset = link_order->offset;
11000 if (! bfd_link_relocatable (info))
11001 offset += output_section->vma;
11003 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11005 irel[i].r_offset = offset;
11006 irel[i].r_info = 0;
11007 irel[i].r_addend = 0;
11009 if (bed->s->arch_size == 32)
11010 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11012 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11014 rel_hdr = reldata->hdr;
11015 erel = rel_hdr->contents;
11016 if (rel_hdr->sh_type == SHT_REL)
11018 erel += reldata->count * bed->s->sizeof_rel;
11019 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11023 irel[0].r_addend = addend;
11024 erel += reldata->count * bed->s->sizeof_rela;
11025 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11034 /* Get the output vma of the section pointed to by the sh_link field. */
11037 elf_get_linked_section_vma (struct bfd_link_order *p)
11039 Elf_Internal_Shdr **elf_shdrp;
11043 s = p->u.indirect.section;
11044 elf_shdrp = elf_elfsections (s->owner);
11045 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
11046 elfsec = elf_shdrp[elfsec]->sh_link;
11048 The Intel C compiler generates SHT_IA_64_UNWIND with
11049 SHF_LINK_ORDER. But it doesn't set the sh_link or
11050 sh_info fields. Hence we could get the situation
11051 where elfsec is 0. */
11054 const struct elf_backend_data *bed
11055 = get_elf_backend_data (s->owner);
11056 if (bed->link_order_error_handler)
11057 bed->link_order_error_handler
11058 /* xgettext:c-format */
11059 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
11064 s = elf_shdrp[elfsec]->bfd_section;
11065 return s->output_section->vma + s->output_offset;
11070 /* Compare two sections based on the locations of the sections they are
11071 linked to. Used by elf_fixup_link_order. */
11074 compare_link_order (const void * a, const void * b)
11079 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
11080 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
11083 return apos > bpos;
11087 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11088 order as their linked sections. Returns false if this could not be done
11089 because an output section includes both ordered and unordered
11090 sections. Ideally we'd do this in the linker proper. */
11093 elf_fixup_link_order (bfd *abfd, asection *o)
11095 int seen_linkorder;
11098 struct bfd_link_order *p;
11100 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11102 struct bfd_link_order **sections;
11103 asection *s, *other_sec, *linkorder_sec;
11107 linkorder_sec = NULL;
11109 seen_linkorder = 0;
11110 for (p = o->map_head.link_order; p != NULL; p = p->next)
11112 if (p->type == bfd_indirect_link_order)
11114 s = p->u.indirect.section;
11116 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11117 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
11118 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
11119 && elfsec < elf_numsections (sub)
11120 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
11121 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
11135 if (seen_other && seen_linkorder)
11137 if (other_sec && linkorder_sec)
11139 /* xgettext:c-format */
11140 (_("%A has both ordered [`%A' in %B] "
11141 "and unordered [`%A' in %B] sections"),
11143 linkorder_sec->owner, other_sec,
11147 (_("%A has both ordered and unordered sections"), o);
11148 bfd_set_error (bfd_error_bad_value);
11153 if (!seen_linkorder)
11156 sections = (struct bfd_link_order **)
11157 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
11158 if (sections == NULL)
11160 seen_linkorder = 0;
11162 for (p = o->map_head.link_order; p != NULL; p = p->next)
11164 sections[seen_linkorder++] = p;
11166 /* Sort the input sections in the order of their linked section. */
11167 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
11168 compare_link_order);
11170 /* Change the offsets of the sections. */
11172 for (n = 0; n < seen_linkorder; n++)
11174 s = sections[n]->u.indirect.section;
11175 offset &= ~(bfd_vma) 0 << s->alignment_power;
11176 s->output_offset = offset / bfd_octets_per_byte (abfd);
11177 sections[n]->offset = offset;
11178 offset += sections[n]->size;
11185 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11186 Returns TRUE upon success, FALSE otherwise. */
11189 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11191 bfd_boolean ret = FALSE;
11193 const struct elf_backend_data *bed;
11195 enum bfd_architecture arch;
11197 asymbol **sympp = NULL;
11201 elf_symbol_type *osymbuf;
11203 implib_bfd = info->out_implib_bfd;
11204 bed = get_elf_backend_data (abfd);
11206 if (!bfd_set_format (implib_bfd, bfd_object))
11209 flags = bfd_get_file_flags (abfd);
11210 flags &= ~HAS_RELOC;
11211 if (!bfd_set_start_address (implib_bfd, 0)
11212 || !bfd_set_file_flags (implib_bfd, flags))
11215 /* Copy architecture of output file to import library file. */
11216 arch = bfd_get_arch (abfd);
11217 mach = bfd_get_mach (abfd);
11218 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11219 && (abfd->target_defaulted
11220 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11223 /* Get symbol table size. */
11224 symsize = bfd_get_symtab_upper_bound (abfd);
11228 /* Read in the symbol table. */
11229 sympp = (asymbol **) xmalloc (symsize);
11230 symcount = bfd_canonicalize_symtab (abfd, sympp);
11234 /* Allow the BFD backend to copy any private header data it
11235 understands from the output BFD to the import library BFD. */
11236 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11239 /* Filter symbols to appear in the import library. */
11240 if (bed->elf_backend_filter_implib_symbols)
11241 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11244 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11247 bfd_set_error (bfd_error_no_symbols);
11248 _bfd_error_handler (_("%B: no symbol found for import library"),
11254 /* Make symbols absolute. */
11255 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11256 sizeof (*osymbuf));
11257 for (src_count = 0; src_count < symcount; src_count++)
11259 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11260 sizeof (*osymbuf));
11261 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11262 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11263 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11264 osymbuf[src_count].internal_elf_sym.st_value =
11265 osymbuf[src_count].symbol.value;
11266 sympp[src_count] = &osymbuf[src_count].symbol;
11269 bfd_set_symtab (implib_bfd, sympp, symcount);
11271 /* Allow the BFD backend to copy any private data it understands
11272 from the output BFD to the import library BFD. This is done last
11273 to permit the routine to look at the filtered symbol table. */
11274 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11277 if (!bfd_close (implib_bfd))
11288 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11292 if (flinfo->symstrtab != NULL)
11293 _bfd_elf_strtab_free (flinfo->symstrtab);
11294 if (flinfo->contents != NULL)
11295 free (flinfo->contents);
11296 if (flinfo->external_relocs != NULL)
11297 free (flinfo->external_relocs);
11298 if (flinfo->internal_relocs != NULL)
11299 free (flinfo->internal_relocs);
11300 if (flinfo->external_syms != NULL)
11301 free (flinfo->external_syms);
11302 if (flinfo->locsym_shndx != NULL)
11303 free (flinfo->locsym_shndx);
11304 if (flinfo->internal_syms != NULL)
11305 free (flinfo->internal_syms);
11306 if (flinfo->indices != NULL)
11307 free (flinfo->indices);
11308 if (flinfo->sections != NULL)
11309 free (flinfo->sections);
11310 if (flinfo->symshndxbuf != NULL)
11311 free (flinfo->symshndxbuf);
11312 for (o = obfd->sections; o != NULL; o = o->next)
11314 struct bfd_elf_section_data *esdo = elf_section_data (o);
11315 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11316 free (esdo->rel.hashes);
11317 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11318 free (esdo->rela.hashes);
11322 /* Do the final step of an ELF link. */
11325 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11327 bfd_boolean dynamic;
11328 bfd_boolean emit_relocs;
11330 struct elf_final_link_info flinfo;
11332 struct bfd_link_order *p;
11334 bfd_size_type max_contents_size;
11335 bfd_size_type max_external_reloc_size;
11336 bfd_size_type max_internal_reloc_count;
11337 bfd_size_type max_sym_count;
11338 bfd_size_type max_sym_shndx_count;
11339 Elf_Internal_Sym elfsym;
11341 Elf_Internal_Shdr *symtab_hdr;
11342 Elf_Internal_Shdr *symtab_shndx_hdr;
11343 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11344 struct elf_outext_info eoinfo;
11345 bfd_boolean merged;
11346 size_t relativecount = 0;
11347 asection *reldyn = 0;
11349 asection *attr_section = NULL;
11350 bfd_vma attr_size = 0;
11351 const char *std_attrs_section;
11352 struct elf_link_hash_table *htab = elf_hash_table (info);
11354 if (!is_elf_hash_table (htab))
11357 if (bfd_link_pic (info))
11358 abfd->flags |= DYNAMIC;
11360 dynamic = htab->dynamic_sections_created;
11361 dynobj = htab->dynobj;
11363 emit_relocs = (bfd_link_relocatable (info)
11364 || info->emitrelocations);
11366 flinfo.info = info;
11367 flinfo.output_bfd = abfd;
11368 flinfo.symstrtab = _bfd_elf_strtab_init ();
11369 if (flinfo.symstrtab == NULL)
11374 flinfo.hash_sec = NULL;
11375 flinfo.symver_sec = NULL;
11379 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11380 /* Note that dynsym_sec can be NULL (on VMS). */
11381 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11382 /* Note that it is OK if symver_sec is NULL. */
11385 flinfo.contents = NULL;
11386 flinfo.external_relocs = NULL;
11387 flinfo.internal_relocs = NULL;
11388 flinfo.external_syms = NULL;
11389 flinfo.locsym_shndx = NULL;
11390 flinfo.internal_syms = NULL;
11391 flinfo.indices = NULL;
11392 flinfo.sections = NULL;
11393 flinfo.symshndxbuf = NULL;
11394 flinfo.filesym_count = 0;
11396 /* The object attributes have been merged. Remove the input
11397 sections from the link, and set the contents of the output
11399 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11400 for (o = abfd->sections; o != NULL; o = o->next)
11402 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11403 || strcmp (o->name, ".gnu.attributes") == 0)
11405 for (p = o->map_head.link_order; p != NULL; p = p->next)
11407 asection *input_section;
11409 if (p->type != bfd_indirect_link_order)
11411 input_section = p->u.indirect.section;
11412 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11413 elf_link_input_bfd ignores this section. */
11414 input_section->flags &= ~SEC_HAS_CONTENTS;
11417 attr_size = bfd_elf_obj_attr_size (abfd);
11420 bfd_set_section_size (abfd, o, attr_size);
11422 /* Skip this section later on. */
11423 o->map_head.link_order = NULL;
11426 o->flags |= SEC_EXCLUDE;
11430 /* Count up the number of relocations we will output for each output
11431 section, so that we know the sizes of the reloc sections. We
11432 also figure out some maximum sizes. */
11433 max_contents_size = 0;
11434 max_external_reloc_size = 0;
11435 max_internal_reloc_count = 0;
11437 max_sym_shndx_count = 0;
11439 for (o = abfd->sections; o != NULL; o = o->next)
11441 struct bfd_elf_section_data *esdo = elf_section_data (o);
11442 o->reloc_count = 0;
11444 for (p = o->map_head.link_order; p != NULL; p = p->next)
11446 unsigned int reloc_count = 0;
11447 unsigned int additional_reloc_count = 0;
11448 struct bfd_elf_section_data *esdi = NULL;
11450 if (p->type == bfd_section_reloc_link_order
11451 || p->type == bfd_symbol_reloc_link_order)
11453 else if (p->type == bfd_indirect_link_order)
11457 sec = p->u.indirect.section;
11459 /* Mark all sections which are to be included in the
11460 link. This will normally be every section. We need
11461 to do this so that we can identify any sections which
11462 the linker has decided to not include. */
11463 sec->linker_mark = TRUE;
11465 if (sec->flags & SEC_MERGE)
11468 if (sec->rawsize > max_contents_size)
11469 max_contents_size = sec->rawsize;
11470 if (sec->size > max_contents_size)
11471 max_contents_size = sec->size;
11473 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11474 && (sec->owner->flags & DYNAMIC) == 0)
11478 /* We are interested in just local symbols, not all
11480 if (elf_bad_symtab (sec->owner))
11481 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11482 / bed->s->sizeof_sym);
11484 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11486 if (sym_count > max_sym_count)
11487 max_sym_count = sym_count;
11489 if (sym_count > max_sym_shndx_count
11490 && elf_symtab_shndx_list (sec->owner) != NULL)
11491 max_sym_shndx_count = sym_count;
11493 if (esdo->this_hdr.sh_type == SHT_REL
11494 || esdo->this_hdr.sh_type == SHT_RELA)
11495 /* Some backends use reloc_count in relocation sections
11496 to count particular types of relocs. Of course,
11497 reloc sections themselves can't have relocations. */
11499 else if (emit_relocs)
11501 reloc_count = sec->reloc_count;
11502 if (bed->elf_backend_count_additional_relocs)
11505 c = (*bed->elf_backend_count_additional_relocs) (sec);
11506 additional_reloc_count += c;
11509 else if (bed->elf_backend_count_relocs)
11510 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11512 esdi = elf_section_data (sec);
11514 if ((sec->flags & SEC_RELOC) != 0)
11516 size_t ext_size = 0;
11518 if (esdi->rel.hdr != NULL)
11519 ext_size = esdi->rel.hdr->sh_size;
11520 if (esdi->rela.hdr != NULL)
11521 ext_size += esdi->rela.hdr->sh_size;
11523 if (ext_size > max_external_reloc_size)
11524 max_external_reloc_size = ext_size;
11525 if (sec->reloc_count > max_internal_reloc_count)
11526 max_internal_reloc_count = sec->reloc_count;
11531 if (reloc_count == 0)
11534 reloc_count += additional_reloc_count;
11535 o->reloc_count += reloc_count;
11537 if (p->type == bfd_indirect_link_order && emit_relocs)
11541 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11542 esdo->rel.count += additional_reloc_count;
11544 if (esdi->rela.hdr)
11546 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11547 esdo->rela.count += additional_reloc_count;
11553 esdo->rela.count += reloc_count;
11555 esdo->rel.count += reloc_count;
11559 if (o->reloc_count > 0)
11560 o->flags |= SEC_RELOC;
11563 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11564 set it (this is probably a bug) and if it is set
11565 assign_section_numbers will create a reloc section. */
11566 o->flags &=~ SEC_RELOC;
11569 /* If the SEC_ALLOC flag is not set, force the section VMA to
11570 zero. This is done in elf_fake_sections as well, but forcing
11571 the VMA to 0 here will ensure that relocs against these
11572 sections are handled correctly. */
11573 if ((o->flags & SEC_ALLOC) == 0
11574 && ! o->user_set_vma)
11578 if (! bfd_link_relocatable (info) && merged)
11579 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
11581 /* Figure out the file positions for everything but the symbol table
11582 and the relocs. We set symcount to force assign_section_numbers
11583 to create a symbol table. */
11584 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11585 BFD_ASSERT (! abfd->output_has_begun);
11586 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11589 /* Set sizes, and assign file positions for reloc sections. */
11590 for (o = abfd->sections; o != NULL; o = o->next)
11592 struct bfd_elf_section_data *esdo = elf_section_data (o);
11593 if ((o->flags & SEC_RELOC) != 0)
11596 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11600 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11604 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11605 to count upwards while actually outputting the relocations. */
11606 esdo->rel.count = 0;
11607 esdo->rela.count = 0;
11609 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11611 /* Cache the section contents so that they can be compressed
11612 later. Use bfd_malloc since it will be freed by
11613 bfd_compress_section_contents. */
11614 unsigned char *contents = esdo->this_hdr.contents;
11615 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11618 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11619 if (contents == NULL)
11621 esdo->this_hdr.contents = contents;
11625 /* We have now assigned file positions for all the sections except
11626 .symtab, .strtab, and non-loaded reloc sections. We start the
11627 .symtab section at the current file position, and write directly
11628 to it. We build the .strtab section in memory. */
11629 bfd_get_symcount (abfd) = 0;
11630 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11631 /* sh_name is set in prep_headers. */
11632 symtab_hdr->sh_type = SHT_SYMTAB;
11633 /* sh_flags, sh_addr and sh_size all start off zero. */
11634 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11635 /* sh_link is set in assign_section_numbers. */
11636 /* sh_info is set below. */
11637 /* sh_offset is set just below. */
11638 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11640 if (max_sym_count < 20)
11641 max_sym_count = 20;
11642 htab->strtabsize = max_sym_count;
11643 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11644 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
11645 if (htab->strtab == NULL)
11647 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11649 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11650 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11652 if (info->strip != strip_all || emit_relocs)
11654 file_ptr off = elf_next_file_pos (abfd);
11656 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11658 /* Note that at this point elf_next_file_pos (abfd) is
11659 incorrect. We do not yet know the size of the .symtab section.
11660 We correct next_file_pos below, after we do know the size. */
11662 /* Start writing out the symbol table. The first symbol is always a
11664 elfsym.st_value = 0;
11665 elfsym.st_size = 0;
11666 elfsym.st_info = 0;
11667 elfsym.st_other = 0;
11668 elfsym.st_shndx = SHN_UNDEF;
11669 elfsym.st_target_internal = 0;
11670 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11671 bfd_und_section_ptr, NULL) != 1)
11674 /* Output a symbol for each section. We output these even if we are
11675 discarding local symbols, since they are used for relocs. These
11676 symbols have no names. We store the index of each one in the
11677 index field of the section, so that we can find it again when
11678 outputting relocs. */
11680 elfsym.st_size = 0;
11681 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11682 elfsym.st_other = 0;
11683 elfsym.st_value = 0;
11684 elfsym.st_target_internal = 0;
11685 for (i = 1; i < elf_numsections (abfd); i++)
11687 o = bfd_section_from_elf_index (abfd, i);
11690 o->target_index = bfd_get_symcount (abfd);
11691 elfsym.st_shndx = i;
11692 if (!bfd_link_relocatable (info))
11693 elfsym.st_value = o->vma;
11694 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11701 /* Allocate some memory to hold information read in from the input
11703 if (max_contents_size != 0)
11705 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11706 if (flinfo.contents == NULL)
11710 if (max_external_reloc_size != 0)
11712 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11713 if (flinfo.external_relocs == NULL)
11717 if (max_internal_reloc_count != 0)
11719 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11720 amt *= sizeof (Elf_Internal_Rela);
11721 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11722 if (flinfo.internal_relocs == NULL)
11726 if (max_sym_count != 0)
11728 amt = max_sym_count * bed->s->sizeof_sym;
11729 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11730 if (flinfo.external_syms == NULL)
11733 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11734 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11735 if (flinfo.internal_syms == NULL)
11738 amt = max_sym_count * sizeof (long);
11739 flinfo.indices = (long int *) bfd_malloc (amt);
11740 if (flinfo.indices == NULL)
11743 amt = max_sym_count * sizeof (asection *);
11744 flinfo.sections = (asection **) bfd_malloc (amt);
11745 if (flinfo.sections == NULL)
11749 if (max_sym_shndx_count != 0)
11751 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11752 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11753 if (flinfo.locsym_shndx == NULL)
11759 bfd_vma base, end = 0;
11762 for (sec = htab->tls_sec;
11763 sec && (sec->flags & SEC_THREAD_LOCAL);
11766 bfd_size_type size = sec->size;
11769 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11771 struct bfd_link_order *ord = sec->map_tail.link_order;
11774 size = ord->offset + ord->size;
11776 end = sec->vma + size;
11778 base = htab->tls_sec->vma;
11779 /* Only align end of TLS section if static TLS doesn't have special
11780 alignment requirements. */
11781 if (bed->static_tls_alignment == 1)
11782 end = align_power (end, htab->tls_sec->alignment_power);
11783 htab->tls_size = end - base;
11786 /* Reorder SHF_LINK_ORDER sections. */
11787 for (o = abfd->sections; o != NULL; o = o->next)
11789 if (!elf_fixup_link_order (abfd, o))
11793 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11796 /* Since ELF permits relocations to be against local symbols, we
11797 must have the local symbols available when we do the relocations.
11798 Since we would rather only read the local symbols once, and we
11799 would rather not keep them in memory, we handle all the
11800 relocations for a single input file at the same time.
11802 Unfortunately, there is no way to know the total number of local
11803 symbols until we have seen all of them, and the local symbol
11804 indices precede the global symbol indices. This means that when
11805 we are generating relocatable output, and we see a reloc against
11806 a global symbol, we can not know the symbol index until we have
11807 finished examining all the local symbols to see which ones we are
11808 going to output. To deal with this, we keep the relocations in
11809 memory, and don't output them until the end of the link. This is
11810 an unfortunate waste of memory, but I don't see a good way around
11811 it. Fortunately, it only happens when performing a relocatable
11812 link, which is not the common case. FIXME: If keep_memory is set
11813 we could write the relocs out and then read them again; I don't
11814 know how bad the memory loss will be. */
11816 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11817 sub->output_has_begun = FALSE;
11818 for (o = abfd->sections; o != NULL; o = o->next)
11820 for (p = o->map_head.link_order; p != NULL; p = p->next)
11822 if (p->type == bfd_indirect_link_order
11823 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11824 == bfd_target_elf_flavour)
11825 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11827 if (! sub->output_has_begun)
11829 if (! elf_link_input_bfd (&flinfo, sub))
11831 sub->output_has_begun = TRUE;
11834 else if (p->type == bfd_section_reloc_link_order
11835 || p->type == bfd_symbol_reloc_link_order)
11837 if (! elf_reloc_link_order (abfd, info, o, p))
11842 if (! _bfd_default_link_order (abfd, info, o, p))
11844 if (p->type == bfd_indirect_link_order
11845 && (bfd_get_flavour (sub)
11846 == bfd_target_elf_flavour)
11847 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11848 != bed->s->elfclass))
11850 const char *iclass, *oclass;
11852 switch (bed->s->elfclass)
11854 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11855 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11856 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11860 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11862 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11863 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11864 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11868 bfd_set_error (bfd_error_wrong_format);
11870 /* xgettext:c-format */
11871 (_("%B: file class %s incompatible with %s"),
11872 sub, iclass, oclass);
11881 /* Free symbol buffer if needed. */
11882 if (!info->reduce_memory_overheads)
11884 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11885 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11886 && elf_tdata (sub)->symbuf)
11888 free (elf_tdata (sub)->symbuf);
11889 elf_tdata (sub)->symbuf = NULL;
11893 /* Output any global symbols that got converted to local in a
11894 version script or due to symbol visibility. We do this in a
11895 separate step since ELF requires all local symbols to appear
11896 prior to any global symbols. FIXME: We should only do this if
11897 some global symbols were, in fact, converted to become local.
11898 FIXME: Will this work correctly with the Irix 5 linker? */
11899 eoinfo.failed = FALSE;
11900 eoinfo.flinfo = &flinfo;
11901 eoinfo.localsyms = TRUE;
11902 eoinfo.file_sym_done = FALSE;
11903 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11907 /* If backend needs to output some local symbols not present in the hash
11908 table, do it now. */
11909 if (bed->elf_backend_output_arch_local_syms
11910 && (info->strip != strip_all || emit_relocs))
11912 typedef int (*out_sym_func)
11913 (void *, const char *, Elf_Internal_Sym *, asection *,
11914 struct elf_link_hash_entry *);
11916 if (! ((*bed->elf_backend_output_arch_local_syms)
11917 (abfd, info, &flinfo,
11918 (out_sym_func) elf_link_output_symstrtab)))
11922 /* That wrote out all the local symbols. Finish up the symbol table
11923 with the global symbols. Even if we want to strip everything we
11924 can, we still need to deal with those global symbols that got
11925 converted to local in a version script. */
11927 /* The sh_info field records the index of the first non local symbol. */
11928 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11931 && htab->dynsym != NULL
11932 && htab->dynsym->output_section != bfd_abs_section_ptr)
11934 Elf_Internal_Sym sym;
11935 bfd_byte *dynsym = htab->dynsym->contents;
11937 o = htab->dynsym->output_section;
11938 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
11940 /* Write out the section symbols for the output sections. */
11941 if (bfd_link_pic (info)
11942 || htab->is_relocatable_executable)
11948 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11950 sym.st_target_internal = 0;
11952 for (s = abfd->sections; s != NULL; s = s->next)
11958 dynindx = elf_section_data (s)->dynindx;
11961 indx = elf_section_data (s)->this_idx;
11962 BFD_ASSERT (indx > 0);
11963 sym.st_shndx = indx;
11964 if (! check_dynsym (abfd, &sym))
11966 sym.st_value = s->vma;
11967 dest = dynsym + dynindx * bed->s->sizeof_sym;
11968 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11972 /* Write out the local dynsyms. */
11973 if (htab->dynlocal)
11975 struct elf_link_local_dynamic_entry *e;
11976 for (e = htab->dynlocal; e ; e = e->next)
11981 /* Copy the internal symbol and turn off visibility.
11982 Note that we saved a word of storage and overwrote
11983 the original st_name with the dynstr_index. */
11985 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11987 s = bfd_section_from_elf_index (e->input_bfd,
11992 elf_section_data (s->output_section)->this_idx;
11993 if (! check_dynsym (abfd, &sym))
11995 sym.st_value = (s->output_section->vma
11997 + e->isym.st_value);
12000 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12001 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12006 /* We get the global symbols from the hash table. */
12007 eoinfo.failed = FALSE;
12008 eoinfo.localsyms = FALSE;
12009 eoinfo.flinfo = &flinfo;
12010 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12014 /* If backend needs to output some symbols not present in the hash
12015 table, do it now. */
12016 if (bed->elf_backend_output_arch_syms
12017 && (info->strip != strip_all || emit_relocs))
12019 typedef int (*out_sym_func)
12020 (void *, const char *, Elf_Internal_Sym *, asection *,
12021 struct elf_link_hash_entry *);
12023 if (! ((*bed->elf_backend_output_arch_syms)
12024 (abfd, info, &flinfo,
12025 (out_sym_func) elf_link_output_symstrtab)))
12029 /* Finalize the .strtab section. */
12030 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12032 /* Swap out the .strtab section. */
12033 if (!elf_link_swap_symbols_out (&flinfo))
12036 /* Now we know the size of the symtab section. */
12037 if (bfd_get_symcount (abfd) > 0)
12039 /* Finish up and write out the symbol string table (.strtab)
12041 Elf_Internal_Shdr *symstrtab_hdr;
12042 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12044 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12045 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12047 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12048 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12049 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12050 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12051 symtab_shndx_hdr->sh_size = amt;
12053 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12056 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12057 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12061 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12062 /* sh_name was set in prep_headers. */
12063 symstrtab_hdr->sh_type = SHT_STRTAB;
12064 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12065 symstrtab_hdr->sh_addr = 0;
12066 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12067 symstrtab_hdr->sh_entsize = 0;
12068 symstrtab_hdr->sh_link = 0;
12069 symstrtab_hdr->sh_info = 0;
12070 /* sh_offset is set just below. */
12071 symstrtab_hdr->sh_addralign = 1;
12073 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12075 elf_next_file_pos (abfd) = off;
12077 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12078 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12082 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12084 _bfd_error_handler (_("%B: failed to generate import library"),
12085 info->out_implib_bfd);
12089 /* Adjust the relocs to have the correct symbol indices. */
12090 for (o = abfd->sections; o != NULL; o = o->next)
12092 struct bfd_elf_section_data *esdo = elf_section_data (o);
12094 if ((o->flags & SEC_RELOC) == 0)
12097 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12098 if (esdo->rel.hdr != NULL
12099 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort))
12101 if (esdo->rela.hdr != NULL
12102 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort))
12105 /* Set the reloc_count field to 0 to prevent write_relocs from
12106 trying to swap the relocs out itself. */
12107 o->reloc_count = 0;
12110 if (dynamic && info->combreloc && dynobj != NULL)
12111 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12113 /* If we are linking against a dynamic object, or generating a
12114 shared library, finish up the dynamic linking information. */
12117 bfd_byte *dyncon, *dynconend;
12119 /* Fix up .dynamic entries. */
12120 o = bfd_get_linker_section (dynobj, ".dynamic");
12121 BFD_ASSERT (o != NULL);
12123 dyncon = o->contents;
12124 dynconend = o->contents + o->size;
12125 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12127 Elf_Internal_Dyn dyn;
12130 bfd_size_type sh_size;
12133 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12140 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12142 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12144 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12145 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12148 dyn.d_un.d_val = relativecount;
12155 name = info->init_function;
12158 name = info->fini_function;
12161 struct elf_link_hash_entry *h;
12163 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12165 && (h->root.type == bfd_link_hash_defined
12166 || h->root.type == bfd_link_hash_defweak))
12168 dyn.d_un.d_ptr = h->root.u.def.value;
12169 o = h->root.u.def.section;
12170 if (o->output_section != NULL)
12171 dyn.d_un.d_ptr += (o->output_section->vma
12172 + o->output_offset);
12175 /* The symbol is imported from another shared
12176 library and does not apply to this one. */
12177 dyn.d_un.d_ptr = 0;
12184 case DT_PREINIT_ARRAYSZ:
12185 name = ".preinit_array";
12187 case DT_INIT_ARRAYSZ:
12188 name = ".init_array";
12190 case DT_FINI_ARRAYSZ:
12191 name = ".fini_array";
12193 o = bfd_get_section_by_name (abfd, name);
12197 (_("could not find section %s"), name);
12202 (_("warning: %s section has zero size"), name);
12203 dyn.d_un.d_val = o->size;
12206 case DT_PREINIT_ARRAY:
12207 name = ".preinit_array";
12209 case DT_INIT_ARRAY:
12210 name = ".init_array";
12212 case DT_FINI_ARRAY:
12213 name = ".fini_array";
12215 o = bfd_get_section_by_name (abfd, name);
12222 name = ".gnu.hash";
12231 name = ".gnu.version_d";
12234 name = ".gnu.version_r";
12237 name = ".gnu.version";
12239 o = bfd_get_linker_section (dynobj, name);
12244 (_("could not find section %s"), name);
12247 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12250 (_("warning: section '%s' is being made into a note"), name);
12251 bfd_set_error (bfd_error_nonrepresentable_section);
12254 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12261 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12267 for (i = 1; i < elf_numsections (abfd); i++)
12269 Elf_Internal_Shdr *hdr;
12271 hdr = elf_elfsections (abfd)[i];
12272 if (hdr->sh_type == type
12273 && (hdr->sh_flags & SHF_ALLOC) != 0)
12275 sh_size += hdr->sh_size;
12277 || sh_addr > hdr->sh_addr)
12278 sh_addr = hdr->sh_addr;
12282 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12284 /* Don't count procedure linkage table relocs in the
12285 overall reloc count. */
12286 sh_size -= htab->srelplt->size;
12288 /* If the size is zero, make the address zero too.
12289 This is to avoid a glibc bug. If the backend
12290 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12291 zero, then we'll put DT_RELA at the end of
12292 DT_JMPREL. glibc will interpret the end of
12293 DT_RELA matching the end of DT_JMPREL as the
12294 case where DT_RELA includes DT_JMPREL, and for
12295 LD_BIND_NOW will decide that processing DT_RELA
12296 will process the PLT relocs too. Net result:
12297 No PLT relocs applied. */
12300 /* If .rela.plt is the first .rela section, exclude
12301 it from DT_RELA. */
12302 else if (sh_addr == (htab->srelplt->output_section->vma
12303 + htab->srelplt->output_offset))
12304 sh_addr += htab->srelplt->size;
12307 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12308 dyn.d_un.d_val = sh_size;
12310 dyn.d_un.d_ptr = sh_addr;
12313 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12317 /* If we have created any dynamic sections, then output them. */
12318 if (dynobj != NULL)
12320 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12323 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12324 if (((info->warn_shared_textrel && bfd_link_pic (info))
12325 || info->error_textrel)
12326 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12328 bfd_byte *dyncon, *dynconend;
12330 dyncon = o->contents;
12331 dynconend = o->contents + o->size;
12332 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12334 Elf_Internal_Dyn dyn;
12336 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12338 if (dyn.d_tag == DT_TEXTREL)
12340 if (info->error_textrel)
12341 info->callbacks->einfo
12342 (_("%P%X: read-only segment has dynamic relocations.\n"));
12344 info->callbacks->einfo
12345 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
12351 for (o = dynobj->sections; o != NULL; o = o->next)
12353 if ((o->flags & SEC_HAS_CONTENTS) == 0
12355 || o->output_section == bfd_abs_section_ptr)
12357 if ((o->flags & SEC_LINKER_CREATED) == 0)
12359 /* At this point, we are only interested in sections
12360 created by _bfd_elf_link_create_dynamic_sections. */
12363 if (htab->stab_info.stabstr == o)
12365 if (htab->eh_info.hdr_sec == o)
12367 if (strcmp (o->name, ".dynstr") != 0)
12369 if (! bfd_set_section_contents (abfd, o->output_section,
12371 (file_ptr) o->output_offset
12372 * bfd_octets_per_byte (abfd),
12378 /* The contents of the .dynstr section are actually in a
12382 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12383 if (bfd_seek (abfd, off, SEEK_SET) != 0
12384 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12390 if (bfd_link_relocatable (info))
12392 bfd_boolean failed = FALSE;
12394 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12399 /* If we have optimized stabs strings, output them. */
12400 if (htab->stab_info.stabstr != NULL)
12402 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12406 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12409 elf_final_link_free (abfd, &flinfo);
12411 elf_linker (abfd) = TRUE;
12415 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12416 if (contents == NULL)
12417 return FALSE; /* Bail out and fail. */
12418 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12419 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12426 elf_final_link_free (abfd, &flinfo);
12430 /* Initialize COOKIE for input bfd ABFD. */
12433 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12434 struct bfd_link_info *info, bfd *abfd)
12436 Elf_Internal_Shdr *symtab_hdr;
12437 const struct elf_backend_data *bed;
12439 bed = get_elf_backend_data (abfd);
12440 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12442 cookie->abfd = abfd;
12443 cookie->sym_hashes = elf_sym_hashes (abfd);
12444 cookie->bad_symtab = elf_bad_symtab (abfd);
12445 if (cookie->bad_symtab)
12447 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12448 cookie->extsymoff = 0;
12452 cookie->locsymcount = symtab_hdr->sh_info;
12453 cookie->extsymoff = symtab_hdr->sh_info;
12456 if (bed->s->arch_size == 32)
12457 cookie->r_sym_shift = 8;
12459 cookie->r_sym_shift = 32;
12461 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12462 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12464 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12465 cookie->locsymcount, 0,
12467 if (cookie->locsyms == NULL)
12469 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12472 if (info->keep_memory)
12473 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12478 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12481 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12483 Elf_Internal_Shdr *symtab_hdr;
12485 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12486 if (cookie->locsyms != NULL
12487 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12488 free (cookie->locsyms);
12491 /* Initialize the relocation information in COOKIE for input section SEC
12492 of input bfd ABFD. */
12495 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12496 struct bfd_link_info *info, bfd *abfd,
12499 const struct elf_backend_data *bed;
12501 if (sec->reloc_count == 0)
12503 cookie->rels = NULL;
12504 cookie->relend = NULL;
12508 bed = get_elf_backend_data (abfd);
12510 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12511 info->keep_memory);
12512 if (cookie->rels == NULL)
12514 cookie->rel = cookie->rels;
12515 cookie->relend = (cookie->rels
12516 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12518 cookie->rel = cookie->rels;
12522 /* Free the memory allocated by init_reloc_cookie_rels,
12526 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12529 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12530 free (cookie->rels);
12533 /* Initialize the whole of COOKIE for input section SEC. */
12536 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12537 struct bfd_link_info *info,
12540 if (!init_reloc_cookie (cookie, info, sec->owner))
12542 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12547 fini_reloc_cookie (cookie, sec->owner);
12552 /* Free the memory allocated by init_reloc_cookie_for_section,
12556 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12559 fini_reloc_cookie_rels (cookie, sec);
12560 fini_reloc_cookie (cookie, sec->owner);
12563 /* Garbage collect unused sections. */
12565 /* Default gc_mark_hook. */
12568 _bfd_elf_gc_mark_hook (asection *sec,
12569 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12570 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12571 struct elf_link_hash_entry *h,
12572 Elf_Internal_Sym *sym)
12576 switch (h->root.type)
12578 case bfd_link_hash_defined:
12579 case bfd_link_hash_defweak:
12580 return h->root.u.def.section;
12582 case bfd_link_hash_common:
12583 return h->root.u.c.p->section;
12590 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12595 /* For undefined __start_<name> and __stop_<name> symbols, return the
12596 first input section matching <name>. Return NULL otherwise. */
12599 _bfd_elf_is_start_stop (const struct bfd_link_info *info,
12600 struct elf_link_hash_entry *h)
12603 const char *sec_name;
12605 if (h->root.type != bfd_link_hash_undefined
12606 && h->root.type != bfd_link_hash_undefweak)
12609 s = h->root.u.undef.section;
12612 if (s == (asection *) 0 - 1)
12618 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12619 sec_name = h->root.root.string + 8;
12620 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12621 sec_name = h->root.root.string + 7;
12623 if (sec_name != NULL && *sec_name != '\0')
12627 for (i = info->input_bfds; i != NULL; i = i->link.next)
12629 s = bfd_get_section_by_name (i, sec_name);
12632 h->root.u.undef.section = s;
12639 h->root.u.undef.section = (asection *) 0 - 1;
12644 /* COOKIE->rel describes a relocation against section SEC, which is
12645 a section we've decided to keep. Return the section that contains
12646 the relocation symbol, or NULL if no section contains it. */
12649 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12650 elf_gc_mark_hook_fn gc_mark_hook,
12651 struct elf_reloc_cookie *cookie,
12652 bfd_boolean *start_stop)
12654 unsigned long r_symndx;
12655 struct elf_link_hash_entry *h;
12657 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12658 if (r_symndx == STN_UNDEF)
12661 if (r_symndx >= cookie->locsymcount
12662 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12664 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12667 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12671 while (h->root.type == bfd_link_hash_indirect
12672 || h->root.type == bfd_link_hash_warning)
12673 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12675 /* If this symbol is weak and there is a non-weak definition, we
12676 keep the non-weak definition because many backends put
12677 dynamic reloc info on the non-weak definition for code
12678 handling copy relocs. */
12679 if (h->u.weakdef != NULL)
12680 h->u.weakdef->mark = 1;
12682 if (start_stop != NULL)
12684 /* To work around a glibc bug, mark all XXX input sections
12685 when there is an as yet undefined reference to __start_XXX
12686 or __stop_XXX symbols. The linker will later define such
12687 symbols for orphan input sections that have a name
12688 representable as a C identifier. */
12689 asection *s = _bfd_elf_is_start_stop (info, h);
12693 *start_stop = !s->gc_mark;
12698 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12701 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12702 &cookie->locsyms[r_symndx]);
12705 /* COOKIE->rel describes a relocation against section SEC, which is
12706 a section we've decided to keep. Mark the section that contains
12707 the relocation symbol. */
12710 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12712 elf_gc_mark_hook_fn gc_mark_hook,
12713 struct elf_reloc_cookie *cookie)
12716 bfd_boolean start_stop = FALSE;
12718 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12719 while (rsec != NULL)
12721 if (!rsec->gc_mark)
12723 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12724 || (rsec->owner->flags & DYNAMIC) != 0)
12726 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12731 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12736 /* The mark phase of garbage collection. For a given section, mark
12737 it and any sections in this section's group, and all the sections
12738 which define symbols to which it refers. */
12741 _bfd_elf_gc_mark (struct bfd_link_info *info,
12743 elf_gc_mark_hook_fn gc_mark_hook)
12746 asection *group_sec, *eh_frame;
12750 /* Mark all the sections in the group. */
12751 group_sec = elf_section_data (sec)->next_in_group;
12752 if (group_sec && !group_sec->gc_mark)
12753 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12756 /* Look through the section relocs. */
12758 eh_frame = elf_eh_frame_section (sec->owner);
12759 if ((sec->flags & SEC_RELOC) != 0
12760 && sec->reloc_count > 0
12761 && sec != eh_frame)
12763 struct elf_reloc_cookie cookie;
12765 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12769 for (; cookie.rel < cookie.relend; cookie.rel++)
12770 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12775 fini_reloc_cookie_for_section (&cookie, sec);
12779 if (ret && eh_frame && elf_fde_list (sec))
12781 struct elf_reloc_cookie cookie;
12783 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12787 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12788 gc_mark_hook, &cookie))
12790 fini_reloc_cookie_for_section (&cookie, eh_frame);
12794 eh_frame = elf_section_eh_frame_entry (sec);
12795 if (ret && eh_frame && !eh_frame->gc_mark)
12796 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12802 /* Scan and mark sections in a special or debug section group. */
12805 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12807 /* Point to first section of section group. */
12809 /* Used to iterate the section group. */
12812 bfd_boolean is_special_grp = TRUE;
12813 bfd_boolean is_debug_grp = TRUE;
12815 /* First scan to see if group contains any section other than debug
12816 and special section. */
12817 ssec = msec = elf_next_in_group (grp);
12820 if ((msec->flags & SEC_DEBUGGING) == 0)
12821 is_debug_grp = FALSE;
12823 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12824 is_special_grp = FALSE;
12826 msec = elf_next_in_group (msec);
12828 while (msec != ssec);
12830 /* If this is a pure debug section group or pure special section group,
12831 keep all sections in this group. */
12832 if (is_debug_grp || is_special_grp)
12837 msec = elf_next_in_group (msec);
12839 while (msec != ssec);
12843 /* Keep debug and special sections. */
12846 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12847 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12851 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12854 bfd_boolean some_kept;
12855 bfd_boolean debug_frag_seen;
12857 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12860 /* Ensure all linker created sections are kept,
12861 see if any other section is already marked,
12862 and note if we have any fragmented debug sections. */
12863 debug_frag_seen = some_kept = FALSE;
12864 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12866 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12868 else if (isec->gc_mark)
12871 if (debug_frag_seen == FALSE
12872 && (isec->flags & SEC_DEBUGGING)
12873 && CONST_STRNEQ (isec->name, ".debug_line."))
12874 debug_frag_seen = TRUE;
12877 /* If no section in this file will be kept, then we can
12878 toss out the debug and special sections. */
12882 /* Keep debug and special sections like .comment when they are
12883 not part of a group. Also keep section groups that contain
12884 just debug sections or special sections. */
12885 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12887 if ((isec->flags & SEC_GROUP) != 0)
12888 _bfd_elf_gc_mark_debug_special_section_group (isec);
12889 else if (((isec->flags & SEC_DEBUGGING) != 0
12890 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12891 && elf_next_in_group (isec) == NULL)
12895 if (! debug_frag_seen)
12898 /* Look for CODE sections which are going to be discarded,
12899 and find and discard any fragmented debug sections which
12900 are associated with that code section. */
12901 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12902 if ((isec->flags & SEC_CODE) != 0
12903 && isec->gc_mark == 0)
12908 ilen = strlen (isec->name);
12910 /* Association is determined by the name of the debug section
12911 containing the name of the code section as a suffix. For
12912 example .debug_line.text.foo is a debug section associated
12914 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12918 if (dsec->gc_mark == 0
12919 || (dsec->flags & SEC_DEBUGGING) == 0)
12922 dlen = strlen (dsec->name);
12925 && strncmp (dsec->name + (dlen - ilen),
12926 isec->name, ilen) == 0)
12936 /* The sweep phase of garbage collection. Remove all garbage sections. */
12938 typedef bfd_boolean (*gc_sweep_hook_fn)
12939 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12942 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12945 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12946 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12948 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12952 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12953 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12956 for (o = sub->sections; o != NULL; o = o->next)
12958 /* When any section in a section group is kept, we keep all
12959 sections in the section group. If the first member of
12960 the section group is excluded, we will also exclude the
12962 if (o->flags & SEC_GROUP)
12964 asection *first = elf_next_in_group (o);
12965 o->gc_mark = first->gc_mark;
12971 /* Skip sweeping sections already excluded. */
12972 if (o->flags & SEC_EXCLUDE)
12975 /* Since this is early in the link process, it is simple
12976 to remove a section from the output. */
12977 o->flags |= SEC_EXCLUDE;
12979 if (info->print_gc_sections && o->size != 0)
12980 /* xgettext:c-format */
12981 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"),
12984 /* But we also have to update some of the relocation
12985 info we collected before. */
12987 && (o->flags & SEC_RELOC) != 0
12988 && o->reloc_count != 0
12989 && !((info->strip == strip_all || info->strip == strip_debugger)
12990 && (o->flags & SEC_DEBUGGING) != 0)
12991 && !bfd_is_abs_section (o->output_section))
12993 Elf_Internal_Rela *internal_relocs;
12997 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12998 info->keep_memory);
12999 if (internal_relocs == NULL)
13002 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
13004 if (elf_section_data (o)->relocs != internal_relocs)
13005 free (internal_relocs);
13016 /* Propagate collected vtable information. This is called through
13017 elf_link_hash_traverse. */
13020 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13022 /* Those that are not vtables. */
13023 if (h->vtable == NULL || h->vtable->parent == NULL)
13026 /* Those vtables that do not have parents, we cannot merge. */
13027 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
13030 /* If we've already been done, exit. */
13031 if (h->vtable->used && h->vtable->used[-1])
13034 /* Make sure the parent's table is up to date. */
13035 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
13037 if (h->vtable->used == NULL)
13039 /* None of this table's entries were referenced. Re-use the
13041 h->vtable->used = h->vtable->parent->vtable->used;
13042 h->vtable->size = h->vtable->parent->vtable->size;
13047 bfd_boolean *cu, *pu;
13049 /* Or the parent's entries into ours. */
13050 cu = h->vtable->used;
13052 pu = h->vtable->parent->vtable->used;
13055 const struct elf_backend_data *bed;
13056 unsigned int log_file_align;
13058 bed = get_elf_backend_data (h->root.u.def.section->owner);
13059 log_file_align = bed->s->log_file_align;
13060 n = h->vtable->parent->vtable->size >> log_file_align;
13075 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13078 bfd_vma hstart, hend;
13079 Elf_Internal_Rela *relstart, *relend, *rel;
13080 const struct elf_backend_data *bed;
13081 unsigned int log_file_align;
13083 /* Take care of both those symbols that do not describe vtables as
13084 well as those that are not loaded. */
13085 if (h->vtable == NULL || h->vtable->parent == NULL)
13088 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13089 || h->root.type == bfd_link_hash_defweak);
13091 sec = h->root.u.def.section;
13092 hstart = h->root.u.def.value;
13093 hend = hstart + h->size;
13095 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13097 return *(bfd_boolean *) okp = FALSE;
13098 bed = get_elf_backend_data (sec->owner);
13099 log_file_align = bed->s->log_file_align;
13101 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
13103 for (rel = relstart; rel < relend; ++rel)
13104 if (rel->r_offset >= hstart && rel->r_offset < hend)
13106 /* If the entry is in use, do nothing. */
13107 if (h->vtable->used
13108 && (rel->r_offset - hstart) < h->vtable->size)
13110 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13111 if (h->vtable->used[entry])
13114 /* Otherwise, kill it. */
13115 rel->r_offset = rel->r_info = rel->r_addend = 0;
13121 /* Mark sections containing dynamically referenced symbols. When
13122 building shared libraries, we must assume that any visible symbol is
13126 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13128 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13129 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13131 if ((h->root.type == bfd_link_hash_defined
13132 || h->root.type == bfd_link_hash_defweak)
13134 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13135 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13136 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13137 && (!bfd_link_executable (info)
13138 || info->gc_keep_exported
13139 || info->export_dynamic
13142 && (*d->match) (&d->head, NULL, h->root.root.string)))
13143 && (h->versioned >= versioned
13144 || !bfd_hide_sym_by_version (info->version_info,
13145 h->root.root.string)))))
13146 h->root.u.def.section->flags |= SEC_KEEP;
13151 /* Keep all sections containing symbols undefined on the command-line,
13152 and the section containing the entry symbol. */
13155 _bfd_elf_gc_keep (struct bfd_link_info *info)
13157 struct bfd_sym_chain *sym;
13159 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13161 struct elf_link_hash_entry *h;
13163 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13164 FALSE, FALSE, FALSE);
13167 && (h->root.type == bfd_link_hash_defined
13168 || h->root.type == bfd_link_hash_defweak)
13169 && !bfd_is_abs_section (h->root.u.def.section)
13170 && !bfd_is_und_section (h->root.u.def.section))
13171 h->root.u.def.section->flags |= SEC_KEEP;
13176 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13177 struct bfd_link_info *info)
13179 bfd *ibfd = info->input_bfds;
13181 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13184 struct elf_reloc_cookie cookie;
13186 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13189 if (!init_reloc_cookie (&cookie, info, ibfd))
13192 for (sec = ibfd->sections; sec; sec = sec->next)
13194 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
13195 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13197 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13198 fini_reloc_cookie_rels (&cookie, sec);
13205 /* Do mark and sweep of unused sections. */
13208 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13210 bfd_boolean ok = TRUE;
13212 elf_gc_mark_hook_fn gc_mark_hook;
13213 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13214 struct elf_link_hash_table *htab;
13216 if (!bed->can_gc_sections
13217 || !is_elf_hash_table (info->hash))
13219 _bfd_error_handler(_("Warning: gc-sections option ignored"));
13223 bed->gc_keep (info);
13224 htab = elf_hash_table (info);
13226 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13227 at the .eh_frame section if we can mark the FDEs individually. */
13228 for (sub = info->input_bfds;
13229 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13230 sub = sub->link.next)
13233 struct elf_reloc_cookie cookie;
13235 sec = bfd_get_section_by_name (sub, ".eh_frame");
13236 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13238 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13239 if (elf_section_data (sec)->sec_info
13240 && (sec->flags & SEC_LINKER_CREATED) == 0)
13241 elf_eh_frame_section (sub) = sec;
13242 fini_reloc_cookie_for_section (&cookie, sec);
13243 sec = bfd_get_next_section_by_name (NULL, sec);
13247 /* Apply transitive closure to the vtable entry usage info. */
13248 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13252 /* Kill the vtable relocations that were not used. */
13253 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13257 /* Mark dynamically referenced symbols. */
13258 if (htab->dynamic_sections_created || info->gc_keep_exported)
13259 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13261 /* Grovel through relocs to find out who stays ... */
13262 gc_mark_hook = bed->gc_mark_hook;
13263 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13267 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13268 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13271 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13272 Also treat note sections as a root, if the section is not part
13274 for (o = sub->sections; o != NULL; o = o->next)
13276 && (o->flags & SEC_EXCLUDE) == 0
13277 && ((o->flags & SEC_KEEP) != 0
13278 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13279 && elf_next_in_group (o) == NULL )))
13281 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13286 /* Allow the backend to mark additional target specific sections. */
13287 bed->gc_mark_extra_sections (info, gc_mark_hook);
13289 /* ... and mark SEC_EXCLUDE for those that go. */
13290 return elf_gc_sweep (abfd, info);
13293 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13296 bfd_elf_gc_record_vtinherit (bfd *abfd,
13298 struct elf_link_hash_entry *h,
13301 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13302 struct elf_link_hash_entry **search, *child;
13303 size_t extsymcount;
13304 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13306 /* The sh_info field of the symtab header tells us where the
13307 external symbols start. We don't care about the local symbols at
13309 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13310 if (!elf_bad_symtab (abfd))
13311 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13313 sym_hashes = elf_sym_hashes (abfd);
13314 sym_hashes_end = sym_hashes + extsymcount;
13316 /* Hunt down the child symbol, which is in this section at the same
13317 offset as the relocation. */
13318 for (search = sym_hashes; search != sym_hashes_end; ++search)
13320 if ((child = *search) != NULL
13321 && (child->root.type == bfd_link_hash_defined
13322 || child->root.type == bfd_link_hash_defweak)
13323 && child->root.u.def.section == sec
13324 && child->root.u.def.value == offset)
13328 /* xgettext:c-format */
13329 _bfd_error_handler (_("%B: %A+%lu: No symbol found for INHERIT"),
13330 abfd, sec, (unsigned long) offset);
13331 bfd_set_error (bfd_error_invalid_operation);
13335 if (!child->vtable)
13337 child->vtable = ((struct elf_link_virtual_table_entry *)
13338 bfd_zalloc (abfd, sizeof (*child->vtable)));
13339 if (!child->vtable)
13344 /* This *should* only be the absolute section. It could potentially
13345 be that someone has defined a non-global vtable though, which
13346 would be bad. It isn't worth paging in the local symbols to be
13347 sure though; that case should simply be handled by the assembler. */
13349 child->vtable->parent = (struct elf_link_hash_entry *) -1;
13352 child->vtable->parent = h;
13357 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13360 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
13361 asection *sec ATTRIBUTE_UNUSED,
13362 struct elf_link_hash_entry *h,
13365 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13366 unsigned int log_file_align = bed->s->log_file_align;
13370 h->vtable = ((struct elf_link_virtual_table_entry *)
13371 bfd_zalloc (abfd, sizeof (*h->vtable)));
13376 if (addend >= h->vtable->size)
13378 size_t size, bytes, file_align;
13379 bfd_boolean *ptr = h->vtable->used;
13381 /* While the symbol is undefined, we have to be prepared to handle
13383 file_align = 1 << log_file_align;
13384 if (h->root.type == bfd_link_hash_undefined)
13385 size = addend + file_align;
13389 if (addend >= size)
13391 /* Oops! We've got a reference past the defined end of
13392 the table. This is probably a bug -- shall we warn? */
13393 size = addend + file_align;
13396 size = (size + file_align - 1) & -file_align;
13398 /* Allocate one extra entry for use as a "done" flag for the
13399 consolidation pass. */
13400 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13404 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13410 oldbytes = (((h->vtable->size >> log_file_align) + 1)
13411 * sizeof (bfd_boolean));
13412 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13416 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13421 /* And arrange for that done flag to be at index -1. */
13422 h->vtable->used = ptr + 1;
13423 h->vtable->size = size;
13426 h->vtable->used[addend >> log_file_align] = TRUE;
13431 /* Map an ELF section header flag to its corresponding string. */
13435 flagword flag_value;
13436 } elf_flags_to_name_table;
13438 static elf_flags_to_name_table elf_flags_to_names [] =
13440 { "SHF_WRITE", SHF_WRITE },
13441 { "SHF_ALLOC", SHF_ALLOC },
13442 { "SHF_EXECINSTR", SHF_EXECINSTR },
13443 { "SHF_MERGE", SHF_MERGE },
13444 { "SHF_STRINGS", SHF_STRINGS },
13445 { "SHF_INFO_LINK", SHF_INFO_LINK},
13446 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13447 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13448 { "SHF_GROUP", SHF_GROUP },
13449 { "SHF_TLS", SHF_TLS },
13450 { "SHF_MASKOS", SHF_MASKOS },
13451 { "SHF_EXCLUDE", SHF_EXCLUDE },
13454 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13456 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13457 struct flag_info *flaginfo,
13460 const bfd_vma sh_flags = elf_section_flags (section);
13462 if (!flaginfo->flags_initialized)
13464 bfd *obfd = info->output_bfd;
13465 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13466 struct flag_info_list *tf = flaginfo->flag_list;
13468 int without_hex = 0;
13470 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13473 flagword (*lookup) (char *);
13475 lookup = bed->elf_backend_lookup_section_flags_hook;
13476 if (lookup != NULL)
13478 flagword hexval = (*lookup) ((char *) tf->name);
13482 if (tf->with == with_flags)
13483 with_hex |= hexval;
13484 else if (tf->with == without_flags)
13485 without_hex |= hexval;
13490 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13492 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13494 if (tf->with == with_flags)
13495 with_hex |= elf_flags_to_names[i].flag_value;
13496 else if (tf->with == without_flags)
13497 without_hex |= elf_flags_to_names[i].flag_value;
13504 info->callbacks->einfo
13505 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13509 flaginfo->flags_initialized = TRUE;
13510 flaginfo->only_with_flags |= with_hex;
13511 flaginfo->not_with_flags |= without_hex;
13514 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13517 if ((flaginfo->not_with_flags & sh_flags) != 0)
13523 struct alloc_got_off_arg {
13525 struct bfd_link_info *info;
13528 /* We need a special top-level link routine to convert got reference counts
13529 to real got offsets. */
13532 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13534 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13535 bfd *obfd = gofarg->info->output_bfd;
13536 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13538 if (h->got.refcount > 0)
13540 h->got.offset = gofarg->gotoff;
13541 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13544 h->got.offset = (bfd_vma) -1;
13549 /* And an accompanying bit to work out final got entry offsets once
13550 we're done. Should be called from final_link. */
13553 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13554 struct bfd_link_info *info)
13557 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13559 struct alloc_got_off_arg gofarg;
13561 BFD_ASSERT (abfd == info->output_bfd);
13563 if (! is_elf_hash_table (info->hash))
13566 /* The GOT offset is relative to the .got section, but the GOT header is
13567 put into the .got.plt section, if the backend uses it. */
13568 if (bed->want_got_plt)
13571 gotoff = bed->got_header_size;
13573 /* Do the local .got entries first. */
13574 for (i = info->input_bfds; i; i = i->link.next)
13576 bfd_signed_vma *local_got;
13577 size_t j, locsymcount;
13578 Elf_Internal_Shdr *symtab_hdr;
13580 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13583 local_got = elf_local_got_refcounts (i);
13587 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13588 if (elf_bad_symtab (i))
13589 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13591 locsymcount = symtab_hdr->sh_info;
13593 for (j = 0; j < locsymcount; ++j)
13595 if (local_got[j] > 0)
13597 local_got[j] = gotoff;
13598 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13601 local_got[j] = (bfd_vma) -1;
13605 /* Then the global .got entries. .plt refcounts are handled by
13606 adjust_dynamic_symbol */
13607 gofarg.gotoff = gotoff;
13608 gofarg.info = info;
13609 elf_link_hash_traverse (elf_hash_table (info),
13610 elf_gc_allocate_got_offsets,
13615 /* Many folk need no more in the way of final link than this, once
13616 got entry reference counting is enabled. */
13619 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13621 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13624 /* Invoke the regular ELF backend linker to do all the work. */
13625 return bfd_elf_final_link (abfd, info);
13629 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13631 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13633 if (rcookie->bad_symtab)
13634 rcookie->rel = rcookie->rels;
13636 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13638 unsigned long r_symndx;
13640 if (! rcookie->bad_symtab)
13641 if (rcookie->rel->r_offset > offset)
13643 if (rcookie->rel->r_offset != offset)
13646 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13647 if (r_symndx == STN_UNDEF)
13650 if (r_symndx >= rcookie->locsymcount
13651 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13653 struct elf_link_hash_entry *h;
13655 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13657 while (h->root.type == bfd_link_hash_indirect
13658 || h->root.type == bfd_link_hash_warning)
13659 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13661 if ((h->root.type == bfd_link_hash_defined
13662 || h->root.type == bfd_link_hash_defweak)
13663 && (h->root.u.def.section->owner != rcookie->abfd
13664 || h->root.u.def.section->kept_section != NULL
13665 || discarded_section (h->root.u.def.section)))
13670 /* It's not a relocation against a global symbol,
13671 but it could be a relocation against a local
13672 symbol for a discarded section. */
13674 Elf_Internal_Sym *isym;
13676 /* Need to: get the symbol; get the section. */
13677 isym = &rcookie->locsyms[r_symndx];
13678 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13680 && (isec->kept_section != NULL
13681 || discarded_section (isec)))
13689 /* Discard unneeded references to discarded sections.
13690 Returns -1 on error, 1 if any section's size was changed, 0 if
13691 nothing changed. This function assumes that the relocations are in
13692 sorted order, which is true for all known assemblers. */
13695 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13697 struct elf_reloc_cookie cookie;
13702 if (info->traditional_format
13703 || !is_elf_hash_table (info->hash))
13706 o = bfd_get_section_by_name (output_bfd, ".stab");
13711 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13714 || i->reloc_count == 0
13715 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13719 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13722 if (!init_reloc_cookie_for_section (&cookie, info, i))
13725 if (_bfd_discard_section_stabs (abfd, i,
13726 elf_section_data (i)->sec_info,
13727 bfd_elf_reloc_symbol_deleted_p,
13731 fini_reloc_cookie_for_section (&cookie, i);
13736 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13737 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13742 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13748 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13751 if (!init_reloc_cookie_for_section (&cookie, info, i))
13754 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13755 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13756 bfd_elf_reloc_symbol_deleted_p,
13760 fini_reloc_cookie_for_section (&cookie, i);
13764 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13766 const struct elf_backend_data *bed;
13768 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13771 bed = get_elf_backend_data (abfd);
13773 if (bed->elf_backend_discard_info != NULL)
13775 if (!init_reloc_cookie (&cookie, info, abfd))
13778 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13781 fini_reloc_cookie (&cookie, abfd);
13785 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13786 _bfd_elf_end_eh_frame_parsing (info);
13788 if (info->eh_frame_hdr_type
13789 && !bfd_link_relocatable (info)
13790 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13797 _bfd_elf_section_already_linked (bfd *abfd,
13799 struct bfd_link_info *info)
13802 const char *name, *key;
13803 struct bfd_section_already_linked *l;
13804 struct bfd_section_already_linked_hash_entry *already_linked_list;
13806 if (sec->output_section == bfd_abs_section_ptr)
13809 flags = sec->flags;
13811 /* Return if it isn't a linkonce section. A comdat group section
13812 also has SEC_LINK_ONCE set. */
13813 if ((flags & SEC_LINK_ONCE) == 0)
13816 /* Don't put group member sections on our list of already linked
13817 sections. They are handled as a group via their group section. */
13818 if (elf_sec_group (sec) != NULL)
13821 /* For a SHT_GROUP section, use the group signature as the key. */
13823 if ((flags & SEC_GROUP) != 0
13824 && elf_next_in_group (sec) != NULL
13825 && elf_group_name (elf_next_in_group (sec)) != NULL)
13826 key = elf_group_name (elf_next_in_group (sec));
13829 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13830 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13831 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13834 /* Must be a user linkonce section that doesn't follow gcc's
13835 naming convention. In this case we won't be matching
13836 single member groups. */
13840 already_linked_list = bfd_section_already_linked_table_lookup (key);
13842 for (l = already_linked_list->entry; l != NULL; l = l->next)
13844 /* We may have 2 different types of sections on the list: group
13845 sections with a signature of <key> (<key> is some string),
13846 and linkonce sections named .gnu.linkonce.<type>.<key>.
13847 Match like sections. LTO plugin sections are an exception.
13848 They are always named .gnu.linkonce.t.<key> and match either
13849 type of section. */
13850 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13851 && ((flags & SEC_GROUP) != 0
13852 || strcmp (name, l->sec->name) == 0))
13853 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13855 /* The section has already been linked. See if we should
13856 issue a warning. */
13857 if (!_bfd_handle_already_linked (sec, l, info))
13860 if (flags & SEC_GROUP)
13862 asection *first = elf_next_in_group (sec);
13863 asection *s = first;
13867 s->output_section = bfd_abs_section_ptr;
13868 /* Record which group discards it. */
13869 s->kept_section = l->sec;
13870 s = elf_next_in_group (s);
13871 /* These lists are circular. */
13881 /* A single member comdat group section may be discarded by a
13882 linkonce section and vice versa. */
13883 if ((flags & SEC_GROUP) != 0)
13885 asection *first = elf_next_in_group (sec);
13887 if (first != NULL && elf_next_in_group (first) == first)
13888 /* Check this single member group against linkonce sections. */
13889 for (l = already_linked_list->entry; l != NULL; l = l->next)
13890 if ((l->sec->flags & SEC_GROUP) == 0
13891 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13893 first->output_section = bfd_abs_section_ptr;
13894 first->kept_section = l->sec;
13895 sec->output_section = bfd_abs_section_ptr;
13900 /* Check this linkonce section against single member groups. */
13901 for (l = already_linked_list->entry; l != NULL; l = l->next)
13902 if (l->sec->flags & SEC_GROUP)
13904 asection *first = elf_next_in_group (l->sec);
13907 && elf_next_in_group (first) == first
13908 && bfd_elf_match_symbols_in_sections (first, sec, info))
13910 sec->output_section = bfd_abs_section_ptr;
13911 sec->kept_section = first;
13916 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13917 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13918 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13919 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13920 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13921 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13922 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13923 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13924 The reverse order cannot happen as there is never a bfd with only the
13925 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13926 matter as here were are looking only for cross-bfd sections. */
13928 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13929 for (l = already_linked_list->entry; l != NULL; l = l->next)
13930 if ((l->sec->flags & SEC_GROUP) == 0
13931 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13933 if (abfd != l->sec->owner)
13934 sec->output_section = bfd_abs_section_ptr;
13938 /* This is the first section with this name. Record it. */
13939 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13940 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13941 return sec->output_section == bfd_abs_section_ptr;
13945 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13947 return sym->st_shndx == SHN_COMMON;
13951 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13957 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13959 return bfd_com_section_ptr;
13963 _bfd_elf_default_got_elt_size (bfd *abfd,
13964 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13965 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13966 bfd *ibfd ATTRIBUTE_UNUSED,
13967 unsigned long symndx ATTRIBUTE_UNUSED)
13969 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13970 return bed->s->arch_size / 8;
13973 /* Routines to support the creation of dynamic relocs. */
13975 /* Returns the name of the dynamic reloc section associated with SEC. */
13977 static const char *
13978 get_dynamic_reloc_section_name (bfd * abfd,
13980 bfd_boolean is_rela)
13983 const char *old_name = bfd_get_section_name (NULL, sec);
13984 const char *prefix = is_rela ? ".rela" : ".rel";
13986 if (old_name == NULL)
13989 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13990 sprintf (name, "%s%s", prefix, old_name);
13995 /* Returns the dynamic reloc section associated with SEC.
13996 If necessary compute the name of the dynamic reloc section based
13997 on SEC's name (looked up in ABFD's string table) and the setting
14001 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14003 bfd_boolean is_rela)
14005 asection * reloc_sec = elf_section_data (sec)->sreloc;
14007 if (reloc_sec == NULL)
14009 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14013 reloc_sec = bfd_get_linker_section (abfd, name);
14015 if (reloc_sec != NULL)
14016 elf_section_data (sec)->sreloc = reloc_sec;
14023 /* Returns the dynamic reloc section associated with SEC. If the
14024 section does not exist it is created and attached to the DYNOBJ
14025 bfd and stored in the SRELOC field of SEC's elf_section_data
14028 ALIGNMENT is the alignment for the newly created section and
14029 IS_RELA defines whether the name should be .rela.<SEC's name>
14030 or .rel.<SEC's name>. The section name is looked up in the
14031 string table associated with ABFD. */
14034 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14036 unsigned int alignment,
14038 bfd_boolean is_rela)
14040 asection * reloc_sec = elf_section_data (sec)->sreloc;
14042 if (reloc_sec == NULL)
14044 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14049 reloc_sec = bfd_get_linker_section (dynobj, name);
14051 if (reloc_sec == NULL)
14053 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14054 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14055 if ((sec->flags & SEC_ALLOC) != 0)
14056 flags |= SEC_ALLOC | SEC_LOAD;
14058 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14059 if (reloc_sec != NULL)
14061 /* _bfd_elf_get_sec_type_attr chooses a section type by
14062 name. Override as it may be wrong, eg. for a user
14063 section named "auto" we'll get ".relauto" which is
14064 seen to be a .rela section. */
14065 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14066 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
14071 elf_section_data (sec)->sreloc = reloc_sec;
14077 /* Copy the ELF symbol type and other attributes for a linker script
14078 assignment from HSRC to HDEST. Generally this should be treated as
14079 if we found a strong non-dynamic definition for HDEST (except that
14080 ld ignores multiple definition errors). */
14082 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14083 struct bfd_link_hash_entry *hdest,
14084 struct bfd_link_hash_entry *hsrc)
14086 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14087 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14088 Elf_Internal_Sym isym;
14090 ehdest->type = ehsrc->type;
14091 ehdest->target_internal = ehsrc->target_internal;
14093 isym.st_other = ehsrc->other;
14094 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14097 /* Append a RELA relocation REL to section S in BFD. */
14100 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14102 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14103 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14104 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14105 bed->s->swap_reloca_out (abfd, rel, loc);
14108 /* Append a REL relocation REL to section S in BFD. */
14111 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14113 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14114 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14115 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14116 bed->s->swap_reloc_out (abfd, rel, loc);