1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
23 #include "bfd_stdint.h"
28 #include "safe-ctype.h"
29 #include "libiberty.h"
32 /* This struct is used to pass information to routines called via
33 elf_link_hash_traverse which must return failure. */
35 struct elf_info_failed
37 struct bfd_link_info *info;
41 /* This structure is used to pass information to
42 _bfd_elf_link_find_version_dependencies. */
44 struct elf_find_verdep_info
46 /* General link information. */
47 struct bfd_link_info *info;
48 /* The number of dependencies. */
50 /* Whether we had a failure. */
54 static bfd_boolean _bfd_elf_fix_symbol_flags
55 (struct elf_link_hash_entry *, struct elf_info_failed *);
58 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
59 unsigned long r_symndx,
62 if (r_symndx >= cookie->locsymcount
63 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
65 struct elf_link_hash_entry *h;
67 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
69 while (h->root.type == bfd_link_hash_indirect
70 || h->root.type == bfd_link_hash_warning)
71 h = (struct elf_link_hash_entry *) h->root.u.i.link;
73 if ((h->root.type == bfd_link_hash_defined
74 || h->root.type == bfd_link_hash_defweak)
75 && discarded_section (h->root.u.def.section))
76 return h->root.u.def.section;
82 /* It's not a relocation against a global symbol,
83 but it could be a relocation against a local
84 symbol for a discarded section. */
86 Elf_Internal_Sym *isym;
88 /* Need to: get the symbol; get the section. */
89 isym = &cookie->locsyms[r_symndx];
90 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
92 && discard ? discarded_section (isec) : 1)
98 /* Define a symbol in a dynamic linkage section. */
100 struct elf_link_hash_entry *
101 _bfd_elf_define_linkage_sym (bfd *abfd,
102 struct bfd_link_info *info,
106 struct elf_link_hash_entry *h;
107 struct bfd_link_hash_entry *bh;
108 const struct elf_backend_data *bed;
110 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE);
113 /* Zap symbol defined in an as-needed lib that wasn't linked.
114 This is a symptom of a larger problem: Absolute symbols
115 defined in shared libraries can't be overridden, because we
116 lose the link to the bfd which is via the symbol section. */
117 h->root.type = bfd_link_hash_new;
121 bed = get_elf_backend_data (abfd);
122 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
123 sec, 0, NULL, FALSE, bed->collect,
126 h = (struct elf_link_hash_entry *) bh;
129 h->root.linker_def = 1;
130 h->type = STT_OBJECT;
131 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
132 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
134 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
139 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
143 struct elf_link_hash_entry *h;
144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
145 struct elf_link_hash_table *htab = elf_hash_table (info);
147 /* This function may be called more than once. */
148 s = bfd_get_linker_section (abfd, ".got");
152 flags = bed->dynamic_sec_flags;
154 s = bfd_make_section_anyway_with_flags (abfd,
155 (bed->rela_plts_and_copies_p
156 ? ".rela.got" : ".rel.got"),
157 (bed->dynamic_sec_flags
160 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
164 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
166 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
170 if (bed->want_got_plt)
172 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
174 || !bfd_set_section_alignment (abfd, s,
175 bed->s->log_file_align))
180 /* The first bit of the global offset table is the header. */
181 s->size += bed->got_header_size;
183 if (bed->want_got_sym)
185 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
186 (or .got.plt) section. We don't do this in the linker script
187 because we don't want to define the symbol if we are not creating
188 a global offset table. */
189 h = _bfd_elf_define_linkage_sym (abfd, info, s,
190 "_GLOBAL_OFFSET_TABLE_");
191 elf_hash_table (info)->hgot = h;
199 /* Create a strtab to hold the dynamic symbol names. */
201 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
203 struct elf_link_hash_table *hash_table;
205 hash_table = elf_hash_table (info);
206 if (hash_table->dynobj == NULL)
207 hash_table->dynobj = abfd;
209 if (hash_table->dynstr == NULL)
211 hash_table->dynstr = _bfd_elf_strtab_init ();
212 if (hash_table->dynstr == NULL)
218 /* Create some sections which will be filled in with dynamic linking
219 information. ABFD is an input file which requires dynamic sections
220 to be created. The dynamic sections take up virtual memory space
221 when the final executable is run, so we need to create them before
222 addresses are assigned to the output sections. We work out the
223 actual contents and size of these sections later. */
226 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
230 const struct elf_backend_data *bed;
231 struct elf_link_hash_entry *h;
233 if (! is_elf_hash_table (info->hash))
236 if (elf_hash_table (info)->dynamic_sections_created)
239 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
242 abfd = elf_hash_table (info)->dynobj;
243 bed = get_elf_backend_data (abfd);
245 flags = bed->dynamic_sec_flags;
247 /* A dynamically linked executable has a .interp section, but a
248 shared library does not. */
249 if (bfd_link_executable (info) && !info->nointerp)
251 s = bfd_make_section_anyway_with_flags (abfd, ".interp",
252 flags | SEC_READONLY);
257 /* Create sections to hold version informations. These are removed
258 if they are not needed. */
259 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
260 flags | SEC_READONLY);
262 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
265 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
266 flags | SEC_READONLY);
268 || ! bfd_set_section_alignment (abfd, s, 1))
271 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
272 flags | SEC_READONLY);
274 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
277 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
278 flags | SEC_READONLY);
280 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
282 elf_hash_table (info)->dynsym = s;
284 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
285 flags | SEC_READONLY);
289 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
291 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
294 /* The special symbol _DYNAMIC is always set to the start of the
295 .dynamic section. We could set _DYNAMIC in a linker script, but we
296 only want to define it if we are, in fact, creating a .dynamic
297 section. We don't want to define it if there is no .dynamic
298 section, since on some ELF platforms the start up code examines it
299 to decide how to initialize the process. */
300 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
301 elf_hash_table (info)->hdynamic = h;
307 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
308 flags | SEC_READONLY);
310 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
312 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
315 if (info->emit_gnu_hash)
317 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
318 flags | SEC_READONLY);
320 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
322 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
323 4 32-bit words followed by variable count of 64-bit words, then
324 variable count of 32-bit words. */
325 if (bed->s->arch_size == 64)
326 elf_section_data (s)->this_hdr.sh_entsize = 0;
328 elf_section_data (s)->this_hdr.sh_entsize = 4;
331 /* Let the backend create the rest of the sections. This lets the
332 backend set the right flags. The backend will normally create
333 the .got and .plt sections. */
334 if (bed->elf_backend_create_dynamic_sections == NULL
335 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
338 elf_hash_table (info)->dynamic_sections_created = TRUE;
343 /* Create dynamic sections when linking against a dynamic object. */
346 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
348 flagword flags, pltflags;
349 struct elf_link_hash_entry *h;
351 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
352 struct elf_link_hash_table *htab = elf_hash_table (info);
354 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
355 .rel[a].bss sections. */
356 flags = bed->dynamic_sec_flags;
359 if (bed->plt_not_loaded)
360 /* We do not clear SEC_ALLOC here because we still want the OS to
361 allocate space for the section; it's just that there's nothing
362 to read in from the object file. */
363 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
365 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
366 if (bed->plt_readonly)
367 pltflags |= SEC_READONLY;
369 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
371 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
375 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
377 if (bed->want_plt_sym)
379 h = _bfd_elf_define_linkage_sym (abfd, info, s,
380 "_PROCEDURE_LINKAGE_TABLE_");
381 elf_hash_table (info)->hplt = h;
386 s = bfd_make_section_anyway_with_flags (abfd,
387 (bed->rela_plts_and_copies_p
388 ? ".rela.plt" : ".rel.plt"),
389 flags | SEC_READONLY);
391 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
395 if (! _bfd_elf_create_got_section (abfd, info))
398 if (bed->want_dynbss)
400 /* The .dynbss section is a place to put symbols which are defined
401 by dynamic objects, are referenced by regular objects, and are
402 not functions. We must allocate space for them in the process
403 image and use a R_*_COPY reloc to tell the dynamic linker to
404 initialize them at run time. The linker script puts the .dynbss
405 section into the .bss section of the final image. */
406 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
407 (SEC_ALLOC | SEC_LINKER_CREATED));
411 /* The .rel[a].bss section holds copy relocs. This section is not
412 normally needed. We need to create it here, though, so that the
413 linker will map it to an output section. We can't just create it
414 only if we need it, because we will not know whether we need it
415 until we have seen all the input files, and the first time the
416 main linker code calls BFD after examining all the input files
417 (size_dynamic_sections) the input sections have already been
418 mapped to the output sections. If the section turns out not to
419 be needed, we can discard it later. We will never need this
420 section when generating a shared object, since they do not use
422 if (! bfd_link_pic (info))
424 s = bfd_make_section_anyway_with_flags (abfd,
425 (bed->rela_plts_and_copies_p
426 ? ".rela.bss" : ".rel.bss"),
427 flags | SEC_READONLY);
429 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
437 /* Record a new dynamic symbol. We record the dynamic symbols as we
438 read the input files, since we need to have a list of all of them
439 before we can determine the final sizes of the output sections.
440 Note that we may actually call this function even though we are not
441 going to output any dynamic symbols; in some cases we know that a
442 symbol should be in the dynamic symbol table, but only if there is
446 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
447 struct elf_link_hash_entry *h)
449 if (h->dynindx == -1)
451 struct elf_strtab_hash *dynstr;
456 /* XXX: The ABI draft says the linker must turn hidden and
457 internal symbols into STB_LOCAL symbols when producing the
458 DSO. However, if ld.so honors st_other in the dynamic table,
459 this would not be necessary. */
460 switch (ELF_ST_VISIBILITY (h->other))
464 if (h->root.type != bfd_link_hash_undefined
465 && h->root.type != bfd_link_hash_undefweak)
468 if (!elf_hash_table (info)->is_relocatable_executable)
476 h->dynindx = elf_hash_table (info)->dynsymcount;
477 ++elf_hash_table (info)->dynsymcount;
479 dynstr = elf_hash_table (info)->dynstr;
482 /* Create a strtab to hold the dynamic symbol names. */
483 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
488 /* We don't put any version information in the dynamic string
490 name = h->root.root.string;
491 p = strchr (name, ELF_VER_CHR);
493 /* We know that the p points into writable memory. In fact,
494 there are only a few symbols that have read-only names, being
495 those like _GLOBAL_OFFSET_TABLE_ that are created specially
496 by the backends. Most symbols will have names pointing into
497 an ELF string table read from a file, or to objalloc memory. */
500 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
505 if (indx == (bfd_size_type) -1)
507 h->dynstr_index = indx;
513 /* Mark a symbol dynamic. */
516 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
517 struct elf_link_hash_entry *h,
518 Elf_Internal_Sym *sym)
520 struct bfd_elf_dynamic_list *d = info->dynamic_list;
522 /* It may be called more than once on the same H. */
523 if(h->dynamic || bfd_link_relocatable (info))
526 if ((info->dynamic_data
527 && (h->type == STT_OBJECT
529 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
531 && h->root.type == bfd_link_hash_new
532 && (*d->match) (&d->head, NULL, h->root.root.string)))
536 /* Record an assignment to a symbol made by a linker script. We need
537 this in case some dynamic object refers to this symbol. */
540 bfd_elf_record_link_assignment (bfd *output_bfd,
541 struct bfd_link_info *info,
546 struct elf_link_hash_entry *h, *hv;
547 struct elf_link_hash_table *htab;
548 const struct elf_backend_data *bed;
550 if (!is_elf_hash_table (info->hash))
553 htab = elf_hash_table (info);
554 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
558 switch (h->root.type)
560 case bfd_link_hash_defined:
561 case bfd_link_hash_defweak:
562 case bfd_link_hash_common:
564 case bfd_link_hash_undefweak:
565 case bfd_link_hash_undefined:
566 /* Since we're defining the symbol, don't let it seem to have not
567 been defined. record_dynamic_symbol and size_dynamic_sections
568 may depend on this. */
569 h->root.type = bfd_link_hash_new;
570 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
571 bfd_link_repair_undef_list (&htab->root);
573 case bfd_link_hash_new:
574 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
577 case bfd_link_hash_indirect:
578 /* We had a versioned symbol in a dynamic library. We make the
579 the versioned symbol point to this one. */
580 bed = get_elf_backend_data (output_bfd);
582 while (hv->root.type == bfd_link_hash_indirect
583 || hv->root.type == bfd_link_hash_warning)
584 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
585 /* We don't need to update h->root.u since linker will set them
587 h->root.type = bfd_link_hash_undefined;
588 hv->root.type = bfd_link_hash_indirect;
589 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
590 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
592 case bfd_link_hash_warning:
597 /* If this symbol is being provided by the linker script, and it is
598 currently defined by a dynamic object, but not by a regular
599 object, then mark it as undefined so that the generic linker will
600 force the correct value. */
604 h->root.type = bfd_link_hash_undefined;
606 /* If this symbol is not being provided by the linker script, and it is
607 currently defined by a dynamic object, but not by a regular object,
608 then clear out any version information because the symbol will not be
609 associated with the dynamic object any more. */
613 h->verinfo.verdef = NULL;
619 bed = get_elf_backend_data (output_bfd);
620 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
621 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
622 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
625 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
627 if (!bfd_link_relocatable (info)
629 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
630 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
635 || bfd_link_dll (info)
636 || elf_hash_table (info)->is_relocatable_executable)
639 if (! bfd_elf_link_record_dynamic_symbol (info, h))
642 /* If this is a weak defined symbol, and we know a corresponding
643 real symbol from the same dynamic object, make sure the real
644 symbol is also made into a dynamic symbol. */
645 if (h->u.weakdef != NULL
646 && h->u.weakdef->dynindx == -1)
648 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
656 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
657 success, and 2 on a failure caused by attempting to record a symbol
658 in a discarded section, eg. a discarded link-once section symbol. */
661 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
666 struct elf_link_local_dynamic_entry *entry;
667 struct elf_link_hash_table *eht;
668 struct elf_strtab_hash *dynstr;
669 unsigned long dynstr_index;
671 Elf_External_Sym_Shndx eshndx;
672 char esym[sizeof (Elf64_External_Sym)];
674 if (! is_elf_hash_table (info->hash))
677 /* See if the entry exists already. */
678 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
679 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
682 amt = sizeof (*entry);
683 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
687 /* Go find the symbol, so that we can find it's name. */
688 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
689 1, input_indx, &entry->isym, esym, &eshndx))
691 bfd_release (input_bfd, entry);
695 if (entry->isym.st_shndx != SHN_UNDEF
696 && entry->isym.st_shndx < SHN_LORESERVE)
700 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
701 if (s == NULL || bfd_is_abs_section (s->output_section))
703 /* We can still bfd_release here as nothing has done another
704 bfd_alloc. We can't do this later in this function. */
705 bfd_release (input_bfd, entry);
710 name = (bfd_elf_string_from_elf_section
711 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
712 entry->isym.st_name));
714 dynstr = elf_hash_table (info)->dynstr;
717 /* Create a strtab to hold the dynamic symbol names. */
718 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
723 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
724 if (dynstr_index == (unsigned long) -1)
726 entry->isym.st_name = dynstr_index;
728 eht = elf_hash_table (info);
730 entry->next = eht->dynlocal;
731 eht->dynlocal = entry;
732 entry->input_bfd = input_bfd;
733 entry->input_indx = input_indx;
736 /* Whatever binding the symbol had before, it's now local. */
738 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
740 /* The dynindx will be set at the end of size_dynamic_sections. */
745 /* Return the dynindex of a local dynamic symbol. */
748 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
752 struct elf_link_local_dynamic_entry *e;
754 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
755 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
760 /* This function is used to renumber the dynamic symbols, if some of
761 them are removed because they are marked as local. This is called
762 via elf_link_hash_traverse. */
765 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
768 size_t *count = (size_t *) data;
773 if (h->dynindx != -1)
774 h->dynindx = ++(*count);
780 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
781 STB_LOCAL binding. */
784 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
787 size_t *count = (size_t *) data;
789 if (!h->forced_local)
792 if (h->dynindx != -1)
793 h->dynindx = ++(*count);
798 /* Return true if the dynamic symbol for a given section should be
799 omitted when creating a shared library. */
801 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
802 struct bfd_link_info *info,
805 struct elf_link_hash_table *htab;
808 switch (elf_section_data (p)->this_hdr.sh_type)
812 /* If sh_type is yet undecided, assume it could be
813 SHT_PROGBITS/SHT_NOBITS. */
815 htab = elf_hash_table (info);
816 if (p == htab->tls_sec)
819 if (htab->text_index_section != NULL)
820 return p != htab->text_index_section && p != htab->data_index_section;
822 return (htab->dynobj != NULL
823 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
824 && ip->output_section == p);
826 /* There shouldn't be section relative relocations
827 against any other section. */
833 /* Assign dynsym indices. In a shared library we generate a section
834 symbol for each output section, which come first. Next come symbols
835 which have been forced to local binding. Then all of the back-end
836 allocated local dynamic syms, followed by the rest of the global
840 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
841 struct bfd_link_info *info,
842 unsigned long *section_sym_count)
844 unsigned long dynsymcount = 0;
846 if (bfd_link_pic (info)
847 || elf_hash_table (info)->is_relocatable_executable)
849 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
851 for (p = output_bfd->sections; p ; p = p->next)
852 if ((p->flags & SEC_EXCLUDE) == 0
853 && (p->flags & SEC_ALLOC) != 0
854 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
855 elf_section_data (p)->dynindx = ++dynsymcount;
857 elf_section_data (p)->dynindx = 0;
859 *section_sym_count = dynsymcount;
861 elf_link_hash_traverse (elf_hash_table (info),
862 elf_link_renumber_local_hash_table_dynsyms,
865 if (elf_hash_table (info)->dynlocal)
867 struct elf_link_local_dynamic_entry *p;
868 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
869 p->dynindx = ++dynsymcount;
872 elf_link_hash_traverse (elf_hash_table (info),
873 elf_link_renumber_hash_table_dynsyms,
876 /* There is an unused NULL entry at the head of the table which
877 we must account for in our count. We always create the dynsym
878 section, even if it is empty, with dynamic sections. */
879 if (elf_hash_table (info)->dynamic_sections_created)
882 elf_hash_table (info)->dynsymcount = dynsymcount;
886 /* Merge st_other field. */
889 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
890 const Elf_Internal_Sym *isym, asection *sec,
891 bfd_boolean definition, bfd_boolean dynamic)
893 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
895 /* If st_other has a processor-specific meaning, specific
896 code might be needed here. */
897 if (bed->elf_backend_merge_symbol_attribute)
898 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
903 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
904 unsigned hvis = ELF_ST_VISIBILITY (h->other);
906 /* Keep the most constraining visibility. Leave the remainder
907 of the st_other field to elf_backend_merge_symbol_attribute. */
908 if (symvis - 1 < hvis - 1)
909 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
912 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
913 && (sec->flags & SEC_READONLY) == 0)
914 h->protected_def = 1;
917 /* This function is called when we want to merge a new symbol with an
918 existing symbol. It handles the various cases which arise when we
919 find a definition in a dynamic object, or when there is already a
920 definition in a dynamic object. The new symbol is described by
921 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
922 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
923 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
924 of an old common symbol. We set OVERRIDE if the old symbol is
925 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
926 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
927 to change. By OK to change, we mean that we shouldn't warn if the
928 type or size does change. */
931 _bfd_elf_merge_symbol (bfd *abfd,
932 struct bfd_link_info *info,
934 Elf_Internal_Sym *sym,
937 struct elf_link_hash_entry **sym_hash,
939 bfd_boolean *pold_weak,
940 unsigned int *pold_alignment,
942 bfd_boolean *override,
943 bfd_boolean *type_change_ok,
944 bfd_boolean *size_change_ok,
945 bfd_boolean *matched)
947 asection *sec, *oldsec;
948 struct elf_link_hash_entry *h;
949 struct elf_link_hash_entry *hi;
950 struct elf_link_hash_entry *flip;
953 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
954 bfd_boolean newweak, oldweak, newfunc, oldfunc;
955 const struct elf_backend_data *bed;
962 bind = ELF_ST_BIND (sym->st_info);
964 if (! bfd_is_und_section (sec))
965 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
967 h = ((struct elf_link_hash_entry *)
968 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
973 bed = get_elf_backend_data (abfd);
975 /* NEW_VERSION is the symbol version of the new symbol. */
976 if (h->versioned != unversioned)
978 /* Symbol version is unknown or versioned. */
979 new_version = strrchr (name, ELF_VER_CHR);
982 if (h->versioned == unknown)
984 if (new_version > name && new_version[-1] != ELF_VER_CHR)
985 h->versioned = versioned_hidden;
987 h->versioned = versioned;
990 if (new_version[0] == '\0')
994 h->versioned = unversioned;
999 /* For merging, we only care about real symbols. But we need to make
1000 sure that indirect symbol dynamic flags are updated. */
1002 while (h->root.type == bfd_link_hash_indirect
1003 || h->root.type == bfd_link_hash_warning)
1004 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1008 if (hi == h || h->root.type == bfd_link_hash_new)
1012 /* OLD_HIDDEN is true if the existing symbol is only visible
1013 to the symbol with the same symbol version. NEW_HIDDEN is
1014 true if the new symbol is only visible to the symbol with
1015 the same symbol version. */
1016 bfd_boolean old_hidden = h->versioned == versioned_hidden;
1017 bfd_boolean new_hidden = hi->versioned == versioned_hidden;
1018 if (!old_hidden && !new_hidden)
1019 /* The new symbol matches the existing symbol if both
1024 /* OLD_VERSION is the symbol version of the existing
1028 if (h->versioned >= versioned)
1029 old_version = strrchr (h->root.root.string,
1034 /* The new symbol matches the existing symbol if they
1035 have the same symbol version. */
1036 *matched = (old_version == new_version
1037 || (old_version != NULL
1038 && new_version != NULL
1039 && strcmp (old_version, new_version) == 0));
1044 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
1049 switch (h->root.type)
1054 case bfd_link_hash_undefined:
1055 case bfd_link_hash_undefweak:
1056 oldbfd = h->root.u.undef.abfd;
1059 case bfd_link_hash_defined:
1060 case bfd_link_hash_defweak:
1061 oldbfd = h->root.u.def.section->owner;
1062 oldsec = h->root.u.def.section;
1065 case bfd_link_hash_common:
1066 oldbfd = h->root.u.c.p->section->owner;
1067 oldsec = h->root.u.c.p->section;
1069 *pold_alignment = h->root.u.c.p->alignment_power;
1072 if (poldbfd && *poldbfd == NULL)
1075 /* Differentiate strong and weak symbols. */
1076 newweak = bind == STB_WEAK;
1077 oldweak = (h->root.type == bfd_link_hash_defweak
1078 || h->root.type == bfd_link_hash_undefweak);
1080 *pold_weak = oldweak;
1082 /* This code is for coping with dynamic objects, and is only useful
1083 if we are doing an ELF link. */
1084 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1087 /* We have to check it for every instance since the first few may be
1088 references and not all compilers emit symbol type for undefined
1090 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1092 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1093 respectively, is from a dynamic object. */
1095 newdyn = (abfd->flags & DYNAMIC) != 0;
1097 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1098 syms and defined syms in dynamic libraries respectively.
1099 ref_dynamic on the other hand can be set for a symbol defined in
1100 a dynamic library, and def_dynamic may not be set; When the
1101 definition in a dynamic lib is overridden by a definition in the
1102 executable use of the symbol in the dynamic lib becomes a
1103 reference to the executable symbol. */
1106 if (bfd_is_und_section (sec))
1108 if (bind != STB_WEAK)
1110 h->ref_dynamic_nonweak = 1;
1111 hi->ref_dynamic_nonweak = 1;
1116 /* Update the existing symbol only if they match. */
1119 hi->dynamic_def = 1;
1123 /* If we just created the symbol, mark it as being an ELF symbol.
1124 Other than that, there is nothing to do--there is no merge issue
1125 with a newly defined symbol--so we just return. */
1127 if (h->root.type == bfd_link_hash_new)
1133 /* In cases involving weak versioned symbols, we may wind up trying
1134 to merge a symbol with itself. Catch that here, to avoid the
1135 confusion that results if we try to override a symbol with
1136 itself. The additional tests catch cases like
1137 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1138 dynamic object, which we do want to handle here. */
1140 && (newweak || oldweak)
1141 && ((abfd->flags & DYNAMIC) == 0
1142 || !h->def_regular))
1147 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1148 else if (oldsec != NULL)
1150 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1151 indices used by MIPS ELF. */
1152 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1155 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1156 respectively, appear to be a definition rather than reference. */
1158 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1160 olddef = (h->root.type != bfd_link_hash_undefined
1161 && h->root.type != bfd_link_hash_undefweak
1162 && h->root.type != bfd_link_hash_common);
1164 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1165 respectively, appear to be a function. */
1167 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1168 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1170 oldfunc = (h->type != STT_NOTYPE
1171 && bed->is_function_type (h->type));
1173 /* When we try to create a default indirect symbol from the dynamic
1174 definition with the default version, we skip it if its type and
1175 the type of existing regular definition mismatch. */
1176 if (pold_alignment == NULL
1180 && (((olddef || h->root.type == bfd_link_hash_common)
1181 && ELF_ST_TYPE (sym->st_info) != h->type
1182 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1183 && h->type != STT_NOTYPE
1184 && !(newfunc && oldfunc))
1186 && ((h->type == STT_GNU_IFUNC)
1187 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1193 /* Check TLS symbols. We don't check undefined symbols introduced
1194 by "ld -u" which have no type (and oldbfd NULL), and we don't
1195 check symbols from plugins because they also have no type. */
1197 && (oldbfd->flags & BFD_PLUGIN) == 0
1198 && (abfd->flags & BFD_PLUGIN) == 0
1199 && ELF_ST_TYPE (sym->st_info) != h->type
1200 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1203 bfd_boolean ntdef, tdef;
1204 asection *ntsec, *tsec;
1206 if (h->type == STT_TLS)
1226 (*_bfd_error_handler)
1227 (_("%s: TLS definition in %B section %A "
1228 "mismatches non-TLS definition in %B section %A"),
1229 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1230 else if (!tdef && !ntdef)
1231 (*_bfd_error_handler)
1232 (_("%s: TLS reference in %B "
1233 "mismatches non-TLS reference in %B"),
1234 tbfd, ntbfd, h->root.root.string);
1236 (*_bfd_error_handler)
1237 (_("%s: TLS definition in %B section %A "
1238 "mismatches non-TLS reference in %B"),
1239 tbfd, tsec, ntbfd, h->root.root.string);
1241 (*_bfd_error_handler)
1242 (_("%s: TLS reference in %B "
1243 "mismatches non-TLS definition in %B section %A"),
1244 tbfd, ntbfd, ntsec, h->root.root.string);
1246 bfd_set_error (bfd_error_bad_value);
1250 /* If the old symbol has non-default visibility, we ignore the new
1251 definition from a dynamic object. */
1253 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1254 && !bfd_is_und_section (sec))
1257 /* Make sure this symbol is dynamic. */
1259 hi->ref_dynamic = 1;
1260 /* A protected symbol has external availability. Make sure it is
1261 recorded as dynamic.
1263 FIXME: Should we check type and size for protected symbol? */
1264 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1265 return bfd_elf_link_record_dynamic_symbol (info, h);
1270 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1273 /* If the new symbol with non-default visibility comes from a
1274 relocatable file and the old definition comes from a dynamic
1275 object, we remove the old definition. */
1276 if (hi->root.type == bfd_link_hash_indirect)
1278 /* Handle the case where the old dynamic definition is
1279 default versioned. We need to copy the symbol info from
1280 the symbol with default version to the normal one if it
1281 was referenced before. */
1284 hi->root.type = h->root.type;
1285 h->root.type = bfd_link_hash_indirect;
1286 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1288 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1289 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1291 /* If the new symbol is hidden or internal, completely undo
1292 any dynamic link state. */
1293 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1294 h->forced_local = 0;
1301 /* FIXME: Should we check type and size for protected symbol? */
1311 /* If the old symbol was undefined before, then it will still be
1312 on the undefs list. If the new symbol is undefined or
1313 common, we can't make it bfd_link_hash_new here, because new
1314 undefined or common symbols will be added to the undefs list
1315 by _bfd_generic_link_add_one_symbol. Symbols may not be
1316 added twice to the undefs list. Also, if the new symbol is
1317 undefweak then we don't want to lose the strong undef. */
1318 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1320 h->root.type = bfd_link_hash_undefined;
1321 h->root.u.undef.abfd = abfd;
1325 h->root.type = bfd_link_hash_new;
1326 h->root.u.undef.abfd = NULL;
1329 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1331 /* If the new symbol is hidden or internal, completely undo
1332 any dynamic link state. */
1333 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1334 h->forced_local = 0;
1340 /* FIXME: Should we check type and size for protected symbol? */
1346 /* If a new weak symbol definition comes from a regular file and the
1347 old symbol comes from a dynamic library, we treat the new one as
1348 strong. Similarly, an old weak symbol definition from a regular
1349 file is treated as strong when the new symbol comes from a dynamic
1350 library. Further, an old weak symbol from a dynamic library is
1351 treated as strong if the new symbol is from a dynamic library.
1352 This reflects the way glibc's ld.so works.
1354 Do this before setting *type_change_ok or *size_change_ok so that
1355 we warn properly when dynamic library symbols are overridden. */
1357 if (newdef && !newdyn && olddyn)
1359 if (olddef && newdyn)
1362 /* Allow changes between different types of function symbol. */
1363 if (newfunc && oldfunc)
1364 *type_change_ok = TRUE;
1366 /* It's OK to change the type if either the existing symbol or the
1367 new symbol is weak. A type change is also OK if the old symbol
1368 is undefined and the new symbol is defined. */
1373 && h->root.type == bfd_link_hash_undefined))
1374 *type_change_ok = TRUE;
1376 /* It's OK to change the size if either the existing symbol or the
1377 new symbol is weak, or if the old symbol is undefined. */
1380 || h->root.type == bfd_link_hash_undefined)
1381 *size_change_ok = TRUE;
1383 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1384 symbol, respectively, appears to be a common symbol in a dynamic
1385 object. If a symbol appears in an uninitialized section, and is
1386 not weak, and is not a function, then it may be a common symbol
1387 which was resolved when the dynamic object was created. We want
1388 to treat such symbols specially, because they raise special
1389 considerations when setting the symbol size: if the symbol
1390 appears as a common symbol in a regular object, and the size in
1391 the regular object is larger, we must make sure that we use the
1392 larger size. This problematic case can always be avoided in C,
1393 but it must be handled correctly when using Fortran shared
1396 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1397 likewise for OLDDYNCOMMON and OLDDEF.
1399 Note that this test is just a heuristic, and that it is quite
1400 possible to have an uninitialized symbol in a shared object which
1401 is really a definition, rather than a common symbol. This could
1402 lead to some minor confusion when the symbol really is a common
1403 symbol in some regular object. However, I think it will be
1409 && (sec->flags & SEC_ALLOC) != 0
1410 && (sec->flags & SEC_LOAD) == 0
1413 newdyncommon = TRUE;
1415 newdyncommon = FALSE;
1419 && h->root.type == bfd_link_hash_defined
1421 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1422 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1425 olddyncommon = TRUE;
1427 olddyncommon = FALSE;
1429 /* We now know everything about the old and new symbols. We ask the
1430 backend to check if we can merge them. */
1431 if (bed->merge_symbol != NULL)
1433 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1438 /* If both the old and the new symbols look like common symbols in a
1439 dynamic object, set the size of the symbol to the larger of the
1444 && sym->st_size != h->size)
1446 /* Since we think we have two common symbols, issue a multiple
1447 common warning if desired. Note that we only warn if the
1448 size is different. If the size is the same, we simply let
1449 the old symbol override the new one as normally happens with
1450 symbols defined in dynamic objects. */
1452 if (! ((*info->callbacks->multiple_common)
1453 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1456 if (sym->st_size > h->size)
1457 h->size = sym->st_size;
1459 *size_change_ok = TRUE;
1462 /* If we are looking at a dynamic object, and we have found a
1463 definition, we need to see if the symbol was already defined by
1464 some other object. If so, we want to use the existing
1465 definition, and we do not want to report a multiple symbol
1466 definition error; we do this by clobbering *PSEC to be
1467 bfd_und_section_ptr.
1469 We treat a common symbol as a definition if the symbol in the
1470 shared library is a function, since common symbols always
1471 represent variables; this can cause confusion in principle, but
1472 any such confusion would seem to indicate an erroneous program or
1473 shared library. We also permit a common symbol in a regular
1474 object to override a weak symbol in a shared object. */
1479 || (h->root.type == bfd_link_hash_common
1480 && (newweak || newfunc))))
1484 newdyncommon = FALSE;
1486 *psec = sec = bfd_und_section_ptr;
1487 *size_change_ok = TRUE;
1489 /* If we get here when the old symbol is a common symbol, then
1490 we are explicitly letting it override a weak symbol or
1491 function in a dynamic object, and we don't want to warn about
1492 a type change. If the old symbol is a defined symbol, a type
1493 change warning may still be appropriate. */
1495 if (h->root.type == bfd_link_hash_common)
1496 *type_change_ok = TRUE;
1499 /* Handle the special case of an old common symbol merging with a
1500 new symbol which looks like a common symbol in a shared object.
1501 We change *PSEC and *PVALUE to make the new symbol look like a
1502 common symbol, and let _bfd_generic_link_add_one_symbol do the
1506 && h->root.type == bfd_link_hash_common)
1510 newdyncommon = FALSE;
1511 *pvalue = sym->st_size;
1512 *psec = sec = bed->common_section (oldsec);
1513 *size_change_ok = TRUE;
1516 /* Skip weak definitions of symbols that are already defined. */
1517 if (newdef && olddef && newweak)
1519 /* Don't skip new non-IR weak syms. */
1520 if (!(oldbfd != NULL
1521 && (oldbfd->flags & BFD_PLUGIN) != 0
1522 && (abfd->flags & BFD_PLUGIN) == 0))
1528 /* Merge st_other. If the symbol already has a dynamic index,
1529 but visibility says it should not be visible, turn it into a
1531 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1532 if (h->dynindx != -1)
1533 switch (ELF_ST_VISIBILITY (h->other))
1537 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1542 /* If the old symbol is from a dynamic object, and the new symbol is
1543 a definition which is not from a dynamic object, then the new
1544 symbol overrides the old symbol. Symbols from regular files
1545 always take precedence over symbols from dynamic objects, even if
1546 they are defined after the dynamic object in the link.
1548 As above, we again permit a common symbol in a regular object to
1549 override a definition in a shared object if the shared object
1550 symbol is a function or is weak. */
1555 || (bfd_is_com_section (sec)
1556 && (oldweak || oldfunc)))
1561 /* Change the hash table entry to undefined, and let
1562 _bfd_generic_link_add_one_symbol do the right thing with the
1565 h->root.type = bfd_link_hash_undefined;
1566 h->root.u.undef.abfd = h->root.u.def.section->owner;
1567 *size_change_ok = TRUE;
1570 olddyncommon = FALSE;
1572 /* We again permit a type change when a common symbol may be
1573 overriding a function. */
1575 if (bfd_is_com_section (sec))
1579 /* If a common symbol overrides a function, make sure
1580 that it isn't defined dynamically nor has type
1583 h->type = STT_NOTYPE;
1585 *type_change_ok = TRUE;
1588 if (hi->root.type == bfd_link_hash_indirect)
1591 /* This union may have been set to be non-NULL when this symbol
1592 was seen in a dynamic object. We must force the union to be
1593 NULL, so that it is correct for a regular symbol. */
1594 h->verinfo.vertree = NULL;
1597 /* Handle the special case of a new common symbol merging with an
1598 old symbol that looks like it might be a common symbol defined in
1599 a shared object. Note that we have already handled the case in
1600 which a new common symbol should simply override the definition
1601 in the shared library. */
1604 && bfd_is_com_section (sec)
1607 /* It would be best if we could set the hash table entry to a
1608 common symbol, but we don't know what to use for the section
1609 or the alignment. */
1610 if (! ((*info->callbacks->multiple_common)
1611 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1614 /* If the presumed common symbol in the dynamic object is
1615 larger, pretend that the new symbol has its size. */
1617 if (h->size > *pvalue)
1620 /* We need to remember the alignment required by the symbol
1621 in the dynamic object. */
1622 BFD_ASSERT (pold_alignment);
1623 *pold_alignment = h->root.u.def.section->alignment_power;
1626 olddyncommon = FALSE;
1628 h->root.type = bfd_link_hash_undefined;
1629 h->root.u.undef.abfd = h->root.u.def.section->owner;
1631 *size_change_ok = TRUE;
1632 *type_change_ok = TRUE;
1634 if (hi->root.type == bfd_link_hash_indirect)
1637 h->verinfo.vertree = NULL;
1642 /* Handle the case where we had a versioned symbol in a dynamic
1643 library and now find a definition in a normal object. In this
1644 case, we make the versioned symbol point to the normal one. */
1645 flip->root.type = h->root.type;
1646 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1647 h->root.type = bfd_link_hash_indirect;
1648 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1649 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1653 flip->ref_dynamic = 1;
1660 /* This function is called to create an indirect symbol from the
1661 default for the symbol with the default version if needed. The
1662 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1663 set DYNSYM if the new indirect symbol is dynamic. */
1666 _bfd_elf_add_default_symbol (bfd *abfd,
1667 struct bfd_link_info *info,
1668 struct elf_link_hash_entry *h,
1670 Elf_Internal_Sym *sym,
1674 bfd_boolean *dynsym)
1676 bfd_boolean type_change_ok;
1677 bfd_boolean size_change_ok;
1680 struct elf_link_hash_entry *hi;
1681 struct bfd_link_hash_entry *bh;
1682 const struct elf_backend_data *bed;
1683 bfd_boolean collect;
1684 bfd_boolean dynamic;
1685 bfd_boolean override;
1687 size_t len, shortlen;
1689 bfd_boolean matched;
1691 if (h->versioned == unversioned || h->versioned == versioned_hidden)
1694 /* If this symbol has a version, and it is the default version, we
1695 create an indirect symbol from the default name to the fully
1696 decorated name. This will cause external references which do not
1697 specify a version to be bound to this version of the symbol. */
1698 p = strchr (name, ELF_VER_CHR);
1699 if (h->versioned == unknown)
1703 h->versioned = unversioned;
1708 if (p[1] != ELF_VER_CHR)
1710 h->versioned = versioned_hidden;
1714 h->versioned = versioned;
1719 /* PR ld/19073: We may see an unversioned definition after the
1725 bed = get_elf_backend_data (abfd);
1726 collect = bed->collect;
1727 dynamic = (abfd->flags & DYNAMIC) != 0;
1729 shortlen = p - name;
1730 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1731 if (shortname == NULL)
1733 memcpy (shortname, name, shortlen);
1734 shortname[shortlen] = '\0';
1736 /* We are going to create a new symbol. Merge it with any existing
1737 symbol with this name. For the purposes of the merge, act as
1738 though we were defining the symbol we just defined, although we
1739 actually going to define an indirect symbol. */
1740 type_change_ok = FALSE;
1741 size_change_ok = FALSE;
1744 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1745 &hi, poldbfd, NULL, NULL, &skip, &override,
1746 &type_change_ok, &size_change_ok, &matched))
1754 /* Add the default symbol if not performing a relocatable link. */
1755 if (! bfd_link_relocatable (info))
1758 if (! (_bfd_generic_link_add_one_symbol
1759 (info, abfd, shortname, BSF_INDIRECT,
1760 bfd_ind_section_ptr,
1761 0, name, FALSE, collect, &bh)))
1763 hi = (struct elf_link_hash_entry *) bh;
1768 /* In this case the symbol named SHORTNAME is overriding the
1769 indirect symbol we want to add. We were planning on making
1770 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1771 is the name without a version. NAME is the fully versioned
1772 name, and it is the default version.
1774 Overriding means that we already saw a definition for the
1775 symbol SHORTNAME in a regular object, and it is overriding
1776 the symbol defined in the dynamic object.
1778 When this happens, we actually want to change NAME, the
1779 symbol we just added, to refer to SHORTNAME. This will cause
1780 references to NAME in the shared object to become references
1781 to SHORTNAME in the regular object. This is what we expect
1782 when we override a function in a shared object: that the
1783 references in the shared object will be mapped to the
1784 definition in the regular object. */
1786 while (hi->root.type == bfd_link_hash_indirect
1787 || hi->root.type == bfd_link_hash_warning)
1788 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1790 h->root.type = bfd_link_hash_indirect;
1791 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1795 hi->ref_dynamic = 1;
1799 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1804 /* Now set HI to H, so that the following code will set the
1805 other fields correctly. */
1809 /* Check if HI is a warning symbol. */
1810 if (hi->root.type == bfd_link_hash_warning)
1811 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1813 /* If there is a duplicate definition somewhere, then HI may not
1814 point to an indirect symbol. We will have reported an error to
1815 the user in that case. */
1817 if (hi->root.type == bfd_link_hash_indirect)
1819 struct elf_link_hash_entry *ht;
1821 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1822 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1824 /* A reference to the SHORTNAME symbol from a dynamic library
1825 will be satisfied by the versioned symbol at runtime. In
1826 effect, we have a reference to the versioned symbol. */
1827 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1828 hi->dynamic_def |= ht->dynamic_def;
1830 /* See if the new flags lead us to realize that the symbol must
1836 if (! bfd_link_executable (info)
1843 if (hi->ref_regular)
1849 /* We also need to define an indirection from the nondefault version
1853 len = strlen (name);
1854 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1855 if (shortname == NULL)
1857 memcpy (shortname, name, shortlen);
1858 memcpy (shortname + shortlen, p + 1, len - shortlen);
1860 /* Once again, merge with any existing symbol. */
1861 type_change_ok = FALSE;
1862 size_change_ok = FALSE;
1864 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1865 &hi, poldbfd, NULL, NULL, &skip, &override,
1866 &type_change_ok, &size_change_ok, &matched))
1874 /* Here SHORTNAME is a versioned name, so we don't expect to see
1875 the type of override we do in the case above unless it is
1876 overridden by a versioned definition. */
1877 if (hi->root.type != bfd_link_hash_defined
1878 && hi->root.type != bfd_link_hash_defweak)
1879 (*_bfd_error_handler)
1880 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1886 if (! (_bfd_generic_link_add_one_symbol
1887 (info, abfd, shortname, BSF_INDIRECT,
1888 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1890 hi = (struct elf_link_hash_entry *) bh;
1892 /* If there is a duplicate definition somewhere, then HI may not
1893 point to an indirect symbol. We will have reported an error
1894 to the user in that case. */
1896 if (hi->root.type == bfd_link_hash_indirect)
1898 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1899 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1900 hi->dynamic_def |= h->dynamic_def;
1902 /* See if the new flags lead us to realize that the symbol
1908 if (! bfd_link_executable (info)
1914 if (hi->ref_regular)
1924 /* This routine is used to export all defined symbols into the dynamic
1925 symbol table. It is called via elf_link_hash_traverse. */
1928 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1930 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1932 /* Ignore indirect symbols. These are added by the versioning code. */
1933 if (h->root.type == bfd_link_hash_indirect)
1936 /* Ignore this if we won't export it. */
1937 if (!eif->info->export_dynamic && !h->dynamic)
1940 if (h->dynindx == -1
1941 && (h->def_regular || h->ref_regular)
1942 && ! bfd_hide_sym_by_version (eif->info->version_info,
1943 h->root.root.string))
1945 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1955 /* Look through the symbols which are defined in other shared
1956 libraries and referenced here. Update the list of version
1957 dependencies. This will be put into the .gnu.version_r section.
1958 This function is called via elf_link_hash_traverse. */
1961 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1964 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1965 Elf_Internal_Verneed *t;
1966 Elf_Internal_Vernaux *a;
1969 /* We only care about symbols defined in shared objects with version
1974 || h->verinfo.verdef == NULL
1975 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1976 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1979 /* See if we already know about this version. */
1980 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1984 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1987 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1988 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1994 /* This is a new version. Add it to tree we are building. */
1999 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
2002 rinfo->failed = TRUE;
2006 t->vn_bfd = h->verinfo.verdef->vd_bfd;
2007 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
2008 elf_tdata (rinfo->info->output_bfd)->verref = t;
2012 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
2015 rinfo->failed = TRUE;
2019 /* Note that we are copying a string pointer here, and testing it
2020 above. If bfd_elf_string_from_elf_section is ever changed to
2021 discard the string data when low in memory, this will have to be
2023 a->vna_nodename = h->verinfo.verdef->vd_nodename;
2025 a->vna_flags = h->verinfo.verdef->vd_flags;
2026 a->vna_nextptr = t->vn_auxptr;
2028 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
2031 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
2038 /* Figure out appropriate versions for all the symbols. We may not
2039 have the version number script until we have read all of the input
2040 files, so until that point we don't know which symbols should be
2041 local. This function is called via elf_link_hash_traverse. */
2044 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
2046 struct elf_info_failed *sinfo;
2047 struct bfd_link_info *info;
2048 const struct elf_backend_data *bed;
2049 struct elf_info_failed eif;
2053 sinfo = (struct elf_info_failed *) data;
2056 /* Fix the symbol flags. */
2059 if (! _bfd_elf_fix_symbol_flags (h, &eif))
2062 sinfo->failed = TRUE;
2066 /* We only need version numbers for symbols defined in regular
2068 if (!h->def_regular)
2071 bed = get_elf_backend_data (info->output_bfd);
2072 p = strchr (h->root.root.string, ELF_VER_CHR);
2073 if (p != NULL && h->verinfo.vertree == NULL)
2075 struct bfd_elf_version_tree *t;
2078 if (*p == ELF_VER_CHR)
2081 /* If there is no version string, we can just return out. */
2085 /* Look for the version. If we find it, it is no longer weak. */
2086 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2088 if (strcmp (t->name, p) == 0)
2092 struct bfd_elf_version_expr *d;
2094 len = p - h->root.root.string;
2095 alc = (char *) bfd_malloc (len);
2098 sinfo->failed = TRUE;
2101 memcpy (alc, h->root.root.string, len - 1);
2102 alc[len - 1] = '\0';
2103 if (alc[len - 2] == ELF_VER_CHR)
2104 alc[len - 2] = '\0';
2106 h->verinfo.vertree = t;
2110 if (t->globals.list != NULL)
2111 d = (*t->match) (&t->globals, NULL, alc);
2113 /* See if there is anything to force this symbol to
2115 if (d == NULL && t->locals.list != NULL)
2117 d = (*t->match) (&t->locals, NULL, alc);
2120 && ! info->export_dynamic)
2121 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2129 /* If we are building an application, we need to create a
2130 version node for this version. */
2131 if (t == NULL && bfd_link_executable (info))
2133 struct bfd_elf_version_tree **pp;
2136 /* If we aren't going to export this symbol, we don't need
2137 to worry about it. */
2138 if (h->dynindx == -1)
2142 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2145 sinfo->failed = TRUE;
2150 t->name_indx = (unsigned int) -1;
2154 /* Don't count anonymous version tag. */
2155 if (sinfo->info->version_info != NULL
2156 && sinfo->info->version_info->vernum == 0)
2158 for (pp = &sinfo->info->version_info;
2162 t->vernum = version_index;
2166 h->verinfo.vertree = t;
2170 /* We could not find the version for a symbol when
2171 generating a shared archive. Return an error. */
2172 (*_bfd_error_handler)
2173 (_("%B: version node not found for symbol %s"),
2174 info->output_bfd, h->root.root.string);
2175 bfd_set_error (bfd_error_bad_value);
2176 sinfo->failed = TRUE;
2181 /* If we don't have a version for this symbol, see if we can find
2183 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2188 = bfd_find_version_for_sym (sinfo->info->version_info,
2189 h->root.root.string, &hide);
2190 if (h->verinfo.vertree != NULL && hide)
2191 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2197 /* Read and swap the relocs from the section indicated by SHDR. This
2198 may be either a REL or a RELA section. The relocations are
2199 translated into RELA relocations and stored in INTERNAL_RELOCS,
2200 which should have already been allocated to contain enough space.
2201 The EXTERNAL_RELOCS are a buffer where the external form of the
2202 relocations should be stored.
2204 Returns FALSE if something goes wrong. */
2207 elf_link_read_relocs_from_section (bfd *abfd,
2209 Elf_Internal_Shdr *shdr,
2210 void *external_relocs,
2211 Elf_Internal_Rela *internal_relocs)
2213 const struct elf_backend_data *bed;
2214 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2215 const bfd_byte *erela;
2216 const bfd_byte *erelaend;
2217 Elf_Internal_Rela *irela;
2218 Elf_Internal_Shdr *symtab_hdr;
2221 /* Position ourselves at the start of the section. */
2222 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2225 /* Read the relocations. */
2226 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2229 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2230 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2232 bed = get_elf_backend_data (abfd);
2234 /* Convert the external relocations to the internal format. */
2235 if (shdr->sh_entsize == bed->s->sizeof_rel)
2236 swap_in = bed->s->swap_reloc_in;
2237 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2238 swap_in = bed->s->swap_reloca_in;
2241 bfd_set_error (bfd_error_wrong_format);
2245 erela = (const bfd_byte *) external_relocs;
2246 erelaend = erela + shdr->sh_size;
2247 irela = internal_relocs;
2248 while (erela < erelaend)
2252 (*swap_in) (abfd, erela, irela);
2253 r_symndx = ELF32_R_SYM (irela->r_info);
2254 if (bed->s->arch_size == 64)
2258 if ((size_t) r_symndx >= nsyms)
2260 (*_bfd_error_handler)
2261 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2262 " for offset 0x%lx in section `%A'"),
2264 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2265 bfd_set_error (bfd_error_bad_value);
2269 else if (r_symndx != STN_UNDEF)
2271 (*_bfd_error_handler)
2272 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2273 " when the object file has no symbol table"),
2275 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2276 bfd_set_error (bfd_error_bad_value);
2279 irela += bed->s->int_rels_per_ext_rel;
2280 erela += shdr->sh_entsize;
2286 /* Read and swap the relocs for a section O. They may have been
2287 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2288 not NULL, they are used as buffers to read into. They are known to
2289 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2290 the return value is allocated using either malloc or bfd_alloc,
2291 according to the KEEP_MEMORY argument. If O has two relocation
2292 sections (both REL and RELA relocations), then the REL_HDR
2293 relocations will appear first in INTERNAL_RELOCS, followed by the
2294 RELA_HDR relocations. */
2297 _bfd_elf_link_read_relocs (bfd *abfd,
2299 void *external_relocs,
2300 Elf_Internal_Rela *internal_relocs,
2301 bfd_boolean keep_memory)
2303 void *alloc1 = NULL;
2304 Elf_Internal_Rela *alloc2 = NULL;
2305 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2306 struct bfd_elf_section_data *esdo = elf_section_data (o);
2307 Elf_Internal_Rela *internal_rela_relocs;
2309 if (esdo->relocs != NULL)
2310 return esdo->relocs;
2312 if (o->reloc_count == 0)
2315 if (internal_relocs == NULL)
2319 size = o->reloc_count;
2320 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2322 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2324 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2325 if (internal_relocs == NULL)
2329 if (external_relocs == NULL)
2331 bfd_size_type size = 0;
2334 size += esdo->rel.hdr->sh_size;
2336 size += esdo->rela.hdr->sh_size;
2338 alloc1 = bfd_malloc (size);
2341 external_relocs = alloc1;
2344 internal_rela_relocs = internal_relocs;
2347 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2351 external_relocs = (((bfd_byte *) external_relocs)
2352 + esdo->rel.hdr->sh_size);
2353 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2354 * bed->s->int_rels_per_ext_rel);
2358 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2360 internal_rela_relocs)))
2363 /* Cache the results for next time, if we can. */
2365 esdo->relocs = internal_relocs;
2370 /* Don't free alloc2, since if it was allocated we are passing it
2371 back (under the name of internal_relocs). */
2373 return internal_relocs;
2381 bfd_release (abfd, alloc2);
2388 /* Compute the size of, and allocate space for, REL_HDR which is the
2389 section header for a section containing relocations for O. */
2392 _bfd_elf_link_size_reloc_section (bfd *abfd,
2393 struct bfd_elf_section_reloc_data *reldata)
2395 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2397 /* That allows us to calculate the size of the section. */
2398 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2400 /* The contents field must last into write_object_contents, so we
2401 allocate it with bfd_alloc rather than malloc. Also since we
2402 cannot be sure that the contents will actually be filled in,
2403 we zero the allocated space. */
2404 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2405 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2408 if (reldata->hashes == NULL && reldata->count)
2410 struct elf_link_hash_entry **p;
2412 p = ((struct elf_link_hash_entry **)
2413 bfd_zmalloc (reldata->count * sizeof (*p)));
2417 reldata->hashes = p;
2423 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2424 originated from the section given by INPUT_REL_HDR) to the
2428 _bfd_elf_link_output_relocs (bfd *output_bfd,
2429 asection *input_section,
2430 Elf_Internal_Shdr *input_rel_hdr,
2431 Elf_Internal_Rela *internal_relocs,
2432 struct elf_link_hash_entry **rel_hash
2435 Elf_Internal_Rela *irela;
2436 Elf_Internal_Rela *irelaend;
2438 struct bfd_elf_section_reloc_data *output_reldata;
2439 asection *output_section;
2440 const struct elf_backend_data *bed;
2441 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2442 struct bfd_elf_section_data *esdo;
2444 output_section = input_section->output_section;
2446 bed = get_elf_backend_data (output_bfd);
2447 esdo = elf_section_data (output_section);
2448 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2450 output_reldata = &esdo->rel;
2451 swap_out = bed->s->swap_reloc_out;
2453 else if (esdo->rela.hdr
2454 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2456 output_reldata = &esdo->rela;
2457 swap_out = bed->s->swap_reloca_out;
2461 (*_bfd_error_handler)
2462 (_("%B: relocation size mismatch in %B section %A"),
2463 output_bfd, input_section->owner, input_section);
2464 bfd_set_error (bfd_error_wrong_format);
2468 erel = output_reldata->hdr->contents;
2469 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2470 irela = internal_relocs;
2471 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2472 * bed->s->int_rels_per_ext_rel);
2473 while (irela < irelaend)
2475 (*swap_out) (output_bfd, irela, erel);
2476 irela += bed->s->int_rels_per_ext_rel;
2477 erel += input_rel_hdr->sh_entsize;
2480 /* Bump the counter, so that we know where to add the next set of
2482 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2487 /* Make weak undefined symbols in PIE dynamic. */
2490 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2491 struct elf_link_hash_entry *h)
2493 if (bfd_link_pie (info)
2495 && h->root.type == bfd_link_hash_undefweak)
2496 return bfd_elf_link_record_dynamic_symbol (info, h);
2501 /* Fix up the flags for a symbol. This handles various cases which
2502 can only be fixed after all the input files are seen. This is
2503 currently called by both adjust_dynamic_symbol and
2504 assign_sym_version, which is unnecessary but perhaps more robust in
2505 the face of future changes. */
2508 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2509 struct elf_info_failed *eif)
2511 const struct elf_backend_data *bed;
2513 /* If this symbol was mentioned in a non-ELF file, try to set
2514 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2515 permit a non-ELF file to correctly refer to a symbol defined in
2516 an ELF dynamic object. */
2519 while (h->root.type == bfd_link_hash_indirect)
2520 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2522 if (h->root.type != bfd_link_hash_defined
2523 && h->root.type != bfd_link_hash_defweak)
2526 h->ref_regular_nonweak = 1;
2530 if (h->root.u.def.section->owner != NULL
2531 && (bfd_get_flavour (h->root.u.def.section->owner)
2532 == bfd_target_elf_flavour))
2535 h->ref_regular_nonweak = 1;
2541 if (h->dynindx == -1
2545 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2554 /* Unfortunately, NON_ELF is only correct if the symbol
2555 was first seen in a non-ELF file. Fortunately, if the symbol
2556 was first seen in an ELF file, we're probably OK unless the
2557 symbol was defined in a non-ELF file. Catch that case here.
2558 FIXME: We're still in trouble if the symbol was first seen in
2559 a dynamic object, and then later in a non-ELF regular object. */
2560 if ((h->root.type == bfd_link_hash_defined
2561 || h->root.type == bfd_link_hash_defweak)
2563 && (h->root.u.def.section->owner != NULL
2564 ? (bfd_get_flavour (h->root.u.def.section->owner)
2565 != bfd_target_elf_flavour)
2566 : (bfd_is_abs_section (h->root.u.def.section)
2567 && !h->def_dynamic)))
2571 /* Backend specific symbol fixup. */
2572 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2573 if (bed->elf_backend_fixup_symbol
2574 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2577 /* If this is a final link, and the symbol was defined as a common
2578 symbol in a regular object file, and there was no definition in
2579 any dynamic object, then the linker will have allocated space for
2580 the symbol in a common section but the DEF_REGULAR
2581 flag will not have been set. */
2582 if (h->root.type == bfd_link_hash_defined
2586 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2589 /* If -Bsymbolic was used (which means to bind references to global
2590 symbols to the definition within the shared object), and this
2591 symbol was defined in a regular object, then it actually doesn't
2592 need a PLT entry. Likewise, if the symbol has non-default
2593 visibility. If the symbol has hidden or internal visibility, we
2594 will force it local. */
2596 && bfd_link_pic (eif->info)
2597 && is_elf_hash_table (eif->info->hash)
2598 && (SYMBOLIC_BIND (eif->info, h)
2599 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2602 bfd_boolean force_local;
2604 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2605 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2606 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2609 /* If a weak undefined symbol has non-default visibility, we also
2610 hide it from the dynamic linker. */
2611 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2612 && h->root.type == bfd_link_hash_undefweak)
2613 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2615 /* If this is a weak defined symbol in a dynamic object, and we know
2616 the real definition in the dynamic object, copy interesting flags
2617 over to the real definition. */
2618 if (h->u.weakdef != NULL)
2620 /* If the real definition is defined by a regular object file,
2621 don't do anything special. See the longer description in
2622 _bfd_elf_adjust_dynamic_symbol, below. */
2623 if (h->u.weakdef->def_regular)
2624 h->u.weakdef = NULL;
2627 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2629 while (h->root.type == bfd_link_hash_indirect)
2630 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2632 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2633 || h->root.type == bfd_link_hash_defweak);
2634 BFD_ASSERT (weakdef->def_dynamic);
2635 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2636 || weakdef->root.type == bfd_link_hash_defweak);
2637 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2644 /* Make the backend pick a good value for a dynamic symbol. This is
2645 called via elf_link_hash_traverse, and also calls itself
2649 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2651 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2653 const struct elf_backend_data *bed;
2655 if (! is_elf_hash_table (eif->info->hash))
2658 /* Ignore indirect symbols. These are added by the versioning code. */
2659 if (h->root.type == bfd_link_hash_indirect)
2662 /* Fix the symbol flags. */
2663 if (! _bfd_elf_fix_symbol_flags (h, eif))
2666 /* If this symbol does not require a PLT entry, and it is not
2667 defined by a dynamic object, or is not referenced by a regular
2668 object, ignore it. We do have to handle a weak defined symbol,
2669 even if no regular object refers to it, if we decided to add it
2670 to the dynamic symbol table. FIXME: Do we normally need to worry
2671 about symbols which are defined by one dynamic object and
2672 referenced by another one? */
2674 && h->type != STT_GNU_IFUNC
2678 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2680 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2684 /* If we've already adjusted this symbol, don't do it again. This
2685 can happen via a recursive call. */
2686 if (h->dynamic_adjusted)
2689 /* Don't look at this symbol again. Note that we must set this
2690 after checking the above conditions, because we may look at a
2691 symbol once, decide not to do anything, and then get called
2692 recursively later after REF_REGULAR is set below. */
2693 h->dynamic_adjusted = 1;
2695 /* If this is a weak definition, and we know a real definition, and
2696 the real symbol is not itself defined by a regular object file,
2697 then get a good value for the real definition. We handle the
2698 real symbol first, for the convenience of the backend routine.
2700 Note that there is a confusing case here. If the real definition
2701 is defined by a regular object file, we don't get the real symbol
2702 from the dynamic object, but we do get the weak symbol. If the
2703 processor backend uses a COPY reloc, then if some routine in the
2704 dynamic object changes the real symbol, we will not see that
2705 change in the corresponding weak symbol. This is the way other
2706 ELF linkers work as well, and seems to be a result of the shared
2709 I will clarify this issue. Most SVR4 shared libraries define the
2710 variable _timezone and define timezone as a weak synonym. The
2711 tzset call changes _timezone. If you write
2712 extern int timezone;
2714 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2715 you might expect that, since timezone is a synonym for _timezone,
2716 the same number will print both times. However, if the processor
2717 backend uses a COPY reloc, then actually timezone will be copied
2718 into your process image, and, since you define _timezone
2719 yourself, _timezone will not. Thus timezone and _timezone will
2720 wind up at different memory locations. The tzset call will set
2721 _timezone, leaving timezone unchanged. */
2723 if (h->u.weakdef != NULL)
2725 /* If we get to this point, there is an implicit reference to
2726 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2727 h->u.weakdef->ref_regular = 1;
2729 /* Ensure that the backend adjust_dynamic_symbol function sees
2730 H->U.WEAKDEF before H by recursively calling ourselves. */
2731 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2735 /* If a symbol has no type and no size and does not require a PLT
2736 entry, then we are probably about to do the wrong thing here: we
2737 are probably going to create a COPY reloc for an empty object.
2738 This case can arise when a shared object is built with assembly
2739 code, and the assembly code fails to set the symbol type. */
2741 && h->type == STT_NOTYPE
2743 (*_bfd_error_handler)
2744 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2745 h->root.root.string);
2747 dynobj = elf_hash_table (eif->info)->dynobj;
2748 bed = get_elf_backend_data (dynobj);
2750 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2759 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2763 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2764 struct elf_link_hash_entry *h,
2767 unsigned int power_of_two;
2769 asection *sec = h->root.u.def.section;
2771 /* The section aligment of definition is the maximum alignment
2772 requirement of symbols defined in the section. Since we don't
2773 know the symbol alignment requirement, we start with the
2774 maximum alignment and check low bits of the symbol address
2775 for the minimum alignment. */
2776 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2777 mask = ((bfd_vma) 1 << power_of_two) - 1;
2778 while ((h->root.u.def.value & mask) != 0)
2784 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2787 /* Adjust the section alignment if needed. */
2788 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2793 /* We make sure that the symbol will be aligned properly. */
2794 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2796 /* Define the symbol as being at this point in DYNBSS. */
2797 h->root.u.def.section = dynbss;
2798 h->root.u.def.value = dynbss->size;
2800 /* Increment the size of DYNBSS to make room for the symbol. */
2801 dynbss->size += h->size;
2803 /* No error if extern_protected_data is true. */
2804 if (h->protected_def
2805 && (!info->extern_protected_data
2806 || (info->extern_protected_data < 0
2807 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2808 info->callbacks->einfo
2809 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2810 h->root.root.string);
2815 /* Adjust all external symbols pointing into SEC_MERGE sections
2816 to reflect the object merging within the sections. */
2819 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2823 if ((h->root.type == bfd_link_hash_defined
2824 || h->root.type == bfd_link_hash_defweak)
2825 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2826 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2828 bfd *output_bfd = (bfd *) data;
2830 h->root.u.def.value =
2831 _bfd_merged_section_offset (output_bfd,
2832 &h->root.u.def.section,
2833 elf_section_data (sec)->sec_info,
2834 h->root.u.def.value);
2840 /* Returns false if the symbol referred to by H should be considered
2841 to resolve local to the current module, and true if it should be
2842 considered to bind dynamically. */
2845 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2846 struct bfd_link_info *info,
2847 bfd_boolean not_local_protected)
2849 bfd_boolean binding_stays_local_p;
2850 const struct elf_backend_data *bed;
2851 struct elf_link_hash_table *hash_table;
2856 while (h->root.type == bfd_link_hash_indirect
2857 || h->root.type == bfd_link_hash_warning)
2858 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2860 /* If it was forced local, then clearly it's not dynamic. */
2861 if (h->dynindx == -1)
2863 if (h->forced_local)
2866 /* Identify the cases where name binding rules say that a
2867 visible symbol resolves locally. */
2868 binding_stays_local_p = (bfd_link_executable (info)
2869 || SYMBOLIC_BIND (info, h));
2871 switch (ELF_ST_VISIBILITY (h->other))
2878 hash_table = elf_hash_table (info);
2879 if (!is_elf_hash_table (hash_table))
2882 bed = get_elf_backend_data (hash_table->dynobj);
2884 /* Proper resolution for function pointer equality may require
2885 that these symbols perhaps be resolved dynamically, even though
2886 we should be resolving them to the current module. */
2887 if (!not_local_protected || !bed->is_function_type (h->type))
2888 binding_stays_local_p = TRUE;
2895 /* If it isn't defined locally, then clearly it's dynamic. */
2896 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2899 /* Otherwise, the symbol is dynamic if binding rules don't tell
2900 us that it remains local. */
2901 return !binding_stays_local_p;
2904 /* Return true if the symbol referred to by H should be considered
2905 to resolve local to the current module, and false otherwise. Differs
2906 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2907 undefined symbols. The two functions are virtually identical except
2908 for the place where forced_local and dynindx == -1 are tested. If
2909 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2910 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2911 the symbol is local only for defined symbols.
2912 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2913 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2914 treatment of undefined weak symbols. For those that do not make
2915 undefined weak symbols dynamic, both functions may return false. */
2918 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2919 struct bfd_link_info *info,
2920 bfd_boolean local_protected)
2922 const struct elf_backend_data *bed;
2923 struct elf_link_hash_table *hash_table;
2925 /* If it's a local sym, of course we resolve locally. */
2929 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2930 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2931 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2934 /* Common symbols that become definitions don't get the DEF_REGULAR
2935 flag set, so test it first, and don't bail out. */
2936 if (ELF_COMMON_DEF_P (h))
2938 /* If we don't have a definition in a regular file, then we can't
2939 resolve locally. The sym is either undefined or dynamic. */
2940 else if (!h->def_regular)
2943 /* Forced local symbols resolve locally. */
2944 if (h->forced_local)
2947 /* As do non-dynamic symbols. */
2948 if (h->dynindx == -1)
2951 /* At this point, we know the symbol is defined and dynamic. In an
2952 executable it must resolve locally, likewise when building symbolic
2953 shared libraries. */
2954 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
2957 /* Now deal with defined dynamic symbols in shared libraries. Ones
2958 with default visibility might not resolve locally. */
2959 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2962 hash_table = elf_hash_table (info);
2963 if (!is_elf_hash_table (hash_table))
2966 bed = get_elf_backend_data (hash_table->dynobj);
2968 /* If extern_protected_data is false, STV_PROTECTED non-function
2969 symbols are local. */
2970 if ((!info->extern_protected_data
2971 || (info->extern_protected_data < 0
2972 && !bed->extern_protected_data))
2973 && !bed->is_function_type (h->type))
2976 /* Function pointer equality tests may require that STV_PROTECTED
2977 symbols be treated as dynamic symbols. If the address of a
2978 function not defined in an executable is set to that function's
2979 plt entry in the executable, then the address of the function in
2980 a shared library must also be the plt entry in the executable. */
2981 return local_protected;
2984 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2985 aligned. Returns the first TLS output section. */
2987 struct bfd_section *
2988 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2990 struct bfd_section *sec, *tls;
2991 unsigned int align = 0;
2993 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2994 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2998 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2999 if (sec->alignment_power > align)
3000 align = sec->alignment_power;
3002 elf_hash_table (info)->tls_sec = tls;
3004 /* Ensure the alignment of the first section is the largest alignment,
3005 so that the tls segment starts aligned. */
3007 tls->alignment_power = align;
3012 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
3014 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
3015 Elf_Internal_Sym *sym)
3017 const struct elf_backend_data *bed;
3019 /* Local symbols do not count, but target specific ones might. */
3020 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
3021 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
3024 bed = get_elf_backend_data (abfd);
3025 /* Function symbols do not count. */
3026 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
3029 /* If the section is undefined, then so is the symbol. */
3030 if (sym->st_shndx == SHN_UNDEF)
3033 /* If the symbol is defined in the common section, then
3034 it is a common definition and so does not count. */
3035 if (bed->common_definition (sym))
3038 /* If the symbol is in a target specific section then we
3039 must rely upon the backend to tell us what it is. */
3040 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
3041 /* FIXME - this function is not coded yet:
3043 return _bfd_is_global_symbol_definition (abfd, sym);
3045 Instead for now assume that the definition is not global,
3046 Even if this is wrong, at least the linker will behave
3047 in the same way that it used to do. */
3053 /* Search the symbol table of the archive element of the archive ABFD
3054 whose archive map contains a mention of SYMDEF, and determine if
3055 the symbol is defined in this element. */
3057 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
3059 Elf_Internal_Shdr * hdr;
3060 bfd_size_type symcount;
3061 bfd_size_type extsymcount;
3062 bfd_size_type extsymoff;
3063 Elf_Internal_Sym *isymbuf;
3064 Elf_Internal_Sym *isym;
3065 Elf_Internal_Sym *isymend;
3068 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
3072 /* Return FALSE if the object has been claimed by plugin. */
3073 if (abfd->plugin_format == bfd_plugin_yes)
3076 if (! bfd_check_format (abfd, bfd_object))
3079 /* Select the appropriate symbol table. */
3080 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
3081 hdr = &elf_tdata (abfd)->symtab_hdr;
3083 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3085 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3087 /* The sh_info field of the symtab header tells us where the
3088 external symbols start. We don't care about the local symbols. */
3089 if (elf_bad_symtab (abfd))
3091 extsymcount = symcount;
3096 extsymcount = symcount - hdr->sh_info;
3097 extsymoff = hdr->sh_info;
3100 if (extsymcount == 0)
3103 /* Read in the symbol table. */
3104 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3106 if (isymbuf == NULL)
3109 /* Scan the symbol table looking for SYMDEF. */
3111 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3115 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3120 if (strcmp (name, symdef->name) == 0)
3122 result = is_global_data_symbol_definition (abfd, isym);
3132 /* Add an entry to the .dynamic table. */
3135 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3139 struct elf_link_hash_table *hash_table;
3140 const struct elf_backend_data *bed;
3142 bfd_size_type newsize;
3143 bfd_byte *newcontents;
3144 Elf_Internal_Dyn dyn;
3146 hash_table = elf_hash_table (info);
3147 if (! is_elf_hash_table (hash_table))
3150 bed = get_elf_backend_data (hash_table->dynobj);
3151 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3152 BFD_ASSERT (s != NULL);
3154 newsize = s->size + bed->s->sizeof_dyn;
3155 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3156 if (newcontents == NULL)
3160 dyn.d_un.d_val = val;
3161 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3164 s->contents = newcontents;
3169 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3170 otherwise just check whether one already exists. Returns -1 on error,
3171 1 if a DT_NEEDED tag already exists, and 0 on success. */
3174 elf_add_dt_needed_tag (bfd *abfd,
3175 struct bfd_link_info *info,
3179 struct elf_link_hash_table *hash_table;
3180 bfd_size_type strindex;
3182 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3185 hash_table = elf_hash_table (info);
3186 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3187 if (strindex == (bfd_size_type) -1)
3190 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3193 const struct elf_backend_data *bed;
3196 bed = get_elf_backend_data (hash_table->dynobj);
3197 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3199 for (extdyn = sdyn->contents;
3200 extdyn < sdyn->contents + sdyn->size;
3201 extdyn += bed->s->sizeof_dyn)
3203 Elf_Internal_Dyn dyn;
3205 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3206 if (dyn.d_tag == DT_NEEDED
3207 && dyn.d_un.d_val == strindex)
3209 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3217 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3220 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3224 /* We were just checking for existence of the tag. */
3225 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3231 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3233 for (; needed != NULL; needed = needed->next)
3234 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3235 && strcmp (soname, needed->name) == 0)
3241 /* Sort symbol by value, section, and size. */
3243 elf_sort_symbol (const void *arg1, const void *arg2)
3245 const struct elf_link_hash_entry *h1;
3246 const struct elf_link_hash_entry *h2;
3247 bfd_signed_vma vdiff;
3249 h1 = *(const struct elf_link_hash_entry **) arg1;
3250 h2 = *(const struct elf_link_hash_entry **) arg2;
3251 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3253 return vdiff > 0 ? 1 : -1;
3256 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3258 return sdiff > 0 ? 1 : -1;
3260 vdiff = h1->size - h2->size;
3261 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3264 /* This function is used to adjust offsets into .dynstr for
3265 dynamic symbols. This is called via elf_link_hash_traverse. */
3268 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3270 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3272 if (h->dynindx != -1)
3273 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3277 /* Assign string offsets in .dynstr, update all structures referencing
3281 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3283 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3284 struct elf_link_local_dynamic_entry *entry;
3285 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3286 bfd *dynobj = hash_table->dynobj;
3289 const struct elf_backend_data *bed;
3292 _bfd_elf_strtab_finalize (dynstr);
3293 size = _bfd_elf_strtab_size (dynstr);
3295 bed = get_elf_backend_data (dynobj);
3296 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3297 BFD_ASSERT (sdyn != NULL);
3299 /* Update all .dynamic entries referencing .dynstr strings. */
3300 for (extdyn = sdyn->contents;
3301 extdyn < sdyn->contents + sdyn->size;
3302 extdyn += bed->s->sizeof_dyn)
3304 Elf_Internal_Dyn dyn;
3306 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3310 dyn.d_un.d_val = size;
3320 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3325 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3328 /* Now update local dynamic symbols. */
3329 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3330 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3331 entry->isym.st_name);
3333 /* And the rest of dynamic symbols. */
3334 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3336 /* Adjust version definitions. */
3337 if (elf_tdata (output_bfd)->cverdefs)
3342 Elf_Internal_Verdef def;
3343 Elf_Internal_Verdaux defaux;
3345 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3349 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3351 p += sizeof (Elf_External_Verdef);
3352 if (def.vd_aux != sizeof (Elf_External_Verdef))
3354 for (i = 0; i < def.vd_cnt; ++i)
3356 _bfd_elf_swap_verdaux_in (output_bfd,
3357 (Elf_External_Verdaux *) p, &defaux);
3358 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3360 _bfd_elf_swap_verdaux_out (output_bfd,
3361 &defaux, (Elf_External_Verdaux *) p);
3362 p += sizeof (Elf_External_Verdaux);
3365 while (def.vd_next);
3368 /* Adjust version references. */
3369 if (elf_tdata (output_bfd)->verref)
3374 Elf_Internal_Verneed need;
3375 Elf_Internal_Vernaux needaux;
3377 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3381 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3383 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3384 _bfd_elf_swap_verneed_out (output_bfd, &need,
3385 (Elf_External_Verneed *) p);
3386 p += sizeof (Elf_External_Verneed);
3387 for (i = 0; i < need.vn_cnt; ++i)
3389 _bfd_elf_swap_vernaux_in (output_bfd,
3390 (Elf_External_Vernaux *) p, &needaux);
3391 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3393 _bfd_elf_swap_vernaux_out (output_bfd,
3395 (Elf_External_Vernaux *) p);
3396 p += sizeof (Elf_External_Vernaux);
3399 while (need.vn_next);
3405 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3406 The default is to only match when the INPUT and OUTPUT are exactly
3410 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3411 const bfd_target *output)
3413 return input == output;
3416 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3417 This version is used when different targets for the same architecture
3418 are virtually identical. */
3421 _bfd_elf_relocs_compatible (const bfd_target *input,
3422 const bfd_target *output)
3424 const struct elf_backend_data *obed, *ibed;
3426 if (input == output)
3429 ibed = xvec_get_elf_backend_data (input);
3430 obed = xvec_get_elf_backend_data (output);
3432 if (ibed->arch != obed->arch)
3435 /* If both backends are using this function, deem them compatible. */
3436 return ibed->relocs_compatible == obed->relocs_compatible;
3439 /* Make a special call to the linker "notice" function to tell it that
3440 we are about to handle an as-needed lib, or have finished
3441 processing the lib. */
3444 _bfd_elf_notice_as_needed (bfd *ibfd,
3445 struct bfd_link_info *info,
3446 enum notice_asneeded_action act)
3448 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3451 /* Add symbols from an ELF object file to the linker hash table. */
3454 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3456 Elf_Internal_Ehdr *ehdr;
3457 Elf_Internal_Shdr *hdr;
3458 bfd_size_type symcount;
3459 bfd_size_type extsymcount;
3460 bfd_size_type extsymoff;
3461 struct elf_link_hash_entry **sym_hash;
3462 bfd_boolean dynamic;
3463 Elf_External_Versym *extversym = NULL;
3464 Elf_External_Versym *ever;
3465 struct elf_link_hash_entry *weaks;
3466 struct elf_link_hash_entry **nondeflt_vers = NULL;
3467 bfd_size_type nondeflt_vers_cnt = 0;
3468 Elf_Internal_Sym *isymbuf = NULL;
3469 Elf_Internal_Sym *isym;
3470 Elf_Internal_Sym *isymend;
3471 const struct elf_backend_data *bed;
3472 bfd_boolean add_needed;
3473 struct elf_link_hash_table *htab;
3475 void *alloc_mark = NULL;
3476 struct bfd_hash_entry **old_table = NULL;
3477 unsigned int old_size = 0;
3478 unsigned int old_count = 0;
3479 void *old_tab = NULL;
3481 struct bfd_link_hash_entry *old_undefs = NULL;
3482 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3483 long old_dynsymcount = 0;
3484 bfd_size_type old_dynstr_size = 0;
3487 bfd_boolean just_syms;
3489 htab = elf_hash_table (info);
3490 bed = get_elf_backend_data (abfd);
3492 if ((abfd->flags & DYNAMIC) == 0)
3498 /* You can't use -r against a dynamic object. Also, there's no
3499 hope of using a dynamic object which does not exactly match
3500 the format of the output file. */
3501 if (bfd_link_relocatable (info)
3502 || !is_elf_hash_table (htab)
3503 || info->output_bfd->xvec != abfd->xvec)
3505 if (bfd_link_relocatable (info))
3506 bfd_set_error (bfd_error_invalid_operation);
3508 bfd_set_error (bfd_error_wrong_format);
3513 ehdr = elf_elfheader (abfd);
3514 if (info->warn_alternate_em
3515 && bed->elf_machine_code != ehdr->e_machine
3516 && ((bed->elf_machine_alt1 != 0
3517 && ehdr->e_machine == bed->elf_machine_alt1)
3518 || (bed->elf_machine_alt2 != 0
3519 && ehdr->e_machine == bed->elf_machine_alt2)))
3520 info->callbacks->einfo
3521 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3522 ehdr->e_machine, abfd, bed->elf_machine_code);
3524 /* As a GNU extension, any input sections which are named
3525 .gnu.warning.SYMBOL are treated as warning symbols for the given
3526 symbol. This differs from .gnu.warning sections, which generate
3527 warnings when they are included in an output file. */
3528 /* PR 12761: Also generate this warning when building shared libraries. */
3529 for (s = abfd->sections; s != NULL; s = s->next)
3533 name = bfd_get_section_name (abfd, s);
3534 if (CONST_STRNEQ (name, ".gnu.warning."))
3539 name += sizeof ".gnu.warning." - 1;
3541 /* If this is a shared object, then look up the symbol
3542 in the hash table. If it is there, and it is already
3543 been defined, then we will not be using the entry
3544 from this shared object, so we don't need to warn.
3545 FIXME: If we see the definition in a regular object
3546 later on, we will warn, but we shouldn't. The only
3547 fix is to keep track of what warnings we are supposed
3548 to emit, and then handle them all at the end of the
3552 struct elf_link_hash_entry *h;
3554 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3556 /* FIXME: What about bfd_link_hash_common? */
3558 && (h->root.type == bfd_link_hash_defined
3559 || h->root.type == bfd_link_hash_defweak))
3564 msg = (char *) bfd_alloc (abfd, sz + 1);
3568 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3573 if (! (_bfd_generic_link_add_one_symbol
3574 (info, abfd, name, BSF_WARNING, s, 0, msg,
3575 FALSE, bed->collect, NULL)))
3578 if (bfd_link_executable (info))
3580 /* Clobber the section size so that the warning does
3581 not get copied into the output file. */
3584 /* Also set SEC_EXCLUDE, so that symbols defined in
3585 the warning section don't get copied to the output. */
3586 s->flags |= SEC_EXCLUDE;
3591 just_syms = ((s = abfd->sections) != NULL
3592 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3597 /* If we are creating a shared library, create all the dynamic
3598 sections immediately. We need to attach them to something,
3599 so we attach them to this BFD, provided it is the right
3600 format and is not from ld --just-symbols. Always create the
3601 dynamic sections for -E/--dynamic-list. FIXME: If there
3602 are no input BFD's of the same format as the output, we can't
3603 make a shared library. */
3605 && (bfd_link_pic (info)
3606 || info->export_dynamic
3608 && is_elf_hash_table (htab)
3609 && info->output_bfd->xvec == abfd->xvec
3610 && !htab->dynamic_sections_created)
3612 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3616 else if (!is_elf_hash_table (htab))
3620 const char *soname = NULL;
3622 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3625 /* ld --just-symbols and dynamic objects don't mix very well.
3626 ld shouldn't allow it. */
3630 /* If this dynamic lib was specified on the command line with
3631 --as-needed in effect, then we don't want to add a DT_NEEDED
3632 tag unless the lib is actually used. Similary for libs brought
3633 in by another lib's DT_NEEDED. When --no-add-needed is used
3634 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3635 any dynamic library in DT_NEEDED tags in the dynamic lib at
3637 add_needed = (elf_dyn_lib_class (abfd)
3638 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3639 | DYN_NO_NEEDED)) == 0;
3641 s = bfd_get_section_by_name (abfd, ".dynamic");
3646 unsigned int elfsec;
3647 unsigned long shlink;
3649 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3656 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3657 if (elfsec == SHN_BAD)
3658 goto error_free_dyn;
3659 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3661 for (extdyn = dynbuf;
3662 extdyn < dynbuf + s->size;
3663 extdyn += bed->s->sizeof_dyn)
3665 Elf_Internal_Dyn dyn;
3667 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3668 if (dyn.d_tag == DT_SONAME)
3670 unsigned int tagv = dyn.d_un.d_val;
3671 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3673 goto error_free_dyn;
3675 if (dyn.d_tag == DT_NEEDED)
3677 struct bfd_link_needed_list *n, **pn;
3679 unsigned int tagv = dyn.d_un.d_val;
3681 amt = sizeof (struct bfd_link_needed_list);
3682 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3683 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3684 if (n == NULL || fnm == NULL)
3685 goto error_free_dyn;
3686 amt = strlen (fnm) + 1;
3687 anm = (char *) bfd_alloc (abfd, amt);
3689 goto error_free_dyn;
3690 memcpy (anm, fnm, amt);
3694 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3698 if (dyn.d_tag == DT_RUNPATH)
3700 struct bfd_link_needed_list *n, **pn;
3702 unsigned int tagv = dyn.d_un.d_val;
3704 amt = sizeof (struct bfd_link_needed_list);
3705 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3706 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3707 if (n == NULL || fnm == NULL)
3708 goto error_free_dyn;
3709 amt = strlen (fnm) + 1;
3710 anm = (char *) bfd_alloc (abfd, amt);
3712 goto error_free_dyn;
3713 memcpy (anm, fnm, amt);
3717 for (pn = & runpath;
3723 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3724 if (!runpath && dyn.d_tag == DT_RPATH)
3726 struct bfd_link_needed_list *n, **pn;
3728 unsigned int tagv = dyn.d_un.d_val;
3730 amt = sizeof (struct bfd_link_needed_list);
3731 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3732 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3733 if (n == NULL || fnm == NULL)
3734 goto error_free_dyn;
3735 amt = strlen (fnm) + 1;
3736 anm = (char *) bfd_alloc (abfd, amt);
3738 goto error_free_dyn;
3739 memcpy (anm, fnm, amt);
3749 if (dyn.d_tag == DT_AUDIT)
3751 unsigned int tagv = dyn.d_un.d_val;
3752 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3759 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3760 frees all more recently bfd_alloc'd blocks as well. */
3766 struct bfd_link_needed_list **pn;
3767 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3772 /* We do not want to include any of the sections in a dynamic
3773 object in the output file. We hack by simply clobbering the
3774 list of sections in the BFD. This could be handled more
3775 cleanly by, say, a new section flag; the existing
3776 SEC_NEVER_LOAD flag is not the one we want, because that one
3777 still implies that the section takes up space in the output
3779 bfd_section_list_clear (abfd);
3781 /* Find the name to use in a DT_NEEDED entry that refers to this
3782 object. If the object has a DT_SONAME entry, we use it.
3783 Otherwise, if the generic linker stuck something in
3784 elf_dt_name, we use that. Otherwise, we just use the file
3786 if (soname == NULL || *soname == '\0')
3788 soname = elf_dt_name (abfd);
3789 if (soname == NULL || *soname == '\0')
3790 soname = bfd_get_filename (abfd);
3793 /* Save the SONAME because sometimes the linker emulation code
3794 will need to know it. */
3795 elf_dt_name (abfd) = soname;
3797 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3801 /* If we have already included this dynamic object in the
3802 link, just ignore it. There is no reason to include a
3803 particular dynamic object more than once. */
3807 /* Save the DT_AUDIT entry for the linker emulation code. */
3808 elf_dt_audit (abfd) = audit;
3811 /* If this is a dynamic object, we always link against the .dynsym
3812 symbol table, not the .symtab symbol table. The dynamic linker
3813 will only see the .dynsym symbol table, so there is no reason to
3814 look at .symtab for a dynamic object. */
3816 if (! dynamic || elf_dynsymtab (abfd) == 0)
3817 hdr = &elf_tdata (abfd)->symtab_hdr;
3819 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3821 symcount = hdr->sh_size / bed->s->sizeof_sym;
3823 /* The sh_info field of the symtab header tells us where the
3824 external symbols start. We don't care about the local symbols at
3826 if (elf_bad_symtab (abfd))
3828 extsymcount = symcount;
3833 extsymcount = symcount - hdr->sh_info;
3834 extsymoff = hdr->sh_info;
3837 sym_hash = elf_sym_hashes (abfd);
3838 if (extsymcount != 0)
3840 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3842 if (isymbuf == NULL)
3845 if (sym_hash == NULL)
3847 /* We store a pointer to the hash table entry for each
3849 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3850 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3851 if (sym_hash == NULL)
3852 goto error_free_sym;
3853 elf_sym_hashes (abfd) = sym_hash;
3859 /* Read in any version definitions. */
3860 if (!_bfd_elf_slurp_version_tables (abfd,
3861 info->default_imported_symver))
3862 goto error_free_sym;
3864 /* Read in the symbol versions, but don't bother to convert them
3865 to internal format. */
3866 if (elf_dynversym (abfd) != 0)
3868 Elf_Internal_Shdr *versymhdr;
3870 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3871 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3872 if (extversym == NULL)
3873 goto error_free_sym;
3874 amt = versymhdr->sh_size;
3875 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3876 || bfd_bread (extversym, amt, abfd) != amt)
3877 goto error_free_vers;
3881 /* If we are loading an as-needed shared lib, save the symbol table
3882 state before we start adding symbols. If the lib turns out
3883 to be unneeded, restore the state. */
3884 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3889 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3891 struct bfd_hash_entry *p;
3892 struct elf_link_hash_entry *h;
3894 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3896 h = (struct elf_link_hash_entry *) p;
3897 entsize += htab->root.table.entsize;
3898 if (h->root.type == bfd_link_hash_warning)
3899 entsize += htab->root.table.entsize;
3903 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3904 old_tab = bfd_malloc (tabsize + entsize);
3905 if (old_tab == NULL)
3906 goto error_free_vers;
3908 /* Remember the current objalloc pointer, so that all mem for
3909 symbols added can later be reclaimed. */
3910 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3911 if (alloc_mark == NULL)
3912 goto error_free_vers;
3914 /* Make a special call to the linker "notice" function to
3915 tell it that we are about to handle an as-needed lib. */
3916 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3917 goto error_free_vers;
3919 /* Clone the symbol table. Remember some pointers into the
3920 symbol table, and dynamic symbol count. */
3921 old_ent = (char *) old_tab + tabsize;
3922 memcpy (old_tab, htab->root.table.table, tabsize);
3923 old_undefs = htab->root.undefs;
3924 old_undefs_tail = htab->root.undefs_tail;
3925 old_table = htab->root.table.table;
3926 old_size = htab->root.table.size;
3927 old_count = htab->root.table.count;
3928 old_dynsymcount = htab->dynsymcount;
3929 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3931 for (i = 0; i < htab->root.table.size; i++)
3933 struct bfd_hash_entry *p;
3934 struct elf_link_hash_entry *h;
3936 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3938 memcpy (old_ent, p, htab->root.table.entsize);
3939 old_ent = (char *) old_ent + htab->root.table.entsize;
3940 h = (struct elf_link_hash_entry *) p;
3941 if (h->root.type == bfd_link_hash_warning)
3943 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3944 old_ent = (char *) old_ent + htab->root.table.entsize;
3951 ever = extversym != NULL ? extversym + extsymoff : NULL;
3952 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3954 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3958 asection *sec, *new_sec;
3961 struct elf_link_hash_entry *h;
3962 struct elf_link_hash_entry *hi;
3963 bfd_boolean definition;
3964 bfd_boolean size_change_ok;
3965 bfd_boolean type_change_ok;
3966 bfd_boolean new_weakdef;
3967 bfd_boolean new_weak;
3968 bfd_boolean old_weak;
3969 bfd_boolean override;
3971 unsigned int old_alignment;
3973 bfd_boolean matched;
3977 flags = BSF_NO_FLAGS;
3979 value = isym->st_value;
3980 common = bed->common_definition (isym);
3982 bind = ELF_ST_BIND (isym->st_info);
3986 /* This should be impossible, since ELF requires that all
3987 global symbols follow all local symbols, and that sh_info
3988 point to the first global symbol. Unfortunately, Irix 5
3993 if (isym->st_shndx != SHN_UNDEF && !common)
4001 case STB_GNU_UNIQUE:
4002 flags = BSF_GNU_UNIQUE;
4006 /* Leave it up to the processor backend. */
4010 if (isym->st_shndx == SHN_UNDEF)
4011 sec = bfd_und_section_ptr;
4012 else if (isym->st_shndx == SHN_ABS)
4013 sec = bfd_abs_section_ptr;
4014 else if (isym->st_shndx == SHN_COMMON)
4016 sec = bfd_com_section_ptr;
4017 /* What ELF calls the size we call the value. What ELF
4018 calls the value we call the alignment. */
4019 value = isym->st_size;
4023 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4025 sec = bfd_abs_section_ptr;
4026 else if (discarded_section (sec))
4028 /* Symbols from discarded section are undefined. We keep
4030 sec = bfd_und_section_ptr;
4031 isym->st_shndx = SHN_UNDEF;
4033 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
4037 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4040 goto error_free_vers;
4042 if (isym->st_shndx == SHN_COMMON
4043 && (abfd->flags & BFD_PLUGIN) != 0)
4045 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
4049 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
4051 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
4053 goto error_free_vers;
4057 else if (isym->st_shndx == SHN_COMMON
4058 && ELF_ST_TYPE (isym->st_info) == STT_TLS
4059 && !bfd_link_relocatable (info))
4061 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
4065 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
4066 | SEC_LINKER_CREATED);
4067 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
4069 goto error_free_vers;
4073 else if (bed->elf_add_symbol_hook)
4075 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
4077 goto error_free_vers;
4079 /* The hook function sets the name to NULL if this symbol
4080 should be skipped for some reason. */
4085 /* Sanity check that all possibilities were handled. */
4088 bfd_set_error (bfd_error_bad_value);
4089 goto error_free_vers;
4092 /* Silently discard TLS symbols from --just-syms. There's
4093 no way to combine a static TLS block with a new TLS block
4094 for this executable. */
4095 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4096 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4099 if (bfd_is_und_section (sec)
4100 || bfd_is_com_section (sec))
4105 size_change_ok = FALSE;
4106 type_change_ok = bed->type_change_ok;
4113 if (is_elf_hash_table (htab))
4115 Elf_Internal_Versym iver;
4116 unsigned int vernum = 0;
4121 if (info->default_imported_symver)
4122 /* Use the default symbol version created earlier. */
4123 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4128 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4130 vernum = iver.vs_vers & VERSYM_VERSION;
4132 /* If this is a hidden symbol, or if it is not version
4133 1, we append the version name to the symbol name.
4134 However, we do not modify a non-hidden absolute symbol
4135 if it is not a function, because it might be the version
4136 symbol itself. FIXME: What if it isn't? */
4137 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4139 && (!bfd_is_abs_section (sec)
4140 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4143 size_t namelen, verlen, newlen;
4146 if (isym->st_shndx != SHN_UNDEF)
4148 if (vernum > elf_tdata (abfd)->cverdefs)
4150 else if (vernum > 1)
4152 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4158 (*_bfd_error_handler)
4159 (_("%B: %s: invalid version %u (max %d)"),
4161 elf_tdata (abfd)->cverdefs);
4162 bfd_set_error (bfd_error_bad_value);
4163 goto error_free_vers;
4168 /* We cannot simply test for the number of
4169 entries in the VERNEED section since the
4170 numbers for the needed versions do not start
4172 Elf_Internal_Verneed *t;
4175 for (t = elf_tdata (abfd)->verref;
4179 Elf_Internal_Vernaux *a;
4181 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4183 if (a->vna_other == vernum)
4185 verstr = a->vna_nodename;
4194 (*_bfd_error_handler)
4195 (_("%B: %s: invalid needed version %d"),
4196 abfd, name, vernum);
4197 bfd_set_error (bfd_error_bad_value);
4198 goto error_free_vers;
4202 namelen = strlen (name);
4203 verlen = strlen (verstr);
4204 newlen = namelen + verlen + 2;
4205 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4206 && isym->st_shndx != SHN_UNDEF)
4209 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4210 if (newname == NULL)
4211 goto error_free_vers;
4212 memcpy (newname, name, namelen);
4213 p = newname + namelen;
4215 /* If this is a defined non-hidden version symbol,
4216 we add another @ to the name. This indicates the
4217 default version of the symbol. */
4218 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4219 && isym->st_shndx != SHN_UNDEF)
4221 memcpy (p, verstr, verlen + 1);
4226 /* If this symbol has default visibility and the user has
4227 requested we not re-export it, then mark it as hidden. */
4228 if (!bfd_is_und_section (sec)
4231 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4232 isym->st_other = (STV_HIDDEN
4233 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4235 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4236 sym_hash, &old_bfd, &old_weak,
4237 &old_alignment, &skip, &override,
4238 &type_change_ok, &size_change_ok,
4240 goto error_free_vers;
4245 /* Override a definition only if the new symbol matches the
4247 if (override && matched)
4251 while (h->root.type == bfd_link_hash_indirect
4252 || h->root.type == bfd_link_hash_warning)
4253 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4255 if (elf_tdata (abfd)->verdef != NULL
4258 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4261 if (! (_bfd_generic_link_add_one_symbol
4262 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4263 (struct bfd_link_hash_entry **) sym_hash)))
4264 goto error_free_vers;
4267 /* We need to make sure that indirect symbol dynamic flags are
4270 while (h->root.type == bfd_link_hash_indirect
4271 || h->root.type == bfd_link_hash_warning)
4272 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4276 new_weak = (flags & BSF_WEAK) != 0;
4277 new_weakdef = FALSE;
4281 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4282 && is_elf_hash_table (htab)
4283 && h->u.weakdef == NULL)
4285 /* Keep a list of all weak defined non function symbols from
4286 a dynamic object, using the weakdef field. Later in this
4287 function we will set the weakdef field to the correct
4288 value. We only put non-function symbols from dynamic
4289 objects on this list, because that happens to be the only
4290 time we need to know the normal symbol corresponding to a
4291 weak symbol, and the information is time consuming to
4292 figure out. If the weakdef field is not already NULL,
4293 then this symbol was already defined by some previous
4294 dynamic object, and we will be using that previous
4295 definition anyhow. */
4297 h->u.weakdef = weaks;
4302 /* Set the alignment of a common symbol. */
4303 if ((common || bfd_is_com_section (sec))
4304 && h->root.type == bfd_link_hash_common)
4309 align = bfd_log2 (isym->st_value);
4312 /* The new symbol is a common symbol in a shared object.
4313 We need to get the alignment from the section. */
4314 align = new_sec->alignment_power;
4316 if (align > old_alignment)
4317 h->root.u.c.p->alignment_power = align;
4319 h->root.u.c.p->alignment_power = old_alignment;
4322 if (is_elf_hash_table (htab))
4324 /* Set a flag in the hash table entry indicating the type of
4325 reference or definition we just found. A dynamic symbol
4326 is one which is referenced or defined by both a regular
4327 object and a shared object. */
4328 bfd_boolean dynsym = FALSE;
4330 /* Plugin symbols aren't normal. Don't set def_regular or
4331 ref_regular for them, or make them dynamic. */
4332 if ((abfd->flags & BFD_PLUGIN) != 0)
4339 if (bind != STB_WEAK)
4340 h->ref_regular_nonweak = 1;
4352 /* If the indirect symbol has been forced local, don't
4353 make the real symbol dynamic. */
4354 if ((h == hi || !hi->forced_local)
4355 && (bfd_link_dll (info)
4365 hi->ref_dynamic = 1;
4370 hi->def_dynamic = 1;
4373 /* If the indirect symbol has been forced local, don't
4374 make the real symbol dynamic. */
4375 if ((h == hi || !hi->forced_local)
4378 || (h->u.weakdef != NULL
4380 && h->u.weakdef->dynindx != -1)))
4384 /* Check to see if we need to add an indirect symbol for
4385 the default name. */
4387 || (!override && h->root.type == bfd_link_hash_common))
4388 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4389 sec, value, &old_bfd, &dynsym))
4390 goto error_free_vers;
4392 /* Check the alignment when a common symbol is involved. This
4393 can change when a common symbol is overridden by a normal
4394 definition or a common symbol is ignored due to the old
4395 normal definition. We need to make sure the maximum
4396 alignment is maintained. */
4397 if ((old_alignment || common)
4398 && h->root.type != bfd_link_hash_common)
4400 unsigned int common_align;
4401 unsigned int normal_align;
4402 unsigned int symbol_align;
4406 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4407 || h->root.type == bfd_link_hash_defweak);
4409 symbol_align = ffs (h->root.u.def.value) - 1;
4410 if (h->root.u.def.section->owner != NULL
4411 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4413 normal_align = h->root.u.def.section->alignment_power;
4414 if (normal_align > symbol_align)
4415 normal_align = symbol_align;
4418 normal_align = symbol_align;
4422 common_align = old_alignment;
4423 common_bfd = old_bfd;
4428 common_align = bfd_log2 (isym->st_value);
4430 normal_bfd = old_bfd;
4433 if (normal_align < common_align)
4435 /* PR binutils/2735 */
4436 if (normal_bfd == NULL)
4437 (*_bfd_error_handler)
4438 (_("Warning: alignment %u of common symbol `%s' in %B is"
4439 " greater than the alignment (%u) of its section %A"),
4440 common_bfd, h->root.u.def.section,
4441 1 << common_align, name, 1 << normal_align);
4443 (*_bfd_error_handler)
4444 (_("Warning: alignment %u of symbol `%s' in %B"
4445 " is smaller than %u in %B"),
4446 normal_bfd, common_bfd,
4447 1 << normal_align, name, 1 << common_align);
4451 /* Remember the symbol size if it isn't undefined. */
4452 if (isym->st_size != 0
4453 && isym->st_shndx != SHN_UNDEF
4454 && (definition || h->size == 0))
4457 && h->size != isym->st_size
4458 && ! size_change_ok)
4459 (*_bfd_error_handler)
4460 (_("Warning: size of symbol `%s' changed"
4461 " from %lu in %B to %lu in %B"),
4463 name, (unsigned long) h->size,
4464 (unsigned long) isym->st_size);
4466 h->size = isym->st_size;
4469 /* If this is a common symbol, then we always want H->SIZE
4470 to be the size of the common symbol. The code just above
4471 won't fix the size if a common symbol becomes larger. We
4472 don't warn about a size change here, because that is
4473 covered by --warn-common. Allow changes between different
4475 if (h->root.type == bfd_link_hash_common)
4476 h->size = h->root.u.c.size;
4478 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4479 && ((definition && !new_weak)
4480 || (old_weak && h->root.type == bfd_link_hash_common)
4481 || h->type == STT_NOTYPE))
4483 unsigned int type = ELF_ST_TYPE (isym->st_info);
4485 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4487 if (type == STT_GNU_IFUNC
4488 && (abfd->flags & DYNAMIC) != 0)
4491 if (h->type != type)
4493 if (h->type != STT_NOTYPE && ! type_change_ok)
4494 (*_bfd_error_handler)
4495 (_("Warning: type of symbol `%s' changed"
4496 " from %d to %d in %B"),
4497 abfd, name, h->type, type);
4503 /* Merge st_other field. */
4504 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4506 /* We don't want to make debug symbol dynamic. */
4508 && (sec->flags & SEC_DEBUGGING)
4509 && !bfd_link_relocatable (info))
4512 /* Nor should we make plugin symbols dynamic. */
4513 if ((abfd->flags & BFD_PLUGIN) != 0)
4518 h->target_internal = isym->st_target_internal;
4519 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4522 if (definition && !dynamic)
4524 char *p = strchr (name, ELF_VER_CHR);
4525 if (p != NULL && p[1] != ELF_VER_CHR)
4527 /* Queue non-default versions so that .symver x, x@FOO
4528 aliases can be checked. */
4531 amt = ((isymend - isym + 1)
4532 * sizeof (struct elf_link_hash_entry *));
4534 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4536 goto error_free_vers;
4538 nondeflt_vers[nondeflt_vers_cnt++] = h;
4542 if (dynsym && h->dynindx == -1)
4544 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4545 goto error_free_vers;
4546 if (h->u.weakdef != NULL
4548 && h->u.weakdef->dynindx == -1)
4550 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4551 goto error_free_vers;
4554 else if (dynsym && h->dynindx != -1)
4555 /* If the symbol already has a dynamic index, but
4556 visibility says it should not be visible, turn it into
4558 switch (ELF_ST_VISIBILITY (h->other))
4562 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4567 /* Don't add DT_NEEDED for references from the dummy bfd nor
4568 for unmatched symbol. */
4573 && h->ref_regular_nonweak
4575 || (old_bfd->flags & BFD_PLUGIN) == 0))
4576 || (h->ref_dynamic_nonweak
4577 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4578 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4581 const char *soname = elf_dt_name (abfd);
4583 info->callbacks->minfo ("%!", soname, old_bfd,
4584 h->root.root.string);
4586 /* A symbol from a library loaded via DT_NEEDED of some
4587 other library is referenced by a regular object.
4588 Add a DT_NEEDED entry for it. Issue an error if
4589 --no-add-needed is used and the reference was not
4592 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4594 (*_bfd_error_handler)
4595 (_("%B: undefined reference to symbol '%s'"),
4597 bfd_set_error (bfd_error_missing_dso);
4598 goto error_free_vers;
4601 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4602 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4605 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4607 goto error_free_vers;
4609 BFD_ASSERT (ret == 0);
4614 if (extversym != NULL)
4620 if (isymbuf != NULL)
4626 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4630 /* Restore the symbol table. */
4631 old_ent = (char *) old_tab + tabsize;
4632 memset (elf_sym_hashes (abfd), 0,
4633 extsymcount * sizeof (struct elf_link_hash_entry *));
4634 htab->root.table.table = old_table;
4635 htab->root.table.size = old_size;
4636 htab->root.table.count = old_count;
4637 memcpy (htab->root.table.table, old_tab, tabsize);
4638 htab->root.undefs = old_undefs;
4639 htab->root.undefs_tail = old_undefs_tail;
4640 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4641 for (i = 0; i < htab->root.table.size; i++)
4643 struct bfd_hash_entry *p;
4644 struct elf_link_hash_entry *h;
4646 unsigned int alignment_power;
4648 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4650 h = (struct elf_link_hash_entry *) p;
4651 if (h->root.type == bfd_link_hash_warning)
4652 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4653 if (h->dynindx >= old_dynsymcount
4654 && h->dynstr_index < old_dynstr_size)
4655 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4657 /* Preserve the maximum alignment and size for common
4658 symbols even if this dynamic lib isn't on DT_NEEDED
4659 since it can still be loaded at run time by another
4661 if (h->root.type == bfd_link_hash_common)
4663 size = h->root.u.c.size;
4664 alignment_power = h->root.u.c.p->alignment_power;
4669 alignment_power = 0;
4671 memcpy (p, old_ent, htab->root.table.entsize);
4672 old_ent = (char *) old_ent + htab->root.table.entsize;
4673 h = (struct elf_link_hash_entry *) p;
4674 if (h->root.type == bfd_link_hash_warning)
4676 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4677 old_ent = (char *) old_ent + htab->root.table.entsize;
4678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4680 if (h->root.type == bfd_link_hash_common)
4682 if (size > h->root.u.c.size)
4683 h->root.u.c.size = size;
4684 if (alignment_power > h->root.u.c.p->alignment_power)
4685 h->root.u.c.p->alignment_power = alignment_power;
4690 /* Make a special call to the linker "notice" function to
4691 tell it that symbols added for crefs may need to be removed. */
4692 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4693 goto error_free_vers;
4696 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4698 if (nondeflt_vers != NULL)
4699 free (nondeflt_vers);
4703 if (old_tab != NULL)
4705 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4706 goto error_free_vers;
4711 /* Now that all the symbols from this input file are created, if
4712 not performing a relocatable link, handle .symver foo, foo@BAR
4713 such that any relocs against foo become foo@BAR. */
4714 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
4716 bfd_size_type cnt, symidx;
4718 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4720 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4721 char *shortname, *p;
4723 p = strchr (h->root.root.string, ELF_VER_CHR);
4725 || (h->root.type != bfd_link_hash_defined
4726 && h->root.type != bfd_link_hash_defweak))
4729 amt = p - h->root.root.string;
4730 shortname = (char *) bfd_malloc (amt + 1);
4732 goto error_free_vers;
4733 memcpy (shortname, h->root.root.string, amt);
4734 shortname[amt] = '\0';
4736 hi = (struct elf_link_hash_entry *)
4737 bfd_link_hash_lookup (&htab->root, shortname,
4738 FALSE, FALSE, FALSE);
4740 && hi->root.type == h->root.type
4741 && hi->root.u.def.value == h->root.u.def.value
4742 && hi->root.u.def.section == h->root.u.def.section)
4744 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4745 hi->root.type = bfd_link_hash_indirect;
4746 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4747 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4748 sym_hash = elf_sym_hashes (abfd);
4750 for (symidx = 0; symidx < extsymcount; ++symidx)
4751 if (sym_hash[symidx] == hi)
4753 sym_hash[symidx] = h;
4759 free (nondeflt_vers);
4760 nondeflt_vers = NULL;
4763 /* Now set the weakdefs field correctly for all the weak defined
4764 symbols we found. The only way to do this is to search all the
4765 symbols. Since we only need the information for non functions in
4766 dynamic objects, that's the only time we actually put anything on
4767 the list WEAKS. We need this information so that if a regular
4768 object refers to a symbol defined weakly in a dynamic object, the
4769 real symbol in the dynamic object is also put in the dynamic
4770 symbols; we also must arrange for both symbols to point to the
4771 same memory location. We could handle the general case of symbol
4772 aliasing, but a general symbol alias can only be generated in
4773 assembler code, handling it correctly would be very time
4774 consuming, and other ELF linkers don't handle general aliasing
4778 struct elf_link_hash_entry **hpp;
4779 struct elf_link_hash_entry **hppend;
4780 struct elf_link_hash_entry **sorted_sym_hash;
4781 struct elf_link_hash_entry *h;
4784 /* Since we have to search the whole symbol list for each weak
4785 defined symbol, search time for N weak defined symbols will be
4786 O(N^2). Binary search will cut it down to O(NlogN). */
4787 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4788 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4789 if (sorted_sym_hash == NULL)
4791 sym_hash = sorted_sym_hash;
4792 hpp = elf_sym_hashes (abfd);
4793 hppend = hpp + extsymcount;
4795 for (; hpp < hppend; hpp++)
4799 && h->root.type == bfd_link_hash_defined
4800 && !bed->is_function_type (h->type))
4808 qsort (sorted_sym_hash, sym_count,
4809 sizeof (struct elf_link_hash_entry *),
4812 while (weaks != NULL)
4814 struct elf_link_hash_entry *hlook;
4817 size_t i, j, idx = 0;
4820 weaks = hlook->u.weakdef;
4821 hlook->u.weakdef = NULL;
4823 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4824 || hlook->root.type == bfd_link_hash_defweak
4825 || hlook->root.type == bfd_link_hash_common
4826 || hlook->root.type == bfd_link_hash_indirect);
4827 slook = hlook->root.u.def.section;
4828 vlook = hlook->root.u.def.value;
4834 bfd_signed_vma vdiff;
4836 h = sorted_sym_hash[idx];
4837 vdiff = vlook - h->root.u.def.value;
4844 int sdiff = slook->id - h->root.u.def.section->id;
4854 /* We didn't find a value/section match. */
4858 /* With multiple aliases, or when the weak symbol is already
4859 strongly defined, we have multiple matching symbols and
4860 the binary search above may land on any of them. Step
4861 one past the matching symbol(s). */
4864 h = sorted_sym_hash[idx];
4865 if (h->root.u.def.section != slook
4866 || h->root.u.def.value != vlook)
4870 /* Now look back over the aliases. Since we sorted by size
4871 as well as value and section, we'll choose the one with
4872 the largest size. */
4875 h = sorted_sym_hash[idx];
4877 /* Stop if value or section doesn't match. */
4878 if (h->root.u.def.section != slook
4879 || h->root.u.def.value != vlook)
4881 else if (h != hlook)
4883 hlook->u.weakdef = h;
4885 /* If the weak definition is in the list of dynamic
4886 symbols, make sure the real definition is put
4888 if (hlook->dynindx != -1 && h->dynindx == -1)
4890 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4893 free (sorted_sym_hash);
4898 /* If the real definition is in the list of dynamic
4899 symbols, make sure the weak definition is put
4900 there as well. If we don't do this, then the
4901 dynamic loader might not merge the entries for the
4902 real definition and the weak definition. */
4903 if (h->dynindx != -1 && hlook->dynindx == -1)
4905 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4906 goto err_free_sym_hash;
4913 free (sorted_sym_hash);
4916 if (bed->check_directives
4917 && !(*bed->check_directives) (abfd, info))
4920 /* If this object is the same format as the output object, and it is
4921 not a shared library, then let the backend look through the
4924 This is required to build global offset table entries and to
4925 arrange for dynamic relocs. It is not required for the
4926 particular common case of linking non PIC code, even when linking
4927 against shared libraries, but unfortunately there is no way of
4928 knowing whether an object file has been compiled PIC or not.
4929 Looking through the relocs is not particularly time consuming.
4930 The problem is that we must either (1) keep the relocs in memory,
4931 which causes the linker to require additional runtime memory or
4932 (2) read the relocs twice from the input file, which wastes time.
4933 This would be a good case for using mmap.
4935 I have no idea how to handle linking PIC code into a file of a
4936 different format. It probably can't be done. */
4938 && is_elf_hash_table (htab)
4939 && bed->check_relocs != NULL
4940 && elf_object_id (abfd) == elf_hash_table_id (htab)
4941 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4945 for (o = abfd->sections; o != NULL; o = o->next)
4947 Elf_Internal_Rela *internal_relocs;
4950 if ((o->flags & SEC_RELOC) == 0
4951 || o->reloc_count == 0
4952 || ((info->strip == strip_all || info->strip == strip_debugger)
4953 && (o->flags & SEC_DEBUGGING) != 0)
4954 || bfd_is_abs_section (o->output_section))
4957 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4959 if (internal_relocs == NULL)
4962 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4964 if (elf_section_data (o)->relocs != internal_relocs)
4965 free (internal_relocs);
4972 /* If this is a non-traditional link, try to optimize the handling
4973 of the .stab/.stabstr sections. */
4975 && ! info->traditional_format
4976 && is_elf_hash_table (htab)
4977 && (info->strip != strip_all && info->strip != strip_debugger))
4981 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4982 if (stabstr != NULL)
4984 bfd_size_type string_offset = 0;
4987 for (stab = abfd->sections; stab; stab = stab->next)
4988 if (CONST_STRNEQ (stab->name, ".stab")
4989 && (!stab->name[5] ||
4990 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4991 && (stab->flags & SEC_MERGE) == 0
4992 && !bfd_is_abs_section (stab->output_section))
4994 struct bfd_elf_section_data *secdata;
4996 secdata = elf_section_data (stab);
4997 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4998 stabstr, &secdata->sec_info,
5001 if (secdata->sec_info)
5002 stab->sec_info_type = SEC_INFO_TYPE_STABS;
5007 if (is_elf_hash_table (htab) && add_needed)
5009 /* Add this bfd to the loaded list. */
5010 struct elf_link_loaded_list *n;
5012 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
5016 n->next = htab->loaded;
5023 if (old_tab != NULL)
5025 if (nondeflt_vers != NULL)
5026 free (nondeflt_vers);
5027 if (extversym != NULL)
5030 if (isymbuf != NULL)
5036 /* Return the linker hash table entry of a symbol that might be
5037 satisfied by an archive symbol. Return -1 on error. */
5039 struct elf_link_hash_entry *
5040 _bfd_elf_archive_symbol_lookup (bfd *abfd,
5041 struct bfd_link_info *info,
5044 struct elf_link_hash_entry *h;
5048 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
5052 /* If this is a default version (the name contains @@), look up the
5053 symbol again with only one `@' as well as without the version.
5054 The effect is that references to the symbol with and without the
5055 version will be matched by the default symbol in the archive. */
5057 p = strchr (name, ELF_VER_CHR);
5058 if (p == NULL || p[1] != ELF_VER_CHR)
5061 /* First check with only one `@'. */
5062 len = strlen (name);
5063 copy = (char *) bfd_alloc (abfd, len);
5065 return (struct elf_link_hash_entry *) 0 - 1;
5067 first = p - name + 1;
5068 memcpy (copy, name, first);
5069 memcpy (copy + first, name + first + 1, len - first);
5071 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
5074 /* We also need to check references to the symbol without the
5076 copy[first - 1] = '\0';
5077 h = elf_link_hash_lookup (elf_hash_table (info), copy,
5078 FALSE, FALSE, TRUE);
5081 bfd_release (abfd, copy);
5085 /* Add symbols from an ELF archive file to the linker hash table. We
5086 don't use _bfd_generic_link_add_archive_symbols because we need to
5087 handle versioned symbols.
5089 Fortunately, ELF archive handling is simpler than that done by
5090 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
5091 oddities. In ELF, if we find a symbol in the archive map, and the
5092 symbol is currently undefined, we know that we must pull in that
5095 Unfortunately, we do have to make multiple passes over the symbol
5096 table until nothing further is resolved. */
5099 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5102 unsigned char *included = NULL;
5106 const struct elf_backend_data *bed;
5107 struct elf_link_hash_entry * (*archive_symbol_lookup)
5108 (bfd *, struct bfd_link_info *, const char *);
5110 if (! bfd_has_map (abfd))
5112 /* An empty archive is a special case. */
5113 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5115 bfd_set_error (bfd_error_no_armap);
5119 /* Keep track of all symbols we know to be already defined, and all
5120 files we know to be already included. This is to speed up the
5121 second and subsequent passes. */
5122 c = bfd_ardata (abfd)->symdef_count;
5126 amt *= sizeof (*included);
5127 included = (unsigned char *) bfd_zmalloc (amt);
5128 if (included == NULL)
5131 symdefs = bfd_ardata (abfd)->symdefs;
5132 bed = get_elf_backend_data (abfd);
5133 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5146 symdefend = symdef + c;
5147 for (i = 0; symdef < symdefend; symdef++, i++)
5149 struct elf_link_hash_entry *h;
5151 struct bfd_link_hash_entry *undefs_tail;
5156 if (symdef->file_offset == last)
5162 h = archive_symbol_lookup (abfd, info, symdef->name);
5163 if (h == (struct elf_link_hash_entry *) 0 - 1)
5169 if (h->root.type == bfd_link_hash_common)
5171 /* We currently have a common symbol. The archive map contains
5172 a reference to this symbol, so we may want to include it. We
5173 only want to include it however, if this archive element
5174 contains a definition of the symbol, not just another common
5177 Unfortunately some archivers (including GNU ar) will put
5178 declarations of common symbols into their archive maps, as
5179 well as real definitions, so we cannot just go by the archive
5180 map alone. Instead we must read in the element's symbol
5181 table and check that to see what kind of symbol definition
5183 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5186 else if (h->root.type != bfd_link_hash_undefined)
5188 if (h->root.type != bfd_link_hash_undefweak)
5189 /* Symbol must be defined. Don't check it again. */
5194 /* We need to include this archive member. */
5195 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5196 if (element == NULL)
5199 if (! bfd_check_format (element, bfd_object))
5202 undefs_tail = info->hash->undefs_tail;
5204 if (!(*info->callbacks
5205 ->add_archive_element) (info, element, symdef->name, &element))
5207 if (!bfd_link_add_symbols (element, info))
5210 /* If there are any new undefined symbols, we need to make
5211 another pass through the archive in order to see whether
5212 they can be defined. FIXME: This isn't perfect, because
5213 common symbols wind up on undefs_tail and because an
5214 undefined symbol which is defined later on in this pass
5215 does not require another pass. This isn't a bug, but it
5216 does make the code less efficient than it could be. */
5217 if (undefs_tail != info->hash->undefs_tail)
5220 /* Look backward to mark all symbols from this object file
5221 which we have already seen in this pass. */
5225 included[mark] = TRUE;
5230 while (symdefs[mark].file_offset == symdef->file_offset);
5232 /* We mark subsequent symbols from this object file as we go
5233 on through the loop. */
5234 last = symdef->file_offset;
5244 if (included != NULL)
5249 /* Given an ELF BFD, add symbols to the global hash table as
5253 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5255 switch (bfd_get_format (abfd))
5258 return elf_link_add_object_symbols (abfd, info);
5260 return elf_link_add_archive_symbols (abfd, info);
5262 bfd_set_error (bfd_error_wrong_format);
5267 struct hash_codes_info
5269 unsigned long *hashcodes;
5273 /* This function will be called though elf_link_hash_traverse to store
5274 all hash value of the exported symbols in an array. */
5277 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5279 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5284 /* Ignore indirect symbols. These are added by the versioning code. */
5285 if (h->dynindx == -1)
5288 name = h->root.root.string;
5289 if (h->versioned >= versioned)
5291 char *p = strchr (name, ELF_VER_CHR);
5294 alc = (char *) bfd_malloc (p - name + 1);
5300 memcpy (alc, name, p - name);
5301 alc[p - name] = '\0';
5306 /* Compute the hash value. */
5307 ha = bfd_elf_hash (name);
5309 /* Store the found hash value in the array given as the argument. */
5310 *(inf->hashcodes)++ = ha;
5312 /* And store it in the struct so that we can put it in the hash table
5314 h->u.elf_hash_value = ha;
5322 struct collect_gnu_hash_codes
5325 const struct elf_backend_data *bed;
5326 unsigned long int nsyms;
5327 unsigned long int maskbits;
5328 unsigned long int *hashcodes;
5329 unsigned long int *hashval;
5330 unsigned long int *indx;
5331 unsigned long int *counts;
5334 long int min_dynindx;
5335 unsigned long int bucketcount;
5336 unsigned long int symindx;
5337 long int local_indx;
5338 long int shift1, shift2;
5339 unsigned long int mask;
5343 /* This function will be called though elf_link_hash_traverse to store
5344 all hash value of the exported symbols in an array. */
5347 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5349 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5354 /* Ignore indirect symbols. These are added by the versioning code. */
5355 if (h->dynindx == -1)
5358 /* Ignore also local symbols and undefined symbols. */
5359 if (! (*s->bed->elf_hash_symbol) (h))
5362 name = h->root.root.string;
5363 if (h->versioned >= versioned)
5365 char *p = strchr (name, ELF_VER_CHR);
5368 alc = (char *) bfd_malloc (p - name + 1);
5374 memcpy (alc, name, p - name);
5375 alc[p - name] = '\0';
5380 /* Compute the hash value. */
5381 ha = bfd_elf_gnu_hash (name);
5383 /* Store the found hash value in the array for compute_bucket_count,
5384 and also for .dynsym reordering purposes. */
5385 s->hashcodes[s->nsyms] = ha;
5386 s->hashval[h->dynindx] = ha;
5388 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5389 s->min_dynindx = h->dynindx;
5397 /* This function will be called though elf_link_hash_traverse to do
5398 final dynaminc symbol renumbering. */
5401 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5403 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5404 unsigned long int bucket;
5405 unsigned long int val;
5407 /* Ignore indirect symbols. */
5408 if (h->dynindx == -1)
5411 /* Ignore also local symbols and undefined symbols. */
5412 if (! (*s->bed->elf_hash_symbol) (h))
5414 if (h->dynindx >= s->min_dynindx)
5415 h->dynindx = s->local_indx++;
5419 bucket = s->hashval[h->dynindx] % s->bucketcount;
5420 val = (s->hashval[h->dynindx] >> s->shift1)
5421 & ((s->maskbits >> s->shift1) - 1);
5422 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5424 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5425 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5426 if (s->counts[bucket] == 1)
5427 /* Last element terminates the chain. */
5429 bfd_put_32 (s->output_bfd, val,
5430 s->contents + (s->indx[bucket] - s->symindx) * 4);
5431 --s->counts[bucket];
5432 h->dynindx = s->indx[bucket]++;
5436 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5439 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5441 return !(h->forced_local
5442 || h->root.type == bfd_link_hash_undefined
5443 || h->root.type == bfd_link_hash_undefweak
5444 || ((h->root.type == bfd_link_hash_defined
5445 || h->root.type == bfd_link_hash_defweak)
5446 && h->root.u.def.section->output_section == NULL));
5449 /* Array used to determine the number of hash table buckets to use
5450 based on the number of symbols there are. If there are fewer than
5451 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5452 fewer than 37 we use 17 buckets, and so forth. We never use more
5453 than 32771 buckets. */
5455 static const size_t elf_buckets[] =
5457 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5461 /* Compute bucket count for hashing table. We do not use a static set
5462 of possible tables sizes anymore. Instead we determine for all
5463 possible reasonable sizes of the table the outcome (i.e., the
5464 number of collisions etc) and choose the best solution. The
5465 weighting functions are not too simple to allow the table to grow
5466 without bounds. Instead one of the weighting factors is the size.
5467 Therefore the result is always a good payoff between few collisions
5468 (= short chain lengths) and table size. */
5470 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5471 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5472 unsigned long int nsyms,
5475 size_t best_size = 0;
5476 unsigned long int i;
5478 /* We have a problem here. The following code to optimize the table
5479 size requires an integer type with more the 32 bits. If
5480 BFD_HOST_U_64_BIT is set we know about such a type. */
5481 #ifdef BFD_HOST_U_64_BIT
5486 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5487 bfd *dynobj = elf_hash_table (info)->dynobj;
5488 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5489 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5490 unsigned long int *counts;
5492 unsigned int no_improvement_count = 0;
5494 /* Possible optimization parameters: if we have NSYMS symbols we say
5495 that the hashing table must at least have NSYMS/4 and at most
5497 minsize = nsyms / 4;
5500 best_size = maxsize = nsyms * 2;
5505 if ((best_size & 31) == 0)
5509 /* Create array where we count the collisions in. We must use bfd_malloc
5510 since the size could be large. */
5512 amt *= sizeof (unsigned long int);
5513 counts = (unsigned long int *) bfd_malloc (amt);
5517 /* Compute the "optimal" size for the hash table. The criteria is a
5518 minimal chain length. The minor criteria is (of course) the size
5520 for (i = minsize; i < maxsize; ++i)
5522 /* Walk through the array of hashcodes and count the collisions. */
5523 BFD_HOST_U_64_BIT max;
5524 unsigned long int j;
5525 unsigned long int fact;
5527 if (gnu_hash && (i & 31) == 0)
5530 memset (counts, '\0', i * sizeof (unsigned long int));
5532 /* Determine how often each hash bucket is used. */
5533 for (j = 0; j < nsyms; ++j)
5534 ++counts[hashcodes[j] % i];
5536 /* For the weight function we need some information about the
5537 pagesize on the target. This is information need not be 100%
5538 accurate. Since this information is not available (so far) we
5539 define it here to a reasonable default value. If it is crucial
5540 to have a better value some day simply define this value. */
5541 # ifndef BFD_TARGET_PAGESIZE
5542 # define BFD_TARGET_PAGESIZE (4096)
5545 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5547 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5550 /* Variant 1: optimize for short chains. We add the squares
5551 of all the chain lengths (which favors many small chain
5552 over a few long chains). */
5553 for (j = 0; j < i; ++j)
5554 max += counts[j] * counts[j];
5556 /* This adds penalties for the overall size of the table. */
5557 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5560 /* Variant 2: Optimize a lot more for small table. Here we
5561 also add squares of the size but we also add penalties for
5562 empty slots (the +1 term). */
5563 for (j = 0; j < i; ++j)
5564 max += (1 + counts[j]) * (1 + counts[j]);
5566 /* The overall size of the table is considered, but not as
5567 strong as in variant 1, where it is squared. */
5568 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5572 /* Compare with current best results. */
5573 if (max < best_chlen)
5577 no_improvement_count = 0;
5579 /* PR 11843: Avoid futile long searches for the best bucket size
5580 when there are a large number of symbols. */
5581 else if (++no_improvement_count == 100)
5588 #endif /* defined (BFD_HOST_U_64_BIT) */
5590 /* This is the fallback solution if no 64bit type is available or if we
5591 are not supposed to spend much time on optimizations. We select the
5592 bucket count using a fixed set of numbers. */
5593 for (i = 0; elf_buckets[i] != 0; i++)
5595 best_size = elf_buckets[i];
5596 if (nsyms < elf_buckets[i + 1])
5599 if (gnu_hash && best_size < 2)
5606 /* Size any SHT_GROUP section for ld -r. */
5609 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5613 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5614 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5615 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5620 /* Set a default stack segment size. The value in INFO wins. If it
5621 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5622 undefined it is initialized. */
5625 bfd_elf_stack_segment_size (bfd *output_bfd,
5626 struct bfd_link_info *info,
5627 const char *legacy_symbol,
5628 bfd_vma default_size)
5630 struct elf_link_hash_entry *h = NULL;
5632 /* Look for legacy symbol. */
5634 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5635 FALSE, FALSE, FALSE);
5636 if (h && (h->root.type == bfd_link_hash_defined
5637 || h->root.type == bfd_link_hash_defweak)
5639 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5641 /* The symbol has no type if specified on the command line. */
5642 h->type = STT_OBJECT;
5643 if (info->stacksize)
5644 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5645 output_bfd, legacy_symbol);
5646 else if (h->root.u.def.section != bfd_abs_section_ptr)
5647 (*_bfd_error_handler) (_("%B: %s not absolute"),
5648 output_bfd, legacy_symbol);
5650 info->stacksize = h->root.u.def.value;
5653 if (!info->stacksize)
5654 /* If the user didn't set a size, or explicitly inhibit the
5655 size, set it now. */
5656 info->stacksize = default_size;
5658 /* Provide the legacy symbol, if it is referenced. */
5659 if (h && (h->root.type == bfd_link_hash_undefined
5660 || h->root.type == bfd_link_hash_undefweak))
5662 struct bfd_link_hash_entry *bh = NULL;
5664 if (!(_bfd_generic_link_add_one_symbol
5665 (info, output_bfd, legacy_symbol,
5666 BSF_GLOBAL, bfd_abs_section_ptr,
5667 info->stacksize >= 0 ? info->stacksize : 0,
5668 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5671 h = (struct elf_link_hash_entry *) bh;
5673 h->type = STT_OBJECT;
5679 /* Set up the sizes and contents of the ELF dynamic sections. This is
5680 called by the ELF linker emulation before_allocation routine. We
5681 must set the sizes of the sections before the linker sets the
5682 addresses of the various sections. */
5685 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5688 const char *filter_shlib,
5690 const char *depaudit,
5691 const char * const *auxiliary_filters,
5692 struct bfd_link_info *info,
5693 asection **sinterpptr)
5695 bfd_size_type soname_indx;
5697 const struct elf_backend_data *bed;
5698 struct elf_info_failed asvinfo;
5702 soname_indx = (bfd_size_type) -1;
5704 if (!is_elf_hash_table (info->hash))
5707 bed = get_elf_backend_data (output_bfd);
5709 /* Any syms created from now on start with -1 in
5710 got.refcount/offset and plt.refcount/offset. */
5711 elf_hash_table (info)->init_got_refcount
5712 = elf_hash_table (info)->init_got_offset;
5713 elf_hash_table (info)->init_plt_refcount
5714 = elf_hash_table (info)->init_plt_offset;
5716 if (bfd_link_relocatable (info)
5717 && !_bfd_elf_size_group_sections (info))
5720 /* The backend may have to create some sections regardless of whether
5721 we're dynamic or not. */
5722 if (bed->elf_backend_always_size_sections
5723 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5726 /* Determine any GNU_STACK segment requirements, after the backend
5727 has had a chance to set a default segment size. */
5728 if (info->execstack)
5729 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5730 else if (info->noexecstack)
5731 elf_stack_flags (output_bfd) = PF_R | PF_W;
5735 asection *notesec = NULL;
5738 for (inputobj = info->input_bfds;
5740 inputobj = inputobj->link.next)
5745 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5747 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5750 if (s->flags & SEC_CODE)
5754 else if (bed->default_execstack)
5757 if (notesec || info->stacksize > 0)
5758 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5759 if (notesec && exec && bfd_link_relocatable (info)
5760 && notesec->output_section != bfd_abs_section_ptr)
5761 notesec->output_section->flags |= SEC_CODE;
5764 dynobj = elf_hash_table (info)->dynobj;
5766 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5768 struct elf_info_failed eif;
5769 struct elf_link_hash_entry *h;
5771 struct bfd_elf_version_tree *t;
5772 struct bfd_elf_version_expr *d;
5774 bfd_boolean all_defined;
5776 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5777 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
5781 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5783 if (soname_indx == (bfd_size_type) -1
5784 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5790 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5792 info->flags |= DF_SYMBOLIC;
5800 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5802 if (indx == (bfd_size_type) -1)
5805 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5806 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5810 if (filter_shlib != NULL)
5814 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5815 filter_shlib, TRUE);
5816 if (indx == (bfd_size_type) -1
5817 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5821 if (auxiliary_filters != NULL)
5823 const char * const *p;
5825 for (p = auxiliary_filters; *p != NULL; p++)
5829 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5831 if (indx == (bfd_size_type) -1
5832 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5841 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5843 if (indx == (bfd_size_type) -1
5844 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5848 if (depaudit != NULL)
5852 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5854 if (indx == (bfd_size_type) -1
5855 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5862 /* If we are supposed to export all symbols into the dynamic symbol
5863 table (this is not the normal case), then do so. */
5864 if (info->export_dynamic
5865 || (bfd_link_executable (info) && info->dynamic))
5867 elf_link_hash_traverse (elf_hash_table (info),
5868 _bfd_elf_export_symbol,
5874 /* Make all global versions with definition. */
5875 for (t = info->version_info; t != NULL; t = t->next)
5876 for (d = t->globals.list; d != NULL; d = d->next)
5877 if (!d->symver && d->literal)
5879 const char *verstr, *name;
5880 size_t namelen, verlen, newlen;
5881 char *newname, *p, leading_char;
5882 struct elf_link_hash_entry *newh;
5884 leading_char = bfd_get_symbol_leading_char (output_bfd);
5886 namelen = strlen (name) + (leading_char != '\0');
5888 verlen = strlen (verstr);
5889 newlen = namelen + verlen + 3;
5891 newname = (char *) bfd_malloc (newlen);
5892 if (newname == NULL)
5894 newname[0] = leading_char;
5895 memcpy (newname + (leading_char != '\0'), name, namelen);
5897 /* Check the hidden versioned definition. */
5898 p = newname + namelen;
5900 memcpy (p, verstr, verlen + 1);
5901 newh = elf_link_hash_lookup (elf_hash_table (info),
5902 newname, FALSE, FALSE,
5905 || (newh->root.type != bfd_link_hash_defined
5906 && newh->root.type != bfd_link_hash_defweak))
5908 /* Check the default versioned definition. */
5910 memcpy (p, verstr, verlen + 1);
5911 newh = elf_link_hash_lookup (elf_hash_table (info),
5912 newname, FALSE, FALSE,
5917 /* Mark this version if there is a definition and it is
5918 not defined in a shared object. */
5920 && !newh->def_dynamic
5921 && (newh->root.type == bfd_link_hash_defined
5922 || newh->root.type == bfd_link_hash_defweak))
5926 /* Attach all the symbols to their version information. */
5927 asvinfo.info = info;
5928 asvinfo.failed = FALSE;
5930 elf_link_hash_traverse (elf_hash_table (info),
5931 _bfd_elf_link_assign_sym_version,
5936 if (!info->allow_undefined_version)
5938 /* Check if all global versions have a definition. */
5940 for (t = info->version_info; t != NULL; t = t->next)
5941 for (d = t->globals.list; d != NULL; d = d->next)
5942 if (d->literal && !d->symver && !d->script)
5944 (*_bfd_error_handler)
5945 (_("%s: undefined version: %s"),
5946 d->pattern, t->name);
5947 all_defined = FALSE;
5952 bfd_set_error (bfd_error_bad_value);
5957 /* Find all symbols which were defined in a dynamic object and make
5958 the backend pick a reasonable value for them. */
5959 elf_link_hash_traverse (elf_hash_table (info),
5960 _bfd_elf_adjust_dynamic_symbol,
5965 /* Add some entries to the .dynamic section. We fill in some of the
5966 values later, in bfd_elf_final_link, but we must add the entries
5967 now so that we know the final size of the .dynamic section. */
5969 /* If there are initialization and/or finalization functions to
5970 call then add the corresponding DT_INIT/DT_FINI entries. */
5971 h = (info->init_function
5972 ? elf_link_hash_lookup (elf_hash_table (info),
5973 info->init_function, FALSE,
5980 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5983 h = (info->fini_function
5984 ? elf_link_hash_lookup (elf_hash_table (info),
5985 info->fini_function, FALSE,
5992 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5996 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5997 if (s != NULL && s->linker_has_input)
5999 /* DT_PREINIT_ARRAY is not allowed in shared library. */
6000 if (! bfd_link_executable (info))
6005 for (sub = info->input_bfds; sub != NULL;
6006 sub = sub->link.next)
6007 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
6008 for (o = sub->sections; o != NULL; o = o->next)
6009 if (elf_section_data (o)->this_hdr.sh_type
6010 == SHT_PREINIT_ARRAY)
6012 (*_bfd_error_handler)
6013 (_("%B: .preinit_array section is not allowed in DSO"),
6018 bfd_set_error (bfd_error_nonrepresentable_section);
6022 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
6023 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
6026 s = bfd_get_section_by_name (output_bfd, ".init_array");
6027 if (s != NULL && s->linker_has_input)
6029 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
6030 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
6033 s = bfd_get_section_by_name (output_bfd, ".fini_array");
6034 if (s != NULL && s->linker_has_input)
6036 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
6037 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
6041 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
6042 /* If .dynstr is excluded from the link, we don't want any of
6043 these tags. Strictly, we should be checking each section
6044 individually; This quick check covers for the case where
6045 someone does a /DISCARD/ : { *(*) }. */
6046 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
6048 bfd_size_type strsize;
6050 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6051 if ((info->emit_hash
6052 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
6053 || (info->emit_gnu_hash
6054 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
6055 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
6056 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
6057 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
6058 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
6059 bed->s->sizeof_sym))
6064 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
6067 /* The backend must work out the sizes of all the other dynamic
6070 && bed->elf_backend_size_dynamic_sections != NULL
6071 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
6074 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
6076 unsigned long section_sym_count;
6077 struct bfd_elf_version_tree *verdefs;
6080 /* Set up the version definition section. */
6081 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
6082 BFD_ASSERT (s != NULL);
6084 /* We may have created additional version definitions if we are
6085 just linking a regular application. */
6086 verdefs = info->version_info;
6088 /* Skip anonymous version tag. */
6089 if (verdefs != NULL && verdefs->vernum == 0)
6090 verdefs = verdefs->next;
6092 if (verdefs == NULL && !info->create_default_symver)
6093 s->flags |= SEC_EXCLUDE;
6098 struct bfd_elf_version_tree *t;
6100 Elf_Internal_Verdef def;
6101 Elf_Internal_Verdaux defaux;
6102 struct bfd_link_hash_entry *bh;
6103 struct elf_link_hash_entry *h;
6109 /* Make space for the base version. */
6110 size += sizeof (Elf_External_Verdef);
6111 size += sizeof (Elf_External_Verdaux);
6114 /* Make space for the default version. */
6115 if (info->create_default_symver)
6117 size += sizeof (Elf_External_Verdef);
6121 for (t = verdefs; t != NULL; t = t->next)
6123 struct bfd_elf_version_deps *n;
6125 /* Don't emit base version twice. */
6129 size += sizeof (Elf_External_Verdef);
6130 size += sizeof (Elf_External_Verdaux);
6133 for (n = t->deps; n != NULL; n = n->next)
6134 size += sizeof (Elf_External_Verdaux);
6138 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6139 if (s->contents == NULL && s->size != 0)
6142 /* Fill in the version definition section. */
6146 def.vd_version = VER_DEF_CURRENT;
6147 def.vd_flags = VER_FLG_BASE;
6150 if (info->create_default_symver)
6152 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6153 def.vd_next = sizeof (Elf_External_Verdef);
6157 def.vd_aux = sizeof (Elf_External_Verdef);
6158 def.vd_next = (sizeof (Elf_External_Verdef)
6159 + sizeof (Elf_External_Verdaux));
6162 if (soname_indx != (bfd_size_type) -1)
6164 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6166 def.vd_hash = bfd_elf_hash (soname);
6167 defaux.vda_name = soname_indx;
6174 name = lbasename (output_bfd->filename);
6175 def.vd_hash = bfd_elf_hash (name);
6176 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6178 if (indx == (bfd_size_type) -1)
6180 defaux.vda_name = indx;
6182 defaux.vda_next = 0;
6184 _bfd_elf_swap_verdef_out (output_bfd, &def,
6185 (Elf_External_Verdef *) p);
6186 p += sizeof (Elf_External_Verdef);
6187 if (info->create_default_symver)
6189 /* Add a symbol representing this version. */
6191 if (! (_bfd_generic_link_add_one_symbol
6192 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6194 get_elf_backend_data (dynobj)->collect, &bh)))
6196 h = (struct elf_link_hash_entry *) bh;
6199 h->type = STT_OBJECT;
6200 h->verinfo.vertree = NULL;
6202 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6205 /* Create a duplicate of the base version with the same
6206 aux block, but different flags. */
6209 def.vd_aux = sizeof (Elf_External_Verdef);
6211 def.vd_next = (sizeof (Elf_External_Verdef)
6212 + sizeof (Elf_External_Verdaux));
6215 _bfd_elf_swap_verdef_out (output_bfd, &def,
6216 (Elf_External_Verdef *) p);
6217 p += sizeof (Elf_External_Verdef);
6219 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6220 (Elf_External_Verdaux *) p);
6221 p += sizeof (Elf_External_Verdaux);
6223 for (t = verdefs; t != NULL; t = t->next)
6226 struct bfd_elf_version_deps *n;
6228 /* Don't emit the base version twice. */
6233 for (n = t->deps; n != NULL; n = n->next)
6236 /* Add a symbol representing this version. */
6238 if (! (_bfd_generic_link_add_one_symbol
6239 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6241 get_elf_backend_data (dynobj)->collect, &bh)))
6243 h = (struct elf_link_hash_entry *) bh;
6246 h->type = STT_OBJECT;
6247 h->verinfo.vertree = t;
6249 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6252 def.vd_version = VER_DEF_CURRENT;
6254 if (t->globals.list == NULL
6255 && t->locals.list == NULL
6257 def.vd_flags |= VER_FLG_WEAK;
6258 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6259 def.vd_cnt = cdeps + 1;
6260 def.vd_hash = bfd_elf_hash (t->name);
6261 def.vd_aux = sizeof (Elf_External_Verdef);
6264 /* If a basever node is next, it *must* be the last node in
6265 the chain, otherwise Verdef construction breaks. */
6266 if (t->next != NULL && t->next->vernum == 0)
6267 BFD_ASSERT (t->next->next == NULL);
6269 if (t->next != NULL && t->next->vernum != 0)
6270 def.vd_next = (sizeof (Elf_External_Verdef)
6271 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6273 _bfd_elf_swap_verdef_out (output_bfd, &def,
6274 (Elf_External_Verdef *) p);
6275 p += sizeof (Elf_External_Verdef);
6277 defaux.vda_name = h->dynstr_index;
6278 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6280 defaux.vda_next = 0;
6281 if (t->deps != NULL)
6282 defaux.vda_next = sizeof (Elf_External_Verdaux);
6283 t->name_indx = defaux.vda_name;
6285 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6286 (Elf_External_Verdaux *) p);
6287 p += sizeof (Elf_External_Verdaux);
6289 for (n = t->deps; n != NULL; n = n->next)
6291 if (n->version_needed == NULL)
6293 /* This can happen if there was an error in the
6295 defaux.vda_name = 0;
6299 defaux.vda_name = n->version_needed->name_indx;
6300 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6303 if (n->next == NULL)
6304 defaux.vda_next = 0;
6306 defaux.vda_next = sizeof (Elf_External_Verdaux);
6308 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6309 (Elf_External_Verdaux *) p);
6310 p += sizeof (Elf_External_Verdaux);
6314 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6315 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6318 elf_tdata (output_bfd)->cverdefs = cdefs;
6321 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6323 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6326 else if (info->flags & DF_BIND_NOW)
6328 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6334 if (bfd_link_executable (info))
6335 info->flags_1 &= ~ (DF_1_INITFIRST
6338 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6342 /* Work out the size of the version reference section. */
6344 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6345 BFD_ASSERT (s != NULL);
6347 struct elf_find_verdep_info sinfo;
6350 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6351 if (sinfo.vers == 0)
6353 sinfo.failed = FALSE;
6355 elf_link_hash_traverse (elf_hash_table (info),
6356 _bfd_elf_link_find_version_dependencies,
6361 if (elf_tdata (output_bfd)->verref == NULL)
6362 s->flags |= SEC_EXCLUDE;
6365 Elf_Internal_Verneed *t;
6370 /* Build the version dependency section. */
6373 for (t = elf_tdata (output_bfd)->verref;
6377 Elf_Internal_Vernaux *a;
6379 size += sizeof (Elf_External_Verneed);
6381 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6382 size += sizeof (Elf_External_Vernaux);
6386 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6387 if (s->contents == NULL)
6391 for (t = elf_tdata (output_bfd)->verref;
6396 Elf_Internal_Vernaux *a;
6400 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6403 t->vn_version = VER_NEED_CURRENT;
6405 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6406 elf_dt_name (t->vn_bfd) != NULL
6407 ? elf_dt_name (t->vn_bfd)
6408 : lbasename (t->vn_bfd->filename),
6410 if (indx == (bfd_size_type) -1)
6413 t->vn_aux = sizeof (Elf_External_Verneed);
6414 if (t->vn_nextref == NULL)
6417 t->vn_next = (sizeof (Elf_External_Verneed)
6418 + caux * sizeof (Elf_External_Vernaux));
6420 _bfd_elf_swap_verneed_out (output_bfd, t,
6421 (Elf_External_Verneed *) p);
6422 p += sizeof (Elf_External_Verneed);
6424 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6426 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6427 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6428 a->vna_nodename, FALSE);
6429 if (indx == (bfd_size_type) -1)
6432 if (a->vna_nextptr == NULL)
6435 a->vna_next = sizeof (Elf_External_Vernaux);
6437 _bfd_elf_swap_vernaux_out (output_bfd, a,
6438 (Elf_External_Vernaux *) p);
6439 p += sizeof (Elf_External_Vernaux);
6443 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6444 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6447 elf_tdata (output_bfd)->cverrefs = crefs;
6451 if ((elf_tdata (output_bfd)->cverrefs == 0
6452 && elf_tdata (output_bfd)->cverdefs == 0)
6453 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6454 §ion_sym_count) == 0)
6456 s = bfd_get_linker_section (dynobj, ".gnu.version");
6457 s->flags |= SEC_EXCLUDE;
6463 /* Find the first non-excluded output section. We'll use its
6464 section symbol for some emitted relocs. */
6466 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6470 for (s = output_bfd->sections; s != NULL; s = s->next)
6471 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6472 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6474 elf_hash_table (info)->text_index_section = s;
6479 /* Find two non-excluded output sections, one for code, one for data.
6480 We'll use their section symbols for some emitted relocs. */
6482 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6486 /* Data first, since setting text_index_section changes
6487 _bfd_elf_link_omit_section_dynsym. */
6488 for (s = output_bfd->sections; s != NULL; s = s->next)
6489 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6490 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6492 elf_hash_table (info)->data_index_section = s;
6496 for (s = output_bfd->sections; s != NULL; s = s->next)
6497 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6498 == (SEC_ALLOC | SEC_READONLY))
6499 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6501 elf_hash_table (info)->text_index_section = s;
6505 if (elf_hash_table (info)->text_index_section == NULL)
6506 elf_hash_table (info)->text_index_section
6507 = elf_hash_table (info)->data_index_section;
6511 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6513 const struct elf_backend_data *bed;
6515 if (!is_elf_hash_table (info->hash))
6518 bed = get_elf_backend_data (output_bfd);
6519 (*bed->elf_backend_init_index_section) (output_bfd, info);
6521 if (elf_hash_table (info)->dynamic_sections_created)
6525 bfd_size_type dynsymcount;
6526 unsigned long section_sym_count;
6527 unsigned int dtagcount;
6529 dynobj = elf_hash_table (info)->dynobj;
6531 /* Assign dynsym indicies. In a shared library we generate a
6532 section symbol for each output section, which come first.
6533 Next come all of the back-end allocated local dynamic syms,
6534 followed by the rest of the global symbols. */
6536 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6537 §ion_sym_count);
6539 /* Work out the size of the symbol version section. */
6540 s = bfd_get_linker_section (dynobj, ".gnu.version");
6541 BFD_ASSERT (s != NULL);
6542 if (dynsymcount != 0
6543 && (s->flags & SEC_EXCLUDE) == 0)
6545 s->size = dynsymcount * sizeof (Elf_External_Versym);
6546 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6547 if (s->contents == NULL)
6550 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6554 /* Set the size of the .dynsym and .hash sections. We counted
6555 the number of dynamic symbols in elf_link_add_object_symbols.
6556 We will build the contents of .dynsym and .hash when we build
6557 the final symbol table, because until then we do not know the
6558 correct value to give the symbols. We built the .dynstr
6559 section as we went along in elf_link_add_object_symbols. */
6560 s = elf_hash_table (info)->dynsym;
6561 BFD_ASSERT (s != NULL);
6562 s->size = dynsymcount * bed->s->sizeof_sym;
6564 if (dynsymcount != 0)
6566 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6567 if (s->contents == NULL)
6570 /* The first entry in .dynsym is a dummy symbol.
6571 Clear all the section syms, in case we don't output them all. */
6572 ++section_sym_count;
6573 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6576 elf_hash_table (info)->bucketcount = 0;
6578 /* Compute the size of the hashing table. As a side effect this
6579 computes the hash values for all the names we export. */
6580 if (info->emit_hash)
6582 unsigned long int *hashcodes;
6583 struct hash_codes_info hashinf;
6585 unsigned long int nsyms;
6587 size_t hash_entry_size;
6589 /* Compute the hash values for all exported symbols. At the same
6590 time store the values in an array so that we could use them for
6592 amt = dynsymcount * sizeof (unsigned long int);
6593 hashcodes = (unsigned long int *) bfd_malloc (amt);
6594 if (hashcodes == NULL)
6596 hashinf.hashcodes = hashcodes;
6597 hashinf.error = FALSE;
6599 /* Put all hash values in HASHCODES. */
6600 elf_link_hash_traverse (elf_hash_table (info),
6601 elf_collect_hash_codes, &hashinf);
6608 nsyms = hashinf.hashcodes - hashcodes;
6610 = compute_bucket_count (info, hashcodes, nsyms, 0);
6613 if (bucketcount == 0)
6616 elf_hash_table (info)->bucketcount = bucketcount;
6618 s = bfd_get_linker_section (dynobj, ".hash");
6619 BFD_ASSERT (s != NULL);
6620 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6621 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6622 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6623 if (s->contents == NULL)
6626 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6627 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6628 s->contents + hash_entry_size);
6631 if (info->emit_gnu_hash)
6634 unsigned char *contents;
6635 struct collect_gnu_hash_codes cinfo;
6639 memset (&cinfo, 0, sizeof (cinfo));
6641 /* Compute the hash values for all exported symbols. At the same
6642 time store the values in an array so that we could use them for
6644 amt = dynsymcount * 2 * sizeof (unsigned long int);
6645 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6646 if (cinfo.hashcodes == NULL)
6649 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6650 cinfo.min_dynindx = -1;
6651 cinfo.output_bfd = output_bfd;
6654 /* Put all hash values in HASHCODES. */
6655 elf_link_hash_traverse (elf_hash_table (info),
6656 elf_collect_gnu_hash_codes, &cinfo);
6659 free (cinfo.hashcodes);
6664 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6666 if (bucketcount == 0)
6668 free (cinfo.hashcodes);
6672 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6673 BFD_ASSERT (s != NULL);
6675 if (cinfo.nsyms == 0)
6677 /* Empty .gnu.hash section is special. */
6678 BFD_ASSERT (cinfo.min_dynindx == -1);
6679 free (cinfo.hashcodes);
6680 s->size = 5 * 4 + bed->s->arch_size / 8;
6681 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6682 if (contents == NULL)
6684 s->contents = contents;
6685 /* 1 empty bucket. */
6686 bfd_put_32 (output_bfd, 1, contents);
6687 /* SYMIDX above the special symbol 0. */
6688 bfd_put_32 (output_bfd, 1, contents + 4);
6689 /* Just one word for bitmask. */
6690 bfd_put_32 (output_bfd, 1, contents + 8);
6691 /* Only hash fn bloom filter. */
6692 bfd_put_32 (output_bfd, 0, contents + 12);
6693 /* No hashes are valid - empty bitmask. */
6694 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6695 /* No hashes in the only bucket. */
6696 bfd_put_32 (output_bfd, 0,
6697 contents + 16 + bed->s->arch_size / 8);
6701 unsigned long int maskwords, maskbitslog2, x;
6702 BFD_ASSERT (cinfo.min_dynindx != -1);
6706 while ((x >>= 1) != 0)
6708 if (maskbitslog2 < 3)
6710 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6711 maskbitslog2 = maskbitslog2 + 3;
6713 maskbitslog2 = maskbitslog2 + 2;
6714 if (bed->s->arch_size == 64)
6716 if (maskbitslog2 == 5)
6722 cinfo.mask = (1 << cinfo.shift1) - 1;
6723 cinfo.shift2 = maskbitslog2;
6724 cinfo.maskbits = 1 << maskbitslog2;
6725 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6726 amt = bucketcount * sizeof (unsigned long int) * 2;
6727 amt += maskwords * sizeof (bfd_vma);
6728 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6729 if (cinfo.bitmask == NULL)
6731 free (cinfo.hashcodes);
6735 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6736 cinfo.indx = cinfo.counts + bucketcount;
6737 cinfo.symindx = dynsymcount - cinfo.nsyms;
6738 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6740 /* Determine how often each hash bucket is used. */
6741 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6742 for (i = 0; i < cinfo.nsyms; ++i)
6743 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6745 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6746 if (cinfo.counts[i] != 0)
6748 cinfo.indx[i] = cnt;
6749 cnt += cinfo.counts[i];
6751 BFD_ASSERT (cnt == dynsymcount);
6752 cinfo.bucketcount = bucketcount;
6753 cinfo.local_indx = cinfo.min_dynindx;
6755 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6756 s->size += cinfo.maskbits / 8;
6757 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6758 if (contents == NULL)
6760 free (cinfo.bitmask);
6761 free (cinfo.hashcodes);
6765 s->contents = contents;
6766 bfd_put_32 (output_bfd, bucketcount, contents);
6767 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6768 bfd_put_32 (output_bfd, maskwords, contents + 8);
6769 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6770 contents += 16 + cinfo.maskbits / 8;
6772 for (i = 0; i < bucketcount; ++i)
6774 if (cinfo.counts[i] == 0)
6775 bfd_put_32 (output_bfd, 0, contents);
6777 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6781 cinfo.contents = contents;
6783 /* Renumber dynamic symbols, populate .gnu.hash section. */
6784 elf_link_hash_traverse (elf_hash_table (info),
6785 elf_renumber_gnu_hash_syms, &cinfo);
6787 contents = s->contents + 16;
6788 for (i = 0; i < maskwords; ++i)
6790 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6792 contents += bed->s->arch_size / 8;
6795 free (cinfo.bitmask);
6796 free (cinfo.hashcodes);
6800 s = bfd_get_linker_section (dynobj, ".dynstr");
6801 BFD_ASSERT (s != NULL);
6803 elf_finalize_dynstr (output_bfd, info);
6805 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6807 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6808 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6815 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6818 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6821 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6822 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6825 /* Finish SHF_MERGE section merging. */
6828 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
6833 if (!is_elf_hash_table (info->hash))
6836 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6837 if ((ibfd->flags & DYNAMIC) == 0
6838 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
6839 && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
6840 == get_elf_backend_data (obfd)->s->elfclass))
6841 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6842 if ((sec->flags & SEC_MERGE) != 0
6843 && !bfd_is_abs_section (sec->output_section))
6845 struct bfd_elf_section_data *secdata;
6847 secdata = elf_section_data (sec);
6848 if (! _bfd_add_merge_section (obfd,
6849 &elf_hash_table (info)->merge_info,
6850 sec, &secdata->sec_info))
6852 else if (secdata->sec_info)
6853 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6856 if (elf_hash_table (info)->merge_info != NULL)
6857 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
6858 merge_sections_remove_hook);
6862 /* Create an entry in an ELF linker hash table. */
6864 struct bfd_hash_entry *
6865 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6866 struct bfd_hash_table *table,
6869 /* Allocate the structure if it has not already been allocated by a
6873 entry = (struct bfd_hash_entry *)
6874 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6879 /* Call the allocation method of the superclass. */
6880 entry = _bfd_link_hash_newfunc (entry, table, string);
6883 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6884 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6886 /* Set local fields. */
6889 ret->got = htab->init_got_refcount;
6890 ret->plt = htab->init_plt_refcount;
6891 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6892 - offsetof (struct elf_link_hash_entry, size)));
6893 /* Assume that we have been called by a non-ELF symbol reader.
6894 This flag is then reset by the code which reads an ELF input
6895 file. This ensures that a symbol created by a non-ELF symbol
6896 reader will have the flag set correctly. */
6903 /* Copy data from an indirect symbol to its direct symbol, hiding the
6904 old indirect symbol. Also used for copying flags to a weakdef. */
6907 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6908 struct elf_link_hash_entry *dir,
6909 struct elf_link_hash_entry *ind)
6911 struct elf_link_hash_table *htab;
6913 /* Copy down any references that we may have already seen to the
6914 symbol which just became indirect if DIR isn't a hidden versioned
6917 if (dir->versioned != versioned_hidden)
6919 dir->ref_dynamic |= ind->ref_dynamic;
6920 dir->ref_regular |= ind->ref_regular;
6921 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6922 dir->non_got_ref |= ind->non_got_ref;
6923 dir->needs_plt |= ind->needs_plt;
6924 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6927 if (ind->root.type != bfd_link_hash_indirect)
6930 /* Copy over the global and procedure linkage table refcount entries.
6931 These may have been already set up by a check_relocs routine. */
6932 htab = elf_hash_table (info);
6933 if (ind->got.refcount > htab->init_got_refcount.refcount)
6935 if (dir->got.refcount < 0)
6936 dir->got.refcount = 0;
6937 dir->got.refcount += ind->got.refcount;
6938 ind->got.refcount = htab->init_got_refcount.refcount;
6941 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6943 if (dir->plt.refcount < 0)
6944 dir->plt.refcount = 0;
6945 dir->plt.refcount += ind->plt.refcount;
6946 ind->plt.refcount = htab->init_plt_refcount.refcount;
6949 if (ind->dynindx != -1)
6951 if (dir->dynindx != -1)
6952 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6953 dir->dynindx = ind->dynindx;
6954 dir->dynstr_index = ind->dynstr_index;
6956 ind->dynstr_index = 0;
6961 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6962 struct elf_link_hash_entry *h,
6963 bfd_boolean force_local)
6965 /* STT_GNU_IFUNC symbol must go through PLT. */
6966 if (h->type != STT_GNU_IFUNC)
6968 h->plt = elf_hash_table (info)->init_plt_offset;
6973 h->forced_local = 1;
6974 if (h->dynindx != -1)
6977 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6983 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6987 _bfd_elf_link_hash_table_init
6988 (struct elf_link_hash_table *table,
6990 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6991 struct bfd_hash_table *,
6993 unsigned int entsize,
6994 enum elf_target_id target_id)
6997 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6999 table->init_got_refcount.refcount = can_refcount - 1;
7000 table->init_plt_refcount.refcount = can_refcount - 1;
7001 table->init_got_offset.offset = -(bfd_vma) 1;
7002 table->init_plt_offset.offset = -(bfd_vma) 1;
7003 /* The first dynamic symbol is a dummy. */
7004 table->dynsymcount = 1;
7006 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
7008 table->root.type = bfd_link_elf_hash_table;
7009 table->hash_table_id = target_id;
7014 /* Create an ELF linker hash table. */
7016 struct bfd_link_hash_table *
7017 _bfd_elf_link_hash_table_create (bfd *abfd)
7019 struct elf_link_hash_table *ret;
7020 bfd_size_type amt = sizeof (struct elf_link_hash_table);
7022 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
7026 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
7027 sizeof (struct elf_link_hash_entry),
7033 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
7038 /* Destroy an ELF linker hash table. */
7041 _bfd_elf_link_hash_table_free (bfd *obfd)
7043 struct elf_link_hash_table *htab;
7045 htab = (struct elf_link_hash_table *) obfd->link.hash;
7046 if (htab->dynstr != NULL)
7047 _bfd_elf_strtab_free (htab->dynstr);
7048 _bfd_merge_sections_free (htab->merge_info);
7049 _bfd_generic_link_hash_table_free (obfd);
7052 /* This is a hook for the ELF emulation code in the generic linker to
7053 tell the backend linker what file name to use for the DT_NEEDED
7054 entry for a dynamic object. */
7057 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
7059 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7060 && bfd_get_format (abfd) == bfd_object)
7061 elf_dt_name (abfd) = name;
7065 bfd_elf_get_dyn_lib_class (bfd *abfd)
7068 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7069 && bfd_get_format (abfd) == bfd_object)
7070 lib_class = elf_dyn_lib_class (abfd);
7077 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
7079 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7080 && bfd_get_format (abfd) == bfd_object)
7081 elf_dyn_lib_class (abfd) = lib_class;
7084 /* Get the list of DT_NEEDED entries for a link. This is a hook for
7085 the linker ELF emulation code. */
7087 struct bfd_link_needed_list *
7088 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
7089 struct bfd_link_info *info)
7091 if (! is_elf_hash_table (info->hash))
7093 return elf_hash_table (info)->needed;
7096 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
7097 hook for the linker ELF emulation code. */
7099 struct bfd_link_needed_list *
7100 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
7101 struct bfd_link_info *info)
7103 if (! is_elf_hash_table (info->hash))
7105 return elf_hash_table (info)->runpath;
7108 /* Get the name actually used for a dynamic object for a link. This
7109 is the SONAME entry if there is one. Otherwise, it is the string
7110 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7113 bfd_elf_get_dt_soname (bfd *abfd)
7115 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7116 && bfd_get_format (abfd) == bfd_object)
7117 return elf_dt_name (abfd);
7121 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7122 the ELF linker emulation code. */
7125 bfd_elf_get_bfd_needed_list (bfd *abfd,
7126 struct bfd_link_needed_list **pneeded)
7129 bfd_byte *dynbuf = NULL;
7130 unsigned int elfsec;
7131 unsigned long shlink;
7132 bfd_byte *extdyn, *extdynend;
7134 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7138 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7139 || bfd_get_format (abfd) != bfd_object)
7142 s = bfd_get_section_by_name (abfd, ".dynamic");
7143 if (s == NULL || s->size == 0)
7146 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7149 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7150 if (elfsec == SHN_BAD)
7153 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7155 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7156 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7159 extdynend = extdyn + s->size;
7160 for (; extdyn < extdynend; extdyn += extdynsize)
7162 Elf_Internal_Dyn dyn;
7164 (*swap_dyn_in) (abfd, extdyn, &dyn);
7166 if (dyn.d_tag == DT_NULL)
7169 if (dyn.d_tag == DT_NEEDED)
7172 struct bfd_link_needed_list *l;
7173 unsigned int tagv = dyn.d_un.d_val;
7176 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7181 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7202 struct elf_symbuf_symbol
7204 unsigned long st_name; /* Symbol name, index in string tbl */
7205 unsigned char st_info; /* Type and binding attributes */
7206 unsigned char st_other; /* Visibilty, and target specific */
7209 struct elf_symbuf_head
7211 struct elf_symbuf_symbol *ssym;
7212 bfd_size_type count;
7213 unsigned int st_shndx;
7220 Elf_Internal_Sym *isym;
7221 struct elf_symbuf_symbol *ssym;
7226 /* Sort references to symbols by ascending section number. */
7229 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7231 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7232 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7234 return s1->st_shndx - s2->st_shndx;
7238 elf_sym_name_compare (const void *arg1, const void *arg2)
7240 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7241 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7242 return strcmp (s1->name, s2->name);
7245 static struct elf_symbuf_head *
7246 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7248 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7249 struct elf_symbuf_symbol *ssym;
7250 struct elf_symbuf_head *ssymbuf, *ssymhead;
7251 bfd_size_type i, shndx_count, total_size;
7253 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7257 for (ind = indbuf, i = 0; i < symcount; i++)
7258 if (isymbuf[i].st_shndx != SHN_UNDEF)
7259 *ind++ = &isymbuf[i];
7262 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7263 elf_sort_elf_symbol);
7266 if (indbufend > indbuf)
7267 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7268 if (ind[0]->st_shndx != ind[1]->st_shndx)
7271 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7272 + (indbufend - indbuf) * sizeof (*ssym));
7273 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7274 if (ssymbuf == NULL)
7280 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7281 ssymbuf->ssym = NULL;
7282 ssymbuf->count = shndx_count;
7283 ssymbuf->st_shndx = 0;
7284 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7286 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7289 ssymhead->ssym = ssym;
7290 ssymhead->count = 0;
7291 ssymhead->st_shndx = (*ind)->st_shndx;
7293 ssym->st_name = (*ind)->st_name;
7294 ssym->st_info = (*ind)->st_info;
7295 ssym->st_other = (*ind)->st_other;
7298 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7299 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7306 /* Check if 2 sections define the same set of local and global
7310 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7311 struct bfd_link_info *info)
7314 const struct elf_backend_data *bed1, *bed2;
7315 Elf_Internal_Shdr *hdr1, *hdr2;
7316 bfd_size_type symcount1, symcount2;
7317 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7318 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7319 Elf_Internal_Sym *isym, *isymend;
7320 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7321 bfd_size_type count1, count2, i;
7322 unsigned int shndx1, shndx2;
7328 /* Both sections have to be in ELF. */
7329 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7330 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7333 if (elf_section_type (sec1) != elf_section_type (sec2))
7336 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7337 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7338 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7341 bed1 = get_elf_backend_data (bfd1);
7342 bed2 = get_elf_backend_data (bfd2);
7343 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7344 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7345 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7346 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7348 if (symcount1 == 0 || symcount2 == 0)
7354 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7355 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7357 if (ssymbuf1 == NULL)
7359 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7361 if (isymbuf1 == NULL)
7364 if (!info->reduce_memory_overheads)
7365 elf_tdata (bfd1)->symbuf = ssymbuf1
7366 = elf_create_symbuf (symcount1, isymbuf1);
7369 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7371 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7373 if (isymbuf2 == NULL)
7376 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7377 elf_tdata (bfd2)->symbuf = ssymbuf2
7378 = elf_create_symbuf (symcount2, isymbuf2);
7381 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7383 /* Optimized faster version. */
7384 bfd_size_type lo, hi, mid;
7385 struct elf_symbol *symp;
7386 struct elf_symbuf_symbol *ssym, *ssymend;
7389 hi = ssymbuf1->count;
7394 mid = (lo + hi) / 2;
7395 if (shndx1 < ssymbuf1[mid].st_shndx)
7397 else if (shndx1 > ssymbuf1[mid].st_shndx)
7401 count1 = ssymbuf1[mid].count;
7408 hi = ssymbuf2->count;
7413 mid = (lo + hi) / 2;
7414 if (shndx2 < ssymbuf2[mid].st_shndx)
7416 else if (shndx2 > ssymbuf2[mid].st_shndx)
7420 count2 = ssymbuf2[mid].count;
7426 if (count1 == 0 || count2 == 0 || count1 != count2)
7430 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7432 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7433 if (symtable1 == NULL || symtable2 == NULL)
7437 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7438 ssym < ssymend; ssym++, symp++)
7440 symp->u.ssym = ssym;
7441 symp->name = bfd_elf_string_from_elf_section (bfd1,
7447 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7448 ssym < ssymend; ssym++, symp++)
7450 symp->u.ssym = ssym;
7451 symp->name = bfd_elf_string_from_elf_section (bfd2,
7456 /* Sort symbol by name. */
7457 qsort (symtable1, count1, sizeof (struct elf_symbol),
7458 elf_sym_name_compare);
7459 qsort (symtable2, count1, sizeof (struct elf_symbol),
7460 elf_sym_name_compare);
7462 for (i = 0; i < count1; i++)
7463 /* Two symbols must have the same binding, type and name. */
7464 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7465 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7466 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7473 symtable1 = (struct elf_symbol *)
7474 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7475 symtable2 = (struct elf_symbol *)
7476 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7477 if (symtable1 == NULL || symtable2 == NULL)
7480 /* Count definitions in the section. */
7482 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7483 if (isym->st_shndx == shndx1)
7484 symtable1[count1++].u.isym = isym;
7487 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7488 if (isym->st_shndx == shndx2)
7489 symtable2[count2++].u.isym = isym;
7491 if (count1 == 0 || count2 == 0 || count1 != count2)
7494 for (i = 0; i < count1; i++)
7496 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7497 symtable1[i].u.isym->st_name);
7499 for (i = 0; i < count2; i++)
7501 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7502 symtable2[i].u.isym->st_name);
7504 /* Sort symbol by name. */
7505 qsort (symtable1, count1, sizeof (struct elf_symbol),
7506 elf_sym_name_compare);
7507 qsort (symtable2, count1, sizeof (struct elf_symbol),
7508 elf_sym_name_compare);
7510 for (i = 0; i < count1; i++)
7511 /* Two symbols must have the same binding, type and name. */
7512 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7513 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7514 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7532 /* Return TRUE if 2 section types are compatible. */
7535 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7536 bfd *bbfd, const asection *bsec)
7540 || abfd->xvec->flavour != bfd_target_elf_flavour
7541 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7544 return elf_section_type (asec) == elf_section_type (bsec);
7547 /* Final phase of ELF linker. */
7549 /* A structure we use to avoid passing large numbers of arguments. */
7551 struct elf_final_link_info
7553 /* General link information. */
7554 struct bfd_link_info *info;
7557 /* Symbol string table. */
7558 struct elf_strtab_hash *symstrtab;
7559 /* .hash section. */
7561 /* symbol version section (.gnu.version). */
7562 asection *symver_sec;
7563 /* Buffer large enough to hold contents of any section. */
7565 /* Buffer large enough to hold external relocs of any section. */
7566 void *external_relocs;
7567 /* Buffer large enough to hold internal relocs of any section. */
7568 Elf_Internal_Rela *internal_relocs;
7569 /* Buffer large enough to hold external local symbols of any input
7571 bfd_byte *external_syms;
7572 /* And a buffer for symbol section indices. */
7573 Elf_External_Sym_Shndx *locsym_shndx;
7574 /* Buffer large enough to hold internal local symbols of any input
7576 Elf_Internal_Sym *internal_syms;
7577 /* Array large enough to hold a symbol index for each local symbol
7578 of any input BFD. */
7580 /* Array large enough to hold a section pointer for each local
7581 symbol of any input BFD. */
7582 asection **sections;
7583 /* Buffer for SHT_SYMTAB_SHNDX section. */
7584 Elf_External_Sym_Shndx *symshndxbuf;
7585 /* Number of STT_FILE syms seen. */
7586 size_t filesym_count;
7589 /* This struct is used to pass information to elf_link_output_extsym. */
7591 struct elf_outext_info
7594 bfd_boolean localsyms;
7595 bfd_boolean file_sym_done;
7596 struct elf_final_link_info *flinfo;
7600 /* Support for evaluating a complex relocation.
7602 Complex relocations are generalized, self-describing relocations. The
7603 implementation of them consists of two parts: complex symbols, and the
7604 relocations themselves.
7606 The relocations are use a reserved elf-wide relocation type code (R_RELC
7607 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7608 information (start bit, end bit, word width, etc) into the addend. This
7609 information is extracted from CGEN-generated operand tables within gas.
7611 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7612 internal) representing prefix-notation expressions, including but not
7613 limited to those sorts of expressions normally encoded as addends in the
7614 addend field. The symbol mangling format is:
7617 | <unary-operator> ':' <node>
7618 | <binary-operator> ':' <node> ':' <node>
7621 <literal> := 's' <digits=N> ':' <N character symbol name>
7622 | 'S' <digits=N> ':' <N character section name>
7626 <binary-operator> := as in C
7627 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7630 set_symbol_value (bfd *bfd_with_globals,
7631 Elf_Internal_Sym *isymbuf,
7636 struct elf_link_hash_entry **sym_hashes;
7637 struct elf_link_hash_entry *h;
7638 size_t extsymoff = locsymcount;
7640 if (symidx < locsymcount)
7642 Elf_Internal_Sym *sym;
7644 sym = isymbuf + symidx;
7645 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7647 /* It is a local symbol: move it to the
7648 "absolute" section and give it a value. */
7649 sym->st_shndx = SHN_ABS;
7650 sym->st_value = val;
7653 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7657 /* It is a global symbol: set its link type
7658 to "defined" and give it a value. */
7660 sym_hashes = elf_sym_hashes (bfd_with_globals);
7661 h = sym_hashes [symidx - extsymoff];
7662 while (h->root.type == bfd_link_hash_indirect
7663 || h->root.type == bfd_link_hash_warning)
7664 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7665 h->root.type = bfd_link_hash_defined;
7666 h->root.u.def.value = val;
7667 h->root.u.def.section = bfd_abs_section_ptr;
7671 resolve_symbol (const char *name,
7673 struct elf_final_link_info *flinfo,
7675 Elf_Internal_Sym *isymbuf,
7678 Elf_Internal_Sym *sym;
7679 struct bfd_link_hash_entry *global_entry;
7680 const char *candidate = NULL;
7681 Elf_Internal_Shdr *symtab_hdr;
7684 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7686 for (i = 0; i < locsymcount; ++ i)
7690 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7693 candidate = bfd_elf_string_from_elf_section (input_bfd,
7694 symtab_hdr->sh_link,
7697 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7698 name, candidate, (unsigned long) sym->st_value);
7700 if (candidate && strcmp (candidate, name) == 0)
7702 asection *sec = flinfo->sections [i];
7704 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7705 *result += sec->output_offset + sec->output_section->vma;
7707 printf ("Found symbol with value %8.8lx\n",
7708 (unsigned long) *result);
7714 /* Hmm, haven't found it yet. perhaps it is a global. */
7715 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7716 FALSE, FALSE, TRUE);
7720 if (global_entry->type == bfd_link_hash_defined
7721 || global_entry->type == bfd_link_hash_defweak)
7723 *result = (global_entry->u.def.value
7724 + global_entry->u.def.section->output_section->vma
7725 + global_entry->u.def.section->output_offset);
7727 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7728 global_entry->root.string, (unsigned long) *result);
7737 resolve_section (const char *name,
7744 for (curr = sections; curr; curr = curr->next)
7745 if (strcmp (curr->name, name) == 0)
7747 *result = curr->vma;
7751 /* Hmm. still haven't found it. try pseudo-section names. */
7752 for (curr = sections; curr; curr = curr->next)
7754 len = strlen (curr->name);
7755 if (len > strlen (name))
7758 if (strncmp (curr->name, name, len) == 0)
7760 if (strncmp (".end", name + len, 4) == 0)
7762 *result = curr->vma + curr->size;
7766 /* Insert more pseudo-section names here, if you like. */
7774 undefined_reference (const char *reftype, const char *name)
7776 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7781 eval_symbol (bfd_vma *result,
7784 struct elf_final_link_info *flinfo,
7786 Elf_Internal_Sym *isymbuf,
7795 const char *sym = *symp;
7797 bfd_boolean symbol_is_section = FALSE;
7802 if (len < 1 || len > sizeof (symbuf))
7804 bfd_set_error (bfd_error_invalid_operation);
7817 *result = strtoul (sym, (char **) symp, 16);
7821 symbol_is_section = TRUE;
7824 symlen = strtol (sym, (char **) symp, 10);
7825 sym = *symp + 1; /* Skip the trailing ':'. */
7827 if (symend < sym || symlen + 1 > sizeof (symbuf))
7829 bfd_set_error (bfd_error_invalid_operation);
7833 memcpy (symbuf, sym, symlen);
7834 symbuf[symlen] = '\0';
7835 *symp = sym + symlen;
7837 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7838 the symbol as a section, or vice-versa. so we're pretty liberal in our
7839 interpretation here; section means "try section first", not "must be a
7840 section", and likewise with symbol. */
7842 if (symbol_is_section)
7844 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7845 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7846 isymbuf, locsymcount))
7848 undefined_reference ("section", symbuf);
7854 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7855 isymbuf, locsymcount)
7856 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7859 undefined_reference ("symbol", symbuf);
7866 /* All that remains are operators. */
7868 #define UNARY_OP(op) \
7869 if (strncmp (sym, #op, strlen (#op)) == 0) \
7871 sym += strlen (#op); \
7875 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7876 isymbuf, locsymcount, signed_p)) \
7879 *result = op ((bfd_signed_vma) a); \
7885 #define BINARY_OP(op) \
7886 if (strncmp (sym, #op, strlen (#op)) == 0) \
7888 sym += strlen (#op); \
7892 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7893 isymbuf, locsymcount, signed_p)) \
7896 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7897 isymbuf, locsymcount, signed_p)) \
7900 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7930 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7931 bfd_set_error (bfd_error_invalid_operation);
7937 put_value (bfd_vma size,
7938 unsigned long chunksz,
7943 location += (size - chunksz);
7945 for (; size; size -= chunksz, location -= chunksz)
7950 bfd_put_8 (input_bfd, x, location);
7954 bfd_put_16 (input_bfd, x, location);
7958 bfd_put_32 (input_bfd, x, location);
7959 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7965 bfd_put_64 (input_bfd, x, location);
7966 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7979 get_value (bfd_vma size,
7980 unsigned long chunksz,
7987 /* Sanity checks. */
7988 BFD_ASSERT (chunksz <= sizeof (x)
7991 && (size % chunksz) == 0
7992 && input_bfd != NULL
7993 && location != NULL);
7995 if (chunksz == sizeof (x))
7997 BFD_ASSERT (size == chunksz);
7999 /* Make sure that we do not perform an undefined shift operation.
8000 We know that size == chunksz so there will only be one iteration
8001 of the loop below. */
8005 shift = 8 * chunksz;
8007 for (; size; size -= chunksz, location += chunksz)
8012 x = (x << shift) | bfd_get_8 (input_bfd, location);
8015 x = (x << shift) | bfd_get_16 (input_bfd, location);
8018 x = (x << shift) | bfd_get_32 (input_bfd, location);
8022 x = (x << shift) | bfd_get_64 (input_bfd, location);
8033 decode_complex_addend (unsigned long *start, /* in bits */
8034 unsigned long *oplen, /* in bits */
8035 unsigned long *len, /* in bits */
8036 unsigned long *wordsz, /* in bytes */
8037 unsigned long *chunksz, /* in bytes */
8038 unsigned long *lsb0_p,
8039 unsigned long *signed_p,
8040 unsigned long *trunc_p,
8041 unsigned long encoded)
8043 * start = encoded & 0x3F;
8044 * len = (encoded >> 6) & 0x3F;
8045 * oplen = (encoded >> 12) & 0x3F;
8046 * wordsz = (encoded >> 18) & 0xF;
8047 * chunksz = (encoded >> 22) & 0xF;
8048 * lsb0_p = (encoded >> 27) & 1;
8049 * signed_p = (encoded >> 28) & 1;
8050 * trunc_p = (encoded >> 29) & 1;
8053 bfd_reloc_status_type
8054 bfd_elf_perform_complex_relocation (bfd *input_bfd,
8055 asection *input_section ATTRIBUTE_UNUSED,
8057 Elf_Internal_Rela *rel,
8060 bfd_vma shift, x, mask;
8061 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
8062 bfd_reloc_status_type r;
8064 /* Perform this reloc, since it is complex.
8065 (this is not to say that it necessarily refers to a complex
8066 symbol; merely that it is a self-describing CGEN based reloc.
8067 i.e. the addend has the complete reloc information (bit start, end,
8068 word size, etc) encoded within it.). */
8070 decode_complex_addend (&start, &oplen, &len, &wordsz,
8071 &chunksz, &lsb0_p, &signed_p,
8072 &trunc_p, rel->r_addend);
8074 mask = (((1L << (len - 1)) - 1) << 1) | 1;
8077 shift = (start + 1) - len;
8079 shift = (8 * wordsz) - (start + len);
8081 /* FIXME: octets_per_byte. */
8082 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
8085 printf ("Doing complex reloc: "
8086 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
8087 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
8088 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
8089 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
8090 oplen, (unsigned long) x, (unsigned long) mask,
8091 (unsigned long) relocation);
8096 /* Now do an overflow check. */
8097 r = bfd_check_overflow ((signed_p
8098 ? complain_overflow_signed
8099 : complain_overflow_unsigned),
8100 len, 0, (8 * wordsz),
8104 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8107 printf (" relocation: %8.8lx\n"
8108 " shifted mask: %8.8lx\n"
8109 " shifted/masked reloc: %8.8lx\n"
8110 " result: %8.8lx\n",
8111 (unsigned long) relocation, (unsigned long) (mask << shift),
8112 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8114 /* FIXME: octets_per_byte. */
8115 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8119 /* Functions to read r_offset from external (target order) reloc
8120 entry. Faster than bfd_getl32 et al, because we let the compiler
8121 know the value is aligned. */
8124 ext32l_r_offset (const void *p)
8131 const union aligned32 *a
8132 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8134 uint32_t aval = ( (uint32_t) a->c[0]
8135 | (uint32_t) a->c[1] << 8
8136 | (uint32_t) a->c[2] << 16
8137 | (uint32_t) a->c[3] << 24);
8142 ext32b_r_offset (const void *p)
8149 const union aligned32 *a
8150 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
8152 uint32_t aval = ( (uint32_t) a->c[0] << 24
8153 | (uint32_t) a->c[1] << 16
8154 | (uint32_t) a->c[2] << 8
8155 | (uint32_t) a->c[3]);
8159 #ifdef BFD_HOST_64_BIT
8161 ext64l_r_offset (const void *p)
8168 const union aligned64 *a
8169 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8171 uint64_t aval = ( (uint64_t) a->c[0]
8172 | (uint64_t) a->c[1] << 8
8173 | (uint64_t) a->c[2] << 16
8174 | (uint64_t) a->c[3] << 24
8175 | (uint64_t) a->c[4] << 32
8176 | (uint64_t) a->c[5] << 40
8177 | (uint64_t) a->c[6] << 48
8178 | (uint64_t) a->c[7] << 56);
8183 ext64b_r_offset (const void *p)
8190 const union aligned64 *a
8191 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
8193 uint64_t aval = ( (uint64_t) a->c[0] << 56
8194 | (uint64_t) a->c[1] << 48
8195 | (uint64_t) a->c[2] << 40
8196 | (uint64_t) a->c[3] << 32
8197 | (uint64_t) a->c[4] << 24
8198 | (uint64_t) a->c[5] << 16
8199 | (uint64_t) a->c[6] << 8
8200 | (uint64_t) a->c[7]);
8205 /* When performing a relocatable link, the input relocations are
8206 preserved. But, if they reference global symbols, the indices
8207 referenced must be updated. Update all the relocations found in
8211 elf_link_adjust_relocs (bfd *abfd,
8212 struct bfd_elf_section_reloc_data *reldata,
8216 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8218 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8219 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8220 bfd_vma r_type_mask;
8222 unsigned int count = reldata->count;
8223 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8225 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8227 swap_in = bed->s->swap_reloc_in;
8228 swap_out = bed->s->swap_reloc_out;
8230 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8232 swap_in = bed->s->swap_reloca_in;
8233 swap_out = bed->s->swap_reloca_out;
8238 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8241 if (bed->s->arch_size == 32)
8248 r_type_mask = 0xffffffff;
8252 erela = reldata->hdr->contents;
8253 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8255 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8258 if (*rel_hash == NULL)
8261 BFD_ASSERT ((*rel_hash)->indx >= 0);
8263 (*swap_in) (abfd, erela, irela);
8264 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8265 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8266 | (irela[j].r_info & r_type_mask));
8267 (*swap_out) (abfd, irela, erela);
8270 if (sort && count != 0)
8272 bfd_vma (*ext_r_off) (const void *);
8275 bfd_byte *base, *end, *p, *loc;
8276 bfd_byte *buf = NULL;
8278 if (bed->s->arch_size == 32)
8280 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8281 ext_r_off = ext32l_r_offset;
8282 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8283 ext_r_off = ext32b_r_offset;
8289 #ifdef BFD_HOST_64_BIT
8290 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8291 ext_r_off = ext64l_r_offset;
8292 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8293 ext_r_off = ext64b_r_offset;
8299 /* Must use a stable sort here. A modified insertion sort,
8300 since the relocs are mostly sorted already. */
8301 elt_size = reldata->hdr->sh_entsize;
8302 base = reldata->hdr->contents;
8303 end = base + count * elt_size;
8304 if (elt_size > sizeof (Elf64_External_Rela))
8307 /* Ensure the first element is lowest. This acts as a sentinel,
8308 speeding the main loop below. */
8309 r_off = (*ext_r_off) (base);
8310 for (p = loc = base; (p += elt_size) < end; )
8312 bfd_vma r_off2 = (*ext_r_off) (p);
8321 /* Don't just swap *base and *loc as that changes the order
8322 of the original base[0] and base[1] if they happen to
8323 have the same r_offset. */
8324 bfd_byte onebuf[sizeof (Elf64_External_Rela)];
8325 memcpy (onebuf, loc, elt_size);
8326 memmove (base + elt_size, base, loc - base);
8327 memcpy (base, onebuf, elt_size);
8330 for (p = base + elt_size; (p += elt_size) < end; )
8332 /* base to p is sorted, *p is next to insert. */
8333 r_off = (*ext_r_off) (p);
8334 /* Search the sorted region for location to insert. */
8336 while (r_off < (*ext_r_off) (loc))
8341 /* Chances are there is a run of relocs to insert here,
8342 from one of more input files. Files are not always
8343 linked in order due to the way elf_link_input_bfd is
8344 called. See pr17666. */
8345 size_t sortlen = p - loc;
8346 bfd_vma r_off2 = (*ext_r_off) (loc);
8347 size_t runlen = elt_size;
8348 size_t buf_size = 96 * 1024;
8349 while (p + runlen < end
8350 && (sortlen <= buf_size
8351 || runlen + elt_size <= buf_size)
8352 && r_off2 > (*ext_r_off) (p + runlen))
8356 buf = bfd_malloc (buf_size);
8360 if (runlen < sortlen)
8362 memcpy (buf, p, runlen);
8363 memmove (loc + runlen, loc, sortlen);
8364 memcpy (loc, buf, runlen);
8368 memcpy (buf, loc, sortlen);
8369 memmove (loc, p, runlen);
8370 memcpy (loc + runlen, buf, sortlen);
8372 p += runlen - elt_size;
8375 /* Hashes are no longer valid. */
8376 free (reldata->hashes);
8377 reldata->hashes = NULL;
8383 struct elf_link_sort_rela
8389 enum elf_reloc_type_class type;
8390 /* We use this as an array of size int_rels_per_ext_rel. */
8391 Elf_Internal_Rela rela[1];
8395 elf_link_sort_cmp1 (const void *A, const void *B)
8397 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8398 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8399 int relativea, relativeb;
8401 relativea = a->type == reloc_class_relative;
8402 relativeb = b->type == reloc_class_relative;
8404 if (relativea < relativeb)
8406 if (relativea > relativeb)
8408 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8410 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8412 if (a->rela->r_offset < b->rela->r_offset)
8414 if (a->rela->r_offset > b->rela->r_offset)
8420 elf_link_sort_cmp2 (const void *A, const void *B)
8422 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8423 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8425 if (a->type < b->type)
8427 if (a->type > b->type)
8429 if (a->u.offset < b->u.offset)
8431 if (a->u.offset > b->u.offset)
8433 if (a->rela->r_offset < b->rela->r_offset)
8435 if (a->rela->r_offset > b->rela->r_offset)
8441 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8443 asection *dynamic_relocs;
8446 bfd_size_type count, size;
8447 size_t i, ret, sort_elt, ext_size;
8448 bfd_byte *sort, *s_non_relative, *p;
8449 struct elf_link_sort_rela *sq;
8450 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8451 int i2e = bed->s->int_rels_per_ext_rel;
8452 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8453 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8454 struct bfd_link_order *lo;
8456 bfd_boolean use_rela;
8458 /* Find a dynamic reloc section. */
8459 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8460 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8461 if (rela_dyn != NULL && rela_dyn->size > 0
8462 && rel_dyn != NULL && rel_dyn->size > 0)
8464 bfd_boolean use_rela_initialised = FALSE;
8466 /* This is just here to stop gcc from complaining.
8467 It's initialization checking code is not perfect. */
8470 /* Both sections are present. Examine the sizes
8471 of the indirect sections to help us choose. */
8472 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8473 if (lo->type == bfd_indirect_link_order)
8475 asection *o = lo->u.indirect.section;
8477 if ((o->size % bed->s->sizeof_rela) == 0)
8479 if ((o->size % bed->s->sizeof_rel) == 0)
8480 /* Section size is divisible by both rel and rela sizes.
8481 It is of no help to us. */
8485 /* Section size is only divisible by rela. */
8486 if (use_rela_initialised && (use_rela == FALSE))
8489 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8490 bfd_set_error (bfd_error_invalid_operation);
8496 use_rela_initialised = TRUE;
8500 else if ((o->size % bed->s->sizeof_rel) == 0)
8502 /* Section size is only divisible by rel. */
8503 if (use_rela_initialised && (use_rela == TRUE))
8506 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8507 bfd_set_error (bfd_error_invalid_operation);
8513 use_rela_initialised = TRUE;
8518 /* The section size is not divisible by either - something is wrong. */
8520 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8521 bfd_set_error (bfd_error_invalid_operation);
8526 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8527 if (lo->type == bfd_indirect_link_order)
8529 asection *o = lo->u.indirect.section;
8531 if ((o->size % bed->s->sizeof_rela) == 0)
8533 if ((o->size % bed->s->sizeof_rel) == 0)
8534 /* Section size is divisible by both rel and rela sizes.
8535 It is of no help to us. */
8539 /* Section size is only divisible by rela. */
8540 if (use_rela_initialised && (use_rela == FALSE))
8543 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8544 bfd_set_error (bfd_error_invalid_operation);
8550 use_rela_initialised = TRUE;
8554 else if ((o->size % bed->s->sizeof_rel) == 0)
8556 /* Section size is only divisible by rel. */
8557 if (use_rela_initialised && (use_rela == TRUE))
8560 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8561 bfd_set_error (bfd_error_invalid_operation);
8567 use_rela_initialised = TRUE;
8572 /* The section size is not divisible by either - something is wrong. */
8574 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8575 bfd_set_error (bfd_error_invalid_operation);
8580 if (! use_rela_initialised)
8584 else if (rela_dyn != NULL && rela_dyn->size > 0)
8586 else if (rel_dyn != NULL && rel_dyn->size > 0)
8593 dynamic_relocs = rela_dyn;
8594 ext_size = bed->s->sizeof_rela;
8595 swap_in = bed->s->swap_reloca_in;
8596 swap_out = bed->s->swap_reloca_out;
8600 dynamic_relocs = rel_dyn;
8601 ext_size = bed->s->sizeof_rel;
8602 swap_in = bed->s->swap_reloc_in;
8603 swap_out = bed->s->swap_reloc_out;
8607 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8608 if (lo->type == bfd_indirect_link_order)
8609 size += lo->u.indirect.section->size;
8611 if (size != dynamic_relocs->size)
8614 sort_elt = (sizeof (struct elf_link_sort_rela)
8615 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8617 count = dynamic_relocs->size / ext_size;
8620 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8624 (*info->callbacks->warning)
8625 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8629 if (bed->s->arch_size == 32)
8630 r_sym_mask = ~(bfd_vma) 0xff;
8632 r_sym_mask = ~(bfd_vma) 0xffffffff;
8634 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8635 if (lo->type == bfd_indirect_link_order)
8637 bfd_byte *erel, *erelend;
8638 asection *o = lo->u.indirect.section;
8640 if (o->contents == NULL && o->size != 0)
8642 /* This is a reloc section that is being handled as a normal
8643 section. See bfd_section_from_shdr. We can't combine
8644 relocs in this case. */
8649 erelend = o->contents + o->size;
8650 /* FIXME: octets_per_byte. */
8651 p = sort + o->output_offset / ext_size * sort_elt;
8653 while (erel < erelend)
8655 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8657 (*swap_in) (abfd, erel, s->rela);
8658 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8659 s->u.sym_mask = r_sym_mask;
8665 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8667 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8669 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8670 if (s->type != reloc_class_relative)
8676 sq = (struct elf_link_sort_rela *) s_non_relative;
8677 for (; i < count; i++, p += sort_elt)
8679 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8680 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8682 sp->u.offset = sq->rela->r_offset;
8685 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8687 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8688 if (lo->type == bfd_indirect_link_order)
8690 bfd_byte *erel, *erelend;
8691 asection *o = lo->u.indirect.section;
8694 erelend = o->contents + o->size;
8695 /* FIXME: octets_per_byte. */
8696 p = sort + o->output_offset / ext_size * sort_elt;
8697 while (erel < erelend)
8699 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8700 (*swap_out) (abfd, s->rela, erel);
8707 *psec = dynamic_relocs;
8711 /* Add a symbol to the output symbol string table. */
8714 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8716 Elf_Internal_Sym *elfsym,
8717 asection *input_sec,
8718 struct elf_link_hash_entry *h)
8720 int (*output_symbol_hook)
8721 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8722 struct elf_link_hash_entry *);
8723 struct elf_link_hash_table *hash_table;
8724 const struct elf_backend_data *bed;
8725 bfd_size_type strtabsize;
8727 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8729 bed = get_elf_backend_data (flinfo->output_bfd);
8730 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8731 if (output_symbol_hook != NULL)
8733 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8740 || (input_sec->flags & SEC_EXCLUDE))
8741 elfsym->st_name = (unsigned long) -1;
8744 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8745 to get the final offset for st_name. */
8747 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8749 if (elfsym->st_name == (unsigned long) -1)
8753 hash_table = elf_hash_table (flinfo->info);
8754 strtabsize = hash_table->strtabsize;
8755 if (strtabsize <= hash_table->strtabcount)
8757 strtabsize += strtabsize;
8758 hash_table->strtabsize = strtabsize;
8759 strtabsize *= sizeof (*hash_table->strtab);
8761 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8763 if (hash_table->strtab == NULL)
8766 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8767 hash_table->strtab[hash_table->strtabcount].dest_index
8768 = hash_table->strtabcount;
8769 hash_table->strtab[hash_table->strtabcount].destshndx_index
8770 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8772 bfd_get_symcount (flinfo->output_bfd) += 1;
8773 hash_table->strtabcount += 1;
8778 /* Swap symbols out to the symbol table and flush the output symbols to
8782 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8784 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8785 bfd_size_type amt, i;
8786 const struct elf_backend_data *bed;
8788 Elf_Internal_Shdr *hdr;
8792 if (!hash_table->strtabcount)
8795 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8797 bed = get_elf_backend_data (flinfo->output_bfd);
8799 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8800 symbuf = (bfd_byte *) bfd_malloc (amt);
8804 if (flinfo->symshndxbuf)
8806 amt = (sizeof (Elf_External_Sym_Shndx)
8807 * (bfd_get_symcount (flinfo->output_bfd)));
8808 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8809 if (flinfo->symshndxbuf == NULL)
8816 for (i = 0; i < hash_table->strtabcount; i++)
8818 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8819 if (elfsym->sym.st_name == (unsigned long) -1)
8820 elfsym->sym.st_name = 0;
8823 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8824 elfsym->sym.st_name);
8825 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8826 ((bfd_byte *) symbuf
8827 + (elfsym->dest_index
8828 * bed->s->sizeof_sym)),
8829 (flinfo->symshndxbuf
8830 + elfsym->destshndx_index));
8833 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8834 pos = hdr->sh_offset + hdr->sh_size;
8835 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8836 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8837 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8839 hdr->sh_size += amt;
8847 free (hash_table->strtab);
8848 hash_table->strtab = NULL;
8853 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8856 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8858 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8859 && sym->st_shndx < SHN_LORESERVE)
8861 /* The gABI doesn't support dynamic symbols in output sections
8863 (*_bfd_error_handler)
8864 (_("%B: Too many sections: %d (>= %d)"),
8865 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8866 bfd_set_error (bfd_error_nonrepresentable_section);
8872 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8873 allowing an unsatisfied unversioned symbol in the DSO to match a
8874 versioned symbol that would normally require an explicit version.
8875 We also handle the case that a DSO references a hidden symbol
8876 which may be satisfied by a versioned symbol in another DSO. */
8879 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8880 const struct elf_backend_data *bed,
8881 struct elf_link_hash_entry *h)
8884 struct elf_link_loaded_list *loaded;
8886 if (!is_elf_hash_table (info->hash))
8889 /* Check indirect symbol. */
8890 while (h->root.type == bfd_link_hash_indirect)
8891 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8893 switch (h->root.type)
8899 case bfd_link_hash_undefined:
8900 case bfd_link_hash_undefweak:
8901 abfd = h->root.u.undef.abfd;
8902 if ((abfd->flags & DYNAMIC) == 0
8903 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8907 case bfd_link_hash_defined:
8908 case bfd_link_hash_defweak:
8909 abfd = h->root.u.def.section->owner;
8912 case bfd_link_hash_common:
8913 abfd = h->root.u.c.p->section->owner;
8916 BFD_ASSERT (abfd != NULL);
8918 for (loaded = elf_hash_table (info)->loaded;
8920 loaded = loaded->next)
8923 Elf_Internal_Shdr *hdr;
8924 bfd_size_type symcount;
8925 bfd_size_type extsymcount;
8926 bfd_size_type extsymoff;
8927 Elf_Internal_Shdr *versymhdr;
8928 Elf_Internal_Sym *isym;
8929 Elf_Internal_Sym *isymend;
8930 Elf_Internal_Sym *isymbuf;
8931 Elf_External_Versym *ever;
8932 Elf_External_Versym *extversym;
8934 input = loaded->abfd;
8936 /* We check each DSO for a possible hidden versioned definition. */
8938 || (input->flags & DYNAMIC) == 0
8939 || elf_dynversym (input) == 0)
8942 hdr = &elf_tdata (input)->dynsymtab_hdr;
8944 symcount = hdr->sh_size / bed->s->sizeof_sym;
8945 if (elf_bad_symtab (input))
8947 extsymcount = symcount;
8952 extsymcount = symcount - hdr->sh_info;
8953 extsymoff = hdr->sh_info;
8956 if (extsymcount == 0)
8959 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8961 if (isymbuf == NULL)
8964 /* Read in any version definitions. */
8965 versymhdr = &elf_tdata (input)->dynversym_hdr;
8966 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8967 if (extversym == NULL)
8970 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8971 || (bfd_bread (extversym, versymhdr->sh_size, input)
8972 != versymhdr->sh_size))
8980 ever = extversym + extsymoff;
8981 isymend = isymbuf + extsymcount;
8982 for (isym = isymbuf; isym < isymend; isym++, ever++)
8985 Elf_Internal_Versym iver;
8986 unsigned short version_index;
8988 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8989 || isym->st_shndx == SHN_UNDEF)
8992 name = bfd_elf_string_from_elf_section (input,
8995 if (strcmp (name, h->root.root.string) != 0)
8998 _bfd_elf_swap_versym_in (input, ever, &iver);
9000 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
9002 && h->forced_local))
9004 /* If we have a non-hidden versioned sym, then it should
9005 have provided a definition for the undefined sym unless
9006 it is defined in a non-shared object and forced local.
9011 version_index = iver.vs_vers & VERSYM_VERSION;
9012 if (version_index == 1 || version_index == 2)
9014 /* This is the base or first version. We can use it. */
9028 /* Add an external symbol to the symbol table. This is called from
9029 the hash table traversal routine. When generating a shared object,
9030 we go through the symbol table twice. The first time we output
9031 anything that might have been forced to local scope in a version
9032 script. The second time we output the symbols that are still
9036 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
9038 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
9039 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
9040 struct elf_final_link_info *flinfo = eoinfo->flinfo;
9042 Elf_Internal_Sym sym;
9043 asection *input_sec;
9044 const struct elf_backend_data *bed;
9047 /* A symbol is bound locally if it is forced local or it is locally
9048 defined, hidden versioned, not referenced by shared library and
9049 not exported when linking executable. */
9050 bfd_boolean local_bind = (h->forced_local
9051 || (bfd_link_executable (flinfo->info)
9052 && !flinfo->info->export_dynamic
9056 && h->versioned == versioned_hidden));
9058 if (h->root.type == bfd_link_hash_warning)
9060 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9061 if (h->root.type == bfd_link_hash_new)
9065 /* Decide whether to output this symbol in this pass. */
9066 if (eoinfo->localsyms)
9077 bed = get_elf_backend_data (flinfo->output_bfd);
9079 if (h->root.type == bfd_link_hash_undefined)
9081 /* If we have an undefined symbol reference here then it must have
9082 come from a shared library that is being linked in. (Undefined
9083 references in regular files have already been handled unless
9084 they are in unreferenced sections which are removed by garbage
9086 bfd_boolean ignore_undef = FALSE;
9088 /* Some symbols may be special in that the fact that they're
9089 undefined can be safely ignored - let backend determine that. */
9090 if (bed->elf_backend_ignore_undef_symbol)
9091 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
9093 /* If we are reporting errors for this situation then do so now. */
9096 && (!h->ref_regular || flinfo->info->gc_sections)
9097 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
9098 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
9100 if (!(flinfo->info->callbacks->undefined_symbol
9101 (flinfo->info, h->root.root.string,
9102 h->ref_regular ? NULL : h->root.u.undef.abfd,
9104 (flinfo->info->unresolved_syms_in_shared_libs
9105 == RM_GENERATE_ERROR))))
9107 bfd_set_error (bfd_error_bad_value);
9108 eoinfo->failed = TRUE;
9114 /* We should also warn if a forced local symbol is referenced from
9115 shared libraries. */
9116 if (bfd_link_executable (flinfo->info)
9121 && h->ref_dynamic_nonweak
9122 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
9126 struct elf_link_hash_entry *hi = h;
9128 /* Check indirect symbol. */
9129 while (hi->root.type == bfd_link_hash_indirect)
9130 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
9132 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
9133 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
9134 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
9135 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
9137 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
9138 def_bfd = flinfo->output_bfd;
9139 if (hi->root.u.def.section != bfd_abs_section_ptr)
9140 def_bfd = hi->root.u.def.section->owner;
9141 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
9142 h->root.root.string);
9143 bfd_set_error (bfd_error_bad_value);
9144 eoinfo->failed = TRUE;
9148 /* We don't want to output symbols that have never been mentioned by
9149 a regular file, or that we have been told to strip. However, if
9150 h->indx is set to -2, the symbol is used by a reloc and we must
9155 else if ((h->def_dynamic
9157 || h->root.type == bfd_link_hash_new)
9161 else if (flinfo->info->strip == strip_all)
9163 else if (flinfo->info->strip == strip_some
9164 && bfd_hash_lookup (flinfo->info->keep_hash,
9165 h->root.root.string, FALSE, FALSE) == NULL)
9167 else if ((h->root.type == bfd_link_hash_defined
9168 || h->root.type == bfd_link_hash_defweak)
9169 && ((flinfo->info->strip_discarded
9170 && discarded_section (h->root.u.def.section))
9171 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9172 && h->root.u.def.section->owner != NULL
9173 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9175 else if ((h->root.type == bfd_link_hash_undefined
9176 || h->root.type == bfd_link_hash_undefweak)
9177 && h->root.u.undef.abfd != NULL
9178 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9181 /* If we're stripping it, and it's not a dynamic symbol, there's
9182 nothing else to do. However, if it is a forced local symbol or
9183 an ifunc symbol we need to give the backend finish_dynamic_symbol
9184 function a chance to make it dynamic. */
9187 && h->type != STT_GNU_IFUNC
9188 && !h->forced_local)
9192 sym.st_size = h->size;
9193 sym.st_other = h->other;
9196 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9197 /* Turn off visibility on local symbol. */
9198 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9200 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9201 else if (h->unique_global && h->def_regular)
9202 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9203 else if (h->root.type == bfd_link_hash_undefweak
9204 || h->root.type == bfd_link_hash_defweak)
9205 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9207 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9208 sym.st_target_internal = h->target_internal;
9210 switch (h->root.type)
9213 case bfd_link_hash_new:
9214 case bfd_link_hash_warning:
9218 case bfd_link_hash_undefined:
9219 case bfd_link_hash_undefweak:
9220 input_sec = bfd_und_section_ptr;
9221 sym.st_shndx = SHN_UNDEF;
9224 case bfd_link_hash_defined:
9225 case bfd_link_hash_defweak:
9227 input_sec = h->root.u.def.section;
9228 if (input_sec->output_section != NULL)
9231 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9232 input_sec->output_section);
9233 if (sym.st_shndx == SHN_BAD)
9235 (*_bfd_error_handler)
9236 (_("%B: could not find output section %A for input section %A"),
9237 flinfo->output_bfd, input_sec->output_section, input_sec);
9238 bfd_set_error (bfd_error_nonrepresentable_section);
9239 eoinfo->failed = TRUE;
9243 /* ELF symbols in relocatable files are section relative,
9244 but in nonrelocatable files they are virtual
9246 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9247 if (!bfd_link_relocatable (flinfo->info))
9249 sym.st_value += input_sec->output_section->vma;
9250 if (h->type == STT_TLS)
9252 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9253 if (tls_sec != NULL)
9254 sym.st_value -= tls_sec->vma;
9260 BFD_ASSERT (input_sec->owner == NULL
9261 || (input_sec->owner->flags & DYNAMIC) != 0);
9262 sym.st_shndx = SHN_UNDEF;
9263 input_sec = bfd_und_section_ptr;
9268 case bfd_link_hash_common:
9269 input_sec = h->root.u.c.p->section;
9270 sym.st_shndx = bed->common_section_index (input_sec);
9271 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9274 case bfd_link_hash_indirect:
9275 /* These symbols are created by symbol versioning. They point
9276 to the decorated version of the name. For example, if the
9277 symbol foo@@GNU_1.2 is the default, which should be used when
9278 foo is used with no version, then we add an indirect symbol
9279 foo which points to foo@@GNU_1.2. We ignore these symbols,
9280 since the indirected symbol is already in the hash table. */
9284 /* Give the processor backend a chance to tweak the symbol value,
9285 and also to finish up anything that needs to be done for this
9286 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9287 forced local syms when non-shared is due to a historical quirk.
9288 STT_GNU_IFUNC symbol must go through PLT. */
9289 if ((h->type == STT_GNU_IFUNC
9291 && !bfd_link_relocatable (flinfo->info))
9292 || ((h->dynindx != -1
9294 && ((bfd_link_pic (flinfo->info)
9295 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9296 || h->root.type != bfd_link_hash_undefweak))
9297 || !h->forced_local)
9298 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9300 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9301 (flinfo->output_bfd, flinfo->info, h, &sym)))
9303 eoinfo->failed = TRUE;
9308 /* If we are marking the symbol as undefined, and there are no
9309 non-weak references to this symbol from a regular object, then
9310 mark the symbol as weak undefined; if there are non-weak
9311 references, mark the symbol as strong. We can't do this earlier,
9312 because it might not be marked as undefined until the
9313 finish_dynamic_symbol routine gets through with it. */
9314 if (sym.st_shndx == SHN_UNDEF
9316 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9317 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9320 unsigned int type = ELF_ST_TYPE (sym.st_info);
9322 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9323 if (type == STT_GNU_IFUNC)
9326 if (h->ref_regular_nonweak)
9327 bindtype = STB_GLOBAL;
9329 bindtype = STB_WEAK;
9330 sym.st_info = ELF_ST_INFO (bindtype, type);
9333 /* If this is a symbol defined in a dynamic library, don't use the
9334 symbol size from the dynamic library. Relinking an executable
9335 against a new library may introduce gratuitous changes in the
9336 executable's symbols if we keep the size. */
9337 if (sym.st_shndx == SHN_UNDEF
9342 /* If a non-weak symbol with non-default visibility is not defined
9343 locally, it is a fatal error. */
9344 if (!bfd_link_relocatable (flinfo->info)
9345 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9346 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9347 && h->root.type == bfd_link_hash_undefined
9352 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9353 msg = _("%B: protected symbol `%s' isn't defined");
9354 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9355 msg = _("%B: internal symbol `%s' isn't defined");
9357 msg = _("%B: hidden symbol `%s' isn't defined");
9358 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9359 bfd_set_error (bfd_error_bad_value);
9360 eoinfo->failed = TRUE;
9364 /* If this symbol should be put in the .dynsym section, then put it
9365 there now. We already know the symbol index. We also fill in
9366 the entry in the .hash section. */
9367 if (elf_hash_table (flinfo->info)->dynsym != NULL
9369 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9373 /* Since there is no version information in the dynamic string,
9374 if there is no version info in symbol version section, we will
9375 have a run-time problem if not linking executable, referenced
9376 by shared library, not locally defined, or not bound locally.
9378 if (h->verinfo.verdef == NULL
9380 && (!bfd_link_executable (flinfo->info)
9382 || !h->def_regular))
9384 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9386 if (p && p [1] != '\0')
9388 (*_bfd_error_handler)
9389 (_("%B: No symbol version section for versioned symbol `%s'"),
9390 flinfo->output_bfd, h->root.root.string);
9391 eoinfo->failed = TRUE;
9396 sym.st_name = h->dynstr_index;
9397 esym = (elf_hash_table (flinfo->info)->dynsym->contents
9398 + h->dynindx * bed->s->sizeof_sym);
9399 if (!check_dynsym (flinfo->output_bfd, &sym))
9401 eoinfo->failed = TRUE;
9404 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9406 if (flinfo->hash_sec != NULL)
9408 size_t hash_entry_size;
9409 bfd_byte *bucketpos;
9414 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9415 bucket = h->u.elf_hash_value % bucketcount;
9418 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9419 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9420 + (bucket + 2) * hash_entry_size);
9421 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9422 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9424 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9425 ((bfd_byte *) flinfo->hash_sec->contents
9426 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9429 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9431 Elf_Internal_Versym iversym;
9432 Elf_External_Versym *eversym;
9434 if (!h->def_regular)
9436 if (h->verinfo.verdef == NULL
9437 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9438 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9439 iversym.vs_vers = 0;
9441 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9445 if (h->verinfo.vertree == NULL)
9446 iversym.vs_vers = 1;
9448 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9449 if (flinfo->info->create_default_symver)
9453 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
9455 if (h->versioned == versioned_hidden && h->def_regular)
9456 iversym.vs_vers |= VERSYM_HIDDEN;
9458 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9459 eversym += h->dynindx;
9460 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9464 /* If the symbol is undefined, and we didn't output it to .dynsym,
9465 strip it from .symtab too. Obviously we can't do this for
9466 relocatable output or when needed for --emit-relocs. */
9467 else if (input_sec == bfd_und_section_ptr
9469 && !bfd_link_relocatable (flinfo->info))
9471 /* Also strip others that we couldn't earlier due to dynamic symbol
9475 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9478 /* Output a FILE symbol so that following locals are not associated
9479 with the wrong input file. We need one for forced local symbols
9480 if we've seen more than one FILE symbol or when we have exactly
9481 one FILE symbol but global symbols are present in a file other
9482 than the one with the FILE symbol. We also need one if linker
9483 defined symbols are present. In practice these conditions are
9484 always met, so just emit the FILE symbol unconditionally. */
9485 if (eoinfo->localsyms
9486 && !eoinfo->file_sym_done
9487 && eoinfo->flinfo->filesym_count != 0)
9489 Elf_Internal_Sym fsym;
9491 memset (&fsym, 0, sizeof (fsym));
9492 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9493 fsym.st_shndx = SHN_ABS;
9494 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9495 bfd_und_section_ptr, NULL))
9498 eoinfo->file_sym_done = TRUE;
9501 indx = bfd_get_symcount (flinfo->output_bfd);
9502 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9506 eoinfo->failed = TRUE;
9511 else if (h->indx == -2)
9517 /* Return TRUE if special handling is done for relocs in SEC against
9518 symbols defined in discarded sections. */
9521 elf_section_ignore_discarded_relocs (asection *sec)
9523 const struct elf_backend_data *bed;
9525 switch (sec->sec_info_type)
9527 case SEC_INFO_TYPE_STABS:
9528 case SEC_INFO_TYPE_EH_FRAME:
9529 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9535 bed = get_elf_backend_data (sec->owner);
9536 if (bed->elf_backend_ignore_discarded_relocs != NULL
9537 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9543 /* Return a mask saying how ld should treat relocations in SEC against
9544 symbols defined in discarded sections. If this function returns
9545 COMPLAIN set, ld will issue a warning message. If this function
9546 returns PRETEND set, and the discarded section was link-once and the
9547 same size as the kept link-once section, ld will pretend that the
9548 symbol was actually defined in the kept section. Otherwise ld will
9549 zero the reloc (at least that is the intent, but some cooperation by
9550 the target dependent code is needed, particularly for REL targets). */
9553 _bfd_elf_default_action_discarded (asection *sec)
9555 if (sec->flags & SEC_DEBUGGING)
9558 if (strcmp (".eh_frame", sec->name) == 0)
9561 if (strcmp (".gcc_except_table", sec->name) == 0)
9564 return COMPLAIN | PRETEND;
9567 /* Find a match between a section and a member of a section group. */
9570 match_group_member (asection *sec, asection *group,
9571 struct bfd_link_info *info)
9573 asection *first = elf_next_in_group (group);
9574 asection *s = first;
9578 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9581 s = elf_next_in_group (s);
9589 /* Check if the kept section of a discarded section SEC can be used
9590 to replace it. Return the replacement if it is OK. Otherwise return
9594 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9598 kept = sec->kept_section;
9601 if ((kept->flags & SEC_GROUP) != 0)
9602 kept = match_group_member (sec, kept, info);
9604 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9605 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9607 sec->kept_section = kept;
9612 /* Link an input file into the linker output file. This function
9613 handles all the sections and relocations of the input file at once.
9614 This is so that we only have to read the local symbols once, and
9615 don't have to keep them in memory. */
9618 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9620 int (*relocate_section)
9621 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9622 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9624 Elf_Internal_Shdr *symtab_hdr;
9627 Elf_Internal_Sym *isymbuf;
9628 Elf_Internal_Sym *isym;
9629 Elf_Internal_Sym *isymend;
9631 asection **ppsection;
9633 const struct elf_backend_data *bed;
9634 struct elf_link_hash_entry **sym_hashes;
9635 bfd_size_type address_size;
9636 bfd_vma r_type_mask;
9638 bfd_boolean have_file_sym = FALSE;
9640 output_bfd = flinfo->output_bfd;
9641 bed = get_elf_backend_data (output_bfd);
9642 relocate_section = bed->elf_backend_relocate_section;
9644 /* If this is a dynamic object, we don't want to do anything here:
9645 we don't want the local symbols, and we don't want the section
9647 if ((input_bfd->flags & DYNAMIC) != 0)
9650 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9651 if (elf_bad_symtab (input_bfd))
9653 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9658 locsymcount = symtab_hdr->sh_info;
9659 extsymoff = symtab_hdr->sh_info;
9662 /* Read the local symbols. */
9663 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9664 if (isymbuf == NULL && locsymcount != 0)
9666 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9667 flinfo->internal_syms,
9668 flinfo->external_syms,
9669 flinfo->locsym_shndx);
9670 if (isymbuf == NULL)
9674 /* Find local symbol sections and adjust values of symbols in
9675 SEC_MERGE sections. Write out those local symbols we know are
9676 going into the output file. */
9677 isymend = isymbuf + locsymcount;
9678 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9680 isym++, pindex++, ppsection++)
9684 Elf_Internal_Sym osym;
9690 if (elf_bad_symtab (input_bfd))
9692 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9699 if (isym->st_shndx == SHN_UNDEF)
9700 isec = bfd_und_section_ptr;
9701 else if (isym->st_shndx == SHN_ABS)
9702 isec = bfd_abs_section_ptr;
9703 else if (isym->st_shndx == SHN_COMMON)
9704 isec = bfd_com_section_ptr;
9707 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9710 /* Don't attempt to output symbols with st_shnx in the
9711 reserved range other than SHN_ABS and SHN_COMMON. */
9715 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9716 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9718 _bfd_merged_section_offset (output_bfd, &isec,
9719 elf_section_data (isec)->sec_info,
9725 /* Don't output the first, undefined, symbol. In fact, don't
9726 output any undefined local symbol. */
9727 if (isec == bfd_und_section_ptr)
9730 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9732 /* We never output section symbols. Instead, we use the
9733 section symbol of the corresponding section in the output
9738 /* If we are stripping all symbols, we don't want to output this
9740 if (flinfo->info->strip == strip_all)
9743 /* If we are discarding all local symbols, we don't want to
9744 output this one. If we are generating a relocatable output
9745 file, then some of the local symbols may be required by
9746 relocs; we output them below as we discover that they are
9748 if (flinfo->info->discard == discard_all)
9751 /* If this symbol is defined in a section which we are
9752 discarding, we don't need to keep it. */
9753 if (isym->st_shndx != SHN_UNDEF
9754 && isym->st_shndx < SHN_LORESERVE
9755 && bfd_section_removed_from_list (output_bfd,
9756 isec->output_section))
9759 /* Get the name of the symbol. */
9760 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9765 /* See if we are discarding symbols with this name. */
9766 if ((flinfo->info->strip == strip_some
9767 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9769 || (((flinfo->info->discard == discard_sec_merge
9770 && (isec->flags & SEC_MERGE)
9771 && !bfd_link_relocatable (flinfo->info))
9772 || flinfo->info->discard == discard_l)
9773 && bfd_is_local_label_name (input_bfd, name)))
9776 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9778 if (input_bfd->lto_output)
9779 /* -flto puts a temp file name here. This means builds
9780 are not reproducible. Discard the symbol. */
9782 have_file_sym = TRUE;
9783 flinfo->filesym_count += 1;
9787 /* In the absence of debug info, bfd_find_nearest_line uses
9788 FILE symbols to determine the source file for local
9789 function symbols. Provide a FILE symbol here if input
9790 files lack such, so that their symbols won't be
9791 associated with a previous input file. It's not the
9792 source file, but the best we can do. */
9793 have_file_sym = TRUE;
9794 flinfo->filesym_count += 1;
9795 memset (&osym, 0, sizeof (osym));
9796 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9797 osym.st_shndx = SHN_ABS;
9798 if (!elf_link_output_symstrtab (flinfo,
9799 (input_bfd->lto_output ? NULL
9800 : input_bfd->filename),
9801 &osym, bfd_abs_section_ptr,
9808 /* Adjust the section index for the output file. */
9809 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9810 isec->output_section);
9811 if (osym.st_shndx == SHN_BAD)
9814 /* ELF symbols in relocatable files are section relative, but
9815 in executable files they are virtual addresses. Note that
9816 this code assumes that all ELF sections have an associated
9817 BFD section with a reasonable value for output_offset; below
9818 we assume that they also have a reasonable value for
9819 output_section. Any special sections must be set up to meet
9820 these requirements. */
9821 osym.st_value += isec->output_offset;
9822 if (!bfd_link_relocatable (flinfo->info))
9824 osym.st_value += isec->output_section->vma;
9825 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9827 /* STT_TLS symbols are relative to PT_TLS segment base. */
9828 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9829 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9833 indx = bfd_get_symcount (output_bfd);
9834 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9841 if (bed->s->arch_size == 32)
9849 r_type_mask = 0xffffffff;
9854 /* Relocate the contents of each section. */
9855 sym_hashes = elf_sym_hashes (input_bfd);
9856 for (o = input_bfd->sections; o != NULL; o = o->next)
9860 if (! o->linker_mark)
9862 /* This section was omitted from the link. */
9866 if (bfd_link_relocatable (flinfo->info)
9867 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9869 /* Deal with the group signature symbol. */
9870 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9871 unsigned long symndx = sec_data->this_hdr.sh_info;
9872 asection *osec = o->output_section;
9874 if (symndx >= locsymcount
9875 || (elf_bad_symtab (input_bfd)
9876 && flinfo->sections[symndx] == NULL))
9878 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9879 while (h->root.type == bfd_link_hash_indirect
9880 || h->root.type == bfd_link_hash_warning)
9881 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9882 /* Arrange for symbol to be output. */
9884 elf_section_data (osec)->this_hdr.sh_info = -2;
9886 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9888 /* We'll use the output section target_index. */
9889 asection *sec = flinfo->sections[symndx]->output_section;
9890 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9894 if (flinfo->indices[symndx] == -1)
9896 /* Otherwise output the local symbol now. */
9897 Elf_Internal_Sym sym = isymbuf[symndx];
9898 asection *sec = flinfo->sections[symndx]->output_section;
9903 name = bfd_elf_string_from_elf_section (input_bfd,
9904 symtab_hdr->sh_link,
9909 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9911 if (sym.st_shndx == SHN_BAD)
9914 sym.st_value += o->output_offset;
9916 indx = bfd_get_symcount (output_bfd);
9917 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9922 flinfo->indices[symndx] = indx;
9926 elf_section_data (osec)->this_hdr.sh_info
9927 = flinfo->indices[symndx];
9931 if ((o->flags & SEC_HAS_CONTENTS) == 0
9932 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9935 if ((o->flags & SEC_LINKER_CREATED) != 0)
9937 /* Section was created by _bfd_elf_link_create_dynamic_sections
9942 /* Get the contents of the section. They have been cached by a
9943 relaxation routine. Note that o is a section in an input
9944 file, so the contents field will not have been set by any of
9945 the routines which work on output files. */
9946 if (elf_section_data (o)->this_hdr.contents != NULL)
9948 contents = elf_section_data (o)->this_hdr.contents;
9949 if (bed->caches_rawsize
9951 && o->rawsize < o->size)
9953 memcpy (flinfo->contents, contents, o->rawsize);
9954 contents = flinfo->contents;
9959 contents = flinfo->contents;
9960 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9964 if ((o->flags & SEC_RELOC) != 0)
9966 Elf_Internal_Rela *internal_relocs;
9967 Elf_Internal_Rela *rel, *relend;
9968 int action_discarded;
9971 /* Get the swapped relocs. */
9973 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9974 flinfo->internal_relocs, FALSE);
9975 if (internal_relocs == NULL
9976 && o->reloc_count > 0)
9979 /* We need to reverse-copy input .ctors/.dtors sections if
9980 they are placed in .init_array/.finit_array for output. */
9981 if (o->size > address_size
9982 && ((strncmp (o->name, ".ctors", 6) == 0
9983 && strcmp (o->output_section->name,
9984 ".init_array") == 0)
9985 || (strncmp (o->name, ".dtors", 6) == 0
9986 && strcmp (o->output_section->name,
9987 ".fini_array") == 0))
9988 && (o->name[6] == 0 || o->name[6] == '.'))
9990 if (o->size != o->reloc_count * address_size)
9992 (*_bfd_error_handler)
9993 (_("error: %B: size of section %A is not "
9994 "multiple of address size"),
9996 bfd_set_error (bfd_error_on_input);
9999 o->flags |= SEC_ELF_REVERSE_COPY;
10002 action_discarded = -1;
10003 if (!elf_section_ignore_discarded_relocs (o))
10004 action_discarded = (*bed->action_discarded) (o);
10006 /* Run through the relocs evaluating complex reloc symbols and
10007 looking for relocs against symbols from discarded sections
10008 or section symbols from removed link-once sections.
10009 Complain about relocs against discarded sections. Zero
10010 relocs against removed link-once sections. */
10012 rel = internal_relocs;
10013 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
10014 for ( ; rel < relend; rel++)
10016 unsigned long r_symndx = rel->r_info >> r_sym_shift;
10017 unsigned int s_type;
10018 asection **ps, *sec;
10019 struct elf_link_hash_entry *h = NULL;
10020 const char *sym_name;
10022 if (r_symndx == STN_UNDEF)
10025 if (r_symndx >= locsymcount
10026 || (elf_bad_symtab (input_bfd)
10027 && flinfo->sections[r_symndx] == NULL))
10029 h = sym_hashes[r_symndx - extsymoff];
10031 /* Badly formatted input files can contain relocs that
10032 reference non-existant symbols. Check here so that
10033 we do not seg fault. */
10038 sprintf_vma (buffer, rel->r_info);
10039 (*_bfd_error_handler)
10040 (_("error: %B contains a reloc (0x%s) for section %A "
10041 "that references a non-existent global symbol"),
10042 input_bfd, o, buffer);
10043 bfd_set_error (bfd_error_bad_value);
10047 while (h->root.type == bfd_link_hash_indirect
10048 || h->root.type == bfd_link_hash_warning)
10049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10053 /* If a plugin symbol is referenced from a non-IR file,
10054 mark the symbol as undefined. Note that the
10055 linker may attach linker created dynamic sections
10056 to the plugin bfd. Symbols defined in linker
10057 created sections are not plugin symbols. */
10058 if (h->root.non_ir_ref
10059 && (h->root.type == bfd_link_hash_defined
10060 || h->root.type == bfd_link_hash_defweak)
10061 && (h->root.u.def.section->flags
10062 & SEC_LINKER_CREATED) == 0
10063 && h->root.u.def.section->owner != NULL
10064 && (h->root.u.def.section->owner->flags
10065 & BFD_PLUGIN) != 0)
10067 h->root.type = bfd_link_hash_undefined;
10068 h->root.u.undef.abfd = h->root.u.def.section->owner;
10072 if (h->root.type == bfd_link_hash_defined
10073 || h->root.type == bfd_link_hash_defweak)
10074 ps = &h->root.u.def.section;
10076 sym_name = h->root.root.string;
10080 Elf_Internal_Sym *sym = isymbuf + r_symndx;
10082 s_type = ELF_ST_TYPE (sym->st_info);
10083 ps = &flinfo->sections[r_symndx];
10084 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
10088 if ((s_type == STT_RELC || s_type == STT_SRELC)
10089 && !bfd_link_relocatable (flinfo->info))
10092 bfd_vma dot = (rel->r_offset
10093 + o->output_offset + o->output_section->vma);
10095 printf ("Encountered a complex symbol!");
10096 printf (" (input_bfd %s, section %s, reloc %ld\n",
10097 input_bfd->filename, o->name,
10098 (long) (rel - internal_relocs));
10099 printf (" symbol: idx %8.8lx, name %s\n",
10100 r_symndx, sym_name);
10101 printf (" reloc : info %8.8lx, addr %8.8lx\n",
10102 (unsigned long) rel->r_info,
10103 (unsigned long) rel->r_offset);
10105 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
10106 isymbuf, locsymcount, s_type == STT_SRELC))
10109 /* Symbol evaluated OK. Update to absolute value. */
10110 set_symbol_value (input_bfd, isymbuf, locsymcount,
10115 if (action_discarded != -1 && ps != NULL)
10117 /* Complain if the definition comes from a
10118 discarded section. */
10119 if ((sec = *ps) != NULL && discarded_section (sec))
10121 BFD_ASSERT (r_symndx != STN_UNDEF);
10122 if (action_discarded & COMPLAIN)
10123 (*flinfo->info->callbacks->einfo)
10124 (_("%X`%s' referenced in section `%A' of %B: "
10125 "defined in discarded section `%A' of %B\n"),
10126 sym_name, o, input_bfd, sec, sec->owner);
10128 /* Try to do the best we can to support buggy old
10129 versions of gcc. Pretend that the symbol is
10130 really defined in the kept linkonce section.
10131 FIXME: This is quite broken. Modifying the
10132 symbol here means we will be changing all later
10133 uses of the symbol, not just in this section. */
10134 if (action_discarded & PRETEND)
10138 kept = _bfd_elf_check_kept_section (sec,
10150 /* Relocate the section by invoking a back end routine.
10152 The back end routine is responsible for adjusting the
10153 section contents as necessary, and (if using Rela relocs
10154 and generating a relocatable output file) adjusting the
10155 reloc addend as necessary.
10157 The back end routine does not have to worry about setting
10158 the reloc address or the reloc symbol index.
10160 The back end routine is given a pointer to the swapped in
10161 internal symbols, and can access the hash table entries
10162 for the external symbols via elf_sym_hashes (input_bfd).
10164 When generating relocatable output, the back end routine
10165 must handle STB_LOCAL/STT_SECTION symbols specially. The
10166 output symbol is going to be a section symbol
10167 corresponding to the output section, which will require
10168 the addend to be adjusted. */
10170 ret = (*relocate_section) (output_bfd, flinfo->info,
10171 input_bfd, o, contents,
10179 || bfd_link_relocatable (flinfo->info)
10180 || flinfo->info->emitrelocations)
10182 Elf_Internal_Rela *irela;
10183 Elf_Internal_Rela *irelaend, *irelamid;
10184 bfd_vma last_offset;
10185 struct elf_link_hash_entry **rel_hash;
10186 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10187 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10188 unsigned int next_erel;
10189 bfd_boolean rela_normal;
10190 struct bfd_elf_section_data *esdi, *esdo;
10192 esdi = elf_section_data (o);
10193 esdo = elf_section_data (o->output_section);
10194 rela_normal = FALSE;
10196 /* Adjust the reloc addresses and symbol indices. */
10198 irela = internal_relocs;
10199 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10200 rel_hash = esdo->rel.hashes + esdo->rel.count;
10201 /* We start processing the REL relocs, if any. When we reach
10202 IRELAMID in the loop, we switch to the RELA relocs. */
10204 if (esdi->rel.hdr != NULL)
10205 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10206 * bed->s->int_rels_per_ext_rel);
10207 rel_hash_list = rel_hash;
10208 rela_hash_list = NULL;
10209 last_offset = o->output_offset;
10210 if (!bfd_link_relocatable (flinfo->info))
10211 last_offset += o->output_section->vma;
10212 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10214 unsigned long r_symndx;
10216 Elf_Internal_Sym sym;
10218 if (next_erel == bed->s->int_rels_per_ext_rel)
10224 if (irela == irelamid)
10226 rel_hash = esdo->rela.hashes + esdo->rela.count;
10227 rela_hash_list = rel_hash;
10228 rela_normal = bed->rela_normal;
10231 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10234 if (irela->r_offset >= (bfd_vma) -2)
10236 /* This is a reloc for a deleted entry or somesuch.
10237 Turn it into an R_*_NONE reloc, at the same
10238 offset as the last reloc. elf_eh_frame.c and
10239 bfd_elf_discard_info rely on reloc offsets
10241 irela->r_offset = last_offset;
10243 irela->r_addend = 0;
10247 irela->r_offset += o->output_offset;
10249 /* Relocs in an executable have to be virtual addresses. */
10250 if (!bfd_link_relocatable (flinfo->info))
10251 irela->r_offset += o->output_section->vma;
10253 last_offset = irela->r_offset;
10255 r_symndx = irela->r_info >> r_sym_shift;
10256 if (r_symndx == STN_UNDEF)
10259 if (r_symndx >= locsymcount
10260 || (elf_bad_symtab (input_bfd)
10261 && flinfo->sections[r_symndx] == NULL))
10263 struct elf_link_hash_entry *rh;
10264 unsigned long indx;
10266 /* This is a reloc against a global symbol. We
10267 have not yet output all the local symbols, so
10268 we do not know the symbol index of any global
10269 symbol. We set the rel_hash entry for this
10270 reloc to point to the global hash table entry
10271 for this symbol. The symbol index is then
10272 set at the end of bfd_elf_final_link. */
10273 indx = r_symndx - extsymoff;
10274 rh = elf_sym_hashes (input_bfd)[indx];
10275 while (rh->root.type == bfd_link_hash_indirect
10276 || rh->root.type == bfd_link_hash_warning)
10277 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10279 /* Setting the index to -2 tells
10280 elf_link_output_extsym that this symbol is
10281 used by a reloc. */
10282 BFD_ASSERT (rh->indx < 0);
10290 /* This is a reloc against a local symbol. */
10293 sym = isymbuf[r_symndx];
10294 sec = flinfo->sections[r_symndx];
10295 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10297 /* I suppose the backend ought to fill in the
10298 section of any STT_SECTION symbol against a
10299 processor specific section. */
10300 r_symndx = STN_UNDEF;
10301 if (bfd_is_abs_section (sec))
10303 else if (sec == NULL || sec->owner == NULL)
10305 bfd_set_error (bfd_error_bad_value);
10310 asection *osec = sec->output_section;
10312 /* If we have discarded a section, the output
10313 section will be the absolute section. In
10314 case of discarded SEC_MERGE sections, use
10315 the kept section. relocate_section should
10316 have already handled discarded linkonce
10318 if (bfd_is_abs_section (osec)
10319 && sec->kept_section != NULL
10320 && sec->kept_section->output_section != NULL)
10322 osec = sec->kept_section->output_section;
10323 irela->r_addend -= osec->vma;
10326 if (!bfd_is_abs_section (osec))
10328 r_symndx = osec->target_index;
10329 if (r_symndx == STN_UNDEF)
10331 irela->r_addend += osec->vma;
10332 osec = _bfd_nearby_section (output_bfd, osec,
10334 irela->r_addend -= osec->vma;
10335 r_symndx = osec->target_index;
10340 /* Adjust the addend according to where the
10341 section winds up in the output section. */
10343 irela->r_addend += sec->output_offset;
10347 if (flinfo->indices[r_symndx] == -1)
10349 unsigned long shlink;
10354 if (flinfo->info->strip == strip_all)
10356 /* You can't do ld -r -s. */
10357 bfd_set_error (bfd_error_invalid_operation);
10361 /* This symbol was skipped earlier, but
10362 since it is needed by a reloc, we
10363 must output it now. */
10364 shlink = symtab_hdr->sh_link;
10365 name = (bfd_elf_string_from_elf_section
10366 (input_bfd, shlink, sym.st_name));
10370 osec = sec->output_section;
10372 _bfd_elf_section_from_bfd_section (output_bfd,
10374 if (sym.st_shndx == SHN_BAD)
10377 sym.st_value += sec->output_offset;
10378 if (!bfd_link_relocatable (flinfo->info))
10380 sym.st_value += osec->vma;
10381 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10383 /* STT_TLS symbols are relative to PT_TLS
10385 BFD_ASSERT (elf_hash_table (flinfo->info)
10386 ->tls_sec != NULL);
10387 sym.st_value -= (elf_hash_table (flinfo->info)
10392 indx = bfd_get_symcount (output_bfd);
10393 ret = elf_link_output_symstrtab (flinfo, name,
10399 flinfo->indices[r_symndx] = indx;
10404 r_symndx = flinfo->indices[r_symndx];
10407 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10408 | (irela->r_info & r_type_mask));
10411 /* Swap out the relocs. */
10412 input_rel_hdr = esdi->rel.hdr;
10413 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10415 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10420 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10421 * bed->s->int_rels_per_ext_rel);
10422 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10425 input_rela_hdr = esdi->rela.hdr;
10426 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10428 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10437 /* Write out the modified section contents. */
10438 if (bed->elf_backend_write_section
10439 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10442 /* Section written out. */
10444 else switch (o->sec_info_type)
10446 case SEC_INFO_TYPE_STABS:
10447 if (! (_bfd_write_section_stabs
10449 &elf_hash_table (flinfo->info)->stab_info,
10450 o, &elf_section_data (o)->sec_info, contents)))
10453 case SEC_INFO_TYPE_MERGE:
10454 if (! _bfd_write_merged_section (output_bfd, o,
10455 elf_section_data (o)->sec_info))
10458 case SEC_INFO_TYPE_EH_FRAME:
10460 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10465 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10467 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10475 /* FIXME: octets_per_byte. */
10476 if (! (o->flags & SEC_EXCLUDE))
10478 file_ptr offset = (file_ptr) o->output_offset;
10479 bfd_size_type todo = o->size;
10480 if ((o->flags & SEC_ELF_REVERSE_COPY))
10482 /* Reverse-copy input section to output. */
10485 todo -= address_size;
10486 if (! bfd_set_section_contents (output_bfd,
10494 offset += address_size;
10498 else if (! bfd_set_section_contents (output_bfd,
10512 /* Generate a reloc when linking an ELF file. This is a reloc
10513 requested by the linker, and does not come from any input file. This
10514 is used to build constructor and destructor tables when linking
10518 elf_reloc_link_order (bfd *output_bfd,
10519 struct bfd_link_info *info,
10520 asection *output_section,
10521 struct bfd_link_order *link_order)
10523 reloc_howto_type *howto;
10527 struct bfd_elf_section_reloc_data *reldata;
10528 struct elf_link_hash_entry **rel_hash_ptr;
10529 Elf_Internal_Shdr *rel_hdr;
10530 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10531 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10534 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10536 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10539 bfd_set_error (bfd_error_bad_value);
10543 addend = link_order->u.reloc.p->addend;
10546 reldata = &esdo->rel;
10547 else if (esdo->rela.hdr)
10548 reldata = &esdo->rela;
10555 /* Figure out the symbol index. */
10556 rel_hash_ptr = reldata->hashes + reldata->count;
10557 if (link_order->type == bfd_section_reloc_link_order)
10559 indx = link_order->u.reloc.p->u.section->target_index;
10560 BFD_ASSERT (indx != 0);
10561 *rel_hash_ptr = NULL;
10565 struct elf_link_hash_entry *h;
10567 /* Treat a reloc against a defined symbol as though it were
10568 actually against the section. */
10569 h = ((struct elf_link_hash_entry *)
10570 bfd_wrapped_link_hash_lookup (output_bfd, info,
10571 link_order->u.reloc.p->u.name,
10572 FALSE, FALSE, TRUE));
10574 && (h->root.type == bfd_link_hash_defined
10575 || h->root.type == bfd_link_hash_defweak))
10579 section = h->root.u.def.section;
10580 indx = section->output_section->target_index;
10581 *rel_hash_ptr = NULL;
10582 /* It seems that we ought to add the symbol value to the
10583 addend here, but in practice it has already been added
10584 because it was passed to constructor_callback. */
10585 addend += section->output_section->vma + section->output_offset;
10587 else if (h != NULL)
10589 /* Setting the index to -2 tells elf_link_output_extsym that
10590 this symbol is used by a reloc. */
10597 if (! ((*info->callbacks->unattached_reloc)
10598 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10604 /* If this is an inplace reloc, we must write the addend into the
10606 if (howto->partial_inplace && addend != 0)
10608 bfd_size_type size;
10609 bfd_reloc_status_type rstat;
10612 const char *sym_name;
10614 size = (bfd_size_type) bfd_get_reloc_size (howto);
10615 buf = (bfd_byte *) bfd_zmalloc (size);
10616 if (buf == NULL && size != 0)
10618 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10625 case bfd_reloc_outofrange:
10628 case bfd_reloc_overflow:
10629 if (link_order->type == bfd_section_reloc_link_order)
10630 sym_name = bfd_section_name (output_bfd,
10631 link_order->u.reloc.p->u.section);
10633 sym_name = link_order->u.reloc.p->u.name;
10634 if (! ((*info->callbacks->reloc_overflow)
10635 (info, NULL, sym_name, howto->name, addend, NULL,
10636 NULL, (bfd_vma) 0)))
10643 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10644 link_order->offset, size);
10650 /* The address of a reloc is relative to the section in a
10651 relocatable file, and is a virtual address in an executable
10653 offset = link_order->offset;
10654 if (! bfd_link_relocatable (info))
10655 offset += output_section->vma;
10657 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10659 irel[i].r_offset = offset;
10660 irel[i].r_info = 0;
10661 irel[i].r_addend = 0;
10663 if (bed->s->arch_size == 32)
10664 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10666 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10668 rel_hdr = reldata->hdr;
10669 erel = rel_hdr->contents;
10670 if (rel_hdr->sh_type == SHT_REL)
10672 erel += reldata->count * bed->s->sizeof_rel;
10673 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10677 irel[0].r_addend = addend;
10678 erel += reldata->count * bed->s->sizeof_rela;
10679 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10688 /* Get the output vma of the section pointed to by the sh_link field. */
10691 elf_get_linked_section_vma (struct bfd_link_order *p)
10693 Elf_Internal_Shdr **elf_shdrp;
10697 s = p->u.indirect.section;
10698 elf_shdrp = elf_elfsections (s->owner);
10699 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10700 elfsec = elf_shdrp[elfsec]->sh_link;
10702 The Intel C compiler generates SHT_IA_64_UNWIND with
10703 SHF_LINK_ORDER. But it doesn't set the sh_link or
10704 sh_info fields. Hence we could get the situation
10705 where elfsec is 0. */
10708 const struct elf_backend_data *bed
10709 = get_elf_backend_data (s->owner);
10710 if (bed->link_order_error_handler)
10711 bed->link_order_error_handler
10712 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10717 s = elf_shdrp[elfsec]->bfd_section;
10718 return s->output_section->vma + s->output_offset;
10723 /* Compare two sections based on the locations of the sections they are
10724 linked to. Used by elf_fixup_link_order. */
10727 compare_link_order (const void * a, const void * b)
10732 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10733 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10736 return apos > bpos;
10740 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10741 order as their linked sections. Returns false if this could not be done
10742 because an output section includes both ordered and unordered
10743 sections. Ideally we'd do this in the linker proper. */
10746 elf_fixup_link_order (bfd *abfd, asection *o)
10748 int seen_linkorder;
10751 struct bfd_link_order *p;
10753 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10755 struct bfd_link_order **sections;
10756 asection *s, *other_sec, *linkorder_sec;
10760 linkorder_sec = NULL;
10762 seen_linkorder = 0;
10763 for (p = o->map_head.link_order; p != NULL; p = p->next)
10765 if (p->type == bfd_indirect_link_order)
10767 s = p->u.indirect.section;
10769 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10770 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10771 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10772 && elfsec < elf_numsections (sub)
10773 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10774 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10788 if (seen_other && seen_linkorder)
10790 if (other_sec && linkorder_sec)
10791 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10793 linkorder_sec->owner, other_sec,
10796 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10798 bfd_set_error (bfd_error_bad_value);
10803 if (!seen_linkorder)
10806 sections = (struct bfd_link_order **)
10807 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10808 if (sections == NULL)
10810 seen_linkorder = 0;
10812 for (p = o->map_head.link_order; p != NULL; p = p->next)
10814 sections[seen_linkorder++] = p;
10816 /* Sort the input sections in the order of their linked section. */
10817 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10818 compare_link_order);
10820 /* Change the offsets of the sections. */
10822 for (n = 0; n < seen_linkorder; n++)
10824 s = sections[n]->u.indirect.section;
10825 offset &= ~(bfd_vma) 0 << s->alignment_power;
10826 s->output_offset = offset;
10827 sections[n]->offset = offset;
10828 /* FIXME: octets_per_byte. */
10829 offset += sections[n]->size;
10837 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10841 if (flinfo->symstrtab != NULL)
10842 _bfd_elf_strtab_free (flinfo->symstrtab);
10843 if (flinfo->contents != NULL)
10844 free (flinfo->contents);
10845 if (flinfo->external_relocs != NULL)
10846 free (flinfo->external_relocs);
10847 if (flinfo->internal_relocs != NULL)
10848 free (flinfo->internal_relocs);
10849 if (flinfo->external_syms != NULL)
10850 free (flinfo->external_syms);
10851 if (flinfo->locsym_shndx != NULL)
10852 free (flinfo->locsym_shndx);
10853 if (flinfo->internal_syms != NULL)
10854 free (flinfo->internal_syms);
10855 if (flinfo->indices != NULL)
10856 free (flinfo->indices);
10857 if (flinfo->sections != NULL)
10858 free (flinfo->sections);
10859 if (flinfo->symshndxbuf != NULL)
10860 free (flinfo->symshndxbuf);
10861 for (o = obfd->sections; o != NULL; o = o->next)
10863 struct bfd_elf_section_data *esdo = elf_section_data (o);
10864 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10865 free (esdo->rel.hashes);
10866 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10867 free (esdo->rela.hashes);
10871 /* Do the final step of an ELF link. */
10874 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10876 bfd_boolean dynamic;
10877 bfd_boolean emit_relocs;
10879 struct elf_final_link_info flinfo;
10881 struct bfd_link_order *p;
10883 bfd_size_type max_contents_size;
10884 bfd_size_type max_external_reloc_size;
10885 bfd_size_type max_internal_reloc_count;
10886 bfd_size_type max_sym_count;
10887 bfd_size_type max_sym_shndx_count;
10888 Elf_Internal_Sym elfsym;
10890 Elf_Internal_Shdr *symtab_hdr;
10891 Elf_Internal_Shdr *symtab_shndx_hdr;
10892 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10893 struct elf_outext_info eoinfo;
10894 bfd_boolean merged;
10895 size_t relativecount = 0;
10896 asection *reldyn = 0;
10898 asection *attr_section = NULL;
10899 bfd_vma attr_size = 0;
10900 const char *std_attrs_section;
10902 if (! is_elf_hash_table (info->hash))
10905 if (bfd_link_pic (info))
10906 abfd->flags |= DYNAMIC;
10908 dynamic = elf_hash_table (info)->dynamic_sections_created;
10909 dynobj = elf_hash_table (info)->dynobj;
10911 emit_relocs = (bfd_link_relocatable (info)
10912 || info->emitrelocations);
10914 flinfo.info = info;
10915 flinfo.output_bfd = abfd;
10916 flinfo.symstrtab = _bfd_elf_strtab_init ();
10917 if (flinfo.symstrtab == NULL)
10922 flinfo.hash_sec = NULL;
10923 flinfo.symver_sec = NULL;
10927 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10928 /* Note that dynsym_sec can be NULL (on VMS). */
10929 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10930 /* Note that it is OK if symver_sec is NULL. */
10933 flinfo.contents = NULL;
10934 flinfo.external_relocs = NULL;
10935 flinfo.internal_relocs = NULL;
10936 flinfo.external_syms = NULL;
10937 flinfo.locsym_shndx = NULL;
10938 flinfo.internal_syms = NULL;
10939 flinfo.indices = NULL;
10940 flinfo.sections = NULL;
10941 flinfo.symshndxbuf = NULL;
10942 flinfo.filesym_count = 0;
10944 /* The object attributes have been merged. Remove the input
10945 sections from the link, and set the contents of the output
10947 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10948 for (o = abfd->sections; o != NULL; o = o->next)
10950 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10951 || strcmp (o->name, ".gnu.attributes") == 0)
10953 for (p = o->map_head.link_order; p != NULL; p = p->next)
10955 asection *input_section;
10957 if (p->type != bfd_indirect_link_order)
10959 input_section = p->u.indirect.section;
10960 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10961 elf_link_input_bfd ignores this section. */
10962 input_section->flags &= ~SEC_HAS_CONTENTS;
10965 attr_size = bfd_elf_obj_attr_size (abfd);
10968 bfd_set_section_size (abfd, o, attr_size);
10970 /* Skip this section later on. */
10971 o->map_head.link_order = NULL;
10974 o->flags |= SEC_EXCLUDE;
10978 /* Count up the number of relocations we will output for each output
10979 section, so that we know the sizes of the reloc sections. We
10980 also figure out some maximum sizes. */
10981 max_contents_size = 0;
10982 max_external_reloc_size = 0;
10983 max_internal_reloc_count = 0;
10985 max_sym_shndx_count = 0;
10987 for (o = abfd->sections; o != NULL; o = o->next)
10989 struct bfd_elf_section_data *esdo = elf_section_data (o);
10990 o->reloc_count = 0;
10992 for (p = o->map_head.link_order; p != NULL; p = p->next)
10994 unsigned int reloc_count = 0;
10995 unsigned int additional_reloc_count = 0;
10996 struct bfd_elf_section_data *esdi = NULL;
10998 if (p->type == bfd_section_reloc_link_order
10999 || p->type == bfd_symbol_reloc_link_order)
11001 else if (p->type == bfd_indirect_link_order)
11005 sec = p->u.indirect.section;
11006 esdi = elf_section_data (sec);
11008 /* Mark all sections which are to be included in the
11009 link. This will normally be every section. We need
11010 to do this so that we can identify any sections which
11011 the linker has decided to not include. */
11012 sec->linker_mark = TRUE;
11014 if (sec->flags & SEC_MERGE)
11017 if (esdo->this_hdr.sh_type == SHT_REL
11018 || esdo->this_hdr.sh_type == SHT_RELA)
11019 /* Some backends use reloc_count in relocation sections
11020 to count particular types of relocs. Of course,
11021 reloc sections themselves can't have relocations. */
11023 else if (emit_relocs)
11025 reloc_count = sec->reloc_count;
11026 if (bed->elf_backend_count_additional_relocs)
11029 c = (*bed->elf_backend_count_additional_relocs) (sec);
11030 additional_reloc_count += c;
11033 else if (bed->elf_backend_count_relocs)
11034 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
11036 if (sec->rawsize > max_contents_size)
11037 max_contents_size = sec->rawsize;
11038 if (sec->size > max_contents_size)
11039 max_contents_size = sec->size;
11041 /* We are interested in just local symbols, not all
11043 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
11044 && (sec->owner->flags & DYNAMIC) == 0)
11048 if (elf_bad_symtab (sec->owner))
11049 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
11050 / bed->s->sizeof_sym);
11052 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
11054 if (sym_count > max_sym_count)
11055 max_sym_count = sym_count;
11057 if (sym_count > max_sym_shndx_count
11058 && elf_symtab_shndx_list (sec->owner) != NULL)
11059 max_sym_shndx_count = sym_count;
11061 if ((sec->flags & SEC_RELOC) != 0)
11063 size_t ext_size = 0;
11065 if (esdi->rel.hdr != NULL)
11066 ext_size = esdi->rel.hdr->sh_size;
11067 if (esdi->rela.hdr != NULL)
11068 ext_size += esdi->rela.hdr->sh_size;
11070 if (ext_size > max_external_reloc_size)
11071 max_external_reloc_size = ext_size;
11072 if (sec->reloc_count > max_internal_reloc_count)
11073 max_internal_reloc_count = sec->reloc_count;
11078 if (reloc_count == 0)
11081 reloc_count += additional_reloc_count;
11082 o->reloc_count += reloc_count;
11084 if (p->type == bfd_indirect_link_order && emit_relocs)
11088 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
11089 esdo->rel.count += additional_reloc_count;
11091 if (esdi->rela.hdr)
11093 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
11094 esdo->rela.count += additional_reloc_count;
11100 esdo->rela.count += reloc_count;
11102 esdo->rel.count += reloc_count;
11106 if (o->reloc_count > 0)
11107 o->flags |= SEC_RELOC;
11110 /* Explicitly clear the SEC_RELOC flag. The linker tends to
11111 set it (this is probably a bug) and if it is set
11112 assign_section_numbers will create a reloc section. */
11113 o->flags &=~ SEC_RELOC;
11116 /* If the SEC_ALLOC flag is not set, force the section VMA to
11117 zero. This is done in elf_fake_sections as well, but forcing
11118 the VMA to 0 here will ensure that relocs against these
11119 sections are handled correctly. */
11120 if ((o->flags & SEC_ALLOC) == 0
11121 && ! o->user_set_vma)
11125 if (! bfd_link_relocatable (info) && merged)
11126 elf_link_hash_traverse (elf_hash_table (info),
11127 _bfd_elf_link_sec_merge_syms, abfd);
11129 /* Figure out the file positions for everything but the symbol table
11130 and the relocs. We set symcount to force assign_section_numbers
11131 to create a symbol table. */
11132 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
11133 BFD_ASSERT (! abfd->output_has_begun);
11134 if (! _bfd_elf_compute_section_file_positions (abfd, info))
11137 /* Set sizes, and assign file positions for reloc sections. */
11138 for (o = abfd->sections; o != NULL; o = o->next)
11140 struct bfd_elf_section_data *esdo = elf_section_data (o);
11141 if ((o->flags & SEC_RELOC) != 0)
11144 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
11148 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
11152 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
11153 to count upwards while actually outputting the relocations. */
11154 esdo->rel.count = 0;
11155 esdo->rela.count = 0;
11157 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
11159 /* Cache the section contents so that they can be compressed
11160 later. Use bfd_malloc since it will be freed by
11161 bfd_compress_section_contents. */
11162 unsigned char *contents = esdo->this_hdr.contents;
11163 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
11166 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
11167 if (contents == NULL)
11169 esdo->this_hdr.contents = contents;
11173 /* We have now assigned file positions for all the sections except
11174 .symtab, .strtab, and non-loaded reloc sections. We start the
11175 .symtab section at the current file position, and write directly
11176 to it. We build the .strtab section in memory. */
11177 bfd_get_symcount (abfd) = 0;
11178 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11179 /* sh_name is set in prep_headers. */
11180 symtab_hdr->sh_type = SHT_SYMTAB;
11181 /* sh_flags, sh_addr and sh_size all start off zero. */
11182 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11183 /* sh_link is set in assign_section_numbers. */
11184 /* sh_info is set below. */
11185 /* sh_offset is set just below. */
11186 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11188 if (max_sym_count < 20)
11189 max_sym_count = 20;
11190 elf_hash_table (info)->strtabsize = max_sym_count;
11191 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11192 elf_hash_table (info)->strtab
11193 = (struct elf_sym_strtab *) bfd_malloc (amt);
11194 if (elf_hash_table (info)->strtab == NULL)
11196 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11198 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11199 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11201 if (info->strip != strip_all || emit_relocs)
11203 file_ptr off = elf_next_file_pos (abfd);
11205 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11207 /* Note that at this point elf_next_file_pos (abfd) is
11208 incorrect. We do not yet know the size of the .symtab section.
11209 We correct next_file_pos below, after we do know the size. */
11211 /* Start writing out the symbol table. The first symbol is always a
11213 elfsym.st_value = 0;
11214 elfsym.st_size = 0;
11215 elfsym.st_info = 0;
11216 elfsym.st_other = 0;
11217 elfsym.st_shndx = SHN_UNDEF;
11218 elfsym.st_target_internal = 0;
11219 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11220 bfd_und_section_ptr, NULL) != 1)
11223 /* Output a symbol for each section. We output these even if we are
11224 discarding local symbols, since they are used for relocs. These
11225 symbols have no names. We store the index of each one in the
11226 index field of the section, so that we can find it again when
11227 outputting relocs. */
11229 elfsym.st_size = 0;
11230 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11231 elfsym.st_other = 0;
11232 elfsym.st_value = 0;
11233 elfsym.st_target_internal = 0;
11234 for (i = 1; i < elf_numsections (abfd); i++)
11236 o = bfd_section_from_elf_index (abfd, i);
11239 o->target_index = bfd_get_symcount (abfd);
11240 elfsym.st_shndx = i;
11241 if (!bfd_link_relocatable (info))
11242 elfsym.st_value = o->vma;
11243 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11250 /* Allocate some memory to hold information read in from the input
11252 if (max_contents_size != 0)
11254 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11255 if (flinfo.contents == NULL)
11259 if (max_external_reloc_size != 0)
11261 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11262 if (flinfo.external_relocs == NULL)
11266 if (max_internal_reloc_count != 0)
11268 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11269 amt *= sizeof (Elf_Internal_Rela);
11270 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11271 if (flinfo.internal_relocs == NULL)
11275 if (max_sym_count != 0)
11277 amt = max_sym_count * bed->s->sizeof_sym;
11278 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11279 if (flinfo.external_syms == NULL)
11282 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11283 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11284 if (flinfo.internal_syms == NULL)
11287 amt = max_sym_count * sizeof (long);
11288 flinfo.indices = (long int *) bfd_malloc (amt);
11289 if (flinfo.indices == NULL)
11292 amt = max_sym_count * sizeof (asection *);
11293 flinfo.sections = (asection **) bfd_malloc (amt);
11294 if (flinfo.sections == NULL)
11298 if (max_sym_shndx_count != 0)
11300 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11301 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11302 if (flinfo.locsym_shndx == NULL)
11306 if (elf_hash_table (info)->tls_sec)
11308 bfd_vma base, end = 0;
11311 for (sec = elf_hash_table (info)->tls_sec;
11312 sec && (sec->flags & SEC_THREAD_LOCAL);
11315 bfd_size_type size = sec->size;
11318 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11320 struct bfd_link_order *ord = sec->map_tail.link_order;
11323 size = ord->offset + ord->size;
11325 end = sec->vma + size;
11327 base = elf_hash_table (info)->tls_sec->vma;
11328 /* Only align end of TLS section if static TLS doesn't have special
11329 alignment requirements. */
11330 if (bed->static_tls_alignment == 1)
11331 end = align_power (end,
11332 elf_hash_table (info)->tls_sec->alignment_power);
11333 elf_hash_table (info)->tls_size = end - base;
11336 /* Reorder SHF_LINK_ORDER sections. */
11337 for (o = abfd->sections; o != NULL; o = o->next)
11339 if (!elf_fixup_link_order (abfd, o))
11343 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11346 /* Since ELF permits relocations to be against local symbols, we
11347 must have the local symbols available when we do the relocations.
11348 Since we would rather only read the local symbols once, and we
11349 would rather not keep them in memory, we handle all the
11350 relocations for a single input file at the same time.
11352 Unfortunately, there is no way to know the total number of local
11353 symbols until we have seen all of them, and the local symbol
11354 indices precede the global symbol indices. This means that when
11355 we are generating relocatable output, and we see a reloc against
11356 a global symbol, we can not know the symbol index until we have
11357 finished examining all the local symbols to see which ones we are
11358 going to output. To deal with this, we keep the relocations in
11359 memory, and don't output them until the end of the link. This is
11360 an unfortunate waste of memory, but I don't see a good way around
11361 it. Fortunately, it only happens when performing a relocatable
11362 link, which is not the common case. FIXME: If keep_memory is set
11363 we could write the relocs out and then read them again; I don't
11364 know how bad the memory loss will be. */
11366 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11367 sub->output_has_begun = FALSE;
11368 for (o = abfd->sections; o != NULL; o = o->next)
11370 for (p = o->map_head.link_order; p != NULL; p = p->next)
11372 if (p->type == bfd_indirect_link_order
11373 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11374 == bfd_target_elf_flavour)
11375 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11377 if (! sub->output_has_begun)
11379 if (! elf_link_input_bfd (&flinfo, sub))
11381 sub->output_has_begun = TRUE;
11384 else if (p->type == bfd_section_reloc_link_order
11385 || p->type == bfd_symbol_reloc_link_order)
11387 if (! elf_reloc_link_order (abfd, info, o, p))
11392 if (! _bfd_default_link_order (abfd, info, o, p))
11394 if (p->type == bfd_indirect_link_order
11395 && (bfd_get_flavour (sub)
11396 == bfd_target_elf_flavour)
11397 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11398 != bed->s->elfclass))
11400 const char *iclass, *oclass;
11402 switch (bed->s->elfclass)
11404 case ELFCLASS64: oclass = "ELFCLASS64"; break;
11405 case ELFCLASS32: oclass = "ELFCLASS32"; break;
11406 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
11410 switch (elf_elfheader (sub)->e_ident[EI_CLASS])
11412 case ELFCLASS64: iclass = "ELFCLASS64"; break;
11413 case ELFCLASS32: iclass = "ELFCLASS32"; break;
11414 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
11418 bfd_set_error (bfd_error_wrong_format);
11419 (*_bfd_error_handler)
11420 (_("%B: file class %s incompatible with %s"),
11421 sub, iclass, oclass);
11430 /* Free symbol buffer if needed. */
11431 if (!info->reduce_memory_overheads)
11433 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11434 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11435 && elf_tdata (sub)->symbuf)
11437 free (elf_tdata (sub)->symbuf);
11438 elf_tdata (sub)->symbuf = NULL;
11442 /* Output any global symbols that got converted to local in a
11443 version script or due to symbol visibility. We do this in a
11444 separate step since ELF requires all local symbols to appear
11445 prior to any global symbols. FIXME: We should only do this if
11446 some global symbols were, in fact, converted to become local.
11447 FIXME: Will this work correctly with the Irix 5 linker? */
11448 eoinfo.failed = FALSE;
11449 eoinfo.flinfo = &flinfo;
11450 eoinfo.localsyms = TRUE;
11451 eoinfo.file_sym_done = FALSE;
11452 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11456 /* If backend needs to output some local symbols not present in the hash
11457 table, do it now. */
11458 if (bed->elf_backend_output_arch_local_syms
11459 && (info->strip != strip_all || emit_relocs))
11461 typedef int (*out_sym_func)
11462 (void *, const char *, Elf_Internal_Sym *, asection *,
11463 struct elf_link_hash_entry *);
11465 if (! ((*bed->elf_backend_output_arch_local_syms)
11466 (abfd, info, &flinfo,
11467 (out_sym_func) elf_link_output_symstrtab)))
11471 /* That wrote out all the local symbols. Finish up the symbol table
11472 with the global symbols. Even if we want to strip everything we
11473 can, we still need to deal with those global symbols that got
11474 converted to local in a version script. */
11476 /* The sh_info field records the index of the first non local symbol. */
11477 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11480 && elf_hash_table (info)->dynsym != NULL
11481 && (elf_hash_table (info)->dynsym->output_section
11482 != bfd_abs_section_ptr))
11484 Elf_Internal_Sym sym;
11485 bfd_byte *dynsym = elf_hash_table (info)->dynsym->contents;
11486 long last_local = 0;
11488 /* Write out the section symbols for the output sections. */
11489 if (bfd_link_pic (info)
11490 || elf_hash_table (info)->is_relocatable_executable)
11496 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11498 sym.st_target_internal = 0;
11500 for (s = abfd->sections; s != NULL; s = s->next)
11506 dynindx = elf_section_data (s)->dynindx;
11509 indx = elf_section_data (s)->this_idx;
11510 BFD_ASSERT (indx > 0);
11511 sym.st_shndx = indx;
11512 if (! check_dynsym (abfd, &sym))
11514 sym.st_value = s->vma;
11515 dest = dynsym + dynindx * bed->s->sizeof_sym;
11516 if (last_local < dynindx)
11517 last_local = dynindx;
11518 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11522 /* Write out the local dynsyms. */
11523 if (elf_hash_table (info)->dynlocal)
11525 struct elf_link_local_dynamic_entry *e;
11526 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11531 /* Copy the internal symbol and turn off visibility.
11532 Note that we saved a word of storage and overwrote
11533 the original st_name with the dynstr_index. */
11535 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11537 s = bfd_section_from_elf_index (e->input_bfd,
11542 elf_section_data (s->output_section)->this_idx;
11543 if (! check_dynsym (abfd, &sym))
11545 sym.st_value = (s->output_section->vma
11547 + e->isym.st_value);
11550 if (last_local < e->dynindx)
11551 last_local = e->dynindx;
11553 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11554 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11558 elf_section_data (elf_hash_table (info)->dynsym->output_section)->this_hdr.sh_info =
11562 /* We get the global symbols from the hash table. */
11563 eoinfo.failed = FALSE;
11564 eoinfo.localsyms = FALSE;
11565 eoinfo.flinfo = &flinfo;
11566 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11570 /* If backend needs to output some symbols not present in the hash
11571 table, do it now. */
11572 if (bed->elf_backend_output_arch_syms
11573 && (info->strip != strip_all || emit_relocs))
11575 typedef int (*out_sym_func)
11576 (void *, const char *, Elf_Internal_Sym *, asection *,
11577 struct elf_link_hash_entry *);
11579 if (! ((*bed->elf_backend_output_arch_syms)
11580 (abfd, info, &flinfo,
11581 (out_sym_func) elf_link_output_symstrtab)))
11585 /* Finalize the .strtab section. */
11586 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11588 /* Swap out the .strtab section. */
11589 if (!elf_link_swap_symbols_out (&flinfo))
11592 /* Now we know the size of the symtab section. */
11593 if (bfd_get_symcount (abfd) > 0)
11595 /* Finish up and write out the symbol string table (.strtab)
11597 Elf_Internal_Shdr *symstrtab_hdr;
11598 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11600 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
11601 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
11603 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11604 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11605 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11606 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11607 symtab_shndx_hdr->sh_size = amt;
11609 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11612 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11613 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11617 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11618 /* sh_name was set in prep_headers. */
11619 symstrtab_hdr->sh_type = SHT_STRTAB;
11620 symstrtab_hdr->sh_flags = 0;
11621 symstrtab_hdr->sh_addr = 0;
11622 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11623 symstrtab_hdr->sh_entsize = 0;
11624 symstrtab_hdr->sh_link = 0;
11625 symstrtab_hdr->sh_info = 0;
11626 /* sh_offset is set just below. */
11627 symstrtab_hdr->sh_addralign = 1;
11629 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11631 elf_next_file_pos (abfd) = off;
11633 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11634 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11638 /* Adjust the relocs to have the correct symbol indices. */
11639 for (o = abfd->sections; o != NULL; o = o->next)
11641 struct bfd_elf_section_data *esdo = elf_section_data (o);
11643 if ((o->flags & SEC_RELOC) == 0)
11646 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11647 if (esdo->rel.hdr != NULL
11648 && !elf_link_adjust_relocs (abfd, &esdo->rel, sort))
11650 if (esdo->rela.hdr != NULL
11651 && !elf_link_adjust_relocs (abfd, &esdo->rela, sort))
11654 /* Set the reloc_count field to 0 to prevent write_relocs from
11655 trying to swap the relocs out itself. */
11656 o->reloc_count = 0;
11659 if (dynamic && info->combreloc && dynobj != NULL)
11660 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11662 /* If we are linking against a dynamic object, or generating a
11663 shared library, finish up the dynamic linking information. */
11666 bfd_byte *dyncon, *dynconend;
11668 /* Fix up .dynamic entries. */
11669 o = bfd_get_linker_section (dynobj, ".dynamic");
11670 BFD_ASSERT (o != NULL);
11672 dyncon = o->contents;
11673 dynconend = o->contents + o->size;
11674 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11676 Elf_Internal_Dyn dyn;
11680 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11687 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11689 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11691 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11692 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11695 dyn.d_un.d_val = relativecount;
11702 name = info->init_function;
11705 name = info->fini_function;
11708 struct elf_link_hash_entry *h;
11710 h = elf_link_hash_lookup (elf_hash_table (info), name,
11711 FALSE, FALSE, TRUE);
11713 && (h->root.type == bfd_link_hash_defined
11714 || h->root.type == bfd_link_hash_defweak))
11716 dyn.d_un.d_ptr = h->root.u.def.value;
11717 o = h->root.u.def.section;
11718 if (o->output_section != NULL)
11719 dyn.d_un.d_ptr += (o->output_section->vma
11720 + o->output_offset);
11723 /* The symbol is imported from another shared
11724 library and does not apply to this one. */
11725 dyn.d_un.d_ptr = 0;
11732 case DT_PREINIT_ARRAYSZ:
11733 name = ".preinit_array";
11735 case DT_INIT_ARRAYSZ:
11736 name = ".init_array";
11738 case DT_FINI_ARRAYSZ:
11739 name = ".fini_array";
11741 o = bfd_get_section_by_name (abfd, name);
11744 (*_bfd_error_handler)
11745 (_("%B: could not find output section %s"), abfd, name);
11749 (*_bfd_error_handler)
11750 (_("warning: %s section has zero size"), name);
11751 dyn.d_un.d_val = o->size;
11754 case DT_PREINIT_ARRAY:
11755 name = ".preinit_array";
11757 case DT_INIT_ARRAY:
11758 name = ".init_array";
11760 case DT_FINI_ARRAY:
11761 name = ".fini_array";
11768 name = ".gnu.hash";
11777 name = ".gnu.version_d";
11780 name = ".gnu.version_r";
11783 name = ".gnu.version";
11785 o = bfd_get_section_by_name (abfd, name);
11788 (*_bfd_error_handler)
11789 (_("%B: could not find output section %s"), abfd, name);
11792 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11794 (*_bfd_error_handler)
11795 (_("warning: section '%s' is being made into a note"), name);
11796 bfd_set_error (bfd_error_nonrepresentable_section);
11799 dyn.d_un.d_ptr = o->vma;
11806 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11810 dyn.d_un.d_val = 0;
11811 dyn.d_un.d_ptr = 0;
11812 for (i = 1; i < elf_numsections (abfd); i++)
11814 Elf_Internal_Shdr *hdr;
11816 hdr = elf_elfsections (abfd)[i];
11817 if (hdr->sh_type == type
11818 && (hdr->sh_flags & SHF_ALLOC) != 0)
11820 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11821 dyn.d_un.d_val += hdr->sh_size;
11824 if (dyn.d_un.d_ptr == 0
11825 || hdr->sh_addr < dyn.d_un.d_ptr)
11826 dyn.d_un.d_ptr = hdr->sh_addr;
11832 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11836 /* If we have created any dynamic sections, then output them. */
11837 if (dynobj != NULL)
11839 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11842 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11843 if (((info->warn_shared_textrel && bfd_link_pic (info))
11844 || info->error_textrel)
11845 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11847 bfd_byte *dyncon, *dynconend;
11849 dyncon = o->contents;
11850 dynconend = o->contents + o->size;
11851 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11853 Elf_Internal_Dyn dyn;
11855 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11857 if (dyn.d_tag == DT_TEXTREL)
11859 if (info->error_textrel)
11860 info->callbacks->einfo
11861 (_("%P%X: read-only segment has dynamic relocations.\n"));
11863 info->callbacks->einfo
11864 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11870 for (o = dynobj->sections; o != NULL; o = o->next)
11872 if ((o->flags & SEC_HAS_CONTENTS) == 0
11874 || o->output_section == bfd_abs_section_ptr)
11876 if ((o->flags & SEC_LINKER_CREATED) == 0)
11878 /* At this point, we are only interested in sections
11879 created by _bfd_elf_link_create_dynamic_sections. */
11882 if (elf_hash_table (info)->stab_info.stabstr == o)
11884 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11886 if (strcmp (o->name, ".dynstr") != 0)
11888 /* FIXME: octets_per_byte. */
11889 if (! bfd_set_section_contents (abfd, o->output_section,
11891 (file_ptr) o->output_offset,
11897 /* The contents of the .dynstr section are actually in a
11901 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11902 if (bfd_seek (abfd, off, SEEK_SET) != 0
11903 || ! _bfd_elf_strtab_emit (abfd,
11904 elf_hash_table (info)->dynstr))
11910 if (bfd_link_relocatable (info))
11912 bfd_boolean failed = FALSE;
11914 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11919 /* If we have optimized stabs strings, output them. */
11920 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11922 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11926 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11929 elf_final_link_free (abfd, &flinfo);
11931 elf_linker (abfd) = TRUE;
11935 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11936 if (contents == NULL)
11937 return FALSE; /* Bail out and fail. */
11938 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11939 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11946 elf_final_link_free (abfd, &flinfo);
11950 /* Initialize COOKIE for input bfd ABFD. */
11953 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11954 struct bfd_link_info *info, bfd *abfd)
11956 Elf_Internal_Shdr *symtab_hdr;
11957 const struct elf_backend_data *bed;
11959 bed = get_elf_backend_data (abfd);
11960 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11962 cookie->abfd = abfd;
11963 cookie->sym_hashes = elf_sym_hashes (abfd);
11964 cookie->bad_symtab = elf_bad_symtab (abfd);
11965 if (cookie->bad_symtab)
11967 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11968 cookie->extsymoff = 0;
11972 cookie->locsymcount = symtab_hdr->sh_info;
11973 cookie->extsymoff = symtab_hdr->sh_info;
11976 if (bed->s->arch_size == 32)
11977 cookie->r_sym_shift = 8;
11979 cookie->r_sym_shift = 32;
11981 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11982 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11984 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11985 cookie->locsymcount, 0,
11987 if (cookie->locsyms == NULL)
11989 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11992 if (info->keep_memory)
11993 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11998 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
12001 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
12003 Elf_Internal_Shdr *symtab_hdr;
12005 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12006 if (cookie->locsyms != NULL
12007 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
12008 free (cookie->locsyms);
12011 /* Initialize the relocation information in COOKIE for input section SEC
12012 of input bfd ABFD. */
12015 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12016 struct bfd_link_info *info, bfd *abfd,
12019 const struct elf_backend_data *bed;
12021 if (sec->reloc_count == 0)
12023 cookie->rels = NULL;
12024 cookie->relend = NULL;
12028 bed = get_elf_backend_data (abfd);
12030 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12031 info->keep_memory);
12032 if (cookie->rels == NULL)
12034 cookie->rel = cookie->rels;
12035 cookie->relend = (cookie->rels
12036 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
12038 cookie->rel = cookie->rels;
12042 /* Free the memory allocated by init_reloc_cookie_rels,
12046 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
12049 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
12050 free (cookie->rels);
12053 /* Initialize the whole of COOKIE for input section SEC. */
12056 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12057 struct bfd_link_info *info,
12060 if (!init_reloc_cookie (cookie, info, sec->owner))
12062 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
12067 fini_reloc_cookie (cookie, sec->owner);
12072 /* Free the memory allocated by init_reloc_cookie_for_section,
12076 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
12079 fini_reloc_cookie_rels (cookie, sec);
12080 fini_reloc_cookie (cookie, sec->owner);
12083 /* Garbage collect unused sections. */
12085 /* Default gc_mark_hook. */
12088 _bfd_elf_gc_mark_hook (asection *sec,
12089 struct bfd_link_info *info ATTRIBUTE_UNUSED,
12090 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
12091 struct elf_link_hash_entry *h,
12092 Elf_Internal_Sym *sym)
12096 switch (h->root.type)
12098 case bfd_link_hash_defined:
12099 case bfd_link_hash_defweak:
12100 return h->root.u.def.section;
12102 case bfd_link_hash_common:
12103 return h->root.u.c.p->section;
12110 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
12115 /* COOKIE->rel describes a relocation against section SEC, which is
12116 a section we've decided to keep. Return the section that contains
12117 the relocation symbol, or NULL if no section contains it. */
12120 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
12121 elf_gc_mark_hook_fn gc_mark_hook,
12122 struct elf_reloc_cookie *cookie,
12123 bfd_boolean *start_stop)
12125 unsigned long r_symndx;
12126 struct elf_link_hash_entry *h;
12128 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
12129 if (r_symndx == STN_UNDEF)
12132 if (r_symndx >= cookie->locsymcount
12133 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12135 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
12138 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
12142 while (h->root.type == bfd_link_hash_indirect
12143 || h->root.type == bfd_link_hash_warning)
12144 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12146 /* If this symbol is weak and there is a non-weak definition, we
12147 keep the non-weak definition because many backends put
12148 dynamic reloc info on the non-weak definition for code
12149 handling copy relocs. */
12150 if (h->u.weakdef != NULL)
12151 h->u.weakdef->mark = 1;
12153 if (start_stop != NULL
12154 && (h->root.type == bfd_link_hash_undefined
12155 || h->root.type == bfd_link_hash_undefweak))
12157 /* To work around a glibc bug, mark all XXX input sections
12158 when there is an as yet undefined reference to __start_XXX
12159 or __stop_XXX symbols. The linker will later define such
12160 symbols for orphan input sections that have a name
12161 representable as a C identifier. */
12162 const char *sec_name = NULL;
12163 if (strncmp (h->root.root.string, "__start_", 8) == 0)
12164 sec_name = h->root.root.string + 8;
12165 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
12166 sec_name = h->root.root.string + 7;
12168 if (sec_name != NULL && *sec_name != '\0')
12172 for (i = info->input_bfds; i != NULL; i = i->link.next)
12174 asection *s = bfd_get_section_by_name (i, sec_name);
12175 if (s != NULL && !s->gc_mark)
12177 *start_stop = TRUE;
12184 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12187 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12188 &cookie->locsyms[r_symndx]);
12191 /* COOKIE->rel describes a relocation against section SEC, which is
12192 a section we've decided to keep. Mark the section that contains
12193 the relocation symbol. */
12196 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12198 elf_gc_mark_hook_fn gc_mark_hook,
12199 struct elf_reloc_cookie *cookie)
12202 bfd_boolean start_stop = FALSE;
12204 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
12205 while (rsec != NULL)
12207 if (!rsec->gc_mark)
12209 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12210 || (rsec->owner->flags & DYNAMIC) != 0)
12212 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12217 rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
12222 /* The mark phase of garbage collection. For a given section, mark
12223 it and any sections in this section's group, and all the sections
12224 which define symbols to which it refers. */
12227 _bfd_elf_gc_mark (struct bfd_link_info *info,
12229 elf_gc_mark_hook_fn gc_mark_hook)
12232 asection *group_sec, *eh_frame;
12236 /* Mark all the sections in the group. */
12237 group_sec = elf_section_data (sec)->next_in_group;
12238 if (group_sec && !group_sec->gc_mark)
12239 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12242 /* Look through the section relocs. */
12244 eh_frame = elf_eh_frame_section (sec->owner);
12245 if ((sec->flags & SEC_RELOC) != 0
12246 && sec->reloc_count > 0
12247 && sec != eh_frame)
12249 struct elf_reloc_cookie cookie;
12251 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12255 for (; cookie.rel < cookie.relend; cookie.rel++)
12256 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12261 fini_reloc_cookie_for_section (&cookie, sec);
12265 if (ret && eh_frame && elf_fde_list (sec))
12267 struct elf_reloc_cookie cookie;
12269 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12273 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12274 gc_mark_hook, &cookie))
12276 fini_reloc_cookie_for_section (&cookie, eh_frame);
12280 eh_frame = elf_section_eh_frame_entry (sec);
12281 if (ret && eh_frame && !eh_frame->gc_mark)
12282 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12288 /* Scan and mark sections in a special or debug section group. */
12291 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12293 /* Point to first section of section group. */
12295 /* Used to iterate the section group. */
12298 bfd_boolean is_special_grp = TRUE;
12299 bfd_boolean is_debug_grp = TRUE;
12301 /* First scan to see if group contains any section other than debug
12302 and special section. */
12303 ssec = msec = elf_next_in_group (grp);
12306 if ((msec->flags & SEC_DEBUGGING) == 0)
12307 is_debug_grp = FALSE;
12309 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12310 is_special_grp = FALSE;
12312 msec = elf_next_in_group (msec);
12314 while (msec != ssec);
12316 /* If this is a pure debug section group or pure special section group,
12317 keep all sections in this group. */
12318 if (is_debug_grp || is_special_grp)
12323 msec = elf_next_in_group (msec);
12325 while (msec != ssec);
12329 /* Keep debug and special sections. */
12332 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12333 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12337 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12340 bfd_boolean some_kept;
12341 bfd_boolean debug_frag_seen;
12343 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12346 /* Ensure all linker created sections are kept,
12347 see if any other section is already marked,
12348 and note if we have any fragmented debug sections. */
12349 debug_frag_seen = some_kept = FALSE;
12350 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12352 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12354 else if (isec->gc_mark)
12357 if (debug_frag_seen == FALSE
12358 && (isec->flags & SEC_DEBUGGING)
12359 && CONST_STRNEQ (isec->name, ".debug_line."))
12360 debug_frag_seen = TRUE;
12363 /* If no section in this file will be kept, then we can
12364 toss out the debug and special sections. */
12368 /* Keep debug and special sections like .comment when they are
12369 not part of a group. Also keep section groups that contain
12370 just debug sections or special sections. */
12371 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12373 if ((isec->flags & SEC_GROUP) != 0)
12374 _bfd_elf_gc_mark_debug_special_section_group (isec);
12375 else if (((isec->flags & SEC_DEBUGGING) != 0
12376 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12377 && elf_next_in_group (isec) == NULL)
12381 if (! debug_frag_seen)
12384 /* Look for CODE sections which are going to be discarded,
12385 and find and discard any fragmented debug sections which
12386 are associated with that code section. */
12387 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12388 if ((isec->flags & SEC_CODE) != 0
12389 && isec->gc_mark == 0)
12394 ilen = strlen (isec->name);
12396 /* Association is determined by the name of the debug section
12397 containing the name of the code section as a suffix. For
12398 example .debug_line.text.foo is a debug section associated
12400 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12404 if (dsec->gc_mark == 0
12405 || (dsec->flags & SEC_DEBUGGING) == 0)
12408 dlen = strlen (dsec->name);
12411 && strncmp (dsec->name + (dlen - ilen),
12412 isec->name, ilen) == 0)
12422 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12424 struct elf_gc_sweep_symbol_info
12426 struct bfd_link_info *info;
12427 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12432 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12435 && (((h->root.type == bfd_link_hash_defined
12436 || h->root.type == bfd_link_hash_defweak)
12437 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12438 && h->root.u.def.section->gc_mark))
12439 || h->root.type == bfd_link_hash_undefined
12440 || h->root.type == bfd_link_hash_undefweak))
12442 struct elf_gc_sweep_symbol_info *inf;
12444 inf = (struct elf_gc_sweep_symbol_info *) data;
12445 (*inf->hide_symbol) (inf->info, h, TRUE);
12446 h->def_regular = 0;
12447 h->ref_regular = 0;
12448 h->ref_regular_nonweak = 0;
12454 /* The sweep phase of garbage collection. Remove all garbage sections. */
12456 typedef bfd_boolean (*gc_sweep_hook_fn)
12457 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12460 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12463 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12464 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12465 unsigned long section_sym_count;
12466 struct elf_gc_sweep_symbol_info sweep_info;
12468 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12472 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12473 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12476 for (o = sub->sections; o != NULL; o = o->next)
12478 /* When any section in a section group is kept, we keep all
12479 sections in the section group. If the first member of
12480 the section group is excluded, we will also exclude the
12482 if (o->flags & SEC_GROUP)
12484 asection *first = elf_next_in_group (o);
12485 o->gc_mark = first->gc_mark;
12491 /* Skip sweeping sections already excluded. */
12492 if (o->flags & SEC_EXCLUDE)
12495 /* Since this is early in the link process, it is simple
12496 to remove a section from the output. */
12497 o->flags |= SEC_EXCLUDE;
12499 if (info->print_gc_sections && o->size != 0)
12500 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12502 /* But we also have to update some of the relocation
12503 info we collected before. */
12505 && (o->flags & SEC_RELOC) != 0
12506 && o->reloc_count != 0
12507 && !((info->strip == strip_all || info->strip == strip_debugger)
12508 && (o->flags & SEC_DEBUGGING) != 0)
12509 && !bfd_is_abs_section (o->output_section))
12511 Elf_Internal_Rela *internal_relocs;
12515 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12516 info->keep_memory);
12517 if (internal_relocs == NULL)
12520 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12522 if (elf_section_data (o)->relocs != internal_relocs)
12523 free (internal_relocs);
12531 /* Remove the symbols that were in the swept sections from the dynamic
12532 symbol table. GCFIXME: Anyone know how to get them out of the
12533 static symbol table as well? */
12534 sweep_info.info = info;
12535 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12536 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12539 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12543 /* Propagate collected vtable information. This is called through
12544 elf_link_hash_traverse. */
12547 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12549 /* Those that are not vtables. */
12550 if (h->vtable == NULL || h->vtable->parent == NULL)
12553 /* Those vtables that do not have parents, we cannot merge. */
12554 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12557 /* If we've already been done, exit. */
12558 if (h->vtable->used && h->vtable->used[-1])
12561 /* Make sure the parent's table is up to date. */
12562 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12564 if (h->vtable->used == NULL)
12566 /* None of this table's entries were referenced. Re-use the
12568 h->vtable->used = h->vtable->parent->vtable->used;
12569 h->vtable->size = h->vtable->parent->vtable->size;
12574 bfd_boolean *cu, *pu;
12576 /* Or the parent's entries into ours. */
12577 cu = h->vtable->used;
12579 pu = h->vtable->parent->vtable->used;
12582 const struct elf_backend_data *bed;
12583 unsigned int log_file_align;
12585 bed = get_elf_backend_data (h->root.u.def.section->owner);
12586 log_file_align = bed->s->log_file_align;
12587 n = h->vtable->parent->vtable->size >> log_file_align;
12602 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12605 bfd_vma hstart, hend;
12606 Elf_Internal_Rela *relstart, *relend, *rel;
12607 const struct elf_backend_data *bed;
12608 unsigned int log_file_align;
12610 /* Take care of both those symbols that do not describe vtables as
12611 well as those that are not loaded. */
12612 if (h->vtable == NULL || h->vtable->parent == NULL)
12615 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12616 || h->root.type == bfd_link_hash_defweak);
12618 sec = h->root.u.def.section;
12619 hstart = h->root.u.def.value;
12620 hend = hstart + h->size;
12622 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12624 return *(bfd_boolean *) okp = FALSE;
12625 bed = get_elf_backend_data (sec->owner);
12626 log_file_align = bed->s->log_file_align;
12628 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12630 for (rel = relstart; rel < relend; ++rel)
12631 if (rel->r_offset >= hstart && rel->r_offset < hend)
12633 /* If the entry is in use, do nothing. */
12634 if (h->vtable->used
12635 && (rel->r_offset - hstart) < h->vtable->size)
12637 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12638 if (h->vtable->used[entry])
12641 /* Otherwise, kill it. */
12642 rel->r_offset = rel->r_info = rel->r_addend = 0;
12648 /* Mark sections containing dynamically referenced symbols. When
12649 building shared libraries, we must assume that any visible symbol is
12653 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12655 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12656 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12658 if ((h->root.type == bfd_link_hash_defined
12659 || h->root.type == bfd_link_hash_defweak)
12661 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12662 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12663 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12664 && (!bfd_link_executable (info)
12665 || info->export_dynamic
12668 && (*d->match) (&d->head, NULL, h->root.root.string)))
12669 && (h->versioned >= versioned
12670 || !bfd_hide_sym_by_version (info->version_info,
12671 h->root.root.string)))))
12672 h->root.u.def.section->flags |= SEC_KEEP;
12677 /* Keep all sections containing symbols undefined on the command-line,
12678 and the section containing the entry symbol. */
12681 _bfd_elf_gc_keep (struct bfd_link_info *info)
12683 struct bfd_sym_chain *sym;
12685 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12687 struct elf_link_hash_entry *h;
12689 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12690 FALSE, FALSE, FALSE);
12693 && (h->root.type == bfd_link_hash_defined
12694 || h->root.type == bfd_link_hash_defweak)
12695 && !bfd_is_abs_section (h->root.u.def.section))
12696 h->root.u.def.section->flags |= SEC_KEEP;
12701 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12702 struct bfd_link_info *info)
12704 bfd *ibfd = info->input_bfds;
12706 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12709 struct elf_reloc_cookie cookie;
12711 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12714 if (!init_reloc_cookie (&cookie, info, ibfd))
12717 for (sec = ibfd->sections; sec; sec = sec->next)
12719 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12720 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12722 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12723 fini_reloc_cookie_rels (&cookie, sec);
12730 /* Do mark and sweep of unused sections. */
12733 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12735 bfd_boolean ok = TRUE;
12737 elf_gc_mark_hook_fn gc_mark_hook;
12738 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12739 struct elf_link_hash_table *htab;
12741 if (!bed->can_gc_sections
12742 || !is_elf_hash_table (info->hash))
12744 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12748 bed->gc_keep (info);
12749 htab = elf_hash_table (info);
12751 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12752 at the .eh_frame section if we can mark the FDEs individually. */
12753 for (sub = info->input_bfds;
12754 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12755 sub = sub->link.next)
12758 struct elf_reloc_cookie cookie;
12760 sec = bfd_get_section_by_name (sub, ".eh_frame");
12761 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12763 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12764 if (elf_section_data (sec)->sec_info
12765 && (sec->flags & SEC_LINKER_CREATED) == 0)
12766 elf_eh_frame_section (sub) = sec;
12767 fini_reloc_cookie_for_section (&cookie, sec);
12768 sec = bfd_get_next_section_by_name (NULL, sec);
12772 /* Apply transitive closure to the vtable entry usage info. */
12773 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12777 /* Kill the vtable relocations that were not used. */
12778 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12782 /* Mark dynamically referenced symbols. */
12783 if (htab->dynamic_sections_created)
12784 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12786 /* Grovel through relocs to find out who stays ... */
12787 gc_mark_hook = bed->gc_mark_hook;
12788 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12792 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12793 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12796 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12797 Also treat note sections as a root, if the section is not part
12799 for (o = sub->sections; o != NULL; o = o->next)
12801 && (o->flags & SEC_EXCLUDE) == 0
12802 && ((o->flags & SEC_KEEP) != 0
12803 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12804 && elf_next_in_group (o) == NULL )))
12806 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12811 /* Allow the backend to mark additional target specific sections. */
12812 bed->gc_mark_extra_sections (info, gc_mark_hook);
12814 /* ... and mark SEC_EXCLUDE for those that go. */
12815 return elf_gc_sweep (abfd, info);
12818 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12821 bfd_elf_gc_record_vtinherit (bfd *abfd,
12823 struct elf_link_hash_entry *h,
12826 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12827 struct elf_link_hash_entry **search, *child;
12828 bfd_size_type extsymcount;
12829 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12831 /* The sh_info field of the symtab header tells us where the
12832 external symbols start. We don't care about the local symbols at
12834 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12835 if (!elf_bad_symtab (abfd))
12836 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12838 sym_hashes = elf_sym_hashes (abfd);
12839 sym_hashes_end = sym_hashes + extsymcount;
12841 /* Hunt down the child symbol, which is in this section at the same
12842 offset as the relocation. */
12843 for (search = sym_hashes; search != sym_hashes_end; ++search)
12845 if ((child = *search) != NULL
12846 && (child->root.type == bfd_link_hash_defined
12847 || child->root.type == bfd_link_hash_defweak)
12848 && child->root.u.def.section == sec
12849 && child->root.u.def.value == offset)
12853 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12854 abfd, sec, (unsigned long) offset);
12855 bfd_set_error (bfd_error_invalid_operation);
12859 if (!child->vtable)
12861 child->vtable = ((struct elf_link_virtual_table_entry *)
12862 bfd_zalloc (abfd, sizeof (*child->vtable)));
12863 if (!child->vtable)
12868 /* This *should* only be the absolute section. It could potentially
12869 be that someone has defined a non-global vtable though, which
12870 would be bad. It isn't worth paging in the local symbols to be
12871 sure though; that case should simply be handled by the assembler. */
12873 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12876 child->vtable->parent = h;
12881 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12884 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12885 asection *sec ATTRIBUTE_UNUSED,
12886 struct elf_link_hash_entry *h,
12889 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12890 unsigned int log_file_align = bed->s->log_file_align;
12894 h->vtable = ((struct elf_link_virtual_table_entry *)
12895 bfd_zalloc (abfd, sizeof (*h->vtable)));
12900 if (addend >= h->vtable->size)
12902 size_t size, bytes, file_align;
12903 bfd_boolean *ptr = h->vtable->used;
12905 /* While the symbol is undefined, we have to be prepared to handle
12907 file_align = 1 << log_file_align;
12908 if (h->root.type == bfd_link_hash_undefined)
12909 size = addend + file_align;
12913 if (addend >= size)
12915 /* Oops! We've got a reference past the defined end of
12916 the table. This is probably a bug -- shall we warn? */
12917 size = addend + file_align;
12920 size = (size + file_align - 1) & -file_align;
12922 /* Allocate one extra entry for use as a "done" flag for the
12923 consolidation pass. */
12924 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12928 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12934 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12935 * sizeof (bfd_boolean));
12936 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12940 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12945 /* And arrange for that done flag to be at index -1. */
12946 h->vtable->used = ptr + 1;
12947 h->vtable->size = size;
12950 h->vtable->used[addend >> log_file_align] = TRUE;
12955 /* Map an ELF section header flag to its corresponding string. */
12959 flagword flag_value;
12960 } elf_flags_to_name_table;
12962 static elf_flags_to_name_table elf_flags_to_names [] =
12964 { "SHF_WRITE", SHF_WRITE },
12965 { "SHF_ALLOC", SHF_ALLOC },
12966 { "SHF_EXECINSTR", SHF_EXECINSTR },
12967 { "SHF_MERGE", SHF_MERGE },
12968 { "SHF_STRINGS", SHF_STRINGS },
12969 { "SHF_INFO_LINK", SHF_INFO_LINK},
12970 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12971 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12972 { "SHF_GROUP", SHF_GROUP },
12973 { "SHF_TLS", SHF_TLS },
12974 { "SHF_MASKOS", SHF_MASKOS },
12975 { "SHF_EXCLUDE", SHF_EXCLUDE },
12978 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12980 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12981 struct flag_info *flaginfo,
12984 const bfd_vma sh_flags = elf_section_flags (section);
12986 if (!flaginfo->flags_initialized)
12988 bfd *obfd = info->output_bfd;
12989 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12990 struct flag_info_list *tf = flaginfo->flag_list;
12992 int without_hex = 0;
12994 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12997 flagword (*lookup) (char *);
12999 lookup = bed->elf_backend_lookup_section_flags_hook;
13000 if (lookup != NULL)
13002 flagword hexval = (*lookup) ((char *) tf->name);
13006 if (tf->with == with_flags)
13007 with_hex |= hexval;
13008 else if (tf->with == without_flags)
13009 without_hex |= hexval;
13014 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
13016 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
13018 if (tf->with == with_flags)
13019 with_hex |= elf_flags_to_names[i].flag_value;
13020 else if (tf->with == without_flags)
13021 without_hex |= elf_flags_to_names[i].flag_value;
13028 info->callbacks->einfo
13029 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
13033 flaginfo->flags_initialized = TRUE;
13034 flaginfo->only_with_flags |= with_hex;
13035 flaginfo->not_with_flags |= without_hex;
13038 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
13041 if ((flaginfo->not_with_flags & sh_flags) != 0)
13047 struct alloc_got_off_arg {
13049 struct bfd_link_info *info;
13052 /* We need a special top-level link routine to convert got reference counts
13053 to real got offsets. */
13056 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
13058 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
13059 bfd *obfd = gofarg->info->output_bfd;
13060 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
13062 if (h->got.refcount > 0)
13064 h->got.offset = gofarg->gotoff;
13065 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
13068 h->got.offset = (bfd_vma) -1;
13073 /* And an accompanying bit to work out final got entry offsets once
13074 we're done. Should be called from final_link. */
13077 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
13078 struct bfd_link_info *info)
13081 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13083 struct alloc_got_off_arg gofarg;
13085 BFD_ASSERT (abfd == info->output_bfd);
13087 if (! is_elf_hash_table (info->hash))
13090 /* The GOT offset is relative to the .got section, but the GOT header is
13091 put into the .got.plt section, if the backend uses it. */
13092 if (bed->want_got_plt)
13095 gotoff = bed->got_header_size;
13097 /* Do the local .got entries first. */
13098 for (i = info->input_bfds; i; i = i->link.next)
13100 bfd_signed_vma *local_got;
13101 bfd_size_type j, locsymcount;
13102 Elf_Internal_Shdr *symtab_hdr;
13104 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
13107 local_got = elf_local_got_refcounts (i);
13111 symtab_hdr = &elf_tdata (i)->symtab_hdr;
13112 if (elf_bad_symtab (i))
13113 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
13115 locsymcount = symtab_hdr->sh_info;
13117 for (j = 0; j < locsymcount; ++j)
13119 if (local_got[j] > 0)
13121 local_got[j] = gotoff;
13122 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
13125 local_got[j] = (bfd_vma) -1;
13129 /* Then the global .got entries. .plt refcounts are handled by
13130 adjust_dynamic_symbol */
13131 gofarg.gotoff = gotoff;
13132 gofarg.info = info;
13133 elf_link_hash_traverse (elf_hash_table (info),
13134 elf_gc_allocate_got_offsets,
13139 /* Many folk need no more in the way of final link than this, once
13140 got entry reference counting is enabled. */
13143 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
13145 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
13148 /* Invoke the regular ELF backend linker to do all the work. */
13149 return bfd_elf_final_link (abfd, info);
13153 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
13155 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
13157 if (rcookie->bad_symtab)
13158 rcookie->rel = rcookie->rels;
13160 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
13162 unsigned long r_symndx;
13164 if (! rcookie->bad_symtab)
13165 if (rcookie->rel->r_offset > offset)
13167 if (rcookie->rel->r_offset != offset)
13170 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
13171 if (r_symndx == STN_UNDEF)
13174 if (r_symndx >= rcookie->locsymcount
13175 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
13177 struct elf_link_hash_entry *h;
13179 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
13181 while (h->root.type == bfd_link_hash_indirect
13182 || h->root.type == bfd_link_hash_warning)
13183 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13185 if ((h->root.type == bfd_link_hash_defined
13186 || h->root.type == bfd_link_hash_defweak)
13187 && (h->root.u.def.section->owner != rcookie->abfd
13188 || h->root.u.def.section->kept_section != NULL
13189 || discarded_section (h->root.u.def.section)))
13194 /* It's not a relocation against a global symbol,
13195 but it could be a relocation against a local
13196 symbol for a discarded section. */
13198 Elf_Internal_Sym *isym;
13200 /* Need to: get the symbol; get the section. */
13201 isym = &rcookie->locsyms[r_symndx];
13202 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13204 && (isec->kept_section != NULL
13205 || discarded_section (isec)))
13213 /* Discard unneeded references to discarded sections.
13214 Returns -1 on error, 1 if any section's size was changed, 0 if
13215 nothing changed. This function assumes that the relocations are in
13216 sorted order, which is true for all known assemblers. */
13219 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13221 struct elf_reloc_cookie cookie;
13226 if (info->traditional_format
13227 || !is_elf_hash_table (info->hash))
13230 o = bfd_get_section_by_name (output_bfd, ".stab");
13235 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13238 || i->reloc_count == 0
13239 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13243 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13246 if (!init_reloc_cookie_for_section (&cookie, info, i))
13249 if (_bfd_discard_section_stabs (abfd, i,
13250 elf_section_data (i)->sec_info,
13251 bfd_elf_reloc_symbol_deleted_p,
13255 fini_reloc_cookie_for_section (&cookie, i);
13260 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13261 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13266 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13272 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13275 if (!init_reloc_cookie_for_section (&cookie, info, i))
13278 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13279 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13280 bfd_elf_reloc_symbol_deleted_p,
13284 fini_reloc_cookie_for_section (&cookie, i);
13288 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13290 const struct elf_backend_data *bed;
13292 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13295 bed = get_elf_backend_data (abfd);
13297 if (bed->elf_backend_discard_info != NULL)
13299 if (!init_reloc_cookie (&cookie, info, abfd))
13302 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13305 fini_reloc_cookie (&cookie, abfd);
13309 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13310 _bfd_elf_end_eh_frame_parsing (info);
13312 if (info->eh_frame_hdr_type
13313 && !bfd_link_relocatable (info)
13314 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13321 _bfd_elf_section_already_linked (bfd *abfd,
13323 struct bfd_link_info *info)
13326 const char *name, *key;
13327 struct bfd_section_already_linked *l;
13328 struct bfd_section_already_linked_hash_entry *already_linked_list;
13330 if (sec->output_section == bfd_abs_section_ptr)
13333 flags = sec->flags;
13335 /* Return if it isn't a linkonce section. A comdat group section
13336 also has SEC_LINK_ONCE set. */
13337 if ((flags & SEC_LINK_ONCE) == 0)
13340 /* Don't put group member sections on our list of already linked
13341 sections. They are handled as a group via their group section. */
13342 if (elf_sec_group (sec) != NULL)
13345 /* For a SHT_GROUP section, use the group signature as the key. */
13347 if ((flags & SEC_GROUP) != 0
13348 && elf_next_in_group (sec) != NULL
13349 && elf_group_name (elf_next_in_group (sec)) != NULL)
13350 key = elf_group_name (elf_next_in_group (sec));
13353 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13354 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13355 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13358 /* Must be a user linkonce section that doesn't follow gcc's
13359 naming convention. In this case we won't be matching
13360 single member groups. */
13364 already_linked_list = bfd_section_already_linked_table_lookup (key);
13366 for (l = already_linked_list->entry; l != NULL; l = l->next)
13368 /* We may have 2 different types of sections on the list: group
13369 sections with a signature of <key> (<key> is some string),
13370 and linkonce sections named .gnu.linkonce.<type>.<key>.
13371 Match like sections. LTO plugin sections are an exception.
13372 They are always named .gnu.linkonce.t.<key> and match either
13373 type of section. */
13374 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13375 && ((flags & SEC_GROUP) != 0
13376 || strcmp (name, l->sec->name) == 0))
13377 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13379 /* The section has already been linked. See if we should
13380 issue a warning. */
13381 if (!_bfd_handle_already_linked (sec, l, info))
13384 if (flags & SEC_GROUP)
13386 asection *first = elf_next_in_group (sec);
13387 asection *s = first;
13391 s->output_section = bfd_abs_section_ptr;
13392 /* Record which group discards it. */
13393 s->kept_section = l->sec;
13394 s = elf_next_in_group (s);
13395 /* These lists are circular. */
13405 /* A single member comdat group section may be discarded by a
13406 linkonce section and vice versa. */
13407 if ((flags & SEC_GROUP) != 0)
13409 asection *first = elf_next_in_group (sec);
13411 if (first != NULL && elf_next_in_group (first) == first)
13412 /* Check this single member group against linkonce sections. */
13413 for (l = already_linked_list->entry; l != NULL; l = l->next)
13414 if ((l->sec->flags & SEC_GROUP) == 0
13415 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13417 first->output_section = bfd_abs_section_ptr;
13418 first->kept_section = l->sec;
13419 sec->output_section = bfd_abs_section_ptr;
13424 /* Check this linkonce section against single member groups. */
13425 for (l = already_linked_list->entry; l != NULL; l = l->next)
13426 if (l->sec->flags & SEC_GROUP)
13428 asection *first = elf_next_in_group (l->sec);
13431 && elf_next_in_group (first) == first
13432 && bfd_elf_match_symbols_in_sections (first, sec, info))
13434 sec->output_section = bfd_abs_section_ptr;
13435 sec->kept_section = first;
13440 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13441 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13442 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13443 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13444 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13445 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13446 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13447 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13448 The reverse order cannot happen as there is never a bfd with only the
13449 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13450 matter as here were are looking only for cross-bfd sections. */
13452 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13453 for (l = already_linked_list->entry; l != NULL; l = l->next)
13454 if ((l->sec->flags & SEC_GROUP) == 0
13455 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13457 if (abfd != l->sec->owner)
13458 sec->output_section = bfd_abs_section_ptr;
13462 /* This is the first section with this name. Record it. */
13463 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13464 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13465 return sec->output_section == bfd_abs_section_ptr;
13469 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13471 return sym->st_shndx == SHN_COMMON;
13475 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13481 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13483 return bfd_com_section_ptr;
13487 _bfd_elf_default_got_elt_size (bfd *abfd,
13488 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13489 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13490 bfd *ibfd ATTRIBUTE_UNUSED,
13491 unsigned long symndx ATTRIBUTE_UNUSED)
13493 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13494 return bed->s->arch_size / 8;
13497 /* Routines to support the creation of dynamic relocs. */
13499 /* Returns the name of the dynamic reloc section associated with SEC. */
13501 static const char *
13502 get_dynamic_reloc_section_name (bfd * abfd,
13504 bfd_boolean is_rela)
13507 const char *old_name = bfd_get_section_name (NULL, sec);
13508 const char *prefix = is_rela ? ".rela" : ".rel";
13510 if (old_name == NULL)
13513 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13514 sprintf (name, "%s%s", prefix, old_name);
13519 /* Returns the dynamic reloc section associated with SEC.
13520 If necessary compute the name of the dynamic reloc section based
13521 on SEC's name (looked up in ABFD's string table) and the setting
13525 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13527 bfd_boolean is_rela)
13529 asection * reloc_sec = elf_section_data (sec)->sreloc;
13531 if (reloc_sec == NULL)
13533 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13537 reloc_sec = bfd_get_linker_section (abfd, name);
13539 if (reloc_sec != NULL)
13540 elf_section_data (sec)->sreloc = reloc_sec;
13547 /* Returns the dynamic reloc section associated with SEC. If the
13548 section does not exist it is created and attached to the DYNOBJ
13549 bfd and stored in the SRELOC field of SEC's elf_section_data
13552 ALIGNMENT is the alignment for the newly created section and
13553 IS_RELA defines whether the name should be .rela.<SEC's name>
13554 or .rel.<SEC's name>. The section name is looked up in the
13555 string table associated with ABFD. */
13558 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13560 unsigned int alignment,
13562 bfd_boolean is_rela)
13564 asection * reloc_sec = elf_section_data (sec)->sreloc;
13566 if (reloc_sec == NULL)
13568 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13573 reloc_sec = bfd_get_linker_section (dynobj, name);
13575 if (reloc_sec == NULL)
13577 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13578 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13579 if ((sec->flags & SEC_ALLOC) != 0)
13580 flags |= SEC_ALLOC | SEC_LOAD;
13582 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13583 if (reloc_sec != NULL)
13585 /* _bfd_elf_get_sec_type_attr chooses a section type by
13586 name. Override as it may be wrong, eg. for a user
13587 section named "auto" we'll get ".relauto" which is
13588 seen to be a .rela section. */
13589 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13590 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13595 elf_section_data (sec)->sreloc = reloc_sec;
13601 /* Copy the ELF symbol type and other attributes for a linker script
13602 assignment from HSRC to HDEST. Generally this should be treated as
13603 if we found a strong non-dynamic definition for HDEST (except that
13604 ld ignores multiple definition errors). */
13606 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13607 struct bfd_link_hash_entry *hdest,
13608 struct bfd_link_hash_entry *hsrc)
13610 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13611 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13612 Elf_Internal_Sym isym;
13614 ehdest->type = ehsrc->type;
13615 ehdest->target_internal = ehsrc->target_internal;
13617 isym.st_other = ehsrc->other;
13618 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13621 /* Append a RELA relocation REL to section S in BFD. */
13624 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13626 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13627 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13628 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13629 bed->s->swap_reloca_out (abfd, rel, loc);
13632 /* Append a REL relocation REL to section S in BFD. */
13635 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13637 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13638 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13639 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13640 bed->s->swap_reloc_out (abfd, rel, loc);