1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2019 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. */
27 #include "safe-ctype.h"
28 #include "libiberty.h"
30 #if BFD_SUPPORTS_PLUGINS
31 #include "plugin-api.h"
35 /* This struct is used to pass information to routines called via
36 elf_link_hash_traverse which must return failure. */
38 struct elf_info_failed
40 struct bfd_link_info *info;
44 /* This structure is used to pass information to
45 _bfd_elf_link_find_version_dependencies. */
47 struct elf_find_verdep_info
49 /* General link information. */
50 struct bfd_link_info *info;
51 /* The number of dependencies. */
53 /* Whether we had a failure. */
57 static bfd_boolean _bfd_elf_fix_symbol_flags
58 (struct elf_link_hash_entry *, struct elf_info_failed *);
61 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
62 unsigned long r_symndx,
65 if (r_symndx >= cookie->locsymcount
66 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
68 struct elf_link_hash_entry *h;
70 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
72 while (h->root.type == bfd_link_hash_indirect
73 || h->root.type == bfd_link_hash_warning)
74 h = (struct elf_link_hash_entry *) h->root.u.i.link;
76 if ((h->root.type == bfd_link_hash_defined
77 || h->root.type == bfd_link_hash_defweak)
78 && discarded_section (h->root.u.def.section))
79 return h->root.u.def.section;
85 /* It's not a relocation against a global symbol,
86 but it could be a relocation against a local
87 symbol for a discarded section. */
89 Elf_Internal_Sym *isym;
91 /* Need to: get the symbol; get the section. */
92 isym = &cookie->locsyms[r_symndx];
93 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
95 && discard ? discarded_section (isec) : 1)
101 /* Define a symbol in a dynamic linkage section. */
103 struct elf_link_hash_entry *
104 _bfd_elf_define_linkage_sym (bfd *abfd,
105 struct bfd_link_info *info,
109 struct elf_link_hash_entry *h;
110 struct bfd_link_hash_entry *bh;
111 const struct elf_backend_data *bed;
113 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
116 /* Zap symbol defined in an as-needed lib that wasn't linked.
117 This is a symptom of a larger problem: Absolute symbols
118 defined in shared libraries can't be overridden, because we
119 lose the link to the bfd which is via the symbol section. */
120 h->root.type = bfd_link_hash_new;
126 bed = get_elf_backend_data (abfd);
127 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
128 sec, 0, NULL, FALSE, bed->collect,
131 h = (struct elf_link_hash_entry *) bh;
132 BFD_ASSERT (h != NULL);
135 h->root.linker_def = 1;
136 h->type = STT_OBJECT;
137 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
138 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
140 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
145 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
149 struct elf_link_hash_entry *h;
150 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
151 struct elf_link_hash_table *htab = elf_hash_table (info);
153 /* This function may be called more than once. */
154 if (htab->sgot != NULL)
157 flags = bed->dynamic_sec_flags;
159 s = bfd_make_section_anyway_with_flags (abfd,
160 (bed->rela_plts_and_copies_p
161 ? ".rela.got" : ".rel.got"),
162 (bed->dynamic_sec_flags
165 || !bfd_set_section_alignment (s, bed->s->log_file_align))
169 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
171 || !bfd_set_section_alignment (s, bed->s->log_file_align))
175 if (bed->want_got_plt)
177 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
179 || !bfd_set_section_alignment (s, bed->s->log_file_align))
184 /* The first bit of the global offset table is the header. */
185 s->size += bed->got_header_size;
187 if (bed->want_got_sym)
189 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
190 (or .got.plt) section. We don't do this in the linker script
191 because we don't want to define the symbol if we are not creating
192 a global offset table. */
193 h = _bfd_elf_define_linkage_sym (abfd, info, s,
194 "_GLOBAL_OFFSET_TABLE_");
195 elf_hash_table (info)->hgot = h;
203 /* Create a strtab to hold the dynamic symbol names. */
205 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
207 struct elf_link_hash_table *hash_table;
209 hash_table = elf_hash_table (info);
210 if (hash_table->dynobj == NULL)
212 /* We may not set dynobj, an input file holding linker created
213 dynamic sections to abfd, which may be a dynamic object with
214 its own dynamic sections. We need to find a normal input file
215 to hold linker created sections if possible. */
216 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
220 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
222 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
223 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
224 && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
225 && !((s = ibfd->sections) != NULL
226 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
232 hash_table->dynobj = abfd;
235 if (hash_table->dynstr == NULL)
237 hash_table->dynstr = _bfd_elf_strtab_init ();
238 if (hash_table->dynstr == NULL)
244 /* Create some sections which will be filled in with dynamic linking
245 information. ABFD is an input file which requires dynamic sections
246 to be created. The dynamic sections take up virtual memory space
247 when the final executable is run, so we need to create them before
248 addresses are assigned to the output sections. We work out the
249 actual contents and size of these sections later. */
252 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
256 const struct elf_backend_data *bed;
257 struct elf_link_hash_entry *h;
259 if (! is_elf_hash_table (info->hash))
262 if (elf_hash_table (info)->dynamic_sections_created)
265 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
268 abfd = elf_hash_table (info)->dynobj;
269 bed = get_elf_backend_data (abfd);
271 flags = bed->dynamic_sec_flags;
273 /* A dynamically linked executable has a .interp section, but a
274 shared library does not. */
275 if (bfd_link_executable (info) && !info->nointerp)
277 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
278 flags | SEC_READONLY);
283 /* Create sections to hold version informations. These are removed
284 if they are not needed. */
285 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
286 flags | SEC_READONLY);
288 || !bfd_set_section_alignment (s, bed->s->log_file_align))
291 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
292 flags | SEC_READONLY);
294 || !bfd_set_section_alignment (s, 1))
297 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
298 flags | SEC_READONLY);
300 || !bfd_set_section_alignment (s, bed->s->log_file_align))
303 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
304 flags | SEC_READONLY);
306 || !bfd_set_section_alignment (s, bed->s->log_file_align))
308 elf_hash_table (info)->dynsym = s;
310 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
311 flags | SEC_READONLY);
315 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
317 || !bfd_set_section_alignment (s, bed->s->log_file_align))
320 /* The special symbol _DYNAMIC is always set to the start of the
321 .dynamic section. We could set _DYNAMIC in a linker script, but we
322 only want to define it if we are, in fact, creating a .dynamic
323 section. We don't want to define it if there is no .dynamic
324 section, since on some ELF platforms the start up code examines it
325 to decide how to initialize the process. */
326 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
327 elf_hash_table (info)->hdynamic = h;
333 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
334 flags | SEC_READONLY);
336 || !bfd_set_section_alignment (s, bed->s->log_file_align))
338 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
341 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
343 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
344 flags | SEC_READONLY);
346 || !bfd_set_section_alignment (s, bed->s->log_file_align))
348 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
349 4 32-bit words followed by variable count of 64-bit words, then
350 variable count of 32-bit words. */
351 if (bed->s->arch_size == 64)
352 elf_section_data (s)->this_hdr.sh_entsize = 0;
354 elf_section_data (s)->this_hdr.sh_entsize = 4;
357 /* Let the backend create the rest of the sections. This lets the
358 backend set the right flags. The backend will normally create
359 the .got and .plt sections. */
360 if (bed->elf_backend_create_dynamic_sections == NULL
361 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
364 elf_hash_table (info)->dynamic_sections_created = TRUE;
369 /* Create dynamic sections when linking against a dynamic object. */
372 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
374 flagword flags, pltflags;
375 struct elf_link_hash_entry *h;
377 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
378 struct elf_link_hash_table *htab = elf_hash_table (info);
380 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
381 .rel[a].bss sections. */
382 flags = bed->dynamic_sec_flags;
385 if (bed->plt_not_loaded)
386 /* We do not clear SEC_ALLOC here because we still want the OS to
387 allocate space for the section; it's just that there's nothing
388 to read in from the object file. */
389 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
391 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
392 if (bed->plt_readonly)
393 pltflags |= SEC_READONLY;
395 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
397 || !bfd_set_section_alignment (s, bed->plt_alignment))
401 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
403 if (bed->want_plt_sym)
405 h = _bfd_elf_define_linkage_sym (abfd, info, s,
406 "_PROCEDURE_LINKAGE_TABLE_");
407 elf_hash_table (info)->hplt = h;
412 s = bfd_make_section_anyway_with_flags (abfd,
413 (bed->rela_plts_and_copies_p
414 ? ".rela.plt" : ".rel.plt"),
415 flags | SEC_READONLY);
417 || !bfd_set_section_alignment (s, bed->s->log_file_align))
421 if (! _bfd_elf_create_got_section (abfd, info))
424 if (bed->want_dynbss)
426 /* The .dynbss section is a place to put symbols which are defined
427 by dynamic objects, are referenced by regular objects, and are
428 not functions. We must allocate space for them in the process
429 image and use a R_*_COPY reloc to tell the dynamic linker to
430 initialize them at run time. The linker script puts the .dynbss
431 section into the .bss section of the final image. */
432 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
433 SEC_ALLOC | SEC_LINKER_CREATED);
438 if (bed->want_dynrelro)
440 /* Similarly, but for symbols that were originally in read-only
441 sections. This section doesn't really need to have contents,
442 but make it like other .data.rel.ro sections. */
443 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
450 /* The .rel[a].bss section holds copy relocs. This section is not
451 normally needed. We need to create it here, though, so that the
452 linker will map it to an output section. We can't just create it
453 only if we need it, because we will not know whether we need it
454 until we have seen all the input files, and the first time the
455 main linker code calls BFD after examining all the input files
456 (size_dynamic_sections) the input sections have already been
457 mapped to the output sections. If the section turns out not to
458 be needed, we can discard it later. We will never need this
459 section when generating a shared object, since they do not use
461 if (bfd_link_executable (info))
463 s = bfd_make_section_anyway_with_flags (abfd,
464 (bed->rela_plts_and_copies_p
465 ? ".rela.bss" : ".rel.bss"),
466 flags | SEC_READONLY);
468 || !bfd_set_section_alignment (s, bed->s->log_file_align))
472 if (bed->want_dynrelro)
474 s = (bfd_make_section_anyway_with_flags
475 (abfd, (bed->rela_plts_and_copies_p
476 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
477 flags | SEC_READONLY));
479 || !bfd_set_section_alignment (s, bed->s->log_file_align))
481 htab->sreldynrelro = s;
489 /* Record a new dynamic symbol. We record the dynamic symbols as we
490 read the input files, since we need to have a list of all of them
491 before we can determine the final sizes of the output sections.
492 Note that we may actually call this function even though we are not
493 going to output any dynamic symbols; in some cases we know that a
494 symbol should be in the dynamic symbol table, but only if there is
498 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
499 struct elf_link_hash_entry *h)
501 if (h->dynindx == -1)
503 struct elf_strtab_hash *dynstr;
508 /* XXX: The ABI draft says the linker must turn hidden and
509 internal symbols into STB_LOCAL symbols when producing the
510 DSO. However, if ld.so honors st_other in the dynamic table,
511 this would not be necessary. */
512 switch (ELF_ST_VISIBILITY (h->other))
516 if (h->root.type != bfd_link_hash_undefined
517 && h->root.type != bfd_link_hash_undefweak)
520 if (!elf_hash_table (info)->is_relocatable_executable)
528 h->dynindx = elf_hash_table (info)->dynsymcount;
529 ++elf_hash_table (info)->dynsymcount;
531 dynstr = elf_hash_table (info)->dynstr;
534 /* Create a strtab to hold the dynamic symbol names. */
535 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
540 /* We don't put any version information in the dynamic string
542 name = h->root.root.string;
543 p = strchr (name, ELF_VER_CHR);
545 /* We know that the p points into writable memory. In fact,
546 there are only a few symbols that have read-only names, being
547 those like _GLOBAL_OFFSET_TABLE_ that are created specially
548 by the backends. Most symbols will have names pointing into
549 an ELF string table read from a file, or to objalloc memory. */
552 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
557 if (indx == (size_t) -1)
559 h->dynstr_index = indx;
565 /* Mark a symbol dynamic. */
568 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
569 struct elf_link_hash_entry *h,
570 Elf_Internal_Sym *sym)
572 struct bfd_elf_dynamic_list *d = info->dynamic_list;
574 /* It may be called more than once on the same H. */
575 if(h->dynamic || bfd_link_relocatable (info))
578 if ((info->dynamic_data
579 && (h->type == STT_OBJECT
580 || h->type == STT_COMMON
582 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
583 || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
586 && (*d->match) (&d->head, NULL, h->root.root.string)))
589 /* NB: If a symbol is made dynamic by --dynamic-list, it has
591 h->root.non_ir_ref_dynamic = 1;
595 /* Record an assignment to a symbol made by a linker script. We need
596 this in case some dynamic object refers to this symbol. */
599 bfd_elf_record_link_assignment (bfd *output_bfd,
600 struct bfd_link_info *info,
605 struct elf_link_hash_entry *h, *hv;
606 struct elf_link_hash_table *htab;
607 const struct elf_backend_data *bed;
609 if (!is_elf_hash_table (info->hash))
612 htab = elf_hash_table (info);
613 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
617 if (h->root.type == bfd_link_hash_warning)
618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
620 if (h->versioned == unknown)
622 /* Set versioned if symbol version is unknown. */
623 char *version = strrchr (name, ELF_VER_CHR);
626 if (version > name && version[-1] != ELF_VER_CHR)
627 h->versioned = versioned_hidden;
629 h->versioned = versioned;
633 /* Symbols defined in a linker script but not referenced anywhere
634 else will have non_elf set. */
637 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
641 switch (h->root.type)
643 case bfd_link_hash_defined:
644 case bfd_link_hash_defweak:
645 case bfd_link_hash_common:
647 case bfd_link_hash_undefweak:
648 case bfd_link_hash_undefined:
649 /* Since we're defining the symbol, don't let it seem to have not
650 been defined. record_dynamic_symbol and size_dynamic_sections
651 may depend on this. */
652 h->root.type = bfd_link_hash_new;
653 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
654 bfd_link_repair_undef_list (&htab->root);
656 case bfd_link_hash_new:
658 case bfd_link_hash_indirect:
659 /* We had a versioned symbol in a dynamic library. We make the
660 the versioned symbol point to this one. */
661 bed = get_elf_backend_data (output_bfd);
663 while (hv->root.type == bfd_link_hash_indirect
664 || hv->root.type == bfd_link_hash_warning)
665 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
666 /* We don't need to update h->root.u since linker will set them
668 h->root.type = bfd_link_hash_undefined;
669 hv->root.type = bfd_link_hash_indirect;
670 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
671 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
678 /* If this symbol is being provided by the linker script, and it is
679 currently defined by a dynamic object, but not by a regular
680 object, then mark it as undefined so that the generic linker will
681 force the correct value. */
685 h->root.type = bfd_link_hash_undefined;
687 /* If this symbol is currently defined by a dynamic object, but not
688 by a regular object, then clear out any version information because
689 the symbol will not be associated with the dynamic object any
691 if (h->def_dynamic && !h->def_regular)
692 h->verinfo.verdef = NULL;
694 /* Make sure this symbol is not garbage collected. */
701 bed = get_elf_backend_data (output_bfd);
702 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
703 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
704 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
707 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
709 if (!bfd_link_relocatable (info)
711 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
712 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
717 || bfd_link_dll (info)
718 || elf_hash_table (info)->is_relocatable_executable)
722 if (! bfd_elf_link_record_dynamic_symbol (info, h))
725 /* If this is a weak defined symbol, and we know a corresponding
726 real symbol from the same dynamic object, make sure the real
727 symbol is also made into a dynamic symbol. */
730 struct elf_link_hash_entry *def = weakdef (h);
732 if (def->dynindx == -1
733 && !bfd_elf_link_record_dynamic_symbol (info, def))
741 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
742 success, and 2 on a failure caused by attempting to record a symbol
743 in a discarded section, eg. a discarded link-once section symbol. */
746 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
751 struct elf_link_local_dynamic_entry *entry;
752 struct elf_link_hash_table *eht;
753 struct elf_strtab_hash *dynstr;
756 Elf_External_Sym_Shndx eshndx;
757 char esym[sizeof (Elf64_External_Sym)];
759 if (! is_elf_hash_table (info->hash))
762 /* See if the entry exists already. */
763 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
764 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
767 amt = sizeof (*entry);
768 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
772 /* Go find the symbol, so that we can find it's name. */
773 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
774 1, input_indx, &entry->isym, esym, &eshndx))
776 bfd_release (input_bfd, entry);
780 if (entry->isym.st_shndx != SHN_UNDEF
781 && entry->isym.st_shndx < SHN_LORESERVE)
785 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
786 if (s == NULL || bfd_is_abs_section (s->output_section))
788 /* We can still bfd_release here as nothing has done another
789 bfd_alloc. We can't do this later in this function. */
790 bfd_release (input_bfd, entry);
795 name = (bfd_elf_string_from_elf_section
796 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
797 entry->isym.st_name));
799 dynstr = elf_hash_table (info)->dynstr;
802 /* Create a strtab to hold the dynamic symbol names. */
803 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
808 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
809 if (dynstr_index == (size_t) -1)
811 entry->isym.st_name = dynstr_index;
813 eht = elf_hash_table (info);
815 entry->next = eht->dynlocal;
816 eht->dynlocal = entry;
817 entry->input_bfd = input_bfd;
818 entry->input_indx = input_indx;
821 /* Whatever binding the symbol had before, it's now local. */
823 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
825 /* The dynindx will be set at the end of size_dynamic_sections. */
830 /* Return the dynindex of a local dynamic symbol. */
833 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
837 struct elf_link_local_dynamic_entry *e;
839 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
840 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
845 /* This function is used to renumber the dynamic symbols, if some of
846 them are removed because they are marked as local. This is called
847 via elf_link_hash_traverse. */
850 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
853 size_t *count = (size_t *) data;
858 if (h->dynindx != -1)
859 h->dynindx = ++(*count);
865 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
866 STB_LOCAL binding. */
869 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
872 size_t *count = (size_t *) data;
874 if (!h->forced_local)
877 if (h->dynindx != -1)
878 h->dynindx = ++(*count);
883 /* Return true if the dynamic symbol for a given section should be
884 omitted when creating a shared library. */
886 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
887 struct bfd_link_info *info,
890 struct elf_link_hash_table *htab;
893 switch (elf_section_data (p)->this_hdr.sh_type)
897 /* If sh_type is yet undecided, assume it could be
898 SHT_PROGBITS/SHT_NOBITS. */
900 htab = elf_hash_table (info);
901 if (htab->text_index_section != NULL)
902 return p != htab->text_index_section && p != htab->data_index_section;
904 return (htab->dynobj != NULL
905 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
906 && ip->output_section == p);
908 /* There shouldn't be section relative relocations
909 against any other section. */
916 _bfd_elf_omit_section_dynsym_all
917 (bfd *output_bfd ATTRIBUTE_UNUSED,
918 struct bfd_link_info *info ATTRIBUTE_UNUSED,
919 asection *p ATTRIBUTE_UNUSED)
924 /* Assign dynsym indices. In a shared library we generate a section
925 symbol for each output section, which come first. Next come symbols
926 which have been forced to local binding. Then all of the back-end
927 allocated local dynamic syms, followed by the rest of the global
928 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
929 (This prevents the early call before elf_backend_init_index_section
930 and strip_excluded_output_sections setting dynindx for sections
931 that are stripped.) */
934 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
935 struct bfd_link_info *info,
936 unsigned long *section_sym_count)
938 unsigned long dynsymcount = 0;
939 bfd_boolean do_sec = section_sym_count != NULL;
941 if (bfd_link_pic (info)
942 || elf_hash_table (info)->is_relocatable_executable)
944 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
946 for (p = output_bfd->sections; p ; p = p->next)
947 if ((p->flags & SEC_EXCLUDE) == 0
948 && (p->flags & SEC_ALLOC) != 0
949 && elf_hash_table (info)->dynamic_relocs
950 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
954 elf_section_data (p)->dynindx = dynsymcount;
957 elf_section_data (p)->dynindx = 0;
960 *section_sym_count = dynsymcount;
962 elf_link_hash_traverse (elf_hash_table (info),
963 elf_link_renumber_local_hash_table_dynsyms,
966 if (elf_hash_table (info)->dynlocal)
968 struct elf_link_local_dynamic_entry *p;
969 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
970 p->dynindx = ++dynsymcount;
972 elf_hash_table (info)->local_dynsymcount = dynsymcount;
974 elf_link_hash_traverse (elf_hash_table (info),
975 elf_link_renumber_hash_table_dynsyms,
978 /* There is an unused NULL entry at the head of the table which we
979 must account for in our count even if the table is empty since it
980 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
984 elf_hash_table (info)->dynsymcount = dynsymcount;
988 /* Merge st_other field. */
991 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
992 const Elf_Internal_Sym *isym, asection *sec,
993 bfd_boolean definition, bfd_boolean dynamic)
995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
997 /* If st_other has a processor-specific meaning, specific
998 code might be needed here. */
999 if (bed->elf_backend_merge_symbol_attribute)
1000 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
1005 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
1006 unsigned hvis = ELF_ST_VISIBILITY (h->other);
1008 /* Keep the most constraining visibility. Leave the remainder
1009 of the st_other field to elf_backend_merge_symbol_attribute. */
1010 if (symvis - 1 < hvis - 1)
1011 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
1014 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
1015 && (sec->flags & SEC_READONLY) == 0)
1016 h->protected_def = 1;
1019 /* This function is called when we want to merge a new symbol with an
1020 existing symbol. It handles the various cases which arise when we
1021 find a definition in a dynamic object, or when there is already a
1022 definition in a dynamic object. The new symbol is described by
1023 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
1024 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
1025 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
1026 of an old common symbol. We set OVERRIDE if the old symbol is
1027 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
1028 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
1029 to change. By OK to change, we mean that we shouldn't warn if the
1030 type or size does change. */
1033 _bfd_elf_merge_symbol (bfd *abfd,
1034 struct bfd_link_info *info,
1036 Elf_Internal_Sym *sym,
1039 struct elf_link_hash_entry **sym_hash,
1041 bfd_boolean *pold_weak,
1042 unsigned int *pold_alignment,
1044 bfd_boolean *override,
1045 bfd_boolean *type_change_ok,
1046 bfd_boolean *size_change_ok,
1047 bfd_boolean *matched)
1049 asection *sec, *oldsec;
1050 struct elf_link_hash_entry *h;
1051 struct elf_link_hash_entry *hi;
1052 struct elf_link_hash_entry *flip;
1055 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
1056 bfd_boolean newweak, oldweak, newfunc, oldfunc;
1057 const struct elf_backend_data *bed;
1059 bfd_boolean default_sym = *matched;
1065 bind = ELF_ST_BIND (sym->st_info);
1067 if (! bfd_is_und_section (sec))
1068 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
1070 h = ((struct elf_link_hash_entry *)
1071 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
1076 bed = get_elf_backend_data (abfd);
1078 /* NEW_VERSION is the symbol version of the new symbol. */
1079 if (h->versioned != unversioned)
1081 /* Symbol version is unknown or versioned. */
1082 new_version = strrchr (name, ELF_VER_CHR);
1085 if (h->versioned == unknown)
1087 if (new_version > name && new_version[-1] != ELF_VER_CHR)
1088 h->versioned = versioned_hidden;
1090 h->versioned = versioned;
1093 if (new_version[0] == '\0')
1097 h->versioned = unversioned;
1102 /* For merging, we only care about real symbols. But we need to make
1103 sure that indirect symbol dynamic flags are updated. */
1105 while (h->root.type == bfd_link_hash_indirect
1106 || h->root.type == bfd_link_hash_warning)
1107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1111 if (hi == h || h->root.type == bfd_link_hash_new)
1115 /* OLD_HIDDEN is true if the existing symbol is only visible
1116 to the symbol with the same symbol version. NEW_HIDDEN is
1117 true if the new symbol is only visible to the symbol with
1118 the same symbol version. */
1119 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1120 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1121 if (!old_hidden && !new_hidden)
1122 /* The new symbol matches the existing symbol if both
1127 /* OLD_VERSION is the symbol version of the existing
1131 if (h->versioned >= versioned)
1132 old_version = strrchr (h->root.root.string,
1137 /* The new symbol matches the existing symbol if they
1138 have the same symbol version. */
1139 *matched = (old_version == new_version
1140 || (old_version != NULL
1141 && new_version != NULL
1142 && strcmp (old_version, new_version) == 0));
1147 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1152 switch (h->root.type)
1157 case bfd_link_hash_undefined:
1158 case bfd_link_hash_undefweak:
1159 oldbfd = h->root.u.undef.abfd;
1162 case bfd_link_hash_defined:
1163 case bfd_link_hash_defweak:
1164 oldbfd = h->root.u.def.section->owner;
1165 oldsec = h->root.u.def.section;
1168 case bfd_link_hash_common:
1169 oldbfd = h->root.u.c.p->section->owner;
1170 oldsec = h->root.u.c.p->section;
1172 *pold_alignment = h->root.u.c.p->alignment_power;
1175 if (poldbfd && *poldbfd == NULL)
1178 /* Differentiate strong and weak symbols. */
1179 newweak = bind == STB_WEAK;
1180 oldweak = (h->root.type == bfd_link_hash_defweak
1181 || h->root.type == bfd_link_hash_undefweak);
1183 *pold_weak = oldweak;
1185 /* We have to check it for every instance since the first few may be
1186 references and not all compilers emit symbol type for undefined
1188 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1190 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1191 respectively, is from a dynamic object. */
1193 newdyn = (abfd->flags & DYNAMIC) != 0;
1195 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1196 syms and defined syms in dynamic libraries respectively.
1197 ref_dynamic on the other hand can be set for a symbol defined in
1198 a dynamic library, and def_dynamic may not be set; When the
1199 definition in a dynamic lib is overridden by a definition in the
1200 executable use of the symbol in the dynamic lib becomes a
1201 reference to the executable symbol. */
1204 if (bfd_is_und_section (sec))
1206 if (bind != STB_WEAK)
1208 h->ref_dynamic_nonweak = 1;
1209 hi->ref_dynamic_nonweak = 1;
1214 /* Update the existing symbol only if they match. */
1217 hi->dynamic_def = 1;
1221 /* If we just created the symbol, mark it as being an ELF symbol.
1222 Other than that, there is nothing to do--there is no merge issue
1223 with a newly defined symbol--so we just return. */
1225 if (h->root.type == bfd_link_hash_new)
1231 /* In cases involving weak versioned symbols, we may wind up trying
1232 to merge a symbol with itself. Catch that here, to avoid the
1233 confusion that results if we try to override a symbol with
1234 itself. The additional tests catch cases like
1235 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1236 dynamic object, which we do want to handle here. */
1238 && (newweak || oldweak)
1239 && ((abfd->flags & DYNAMIC) == 0
1240 || !h->def_regular))
1245 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1246 else if (oldsec != NULL)
1248 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1249 indices used by MIPS ELF. */
1250 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1253 /* Handle a case where plugin_notice won't be called and thus won't
1254 set the non_ir_ref flags on the first pass over symbols. */
1256 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)
1257 && newdyn != olddyn)
1259 h->root.non_ir_ref_dynamic = TRUE;
1260 hi->root.non_ir_ref_dynamic = TRUE;
1263 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1264 respectively, appear to be a definition rather than reference. */
1266 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1268 olddef = (h->root.type != bfd_link_hash_undefined
1269 && h->root.type != bfd_link_hash_undefweak
1270 && h->root.type != bfd_link_hash_common);
1272 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1273 respectively, appear to be a function. */
1275 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1276 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1278 oldfunc = (h->type != STT_NOTYPE
1279 && bed->is_function_type (h->type));
1281 if (!(newfunc && oldfunc)
1282 && ELF_ST_TYPE (sym->st_info) != h->type
1283 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1284 && h->type != STT_NOTYPE
1285 && (newdef || bfd_is_com_section (sec))
1286 && (olddef || h->root.type == bfd_link_hash_common))
1288 /* If creating a default indirect symbol ("foo" or "foo@") from
1289 a dynamic versioned definition ("foo@@") skip doing so if
1290 there is an existing regular definition with a different
1291 type. We don't want, for example, a "time" variable in the
1292 executable overriding a "time" function in a shared library. */
1300 /* When adding a symbol from a regular object file after we have
1301 created indirect symbols, undo the indirection and any
1308 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1309 h->forced_local = 0;
1313 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1315 h->root.type = bfd_link_hash_undefined;
1316 h->root.u.undef.abfd = abfd;
1320 h->root.type = bfd_link_hash_new;
1321 h->root.u.undef.abfd = NULL;
1327 /* Check TLS symbols. We don't check undefined symbols introduced
1328 by "ld -u" which have no type (and oldbfd NULL), and we don't
1329 check symbols from plugins because they also have no type. */
1331 && (oldbfd->flags & BFD_PLUGIN) == 0
1332 && (abfd->flags & BFD_PLUGIN) == 0
1333 && ELF_ST_TYPE (sym->st_info) != h->type
1334 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1337 bfd_boolean ntdef, tdef;
1338 asection *ntsec, *tsec;
1340 if (h->type == STT_TLS)
1361 /* xgettext:c-format */
1362 (_("%s: TLS definition in %pB section %pA "
1363 "mismatches non-TLS definition in %pB section %pA"),
1364 h->root.root.string, tbfd, tsec, ntbfd, ntsec);
1365 else if (!tdef && !ntdef)
1367 /* xgettext:c-format */
1368 (_("%s: TLS reference in %pB "
1369 "mismatches non-TLS reference in %pB"),
1370 h->root.root.string, tbfd, ntbfd);
1373 /* xgettext:c-format */
1374 (_("%s: TLS definition in %pB section %pA "
1375 "mismatches non-TLS reference in %pB"),
1376 h->root.root.string, tbfd, tsec, ntbfd);
1379 /* xgettext:c-format */
1380 (_("%s: TLS reference in %pB "
1381 "mismatches non-TLS definition in %pB section %pA"),
1382 h->root.root.string, tbfd, ntbfd, ntsec);
1384 bfd_set_error (bfd_error_bad_value);
1388 /* If the old symbol has non-default visibility, we ignore the new
1389 definition from a dynamic object. */
1391 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1392 && !bfd_is_und_section (sec))
1395 /* Make sure this symbol is dynamic. */
1397 hi->ref_dynamic = 1;
1398 /* A protected symbol has external availability. Make sure it is
1399 recorded as dynamic.
1401 FIXME: Should we check type and size for protected symbol? */
1402 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1403 return bfd_elf_link_record_dynamic_symbol (info, h);
1408 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1411 /* If the new symbol with non-default visibility comes from a
1412 relocatable file and the old definition comes from a dynamic
1413 object, we remove the old definition. */
1414 if (hi->root.type == bfd_link_hash_indirect)
1416 /* Handle the case where the old dynamic definition is
1417 default versioned. We need to copy the symbol info from
1418 the symbol with default version to the normal one if it
1419 was referenced before. */
1422 hi->root.type = h->root.type;
1423 h->root.type = bfd_link_hash_indirect;
1424 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1426 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1427 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1429 /* If the new symbol is hidden or internal, completely undo
1430 any dynamic link state. */
1431 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1432 h->forced_local = 0;
1439 /* FIXME: Should we check type and size for protected symbol? */
1449 /* If the old symbol was undefined before, then it will still be
1450 on the undefs list. If the new symbol is undefined or
1451 common, we can't make it bfd_link_hash_new here, because new
1452 undefined or common symbols will be added to the undefs list
1453 by _bfd_generic_link_add_one_symbol. Symbols may not be
1454 added twice to the undefs list. Also, if the new symbol is
1455 undefweak then we don't want to lose the strong undef. */
1456 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1458 h->root.type = bfd_link_hash_undefined;
1459 h->root.u.undef.abfd = abfd;
1463 h->root.type = bfd_link_hash_new;
1464 h->root.u.undef.abfd = NULL;
1467 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1469 /* If the new symbol is hidden or internal, completely undo
1470 any dynamic link state. */
1471 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1472 h->forced_local = 0;
1478 /* FIXME: Should we check type and size for protected symbol? */
1484 /* If a new weak symbol definition comes from a regular file and the
1485 old symbol comes from a dynamic library, we treat the new one as
1486 strong. Similarly, an old weak symbol definition from a regular
1487 file is treated as strong when the new symbol comes from a dynamic
1488 library. Further, an old weak symbol from a dynamic library is
1489 treated as strong if the new symbol is from a dynamic library.
1490 This reflects the way glibc's ld.so works.
1492 Also allow a weak symbol to override a linker script symbol
1493 defined by an early pass over the script. This is done so the
1494 linker knows the symbol is defined in an object file, for the
1495 DEFINED script function.
1497 Do this before setting *type_change_ok or *size_change_ok so that
1498 we warn properly when dynamic library symbols are overridden. */
1500 if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
1502 if (olddef && newdyn)
1505 /* Allow changes between different types of function symbol. */
1506 if (newfunc && oldfunc)
1507 *type_change_ok = TRUE;
1509 /* It's OK to change the type if either the existing symbol or the
1510 new symbol is weak. A type change is also OK if the old symbol
1511 is undefined and the new symbol is defined. */
1516 && h->root.type == bfd_link_hash_undefined))
1517 *type_change_ok = TRUE;
1519 /* It's OK to change the size if either the existing symbol or the
1520 new symbol is weak, or if the old symbol is undefined. */
1523 || h->root.type == bfd_link_hash_undefined)
1524 *size_change_ok = TRUE;
1526 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1527 symbol, respectively, appears to be a common symbol in a dynamic
1528 object. If a symbol appears in an uninitialized section, and is
1529 not weak, and is not a function, then it may be a common symbol
1530 which was resolved when the dynamic object was created. We want
1531 to treat such symbols specially, because they raise special
1532 considerations when setting the symbol size: if the symbol
1533 appears as a common symbol in a regular object, and the size in
1534 the regular object is larger, we must make sure that we use the
1535 larger size. This problematic case can always be avoided in C,
1536 but it must be handled correctly when using Fortran shared
1539 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1540 likewise for OLDDYNCOMMON and OLDDEF.
1542 Note that this test is just a heuristic, and that it is quite
1543 possible to have an uninitialized symbol in a shared object which
1544 is really a definition, rather than a common symbol. This could
1545 lead to some minor confusion when the symbol really is a common
1546 symbol in some regular object. However, I think it will be
1552 && (sec->flags & SEC_ALLOC) != 0
1553 && (sec->flags & SEC_LOAD) == 0
1556 newdyncommon = TRUE;
1558 newdyncommon = FALSE;
1562 && h->root.type == bfd_link_hash_defined
1564 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1565 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1568 olddyncommon = TRUE;
1570 olddyncommon = FALSE;
1572 /* We now know everything about the old and new symbols. We ask the
1573 backend to check if we can merge them. */
1574 if (bed->merge_symbol != NULL)
1576 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1581 /* There are multiple definitions of a normal symbol. Skip the
1582 default symbol as well as definition from an IR object. */
1583 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
1584 && !default_sym && h->def_regular
1586 && (oldbfd->flags & BFD_PLUGIN) != 0
1587 && (abfd->flags & BFD_PLUGIN) == 0))
1589 /* Handle a multiple definition. */
1590 (*info->callbacks->multiple_definition) (info, &h->root,
1591 abfd, sec, *pvalue);
1596 /* If both the old and the new symbols look like common symbols in a
1597 dynamic object, set the size of the symbol to the larger of the
1602 && sym->st_size != h->size)
1604 /* Since we think we have two common symbols, issue a multiple
1605 common warning if desired. Note that we only warn if the
1606 size is different. If the size is the same, we simply let
1607 the old symbol override the new one as normally happens with
1608 symbols defined in dynamic objects. */
1610 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1611 bfd_link_hash_common, sym->st_size);
1612 if (sym->st_size > h->size)
1613 h->size = sym->st_size;
1615 *size_change_ok = TRUE;
1618 /* If we are looking at a dynamic object, and we have found a
1619 definition, we need to see if the symbol was already defined by
1620 some other object. If so, we want to use the existing
1621 definition, and we do not want to report a multiple symbol
1622 definition error; we do this by clobbering *PSEC to be
1623 bfd_und_section_ptr.
1625 We treat a common symbol as a definition if the symbol in the
1626 shared library is a function, since common symbols always
1627 represent variables; this can cause confusion in principle, but
1628 any such confusion would seem to indicate an erroneous program or
1629 shared library. We also permit a common symbol in a regular
1630 object to override a weak symbol in a shared object. */
1635 || (h->root.type == bfd_link_hash_common
1636 && (newweak || newfunc))))
1640 newdyncommon = FALSE;
1642 *psec = sec = bfd_und_section_ptr;
1643 *size_change_ok = TRUE;
1645 /* If we get here when the old symbol is a common symbol, then
1646 we are explicitly letting it override a weak symbol or
1647 function in a dynamic object, and we don't want to warn about
1648 a type change. If the old symbol is a defined symbol, a type
1649 change warning may still be appropriate. */
1651 if (h->root.type == bfd_link_hash_common)
1652 *type_change_ok = TRUE;
1655 /* Handle the special case of an old common symbol merging with a
1656 new symbol which looks like a common symbol in a shared object.
1657 We change *PSEC and *PVALUE to make the new symbol look like a
1658 common symbol, and let _bfd_generic_link_add_one_symbol do the
1662 && h->root.type == bfd_link_hash_common)
1666 newdyncommon = FALSE;
1667 *pvalue = sym->st_size;
1668 *psec = sec = bed->common_section (oldsec);
1669 *size_change_ok = TRUE;
1672 /* Skip weak definitions of symbols that are already defined. */
1673 if (newdef && olddef && newweak)
1675 /* Don't skip new non-IR weak syms. */
1676 if (!(oldbfd != NULL
1677 && (oldbfd->flags & BFD_PLUGIN) != 0
1678 && (abfd->flags & BFD_PLUGIN) == 0))
1684 /* Merge st_other. If the symbol already has a dynamic index,
1685 but visibility says it should not be visible, turn it into a
1687 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1688 if (h->dynindx != -1)
1689 switch (ELF_ST_VISIBILITY (h->other))
1693 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1698 /* If the old symbol is from a dynamic object, and the new symbol is
1699 a definition which is not from a dynamic object, then the new
1700 symbol overrides the old symbol. Symbols from regular files
1701 always take precedence over symbols from dynamic objects, even if
1702 they are defined after the dynamic object in the link.
1704 As above, we again permit a common symbol in a regular object to
1705 override a definition in a shared object if the shared object
1706 symbol is a function or is weak. */
1711 || (bfd_is_com_section (sec)
1712 && (oldweak || oldfunc)))
1717 /* Change the hash table entry to undefined, and let
1718 _bfd_generic_link_add_one_symbol do the right thing with the
1721 h->root.type = bfd_link_hash_undefined;
1722 h->root.u.undef.abfd = h->root.u.def.section->owner;
1723 *size_change_ok = TRUE;
1726 olddyncommon = FALSE;
1728 /* We again permit a type change when a common symbol may be
1729 overriding a function. */
1731 if (bfd_is_com_section (sec))
1735 /* If a common symbol overrides a function, make sure
1736 that it isn't defined dynamically nor has type
1739 h->type = STT_NOTYPE;
1741 *type_change_ok = TRUE;
1744 if (hi->root.type == bfd_link_hash_indirect)
1747 /* This union may have been set to be non-NULL when this symbol
1748 was seen in a dynamic object. We must force the union to be
1749 NULL, so that it is correct for a regular symbol. */
1750 h->verinfo.vertree = NULL;
1753 /* Handle the special case of a new common symbol merging with an
1754 old symbol that looks like it might be a common symbol defined in
1755 a shared object. Note that we have already handled the case in
1756 which a new common symbol should simply override the definition
1757 in the shared library. */
1760 && bfd_is_com_section (sec)
1763 /* It would be best if we could set the hash table entry to a
1764 common symbol, but we don't know what to use for the section
1765 or the alignment. */
1766 (*info->callbacks->multiple_common) (info, &h->root, abfd,
1767 bfd_link_hash_common, sym->st_size);
1769 /* If the presumed common symbol in the dynamic object is
1770 larger, pretend that the new symbol has its size. */
1772 if (h->size > *pvalue)
1775 /* We need to remember the alignment required by the symbol
1776 in the dynamic object. */
1777 BFD_ASSERT (pold_alignment);
1778 *pold_alignment = h->root.u.def.section->alignment_power;
1781 olddyncommon = FALSE;
1783 h->root.type = bfd_link_hash_undefined;
1784 h->root.u.undef.abfd = h->root.u.def.section->owner;
1786 *size_change_ok = TRUE;
1787 *type_change_ok = TRUE;
1789 if (hi->root.type == bfd_link_hash_indirect)
1792 h->verinfo.vertree = NULL;
1797 /* Handle the case where we had a versioned symbol in a dynamic
1798 library and now find a definition in a normal object. In this
1799 case, we make the versioned symbol point to the normal one. */
1800 flip->root.type = h->root.type;
1801 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1802 h->root.type = bfd_link_hash_indirect;
1803 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1804 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1808 flip->ref_dynamic = 1;
1815 /* This function is called to create an indirect symbol from the
1816 default for the symbol with the default version if needed. The
1817 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1818 set DYNSYM if the new indirect symbol is dynamic. */
1821 _bfd_elf_add_default_symbol (bfd *abfd,
1822 struct bfd_link_info *info,
1823 struct elf_link_hash_entry *h,
1825 Elf_Internal_Sym *sym,
1829 bfd_boolean *dynsym)
1831 bfd_boolean type_change_ok;
1832 bfd_boolean size_change_ok;
1835 struct elf_link_hash_entry *hi;
1836 struct bfd_link_hash_entry *bh;
1837 const struct elf_backend_data *bed;
1838 bfd_boolean collect;
1839 bfd_boolean dynamic;
1840 bfd_boolean override;
1842 size_t len, shortlen;
1844 bfd_boolean matched;
1846 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1849 /* If this symbol has a version, and it is the default version, we
1850 create an indirect symbol from the default name to the fully
1851 decorated name. This will cause external references which do not
1852 specify a version to be bound to this version of the symbol. */
1853 p = strchr (name, ELF_VER_CHR);
1854 if (h->versioned == unknown)
1858 h->versioned = unversioned;
1863 if (p[1] != ELF_VER_CHR)
1865 h->versioned = versioned_hidden;
1869 h->versioned = versioned;
1874 /* PR ld/19073: We may see an unversioned definition after the
1880 bed = get_elf_backend_data (abfd);
1881 collect = bed->collect;
1882 dynamic = (abfd->flags & DYNAMIC) != 0;
1884 shortlen = p - name;
1885 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1886 if (shortname == NULL)
1888 memcpy (shortname, name, shortlen);
1889 shortname[shortlen] = '\0';
1891 /* We are going to create a new symbol. Merge it with any existing
1892 symbol with this name. For the purposes of the merge, act as
1893 though we were defining the symbol we just defined, although we
1894 actually going to define an indirect symbol. */
1895 type_change_ok = FALSE;
1896 size_change_ok = FALSE;
1899 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1900 &hi, poldbfd, NULL, NULL, &skip, &override,
1901 &type_change_ok, &size_change_ok, &matched))
1907 if (hi->def_regular)
1909 /* If the undecorated symbol will have a version added by a
1910 script different to H, then don't indirect to/from the
1911 undecorated symbol. This isn't ideal because we may not yet
1912 have seen symbol versions, if given by a script on the
1913 command line rather than via --version-script. */
1914 if (hi->verinfo.vertree == NULL && info->version_info != NULL)
1919 = bfd_find_version_for_sym (info->version_info,
1920 hi->root.root.string, &hide);
1921 if (hi->verinfo.vertree != NULL && hide)
1923 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
1927 if (hi->verinfo.vertree != NULL
1928 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
1934 /* Add the default symbol if not performing a relocatable link. */
1935 if (! bfd_link_relocatable (info))
1938 if (bh->type == bfd_link_hash_defined
1939 && bh->u.def.section->owner != NULL
1940 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
1942 /* Mark the previous definition from IR object as
1943 undefined so that the generic linker will override
1945 bh->type = bfd_link_hash_undefined;
1946 bh->u.undef.abfd = bh->u.def.section->owner;
1948 if (! (_bfd_generic_link_add_one_symbol
1949 (info, abfd, shortname, BSF_INDIRECT,
1950 bfd_ind_section_ptr,
1951 0, name, FALSE, collect, &bh)))
1953 hi = (struct elf_link_hash_entry *) bh;
1958 /* In this case the symbol named SHORTNAME is overriding the
1959 indirect symbol we want to add. We were planning on making
1960 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1961 is the name without a version. NAME is the fully versioned
1962 name, and it is the default version.
1964 Overriding means that we already saw a definition for the
1965 symbol SHORTNAME in a regular object, and it is overriding
1966 the symbol defined in the dynamic object.
1968 When this happens, we actually want to change NAME, the
1969 symbol we just added, to refer to SHORTNAME. This will cause
1970 references to NAME in the shared object to become references
1971 to SHORTNAME in the regular object. This is what we expect
1972 when we override a function in a shared object: that the
1973 references in the shared object will be mapped to the
1974 definition in the regular object. */
1976 while (hi->root.type == bfd_link_hash_indirect
1977 || hi->root.type == bfd_link_hash_warning)
1978 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1980 h->root.type = bfd_link_hash_indirect;
1981 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1985 hi->ref_dynamic = 1;
1989 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1994 /* Now set HI to H, so that the following code will set the
1995 other fields correctly. */
1999 /* Check if HI is a warning symbol. */
2000 if (hi->root.type == bfd_link_hash_warning)
2001 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
2003 /* If there is a duplicate definition somewhere, then HI may not
2004 point to an indirect symbol. We will have reported an error to
2005 the user in that case. */
2007 if (hi->root.type == bfd_link_hash_indirect)
2009 struct elf_link_hash_entry *ht;
2011 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
2012 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
2014 /* A reference to the SHORTNAME symbol from a dynamic library
2015 will be satisfied by the versioned symbol at runtime. In
2016 effect, we have a reference to the versioned symbol. */
2017 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2018 hi->dynamic_def |= ht->dynamic_def;
2020 /* See if the new flags lead us to realize that the symbol must
2026 if (! bfd_link_executable (info)
2033 if (hi->ref_regular)
2039 /* We also need to define an indirection from the nondefault version
2043 len = strlen (name);
2044 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
2045 if (shortname == NULL)
2047 memcpy (shortname, name, shortlen);
2048 memcpy (shortname + shortlen, p + 1, len - shortlen);
2050 /* Once again, merge with any existing symbol. */
2051 type_change_ok = FALSE;
2052 size_change_ok = FALSE;
2054 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
2055 &hi, poldbfd, NULL, NULL, &skip, &override,
2056 &type_change_ok, &size_change_ok, &matched))
2064 /* Here SHORTNAME is a versioned name, so we don't expect to see
2065 the type of override we do in the case above unless it is
2066 overridden by a versioned definition. */
2067 if (hi->root.type != bfd_link_hash_defined
2068 && hi->root.type != bfd_link_hash_defweak)
2070 /* xgettext:c-format */
2071 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
2077 if (! (_bfd_generic_link_add_one_symbol
2078 (info, abfd, shortname, BSF_INDIRECT,
2079 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
2081 hi = (struct elf_link_hash_entry *) bh;
2083 /* If there is a duplicate definition somewhere, then HI may not
2084 point to an indirect symbol. We will have reported an error
2085 to the user in that case. */
2087 if (hi->root.type == bfd_link_hash_indirect)
2089 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
2090 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
2091 hi->dynamic_def |= h->dynamic_def;
2093 /* See if the new flags lead us to realize that the symbol
2099 if (! bfd_link_executable (info)
2105 if (hi->ref_regular)
2115 /* This routine is used to export all defined symbols into the dynamic
2116 symbol table. It is called via elf_link_hash_traverse. */
2119 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
2121 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2123 /* Ignore indirect symbols. These are added by the versioning code. */
2124 if (h->root.type == bfd_link_hash_indirect)
2127 /* Ignore this if we won't export it. */
2128 if (!eif->info->export_dynamic && !h->dynamic)
2131 if (h->dynindx == -1
2132 && (h->def_regular || h->ref_regular)
2133 && ! bfd_hide_sym_by_version (eif->info->version_info,
2134 h->root.root.string))
2136 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2146 /* Look through the symbols which are defined in other shared
2147 libraries and referenced here. Update the list of version
2148 dependencies. This will be put into the .gnu.version_r section.
2149 This function is called via elf_link_hash_traverse. */
2152 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
2155 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
2156 Elf_Internal_Verneed *t;
2157 Elf_Internal_Vernaux *a;
2160 /* We only care about symbols defined in shared objects with version
2165 || h->verinfo.verdef == NULL
2166 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
2167 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
2170 /* See if we already know about this version. */
2171 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
2175 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
2178 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2179 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
2185 /* This is a new version. Add it to tree we are building. */
2190 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2193 rinfo->failed = TRUE;
2197 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2198 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2199 elf_tdata (rinfo->info->output_bfd)->verref = t;
2203 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2206 rinfo->failed = TRUE;
2210 /* Note that we are copying a string pointer here, and testing it
2211 above. If bfd_elf_string_from_elf_section is ever changed to
2212 discard the string data when low in memory, this will have to be
2214 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2216 a->vna_flags = h->verinfo.verdef->vd_flags;
2217 a->vna_nextptr = t->vn_auxptr;
2219 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2222 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2229 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
2230 hidden. Set *T_P to NULL if there is no match. */
2233 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
2234 struct elf_link_hash_entry *h,
2235 const char *version_p,
2236 struct bfd_elf_version_tree **t_p,
2239 struct bfd_elf_version_tree *t;
2241 /* Look for the version. If we find it, it is no longer weak. */
2242 for (t = info->version_info; t != NULL; t = t->next)
2244 if (strcmp (t->name, version_p) == 0)
2248 struct bfd_elf_version_expr *d;
2250 len = version_p - h->root.root.string;
2251 alc = (char *) bfd_malloc (len);
2254 memcpy (alc, h->root.root.string, len - 1);
2255 alc[len - 1] = '\0';
2256 if (alc[len - 2] == ELF_VER_CHR)
2257 alc[len - 2] = '\0';
2259 h->verinfo.vertree = t;
2263 if (t->globals.list != NULL)
2264 d = (*t->match) (&t->globals, NULL, alc);
2266 /* See if there is anything to force this symbol to
2268 if (d == NULL && t->locals.list != NULL)
2270 d = (*t->match) (&t->locals, NULL, alc);
2273 && ! info->export_dynamic)
2287 /* Return TRUE if the symbol H is hidden by version script. */
2290 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
2291 struct elf_link_hash_entry *h)
2294 bfd_boolean hide = FALSE;
2295 const struct elf_backend_data *bed
2296 = get_elf_backend_data (info->output_bfd);
2298 /* Version script only hides symbols defined in regular objects. */
2299 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2302 p = strchr (h->root.root.string, ELF_VER_CHR);
2303 if (p != NULL && h->verinfo.vertree == NULL)
2305 struct bfd_elf_version_tree *t;
2308 if (*p == ELF_VER_CHR)
2312 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
2316 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2321 /* If we don't have a version for this symbol, see if we can find
2323 if (h->verinfo.vertree == NULL && info->version_info != NULL)
2326 = bfd_find_version_for_sym (info->version_info,
2327 h->root.root.string, &hide);
2328 if (h->verinfo.vertree != NULL && hide)
2330 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2338 /* Figure out appropriate versions for all the symbols. We may not
2339 have the version number script until we have read all of the input
2340 files, so until that point we don't know which symbols should be
2341 local. This function is called via elf_link_hash_traverse. */
2344 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2346 struct elf_info_failed *sinfo;
2347 struct bfd_link_info *info;
2348 const struct elf_backend_data *bed;
2349 struct elf_info_failed eif;
2353 sinfo = (struct elf_info_failed *) data;
2356 /* Fix the symbol flags. */
2359 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2362 sinfo->failed = TRUE;
2366 bed = get_elf_backend_data (info->output_bfd);
2368 /* We only need version numbers for symbols defined in regular
2370 if (!h->def_regular)
2372 /* Hide symbols defined in discarded input sections. */
2373 if ((h->root.type == bfd_link_hash_defined
2374 || h->root.type == bfd_link_hash_defweak)
2375 && discarded_section (h->root.u.def.section))
2376 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2381 p = strchr (h->root.root.string, ELF_VER_CHR);
2382 if (p != NULL && h->verinfo.vertree == NULL)
2384 struct bfd_elf_version_tree *t;
2387 if (*p == ELF_VER_CHR)
2390 /* If there is no version string, we can just return out. */
2394 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
2396 sinfo->failed = TRUE;
2401 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2403 /* If we are building an application, we need to create a
2404 version node for this version. */
2405 if (t == NULL && bfd_link_executable (info))
2407 struct bfd_elf_version_tree **pp;
2410 /* If we aren't going to export this symbol, we don't need
2411 to worry about it. */
2412 if (h->dynindx == -1)
2415 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
2419 sinfo->failed = TRUE;
2424 t->name_indx = (unsigned int) -1;
2428 /* Don't count anonymous version tag. */
2429 if (sinfo->info->version_info != NULL
2430 && sinfo->info->version_info->vernum == 0)
2432 for (pp = &sinfo->info->version_info;
2436 t->vernum = version_index;
2440 h->verinfo.vertree = t;
2444 /* We could not find the version for a symbol when
2445 generating a shared archive. Return an error. */
2447 /* xgettext:c-format */
2448 (_("%pB: version node not found for symbol %s"),
2449 info->output_bfd, h->root.root.string);
2450 bfd_set_error (bfd_error_bad_value);
2451 sinfo->failed = TRUE;
2456 /* If we don't have a version for this symbol, see if we can find
2459 && h->verinfo.vertree == NULL
2460 && sinfo->info->version_info != NULL)
2463 = bfd_find_version_for_sym (sinfo->info->version_info,
2464 h->root.root.string, &hide);
2465 if (h->verinfo.vertree != NULL && hide)
2466 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2472 /* Read and swap the relocs from the section indicated by SHDR. This
2473 may be either a REL or a RELA section. The relocations are
2474 translated into RELA relocations and stored in INTERNAL_RELOCS,
2475 which should have already been allocated to contain enough space.
2476 The EXTERNAL_RELOCS are a buffer where the external form of the
2477 relocations should be stored.
2479 Returns FALSE if something goes wrong. */
2482 elf_link_read_relocs_from_section (bfd *abfd,
2484 Elf_Internal_Shdr *shdr,
2485 void *external_relocs,
2486 Elf_Internal_Rela *internal_relocs)
2488 const struct elf_backend_data *bed;
2489 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2490 const bfd_byte *erela;
2491 const bfd_byte *erelaend;
2492 Elf_Internal_Rela *irela;
2493 Elf_Internal_Shdr *symtab_hdr;
2496 /* Position ourselves at the start of the section. */
2497 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2500 /* Read the relocations. */
2501 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2504 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2505 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2507 bed = get_elf_backend_data (abfd);
2509 /* Convert the external relocations to the internal format. */
2510 if (shdr->sh_entsize == bed->s->sizeof_rel)
2511 swap_in = bed->s->swap_reloc_in;
2512 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2513 swap_in = bed->s->swap_reloca_in;
2516 bfd_set_error (bfd_error_wrong_format);
2520 erela = (const bfd_byte *) external_relocs;
2521 /* Setting erelaend like this and comparing with <= handles case of
2522 a fuzzed object with sh_size not a multiple of sh_entsize. */
2523 erelaend = erela + shdr->sh_size - shdr->sh_entsize;
2524 irela = internal_relocs;
2525 while (erela <= erelaend)
2529 (*swap_in) (abfd, erela, irela);
2530 r_symndx = ELF32_R_SYM (irela->r_info);
2531 if (bed->s->arch_size == 64)
2535 if ((size_t) r_symndx >= nsyms)
2538 /* xgettext:c-format */
2539 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
2540 " for offset %#" PRIx64 " in section `%pA'"),
2541 abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
2542 (uint64_t) irela->r_offset, sec);
2543 bfd_set_error (bfd_error_bad_value);
2547 else if (r_symndx != STN_UNDEF)
2550 /* xgettext:c-format */
2551 (_("%pB: non-zero symbol index (%#" PRIx64 ")"
2552 " for offset %#" PRIx64 " in section `%pA'"
2553 " when the object file has no symbol table"),
2554 abfd, (uint64_t) r_symndx,
2555 (uint64_t) irela->r_offset, sec);
2556 bfd_set_error (bfd_error_bad_value);
2559 irela += bed->s->int_rels_per_ext_rel;
2560 erela += shdr->sh_entsize;
2566 /* Read and swap the relocs for a section O. They may have been
2567 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2568 not NULL, they are used as buffers to read into. They are known to
2569 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2570 the return value is allocated using either malloc or bfd_alloc,
2571 according to the KEEP_MEMORY argument. If O has two relocation
2572 sections (both REL and RELA relocations), then the REL_HDR
2573 relocations will appear first in INTERNAL_RELOCS, followed by the
2574 RELA_HDR relocations. */
2577 _bfd_elf_link_read_relocs (bfd *abfd,
2579 void *external_relocs,
2580 Elf_Internal_Rela *internal_relocs,
2581 bfd_boolean keep_memory)
2583 void *alloc1 = NULL;
2584 Elf_Internal_Rela *alloc2 = NULL;
2585 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2586 struct bfd_elf_section_data *esdo = elf_section_data (o);
2587 Elf_Internal_Rela *internal_rela_relocs;
2589 if (esdo->relocs != NULL)
2590 return esdo->relocs;
2592 if (o->reloc_count == 0)
2595 if (internal_relocs == NULL)
2599 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
2601 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2603 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2604 if (internal_relocs == NULL)
2608 if (external_relocs == NULL)
2610 bfd_size_type size = 0;
2613 size += esdo->rel.hdr->sh_size;
2615 size += esdo->rela.hdr->sh_size;
2617 alloc1 = bfd_malloc (size);
2620 external_relocs = alloc1;
2623 internal_rela_relocs = internal_relocs;
2626 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2630 external_relocs = (((bfd_byte *) external_relocs)
2631 + esdo->rel.hdr->sh_size);
2632 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2633 * bed->s->int_rels_per_ext_rel);
2637 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2639 internal_rela_relocs)))
2642 /* Cache the results for next time, if we can. */
2644 esdo->relocs = internal_relocs;
2649 /* Don't free alloc2, since if it was allocated we are passing it
2650 back (under the name of internal_relocs). */
2652 return internal_relocs;
2660 bfd_release (abfd, alloc2);
2667 /* Compute the size of, and allocate space for, REL_HDR which is the
2668 section header for a section containing relocations for O. */
2671 _bfd_elf_link_size_reloc_section (bfd *abfd,
2672 struct bfd_elf_section_reloc_data *reldata)
2674 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2676 /* That allows us to calculate the size of the section. */
2677 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2679 /* The contents field must last into write_object_contents, so we
2680 allocate it with bfd_alloc rather than malloc. Also since we
2681 cannot be sure that the contents will actually be filled in,
2682 we zero the allocated space. */
2683 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2684 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2687 if (reldata->hashes == NULL && reldata->count)
2689 struct elf_link_hash_entry **p;
2691 p = ((struct elf_link_hash_entry **)
2692 bfd_zmalloc (reldata->count * sizeof (*p)));
2696 reldata->hashes = p;
2702 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2703 originated from the section given by INPUT_REL_HDR) to the
2707 _bfd_elf_link_output_relocs (bfd *output_bfd,
2708 asection *input_section,
2709 Elf_Internal_Shdr *input_rel_hdr,
2710 Elf_Internal_Rela *internal_relocs,
2711 struct elf_link_hash_entry **rel_hash
2714 Elf_Internal_Rela *irela;
2715 Elf_Internal_Rela *irelaend;
2717 struct bfd_elf_section_reloc_data *output_reldata;
2718 asection *output_section;
2719 const struct elf_backend_data *bed;
2720 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2721 struct bfd_elf_section_data *esdo;
2723 output_section = input_section->output_section;
2725 bed = get_elf_backend_data (output_bfd);
2726 esdo = elf_section_data (output_section);
2727 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2729 output_reldata = &esdo->rel;
2730 swap_out = bed->s->swap_reloc_out;
2732 else if (esdo->rela.hdr
2733 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2735 output_reldata = &esdo->rela;
2736 swap_out = bed->s->swap_reloca_out;
2741 /* xgettext:c-format */
2742 (_("%pB: relocation size mismatch in %pB section %pA"),
2743 output_bfd, input_section->owner, input_section);
2744 bfd_set_error (bfd_error_wrong_format);
2748 erel = output_reldata->hdr->contents;
2749 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2750 irela = internal_relocs;
2751 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2752 * bed->s->int_rels_per_ext_rel);
2753 while (irela < irelaend)
2755 (*swap_out) (output_bfd, irela, erel);
2756 irela += bed->s->int_rels_per_ext_rel;
2757 erel += input_rel_hdr->sh_entsize;
2760 /* Bump the counter, so that we know where to add the next set of
2762 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2767 /* Make weak undefined symbols in PIE dynamic. */
2770 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2771 struct elf_link_hash_entry *h)
2773 if (bfd_link_pie (info)
2775 && h->root.type == bfd_link_hash_undefweak)
2776 return bfd_elf_link_record_dynamic_symbol (info, h);
2781 /* Fix up the flags for a symbol. This handles various cases which
2782 can only be fixed after all the input files are seen. This is
2783 currently called by both adjust_dynamic_symbol and
2784 assign_sym_version, which is unnecessary but perhaps more robust in
2785 the face of future changes. */
2788 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2789 struct elf_info_failed *eif)
2791 const struct elf_backend_data *bed;
2793 /* If this symbol was mentioned in a non-ELF file, try to set
2794 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2795 permit a non-ELF file to correctly refer to a symbol defined in
2796 an ELF dynamic object. */
2799 while (h->root.type == bfd_link_hash_indirect)
2800 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2802 if (h->root.type != bfd_link_hash_defined
2803 && h->root.type != bfd_link_hash_defweak)
2806 h->ref_regular_nonweak = 1;
2810 if (h->root.u.def.section->owner != NULL
2811 && (bfd_get_flavour (h->root.u.def.section->owner)
2812 == bfd_target_elf_flavour))
2815 h->ref_regular_nonweak = 1;
2821 if (h->dynindx == -1
2825 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2834 /* Unfortunately, NON_ELF is only correct if the symbol
2835 was first seen in a non-ELF file. Fortunately, if the symbol
2836 was first seen in an ELF file, we're probably OK unless the
2837 symbol was defined in a non-ELF file. Catch that case here.
2838 FIXME: We're still in trouble if the symbol was first seen in
2839 a dynamic object, and then later in a non-ELF regular object. */
2840 if ((h->root.type == bfd_link_hash_defined
2841 || h->root.type == bfd_link_hash_defweak)
2843 && (h->root.u.def.section->owner != NULL
2844 ? (bfd_get_flavour (h->root.u.def.section->owner)
2845 != bfd_target_elf_flavour)
2846 : (bfd_is_abs_section (h->root.u.def.section)
2847 && !h->def_dynamic)))
2851 /* Backend specific symbol fixup. */
2852 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2853 if (bed->elf_backend_fixup_symbol
2854 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2857 /* If this is a final link, and the symbol was defined as a common
2858 symbol in a regular object file, and there was no definition in
2859 any dynamic object, then the linker will have allocated space for
2860 the symbol in a common section but the DEF_REGULAR
2861 flag will not have been set. */
2862 if (h->root.type == bfd_link_hash_defined
2866 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2869 /* Symbols defined in discarded sections shouldn't be dynamic. */
2870 if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
2871 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2873 /* If a weak undefined symbol has non-default visibility, we also
2874 hide it from the dynamic linker. */
2875 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2876 && h->root.type == bfd_link_hash_undefweak)
2877 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2879 /* A hidden versioned symbol in executable should be forced local if
2880 it is is locally defined, not referenced by shared library and not
2882 else if (bfd_link_executable (eif->info)
2883 && h->versioned == versioned_hidden
2884 && !eif->info->export_dynamic
2888 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2890 /* If -Bsymbolic was used (which means to bind references to global
2891 symbols to the definition within the shared object), and this
2892 symbol was defined in a regular object, then it actually doesn't
2893 need a PLT entry. Likewise, if the symbol has non-default
2894 visibility. If the symbol has hidden or internal visibility, we
2895 will force it local. */
2896 else if (h->needs_plt
2897 && bfd_link_pic (eif->info)
2898 && is_elf_hash_table (eif->info->hash)
2899 && (SYMBOLIC_BIND (eif->info, h)
2900 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2903 bfd_boolean force_local;
2905 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2906 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2907 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2910 /* If this is a weak defined symbol in a dynamic object, and we know
2911 the real definition in the dynamic object, copy interesting flags
2912 over to the real definition. */
2913 if (h->is_weakalias)
2915 struct elf_link_hash_entry *def = weakdef (h);
2917 /* If the real definition is defined by a regular object file,
2918 don't do anything special. See the longer description in
2919 _bfd_elf_adjust_dynamic_symbol, below. If the def is not
2920 bfd_link_hash_defined as it was when put on the alias list
2921 then it must have originally been a versioned symbol (for
2922 which a non-versioned indirect symbol is created) and later
2923 a definition for the non-versioned symbol is found. In that
2924 case the indirection is flipped with the versioned symbol
2925 becoming an indirect pointing at the non-versioned symbol.
2926 Thus, not an alias any more. */
2927 if (def->def_regular
2928 || def->root.type != bfd_link_hash_defined)
2931 while ((h = h->u.alias) != def)
2932 h->is_weakalias = 0;
2936 while (h->root.type == bfd_link_hash_indirect)
2937 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2938 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2939 || h->root.type == bfd_link_hash_defweak);
2940 BFD_ASSERT (def->def_dynamic);
2941 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
2948 /* Make the backend pick a good value for a dynamic symbol. This is
2949 called via elf_link_hash_traverse, and also calls itself
2953 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2955 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2956 struct elf_link_hash_table *htab;
2957 const struct elf_backend_data *bed;
2959 if (! is_elf_hash_table (eif->info->hash))
2962 /* Ignore indirect symbols. These are added by the versioning code. */
2963 if (h->root.type == bfd_link_hash_indirect)
2966 /* Fix the symbol flags. */
2967 if (! _bfd_elf_fix_symbol_flags (h, eif))
2970 htab = elf_hash_table (eif->info);
2971 bed = get_elf_backend_data (htab->dynobj);
2973 if (h->root.type == bfd_link_hash_undefweak)
2975 if (eif->info->dynamic_undefined_weak == 0)
2976 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2977 else if (eif->info->dynamic_undefined_weak > 0
2979 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2980 && !bfd_hide_sym_by_version (eif->info->version_info,
2981 h->root.root.string))
2983 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
2991 /* If this symbol does not require a PLT entry, and it is not
2992 defined by a dynamic object, or is not referenced by a regular
2993 object, ignore it. We do have to handle a weak defined symbol,
2994 even if no regular object refers to it, if we decided to add it
2995 to the dynamic symbol table. FIXME: Do we normally need to worry
2996 about symbols which are defined by one dynamic object and
2997 referenced by another one? */
2999 && h->type != STT_GNU_IFUNC
3003 && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
3005 h->plt = elf_hash_table (eif->info)->init_plt_offset;
3009 /* If we've already adjusted this symbol, don't do it again. This
3010 can happen via a recursive call. */
3011 if (h->dynamic_adjusted)
3014 /* Don't look at this symbol again. Note that we must set this
3015 after checking the above conditions, because we may look at a
3016 symbol once, decide not to do anything, and then get called
3017 recursively later after REF_REGULAR is set below. */
3018 h->dynamic_adjusted = 1;
3020 /* If this is a weak definition, and we know a real definition, and
3021 the real symbol is not itself defined by a regular object file,
3022 then get a good value for the real definition. We handle the
3023 real symbol first, for the convenience of the backend routine.
3025 Note that there is a confusing case here. If the real definition
3026 is defined by a regular object file, we don't get the real symbol
3027 from the dynamic object, but we do get the weak symbol. If the
3028 processor backend uses a COPY reloc, then if some routine in the
3029 dynamic object changes the real symbol, we will not see that
3030 change in the corresponding weak symbol. This is the way other
3031 ELF linkers work as well, and seems to be a result of the shared
3034 I will clarify this issue. Most SVR4 shared libraries define the
3035 variable _timezone and define timezone as a weak synonym. The
3036 tzset call changes _timezone. If you write
3037 extern int timezone;
3039 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
3040 you might expect that, since timezone is a synonym for _timezone,
3041 the same number will print both times. However, if the processor
3042 backend uses a COPY reloc, then actually timezone will be copied
3043 into your process image, and, since you define _timezone
3044 yourself, _timezone will not. Thus timezone and _timezone will
3045 wind up at different memory locations. The tzset call will set
3046 _timezone, leaving timezone unchanged. */
3048 if (h->is_weakalias)
3050 struct elf_link_hash_entry *def = weakdef (h);
3052 /* If we get to this point, there is an implicit reference to
3053 the alias by a regular object file via the weak symbol H. */
3054 def->ref_regular = 1;
3056 /* Ensure that the backend adjust_dynamic_symbol function sees
3057 the strong alias before H by recursively calling ourselves. */
3058 if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
3062 /* If a symbol has no type and no size and does not require a PLT
3063 entry, then we are probably about to do the wrong thing here: we
3064 are probably going to create a COPY reloc for an empty object.
3065 This case can arise when a shared object is built with assembly
3066 code, and the assembly code fails to set the symbol type. */
3068 && h->type == STT_NOTYPE
3071 (_("warning: type and size of dynamic symbol `%s' are not defined"),
3072 h->root.root.string);
3074 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
3083 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
3087 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
3088 struct elf_link_hash_entry *h,
3091 unsigned int power_of_two;
3093 asection *sec = h->root.u.def.section;
3095 /* The section alignment of the definition is the maximum alignment
3096 requirement of symbols defined in the section. Since we don't
3097 know the symbol alignment requirement, we start with the
3098 maximum alignment and check low bits of the symbol address
3099 for the minimum alignment. */
3100 power_of_two = bfd_section_alignment (sec);
3101 mask = ((bfd_vma) 1 << power_of_two) - 1;
3102 while ((h->root.u.def.value & mask) != 0)
3108 if (power_of_two > bfd_section_alignment (dynbss))
3110 /* Adjust the section alignment if needed. */
3111 if (!bfd_set_section_alignment (dynbss, power_of_two))
3115 /* We make sure that the symbol will be aligned properly. */
3116 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
3118 /* Define the symbol as being at this point in DYNBSS. */
3119 h->root.u.def.section = dynbss;
3120 h->root.u.def.value = dynbss->size;
3122 /* Increment the size of DYNBSS to make room for the symbol. */
3123 dynbss->size += h->size;
3125 /* No error if extern_protected_data is true. */
3126 if (h->protected_def
3127 && (!info->extern_protected_data
3128 || (info->extern_protected_data < 0
3129 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
3130 info->callbacks->einfo
3131 (_("%P: copy reloc against protected `%pT' is dangerous\n"),
3132 h->root.root.string);
3137 /* Adjust all external symbols pointing into SEC_MERGE sections
3138 to reflect the object merging within the sections. */
3141 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
3145 if ((h->root.type == bfd_link_hash_defined
3146 || h->root.type == bfd_link_hash_defweak)
3147 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
3148 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3150 bfd *output_bfd = (bfd *) data;
3152 h->root.u.def.value =
3153 _bfd_merged_section_offset (output_bfd,
3154 &h->root.u.def.section,
3155 elf_section_data (sec)->sec_info,
3156 h->root.u.def.value);
3162 /* Returns false if the symbol referred to by H should be considered
3163 to resolve local to the current module, and true if it should be
3164 considered to bind dynamically. */
3167 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
3168 struct bfd_link_info *info,
3169 bfd_boolean not_local_protected)
3171 bfd_boolean binding_stays_local_p;
3172 const struct elf_backend_data *bed;
3173 struct elf_link_hash_table *hash_table;
3178 while (h->root.type == bfd_link_hash_indirect
3179 || h->root.type == bfd_link_hash_warning)
3180 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3182 /* If it was forced local, then clearly it's not dynamic. */
3183 if (h->dynindx == -1)
3185 if (h->forced_local)
3188 /* Identify the cases where name binding rules say that a
3189 visible symbol resolves locally. */
3190 binding_stays_local_p = (bfd_link_executable (info)
3191 || SYMBOLIC_BIND (info, h));
3193 switch (ELF_ST_VISIBILITY (h->other))
3200 hash_table = elf_hash_table (info);
3201 if (!is_elf_hash_table (hash_table))
3204 bed = get_elf_backend_data (hash_table->dynobj);
3206 /* Proper resolution for function pointer equality may require
3207 that these symbols perhaps be resolved dynamically, even though
3208 we should be resolving them to the current module. */
3209 if (!not_local_protected || !bed->is_function_type (h->type))
3210 binding_stays_local_p = TRUE;
3217 /* If it isn't defined locally, then clearly it's dynamic. */
3218 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
3221 /* Otherwise, the symbol is dynamic if binding rules don't tell
3222 us that it remains local. */
3223 return !binding_stays_local_p;
3226 /* Return true if the symbol referred to by H should be considered
3227 to resolve local to the current module, and false otherwise. Differs
3228 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
3229 undefined symbols. The two functions are virtually identical except
3230 for the place where dynindx == -1 is tested. If that test is true,
3231 _bfd_elf_dynamic_symbol_p will say the symbol is local, while
3232 _bfd_elf_symbol_refs_local_p will say the symbol is local only for
3234 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
3235 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
3236 treatment of undefined weak symbols. For those that do not make
3237 undefined weak symbols dynamic, both functions may return false. */
3240 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
3241 struct bfd_link_info *info,
3242 bfd_boolean local_protected)
3244 const struct elf_backend_data *bed;
3245 struct elf_link_hash_table *hash_table;
3247 /* If it's a local sym, of course we resolve locally. */
3251 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
3252 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
3253 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
3256 /* Forced local symbols resolve locally. */
3257 if (h->forced_local)
3260 /* Common symbols that become definitions don't get the DEF_REGULAR
3261 flag set, so test it first, and don't bail out. */
3262 if (ELF_COMMON_DEF_P (h))
3264 /* If we don't have a definition in a regular file, then we can't
3265 resolve locally. The sym is either undefined or dynamic. */
3266 else if (!h->def_regular)
3269 /* Non-dynamic symbols resolve locally. */
3270 if (h->dynindx == -1)
3273 /* At this point, we know the symbol is defined and dynamic. In an
3274 executable it must resolve locally, likewise when building symbolic
3275 shared libraries. */
3276 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
3279 /* Now deal with defined dynamic symbols in shared libraries. Ones
3280 with default visibility might not resolve locally. */
3281 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3284 hash_table = elf_hash_table (info);
3285 if (!is_elf_hash_table (hash_table))
3288 bed = get_elf_backend_data (hash_table->dynobj);
3290 /* If extern_protected_data is false, STV_PROTECTED non-function
3291 symbols are local. */
3292 if ((!info->extern_protected_data
3293 || (info->extern_protected_data < 0
3294 && !bed->extern_protected_data))
3295 && !bed->is_function_type (h->type))
3298 /* Function pointer equality tests may require that STV_PROTECTED
3299 symbols be treated as dynamic symbols. If the address of a
3300 function not defined in an executable is set to that function's
3301 plt entry in the executable, then the address of the function in
3302 a shared library must also be the plt entry in the executable. */
3303 return local_protected;
3306 /* Caches some TLS segment info, and ensures that the TLS segment vma is
3307 aligned. Returns the first TLS output section. */
3309 struct bfd_section *
3310 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3312 struct bfd_section *sec, *tls;
3313 unsigned int align = 0;
3315 for (sec = obfd->sections; sec != NULL; sec = sec->next)
3316 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
3320 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
3321 if (sec->alignment_power > align)
3322 align = sec->alignment_power;
3324 elf_hash_table (info)->tls_sec = tls;
3326 /* Ensure the alignment of the first section is the largest alignment,
3327 so that the tls segment starts aligned. */
3329 tls->alignment_power = align;
3334 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3336 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3337 Elf_Internal_Sym *sym)
3339 const struct elf_backend_data *bed;
3341 /* Local symbols do not count, but target specific ones might. */
3342 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3343 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3346 bed = get_elf_backend_data (abfd);
3347 /* Function symbols do not count. */
3348 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3351 /* If the section is undefined, then so is the symbol. */
3352 if (sym->st_shndx == SHN_UNDEF)
3355 /* If the symbol is defined in the common section, then
3356 it is a common definition and so does not count. */
3357 if (bed->common_definition (sym))
3360 /* If the symbol is in a target specific section then we
3361 must rely upon the backend to tell us what it is. */
3362 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3363 /* FIXME - this function is not coded yet:
3365 return _bfd_is_global_symbol_definition (abfd, sym);
3367 Instead for now assume that the definition is not global,
3368 Even if this is wrong, at least the linker will behave
3369 in the same way that it used to do. */
3375 /* Search the symbol table of the archive element of the archive ABFD
3376 whose archive map contains a mention of SYMDEF, and determine if
3377 the symbol is defined in this element. */
3379 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3381 Elf_Internal_Shdr * hdr;
3385 Elf_Internal_Sym *isymbuf;
3386 Elf_Internal_Sym *isym;
3387 Elf_Internal_Sym *isymend;
3390 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3394 if (! bfd_check_format (abfd, bfd_object))
3397 /* Select the appropriate symbol table. If we don't know if the
3398 object file is an IR object, give linker LTO plugin a chance to
3399 get the correct symbol table. */
3400 if (abfd->plugin_format == bfd_plugin_yes
3401 #if BFD_SUPPORTS_PLUGINS
3402 || (abfd->plugin_format == bfd_plugin_unknown
3403 && bfd_link_plugin_object_p (abfd))
3407 /* Use the IR symbol table if the object has been claimed by
3409 abfd = abfd->plugin_dummy_bfd;
3410 hdr = &elf_tdata (abfd)->symtab_hdr;
3412 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3413 hdr = &elf_tdata (abfd)->symtab_hdr;
3415 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3419 /* The sh_info field of the symtab header tells us where the
3420 external symbols start. We don't care about the local symbols. */
3421 if (elf_bad_symtab (abfd))
3423 extsymcount = symcount;
3428 extsymcount = symcount - hdr->sh_info;
3429 extsymoff = hdr->sh_info;
3432 if (extsymcount == 0)
3435 /* Read in the symbol table. */
3436 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3438 if (isymbuf == NULL)
3441 /* Scan the symbol table looking for SYMDEF. */
3443 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3447 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3452 if (strcmp (name, symdef->name) == 0)
3454 result = is_global_data_symbol_definition (abfd, isym);
3464 /* Add an entry to the .dynamic table. */
3467 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3471 struct elf_link_hash_table *hash_table;
3472 const struct elf_backend_data *bed;
3474 bfd_size_type newsize;
3475 bfd_byte *newcontents;
3476 Elf_Internal_Dyn dyn;
3478 hash_table = elf_hash_table (info);
3479 if (! is_elf_hash_table (hash_table))
3482 if (tag == DT_RELA || tag == DT_REL)
3483 hash_table->dynamic_relocs = TRUE;
3485 bed = get_elf_backend_data (hash_table->dynobj);
3486 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3487 BFD_ASSERT (s != NULL);
3489 newsize = s->size + bed->s->sizeof_dyn;
3490 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3491 if (newcontents == NULL)
3495 dyn.d_un.d_val = val;
3496 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3499 s->contents = newcontents;
3504 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3505 otherwise just check whether one already exists. Returns -1 on error,
3506 1 if a DT_NEEDED tag already exists, and 0 on success. */
3509 elf_add_dt_needed_tag (bfd *abfd,
3510 struct bfd_link_info *info,
3514 struct elf_link_hash_table *hash_table;
3517 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3520 hash_table = elf_hash_table (info);
3521 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3522 if (strindex == (size_t) -1)
3525 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3528 const struct elf_backend_data *bed;
3531 bed = get_elf_backend_data (hash_table->dynobj);
3532 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3534 for (extdyn = sdyn->contents;
3535 extdyn < sdyn->contents + sdyn->size;
3536 extdyn += bed->s->sizeof_dyn)
3538 Elf_Internal_Dyn dyn;
3540 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3541 if (dyn.d_tag == DT_NEEDED
3542 && dyn.d_un.d_val == strindex)
3544 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3552 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3555 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3559 /* We were just checking for existence of the tag. */
3560 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3565 /* Return true if SONAME is on the needed list between NEEDED and STOP
3566 (or the end of list if STOP is NULL), and needed by a library that
3570 on_needed_list (const char *soname,
3571 struct bfd_link_needed_list *needed,
3572 struct bfd_link_needed_list *stop)
3574 struct bfd_link_needed_list *look;
3575 for (look = needed; look != stop; look = look->next)
3576 if (strcmp (soname, look->name) == 0
3577 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
3578 /* If needed by a library that itself is not directly
3579 needed, recursively check whether that library is
3580 indirectly needed. Since we add DT_NEEDED entries to
3581 the end of the list, library dependencies appear after
3582 the library. Therefore search prior to the current
3583 LOOK, preventing possible infinite recursion. */
3584 || on_needed_list (elf_dt_name (look->by), needed, look)))
3590 /* Sort symbol by value, section, and size. */
3592 elf_sort_symbol (const void *arg1, const void *arg2)
3594 const struct elf_link_hash_entry *h1;
3595 const struct elf_link_hash_entry *h2;
3596 bfd_signed_vma vdiff;
3598 h1 = *(const struct elf_link_hash_entry **) arg1;
3599 h2 = *(const struct elf_link_hash_entry **) arg2;
3600 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3602 return vdiff > 0 ? 1 : -1;
3605 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3607 return sdiff > 0 ? 1 : -1;
3609 vdiff = h1->size - h2->size;
3610 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3613 /* This function is used to adjust offsets into .dynstr for
3614 dynamic symbols. This is called via elf_link_hash_traverse. */
3617 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3619 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3621 if (h->dynindx != -1)
3622 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3626 /* Assign string offsets in .dynstr, update all structures referencing
3630 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3632 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3633 struct elf_link_local_dynamic_entry *entry;
3634 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3635 bfd *dynobj = hash_table->dynobj;
3638 const struct elf_backend_data *bed;
3641 _bfd_elf_strtab_finalize (dynstr);
3642 size = _bfd_elf_strtab_size (dynstr);
3644 bed = get_elf_backend_data (dynobj);
3645 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3646 BFD_ASSERT (sdyn != NULL);
3648 /* Update all .dynamic entries referencing .dynstr strings. */
3649 for (extdyn = sdyn->contents;
3650 extdyn < sdyn->contents + sdyn->size;
3651 extdyn += bed->s->sizeof_dyn)
3653 Elf_Internal_Dyn dyn;
3655 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3659 dyn.d_un.d_val = size;
3669 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3674 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3677 /* Now update local dynamic symbols. */
3678 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3679 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3680 entry->isym.st_name);
3682 /* And the rest of dynamic symbols. */
3683 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3685 /* Adjust version definitions. */
3686 if (elf_tdata (output_bfd)->cverdefs)
3691 Elf_Internal_Verdef def;
3692 Elf_Internal_Verdaux defaux;
3694 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3698 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3700 p += sizeof (Elf_External_Verdef);
3701 if (def.vd_aux != sizeof (Elf_External_Verdef))
3703 for (i = 0; i < def.vd_cnt; ++i)
3705 _bfd_elf_swap_verdaux_in (output_bfd,
3706 (Elf_External_Verdaux *) p, &defaux);
3707 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3709 _bfd_elf_swap_verdaux_out (output_bfd,
3710 &defaux, (Elf_External_Verdaux *) p);
3711 p += sizeof (Elf_External_Verdaux);
3714 while (def.vd_next);
3717 /* Adjust version references. */
3718 if (elf_tdata (output_bfd)->verref)
3723 Elf_Internal_Verneed need;
3724 Elf_Internal_Vernaux needaux;
3726 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3730 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3732 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3733 _bfd_elf_swap_verneed_out (output_bfd, &need,
3734 (Elf_External_Verneed *) p);
3735 p += sizeof (Elf_External_Verneed);
3736 for (i = 0; i < need.vn_cnt; ++i)
3738 _bfd_elf_swap_vernaux_in (output_bfd,
3739 (Elf_External_Vernaux *) p, &needaux);
3740 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3742 _bfd_elf_swap_vernaux_out (output_bfd,
3744 (Elf_External_Vernaux *) p);
3745 p += sizeof (Elf_External_Vernaux);
3748 while (need.vn_next);
3754 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3755 The default is to only match when the INPUT and OUTPUT are exactly
3759 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3760 const bfd_target *output)
3762 return input == output;
3765 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3766 This version is used when different targets for the same architecture
3767 are virtually identical. */
3770 _bfd_elf_relocs_compatible (const bfd_target *input,
3771 const bfd_target *output)
3773 const struct elf_backend_data *obed, *ibed;
3775 if (input == output)
3778 ibed = xvec_get_elf_backend_data (input);
3779 obed = xvec_get_elf_backend_data (output);
3781 if (ibed->arch != obed->arch)
3784 /* If both backends are using this function, deem them compatible. */
3785 return ibed->relocs_compatible == obed->relocs_compatible;
3788 /* Make a special call to the linker "notice" function to tell it that
3789 we are about to handle an as-needed lib, or have finished
3790 processing the lib. */
3793 _bfd_elf_notice_as_needed (bfd *ibfd,
3794 struct bfd_link_info *info,
3795 enum notice_asneeded_action act)
3797 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3800 /* Check relocations an ELF object file. */
3803 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
3805 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3806 struct elf_link_hash_table *htab = elf_hash_table (info);
3808 /* If this object is the same format as the output object, and it is
3809 not a shared library, then let the backend look through the
3812 This is required to build global offset table entries and to
3813 arrange for dynamic relocs. It is not required for the
3814 particular common case of linking non PIC code, even when linking
3815 against shared libraries, but unfortunately there is no way of
3816 knowing whether an object file has been compiled PIC or not.
3817 Looking through the relocs is not particularly time consuming.
3818 The problem is that we must either (1) keep the relocs in memory,
3819 which causes the linker to require additional runtime memory or
3820 (2) read the relocs twice from the input file, which wastes time.
3821 This would be a good case for using mmap.
3823 I have no idea how to handle linking PIC code into a file of a
3824 different format. It probably can't be done. */
3825 if ((abfd->flags & DYNAMIC) == 0
3826 && is_elf_hash_table (htab)
3827 && bed->check_relocs != NULL
3828 && elf_object_id (abfd) == elf_hash_table_id (htab)
3829 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
3833 for (o = abfd->sections; o != NULL; o = o->next)
3835 Elf_Internal_Rela *internal_relocs;
3838 /* Don't check relocations in excluded sections. */
3839 if ((o->flags & SEC_RELOC) == 0
3840 || (o->flags & SEC_EXCLUDE) != 0
3841 || o->reloc_count == 0
3842 || ((info->strip == strip_all || info->strip == strip_debugger)
3843 && (o->flags & SEC_DEBUGGING) != 0)
3844 || bfd_is_abs_section (o->output_section))
3847 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
3849 if (internal_relocs == NULL)
3852 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
3854 if (elf_section_data (o)->relocs != internal_relocs)
3855 free (internal_relocs);
3865 /* Add symbols from an ELF object file to the linker hash table. */
3868 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3870 Elf_Internal_Ehdr *ehdr;
3871 Elf_Internal_Shdr *hdr;
3875 struct elf_link_hash_entry **sym_hash;
3876 bfd_boolean dynamic;
3877 Elf_External_Versym *extversym = NULL;
3878 Elf_External_Versym *extversym_end = NULL;
3879 Elf_External_Versym *ever;
3880 struct elf_link_hash_entry *weaks;
3881 struct elf_link_hash_entry **nondeflt_vers = NULL;
3882 size_t nondeflt_vers_cnt = 0;
3883 Elf_Internal_Sym *isymbuf = NULL;
3884 Elf_Internal_Sym *isym;
3885 Elf_Internal_Sym *isymend;
3886 const struct elf_backend_data *bed;
3887 bfd_boolean add_needed;
3888 struct elf_link_hash_table *htab;
3890 void *alloc_mark = NULL;
3891 struct bfd_hash_entry **old_table = NULL;
3892 unsigned int old_size = 0;
3893 unsigned int old_count = 0;
3894 void *old_tab = NULL;
3896 struct bfd_link_hash_entry *old_undefs = NULL;
3897 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3898 void *old_strtab = NULL;
3901 bfd_boolean just_syms;
3903 htab = elf_hash_table (info);
3904 bed = get_elf_backend_data (abfd);
3906 if ((abfd->flags & DYNAMIC) == 0)
3912 /* You can't use -r against a dynamic object. Also, there's no
3913 hope of using a dynamic object which does not exactly match
3914 the format of the output file. */
3915 if (bfd_link_relocatable (info)
3916 || !is_elf_hash_table (htab)
3917 || info->output_bfd->xvec != abfd->xvec)
3919 if (bfd_link_relocatable (info))
3920 bfd_set_error (bfd_error_invalid_operation);
3922 bfd_set_error (bfd_error_wrong_format);
3927 ehdr = elf_elfheader (abfd);
3928 if (info->warn_alternate_em
3929 && bed->elf_machine_code != ehdr->e_machine
3930 && ((bed->elf_machine_alt1 != 0
3931 && ehdr->e_machine == bed->elf_machine_alt1)
3932 || (bed->elf_machine_alt2 != 0
3933 && ehdr->e_machine == bed->elf_machine_alt2)))
3935 /* xgettext:c-format */
3936 (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
3937 ehdr->e_machine, abfd, bed->elf_machine_code);
3939 /* As a GNU extension, any input sections which are named
3940 .gnu.warning.SYMBOL are treated as warning symbols for the given
3941 symbol. This differs from .gnu.warning sections, which generate
3942 warnings when they are included in an output file. */
3943 /* PR 12761: Also generate this warning when building shared libraries. */
3944 for (s = abfd->sections; s != NULL; s = s->next)
3948 name = bfd_section_name (s);
3949 if (CONST_STRNEQ (name, ".gnu.warning."))
3954 name += sizeof ".gnu.warning." - 1;
3956 /* If this is a shared object, then look up the symbol
3957 in the hash table. If it is there, and it is already
3958 been defined, then we will not be using the entry
3959 from this shared object, so we don't need to warn.
3960 FIXME: If we see the definition in a regular object
3961 later on, we will warn, but we shouldn't. The only
3962 fix is to keep track of what warnings we are supposed
3963 to emit, and then handle them all at the end of the
3967 struct elf_link_hash_entry *h;
3969 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3971 /* FIXME: What about bfd_link_hash_common? */
3973 && (h->root.type == bfd_link_hash_defined
3974 || h->root.type == bfd_link_hash_defweak))
3979 msg = (char *) bfd_alloc (abfd, sz + 1);
3983 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3988 if (! (_bfd_generic_link_add_one_symbol
3989 (info, abfd, name, BSF_WARNING, s, 0, msg,
3990 FALSE, bed->collect, NULL)))
3993 if (bfd_link_executable (info))
3995 /* Clobber the section size so that the warning does
3996 not get copied into the output file. */
3999 /* Also set SEC_EXCLUDE, so that symbols defined in
4000 the warning section don't get copied to the output. */
4001 s->flags |= SEC_EXCLUDE;
4006 just_syms = ((s = abfd->sections) != NULL
4007 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
4012 /* If we are creating a shared library, create all the dynamic
4013 sections immediately. We need to attach them to something,
4014 so we attach them to this BFD, provided it is the right
4015 format and is not from ld --just-symbols. Always create the
4016 dynamic sections for -E/--dynamic-list. FIXME: If there
4017 are no input BFD's of the same format as the output, we can't
4018 make a shared library. */
4020 && (bfd_link_pic (info)
4021 || (!bfd_link_relocatable (info)
4023 && (info->export_dynamic || info->dynamic)))
4024 && is_elf_hash_table (htab)
4025 && info->output_bfd->xvec == abfd->xvec
4026 && !htab->dynamic_sections_created)
4028 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
4032 else if (!is_elf_hash_table (htab))
4036 const char *soname = NULL;
4038 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
4039 const Elf_Internal_Phdr *phdr;
4042 /* ld --just-symbols and dynamic objects don't mix very well.
4043 ld shouldn't allow it. */
4047 /* If this dynamic lib was specified on the command line with
4048 --as-needed in effect, then we don't want to add a DT_NEEDED
4049 tag unless the lib is actually used. Similary for libs brought
4050 in by another lib's DT_NEEDED. When --no-add-needed is used
4051 on a dynamic lib, we don't want to add a DT_NEEDED entry for
4052 any dynamic library in DT_NEEDED tags in the dynamic lib at
4054 add_needed = (elf_dyn_lib_class (abfd)
4055 & (DYN_AS_NEEDED | DYN_DT_NEEDED
4056 | DYN_NO_NEEDED)) == 0;
4058 s = bfd_get_section_by_name (abfd, ".dynamic");
4063 unsigned int elfsec;
4064 unsigned long shlink;
4066 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
4073 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
4074 if (elfsec == SHN_BAD)
4075 goto error_free_dyn;
4076 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
4078 for (extdyn = dynbuf;
4079 extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
4080 extdyn += bed->s->sizeof_dyn)
4082 Elf_Internal_Dyn dyn;
4084 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
4085 if (dyn.d_tag == DT_SONAME)
4087 unsigned int tagv = dyn.d_un.d_val;
4088 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4090 goto error_free_dyn;
4092 if (dyn.d_tag == DT_NEEDED)
4094 struct bfd_link_needed_list *n, **pn;
4096 unsigned int tagv = dyn.d_un.d_val;
4098 amt = sizeof (struct bfd_link_needed_list);
4099 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4100 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4101 if (n == NULL || fnm == NULL)
4102 goto error_free_dyn;
4103 amt = strlen (fnm) + 1;
4104 anm = (char *) bfd_alloc (abfd, amt);
4106 goto error_free_dyn;
4107 memcpy (anm, fnm, amt);
4111 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
4115 if (dyn.d_tag == DT_RUNPATH)
4117 struct bfd_link_needed_list *n, **pn;
4119 unsigned int tagv = dyn.d_un.d_val;
4121 amt = sizeof (struct bfd_link_needed_list);
4122 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4123 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4124 if (n == NULL || fnm == NULL)
4125 goto error_free_dyn;
4126 amt = strlen (fnm) + 1;
4127 anm = (char *) bfd_alloc (abfd, amt);
4129 goto error_free_dyn;
4130 memcpy (anm, fnm, amt);
4134 for (pn = & runpath;
4140 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
4141 if (!runpath && dyn.d_tag == DT_RPATH)
4143 struct bfd_link_needed_list *n, **pn;
4145 unsigned int tagv = dyn.d_un.d_val;
4147 amt = sizeof (struct bfd_link_needed_list);
4148 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
4149 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4150 if (n == NULL || fnm == NULL)
4151 goto error_free_dyn;
4152 amt = strlen (fnm) + 1;
4153 anm = (char *) bfd_alloc (abfd, amt);
4155 goto error_free_dyn;
4156 memcpy (anm, fnm, amt);
4166 if (dyn.d_tag == DT_AUDIT)
4168 unsigned int tagv = dyn.d_un.d_val;
4169 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
4176 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
4177 frees all more recently bfd_alloc'd blocks as well. */
4183 struct bfd_link_needed_list **pn;
4184 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
4189 /* If we have a PT_GNU_RELRO program header, mark as read-only
4190 all sections contained fully therein. This makes relro
4191 shared library sections appear as they will at run-time. */
4192 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
4193 while (phdr-- > elf_tdata (abfd)->phdr)
4194 if (phdr->p_type == PT_GNU_RELRO)
4196 for (s = abfd->sections; s != NULL; s = s->next)
4197 if ((s->flags & SEC_ALLOC) != 0
4198 && s->vma >= phdr->p_vaddr
4199 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
4200 s->flags |= SEC_READONLY;
4204 /* We do not want to include any of the sections in a dynamic
4205 object in the output file. We hack by simply clobbering the
4206 list of sections in the BFD. This could be handled more
4207 cleanly by, say, a new section flag; the existing
4208 SEC_NEVER_LOAD flag is not the one we want, because that one
4209 still implies that the section takes up space in the output
4211 bfd_section_list_clear (abfd);
4213 /* Find the name to use in a DT_NEEDED entry that refers to this
4214 object. If the object has a DT_SONAME entry, we use it.
4215 Otherwise, if the generic linker stuck something in
4216 elf_dt_name, we use that. Otherwise, we just use the file
4218 if (soname == NULL || *soname == '\0')
4220 soname = elf_dt_name (abfd);
4221 if (soname == NULL || *soname == '\0')
4222 soname = bfd_get_filename (abfd);
4225 /* Save the SONAME because sometimes the linker emulation code
4226 will need to know it. */
4227 elf_dt_name (abfd) = soname;
4229 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4233 /* If we have already included this dynamic object in the
4234 link, just ignore it. There is no reason to include a
4235 particular dynamic object more than once. */
4239 /* Save the DT_AUDIT entry for the linker emulation code. */
4240 elf_dt_audit (abfd) = audit;
4243 /* If this is a dynamic object, we always link against the .dynsym
4244 symbol table, not the .symtab symbol table. The dynamic linker
4245 will only see the .dynsym symbol table, so there is no reason to
4246 look at .symtab for a dynamic object. */
4248 if (! dynamic || elf_dynsymtab (abfd) == 0)
4249 hdr = &elf_tdata (abfd)->symtab_hdr;
4251 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4253 symcount = hdr->sh_size / bed->s->sizeof_sym;
4255 /* The sh_info field of the symtab header tells us where the
4256 external symbols start. We don't care about the local symbols at
4258 if (elf_bad_symtab (abfd))
4260 extsymcount = symcount;
4265 extsymcount = symcount - hdr->sh_info;
4266 extsymoff = hdr->sh_info;
4269 sym_hash = elf_sym_hashes (abfd);
4270 if (extsymcount != 0)
4272 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
4274 if (isymbuf == NULL)
4277 if (sym_hash == NULL)
4279 /* We store a pointer to the hash table entry for each
4282 amt *= sizeof (struct elf_link_hash_entry *);
4283 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
4284 if (sym_hash == NULL)
4285 goto error_free_sym;
4286 elf_sym_hashes (abfd) = sym_hash;
4292 /* Read in any version definitions. */
4293 if (!_bfd_elf_slurp_version_tables (abfd,
4294 info->default_imported_symver))
4295 goto error_free_sym;
4297 /* Read in the symbol versions, but don't bother to convert them
4298 to internal format. */
4299 if (elf_dynversym (abfd) != 0)
4301 Elf_Internal_Shdr *versymhdr;
4303 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
4304 amt = versymhdr->sh_size;
4305 extversym = (Elf_External_Versym *) bfd_malloc (amt);
4306 if (extversym == NULL)
4307 goto error_free_sym;
4308 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
4309 || bfd_bread (extversym, amt, abfd) != amt)
4310 goto error_free_vers;
4311 extversym_end = extversym + (amt / sizeof (* extversym));
4315 /* If we are loading an as-needed shared lib, save the symbol table
4316 state before we start adding symbols. If the lib turns out
4317 to be unneeded, restore the state. */
4318 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4323 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
4325 struct bfd_hash_entry *p;
4326 struct elf_link_hash_entry *h;
4328 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4330 h = (struct elf_link_hash_entry *) p;
4331 entsize += htab->root.table.entsize;
4332 if (h->root.type == bfd_link_hash_warning)
4333 entsize += htab->root.table.entsize;
4337 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
4338 old_tab = bfd_malloc (tabsize + entsize);
4339 if (old_tab == NULL)
4340 goto error_free_vers;
4342 /* Remember the current objalloc pointer, so that all mem for
4343 symbols added can later be reclaimed. */
4344 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
4345 if (alloc_mark == NULL)
4346 goto error_free_vers;
4348 /* Make a special call to the linker "notice" function to
4349 tell it that we are about to handle an as-needed lib. */
4350 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
4351 goto error_free_vers;
4353 /* Clone the symbol table. Remember some pointers into the
4354 symbol table, and dynamic symbol count. */
4355 old_ent = (char *) old_tab + tabsize;
4356 memcpy (old_tab, htab->root.table.table, tabsize);
4357 old_undefs = htab->root.undefs;
4358 old_undefs_tail = htab->root.undefs_tail;
4359 old_table = htab->root.table.table;
4360 old_size = htab->root.table.size;
4361 old_count = htab->root.table.count;
4362 old_strtab = _bfd_elf_strtab_save (htab->dynstr);
4363 if (old_strtab == NULL)
4364 goto error_free_vers;
4366 for (i = 0; i < htab->root.table.size; i++)
4368 struct bfd_hash_entry *p;
4369 struct elf_link_hash_entry *h;
4371 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4373 memcpy (old_ent, p, htab->root.table.entsize);
4374 old_ent = (char *) old_ent + htab->root.table.entsize;
4375 h = (struct elf_link_hash_entry *) p;
4376 if (h->root.type == bfd_link_hash_warning)
4378 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
4379 old_ent = (char *) old_ent + htab->root.table.entsize;
4386 if (extversym == NULL)
4388 else if (extversym + extsymoff < extversym_end)
4389 ever = extversym + extsymoff;
4392 /* xgettext:c-format */
4393 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
4394 abfd, (long) extsymoff,
4395 (long) (extversym_end - extversym) / sizeof (* extversym));
4396 bfd_set_error (bfd_error_bad_value);
4397 goto error_free_vers;
4400 if (!bfd_link_relocatable (info)
4401 && abfd->lto_slim_object)
4404 (_("%pB: plugin needed to handle lto object"), abfd);
4407 for (isym = isymbuf, isymend = isymbuf + extsymcount;
4409 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
4413 asection *sec, *new_sec;
4416 struct elf_link_hash_entry *h;
4417 struct elf_link_hash_entry *hi;
4418 bfd_boolean definition;
4419 bfd_boolean size_change_ok;
4420 bfd_boolean type_change_ok;
4421 bfd_boolean new_weak;
4422 bfd_boolean old_weak;
4423 bfd_boolean override;
4425 bfd_boolean discarded;
4426 unsigned int old_alignment;
4427 unsigned int shindex;
4429 bfd_boolean matched;
4433 flags = BSF_NO_FLAGS;
4435 value = isym->st_value;
4436 common = bed->common_definition (isym);
4437 if (common && info->inhibit_common_definition)
4439 /* Treat common symbol as undefined for --no-define-common. */
4440 isym->st_shndx = SHN_UNDEF;
4445 bind = ELF_ST_BIND (isym->st_info);
4449 /* This should be impossible, since ELF requires that all
4450 global symbols follow all local symbols, and that sh_info
4451 point to the first global symbol. Unfortunately, Irix 5
4453 if (elf_bad_symtab (abfd))
4456 /* If we aren't prepared to handle locals within the globals
4457 then we'll likely segfault on a NULL symbol hash if the
4458 symbol is ever referenced in relocations. */
4459 shindex = elf_elfheader (abfd)->e_shstrndx;
4460 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
4461 _bfd_error_handler (_("%pB: %s local symbol at index %lu"
4462 " (>= sh_info of %lu)"),
4463 abfd, name, (long) (isym - isymbuf + extsymoff),
4466 /* Dynamic object relocations are not processed by ld, so
4467 ld won't run into the problem mentioned above. */
4470 bfd_set_error (bfd_error_bad_value);
4471 goto error_free_vers;
4474 if (isym->st_shndx != SHN_UNDEF && !common)
4482 case STB_GNU_UNIQUE:
4483 flags = BSF_GNU_UNIQUE;
4487 /* Leave it up to the processor backend. */
4491 if (isym->st_shndx == SHN_UNDEF)
4492 sec = bfd_und_section_ptr;
4493 else if (isym->st_shndx == SHN_ABS)
4494 sec = bfd_abs_section_ptr;
4495 else if (isym->st_shndx == SHN_COMMON)
4497 sec = bfd_com_section_ptr;
4498 /* What ELF calls the size we call the value. What ELF
4499 calls the value we call the alignment. */
4500 value = isym->st_size;
4504 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4506 sec = bfd_abs_section_ptr;
4507 else if (discarded_section (sec))
4509 /* Symbols from discarded section are undefined. We keep
4511 sec = bfd_und_section_ptr;
4513 isym->st_shndx = SHN_UNDEF;
4515 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4519 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4522 goto error_free_vers;
4524 if (isym->st_shndx == SHN_COMMON
4525 && (abfd->flags & BFD_PLUGIN) != 0)
4527 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4531 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4533 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4535 goto error_free_vers;
4539 else if (isym->st_shndx == SHN_COMMON
4540 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4541 && !bfd_link_relocatable (info))
4543 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4547 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4548 | SEC_LINKER_CREATED);
4549 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4551 goto error_free_vers;
4555 else if (bed->elf_add_symbol_hook)
4557 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4559 goto error_free_vers;
4561 /* The hook function sets the name to NULL if this symbol
4562 should be skipped for some reason. */
4567 /* Sanity check that all possibilities were handled. */
4571 /* Silently discard TLS symbols from --just-syms. There's
4572 no way to combine a static TLS block with a new TLS block
4573 for this executable. */
4574 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4575 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4578 if (bfd_is_und_section (sec)
4579 || bfd_is_com_section (sec))
4584 size_change_ok = FALSE;
4585 type_change_ok = bed->type_change_ok;
4592 if (is_elf_hash_table (htab))
4594 Elf_Internal_Versym iver;
4595 unsigned int vernum = 0;
4600 if (info->default_imported_symver)
4601 /* Use the default symbol version created earlier. */
4602 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4606 else if (ever >= extversym_end)
4608 /* xgettext:c-format */
4609 _bfd_error_handler (_("%pB: not enough version information"),
4611 bfd_set_error (bfd_error_bad_value);
4612 goto error_free_vers;
4615 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4617 vernum = iver.vs_vers & VERSYM_VERSION;
4619 /* If this is a hidden symbol, or if it is not version
4620 1, we append the version name to the symbol name.
4621 However, we do not modify a non-hidden absolute symbol
4622 if it is not a function, because it might be the version
4623 symbol itself. FIXME: What if it isn't? */
4624 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4626 && (!bfd_is_abs_section (sec)
4627 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4630 size_t namelen, verlen, newlen;
4633 if (isym->st_shndx != SHN_UNDEF)
4635 if (vernum > elf_tdata (abfd)->cverdefs)
4637 else if (vernum > 1)
4639 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4646 /* xgettext:c-format */
4647 (_("%pB: %s: invalid version %u (max %d)"),
4649 elf_tdata (abfd)->cverdefs);
4650 bfd_set_error (bfd_error_bad_value);
4651 goto error_free_vers;
4656 /* We cannot simply test for the number of
4657 entries in the VERNEED section since the
4658 numbers for the needed versions do not start
4660 Elf_Internal_Verneed *t;
4663 for (t = elf_tdata (abfd)->verref;
4667 Elf_Internal_Vernaux *a;
4669 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4671 if (a->vna_other == vernum)
4673 verstr = a->vna_nodename;
4683 /* xgettext:c-format */
4684 (_("%pB: %s: invalid needed version %d"),
4685 abfd, name, vernum);
4686 bfd_set_error (bfd_error_bad_value);
4687 goto error_free_vers;
4691 namelen = strlen (name);
4692 verlen = strlen (verstr);
4693 newlen = namelen + verlen + 2;
4694 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4695 && isym->st_shndx != SHN_UNDEF)
4698 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4699 if (newname == NULL)
4700 goto error_free_vers;
4701 memcpy (newname, name, namelen);
4702 p = newname + namelen;
4704 /* If this is a defined non-hidden version symbol,
4705 we add another @ to the name. This indicates the
4706 default version of the symbol. */
4707 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4708 && isym->st_shndx != SHN_UNDEF)
4710 memcpy (p, verstr, verlen + 1);
4715 /* If this symbol has default visibility and the user has
4716 requested we not re-export it, then mark it as hidden. */
4717 if (!bfd_is_und_section (sec)
4720 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4721 isym->st_other = (STV_HIDDEN
4722 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4724 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4725 sym_hash, &old_bfd, &old_weak,
4726 &old_alignment, &skip, &override,
4727 &type_change_ok, &size_change_ok,
4729 goto error_free_vers;
4734 /* Override a definition only if the new symbol matches the
4736 if (override && matched)
4740 while (h->root.type == bfd_link_hash_indirect
4741 || h->root.type == bfd_link_hash_warning)
4742 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4744 if (elf_tdata (abfd)->verdef != NULL
4747 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4750 if (! (_bfd_generic_link_add_one_symbol
4751 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4752 (struct bfd_link_hash_entry **) sym_hash)))
4753 goto error_free_vers;
4756 /* We need to make sure that indirect symbol dynamic flags are
4759 while (h->root.type == bfd_link_hash_indirect
4760 || h->root.type == bfd_link_hash_warning)
4761 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4763 /* Setting the index to -3 tells elf_link_output_extsym that
4764 this symbol is defined in a discarded section. */
4770 new_weak = (flags & BSF_WEAK) != 0;
4774 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4775 && is_elf_hash_table (htab)
4776 && h->u.alias == NULL)
4778 /* Keep a list of all weak defined non function symbols from
4779 a dynamic object, using the alias field. Later in this
4780 function we will set the alias field to the correct
4781 value. We only put non-function symbols from dynamic
4782 objects on this list, because that happens to be the only
4783 time we need to know the normal symbol corresponding to a
4784 weak symbol, and the information is time consuming to
4785 figure out. If the alias field is not already NULL,
4786 then this symbol was already defined by some previous
4787 dynamic object, and we will be using that previous
4788 definition anyhow. */
4794 /* Set the alignment of a common symbol. */
4795 if ((common || bfd_is_com_section (sec))
4796 && h->root.type == bfd_link_hash_common)
4801 align = bfd_log2 (isym->st_value);
4804 /* The new symbol is a common symbol in a shared object.
4805 We need to get the alignment from the section. */
4806 align = new_sec->alignment_power;
4808 if (align > old_alignment)
4809 h->root.u.c.p->alignment_power = align;
4811 h->root.u.c.p->alignment_power = old_alignment;
4814 if (is_elf_hash_table (htab))
4816 /* Set a flag in the hash table entry indicating the type of
4817 reference or definition we just found. A dynamic symbol
4818 is one which is referenced or defined by both a regular
4819 object and a shared object. */
4820 bfd_boolean dynsym = FALSE;
4822 /* Plugin symbols aren't normal. Don't set def_regular or
4823 ref_regular for them, or make them dynamic. */
4824 if ((abfd->flags & BFD_PLUGIN) != 0)
4831 if (bind != STB_WEAK)
4832 h->ref_regular_nonweak = 1;
4844 /* If the indirect symbol has been forced local, don't
4845 make the real symbol dynamic. */
4846 if ((h == hi || !hi->forced_local)
4847 && (bfd_link_dll (info)
4857 hi->ref_dynamic = 1;
4862 hi->def_dynamic = 1;
4865 /* If the indirect symbol has been forced local, don't
4866 make the real symbol dynamic. */
4867 if ((h == hi || !hi->forced_local)
4871 && weakdef (h)->dynindx != -1)))
4875 /* Check to see if we need to add an indirect symbol for
4876 the default name. */
4878 || (!override && h->root.type == bfd_link_hash_common))
4879 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4880 sec, value, &old_bfd, &dynsym))
4881 goto error_free_vers;
4883 /* Check the alignment when a common symbol is involved. This
4884 can change when a common symbol is overridden by a normal
4885 definition or a common symbol is ignored due to the old
4886 normal definition. We need to make sure the maximum
4887 alignment is maintained. */
4888 if ((old_alignment || common)
4889 && h->root.type != bfd_link_hash_common)
4891 unsigned int common_align;
4892 unsigned int normal_align;
4893 unsigned int symbol_align;
4897 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4898 || h->root.type == bfd_link_hash_defweak);
4900 symbol_align = ffs (h->root.u.def.value) - 1;
4901 if (h->root.u.def.section->owner != NULL
4902 && (h->root.u.def.section->owner->flags
4903 & (DYNAMIC | BFD_PLUGIN)) == 0)
4905 normal_align = h->root.u.def.section->alignment_power;
4906 if (normal_align > symbol_align)
4907 normal_align = symbol_align;
4910 normal_align = symbol_align;
4914 common_align = old_alignment;
4915 common_bfd = old_bfd;
4920 common_align = bfd_log2 (isym->st_value);
4922 normal_bfd = old_bfd;
4925 if (normal_align < common_align)
4927 /* PR binutils/2735 */
4928 if (normal_bfd == NULL)
4930 /* xgettext:c-format */
4931 (_("warning: alignment %u of common symbol `%s' in %pB is"
4932 " greater than the alignment (%u) of its section %pA"),
4933 1 << common_align, name, common_bfd,
4934 1 << normal_align, h->root.u.def.section);
4937 /* xgettext:c-format */
4938 (_("warning: alignment %u of symbol `%s' in %pB"
4939 " is smaller than %u in %pB"),
4940 1 << normal_align, name, normal_bfd,
4941 1 << common_align, common_bfd);
4945 /* Remember the symbol size if it isn't undefined. */
4946 if (isym->st_size != 0
4947 && isym->st_shndx != SHN_UNDEF
4948 && (definition || h->size == 0))
4951 && h->size != isym->st_size
4952 && ! size_change_ok)
4954 /* xgettext:c-format */
4955 (_("warning: size of symbol `%s' changed"
4956 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
4957 name, (uint64_t) h->size, old_bfd,
4958 (uint64_t) isym->st_size, abfd);
4960 h->size = isym->st_size;
4963 /* If this is a common symbol, then we always want H->SIZE
4964 to be the size of the common symbol. The code just above
4965 won't fix the size if a common symbol becomes larger. We
4966 don't warn about a size change here, because that is
4967 covered by --warn-common. Allow changes between different
4969 if (h->root.type == bfd_link_hash_common)
4970 h->size = h->root.u.c.size;
4972 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4973 && ((definition && !new_weak)
4974 || (old_weak && h->root.type == bfd_link_hash_common)
4975 || h->type == STT_NOTYPE))
4977 unsigned int type = ELF_ST_TYPE (isym->st_info);
4979 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4981 if (type == STT_GNU_IFUNC
4982 && (abfd->flags & DYNAMIC) != 0)
4985 if (h->type != type)
4987 if (h->type != STT_NOTYPE && ! type_change_ok)
4988 /* xgettext:c-format */
4990 (_("warning: type of symbol `%s' changed"
4991 " from %d to %d in %pB"),
4992 name, h->type, type, abfd);
4998 /* Merge st_other field. */
4999 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
5001 /* We don't want to make debug symbol dynamic. */
5003 && (sec->flags & SEC_DEBUGGING)
5004 && !bfd_link_relocatable (info))
5007 /* Nor should we make plugin symbols dynamic. */
5008 if ((abfd->flags & BFD_PLUGIN) != 0)
5013 h->target_internal = isym->st_target_internal;
5014 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
5017 if (definition && !dynamic)
5019 char *p = strchr (name, ELF_VER_CHR);
5020 if (p != NULL && p[1] != ELF_VER_CHR)
5022 /* Queue non-default versions so that .symver x, x@FOO
5023 aliases can be checked. */
5026 amt = ((isymend - isym + 1)
5027 * sizeof (struct elf_link_hash_entry *));
5029 = (struct elf_link_hash_entry **) bfd_malloc (amt);
5031 goto error_free_vers;
5033 nondeflt_vers[nondeflt_vers_cnt++] = h;
5037 if (dynsym && h->dynindx == -1)
5039 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5040 goto error_free_vers;
5042 && weakdef (h)->dynindx == -1)
5044 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
5045 goto error_free_vers;
5048 else if (h->dynindx != -1)
5049 /* If the symbol already has a dynamic index, but
5050 visibility says it should not be visible, turn it into
5052 switch (ELF_ST_VISIBILITY (h->other))
5056 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
5061 /* Don't add DT_NEEDED for references from the dummy bfd nor
5062 for unmatched symbol. */
5067 && h->ref_regular_nonweak
5069 || (old_bfd->flags & BFD_PLUGIN) == 0))
5070 || (h->ref_dynamic_nonweak
5071 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
5072 && !on_needed_list (elf_dt_name (abfd),
5073 htab->needed, NULL))))
5076 const char *soname = elf_dt_name (abfd);
5078 info->callbacks->minfo ("%!", soname, old_bfd,
5079 h->root.root.string);
5081 /* A symbol from a library loaded via DT_NEEDED of some
5082 other library is referenced by a regular object.
5083 Add a DT_NEEDED entry for it. Issue an error if
5084 --no-add-needed is used and the reference was not
5087 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
5090 /* xgettext:c-format */
5091 (_("%pB: undefined reference to symbol '%s'"),
5093 bfd_set_error (bfd_error_missing_dso);
5094 goto error_free_vers;
5097 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
5098 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
5101 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
5103 goto error_free_vers;
5105 BFD_ASSERT (ret == 0);
5110 if (info->lto_plugin_active
5111 && !bfd_link_relocatable (info)
5112 && (abfd->flags & BFD_PLUGIN) == 0
5118 if (bed->s->arch_size == 32)
5123 /* If linker plugin is enabled, set non_ir_ref_regular on symbols
5124 referenced in regular objects so that linker plugin will get
5125 the correct symbol resolution. */
5127 sym_hash = elf_sym_hashes (abfd);
5128 for (s = abfd->sections; s != NULL; s = s->next)
5130 Elf_Internal_Rela *internal_relocs;
5131 Elf_Internal_Rela *rel, *relend;
5133 /* Don't check relocations in excluded sections. */
5134 if ((s->flags & SEC_RELOC) == 0
5135 || s->reloc_count == 0
5136 || (s->flags & SEC_EXCLUDE) != 0
5137 || ((info->strip == strip_all
5138 || info->strip == strip_debugger)
5139 && (s->flags & SEC_DEBUGGING) != 0))
5142 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
5145 if (internal_relocs == NULL)
5146 goto error_free_vers;
5148 rel = internal_relocs;
5149 relend = rel + s->reloc_count;
5150 for ( ; rel < relend; rel++)
5152 unsigned long r_symndx = rel->r_info >> r_sym_shift;
5153 struct elf_link_hash_entry *h;
5155 /* Skip local symbols. */
5156 if (r_symndx < extsymoff)
5159 h = sym_hash[r_symndx - extsymoff];
5161 h->root.non_ir_ref_regular = 1;
5164 if (elf_section_data (s)->relocs != internal_relocs)
5165 free (internal_relocs);
5169 if (extversym != NULL)
5175 if (isymbuf != NULL)
5181 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
5185 /* Restore the symbol table. */
5186 old_ent = (char *) old_tab + tabsize;
5187 memset (elf_sym_hashes (abfd), 0,
5188 extsymcount * sizeof (struct elf_link_hash_entry *));
5189 htab->root.table.table = old_table;
5190 htab->root.table.size = old_size;
5191 htab->root.table.count = old_count;
5192 memcpy (htab->root.table.table, old_tab, tabsize);
5193 htab->root.undefs = old_undefs;
5194 htab->root.undefs_tail = old_undefs_tail;
5195 _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
5198 for (i = 0; i < htab->root.table.size; i++)
5200 struct bfd_hash_entry *p;
5201 struct elf_link_hash_entry *h;
5203 unsigned int alignment_power;
5204 unsigned int non_ir_ref_dynamic;
5206 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
5208 h = (struct elf_link_hash_entry *) p;
5209 if (h->root.type == bfd_link_hash_warning)
5210 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5212 /* Preserve the maximum alignment and size for common
5213 symbols even if this dynamic lib isn't on DT_NEEDED
5214 since it can still be loaded at run time by another
5216 if (h->root.type == bfd_link_hash_common)
5218 size = h->root.u.c.size;
5219 alignment_power = h->root.u.c.p->alignment_power;
5224 alignment_power = 0;
5226 /* Preserve non_ir_ref_dynamic so that this symbol
5227 will be exported when the dynamic lib becomes needed
5228 in the second pass. */
5229 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
5230 memcpy (p, old_ent, htab->root.table.entsize);
5231 old_ent = (char *) old_ent + htab->root.table.entsize;
5232 h = (struct elf_link_hash_entry *) p;
5233 if (h->root.type == bfd_link_hash_warning)
5235 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
5236 old_ent = (char *) old_ent + htab->root.table.entsize;
5237 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5239 if (h->root.type == bfd_link_hash_common)
5241 if (size > h->root.u.c.size)
5242 h->root.u.c.size = size;
5243 if (alignment_power > h->root.u.c.p->alignment_power)
5244 h->root.u.c.p->alignment_power = alignment_power;
5246 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
5250 /* Make a special call to the linker "notice" function to
5251 tell it that symbols added for crefs may need to be removed. */
5252 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
5253 goto error_free_vers;
5256 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
5258 if (nondeflt_vers != NULL)
5259 free (nondeflt_vers);
5263 if (old_tab != NULL)
5265 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
5266 goto error_free_vers;
5271 /* Now that all the symbols from this input file are created, if
5272 not performing a relocatable link, handle .symver foo, foo@BAR
5273 such that any relocs against foo become foo@BAR. */
5274 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
5278 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
5280 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
5281 char *shortname, *p;
5283 p = strchr (h->root.root.string, ELF_VER_CHR);
5285 || (h->root.type != bfd_link_hash_defined
5286 && h->root.type != bfd_link_hash_defweak))
5289 amt = p - h->root.root.string;
5290 shortname = (char *) bfd_malloc (amt + 1);
5292 goto error_free_vers;
5293 memcpy (shortname, h->root.root.string, amt);
5294 shortname[amt] = '\0';
5296 hi = (struct elf_link_hash_entry *)
5297 bfd_link_hash_lookup (&htab->root, shortname,
5298 FALSE, FALSE, FALSE);
5300 && hi->root.type == h->root.type
5301 && hi->root.u.def.value == h->root.u.def.value
5302 && hi->root.u.def.section == h->root.u.def.section)
5304 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
5305 hi->root.type = bfd_link_hash_indirect;
5306 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
5307 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
5308 sym_hash = elf_sym_hashes (abfd);
5310 for (symidx = 0; symidx < extsymcount; ++symidx)
5311 if (sym_hash[symidx] == hi)
5313 sym_hash[symidx] = h;
5319 free (nondeflt_vers);
5320 nondeflt_vers = NULL;
5323 /* Now set the alias field correctly for all the weak defined
5324 symbols we found. The only way to do this is to search all the
5325 symbols. Since we only need the information for non functions in
5326 dynamic objects, that's the only time we actually put anything on
5327 the list WEAKS. We need this information so that if a regular
5328 object refers to a symbol defined weakly in a dynamic object, the
5329 real symbol in the dynamic object is also put in the dynamic
5330 symbols; we also must arrange for both symbols to point to the
5331 same memory location. We could handle the general case of symbol
5332 aliasing, but a general symbol alias can only be generated in
5333 assembler code, handling it correctly would be very time
5334 consuming, and other ELF linkers don't handle general aliasing
5338 struct elf_link_hash_entry **hpp;
5339 struct elf_link_hash_entry **hppend;
5340 struct elf_link_hash_entry **sorted_sym_hash;
5341 struct elf_link_hash_entry *h;
5344 /* Since we have to search the whole symbol list for each weak
5345 defined symbol, search time for N weak defined symbols will be
5346 O(N^2). Binary search will cut it down to O(NlogN). */
5348 amt *= sizeof (struct elf_link_hash_entry *);
5349 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
5350 if (sorted_sym_hash == NULL)
5352 sym_hash = sorted_sym_hash;
5353 hpp = elf_sym_hashes (abfd);
5354 hppend = hpp + extsymcount;
5356 for (; hpp < hppend; hpp++)
5360 && h->root.type == bfd_link_hash_defined
5361 && !bed->is_function_type (h->type))
5369 qsort (sorted_sym_hash, sym_count,
5370 sizeof (struct elf_link_hash_entry *),
5373 while (weaks != NULL)
5375 struct elf_link_hash_entry *hlook;
5378 size_t i, j, idx = 0;
5381 weaks = hlook->u.alias;
5382 hlook->u.alias = NULL;
5384 if (hlook->root.type != bfd_link_hash_defined
5385 && hlook->root.type != bfd_link_hash_defweak)
5388 slook = hlook->root.u.def.section;
5389 vlook = hlook->root.u.def.value;
5395 bfd_signed_vma vdiff;
5397 h = sorted_sym_hash[idx];
5398 vdiff = vlook - h->root.u.def.value;
5405 int sdiff = slook->id - h->root.u.def.section->id;
5415 /* We didn't find a value/section match. */
5419 /* With multiple aliases, or when the weak symbol is already
5420 strongly defined, we have multiple matching symbols and
5421 the binary search above may land on any of them. Step
5422 one past the matching symbol(s). */
5425 h = sorted_sym_hash[idx];
5426 if (h->root.u.def.section != slook
5427 || h->root.u.def.value != vlook)
5431 /* Now look back over the aliases. Since we sorted by size
5432 as well as value and section, we'll choose the one with
5433 the largest size. */
5436 h = sorted_sym_hash[idx];
5438 /* Stop if value or section doesn't match. */
5439 if (h->root.u.def.section != slook
5440 || h->root.u.def.value != vlook)
5442 else if (h != hlook)
5444 struct elf_link_hash_entry *t;
5447 hlook->is_weakalias = 1;
5449 if (t->u.alias != NULL)
5450 while (t->u.alias != h)
5454 /* If the weak definition is in the list of dynamic
5455 symbols, make sure the real definition is put
5457 if (hlook->dynindx != -1 && h->dynindx == -1)
5459 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5462 free (sorted_sym_hash);
5467 /* If the real definition is in the list of dynamic
5468 symbols, make sure the weak definition is put
5469 there as well. If we don't do this, then the
5470 dynamic loader might not merge the entries for the
5471 real definition and the weak definition. */
5472 if (h->dynindx != -1 && hlook->dynindx == -1)
5474 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
5475 goto err_free_sym_hash;
5482 free (sorted_sym_hash);
5485 if (bed->check_directives
5486 && !(*bed->check_directives) (abfd, info))
5489 /* If this is a non-traditional link, try to optimize the handling
5490 of the .stab/.stabstr sections. */
5492 && ! info->traditional_format
5493 && is_elf_hash_table (htab)
5494 && (info->strip != strip_all && info->strip != strip_debugger))
5498 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
5499 if (stabstr != NULL)
5501 bfd_size_type string_offset = 0;
5504 for (stab = abfd->sections; stab; stab = stab->next)
5505 if (CONST_STRNEQ (stab->name, ".stab")
5506 && (!stab->name[5] ||
5507 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
5508 && (stab->flags & SEC_MERGE) == 0
5509 && !bfd_is_abs_section (stab->output_section))
5511 struct bfd_elf_section_data *secdata;
5513 secdata = elf_section_data (stab);
5514 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
5515 stabstr, &secdata->sec_info,
5518 if (secdata->sec_info)
5519 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5524 if (is_elf_hash_table (htab) && add_needed)
5526 /* Add this bfd to the loaded list. */
5527 struct elf_link_loaded_list *n;
5529 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5533 n->next = htab->loaded;
5540 if (old_tab != NULL)
5542 if (old_strtab != NULL)
5544 if (nondeflt_vers != NULL)
5545 free (nondeflt_vers);
5546 if (extversym != NULL)
5549 if (isymbuf != NULL)
5555 /* Return the linker hash table entry of a symbol that might be
5556 satisfied by an archive symbol. Return -1 on error. */
5558 struct elf_link_hash_entry *
5559 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5560 struct bfd_link_info *info,
5563 struct elf_link_hash_entry *h;
5567 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5571 /* If this is a default version (the name contains @@), look up the
5572 symbol again with only one `@' as well as without the version.
5573 The effect is that references to the symbol with and without the
5574 version will be matched by the default symbol in the archive. */
5576 p = strchr (name, ELF_VER_CHR);
5577 if (p == NULL || p[1] != ELF_VER_CHR)
5580 /* First check with only one `@'. */
5581 len = strlen (name);
5582 copy = (char *) bfd_alloc (abfd, len);
5584 return (struct elf_link_hash_entry *) -1;
5586 first = p - name + 1;
5587 memcpy (copy, name, first);
5588 memcpy (copy + first, name + first + 1, len - first);
5590 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5593 /* We also need to check references to the symbol without the
5595 copy[first - 1] = '\0';
5596 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5597 FALSE, FALSE, TRUE);
5600 bfd_release (abfd, copy);
5604 /* Add symbols from an ELF archive file to the linker hash table. We
5605 don't use _bfd_generic_link_add_archive_symbols because we need to
5606 handle versioned symbols.
5608 Fortunately, ELF archive handling is simpler than that done by
5609 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5610 oddities. In ELF, if we find a symbol in the archive map, and the
5611 symbol is currently undefined, we know that we must pull in that
5614 Unfortunately, we do have to make multiple passes over the symbol
5615 table until nothing further is resolved. */
5618 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5621 unsigned char *included = NULL;
5625 const struct elf_backend_data *bed;
5626 struct elf_link_hash_entry * (*archive_symbol_lookup)
5627 (bfd *, struct bfd_link_info *, const char *);
5629 if (! bfd_has_map (abfd))
5631 /* An empty archive is a special case. */
5632 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5634 bfd_set_error (bfd_error_no_armap);
5638 /* Keep track of all symbols we know to be already defined, and all
5639 files we know to be already included. This is to speed up the
5640 second and subsequent passes. */
5641 c = bfd_ardata (abfd)->symdef_count;
5645 amt *= sizeof (*included);
5646 included = (unsigned char *) bfd_zmalloc (amt);
5647 if (included == NULL)
5650 symdefs = bfd_ardata (abfd)->symdefs;
5651 bed = get_elf_backend_data (abfd);
5652 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5665 symdefend = symdef + c;
5666 for (i = 0; symdef < symdefend; symdef++, i++)
5668 struct elf_link_hash_entry *h;
5670 struct bfd_link_hash_entry *undefs_tail;
5675 if (symdef->file_offset == last)
5681 h = archive_symbol_lookup (abfd, info, symdef->name);
5682 if (h == (struct elf_link_hash_entry *) -1)
5688 if (h->root.type == bfd_link_hash_common)
5690 /* We currently have a common symbol. The archive map contains
5691 a reference to this symbol, so we may want to include it. We
5692 only want to include it however, if this archive element
5693 contains a definition of the symbol, not just another common
5696 Unfortunately some archivers (including GNU ar) will put
5697 declarations of common symbols into their archive maps, as
5698 well as real definitions, so we cannot just go by the archive
5699 map alone. Instead we must read in the element's symbol
5700 table and check that to see what kind of symbol definition
5702 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5705 else if (h->root.type != bfd_link_hash_undefined)
5707 if (h->root.type != bfd_link_hash_undefweak)
5708 /* Symbol must be defined. Don't check it again. */
5713 /* We need to include this archive member. */
5714 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5715 if (element == NULL)
5718 if (! bfd_check_format (element, bfd_object))
5721 undefs_tail = info->hash->undefs_tail;
5723 if (!(*info->callbacks
5724 ->add_archive_element) (info, element, symdef->name, &element))
5726 if (!bfd_link_add_symbols (element, info))
5729 /* If there are any new undefined symbols, we need to make
5730 another pass through the archive in order to see whether
5731 they can be defined. FIXME: This isn't perfect, because
5732 common symbols wind up on undefs_tail and because an
5733 undefined symbol which is defined later on in this pass
5734 does not require another pass. This isn't a bug, but it
5735 does make the code less efficient than it could be. */
5736 if (undefs_tail != info->hash->undefs_tail)
5739 /* Look backward to mark all symbols from this object file
5740 which we have already seen in this pass. */
5744 included[mark] = TRUE;
5749 while (symdefs[mark].file_offset == symdef->file_offset);
5751 /* We mark subsequent symbols from this object file as we go
5752 on through the loop. */
5753 last = symdef->file_offset;
5763 if (included != NULL)
5768 /* Given an ELF BFD, add symbols to the global hash table as
5772 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5774 switch (bfd_get_format (abfd))
5777 return elf_link_add_object_symbols (abfd, info);
5779 return elf_link_add_archive_symbols (abfd, info);
5781 bfd_set_error (bfd_error_wrong_format);
5786 struct hash_codes_info
5788 unsigned long *hashcodes;
5792 /* This function will be called though elf_link_hash_traverse to store
5793 all hash value of the exported symbols in an array. */
5796 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5798 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5803 /* Ignore indirect symbols. These are added by the versioning code. */
5804 if (h->dynindx == -1)
5807 name = h->root.root.string;
5808 if (h->versioned >= versioned)
5810 char *p = strchr (name, ELF_VER_CHR);
5813 alc = (char *) bfd_malloc (p - name + 1);
5819 memcpy (alc, name, p - name);
5820 alc[p - name] = '\0';
5825 /* Compute the hash value. */
5826 ha = bfd_elf_hash (name);
5828 /* Store the found hash value in the array given as the argument. */
5829 *(inf->hashcodes)++ = ha;
5831 /* And store it in the struct so that we can put it in the hash table
5833 h->u.elf_hash_value = ha;
5841 struct collect_gnu_hash_codes
5844 const struct elf_backend_data *bed;
5845 unsigned long int nsyms;
5846 unsigned long int maskbits;
5847 unsigned long int *hashcodes;
5848 unsigned long int *hashval;
5849 unsigned long int *indx;
5850 unsigned long int *counts;
5854 long int min_dynindx;
5855 unsigned long int bucketcount;
5856 unsigned long int symindx;
5857 long int local_indx;
5858 long int shift1, shift2;
5859 unsigned long int mask;
5863 /* This function will be called though elf_link_hash_traverse to store
5864 all hash value of the exported symbols in an array. */
5867 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5869 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5874 /* Ignore indirect symbols. These are added by the versioning code. */
5875 if (h->dynindx == -1)
5878 /* Ignore also local symbols and undefined symbols. */
5879 if (! (*s->bed->elf_hash_symbol) (h))
5882 name = h->root.root.string;
5883 if (h->versioned >= versioned)
5885 char *p = strchr (name, ELF_VER_CHR);
5888 alc = (char *) bfd_malloc (p - name + 1);
5894 memcpy (alc, name, p - name);
5895 alc[p - name] = '\0';
5900 /* Compute the hash value. */
5901 ha = bfd_elf_gnu_hash (name);
5903 /* Store the found hash value in the array for compute_bucket_count,
5904 and also for .dynsym reordering purposes. */
5905 s->hashcodes[s->nsyms] = ha;
5906 s->hashval[h->dynindx] = ha;
5908 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5909 s->min_dynindx = h->dynindx;
5917 /* This function will be called though elf_link_hash_traverse to do
5918 final dynamic symbol renumbering in case of .gnu.hash.
5919 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
5920 to the translation table. */
5923 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
5925 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5926 unsigned long int bucket;
5927 unsigned long int val;
5929 /* Ignore indirect symbols. */
5930 if (h->dynindx == -1)
5933 /* Ignore also local symbols and undefined symbols. */
5934 if (! (*s->bed->elf_hash_symbol) (h))
5936 if (h->dynindx >= s->min_dynindx)
5938 if (s->bed->record_xhash_symbol != NULL)
5940 (*s->bed->record_xhash_symbol) (h, 0);
5944 h->dynindx = s->local_indx++;
5949 bucket = s->hashval[h->dynindx] % s->bucketcount;
5950 val = (s->hashval[h->dynindx] >> s->shift1)
5951 & ((s->maskbits >> s->shift1) - 1);
5952 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5954 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5955 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5956 if (s->counts[bucket] == 1)
5957 /* Last element terminates the chain. */
5959 bfd_put_32 (s->output_bfd, val,
5960 s->contents + (s->indx[bucket] - s->symindx) * 4);
5961 --s->counts[bucket];
5962 if (s->bed->record_xhash_symbol != NULL)
5964 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
5966 (*s->bed->record_xhash_symbol) (h, xlat_loc);
5969 h->dynindx = s->indx[bucket]++;
5973 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5976 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5978 return !(h->forced_local
5979 || h->root.type == bfd_link_hash_undefined
5980 || h->root.type == bfd_link_hash_undefweak
5981 || ((h->root.type == bfd_link_hash_defined
5982 || h->root.type == bfd_link_hash_defweak)
5983 && h->root.u.def.section->output_section == NULL));
5986 /* Array used to determine the number of hash table buckets to use
5987 based on the number of symbols there are. If there are fewer than
5988 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5989 fewer than 37 we use 17 buckets, and so forth. We never use more
5990 than 32771 buckets. */
5992 static const size_t elf_buckets[] =
5994 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5998 /* Compute bucket count for hashing table. We do not use a static set
5999 of possible tables sizes anymore. Instead we determine for all
6000 possible reasonable sizes of the table the outcome (i.e., the
6001 number of collisions etc) and choose the best solution. The
6002 weighting functions are not too simple to allow the table to grow
6003 without bounds. Instead one of the weighting factors is the size.
6004 Therefore the result is always a good payoff between few collisions
6005 (= short chain lengths) and table size. */
6007 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
6008 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
6009 unsigned long int nsyms,
6012 size_t best_size = 0;
6013 unsigned long int i;
6015 /* We have a problem here. The following code to optimize the table
6016 size requires an integer type with more the 32 bits. If
6017 BFD_HOST_U_64_BIT is set we know about such a type. */
6018 #ifdef BFD_HOST_U_64_BIT
6023 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
6024 bfd *dynobj = elf_hash_table (info)->dynobj;
6025 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
6026 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
6027 unsigned long int *counts;
6029 unsigned int no_improvement_count = 0;
6031 /* Possible optimization parameters: if we have NSYMS symbols we say
6032 that the hashing table must at least have NSYMS/4 and at most
6034 minsize = nsyms / 4;
6037 best_size = maxsize = nsyms * 2;
6042 if ((best_size & 31) == 0)
6046 /* Create array where we count the collisions in. We must use bfd_malloc
6047 since the size could be large. */
6049 amt *= sizeof (unsigned long int);
6050 counts = (unsigned long int *) bfd_malloc (amt);
6054 /* Compute the "optimal" size for the hash table. The criteria is a
6055 minimal chain length. The minor criteria is (of course) the size
6057 for (i = minsize; i < maxsize; ++i)
6059 /* Walk through the array of hashcodes and count the collisions. */
6060 BFD_HOST_U_64_BIT max;
6061 unsigned long int j;
6062 unsigned long int fact;
6064 if (gnu_hash && (i & 31) == 0)
6067 memset (counts, '\0', i * sizeof (unsigned long int));
6069 /* Determine how often each hash bucket is used. */
6070 for (j = 0; j < nsyms; ++j)
6071 ++counts[hashcodes[j] % i];
6073 /* For the weight function we need some information about the
6074 pagesize on the target. This is information need not be 100%
6075 accurate. Since this information is not available (so far) we
6076 define it here to a reasonable default value. If it is crucial
6077 to have a better value some day simply define this value. */
6078 # ifndef BFD_TARGET_PAGESIZE
6079 # define BFD_TARGET_PAGESIZE (4096)
6082 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
6084 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
6087 /* Variant 1: optimize for short chains. We add the squares
6088 of all the chain lengths (which favors many small chain
6089 over a few long chains). */
6090 for (j = 0; j < i; ++j)
6091 max += counts[j] * counts[j];
6093 /* This adds penalties for the overall size of the table. */
6094 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6097 /* Variant 2: Optimize a lot more for small table. Here we
6098 also add squares of the size but we also add penalties for
6099 empty slots (the +1 term). */
6100 for (j = 0; j < i; ++j)
6101 max += (1 + counts[j]) * (1 + counts[j]);
6103 /* The overall size of the table is considered, but not as
6104 strong as in variant 1, where it is squared. */
6105 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
6109 /* Compare with current best results. */
6110 if (max < best_chlen)
6114 no_improvement_count = 0;
6116 /* PR 11843: Avoid futile long searches for the best bucket size
6117 when there are a large number of symbols. */
6118 else if (++no_improvement_count == 100)
6125 #endif /* defined (BFD_HOST_U_64_BIT) */
6127 /* This is the fallback solution if no 64bit type is available or if we
6128 are not supposed to spend much time on optimizations. We select the
6129 bucket count using a fixed set of numbers. */
6130 for (i = 0; elf_buckets[i] != 0; i++)
6132 best_size = elf_buckets[i];
6133 if (nsyms < elf_buckets[i + 1])
6136 if (gnu_hash && best_size < 2)
6143 /* Size any SHT_GROUP section for ld -r. */
6146 _bfd_elf_size_group_sections (struct bfd_link_info *info)
6151 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6152 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6153 && (s = ibfd->sections) != NULL
6154 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
6155 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
6160 /* Set a default stack segment size. The value in INFO wins. If it
6161 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
6162 undefined it is initialized. */
6165 bfd_elf_stack_segment_size (bfd *output_bfd,
6166 struct bfd_link_info *info,
6167 const char *legacy_symbol,
6168 bfd_vma default_size)
6170 struct elf_link_hash_entry *h = NULL;
6172 /* Look for legacy symbol. */
6174 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
6175 FALSE, FALSE, FALSE);
6176 if (h && (h->root.type == bfd_link_hash_defined
6177 || h->root.type == bfd_link_hash_defweak)
6179 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
6181 /* The symbol has no type if specified on the command line. */
6182 h->type = STT_OBJECT;
6183 if (info->stacksize)
6184 /* xgettext:c-format */
6185 _bfd_error_handler (_("%pB: stack size specified and %s set"),
6186 output_bfd, legacy_symbol);
6187 else if (h->root.u.def.section != bfd_abs_section_ptr)
6188 /* xgettext:c-format */
6189 _bfd_error_handler (_("%pB: %s not absolute"),
6190 output_bfd, legacy_symbol);
6192 info->stacksize = h->root.u.def.value;
6195 if (!info->stacksize)
6196 /* If the user didn't set a size, or explicitly inhibit the
6197 size, set it now. */
6198 info->stacksize = default_size;
6200 /* Provide the legacy symbol, if it is referenced. */
6201 if (h && (h->root.type == bfd_link_hash_undefined
6202 || h->root.type == bfd_link_hash_undefweak))
6204 struct bfd_link_hash_entry *bh = NULL;
6206 if (!(_bfd_generic_link_add_one_symbol
6207 (info, output_bfd, legacy_symbol,
6208 BSF_GLOBAL, bfd_abs_section_ptr,
6209 info->stacksize >= 0 ? info->stacksize : 0,
6210 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
6213 h = (struct elf_link_hash_entry *) bh;
6215 h->type = STT_OBJECT;
6221 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
6223 struct elf_gc_sweep_symbol_info
6225 struct bfd_link_info *info;
6226 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
6231 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
6234 && (((h->root.type == bfd_link_hash_defined
6235 || h->root.type == bfd_link_hash_defweak)
6236 && !((h->def_regular || ELF_COMMON_DEF_P (h))
6237 && h->root.u.def.section->gc_mark))
6238 || h->root.type == bfd_link_hash_undefined
6239 || h->root.type == bfd_link_hash_undefweak))
6241 struct elf_gc_sweep_symbol_info *inf;
6243 inf = (struct elf_gc_sweep_symbol_info *) data;
6244 (*inf->hide_symbol) (inf->info, h, TRUE);
6247 h->ref_regular_nonweak = 0;
6253 /* Set up the sizes and contents of the ELF dynamic sections. This is
6254 called by the ELF linker emulation before_allocation routine. We
6255 must set the sizes of the sections before the linker sets the
6256 addresses of the various sections. */
6259 bfd_elf_size_dynamic_sections (bfd *output_bfd,
6262 const char *filter_shlib,
6264 const char *depaudit,
6265 const char * const *auxiliary_filters,
6266 struct bfd_link_info *info,
6267 asection **sinterpptr)
6270 const struct elf_backend_data *bed;
6274 if (!is_elf_hash_table (info->hash))
6277 dynobj = elf_hash_table (info)->dynobj;
6279 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6281 struct bfd_elf_version_tree *verdefs;
6282 struct elf_info_failed asvinfo;
6283 struct bfd_elf_version_tree *t;
6284 struct bfd_elf_version_expr *d;
6288 /* If we are supposed to export all symbols into the dynamic symbol
6289 table (this is not the normal case), then do so. */
6290 if (info->export_dynamic
6291 || (bfd_link_executable (info) && info->dynamic))
6293 struct elf_info_failed eif;
6297 elf_link_hash_traverse (elf_hash_table (info),
6298 _bfd_elf_export_symbol,
6306 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6308 if (soname_indx == (size_t) -1
6309 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
6313 soname_indx = (size_t) -1;
6315 /* Make all global versions with definition. */
6316 for (t = info->version_info; t != NULL; t = t->next)
6317 for (d = t->globals.list; d != NULL; d = d->next)
6318 if (!d->symver && d->literal)
6320 const char *verstr, *name;
6321 size_t namelen, verlen, newlen;
6322 char *newname, *p, leading_char;
6323 struct elf_link_hash_entry *newh;
6325 leading_char = bfd_get_symbol_leading_char (output_bfd);
6327 namelen = strlen (name) + (leading_char != '\0');
6329 verlen = strlen (verstr);
6330 newlen = namelen + verlen + 3;
6332 newname = (char *) bfd_malloc (newlen);
6333 if (newname == NULL)
6335 newname[0] = leading_char;
6336 memcpy (newname + (leading_char != '\0'), name, namelen);
6338 /* Check the hidden versioned definition. */
6339 p = newname + namelen;
6341 memcpy (p, verstr, verlen + 1);
6342 newh = elf_link_hash_lookup (elf_hash_table (info),
6343 newname, FALSE, FALSE,
6346 || (newh->root.type != bfd_link_hash_defined
6347 && newh->root.type != bfd_link_hash_defweak))
6349 /* Check the default versioned definition. */
6351 memcpy (p, verstr, verlen + 1);
6352 newh = elf_link_hash_lookup (elf_hash_table (info),
6353 newname, FALSE, FALSE,
6358 /* Mark this version if there is a definition and it is
6359 not defined in a shared object. */
6361 && !newh->def_dynamic
6362 && (newh->root.type == bfd_link_hash_defined
6363 || newh->root.type == bfd_link_hash_defweak))
6367 /* Attach all the symbols to their version information. */
6368 asvinfo.info = info;
6369 asvinfo.failed = FALSE;
6371 elf_link_hash_traverse (elf_hash_table (info),
6372 _bfd_elf_link_assign_sym_version,
6377 if (!info->allow_undefined_version)
6379 /* Check if all global versions have a definition. */
6380 bfd_boolean all_defined = TRUE;
6381 for (t = info->version_info; t != NULL; t = t->next)
6382 for (d = t->globals.list; d != NULL; d = d->next)
6383 if (d->literal && !d->symver && !d->script)
6386 (_("%s: undefined version: %s"),
6387 d->pattern, t->name);
6388 all_defined = FALSE;
6393 bfd_set_error (bfd_error_bad_value);
6398 /* Set up the version definition section. */
6399 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6400 BFD_ASSERT (s != NULL);
6402 /* We may have created additional version definitions if we are
6403 just linking a regular application. */
6404 verdefs = info->version_info;
6406 /* Skip anonymous version tag. */
6407 if (verdefs != NULL && verdefs->vernum == 0)
6408 verdefs = verdefs->next;
6410 if (verdefs == NULL && !info->create_default_symver)
6411 s->flags |= SEC_EXCLUDE;
6417 Elf_Internal_Verdef def;
6418 Elf_Internal_Verdaux defaux;
6419 struct bfd_link_hash_entry *bh;
6420 struct elf_link_hash_entry *h;
6426 /* Make space for the base version. */
6427 size += sizeof (Elf_External_Verdef);
6428 size += sizeof (Elf_External_Verdaux);
6431 /* Make space for the default version. */
6432 if (info->create_default_symver)
6434 size += sizeof (Elf_External_Verdef);
6438 for (t = verdefs; t != NULL; t = t->next)
6440 struct bfd_elf_version_deps *n;
6442 /* Don't emit base version twice. */
6446 size += sizeof (Elf_External_Verdef);
6447 size += sizeof (Elf_External_Verdaux);
6450 for (n = t->deps; n != NULL; n = n->next)
6451 size += sizeof (Elf_External_Verdaux);
6455 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6456 if (s->contents == NULL && s->size != 0)
6459 /* Fill in the version definition section. */
6463 def.vd_version = VER_DEF_CURRENT;
6464 def.vd_flags = VER_FLG_BASE;
6467 if (info->create_default_symver)
6469 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6470 def.vd_next = sizeof (Elf_External_Verdef);
6474 def.vd_aux = sizeof (Elf_External_Verdef);
6475 def.vd_next = (sizeof (Elf_External_Verdef)
6476 + sizeof (Elf_External_Verdaux));
6479 if (soname_indx != (size_t) -1)
6481 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6483 def.vd_hash = bfd_elf_hash (soname);
6484 defaux.vda_name = soname_indx;
6491 name = lbasename (output_bfd->filename);
6492 def.vd_hash = bfd_elf_hash (name);
6493 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6495 if (indx == (size_t) -1)
6497 defaux.vda_name = indx;
6499 defaux.vda_next = 0;
6501 _bfd_elf_swap_verdef_out (output_bfd, &def,
6502 (Elf_External_Verdef *) p);
6503 p += sizeof (Elf_External_Verdef);
6504 if (info->create_default_symver)
6506 /* Add a symbol representing this version. */
6508 if (! (_bfd_generic_link_add_one_symbol
6509 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6511 get_elf_backend_data (dynobj)->collect, &bh)))
6513 h = (struct elf_link_hash_entry *) bh;
6516 h->type = STT_OBJECT;
6517 h->verinfo.vertree = NULL;
6519 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6522 /* Create a duplicate of the base version with the same
6523 aux block, but different flags. */
6526 def.vd_aux = sizeof (Elf_External_Verdef);
6528 def.vd_next = (sizeof (Elf_External_Verdef)
6529 + sizeof (Elf_External_Verdaux));
6532 _bfd_elf_swap_verdef_out (output_bfd, &def,
6533 (Elf_External_Verdef *) p);
6534 p += sizeof (Elf_External_Verdef);
6536 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6537 (Elf_External_Verdaux *) p);
6538 p += sizeof (Elf_External_Verdaux);
6540 for (t = verdefs; t != NULL; t = t->next)
6543 struct bfd_elf_version_deps *n;
6545 /* Don't emit the base version twice. */
6550 for (n = t->deps; n != NULL; n = n->next)
6553 /* Add a symbol representing this version. */
6555 if (! (_bfd_generic_link_add_one_symbol
6556 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6558 get_elf_backend_data (dynobj)->collect, &bh)))
6560 h = (struct elf_link_hash_entry *) bh;
6563 h->type = STT_OBJECT;
6564 h->verinfo.vertree = t;
6566 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6569 def.vd_version = VER_DEF_CURRENT;
6571 if (t->globals.list == NULL
6572 && t->locals.list == NULL
6574 def.vd_flags |= VER_FLG_WEAK;
6575 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6576 def.vd_cnt = cdeps + 1;
6577 def.vd_hash = bfd_elf_hash (t->name);
6578 def.vd_aux = sizeof (Elf_External_Verdef);
6581 /* If a basever node is next, it *must* be the last node in
6582 the chain, otherwise Verdef construction breaks. */
6583 if (t->next != NULL && t->next->vernum == 0)
6584 BFD_ASSERT (t->next->next == NULL);
6586 if (t->next != NULL && t->next->vernum != 0)
6587 def.vd_next = (sizeof (Elf_External_Verdef)
6588 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6590 _bfd_elf_swap_verdef_out (output_bfd, &def,
6591 (Elf_External_Verdef *) p);
6592 p += sizeof (Elf_External_Verdef);
6594 defaux.vda_name = h->dynstr_index;
6595 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6597 defaux.vda_next = 0;
6598 if (t->deps != NULL)
6599 defaux.vda_next = sizeof (Elf_External_Verdaux);
6600 t->name_indx = defaux.vda_name;
6602 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6603 (Elf_External_Verdaux *) p);
6604 p += sizeof (Elf_External_Verdaux);
6606 for (n = t->deps; n != NULL; n = n->next)
6608 if (n->version_needed == NULL)
6610 /* This can happen if there was an error in the
6612 defaux.vda_name = 0;
6616 defaux.vda_name = n->version_needed->name_indx;
6617 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6620 if (n->next == NULL)
6621 defaux.vda_next = 0;
6623 defaux.vda_next = sizeof (Elf_External_Verdaux);
6625 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6626 (Elf_External_Verdaux *) p);
6627 p += sizeof (Elf_External_Verdaux);
6631 elf_tdata (output_bfd)->cverdefs = cdefs;
6635 bed = get_elf_backend_data (output_bfd);
6637 if (info->gc_sections && bed->can_gc_sections)
6639 struct elf_gc_sweep_symbol_info sweep_info;
6641 /* Remove the symbols that were in the swept sections from the
6642 dynamic symbol table. */
6643 sweep_info.info = info;
6644 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
6645 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
6649 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6652 struct elf_find_verdep_info sinfo;
6654 /* Work out the size of the version reference section. */
6656 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6657 BFD_ASSERT (s != NULL);
6660 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6661 if (sinfo.vers == 0)
6663 sinfo.failed = FALSE;
6665 elf_link_hash_traverse (elf_hash_table (info),
6666 _bfd_elf_link_find_version_dependencies,
6671 if (elf_tdata (output_bfd)->verref == NULL)
6672 s->flags |= SEC_EXCLUDE;
6675 Elf_Internal_Verneed *vn;
6680 /* Build the version dependency section. */
6683 for (vn = elf_tdata (output_bfd)->verref;
6685 vn = vn->vn_nextref)
6687 Elf_Internal_Vernaux *a;
6689 size += sizeof (Elf_External_Verneed);
6691 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6692 size += sizeof (Elf_External_Vernaux);
6696 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6697 if (s->contents == NULL)
6701 for (vn = elf_tdata (output_bfd)->verref;
6703 vn = vn->vn_nextref)
6706 Elf_Internal_Vernaux *a;
6710 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6713 vn->vn_version = VER_NEED_CURRENT;
6715 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6716 elf_dt_name (vn->vn_bfd) != NULL
6717 ? elf_dt_name (vn->vn_bfd)
6718 : lbasename (vn->vn_bfd->filename),
6720 if (indx == (size_t) -1)
6723 vn->vn_aux = sizeof (Elf_External_Verneed);
6724 if (vn->vn_nextref == NULL)
6727 vn->vn_next = (sizeof (Elf_External_Verneed)
6728 + caux * sizeof (Elf_External_Vernaux));
6730 _bfd_elf_swap_verneed_out (output_bfd, vn,
6731 (Elf_External_Verneed *) p);
6732 p += sizeof (Elf_External_Verneed);
6734 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
6736 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6737 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6738 a->vna_nodename, FALSE);
6739 if (indx == (size_t) -1)
6742 if (a->vna_nextptr == NULL)
6745 a->vna_next = sizeof (Elf_External_Vernaux);
6747 _bfd_elf_swap_vernaux_out (output_bfd, a,
6748 (Elf_External_Vernaux *) p);
6749 p += sizeof (Elf_External_Vernaux);
6753 elf_tdata (output_bfd)->cverrefs = crefs;
6757 /* Any syms created from now on start with -1 in
6758 got.refcount/offset and plt.refcount/offset. */
6759 elf_hash_table (info)->init_got_refcount
6760 = elf_hash_table (info)->init_got_offset;
6761 elf_hash_table (info)->init_plt_refcount
6762 = elf_hash_table (info)->init_plt_offset;
6764 if (bfd_link_relocatable (info)
6765 && !_bfd_elf_size_group_sections (info))
6768 /* The backend may have to create some sections regardless of whether
6769 we're dynamic or not. */
6770 if (bed->elf_backend_always_size_sections
6771 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
6774 /* Determine any GNU_STACK segment requirements, after the backend
6775 has had a chance to set a default segment size. */
6776 if (info->execstack)
6777 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
6778 else if (info->noexecstack)
6779 elf_stack_flags (output_bfd) = PF_R | PF_W;
6783 asection *notesec = NULL;
6786 for (inputobj = info->input_bfds;
6788 inputobj = inputobj->link.next)
6793 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
6795 s = inputobj->sections;
6796 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
6799 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
6802 if (s->flags & SEC_CODE)
6806 else if (bed->default_execstack)
6809 if (notesec || info->stacksize > 0)
6810 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
6811 if (notesec && exec && bfd_link_relocatable (info)
6812 && notesec->output_section != bfd_abs_section_ptr)
6813 notesec->output_section->flags |= SEC_CODE;
6816 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6818 struct elf_info_failed eif;
6819 struct elf_link_hash_entry *h;
6823 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
6824 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
6828 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
6830 info->flags |= DF_SYMBOLIC;
6838 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
6840 if (indx == (size_t) -1)
6843 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
6844 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
6848 if (filter_shlib != NULL)
6852 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6853 filter_shlib, TRUE);
6854 if (indx == (size_t) -1
6855 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
6859 if (auxiliary_filters != NULL)
6861 const char * const *p;
6863 for (p = auxiliary_filters; *p != NULL; p++)
6867 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6869 if (indx == (size_t) -1
6870 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
6879 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
6881 if (indx == (size_t) -1
6882 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
6886 if (depaudit != NULL)
6890 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
6892 if (indx == (size_t) -1
6893 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
6900 /* Find all symbols which were defined in a dynamic object and make
6901 the backend pick a reasonable value for them. */
6902 elf_link_hash_traverse (elf_hash_table (info),
6903 _bfd_elf_adjust_dynamic_symbol,
6908 /* Add some entries to the .dynamic section. We fill in some of the
6909 values later, in bfd_elf_final_link, but we must add the entries
6910 now so that we know the final size of the .dynamic section. */
6912 /* If there are initialization and/or finalization functions to
6913 call then add the corresponding DT_INIT/DT_FINI entries. */
6914 h = (info->init_function
6915 ? elf_link_hash_lookup (elf_hash_table (info),
6916 info->init_function, FALSE,
6923 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
6926 h = (info->fini_function
6927 ? elf_link_hash_lookup (elf_hash_table (info),
6928 info->fini_function, FALSE,
6935 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
6939 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
6940 if (s != NULL && s->linker_has_input)
6942 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6943 if (! bfd_link_executable (info))
6948 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
6949 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
6950 && (o = sub->sections) != NULL
6951 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
6952 for (o = sub->sections; o != NULL; o = o->next)
6953 if (elf_section_data (o)->this_hdr.sh_type
6954 == SHT_PREINIT_ARRAY)
6957 (_("%pB: .preinit_array section is not allowed in DSO"),
6962 bfd_set_error (bfd_error_nonrepresentable_section);
6966 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6967 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6970 s = bfd_get_section_by_name (output_bfd, ".init_array");
6971 if (s != NULL && s->linker_has_input)
6973 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6974 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6977 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6978 if (s != NULL && s->linker_has_input)
6980 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6981 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6985 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6986 /* If .dynstr is excluded from the link, we don't want any of
6987 these tags. Strictly, we should be checking each section
6988 individually; This quick check covers for the case where
6989 someone does a /DISCARD/ : { *(*) }. */
6990 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6992 bfd_size_type strsize;
6994 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6995 if ((info->emit_hash
6996 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6997 || (info->emit_gnu_hash
6998 && (bed->record_xhash_symbol == NULL
6999 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
7000 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
7001 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
7002 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
7003 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
7004 bed->s->sizeof_sym))
7009 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
7012 /* The backend must work out the sizes of all the other dynamic
7015 && bed->elf_backend_size_dynamic_sections != NULL
7016 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
7019 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
7021 if (elf_tdata (output_bfd)->cverdefs)
7023 unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
7025 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
7026 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
7030 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
7032 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
7035 else if (info->flags & DF_BIND_NOW)
7037 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
7043 if (bfd_link_executable (info))
7044 info->flags_1 &= ~ (DF_1_INITFIRST
7047 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
7051 if (elf_tdata (output_bfd)->cverrefs)
7053 unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
7055 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
7056 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
7060 if ((elf_tdata (output_bfd)->cverrefs == 0
7061 && elf_tdata (output_bfd)->cverdefs == 0)
7062 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
7066 s = bfd_get_linker_section (dynobj, ".gnu.version");
7067 s->flags |= SEC_EXCLUDE;
7073 /* Find the first non-excluded output section. We'll use its
7074 section symbol for some emitted relocs. */
7076 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
7079 asection *found = NULL;
7081 for (s = output_bfd->sections; s != NULL; s = s->next)
7082 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7083 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7086 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7089 elf_hash_table (info)->text_index_section = found;
7092 /* Find two non-excluded output sections, one for code, one for data.
7093 We'll use their section symbols for some emitted relocs. */
7095 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
7098 asection *found = NULL;
7100 /* Data first, since setting text_index_section changes
7101 _bfd_elf_omit_section_dynsym_default. */
7102 for (s = output_bfd->sections; s != NULL; s = s->next)
7103 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7104 && !(s->flags & SEC_READONLY)
7105 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7108 if ((s->flags & SEC_THREAD_LOCAL) == 0)
7111 elf_hash_table (info)->data_index_section = found;
7113 for (s = output_bfd->sections; s != NULL; s = s->next)
7114 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
7115 && (s->flags & SEC_READONLY)
7116 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
7121 elf_hash_table (info)->text_index_section = found;
7124 #define GNU_HASH_SECTION_NAME(bed) \
7125 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
7128 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
7130 const struct elf_backend_data *bed;
7131 unsigned long section_sym_count;
7132 bfd_size_type dynsymcount = 0;
7134 if (!is_elf_hash_table (info->hash))
7137 bed = get_elf_backend_data (output_bfd);
7138 (*bed->elf_backend_init_index_section) (output_bfd, info);
7140 /* Assign dynsym indices. In a shared library we generate a section
7141 symbol for each output section, which come first. Next come all
7142 of the back-end allocated local dynamic syms, followed by the rest
7143 of the global symbols.
7145 This is usually not needed for static binaries, however backends
7146 can request to always do it, e.g. the MIPS backend uses dynamic
7147 symbol counts to lay out GOT, which will be produced in the
7148 presence of GOT relocations even in static binaries (holding fixed
7149 data in that case, to satisfy those relocations). */
7151 if (elf_hash_table (info)->dynamic_sections_created
7152 || bed->always_renumber_dynsyms)
7153 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
7154 §ion_sym_count);
7156 if (elf_hash_table (info)->dynamic_sections_created)
7160 unsigned int dtagcount;
7162 dynobj = elf_hash_table (info)->dynobj;
7164 /* Work out the size of the symbol version section. */
7165 s = bfd_get_linker_section (dynobj, ".gnu.version");
7166 BFD_ASSERT (s != NULL);
7167 if ((s->flags & SEC_EXCLUDE) == 0)
7169 s->size = dynsymcount * sizeof (Elf_External_Versym);
7170 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7171 if (s->contents == NULL)
7174 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
7178 /* Set the size of the .dynsym and .hash sections. We counted
7179 the number of dynamic symbols in elf_link_add_object_symbols.
7180 We will build the contents of .dynsym and .hash when we build
7181 the final symbol table, because until then we do not know the
7182 correct value to give the symbols. We built the .dynstr
7183 section as we went along in elf_link_add_object_symbols. */
7184 s = elf_hash_table (info)->dynsym;
7185 BFD_ASSERT (s != NULL);
7186 s->size = dynsymcount * bed->s->sizeof_sym;
7188 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
7189 if (s->contents == NULL)
7192 /* The first entry in .dynsym is a dummy symbol. Clear all the
7193 section syms, in case we don't output them all. */
7194 ++section_sym_count;
7195 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
7197 elf_hash_table (info)->bucketcount = 0;
7199 /* Compute the size of the hashing table. As a side effect this
7200 computes the hash values for all the names we export. */
7201 if (info->emit_hash)
7203 unsigned long int *hashcodes;
7204 struct hash_codes_info hashinf;
7206 unsigned long int nsyms;
7208 size_t hash_entry_size;
7210 /* Compute the hash values for all exported symbols. At the same
7211 time store the values in an array so that we could use them for
7213 amt = dynsymcount * sizeof (unsigned long int);
7214 hashcodes = (unsigned long int *) bfd_malloc (amt);
7215 if (hashcodes == NULL)
7217 hashinf.hashcodes = hashcodes;
7218 hashinf.error = FALSE;
7220 /* Put all hash values in HASHCODES. */
7221 elf_link_hash_traverse (elf_hash_table (info),
7222 elf_collect_hash_codes, &hashinf);
7229 nsyms = hashinf.hashcodes - hashcodes;
7231 = compute_bucket_count (info, hashcodes, nsyms, 0);
7234 if (bucketcount == 0 && nsyms > 0)
7237 elf_hash_table (info)->bucketcount = bucketcount;
7239 s = bfd_get_linker_section (dynobj, ".hash");
7240 BFD_ASSERT (s != NULL);
7241 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
7242 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
7243 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7244 if (s->contents == NULL)
7247 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
7248 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
7249 s->contents + hash_entry_size);
7252 if (info->emit_gnu_hash)
7255 unsigned char *contents;
7256 struct collect_gnu_hash_codes cinfo;
7260 memset (&cinfo, 0, sizeof (cinfo));
7262 /* Compute the hash values for all exported symbols. At the same
7263 time store the values in an array so that we could use them for
7265 amt = dynsymcount * 2 * sizeof (unsigned long int);
7266 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
7267 if (cinfo.hashcodes == NULL)
7270 cinfo.hashval = cinfo.hashcodes + dynsymcount;
7271 cinfo.min_dynindx = -1;
7272 cinfo.output_bfd = output_bfd;
7275 /* Put all hash values in HASHCODES. */
7276 elf_link_hash_traverse (elf_hash_table (info),
7277 elf_collect_gnu_hash_codes, &cinfo);
7280 free (cinfo.hashcodes);
7285 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
7287 if (bucketcount == 0)
7289 free (cinfo.hashcodes);
7293 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
7294 BFD_ASSERT (s != NULL);
7296 if (cinfo.nsyms == 0)
7298 /* Empty .gnu.hash or .MIPS.xhash section is special. */
7299 BFD_ASSERT (cinfo.min_dynindx == -1);
7300 free (cinfo.hashcodes);
7301 s->size = 5 * 4 + bed->s->arch_size / 8;
7302 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7303 if (contents == NULL)
7305 s->contents = contents;
7306 /* 1 empty bucket. */
7307 bfd_put_32 (output_bfd, 1, contents);
7308 /* SYMIDX above the special symbol 0. */
7309 bfd_put_32 (output_bfd, 1, contents + 4);
7310 /* Just one word for bitmask. */
7311 bfd_put_32 (output_bfd, 1, contents + 8);
7312 /* Only hash fn bloom filter. */
7313 bfd_put_32 (output_bfd, 0, contents + 12);
7314 /* No hashes are valid - empty bitmask. */
7315 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
7316 /* No hashes in the only bucket. */
7317 bfd_put_32 (output_bfd, 0,
7318 contents + 16 + bed->s->arch_size / 8);
7322 unsigned long int maskwords, maskbitslog2, x;
7323 BFD_ASSERT (cinfo.min_dynindx != -1);
7327 while ((x >>= 1) != 0)
7329 if (maskbitslog2 < 3)
7331 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
7332 maskbitslog2 = maskbitslog2 + 3;
7334 maskbitslog2 = maskbitslog2 + 2;
7335 if (bed->s->arch_size == 64)
7337 if (maskbitslog2 == 5)
7343 cinfo.mask = (1 << cinfo.shift1) - 1;
7344 cinfo.shift2 = maskbitslog2;
7345 cinfo.maskbits = 1 << maskbitslog2;
7346 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
7347 amt = bucketcount * sizeof (unsigned long int) * 2;
7348 amt += maskwords * sizeof (bfd_vma);
7349 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
7350 if (cinfo.bitmask == NULL)
7352 free (cinfo.hashcodes);
7356 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
7357 cinfo.indx = cinfo.counts + bucketcount;
7358 cinfo.symindx = dynsymcount - cinfo.nsyms;
7359 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
7361 /* Determine how often each hash bucket is used. */
7362 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
7363 for (i = 0; i < cinfo.nsyms; ++i)
7364 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
7366 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
7367 if (cinfo.counts[i] != 0)
7369 cinfo.indx[i] = cnt;
7370 cnt += cinfo.counts[i];
7372 BFD_ASSERT (cnt == dynsymcount);
7373 cinfo.bucketcount = bucketcount;
7374 cinfo.local_indx = cinfo.min_dynindx;
7376 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
7377 s->size += cinfo.maskbits / 8;
7378 if (bed->record_xhash_symbol != NULL)
7379 s->size += cinfo.nsyms * 4;
7380 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
7381 if (contents == NULL)
7383 free (cinfo.bitmask);
7384 free (cinfo.hashcodes);
7388 s->contents = contents;
7389 bfd_put_32 (output_bfd, bucketcount, contents);
7390 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
7391 bfd_put_32 (output_bfd, maskwords, contents + 8);
7392 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
7393 contents += 16 + cinfo.maskbits / 8;
7395 for (i = 0; i < bucketcount; ++i)
7397 if (cinfo.counts[i] == 0)
7398 bfd_put_32 (output_bfd, 0, contents);
7400 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
7404 cinfo.contents = contents;
7406 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
7407 /* Renumber dynamic symbols, if populating .gnu.hash section.
7408 If using .MIPS.xhash, populate the translation table. */
7409 elf_link_hash_traverse (elf_hash_table (info),
7410 elf_gnu_hash_process_symidx, &cinfo);
7412 contents = s->contents + 16;
7413 for (i = 0; i < maskwords; ++i)
7415 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
7417 contents += bed->s->arch_size / 8;
7420 free (cinfo.bitmask);
7421 free (cinfo.hashcodes);
7425 s = bfd_get_linker_section (dynobj, ".dynstr");
7426 BFD_ASSERT (s != NULL);
7428 elf_finalize_dynstr (output_bfd, info);
7430 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
7432 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
7433 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
7440 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
7443 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
7446 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
7447 sec->sec_info_type = SEC_INFO_TYPE_NONE;
7450 /* Finish SHF_MERGE section merging. */
7453 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
7458 if (!is_elf_hash_table (info->hash))
7461 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7462 if ((ibfd->flags & DYNAMIC) == 0
7463 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
7464 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
7465 == get_elf_backend_data (obfd)->s->elfclass))
7466 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7467 if ((sec->flags & SEC_MERGE) != 0
7468 && !bfd_is_abs_section (sec->output_section))
7470 struct bfd_elf_section_data *secdata;
7472 secdata = elf_section_data (sec);
7473 if (! _bfd_add_merge_section (obfd,
7474 &elf_hash_table (info)->merge_info,
7475 sec, &secdata->sec_info))
7477 else if (secdata->sec_info)
7478 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
7481 if (elf_hash_table (info)->merge_info != NULL)
7482 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
7483 merge_sections_remove_hook);
7487 /* Create an entry in an ELF linker hash table. */
7489 struct bfd_hash_entry *
7490 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
7491 struct bfd_hash_table *table,
7494 /* Allocate the structure if it has not already been allocated by a
7498 entry = (struct bfd_hash_entry *)
7499 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
7504 /* Call the allocation method of the superclass. */
7505 entry = _bfd_link_hash_newfunc (entry, table, string);
7508 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
7509 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
7511 /* Set local fields. */
7514 ret->got = htab->init_got_refcount;
7515 ret->plt = htab->init_plt_refcount;
7516 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
7517 - offsetof (struct elf_link_hash_entry, size)));
7518 /* Assume that we have been called by a non-ELF symbol reader.
7519 This flag is then reset by the code which reads an ELF input
7520 file. This ensures that a symbol created by a non-ELF symbol
7521 reader will have the flag set correctly. */
7528 /* Copy data from an indirect symbol to its direct symbol, hiding the
7529 old indirect symbol. Also used for copying flags to a weakdef. */
7532 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
7533 struct elf_link_hash_entry *dir,
7534 struct elf_link_hash_entry *ind)
7536 struct elf_link_hash_table *htab;
7538 /* Copy down any references that we may have already seen to the
7539 symbol which just became indirect. */
7541 if (dir->versioned != versioned_hidden)
7542 dir->ref_dynamic |= ind->ref_dynamic;
7543 dir->ref_regular |= ind->ref_regular;
7544 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
7545 dir->non_got_ref |= ind->non_got_ref;
7546 dir->needs_plt |= ind->needs_plt;
7547 dir->pointer_equality_needed |= ind->pointer_equality_needed;
7549 if (ind->root.type != bfd_link_hash_indirect)
7552 /* Copy over the global and procedure linkage table refcount entries.
7553 These may have been already set up by a check_relocs routine. */
7554 htab = elf_hash_table (info);
7555 if (ind->got.refcount > htab->init_got_refcount.refcount)
7557 if (dir->got.refcount < 0)
7558 dir->got.refcount = 0;
7559 dir->got.refcount += ind->got.refcount;
7560 ind->got.refcount = htab->init_got_refcount.refcount;
7563 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
7565 if (dir->plt.refcount < 0)
7566 dir->plt.refcount = 0;
7567 dir->plt.refcount += ind->plt.refcount;
7568 ind->plt.refcount = htab->init_plt_refcount.refcount;
7571 if (ind->dynindx != -1)
7573 if (dir->dynindx != -1)
7574 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
7575 dir->dynindx = ind->dynindx;
7576 dir->dynstr_index = ind->dynstr_index;
7578 ind->dynstr_index = 0;
7583 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
7584 struct elf_link_hash_entry *h,
7585 bfd_boolean force_local)
7587 /* STT_GNU_IFUNC symbol must go through PLT. */
7588 if (h->type != STT_GNU_IFUNC)
7590 h->plt = elf_hash_table (info)->init_plt_offset;
7595 h->forced_local = 1;
7596 if (h->dynindx != -1)
7598 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7601 h->dynstr_index = 0;
7606 /* Hide a symbol. */
7609 _bfd_elf_link_hide_symbol (bfd *output_bfd,
7610 struct bfd_link_info *info,
7611 struct bfd_link_hash_entry *h)
7613 if (is_elf_hash_table (info->hash))
7615 const struct elf_backend_data *bed
7616 = get_elf_backend_data (output_bfd);
7617 struct elf_link_hash_entry *eh
7618 = (struct elf_link_hash_entry *) h;
7619 bed->elf_backend_hide_symbol (info, eh, TRUE);
7620 eh->def_dynamic = 0;
7621 eh->ref_dynamic = 0;
7622 eh->dynamic_def = 0;
7626 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
7630 _bfd_elf_link_hash_table_init
7631 (struct elf_link_hash_table *table,
7633 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
7634 struct bfd_hash_table *,
7636 unsigned int entsize,
7637 enum elf_target_id target_id)
7640 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
7642 table->init_got_refcount.refcount = can_refcount - 1;
7643 table->init_plt_refcount.refcount = can_refcount - 1;
7644 table->init_got_offset.offset = -(bfd_vma) 1;
7645 table->init_plt_offset.offset = -(bfd_vma) 1;
7646 /* The first dynamic symbol is a dummy. */
7647 table->dynsymcount = 1;
7649 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7651 table->root.type = bfd_link_elf_hash_table;
7652 table->hash_table_id = target_id;
7657 /* Create an ELF linker hash table. */
7659 struct bfd_link_hash_table *
7660 _bfd_elf_link_hash_table_create (bfd *abfd)
7662 struct elf_link_hash_table *ret;
7663 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7665 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7669 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7670 sizeof (struct elf_link_hash_entry),
7676 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7681 /* Destroy an ELF linker hash table. */
7684 _bfd_elf_link_hash_table_free (bfd *obfd)
7686 struct elf_link_hash_table *htab;
7688 htab = (struct elf_link_hash_table *) obfd->link.hash;
7689 if (htab->dynstr != NULL)
7690 _bfd_elf_strtab_free (htab->dynstr);
7691 _bfd_merge_sections_free (htab->merge_info);
7692 _bfd_generic_link_hash_table_free (obfd);
7695 /* This is a hook for the ELF emulation code in the generic linker to
7696 tell the backend linker what file name to use for the DT_NEEDED
7697 entry for a dynamic object. */
7700 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7702 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7703 && bfd_get_format (abfd) == bfd_object)
7704 elf_dt_name (abfd) = name;
7708 bfd_elf_get_dyn_lib_class (bfd *abfd)
7711 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7712 && bfd_get_format (abfd) == bfd_object)
7713 lib_class = elf_dyn_lib_class (abfd);
7720 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7722 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7723 && bfd_get_format (abfd) == bfd_object)
7724 elf_dyn_lib_class (abfd) = lib_class;
7727 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7728 the linker ELF emulation code. */
7730 struct bfd_link_needed_list *
7731 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7732 struct bfd_link_info *info)
7734 if (! is_elf_hash_table (info->hash))
7736 return elf_hash_table (info)->needed;
7739 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7740 hook for the linker ELF emulation code. */
7742 struct bfd_link_needed_list *
7743 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7744 struct bfd_link_info *info)
7746 if (! is_elf_hash_table (info->hash))
7748 return elf_hash_table (info)->runpath;
7751 /* Get the name actually used for a dynamic object for a link. This
7752 is the SONAME entry if there is one. Otherwise, it is the string
7753 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7756 bfd_elf_get_dt_soname (bfd *abfd)
7758 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7759 && bfd_get_format (abfd) == bfd_object)
7760 return elf_dt_name (abfd);
7764 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7765 the ELF linker emulation code. */
7768 bfd_elf_get_bfd_needed_list (bfd *abfd,
7769 struct bfd_link_needed_list **pneeded)
7772 bfd_byte *dynbuf = NULL;
7773 unsigned int elfsec;
7774 unsigned long shlink;
7775 bfd_byte *extdyn, *extdynend;
7777 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7781 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7782 || bfd_get_format (abfd) != bfd_object)
7785 s = bfd_get_section_by_name (abfd, ".dynamic");
7786 if (s == NULL || s->size == 0)
7789 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7792 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7793 if (elfsec == SHN_BAD)
7796 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7798 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7799 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7802 extdynend = extdyn + s->size;
7803 for (; extdyn < extdynend; extdyn += extdynsize)
7805 Elf_Internal_Dyn dyn;
7807 (*swap_dyn_in) (abfd, extdyn, &dyn);
7809 if (dyn.d_tag == DT_NULL)
7812 if (dyn.d_tag == DT_NEEDED)
7815 struct bfd_link_needed_list *l;
7816 unsigned int tagv = dyn.d_un.d_val;
7819 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7824 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7845 struct elf_symbuf_symbol
7847 unsigned long st_name; /* Symbol name, index in string tbl */
7848 unsigned char st_info; /* Type and binding attributes */
7849 unsigned char st_other; /* Visibilty, and target specific */
7852 struct elf_symbuf_head
7854 struct elf_symbuf_symbol *ssym;
7856 unsigned int st_shndx;
7863 Elf_Internal_Sym *isym;
7864 struct elf_symbuf_symbol *ssym;
7870 /* Sort references to symbols by ascending section number. */
7873 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7875 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7876 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7878 if (s1->st_shndx != s2->st_shndx)
7879 return s1->st_shndx > s2->st_shndx ? 1 : -1;
7880 /* Final sort by the address of the sym in the symbuf ensures
7883 return s1 > s2 ? 1 : -1;
7888 elf_sym_name_compare (const void *arg1, const void *arg2)
7890 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7891 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7892 int ret = strcmp (s1->name, s2->name);
7895 if (s1->u.p != s2->u.p)
7896 return s1->u.p > s2->u.p ? 1 : -1;
7900 static struct elf_symbuf_head *
7901 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
7903 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7904 struct elf_symbuf_symbol *ssym;
7905 struct elf_symbuf_head *ssymbuf, *ssymhead;
7906 size_t i, shndx_count, total_size;
7908 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7912 for (ind = indbuf, i = 0; i < symcount; i++)
7913 if (isymbuf[i].st_shndx != SHN_UNDEF)
7914 *ind++ = &isymbuf[i];
7917 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7918 elf_sort_elf_symbol);
7921 if (indbufend > indbuf)
7922 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7923 if (ind[0]->st_shndx != ind[1]->st_shndx)
7926 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7927 + (indbufend - indbuf) * sizeof (*ssym));
7928 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7929 if (ssymbuf == NULL)
7935 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7936 ssymbuf->ssym = NULL;
7937 ssymbuf->count = shndx_count;
7938 ssymbuf->st_shndx = 0;
7939 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7941 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7944 ssymhead->ssym = ssym;
7945 ssymhead->count = 0;
7946 ssymhead->st_shndx = (*ind)->st_shndx;
7948 ssym->st_name = (*ind)->st_name;
7949 ssym->st_info = (*ind)->st_info;
7950 ssym->st_other = (*ind)->st_other;
7953 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
7954 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7961 /* Check if 2 sections define the same set of local and global
7965 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7966 struct bfd_link_info *info)
7969 const struct elf_backend_data *bed1, *bed2;
7970 Elf_Internal_Shdr *hdr1, *hdr2;
7971 size_t symcount1, symcount2;
7972 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7973 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7974 Elf_Internal_Sym *isym, *isymend;
7975 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7976 size_t count1, count2, i;
7977 unsigned int shndx1, shndx2;
7983 /* Both sections have to be in ELF. */
7984 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7985 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7988 if (elf_section_type (sec1) != elf_section_type (sec2))
7991 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7992 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7993 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7996 bed1 = get_elf_backend_data (bfd1);
7997 bed2 = get_elf_backend_data (bfd2);
7998 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7999 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8000 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8001 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8003 if (symcount1 == 0 || symcount2 == 0)
8009 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
8010 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
8012 if (ssymbuf1 == NULL)
8014 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8016 if (isymbuf1 == NULL)
8019 if (!info->reduce_memory_overheads)
8021 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
8022 elf_tdata (bfd1)->symbuf = ssymbuf1;
8026 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
8028 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8030 if (isymbuf2 == NULL)
8033 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
8035 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
8036 elf_tdata (bfd2)->symbuf = ssymbuf2;
8040 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
8042 /* Optimized faster version. */
8044 struct elf_symbol *symp;
8045 struct elf_symbuf_symbol *ssym, *ssymend;
8048 hi = ssymbuf1->count;
8053 mid = (lo + hi) / 2;
8054 if (shndx1 < ssymbuf1[mid].st_shndx)
8056 else if (shndx1 > ssymbuf1[mid].st_shndx)
8060 count1 = ssymbuf1[mid].count;
8067 hi = ssymbuf2->count;
8072 mid = (lo + hi) / 2;
8073 if (shndx2 < ssymbuf2[mid].st_shndx)
8075 else if (shndx2 > ssymbuf2[mid].st_shndx)
8079 count2 = ssymbuf2[mid].count;
8085 if (count1 == 0 || count2 == 0 || count1 != count2)
8089 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
8091 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
8092 if (symtable1 == NULL || symtable2 == NULL)
8096 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
8097 ssym < ssymend; ssym++, symp++)
8099 symp->u.ssym = ssym;
8100 symp->name = bfd_elf_string_from_elf_section (bfd1,
8106 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
8107 ssym < ssymend; ssym++, symp++)
8109 symp->u.ssym = ssym;
8110 symp->name = bfd_elf_string_from_elf_section (bfd2,
8115 /* Sort symbol by name. */
8116 qsort (symtable1, count1, sizeof (struct elf_symbol),
8117 elf_sym_name_compare);
8118 qsort (symtable2, count1, sizeof (struct elf_symbol),
8119 elf_sym_name_compare);
8121 for (i = 0; i < count1; i++)
8122 /* Two symbols must have the same binding, type and name. */
8123 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
8124 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
8125 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8132 symtable1 = (struct elf_symbol *)
8133 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
8134 symtable2 = (struct elf_symbol *)
8135 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
8136 if (symtable1 == NULL || symtable2 == NULL)
8139 /* Count definitions in the section. */
8141 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
8142 if (isym->st_shndx == shndx1)
8143 symtable1[count1++].u.isym = isym;
8146 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
8147 if (isym->st_shndx == shndx2)
8148 symtable2[count2++].u.isym = isym;
8150 if (count1 == 0 || count2 == 0 || count1 != count2)
8153 for (i = 0; i < count1; i++)
8155 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
8156 symtable1[i].u.isym->st_name);
8158 for (i = 0; i < count2; i++)
8160 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
8161 symtable2[i].u.isym->st_name);
8163 /* Sort symbol by name. */
8164 qsort (symtable1, count1, sizeof (struct elf_symbol),
8165 elf_sym_name_compare);
8166 qsort (symtable2, count1, sizeof (struct elf_symbol),
8167 elf_sym_name_compare);
8169 for (i = 0; i < count1; i++)
8170 /* Two symbols must have the same binding, type and name. */
8171 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
8172 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
8173 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8191 /* Return TRUE if 2 section types are compatible. */
8194 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
8195 bfd *bbfd, const asection *bsec)
8199 || abfd->xvec->flavour != bfd_target_elf_flavour
8200 || bbfd->xvec->flavour != bfd_target_elf_flavour)
8203 return elf_section_type (asec) == elf_section_type (bsec);
8206 /* Final phase of ELF linker. */
8208 /* A structure we use to avoid passing large numbers of arguments. */
8210 struct elf_final_link_info
8212 /* General link information. */
8213 struct bfd_link_info *info;
8216 /* Symbol string table. */
8217 struct elf_strtab_hash *symstrtab;
8218 /* .hash section. */
8220 /* symbol version section (.gnu.version). */
8221 asection *symver_sec;
8222 /* Buffer large enough to hold contents of any section. */
8224 /* Buffer large enough to hold external relocs of any section. */
8225 void *external_relocs;
8226 /* Buffer large enough to hold internal relocs of any section. */
8227 Elf_Internal_Rela *internal_relocs;
8228 /* Buffer large enough to hold external local symbols of any input
8230 bfd_byte *external_syms;
8231 /* And a buffer for symbol section indices. */
8232 Elf_External_Sym_Shndx *locsym_shndx;
8233 /* Buffer large enough to hold internal local symbols of any input
8235 Elf_Internal_Sym *internal_syms;
8236 /* Array large enough to hold a symbol index for each local symbol
8237 of any input BFD. */
8239 /* Array large enough to hold a section pointer for each local
8240 symbol of any input BFD. */
8241 asection **sections;
8242 /* Buffer for SHT_SYMTAB_SHNDX section. */
8243 Elf_External_Sym_Shndx *symshndxbuf;
8244 /* Number of STT_FILE syms seen. */
8245 size_t filesym_count;
8248 /* This struct is used to pass information to elf_link_output_extsym. */
8250 struct elf_outext_info
8253 bfd_boolean localsyms;
8254 bfd_boolean file_sym_done;
8255 struct elf_final_link_info *flinfo;
8259 /* Support for evaluating a complex relocation.
8261 Complex relocations are generalized, self-describing relocations. The
8262 implementation of them consists of two parts: complex symbols, and the
8263 relocations themselves.
8265 The relocations are use a reserved elf-wide relocation type code (R_RELC
8266 external / BFD_RELOC_RELC internal) and an encoding of relocation field
8267 information (start bit, end bit, word width, etc) into the addend. This
8268 information is extracted from CGEN-generated operand tables within gas.
8270 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
8271 internal) representing prefix-notation expressions, including but not
8272 limited to those sorts of expressions normally encoded as addends in the
8273 addend field. The symbol mangling format is:
8276 | <unary-operator> ':' <node>
8277 | <binary-operator> ':' <node> ':' <node>
8280 <literal> := 's' <digits=N> ':' <N character symbol name>
8281 | 'S' <digits=N> ':' <N character section name>
8285 <binary-operator> := as in C
8286 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
8289 set_symbol_value (bfd *bfd_with_globals,
8290 Elf_Internal_Sym *isymbuf,
8295 struct elf_link_hash_entry **sym_hashes;
8296 struct elf_link_hash_entry *h;
8297 size_t extsymoff = locsymcount;
8299 if (symidx < locsymcount)
8301 Elf_Internal_Sym *sym;
8303 sym = isymbuf + symidx;
8304 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
8306 /* It is a local symbol: move it to the
8307 "absolute" section and give it a value. */
8308 sym->st_shndx = SHN_ABS;
8309 sym->st_value = val;
8312 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
8316 /* It is a global symbol: set its link type
8317 to "defined" and give it a value. */
8319 sym_hashes = elf_sym_hashes (bfd_with_globals);
8320 h = sym_hashes [symidx - extsymoff];
8321 while (h->root.type == bfd_link_hash_indirect
8322 || h->root.type == bfd_link_hash_warning)
8323 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8324 h->root.type = bfd_link_hash_defined;
8325 h->root.u.def.value = val;
8326 h->root.u.def.section = bfd_abs_section_ptr;
8330 resolve_symbol (const char *name,
8332 struct elf_final_link_info *flinfo,
8334 Elf_Internal_Sym *isymbuf,
8337 Elf_Internal_Sym *sym;
8338 struct bfd_link_hash_entry *global_entry;
8339 const char *candidate = NULL;
8340 Elf_Internal_Shdr *symtab_hdr;
8343 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
8345 for (i = 0; i < locsymcount; ++ i)
8349 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
8352 candidate = bfd_elf_string_from_elf_section (input_bfd,
8353 symtab_hdr->sh_link,
8356 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
8357 name, candidate, (unsigned long) sym->st_value);
8359 if (candidate && strcmp (candidate, name) == 0)
8361 asection *sec = flinfo->sections [i];
8363 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
8364 *result += sec->output_offset + sec->output_section->vma;
8366 printf ("Found symbol with value %8.8lx\n",
8367 (unsigned long) *result);
8373 /* Hmm, haven't found it yet. perhaps it is a global. */
8374 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
8375 FALSE, FALSE, TRUE);
8379 if (global_entry->type == bfd_link_hash_defined
8380 || global_entry->type == bfd_link_hash_defweak)
8382 *result = (global_entry->u.def.value
8383 + global_entry->u.def.section->output_section->vma
8384 + global_entry->u.def.section->output_offset);
8386 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
8387 global_entry->root.string, (unsigned long) *result);
8395 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
8396 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
8397 names like "foo.end" which is the end address of section "foo". */
8400 resolve_section (const char *name,
8408 for (curr = sections; curr; curr = curr->next)
8409 if (strcmp (curr->name, name) == 0)
8411 *result = curr->vma;
8415 /* Hmm. still haven't found it. try pseudo-section names. */
8416 /* FIXME: This could be coded more efficiently... */
8417 for (curr = sections; curr; curr = curr->next)
8419 len = strlen (curr->name);
8420 if (len > strlen (name))
8423 if (strncmp (curr->name, name, len) == 0)
8425 if (strncmp (".end", name + len, 4) == 0)
8427 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd);
8431 /* Insert more pseudo-section names here, if you like. */
8439 undefined_reference (const char *reftype, const char *name)
8441 /* xgettext:c-format */
8442 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
8447 eval_symbol (bfd_vma *result,
8450 struct elf_final_link_info *flinfo,
8452 Elf_Internal_Sym *isymbuf,
8461 const char *sym = *symp;
8463 bfd_boolean symbol_is_section = FALSE;
8468 if (len < 1 || len > sizeof (symbuf))
8470 bfd_set_error (bfd_error_invalid_operation);
8483 *result = strtoul (sym, (char **) symp, 16);
8487 symbol_is_section = TRUE;
8491 symlen = strtol (sym, (char **) symp, 10);
8492 sym = *symp + 1; /* Skip the trailing ':'. */
8494 if (symend < sym || symlen + 1 > sizeof (symbuf))
8496 bfd_set_error (bfd_error_invalid_operation);
8500 memcpy (symbuf, sym, symlen);
8501 symbuf[symlen] = '\0';
8502 *symp = sym + symlen;
8504 /* Is it always possible, with complex symbols, that gas "mis-guessed"
8505 the symbol as a section, or vice-versa. so we're pretty liberal in our
8506 interpretation here; section means "try section first", not "must be a
8507 section", and likewise with symbol. */
8509 if (symbol_is_section)
8511 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
8512 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
8513 isymbuf, locsymcount))
8515 undefined_reference ("section", symbuf);
8521 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
8522 isymbuf, locsymcount)
8523 && !resolve_section (symbuf, flinfo->output_bfd->sections,
8526 undefined_reference ("symbol", symbuf);
8533 /* All that remains are operators. */
8535 #define UNARY_OP(op) \
8536 if (strncmp (sym, #op, strlen (#op)) == 0) \
8538 sym += strlen (#op); \
8542 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8543 isymbuf, locsymcount, signed_p)) \
8546 *result = op ((bfd_signed_vma) a); \
8552 #define BINARY_OP(op) \
8553 if (strncmp (sym, #op, strlen (#op)) == 0) \
8555 sym += strlen (#op); \
8559 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
8560 isymbuf, locsymcount, signed_p)) \
8563 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
8564 isymbuf, locsymcount, signed_p)) \
8567 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
8597 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
8598 bfd_set_error (bfd_error_invalid_operation);
8604 put_value (bfd_vma size,
8605 unsigned long chunksz,
8610 location += (size - chunksz);
8612 for (; size; size -= chunksz, location -= chunksz)
8617 bfd_put_8 (input_bfd, x, location);
8621 bfd_put_16 (input_bfd, x, location);
8625 bfd_put_32 (input_bfd, x, location);
8626 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
8632 bfd_put_64 (input_bfd, x, location);
8633 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
8646 get_value (bfd_vma size,
8647 unsigned long chunksz,
8654 /* Sanity checks. */
8655 BFD_ASSERT (chunksz <= sizeof (x)
8658 && (size % chunksz) == 0
8659 && input_bfd != NULL
8660 && location != NULL);
8662 if (chunksz == sizeof (x))
8664 BFD_ASSERT (size == chunksz);
8666 /* Make sure that we do not perform an undefined shift operation.
8667 We know that size == chunksz so there will only be one iteration
8668 of the loop below. */
8672 shift = 8 * chunksz;
8674 for (; size; size -= chunksz, location += chunksz)
8679 x = (x << shift) | bfd_get_8 (input_bfd, location);
8682 x = (x << shift) | bfd_get_16 (input_bfd, location);
8685 x = (x << shift) | bfd_get_32 (input_bfd, location);
8689 x = (x << shift) | bfd_get_64 (input_bfd, location);
8700 decode_complex_addend (unsigned long *start, /* in bits */
8701 unsigned long *oplen, /* in bits */
8702 unsigned long *len, /* in bits */
8703 unsigned long *wordsz, /* in bytes */
8704 unsigned long *chunksz, /* in bytes */
8705 unsigned long *lsb0_p,
8706 unsigned long *signed_p,
8707 unsigned long *trunc_p,
8708 unsigned long encoded)
8710 * start = encoded & 0x3F;
8711 * len = (encoded >> 6) & 0x3F;
8712 * oplen = (encoded >> 12) & 0x3F;
8713 * wordsz = (encoded >> 18) & 0xF;
8714 * chunksz = (encoded >> 22) & 0xF;
8715 * lsb0_p = (encoded >> 27) & 1;
8716 * signed_p = (encoded >> 28) & 1;
8717 * trunc_p = (encoded >> 29) & 1;
8720 bfd_reloc_status_type
8721 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8722 asection *input_section ATTRIBUTE_UNUSED,
8724 Elf_Internal_Rela *rel,
8727 bfd_vma shift, x, mask;
8728 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8729 bfd_reloc_status_type r;
8731 /* Perform this reloc, since it is complex.
8732 (this is not to say that it necessarily refers to a complex
8733 symbol; merely that it is a self-describing CGEN based reloc.
8734 i.e. the addend has the complete reloc information (bit start, end,
8735 word size, etc) encoded within it.). */
8737 decode_complex_addend (&start, &oplen, &len, &wordsz,
8738 &chunksz, &lsb0_p, &signed_p,
8739 &trunc_p, rel->r_addend);
8741 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8744 shift = (start + 1) - len;
8746 shift = (8 * wordsz) - (start + len);
8748 x = get_value (wordsz, chunksz, input_bfd,
8749 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8752 printf ("Doing complex reloc: "
8753 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8754 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8755 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8756 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8757 oplen, (unsigned long) x, (unsigned long) mask,
8758 (unsigned long) relocation);
8763 /* Now do an overflow check. */
8764 r = bfd_check_overflow ((signed_p
8765 ? complain_overflow_signed
8766 : complain_overflow_unsigned),
8767 len, 0, (8 * wordsz),
8771 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8774 printf (" relocation: %8.8lx\n"
8775 " shifted mask: %8.8lx\n"
8776 " shifted/masked reloc: %8.8lx\n"
8777 " result: %8.8lx\n",
8778 (unsigned long) relocation, (unsigned long) (mask << shift),
8779 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8781 put_value (wordsz, chunksz, input_bfd, x,
8782 contents + rel->r_offset * bfd_octets_per_byte (input_bfd));
8786 /* Functions to read r_offset from external (target order) reloc
8787 entry. Faster than bfd_getl32 et al, because we let the compiler
8788 know the value is aligned. */
8791 ext32l_r_offset (const void *p)
8798 const union aligned32 *a
8799 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8801 uint32_t aval = ( (uint32_t) a->c[0]
8802 | (uint32_t) a->c[1] << 8
8803 | (uint32_t) a->c[2] << 16
8804 | (uint32_t) a->c[3] << 24);
8809 ext32b_r_offset (const void *p)
8816 const union aligned32 *a
8817 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8819 uint32_t aval = ( (uint32_t) a->c[0] << 24
8820 | (uint32_t) a->c[1] << 16
8821 | (uint32_t) a->c[2] << 8
8822 | (uint32_t) a->c[3]);
8826 #ifdef BFD_HOST_64_BIT
8828 ext64l_r_offset (const void *p)
8835 const union aligned64 *a
8836 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8838 uint64_t aval = ( (uint64_t) a->c[0]
8839 | (uint64_t) a->c[1] << 8
8840 | (uint64_t) a->c[2] << 16
8841 | (uint64_t) a->c[3] << 24
8842 | (uint64_t) a->c[4] << 32
8843 | (uint64_t) a->c[5] << 40
8844 | (uint64_t) a->c[6] << 48
8845 | (uint64_t) a->c[7] << 56);
8850 ext64b_r_offset (const void *p)
8857 const union aligned64 *a
8858 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8860 uint64_t aval = ( (uint64_t) a->c[0] << 56
8861 | (uint64_t) a->c[1] << 48
8862 | (uint64_t) a->c[2] << 40
8863 | (uint64_t) a->c[3] << 32
8864 | (uint64_t) a->c[4] << 24
8865 | (uint64_t) a->c[5] << 16
8866 | (uint64_t) a->c[6] << 8
8867 | (uint64_t) a->c[7]);
8872 /* When performing a relocatable link, the input relocations are
8873 preserved. But, if they reference global symbols, the indices
8874 referenced must be updated. Update all the relocations found in
8878 elf_link_adjust_relocs (bfd *abfd,
8880 struct bfd_elf_section_reloc_data *reldata,
8882 struct bfd_link_info *info)
8885 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8887 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8888 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8889 bfd_vma r_type_mask;
8891 unsigned int count = reldata->count;
8892 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8894 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8896 swap_in = bed->s->swap_reloc_in;
8897 swap_out = bed->s->swap_reloc_out;
8899 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8901 swap_in = bed->s->swap_reloca_in;
8902 swap_out = bed->s->swap_reloca_out;
8907 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8910 if (bed->s->arch_size == 32)
8917 r_type_mask = 0xffffffff;
8921 erela = reldata->hdr->contents;
8922 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8924 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8927 if (*rel_hash == NULL)
8930 if ((*rel_hash)->indx == -2
8931 && info->gc_sections
8932 && ! info->gc_keep_exported)
8934 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
8935 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
8937 (*rel_hash)->root.root.string);
8938 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
8940 bfd_set_error (bfd_error_invalid_operation);
8943 BFD_ASSERT ((*rel_hash)->indx >= 0);
8945 (*swap_in) (abfd, erela, irela);
8946 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8947 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8948 | (irela[j].r_info & r_type_mask));
8949 (*swap_out) (abfd, irela, erela);
8952 if (bed->elf_backend_update_relocs)
8953 (*bed->elf_backend_update_relocs) (sec, reldata);
8955 if (sort && count != 0)
8957 bfd_vma (*ext_r_off) (const void *);
8960 bfd_byte *base, *end, *p, *loc;
8961 bfd_byte *buf = NULL;
8963 if (bed->s->arch_size == 32)
8965 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8966 ext_r_off = ext32l_r_offset;
8967 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8968 ext_r_off = ext32b_r_offset;
8974 #ifdef BFD_HOST_64_BIT
8975 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8976 ext_r_off = ext64l_r_offset;
8977 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8978 ext_r_off = ext64b_r_offset;
8984 /* Must use a stable sort here. A modified insertion sort,
8985 since the relocs are mostly sorted already. */
8986 elt_size = reldata->hdr->sh_entsize;
8987 base = reldata->hdr->contents;
8988 end = base + count * elt_size;
8989 if (elt_size > sizeof (Elf64_External_Rela))
8992 /* Ensure the first element is lowest. This acts as a sentinel,
8993 speeding the main loop below. */
8994 r_off = (*ext_r_off) (base);
8995 for (p = loc = base; (p += elt_size) < end; )
8997 bfd_vma r_off2 = (*ext_r_off) (p);
9006 /* Don't just swap *base and *loc as that changes the order
9007 of the original base[0] and base[1] if they happen to
9008 have the same r_offset. */
9009 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
9010 memcpy (onebuf, loc, elt_size);
9011 memmove (base + elt_size, base, loc - base);
9012 memcpy (base, onebuf, elt_size);
9015 for (p = base + elt_size; (p += elt_size) < end; )
9017 /* base to p is sorted, *p is next to insert. */
9018 r_off = (*ext_r_off) (p);
9019 /* Search the sorted region for location to insert. */
9021 while (r_off < (*ext_r_off) (loc))
9026 /* Chances are there is a run of relocs to insert here,
9027 from one of more input files. Files are not always
9028 linked in order due to the way elf_link_input_bfd is
9029 called. See pr17666. */
9030 size_t sortlen = p - loc;
9031 bfd_vma r_off2 = (*ext_r_off) (loc);
9032 size_t runlen = elt_size;
9033 size_t buf_size = 96 * 1024;
9034 while (p + runlen < end
9035 && (sortlen <= buf_size
9036 || runlen + elt_size <= buf_size)
9037 && r_off2 > (*ext_r_off) (p + runlen))
9041 buf = bfd_malloc (buf_size);
9045 if (runlen < sortlen)
9047 memcpy (buf, p, runlen);
9048 memmove (loc + runlen, loc, sortlen);
9049 memcpy (loc, buf, runlen);
9053 memcpy (buf, loc, sortlen);
9054 memmove (loc, p, runlen);
9055 memcpy (loc + runlen, buf, sortlen);
9057 p += runlen - elt_size;
9060 /* Hashes are no longer valid. */
9061 free (reldata->hashes);
9062 reldata->hashes = NULL;
9068 struct elf_link_sort_rela
9074 enum elf_reloc_type_class type;
9075 /* We use this as an array of size int_rels_per_ext_rel. */
9076 Elf_Internal_Rela rela[1];
9079 /* qsort stability here and for cmp2 is only an issue if multiple
9080 dynamic relocations are emitted at the same address. But targets
9081 that apply a series of dynamic relocations each operating on the
9082 result of the prior relocation can't use -z combreloc as
9083 implemented anyway. Such schemes tend to be broken by sorting on
9084 symbol index. That leaves dynamic NONE relocs as the only other
9085 case where ld might emit multiple relocs at the same address, and
9086 those are only emitted due to target bugs. */
9089 elf_link_sort_cmp1 (const void *A, const void *B)
9091 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9092 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9093 int relativea, relativeb;
9095 relativea = a->type == reloc_class_relative;
9096 relativeb = b->type == reloc_class_relative;
9098 if (relativea < relativeb)
9100 if (relativea > relativeb)
9102 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
9104 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
9106 if (a->rela->r_offset < b->rela->r_offset)
9108 if (a->rela->r_offset > b->rela->r_offset)
9114 elf_link_sort_cmp2 (const void *A, const void *B)
9116 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
9117 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
9119 if (a->type < b->type)
9121 if (a->type > b->type)
9123 if (a->u.offset < b->u.offset)
9125 if (a->u.offset > b->u.offset)
9127 if (a->rela->r_offset < b->rela->r_offset)
9129 if (a->rela->r_offset > b->rela->r_offset)
9135 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
9137 asection *dynamic_relocs;
9140 bfd_size_type count, size;
9141 size_t i, ret, sort_elt, ext_size;
9142 bfd_byte *sort, *s_non_relative, *p;
9143 struct elf_link_sort_rela *sq;
9144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9145 int i2e = bed->s->int_rels_per_ext_rel;
9146 unsigned int opb = bfd_octets_per_byte (abfd);
9147 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
9148 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
9149 struct bfd_link_order *lo;
9151 bfd_boolean use_rela;
9153 /* Find a dynamic reloc section. */
9154 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
9155 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
9156 if (rela_dyn != NULL && rela_dyn->size > 0
9157 && rel_dyn != NULL && rel_dyn->size > 0)
9159 bfd_boolean use_rela_initialised = FALSE;
9161 /* This is just here to stop gcc from complaining.
9162 Its initialization checking code is not perfect. */
9165 /* Both sections are present. Examine the sizes
9166 of the indirect sections to help us choose. */
9167 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9168 if (lo->type == bfd_indirect_link_order)
9170 asection *o = lo->u.indirect.section;
9172 if ((o->size % bed->s->sizeof_rela) == 0)
9174 if ((o->size % bed->s->sizeof_rel) == 0)
9175 /* Section size is divisible by both rel and rela sizes.
9176 It is of no help to us. */
9180 /* Section size is only divisible by rela. */
9181 if (use_rela_initialised && !use_rela)
9183 _bfd_error_handler (_("%pB: unable to sort relocs - "
9184 "they are in more than one size"),
9186 bfd_set_error (bfd_error_invalid_operation);
9192 use_rela_initialised = TRUE;
9196 else if ((o->size % bed->s->sizeof_rel) == 0)
9198 /* Section size is only divisible by rel. */
9199 if (use_rela_initialised && use_rela)
9201 _bfd_error_handler (_("%pB: unable to sort relocs - "
9202 "they are in more than one size"),
9204 bfd_set_error (bfd_error_invalid_operation);
9210 use_rela_initialised = TRUE;
9215 /* The section size is not divisible by either -
9216 something is wrong. */
9217 _bfd_error_handler (_("%pB: unable to sort relocs - "
9218 "they are of an unknown size"), abfd);
9219 bfd_set_error (bfd_error_invalid_operation);
9224 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
9225 if (lo->type == bfd_indirect_link_order)
9227 asection *o = lo->u.indirect.section;
9229 if ((o->size % bed->s->sizeof_rela) == 0)
9231 if ((o->size % bed->s->sizeof_rel) == 0)
9232 /* Section size is divisible by both rel and rela sizes.
9233 It is of no help to us. */
9237 /* Section size is only divisible by rela. */
9238 if (use_rela_initialised && !use_rela)
9240 _bfd_error_handler (_("%pB: unable to sort relocs - "
9241 "they are in more than one size"),
9243 bfd_set_error (bfd_error_invalid_operation);
9249 use_rela_initialised = TRUE;
9253 else if ((o->size % bed->s->sizeof_rel) == 0)
9255 /* Section size is only divisible by rel. */
9256 if (use_rela_initialised && use_rela)
9258 _bfd_error_handler (_("%pB: unable to sort relocs - "
9259 "they are in more than one size"),
9261 bfd_set_error (bfd_error_invalid_operation);
9267 use_rela_initialised = TRUE;
9272 /* The section size is not divisible by either -
9273 something is wrong. */
9274 _bfd_error_handler (_("%pB: unable to sort relocs - "
9275 "they are of an unknown size"), abfd);
9276 bfd_set_error (bfd_error_invalid_operation);
9281 if (! use_rela_initialised)
9285 else if (rela_dyn != NULL && rela_dyn->size > 0)
9287 else if (rel_dyn != NULL && rel_dyn->size > 0)
9294 dynamic_relocs = rela_dyn;
9295 ext_size = bed->s->sizeof_rela;
9296 swap_in = bed->s->swap_reloca_in;
9297 swap_out = bed->s->swap_reloca_out;
9301 dynamic_relocs = rel_dyn;
9302 ext_size = bed->s->sizeof_rel;
9303 swap_in = bed->s->swap_reloc_in;
9304 swap_out = bed->s->swap_reloc_out;
9308 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9309 if (lo->type == bfd_indirect_link_order)
9310 size += lo->u.indirect.section->size;
9312 if (size != dynamic_relocs->size)
9315 sort_elt = (sizeof (struct elf_link_sort_rela)
9316 + (i2e - 1) * sizeof (Elf_Internal_Rela));
9318 count = dynamic_relocs->size / ext_size;
9321 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
9325 (*info->callbacks->warning)
9326 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
9330 if (bed->s->arch_size == 32)
9331 r_sym_mask = ~(bfd_vma) 0xff;
9333 r_sym_mask = ~(bfd_vma) 0xffffffff;
9335 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9336 if (lo->type == bfd_indirect_link_order)
9338 bfd_byte *erel, *erelend;
9339 asection *o = lo->u.indirect.section;
9341 if (o->contents == NULL && o->size != 0)
9343 /* This is a reloc section that is being handled as a normal
9344 section. See bfd_section_from_shdr. We can't combine
9345 relocs in this case. */
9350 erelend = o->contents + o->size;
9351 p = sort + o->output_offset * opb / ext_size * sort_elt;
9353 while (erel < erelend)
9355 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9357 (*swap_in) (abfd, erel, s->rela);
9358 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
9359 s->u.sym_mask = r_sym_mask;
9365 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
9367 for (i = 0, p = sort; i < count; i++, p += sort_elt)
9369 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9370 if (s->type != reloc_class_relative)
9376 sq = (struct elf_link_sort_rela *) s_non_relative;
9377 for (; i < count; i++, p += sort_elt)
9379 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
9380 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
9382 sp->u.offset = sq->rela->r_offset;
9385 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
9387 struct elf_link_hash_table *htab = elf_hash_table (info);
9388 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
9390 /* We have plt relocs in .rela.dyn. */
9391 sq = (struct elf_link_sort_rela *) sort;
9392 for (i = 0; i < count; i++)
9393 if (sq[count - i - 1].type != reloc_class_plt)
9395 if (i != 0 && htab->srelplt->size == i * ext_size)
9397 struct bfd_link_order **plo;
9398 /* Put srelplt link_order last. This is so the output_offset
9399 set in the next loop is correct for DT_JMPREL. */
9400 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
9401 if ((*plo)->type == bfd_indirect_link_order
9402 && (*plo)->u.indirect.section == htab->srelplt)
9408 plo = &(*plo)->next;
9411 dynamic_relocs->map_tail.link_order = lo;
9416 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
9417 if (lo->type == bfd_indirect_link_order)
9419 bfd_byte *erel, *erelend;
9420 asection *o = lo->u.indirect.section;
9423 erelend = o->contents + o->size;
9424 o->output_offset = (p - sort) / sort_elt * ext_size / opb;
9425 while (erel < erelend)
9427 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
9428 (*swap_out) (abfd, s->rela, erel);
9435 *psec = dynamic_relocs;
9439 /* Add a symbol to the output symbol string table. */
9442 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
9444 Elf_Internal_Sym *elfsym,
9445 asection *input_sec,
9446 struct elf_link_hash_entry *h)
9448 int (*output_symbol_hook)
9449 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
9450 struct elf_link_hash_entry *);
9451 struct elf_link_hash_table *hash_table;
9452 const struct elf_backend_data *bed;
9453 bfd_size_type strtabsize;
9455 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9457 bed = get_elf_backend_data (flinfo->output_bfd);
9458 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
9459 if (output_symbol_hook != NULL)
9461 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
9466 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
9467 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
9468 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
9469 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
9473 || (input_sec->flags & SEC_EXCLUDE))
9474 elfsym->st_name = (unsigned long) -1;
9477 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
9478 to get the final offset for st_name. */
9480 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
9482 if (elfsym->st_name == (unsigned long) -1)
9486 hash_table = elf_hash_table (flinfo->info);
9487 strtabsize = hash_table->strtabsize;
9488 if (strtabsize <= hash_table->strtabcount)
9490 strtabsize += strtabsize;
9491 hash_table->strtabsize = strtabsize;
9492 strtabsize *= sizeof (*hash_table->strtab);
9494 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
9496 if (hash_table->strtab == NULL)
9499 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
9500 hash_table->strtab[hash_table->strtabcount].dest_index
9501 = hash_table->strtabcount;
9502 hash_table->strtab[hash_table->strtabcount].destshndx_index
9503 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
9505 flinfo->output_bfd->symcount += 1;
9506 hash_table->strtabcount += 1;
9511 /* Swap symbols out to the symbol table and flush the output symbols to
9515 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
9517 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
9520 const struct elf_backend_data *bed;
9522 Elf_Internal_Shdr *hdr;
9526 if (!hash_table->strtabcount)
9529 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
9531 bed = get_elf_backend_data (flinfo->output_bfd);
9533 amt = bed->s->sizeof_sym * hash_table->strtabcount;
9534 symbuf = (bfd_byte *) bfd_malloc (amt);
9538 if (flinfo->symshndxbuf)
9540 amt = sizeof (Elf_External_Sym_Shndx);
9541 amt *= bfd_get_symcount (flinfo->output_bfd);
9542 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
9543 if (flinfo->symshndxbuf == NULL)
9550 for (i = 0; i < hash_table->strtabcount; i++)
9552 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
9553 if (elfsym->sym.st_name == (unsigned long) -1)
9554 elfsym->sym.st_name = 0;
9557 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
9558 elfsym->sym.st_name);
9559 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
9560 ((bfd_byte *) symbuf
9561 + (elfsym->dest_index
9562 * bed->s->sizeof_sym)),
9563 (flinfo->symshndxbuf
9564 + elfsym->destshndx_index));
9567 /* Allow the linker to examine the strtab and symtab now they are
9570 if (flinfo->info->callbacks->examine_strtab)
9571 flinfo->info->callbacks->examine_strtab (hash_table->strtab,
9572 hash_table->strtabcount,
9575 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
9576 pos = hdr->sh_offset + hdr->sh_size;
9577 amt = hash_table->strtabcount * bed->s->sizeof_sym;
9578 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
9579 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
9581 hdr->sh_size += amt;
9589 free (hash_table->strtab);
9590 hash_table->strtab = NULL;
9595 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
9598 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
9600 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
9601 && sym->st_shndx < SHN_LORESERVE)
9603 /* The gABI doesn't support dynamic symbols in output sections
9606 /* xgettext:c-format */
9607 (_("%pB: too many sections: %d (>= %d)"),
9608 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
9609 bfd_set_error (bfd_error_nonrepresentable_section);
9615 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
9616 allowing an unsatisfied unversioned symbol in the DSO to match a
9617 versioned symbol that would normally require an explicit version.
9618 We also handle the case that a DSO references a hidden symbol
9619 which may be satisfied by a versioned symbol in another DSO. */
9622 elf_link_check_versioned_symbol (struct bfd_link_info *info,
9623 const struct elf_backend_data *bed,
9624 struct elf_link_hash_entry *h)
9627 struct elf_link_loaded_list *loaded;
9629 if (!is_elf_hash_table (info->hash))
9632 /* Check indirect symbol. */
9633 while (h->root.type == bfd_link_hash_indirect)
9634 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9636 switch (h->root.type)
9642 case bfd_link_hash_undefined:
9643 case bfd_link_hash_undefweak:
9644 abfd = h->root.u.undef.abfd;
9646 || (abfd->flags & DYNAMIC) == 0
9647 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
9651 case bfd_link_hash_defined:
9652 case bfd_link_hash_defweak:
9653 abfd = h->root.u.def.section->owner;
9656 case bfd_link_hash_common:
9657 abfd = h->root.u.c.p->section->owner;
9660 BFD_ASSERT (abfd != NULL);
9662 for (loaded = elf_hash_table (info)->loaded;
9664 loaded = loaded->next)
9667 Elf_Internal_Shdr *hdr;
9671 Elf_Internal_Shdr *versymhdr;
9672 Elf_Internal_Sym *isym;
9673 Elf_Internal_Sym *isymend;
9674 Elf_Internal_Sym *isymbuf;
9675 Elf_External_Versym *ever;
9676 Elf_External_Versym *extversym;
9678 input = loaded->abfd;
9680 /* We check each DSO for a possible hidden versioned definition. */
9682 || (input->flags & DYNAMIC) == 0
9683 || elf_dynversym (input) == 0)
9686 hdr = &elf_tdata (input)->dynsymtab_hdr;
9688 symcount = hdr->sh_size / bed->s->sizeof_sym;
9689 if (elf_bad_symtab (input))
9691 extsymcount = symcount;
9696 extsymcount = symcount - hdr->sh_info;
9697 extsymoff = hdr->sh_info;
9700 if (extsymcount == 0)
9703 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
9705 if (isymbuf == NULL)
9708 /* Read in any version definitions. */
9709 versymhdr = &elf_tdata (input)->dynversym_hdr;
9710 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
9711 if (extversym == NULL)
9714 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
9715 || (bfd_bread (extversym, versymhdr->sh_size, input)
9716 != versymhdr->sh_size))
9724 ever = extversym + extsymoff;
9725 isymend = isymbuf + extsymcount;
9726 for (isym = isymbuf; isym < isymend; isym++, ever++)
9729 Elf_Internal_Versym iver;
9730 unsigned short version_index;
9732 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
9733 || isym->st_shndx == SHN_UNDEF)
9736 name = bfd_elf_string_from_elf_section (input,
9739 if (strcmp (name, h->root.root.string) != 0)
9742 _bfd_elf_swap_versym_in (input, ever, &iver);
9744 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9746 && h->forced_local))
9748 /* If we have a non-hidden versioned sym, then it should
9749 have provided a definition for the undefined sym unless
9750 it is defined in a non-shared object and forced local.
9755 version_index = iver.vs_vers & VERSYM_VERSION;
9756 if (version_index == 1 || version_index == 2)
9758 /* This is the base or first version. We can use it. */
9772 /* Convert ELF common symbol TYPE. */
9775 elf_link_convert_common_type (struct bfd_link_info *info, int type)
9777 /* Commom symbol can only appear in relocatable link. */
9778 if (!bfd_link_relocatable (info))
9780 switch (info->elf_stt_common)
9784 case elf_stt_common:
9787 case no_elf_stt_common:
9794 /* Add an external symbol to the symbol table. This is called from
9795 the hash table traversal routine. When generating a shared object,
9796 we go through the symbol table twice. The first time we output
9797 anything that might have been forced to local scope in a version
9798 script. The second time we output the symbols that are still
9802 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9804 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9805 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9806 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9808 Elf_Internal_Sym sym;
9809 asection *input_sec;
9810 const struct elf_backend_data *bed;
9815 if (h->root.type == bfd_link_hash_warning)
9817 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9818 if (h->root.type == bfd_link_hash_new)
9822 /* Decide whether to output this symbol in this pass. */
9823 if (eoinfo->localsyms)
9825 if (!h->forced_local)
9830 if (h->forced_local)
9834 bed = get_elf_backend_data (flinfo->output_bfd);
9836 if (h->root.type == bfd_link_hash_undefined)
9838 /* If we have an undefined symbol reference here then it must have
9839 come from a shared library that is being linked in. (Undefined
9840 references in regular files have already been handled unless
9841 they are in unreferenced sections which are removed by garbage
9843 bfd_boolean ignore_undef = FALSE;
9845 /* Some symbols may be special in that the fact that they're
9846 undefined can be safely ignored - let backend determine that. */
9847 if (bed->elf_backend_ignore_undef_symbol)
9848 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9850 /* If we are reporting errors for this situation then do so now. */
9852 && h->ref_dynamic_nonweak
9853 && (!h->ref_regular || flinfo->info->gc_sections)
9854 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9855 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9856 (*flinfo->info->callbacks->undefined_symbol)
9857 (flinfo->info, h->root.root.string,
9858 h->ref_regular ? NULL : h->root.u.undef.abfd,
9860 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR);
9862 /* Strip a global symbol defined in a discarded section. */
9867 /* We should also warn if a forced local symbol is referenced from
9868 shared libraries. */
9869 if (bfd_link_executable (flinfo->info)
9874 && h->ref_dynamic_nonweak
9875 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9879 struct elf_link_hash_entry *hi = h;
9881 /* Check indirect symbol. */
9882 while (hi->root.type == bfd_link_hash_indirect)
9883 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9885 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9886 /* xgettext:c-format */
9887 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
9888 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9889 /* xgettext:c-format */
9890 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
9892 /* xgettext:c-format */
9893 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
9894 def_bfd = flinfo->output_bfd;
9895 if (hi->root.u.def.section != bfd_abs_section_ptr)
9896 def_bfd = hi->root.u.def.section->owner;
9897 _bfd_error_handler (msg, flinfo->output_bfd,
9898 h->root.root.string, def_bfd);
9899 bfd_set_error (bfd_error_bad_value);
9900 eoinfo->failed = TRUE;
9904 /* We don't want to output symbols that have never been mentioned by
9905 a regular file, or that we have been told to strip. However, if
9906 h->indx is set to -2, the symbol is used by a reloc and we must
9911 else if ((h->def_dynamic
9913 || h->root.type == bfd_link_hash_new)
9917 else if (flinfo->info->strip == strip_all)
9919 else if (flinfo->info->strip == strip_some
9920 && bfd_hash_lookup (flinfo->info->keep_hash,
9921 h->root.root.string, FALSE, FALSE) == NULL)
9923 else if ((h->root.type == bfd_link_hash_defined
9924 || h->root.type == bfd_link_hash_defweak)
9925 && ((flinfo->info->strip_discarded
9926 && discarded_section (h->root.u.def.section))
9927 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9928 && h->root.u.def.section->owner != NULL
9929 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9931 else if ((h->root.type == bfd_link_hash_undefined
9932 || h->root.type == bfd_link_hash_undefweak)
9933 && h->root.u.undef.abfd != NULL
9934 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9939 /* If we're stripping it, and it's not a dynamic symbol, there's
9940 nothing else to do. However, if it is a forced local symbol or
9941 an ifunc symbol we need to give the backend finish_dynamic_symbol
9942 function a chance to make it dynamic. */
9945 && type != STT_GNU_IFUNC
9946 && !h->forced_local)
9950 sym.st_size = h->size;
9951 sym.st_other = h->other;
9952 switch (h->root.type)
9955 case bfd_link_hash_new:
9956 case bfd_link_hash_warning:
9960 case bfd_link_hash_undefined:
9961 case bfd_link_hash_undefweak:
9962 input_sec = bfd_und_section_ptr;
9963 sym.st_shndx = SHN_UNDEF;
9966 case bfd_link_hash_defined:
9967 case bfd_link_hash_defweak:
9969 input_sec = h->root.u.def.section;
9970 if (input_sec->output_section != NULL)
9973 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9974 input_sec->output_section);
9975 if (sym.st_shndx == SHN_BAD)
9978 /* xgettext:c-format */
9979 (_("%pB: could not find output section %pA for input section %pA"),
9980 flinfo->output_bfd, input_sec->output_section, input_sec);
9981 bfd_set_error (bfd_error_nonrepresentable_section);
9982 eoinfo->failed = TRUE;
9986 /* ELF symbols in relocatable files are section relative,
9987 but in nonrelocatable files they are virtual
9989 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9990 if (!bfd_link_relocatable (flinfo->info))
9992 sym.st_value += input_sec->output_section->vma;
9993 if (h->type == STT_TLS)
9995 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9996 if (tls_sec != NULL)
9997 sym.st_value -= tls_sec->vma;
10003 BFD_ASSERT (input_sec->owner == NULL
10004 || (input_sec->owner->flags & DYNAMIC) != 0);
10005 sym.st_shndx = SHN_UNDEF;
10006 input_sec = bfd_und_section_ptr;
10011 case bfd_link_hash_common:
10012 input_sec = h->root.u.c.p->section;
10013 sym.st_shndx = bed->common_section_index (input_sec);
10014 sym.st_value = 1 << h->root.u.c.p->alignment_power;
10017 case bfd_link_hash_indirect:
10018 /* These symbols are created by symbol versioning. They point
10019 to the decorated version of the name. For example, if the
10020 symbol foo@@GNU_1.2 is the default, which should be used when
10021 foo is used with no version, then we add an indirect symbol
10022 foo which points to foo@@GNU_1.2. We ignore these symbols,
10023 since the indirected symbol is already in the hash table. */
10027 if (type == STT_COMMON || type == STT_OBJECT)
10028 switch (h->root.type)
10030 case bfd_link_hash_common:
10031 type = elf_link_convert_common_type (flinfo->info, type);
10033 case bfd_link_hash_defined:
10034 case bfd_link_hash_defweak:
10035 if (bed->common_definition (&sym))
10036 type = elf_link_convert_common_type (flinfo->info, type);
10040 case bfd_link_hash_undefined:
10041 case bfd_link_hash_undefweak:
10047 if (h->forced_local)
10049 sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
10050 /* Turn off visibility on local symbol. */
10051 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
10053 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
10054 else if (h->unique_global && h->def_regular)
10055 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
10056 else if (h->root.type == bfd_link_hash_undefweak
10057 || h->root.type == bfd_link_hash_defweak)
10058 sym.st_info = ELF_ST_INFO (STB_WEAK, type);
10060 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
10061 sym.st_target_internal = h->target_internal;
10063 /* Give the processor backend a chance to tweak the symbol value,
10064 and also to finish up anything that needs to be done for this
10065 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
10066 forced local syms when non-shared is due to a historical quirk.
10067 STT_GNU_IFUNC symbol must go through PLT. */
10068 if ((h->type == STT_GNU_IFUNC
10070 && !bfd_link_relocatable (flinfo->info))
10071 || ((h->dynindx != -1
10072 || h->forced_local)
10073 && ((bfd_link_pic (flinfo->info)
10074 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
10075 || h->root.type != bfd_link_hash_undefweak))
10076 || !h->forced_local)
10077 && elf_hash_table (flinfo->info)->dynamic_sections_created))
10079 if (! ((*bed->elf_backend_finish_dynamic_symbol)
10080 (flinfo->output_bfd, flinfo->info, h, &sym)))
10082 eoinfo->failed = TRUE;
10087 /* If we are marking the symbol as undefined, and there are no
10088 non-weak references to this symbol from a regular object, then
10089 mark the symbol as weak undefined; if there are non-weak
10090 references, mark the symbol as strong. We can't do this earlier,
10091 because it might not be marked as undefined until the
10092 finish_dynamic_symbol routine gets through with it. */
10093 if (sym.st_shndx == SHN_UNDEF
10095 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
10096 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
10099 type = ELF_ST_TYPE (sym.st_info);
10101 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
10102 if (type == STT_GNU_IFUNC)
10105 if (h->ref_regular_nonweak)
10106 bindtype = STB_GLOBAL;
10108 bindtype = STB_WEAK;
10109 sym.st_info = ELF_ST_INFO (bindtype, type);
10112 /* If this is a symbol defined in a dynamic library, don't use the
10113 symbol size from the dynamic library. Relinking an executable
10114 against a new library may introduce gratuitous changes in the
10115 executable's symbols if we keep the size. */
10116 if (sym.st_shndx == SHN_UNDEF
10121 /* If a non-weak symbol with non-default visibility is not defined
10122 locally, it is a fatal error. */
10123 if (!bfd_link_relocatable (flinfo->info)
10124 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
10125 && ELF_ST_BIND (sym.st_info) != STB_WEAK
10126 && h->root.type == bfd_link_hash_undefined
10127 && !h->def_regular)
10131 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
10132 /* xgettext:c-format */
10133 msg = _("%pB: protected symbol `%s' isn't defined");
10134 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
10135 /* xgettext:c-format */
10136 msg = _("%pB: internal symbol `%s' isn't defined");
10138 /* xgettext:c-format */
10139 msg = _("%pB: hidden symbol `%s' isn't defined");
10140 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
10141 bfd_set_error (bfd_error_bad_value);
10142 eoinfo->failed = TRUE;
10146 /* If this symbol should be put in the .dynsym section, then put it
10147 there now. We already know the symbol index. We also fill in
10148 the entry in the .hash section. */
10149 if (h->dynindx != -1
10150 && elf_hash_table (flinfo->info)->dynamic_sections_created
10151 && elf_hash_table (flinfo->info)->dynsym != NULL
10152 && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
10156 /* Since there is no version information in the dynamic string,
10157 if there is no version info in symbol version section, we will
10158 have a run-time problem if not linking executable, referenced
10159 by shared library, or not bound locally. */
10160 if (h->verinfo.verdef == NULL
10161 && (!bfd_link_executable (flinfo->info)
10163 || !h->def_regular))
10165 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
10167 if (p && p [1] != '\0')
10170 /* xgettext:c-format */
10171 (_("%pB: no symbol version section for versioned symbol `%s'"),
10172 flinfo->output_bfd, h->root.root.string);
10173 eoinfo->failed = TRUE;
10178 sym.st_name = h->dynstr_index;
10179 esym = (elf_hash_table (flinfo->info)->dynsym->contents
10180 + h->dynindx * bed->s->sizeof_sym);
10181 if (!check_dynsym (flinfo->output_bfd, &sym))
10183 eoinfo->failed = TRUE;
10186 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
10188 if (flinfo->hash_sec != NULL)
10190 size_t hash_entry_size;
10191 bfd_byte *bucketpos;
10193 size_t bucketcount;
10196 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
10197 bucket = h->u.elf_hash_value % bucketcount;
10200 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
10201 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
10202 + (bucket + 2) * hash_entry_size);
10203 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
10204 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
10206 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
10207 ((bfd_byte *) flinfo->hash_sec->contents
10208 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
10211 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
10213 Elf_Internal_Versym iversym;
10214 Elf_External_Versym *eversym;
10216 if (!h->def_regular)
10218 if (h->verinfo.verdef == NULL
10219 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
10220 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
10221 iversym.vs_vers = 0;
10223 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
10227 if (h->verinfo.vertree == NULL)
10228 iversym.vs_vers = 1;
10230 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
10231 if (flinfo->info->create_default_symver)
10235 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
10236 defined locally. */
10237 if (h->versioned == versioned_hidden && h->def_regular)
10238 iversym.vs_vers |= VERSYM_HIDDEN;
10240 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
10241 eversym += h->dynindx;
10242 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
10246 /* If the symbol is undefined, and we didn't output it to .dynsym,
10247 strip it from .symtab too. Obviously we can't do this for
10248 relocatable output or when needed for --emit-relocs. */
10249 else if (input_sec == bfd_und_section_ptr
10251 /* PR 22319 Do not strip global undefined symbols marked as being needed. */
10252 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
10253 && !bfd_link_relocatable (flinfo->info))
10256 /* Also strip others that we couldn't earlier due to dynamic symbol
10260 if ((input_sec->flags & SEC_EXCLUDE) != 0)
10263 /* Output a FILE symbol so that following locals are not associated
10264 with the wrong input file. We need one for forced local symbols
10265 if we've seen more than one FILE symbol or when we have exactly
10266 one FILE symbol but global symbols are present in a file other
10267 than the one with the FILE symbol. We also need one if linker
10268 defined symbols are present. In practice these conditions are
10269 always met, so just emit the FILE symbol unconditionally. */
10270 if (eoinfo->localsyms
10271 && !eoinfo->file_sym_done
10272 && eoinfo->flinfo->filesym_count != 0)
10274 Elf_Internal_Sym fsym;
10276 memset (&fsym, 0, sizeof (fsym));
10277 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10278 fsym.st_shndx = SHN_ABS;
10279 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
10280 bfd_und_section_ptr, NULL))
10283 eoinfo->file_sym_done = TRUE;
10286 indx = bfd_get_symcount (flinfo->output_bfd);
10287 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
10291 eoinfo->failed = TRUE;
10296 else if (h->indx == -2)
10302 /* Return TRUE if special handling is done for relocs in SEC against
10303 symbols defined in discarded sections. */
10306 elf_section_ignore_discarded_relocs (asection *sec)
10308 const struct elf_backend_data *bed;
10310 switch (sec->sec_info_type)
10312 case SEC_INFO_TYPE_STABS:
10313 case SEC_INFO_TYPE_EH_FRAME:
10314 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10320 bed = get_elf_backend_data (sec->owner);
10321 if (bed->elf_backend_ignore_discarded_relocs != NULL
10322 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
10328 /* Return a mask saying how ld should treat relocations in SEC against
10329 symbols defined in discarded sections. If this function returns
10330 COMPLAIN set, ld will issue a warning message. If this function
10331 returns PRETEND set, and the discarded section was link-once and the
10332 same size as the kept link-once section, ld will pretend that the
10333 symbol was actually defined in the kept section. Otherwise ld will
10334 zero the reloc (at least that is the intent, but some cooperation by
10335 the target dependent code is needed, particularly for REL targets). */
10338 _bfd_elf_default_action_discarded (asection *sec)
10340 if (sec->flags & SEC_DEBUGGING)
10343 if (strcmp (".eh_frame", sec->name) == 0)
10346 if (strcmp (".gcc_except_table", sec->name) == 0)
10349 return COMPLAIN | PRETEND;
10352 /* Find a match between a section and a member of a section group. */
10355 match_group_member (asection *sec, asection *group,
10356 struct bfd_link_info *info)
10358 asection *first = elf_next_in_group (group);
10359 asection *s = first;
10363 if (bfd_elf_match_symbols_in_sections (s, sec, info))
10366 s = elf_next_in_group (s);
10374 /* Check if the kept section of a discarded section SEC can be used
10375 to replace it. Return the replacement if it is OK. Otherwise return
10379 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
10383 kept = sec->kept_section;
10386 if ((kept->flags & SEC_GROUP) != 0)
10387 kept = match_group_member (sec, kept, info);
10389 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
10390 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
10392 sec->kept_section = kept;
10397 /* Link an input file into the linker output file. This function
10398 handles all the sections and relocations of the input file at once.
10399 This is so that we only have to read the local symbols once, and
10400 don't have to keep them in memory. */
10403 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
10405 int (*relocate_section)
10406 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
10407 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
10409 Elf_Internal_Shdr *symtab_hdr;
10410 size_t locsymcount;
10412 Elf_Internal_Sym *isymbuf;
10413 Elf_Internal_Sym *isym;
10414 Elf_Internal_Sym *isymend;
10416 asection **ppsection;
10418 const struct elf_backend_data *bed;
10419 struct elf_link_hash_entry **sym_hashes;
10420 bfd_size_type address_size;
10421 bfd_vma r_type_mask;
10423 bfd_boolean have_file_sym = FALSE;
10425 output_bfd = flinfo->output_bfd;
10426 bed = get_elf_backend_data (output_bfd);
10427 relocate_section = bed->elf_backend_relocate_section;
10429 /* If this is a dynamic object, we don't want to do anything here:
10430 we don't want the local symbols, and we don't want the section
10432 if ((input_bfd->flags & DYNAMIC) != 0)
10435 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
10436 if (elf_bad_symtab (input_bfd))
10438 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
10443 locsymcount = symtab_hdr->sh_info;
10444 extsymoff = symtab_hdr->sh_info;
10447 /* Read the local symbols. */
10448 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
10449 if (isymbuf == NULL && locsymcount != 0)
10451 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
10452 flinfo->internal_syms,
10453 flinfo->external_syms,
10454 flinfo->locsym_shndx);
10455 if (isymbuf == NULL)
10459 /* Find local symbol sections and adjust values of symbols in
10460 SEC_MERGE sections. Write out those local symbols we know are
10461 going into the output file. */
10462 isymend = isymbuf + locsymcount;
10463 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
10465 isym++, pindex++, ppsection++)
10469 Elf_Internal_Sym osym;
10475 if (elf_bad_symtab (input_bfd))
10477 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
10484 if (isym->st_shndx == SHN_UNDEF)
10485 isec = bfd_und_section_ptr;
10486 else if (isym->st_shndx == SHN_ABS)
10487 isec = bfd_abs_section_ptr;
10488 else if (isym->st_shndx == SHN_COMMON)
10489 isec = bfd_com_section_ptr;
10492 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
10495 /* Don't attempt to output symbols with st_shnx in the
10496 reserved range other than SHN_ABS and SHN_COMMON. */
10497 isec = bfd_und_section_ptr;
10499 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
10500 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
10502 _bfd_merged_section_offset (output_bfd, &isec,
10503 elf_section_data (isec)->sec_info,
10509 /* Don't output the first, undefined, symbol. In fact, don't
10510 output any undefined local symbol. */
10511 if (isec == bfd_und_section_ptr)
10514 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
10516 /* We never output section symbols. Instead, we use the
10517 section symbol of the corresponding section in the output
10522 /* If we are stripping all symbols, we don't want to output this
10524 if (flinfo->info->strip == strip_all)
10527 /* If we are discarding all local symbols, we don't want to
10528 output this one. If we are generating a relocatable output
10529 file, then some of the local symbols may be required by
10530 relocs; we output them below as we discover that they are
10532 if (flinfo->info->discard == discard_all)
10535 /* If this symbol is defined in a section which we are
10536 discarding, we don't need to keep it. */
10537 if (isym->st_shndx != SHN_UNDEF
10538 && isym->st_shndx < SHN_LORESERVE
10539 && bfd_section_removed_from_list (output_bfd,
10540 isec->output_section))
10543 /* Get the name of the symbol. */
10544 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
10549 /* See if we are discarding symbols with this name. */
10550 if ((flinfo->info->strip == strip_some
10551 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
10553 || (((flinfo->info->discard == discard_sec_merge
10554 && (isec->flags & SEC_MERGE)
10555 && !bfd_link_relocatable (flinfo->info))
10556 || flinfo->info->discard == discard_l)
10557 && bfd_is_local_label_name (input_bfd, name)))
10560 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
10562 if (input_bfd->lto_output)
10563 /* -flto puts a temp file name here. This means builds
10564 are not reproducible. Discard the symbol. */
10566 have_file_sym = TRUE;
10567 flinfo->filesym_count += 1;
10569 if (!have_file_sym)
10571 /* In the absence of debug info, bfd_find_nearest_line uses
10572 FILE symbols to determine the source file for local
10573 function symbols. Provide a FILE symbol here if input
10574 files lack such, so that their symbols won't be
10575 associated with a previous input file. It's not the
10576 source file, but the best we can do. */
10577 have_file_sym = TRUE;
10578 flinfo->filesym_count += 1;
10579 memset (&osym, 0, sizeof (osym));
10580 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
10581 osym.st_shndx = SHN_ABS;
10582 if (!elf_link_output_symstrtab (flinfo,
10583 (input_bfd->lto_output ? NULL
10584 : input_bfd->filename),
10585 &osym, bfd_abs_section_ptr,
10592 /* Adjust the section index for the output file. */
10593 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10594 isec->output_section);
10595 if (osym.st_shndx == SHN_BAD)
10598 /* ELF symbols in relocatable files are section relative, but
10599 in executable files they are virtual addresses. Note that
10600 this code assumes that all ELF sections have an associated
10601 BFD section with a reasonable value for output_offset; below
10602 we assume that they also have a reasonable value for
10603 output_section. Any special sections must be set up to meet
10604 these requirements. */
10605 osym.st_value += isec->output_offset;
10606 if (!bfd_link_relocatable (flinfo->info))
10608 osym.st_value += isec->output_section->vma;
10609 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
10611 /* STT_TLS symbols are relative to PT_TLS segment base. */
10612 if (elf_hash_table (flinfo->info)->tls_sec != NULL)
10613 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
10615 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
10620 indx = bfd_get_symcount (output_bfd);
10621 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
10628 if (bed->s->arch_size == 32)
10630 r_type_mask = 0xff;
10636 r_type_mask = 0xffffffff;
10641 /* Relocate the contents of each section. */
10642 sym_hashes = elf_sym_hashes (input_bfd);
10643 for (o = input_bfd->sections; o != NULL; o = o->next)
10645 bfd_byte *contents;
10647 if (! o->linker_mark)
10649 /* This section was omitted from the link. */
10653 if (!flinfo->info->resolve_section_groups
10654 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
10656 /* Deal with the group signature symbol. */
10657 struct bfd_elf_section_data *sec_data = elf_section_data (o);
10658 unsigned long symndx = sec_data->this_hdr.sh_info;
10659 asection *osec = o->output_section;
10661 BFD_ASSERT (bfd_link_relocatable (flinfo->info));
10662 if (symndx >= locsymcount
10663 || (elf_bad_symtab (input_bfd)
10664 && flinfo->sections[symndx] == NULL))
10666 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
10667 while (h->root.type == bfd_link_hash_indirect
10668 || h->root.type == bfd_link_hash_warning)
10669 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10670 /* Arrange for symbol to be output. */
10672 elf_section_data (osec)->this_hdr.sh_info = -2;
10674 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
10676 /* We'll use the output section target_index. */
10677 asection *sec = flinfo->sections[symndx]->output_section;
10678 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
10682 if (flinfo->indices[symndx] == -1)
10684 /* Otherwise output the local symbol now. */
10685 Elf_Internal_Sym sym = isymbuf[symndx];
10686 asection *sec = flinfo->sections[symndx]->output_section;
10691 name = bfd_elf_string_from_elf_section (input_bfd,
10692 symtab_hdr->sh_link,
10697 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
10699 if (sym.st_shndx == SHN_BAD)
10702 sym.st_value += o->output_offset;
10704 indx = bfd_get_symcount (output_bfd);
10705 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
10710 flinfo->indices[symndx] = indx;
10714 elf_section_data (osec)->this_hdr.sh_info
10715 = flinfo->indices[symndx];
10719 if ((o->flags & SEC_HAS_CONTENTS) == 0
10720 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
10723 if ((o->flags & SEC_LINKER_CREATED) != 0)
10725 /* Section was created by _bfd_elf_link_create_dynamic_sections
10730 /* Get the contents of the section. They have been cached by a
10731 relaxation routine. Note that o is a section in an input
10732 file, so the contents field will not have been set by any of
10733 the routines which work on output files. */
10734 if (elf_section_data (o)->this_hdr.contents != NULL)
10736 contents = elf_section_data (o)->this_hdr.contents;
10737 if (bed->caches_rawsize
10739 && o->rawsize < o->size)
10741 memcpy (flinfo->contents, contents, o->rawsize);
10742 contents = flinfo->contents;
10747 contents = flinfo->contents;
10748 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
10752 if ((o->flags & SEC_RELOC) != 0)
10754 Elf_Internal_Rela *internal_relocs;
10755 Elf_Internal_Rela *rel, *relend;
10756 int action_discarded;
10759 /* Get the swapped relocs. */
10761 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
10762 flinfo->internal_relocs, FALSE);
10763 if (internal_relocs == NULL
10764 && o->reloc_count > 0)
10767 /* We need to reverse-copy input .ctors/.dtors sections if
10768 they are placed in .init_array/.finit_array for output. */
10769 if (o->size > address_size
10770 && ((strncmp (o->name, ".ctors", 6) == 0
10771 && strcmp (o->output_section->name,
10772 ".init_array") == 0)
10773 || (strncmp (o->name, ".dtors", 6) == 0
10774 && strcmp (o->output_section->name,
10775 ".fini_array") == 0))
10776 && (o->name[6] == 0 || o->name[6] == '.'))
10778 if (o->size * bed->s->int_rels_per_ext_rel
10779 != o->reloc_count * address_size)
10782 /* xgettext:c-format */
10783 (_("error: %pB: size of section %pA is not "
10784 "multiple of address size"),
10786 bfd_set_error (bfd_error_bad_value);
10789 o->flags |= SEC_ELF_REVERSE_COPY;
10792 action_discarded = -1;
10793 if (!elf_section_ignore_discarded_relocs (o))
10794 action_discarded = (*bed->action_discarded) (o);
10796 /* Run through the relocs evaluating complex reloc symbols and
10797 looking for relocs against symbols from discarded sections
10798 or section symbols from removed link-once sections.
10799 Complain about relocs against discarded sections. Zero
10800 relocs against removed link-once sections. */
10802 rel = internal_relocs;
10803 relend = rel + o->reloc_count;
10804 for ( ; rel < relend; rel++)
10806 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10807 unsigned int s_type;
10808 asection **ps, *sec;
10809 struct elf_link_hash_entry *h = NULL;
10810 const char *sym_name;
10812 if (r_symndx == STN_UNDEF)
10815 if (r_symndx >= locsymcount
10816 || (elf_bad_symtab (input_bfd)
10817 && flinfo->sections[r_symndx] == NULL))
10819 h = sym_hashes[r_symndx - extsymoff];
10821 /* Badly formatted input files can contain relocs that
10822 reference non-existant symbols. Check here so that
10823 we do not seg fault. */
10827 /* xgettext:c-format */
10828 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
10829 "that references a non-existent global symbol"),
10830 input_bfd, (uint64_t) rel->r_info, o);
10831 bfd_set_error (bfd_error_bad_value);
10835 while (h->root.type == bfd_link_hash_indirect
10836 || h->root.type == bfd_link_hash_warning)
10837 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10841 /* If a plugin symbol is referenced from a non-IR file,
10842 mark the symbol as undefined. Note that the
10843 linker may attach linker created dynamic sections
10844 to the plugin bfd. Symbols defined in linker
10845 created sections are not plugin symbols. */
10846 if ((h->root.non_ir_ref_regular
10847 || h->root.non_ir_ref_dynamic)
10848 && (h->root.type == bfd_link_hash_defined
10849 || h->root.type == bfd_link_hash_defweak)
10850 && (h->root.u.def.section->flags
10851 & SEC_LINKER_CREATED) == 0
10852 && h->root.u.def.section->owner != NULL
10853 && (h->root.u.def.section->owner->flags
10854 & BFD_PLUGIN) != 0)
10856 h->root.type = bfd_link_hash_undefined;
10857 h->root.u.undef.abfd = h->root.u.def.section->owner;
10861 if (h->root.type == bfd_link_hash_defined
10862 || h->root.type == bfd_link_hash_defweak)
10863 ps = &h->root.u.def.section;
10865 sym_name = h->root.root.string;
10869 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10871 s_type = ELF_ST_TYPE (sym->st_info);
10872 ps = &flinfo->sections[r_symndx];
10873 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10877 if ((s_type == STT_RELC || s_type == STT_SRELC)
10878 && !bfd_link_relocatable (flinfo->info))
10881 bfd_vma dot = (rel->r_offset
10882 + o->output_offset + o->output_section->vma);
10884 printf ("Encountered a complex symbol!");
10885 printf (" (input_bfd %s, section %s, reloc %ld\n",
10886 input_bfd->filename, o->name,
10887 (long) (rel - internal_relocs));
10888 printf (" symbol: idx %8.8lx, name %s\n",
10889 r_symndx, sym_name);
10890 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10891 (unsigned long) rel->r_info,
10892 (unsigned long) rel->r_offset);
10894 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10895 isymbuf, locsymcount, s_type == STT_SRELC))
10898 /* Symbol evaluated OK. Update to absolute value. */
10899 set_symbol_value (input_bfd, isymbuf, locsymcount,
10904 if (action_discarded != -1 && ps != NULL)
10906 /* Complain if the definition comes from a
10907 discarded section. */
10908 if ((sec = *ps) != NULL && discarded_section (sec))
10910 BFD_ASSERT (r_symndx != STN_UNDEF);
10911 if (action_discarded & COMPLAIN)
10912 (*flinfo->info->callbacks->einfo)
10913 /* xgettext:c-format */
10914 (_("%X`%s' referenced in section `%pA' of %pB: "
10915 "defined in discarded section `%pA' of %pB\n"),
10916 sym_name, o, input_bfd, sec, sec->owner);
10918 /* Try to do the best we can to support buggy old
10919 versions of gcc. Pretend that the symbol is
10920 really defined in the kept linkonce section.
10921 FIXME: This is quite broken. Modifying the
10922 symbol here means we will be changing all later
10923 uses of the symbol, not just in this section. */
10924 if (action_discarded & PRETEND)
10928 kept = _bfd_elf_check_kept_section (sec,
10940 /* Relocate the section by invoking a back end routine.
10942 The back end routine is responsible for adjusting the
10943 section contents as necessary, and (if using Rela relocs
10944 and generating a relocatable output file) adjusting the
10945 reloc addend as necessary.
10947 The back end routine does not have to worry about setting
10948 the reloc address or the reloc symbol index.
10950 The back end routine is given a pointer to the swapped in
10951 internal symbols, and can access the hash table entries
10952 for the external symbols via elf_sym_hashes (input_bfd).
10954 When generating relocatable output, the back end routine
10955 must handle STB_LOCAL/STT_SECTION symbols specially. The
10956 output symbol is going to be a section symbol
10957 corresponding to the output section, which will require
10958 the addend to be adjusted. */
10960 ret = (*relocate_section) (output_bfd, flinfo->info,
10961 input_bfd, o, contents,
10969 || bfd_link_relocatable (flinfo->info)
10970 || flinfo->info->emitrelocations)
10972 Elf_Internal_Rela *irela;
10973 Elf_Internal_Rela *irelaend, *irelamid;
10974 bfd_vma last_offset;
10975 struct elf_link_hash_entry **rel_hash;
10976 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10977 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10978 unsigned int next_erel;
10979 bfd_boolean rela_normal;
10980 struct bfd_elf_section_data *esdi, *esdo;
10982 esdi = elf_section_data (o);
10983 esdo = elf_section_data (o->output_section);
10984 rela_normal = FALSE;
10986 /* Adjust the reloc addresses and symbol indices. */
10988 irela = internal_relocs;
10989 irelaend = irela + o->reloc_count;
10990 rel_hash = esdo->rel.hashes + esdo->rel.count;
10991 /* We start processing the REL relocs, if any. When we reach
10992 IRELAMID in the loop, we switch to the RELA relocs. */
10994 if (esdi->rel.hdr != NULL)
10995 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10996 * bed->s->int_rels_per_ext_rel);
10997 rel_hash_list = rel_hash;
10998 rela_hash_list = NULL;
10999 last_offset = o->output_offset;
11000 if (!bfd_link_relocatable (flinfo->info))
11001 last_offset += o->output_section->vma;
11002 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
11004 unsigned long r_symndx;
11006 Elf_Internal_Sym sym;
11008 if (next_erel == bed->s->int_rels_per_ext_rel)
11014 if (irela == irelamid)
11016 rel_hash = esdo->rela.hashes + esdo->rela.count;
11017 rela_hash_list = rel_hash;
11018 rela_normal = bed->rela_normal;
11021 irela->r_offset = _bfd_elf_section_offset (output_bfd,
11024 if (irela->r_offset >= (bfd_vma) -2)
11026 /* This is a reloc for a deleted entry or somesuch.
11027 Turn it into an R_*_NONE reloc, at the same
11028 offset as the last reloc. elf_eh_frame.c and
11029 bfd_elf_discard_info rely on reloc offsets
11031 irela->r_offset = last_offset;
11033 irela->r_addend = 0;
11037 irela->r_offset += o->output_offset;
11039 /* Relocs in an executable have to be virtual addresses. */
11040 if (!bfd_link_relocatable (flinfo->info))
11041 irela->r_offset += o->output_section->vma;
11043 last_offset = irela->r_offset;
11045 r_symndx = irela->r_info >> r_sym_shift;
11046 if (r_symndx == STN_UNDEF)
11049 if (r_symndx >= locsymcount
11050 || (elf_bad_symtab (input_bfd)
11051 && flinfo->sections[r_symndx] == NULL))
11053 struct elf_link_hash_entry *rh;
11054 unsigned long indx;
11056 /* This is a reloc against a global symbol. We
11057 have not yet output all the local symbols, so
11058 we do not know the symbol index of any global
11059 symbol. We set the rel_hash entry for this
11060 reloc to point to the global hash table entry
11061 for this symbol. The symbol index is then
11062 set at the end of bfd_elf_final_link. */
11063 indx = r_symndx - extsymoff;
11064 rh = elf_sym_hashes (input_bfd)[indx];
11065 while (rh->root.type == bfd_link_hash_indirect
11066 || rh->root.type == bfd_link_hash_warning)
11067 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
11069 /* Setting the index to -2 tells
11070 elf_link_output_extsym that this symbol is
11071 used by a reloc. */
11072 BFD_ASSERT (rh->indx < 0);
11079 /* This is a reloc against a local symbol. */
11082 sym = isymbuf[r_symndx];
11083 sec = flinfo->sections[r_symndx];
11084 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
11086 /* I suppose the backend ought to fill in the
11087 section of any STT_SECTION symbol against a
11088 processor specific section. */
11089 r_symndx = STN_UNDEF;
11090 if (bfd_is_abs_section (sec))
11092 else if (sec == NULL || sec->owner == NULL)
11094 bfd_set_error (bfd_error_bad_value);
11099 asection *osec = sec->output_section;
11101 /* If we have discarded a section, the output
11102 section will be the absolute section. In
11103 case of discarded SEC_MERGE sections, use
11104 the kept section. relocate_section should
11105 have already handled discarded linkonce
11107 if (bfd_is_abs_section (osec)
11108 && sec->kept_section != NULL
11109 && sec->kept_section->output_section != NULL)
11111 osec = sec->kept_section->output_section;
11112 irela->r_addend -= osec->vma;
11115 if (!bfd_is_abs_section (osec))
11117 r_symndx = osec->target_index;
11118 if (r_symndx == STN_UNDEF)
11120 irela->r_addend += osec->vma;
11121 osec = _bfd_nearby_section (output_bfd, osec,
11123 irela->r_addend -= osec->vma;
11124 r_symndx = osec->target_index;
11129 /* Adjust the addend according to where the
11130 section winds up in the output section. */
11132 irela->r_addend += sec->output_offset;
11136 if (flinfo->indices[r_symndx] == -1)
11138 unsigned long shlink;
11143 if (flinfo->info->strip == strip_all)
11145 /* You can't do ld -r -s. */
11146 bfd_set_error (bfd_error_invalid_operation);
11150 /* This symbol was skipped earlier, but
11151 since it is needed by a reloc, we
11152 must output it now. */
11153 shlink = symtab_hdr->sh_link;
11154 name = (bfd_elf_string_from_elf_section
11155 (input_bfd, shlink, sym.st_name));
11159 osec = sec->output_section;
11161 _bfd_elf_section_from_bfd_section (output_bfd,
11163 if (sym.st_shndx == SHN_BAD)
11166 sym.st_value += sec->output_offset;
11167 if (!bfd_link_relocatable (flinfo->info))
11169 sym.st_value += osec->vma;
11170 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
11172 struct elf_link_hash_table *htab
11173 = elf_hash_table (flinfo->info);
11175 /* STT_TLS symbols are relative to PT_TLS
11177 if (htab->tls_sec != NULL)
11178 sym.st_value -= htab->tls_sec->vma;
11181 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
11186 indx = bfd_get_symcount (output_bfd);
11187 ret = elf_link_output_symstrtab (flinfo, name,
11193 flinfo->indices[r_symndx] = indx;
11198 r_symndx = flinfo->indices[r_symndx];
11201 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
11202 | (irela->r_info & r_type_mask));
11205 /* Swap out the relocs. */
11206 input_rel_hdr = esdi->rel.hdr;
11207 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
11209 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11214 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
11215 * bed->s->int_rels_per_ext_rel);
11216 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
11219 input_rela_hdr = esdi->rela.hdr;
11220 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
11222 if (!bed->elf_backend_emit_relocs (output_bfd, o,
11231 /* Write out the modified section contents. */
11232 if (bed->elf_backend_write_section
11233 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
11236 /* Section written out. */
11238 else switch (o->sec_info_type)
11240 case SEC_INFO_TYPE_STABS:
11241 if (! (_bfd_write_section_stabs
11243 &elf_hash_table (flinfo->info)->stab_info,
11244 o, &elf_section_data (o)->sec_info, contents)))
11247 case SEC_INFO_TYPE_MERGE:
11248 if (! _bfd_write_merged_section (output_bfd, o,
11249 elf_section_data (o)->sec_info))
11252 case SEC_INFO_TYPE_EH_FRAME:
11254 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
11259 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
11261 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
11269 if (! (o->flags & SEC_EXCLUDE))
11271 file_ptr offset = (file_ptr) o->output_offset;
11272 bfd_size_type todo = o->size;
11274 offset *= bfd_octets_per_byte (output_bfd);
11276 if ((o->flags & SEC_ELF_REVERSE_COPY))
11278 /* Reverse-copy input section to output. */
11281 todo -= address_size;
11282 if (! bfd_set_section_contents (output_bfd,
11290 offset += address_size;
11294 else if (! bfd_set_section_contents (output_bfd,
11308 /* Generate a reloc when linking an ELF file. This is a reloc
11309 requested by the linker, and does not come from any input file. This
11310 is used to build constructor and destructor tables when linking
11314 elf_reloc_link_order (bfd *output_bfd,
11315 struct bfd_link_info *info,
11316 asection *output_section,
11317 struct bfd_link_order *link_order)
11319 reloc_howto_type *howto;
11323 struct bfd_elf_section_reloc_data *reldata;
11324 struct elf_link_hash_entry **rel_hash_ptr;
11325 Elf_Internal_Shdr *rel_hdr;
11326 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
11327 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
11330 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
11332 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
11335 bfd_set_error (bfd_error_bad_value);
11339 addend = link_order->u.reloc.p->addend;
11342 reldata = &esdo->rel;
11343 else if (esdo->rela.hdr)
11344 reldata = &esdo->rela;
11351 /* Figure out the symbol index. */
11352 rel_hash_ptr = reldata->hashes + reldata->count;
11353 if (link_order->type == bfd_section_reloc_link_order)
11355 indx = link_order->u.reloc.p->u.section->target_index;
11356 BFD_ASSERT (indx != 0);
11357 *rel_hash_ptr = NULL;
11361 struct elf_link_hash_entry *h;
11363 /* Treat a reloc against a defined symbol as though it were
11364 actually against the section. */
11365 h = ((struct elf_link_hash_entry *)
11366 bfd_wrapped_link_hash_lookup (output_bfd, info,
11367 link_order->u.reloc.p->u.name,
11368 FALSE, FALSE, TRUE));
11370 && (h->root.type == bfd_link_hash_defined
11371 || h->root.type == bfd_link_hash_defweak))
11375 section = h->root.u.def.section;
11376 indx = section->output_section->target_index;
11377 *rel_hash_ptr = NULL;
11378 /* It seems that we ought to add the symbol value to the
11379 addend here, but in practice it has already been added
11380 because it was passed to constructor_callback. */
11381 addend += section->output_section->vma + section->output_offset;
11383 else if (h != NULL)
11385 /* Setting the index to -2 tells elf_link_output_extsym that
11386 this symbol is used by a reloc. */
11393 (*info->callbacks->unattached_reloc)
11394 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
11399 /* If this is an inplace reloc, we must write the addend into the
11401 if (howto->partial_inplace && addend != 0)
11403 bfd_size_type size;
11404 bfd_reloc_status_type rstat;
11407 const char *sym_name;
11409 size = (bfd_size_type) bfd_get_reloc_size (howto);
11410 buf = (bfd_byte *) bfd_zmalloc (size);
11411 if (buf == NULL && size != 0)
11413 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
11420 case bfd_reloc_outofrange:
11423 case bfd_reloc_overflow:
11424 if (link_order->type == bfd_section_reloc_link_order)
11425 sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
11427 sym_name = link_order->u.reloc.p->u.name;
11428 (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
11429 howto->name, addend, NULL, NULL,
11434 ok = bfd_set_section_contents (output_bfd, output_section, buf,
11436 * bfd_octets_per_byte (output_bfd),
11443 /* The address of a reloc is relative to the section in a
11444 relocatable file, and is a virtual address in an executable
11446 offset = link_order->offset;
11447 if (! bfd_link_relocatable (info))
11448 offset += output_section->vma;
11450 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
11452 irel[i].r_offset = offset;
11453 irel[i].r_info = 0;
11454 irel[i].r_addend = 0;
11456 if (bed->s->arch_size == 32)
11457 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
11459 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
11461 rel_hdr = reldata->hdr;
11462 erel = rel_hdr->contents;
11463 if (rel_hdr->sh_type == SHT_REL)
11465 erel += reldata->count * bed->s->sizeof_rel;
11466 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
11470 irel[0].r_addend = addend;
11471 erel += reldata->count * bed->s->sizeof_rela;
11472 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
11481 /* Compare two sections based on the locations of the sections they are
11482 linked to. Used by elf_fixup_link_order. */
11485 compare_link_order (const void *a, const void *b)
11487 const struct bfd_link_order *alo = *(const struct bfd_link_order **) a;
11488 const struct bfd_link_order *blo = *(const struct bfd_link_order **) b;
11489 asection *asec = elf_linked_to_section (alo->u.indirect.section);
11490 asection *bsec = elf_linked_to_section (blo->u.indirect.section);
11491 bfd_vma apos = asec->output_section->lma + asec->output_offset;
11492 bfd_vma bpos = bsec->output_section->lma + bsec->output_offset;
11499 /* The only way we should get matching LMAs is when the first of two
11500 sections has zero size. */
11501 if (asec->size < bsec->size)
11503 if (asec->size > bsec->size)
11506 /* If they are both zero size then they almost certainly have the same
11507 VMA and thus are not ordered with respect to each other. Test VMA
11508 anyway, and fall back to id to make the result reproducible across
11509 qsort implementations. */
11510 apos = asec->output_section->vma + asec->output_offset;
11511 bpos = bsec->output_section->vma + bsec->output_offset;
11517 return asec->id - bsec->id;
11521 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
11522 order as their linked sections. Returns false if this could not be done
11523 because an output section includes both ordered and unordered
11524 sections. Ideally we'd do this in the linker proper. */
11527 elf_fixup_link_order (bfd *abfd, asection *o)
11529 size_t seen_linkorder;
11532 struct bfd_link_order *p;
11534 struct bfd_link_order **sections;
11535 asection *s, *other_sec, *linkorder_sec;
11539 linkorder_sec = NULL;
11541 seen_linkorder = 0;
11542 for (p = o->map_head.link_order; p != NULL; p = p->next)
11544 if (p->type == bfd_indirect_link_order)
11546 s = p->u.indirect.section;
11548 if ((s->flags & SEC_LINKER_CREATED) == 0
11549 && bfd_get_flavour (sub) == bfd_target_elf_flavour
11550 && elf_section_data (s) != NULL
11551 && elf_linked_to_section (s) != NULL)
11565 if (seen_other && seen_linkorder)
11567 if (other_sec && linkorder_sec)
11569 /* xgettext:c-format */
11570 (_("%pA has both ordered [`%pA' in %pB] "
11571 "and unordered [`%pA' in %pB] sections"),
11572 o, linkorder_sec, linkorder_sec->owner,
11573 other_sec, other_sec->owner);
11576 (_("%pA has both ordered and unordered sections"), o);
11577 bfd_set_error (bfd_error_bad_value);
11582 if (!seen_linkorder)
11585 sections = bfd_malloc (seen_linkorder * sizeof (*sections));
11586 if (sections == NULL)
11589 seen_linkorder = 0;
11590 for (p = o->map_head.link_order; p != NULL; p = p->next)
11591 sections[seen_linkorder++] = p;
11593 /* Sort the input sections in the order of their linked section. */
11594 qsort (sections, seen_linkorder, sizeof (*sections), compare_link_order);
11596 /* Change the offsets of the sections. */
11598 for (n = 0; n < seen_linkorder; n++)
11601 s = sections[n]->u.indirect.section;
11602 mask = ~(bfd_vma) 0 << s->alignment_power;
11603 offset = (offset + ~mask) & mask;
11604 s->output_offset = offset / bfd_octets_per_byte (abfd);
11605 sections[n]->offset = offset;
11606 offset += sections[n]->size;
11613 /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
11614 Returns TRUE upon success, FALSE otherwise. */
11617 elf_output_implib (bfd *abfd, struct bfd_link_info *info)
11619 bfd_boolean ret = FALSE;
11621 const struct elf_backend_data *bed;
11623 enum bfd_architecture arch;
11625 asymbol **sympp = NULL;
11629 elf_symbol_type *osymbuf;
11631 implib_bfd = info->out_implib_bfd;
11632 bed = get_elf_backend_data (abfd);
11634 if (!bfd_set_format (implib_bfd, bfd_object))
11637 /* Use flag from executable but make it a relocatable object. */
11638 flags = bfd_get_file_flags (abfd);
11639 flags &= ~HAS_RELOC;
11640 if (!bfd_set_start_address (implib_bfd, 0)
11641 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
11644 /* Copy architecture of output file to import library file. */
11645 arch = bfd_get_arch (abfd);
11646 mach = bfd_get_mach (abfd);
11647 if (!bfd_set_arch_mach (implib_bfd, arch, mach)
11648 && (abfd->target_defaulted
11649 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
11652 /* Get symbol table size. */
11653 symsize = bfd_get_symtab_upper_bound (abfd);
11657 /* Read in the symbol table. */
11658 sympp = (asymbol **) bfd_malloc (symsize);
11662 symcount = bfd_canonicalize_symtab (abfd, sympp);
11666 /* Allow the BFD backend to copy any private header data it
11667 understands from the output BFD to the import library BFD. */
11668 if (! bfd_copy_private_header_data (abfd, implib_bfd))
11671 /* Filter symbols to appear in the import library. */
11672 if (bed->elf_backend_filter_implib_symbols)
11673 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
11676 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
11679 bfd_set_error (bfd_error_no_symbols);
11680 _bfd_error_handler (_("%pB: no symbol found for import library"),
11686 /* Make symbols absolute. */
11687 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount,
11688 sizeof (*osymbuf));
11689 if (osymbuf == NULL)
11692 for (src_count = 0; src_count < symcount; src_count++)
11694 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
11695 sizeof (*osymbuf));
11696 osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
11697 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
11698 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
11699 osymbuf[src_count].internal_elf_sym.st_value =
11700 osymbuf[src_count].symbol.value;
11701 sympp[src_count] = &osymbuf[src_count].symbol;
11704 bfd_set_symtab (implib_bfd, sympp, symcount);
11706 /* Allow the BFD backend to copy any private data it understands
11707 from the output BFD to the import library BFD. This is done last
11708 to permit the routine to look at the filtered symbol table. */
11709 if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
11712 if (!bfd_close (implib_bfd))
11723 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
11727 if (flinfo->symstrtab != NULL)
11728 _bfd_elf_strtab_free (flinfo->symstrtab);
11729 if (flinfo->contents != NULL)
11730 free (flinfo->contents);
11731 if (flinfo->external_relocs != NULL)
11732 free (flinfo->external_relocs);
11733 if (flinfo->internal_relocs != NULL)
11734 free (flinfo->internal_relocs);
11735 if (flinfo->external_syms != NULL)
11736 free (flinfo->external_syms);
11737 if (flinfo->locsym_shndx != NULL)
11738 free (flinfo->locsym_shndx);
11739 if (flinfo->internal_syms != NULL)
11740 free (flinfo->internal_syms);
11741 if (flinfo->indices != NULL)
11742 free (flinfo->indices);
11743 if (flinfo->sections != NULL)
11744 free (flinfo->sections);
11745 if (flinfo->symshndxbuf != NULL
11746 && flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
11747 free (flinfo->symshndxbuf);
11748 for (o = obfd->sections; o != NULL; o = o->next)
11750 struct bfd_elf_section_data *esdo = elf_section_data (o);
11751 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
11752 free (esdo->rel.hashes);
11753 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
11754 free (esdo->rela.hashes);
11758 /* Do the final step of an ELF link. */
11761 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
11763 bfd_boolean dynamic;
11764 bfd_boolean emit_relocs;
11766 struct elf_final_link_info flinfo;
11768 struct bfd_link_order *p;
11770 bfd_size_type max_contents_size;
11771 bfd_size_type max_external_reloc_size;
11772 bfd_size_type max_internal_reloc_count;
11773 bfd_size_type max_sym_count;
11774 bfd_size_type max_sym_shndx_count;
11775 Elf_Internal_Sym elfsym;
11777 Elf_Internal_Shdr *symtab_hdr;
11778 Elf_Internal_Shdr *symtab_shndx_hdr;
11779 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11780 struct elf_outext_info eoinfo;
11781 bfd_boolean merged;
11782 size_t relativecount = 0;
11783 asection *reldyn = 0;
11785 asection *attr_section = NULL;
11786 bfd_vma attr_size = 0;
11787 const char *std_attrs_section;
11788 struct elf_link_hash_table *htab = elf_hash_table (info);
11790 if (!is_elf_hash_table (htab))
11793 if (bfd_link_pic (info))
11794 abfd->flags |= DYNAMIC;
11796 dynamic = htab->dynamic_sections_created;
11797 dynobj = htab->dynobj;
11799 emit_relocs = (bfd_link_relocatable (info)
11800 || info->emitrelocations);
11802 flinfo.info = info;
11803 flinfo.output_bfd = abfd;
11804 flinfo.symstrtab = _bfd_elf_strtab_init ();
11805 if (flinfo.symstrtab == NULL)
11810 flinfo.hash_sec = NULL;
11811 flinfo.symver_sec = NULL;
11815 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
11816 /* Note that dynsym_sec can be NULL (on VMS). */
11817 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
11818 /* Note that it is OK if symver_sec is NULL. */
11821 flinfo.contents = NULL;
11822 flinfo.external_relocs = NULL;
11823 flinfo.internal_relocs = NULL;
11824 flinfo.external_syms = NULL;
11825 flinfo.locsym_shndx = NULL;
11826 flinfo.internal_syms = NULL;
11827 flinfo.indices = NULL;
11828 flinfo.sections = NULL;
11829 flinfo.symshndxbuf = NULL;
11830 flinfo.filesym_count = 0;
11832 /* The object attributes have been merged. Remove the input
11833 sections from the link, and set the contents of the output
11835 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
11836 for (o = abfd->sections; o != NULL; o = o->next)
11838 bfd_boolean remove_section = FALSE;
11840 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
11841 || strcmp (o->name, ".gnu.attributes") == 0)
11843 for (p = o->map_head.link_order; p != NULL; p = p->next)
11845 asection *input_section;
11847 if (p->type != bfd_indirect_link_order)
11849 input_section = p->u.indirect.section;
11850 /* Hack: reset the SEC_HAS_CONTENTS flag so that
11851 elf_link_input_bfd ignores this section. */
11852 input_section->flags &= ~SEC_HAS_CONTENTS;
11855 attr_size = bfd_elf_obj_attr_size (abfd);
11856 bfd_set_section_size (o, attr_size);
11857 /* Skip this section later on. */
11858 o->map_head.link_order = NULL;
11862 remove_section = TRUE;
11864 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
11866 /* Remove empty group section from linker output. */
11867 remove_section = TRUE;
11869 if (remove_section)
11871 o->flags |= SEC_EXCLUDE;
11872 bfd_section_list_remove (abfd, o);
11873 abfd->section_count--;
11877 /* Count up the number of relocations we will output for each output
11878 section, so that we know the sizes of the reloc sections. We
11879 also figure out some maximum sizes. */
11880 max_contents_size = 0;
11881 max_external_reloc_size = 0;
11882 max_internal_reloc_count = 0;
11884 max_sym_shndx_count = 0;
11886 for (o = abfd->sections; o != NULL; o = o->next)
11888 struct bfd_elf_section_data *esdo = elf_section_data (o);
11889 o->reloc_count = 0;
11891 for (p = o->map_head.link_order; p != NULL; p = p->next)
11893 unsigned int reloc_count = 0;
11894 unsigned int additional_reloc_count = 0;
11895 struct bfd_elf_section_data *esdi = NULL;
11897 if (p->type == bfd_section_reloc_link_order
11898 || p->type == bfd_symbol_reloc_link_order)
11900 else if (p->type == bfd_indirect_link_order)
11904 sec = p->u.indirect.section;
11906 /* Mark all sections which are to be included in the
11907 link. This will normally be every section. We need
11908 to do this so that we can identify any sections which
11909 the linker has decided to not include. */
11910 sec->linker_mark = TRUE;
11912 if (sec->flags & SEC_MERGE)
11915 if (sec->rawsize > max_contents_size)
11916 max_contents_size = sec->rawsize;
11917 if (sec->size > max_contents_size)
11918 max_contents_size = sec->size;
11920 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11921 && (sec->owner->flags & DYNAMIC) == 0)
11925 /* We are interested in just local symbols, not all
11927 if (elf_bad_symtab (sec->owner))
11928 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11929 / bed->s->sizeof_sym);
11931 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11933 if (sym_count > max_sym_count)
11934 max_sym_count = sym_count;
11936 if (sym_count > max_sym_shndx_count
11937 && elf_symtab_shndx_list (sec->owner) != NULL)
11938 max_sym_shndx_count = sym_count;
11940 if (esdo->this_hdr.sh_type == SHT_REL
11941 || esdo->this_hdr.sh_type == SHT_RELA)
11942 /* Some backends use reloc_count in relocation sections
11943 to count particular types of relocs. Of course,
11944 reloc sections themselves can't have relocations. */
11946 else if (emit_relocs)
11948 reloc_count = sec->reloc_count;
11949 if (bed->elf_backend_count_additional_relocs)
11952 c = (*bed->elf_backend_count_additional_relocs) (sec);
11953 additional_reloc_count += c;
11956 else if (bed->elf_backend_count_relocs)
11957 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11959 esdi = elf_section_data (sec);
11961 if ((sec->flags & SEC_RELOC) != 0)
11963 size_t ext_size = 0;
11965 if (esdi->rel.hdr != NULL)
11966 ext_size = esdi->rel.hdr->sh_size;
11967 if (esdi->rela.hdr != NULL)
11968 ext_size += esdi->rela.hdr->sh_size;
11970 if (ext_size > max_external_reloc_size)
11971 max_external_reloc_size = ext_size;
11972 if (sec->reloc_count > max_internal_reloc_count)
11973 max_internal_reloc_count = sec->reloc_count;
11978 if (reloc_count == 0)
11981 reloc_count += additional_reloc_count;
11982 o->reloc_count += reloc_count;
11984 if (p->type == bfd_indirect_link_order && emit_relocs)
11988 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11989 esdo->rel.count += additional_reloc_count;
11991 if (esdi->rela.hdr)
11993 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11994 esdo->rela.count += additional_reloc_count;
12000 esdo->rela.count += reloc_count;
12002 esdo->rel.count += reloc_count;
12006 if (o->reloc_count > 0)
12007 o->flags |= SEC_RELOC;
12010 /* Explicitly clear the SEC_RELOC flag. The linker tends to
12011 set it (this is probably a bug) and if it is set
12012 assign_section_numbers will create a reloc section. */
12013 o->flags &=~ SEC_RELOC;
12016 /* If the SEC_ALLOC flag is not set, force the section VMA to
12017 zero. This is done in elf_fake_sections as well, but forcing
12018 the VMA to 0 here will ensure that relocs against these
12019 sections are handled correctly. */
12020 if ((o->flags & SEC_ALLOC) == 0
12021 && ! o->user_set_vma)
12025 if (! bfd_link_relocatable (info) && merged)
12026 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
12028 /* Figure out the file positions for everything but the symbol table
12029 and the relocs. We set symcount to force assign_section_numbers
12030 to create a symbol table. */
12031 abfd->symcount = info->strip != strip_all || emit_relocs;
12032 BFD_ASSERT (! abfd->output_has_begun);
12033 if (! _bfd_elf_compute_section_file_positions (abfd, info))
12036 /* Set sizes, and assign file positions for reloc sections. */
12037 for (o = abfd->sections; o != NULL; o = o->next)
12039 struct bfd_elf_section_data *esdo = elf_section_data (o);
12040 if ((o->flags & SEC_RELOC) != 0)
12043 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
12047 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
12051 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
12052 to count upwards while actually outputting the relocations. */
12053 esdo->rel.count = 0;
12054 esdo->rela.count = 0;
12056 if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
12057 && !bfd_section_is_ctf (o))
12059 /* Cache the section contents so that they can be compressed
12060 later. Use bfd_malloc since it will be freed by
12061 bfd_compress_section_contents. */
12062 unsigned char *contents = esdo->this_hdr.contents;
12063 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
12066 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
12067 if (contents == NULL)
12069 esdo->this_hdr.contents = contents;
12073 /* We have now assigned file positions for all the sections except .symtab,
12074 .strtab, and non-loaded reloc and compressed debugging sections. We start
12075 the .symtab section at the current file position, and write directly to it.
12076 We build the .strtab section in memory. */
12077 abfd->symcount = 0;
12078 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12079 /* sh_name is set in prep_headers. */
12080 symtab_hdr->sh_type = SHT_SYMTAB;
12081 /* sh_flags, sh_addr and sh_size all start off zero. */
12082 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
12083 /* sh_link is set in assign_section_numbers. */
12084 /* sh_info is set below. */
12085 /* sh_offset is set just below. */
12086 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
12088 if (max_sym_count < 20)
12089 max_sym_count = 20;
12090 htab->strtabsize = max_sym_count;
12091 amt = max_sym_count * sizeof (struct elf_sym_strtab);
12092 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
12093 if (htab->strtab == NULL)
12095 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
12097 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
12098 ? (Elf_External_Sym_Shndx *) -1 : NULL);
12100 if (info->strip != strip_all || emit_relocs)
12102 file_ptr off = elf_next_file_pos (abfd);
12104 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
12106 /* Note that at this point elf_next_file_pos (abfd) is
12107 incorrect. We do not yet know the size of the .symtab section.
12108 We correct next_file_pos below, after we do know the size. */
12110 /* Start writing out the symbol table. The first symbol is always a
12112 elfsym.st_value = 0;
12113 elfsym.st_size = 0;
12114 elfsym.st_info = 0;
12115 elfsym.st_other = 0;
12116 elfsym.st_shndx = SHN_UNDEF;
12117 elfsym.st_target_internal = 0;
12118 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
12119 bfd_und_section_ptr, NULL) != 1)
12122 /* Output a symbol for each section. We output these even if we are
12123 discarding local symbols, since they are used for relocs. These
12124 symbols have no names. We store the index of each one in the
12125 index field of the section, so that we can find it again when
12126 outputting relocs. */
12128 elfsym.st_size = 0;
12129 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12130 elfsym.st_other = 0;
12131 elfsym.st_value = 0;
12132 elfsym.st_target_internal = 0;
12133 for (i = 1; i < elf_numsections (abfd); i++)
12135 o = bfd_section_from_elf_index (abfd, i);
12138 o->target_index = bfd_get_symcount (abfd);
12139 elfsym.st_shndx = i;
12140 if (!bfd_link_relocatable (info))
12141 elfsym.st_value = o->vma;
12142 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
12149 /* Allocate some memory to hold information read in from the input
12151 if (max_contents_size != 0)
12153 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
12154 if (flinfo.contents == NULL)
12158 if (max_external_reloc_size != 0)
12160 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
12161 if (flinfo.external_relocs == NULL)
12165 if (max_internal_reloc_count != 0)
12167 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
12168 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
12169 if (flinfo.internal_relocs == NULL)
12173 if (max_sym_count != 0)
12175 amt = max_sym_count * bed->s->sizeof_sym;
12176 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
12177 if (flinfo.external_syms == NULL)
12180 amt = max_sym_count * sizeof (Elf_Internal_Sym);
12181 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
12182 if (flinfo.internal_syms == NULL)
12185 amt = max_sym_count * sizeof (long);
12186 flinfo.indices = (long int *) bfd_malloc (amt);
12187 if (flinfo.indices == NULL)
12190 amt = max_sym_count * sizeof (asection *);
12191 flinfo.sections = (asection **) bfd_malloc (amt);
12192 if (flinfo.sections == NULL)
12196 if (max_sym_shndx_count != 0)
12198 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
12199 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
12200 if (flinfo.locsym_shndx == NULL)
12206 bfd_vma base, end = 0;
12209 for (sec = htab->tls_sec;
12210 sec && (sec->flags & SEC_THREAD_LOCAL);
12213 bfd_size_type size = sec->size;
12216 && (sec->flags & SEC_HAS_CONTENTS) == 0)
12218 struct bfd_link_order *ord = sec->map_tail.link_order;
12221 size = ord->offset + ord->size;
12223 end = sec->vma + size;
12225 base = htab->tls_sec->vma;
12226 /* Only align end of TLS section if static TLS doesn't have special
12227 alignment requirements. */
12228 if (bed->static_tls_alignment == 1)
12229 end = align_power (end, htab->tls_sec->alignment_power);
12230 htab->tls_size = end - base;
12233 /* Reorder SHF_LINK_ORDER sections. */
12234 for (o = abfd->sections; o != NULL; o = o->next)
12236 if (!elf_fixup_link_order (abfd, o))
12240 if (!_bfd_elf_fixup_eh_frame_hdr (info))
12243 /* Since ELF permits relocations to be against local symbols, we
12244 must have the local symbols available when we do the relocations.
12245 Since we would rather only read the local symbols once, and we
12246 would rather not keep them in memory, we handle all the
12247 relocations for a single input file at the same time.
12249 Unfortunately, there is no way to know the total number of local
12250 symbols until we have seen all of them, and the local symbol
12251 indices precede the global symbol indices. This means that when
12252 we are generating relocatable output, and we see a reloc against
12253 a global symbol, we can not know the symbol index until we have
12254 finished examining all the local symbols to see which ones we are
12255 going to output. To deal with this, we keep the relocations in
12256 memory, and don't output them until the end of the link. This is
12257 an unfortunate waste of memory, but I don't see a good way around
12258 it. Fortunately, it only happens when performing a relocatable
12259 link, which is not the common case. FIXME: If keep_memory is set
12260 we could write the relocs out and then read them again; I don't
12261 know how bad the memory loss will be. */
12263 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12264 sub->output_has_begun = FALSE;
12265 for (o = abfd->sections; o != NULL; o = o->next)
12267 for (p = o->map_head.link_order; p != NULL; p = p->next)
12269 if (p->type == bfd_indirect_link_order
12270 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
12271 == bfd_target_elf_flavour)
12272 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
12274 if (! sub->output_has_begun)
12276 if (! elf_link_input_bfd (&flinfo, sub))
12278 sub->output_has_begun = TRUE;
12281 else if (p->type == bfd_section_reloc_link_order
12282 || p->type == bfd_symbol_reloc_link_order)
12284 if (! elf_reloc_link_order (abfd, info, o, p))
12289 if (! _bfd_default_link_order (abfd, info, o, p))
12291 if (p->type == bfd_indirect_link_order
12292 && (bfd_get_flavour (sub)
12293 == bfd_target_elf_flavour)
12294 && (elf_elfheader (sub)->e_ident[EI_CLASS]
12295 != bed->s->elfclass))
12297 const char *iclass, *oclass;
12299 switch (bed->s->elfclass)
12301 case ELFCLASS64: oclass = "ELFCLASS64"; break;
12302 case ELFCLASS32: oclass = "ELFCLASS32"; break;
12303 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
12307 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
12309 case ELFCLASS64: iclass = "ELFCLASS64"; break;
12310 case ELFCLASS32: iclass = "ELFCLASS32"; break;
12311 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
12315 bfd_set_error (bfd_error_wrong_format);
12317 /* xgettext:c-format */
12318 (_("%pB: file class %s incompatible with %s"),
12319 sub, iclass, oclass);
12328 /* Free symbol buffer if needed. */
12329 if (!info->reduce_memory_overheads)
12331 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12332 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
12333 && elf_tdata (sub)->symbuf)
12335 free (elf_tdata (sub)->symbuf);
12336 elf_tdata (sub)->symbuf = NULL;
12340 /* Output any global symbols that got converted to local in a
12341 version script or due to symbol visibility. We do this in a
12342 separate step since ELF requires all local symbols to appear
12343 prior to any global symbols. FIXME: We should only do this if
12344 some global symbols were, in fact, converted to become local.
12345 FIXME: Will this work correctly with the Irix 5 linker? */
12346 eoinfo.failed = FALSE;
12347 eoinfo.flinfo = &flinfo;
12348 eoinfo.localsyms = TRUE;
12349 eoinfo.file_sym_done = FALSE;
12350 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12354 /* If backend needs to output some local symbols not present in the hash
12355 table, do it now. */
12356 if (bed->elf_backend_output_arch_local_syms
12357 && (info->strip != strip_all || emit_relocs))
12359 typedef int (*out_sym_func)
12360 (void *, const char *, Elf_Internal_Sym *, asection *,
12361 struct elf_link_hash_entry *);
12363 if (! ((*bed->elf_backend_output_arch_local_syms)
12364 (abfd, info, &flinfo,
12365 (out_sym_func) elf_link_output_symstrtab)))
12369 /* That wrote out all the local symbols. Finish up the symbol table
12370 with the global symbols. Even if we want to strip everything we
12371 can, we still need to deal with those global symbols that got
12372 converted to local in a version script. */
12374 /* The sh_info field records the index of the first non local symbol. */
12375 symtab_hdr->sh_info = bfd_get_symcount (abfd);
12378 && htab->dynsym != NULL
12379 && htab->dynsym->output_section != bfd_abs_section_ptr)
12381 Elf_Internal_Sym sym;
12382 bfd_byte *dynsym = htab->dynsym->contents;
12384 o = htab->dynsym->output_section;
12385 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
12387 /* Write out the section symbols for the output sections. */
12388 if (bfd_link_pic (info)
12389 || htab->is_relocatable_executable)
12395 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
12397 sym.st_target_internal = 0;
12399 for (s = abfd->sections; s != NULL; s = s->next)
12405 dynindx = elf_section_data (s)->dynindx;
12408 indx = elf_section_data (s)->this_idx;
12409 BFD_ASSERT (indx > 0);
12410 sym.st_shndx = indx;
12411 if (! check_dynsym (abfd, &sym))
12413 sym.st_value = s->vma;
12414 dest = dynsym + dynindx * bed->s->sizeof_sym;
12415 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12419 /* Write out the local dynsyms. */
12420 if (htab->dynlocal)
12422 struct elf_link_local_dynamic_entry *e;
12423 for (e = htab->dynlocal; e ; e = e->next)
12428 /* Copy the internal symbol and turn off visibility.
12429 Note that we saved a word of storage and overwrote
12430 the original st_name with the dynstr_index. */
12432 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
12434 s = bfd_section_from_elf_index (e->input_bfd,
12439 elf_section_data (s->output_section)->this_idx;
12440 if (! check_dynsym (abfd, &sym))
12442 sym.st_value = (s->output_section->vma
12444 + e->isym.st_value);
12447 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
12448 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
12453 /* We get the global symbols from the hash table. */
12454 eoinfo.failed = FALSE;
12455 eoinfo.localsyms = FALSE;
12456 eoinfo.flinfo = &flinfo;
12457 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
12461 /* If backend needs to output some symbols not present in the hash
12462 table, do it now. */
12463 if (bed->elf_backend_output_arch_syms
12464 && (info->strip != strip_all || emit_relocs))
12466 typedef int (*out_sym_func)
12467 (void *, const char *, Elf_Internal_Sym *, asection *,
12468 struct elf_link_hash_entry *);
12470 if (! ((*bed->elf_backend_output_arch_syms)
12471 (abfd, info, &flinfo,
12472 (out_sym_func) elf_link_output_symstrtab)))
12476 /* Finalize the .strtab section. */
12477 _bfd_elf_strtab_finalize (flinfo.symstrtab);
12479 /* Swap out the .strtab section. */
12480 if (!elf_link_swap_symbols_out (&flinfo))
12483 /* Now we know the size of the symtab section. */
12484 if (bfd_get_symcount (abfd) > 0)
12486 /* Finish up and write out the symbol string table (.strtab)
12488 Elf_Internal_Shdr *symstrtab_hdr = NULL;
12489 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
12491 if (elf_symtab_shndx_list (abfd))
12493 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
12495 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
12497 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
12498 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
12499 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
12500 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
12501 symtab_shndx_hdr->sh_size = amt;
12503 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
12506 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
12507 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
12512 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
12513 /* sh_name was set in prep_headers. */
12514 symstrtab_hdr->sh_type = SHT_STRTAB;
12515 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
12516 symstrtab_hdr->sh_addr = 0;
12517 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
12518 symstrtab_hdr->sh_entsize = 0;
12519 symstrtab_hdr->sh_link = 0;
12520 symstrtab_hdr->sh_info = 0;
12521 /* sh_offset is set just below. */
12522 symstrtab_hdr->sh_addralign = 1;
12524 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
12526 elf_next_file_pos (abfd) = off;
12528 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
12529 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
12533 if (info->out_implib_bfd && !elf_output_implib (abfd, info))
12535 _bfd_error_handler (_("%pB: failed to generate import library"),
12536 info->out_implib_bfd);
12540 /* Adjust the relocs to have the correct symbol indices. */
12541 for (o = abfd->sections; o != NULL; o = o->next)
12543 struct bfd_elf_section_data *esdo = elf_section_data (o);
12546 if ((o->flags & SEC_RELOC) == 0)
12549 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
12550 if (esdo->rel.hdr != NULL
12551 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
12553 if (esdo->rela.hdr != NULL
12554 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
12557 /* Set the reloc_count field to 0 to prevent write_relocs from
12558 trying to swap the relocs out itself. */
12559 o->reloc_count = 0;
12562 if (dynamic && info->combreloc && dynobj != NULL)
12563 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
12565 /* If we are linking against a dynamic object, or generating a
12566 shared library, finish up the dynamic linking information. */
12569 bfd_byte *dyncon, *dynconend;
12571 /* Fix up .dynamic entries. */
12572 o = bfd_get_linker_section (dynobj, ".dynamic");
12573 BFD_ASSERT (o != NULL);
12575 dyncon = o->contents;
12576 dynconend = o->contents + o->size;
12577 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12579 Elf_Internal_Dyn dyn;
12582 bfd_size_type sh_size;
12585 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12592 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
12594 switch (elf_section_data (reldyn)->this_hdr.sh_type)
12596 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
12597 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
12600 dyn.d_un.d_val = relativecount;
12607 name = info->init_function;
12610 name = info->fini_function;
12613 struct elf_link_hash_entry *h;
12615 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
12617 && (h->root.type == bfd_link_hash_defined
12618 || h->root.type == bfd_link_hash_defweak))
12620 dyn.d_un.d_ptr = h->root.u.def.value;
12621 o = h->root.u.def.section;
12622 if (o->output_section != NULL)
12623 dyn.d_un.d_ptr += (o->output_section->vma
12624 + o->output_offset);
12627 /* The symbol is imported from another shared
12628 library and does not apply to this one. */
12629 dyn.d_un.d_ptr = 0;
12636 case DT_PREINIT_ARRAYSZ:
12637 name = ".preinit_array";
12639 case DT_INIT_ARRAYSZ:
12640 name = ".init_array";
12642 case DT_FINI_ARRAYSZ:
12643 name = ".fini_array";
12645 o = bfd_get_section_by_name (abfd, name);
12649 (_("could not find section %s"), name);
12654 (_("warning: %s section has zero size"), name);
12655 dyn.d_un.d_val = o->size;
12658 case DT_PREINIT_ARRAY:
12659 name = ".preinit_array";
12661 case DT_INIT_ARRAY:
12662 name = ".init_array";
12664 case DT_FINI_ARRAY:
12665 name = ".fini_array";
12667 o = bfd_get_section_by_name (abfd, name);
12674 name = ".gnu.hash";
12683 name = ".gnu.version_d";
12686 name = ".gnu.version_r";
12689 name = ".gnu.version";
12691 o = bfd_get_linker_section (dynobj, name);
12693 if (o == NULL || bfd_is_abs_section (o->output_section))
12696 (_("could not find section %s"), name);
12699 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
12702 (_("warning: section '%s' is being made into a note"), name);
12703 bfd_set_error (bfd_error_nonrepresentable_section);
12706 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
12713 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
12719 for (i = 1; i < elf_numsections (abfd); i++)
12721 Elf_Internal_Shdr *hdr;
12723 hdr = elf_elfsections (abfd)[i];
12724 if (hdr->sh_type == type
12725 && (hdr->sh_flags & SHF_ALLOC) != 0)
12727 sh_size += hdr->sh_size;
12729 || sh_addr > hdr->sh_addr)
12730 sh_addr = hdr->sh_addr;
12734 if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
12736 /* Don't count procedure linkage table relocs in the
12737 overall reloc count. */
12738 sh_size -= htab->srelplt->size;
12740 /* If the size is zero, make the address zero too.
12741 This is to avoid a glibc bug. If the backend
12742 emits DT_RELA/DT_RELASZ even when DT_RELASZ is
12743 zero, then we'll put DT_RELA at the end of
12744 DT_JMPREL. glibc will interpret the end of
12745 DT_RELA matching the end of DT_JMPREL as the
12746 case where DT_RELA includes DT_JMPREL, and for
12747 LD_BIND_NOW will decide that processing DT_RELA
12748 will process the PLT relocs too. Net result:
12749 No PLT relocs applied. */
12752 /* If .rela.plt is the first .rela section, exclude
12753 it from DT_RELA. */
12754 else if (sh_addr == (htab->srelplt->output_section->vma
12755 + htab->srelplt->output_offset))
12756 sh_addr += htab->srelplt->size;
12759 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
12760 dyn.d_un.d_val = sh_size;
12762 dyn.d_un.d_ptr = sh_addr;
12765 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
12769 /* If we have created any dynamic sections, then output them. */
12770 if (dynobj != NULL)
12772 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
12775 /* Check for DT_TEXTREL (late, in case the backend removes it). */
12776 if (((info->warn_shared_textrel && bfd_link_pic (info))
12777 || info->error_textrel)
12778 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
12780 bfd_byte *dyncon, *dynconend;
12782 dyncon = o->contents;
12783 dynconend = o->contents + o->size;
12784 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
12786 Elf_Internal_Dyn dyn;
12788 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
12790 if (dyn.d_tag == DT_TEXTREL)
12792 if (info->error_textrel)
12793 info->callbacks->einfo
12794 (_("%P%X: read-only segment has dynamic relocations\n"));
12796 info->callbacks->einfo
12797 (_("%P: warning: creating a DT_TEXTREL in a shared object\n"));
12803 for (o = dynobj->sections; o != NULL; o = o->next)
12805 if ((o->flags & SEC_HAS_CONTENTS) == 0
12807 || o->output_section == bfd_abs_section_ptr)
12809 if ((o->flags & SEC_LINKER_CREATED) == 0)
12811 /* At this point, we are only interested in sections
12812 created by _bfd_elf_link_create_dynamic_sections. */
12815 if (htab->stab_info.stabstr == o)
12817 if (htab->eh_info.hdr_sec == o)
12819 if (strcmp (o->name, ".dynstr") != 0)
12821 if (! bfd_set_section_contents (abfd, o->output_section,
12823 (file_ptr) o->output_offset
12824 * bfd_octets_per_byte (abfd),
12830 /* The contents of the .dynstr section are actually in a
12834 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
12835 if (bfd_seek (abfd, off, SEEK_SET) != 0
12836 || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
12842 if (!info->resolve_section_groups)
12844 bfd_boolean failed = FALSE;
12846 BFD_ASSERT (bfd_link_relocatable (info));
12847 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
12852 /* If we have optimized stabs strings, output them. */
12853 if (htab->stab_info.stabstr != NULL)
12855 if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
12859 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
12862 if (info->callbacks->emit_ctf)
12863 info->callbacks->emit_ctf ();
12865 elf_final_link_free (abfd, &flinfo);
12869 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
12870 if (contents == NULL)
12871 return FALSE; /* Bail out and fail. */
12872 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
12873 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
12880 elf_final_link_free (abfd, &flinfo);
12884 /* Initialize COOKIE for input bfd ABFD. */
12887 init_reloc_cookie (struct elf_reloc_cookie *cookie,
12888 struct bfd_link_info *info, bfd *abfd)
12890 Elf_Internal_Shdr *symtab_hdr;
12891 const struct elf_backend_data *bed;
12893 bed = get_elf_backend_data (abfd);
12894 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12896 cookie->abfd = abfd;
12897 cookie->sym_hashes = elf_sym_hashes (abfd);
12898 cookie->bad_symtab = elf_bad_symtab (abfd);
12899 if (cookie->bad_symtab)
12901 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12902 cookie->extsymoff = 0;
12906 cookie->locsymcount = symtab_hdr->sh_info;
12907 cookie->extsymoff = symtab_hdr->sh_info;
12910 if (bed->s->arch_size == 32)
12911 cookie->r_sym_shift = 8;
12913 cookie->r_sym_shift = 32;
12915 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
12916 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
12918 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
12919 cookie->locsymcount, 0,
12921 if (cookie->locsyms == NULL)
12923 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
12926 if (info->keep_memory)
12927 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
12932 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12935 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12937 Elf_Internal_Shdr *symtab_hdr;
12939 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12940 if (cookie->locsyms != NULL
12941 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12942 free (cookie->locsyms);
12945 /* Initialize the relocation information in COOKIE for input section SEC
12946 of input bfd ABFD. */
12949 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12950 struct bfd_link_info *info, bfd *abfd,
12953 if (sec->reloc_count == 0)
12955 cookie->rels = NULL;
12956 cookie->relend = NULL;
12960 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12961 info->keep_memory);
12962 if (cookie->rels == NULL)
12964 cookie->rel = cookie->rels;
12965 cookie->relend = cookie->rels + sec->reloc_count;
12967 cookie->rel = cookie->rels;
12971 /* Free the memory allocated by init_reloc_cookie_rels,
12975 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12978 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12979 free (cookie->rels);
12982 /* Initialize the whole of COOKIE for input section SEC. */
12985 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12986 struct bfd_link_info *info,
12989 if (!init_reloc_cookie (cookie, info, sec->owner))
12991 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12996 fini_reloc_cookie (cookie, sec->owner);
13001 /* Free the memory allocated by init_reloc_cookie_for_section,
13005 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
13008 fini_reloc_cookie_rels (cookie, sec);
13009 fini_reloc_cookie (cookie, sec->owner);
13012 /* Garbage collect unused sections. */
13014 /* Default gc_mark_hook. */
13017 _bfd_elf_gc_mark_hook (asection *sec,
13018 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13019 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13020 struct elf_link_hash_entry *h,
13021 Elf_Internal_Sym *sym)
13025 switch (h->root.type)
13027 case bfd_link_hash_defined:
13028 case bfd_link_hash_defweak:
13029 return h->root.u.def.section;
13031 case bfd_link_hash_common:
13032 return h->root.u.c.p->section;
13039 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
13044 /* Return the debug definition section. */
13047 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
13048 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13049 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
13050 struct elf_link_hash_entry *h,
13051 Elf_Internal_Sym *sym)
13055 /* Return the global debug definition section. */
13056 if ((h->root.type == bfd_link_hash_defined
13057 || h->root.type == bfd_link_hash_defweak)
13058 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
13059 return h->root.u.def.section;
13063 /* Return the local debug definition section. */
13064 asection *isec = bfd_section_from_elf_index (sec->owner,
13066 if ((isec->flags & SEC_DEBUGGING) != 0)
13073 /* COOKIE->rel describes a relocation against section SEC, which is
13074 a section we've decided to keep. Return the section that contains
13075 the relocation symbol, or NULL if no section contains it. */
13078 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
13079 elf_gc_mark_hook_fn gc_mark_hook,
13080 struct elf_reloc_cookie *cookie,
13081 bfd_boolean *start_stop)
13083 unsigned long r_symndx;
13084 struct elf_link_hash_entry *h;
13086 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
13087 if (r_symndx == STN_UNDEF)
13090 if (r_symndx >= cookie->locsymcount
13091 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13093 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
13096 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
13100 while (h->root.type == bfd_link_hash_indirect
13101 || h->root.type == bfd_link_hash_warning)
13102 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13104 /* If this symbol is weak and there is a non-weak definition, we
13105 keep the non-weak definition because many backends put
13106 dynamic reloc info on the non-weak definition for code
13107 handling copy relocs. */
13108 if (h->is_weakalias)
13109 weakdef (h)->mark = 1;
13111 if (start_stop != NULL)
13113 /* To work around a glibc bug, mark XXX input sections
13114 when there is a reference to __start_XXX or __stop_XXX
13118 asection *s = h->u2.start_stop_section;
13119 *start_stop = !s->gc_mark;
13124 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
13127 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
13128 &cookie->locsyms[r_symndx]);
13131 /* COOKIE->rel describes a relocation against section SEC, which is
13132 a section we've decided to keep. Mark the section that contains
13133 the relocation symbol. */
13136 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
13138 elf_gc_mark_hook_fn gc_mark_hook,
13139 struct elf_reloc_cookie *cookie)
13142 bfd_boolean start_stop = FALSE;
13144 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
13145 while (rsec != NULL)
13147 if (!rsec->gc_mark)
13149 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
13150 || (rsec->owner->flags & DYNAMIC) != 0)
13152 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
13157 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
13162 /* The mark phase of garbage collection. For a given section, mark
13163 it and any sections in this section's group, and all the sections
13164 which define symbols to which it refers. */
13167 _bfd_elf_gc_mark (struct bfd_link_info *info,
13169 elf_gc_mark_hook_fn gc_mark_hook)
13172 asection *group_sec, *eh_frame;
13176 /* Mark all the sections in the group. */
13177 group_sec = elf_section_data (sec)->next_in_group;
13178 if (group_sec && !group_sec->gc_mark)
13179 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
13182 /* Look through the section relocs. */
13184 eh_frame = elf_eh_frame_section (sec->owner);
13185 if ((sec->flags & SEC_RELOC) != 0
13186 && sec->reloc_count > 0
13187 && sec != eh_frame)
13189 struct elf_reloc_cookie cookie;
13191 if (!init_reloc_cookie_for_section (&cookie, info, sec))
13195 for (; cookie.rel < cookie.relend; cookie.rel++)
13196 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
13201 fini_reloc_cookie_for_section (&cookie, sec);
13205 if (ret && eh_frame && elf_fde_list (sec))
13207 struct elf_reloc_cookie cookie;
13209 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
13213 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
13214 gc_mark_hook, &cookie))
13216 fini_reloc_cookie_for_section (&cookie, eh_frame);
13220 eh_frame = elf_section_eh_frame_entry (sec);
13221 if (ret && eh_frame && !eh_frame->gc_mark)
13222 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
13228 /* Scan and mark sections in a special or debug section group. */
13231 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
13233 /* Point to first section of section group. */
13235 /* Used to iterate the section group. */
13238 bfd_boolean is_special_grp = TRUE;
13239 bfd_boolean is_debug_grp = TRUE;
13241 /* First scan to see if group contains any section other than debug
13242 and special section. */
13243 ssec = msec = elf_next_in_group (grp);
13246 if ((msec->flags & SEC_DEBUGGING) == 0)
13247 is_debug_grp = FALSE;
13249 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
13250 is_special_grp = FALSE;
13252 msec = elf_next_in_group (msec);
13254 while (msec != ssec);
13256 /* If this is a pure debug section group or pure special section group,
13257 keep all sections in this group. */
13258 if (is_debug_grp || is_special_grp)
13263 msec = elf_next_in_group (msec);
13265 while (msec != ssec);
13269 /* Keep debug and special sections. */
13272 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
13273 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
13277 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13280 bfd_boolean some_kept;
13281 bfd_boolean debug_frag_seen;
13282 bfd_boolean has_kept_debug_info;
13284 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13286 isec = ibfd->sections;
13287 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13290 /* Ensure all linker created sections are kept,
13291 see if any other section is already marked,
13292 and note if we have any fragmented debug sections. */
13293 debug_frag_seen = some_kept = has_kept_debug_info = FALSE;
13294 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13296 if ((isec->flags & SEC_LINKER_CREATED) != 0)
13298 else if (isec->gc_mark
13299 && (isec->flags & SEC_ALLOC) != 0
13300 && elf_section_type (isec) != SHT_NOTE)
13303 if (!debug_frag_seen
13304 && (isec->flags & SEC_DEBUGGING)
13305 && CONST_STRNEQ (isec->name, ".debug_line."))
13306 debug_frag_seen = TRUE;
13309 /* If no non-note alloc section in this file will be kept, then
13310 we can toss out the debug and special sections. */
13314 /* Keep debug and special sections like .comment when they are
13315 not part of a group. Also keep section groups that contain
13316 just debug sections or special sections. */
13317 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13319 if ((isec->flags & SEC_GROUP) != 0)
13320 _bfd_elf_gc_mark_debug_special_section_group (isec);
13321 else if (((isec->flags & SEC_DEBUGGING) != 0
13322 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
13323 && elf_next_in_group (isec) == NULL)
13325 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
13326 has_kept_debug_info = TRUE;
13329 /* Look for CODE sections which are going to be discarded,
13330 and find and discard any fragmented debug sections which
13331 are associated with that code section. */
13332 if (debug_frag_seen)
13333 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13334 if ((isec->flags & SEC_CODE) != 0
13335 && isec->gc_mark == 0)
13340 ilen = strlen (isec->name);
13342 /* Association is determined by the name of the debug
13343 section containing the name of the code section as
13344 a suffix. For example .debug_line.text.foo is a
13345 debug section associated with .text.foo. */
13346 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
13350 if (dsec->gc_mark == 0
13351 || (dsec->flags & SEC_DEBUGGING) == 0)
13354 dlen = strlen (dsec->name);
13357 && strncmp (dsec->name + (dlen - ilen),
13358 isec->name, ilen) == 0)
13363 /* Mark debug sections referenced by kept debug sections. */
13364 if (has_kept_debug_info)
13365 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
13367 && (isec->flags & SEC_DEBUGGING) != 0)
13368 if (!_bfd_elf_gc_mark (info, isec,
13369 elf_gc_mark_debug_section))
13376 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
13379 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13381 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13385 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13386 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
13387 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13390 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13393 for (o = sub->sections; o != NULL; o = o->next)
13395 /* When any section in a section group is kept, we keep all
13396 sections in the section group. If the first member of
13397 the section group is excluded, we will also exclude the
13399 if (o->flags & SEC_GROUP)
13401 asection *first = elf_next_in_group (o);
13402 o->gc_mark = first->gc_mark;
13408 /* Skip sweeping sections already excluded. */
13409 if (o->flags & SEC_EXCLUDE)
13412 /* Since this is early in the link process, it is simple
13413 to remove a section from the output. */
13414 o->flags |= SEC_EXCLUDE;
13416 if (info->print_gc_sections && o->size != 0)
13417 /* xgettext:c-format */
13418 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
13426 /* Propagate collected vtable information. This is called through
13427 elf_link_hash_traverse. */
13430 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
13432 /* Those that are not vtables. */
13434 || h->u2.vtable == NULL
13435 || h->u2.vtable->parent == NULL)
13438 /* Those vtables that do not have parents, we cannot merge. */
13439 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
13442 /* If we've already been done, exit. */
13443 if (h->u2.vtable->used && h->u2.vtable->used[-1])
13446 /* Make sure the parent's table is up to date. */
13447 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
13449 if (h->u2.vtable->used == NULL)
13451 /* None of this table's entries were referenced. Re-use the
13453 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
13454 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
13459 bfd_boolean *cu, *pu;
13461 /* Or the parent's entries into ours. */
13462 cu = h->u2.vtable->used;
13464 pu = h->u2.vtable->parent->u2.vtable->used;
13467 const struct elf_backend_data *bed;
13468 unsigned int log_file_align;
13470 bed = get_elf_backend_data (h->root.u.def.section->owner);
13471 log_file_align = bed->s->log_file_align;
13472 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
13487 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
13490 bfd_vma hstart, hend;
13491 Elf_Internal_Rela *relstart, *relend, *rel;
13492 const struct elf_backend_data *bed;
13493 unsigned int log_file_align;
13495 /* Take care of both those symbols that do not describe vtables as
13496 well as those that are not loaded. */
13498 || h->u2.vtable == NULL
13499 || h->u2.vtable->parent == NULL)
13502 BFD_ASSERT (h->root.type == bfd_link_hash_defined
13503 || h->root.type == bfd_link_hash_defweak);
13505 sec = h->root.u.def.section;
13506 hstart = h->root.u.def.value;
13507 hend = hstart + h->size;
13509 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
13511 return *(bfd_boolean *) okp = FALSE;
13512 bed = get_elf_backend_data (sec->owner);
13513 log_file_align = bed->s->log_file_align;
13515 relend = relstart + sec->reloc_count;
13517 for (rel = relstart; rel < relend; ++rel)
13518 if (rel->r_offset >= hstart && rel->r_offset < hend)
13520 /* If the entry is in use, do nothing. */
13521 if (h->u2.vtable->used
13522 && (rel->r_offset - hstart) < h->u2.vtable->size)
13524 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
13525 if (h->u2.vtable->used[entry])
13528 /* Otherwise, kill it. */
13529 rel->r_offset = rel->r_info = rel->r_addend = 0;
13535 /* Mark sections containing dynamically referenced symbols. When
13536 building shared libraries, we must assume that any visible symbol is
13540 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
13542 struct bfd_link_info *info = (struct bfd_link_info *) inf;
13543 struct bfd_elf_dynamic_list *d = info->dynamic_list;
13545 if ((h->root.type == bfd_link_hash_defined
13546 || h->root.type == bfd_link_hash_defweak)
13547 && ((h->ref_dynamic && !h->forced_local)
13548 || ((h->def_regular || ELF_COMMON_DEF_P (h))
13549 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
13550 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
13551 && (!bfd_link_executable (info)
13552 || info->gc_keep_exported
13553 || info->export_dynamic
13556 && (*d->match) (&d->head, NULL, h->root.root.string)))
13557 && (h->versioned >= versioned
13558 || !bfd_hide_sym_by_version (info->version_info,
13559 h->root.root.string)))))
13560 h->root.u.def.section->flags |= SEC_KEEP;
13565 /* Keep all sections containing symbols undefined on the command-line,
13566 and the section containing the entry symbol. */
13569 _bfd_elf_gc_keep (struct bfd_link_info *info)
13571 struct bfd_sym_chain *sym;
13573 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
13575 struct elf_link_hash_entry *h;
13577 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
13578 FALSE, FALSE, FALSE);
13581 && (h->root.type == bfd_link_hash_defined
13582 || h->root.type == bfd_link_hash_defweak)
13583 && !bfd_is_abs_section (h->root.u.def.section)
13584 && !bfd_is_und_section (h->root.u.def.section))
13585 h->root.u.def.section->flags |= SEC_KEEP;
13590 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
13591 struct bfd_link_info *info)
13593 bfd *ibfd = info->input_bfds;
13595 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13598 struct elf_reloc_cookie cookie;
13600 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
13602 sec = ibfd->sections;
13603 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13606 if (!init_reloc_cookie (&cookie, info, ibfd))
13609 for (sec = ibfd->sections; sec; sec = sec->next)
13611 if (CONST_STRNEQ (bfd_section_name (sec), ".eh_frame_entry")
13612 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
13614 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
13615 fini_reloc_cookie_rels (&cookie, sec);
13622 /* Do mark and sweep of unused sections. */
13625 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
13627 bfd_boolean ok = TRUE;
13629 elf_gc_mark_hook_fn gc_mark_hook;
13630 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13631 struct elf_link_hash_table *htab;
13633 if (!bed->can_gc_sections
13634 || !is_elf_hash_table (info->hash))
13636 _bfd_error_handler(_("warning: gc-sections option ignored"));
13640 bed->gc_keep (info);
13641 htab = elf_hash_table (info);
13643 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
13644 at the .eh_frame section if we can mark the FDEs individually. */
13645 for (sub = info->input_bfds;
13646 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
13647 sub = sub->link.next)
13650 struct elf_reloc_cookie cookie;
13652 sec = sub->sections;
13653 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13655 sec = bfd_get_section_by_name (sub, ".eh_frame");
13656 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
13658 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
13659 if (elf_section_data (sec)->sec_info
13660 && (sec->flags & SEC_LINKER_CREATED) == 0)
13661 elf_eh_frame_section (sub) = sec;
13662 fini_reloc_cookie_for_section (&cookie, sec);
13663 sec = bfd_get_next_section_by_name (NULL, sec);
13667 /* Apply transitive closure to the vtable entry usage info. */
13668 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
13672 /* Kill the vtable relocations that were not used. */
13673 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
13677 /* Mark dynamically referenced symbols. */
13678 if (htab->dynamic_sections_created || info->gc_keep_exported)
13679 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
13681 /* Grovel through relocs to find out who stays ... */
13682 gc_mark_hook = bed->gc_mark_hook;
13683 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
13687 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
13688 || elf_object_id (sub) != elf_hash_table_id (htab)
13689 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
13693 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
13696 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
13697 Also treat note sections as a root, if the section is not part
13698 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
13699 well as FINI_ARRAY sections for ld -r. */
13700 for (o = sub->sections; o != NULL; o = o->next)
13702 && (o->flags & SEC_EXCLUDE) == 0
13703 && ((o->flags & SEC_KEEP) != 0
13704 || (bfd_link_relocatable (info)
13705 && ((elf_section_data (o)->this_hdr.sh_type
13706 == SHT_PREINIT_ARRAY)
13707 || (elf_section_data (o)->this_hdr.sh_type
13709 || (elf_section_data (o)->this_hdr.sh_type
13710 == SHT_FINI_ARRAY)))
13711 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
13712 && elf_next_in_group (o) == NULL )))
13714 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
13719 /* Allow the backend to mark additional target specific sections. */
13720 bed->gc_mark_extra_sections (info, gc_mark_hook);
13722 /* ... and mark SEC_EXCLUDE for those that go. */
13723 return elf_gc_sweep (abfd, info);
13726 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
13729 bfd_elf_gc_record_vtinherit (bfd *abfd,
13731 struct elf_link_hash_entry *h,
13734 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13735 struct elf_link_hash_entry **search, *child;
13736 size_t extsymcount;
13737 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13739 /* The sh_info field of the symtab header tells us where the
13740 external symbols start. We don't care about the local symbols at
13742 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
13743 if (!elf_bad_symtab (abfd))
13744 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
13746 sym_hashes = elf_sym_hashes (abfd);
13747 sym_hashes_end = sym_hashes + extsymcount;
13749 /* Hunt down the child symbol, which is in this section at the same
13750 offset as the relocation. */
13751 for (search = sym_hashes; search != sym_hashes_end; ++search)
13753 if ((child = *search) != NULL
13754 && (child->root.type == bfd_link_hash_defined
13755 || child->root.type == bfd_link_hash_defweak)
13756 && child->root.u.def.section == sec
13757 && child->root.u.def.value == offset)
13761 /* xgettext:c-format */
13762 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
13763 abfd, sec, (uint64_t) offset);
13764 bfd_set_error (bfd_error_invalid_operation);
13768 if (!child->u2.vtable)
13770 child->u2.vtable = ((struct elf_link_virtual_table_entry *)
13771 bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
13772 if (!child->u2.vtable)
13777 /* This *should* only be the absolute section. It could potentially
13778 be that someone has defined a non-global vtable though, which
13779 would be bad. It isn't worth paging in the local symbols to be
13780 sure though; that case should simply be handled by the assembler. */
13782 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
13785 child->u2.vtable->parent = h;
13790 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
13793 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
13794 struct elf_link_hash_entry *h,
13797 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13798 unsigned int log_file_align = bed->s->log_file_align;
13802 /* xgettext:c-format */
13803 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
13805 bfd_set_error (bfd_error_bad_value);
13811 h->u2.vtable = ((struct elf_link_virtual_table_entry *)
13812 bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
13817 if (addend >= h->u2.vtable->size)
13819 size_t size, bytes, file_align;
13820 bfd_boolean *ptr = h->u2.vtable->used;
13822 /* While the symbol is undefined, we have to be prepared to handle
13824 file_align = 1 << log_file_align;
13825 if (h->root.type == bfd_link_hash_undefined)
13826 size = addend + file_align;
13830 if (addend >= size)
13832 /* Oops! We've got a reference past the defined end of
13833 the table. This is probably a bug -- shall we warn? */
13834 size = addend + file_align;
13837 size = (size + file_align - 1) & -file_align;
13839 /* Allocate one extra entry for use as a "done" flag for the
13840 consolidation pass. */
13841 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
13845 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
13851 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
13852 * sizeof (bfd_boolean));
13853 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
13857 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
13862 /* And arrange for that done flag to be at index -1. */
13863 h->u2.vtable->used = ptr + 1;
13864 h->u2.vtable->size = size;
13867 h->u2.vtable->used[addend >> log_file_align] = TRUE;
13872 /* Map an ELF section header flag to its corresponding string. */
13876 flagword flag_value;
13877 } elf_flags_to_name_table;
13879 static elf_flags_to_name_table elf_flags_to_names [] =
13881 { "SHF_WRITE", SHF_WRITE },
13882 { "SHF_ALLOC", SHF_ALLOC },
13883 { "SHF_EXECINSTR", SHF_EXECINSTR },
13884 { "SHF_MERGE", SHF_MERGE },
13885 { "SHF_STRINGS", SHF_STRINGS },
13886 { "SHF_INFO_LINK", SHF_INFO_LINK},
13887 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
13888 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
13889 { "SHF_GROUP", SHF_GROUP },
13890 { "SHF_TLS", SHF_TLS },
13891 { "SHF_MASKOS", SHF_MASKOS },
13892 { "SHF_EXCLUDE", SHF_EXCLUDE },
13895 /* Returns TRUE if the section is to be included, otherwise FALSE. */
13897 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
13898 struct flag_info *flaginfo,
13901 const bfd_vma sh_flags = elf_section_flags (section);
13903 if (!flaginfo->flags_initialized)
13905 bfd *obfd = info->output_bfd;
13906 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13907 struct flag_info_list *tf = flaginfo->flag_list;
13909 int without_hex = 0;
13911 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
13914 flagword (*lookup) (char *);
13916 lookup = bed->elf_backend_lookup_section_flags_hook;
13917 if (lookup != NULL)
13919 flagword hexval = (*lookup) ((char *) tf->name);
13923 if (tf->with == with_flags)
13924 with_hex |= hexval;
13925 else if (tf->with == without_flags)
13926 without_hex |= hexval;
13931 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13933 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13935 if (tf->with == with_flags)
13936 with_hex |= elf_flags_to_names[i].flag_value;
13937 else if (tf->with == without_flags)
13938 without_hex |= elf_flags_to_names[i].flag_value;
13945 info->callbacks->einfo
13946 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13950 flaginfo->flags_initialized = TRUE;
13951 flaginfo->only_with_flags |= with_hex;
13952 flaginfo->not_with_flags |= without_hex;
13955 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13958 if ((flaginfo->not_with_flags & sh_flags) != 0)
13964 struct alloc_got_off_arg {
13966 struct bfd_link_info *info;
13969 /* We need a special top-level link routine to convert got reference counts
13970 to real got offsets. */
13973 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13975 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13976 bfd *obfd = gofarg->info->output_bfd;
13977 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13979 if (h->got.refcount > 0)
13981 h->got.offset = gofarg->gotoff;
13982 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13985 h->got.offset = (bfd_vma) -1;
13990 /* And an accompanying bit to work out final got entry offsets once
13991 we're done. Should be called from final_link. */
13994 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13995 struct bfd_link_info *info)
13998 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14000 struct alloc_got_off_arg gofarg;
14002 BFD_ASSERT (abfd == info->output_bfd);
14004 if (! is_elf_hash_table (info->hash))
14007 /* The GOT offset is relative to the .got section, but the GOT header is
14008 put into the .got.plt section, if the backend uses it. */
14009 if (bed->want_got_plt)
14012 gotoff = bed->got_header_size;
14014 /* Do the local .got entries first. */
14015 for (i = info->input_bfds; i; i = i->link.next)
14017 bfd_signed_vma *local_got;
14018 size_t j, locsymcount;
14019 Elf_Internal_Shdr *symtab_hdr;
14021 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
14024 local_got = elf_local_got_refcounts (i);
14028 symtab_hdr = &elf_tdata (i)->symtab_hdr;
14029 if (elf_bad_symtab (i))
14030 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
14032 locsymcount = symtab_hdr->sh_info;
14034 for (j = 0; j < locsymcount; ++j)
14036 if (local_got[j] > 0)
14038 local_got[j] = gotoff;
14039 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
14042 local_got[j] = (bfd_vma) -1;
14046 /* Then the global .got entries. .plt refcounts are handled by
14047 adjust_dynamic_symbol */
14048 gofarg.gotoff = gotoff;
14049 gofarg.info = info;
14050 elf_link_hash_traverse (elf_hash_table (info),
14051 elf_gc_allocate_got_offsets,
14056 /* Many folk need no more in the way of final link than this, once
14057 got entry reference counting is enabled. */
14060 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
14062 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
14065 /* Invoke the regular ELF backend linker to do all the work. */
14066 return bfd_elf_final_link (abfd, info);
14070 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
14072 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
14074 if (rcookie->bad_symtab)
14075 rcookie->rel = rcookie->rels;
14077 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
14079 unsigned long r_symndx;
14081 if (! rcookie->bad_symtab)
14082 if (rcookie->rel->r_offset > offset)
14084 if (rcookie->rel->r_offset != offset)
14087 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
14088 if (r_symndx == STN_UNDEF)
14091 if (r_symndx >= rcookie->locsymcount
14092 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
14094 struct elf_link_hash_entry *h;
14096 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
14098 while (h->root.type == bfd_link_hash_indirect
14099 || h->root.type == bfd_link_hash_warning)
14100 h = (struct elf_link_hash_entry *) h->root.u.i.link;
14102 if ((h->root.type == bfd_link_hash_defined
14103 || h->root.type == bfd_link_hash_defweak)
14104 && (h->root.u.def.section->owner != rcookie->abfd
14105 || h->root.u.def.section->kept_section != NULL
14106 || discarded_section (h->root.u.def.section)))
14111 /* It's not a relocation against a global symbol,
14112 but it could be a relocation against a local
14113 symbol for a discarded section. */
14115 Elf_Internal_Sym *isym;
14117 /* Need to: get the symbol; get the section. */
14118 isym = &rcookie->locsyms[r_symndx];
14119 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
14121 && (isec->kept_section != NULL
14122 || discarded_section (isec)))
14130 /* Discard unneeded references to discarded sections.
14131 Returns -1 on error, 1 if any section's size was changed, 0 if
14132 nothing changed. This function assumes that the relocations are in
14133 sorted order, which is true for all known assemblers. */
14136 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
14138 struct elf_reloc_cookie cookie;
14143 if (info->traditional_format
14144 || !is_elf_hash_table (info->hash))
14147 o = bfd_get_section_by_name (output_bfd, ".stab");
14152 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14155 || i->reloc_count == 0
14156 || i->sec_info_type != SEC_INFO_TYPE_STABS)
14160 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14163 if (!init_reloc_cookie_for_section (&cookie, info, i))
14166 if (_bfd_discard_section_stabs (abfd, i,
14167 elf_section_data (i)->sec_info,
14168 bfd_elf_reloc_symbol_deleted_p,
14172 fini_reloc_cookie_for_section (&cookie, i);
14177 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
14178 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
14182 int eh_changed = 0;
14183 unsigned int eh_alignment;
14185 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
14191 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14194 if (!init_reloc_cookie_for_section (&cookie, info, i))
14197 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
14198 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
14199 bfd_elf_reloc_symbol_deleted_p,
14203 if (i->size != i->rawsize)
14207 fini_reloc_cookie_for_section (&cookie, i);
14210 eh_alignment = 1 << o->alignment_power;
14211 /* Skip over zero terminator, and prevent empty sections from
14212 adding alignment padding at the end. */
14213 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
14215 i->flags |= SEC_EXCLUDE;
14216 else if (i->size > 4)
14218 /* The last non-empty eh_frame section doesn't need padding. */
14221 /* Any prior sections must pad the last FDE out to the output
14222 section alignment. Otherwise we might have zero padding
14223 between sections, which would be seen as a terminator. */
14224 for (; i != NULL; i = i->map_tail.s)
14226 /* All but the last zero terminator should have been removed. */
14231 = (i->size + eh_alignment - 1) & -eh_alignment;
14232 if (i->size != size)
14240 elf_link_hash_traverse (elf_hash_table (info),
14241 _bfd_elf_adjust_eh_frame_global_symbol, NULL);
14244 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
14246 const struct elf_backend_data *bed;
14249 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
14251 s = abfd->sections;
14252 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
14255 bed = get_elf_backend_data (abfd);
14257 if (bed->elf_backend_discard_info != NULL)
14259 if (!init_reloc_cookie (&cookie, info, abfd))
14262 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
14265 fini_reloc_cookie (&cookie, abfd);
14269 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
14270 _bfd_elf_end_eh_frame_parsing (info);
14272 if (info->eh_frame_hdr_type
14273 && !bfd_link_relocatable (info)
14274 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
14281 _bfd_elf_section_already_linked (bfd *abfd,
14283 struct bfd_link_info *info)
14286 const char *name, *key;
14287 struct bfd_section_already_linked *l;
14288 struct bfd_section_already_linked_hash_entry *already_linked_list;
14290 if (sec->output_section == bfd_abs_section_ptr)
14293 flags = sec->flags;
14295 /* Return if it isn't a linkonce section. A comdat group section
14296 also has SEC_LINK_ONCE set. */
14297 if ((flags & SEC_LINK_ONCE) == 0)
14300 /* Don't put group member sections on our list of already linked
14301 sections. They are handled as a group via their group section. */
14302 if (elf_sec_group (sec) != NULL)
14305 /* For a SHT_GROUP section, use the group signature as the key. */
14307 if ((flags & SEC_GROUP) != 0
14308 && elf_next_in_group (sec) != NULL
14309 && elf_group_name (elf_next_in_group (sec)) != NULL)
14310 key = elf_group_name (elf_next_in_group (sec));
14313 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
14314 if (CONST_STRNEQ (name, ".gnu.linkonce.")
14315 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
14318 /* Must be a user linkonce section that doesn't follow gcc's
14319 naming convention. In this case we won't be matching
14320 single member groups. */
14324 already_linked_list = bfd_section_already_linked_table_lookup (key);
14326 for (l = already_linked_list->entry; l != NULL; l = l->next)
14328 /* We may have 2 different types of sections on the list: group
14329 sections with a signature of <key> (<key> is some string),
14330 and linkonce sections named .gnu.linkonce.<type>.<key>.
14331 Match like sections. LTO plugin sections are an exception.
14332 They are always named .gnu.linkonce.t.<key> and match either
14333 type of section. */
14334 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
14335 && ((flags & SEC_GROUP) != 0
14336 || strcmp (name, l->sec->name) == 0))
14337 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
14339 /* The section has already been linked. See if we should
14340 issue a warning. */
14341 if (!_bfd_handle_already_linked (sec, l, info))
14344 if (flags & SEC_GROUP)
14346 asection *first = elf_next_in_group (sec);
14347 asection *s = first;
14351 s->output_section = bfd_abs_section_ptr;
14352 /* Record which group discards it. */
14353 s->kept_section = l->sec;
14354 s = elf_next_in_group (s);
14355 /* These lists are circular. */
14365 /* A single member comdat group section may be discarded by a
14366 linkonce section and vice versa. */
14367 if ((flags & SEC_GROUP) != 0)
14369 asection *first = elf_next_in_group (sec);
14371 if (first != NULL && elf_next_in_group (first) == first)
14372 /* Check this single member group against linkonce sections. */
14373 for (l = already_linked_list->entry; l != NULL; l = l->next)
14374 if ((l->sec->flags & SEC_GROUP) == 0
14375 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
14377 first->output_section = bfd_abs_section_ptr;
14378 first->kept_section = l->sec;
14379 sec->output_section = bfd_abs_section_ptr;
14384 /* Check this linkonce section against single member groups. */
14385 for (l = already_linked_list->entry; l != NULL; l = l->next)
14386 if (l->sec->flags & SEC_GROUP)
14388 asection *first = elf_next_in_group (l->sec);
14391 && elf_next_in_group (first) == first
14392 && bfd_elf_match_symbols_in_sections (first, sec, info))
14394 sec->output_section = bfd_abs_section_ptr;
14395 sec->kept_section = first;
14400 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
14401 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
14402 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
14403 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
14404 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
14405 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
14406 `.gnu.linkonce.t.F' section from a different bfd not requiring any
14407 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
14408 The reverse order cannot happen as there is never a bfd with only the
14409 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
14410 matter as here were are looking only for cross-bfd sections. */
14412 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
14413 for (l = already_linked_list->entry; l != NULL; l = l->next)
14414 if ((l->sec->flags & SEC_GROUP) == 0
14415 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
14417 if (abfd != l->sec->owner)
14418 sec->output_section = bfd_abs_section_ptr;
14422 /* This is the first section with this name. Record it. */
14423 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
14424 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
14425 return sec->output_section == bfd_abs_section_ptr;
14429 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
14431 return sym->st_shndx == SHN_COMMON;
14435 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
14441 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
14443 return bfd_com_section_ptr;
14447 _bfd_elf_default_got_elt_size (bfd *abfd,
14448 struct bfd_link_info *info ATTRIBUTE_UNUSED,
14449 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
14450 bfd *ibfd ATTRIBUTE_UNUSED,
14451 unsigned long symndx ATTRIBUTE_UNUSED)
14453 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14454 return bed->s->arch_size / 8;
14457 /* Routines to support the creation of dynamic relocs. */
14459 /* Returns the name of the dynamic reloc section associated with SEC. */
14461 static const char *
14462 get_dynamic_reloc_section_name (bfd * abfd,
14464 bfd_boolean is_rela)
14467 const char *old_name = bfd_section_name (sec);
14468 const char *prefix = is_rela ? ".rela" : ".rel";
14470 if (old_name == NULL)
14473 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
14474 sprintf (name, "%s%s", prefix, old_name);
14479 /* Returns the dynamic reloc section associated with SEC.
14480 If necessary compute the name of the dynamic reloc section based
14481 on SEC's name (looked up in ABFD's string table) and the setting
14485 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
14487 bfd_boolean is_rela)
14489 asection * reloc_sec = elf_section_data (sec)->sreloc;
14491 if (reloc_sec == NULL)
14493 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14497 reloc_sec = bfd_get_linker_section (abfd, name);
14499 if (reloc_sec != NULL)
14500 elf_section_data (sec)->sreloc = reloc_sec;
14507 /* Returns the dynamic reloc section associated with SEC. If the
14508 section does not exist it is created and attached to the DYNOBJ
14509 bfd and stored in the SRELOC field of SEC's elf_section_data
14512 ALIGNMENT is the alignment for the newly created section and
14513 IS_RELA defines whether the name should be .rela.<SEC's name>
14514 or .rel.<SEC's name>. The section name is looked up in the
14515 string table associated with ABFD. */
14518 _bfd_elf_make_dynamic_reloc_section (asection *sec,
14520 unsigned int alignment,
14522 bfd_boolean is_rela)
14524 asection * reloc_sec = elf_section_data (sec)->sreloc;
14526 if (reloc_sec == NULL)
14528 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
14533 reloc_sec = bfd_get_linker_section (dynobj, name);
14535 if (reloc_sec == NULL)
14537 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
14538 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
14539 if ((sec->flags & SEC_ALLOC) != 0)
14540 flags |= SEC_ALLOC | SEC_LOAD;
14542 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
14543 if (reloc_sec != NULL)
14545 /* _bfd_elf_get_sec_type_attr chooses a section type by
14546 name. Override as it may be wrong, eg. for a user
14547 section named "auto" we'll get ".relauto" which is
14548 seen to be a .rela section. */
14549 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
14550 if (!bfd_set_section_alignment (reloc_sec, alignment))
14555 elf_section_data (sec)->sreloc = reloc_sec;
14561 /* Copy the ELF symbol type and other attributes for a linker script
14562 assignment from HSRC to HDEST. Generally this should be treated as
14563 if we found a strong non-dynamic definition for HDEST (except that
14564 ld ignores multiple definition errors). */
14566 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
14567 struct bfd_link_hash_entry *hdest,
14568 struct bfd_link_hash_entry *hsrc)
14570 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
14571 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
14572 Elf_Internal_Sym isym;
14574 ehdest->type = ehsrc->type;
14575 ehdest->target_internal = ehsrc->target_internal;
14577 isym.st_other = ehsrc->other;
14578 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
14581 /* Append a RELA relocation REL to section S in BFD. */
14584 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14586 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14587 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
14588 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
14589 bed->s->swap_reloca_out (abfd, rel, loc);
14592 /* Append a REL relocation REL to section S in BFD. */
14595 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
14597 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
14598 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
14599 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
14600 bed->s->swap_reloc_out (abfd, rel, loc);
14603 /* Define __start, __stop, .startof. or .sizeof. symbol. */
14605 struct bfd_link_hash_entry *
14606 bfd_elf_define_start_stop (struct bfd_link_info *info,
14607 const char *symbol, asection *sec)
14609 struct elf_link_hash_entry *h;
14611 h = elf_link_hash_lookup (elf_hash_table (info), symbol,
14612 FALSE, FALSE, TRUE);
14614 && (h->root.type == bfd_link_hash_undefined
14615 || h->root.type == bfd_link_hash_undefweak
14616 || ((h->ref_regular || h->def_dynamic) && !h->def_regular)))
14618 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic;
14619 h->root.type = bfd_link_hash_defined;
14620 h->root.u.def.section = sec;
14621 h->root.u.def.value = 0;
14622 h->def_regular = 1;
14623 h->def_dynamic = 0;
14625 h->u2.start_stop_section = sec;
14626 if (symbol[0] == '.')
14628 /* .startof. and .sizeof. symbols are local. */
14629 const struct elf_backend_data *bed;
14630 bed = get_elf_backend_data (info->output_bfd);
14631 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
14635 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
14636 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED;
14638 bfd_elf_link_record_dynamic_symbol (info, h);