1 /* ELF linking support for BFD.
2 Copyright (C) 1995-2015 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 (info->executable)
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))
283 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
284 flags | SEC_READONLY);
288 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
290 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
293 /* The special symbol _DYNAMIC is always set to the start of the
294 .dynamic section. We could set _DYNAMIC in a linker script, but we
295 only want to define it if we are, in fact, creating a .dynamic
296 section. We don't want to define it if there is no .dynamic
297 section, since on some ELF platforms the start up code examines it
298 to decide how to initialize the process. */
299 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
300 elf_hash_table (info)->hdynamic = h;
306 s = bfd_make_section_anyway_with_flags (abfd, ".hash",
307 flags | SEC_READONLY);
309 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
311 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
314 if (info->emit_gnu_hash)
316 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
317 flags | SEC_READONLY);
319 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
321 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
322 4 32-bit words followed by variable count of 64-bit words, then
323 variable count of 32-bit words. */
324 if (bed->s->arch_size == 64)
325 elf_section_data (s)->this_hdr.sh_entsize = 0;
327 elf_section_data (s)->this_hdr.sh_entsize = 4;
330 /* Let the backend create the rest of the sections. This lets the
331 backend set the right flags. The backend will normally create
332 the .got and .plt sections. */
333 if (bed->elf_backend_create_dynamic_sections == NULL
334 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
337 elf_hash_table (info)->dynamic_sections_created = TRUE;
342 /* Create dynamic sections when linking against a dynamic object. */
345 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
347 flagword flags, pltflags;
348 struct elf_link_hash_entry *h;
350 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
351 struct elf_link_hash_table *htab = elf_hash_table (info);
353 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
354 .rel[a].bss sections. */
355 flags = bed->dynamic_sec_flags;
358 if (bed->plt_not_loaded)
359 /* We do not clear SEC_ALLOC here because we still want the OS to
360 allocate space for the section; it's just that there's nothing
361 to read in from the object file. */
362 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
364 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
365 if (bed->plt_readonly)
366 pltflags |= SEC_READONLY;
368 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
370 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
374 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
376 if (bed->want_plt_sym)
378 h = _bfd_elf_define_linkage_sym (abfd, info, s,
379 "_PROCEDURE_LINKAGE_TABLE_");
380 elf_hash_table (info)->hplt = h;
385 s = bfd_make_section_anyway_with_flags (abfd,
386 (bed->rela_plts_and_copies_p
387 ? ".rela.plt" : ".rel.plt"),
388 flags | SEC_READONLY);
390 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
394 if (! _bfd_elf_create_got_section (abfd, info))
397 if (bed->want_dynbss)
399 /* The .dynbss section is a place to put symbols which are defined
400 by dynamic objects, are referenced by regular objects, and are
401 not functions. We must allocate space for them in the process
402 image and use a R_*_COPY reloc to tell the dynamic linker to
403 initialize them at run time. The linker script puts the .dynbss
404 section into the .bss section of the final image. */
405 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
406 (SEC_ALLOC | SEC_LINKER_CREATED));
410 /* The .rel[a].bss section holds copy relocs. This section is not
411 normally needed. We need to create it here, though, so that the
412 linker will map it to an output section. We can't just create it
413 only if we need it, because we will not know whether we need it
414 until we have seen all the input files, and the first time the
415 main linker code calls BFD after examining all the input files
416 (size_dynamic_sections) the input sections have already been
417 mapped to the output sections. If the section turns out not to
418 be needed, we can discard it later. We will never need this
419 section when generating a shared object, since they do not use
423 s = bfd_make_section_anyway_with_flags (abfd,
424 (bed->rela_plts_and_copies_p
425 ? ".rela.bss" : ".rel.bss"),
426 flags | SEC_READONLY);
428 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
436 /* Record a new dynamic symbol. We record the dynamic symbols as we
437 read the input files, since we need to have a list of all of them
438 before we can determine the final sizes of the output sections.
439 Note that we may actually call this function even though we are not
440 going to output any dynamic symbols; in some cases we know that a
441 symbol should be in the dynamic symbol table, but only if there is
445 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
446 struct elf_link_hash_entry *h)
448 if (h->dynindx == -1)
450 struct elf_strtab_hash *dynstr;
455 /* XXX: The ABI draft says the linker must turn hidden and
456 internal symbols into STB_LOCAL symbols when producing the
457 DSO. However, if ld.so honors st_other in the dynamic table,
458 this would not be necessary. */
459 switch (ELF_ST_VISIBILITY (h->other))
463 if (h->root.type != bfd_link_hash_undefined
464 && h->root.type != bfd_link_hash_undefweak)
467 if (!elf_hash_table (info)->is_relocatable_executable)
475 h->dynindx = elf_hash_table (info)->dynsymcount;
476 ++elf_hash_table (info)->dynsymcount;
478 dynstr = elf_hash_table (info)->dynstr;
481 /* Create a strtab to hold the dynamic symbol names. */
482 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
487 /* We don't put any version information in the dynamic string
489 name = h->root.root.string;
490 p = strchr (name, ELF_VER_CHR);
492 /* We know that the p points into writable memory. In fact,
493 there are only a few symbols that have read-only names, being
494 those like _GLOBAL_OFFSET_TABLE_ that are created specially
495 by the backends. Most symbols will have names pointing into
496 an ELF string table read from a file, or to objalloc memory. */
499 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
504 if (indx == (bfd_size_type) -1)
506 h->dynstr_index = indx;
512 /* Mark a symbol dynamic. */
515 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
516 struct elf_link_hash_entry *h,
517 Elf_Internal_Sym *sym)
519 struct bfd_elf_dynamic_list *d = info->dynamic_list;
521 /* It may be called more than once on the same H. */
522 if(h->dynamic || info->relocatable)
525 if ((info->dynamic_data
526 && (h->type == STT_OBJECT
528 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT)))
530 && h->root.type == bfd_link_hash_new
531 && (*d->match) (&d->head, NULL, h->root.root.string)))
535 /* Record an assignment to a symbol made by a linker script. We need
536 this in case some dynamic object refers to this symbol. */
539 bfd_elf_record_link_assignment (bfd *output_bfd,
540 struct bfd_link_info *info,
545 struct elf_link_hash_entry *h, *hv;
546 struct elf_link_hash_table *htab;
547 const struct elf_backend_data *bed;
549 if (!is_elf_hash_table (info->hash))
552 htab = elf_hash_table (info);
553 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE);
557 switch (h->root.type)
559 case bfd_link_hash_defined:
560 case bfd_link_hash_defweak:
561 case bfd_link_hash_common:
563 case bfd_link_hash_undefweak:
564 case bfd_link_hash_undefined:
565 /* Since we're defining the symbol, don't let it seem to have not
566 been defined. record_dynamic_symbol and size_dynamic_sections
567 may depend on this. */
568 h->root.type = bfd_link_hash_new;
569 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
570 bfd_link_repair_undef_list (&htab->root);
572 case bfd_link_hash_new:
573 bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
576 case bfd_link_hash_indirect:
577 /* We had a versioned symbol in a dynamic library. We make the
578 the versioned symbol point to this one. */
579 bed = get_elf_backend_data (output_bfd);
581 while (hv->root.type == bfd_link_hash_indirect
582 || hv->root.type == bfd_link_hash_warning)
583 hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
584 /* We don't need to update h->root.u since linker will set them
586 h->root.type = bfd_link_hash_undefined;
587 hv->root.type = bfd_link_hash_indirect;
588 hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
589 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
591 case bfd_link_hash_warning:
596 /* If this symbol is being provided by the linker script, and it is
597 currently defined by a dynamic object, but not by a regular
598 object, then mark it as undefined so that the generic linker will
599 force the correct value. */
603 h->root.type = bfd_link_hash_undefined;
605 /* If this symbol is not being provided by the linker script, and it is
606 currently defined by a dynamic object, but not by a regular object,
607 then clear out any version information because the symbol will not be
608 associated with the dynamic object any more. */
612 h->verinfo.verdef = NULL;
618 bed = get_elf_backend_data (output_bfd);
619 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
620 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
621 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
624 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
626 if (!info->relocatable
628 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
629 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
635 || (info->executable && elf_hash_table (info)->is_relocatable_executable))
638 if (! bfd_elf_link_record_dynamic_symbol (info, h))
641 /* If this is a weak defined symbol, and we know a corresponding
642 real symbol from the same dynamic object, make sure the real
643 symbol is also made into a dynamic symbol. */
644 if (h->u.weakdef != NULL
645 && h->u.weakdef->dynindx == -1)
647 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
655 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
656 success, and 2 on a failure caused by attempting to record a symbol
657 in a discarded section, eg. a discarded link-once section symbol. */
660 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
665 struct elf_link_local_dynamic_entry *entry;
666 struct elf_link_hash_table *eht;
667 struct elf_strtab_hash *dynstr;
668 unsigned long dynstr_index;
670 Elf_External_Sym_Shndx eshndx;
671 char esym[sizeof (Elf64_External_Sym)];
673 if (! is_elf_hash_table (info->hash))
676 /* See if the entry exists already. */
677 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
678 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
681 amt = sizeof (*entry);
682 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
686 /* Go find the symbol, so that we can find it's name. */
687 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
688 1, input_indx, &entry->isym, esym, &eshndx))
690 bfd_release (input_bfd, entry);
694 if (entry->isym.st_shndx != SHN_UNDEF
695 && entry->isym.st_shndx < SHN_LORESERVE)
699 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
700 if (s == NULL || bfd_is_abs_section (s->output_section))
702 /* We can still bfd_release here as nothing has done another
703 bfd_alloc. We can't do this later in this function. */
704 bfd_release (input_bfd, entry);
709 name = (bfd_elf_string_from_elf_section
710 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
711 entry->isym.st_name));
713 dynstr = elf_hash_table (info)->dynstr;
716 /* Create a strtab to hold the dynamic symbol names. */
717 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
722 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
723 if (dynstr_index == (unsigned long) -1)
725 entry->isym.st_name = dynstr_index;
727 eht = elf_hash_table (info);
729 entry->next = eht->dynlocal;
730 eht->dynlocal = entry;
731 entry->input_bfd = input_bfd;
732 entry->input_indx = input_indx;
735 /* Whatever binding the symbol had before, it's now local. */
737 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
739 /* The dynindx will be set at the end of size_dynamic_sections. */
744 /* Return the dynindex of a local dynamic symbol. */
747 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
751 struct elf_link_local_dynamic_entry *e;
753 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
754 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
759 /* This function is used to renumber the dynamic symbols, if some of
760 them are removed because they are marked as local. This is called
761 via elf_link_hash_traverse. */
764 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
767 size_t *count = (size_t *) data;
772 if (h->dynindx != -1)
773 h->dynindx = ++(*count);
779 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
780 STB_LOCAL binding. */
783 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
786 size_t *count = (size_t *) data;
788 if (!h->forced_local)
791 if (h->dynindx != -1)
792 h->dynindx = ++(*count);
797 /* Return true if the dynamic symbol for a given section should be
798 omitted when creating a shared library. */
800 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
801 struct bfd_link_info *info,
804 struct elf_link_hash_table *htab;
807 switch (elf_section_data (p)->this_hdr.sh_type)
811 /* If sh_type is yet undecided, assume it could be
812 SHT_PROGBITS/SHT_NOBITS. */
814 htab = elf_hash_table (info);
815 if (p == htab->tls_sec)
818 if (htab->text_index_section != NULL)
819 return p != htab->text_index_section && p != htab->data_index_section;
821 return (htab->dynobj != NULL
822 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
823 && ip->output_section == p);
825 /* There shouldn't be section relative relocations
826 against any other section. */
832 /* Assign dynsym indices. In a shared library we generate a section
833 symbol for each output section, which come first. Next come symbols
834 which have been forced to local binding. Then all of the back-end
835 allocated local dynamic syms, followed by the rest of the global
839 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
840 struct bfd_link_info *info,
841 unsigned long *section_sym_count)
843 unsigned long dynsymcount = 0;
845 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
847 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
849 for (p = output_bfd->sections; p ; p = p->next)
850 if ((p->flags & SEC_EXCLUDE) == 0
851 && (p->flags & SEC_ALLOC) != 0
852 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
853 elf_section_data (p)->dynindx = ++dynsymcount;
855 elf_section_data (p)->dynindx = 0;
857 *section_sym_count = dynsymcount;
859 elf_link_hash_traverse (elf_hash_table (info),
860 elf_link_renumber_local_hash_table_dynsyms,
863 if (elf_hash_table (info)->dynlocal)
865 struct elf_link_local_dynamic_entry *p;
866 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
867 p->dynindx = ++dynsymcount;
870 elf_link_hash_traverse (elf_hash_table (info),
871 elf_link_renumber_hash_table_dynsyms,
874 /* There is an unused NULL entry at the head of the table which
875 we must account for in our count. Unless there weren't any
876 symbols, which means we'll have no table at all. */
877 if (dynsymcount != 0)
880 elf_hash_table (info)->dynsymcount = dynsymcount;
884 /* Merge st_other field. */
887 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
888 const Elf_Internal_Sym *isym, asection *sec,
889 bfd_boolean definition, bfd_boolean dynamic)
891 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
893 /* If st_other has a processor-specific meaning, specific
894 code might be needed here. */
895 if (bed->elf_backend_merge_symbol_attribute)
896 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition,
901 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other);
902 unsigned hvis = ELF_ST_VISIBILITY (h->other);
904 /* Keep the most constraining visibility. Leave the remainder
905 of the st_other field to elf_backend_merge_symbol_attribute. */
906 if (symvis - 1 < hvis - 1)
907 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
910 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
911 && (sec->flags & SEC_READONLY) == 0)
912 h->protected_def = 1;
915 /* This function is called when we want to merge a new symbol with an
916 existing symbol. It handles the various cases which arise when we
917 find a definition in a dynamic object, or when there is already a
918 definition in a dynamic object. The new symbol is described by
919 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
920 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
921 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
922 of an old common symbol. We set OVERRIDE if the old symbol is
923 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
924 the type to change. We set SIZE_CHANGE_OK if it is OK for the size
925 to change. By OK to change, we mean that we shouldn't warn if the
926 type or size does change. */
929 _bfd_elf_merge_symbol (bfd *abfd,
930 struct bfd_link_info *info,
932 Elf_Internal_Sym *sym,
935 struct elf_link_hash_entry **sym_hash,
937 bfd_boolean *pold_weak,
938 unsigned int *pold_alignment,
940 bfd_boolean *override,
941 bfd_boolean *type_change_ok,
942 bfd_boolean *size_change_ok)
944 asection *sec, *oldsec;
945 struct elf_link_hash_entry *h;
946 struct elf_link_hash_entry *hi;
947 struct elf_link_hash_entry *flip;
950 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
951 bfd_boolean newweak, oldweak, newfunc, oldfunc;
952 const struct elf_backend_data *bed;
958 bind = ELF_ST_BIND (sym->st_info);
960 if (! bfd_is_und_section (sec))
961 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE);
963 h = ((struct elf_link_hash_entry *)
964 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE));
969 bed = get_elf_backend_data (abfd);
971 /* For merging, we only care about real symbols. But we need to make
972 sure that indirect symbol dynamic flags are updated. */
974 while (h->root.type == bfd_link_hash_indirect
975 || h->root.type == bfd_link_hash_warning)
976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
978 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
983 switch (h->root.type)
988 case bfd_link_hash_undefined:
989 case bfd_link_hash_undefweak:
990 oldbfd = h->root.u.undef.abfd;
993 case bfd_link_hash_defined:
994 case bfd_link_hash_defweak:
995 oldbfd = h->root.u.def.section->owner;
996 oldsec = h->root.u.def.section;
999 case bfd_link_hash_common:
1000 oldbfd = h->root.u.c.p->section->owner;
1001 oldsec = h->root.u.c.p->section;
1003 *pold_alignment = h->root.u.c.p->alignment_power;
1006 if (poldbfd && *poldbfd == NULL)
1009 /* Differentiate strong and weak symbols. */
1010 newweak = bind == STB_WEAK;
1011 oldweak = (h->root.type == bfd_link_hash_defweak
1012 || h->root.type == bfd_link_hash_undefweak);
1014 *pold_weak = oldweak;
1016 /* This code is for coping with dynamic objects, and is only useful
1017 if we are doing an ELF link. */
1018 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
1021 /* We have to check it for every instance since the first few may be
1022 references and not all compilers emit symbol type for undefined
1024 bfd_elf_link_mark_dynamic_symbol (info, h, sym);
1026 /* NEWDYN and OLDDYN indicate whether the new or old symbol,
1027 respectively, is from a dynamic object. */
1029 newdyn = (abfd->flags & DYNAMIC) != 0;
1031 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
1032 syms and defined syms in dynamic libraries respectively.
1033 ref_dynamic on the other hand can be set for a symbol defined in
1034 a dynamic library, and def_dynamic may not be set; When the
1035 definition in a dynamic lib is overridden by a definition in the
1036 executable use of the symbol in the dynamic lib becomes a
1037 reference to the executable symbol. */
1040 if (bfd_is_und_section (sec))
1042 if (bind != STB_WEAK)
1044 h->ref_dynamic_nonweak = 1;
1045 hi->ref_dynamic_nonweak = 1;
1051 hi->dynamic_def = 1;
1055 /* If we just created the symbol, mark it as being an ELF symbol.
1056 Other than that, there is nothing to do--there is no merge issue
1057 with a newly defined symbol--so we just return. */
1059 if (h->root.type == bfd_link_hash_new)
1065 /* In cases involving weak versioned symbols, we may wind up trying
1066 to merge a symbol with itself. Catch that here, to avoid the
1067 confusion that results if we try to override a symbol with
1068 itself. The additional tests catch cases like
1069 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
1070 dynamic object, which we do want to handle here. */
1072 && (newweak || oldweak)
1073 && ((abfd->flags & DYNAMIC) == 0
1074 || !h->def_regular))
1079 olddyn = (oldbfd->flags & DYNAMIC) != 0;
1080 else if (oldsec != NULL)
1082 /* This handles the special SHN_MIPS_{TEXT,DATA} section
1083 indices used by MIPS ELF. */
1084 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
1087 /* NEWDEF and OLDDEF indicate whether the new or old symbol,
1088 respectively, appear to be a definition rather than reference. */
1090 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
1092 olddef = (h->root.type != bfd_link_hash_undefined
1093 && h->root.type != bfd_link_hash_undefweak
1094 && h->root.type != bfd_link_hash_common);
1096 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
1097 respectively, appear to be a function. */
1099 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1100 && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
1102 oldfunc = (h->type != STT_NOTYPE
1103 && bed->is_function_type (h->type));
1105 /* When we try to create a default indirect symbol from the dynamic
1106 definition with the default version, we skip it if its type and
1107 the type of existing regular definition mismatch. */
1108 if (pold_alignment == NULL
1112 && (((olddef || h->root.type == bfd_link_hash_common)
1113 && ELF_ST_TYPE (sym->st_info) != h->type
1114 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
1115 && h->type != STT_NOTYPE
1116 && !(newfunc && oldfunc))
1118 && ((h->type == STT_GNU_IFUNC)
1119 != (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))))
1125 /* Check TLS symbols. We don't check undefined symbols introduced
1126 by "ld -u" which have no type (and oldbfd NULL), and we don't
1127 check symbols from plugins because they also have no type. */
1129 && (oldbfd->flags & BFD_PLUGIN) == 0
1130 && (abfd->flags & BFD_PLUGIN) == 0
1131 && ELF_ST_TYPE (sym->st_info) != h->type
1132 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
1135 bfd_boolean ntdef, tdef;
1136 asection *ntsec, *tsec;
1138 if (h->type == STT_TLS)
1158 (*_bfd_error_handler)
1159 (_("%s: TLS definition in %B section %A "
1160 "mismatches non-TLS definition in %B section %A"),
1161 tbfd, tsec, ntbfd, ntsec, h->root.root.string);
1162 else if (!tdef && !ntdef)
1163 (*_bfd_error_handler)
1164 (_("%s: TLS reference in %B "
1165 "mismatches non-TLS reference in %B"),
1166 tbfd, ntbfd, h->root.root.string);
1168 (*_bfd_error_handler)
1169 (_("%s: TLS definition in %B section %A "
1170 "mismatches non-TLS reference in %B"),
1171 tbfd, tsec, ntbfd, h->root.root.string);
1173 (*_bfd_error_handler)
1174 (_("%s: TLS reference in %B "
1175 "mismatches non-TLS definition in %B section %A"),
1176 tbfd, ntbfd, ntsec, h->root.root.string);
1178 bfd_set_error (bfd_error_bad_value);
1182 /* If the old symbol has non-default visibility, we ignore the new
1183 definition from a dynamic object. */
1185 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1186 && !bfd_is_und_section (sec))
1189 /* Make sure this symbol is dynamic. */
1191 hi->ref_dynamic = 1;
1192 /* A protected symbol has external availability. Make sure it is
1193 recorded as dynamic.
1195 FIXME: Should we check type and size for protected symbol? */
1196 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1197 return bfd_elf_link_record_dynamic_symbol (info, h);
1202 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
1205 /* If the new symbol with non-default visibility comes from a
1206 relocatable file and the old definition comes from a dynamic
1207 object, we remove the old definition. */
1208 if (hi->root.type == bfd_link_hash_indirect)
1210 /* Handle the case where the old dynamic definition is
1211 default versioned. We need to copy the symbol info from
1212 the symbol with default version to the normal one if it
1213 was referenced before. */
1216 hi->root.type = h->root.type;
1217 h->root.type = bfd_link_hash_indirect;
1218 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
1220 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1221 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1223 /* If the new symbol is hidden or internal, completely undo
1224 any dynamic link state. */
1225 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1226 h->forced_local = 0;
1233 /* FIXME: Should we check type and size for protected symbol? */
1243 /* If the old symbol was undefined before, then it will still be
1244 on the undefs list. If the new symbol is undefined or
1245 common, we can't make it bfd_link_hash_new here, because new
1246 undefined or common symbols will be added to the undefs list
1247 by _bfd_generic_link_add_one_symbol. Symbols may not be
1248 added twice to the undefs list. Also, if the new symbol is
1249 undefweak then we don't want to lose the strong undef. */
1250 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
1252 h->root.type = bfd_link_hash_undefined;
1253 h->root.u.undef.abfd = abfd;
1257 h->root.type = bfd_link_hash_new;
1258 h->root.u.undef.abfd = NULL;
1261 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
1263 /* If the new symbol is hidden or internal, completely undo
1264 any dynamic link state. */
1265 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1266 h->forced_local = 0;
1272 /* FIXME: Should we check type and size for protected symbol? */
1278 /* If a new weak symbol definition comes from a regular file and the
1279 old symbol comes from a dynamic library, we treat the new one as
1280 strong. Similarly, an old weak symbol definition from a regular
1281 file is treated as strong when the new symbol comes from a dynamic
1282 library. Further, an old weak symbol from a dynamic library is
1283 treated as strong if the new symbol is from a dynamic library.
1284 This reflects the way glibc's ld.so works.
1286 Do this before setting *type_change_ok or *size_change_ok so that
1287 we warn properly when dynamic library symbols are overridden. */
1289 if (newdef && !newdyn && olddyn)
1291 if (olddef && newdyn)
1294 /* Allow changes between different types of function symbol. */
1295 if (newfunc && oldfunc)
1296 *type_change_ok = TRUE;
1298 /* It's OK to change the type if either the existing symbol or the
1299 new symbol is weak. A type change is also OK if the old symbol
1300 is undefined and the new symbol is defined. */
1305 && h->root.type == bfd_link_hash_undefined))
1306 *type_change_ok = TRUE;
1308 /* It's OK to change the size if either the existing symbol or the
1309 new symbol is weak, or if the old symbol is undefined. */
1312 || h->root.type == bfd_link_hash_undefined)
1313 *size_change_ok = TRUE;
1315 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
1316 symbol, respectively, appears to be a common symbol in a dynamic
1317 object. If a symbol appears in an uninitialized section, and is
1318 not weak, and is not a function, then it may be a common symbol
1319 which was resolved when the dynamic object was created. We want
1320 to treat such symbols specially, because they raise special
1321 considerations when setting the symbol size: if the symbol
1322 appears as a common symbol in a regular object, and the size in
1323 the regular object is larger, we must make sure that we use the
1324 larger size. This problematic case can always be avoided in C,
1325 but it must be handled correctly when using Fortran shared
1328 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
1329 likewise for OLDDYNCOMMON and OLDDEF.
1331 Note that this test is just a heuristic, and that it is quite
1332 possible to have an uninitialized symbol in a shared object which
1333 is really a definition, rather than a common symbol. This could
1334 lead to some minor confusion when the symbol really is a common
1335 symbol in some regular object. However, I think it will be
1341 && (sec->flags & SEC_ALLOC) != 0
1342 && (sec->flags & SEC_LOAD) == 0
1345 newdyncommon = TRUE;
1347 newdyncommon = FALSE;
1351 && h->root.type == bfd_link_hash_defined
1353 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1354 && (h->root.u.def.section->flags & SEC_LOAD) == 0
1357 olddyncommon = TRUE;
1359 olddyncommon = FALSE;
1361 /* We now know everything about the old and new symbols. We ask the
1362 backend to check if we can merge them. */
1363 if (bed->merge_symbol != NULL)
1365 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
1370 /* If both the old and the new symbols look like common symbols in a
1371 dynamic object, set the size of the symbol to the larger of the
1376 && sym->st_size != h->size)
1378 /* Since we think we have two common symbols, issue a multiple
1379 common warning if desired. Note that we only warn if the
1380 size is different. If the size is the same, we simply let
1381 the old symbol override the new one as normally happens with
1382 symbols defined in dynamic objects. */
1384 if (! ((*info->callbacks->multiple_common)
1385 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1388 if (sym->st_size > h->size)
1389 h->size = sym->st_size;
1391 *size_change_ok = TRUE;
1394 /* If we are looking at a dynamic object, and we have found a
1395 definition, we need to see if the symbol was already defined by
1396 some other object. If so, we want to use the existing
1397 definition, and we do not want to report a multiple symbol
1398 definition error; we do this by clobbering *PSEC to be
1399 bfd_und_section_ptr.
1401 We treat a common symbol as a definition if the symbol in the
1402 shared library is a function, since common symbols always
1403 represent variables; this can cause confusion in principle, but
1404 any such confusion would seem to indicate an erroneous program or
1405 shared library. We also permit a common symbol in a regular
1406 object to override a weak symbol in a shared object. */
1411 || (h->root.type == bfd_link_hash_common
1412 && (newweak || newfunc))))
1416 newdyncommon = FALSE;
1418 *psec = sec = bfd_und_section_ptr;
1419 *size_change_ok = TRUE;
1421 /* If we get here when the old symbol is a common symbol, then
1422 we are explicitly letting it override a weak symbol or
1423 function in a dynamic object, and we don't want to warn about
1424 a type change. If the old symbol is a defined symbol, a type
1425 change warning may still be appropriate. */
1427 if (h->root.type == bfd_link_hash_common)
1428 *type_change_ok = TRUE;
1431 /* Handle the special case of an old common symbol merging with a
1432 new symbol which looks like a common symbol in a shared object.
1433 We change *PSEC and *PVALUE to make the new symbol look like a
1434 common symbol, and let _bfd_generic_link_add_one_symbol do the
1438 && h->root.type == bfd_link_hash_common)
1442 newdyncommon = FALSE;
1443 *pvalue = sym->st_size;
1444 *psec = sec = bed->common_section (oldsec);
1445 *size_change_ok = TRUE;
1448 /* Skip weak definitions of symbols that are already defined. */
1449 if (newdef && olddef && newweak)
1451 /* Don't skip new non-IR weak syms. */
1452 if (!(oldbfd != NULL
1453 && (oldbfd->flags & BFD_PLUGIN) != 0
1454 && (abfd->flags & BFD_PLUGIN) == 0))
1460 /* Merge st_other. If the symbol already has a dynamic index,
1461 but visibility says it should not be visible, turn it into a
1463 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
1464 if (h->dynindx != -1)
1465 switch (ELF_ST_VISIBILITY (h->other))
1469 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
1474 /* If the old symbol is from a dynamic object, and the new symbol is
1475 a definition which is not from a dynamic object, then the new
1476 symbol overrides the old symbol. Symbols from regular files
1477 always take precedence over symbols from dynamic objects, even if
1478 they are defined after the dynamic object in the link.
1480 As above, we again permit a common symbol in a regular object to
1481 override a definition in a shared object if the shared object
1482 symbol is a function or is weak. */
1487 || (bfd_is_com_section (sec)
1488 && (oldweak || oldfunc)))
1493 /* Change the hash table entry to undefined, and let
1494 _bfd_generic_link_add_one_symbol do the right thing with the
1497 h->root.type = bfd_link_hash_undefined;
1498 h->root.u.undef.abfd = h->root.u.def.section->owner;
1499 *size_change_ok = TRUE;
1502 olddyncommon = FALSE;
1504 /* We again permit a type change when a common symbol may be
1505 overriding a function. */
1507 if (bfd_is_com_section (sec))
1511 /* If a common symbol overrides a function, make sure
1512 that it isn't defined dynamically nor has type
1515 h->type = STT_NOTYPE;
1517 *type_change_ok = TRUE;
1520 if (hi->root.type == bfd_link_hash_indirect)
1523 /* This union may have been set to be non-NULL when this symbol
1524 was seen in a dynamic object. We must force the union to be
1525 NULL, so that it is correct for a regular symbol. */
1526 h->verinfo.vertree = NULL;
1529 /* Handle the special case of a new common symbol merging with an
1530 old symbol that looks like it might be a common symbol defined in
1531 a shared object. Note that we have already handled the case in
1532 which a new common symbol should simply override the definition
1533 in the shared library. */
1536 && bfd_is_com_section (sec)
1539 /* It would be best if we could set the hash table entry to a
1540 common symbol, but we don't know what to use for the section
1541 or the alignment. */
1542 if (! ((*info->callbacks->multiple_common)
1543 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size)))
1546 /* If the presumed common symbol in the dynamic object is
1547 larger, pretend that the new symbol has its size. */
1549 if (h->size > *pvalue)
1552 /* We need to remember the alignment required by the symbol
1553 in the dynamic object. */
1554 BFD_ASSERT (pold_alignment);
1555 *pold_alignment = h->root.u.def.section->alignment_power;
1558 olddyncommon = FALSE;
1560 h->root.type = bfd_link_hash_undefined;
1561 h->root.u.undef.abfd = h->root.u.def.section->owner;
1563 *size_change_ok = TRUE;
1564 *type_change_ok = TRUE;
1566 if (hi->root.type == bfd_link_hash_indirect)
1569 h->verinfo.vertree = NULL;
1574 /* Handle the case where we had a versioned symbol in a dynamic
1575 library and now find a definition in a normal object. In this
1576 case, we make the versioned symbol point to the normal one. */
1577 flip->root.type = h->root.type;
1578 flip->root.u.undef.abfd = h->root.u.undef.abfd;
1579 h->root.type = bfd_link_hash_indirect;
1580 h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
1581 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
1585 flip->ref_dynamic = 1;
1592 /* This function is called to create an indirect symbol from the
1593 default for the symbol with the default version if needed. The
1594 symbol is described by H, NAME, SYM, SEC, and VALUE. We
1595 set DYNSYM if the new indirect symbol is dynamic. */
1598 _bfd_elf_add_default_symbol (bfd *abfd,
1599 struct bfd_link_info *info,
1600 struct elf_link_hash_entry *h,
1602 Elf_Internal_Sym *sym,
1606 bfd_boolean *dynsym)
1608 bfd_boolean type_change_ok;
1609 bfd_boolean size_change_ok;
1612 struct elf_link_hash_entry *hi;
1613 struct bfd_link_hash_entry *bh;
1614 const struct elf_backend_data *bed;
1615 bfd_boolean collect;
1616 bfd_boolean dynamic;
1617 bfd_boolean override;
1619 size_t len, shortlen;
1622 /* If this symbol has a version, and it is the default version, we
1623 create an indirect symbol from the default name to the fully
1624 decorated name. This will cause external references which do not
1625 specify a version to be bound to this version of the symbol. */
1626 p = strchr (name, ELF_VER_CHR);
1627 if (p == NULL || p[1] != ELF_VER_CHR)
1630 bed = get_elf_backend_data (abfd);
1631 collect = bed->collect;
1632 dynamic = (abfd->flags & DYNAMIC) != 0;
1634 shortlen = p - name;
1635 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
1636 if (shortname == NULL)
1638 memcpy (shortname, name, shortlen);
1639 shortname[shortlen] = '\0';
1641 /* We are going to create a new symbol. Merge it with any existing
1642 symbol with this name. For the purposes of the merge, act as
1643 though we were defining the symbol we just defined, although we
1644 actually going to define an indirect symbol. */
1645 type_change_ok = FALSE;
1646 size_change_ok = FALSE;
1648 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1649 &hi, poldbfd, NULL, NULL, &skip, &override,
1650 &type_change_ok, &size_change_ok))
1658 /* Add the default symbol if not performing a relocatable link. */
1659 if (! info->relocatable)
1662 if (! (_bfd_generic_link_add_one_symbol
1663 (info, abfd, shortname, BSF_INDIRECT,
1664 bfd_ind_section_ptr,
1665 0, name, FALSE, collect, &bh)))
1667 hi = (struct elf_link_hash_entry *) bh;
1672 /* In this case the symbol named SHORTNAME is overriding the
1673 indirect symbol we want to add. We were planning on making
1674 SHORTNAME an indirect symbol referring to NAME. SHORTNAME
1675 is the name without a version. NAME is the fully versioned
1676 name, and it is the default version.
1678 Overriding means that we already saw a definition for the
1679 symbol SHORTNAME in a regular object, and it is overriding
1680 the symbol defined in the dynamic object.
1682 When this happens, we actually want to change NAME, the
1683 symbol we just added, to refer to SHORTNAME. This will cause
1684 references to NAME in the shared object to become references
1685 to SHORTNAME in the regular object. This is what we expect
1686 when we override a function in a shared object: that the
1687 references in the shared object will be mapped to the
1688 definition in the regular object. */
1690 while (hi->root.type == bfd_link_hash_indirect
1691 || hi->root.type == bfd_link_hash_warning)
1692 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1694 h->root.type = bfd_link_hash_indirect;
1695 h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
1699 hi->ref_dynamic = 1;
1703 if (! bfd_elf_link_record_dynamic_symbol (info, hi))
1708 /* Now set HI to H, so that the following code will set the
1709 other fields correctly. */
1713 /* Check if HI is a warning symbol. */
1714 if (hi->root.type == bfd_link_hash_warning)
1715 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
1717 /* If there is a duplicate definition somewhere, then HI may not
1718 point to an indirect symbol. We will have reported an error to
1719 the user in that case. */
1721 if (hi->root.type == bfd_link_hash_indirect)
1723 struct elf_link_hash_entry *ht;
1725 ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
1726 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
1728 /* A reference to the SHORTNAME symbol from a dynamic library
1729 will be satisfied by the versioned symbol at runtime. In
1730 effect, we have a reference to the versioned symbol. */
1731 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1732 hi->dynamic_def |= ht->dynamic_def;
1734 /* See if the new flags lead us to realize that the symbol must
1740 if (! info->executable
1747 if (hi->ref_regular)
1753 /* We also need to define an indirection from the nondefault version
1757 len = strlen (name);
1758 shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
1759 if (shortname == NULL)
1761 memcpy (shortname, name, shortlen);
1762 memcpy (shortname + shortlen, p + 1, len - shortlen);
1764 /* Once again, merge with any existing symbol. */
1765 type_change_ok = FALSE;
1766 size_change_ok = FALSE;
1768 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
1769 &hi, poldbfd, NULL, NULL, &skip, &override,
1770 &type_change_ok, &size_change_ok))
1778 /* Here SHORTNAME is a versioned name, so we don't expect to see
1779 the type of override we do in the case above unless it is
1780 overridden by a versioned definition. */
1781 if (hi->root.type != bfd_link_hash_defined
1782 && hi->root.type != bfd_link_hash_defweak)
1783 (*_bfd_error_handler)
1784 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"),
1790 if (! (_bfd_generic_link_add_one_symbol
1791 (info, abfd, shortname, BSF_INDIRECT,
1792 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh)))
1794 hi = (struct elf_link_hash_entry *) bh;
1796 /* If there is a duplicate definition somewhere, then HI may not
1797 point to an indirect symbol. We will have reported an error
1798 to the user in that case. */
1800 if (hi->root.type == bfd_link_hash_indirect)
1802 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
1803 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
1804 hi->dynamic_def |= h->dynamic_def;
1806 /* See if the new flags lead us to realize that the symbol
1812 if (! info->executable
1818 if (hi->ref_regular)
1828 /* This routine is used to export all defined symbols into the dynamic
1829 symbol table. It is called via elf_link_hash_traverse. */
1832 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
1834 struct elf_info_failed *eif = (struct elf_info_failed *) data;
1836 /* Ignore indirect symbols. These are added by the versioning code. */
1837 if (h->root.type == bfd_link_hash_indirect)
1840 /* Ignore this if we won't export it. */
1841 if (!eif->info->export_dynamic && !h->dynamic)
1844 if (h->dynindx == -1
1845 && (h->def_regular || h->ref_regular)
1846 && ! bfd_hide_sym_by_version (eif->info->version_info,
1847 h->root.root.string))
1849 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
1859 /* Look through the symbols which are defined in other shared
1860 libraries and referenced here. Update the list of version
1861 dependencies. This will be put into the .gnu.version_r section.
1862 This function is called via elf_link_hash_traverse. */
1865 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
1868 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
1869 Elf_Internal_Verneed *t;
1870 Elf_Internal_Vernaux *a;
1873 /* We only care about symbols defined in shared objects with version
1878 || h->verinfo.verdef == NULL
1879 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
1880 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
1883 /* See if we already know about this version. */
1884 for (t = elf_tdata (rinfo->info->output_bfd)->verref;
1888 if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
1891 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1892 if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
1898 /* This is a new version. Add it to tree we are building. */
1903 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
1906 rinfo->failed = TRUE;
1910 t->vn_bfd = h->verinfo.verdef->vd_bfd;
1911 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
1912 elf_tdata (rinfo->info->output_bfd)->verref = t;
1916 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
1919 rinfo->failed = TRUE;
1923 /* Note that we are copying a string pointer here, and testing it
1924 above. If bfd_elf_string_from_elf_section is ever changed to
1925 discard the string data when low in memory, this will have to be
1927 a->vna_nodename = h->verinfo.verdef->vd_nodename;
1929 a->vna_flags = h->verinfo.verdef->vd_flags;
1930 a->vna_nextptr = t->vn_auxptr;
1932 h->verinfo.verdef->vd_exp_refno = rinfo->vers;
1935 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
1942 /* Figure out appropriate versions for all the symbols. We may not
1943 have the version number script until we have read all of the input
1944 files, so until that point we don't know which symbols should be
1945 local. This function is called via elf_link_hash_traverse. */
1948 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
1950 struct elf_info_failed *sinfo;
1951 struct bfd_link_info *info;
1952 const struct elf_backend_data *bed;
1953 struct elf_info_failed eif;
1957 sinfo = (struct elf_info_failed *) data;
1960 /* Fix the symbol flags. */
1963 if (! _bfd_elf_fix_symbol_flags (h, &eif))
1966 sinfo->failed = TRUE;
1970 /* We only need version numbers for symbols defined in regular
1972 if (!h->def_regular)
1975 bed = get_elf_backend_data (info->output_bfd);
1976 p = strchr (h->root.root.string, ELF_VER_CHR);
1977 if (p != NULL && h->verinfo.vertree == NULL)
1979 struct bfd_elf_version_tree *t;
1984 /* There are two consecutive ELF_VER_CHR characters if this is
1985 not a hidden symbol. */
1987 if (*p == ELF_VER_CHR)
1993 /* If there is no version string, we can just return out. */
2001 /* Look for the version. If we find it, it is no longer weak. */
2002 for (t = sinfo->info->version_info; t != NULL; t = t->next)
2004 if (strcmp (t->name, p) == 0)
2008 struct bfd_elf_version_expr *d;
2010 len = p - h->root.root.string;
2011 alc = (char *) bfd_malloc (len);
2014 sinfo->failed = TRUE;
2017 memcpy (alc, h->root.root.string, len - 1);
2018 alc[len - 1] = '\0';
2019 if (alc[len - 2] == ELF_VER_CHR)
2020 alc[len - 2] = '\0';
2022 h->verinfo.vertree = t;
2026 if (t->globals.list != NULL)
2027 d = (*t->match) (&t->globals, NULL, alc);
2029 /* See if there is anything to force this symbol to
2031 if (d == NULL && t->locals.list != NULL)
2033 d = (*t->match) (&t->locals, NULL, alc);
2036 && ! info->export_dynamic)
2037 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2045 /* If we are building an application, we need to create a
2046 version node for this version. */
2047 if (t == NULL && info->executable)
2049 struct bfd_elf_version_tree **pp;
2052 /* If we aren't going to export this symbol, we don't need
2053 to worry about it. */
2054 if (h->dynindx == -1)
2058 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt);
2061 sinfo->failed = TRUE;
2066 t->name_indx = (unsigned int) -1;
2070 /* Don't count anonymous version tag. */
2071 if (sinfo->info->version_info != NULL
2072 && sinfo->info->version_info->vernum == 0)
2074 for (pp = &sinfo->info->version_info;
2078 t->vernum = version_index;
2082 h->verinfo.vertree = t;
2086 /* We could not find the version for a symbol when
2087 generating a shared archive. Return an error. */
2088 (*_bfd_error_handler)
2089 (_("%B: version node not found for symbol %s"),
2090 info->output_bfd, h->root.root.string);
2091 bfd_set_error (bfd_error_bad_value);
2092 sinfo->failed = TRUE;
2100 /* If we don't have a version for this symbol, see if we can find
2102 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL)
2107 = bfd_find_version_for_sym (sinfo->info->version_info,
2108 h->root.root.string, &hide);
2109 if (h->verinfo.vertree != NULL && hide)
2110 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
2116 /* Read and swap the relocs from the section indicated by SHDR. This
2117 may be either a REL or a RELA section. The relocations are
2118 translated into RELA relocations and stored in INTERNAL_RELOCS,
2119 which should have already been allocated to contain enough space.
2120 The EXTERNAL_RELOCS are a buffer where the external form of the
2121 relocations should be stored.
2123 Returns FALSE if something goes wrong. */
2126 elf_link_read_relocs_from_section (bfd *abfd,
2128 Elf_Internal_Shdr *shdr,
2129 void *external_relocs,
2130 Elf_Internal_Rela *internal_relocs)
2132 const struct elf_backend_data *bed;
2133 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
2134 const bfd_byte *erela;
2135 const bfd_byte *erelaend;
2136 Elf_Internal_Rela *irela;
2137 Elf_Internal_Shdr *symtab_hdr;
2140 /* Position ourselves at the start of the section. */
2141 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
2144 /* Read the relocations. */
2145 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
2148 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2149 nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
2151 bed = get_elf_backend_data (abfd);
2153 /* Convert the external relocations to the internal format. */
2154 if (shdr->sh_entsize == bed->s->sizeof_rel)
2155 swap_in = bed->s->swap_reloc_in;
2156 else if (shdr->sh_entsize == bed->s->sizeof_rela)
2157 swap_in = bed->s->swap_reloca_in;
2160 bfd_set_error (bfd_error_wrong_format);
2164 erela = (const bfd_byte *) external_relocs;
2165 erelaend = erela + shdr->sh_size;
2166 irela = internal_relocs;
2167 while (erela < erelaend)
2171 (*swap_in) (abfd, erela, irela);
2172 r_symndx = ELF32_R_SYM (irela->r_info);
2173 if (bed->s->arch_size == 64)
2177 if ((size_t) r_symndx >= nsyms)
2179 (*_bfd_error_handler)
2180 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)"
2181 " for offset 0x%lx in section `%A'"),
2183 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2184 bfd_set_error (bfd_error_bad_value);
2188 else if (r_symndx != STN_UNDEF)
2190 (*_bfd_error_handler)
2191 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'"
2192 " when the object file has no symbol table"),
2194 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset);
2195 bfd_set_error (bfd_error_bad_value);
2198 irela += bed->s->int_rels_per_ext_rel;
2199 erela += shdr->sh_entsize;
2205 /* Read and swap the relocs for a section O. They may have been
2206 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
2207 not NULL, they are used as buffers to read into. They are known to
2208 be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
2209 the return value is allocated using either malloc or bfd_alloc,
2210 according to the KEEP_MEMORY argument. If O has two relocation
2211 sections (both REL and RELA relocations), then the REL_HDR
2212 relocations will appear first in INTERNAL_RELOCS, followed by the
2213 RELA_HDR relocations. */
2216 _bfd_elf_link_read_relocs (bfd *abfd,
2218 void *external_relocs,
2219 Elf_Internal_Rela *internal_relocs,
2220 bfd_boolean keep_memory)
2222 void *alloc1 = NULL;
2223 Elf_Internal_Rela *alloc2 = NULL;
2224 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2225 struct bfd_elf_section_data *esdo = elf_section_data (o);
2226 Elf_Internal_Rela *internal_rela_relocs;
2228 if (esdo->relocs != NULL)
2229 return esdo->relocs;
2231 if (o->reloc_count == 0)
2234 if (internal_relocs == NULL)
2238 size = o->reloc_count;
2239 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela);
2241 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
2243 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
2244 if (internal_relocs == NULL)
2248 if (external_relocs == NULL)
2250 bfd_size_type size = 0;
2253 size += esdo->rel.hdr->sh_size;
2255 size += esdo->rela.hdr->sh_size;
2257 alloc1 = bfd_malloc (size);
2260 external_relocs = alloc1;
2263 internal_rela_relocs = internal_relocs;
2266 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
2270 external_relocs = (((bfd_byte *) external_relocs)
2271 + esdo->rel.hdr->sh_size);
2272 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
2273 * bed->s->int_rels_per_ext_rel);
2277 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
2279 internal_rela_relocs)))
2282 /* Cache the results for next time, if we can. */
2284 esdo->relocs = internal_relocs;
2289 /* Don't free alloc2, since if it was allocated we are passing it
2290 back (under the name of internal_relocs). */
2292 return internal_relocs;
2300 bfd_release (abfd, alloc2);
2307 /* Compute the size of, and allocate space for, REL_HDR which is the
2308 section header for a section containing relocations for O. */
2311 _bfd_elf_link_size_reloc_section (bfd *abfd,
2312 struct bfd_elf_section_reloc_data *reldata)
2314 Elf_Internal_Shdr *rel_hdr = reldata->hdr;
2316 /* That allows us to calculate the size of the section. */
2317 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
2319 /* The contents field must last into write_object_contents, so we
2320 allocate it with bfd_alloc rather than malloc. Also since we
2321 cannot be sure that the contents will actually be filled in,
2322 we zero the allocated space. */
2323 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
2324 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
2327 if (reldata->hashes == NULL && reldata->count)
2329 struct elf_link_hash_entry **p;
2331 p = ((struct elf_link_hash_entry **)
2332 bfd_zmalloc (reldata->count * sizeof (*p)));
2336 reldata->hashes = p;
2342 /* Copy the relocations indicated by the INTERNAL_RELOCS (which
2343 originated from the section given by INPUT_REL_HDR) to the
2347 _bfd_elf_link_output_relocs (bfd *output_bfd,
2348 asection *input_section,
2349 Elf_Internal_Shdr *input_rel_hdr,
2350 Elf_Internal_Rela *internal_relocs,
2351 struct elf_link_hash_entry **rel_hash
2354 Elf_Internal_Rela *irela;
2355 Elf_Internal_Rela *irelaend;
2357 struct bfd_elf_section_reloc_data *output_reldata;
2358 asection *output_section;
2359 const struct elf_backend_data *bed;
2360 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
2361 struct bfd_elf_section_data *esdo;
2363 output_section = input_section->output_section;
2365 bed = get_elf_backend_data (output_bfd);
2366 esdo = elf_section_data (output_section);
2367 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2369 output_reldata = &esdo->rel;
2370 swap_out = bed->s->swap_reloc_out;
2372 else if (esdo->rela.hdr
2373 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
2375 output_reldata = &esdo->rela;
2376 swap_out = bed->s->swap_reloca_out;
2380 (*_bfd_error_handler)
2381 (_("%B: relocation size mismatch in %B section %A"),
2382 output_bfd, input_section->owner, input_section);
2383 bfd_set_error (bfd_error_wrong_format);
2387 erel = output_reldata->hdr->contents;
2388 erel += output_reldata->count * input_rel_hdr->sh_entsize;
2389 irela = internal_relocs;
2390 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
2391 * bed->s->int_rels_per_ext_rel);
2392 while (irela < irelaend)
2394 (*swap_out) (output_bfd, irela, erel);
2395 irela += bed->s->int_rels_per_ext_rel;
2396 erel += input_rel_hdr->sh_entsize;
2399 /* Bump the counter, so that we know where to add the next set of
2401 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
2406 /* Make weak undefined symbols in PIE dynamic. */
2409 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
2410 struct elf_link_hash_entry *h)
2414 && h->root.type == bfd_link_hash_undefweak)
2415 return bfd_elf_link_record_dynamic_symbol (info, h);
2420 /* Fix up the flags for a symbol. This handles various cases which
2421 can only be fixed after all the input files are seen. This is
2422 currently called by both adjust_dynamic_symbol and
2423 assign_sym_version, which is unnecessary but perhaps more robust in
2424 the face of future changes. */
2427 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
2428 struct elf_info_failed *eif)
2430 const struct elf_backend_data *bed;
2432 /* If this symbol was mentioned in a non-ELF file, try to set
2433 DEF_REGULAR and REF_REGULAR correctly. This is the only way to
2434 permit a non-ELF file to correctly refer to a symbol defined in
2435 an ELF dynamic object. */
2438 while (h->root.type == bfd_link_hash_indirect)
2439 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2441 if (h->root.type != bfd_link_hash_defined
2442 && h->root.type != bfd_link_hash_defweak)
2445 h->ref_regular_nonweak = 1;
2449 if (h->root.u.def.section->owner != NULL
2450 && (bfd_get_flavour (h->root.u.def.section->owner)
2451 == bfd_target_elf_flavour))
2454 h->ref_regular_nonweak = 1;
2460 if (h->dynindx == -1
2464 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
2473 /* Unfortunately, NON_ELF is only correct if the symbol
2474 was first seen in a non-ELF file. Fortunately, if the symbol
2475 was first seen in an ELF file, we're probably OK unless the
2476 symbol was defined in a non-ELF file. Catch that case here.
2477 FIXME: We're still in trouble if the symbol was first seen in
2478 a dynamic object, and then later in a non-ELF regular object. */
2479 if ((h->root.type == bfd_link_hash_defined
2480 || h->root.type == bfd_link_hash_defweak)
2482 && (h->root.u.def.section->owner != NULL
2483 ? (bfd_get_flavour (h->root.u.def.section->owner)
2484 != bfd_target_elf_flavour)
2485 : (bfd_is_abs_section (h->root.u.def.section)
2486 && !h->def_dynamic)))
2490 /* Backend specific symbol fixup. */
2491 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
2492 if (bed->elf_backend_fixup_symbol
2493 && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
2496 /* If this is a final link, and the symbol was defined as a common
2497 symbol in a regular object file, and there was no definition in
2498 any dynamic object, then the linker will have allocated space for
2499 the symbol in a common section but the DEF_REGULAR
2500 flag will not have been set. */
2501 if (h->root.type == bfd_link_hash_defined
2505 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
2508 /* If -Bsymbolic was used (which means to bind references to global
2509 symbols to the definition within the shared object), and this
2510 symbol was defined in a regular object, then it actually doesn't
2511 need a PLT entry. Likewise, if the symbol has non-default
2512 visibility. If the symbol has hidden or internal visibility, we
2513 will force it local. */
2515 && eif->info->shared
2516 && is_elf_hash_table (eif->info->hash)
2517 && (SYMBOLIC_BIND (eif->info, h)
2518 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2521 bfd_boolean force_local;
2523 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
2524 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
2525 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
2528 /* If a weak undefined symbol has non-default visibility, we also
2529 hide it from the dynamic linker. */
2530 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2531 && h->root.type == bfd_link_hash_undefweak)
2532 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE);
2534 /* If this is a weak defined symbol in a dynamic object, and we know
2535 the real definition in the dynamic object, copy interesting flags
2536 over to the real definition. */
2537 if (h->u.weakdef != NULL)
2539 /* If the real definition is defined by a regular object file,
2540 don't do anything special. See the longer description in
2541 _bfd_elf_adjust_dynamic_symbol, below. */
2542 if (h->u.weakdef->def_regular)
2543 h->u.weakdef = NULL;
2546 struct elf_link_hash_entry *weakdef = h->u.weakdef;
2548 while (h->root.type == bfd_link_hash_indirect)
2549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2551 BFD_ASSERT (h->root.type == bfd_link_hash_defined
2552 || h->root.type == bfd_link_hash_defweak);
2553 BFD_ASSERT (weakdef->def_dynamic);
2554 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined
2555 || weakdef->root.type == bfd_link_hash_defweak);
2556 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h);
2563 /* Make the backend pick a good value for a dynamic symbol. This is
2564 called via elf_link_hash_traverse, and also calls itself
2568 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
2570 struct elf_info_failed *eif = (struct elf_info_failed *) data;
2572 const struct elf_backend_data *bed;
2574 if (! is_elf_hash_table (eif->info->hash))
2577 /* Ignore indirect symbols. These are added by the versioning code. */
2578 if (h->root.type == bfd_link_hash_indirect)
2581 /* Fix the symbol flags. */
2582 if (! _bfd_elf_fix_symbol_flags (h, eif))
2585 /* If this symbol does not require a PLT entry, and it is not
2586 defined by a dynamic object, or is not referenced by a regular
2587 object, ignore it. We do have to handle a weak defined symbol,
2588 even if no regular object refers to it, if we decided to add it
2589 to the dynamic symbol table. FIXME: Do we normally need to worry
2590 about symbols which are defined by one dynamic object and
2591 referenced by another one? */
2593 && h->type != STT_GNU_IFUNC
2597 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1))))
2599 h->plt = elf_hash_table (eif->info)->init_plt_offset;
2603 /* If we've already adjusted this symbol, don't do it again. This
2604 can happen via a recursive call. */
2605 if (h->dynamic_adjusted)
2608 /* Don't look at this symbol again. Note that we must set this
2609 after checking the above conditions, because we may look at a
2610 symbol once, decide not to do anything, and then get called
2611 recursively later after REF_REGULAR is set below. */
2612 h->dynamic_adjusted = 1;
2614 /* If this is a weak definition, and we know a real definition, and
2615 the real symbol is not itself defined by a regular object file,
2616 then get a good value for the real definition. We handle the
2617 real symbol first, for the convenience of the backend routine.
2619 Note that there is a confusing case here. If the real definition
2620 is defined by a regular object file, we don't get the real symbol
2621 from the dynamic object, but we do get the weak symbol. If the
2622 processor backend uses a COPY reloc, then if some routine in the
2623 dynamic object changes the real symbol, we will not see that
2624 change in the corresponding weak symbol. This is the way other
2625 ELF linkers work as well, and seems to be a result of the shared
2628 I will clarify this issue. Most SVR4 shared libraries define the
2629 variable _timezone and define timezone as a weak synonym. The
2630 tzset call changes _timezone. If you write
2631 extern int timezone;
2633 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
2634 you might expect that, since timezone is a synonym for _timezone,
2635 the same number will print both times. However, if the processor
2636 backend uses a COPY reloc, then actually timezone will be copied
2637 into your process image, and, since you define _timezone
2638 yourself, _timezone will not. Thus timezone and _timezone will
2639 wind up at different memory locations. The tzset call will set
2640 _timezone, leaving timezone unchanged. */
2642 if (h->u.weakdef != NULL)
2644 /* If we get to this point, there is an implicit reference to
2645 H->U.WEAKDEF by a regular object file via the weak symbol H. */
2646 h->u.weakdef->ref_regular = 1;
2648 /* Ensure that the backend adjust_dynamic_symbol function sees
2649 H->U.WEAKDEF before H by recursively calling ourselves. */
2650 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif))
2654 /* If a symbol has no type and no size and does not require a PLT
2655 entry, then we are probably about to do the wrong thing here: we
2656 are probably going to create a COPY reloc for an empty object.
2657 This case can arise when a shared object is built with assembly
2658 code, and the assembly code fails to set the symbol type. */
2660 && h->type == STT_NOTYPE
2662 (*_bfd_error_handler)
2663 (_("warning: type and size of dynamic symbol `%s' are not defined"),
2664 h->root.root.string);
2666 dynobj = elf_hash_table (eif->info)->dynobj;
2667 bed = get_elf_backend_data (dynobj);
2669 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
2678 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
2682 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
2683 struct elf_link_hash_entry *h,
2686 unsigned int power_of_two;
2688 asection *sec = h->root.u.def.section;
2690 /* The section aligment of definition is the maximum alignment
2691 requirement of symbols defined in the section. Since we don't
2692 know the symbol alignment requirement, we start with the
2693 maximum alignment and check low bits of the symbol address
2694 for the minimum alignment. */
2695 power_of_two = bfd_get_section_alignment (sec->owner, sec);
2696 mask = ((bfd_vma) 1 << power_of_two) - 1;
2697 while ((h->root.u.def.value & mask) != 0)
2703 if (power_of_two > bfd_get_section_alignment (dynbss->owner,
2706 /* Adjust the section alignment if needed. */
2707 if (! bfd_set_section_alignment (dynbss->owner, dynbss,
2712 /* We make sure that the symbol will be aligned properly. */
2713 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
2715 /* Define the symbol as being at this point in DYNBSS. */
2716 h->root.u.def.section = dynbss;
2717 h->root.u.def.value = dynbss->size;
2719 /* Increment the size of DYNBSS to make room for the symbol. */
2720 dynbss->size += h->size;
2722 /* No error if extern_protected_data is true. */
2723 if (h->protected_def
2724 && (!info->extern_protected_data
2725 || (info->extern_protected_data < 0
2726 && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
2727 info->callbacks->einfo
2728 (_("%P: copy reloc against protected `%T' is dangerous\n"),
2729 h->root.root.string);
2734 /* Adjust all external symbols pointing into SEC_MERGE sections
2735 to reflect the object merging within the sections. */
2738 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
2742 if ((h->root.type == bfd_link_hash_defined
2743 || h->root.type == bfd_link_hash_defweak)
2744 && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
2745 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
2747 bfd *output_bfd = (bfd *) data;
2749 h->root.u.def.value =
2750 _bfd_merged_section_offset (output_bfd,
2751 &h->root.u.def.section,
2752 elf_section_data (sec)->sec_info,
2753 h->root.u.def.value);
2759 /* Returns false if the symbol referred to by H should be considered
2760 to resolve local to the current module, and true if it should be
2761 considered to bind dynamically. */
2764 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
2765 struct bfd_link_info *info,
2766 bfd_boolean not_local_protected)
2768 bfd_boolean binding_stays_local_p;
2769 const struct elf_backend_data *bed;
2770 struct elf_link_hash_table *hash_table;
2775 while (h->root.type == bfd_link_hash_indirect
2776 || h->root.type == bfd_link_hash_warning)
2777 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2779 /* If it was forced local, then clearly it's not dynamic. */
2780 if (h->dynindx == -1)
2782 if (h->forced_local)
2785 /* Identify the cases where name binding rules say that a
2786 visible symbol resolves locally. */
2787 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h);
2789 switch (ELF_ST_VISIBILITY (h->other))
2796 hash_table = elf_hash_table (info);
2797 if (!is_elf_hash_table (hash_table))
2800 bed = get_elf_backend_data (hash_table->dynobj);
2802 /* Proper resolution for function pointer equality may require
2803 that these symbols perhaps be resolved dynamically, even though
2804 we should be resolving them to the current module. */
2805 if (!not_local_protected || !bed->is_function_type (h->type))
2806 binding_stays_local_p = TRUE;
2813 /* If it isn't defined locally, then clearly it's dynamic. */
2814 if (!h->def_regular && !ELF_COMMON_DEF_P (h))
2817 /* Otherwise, the symbol is dynamic if binding rules don't tell
2818 us that it remains local. */
2819 return !binding_stays_local_p;
2822 /* Return true if the symbol referred to by H should be considered
2823 to resolve local to the current module, and false otherwise. Differs
2824 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
2825 undefined symbols. The two functions are virtually identical except
2826 for the place where forced_local and dynindx == -1 are tested. If
2827 either of those tests are true, _bfd_elf_dynamic_symbol_p will say
2828 the symbol is local, while _bfd_elf_symbol_refs_local_p will say
2829 the symbol is local only for defined symbols.
2830 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
2831 !_bfd_elf_symbol_refs_local_p, except that targets differ in their
2832 treatment of undefined weak symbols. For those that do not make
2833 undefined weak symbols dynamic, both functions may return false. */
2836 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
2837 struct bfd_link_info *info,
2838 bfd_boolean local_protected)
2840 const struct elf_backend_data *bed;
2841 struct elf_link_hash_table *hash_table;
2843 /* If it's a local sym, of course we resolve locally. */
2847 /* STV_HIDDEN or STV_INTERNAL ones must be local. */
2848 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
2849 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
2852 /* Common symbols that become definitions don't get the DEF_REGULAR
2853 flag set, so test it first, and don't bail out. */
2854 if (ELF_COMMON_DEF_P (h))
2856 /* If we don't have a definition in a regular file, then we can't
2857 resolve locally. The sym is either undefined or dynamic. */
2858 else if (!h->def_regular)
2861 /* Forced local symbols resolve locally. */
2862 if (h->forced_local)
2865 /* As do non-dynamic symbols. */
2866 if (h->dynindx == -1)
2869 /* At this point, we know the symbol is defined and dynamic. In an
2870 executable it must resolve locally, likewise when building symbolic
2871 shared libraries. */
2872 if (info->executable || SYMBOLIC_BIND (info, h))
2875 /* Now deal with defined dynamic symbols in shared libraries. Ones
2876 with default visibility might not resolve locally. */
2877 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2880 hash_table = elf_hash_table (info);
2881 if (!is_elf_hash_table (hash_table))
2884 bed = get_elf_backend_data (hash_table->dynobj);
2886 /* If extern_protected_data is false, STV_PROTECTED non-function
2887 symbols are local. */
2888 if ((!info->extern_protected_data
2889 || (info->extern_protected_data < 0
2890 && !bed->extern_protected_data))
2891 && !bed->is_function_type (h->type))
2894 /* Function pointer equality tests may require that STV_PROTECTED
2895 symbols be treated as dynamic symbols. If the address of a
2896 function not defined in an executable is set to that function's
2897 plt entry in the executable, then the address of the function in
2898 a shared library must also be the plt entry in the executable. */
2899 return local_protected;
2902 /* Caches some TLS segment info, and ensures that the TLS segment vma is
2903 aligned. Returns the first TLS output section. */
2905 struct bfd_section *
2906 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
2908 struct bfd_section *sec, *tls;
2909 unsigned int align = 0;
2911 for (sec = obfd->sections; sec != NULL; sec = sec->next)
2912 if ((sec->flags & SEC_THREAD_LOCAL) != 0)
2916 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
2917 if (sec->alignment_power > align)
2918 align = sec->alignment_power;
2920 elf_hash_table (info)->tls_sec = tls;
2922 /* Ensure the alignment of the first section is the largest alignment,
2923 so that the tls segment starts aligned. */
2925 tls->alignment_power = align;
2930 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
2932 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
2933 Elf_Internal_Sym *sym)
2935 const struct elf_backend_data *bed;
2937 /* Local symbols do not count, but target specific ones might. */
2938 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
2939 && ELF_ST_BIND (sym->st_info) < STB_LOOS)
2942 bed = get_elf_backend_data (abfd);
2943 /* Function symbols do not count. */
2944 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
2947 /* If the section is undefined, then so is the symbol. */
2948 if (sym->st_shndx == SHN_UNDEF)
2951 /* If the symbol is defined in the common section, then
2952 it is a common definition and so does not count. */
2953 if (bed->common_definition (sym))
2956 /* If the symbol is in a target specific section then we
2957 must rely upon the backend to tell us what it is. */
2958 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
2959 /* FIXME - this function is not coded yet:
2961 return _bfd_is_global_symbol_definition (abfd, sym);
2963 Instead for now assume that the definition is not global,
2964 Even if this is wrong, at least the linker will behave
2965 in the same way that it used to do. */
2971 /* Search the symbol table of the archive element of the archive ABFD
2972 whose archive map contains a mention of SYMDEF, and determine if
2973 the symbol is defined in this element. */
2975 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
2977 Elf_Internal_Shdr * hdr;
2978 bfd_size_type symcount;
2979 bfd_size_type extsymcount;
2980 bfd_size_type extsymoff;
2981 Elf_Internal_Sym *isymbuf;
2982 Elf_Internal_Sym *isym;
2983 Elf_Internal_Sym *isymend;
2986 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
2990 /* Return FALSE if the object has been claimed by plugin. */
2991 if (abfd->plugin_format == bfd_plugin_yes)
2994 if (! bfd_check_format (abfd, bfd_object))
2997 /* Select the appropriate symbol table. */
2998 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
2999 hdr = &elf_tdata (abfd)->symtab_hdr;
3001 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3003 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3005 /* The sh_info field of the symtab header tells us where the
3006 external symbols start. We don't care about the local symbols. */
3007 if (elf_bad_symtab (abfd))
3009 extsymcount = symcount;
3014 extsymcount = symcount - hdr->sh_info;
3015 extsymoff = hdr->sh_info;
3018 if (extsymcount == 0)
3021 /* Read in the symbol table. */
3022 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3024 if (isymbuf == NULL)
3027 /* Scan the symbol table looking for SYMDEF. */
3029 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
3033 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3038 if (strcmp (name, symdef->name) == 0)
3040 result = is_global_data_symbol_definition (abfd, isym);
3050 /* Add an entry to the .dynamic table. */
3053 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
3057 struct elf_link_hash_table *hash_table;
3058 const struct elf_backend_data *bed;
3060 bfd_size_type newsize;
3061 bfd_byte *newcontents;
3062 Elf_Internal_Dyn dyn;
3064 hash_table = elf_hash_table (info);
3065 if (! is_elf_hash_table (hash_table))
3068 bed = get_elf_backend_data (hash_table->dynobj);
3069 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3070 BFD_ASSERT (s != NULL);
3072 newsize = s->size + bed->s->sizeof_dyn;
3073 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
3074 if (newcontents == NULL)
3078 dyn.d_un.d_val = val;
3079 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
3082 s->contents = newcontents;
3087 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true,
3088 otherwise just check whether one already exists. Returns -1 on error,
3089 1 if a DT_NEEDED tag already exists, and 0 on success. */
3092 elf_add_dt_needed_tag (bfd *abfd,
3093 struct bfd_link_info *info,
3097 struct elf_link_hash_table *hash_table;
3098 bfd_size_type strindex;
3100 if (!_bfd_elf_link_create_dynstrtab (abfd, info))
3103 hash_table = elf_hash_table (info);
3104 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE);
3105 if (strindex == (bfd_size_type) -1)
3108 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
3111 const struct elf_backend_data *bed;
3114 bed = get_elf_backend_data (hash_table->dynobj);
3115 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
3117 for (extdyn = sdyn->contents;
3118 extdyn < sdyn->contents + sdyn->size;
3119 extdyn += bed->s->sizeof_dyn)
3121 Elf_Internal_Dyn dyn;
3123 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
3124 if (dyn.d_tag == DT_NEEDED
3125 && dyn.d_un.d_val == strindex)
3127 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3135 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
3138 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
3142 /* We were just checking for existence of the tag. */
3143 _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
3149 on_needed_list (const char *soname, struct bfd_link_needed_list *needed)
3151 for (; needed != NULL; needed = needed->next)
3152 if ((elf_dyn_lib_class (needed->by) & DYN_AS_NEEDED) == 0
3153 && strcmp (soname, needed->name) == 0)
3159 /* Sort symbol by value, section, and size. */
3161 elf_sort_symbol (const void *arg1, const void *arg2)
3163 const struct elf_link_hash_entry *h1;
3164 const struct elf_link_hash_entry *h2;
3165 bfd_signed_vma vdiff;
3167 h1 = *(const struct elf_link_hash_entry **) arg1;
3168 h2 = *(const struct elf_link_hash_entry **) arg2;
3169 vdiff = h1->root.u.def.value - h2->root.u.def.value;
3171 return vdiff > 0 ? 1 : -1;
3174 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
3176 return sdiff > 0 ? 1 : -1;
3178 vdiff = h1->size - h2->size;
3179 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1;
3182 /* This function is used to adjust offsets into .dynstr for
3183 dynamic symbols. This is called via elf_link_hash_traverse. */
3186 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
3188 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
3190 if (h->dynindx != -1)
3191 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
3195 /* Assign string offsets in .dynstr, update all structures referencing
3199 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
3201 struct elf_link_hash_table *hash_table = elf_hash_table (info);
3202 struct elf_link_local_dynamic_entry *entry;
3203 struct elf_strtab_hash *dynstr = hash_table->dynstr;
3204 bfd *dynobj = hash_table->dynobj;
3207 const struct elf_backend_data *bed;
3210 _bfd_elf_strtab_finalize (dynstr);
3211 size = _bfd_elf_strtab_size (dynstr);
3213 bed = get_elf_backend_data (dynobj);
3214 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3215 BFD_ASSERT (sdyn != NULL);
3217 /* Update all .dynamic entries referencing .dynstr strings. */
3218 for (extdyn = sdyn->contents;
3219 extdyn < sdyn->contents + sdyn->size;
3220 extdyn += bed->s->sizeof_dyn)
3222 Elf_Internal_Dyn dyn;
3224 bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
3228 dyn.d_un.d_val = size;
3238 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
3243 bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
3246 /* Now update local dynamic symbols. */
3247 for (entry = hash_table->dynlocal; entry ; entry = entry->next)
3248 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
3249 entry->isym.st_name);
3251 /* And the rest of dynamic symbols. */
3252 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
3254 /* Adjust version definitions. */
3255 if (elf_tdata (output_bfd)->cverdefs)
3260 Elf_Internal_Verdef def;
3261 Elf_Internal_Verdaux defaux;
3263 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
3267 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
3269 p += sizeof (Elf_External_Verdef);
3270 if (def.vd_aux != sizeof (Elf_External_Verdef))
3272 for (i = 0; i < def.vd_cnt; ++i)
3274 _bfd_elf_swap_verdaux_in (output_bfd,
3275 (Elf_External_Verdaux *) p, &defaux);
3276 defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
3278 _bfd_elf_swap_verdaux_out (output_bfd,
3279 &defaux, (Elf_External_Verdaux *) p);
3280 p += sizeof (Elf_External_Verdaux);
3283 while (def.vd_next);
3286 /* Adjust version references. */
3287 if (elf_tdata (output_bfd)->verref)
3292 Elf_Internal_Verneed need;
3293 Elf_Internal_Vernaux needaux;
3295 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
3299 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
3301 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
3302 _bfd_elf_swap_verneed_out (output_bfd, &need,
3303 (Elf_External_Verneed *) p);
3304 p += sizeof (Elf_External_Verneed);
3305 for (i = 0; i < need.vn_cnt; ++i)
3307 _bfd_elf_swap_vernaux_in (output_bfd,
3308 (Elf_External_Vernaux *) p, &needaux);
3309 needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
3311 _bfd_elf_swap_vernaux_out (output_bfd,
3313 (Elf_External_Vernaux *) p);
3314 p += sizeof (Elf_External_Vernaux);
3317 while (need.vn_next);
3323 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3324 The default is to only match when the INPUT and OUTPUT are exactly
3328 _bfd_elf_default_relocs_compatible (const bfd_target *input,
3329 const bfd_target *output)
3331 return input == output;
3334 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
3335 This version is used when different targets for the same architecture
3336 are virtually identical. */
3339 _bfd_elf_relocs_compatible (const bfd_target *input,
3340 const bfd_target *output)
3342 const struct elf_backend_data *obed, *ibed;
3344 if (input == output)
3347 ibed = xvec_get_elf_backend_data (input);
3348 obed = xvec_get_elf_backend_data (output);
3350 if (ibed->arch != obed->arch)
3353 /* If both backends are using this function, deem them compatible. */
3354 return ibed->relocs_compatible == obed->relocs_compatible;
3357 /* Make a special call to the linker "notice" function to tell it that
3358 we are about to handle an as-needed lib, or have finished
3359 processing the lib. */
3362 _bfd_elf_notice_as_needed (bfd *ibfd,
3363 struct bfd_link_info *info,
3364 enum notice_asneeded_action act)
3366 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
3369 /* Add symbols from an ELF object file to the linker hash table. */
3372 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3374 Elf_Internal_Ehdr *ehdr;
3375 Elf_Internal_Shdr *hdr;
3376 bfd_size_type symcount;
3377 bfd_size_type extsymcount;
3378 bfd_size_type extsymoff;
3379 struct elf_link_hash_entry **sym_hash;
3380 bfd_boolean dynamic;
3381 Elf_External_Versym *extversym = NULL;
3382 Elf_External_Versym *ever;
3383 struct elf_link_hash_entry *weaks;
3384 struct elf_link_hash_entry **nondeflt_vers = NULL;
3385 bfd_size_type nondeflt_vers_cnt = 0;
3386 Elf_Internal_Sym *isymbuf = NULL;
3387 Elf_Internal_Sym *isym;
3388 Elf_Internal_Sym *isymend;
3389 const struct elf_backend_data *bed;
3390 bfd_boolean add_needed;
3391 struct elf_link_hash_table *htab;
3393 void *alloc_mark = NULL;
3394 struct bfd_hash_entry **old_table = NULL;
3395 unsigned int old_size = 0;
3396 unsigned int old_count = 0;
3397 void *old_tab = NULL;
3399 struct bfd_link_hash_entry *old_undefs = NULL;
3400 struct bfd_link_hash_entry *old_undefs_tail = NULL;
3401 long old_dynsymcount = 0;
3402 bfd_size_type old_dynstr_size = 0;
3405 bfd_boolean just_syms;
3407 htab = elf_hash_table (info);
3408 bed = get_elf_backend_data (abfd);
3410 if ((abfd->flags & DYNAMIC) == 0)
3416 /* You can't use -r against a dynamic object. Also, there's no
3417 hope of using a dynamic object which does not exactly match
3418 the format of the output file. */
3419 if (info->relocatable
3420 || !is_elf_hash_table (htab)
3421 || info->output_bfd->xvec != abfd->xvec)
3423 if (info->relocatable)
3424 bfd_set_error (bfd_error_invalid_operation);
3426 bfd_set_error (bfd_error_wrong_format);
3431 ehdr = elf_elfheader (abfd);
3432 if (info->warn_alternate_em
3433 && bed->elf_machine_code != ehdr->e_machine
3434 && ((bed->elf_machine_alt1 != 0
3435 && ehdr->e_machine == bed->elf_machine_alt1)
3436 || (bed->elf_machine_alt2 != 0
3437 && ehdr->e_machine == bed->elf_machine_alt2)))
3438 info->callbacks->einfo
3439 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"),
3440 ehdr->e_machine, abfd, bed->elf_machine_code);
3442 /* As a GNU extension, any input sections which are named
3443 .gnu.warning.SYMBOL are treated as warning symbols for the given
3444 symbol. This differs from .gnu.warning sections, which generate
3445 warnings when they are included in an output file. */
3446 /* PR 12761: Also generate this warning when building shared libraries. */
3447 for (s = abfd->sections; s != NULL; s = s->next)
3451 name = bfd_get_section_name (abfd, s);
3452 if (CONST_STRNEQ (name, ".gnu.warning."))
3457 name += sizeof ".gnu.warning." - 1;
3459 /* If this is a shared object, then look up the symbol
3460 in the hash table. If it is there, and it is already
3461 been defined, then we will not be using the entry
3462 from this shared object, so we don't need to warn.
3463 FIXME: If we see the definition in a regular object
3464 later on, we will warn, but we shouldn't. The only
3465 fix is to keep track of what warnings we are supposed
3466 to emit, and then handle them all at the end of the
3470 struct elf_link_hash_entry *h;
3472 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE);
3474 /* FIXME: What about bfd_link_hash_common? */
3476 && (h->root.type == bfd_link_hash_defined
3477 || h->root.type == bfd_link_hash_defweak))
3482 msg = (char *) bfd_alloc (abfd, sz + 1);
3486 if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
3491 if (! (_bfd_generic_link_add_one_symbol
3492 (info, abfd, name, BSF_WARNING, s, 0, msg,
3493 FALSE, bed->collect, NULL)))
3496 if (info->executable)
3498 /* Clobber the section size so that the warning does
3499 not get copied into the output file. */
3502 /* Also set SEC_EXCLUDE, so that symbols defined in
3503 the warning section don't get copied to the output. */
3504 s->flags |= SEC_EXCLUDE;
3509 just_syms = ((s = abfd->sections) != NULL
3510 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
3515 /* If we are creating a shared library, create all the dynamic
3516 sections immediately. We need to attach them to something,
3517 so we attach them to this BFD, provided it is the right
3518 format and is not from ld --just-symbols. FIXME: If there
3519 are no input BFD's of the same format as the output, we can't
3520 make a shared library. */
3523 && is_elf_hash_table (htab)
3524 && info->output_bfd->xvec == abfd->xvec
3525 && !htab->dynamic_sections_created)
3527 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
3531 else if (!is_elf_hash_table (htab))
3535 const char *soname = NULL;
3537 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
3540 /* ld --just-symbols and dynamic objects don't mix very well.
3541 ld shouldn't allow it. */
3545 /* If this dynamic lib was specified on the command line with
3546 --as-needed in effect, then we don't want to add a DT_NEEDED
3547 tag unless the lib is actually used. Similary for libs brought
3548 in by another lib's DT_NEEDED. When --no-add-needed is used
3549 on a dynamic lib, we don't want to add a DT_NEEDED entry for
3550 any dynamic library in DT_NEEDED tags in the dynamic lib at
3552 add_needed = (elf_dyn_lib_class (abfd)
3553 & (DYN_AS_NEEDED | DYN_DT_NEEDED
3554 | DYN_NO_NEEDED)) == 0;
3556 s = bfd_get_section_by_name (abfd, ".dynamic");
3561 unsigned int elfsec;
3562 unsigned long shlink;
3564 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
3571 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
3572 if (elfsec == SHN_BAD)
3573 goto error_free_dyn;
3574 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
3576 for (extdyn = dynbuf;
3577 extdyn < dynbuf + s->size;
3578 extdyn += bed->s->sizeof_dyn)
3580 Elf_Internal_Dyn dyn;
3582 bed->s->swap_dyn_in (abfd, extdyn, &dyn);
3583 if (dyn.d_tag == DT_SONAME)
3585 unsigned int tagv = dyn.d_un.d_val;
3586 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3588 goto error_free_dyn;
3590 if (dyn.d_tag == DT_NEEDED)
3592 struct bfd_link_needed_list *n, **pn;
3594 unsigned int tagv = dyn.d_un.d_val;
3596 amt = sizeof (struct bfd_link_needed_list);
3597 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3598 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3599 if (n == NULL || fnm == NULL)
3600 goto error_free_dyn;
3601 amt = strlen (fnm) + 1;
3602 anm = (char *) bfd_alloc (abfd, amt);
3604 goto error_free_dyn;
3605 memcpy (anm, fnm, amt);
3609 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
3613 if (dyn.d_tag == DT_RUNPATH)
3615 struct bfd_link_needed_list *n, **pn;
3617 unsigned int tagv = dyn.d_un.d_val;
3619 amt = sizeof (struct bfd_link_needed_list);
3620 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3621 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3622 if (n == NULL || fnm == NULL)
3623 goto error_free_dyn;
3624 amt = strlen (fnm) + 1;
3625 anm = (char *) bfd_alloc (abfd, amt);
3627 goto error_free_dyn;
3628 memcpy (anm, fnm, amt);
3632 for (pn = & runpath;
3638 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
3639 if (!runpath && dyn.d_tag == DT_RPATH)
3641 struct bfd_link_needed_list *n, **pn;
3643 unsigned int tagv = dyn.d_un.d_val;
3645 amt = sizeof (struct bfd_link_needed_list);
3646 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
3647 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3648 if (n == NULL || fnm == NULL)
3649 goto error_free_dyn;
3650 amt = strlen (fnm) + 1;
3651 anm = (char *) bfd_alloc (abfd, amt);
3653 goto error_free_dyn;
3654 memcpy (anm, fnm, amt);
3664 if (dyn.d_tag == DT_AUDIT)
3666 unsigned int tagv = dyn.d_un.d_val;
3667 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
3674 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
3675 frees all more recently bfd_alloc'd blocks as well. */
3681 struct bfd_link_needed_list **pn;
3682 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
3687 /* We do not want to include any of the sections in a dynamic
3688 object in the output file. We hack by simply clobbering the
3689 list of sections in the BFD. This could be handled more
3690 cleanly by, say, a new section flag; the existing
3691 SEC_NEVER_LOAD flag is not the one we want, because that one
3692 still implies that the section takes up space in the output
3694 bfd_section_list_clear (abfd);
3696 /* Find the name to use in a DT_NEEDED entry that refers to this
3697 object. If the object has a DT_SONAME entry, we use it.
3698 Otherwise, if the generic linker stuck something in
3699 elf_dt_name, we use that. Otherwise, we just use the file
3701 if (soname == NULL || *soname == '\0')
3703 soname = elf_dt_name (abfd);
3704 if (soname == NULL || *soname == '\0')
3705 soname = bfd_get_filename (abfd);
3708 /* Save the SONAME because sometimes the linker emulation code
3709 will need to know it. */
3710 elf_dt_name (abfd) = soname;
3712 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
3716 /* If we have already included this dynamic object in the
3717 link, just ignore it. There is no reason to include a
3718 particular dynamic object more than once. */
3722 /* Save the DT_AUDIT entry for the linker emulation code. */
3723 elf_dt_audit (abfd) = audit;
3726 /* If this is a dynamic object, we always link against the .dynsym
3727 symbol table, not the .symtab symbol table. The dynamic linker
3728 will only see the .dynsym symbol table, so there is no reason to
3729 look at .symtab for a dynamic object. */
3731 if (! dynamic || elf_dynsymtab (abfd) == 0)
3732 hdr = &elf_tdata (abfd)->symtab_hdr;
3734 hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3736 symcount = hdr->sh_size / bed->s->sizeof_sym;
3738 /* The sh_info field of the symtab header tells us where the
3739 external symbols start. We don't care about the local symbols at
3741 if (elf_bad_symtab (abfd))
3743 extsymcount = symcount;
3748 extsymcount = symcount - hdr->sh_info;
3749 extsymoff = hdr->sh_info;
3752 sym_hash = elf_sym_hashes (abfd);
3753 if (extsymcount != 0)
3755 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
3757 if (isymbuf == NULL)
3760 if (sym_hash == NULL)
3762 /* We store a pointer to the hash table entry for each
3764 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
3765 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
3766 if (sym_hash == NULL)
3767 goto error_free_sym;
3768 elf_sym_hashes (abfd) = sym_hash;
3774 /* Read in any version definitions. */
3775 if (!_bfd_elf_slurp_version_tables (abfd,
3776 info->default_imported_symver))
3777 goto error_free_sym;
3779 /* Read in the symbol versions, but don't bother to convert them
3780 to internal format. */
3781 if (elf_dynversym (abfd) != 0)
3783 Elf_Internal_Shdr *versymhdr;
3785 versymhdr = &elf_tdata (abfd)->dynversym_hdr;
3786 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
3787 if (extversym == NULL)
3788 goto error_free_sym;
3789 amt = versymhdr->sh_size;
3790 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
3791 || bfd_bread (extversym, amt, abfd) != amt)
3792 goto error_free_vers;
3796 /* If we are loading an as-needed shared lib, save the symbol table
3797 state before we start adding symbols. If the lib turns out
3798 to be unneeded, restore the state. */
3799 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
3804 for (entsize = 0, i = 0; i < htab->root.table.size; i++)
3806 struct bfd_hash_entry *p;
3807 struct elf_link_hash_entry *h;
3809 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3811 h = (struct elf_link_hash_entry *) p;
3812 entsize += htab->root.table.entsize;
3813 if (h->root.type == bfd_link_hash_warning)
3814 entsize += htab->root.table.entsize;
3818 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
3819 old_tab = bfd_malloc (tabsize + entsize);
3820 if (old_tab == NULL)
3821 goto error_free_vers;
3823 /* Remember the current objalloc pointer, so that all mem for
3824 symbols added can later be reclaimed. */
3825 alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
3826 if (alloc_mark == NULL)
3827 goto error_free_vers;
3829 /* Make a special call to the linker "notice" function to
3830 tell it that we are about to handle an as-needed lib. */
3831 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
3832 goto error_free_vers;
3834 /* Clone the symbol table. Remember some pointers into the
3835 symbol table, and dynamic symbol count. */
3836 old_ent = (char *) old_tab + tabsize;
3837 memcpy (old_tab, htab->root.table.table, tabsize);
3838 old_undefs = htab->root.undefs;
3839 old_undefs_tail = htab->root.undefs_tail;
3840 old_table = htab->root.table.table;
3841 old_size = htab->root.table.size;
3842 old_count = htab->root.table.count;
3843 old_dynsymcount = htab->dynsymcount;
3844 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr);
3846 for (i = 0; i < htab->root.table.size; i++)
3848 struct bfd_hash_entry *p;
3849 struct elf_link_hash_entry *h;
3851 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
3853 memcpy (old_ent, p, htab->root.table.entsize);
3854 old_ent = (char *) old_ent + htab->root.table.entsize;
3855 h = (struct elf_link_hash_entry *) p;
3856 if (h->root.type == bfd_link_hash_warning)
3858 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize);
3859 old_ent = (char *) old_ent + htab->root.table.entsize;
3866 ever = extversym != NULL ? extversym + extsymoff : NULL;
3867 for (isym = isymbuf, isymend = isymbuf + extsymcount;
3869 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
3873 asection *sec, *new_sec;
3876 struct elf_link_hash_entry *h;
3877 struct elf_link_hash_entry *hi;
3878 bfd_boolean definition;
3879 bfd_boolean size_change_ok;
3880 bfd_boolean type_change_ok;
3881 bfd_boolean new_weakdef;
3882 bfd_boolean new_weak;
3883 bfd_boolean old_weak;
3884 bfd_boolean override;
3886 unsigned int old_alignment;
3891 flags = BSF_NO_FLAGS;
3893 value = isym->st_value;
3894 common = bed->common_definition (isym);
3896 bind = ELF_ST_BIND (isym->st_info);
3900 /* This should be impossible, since ELF requires that all
3901 global symbols follow all local symbols, and that sh_info
3902 point to the first global symbol. Unfortunately, Irix 5
3907 if (isym->st_shndx != SHN_UNDEF && !common)
3915 case STB_GNU_UNIQUE:
3916 flags = BSF_GNU_UNIQUE;
3920 /* Leave it up to the processor backend. */
3924 if (isym->st_shndx == SHN_UNDEF)
3925 sec = bfd_und_section_ptr;
3926 else if (isym->st_shndx == SHN_ABS)
3927 sec = bfd_abs_section_ptr;
3928 else if (isym->st_shndx == SHN_COMMON)
3930 sec = bfd_com_section_ptr;
3931 /* What ELF calls the size we call the value. What ELF
3932 calls the value we call the alignment. */
3933 value = isym->st_size;
3937 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3939 sec = bfd_abs_section_ptr;
3940 else if (discarded_section (sec))
3942 /* Symbols from discarded section are undefined. We keep
3944 sec = bfd_und_section_ptr;
3945 isym->st_shndx = SHN_UNDEF;
3947 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
3951 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3954 goto error_free_vers;
3956 if (isym->st_shndx == SHN_COMMON
3957 && (abfd->flags & BFD_PLUGIN) != 0)
3959 asection *xc = bfd_get_section_by_name (abfd, "COMMON");
3963 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
3965 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
3967 goto error_free_vers;
3971 else if (isym->st_shndx == SHN_COMMON
3972 && ELF_ST_TYPE (isym->st_info) == STT_TLS
3973 && !info->relocatable)
3975 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
3979 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
3980 | SEC_LINKER_CREATED);
3981 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
3983 goto error_free_vers;
3987 else if (bed->elf_add_symbol_hook)
3989 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
3991 goto error_free_vers;
3993 /* The hook function sets the name to NULL if this symbol
3994 should be skipped for some reason. */
3999 /* Sanity check that all possibilities were handled. */
4002 bfd_set_error (bfd_error_bad_value);
4003 goto error_free_vers;
4006 /* Silently discard TLS symbols from --just-syms. There's
4007 no way to combine a static TLS block with a new TLS block
4008 for this executable. */
4009 if (ELF_ST_TYPE (isym->st_info) == STT_TLS
4010 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4013 if (bfd_is_und_section (sec)
4014 || bfd_is_com_section (sec))
4019 size_change_ok = FALSE;
4020 type_change_ok = bed->type_change_ok;
4026 if (is_elf_hash_table (htab))
4028 Elf_Internal_Versym iver;
4029 unsigned int vernum = 0;
4034 if (info->default_imported_symver)
4035 /* Use the default symbol version created earlier. */
4036 iver.vs_vers = elf_tdata (abfd)->cverdefs;
4041 _bfd_elf_swap_versym_in (abfd, ever, &iver);
4043 vernum = iver.vs_vers & VERSYM_VERSION;
4045 /* If this is a hidden symbol, or if it is not version
4046 1, we append the version name to the symbol name.
4047 However, we do not modify a non-hidden absolute symbol
4048 if it is not a function, because it might be the version
4049 symbol itself. FIXME: What if it isn't? */
4050 if ((iver.vs_vers & VERSYM_HIDDEN) != 0
4052 && (!bfd_is_abs_section (sec)
4053 || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
4056 size_t namelen, verlen, newlen;
4059 if (isym->st_shndx != SHN_UNDEF)
4061 if (vernum > elf_tdata (abfd)->cverdefs)
4063 else if (vernum > 1)
4065 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
4071 (*_bfd_error_handler)
4072 (_("%B: %s: invalid version %u (max %d)"),
4074 elf_tdata (abfd)->cverdefs);
4075 bfd_set_error (bfd_error_bad_value);
4076 goto error_free_vers;
4081 /* We cannot simply test for the number of
4082 entries in the VERNEED section since the
4083 numbers for the needed versions do not start
4085 Elf_Internal_Verneed *t;
4088 for (t = elf_tdata (abfd)->verref;
4092 Elf_Internal_Vernaux *a;
4094 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
4096 if (a->vna_other == vernum)
4098 verstr = a->vna_nodename;
4107 (*_bfd_error_handler)
4108 (_("%B: %s: invalid needed version %d"),
4109 abfd, name, vernum);
4110 bfd_set_error (bfd_error_bad_value);
4111 goto error_free_vers;
4115 namelen = strlen (name);
4116 verlen = strlen (verstr);
4117 newlen = namelen + verlen + 2;
4118 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4119 && isym->st_shndx != SHN_UNDEF)
4122 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
4123 if (newname == NULL)
4124 goto error_free_vers;
4125 memcpy (newname, name, namelen);
4126 p = newname + namelen;
4128 /* If this is a defined non-hidden version symbol,
4129 we add another @ to the name. This indicates the
4130 default version of the symbol. */
4131 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
4132 && isym->st_shndx != SHN_UNDEF)
4134 memcpy (p, verstr, verlen + 1);
4139 /* If this symbol has default visibility and the user has
4140 requested we not re-export it, then mark it as hidden. */
4144 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
4145 isym->st_other = (STV_HIDDEN
4146 | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
4148 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
4149 sym_hash, &old_bfd, &old_weak,
4150 &old_alignment, &skip, &override,
4151 &type_change_ok, &size_change_ok))
4152 goto error_free_vers;
4161 while (h->root.type == bfd_link_hash_indirect
4162 || h->root.type == bfd_link_hash_warning)
4163 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4165 if (elf_tdata (abfd)->verdef != NULL
4168 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
4171 if (! (_bfd_generic_link_add_one_symbol
4172 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect,
4173 (struct bfd_link_hash_entry **) sym_hash)))
4174 goto error_free_vers;
4177 /* We need to make sure that indirect symbol dynamic flags are
4180 while (h->root.type == bfd_link_hash_indirect
4181 || h->root.type == bfd_link_hash_warning)
4182 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4186 new_weak = (flags & BSF_WEAK) != 0;
4187 new_weakdef = FALSE;
4191 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
4192 && is_elf_hash_table (htab)
4193 && h->u.weakdef == NULL)
4195 /* Keep a list of all weak defined non function symbols from
4196 a dynamic object, using the weakdef field. Later in this
4197 function we will set the weakdef field to the correct
4198 value. We only put non-function symbols from dynamic
4199 objects on this list, because that happens to be the only
4200 time we need to know the normal symbol corresponding to a
4201 weak symbol, and the information is time consuming to
4202 figure out. If the weakdef field is not already NULL,
4203 then this symbol was already defined by some previous
4204 dynamic object, and we will be using that previous
4205 definition anyhow. */
4207 h->u.weakdef = weaks;
4212 /* Set the alignment of a common symbol. */
4213 if ((common || bfd_is_com_section (sec))
4214 && h->root.type == bfd_link_hash_common)
4219 align = bfd_log2 (isym->st_value);
4222 /* The new symbol is a common symbol in a shared object.
4223 We need to get the alignment from the section. */
4224 align = new_sec->alignment_power;
4226 if (align > old_alignment)
4227 h->root.u.c.p->alignment_power = align;
4229 h->root.u.c.p->alignment_power = old_alignment;
4232 if (is_elf_hash_table (htab))
4234 /* Set a flag in the hash table entry indicating the type of
4235 reference or definition we just found. A dynamic symbol
4236 is one which is referenced or defined by both a regular
4237 object and a shared object. */
4238 bfd_boolean dynsym = FALSE;
4240 /* Plugin symbols aren't normal. Don't set def_regular or
4241 ref_regular for them, or make them dynamic. */
4242 if ((abfd->flags & BFD_PLUGIN) != 0)
4249 if (bind != STB_WEAK)
4250 h->ref_regular_nonweak = 1;
4262 /* If the indirect symbol has been forced local, don't
4263 make the real symbol dynamic. */
4264 if ((h == hi || !hi->forced_local)
4265 && ((! info->executable && ! info->relocatable)
4275 hi->ref_dynamic = 1;
4280 hi->def_dynamic = 1;
4283 /* If the indirect symbol has been forced local, don't
4284 make the real symbol dynamic. */
4285 if ((h == hi || !hi->forced_local)
4288 || (h->u.weakdef != NULL
4290 && h->u.weakdef->dynindx != -1)))
4294 /* Check to see if we need to add an indirect symbol for
4295 the default name. */
4297 || (!override && h->root.type == bfd_link_hash_common))
4298 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
4299 sec, value, &old_bfd, &dynsym))
4300 goto error_free_vers;
4302 /* Check the alignment when a common symbol is involved. This
4303 can change when a common symbol is overridden by a normal
4304 definition or a common symbol is ignored due to the old
4305 normal definition. We need to make sure the maximum
4306 alignment is maintained. */
4307 if ((old_alignment || common)
4308 && h->root.type != bfd_link_hash_common)
4310 unsigned int common_align;
4311 unsigned int normal_align;
4312 unsigned int symbol_align;
4316 BFD_ASSERT (h->root.type == bfd_link_hash_defined
4317 || h->root.type == bfd_link_hash_defweak);
4319 symbol_align = ffs (h->root.u.def.value) - 1;
4320 if (h->root.u.def.section->owner != NULL
4321 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0)
4323 normal_align = h->root.u.def.section->alignment_power;
4324 if (normal_align > symbol_align)
4325 normal_align = symbol_align;
4328 normal_align = symbol_align;
4332 common_align = old_alignment;
4333 common_bfd = old_bfd;
4338 common_align = bfd_log2 (isym->st_value);
4340 normal_bfd = old_bfd;
4343 if (normal_align < common_align)
4345 /* PR binutils/2735 */
4346 if (normal_bfd == NULL)
4347 (*_bfd_error_handler)
4348 (_("Warning: alignment %u of common symbol `%s' in %B is"
4349 " greater than the alignment (%u) of its section %A"),
4350 common_bfd, h->root.u.def.section,
4351 1 << common_align, name, 1 << normal_align);
4353 (*_bfd_error_handler)
4354 (_("Warning: alignment %u of symbol `%s' in %B"
4355 " is smaller than %u in %B"),
4356 normal_bfd, common_bfd,
4357 1 << normal_align, name, 1 << common_align);
4361 /* Remember the symbol size if it isn't undefined. */
4362 if (isym->st_size != 0
4363 && isym->st_shndx != SHN_UNDEF
4364 && (definition || h->size == 0))
4367 && h->size != isym->st_size
4368 && ! size_change_ok)
4369 (*_bfd_error_handler)
4370 (_("Warning: size of symbol `%s' changed"
4371 " from %lu in %B to %lu in %B"),
4373 name, (unsigned long) h->size,
4374 (unsigned long) isym->st_size);
4376 h->size = isym->st_size;
4379 /* If this is a common symbol, then we always want H->SIZE
4380 to be the size of the common symbol. The code just above
4381 won't fix the size if a common symbol becomes larger. We
4382 don't warn about a size change here, because that is
4383 covered by --warn-common. Allow changes between different
4385 if (h->root.type == bfd_link_hash_common)
4386 h->size = h->root.u.c.size;
4388 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
4389 && ((definition && !new_weak)
4390 || (old_weak && h->root.type == bfd_link_hash_common)
4391 || h->type == STT_NOTYPE))
4393 unsigned int type = ELF_ST_TYPE (isym->st_info);
4395 /* Turn an IFUNC symbol from a DSO into a normal FUNC
4397 if (type == STT_GNU_IFUNC
4398 && (abfd->flags & DYNAMIC) != 0)
4401 if (h->type != type)
4403 if (h->type != STT_NOTYPE && ! type_change_ok)
4404 (*_bfd_error_handler)
4405 (_("Warning: type of symbol `%s' changed"
4406 " from %d to %d in %B"),
4407 abfd, name, h->type, type);
4413 /* Merge st_other field. */
4414 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
4416 /* We don't want to make debug symbol dynamic. */
4417 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
4420 /* Nor should we make plugin symbols dynamic. */
4421 if ((abfd->flags & BFD_PLUGIN) != 0)
4426 h->target_internal = isym->st_target_internal;
4427 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
4430 if (definition && !dynamic)
4432 char *p = strchr (name, ELF_VER_CHR);
4433 if (p != NULL && p[1] != ELF_VER_CHR)
4435 /* Queue non-default versions so that .symver x, x@FOO
4436 aliases can be checked. */
4439 amt = ((isymend - isym + 1)
4440 * sizeof (struct elf_link_hash_entry *));
4442 = (struct elf_link_hash_entry **) bfd_malloc (amt);
4444 goto error_free_vers;
4446 nondeflt_vers[nondeflt_vers_cnt++] = h;
4450 if (dynsym && h->dynindx == -1)
4452 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4453 goto error_free_vers;
4454 if (h->u.weakdef != NULL
4456 && h->u.weakdef->dynindx == -1)
4458 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef))
4459 goto error_free_vers;
4462 else if (dynsym && h->dynindx != -1)
4463 /* If the symbol already has a dynamic index, but
4464 visibility says it should not be visible, turn it into
4466 switch (ELF_ST_VISIBILITY (h->other))
4470 (*bed->elf_backend_hide_symbol) (info, h, TRUE);
4475 /* Don't add DT_NEEDED for references from the dummy bfd. */
4479 && h->ref_regular_nonweak
4481 || (old_bfd->flags & BFD_PLUGIN) == 0))
4482 || (h->ref_dynamic_nonweak
4483 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
4484 && !on_needed_list (elf_dt_name (abfd), htab->needed))))
4487 const char *soname = elf_dt_name (abfd);
4489 info->callbacks->minfo ("%!", soname, old_bfd,
4490 h->root.root.string);
4492 /* A symbol from a library loaded via DT_NEEDED of some
4493 other library is referenced by a regular object.
4494 Add a DT_NEEDED entry for it. Issue an error if
4495 --no-add-needed is used and the reference was not
4498 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
4500 (*_bfd_error_handler)
4501 (_("%B: undefined reference to symbol '%s'"),
4503 bfd_set_error (bfd_error_missing_dso);
4504 goto error_free_vers;
4507 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
4508 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
4511 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed);
4513 goto error_free_vers;
4515 BFD_ASSERT (ret == 0);
4520 if (extversym != NULL)
4526 if (isymbuf != NULL)
4532 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
4536 /* Restore the symbol table. */
4537 old_ent = (char *) old_tab + tabsize;
4538 memset (elf_sym_hashes (abfd), 0,
4539 extsymcount * sizeof (struct elf_link_hash_entry *));
4540 htab->root.table.table = old_table;
4541 htab->root.table.size = old_size;
4542 htab->root.table.count = old_count;
4543 memcpy (htab->root.table.table, old_tab, tabsize);
4544 htab->root.undefs = old_undefs;
4545 htab->root.undefs_tail = old_undefs_tail;
4546 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size);
4547 for (i = 0; i < htab->root.table.size; i++)
4549 struct bfd_hash_entry *p;
4550 struct elf_link_hash_entry *h;
4552 unsigned int alignment_power;
4554 for (p = htab->root.table.table[i]; p != NULL; p = p->next)
4556 h = (struct elf_link_hash_entry *) p;
4557 if (h->root.type == bfd_link_hash_warning)
4558 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4559 if (h->dynindx >= old_dynsymcount
4560 && h->dynstr_index < old_dynstr_size)
4561 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index);
4563 /* Preserve the maximum alignment and size for common
4564 symbols even if this dynamic lib isn't on DT_NEEDED
4565 since it can still be loaded at run time by another
4567 if (h->root.type == bfd_link_hash_common)
4569 size = h->root.u.c.size;
4570 alignment_power = h->root.u.c.p->alignment_power;
4575 alignment_power = 0;
4577 memcpy (p, old_ent, htab->root.table.entsize);
4578 old_ent = (char *) old_ent + htab->root.table.entsize;
4579 h = (struct elf_link_hash_entry *) p;
4580 if (h->root.type == bfd_link_hash_warning)
4582 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize);
4583 old_ent = (char *) old_ent + htab->root.table.entsize;
4584 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4586 if (h->root.type == bfd_link_hash_common)
4588 if (size > h->root.u.c.size)
4589 h->root.u.c.size = size;
4590 if (alignment_power > h->root.u.c.p->alignment_power)
4591 h->root.u.c.p->alignment_power = alignment_power;
4596 /* Make a special call to the linker "notice" function to
4597 tell it that symbols added for crefs may need to be removed. */
4598 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
4599 goto error_free_vers;
4602 objalloc_free_block ((struct objalloc *) htab->root.table.memory,
4604 if (nondeflt_vers != NULL)
4605 free (nondeflt_vers);
4609 if (old_tab != NULL)
4611 if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
4612 goto error_free_vers;
4617 /* Now that all the symbols from this input file are created, if
4618 not performing a relocatable link, handle .symver foo, foo@BAR
4619 such that any relocs against foo become foo@BAR. */
4620 if (!info->relocatable && nondeflt_vers != NULL)
4622 bfd_size_type cnt, symidx;
4624 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
4626 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
4627 char *shortname, *p;
4629 p = strchr (h->root.root.string, ELF_VER_CHR);
4631 || (h->root.type != bfd_link_hash_defined
4632 && h->root.type != bfd_link_hash_defweak))
4635 amt = p - h->root.root.string;
4636 shortname = (char *) bfd_malloc (amt + 1);
4638 goto error_free_vers;
4639 memcpy (shortname, h->root.root.string, amt);
4640 shortname[amt] = '\0';
4642 hi = (struct elf_link_hash_entry *)
4643 bfd_link_hash_lookup (&htab->root, shortname,
4644 FALSE, FALSE, FALSE);
4646 && hi->root.type == h->root.type
4647 && hi->root.u.def.value == h->root.u.def.value
4648 && hi->root.u.def.section == h->root.u.def.section)
4650 (*bed->elf_backend_hide_symbol) (info, hi, TRUE);
4651 hi->root.type = bfd_link_hash_indirect;
4652 hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
4653 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
4654 sym_hash = elf_sym_hashes (abfd);
4656 for (symidx = 0; symidx < extsymcount; ++symidx)
4657 if (sym_hash[symidx] == hi)
4659 sym_hash[symidx] = h;
4665 free (nondeflt_vers);
4666 nondeflt_vers = NULL;
4669 /* Now set the weakdefs field correctly for all the weak defined
4670 symbols we found. The only way to do this is to search all the
4671 symbols. Since we only need the information for non functions in
4672 dynamic objects, that's the only time we actually put anything on
4673 the list WEAKS. We need this information so that if a regular
4674 object refers to a symbol defined weakly in a dynamic object, the
4675 real symbol in the dynamic object is also put in the dynamic
4676 symbols; we also must arrange for both symbols to point to the
4677 same memory location. We could handle the general case of symbol
4678 aliasing, but a general symbol alias can only be generated in
4679 assembler code, handling it correctly would be very time
4680 consuming, and other ELF linkers don't handle general aliasing
4684 struct elf_link_hash_entry **hpp;
4685 struct elf_link_hash_entry **hppend;
4686 struct elf_link_hash_entry **sorted_sym_hash;
4687 struct elf_link_hash_entry *h;
4690 /* Since we have to search the whole symbol list for each weak
4691 defined symbol, search time for N weak defined symbols will be
4692 O(N^2). Binary search will cut it down to O(NlogN). */
4693 amt = extsymcount * sizeof (struct elf_link_hash_entry *);
4694 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt);
4695 if (sorted_sym_hash == NULL)
4697 sym_hash = sorted_sym_hash;
4698 hpp = elf_sym_hashes (abfd);
4699 hppend = hpp + extsymcount;
4701 for (; hpp < hppend; hpp++)
4705 && h->root.type == bfd_link_hash_defined
4706 && !bed->is_function_type (h->type))
4714 qsort (sorted_sym_hash, sym_count,
4715 sizeof (struct elf_link_hash_entry *),
4718 while (weaks != NULL)
4720 struct elf_link_hash_entry *hlook;
4723 size_t i, j, idx = 0;
4726 weaks = hlook->u.weakdef;
4727 hlook->u.weakdef = NULL;
4729 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
4730 || hlook->root.type == bfd_link_hash_defweak
4731 || hlook->root.type == bfd_link_hash_common
4732 || hlook->root.type == bfd_link_hash_indirect);
4733 slook = hlook->root.u.def.section;
4734 vlook = hlook->root.u.def.value;
4740 bfd_signed_vma vdiff;
4742 h = sorted_sym_hash[idx];
4743 vdiff = vlook - h->root.u.def.value;
4750 long sdiff = slook->id - h->root.u.def.section->id;
4760 /* We didn't find a value/section match. */
4764 /* With multiple aliases, or when the weak symbol is already
4765 strongly defined, we have multiple matching symbols and
4766 the binary search above may land on any of them. Step
4767 one past the matching symbol(s). */
4770 h = sorted_sym_hash[idx];
4771 if (h->root.u.def.section != slook
4772 || h->root.u.def.value != vlook)
4776 /* Now look back over the aliases. Since we sorted by size
4777 as well as value and section, we'll choose the one with
4778 the largest size. */
4781 h = sorted_sym_hash[idx];
4783 /* Stop if value or section doesn't match. */
4784 if (h->root.u.def.section != slook
4785 || h->root.u.def.value != vlook)
4787 else if (h != hlook)
4789 hlook->u.weakdef = h;
4791 /* If the weak definition is in the list of dynamic
4792 symbols, make sure the real definition is put
4794 if (hlook->dynindx != -1 && h->dynindx == -1)
4796 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4799 free (sorted_sym_hash);
4804 /* If the real definition is in the list of dynamic
4805 symbols, make sure the weak definition is put
4806 there as well. If we don't do this, then the
4807 dynamic loader might not merge the entries for the
4808 real definition and the weak definition. */
4809 if (h->dynindx != -1 && hlook->dynindx == -1)
4811 if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
4812 goto err_free_sym_hash;
4819 free (sorted_sym_hash);
4822 if (bed->check_directives
4823 && !(*bed->check_directives) (abfd, info))
4826 /* If this object is the same format as the output object, and it is
4827 not a shared library, then let the backend look through the
4830 This is required to build global offset table entries and to
4831 arrange for dynamic relocs. It is not required for the
4832 particular common case of linking non PIC code, even when linking
4833 against shared libraries, but unfortunately there is no way of
4834 knowing whether an object file has been compiled PIC or not.
4835 Looking through the relocs is not particularly time consuming.
4836 The problem is that we must either (1) keep the relocs in memory,
4837 which causes the linker to require additional runtime memory or
4838 (2) read the relocs twice from the input file, which wastes time.
4839 This would be a good case for using mmap.
4841 I have no idea how to handle linking PIC code into a file of a
4842 different format. It probably can't be done. */
4844 && is_elf_hash_table (htab)
4845 && bed->check_relocs != NULL
4846 && elf_object_id (abfd) == elf_hash_table_id (htab)
4847 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
4851 for (o = abfd->sections; o != NULL; o = o->next)
4853 Elf_Internal_Rela *internal_relocs;
4856 if ((o->flags & SEC_RELOC) == 0
4857 || o->reloc_count == 0
4858 || ((info->strip == strip_all || info->strip == strip_debugger)
4859 && (o->flags & SEC_DEBUGGING) != 0)
4860 || bfd_is_abs_section (o->output_section))
4863 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
4865 if (internal_relocs == NULL)
4868 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs);
4870 if (elf_section_data (o)->relocs != internal_relocs)
4871 free (internal_relocs);
4878 /* If this is a non-traditional link, try to optimize the handling
4879 of the .stab/.stabstr sections. */
4881 && ! info->traditional_format
4882 && is_elf_hash_table (htab)
4883 && (info->strip != strip_all && info->strip != strip_debugger))
4887 stabstr = bfd_get_section_by_name (abfd, ".stabstr");
4888 if (stabstr != NULL)
4890 bfd_size_type string_offset = 0;
4893 for (stab = abfd->sections; stab; stab = stab->next)
4894 if (CONST_STRNEQ (stab->name, ".stab")
4895 && (!stab->name[5] ||
4896 (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
4897 && (stab->flags & SEC_MERGE) == 0
4898 && !bfd_is_abs_section (stab->output_section))
4900 struct bfd_elf_section_data *secdata;
4902 secdata = elf_section_data (stab);
4903 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
4904 stabstr, &secdata->sec_info,
4907 if (secdata->sec_info)
4908 stab->sec_info_type = SEC_INFO_TYPE_STABS;
4913 if (is_elf_hash_table (htab) && add_needed)
4915 /* Add this bfd to the loaded list. */
4916 struct elf_link_loaded_list *n;
4918 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
4922 n->next = htab->loaded;
4929 if (old_tab != NULL)
4931 if (nondeflt_vers != NULL)
4932 free (nondeflt_vers);
4933 if (extversym != NULL)
4936 if (isymbuf != NULL)
4942 /* Return the linker hash table entry of a symbol that might be
4943 satisfied by an archive symbol. Return -1 on error. */
4945 struct elf_link_hash_entry *
4946 _bfd_elf_archive_symbol_lookup (bfd *abfd,
4947 struct bfd_link_info *info,
4950 struct elf_link_hash_entry *h;
4954 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE);
4958 /* If this is a default version (the name contains @@), look up the
4959 symbol again with only one `@' as well as without the version.
4960 The effect is that references to the symbol with and without the
4961 version will be matched by the default symbol in the archive. */
4963 p = strchr (name, ELF_VER_CHR);
4964 if (p == NULL || p[1] != ELF_VER_CHR)
4967 /* First check with only one `@'. */
4968 len = strlen (name);
4969 copy = (char *) bfd_alloc (abfd, len);
4971 return (struct elf_link_hash_entry *) 0 - 1;
4973 first = p - name + 1;
4974 memcpy (copy, name, first);
4975 memcpy (copy + first, name + first + 1, len - first);
4977 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE);
4980 /* We also need to check references to the symbol without the
4982 copy[first - 1] = '\0';
4983 h = elf_link_hash_lookup (elf_hash_table (info), copy,
4984 FALSE, FALSE, TRUE);
4987 bfd_release (abfd, copy);
4991 /* Add symbols from an ELF archive file to the linker hash table. We
4992 don't use _bfd_generic_link_add_archive_symbols because we need to
4993 handle versioned symbols.
4995 Fortunately, ELF archive handling is simpler than that done by
4996 _bfd_generic_link_add_archive_symbols, which has to allow for a.out
4997 oddities. In ELF, if we find a symbol in the archive map, and the
4998 symbol is currently undefined, we know that we must pull in that
5001 Unfortunately, we do have to make multiple passes over the symbol
5002 table until nothing further is resolved. */
5005 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
5008 unsigned char *included = NULL;
5012 const struct elf_backend_data *bed;
5013 struct elf_link_hash_entry * (*archive_symbol_lookup)
5014 (bfd *, struct bfd_link_info *, const char *);
5016 if (! bfd_has_map (abfd))
5018 /* An empty archive is a special case. */
5019 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
5021 bfd_set_error (bfd_error_no_armap);
5025 /* Keep track of all symbols we know to be already defined, and all
5026 files we know to be already included. This is to speed up the
5027 second and subsequent passes. */
5028 c = bfd_ardata (abfd)->symdef_count;
5032 amt *= sizeof (*included);
5033 included = (unsigned char *) bfd_zmalloc (amt);
5034 if (included == NULL)
5037 symdefs = bfd_ardata (abfd)->symdefs;
5038 bed = get_elf_backend_data (abfd);
5039 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
5052 symdefend = symdef + c;
5053 for (i = 0; symdef < symdefend; symdef++, i++)
5055 struct elf_link_hash_entry *h;
5057 struct bfd_link_hash_entry *undefs_tail;
5062 if (symdef->file_offset == last)
5068 h = archive_symbol_lookup (abfd, info, symdef->name);
5069 if (h == (struct elf_link_hash_entry *) 0 - 1)
5075 if (h->root.type == bfd_link_hash_common)
5077 /* We currently have a common symbol. The archive map contains
5078 a reference to this symbol, so we may want to include it. We
5079 only want to include it however, if this archive element
5080 contains a definition of the symbol, not just another common
5083 Unfortunately some archivers (including GNU ar) will put
5084 declarations of common symbols into their archive maps, as
5085 well as real definitions, so we cannot just go by the archive
5086 map alone. Instead we must read in the element's symbol
5087 table and check that to see what kind of symbol definition
5089 if (! elf_link_is_defined_archive_symbol (abfd, symdef))
5092 else if (h->root.type != bfd_link_hash_undefined)
5094 if (h->root.type != bfd_link_hash_undefweak)
5095 /* Symbol must be defined. Don't check it again. */
5100 /* We need to include this archive member. */
5101 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
5102 if (element == NULL)
5105 if (! bfd_check_format (element, bfd_object))
5108 undefs_tail = info->hash->undefs_tail;
5110 if (!(*info->callbacks
5111 ->add_archive_element) (info, element, symdef->name, &element))
5113 if (!bfd_link_add_symbols (element, info))
5116 /* If there are any new undefined symbols, we need to make
5117 another pass through the archive in order to see whether
5118 they can be defined. FIXME: This isn't perfect, because
5119 common symbols wind up on undefs_tail and because an
5120 undefined symbol which is defined later on in this pass
5121 does not require another pass. This isn't a bug, but it
5122 does make the code less efficient than it could be. */
5123 if (undefs_tail != info->hash->undefs_tail)
5126 /* Look backward to mark all symbols from this object file
5127 which we have already seen in this pass. */
5131 included[mark] = TRUE;
5136 while (symdefs[mark].file_offset == symdef->file_offset);
5138 /* We mark subsequent symbols from this object file as we go
5139 on through the loop. */
5140 last = symdef->file_offset;
5150 if (included != NULL)
5155 /* Given an ELF BFD, add symbols to the global hash table as
5159 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
5161 switch (bfd_get_format (abfd))
5164 return elf_link_add_object_symbols (abfd, info);
5166 return elf_link_add_archive_symbols (abfd, info);
5168 bfd_set_error (bfd_error_wrong_format);
5173 struct hash_codes_info
5175 unsigned long *hashcodes;
5179 /* This function will be called though elf_link_hash_traverse to store
5180 all hash value of the exported symbols in an array. */
5183 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
5185 struct hash_codes_info *inf = (struct hash_codes_info *) data;
5191 /* Ignore indirect symbols. These are added by the versioning code. */
5192 if (h->dynindx == -1)
5195 name = h->root.root.string;
5196 p = strchr (name, ELF_VER_CHR);
5199 alc = (char *) bfd_malloc (p - name + 1);
5205 memcpy (alc, name, p - name);
5206 alc[p - name] = '\0';
5210 /* Compute the hash value. */
5211 ha = bfd_elf_hash (name);
5213 /* Store the found hash value in the array given as the argument. */
5214 *(inf->hashcodes)++ = ha;
5216 /* And store it in the struct so that we can put it in the hash table
5218 h->u.elf_hash_value = ha;
5226 struct collect_gnu_hash_codes
5229 const struct elf_backend_data *bed;
5230 unsigned long int nsyms;
5231 unsigned long int maskbits;
5232 unsigned long int *hashcodes;
5233 unsigned long int *hashval;
5234 unsigned long int *indx;
5235 unsigned long int *counts;
5238 long int min_dynindx;
5239 unsigned long int bucketcount;
5240 unsigned long int symindx;
5241 long int local_indx;
5242 long int shift1, shift2;
5243 unsigned long int mask;
5247 /* This function will be called though elf_link_hash_traverse to store
5248 all hash value of the exported symbols in an array. */
5251 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
5253 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5259 /* Ignore indirect symbols. These are added by the versioning code. */
5260 if (h->dynindx == -1)
5263 /* Ignore also local symbols and undefined symbols. */
5264 if (! (*s->bed->elf_hash_symbol) (h))
5267 name = h->root.root.string;
5268 p = strchr (name, ELF_VER_CHR);
5271 alc = (char *) bfd_malloc (p - name + 1);
5277 memcpy (alc, name, p - name);
5278 alc[p - name] = '\0';
5282 /* Compute the hash value. */
5283 ha = bfd_elf_gnu_hash (name);
5285 /* Store the found hash value in the array for compute_bucket_count,
5286 and also for .dynsym reordering purposes. */
5287 s->hashcodes[s->nsyms] = ha;
5288 s->hashval[h->dynindx] = ha;
5290 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
5291 s->min_dynindx = h->dynindx;
5299 /* This function will be called though elf_link_hash_traverse to do
5300 final dynaminc symbol renumbering. */
5303 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data)
5305 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
5306 unsigned long int bucket;
5307 unsigned long int val;
5309 /* Ignore indirect symbols. */
5310 if (h->dynindx == -1)
5313 /* Ignore also local symbols and undefined symbols. */
5314 if (! (*s->bed->elf_hash_symbol) (h))
5316 if (h->dynindx >= s->min_dynindx)
5317 h->dynindx = s->local_indx++;
5321 bucket = s->hashval[h->dynindx] % s->bucketcount;
5322 val = (s->hashval[h->dynindx] >> s->shift1)
5323 & ((s->maskbits >> s->shift1) - 1);
5324 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
5326 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
5327 val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
5328 if (s->counts[bucket] == 1)
5329 /* Last element terminates the chain. */
5331 bfd_put_32 (s->output_bfd, val,
5332 s->contents + (s->indx[bucket] - s->symindx) * 4);
5333 --s->counts[bucket];
5334 h->dynindx = s->indx[bucket]++;
5338 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5341 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
5343 return !(h->forced_local
5344 || h->root.type == bfd_link_hash_undefined
5345 || h->root.type == bfd_link_hash_undefweak
5346 || ((h->root.type == bfd_link_hash_defined
5347 || h->root.type == bfd_link_hash_defweak)
5348 && h->root.u.def.section->output_section == NULL));
5351 /* Array used to determine the number of hash table buckets to use
5352 based on the number of symbols there are. If there are fewer than
5353 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
5354 fewer than 37 we use 17 buckets, and so forth. We never use more
5355 than 32771 buckets. */
5357 static const size_t elf_buckets[] =
5359 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
5363 /* Compute bucket count for hashing table. We do not use a static set
5364 of possible tables sizes anymore. Instead we determine for all
5365 possible reasonable sizes of the table the outcome (i.e., the
5366 number of collisions etc) and choose the best solution. The
5367 weighting functions are not too simple to allow the table to grow
5368 without bounds. Instead one of the weighting factors is the size.
5369 Therefore the result is always a good payoff between few collisions
5370 (= short chain lengths) and table size. */
5372 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
5373 unsigned long int *hashcodes ATTRIBUTE_UNUSED,
5374 unsigned long int nsyms,
5377 size_t best_size = 0;
5378 unsigned long int i;
5380 /* We have a problem here. The following code to optimize the table
5381 size requires an integer type with more the 32 bits. If
5382 BFD_HOST_U_64_BIT is set we know about such a type. */
5383 #ifdef BFD_HOST_U_64_BIT
5388 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
5389 bfd *dynobj = elf_hash_table (info)->dynobj;
5390 size_t dynsymcount = elf_hash_table (info)->dynsymcount;
5391 const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
5392 unsigned long int *counts;
5394 unsigned int no_improvement_count = 0;
5396 /* Possible optimization parameters: if we have NSYMS symbols we say
5397 that the hashing table must at least have NSYMS/4 and at most
5399 minsize = nsyms / 4;
5402 best_size = maxsize = nsyms * 2;
5407 if ((best_size & 31) == 0)
5411 /* Create array where we count the collisions in. We must use bfd_malloc
5412 since the size could be large. */
5414 amt *= sizeof (unsigned long int);
5415 counts = (unsigned long int *) bfd_malloc (amt);
5419 /* Compute the "optimal" size for the hash table. The criteria is a
5420 minimal chain length. The minor criteria is (of course) the size
5422 for (i = minsize; i < maxsize; ++i)
5424 /* Walk through the array of hashcodes and count the collisions. */
5425 BFD_HOST_U_64_BIT max;
5426 unsigned long int j;
5427 unsigned long int fact;
5429 if (gnu_hash && (i & 31) == 0)
5432 memset (counts, '\0', i * sizeof (unsigned long int));
5434 /* Determine how often each hash bucket is used. */
5435 for (j = 0; j < nsyms; ++j)
5436 ++counts[hashcodes[j] % i];
5438 /* For the weight function we need some information about the
5439 pagesize on the target. This is information need not be 100%
5440 accurate. Since this information is not available (so far) we
5441 define it here to a reasonable default value. If it is crucial
5442 to have a better value some day simply define this value. */
5443 # ifndef BFD_TARGET_PAGESIZE
5444 # define BFD_TARGET_PAGESIZE (4096)
5447 /* We in any case need 2 + DYNSYMCOUNT entries for the size values
5449 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
5452 /* Variant 1: optimize for short chains. We add the squares
5453 of all the chain lengths (which favors many small chain
5454 over a few long chains). */
5455 for (j = 0; j < i; ++j)
5456 max += counts[j] * counts[j];
5458 /* This adds penalties for the overall size of the table. */
5459 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5462 /* Variant 2: Optimize a lot more for small table. Here we
5463 also add squares of the size but we also add penalties for
5464 empty slots (the +1 term). */
5465 for (j = 0; j < i; ++j)
5466 max += (1 + counts[j]) * (1 + counts[j]);
5468 /* The overall size of the table is considered, but not as
5469 strong as in variant 1, where it is squared. */
5470 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
5474 /* Compare with current best results. */
5475 if (max < best_chlen)
5479 no_improvement_count = 0;
5481 /* PR 11843: Avoid futile long searches for the best bucket size
5482 when there are a large number of symbols. */
5483 else if (++no_improvement_count == 100)
5490 #endif /* defined (BFD_HOST_U_64_BIT) */
5492 /* This is the fallback solution if no 64bit type is available or if we
5493 are not supposed to spend much time on optimizations. We select the
5494 bucket count using a fixed set of numbers. */
5495 for (i = 0; elf_buckets[i] != 0; i++)
5497 best_size = elf_buckets[i];
5498 if (nsyms < elf_buckets[i + 1])
5501 if (gnu_hash && best_size < 2)
5508 /* Size any SHT_GROUP section for ld -r. */
5511 _bfd_elf_size_group_sections (struct bfd_link_info *info)
5515 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
5516 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
5517 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
5522 /* Set a default stack segment size. The value in INFO wins. If it
5523 is unset, LEGACY_SYMBOL's value is used, and if that symbol is
5524 undefined it is initialized. */
5527 bfd_elf_stack_segment_size (bfd *output_bfd,
5528 struct bfd_link_info *info,
5529 const char *legacy_symbol,
5530 bfd_vma default_size)
5532 struct elf_link_hash_entry *h = NULL;
5534 /* Look for legacy symbol. */
5536 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
5537 FALSE, FALSE, FALSE);
5538 if (h && (h->root.type == bfd_link_hash_defined
5539 || h->root.type == bfd_link_hash_defweak)
5541 && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
5543 /* The symbol has no type if specified on the command line. */
5544 h->type = STT_OBJECT;
5545 if (info->stacksize)
5546 (*_bfd_error_handler) (_("%B: stack size specified and %s set"),
5547 output_bfd, legacy_symbol);
5548 else if (h->root.u.def.section != bfd_abs_section_ptr)
5549 (*_bfd_error_handler) (_("%B: %s not absolute"),
5550 output_bfd, legacy_symbol);
5552 info->stacksize = h->root.u.def.value;
5555 if (!info->stacksize)
5556 /* If the user didn't set a size, or explicitly inhibit the
5557 size, set it now. */
5558 info->stacksize = default_size;
5560 /* Provide the legacy symbol, if it is referenced. */
5561 if (h && (h->root.type == bfd_link_hash_undefined
5562 || h->root.type == bfd_link_hash_undefweak))
5564 struct bfd_link_hash_entry *bh = NULL;
5566 if (!(_bfd_generic_link_add_one_symbol
5567 (info, output_bfd, legacy_symbol,
5568 BSF_GLOBAL, bfd_abs_section_ptr,
5569 info->stacksize >= 0 ? info->stacksize : 0,
5570 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh)))
5573 h = (struct elf_link_hash_entry *) bh;
5575 h->type = STT_OBJECT;
5581 /* Set up the sizes and contents of the ELF dynamic sections. This is
5582 called by the ELF linker emulation before_allocation routine. We
5583 must set the sizes of the sections before the linker sets the
5584 addresses of the various sections. */
5587 bfd_elf_size_dynamic_sections (bfd *output_bfd,
5590 const char *filter_shlib,
5592 const char *depaudit,
5593 const char * const *auxiliary_filters,
5594 struct bfd_link_info *info,
5595 asection **sinterpptr)
5597 bfd_size_type soname_indx;
5599 const struct elf_backend_data *bed;
5600 struct elf_info_failed asvinfo;
5604 soname_indx = (bfd_size_type) -1;
5606 if (!is_elf_hash_table (info->hash))
5609 bed = get_elf_backend_data (output_bfd);
5611 /* Any syms created from now on start with -1 in
5612 got.refcount/offset and plt.refcount/offset. */
5613 elf_hash_table (info)->init_got_refcount
5614 = elf_hash_table (info)->init_got_offset;
5615 elf_hash_table (info)->init_plt_refcount
5616 = elf_hash_table (info)->init_plt_offset;
5618 if (info->relocatable
5619 && !_bfd_elf_size_group_sections (info))
5622 /* The backend may have to create some sections regardless of whether
5623 we're dynamic or not. */
5624 if (bed->elf_backend_always_size_sections
5625 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
5628 /* Determine any GNU_STACK segment requirements, after the backend
5629 has had a chance to set a default segment size. */
5630 if (info->execstack)
5631 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
5632 else if (info->noexecstack)
5633 elf_stack_flags (output_bfd) = PF_R | PF_W;
5637 asection *notesec = NULL;
5640 for (inputobj = info->input_bfds;
5642 inputobj = inputobj->link.next)
5647 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
5649 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
5652 if (s->flags & SEC_CODE)
5656 else if (bed->default_execstack)
5659 if (notesec || info->stacksize > 0)
5660 elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
5661 if (notesec && exec && info->relocatable
5662 && notesec->output_section != bfd_abs_section_ptr)
5663 notesec->output_section->flags |= SEC_CODE;
5666 dynobj = elf_hash_table (info)->dynobj;
5668 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5670 struct elf_info_failed eif;
5671 struct elf_link_hash_entry *h;
5673 struct bfd_elf_version_tree *t;
5674 struct bfd_elf_version_expr *d;
5676 bfd_boolean all_defined;
5678 *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
5679 BFD_ASSERT (*sinterpptr != NULL || !info->executable);
5683 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5685 if (soname_indx == (bfd_size_type) -1
5686 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
5692 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
5694 info->flags |= DF_SYMBOLIC;
5702 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
5704 if (indx == (bfd_size_type) -1)
5707 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
5708 if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
5712 if (filter_shlib != NULL)
5716 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5717 filter_shlib, TRUE);
5718 if (indx == (bfd_size_type) -1
5719 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
5723 if (auxiliary_filters != NULL)
5725 const char * const *p;
5727 for (p = auxiliary_filters; *p != NULL; p++)
5731 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
5733 if (indx == (bfd_size_type) -1
5734 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
5743 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
5745 if (indx == (bfd_size_type) -1
5746 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
5750 if (depaudit != NULL)
5754 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
5756 if (indx == (bfd_size_type) -1
5757 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
5764 /* If we are supposed to export all symbols into the dynamic symbol
5765 table (this is not the normal case), then do so. */
5766 if (info->export_dynamic
5767 || (info->executable && info->dynamic))
5769 elf_link_hash_traverse (elf_hash_table (info),
5770 _bfd_elf_export_symbol,
5776 /* Make all global versions with definition. */
5777 for (t = info->version_info; t != NULL; t = t->next)
5778 for (d = t->globals.list; d != NULL; d = d->next)
5779 if (!d->symver && d->literal)
5781 const char *verstr, *name;
5782 size_t namelen, verlen, newlen;
5783 char *newname, *p, leading_char;
5784 struct elf_link_hash_entry *newh;
5786 leading_char = bfd_get_symbol_leading_char (output_bfd);
5788 namelen = strlen (name) + (leading_char != '\0');
5790 verlen = strlen (verstr);
5791 newlen = namelen + verlen + 3;
5793 newname = (char *) bfd_malloc (newlen);
5794 if (newname == NULL)
5796 newname[0] = leading_char;
5797 memcpy (newname + (leading_char != '\0'), name, namelen);
5799 /* Check the hidden versioned definition. */
5800 p = newname + namelen;
5802 memcpy (p, verstr, verlen + 1);
5803 newh = elf_link_hash_lookup (elf_hash_table (info),
5804 newname, FALSE, FALSE,
5807 || (newh->root.type != bfd_link_hash_defined
5808 && newh->root.type != bfd_link_hash_defweak))
5810 /* Check the default versioned definition. */
5812 memcpy (p, verstr, verlen + 1);
5813 newh = elf_link_hash_lookup (elf_hash_table (info),
5814 newname, FALSE, FALSE,
5819 /* Mark this version if there is a definition and it is
5820 not defined in a shared object. */
5822 && !newh->def_dynamic
5823 && (newh->root.type == bfd_link_hash_defined
5824 || newh->root.type == bfd_link_hash_defweak))
5828 /* Attach all the symbols to their version information. */
5829 asvinfo.info = info;
5830 asvinfo.failed = FALSE;
5832 elf_link_hash_traverse (elf_hash_table (info),
5833 _bfd_elf_link_assign_sym_version,
5838 if (!info->allow_undefined_version)
5840 /* Check if all global versions have a definition. */
5842 for (t = info->version_info; t != NULL; t = t->next)
5843 for (d = t->globals.list; d != NULL; d = d->next)
5844 if (d->literal && !d->symver && !d->script)
5846 (*_bfd_error_handler)
5847 (_("%s: undefined version: %s"),
5848 d->pattern, t->name);
5849 all_defined = FALSE;
5854 bfd_set_error (bfd_error_bad_value);
5859 /* Find all symbols which were defined in a dynamic object and make
5860 the backend pick a reasonable value for them. */
5861 elf_link_hash_traverse (elf_hash_table (info),
5862 _bfd_elf_adjust_dynamic_symbol,
5867 /* Add some entries to the .dynamic section. We fill in some of the
5868 values later, in bfd_elf_final_link, but we must add the entries
5869 now so that we know the final size of the .dynamic section. */
5871 /* If there are initialization and/or finalization functions to
5872 call then add the corresponding DT_INIT/DT_FINI entries. */
5873 h = (info->init_function
5874 ? elf_link_hash_lookup (elf_hash_table (info),
5875 info->init_function, FALSE,
5882 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
5885 h = (info->fini_function
5886 ? elf_link_hash_lookup (elf_hash_table (info),
5887 info->fini_function, FALSE,
5894 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
5898 s = bfd_get_section_by_name (output_bfd, ".preinit_array");
5899 if (s != NULL && s->linker_has_input)
5901 /* DT_PREINIT_ARRAY is not allowed in shared library. */
5902 if (! info->executable)
5907 for (sub = info->input_bfds; sub != NULL;
5908 sub = sub->link.next)
5909 if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
5910 for (o = sub->sections; o != NULL; o = o->next)
5911 if (elf_section_data (o)->this_hdr.sh_type
5912 == SHT_PREINIT_ARRAY)
5914 (*_bfd_error_handler)
5915 (_("%B: .preinit_array section is not allowed in DSO"),
5920 bfd_set_error (bfd_error_nonrepresentable_section);
5924 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
5925 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
5928 s = bfd_get_section_by_name (output_bfd, ".init_array");
5929 if (s != NULL && s->linker_has_input)
5931 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
5932 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
5935 s = bfd_get_section_by_name (output_bfd, ".fini_array");
5936 if (s != NULL && s->linker_has_input)
5938 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
5939 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
5943 dynstr = bfd_get_linker_section (dynobj, ".dynstr");
5944 /* If .dynstr is excluded from the link, we don't want any of
5945 these tags. Strictly, we should be checking each section
5946 individually; This quick check covers for the case where
5947 someone does a /DISCARD/ : { *(*) }. */
5948 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
5950 bfd_size_type strsize;
5952 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
5953 if ((info->emit_hash
5954 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
5955 || (info->emit_gnu_hash
5956 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))
5957 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
5958 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
5959 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
5960 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
5961 bed->s->sizeof_sym))
5966 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
5969 /* The backend must work out the sizes of all the other dynamic
5972 && bed->elf_backend_size_dynamic_sections != NULL
5973 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
5976 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
5978 unsigned long section_sym_count;
5979 struct bfd_elf_version_tree *verdefs;
5982 /* Set up the version definition section. */
5983 s = bfd_get_linker_section (dynobj, ".gnu.version_d");
5984 BFD_ASSERT (s != NULL);
5986 /* We may have created additional version definitions if we are
5987 just linking a regular application. */
5988 verdefs = info->version_info;
5990 /* Skip anonymous version tag. */
5991 if (verdefs != NULL && verdefs->vernum == 0)
5992 verdefs = verdefs->next;
5994 if (verdefs == NULL && !info->create_default_symver)
5995 s->flags |= SEC_EXCLUDE;
6000 struct bfd_elf_version_tree *t;
6002 Elf_Internal_Verdef def;
6003 Elf_Internal_Verdaux defaux;
6004 struct bfd_link_hash_entry *bh;
6005 struct elf_link_hash_entry *h;
6011 /* Make space for the base version. */
6012 size += sizeof (Elf_External_Verdef);
6013 size += sizeof (Elf_External_Verdaux);
6016 /* Make space for the default version. */
6017 if (info->create_default_symver)
6019 size += sizeof (Elf_External_Verdef);
6023 for (t = verdefs; t != NULL; t = t->next)
6025 struct bfd_elf_version_deps *n;
6027 /* Don't emit base version twice. */
6031 size += sizeof (Elf_External_Verdef);
6032 size += sizeof (Elf_External_Verdaux);
6035 for (n = t->deps; n != NULL; n = n->next)
6036 size += sizeof (Elf_External_Verdaux);
6040 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6041 if (s->contents == NULL && s->size != 0)
6044 /* Fill in the version definition section. */
6048 def.vd_version = VER_DEF_CURRENT;
6049 def.vd_flags = VER_FLG_BASE;
6052 if (info->create_default_symver)
6054 def.vd_aux = 2 * sizeof (Elf_External_Verdef);
6055 def.vd_next = sizeof (Elf_External_Verdef);
6059 def.vd_aux = sizeof (Elf_External_Verdef);
6060 def.vd_next = (sizeof (Elf_External_Verdef)
6061 + sizeof (Elf_External_Verdaux));
6064 if (soname_indx != (bfd_size_type) -1)
6066 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6068 def.vd_hash = bfd_elf_hash (soname);
6069 defaux.vda_name = soname_indx;
6076 name = lbasename (output_bfd->filename);
6077 def.vd_hash = bfd_elf_hash (name);
6078 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6080 if (indx == (bfd_size_type) -1)
6082 defaux.vda_name = indx;
6084 defaux.vda_next = 0;
6086 _bfd_elf_swap_verdef_out (output_bfd, &def,
6087 (Elf_External_Verdef *) p);
6088 p += sizeof (Elf_External_Verdef);
6089 if (info->create_default_symver)
6091 /* Add a symbol representing this version. */
6093 if (! (_bfd_generic_link_add_one_symbol
6094 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
6096 get_elf_backend_data (dynobj)->collect, &bh)))
6098 h = (struct elf_link_hash_entry *) bh;
6101 h->type = STT_OBJECT;
6102 h->verinfo.vertree = NULL;
6104 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6107 /* Create a duplicate of the base version with the same
6108 aux block, but different flags. */
6111 def.vd_aux = sizeof (Elf_External_Verdef);
6113 def.vd_next = (sizeof (Elf_External_Verdef)
6114 + sizeof (Elf_External_Verdaux));
6117 _bfd_elf_swap_verdef_out (output_bfd, &def,
6118 (Elf_External_Verdef *) p);
6119 p += sizeof (Elf_External_Verdef);
6121 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6122 (Elf_External_Verdaux *) p);
6123 p += sizeof (Elf_External_Verdaux);
6125 for (t = verdefs; t != NULL; t = t->next)
6128 struct bfd_elf_version_deps *n;
6130 /* Don't emit the base version twice. */
6135 for (n = t->deps; n != NULL; n = n->next)
6138 /* Add a symbol representing this version. */
6140 if (! (_bfd_generic_link_add_one_symbol
6141 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
6143 get_elf_backend_data (dynobj)->collect, &bh)))
6145 h = (struct elf_link_hash_entry *) bh;
6148 h->type = STT_OBJECT;
6149 h->verinfo.vertree = t;
6151 if (! bfd_elf_link_record_dynamic_symbol (info, h))
6154 def.vd_version = VER_DEF_CURRENT;
6156 if (t->globals.list == NULL
6157 && t->locals.list == NULL
6159 def.vd_flags |= VER_FLG_WEAK;
6160 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
6161 def.vd_cnt = cdeps + 1;
6162 def.vd_hash = bfd_elf_hash (t->name);
6163 def.vd_aux = sizeof (Elf_External_Verdef);
6166 /* If a basever node is next, it *must* be the last node in
6167 the chain, otherwise Verdef construction breaks. */
6168 if (t->next != NULL && t->next->vernum == 0)
6169 BFD_ASSERT (t->next->next == NULL);
6171 if (t->next != NULL && t->next->vernum != 0)
6172 def.vd_next = (sizeof (Elf_External_Verdef)
6173 + (cdeps + 1) * sizeof (Elf_External_Verdaux));
6175 _bfd_elf_swap_verdef_out (output_bfd, &def,
6176 (Elf_External_Verdef *) p);
6177 p += sizeof (Elf_External_Verdef);
6179 defaux.vda_name = h->dynstr_index;
6180 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6182 defaux.vda_next = 0;
6183 if (t->deps != NULL)
6184 defaux.vda_next = sizeof (Elf_External_Verdaux);
6185 t->name_indx = defaux.vda_name;
6187 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6188 (Elf_External_Verdaux *) p);
6189 p += sizeof (Elf_External_Verdaux);
6191 for (n = t->deps; n != NULL; n = n->next)
6193 if (n->version_needed == NULL)
6195 /* This can happen if there was an error in the
6197 defaux.vda_name = 0;
6201 defaux.vda_name = n->version_needed->name_indx;
6202 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
6205 if (n->next == NULL)
6206 defaux.vda_next = 0;
6208 defaux.vda_next = sizeof (Elf_External_Verdaux);
6210 _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
6211 (Elf_External_Verdaux *) p);
6212 p += sizeof (Elf_External_Verdaux);
6216 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
6217 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs))
6220 elf_tdata (output_bfd)->cverdefs = cdefs;
6223 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
6225 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
6228 else if (info->flags & DF_BIND_NOW)
6230 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
6236 if (info->executable)
6237 info->flags_1 &= ~ (DF_1_INITFIRST
6240 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
6244 /* Work out the size of the version reference section. */
6246 s = bfd_get_linker_section (dynobj, ".gnu.version_r");
6247 BFD_ASSERT (s != NULL);
6249 struct elf_find_verdep_info sinfo;
6252 sinfo.vers = elf_tdata (output_bfd)->cverdefs;
6253 if (sinfo.vers == 0)
6255 sinfo.failed = FALSE;
6257 elf_link_hash_traverse (elf_hash_table (info),
6258 _bfd_elf_link_find_version_dependencies,
6263 if (elf_tdata (output_bfd)->verref == NULL)
6264 s->flags |= SEC_EXCLUDE;
6267 Elf_Internal_Verneed *t;
6272 /* Build the version dependency section. */
6275 for (t = elf_tdata (output_bfd)->verref;
6279 Elf_Internal_Vernaux *a;
6281 size += sizeof (Elf_External_Verneed);
6283 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6284 size += sizeof (Elf_External_Vernaux);
6288 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6289 if (s->contents == NULL)
6293 for (t = elf_tdata (output_bfd)->verref;
6298 Elf_Internal_Vernaux *a;
6302 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6305 t->vn_version = VER_NEED_CURRENT;
6307 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6308 elf_dt_name (t->vn_bfd) != NULL
6309 ? elf_dt_name (t->vn_bfd)
6310 : lbasename (t->vn_bfd->filename),
6312 if (indx == (bfd_size_type) -1)
6315 t->vn_aux = sizeof (Elf_External_Verneed);
6316 if (t->vn_nextref == NULL)
6319 t->vn_next = (sizeof (Elf_External_Verneed)
6320 + caux * sizeof (Elf_External_Vernaux));
6322 _bfd_elf_swap_verneed_out (output_bfd, t,
6323 (Elf_External_Verneed *) p);
6324 p += sizeof (Elf_External_Verneed);
6326 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
6328 a->vna_hash = bfd_elf_hash (a->vna_nodename);
6329 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
6330 a->vna_nodename, FALSE);
6331 if (indx == (bfd_size_type) -1)
6334 if (a->vna_nextptr == NULL)
6337 a->vna_next = sizeof (Elf_External_Vernaux);
6339 _bfd_elf_swap_vernaux_out (output_bfd, a,
6340 (Elf_External_Vernaux *) p);
6341 p += sizeof (Elf_External_Vernaux);
6345 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
6346 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
6349 elf_tdata (output_bfd)->cverrefs = crefs;
6353 if ((elf_tdata (output_bfd)->cverrefs == 0
6354 && elf_tdata (output_bfd)->cverdefs == 0)
6355 || _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6356 §ion_sym_count) == 0)
6358 s = bfd_get_linker_section (dynobj, ".gnu.version");
6359 s->flags |= SEC_EXCLUDE;
6365 /* Find the first non-excluded output section. We'll use its
6366 section symbol for some emitted relocs. */
6368 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
6372 for (s = output_bfd->sections; s != NULL; s = s->next)
6373 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
6374 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6376 elf_hash_table (info)->text_index_section = s;
6381 /* Find two non-excluded output sections, one for code, one for data.
6382 We'll use their section symbols for some emitted relocs. */
6384 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
6388 /* Data first, since setting text_index_section changes
6389 _bfd_elf_link_omit_section_dynsym. */
6390 for (s = output_bfd->sections; s != NULL; s = s->next)
6391 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6392 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6394 elf_hash_table (info)->data_index_section = s;
6398 for (s = output_bfd->sections; s != NULL; s = s->next)
6399 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY))
6400 == (SEC_ALLOC | SEC_READONLY))
6401 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s))
6403 elf_hash_table (info)->text_index_section = s;
6407 if (elf_hash_table (info)->text_index_section == NULL)
6408 elf_hash_table (info)->text_index_section
6409 = elf_hash_table (info)->data_index_section;
6413 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
6415 const struct elf_backend_data *bed;
6417 if (!is_elf_hash_table (info->hash))
6420 bed = get_elf_backend_data (output_bfd);
6421 (*bed->elf_backend_init_index_section) (output_bfd, info);
6423 if (elf_hash_table (info)->dynamic_sections_created)
6427 bfd_size_type dynsymcount;
6428 unsigned long section_sym_count;
6429 unsigned int dtagcount;
6431 dynobj = elf_hash_table (info)->dynobj;
6433 /* Assign dynsym indicies. In a shared library we generate a
6434 section symbol for each output section, which come first.
6435 Next come all of the back-end allocated local dynamic syms,
6436 followed by the rest of the global symbols. */
6438 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
6439 §ion_sym_count);
6441 /* Work out the size of the symbol version section. */
6442 s = bfd_get_linker_section (dynobj, ".gnu.version");
6443 BFD_ASSERT (s != NULL);
6444 if (dynsymcount != 0
6445 && (s->flags & SEC_EXCLUDE) == 0)
6447 s->size = dynsymcount * sizeof (Elf_External_Versym);
6448 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6449 if (s->contents == NULL)
6452 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
6456 /* Set the size of the .dynsym and .hash sections. We counted
6457 the number of dynamic symbols in elf_link_add_object_symbols.
6458 We will build the contents of .dynsym and .hash when we build
6459 the final symbol table, because until then we do not know the
6460 correct value to give the symbols. We built the .dynstr
6461 section as we went along in elf_link_add_object_symbols. */
6462 s = bfd_get_linker_section (dynobj, ".dynsym");
6463 BFD_ASSERT (s != NULL);
6464 s->size = dynsymcount * bed->s->sizeof_sym;
6466 if (dynsymcount != 0)
6468 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
6469 if (s->contents == NULL)
6472 /* The first entry in .dynsym is a dummy symbol.
6473 Clear all the section syms, in case we don't output them all. */
6474 ++section_sym_count;
6475 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
6478 elf_hash_table (info)->bucketcount = 0;
6480 /* Compute the size of the hashing table. As a side effect this
6481 computes the hash values for all the names we export. */
6482 if (info->emit_hash)
6484 unsigned long int *hashcodes;
6485 struct hash_codes_info hashinf;
6487 unsigned long int nsyms;
6489 size_t hash_entry_size;
6491 /* Compute the hash values for all exported symbols. At the same
6492 time store the values in an array so that we could use them for
6494 amt = dynsymcount * sizeof (unsigned long int);
6495 hashcodes = (unsigned long int *) bfd_malloc (amt);
6496 if (hashcodes == NULL)
6498 hashinf.hashcodes = hashcodes;
6499 hashinf.error = FALSE;
6501 /* Put all hash values in HASHCODES. */
6502 elf_link_hash_traverse (elf_hash_table (info),
6503 elf_collect_hash_codes, &hashinf);
6510 nsyms = hashinf.hashcodes - hashcodes;
6512 = compute_bucket_count (info, hashcodes, nsyms, 0);
6515 if (bucketcount == 0)
6518 elf_hash_table (info)->bucketcount = bucketcount;
6520 s = bfd_get_linker_section (dynobj, ".hash");
6521 BFD_ASSERT (s != NULL);
6522 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
6523 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
6524 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6525 if (s->contents == NULL)
6528 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
6529 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
6530 s->contents + hash_entry_size);
6533 if (info->emit_gnu_hash)
6536 unsigned char *contents;
6537 struct collect_gnu_hash_codes cinfo;
6541 memset (&cinfo, 0, sizeof (cinfo));
6543 /* Compute the hash values for all exported symbols. At the same
6544 time store the values in an array so that we could use them for
6546 amt = dynsymcount * 2 * sizeof (unsigned long int);
6547 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
6548 if (cinfo.hashcodes == NULL)
6551 cinfo.hashval = cinfo.hashcodes + dynsymcount;
6552 cinfo.min_dynindx = -1;
6553 cinfo.output_bfd = output_bfd;
6556 /* Put all hash values in HASHCODES. */
6557 elf_link_hash_traverse (elf_hash_table (info),
6558 elf_collect_gnu_hash_codes, &cinfo);
6561 free (cinfo.hashcodes);
6566 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
6568 if (bucketcount == 0)
6570 free (cinfo.hashcodes);
6574 s = bfd_get_linker_section (dynobj, ".gnu.hash");
6575 BFD_ASSERT (s != NULL);
6577 if (cinfo.nsyms == 0)
6579 /* Empty .gnu.hash section is special. */
6580 BFD_ASSERT (cinfo.min_dynindx == -1);
6581 free (cinfo.hashcodes);
6582 s->size = 5 * 4 + bed->s->arch_size / 8;
6583 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6584 if (contents == NULL)
6586 s->contents = contents;
6587 /* 1 empty bucket. */
6588 bfd_put_32 (output_bfd, 1, contents);
6589 /* SYMIDX above the special symbol 0. */
6590 bfd_put_32 (output_bfd, 1, contents + 4);
6591 /* Just one word for bitmask. */
6592 bfd_put_32 (output_bfd, 1, contents + 8);
6593 /* Only hash fn bloom filter. */
6594 bfd_put_32 (output_bfd, 0, contents + 12);
6595 /* No hashes are valid - empty bitmask. */
6596 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
6597 /* No hashes in the only bucket. */
6598 bfd_put_32 (output_bfd, 0,
6599 contents + 16 + bed->s->arch_size / 8);
6603 unsigned long int maskwords, maskbitslog2, x;
6604 BFD_ASSERT (cinfo.min_dynindx != -1);
6608 while ((x >>= 1) != 0)
6610 if (maskbitslog2 < 3)
6612 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
6613 maskbitslog2 = maskbitslog2 + 3;
6615 maskbitslog2 = maskbitslog2 + 2;
6616 if (bed->s->arch_size == 64)
6618 if (maskbitslog2 == 5)
6624 cinfo.mask = (1 << cinfo.shift1) - 1;
6625 cinfo.shift2 = maskbitslog2;
6626 cinfo.maskbits = 1 << maskbitslog2;
6627 maskwords = 1 << (maskbitslog2 - cinfo.shift1);
6628 amt = bucketcount * sizeof (unsigned long int) * 2;
6629 amt += maskwords * sizeof (bfd_vma);
6630 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
6631 if (cinfo.bitmask == NULL)
6633 free (cinfo.hashcodes);
6637 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
6638 cinfo.indx = cinfo.counts + bucketcount;
6639 cinfo.symindx = dynsymcount - cinfo.nsyms;
6640 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
6642 /* Determine how often each hash bucket is used. */
6643 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
6644 for (i = 0; i < cinfo.nsyms; ++i)
6645 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
6647 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
6648 if (cinfo.counts[i] != 0)
6650 cinfo.indx[i] = cnt;
6651 cnt += cinfo.counts[i];
6653 BFD_ASSERT (cnt == dynsymcount);
6654 cinfo.bucketcount = bucketcount;
6655 cinfo.local_indx = cinfo.min_dynindx;
6657 s->size = (4 + bucketcount + cinfo.nsyms) * 4;
6658 s->size += cinfo.maskbits / 8;
6659 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
6660 if (contents == NULL)
6662 free (cinfo.bitmask);
6663 free (cinfo.hashcodes);
6667 s->contents = contents;
6668 bfd_put_32 (output_bfd, bucketcount, contents);
6669 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
6670 bfd_put_32 (output_bfd, maskwords, contents + 8);
6671 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
6672 contents += 16 + cinfo.maskbits / 8;
6674 for (i = 0; i < bucketcount; ++i)
6676 if (cinfo.counts[i] == 0)
6677 bfd_put_32 (output_bfd, 0, contents);
6679 bfd_put_32 (output_bfd, cinfo.indx[i], contents);
6683 cinfo.contents = contents;
6685 /* Renumber dynamic symbols, populate .gnu.hash section. */
6686 elf_link_hash_traverse (elf_hash_table (info),
6687 elf_renumber_gnu_hash_syms, &cinfo);
6689 contents = s->contents + 16;
6690 for (i = 0; i < maskwords; ++i)
6692 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
6694 contents += bed->s->arch_size / 8;
6697 free (cinfo.bitmask);
6698 free (cinfo.hashcodes);
6702 s = bfd_get_linker_section (dynobj, ".dynstr");
6703 BFD_ASSERT (s != NULL);
6705 elf_finalize_dynstr (output_bfd, info);
6707 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
6709 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
6710 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
6717 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
6720 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
6723 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
6724 sec->sec_info_type = SEC_INFO_TYPE_NONE;
6727 /* Finish SHF_MERGE section merging. */
6730 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
6735 if (!is_elf_hash_table (info->hash))
6738 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
6739 if ((ibfd->flags & DYNAMIC) == 0)
6740 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6741 if ((sec->flags & SEC_MERGE) != 0
6742 && !bfd_is_abs_section (sec->output_section))
6744 struct bfd_elf_section_data *secdata;
6746 secdata = elf_section_data (sec);
6747 if (! _bfd_add_merge_section (abfd,
6748 &elf_hash_table (info)->merge_info,
6749 sec, &secdata->sec_info))
6751 else if (secdata->sec_info)
6752 sec->sec_info_type = SEC_INFO_TYPE_MERGE;
6755 if (elf_hash_table (info)->merge_info != NULL)
6756 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
6757 merge_sections_remove_hook);
6761 /* Create an entry in an ELF linker hash table. */
6763 struct bfd_hash_entry *
6764 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
6765 struct bfd_hash_table *table,
6768 /* Allocate the structure if it has not already been allocated by a
6772 entry = (struct bfd_hash_entry *)
6773 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
6778 /* Call the allocation method of the superclass. */
6779 entry = _bfd_link_hash_newfunc (entry, table, string);
6782 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
6783 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
6785 /* Set local fields. */
6788 ret->got = htab->init_got_refcount;
6789 ret->plt = htab->init_plt_refcount;
6790 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
6791 - offsetof (struct elf_link_hash_entry, size)));
6792 /* Assume that we have been called by a non-ELF symbol reader.
6793 This flag is then reset by the code which reads an ELF input
6794 file. This ensures that a symbol created by a non-ELF symbol
6795 reader will have the flag set correctly. */
6802 /* Copy data from an indirect symbol to its direct symbol, hiding the
6803 old indirect symbol. Also used for copying flags to a weakdef. */
6806 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
6807 struct elf_link_hash_entry *dir,
6808 struct elf_link_hash_entry *ind)
6810 struct elf_link_hash_table *htab;
6812 /* Copy down any references that we may have already seen to the
6813 symbol which just became indirect. */
6815 dir->ref_dynamic |= ind->ref_dynamic;
6816 dir->ref_regular |= ind->ref_regular;
6817 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
6818 dir->non_got_ref |= ind->non_got_ref;
6819 dir->needs_plt |= ind->needs_plt;
6820 dir->pointer_equality_needed |= ind->pointer_equality_needed;
6822 if (ind->root.type != bfd_link_hash_indirect)
6825 /* Copy over the global and procedure linkage table refcount entries.
6826 These may have been already set up by a check_relocs routine. */
6827 htab = elf_hash_table (info);
6828 if (ind->got.refcount > htab->init_got_refcount.refcount)
6830 if (dir->got.refcount < 0)
6831 dir->got.refcount = 0;
6832 dir->got.refcount += ind->got.refcount;
6833 ind->got.refcount = htab->init_got_refcount.refcount;
6836 if (ind->plt.refcount > htab->init_plt_refcount.refcount)
6838 if (dir->plt.refcount < 0)
6839 dir->plt.refcount = 0;
6840 dir->plt.refcount += ind->plt.refcount;
6841 ind->plt.refcount = htab->init_plt_refcount.refcount;
6844 if (ind->dynindx != -1)
6846 if (dir->dynindx != -1)
6847 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
6848 dir->dynindx = ind->dynindx;
6849 dir->dynstr_index = ind->dynstr_index;
6851 ind->dynstr_index = 0;
6856 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
6857 struct elf_link_hash_entry *h,
6858 bfd_boolean force_local)
6860 /* STT_GNU_IFUNC symbol must go through PLT. */
6861 if (h->type != STT_GNU_IFUNC)
6863 h->plt = elf_hash_table (info)->init_plt_offset;
6868 h->forced_local = 1;
6869 if (h->dynindx != -1)
6872 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
6878 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
6882 _bfd_elf_link_hash_table_init
6883 (struct elf_link_hash_table *table,
6885 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
6886 struct bfd_hash_table *,
6888 unsigned int entsize,
6889 enum elf_target_id target_id)
6892 int can_refcount = get_elf_backend_data (abfd)->can_refcount;
6894 table->init_got_refcount.refcount = can_refcount - 1;
6895 table->init_plt_refcount.refcount = can_refcount - 1;
6896 table->init_got_offset.offset = -(bfd_vma) 1;
6897 table->init_plt_offset.offset = -(bfd_vma) 1;
6898 /* The first dynamic symbol is a dummy. */
6899 table->dynsymcount = 1;
6901 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
6903 table->root.type = bfd_link_elf_hash_table;
6904 table->hash_table_id = target_id;
6909 /* Create an ELF linker hash table. */
6911 struct bfd_link_hash_table *
6912 _bfd_elf_link_hash_table_create (bfd *abfd)
6914 struct elf_link_hash_table *ret;
6915 bfd_size_type amt = sizeof (struct elf_link_hash_table);
6917 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
6921 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
6922 sizeof (struct elf_link_hash_entry),
6928 ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
6933 /* Destroy an ELF linker hash table. */
6936 _bfd_elf_link_hash_table_free (bfd *obfd)
6938 struct elf_link_hash_table *htab;
6940 htab = (struct elf_link_hash_table *) obfd->link.hash;
6941 if (htab->dynstr != NULL)
6942 _bfd_elf_strtab_free (htab->dynstr);
6943 _bfd_merge_sections_free (htab->merge_info);
6944 _bfd_generic_link_hash_table_free (obfd);
6947 /* This is a hook for the ELF emulation code in the generic linker to
6948 tell the backend linker what file name to use for the DT_NEEDED
6949 entry for a dynamic object. */
6952 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
6954 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6955 && bfd_get_format (abfd) == bfd_object)
6956 elf_dt_name (abfd) = name;
6960 bfd_elf_get_dyn_lib_class (bfd *abfd)
6963 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6964 && bfd_get_format (abfd) == bfd_object)
6965 lib_class = elf_dyn_lib_class (abfd);
6972 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
6974 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6975 && bfd_get_format (abfd) == bfd_object)
6976 elf_dyn_lib_class (abfd) = lib_class;
6979 /* Get the list of DT_NEEDED entries for a link. This is a hook for
6980 the linker ELF emulation code. */
6982 struct bfd_link_needed_list *
6983 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
6984 struct bfd_link_info *info)
6986 if (! is_elf_hash_table (info->hash))
6988 return elf_hash_table (info)->needed;
6991 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
6992 hook for the linker ELF emulation code. */
6994 struct bfd_link_needed_list *
6995 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
6996 struct bfd_link_info *info)
6998 if (! is_elf_hash_table (info->hash))
7000 return elf_hash_table (info)->runpath;
7003 /* Get the name actually used for a dynamic object for a link. This
7004 is the SONAME entry if there is one. Otherwise, it is the string
7005 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
7008 bfd_elf_get_dt_soname (bfd *abfd)
7010 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
7011 && bfd_get_format (abfd) == bfd_object)
7012 return elf_dt_name (abfd);
7016 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
7017 the ELF linker emulation code. */
7020 bfd_elf_get_bfd_needed_list (bfd *abfd,
7021 struct bfd_link_needed_list **pneeded)
7024 bfd_byte *dynbuf = NULL;
7025 unsigned int elfsec;
7026 unsigned long shlink;
7027 bfd_byte *extdyn, *extdynend;
7029 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
7033 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
7034 || bfd_get_format (abfd) != bfd_object)
7037 s = bfd_get_section_by_name (abfd, ".dynamic");
7038 if (s == NULL || s->size == 0)
7041 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
7044 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
7045 if (elfsec == SHN_BAD)
7048 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
7050 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
7051 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
7054 extdynend = extdyn + s->size;
7055 for (; extdyn < extdynend; extdyn += extdynsize)
7057 Elf_Internal_Dyn dyn;
7059 (*swap_dyn_in) (abfd, extdyn, &dyn);
7061 if (dyn.d_tag == DT_NULL)
7064 if (dyn.d_tag == DT_NEEDED)
7067 struct bfd_link_needed_list *l;
7068 unsigned int tagv = dyn.d_un.d_val;
7071 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
7076 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
7097 struct elf_symbuf_symbol
7099 unsigned long st_name; /* Symbol name, index in string tbl */
7100 unsigned char st_info; /* Type and binding attributes */
7101 unsigned char st_other; /* Visibilty, and target specific */
7104 struct elf_symbuf_head
7106 struct elf_symbuf_symbol *ssym;
7107 bfd_size_type count;
7108 unsigned int st_shndx;
7115 Elf_Internal_Sym *isym;
7116 struct elf_symbuf_symbol *ssym;
7121 /* Sort references to symbols by ascending section number. */
7124 elf_sort_elf_symbol (const void *arg1, const void *arg2)
7126 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
7127 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
7129 return s1->st_shndx - s2->st_shndx;
7133 elf_sym_name_compare (const void *arg1, const void *arg2)
7135 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
7136 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
7137 return strcmp (s1->name, s2->name);
7140 static struct elf_symbuf_head *
7141 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf)
7143 Elf_Internal_Sym **ind, **indbufend, **indbuf;
7144 struct elf_symbuf_symbol *ssym;
7145 struct elf_symbuf_head *ssymbuf, *ssymhead;
7146 bfd_size_type i, shndx_count, total_size;
7148 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf));
7152 for (ind = indbuf, i = 0; i < symcount; i++)
7153 if (isymbuf[i].st_shndx != SHN_UNDEF)
7154 *ind++ = &isymbuf[i];
7157 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
7158 elf_sort_elf_symbol);
7161 if (indbufend > indbuf)
7162 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
7163 if (ind[0]->st_shndx != ind[1]->st_shndx)
7166 total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
7167 + (indbufend - indbuf) * sizeof (*ssym));
7168 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
7169 if (ssymbuf == NULL)
7175 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
7176 ssymbuf->ssym = NULL;
7177 ssymbuf->count = shndx_count;
7178 ssymbuf->st_shndx = 0;
7179 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
7181 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
7184 ssymhead->ssym = ssym;
7185 ssymhead->count = 0;
7186 ssymhead->st_shndx = (*ind)->st_shndx;
7188 ssym->st_name = (*ind)->st_name;
7189 ssym->st_info = (*ind)->st_info;
7190 ssym->st_other = (*ind)->st_other;
7193 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count
7194 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
7201 /* Check if 2 sections define the same set of local and global
7205 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
7206 struct bfd_link_info *info)
7209 const struct elf_backend_data *bed1, *bed2;
7210 Elf_Internal_Shdr *hdr1, *hdr2;
7211 bfd_size_type symcount1, symcount2;
7212 Elf_Internal_Sym *isymbuf1, *isymbuf2;
7213 struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
7214 Elf_Internal_Sym *isym, *isymend;
7215 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
7216 bfd_size_type count1, count2, i;
7217 unsigned int shndx1, shndx2;
7223 /* Both sections have to be in ELF. */
7224 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
7225 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
7228 if (elf_section_type (sec1) != elf_section_type (sec2))
7231 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
7232 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
7233 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
7236 bed1 = get_elf_backend_data (bfd1);
7237 bed2 = get_elf_backend_data (bfd2);
7238 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
7239 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
7240 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
7241 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
7243 if (symcount1 == 0 || symcount2 == 0)
7249 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
7250 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
7252 if (ssymbuf1 == NULL)
7254 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
7256 if (isymbuf1 == NULL)
7259 if (!info->reduce_memory_overheads)
7260 elf_tdata (bfd1)->symbuf = ssymbuf1
7261 = elf_create_symbuf (symcount1, isymbuf1);
7264 if (ssymbuf1 == NULL || ssymbuf2 == NULL)
7266 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
7268 if (isymbuf2 == NULL)
7271 if (ssymbuf1 != NULL && !info->reduce_memory_overheads)
7272 elf_tdata (bfd2)->symbuf = ssymbuf2
7273 = elf_create_symbuf (symcount2, isymbuf2);
7276 if (ssymbuf1 != NULL && ssymbuf2 != NULL)
7278 /* Optimized faster version. */
7279 bfd_size_type lo, hi, mid;
7280 struct elf_symbol *symp;
7281 struct elf_symbuf_symbol *ssym, *ssymend;
7284 hi = ssymbuf1->count;
7289 mid = (lo + hi) / 2;
7290 if (shndx1 < ssymbuf1[mid].st_shndx)
7292 else if (shndx1 > ssymbuf1[mid].st_shndx)
7296 count1 = ssymbuf1[mid].count;
7303 hi = ssymbuf2->count;
7308 mid = (lo + hi) / 2;
7309 if (shndx2 < ssymbuf2[mid].st_shndx)
7311 else if (shndx2 > ssymbuf2[mid].st_shndx)
7315 count2 = ssymbuf2[mid].count;
7321 if (count1 == 0 || count2 == 0 || count1 != count2)
7325 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
7327 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
7328 if (symtable1 == NULL || symtable2 == NULL)
7332 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1;
7333 ssym < ssymend; ssym++, symp++)
7335 symp->u.ssym = ssym;
7336 symp->name = bfd_elf_string_from_elf_section (bfd1,
7342 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2;
7343 ssym < ssymend; ssym++, symp++)
7345 symp->u.ssym = ssym;
7346 symp->name = bfd_elf_string_from_elf_section (bfd2,
7351 /* Sort symbol by name. */
7352 qsort (symtable1, count1, sizeof (struct elf_symbol),
7353 elf_sym_name_compare);
7354 qsort (symtable2, count1, sizeof (struct elf_symbol),
7355 elf_sym_name_compare);
7357 for (i = 0; i < count1; i++)
7358 /* Two symbols must have the same binding, type and name. */
7359 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
7360 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
7361 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7368 symtable1 = (struct elf_symbol *)
7369 bfd_malloc (symcount1 * sizeof (struct elf_symbol));
7370 symtable2 = (struct elf_symbol *)
7371 bfd_malloc (symcount2 * sizeof (struct elf_symbol));
7372 if (symtable1 == NULL || symtable2 == NULL)
7375 /* Count definitions in the section. */
7377 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
7378 if (isym->st_shndx == shndx1)
7379 symtable1[count1++].u.isym = isym;
7382 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
7383 if (isym->st_shndx == shndx2)
7384 symtable2[count2++].u.isym = isym;
7386 if (count1 == 0 || count2 == 0 || count1 != count2)
7389 for (i = 0; i < count1; i++)
7391 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
7392 symtable1[i].u.isym->st_name);
7394 for (i = 0; i < count2; i++)
7396 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
7397 symtable2[i].u.isym->st_name);
7399 /* Sort symbol by name. */
7400 qsort (symtable1, count1, sizeof (struct elf_symbol),
7401 elf_sym_name_compare);
7402 qsort (symtable2, count1, sizeof (struct elf_symbol),
7403 elf_sym_name_compare);
7405 for (i = 0; i < count1; i++)
7406 /* Two symbols must have the same binding, type and name. */
7407 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
7408 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
7409 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
7427 /* Return TRUE if 2 section types are compatible. */
7430 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
7431 bfd *bbfd, const asection *bsec)
7435 || abfd->xvec->flavour != bfd_target_elf_flavour
7436 || bbfd->xvec->flavour != bfd_target_elf_flavour)
7439 return elf_section_type (asec) == elf_section_type (bsec);
7442 /* Final phase of ELF linker. */
7444 /* A structure we use to avoid passing large numbers of arguments. */
7446 struct elf_final_link_info
7448 /* General link information. */
7449 struct bfd_link_info *info;
7452 /* Symbol string table. */
7453 struct elf_strtab_hash *symstrtab;
7454 /* .dynsym section. */
7455 asection *dynsym_sec;
7456 /* .hash section. */
7458 /* symbol version section (.gnu.version). */
7459 asection *symver_sec;
7460 /* Buffer large enough to hold contents of any section. */
7462 /* Buffer large enough to hold external relocs of any section. */
7463 void *external_relocs;
7464 /* Buffer large enough to hold internal relocs of any section. */
7465 Elf_Internal_Rela *internal_relocs;
7466 /* Buffer large enough to hold external local symbols of any input
7468 bfd_byte *external_syms;
7469 /* And a buffer for symbol section indices. */
7470 Elf_External_Sym_Shndx *locsym_shndx;
7471 /* Buffer large enough to hold internal local symbols of any input
7473 Elf_Internal_Sym *internal_syms;
7474 /* Array large enough to hold a symbol index for each local symbol
7475 of any input BFD. */
7477 /* Array large enough to hold a section pointer for each local
7478 symbol of any input BFD. */
7479 asection **sections;
7480 /* Buffer for SHT_SYMTAB_SHNDX section. */
7481 Elf_External_Sym_Shndx *symshndxbuf;
7482 /* Number of STT_FILE syms seen. */
7483 size_t filesym_count;
7486 /* This struct is used to pass information to elf_link_output_extsym. */
7488 struct elf_outext_info
7491 bfd_boolean localsyms;
7492 bfd_boolean file_sym_done;
7493 struct elf_final_link_info *flinfo;
7497 /* Support for evaluating a complex relocation.
7499 Complex relocations are generalized, self-describing relocations. The
7500 implementation of them consists of two parts: complex symbols, and the
7501 relocations themselves.
7503 The relocations are use a reserved elf-wide relocation type code (R_RELC
7504 external / BFD_RELOC_RELC internal) and an encoding of relocation field
7505 information (start bit, end bit, word width, etc) into the addend. This
7506 information is extracted from CGEN-generated operand tables within gas.
7508 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC
7509 internal) representing prefix-notation expressions, including but not
7510 limited to those sorts of expressions normally encoded as addends in the
7511 addend field. The symbol mangling format is:
7514 | <unary-operator> ':' <node>
7515 | <binary-operator> ':' <node> ':' <node>
7518 <literal> := 's' <digits=N> ':' <N character symbol name>
7519 | 'S' <digits=N> ':' <N character section name>
7523 <binary-operator> := as in C
7524 <unary-operator> := as in C, plus "0-" for unambiguous negation. */
7527 set_symbol_value (bfd *bfd_with_globals,
7528 Elf_Internal_Sym *isymbuf,
7533 struct elf_link_hash_entry **sym_hashes;
7534 struct elf_link_hash_entry *h;
7535 size_t extsymoff = locsymcount;
7537 if (symidx < locsymcount)
7539 Elf_Internal_Sym *sym;
7541 sym = isymbuf + symidx;
7542 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
7544 /* It is a local symbol: move it to the
7545 "absolute" section and give it a value. */
7546 sym->st_shndx = SHN_ABS;
7547 sym->st_value = val;
7550 BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
7554 /* It is a global symbol: set its link type
7555 to "defined" and give it a value. */
7557 sym_hashes = elf_sym_hashes (bfd_with_globals);
7558 h = sym_hashes [symidx - extsymoff];
7559 while (h->root.type == bfd_link_hash_indirect
7560 || h->root.type == bfd_link_hash_warning)
7561 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7562 h->root.type = bfd_link_hash_defined;
7563 h->root.u.def.value = val;
7564 h->root.u.def.section = bfd_abs_section_ptr;
7568 resolve_symbol (const char *name,
7570 struct elf_final_link_info *flinfo,
7572 Elf_Internal_Sym *isymbuf,
7575 Elf_Internal_Sym *sym;
7576 struct bfd_link_hash_entry *global_entry;
7577 const char *candidate = NULL;
7578 Elf_Internal_Shdr *symtab_hdr;
7581 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
7583 for (i = 0; i < locsymcount; ++ i)
7587 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
7590 candidate = bfd_elf_string_from_elf_section (input_bfd,
7591 symtab_hdr->sh_link,
7594 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
7595 name, candidate, (unsigned long) sym->st_value);
7597 if (candidate && strcmp (candidate, name) == 0)
7599 asection *sec = flinfo->sections [i];
7601 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
7602 *result += sec->output_offset + sec->output_section->vma;
7604 printf ("Found symbol with value %8.8lx\n",
7605 (unsigned long) *result);
7611 /* Hmm, haven't found it yet. perhaps it is a global. */
7612 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
7613 FALSE, FALSE, TRUE);
7617 if (global_entry->type == bfd_link_hash_defined
7618 || global_entry->type == bfd_link_hash_defweak)
7620 *result = (global_entry->u.def.value
7621 + global_entry->u.def.section->output_section->vma
7622 + global_entry->u.def.section->output_offset);
7624 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
7625 global_entry->root.string, (unsigned long) *result);
7634 resolve_section (const char *name,
7641 for (curr = sections; curr; curr = curr->next)
7642 if (strcmp (curr->name, name) == 0)
7644 *result = curr->vma;
7648 /* Hmm. still haven't found it. try pseudo-section names. */
7649 for (curr = sections; curr; curr = curr->next)
7651 len = strlen (curr->name);
7652 if (len > strlen (name))
7655 if (strncmp (curr->name, name, len) == 0)
7657 if (strncmp (".end", name + len, 4) == 0)
7659 *result = curr->vma + curr->size;
7663 /* Insert more pseudo-section names here, if you like. */
7671 undefined_reference (const char *reftype, const char *name)
7673 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
7678 eval_symbol (bfd_vma *result,
7681 struct elf_final_link_info *flinfo,
7683 Elf_Internal_Sym *isymbuf,
7692 const char *sym = *symp;
7694 bfd_boolean symbol_is_section = FALSE;
7699 if (len < 1 || len > sizeof (symbuf))
7701 bfd_set_error (bfd_error_invalid_operation);
7714 *result = strtoul (sym, (char **) symp, 16);
7718 symbol_is_section = TRUE;
7721 symlen = strtol (sym, (char **) symp, 10);
7722 sym = *symp + 1; /* Skip the trailing ':'. */
7724 if (symend < sym || symlen + 1 > sizeof (symbuf))
7726 bfd_set_error (bfd_error_invalid_operation);
7730 memcpy (symbuf, sym, symlen);
7731 symbuf[symlen] = '\0';
7732 *symp = sym + symlen;
7734 /* Is it always possible, with complex symbols, that gas "mis-guessed"
7735 the symbol as a section, or vice-versa. so we're pretty liberal in our
7736 interpretation here; section means "try section first", not "must be a
7737 section", and likewise with symbol. */
7739 if (symbol_is_section)
7741 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result)
7742 && !resolve_symbol (symbuf, input_bfd, flinfo, result,
7743 isymbuf, locsymcount))
7745 undefined_reference ("section", symbuf);
7751 if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
7752 isymbuf, locsymcount)
7753 && !resolve_section (symbuf, flinfo->output_bfd->sections,
7756 undefined_reference ("symbol", symbuf);
7763 /* All that remains are operators. */
7765 #define UNARY_OP(op) \
7766 if (strncmp (sym, #op, strlen (#op)) == 0) \
7768 sym += strlen (#op); \
7772 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7773 isymbuf, locsymcount, signed_p)) \
7776 *result = op ((bfd_signed_vma) a); \
7782 #define BINARY_OP(op) \
7783 if (strncmp (sym, #op, strlen (#op)) == 0) \
7785 sym += strlen (#op); \
7789 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
7790 isymbuf, locsymcount, signed_p)) \
7793 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
7794 isymbuf, locsymcount, signed_p)) \
7797 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
7827 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
7828 bfd_set_error (bfd_error_invalid_operation);
7834 put_value (bfd_vma size,
7835 unsigned long chunksz,
7840 location += (size - chunksz);
7842 for (; size; size -= chunksz, location -= chunksz)
7847 bfd_put_8 (input_bfd, x, location);
7851 bfd_put_16 (input_bfd, x, location);
7855 bfd_put_32 (input_bfd, x, location);
7856 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
7862 bfd_put_64 (input_bfd, x, location);
7863 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
7876 get_value (bfd_vma size,
7877 unsigned long chunksz,
7884 /* Sanity checks. */
7885 BFD_ASSERT (chunksz <= sizeof (x)
7888 && (size % chunksz) == 0
7889 && input_bfd != NULL
7890 && location != NULL);
7892 if (chunksz == sizeof (x))
7894 BFD_ASSERT (size == chunksz);
7896 /* Make sure that we do not perform an undefined shift operation.
7897 We know that size == chunksz so there will only be one iteration
7898 of the loop below. */
7902 shift = 8 * chunksz;
7904 for (; size; size -= chunksz, location += chunksz)
7909 x = (x << shift) | bfd_get_8 (input_bfd, location);
7912 x = (x << shift) | bfd_get_16 (input_bfd, location);
7915 x = (x << shift) | bfd_get_32 (input_bfd, location);
7919 x = (x << shift) | bfd_get_64 (input_bfd, location);
7930 decode_complex_addend (unsigned long *start, /* in bits */
7931 unsigned long *oplen, /* in bits */
7932 unsigned long *len, /* in bits */
7933 unsigned long *wordsz, /* in bytes */
7934 unsigned long *chunksz, /* in bytes */
7935 unsigned long *lsb0_p,
7936 unsigned long *signed_p,
7937 unsigned long *trunc_p,
7938 unsigned long encoded)
7940 * start = encoded & 0x3F;
7941 * len = (encoded >> 6) & 0x3F;
7942 * oplen = (encoded >> 12) & 0x3F;
7943 * wordsz = (encoded >> 18) & 0xF;
7944 * chunksz = (encoded >> 22) & 0xF;
7945 * lsb0_p = (encoded >> 27) & 1;
7946 * signed_p = (encoded >> 28) & 1;
7947 * trunc_p = (encoded >> 29) & 1;
7950 bfd_reloc_status_type
7951 bfd_elf_perform_complex_relocation (bfd *input_bfd,
7952 asection *input_section ATTRIBUTE_UNUSED,
7954 Elf_Internal_Rela *rel,
7957 bfd_vma shift, x, mask;
7958 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
7959 bfd_reloc_status_type r;
7961 /* Perform this reloc, since it is complex.
7962 (this is not to say that it necessarily refers to a complex
7963 symbol; merely that it is a self-describing CGEN based reloc.
7964 i.e. the addend has the complete reloc information (bit start, end,
7965 word size, etc) encoded within it.). */
7967 decode_complex_addend (&start, &oplen, &len, &wordsz,
7968 &chunksz, &lsb0_p, &signed_p,
7969 &trunc_p, rel->r_addend);
7971 mask = (((1L << (len - 1)) - 1) << 1) | 1;
7974 shift = (start + 1) - len;
7976 shift = (8 * wordsz) - (start + len);
7978 /* FIXME: octets_per_byte. */
7979 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset);
7982 printf ("Doing complex reloc: "
7983 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
7984 "chunksz %ld, start %ld, len %ld, oplen %ld\n"
7985 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
7986 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
7987 oplen, (unsigned long) x, (unsigned long) mask,
7988 (unsigned long) relocation);
7993 /* Now do an overflow check. */
7994 r = bfd_check_overflow ((signed_p
7995 ? complain_overflow_signed
7996 : complain_overflow_unsigned),
7997 len, 0, (8 * wordsz),
8001 x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
8004 printf (" relocation: %8.8lx\n"
8005 " shifted mask: %8.8lx\n"
8006 " shifted/masked reloc: %8.8lx\n"
8007 " result: %8.8lx\n",
8008 (unsigned long) relocation, (unsigned long) (mask << shift),
8009 (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
8011 /* FIXME: octets_per_byte. */
8012 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset);
8016 /* qsort comparison functions sorting external relocs by r_offset. */
8019 cmp_ext32l_r_offset (const void *p, const void *q)
8026 const union aligned32 *a
8027 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8028 const union aligned32 *b
8029 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8031 uint32_t aval = ( (uint32_t) a->c[0]
8032 | (uint32_t) a->c[1] << 8
8033 | (uint32_t) a->c[2] << 16
8034 | (uint32_t) a->c[3] << 24);
8035 uint32_t bval = ( (uint32_t) b->c[0]
8036 | (uint32_t) b->c[1] << 8
8037 | (uint32_t) b->c[2] << 16
8038 | (uint32_t) b->c[3] << 24);
8041 else if (aval > bval)
8047 cmp_ext32b_r_offset (const void *p, const void *q)
8054 const union aligned32 *a
8055 = (const union aligned32 *) ((const Elf32_External_Rel *) p)->r_offset;
8056 const union aligned32 *b
8057 = (const union aligned32 *) ((const Elf32_External_Rel *) q)->r_offset;
8059 uint32_t aval = ( (uint32_t) a->c[0] << 24
8060 | (uint32_t) a->c[1] << 16
8061 | (uint32_t) a->c[2] << 8
8062 | (uint32_t) a->c[3]);
8063 uint32_t bval = ( (uint32_t) b->c[0] << 24
8064 | (uint32_t) b->c[1] << 16
8065 | (uint32_t) b->c[2] << 8
8066 | (uint32_t) b->c[3]);
8069 else if (aval > bval)
8074 #ifdef BFD_HOST_64_BIT
8076 cmp_ext64l_r_offset (const void *p, const void *q)
8083 const union aligned64 *a
8084 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8085 const union aligned64 *b
8086 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8088 uint64_t aval = ( (uint64_t) a->c[0]
8089 | (uint64_t) a->c[1] << 8
8090 | (uint64_t) a->c[2] << 16
8091 | (uint64_t) a->c[3] << 24
8092 | (uint64_t) a->c[4] << 32
8093 | (uint64_t) a->c[5] << 40
8094 | (uint64_t) a->c[6] << 48
8095 | (uint64_t) a->c[7] << 56);
8096 uint64_t bval = ( (uint64_t) b->c[0]
8097 | (uint64_t) b->c[1] << 8
8098 | (uint64_t) b->c[2] << 16
8099 | (uint64_t) b->c[3] << 24
8100 | (uint64_t) b->c[4] << 32
8101 | (uint64_t) b->c[5] << 40
8102 | (uint64_t) b->c[6] << 48
8103 | (uint64_t) b->c[7] << 56);
8106 else if (aval > bval)
8112 cmp_ext64b_r_offset (const void *p, const void *q)
8119 const union aligned64 *a
8120 = (const union aligned64 *) ((const Elf64_External_Rel *) p)->r_offset;
8121 const union aligned64 *b
8122 = (const union aligned64 *) ((const Elf64_External_Rel *) q)->r_offset;
8124 uint64_t aval = ( (uint64_t) a->c[0] << 56
8125 | (uint64_t) a->c[1] << 48
8126 | (uint64_t) a->c[2] << 40
8127 | (uint64_t) a->c[3] << 32
8128 | (uint64_t) a->c[4] << 24
8129 | (uint64_t) a->c[5] << 16
8130 | (uint64_t) a->c[6] << 8
8131 | (uint64_t) a->c[7]);
8132 uint64_t bval = ( (uint64_t) b->c[0] << 56
8133 | (uint64_t) b->c[1] << 48
8134 | (uint64_t) b->c[2] << 40
8135 | (uint64_t) b->c[3] << 32
8136 | (uint64_t) b->c[4] << 24
8137 | (uint64_t) b->c[5] << 16
8138 | (uint64_t) b->c[6] << 8
8139 | (uint64_t) b->c[7]);
8142 else if (aval > bval)
8148 /* When performing a relocatable link, the input relocations are
8149 preserved. But, if they reference global symbols, the indices
8150 referenced must be updated. Update all the relocations found in
8154 elf_link_adjust_relocs (bfd *abfd,
8155 struct bfd_elf_section_reloc_data *reldata,
8159 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8161 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8162 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8163 bfd_vma r_type_mask;
8165 unsigned int count = reldata->count;
8166 struct elf_link_hash_entry **rel_hash = reldata->hashes;
8168 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
8170 swap_in = bed->s->swap_reloc_in;
8171 swap_out = bed->s->swap_reloc_out;
8173 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
8175 swap_in = bed->s->swap_reloca_in;
8176 swap_out = bed->s->swap_reloca_out;
8181 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
8184 if (bed->s->arch_size == 32)
8191 r_type_mask = 0xffffffff;
8195 erela = reldata->hdr->contents;
8196 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
8198 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
8201 if (*rel_hash == NULL)
8204 BFD_ASSERT ((*rel_hash)->indx >= 0);
8206 (*swap_in) (abfd, erela, irela);
8207 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
8208 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
8209 | (irela[j].r_info & r_type_mask));
8210 (*swap_out) (abfd, irela, erela);
8215 int (*compare) (const void *, const void *);
8217 if (bed->s->arch_size == 32)
8219 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8220 compare = cmp_ext32l_r_offset;
8221 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8222 compare = cmp_ext32b_r_offset;
8228 #ifdef BFD_HOST_64_BIT
8229 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
8230 compare = cmp_ext64l_r_offset;
8231 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
8232 compare = cmp_ext64b_r_offset;
8237 qsort (reldata->hdr->contents, count, reldata->hdr->sh_entsize, compare);
8238 free (reldata->hashes);
8239 reldata->hashes = NULL;
8243 struct elf_link_sort_rela
8249 enum elf_reloc_type_class type;
8250 /* We use this as an array of size int_rels_per_ext_rel. */
8251 Elf_Internal_Rela rela[1];
8255 elf_link_sort_cmp1 (const void *A, const void *B)
8257 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8258 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8259 int relativea, relativeb;
8261 relativea = a->type == reloc_class_relative;
8262 relativeb = b->type == reloc_class_relative;
8264 if (relativea < relativeb)
8266 if (relativea > relativeb)
8268 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
8270 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
8272 if (a->rela->r_offset < b->rela->r_offset)
8274 if (a->rela->r_offset > b->rela->r_offset)
8280 elf_link_sort_cmp2 (const void *A, const void *B)
8282 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
8283 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
8285 if (a->type < b->type)
8287 if (a->type > b->type)
8289 if (a->u.offset < b->u.offset)
8291 if (a->u.offset > b->u.offset)
8293 if (a->rela->r_offset < b->rela->r_offset)
8295 if (a->rela->r_offset > b->rela->r_offset)
8301 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
8303 asection *dynamic_relocs;
8306 bfd_size_type count, size;
8307 size_t i, ret, sort_elt, ext_size;
8308 bfd_byte *sort, *s_non_relative, *p;
8309 struct elf_link_sort_rela *sq;
8310 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8311 int i2e = bed->s->int_rels_per_ext_rel;
8312 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
8313 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
8314 struct bfd_link_order *lo;
8316 bfd_boolean use_rela;
8318 /* Find a dynamic reloc section. */
8319 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
8320 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
8321 if (rela_dyn != NULL && rela_dyn->size > 0
8322 && rel_dyn != NULL && rel_dyn->size > 0)
8324 bfd_boolean use_rela_initialised = FALSE;
8326 /* This is just here to stop gcc from complaining.
8327 It's initialization checking code is not perfect. */
8330 /* Both sections are present. Examine the sizes
8331 of the indirect sections to help us choose. */
8332 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8333 if (lo->type == bfd_indirect_link_order)
8335 asection *o = lo->u.indirect.section;
8337 if ((o->size % bed->s->sizeof_rela) == 0)
8339 if ((o->size % bed->s->sizeof_rel) == 0)
8340 /* Section size is divisible by both rel and rela sizes.
8341 It is of no help to us. */
8345 /* Section size is only divisible by rela. */
8346 if (use_rela_initialised && (use_rela == FALSE))
8349 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8350 bfd_set_error (bfd_error_invalid_operation);
8356 use_rela_initialised = TRUE;
8360 else if ((o->size % bed->s->sizeof_rel) == 0)
8362 /* Section size is only divisible by rel. */
8363 if (use_rela_initialised && (use_rela == TRUE))
8366 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8367 bfd_set_error (bfd_error_invalid_operation);
8373 use_rela_initialised = TRUE;
8378 /* The section size is not divisible by either - something is wrong. */
8380 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8381 bfd_set_error (bfd_error_invalid_operation);
8386 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
8387 if (lo->type == bfd_indirect_link_order)
8389 asection *o = lo->u.indirect.section;
8391 if ((o->size % bed->s->sizeof_rela) == 0)
8393 if ((o->size % bed->s->sizeof_rel) == 0)
8394 /* Section size is divisible by both rel and rela sizes.
8395 It is of no help to us. */
8399 /* Section size is only divisible by rela. */
8400 if (use_rela_initialised && (use_rela == FALSE))
8403 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8404 bfd_set_error (bfd_error_invalid_operation);
8410 use_rela_initialised = TRUE;
8414 else if ((o->size % bed->s->sizeof_rel) == 0)
8416 /* Section size is only divisible by rel. */
8417 if (use_rela_initialised && (use_rela == TRUE))
8420 (_("%B: Unable to sort relocs - they are in more than one size"), abfd);
8421 bfd_set_error (bfd_error_invalid_operation);
8427 use_rela_initialised = TRUE;
8432 /* The section size is not divisible by either - something is wrong. */
8434 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd);
8435 bfd_set_error (bfd_error_invalid_operation);
8440 if (! use_rela_initialised)
8444 else if (rela_dyn != NULL && rela_dyn->size > 0)
8446 else if (rel_dyn != NULL && rel_dyn->size > 0)
8453 dynamic_relocs = rela_dyn;
8454 ext_size = bed->s->sizeof_rela;
8455 swap_in = bed->s->swap_reloca_in;
8456 swap_out = bed->s->swap_reloca_out;
8460 dynamic_relocs = rel_dyn;
8461 ext_size = bed->s->sizeof_rel;
8462 swap_in = bed->s->swap_reloc_in;
8463 swap_out = bed->s->swap_reloc_out;
8467 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8468 if (lo->type == bfd_indirect_link_order)
8469 size += lo->u.indirect.section->size;
8471 if (size != dynamic_relocs->size)
8474 sort_elt = (sizeof (struct elf_link_sort_rela)
8475 + (i2e - 1) * sizeof (Elf_Internal_Rela));
8477 count = dynamic_relocs->size / ext_size;
8480 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
8484 (*info->callbacks->warning)
8485 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0);
8489 if (bed->s->arch_size == 32)
8490 r_sym_mask = ~(bfd_vma) 0xff;
8492 r_sym_mask = ~(bfd_vma) 0xffffffff;
8494 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8495 if (lo->type == bfd_indirect_link_order)
8497 bfd_byte *erel, *erelend;
8498 asection *o = lo->u.indirect.section;
8500 if (o->contents == NULL && o->size != 0)
8502 /* This is a reloc section that is being handled as a normal
8503 section. See bfd_section_from_shdr. We can't combine
8504 relocs in this case. */
8509 erelend = o->contents + o->size;
8510 /* FIXME: octets_per_byte. */
8511 p = sort + o->output_offset / ext_size * sort_elt;
8513 while (erel < erelend)
8515 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8517 (*swap_in) (abfd, erel, s->rela);
8518 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
8519 s->u.sym_mask = r_sym_mask;
8525 qsort (sort, count, sort_elt, elf_link_sort_cmp1);
8527 for (i = 0, p = sort; i < count; i++, p += sort_elt)
8529 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8530 if (s->type != reloc_class_relative)
8536 sq = (struct elf_link_sort_rela *) s_non_relative;
8537 for (; i < count; i++, p += sort_elt)
8539 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
8540 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
8542 sp->u.offset = sq->rela->r_offset;
8545 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
8547 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
8548 if (lo->type == bfd_indirect_link_order)
8550 bfd_byte *erel, *erelend;
8551 asection *o = lo->u.indirect.section;
8554 erelend = o->contents + o->size;
8555 /* FIXME: octets_per_byte. */
8556 p = sort + o->output_offset / ext_size * sort_elt;
8557 while (erel < erelend)
8559 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
8560 (*swap_out) (abfd, s->rela, erel);
8567 *psec = dynamic_relocs;
8571 /* Add a symbol to the output symbol string table. */
8574 elf_link_output_symstrtab (struct elf_final_link_info *flinfo,
8576 Elf_Internal_Sym *elfsym,
8577 asection *input_sec,
8578 struct elf_link_hash_entry *h)
8580 int (*output_symbol_hook)
8581 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
8582 struct elf_link_hash_entry *);
8583 struct elf_link_hash_table *hash_table;
8584 const struct elf_backend_data *bed;
8585 bfd_size_type strtabsize;
8587 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8589 bed = get_elf_backend_data (flinfo->output_bfd);
8590 output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
8591 if (output_symbol_hook != NULL)
8593 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
8600 || (input_sec->flags & SEC_EXCLUDE))
8601 elfsym->st_name = (unsigned long) -1;
8604 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8605 to get the final offset for st_name. */
8607 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
8609 if (elfsym->st_name == (unsigned long) -1)
8613 hash_table = elf_hash_table (flinfo->info);
8614 strtabsize = hash_table->strtabsize;
8615 if (strtabsize <= hash_table->strtabcount)
8617 strtabsize += strtabsize;
8618 hash_table->strtabsize = strtabsize;
8619 strtabsize *= sizeof (*hash_table->strtab);
8621 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
8623 if (hash_table->strtab == NULL)
8626 hash_table->strtab[hash_table->strtabcount].sym = *elfsym;
8627 hash_table->strtab[hash_table->strtabcount].dest_index
8628 = hash_table->strtabcount;
8629 hash_table->strtab[hash_table->strtabcount].destshndx_index
8630 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0;
8632 bfd_get_symcount (flinfo->output_bfd) += 1;
8633 hash_table->strtabcount += 1;
8638 /* Swap symbols out to the symbol table and flush the output symbols to
8642 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
8644 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
8645 bfd_size_type amt, i;
8646 const struct elf_backend_data *bed;
8648 Elf_Internal_Shdr *hdr;
8652 if (!hash_table->strtabcount)
8655 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
8657 bed = get_elf_backend_data (flinfo->output_bfd);
8659 amt = bed->s->sizeof_sym * hash_table->strtabcount;
8660 symbuf = (bfd_byte *) bfd_malloc (amt);
8664 if (flinfo->symshndxbuf)
8666 amt = (sizeof (Elf_External_Sym_Shndx)
8667 * (bfd_get_symcount (flinfo->output_bfd)));
8668 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
8669 if (flinfo->symshndxbuf == NULL)
8676 for (i = 0; i < hash_table->strtabcount; i++)
8678 struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
8679 if (elfsym->sym.st_name == (unsigned long) -1)
8680 elfsym->sym.st_name = 0;
8683 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
8684 elfsym->sym.st_name);
8685 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
8686 ((bfd_byte *) symbuf
8687 + (elfsym->dest_index
8688 * bed->s->sizeof_sym)),
8689 (flinfo->symshndxbuf
8690 + elfsym->destshndx_index));
8693 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
8694 pos = hdr->sh_offset + hdr->sh_size;
8695 amt = hash_table->strtabcount * bed->s->sizeof_sym;
8696 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
8697 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
8699 hdr->sh_size += amt;
8707 free (hash_table->strtab);
8708 hash_table->strtab = NULL;
8713 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
8716 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
8718 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
8719 && sym->st_shndx < SHN_LORESERVE)
8721 /* The gABI doesn't support dynamic symbols in output sections
8723 (*_bfd_error_handler)
8724 (_("%B: Too many sections: %d (>= %d)"),
8725 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
8726 bfd_set_error (bfd_error_nonrepresentable_section);
8732 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
8733 allowing an unsatisfied unversioned symbol in the DSO to match a
8734 versioned symbol that would normally require an explicit version.
8735 We also handle the case that a DSO references a hidden symbol
8736 which may be satisfied by a versioned symbol in another DSO. */
8739 elf_link_check_versioned_symbol (struct bfd_link_info *info,
8740 const struct elf_backend_data *bed,
8741 struct elf_link_hash_entry *h)
8744 struct elf_link_loaded_list *loaded;
8746 if (!is_elf_hash_table (info->hash))
8749 /* Check indirect symbol. */
8750 while (h->root.type == bfd_link_hash_indirect)
8751 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8753 switch (h->root.type)
8759 case bfd_link_hash_undefined:
8760 case bfd_link_hash_undefweak:
8761 abfd = h->root.u.undef.abfd;
8762 if ((abfd->flags & DYNAMIC) == 0
8763 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
8767 case bfd_link_hash_defined:
8768 case bfd_link_hash_defweak:
8769 abfd = h->root.u.def.section->owner;
8772 case bfd_link_hash_common:
8773 abfd = h->root.u.c.p->section->owner;
8776 BFD_ASSERT (abfd != NULL);
8778 for (loaded = elf_hash_table (info)->loaded;
8780 loaded = loaded->next)
8783 Elf_Internal_Shdr *hdr;
8784 bfd_size_type symcount;
8785 bfd_size_type extsymcount;
8786 bfd_size_type extsymoff;
8787 Elf_Internal_Shdr *versymhdr;
8788 Elf_Internal_Sym *isym;
8789 Elf_Internal_Sym *isymend;
8790 Elf_Internal_Sym *isymbuf;
8791 Elf_External_Versym *ever;
8792 Elf_External_Versym *extversym;
8794 input = loaded->abfd;
8796 /* We check each DSO for a possible hidden versioned definition. */
8798 || (input->flags & DYNAMIC) == 0
8799 || elf_dynversym (input) == 0)
8802 hdr = &elf_tdata (input)->dynsymtab_hdr;
8804 symcount = hdr->sh_size / bed->s->sizeof_sym;
8805 if (elf_bad_symtab (input))
8807 extsymcount = symcount;
8812 extsymcount = symcount - hdr->sh_info;
8813 extsymoff = hdr->sh_info;
8816 if (extsymcount == 0)
8819 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
8821 if (isymbuf == NULL)
8824 /* Read in any version definitions. */
8825 versymhdr = &elf_tdata (input)->dynversym_hdr;
8826 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
8827 if (extversym == NULL)
8830 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
8831 || (bfd_bread (extversym, versymhdr->sh_size, input)
8832 != versymhdr->sh_size))
8840 ever = extversym + extsymoff;
8841 isymend = isymbuf + extsymcount;
8842 for (isym = isymbuf; isym < isymend; isym++, ever++)
8845 Elf_Internal_Versym iver;
8846 unsigned short version_index;
8848 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
8849 || isym->st_shndx == SHN_UNDEF)
8852 name = bfd_elf_string_from_elf_section (input,
8855 if (strcmp (name, h->root.root.string) != 0)
8858 _bfd_elf_swap_versym_in (input, ever, &iver);
8860 if ((iver.vs_vers & VERSYM_HIDDEN) == 0
8862 && h->forced_local))
8864 /* If we have a non-hidden versioned sym, then it should
8865 have provided a definition for the undefined sym unless
8866 it is defined in a non-shared object and forced local.
8871 version_index = iver.vs_vers & VERSYM_VERSION;
8872 if (version_index == 1 || version_index == 2)
8874 /* This is the base or first version. We can use it. */
8888 /* Add an external symbol to the symbol table. This is called from
8889 the hash table traversal routine. When generating a shared object,
8890 we go through the symbol table twice. The first time we output
8891 anything that might have been forced to local scope in a version
8892 script. The second time we output the symbols that are still
8896 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
8898 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
8899 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
8900 struct elf_final_link_info *flinfo = eoinfo->flinfo;
8902 Elf_Internal_Sym sym;
8903 asection *input_sec;
8904 const struct elf_backend_data *bed;
8908 if (h->root.type == bfd_link_hash_warning)
8910 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8911 if (h->root.type == bfd_link_hash_new)
8915 /* Decide whether to output this symbol in this pass. */
8916 if (eoinfo->localsyms)
8918 if (!h->forced_local)
8923 if (h->forced_local)
8927 bed = get_elf_backend_data (flinfo->output_bfd);
8929 if (h->root.type == bfd_link_hash_undefined)
8931 /* If we have an undefined symbol reference here then it must have
8932 come from a shared library that is being linked in. (Undefined
8933 references in regular files have already been handled unless
8934 they are in unreferenced sections which are removed by garbage
8936 bfd_boolean ignore_undef = FALSE;
8938 /* Some symbols may be special in that the fact that they're
8939 undefined can be safely ignored - let backend determine that. */
8940 if (bed->elf_backend_ignore_undef_symbol)
8941 ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
8943 /* If we are reporting errors for this situation then do so now. */
8946 && (!h->ref_regular || flinfo->info->gc_sections)
8947 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
8948 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
8950 if (!(flinfo->info->callbacks->undefined_symbol
8951 (flinfo->info, h->root.root.string,
8952 h->ref_regular ? NULL : h->root.u.undef.abfd,
8954 (flinfo->info->unresolved_syms_in_shared_libs
8955 == RM_GENERATE_ERROR))))
8957 bfd_set_error (bfd_error_bad_value);
8958 eoinfo->failed = TRUE;
8964 /* We should also warn if a forced local symbol is referenced from
8965 shared libraries. */
8966 if (flinfo->info->executable
8971 && h->ref_dynamic_nonweak
8972 && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
8976 struct elf_link_hash_entry *hi = h;
8978 /* Check indirect symbol. */
8979 while (hi->root.type == bfd_link_hash_indirect)
8980 hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
8982 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
8983 msg = _("%B: internal symbol `%s' in %B is referenced by DSO");
8984 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
8985 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO");
8987 msg = _("%B: local symbol `%s' in %B is referenced by DSO");
8988 def_bfd = flinfo->output_bfd;
8989 if (hi->root.u.def.section != bfd_abs_section_ptr)
8990 def_bfd = hi->root.u.def.section->owner;
8991 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd,
8992 h->root.root.string);
8993 bfd_set_error (bfd_error_bad_value);
8994 eoinfo->failed = TRUE;
8998 /* We don't want to output symbols that have never been mentioned by
8999 a regular file, or that we have been told to strip. However, if
9000 h->indx is set to -2, the symbol is used by a reloc and we must
9005 else if ((h->def_dynamic
9007 || h->root.type == bfd_link_hash_new)
9011 else if (flinfo->info->strip == strip_all)
9013 else if (flinfo->info->strip == strip_some
9014 && bfd_hash_lookup (flinfo->info->keep_hash,
9015 h->root.root.string, FALSE, FALSE) == NULL)
9017 else if ((h->root.type == bfd_link_hash_defined
9018 || h->root.type == bfd_link_hash_defweak)
9019 && ((flinfo->info->strip_discarded
9020 && discarded_section (h->root.u.def.section))
9021 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
9022 && h->root.u.def.section->owner != NULL
9023 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
9025 else if ((h->root.type == bfd_link_hash_undefined
9026 || h->root.type == bfd_link_hash_undefweak)
9027 && h->root.u.undef.abfd != NULL
9028 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
9031 /* If we're stripping it, and it's not a dynamic symbol, there's
9032 nothing else to do. However, if it is a forced local symbol or
9033 an ifunc symbol we need to give the backend finish_dynamic_symbol
9034 function a chance to make it dynamic. */
9037 && h->type != STT_GNU_IFUNC
9038 && !h->forced_local)
9042 sym.st_size = h->size;
9043 sym.st_other = h->other;
9044 if (h->forced_local)
9046 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type);
9047 /* Turn off visibility on local symbol. */
9048 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
9050 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
9051 else if (h->unique_global && h->def_regular)
9052 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type);
9053 else if (h->root.type == bfd_link_hash_undefweak
9054 || h->root.type == bfd_link_hash_defweak)
9055 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type);
9057 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type);
9058 sym.st_target_internal = h->target_internal;
9060 switch (h->root.type)
9063 case bfd_link_hash_new:
9064 case bfd_link_hash_warning:
9068 case bfd_link_hash_undefined:
9069 case bfd_link_hash_undefweak:
9070 input_sec = bfd_und_section_ptr;
9071 sym.st_shndx = SHN_UNDEF;
9074 case bfd_link_hash_defined:
9075 case bfd_link_hash_defweak:
9077 input_sec = h->root.u.def.section;
9078 if (input_sec->output_section != NULL)
9081 _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
9082 input_sec->output_section);
9083 if (sym.st_shndx == SHN_BAD)
9085 (*_bfd_error_handler)
9086 (_("%B: could not find output section %A for input section %A"),
9087 flinfo->output_bfd, input_sec->output_section, input_sec);
9088 bfd_set_error (bfd_error_nonrepresentable_section);
9089 eoinfo->failed = TRUE;
9093 /* ELF symbols in relocatable files are section relative,
9094 but in nonrelocatable files they are virtual
9096 sym.st_value = h->root.u.def.value + input_sec->output_offset;
9097 if (!flinfo->info->relocatable)
9099 sym.st_value += input_sec->output_section->vma;
9100 if (h->type == STT_TLS)
9102 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
9103 if (tls_sec != NULL)
9104 sym.st_value -= tls_sec->vma;
9110 BFD_ASSERT (input_sec->owner == NULL
9111 || (input_sec->owner->flags & DYNAMIC) != 0);
9112 sym.st_shndx = SHN_UNDEF;
9113 input_sec = bfd_und_section_ptr;
9118 case bfd_link_hash_common:
9119 input_sec = h->root.u.c.p->section;
9120 sym.st_shndx = bed->common_section_index (input_sec);
9121 sym.st_value = 1 << h->root.u.c.p->alignment_power;
9124 case bfd_link_hash_indirect:
9125 /* These symbols are created by symbol versioning. They point
9126 to the decorated version of the name. For example, if the
9127 symbol foo@@GNU_1.2 is the default, which should be used when
9128 foo is used with no version, then we add an indirect symbol
9129 foo which points to foo@@GNU_1.2. We ignore these symbols,
9130 since the indirected symbol is already in the hash table. */
9134 /* Give the processor backend a chance to tweak the symbol value,
9135 and also to finish up anything that needs to be done for this
9136 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
9137 forced local syms when non-shared is due to a historical quirk.
9138 STT_GNU_IFUNC symbol must go through PLT. */
9139 if ((h->type == STT_GNU_IFUNC
9141 && !flinfo->info->relocatable)
9142 || ((h->dynindx != -1
9144 && ((flinfo->info->shared
9145 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9146 || h->root.type != bfd_link_hash_undefweak))
9147 || !h->forced_local)
9148 && elf_hash_table (flinfo->info)->dynamic_sections_created))
9150 if (! ((*bed->elf_backend_finish_dynamic_symbol)
9151 (flinfo->output_bfd, flinfo->info, h, &sym)))
9153 eoinfo->failed = TRUE;
9158 /* If we are marking the symbol as undefined, and there are no
9159 non-weak references to this symbol from a regular object, then
9160 mark the symbol as weak undefined; if there are non-weak
9161 references, mark the symbol as strong. We can't do this earlier,
9162 because it might not be marked as undefined until the
9163 finish_dynamic_symbol routine gets through with it. */
9164 if (sym.st_shndx == SHN_UNDEF
9166 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
9167 || ELF_ST_BIND (sym.st_info) == STB_WEAK))
9170 unsigned int type = ELF_ST_TYPE (sym.st_info);
9172 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
9173 if (type == STT_GNU_IFUNC)
9176 if (h->ref_regular_nonweak)
9177 bindtype = STB_GLOBAL;
9179 bindtype = STB_WEAK;
9180 sym.st_info = ELF_ST_INFO (bindtype, type);
9183 /* If this is a symbol defined in a dynamic library, don't use the
9184 symbol size from the dynamic library. Relinking an executable
9185 against a new library may introduce gratuitous changes in the
9186 executable's symbols if we keep the size. */
9187 if (sym.st_shndx == SHN_UNDEF
9192 /* If a non-weak symbol with non-default visibility is not defined
9193 locally, it is a fatal error. */
9194 if (!flinfo->info->relocatable
9195 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
9196 && ELF_ST_BIND (sym.st_info) != STB_WEAK
9197 && h->root.type == bfd_link_hash_undefined
9202 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
9203 msg = _("%B: protected symbol `%s' isn't defined");
9204 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
9205 msg = _("%B: internal symbol `%s' isn't defined");
9207 msg = _("%B: hidden symbol `%s' isn't defined");
9208 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string);
9209 bfd_set_error (bfd_error_bad_value);
9210 eoinfo->failed = TRUE;
9214 /* If this symbol should be put in the .dynsym section, then put it
9215 there now. We already know the symbol index. We also fill in
9216 the entry in the .hash section. */
9217 if (flinfo->dynsym_sec != NULL
9219 && elf_hash_table (flinfo->info)->dynamic_sections_created)
9223 /* Since there is no version information in the dynamic string,
9224 if there is no version info in symbol version section, we will
9225 have a run-time problem if not linking executable, referenced
9226 by shared library, or not locally defined. */
9227 if (h->verinfo.verdef == NULL
9228 && (!flinfo->info->executable
9230 || !h->def_regular))
9232 char *p = strrchr (h->root.root.string, ELF_VER_CHR);
9234 if (p && p [1] != '\0')
9236 (*_bfd_error_handler)
9237 (_("%B: No symbol version section for versioned symbol `%s'"),
9238 flinfo->output_bfd, h->root.root.string);
9239 eoinfo->failed = TRUE;
9244 sym.st_name = h->dynstr_index;
9245 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym;
9246 if (!check_dynsym (flinfo->output_bfd, &sym))
9248 eoinfo->failed = TRUE;
9251 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
9253 if (flinfo->hash_sec != NULL)
9255 size_t hash_entry_size;
9256 bfd_byte *bucketpos;
9261 bucketcount = elf_hash_table (flinfo->info)->bucketcount;
9262 bucket = h->u.elf_hash_value % bucketcount;
9265 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
9266 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
9267 + (bucket + 2) * hash_entry_size);
9268 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
9269 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
9271 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
9272 ((bfd_byte *) flinfo->hash_sec->contents
9273 + (bucketcount + 2 + h->dynindx) * hash_entry_size));
9276 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
9278 Elf_Internal_Versym iversym;
9279 Elf_External_Versym *eversym;
9281 if (!h->def_regular)
9283 if (h->verinfo.verdef == NULL
9284 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
9285 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
9286 iversym.vs_vers = 0;
9288 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
9292 if (h->verinfo.vertree == NULL)
9293 iversym.vs_vers = 1;
9295 iversym.vs_vers = h->verinfo.vertree->vernum + 1;
9296 if (flinfo->info->create_default_symver)
9301 iversym.vs_vers |= VERSYM_HIDDEN;
9303 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
9304 eversym += h->dynindx;
9305 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
9309 /* If the symbol is undefined, and we didn't output it to .dynsym,
9310 strip it from .symtab too. Obviously we can't do this for
9311 relocatable output or when needed for --emit-relocs. */
9312 else if (input_sec == bfd_und_section_ptr
9314 && !flinfo->info->relocatable)
9316 /* Also strip others that we couldn't earlier due to dynamic symbol
9320 if ((input_sec->flags & SEC_EXCLUDE) != 0)
9323 /* Output a FILE symbol so that following locals are not associated
9324 with the wrong input file. We need one for forced local symbols
9325 if we've seen more than one FILE symbol or when we have exactly
9326 one FILE symbol but global symbols are present in a file other
9327 than the one with the FILE symbol. We also need one if linker
9328 defined symbols are present. In practice these conditions are
9329 always met, so just emit the FILE symbol unconditionally. */
9330 if (eoinfo->localsyms
9331 && !eoinfo->file_sym_done
9332 && eoinfo->flinfo->filesym_count != 0)
9334 Elf_Internal_Sym fsym;
9336 memset (&fsym, 0, sizeof (fsym));
9337 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9338 fsym.st_shndx = SHN_ABS;
9339 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
9340 bfd_und_section_ptr, NULL))
9343 eoinfo->file_sym_done = TRUE;
9346 indx = bfd_get_symcount (flinfo->output_bfd);
9347 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
9351 eoinfo->failed = TRUE;
9356 else if (h->indx == -2)
9362 /* Return TRUE if special handling is done for relocs in SEC against
9363 symbols defined in discarded sections. */
9366 elf_section_ignore_discarded_relocs (asection *sec)
9368 const struct elf_backend_data *bed;
9370 switch (sec->sec_info_type)
9372 case SEC_INFO_TYPE_STABS:
9373 case SEC_INFO_TYPE_EH_FRAME:
9374 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
9380 bed = get_elf_backend_data (sec->owner);
9381 if (bed->elf_backend_ignore_discarded_relocs != NULL
9382 && (*bed->elf_backend_ignore_discarded_relocs) (sec))
9388 /* Return a mask saying how ld should treat relocations in SEC against
9389 symbols defined in discarded sections. If this function returns
9390 COMPLAIN set, ld will issue a warning message. If this function
9391 returns PRETEND set, and the discarded section was link-once and the
9392 same size as the kept link-once section, ld will pretend that the
9393 symbol was actually defined in the kept section. Otherwise ld will
9394 zero the reloc (at least that is the intent, but some cooperation by
9395 the target dependent code is needed, particularly for REL targets). */
9398 _bfd_elf_default_action_discarded (asection *sec)
9400 if (sec->flags & SEC_DEBUGGING)
9403 if (strcmp (".eh_frame", sec->name) == 0)
9406 if (strcmp (".gcc_except_table", sec->name) == 0)
9409 return COMPLAIN | PRETEND;
9412 /* Find a match between a section and a member of a section group. */
9415 match_group_member (asection *sec, asection *group,
9416 struct bfd_link_info *info)
9418 asection *first = elf_next_in_group (group);
9419 asection *s = first;
9423 if (bfd_elf_match_symbols_in_sections (s, sec, info))
9426 s = elf_next_in_group (s);
9434 /* Check if the kept section of a discarded section SEC can be used
9435 to replace it. Return the replacement if it is OK. Otherwise return
9439 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
9443 kept = sec->kept_section;
9446 if ((kept->flags & SEC_GROUP) != 0)
9447 kept = match_group_member (sec, kept, info);
9449 && ((sec->rawsize != 0 ? sec->rawsize : sec->size)
9450 != (kept->rawsize != 0 ? kept->rawsize : kept->size)))
9452 sec->kept_section = kept;
9457 /* Link an input file into the linker output file. This function
9458 handles all the sections and relocations of the input file at once.
9459 This is so that we only have to read the local symbols once, and
9460 don't have to keep them in memory. */
9463 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
9465 int (*relocate_section)
9466 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
9467 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
9469 Elf_Internal_Shdr *symtab_hdr;
9472 Elf_Internal_Sym *isymbuf;
9473 Elf_Internal_Sym *isym;
9474 Elf_Internal_Sym *isymend;
9476 asection **ppsection;
9478 const struct elf_backend_data *bed;
9479 struct elf_link_hash_entry **sym_hashes;
9480 bfd_size_type address_size;
9481 bfd_vma r_type_mask;
9483 bfd_boolean have_file_sym = FALSE;
9485 output_bfd = flinfo->output_bfd;
9486 bed = get_elf_backend_data (output_bfd);
9487 relocate_section = bed->elf_backend_relocate_section;
9489 /* If this is a dynamic object, we don't want to do anything here:
9490 we don't want the local symbols, and we don't want the section
9492 if ((input_bfd->flags & DYNAMIC) != 0)
9495 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9496 if (elf_bad_symtab (input_bfd))
9498 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
9503 locsymcount = symtab_hdr->sh_info;
9504 extsymoff = symtab_hdr->sh_info;
9507 /* Read the local symbols. */
9508 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
9509 if (isymbuf == NULL && locsymcount != 0)
9511 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
9512 flinfo->internal_syms,
9513 flinfo->external_syms,
9514 flinfo->locsym_shndx);
9515 if (isymbuf == NULL)
9519 /* Find local symbol sections and adjust values of symbols in
9520 SEC_MERGE sections. Write out those local symbols we know are
9521 going into the output file. */
9522 isymend = isymbuf + locsymcount;
9523 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
9525 isym++, pindex++, ppsection++)
9529 Elf_Internal_Sym osym;
9535 if (elf_bad_symtab (input_bfd))
9537 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
9544 if (isym->st_shndx == SHN_UNDEF)
9545 isec = bfd_und_section_ptr;
9546 else if (isym->st_shndx == SHN_ABS)
9547 isec = bfd_abs_section_ptr;
9548 else if (isym->st_shndx == SHN_COMMON)
9549 isec = bfd_com_section_ptr;
9552 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
9555 /* Don't attempt to output symbols with st_shnx in the
9556 reserved range other than SHN_ABS and SHN_COMMON. */
9560 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
9561 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
9563 _bfd_merged_section_offset (output_bfd, &isec,
9564 elf_section_data (isec)->sec_info,
9570 /* Don't output the first, undefined, symbol. In fact, don't
9571 output any undefined local symbol. */
9572 if (isec == bfd_und_section_ptr)
9575 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
9577 /* We never output section symbols. Instead, we use the
9578 section symbol of the corresponding section in the output
9583 /* If we are stripping all symbols, we don't want to output this
9585 if (flinfo->info->strip == strip_all)
9588 /* If we are discarding all local symbols, we don't want to
9589 output this one. If we are generating a relocatable output
9590 file, then some of the local symbols may be required by
9591 relocs; we output them below as we discover that they are
9593 if (flinfo->info->discard == discard_all)
9596 /* If this symbol is defined in a section which we are
9597 discarding, we don't need to keep it. */
9598 if (isym->st_shndx != SHN_UNDEF
9599 && isym->st_shndx < SHN_LORESERVE
9600 && bfd_section_removed_from_list (output_bfd,
9601 isec->output_section))
9604 /* Get the name of the symbol. */
9605 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
9610 /* See if we are discarding symbols with this name. */
9611 if ((flinfo->info->strip == strip_some
9612 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE)
9614 || (((flinfo->info->discard == discard_sec_merge
9615 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable)
9616 || flinfo->info->discard == discard_l)
9617 && bfd_is_local_label_name (input_bfd, name)))
9620 if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
9622 if (input_bfd->lto_output)
9623 /* -flto puts a temp file name here. This means builds
9624 are not reproducible. Discard the symbol. */
9626 have_file_sym = TRUE;
9627 flinfo->filesym_count += 1;
9631 /* In the absence of debug info, bfd_find_nearest_line uses
9632 FILE symbols to determine the source file for local
9633 function symbols. Provide a FILE symbol here if input
9634 files lack such, so that their symbols won't be
9635 associated with a previous input file. It's not the
9636 source file, but the best we can do. */
9637 have_file_sym = TRUE;
9638 flinfo->filesym_count += 1;
9639 memset (&osym, 0, sizeof (osym));
9640 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9641 osym.st_shndx = SHN_ABS;
9642 if (!elf_link_output_symstrtab (flinfo,
9643 (input_bfd->lto_output ? NULL
9644 : input_bfd->filename),
9645 &osym, bfd_abs_section_ptr,
9652 /* Adjust the section index for the output file. */
9653 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9654 isec->output_section);
9655 if (osym.st_shndx == SHN_BAD)
9658 /* ELF symbols in relocatable files are section relative, but
9659 in executable files they are virtual addresses. Note that
9660 this code assumes that all ELF sections have an associated
9661 BFD section with a reasonable value for output_offset; below
9662 we assume that they also have a reasonable value for
9663 output_section. Any special sections must be set up to meet
9664 these requirements. */
9665 osym.st_value += isec->output_offset;
9666 if (!flinfo->info->relocatable)
9668 osym.st_value += isec->output_section->vma;
9669 if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
9671 /* STT_TLS symbols are relative to PT_TLS segment base. */
9672 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL);
9673 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
9677 indx = bfd_get_symcount (output_bfd);
9678 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
9685 if (bed->s->arch_size == 32)
9693 r_type_mask = 0xffffffff;
9698 /* Relocate the contents of each section. */
9699 sym_hashes = elf_sym_hashes (input_bfd);
9700 for (o = input_bfd->sections; o != NULL; o = o->next)
9704 if (! o->linker_mark)
9706 /* This section was omitted from the link. */
9710 if (flinfo->info->relocatable
9711 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
9713 /* Deal with the group signature symbol. */
9714 struct bfd_elf_section_data *sec_data = elf_section_data (o);
9715 unsigned long symndx = sec_data->this_hdr.sh_info;
9716 asection *osec = o->output_section;
9718 if (symndx >= locsymcount
9719 || (elf_bad_symtab (input_bfd)
9720 && flinfo->sections[symndx] == NULL))
9722 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
9723 while (h->root.type == bfd_link_hash_indirect
9724 || h->root.type == bfd_link_hash_warning)
9725 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9726 /* Arrange for symbol to be output. */
9728 elf_section_data (osec)->this_hdr.sh_info = -2;
9730 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
9732 /* We'll use the output section target_index. */
9733 asection *sec = flinfo->sections[symndx]->output_section;
9734 elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
9738 if (flinfo->indices[symndx] == -1)
9740 /* Otherwise output the local symbol now. */
9741 Elf_Internal_Sym sym = isymbuf[symndx];
9742 asection *sec = flinfo->sections[symndx]->output_section;
9747 name = bfd_elf_string_from_elf_section (input_bfd,
9748 symtab_hdr->sh_link,
9753 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
9755 if (sym.st_shndx == SHN_BAD)
9758 sym.st_value += o->output_offset;
9760 indx = bfd_get_symcount (output_bfd);
9761 ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
9766 flinfo->indices[symndx] = indx;
9770 elf_section_data (osec)->this_hdr.sh_info
9771 = flinfo->indices[symndx];
9775 if ((o->flags & SEC_HAS_CONTENTS) == 0
9776 || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
9779 if ((o->flags & SEC_LINKER_CREATED) != 0)
9781 /* Section was created by _bfd_elf_link_create_dynamic_sections
9786 /* Get the contents of the section. They have been cached by a
9787 relaxation routine. Note that o is a section in an input
9788 file, so the contents field will not have been set by any of
9789 the routines which work on output files. */
9790 if (elf_section_data (o)->this_hdr.contents != NULL)
9792 contents = elf_section_data (o)->this_hdr.contents;
9793 if (bed->caches_rawsize
9795 && o->rawsize < o->size)
9797 memcpy (flinfo->contents, contents, o->rawsize);
9798 contents = flinfo->contents;
9803 contents = flinfo->contents;
9804 if (! bfd_get_full_section_contents (input_bfd, o, &contents))
9808 if ((o->flags & SEC_RELOC) != 0)
9810 Elf_Internal_Rela *internal_relocs;
9811 Elf_Internal_Rela *rel, *relend;
9812 int action_discarded;
9815 /* Get the swapped relocs. */
9817 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
9818 flinfo->internal_relocs, FALSE);
9819 if (internal_relocs == NULL
9820 && o->reloc_count > 0)
9823 /* We need to reverse-copy input .ctors/.dtors sections if
9824 they are placed in .init_array/.finit_array for output. */
9825 if (o->size > address_size
9826 && ((strncmp (o->name, ".ctors", 6) == 0
9827 && strcmp (o->output_section->name,
9828 ".init_array") == 0)
9829 || (strncmp (o->name, ".dtors", 6) == 0
9830 && strcmp (o->output_section->name,
9831 ".fini_array") == 0))
9832 && (o->name[6] == 0 || o->name[6] == '.'))
9834 if (o->size != o->reloc_count * address_size)
9836 (*_bfd_error_handler)
9837 (_("error: %B: size of section %A is not "
9838 "multiple of address size"),
9840 bfd_set_error (bfd_error_on_input);
9843 o->flags |= SEC_ELF_REVERSE_COPY;
9846 action_discarded = -1;
9847 if (!elf_section_ignore_discarded_relocs (o))
9848 action_discarded = (*bed->action_discarded) (o);
9850 /* Run through the relocs evaluating complex reloc symbols and
9851 looking for relocs against symbols from discarded sections
9852 or section symbols from removed link-once sections.
9853 Complain about relocs against discarded sections. Zero
9854 relocs against removed link-once sections. */
9856 rel = internal_relocs;
9857 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel;
9858 for ( ; rel < relend; rel++)
9860 unsigned long r_symndx = rel->r_info >> r_sym_shift;
9861 unsigned int s_type;
9862 asection **ps, *sec;
9863 struct elf_link_hash_entry *h = NULL;
9864 const char *sym_name;
9866 if (r_symndx == STN_UNDEF)
9869 if (r_symndx >= locsymcount
9870 || (elf_bad_symtab (input_bfd)
9871 && flinfo->sections[r_symndx] == NULL))
9873 h = sym_hashes[r_symndx - extsymoff];
9875 /* Badly formatted input files can contain relocs that
9876 reference non-existant symbols. Check here so that
9877 we do not seg fault. */
9882 sprintf_vma (buffer, rel->r_info);
9883 (*_bfd_error_handler)
9884 (_("error: %B contains a reloc (0x%s) for section %A "
9885 "that references a non-existent global symbol"),
9886 input_bfd, o, buffer);
9887 bfd_set_error (bfd_error_bad_value);
9891 while (h->root.type == bfd_link_hash_indirect
9892 || h->root.type == bfd_link_hash_warning)
9893 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9897 /* If a plugin symbol is referenced from a non-IR file,
9898 mark the symbol as undefined. Note that the
9899 linker may attach linker created dynamic sections
9900 to the plugin bfd. Symbols defined in linker
9901 created sections are not plugin symbols. */
9902 if (h->root.non_ir_ref
9903 && (h->root.type == bfd_link_hash_defined
9904 || h->root.type == bfd_link_hash_defweak)
9905 && (h->root.u.def.section->flags
9906 & SEC_LINKER_CREATED) == 0
9907 && h->root.u.def.section->owner != NULL
9908 && (h->root.u.def.section->owner->flags
9911 h->root.type = bfd_link_hash_undefined;
9912 h->root.u.undef.abfd = h->root.u.def.section->owner;
9916 if (h->root.type == bfd_link_hash_defined
9917 || h->root.type == bfd_link_hash_defweak)
9918 ps = &h->root.u.def.section;
9920 sym_name = h->root.root.string;
9924 Elf_Internal_Sym *sym = isymbuf + r_symndx;
9926 s_type = ELF_ST_TYPE (sym->st_info);
9927 ps = &flinfo->sections[r_symndx];
9928 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
9932 if ((s_type == STT_RELC || s_type == STT_SRELC)
9933 && !flinfo->info->relocatable)
9936 bfd_vma dot = (rel->r_offset
9937 + o->output_offset + o->output_section->vma);
9939 printf ("Encountered a complex symbol!");
9940 printf (" (input_bfd %s, section %s, reloc %ld\n",
9941 input_bfd->filename, o->name,
9942 (long) (rel - internal_relocs));
9943 printf (" symbol: idx %8.8lx, name %s\n",
9944 r_symndx, sym_name);
9945 printf (" reloc : info %8.8lx, addr %8.8lx\n",
9946 (unsigned long) rel->r_info,
9947 (unsigned long) rel->r_offset);
9949 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
9950 isymbuf, locsymcount, s_type == STT_SRELC))
9953 /* Symbol evaluated OK. Update to absolute value. */
9954 set_symbol_value (input_bfd, isymbuf, locsymcount,
9959 if (action_discarded != -1 && ps != NULL)
9961 /* Complain if the definition comes from a
9962 discarded section. */
9963 if ((sec = *ps) != NULL && discarded_section (sec))
9965 BFD_ASSERT (r_symndx != STN_UNDEF);
9966 if (action_discarded & COMPLAIN)
9967 (*flinfo->info->callbacks->einfo)
9968 (_("%X`%s' referenced in section `%A' of %B: "
9969 "defined in discarded section `%A' of %B\n"),
9970 sym_name, o, input_bfd, sec, sec->owner);
9972 /* Try to do the best we can to support buggy old
9973 versions of gcc. Pretend that the symbol is
9974 really defined in the kept linkonce section.
9975 FIXME: This is quite broken. Modifying the
9976 symbol here means we will be changing all later
9977 uses of the symbol, not just in this section. */
9978 if (action_discarded & PRETEND)
9982 kept = _bfd_elf_check_kept_section (sec,
9994 /* Relocate the section by invoking a back end routine.
9996 The back end routine is responsible for adjusting the
9997 section contents as necessary, and (if using Rela relocs
9998 and generating a relocatable output file) adjusting the
9999 reloc addend as necessary.
10001 The back end routine does not have to worry about setting
10002 the reloc address or the reloc symbol index.
10004 The back end routine is given a pointer to the swapped in
10005 internal symbols, and can access the hash table entries
10006 for the external symbols via elf_sym_hashes (input_bfd).
10008 When generating relocatable output, the back end routine
10009 must handle STB_LOCAL/STT_SECTION symbols specially. The
10010 output symbol is going to be a section symbol
10011 corresponding to the output section, which will require
10012 the addend to be adjusted. */
10014 ret = (*relocate_section) (output_bfd, flinfo->info,
10015 input_bfd, o, contents,
10023 || flinfo->info->relocatable
10024 || flinfo->info->emitrelocations)
10026 Elf_Internal_Rela *irela;
10027 Elf_Internal_Rela *irelaend, *irelamid;
10028 bfd_vma last_offset;
10029 struct elf_link_hash_entry **rel_hash;
10030 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
10031 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
10032 unsigned int next_erel;
10033 bfd_boolean rela_normal;
10034 struct bfd_elf_section_data *esdi, *esdo;
10036 esdi = elf_section_data (o);
10037 esdo = elf_section_data (o->output_section);
10038 rela_normal = FALSE;
10040 /* Adjust the reloc addresses and symbol indices. */
10042 irela = internal_relocs;
10043 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel;
10044 rel_hash = esdo->rel.hashes + esdo->rel.count;
10045 /* We start processing the REL relocs, if any. When we reach
10046 IRELAMID in the loop, we switch to the RELA relocs. */
10048 if (esdi->rel.hdr != NULL)
10049 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
10050 * bed->s->int_rels_per_ext_rel);
10051 rel_hash_list = rel_hash;
10052 rela_hash_list = NULL;
10053 last_offset = o->output_offset;
10054 if (!flinfo->info->relocatable)
10055 last_offset += o->output_section->vma;
10056 for (next_erel = 0; irela < irelaend; irela++, next_erel++)
10058 unsigned long r_symndx;
10060 Elf_Internal_Sym sym;
10062 if (next_erel == bed->s->int_rels_per_ext_rel)
10068 if (irela == irelamid)
10070 rel_hash = esdo->rela.hashes + esdo->rela.count;
10071 rela_hash_list = rel_hash;
10072 rela_normal = bed->rela_normal;
10075 irela->r_offset = _bfd_elf_section_offset (output_bfd,
10078 if (irela->r_offset >= (bfd_vma) -2)
10080 /* This is a reloc for a deleted entry or somesuch.
10081 Turn it into an R_*_NONE reloc, at the same
10082 offset as the last reloc. elf_eh_frame.c and
10083 bfd_elf_discard_info rely on reloc offsets
10085 irela->r_offset = last_offset;
10087 irela->r_addend = 0;
10091 irela->r_offset += o->output_offset;
10093 /* Relocs in an executable have to be virtual addresses. */
10094 if (!flinfo->info->relocatable)
10095 irela->r_offset += o->output_section->vma;
10097 last_offset = irela->r_offset;
10099 r_symndx = irela->r_info >> r_sym_shift;
10100 if (r_symndx == STN_UNDEF)
10103 if (r_symndx >= locsymcount
10104 || (elf_bad_symtab (input_bfd)
10105 && flinfo->sections[r_symndx] == NULL))
10107 struct elf_link_hash_entry *rh;
10108 unsigned long indx;
10110 /* This is a reloc against a global symbol. We
10111 have not yet output all the local symbols, so
10112 we do not know the symbol index of any global
10113 symbol. We set the rel_hash entry for this
10114 reloc to point to the global hash table entry
10115 for this symbol. The symbol index is then
10116 set at the end of bfd_elf_final_link. */
10117 indx = r_symndx - extsymoff;
10118 rh = elf_sym_hashes (input_bfd)[indx];
10119 while (rh->root.type == bfd_link_hash_indirect
10120 || rh->root.type == bfd_link_hash_warning)
10121 rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
10123 /* Setting the index to -2 tells
10124 elf_link_output_extsym that this symbol is
10125 used by a reloc. */
10126 BFD_ASSERT (rh->indx < 0);
10134 /* This is a reloc against a local symbol. */
10137 sym = isymbuf[r_symndx];
10138 sec = flinfo->sections[r_symndx];
10139 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
10141 /* I suppose the backend ought to fill in the
10142 section of any STT_SECTION symbol against a
10143 processor specific section. */
10144 r_symndx = STN_UNDEF;
10145 if (bfd_is_abs_section (sec))
10147 else if (sec == NULL || sec->owner == NULL)
10149 bfd_set_error (bfd_error_bad_value);
10154 asection *osec = sec->output_section;
10156 /* If we have discarded a section, the output
10157 section will be the absolute section. In
10158 case of discarded SEC_MERGE sections, use
10159 the kept section. relocate_section should
10160 have already handled discarded linkonce
10162 if (bfd_is_abs_section (osec)
10163 && sec->kept_section != NULL
10164 && sec->kept_section->output_section != NULL)
10166 osec = sec->kept_section->output_section;
10167 irela->r_addend -= osec->vma;
10170 if (!bfd_is_abs_section (osec))
10172 r_symndx = osec->target_index;
10173 if (r_symndx == STN_UNDEF)
10175 irela->r_addend += osec->vma;
10176 osec = _bfd_nearby_section (output_bfd, osec,
10178 irela->r_addend -= osec->vma;
10179 r_symndx = osec->target_index;
10184 /* Adjust the addend according to where the
10185 section winds up in the output section. */
10187 irela->r_addend += sec->output_offset;
10191 if (flinfo->indices[r_symndx] == -1)
10193 unsigned long shlink;
10198 if (flinfo->info->strip == strip_all)
10200 /* You can't do ld -r -s. */
10201 bfd_set_error (bfd_error_invalid_operation);
10205 /* This symbol was skipped earlier, but
10206 since it is needed by a reloc, we
10207 must output it now. */
10208 shlink = symtab_hdr->sh_link;
10209 name = (bfd_elf_string_from_elf_section
10210 (input_bfd, shlink, sym.st_name));
10214 osec = sec->output_section;
10216 _bfd_elf_section_from_bfd_section (output_bfd,
10218 if (sym.st_shndx == SHN_BAD)
10221 sym.st_value += sec->output_offset;
10222 if (!flinfo->info->relocatable)
10224 sym.st_value += osec->vma;
10225 if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
10227 /* STT_TLS symbols are relative to PT_TLS
10229 BFD_ASSERT (elf_hash_table (flinfo->info)
10230 ->tls_sec != NULL);
10231 sym.st_value -= (elf_hash_table (flinfo->info)
10236 indx = bfd_get_symcount (output_bfd);
10237 ret = elf_link_output_symstrtab (flinfo, name,
10243 flinfo->indices[r_symndx] = indx;
10248 r_symndx = flinfo->indices[r_symndx];
10251 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
10252 | (irela->r_info & r_type_mask));
10255 /* Swap out the relocs. */
10256 input_rel_hdr = esdi->rel.hdr;
10257 if (input_rel_hdr && input_rel_hdr->sh_size != 0)
10259 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10264 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
10265 * bed->s->int_rels_per_ext_rel);
10266 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
10269 input_rela_hdr = esdi->rela.hdr;
10270 if (input_rela_hdr && input_rela_hdr->sh_size != 0)
10272 if (!bed->elf_backend_emit_relocs (output_bfd, o,
10281 /* Write out the modified section contents. */
10282 if (bed->elf_backend_write_section
10283 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
10286 /* Section written out. */
10288 else switch (o->sec_info_type)
10290 case SEC_INFO_TYPE_STABS:
10291 if (! (_bfd_write_section_stabs
10293 &elf_hash_table (flinfo->info)->stab_info,
10294 o, &elf_section_data (o)->sec_info, contents)))
10297 case SEC_INFO_TYPE_MERGE:
10298 if (! _bfd_write_merged_section (output_bfd, o,
10299 elf_section_data (o)->sec_info))
10302 case SEC_INFO_TYPE_EH_FRAME:
10304 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
10309 case SEC_INFO_TYPE_EH_FRAME_ENTRY:
10311 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
10319 /* FIXME: octets_per_byte. */
10320 if (! (o->flags & SEC_EXCLUDE))
10322 file_ptr offset = (file_ptr) o->output_offset;
10323 bfd_size_type todo = o->size;
10324 if ((o->flags & SEC_ELF_REVERSE_COPY))
10326 /* Reverse-copy input section to output. */
10329 todo -= address_size;
10330 if (! bfd_set_section_contents (output_bfd,
10338 offset += address_size;
10342 else if (! bfd_set_section_contents (output_bfd,
10356 /* Generate a reloc when linking an ELF file. This is a reloc
10357 requested by the linker, and does not come from any input file. This
10358 is used to build constructor and destructor tables when linking
10362 elf_reloc_link_order (bfd *output_bfd,
10363 struct bfd_link_info *info,
10364 asection *output_section,
10365 struct bfd_link_order *link_order)
10367 reloc_howto_type *howto;
10371 struct bfd_elf_section_reloc_data *reldata;
10372 struct elf_link_hash_entry **rel_hash_ptr;
10373 Elf_Internal_Shdr *rel_hdr;
10374 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
10375 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
10378 struct bfd_elf_section_data *esdo = elf_section_data (output_section);
10380 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
10383 bfd_set_error (bfd_error_bad_value);
10387 addend = link_order->u.reloc.p->addend;
10390 reldata = &esdo->rel;
10391 else if (esdo->rela.hdr)
10392 reldata = &esdo->rela;
10399 /* Figure out the symbol index. */
10400 rel_hash_ptr = reldata->hashes + reldata->count;
10401 if (link_order->type == bfd_section_reloc_link_order)
10403 indx = link_order->u.reloc.p->u.section->target_index;
10404 BFD_ASSERT (indx != 0);
10405 *rel_hash_ptr = NULL;
10409 struct elf_link_hash_entry *h;
10411 /* Treat a reloc against a defined symbol as though it were
10412 actually against the section. */
10413 h = ((struct elf_link_hash_entry *)
10414 bfd_wrapped_link_hash_lookup (output_bfd, info,
10415 link_order->u.reloc.p->u.name,
10416 FALSE, FALSE, TRUE));
10418 && (h->root.type == bfd_link_hash_defined
10419 || h->root.type == bfd_link_hash_defweak))
10423 section = h->root.u.def.section;
10424 indx = section->output_section->target_index;
10425 *rel_hash_ptr = NULL;
10426 /* It seems that we ought to add the symbol value to the
10427 addend here, but in practice it has already been added
10428 because it was passed to constructor_callback. */
10429 addend += section->output_section->vma + section->output_offset;
10431 else if (h != NULL)
10433 /* Setting the index to -2 tells elf_link_output_extsym that
10434 this symbol is used by a reloc. */
10441 if (! ((*info->callbacks->unattached_reloc)
10442 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0)))
10448 /* If this is an inplace reloc, we must write the addend into the
10450 if (howto->partial_inplace && addend != 0)
10452 bfd_size_type size;
10453 bfd_reloc_status_type rstat;
10456 const char *sym_name;
10458 size = (bfd_size_type) bfd_get_reloc_size (howto);
10459 buf = (bfd_byte *) bfd_zmalloc (size);
10460 if (buf == NULL && size != 0)
10462 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
10469 case bfd_reloc_outofrange:
10472 case bfd_reloc_overflow:
10473 if (link_order->type == bfd_section_reloc_link_order)
10474 sym_name = bfd_section_name (output_bfd,
10475 link_order->u.reloc.p->u.section);
10477 sym_name = link_order->u.reloc.p->u.name;
10478 if (! ((*info->callbacks->reloc_overflow)
10479 (info, NULL, sym_name, howto->name, addend, NULL,
10480 NULL, (bfd_vma) 0)))
10487 ok = bfd_set_section_contents (output_bfd, output_section, buf,
10488 link_order->offset, size);
10494 /* The address of a reloc is relative to the section in a
10495 relocatable file, and is a virtual address in an executable
10497 offset = link_order->offset;
10498 if (! info->relocatable)
10499 offset += output_section->vma;
10501 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
10503 irel[i].r_offset = offset;
10504 irel[i].r_info = 0;
10505 irel[i].r_addend = 0;
10507 if (bed->s->arch_size == 32)
10508 irel[0].r_info = ELF32_R_INFO (indx, howto->type);
10510 irel[0].r_info = ELF64_R_INFO (indx, howto->type);
10512 rel_hdr = reldata->hdr;
10513 erel = rel_hdr->contents;
10514 if (rel_hdr->sh_type == SHT_REL)
10516 erel += reldata->count * bed->s->sizeof_rel;
10517 (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
10521 irel[0].r_addend = addend;
10522 erel += reldata->count * bed->s->sizeof_rela;
10523 (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
10532 /* Get the output vma of the section pointed to by the sh_link field. */
10535 elf_get_linked_section_vma (struct bfd_link_order *p)
10537 Elf_Internal_Shdr **elf_shdrp;
10541 s = p->u.indirect.section;
10542 elf_shdrp = elf_elfsections (s->owner);
10543 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s);
10544 elfsec = elf_shdrp[elfsec]->sh_link;
10546 The Intel C compiler generates SHT_IA_64_UNWIND with
10547 SHF_LINK_ORDER. But it doesn't set the sh_link or
10548 sh_info fields. Hence we could get the situation
10549 where elfsec is 0. */
10552 const struct elf_backend_data *bed
10553 = get_elf_backend_data (s->owner);
10554 if (bed->link_order_error_handler)
10555 bed->link_order_error_handler
10556 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s);
10561 s = elf_shdrp[elfsec]->bfd_section;
10562 return s->output_section->vma + s->output_offset;
10567 /* Compare two sections based on the locations of the sections they are
10568 linked to. Used by elf_fixup_link_order. */
10571 compare_link_order (const void * a, const void * b)
10576 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a);
10577 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b);
10580 return apos > bpos;
10584 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same
10585 order as their linked sections. Returns false if this could not be done
10586 because an output section includes both ordered and unordered
10587 sections. Ideally we'd do this in the linker proper. */
10590 elf_fixup_link_order (bfd *abfd, asection *o)
10592 int seen_linkorder;
10595 struct bfd_link_order *p;
10597 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10599 struct bfd_link_order **sections;
10600 asection *s, *other_sec, *linkorder_sec;
10604 linkorder_sec = NULL;
10606 seen_linkorder = 0;
10607 for (p = o->map_head.link_order; p != NULL; p = p->next)
10609 if (p->type == bfd_indirect_link_order)
10611 s = p->u.indirect.section;
10613 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
10614 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass
10615 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s))
10616 && elfsec < elf_numsections (sub)
10617 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER
10618 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub))
10632 if (seen_other && seen_linkorder)
10634 if (other_sec && linkorder_sec)
10635 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"),
10637 linkorder_sec->owner, other_sec,
10640 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"),
10642 bfd_set_error (bfd_error_bad_value);
10647 if (!seen_linkorder)
10650 sections = (struct bfd_link_order **)
10651 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *));
10652 if (sections == NULL)
10654 seen_linkorder = 0;
10656 for (p = o->map_head.link_order; p != NULL; p = p->next)
10658 sections[seen_linkorder++] = p;
10660 /* Sort the input sections in the order of their linked section. */
10661 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *),
10662 compare_link_order);
10664 /* Change the offsets of the sections. */
10666 for (n = 0; n < seen_linkorder; n++)
10668 s = sections[n]->u.indirect.section;
10669 offset &= ~(bfd_vma) 0 << s->alignment_power;
10670 s->output_offset = offset;
10671 sections[n]->offset = offset;
10672 /* FIXME: octets_per_byte. */
10673 offset += sections[n]->size;
10681 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
10685 if (flinfo->symstrtab != NULL)
10686 _bfd_elf_strtab_free (flinfo->symstrtab);
10687 if (flinfo->contents != NULL)
10688 free (flinfo->contents);
10689 if (flinfo->external_relocs != NULL)
10690 free (flinfo->external_relocs);
10691 if (flinfo->internal_relocs != NULL)
10692 free (flinfo->internal_relocs);
10693 if (flinfo->external_syms != NULL)
10694 free (flinfo->external_syms);
10695 if (flinfo->locsym_shndx != NULL)
10696 free (flinfo->locsym_shndx);
10697 if (flinfo->internal_syms != NULL)
10698 free (flinfo->internal_syms);
10699 if (flinfo->indices != NULL)
10700 free (flinfo->indices);
10701 if (flinfo->sections != NULL)
10702 free (flinfo->sections);
10703 if (flinfo->symshndxbuf != NULL)
10704 free (flinfo->symshndxbuf);
10705 for (o = obfd->sections; o != NULL; o = o->next)
10707 struct bfd_elf_section_data *esdo = elf_section_data (o);
10708 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL)
10709 free (esdo->rel.hashes);
10710 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL)
10711 free (esdo->rela.hashes);
10715 /* Do the final step of an ELF link. */
10718 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
10720 bfd_boolean dynamic;
10721 bfd_boolean emit_relocs;
10723 struct elf_final_link_info flinfo;
10725 struct bfd_link_order *p;
10727 bfd_size_type max_contents_size;
10728 bfd_size_type max_external_reloc_size;
10729 bfd_size_type max_internal_reloc_count;
10730 bfd_size_type max_sym_count;
10731 bfd_size_type max_sym_shndx_count;
10732 Elf_Internal_Sym elfsym;
10734 Elf_Internal_Shdr *symtab_hdr;
10735 Elf_Internal_Shdr *symtab_shndx_hdr;
10736 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10737 struct elf_outext_info eoinfo;
10738 bfd_boolean merged;
10739 size_t relativecount = 0;
10740 asection *reldyn = 0;
10742 asection *attr_section = NULL;
10743 bfd_vma attr_size = 0;
10744 const char *std_attrs_section;
10746 if (! is_elf_hash_table (info->hash))
10750 abfd->flags |= DYNAMIC;
10752 dynamic = elf_hash_table (info)->dynamic_sections_created;
10753 dynobj = elf_hash_table (info)->dynobj;
10755 emit_relocs = (info->relocatable
10756 || info->emitrelocations);
10758 flinfo.info = info;
10759 flinfo.output_bfd = abfd;
10760 flinfo.symstrtab = _bfd_elf_strtab_init ();
10761 if (flinfo.symstrtab == NULL)
10766 flinfo.dynsym_sec = NULL;
10767 flinfo.hash_sec = NULL;
10768 flinfo.symver_sec = NULL;
10772 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym");
10773 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
10774 /* Note that dynsym_sec can be NULL (on VMS). */
10775 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
10776 /* Note that it is OK if symver_sec is NULL. */
10779 flinfo.contents = NULL;
10780 flinfo.external_relocs = NULL;
10781 flinfo.internal_relocs = NULL;
10782 flinfo.external_syms = NULL;
10783 flinfo.locsym_shndx = NULL;
10784 flinfo.internal_syms = NULL;
10785 flinfo.indices = NULL;
10786 flinfo.sections = NULL;
10787 flinfo.symshndxbuf = NULL;
10788 flinfo.filesym_count = 0;
10790 /* The object attributes have been merged. Remove the input
10791 sections from the link, and set the contents of the output
10793 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
10794 for (o = abfd->sections; o != NULL; o = o->next)
10796 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
10797 || strcmp (o->name, ".gnu.attributes") == 0)
10799 for (p = o->map_head.link_order; p != NULL; p = p->next)
10801 asection *input_section;
10803 if (p->type != bfd_indirect_link_order)
10805 input_section = p->u.indirect.section;
10806 /* Hack: reset the SEC_HAS_CONTENTS flag so that
10807 elf_link_input_bfd ignores this section. */
10808 input_section->flags &= ~SEC_HAS_CONTENTS;
10811 attr_size = bfd_elf_obj_attr_size (abfd);
10814 bfd_set_section_size (abfd, o, attr_size);
10816 /* Skip this section later on. */
10817 o->map_head.link_order = NULL;
10820 o->flags |= SEC_EXCLUDE;
10824 /* Count up the number of relocations we will output for each output
10825 section, so that we know the sizes of the reloc sections. We
10826 also figure out some maximum sizes. */
10827 max_contents_size = 0;
10828 max_external_reloc_size = 0;
10829 max_internal_reloc_count = 0;
10831 max_sym_shndx_count = 0;
10833 for (o = abfd->sections; o != NULL; o = o->next)
10835 struct bfd_elf_section_data *esdo = elf_section_data (o);
10836 o->reloc_count = 0;
10838 for (p = o->map_head.link_order; p != NULL; p = p->next)
10840 unsigned int reloc_count = 0;
10841 struct bfd_elf_section_data *esdi = NULL;
10843 if (p->type == bfd_section_reloc_link_order
10844 || p->type == bfd_symbol_reloc_link_order)
10846 else if (p->type == bfd_indirect_link_order)
10850 sec = p->u.indirect.section;
10851 esdi = elf_section_data (sec);
10853 /* Mark all sections which are to be included in the
10854 link. This will normally be every section. We need
10855 to do this so that we can identify any sections which
10856 the linker has decided to not include. */
10857 sec->linker_mark = TRUE;
10859 if (sec->flags & SEC_MERGE)
10862 if (esdo->this_hdr.sh_type == SHT_REL
10863 || esdo->this_hdr.sh_type == SHT_RELA)
10864 /* Some backends use reloc_count in relocation sections
10865 to count particular types of relocs. Of course,
10866 reloc sections themselves can't have relocations. */
10868 else if (info->relocatable || info->emitrelocations)
10869 reloc_count = sec->reloc_count;
10870 else if (bed->elf_backend_count_relocs)
10871 reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
10873 if (sec->rawsize > max_contents_size)
10874 max_contents_size = sec->rawsize;
10875 if (sec->size > max_contents_size)
10876 max_contents_size = sec->size;
10878 /* We are interested in just local symbols, not all
10880 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
10881 && (sec->owner->flags & DYNAMIC) == 0)
10885 if (elf_bad_symtab (sec->owner))
10886 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
10887 / bed->s->sizeof_sym);
10889 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
10891 if (sym_count > max_sym_count)
10892 max_sym_count = sym_count;
10894 if (sym_count > max_sym_shndx_count
10895 && elf_symtab_shndx (sec->owner) != 0)
10896 max_sym_shndx_count = sym_count;
10898 if ((sec->flags & SEC_RELOC) != 0)
10900 size_t ext_size = 0;
10902 if (esdi->rel.hdr != NULL)
10903 ext_size = esdi->rel.hdr->sh_size;
10904 if (esdi->rela.hdr != NULL)
10905 ext_size += esdi->rela.hdr->sh_size;
10907 if (ext_size > max_external_reloc_size)
10908 max_external_reloc_size = ext_size;
10909 if (sec->reloc_count > max_internal_reloc_count)
10910 max_internal_reloc_count = sec->reloc_count;
10915 if (reloc_count == 0)
10918 o->reloc_count += reloc_count;
10920 if (p->type == bfd_indirect_link_order
10921 && (info->relocatable || info->emitrelocations))
10924 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
10925 if (esdi->rela.hdr)
10926 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
10931 esdo->rela.count += reloc_count;
10933 esdo->rel.count += reloc_count;
10937 if (o->reloc_count > 0)
10938 o->flags |= SEC_RELOC;
10941 /* Explicitly clear the SEC_RELOC flag. The linker tends to
10942 set it (this is probably a bug) and if it is set
10943 assign_section_numbers will create a reloc section. */
10944 o->flags &=~ SEC_RELOC;
10947 /* If the SEC_ALLOC flag is not set, force the section VMA to
10948 zero. This is done in elf_fake_sections as well, but forcing
10949 the VMA to 0 here will ensure that relocs against these
10950 sections are handled correctly. */
10951 if ((o->flags & SEC_ALLOC) == 0
10952 && ! o->user_set_vma)
10956 if (! info->relocatable && merged)
10957 elf_link_hash_traverse (elf_hash_table (info),
10958 _bfd_elf_link_sec_merge_syms, abfd);
10960 /* Figure out the file positions for everything but the symbol table
10961 and the relocs. We set symcount to force assign_section_numbers
10962 to create a symbol table. */
10963 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs;
10964 BFD_ASSERT (! abfd->output_has_begun);
10965 if (! _bfd_elf_compute_section_file_positions (abfd, info))
10968 /* Set sizes, and assign file positions for reloc sections. */
10969 for (o = abfd->sections; o != NULL; o = o->next)
10971 struct bfd_elf_section_data *esdo = elf_section_data (o);
10972 if ((o->flags & SEC_RELOC) != 0)
10975 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
10979 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
10983 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
10984 to count upwards while actually outputting the relocations. */
10985 esdo->rel.count = 0;
10986 esdo->rela.count = 0;
10988 if (esdo->this_hdr.sh_offset == (file_ptr) -1)
10990 /* Cache the section contents so that they can be compressed
10991 later. Use bfd_malloc since it will be freed by
10992 bfd_compress_section_contents. */
10993 unsigned char *contents = esdo->this_hdr.contents;
10994 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
10997 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
10998 if (contents == NULL)
11000 esdo->this_hdr.contents = contents;
11004 /* We have now assigned file positions for all the sections except
11005 .symtab, .strtab, and non-loaded reloc sections. We start the
11006 .symtab section at the current file position, and write directly
11007 to it. We build the .strtab section in memory. */
11008 bfd_get_symcount (abfd) = 0;
11009 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11010 /* sh_name is set in prep_headers. */
11011 symtab_hdr->sh_type = SHT_SYMTAB;
11012 /* sh_flags, sh_addr and sh_size all start off zero. */
11013 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
11014 /* sh_link is set in assign_section_numbers. */
11015 /* sh_info is set below. */
11016 /* sh_offset is set just below. */
11017 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
11019 if (max_sym_count < 20)
11020 max_sym_count = 20;
11021 elf_hash_table (info)->strtabsize = max_sym_count;
11022 amt = max_sym_count * sizeof (struct elf_sym_strtab);
11023 elf_hash_table (info)->strtab
11024 = (struct elf_sym_strtab *) bfd_malloc (amt);
11025 if (elf_hash_table (info)->strtab == NULL)
11027 /* The real buffer will be allocated in elf_link_swap_symbols_out. */
11029 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
11030 ? (Elf_External_Sym_Shndx *) -1 : NULL);
11032 if (info->strip != strip_all || emit_relocs)
11034 file_ptr off = elf_next_file_pos (abfd);
11036 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
11038 /* Note that at this point elf_next_file_pos (abfd) is
11039 incorrect. We do not yet know the size of the .symtab section.
11040 We correct next_file_pos below, after we do know the size. */
11042 /* Start writing out the symbol table. The first symbol is always a
11044 elfsym.st_value = 0;
11045 elfsym.st_size = 0;
11046 elfsym.st_info = 0;
11047 elfsym.st_other = 0;
11048 elfsym.st_shndx = SHN_UNDEF;
11049 elfsym.st_target_internal = 0;
11050 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
11051 bfd_und_section_ptr, NULL) != 1)
11054 /* Output a symbol for each section. We output these even if we are
11055 discarding local symbols, since they are used for relocs. These
11056 symbols have no names. We store the index of each one in the
11057 index field of the section, so that we can find it again when
11058 outputting relocs. */
11060 elfsym.st_size = 0;
11061 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11062 elfsym.st_other = 0;
11063 elfsym.st_value = 0;
11064 elfsym.st_target_internal = 0;
11065 for (i = 1; i < elf_numsections (abfd); i++)
11067 o = bfd_section_from_elf_index (abfd, i);
11070 o->target_index = bfd_get_symcount (abfd);
11071 elfsym.st_shndx = i;
11072 if (!info->relocatable)
11073 elfsym.st_value = o->vma;
11074 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o,
11081 /* Allocate some memory to hold information read in from the input
11083 if (max_contents_size != 0)
11085 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
11086 if (flinfo.contents == NULL)
11090 if (max_external_reloc_size != 0)
11092 flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
11093 if (flinfo.external_relocs == NULL)
11097 if (max_internal_reloc_count != 0)
11099 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel;
11100 amt *= sizeof (Elf_Internal_Rela);
11101 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
11102 if (flinfo.internal_relocs == NULL)
11106 if (max_sym_count != 0)
11108 amt = max_sym_count * bed->s->sizeof_sym;
11109 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
11110 if (flinfo.external_syms == NULL)
11113 amt = max_sym_count * sizeof (Elf_Internal_Sym);
11114 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
11115 if (flinfo.internal_syms == NULL)
11118 amt = max_sym_count * sizeof (long);
11119 flinfo.indices = (long int *) bfd_malloc (amt);
11120 if (flinfo.indices == NULL)
11123 amt = max_sym_count * sizeof (asection *);
11124 flinfo.sections = (asection **) bfd_malloc (amt);
11125 if (flinfo.sections == NULL)
11129 if (max_sym_shndx_count != 0)
11131 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
11132 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
11133 if (flinfo.locsym_shndx == NULL)
11137 if (elf_hash_table (info)->tls_sec)
11139 bfd_vma base, end = 0;
11142 for (sec = elf_hash_table (info)->tls_sec;
11143 sec && (sec->flags & SEC_THREAD_LOCAL);
11146 bfd_size_type size = sec->size;
11149 && (sec->flags & SEC_HAS_CONTENTS) == 0)
11151 struct bfd_link_order *ord = sec->map_tail.link_order;
11154 size = ord->offset + ord->size;
11156 end = sec->vma + size;
11158 base = elf_hash_table (info)->tls_sec->vma;
11159 /* Only align end of TLS section if static TLS doesn't have special
11160 alignment requirements. */
11161 if (bed->static_tls_alignment == 1)
11162 end = align_power (end,
11163 elf_hash_table (info)->tls_sec->alignment_power);
11164 elf_hash_table (info)->tls_size = end - base;
11167 /* Reorder SHF_LINK_ORDER sections. */
11168 for (o = abfd->sections; o != NULL; o = o->next)
11170 if (!elf_fixup_link_order (abfd, o))
11174 if (!_bfd_elf_fixup_eh_frame_hdr (info))
11177 /* Since ELF permits relocations to be against local symbols, we
11178 must have the local symbols available when we do the relocations.
11179 Since we would rather only read the local symbols once, and we
11180 would rather not keep them in memory, we handle all the
11181 relocations for a single input file at the same time.
11183 Unfortunately, there is no way to know the total number of local
11184 symbols until we have seen all of them, and the local symbol
11185 indices precede the global symbol indices. This means that when
11186 we are generating relocatable output, and we see a reloc against
11187 a global symbol, we can not know the symbol index until we have
11188 finished examining all the local symbols to see which ones we are
11189 going to output. To deal with this, we keep the relocations in
11190 memory, and don't output them until the end of the link. This is
11191 an unfortunate waste of memory, but I don't see a good way around
11192 it. Fortunately, it only happens when performing a relocatable
11193 link, which is not the common case. FIXME: If keep_memory is set
11194 we could write the relocs out and then read them again; I don't
11195 know how bad the memory loss will be. */
11197 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11198 sub->output_has_begun = FALSE;
11199 for (o = abfd->sections; o != NULL; o = o->next)
11201 for (p = o->map_head.link_order; p != NULL; p = p->next)
11203 if (p->type == bfd_indirect_link_order
11204 && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
11205 == bfd_target_elf_flavour)
11206 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
11208 if (! sub->output_has_begun)
11210 if (! elf_link_input_bfd (&flinfo, sub))
11212 sub->output_has_begun = TRUE;
11215 else if (p->type == bfd_section_reloc_link_order
11216 || p->type == bfd_symbol_reloc_link_order)
11218 if (! elf_reloc_link_order (abfd, info, o, p))
11223 if (! _bfd_default_link_order (abfd, info, o, p))
11225 if (p->type == bfd_indirect_link_order
11226 && (bfd_get_flavour (sub)
11227 == bfd_target_elf_flavour)
11228 && (elf_elfheader (sub)->e_ident[EI_CLASS]
11229 != bed->s->elfclass))
11231 const char *iclass, *oclass;
11233 if (bed->s->elfclass == ELFCLASS64)
11235 iclass = "ELFCLASS32";
11236 oclass = "ELFCLASS64";
11240 iclass = "ELFCLASS64";
11241 oclass = "ELFCLASS32";
11244 bfd_set_error (bfd_error_wrong_format);
11245 (*_bfd_error_handler)
11246 (_("%B: file class %s incompatible with %s"),
11247 sub, iclass, oclass);
11256 /* Free symbol buffer if needed. */
11257 if (!info->reduce_memory_overheads)
11259 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
11260 if (bfd_get_flavour (sub) == bfd_target_elf_flavour
11261 && elf_tdata (sub)->symbuf)
11263 free (elf_tdata (sub)->symbuf);
11264 elf_tdata (sub)->symbuf = NULL;
11268 /* Output any global symbols that got converted to local in a
11269 version script or due to symbol visibility. We do this in a
11270 separate step since ELF requires all local symbols to appear
11271 prior to any global symbols. FIXME: We should only do this if
11272 some global symbols were, in fact, converted to become local.
11273 FIXME: Will this work correctly with the Irix 5 linker? */
11274 eoinfo.failed = FALSE;
11275 eoinfo.flinfo = &flinfo;
11276 eoinfo.localsyms = TRUE;
11277 eoinfo.file_sym_done = FALSE;
11278 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11282 /* If backend needs to output some local symbols not present in the hash
11283 table, do it now. */
11284 if (bed->elf_backend_output_arch_local_syms
11285 && (info->strip != strip_all || emit_relocs))
11287 typedef int (*out_sym_func)
11288 (void *, const char *, Elf_Internal_Sym *, asection *,
11289 struct elf_link_hash_entry *);
11291 if (! ((*bed->elf_backend_output_arch_local_syms)
11292 (abfd, info, &flinfo,
11293 (out_sym_func) elf_link_output_symstrtab)))
11297 /* That wrote out all the local symbols. Finish up the symbol table
11298 with the global symbols. Even if we want to strip everything we
11299 can, we still need to deal with those global symbols that got
11300 converted to local in a version script. */
11302 /* The sh_info field records the index of the first non local symbol. */
11303 symtab_hdr->sh_info = bfd_get_symcount (abfd);
11306 && flinfo.dynsym_sec != NULL
11307 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr)
11309 Elf_Internal_Sym sym;
11310 bfd_byte *dynsym = flinfo.dynsym_sec->contents;
11311 long last_local = 0;
11313 /* Write out the section symbols for the output sections. */
11314 if (info->shared || elf_hash_table (info)->is_relocatable_executable)
11320 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
11322 sym.st_target_internal = 0;
11324 for (s = abfd->sections; s != NULL; s = s->next)
11330 dynindx = elf_section_data (s)->dynindx;
11333 indx = elf_section_data (s)->this_idx;
11334 BFD_ASSERT (indx > 0);
11335 sym.st_shndx = indx;
11336 if (! check_dynsym (abfd, &sym))
11338 sym.st_value = s->vma;
11339 dest = dynsym + dynindx * bed->s->sizeof_sym;
11340 if (last_local < dynindx)
11341 last_local = dynindx;
11342 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11346 /* Write out the local dynsyms. */
11347 if (elf_hash_table (info)->dynlocal)
11349 struct elf_link_local_dynamic_entry *e;
11350 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
11355 /* Copy the internal symbol and turn off visibility.
11356 Note that we saved a word of storage and overwrote
11357 the original st_name with the dynstr_index. */
11359 sym.st_other &= ~ELF_ST_VISIBILITY (-1);
11361 s = bfd_section_from_elf_index (e->input_bfd,
11366 elf_section_data (s->output_section)->this_idx;
11367 if (! check_dynsym (abfd, &sym))
11369 sym.st_value = (s->output_section->vma
11371 + e->isym.st_value);
11374 if (last_local < e->dynindx)
11375 last_local = e->dynindx;
11377 dest = dynsym + e->dynindx * bed->s->sizeof_sym;
11378 bed->s->swap_symbol_out (abfd, &sym, dest, 0);
11382 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info =
11386 /* We get the global symbols from the hash table. */
11387 eoinfo.failed = FALSE;
11388 eoinfo.localsyms = FALSE;
11389 eoinfo.flinfo = &flinfo;
11390 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
11394 /* If backend needs to output some symbols not present in the hash
11395 table, do it now. */
11396 if (bed->elf_backend_output_arch_syms
11397 && (info->strip != strip_all || emit_relocs))
11399 typedef int (*out_sym_func)
11400 (void *, const char *, Elf_Internal_Sym *, asection *,
11401 struct elf_link_hash_entry *);
11403 if (! ((*bed->elf_backend_output_arch_syms)
11404 (abfd, info, &flinfo,
11405 (out_sym_func) elf_link_output_symstrtab)))
11409 /* Finalize the .strtab section. */
11410 _bfd_elf_strtab_finalize (flinfo.symstrtab);
11412 /* Swap out the .strtab section. */
11413 if (!elf_link_swap_symbols_out (&flinfo))
11416 /* Now we know the size of the symtab section. */
11417 if (bfd_get_symcount (abfd) > 0)
11419 /* Finish up and write out the symbol string table (.strtab)
11421 Elf_Internal_Shdr *symstrtab_hdr;
11422 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
11424 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
11425 if (symtab_shndx_hdr->sh_name != 0)
11427 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
11428 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
11429 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
11430 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
11431 symtab_shndx_hdr->sh_size = amt;
11433 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
11436 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
11437 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
11441 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
11442 /* sh_name was set in prep_headers. */
11443 symstrtab_hdr->sh_type = SHT_STRTAB;
11444 symstrtab_hdr->sh_flags = 0;
11445 symstrtab_hdr->sh_addr = 0;
11446 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
11447 symstrtab_hdr->sh_entsize = 0;
11448 symstrtab_hdr->sh_link = 0;
11449 symstrtab_hdr->sh_info = 0;
11450 /* sh_offset is set just below. */
11451 symstrtab_hdr->sh_addralign = 1;
11453 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
11455 elf_next_file_pos (abfd) = off;
11457 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
11458 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
11462 /* Adjust the relocs to have the correct symbol indices. */
11463 for (o = abfd->sections; o != NULL; o = o->next)
11465 struct bfd_elf_section_data *esdo = elf_section_data (o);
11467 if ((o->flags & SEC_RELOC) == 0)
11470 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
11471 if (esdo->rel.hdr != NULL)
11472 elf_link_adjust_relocs (abfd, &esdo->rel, sort);
11473 if (esdo->rela.hdr != NULL)
11474 elf_link_adjust_relocs (abfd, &esdo->rela, sort);
11476 /* Set the reloc_count field to 0 to prevent write_relocs from
11477 trying to swap the relocs out itself. */
11478 o->reloc_count = 0;
11481 if (dynamic && info->combreloc && dynobj != NULL)
11482 relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
11484 /* If we are linking against a dynamic object, or generating a
11485 shared library, finish up the dynamic linking information. */
11488 bfd_byte *dyncon, *dynconend;
11490 /* Fix up .dynamic entries. */
11491 o = bfd_get_linker_section (dynobj, ".dynamic");
11492 BFD_ASSERT (o != NULL);
11494 dyncon = o->contents;
11495 dynconend = o->contents + o->size;
11496 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11498 Elf_Internal_Dyn dyn;
11502 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11509 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend)
11511 switch (elf_section_data (reldyn)->this_hdr.sh_type)
11513 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
11514 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
11517 dyn.d_un.d_val = relativecount;
11524 name = info->init_function;
11527 name = info->fini_function;
11530 struct elf_link_hash_entry *h;
11532 h = elf_link_hash_lookup (elf_hash_table (info), name,
11533 FALSE, FALSE, TRUE);
11535 && (h->root.type == bfd_link_hash_defined
11536 || h->root.type == bfd_link_hash_defweak))
11538 dyn.d_un.d_ptr = h->root.u.def.value;
11539 o = h->root.u.def.section;
11540 if (o->output_section != NULL)
11541 dyn.d_un.d_ptr += (o->output_section->vma
11542 + o->output_offset);
11545 /* The symbol is imported from another shared
11546 library and does not apply to this one. */
11547 dyn.d_un.d_ptr = 0;
11554 case DT_PREINIT_ARRAYSZ:
11555 name = ".preinit_array";
11557 case DT_INIT_ARRAYSZ:
11558 name = ".init_array";
11560 case DT_FINI_ARRAYSZ:
11561 name = ".fini_array";
11563 o = bfd_get_section_by_name (abfd, name);
11566 (*_bfd_error_handler)
11567 (_("%B: could not find output section %s"), abfd, name);
11571 (*_bfd_error_handler)
11572 (_("warning: %s section has zero size"), name);
11573 dyn.d_un.d_val = o->size;
11576 case DT_PREINIT_ARRAY:
11577 name = ".preinit_array";
11579 case DT_INIT_ARRAY:
11580 name = ".init_array";
11582 case DT_FINI_ARRAY:
11583 name = ".fini_array";
11590 name = ".gnu.hash";
11599 name = ".gnu.version_d";
11602 name = ".gnu.version_r";
11605 name = ".gnu.version";
11607 o = bfd_get_section_by_name (abfd, name);
11610 (*_bfd_error_handler)
11611 (_("%B: could not find output section %s"), abfd, name);
11614 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
11616 (*_bfd_error_handler)
11617 (_("warning: section '%s' is being made into a note"), name);
11618 bfd_set_error (bfd_error_nonrepresentable_section);
11621 dyn.d_un.d_ptr = o->vma;
11628 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
11632 dyn.d_un.d_val = 0;
11633 dyn.d_un.d_ptr = 0;
11634 for (i = 1; i < elf_numsections (abfd); i++)
11636 Elf_Internal_Shdr *hdr;
11638 hdr = elf_elfsections (abfd)[i];
11639 if (hdr->sh_type == type
11640 && (hdr->sh_flags & SHF_ALLOC) != 0)
11642 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
11643 dyn.d_un.d_val += hdr->sh_size;
11646 if (dyn.d_un.d_ptr == 0
11647 || hdr->sh_addr < dyn.d_un.d_ptr)
11648 dyn.d_un.d_ptr = hdr->sh_addr;
11654 bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
11658 /* If we have created any dynamic sections, then output them. */
11659 if (dynobj != NULL)
11661 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
11664 /* Check for DT_TEXTREL (late, in case the backend removes it). */
11665 if (((info->warn_shared_textrel && info->shared)
11666 || info->error_textrel)
11667 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL)
11669 bfd_byte *dyncon, *dynconend;
11671 dyncon = o->contents;
11672 dynconend = o->contents + o->size;
11673 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
11675 Elf_Internal_Dyn dyn;
11677 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
11679 if (dyn.d_tag == DT_TEXTREL)
11681 if (info->error_textrel)
11682 info->callbacks->einfo
11683 (_("%P%X: read-only segment has dynamic relocations.\n"));
11685 info->callbacks->einfo
11686 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n"));
11692 for (o = dynobj->sections; o != NULL; o = o->next)
11694 if ((o->flags & SEC_HAS_CONTENTS) == 0
11696 || o->output_section == bfd_abs_section_ptr)
11698 if ((o->flags & SEC_LINKER_CREATED) == 0)
11700 /* At this point, we are only interested in sections
11701 created by _bfd_elf_link_create_dynamic_sections. */
11704 if (elf_hash_table (info)->stab_info.stabstr == o)
11706 if (elf_hash_table (info)->eh_info.hdr_sec == o)
11708 if (strcmp (o->name, ".dynstr") != 0)
11710 /* FIXME: octets_per_byte. */
11711 if (! bfd_set_section_contents (abfd, o->output_section,
11713 (file_ptr) o->output_offset,
11719 /* The contents of the .dynstr section are actually in a
11723 off = elf_section_data (o->output_section)->this_hdr.sh_offset;
11724 if (bfd_seek (abfd, off, SEEK_SET) != 0
11725 || ! _bfd_elf_strtab_emit (abfd,
11726 elf_hash_table (info)->dynstr))
11732 if (info->relocatable)
11734 bfd_boolean failed = FALSE;
11736 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
11741 /* If we have optimized stabs strings, output them. */
11742 if (elf_hash_table (info)->stab_info.stabstr != NULL)
11744 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info))
11748 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
11751 elf_final_link_free (abfd, &flinfo);
11753 elf_linker (abfd) = TRUE;
11757 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
11758 if (contents == NULL)
11759 return FALSE; /* Bail out and fail. */
11760 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
11761 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
11768 elf_final_link_free (abfd, &flinfo);
11772 /* Initialize COOKIE for input bfd ABFD. */
11775 init_reloc_cookie (struct elf_reloc_cookie *cookie,
11776 struct bfd_link_info *info, bfd *abfd)
11778 Elf_Internal_Shdr *symtab_hdr;
11779 const struct elf_backend_data *bed;
11781 bed = get_elf_backend_data (abfd);
11782 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11784 cookie->abfd = abfd;
11785 cookie->sym_hashes = elf_sym_hashes (abfd);
11786 cookie->bad_symtab = elf_bad_symtab (abfd);
11787 if (cookie->bad_symtab)
11789 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
11790 cookie->extsymoff = 0;
11794 cookie->locsymcount = symtab_hdr->sh_info;
11795 cookie->extsymoff = symtab_hdr->sh_info;
11798 if (bed->s->arch_size == 32)
11799 cookie->r_sym_shift = 8;
11801 cookie->r_sym_shift = 32;
11803 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
11804 if (cookie->locsyms == NULL && cookie->locsymcount != 0)
11806 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
11807 cookie->locsymcount, 0,
11809 if (cookie->locsyms == NULL)
11811 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
11814 if (info->keep_memory)
11815 symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
11820 /* Free the memory allocated by init_reloc_cookie, if appropriate. */
11823 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
11825 Elf_Internal_Shdr *symtab_hdr;
11827 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11828 if (cookie->locsyms != NULL
11829 && symtab_hdr->contents != (unsigned char *) cookie->locsyms)
11830 free (cookie->locsyms);
11833 /* Initialize the relocation information in COOKIE for input section SEC
11834 of input bfd ABFD. */
11837 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11838 struct bfd_link_info *info, bfd *abfd,
11841 const struct elf_backend_data *bed;
11843 if (sec->reloc_count == 0)
11845 cookie->rels = NULL;
11846 cookie->relend = NULL;
11850 bed = get_elf_backend_data (abfd);
11852 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11853 info->keep_memory);
11854 if (cookie->rels == NULL)
11856 cookie->rel = cookie->rels;
11857 cookie->relend = (cookie->rels
11858 + sec->reloc_count * bed->s->int_rels_per_ext_rel);
11860 cookie->rel = cookie->rels;
11864 /* Free the memory allocated by init_reloc_cookie_rels,
11868 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
11871 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels)
11872 free (cookie->rels);
11875 /* Initialize the whole of COOKIE for input section SEC. */
11878 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11879 struct bfd_link_info *info,
11882 if (!init_reloc_cookie (cookie, info, sec->owner))
11884 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
11889 fini_reloc_cookie (cookie, sec->owner);
11894 /* Free the memory allocated by init_reloc_cookie_for_section,
11898 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
11901 fini_reloc_cookie_rels (cookie, sec);
11902 fini_reloc_cookie (cookie, sec->owner);
11905 /* Garbage collect unused sections. */
11907 /* Default gc_mark_hook. */
11910 _bfd_elf_gc_mark_hook (asection *sec,
11911 struct bfd_link_info *info ATTRIBUTE_UNUSED,
11912 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
11913 struct elf_link_hash_entry *h,
11914 Elf_Internal_Sym *sym)
11916 const char *sec_name;
11920 switch (h->root.type)
11922 case bfd_link_hash_defined:
11923 case bfd_link_hash_defweak:
11924 return h->root.u.def.section;
11926 case bfd_link_hash_common:
11927 return h->root.u.c.p->section;
11929 case bfd_link_hash_undefined:
11930 case bfd_link_hash_undefweak:
11931 /* To work around a glibc bug, keep all XXX input sections
11932 when there is an as yet undefined reference to __start_XXX
11933 or __stop_XXX symbols. The linker will later define such
11934 symbols for orphan input sections that have a name
11935 representable as a C identifier. */
11936 if (strncmp (h->root.root.string, "__start_", 8) == 0)
11937 sec_name = h->root.root.string + 8;
11938 else if (strncmp (h->root.root.string, "__stop_", 7) == 0)
11939 sec_name = h->root.root.string + 7;
11943 if (sec_name && *sec_name != '\0')
11947 for (i = info->input_bfds; i; i = i->link.next)
11949 sec = bfd_get_section_by_name (i, sec_name);
11951 sec->flags |= SEC_KEEP;
11961 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
11966 /* COOKIE->rel describes a relocation against section SEC, which is
11967 a section we've decided to keep. Return the section that contains
11968 the relocation symbol, or NULL if no section contains it. */
11971 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
11972 elf_gc_mark_hook_fn gc_mark_hook,
11973 struct elf_reloc_cookie *cookie)
11975 unsigned long r_symndx;
11976 struct elf_link_hash_entry *h;
11978 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
11979 if (r_symndx == STN_UNDEF)
11982 if (r_symndx >= cookie->locsymcount
11983 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
11985 h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
11988 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"),
11992 while (h->root.type == bfd_link_hash_indirect
11993 || h->root.type == bfd_link_hash_warning)
11994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11996 /* If this symbol is weak and there is a non-weak definition, we
11997 keep the non-weak definition because many backends put
11998 dynamic reloc info on the non-weak definition for code
11999 handling copy relocs. */
12000 if (h->u.weakdef != NULL)
12001 h->u.weakdef->mark = 1;
12002 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
12005 return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
12006 &cookie->locsyms[r_symndx]);
12009 /* COOKIE->rel describes a relocation against section SEC, which is
12010 a section we've decided to keep. Mark the section that contains
12011 the relocation symbol. */
12014 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
12016 elf_gc_mark_hook_fn gc_mark_hook,
12017 struct elf_reloc_cookie *cookie)
12021 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
12022 if (rsec && !rsec->gc_mark)
12024 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
12025 || (rsec->owner->flags & DYNAMIC) != 0)
12027 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
12033 /* The mark phase of garbage collection. For a given section, mark
12034 it and any sections in this section's group, and all the sections
12035 which define symbols to which it refers. */
12038 _bfd_elf_gc_mark (struct bfd_link_info *info,
12040 elf_gc_mark_hook_fn gc_mark_hook)
12043 asection *group_sec, *eh_frame;
12047 /* Mark all the sections in the group. */
12048 group_sec = elf_section_data (sec)->next_in_group;
12049 if (group_sec && !group_sec->gc_mark)
12050 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
12053 /* Look through the section relocs. */
12055 eh_frame = elf_eh_frame_section (sec->owner);
12056 if ((sec->flags & SEC_RELOC) != 0
12057 && sec->reloc_count > 0
12058 && sec != eh_frame)
12060 struct elf_reloc_cookie cookie;
12062 if (!init_reloc_cookie_for_section (&cookie, info, sec))
12066 for (; cookie.rel < cookie.relend; cookie.rel++)
12067 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
12072 fini_reloc_cookie_for_section (&cookie, sec);
12076 if (ret && eh_frame && elf_fde_list (sec))
12078 struct elf_reloc_cookie cookie;
12080 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
12084 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
12085 gc_mark_hook, &cookie))
12087 fini_reloc_cookie_for_section (&cookie, eh_frame);
12091 eh_frame = elf_section_eh_frame_entry (sec);
12092 if (ret && eh_frame && !eh_frame->gc_mark)
12093 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
12099 /* Scan and mark sections in a special or debug section group. */
12102 _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
12104 /* Point to first section of section group. */
12106 /* Used to iterate the section group. */
12109 bfd_boolean is_special_grp = TRUE;
12110 bfd_boolean is_debug_grp = TRUE;
12112 /* First scan to see if group contains any section other than debug
12113 and special section. */
12114 ssec = msec = elf_next_in_group (grp);
12117 if ((msec->flags & SEC_DEBUGGING) == 0)
12118 is_debug_grp = FALSE;
12120 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
12121 is_special_grp = FALSE;
12123 msec = elf_next_in_group (msec);
12125 while (msec != ssec);
12127 /* If this is a pure debug section group or pure special section group,
12128 keep all sections in this group. */
12129 if (is_debug_grp || is_special_grp)
12134 msec = elf_next_in_group (msec);
12136 while (msec != ssec);
12140 /* Keep debug and special sections. */
12143 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
12144 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
12148 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12151 bfd_boolean some_kept;
12152 bfd_boolean debug_frag_seen;
12154 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12157 /* Ensure all linker created sections are kept,
12158 see if any other section is already marked,
12159 and note if we have any fragmented debug sections. */
12160 debug_frag_seen = some_kept = FALSE;
12161 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12163 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12165 else if (isec->gc_mark)
12168 if (debug_frag_seen == FALSE
12169 && (isec->flags & SEC_DEBUGGING)
12170 && CONST_STRNEQ (isec->name, ".debug_line."))
12171 debug_frag_seen = TRUE;
12174 /* If no section in this file will be kept, then we can
12175 toss out the debug and special sections. */
12179 /* Keep debug and special sections like .comment when they are
12180 not part of a group. Also keep section groups that contain
12181 just debug sections or special sections. */
12182 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12184 if ((isec->flags & SEC_GROUP) != 0)
12185 _bfd_elf_gc_mark_debug_special_section_group (isec);
12186 else if (((isec->flags & SEC_DEBUGGING) != 0
12187 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
12188 && elf_next_in_group (isec) == NULL)
12192 if (! debug_frag_seen)
12195 /* Look for CODE sections which are going to be discarded,
12196 and find and discard any fragmented debug sections which
12197 are associated with that code section. */
12198 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
12199 if ((isec->flags & SEC_CODE) != 0
12200 && isec->gc_mark == 0)
12205 ilen = strlen (isec->name);
12207 /* Association is determined by the name of the debug section
12208 containing the name of the code section as a suffix. For
12209 example .debug_line.text.foo is a debug section associated
12211 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
12215 if (dsec->gc_mark == 0
12216 || (dsec->flags & SEC_DEBUGGING) == 0)
12219 dlen = strlen (dsec->name);
12222 && strncmp (dsec->name + (dlen - ilen),
12223 isec->name, ilen) == 0)
12233 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
12235 struct elf_gc_sweep_symbol_info
12237 struct bfd_link_info *info;
12238 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
12243 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
12246 && (((h->root.type == bfd_link_hash_defined
12247 || h->root.type == bfd_link_hash_defweak)
12248 && !((h->def_regular || ELF_COMMON_DEF_P (h))
12249 && h->root.u.def.section->gc_mark))
12250 || h->root.type == bfd_link_hash_undefined
12251 || h->root.type == bfd_link_hash_undefweak))
12253 struct elf_gc_sweep_symbol_info *inf;
12255 inf = (struct elf_gc_sweep_symbol_info *) data;
12256 (*inf->hide_symbol) (inf->info, h, TRUE);
12257 h->def_regular = 0;
12258 h->ref_regular = 0;
12259 h->ref_regular_nonweak = 0;
12265 /* The sweep phase of garbage collection. Remove all garbage sections. */
12267 typedef bfd_boolean (*gc_sweep_hook_fn)
12268 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
12271 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
12274 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12275 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook;
12276 unsigned long section_sym_count;
12277 struct elf_gc_sweep_symbol_info sweep_info;
12279 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12283 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12284 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12287 for (o = sub->sections; o != NULL; o = o->next)
12289 /* When any section in a section group is kept, we keep all
12290 sections in the section group. If the first member of
12291 the section group is excluded, we will also exclude the
12293 if (o->flags & SEC_GROUP)
12295 asection *first = elf_next_in_group (o);
12296 o->gc_mark = first->gc_mark;
12302 /* Skip sweeping sections already excluded. */
12303 if (o->flags & SEC_EXCLUDE)
12306 /* Since this is early in the link process, it is simple
12307 to remove a section from the output. */
12308 o->flags |= SEC_EXCLUDE;
12310 if (info->print_gc_sections && o->size != 0)
12311 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
12313 /* But we also have to update some of the relocation
12314 info we collected before. */
12316 && (o->flags & SEC_RELOC) != 0
12317 && o->reloc_count != 0
12318 && !((info->strip == strip_all || info->strip == strip_debugger)
12319 && (o->flags & SEC_DEBUGGING) != 0)
12320 && !bfd_is_abs_section (o->output_section))
12322 Elf_Internal_Rela *internal_relocs;
12326 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL,
12327 info->keep_memory);
12328 if (internal_relocs == NULL)
12331 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
12333 if (elf_section_data (o)->relocs != internal_relocs)
12334 free (internal_relocs);
12342 /* Remove the symbols that were in the swept sections from the dynamic
12343 symbol table. GCFIXME: Anyone know how to get them out of the
12344 static symbol table as well? */
12345 sweep_info.info = info;
12346 sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
12347 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
12350 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count);
12354 /* Propagate collected vtable information. This is called through
12355 elf_link_hash_traverse. */
12358 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
12360 /* Those that are not vtables. */
12361 if (h->vtable == NULL || h->vtable->parent == NULL)
12364 /* Those vtables that do not have parents, we cannot merge. */
12365 if (h->vtable->parent == (struct elf_link_hash_entry *) -1)
12368 /* If we've already been done, exit. */
12369 if (h->vtable->used && h->vtable->used[-1])
12372 /* Make sure the parent's table is up to date. */
12373 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp);
12375 if (h->vtable->used == NULL)
12377 /* None of this table's entries were referenced. Re-use the
12379 h->vtable->used = h->vtable->parent->vtable->used;
12380 h->vtable->size = h->vtable->parent->vtable->size;
12385 bfd_boolean *cu, *pu;
12387 /* Or the parent's entries into ours. */
12388 cu = h->vtable->used;
12390 pu = h->vtable->parent->vtable->used;
12393 const struct elf_backend_data *bed;
12394 unsigned int log_file_align;
12396 bed = get_elf_backend_data (h->root.u.def.section->owner);
12397 log_file_align = bed->s->log_file_align;
12398 n = h->vtable->parent->vtable->size >> log_file_align;
12413 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
12416 bfd_vma hstart, hend;
12417 Elf_Internal_Rela *relstart, *relend, *rel;
12418 const struct elf_backend_data *bed;
12419 unsigned int log_file_align;
12421 /* Take care of both those symbols that do not describe vtables as
12422 well as those that are not loaded. */
12423 if (h->vtable == NULL || h->vtable->parent == NULL)
12426 BFD_ASSERT (h->root.type == bfd_link_hash_defined
12427 || h->root.type == bfd_link_hash_defweak);
12429 sec = h->root.u.def.section;
12430 hstart = h->root.u.def.value;
12431 hend = hstart + h->size;
12433 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
12435 return *(bfd_boolean *) okp = FALSE;
12436 bed = get_elf_backend_data (sec->owner);
12437 log_file_align = bed->s->log_file_align;
12439 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel;
12441 for (rel = relstart; rel < relend; ++rel)
12442 if (rel->r_offset >= hstart && rel->r_offset < hend)
12444 /* If the entry is in use, do nothing. */
12445 if (h->vtable->used
12446 && (rel->r_offset - hstart) < h->vtable->size)
12448 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
12449 if (h->vtable->used[entry])
12452 /* Otherwise, kill it. */
12453 rel->r_offset = rel->r_info = rel->r_addend = 0;
12459 /* Mark sections containing dynamically referenced symbols. When
12460 building shared libraries, we must assume that any visible symbol is
12464 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
12466 struct bfd_link_info *info = (struct bfd_link_info *) inf;
12467 struct bfd_elf_dynamic_list *d = info->dynamic_list;
12469 if ((h->root.type == bfd_link_hash_defined
12470 || h->root.type == bfd_link_hash_defweak)
12472 || ((h->def_regular || ELF_COMMON_DEF_P (h))
12473 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
12474 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
12475 && (!info->executable
12476 || info->export_dynamic
12479 && (*d->match) (&d->head, NULL, h->root.root.string)))
12480 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL
12481 || !bfd_hide_sym_by_version (info->version_info,
12482 h->root.root.string)))))
12483 h->root.u.def.section->flags |= SEC_KEEP;
12488 /* Keep all sections containing symbols undefined on the command-line,
12489 and the section containing the entry symbol. */
12492 _bfd_elf_gc_keep (struct bfd_link_info *info)
12494 struct bfd_sym_chain *sym;
12496 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
12498 struct elf_link_hash_entry *h;
12500 h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
12501 FALSE, FALSE, FALSE);
12504 && (h->root.type == bfd_link_hash_defined
12505 || h->root.type == bfd_link_hash_defweak)
12506 && !bfd_is_abs_section (h->root.u.def.section))
12507 h->root.u.def.section->flags |= SEC_KEEP;
12512 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
12513 struct bfd_link_info *info)
12515 bfd *ibfd = info->input_bfds;
12517 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12520 struct elf_reloc_cookie cookie;
12522 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
12525 if (!init_reloc_cookie (&cookie, info, ibfd))
12528 for (sec = ibfd->sections; sec; sec = sec->next)
12530 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
12531 && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
12533 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
12534 fini_reloc_cookie_rels (&cookie, sec);
12541 /* Do mark and sweep of unused sections. */
12544 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
12546 bfd_boolean ok = TRUE;
12548 elf_gc_mark_hook_fn gc_mark_hook;
12549 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12550 struct elf_link_hash_table *htab;
12552 if (!bed->can_gc_sections
12553 || !is_elf_hash_table (info->hash))
12555 (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
12559 bed->gc_keep (info);
12560 htab = elf_hash_table (info);
12562 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
12563 at the .eh_frame section if we can mark the FDEs individually. */
12564 for (sub = info->input_bfds;
12565 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
12566 sub = sub->link.next)
12569 struct elf_reloc_cookie cookie;
12571 sec = bfd_get_section_by_name (sub, ".eh_frame");
12572 while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
12574 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
12575 if (elf_section_data (sec)->sec_info
12576 && (sec->flags & SEC_LINKER_CREATED) == 0)
12577 elf_eh_frame_section (sub) = sec;
12578 fini_reloc_cookie_for_section (&cookie, sec);
12579 sec = bfd_get_next_section_by_name (sec);
12583 /* Apply transitive closure to the vtable entry usage info. */
12584 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
12588 /* Kill the vtable relocations that were not used. */
12589 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
12593 /* Mark dynamically referenced symbols. */
12594 if (htab->dynamic_sections_created)
12595 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
12597 /* Grovel through relocs to find out who stays ... */
12598 gc_mark_hook = bed->gc_mark_hook;
12599 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
12603 if (bfd_get_flavour (sub) != bfd_target_elf_flavour
12604 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
12607 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
12608 Also treat note sections as a root, if the section is not part
12610 for (o = sub->sections; o != NULL; o = o->next)
12612 && (o->flags & SEC_EXCLUDE) == 0
12613 && ((o->flags & SEC_KEEP) != 0
12614 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
12615 && elf_next_in_group (o) == NULL )))
12617 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
12622 /* Allow the backend to mark additional target specific sections. */
12623 bed->gc_mark_extra_sections (info, gc_mark_hook);
12625 /* ... and mark SEC_EXCLUDE for those that go. */
12626 return elf_gc_sweep (abfd, info);
12629 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
12632 bfd_elf_gc_record_vtinherit (bfd *abfd,
12634 struct elf_link_hash_entry *h,
12637 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
12638 struct elf_link_hash_entry **search, *child;
12639 bfd_size_type extsymcount;
12640 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12642 /* The sh_info field of the symtab header tells us where the
12643 external symbols start. We don't care about the local symbols at
12645 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
12646 if (!elf_bad_symtab (abfd))
12647 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
12649 sym_hashes = elf_sym_hashes (abfd);
12650 sym_hashes_end = sym_hashes + extsymcount;
12652 /* Hunt down the child symbol, which is in this section at the same
12653 offset as the relocation. */
12654 for (search = sym_hashes; search != sym_hashes_end; ++search)
12656 if ((child = *search) != NULL
12657 && (child->root.type == bfd_link_hash_defined
12658 || child->root.type == bfd_link_hash_defweak)
12659 && child->root.u.def.section == sec
12660 && child->root.u.def.value == offset)
12664 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT",
12665 abfd, sec, (unsigned long) offset);
12666 bfd_set_error (bfd_error_invalid_operation);
12670 if (!child->vtable)
12672 child->vtable = ((struct elf_link_virtual_table_entry *)
12673 bfd_zalloc (abfd, sizeof (*child->vtable)));
12674 if (!child->vtable)
12679 /* This *should* only be the absolute section. It could potentially
12680 be that someone has defined a non-global vtable though, which
12681 would be bad. It isn't worth paging in the local symbols to be
12682 sure though; that case should simply be handled by the assembler. */
12684 child->vtable->parent = (struct elf_link_hash_entry *) -1;
12687 child->vtable->parent = h;
12692 /* Called from check_relocs to record the existence of a VTENTRY reloc. */
12695 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED,
12696 asection *sec ATTRIBUTE_UNUSED,
12697 struct elf_link_hash_entry *h,
12700 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12701 unsigned int log_file_align = bed->s->log_file_align;
12705 h->vtable = ((struct elf_link_virtual_table_entry *)
12706 bfd_zalloc (abfd, sizeof (*h->vtable)));
12711 if (addend >= h->vtable->size)
12713 size_t size, bytes, file_align;
12714 bfd_boolean *ptr = h->vtable->used;
12716 /* While the symbol is undefined, we have to be prepared to handle
12718 file_align = 1 << log_file_align;
12719 if (h->root.type == bfd_link_hash_undefined)
12720 size = addend + file_align;
12724 if (addend >= size)
12726 /* Oops! We've got a reference past the defined end of
12727 the table. This is probably a bug -- shall we warn? */
12728 size = addend + file_align;
12731 size = (size + file_align - 1) & -file_align;
12733 /* Allocate one extra entry for use as a "done" flag for the
12734 consolidation pass. */
12735 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean);
12739 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes);
12745 oldbytes = (((h->vtable->size >> log_file_align) + 1)
12746 * sizeof (bfd_boolean));
12747 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
12751 ptr = (bfd_boolean *) bfd_zmalloc (bytes);
12756 /* And arrange for that done flag to be at index -1. */
12757 h->vtable->used = ptr + 1;
12758 h->vtable->size = size;
12761 h->vtable->used[addend >> log_file_align] = TRUE;
12766 /* Map an ELF section header flag to its corresponding string. */
12770 flagword flag_value;
12771 } elf_flags_to_name_table;
12773 static elf_flags_to_name_table elf_flags_to_names [] =
12775 { "SHF_WRITE", SHF_WRITE },
12776 { "SHF_ALLOC", SHF_ALLOC },
12777 { "SHF_EXECINSTR", SHF_EXECINSTR },
12778 { "SHF_MERGE", SHF_MERGE },
12779 { "SHF_STRINGS", SHF_STRINGS },
12780 { "SHF_INFO_LINK", SHF_INFO_LINK},
12781 { "SHF_LINK_ORDER", SHF_LINK_ORDER},
12782 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
12783 { "SHF_GROUP", SHF_GROUP },
12784 { "SHF_TLS", SHF_TLS },
12785 { "SHF_MASKOS", SHF_MASKOS },
12786 { "SHF_EXCLUDE", SHF_EXCLUDE },
12789 /* Returns TRUE if the section is to be included, otherwise FALSE. */
12791 bfd_elf_lookup_section_flags (struct bfd_link_info *info,
12792 struct flag_info *flaginfo,
12795 const bfd_vma sh_flags = elf_section_flags (section);
12797 if (!flaginfo->flags_initialized)
12799 bfd *obfd = info->output_bfd;
12800 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12801 struct flag_info_list *tf = flaginfo->flag_list;
12803 int without_hex = 0;
12805 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
12808 flagword (*lookup) (char *);
12810 lookup = bed->elf_backend_lookup_section_flags_hook;
12811 if (lookup != NULL)
12813 flagword hexval = (*lookup) ((char *) tf->name);
12817 if (tf->with == with_flags)
12818 with_hex |= hexval;
12819 else if (tf->with == without_flags)
12820 without_hex |= hexval;
12825 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
12827 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
12829 if (tf->with == with_flags)
12830 with_hex |= elf_flags_to_names[i].flag_value;
12831 else if (tf->with == without_flags)
12832 without_hex |= elf_flags_to_names[i].flag_value;
12839 info->callbacks->einfo
12840 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
12844 flaginfo->flags_initialized = TRUE;
12845 flaginfo->only_with_flags |= with_hex;
12846 flaginfo->not_with_flags |= without_hex;
12849 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
12852 if ((flaginfo->not_with_flags & sh_flags) != 0)
12858 struct alloc_got_off_arg {
12860 struct bfd_link_info *info;
12863 /* We need a special top-level link routine to convert got reference counts
12864 to real got offsets. */
12867 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
12869 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
12870 bfd *obfd = gofarg->info->output_bfd;
12871 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
12873 if (h->got.refcount > 0)
12875 h->got.offset = gofarg->gotoff;
12876 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
12879 h->got.offset = (bfd_vma) -1;
12884 /* And an accompanying bit to work out final got entry offsets once
12885 we're done. Should be called from final_link. */
12888 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
12889 struct bfd_link_info *info)
12892 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
12894 struct alloc_got_off_arg gofarg;
12896 BFD_ASSERT (abfd == info->output_bfd);
12898 if (! is_elf_hash_table (info->hash))
12901 /* The GOT offset is relative to the .got section, but the GOT header is
12902 put into the .got.plt section, if the backend uses it. */
12903 if (bed->want_got_plt)
12906 gotoff = bed->got_header_size;
12908 /* Do the local .got entries first. */
12909 for (i = info->input_bfds; i; i = i->link.next)
12911 bfd_signed_vma *local_got;
12912 bfd_size_type j, locsymcount;
12913 Elf_Internal_Shdr *symtab_hdr;
12915 if (bfd_get_flavour (i) != bfd_target_elf_flavour)
12918 local_got = elf_local_got_refcounts (i);
12922 symtab_hdr = &elf_tdata (i)->symtab_hdr;
12923 if (elf_bad_symtab (i))
12924 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
12926 locsymcount = symtab_hdr->sh_info;
12928 for (j = 0; j < locsymcount; ++j)
12930 if (local_got[j] > 0)
12932 local_got[j] = gotoff;
12933 gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
12936 local_got[j] = (bfd_vma) -1;
12940 /* Then the global .got entries. .plt refcounts are handled by
12941 adjust_dynamic_symbol */
12942 gofarg.gotoff = gotoff;
12943 gofarg.info = info;
12944 elf_link_hash_traverse (elf_hash_table (info),
12945 elf_gc_allocate_got_offsets,
12950 /* Many folk need no more in the way of final link than this, once
12951 got entry reference counting is enabled. */
12954 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
12956 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
12959 /* Invoke the regular ELF backend linker to do all the work. */
12960 return bfd_elf_final_link (abfd, info);
12964 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
12966 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
12968 if (rcookie->bad_symtab)
12969 rcookie->rel = rcookie->rels;
12971 for (; rcookie->rel < rcookie->relend; rcookie->rel++)
12973 unsigned long r_symndx;
12975 if (! rcookie->bad_symtab)
12976 if (rcookie->rel->r_offset > offset)
12978 if (rcookie->rel->r_offset != offset)
12981 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
12982 if (r_symndx == STN_UNDEF)
12985 if (r_symndx >= rcookie->locsymcount
12986 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
12988 struct elf_link_hash_entry *h;
12990 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
12992 while (h->root.type == bfd_link_hash_indirect
12993 || h->root.type == bfd_link_hash_warning)
12994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
12996 if ((h->root.type == bfd_link_hash_defined
12997 || h->root.type == bfd_link_hash_defweak)
12998 && (h->root.u.def.section->owner != rcookie->abfd
12999 || h->root.u.def.section->kept_section != NULL
13000 || discarded_section (h->root.u.def.section)))
13005 /* It's not a relocation against a global symbol,
13006 but it could be a relocation against a local
13007 symbol for a discarded section. */
13009 Elf_Internal_Sym *isym;
13011 /* Need to: get the symbol; get the section. */
13012 isym = &rcookie->locsyms[r_symndx];
13013 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
13015 && (isec->kept_section != NULL
13016 || discarded_section (isec)))
13024 /* Discard unneeded references to discarded sections.
13025 Returns -1 on error, 1 if any section's size was changed, 0 if
13026 nothing changed. This function assumes that the relocations are in
13027 sorted order, which is true for all known assemblers. */
13030 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
13032 struct elf_reloc_cookie cookie;
13037 if (info->traditional_format
13038 || !is_elf_hash_table (info->hash))
13041 o = bfd_get_section_by_name (output_bfd, ".stab");
13046 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13049 || i->reloc_count == 0
13050 || i->sec_info_type != SEC_INFO_TYPE_STABS)
13054 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13057 if (!init_reloc_cookie_for_section (&cookie, info, i))
13060 if (_bfd_discard_section_stabs (abfd, i,
13061 elf_section_data (i)->sec_info,
13062 bfd_elf_reloc_symbol_deleted_p,
13066 fini_reloc_cookie_for_section (&cookie, i);
13071 if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
13072 o = bfd_get_section_by_name (output_bfd, ".eh_frame");
13077 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13083 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13086 if (!init_reloc_cookie_for_section (&cookie, info, i))
13089 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
13090 if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
13091 bfd_elf_reloc_symbol_deleted_p,
13095 fini_reloc_cookie_for_section (&cookie, i);
13099 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
13101 const struct elf_backend_data *bed;
13103 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
13106 bed = get_elf_backend_data (abfd);
13108 if (bed->elf_backend_discard_info != NULL)
13110 if (!init_reloc_cookie (&cookie, info, abfd))
13113 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
13116 fini_reloc_cookie (&cookie, abfd);
13120 if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
13121 _bfd_elf_end_eh_frame_parsing (info);
13123 if (info->eh_frame_hdr_type
13124 && !info->relocatable
13125 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
13132 _bfd_elf_section_already_linked (bfd *abfd,
13134 struct bfd_link_info *info)
13137 const char *name, *key;
13138 struct bfd_section_already_linked *l;
13139 struct bfd_section_already_linked_hash_entry *already_linked_list;
13141 if (sec->output_section == bfd_abs_section_ptr)
13144 flags = sec->flags;
13146 /* Return if it isn't a linkonce section. A comdat group section
13147 also has SEC_LINK_ONCE set. */
13148 if ((flags & SEC_LINK_ONCE) == 0)
13151 /* Don't put group member sections on our list of already linked
13152 sections. They are handled as a group via their group section. */
13153 if (elf_sec_group (sec) != NULL)
13156 /* For a SHT_GROUP section, use the group signature as the key. */
13158 if ((flags & SEC_GROUP) != 0
13159 && elf_next_in_group (sec) != NULL
13160 && elf_group_name (elf_next_in_group (sec)) != NULL)
13161 key = elf_group_name (elf_next_in_group (sec));
13164 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
13165 if (CONST_STRNEQ (name, ".gnu.linkonce.")
13166 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
13169 /* Must be a user linkonce section that doesn't follow gcc's
13170 naming convention. In this case we won't be matching
13171 single member groups. */
13175 already_linked_list = bfd_section_already_linked_table_lookup (key);
13177 for (l = already_linked_list->entry; l != NULL; l = l->next)
13179 /* We may have 2 different types of sections on the list: group
13180 sections with a signature of <key> (<key> is some string),
13181 and linkonce sections named .gnu.linkonce.<type>.<key>.
13182 Match like sections. LTO plugin sections are an exception.
13183 They are always named .gnu.linkonce.t.<key> and match either
13184 type of section. */
13185 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
13186 && ((flags & SEC_GROUP) != 0
13187 || strcmp (name, l->sec->name) == 0))
13188 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
13190 /* The section has already been linked. See if we should
13191 issue a warning. */
13192 if (!_bfd_handle_already_linked (sec, l, info))
13195 if (flags & SEC_GROUP)
13197 asection *first = elf_next_in_group (sec);
13198 asection *s = first;
13202 s->output_section = bfd_abs_section_ptr;
13203 /* Record which group discards it. */
13204 s->kept_section = l->sec;
13205 s = elf_next_in_group (s);
13206 /* These lists are circular. */
13216 /* A single member comdat group section may be discarded by a
13217 linkonce section and vice versa. */
13218 if ((flags & SEC_GROUP) != 0)
13220 asection *first = elf_next_in_group (sec);
13222 if (first != NULL && elf_next_in_group (first) == first)
13223 /* Check this single member group against linkonce sections. */
13224 for (l = already_linked_list->entry; l != NULL; l = l->next)
13225 if ((l->sec->flags & SEC_GROUP) == 0
13226 && bfd_elf_match_symbols_in_sections (l->sec, first, info))
13228 first->output_section = bfd_abs_section_ptr;
13229 first->kept_section = l->sec;
13230 sec->output_section = bfd_abs_section_ptr;
13235 /* Check this linkonce section against single member groups. */
13236 for (l = already_linked_list->entry; l != NULL; l = l->next)
13237 if (l->sec->flags & SEC_GROUP)
13239 asection *first = elf_next_in_group (l->sec);
13242 && elf_next_in_group (first) == first
13243 && bfd_elf_match_symbols_in_sections (first, sec, info))
13245 sec->output_section = bfd_abs_section_ptr;
13246 sec->kept_section = first;
13251 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
13252 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
13253 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
13254 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
13255 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
13256 but its `.gnu.linkonce.t.F' is discarded means we chose one-only
13257 `.gnu.linkonce.t.F' section from a different bfd not requiring any
13258 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
13259 The reverse order cannot happen as there is never a bfd with only the
13260 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
13261 matter as here were are looking only for cross-bfd sections. */
13263 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r."))
13264 for (l = already_linked_list->entry; l != NULL; l = l->next)
13265 if ((l->sec->flags & SEC_GROUP) == 0
13266 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t."))
13268 if (abfd != l->sec->owner)
13269 sec->output_section = bfd_abs_section_ptr;
13273 /* This is the first section with this name. Record it. */
13274 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
13275 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
13276 return sec->output_section == bfd_abs_section_ptr;
13280 _bfd_elf_common_definition (Elf_Internal_Sym *sym)
13282 return sym->st_shndx == SHN_COMMON;
13286 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
13292 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
13294 return bfd_com_section_ptr;
13298 _bfd_elf_default_got_elt_size (bfd *abfd,
13299 struct bfd_link_info *info ATTRIBUTE_UNUSED,
13300 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
13301 bfd *ibfd ATTRIBUTE_UNUSED,
13302 unsigned long symndx ATTRIBUTE_UNUSED)
13304 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13305 return bed->s->arch_size / 8;
13308 /* Routines to support the creation of dynamic relocs. */
13310 /* Returns the name of the dynamic reloc section associated with SEC. */
13312 static const char *
13313 get_dynamic_reloc_section_name (bfd * abfd,
13315 bfd_boolean is_rela)
13318 const char *old_name = bfd_get_section_name (NULL, sec);
13319 const char *prefix = is_rela ? ".rela" : ".rel";
13321 if (old_name == NULL)
13324 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
13325 sprintf (name, "%s%s", prefix, old_name);
13330 /* Returns the dynamic reloc section associated with SEC.
13331 If necessary compute the name of the dynamic reloc section based
13332 on SEC's name (looked up in ABFD's string table) and the setting
13336 _bfd_elf_get_dynamic_reloc_section (bfd * abfd,
13338 bfd_boolean is_rela)
13340 asection * reloc_sec = elf_section_data (sec)->sreloc;
13342 if (reloc_sec == NULL)
13344 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13348 reloc_sec = bfd_get_linker_section (abfd, name);
13350 if (reloc_sec != NULL)
13351 elf_section_data (sec)->sreloc = reloc_sec;
13358 /* Returns the dynamic reloc section associated with SEC. If the
13359 section does not exist it is created and attached to the DYNOBJ
13360 bfd and stored in the SRELOC field of SEC's elf_section_data
13363 ALIGNMENT is the alignment for the newly created section and
13364 IS_RELA defines whether the name should be .rela.<SEC's name>
13365 or .rel.<SEC's name>. The section name is looked up in the
13366 string table associated with ABFD. */
13369 _bfd_elf_make_dynamic_reloc_section (asection *sec,
13371 unsigned int alignment,
13373 bfd_boolean is_rela)
13375 asection * reloc_sec = elf_section_data (sec)->sreloc;
13377 if (reloc_sec == NULL)
13379 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
13384 reloc_sec = bfd_get_linker_section (dynobj, name);
13386 if (reloc_sec == NULL)
13388 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
13389 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
13390 if ((sec->flags & SEC_ALLOC) != 0)
13391 flags |= SEC_ALLOC | SEC_LOAD;
13393 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
13394 if (reloc_sec != NULL)
13396 /* _bfd_elf_get_sec_type_attr chooses a section type by
13397 name. Override as it may be wrong, eg. for a user
13398 section named "auto" we'll get ".relauto" which is
13399 seen to be a .rela section. */
13400 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
13401 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment))
13406 elf_section_data (sec)->sreloc = reloc_sec;
13412 /* Copy the ELF symbol type and other attributes for a linker script
13413 assignment from HSRC to HDEST. Generally this should be treated as
13414 if we found a strong non-dynamic definition for HDEST (except that
13415 ld ignores multiple definition errors). */
13417 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
13418 struct bfd_link_hash_entry *hdest,
13419 struct bfd_link_hash_entry *hsrc)
13421 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
13422 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
13423 Elf_Internal_Sym isym;
13425 ehdest->type = ehsrc->type;
13426 ehdest->target_internal = ehsrc->target_internal;
13428 isym.st_other = ehsrc->other;
13429 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
13432 /* Append a RELA relocation REL to section S in BFD. */
13435 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13437 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13438 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
13439 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
13440 bed->s->swap_reloca_out (abfd, rel, loc);
13443 /* Append a REL relocation REL to section S in BFD. */
13446 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
13448 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
13449 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
13450 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
13451 bed->s->swap_reloc_out (abfd, rel, loc);